Search in sources :

Example 1 with SimpleCaseExpression

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

the class TransformCorrelatedScalarSubquery method apply.

@Override
public Result apply(LateralJoinNode lateralJoinNode, Captures captures, Context context) {
    PlanNode subquery = context.getLookup().resolve(lateralJoinNode.getSubquery());
    if (!searchFrom(subquery, context.getLookup()).where(EnforceSingleRowNode.class::isInstance).recurseOnlyWhen(ProjectNode.class::isInstance).matches()) {
        return Result.empty();
    }
    PlanNode rewrittenSubquery = searchFrom(subquery, context.getLookup()).where(EnforceSingleRowNode.class::isInstance).recurseOnlyWhen(ProjectNode.class::isInstance).removeFirst();
    if (isAtMostScalar(rewrittenSubquery, context.getLookup())) {
        return Result.ofPlanNode(new LateralJoinNode(lateralJoinNode.getSourceLocation(), context.getIdAllocator().getNextId(), lateralJoinNode.getInput(), rewrittenSubquery, lateralJoinNode.getCorrelation(), lateralJoinNode.getType(), lateralJoinNode.getOriginSubqueryError()));
    }
    VariableReferenceExpression unique = context.getVariableAllocator().newVariable("unique", BIGINT);
    LateralJoinNode rewrittenLateralJoinNode = new LateralJoinNode(lateralJoinNode.getSourceLocation(), context.getIdAllocator().getNextId(), new AssignUniqueId(lateralJoinNode.getSourceLocation(), context.getIdAllocator().getNextId(), lateralJoinNode.getInput(), unique), rewrittenSubquery, lateralJoinNode.getCorrelation(), lateralJoinNode.getType(), lateralJoinNode.getOriginSubqueryError());
    VariableReferenceExpression isDistinct = context.getVariableAllocator().newVariable("is_distinct", BooleanType.BOOLEAN);
    MarkDistinctNode markDistinctNode = new MarkDistinctNode(rewrittenLateralJoinNode.getSourceLocation(), context.getIdAllocator().getNextId(), rewrittenLateralJoinNode, isDistinct, rewrittenLateralJoinNode.getInput().getOutputVariables(), Optional.empty());
    FilterNode filterNode = new FilterNode(markDistinctNode.getSourceLocation(), context.getIdAllocator().getNextId(), markDistinctNode, castToRowExpression(new SimpleCaseExpression(createSymbolReference(isDistinct), ImmutableList.of(new WhenClause(TRUE_LITERAL, TRUE_LITERAL)), Optional.of(new Cast(new FunctionCall(QualifiedName.of("fail"), ImmutableList.of(new LongLiteral(Integer.toString(SUBQUERY_MULTIPLE_ROWS.toErrorCode().getCode())), new StringLiteral("Scalar sub-query has returned multiple rows"))), BOOLEAN)))));
    return Result.ofPlanNode(new ProjectNode(context.getIdAllocator().getNextId(), filterNode, identityAssignmentsAsSymbolReferences(lateralJoinNode.getOutputVariables())));
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) MarkDistinctNode(com.facebook.presto.spi.plan.MarkDistinctNode) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) FilterNode(com.facebook.presto.spi.plan.FilterNode) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) WhenClause(com.facebook.presto.sql.tree.WhenClause) PlanNode(com.facebook.presto.spi.plan.PlanNode) AssignUniqueId(com.facebook.presto.sql.planner.plan.AssignUniqueId) LateralJoinNode(com.facebook.presto.sql.planner.plan.LateralJoinNode) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) EnforceSingleRowNode(com.facebook.presto.sql.planner.plan.EnforceSingleRowNode) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall)

Example 2 with SimpleCaseExpression

use of com.facebook.presto.sql.tree.SimpleCaseExpression 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

Cast (com.facebook.presto.sql.tree.Cast)2 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)2 SimpleCaseExpression (com.facebook.presto.sql.tree.SimpleCaseExpression)2 WhenClause (com.facebook.presto.sql.tree.WhenClause)2 FilterNode (com.facebook.presto.spi.plan.FilterNode)1 MarkDistinctNode (com.facebook.presto.spi.plan.MarkDistinctNode)1 PlanNode (com.facebook.presto.spi.plan.PlanNode)1 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)1 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)1 AssignUniqueId (com.facebook.presto.sql.planner.plan.AssignUniqueId)1 EnforceSingleRowNode (com.facebook.presto.sql.planner.plan.EnforceSingleRowNode)1 LateralJoinNode (com.facebook.presto.sql.planner.plan.LateralJoinNode)1 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)1 ArrayConstructor (com.facebook.presto.sql.tree.ArrayConstructor)1 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)1 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)1 Expression (com.facebook.presto.sql.tree.Expression)1 IfExpression (com.facebook.presto.sql.tree.IfExpression)1 InListExpression (com.facebook.presto.sql.tree.InListExpression)1 InPredicate (com.facebook.presto.sql.tree.InPredicate)1