use of io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.ALL_SHOW_EMPTY 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.tree.PatternRecognitionRelation.RowsPerMatch.ALL_SHOW_EMPTY 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.tree.PatternRecognitionRelation.RowsPerMatch.ALL_SHOW_EMPTY in project trino by trinodb.
the class TestMergePatternRecognitionNodes method testParentDependsOnSourceCreatedOutputs.
@Test
public void testParentDependsOnSourceCreatedOutputs() {
ResolvedFunction lag = createTestMetadataManager().resolveFunction(tester().getSession(), QualifiedName.of("lag"), fromTypes(BIGINT));
// parent node's measure depends on child node's measure output
tester().assertThat(new MergePatternRecognitionNodesWithoutProject()).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.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();
// parent node's measure depends on child node's window function output
tester().assertThat(new MergePatternRecognitionNodesWithoutProject()).on(p -> p.patternRecognition(parentBuilder -> parentBuilder.addMeasure(p.symbol("dependent"), "LAST(X.function)", BIGINT).rowsPerMatch(WINDOW).frame(new WindowNode.Frame(ROWS, CURRENT_ROW, Optional.empty(), Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.patternRecognition(childBuilder -> childBuilder.addWindowFunction(p.symbol("function"), new WindowNode.Function(lag, ImmutableList.of(p.symbol("a").toSymbolReference()), DEFAULT_FRAME, false)).rowsPerMatch(WINDOW).frame(new WindowNode.Frame(ROWS, CURRENT_ROW, Optional.empty(), Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.values(p.symbol("a"))))))).doesNotFire();
// parent node's window function depends on child node's window function output
tester().assertThat(new MergePatternRecognitionNodesWithoutProject()).on(p -> p.patternRecognition(parentBuilder -> parentBuilder.addWindowFunction(p.symbol("dependent"), new WindowNode.Function(lag, ImmutableList.of(p.symbol("function").toSymbolReference()), DEFAULT_FRAME, false)).rowsPerMatch(WINDOW).frame(new WindowNode.Frame(ROWS, CURRENT_ROW, Optional.empty(), Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.patternRecognition(childBuilder -> childBuilder.addWindowFunction(p.symbol("function"), new WindowNode.Function(lag, ImmutableList.of(p.symbol("a").toSymbolReference()), DEFAULT_FRAME, false)).rowsPerMatch(WINDOW).frame(new WindowNode.Frame(ROWS, CURRENT_ROW, Optional.empty(), Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.values(p.symbol("a"))))))).doesNotFire();
// parent node's window function depends on child node's measure output
tester().assertThat(new MergePatternRecognitionNodesWithoutProject()).on(p -> p.patternRecognition(parentBuilder -> parentBuilder.addWindowFunction(p.symbol("dependent"), new WindowNode.Function(lag, ImmutableList.of(p.symbol("measure").toSymbolReference()), DEFAULT_FRAME, false)).rowsPerMatch(WINDOW).frame(new WindowNode.Frame(ROWS, CURRENT_ROW, Optional.empty(), Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.patternRecognition(childBuilder -> childBuilder.addMeasure(p.symbol("measure"), "MATCH_NUMBER()", BIGINT).rowsPerMatch(WINDOW).frame(new WindowNode.Frame(ROWS, CURRENT_ROW, Optional.empty(), Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "true").source(p.values(p.symbol("a"))))))).doesNotFire();
}
Aggregations