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


##########
datafusion/optimizer/src/extract_leaf_expressions.rs:
##########
@@ -71,12 +71,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:
   Canonical user aliases still allow guarded division to execute before 
filtering, while generated `_MAX` aliases are rejected and can cause schema 
ambiguity. Please classify by prefix and 
`alias.expr.placement().should_push_to_leaves()`, retaining numeric parsing 
only for seeding. Add regressions for both failures.



##########
datafusion/optimizer/src/extract_leaf_expressions.rs:
##########
@@ -1238,7 +1250,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:
   Please update the rustdoc to describe prefix and expression-based 
classification rather than canonical numeric suffixes. Reserve numeric suffix 
validation for generator seeding.



##########
datafusion/sqllogictest/test_files/projection_pushdown.slt:
##########
@@ -1949,6 +1949,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:
   Please add a regression combining `id > 0`, a struct-field extraction 
predicate, and guarded division. Assert both the correct result and an 
`EXPLAIN` plan showing leaf extraction.



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