Search in sources :

Example 6 with ArithmeticBinaryExpression

use of io.trino.sql.tree.ArithmeticBinaryExpression in project trino by trinodb.

the class TestSqlParser method testPrecedenceAndAssociativity.

@Test
public void testPrecedenceAndAssociativity() {
    assertThat(expression("1 AND 2 AND 3 AND 4")).isEqualTo(new LogicalExpression(location(1, 1), LogicalExpression.Operator.AND, ImmutableList.of(new LongLiteral(location(1, 1), "1"), new LongLiteral(location(1, 7), "2"), new LongLiteral(location(1, 13), "3"), new LongLiteral(location(1, 19), "4"))));
    assertThat(expression("1 OR 2 OR 3 OR 4")).isEqualTo(new LogicalExpression(location(1, 1), LogicalExpression.Operator.OR, ImmutableList.of(new LongLiteral(location(1, 1), "1"), new LongLiteral(location(1, 6), "2"), new LongLiteral(location(1, 11), "3"), new LongLiteral(location(1, 16), "4"))));
    assertThat(expression("1 AND 2 AND 3 OR 4 AND 5 AND 6 OR 7 AND 8 AND 9")).isEqualTo(new LogicalExpression(location(1, 1), LogicalExpression.Operator.OR, ImmutableList.of(new LogicalExpression(location(1, 1), LogicalExpression.Operator.AND, ImmutableList.of(new LongLiteral(location(1, 1), "1"), new LongLiteral(location(1, 7), "2"), new LongLiteral(location(1, 13), "3"))), new LogicalExpression(location(1, 18), LogicalExpression.Operator.AND, ImmutableList.of(new LongLiteral(location(1, 18), "4"), new LongLiteral(location(1, 24), "5"), new LongLiteral(location(1, 30), "6"))), new LogicalExpression(location(1, 35), LogicalExpression.Operator.AND, ImmutableList.of(new LongLiteral(location(1, 35), "7"), new LongLiteral(location(1, 41), "8"), new LongLiteral(location(1, 47), "9"))))));
    assertExpression("1 AND 2 OR 3", LogicalExpression.or(LogicalExpression.and(new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3")));
    assertExpression("1 OR 2 AND 3", LogicalExpression.or(new LongLiteral("1"), LogicalExpression.and(new LongLiteral("2"), new LongLiteral("3"))));
    assertExpression("NOT 1 AND 2", LogicalExpression.and(new NotExpression(new LongLiteral("1")), new LongLiteral("2")));
    assertExpression("NOT 1 OR 2", LogicalExpression.or(new NotExpression(new LongLiteral("1")), new LongLiteral("2")));
    assertExpression("-1 + 2", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD, new LongLiteral("-1"), new LongLiteral("2")));
    assertExpression("1 - 2 - 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.SUBTRACT, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.SUBTRACT, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3")));
    assertExpression("1 / 2 / 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.DIVIDE, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.DIVIDE, new LongLiteral("1"), new LongLiteral("2")), new LongLiteral("3")));
    assertExpression("1 + 2 * 3", new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.ADD, new LongLiteral("1"), new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new LongLiteral("2"), new LongLiteral("3"))));
}
Also used : ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) LogicalExpression(io.trino.sql.tree.LogicalExpression) LongLiteral(io.trino.sql.tree.LongLiteral) NotExpression(io.trino.sql.tree.NotExpression) Test(org.junit.jupiter.api.Test)

Example 7 with ArithmeticBinaryExpression

use of io.trino.sql.tree.ArithmeticBinaryExpression in project trino by trinodb.

the class ImplementExceptAll method apply.

@Override
public Result apply(ExceptNode node, Captures captures, Context context) {
    SetOperationNodeTranslator translator = new SetOperationNodeTranslator(context.getSession(), metadata, context.getSymbolAllocator(), context.getIdAllocator());
    SetOperationNodeTranslator.TranslationResult result = translator.makeSetContainmentPlanForAll(node);
    // compute expected multiplicity for every row
    checkState(result.getCountSymbols().size() > 0, "ExceptNode translation result has no count symbols");
    ResolvedFunction greatest = metadata.resolveFunction(context.getSession(), QualifiedName.of("greatest"), fromTypes(BIGINT, BIGINT));
    Expression count = result.getCountSymbols().get(0).toSymbolReference();
    for (int i = 1; i < result.getCountSymbols().size(); i++) {
        count = new FunctionCall(greatest.toQualifiedName(), ImmutableList.of(new ArithmeticBinaryExpression(SUBTRACT, count, result.getCountSymbols().get(i).toSymbolReference()), new GenericLiteral("BIGINT", "0")));
    }
    // filter rows so that expected number of rows remains
    Expression removeExtraRows = new ComparisonExpression(LESS_THAN_OR_EQUAL, result.getRowNumberSymbol().toSymbolReference(), count);
    FilterNode filter = new FilterNode(context.getIdAllocator().getNextId(), result.getPlanNode(), removeExtraRows);
    // prune helper symbols
    ProjectNode project = new ProjectNode(context.getIdAllocator().getNextId(), filter, Assignments.identity(node.getOutputSymbols()));
    return Result.ofPlanNode(project);
}
Also used : ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) Expression(io.trino.sql.tree.Expression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) ResolvedFunction(io.trino.metadata.ResolvedFunction) FilterNode(io.trino.sql.planner.plan.FilterNode) ProjectNode(io.trino.sql.planner.plan.ProjectNode) FunctionCall(io.trino.sql.tree.FunctionCall) GenericLiteral(io.trino.sql.tree.GenericLiteral)

Example 8 with ArithmeticBinaryExpression

use of io.trino.sql.tree.ArithmeticBinaryExpression in project trino by trinodb.

the class TestMergeProjectWithValues method testCorrelation.

@Test
public void testCorrelation() {
    // correlation symbol in projection (note: the resulting plan is not yet supported in execution)
    tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(p.symbol("x"), expression("a + corr")), 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(new ArithmeticBinaryExpression(ADD, new LongLiteral("1"), new SymbolReference("corr"))))));
    // correlation symbol in values (note: the resulting plan is not yet supported in execution)
    tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(p.symbol("x"), expression("a")), p.valuesOfExpressions(ImmutableList.of(p.symbol("a")), ImmutableList.of(new Row(ImmutableList.of(new SymbolReference("corr"))))))).matches(values(ImmutableList.of("x"), ImmutableList.of(ImmutableList.of(new SymbolReference("corr")))));
    // correlation symbol is not present in the resulting expression
    tester().assertThat(new MergeProjectWithValues(tester().getMetadata())).on(p -> p.project(Assignments.of(p.symbol("x"), expression("1")), p.valuesOfExpressions(ImmutableList.of(p.symbol("a")), ImmutableList.of(new Row(ImmutableList.of(new SymbolReference("corr"))))))).matches(values(ImmutableList.of("x"), ImmutableList.of(ImmutableList.of(new LongLiteral("1")))));
}
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) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) LongLiteral(io.trino.sql.tree.LongLiteral) SymbolReference(io.trino.sql.tree.SymbolReference) Row(io.trino.sql.tree.Row) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Example 9 with ArithmeticBinaryExpression

use of io.trino.sql.tree.ArithmeticBinaryExpression in project trino by trinodb.

the class TestEliminateCrossJoins method testGiveUpOnComplexProjections.

@Test
public void testGiveUpOnComplexProjections() {
    Session session = testSessionBuilder().build();
    PlanNode plan = joinNode(projectNode(joinNode(values("a1"), values("b")), "a2", new ArithmeticBinaryExpression(ADD, new SymbolReference("a1"), new SymbolReference("b")), "b", new SymbolReference("b")), values("c"), "a2", "c", "b", "c");
    assertEquals(JoinGraph.buildFrom(tester().getPlannerContext(), plan, noLookup(), new PlanNodeIdAllocator(), session, createTestingTypeAnalyzer(tester().getPlannerContext()), TypeProvider.empty()).size(), 2);
}
Also used : ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) PlanNode(io.trino.sql.planner.plan.PlanNode) PlanNodeIdAllocator(io.trino.sql.planner.PlanNodeIdAllocator) SymbolReference(io.trino.sql.tree.SymbolReference) Session(io.trino.Session) Test(org.testng.annotations.Test) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest)

Example 10 with ArithmeticBinaryExpression

use of io.trino.sql.tree.ArithmeticBinaryExpression in project trino by trinodb.

the class TestJoinNodeFlattener method testCombinesCriteriaAndFilters.

@Test
public void testCombinesCriteriaAndFilters() {
    PlanNodeIdAllocator planNodeIdAllocator = new PlanNodeIdAllocator();
    PlanBuilder p = planBuilder(planNodeIdAllocator);
    Symbol a1 = p.symbol("A1");
    Symbol b1 = p.symbol("B1");
    Symbol b2 = p.symbol("B2");
    Symbol c1 = p.symbol("C1");
    Symbol c2 = p.symbol("C2");
    ValuesNode valuesA = p.values(a1);
    ValuesNode valuesB = p.values(b1, b2);
    ValuesNode valuesC = p.values(c1, c2);
    Expression bcFilter = and(new ComparisonExpression(GREATER_THAN, c2.toSymbolReference(), new LongLiteral("0")), new ComparisonExpression(NOT_EQUAL, c2.toSymbolReference(), new LongLiteral("7")), new ComparisonExpression(GREATER_THAN, b2.toSymbolReference(), c2.toSymbolReference()));
    ComparisonExpression abcFilter = new ComparisonExpression(LESS_THAN, new ArithmeticBinaryExpression(ADD, a1.toSymbolReference(), c1.toSymbolReference()), b1.toSymbolReference());
    JoinNode joinNode = p.join(INNER, valuesA, p.join(INNER, valuesB, valuesC, ImmutableList.of(equiJoinClause(b1, c1)), ImmutableList.of(b1, b2), ImmutableList.of(c1, c2), Optional.of(bcFilter)), ImmutableList.of(equiJoinClause(a1, b1)), ImmutableList.of(a1), ImmutableList.of(b1, b2, c1, c2), Optional.of(abcFilter));
    MultiJoinNode expected = new MultiJoinNode(new LinkedHashSet<>(ImmutableList.of(valuesA, valuesB, valuesC)), and(new ComparisonExpression(EQUAL, b1.toSymbolReference(), c1.toSymbolReference()), new ComparisonExpression(EQUAL, a1.toSymbolReference(), b1.toSymbolReference()), bcFilter, abcFilter), ImmutableList.of(a1, b1, b2, c1, c2), false);
    assertEquals(toMultiJoinNode(queryRunner.getPlannerContext(), joinNode, noLookup(), planNodeIdAllocator, DEFAULT_JOIN_LIMIT, false, testSessionBuilder().build(), createTestingTypeAnalyzer(queryRunner.getPlannerContext()), p.getTypes()), expected);
}
Also used : ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) ValuesNode(io.trino.sql.planner.plan.ValuesNode) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) PlanNodeIdAllocator(io.trino.sql.planner.PlanNodeIdAllocator) ArithmeticUnaryExpression(io.trino.sql.tree.ArithmeticUnaryExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) Expression(io.trino.sql.tree.Expression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) LongLiteral(io.trino.sql.tree.LongLiteral) Symbol(io.trino.sql.planner.Symbol) JoinNode(io.trino.sql.planner.plan.JoinNode) MultiJoinNode(io.trino.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode) MultiJoinNode.toMultiJoinNode(io.trino.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode.toMultiJoinNode) PlanBuilder(io.trino.sql.planner.iterative.rule.test.PlanBuilder) MultiJoinNode(io.trino.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode) MultiJoinNode.toMultiJoinNode(io.trino.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode.toMultiJoinNode) Test(org.testng.annotations.Test)

Aggregations

ArithmeticBinaryExpression (io.trino.sql.tree.ArithmeticBinaryExpression)18 Test (org.testng.annotations.Test)14 LongLiteral (io.trino.sql.tree.LongLiteral)11 Symbol (io.trino.sql.planner.Symbol)9 BaseRuleTest (io.trino.sql.planner.iterative.rule.test.BaseRuleTest)9 SymbolReference (io.trino.sql.tree.SymbolReference)9 ImmutableList (com.google.common.collect.ImmutableList)7 Expression (io.trino.sql.tree.Expression)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)6 BIGINT (io.trino.spi.type.BigintType.BIGINT)5 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)5 Assignments (io.trino.sql.planner.plan.Assignments)5 GenericLiteral (io.trino.sql.tree.GenericLiteral)5 PlanNodeIdAllocator (io.trino.sql.planner.PlanNodeIdAllocator)4 PlanMatchPattern.expression (io.trino.sql.planner.assertions.PlanMatchPattern.expression)4 PlanMatchPattern.project (io.trino.sql.planner.assertions.PlanMatchPattern.project)4 PlanMatchPattern.strictProject (io.trino.sql.planner.assertions.PlanMatchPattern.strictProject)4 PlanBuilder (io.trino.sql.planner.iterative.rule.test.PlanBuilder)4 FunctionCall (io.trino.sql.tree.FunctionCall)4