Search in sources :

Example 1 with GREATER_THAN

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

the class TestTransformCorrelatedJoinToJoin method testRewriteInnerCorrelatedJoin.

@Test
public void testRewriteInnerCorrelatedJoin() {
    tester().assertThat(new TransformCorrelatedJoinToJoin(tester().getPlannerContext())).on(p -> {
        Symbol a = p.symbol("a");
        Symbol b = p.symbol("b");
        return p.correlatedJoin(ImmutableList.of(a), p.values(a), p.filter(new ComparisonExpression(GREATER_THAN, b.toSymbolReference(), a.toSymbolReference()), p.values(b)));
    }).matches(join(JoinNode.Type.INNER, ImmutableList.of(), Optional.of("b > a"), values("a"), filter(TRUE_LITERAL, values("b"))));
    tester().assertThat(new TransformCorrelatedJoinToJoin(tester().getPlannerContext())).on(p -> {
        Symbol a = p.symbol("a");
        Symbol b = p.symbol("b");
        return p.correlatedJoin(ImmutableList.of(a), p.values(a), INNER, new ComparisonExpression(LESS_THAN, b.toSymbolReference(), new LongLiteral("3")), p.filter(new ComparisonExpression(GREATER_THAN, b.toSymbolReference(), a.toSymbolReference()), p.values(b)));
    }).matches(join(JoinNode.Type.INNER, ImmutableList.of(), Optional.of("b > a AND b < 3"), values("a"), filter(TRUE_LITERAL, values("b"))));
}
Also used : Symbol(io.trino.sql.planner.Symbol) INNER(io.trino.sql.planner.plan.CorrelatedJoinNode.Type.INNER) LEFT(io.trino.sql.planner.plan.CorrelatedJoinNode.Type.LEFT) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test) PlanMatchPattern.filter(io.trino.sql.planner.assertions.PlanMatchPattern.filter) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) TRUE_LITERAL(io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL) LESS_THAN(io.trino.sql.tree.ComparisonExpression.Operator.LESS_THAN) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) ImmutableList(com.google.common.collect.ImmutableList) LongLiteral(io.trino.sql.tree.LongLiteral) 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) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) LongLiteral(io.trino.sql.tree.LongLiteral) Symbol(io.trino.sql.planner.Symbol) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Example 2 with GREATER_THAN

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

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

use of io.trino.sql.tree.ComparisonExpression.Operator.GREATER_THAN 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)

Example 5 with GREATER_THAN

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

the class TestTransformCorrelatedJoinToJoin method testRewriteLeftCorrelatedJoin.

@Test
public void testRewriteLeftCorrelatedJoin() {
    tester().assertThat(new TransformCorrelatedJoinToJoin(tester().getPlannerContext())).on(p -> {
        Symbol a = p.symbol("a");
        Symbol b = p.symbol("b");
        return p.correlatedJoin(ImmutableList.of(a), p.values(a), LEFT, TRUE_LITERAL, p.filter(new ComparisonExpression(GREATER_THAN, b.toSymbolReference(), a.toSymbolReference()), p.values(b)));
    }).matches(join(JoinNode.Type.LEFT, ImmutableList.of(), Optional.of("b > a"), values("a"), filter(TRUE_LITERAL, values("b"))));
    tester().assertThat(new TransformCorrelatedJoinToJoin(tester().getPlannerContext())).on(p -> {
        Symbol a = p.symbol("a");
        Symbol b = p.symbol("b");
        return p.correlatedJoin(ImmutableList.of(a), p.values(a), LEFT, new ComparisonExpression(LESS_THAN, b.toSymbolReference(), new LongLiteral("3")), p.filter(new ComparisonExpression(GREATER_THAN, b.toSymbolReference(), a.toSymbolReference()), p.values(b)));
    }).matches(join(JoinNode.Type.LEFT, ImmutableList.of(), Optional.of("b > a AND b < 3"), values("a"), filter(TRUE_LITERAL, values("b"))));
}
Also used : Symbol(io.trino.sql.planner.Symbol) INNER(io.trino.sql.planner.plan.CorrelatedJoinNode.Type.INNER) LEFT(io.trino.sql.planner.plan.CorrelatedJoinNode.Type.LEFT) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test) PlanMatchPattern.filter(io.trino.sql.planner.assertions.PlanMatchPattern.filter) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) TRUE_LITERAL(io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL) LESS_THAN(io.trino.sql.tree.ComparisonExpression.Operator.LESS_THAN) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) ImmutableList(com.google.common.collect.ImmutableList) LongLiteral(io.trino.sql.tree.LongLiteral) 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) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) LongLiteral(io.trino.sql.tree.LongLiteral) Symbol(io.trino.sql.planner.Symbol) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)8 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)8 BaseRuleTest (io.trino.sql.planner.iterative.rule.test.BaseRuleTest)8 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)8 GREATER_THAN (io.trino.sql.tree.ComparisonExpression.Operator.GREATER_THAN)8 Test (org.testng.annotations.Test)8 ImmutableMap (com.google.common.collect.ImmutableMap)6 Optional (java.util.Optional)6 Symbol (io.trino.sql.planner.Symbol)5 PlanMatchPattern.project (io.trino.sql.planner.assertions.PlanMatchPattern.project)5 Assignments (io.trino.sql.planner.plan.Assignments)5 ExpressionMatcher (io.trino.sql.planner.assertions.ExpressionMatcher)4 PlanMatchPattern (io.trino.sql.planner.assertions.PlanMatchPattern)4 LEFT (io.trino.sql.planner.plan.CorrelatedJoinNode.Type.LEFT)4 TRUE_LITERAL (io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL)4 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