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 testDoesNotFireForExpressionsInCanonicalForm() { tester().assertThat(canonicalizeExpressions.filterExpressionRewrite()) .on(p -> p.filter(FALSE_LITERAL, p.values())) .doesNotFire(); }
@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 testFilterExpressionNotRewritten() { tester().assertThat(zeroRewriter.filterExpressionRewrite()) .on(p -> p.filter(new LongLiteral("0"), p.values())) .doesNotFire(); }
@Test public void testDoesNotFire() { tester().assertThat(new RemoveTrivialFilters()) .on(p -> p.filter(p.expression("1 = 1"), p.values())) .doesNotFire(); }
@Test public void doesNotFireIfRuleNotChangePlan() { tester().assertThat(pickTableLayout.pickTableLayoutForPredicate()) .on(p -> p.filter(expression("nationkey % 17 = BIGINT '44' AND nationkey % 15 = BIGINT '43'"), p.tableScan( nationTableHandle, ImmutableList.of(p.symbol("nationkey", BIGINT)), ImmutableMap.of(p.symbol("nationkey", BIGINT), new TpchColumnHandle("nationkey", BIGINT)), Optional.of(nationTableLayoutHandle), TupleDomain.all(), TupleDomain.all()))) .doesNotFire(); }
@Test public void ruleAddedTableLayoutToFilterTableScan() { Map<String, Domain> filterConstraint = ImmutableMap.<String, Domain>builder() .put("orderstatus", singleValue(createVarcharType(1), utf8Slice("F"))) .build(); tester().assertThat(pickTableLayout.pickTableLayoutForPredicate()) .on(p -> p.filter(expression("orderstatus = CAST ('F' AS VARCHAR(1))"), p.tableScan( ordersTableHandle, ImmutableList.of(p.symbol("orderstatus", createVarcharType(1))), ImmutableMap.of(p.symbol("orderstatus", createVarcharType(1)), new TpchColumnHandle("orderstatus", createVarcharType(1)))))) .matches( constrainedTableScanWithTableLayout("orders", filterConstraint, ImmutableMap.of("orderstatus", "orderstatus"))); }
@Test public void eliminateTableScanWhenNoLayoutExist() { tester().assertThat(pickTableLayout.pickTableLayoutForPredicate()) .on(p -> p.filter(expression("orderstatus = 'G'"), p.tableScan( ordersTableHandle, ImmutableList.of(p.symbol("orderstatus", createVarcharType(1))), ImmutableMap.of(p.symbol("orderstatus", createVarcharType(1)), new TpchColumnHandle("orderstatus", createVarcharType(1))), Optional.of(ordersTableLayoutHandle)))) .matches(values("A")); }
@Test public void ruleWithPushdownableToTableLayoutPredicate() { Type orderStatusType = createVarcharType(1); tester().assertThat(pickTableLayout.pickTableLayoutForPredicate()) .on(p -> p.filter(expression("orderstatus = 'O'"), p.tableScan( ordersTableHandle, ImmutableList.of(p.symbol("orderstatus", orderStatusType)), ImmutableMap.of(p.symbol("orderstatus", orderStatusType), new TpchColumnHandle("orderstatus", orderStatusType))))) .matches(constrainedTableScanWithTableLayout( "orders", ImmutableMap.of("orderstatus", singleValue(orderStatusType, utf8Slice("O"))), ImmutableMap.of("orderstatus", "orderstatus"))); }
@Test public void testRemovesTrueFilter() { tester().assertThat(new RemoveTrivialFilters()) .on(p -> p.filter(p.expression("TRUE"), p.values())) .matches(values()); }
@Test public void replaceWithExistsWhenNoLayoutExist() { ColumnHandle columnHandle = new TpchColumnHandle("nationkey", BIGINT); tester().assertThat(pickTableLayout.pickTableLayoutForPredicate()) .on(p -> p.filter(expression("nationkey = BIGINT '44'"), p.tableScan( nationTableHandle, ImmutableList.of(p.symbol("nationkey", BIGINT)), ImmutableMap.of(p.symbol("nationkey", BIGINT), columnHandle), Optional.of(nationTableLayoutHandle), TupleDomain.none(), TupleDomain.none()))) .matches(values("A")); }
@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 public void ruleAddedNewTableLayoutIfTableScanHasEmptyConstraint() { tester().assertThat(pickTableLayout.pickTableLayoutForPredicate()) .on(p -> p.filter(expression("orderstatus = 'F'"), p.tableScan( ordersTableHandle, ImmutableList.of(p.symbol("orderstatus", createVarcharType(1))), ImmutableMap.of(p.symbol("orderstatus", createVarcharType(1)), new TpchColumnHandle("orderstatus", createVarcharType(1))), Optional.of(ordersTableLayoutHandle)))) .matches( constrainedTableScanWithTableLayout( "orders", ImmutableMap.of("orderstatus", singleValue(createVarcharType(1), utf8Slice("F"))), ImmutableMap.of("orderstatus", "orderstatus"))); }
@Test public void testPushDownBothArguments() { assertRuleApplication() .on(p -> p.filter(PlanBuilder.expression("ST_Contains(ST_GeometryFromText(wkt), ST_Point(lng, lat))"), p.join(INNER, p.values(p.symbol("wkt", VARCHAR)), p.values(p.symbol("lat"), p.symbol("lng"))))) .matches( spatialJoin("ST_Contains(st_geometryfromtext, st_point)", project(ImmutableMap.of("st_geometryfromtext", expression("ST_GeometryFromText(wkt)")), values(ImmutableMap.of("wkt", 0))), project(ImmutableMap.of("st_point", expression("ST_Point(lng, lat)")), values(ImmutableMap.of("lat", 0, "lng", 1))))); }
@Test public void testPushDownOppositeOrder() { assertRuleApplication() .on(p -> p.filter(PlanBuilder.expression("ST_Contains(ST_GeometryFromText(wkt), ST_Point(lng, lat))"), p.join(INNER, p.values(p.symbol("lat"), p.symbol("lng")), p.values(p.symbol("wkt", VARCHAR))))) .matches( spatialJoin("ST_Contains(st_geometryfromtext, st_point)", project(ImmutableMap.of("st_point", expression("ST_Point(lng, lat)")), values(ImmutableMap.of("lat", 0, "lng", 1))), project(ImmutableMap.of("st_geometryfromtext", expression("ST_GeometryFromText(wkt)")), values(ImmutableMap.of("wkt", 0))))); }
@Test public void testFilterExpressionRewrite() { tester().assertThat(zeroRewriter.filterExpressionRewrite()) .on(p -> p.filter(new LongLiteral("1"), p.values())) .matches( filter("0", values())); }
private void testSimpleDistanceQuery(String filter, String newFilter) { assertRuleApplication() .on(p -> p.filter(PlanBuilder.expression(filter), p.join(INNER, p.values(p.symbol("a", GEOMETRY), p.symbol("name_a")), p.values(p.symbol("b", GEOMETRY), p.symbol("name_b"), p.symbol("r"))))) .matches( spatialJoin(newFilter, values(ImmutableMap.of("a", 0, "name_a", 1)), values(ImmutableMap.of("b", 0, "name_b", 1, "r", 2)))); }
private void testPointExpressionsInDistanceQuery(String filter, String newFilter, String radiusExpression) { assertRuleApplication() .on(p -> p.filter(PlanBuilder.expression(filter), p.join(INNER, p.values(p.symbol("lat_a"), p.symbol("lng_a"), p.symbol("name_a")), p.values(p.symbol("lat_b"), p.symbol("lng_b"), p.symbol("name_b"))))) .matches( spatialJoin(newFilter, project(ImmutableMap.of("point_a", expression("ST_Point(lng_a, lat_a)")), values(ImmutableMap.of("lat_a", 0, "lng_a", 1, "name_a", 2))), project(ImmutableMap.of("point_b", expression("ST_Point(lng_b, lat_b)")), project(ImmutableMap.of("radius", expression(radiusExpression)), values(ImmutableMap.of("lat_b", 0, "lng_b", 1, "name_b", 2)))))); }
private void testRadiusExpressionInDistanceQuery(String filter, String newFilter, String radiusExpression) { assertRuleApplication() .on(p -> p.filter(PlanBuilder.expression(filter), p.join(INNER, p.values(p.symbol("a", GEOMETRY), p.symbol("name_a")), p.values(p.symbol("b", GEOMETRY), p.symbol("name_b"), p.symbol("r"))))) .matches( spatialJoin(newFilter, values(ImmutableMap.of("a", 0, "name_a", 1)), project(ImmutableMap.of("radius", expression(radiusExpression)), values(ImmutableMap.of("b", 0, "name_b", 1, "r", 2))))); }