@Override public Void visitSetSession(SetSession node, Integer indent) { builder.append("SET SESSION ") .append(node.getName()) .append(" = ") .append(formatExpression(node.getValue(), parameters, indent)); return null; }
@Override public CompletableFuture<?> execute(SetSession statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine) { Session session = stateMachine.getSession(); QualifiedName propertyName = statement.getName(); if (propertyName.getParts().size() > 2) { throw new SemanticException(INVALID_SESSION_PROPERTY, statement, "Invalid session property '%s'", propertyName); } PropertyMetadata<?> propertyMetadata = metadata.getSessionPropertyManager().getSessionPropertyMetadata(propertyName.toString()); if (propertyName.getParts().size() == 1) { accessControl.checkCanSetSystemSessionProperty(session.getIdentity(), propertyName.getParts().get(0)); } else if (propertyName.getParts().size() == 2) { accessControl.checkCanSetCatalogSessionProperty(session.getIdentity(), propertyName.getParts().get(0), propertyName.getParts().get(1)); } Type type = propertyMetadata.getSqlType(); Object objectValue; try { objectValue = evaluatePropertyValue(statement.getValue(), type, session, metadata); } catch (SemanticException e) { throw new PrestoException(StandardErrorCode.INVALID_SESSION_PROPERTY, format("Unable to set session property '%s' to '%s': %s", propertyName, statement.getValue(), e.getMessage())); } String value = serializeSessionProperty(type, objectValue); // verify the SQL value can be decoded by the property metadata.getSessionPropertyManager().decodeProperty(propertyName.toString(), value, propertyMetadata.getJavaType()); stateMachine.addSetSessionProperties(propertyName.toString(), value); return completedFuture(null); }
private void testSetSession(Expression expression, String expectedValue) throws Exception { TransactionManager transactionManager = createTestTransactionManager(); QueryStateMachine stateMachine = QueryStateMachine.begin(new QueryId("query"), "set foo.bar = 'baz'", TEST_SESSION, URI.create("fake://uri"), false, transactionManager, executor); new SetSessionTask().execute(new SetSession(QualifiedName.of("foo", "bar"), expression), transactionManager, metadata, new AllowAllAccessControl(), stateMachine).join(); Map<String, String> sessionProperties = stateMachine.getSetSessionProperties(); assertEquals(sessionProperties, ImmutableMap.of("foo.bar", expectedValue)); }
@Override public Void visitSetSession(SetSession node, Integer context) { builder.append("SET SESSION ") .append(node.getName()) .append(" = ") .append(formatExpression(node.getValue())); return null; }
@Test public void testSetSession() throws Exception { assertStatement("SET SESSION foo = 'bar'", new SetSession(QualifiedName.of("foo"), new StringLiteral("bar"))); assertStatement("SET SESSION foo.bar = 'baz'", new SetSession(QualifiedName.of("foo", "bar"), new StringLiteral("baz"))); assertStatement("SET SESSION foo.bar.boo = 'baz'", new SetSession(QualifiedName.of("foo", "bar", "boo"), new StringLiteral("baz"))); assertStatement("SET SESSION foo.bar = 'ban' || 'ana'", new SetSession( QualifiedName.of("foo", "bar"), new FunctionCall(QualifiedName.of("concat"), ImmutableList.of( new StringLiteral("ban"), new StringLiteral("ana"))))); }
@Override public Node visitSetSession(SqlBaseParser.SetSessionContext context) { return new SetSession(getLocation(context), getQualifiedName(context.qualifiedName()), (Expression) visit(context.expression())); }