use of io.crate.analyze.AnalyzedUpdateStatement in project crate by crate.
the class UpdatePlanner method plan.
public static Plan plan(AnalyzedUpdateStatement update, PlannerContext plannerCtx, SubqueryPlanner subqueryPlanner) {
if (update.outputs() != null && !plannerCtx.clusterState().getNodes().getMinNodeVersion().onOrAfter(Version.V_4_2_0)) {
throw new UnsupportedFeatureException(RETURNING_VERSION_ERROR_MSG);
}
AbstractTableRelation<?> table = update.table();
Plan plan;
if (table instanceof DocTableRelation) {
DocTableRelation docTable = (DocTableRelation) table;
plan = plan(docTable, update.assignmentByTargetCol(), update.query(), plannerCtx, update.outputs());
} else {
plan = new Update((plannerContext, params, subQueryValues) -> sysUpdate(plannerContext, (TableRelation) table, update.assignmentByTargetCol(), update.query(), params, subQueryValues, update.outputs()));
}
Map<LogicalPlan, SelectSymbol> subQueries = subqueryPlanner.planSubQueries(update);
return MultiPhasePlan.createIfNeeded(plan, subQueries);
}
use of io.crate.analyze.AnalyzedUpdateStatement in project crate by crate.
the class ReturnValueGenTest method setupStatement.
private void setupStatement(String stmt) throws IOException {
SQLExecutor executor = SQLExecutor.builder(clusterService).addTable(CREATE_TEST_TABLE).build();
AnalyzedUpdateStatement update = executor.analyze(stmt);
tableInfo = (DocTableInfo) update.table().tableInfo();
returnValueGen = new ReturnValueGen(txnCtx, executor.nodeCtx, tableInfo, update.outputs() == null ? null : update.outputs().toArray(new Symbol[0]));
}
use of io.crate.analyze.AnalyzedUpdateStatement in project crate by crate.
the class UpdateSourceGenTest method test_update_child_of_object_column_that_is_null_implicitly_creates_the_object.
@Test
public void test_update_child_of_object_column_that_is_null_implicitly_creates_the_object() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table t (obj object as (x int))").build();
AnalyzedUpdateStatement update = e.analyze("update t set obj['x'] = 10");
Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
DocTableInfo table = (DocTableInfo) update.table().tableInfo();
UpdateSourceGen updateSourceGen = new UpdateSourceGen(txnCtx, e.nodeCtx, table, assignments.targetNames());
Map<String, Object> updatedSource = updateSourceGen.generateSource(new Doc(1, table.concreteIndices()[0], "1", 1, 1, 1, Collections.singletonMap("obj", null), () -> "{\"obj\": null}"), assignments.sources(), new Object[0]);
assertThat(updatedSource, is(Map.of("obj", Map.of("x", 10))));
}
use of io.crate.analyze.AnalyzedUpdateStatement in project crate by crate.
the class UpdateSourceGenTest method testNestedGeneratedColumnRaiseErrorIfGivenByUserDoesNotMatch.
@Test
public void testNestedGeneratedColumnRaiseErrorIfGivenByUserDoesNotMatch() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table t (x int, obj object as (y as 'foo'))").build();
AnalyzedUpdateStatement update = e.analyze("update t set x = 4, obj = {y='bar'}");
Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
DocTableInfo table = (DocTableInfo) update.table().tableInfo();
UpdateSourceGen updateSourceGen = new UpdateSourceGen(txnCtx, e.nodeCtx, table, assignments.targetNames());
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Given value bar for generated column obj['y'] does not match calculation 'foo' = foo");
updateSourceGen.generateSource(new Doc(1, table.concreteIndices()[0], "1", 1, 1, 1, emptyMap(), () -> "{}"), assignments.sources(), new Object[0]);
}
use of io.crate.analyze.AnalyzedUpdateStatement in project crate by crate.
the class UpdateSourceGenTest method testNestedGeneratedColumnIsGenerated.
@Test
public void testNestedGeneratedColumnIsGenerated() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table t (x int, obj object as (y as x + 1))").build();
AnalyzedUpdateStatement update = e.analyze("update t set x = 4");
Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
DocTableInfo table = (DocTableInfo) update.table().tableInfo();
UpdateSourceGen updateSourceGen = new UpdateSourceGen(txnCtx, e.nodeCtx, table, assignments.targetNames());
Map<String, Object> updatedSource = updateSourceGen.generateSource(new Doc(1, table.concreteIndices()[0], "1", 1, 1, 1, emptyMap(), () -> "{}"), assignments.sources(), new Object[0]);
assertThat(updatedSource, is(Map.of("obj", Map.of("y", 5), "x", 4)));
}
Aggregations