michaelsembwever commented on code in PR #24657:
URL: https://github.com/apache/datafusion/pull/24657#discussion_r3944933570
##########
datafusion/core/src/physical_planner.rs:
##########
@@ -2203,6 +2220,132 @@ fn get_physical_expr_pair(
Ok((physical_expr, physical_name))
}
+/// How a DELETE or an UPDATE reaches its target table.
+///
+/// The `filters` argument of [`TableProvider::delete_from`] and
+/// [`TableProvider::update`] is the only channel that carries the `WHERE`
clause
+/// to the provider, and an empty vector means "no `WHERE` clause, so every
row".
+/// A plan whose row restriction cannot travel through that channel must
+/// therefore never reach the provider.
+///
+/// [`TableProvider::delete_from`]:
datafusion_catalog::TableProvider::delete_from
+/// [`TableProvider::update`]: datafusion_catalog::TableProvider::update
+enum DmlInput {
+ /// Every row restriction of the statement reaches the provider as a
filter.
+ Filters,
+ /// No row matches, so the statement affects no rows and the provider is
not
+ /// called at all.
+ NoRows,
+}
+
+/// Check that the input plan of a DELETE or an UPDATE can reach the table
+/// provider without losing part of its `WHERE` clause.
+///
+/// The optimizer rewrites an `IN` or an `EXISTS` subquery into a semi join,
and
+/// it folds an always-false predicate into an empty relation. In both cases
the
+/// condition leaves the `Filter` nodes that [`extract_dml_filters`] reads, and
+/// the provider would see an empty filter list and change every row.
+///
+/// # Parameters
+/// - `input`: the input plan of the DELETE or the UPDATE
+/// - `target`: the target table of the statement
+/// - `op`: `"DELETE"` or `"UPDATE"`, used in the error message
+///
+/// # Returns
+/// [`DmlInput::Filters`] when the provider may be called, [`DmlInput::NoRows`]
+/// when the statement matches no row, and a "not implemented" error when part
of
+/// the `WHERE` clause cannot reach the provider.
+fn classify_dml_input(
+ input: &Arc<LogicalPlan>,
+ target: &TableReference,
+ op: &str,
+) -> Result<DmlInput> {
+ let mut allowed_refs = vec![target.clone()];
+ input.apply(|node| {
+ if let LogicalPlan::SubqueryAlias(alias) = node
+ && let LogicalPlan::TableScan(scan) = alias.input.as_ref()
+ && scan.table_name.resolved_eq(target)
+ {
+ allowed_refs.push(TableReference::bare(alias.alias.to_string()));
+ }
+ Ok(TreeNodeRecursion::Continue)
+ })?;
Review Comment:
extracted `collect_dml_target_refs()`. Both `classify_dml_input()` and
e`xtract_dml_filters()` had the same alias-collecting loop; each DML arm now
calls the helper once and passes `&[TableReference]` into both.
##########
datafusion/core/src/physical_planner.rs:
##########
@@ -793,17 +793,26 @@ impl DefaultPhysicalPlanner {
target,
op: WriteOp::Delete,
input,
- ..
+ output_schema,
}) => {
if let Some(provider) =
target.downcast_ref::<DefaultTableSource>() {
- let filters = extract_dml_filters(input, table_name)?;
- provider
- .table_provider
- .delete_from(session_state, filters)
- .await
- .map_err(|e| {
- e.context(format!("DELETE operation on table
'{table_name}'"))
- })?
+ match classify_dml_input(input, table_name, "DELETE")? {
+ DmlInput::NoRows => {
+
zero_rows_affected_exec(Arc::clone(output_schema.inner()))?
+ }
+ DmlInput::Filters => {
+ let filters = extract_dml_filters(input,
table_name)?;
Review Comment:
extracted `collect_dml_target_refs()`. Both `classify_dml_input()` and
e`xtract_dml_filters()` had the same alias-collecting loop; each DML arm now
calls the helper once and passes `&[TableReference]` into both.
--
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]