v28k commented on code in PR #27236:
URL: https://github.com/apache/datafusion/pull/27236#discussion_r4785342652
##########
datafusion/core/src/physical_planner.rs:
##########
@@ -2818,8 +2818,10 @@ fn extract_dml_filters(
LogicalPlan::Filter(filter) => {
// Split AND predicates into individual expressions
for predicate in split_conjunction(&filter.predicate) {
- if predicate_is_on_target_multi(predicate, allowed_refs)? {
- filters.push(predicate.clone());
+ let predicate =
+ inline_projected_columns(predicate.clone(),
&filter.input)?;
+ if predicate_is_on_target_multi(&predicate, allowed_refs)?
{
Review Comment:
Good catch. Updated `collect_dml_target_refs` to resolve aliases through
projections/filters/sorts/limits/nested aliases, inlined projected columns
inside `classify_dml_input` before target validation, and made
`extract_dml_filters` return `NotImplemented` instead of dropping non-target
predicates. Added planner-level regressions for both cases in `dml_planning.rs`.
##########
datafusion/sqllogictest/test_files/dml_update.slt:
##########
@@ -423,6 +423,41 @@ SELECT * FROM test_update_false;
statement ok
DROP TABLE test_update_false;
+# Repeated expressions in the assignments and in the WHERE clause: common
+# subexpression elimination computes them once in a projection below, and the
+# table provider must still receive expressions over the table's own columns
+# (the INT column is cast to BIGINT twice to match the literal)
+statement ok
+CREATE TABLE test_update_cse(c0 INT, c1 VARCHAR, c2 BIGINT);
+
+statement ok
+INSERT INTO test_update_cse VALUES (1, 'a', 10), (NULL, 'b', 20), (7, 'c', 30);
+
+query I
+UPDATE test_update_cse SET c0 = COALESCE(c0, 0);
+----
+3
+
+query I
+UPDATE test_update_cse SET c2 = COALESCE(c0, 0) + COALESCE(c0, 0) WHERE c1 <>
'c';
Review Comment:
Added the aliased `UPDATE` test with two `SET` targets sharing
`COALESCE(x.c0, 0)` and a selective `WHERE` repeating it in `dml_update.slt`.
##########
datafusion/core/src/physical_planner.rs:
##########
@@ -2994,6 +3003,41 @@ fn extract_update_assignments(input: &Arc<LogicalPlan>)
-> Result<Vec<(String, E
Ok(assignments)
}
+/// Replace references to columns computed by projections in the DML input
+/// (such as the `__common_expr` columns added by common subexpression
+/// elimination) with the expressions that compute them, so that an assignment
+/// or a filter only references columns of the target table.
+fn inline_projected_columns(mut expr: Expr, mut input: &LogicalPlan) ->
Result<Expr> {
+ loop {
+ input = match input {
+ LogicalPlan::Projection(projection) => {
+ let replacements: HashMap<Column, Expr> = projection
+ .schema
+ .columns()
+ .into_iter()
+ .zip(projection.expr.iter().map(|e| e.clone().unalias()))
+ .collect();
+ expr = expr
+ .transform(|e| match &e {
Review Comment:
Switched to `transform_up` explicitly and added a comment explaining why
replacements are not revisited in the same pass.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]