Search in sources :

Example 1 with MergePatternRecognitionNodesWithProject

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();
}
Also used : MergePatternRecognitionNodesWithProject(io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithProject) CURRENT_ROW(io.trino.sql.tree.FrameBound.Type.CURRENT_ROW) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) Test(org.testng.annotations.Test) PlanMatchPattern.patternRecognition(io.trino.sql.planner.assertions.PlanMatchPattern.patternRecognition) PlanMatchPattern.specification(io.trino.sql.planner.assertions.PlanMatchPattern.specification) ALL_SHOW_EMPTY(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.ALL_SHOW_EMPTY) ImmutableList(com.google.common.collect.ImmutableList) ONE(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.ONE) GREATER_THAN(io.trino.sql.tree.ComparisonExpression.Operator.GREATER_THAN) WINDOW(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.WINDOW) FunctionCall(io.trino.sql.tree.FunctionCall) ExpressionMatcher(io.trino.sql.planner.assertions.ExpressionMatcher) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) ResolvedFunction(io.trino.metadata.ResolvedFunction) Assignments(io.trino.sql.planner.plan.Assignments) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) OrderingScheme(io.trino.sql.planner.OrderingScheme) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) DEFAULT_FRAME(io.trino.sql.planner.plan.WindowNode.Frame.DEFAULT_FRAME) PlanMatchPattern.functionCall(io.trino.sql.planner.assertions.PlanMatchPattern.functionCall) PlanMatchPattern.windowFrame(io.trino.sql.planner.assertions.PlanMatchPattern.windowFrame) QualifiedName(io.trino.sql.tree.QualifiedName) ASC_NULLS_LAST(io.trino.spi.connector.SortOrder.ASC_NULLS_LAST) BIGINT(io.trino.spi.type.BigintType.BIGINT) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) MetadataManager.createTestMetadataManager(io.trino.metadata.MetadataManager.createTestMetadataManager) Optional(java.util.Optional) ROWS(io.trino.sql.tree.WindowFrame.Type.ROWS) WindowNode(io.trino.sql.planner.plan.WindowNode) PlanBuilder.expression(io.trino.sql.planner.iterative.rule.test.PlanBuilder.expression) UNBOUNDED_FOLLOWING(io.trino.sql.tree.FrameBound.Type.UNBOUNDED_FOLLOWING) LAST(io.trino.sql.tree.SkipTo.Position.LAST) MergePatternRecognitionNodesWithoutProject(io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithoutProject) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) MergePatternRecognitionNodesWithProject(io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithProject) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Example 2 with MergePatternRecognitionNodesWithProject

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")))));
}
Also used : MergePatternRecognitionNodesWithProject(io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithProject) CURRENT_ROW(io.trino.sql.tree.FrameBound.Type.CURRENT_ROW) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) Test(org.testng.annotations.Test) PlanMatchPattern.patternRecognition(io.trino.sql.planner.assertions.PlanMatchPattern.patternRecognition) PlanMatchPattern.specification(io.trino.sql.planner.assertions.PlanMatchPattern.specification) ALL_SHOW_EMPTY(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.ALL_SHOW_EMPTY) ImmutableList(com.google.common.collect.ImmutableList) ONE(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.ONE) GREATER_THAN(io.trino.sql.tree.ComparisonExpression.Operator.GREATER_THAN) WINDOW(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.WINDOW) FunctionCall(io.trino.sql.tree.FunctionCall) ExpressionMatcher(io.trino.sql.planner.assertions.ExpressionMatcher) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) ResolvedFunction(io.trino.metadata.ResolvedFunction) Assignments(io.trino.sql.planner.plan.Assignments) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) OrderingScheme(io.trino.sql.planner.OrderingScheme) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) DEFAULT_FRAME(io.trino.sql.planner.plan.WindowNode.Frame.DEFAULT_FRAME) PlanMatchPattern.functionCall(io.trino.sql.planner.assertions.PlanMatchPattern.functionCall) PlanMatchPattern.windowFrame(io.trino.sql.planner.assertions.PlanMatchPattern.windowFrame) QualifiedName(io.trino.sql.tree.QualifiedName) ASC_NULLS_LAST(io.trino.spi.connector.SortOrder.ASC_NULLS_LAST) BIGINT(io.trino.spi.type.BigintType.BIGINT) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) MetadataManager.createTestMetadataManager(io.trino.metadata.MetadataManager.createTestMetadataManager) Optional(java.util.Optional) ROWS(io.trino.sql.tree.WindowFrame.Type.ROWS) WindowNode(io.trino.sql.planner.plan.WindowNode) PlanBuilder.expression(io.trino.sql.planner.iterative.rule.test.PlanBuilder.expression) UNBOUNDED_FOLLOWING(io.trino.sql.tree.FrameBound.Type.UNBOUNDED_FOLLOWING) LAST(io.trino.sql.tree.SkipTo.Position.LAST) MergePatternRecognitionNodesWithoutProject(io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithoutProject) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) MergePatternRecognitionNodesWithProject(io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithProject) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Example 3 with MergePatternRecognitionNodesWithProject

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();
}
Also used : MergePatternRecognitionNodesWithProject(io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithProject) CURRENT_ROW(io.trino.sql.tree.FrameBound.Type.CURRENT_ROW) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) Test(org.testng.annotations.Test) PlanMatchPattern.patternRecognition(io.trino.sql.planner.assertions.PlanMatchPattern.patternRecognition) PlanMatchPattern.specification(io.trino.sql.planner.assertions.PlanMatchPattern.specification) ALL_SHOW_EMPTY(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.ALL_SHOW_EMPTY) ImmutableList(com.google.common.collect.ImmutableList) ONE(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.ONE) GREATER_THAN(io.trino.sql.tree.ComparisonExpression.Operator.GREATER_THAN) WINDOW(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.WINDOW) FunctionCall(io.trino.sql.tree.FunctionCall) ExpressionMatcher(io.trino.sql.planner.assertions.ExpressionMatcher) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) ResolvedFunction(io.trino.metadata.ResolvedFunction) Assignments(io.trino.sql.planner.plan.Assignments) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) OrderingScheme(io.trino.sql.planner.OrderingScheme) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) DEFAULT_FRAME(io.trino.sql.planner.plan.WindowNode.Frame.DEFAULT_FRAME) PlanMatchPattern.functionCall(io.trino.sql.planner.assertions.PlanMatchPattern.functionCall) PlanMatchPattern.windowFrame(io.trino.sql.planner.assertions.PlanMatchPattern.windowFrame) QualifiedName(io.trino.sql.tree.QualifiedName) ASC_NULLS_LAST(io.trino.spi.connector.SortOrder.ASC_NULLS_LAST) BIGINT(io.trino.spi.type.BigintType.BIGINT) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) MetadataManager.createTestMetadataManager(io.trino.metadata.MetadataManager.createTestMetadataManager) Optional(java.util.Optional) ROWS(io.trino.sql.tree.WindowFrame.Type.ROWS) WindowNode(io.trino.sql.planner.plan.WindowNode) PlanBuilder.expression(io.trino.sql.planner.iterative.rule.test.PlanBuilder.expression) UNBOUNDED_FOLLOWING(io.trino.sql.tree.FrameBound.Type.UNBOUNDED_FOLLOWING) LAST(io.trino.sql.tree.SkipTo.Position.LAST) MergePatternRecognitionNodesWithoutProject(io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithoutProject) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) MergePatternRecognitionNodesWithoutProject(io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithoutProject) QualifiedName(io.trino.sql.tree.QualifiedName) MergePatternRecognitionNodesWithProject(io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithProject) FunctionCall(io.trino.sql.tree.FunctionCall) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 MetadataManager.createTestMetadataManager (io.trino.metadata.MetadataManager.createTestMetadataManager)3 ResolvedFunction (io.trino.metadata.ResolvedFunction)3 ASC_NULLS_LAST (io.trino.spi.connector.SortOrder.ASC_NULLS_LAST)3 BIGINT (io.trino.spi.type.BigintType.BIGINT)3 TypeSignatureProvider.fromTypes (io.trino.sql.analyzer.TypeSignatureProvider.fromTypes)3 OrderingScheme (io.trino.sql.planner.OrderingScheme)3 ExpressionMatcher (io.trino.sql.planner.assertions.ExpressionMatcher)3 PlanMatchPattern (io.trino.sql.planner.assertions.PlanMatchPattern)3 PlanMatchPattern.functionCall (io.trino.sql.planner.assertions.PlanMatchPattern.functionCall)3 PlanMatchPattern.patternRecognition (io.trino.sql.planner.assertions.PlanMatchPattern.patternRecognition)3 PlanMatchPattern.project (io.trino.sql.planner.assertions.PlanMatchPattern.project)3 PlanMatchPattern.specification (io.trino.sql.planner.assertions.PlanMatchPattern.specification)3 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)3 PlanMatchPattern.windowFrame (io.trino.sql.planner.assertions.PlanMatchPattern.windowFrame)3 MergePatternRecognitionNodesWithProject (io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithProject)3 MergePatternRecognitionNodesWithoutProject (io.trino.sql.planner.iterative.rule.MergePatternRecognitionNodes.MergePatternRecognitionNodesWithoutProject)3 BaseRuleTest (io.trino.sql.planner.iterative.rule.test.BaseRuleTest)3