Skip to content

Commit 96b7f67

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 f96a64c commit 96b7f67

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
@@ -92,6 +92,9 @@ pub struct ForeignLibraryModule {
9292

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

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

@@ -254,6 +257,7 @@ pub extern "C" fn datafusion_ffi_get_module() -> ForeignLibraryModule {
254257
create_nullary_udf: create_ffi_random_func,
255258
create_timezone_udf: udf_udaf_udwf::create_timezone_func,
256259
create_placement_udf: udf_udaf_udwf::create_placement_func,
260+
create_with_config_udf: udf_udaf_udwf::create_with_config_func,
257261
create_table_function: create_ffi_table_func,
258262
create_sum_udaf: create_ffi_sum_func,
259263
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::{
2425
AggregateUDF, ColumnarValue, ExpressionPlacement, ScalarFunctionArgs, ScalarUDF,
2526
ScalarUDFImpl, Signature, Volatility, WindowUDF,
@@ -162,6 +163,55 @@ pub(crate) extern "C" fn create_placement_func() -> FFI_ScalarUDF {
162163
udf.into()
163164
}
164165

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

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

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::config::FFI_ConfigOptions;
4343
use crate::expr::columnar_value::FFI_ColumnarValue;
4444
use crate::placement::FFI_ExpressionPlacement;
4545
use crate::util::{
46-
FFI_Result, rvec_wrapped_to_vec_datatype, vec_datatype_to_rvec_wrapped,
46+
FFI_Option, FFI_Result, rvec_wrapped_to_vec_datatype, vec_datatype_to_rvec_wrapped,
4747
};
4848
use crate::volatility::FFI_Volatility;
4949
use crate::{df_result, sresult, sresult_return};
@@ -100,6 +100,17 @@ pub struct FFI_ScalarUDF {
100100
args: SVec<FFI_ExpressionPlacement>,
101101
) -> FFI_ExpressionPlacement,
102102

103+
/// FFI equivalent to [`ScalarUDFImpl`]'s `with_updated_config`. Given the
104+
/// session [`ConfigOptions`], optionally returns a reconfigured copy of the
105+
/// UDF, or `None` when the provider does not specialize on config. Wrapped
106+
/// in an `FFI_Result` because recovering the `ConfigOptions` on the provider
107+
/// side is fallible.
108+
pub with_updated_config:
109+
unsafe extern "C" fn(
110+
udf: &Self,
111+
config: FFI_ConfigOptions,
112+
) -> FFI_Result<FFI_Option<FFI_ScalarUDF>>,
113+
103114
/// Used to create a clone on the provider of the udf. This should
104115
/// only need to be called by the receiver of the udf.
105116
pub clone: unsafe extern "C" fn(udf: &Self) -> Self,
@@ -178,6 +189,21 @@ unsafe extern "C" fn placement_fn_wrapper(
178189
udf.inner().placement(&args).into()
179190
}
180191

192+
unsafe extern "C" fn with_updated_config_fn_wrapper(
193+
udf: &FFI_ScalarUDF,
194+
config: FFI_ConfigOptions,
195+
) -> FFI_Result<FFI_Option<FFI_ScalarUDF>> {
196+
let config = sresult_return!(ConfigOptions::try_from(config));
197+
198+
let updated: Option<FFI_ScalarUDF> = udf
199+
.inner()
200+
.inner()
201+
.with_updated_config(&config)
202+
.map(|updated| Arc::new(updated).into());
203+
204+
FFI_Result::Ok(updated.into())
205+
}
206+
181207
unsafe extern "C" fn invoke_with_args_fn_wrapper(
182208
udf: &FFI_ScalarUDF,
183209
args: SVec<WrappedArray>,
@@ -272,6 +298,7 @@ impl From<Arc<ScalarUDF>> for FFI_ScalarUDF {
272298
return_field_from_args: return_field_from_args_fn_wrapper,
273299
coerce_types: coerce_types_fn_wrapper,
274300
placement: placement_fn_wrapper,
301+
with_updated_config: with_updated_config_fn_wrapper,
275302
clone: clone_fn_wrapper,
276303
release: release_fn_wrapper,
277304
private_data: Box::into_raw(private_data) as *mut c_void,
@@ -460,6 +487,19 @@ impl ScalarUDFImpl for ForeignScalarUDF {
460487

461488
result.into()
462489
}
490+
491+
fn with_updated_config(&self, config: &ConfigOptions) -> Option<ScalarUDF> {
492+
let config: FFI_ConfigOptions = config.into();
493+
494+
let result = unsafe { (self.udf.with_updated_config)(&self.udf, config) };
495+
496+
// The trait method is infallible, so a transport-level error (the config
497+
// failing to round-trip on the provider side) degrades to `None`; the
498+
// same outcome as a provider that does not specialize on config.
499+
let updated = df_result!(result).ok()?.into_option()?;
500+
501+
Some(ScalarUDF::new_from_shared_impl((&updated).into()))
502+
}
463503
}
464504

465505
#[cfg(test)]
@@ -574,4 +614,63 @@ mod tests {
574614

575615
Ok(())
576616
}
617+
618+
#[derive(Debug, PartialEq, Eq, Hash)]
619+
struct ConfigSpecializingUdf {
620+
signature: Signature,
621+
specialized: bool,
622+
}
623+
624+
impl ScalarUDFImpl for ConfigSpecializingUdf {
625+
fn name(&self) -> &str {
626+
if self.specialized {
627+
"specialized"
628+
} else {
629+
"base"
630+
}
631+
}
632+
633+
fn signature(&self) -> &Signature {
634+
&self.signature
635+
}
636+
637+
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
638+
Ok(DataType::Int32)
639+
}
640+
641+
fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> {
642+
internal_err!("ConfigSpecializingUdf is not meant to be invoked")
643+
}
644+
645+
fn with_updated_config(&self, _config: &ConfigOptions) -> Option<ScalarUDF> {
646+
Some(ScalarUDF::from(ConfigSpecializingUdf {
647+
signature: self.signature.clone(),
648+
specialized: true,
649+
}))
650+
}
651+
}
652+
653+
#[test]
654+
fn test_ffi_udf_with_updated_config() -> Result<()> {
655+
let original_udf = Arc::new(ScalarUDF::from(ConfigSpecializingUdf {
656+
signature: Signature::exact(vec![], datafusion_expr::Volatility::Immutable),
657+
specialized: false,
658+
}));
659+
660+
let mut ffi_udf = FFI_ScalarUDF::from(Arc::clone(&original_udf));
661+
ffi_udf.library_marker_id = crate::mock_foreign_marker_id;
662+
663+
let foreign_udf: Arc<dyn ScalarUDFImpl> = (&ffi_udf).into();
664+
assert!(foreign_udf.is::<ForeignScalarUDF>());
665+
666+
// The producer override must survive the foreign path; the trait default
667+
// returns `None`.
668+
let updated = foreign_udf.with_updated_config(&ConfigOptions::default());
669+
assert_eq!(
670+
updated.map(|udf| udf.name().to_string()),
671+
Some("specialized".to_string())
672+
);
673+
674+
Ok(())
675+
}
577676
}

‎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};
2323
use arrow::datatypes::DataType;
24+
use datafusion::common::config::ConfigOptions;
2425
use datafusion::common::record_batch;
2526
use datafusion::error::Result;
2627
use datafusion::logical_expr::{ExpressionPlacement, ScalarUDF, ScalarUDFImpl};
@@ -152,4 +153,25 @@ mod tests {
152153

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

0 commit comments

Comments
 (0)