Skip to content
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

fix: preserve expression names when replacing placeholders #12126

Merged
merged 3 commits into from
Aug 25, 2024

Conversation

jonahgao
Copy link
Member

Which issue does this PR close?

Closes #12065.

Rationale for this change

When replacing placeholders with literal values, the names of the expressions might change, but there are references to the old names in the parent plan.

What changes are included in this PR?

  • Preserve expression names when replacing placeholders.
  • Move NamePreserver to datafusion-expr package because it is needed there.

Are these changes tested?

Yes

Are there any user-facing changes?

After replacing the parameters, the logical plan might differ from before because aliases have been added.

@github-actions github-actions bot added sql SQL Planner logical-expr Logical plan and expressions optimizer Optimizer rules core Core DataFusion crate labels Aug 23, 2024

// Test issue: https://github.com/apache/datafusion/issues/12065
#[tokio::test]
async fn filtered_aggr_with_param_values() -> Result<()> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test on the main branch gave the following error

Error: Context("type_coercion", SchemaError(FieldNotFound { field: 
    Column { relation: None, name: "count(table1.c2) FILTER (WHERE table1.c3 > $1)" }, 

valid_fields: [
    Column { relation: None, name: "count(table1.c2) FILTER (WHERE table1.c3 > UInt64(10))" }
] }, Some("")))

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 -- this is a very elegant solution @jonahgao. Thank you very much.

FYI @karlovnv

@@ -28,6 +28,10 @@ use datafusion_expr::{logical_plan::LogicalPlan, Expr, Operator};

use log::{debug, trace};

/// Re-export of `NamesPreserver` for backwards compatibility,
/// as it was initially placed here and then moved elsewhere.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -279,6 +279,57 @@ where
expr.alias_if_changed(original_name)
}

/// Handles ensuring the name of rewritten expressions is not changed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @findepi here is an example of using Expr::Alias as described in #1468 (comment)

@karlovnv
Copy link

@jonahgao @alamb Amazing!
A query like select count (case when f_1 > $1 then f_0 else null end) from table1 for now is working as well!

@alamb alamb merged commit 945902d into apache:main Aug 25, 2024
24 checks passed
@alamb
Copy link
Contributor

alamb commented Aug 25, 2024

Thanks again @jonahgao

@jonahgao jonahgao deleted the fix_parameter branch August 25, 2024 12:39
berkaysynnada pushed a commit to synnada-ai/datafusion-upstream that referenced this pull request Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate logical-expr Logical plan and expressions optimizer Optimizer rules sql SQL Planner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Aggregations with parametrized FILTER WHERE clauses don't work
3 participants