Search in sources :

Example 1 with SubqueryPlanner

use of io.crate.planner.SubqueryPlanner in project crate by crate.

the class LogicalPlanner method planSubSelect.

public LogicalPlan planSubSelect(SelectSymbol selectSymbol, PlannerContext plannerContext) {
    CoordinatorTxnCtx txnCtx = plannerContext.transactionContext();
    AnalyzedRelation relation = selectSymbol.relation();
    final int fetchSize;
    final java.util.function.Function<LogicalPlan, LogicalPlan> maybeApplySoftLimit;
    if (selectSymbol.getResultType() == SINGLE_COLUMN_SINGLE_VALUE) {
        // SELECT (SELECT foo FROM t)
        // ^^^^^^^^^^^^^^^^^
        // The subquery must return at most 1 row, if more than 1 row is returned semantics require us to throw an error.
        // So we limit the query to 2 if there is no limit to avoid retrieval of many rows while being able to validate max1row
        fetchSize = 2;
        maybeApplySoftLimit = plan -> new Limit(plan, Literal.of(2L), Literal.of(0L));
    } else {
        fetchSize = 0;
        maybeApplySoftLimit = plan -> plan;
    }
    PlannerContext subSelectPlannerContext = PlannerContext.forSubPlan(plannerContext, fetchSize);
    SubqueryPlanner subqueryPlanner = new SubqueryPlanner(s -> planSubSelect(s, subSelectPlannerContext));
    var planBuilder = new PlanBuilder(subqueryPlanner, txnCtx, tableStats, subSelectPlannerContext.params());
    LogicalPlan plan = relation.accept(planBuilder, relation.outputs());
    plan = tryOptimizeForInSubquery(selectSymbol, relation, plan);
    LogicalPlan optimizedPlan = optimizer.optimize(maybeApplySoftLimit.apply(plan), tableStats, txnCtx);
    return new RootRelationBoundary(optimizedPlan);
}
Also used : CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) SubqueryPlanner(io.crate.planner.SubqueryPlanner) AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) PlannerContext(io.crate.planner.PlannerContext)

Example 2 with SubqueryPlanner

use of io.crate.planner.SubqueryPlanner in project crate by crate.

the class UpdatePlanner method plan.

public static Plan plan(AnalyzedUpdateStatement update, PlannerContext plannerCtx, SubqueryPlanner subqueryPlanner) {
    if (update.outputs() != null && !plannerCtx.clusterState().getNodes().getMinNodeVersion().onOrAfter(Version.V_4_2_0)) {
        throw new UnsupportedFeatureException(RETURNING_VERSION_ERROR_MSG);
    }
    AbstractTableRelation<?> table = update.table();
    Plan plan;
    if (table instanceof DocTableRelation) {
        DocTableRelation docTable = (DocTableRelation) table;
        plan = plan(docTable, update.assignmentByTargetCol(), update.query(), plannerCtx, update.outputs());
    } else {
        plan = new Update((plannerContext, params, subQueryValues) -> sysUpdate(plannerContext, (TableRelation) table, update.assignmentByTargetCol(), update.query(), params, subQueryValues, update.outputs()));
    }
    Map<LogicalPlan, SelectSymbol> subQueries = subqueryPlanner.planSubQueries(update);
    return MultiPhasePlan.createIfNeeded(plan, subQueries);
}
Also used : UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) UpdateProjection(io.crate.execution.dsl.projection.UpdateProjection) SessionContext(io.crate.action.sql.SessionContext) SysUpdateProjection(io.crate.execution.dsl.projection.SysUpdateProjection) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) Collections.singletonList(java.util.Collections.singletonList) DependencyCarrier(io.crate.planner.DependencyCarrier) Map(java.util.Map) SelectSymbol(io.crate.expression.symbol.SelectSymbol) DistributionInfo(io.crate.planner.distribution.DistributionInfo) DocSysColumns(io.crate.metadata.doc.DocSysColumns) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) LogicalPlan(io.crate.planner.operators.LogicalPlan) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) ExecutionPlan(io.crate.planner.ExecutionPlan) List(java.util.List) Version(org.elasticsearch.Version) Row(io.crate.data.Row) Projection(io.crate.execution.dsl.projection.Projection) Symbol(io.crate.expression.symbol.Symbol) DataTypes(io.crate.types.DataTypes) SubQueryResults(io.crate.planner.operators.SubQueryResults) Assignments(io.crate.expression.symbol.Assignments) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) InputColumn(io.crate.expression.symbol.InputColumn) SubqueryPlanner(io.crate.planner.SubqueryPlanner) WhereClauseOptimizer(io.crate.planner.WhereClauseOptimizer) CompletableFuture(java.util.concurrent.CompletableFuture) AnalyzedUpdateStatement(io.crate.analyze.AnalyzedUpdateStatement) ArrayList(java.util.ArrayList) Routing(io.crate.metadata.Routing) Objects.requireNonNull(java.util.Objects.requireNonNull) UpdateById(io.crate.planner.node.dml.UpdateById) VersioningValidationException(io.crate.exceptions.VersioningValidationException) TopN(io.crate.execution.engine.pipeline.TopN) Optimizer(io.crate.planner.optimizer.symbol.Optimizer) Nullable(javax.annotation.Nullable) MergeCountProjection(io.crate.execution.dsl.projection.MergeCountProjection) TableRelation(io.crate.analyze.relations.TableRelation) WhereClause(io.crate.analyze.WhereClause) NodeOperationTree(io.crate.execution.dsl.phases.NodeOperationTree) Reference(io.crate.metadata.Reference) NodeOperationTreeGenerator(io.crate.execution.engine.NodeOperationTreeGenerator) SubQueryAndParamBinder(io.crate.planner.operators.SubQueryAndParamBinder) Merge(io.crate.planner.Merge) RoutingProvider(io.crate.metadata.RoutingProvider) RowConsumer(io.crate.data.RowConsumer) DocTableRelation(io.crate.analyze.relations.DocTableRelation) AbstractTableRelation(io.crate.analyze.relations.AbstractTableRelation) PlannerContext(io.crate.planner.PlannerContext) Plan(io.crate.planner.Plan) MultiPhasePlan(io.crate.planner.MultiPhasePlan) Collect(io.crate.planner.node.dql.Collect) SelectSymbol(io.crate.expression.symbol.SelectSymbol) UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) DocTableRelation(io.crate.analyze.relations.DocTableRelation) LogicalPlan(io.crate.planner.operators.LogicalPlan) LogicalPlan(io.crate.planner.operators.LogicalPlan) ExecutionPlan(io.crate.planner.ExecutionPlan) Plan(io.crate.planner.Plan) MultiPhasePlan(io.crate.planner.MultiPhasePlan)

Aggregations

PlannerContext (io.crate.planner.PlannerContext)2 SubqueryPlanner (io.crate.planner.SubqueryPlanner)2 SessionContext (io.crate.action.sql.SessionContext)1 AnalyzedUpdateStatement (io.crate.analyze.AnalyzedUpdateStatement)1 WhereClause (io.crate.analyze.WhereClause)1 AbstractTableRelation (io.crate.analyze.relations.AbstractTableRelation)1 AliasedAnalyzedRelation (io.crate.analyze.relations.AliasedAnalyzedRelation)1 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)1 DocTableRelation (io.crate.analyze.relations.DocTableRelation)1 TableRelation (io.crate.analyze.relations.TableRelation)1 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)1 Row (io.crate.data.Row)1 RowConsumer (io.crate.data.RowConsumer)1 UnsupportedFeatureException (io.crate.exceptions.UnsupportedFeatureException)1 VersioningValidationException (io.crate.exceptions.VersioningValidationException)1 NodeOperationTree (io.crate.execution.dsl.phases.NodeOperationTree)1 RoutedCollectPhase (io.crate.execution.dsl.phases.RoutedCollectPhase)1 MergeCountProjection (io.crate.execution.dsl.projection.MergeCountProjection)1 Projection (io.crate.execution.dsl.projection.Projection)1 SysUpdateProjection (io.crate.execution.dsl.projection.SysUpdateProjection)1