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"))));
}
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")))));
}
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"))))));
}
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"))));
}
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"))));
}
Aggregations