use of io.trino.sql.planner.OrderingScheme in project trino by trinodb.
the class PlanPrinter method formatAggregation.
public static String formatAggregation(Aggregation aggregation) {
StringBuilder builder = new StringBuilder();
String arguments = Joiner.on(", ").join(aggregation.getArguments());
if (aggregation.getArguments().isEmpty() && "count".equalsIgnoreCase(aggregation.getResolvedFunction().getSignature().getName())) {
arguments = "*";
}
if (aggregation.isDistinct()) {
arguments = "DISTINCT " + arguments;
}
builder.append(aggregation.getResolvedFunction().getSignature().getName()).append('(').append(arguments);
aggregation.getOrderingScheme().ifPresent(orderingScheme -> builder.append(' ').append(orderingScheme.getOrderBy().stream().map(input -> input + " " + orderingScheme.getOrdering(input)).collect(joining(", "))));
builder.append(')');
aggregation.getFilter().ifPresent(expression -> builder.append(" FILTER (WHERE ").append(expression).append(")"));
aggregation.getMask().ifPresent(symbol -> builder.append(" (mask = ").append(symbol).append(")"));
return builder.toString();
}
use of io.trino.sql.planner.OrderingScheme in project trino by trinodb.
the class TestPruneWindowColumns method buildProjectedWindow.
private static PlanNode buildProjectedWindow(PlanBuilder p, Predicate<Symbol> projectionFilter, Predicate<Symbol> sourceFilter) {
Symbol orderKey = p.symbol("orderKey");
Symbol partitionKey = p.symbol("partitionKey");
Symbol hash = p.symbol("hash");
Symbol startValue1 = p.symbol("startValue1");
Symbol startValue2 = p.symbol("startValue2");
Symbol endValue1 = p.symbol("endValue1");
Symbol endValue2 = p.symbol("endValue2");
Symbol input1 = p.symbol("input1");
Symbol input2 = p.symbol("input2");
Symbol unused = p.symbol("unused");
Symbol output1 = p.symbol("output1");
Symbol output2 = p.symbol("output2");
List<Symbol> inputs = ImmutableList.of(orderKey, partitionKey, hash, startValue1, startValue2, endValue1, endValue2, input1, input2, unused);
List<Symbol> outputs = ImmutableList.<Symbol>builder().addAll(inputs).add(output1, output2).build();
return p.project(Assignments.identity(outputs.stream().filter(projectionFilter).collect(toImmutableList())), p.window(new WindowNode.Specification(ImmutableList.of(partitionKey), Optional.of(new OrderingScheme(ImmutableList.of(orderKey), ImmutableMap.of(orderKey, SortOrder.ASC_NULLS_FIRST)))), ImmutableMap.of(output1, new WindowNode.Function(MIN_FUNCTION, ImmutableList.of(input1.toSymbolReference()), new WindowNode.Frame(WindowFrame.Type.RANGE, UNBOUNDED_PRECEDING, Optional.of(startValue1), Optional.of(orderKey), CURRENT_ROW, Optional.of(endValue1), Optional.of(orderKey), Optional.of(startValue1.toSymbolReference()), Optional.of(endValue2.toSymbolReference())), false), output2, new WindowNode.Function(MIN_FUNCTION, ImmutableList.of(input2.toSymbolReference()), new WindowNode.Frame(WindowFrame.Type.RANGE, UNBOUNDED_PRECEDING, Optional.of(startValue2), Optional.of(orderKey), CURRENT_ROW, Optional.of(endValue2), Optional.of(orderKey), Optional.of(startValue2.toSymbolReference()), Optional.of(endValue2.toSymbolReference())), false)), hash, p.values(inputs.stream().filter(sourceFilter).collect(toImmutableList()), ImmutableList.of())));
}
use of io.trino.sql.planner.OrderingScheme in project trino by trinodb.
the class PushAggregationIntoTableScan method toAggregateFunction.
private static AggregateFunction toAggregateFunction(Metadata metadata, Context context, AggregationNode.Aggregation aggregation) {
String canonicalName = metadata.getFunctionMetadata(aggregation.getResolvedFunction()).getCanonicalName();
BoundSignature signature = aggregation.getResolvedFunction().getSignature();
ImmutableList.Builder<ConnectorExpression> arguments = ImmutableList.builder();
for (int i = 0; i < aggregation.getArguments().size(); i++) {
SymbolReference argument = (SymbolReference) aggregation.getArguments().get(i);
arguments.add(new Variable(argument.getName(), signature.getArgumentTypes().get(i)));
}
Optional<OrderingScheme> orderingScheme = aggregation.getOrderingScheme();
Optional<List<SortItem>> sortBy = orderingScheme.map(OrderingScheme::toSortItems);
Optional<ConnectorExpression> filter = aggregation.getFilter().map(symbol -> new Variable(symbol.getName(), context.getSymbolAllocator().getTypes().get(symbol)));
return new AggregateFunction(canonicalName, signature.getReturnType(), arguments.build(), sortBy.orElse(ImmutableList.of()), aggregation.isDistinct(), filter);
}
use of io.trino.sql.planner.OrderingScheme in project trino by trinodb.
the class SymbolMapper method map.
public OrderingScheme map(OrderingScheme orderingScheme) {
ImmutableList.Builder<Symbol> newSymbols = ImmutableList.builder();
ImmutableMap.Builder<Symbol, SortOrder> newOrderings = ImmutableMap.builder();
Set<Symbol> added = new HashSet<>(orderingScheme.getOrderBy().size());
for (Symbol symbol : orderingScheme.getOrderBy()) {
Symbol canonical = map(symbol);
if (added.add(canonical)) {
newSymbols.add(canonical);
newOrderings.put(canonical, orderingScheme.getOrdering(symbol));
}
}
return new OrderingScheme(newSymbols.build(), newOrderings.buildOrThrow());
}
use of io.trino.sql.planner.OrderingScheme in project trino by trinodb.
the class LimitMatcher method detailMatches.
@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
checkState(shapeMatches(node));
LimitNode limitNode = (LimitNode) node;
if (!limitNode.isWithTies()) {
return match();
}
OrderingScheme tiesResolvingScheme = limitNode.getTiesResolvingScheme().get();
if (orderingSchemeMatches(tiesResolvers, tiesResolvingScheme, symbolAliases)) {
return match();
}
if (!limitNode.requiresPreSortedInputs()) {
return match();
}
if (preSortedInputs.stream().map(alias -> alias.toSymbol(symbolAliases)).collect(toImmutableSet()).equals(ImmutableSet.copyOf(limitNode.getPreSortedInputs()))) {
return match();
}
return NO_MATCH;
}
Aggregations