-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Consistent API to set parameters of aggregate and window functions (AggregateExt
--> ExprFunctionExt
)
#11550
Merged
jayzhan211
merged 22 commits into
apache:main
from
timsaucer:feature/expr-function-extension
Jul 24, 2024
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
76b6655
Moving over AggregateExt to ExprFunctionExt and adding in function se…
timsaucer-may 4b124c6
Switch WindowFrame to only need the window function definition and ar…
timsaucer-may 31a82dd
Changing null_treatment to take an option, but this is mostly for cod…
timsaucer-may d290d2e
Moving functions in ExprFuncBuilder over to be explicitly implementin…
timsaucer-may d267d0e
Apply cargo fmt
timsaucer-may 2e758ad
Add deprecated trait AggregateExt so that users get a warning but sti…
timsaucer fbde31f
Window helper functions should return Expr
timsaucer 99f1c79
Update documentation to show window function example
timsaucer fd9ebdf
Add license info
timsaucer 4344a9f
Update comments that are no longer applicable
timsaucer a154ddc
Remove first_value and last_value since these are already implemented…
timsaucer 64cbc36
Update to use WindowFunction::new to set additional parameters for o…
timsaucer 532f262
Apply cargo fmt
timsaucer 6436499
Merge remote-tracking branch 'apache/main' into feature/expr-function…
alamb acfcece
Fix up clippy
alamb 039f427
fix doc example
alamb 75e364a
fmt
alamb 1a801f6
doc tweaks
alamb d689872
more doc tweaks
alamb 4884c8a
fix up links
alamb 9bfd1dd
fix integration test
alamb 77726eb
fix anothr doc example
alamb 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -28,8 +28,8 @@ use crate::expr_fn::binary_expr; | |
use crate::logical_plan::Subquery; | ||
use crate::utils::expr_to_columns; | ||
use crate::{ | ||
aggregate_function, built_in_window_function, udaf, ExprSchemable, Operator, | ||
Signature, | ||
aggregate_function, built_in_window_function, udaf, BuiltInWindowFunction, | ||
ExprSchemable, Operator, Signature, WindowFrame, WindowUDF, | ||
}; | ||
use crate::{window_frame, Volatility}; | ||
|
||
|
@@ -60,6 +60,10 @@ use sqlparser::ast::NullTreatment; | |
/// use the fluent APIs in [`crate::expr_fn`] such as [`col`] and [`lit`], or | ||
/// methods such as [`Expr::alias`], [`Expr::cast_to`], and [`Expr::Like`]). | ||
/// | ||
/// See also [`ExprFunctionExt`] for creating aggregate and window functions. | ||
/// | ||
/// [`ExprFunctionExt`]: crate::expr_fn::ExprFunctionExt | ||
/// | ||
/// # Schema Access | ||
/// | ||
/// See [`ExprSchemable::get_type`] to access the [`DataType`] and nullability | ||
|
@@ -283,15 +287,17 @@ pub enum Expr { | |
/// This expression is guaranteed to have a fixed type. | ||
TryCast(TryCast), | ||
/// A sort expression, that can be used to sort values. | ||
/// | ||
/// See [Expr::sort] for more details | ||
Sort(Sort), | ||
/// Represents the call of a scalar function with a set of arguments. | ||
ScalarFunction(ScalarFunction), | ||
/// Calls an aggregate function with arguments, and optional | ||
/// `ORDER BY`, `FILTER`, `DISTINCT` and `NULL TREATMENT`. | ||
/// | ||
/// See also [`AggregateExt`] to set these fields. | ||
/// See also [`ExprFunctionExt`] to set these fields. | ||
/// | ||
/// [`AggregateExt`]: crate::udaf::AggregateExt | ||
/// [`ExprFunctionExt`]: crate::expr_fn::ExprFunctionExt | ||
AggregateFunction(AggregateFunction), | ||
/// Represents the call of a window function with arguments. | ||
WindowFunction(WindowFunction), | ||
|
@@ -641,9 +647,9 @@ impl AggregateFunctionDefinition { | |
|
||
/// Aggregate function | ||
/// | ||
/// See also [`AggregateExt`] to set these fields on `Expr` | ||
/// See also [`ExprFunctionExt`] to set these fields on `Expr` | ||
/// | ||
/// [`AggregateExt`]: crate::udaf::AggregateExt | ||
/// [`ExprFunctionExt`]: crate::expr_fn::ExprFunctionExt | ||
#[derive(Clone, PartialEq, Eq, Hash, Debug)] | ||
pub struct AggregateFunction { | ||
/// Name of the function | ||
|
@@ -769,7 +775,52 @@ impl fmt::Display for WindowFunctionDefinition { | |
} | ||
} | ||
|
||
impl From<aggregate_function::AggregateFunction> for WindowFunctionDefinition { | ||
fn from(value: aggregate_function::AggregateFunction) -> Self { | ||
Self::AggregateFunction(value) | ||
} | ||
} | ||
|
||
impl From<BuiltInWindowFunction> for WindowFunctionDefinition { | ||
fn from(value: BuiltInWindowFunction) -> Self { | ||
Self::BuiltInWindowFunction(value) | ||
} | ||
} | ||
|
||
impl From<Arc<crate::AggregateUDF>> for WindowFunctionDefinition { | ||
fn from(value: Arc<crate::AggregateUDF>) -> Self { | ||
Self::AggregateUDF(value) | ||
} | ||
} | ||
|
||
impl From<Arc<WindowUDF>> for WindowFunctionDefinition { | ||
fn from(value: Arc<WindowUDF>) -> Self { | ||
Self::WindowUDF(value) | ||
} | ||
} | ||
|
||
/// Window function | ||
/// | ||
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. ❤️ |
||
/// Holds the actual actual function to call [`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`) | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// # use datafusion_expr::{Expr, BuiltInWindowFunction, col, ExprFunctionExt}; | ||
/// # use datafusion_expr::expr::WindowFunction; | ||
/// // Create FIRST_VALUE(a) OVER (PARTITION BY b ORDER BY c) | ||
/// let expr = Expr::WindowFunction( | ||
/// WindowFunction::new(BuiltInWindowFunction::FirstValue, vec![col("a")]) | ||
/// ) | ||
/// .partition_by(vec![col("b")]) | ||
/// .order_by(vec![col("b").sort(true, true)]) | ||
/// .build() | ||
/// .unwrap(); | ||
/// ``` | ||
#[derive(Clone, PartialEq, Eq, Hash, Debug)] | ||
pub struct WindowFunction { | ||
/// Name of the function | ||
|
@@ -787,22 +838,16 @@ pub struct WindowFunction { | |
} | ||
|
||
impl WindowFunction { | ||
/// Create a new Window expression | ||
pub fn new( | ||
fun: WindowFunctionDefinition, | ||
args: Vec<Expr>, | ||
partition_by: Vec<Expr>, | ||
order_by: Vec<Expr>, | ||
window_frame: window_frame::WindowFrame, | ||
null_treatment: Option<NullTreatment>, | ||
) -> Self { | ||
/// Create a new Window expression with the specified argument an | ||
/// empty `OVER` clause | ||
pub fn new(fun: impl Into<WindowFunctionDefinition>, args: Vec<Expr>) -> Self { | ||
Self { | ||
fun, | ||
fun: fun.into(), | ||
args, | ||
partition_by, | ||
order_by, | ||
window_frame, | ||
null_treatment, | ||
partition_by: Vec::default(), | ||
order_by: Vec::default(), | ||
window_frame: WindowFrame::new(None), | ||
null_treatment: None, | ||
} | ||
} | ||
} | ||
|
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.
this is so much nicer