Search in sources :

Example 11 with IrLabel

use of io.trino.sql.planner.rowpattern.ir.IrLabel in project trino by trinodb.

the class TestPatternRecognitionNodeSerialization method testExpressionAndValuePointersRoundtrip.

@Test
public void testExpressionAndValuePointersRoundtrip() {
    ObjectMapperProvider provider = new ObjectMapperProvider();
    provider.setJsonSerializers(ImmutableMap.of(Expression.class, new ExpressionSerialization.ExpressionSerializer()));
    provider.setJsonDeserializers(ImmutableMap.of(Expression.class, new ExpressionSerialization.ExpressionDeserializer(new SqlParser())));
    JsonCodec<ExpressionAndValuePointers> codec = new JsonCodecFactory(provider).jsonCodec(ExpressionAndValuePointers.class);
    assertJsonRoundTrip(codec, new ExpressionAndValuePointers(new NullLiteral(), ImmutableList.of(), ImmutableList.of(), ImmutableSet.of(), ImmutableSet.of()));
    assertJsonRoundTrip(codec, new ExpressionAndValuePointers(new IfExpression(new ComparisonExpression(GREATER_THAN, new SymbolReference("classifier"), new SymbolReference("x")), new FunctionCall(QualifiedName.of("rand"), ImmutableList.of()), new ArithmeticUnaryExpression(MINUS, new SymbolReference("match_number"))), ImmutableList.of(new Symbol("classifier"), new Symbol("x"), new Symbol("match_number")), ImmutableList.of(new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(new IrLabel("A"), new IrLabel("B")), false, true, 1, -1), new Symbol("input_symbol_a")), new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(new IrLabel("B")), true, false, 2, 1), new Symbol("input_symbol_a")), new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(), true, true, 0, 0), new Symbol("input_symbol_a"))), ImmutableSet.of(new Symbol("classifier")), ImmutableSet.of(new Symbol("match_number"))));
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) IfExpression(io.trino.sql.tree.IfExpression) ScalarValuePointer(io.trino.sql.planner.rowpattern.ScalarValuePointer) SymbolReference(io.trino.sql.tree.SymbolReference) Symbol(io.trino.sql.planner.Symbol) SqlParser(io.trino.sql.parser.SqlParser) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) LogicalIndexPointer(io.trino.sql.planner.rowpattern.LogicalIndexPointer) FunctionCall(io.trino.sql.tree.FunctionCall) JsonCodecFactory(io.airlift.json.JsonCodecFactory) NullLiteral(io.trino.sql.tree.NullLiteral) Test(org.testng.annotations.Test)

Example 12 with IrLabel

use of io.trino.sql.planner.rowpattern.ir.IrLabel in project trino by trinodb.

the class TestPatternRecognitionNodeSerialization method testMeasureRoundtrip.

@Test
public void testMeasureRoundtrip() {
    ObjectMapperProvider provider = new ObjectMapperProvider();
    provider.setJsonSerializers(ImmutableMap.of(Expression.class, new ExpressionSerialization.ExpressionSerializer()));
    provider.setJsonDeserializers(ImmutableMap.of(Expression.class, new ExpressionSerialization.ExpressionDeserializer(new SqlParser()), Type.class, new TypeDeserializer(TESTING_TYPE_MANAGER)));
    JsonCodec<Measure> codec = new JsonCodecFactory(provider).jsonCodec(Measure.class);
    assertJsonRoundTrip(codec, new Measure(new ExpressionAndValuePointers(new NullLiteral(), ImmutableList.of(), ImmutableList.of(), ImmutableSet.of(), ImmutableSet.of()), BOOLEAN));
    assertJsonRoundTrip(codec, new Measure(new ExpressionAndValuePointers(new IfExpression(new ComparisonExpression(GREATER_THAN, new SymbolReference("match_number"), new SymbolReference("x")), new GenericLiteral("BIGINT", "10"), new ArithmeticUnaryExpression(MINUS, new SymbolReference("y"))), ImmutableList.of(new Symbol("match_number"), new Symbol("x"), new Symbol("y")), ImmutableList.of(new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(), true, true, 0, 0), new Symbol("input_symbol_a")), new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(new IrLabel("A")), false, true, 1, -1), new Symbol("input_symbol_a")), new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(new IrLabel("B")), false, true, 1, -1), new Symbol("input_symbol_b"))), ImmutableSet.of(), ImmutableSet.of(new Symbol("match_number"))), BIGINT));
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) IfExpression(io.trino.sql.tree.IfExpression) ScalarValuePointer(io.trino.sql.planner.rowpattern.ScalarValuePointer) SymbolReference(io.trino.sql.tree.SymbolReference) Symbol(io.trino.sql.planner.Symbol) SqlParser(io.trino.sql.parser.SqlParser) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) GenericLiteral(io.trino.sql.tree.GenericLiteral) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) Type(io.trino.spi.type.Type) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) LogicalIndexPointer(io.trino.sql.planner.rowpattern.LogicalIndexPointer) TypeDeserializer(io.trino.type.TypeDeserializer) JsonCodecFactory(io.airlift.json.JsonCodecFactory) NullLiteral(io.trino.sql.tree.NullLiteral) Test(org.testng.annotations.Test)

Example 13 with IrLabel

use of io.trino.sql.planner.rowpattern.ir.IrLabel in project trino by trinodb.

the class TestPatternRecognitionNodeSerialization method testScalarValuePointerRoundtrip.

@Test
public void testScalarValuePointerRoundtrip() {
    JsonCodec<ValuePointer> codec = new JsonCodecFactory(new ObjectMapperProvider()).jsonCodec(ValuePointer.class);
    assertJsonRoundTrip(codec, new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(), false, false, 5, 5), new Symbol("input_symbol")));
    assertJsonRoundTrip(codec, new ScalarValuePointer(new LogicalIndexPointer(ImmutableSet.of(new IrLabel("A"), new IrLabel("B")), true, true, 1, -1), new Symbol("input_symbol")));
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) ScalarValuePointer(io.trino.sql.planner.rowpattern.ScalarValuePointer) Symbol(io.trino.sql.planner.Symbol) AggregationValuePointer(io.trino.sql.planner.rowpattern.AggregationValuePointer) ScalarValuePointer(io.trino.sql.planner.rowpattern.ScalarValuePointer) ValuePointer(io.trino.sql.planner.rowpattern.ValuePointer) LogicalIndexPointer(io.trino.sql.planner.rowpattern.LogicalIndexPointer) JsonCodecFactory(io.airlift.json.JsonCodecFactory) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) Test(org.testng.annotations.Test)

Example 14 with IrLabel

use of io.trino.sql.planner.rowpattern.ir.IrLabel in project trino by trinodb.

the class TestPatternRecognitionNodeSerialization method testPatternRecognitionNodeRoundtrip.

@Test
public void testPatternRecognitionNodeRoundtrip() {
    ObjectMapperProvider provider = new ObjectMapperProvider();
    provider.setJsonSerializers(ImmutableMap.of(Expression.class, new ExpressionSerialization.ExpressionSerializer()));
    provider.setJsonDeserializers(ImmutableMap.of(Expression.class, new ExpressionSerialization.ExpressionDeserializer(new SqlParser()), Type.class, new TypeDeserializer(TESTING_TYPE_MANAGER)));
    provider.setKeyDeserializers(ImmutableMap.of(TypeSignature.class, new TypeSignatureKeyDeserializer()));
    JsonCodec<PatternRecognitionNode> codec = new JsonCodecFactory(provider).jsonCodec(PatternRecognitionNode.class);
    ResolvedFunction rankFunction = createTestMetadataManager().resolveFunction(TEST_SESSION, QualifiedName.of("rank"), ImmutableList.of());
    // test remaining fields inside PatternRecognitionNode specific to pattern recognition:
    // windowFunctions, measures, commonBaseFrame, rowsPerMatch, skipToLabel, skipToPosition, initial, pattern, subsets, variableDefinitions
    PatternRecognitionNode node = new PatternRecognitionNode(new PlanNodeId("0"), new ValuesNode(new PlanNodeId("1"), 1), new Specification(ImmutableList.of(), Optional.empty()), Optional.empty(), ImmutableSet.of(), 0, ImmutableMap.of(new Symbol("rank"), new Function(rankFunction, ImmutableList.of(), new Frame(ROWS, CURRENT_ROW, Optional.empty(), Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), false)), ImmutableMap.of(new Symbol("measure"), new Measure(new ExpressionAndValuePointers(new NullLiteral(), ImmutableList.of(), ImmutableList.of(), ImmutableSet.of(), ImmutableSet.of()), BOOLEAN)), Optional.of(new Frame(ROWS, CURRENT_ROW, Optional.empty(), Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())), WINDOW, Optional.of(new IrLabel("B")), LAST, true, new IrConcatenation(ImmutableList.of(new IrLabel("A"), new IrLabel("B"), new IrLabel("C"))), ImmutableMap.of(new IrLabel("U"), ImmutableSet.of(new IrLabel("A"), new IrLabel("B")), new IrLabel("V"), ImmutableSet.of(new IrLabel("B"), new IrLabel("C"))), ImmutableMap.of(new IrLabel("B"), new ExpressionAndValuePointers(new NullLiteral(), ImmutableList.of(), ImmutableList.of(), ImmutableSet.of(), ImmutableSet.of()), new IrLabel("C"), new ExpressionAndValuePointers(new NullLiteral(), ImmutableList.of(), ImmutableList.of(), ImmutableSet.of(), ImmutableSet.of())));
    PatternRecognitionNode roundtripNode = codec.fromJson(codec.toJson(node));
    assertEquals(roundtripNode.getMeasures(), node.getMeasures());
    assertEquals(roundtripNode.getRowsPerMatch(), node.getRowsPerMatch());
    assertEquals(roundtripNode.getSkipToLabel(), node.getSkipToLabel());
    assertEquals(roundtripNode.getSkipToPosition(), node.getSkipToPosition());
    assertEquals(roundtripNode.isInitial(), node.isInitial());
    assertEquals(roundtripNode.getPattern(), node.getPattern());
    assertEquals(roundtripNode.getSubsets(), node.getSubsets());
    assertEquals(roundtripNode.getVariableDefinitions(), node.getVariableDefinitions());
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) Frame(io.trino.sql.planner.plan.WindowNode.Frame) ResolvedFunction(io.trino.metadata.ResolvedFunction) Symbol(io.trino.sql.planner.Symbol) SqlParser(io.trino.sql.parser.SqlParser) Specification(io.trino.sql.planner.plan.WindowNode.Specification) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) IrConcatenation(io.trino.sql.planner.rowpattern.ir.IrConcatenation) ResolvedFunction(io.trino.metadata.ResolvedFunction) Function(io.trino.sql.planner.plan.WindowNode.Function) Type(io.trino.spi.type.Type) TypeSignature(io.trino.spi.type.TypeSignature) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure) TypeDeserializer(io.trino.type.TypeDeserializer) JsonCodecFactory(io.airlift.json.JsonCodecFactory) NullLiteral(io.trino.sql.tree.NullLiteral) TypeSignatureKeyDeserializer(io.trino.type.TypeSignatureKeyDeserializer) Test(org.testng.annotations.Test)

Example 15 with IrLabel

use of io.trino.sql.planner.rowpattern.ir.IrLabel 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)

Aggregations

IrLabel (io.trino.sql.planner.rowpattern.ir.IrLabel)22 Test (org.testng.annotations.Test)16 ImmutableMap (com.google.common.collect.ImmutableMap)15 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)15 ImmutableList (com.google.common.collect.ImmutableList)13 FunctionCall (io.trino.sql.tree.FunctionCall)13 ResolvedFunction (io.trino.metadata.ResolvedFunction)12 BIGINT (io.trino.spi.type.BigintType.BIGINT)12 WindowNode (io.trino.sql.planner.plan.WindowNode)12 Optional (java.util.Optional)12 MetadataManager.createTestMetadataManager (io.trino.metadata.MetadataManager.createTestMetadataManager)11 ASC_NULLS_LAST (io.trino.spi.connector.SortOrder.ASC_NULLS_LAST)11 PlanMatchPattern.functionCall (io.trino.sql.planner.assertions.PlanMatchPattern.functionCall)11 PlanMatchPattern.patternRecognition (io.trino.sql.planner.assertions.PlanMatchPattern.patternRecognition)11 PlanMatchPattern.specification (io.trino.sql.planner.assertions.PlanMatchPattern.specification)11 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)11 PlanMatchPattern.windowFrame (io.trino.sql.planner.assertions.PlanMatchPattern.windowFrame)11 Assignments (io.trino.sql.planner.plan.Assignments)11 CURRENT_ROW (io.trino.sql.tree.FrameBound.Type.CURRENT_ROW)11 UNBOUNDED_FOLLOWING (io.trino.sql.tree.FrameBound.Type.UNBOUNDED_FOLLOWING)11