Skip to content

Commit db0c31b

Browse files
authored
fix(ffi): preserve aggregate null-handling support (apache#23908)
## Which issue does this PR close? Part of apache#22331. ## Rationale for this change `ForeignAggregateUDF` inherits the default `supports_null_handling_clause`, so producer overrides are lost across the FFI boundary. ## What changes are included in this PR? - Forward `supports_null_handling_clause` through `FFI_AggregateUDF`. - Use `first_value` as the positive case and `sum` as the negative case in forced-foreign and dynamic-library tests. ## Are these changes tested? - `cargo test -p datafusion-ffi --features integration-tests` - `cargo clippy --all-targets --all-features -- -D warnings` ## Are there any user-facing changes? The FFI ABI changes. Foreign libraries must rebuild against the new DataFusion version.
1 parent 26fbf4d commit db0c31b

4 files changed

Lines changed: 58 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ use datafusion_expr::{Expr, TableType};
3131
use datafusion_physical_plan::ExecutionPlan;
3232
use sync_provider::create_sync_table_provider;
3333
use udf_udaf_udwf::{
34-
create_ffi_abs_func, create_ffi_random_func, create_ffi_rank_func,
35-
create_ffi_stddev_func, create_ffi_sum_func, create_ffi_table_func,
34+
create_ffi_abs_func, create_ffi_first_value_func, create_ffi_random_func,
35+
create_ffi_rank_func, create_ffi_stddev_func, create_ffi_sum_func,
36+
create_ffi_table_func,
3637
};
3738

3839
use crate::catalog_provider::FFI_CatalogProvider;
@@ -117,6 +118,9 @@ pub struct ForeignLibraryModule {
117118
pub create_context_aware_optimizer_rule: extern "C" fn() -> FFI_PhysicalOptimizerRule,
118119

119120
pub version: extern "C" fn() -> u64,
121+
122+
/// Create an aggregate UDAF using first_value
123+
pub create_first_value_udaf: extern "C" fn() -> FFI_AggregateUDF,
120124
}
121125

122126
pub fn create_test_schema() -> Arc<Schema> {
@@ -266,5 +270,6 @@ pub extern "C" fn datafusion_ffi_get_module() -> ForeignLibraryModule {
266270
create_context_aware_optimizer_rule:
267271
physical_optimizer::create_context_aware_optimizer_rule,
268272
version: super::version,
273+
create_first_value_udaf: create_ffi_first_value_func,
269274
}
270275
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use datafusion_expr::{
2727
};
2828
use datafusion_functions::math::abs::AbsFunc;
2929
use datafusion_functions::math::random::RandomFunc;
30+
use datafusion_functions_aggregate::first_last::FirstValue;
3031
use datafusion_functions_aggregate::stddev::Stddev;
3132
use datafusion_functions_aggregate::sum::Sum;
3233
use datafusion_functions_table::generate_series::RangeFunc;
@@ -184,6 +185,12 @@ pub(crate) extern "C" fn create_ffi_sum_func() -> FFI_AggregateUDF {
184185
udaf.into()
185186
}
186187

188+
pub(crate) extern "C" fn create_ffi_first_value_func() -> FFI_AggregateUDF {
189+
let udaf: Arc<AggregateUDF> = Arc::new(FirstValue::new().into());
190+
191+
udaf.into()
192+
}
193+
187194
pub(crate) extern "C" fn create_ffi_stddev_func() -> FFI_AggregateUDF {
188195
let udaf: Arc<AggregateUDF> = Arc::new(Stddev::new().into());
189196

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ pub struct FFI_AggregateUDF {
145145
/// the foreign interface. See [`crate::get_library_marker_id`] and
146146
/// the crate's `README.md` for more information.
147147
pub library_marker_id: extern "C" fn() -> usize,
148+
149+
/// FFI equivalent to [`AggregateUDF::supports_null_handling_clause`]
150+
pub supports_null_handling_clause:
151+
unsafe extern "C" fn(udaf: &FFI_AggregateUDF) -> bool,
148152
}
149153

150154
unsafe impl Send for FFI_AggregateUDF {}
@@ -327,6 +331,12 @@ unsafe extern "C" fn order_sensitivity_fn_wrapper(
327331
unsafe { udaf.inner().order_sensitivity().into() }
328332
}
329333

334+
unsafe extern "C" fn supports_null_handling_clause_fn_wrapper(
335+
udaf: &FFI_AggregateUDF,
336+
) -> bool {
337+
unsafe { udaf.inner().supports_null_handling_clause() }
338+
}
339+
330340
unsafe extern "C" fn coerce_types_fn_wrapper(
331341
udaf: &FFI_AggregateUDF,
332342
arg_types: SVec<WrappedSchema>,
@@ -401,6 +411,7 @@ impl From<Arc<AggregateUDF>> for FFI_AggregateUDF {
401411
release: release_fn_wrapper,
402412
private_data: Box::into_raw(private_data) as *mut c_void,
403413
library_marker_id: crate::get_library_marker_id,
414+
supports_null_handling_clause: supports_null_handling_clause_fn_wrapper,
404415
}
405416
}
406417
}
@@ -595,6 +606,10 @@ impl AggregateUDFImpl for ForeignAggregateUDF {
595606
unsafe { (self.udaf.order_sensitivity)(&self.udaf).into() }
596607
}
597608

609+
fn supports_null_handling_clause(&self) -> bool {
610+
unsafe { (self.udaf.supports_null_handling_clause)(&self.udaf) }
611+
}
612+
598613
fn simplify(&self) -> Option<AggregateFunctionSimplification> {
599614
None
600615
}
@@ -774,6 +789,19 @@ mod tests {
774789
Ok(())
775790
}
776791

792+
#[test]
793+
fn test_supports_null_handling_clause() -> Result<()> {
794+
let first_value = create_test_foreign_udaf(
795+
datafusion::functions_aggregate::first_last::FirstValue::new(),
796+
)?;
797+
assert!(first_value.supports_null_handling_clause());
798+
799+
let sum = create_test_foreign_udaf(Sum::new())?;
800+
assert!(!sum.supports_null_handling_clause());
801+
802+
Ok(())
803+
}
804+
777805
#[test]
778806
fn test_beneficial_ordering() -> Result<()> {
779807
let foreign_udaf = create_test_foreign_udaf(

‎datafusion/ffi/tests/ffi_udaf.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ mod tests {
6666
Ok(())
6767
}
6868

69+
#[test]
70+
fn test_supports_null_handling_clause() -> Result<()> {
71+
let module = get_module()?;
72+
73+
let ffi_first_value_func = (module.create_first_value_udaf)();
74+
let foreign_first_value_func: Arc<dyn AggregateUDFImpl> =
75+
(&ffi_first_value_func).into();
76+
assert!(foreign_first_value_func.supports_null_handling_clause());
77+
78+
let ffi_sum_func = (module.create_sum_udaf)();
79+
let foreign_sum_func: Arc<dyn AggregateUDFImpl> = (&ffi_sum_func).into();
80+
assert!(!foreign_sum_func.supports_null_handling_clause());
81+
82+
Ok(())
83+
}
84+
6985
#[tokio::test]
7086
async fn test_ffi_grouping_udaf() -> Result<()> {
7187
let module = get_module()?;

0 commit comments

Comments
 (0)