-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Introduce expr builder for aggregate function #10560
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
Changes from 3 commits
f527e13
698ab8f
6994621
3c10e71
d2fc1d1
722e6dc
f2fc8d5
a471385
fc5a6a5
d547853
980c30c
a851efc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use datafusion::{ | ||
execution::{ | ||
config::SessionConfig, | ||
context::{SessionContext, SessionState}, | ||
}, | ||
functions_aggregate::{expr_fn::first_value, register_all}, | ||
}; | ||
|
||
use datafusion_common::Result; | ||
use datafusion_expr::{col, AggregateExt}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let ctx = SessionContext::new(); | ||
let config = SessionConfig::new(); | ||
let mut state = SessionState::new_with_config_rt(config, ctx.runtime_env()); | ||
let _ = register_all(&mut state); | ||
|
||
let first_value_udaf = state.aggregate_functions().get("first_value").unwrap(); | ||
let first_value_builder = first_value_udaf | ||
jayzhan211 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.call(vec![col("a")]) | ||
.order_by(vec![col("b")]) | ||
.build()?; | ||
|
||
let first_value_fn = first_value(col("a"), Some(vec![col("b")])); | ||
assert_eq!(first_value_builder, first_value_fn); | ||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -31,20 +31,28 @@ use datafusion_common::{ | |||||||
use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs}; | ||||||||
use datafusion_expr::utils::{format_state_name, AggregateOrderSensitivity}; | ||||||||
use datafusion_expr::{ | ||||||||
Accumulator, AggregateUDFImpl, ArrayFunctionSignature, Signature, TypeSignature, | ||||||||
Volatility, | ||||||||
Accumulator, AggregateExt, AggregateUDFImpl, ArrayFunctionSignature, Expr, Signature, | ||||||||
TypeSignature, Volatility, | ||||||||
}; | ||||||||
use datafusion_physical_expr_common::aggregate::utils::get_sort_options; | ||||||||
use datafusion_physical_expr_common::sort_expr::{ | ||||||||
limited_convert_logical_sort_exprs_to_physical, LexOrdering, PhysicalSortExpr, | ||||||||
}; | ||||||||
|
||||||||
make_udaf_expr_and_func!( | ||||||||
FirstValue, | ||||||||
first_value, | ||||||||
"Returns the first value in a group of values.", | ||||||||
first_value_udaf | ||||||||
); | ||||||||
create_func!(FirstValue, first_value_udaf); | ||||||||
|
||||||||
/// Returns the first value in a group of values. | ||||||||
pub fn first_value(expression: Expr, order_by: Option<Vec<Expr>>) -> Expr { | ||||||||
if let Some(order_by) = order_by { | ||||||||
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. that is pretty cool indeed |
||||||||
first_value_udaf() | ||||||||
.call(vec![expression]) | ||||||||
.order_by(order_by) | ||||||||
.build() | ||||||||
.unwrap() | ||||||||
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. I think it is fine to unwrap since udaf.call() is guaranteed to be Expr::AggregateFunction 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. 100% agree
Suggested change
|
||||||||
} else { | ||||||||
first_value_udaf().call(vec![expression]) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
pub struct FirstValue { | ||||||||
signature: Signature, | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,45 +48,25 @@ macro_rules! make_udaf_expr_and_func { | |
None, | ||
)) | ||
} | ||
create_func!($UDAF, $AGGREGATE_UDF_FN); | ||
}; | ||
($UDAF:ty, $EXPR_FN:ident, $($arg:ident)*, $distinct:ident, $DOC:expr, $AGGREGATE_UDF_FN:ident) => { | ||
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. I plan to introduce another macro for distinct, so we have I change to 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. Instead of Like let agg = count_builder()
.args(col("a"))
.distinct()
.build()? 🤔 |
||
// "fluent expr_fn" style function | ||
#[doc = $DOC] | ||
pub fn $EXPR_FN( | ||
$($arg: datafusion_expr::Expr,)* | ||
distinct: bool, | ||
) -> datafusion_expr::Expr { | ||
datafusion_expr::Expr::AggregateFunction(datafusion_expr::expr::AggregateFunction::new_udf( | ||
$AGGREGATE_UDF_FN(), | ||
vec![$($arg),*], | ||
distinct, | ||
None, | ||
None, | ||
None | ||
)) | ||
} | ||
|
||
create_func!($UDAF, $AGGREGATE_UDF_FN); | ||
}; | ||
($UDAF:ty, $EXPR_FN:ident, $DOC:expr, $AGGREGATE_UDF_FN:ident) => { | ||
// "fluent expr_fn" style function | ||
#[doc = $DOC] | ||
pub fn $EXPR_FN( | ||
args: Vec<datafusion_expr::Expr>, | ||
distinct: bool, | ||
filter: Option<Box<datafusion_expr::Expr>>, | ||
order_by: Option<Vec<datafusion_expr::Expr>>, | ||
null_treatment: Option<sqlparser::ast::NullTreatment> | ||
) -> datafusion_expr::Expr { | ||
datafusion_expr::Expr::AggregateFunction(datafusion_expr::expr::AggregateFunction::new_udf( | ||
$AGGREGATE_UDF_FN(), | ||
args, | ||
distinct, | ||
filter, | ||
order_by, | ||
null_treatment, | ||
false, | ||
None, | ||
None, | ||
None, | ||
)) | ||
} | ||
|
||
create_func!($UDAF, $AGGREGATE_UDF_FN); | ||
}; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.