Skip to content
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

Minor: Extend more style of udaf expr_fn, Remove order args forcovar_samp and covar_pop #10492

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions datafusion/functions-aggregate/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,55 @@
// specific language governing permissions and limitations
// under the License.

// 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.

macro_rules! make_udaf_expr_and_func {
($UDAF:ty, $EXPR_FN:ident, $($arg:ident)*, $DOC:expr, $AGGREGATE_UDF_FN:ident) => {
// "fluent expr_fn" style function
#[doc = $DOC]
pub fn $EXPR_FN(
$($arg: datafusion_expr::Expr,)*
) -> datafusion_expr::Expr {
datafusion_expr::Expr::AggregateFunction(datafusion_expr::expr::AggregateFunction::new_udf(
$AGGREGATE_UDF_FN(),
vec![$($arg),*],
false,
None,
None,
None,
))
}
create_func!($UDAF, $AGGREGATE_UDF_FN);
};
($UDAF:ty, $EXPR_FN:ident, $($arg:ident)*, $distinct:ident, $DOC:expr, $AGGREGATE_UDF_FN:ident) => {
// "fluent expr_fn" style function
#[doc = $DOC]
pub fn $EXPR_FN(
$($arg: 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(),
vec![$($arg),*],
distinct,
filter,
order_by,
null_treatment,
None,
None,
None
))
}
create_func!($UDAF, $AGGREGATE_UDF_FN);
Expand Down
4 changes: 2 additions & 2 deletions datafusion/proto/tests/cases/roundtrip_logical_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,8 @@ async fn roundtrip_expr_api() -> Result<()> {
),
array_replace_all(make_array(vec![lit(1), lit(2), lit(3)]), lit(2), lit(4)),
first_value(vec![lit(1)], false, None, None, None),
covar_samp(lit(1.5), lit(2.2), false, None, None, None),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change now means the expr_fn can't be used with ordering / null_treatment right? I think that is probably ok

Perhaps we can consider some better API for creating aggregate functions with ORDER BY, perhaps inspired by #6747

Perhaps something like

// form `FIRST_VALUE(a ORDER BY b)`
let agg_expr = AggregateUDF::call(first_value())
  .args(col("a"))
  .order_by(vec![col("b"))
  .build()?;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change now means the expr_fn can't be used with ordering / null_treatment right? I think that is probably ok

Perhaps we can consider some better API for creating aggregate functions with ORDER BY, perhaps inspired by #6747

Perhaps something like

// form `FIRST_VALUE(a ORDER BY b)`
let agg_expr = AggregateUDF::call(first_value())
  .args(col("a"))
  .order_by(vec![col("b"))
  .build()?;

This looks nice

covar_pop(lit(1.5), lit(2.2), true, None, None, None),
covar_samp(lit(1.5), lit(2.2)),
covar_pop(lit(1.5), lit(2.2)),
];

// ensure expressions created with the expr api can be round tripped
Expand Down