Search in sources :

Example 1 with SearchedCaseExpression

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

the class TransformCorrelatedInPredicateToJoin method buildInPredicateEquivalent.

private PlanNode buildInPredicateEquivalent(ApplyNode apply, InPredicate inPredicate, VariableReferenceExpression inPredicateOutputVariable, Decorrelated decorrelated, PlanNodeIdAllocator idAllocator, PlanVariableAllocator variableAllocator) {
    Expression correlationCondition = and(decorrelated.getCorrelatedPredicates());
    PlanNode decorrelatedBuildSource = decorrelated.getDecorrelatedNode();
    AssignUniqueId probeSide = new AssignUniqueId(apply.getSourceLocation(), idAllocator.getNextId(), apply.getInput(), variableAllocator.newVariable("unique", BIGINT));
    VariableReferenceExpression buildSideKnownNonNull = variableAllocator.newVariable(inPredicateOutputVariable.getSourceLocation(), "buildSideKnownNonNull", BIGINT);
    ProjectNode buildSide = new ProjectNode(idAllocator.getNextId(), decorrelatedBuildSource, Assignments.builder().putAll(identitiesAsSymbolReferences(decorrelatedBuildSource.getOutputVariables())).put(buildSideKnownNonNull, castToRowExpression(bigint(0))).build());
    checkArgument(inPredicate.getValue() instanceof SymbolReference, "Unexpected expression: %s", inPredicate.getValue());
    SymbolReference probeSideSymbolReference = (SymbolReference) inPredicate.getValue();
    checkArgument(inPredicate.getValueList() instanceof SymbolReference, "Unexpected expression: %s", inPredicate.getValueList());
    SymbolReference buildSideSymbolReference = (SymbolReference) inPredicate.getValueList();
    Expression joinExpression = and(or(new IsNullPredicate(probeSideSymbolReference), new ComparisonExpression(ComparisonExpression.Operator.EQUAL, probeSideSymbolReference, buildSideSymbolReference), new IsNullPredicate(buildSideSymbolReference)), correlationCondition);
    JoinNode leftOuterJoin = leftOuterJoin(idAllocator, probeSide, buildSide, joinExpression);
    VariableReferenceExpression countMatchesVariable = variableAllocator.newVariable(getSourceLocation(buildSideSymbolReference.getLocation()), "countMatches", BIGINT);
    VariableReferenceExpression countNullMatchesVariable = variableAllocator.newVariable(getSourceLocation(buildSideSymbolReference.getLocation()), "countNullMatches", BIGINT);
    Expression matchCondition = and(new IsNotNullPredicate(probeSideSymbolReference), new IsNotNullPredicate(buildSideSymbolReference));
    Expression nullMatchCondition = and(new IsNotNullPredicate(createSymbolReference(buildSideKnownNonNull)), new NotExpression(matchCondition));
    AggregationNode aggregation = new AggregationNode(apply.getSourceLocation(), idAllocator.getNextId(), leftOuterJoin, ImmutableMap.<VariableReferenceExpression, AggregationNode.Aggregation>builder().put(countMatchesVariable, countWithFilter(matchCondition)).put(countNullMatchesVariable, countWithFilter(nullMatchCondition)).build(), singleGroupingSet(probeSide.getOutputVariables()), ImmutableList.of(), AggregationNode.Step.SINGLE, Optional.empty(), 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(countMatchesVariable, 0), booleanConstant(true)), new WhenClause(isGreaterThan(countNullMatchesVariable, 0), booleanConstant(null))), Optional.of(booleanConstant(false)));
    return new ProjectNode(idAllocator.getNextId(), aggregation, Assignments.builder().putAll(identitiesAsSymbolReferences(apply.getInput().getOutputVariables())).put(inPredicateOutputVariable, castToRowExpression(inPredicateEquivalent)).build());
}
Also used : SymbolReference(com.facebook.presto.sql.tree.SymbolReference) ExpressionTreeUtils.createSymbolReference(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) NotExpression(com.facebook.presto.sql.tree.NotExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) IsNotNullPredicate(com.facebook.presto.sql.tree.IsNotNullPredicate) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) WhenClause(com.facebook.presto.sql.tree.WhenClause) PlanNode(com.facebook.presto.spi.plan.PlanNode) AssignUniqueId(com.facebook.presto.sql.planner.plan.AssignUniqueId) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) OriginalExpressionUtils.castToExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) IsNullPredicate(com.facebook.presto.sql.tree.IsNullPredicate) ProjectNode(com.facebook.presto.spi.plan.ProjectNode)

Example 2 with SearchedCaseExpression

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

the class TestEqualityInference method testExpressionsThatMayReturnNullOnNonNullInput.

@Test
public void testExpressionsThatMayReturnNullOnNonNullInput() throws Exception {
    List<Expression> candidates = ImmutableList.of(// try_cast
    new Cast(nameReference("b"), "BIGINT", true), new FunctionCall(QualifiedName.of("try"), ImmutableList.of(nameReference("b"))), new NullIfExpression(nameReference("b"), number(1)), new IfExpression(nameReference("b"), number(1), new NullLiteral()), new DereferenceExpression(nameReference("b"), "x"), new InPredicate(nameReference("b"), new InListExpression(ImmutableList.of(new NullLiteral()))), new SearchedCaseExpression(ImmutableList.of(new WhenClause(new IsNotNullPredicate(nameReference("b")), new NullLiteral())), Optional.empty()), new SimpleCaseExpression(nameReference("b"), ImmutableList.of(new WhenClause(number(1), new NullLiteral())), Optional.empty()), new SubscriptExpression(new ArrayConstructor(ImmutableList.of(new NullLiteral())), nameReference("b")));
    for (Expression candidate : candidates) {
        EqualityInference.Builder builder = new EqualityInference.Builder();
        builder.extractInferenceCandidates(equals(nameReference("b"), nameReference("x")));
        builder.extractInferenceCandidates(equals(nameReference("a"), candidate));
        EqualityInference inference = builder.build();
        List<Expression> equalities = inference.generateEqualitiesPartitionedBy(matchesSymbols("b")).getScopeStraddlingEqualities();
        assertEquals(equalities.size(), 1);
        assertTrue(equalities.get(0).equals(equals(nameReference("x"), nameReference("b"))) || equalities.get(0).equals(equals(nameReference("b"), nameReference("x"))));
    }
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) IfExpression(com.facebook.presto.sql.tree.IfExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) InPredicate(com.facebook.presto.sql.tree.InPredicate) IsNotNullPredicate(com.facebook.presto.sql.tree.IsNotNullPredicate) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) WhenClause(com.facebook.presto.sql.tree.WhenClause) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) IfExpression(com.facebook.presto.sql.tree.IfExpression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) NullLiteral(com.facebook.presto.sql.tree.NullLiteral) Test(org.testng.annotations.Test)

Aggregations

ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)2 Expression (com.facebook.presto.sql.tree.Expression)2 IsNotNullPredicate (com.facebook.presto.sql.tree.IsNotNullPredicate)2 SearchedCaseExpression (com.facebook.presto.sql.tree.SearchedCaseExpression)2 WhenClause (com.facebook.presto.sql.tree.WhenClause)2 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)1 PlanNode (com.facebook.presto.spi.plan.PlanNode)1 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)1 CallExpression (com.facebook.presto.spi.relation.CallExpression)1 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)1 ExpressionTreeUtils.createSymbolReference (com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference)1 AssignUniqueId (com.facebook.presto.sql.planner.plan.AssignUniqueId)1 JoinNode (com.facebook.presto.sql.planner.plan.JoinNode)1 OriginalExpressionUtils.castToExpression (com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression)1 OriginalExpressionUtils.castToRowExpression (com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression)1 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)1 ArrayConstructor (com.facebook.presto.sql.tree.ArrayConstructor)1 Cast (com.facebook.presto.sql.tree.Cast)1 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)1 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)1