use of io.crate.expression.reference.Doc in project crate by crate.
the class PKLookupOperation method lookupDoc.
@Nullable
public static Doc lookupDoc(IndexShard shard, String id, long version, VersionType versionType, long seqNo, long primaryTerm) {
Term uidTerm = new Term(IdFieldMapper.NAME, Uid.encodeId(id));
Engine.Get get = new Engine.Get(id, uidTerm).version(version).versionType(versionType).setIfSeqNo(seqNo).setIfPrimaryTerm(primaryTerm);
try (Engine.GetResult getResult = shard.get(get)) {
var docIdAndVersion = getResult.docIdAndVersion();
if (docIdAndVersion == null) {
return null;
}
SourceFieldVisitor visitor = new SourceFieldVisitor();
try {
docIdAndVersion.reader.document(docIdAndVersion.docId, visitor);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return new Doc(docIdAndVersion.docId, shard.shardId().getIndexName(), id, docIdAndVersion.version, docIdAndVersion.seqNo, docIdAndVersion.primaryTerm, XContentHelper.toMap(visitor.source(), XContentType.JSON), () -> visitor.source().utf8ToString());
}
}
use of io.crate.expression.reference.Doc in project crate by crate.
the class TransportShardUpsertAction method insert.
@VisibleForTesting
protected IndexItemResponse insert(ShardUpsertRequest request, ShardUpsertRequest.Item item, IndexShard indexShard, boolean isRetry, @Nullable ReturnValueGen returnGen, InsertSourceGen insertSourceGen) throws Exception {
assert insertSourceGen != null : "InsertSourceGen must not be null";
BytesReference rawSource;
Map<String, Object> source = null;
try {
// the rawSource
if (insertSourceGen instanceof FromRawInsertSource) {
rawSource = insertSourceGen.generateSourceAndCheckConstraintsAsBytesReference(item.insertValues());
} else {
source = insertSourceGen.generateSourceAndCheckConstraints(item.insertValues());
rawSource = BytesReference.bytes(XContentFactory.jsonBuilder().map(source, SOURCE_WRITERS));
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
item.source(rawSource);
long version = request.duplicateKeyAction() == DuplicateKeyAction.OVERWRITE ? Versions.MATCH_ANY : Versions.MATCH_DELETED;
long seqNo = SequenceNumbers.UNASSIGNED_SEQ_NO;
long primaryTerm = SequenceNumbers.UNASSIGNED_PRIMARY_TERM;
Engine.IndexResult indexResult = index(item, indexShard, isRetry, seqNo, primaryTerm, version);
Object[] returnvalues = null;
if (returnGen != null) {
// when return values are requested
if (source == null) {
source = JsonXContent.JSON_XCONTENT.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.toBytes(rawSource)).map();
}
returnvalues = returnGen.generateReturnValues(// we want to avoid. The docId is anyway just valid with the lifetime of a searcher and can change afterwards.
new Doc(-1, indexShard.shardId().getIndexName(), item.id(), 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 GeneratedColumnsTest method testSubscriptExpressionThatReturnsAnArray.
@Test
public void testSubscriptExpressionThatReturnsAnArray() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table t (obj object as (arr array(integer)), arr as obj['arr'])").build();
QueriedSelectRelation query = e.analyze("select obj, arr from t");
DocTableInfo table = ((DocTableRelation) query.from().get(0)).tableInfo();
GeneratedColumns<Doc> generatedColumns = new GeneratedColumns<>(new InputFactory(e.nodeCtx), CoordinatorTxnCtx.systemTransactionContext(), GeneratedColumns.Validation.NONE, new DocRefResolver(Collections.emptyList()), Collections.emptyList(), table.generatedColumns());
BytesReference bytes = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startObject("obj").startArray("arr").value(10).value(20).endArray().endObject().endObject());
generatedColumns.setNextRow(new Doc(1, table.concreteIndices()[0], "1", 1, 1, 1, XContentHelper.convertToMap(bytes, false, XContentType.JSON).v2(), bytes::utf8ToString));
Map.Entry<Reference, Input<?>> generatedColumn = generatedColumns.generatedToInject().iterator().next();
assertThat((List<Object>) generatedColumn.getValue().value(), contains(10, 20));
}
use of io.crate.expression.reference.Doc in project crate by crate.
the class UpdateSourceGenTest method testSetXBasedOnXAndPartitionedColumn.
@Test
public void testSetXBasedOnXAndPartitionedColumn() throws Exception {
SQLExecutor e = SQLExecutor.builder(clusterService).addPartitionedTable("create table t (x int, p int) partitioned by (p)", new PartitionName(new RelationName("doc", "t"), Collections.singletonList("1")).asIndexName()).build();
AnalyzedUpdateStatement update = e.analyze("update t set x = x + p");
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> source = singletonMap("x", 1);
Map<String, Object> updatedSource = updateSourceGen.generateSource(new Doc(1, table.concreteIndices()[0], "1", 1, 1, 1, source, () -> {
try {
return Strings.toString(XContentFactory.jsonBuilder().map(source));
} catch (IOException e1) {
throw new RuntimeException(e1);
}
}), assignments.sources(), new Object[0]);
assertThat(updatedSource, is(Map.of("x", 2)));
}
use of io.crate.expression.reference.Doc in project crate by crate.
the class UpdateSourceGenTest method testNestedGeneratedColumnIsGeneratedValidateValueIfGivenByUser.
@Test
public void testNestedGeneratedColumnIsGeneratedValidateValueIfGivenByUser() 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='foo'}");
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", "foo"), "x", 4)));
}
Aggregations