Search in sources :

Example 1 with UpdateProjection

use of io.crate.planner.projection.UpdateProjection in project crate by crate.

the class UpdateConsumer method upsertByQuery.

private static Plan upsertByQuery(UpdateAnalyzedStatement.NestedAnalyzedStatement nestedAnalysis, Planner.Context plannerContext, DocTableInfo tableInfo, WhereClause whereClause) {
    Symbol versionSymbol = null;
    if (whereClause.hasVersions()) {
        versionSymbol = VersionRewriter.get(whereClause.query());
        whereClause = new WhereClause(whereClause.query(), whereClause.docKeys().orElse(null), whereClause.partitions());
    }
    if (!whereClause.noMatch() || !(tableInfo.isPartitioned() && whereClause.partitions().isEmpty())) {
        // for updates, we always need to collect the `_id`
        Reference idReference = tableInfo.getReference(DocSysColumns.ID);
        Tuple<String[], Symbol[]> assignments = Assignments.convert(nestedAnalysis.assignments());
        Long version = null;
        if (versionSymbol != null) {
            version = ValueSymbolVisitor.LONG.process(versionSymbol);
        }
        UpdateProjection updateProjection = new UpdateProjection(new InputColumn(0, DataTypes.STRING), assignments.v1(), assignments.v2(), version);
        Routing routing = plannerContext.allocateRouting(tableInfo, whereClause, Preference.PRIMARY.type());
        return createPlan(plannerContext, routing, tableInfo, idReference, updateProjection, whereClause);
    } else {
        return null;
    }
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) Reference(io.crate.metadata.Reference) InputColumn(io.crate.analyze.symbol.InputColumn) WhereClause(io.crate.analyze.WhereClause) Routing(io.crate.metadata.Routing) SysUpdateProjection(io.crate.planner.projection.SysUpdateProjection) UpdateProjection(io.crate.planner.projection.UpdateProjection)

Example 2 with UpdateProjection

use of io.crate.planner.projection.UpdateProjection in project crate by crate.

the class UpdatePlannerTest method testUpdateByQueryPlan.

@Test
public void testUpdateByQueryPlan() throws Exception {
    Upsert plan = e.plan("update users set name='Vogon lyric fan'");
    assertThat(plan.nodes().size(), is(1));
    Merge merge = (Merge) plan.nodes().get(0);
    Collect collect = (Collect) merge.subPlan();
    RoutedCollectPhase collectPhase = ((RoutedCollectPhase) collect.collectPhase());
    assertThat(collectPhase.routing(), is(TableDefinitions.SHARD_ROUTING));
    assertFalse(collectPhase.whereClause().noMatch());
    assertFalse(collectPhase.whereClause().hasQuery());
    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)).ident().columnIdent().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.planner.projection.MergeCountProjection) Upsert(io.crate.planner.node.dml.Upsert) MergePhase(io.crate.planner.node.dql.MergePhase) Collect(io.crate.planner.node.dql.Collect) Reference(io.crate.metadata.Reference) InputColumn(io.crate.analyze.symbol.InputColumn) Symbol(io.crate.analyze.symbol.Symbol) UpdateProjection(io.crate.planner.projection.UpdateProjection) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Aggregations

InputColumn (io.crate.analyze.symbol.InputColumn)2 Symbol (io.crate.analyze.symbol.Symbol)2 Reference (io.crate.metadata.Reference)2 UpdateProjection (io.crate.planner.projection.UpdateProjection)2 WhereClause (io.crate.analyze.WhereClause)1 Routing (io.crate.metadata.Routing)1 Upsert (io.crate.planner.node.dml.Upsert)1 Collect (io.crate.planner.node.dql.Collect)1 MergePhase (io.crate.planner.node.dql.MergePhase)1 RoutedCollectPhase (io.crate.planner.node.dql.RoutedCollectPhase)1 MergeCountProjection (io.crate.planner.projection.MergeCountProjection)1 SysUpdateProjection (io.crate.planner.projection.SysUpdateProjection)1 CrateUnitTest (io.crate.test.integration.CrateUnitTest)1 Test (org.junit.Test)1