Search in sources :

Example 16 with ArithmeticBinaryExpression

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

the class GroupingOperationRewriter method rewriteGroupingOperation.

public static Expression rewriteGroupingOperation(GroupingOperation expression, List<Set<Integer>> groupingSets, Map<NodeRef<Expression>, ResolvedField> columnReferenceFields, Optional<Symbol> groupIdSymbol) {
    requireNonNull(groupIdSymbol, "groupIdSymbol is null");
    // See SQL:2011:4.16.2 and SQL:2011:6.9.10.
    if (groupingSets.size() == 1) {
        return new LongLiteral("0");
    } else {
        checkState(groupIdSymbol.isPresent(), "groupId symbol is missing");
        RelationId relationId = columnReferenceFields.get(NodeRef.of(expression.getGroupingColumns().get(0))).getFieldId().getRelationId();
        List<Integer> columns = expression.getGroupingColumns().stream().map(NodeRef::of).peek(groupingColumn -> checkState(columnReferenceFields.containsKey(groupingColumn), "the grouping column is not in the columnReferencesField map")).map(columnReferenceFields::get).map(ResolvedField::getFieldId).map(fieldId -> translateFieldToInteger(fieldId, relationId)).collect(toImmutableList());
        List<Expression> groupingResults = groupingSets.stream().map(groupingSet -> String.valueOf(calculateGrouping(groupingSet, columns))).map(LongLiteral::new).collect(toImmutableList());
        // It is necessary to add a 1 to the groupId because the underlying array is indexed starting at 1
        return new SubscriptExpression(new ArrayConstructor(groupingResults), new ArithmeticBinaryExpression(ADD, groupIdSymbol.get().toSymbolReference(), new GenericLiteral("BIGINT", "1")));
    }
}
Also used : FieldId(io.trino.sql.analyzer.FieldId) ArrayConstructor(io.trino.sql.tree.ArrayConstructor) ResolvedField(io.trino.sql.analyzer.ResolvedField) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) GroupingOperation(io.trino.sql.tree.GroupingOperation) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) Preconditions.checkState(com.google.common.base.Preconditions.checkState) GenericLiteral(io.trino.sql.tree.GenericLiteral) List(java.util.List) ADD(io.trino.sql.tree.ArithmeticBinaryExpression.Operator.ADD) LongLiteral(io.trino.sql.tree.LongLiteral) NodeRef(io.trino.sql.tree.NodeRef) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Optional(java.util.Optional) RelationId(io.trino.sql.analyzer.RelationId) Expression(io.trino.sql.tree.Expression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) ResolvedField(io.trino.sql.analyzer.ResolvedField) LongLiteral(io.trino.sql.tree.LongLiteral) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) Expression(io.trino.sql.tree.Expression) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) RelationId(io.trino.sql.analyzer.RelationId) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) ArrayConstructor(io.trino.sql.tree.ArrayConstructor) GenericLiteral(io.trino.sql.tree.GenericLiteral)

Example 17 with ArithmeticBinaryExpression

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

the class TestPushProjectionThroughExchange method testDoNotSkipIdentityProjectionIfOutputAbsent.

@Test
public void testDoNotSkipIdentityProjectionIfOutputAbsent() {
    // In the following example, the Projection over Exchange has got an identity assignment (a -> a).
    // The Projection is pushed down to Exchange's source, and the identity assignment is translated into
    // a0 -> a. Input symbol 'a' is not used in the Exchange for partitioning, ordering or as a hash symbol.
    // It is just passed to output.
    // When all the assignments from the parent Projection are added to the pushed-down Projection,
    // the translated assignment is added too, so that the input symbol 'a' can be passed to the Exchange's output.
    tester().assertThat(new PushProjectionThroughExchange()).on(p -> {
        Symbol a = p.symbol("a");
        Symbol aTimes5 = p.symbol("a_times_5");
        return p.project(Assignments.of(aTimes5, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new SymbolReference("a"), new LongLiteral("5")), a, a.toSymbolReference()), p.exchange(e -> e.addSource(p.values(a)).addInputsSet(a).singleDistributionPartitioningScheme(a)));
    }).matches(exchange(strictProject(ImmutableMap.of("a_0", expression("a"), "a_times_5", expression("a * 5")), values(ImmutableList.of("a")))));
    // In the following example, the Projection over Exchange has got an identity assignment (b -> b).
    // The Projection is pushed down to Exchange's source, and the identity assignment is translated into
    // a0 -> a. Input symbol 'a' is not used in the Exchange for partitioning, ordering or as a hash symbol.
    // It is just passed to output.
    // When all the assignments from the parent Projection are added to the pushed-down Projection,
    // the translated assignment is added too, so that the input symbol 'a' can be passed to the Exchange's output.
    tester().assertThat(new PushProjectionThroughExchange()).on(p -> {
        Symbol a = p.symbol("a");
        Symbol bTimes5 = p.symbol("b_times_5");
        Symbol b = p.symbol("b");
        return p.project(Assignments.of(bTimes5, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new SymbolReference("b"), new LongLiteral("5")), b, b.toSymbolReference()), p.exchange(e -> e.addSource(p.values(a)).addInputsSet(a).singleDistributionPartitioningScheme(b)));
    }).matches(exchange(strictProject(ImmutableMap.of("a_0", expression("a"), "a_times_5", expression("a * 5")), values(ImmutableList.of("a")))));
}
Also used : Symbol(io.trino.sql.planner.Symbol) PlanMatchPattern.expression(io.trino.sql.planner.assertions.PlanMatchPattern.expression) ImmutableMap(com.google.common.collect.ImmutableMap) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Assignments(io.trino.sql.planner.plan.Assignments) Test(org.testng.annotations.Test) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) OrderingScheme(io.trino.sql.planner.OrderingScheme) FIRST(io.trino.sql.tree.SortItem.NullOrdering.FIRST) SortOrder(io.trino.spi.connector.SortOrder) PlanMatchPattern.strictProject(io.trino.sql.planner.assertions.PlanMatchPattern.strictProject) GATHER(io.trino.sql.planner.plan.ExchangeNode.Type.GATHER) ImmutableList(com.google.common.collect.ImmutableList) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) SymbolReference(io.trino.sql.tree.SymbolReference) LongLiteral(io.trino.sql.tree.LongLiteral) PlanMatchPattern.exchange(io.trino.sql.planner.assertions.PlanMatchPattern.exchange) PlanMatchPattern.sort(io.trino.sql.planner.assertions.PlanMatchPattern.sort) REMOTE(io.trino.sql.planner.plan.ExchangeNode.Scope.REMOTE) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) ASCENDING(io.trino.sql.tree.SortItem.Ordering.ASCENDING) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) LongLiteral(io.trino.sql.tree.LongLiteral) Symbol(io.trino.sql.planner.Symbol) SymbolReference(io.trino.sql.tree.SymbolReference) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Example 18 with ArithmeticBinaryExpression

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

the class TestPushProjectionThroughJoin method testDoesNotPushStraddlingProjection.

@Test
public void testDoesNotPushStraddlingProjection() {
    PlanBuilder p = new PlanBuilder(new PlanNodeIdAllocator(), dummyMetadata(), TEST_SESSION);
    Symbol a = p.symbol("a");
    Symbol b = p.symbol("b");
    Symbol c = p.symbol("c");
    ProjectNode planNode = p.project(Assignments.of(c, new ArithmeticBinaryExpression(ADD, a.toSymbolReference(), b.toSymbolReference())), p.join(INNER, p.values(a), p.values(b)));
    Optional<PlanNode> rewritten = pushProjectionThroughJoin(PLANNER_CONTEXT, planNode, noLookup(), new PlanNodeIdAllocator(), testSessionBuilder().build(), createTestingTypeAnalyzer(PLANNER_CONTEXT), p.getTypes());
    assertThat(rewritten).isEmpty();
}
Also used : ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) PlanNode(io.trino.sql.planner.plan.PlanNode) PlanNodeIdAllocator(io.trino.sql.planner.PlanNodeIdAllocator) Symbol(io.trino.sql.planner.Symbol) ProjectNode(io.trino.sql.planner.plan.ProjectNode) PlanBuilder(io.trino.sql.planner.iterative.rule.test.PlanBuilder) 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