You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by "sohardforaname (via GitHub)" <gi...@apache.org> on 2023/06/01 06:42:00 UTC

[GitHub] [doris] sohardforaname opened a new pull request, #20313: [Feature](Nereids)support update.

sohardforaname opened a new pull request, #20313:
URL: https://github.com/apache/doris/pull/20313

   ## Proposed changes
   
   Issue Number: close #xxx
   
   <--Describe your changes.-->
   
   ## Further comments
   
   If this is a relatively large or complex change, kick off the discussion at [dev@doris.apache.org](mailto:dev@doris.apache.org) by explaining why you chose the solution you did and what alternatives you considered, etc...
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on PR #20313:
URL: https://github.com/apache/doris/pull/20313#issuecomment-1573399916

   run buildall


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on a diff in pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on code in PR #20313:
URL: https://github.com/apache/doris/pull/20313#discussion_r1217586505


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateCommand.java:
##########
@@ -0,0 +1,152 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.KeysType;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.catalog.TableIf;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.Pair;
+import org.apache.doris.nereids.analyzer.UnboundOlapTableSink;
+import org.apache.doris.nereids.analyzer.UnboundSlot;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.SessionVariable;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * update command
+ * the two case will be handled as:
+ * case 1:
+ *  update table t1 set v1 = v1 + 1 where k1 = 1 and k2 = 2;
+ * =>
+ *  insert into table (v1) select v1 + 1 from table t1 where k1 = 1 and k2 = 2
+ * case 2:
+ *  update t1 set t1.c1 = t2.c1, t1.c3 = t2.c3 * 100
+ *  from t2 inner join t3 on t2.id = t3.id
+ *  where t1.id = t2.id;
+ * =>
+ *  insert into t1 (c1, c3) select t2.c1, t2.c3 * 100 from t1 join t2 inner join t3 on t2.id = t3.id where t1.id = t2.id
+ */
+public class UpdateCommand extends Command implements ForwardWithSync {
+    private final List<Pair<List<String>, Expression>> assignments;
+    private final List<String> nameParts;
+    private final String tableAlias;
+    private LogicalPlan logicalQuery;
+    private OlapTable targetTable;
+    private ExplainLevel explainLevel = null;
+
+    public UpdateCommand(List<String> nameParts, String tableAlias, List<Pair<List<String>, Expression>> assignments,
+            LogicalPlan logicalQuery) {
+        super(PlanType.UPDATE_COMMAND);
+        this.nameParts = nameParts;
+        this.tableAlias = tableAlias;
+        this.assignments = assignments;
+        this.logicalQuery = logicalQuery;
+    }
+
+    @Override
+    public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
+        getQueryPlan(ctx);
+
+        if (explainLevel != null) {
+            new ExplainCommand(explainLevel, logicalQuery).run(ctx, executor);
+        } else {
+            new InsertIntoTableCommand(logicalQuery, null).run(ctx, executor);

Review Comment:
   it's the way to reuse as more code as possible.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on a diff in pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on code in PR #20313:
URL: https://github.com/apache/doris/pull/20313#discussion_r1217562702


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateCommand.java:
##########
@@ -0,0 +1,152 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.KeysType;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.catalog.TableIf;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.Pair;
+import org.apache.doris.nereids.analyzer.UnboundOlapTableSink;
+import org.apache.doris.nereids.analyzer.UnboundSlot;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.SessionVariable;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * update command
+ * the two case will be handled as:
+ * case 1:
+ *  update table t1 set v1 = v1 + 1 where k1 = 1 and k2 = 2;
+ * =>
+ *  insert into table (v1) select v1 + 1 from table t1 where k1 = 1 and k2 = 2
+ * case 2:
+ *  update t1 set t1.c1 = t2.c1, t1.c3 = t2.c3 * 100
+ *  from t2 inner join t3 on t2.id = t3.id
+ *  where t1.id = t2.id;
+ * =>
+ *  insert into t1 (c1, c3) select t2.c1, t2.c3 * 100 from t1 join t2 inner join t3 on t2.id = t3.id where t1.id = t2.id
+ */
+public class UpdateCommand extends Command implements ForwardWithSync {
+    private final List<Pair<List<String>, Expression>> assignments;
+    private final List<String> nameParts;
+    private final String tableAlias;
+    private LogicalPlan logicalQuery;
+    private OlapTable targetTable;
+    private ExplainLevel explainLevel = null;
+
+    public UpdateCommand(List<String> nameParts, String tableAlias, List<Pair<List<String>, Expression>> assignments,
+            LogicalPlan logicalQuery) {
+        super(PlanType.UPDATE_COMMAND);
+        this.nameParts = nameParts;
+        this.tableAlias = tableAlias;
+        this.assignments = assignments;
+        this.logicalQuery = logicalQuery;
+    }
+
+    @Override
+    public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
+        getQueryPlan(ctx);
+
+        if (explainLevel != null) {
+            new ExplainCommand(explainLevel, logicalQuery).run(ctx, executor);
+        } else {
+            new InsertIntoTableCommand(logicalQuery, null).run(ctx, executor);
+        }
+    }
+
+    /**
+     * public for test
+     */
+    public void getQueryPlan(ConnectContext ctx) throws AnalysisException {
+        checkTable(ctx);
+
+        if (logicalQuery instanceof ExplainCommand) {
+            explainLevel = ((ExplainCommand) logicalQuery).getLevel();
+            logicalQuery = ((ExplainCommand) logicalQuery).getLogicalPlan();
+        }

Review Comment:
   it's not the same problem as insert, because update will add project and olapTableSink node to the query plan,so we add update command.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on PR #20313:
URL: https://github.com/apache/doris/pull/20313#issuecomment-1575949162

   run buildall


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on PR #20313:
URL: https://github.com/apache/doris/pull/20313#issuecomment-1577837528

   run buildall


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on a diff in pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on code in PR #20313:
URL: https://github.com/apache/doris/pull/20313#discussion_r1217587379


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateCommand.java:
##########
@@ -0,0 +1,152 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.KeysType;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.catalog.TableIf;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.Pair;
+import org.apache.doris.nereids.analyzer.UnboundOlapTableSink;
+import org.apache.doris.nereids.analyzer.UnboundSlot;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.SessionVariable;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * update command
+ * the two case will be handled as:
+ * case 1:
+ *  update table t1 set v1 = v1 + 1 where k1 = 1 and k2 = 2;
+ * =>
+ *  insert into table (v1) select v1 + 1 from table t1 where k1 = 1 and k2 = 2
+ * case 2:
+ *  update t1 set t1.c1 = t2.c1, t1.c3 = t2.c3 * 100
+ *  from t2 inner join t3 on t2.id = t3.id
+ *  where t1.id = t2.id;
+ * =>
+ *  insert into t1 (c1, c3) select t2.c1, t2.c3 * 100 from t1 join t2 inner join t3 on t2.id = t3.id where t1.id = t2.id
+ */
+public class UpdateCommand extends Command implements ForwardWithSync {
+    private final List<Pair<List<String>, Expression>> assignments;
+    private final List<String> nameParts;
+    private final String tableAlias;
+    private LogicalPlan logicalQuery;
+    private OlapTable targetTable;
+    private ExplainLevel explainLevel = null;
+
+    public UpdateCommand(List<String> nameParts, String tableAlias, List<Pair<List<String>, Expression>> assignments,
+            LogicalPlan logicalQuery) {
+        super(PlanType.UPDATE_COMMAND);
+        this.nameParts = nameParts;
+        this.tableAlias = tableAlias;
+        this.assignments = assignments;
+        this.logicalQuery = logicalQuery;
+    }
+
+    @Override
+    public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
+        getQueryPlan(ctx);
+
+        if (explainLevel != null) {
+            new ExplainCommand(explainLevel, logicalQuery).run(ctx, executor);
+        } else {
+            new InsertIntoTableCommand(logicalQuery, null).run(ctx, executor);

Review Comment:
   it's the way to reuse the code as more as possible.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] github-actions[bot] commented on pull request #20313: [Feature](Nereids)support update.

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #20313:
URL: https://github.com/apache/doris/pull/20313#issuecomment-1578506871

   PR approved by anyone and no changes requested.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on PR #20313:
URL: https://github.com/apache/doris/pull/20313#issuecomment-1576435230

   run buildall


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on a diff in pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on code in PR #20313:
URL: https://github.com/apache/doris/pull/20313#discussion_r1217459857


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -76,7 +76,7 @@ public NereidsPlanner getPlanner() {
 
     @Override
     public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
-        if (!ctx.getSessionVariable().isEnableNereidsDML()) {
+        if (false && !ctx.getSessionVariable().isEnableNereidsDML()) {

Review Comment:
   For test, I will remove it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on a diff in pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on code in PR #20313:
URL: https://github.com/apache/doris/pull/20313#discussion_r1217566124


##########
regression-test/suites/nereids_p0/update/load.groovy:
##########
@@ -0,0 +1,85 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("load") {

Review Comment:
   what do you mean? It's  just a data load case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on a diff in pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on code in PR #20313:
URL: https://github.com/apache/doris/pull/20313#discussion_r1217459306


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java:
##########
@@ -2221,6 +2222,11 @@ private PlanFragment exchangeToMergeFragment(PlanFragment inputFragment, PlanTra
         PlanFragment fragment = new PlanFragment(context.nextFragmentId(), mergePlan, dataPartition);
         inputFragment.setDestination(mergePlan);
         context.addPlanFragment(fragment);
+        if (inputFragment.getSink() instanceof OlapTableSink) {
+            DataSink sink = inputFragment.getSink();
+            inputFragment.cleanSink();
+            fragment.setSink(sink);
+        }

Review Comment:
   because it's changed at the same time as the pr, I remove it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] github-actions[bot] commented on pull request #20313: [Feature](Nereids)support update.

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #20313:
URL: https://github.com/apache/doris/pull/20313#issuecomment-1578506740

   PR approved by at least one committer and no changes requested.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] morrySnow commented on a diff in pull request #20313: [Feature](Nereids)support update.

Posted by "morrySnow (via GitHub)" <gi...@apache.org>.
morrySnow commented on code in PR #20313:
URL: https://github.com/apache/doris/pull/20313#discussion_r1217439283


##########
fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4:
##########
@@ -525,6 +538,7 @@ nonReserved
     | DATE_DIFF
     | DAY
     | DBPROPERTIES
+    | DEFAULT

Review Comment:
   default is not in non reserved list in legacy planer



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java:
##########
@@ -2221,6 +2222,11 @@ private PlanFragment exchangeToMergeFragment(PlanFragment inputFragment, PlanTra
         PlanFragment fragment = new PlanFragment(context.nextFragmentId(), mergePlan, dataPartition);
         inputFragment.setDestination(mergePlan);
         context.addPlanFragment(fragment);
+        if (inputFragment.getSink() instanceof OlapTableSink) {
+            DataSink sink = inputFragment.getSink();
+            inputFragment.cleanSink();
+            fragment.setSink(sink);
+        }

Review Comment:
   ? why add this again?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateCommand.java:
##########
@@ -0,0 +1,152 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.KeysType;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.catalog.TableIf;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.Pair;
+import org.apache.doris.nereids.analyzer.UnboundOlapTableSink;
+import org.apache.doris.nereids.analyzer.UnboundSlot;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.SessionVariable;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * update command
+ * the two case will be handled as:
+ * case 1:
+ *  update table t1 set v1 = v1 + 1 where k1 = 1 and k2 = 2;
+ * =>
+ *  insert into table (v1) select v1 + 1 from table t1 where k1 = 1 and k2 = 2
+ * case 2:
+ *  update t1 set t1.c1 = t2.c1, t1.c3 = t2.c3 * 100
+ *  from t2 inner join t3 on t2.id = t3.id
+ *  where t1.id = t2.id;
+ * =>
+ *  insert into t1 (c1, c3) select t2.c1, t2.c3 * 100 from t1 join t2 inner join t3 on t2.id = t3.id where t1.id = t2.id
+ */
+public class UpdateCommand extends Command implements ForwardWithSync {
+    private final List<Pair<List<String>, Expression>> assignments;
+    private final List<String> nameParts;
+    private final String tableAlias;
+    private LogicalPlan logicalQuery;
+    private OlapTable targetTable;
+    private ExplainLevel explainLevel = null;
+
+    public UpdateCommand(List<String> nameParts, String tableAlias, List<Pair<List<String>, Expression>> assignments,
+            LogicalPlan logicalQuery) {
+        super(PlanType.UPDATE_COMMAND);
+        this.nameParts = nameParts;
+        this.tableAlias = tableAlias;
+        this.assignments = assignments;
+        this.logicalQuery = logicalQuery;
+    }
+
+    @Override
+    public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
+        getQueryPlan(ctx);
+
+        if (explainLevel != null) {
+            new ExplainCommand(explainLevel, logicalQuery).run(ctx, executor);
+        } else {
+            new InsertIntoTableCommand(logicalQuery, null).run(ctx, executor);
+        }
+    }
+
+    /**
+     * public for test
+     */
+    public void getQueryPlan(ConnectContext ctx) throws AnalysisException {

Review Comment:
   if ur function named getXX, u must have a XX object as return value. please change this function name



##########
regression-test/suites/nereids_p0/update/update_unique_table.groovy:
##########
@@ -0,0 +1,39 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite('update_unique_table') {
+    sql 'use nereids_update_test'

Review Comment:
   why need to change database?



##########
regression-test/suites/nereids_p0/update/update_unique_table.groovy:
##########
@@ -0,0 +1,39 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite('update_unique_table') {
+    sql 'use nereids_update_test'
+    sql 'set enable_nereids_planner=true'
+    sql 'set enable_fallback_to_original_planner=false'
+    sql 'set enable_nereids_dml=true'
+    sql 'set parallel_fragment_exec_instance_num=13'
+
+    qt_sql 'select * from t1 order by id'
+
+    sql 'update t1 set c1 = c1 + 1, c3 = c2 * 2 where id = 1'
+
+    qt_sql 'select * from t1 order by id'
+
+    sql '''

Review Comment:
   add a case with simple update, such as `update a set c1 = 5 where id = 3`



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateCommand.java:
##########
@@ -0,0 +1,152 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.KeysType;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.catalog.TableIf;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.Pair;
+import org.apache.doris.nereids.analyzer.UnboundOlapTableSink;
+import org.apache.doris.nereids.analyzer.UnboundSlot;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.SessionVariable;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * update command
+ * the two case will be handled as:
+ * case 1:
+ *  update table t1 set v1 = v1 + 1 where k1 = 1 and k2 = 2;
+ * =>
+ *  insert into table (v1) select v1 + 1 from table t1 where k1 = 1 and k2 = 2
+ * case 2:
+ *  update t1 set t1.c1 = t2.c1, t1.c3 = t2.c3 * 100
+ *  from t2 inner join t3 on t2.id = t3.id
+ *  where t1.id = t2.id;
+ * =>
+ *  insert into t1 (c1, c3) select t2.c1, t2.c3 * 100 from t1 join t2 inner join t3 on t2.id = t3.id where t1.id = t2.id
+ */
+public class UpdateCommand extends Command implements ForwardWithSync {
+    private final List<Pair<List<String>, Expression>> assignments;
+    private final List<String> nameParts;
+    private final String tableAlias;
+    private LogicalPlan logicalQuery;
+    private OlapTable targetTable;
+    private ExplainLevel explainLevel = null;
+
+    public UpdateCommand(List<String> nameParts, String tableAlias, List<Pair<List<String>, Expression>> assignments,
+            LogicalPlan logicalQuery) {
+        super(PlanType.UPDATE_COMMAND);
+        this.nameParts = nameParts;
+        this.tableAlias = tableAlias;
+        this.assignments = assignments;
+        this.logicalQuery = logicalQuery;
+    }
+
+    @Override
+    public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
+        getQueryPlan(ctx);
+
+        if (explainLevel != null) {
+            new ExplainCommand(explainLevel, logicalQuery).run(ctx, executor);
+        } else {
+            new InsertIntoTableCommand(logicalQuery, null).run(ctx, executor);
+        }
+    }
+
+    /**
+     * public for test
+     */
+    public void getQueryPlan(ConnectContext ctx) throws AnalysisException {
+        checkTable(ctx);
+
+        if (logicalQuery instanceof ExplainCommand) {
+            explainLevel = ((ExplainCommand) logicalQuery).getLevel();
+            logicalQuery = ((ExplainCommand) logicalQuery).getLogicalPlan();
+        }
+
+        Map<String, Expression> colNameToExpression = Maps.newHashMap();
+        for (Pair<List<String>, Expression> pair : assignments) {
+            colNameToExpression.put(pair.first.get(pair.first.size() - 1), pair.second);
+        }
+        List<NamedExpression> selectLists = Lists.newArrayList();

Review Comment:
   ```suggestion
           List<NamedExpression> selectItems = Lists.newArrayList();
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java:
##########
@@ -1318,6 +1342,18 @@ public List<String> visitIdentifierSeq(IdentifierSeqContext ctx) {
             .collect(ImmutableList.toImmutableList());
     }
 
+    @Override
+    public Pair<List<String>, Expression> visitUpdateAssignment(UpdateAssignmentContext ctx) {
+        return Pair.of(visitMultipartIdentifier(ctx.multipartIdentifier()), getExpression(ctx.expression()));
+    }

Review Comment:
   why not gernate `Equals(unboundSlot, Expression)`



##########
regression-test/suites/nereids_p0/update/update_unique_table.groovy:
##########
@@ -0,0 +1,39 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite('update_unique_table') {
+    sql 'use nereids_update_test'
+    sql 'set enable_nereids_planner=true'
+    sql 'set enable_fallback_to_original_planner=false'
+    sql 'set enable_nereids_dml=true'
+    sql 'set parallel_fragment_exec_instance_num=13'

Review Comment:
   remove set parallel_fragment_exec_instance_num=13



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/InsertIntoTableCommand.java:
##########
@@ -76,7 +76,7 @@ public NereidsPlanner getPlanner() {
 
     @Override
     public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
-        if (!ctx.getSessionVariable().isEnableNereidsDML()) {
+        if (false && !ctx.getSessionVariable().isEnableNereidsDML()) {

Review Comment:
   ??



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/PlanVisitor.java:
##########
@@ -128,6 +129,10 @@ public R visitInsertIntoCommand(InsertIntoTableCommand insertIntoSelectCommand,
         return visit(insertIntoSelectCommand, context);
     }
 
+    public R visitUpdateCommand(UpdateCommand updateCommand, C context) {
+        return visit(updateCommand, context);
+    }
+

Review Comment:
   like what do on sclarFunction and agg function in expression vistor, move all command visitor into another file



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateCommand.java:
##########
@@ -0,0 +1,152 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.KeysType;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.catalog.TableIf;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.Pair;
+import org.apache.doris.nereids.analyzer.UnboundOlapTableSink;
+import org.apache.doris.nereids.analyzer.UnboundSlot;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.SessionVariable;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * update command
+ * the two case will be handled as:
+ * case 1:
+ *  update table t1 set v1 = v1 + 1 where k1 = 1 and k2 = 2;
+ * =>
+ *  insert into table (v1) select v1 + 1 from table t1 where k1 = 1 and k2 = 2
+ * case 2:
+ *  update t1 set t1.c1 = t2.c1, t1.c3 = t2.c3 * 100
+ *  from t2 inner join t3 on t2.id = t3.id
+ *  where t1.id = t2.id;
+ * =>
+ *  insert into t1 (c1, c3) select t2.c1, t2.c3 * 100 from t1 join t2 inner join t3 on t2.id = t3.id where t1.id = t2.id
+ */
+public class UpdateCommand extends Command implements ForwardWithSync {
+    private final List<Pair<List<String>, Expression>> assignments;
+    private final List<String> nameParts;
+    private final String tableAlias;
+    private LogicalPlan logicalQuery;
+    private OlapTable targetTable;
+    private ExplainLevel explainLevel = null;
+
+    public UpdateCommand(List<String> nameParts, String tableAlias, List<Pair<List<String>, Expression>> assignments,
+            LogicalPlan logicalQuery) {
+        super(PlanType.UPDATE_COMMAND);
+        this.nameParts = nameParts;
+        this.tableAlias = tableAlias;
+        this.assignments = assignments;
+        this.logicalQuery = logicalQuery;
+    }
+
+    @Override
+    public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
+        getQueryPlan(ctx);
+
+        if (explainLevel != null) {
+            new ExplainCommand(explainLevel, logicalQuery).run(ctx, executor);
+        } else {
+            new InsertIntoTableCommand(logicalQuery, null).run(ctx, executor);
+        }
+    }
+
+    /**
+     * public for test
+     */
+    public void getQueryPlan(ConnectContext ctx) throws AnalysisException {
+        checkTable(ctx);
+
+        if (logicalQuery instanceof ExplainCommand) {
+            explainLevel = ((ExplainCommand) logicalQuery).getLevel();
+            logicalQuery = ((ExplainCommand) logicalQuery).getLogicalPlan();
+        }

Review Comment:
   same problem with insert, if user write `explain update xxx`, u should generate a ExplainCommand other than UpdateCommand



##########
regression-test/suites/nereids_p0/update/load.groovy:
##########
@@ -0,0 +1,85 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("load") {

Review Comment:
   why add this case?



##########
fe/fe-core/src/main/java/org/apache/doris/planner/PlanFragment.java:
##########
@@ -390,6 +390,10 @@ public DataSink getSink() {
         return sink;
     }
 
+    public void cleanSink() {
+        this.sink = null;
+    }
+

Review Comment:
   why need this?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateCommand.java:
##########
@@ -0,0 +1,152 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.KeysType;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.catalog.TableIf;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.Pair;
+import org.apache.doris.nereids.analyzer.UnboundOlapTableSink;
+import org.apache.doris.nereids.analyzer.UnboundSlot;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.SessionVariable;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * update command
+ * the two case will be handled as:
+ * case 1:
+ *  update table t1 set v1 = v1 + 1 where k1 = 1 and k2 = 2;
+ * =>
+ *  insert into table (v1) select v1 + 1 from table t1 where k1 = 1 and k2 = 2
+ * case 2:
+ *  update t1 set t1.c1 = t2.c1, t1.c3 = t2.c3 * 100
+ *  from t2 inner join t3 on t2.id = t3.id
+ *  where t1.id = t2.id;
+ * =>
+ *  insert into t1 (c1, c3) select t2.c1, t2.c3 * 100 from t1 join t2 inner join t3 on t2.id = t3.id where t1.id = t2.id
+ */
+public class UpdateCommand extends Command implements ForwardWithSync {
+    private final List<Pair<List<String>, Expression>> assignments;
+    private final List<String> nameParts;
+    private final String tableAlias;
+    private LogicalPlan logicalQuery;
+    private OlapTable targetTable;
+    private ExplainLevel explainLevel = null;
+
+    public UpdateCommand(List<String> nameParts, String tableAlias, List<Pair<List<String>, Expression>> assignments,
+            LogicalPlan logicalQuery) {
+        super(PlanType.UPDATE_COMMAND);
+        this.nameParts = nameParts;
+        this.tableAlias = tableAlias;
+        this.assignments = assignments;
+        this.logicalQuery = logicalQuery;

Review Comment:
   1. ImmutableXXX.copyOf
   2. require non null



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateCommand.java:
##########
@@ -0,0 +1,152 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.KeysType;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.catalog.TableIf;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.Pair;
+import org.apache.doris.nereids.analyzer.UnboundOlapTableSink;
+import org.apache.doris.nereids.analyzer.UnboundSlot;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.SessionVariable;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * update command
+ * the two case will be handled as:
+ * case 1:
+ *  update table t1 set v1 = v1 + 1 where k1 = 1 and k2 = 2;
+ * =>
+ *  insert into table (v1) select v1 + 1 from table t1 where k1 = 1 and k2 = 2
+ * case 2:
+ *  update t1 set t1.c1 = t2.c1, t1.c3 = t2.c3 * 100
+ *  from t2 inner join t3 on t2.id = t3.id
+ *  where t1.id = t2.id;
+ * =>
+ *  insert into t1 (c1, c3) select t2.c1, t2.c3 * 100 from t1 join t2 inner join t3 on t2.id = t3.id where t1.id = t2.id
+ */
+public class UpdateCommand extends Command implements ForwardWithSync {
+    private final List<Pair<List<String>, Expression>> assignments;
+    private final List<String> nameParts;
+    private final String tableAlias;
+    private LogicalPlan logicalQuery;
+    private OlapTable targetTable;
+    private ExplainLevel explainLevel = null;
+
+    public UpdateCommand(List<String> nameParts, String tableAlias, List<Pair<List<String>, Expression>> assignments,
+            LogicalPlan logicalQuery) {
+        super(PlanType.UPDATE_COMMAND);
+        this.nameParts = nameParts;
+        this.tableAlias = tableAlias;
+        this.assignments = assignments;
+        this.logicalQuery = logicalQuery;
+    }
+
+    @Override
+    public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
+        getQueryPlan(ctx);
+
+        if (explainLevel != null) {
+            new ExplainCommand(explainLevel, logicalQuery).run(ctx, executor);
+        } else {
+            new InsertIntoTableCommand(logicalQuery, null).run(ctx, executor);

Review Comment:
   could we do some abstract, not to new a InsertIntoTableCommand explicitly, but reuse some code between insert, update and delete?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java:
##########
@@ -315,6 +319,26 @@ public LogicalPlan visitInsertIntoQuery(InsertIntoQueryContext ctx) {
         return new InsertIntoTableCommand(sink, labelName);
     }
 
+    @Override
+    public Object visitUpdate(UpdateContext ctx) {
+        LogicalPlan query = withCheckPolicy(new UnboundRelation(
+                RelationUtil.newRelationId(), visitMultipartIdentifier(ctx.tableName)));

Review Comment:
   why not process alias here with `withTableAlias`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on PR #20313:
URL: https://github.com/apache/doris/pull/20313#issuecomment-1576719223

   run buildall


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] morrySnow commented on a diff in pull request #20313: [Feature](Nereids)support update.

Posted by "morrySnow (via GitHub)" <gi...@apache.org>.
morrySnow commented on code in PR #20313:
URL: https://github.com/apache/doris/pull/20313#discussion_r1217981693


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateCommand.java:
##########
@@ -0,0 +1,163 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.KeysType;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.catalog.TableIf;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.nereids.analyzer.UnboundOlapTableSink;
+import org.apache.doris.nereids.analyzer.UnboundSlot;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.EqualTo;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.nereids.util.RelationUtil;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.SessionVariable;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * update command
+ * the two case will be handled as:
+ * case 1:
+ *  update table t1 set v1 = v1 + 1 where k1 = 1 and k2 = 2;
+ * =>
+ *  insert into table (v1) select v1 + 1 from table t1 where k1 = 1 and k2 = 2
+ * case 2:
+ *  update t1 set t1.c1 = t2.c1, t1.c3 = t2.c3 * 100
+ *  from t2 inner join t3 on t2.id = t3.id
+ *  where t1.id = t2.id;
+ * =>
+ *  insert into t1 (c1, c3) select t2.c1, t2.c3 * 100 from t1 join t2 inner join t3 on t2.id = t3.id where t1.id = t2.id
+ */
+public class UpdateCommand extends Command implements ForwardWithSync {
+    private final List<EqualTo> assignments;
+    private final List<String> nameParts;
+    private final String tableAlias;
+    private LogicalPlan logicalQuery;
+    private OlapTable targetTable;
+    private ExplainLevel explainLevel = null;
+
+    /**
+     * constructor
+     */
+    public UpdateCommand(List<String> nameParts, String tableAlias, List<EqualTo> assignments,
+            LogicalPlan logicalQuery) {
+        super(PlanType.UPDATE_COMMAND);
+        this.nameParts = ImmutableList.copyOf(Objects.requireNonNull(nameParts,
+                "tableName is required in update command"));
+        this.assignments = ImmutableList.copyOf(Objects.requireNonNull(assignments,
+                "assignment is required in update command"));
+        this.tableAlias = tableAlias;
+        this.logicalQuery = Objects.requireNonNull(logicalQuery, "logicalQuery is required in update command");
+    }
+
+    @Override
+    public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
+        completeQueryPlan(ctx);
+
+        if (explainLevel != null) {
+            new ExplainCommand(explainLevel, logicalQuery).run(ctx, executor);
+        } else {
+            new InsertIntoTableCommand(logicalQuery, null).run(ctx, executor);
+        }
+    }
+
+    /**
+     * public for test
+     */
+    public void completeQueryPlan(ConnectContext ctx) throws AnalysisException {
+        checkTable(ctx);
+
+        if (logicalQuery instanceof ExplainCommand) {
+            explainLevel = ((ExplainCommand) logicalQuery).getLevel();
+            logicalQuery = ((ExplainCommand) logicalQuery).getLogicalPlan();
+        }
+
+        Map<String, Expression> colNameToExpression = Maps.newHashMap();
+        for (EqualTo equalTo : assignments) {
+            List<String> nameParts = ((UnboundSlot) equalTo.left()).getNameParts();
+            colNameToExpression.put(nameParts.get(nameParts.size() - 1), equalTo.right());
+        }
+        List<NamedExpression> selectItems = Lists.newArrayList();
+        for (Column column : targetTable.getFullSchema()) {
+            if (!column.isVisible()) {
+                continue;
+            }
+            if (colNameToExpression.containsKey(column.getName())) {
+                Expression expr = colNameToExpression.get(column.getName());
+                selectItems.add(expr instanceof UnboundSlot
+                        ? ((NamedExpression) expr)
+                        : new Alias(expr, expr.toSql()));
+            } else {
+                selectItems.add(new UnboundSlot(
+                        tableAlias != null

Review Comment:
   generate it before `for` loop  for easy read



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java:
##########
@@ -315,6 +319,23 @@ public LogicalPlan visitInsertIntoQuery(InsertIntoQueryContext ctx) {
         return new InsertIntoTableCommand(sink, labelName);
     }
 
+    @Override
+    public Object visitUpdate(UpdateContext ctx) {

Review Comment:
   ```suggestion
       public LogicalPlan visitUpdate(UpdateContext ctx) {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] morrySnow merged pull request #20313: [Feature](Nereids)support update.

Posted by "morrySnow (via GitHub)" <gi...@apache.org>.
morrySnow merged PR #20313:
URL: https://github.com/apache/doris/pull/20313


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on PR #20313:
URL: https://github.com/apache/doris/pull/20313#issuecomment-1576285003

   run buildall


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on PR #20313:
URL: https://github.com/apache/doris/pull/20313#issuecomment-1578102596

   run buildall


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on a diff in pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on code in PR #20313:
URL: https://github.com/apache/doris/pull/20313#discussion_r1217564096


##########
regression-test/suites/nereids_p0/update/update_unique_table.groovy:
##########
@@ -0,0 +1,39 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite('update_unique_table') {
+    sql 'use nereids_update_test'

Review Comment:
   because the test database for update is nereids_update_test not the default database generated by regression-test framework.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on a diff in pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on code in PR #20313:
URL: https://github.com/apache/doris/pull/20313#discussion_r1217579614


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java:
##########
@@ -1318,6 +1342,18 @@ public List<String> visitIdentifierSeq(IdentifierSeqContext ctx) {
             .collect(ImmutableList.toImmutableList());
     }
 
+    @Override
+    public Pair<List<String>, Expression> visitUpdateAssignment(UpdateAssignmentContext ctx) {
+        return Pair.of(visitMultipartIdentifier(ctx.multipartIdentifier()), getExpression(ctx.expression()));
+    }

Review Comment:
   EqualTo is also ok.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


[GitHub] [doris] sohardforaname commented on pull request #20313: [Feature](Nereids)support update.

Posted by "sohardforaname (via GitHub)" <gi...@apache.org>.
sohardforaname commented on PR #20313:
URL: https://github.com/apache/doris/pull/20313#issuecomment-1571970993

   run buildall


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org