Search in sources :

Example 41 with PlanNode

use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.

the class RelationPlanner method visitIntersect.

@Override
protected RelationPlan visitIntersect(Intersect node, Void context) {
    checkArgument(!node.getRelations().isEmpty(), "No relations specified for INTERSECT");
    SetOperationPlan setOperationPlan = process(node);
    PlanNode planNode = new IntersectNode(idAllocator.getNextId(), setOperationPlan.getSources(), setOperationPlan.getSymbolMapping(), ImmutableList.copyOf(setOperationPlan.getSymbolMapping().keySet()));
    return new RelationPlan(planNode, analysis.getScope(node), planNode.getOutputSymbols());
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) IntersectNode(com.facebook.presto.sql.planner.plan.IntersectNode)

Example 42 with PlanNode

use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.

the class RelationPlanner method visitJoin.

@Override
protected RelationPlan visitJoin(Join node, Void context) {
    // TODO: translate the RIGHT join into a mirrored LEFT join when we refactor (@martint)
    RelationPlan leftPlan = process(node.getLeft(), context);
    // Convert CROSS JOIN UNNEST to an UnnestNode
    if (node.getRight() instanceof Unnest || (node.getRight() instanceof AliasedRelation && ((AliasedRelation) node.getRight()).getRelation() instanceof Unnest)) {
        Unnest unnest;
        if (node.getRight() instanceof AliasedRelation) {
            unnest = (Unnest) ((AliasedRelation) node.getRight()).getRelation();
        } else {
            unnest = (Unnest) node.getRight();
        }
        if (node.getType() != Join.Type.CROSS && node.getType() != Join.Type.IMPLICIT) {
            throw notSupportedException(unnest, "UNNEST on other than the right side of CROSS JOIN");
        }
        return planCrossJoinUnnest(leftPlan, node, unnest);
    }
    RelationPlan rightPlan = process(node.getRight(), context);
    PlanBuilder leftPlanBuilder = initializePlanBuilder(leftPlan);
    PlanBuilder rightPlanBuilder = initializePlanBuilder(rightPlan);
    // NOTE: symbols must be in the same order as the outputDescriptor
    List<Symbol> outputSymbols = ImmutableList.<Symbol>builder().addAll(leftPlan.getFieldMappings()).addAll(rightPlan.getFieldMappings()).build();
    ImmutableList.Builder<JoinNode.EquiJoinClause> equiClauses = ImmutableList.builder();
    List<Expression> complexJoinExpressions = new ArrayList<>();
    List<Expression> postInnerJoinConditions = new ArrayList<>();
    if (node.getType() != Join.Type.CROSS && node.getType() != Join.Type.IMPLICIT) {
        Expression criteria = analysis.getJoinCriteria(node);
        RelationType left = analysis.getOutputDescriptor(node.getLeft());
        RelationType right = analysis.getOutputDescriptor(node.getRight());
        List<Expression> leftComparisonExpressions = new ArrayList<>();
        List<Expression> rightComparisonExpressions = new ArrayList<>();
        List<ComparisonExpressionType> joinConditionComparisonTypes = new ArrayList<>();
        for (Expression conjunct : ExpressionUtils.extractConjuncts(criteria)) {
            conjunct = ExpressionUtils.normalize(conjunct);
            if (!isEqualComparisonExpression(conjunct) && node.getType() != INNER) {
                complexJoinExpressions.add(conjunct);
                continue;
            }
            Set<QualifiedName> dependencies = DependencyExtractor.extractNames(conjunct, analysis.getColumnReferences());
            boolean isJoinUsing = node.getCriteria().filter(JoinUsing.class::isInstance).isPresent();
            if (!isJoinUsing && (dependencies.stream().allMatch(left::canResolve) || dependencies.stream().allMatch(right::canResolve))) {
                // If the conjunct can be evaluated entirely with the inputs on either side of the join, add
                // it to the list complex expressions and let the optimizers figure out how to push it down later.
                // Due to legacy reasons, the expression for "join using" looks like "x = x", which (incorrectly)
                // appears to fit the condition we're after. So we skip them.
                complexJoinExpressions.add(conjunct);
            } else if (conjunct instanceof ComparisonExpression) {
                Expression firstExpression = ((ComparisonExpression) conjunct).getLeft();
                Expression secondExpression = ((ComparisonExpression) conjunct).getRight();
                ComparisonExpressionType comparisonType = ((ComparisonExpression) conjunct).getType();
                Set<QualifiedName> firstDependencies = DependencyExtractor.extractNames(firstExpression, analysis.getColumnReferences());
                Set<QualifiedName> secondDependencies = DependencyExtractor.extractNames(secondExpression, analysis.getColumnReferences());
                if (firstDependencies.stream().allMatch(left::canResolve) && secondDependencies.stream().allMatch(right::canResolve)) {
                    leftComparisonExpressions.add(firstExpression);
                    rightComparisonExpressions.add(secondExpression);
                    joinConditionComparisonTypes.add(comparisonType);
                } else if (firstDependencies.stream().allMatch(right::canResolve) && secondDependencies.stream().allMatch(left::canResolve)) {
                    leftComparisonExpressions.add(secondExpression);
                    rightComparisonExpressions.add(firstExpression);
                    joinConditionComparisonTypes.add(comparisonType.flip());
                } else {
                    // the case when we mix symbols from both left and right join side on either side of condition.
                    complexJoinExpressions.add(conjunct);
                }
            } else {
                complexJoinExpressions.add(conjunct);
            }
        }
        leftPlanBuilder = subqueryPlanner.handleSubqueries(leftPlanBuilder, leftComparisonExpressions, node);
        rightPlanBuilder = subqueryPlanner.handleSubqueries(rightPlanBuilder, rightComparisonExpressions, node);
        // Add projections for join criteria
        leftPlanBuilder = leftPlanBuilder.appendProjections(leftComparisonExpressions, symbolAllocator, idAllocator);
        rightPlanBuilder = rightPlanBuilder.appendProjections(rightComparisonExpressions, symbolAllocator, idAllocator);
        for (int i = 0; i < leftComparisonExpressions.size(); i++) {
            if (joinConditionComparisonTypes.get(i) == ComparisonExpressionType.EQUAL) {
                Symbol leftSymbol = leftPlanBuilder.translate(leftComparisonExpressions.get(i));
                Symbol rightSymbol = rightPlanBuilder.translate(rightComparisonExpressions.get(i));
                equiClauses.add(new JoinNode.EquiJoinClause(leftSymbol, rightSymbol));
            } else {
                Expression leftExpression = leftPlanBuilder.rewrite(leftComparisonExpressions.get(i));
                Expression rightExpression = rightPlanBuilder.rewrite(rightComparisonExpressions.get(i));
                postInnerJoinConditions.add(new ComparisonExpression(joinConditionComparisonTypes.get(i), leftExpression, rightExpression));
            }
        }
    }
    PlanNode root = new JoinNode(idAllocator.getNextId(), JoinNode.Type.typeConvert(node.getType()), leftPlanBuilder.getRoot(), rightPlanBuilder.getRoot(), equiClauses.build(), ImmutableList.<Symbol>builder().addAll(leftPlanBuilder.getRoot().getOutputSymbols()).addAll(rightPlanBuilder.getRoot().getOutputSymbols()).build(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
    if (node.getType() != INNER) {
        for (Expression complexExpression : complexJoinExpressions) {
            Set<InPredicate> inPredicates = subqueryPlanner.collectInPredicateSubqueries(complexExpression, node);
            if (!inPredicates.isEmpty()) {
                InPredicate inPredicate = Iterables.getLast(inPredicates);
                throw notSupportedException(inPredicate, "IN with subquery predicate in join condition");
            }
        }
        // subqueries can be applied only to one side of join - left side is selected in arbitrary way
        leftPlanBuilder = subqueryPlanner.handleUncorrelatedSubqueries(leftPlanBuilder, complexJoinExpressions, node);
    }
    RelationPlan intermediateRootRelationPlan = new RelationPlan(root, analysis.getScope(node), outputSymbols);
    TranslationMap translationMap = new TranslationMap(intermediateRootRelationPlan, analysis, lambdaDeclarationToSymbolMap);
    translationMap.setFieldMappings(outputSymbols);
    translationMap.putExpressionMappingsFrom(leftPlanBuilder.getTranslations());
    translationMap.putExpressionMappingsFrom(rightPlanBuilder.getTranslations());
    if (node.getType() != INNER && !complexJoinExpressions.isEmpty()) {
        Expression joinedFilterCondition = ExpressionUtils.and(complexJoinExpressions);
        joinedFilterCondition = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(analysis.getParameters(), analysis), joinedFilterCondition);
        Expression rewrittenFilterCondition = translationMap.rewrite(joinedFilterCondition);
        root = new JoinNode(idAllocator.getNextId(), JoinNode.Type.typeConvert(node.getType()), leftPlanBuilder.getRoot(), rightPlanBuilder.getRoot(), equiClauses.build(), ImmutableList.<Symbol>builder().addAll(leftPlanBuilder.getRoot().getOutputSymbols()).addAll(rightPlanBuilder.getRoot().getOutputSymbols()).build(), Optional.of(rewrittenFilterCondition), Optional.empty(), Optional.empty(), Optional.empty());
    }
    if (node.getType() == INNER) {
        // rewrite all the other conditions using output symbols from left + right plan node.
        PlanBuilder rootPlanBuilder = new PlanBuilder(translationMap, root, analysis.getParameters());
        rootPlanBuilder = subqueryPlanner.handleSubqueries(rootPlanBuilder, complexJoinExpressions, node);
        for (Expression expression : complexJoinExpressions) {
            expression = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(analysis.getParameters(), analysis), expression);
            postInnerJoinConditions.add(rootPlanBuilder.rewrite(expression));
        }
        root = rootPlanBuilder.getRoot();
        Expression postInnerJoinCriteria;
        if (!postInnerJoinConditions.isEmpty()) {
            postInnerJoinCriteria = ExpressionUtils.and(postInnerJoinConditions);
            root = new FilterNode(idAllocator.getNextId(), root, postInnerJoinCriteria);
        }
    }
    return new RelationPlan(root, analysis.getScope(node), outputSymbols);
}
Also used : Set(java.util.Set) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) FilterNode(com.facebook.presto.sql.planner.plan.FilterNode) ArrayList(java.util.ArrayList) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) RelationType(com.facebook.presto.sql.analyzer.RelationType) Unnest(com.facebook.presto.sql.tree.Unnest) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) ComparisonExpressionType(com.facebook.presto.sql.tree.ComparisonExpressionType) InPredicate(com.facebook.presto.sql.tree.InPredicate) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) ExpressionInterpreter.evaluateConstantExpression(com.facebook.presto.sql.planner.ExpressionInterpreter.evaluateConstantExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation)

Example 43 with PlanNode

use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.

the class RelationPlanner method visitTable.

@Override
protected RelationPlan visitTable(Table node, Void context) {
    Query namedQuery = analysis.getNamedQuery(node);
    Scope scope = analysis.getScope(node);
    if (namedQuery != null) {
        RelationPlan 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)
        Type[] types = scope.getRelationType().getAllFields().stream().map(Field::getType).toArray(Type[]::new);
        RelationPlan withCoercions = addCoercions(subPlan, types);
        return new RelationPlan(withCoercions.getRoot(), scope, withCoercions.getFieldMappings());
    }
    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.getName().get(), field.getType());
        outputSymbolsBuilder.add(symbol);
        columns.put(symbol, analysis.getColumn(field));
    }
    List<Symbol> outputSymbols = outputSymbolsBuilder.build();
    PlanNode root = new TableScanNode(idAllocator.getNextId(), handle, outputSymbols, columns.build(), Optional.empty(), TupleDomain.all(), null);
    return new RelationPlan(root, scope, outputSymbols);
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) Query(com.facebook.presto.sql.tree.Query) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) Field(com.facebook.presto.sql.analyzer.Field) ComparisonExpressionType(com.facebook.presto.sql.tree.ComparisonExpressionType) ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Scope(com.facebook.presto.sql.analyzer.Scope) TableScanNode(com.facebook.presto.sql.planner.plan.TableScanNode) TableHandle(com.facebook.presto.metadata.TableHandle)

Example 44 with PlanNode

use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.

the class RelationPlanner method visitSampledRelation.

@Override
protected RelationPlan visitSampledRelation(SampledRelation node, Void context) {
    RelationPlan subPlan = process(node.getRelation(), context);
    double ratio = analysis.getSampleRatio(node);
    PlanNode planNode = new SampleNode(idAllocator.getNextId(), subPlan.getRoot(), ratio, SampleNode.Type.fromType(node.getType()));
    return new RelationPlan(planNode, analysis.getScope(node), subPlan.getFieldMappings());
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) SampleNode(com.facebook.presto.sql.planner.plan.SampleNode)

Example 45 with PlanNode

use of com.facebook.presto.sql.planner.plan.PlanNode in project presto by prestodb.

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(symbolAllocator.newSymbol(symbol.getName(), symbolAllocator.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 : ComparisonExpressionType(com.facebook.presto.sql.tree.ComparisonExpressionType) ExpressionTreeRewriter(com.facebook.presto.sql.tree.ExpressionTreeRewriter) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation) ListMultimap(com.google.common.collect.ListMultimap) Field(com.facebook.presto.sql.analyzer.Field) Values(com.facebook.presto.sql.tree.Values) ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Join(com.facebook.presto.sql.tree.Join) JoinUsing(com.facebook.presto.sql.tree.JoinUsing) SetOperation(com.facebook.presto.sql.tree.SetOperation) ExpressionInterpreter.evaluateConstantExpression(com.facebook.presto.sql.planner.ExpressionInterpreter.evaluateConstantExpression) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) Assignments(com.facebook.presto.sql.planner.plan.Assignments) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) Query(com.facebook.presto.sql.tree.Query) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) ExceptNode(com.facebook.presto.sql.planner.plan.ExceptNode) SampledRelation(com.facebook.presto.sql.tree.SampledRelation) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) List(java.util.List) InPredicate(com.facebook.presto.sql.tree.InPredicate) DefaultTraversalVisitor(com.facebook.presto.sql.tree.DefaultTraversalVisitor) SampleNode(com.facebook.presto.sql.planner.plan.SampleNode) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) SemanticExceptions.notSupportedException(com.facebook.presto.sql.analyzer.SemanticExceptions.notSupportedException) Analysis(com.facebook.presto.sql.analyzer.Analysis) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) Optional(java.util.Optional) Except(com.facebook.presto.sql.tree.Except) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) Iterables(com.google.common.collect.Iterables) LambdaArgumentDeclaration(com.facebook.presto.sql.tree.LambdaArgumentDeclaration) Table(com.facebook.presto.sql.tree.Table) ArrayType(com.facebook.presto.type.ArrayType) Intersect(com.facebook.presto.sql.tree.Intersect) MapType(com.facebook.presto.type.MapType) ExpressionUtils(com.facebook.presto.sql.ExpressionUtils) ArrayList(java.util.ArrayList) Unnest(com.facebook.presto.sql.tree.Unnest) TableScanNode(com.facebook.presto.sql.planner.plan.TableScanNode) ImmutableList(com.google.common.collect.ImmutableList) Verify.verify(com.google.common.base.Verify.verify) Type(com.facebook.presto.spi.type.Type) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Objects.requireNonNull(java.util.Objects.requireNonNull) TableSubquery(com.facebook.presto.sql.tree.TableSubquery) Cast(com.facebook.presto.sql.tree.Cast) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) UnnestNode(com.facebook.presto.sql.planner.plan.UnnestNode) Union(com.facebook.presto.sql.tree.Union) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) Iterator(java.util.Iterator) Relation(com.facebook.presto.sql.tree.Relation) Session(com.facebook.presto.Session) RelationType(com.facebook.presto.sql.analyzer.RelationType) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Scope(com.facebook.presto.sql.analyzer.Scope) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) UnionNode(com.facebook.presto.sql.planner.plan.UnionNode) Expression(com.facebook.presto.sql.tree.Expression) ColumnHandle(com.facebook.presto.spi.ColumnHandle) TableHandle(com.facebook.presto.metadata.TableHandle) INNER(com.facebook.presto.sql.tree.Join.Type.INNER) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) FilterNode(com.facebook.presto.sql.planner.plan.FilterNode) IdentityLinkedHashMap(com.facebook.presto.util.maps.IdentityLinkedHashMap) UnmodifiableIterator(com.google.common.collect.UnmodifiableIterator) Row(com.facebook.presto.sql.tree.Row) IntersectNode(com.facebook.presto.sql.planner.plan.IntersectNode) Metadata(com.facebook.presto.metadata.Metadata) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Field(com.facebook.presto.sql.analyzer.Field) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) RelationType(com.facebook.presto.sql.analyzer.RelationType) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap)

Aggregations

PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)89 Test (org.testng.annotations.Test)41 Expression (com.facebook.presto.sql.tree.Expression)28 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)18 Symbol (com.facebook.presto.sql.planner.Symbol)12 JoinNode (com.facebook.presto.sql.planner.plan.JoinNode)12 ProjectNode (com.facebook.presto.sql.planner.plan.ProjectNode)12 TableScanNode (com.facebook.presto.sql.planner.plan.TableScanNode)12 ImmutableList (com.google.common.collect.ImmutableList)12 AggregationNode (com.facebook.presto.sql.planner.plan.AggregationNode)10 ColumnHandle (com.facebook.presto.spi.ColumnHandle)9 FilterNode (com.facebook.presto.sql.planner.plan.FilterNode)9 LimitNode (com.facebook.presto.sql.planner.plan.LimitNode)9 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)9 Signature (com.facebook.presto.metadata.Signature)7 SemiJoinNode (com.facebook.presto.sql.planner.plan.SemiJoinNode)7 List (java.util.List)7 JoinGraph (com.facebook.presto.sql.planner.optimizations.joins.JoinGraph)6 ValuesNode (com.facebook.presto.sql.planner.plan.ValuesNode)6 Session (com.facebook.presto.Session)5