You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/06/09 20:58:24 UTC
[GitHub] [arrow-datafusion] alamb opened a new pull request, #6621: Simplify and encapsulate window function state management
alamb opened a new pull request, #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621
# Which issue does this PR close?
Related to #5781
# Rationale for this change
`PartitionEvaluator` currently has some methods that expose an enum `BuiltinWindowState` specific to the built in functions. If we choose to expose `PartitionEvaluator` externally it needs to not include details specifically about the built i window functions
It turns out there is exactly one use for this state, which is memoization (for nth value calculation). We can move that functionality into the trait and avoid the need for `BuiltinWindowState` entirely
# What changes are included in this PR?
1. Move the memoization code to the window function to the window function that uses it (nth_value)
2. Remove `PartitionEvaluator::get_state`, `PartitionEvaluator::set_state()` and `BuiltinWindowState`
# Are these changes tested?
existing tests
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
3. 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 these are all (currently) internal APIs
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mustafasrepo commented on a diff in pull request #6621: Simplify and encapsulate window function state management
Posted by "mustafasrepo (via GitHub)" <gi...@apache.org>.
mustafasrepo commented on code in PR #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621#discussion_r1226191603
##########
datafusion/physical-expr/src/window/nth_value.rs:
##########
@@ -169,9 +164,35 @@ impl PartitionEvaluator for NthValueEvaluator {
Ok(())
}
- fn set_state(&mut self, state: &BuiltinWindowState) -> Result<()> {
- if let BuiltinWindowState::NthValue(nth_value_state) = state {
- self.state = nth_value_state.clone()
+ /// When the window frame has a fixed beginning (e.g UNBOUNDED
+ /// PRECEDING), some functions such as FIRST_VALUE, LAST_VALUE and
+ /// NTH_VALUE we can memoize result. Once result is calculated it
+ /// will always stay same. Hence, we do not need to keep past data
+ /// as we process the entire dataset. This feature enables us to
+ /// prune rows from table. The default implementation does nothing
+ fn memoize(&mut self, state: &mut WindowAggState) -> Result<()> {
+ let out = &state.out_col;
+ let size = out.len();
+ let (is_prunable, new_prunable) = match self.state.kind {
+ NthValueKind::First => {
+ let n_range =
+ state.window_frame_range.end - state.window_frame_range.start;
+ (n_range > 0 && size > 0, true)
Review Comment:
in case we use `is_last` flag, second flag should be `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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #6621: Simplify and encapsulate window function state management
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621#discussion_r1227359136
##########
datafusion/physical-expr/src/window/nth_value.rs:
##########
@@ -169,9 +164,35 @@ impl PartitionEvaluator for NthValueEvaluator {
Ok(())
}
- fn set_state(&mut self, state: &BuiltinWindowState) -> Result<()> {
- if let BuiltinWindowState::NthValue(nth_value_state) = state {
- self.state = nth_value_state.clone()
+ /// When the window frame has a fixed beginning (e.g UNBOUNDED
+ /// PRECEDING), some functions such as FIRST_VALUE, LAST_VALUE and
Review Comment:
Done (BTW if you do "CTRL-G" you can get inline suggestions, to get the github UI to show the suggestion inline):
```suggestion
/// PRECEDING), for some functions such as FIRST_VALUE, LAST_VALUE and
```
<img width="795" alt="Screenshot 2023-06-12 at 8 11 45 PM" src="https://github.com/apache/arrow-datafusion/assets/490673/12c5c298-e825-4839-b068-cf732f3c09c6">
##########
datafusion/physical-expr/src/window/nth_value.rs:
##########
@@ -169,9 +164,35 @@ impl PartitionEvaluator for NthValueEvaluator {
Ok(())
}
- fn set_state(&mut self, state: &BuiltinWindowState) -> Result<()> {
- if let BuiltinWindowState::NthValue(nth_value_state) = state {
- self.state = nth_value_state.clone()
+ /// When the window frame has a fixed beginning (e.g UNBOUNDED
+ /// PRECEDING), some functions such as FIRST_VALUE, LAST_VALUE and
+ /// NTH_VALUE we can memoize result. Once result is calculated it
+ /// will always stay same. Hence, we do not need to keep past data
+ /// as we process the entire dataset. This feature enables us to
+ /// prune rows from table. The default implementation does nothing
+ fn memoize(&mut self, state: &mut WindowAggState) -> Result<()> {
+ let out = &state.out_col;
+ let size = out.len();
+ let (is_prunable, new_prunable) = match self.state.kind {
+ NthValueKind::First => {
+ let n_range =
+ state.window_frame_range.end - state.window_frame_range.start;
+ (n_range > 0 && size > 0, true)
+ }
+ NthValueKind::Last => (true, false),
+ NthValueKind::Nth(n) => {
+ let n_range =
+ state.window_frame_range.end - state.window_frame_range.start;
+ (n_range >= (n as usize) && size >= (n as usize), true)
+ }
+ };
+ if is_prunable {
+ if self.state.finalized_result.is_none() && new_prunable {
Review Comment:
I agree the intent is clearer using the term `is_last`. Thank you for that suggestion . Done in 45427999e2
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #6621: Simplify and encapsulate window function state management
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621#discussion_r1224770664
##########
datafusion/physical-expr/src/window/built_in.rs:
##########
@@ -211,13 +209,7 @@ impl WindowExpr for BuiltInWindowExpr {
state.update(&out_col, partition_batch_state)?;
if self.window_frame.start_bound.is_unbounded() {
- let mut evaluator_state = evaluator.state()?;
- if let BuiltinWindowState::NthValue(nth_value_state) =
- &mut evaluator_state
- {
- memoize_nth_value(state, nth_value_state)?;
- evaluator.set_state(&evaluator_state)?;
- }
+ evaluator.memoize(state)?;
Review Comment:
I think this is actually more readable as well as cleaning up the trait
##########
datafusion/physical-expr/src/window/window_expr.rs:
##########
@@ -327,16 +327,7 @@ pub struct LeadLagState {
pub idx: usize,
}
-#[derive(Debug, Clone, Default)]
Review Comment:
It turns out this structure is not used anywhere else
##########
datafusion/physical-expr/src/window/partition_evaluator.rs:
##########
@@ -100,14 +99,6 @@ pub trait PartitionEvaluator: Debug + Send {
false
}
- /// Returns the internal state of the window function
Review Comment:
The point of the PR is to get of the functions on this trait that serialized / set state (which has a single use)
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb merged pull request #6621: Simplify and encapsulate window function state management
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mustafasrepo commented on a diff in pull request #6621: Simplify and encapsulate window function state management
Posted by "mustafasrepo (via GitHub)" <gi...@apache.org>.
mustafasrepo commented on code in PR #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621#discussion_r1226193242
##########
datafusion/physical-expr/src/window/nth_value.rs:
##########
@@ -169,9 +164,35 @@ impl PartitionEvaluator for NthValueEvaluator {
Ok(())
}
- fn set_state(&mut self, state: &BuiltinWindowState) -> Result<()> {
- if let BuiltinWindowState::NthValue(nth_value_state) = state {
- self.state = nth_value_state.clone()
+ /// When the window frame has a fixed beginning (e.g UNBOUNDED
+ /// PRECEDING), some functions such as FIRST_VALUE, LAST_VALUE and
+ /// NTH_VALUE we can memoize result. Once result is calculated it
+ /// will always stay same. Hence, we do not need to keep past data
+ /// as we process the entire dataset. This feature enables us to
+ /// prune rows from table. The default implementation does nothing
+ fn memoize(&mut self, state: &mut WindowAggState) -> Result<()> {
+ let out = &state.out_col;
+ let size = out.len();
+ let (is_prunable, new_prunable) = match self.state.kind {
+ NthValueKind::First => {
+ let n_range =
+ state.window_frame_range.end - state.window_frame_range.start;
+ (n_range > 0 && size > 0, true)
+ }
+ NthValueKind::Last => (true, false),
+ NthValueKind::Nth(n) => {
+ let n_range =
+ state.window_frame_range.end - state.window_frame_range.start;
+ (n_range >= (n as usize) && size >= (n as usize), true)
+ }
+ };
+ if is_prunable {
+ if self.state.finalized_result.is_none() && new_prunable {
Review Comment:
in case we use `is_last` flag, condition should be `if self.state.finalized_result.is_none() && !is_last {`
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #6621: Simplify and encapsulate window function state management
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621#discussion_r1224777480
##########
datafusion/physical-expr/src/window/partition_evaluator.rs:
##########
@@ -127,13 +117,16 @@ pub trait PartitionEvaluator: Debug + Send {
Ok(())
}
- /// Sets the internal state for window function
+ /// When the window frame has a fixed beginning (e.g UNBOUNDED
Review Comment:
While writing this explanation it occurred to me that the term `memoize`, while technically accurate is not the entire story here. I wonder if there is a better name like `adjust_window_state` 🤔
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mustafasrepo commented on a diff in pull request #6621: Simplify and encapsulate window function state management
Posted by "mustafasrepo (via GitHub)" <gi...@apache.org>.
mustafasrepo commented on code in PR #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621#discussion_r1226191922
##########
datafusion/physical-expr/src/window/nth_value.rs:
##########
@@ -169,9 +164,35 @@ impl PartitionEvaluator for NthValueEvaluator {
Ok(())
}
- fn set_state(&mut self, state: &BuiltinWindowState) -> Result<()> {
- if let BuiltinWindowState::NthValue(nth_value_state) = state {
- self.state = nth_value_state.clone()
+ /// When the window frame has a fixed beginning (e.g UNBOUNDED
+ /// PRECEDING), some functions such as FIRST_VALUE, LAST_VALUE and
+ /// NTH_VALUE we can memoize result. Once result is calculated it
+ /// will always stay same. Hence, we do not need to keep past data
+ /// as we process the entire dataset. This feature enables us to
+ /// prune rows from table. The default implementation does nothing
+ fn memoize(&mut self, state: &mut WindowAggState) -> Result<()> {
+ let out = &state.out_col;
+ let size = out.len();
+ let (is_prunable, new_prunable) = match self.state.kind {
+ NthValueKind::First => {
+ let n_range =
+ state.window_frame_range.end - state.window_frame_range.start;
+ (n_range > 0 && size > 0, true)
+ }
+ NthValueKind::Last => (true, false),
Review Comment:
in case we use `is_last` flag, second flag should be `true`
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mustafasrepo commented on a diff in pull request #6621: Simplify and encapsulate window function state management
Posted by "mustafasrepo (via GitHub)" <gi...@apache.org>.
mustafasrepo commented on code in PR #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621#discussion_r1226191313
##########
datafusion/physical-expr/src/window/nth_value.rs:
##########
@@ -169,9 +164,35 @@ impl PartitionEvaluator for NthValueEvaluator {
Ok(())
}
- fn set_state(&mut self, state: &BuiltinWindowState) -> Result<()> {
- if let BuiltinWindowState::NthValue(nth_value_state) = state {
- self.state = nth_value_state.clone()
+ /// When the window frame has a fixed beginning (e.g UNBOUNDED
+ /// PRECEDING), some functions such as FIRST_VALUE, LAST_VALUE and
+ /// NTH_VALUE we can memoize result. Once result is calculated it
+ /// will always stay same. Hence, we do not need to keep past data
+ /// as we process the entire dataset. This feature enables us to
+ /// prune rows from table. The default implementation does nothing
+ fn memoize(&mut self, state: &mut WindowAggState) -> Result<()> {
+ let out = &state.out_col;
+ let size = out.len();
+ let (is_prunable, new_prunable) = match self.state.kind {
Review Comment:
I know that is is not introduced here (and introduced by me) but while reading the code maybe instead of `new_prunable` we can use `is_last` flag. I think, that way intent will be more clear.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mustafasrepo commented on a diff in pull request #6621: Simplify and encapsulate window function state management
Posted by "mustafasrepo (via GitHub)" <gi...@apache.org>.
mustafasrepo commented on code in PR #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621#discussion_r1226176865
##########
datafusion/physical-expr/src/window/nth_value.rs:
##########
@@ -169,9 +164,35 @@ impl PartitionEvaluator for NthValueEvaluator {
Ok(())
}
- fn set_state(&mut self, state: &BuiltinWindowState) -> Result<()> {
- if let BuiltinWindowState::NthValue(nth_value_state) = state {
- self.state = nth_value_state.clone()
+ /// When the window frame has a fixed beginning (e.g UNBOUNDED
+ /// PRECEDING), some functions such as FIRST_VALUE, LAST_VALUE and
Review Comment:
I think we can replace this line with `PRECEDING), for some functions such as FIRST_VALUE, LAST_VALUE 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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mustafasrepo commented on a diff in pull request #6621: Simplify and encapsulate window function state management
Posted by "mustafasrepo (via GitHub)" <gi...@apache.org>.
mustafasrepo commented on code in PR #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621#discussion_r1226192582
##########
datafusion/physical-expr/src/window/nth_value.rs:
##########
@@ -169,9 +164,35 @@ impl PartitionEvaluator for NthValueEvaluator {
Ok(())
}
- fn set_state(&mut self, state: &BuiltinWindowState) -> Result<()> {
- if let BuiltinWindowState::NthValue(nth_value_state) = state {
- self.state = nth_value_state.clone()
+ /// When the window frame has a fixed beginning (e.g UNBOUNDED
+ /// PRECEDING), some functions such as FIRST_VALUE, LAST_VALUE and
+ /// NTH_VALUE we can memoize result. Once result is calculated it
+ /// will always stay same. Hence, we do not need to keep past data
+ /// as we process the entire dataset. This feature enables us to
+ /// prune rows from table. The default implementation does nothing
+ fn memoize(&mut self, state: &mut WindowAggState) -> Result<()> {
+ let out = &state.out_col;
+ let size = out.len();
+ let (is_prunable, new_prunable) = match self.state.kind {
+ NthValueKind::First => {
+ let n_range =
+ state.window_frame_range.end - state.window_frame_range.start;
+ (n_range > 0 && size > 0, true)
+ }
+ NthValueKind::Last => (true, false),
+ NthValueKind::Nth(n) => {
+ let n_range =
+ state.window_frame_range.end - state.window_frame_range.start;
+ (n_range >= (n as usize) && size >= (n as usize), true)
Review Comment:
In case we use `is_last` flag, second flag should be `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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mustafasrepo commented on a diff in pull request #6621: Simplify and encapsulate window function state management
Posted by "mustafasrepo (via GitHub)" <gi...@apache.org>.
mustafasrepo commented on code in PR #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621#discussion_r1226191603
##########
datafusion/physical-expr/src/window/nth_value.rs:
##########
@@ -169,9 +164,35 @@ impl PartitionEvaluator for NthValueEvaluator {
Ok(())
}
- fn set_state(&mut self, state: &BuiltinWindowState) -> Result<()> {
- if let BuiltinWindowState::NthValue(nth_value_state) = state {
- self.state = nth_value_state.clone()
+ /// When the window frame has a fixed beginning (e.g UNBOUNDED
+ /// PRECEDING), some functions such as FIRST_VALUE, LAST_VALUE and
+ /// NTH_VALUE we can memoize result. Once result is calculated it
+ /// will always stay same. Hence, we do not need to keep past data
+ /// as we process the entire dataset. This feature enables us to
+ /// prune rows from table. The default implementation does nothing
+ fn memoize(&mut self, state: &mut WindowAggState) -> Result<()> {
+ let out = &state.out_col;
+ let size = out.len();
+ let (is_prunable, new_prunable) = match self.state.kind {
+ NthValueKind::First => {
+ let n_range =
+ state.window_frame_range.end - state.window_frame_range.start;
+ (n_range > 0 && size > 0, true)
Review Comment:
in that case second flag should be `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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mustafasrepo commented on a diff in pull request #6621: Simplify and encapsulate window function state management
Posted by "mustafasrepo (via GitHub)" <gi...@apache.org>.
mustafasrepo commented on code in PR #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621#discussion_r1226187241
##########
datafusion/physical-expr/src/window/partition_evaluator.rs:
##########
@@ -127,13 +117,16 @@ pub trait PartitionEvaluator: Debug + Send {
Ok(())
}
- /// Sets the internal state for window function
+ /// When the window frame has a fixed beginning (e.g UNBOUNDED
Review Comment:
I think `adjust_window_state` feels like a more general functionality. However, this function is a special case for `nth_value` when window frame boundary has `UNBOUNDED PRECEDING`. Hence I think `memoize` is better for this case.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #6621: Simplify and encapsulate window function state management
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621#discussion_r1227357856
##########
datafusion/physical-expr/src/window/partition_evaluator.rs:
##########
@@ -127,13 +117,16 @@ pub trait PartitionEvaluator: Debug + Send {
Ok(())
}
- /// Sets the internal state for window function
+ /// When the window frame has a fixed beginning (e.g UNBOUNDED
Review Comment:
I will leave it named `memoize`
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-datafusion] mustafasrepo commented on a diff in pull request #6621: Simplify and encapsulate window function state management
Posted by "mustafasrepo (via GitHub)" <gi...@apache.org>.
mustafasrepo commented on code in PR #6621:
URL: https://github.com/apache/arrow-datafusion/pull/6621#discussion_r1227550120
##########
datafusion/physical-expr/src/window/nth_value.rs:
##########
@@ -169,9 +164,35 @@ impl PartitionEvaluator for NthValueEvaluator {
Ok(())
}
- fn set_state(&mut self, state: &BuiltinWindowState) -> Result<()> {
- if let BuiltinWindowState::NthValue(nth_value_state) = state {
- self.state = nth_value_state.clone()
+ /// When the window frame has a fixed beginning (e.g UNBOUNDED
+ /// PRECEDING), some functions such as FIRST_VALUE, LAST_VALUE and
Review Comment:
Thanks for the tip
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org