Search in sources :

Example 6 with Assignments

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)));
}
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 7 with Assignments

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))));
}
Also used : ArrayType(io.crate.types.ArrayType) DocTableInfo(io.crate.metadata.doc.DocTableInfo) RowN(io.crate.data.RowN) ParameterSymbol(io.crate.expression.symbol.ParameterSymbol) Symbol(io.crate.expression.symbol.Symbol) Assignments(io.crate.expression.symbol.Assignments) DataType(io.crate.types.DataType) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 8 with Assignments

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));
}
Also used : ArrayType(io.crate.types.ArrayType) DocTableInfo(io.crate.metadata.doc.DocTableInfo) RowN(io.crate.data.RowN) ParameterSymbol(io.crate.expression.symbol.ParameterSymbol) Symbol(io.crate.expression.symbol.Symbol) Literal(io.crate.expression.symbol.Literal) SymbolMatchers.isLiteral(io.crate.testing.SymbolMatchers.isLiteral) Assignments(io.crate.expression.symbol.Assignments) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 9 with Assignments

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));
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) SymbolMatchers.isReference(io.crate.testing.SymbolMatchers.isReference) DynamicReference(io.crate.expression.symbol.DynamicReference) Reference(io.crate.metadata.Reference) ParameterSymbol(io.crate.expression.symbol.ParameterSymbol) Symbol(io.crate.expression.symbol.Symbol) Assignments(io.crate.expression.symbol.Assignments) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 10 with Assignments

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);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) RowN(io.crate.data.RowN) Assignments(io.crate.expression.symbol.Assignments) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

Assignments (io.crate.expression.symbol.Assignments)15 DocTableInfo (io.crate.metadata.doc.DocTableInfo)15 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)12 Test (org.junit.Test)12 AnalyzedUpdateStatement (io.crate.analyze.AnalyzedUpdateStatement)7 Doc (io.crate.expression.reference.Doc)7 SQLExecutor (io.crate.testing.SQLExecutor)7 RowN (io.crate.data.RowN)6 Symbol (io.crate.expression.symbol.Symbol)6 Reference (io.crate.metadata.Reference)4 ParameterSymbol (io.crate.expression.symbol.ParameterSymbol)3 DataType (io.crate.types.DataType)3 IntArrayList (com.carrotsearch.hppc.IntArrayList)2 FutureActionListener (io.crate.action.FutureActionListener)2 OrderBy (io.crate.analyze.OrderBy)2 SymbolEvaluator (io.crate.analyze.SymbolEvaluator)2 AbstractTableRelation (io.crate.analyze.relations.AbstractTableRelation)2 TableFunctionRelation (io.crate.analyze.relations.TableFunctionRelation)2 RamAccounting (io.crate.breaker.RamAccounting)2 TypeGuessEstimateRowSize (io.crate.breaker.TypeGuessEstimateRowSize)2