Skip to content

Commit a6e5c22

Browse files
committed
FFI: plumb with_updated_config for FFI_ScalarUDF
part of apache#22330. adds the `with_updated_config` fn pointer so a provider's override survives the FFI boundary instead of silently falling back to the trait default (None) on the consumer side. the config crosses in via the existing FFI_ConfigOptions and the reconfigured udf comes back as FFI_Option<FFI_ScalarUDF>. covered by a forced-foreign unit test and a cross-cdylib integration test that both assert the override crosses.
1 parent b4cde12 commit a6e5c22

4 files changed

Lines changed: 176 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ pub struct ForeignLibraryModule {
9191

9292
pub create_placement_udf: extern "C" fn() -> FFI_ScalarUDF,
9393

94+
/// Create a scalar UDF that overrides `with_updated_config`
95+
pub create_with_config_udf: extern "C" fn() -> FFI_ScalarUDF,
96+
9497
pub create_table_function:
9598
extern "C" fn(FFI_LogicalExtensionCodec) -> FFI_TableFunction,
9699

@@ -253,6 +256,7 @@ pub extern "C" fn datafusion_ffi_get_module() -> ForeignLibraryModule {
253256
create_nullary_udf: create_ffi_random_func,
254257
create_timezone_udf: udf_udaf_udwf::create_timezone_func,
255258
create_placement_udf: udf_udaf_udwf::create_placement_func,
259+
create_with_config_udf: udf_udaf_udwf::create_with_config_func,
256260
create_table_function: create_ffi_table_func,
257261
create_sum_udaf: create_ffi_sum_func,
258262
create_stddev_udaf: create_ffi_stddev_func,

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::sync::Arc;
2020
use arrow_schema::DataType;
2121
use datafusion_catalog::TableFunctionImpl;
2222
use datafusion_common::ScalarValue;
23+
use datafusion_common::config::ConfigOptions;
2324
use datafusion_expr::sort_properties::ExprProperties;
2425
use datafusion_expr::{
2526
AggregateUDF, ColumnarValue, ExpressionPlacement, ScalarFunctionArgs, ScalarUDF,
@@ -170,6 +171,55 @@ pub(crate) extern "C" fn create_placement_func() -> FFI_ScalarUDF {
170171
udf.into()
171172
}
172173

174+
#[derive(Debug, PartialEq, Eq, Hash)]
175+
struct WithConfigUpdateUDF {
176+
signature: Signature,
177+
name: String,
178+
}
179+
180+
impl ScalarUDFImpl for WithConfigUpdateUDF {
181+
fn name(&self) -> &str {
182+
&self.name
183+
}
184+
185+
fn signature(&self) -> &Signature {
186+
&self.signature
187+
}
188+
189+
fn return_type(
190+
&self,
191+
_arg_types: &[DataType],
192+
) -> datafusion_common::Result<DataType> {
193+
Ok(DataType::Utf8)
194+
}
195+
196+
fn invoke_with_args(
197+
&self,
198+
_args: ScalarFunctionArgs,
199+
) -> datafusion_common::Result<ColumnarValue> {
200+
Ok(ColumnarValue::Scalar(ScalarValue::from(self.name.clone())))
201+
}
202+
203+
fn with_updated_config(&self, config: &ConfigOptions) -> Option<ScalarUDF> {
204+
// Specialize the name from the configured time zone so the consumer can
205+
// confirm the producer override survived the FFI round trip.
206+
let tz = config.execution.time_zone.clone().unwrap_or_default();
207+
Some(ScalarUDF::from(WithConfigUpdateUDF {
208+
signature: self.signature.clone(),
209+
name: format!("with_config_{tz}"),
210+
}))
211+
}
212+
}
213+
214+
pub(crate) extern "C" fn create_with_config_func() -> FFI_ScalarUDF {
215+
let udf: Arc<ScalarUDF> = Arc::new(ScalarUDF::from(WithConfigUpdateUDF {
216+
signature: Signature::uniform(1, vec![DataType::Utf8], Volatility::Stable),
217+
name: "with_config_base".to_string(),
218+
}));
219+
220+
udf.into()
221+
}
222+
173223
pub(crate) extern "C" fn create_ffi_table_func(
174224
codec: FFI_LogicalExtensionCodec,
175225
) -> FFI_TableFunction {

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

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::expr::columnar_value::FFI_ColumnarValue;
4545
use crate::expr::expr_properties::FFI_ExprProperties;
4646
use crate::placement::FFI_ExpressionPlacement;
4747
use crate::util::{
48-
FFI_Result, rvec_wrapped_to_vec_datatype, vec_datatype_to_rvec_wrapped,
48+
FFI_Option, FFI_Result, rvec_wrapped_to_vec_datatype, vec_datatype_to_rvec_wrapped,
4949
};
5050
use crate::volatility::FFI_Volatility;
5151
use crate::{df_result, sresult, sresult_return};
@@ -123,6 +123,17 @@ pub struct FFI_ScalarUDF {
123123
udf: &Self,
124124
inputs: SVec<FFI_ExprProperties>,
125125
) -> FFI_Result<bool>,
126+
127+
/// FFI equivalent to [`ScalarUDFImpl`]'s `with_updated_config`. Given the
128+
/// session [`ConfigOptions`], optionally returns a reconfigured copy of the
129+
/// UDF, or `None` when the provider does not specialize on config. Wrapped
130+
/// in an `FFI_Result` because recovering the `ConfigOptions` on the provider
131+
/// side is fallible.
132+
pub with_updated_config:
133+
unsafe extern "C" fn(
134+
udf: &Self,
135+
config: FFI_ConfigOptions,
136+
) -> FFI_Result<FFI_Option<FFI_ScalarUDF>>,
126137
}
127138

128139
unsafe impl Send for FFI_ScalarUDF {}
@@ -199,6 +210,21 @@ unsafe extern "C" fn preserves_lex_ordering_fn_wrapper(
199210
sresult!(result)
200211
}
201212

213+
unsafe extern "C" fn with_updated_config_fn_wrapper(
214+
udf: &FFI_ScalarUDF,
215+
config: FFI_ConfigOptions,
216+
) -> FFI_Result<FFI_Option<FFI_ScalarUDF>> {
217+
let config = sresult_return!(ConfigOptions::try_from(config));
218+
219+
let updated: Option<FFI_ScalarUDF> = udf
220+
.inner()
221+
.inner()
222+
.with_updated_config(&config)
223+
.map(|updated| Arc::new(updated).into());
224+
225+
FFI_Result::Ok(updated.into())
226+
}
227+
202228
unsafe extern "C" fn invoke_with_args_fn_wrapper(
203229
udf: &FFI_ScalarUDF,
204230
args: SVec<WrappedArray>,
@@ -298,6 +324,7 @@ impl From<Arc<ScalarUDF>> for FFI_ScalarUDF {
298324
private_data: Box::into_raw(private_data) as *mut c_void,
299325
library_marker_id: crate::get_library_marker_id,
300326
preserves_lex_ordering: preserves_lex_ordering_fn_wrapper,
327+
with_updated_config: with_updated_config_fn_wrapper,
301328
}
302329
}
303330
}
@@ -494,6 +521,19 @@ impl ScalarUDFImpl for ForeignScalarUDF {
494521
df_result!(result)
495522
})
496523
}
524+
525+
fn with_updated_config(&self, config: &ConfigOptions) -> Option<ScalarUDF> {
526+
let config: FFI_ConfigOptions = config.into();
527+
528+
let result = unsafe { (self.udf.with_updated_config)(&self.udf, config) };
529+
530+
// The trait method is infallible, so a transport-level error (the config
531+
// failing to round-trip on the provider side) degrades to `None`; the
532+
// same outcome as a provider that does not specialize on config.
533+
let updated = df_result!(result).ok()?.into_option()?;
534+
535+
Some(ScalarUDF::new_from_shared_impl((&updated).into()))
536+
}
497537
}
498538

499539
#[cfg(test)]
@@ -631,4 +671,63 @@ mod tests {
631671

632672
Ok(())
633673
}
674+
675+
#[derive(Debug, PartialEq, Eq, Hash)]
676+
struct ConfigSpecializingUdf {
677+
signature: Signature,
678+
specialized: bool,
679+
}
680+
681+
impl ScalarUDFImpl for ConfigSpecializingUdf {
682+
fn name(&self) -> &str {
683+
if self.specialized {
684+
"specialized"
685+
} else {
686+
"base"
687+
}
688+
}
689+
690+
fn signature(&self) -> &Signature {
691+
&self.signature
692+
}
693+
694+
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
695+
Ok(DataType::Int32)
696+
}
697+
698+
fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> {
699+
internal_err!("ConfigSpecializingUdf is not meant to be invoked")
700+
}
701+
702+
fn with_updated_config(&self, _config: &ConfigOptions) -> Option<ScalarUDF> {
703+
Some(ScalarUDF::from(ConfigSpecializingUdf {
704+
signature: self.signature.clone(),
705+
specialized: true,
706+
}))
707+
}
708+
}
709+
710+
#[test]
711+
fn test_ffi_udf_with_updated_config() -> Result<()> {
712+
let original_udf = Arc::new(ScalarUDF::from(ConfigSpecializingUdf {
713+
signature: Signature::exact(vec![], datafusion_expr::Volatility::Immutable),
714+
specialized: false,
715+
}));
716+
717+
let mut ffi_udf = FFI_ScalarUDF::from(Arc::clone(&original_udf));
718+
ffi_udf.library_marker_id = crate::mock_foreign_marker_id;
719+
720+
let foreign_udf: Arc<dyn ScalarUDFImpl> = (&ffi_udf).into();
721+
assert!(foreign_udf.is::<ForeignScalarUDF>());
722+
723+
// The producer override must survive the foreign path; the trait default
724+
// returns `None`.
725+
let updated = foreign_udf.with_updated_config(&ConfigOptions::default());
726+
assert_eq!(
727+
updated.map(|udf| udf.name().to_string()),
728+
Some("specialized".to_string())
729+
);
730+
731+
Ok(())
732+
}
634733
}

‎datafusion/ffi/tests/ffi_udf.rs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
mod tests {
2222
use arrow::array::{Array, AsArray, record_batch};
2323
use arrow::datatypes::DataType;
24+
use datafusion::common::config::ConfigOptions;
2425
use datafusion::error::Result;
2526
use datafusion::logical_expr::{ExpressionPlacement, ScalarUDF, ScalarUDFImpl};
2627
use datafusion::prelude::{SessionContext, col};
@@ -157,4 +158,25 @@ mod tests {
157158

158159
Ok(())
159160
}
161+
162+
/// Validates that a provider's `with_updated_config` override survives the
163+
/// FFI boundary (the trait default returns `None`).
164+
#[test]
165+
fn test_with_updated_config_on_scalar_udf() -> Result<()> {
166+
let module = get_module()?;
167+
168+
let ffi_udf = (module.create_with_config_udf)();
169+
let foreign_udf: Arc<dyn ScalarUDFImpl> = (&ffi_udf).into();
170+
171+
let mut options = ConfigOptions::default();
172+
options.execution.time_zone = Some("AEST".into());
173+
174+
let updated = foreign_udf.with_updated_config(&options);
175+
assert_eq!(
176+
updated.map(|udf| udf.name().to_string()),
177+
Some("with_config_AEST".to_string())
178+
);
179+
180+
Ok(())
181+
}
160182
}

0 commit comments

Comments
 (0)