Search in sources :

Example 46 with Metadata

use of io.trino.metadata.Metadata in project trino by trinodb.

the class BaseHiveConnectorTest method getHiveInsertTableHandle.

private HiveInsertTableHandle getHiveInsertTableHandle(Session session, String tableName) {
    Metadata metadata = getDistributedQueryRunner().getCoordinator().getMetadata();
    return transaction(getQueryRunner().getTransactionManager(), getQueryRunner().getAccessControl()).execute(session, transactionSession -> {
        QualifiedObjectName objectName = new QualifiedObjectName(catalog, TPCH_SCHEMA, tableName);
        Optional<TableHandle> handle = metadata.getTableHandle(transactionSession, objectName);
        List<ColumnHandle> columns = ImmutableList.copyOf(metadata.getColumnHandles(transactionSession, handle.get()).values());
        InsertTableHandle insertTableHandle = metadata.beginInsert(transactionSession, handle.get(), columns);
        HiveInsertTableHandle hiveInsertTableHandle = (HiveInsertTableHandle) insertTableHandle.getConnectorHandle();
        metadata.finishInsert(transactionSession, insertTableHandle, ImmutableList.of(), ImmutableList.of());
        return hiveInsertTableHandle;
    });
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) TableMetadata(io.trino.metadata.TableMetadata) Metadata(io.trino.metadata.Metadata) InsertTableHandle(io.trino.metadata.InsertTableHandle) TableHandle(io.trino.metadata.TableHandle) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) InsertTableHandle(io.trino.metadata.InsertTableHandle)

Example 47 with Metadata

use of io.trino.metadata.Metadata in project trino by trinodb.

the class BaseHiveConnectorTest method getHiveTableProperty.

private Object getHiveTableProperty(String tableName, Function<HiveTableHandle, Object> propertyGetter) {
    Session session = getSession();
    Metadata metadata = getDistributedQueryRunner().getCoordinator().getMetadata();
    return transaction(getQueryRunner().getTransactionManager(), getQueryRunner().getAccessControl()).readOnly().execute(session, transactionSession -> {
        QualifiedObjectName name = new QualifiedObjectName(catalog, TPCH_SCHEMA, tableName);
        TableHandle table = metadata.getTableHandle(transactionSession, name).orElseThrow(() -> new AssertionError("table not found: " + name));
        table = metadata.applyFilter(transactionSession, table, Constraint.alwaysTrue()).orElseThrow(() -> new AssertionError("applyFilter did not return a result")).getHandle();
        return propertyGetter.apply((HiveTableHandle) table.getConnectorHandle());
    });
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) TableMetadata(io.trino.metadata.TableMetadata) Metadata(io.trino.metadata.Metadata) InsertTableHandle(io.trino.metadata.InsertTableHandle) TableHandle(io.trino.metadata.TableHandle) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) HiveQueryRunner.createBucketedSession(io.trino.plugin.hive.HiveQueryRunner.createBucketedSession) Session(io.trino.Session)

Example 48 with Metadata

use of io.trino.metadata.Metadata in project trino by trinodb.

the class SetRoleTask method execute.

@Override
public ListenableFuture<Void> execute(SetRole statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Session session = stateMachine.getSession();
    Optional<String> catalog = processRoleCommandCatalog(metadata, session, statement, statement.getCatalog().map(Identifier::getValue));
    if (statement.getType() == SetRole.Type.ROLE) {
        String role = statement.getRole().map(c -> c.getValue().toLowerCase(ENGLISH)).orElseThrow();
        if (!metadata.roleExists(session, role, catalog)) {
            throw semanticException(ROLE_NOT_FOUND, statement, "Role '%s' does not exist", role);
        }
        if (catalog.isPresent()) {
            accessControl.checkCanSetCatalogRole(SecurityContext.of(session), role, catalog.get());
        } else {
            Set<RoleGrant> roleGrants = metadata.listApplicableRoles(session, new TrinoPrincipal(USER, session.getUser()), Optional.empty());
            if (roleGrants.stream().map(RoleGrant::getRoleName).noneMatch(role::equals)) {
                denySetRole(role);
            }
        }
    }
    SelectedRole.Type type = toSelectedRoleType(statement.getType());
    stateMachine.addSetRole(catalog.orElse("system"), new SelectedRole(type, statement.getRole().map(c -> c.getValue().toLowerCase(ENGLISH))));
    return immediateVoidFuture();
}
Also used : Futures.immediateVoidFuture(com.google.common.util.concurrent.Futures.immediateVoidFuture) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) USER(io.trino.spi.security.PrincipalType.USER) Set(java.util.Set) RoleGrant(io.trino.spi.security.RoleGrant) AccessDeniedException.denySetRole(io.trino.spi.security.AccessDeniedException.denySetRole) Inject(javax.inject.Inject) SelectedRole(io.trino.spi.security.SelectedRole) List(java.util.List) AccessControl(io.trino.security.AccessControl) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) SetRole(io.trino.sql.tree.SetRole) Objects.requireNonNull(java.util.Objects.requireNonNull) WarningCollector(io.trino.execution.warnings.WarningCollector) Metadata(io.trino.metadata.Metadata) Optional(java.util.Optional) Expression(io.trino.sql.tree.Expression) SecurityContext(io.trino.security.SecurityContext) MetadataUtil.processRoleCommandCatalog(io.trino.metadata.MetadataUtil.processRoleCommandCatalog) SemanticExceptions.semanticException(io.trino.sql.analyzer.SemanticExceptions.semanticException) ENGLISH(java.util.Locale.ENGLISH) Identifier(io.trino.sql.tree.Identifier) ROLE_NOT_FOUND(io.trino.spi.StandardErrorCode.ROLE_NOT_FOUND) Session(io.trino.Session) RoleGrant(io.trino.spi.security.RoleGrant) SelectedRole(io.trino.spi.security.SelectedRole) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) Session(io.trino.Session)

Example 49 with Metadata

use of io.trino.metadata.Metadata in project trino by trinodb.

the class AbstractOperatorBenchmark method getColumnTypes.

protected final List<Type> getColumnTypes(String tableName, String... columnNames) {
    checkState(session.getCatalog().isPresent(), "catalog not set");
    checkState(session.getSchema().isPresent(), "schema not set");
    // look up the table
    Metadata metadata = localQueryRunner.getMetadata();
    QualifiedObjectName qualifiedTableName = new QualifiedObjectName(session.getCatalog().get(), session.getSchema().get(), tableName);
    TableHandle tableHandle = metadata.getTableHandle(session, qualifiedTableName).orElseThrow(() -> new IllegalArgumentException(format("Table '%s' does not exist", qualifiedTableName)));
    Map<String, ColumnHandle> allColumnHandles = metadata.getColumnHandles(session, tableHandle);
    return Arrays.stream(columnNames).map(allColumnHandles::get).map(columnHandle -> metadata.getColumnMetadata(session, tableHandle, columnHandle).getType()).collect(toImmutableList());
}
Also used : QueryId(io.trino.spi.QueryId) EMPTY(io.trino.spi.connector.DynamicFilter.EMPTY) Arrays(java.util.Arrays) SpillSpaceTracker(io.trino.spiller.SpillSpaceTracker) SymbolAllocator(io.trino.sql.planner.SymbolAllocator) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) TaskStateMachine(io.trino.execution.TaskStateMachine) OperatorContext(io.trino.operator.OperatorContext) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) PageSourceOperator(io.trino.operator.PageSourceOperator) PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) Map(java.util.Map) ConnectorPageSource(io.trino.spi.connector.ConnectorPageSource) MemoryPool(io.trino.memory.MemoryPool) Operator(io.trino.operator.Operator) ImmutableMap(com.google.common.collect.ImmutableMap) ResolvedFunction(io.trino.metadata.ResolvedFunction) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) PageFunctionCompiler(io.trino.sql.gen.PageFunctionCompiler) SplitSource(io.trino.split.SplitSource) CpuTimer(io.airlift.stats.CpuTimer) QueryContext(io.trino.memory.QueryContext) TaskId(io.trino.execution.TaskId) PageProjection(io.trino.operator.project.PageProjection) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Constraint.alwaysTrue(io.trino.spi.connector.Constraint.alwaysTrue) DataSize(io.airlift.units.DataSize) List(java.util.List) NOT_PARTITIONED(io.trino.spi.connector.NotPartitionedPartitionHandle.NOT_PARTITIONED) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) DriverContext(io.trino.operator.DriverContext) BIGINT(io.trino.spi.type.BigintType.BIGINT) Split(io.trino.metadata.Split) DynamicFilter(io.trino.spi.connector.DynamicFilter) Optional(java.util.Optional) Expression(io.trino.sql.tree.Expression) SystemSessionProperties.getFilterAndProjectMinOutputPageSize(io.trino.SystemSessionProperties.getFilterAndProjectMinOutputPageSize) Session(io.trino.Session) TypeAnalyzer.createTestingTypeAnalyzer(io.trino.sql.planner.TypeAnalyzer.createTestingTypeAnalyzer) SqlToRowExpressionTranslator.translate(io.trino.sql.relational.SqlToRowExpressionTranslator.translate) Iterables(com.google.common.collect.Iterables) StageId(io.trino.execution.StageId) AllowAllAccessControl(io.trino.security.AllowAllAccessControl) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) MEGABYTE(io.airlift.units.DataSize.Unit.MEGABYTE) Type(io.trino.spi.type.Type) SystemSessionProperties.getFilterAndProjectMinOutputPageRowCount(io.trino.SystemSessionProperties.getFilterAndProjectMinOutputPageRowCount) ArrayList(java.util.ArrayList) HashGenerationOptimizer(io.trino.sql.planner.optimizations.HashGenerationOptimizer) GIGABYTE(io.airlift.units.DataSize.Unit.GIGABYTE) ImmutableList(com.google.common.collect.ImmutableList) TaskStats(io.trino.operator.TaskStats) InputPageProjection(io.trino.operator.project.InputPageProjection) Verify.verify(com.google.common.base.Verify.verify) UNGROUPED_SCHEDULING(io.trino.spi.connector.ConnectorSplitManager.SplitSchedulingStrategy.UNGROUPED_SCHEDULING) NodeRef(io.trino.sql.tree.NodeRef) LocalQueryRunner(io.trino.testing.LocalQueryRunner) Objects.requireNonNull(java.util.Objects.requireNonNull) TaskContext(io.trino.operator.TaskContext) PageProcessor(io.trino.operator.project.PageProcessor) ColumnHandle(io.trino.spi.connector.ColumnHandle) Lifespan(io.trino.execution.Lifespan) OperatorFactory(io.trino.operator.OperatorFactory) Symbol(io.trino.sql.planner.Symbol) CpuDuration(io.airlift.stats.CpuTimer.CpuDuration) AggregationMetadata(io.trino.operator.aggregation.AggregationMetadata) MoreFutures.getFutureValue(io.airlift.concurrent.MoreFutures.getFutureValue) FilterAndProjectOperator(io.trino.operator.FilterAndProjectOperator) QualifiedName(io.trino.sql.tree.QualifiedName) TableHandle(io.trino.metadata.TableHandle) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) RowExpression(io.trino.sql.relational.RowExpression) Metadata(io.trino.metadata.Metadata) TypeProvider(io.trino.sql.planner.TypeProvider) TransactionId(io.trino.transaction.TransactionId) Driver(io.trino.operator.Driver) SECONDS(java.util.concurrent.TimeUnit.SECONDS) TestingGcMonitor(io.airlift.stats.TestingGcMonitor) ColumnHandle(io.trino.spi.connector.ColumnHandle) AggregationMetadata(io.trino.operator.aggregation.AggregationMetadata) Metadata(io.trino.metadata.Metadata) TableHandle(io.trino.metadata.TableHandle) QualifiedObjectName(io.trino.metadata.QualifiedObjectName)

Example 50 with Metadata

use of io.trino.metadata.Metadata in project trino by trinodb.

the class MemoryLocalQueryRunner method dropTable.

public void dropTable(String tableName) {
    Session session = localQueryRunner.getDefaultSession();
    Metadata metadata = localQueryRunner.getMetadata();
    Optional<TableHandle> tableHandle = metadata.getTableHandle(session, QualifiedObjectName.valueOf(tableName));
    assertTrue(tableHandle.isPresent(), "Table " + tableName + " does not exist");
    metadata.dropTable(session, tableHandle.get());
}
Also used : Metadata(io.trino.metadata.Metadata) TableHandle(io.trino.metadata.TableHandle) Session(io.trino.Session)

Aggregations

Metadata (io.trino.metadata.Metadata)50 Session (io.trino.Session)35 List (java.util.List)34 Optional (java.util.Optional)31 Objects.requireNonNull (java.util.Objects.requireNonNull)30 PlanNode (io.trino.sql.planner.plan.PlanNode)22 Expression (io.trino.sql.tree.Expression)21 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)20 Set (java.util.Set)19 Preconditions.checkState (com.google.common.base.Preconditions.checkState)18 Map (java.util.Map)16 ImmutableList (com.google.common.collect.ImmutableList)15 MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)14 StatsProvider (io.trino.cost.StatsProvider)14 QualifiedObjectName (io.trino.metadata.QualifiedObjectName)14 Symbol (io.trino.sql.planner.Symbol)14 ImmutableMap (com.google.common.collect.ImmutableMap)13 ImmutableSet (com.google.common.collect.ImmutableSet)13 TableHandle (io.trino.metadata.TableHandle)13 NO_MATCH (io.trino.sql.planner.assertions.MatchResult.NO_MATCH)13