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);
}
}
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));
}
Aggregations