Search in sources :

Example 6 with Doc

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)));
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) SQLExecutor(io.crate.testing.SQLExecutor) AnalyzedUpdateStatement(io.crate.analyze.AnalyzedUpdateStatement) Assignments(io.crate.expression.symbol.Assignments) Doc(io.crate.expression.reference.Doc) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 7 with Doc

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)));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) DocTableInfo(io.crate.metadata.doc.DocTableInfo) SQLExecutor(io.crate.testing.SQLExecutor) AnalyzedUpdateStatement(io.crate.analyze.AnalyzedUpdateStatement) Assignments(io.crate.expression.symbol.Assignments) Doc(io.crate.expression.reference.Doc) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 8 with Doc

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);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Doc(io.crate.expression.reference.Doc) Engine(org.elasticsearch.index.engine.Engine)

Example 9 with Doc

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;
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) HashMap(java.util.HashMap) Reference(io.crate.metadata.Reference) Doc(io.crate.expression.reference.Doc)

Example 10 with Doc

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))));
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) SQLExecutor(io.crate.testing.SQLExecutor) AnalyzedUpdateStatement(io.crate.analyze.AnalyzedUpdateStatement) Assignments(io.crate.expression.symbol.Assignments) Doc(io.crate.expression.reference.Doc) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

Doc (io.crate.expression.reference.Doc)13 Test (org.junit.Test)9 DocTableInfo (io.crate.metadata.doc.DocTableInfo)8 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)8 SQLExecutor (io.crate.testing.SQLExecutor)8 AnalyzedUpdateStatement (io.crate.analyze.AnalyzedUpdateStatement)7 Assignments (io.crate.expression.symbol.Assignments)7 BytesReference (org.elasticsearch.common.bytes.BytesReference)5 Reference (io.crate.metadata.Reference)3 IOException (java.io.IOException)3 Engine (org.elasticsearch.index.engine.Engine)3 UncheckedIOException (java.io.UncheckedIOException)2 QueriedSelectRelation (io.crate.analyze.QueriedSelectRelation)1 DocTableRelation (io.crate.analyze.relations.DocTableRelation)1 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)1 Input (io.crate.data.Input)1 CollectExpression (io.crate.execution.engine.collect.CollectExpression)1 InputFactory (io.crate.expression.InputFactory)1 DocRefResolver (io.crate.expression.reference.DocRefResolver)1 SourceFieldVisitor (io.crate.expression.reference.doc.lucene.SourceFieldVisitor)1