kumarUjjawal commented on code in PR #24808:
URL: https://github.com/apache/datafusion/pull/24808#discussion_r3893159409


##########
datafusion/sql/src/unparser/plan.rs:
##########
@@ -451,6 +457,92 @@ impl Unparser<'_> {
         }
     }
 
+    /// Return the alias recursion would assign when `plan` must become a
+    /// derived relation below an already rendered projection.
+    fn derived_input_alias(plan: &LogicalPlan) -> Option<&'static str> {
+        match plan {
+            LogicalPlan::Projection(_) => Some("derived_projection"),
+            LogicalPlan::Limit(_) => Some("derived_limit"),
+            LogicalPlan::Sort(_) => Some("derived_sort"),
+            LogicalPlan::Distinct(_) => Some("derived_distinct"),
+            LogicalPlan::Filter(filter) => {
+                Self::derived_input_alias(filter.input.as_ref())
+            }
+            LogicalPlan::Repartition(repartition) => {
+                Self::derived_input_alias(repartition.input.as_ref())
+            }
+            _ => None,
+        }
+    }
+
+    fn derived_input_scope<'a>(
+        plan: &'a LogicalPlan,
+        select: &SelectBuilder,
+    ) -> Option<DerivedInputScope<'a>> {
+        if select.inside_subquery_alias() {
+            return None;
+        }
+
+        match plan {
+            LogicalPlan::Projection(projection) => {
+                let alias = 
Self::derived_input_alias(projection.input.as_ref())?;

Review Comment:
   `derived_input_scope` examines only the input of this `Projection`. It 
misses this projection when `select.already_projected()` is already true.
   
   For example:
   
   `Projection(lit(1)) -> Filter(ta.id > 0) -> Projection(ta.id) -> TableScan`
   
   The filter sees no scope. The lower projection later becomes 
`derived_projection`, but the SQL keeps `WHERE ta.id > 0` outside it.
   
   Please return the first boundary that the recursive handlers will create for 
the current `SelectBuilder`. also add this manual-plan regression.



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