Skip to content

Commit 1818efd

Browse files
committed
Update to use WindowFunction::new to set additional parameters for order_by using ExprFunctionExt
1 parent 404f394 commit 1818efd

File tree

4 files changed

+40
-29
lines changed

4 files changed

+40
-29
lines changed

datafusion-examples/examples/advanced_udwf.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,11 @@ async fn main() -> Result<()> {
216216
df.show().await?;
217217

218218
// Now, run the function using the DataFrame API:
219-
let window_expr = smooth_it.call(
220-
vec![col("speed")], // smooth_it(speed)
221-
vec![col("car")], // PARTITION BY car
222-
vec![col("time").sort(true, true)], // ORDER BY time ASC
223-
WindowFrame::new(None),
224-
);
219+
let window_expr = smooth_it.call(vec![col("speed")]) // smooth_it(speed)
220+
.partition_by(vec![col("car")]) // PARTITION BY car
221+
.order_by(vec![col("time").sort(true, true)]) // ORDER BY time ASC
222+
.window_frame(WindowFrame::new(None))
223+
.build()?;
225224
let df = ctx.table("cars").await?.window(vec![window_expr])?;
226225

227226
// print the results

datafusion-examples/examples/simple_udwf.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,11 @@ async fn main() -> Result<()> {
118118
df.show().await?;
119119

120120
// Now, run the function using the DataFrame API:
121-
let window_expr = smooth_it.call(
122-
vec![col("speed")], // smooth_it(speed)
123-
vec![col("car")], // PARTITION BY car
124-
vec![col("time").sort(true, true)], // ORDER BY time ASC
125-
WindowFrame::new(None),
126-
);
121+
let window_expr = smooth_it.call(vec![col("speed")]) // smooth_it(speed)
122+
.partition_by(vec![col("car")]) // PARTITION BY car
123+
.order_by(vec![col("time").sort(true, true)]) // ORDER BY time ASC
124+
.window_frame(WindowFrame::new(None))
125+
.build()?;
127126
let df = ctx.table("cars").await?.window(vec![window_expr])?;
128127

129128
// print the results

datafusion/expr/src/expr.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,27 @@ impl From<Arc<WindowUDF>> for WindowFunctionDefinition {
794794
}
795795

796796
/// Window function
797+
///
798+
/// Holds the actual actual function to call
799+
/// [`window_function::WindowFunction`] as well as its arguments
800+
/// (`args`) and the contents of the `OVER` clause:
801+
///
802+
/// 1. `PARTITION BY`
803+
/// 2. `ORDER BY`
804+
/// 3. Window frame (e.g. `ROWS 1 PRECEDING AND 1 FOLLOWING`)
805+
///
806+
/// See [`Self::build`] to create an [`Expr`]
807+
///
808+
/// # Example
809+
/// ```/// # use datafusion_expr::expr::WindowFunction;
810+
/// // Create FIRST_VALUE(a) OVER (PARTITION BY b ORDER BY c)
811+
/// let expr: Expr = Expr::WindowFunction(
812+
/// WindowFunction::new(BuiltInWindowFunction::FirstValue, vec![col("a")])
813+
/// )
814+
/// .with_partition_by(vec![col("b")])
815+
/// .with_order_by(vec![col("b")])
816+
/// .build()?;
817+
/// ```
797818
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
798819
pub struct WindowFunction {
799820
/// Name of the function
@@ -811,7 +832,8 @@ pub struct WindowFunction {
811832
}
812833

813834
impl WindowFunction {
814-
/// Create a new Window expression
835+
/// Create a new Window expression with the specified argument an
836+
/// empty `OVER` clause
815837
pub fn new(fun: impl Into<WindowFunctionDefinition>, args: Vec<Expr>) -> Self {
816838
Self {
817839
fun: fun.into(),

datafusion/expr/src/udwf.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ use arrow::datatypes::DataType;
2828

2929
use datafusion_common::Result;
3030

31+
use crate::expr::WindowFunction;
3132
use crate::{
3233
function::WindowFunctionSimplification, Expr, PartitionEvaluator,
33-
PartitionEvaluatorFactory, ReturnTypeFunction, Signature, WindowFrame,
34+
PartitionEvaluatorFactory, ReturnTypeFunction, Signature,
3435
};
3536

3637
/// Logical representation of a user-defined window function (UDWF)
@@ -123,28 +124,18 @@ impl WindowUDF {
123124
Self::new_from_impl(AliasedWindowUDFImpl::new(Arc::clone(&self.inner), aliases))
124125
}
125126

126-
/// creates a [`Expr`] that calls the window function given
127-
/// the `partition_by`, `order_by`, and `window_frame` definition
127+
/// creates a [`Expr`] that calls the window function with default
128+
/// values for order_by, partition_by, window_frame. See [`ExprFunctionExt`]
129+
/// for details on setting these values.
128130
///
129131
/// This utility allows using the UDWF without requiring access to
130132
/// the registry, such as with the DataFrame API.
131133
pub fn call(
132134
&self,
133-
args: Vec<Expr>,
134-
partition_by: Vec<Expr>,
135-
order_by: Vec<Expr>,
136-
window_frame: WindowFrame,
137-
) -> Expr {
135+
args: Vec<Expr>) -> Expr {
138136
let fun = crate::WindowFunctionDefinition::WindowUDF(Arc::new(self.clone()));
139137

140-
Expr::WindowFunction(crate::expr::WindowFunction {
141-
fun,
142-
args,
143-
partition_by,
144-
order_by,
145-
window_frame,
146-
null_treatment: None,
147-
})
138+
Expr::WindowFunction(WindowFunction::new(fun, args))
148139
}
149140

150141
/// Returns this function's name

0 commit comments

Comments
 (0)