Skip to content

Commit 86d1ad0

Browse files
migrate count to UDAF
Builtin Count was removed upstream. TBD whether we want to re-implement `count_star` with new API. Ref: apache/datafusion#10893
1 parent 61f5ea3 commit 86d1ad0

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/functions.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use datafusion::functions_aggregate;
3131
use datafusion_common::{Column, ScalarValue, TableReference};
3232
use datafusion_expr::expr::Alias;
3333
use datafusion_expr::{
34-
aggregate_function,
3534
expr::{
3635
find_df_window_func, AggregateFunction, AggregateFunctionDefinition, Sort, WindowFunction,
3736
},
@@ -327,21 +326,29 @@ fn col(name: &str) -> PyResult<PyExpr> {
327326
})
328327
}
329328

330-
/// Create a COUNT(1) aggregate expression
329+
// TODO: do we want to create an equivalent?
330+
// /// Create a COUNT(1) aggregate expression
331+
// #[pyfunction]
332+
// fn count_star() -> PyResult<PyExpr> {
333+
// Ok(PyExpr {
334+
// expr: Expr::AggregateFunction(AggregateFunction {
335+
// func_def: datafusion_expr::expr::AggregateFunctionDefinition::BuiltIn(
336+
// aggregate_function::AggregateFunction::Count,
337+
// ),
338+
// args: vec![lit(1)],
339+
// distinct: false,
340+
// filter: None,
341+
// order_by: None,
342+
// null_treatment: None,
343+
// }),
344+
// })
345+
// }
346+
347+
/// Wrapper for [`functions_aggregate::expr_fn::count`]
348+
/// Count the number of non-null values in the column
331349
#[pyfunction]
332-
fn count_star() -> PyResult<PyExpr> {
333-
Ok(PyExpr {
334-
expr: Expr::AggregateFunction(AggregateFunction {
335-
func_def: datafusion_expr::expr::AggregateFunctionDefinition::BuiltIn(
336-
aggregate_function::AggregateFunction::Count,
337-
),
338-
args: vec![lit(1)],
339-
distinct: false,
340-
filter: None,
341-
order_by: None,
342-
null_treatment: None,
343-
}),
344-
})
350+
fn count(expr: PyExpr) -> PyExpr {
351+
functions_aggregate::expr_fn::count(expr.expr).into()
345352
}
346353

347354
/// Create a CASE WHEN statement with literal WHEN expressions for comparison to the base expression.
@@ -730,7 +737,6 @@ aggregate_function!(
730737
aggregate_function!(array_agg, ArrayAgg);
731738
aggregate_function!(avg, Avg);
732739
aggregate_function!(corr, Correlation);
733-
aggregate_function!(count, Count);
734740
aggregate_function!(grouping, Grouping);
735741
aggregate_function!(max, Max);
736742
aggregate_function!(mean, Avg);
@@ -791,7 +797,7 @@ pub(crate) fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
791797
m.add_wrapped(wrap_pyfunction!(cosh))?;
792798
m.add_wrapped(wrap_pyfunction!(cot))?;
793799
m.add_wrapped(wrap_pyfunction!(count))?;
794-
m.add_wrapped(wrap_pyfunction!(count_star))?;
800+
// m.add_wrapped(wrap_pyfunction!(count_star))?;
795801
m.add_wrapped(wrap_pyfunction!(covar))?;
796802
m.add_wrapped(wrap_pyfunction!(covar_pop))?;
797803
m.add_wrapped(wrap_pyfunction!(covar_samp))?;

0 commit comments

Comments
 (0)