Search in sources :

Example 16 with PlanMatchPattern

use of io.trino.sql.planner.assertions.PlanMatchPattern in project trino by trinodb.

the class TestReorderJoins method testReplicatedScalarJoinEvenWhereSessionRequiresRepartitioned.

@Test
public void testReplicatedScalarJoinEvenWhereSessionRequiresRepartitioned() {
    PlanMatchPattern expectedPlan = join(INNER, ImmutableList.of(equiJoinClause("A1", "B1")), Optional.empty(), Optional.of(REPLICATED), values(ImmutableMap.of("A1", 0)), values(ImmutableMap.of("B1", 0)));
    PlanNodeStatsEstimate valuesA = PlanNodeStatsEstimate.builder().setOutputRowCount(10000).addSymbolStatistics(ImmutableMap.of(new Symbol("A1"), new SymbolStatsEstimate(0, 100, 0, 640000, 100))).build();
    PlanNodeStatsEstimate valuesB = PlanNodeStatsEstimate.builder().setOutputRowCount(10000).addSymbolStatistics(ImmutableMap.of(new Symbol("B1"), new SymbolStatsEstimate(0, 100, 0, 640000, 100))).build();
    assertReorderJoins().setSystemProperty(JOIN_DISTRIBUTION_TYPE, JoinDistributionType.PARTITIONED.name()).on(p -> p.join(INNER, // matches isAtMostScalar
    p.values(new PlanNodeId("valuesA"), p.symbol("A1")), p.values(new PlanNodeId("valuesB"), 2, p.symbol("B1")), ImmutableList.of(new EquiJoinClause(p.symbol("A1"), p.symbol("B1"))), ImmutableList.of(p.symbol("A1")), ImmutableList.of(p.symbol("B1")), Optional.empty())).overrideStats("valuesA", valuesA).overrideStats("valuesB", valuesB).matches(expectedPlan);
    assertReorderJoins().setSystemProperty(JOIN_DISTRIBUTION_TYPE, JoinDistributionType.PARTITIONED.name()).on(p -> p.join(INNER, p.values(new PlanNodeId("valuesB"), 2, p.symbol("B1")), // matches isAtMostScalar
    p.values(new PlanNodeId("valuesA"), p.symbol("A1")), ImmutableList.of(new EquiJoinClause(p.symbol("B1"), p.symbol("A1"))), ImmutableList.of(p.symbol("B1")), ImmutableList.of(p.symbol("A1")), Optional.empty())).overrideStats("valuesA", valuesA).overrideStats("valuesB", valuesB).matches(expectedPlan);
}
Also used : TypeAnalyzer.createTestingTypeAnalyzer(io.trino.sql.planner.TypeAnalyzer.createTestingTypeAnalyzer) PARTITIONED(io.trino.sql.planner.plan.JoinNode.DistributionType.PARTITIONED) INNER(io.trino.sql.planner.plan.JoinNode.Type.INNER) TestingFunctionResolution(io.trino.metadata.TestingFunctionResolution) Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) Test(org.testng.annotations.Test) REPLICATED(io.trino.sql.planner.plan.JoinNode.DistributionType.REPLICATED) BROADCAST(io.trino.sql.planner.OptimizerConfig.JoinDistributionType.BROADCAST) RuleAssert(io.trino.sql.planner.iterative.rule.test.RuleAssert) ImmutableList(com.google.common.collect.ImmutableList) EquiJoinClause(io.trino.sql.planner.plan.JoinNode.EquiJoinClause) PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) JoinReorderingStrategy(io.trino.sql.planner.OptimizerConfig.JoinReorderingStrategy) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) PlanMatchPattern.equiJoinClause(io.trino.sql.planner.assertions.PlanMatchPattern.equiJoinClause) AUTOMATIC(io.trino.sql.planner.OptimizerConfig.JoinDistributionType.AUTOMATIC) JOIN_REORDERING_STRATEGY(io.trino.SystemSessionProperties.JOIN_REORDERING_STRATEGY) PlanMatchPattern.join(io.trino.sql.planner.assertions.PlanMatchPattern.join) PlanNodeStatsEstimate(io.trino.cost.PlanNodeStatsEstimate) JOIN_MAX_BROADCAST_TABLE_SIZE(io.trino.SystemSessionProperties.JOIN_MAX_BROADCAST_TABLE_SIZE) Symbol(io.trino.sql.planner.Symbol) AfterClass(org.testng.annotations.AfterClass) PlanMatchPattern.expression(io.trino.sql.planner.assertions.PlanMatchPattern.expression) SymbolStatsEstimate(io.trino.cost.SymbolStatsEstimate) RuleTester.defaultRuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester) ImmutableMap(com.google.common.collect.ImmutableMap) BeforeClass(org.testng.annotations.BeforeClass) Assignments(io.trino.sql.planner.plan.Assignments) RuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) LESS_THAN(io.trino.sql.tree.ComparisonExpression.Operator.LESS_THAN) JoinDistributionType(io.trino.sql.planner.OptimizerConfig.JoinDistributionType) CostComparator(io.trino.cost.CostComparator) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) QualifiedName(io.trino.sql.tree.QualifiedName) EQUAL(io.trino.sql.tree.ComparisonExpression.Operator.EQUAL) Closeables.closeAllRuntimeException(io.airlift.testing.Closeables.closeAllRuntimeException) PlanMatchPattern.strictProject(io.trino.sql.planner.assertions.PlanMatchPattern.strictProject) PLANNER_CONTEXT(io.trino.sql.planner.TestingPlannerContext.PLANNER_CONTEXT) JOIN_DISTRIBUTION_TYPE(io.trino.SystemSessionProperties.JOIN_DISTRIBUTION_TYPE) Optional(java.util.Optional) MINUS(io.trino.sql.tree.ArithmeticUnaryExpression.Sign.MINUS) PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) PlanNodeStatsEstimate(io.trino.cost.PlanNodeStatsEstimate) Symbol(io.trino.sql.planner.Symbol) EquiJoinClause(io.trino.sql.planner.plan.JoinNode.EquiJoinClause) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) SymbolStatsEstimate(io.trino.cost.SymbolStatsEstimate) Test(org.testng.annotations.Test)

Example 17 with PlanMatchPattern

use of io.trino.sql.planner.assertions.PlanMatchPattern in project trino by trinodb.

the class TestMergeWindows method testMergeableWindowsAllOptimizers.

/**
 * There are two types of tests in here, and they answer two different
 * questions about MergeWindows (MW):
 * <p>
 * 1) Is MW working as it's supposed to be? The tests running the minimal
 * set of optimizers can tell us this.
 * 2) Has some other optimizer changed the plan in such a way that MW no
 * longer merges windows with identical specifications because the plan
 * that MW sees cannot be optimized by MW? The test running the full set
 * of optimizers answers this, though it isn't actually meaningful unless
 * we know the answer to question 1 is "yes".
 * <p>
 * The tests that use only the minimal set of optimizers are closer to true
 * "unit" tests in that they verify the behavior of MW with as few
 * external dependencies as possible. Those dependencies to include the
 * parser and analyzer, so the phrase "unit" tests should be taken with a
 * grain of salt. Using the parser and anayzler instead of creating plan
 * nodes by hand does have a couple of advantages over a true unit test:
 * 1) The tests are more self-maintaining.
 * 2) They're a lot easier to read.
 * 3) It's a lot less typing.
 * <p>
 * The test that runs with all of the optimzers acts as an integration test
 * and ensures that MW is effective when run with the complete set of
 * optimizers.
 */
@Test
public void testMergeableWindowsAllOptimizers() {
    @Language("SQL") String sql = "SELECT " + "SUM(quantity) OVER (PARTITION BY suppkey ORDER BY orderkey ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) sum_quantity_A, " + "SUM(quantity) OVER (PARTITION BY orderkey ORDER BY shipdate ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) sum_quantity_B, " + "SUM(discount) OVER (PARTITION BY suppkey ORDER BY orderkey ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) sum_discount_A " + "FROM lineitem";
    PlanMatchPattern pattern = anyTree(window(windowMatcherBuilder -> windowMatcherBuilder.specification(specificationA).addFunction(functionCall("sum", COMMON_FRAME, ImmutableList.of(QUANTITY_ALIAS))).addFunction(functionCall("sum", COMMON_FRAME, ImmutableList.of(DISCOUNT_ALIAS))), anyTree(window(windowMatcherBuilder -> windowMatcherBuilder.specification(specificationB).addFunction(functionCall("sum", COMMON_FRAME, ImmutableList.of(QUANTITY_ALIAS))), anyNot(WindowNode.class, // should be anyTree(LINEITEM_TABLESCAN_DOQSS) but anyTree does not handle zero nodes case correctly
    LINEITEM_TABLESCAN_DOQSS)))));
    assertPlan(sql, pattern);
}
Also used : RemoveRedundantIdentityProjections(io.trino.sql.planner.iterative.rule.RemoveRedundantIdentityProjections) PlanMatchPattern.any(io.trino.sql.planner.assertions.PlanMatchPattern.any) PlanMatchPattern.window(io.trino.sql.planner.assertions.PlanMatchPattern.window) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) Test(org.testng.annotations.Test) PlanMatchPattern.filter(io.trino.sql.planner.assertions.PlanMatchPattern.filter) PlanOptimizers.columnPruningRules(io.trino.sql.planner.PlanOptimizers.columnPruningRules) PlanMatchPattern.specification(io.trino.sql.planner.assertions.PlanMatchPattern.specification) RuleStatsRecorder(io.trino.sql.planner.RuleStatsRecorder) ImmutableList(com.google.common.collect.ImmutableList) GatherAndMergeWindows(io.trino.sql.planner.iterative.rule.GatherAndMergeWindows) Map(java.util.Map) Rule(io.trino.sql.planner.iterative.Rule) JoinNode(io.trino.sql.planner.plan.JoinNode) BasePlanTest(io.trino.sql.planner.assertions.BasePlanTest) PlanMatchPattern.join(io.trino.sql.planner.assertions.PlanMatchPattern.join) PlanMatchPattern.expression(io.trino.sql.planner.assertions.PlanMatchPattern.expression) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Language(org.intellij.lang.annotations.Language) SortOrder(io.trino.spi.connector.SortOrder) PlanMatchPattern.functionCall(io.trino.sql.planner.assertions.PlanMatchPattern.functionCall) ExpectedValueProvider(io.trino.sql.planner.assertions.ExpectedValueProvider) List(java.util.List) PlanMatchPattern.anyTree(io.trino.sql.planner.assertions.PlanMatchPattern.anyTree) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) FrameBound(io.trino.sql.tree.FrameBound) WindowFrame(io.trino.sql.tree.WindowFrame) Optional(java.util.Optional) IterativeOptimizer(io.trino.sql.planner.iterative.IterativeOptimizer) WindowNode(io.trino.sql.planner.plan.WindowNode) PlanMatchPattern.tableScan(io.trino.sql.planner.assertions.PlanMatchPattern.tableScan) PlanMatchPattern.anyNot(io.trino.sql.planner.assertions.PlanMatchPattern.anyNot) Language(org.intellij.lang.annotations.Language) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) Test(org.testng.annotations.Test) BasePlanTest(io.trino.sql.planner.assertions.BasePlanTest)

Example 18 with PlanMatchPattern

use of io.trino.sql.planner.assertions.PlanMatchPattern in project trino by trinodb.

the class TestReorderWindows method testNonMergeableABAReordersToAABAllOptimizers.

@Test
public void testNonMergeableABAReordersToAABAllOptimizers() {
    @Language("SQL") String sql = "select " + "sum(quantity) over(PARTITION BY suppkey ORDER BY orderkey ASC NULLS LAST) sum_quantity_A, " + "avg(discount) over(PARTITION BY partkey ORDER BY receiptdate ASC NULLS LAST) avg_discount_B, " + "min(tax) over(PARTITION BY suppkey ORDER BY shipdate ASC NULLS LAST) min_tax_A " + "from lineitem";
    PlanMatchPattern pattern = anyTree(window(windowMatcherBuilder -> windowMatcherBuilder.specification(windowAp).addFunction(functionCall("min", commonFrame, ImmutableList.of(TAX_ALIAS))), project(window(windowMatcherBuilder -> windowMatcherBuilder.specification(windowA).addFunction(functionCall("sum", commonFrame, ImmutableList.of(QUANTITY_ALIAS))), project(window(windowMatcherBuilder -> windowMatcherBuilder.specification(windowB).addFunction(functionCall("avg", commonFrame, ImmutableList.of(DISCOUNT_ALIAS))), anyTree(LINEITEM_TABLESCAN_DOQPRSST)))))));
    assertPlan(sql, pattern);
}
Also used : TypeAnalyzer.createTestingTypeAnalyzer(io.trino.sql.planner.TypeAnalyzer.createTestingTypeAnalyzer) RemoveRedundantIdentityProjections(io.trino.sql.planner.iterative.rule.RemoveRedundantIdentityProjections) PlanMatchPattern.window(io.trino.sql.planner.assertions.PlanMatchPattern.window) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) Test(org.testng.annotations.Test) PlanMatchPattern.filter(io.trino.sql.planner.assertions.PlanMatchPattern.filter) PlanOptimizers.columnPruningRules(io.trino.sql.planner.PlanOptimizers.columnPruningRules) PlanMatchPattern.specification(io.trino.sql.planner.assertions.PlanMatchPattern.specification) RuleStatsRecorder(io.trino.sql.planner.RuleStatsRecorder) ImmutableList(com.google.common.collect.ImmutableList) GatherAndMergeWindows(io.trino.sql.planner.iterative.rule.GatherAndMergeWindows) Rule(io.trino.sql.planner.iterative.Rule) BasePlanTest(io.trino.sql.planner.assertions.BasePlanTest) PlanMatchPattern.expression(io.trino.sql.planner.assertions.PlanMatchPattern.expression) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Language(org.intellij.lang.annotations.Language) SortOrder(io.trino.spi.connector.SortOrder) PlanMatchPattern.functionCall(io.trino.sql.planner.assertions.PlanMatchPattern.functionCall) ExpectedValueProvider(io.trino.sql.planner.assertions.ExpectedValueProvider) List(java.util.List) PlanMatchPattern.anyTree(io.trino.sql.planner.assertions.PlanMatchPattern.anyTree) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) WindowFrame(io.trino.sql.tree.WindowFrame) Optional(java.util.Optional) IterativeOptimizer(io.trino.sql.planner.iterative.IterativeOptimizer) WindowNode(io.trino.sql.planner.plan.WindowNode) PlanMatchPattern.tableScan(io.trino.sql.planner.assertions.PlanMatchPattern.tableScan) Language(org.intellij.lang.annotations.Language) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) Test(org.testng.annotations.Test) BasePlanTest(io.trino.sql.planner.assertions.BasePlanTest)

Example 19 with PlanMatchPattern

use of io.trino.sql.planner.assertions.PlanMatchPattern in project trino by trinodb.

the class TestRuleTester method testReportNoFireWithTableScan.

@Test
public void testReportNoFireWithTableScan() {
    try (RuleTester tester = defaultRuleTester()) {
        RuleAssert ruleAssert = tester.assertThat(rule("testReportNoFireWithTableScan rule", Pattern.typeOf(PlanNode.class), (node, captures, context) -> Result.empty())).on(p -> p.tableScan(new TableHandle(tester.getCurrentConnectorId(), new TpchTableHandle("sf1", "nation", 1.0), TestingTransactionHandle.create()), List.of(p.symbol("x")), Map.of(p.symbol("x"), new TestingColumnHandle("column"))));
        PlanMatchPattern expected = values(List.of("whatever"), List.of());
        assertThatThrownBy(() -> ruleAssert.matches(expected)).isInstanceOf(AssertionError.class).hasMessageMatching("testReportNoFireWithTableScan rule did not fire for:\n" + "(?s:.*)" + "\\QEstimates: {rows: 25 (225B), cpu: 225, memory: 0B, network: 0B}\\E\n" + "(?s:.*)");
    }
}
Also used : TestingColumnHandle(io.trino.spi.connector.TestingColumnHandle) PlanNode(io.trino.sql.planner.plan.PlanNode) RuleTester.defaultRuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) TableHandle(io.trino.metadata.TableHandle) TpchTableHandle(io.trino.plugin.tpch.TpchTableHandle) TpchTableHandle(io.trino.plugin.tpch.TpchTableHandle) Test(org.testng.annotations.Test)

Example 20 with PlanMatchPattern

use of io.trino.sql.planner.assertions.PlanMatchPattern in project trino by trinodb.

the class TestRuleTester method testReportNoFire.

@Test
public void testReportNoFire() {
    try (RuleTester tester = defaultRuleTester()) {
        RuleAssert ruleAssert = tester.assertThat(rule("testReportNoFire rule", Pattern.typeOf(PlanNode.class), (node, captures, context) -> Result.empty())).on(p -> p.values(List.of(p.symbol("x")), List.of(List.of(expression("1")))));
        PlanMatchPattern expected = values(List.of("whatever"), List.of());
        assertThatThrownBy(() -> ruleAssert.matches(expected)).isInstanceOf(AssertionError.class).hasMessageMatching("testReportNoFire rule did not fire for:(?s:.*)");
    }
}
Also used : PlanNode(io.trino.sql.planner.plan.PlanNode) RuleTester.defaultRuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) Test(org.testng.annotations.Test)

Aggregations

PlanMatchPattern (io.trino.sql.planner.assertions.PlanMatchPattern)27 Test (org.testng.annotations.Test)27 ImmutableList (com.google.common.collect.ImmutableList)18 ImmutableMap (com.google.common.collect.ImmutableMap)16 BasePlanTest (io.trino.sql.planner.assertions.BasePlanTest)16 PlanMatchPattern.anyTree (io.trino.sql.planner.assertions.PlanMatchPattern.anyTree)16 Optional (java.util.Optional)15 Language (org.intellij.lang.annotations.Language)15 SortOrder (io.trino.spi.connector.SortOrder)14 PlanMatchPattern.functionCall (io.trino.sql.planner.assertions.PlanMatchPattern.functionCall)14 PlanMatchPattern.expression (io.trino.sql.planner.assertions.PlanMatchPattern.expression)13 PlanMatchPattern.specification (io.trino.sql.planner.assertions.PlanMatchPattern.specification)13 PlanMatchPattern.window (io.trino.sql.planner.assertions.PlanMatchPattern.window)13 PlanMatchPattern.project (io.trino.sql.planner.assertions.PlanMatchPattern.project)12 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)10 CREATED (io.trino.sql.planner.LogicalPlanner.Stage.CREATED)9 QualifiedName (io.trino.sql.tree.QualifiedName)9 List (java.util.List)9 TEST_SESSION (io.trino.SessionTestUtils.TEST_SESSION)8 INTEGER (io.trino.spi.type.IntegerType.INTEGER)8