Search in sources :

Example 26 with ComparisonExpression

use of io.prestosql.sql.tree.ComparisonExpression in project hetu-core by openlookeng.

the class TransformUnCorrelatedInPredicateSubQuerySelfJoinToAggregate method getAllSymbols.

private static void getAllSymbols(Expression expression, List<SymbolReference> symbols) {
    if (expression instanceof LogicalBinaryExpression) {
        LogicalBinaryExpression logicalBinaryExpression = (LogicalBinaryExpression) expression;
        getAllSymbols(logicalBinaryExpression.getLeft(), symbols);
        getAllSymbols(logicalBinaryExpression.getRight(), symbols);
    } else if (expression instanceof ComparisonExpression) {
        ComparisonExpression comparisonExpression = (ComparisonExpression) expression;
        getAllSymbols(comparisonExpression.getLeft(), symbols);
        getAllSymbols(comparisonExpression.getRight(), symbols);
    } else if (expression instanceof SymbolReference) {
        symbols.add((SymbolReference) expression);
    }
}
Also used : LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) SymbolReference(io.prestosql.sql.tree.SymbolReference)

Example 27 with ComparisonExpression

use of io.prestosql.sql.tree.ComparisonExpression in project hetu-core by openlookeng.

the class TransformUnCorrelatedInPredicateSubQuerySelfJoinToAggregate method isSelfJoin.

private static boolean isSelfJoin(ProjectNode projectNode, Expression predicate, JoinNode joinNode, Lookup lookup) {
    PlanNode left = lookup.resolve(joinNode.getLeft());
    PlanNode right = lookup.resolve(joinNode.getRight());
    // TODO FIX FOR framenwork changes
    if (joinNode.getType() == JoinNode.Type.INNER && left instanceof TableScanNode && right instanceof TableScanNode && ((TableScanNode) left).getTable().getFullyQualifiedName().equals(((TableScanNode) right).getTable().getFullyQualifiedName())) {
        if (!(predicate instanceof LogicalBinaryExpression && ((LogicalBinaryExpression) predicate).getLeft() instanceof ComparisonExpression && ((LogicalBinaryExpression) predicate).getRight() instanceof ComparisonExpression)) {
            return false;
        }
        SymbolReference projected = SymbolUtils.toSymbolReference(getOnlyElement(projectNode.getOutputSymbols()));
        ComparisonExpression leftPredicate = (ComparisonExpression) ((LogicalBinaryExpression) predicate).getLeft();
        ComparisonExpression rightPredicate = (ComparisonExpression) ((LogicalBinaryExpression) predicate).getRight();
        if (lookup.resolve(projectNode.getSource()) instanceof CTEScanNode) {
            CTEScanNode cteScanNode = (CTEScanNode) lookup.resolve(projectNode.getSource());
            ProjectNode childProjectOfCte = (ProjectNode) lookup.resolve(cteScanNode.getSource());
            RowExpression projectedExpresssion = (childProjectOfCte.getAssignments().get(getOnlyElement(projectNode.getOutputSymbols())));
            for (Symbol symbol : lookup.resolve(childProjectOfCte.getSource()).getOutputSymbols()) {
                if (symbol.getName().equals(((SymbolReference) OriginalExpressionUtils.castToExpression(projectedExpresssion)).getName())) {
                    projected = SymbolUtils.toSymbolReference(symbol);
                }
            }
        }
        if (leftPredicate.getChildren().contains(projected) && leftPredicate.getOperator() == ComparisonExpression.Operator.EQUAL && rightPredicate.getOperator() == ComparisonExpression.Operator.NOT_EQUAL) {
            return true;
        } else if (rightPredicate.getChildren().contains(projected) && rightPredicate.getOperator() == ComparisonExpression.Operator.EQUAL && leftPredicate.getOperator() == ComparisonExpression.Operator.NOT_EQUAL) {
            return true;
        }
    }
    return false;
}
Also used : LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) PlanNode(io.prestosql.spi.plan.PlanNode) TableScanNode(io.prestosql.spi.plan.TableScanNode) SymbolReference(io.prestosql.sql.tree.SymbolReference) Symbol(io.prestosql.spi.plan.Symbol) RowExpression(io.prestosql.spi.relation.RowExpression) CTEScanNode(io.prestosql.spi.plan.CTEScanNode) ProjectNode(io.prestosql.spi.plan.ProjectNode)

Example 28 with ComparisonExpression

use of io.prestosql.sql.tree.ComparisonExpression in project hetu-core by openlookeng.

the class SetOperationNodeTranslator method makeSetContainmentPlan.

public TranslationResult makeSetContainmentPlan(SetOperationNode node) {
    checkArgument(!(node instanceof UnionNode), "Cannot simplify a UnionNode");
    List<Symbol> markers = allocateSymbols(node.getSources().size(), MARKER, BOOLEAN);
    // identity projection for all the fields in each of the sources plus marker columns
    List<PlanNode> withMarkers = appendMarkers(markers, node.getSources(), node);
    // add a union over all the rewritten sources. The outputs of the union have the same name as the
    // original intersect node
    List<Symbol> outputs = node.getOutputSymbols();
    UnionNode union = union(withMarkers, ImmutableList.copyOf(concat(outputs, markers)));
    // add count aggregations and filter rows where any of the counts is >= 1
    List<Symbol> aggregationOutputs = allocateSymbols(markers.size(), "count", BIGINT);
    AggregationNode aggregation = computeCounts(union, outputs, markers, aggregationOutputs);
    List<Expression> presentExpression = aggregationOutputs.stream().map(symbol -> new ComparisonExpression(GREATER_THAN_OR_EQUAL, toSymbolReference(symbol), GENERIC_LITERAL)).collect(toImmutableList());
    return new TranslationResult(aggregation, presentExpression);
}
Also used : GREATER_THAN_OR_EQUAL(io.prestosql.sql.tree.ComparisonExpression.Operator.GREATER_THAN_OR_EQUAL) StandardTypes(io.prestosql.spi.type.StandardTypes) Literal(io.prestosql.sql.tree.Literal) DEFAULT_NAMESPACE(io.prestosql.spi.connector.CatalogSchemaName.DEFAULT_NAMESPACE) TRUE_LITERAL(io.prestosql.sql.tree.BooleanLiteral.TRUE_LITERAL) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) AggregationNode(io.prestosql.spi.plan.AggregationNode) CallExpression(io.prestosql.spi.relation.CallExpression) Cast(io.prestosql.sql.tree.Cast) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Iterables.concat(com.google.common.collect.Iterables.concat) Map(java.util.Map) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) Objects.requireNonNull(java.util.Objects.requireNonNull) BOOLEAN(io.prestosql.spi.type.BooleanType.BOOLEAN) Type(io.prestosql.spi.type.Type) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) AggregationNode.singleGroupingSet(io.prestosql.spi.plan.AggregationNode.singleGroupingSet) Symbol(io.prestosql.spi.plan.Symbol) SetOperationNodeUtils.sourceSymbolMap(io.prestosql.sql.planner.optimizations.SetOperationNodeUtils.sourceSymbolMap) TypeSignatureProvider.fromTypes(io.prestosql.sql.analyzer.TypeSignatureProvider.fromTypes) ImmutableMap(com.google.common.collect.ImmutableMap) Assignments(io.prestosql.spi.plan.Assignments) SetOperationNode(io.prestosql.spi.plan.SetOperationNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) NullLiteral(io.prestosql.sql.tree.NullLiteral) PlanNode(io.prestosql.spi.plan.PlanNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) Metadata(io.prestosql.metadata.Metadata) PlanSymbolAllocator(io.prestosql.sql.planner.PlanSymbolAllocator) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) List(java.util.List) GenericLiteral(io.prestosql.sql.tree.GenericLiteral) SymbolReference(io.prestosql.sql.tree.SymbolReference) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) PlanNodeIdAllocator(io.prestosql.spi.plan.PlanNodeIdAllocator) UnionNode(io.prestosql.spi.plan.UnionNode) Optional(java.util.Optional) Expression(io.prestosql.sql.tree.Expression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) PlanNode(io.prestosql.spi.plan.PlanNode) UnionNode(io.prestosql.spi.plan.UnionNode) CallExpression(io.prestosql.spi.relation.CallExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) Symbol(io.prestosql.spi.plan.Symbol) AggregationNode(io.prestosql.spi.plan.AggregationNode)

Example 29 with ComparisonExpression

use of io.prestosql.sql.tree.ComparisonExpression in project hetu-core by openlookeng.

the class TablePushdown method needNewInnerJoinFilter.

/**
 * @param originalJoinNode the original JoinNode captured by the rule
 * @param childOfInnerJoin the child of the InnerJoinNode that has to be moved(the outer Table)
 * @return boolean signifying if the table being pushed down will need a join filter for the new join
 */
private boolean needNewInnerJoinFilter(JoinNode originalJoinNode, PlanNode childOfInnerJoin) {
    if (originalJoinNode.getFilter().isPresent()) {
        ComparisonExpression originalJoinNodeFilter = (ComparisonExpression) castToExpression(originalJoinNode.getFilter().get());
        List<Symbol> innerJoinChildOPSymbols = childOfInnerJoin.getOutputSymbols();
        SymbolReference originalFilterSymbolRefLeft = (SymbolReference) originalJoinNodeFilter.getLeft();
        SymbolReference originalFilterSymbolRefRight = (SymbolReference) originalJoinNodeFilter.getRight();
        for (Symbol symbol : innerJoinChildOPSymbols) {
            if (symbol.toString().equalsIgnoreCase(originalFilterSymbolRefLeft.toString()) || symbol.toString().equalsIgnoreCase(originalFilterSymbolRefRight.toString())) {
                return true;
            }
        }
    }
    return false;
}
Also used : ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Symbol(io.prestosql.spi.plan.Symbol) SymbolReference(io.prestosql.sql.tree.SymbolReference)

Example 30 with ComparisonExpression

use of io.prestosql.sql.tree.ComparisonExpression in project hetu-core by openlookeng.

the class TransformCorrelatedInPredicateToJoin method buildInPredicateEquivalent.

private PlanNode buildInPredicateEquivalent(ApplyNode apply, InPredicate inPredicate, Symbol inPredicateOutputSymbol, Decorrelated decorrelated, PlanNodeIdAllocator idAllocator, PlanSymbolAllocator planSymbolAllocator) {
    Expression correlationCondition = and(decorrelated.getCorrelatedPredicates());
    PlanNode decorrelatedBuildSource = decorrelated.getDecorrelatedNode();
    AssignUniqueId probeSide = new AssignUniqueId(idAllocator.getNextId(), apply.getInput(), planSymbolAllocator.newSymbol("unique", BIGINT));
    Symbol buildSideKnownNonNull = planSymbolAllocator.newSymbol("buildSideKnownNonNull", BIGINT);
    ProjectNode buildSide = new ProjectNode(idAllocator.getNextId(), decorrelatedBuildSource, Assignments.builder().putAll(identityAsSymbolReferences(decorrelatedBuildSource.getOutputSymbols())).put(buildSideKnownNonNull, castToRowExpression(bigint(0))).build());
    Symbol probeSideSymbol = SymbolUtils.from(inPredicate.getValue());
    Symbol buildSideSymbol = SymbolUtils.from(inPredicate.getValueList());
    Expression joinExpression = and(or(new IsNullPredicate(toSymbolReference(probeSideSymbol)), new ComparisonExpression(ComparisonExpression.Operator.EQUAL, toSymbolReference(probeSideSymbol), toSymbolReference(buildSideSymbol)), new IsNullPredicate(toSymbolReference(buildSideSymbol))), correlationCondition);
    JoinNode leftOuterJoin = leftOuterJoin(idAllocator, probeSide, buildSide, joinExpression);
    Symbol matchConditionSymbol = planSymbolAllocator.newSymbol("matchConditionSymbol", BOOLEAN);
    Expression matchCondition = and(isNotNull(probeSideSymbol), isNotNull(buildSideSymbol));
    Symbol nullMatchConditionSymbol = planSymbolAllocator.newSymbol("nullMatchConditionSymbol", BOOLEAN);
    Expression nullMatchCondition = and(isNotNull(buildSideKnownNonNull), not(matchCondition));
    ProjectNode preProjection = new ProjectNode(idAllocator.getNextId(), leftOuterJoin, Assignments.builder().putAll(AssignmentUtils.identityAsSymbolReferences(leftOuterJoin.getOutputSymbols())).put(matchConditionSymbol, castToRowExpression(matchCondition)).put(nullMatchConditionSymbol, castToRowExpression(nullMatchCondition)).build());
    Symbol countMatchesSymbol = planSymbolAllocator.newSymbol("countMatches", BIGINT);
    Symbol countNullMatchesSymbol = planSymbolAllocator.newSymbol("countNullMatches", BIGINT);
    AggregationNode aggregation = new AggregationNode(idAllocator.getNextId(), preProjection, ImmutableMap.<Symbol, AggregationNode.Aggregation>builder().put(countMatchesSymbol, countWithFilter(matchConditionSymbol)).put(countNullMatchesSymbol, countWithFilter(nullMatchConditionSymbol)).build(), singleGroupingSet(probeSide.getOutputSymbols()), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), Optional.empty(), AggregationNode.AggregationType.HASH, Optional.empty());
    // TODO since we care only about "some count > 0", we could have specialized node instead of leftOuterJoin that does the job without materializing join results
    SearchedCaseExpression inPredicateEquivalent = new SearchedCaseExpression(ImmutableList.of(new WhenClause(isGreaterThan(countMatchesSymbol, 0), booleanConstant(true)), new WhenClause(isGreaterThan(countNullMatchesSymbol, 0), booleanConstant(null))), Optional.of(booleanConstant(false)));
    return new ProjectNode(idAllocator.getNextId(), aggregation, Assignments.builder().putAll(identityAsSymbolReferences(apply.getInput().getOutputSymbols())).put(inPredicateOutputSymbol, castToRowExpression(inPredicateEquivalent)).build());
}
Also used : ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) WhenClause(io.prestosql.sql.tree.WhenClause) PlanNode(io.prestosql.spi.plan.PlanNode) AssignUniqueId(io.prestosql.sql.planner.plan.AssignUniqueId) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) CallExpression(io.prestosql.spi.relation.CallExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) Symbol(io.prestosql.spi.plan.Symbol) JoinNode(io.prestosql.spi.plan.JoinNode) IsNullPredicate(io.prestosql.sql.tree.IsNullPredicate) ProjectNode(io.prestosql.spi.plan.ProjectNode) AggregationNode(io.prestosql.spi.plan.AggregationNode)

Aggregations

ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)44 Test (org.testng.annotations.Test)21 SymbolReference (io.prestosql.sql.tree.SymbolReference)20 Expression (io.prestosql.sql.tree.Expression)19 LongLiteral (io.prestosql.sql.tree.LongLiteral)17 Symbol (io.prestosql.spi.plan.Symbol)13 InListExpression (io.prestosql.sql.tree.InListExpression)11 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)10 NotExpression (io.prestosql.sql.tree.NotExpression)10 StringLiteral (io.prestosql.sql.tree.StringLiteral)10 Cast (io.prestosql.sql.tree.Cast)9 ArrayList (java.util.ArrayList)9 SymbolUtils.toSymbolReference (io.prestosql.sql.planner.SymbolUtils.toSymbolReference)8 Identifier (io.prestosql.sql.tree.Identifier)8 InPredicate (io.prestosql.sql.tree.InPredicate)8 ProjectNode (io.prestosql.spi.plan.ProjectNode)7 BetweenPredicate (io.prestosql.sql.tree.BetweenPredicate)7 GenericLiteral (io.prestosql.sql.tree.GenericLiteral)7 ImmutableList (com.google.common.collect.ImmutableList)6 PlanNode (io.prestosql.spi.plan.PlanNode)6