Search in sources :

Example 6 with Field

use of io.trino.sql.analyzer.Field in project trino by trinodb.

the class RelationPlanner method visitTable.

@Override
protected RelationPlan visitTable(Table node, Void context) {
    // is this a recursive reference in expandable named query? If so, there's base relation already planned.
    RelationPlan expansion = recursiveSubqueries.get(NodeRef.of(node));
    if (expansion != null) {
        // put the pre-planned recursive subquery in the actual outer context to enable resolving correlation
        return new RelationPlan(expansion.getRoot(), expansion.getScope(), expansion.getFieldMappings(), outerContext);
    }
    Query namedQuery = analysis.getNamedQuery(node);
    Scope scope = analysis.getScope(node);
    RelationPlan plan;
    if (namedQuery != null) {
        RelationPlan subPlan;
        if (analysis.isExpandableQuery(namedQuery)) {
            subPlan = new QueryPlanner(analysis, symbolAllocator, idAllocator, lambdaDeclarationToSymbolMap, plannerContext, outerContext, session, recursiveSubqueries).planExpand(namedQuery);
        } else {
            subPlan = process(namedQuery, null);
        }
        // Add implicit coercions if view query produces types that don't match the declared output types
        // of the view (e.g., if the underlying tables referenced by the view changed)
        List<Type> types = analysis.getOutputDescriptor(node).getAllFields().stream().map(Field::getType).collect(toImmutableList());
        NodeAndMappings coerced = coerce(subPlan, types, symbolAllocator, idAllocator);
        plan = new RelationPlan(coerced.getNode(), scope, coerced.getFields(), outerContext);
    } else {
        TableHandle handle = analysis.getTableHandle(node);
        ImmutableList.Builder<Symbol> outputSymbolsBuilder = ImmutableList.builder();
        ImmutableMap.Builder<Symbol, ColumnHandle> columns = ImmutableMap.builder();
        for (Field field : scope.getRelationType().getAllFields()) {
            Symbol symbol = symbolAllocator.newSymbol(field);
            outputSymbolsBuilder.add(symbol);
            columns.put(symbol, analysis.getColumn(field));
        }
        List<Symbol> outputSymbols = outputSymbolsBuilder.build();
        boolean updateTarget = analysis.isUpdateTarget(node);
        PlanNode root = TableScanNode.newInstance(idAllocator.getNextId(), handle, outputSymbols, columns.buildOrThrow(), updateTarget, Optional.empty());
        plan = new RelationPlan(root, scope, outputSymbols, outerContext);
        List<Type> types = analysis.getRelationCoercion(node);
        if (types != null) {
            // apply required coercion and prune invisible fields from child outputs
            NodeAndMappings coerced = coerce(plan, types, symbolAllocator, idAllocator);
            plan = new RelationPlan(coerced.getNode(), scope, coerced.getFields(), outerContext);
        }
    }
    plan = addRowFilters(node, plan);
    plan = addColumnMasks(node, plan);
    return plan;
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) Query(io.trino.sql.tree.Query) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) Field(io.trino.sql.analyzer.Field) RelationType(io.trino.sql.analyzer.RelationType) RowType(io.trino.spi.type.RowType) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) Type(io.trino.spi.type.Type) PlanNode(io.trino.sql.planner.plan.PlanNode) Scope(io.trino.sql.analyzer.Scope) TableHandle(io.trino.metadata.TableHandle)

Example 7 with Field

use of io.trino.sql.analyzer.Field in project trino by trinodb.

the class RelationPlanner method addColumnMasks.

private RelationPlan addColumnMasks(Table table, RelationPlan plan) {
    Map<String, List<Expression>> columnMasks = analysis.getColumnMasks(table);
    // if the masks are missing
    if (columnMasks.isEmpty()) {
        return plan;
    }
    PlanBuilder planBuilder = newPlanBuilder(plan, analysis, lambdaDeclarationToSymbolMap).withScope(analysis.getAccessControlScope(table), // The fields in the access control scope has the same layout as those for the table scope
    plan.getFieldMappings());
    for (int i = 0; i < plan.getDescriptor().getAllFieldCount(); i++) {
        Field field = plan.getDescriptor().getFieldByIndex(i);
        for (Expression mask : columnMasks.getOrDefault(field.getName().orElseThrow(), ImmutableList.of())) {
            planBuilder = subqueryPlanner.handleSubqueries(planBuilder, mask, analysis.getSubqueries(mask));
            Map<Symbol, Expression> assignments = new LinkedHashMap<>();
            for (Symbol symbol : planBuilder.getRoot().getOutputSymbols()) {
                assignments.put(symbol, symbol.toSymbolReference());
            }
            assignments.put(plan.getFieldMappings().get(i), coerceIfNecessary(analysis, mask, planBuilder.rewrite(mask)));
            planBuilder = planBuilder.withNewRoot(new ProjectNode(idAllocator.getNextId(), planBuilder.getRoot(), Assignments.copyOf(assignments)));
        }
    }
    return new RelationPlan(planBuilder.getRoot(), plan.getScope(), plan.getFieldMappings(), outerContext);
}
Also used : Field(io.trino.sql.analyzer.Field) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) Expression(io.trino.sql.tree.Expression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ProjectNode(io.trino.sql.planner.plan.ProjectNode) PlanBuilder.newPlanBuilder(io.trino.sql.planner.PlanBuilder.newPlanBuilder) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with Field

use of io.trino.sql.analyzer.Field in project trino by trinodb.

the class LogicalPlanner method createOutputPlan.

private PlanNode createOutputPlan(RelationPlan plan, Analysis analysis) {
    ImmutableList.Builder<Symbol> outputs = ImmutableList.builder();
    ImmutableList.Builder<String> names = ImmutableList.builder();
    int columnNumber = 0;
    RelationType outputDescriptor = analysis.getOutputDescriptor();
    for (Field field : outputDescriptor.getVisibleFields()) {
        String name = field.getName().orElse("_col" + columnNumber);
        names.add(name);
        int fieldIndex = outputDescriptor.indexOf(field);
        Symbol symbol = plan.getSymbol(fieldIndex);
        outputs.add(symbol);
        columnNumber++;
    }
    return new OutputNode(idAllocator.getNextId(), plan.getRoot(), names.build(), outputs.build());
}
Also used : Field(io.trino.sql.analyzer.Field) OutputNode(io.trino.sql.planner.plan.OutputNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RelationType(io.trino.sql.analyzer.RelationType)

Example 9 with Field

use of io.trino.sql.analyzer.Field in project trino by trinodb.

the class LogicalPlanner method createExplainAnalyzePlan.

private RelationPlan createExplainAnalyzePlan(Analysis analysis, ExplainAnalyze statement) {
    RelationPlan underlyingPlan = planStatementWithoutOutput(analysis, statement.getStatement());
    PlanNode root = underlyingPlan.getRoot();
    Scope scope = analysis.getScope(statement);
    Symbol outputSymbol = symbolAllocator.newSymbol(scope.getRelationType().getFieldByIndex(0));
    ImmutableList.Builder<Symbol> actualOutputs = ImmutableList.builder();
    RelationType outputDescriptor = analysis.getOutputDescriptor(statement.getStatement());
    for (Field field : outputDescriptor.getVisibleFields()) {
        int fieldIndex = outputDescriptor.indexOf(field);
        Symbol symbol = underlyingPlan.getSymbol(fieldIndex);
        actualOutputs.add(symbol);
    }
    root = new ExplainAnalyzeNode(idAllocator.getNextId(), root, outputSymbol, actualOutputs.build(), statement.isVerbose());
    return new RelationPlan(root, scope, ImmutableList.of(outputSymbol), Optional.empty());
}
Also used : Field(io.trino.sql.analyzer.Field) PlanNode(io.trino.sql.planner.plan.PlanNode) Scope(io.trino.sql.analyzer.Scope) ExplainAnalyzeNode(io.trino.sql.planner.plan.ExplainAnalyzeNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RelationType(io.trino.sql.analyzer.RelationType)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 Field (io.trino.sql.analyzer.Field)9 RelationType (io.trino.sql.analyzer.RelationType)7 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)6 Expression (io.trino.sql.tree.Expression)6 Type (io.trino.spi.type.Type)5 TypeSignatureTranslator.toSqlType (io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType)5 PlanBuilder.newPlanBuilder (io.trino.sql.planner.PlanBuilder.newPlanBuilder)5 PlanNode (io.trino.sql.planner.plan.PlanNode)5 ProjectNode (io.trino.sql.planner.plan.ProjectNode)5 SubqueryExpression (io.trino.sql.tree.SubqueryExpression)5 Scope (io.trino.sql.analyzer.Scope)4 Cast (io.trino.sql.tree.Cast)4 CoalesceExpression (io.trino.sql.tree.CoalesceExpression)4 Row (io.trino.sql.tree.Row)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)3 TableHandle (io.trino.metadata.TableHandle)3 ColumnHandle (io.trino.spi.connector.ColumnHandle)3