Search in sources :

Example 11 with SortItem

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;
}
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)

Example 12 with SortItem

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);
}
Also used : SortItem(io.trino.sql.tree.SortItem) 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) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) SortOrder(io.trino.spi.connector.SortOrder) OrderingScheme.sortItemToSortOrder(io.trino.sql.planner.OrderingScheme.sortItemToSortOrder) QuerySpecification(io.trino.sql.tree.QuerySpecification) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

SortItem (io.trino.sql.tree.SortItem)12 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)8 Expression (io.trino.sql.tree.Expression)8 ImmutableList (com.google.common.collect.ImmutableList)7 FunctionCall (io.trino.sql.tree.FunctionCall)7 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)6 SelectExpression (io.trino.sql.analyzer.Analysis.SelectExpression)5 IfExpression (io.trino.sql.tree.IfExpression)5 LambdaExpression (io.trino.sql.tree.LambdaExpression)5 OrderBy (io.trino.sql.tree.OrderBy)5 QualifiedName (io.trino.sql.tree.QualifiedName)5 QuerySpecification (io.trino.sql.tree.QuerySpecification)5 WindowFrame (io.trino.sql.tree.WindowFrame)5 LinkedHashMap (java.util.LinkedHashMap)5 QueryUtil.quotedIdentifier (io.trino.sql.QueryUtil.quotedIdentifier)4 Identifier (io.trino.sql.tree.Identifier)4 Query (io.trino.sql.tree.Query)4 Table (io.trino.sql.tree.Table)4 HashMap (java.util.HashMap)4 Test (org.junit.jupiter.api.Test)4