@Test public void testDoesNotFireOnNoCorrelation() { tester().assertThat(new TransformUncorrelatedInPredicateSubqueryToSemiJoin()) .on(p -> p.apply( Assignments.of(), emptyList(), p.values(), p.values())) .doesNotFire(); }
@Test public void testEmptyAssignments() { tester().assertThat(new RemoveUnreferencedScalarApplyNodes()) .on(p -> p.apply( Assignments.of(), ImmutableList.of(), p.values(p.symbol("x")), p.values(p.symbol("y")))) .matches(values("x")); } }
@Test public void testDoesNotFire() { tester().assertThat(new RemoveUnreferencedScalarApplyNodes()) .on(p -> p.apply( Assignments.of(p.symbol("z"), p.expression("x IN (y)")), ImmutableList.of(), p.values(p.symbol("x")), p.values(p.symbol("y")))) .doesNotFire(); }
@Test public void testRewrite() { tester().assertThat(new TransformExistsApplyToLateralNode(tester().getMetadata().getFunctionRegistry())) .on(p -> p.apply( Assignments.of(p.symbol("b", BOOLEAN), expression("EXISTS(SELECT TRUE)")), ImmutableList.of(), p.values(), p.values())) .matches(lateral( ImmutableList.of(), values(ImmutableMap.of()), project( ImmutableMap.of("b", PlanMatchPattern.expression("(\"count_expr\" > CAST(0 AS bigint))")), aggregation(ImmutableMap.of("count_expr", functionCall("count", ImmutableList.of())), values())))); }
@Test public void testDoesNotFireOnNonInPredicateSubquery() { tester().assertThat(new TransformUncorrelatedInPredicateSubqueryToSemiJoin()) .on(p -> p.apply( Assignments.of(p.symbol("x"), new ExistsPredicate(new LongLiteral("1"))), emptyList(), p.values(), p.values())) .doesNotFire(); }
@Test public void testApplyExpressionRewrite() { tester().assertThat(applyRewriter.applyExpressionRewrite()) .on(p -> p.apply( Assignments.of( p.symbol("a", BigintType.BIGINT), new InPredicate( new LongLiteral("1"), new InListExpression(ImmutableList.of( new LongLiteral("1"), new LongLiteral("2"))))), ImmutableList.of(), p.values(), p.values())) .matches( apply( ImmutableList.of(), ImmutableMap.of("a", expression("0 IN (1, 2)")), values(), values())); }
@Test public void testApplyExpressionNotRewritten() { tester().assertThat(applyRewriter.applyExpressionRewrite()) .on(p -> p.apply( Assignments.of( p.symbol("a", BigintType.BIGINT), new InPredicate( new LongLiteral("0"), new InListExpression(ImmutableList.of( new LongLiteral("1"), new LongLiteral("2"))))), ImmutableList.of(), p.values(), p.values())) .doesNotFire(); } }
@Test public void testRewritesToLimit() { tester().assertThat(new TransformExistsApplyToLateralNode(tester().getMetadata().getFunctionRegistry())) .on(p -> p.apply( Assignments.of(p.symbol("b", BOOLEAN), expression("EXISTS(SELECT TRUE)")), ImmutableList.of(p.symbol("corr")), p.values(p.symbol("corr")), p.project(Assignments.of(), p.filter( expression("corr = column"), p.values(p.symbol("column")))))) .matches( project(ImmutableMap.of("b", PlanMatchPattern.expression("COALESCE(subquerytrue, false)")), lateral( ImmutableList.of("corr"), values("corr"), project( ImmutableMap.of("subquerytrue", PlanMatchPattern.expression("true")), limit(1, project(ImmutableMap.of(), node(FilterNode.class, values("column")))))))); } }
@Test public void testFiresForInPredicate() { tester().assertThat(new TransformUncorrelatedInPredicateSubqueryToSemiJoin()) .on(p -> p.apply( Assignments.of( p.symbol("x"), new InPredicate( new SymbolReference("y"), new SymbolReference("z"))), emptyList(), p.values(p.symbol("y")), p.values(p.symbol("z")))) .matches(node(SemiJoinNode.class, values("y"), values("z"))); } }