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)));
}
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"));
}
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[] { "{}" });
}
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));
}
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));
}
Aggregations