Search in sources :

Example 11 with Row

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;
}
Also used : ValuesNode(io.trino.sql.planner.plan.ValuesNode) Expression(io.trino.sql.tree.Expression) Row(io.trino.sql.tree.Row)

Example 12 with 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()))));
}
Also used : IsNullPredicate(io.trino.sql.tree.IsNullPredicate) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) Test(org.testng.annotations.Test) Cast(io.trino.sql.tree.Cast) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) BooleanLiteral(io.trino.sql.tree.BooleanLiteral) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) LongLiteral(io.trino.sql.tree.LongLiteral) NullLiteral(io.trino.sql.tree.NullLiteral) FunctionCall(io.trino.sql.tree.FunctionCall) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) Symbol(io.trino.sql.planner.Symbol) RowType(io.trino.spi.type.RowType) StringLiteral(io.trino.sql.tree.StringLiteral) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) Assignments(io.trino.sql.planner.plan.Assignments) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) DoubleLiteral(io.trino.sql.tree.DoubleLiteral) QualifiedName(io.trino.sql.tree.QualifiedName) CharLiteral(io.trino.sql.tree.CharLiteral) ADD(io.trino.sql.tree.ArithmeticBinaryExpression.Operator.ADD) BIGINT(io.trino.spi.type.BigintType.BIGINT) SymbolReference(io.trino.sql.tree.SymbolReference) Row(io.trino.sql.tree.Row) PlanBuilder.expression(io.trino.sql.planner.iterative.rule.test.PlanBuilder.expression) MINUS(io.trino.sql.tree.ArithmeticUnaryExpression.Sign.MINUS) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) FunctionCall(io.trino.sql.tree.FunctionCall) Row(io.trino.sql.tree.Row) DoubleLiteral(io.trino.sql.tree.DoubleLiteral) NullLiteral(io.trino.sql.tree.NullLiteral) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Example 13 with Row

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();
}
Also used : IsNullPredicate(io.trino.sql.tree.IsNullPredicate) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) Test(org.testng.annotations.Test) Cast(io.trino.sql.tree.Cast) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) BooleanLiteral(io.trino.sql.tree.BooleanLiteral) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) LongLiteral(io.trino.sql.tree.LongLiteral) NullLiteral(io.trino.sql.tree.NullLiteral) FunctionCall(io.trino.sql.tree.FunctionCall) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) Symbol(io.trino.sql.planner.Symbol) RowType(io.trino.spi.type.RowType) StringLiteral(io.trino.sql.tree.StringLiteral) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) Assignments(io.trino.sql.planner.plan.Assignments) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) DoubleLiteral(io.trino.sql.tree.DoubleLiteral) QualifiedName(io.trino.sql.tree.QualifiedName) CharLiteral(io.trino.sql.tree.CharLiteral) ADD(io.trino.sql.tree.ArithmeticBinaryExpression.Operator.ADD) BIGINT(io.trino.spi.type.BigintType.BIGINT) SymbolReference(io.trino.sql.tree.SymbolReference) Row(io.trino.sql.tree.Row) PlanBuilder.expression(io.trino.sql.planner.iterative.rule.test.PlanBuilder.expression) MINUS(io.trino.sql.tree.ArithmeticUnaryExpression.Sign.MINUS) FunctionCall(io.trino.sql.tree.FunctionCall) Row(io.trino.sql.tree.Row) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Example 14 with Row

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()));
}
Also used : IsNullPredicate(io.trino.sql.tree.IsNullPredicate) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) Test(org.testng.annotations.Test) Cast(io.trino.sql.tree.Cast) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) BooleanLiteral(io.trino.sql.tree.BooleanLiteral) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) LongLiteral(io.trino.sql.tree.LongLiteral) NullLiteral(io.trino.sql.tree.NullLiteral) FunctionCall(io.trino.sql.tree.FunctionCall) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) Symbol(io.trino.sql.planner.Symbol) RowType(io.trino.spi.type.RowType) StringLiteral(io.trino.sql.tree.StringLiteral) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) Assignments(io.trino.sql.planner.plan.Assignments) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) DoubleLiteral(io.trino.sql.tree.DoubleLiteral) QualifiedName(io.trino.sql.tree.QualifiedName) CharLiteral(io.trino.sql.tree.CharLiteral) ADD(io.trino.sql.tree.ArithmeticBinaryExpression.Operator.ADD) BIGINT(io.trino.spi.type.BigintType.BIGINT) SymbolReference(io.trino.sql.tree.SymbolReference) Row(io.trino.sql.tree.Row) PlanBuilder.expression(io.trino.sql.planner.iterative.rule.test.PlanBuilder.expression) MINUS(io.trino.sql.tree.ArithmeticUnaryExpression.Sign.MINUS) CharLiteral(io.trino.sql.tree.CharLiteral) LongLiteral(io.trino.sql.tree.LongLiteral) Symbol(io.trino.sql.planner.Symbol) BooleanLiteral(io.trino.sql.tree.BooleanLiteral) IsNullPredicate(io.trino.sql.tree.IsNullPredicate) Row(io.trino.sql.tree.Row) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Example 15 with Row

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))));
}
Also used : IsNullPredicate(io.trino.sql.tree.IsNullPredicate) TypeSignatureProvider.fromTypes(io.trino.sql.analyzer.TypeSignatureProvider.fromTypes) Test(org.testng.annotations.Test) Cast(io.trino.sql.tree.Cast) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) BooleanLiteral(io.trino.sql.tree.BooleanLiteral) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) LongLiteral(io.trino.sql.tree.LongLiteral) NullLiteral(io.trino.sql.tree.NullLiteral) FunctionCall(io.trino.sql.tree.FunctionCall) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) Symbol(io.trino.sql.planner.Symbol) RowType(io.trino.spi.type.RowType) StringLiteral(io.trino.sql.tree.StringLiteral) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) Assignments(io.trino.sql.planner.plan.Assignments) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) DoubleLiteral(io.trino.sql.tree.DoubleLiteral) QualifiedName(io.trino.sql.tree.QualifiedName) CharLiteral(io.trino.sql.tree.CharLiteral) ADD(io.trino.sql.tree.ArithmeticBinaryExpression.Operator.ADD) BIGINT(io.trino.spi.type.BigintType.BIGINT) SymbolReference(io.trino.sql.tree.SymbolReference) Row(io.trino.sql.tree.Row) PlanBuilder.expression(io.trino.sql.planner.iterative.rule.test.PlanBuilder.expression) MINUS(io.trino.sql.tree.ArithmeticUnaryExpression.Sign.MINUS) StringLiteral(io.trino.sql.tree.StringLiteral) LongLiteral(io.trino.sql.tree.LongLiteral) FunctionCall(io.trino.sql.tree.FunctionCall) Row(io.trino.sql.tree.Row) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Aggregations

Row (io.trino.sql.tree.Row)26 ImmutableList (com.google.common.collect.ImmutableList)15 Cast (io.trino.sql.tree.Cast)15 TypeSignatureTranslator.toSqlType (io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType)14 ValuesNode (io.trino.sql.planner.plan.ValuesNode)13 Symbol (io.trino.sql.planner.Symbol)12 Expression (io.trino.sql.tree.Expression)11 FunctionCall (io.trino.sql.tree.FunctionCall)11 LongLiteral (io.trino.sql.tree.LongLiteral)11 Assignments (io.trino.sql.planner.plan.Assignments)10 NullLiteral (io.trino.sql.tree.NullLiteral)10 QualifiedName (io.trino.sql.tree.QualifiedName)10 StringLiteral (io.trino.sql.tree.StringLiteral)10 Test (org.testng.annotations.Test)10 RowType (io.trino.spi.type.RowType)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)8 BIGINT (io.trino.spi.type.BigintType.BIGINT)8 VARCHAR (io.trino.spi.type.VarcharType.VARCHAR)8 Type (io.trino.spi.type.Type)7 TypeSignatureProvider.fromTypes (io.trino.sql.analyzer.TypeSignatureProvider.fromTypes)7