kosiew commented on code in PR #27236:
URL: https://github.com/apache/datafusion/pull/27236#discussion_r4766665679
##########
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:
Consider adding an aliased UPDATE with two SET targets sharing
`COALESCE(x.c0, 0)` and a selective WHERE repeating it. Assert the affected
count, computed values, and unchanged row.
##########
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:
Consider using `transform_up` explicitly and documenting why replacements
are not revisited: their column references belong to the projection's input
rather than its output.
##########
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:
Inlining can rewrite `id` to `s.id`, causing validation to silently discard
the predicate and delete all rows. Please resolve through the alias and
projection, reject unresolved restrictions before invoking the provider, and
add planner-level regressions for both cases.
--
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]