Search in sources :

Example 1 with SqlPath

use of io.trino.sql.SqlPath in project trino by trinodb.

the class SetPathTask method execute.

@Override
public ListenableFuture<Void> execute(SetPath statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Session session = stateMachine.getSession();
    if (!session.getClientCapabilities().contains(ClientCapabilities.PATH.toString())) {
        throw new TrinoException(NOT_SUPPORTED, "SET PATH not supported by client");
    }
    // convert to IR before setting HTTP headers - ensures that the representations of all path objects outside the parser remain consistent
    SqlPath sqlPath = new SqlPath(Optional.of(statement.getPathSpecification().toString()));
    for (SqlPathElement element : sqlPath.getParsedPath()) {
        if (element.getCatalog().isEmpty() && session.getCatalog().isEmpty()) {
            throw semanticException(MISSING_CATALOG_NAME, statement, "Catalog must be specified for each path element when session catalog is not set");
        }
        element.getCatalog().ifPresent(catalog -> {
            String catalogName = catalog.getValue().toLowerCase(ENGLISH);
            getRequiredCatalogHandle(metadata, session, statement, catalogName);
        });
    }
    stateMachine.setSetPath(sqlPath.toString());
    return immediateVoidFuture();
}
Also used : SqlPath(io.trino.sql.SqlPath) TrinoException(io.trino.spi.TrinoException) SqlPathElement(io.trino.sql.SqlPathElement) Session(io.trino.Session)

Example 2 with SqlPath

use of io.trino.sql.SqlPath in project trino by trinodb.

the class QuerySessionSupplier method createSession.

@Override
public Session createSession(QueryId queryId, SessionContext context) {
    Identity identity = context.getIdentity();
    accessControl.checkCanSetUser(identity.getPrincipal(), identity.getUser());
    // authenticated identity is not present for HTTP or if authentication is not setup
    if (context.getAuthenticatedIdentity().isPresent()) {
        Identity authenticatedIdentity = context.getAuthenticatedIdentity().get();
        // only check impersonation if authenticated user is not the same as the explicitly set user
        if (!authenticatedIdentity.getUser().equals(identity.getUser())) {
            // add enabled roles for authenticated identity, so impersonation permissions can be assigned to roles
            authenticatedIdentity = addEnabledRoles(authenticatedIdentity, context.getSelectedRole(), metadata);
            accessControl.checkCanImpersonateUser(authenticatedIdentity, identity.getUser());
        }
    }
    // add the enabled roles
    identity = addEnabledRoles(identity, context.getSelectedRole(), metadata);
    SessionBuilder sessionBuilder = Session.builder(sessionPropertyManager).setQueryId(queryId).setIdentity(identity).setPath(context.getPath().or(() -> defaultPath).map(SqlPath::new)).setSource(context.getSource()).setRemoteUserAddress(context.getRemoteUserAddress()).setUserAgent(context.getUserAgent()).setClientInfo(context.getClientInfo()).setClientTags(context.getClientTags()).setClientCapabilities(context.getClientCapabilities()).setTraceToken(context.getTraceToken()).setResourceEstimates(context.getResourceEstimates()).setProtocolHeaders(context.getProtocolHeaders());
    if (context.getCatalog().isPresent()) {
        sessionBuilder.setCatalog(context.getCatalog());
        sessionBuilder.setSchema(context.getSchema());
    } else {
        defaultCatalog.ifPresent(sessionBuilder::setCatalog);
        defaultSchema.ifPresent(sessionBuilder::setSchema);
    }
    if (forcedSessionTimeZone.isPresent()) {
        sessionBuilder.setTimeZoneKey(forcedSessionTimeZone.get());
    } else {
        String sessionTimeZoneId = context.getSystemProperties().get(TIME_ZONE_ID);
        if (sessionTimeZoneId != null) {
            sessionBuilder.setTimeZoneKey(getTimeZoneKey(sessionTimeZoneId));
        } else {
            sessionBuilder.setTimeZoneKey(context.getTimeZoneId().map(TimeZoneKey::getTimeZoneKey));
        }
    }
    context.getLanguage().ifPresent(s -> sessionBuilder.setLocale(Locale.forLanguageTag(s)));
    for (Entry<String, String> entry : context.getSystemProperties().entrySet()) {
        sessionBuilder.setSystemProperty(entry.getKey(), entry.getValue());
    }
    for (Entry<String, Map<String, String>> catalogProperties : context.getCatalogSessionProperties().entrySet()) {
        String catalog = catalogProperties.getKey();
        for (Entry<String, String> entry : catalogProperties.getValue().entrySet()) {
            sessionBuilder.setCatalogSessionProperty(catalog, entry.getKey(), entry.getValue());
        }
    }
    for (Entry<String, String> preparedStatement : context.getPreparedStatements().entrySet()) {
        sessionBuilder.addPreparedStatement(preparedStatement.getKey(), preparedStatement.getValue());
    }
    if (context.supportClientTransaction()) {
        sessionBuilder.setClientTransactionSupport();
    }
    return sessionBuilder.build();
}
Also used : SqlPath(io.trino.sql.SqlPath) SessionBuilder(io.trino.Session.SessionBuilder) Identity(io.trino.spi.security.Identity) Map(java.util.Map)

Example 3 with SqlPath

use of io.trino.sql.SqlPath in project trino by trinodb.

the class TestQuerySessionSupplier method testSqlPathCreation.

@Test
public void testSqlPathCreation() {
    ImmutableList.Builder<SqlPathElement> correctValues = ImmutableList.builder();
    correctValues.add(new SqlPathElement(Optional.of(new Identifier("normal")), new Identifier("schema")));
    correctValues.add(new SqlPathElement(Optional.of(new Identifier("who.uses.periods")), new Identifier("in.schema.names")));
    correctValues.add(new SqlPathElement(Optional.of(new Identifier("same,deal")), new Identifier("with,commas")));
    correctValues.add(new SqlPathElement(Optional.of(new Identifier("aterrible")), new Identifier("thing!@#$%^&*()")));
    List<SqlPathElement> expected = correctValues.build();
    SqlPath path = new SqlPath(Optional.of("normal.schema," + "\"who.uses.periods\".\"in.schema.names\"," + "\"same,deal\".\"with,commas\"," + "aterrible.\"thing!@#$%^&*()\""));
    assertEquals(path.getParsedPath(), expected);
    assertEquals(path.toString(), Joiner.on(", ").join(expected));
}
Also used : Identifier(io.trino.sql.tree.Identifier) SqlPath(io.trino.sql.SqlPath) ImmutableList(com.google.common.collect.ImmutableList) SqlPathElement(io.trino.sql.SqlPathElement) Test(org.testng.annotations.Test)

Example 4 with SqlPath

use of io.trino.sql.SqlPath in project trino by trinodb.

the class TestSessionFunctions method testCurrentPath.

@Test
public void testCurrentPath() {
    Session session = testSessionBuilder().setPath(new SqlPath(Optional.of("testPath"))).build();
    try (QueryAssertions queryAssertions = new QueryAssertions(session)) {
        assertThat(queryAssertions.query("SELECT CURRENT_PATH")).matches("SELECT CAST('" + session.getPath().toString() + "' AS VARCHAR)");
    }
    Session emptyPathSession = testSessionBuilder().setPath(new SqlPath(Optional.empty())).build();
    try (QueryAssertions queryAssertions = new QueryAssertions(emptyPathSession)) {
        assertThat(queryAssertions.query("SELECT CURRENT_PATH")).matches("VALUES VARCHAR ''");
    }
}
Also used : SqlPath(io.trino.sql.SqlPath) Session(io.trino.Session) Test(org.junit.jupiter.api.Test)

Aggregations

SqlPath (io.trino.sql.SqlPath)4 Session (io.trino.Session)2 SqlPathElement (io.trino.sql.SqlPathElement)2 ImmutableList (com.google.common.collect.ImmutableList)1 SessionBuilder (io.trino.Session.SessionBuilder)1 TrinoException (io.trino.spi.TrinoException)1 Identity (io.trino.spi.security.Identity)1 Identifier (io.trino.sql.tree.Identifier)1 Map (java.util.Map)1 Test (org.junit.jupiter.api.Test)1 Test (org.testng.annotations.Test)1