@Test public void testValueExpressionNotRewritten() { tester().assertThat(zeroRewriter.valuesExpressionRewrite()) .on(p -> p.values( ImmutableList.<Symbol>of(p.symbol("a")), ImmutableList.of((ImmutableList.of(PlanBuilder.expression("0")))))) .doesNotFire(); }
private void testReplicateNoEquiCriteria(Type joinType) { assertDetermineJoinDistributionType() .on(p -> p.join( joinType, p.values(ImmutableList.of(p.symbol("A1")), ImmutableList.of(expressions("10"), expressions("11"))), p.values(ImmutableList.of(p.symbol("B1")), ImmutableList.of(expressions("50"), expressions("11"))), ImmutableList.of(), ImmutableList.of(p.symbol("A1", BIGINT), p.symbol("B1", BIGINT)), Optional.of(expression("A1 * B1 > 100")))) .setSystemProperty(JOIN_DISTRIBUTION_TYPE, JoinDistributionType.PARTITIONED.name()) .matches(join( joinType, ImmutableList.of(), Optional.of("A1 * B1 > 100"), Optional.of(DistributionType.REPLICATED), values(ImmutableMap.of("A1", 0)), values(ImmutableMap.of("B1", 0)))); }
private ProjectNode buildProjectedFilter(PlanBuilder planBuilder, Predicate<Symbol> projectionFilter) { Symbol a = planBuilder.symbol("a"); Symbol b = planBuilder.symbol("b"); return planBuilder.project( Assignments.identity(Stream.of(a, b).filter(projectionFilter).collect(toImmutableSet())), planBuilder.filter( planBuilder.expression("b > 5"), planBuilder.values(a, b))); } }
@Test public void testRewriteBasicLambda() { final Map<Symbol, Type> symbols = ImmutableMap.of(new Symbol("a"), BigintType.BIGINT); final SymbolAllocator allocator = new SymbolAllocator(symbols); assertEquals(rewrite(expression("x -> a + x"), allocator.getTypes(), allocator), new BindExpression( ImmutableList.of(expression("a")), new LambdaExpression( Stream.of("a_0", "x") .map(Identifier::new) .map(LambdaArgumentDeclaration::new) .collect(toList()), expression("a_0 + x")))); } }
@Test public void testValueExpressionRewrite() { tester().assertThat(zeroRewriter.valuesExpressionRewrite()) .on(p -> p.values( ImmutableList.<Symbol>of(p.symbol("a")), ImmutableList.of((ImmutableList.of(PlanBuilder.expression("1")))))) .matches( values(ImmutableList.of("a"), ImmutableList.of(ImmutableList.of(new LongLiteral("0"))))); }
@Test public void testRemovesFalseFilter() { tester().assertThat(new RemoveTrivialFilters()) .on(p -> p.filter( p.expression("FALSE"), p.values( ImmutableList.of(p.symbol("a")), ImmutableList.of(p.expressions("1"))))) .matches(values("a")); } }
@Test public void testNotAllOutputsReferenced() { tester().assertThat(new PruneValuesColumns()) .on(p -> p.project( Assignments.of(p.symbol("y"), expression("x")), p.values( ImmutableList.of(p.symbol("unused"), p.symbol("x")), ImmutableList.of( ImmutableList.of(expression("1"), expression("2")), ImmutableList.of(expression("3"), expression("4")))))) .matches( project( ImmutableMap.of("y", PlanMatchPattern.expression("x")), values( ImmutableList.of("x"), ImmutableList.of( ImmutableList.of(expression("2")), ImmutableList.of(expression("4")))))); }
@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 test() { tester().assertThat(new EvaluateZeroLimit()) .on(p -> p.limit( 0, p.filter( expression("b > 5"), p.values( ImmutableList.of(p.symbol("a"), p.symbol("b")), ImmutableList.of( expressions("1", "10"), expressions("2", "11")))))) // TODO: verify contents .matches(values(ImmutableMap.of())); } }
@Test(expectedExceptions = AssertionError.class, expectedExceptionsMessageRegExp = "Plan does not match, expected .* but found .*") public void testReportWrongMatch() { try (RuleTester tester = new RuleTester()) { tester.assertThat(new DummyReplaceNodeRule()) .on(p -> p.project( Assignments.of(p.symbol("y"), expression("x")), p.values( ImmutableList.of(p.symbol("x")), ImmutableList.of(ImmutableList.of(expression("1")))))) .matches( values(ImmutableList.of("different"), ImmutableList.of())); } }
@Test public void test() { tester().assertThat(new RemoveFullSample()) .on(p -> p.sample( 1.0, Type.BERNOULLI, p.filter( expression("b > 5"), p.values( ImmutableList.of(p.symbol("a"), p.symbol("b")), ImmutableList.of( expressions("1", "10"), expressions("2", "11")))))) // TODO: verify contents .matches(filter("b > 5", values(ImmutableMap.of("a", 0, "b", 1)))); } }
@Test public void testMultipleAggregations() { tester().assertThat(new MultipleDistinctAggregationToMarkDistinct()) .on(p -> p.aggregation(builder -> builder .globalGrouping() .addAggregation(p.symbol("output1"), expression("count(DISTINCT input)"), ImmutableList.of(BIGINT)) .addAggregation(p.symbol("output2"), expression("sum(DISTINCT input)"), ImmutableList.of(BIGINT)) .source( p.values(p.symbol("input"))))) .doesNotFire(); }
@Test public void test() { tester().assertThat(new MergeFilters()) .on(p -> p.filter(expression("b > 44"), p.filter(expression("a < 42"), p.values(p.symbol("a"), p.symbol("b"))))) .matches(filter("(a < 42) AND (b > 44)", values(ImmutableMap.of("a", 0, "b", 1)))); } }
@Test public void test() { tester().assertThat(new EvaluateZeroSample()) .on(p -> p.sample( 0, Type.BERNOULLI, p.filter( expression("b > 5"), p.values( ImmutableList.of(p.symbol("a"), p.symbol("b")), ImmutableList.of( expressions("1", "10"), expressions("2", "11")))))) // TODO: verify contents .matches(values(ImmutableMap.of())); } }
@Test public void testMultipleDistincts() { tester().assertThat(new SingleDistinctAggregationToGroupBy()) .on(p -> p.aggregation(builder -> builder .globalGrouping() .addAggregation(p.symbol("output1"), expression("count(DISTINCT input1)"), ImmutableList.of(BIGINT)) .addAggregation(p.symbol("output2"), expression("count(DISTINCT input2)"), ImmutableList.of(BIGINT)) .source( p.values( p.symbol("input1"), p.symbol("input2"))))) .doesNotFire(); }
@Test public void testNoDistinct() { tester().assertThat(new SingleDistinctAggregationToGroupBy()) .on(p -> p.aggregation(builder -> builder .globalGrouping() .addAggregation(p.symbol("output1"), expression("count(input1)"), ImmutableList.of(BIGINT)) .addAggregation(p.symbol("output2"), expression("count(input2)"), ImmutableList.of(BIGINT)) .source( p.values( p.symbol("input1"), p.symbol("input2"))))) .doesNotFire(); }
@Test public void testProjectionExpressionNotRewritten() { tester().assertThat(zeroRewriter.projectExpressionRewrite()) .on(p -> p.project( Assignments.of(p.symbol("y"), PlanBuilder.expression("0")), p.values(p.symbol("x")))) .doesNotFire(); }
@Test public void testMixedDistinctAndNonDistinct() { tester().assertThat(new SingleDistinctAggregationToGroupBy()) .on(p -> p.aggregation(builder -> builder .globalGrouping() .addAggregation(p.symbol("output1"), expression("count(DISTINCT input1)"), ImmutableList.of(BIGINT)) .addAggregation(p.symbol("output2"), expression("count(input2)"), ImmutableList.of(BIGINT)) .source( p.values( p.symbol("input1"), p.symbol("input2"))))) .doesNotFire(); }
@Test public void testAllOutputsReferenced() { tester().assertThat(new PruneValuesColumns()) .on(p -> p.project( Assignments.of(p.symbol("y"), expression("x")), p.values(p.symbol("x")))) .doesNotFire(); } }
@Test public void testDistinctWithFilter() { tester().assertThat(new SingleDistinctAggregationToGroupBy()) .on(p -> p.aggregation(builder -> builder .globalGrouping() .addAggregation(p.symbol("output"), expression("count(DISTINCT input1) filter (where input2 > 0)"), ImmutableList.of(BIGINT)) .source( p.values( p.symbol("input1"), p.symbol("input2"))))) .doesNotFire(); }