You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "Braedon-Wooding-Displayr (via GitHub)" <gi...@apache.org> on 2026/08/01 07:10:42 UTC

[PR] fix: infer placeholder types in GROUP BY, HAVING, QUALIFY and ORDER BY (fix for #24042) [datafusion]

Braedon-Wooding-Displayr opened a new pull request, #24043:
URL: https://github.com/apache/datafusion/pull/24043

   The SELECT list is planned by sql_to_expr, which infers placeholder types. These four clauses are planned by sql_expr_to_logical_expr, which does not, so the same expression written in both places does not compare equal.
   
   The result is that a grouping key containing a placeholder is never matched against the identical SELECT expression, and the columns inside it are reported as ungrouped. The same query with literals in place of the placeholder plans fine. QUALIFY fails differently, on a duplicate field name, because the typed and untyped spellings print alike but are not equal.
   
   Adds a planner test per clause.
   
   ## Which issue does this PR close?
   
   <!--
   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 #24042.
   
   ## 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.  
   -->
   
   I think it's clearly explained above / in issue.
   
   ## 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.
   -->
   
   New tests for each clause case + inferring types in the clauses.
   
   ## Are these changes tested?
   
   <!--
   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)?
   -->
   
   Yes tests are there.
   
   ## 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: infer placeholder types in GROUP BY, HAVING, QUALIFY and ORDER BY (fix for #24042) [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #24043:
URL: https://github.com/apache/datafusion/pull/24043


-- 
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: infer placeholder types in GROUP BY, HAVING, QUALIFY and ORDER BY (fix for #24042) [datafusion]

Posted by "kosiew (via GitHub)" <gi...@apache.org>.
kosiew commented on code in PR #24043:
URL: https://github.com/apache/datafusion/pull/24043#discussion_r3712158987


##########
datafusion/sql/src/expr/order_by.rs:
##########
@@ -109,7 +109,13 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
                     ))
                 }
                 e => {
-                    self.sql_expr_to_logical_expr(e, order_by_schema, planner_context)?
+                    let expr = self.sql_expr_to_logical_expr(
+                        e,
+                        order_by_schema,
+                        planner_context,
+                    )?;
+                    let (expr, _) = expr.infer_placeholder_types(order_by_schema)?;

Review Comment:
   Nice catch applying placeholder inference here. I think this introduces a regression for `DISTINCT ON`, though.
   
   `ORDER BY` expressions are now inferred, but the corresponding `DISTINCT ON` expressions are still planned through `sql_expr_to_logical_expr` without inference (`datafusion/sql/src/select.rs:185-195`). Since `DistinctOn::with_sort_expr` requires structural equality, these expressions no longer compare equal.
   
   For example:
   
   ```sql
   SELECT DISTINCT ON (
       CASE WHEN age < $1 THEN 'young' ELSE 'old' END
   ) first_name
   FROM person
   ORDER BY CASE WHEN age < $1 THEN 'young' ELSE 'old' END;
   ```
   
   This now fails with `SELECT DISTINCT ON expressions must match initial ORDER BY expressions`, whereas before this change both expressions were untyped and matched.
   
   Could we also infer placeholder types for the `DISTINCT ON` expressions after alias substitution and normalization? It would also be great to add this query as a regression test.



##########
datafusion/sql/tests/sql_integration.rs:
##########
@@ -1504,6 +1504,85 @@ fn select_aggregate_with_group_by_with_having_using_count_star_not_in_select() {
     );
 }
 
+/// An expression containing a placeholder, written in both the SELECT list and
+/// the GROUP BY, has to be recognised as one expression the way its literal
+/// equivalent is. Otherwise the columns inside it read as ungrouped, because the
+/// SELECT list has its placeholder types inferred and the grouping key does not.
+#[test]
+fn select_aggregate_with_group_by_placeholder_expression() {

Review Comment:
   These snapshot tests demonstrate that planning succeeds, which is great. One small suggestion would be to add a focused structural assertion that `$1` is inferred as the type of `person.age`. That would make the placeholder inference contract explicit instead of only verifying it indirectly through expression equality.



-- 
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: infer placeholder types in GROUP BY, HAVING, QUALIFY and ORDER BY (fix for #24042) [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #24043:
URL: https://github.com/apache/datafusion/pull/24043#issuecomment-5258646807

   Thanks @kosiew and @


-- 
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: infer placeholder types in GROUP BY, HAVING, QUALIFY and ORDER BY (fix for #24042) [datafusion]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #24043:
URL: https://github.com/apache/datafusion/pull/24043#issuecomment-5151863826

   ## [Codecov](https://app.codecov.io/gh/apache/datafusion/pull/24043?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `58.82353%` with `7 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 80.85%. Comparing base ([`f2b4835`](https://app.codecov.io/gh/apache/datafusion/commit/f2b483598b23e1557a0f25097df55191f36a7a41?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`c25ee4d`](https://app.codecov.io/gh/apache/datafusion/commit/c25ee4d6487577bfdd68a37452c2cda5a864131a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 3 commits behind head on main.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/datafusion/pull/24043?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [datafusion/sql/src/select.rs](https://app.codecov.io/gh/apache/datafusion/pull/24043?src=pr&el=tree&filepath=datafusion%2Fsql%2Fsrc%2Fselect.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZGF0YWZ1c2lvbi9zcWwvc3JjL3NlbGVjdC5ycw==) | 50.00% | [0 Missing and 5 partials :warning: ](https://app.codecov.io/gh/apache/datafusion/pull/24043?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [datafusion/sql/src/expr/order\_by.rs](https://app.codecov.io/gh/apache/datafusion/pull/24043?src=pr&el=tree&filepath=datafusion%2Fsql%2Fsrc%2Fexpr%2Forder_by.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZGF0YWZ1c2lvbi9zcWwvc3JjL2V4cHIvb3JkZXJfYnkucnM=) | 71.42% | [1 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/datafusion/pull/24043?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@            Coverage Diff             @@
   ##             main   #24043      +/-   ##
   ==========================================
   - Coverage   80.85%   80.85%   -0.01%     
   ==========================================
     Files        1101     1101              
     Lines      375467   375481      +14     
     Branches   375467   375481      +14     
   ==========================================
   + Hits       303596   303600       +4     
   - Misses      53777    53778       +1     
   - Partials    18094    18103       +9     
   ```
   </details>
   
   [:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/datafusion/pull/24043?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
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: infer placeholder types in GROUP BY, HAVING, QUALIFY and ORDER BY (fix for #24042) [datafusion]

Posted by "Braedon-Wooding-Displayr (via GitHub)" <gi...@apache.org>.
Braedon-Wooding-Displayr commented on PR #24043:
URL: https://github.com/apache/datafusion/pull/24043#issuecomment-5225514700

   Sorry for delay @kosiew 
   
   Fixes have been pushed.  I appreciate the review a lot.


-- 
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: infer placeholder types in GROUP BY, HAVING, QUALIFY and ORDER BY (fix for #24042) [datafusion]

Posted by "Braedon-Wooding-Displayr (via GitHub)" <gi...@apache.org>.
Braedon-Wooding-Displayr commented on PR #24043:
URL: https://github.com/apache/datafusion/pull/24043#issuecomment-5161258518

   Most of the missing coverage seems to just be error propagation?  Which should be okay.


-- 
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: infer placeholder types in GROUP BY, HAVING, QUALIFY and ORDER BY (fix for #24042) [datafusion]

Posted by "Braedon-Wooding-Displayr (via GitHub)" <gi...@apache.org>.
Braedon-Wooding-Displayr commented on code in PR #24043:
URL: https://github.com/apache/datafusion/pull/24043#discussion_r3740418145


##########
datafusion/sql/tests/sql_integration.rs:
##########
@@ -1504,6 +1504,85 @@ fn select_aggregate_with_group_by_with_having_using_count_star_not_in_select() {
     );
 }
 
+/// An expression containing a placeholder, written in both the SELECT list and
+/// the GROUP BY, has to be recognised as one expression the way its literal
+/// equivalent is. Otherwise the columns inside it read as ungrouped, because the
+/// SELECT list has its placeholder types inferred and the grouping key does not.
+#[test]
+fn select_aggregate_with_group_by_placeholder_expression() {

Review Comment:
   Yes that makes perfect sense, I've done this.



##########
datafusion/sql/src/expr/order_by.rs:
##########
@@ -109,7 +109,13 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
                     ))
                 }
                 e => {
-                    self.sql_expr_to_logical_expr(e, order_by_schema, planner_context)?
+                    let expr = self.sql_expr_to_logical_expr(
+                        e,
+                        order_by_schema,
+                        planner_context,
+                    )?;
+                    let (expr, _) = expr.infer_placeholder_types(order_by_schema)?;

Review Comment:
   Great catch, I've done this.



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