use of io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithProject in project trino by trinodb.
the class TestMergePatternRecognitionNodes method testParentDependsOnSourceCreatedOutputsWithProject.
@Test
public void testParentDependsOnSourceCreatedOutputsWithProject() {
// identity projection over created symbol
tester().assertThat(new MergePatternRecognitionNodesWithProject()).on(p -> p.patternRecognition(parentBuilder -> parentBuilder.addMeasure(p.symbol("dependent"), "LAST(X.measure)", BIGINT).rowsPerMatch(ALL_SHOW_EMPTY).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.project(Assignments.identity(p.symbol("measure")), p.patternRecognition(childBuilder -> childBuilder.addMeasure(p.symbol("measure"), "MATCH_NUMBER()", BIGINT).rowsPerMatch(ALL_SHOW_EMPTY).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.values(p.symbol("a")))))))).doesNotFire();
// renaming projection over created symbol
tester().assertThat(new MergePatternRecognitionNodesWithProject()).on(p -> p.patternRecognition(parentBuilder -> parentBuilder.addMeasure(p.symbol("dependent"), "LAST(X.renamed)", BIGINT).rowsPerMatch(ALL_SHOW_EMPTY).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.project(Assignments.of(p.symbol("renamed"), expression("measure")), p.patternRecognition(childBuilder -> childBuilder.addMeasure(p.symbol("measure"), "MATCH_NUMBER()", BIGINT).rowsPerMatch(ALL_SHOW_EMPTY).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.values(p.symbol("a")))))))).doesNotFire();
// complex projection over created symbol
tester().assertThat(new MergePatternRecognitionNodesWithProject()).on(p -> p.patternRecognition(parentBuilder -> parentBuilder.addMeasure(p.symbol("dependent"), "LAST(X.projected)", BIGINT).rowsPerMatch(ALL_SHOW_EMPTY).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.project(Assignments.of(p.symbol("projected"), expression("a * measure")), p.patternRecognition(childBuilder -> childBuilder.addMeasure(p.symbol("measure"), "MATCH_NUMBER()", BIGINT).rowsPerMatch(ALL_SHOW_EMPTY).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.values(p.symbol("a")))))))).doesNotFire();
}
use of io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithProject in project trino by trinodb.
the class TestMergePatternRecognitionNodes method testMergeWithProject.
@Test
public void testMergeWithProject() {
// project is based on pass-through symbols only
// it does not produce symbols necessary for parent node, so it is moved on top of merged node
tester().assertThat(new MergePatternRecognitionNodesWithProject()).on(p -> p.patternRecognition(parentBuilder -> parentBuilder.addMeasure(p.symbol("parent_measure"), "LAST(X.a)", BIGINT).rowsPerMatch(ALL_SHOW_EMPTY).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.project(Assignments.of(p.symbol("a"), expression("a"), p.symbol("expression"), expression("a * b")), p.patternRecognition(childBuilder -> childBuilder.addMeasure(p.symbol("child_measure"), "FIRST(X.b)", BIGINT).rowsPerMatch(ALL_SHOW_EMPTY).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.values(p.symbol("a"), p.symbol("b")))))))).matches(project(ImmutableMap.of("a", PlanMatchPattern.expression("a"), "parent_measure", PlanMatchPattern.expression("parent_measure"), "expression", PlanMatchPattern.expression("expression")), project(ImmutableMap.of("a", PlanMatchPattern.expression("a"), "b", PlanMatchPattern.expression("b"), "parent_measure", PlanMatchPattern.expression("parent_measure"), "child_measure", PlanMatchPattern.expression("child_measure"), "expression", PlanMatchPattern.expression("a * b")), patternRecognition(builder -> builder.addMeasure("parent_measure", "LAST(X.a)", BIGINT).addMeasure("child_measure", "FIRST(X.b)", BIGINT).rowsPerMatch(ALL_SHOW_EMPTY).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true"), values("a", "b")))));
// project is based on symbols created by the child node
// it does not produce symbols necessary for parent node, so it is moved on top of merged node
tester().assertThat(new MergePatternRecognitionNodesWithProject()).on(p -> p.patternRecognition(parentBuilder -> parentBuilder.addMeasure(p.symbol("parent_measure"), "LAST(X.a)", BIGINT).rowsPerMatch(ALL_SHOW_EMPTY).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.project(Assignments.of(p.symbol("a"), expression("a"), p.symbol("expression"), expression("a * b * child_measure")), p.patternRecognition(childBuilder -> childBuilder.addMeasure(p.symbol("child_measure"), "FIRST(X.b)", BIGINT).rowsPerMatch(ALL_SHOW_EMPTY).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.values(p.symbol("a"), p.symbol("b")))))))).matches(project(ImmutableMap.of("a", PlanMatchPattern.expression("a"), "parent_measure", PlanMatchPattern.expression("parent_measure"), "expression", PlanMatchPattern.expression("expression")), project(ImmutableMap.of("a", PlanMatchPattern.expression("a"), "b", PlanMatchPattern.expression("b"), "parent_measure", PlanMatchPattern.expression("parent_measure"), "child_measure", PlanMatchPattern.expression("child_measure"), "expression", PlanMatchPattern.expression("a * b * child_measure")), patternRecognition(builder -> builder.addMeasure("parent_measure", "LAST(X.a)", BIGINT).addMeasure("child_measure", "FIRST(X.b)", BIGINT).rowsPerMatch(ALL_SHOW_EMPTY).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true"), values("a", "b")))));
}
use of io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithProject in project trino by trinodb.
the class TestMergePatternRecognitionNodes method testSpecificationsDoNotMatch.
@Test
public void testSpecificationsDoNotMatch() {
tester().assertThat(new MergePatternRecognitionNodesWithoutProject()).on(p -> p.patternRecognition(parentBuilder -> parentBuilder.pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.patternRecognition(childBuilder -> childBuilder.pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "false").source(p.values(p.symbol("a"))))))).doesNotFire();
tester().assertThat(new MergePatternRecognitionNodesWithProject()).on(p -> p.patternRecognition(parentBuilder -> parentBuilder.pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.project(Assignments.identity(p.symbol("a")), p.patternRecognition(childBuilder -> childBuilder.pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "false").source(p.values(p.symbol("a")))))))).doesNotFire();
// aggregations in variable definitions do not match
QualifiedName count = tester().getMetadata().resolveFunction(tester().getSession(), QualifiedName.of("count"), fromTypes(BIGINT)).toQualifiedName();
tester().assertThat(new MergePatternRecognitionNodesWithoutProject()).on(p -> p.patternRecognition(parentBuilder -> parentBuilder.pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), new ComparisonExpression(GREATER_THAN, new FunctionCall(count, ImmutableList.of(expression("a"))), expression("5"))).source(p.patternRecognition(childBuilder -> childBuilder.pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), new ComparisonExpression(GREATER_THAN, new FunctionCall(count, ImmutableList.of(expression("b"))), expression("5"))).source(p.values(p.symbol("a"), p.symbol("b"))))))).doesNotFire();
}
Aggregations