Search in sources :

Example 1 with SetSession

use of io.trino.sql.tree.SetSession in project trino by trinodb.

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.trino.sql.tree.SetSession) StringLiteral(io.trino.sql.tree.StringLiteral) FunctionCall(io.trino.sql.tree.FunctionCall) Test(org.junit.jupiter.api.Test)

Example 2 with SetSession

use of io.trino.sql.tree.SetSession in project trino by trinodb.

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(Optional.empty(), format("set %s = 'old_value'", qualifiedPropName), Optional.empty(), TEST_SESSION, URI.create("fake://uri"), new ResourceGroupId("test"), false, transactionManager, accessControl, executor, metadata, WarningCollector.NOOP, Optional.empty());
    getFutureValue(new SetSessionTask(plannerContext, accessControl, sessionPropertyManager).execute(new SetSession(qualifiedPropName, expression), stateMachine, parameters, WarningCollector.NOOP));
    Map<String, String> sessionProperties = stateMachine.getSetSessionProperties();
    assertEquals(sessionProperties, ImmutableMap.of(qualifiedPropName.toString(), expectedValue));
}
Also used : ResourceGroupId(io.trino.spi.resourcegroups.ResourceGroupId) SetSession(io.trino.sql.tree.SetSession) QualifiedName(io.trino.sql.tree.QualifiedName)

Example 3 with SetSession

use of io.trino.sql.tree.SetSession in project trino by trinodb.

the class SetSessionTask method execute.

@Override
public ListenableFuture<Void> execute(SetSession statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Session session = stateMachine.getSession();
    QualifiedName propertyName = statement.getName();
    List<String> parts = propertyName.getParts();
    if (parts.size() > 2) {
        throw 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 = sessionPropertyManager.getSystemSessionPropertyMetadata(parts.get(0)).orElseThrow(() -> semanticException(INVALID_SESSION_PROPERTY, statement, "Session property '%s' does not exist", statement.getName()));
    } else {
        CatalogName catalogName = plannerContext.getMetadata().getCatalogHandle(stateMachine.getSession(), parts.get(0)).orElseThrow(() -> semanticException(CATALOG_NOT_FOUND, statement, "Catalog '%s' does not exist", parts.get(0)));
        accessControl.checkCanSetCatalogSessionProperty(SecurityContext.of(session), parts.get(0), parts.get(1));
        propertyMetadata = sessionPropertyManager.getConnectorSessionPropertyMetadata(catalogName, parts.get(1)).orElseThrow(() -> 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, plannerContext, accessControl, parameterExtractor(statement, parameters));
    } catch (TrinoException e) {
        throw new TrinoException(INVALID_SESSION_PROPERTY, format("Unable to set session property '%s' to '%s': %s", propertyName, statement.getValue(), e.getRawMessage()));
    }
    String value = serializeSessionProperty(type, objectValue);
    // verify the SQL value can be decoded by the property
    try {
        propertyMetadata.decode(objectValue);
    } catch (RuntimeException e) {
        throw semanticException(INVALID_SESSION_PROPERTY, statement, e.getMessage());
    }
    stateMachine.addSetSessionProperties(propertyName.toString(), value);
    return immediateVoidFuture();
}
Also used : Type(io.trino.spi.type.Type) QualifiedName(io.trino.sql.tree.QualifiedName) TrinoException(io.trino.spi.TrinoException) CatalogName(io.trino.connector.CatalogName) SetSession(io.trino.sql.tree.SetSession) Session(io.trino.Session)

Example 4 with SetSession

use of io.trino.sql.tree.SetSession in project trino by trinodb.

the class TestSqlParser method testSessionIdentifiers.

@Test
public void testSessionIdentifiers() {
    assertStatement("SET SESSION \"foo-bar\".baz = 'x'", new SetSession(QualifiedName.of("foo-bar", "baz"), new StringLiteral("x")));
    assertStatementIsInvalid("SET SESSION foo-bar.name = 'value'").withMessage("line 1:16: mismatched input '-'. Expecting: '.', '='");
    assertStatement("RESET SESSION \"foo-bar\".baz", new ResetSession(QualifiedName.of("foo-bar", "baz")));
    assertStatementIsInvalid("RESET SESSION foo-bar.name").withMessage("line 1:18: mismatched input '-'. Expecting: '.', <EOF>");
}
Also used : SetSession(io.trino.sql.tree.SetSession) StringLiteral(io.trino.sql.tree.StringLiteral) ResetSession(io.trino.sql.tree.ResetSession) Test(org.junit.jupiter.api.Test)

Aggregations

SetSession (io.trino.sql.tree.SetSession)4 QualifiedName (io.trino.sql.tree.QualifiedName)2 StringLiteral (io.trino.sql.tree.StringLiteral)2 Test (org.junit.jupiter.api.Test)2 Session (io.trino.Session)1 CatalogName (io.trino.connector.CatalogName)1 TrinoException (io.trino.spi.TrinoException)1 ResourceGroupId (io.trino.spi.resourcegroups.ResourceGroupId)1 Type (io.trino.spi.type.Type)1 FunctionCall (io.trino.sql.tree.FunctionCall)1 ResetSession (io.trino.sql.tree.ResetSession)1