Search in sources :

Example 1 with UpdateProjection

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

the class UpdatePlanner method updateByQuery.

private static ExecutionPlan updateByQuery(PlannerContext plannerCtx, DocTableRelation table, Map<Reference, Symbol> assignmentByTargetCol, WhereClauseOptimizer.DetailedQuery detailedQuery, Row params, SubQueryResults subQueryResults, @Nullable List<Symbol> returnValues) {
    DocTableInfo tableInfo = table.tableInfo();
    Reference idReference = requireNonNull(tableInfo.getReference(DocSysColumns.ID), "Table must have a _id column");
    Assignments assignments = Assignments.convert(assignmentByTargetCol, plannerCtx.nodeContext());
    Symbol[] assignmentSources = assignments.bindSources(tableInfo, params, subQueryResults);
    Symbol[] outputSymbols;
    if (returnValues == null) {
        // When there are no return values, set the output to a long representing the count of updated rows
        outputSymbols = new Symbol[] { new InputColumn(0, DataTypes.LONG) };
    } else {
        outputSymbols = new Symbol[returnValues.size()];
        for (int i = 0; i < returnValues.size(); i++) {
            outputSymbols[i] = new InputColumn(i, returnValues.get(i).valueType());
        }
    }
    UpdateProjection updateProjection = new UpdateProjection(new InputColumn(0, idReference.valueType()), assignments.targetNames(), assignmentSources, outputSymbols, returnValues == null ? null : returnValues.toArray(new Symbol[0]), null);
    WhereClause where = detailedQuery.toBoundWhereClause(tableInfo, params, subQueryResults, plannerCtx.transactionContext(), plannerCtx.nodeContext());
    if (where.hasVersions()) {
        throw VersioningValidationException.versionInvalidUsage();
    } else if (where.hasSeqNoAndPrimaryTerm()) {
        throw VersioningValidationException.seqNoAndPrimaryTermUsage();
    }
    if (returnValues == null) {
        return createCollectAndMerge(plannerCtx, tableInfo, idReference, updateProjection, where, 1, 1, MergeCountProjection.INSTANCE);
    } else {
        return createCollectAndMerge(plannerCtx, tableInfo, idReference, updateProjection, where, updateProjection.outputs().size(), -1);
    }
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) Reference(io.crate.metadata.Reference) SelectSymbol(io.crate.expression.symbol.SelectSymbol) Symbol(io.crate.expression.symbol.Symbol) InputColumn(io.crate.expression.symbol.InputColumn) Assignments(io.crate.expression.symbol.Assignments) WhereClause(io.crate.analyze.WhereClause) UpdateProjection(io.crate.execution.dsl.projection.UpdateProjection) SysUpdateProjection(io.crate.execution.dsl.projection.SysUpdateProjection)

Example 2 with UpdateProjection

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

the class UpdatePlannerTest method testUpdateByQueryPlan.

@Test
public void testUpdateByQueryPlan() throws Exception {
    UpdatePlanner.Update plan = e.plan("update users set name='Vogon lyric fan'");
    Merge merge = (Merge) plan.createExecutionPlan.create(e.getPlannerContext(clusterService.state()), Row.EMPTY, SubQueryResults.EMPTY);
    Collect collect = (Collect) merge.subPlan();
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
    assertThat(collectPhase.where(), isSQL("true"));
    assertThat(collectPhase.projections().size(), is(1));
    assertThat(collectPhase.projections().get(0), instanceOf(UpdateProjection.class));
    assertThat(collectPhase.toCollect().size(), is(1));
    assertThat(collectPhase.toCollect().get(0), instanceOf(Reference.class));
    assertThat(((Reference) collectPhase.toCollect().get(0)).column().fqn(), is("_id"));
    UpdateProjection updateProjection = (UpdateProjection) collectPhase.projections().get(0);
    assertThat(updateProjection.uidSymbol(), instanceOf(InputColumn.class));
    assertThat(updateProjection.assignmentsColumns()[0], is("name"));
    Symbol symbol = updateProjection.assignments()[0];
    assertThat(symbol, isLiteral("Vogon lyric fan", DataTypes.STRING));
    MergePhase mergePhase = merge.mergePhase();
    assertThat(mergePhase.projections().size(), is(1));
    assertThat(mergePhase.projections().get(0), instanceOf(MergeCountProjection.class));
    assertThat(mergePhase.outputTypes().size(), is(1));
}
Also used : MergeCountProjection(io.crate.execution.dsl.projection.MergeCountProjection) MergePhase(io.crate.execution.dsl.phases.MergePhase) Collect(io.crate.planner.node.dql.Collect) Reference(io.crate.metadata.Reference) SymbolMatchers.isReference(io.crate.testing.SymbolMatchers.isReference) InputColumn(io.crate.expression.symbol.InputColumn) SelectSymbol(io.crate.expression.symbol.SelectSymbol) Symbol(io.crate.expression.symbol.Symbol) UpdatePlanner(io.crate.planner.consumer.UpdatePlanner) UpdateProjection(io.crate.execution.dsl.projection.UpdateProjection) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Aggregations

UpdateProjection (io.crate.execution.dsl.projection.UpdateProjection)2 InputColumn (io.crate.expression.symbol.InputColumn)2 SelectSymbol (io.crate.expression.symbol.SelectSymbol)2 Symbol (io.crate.expression.symbol.Symbol)2 Reference (io.crate.metadata.Reference)2 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)1 WhereClause (io.crate.analyze.WhereClause)1 MergePhase (io.crate.execution.dsl.phases.MergePhase)1 RoutedCollectPhase (io.crate.execution.dsl.phases.RoutedCollectPhase)1 MergeCountProjection (io.crate.execution.dsl.projection.MergeCountProjection)1 SysUpdateProjection (io.crate.execution.dsl.projection.SysUpdateProjection)1 Assignments (io.crate.expression.symbol.Assignments)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1 UpdatePlanner (io.crate.planner.consumer.UpdatePlanner)1 Collect (io.crate.planner.node.dql.Collect)1 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)1 SymbolMatchers.isReference (io.crate.testing.SymbolMatchers.isReference)1 Test (org.junit.Test)1