use of io.crate.analyze.AnalyzedUpdateStatement 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.analyze.AnalyzedUpdateStatement 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)));
}
use of io.crate.analyze.AnalyzedUpdateStatement 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)));
}
use of io.crate.analyze.AnalyzedUpdateStatement 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.analyze.AnalyzedUpdateStatement in project crate by crate.
the class WhereClauseAnalyzerTest method testUpdateWherePartitionedByColumn.
@Test
public void testUpdateWherePartitionedByColumn() throws Exception {
AnalyzedUpdateStatement update = analyzeUpdate("update parted set id = 2 where date = 1395874800000::timestamp");
assertThat(update.query(), isFunction(EqOperator.NAME, isReference("date"), isLiteral(1395874800000L)));
}
Aggregations