Skip to content

Expr::apply_children/map_children & LogicalPlan::map_expressions does not correctly map subquery outer_ref_column expressions #16147

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
phillipleblanc opened this issue May 22, 2025 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@phillipleblanc
Copy link
Contributor

Describe the bug

The TreeNode implementation for Expr does not handle the outer_ref_column expressions present in the Subquery nodes of Expr::InSubquery, Expr::ScalarSubquery, and Expr::Exists.

The LogicalPlan::map_expressions implementation does not handle the outer_ref_column expression in LogicalPlan::Subquery

To Reproduce

Create a LogicalPlan from a SQL statement with a subquery expression that has an outer column reference, i.e.

SELECT 
    e.employee_id,
    e.first_name,
    e.salary
FROM 
    employees e
WHERE 
    e.salary > (
        SELECT AVG(salary)
        FROM employees e2
        WHERE e2.department_id = e.department_id
    );

I would expect to be able to do something like this:

let plan = sql_to_plan(sql);
plan.apply(|plan| {
  plan.apply_expressions(|expr| {
    match expr {
      Expr::OuterReferenceColumn(_, _) => { do something useful }
      _ => Ok(Transformed::no(expr))
    }
  });
});

Expected behavior

The outer_ref_column expressions are part of the TreeNode visitor implementations in both Expr and LogicalPlan

Additional context

I will not work on this, so someone should feel free to take this. There is a workaround if you know about this behavior, which is to match on the expressions/plans which contain these outer ref column expressions.

@phillipleblanc phillipleblanc added the bug Something isn't working label May 22, 2025
@irenjj
Copy link
Contributor

irenjj commented May 23, 2025

take

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

No branches or pull requests

2 participants