Search in sources :

Example 1 with SelectSymbol

use of io.crate.expression.symbol.SelectSymbol in project crate by crate.

the class LogicalPlanner method tryOptimizeForInSubquery.

// In case the subselect is inside an IN() or = ANY() apply a "natural" OrderBy to optimize
// the building of TermInSetQuery which does a sort on the collection of values.
// See issue https://github.com/crate/crate/issues/6755
// If the output values are already sorted (even in desc order) no optimization is needed
private LogicalPlan tryOptimizeForInSubquery(SelectSymbol selectSymbol, AnalyzedRelation relation, LogicalPlan planBuilder) {
    if (selectSymbol.getResultType() == SelectSymbol.ResultType.SINGLE_COLUMN_MULTIPLE_VALUES && relation instanceof QueriedSelectRelation) {
        QueriedSelectRelation queriedRelation = (QueriedSelectRelation) relation;
        OrderBy relationOrderBy = queriedRelation.orderBy();
        Symbol firstOutput = queriedRelation.outputs().get(0);
        if ((relationOrderBy == null || relationOrderBy.orderBySymbols().get(0).equals(firstOutput) == false) && DataTypes.isPrimitive(firstOutput.valueType())) {
            return Order.create(planBuilder, new OrderBy(Collections.singletonList(firstOutput)));
        }
    }
    return planBuilder;
}
Also used : OrderBy(io.crate.analyze.OrderBy) SelectSymbol(io.crate.expression.symbol.SelectSymbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Symbol(io.crate.expression.symbol.Symbol) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation)

Example 2 with SelectSymbol

use of io.crate.expression.symbol.SelectSymbol in project crate by crate.

the class MultiPhaseExecutor method execute.

public static CompletableFuture<SubQueryResults> execute(Map<LogicalPlan, SelectSymbol> dependencies, DependencyCarrier executor, PlannerContext plannerContext, Row params) {
    List<CompletableFuture<?>> dependencyFutures = new ArrayList<>(dependencies.size());
    IdentityHashMap<SelectSymbol, Object> valueBySubQuery = new IdentityHashMap<>();
    for (Map.Entry<LogicalPlan, SelectSymbol> entry : dependencies.entrySet()) {
        LogicalPlan depPlan = entry.getKey();
        SelectSymbol selectSymbol = entry.getValue();
        CollectingRowConsumer<?, ?> rowConsumer = getConsumer(selectSymbol.getResultType());
        depPlan.execute(executor, PlannerContext.forSubPlan(plannerContext), rowConsumer, params, SubQueryResults.EMPTY);
        dependencyFutures.add(rowConsumer.completionFuture().thenAccept(val -> {
            synchronized (valueBySubQuery) {
                valueBySubQuery.put(selectSymbol, val);
            }
        }));
    }
    return CompletableFuture.allOf(dependencyFutures.toArray(new CompletableFuture[0])).thenApply(ignored -> new SubQueryResults(valueBySubQuery));
}
Also used : SelectSymbol(io.crate.expression.symbol.SelectSymbol) LogicalPlan(io.crate.planner.operators.LogicalPlan) IdentityHashMap(java.util.IdentityHashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CollectingRowConsumer(io.crate.data.CollectingRowConsumer) ArrayList(java.util.ArrayList) DependencyCarrier(io.crate.planner.DependencyCarrier) List(java.util.List) Row(io.crate.data.Row) PlannerContext(io.crate.planner.PlannerContext) Map(java.util.Map) SelectSymbol(io.crate.expression.symbol.SelectSymbol) SubQueryResults(io.crate.planner.operators.SubQueryResults) FirstColumnConsumers(io.crate.execution.engine.FirstColumnConsumers) IdentityHashMap(java.util.IdentityHashMap) SubQueryResults(io.crate.planner.operators.SubQueryResults) ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) LogicalPlan(io.crate.planner.operators.LogicalPlan) IdentityHashMap(java.util.IdentityHashMap) Map(java.util.Map)

Example 3 with SelectSymbol

use of io.crate.expression.symbol.SelectSymbol in project crate by crate.

the class SQLExecutor method planInternal.

private <T> T planInternal(AnalyzedStatement analyzedStatement, UUID jobId, int fetchSize, Row params) {
    RoutingProvider routingProvider = new RoutingProvider(random.nextInt(), emptyList());
    PlannerContext plannerContext = new PlannerContext(planner.currentClusterState(), routingProvider, jobId, coordinatorTxnCtx, nodeCtx, fetchSize, null);
    Plan plan = planner.plan(analyzedStatement, plannerContext);
    if (plan instanceof LogicalPlan) {
        return (T) ((LogicalPlan) plan).build(plannerContext, Set.of(), new ProjectionBuilder(nodeCtx), TopN.NO_LIMIT, 0, null, null, params, new SubQueryResults(emptyMap()) {

            @Override
            public Object getSafe(SelectSymbol key) {
                return Literal.of(key.valueType(), null);
            }
        });
    }
    return (T) plan;
}
Also used : SelectSymbol(io.crate.expression.symbol.SelectSymbol) RoutingProvider(io.crate.metadata.RoutingProvider) PlannerContext(io.crate.planner.PlannerContext) SubQueryResults(io.crate.planner.operators.SubQueryResults) LogicalPlan(io.crate.planner.operators.LogicalPlan) CreateBlobTablePlan(io.crate.planner.node.ddl.CreateBlobTablePlan) LogicalPlan(io.crate.planner.operators.LogicalPlan) Plan(io.crate.planner.Plan) CreateTablePlan(io.crate.planner.node.ddl.CreateTablePlan) ProjectionBuilder(io.crate.execution.dsl.projection.builder.ProjectionBuilder)

Example 4 with SelectSymbol

use of io.crate.expression.symbol.SelectSymbol in project crate by crate.

the class SelectStatementAnalyzerTest method testArraySubqueryExpression.

@Test
public void testArraySubqueryExpression() throws Exception {
    var executor = SQLExecutor.builder(clusterService).build();
    AnalyzedRelation relation = executor.analyze("select array(select id from sys.shards) as shards_id_array from sys.shards");
    SelectSymbol arrayProjection = (SelectSymbol) ((AliasSymbol) relation.outputs().get(0)).symbol();
    assertThat(arrayProjection.getResultType(), is(SelectSymbol.ResultType.SINGLE_COLUMN_MULTIPLE_VALUES));
    assertThat(arrayProjection.valueType().id(), is(ArrayType.ID));
}
Also used : SelectSymbol(io.crate.expression.symbol.SelectSymbol) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 5 with SelectSymbol

use of io.crate.expression.symbol.SelectSymbol in project crate by crate.

the class CollectQueryCastRulesTest method assertCollectQuery.

private void assertCollectQuery(String query, String expected) {
    var collect = new Collect(tr1, Collections.emptyList(), new WhereClause(e.asSymbol(query)), 100, 10);
    var plan = (io.crate.planner.node.dql.Collect) collect.build(plannerContext, Set.of(), new ProjectionBuilder(e.nodeCtx), TopN.NO_LIMIT, 0, null, null, Row.EMPTY, new SubQueryResults(emptyMap()) {

        @Override
        public Object getSafe(SelectSymbol key) {
            return Literal.of(key.valueType(), null);
        }
    });
    assertThat(((RoutedCollectPhase) plan.collectPhase()).where().toString(), is(expected));
}
Also used : SelectSymbol(io.crate.expression.symbol.SelectSymbol) Collect(io.crate.planner.operators.Collect) SubQueryResults(io.crate.planner.operators.SubQueryResults) WhereClause(io.crate.analyze.WhereClause) ProjectionBuilder(io.crate.execution.dsl.projection.builder.ProjectionBuilder) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase)

Aggregations

SelectSymbol (io.crate.expression.symbol.SelectSymbol)7 LogicalPlan (io.crate.planner.operators.LogicalPlan)4 SubQueryResults (io.crate.planner.operators.SubQueryResults)4 PlannerContext (io.crate.planner.PlannerContext)3 WhereClause (io.crate.analyze.WhereClause)2 Row (io.crate.data.Row)2 RoutedCollectPhase (io.crate.execution.dsl.phases.RoutedCollectPhase)2 ProjectionBuilder (io.crate.execution.dsl.projection.builder.ProjectionBuilder)2 Symbol (io.crate.expression.symbol.Symbol)2 RoutingProvider (io.crate.metadata.RoutingProvider)2 DependencyCarrier (io.crate.planner.DependencyCarrier)2 Plan (io.crate.planner.Plan)2 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)1 SessionContext (io.crate.action.sql.SessionContext)1 AnalyzedUpdateStatement (io.crate.analyze.AnalyzedUpdateStatement)1 OrderBy (io.crate.analyze.OrderBy)1 QueriedSelectRelation (io.crate.analyze.QueriedSelectRelation)1 AbstractTableRelation (io.crate.analyze.relations.AbstractTableRelation)1 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)1 DocTableRelation (io.crate.analyze.relations.DocTableRelation)1