Skip to content

Commit d370149

Browse files
test(ffi): check foreign min(DISTINCT) drops DISTINCT in the optimized plan
1 parent e83de0b commit d370149

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

‎datafusion/ffi/src/tests/mod.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ use datafusion_physical_expr::{
3636
use datafusion_physical_plan::{ExecutionPlan, Partitioning};
3737
use sync_provider::create_sync_table_provider;
3838
use udf_udaf_udwf::{
39-
create_ffi_abs_func, create_ffi_first_value_func, create_ffi_random_func,
40-
create_ffi_rank_func, create_ffi_stddev_func, create_ffi_sum_func,
41-
create_ffi_table_func,
39+
create_ffi_abs_func, create_ffi_first_value_func, create_ffi_min_func,
40+
create_ffi_random_func, create_ffi_rank_func, create_ffi_stddev_func,
41+
create_ffi_sum_func, create_ffi_table_func,
4242
};
4343

4444
use crate::catalog_provider::FFI_CatalogProvider;
@@ -142,6 +142,9 @@ pub struct ForeignLibraryModule {
142142

143143
/// Create an aggregate UDAF using first_value
144144
pub create_first_value_udaf: extern "C" fn() -> FFI_AggregateUDF,
145+
146+
/// Create an aggregate UDAF using min
147+
pub create_min_udaf: extern "C" fn() -> FFI_AggregateUDF,
145148
}
146149

147150
pub fn create_test_schema() -> Arc<Schema> {
@@ -429,5 +432,6 @@ pub extern "C" fn datafusion_ffi_get_module() -> ForeignLibraryModule {
429432
create_query_planner: query_planner::create_query_planner,
430433
version: super::version,
431434
create_first_value_udaf: create_ffi_first_value_func,
435+
create_min_udaf: create_ffi_min_func,
432436
}
433437
}

‎datafusion/ffi/src/tests/udf_udaf_udwf.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use datafusion_expr::{
2929
use datafusion_functions::math::abs::AbsFunc;
3030
use datafusion_functions::math::random::RandomFunc;
3131
use datafusion_functions_aggregate::first_last::FirstValue;
32+
use datafusion_functions_aggregate::min_max::Min;
3233
use datafusion_functions_aggregate::stddev::Stddev;
3334
use datafusion_functions_aggregate::sum::Sum;
3435
use datafusion_functions_table::generate_series::RangeFunc;
@@ -205,6 +206,12 @@ pub(crate) extern "C" fn create_ffi_stddev_func() -> FFI_AggregateUDF {
205206
udaf.into()
206207
}
207208

209+
pub(crate) extern "C" fn create_ffi_min_func() -> FFI_AggregateUDF {
210+
let udaf: Arc<AggregateUDF> = Arc::new(Min::new().into());
211+
212+
udaf.into()
213+
}
214+
208215
pub(crate) extern "C" fn create_ffi_rank_func() -> FFI_WindowUDF {
209216
let udwf: Arc<WindowUDF> = Arc::new(
210217
Rank::new(

‎datafusion/ffi/tests/ffi_udaf.rs‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mod tests {
2626
use datafusion::logical_expr::{AggregateUDF, AggregateUDFImpl, DistinctHandling};
2727
use datafusion::prelude::{SessionContext, col};
2828
use datafusion_catalog::MemTable;
29-
use datafusion_expr::{ScalarUDF, ScalarUDFImpl};
29+
use datafusion_expr::{ExprFunctionExt, ScalarUDF, ScalarUDFImpl};
3030
use datafusion_ffi::tests::utils::get_module;
3131

3232
#[tokio::test]
@@ -103,6 +103,50 @@ mod tests {
103103
Ok(())
104104
}
105105

106+
#[tokio::test]
107+
async fn test_foreign_min_distinct_drops_distinct() -> Result<()> {
108+
let module = get_module()?;
109+
110+
let ffi_min_func = (module.create_min_udaf)();
111+
let foreign_min_func: Arc<dyn AggregateUDFImpl> = (&ffi_min_func).into();
112+
113+
let udaf = AggregateUDF::new_from_shared_impl(foreign_min_func);
114+
115+
let ctx = SessionContext::default();
116+
let record_batch = record_batch!(
117+
("a", Int32, vec![1, 2, 2, 4, 4, 4, 4]),
118+
("b", Float64, vec![3.0, 2.0, 2.0, 4.0, 1.0, 1.0, 4.0])
119+
)
120+
.unwrap();
121+
122+
let df = ctx
123+
.read_batch(record_batch)?
124+
.aggregate(
125+
vec![col("a")],
126+
vec![udaf.call(vec![col("b")]).distinct().build()?.alias("min_b")],
127+
)?
128+
.sort_by(vec![col("a")])?;
129+
130+
let plan = df.clone().into_optimized_plan()?;
131+
assert_eq!(
132+
plan.display_indent().to_string(),
133+
"Sort: ?table?.a ASC NULLS LAST\
134+
\n Aggregate: groupBy=[[?table?.a]], aggr=[[min(?table?.b) AS min_b]]\
135+
\n TableScan: ?table? projection=[a, b]"
136+
);
137+
138+
let result = df.collect().await?;
139+
140+
let expected = record_batch!(
141+
("a", Int32, vec![1, 2, 4]),
142+
("min_b", Float64, vec![3.0, 2.0, 1.0])
143+
)?;
144+
145+
assert_eq!(result[0], expected);
146+
147+
Ok(())
148+
}
149+
106150
#[tokio::test]
107151
async fn test_ffi_grouping_udaf() -> Result<()> {
108152
let module = get_module()?;

0 commit comments

Comments
 (0)