Search in sources :

Example 1 with SetSession

use of com.facebook.presto.sql.tree.SetSession in project presto by prestodb.

the class SetSessionTask method execute.

@Override
public ListenableFuture<?> execute(SetSession statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters) {
    Session session = stateMachine.getSession();
    QualifiedName propertyName = statement.getName();
    List<String> parts = propertyName.getParts();
    if (parts.size() > 2) {
        throw new SemanticException(INVALID_SESSION_PROPERTY, statement, "Invalid session property '%s'", propertyName);
    }
    // validate the property name
    PropertyMetadata<?> propertyMetadata;
    if (parts.size() == 1) {
        accessControl.checkCanSetSystemSessionProperty(session.getIdentity(), parts.get(0));
        propertyMetadata = metadata.getSessionPropertyManager().getSystemSessionPropertyMetadata(parts.get(0)).orElseThrow(() -> new SemanticException(INVALID_SESSION_PROPERTY, statement, "Session property %s does not exist", statement.getName()));
    } else {
        ConnectorId connectorId = metadata.getCatalogHandle(stateMachine.getSession(), parts.get(0)).orElseThrow(() -> new SemanticException(MISSING_CATALOG, statement, "Catalog %s does not exist", parts.get(0)));
        accessControl.checkCanSetCatalogSessionProperty(session.getRequiredTransactionId(), session.getIdentity(), parts.get(0), parts.get(1));
        propertyMetadata = metadata.getSessionPropertyManager().getConnectorSessionPropertyMetadata(connectorId, parts.get(1)).orElseThrow(() -> new SemanticException(INVALID_SESSION_PROPERTY, statement, "Session property %s does not exist", statement.getName()));
    }
    Type type = propertyMetadata.getSqlType();
    Object objectValue;
    try {
        objectValue = evaluatePropertyValue(statement.getValue(), type, session, metadata, parameters);
    } 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
    stateMachine.addSetSessionProperties(propertyName.toString(), value);
    return immediateFuture(null);
}
Also used : Type(com.facebook.presto.spi.type.Type) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) PrestoException(com.facebook.presto.spi.PrestoException) Session(com.facebook.presto.Session) SetSession(com.facebook.presto.sql.tree.SetSession) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) ConnectorId(com.facebook.presto.connector.ConnectorId)

Example 2 with SetSession

use of com.facebook.presto.sql.tree.SetSession in project presto by prestodb.

the class TestSqlParser method testSetSession.

@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")))));
}
Also used : SetSession(com.facebook.presto.sql.tree.SetSession) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 3 with SetSession

use of com.facebook.presto.sql.tree.SetSession in project presto by prestodb.

the class TestSetSessionTask method testSetSessionWithParameters.

private void testSetSessionWithParameters(Expression expression, String expectedValue, List<Expression> parameters) throws Exception {
    QueryStateMachine stateMachine = QueryStateMachine.begin(new QueryId("query"), "set foo.bar = 'baz'", TEST_SESSION, URI.create("fake://uri"), false, transactionManager, accessControl, executor, metadata);
    getFutureValue(new SetSessionTask().execute(new SetSession(QualifiedName.of(CATALOG_NAME, "bar"), expression), transactionManager, metadata, accessControl, stateMachine, parameters));
    Map<String, String> sessionProperties = stateMachine.getSetSessionProperties();
    assertEquals(sessionProperties, ImmutableMap.of("foo.bar", expectedValue));
}
Also used : SetSession(com.facebook.presto.sql.tree.SetSession) QueryId(com.facebook.presto.spi.QueryId)

Aggregations

SetSession (com.facebook.presto.sql.tree.SetSession)3 Session (com.facebook.presto.Session)1 ConnectorId (com.facebook.presto.connector.ConnectorId)1 PrestoException (com.facebook.presto.spi.PrestoException)1 QueryId (com.facebook.presto.spi.QueryId)1 Type (com.facebook.presto.spi.type.Type)1 SemanticException (com.facebook.presto.sql.analyzer.SemanticException)1 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)1 QualifiedName (com.facebook.presto.sql.tree.QualifiedName)1 StringLiteral (com.facebook.presto.sql.tree.StringLiteral)1 Test (org.testng.annotations.Test)1