use of io.crate.expression.reference.Doc in project crate by crate.
the class UpdateSourceGenTest method testGeneratedColumnUsingFunctionDependingOnActiveTransaction.
@Test
public void testGeneratedColumnUsingFunctionDependingOnActiveTransaction() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table t (x int, gen as current_schema)").build();
AnalyzedUpdateStatement update = e.analyze("update t set x = 1");
Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
DocTableInfo table = (DocTableInfo) update.table().tableInfo();
UpdateSourceGen sourceGen = new UpdateSourceGen(TransactionContext.of(DUMMY_SESSION_INFO), e.nodeCtx, table, assignments.targetNames());
Map<String, Object> source = sourceGen.generateSource(new Doc(1, table.concreteIndices()[0], "1", 1, 1, 1, emptyMap(), () -> "{}"), assignments.sources(), new Object[0]);
assertThat(source, is(Map.of("gen", "dummySchema", "x", 1)));
}
use of io.crate.expression.reference.Doc in project crate by crate.
the class UpdateSourceGenTest method testSourceGenerationWithAssignmentUsingDocumentPrimaryKey.
@Test
public void testSourceGenerationWithAssignmentUsingDocumentPrimaryKey() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table t (y int)").build();
AnalyzedUpdateStatement update = e.analyze("update t set y = _id::integer * 2");
Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
DocTableInfo table = (DocTableInfo) update.table().tableInfo();
UpdateSourceGen updateSourceGen = new UpdateSourceGen(txnCtx, e.nodeCtx, table, assignments.targetNames());
BytesReference source = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("y", 100).endObject());
Map<String, Object> updatedSource = updateSourceGen.generateSource(new Doc(1, table.concreteIndices()[0], "4", 1, 1, 1, emptyMap(), source::utf8ToString), assignments.sources(), new Object[0]);
assertThat(updatedSource, is(Map.of("y", 8)));
}
use of io.crate.expression.reference.Doc in project crate by crate.
the class TransportShardUpsertAction method update.
protected IndexItemResponse update(ShardUpsertRequest.Item item, IndexShard indexShard, boolean isRetry, @Nullable ReturnValueGen returnGen, UpdateSourceGen updateSourceGen) throws Exception {
assert updateSourceGen != null : "UpdateSourceGen must not be null";
Doc fetchedDoc = getDocument(indexShard, item.id(), item.version(), item.seqNo(), item.primaryTerm());
Map<String, Object> source = updateSourceGen.generateSource(fetchedDoc, item.updateAssignments(), item.insertValues());
BytesReference rawSource = BytesReference.bytes(XContentFactory.jsonBuilder().map(source, SOURCE_WRITERS));
item.source(rawSource);
long seqNo = item.seqNo();
long primaryTerm = item.primaryTerm();
long version = Versions.MATCH_ANY;
Engine.IndexResult indexResult = index(item, indexShard, isRetry, seqNo, primaryTerm, version);
Object[] returnvalues = null;
if (returnGen != null) {
returnvalues = returnGen.generateReturnValues(new Doc(fetchedDoc.docId(), fetchedDoc.getIndex(), fetchedDoc.getId(), indexResult.getVersion(), indexResult.getSeqNo(), indexResult.getTerm(), source, rawSource::utf8ToString));
}
return new IndexItemResponse(indexResult.getTranslogLocation(), returnvalues);
}
use of io.crate.expression.reference.Doc in project crate by crate.
the class UpdateSourceGen method generateSource.
Map<String, Object> generateSource(Doc result, Symbol[] updateAssignments, Object[] insertValues) {
/* We require a new HashMap because all evaluations of the updateAssignments need to be based on the
* values *before* the update. For example:
*
* source: x=5
* SET x = 10, y = x + 5
*
* Must result in y = 10, not 15
*/
Values values = new Values(result, insertValues);
HashMap<String, Object> updatedSource = new HashMap<>(result.getSource());
Doc updatedDoc = result.withUpdatedSource(updatedSource);
for (int i = 0; i < updateColumns.size(); i++) {
Reference ref = updateColumns.get(i);
Object value = updateAssignments[i].accept(eval, values).value();
ColumnIdent column = ref.column();
Maps.mergeInto(updatedSource, column.name(), column.path(), value);
}
generatedColumns.setNextRow(updatedDoc);
generatedColumns.validateValues(updatedSource);
injectGeneratedColumns(updatedSource);
checks.validate(updatedDoc);
return updatedSource;
}
use of io.crate.expression.reference.Doc 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))));
}
Aggregations