kosiew commented on code in PR #26969:
URL: https://github.com/apache/datafusion/pull/26969#discussion_r4628274244


##########
datafusion/sqllogictest/test_files/projection_pushdown.slt:
##########
@@ -1962,6 +1962,51 @@ ORDER BY simple_struct.id;
 # (https://github.com/apache/datafusion/issues/20430)
 # =========================================================================
 
+# A prefix alone does not make an alias an extraction. In particular, moving
+# this division below the filter would evaluate it on the zero row as well.
+statement ok
+CREATE TABLE extracted_alias_names AS
+SELECT * FROM (VALUES (0), (1)) AS v(id);
+
+query I
+SELECT 10 / id AS __datafusion_extracted_manual

Review Comment:
   Consider testing `10 / id AS __datafusion_extracted_manual` with `id > 0 AND 
s['a'] = 1`, where both rows satisfy the struct predicate. This verifies that 
genuine leaf extraction does not move division below its guard.



##########
datafusion/optimizer/src/extract_leaf_expressions.rs:
##########
@@ -1251,7 +1263,7 @@ fn split_and_push_projection(
 }
 
 /// Returns true if `exprs` are the expressions of a *pure extraction
-/// projection*: every expression is either `Alias(EXTRACTED_EXPR_PREFIX, ...)`
+/// projection*: every expression is either an alias named 
`__datafusion_extracted_<number>`

Review Comment:
   Could we link this documentation to `extracted_expr_id` and specify 
canonical positive IDs? `<number>` incorrectly suggests `_0` and `_01` qualify.



##########
datafusion/optimizer/src/extract_leaf_expressions.rs:
##########
@@ -84,12 +84,28 @@ use crate::{OptimizerConfig, OptimizerRule};
 
 /// Prefix for aliases generated by the extraction optimizer passes.
 ///
-/// This prefix is **reserved for internal optimizer use**. User-defined 
aliases
-/// starting with this prefix may be misidentified as optimizer-generated
-/// extraction aliases, leading to unexpected behavior. Do not use this prefix
-/// in user queries.
+/// Names of the form `__datafusion_extracted_N`, where `N` is in 
`1..usize::MAX`
+/// with no leading zeros, are **reserved for internal optimizer use**.
+/// User-defined aliases matching that form may still be misidentified as
+/// optimizer-generated extraction aliases.
 const EXTRACTED_EXPR_PREFIX: &str = "__datafusion_extracted";
 
+/// Recognize canonical positive decimal IDs that the counter can advance past.
+/// Use the same check when identifying extractions and when advancing the
+/// generator past them.
+fn extracted_expr_id(name: &str) -> Option<usize> {
+    let suffix = name
+        .strip_prefix(EXTRACTED_EXPR_PREFIX)?
+        .strip_prefix('_')?;
+    // Integer parsing alone also accepts leading zeros and a leading '+'.
+    if suffix.starts_with('0') || !suffix.bytes().all(|b| b.is_ascii_digit()) {
+        return None;
+    }
+    let id = suffix.parse::<usize>().ok()?;
+    // update_min_id adds one, so the maximum value has no usable successor.
+    (id < usize::MAX).then_some(id)

Review Comment:
   The allocator can emit `_MAX` and then wrap to `_0`, both rejected here. 
This causes `AmbiguousReference` with existing extraction-like columns. Please 
make `add_extracted` skip extraction when the generated alias fails validation, 
update `columns_needed` only afterward, and add a regression test preserving 
the plan and schema.



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

Reply via email to