use of io.trino.sql.tree.SortItem in project trino by trinodb.
the class QueryPlanner method planWindowMeasures.
private PlanBuilder planWindowMeasures(Node node, PlanBuilder subPlan, List<WindowOperation> windowMeasures) {
if (windowMeasures.isEmpty()) {
return subPlan;
}
for (WindowOperation windowMeasure : scopeAwareDistinct(subPlan, windowMeasures)) {
ResolvedWindow window = analysis.getWindow(windowMeasure);
checkState(window != null, "no resolved window for: " + windowMeasure);
// pre-project inputs
ImmutableList.Builder<Expression> inputsBuilder = ImmutableList.<Expression>builder().addAll(window.getPartitionBy()).addAll(getSortItemsFromOrderBy(window.getOrderBy()).stream().map(SortItem::getSortKey).iterator());
WindowFrame frame = window.getFrame().orElseThrow();
Optional<Expression> endValue = frame.getEnd().orElseThrow().getValue();
endValue.ifPresent(inputsBuilder::add);
List<Expression> inputs = inputsBuilder.build();
subPlan = subqueryPlanner.handleSubqueries(subPlan, inputs, analysis.getSubqueries(node));
subPlan = subPlan.appendProjections(inputs, symbolAllocator, idAllocator);
// process frame end
FrameOffsetPlanAndSymbol plan = planFrameOffset(subPlan, endValue.map(subPlan::translate));
subPlan = plan.getSubPlan();
Optional<Symbol> frameEnd = plan.getFrameOffsetSymbol();
subPlan = subqueryPlanner.handleSubqueries(subPlan, extractPatternRecognitionExpressions(frame.getVariableDefinitions(), frame.getMeasures()), analysis.getSubqueries(node));
subPlan = planPatternRecognition(subPlan, windowMeasure, window, frameEnd);
}
return subPlan;
}
use of io.trino.sql.tree.SortItem in project trino by trinodb.
the class QueryPlanner method planWindowSpecification.
public static WindowNode.Specification planWindowSpecification(List<Expression> partitionBy, Optional<OrderBy> orderBy, Function<Expression, Symbol> expressionRewrite) {
// Rewrite PARTITION BY
ImmutableList.Builder<Symbol> partitionBySymbols = ImmutableList.builder();
for (Expression expression : partitionBy) {
partitionBySymbols.add(expressionRewrite.apply(expression));
}
// Rewrite ORDER BY
LinkedHashMap<Symbol, SortOrder> orderings = new LinkedHashMap<>();
for (SortItem item : getSortItemsFromOrderBy(orderBy)) {
Symbol symbol = expressionRewrite.apply(item.getSortKey());
// don't override existing keys, i.e. when "ORDER BY a ASC, a DESC" is specified
orderings.putIfAbsent(symbol, sortItemToSortOrder(item));
}
Optional<OrderingScheme> orderingScheme = Optional.empty();
if (!orderings.isEmpty()) {
orderingScheme = Optional.of(new OrderingScheme(ImmutableList.copyOf(orderings.keySet()), orderings));
}
return new WindowNode.Specification(partitionBySymbols.build(), orderingScheme);
}
Aggregations