Search in sources :

Example 71 with QualifiedObjectName

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

the class AbstractOperatorBenchmark method createTableScanOperator.

protected final OperatorFactory createTableScanOperator(int operatorId, PlanNodeId planNodeId, String tableName, String... columnNames) {
    checkArgument(session.getCatalog().isPresent(), "catalog not set");
    checkArgument(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).orElse(null);
    checkArgument(tableHandle != null, "Table '%s' does not exist", qualifiedTableName);
    // lookup the columns
    Map<String, ColumnHandle> allColumnHandles = metadata.getColumnHandles(session, tableHandle);
    ImmutableList.Builder<ColumnHandle> columnHandlesBuilder = ImmutableList.builder();
    for (String columnName : columnNames) {
        ColumnHandle columnHandle = allColumnHandles.get(columnName);
        checkArgument(columnHandle != null, "Table '%s' does not have a column '%s'", tableName, columnName);
        columnHandlesBuilder.add(columnHandle);
    }
    List<ColumnHandle> columnHandles = columnHandlesBuilder.build();
    // get the split for this table
    Split split = getLocalQuerySplit(session, tableHandle);
    return new OperatorFactory() {

        @Override
        public Operator createOperator(DriverContext driverContext) {
            OperatorContext operatorContext = driverContext.addOperatorContext(operatorId, planNodeId, "BenchmarkSource");
            ConnectorPageSource pageSource = localQueryRunner.getPageSourceManager().createPageSource(session, split, tableHandle, columnHandles, DynamicFilter.EMPTY);
            return new PageSourceOperator(pageSource, operatorContext);
        }

        @Override
        public void noMoreOperators() {
        }

        @Override
        public OperatorFactory duplicate() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) DriverContext(io.trino.operator.DriverContext) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) AggregationMetadata(io.trino.operator.aggregation.AggregationMetadata) Metadata(io.trino.metadata.Metadata) ConnectorPageSource(io.trino.spi.connector.ConnectorPageSource) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) PageSourceOperator(io.trino.operator.PageSourceOperator) OperatorFactory(io.trino.operator.OperatorFactory) OperatorContext(io.trino.operator.OperatorContext) TableHandle(io.trino.metadata.TableHandle) Split(io.trino.metadata.Split)

Example 72 with QualifiedObjectName

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

the class ApplyTableScanRedirection method apply.

@Override
public Result apply(TableScanNode scanNode, Captures captures, Context context) {
    Optional<TableScanRedirectApplicationResult> tableScanRedirectApplicationResult = plannerContext.getMetadata().applyTableScanRedirect(context.getSession(), scanNode.getTable());
    if (tableScanRedirectApplicationResult.isEmpty()) {
        return Result.empty();
    }
    CatalogSchemaTableName destinationTable = tableScanRedirectApplicationResult.get().getDestinationTable();
    QualifiedObjectName destinationObjectName = convertFromSchemaTableName(destinationTable.getCatalogName()).apply(destinationTable.getSchemaTableName());
    Optional<QualifiedObjectName> redirectedObjectName = plannerContext.getMetadata().getRedirectionAwareTableHandle(context.getSession(), destinationObjectName).getRedirectedTableName();
    redirectedObjectName.ifPresent(name -> {
        throw new TrinoException(NOT_SUPPORTED, format("Further redirection of destination table '%s' to '%s' is not supported", destinationObjectName, name));
    });
    TableMetadata tableMetadata = plannerContext.getMetadata().getTableMetadata(context.getSession(), scanNode.getTable());
    CatalogSchemaTableName sourceTable = new CatalogSchemaTableName(tableMetadata.getCatalogName().getCatalogName(), tableMetadata.getTable());
    if (destinationTable.equals(sourceTable)) {
        return Result.empty();
    }
    Optional<TableHandle> destinationTableHandle = plannerContext.getMetadata().getTableHandle(context.getSession(), convertFromSchemaTableName(destinationTable.getCatalogName()).apply(destinationTable.getSchemaTableName()));
    if (destinationTableHandle.isEmpty()) {
        throw new TrinoException(TABLE_NOT_FOUND, format("Destination table %s from table scan redirection not found", destinationTable));
    }
    Map<ColumnHandle, String> columnMapping = tableScanRedirectApplicationResult.get().getDestinationColumns();
    Map<String, ColumnHandle> destinationColumnHandles = plannerContext.getMetadata().getColumnHandles(context.getSession(), destinationTableHandle.get());
    ImmutableMap.Builder<Symbol, Cast> casts = ImmutableMap.builder();
    ImmutableMap.Builder<Symbol, ColumnHandle> newAssignmentsBuilder = ImmutableMap.builder();
    for (Map.Entry<Symbol, ColumnHandle> assignment : scanNode.getAssignments().entrySet()) {
        String destinationColumn = columnMapping.get(assignment.getValue());
        if (destinationColumn == null) {
            throw new TrinoException(COLUMN_NOT_FOUND, format("Did not find mapping for source column %s in table scan redirection", assignment.getValue()));
        }
        ColumnHandle destinationColumnHandle = destinationColumnHandles.get(destinationColumn);
        if (destinationColumnHandle == null) {
            throw new TrinoException(COLUMN_NOT_FOUND, format("Did not find handle for column %s in destination table %s", destinationColumn, destinationTable));
        }
        // insert ts if redirected types don't match source types
        Type sourceType = context.getSymbolAllocator().getTypes().get(assignment.getKey());
        Type redirectedType = plannerContext.getMetadata().getColumnMetadata(context.getSession(), destinationTableHandle.get(), destinationColumnHandle).getType();
        if (!sourceType.equals(redirectedType)) {
            Symbol redirectedSymbol = context.getSymbolAllocator().newSymbol(destinationColumn, redirectedType);
            Cast cast = getCast(context.getSession(), destinationTable, destinationColumn, redirectedType, redirectedSymbol, sourceTable, assignment.getValue(), sourceType);
            casts.put(assignment.getKey(), cast);
            newAssignmentsBuilder.put(redirectedSymbol, destinationColumnHandle);
        } else {
            newAssignmentsBuilder.put(assignment.getKey(), destinationColumnHandle);
        }
    }
    TupleDomain<String> requiredFilter = tableScanRedirectApplicationResult.get().getFilter();
    if (requiredFilter.isAll()) {
        ImmutableMap<Symbol, ColumnHandle> newAssignments = newAssignmentsBuilder.buildOrThrow();
        return Result.ofPlanNode(applyProjection(context.getIdAllocator(), ImmutableSet.copyOf(scanNode.getOutputSymbols()), casts.buildOrThrow(), new TableScanNode(scanNode.getId(), destinationTableHandle.get(), ImmutableList.copyOf(newAssignments.keySet()), newAssignments, TupleDomain.all(), // Use table statistics from destination table
        Optional.empty(), scanNode.isUpdateTarget(), Optional.empty())));
    }
    Map<ColumnHandle, Symbol> inverseAssignments = ImmutableBiMap.copyOf(scanNode.getAssignments()).inverse();
    Map<String, ColumnHandle> inverseColumnsMapping = ImmutableBiMap.copyOf(columnMapping).inverse();
    TupleDomain<Symbol> transformedConstraint = requiredFilter.transformKeys(destinationColumn -> {
        ColumnHandle sourceColumnHandle = inverseColumnsMapping.get(destinationColumn);
        if (sourceColumnHandle == null) {
            throw new TrinoException(COLUMN_NOT_FOUND, format("Did not find mapping for destination column %s in table scan redirection", destinationColumn));
        }
        Symbol symbol = inverseAssignments.get(sourceColumnHandle);
        if (symbol != null) {
            // domain symbol should already be mapped in redirected table scan
            return symbol;
        }
        // Column pruning after predicate is pushed into table scan can remove assignments for filter columns from the scan node
        Type domainType = requiredFilter.getDomains().get().get(destinationColumn).getType();
        symbol = context.getSymbolAllocator().newSymbol(destinationColumn, domainType);
        ColumnHandle destinationColumnHandle = destinationColumnHandles.get(destinationColumn);
        if (destinationColumnHandle == null) {
            throw new TrinoException(COLUMN_NOT_FOUND, format("Did not find handle for column %s in destination table %s", destinationColumn, destinationTable));
        }
        // insert casts if redirected types don't match domain types
        Type redirectedType = plannerContext.getMetadata().getColumnMetadata(context.getSession(), destinationTableHandle.get(), destinationColumnHandle).getType();
        if (!domainType.equals(redirectedType)) {
            Symbol redirectedSymbol = context.getSymbolAllocator().newSymbol(destinationColumn, redirectedType);
            Cast cast = getCast(context.getSession(), destinationTable, destinationColumn, redirectedType, redirectedSymbol, sourceTable, sourceColumnHandle, domainType);
            casts.put(symbol, cast);
            newAssignmentsBuilder.put(redirectedSymbol, destinationColumnHandle);
        } else {
            newAssignmentsBuilder.put(symbol, destinationColumnHandle);
        }
        return symbol;
    });
    Map<Symbol, ColumnHandle> newAssignments = newAssignmentsBuilder.buildOrThrow();
    TableScanNode newScanNode = new TableScanNode(scanNode.getId(), destinationTableHandle.get(), ImmutableList.copyOf(newAssignments.keySet()), newAssignments, TupleDomain.all(), // Use table statistics from destination table
    Optional.empty(), scanNode.isUpdateTarget(), Optional.empty());
    DomainTranslator domainTranslator = new DomainTranslator(plannerContext);
    FilterNode filterNode = new FilterNode(context.getIdAllocator().getNextId(), applyProjection(context.getIdAllocator(), newAssignments.keySet(), casts.buildOrThrow(), newScanNode), domainTranslator.toPredicate(context.getSession(), transformedConstraint));
    return Result.ofPlanNode(applyProjection(context.getIdAllocator(), ImmutableSet.copyOf(scanNode.getOutputSymbols()), ImmutableMap.of(), filterNode));
}
Also used : Cast(io.trino.sql.tree.Cast) TableScanRedirectApplicationResult(io.trino.spi.connector.TableScanRedirectApplicationResult) Symbol(io.trino.sql.planner.Symbol) FilterNode(io.trino.sql.planner.plan.FilterNode) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) DomainTranslator(io.trino.sql.planner.DomainTranslator) TableMetadata(io.trino.metadata.TableMetadata) ColumnHandle(io.trino.spi.connector.ColumnHandle) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) ImmutableMap(com.google.common.collect.ImmutableMap) Type(io.trino.spi.type.Type) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) TableScanNode(io.trino.sql.planner.plan.TableScanNode) TrinoException(io.trino.spi.TrinoException) TableHandle(io.trino.metadata.TableHandle) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 73 with QualifiedObjectName

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

the class TestRenameViewTask method testRenameViewOnTable.

@Test
public void testRenameViewOnTable() {
    QualifiedObjectName tableName = qualifiedObjectName("existing_table");
    metadata.createTable(testSession, CATALOG_NAME, someTable(tableName), false);
    assertTrinoExceptionThrownBy(() -> getFutureValue(executeRenameView(asQualifiedName(tableName), qualifiedName("existing_table_new")))).hasErrorCode(TABLE_NOT_FOUND).hasMessage("View '%s' does not exist, but a table with that name exists. Did you mean ALTER TABLE %s RENAME ...?", tableName, tableName);
}
Also used : QualifiedObjectName(io.trino.metadata.QualifiedObjectName) Test(org.testng.annotations.Test)

Example 74 with QualifiedObjectName

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

the class TestDropViewTask method testDropViewOnTableIfExists.

@Test
public void testDropViewOnTableIfExists() {
    QualifiedObjectName tableName = qualifiedObjectName("existing_table");
    metadata.createTable(testSession, CATALOG_NAME, someTable(tableName), false);
    getFutureValue(executeDropView(asQualifiedName(tableName), true));
// no exception
}
Also used : QualifiedObjectName(io.trino.metadata.QualifiedObjectName) Test(org.testng.annotations.Test)

Example 75 with QualifiedObjectName

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

the class TestDropViewTask method testDropExistingView.

@Test
public void testDropExistingView() {
    QualifiedObjectName viewName = qualifiedObjectName("existing_view");
    metadata.createView(testSession, viewName, someView(), false);
    assertThat(metadata.isView(testSession, viewName)).isTrue();
    getFutureValue(executeDropView(asQualifiedName(viewName), false));
    assertThat(metadata.isView(testSession, viewName)).isFalse();
}
Also used : QualifiedObjectName(io.trino.metadata.QualifiedObjectName) Test(org.testng.annotations.Test)

Aggregations

QualifiedObjectName (io.trino.metadata.QualifiedObjectName)142 ViewExpression (io.trino.spi.security.ViewExpression)51 Test (org.testng.annotations.Test)51 Test (org.junit.jupiter.api.Test)41 Session (io.trino.Session)40 TableHandle (io.trino.metadata.TableHandle)33 MetadataUtil.createQualifiedObjectName (io.trino.metadata.MetadataUtil.createQualifiedObjectName)24 Optional (java.util.Optional)20 Metadata (io.trino.metadata.Metadata)17 Map (java.util.Map)17 Objects.requireNonNull (java.util.Objects.requireNonNull)16 ImmutableList (com.google.common.collect.ImmutableList)15 List (java.util.List)15 ImmutableMap (com.google.common.collect.ImmutableMap)14 TrinoException (io.trino.spi.TrinoException)14 ImmutableSet (com.google.common.collect.ImmutableSet)13 ColumnHandle (io.trino.spi.connector.ColumnHandle)13 Type (io.trino.spi.type.Type)11 Set (java.util.Set)11 CatalogName (io.trino.connector.CatalogName)10