@Test public void testRemoveUnreferencedInput() { tester().assertThat(new RemoveUnreferencedScalarLateralNodes()) .on(p -> p.lateral( emptyList(), p.values(p.symbol("x", BigintType.BIGINT)), p.values(emptyList(), ImmutableList.of(emptyList())))) .matches(values("x")); }
@Test public void testRemoveUnreferencedSubquery() { tester().assertThat(new RemoveUnreferencedScalarLateralNodes()) .on(p -> p.lateral( emptyList(), p.values(emptyList(), ImmutableList.of(emptyList())), p.values(p.symbol("x", BigintType.BIGINT)))) .matches(values("x")); }
@Test public void doesNotFireOnUncorrelated() { tester().assertThat(rule) .on(p -> p.lateral( ImmutableList.<Symbol>of(), p.values(p.symbol("a")), p.values(ImmutableList.of(p.symbol("b")), ImmutableList.of(expressions("1"))))) .doesNotFire(); }
@Test public void testDoesNotFire() { tester().assertThat(new RemoveUnreferencedScalarLateralNodes()) .on(p -> p.lateral( emptyList(), p.values(), p.values())) .doesNotFire(); } }
@Test public void testDoesNotFire() { Symbol symbol = new Symbol("x"); tester() .assertThat(new TransformUncorrelatedLateralToJoin()) .on(p -> p.lateral(ImmutableList.of(symbol), p.values(symbol), p.values())) .doesNotFire(); } }
@Test public void test() { tester() .assertThat(new TransformUncorrelatedLateralToJoin()) .on(p -> p.lateral(emptyList(), p.values(), p.values())) .matches(join(JoinNode.Type.INNER, emptyList(), values(), values())); }
@Test public void doesNotFireOnCorrelatedNonScalar() { tester().assertThat(rule) .on(p -> p.lateral( ImmutableList.of(p.symbol("corr")), p.values(p.symbol("corr")), p.values(p.symbol("a")))) .doesNotFire(); }
@Test public void testDoesNotFireWithEmptyValuesNode() { tester().assertThat(new TransformCorrelatedSingleRowSubqueryToProject()) .on(p -> p.lateral( ImmutableList.of(p.symbol("a")), p.values(p.symbol("a")), p.values(p.symbol("a")))) .doesNotFire(); } }
@Test public void doesNotFireOnUncorrelated() { tester().assertThat(new TransformCorrelatedScalarAggregationToJoin(tester().getMetadata().getFunctionRegistry())) .on(p -> p.lateral( ImmutableList.of(), p.values(p.symbol("a")), p.values(p.symbol("b")))) .doesNotFire(); }
@Test public void testRewrite() { tester().assertThat(new TransformCorrelatedSingleRowSubqueryToProject()) .on(p -> p.lateral( ImmutableList.of(p.symbol("l_nationkey")), p.tableScan(new TableHandle( new ConnectorId("local"), new TpchTableHandle("nation", TINY_SCALE_FACTOR)), ImmutableList.of(p.symbol("l_nationkey")), ImmutableMap.of(p.symbol("l_nationkey"), new TpchColumnHandle("nationkey", BIGINT))), p.project( Assignments.of(p.symbol("l_expr2"), expression("l_nationkey + 1")), p.values( ImmutableList.of(), ImmutableList.of( ImmutableList.of()))))) .matches(project( ImmutableMap.of( ("l_expr2"), PlanMatchPattern.expression("l_nationkey + 1"), "l_nationkey", PlanMatchPattern.expression("l_nationkey")), tableScan("nation", ImmutableMap.of("l_nationkey", "nationkey")))); }
@Test public void doesNotFireOnCorrelatedWithoutAggregation() { tester().assertThat(new TransformCorrelatedScalarAggregationToJoin(tester().getMetadata().getFunctionRegistry())) .on(p -> p.lateral( ImmutableList.of(p.symbol("corr")), p.values(p.symbol("corr")), p.values(p.symbol("a")))) .doesNotFire(); }
@Test public void rewritesScalarSubquery() { tester().assertThat(rule) .on(p -> p.lateral( ImmutableList.of(p.symbol("corr")), p.values(p.symbol("corr")), p.enforceSingleRow( p.filter( p.expression("1 = a"), // TODO use correlated predicate, it requires support for correlated subqueries in plan matchers p.values(ImmutableList.of(p.symbol("a")), ONE_ROW))))) .matches( lateral( ImmutableList.of("corr"), values("corr"), filter( "1 = a", values("a")))); }
@Test public void rewritesOnSubqueryWithoutProjection() { tester().assertThat(new TransformCorrelatedScalarAggregationToJoin(tester().getMetadata().getFunctionRegistry())) .on(p -> p.lateral( ImmutableList.of(p.symbol("corr")), p.values(p.symbol("corr")), p.aggregation(ab -> ab .source(p.values(p.symbol("a"), p.symbol("b"))) .addAggregation(p.symbol("sum"), PlanBuilder.expression("sum(a)"), ImmutableList.of(BIGINT)) .globalGrouping()))) .matches( project(ImmutableMap.of("sum_1", expression("sum_1"), "corr", expression("corr")), aggregation(ImmutableMap.of("sum_1", functionCall("sum", ImmutableList.of("a"))), join(JoinNode.Type.LEFT, ImmutableList.of(), assignUniqueId("unique", values(ImmutableMap.of("corr", 0))), project(ImmutableMap.of("non_null", expression("true")), values(ImmutableMap.of("a", 0, "b", 1))))))); }
@Test public void rewritesOnSubqueryWithProjection() { tester().assertThat(new TransformCorrelatedScalarAggregationToJoin(tester().getMetadata().getFunctionRegistry())) .on(p -> p.lateral( ImmutableList.of(p.symbol("corr")), p.values(p.symbol("corr")), p.project(Assignments.of(p.symbol("expr"), p.expression("sum + 1")), p.aggregation(ab -> ab .source(p.values(p.symbol("a"), p.symbol("b"))) .addAggregation(p.symbol("sum"), PlanBuilder.expression("sum(a)"), ImmutableList.of(BIGINT)) .globalGrouping())))) .matches( project(ImmutableMap.of("corr", expression("corr"), "expr", expression("(\"sum_1\" + 1)")), aggregation(ImmutableMap.of("sum_1", functionCall("sum", ImmutableList.of("a"))), join(JoinNode.Type.LEFT, ImmutableList.of(), assignUniqueId("unique", values(ImmutableMap.of("corr", 0))), project(ImmutableMap.of("non_null", expression("true")), values(ImmutableMap.of("a", 0, "b", 1))))))); } }
@Test public void rewritesOnSubqueryWithoutProjection() { tester().assertThat(rule) .on(p -> p.lateral( ImmutableList.of(p.symbol("corr")), p.values(p.symbol("corr")), p.enforceSingleRow( p.filter( p.expression("1 = a"), // TODO use correlated predicate, it requires support for correlated subqueries in plan matchers p.values(ImmutableList.of(p.symbol("a")), TWO_ROWS))))) .matches( project( filter( ensureScalarSubquery(), markDistinct( "is_distinct", ImmutableList.of("corr", "unique"), lateral( ImmutableList.of("corr"), assignUniqueId( "unique", values("corr")), filter( "1 = a", values("a"))))))); }
@Test public void doesNotFireOnCorrelatedWithNonScalarAggregation() { tester().assertThat(new TransformCorrelatedScalarAggregationToJoin(tester().getMetadata().getFunctionRegistry())) .on(p -> p.lateral( ImmutableList.of(p.symbol("corr")), p.values(p.symbol("corr")), p.aggregation(ab -> ab .source(p.values(p.symbol("a"), p.symbol("b"))) .addAggregation(p.symbol("sum"), PlanBuilder.expression("sum(a)"), ImmutableList.of(BIGINT)) .singleGroupingSet(p.symbol("b"))))) .doesNotFire(); }
@Test public void rewritesOnSubqueryWithProjection() { tester().assertThat(rule) .on(p -> p.lateral( ImmutableList.of(p.symbol("corr")), p.values(p.symbol("corr")), p.enforceSingleRow( p.project( Assignments.of(p.symbol("a2"), p.expression("a * 2")), p.filter( p.expression("1 = a"), // TODO use correlated predicate, it requires support for correlated subqueries in plan matchers p.values(ImmutableList.of(p.symbol("a")), TWO_ROWS)))))) .matches( project( filter( ensureScalarSubquery(), markDistinct( "is_distinct", ImmutableList.of("corr", "unique"), lateral( ImmutableList.of("corr"), assignUniqueId( "unique", values("corr")), project(ImmutableMap.of("a2", expression("a * 2")), filter("1 = a", values("a")))))))); }
.on(p -> p.lateral( ImmutableList.of(p.symbol("corr")), p.values(p.symbol("corr")),
@Test public void testDoesNotFire() { tester().assertThat(new TransformExistsApplyToLateralNode(tester().getMetadata().getFunctionRegistry())) .on(p -> p.values(p.symbol("a"))) .doesNotFire(); tester().assertThat(new TransformExistsApplyToLateralNode(tester().getMetadata().getFunctionRegistry())) .on(p -> p.lateral( ImmutableList.of(p.symbol("a")), p.values(p.symbol("a")), p.values(p.symbol("a")))) .doesNotFire(); }