Search in sources :

Example 36 with DocTableInfo

use of io.crate.metadata.doc.DocTableInfo 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 37 with DocTableInfo

use of io.crate.metadata.doc.DocTableInfo in project crate by crate.

the class GeneratedColsFromRawInsertSourceTest method test_generate_value_text_type_with_length_exceeding_whitespaces_trimmed.

@Test
public void test_generate_value_text_type_with_length_exceeding_whitespaces_trimmed() throws Exception {
    var e = SQLExecutor.builder(clusterService).addTable("create table t (x varchar(2) as 'ab  ')").build();
    DocTableInfo t = e.resolveTableInfo("t");
    var insertSource = new GeneratedColsFromRawInsertSource(txnCtx, e.nodeCtx, t.generatedColumns(), t.defaultExpressionColumns());
    Map<String, Object> map = insertSource.generateSourceAndCheckConstraints(new Object[] { "{}" });
    assertThat(Maps.getByPath(map, "x"), is("ab"));
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 38 with DocTableInfo

use of io.crate.metadata.doc.DocTableInfo in project crate by crate.

the class GeneratedColsFromRawInsertSourceTest method test_generate_value_that_exceeds_text_type_with_length_throws_exception.

@Test
public void test_generate_value_that_exceeds_text_type_with_length_throws_exception() throws Exception {
    var e = SQLExecutor.builder(clusterService).addTable("create table t (x varchar(2) as 'abc')").build();
    DocTableInfo t = e.resolveTableInfo("t");
    var insertSource = new GeneratedColsFromRawInsertSource(txnCtx, e.nodeCtx, t.generatedColumns(), t.defaultExpressionColumns());
    expectedException.expect(IllegalArgumentException.class);
    expectedException.expectMessage("'abc' is too long for the text type of length: 2");
    insertSource.generateSourceAndCheckConstraints(new Object[] { "{}" });
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 39 with DocTableInfo

use of io.crate.metadata.doc.DocTableInfo in project crate by crate.

the class GeneratedColsFromRawInsertSourceTest method test_generated_based_on_default.

@Test
public void test_generated_based_on_default() throws Exception {
    var e = SQLExecutor.builder(clusterService).addTable("create table generated_based_on_default (x int default 1, y as x + 1)").build();
    DocTableInfo t = e.resolveTableInfo("generated_based_on_default");
    GeneratedColsFromRawInsertSource insertSource = new GeneratedColsFromRawInsertSource(txnCtx, e.nodeCtx, t.generatedColumns(), t.defaultExpressionColumns());
    Map<String, Object> map = insertSource.generateSourceAndCheckConstraints(new Object[] { "{}" });
    assertThat(Maps.getByPath(map, "x"), is(1));
    assertThat(Maps.getByPath(map, "y"), is(2));
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 40 with DocTableInfo

use of io.crate.metadata.doc.DocTableInfo in project crate by crate.

the class GeneratedColsFromRawInsertSourceTest method test_value_is_not_overwritten_by_default.

@Test
public void test_value_is_not_overwritten_by_default() throws Exception {
    var e = SQLExecutor.builder(clusterService).addTable("create table generated_based_on_default (x int default 1, y as x + 1)").build();
    DocTableInfo t = e.resolveTableInfo("generated_based_on_default");
    GeneratedColsFromRawInsertSource insertSource = new GeneratedColsFromRawInsertSource(txnCtx, e.nodeCtx, t.generatedColumns(), t.defaultExpressionColumns());
    Map<String, Object> map = insertSource.generateSourceAndCheckConstraints(new Object[] { "{\"x\":2}" });
    assertThat(Maps.getByPath(map, "x"), is(2));
    assertThat(Maps.getByPath(map, "y"), is(3));
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

DocTableInfo (io.crate.metadata.doc.DocTableInfo)127 Test (org.junit.Test)56 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)40 Symbol (io.crate.expression.symbol.Symbol)27 Reference (io.crate.metadata.Reference)27 SQLExecutor (io.crate.testing.SQLExecutor)25 RelationName (io.crate.metadata.RelationName)24 DocTableRelation (io.crate.analyze.relations.DocTableRelation)20 ColumnIdent (io.crate.metadata.ColumnIdent)20 TableInfo (io.crate.metadata.table.TableInfo)18 Assignments (io.crate.expression.symbol.Assignments)16 Row (io.crate.data.Row)14 PlannerContext (io.crate.planner.PlannerContext)13 Before (org.junit.Before)13 RowConsumer (io.crate.data.RowConsumer)12 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)12 PartitionName (io.crate.metadata.PartitionName)12 DependencyCarrier (io.crate.planner.DependencyCarrier)12 ArrayList (java.util.ArrayList)12 Map (java.util.Map)12