-
Notifications
You must be signed in to change notification settings - Fork 1.5k
RFC: Make it easier to call window functions via expression API (and add example) #6746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -443,7 +443,30 @@ impl AggregateFunction { | |
} | ||
} | ||
|
||
/// Window function | ||
/// Window Function Expression (part of `Expr::WindowFunction`). | ||
/// | ||
/// Holds the actual actual function to call | ||
/// [`window_function::WindowFunction`] as well as its arguments | ||
/// (`args`) and the contents of the `OVER` clause: | ||
/// | ||
/// 1. `PARTITION BY` | ||
/// 2. `ORDER BY` | ||
/// 3. Window frame (e.g. `ROWS 1 PRECEDING AND 1 FOLLOWING`) | ||
/// | ||
/// See [`Self::build`] to create an [`Expr`] | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// # use datafusion_expr::expr::WindowFunction; | ||
/// // Create FIRST_VALUE(a) OVER (PARTITION BY b ORDER BY c) | ||
/// let expr: Expr = WindowFunction::new( | ||
/// BuiltInWindowFunction::FirstValue, | ||
/// vec![col("a")] | ||
/// ) | ||
/// .with_partition_by(vec![col("b")]) | ||
/// .with_order_by(vec![col("b")]) | ||
/// .build(); | ||
/// ``` | ||
#[derive(Clone, PartialEq, Eq, Hash, Debug)] | ||
pub struct WindowFunction { | ||
/// Name of the function | ||
|
@@ -459,22 +482,40 @@ pub struct WindowFunction { | |
} | ||
|
||
impl WindowFunction { | ||
/// Create a new Window expression | ||
pub fn new( | ||
fun: window_function::WindowFunction, | ||
args: Vec<Expr>, | ||
partition_by: Vec<Expr>, | ||
order_by: Vec<Expr>, | ||
window_frame: window_frame::WindowFrame, | ||
) -> Self { | ||
/// Create a new Window expression with the specified argument an | ||
/// empty `OVER` clause | ||
pub fn new(fun: impl Into<window_function::WindowFunction>, args: Vec<Expr>) -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the major change -- so that WindowFunction acts like a |
||
Self { | ||
fun, | ||
fun: fun.into(), | ||
args, | ||
partition_by, | ||
order_by, | ||
window_frame, | ||
partition_by: vec![], | ||
order_by: vec![], | ||
window_frame: window_frame::WindowFrame::new(false), | ||
} | ||
} | ||
|
||
/// set the partition by expressions | ||
pub fn with_partition_by(mut self, partition_by: Vec<Expr>) -> Self { | ||
self.partition_by = partition_by; | ||
self | ||
} | ||
|
||
/// set the order by expressions | ||
pub fn with_order_by(mut self, order_by: Vec<Expr>) -> Self { | ||
self.order_by = order_by; | ||
self | ||
} | ||
|
||
/// set the window frame | ||
pub fn with_window_frame(mut self, window_frame: window_frame::WindowFrame) -> Self { | ||
self.window_frame = window_frame; | ||
self | ||
} | ||
|
||
/// convert this WindowFunction into an [`Expr`] | ||
pub fn build(self) -> Expr { | ||
Expr::WindowFunction(self) | ||
} | ||
} | ||
|
||
// Exists expression. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rest of the PR is motivated by trying to write this example. It turned out to be quite a pain to create the right
Expr
.