You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "korowa (via GitHub)" <gi...@apache.org> on 2024/05/26 15:00:21 UTC
[PR] fix: inconsistent group by position planning [datafusion]
korowa opened a new pull request, #10679:
URL: https://github.com/apache/datafusion/pull/10679
## 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 #.
## 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.
-->
While #10591 found that planning for INT literals in GROUP BY clause is inconsistent. For example:
```sql
EXPLAIN SELECT "URL", COUNT(1) from hits GROUP BY 1, 3, 4, 5
```
produces following logical plan (this is logical plan before any optimizations):
```sql
Explain
Projection: hits.URL, COUNT(Int64(1))
Aggregate: groupBy=[[hits.URL, Int64(3), Int64(4), Int64(5)]], aggr=[[COUNT(Int64(1))]]
TableScan: hits
```
`1` resolved as column from select clause, and `3, 4, 5` remain as constants. Don't think this behavior is intended + there are couple of tests in `sql_integration.rs` (modified below), for similar cases, expect queries to fail.
The suggestion is to fail while planning phase for such cases, and return error, specifying first index which can not be resolved as select column.
## 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.
-->
Resolution for positions (integer literals in `GROUP BY` clause) now returns error on first unresolved position.
SIgnature for `resolve_positions_to_exprs` changed to avoid unnecessary cloning. `resolve_aliases_to_exprs` signature has similar changes, for the same reasons (it's been changed just for consistency with resolve_positions_... function).
## 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)?
-->
Added sqllogictests + some test coverage for `resolve_positions_to_exprs`
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
Users might experience query planning failures for `GROUP BY %idx%` where `idx` is out of bounds of `SELECT` expressions list (which seems to be OK from SQL semantics point of view -- PG/MySQL handle this case in the same way).
<!--
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] minor: inconsistent group by position planning [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #10679:
URL: https://github.com/apache/datafusion/pull/10679#discussion_r1615939934
##########
datafusion/sql/src/utils.rs:
##########
@@ -149,47 +149,51 @@ pub(crate) fn extract_aliases(exprs: &[Expr]) -> HashMap<String, Expr> {
}
/// Given an expression that's literal int encoding position, lookup the corresponding expression
-/// in the select_exprs list, if the index is within the bounds and it is indeed a position literal;
-/// Otherwise, return None
+/// in the select_exprs list, if the index is within the bounds and it is indeed a position literal,
+/// otherwise, returns planning error.
+/// If input expression is not an int literal, returns expression as-is.
pub(crate) fn resolve_positions_to_exprs(
- expr: &Expr,
+ expr: Expr,
select_exprs: &[Expr],
-) -> Option<Expr> {
+) -> Result<Expr> {
match expr {
// sql_expr_to_logical_expr maps number to i64
// https://github.com/apache/datafusion/blob/8d175c759e17190980f270b5894348dc4cff9bbf/datafusion/src/sql/planner.rs#L882-L887
Expr::Literal(ScalarValue::Int64(Some(position)))
- if position > &0_i64 && position <= &(select_exprs.len() as i64) =>
+ if position > 0_i64 && position <= select_exprs.len() as i64 =>
{
let index = (position - 1) as usize;
let select_expr = &select_exprs[index];
- Some(match select_expr {
+ Ok(match select_expr {
Expr::Alias(Alias { expr, .. }) => *expr.clone(),
_ => select_expr.clone(),
})
}
- _ => None,
+ Expr::Literal(ScalarValue::Int64(Some(position))) => plan_err!(
+ "Cannot find column with position {} in SELECT clause",
Review Comment:
The fact that these positions are 1 based sometimes is confusing for people who might expect them to be zero based. I suggest we improve the message with the valid values. For example:
```suggestion
"Cannot find column with position {} in SELECT clause. Valid columns: 1 to {}",
select_exprs.len()
```
--
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] minor: inconsistent group by position planning [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #10679:
URL: https://github.com/apache/datafusion/pull/10679#issuecomment-2134165403
Thanks again @korowa and @jonahgao
--
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] minor: inconsistent group by position planning [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #10679:
URL: https://github.com/apache/datafusion/pull/10679
--
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