Search in sources :

Example 11 with Assignments

use of io.crate.expression.symbol.Assignments in project crate by crate.

the class UpdateAnalyzerTest method testUpdateWithWrongParameters.

@Test
public void testUpdateWithWrongParameters() throws Exception {
    Object[] params = { List.of(new HashMap<String, Object>()), new Map[0], new Long[] { 1L, 2L, 3L } };
    AnalyzedUpdateStatement update = analyze("update users set name=?, friends=? where other_id=?");
    Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
    expectedException.expect(ConversionException.class);
    expectedException.expectMessage("Cannot cast value `[{}]` to type `text`");
    assignments.bindSources(((DocTableInfo) update.table().tableInfo()), new RowN(params), SubQueryResults.EMPTY);
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) RowN(io.crate.data.RowN) HashMap(java.util.HashMap) Assignments(io.crate.expression.symbol.Assignments) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 12 with Assignments

use of io.crate.expression.symbol.Assignments in project crate by crate.

the class UpdatePlanner method updateByQuery.

private static ExecutionPlan updateByQuery(PlannerContext plannerCtx, DocTableRelation table, Map<Reference, Symbol> assignmentByTargetCol, WhereClauseOptimizer.DetailedQuery detailedQuery, Row params, SubQueryResults subQueryResults, @Nullable List<Symbol> returnValues) {
    DocTableInfo tableInfo = table.tableInfo();
    Reference idReference = requireNonNull(tableInfo.getReference(DocSysColumns.ID), "Table must have a _id column");
    Assignments assignments = Assignments.convert(assignmentByTargetCol, plannerCtx.nodeContext());
    Symbol[] assignmentSources = assignments.bindSources(tableInfo, params, subQueryResults);
    Symbol[] outputSymbols;
    if (returnValues == null) {
        // When there are no return values, set the output to a long representing the count of updated rows
        outputSymbols = new Symbol[] { new InputColumn(0, DataTypes.LONG) };
    } else {
        outputSymbols = new Symbol[returnValues.size()];
        for (int i = 0; i < returnValues.size(); i++) {
            outputSymbols[i] = new InputColumn(i, returnValues.get(i).valueType());
        }
    }
    UpdateProjection updateProjection = new UpdateProjection(new InputColumn(0, idReference.valueType()), assignments.targetNames(), assignmentSources, outputSymbols, returnValues == null ? null : returnValues.toArray(new Symbol[0]), null);
    WhereClause where = detailedQuery.toBoundWhereClause(tableInfo, params, subQueryResults, plannerCtx.transactionContext(), plannerCtx.nodeContext());
    if (where.hasVersions()) {
        throw VersioningValidationException.versionInvalidUsage();
    } else if (where.hasSeqNoAndPrimaryTerm()) {
        throw VersioningValidationException.seqNoAndPrimaryTermUsage();
    }
    if (returnValues == null) {
        return createCollectAndMerge(plannerCtx, tableInfo, idReference, updateProjection, where, 1, 1, MergeCountProjection.INSTANCE);
    } else {
        return createCollectAndMerge(plannerCtx, tableInfo, idReference, updateProjection, where, updateProjection.outputs().size(), -1);
    }
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) Reference(io.crate.metadata.Reference) SelectSymbol(io.crate.expression.symbol.SelectSymbol) Symbol(io.crate.expression.symbol.Symbol) InputColumn(io.crate.expression.symbol.InputColumn) Assignments(io.crate.expression.symbol.Assignments) WhereClause(io.crate.analyze.WhereClause) UpdateProjection(io.crate.execution.dsl.projection.UpdateProjection) SysUpdateProjection(io.crate.execution.dsl.projection.SysUpdateProjection)

Example 13 with Assignments

use of io.crate.expression.symbol.Assignments in project crate by crate.

the class UpdateSourceGenTest method test_update_child_of_object_column_that_is_null_implicitly_creates_the_object.

@Test
public void test_update_child_of_object_column_that_is_null_implicitly_creates_the_object() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table t (obj object as (x int))").build();
    AnalyzedUpdateStatement update = e.analyze("update t set obj['x'] = 10");
    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, Collections.singletonMap("obj", null), () -> "{\"obj\": null}"), assignments.sources(), new Object[0]);
    assertThat(updatedSource, is(Map.of("obj", Map.of("x", 10))));
}
Also used : 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 14 with Assignments

use of io.crate.expression.symbol.Assignments in project crate by crate.

the class UpdateSourceGenTest method testNestedGeneratedColumnRaiseErrorIfGivenByUserDoesNotMatch.

@Test
public void testNestedGeneratedColumnRaiseErrorIfGivenByUserDoesNotMatch() 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='bar'}");
    Assignments assignments = Assignments.convert(update.assignmentByTargetCol(), e.nodeCtx);
    DocTableInfo table = (DocTableInfo) update.table().tableInfo();
    UpdateSourceGen updateSourceGen = new UpdateSourceGen(txnCtx, e.nodeCtx, table, assignments.targetNames());
    expectedException.expect(IllegalArgumentException.class);
    expectedException.expectMessage("Given value bar for generated column obj['y'] does not match calculation 'foo' = foo");
    updateSourceGen.generateSource(new Doc(1, table.concreteIndices()[0], "1", 1, 1, 1, emptyMap(), () -> "{}"), assignments.sources(), new Object[0]);
}
Also used : 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 15 with Assignments

use of io.crate.expression.symbol.Assignments in project crate by crate.

the class UpdateSourceGenTest method testNestedGeneratedColumnIsGenerated.

@Test
public void testNestedGeneratedColumnIsGenerated() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table t (x int, obj object as (y as x + 1))").build();
    AnalyzedUpdateStatement update = e.analyze("update t set x = 4");
    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", 5), "x", 4)));
}
Also used : 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)

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