Search in sources :

Example 21 with ComparisonExpression

use of io.trino.sql.tree.ComparisonExpression in project trino by trinodb.

the class TestPrunePattenRecognitionColumns method testDoNotPruneVariableDefinitionSources.

@Test
public void testDoNotPruneVariableDefinitionSources() {
    // input symbol "a" is used only by the variable definition
    tester().assertThat(new PrunePattenRecognitionColumns()).on(p -> p.project(Assignments.of(), p.patternRecognition(builder -> builder.addMeasure(p.symbol("measure"), "1", BIGINT).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "LAST(X.a) > 0").source(p.values(p.symbol("a"), p.symbol("b")))))).matches(strictProject(ImmutableMap.of(), patternRecognition(builder -> builder.pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), "LAST(X.a) > 0"), strictProject(ImmutableMap.of("a", expression("a")), values("a", "b")))));
    // inputs "a", "b" are used as aggregation arguments
    QualifiedName maxBy = tester().getMetadata().resolveFunction(tester().getSession(), QualifiedName.of("max_by"), fromTypes(BIGINT, BIGINT)).toQualifiedName();
    tester().assertThat(new PrunePattenRecognitionColumns()).on(p -> p.project(Assignments.of(), p.patternRecognition(builder -> builder.addMeasure(p.symbol("measure"), "1", BIGINT).pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), new ComparisonExpression(GREATER_THAN, new FunctionCall(maxBy, ImmutableList.of(PlanBuilder.expression("a"), PlanBuilder.expression("b"))), PlanBuilder.expression("5"))).source(p.values(p.symbol("a"), p.symbol("b"), p.symbol("c")))))).matches(strictProject(ImmutableMap.of(), patternRecognition(builder -> builder.pattern(new IrLabel("X")).addVariableDefinition(new IrLabel("X"), new ComparisonExpression(GREATER_THAN, new FunctionCall(maxBy, ImmutableList.of(PlanBuilder.expression("a"), PlanBuilder.expression("b"))), PlanBuilder.expression("5"))), strictProject(ImmutableMap.of("a", expression("a"), "b", expression("b")), values("a", "b", "c")))));
}
Also used : CURRENT_ROW(io.trino.sql.tree.FrameBound.Type.CURRENT_ROW) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) Test(org.testng.annotations.Test) PlanMatchPattern.patternRecognition(io.trino.sql.planner.assertions.PlanMatchPattern.patternRecognition) FOLLOWING(io.trino.sql.tree.FrameBound.Type.FOLLOWING) PlanMatchPattern.specification(io.trino.sql.planner.assertions.PlanMatchPattern.specification) ALL_WITH_UNMATCHED(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.ALL_WITH_UNMATCHED) ALL_SHOW_EMPTY(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.ALL_SHOW_EMPTY) ImmutableList(com.google.common.collect.ImmutableList) PlanBuilder(io.trino.sql.planner.iterative.rule.test.PlanBuilder) GREATER_THAN(io.trino.sql.tree.ComparisonExpression.Operator.GREATER_THAN) WINDOW(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.WINDOW) NEXT(io.trino.sql.tree.SkipTo.Position.NEXT) FunctionCall(io.trino.sql.tree.FunctionCall) PlanMatchPattern.expression(io.trino.sql.planner.assertions.PlanMatchPattern.expression) 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) PAST_LAST(io.trino.sql.tree.SkipTo.Position.PAST_LAST) 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) PlanMatchPattern.strictProject(io.trino.sql.planner.assertions.PlanMatchPattern.strictProject) BIGINT(io.trino.spi.type.BigintType.BIGINT) 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) UNBOUNDED_FOLLOWING(io.trino.sql.tree.FrameBound.Type.UNBOUNDED_FOLLOWING) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) QualifiedName(io.trino.sql.tree.QualifiedName) FunctionCall(io.trino.sql.tree.FunctionCall) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Example 22 with ComparisonExpression

use of io.trino.sql.tree.ComparisonExpression in project trino by trinodb.

the class TestPruneApplyColumns method testRemoveUnreferencedAssignments.

@Test
public void testRemoveUnreferencedAssignments() {
    // remove assignment and prune unused input symbol
    tester().assertThat(new PruneApplyColumns()).on(p -> {
        Symbol a = p.symbol("a");
        Symbol b = p.symbol("b");
        Symbol correlationSymbol = p.symbol("correlation_symbol");
        Symbol subquerySymbol = p.symbol("subquery_symbol");
        Symbol inResult1 = p.symbol("in_result_1");
        Symbol inResult2 = p.symbol("in_result_2");
        return p.project(Assignments.identity(a, inResult1), p.apply(Assignments.of(inResult1, new InPredicate(a.toSymbolReference(), subquerySymbol.toSymbolReference()), inResult2, new InPredicate(b.toSymbolReference(), subquerySymbol.toSymbolReference())), ImmutableList.of(correlationSymbol), p.values(a, b, correlationSymbol), p.filter(new ComparisonExpression(GREATER_THAN, subquerySymbol.toSymbolReference(), correlationSymbol.toSymbolReference()), p.values(subquerySymbol))));
    }).matches(project(ImmutableMap.of("a", PlanMatchPattern.expression("a"), "in_result_1", PlanMatchPattern.expression("in_result_1")), apply(ImmutableList.of("correlation_symbol"), ImmutableMap.of("in_result_1", ExpressionMatcher.inPredicate(new SymbolReference("a"), new SymbolReference("subquery_symbol"))), project(ImmutableMap.of("a", PlanMatchPattern.expression("a"), "correlation_symbol", PlanMatchPattern.expression("correlation_symbol")), values("a", "b", "correlation_symbol")), node(FilterNode.class, values("subquery_symbol")))));
    // remove assignment and prune unused subquery symbol
    tester().assertThat(new PruneApplyColumns()).on(p -> {
        Symbol a = p.symbol("a");
        Symbol correlationSymbol = p.symbol("correlation_symbol");
        Symbol subquerySymbol1 = p.symbol("subquery_symbol_1");
        Symbol subquerySymbol2 = p.symbol("subquery_symbol_2");
        Symbol inResult1 = p.symbol("in_result_1");
        Symbol inResult2 = p.symbol("in_result_2");
        return p.project(Assignments.identity(a, inResult1), p.apply(Assignments.of(inResult1, new InPredicate(a.toSymbolReference(), subquerySymbol1.toSymbolReference()), inResult2, new InPredicate(a.toSymbolReference(), subquerySymbol2.toSymbolReference())), ImmutableList.of(correlationSymbol), p.values(a, correlationSymbol), p.filter(new ComparisonExpression(GREATER_THAN, subquerySymbol1.toSymbolReference(), correlationSymbol.toSymbolReference()), p.values(subquerySymbol1, subquerySymbol2))));
    }).matches(project(ImmutableMap.of("a", PlanMatchPattern.expression("a"), "in_result_1", PlanMatchPattern.expression("in_result_1")), apply(ImmutableList.of("correlation_symbol"), ImmutableMap.of("in_result_1", ExpressionMatcher.inPredicate(new SymbolReference("a"), new SymbolReference("subquery_symbol_1"))), values("a", "correlation_symbol"), project(ImmutableMap.of("subquery_symbol_1", PlanMatchPattern.expression("subquery_symbol_1")), node(FilterNode.class, values("subquery_symbol_1", "subquery_symbol_2"))))));
}
Also used : ExpressionMatcher(io.trino.sql.planner.assertions.ExpressionMatcher) Symbol(io.trino.sql.planner.Symbol) ImmutableMap(com.google.common.collect.ImmutableMap) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Assignments(io.trino.sql.planner.plan.Assignments) PlanMatchPattern(io.trino.sql.planner.assertions.PlanMatchPattern) Test(org.testng.annotations.Test) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) FilterNode(io.trino.sql.planner.plan.FilterNode) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) InPredicate(io.trino.sql.tree.InPredicate) PlanMatchPattern.node(io.trino.sql.planner.assertions.PlanMatchPattern.node) ImmutableList(com.google.common.collect.ImmutableList) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) SymbolReference(io.trino.sql.tree.SymbolReference) GREATER_THAN(io.trino.sql.tree.ComparisonExpression.Operator.GREATER_THAN) PlanMatchPattern.apply(io.trino.sql.planner.assertions.PlanMatchPattern.apply) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) Symbol(io.trino.sql.planner.Symbol) SymbolReference(io.trino.sql.tree.SymbolReference) FilterNode(io.trino.sql.planner.plan.FilterNode) InPredicate(io.trino.sql.tree.InPredicate) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Example 23 with ComparisonExpression

use of io.trino.sql.tree.ComparisonExpression 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 24 with ComparisonExpression

use of io.trino.sql.tree.ComparisonExpression 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 25 with ComparisonExpression

use of io.trino.sql.tree.ComparisonExpression in project trino by trinodb.

the class TestTransformUncorrelatedSubqueryToJoin method testRewriteRightCorrelatedJoin.

@Test
public void testRewriteRightCorrelatedJoin() {
    tester().assertThat(new TransformUncorrelatedSubqueryToJoin()).on(p -> {
        Symbol a = p.symbol("a");
        Symbol b = p.symbol("b");
        return p.correlatedJoin(emptyList(), p.values(a), RIGHT, TRUE_LITERAL, p.values(b));
    }).matches(join(JoinNode.Type.INNER, ImmutableList.of(), Optional.empty(), values("a"), values("b")));
    tester().assertThat(new TransformUncorrelatedSubqueryToJoin()).on(p -> {
        Symbol a = p.symbol("a");
        Symbol b = p.symbol("b");
        return p.correlatedJoin(emptyList(), p.values(a), RIGHT, new ComparisonExpression(GREATER_THAN, b.toSymbolReference(), a.toSymbolReference()), p.values(b));
    }).matches(project(ImmutableMap.of("a", new ExpressionMatcher("if(b > a, a, null)"), "b", new ExpressionMatcher("b")), join(JoinNode.Type.INNER, ImmutableList.of(), Optional.empty(), values("a"), values("b"))));
}
Also used : ExpressionMatcher(io.trino.sql.planner.assertions.ExpressionMatcher) Symbol(io.trino.sql.planner.Symbol) INNER(io.trino.sql.planner.plan.CorrelatedJoinNode.Type.INNER) LEFT(io.trino.sql.planner.plan.CorrelatedJoinNode.Type.LEFT) ImmutableMap(com.google.common.collect.ImmutableMap) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) RIGHT(io.trino.sql.planner.plan.CorrelatedJoinNode.Type.RIGHT) Collections.emptyList(java.util.Collections.emptyList) Test(org.testng.annotations.Test) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) TRUE_LITERAL(io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) FULL(io.trino.sql.planner.plan.CorrelatedJoinNode.Type.FULL) ImmutableList(com.google.common.collect.ImmutableList) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) GREATER_THAN(io.trino.sql.tree.ComparisonExpression.Operator.GREATER_THAN) Optional(java.util.Optional) JoinNode(io.trino.sql.planner.plan.JoinNode) PlanMatchPattern.join(io.trino.sql.planner.assertions.PlanMatchPattern.join) ExpressionMatcher(io.trino.sql.planner.assertions.ExpressionMatcher) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) Symbol(io.trino.sql.planner.Symbol) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Aggregations

ComparisonExpression (io.trino.sql.tree.ComparisonExpression)58 Test (org.testng.annotations.Test)30 ImmutableList (com.google.common.collect.ImmutableList)24 Expression (io.trino.sql.tree.Expression)23 Symbol (io.trino.sql.planner.Symbol)22 SymbolReference (io.trino.sql.tree.SymbolReference)21 ImmutableMap (com.google.common.collect.ImmutableMap)18 LongLiteral (io.trino.sql.tree.LongLiteral)17 BaseRuleTest (io.trino.sql.planner.iterative.rule.test.BaseRuleTest)15 FunctionCall (io.trino.sql.tree.FunctionCall)15 GenericLiteral (io.trino.sql.tree.GenericLiteral)15 QualifiedName (io.trino.sql.tree.QualifiedName)15 Optional (java.util.Optional)15 StringLiteral (io.trino.sql.tree.StringLiteral)14 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)13 Type (io.trino.spi.type.Type)12 BIGINT (io.trino.spi.type.BigintType.BIGINT)11 FilterNode (io.trino.sql.planner.plan.FilterNode)11 ResolvedFunction (io.trino.metadata.ResolvedFunction)10 GREATER_THAN (io.trino.sql.tree.ComparisonExpression.Operator.GREATER_THAN)10