alamb opened a new issue, #19816:
URL: https://github.com/apache/datafusion/issues/19816

   ### Is your feature request related to a problem or challenge?
   
   - Broken out of https://github.com/apache/datafusion/issues/15558 from 
@irenjj 
   
   I believe the CMU [optd](https://db.cs.cmu.edu/projects/optd/) project is 
trying to plan multi-level correlated subqueries (specifically @yliang412 👋 )
   
   
   ```sql
   CREATE TABLE employees (
       employee_id INTEGER,
       employee_name VARCHAR,
       dept_id INTEGER,
       salary DECIMAL
   );
   
   CREATE TABLE project_assignments (
       project_id INTEGER,
       employee_id INTEGER,
       priority INTEGER
   );
   ```
   
   Current running this  query fails
   
   
   ```sql
   SELECT e1.employee_name, e1.salary
   FROM employees e1
   WHERE e1.salary > (
       SELECT AVG(e2.salary)
       FROM employees e2 
       WHERE e2.dept_id = e1.dept_id
       AND e2.salary > (
           SELECT AVG(e3.salary)
           FROM employees e3
           WHERE e3.dept_id = e1.dept_id
       )
   );
   ```
   
   
   ```
   Schema error: No field named e1.dept_id. Did you mean 'e3.dept_id'?.
   ```
   
   
   Feedback from @yliang412 :
   
   > I believe it couldn't find the e1.dept_id. Even without unnesting, the 
planner should still correctly infer that `e1.dept_id` is from the employees 
table in the top-level scope. I suspect it has something to do with resolving 
the OuterReferenceColumn when it is not from the immediate outer scope.
   
   
   
   ### Describe the solution you'd like
   
   We would like DataFusion to at least **Bind** the query correctly (aka in 
DataFusion terms, have `SqlToRel` successfully create a LogicalPlan)
   
   Not that successfully executing the query and getting the correct answer 
likely requires more unnesting rules which we don't yet have. This ticket just 
focus on the logical planning of the query and this ticket covers actually 
executing it:
   - https://github.com/apache/datafusion/issues/15558 covers t
   
   
   ### Describe alternatives you've considered
   
   
   @duongcongtoai  looks like they had a PR from a while ago to resolve this 
problem
   - https://github.com/apache/datafusion/pull/18806
   
   ### Additional context
   
   _No response_


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