Search in sources :

Example 1 with UpsertById

use of io.crate.planner.node.dml.UpsertById in project crate by crate.

the class InsertPlannerTest method testInsertPlanMultipleValues.

@Test
public void testInsertPlanMultipleValues() throws Exception {
    UpsertById upsertById = e.plan("insert into users (id, name) values (42, 'Deep Thought'), (99, 'Marvin')");
    assertThat(upsertById.insertColumns().length, is(2));
    Reference idRef = upsertById.insertColumns()[0];
    assertThat(idRef.ident().columnIdent().fqn(), is("id"));
    Reference nameRef = upsertById.insertColumns()[1];
    assertThat(nameRef.ident().columnIdent().fqn(), is("name"));
    assertThat(upsertById.items().size(), is(2));
    UpsertById.Item item1 = upsertById.items().get(0);
    assertThat(item1.index(), is("users"));
    assertThat(item1.id(), is("42"));
    assertThat(item1.routing(), is("42"));
    assertThat(item1.insertValues().length, is(2));
    assertThat((Long) item1.insertValues()[0], is(42L));
    assertThat((BytesRef) item1.insertValues()[1], is(new BytesRef("Deep Thought")));
    UpsertById.Item item2 = upsertById.items().get(1);
    assertThat(item2.index(), is("users"));
    assertThat(item2.id(), is("99"));
    assertThat(item2.routing(), is("99"));
    assertThat(item2.insertValues().length, is(2));
    assertThat((Long) item2.insertValues()[0], is(99L));
    assertThat((BytesRef) item2.insertValues()[1], is(new BytesRef("Marvin")));
}
Also used : Reference(io.crate.metadata.Reference) UpsertById(io.crate.planner.node.dml.UpsertById) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 2 with UpsertById

use of io.crate.planner.node.dml.UpsertById in project crate by crate.

the class InsertPlannerTest method testInsertFromValuesWithOnDuplicateKey.

@Test
public void testInsertFromValuesWithOnDuplicateKey() throws Exception {
    UpsertById node = e.plan("insert into users (id, name) values (1, null) on duplicate key update name = values(name)");
    assertThat(node.updateColumns(), is(new String[] { "name" }));
    assertThat(node.insertColumns().length, is(2));
    Reference idRef = node.insertColumns()[0];
    assertThat(idRef.ident().columnIdent().fqn(), is("id"));
    Reference nameRef = node.insertColumns()[1];
    assertThat(nameRef.ident().columnIdent().fqn(), is("name"));
    assertThat(node.items().size(), is(1));
    UpsertById.Item item = node.items().get(0);
    assertThat(item.index(), is("users"));
    assertThat(item.id(), is("1"));
    assertThat(item.routing(), is("1"));
    assertThat(item.insertValues().length, is(2));
    assertThat((Long) item.insertValues()[0], is(1L));
    assertNull(item.insertValues()[1]);
    assertThat(item.updateAssignments().length, is(1));
    assertThat(item.updateAssignments()[0], isLiteral(null, DataTypes.STRING));
}
Also used : Reference(io.crate.metadata.Reference) UpsertById(io.crate.planner.node.dml.UpsertById) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 3 with UpsertById

use of io.crate.planner.node.dml.UpsertById in project crate by crate.

the class InsertPlannerTest method testInsertPlan.

@Test
public void testInsertPlan() throws Exception {
    UpsertById upsertById = e.plan("insert into users (id, name) values (42, 'Deep Thought')");
    assertThat(upsertById.insertColumns().length, is(2));
    Reference idRef = upsertById.insertColumns()[0];
    assertThat(idRef.ident().columnIdent().fqn(), is("id"));
    Reference nameRef = upsertById.insertColumns()[1];
    assertThat(nameRef.ident().columnIdent().fqn(), is("name"));
    assertThat(upsertById.items().size(), is(1));
    UpsertById.Item item = upsertById.items().get(0);
    assertThat(item.index(), is("users"));
    assertThat(item.id(), is("42"));
    assertThat(item.routing(), is("42"));
    assertThat(item.insertValues().length, is(2));
    assertThat((Long) item.insertValues()[0], is(42L));
    assertThat((BytesRef) item.insertValues()[1], is(new BytesRef("Deep Thought")));
}
Also used : Reference(io.crate.metadata.Reference) UpsertById(io.crate.planner.node.dml.UpsertById) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 4 with UpsertById

use of io.crate.planner.node.dml.UpsertById in project crate by crate.

the class Planner method processInsertStatement.

private UpsertById processInsertStatement(InsertFromValuesAnalyzedStatement analysis, Context context) {
    String[] onDuplicateKeyAssignmentsColumns = null;
    if (analysis.onDuplicateKeyAssignmentsColumns().size() > 0) {
        onDuplicateKeyAssignmentsColumns = analysis.onDuplicateKeyAssignmentsColumns().get(0);
    }
    UpsertById upsertById = new UpsertById(context.jobId(), context.nextExecutionPhaseId(), analysis.tableInfo().isPartitioned(), analysis.numBulkResponses(), analysis.bulkIndices(), onDuplicateKeyAssignmentsColumns, analysis.columns().toArray(new Reference[analysis.columns().size()]));
    if (analysis.tableInfo().isPartitioned()) {
        List<String> partitions = analysis.generatePartitions();
        String[] indices = partitions.toArray(new String[partitions.size()]);
        for (int i = 0; i < indices.length; i++) {
            Symbol[] onDuplicateKeyAssignments = null;
            if (analysis.onDuplicateKeyAssignmentsColumns().size() > i) {
                onDuplicateKeyAssignments = analysis.onDuplicateKeyAssignments().get(i);
            }
            upsertById.add(indices[i], analysis.ids().get(i), analysis.routingValues().get(i), onDuplicateKeyAssignments, null, analysis.sourceMaps().get(i));
        }
    } else {
        for (int i = 0; i < analysis.ids().size(); i++) {
            Symbol[] onDuplicateKeyAssignments = null;
            if (analysis.onDuplicateKeyAssignments().size() > i) {
                onDuplicateKeyAssignments = analysis.onDuplicateKeyAssignments().get(i);
            }
            upsertById.add(analysis.tableInfo().ident().indexName(), analysis.ids().get(i), analysis.routingValues().get(i), onDuplicateKeyAssignments, null, analysis.sourceMaps().get(i));
        }
    }
    return upsertById;
}
Also used : Symbol(io.crate.analyze.symbol.Symbol) UpsertById(io.crate.planner.node.dml.UpsertById)

Aggregations

UpsertById (io.crate.planner.node.dml.UpsertById)4 Reference (io.crate.metadata.Reference)3 CrateUnitTest (io.crate.test.integration.CrateUnitTest)3 Test (org.junit.Test)3 BytesRef (org.apache.lucene.util.BytesRef)2 Symbol (io.crate.analyze.symbol.Symbol)1