Search in sources :

Example 1 with SetOperation

use of com.facebook.presto.sql.tree.SetOperation in project presto by prestodb.

the class RelationPlanner method process.

private SetOperationPlan process(SetOperation node) {
    List<VariableReferenceExpression> outputs = null;
    ImmutableList.Builder<PlanNode> sources = ImmutableList.builder();
    ImmutableListMultimap.Builder<VariableReferenceExpression, VariableReferenceExpression> variableMapping = ImmutableListMultimap.builder();
    List<RelationPlan> subPlans = node.getRelations().stream().map(relation -> processAndCoerceIfNecessary(relation, null)).collect(toImmutableList());
    for (RelationPlan relationPlan : subPlans) {
        List<VariableReferenceExpression> childOutputVariables = relationPlan.getFieldMappings();
        if (outputs == null) {
            // Use the first Relation to derive output variable names
            RelationType descriptor = relationPlan.getDescriptor();
            ImmutableList.Builder<VariableReferenceExpression> outputVariableBuilder = ImmutableList.builder();
            for (Field field : descriptor.getVisibleFields()) {
                int fieldIndex = descriptor.indexOf(field);
                VariableReferenceExpression variable = childOutputVariables.get(fieldIndex);
                outputVariableBuilder.add(variableAllocator.newVariable(variable));
            }
            outputs = outputVariableBuilder.build();
        }
        RelationType descriptor = relationPlan.getDescriptor();
        checkArgument(descriptor.getVisibleFieldCount() == outputs.size(), "Expected relation to have %s variables but has %s variables", descriptor.getVisibleFieldCount(), outputs.size());
        int fieldId = 0;
        for (Field field : descriptor.getVisibleFields()) {
            int fieldIndex = descriptor.indexOf(field);
            variableMapping.put(outputs.get(fieldId), childOutputVariables.get(fieldIndex));
            fieldId++;
        }
        sources.add(relationPlan.getRoot());
    }
    return new SetOperationPlan(sources.build(), variableMapping.build());
}
Also used : ExpressionTreeRewriter(com.facebook.presto.sql.tree.ExpressionTreeRewriter) SampleNodeUtil(com.facebook.presto.sql.planner.optimizations.SampleNodeUtil) ListMultimap(com.google.common.collect.ListMultimap) Field(com.facebook.presto.sql.analyzer.Field) Values(com.facebook.presto.sql.tree.Values) ValuesNode(com.facebook.presto.spi.plan.ValuesNode) JoinUsing(com.facebook.presto.sql.tree.JoinUsing) ExpressionTreeUtils.resolveEnumLiteral(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.resolveEnumLiteral) SetOperation(com.facebook.presto.sql.tree.SetOperation) Map(java.util.Map) AggregationNode.singleGroupingSet(com.facebook.presto.spi.plan.AggregationNode.singleGroupingSet) Query(com.facebook.presto.sql.tree.Query) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) EnumLiteral(com.facebook.presto.sql.tree.EnumLiteral) TypeUtils.isEnumType(com.facebook.presto.common.type.TypeUtils.isEnumType) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) DefaultTraversalVisitor(com.facebook.presto.sql.tree.DefaultTraversalVisitor) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ExpressionTreeUtils.getSourceLocation(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.getSourceLocation) SemanticExceptions.notSupportedException(com.facebook.presto.sql.analyzer.SemanticExceptions.notSupportedException) SystemSessionProperties(com.facebook.presto.SystemSessionProperties) Iterables(com.google.common.collect.Iterables) LambdaArgumentDeclaration(com.facebook.presto.sql.tree.LambdaArgumentDeclaration) Table(com.facebook.presto.sql.tree.Table) Assignments(com.facebook.presto.spi.plan.Assignments) ArrayList(java.util.ArrayList) Identifier(com.facebook.presto.sql.tree.Identifier) LinkedHashMap(java.util.LinkedHashMap) TableSubquery(com.facebook.presto.sql.tree.TableSubquery) ArrayType(com.facebook.presto.common.type.ArrayType) TableHandle(com.facebook.presto.spi.TableHandle) Cast(com.facebook.presto.sql.tree.Cast) Union(com.facebook.presto.sql.tree.Union) PlanNodeIdAllocator(com.facebook.presto.spi.plan.PlanNodeIdAllocator) Session(com.facebook.presto.Session) ExpressionRewriter(com.facebook.presto.sql.tree.ExpressionRewriter) NodeRef(com.facebook.presto.sql.tree.NodeRef) Scope(com.facebook.presto.sql.analyzer.Scope) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableScanNode(com.facebook.presto.spi.plan.TableScanNode) INNER(com.facebook.presto.sql.tree.Join.Type.INNER) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) UnmodifiableIterator(com.google.common.collect.UnmodifiableIterator) Row(com.facebook.presto.sql.tree.Row) OriginalExpressionUtils.asSymbolReference(com.facebook.presto.sql.relational.OriginalExpressionUtils.asSymbolReference) RowType(com.facebook.presto.common.type.RowType) Metadata(com.facebook.presto.metadata.Metadata) ExpressionTreeUtils.createSymbolReference(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) IntersectNode(com.facebook.presto.spi.plan.IntersectNode) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Join(com.facebook.presto.sql.tree.Join) LOCAL(com.facebook.presto.spi.plan.ProjectNode.Locality.LOCAL) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) AssignmentUtils.identitiesAsSymbolReferences(com.facebook.presto.sql.planner.plan.AssignmentUtils.identitiesAsSymbolReferences) RelationId(com.facebook.presto.sql.analyzer.RelationId) Lateral(com.facebook.presto.sql.tree.Lateral) ImmutableMap(com.google.common.collect.ImmutableMap) SampledRelation(com.facebook.presto.sql.tree.SampledRelation) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) InPredicate(com.facebook.presto.sql.tree.InPredicate) SampleNode(com.facebook.presto.sql.planner.plan.SampleNode) Analysis(com.facebook.presto.sql.analyzer.Analysis) Optional(java.util.Optional) JoinNodeUtils(com.facebook.presto.sql.planner.optimizations.JoinNodeUtils) Except(com.facebook.presto.sql.tree.Except) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) MapType(com.facebook.presto.common.type.MapType) UnionNode(com.facebook.presto.spi.plan.UnionNode) Intersect(com.facebook.presto.sql.tree.Intersect) HashMap(java.util.HashMap) ExceptNode(com.facebook.presto.spi.plan.ExceptNode) ExpressionUtils(com.facebook.presto.sql.ExpressionUtils) Unnest(com.facebook.presto.sql.tree.Unnest) FilterNode(com.facebook.presto.spi.plan.FilterNode) ImmutableList(com.google.common.collect.ImmutableList) Verify.verify(com.google.common.base.Verify.verify) Objects.requireNonNull(java.util.Objects.requireNonNull) UnnestNode(com.facebook.presto.sql.planner.plan.UnnestNode) Type(com.facebook.presto.common.type.Type) RowExpression(com.facebook.presto.spi.relation.RowExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) Iterator(java.util.Iterator) Relation(com.facebook.presto.sql.tree.Relation) IsNotNullPredicate(com.facebook.presto.sql.tree.IsNotNullPredicate) RelationType(com.facebook.presto.sql.analyzer.RelationType) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) ExpressionTreeUtils.isEqualComparisonExpression(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.isEqualComparisonExpression) PlanNode(com.facebook.presto.spi.plan.PlanNode) Expression(com.facebook.presto.sql.tree.Expression) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Field(com.facebook.presto.sql.analyzer.Field) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RelationType(com.facebook.presto.sql.analyzer.RelationType) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap)

Aggregations

Session (com.facebook.presto.Session)1 SystemSessionProperties (com.facebook.presto.SystemSessionProperties)1 TupleDomain (com.facebook.presto.common.predicate.TupleDomain)1 ArrayType (com.facebook.presto.common.type.ArrayType)1 MapType (com.facebook.presto.common.type.MapType)1 RowType (com.facebook.presto.common.type.RowType)1 Type (com.facebook.presto.common.type.Type)1 TypeUtils.isEnumType (com.facebook.presto.common.type.TypeUtils.isEnumType)1 Metadata (com.facebook.presto.metadata.Metadata)1 ColumnHandle (com.facebook.presto.spi.ColumnHandle)1 TableHandle (com.facebook.presto.spi.TableHandle)1 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)1 AggregationNode.singleGroupingSet (com.facebook.presto.spi.plan.AggregationNode.singleGroupingSet)1 Assignments (com.facebook.presto.spi.plan.Assignments)1 ExceptNode (com.facebook.presto.spi.plan.ExceptNode)1 FilterNode (com.facebook.presto.spi.plan.FilterNode)1 IntersectNode (com.facebook.presto.spi.plan.IntersectNode)1 PlanNode (com.facebook.presto.spi.plan.PlanNode)1 PlanNodeIdAllocator (com.facebook.presto.spi.plan.PlanNodeIdAllocator)1 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)1