You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "ozankabak (via GitHub)" <gi...@apache.org> on 2024/07/25 11:16:27 UTC
[PR] Refactor/simplify window frame utils [datafusion]
ozankabak opened a new pull request, #11648:
URL: https://github.com/apache/datafusion/pull/11648
## 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
Reducing three very similar APIs that check/verify/regularize window frames into one.
<!--
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?
Apart from the said API simplification, fixes the window fuzz tests to make it clear when streaming execution is not possible. Also removes some unwrap calls from window related code.
<!--
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?
Yes
<!--
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?
A very minor change at the API level: The `regularize` function becomes a method of `WindowFrame`
<!--
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] Refactor/simplify window frame utils [datafusion]
Posted by "ozankabak (via GitHub)" <gi...@apache.org>.
ozankabak commented on PR #11648:
URL: https://github.com/apache/datafusion/pull/11648#issuecomment-2250205783
Did a Rust version update just land? CI checks were passing in-branch, but we have clippy and doc failures on CI post merge.
--
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] Refactor/simplify window frame utils [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #11648:
URL: https://github.com/apache/datafusion/pull/11648#discussion_r1691295989
##########
datafusion/proto/src/logical_plan/from_proto.rs:
##########
@@ -353,15 +347,15 @@ pub fn parse_expr(
parse_optional_expr(expr.expr.as_deref(), registry, codec)?
.map(|e| vec![e])
.unwrap_or_else(Vec::new);
- Ok(Expr::WindowFunction(WindowFunction::new(
+ Expr::WindowFunction(WindowFunction::new(
expr::WindowFunctionDefinition::AggregateUDF(udaf_function),
args,
))
.partition_by(partition_by)
.order_by(order_by)
.window_frame(window_frame)
.build()
- .unwrap())
Review Comment:
this is a nice improvement too
--
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] Refactor/simplify window frame utils [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #11648:
URL: https://github.com/apache/datafusion/pull/11648#discussion_r1691296939
##########
datafusion/expr/src/window_frame.rs:
##########
@@ -246,59 +246,55 @@ impl WindowFrame {
causal,
}
}
-}
-/// Regularizes ORDER BY clause for window definition for implicit corner cases.
-pub fn regularize_window_order_by(
- frame: &WindowFrame,
- order_by: &mut Vec<Expr>,
-) -> Result<()> {
- if frame.units == WindowFrameUnits::Range && order_by.len() != 1 {
- // Normally, RANGE frames require an ORDER BY clause with exactly one
- // column. However, an ORDER BY clause may be absent or present but with
- // more than one column in two edge cases:
- // 1. start bound is UNBOUNDED or CURRENT ROW
- // 2. end bound is CURRENT ROW or UNBOUNDED.
- // In these cases, we regularize the ORDER BY clause if the ORDER BY clause
- // is absent. If an ORDER BY clause is present but has more than one column,
- // the ORDER BY clause is unchanged. Note that this follows Postgres behavior.
- if (frame.start_bound.is_unbounded()
- || frame.start_bound == WindowFrameBound::CurrentRow)
- && (frame.end_bound == WindowFrameBound::CurrentRow
- || frame.end_bound.is_unbounded())
- {
- // If an ORDER BY clause is absent, it is equivalent to a ORDER BY clause
- // with constant value as sort key.
- // If an ORDER BY clause is present but has more than one column, it is
- // unchanged.
- if order_by.is_empty() {
- order_by.push(Expr::Sort(Sort::new(
- Box::new(Expr::Literal(ScalarValue::UInt64(Some(1)))),
- true,
- false,
- )));
+ /// Regularizes the ORDER BY clause of the window frame.
+ pub fn regularize_order_bys(&self, order_by: &mut Vec<Expr>) -> Result<()> {
+ match self.units {
+ // Normally, RANGE frames require an ORDER BY clause with exactly
+ // one column. However, an ORDER BY clause may be absent or have
+ // more than one column when the start/end bounds are UNBOUNDED or
+ // CURRENT ROW.
+ WindowFrameUnits::Range if self.free_range() => {
+ // If an ORDER BY clause is absent, it is equivalent to an
+ // ORDER BY clause with constant value as sort key. If an
+ // ORDER BY clause is present but has more than one column,
+ // it is unchanged. Note that this follows PostgreSQL behavior.
+ if order_by.is_empty() {
+ order_by.push(Expr::Sort(Sort::new(
+ Box::new(Expr::Literal(ScalarValue::UInt64(Some(1)))),
+ true,
+ false,
+ )));
+ }
+ }
+ WindowFrameUnits::Range if order_by.len() != 1 => {
+ return plan_err!("RANGE requires exactly one ORDER BY column");
+ }
+ WindowFrameUnits::Groups if order_by.is_empty() => {
+ return plan_err!("GROUPS requires an ORDER BY clause");
}
+ _ => {}
}
+ Ok(())
}
- Ok(())
-}
-/// Checks if given window frame is valid. In particular, if the frame is RANGE
-/// with offset PRECEDING/FOLLOWING, it must have exactly one ORDER BY column.
-pub fn check_window_frame(frame: &WindowFrame, order_bys: usize) -> Result<()> {
- if frame.units == WindowFrameUnits::Range && order_bys != 1 {
- // See `regularize_window_order_by`.
- if !(frame.start_bound.is_unbounded()
- || frame.start_bound == WindowFrameBound::CurrentRow)
- || !(frame.end_bound == WindowFrameBound::CurrentRow
- || frame.end_bound.is_unbounded())
- {
- plan_err!("RANGE requires exactly one ORDER BY column")?
+ /// Returns whether the window frame can accept multiple ORDER BY expressons.
Review Comment:
I agree this is much nicer
--
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] Refactor/simplify window frame utils [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #11648:
URL: https://github.com/apache/datafusion/pull/11648#discussion_r1691297827
##########
datafusion/expr/src/window_frame.rs:
##########
@@ -246,59 +246,55 @@ impl WindowFrame {
causal,
}
}
-}
-/// Regularizes ORDER BY clause for window definition for implicit corner cases.
-pub fn regularize_window_order_by(
- frame: &WindowFrame,
- order_by: &mut Vec<Expr>,
-) -> Result<()> {
- if frame.units == WindowFrameUnits::Range && order_by.len() != 1 {
- // Normally, RANGE frames require an ORDER BY clause with exactly one
- // column. However, an ORDER BY clause may be absent or present but with
- // more than one column in two edge cases:
- // 1. start bound is UNBOUNDED or CURRENT ROW
- // 2. end bound is CURRENT ROW or UNBOUNDED.
- // In these cases, we regularize the ORDER BY clause if the ORDER BY clause
- // is absent. If an ORDER BY clause is present but has more than one column,
- // the ORDER BY clause is unchanged. Note that this follows Postgres behavior.
- if (frame.start_bound.is_unbounded()
- || frame.start_bound == WindowFrameBound::CurrentRow)
- && (frame.end_bound == WindowFrameBound::CurrentRow
- || frame.end_bound.is_unbounded())
- {
- // If an ORDER BY clause is absent, it is equivalent to a ORDER BY clause
- // with constant value as sort key.
- // If an ORDER BY clause is present but has more than one column, it is
- // unchanged.
- if order_by.is_empty() {
- order_by.push(Expr::Sort(Sort::new(
- Box::new(Expr::Literal(ScalarValue::UInt64(Some(1)))),
- true,
- false,
- )));
+ /// Regularizes the ORDER BY clause of the window frame.
+ pub fn regularize_order_bys(&self, order_by: &mut Vec<Expr>) -> Result<()> {
+ match self.units {
+ // Normally, RANGE frames require an ORDER BY clause with exactly
+ // one column. However, an ORDER BY clause may be absent or have
+ // more than one column when the start/end bounds are UNBOUNDED or
+ // CURRENT ROW.
+ WindowFrameUnits::Range if self.free_range() => {
+ // If an ORDER BY clause is absent, it is equivalent to an
+ // ORDER BY clause with constant value as sort key. If an
+ // ORDER BY clause is present but has more than one column,
+ // it is unchanged. Note that this follows PostgreSQL behavior.
+ if order_by.is_empty() {
+ order_by.push(Expr::Sort(Sort::new(
+ Box::new(Expr::Literal(ScalarValue::UInt64(Some(1)))),
+ true,
+ false,
+ )));
Review Comment:
FWIW you can write this more succinctly with the expr API like this I think
```suggestion
order_by.push(lit(1u64).sort(true, false));
```
--
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] Refactor/simplify window frame utils [datafusion]
Posted by "ozankabak (via GitHub)" <gi...@apache.org>.
ozankabak commented on code in PR #11648:
URL: https://github.com/apache/datafusion/pull/11648#discussion_r1691307315
##########
datafusion/expr/src/window_frame.rs:
##########
@@ -246,59 +246,55 @@ impl WindowFrame {
causal,
}
}
-}
-/// Regularizes ORDER BY clause for window definition for implicit corner cases.
-pub fn regularize_window_order_by(
- frame: &WindowFrame,
- order_by: &mut Vec<Expr>,
-) -> Result<()> {
- if frame.units == WindowFrameUnits::Range && order_by.len() != 1 {
- // Normally, RANGE frames require an ORDER BY clause with exactly one
- // column. However, an ORDER BY clause may be absent or present but with
- // more than one column in two edge cases:
- // 1. start bound is UNBOUNDED or CURRENT ROW
- // 2. end bound is CURRENT ROW or UNBOUNDED.
- // In these cases, we regularize the ORDER BY clause if the ORDER BY clause
- // is absent. If an ORDER BY clause is present but has more than one column,
- // the ORDER BY clause is unchanged. Note that this follows Postgres behavior.
- if (frame.start_bound.is_unbounded()
- || frame.start_bound == WindowFrameBound::CurrentRow)
- && (frame.end_bound == WindowFrameBound::CurrentRow
- || frame.end_bound.is_unbounded())
- {
- // If an ORDER BY clause is absent, it is equivalent to a ORDER BY clause
- // with constant value as sort key.
- // If an ORDER BY clause is present but has more than one column, it is
- // unchanged.
- if order_by.is_empty() {
- order_by.push(Expr::Sort(Sort::new(
- Box::new(Expr::Literal(ScalarValue::UInt64(Some(1)))),
- true,
- false,
- )));
+ /// Regularizes the ORDER BY clause of the window frame.
+ pub fn regularize_order_bys(&self, order_by: &mut Vec<Expr>) -> Result<()> {
+ match self.units {
+ // Normally, RANGE frames require an ORDER BY clause with exactly
+ // one column. However, an ORDER BY clause may be absent or have
+ // more than one column when the start/end bounds are UNBOUNDED or
+ // CURRENT ROW.
+ WindowFrameUnits::Range if self.free_range() => {
+ // If an ORDER BY clause is absent, it is equivalent to an
+ // ORDER BY clause with constant value as sort key. If an
+ // ORDER BY clause is present but has more than one column,
+ // it is unchanged. Note that this follows PostgreSQL behavior.
+ if order_by.is_empty() {
+ order_by.push(Expr::Sort(Sort::new(
+ Box::new(Expr::Literal(ScalarValue::UInt64(Some(1)))),
+ true,
+ false,
+ )));
Review Comment:
Indeed much nicer, thank you -- added
--
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] Refactor/simplify window frame utils [datafusion]
Posted by "ozankabak (via GitHub)" <gi...@apache.org>.
ozankabak merged PR #11648:
URL: https://github.com/apache/datafusion/pull/11648
--
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