Search in sources :

Example 1 with SubQueryResults

use of io.crate.planner.operators.SubQueryResults in project crate by crate.

the class RefreshTablePlan method executeOrFail.

@Override
public void executeOrFail(DependencyCarrier dependencies, PlannerContext plannerContext, RowConsumer consumer, Row parameters, SubQueryResults subQueryResults) {
    if (analysis.tables().isEmpty()) {
        consumer.accept(InMemoryBatchIterator.empty(SENTINEL), null);
        return;
    }
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(plannerContext.transactionContext(), plannerContext.nodeContext(), x, parameters, subQueryResults);
    ArrayList<String> toRefresh = new ArrayList<>();
    for (Map.Entry<Table<Symbol>, DocTableInfo> table : analysis.tables().entrySet()) {
        var tableInfo = table.getValue();
        var tableSymbol = table.getKey();
        if (tableSymbol.partitionProperties().isEmpty()) {
            toRefresh.addAll(Arrays.asList(tableInfo.concreteOpenIndices()));
        } else {
            var partitionName = toPartitionName(tableInfo, Lists2.map(tableSymbol.partitionProperties(), p -> p.map(eval)));
            if (!tableInfo.partitions().contains(partitionName)) {
                throw new PartitionUnknownException(partitionName);
            }
            toRefresh.add(partitionName.asIndexName());
        }
    }
    RefreshRequest request = new RefreshRequest(toRefresh.toArray(String[]::new));
    request.indicesOptions(IndicesOptions.lenientExpandOpen());
    var transportRefreshAction = dependencies.transportActionProvider().transportRefreshAction();
    transportRefreshAction.execute(request, new OneRowActionListener<>(consumer, response -> new Row1(toRefresh.isEmpty() ? -1L : (long) toRefresh.size())));
}
Also used : AnalyzedRefreshTable(io.crate.analyze.AnalyzedRefreshTable) DocTableInfo(io.crate.metadata.doc.DocTableInfo) Arrays(java.util.Arrays) SENTINEL(io.crate.data.SentinelRow.SENTINEL) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) Table(io.crate.sql.tree.Table) Function(java.util.function.Function) Lists2(io.crate.common.collections.Lists2) ArrayList(java.util.ArrayList) RowConsumer(io.crate.data.RowConsumer) DependencyCarrier(io.crate.planner.DependencyCarrier) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) PlannerContext(io.crate.planner.PlannerContext) Plan(io.crate.planner.Plan) Map(java.util.Map) SubQueryResults(io.crate.planner.operators.SubQueryResults) RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) OneRowActionListener(io.crate.execution.support.OneRowActionListener) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) PartitionPropertiesAnalyzer.toPartitionName(io.crate.analyze.PartitionPropertiesAnalyzer.toPartitionName) Row1(io.crate.data.Row1) RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) DocTableInfo(io.crate.metadata.doc.DocTableInfo) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) AnalyzedRefreshTable(io.crate.analyze.AnalyzedRefreshTable) Table(io.crate.sql.tree.Table) ArrayList(java.util.ArrayList) Row1(io.crate.data.Row1) Map(java.util.Map)

Example 2 with SubQueryResults

use of io.crate.planner.operators.SubQueryResults in project crate by crate.

the class CopyToPlan method bind.

@VisibleForTesting
public static BoundCopyTo bind(AnalyzedCopyTo copyTo, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx, Row parameters, SubQueryResults subQueryResults) {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(txnCtx, nodeCtx, x, parameters, subQueryResults);
    DocTableInfo table = (DocTableInfo) copyTo.tableInfo();
    List<String> partitions = resolvePartitions(Lists2.map(copyTo.table().partitionProperties(), x -> x.map(eval)), table);
    List<Symbol> outputs = new ArrayList<>();
    Map<ColumnIdent, Symbol> overwrites = null;
    boolean columnsDefined = false;
    final List<String> outputNames = new ArrayList<>(copyTo.columns().size());
    if (!copyTo.columns().isEmpty()) {
        // TODO: remove outputNames?
        for (Symbol symbol : copyTo.columns()) {
            assert symbol instanceof Reference : "Only references are expected here";
            RefVisitor.visitRefs(symbol, r -> outputNames.add(r.column().sqlFqn()));
            outputs.add(DocReferences.toSourceLookup(symbol));
        }
        columnsDefined = true;
    } else {
        Reference sourceRef;
        if (table.isPartitioned() && partitions.isEmpty()) {
            // table is partitioned, insert partitioned columns into the output
            overwrites = new HashMap<>();
            for (Reference reference : table.partitionedByColumns()) {
                if (!(reference instanceof GeneratedReference)) {
                    overwrites.put(reference.column(), reference);
                }
            }
            if (overwrites.size() > 0) {
                sourceRef = table.getReference(DocSysColumns.DOC);
            } else {
                sourceRef = table.getReference(DocSysColumns.RAW);
            }
        } else {
            sourceRef = table.getReference(DocSysColumns.RAW);
        }
        outputs = List.of(sourceRef);
    }
    Settings settings = genericPropertiesToSettings(copyTo.properties().map(eval));
    WriterProjection.CompressionType compressionType = settingAsEnum(WriterProjection.CompressionType.class, COMPRESSION_SETTING.get(settings));
    WriterProjection.OutputFormat outputFormat = settingAsEnum(WriterProjection.OutputFormat.class, OUTPUT_FORMAT_SETTING.get(settings));
    if (!columnsDefined && outputFormat == WriterProjection.OutputFormat.JSON_ARRAY) {
        throw new UnsupportedFeatureException("Output format not supported without specifying columns.");
    }
    WhereClause whereClause = new WhereClause(copyTo.whereClause(), partitions, Collections.emptySet());
    return new BoundCopyTo(outputs, table, whereClause, Literal.of(DataTypes.STRING.sanitizeValue(eval.apply(copyTo.uri()))), compressionType, outputFormat, outputNames.isEmpty() ? null : outputNames, columnsDefined, overwrites, settings);
}
Also used : UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) GenericPropertiesConverter.genericPropertiesToSettings(io.crate.analyze.GenericPropertiesConverter.genericPropertiesToSettings) Match(io.crate.planner.optimizer.matcher.Match) BoundCopyTo(io.crate.analyze.BoundCopyTo) DependencyCarrier(io.crate.planner.DependencyCarrier) RefVisitor(io.crate.expression.symbol.RefVisitor) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) TableStats(io.crate.statistics.TableStats) Settings(org.elasticsearch.common.settings.Settings) Map(java.util.Map) OUTPUT_FORMAT_SETTING(io.crate.analyze.CopyStatementSettings.OUTPUT_FORMAT_SETTING) DocSysColumns(io.crate.metadata.doc.DocSysColumns) DocTableInfo(io.crate.metadata.doc.DocTableInfo) NodeContext(io.crate.metadata.NodeContext) LogicalPlan(io.crate.planner.operators.LogicalPlan) Captures(io.crate.planner.optimizer.matcher.Captures) GeneratedReference(io.crate.metadata.GeneratedReference) Collect(io.crate.planner.operators.Collect) Set(java.util.Set) Lists2(io.crate.common.collections.Lists2) ExecutionPlan(io.crate.planner.ExecutionPlan) OptimizeCollectWhereClauseAccess(io.crate.planner.optimizer.rule.OptimizeCollectWhereClauseAccess) DocReferences(io.crate.metadata.DocReferences) List(java.util.List) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) DataTypes(io.crate.types.DataTypes) SubQueryResults(io.crate.planner.operators.SubQueryResults) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) PartitionPropertiesAnalyzer(io.crate.analyze.PartitionPropertiesAnalyzer) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Assignment(io.crate.sql.tree.Assignment) COMPRESSION_SETTING(io.crate.analyze.CopyStatementSettings.COMPRESSION_SETTING) AnalyzedCopyTo(io.crate.analyze.AnalyzedCopyTo) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) ProjectionBuilder(io.crate.execution.dsl.projection.builder.ProjectionBuilder) MergeCountProjection(io.crate.execution.dsl.projection.MergeCountProjection) WhereClause(io.crate.analyze.WhereClause) NodeOperationTree(io.crate.execution.dsl.phases.NodeOperationTree) ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference) NodeOperationTreeGenerator(io.crate.execution.engine.NodeOperationTreeGenerator) Merge(io.crate.planner.Merge) RowConsumer(io.crate.data.RowConsumer) WriterProjection(io.crate.execution.dsl.projection.WriterProjection) DocTableRelation(io.crate.analyze.relations.DocTableRelation) Literal(io.crate.expression.symbol.Literal) PlannerContext(io.crate.planner.PlannerContext) CopyStatementSettings.settingAsEnum(io.crate.analyze.CopyStatementSettings.settingAsEnum) Plan(io.crate.planner.Plan) Collections(java.util.Collections) DocTableInfo(io.crate.metadata.doc.DocTableInfo) GeneratedReference(io.crate.metadata.GeneratedReference) UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) BoundCopyTo(io.crate.analyze.BoundCopyTo) Symbol(io.crate.expression.symbol.Symbol) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) ArrayList(java.util.ArrayList) WhereClause(io.crate.analyze.WhereClause) WriterProjection(io.crate.execution.dsl.projection.WriterProjection) ColumnIdent(io.crate.metadata.ColumnIdent) GenericPropertiesConverter.genericPropertiesToSettings(io.crate.analyze.GenericPropertiesConverter.genericPropertiesToSettings) Settings(org.elasticsearch.common.settings.Settings) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 3 with SubQueryResults

use of io.crate.planner.operators.SubQueryResults in project crate by crate.

the class SetSessionPlan method executeOrFail.

@Override
public void executeOrFail(DependencyCarrier executor, PlannerContext plannerContext, RowConsumer consumer, Row params, SubQueryResults subQueryResults) throws Exception {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(plannerContext.transactionContext(), plannerContext.nodeContext(), x, params, subQueryResults);
    SessionContext sessionContext = plannerContext.transactionContext().sessionContext();
    Assignment<Symbol> assignment = settings.get(0);
    String settingName = eval.apply(assignment.columnName()).toString();
    validateSetting(settingName);
    SessionSetting<?> sessionSetting = sessionSettingRegistry.settings().get(settingName);
    if (sessionSetting == null) {
        LOGGER.info("SET SESSION STATEMENT WILL BE IGNORED: {}", settingName);
    } else {
        sessionSetting.apply(sessionContext, assignment.expressions(), eval);
    }
    consumer.accept(InMemoryBatchIterator.empty(SENTINEL), null);
}
Also used : CrateSettings(io.crate.metadata.settings.CrateSettings) SessionContext(io.crate.action.sql.SessionContext) SENTINEL(io.crate.data.SentinelRow.SENTINEL) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) SessionSetting(io.crate.metadata.settings.session.SessionSetting) Function(java.util.function.Function) RowConsumer(io.crate.data.RowConsumer) DependencyCarrier(io.crate.planner.DependencyCarrier) Assignment(io.crate.sql.tree.Assignment) List(java.util.List) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) Logger(org.apache.logging.log4j.Logger) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) PlannerContext(io.crate.planner.PlannerContext) Locale(java.util.Locale) Plan(io.crate.planner.Plan) SubQueryResults(io.crate.planner.operators.SubQueryResults) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) LogManager(org.apache.logging.log4j.LogManager) SessionSettingRegistry(io.crate.metadata.settings.session.SessionSettingRegistry) Symbol(io.crate.expression.symbol.Symbol) SessionContext(io.crate.action.sql.SessionContext)

Example 4 with SubQueryResults

use of io.crate.planner.operators.SubQueryResults 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 5 with SubQueryResults

use of io.crate.planner.operators.SubQueryResults in project crate by crate.

the class CreateViewPlan method executeOrFail.

@Override
public void executeOrFail(DependencyCarrier dependencies, PlannerContext plannerContext, RowConsumer consumer, Row params, SubQueryResults subQueryResults) {
    User owner = createViewStmt.owner();
    String formattedQuery = SqlFormatter.formatSql(createViewStmt.query(), makeExpressions(params));
    ensureFormattedQueryCanStillBeAnalyzed(createViewStmt.name(), dependencies.nodeContext(), dependencies.schemas(), plannerContext.transactionContext(), formattedQuery, createViewStmt.replaceExisting());
    CreateViewRequest request = new CreateViewRequest(createViewStmt.name(), formattedQuery, createViewStmt.replaceExisting(), owner == null ? null : owner.name());
    dependencies.createViewAction().execute(request, new OneRowActionListener<>(consumer, resp -> {
        if (resp.alreadyExistsFailure()) {
            throw new RelationAlreadyExists(createViewStmt.name());
        }
        return new Row1(1L);
    }));
}
Also used : ParamTypeHints(io.crate.analyze.ParamTypeHints) Literal(io.crate.sql.tree.Literal) RelationName(io.crate.metadata.RelationName) SearchPath(io.crate.metadata.SearchPath) DefaultTraversalVisitor(io.crate.sql.tree.DefaultTraversalVisitor) ArrayList(java.util.ArrayList) CreateViewStmt(io.crate.analyze.CreateViewStmt) SqlParser(io.crate.sql.parser.SqlParser) OneRowActionListener(io.crate.execution.support.OneRowActionListener) Query(io.crate.sql.tree.Query) Nullable(javax.annotation.Nullable) NodeContext(io.crate.metadata.NodeContext) ParameterExpression(io.crate.sql.tree.ParameterExpression) User(io.crate.user.User) CreateViewRequest(io.crate.execution.ddl.views.CreateViewRequest) SqlFormatter(io.crate.sql.SqlFormatter) Table(io.crate.sql.tree.Table) RowConsumer(io.crate.data.RowConsumer) List(java.util.List) Row(io.crate.data.Row) RelationAlreadyExists(io.crate.exceptions.RelationAlreadyExists) Symbol(io.crate.expression.symbol.Symbol) RelationAnalyzer(io.crate.analyze.relations.RelationAnalyzer) SubQueryResults(io.crate.planner.operators.SubQueryResults) Schemas(io.crate.metadata.Schemas) Expression(io.crate.sql.tree.Expression) Row1(io.crate.data.Row1) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) Row1(io.crate.data.Row1) User(io.crate.user.User) RelationAlreadyExists(io.crate.exceptions.RelationAlreadyExists) CreateViewRequest(io.crate.execution.ddl.views.CreateViewRequest)

Aggregations

SubQueryResults (io.crate.planner.operators.SubQueryResults)36 Row (io.crate.data.Row)34 RowConsumer (io.crate.data.RowConsumer)33 PlannerContext (io.crate.planner.PlannerContext)32 DependencyCarrier (io.crate.planner.DependencyCarrier)31 Plan (io.crate.planner.Plan)31 Row1 (io.crate.data.Row1)30 OneRowActionListener (io.crate.execution.support.OneRowActionListener)30 SymbolEvaluator (io.crate.analyze.SymbolEvaluator)25 Symbol (io.crate.expression.symbol.Symbol)25 Function (java.util.function.Function)25 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)20 NodeContext (io.crate.metadata.NodeContext)19 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)18 Settings (org.elasticsearch.common.settings.Settings)17 List (java.util.List)13 DocTableInfo (io.crate.metadata.doc.DocTableInfo)11 ArrayList (java.util.ArrayList)10 Table (io.crate.sql.tree.Table)9 Lists2 (io.crate.common.collections.Lists2)8