Search in sources :

Example 6 with AnalyzedUpdateStatement

use of io.crate.analyze.AnalyzedUpdateStatement in project crate by crate.

the class UpdatePlanner method plan.

public static Plan plan(AnalyzedUpdateStatement update, PlannerContext plannerCtx, SubqueryPlanner subqueryPlanner) {
    if (update.outputs() != null && !plannerCtx.clusterState().getNodes().getMinNodeVersion().onOrAfter(Version.V_4_2_0)) {
        throw new UnsupportedFeatureException(RETURNING_VERSION_ERROR_MSG);
    }
    AbstractTableRelation<?> table = update.table();
    Plan plan;
    if (table instanceof DocTableRelation) {
        DocTableRelation docTable = (DocTableRelation) table;
        plan = plan(docTable, update.assignmentByTargetCol(), update.query(), plannerCtx, update.outputs());
    } else {
        plan = new Update((plannerContext, params, subQueryValues) -> sysUpdate(plannerContext, (TableRelation) table, update.assignmentByTargetCol(), update.query(), params, subQueryValues, update.outputs()));
    }
    Map<LogicalPlan, SelectSymbol> subQueries = subqueryPlanner.planSubQueries(update);
    return MultiPhasePlan.createIfNeeded(plan, subQueries);
}
Also used : UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) UpdateProjection(io.crate.execution.dsl.projection.UpdateProjection) SessionContext(io.crate.action.sql.SessionContext) SysUpdateProjection(io.crate.execution.dsl.projection.SysUpdateProjection) EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) Collections.singletonList(java.util.Collections.singletonList) DependencyCarrier(io.crate.planner.DependencyCarrier) Map(java.util.Map) SelectSymbol(io.crate.expression.symbol.SelectSymbol) DistributionInfo(io.crate.planner.distribution.DistributionInfo) DocSysColumns(io.crate.metadata.doc.DocSysColumns) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) LogicalPlan(io.crate.planner.operators.LogicalPlan) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) ExecutionPlan(io.crate.planner.ExecutionPlan) List(java.util.List) Version(org.elasticsearch.Version) Row(io.crate.data.Row) Projection(io.crate.execution.dsl.projection.Projection) Symbol(io.crate.expression.symbol.Symbol) DataTypes(io.crate.types.DataTypes) SubQueryResults(io.crate.planner.operators.SubQueryResults) Assignments(io.crate.expression.symbol.Assignments) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) InputColumn(io.crate.expression.symbol.InputColumn) SubqueryPlanner(io.crate.planner.SubqueryPlanner) WhereClauseOptimizer(io.crate.planner.WhereClauseOptimizer) CompletableFuture(java.util.concurrent.CompletableFuture) AnalyzedUpdateStatement(io.crate.analyze.AnalyzedUpdateStatement) ArrayList(java.util.ArrayList) Routing(io.crate.metadata.Routing) Objects.requireNonNull(java.util.Objects.requireNonNull) UpdateById(io.crate.planner.node.dml.UpdateById) VersioningValidationException(io.crate.exceptions.VersioningValidationException) TopN(io.crate.execution.engine.pipeline.TopN) Optimizer(io.crate.planner.optimizer.symbol.Optimizer) Nullable(javax.annotation.Nullable) MergeCountProjection(io.crate.execution.dsl.projection.MergeCountProjection) TableRelation(io.crate.analyze.relations.TableRelation) WhereClause(io.crate.analyze.WhereClause) NodeOperationTree(io.crate.execution.dsl.phases.NodeOperationTree) Reference(io.crate.metadata.Reference) NodeOperationTreeGenerator(io.crate.execution.engine.NodeOperationTreeGenerator) SubQueryAndParamBinder(io.crate.planner.operators.SubQueryAndParamBinder) Merge(io.crate.planner.Merge) RoutingProvider(io.crate.metadata.RoutingProvider) RowConsumer(io.crate.data.RowConsumer) DocTableRelation(io.crate.analyze.relations.DocTableRelation) AbstractTableRelation(io.crate.analyze.relations.AbstractTableRelation) PlannerContext(io.crate.planner.PlannerContext) Plan(io.crate.planner.Plan) MultiPhasePlan(io.crate.planner.MultiPhasePlan) Collect(io.crate.planner.node.dql.Collect) SelectSymbol(io.crate.expression.symbol.SelectSymbol) UnsupportedFeatureException(io.crate.exceptions.UnsupportedFeatureException) DocTableRelation(io.crate.analyze.relations.DocTableRelation) LogicalPlan(io.crate.planner.operators.LogicalPlan) LogicalPlan(io.crate.planner.operators.LogicalPlan) ExecutionPlan(io.crate.planner.ExecutionPlan) Plan(io.crate.planner.Plan) MultiPhasePlan(io.crate.planner.MultiPhasePlan)

Example 7 with AnalyzedUpdateStatement

use of io.crate.analyze.AnalyzedUpdateStatement in project crate by crate.

the class ReturnValueGenTest method setupStatement.

private void setupStatement(String stmt) throws IOException {
    SQLExecutor executor = SQLExecutor.builder(clusterService).addTable(CREATE_TEST_TABLE).build();
    AnalyzedUpdateStatement update = executor.analyze(stmt);
    tableInfo = (DocTableInfo) update.table().tableInfo();
    returnValueGen = new ReturnValueGen(txnCtx, executor.nodeCtx, tableInfo, update.outputs() == null ? null : update.outputs().toArray(new Symbol[0]));
}
Also used : SQLExecutor(io.crate.testing.SQLExecutor) AnalyzedUpdateStatement(io.crate.analyze.AnalyzedUpdateStatement) Symbol(io.crate.expression.symbol.Symbol)

Example 8 with AnalyzedUpdateStatement

use of io.crate.analyze.AnalyzedUpdateStatement 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 9 with AnalyzedUpdateStatement

use of io.crate.analyze.AnalyzedUpdateStatement 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 10 with AnalyzedUpdateStatement

use of io.crate.analyze.AnalyzedUpdateStatement 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

AnalyzedUpdateStatement (io.crate.analyze.AnalyzedUpdateStatement)10 Assignments (io.crate.expression.symbol.Assignments)8 DocTableInfo (io.crate.metadata.doc.DocTableInfo)8 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)8 SQLExecutor (io.crate.testing.SQLExecutor)8 Test (org.junit.Test)8 Doc (io.crate.expression.reference.Doc)7 Symbol (io.crate.expression.symbol.Symbol)2 SessionContext (io.crate.action.sql.SessionContext)1 WhereClause (io.crate.analyze.WhereClause)1 AbstractTableRelation (io.crate.analyze.relations.AbstractTableRelation)1 DocTableRelation (io.crate.analyze.relations.DocTableRelation)1 TableRelation (io.crate.analyze.relations.TableRelation)1 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)1 Row (io.crate.data.Row)1 RowConsumer (io.crate.data.RowConsumer)1 UnsupportedFeatureException (io.crate.exceptions.UnsupportedFeatureException)1 VersioningValidationException (io.crate.exceptions.VersioningValidationException)1 NodeOperationTree (io.crate.execution.dsl.phases.NodeOperationTree)1 RoutedCollectPhase (io.crate.execution.dsl.phases.RoutedCollectPhase)1