Skip to content

Nested correlated subquery error with a depth exceeding 1 #15558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Tracked by #5483
irenjj opened this issue Apr 3, 2025 · 4 comments · May be fixed by #16060
Open
Tracked by #5483

Nested correlated subquery error with a depth exceeding 1 #15558

irenjj opened this issue Apr 3, 2025 · 4 comments · May be fixed by #16060
Assignees
Labels
bug Something isn't working

Comments

@irenjj
Copy link
Contributor

irenjj commented Apr 3, 2025

Describe the bug

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    --------┘
    )
);

Query execution fails for correlated subqueries with a depth exceeding 1, generation error as follows:

Schema error: No field named e1.dept_id. Did you mean 'e3.dept_id'?.

To Reproduce

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

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

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

Expected behavior

duckdb can run it well:

D 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
      )
  );
┌───────────────┬───────────────┐
│ employee_name │    salary     │
│    varchar    │ decimal(18,3) │
├───────────────┴───────────────┤
│            0 rows             │
└───────────────────────────────┘

Additional context

No response

@irenjj irenjj added the bug Something isn't working label Apr 3, 2025
@irenjj
Copy link
Contributor Author

irenjj commented Apr 3, 2025

take

@alamb
Copy link
Contributor

alamb commented Apr 17, 2025

I think support for this kind of query will require a more unified approach, such as the one described by @duongcongtoai in

@alamb
Copy link
Contributor

alamb commented May 15, 2025

There is a related discussion in discord here: https://discord.com/channels/885562378132000778/1372147109679075328/1372395271442665492

@alamb
Copy link
Contributor

alamb commented May 18, 2025

I recommend we continue the discussion on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants