use of io.trino.sql.tree.Row in project trino by trinodb.
the class ReplaceJoinOverConstantWithProject method isSingleConstantRow.
private boolean isSingleConstantRow(PlanNode node) {
if (!(node instanceof ValuesNode)) {
return false;
}
ValuesNode values = (ValuesNode) node;
if (values.getRowCount() != 1) {
return false;
}
if (values.getRows().isEmpty()) {
return true;
}
Expression row = getOnlyElement(values.getRows().get());
return row instanceof Row;
}
use of io.trino.sql.tree.Row in project trino by trinodb.
the class TestMergeProjectWithValues method testNonDeterministicValues.
@Test
public void testNonDeterministicValues() {
FunctionCall randomFunction = new FunctionCall(tester().getMetadata().resolveFunction(tester().getSession(), QualifiedName.of("random"), ImmutableList.of()).toQualifiedName(), ImmutableList.of());
tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(p.symbol("rand"), expression("rand")), p.valuesOfExpressions(ImmutableList.of(p.symbol("rand")), ImmutableList.of(new Row(ImmutableList.of(randomFunction)))))).matches(values(ImmutableList.of("rand"), ImmutableList.of(ImmutableList.of(randomFunction))));
// ValuesNode has multiple rows
tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(p.symbol("output"), expression("value")), p.valuesOfExpressions(ImmutableList.of(p.symbol("value")), ImmutableList.of(new Row(ImmutableList.of(new NullLiteral())), new Row(ImmutableList.of(randomFunction)), new Row(ImmutableList.of(new ArithmeticUnaryExpression(MINUS, randomFunction))))))).matches(values(ImmutableList.of("output"), ImmutableList.of(ImmutableList.of(new NullLiteral()), ImmutableList.of(randomFunction), ImmutableList.of(new ArithmeticUnaryExpression(MINUS, randomFunction)))));
// ValuesNode has multiple non-deterministic outputs
tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(p.symbol("x"), expression("-a"), p.symbol("y"), expression("b")), p.valuesOfExpressions(ImmutableList.of(p.symbol("a"), p.symbol("b")), ImmutableList.of(new Row(ImmutableList.of(new DoubleLiteral("1e0"), randomFunction)), new Row(ImmutableList.of(randomFunction, new NullLiteral())), new Row(ImmutableList.of(new ArithmeticUnaryExpression(MINUS, randomFunction), new NullLiteral())))))).matches(values(ImmutableList.of("x", "y"), ImmutableList.of(ImmutableList.of(new ArithmeticUnaryExpression(MINUS, new DoubleLiteral("1e0")), randomFunction), ImmutableList.of(new ArithmeticUnaryExpression(MINUS, randomFunction), new NullLiteral()), ImmutableList.of(new ArithmeticUnaryExpression(MINUS, new ArithmeticUnaryExpression(MINUS, randomFunction)), new NullLiteral()))));
}
use of io.trino.sql.tree.Row in project trino by trinodb.
the class TestMergeProjectWithValues method testDoNotFireOnNonDeterministicValues.
@Test
public void testDoNotFireOnNonDeterministicValues() {
FunctionCall randomFunction = new FunctionCall(tester().getMetadata().resolveFunction(tester().getSession(), QualifiedName.of("random"), ImmutableList.of()).toQualifiedName(), ImmutableList.of());
tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(p.symbol("x"), expression("rand"), p.symbol("y"), expression("rand")), p.valuesOfExpressions(ImmutableList.of(p.symbol("rand")), ImmutableList.of(new Row(ImmutableList.of(randomFunction)))))).doesNotFire();
tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(p.symbol("x"), expression("rand + rand")), p.valuesOfExpressions(ImmutableList.of(p.symbol("rand")), ImmutableList.of(new Row(ImmutableList.of(randomFunction)))))).doesNotFire();
}
use of io.trino.sql.tree.Row in project trino by trinodb.
the class TestMergeProjectWithValues method testMergeProjectWithValues.
@Test
public void testMergeProjectWithValues() {
tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> {
Symbol a = p.symbol("a");
Symbol b = p.symbol("b");
Symbol c = p.symbol("c");
Symbol d = p.symbol("d");
Symbol e = p.symbol("e");
Symbol f = p.symbol("f");
Assignments.Builder assignments = Assignments.builder();
// identity assignment
assignments.putIdentity(a);
// renaming assignment
assignments.put(d, b.toSymbolReference());
// expression involving input symbol
assignments.put(e, new IsNullPredicate(a.toSymbolReference()));
// constant expression
assignments.put(f, new LongLiteral("1"));
return p.project(assignments.build(), p.valuesOfExpressions(ImmutableList.of(a, b, c), ImmutableList.of(new Row(ImmutableList.of(new CharLiteral("x"), new BooleanLiteral("true"), new LongLiteral("1"))), new Row(ImmutableList.of(new CharLiteral("y"), new BooleanLiteral("false"), new LongLiteral("2"))), new Row(ImmutableList.of(new CharLiteral("z"), new BooleanLiteral("true"), new LongLiteral("3"))))));
}).matches(values(ImmutableList.of("a", "d", "e", "f"), ImmutableList.of(ImmutableList.of(new CharLiteral("x"), new BooleanLiteral("true"), new IsNullPredicate(new CharLiteral("x")), new LongLiteral("1")), ImmutableList.of(new CharLiteral("y"), new BooleanLiteral("false"), new IsNullPredicate(new CharLiteral("y")), new LongLiteral("1")), ImmutableList.of(new CharLiteral("z"), new BooleanLiteral("true"), new IsNullPredicate(new CharLiteral("z")), new LongLiteral("1")))));
// ValuesNode has no rows
tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> {
Symbol a = p.symbol("a");
Symbol b = p.symbol("b");
Symbol c = p.symbol("c");
Symbol d = p.symbol("d");
Symbol e = p.symbol("e");
Symbol f = p.symbol("f");
Assignments.Builder assignments = Assignments.builder();
// identity assignment
assignments.putIdentity(a);
// renaming assignment
assignments.put(d, b.toSymbolReference());
// expression involving input symbol
assignments.put(e, new IsNullPredicate(a.toSymbolReference()));
// constant expression
assignments.put(f, new LongLiteral("1"));
return p.project(assignments.build(), p.values(ImmutableList.of(a, b, c), ImmutableList.of()));
}).matches(values(ImmutableList.of("a", "d", "e", "f"), ImmutableList.of()));
}
use of io.trino.sql.tree.Row in project trino by trinodb.
the class TestMergeProjectWithValues method testFailingExpression.
@Test
public void testFailingExpression() {
FunctionCall failFunction = new FunctionCall(tester().getMetadata().resolveFunction(tester().getSession(), QualifiedName.of("fail"), fromTypes(VARCHAR)).toQualifiedName(), ImmutableList.of(new StringLiteral("message")));
tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(p.symbol("x"), failFunction), p.valuesOfExpressions(ImmutableList.of(p.symbol("a")), ImmutableList.of(new Row(ImmutableList.of(new LongLiteral("1"))))))).matches(values(ImmutableList.of("x"), ImmutableList.of(ImmutableList.of(failFunction))));
}
Aggregations