morrySnow commented on code in PR #17639:
URL: https://github.com/apache/doris/pull/17639#discussion_r1133654558


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/UpdateStmt.java:
##########
@@ -17,101 +17,123 @@
 
 package org.apache.doris.analysis;
 
+import org.apache.doris.catalog.Column;
 import org.apache.doris.catalog.Database;
 import org.apache.doris.catalog.Env;
 import org.apache.doris.catalog.KeysType;
 import org.apache.doris.catalog.OlapTable;
 import org.apache.doris.catalog.Table;
-import org.apache.doris.catalog.Type;
 import org.apache.doris.common.AnalysisException;
 import org.apache.doris.common.ErrorCode;
 import org.apache.doris.common.ErrorReport;
 import org.apache.doris.common.UserException;
 import org.apache.doris.common.util.Util;
 import org.apache.doris.mysql.privilege.PrivPredicate;
 import org.apache.doris.qe.ConnectContext;
-import org.apache.doris.rewrite.ExprRewriter;
 
 import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
 
 import java.util.List;
 import java.util.Set;
 import java.util.TreeSet;
 
 /**
- * UPDATE is a DML statement that modifies rows in a table.
+ * UPDATE is a DML statement that modifies rows in a unique key olap table.
  * The current update syntax only supports updating the filtered data of a 
single table.
- *
+ * <p>
  * UPDATE table_reference
  *     SET assignment_list
+ *     [from_clause]
  *     [WHERE where_condition]
- *
- * value:
- *     {expr}
- *
- * assignment:
- *     col_name = value
- *
+ * <p>
  * assignment_list:
  *     assignment [, assignment] ...
+ * <p>
+ * assignment:
+ *     col_name = value
+ * <p>
+ * value:
+ *     {expr}
  */
 public class UpdateStmt extends DdlStmt {
 
-    private TableName tableName;
-    private List<Expr> setExprs;
-    private Expr whereExpr;
-
-    // After analyzed
+    private final TableName tableName;
+    private final List<BinaryPredicate> setExprs;
+    private final Expr whereExpr;
+    private final FromClause fromClause;
+    private InsertStmt insertStmt;
     private Table targetTable;
-    private TupleDescriptor srcTupleDesc;
+    List<SelectListItem> selectListItems = Lists.newArrayList();
+    List<String> cols = Lists.newArrayList();
 
-    public UpdateStmt(TableName tableName, List<Expr> setExprs, Expr 
whereExpr) {
+    public UpdateStmt(TableName tableName, List<BinaryPredicate> setExprs, 
FromClause fromClause, Expr whereExpr) {
         this.tableName = tableName;
         this.setExprs = setExprs;
+        this.fromClause = fromClause;
         this.whereExpr = whereExpr;
-    }
-
-    public TableName getTableName() {
-        return tableName;
-    }
-
-    public List<Expr> getSetExprs() {
-        return setExprs;
-    }
-
-    public Expr getWhereExpr() {
-        return whereExpr;
-    }
 
-    public Table getTargetTable() {
-        return targetTable;
     }
 
-    public TupleDescriptor getSrcTupleDesc() {
-        return srcTupleDesc;
+    public InsertStmt getInsertStmt() {
+        return insertStmt;
     }
 
     @Override
     public void analyze(Analyzer analyzer) throws UserException {
         super.analyze(analyzer);
         analyzeTargetTable(analyzer);
         analyzeSetExprs(analyzer);
-        analyzeWhereExpr(analyzer);
+        constructInsertStmt();
+    }
+
+    private void constructInsertStmt() {
+        // not use origin from clause, because we need to mod it, and this 
action will affect toSql().
+        FromClause fromUsedInInsert;
+        TableRef tableRef = new TableRef(tableName, null);
+        if (fromClause == null) {
+            fromUsedInInsert = new FromClause(Lists.newArrayList(tableRef));

Review Comment:
   sequence column will keep the original value



##########
docs/zh-CN/docs/sql-manual/sql-reference/Data-Manipulation-Statements/Manipulation/UPDATE.md:
##########
@@ -76,9 +102,69 @@ UPDATE test SET v1 = 1 WHERE k1=1 and k2=2;
 UPDATE test SET v1 = v1+1 WHERE k1=1;
 ```
 
-### Keywords
+<version since="dev">
 
-    UPDATE
+UPDATE_FROM
+
+</version>
 
-### Best Practice
+3. 使用`t2`和`t3`表连接的结果,更新`t1`
 
+```sql
+-- 创建t1, t2, t3三张表
+CREATE TABLE t1
+  (id INT, c1 BIGINT, c2 STRING, c3 DOUBLE, c4 DATE)
+UNIQUE KEY (id)
+DISTRIBUTED BY HASH (id)
+PREOPERTIES('replication_num'='1');
+
+CREATE TABLE t2
+  (id INT, c1 BIGINT, c2 STRING, c3 DOUBLE, c4 DATE)
+DISTRIBUTED BY HASH (id)
+PREOPERTIES('replication_num'='1');
+
+CREATE TABLE t3
+  (id INT)
+DISTRIBUTED BY HASH (id)
+properties('replication_num'='1');
+
+-- 插入数据
+INSERT INTO t1 VALUES
+  (1, 1, '1', 1.0, '2000-01-01'),
+  (2, 2, '2', 2.0, '2000-01-02'),
+  (3, 3, '3', 3.0, '2000-01-03');
+
+INSERT INTO t2 VALUES
+  (1, 10, '10', 10.0, '2000-01-10'),
+  (2, 20, '20', 20.0, '2000-01-20'),
+  (3, 30, '30', 30.0, '2000-01-30'),
+  (4, 4, '4', 4.0, '2000-01-04'),
+  (5, 5, '5', 5.0, '2000-01-05');
+
+INSERT INTO t3 VALUES
+  (1),
+  (4),
+  (5);
+
+-- 更新 t1
+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;
+```
+
+预期结果为,更新了`t1`表`id`为`1`的列
+
+```
++----+----+----+--------+------------+
+| id | c1 | c2 | c3     | c4         |
++----+----+----+--------+------------+
+| 3  | 3  | 3  |    3.0 | 2000-01-03 |
+| 2  | 2  | 2  |    2.0 | 2000-01-02 |
+| 1  | 10 | 1  | 1000.0 | 2000-01-01 |
++----+----+----+--------+------------+
+```
+

Review Comment:
   done



-- 
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