@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 testSemiJoinNotNeeded() { tester().assertThat(new PruneSemiJoinColumns()) .on(p -> buildProjectedSemiJoin(p, symbol -> symbol.getName().equals("leftValue"))) .matches( strictProject( ImmutableMap.of("leftValue", expression("leftValue")), values("leftKey", "leftKeyHash", "leftValue"))); }
@Test public void ruleAddedTableLayoutToTableScan() { tester().assertThat(pickTableLayout.pickTableLayoutWithoutPredicate()) .on(p -> p.tableScan( nationTableHandle, ImmutableList.of(p.symbol("nationkey", BIGINT)), ImmutableMap.of(p.symbol("nationkey", BIGINT), new TpchColumnHandle("nationkey", BIGINT)))) .matches( constrainedTableScanWithTableLayout("nation", ImmutableMap.of(), ImmutableMap.of("nationkey", "nationkey"))); }
@Test public void testBasics() { tester().assertThat(new PruneOrderByInAggregation(functionRegistry)) .on(this::buildAggregation) .matches( aggregation( singleGroupingSet("key"), ImmutableMap.of( Optional.of("avg"), functionCall("avg", ImmutableList.of("input")), Optional.of("array_agg"), functionCall( "array_agg", ImmutableList.of("input"), ImmutableList.of(sort("input", SortItem.Ordering.ASCENDING, SortItem.NullOrdering.UNDEFINED)))), ImmutableMap.of(), Optional.empty(), SINGLE, values("input", "key", "keyHash", "mask"))); }
@Test public void testWindowNotNeeded() { tester().assertThat(new PruneWindowColumns()) .on(p -> buildProjectedWindow(p, symbol -> inputSymbolNameSet.contains(symbol.getName()), alwaysTrue())) .matches( strictProject( Maps.asMap(inputSymbolNameSet, PlanMatchPattern::expression), values(inputSymbolNameList))); }
@Test public void testNotAllOutputsReferenced() { tester().assertThat(new PruneJoinColumns()) .on(p -> buildProjectedJoin(p, symbol -> symbol.getName().equals("rightValue"))) .matches( strictProject( ImmutableMap.of("rightValue", PlanMatchPattern.expression("rightValue")), join( JoinNode.Type.INNER, ImmutableList.of(equiJoinClause("leftKey", "rightKey")), Optional.empty(), values(ImmutableList.of("leftKey", "leftValue")), values(ImmutableList.of("rightKey", "rightValue"))) .withExactOutputs("rightValue"))); }
@Test public void testRemovesTrueFilter() { tester().assertThat(new RemoveTrivialFilters()) .on(p -> p.filter(p.expression("TRUE"), p.values())) .matches(values()); }
@Test public void testLeftInputNotReferenced() { tester().assertThat(new PruneCrossJoinColumns()) .on(p -> buildProjectedCrossJoin(p, symbol -> symbol.getName().equals("rightValue"))) .matches( strictProject( ImmutableMap.of("rightValue", PlanMatchPattern.expression("rightValue")), join( JoinNode.Type.INNER, ImmutableList.of(), Optional.empty(), strictProject( ImmutableMap.of(), values(ImmutableList.of("leftValue"))), values(ImmutableList.of("rightValue"))) .withExactOutputs("rightValue"))); }
@Test public void testRightInputNotReferenced() { tester().assertThat(new PruneCrossJoinColumns()) .on(p -> buildProjectedCrossJoin(p, symbol -> symbol.getName().equals("leftValue"))) .matches( strictProject( ImmutableMap.of("leftValue", PlanMatchPattern.expression("leftValue")), join( JoinNode.Type.INNER, ImmutableList.of(), Optional.empty(), values(ImmutableList.of("leftValue")), strictProject( ImmutableMap.of(), values(ImmutableList.of("rightValue")))) .withExactOutputs("leftValue"))); }
@Test public void testNotAllColumnsReferenced() { tester().assertThat(new PruneSemiJoinFilteringSourceColumns()) .on(p -> buildSemiJoin(p, symbol -> true)) .matches( semiJoin("leftKey", "rightKey", "match", values("leftKey"), strictProject( ImmutableMap.of( "rightKey", expression("rightKey"), "rightKeyHash", expression("rightKeyHash")), values("rightKey", "rightKeyHash", "rightValue")))); }
@Test public void testNotAllInputsReferenced() { tester().assertThat(new PruneAggregationColumns()) .on(p -> buildProjectedAggregation(p, symbol -> symbol.getName().equals("b"))) .matches( strictProject( ImmutableMap.of("b", expression("b")), aggregation( singleGroupingSet("key"), ImmutableMap.of( Optional.of("b"), functionCall("count", false, ImmutableList.of())), ImmutableMap.of(), Optional.empty(), SINGLE, values("key")))); }
@Test public void testFilterExpressionRewrite() { tester().assertThat(zeroRewriter.filterExpressionRewrite()) .on(p -> p.filter(new LongLiteral("1"), p.values())) .matches( filter("0", values())); }
@Test public void testNotAllInputsRereferenced() { tester().assertThat(new PruneJoinChildrenColumns()) .on(p -> buildJoin(p, symbol -> symbol.getName().equals("leftValue"))) .matches( join( JoinNode.Type.INNER, ImmutableList.of(equiJoinClause("leftKey", "rightKey")), Optional.of("leftValue > 5"), values("leftKey", "leftKeyHash", "leftValue"), strictProject( ImmutableMap.of( "rightKey", PlanMatchPattern.expression("rightKey"), "rightKeyHash", PlanMatchPattern.expression("rightKeyHash")), values("rightKey", "rightKeyHash", "rightValue")))); }
@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 testNotAllInputsReferenced() { tester().assertThat(new PruneTopNColumns()) .on(p -> buildProjectedTopN(p, symbol -> symbol.getName().equals("b"))) .matches( strictProject( ImmutableMap.of("b", expression("b")), topN( COUNT, ImmutableList.of(sort("b", ASCENDING, FIRST)), strictProject( ImmutableMap.of("b", expression("b")), values("a", "b"))))); }