Search in sources :

Example 1 with SetSession

use of io.prestosql.sql.tree.SetSession in project hetu-core by openlookeng.

the class SetSessionTask method execute.

@Override
public ListenableFuture<?> execute(SetSession statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters, HeuristicIndexerManager heuristicIndexerManager) {
    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 {
        CatalogName catalogName = 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(catalogName, 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, String.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
    propertyMetadata.decode(objectValue);
    stateMachine.addSetSessionProperties(propertyName.toString(), value);
    return immediateFuture(null);
}
Also used : Type(io.prestosql.spi.type.Type) QualifiedName(io.prestosql.sql.tree.QualifiedName) CatalogName(io.prestosql.spi.connector.CatalogName) PrestoException(io.prestosql.spi.PrestoException) SetSession(io.prestosql.sql.tree.SetSession) Session(io.prestosql.Session) SemanticException(io.prestosql.sql.analyzer.SemanticException)

Example 2 with SetSession

use of io.prestosql.sql.tree.SetSession in project hetu-core by openlookeng.

the class TestSqlParser method testSetSession.

@Test
public void testSetSession() {
    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(io.prestosql.sql.tree.SetSession) StringLiteral(io.prestosql.sql.tree.StringLiteral) FunctionCall(io.prestosql.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Example 3 with SetSession

use of io.prestosql.sql.tree.SetSession in project hetu-core by openlookeng.

the class TestSetSessionTask method testSetSessionWithParameters.

private void testSetSessionWithParameters(String property, Expression expression, String expectedValue, List<Expression> parameters) {
    QualifiedName qualifiedPropName = QualifiedName.of(CATALOG_NAME, property);
    QueryStateMachine stateMachine = QueryStateMachine.begin(format("set %s = 'old_value'", qualifiedPropName), Optional.empty(), TEST_SESSION, URI.create("fake://uri"), new ResourceGroupId("test"), new NoOpResourceGroupManager(), false, transactionManager, accessControl, executor, metadata, WarningCollector.NOOP);
    getFutureValue(new SetSessionTask().execute(new SetSession(qualifiedPropName, expression), transactionManager, metadata, accessControl, stateMachine, parameters, new HeuristicIndexerManager(new FileSystemClientManager(), new HetuMetaStoreManager())));
    Map<String, String> sessionProperties = stateMachine.getSetSessionProperties();
    assertEquals(sessionProperties, ImmutableMap.of(qualifiedPropName.toString(), expectedValue));
}
Also used : ResourceGroupId(io.prestosql.spi.resourcegroups.ResourceGroupId) SetSession(io.prestosql.sql.tree.SetSession) QualifiedName(io.prestosql.sql.tree.QualifiedName) HeuristicIndexerManager(io.prestosql.heuristicindex.HeuristicIndexerManager) HetuMetaStoreManager(io.prestosql.metastore.HetuMetaStoreManager) NoOpResourceGroupManager(io.prestosql.execution.resourcegroups.NoOpResourceGroupManager) FileSystemClientManager(io.prestosql.filesystem.FileSystemClientManager)

Aggregations

SetSession (io.prestosql.sql.tree.SetSession)3 QualifiedName (io.prestosql.sql.tree.QualifiedName)2 Session (io.prestosql.Session)1 NoOpResourceGroupManager (io.prestosql.execution.resourcegroups.NoOpResourceGroupManager)1 FileSystemClientManager (io.prestosql.filesystem.FileSystemClientManager)1 HeuristicIndexerManager (io.prestosql.heuristicindex.HeuristicIndexerManager)1 HetuMetaStoreManager (io.prestosql.metastore.HetuMetaStoreManager)1 PrestoException (io.prestosql.spi.PrestoException)1 CatalogName (io.prestosql.spi.connector.CatalogName)1 ResourceGroupId (io.prestosql.spi.resourcegroups.ResourceGroupId)1 Type (io.prestosql.spi.type.Type)1 SemanticException (io.prestosql.sql.analyzer.SemanticException)1 FunctionCall (io.prestosql.sql.tree.FunctionCall)1 StringLiteral (io.prestosql.sql.tree.StringLiteral)1 Test (org.testng.annotations.Test)1