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