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