Search in sources :

Example 1 with WindowOperation

use of io.trino.sql.tree.WindowOperation 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;
}
Also used : WindowOperation(io.trino.sql.tree.WindowOperation) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) SortItem(io.trino.sql.tree.SortItem) WindowFrame(io.trino.sql.tree.WindowFrame) SelectExpression(io.trino.sql.analyzer.Analysis.SelectExpression) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) IfExpression(io.trino.sql.tree.IfExpression) Expression(io.trino.sql.tree.Expression) LambdaExpression(io.trino.sql.tree.LambdaExpression) ResolvedWindow(io.trino.sql.analyzer.Analysis.ResolvedWindow)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ResolvedWindow (io.trino.sql.analyzer.Analysis.ResolvedWindow)1 SelectExpression (io.trino.sql.analyzer.Analysis.SelectExpression)1 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)1 Expression (io.trino.sql.tree.Expression)1 IfExpression (io.trino.sql.tree.IfExpression)1 LambdaExpression (io.trino.sql.tree.LambdaExpression)1 SortItem (io.trino.sql.tree.SortItem)1 WindowFrame (io.trino.sql.tree.WindowFrame)1 WindowOperation (io.trino.sql.tree.WindowOperation)1