Search in sources :

Example 1 with SelectExpression

use of io.trino.sql.analyzer.Analysis.SelectExpression in project trino by trinodb.

the class QueryPlanner method plan.

public RelationPlan plan(QuerySpecification node) {
    PlanBuilder builder = planFrom(node);
    builder = filter(builder, analysis.getWhere(node), node);
    builder = aggregate(builder, node);
    builder = filter(builder, analysis.getHaving(node), node);
    builder = planWindowFunctions(node, builder, ImmutableList.copyOf(analysis.getWindowFunctions(node)));
    builder = planWindowMeasures(node, builder, ImmutableList.copyOf(analysis.getWindowMeasures(node)));
    List<SelectExpression> selectExpressions = analysis.getSelectExpressions(node);
    List<Expression> expressions = selectExpressions.stream().map(SelectExpression::getExpression).collect(toImmutableList());
    builder = subqueryPlanner.handleSubqueries(builder, expressions, analysis.getSubqueries(node));
    if (hasExpressionsToUnfold(selectExpressions)) {
        // pre-project the folded expressions to preserve any non-deterministic semantics of functions that might be referenced
        builder = builder.appendProjections(expressions, symbolAllocator, idAllocator);
    }
    List<Expression> outputs = outputExpressions(selectExpressions);
    if (node.getOrderBy().isPresent()) {
        // For queries with aggregation, it also requires grouping keys and translated aggregations.
        if (analysis.isAggregation(node)) {
            // Add projections for aggregations required by ORDER BY. After this step, grouping keys and translated
            // aggregations are visible.
            List<Expression> orderByAggregates = analysis.getOrderByAggregates(node.getOrderBy().get());
            builder = builder.appendProjections(orderByAggregates, symbolAllocator, idAllocator);
        }
        // Add projections for the outputs of SELECT, but stack them on top of the ones from the FROM clause so both are visible
        // when resolving the ORDER BY clause.
        builder = builder.appendProjections(outputs, symbolAllocator, idAllocator);
        // The new scope is the composite of the fields from the FROM and SELECT clause (local nested scopes). Fields from the bottom of
        // the scope stack need to be placed first to match the expected layout for nested scopes.
        List<Symbol> newFields = new ArrayList<>();
        newFields.addAll(builder.getTranslations().getFieldSymbols());
        outputs.stream().map(builder::translate).forEach(newFields::add);
        builder = builder.withScope(analysis.getScope(node.getOrderBy().get()), newFields);
        builder = planWindowFunctions(node, builder, ImmutableList.copyOf(analysis.getOrderByWindowFunctions(node.getOrderBy().get())));
        builder = planWindowMeasures(node, builder, ImmutableList.copyOf(analysis.getOrderByWindowMeasures(node.getOrderBy().get())));
    }
    List<Expression> orderBy = analysis.getOrderByExpressions(node);
    builder = subqueryPlanner.handleSubqueries(builder, orderBy, analysis.getSubqueries(node));
    builder = builder.appendProjections(Iterables.concat(orderBy, outputs), symbolAllocator, idAllocator);
    builder = distinct(builder, node, outputs);
    Optional<OrderingScheme> orderingScheme = orderingScheme(builder, node.getOrderBy(), analysis.getOrderByExpressions(node));
    builder = sort(builder, orderingScheme);
    builder = offset(builder, node.getOffset());
    builder = limit(builder, node.getLimit(), orderingScheme);
    builder = builder.appendProjections(outputs, symbolAllocator, idAllocator);
    return new RelationPlan(builder.getRoot(), analysis.getScope(node), computeOutputs(builder, outputs), outerContext);
}
Also used : 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) ArrayList(java.util.ArrayList) SelectExpression(io.trino.sql.analyzer.Analysis.SelectExpression) PlanBuilder.newPlanBuilder(io.trino.sql.planner.PlanBuilder.newPlanBuilder)

Example 2 with SelectExpression

use of io.trino.sql.analyzer.Analysis.SelectExpression in project trino by trinodb.

the class QueryPlanner method plan.

public RelationPlan plan(Query query) {
    PlanBuilder builder = planQueryBody(query);
    List<Expression> orderBy = analysis.getOrderByExpressions(query);
    builder = subqueryPlanner.handleSubqueries(builder, orderBy, analysis.getSubqueries(query));
    List<SelectExpression> selectExpressions = analysis.getSelectExpressions(query);
    List<Expression> outputs = selectExpressions.stream().map(SelectExpression::getExpression).collect(toImmutableList());
    builder = builder.appendProjections(Iterables.concat(orderBy, outputs), symbolAllocator, idAllocator);
    Optional<OrderingScheme> orderingScheme = orderingScheme(builder, query.getOrderBy(), analysis.getOrderByExpressions(query));
    builder = sort(builder, orderingScheme);
    builder = offset(builder, query.getOffset());
    builder = limit(builder, query.getLimit(), orderingScheme);
    builder = builder.appendProjections(outputs, symbolAllocator, idAllocator);
    return new RelationPlan(builder.getRoot(), analysis.getScope(query), computeOutputs(builder, outputs), outerContext);
}
Also used : 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) SelectExpression(io.trino.sql.analyzer.Analysis.SelectExpression) PlanBuilder.newPlanBuilder(io.trino.sql.planner.PlanBuilder.newPlanBuilder)

Aggregations

SelectExpression (io.trino.sql.analyzer.Analysis.SelectExpression)2 PlanBuilder.newPlanBuilder (io.trino.sql.planner.PlanBuilder.newPlanBuilder)2 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)2 Expression (io.trino.sql.tree.Expression)2 IfExpression (io.trino.sql.tree.IfExpression)2 LambdaExpression (io.trino.sql.tree.LambdaExpression)2 ArrayList (java.util.ArrayList)1