QuakeWang commented on code in PR #1397:
URL: https://github.com/apache/paimon-rust/pull/1397#discussion_r4565666301


##########
crates/integrations/datafusion/src/update.rs:
##########
@@ -114,12 +114,13 @@ async fn execute_update_once(
             .collect();
 
     let select_clause = select_parts.join(", ");
-    let where_clause = match &update.selection {
-        Some(expr) => format!(" WHERE {expr}"),
-        None => String::new(),
-    };
+    let selection_clause = build_dml_selection_clause(
+        update.selection.as_ref(),
+        &update.order_by,

Review Comment:
   If the table has a column named `__upd_id`, `UPDATE t SET id = id + 100 
ORDER BY __upd_id LIMIT 1` sorts by the generated `SET` alias. I tried this 
with `(id, __upd_id)` values `(1, 20)` and `(2, 10)`. Both paths updated row 1, 
but row 2 has the smaller `__upd_id`. Can we select the target rows before 
projecting the `SET` expressions?



##########
crates/integrations/datafusion/src/update.rs:
##########
@@ -114,12 +114,13 @@ async fn execute_update_once(
             .collect();
 
     let select_clause = select_parts.join(", ");
-    let where_clause = match &update.selection {
-        Some(expr) => format!(" WHERE {expr}"),
-        None => String::new(),
-    };
+    let selection_clause = build_dml_selection_clause(
+        update.selection.as_ref(),
+        &update.order_by,
+        update.limit.as_ref(),
+    );
 
-    let query_sql = format!("SELECT {select_clause} FROM 
{table_ref}{where_clause}");
+    let query_sql = format!("SELECT {select_clause} FROM 
{table_ref}{selection_clause}");

Review Comment:
   With IDs 1 and 2, `UPDATE t SET id = 12 / (2 - id) ORDER BY id LIMIT 1` 
fails with divide by zero on both paths. Only row 1 is selected, but `SET` is 
also evaluated for row 2. Moving the `SET` projection after `ORDER BY` and 
`LIMIT` would fix this too.



##########
crates/integrations/datafusion/src/delete.rs:
##########
@@ -182,14 +184,15 @@ async fn execute_cow_delete_inner(
     delete: &Delete,
     writer: &mut CopyOnWriteMergeWriter,
 ) -> DFResult<u64> {
-    let where_clause = match &delete.selection {
-        Some(expr) => format!(" WHERE {expr}"),
-        None => String::new(),
-    };
+    let selection_clause = build_dml_selection_clause(
+        delete.selection.as_ref(),
+        &delete.order_by,
+        delete.limit.as_ref(),
+    );
 
-    // Safety: where_clause comes from sqlparser AST to_string(), not raw user 
input.
+    // Selection clauses are rendered from the sqlparser AST.
     let query_sql = format!(
-        "SELECT \"__paimon_file_idx\", \"__paimon_row_offset\" FROM 
{cow_table_name}{where_clause}"
+        "SELECT \"__paimon_file_idx\", \"__paimon_row_offset\" FROM 
{cow_table_name}{selection_clause}"

Review Comment:
   `DELETE FROM t ORDER BY t.id DESC LIMIT 1` fails on CoW because the query 
now reads from `__cow_target_*` and `t.id` no longer resolves. I hit the same 
error with `UPDATE`. Can we preserve the target table qualifier when querying 
the temp table?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to