Search in sources :

Example 1 with SourceIndexWriterReturnSummaryProjection

use of io.crate.execution.dsl.projection.SourceIndexWriterReturnSummaryProjection in project crate by crate.

the class ProjectionToProjectorVisitor method visitSourceIndexWriterProjection.

@Override
public Projector visitSourceIndexWriterProjection(SourceIndexWriterProjection projection, Context context) {
    InputFactory.Context<CollectExpression<Row, ?>> ctx = inputFactory.ctxForInputColumns(context.txnCtx);
    List<Input<?>> partitionedByInputs = new ArrayList<>(projection.partitionedBySymbols().size());
    for (Symbol partitionedBySymbol : projection.partitionedBySymbols()) {
        partitionedByInputs.add(ctx.add(partitionedBySymbol));
    }
    Input<?> sourceInput = ctx.add(projection.rawSource());
    Supplier<String> indexNameResolver = IndexNameResolver.create(projection.tableIdent(), projection.partitionIdent(), partitionedByInputs);
    ClusterState state = clusterService.state();
    Settings tableSettings = TableSettingsResolver.get(state.getMetadata(), projection.tableIdent(), !projection.partitionedBySymbols().isEmpty());
    int targetTableNumShards = IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.get(tableSettings);
    int targetTableNumReplicas = NumberOfReplicas.fromSettings(tableSettings, state.getNodes().getSize());
    UpsertResultContext upsertResultContext;
    if (projection instanceof SourceIndexWriterReturnSummaryProjection) {
        upsertResultContext = UpsertResultContext.forReturnSummary(context.txnCtx, (SourceIndexWriterReturnSummaryProjection) projection, clusterService.localNode(), inputFactory);
    } else {
        upsertResultContext = UpsertResultContext.forRowCount();
    }
    return new IndexWriterProjector(clusterService, nodeJobsCounter, circuitBreakerService.getBreaker(HierarchyCircuitBreakerService.QUERY), context.ramAccounting, threadPool.scheduler(), threadPool.executor(ThreadPool.Names.SEARCH), context.txnCtx, nodeCtx, state.metadata().settings(), targetTableNumShards, targetTableNumReplicas, transportActionProvider.transportBulkCreateIndicesAction(), transportActionProvider.transportShardUpsertAction()::execute, indexNameResolver, projection.rawSourceReference(), projection.primaryKeys(), projection.ids(), projection.clusteredBy(), projection.clusteredByIdent(), sourceInput, ctx.expressions(), projection.bulkActions(), projection.includes(), projection.excludes(), projection.autoCreateIndices(), projection.overwriteDuplicates(), context.jobId, upsertResultContext, projection.failFast());
}
Also used : InputFactory(io.crate.expression.InputFactory) ClusterState(org.elasticsearch.cluster.ClusterState) UpsertResultContext(io.crate.execution.engine.indexing.UpsertResultContext) SourceIndexWriterReturnSummaryProjection(io.crate.execution.dsl.projection.SourceIndexWriterReturnSummaryProjection) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList) NestableCollectExpression(io.crate.execution.engine.collect.NestableCollectExpression) CollectExpression(io.crate.execution.engine.collect.CollectExpression) IndexWriterProjector(io.crate.execution.engine.indexing.IndexWriterProjector) ColumnIndexWriterProjector(io.crate.execution.engine.indexing.ColumnIndexWriterProjector) Input(io.crate.data.Input) Settings(org.elasticsearch.common.settings.Settings)

Example 2 with SourceIndexWriterReturnSummaryProjection

use of io.crate.execution.dsl.projection.SourceIndexWriterReturnSummaryProjection in project crate by crate.

the class CopyFromPlan method planCopyFromExecution.

public static ExecutionPlan planCopyFromExecution(AnalyzedCopyFrom copyFrom, DiscoveryNodes allNodes, PlannerContext context, Row params, SubQueryResults subQueryResults) {
    var boundedCopyFrom = bind(copyFrom, context.transactionContext(), context.nodeContext(), params, subQueryResults);
    /*
         * Create a plan that reads json-objects-lines from a file
         * and then executes upsert requests to index the data
         */
    DocTableInfo table = boundedCopyFrom.tableInfo();
    String partitionIdent = boundedCopyFrom.partitionIdent();
    List<String> partitionedByNames = Collections.emptyList();
    List<String> partitionValues = Collections.emptyList();
    if (partitionIdent == null) {
        if (table.isPartitioned()) {
            partitionedByNames = Lists2.map(table.partitionedBy(), ColumnIdent::fqn);
        }
    } else {
        assert table.isPartitioned() : "table must be partitioned if partitionIdent is set";
        // partitionIdent is present -> possible to index raw source into concrete es index
        partitionValues = PartitionName.decodeIdent(partitionIdent);
    }
    // need to exclude _id columns; they're auto generated and won't be available in the files being imported
    ColumnIdent clusteredBy = table.clusteredBy();
    if (DocSysColumns.ID.equals(clusteredBy)) {
        clusteredBy = null;
    }
    List<Reference> primaryKeyRefs = table.primaryKey().stream().filter(r -> !r.equals(DocSysColumns.ID)).map(table::getReference).collect(Collectors.toList());
    List<Symbol> toCollect = getSymbolsRequiredForShardIdCalc(primaryKeyRefs, table.partitionedByColumns(), clusteredBy == null ? null : table.getReference(clusteredBy));
    Reference rawOrDoc = rawOrDoc(table, partitionIdent);
    final int rawOrDocIdx = toCollect.size();
    toCollect.add(rawOrDoc);
    String[] excludes = partitionedByNames.size() > 0 ? partitionedByNames.toArray(new String[0]) : null;
    InputColumns.SourceSymbols sourceSymbols = new InputColumns.SourceSymbols(toCollect);
    Symbol clusteredByInputCol = null;
    if (clusteredBy != null) {
        clusteredByInputCol = InputColumns.create(table.getReference(clusteredBy), sourceSymbols);
    }
    SourceIndexWriterProjection sourceIndexWriterProjection;
    List<? extends Symbol> projectionOutputs = AbstractIndexWriterProjection.OUTPUTS;
    boolean returnSummary = copyFrom instanceof AnalyzedCopyFromReturnSummary;
    boolean failFast = boundedCopyFrom.settings().getAsBoolean("fail_fast", false);
    if (returnSummary || failFast) {
        final InputColumn sourceUriSymbol = new InputColumn(toCollect.size(), DataTypes.STRING);
        toCollect.add(SourceUriExpression.getReferenceForRelation(table.ident()));
        final InputColumn sourceUriFailureSymbol = new InputColumn(toCollect.size(), DataTypes.STRING);
        toCollect.add(SourceUriFailureExpression.getReferenceForRelation(table.ident()));
        final InputColumn lineNumberSymbol = new InputColumn(toCollect.size(), DataTypes.LONG);
        toCollect.add(SourceLineNumberExpression.getReferenceForRelation(table.ident()));
        if (returnSummary) {
            List<? extends Symbol> fields = ((AnalyzedCopyFromReturnSummary) copyFrom).outputs();
            projectionOutputs = InputColumns.create(fields, new InputColumns.SourceSymbols(fields));
        }
        sourceIndexWriterProjection = new SourceIndexWriterReturnSummaryProjection(table.ident(), partitionIdent, table.getReference(DocSysColumns.RAW), new InputColumn(rawOrDocIdx, rawOrDoc.valueType()), table.primaryKey(), InputColumns.create(table.partitionedByColumns(), sourceSymbols), clusteredBy, boundedCopyFrom.settings(), null, excludes, InputColumns.create(primaryKeyRefs, sourceSymbols), clusteredByInputCol, projectionOutputs, // autoCreateIndices
        table.isPartitioned(), sourceUriSymbol, sourceUriFailureSymbol, lineNumberSymbol);
    } else {
        sourceIndexWriterProjection = new SourceIndexWriterProjection(table.ident(), partitionIdent, table.getReference(DocSysColumns.RAW), new InputColumn(rawOrDocIdx, rawOrDoc.valueType()), table.primaryKey(), InputColumns.create(table.partitionedByColumns(), sourceSymbols), clusteredBy, boundedCopyFrom.settings(), null, excludes, InputColumns.create(primaryKeyRefs, sourceSymbols), clusteredByInputCol, projectionOutputs, // autoCreateIndices
        table.isPartitioned());
    }
    // the partitionedBy-inputColumns created for the projection are still valid because the positions are not changed
    if (partitionValues != null) {
        rewriteToCollectToUsePartitionValues(table.partitionedByColumns(), partitionValues, toCollect);
    }
    FileUriCollectPhase collectPhase = new FileUriCollectPhase(context.jobId(), context.nextExecutionPhaseId(), "copyFrom", getExecutionNodes(allNodes, boundedCopyFrom.settings().getAsInt("num_readers", allNodes.getSize()), boundedCopyFrom.nodePredicate()), boundedCopyFrom.uri(), toCollect, Collections.emptyList(), boundedCopyFrom.settings().get("compression", null), boundedCopyFrom.settings().getAsBoolean("shared", null), CopyFromParserProperties.of(boundedCopyFrom.settings()), boundedCopyFrom.inputFormat(), boundedCopyFrom.settings());
    Collect collect = new Collect(collectPhase, TopN.NO_LIMIT, 0, 1, -1, null);
    // add the projection to the plan to ensure that the outputs are correctly set to the projection outputs
    collect.addProjection(sourceIndexWriterProjection);
    List<Projection> handlerProjections;
    if (returnSummary) {
        handlerProjections = Collections.emptyList();
    } else {
        handlerProjections = List.of(MergeCountProjection.INSTANCE);
    }
    return Merge.ensureOnHandler(collect, context, handlerProjections);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) InputColumns(io.crate.execution.dsl.projection.builder.InputColumns) SourceIndexWriterReturnSummaryProjection(io.crate.execution.dsl.projection.SourceIndexWriterReturnSummaryProjection) Collect(io.crate.planner.node.dql.Collect) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) Symbol(io.crate.expression.symbol.Symbol) SourceIndexWriterProjection(io.crate.execution.dsl.projection.SourceIndexWriterProjection) Projection(io.crate.execution.dsl.projection.Projection) SourceIndexWriterProjection(io.crate.execution.dsl.projection.SourceIndexWriterProjection) MergeCountProjection(io.crate.execution.dsl.projection.MergeCountProjection) AbstractIndexWriterProjection(io.crate.execution.dsl.projection.AbstractIndexWriterProjection) SourceIndexWriterReturnSummaryProjection(io.crate.execution.dsl.projection.SourceIndexWriterReturnSummaryProjection) FileUriCollectPhase(io.crate.execution.dsl.phases.FileUriCollectPhase) ColumnIdent(io.crate.metadata.ColumnIdent) InputColumn(io.crate.expression.symbol.InputColumn) AnalyzedCopyFromReturnSummary(io.crate.analyze.AnalyzedCopyFromReturnSummary)

Aggregations

SourceIndexWriterReturnSummaryProjection (io.crate.execution.dsl.projection.SourceIndexWriterReturnSummaryProjection)2 Symbol (io.crate.expression.symbol.Symbol)2 AnalyzedCopyFromReturnSummary (io.crate.analyze.AnalyzedCopyFromReturnSummary)1 Input (io.crate.data.Input)1 FileUriCollectPhase (io.crate.execution.dsl.phases.FileUriCollectPhase)1 AbstractIndexWriterProjection (io.crate.execution.dsl.projection.AbstractIndexWriterProjection)1 MergeCountProjection (io.crate.execution.dsl.projection.MergeCountProjection)1 Projection (io.crate.execution.dsl.projection.Projection)1 SourceIndexWriterProjection (io.crate.execution.dsl.projection.SourceIndexWriterProjection)1 InputColumns (io.crate.execution.dsl.projection.builder.InputColumns)1 CollectExpression (io.crate.execution.engine.collect.CollectExpression)1 NestableCollectExpression (io.crate.execution.engine.collect.NestableCollectExpression)1 ColumnIndexWriterProjector (io.crate.execution.engine.indexing.ColumnIndexWriterProjector)1 IndexWriterProjector (io.crate.execution.engine.indexing.IndexWriterProjector)1 UpsertResultContext (io.crate.execution.engine.indexing.UpsertResultContext)1 InputFactory (io.crate.expression.InputFactory)1 InputColumn (io.crate.expression.symbol.InputColumn)1 ColumnIdent (io.crate.metadata.ColumnIdent)1 GeneratedReference (io.crate.metadata.GeneratedReference)1 Reference (io.crate.metadata.Reference)1