Search in sources :

Example 16 with ComparisonExpression

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

the class ImplementOffset method apply.

@Override
public Result apply(OffsetNode parent, Captures captures, Context context) {
    VariableReferenceExpression rowNumberSymbol = context.getVariableAllocator().newVariable("row_number", BIGINT);
    RowNumberNode rowNumberNode = new RowNumberNode(parent.getSourceLocation(), context.getIdAllocator().getNextId(), parent.getSource(), ImmutableList.of(), rowNumberSymbol, Optional.empty(), Optional.empty());
    FilterNode filterNode = new FilterNode(parent.getSourceLocation(), context.getIdAllocator().getNextId(), rowNumberNode, castToRowExpression(new ComparisonExpression(ComparisonExpression.Operator.GREATER_THAN, createSymbolReference(rowNumberSymbol), new GenericLiteral("BIGINT", Long.toString(parent.getCount())))));
    ProjectNode projectNode = new ProjectNode(context.getIdAllocator().getNextId(), filterNode, identityAssignmentsAsSymbolReferences(parent.getOutputVariables()));
    return Result.ofPlanNode(projectNode);
}
Also used : ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) FilterNode(com.facebook.presto.spi.plan.FilterNode) RowNumberNode(com.facebook.presto.sql.planner.plan.RowNumberNode) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral)

Example 17 with ComparisonExpression

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

the class TestComparisonStatsCalculator method symbolToLiteralEqualStats.

@Test
public void symbolToLiteralEqualStats() {
    // Simple case
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("y"), new DoubleLiteral("2.5"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    25.0).variableStats(new VariableReferenceExpression(Optional.empty(), "y", DOUBLE), symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(2.5).highValue(2.5).nullsFraction(0.0);
    });
    // Literal on the edge of symbol range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("x"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.75).variableStats(new VariableReferenceExpression(Optional.empty(), "x", DOUBLE), symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(10.0).highValue(10.0).nullsFraction(0.0);
    });
    // Literal out of symbol range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("y"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    0.0).variableStats(new VariableReferenceExpression(Optional.empty(), "y", DOUBLE), symbolAssert -> {
        symbolAssert.averageRowSize(0.0).distinctValuesCount(0.0).emptyRange().nullsFraction(1.0);
    });
    // Literal in left open range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("leftOpen"), new DoubleLiteral("2.5"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.0).variableStats(new VariableReferenceExpression(Optional.empty(), "leftOpen", DOUBLE), symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(2.5).highValue(2.5).nullsFraction(0.0);
    });
    // Literal in right open range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("rightOpen"), new DoubleLiteral("-2.5"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.0).variableStats(new VariableReferenceExpression(Optional.empty(), "rightOpen", DOUBLE), symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(-2.5).highValue(-2.5).nullsFraction(0.0);
    });
    // Literal in unknown range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("unknownRange"), new DoubleLiteral("0.0"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.0).variableStats(new VariableReferenceExpression(Optional.empty(), "unknownRange", DOUBLE), symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(0.0).highValue(0.0).nullsFraction(0.0);
    });
    // Literal in empty range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("emptyRange"), new DoubleLiteral("0.0"))).outputRowsCount(0.0).variableStats(new VariableReferenceExpression(Optional.empty(), "emptyRange", DOUBLE), equalTo(emptyRangeStats));
    // Column with values not representable as double (unknown range)
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("varchar"), new StringLiteral("blah"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.0).variableStats(new VariableReferenceExpression(Optional.empty(), "varchar", VarcharType.createVarcharType(10)), symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(NEGATIVE_INFINITY).highValue(POSITIVE_INFINITY).nullsFraction(0.0);
    });
}
Also used : ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) DoubleLiteral(com.facebook.presto.sql.tree.DoubleLiteral) Test(org.testng.annotations.Test)

Example 18 with ComparisonExpression

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

the class DomainTranslator method extractDisjuncts.

private static List<Expression> extractDisjuncts(Type type, Ranges ranges, SymbolReference reference) {
    List<Expression> disjuncts = new ArrayList<>();
    List<Expression> singleValues = new ArrayList<>();
    List<Range> orderedRanges = ranges.getOrderedRanges();
    SortedRangeSet sortedRangeSet = SortedRangeSet.copyOf(type, orderedRanges);
    SortedRangeSet complement = sortedRangeSet.complement();
    List<Range> singleValueExclusionsList = complement.getOrderedRanges().stream().filter(Range::isSingleValue).collect(toList());
    List<Range> originalUnionSingleValues = SortedRangeSet.copyOf(type, singleValueExclusionsList).union(sortedRangeSet).getOrderedRanges();
    PeekingIterator<Range> singleValueExclusions = peekingIterator(singleValueExclusionsList.iterator());
    for (Range range : originalUnionSingleValues) {
        if (range.isSingleValue()) {
            singleValues.add(toExpression(range.getSingleValue(), type));
            continue;
        }
        // attempt to optimize ranges that can be coalesced as long as single value points are excluded
        List<Expression> singleValuesInRange = new ArrayList<>();
        while (singleValueExclusions.hasNext() && range.contains(singleValueExclusions.peek())) {
            singleValuesInRange.add(toExpression(singleValueExclusions.next().getSingleValue(), type));
        }
        if (!singleValuesInRange.isEmpty()) {
            disjuncts.add(combineRangeWithExcludedPoints(type, reference, range, singleValuesInRange));
            continue;
        }
        disjuncts.add(processRange(type, range, reference));
    }
    // Add back all of the possible single values either as an equality or an IN predicate
    if (singleValues.size() == 1) {
        disjuncts.add(new ComparisonExpression(EQUAL, reference, getOnlyElement(singleValues)));
    } else if (singleValues.size() > 1) {
        disjuncts.add(new InPredicate(reference, new InListExpression(singleValues)));
    }
    return disjuncts;
}
Also used : ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) SortedRangeSet(com.facebook.presto.spi.predicate.SortedRangeSet) NotExpression(com.facebook.presto.sql.tree.NotExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LiteralInterpreter.toExpression(com.facebook.presto.sql.planner.LiteralInterpreter.toExpression) Expression(com.facebook.presto.sql.tree.Expression) ArrayList(java.util.ArrayList) InListExpression(com.facebook.presto.sql.tree.InListExpression) Range(com.facebook.presto.spi.predicate.Range) InPredicate(com.facebook.presto.sql.tree.InPredicate)

Example 19 with ComparisonExpression

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

the class EffectivePredicateExtractor method visitJoin.

@Override
public Expression visitJoin(JoinNode node, Void context) {
    Expression leftPredicate = node.getLeft().accept(this, context);
    Expression rightPredicate = node.getRight().accept(this, context);
    List<Expression> joinConjuncts = new ArrayList<>();
    for (JoinNode.EquiJoinClause clause : node.getCriteria()) {
        joinConjuncts.add(new ComparisonExpression(ComparisonExpressionType.EQUAL, clause.getLeft().toSymbolReference(), clause.getRight().toSymbolReference()));
    }
    switch(node.getType()) {
        case INNER:
            return combineConjuncts(ImmutableList.<Expression>builder().add(pullExpressionThroughSymbols(leftPredicate, node.getOutputSymbols())).add(pullExpressionThroughSymbols(rightPredicate, node.getOutputSymbols())).addAll(pullExpressionsThroughSymbols(joinConjuncts, node.getOutputSymbols())).build());
        case LEFT:
            return combineConjuncts(ImmutableList.<Expression>builder().add(pullExpressionThroughSymbols(leftPredicate, node.getOutputSymbols())).addAll(pullNullableConjunctsThroughOuterJoin(extractConjuncts(rightPredicate), node.getOutputSymbols(), node.getRight().getOutputSymbols()::contains)).addAll(pullNullableConjunctsThroughOuterJoin(joinConjuncts, node.getOutputSymbols(), node.getRight().getOutputSymbols()::contains)).build());
        case RIGHT:
            return combineConjuncts(ImmutableList.<Expression>builder().add(pullExpressionThroughSymbols(rightPredicate, node.getOutputSymbols())).addAll(pullNullableConjunctsThroughOuterJoin(extractConjuncts(leftPredicate), node.getOutputSymbols(), node.getLeft().getOutputSymbols()::contains)).addAll(pullNullableConjunctsThroughOuterJoin(joinConjuncts, node.getOutputSymbols(), node.getLeft().getOutputSymbols()::contains)).build());
        case FULL:
            return combineConjuncts(ImmutableList.<Expression>builder().addAll(pullNullableConjunctsThroughOuterJoin(extractConjuncts(leftPredicate), node.getOutputSymbols(), node.getLeft().getOutputSymbols()::contains)).addAll(pullNullableConjunctsThroughOuterJoin(extractConjuncts(rightPredicate), node.getOutputSymbols(), node.getRight().getOutputSymbols()::contains)).addAll(pullNullableConjunctsThroughOuterJoin(joinConjuncts, node.getOutputSymbols(), node.getLeft().getOutputSymbols()::contains, node.getRight().getOutputSymbols()::contains)).build());
        default:
            throw new UnsupportedOperationException("Unknown join type: " + node.getType());
    }
}
Also used : ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) SemiJoinNode(com.facebook.presto.sql.planner.plan.SemiJoinNode) ArrayList(java.util.ArrayList)

Example 20 with ComparisonExpression

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

the class TestEqualityInference method equalityAsSet.

private static Set<Expression> equalityAsSet(Expression expression) {
    Preconditions.checkArgument(expression instanceof ComparisonExpression);
    ComparisonExpression comparisonExpression = (ComparisonExpression) expression;
    Preconditions.checkArgument(comparisonExpression.getType() == EQUAL);
    return ImmutableSet.of(comparisonExpression.getLeft(), comparisonExpression.getRight());
}
Also used : ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression)

Aggregations

ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)34 Test (org.testng.annotations.Test)16 Expression (com.facebook.presto.sql.tree.Expression)15 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)12 SymbolReference (com.facebook.presto.sql.tree.SymbolReference)12 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)10 ArrayList (java.util.ArrayList)10 LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)9 NotExpression (com.facebook.presto.sql.tree.NotExpression)9 InListExpression (com.facebook.presto.sql.tree.InListExpression)7 Cast (com.facebook.presto.sql.tree.Cast)6 StringLiteral (com.facebook.presto.sql.tree.StringLiteral)6 DoubleLiteral (com.facebook.presto.sql.tree.DoubleLiteral)5 ImmutableList (com.google.common.collect.ImmutableList)5 InPredicate (com.facebook.presto.sql.tree.InPredicate)4 IsNullPredicate (com.facebook.presto.sql.tree.IsNullPredicate)4 Session (com.facebook.presto.Session)3 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)3 JoinNode (com.facebook.presto.sql.planner.plan.JoinNode)3 BetweenPredicate (com.facebook.presto.sql.tree.BetweenPredicate)3