use of io.crate.expression.symbol.Assignments in project crate by crate.
the class UpdateAnalyzerTest method testUpdateWithWrongParameters.
@Test
public void testUpdateWithWrongParameters() throws Exception {
Object[] params = { List.of(new HashMap<String, Object>()), new Map[0], new Long[] { 1L, 2L, 3L } };
AnalyzedUpdateStatement update = analyze("update users set name=?, friends=? where other_id=?");
Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
expectedException.expect(ConversionException.class);
expectedException.expectMessage("Cannot cast value `[{}]` to type `text`");
assignments.bindSources(((DocTableInfo) update.table().tableInfo()), new RowN(params), SubQueryResults.EMPTY);
}
use of io.crate.expression.symbol.Assignments 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.expression.symbol.Assignments 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.expression.symbol.Assignments 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.expression.symbol.Assignments 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