Search in sources :

Example 16 with Session

use of io.trino.Session in project trino by trinodb.

the class RenameSchemaTask method execute.

@Override
public ListenableFuture<Void> execute(RenameSchema statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Session session = stateMachine.getSession();
    CatalogSchemaName source = createCatalogSchemaName(session, statement, Optional.of(statement.getSource()));
    CatalogSchemaName target = new CatalogSchemaName(source.getCatalogName(), statement.getTarget().getValue());
    if (!metadata.schemaExists(session, source)) {
        throw semanticException(SCHEMA_NOT_FOUND, statement, "Schema '%s' does not exist", source);
    }
    if (metadata.schemaExists(session, target)) {
        throw semanticException(SCHEMA_ALREADY_EXISTS, statement, "Target schema '%s' already exists", target);
    }
    accessControl.checkCanRenameSchema(session.toSecurityContext(), source, statement.getTarget().getValue());
    metadata.renameSchema(session, source, statement.getTarget().getValue());
    return immediateVoidFuture();
}
Also used : CatalogSchemaName(io.trino.spi.connector.CatalogSchemaName) MetadataUtil.createCatalogSchemaName(io.trino.metadata.MetadataUtil.createCatalogSchemaName) Session(io.trino.Session)

Example 17 with Session

use of io.trino.Session in project trino by trinodb.

the class RevokeRolesTask method execute.

@Override
public ListenableFuture<Void> execute(RevokeRoles statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Session session = stateMachine.getSession();
    Set<String> roles = statement.getRoles().stream().map(role -> role.getValue().toLowerCase(Locale.ENGLISH)).collect(toImmutableSet());
    Set<TrinoPrincipal> grantees = statement.getGrantees().stream().map(MetadataUtil::createPrincipal).collect(toImmutableSet());
    boolean adminOption = statement.isAdminOption();
    Optional<TrinoPrincipal> grantor = statement.getGrantor().map(specification -> createPrincipal(session, specification));
    Optional<String> catalog = processRoleCommandCatalog(metadata, session, statement, statement.getCatalog().map(Identifier::getValue));
    Set<String> specifiedRoles = new LinkedHashSet<>();
    specifiedRoles.addAll(roles);
    grantees.stream().filter(principal -> principal.getType() == ROLE).map(TrinoPrincipal::getName).forEach(specifiedRoles::add);
    if (grantor.isPresent() && grantor.get().getType() == ROLE) {
        specifiedRoles.add(grantor.get().getName());
    }
    for (String role : specifiedRoles) {
        checkRoleExists(session, statement, metadata, role, catalog);
    }
    accessControl.checkCanRevokeRoles(session.toSecurityContext(), roles, grantees, adminOption, grantor, catalog);
    metadata.revokeRoles(session, roles, grantees, adminOption, grantor, catalog);
    return immediateVoidFuture();
}
Also used : Futures.immediateVoidFuture(com.google.common.util.concurrent.Futures.immediateVoidFuture) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) MetadataUtil.checkRoleExists(io.trino.metadata.MetadataUtil.checkRoleExists) MetadataUtil(io.trino.metadata.MetadataUtil) RevokeRoles(io.trino.sql.tree.RevokeRoles) Set(java.util.Set) ROLE(io.trino.spi.security.PrincipalType.ROLE) Inject(javax.inject.Inject) List(java.util.List) AccessControl(io.trino.security.AccessControl) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) MetadataUtil.createPrincipal(io.trino.metadata.MetadataUtil.createPrincipal) Locale(java.util.Locale) Objects.requireNonNull(java.util.Objects.requireNonNull) WarningCollector(io.trino.execution.warnings.WarningCollector) Metadata(io.trino.metadata.Metadata) Optional(java.util.Optional) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Expression(io.trino.sql.tree.Expression) MetadataUtil.processRoleCommandCatalog(io.trino.metadata.MetadataUtil.processRoleCommandCatalog) Identifier(io.trino.sql.tree.Identifier) LinkedHashSet(java.util.LinkedHashSet) Session(io.trino.Session) LinkedHashSet(java.util.LinkedHashSet) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) Session(io.trino.Session)

Example 18 with Session

use of io.trino.Session 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 19 with Session

use of io.trino.Session in project trino by trinodb.

the class SetPropertiesTask method execute.

@Override
public ListenableFuture<Void> execute(SetProperties statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Session session = stateMachine.getSession();
    QualifiedObjectName objectName = createQualifiedObjectName(session, statement, statement.getName());
    if (statement.getType() == TABLE) {
        Map<String, Optional<Object>> properties = tablePropertyManager.getNullableProperties(getRequiredCatalogHandle(plannerContext.getMetadata(), session, statement, objectName.getCatalogName()), statement.getProperties(), session, plannerContext, accessControl, parameterExtractor(statement, parameters), false);
        setTableProperties(statement, objectName, session, properties);
    } else if (statement.getType() == MATERIALIZED_VIEW) {
        Map<String, Optional<Object>> properties = materializedViewPropertyManager.getNullableProperties(getRequiredCatalogHandle(plannerContext.getMetadata(), session, statement, objectName.getCatalogName()), statement.getProperties(), session, plannerContext, accessControl, parameterExtractor(statement, parameters), false);
        setMaterializedViewProperties(statement, objectName, session, properties);
    } else {
        throw semanticException(NOT_SUPPORTED, statement, "Unsupported target type: %s", statement.getType());
    }
    return immediateVoidFuture();
}
Also used : Optional(java.util.Optional) Map(java.util.Map) MetadataUtil.createQualifiedObjectName(io.trino.metadata.MetadataUtil.createQualifiedObjectName) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) Session(io.trino.Session)

Example 20 with Session

use of io.trino.Session in project trino by trinodb.

the class SetSchemaAuthorizationTask method execute.

@Override
public ListenableFuture<Void> execute(SetSchemaAuthorization statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Session session = stateMachine.getSession();
    CatalogSchemaName source = createCatalogSchemaName(session, statement, Optional.of(statement.getSource()));
    if (!metadata.schemaExists(session, source)) {
        throw semanticException(SCHEMA_NOT_FOUND, statement, "Schema '%s' does not exist", source);
    }
    TrinoPrincipal principal = createPrincipal(statement.getPrincipal());
    checkRoleExists(session, statement, metadata, principal, Optional.of(source.getCatalogName()).filter(catalog -> metadata.isCatalogManagedSecurity(session, catalog)));
    accessControl.checkCanSetSchemaAuthorization(session.toSecurityContext(), source, principal);
    metadata.setSchemaAuthorization(session, source, principal);
    return immediateVoidFuture();
}
Also used : Futures.immediateVoidFuture(com.google.common.util.concurrent.Futures.immediateVoidFuture) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) MetadataUtil.checkRoleExists(io.trino.metadata.MetadataUtil.checkRoleExists) Inject(javax.inject.Inject) List(java.util.List) AccessControl(io.trino.security.AccessControl) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) MetadataUtil.createPrincipal(io.trino.metadata.MetadataUtil.createPrincipal) Objects.requireNonNull(java.util.Objects.requireNonNull) WarningCollector(io.trino.execution.warnings.WarningCollector) CatalogSchemaName(io.trino.spi.connector.CatalogSchemaName) Metadata(io.trino.metadata.Metadata) SetSchemaAuthorization(io.trino.sql.tree.SetSchemaAuthorization) Optional(java.util.Optional) Expression(io.trino.sql.tree.Expression) MetadataUtil.createCatalogSchemaName(io.trino.metadata.MetadataUtil.createCatalogSchemaName) SemanticExceptions.semanticException(io.trino.sql.analyzer.SemanticExceptions.semanticException) Session(io.trino.Session) SCHEMA_NOT_FOUND(io.trino.spi.StandardErrorCode.SCHEMA_NOT_FOUND) CatalogSchemaName(io.trino.spi.connector.CatalogSchemaName) MetadataUtil.createCatalogSchemaName(io.trino.metadata.MetadataUtil.createCatalogSchemaName) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) Session(io.trino.Session)

Aggregations

Session (io.trino.Session)483 Test (org.testng.annotations.Test)303 Optional (java.util.Optional)103 List (java.util.List)91 ImmutableList (com.google.common.collect.ImmutableList)82 ImmutableMap (com.google.common.collect.ImmutableMap)72 Map (java.util.Map)70 BaseConnectorTest (io.trino.testing.BaseConnectorTest)67 Objects.requireNonNull (java.util.Objects.requireNonNull)65 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)61 HiveQueryRunner.createBucketedSession (io.trino.plugin.hive.HiveQueryRunner.createBucketedSession)60 ImmutableSet (com.google.common.collect.ImmutableSet)51 Set (java.util.Set)50 ColumnHandle (io.trino.spi.connector.ColumnHandle)49 Metadata (io.trino.metadata.Metadata)47 QualifiedObjectName (io.trino.metadata.QualifiedObjectName)46 SchemaTableName (io.trino.spi.connector.SchemaTableName)45 TestingSession (io.trino.testing.TestingSession)45 TableHandle (io.trino.metadata.TableHandle)42 Type (io.trino.spi.type.Type)41