You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "kumarUjjawal (via GitHub)" <gi...@apache.org> on 2025/11/17 12:13:07 UTC
[PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
kumarUjjawal opened a new pull request, #18767:
URL: https://github.com/apache/datafusion/pull/18767
## Which issue does this PR close?
`rewrite_sort_cols_by_agg_alias` was rewriting `ORDER BY min(c2)` to a column named just `min(t.c2)` the rewritten sort expression didn’t match the schema entry so the assertion failed.
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
- Closes #18749
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
## Are these changes tested?
Ran both the module level and project level tests.
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
## Are there any user-facing changes?
No
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on PR #18767:
URL: https://github.com/apache/datafusion/pull/18767#issuecomment-3543324776
@alamb My understanding is the `rewrite_sort_cols_by_agg_alias` test never actually asserted the problematic shape that is a column whose name itself contains dots like the literal alias "min(t.c2)", because it built its expectation with `col("min(t.c2)")`, and `col(...)` normalizes the input into relation/name. That meant we never had a test that expected a flat alias like "min(t.c2)".
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #18767: fix: preserve aggregate alias qualifiers in order-by rewrite
URL: https://github.com/apache/datafusion/pull/18767
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on PR #18767:
URL: https://github.com/apache/datafusion/pull/18767#issuecomment-3543151455
> Thank you @kumarUjjawal 🙏
>
> Can we please figure out why the CI was not failing ? It seems like we have a testing gap and even if this PR fixes the issues, the testing gap will allow it to potentially start failing again
I will investigate further.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #18767:
URL: https://github.com/apache/datafusion/pull/18767#discussion_r2534130528
##########
datafusion/expr/src/expr_rewriter/order_by.rs:
##########
@@ -133,6 +140,29 @@ fn rewrite_in_terms_of_projection(
.data()
}
+fn ensure_column_qualifiers(expr: Expr) -> Expr {
+ match expr {
+ Expr::Column(column) => Expr::Column(qualify_column(column)),
+ Expr::Alias(mut alias) => {
+ alias.expr = Box::new(ensure_column_qualifiers(*alias.expr));
+ Expr::Alias(alias)
+ }
+ other => other,
+ }
+}
+
+fn qualify_column(column: Column) -> Column {
+ if column.relation.is_some() {
Review Comment:
```suggestion
if column.relation.is_some() || !column.name.contains('.') {
```
##########
datafusion/expr/src/expr_rewriter/order_by.rs:
##########
@@ -133,6 +140,29 @@ fn rewrite_in_terms_of_projection(
.data()
}
+fn ensure_column_qualifiers(expr: Expr) -> Expr {
+ match expr {
+ Expr::Column(column) => Expr::Column(qualify_column(column)),
+ Expr::Alias(mut alias) => {
+ alias.expr = Box::new(ensure_column_qualifiers(*alias.expr));
+ Expr::Alias(alias)
+ }
+ other => other,
Review Comment:
What about expressions which contain nested columns ?
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #18767:
URL: https://github.com/apache/datafusion/pull/18767#discussion_r2536562283
##########
datafusion/expr/src/expr_rewriter/order_by.rs:
##########
@@ -363,4 +363,59 @@ mod test {
let nulls_first = true;
expr.sort(asc, nulls_first)
}
+
+ #[test]
+ fn qualify_column_handles_dotted_and_simple_names() {
+ let qualified =
+ Column::new(Some(TableReference::Bare { table: "t".into() }), "c1");
+ assert_eq!(qualify_column(qualified.clone()), qualified);
+
+ let simple = Column::new_unqualified("alias_without_dot");
+ assert_eq!(qualify_column(simple.clone()), simple);
+
+ let dotted = Column::new_unqualified("min(t.c2)");
+ let parsed = qualify_column(dotted);
+ assert_eq!(
+ parsed.relation,
+ Some(TableReference::Bare {
+ table: "min(t".into()
+ })
+ );
+ assert_eq!(parsed.name, "c2)");
Review Comment:
I think you might have reviewed a earlier version of my code because I have reverted to simply reusing the projection’s alias string, so we no longer invent table references like min(t).
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18767:
URL: https://github.com/apache/datafusion/pull/18767#issuecomment-3978868251
Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #18767:
URL: https://github.com/apache/datafusion/pull/18767#discussion_r2536471463
##########
datafusion/expr/src/expr_rewriter/order_by.rs:
##########
@@ -363,4 +363,59 @@ mod test {
let nulls_first = true;
expr.sort(asc, nulls_first)
}
+
+ #[test]
+ fn qualify_column_handles_dotted_and_simple_names() {
+ let qualified =
+ Column::new(Some(TableReference::Bare { table: "t".into() }), "c1");
+ assert_eq!(qualify_column(qualified.clone()), qualified);
+
+ let simple = Column::new_unqualified("alias_without_dot");
+ assert_eq!(qualify_column(simple.clone()), simple);
+
+ let dotted = Column::new_unqualified("min(t.c2)");
+ let parsed = qualify_column(dotted);
+ assert_eq!(
+ parsed.relation,
+ Some(TableReference::Bare {
+ table: "min(t".into()
+ })
+ );
+ assert_eq!(parsed.name, "c2)");
Review Comment:
Let's see what others think about this.
For me the parsing is broken if `min(` appears in the table (reference) name and `)` appears in the plain column name.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on PR #18767:
URL: https://github.com/apache/datafusion/pull/18767#issuecomment-3547357616
> @alamb My understanding is the `rewrite_sort_cols_by_agg_alias` test never actually asserted the problematic shape that is a column whose name itself contains dots like the literal alias "min(t.c2)", because it built its expectation with `col("min(t.c2)")`, and `col(...)` normalizes the input into relation/name. That meant we never had a test that expected a flat alias like "min(t.c2)".
But how does this fit with CI succeeding and `cargo test` as a whole succeeding, but running `cargo test -p datafusion-expr` itself causes this error? 🤔
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on PR #18767:
URL: https://github.com/apache/datafusion/pull/18767#issuecomment-3542434353
@martin-g Please check. Thanks!
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #18767:
URL: https://github.com/apache/datafusion/pull/18767#discussion_r2536927639
##########
datafusion/expr/src/expr_rewriter/order_by.rs:
##########
@@ -235,18 +241,18 @@ mod test {
TestCase {
desc: r#"min(c2) --> "min(c2)" -- (column *named* "min(t.c2)"!)"#,
input: sort(min(col("c2"))),
- expected: sort(col("min(t.c2)")),
+ expected: sort(derived_col("min(t.c2)")),
},
TestCase {
desc: r#"c1 + min(c2) --> "c1 + min(c2)" -- (column *named* "min(t.c2)"!)"#,
Review Comment:
```suggestion
desc: r#"c1 + min(c2) --> "c1 + min(t.c2)" -- (column *named* "min(t.c2)"!)"#,
```
##########
datafusion/expr/src/expr_rewriter/order_by.rs:
##########
@@ -235,18 +241,18 @@ mod test {
TestCase {
desc: r#"min(c2) --> "min(c2)" -- (column *named* "min(t.c2)"!)"#,
Review Comment:
```suggestion
desc: r#"min(c2) --> "min(t.c2)" -- (column *named* "min(t.c2)"!)"#,
```
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #18767:
URL: https://github.com/apache/datafusion/pull/18767#discussion_r2536937660
##########
datafusion/expr/src/expr_rewriter/order_by.rs:
##########
@@ -363,4 +363,59 @@ mod test {
let nulls_first = true;
expr.sort(asc, nulls_first)
}
+
+ #[test]
+ fn qualify_column_handles_dotted_and_simple_names() {
+ let qualified =
+ Column::new(Some(TableReference::Bare { table: "t".into() }), "c1");
+ assert_eq!(qualify_column(qualified.clone()), qualified);
+
+ let simple = Column::new_unqualified("alias_without_dot");
+ assert_eq!(qualify_column(simple.clone()), simple);
+
+ let dotted = Column::new_unqualified("min(t.c2)");
+ let parsed = qualify_column(dotted);
+ assert_eq!(
+ parsed.relation,
+ Some(TableReference::Bare {
+ table: "min(t".into()
+ })
+ );
+ assert_eq!(parsed.name, "c2)");
Review Comment:
Indeed! It seems I was talking about a previous state of this PR.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #18767:
URL: https://github.com/apache/datafusion/pull/18767#issuecomment-3543120874
Thank you @kumarUjjawal 🙏
Can we please figure out why the CI was not failing ? It seems like we have a testing gap and even if this PR fixes the issues, the testing gap will allow it to potentially start failing again
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #18767:
URL: https://github.com/apache/datafusion/pull/18767#discussion_r2535403721
##########
datafusion/expr/src/expr_rewriter/order_by.rs:
##########
@@ -363,4 +363,59 @@ mod test {
let nulls_first = true;
expr.sort(asc, nulls_first)
}
+
+ #[test]
+ fn qualify_column_handles_dotted_and_simple_names() {
+ let qualified =
+ Column::new(Some(TableReference::Bare { table: "t".into() }), "c1");
+ assert_eq!(qualify_column(qualified.clone()), qualified);
+
+ let simple = Column::new_unqualified("alias_without_dot");
+ assert_eq!(qualify_column(simple.clone()), simple);
+
+ let dotted = Column::new_unqualified("min(t.c2)");
+ let parsed = qualify_column(dotted);
+ assert_eq!(
+ parsed.relation,
+ Some(TableReference::Bare {
+ table: "min(t".into()
Review Comment:
Is `min(` expected here ?!
I'd expect just `t`
##########
datafusion/expr/src/expr_rewriter/order_by.rs:
##########
@@ -363,4 +363,59 @@ mod test {
let nulls_first = true;
expr.sort(asc, nulls_first)
}
+
+ #[test]
+ fn qualify_column_handles_dotted_and_simple_names() {
+ let qualified =
+ Column::new(Some(TableReference::Bare { table: "t".into() }), "c1");
+ assert_eq!(qualify_column(qualified.clone()), qualified);
+
+ let simple = Column::new_unqualified("alias_without_dot");
+ assert_eq!(qualify_column(simple.clone()), simple);
+
+ let dotted = Column::new_unqualified("min(t.c2)");
+ let parsed = qualify_column(dotted);
+ assert_eq!(
+ parsed.relation,
+ Some(TableReference::Bare {
+ table: "min(t".into()
+ })
+ );
+ assert_eq!(parsed.name, "c2)");
Review Comment:
And here I'd expect just `c2` without the trailing `)`
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: preserve aggregate alias qualifiers in order-by rewrite [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #18767:
URL: https://github.com/apache/datafusion/pull/18767#discussion_r2536279488
##########
datafusion/expr/src/expr_rewriter/order_by.rs:
##########
@@ -363,4 +363,59 @@ mod test {
let nulls_first = true;
expr.sort(asc, nulls_first)
}
+
+ #[test]
+ fn qualify_column_handles_dotted_and_simple_names() {
+ let qualified =
+ Column::new(Some(TableReference::Bare { table: "t".into() }), "c1");
+ assert_eq!(qualify_column(qualified.clone()), qualified);
+
+ let simple = Column::new_unqualified("alias_without_dot");
+ assert_eq!(qualify_column(simple.clone()), simple);
+
+ let dotted = Column::new_unqualified("min(t.c2)");
+ let parsed = qualify_column(dotted);
+ assert_eq!(
+ parsed.relation,
+ Some(TableReference::Bare {
+ table: "min(t".into()
+ })
+ );
+ assert_eq!(parsed.name, "c2)");
Review Comment:
I’ve dropped the whole “split on dot” logic, so we no longer try to invent qualifiers like min(t / c2). The rewrite now just reuses whatever alias string the projection produced (e.g. the flat "min(t.c2)"), and the unit test asserts that exact name using a derived_col("min(t.c2)"). So the expectations you highlighted are gone.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org