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-unsubscr...@doris.apache.org

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


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

Reply via email to