Search in sources :

Example 16 with Field

use of io.prestosql.sql.analyzer.Field in project hetu-core by openlookeng.

the class RelationPlanner method process.

private SetOperationPlan process(SetOperation node) {
    List<Symbol> outputs = null;
    ImmutableList.Builder<PlanNode> sources = ImmutableList.builder();
    ImmutableListMultimap.Builder<Symbol, Symbol> symbolMapping = ImmutableListMultimap.builder();
    List<RelationPlan> subPlans = node.getRelations().stream().map(relation -> processAndCoerceIfNecessary(relation, null)).collect(toImmutableList());
    for (RelationPlan relationPlan : subPlans) {
        List<Symbol> childOutputSymbols = relationPlan.getFieldMappings();
        if (outputs == null) {
            // Use the first Relation to derive output symbol names
            RelationType descriptor = relationPlan.getDescriptor();
            ImmutableList.Builder<Symbol> outputSymbolBuilder = ImmutableList.builder();
            for (Field field : descriptor.getVisibleFields()) {
                int fieldIndex = descriptor.indexOf(field);
                Symbol symbol = childOutputSymbols.get(fieldIndex);
                outputSymbolBuilder.add(planSymbolAllocator.newSymbol(symbol.getName(), planSymbolAllocator.getTypes().get(symbol)));
            }
            outputs = outputSymbolBuilder.build();
        }
        RelationType descriptor = relationPlan.getDescriptor();
        checkArgument(descriptor.getVisibleFieldCount() == outputs.size(), "Expected relation to have %s symbols but has %s symbols", descriptor.getVisibleFieldCount(), outputs.size());
        int fieldId = 0;
        for (Field field : descriptor.getVisibleFields()) {
            int fieldIndex = descriptor.indexOf(field);
            symbolMapping.put(outputs.get(fieldId), childOutputSymbols.get(fieldIndex));
            fieldId++;
        }
        sources.add(relationPlan.getRoot());
    }
    return new SetOperationPlan(sources.build(), symbolMapping.build());
}
Also used : Table(io.prestosql.sql.tree.Table) SampledRelation(io.prestosql.sql.tree.SampledRelation) ListMultimap(com.google.common.collect.ListMultimap) Relation(io.prestosql.sql.tree.Relation) CTEScanNode(io.prestosql.spi.plan.CTEScanNode) AliasedRelation(io.prestosql.sql.tree.AliasedRelation) AggregationNode(io.prestosql.spi.plan.AggregationNode) Cast(io.prestosql.sql.tree.Cast) SemanticExceptions.notSupportedException(io.prestosql.sql.analyzer.SemanticExceptions.notSupportedException) Join(io.prestosql.sql.tree.Join) Map(java.util.Map) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) RowType(io.prestosql.spi.type.RowType) SystemSessionProperties.getTaskConcurrency(io.prestosql.SystemSessionProperties.getTaskConcurrency) Identifier(io.prestosql.sql.tree.Identifier) Row(io.prestosql.sql.tree.Row) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TableScanNode(io.prestosql.spi.plan.TableScanNode) Set(java.util.Set) PlanNode(io.prestosql.spi.plan.PlanNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) Metadata(io.prestosql.metadata.Metadata) SystemSessionProperties.getCteMaxQueueSize(io.prestosql.SystemSessionProperties.getCteMaxQueueSize) NodeRef(io.prestosql.sql.tree.NodeRef) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) ReuseExchangeOperator(io.prestosql.spi.operator.ReuseExchangeOperator) ExpressionUtils(io.prestosql.sql.ExpressionUtils) SymbolReference(io.prestosql.sql.tree.SymbolReference) Union(io.prestosql.sql.tree.Union) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Field(io.prestosql.sql.analyzer.Field) Iterables(com.google.common.collect.Iterables) IntersectNode(io.prestosql.spi.plan.IntersectNode) ExpressionTreeRewriter(io.prestosql.sql.tree.ExpressionTreeRewriter) UnnestNode(io.prestosql.sql.planner.plan.UnnestNode) ArrayList(java.util.ArrayList) MapType(io.prestosql.spi.type.MapType) LinkedHashMap(java.util.LinkedHashMap) Session(io.prestosql.Session) AssignmentUtils(io.prestosql.sql.planner.plan.AssignmentUtils) SetOperation(io.prestosql.sql.tree.SetOperation) Query(io.prestosql.sql.tree.Query) Assignments(io.prestosql.spi.plan.Assignments) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) ValuesNode(io.prestosql.spi.plan.ValuesNode) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) SampleNode(io.prestosql.sql.planner.plan.SampleNode) Lateral(io.prestosql.sql.tree.Lateral) UnmodifiableIterator(com.google.common.collect.UnmodifiableIterator) Expression(io.prestosql.sql.tree.Expression) JoinCriteria(io.prestosql.sql.tree.JoinCriteria) TableSubquery(io.prestosql.sql.tree.TableSubquery) Unnest(io.prestosql.sql.tree.Unnest) SystemSessionProperties.getExecutionPolicy(io.prestosql.SystemSessionProperties.getExecutionPolicy) Intersect(io.prestosql.sql.tree.Intersect) QualifiedName(io.prestosql.sql.tree.QualifiedName) DefaultTraversalVisitor(io.prestosql.sql.tree.DefaultTraversalVisitor) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) FilterNode(io.prestosql.spi.plan.FilterNode) Type(io.prestosql.spi.type.Type) QuerySpecification(io.prestosql.sql.tree.QuerySpecification) TypeCoercion(io.prestosql.type.TypeCoercion) Except(io.prestosql.sql.tree.Except) AggregationNode.singleGroupingSet(io.prestosql.spi.plan.AggregationNode.singleGroupingSet) ImmutableMap(com.google.common.collect.ImmutableMap) ArrayType(io.prestosql.spi.type.ArrayType) NaturalJoin(io.prestosql.sql.tree.NaturalJoin) UUID(java.util.UUID) RelationType(io.prestosql.sql.analyzer.RelationType) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Scope(io.prestosql.sql.analyzer.Scope) List(java.util.List) UnionNode(io.prestosql.spi.plan.UnionNode) Optional(java.util.Optional) Analysis(io.prestosql.sql.analyzer.Analysis) ExceptNode(io.prestosql.spi.plan.ExceptNode) MetadataUtil.createQualifiedObjectName(io.prestosql.metadata.MetadataUtil.createQualifiedObjectName) JoinNodeUtils(io.prestosql.sql.planner.plan.JoinNodeUtils) InPredicate(io.prestosql.sql.tree.InPredicate) Logger(io.airlift.log.Logger) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) LateralJoinNode(io.prestosql.sql.planner.plan.LateralJoinNode) HashMap(java.util.HashMap) RelationId(io.prestosql.sql.analyzer.RelationId) INNER(io.prestosql.sql.tree.Join.Type.INNER) TableHandle(io.prestosql.spi.metadata.TableHandle) TRUE_LITERAL(io.prestosql.sql.tree.BooleanLiteral.TRUE_LITERAL) HashSet(java.util.HashSet) Values(io.prestosql.sql.tree.Values) SystemSessionProperties.isCTEReuseEnabled(io.prestosql.SystemSessionProperties.isCTEReuseEnabled) ImmutableList(com.google.common.collect.ImmutableList) JoinUsing(io.prestosql.sql.tree.JoinUsing) Verify.verify(com.google.common.base.Verify.verify) Objects.requireNonNull(java.util.Objects.requireNonNull) JoinNode(io.prestosql.spi.plan.JoinNode) Symbol(io.prestosql.spi.plan.Symbol) Iterator(java.util.Iterator) LambdaArgumentDeclaration(io.prestosql.sql.tree.LambdaArgumentDeclaration) PlanNodeIdAllocator(io.prestosql.spi.plan.PlanNodeIdAllocator) RowExpression(io.prestosql.spi.relation.RowExpression) Symbol(io.prestosql.spi.plan.Symbol) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Field(io.prestosql.sql.analyzer.Field) PlanNode(io.prestosql.spi.plan.PlanNode) RelationType(io.prestosql.sql.analyzer.RelationType) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap)

Example 17 with Field

use of io.prestosql.sql.analyzer.Field in project hetu-core by openlookeng.

the class RelationPlanner method visitUnnest.

@Override
protected RelationPlan visitUnnest(Unnest node, Void context) {
    Scope scope = analysis.getScope(node);
    ImmutableList.Builder<Symbol> outputSymbolsBuilder = ImmutableList.builder();
    for (Field field : scope.getRelationType().getVisibleFields()) {
        Symbol symbol = planSymbolAllocator.newSymbol(field);
        outputSymbolsBuilder.add(symbol);
    }
    List<Symbol> unnestedSymbols = outputSymbolsBuilder.build();
    // If we got here, then we must be unnesting a constant, and not be in a join (where there could be column references)
    ImmutableList.Builder<Symbol> argumentSymbols = ImmutableList.builder();
    ImmutableList.Builder<RowExpression> values = ImmutableList.builder();
    ImmutableMap.Builder<Symbol, List<Symbol>> unnestSymbols = ImmutableMap.builder();
    Iterator<Symbol> unnestedSymbolsIterator = unnestedSymbols.iterator();
    for (Expression expression : node.getExpressions()) {
        Type type = analysis.getType(expression);
        Expression rewritten = Coercer.addCoercions(expression, analysis);
        rewritten = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(analysis.getParameters(), analysis), rewritten);
        values.add(castToRowExpression(rewritten));
        Symbol inputSymbol = planSymbolAllocator.newSymbol(rewritten, type);
        argumentSymbols.add(inputSymbol);
        if (type instanceof ArrayType) {
            Type elementType = ((ArrayType) type).getElementType();
            if (elementType instanceof RowType) {
                ImmutableList.Builder<Symbol> unnestSymbolBuilder = ImmutableList.builder();
                for (int i = 0; i < ((RowType) elementType).getFields().size(); i++) {
                    unnestSymbolBuilder.add(unnestedSymbolsIterator.next());
                }
                unnestSymbols.put(inputSymbol, unnestSymbolBuilder.build());
            } else {
                unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next()));
            }
        } else if (type instanceof MapType) {
            unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next(), unnestedSymbolsIterator.next()));
        } else {
            throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
        }
    }
    Optional<Symbol> ordinalitySymbol = node.isWithOrdinality() ? Optional.of(unnestedSymbolsIterator.next()) : Optional.empty();
    checkState(!unnestedSymbolsIterator.hasNext(), "Not all output symbols were matched with input symbols");
    ValuesNode valuesNode = new ValuesNode(idAllocator.getNextId(), argumentSymbols.build(), ImmutableList.of(values.build()));
    UnnestNode unnestNode = new UnnestNode(idAllocator.getNextId(), valuesNode, ImmutableList.of(), unnestSymbols.build(), ordinalitySymbol);
    return new RelationPlan(unnestNode, scope, unnestedSymbols);
}
Also used : ValuesNode(io.prestosql.spi.plan.ValuesNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Symbol(io.prestosql.spi.plan.Symbol) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) RowExpression(io.prestosql.spi.relation.RowExpression) RowType(io.prestosql.spi.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap) MapType(io.prestosql.spi.type.MapType) ArrayType(io.prestosql.spi.type.ArrayType) Field(io.prestosql.sql.analyzer.Field) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) RelationType(io.prestosql.sql.analyzer.RelationType) Scope(io.prestosql.sql.analyzer.Scope) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) UnnestNode(io.prestosql.sql.planner.plan.UnnestNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 18 with Field

use of io.prestosql.sql.analyzer.Field in project hetu-core by openlookeng.

the class RelationPlanner method addColumnMasks.

private RelationPlan addColumnMasks(Table table, RelationPlan plan) {
    Map<String, List<Expression>> columnMasks = analysis.getColumnMasks(table);
    PlanNode root = plan.getRoot();
    List<Symbol> mappings = plan.getFieldMappings();
    TranslationMap translations = new TranslationMap(plan, analysis, lambdaDeclarationToSymbolMap);
    translations.setFieldMappings(mappings);
    PlanBuilder planBuilder = new PlanBuilder(translations, root);
    for (int i = 0; i < plan.getDescriptor().getAllFieldCount(); i++) {
        Field field = plan.getDescriptor().getFieldByIndex(i);
        for (Expression mask : columnMasks.getOrDefault(field.getName().get(), ImmutableList.of())) {
            planBuilder = subqueryPlanner.handleSubqueries(planBuilder, mask, mask);
            Map<Symbol, RowExpression> assignments = new LinkedHashMap<>();
            for (Symbol symbol : root.getOutputSymbols()) {
                assignments.put(symbol, castToRowExpression(toSymbolReference(symbol)));
            }
            assignments.put(mappings.get(i), castToRowExpression(translations.rewrite(mask)));
            planBuilder = planBuilder.withNewRoot(new ProjectNode(idAllocator.getNextId(), planBuilder.getRoot(), Assignments.copyOf(assignments)));
        }
    }
    return new RelationPlan(planBuilder.getRoot(), plan.getScope(), mappings);
}
Also used : Symbol(io.prestosql.spi.plan.Symbol) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) RowExpression(io.prestosql.spi.relation.RowExpression) LinkedHashMap(java.util.LinkedHashMap) Field(io.prestosql.sql.analyzer.Field) PlanNode(io.prestosql.spi.plan.PlanNode) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ProjectNode(io.prestosql.spi.plan.ProjectNode)

Aggregations

Field (io.prestosql.sql.analyzer.Field)18 ImmutableList (com.google.common.collect.ImmutableList)16 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)15 Symbol (io.prestosql.spi.plan.Symbol)14 RelationType (io.prestosql.sql.analyzer.RelationType)13 Type (io.prestosql.spi.type.Type)12 Expression (io.prestosql.sql.tree.Expression)12 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)11 ArrayList (java.util.ArrayList)11 ProjectNode (io.prestosql.spi.plan.ProjectNode)10 List (java.util.List)10 ImmutableMap (com.google.common.collect.ImmutableMap)9 PlanNode (io.prestosql.spi.plan.PlanNode)9 RowExpression (io.prestosql.spi.relation.RowExpression)9 Scope (io.prestosql.sql.analyzer.Scope)9 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)9 TableHandle (io.prestosql.spi.metadata.TableHandle)8 Assignments (io.prestosql.spi.plan.Assignments)8 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)8