use of io.crate.expression.symbol.Assignments 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.expression.symbol.Assignments in project crate by crate.
the class UpdateAnalyzerTest method testUpdateDynamicNestedArrayParam.
@Test
public void testUpdateDynamicNestedArrayParam() throws Exception {
Object[] params = { new Object[] { new Object[] { 1.9, 4.8 }, new Object[] { 9.7, 12.7 } } };
AnalyzedUpdateStatement update = analyze("update users set new=? where id=1");
Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
Symbol[] sources = assignments.bindSources(((DocTableInfo) update.table().tableInfo()), new RowN(params), SubQueryResults.EMPTY);
DataType dataType = sources[0].valueType();
assertThat(dataType, is(new ArrayType(new ArrayType(DoubleType.INSTANCE))));
}
use of io.crate.expression.symbol.Assignments in project crate by crate.
the class UpdateAnalyzerTest method testUpdateWithEmptyObjectArray.
@Test
public void testUpdateWithEmptyObjectArray() throws Exception {
Object[] params = { new Map[0], 0 };
AnalyzedUpdateStatement update = analyze("update users set friends=? where other_id=0");
Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
Symbol[] sources = assignments.bindSources(((DocTableInfo) update.table().tableInfo()), new RowN(params), SubQueryResults.EMPTY);
assertThat(sources[0].valueType().id(), is(ArrayType.ID));
assertThat(((ArrayType) sources[0].valueType()).innerType().id(), is(ObjectType.ID));
assertThat(((List) ((Literal) sources[0]).value()).size(), is(0));
}
use of io.crate.expression.symbol.Assignments in project crate by crate.
the class UpdateAnalyzerTest method testUpdateAssignmentConvertableType.
@Test
public void testUpdateAssignmentConvertableType() throws Exception {
AnalyzedUpdateStatement update = analyze("update users set other_id=9.9");
Reference ref = update.assignmentByTargetCol().keySet().iterator().next();
assertThat(ref, not(instanceOf(DynamicReference.class)));
assertEquals(DataTypes.LONG, ref.valueType());
Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
Symbol[] sources = assignments.bindSources(((DocTableInfo) update.table().tableInfo()), Row.EMPTY, SubQueryResults.EMPTY);
assertThat(sources[0], isLiteral(9L));
}
use of io.crate.expression.symbol.Assignments in project crate by crate.
the class UpdateAnalyzerTest method testUpdateInvalidType.
@Test
public void testUpdateInvalidType() throws Exception {
Object[] params = { new Object[] { new Object[] { "a", "b" } } };
AnalyzedUpdateStatement update = analyze("update users set tags=? where id=1");
expectedException.expect(ConversionException.class);
expectedException.expectMessage("Cannot cast value `[[a, b]]` to type `text_array`");
Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
assignments.bindSources(((DocTableInfo) update.table().tableInfo()), new RowN(params), SubQueryResults.EMPTY);
}
Aggregations