Skip to content

Commit 9facaaa

Browse files
authored
FFI: forward ScalarUDF preserves_lex_ordering (#23069)
## Which issue does this PR close? Part of #22330. ## Rationale for this change `ForeignScalarUDF` inherits the default `preserves_lex_ordering`, so producer overrides are lost across the FFI boundary. ## What changes are included in this PR? - Forward `preserves_lex_ordering` through `FFI_ScalarUDF`. - Reuse the existing placement UDF for unit and dynamic-library coverage. ## 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. --------- Signed-off-by: Amogh Ramesh <[email protected]>
1 parent 1c3232c commit 9facaaa

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

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

Lines changed: 8 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_expr::sort_properties::ExprProperties;
2324
use datafusion_expr::{
2425
AggregateUDF, ColumnarValue, ExpressionPlacement, ScalarFunctionArgs, ScalarUDF,
2526
ScalarUDFImpl, Signature, Volatility, WindowUDF,
@@ -152,6 +153,13 @@ impl ScalarUDFImpl for PlacementUDF {
152153
ExpressionPlacement::KeepInPlace
153154
}
154155
}
156+
157+
fn preserves_lex_ordering(
158+
&self,
159+
inputs: &[ExprProperties],
160+
) -> datafusion_common::Result<bool> {
161+
Ok(inputs.iter().all(|input| input.preserves_lex_ordering))
162+
}
155163
}
156164

157165
pub(crate) extern "C" fn create_placement_func() -> FFI_ScalarUDF {

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use arrow::ffi::{FFI_ArrowSchema, from_ffi, to_ffi};
2626
use arrow_schema::FieldRef;
2727
use datafusion_common::config::ConfigOptions;
2828
use datafusion_common::{DataFusionError, Result, internal_err};
29+
use datafusion_expr::sort_properties::ExprProperties;
2930
use datafusion_expr::type_coercion::functions::fields_with_udf;
3031
use datafusion_expr::{
3132
ColumnarValue, ExpressionPlacement, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDF,
@@ -41,6 +42,7 @@ use stabby::vec::Vec as SVec;
4142
use crate::arrow_wrappers::{WrappedArray, WrappedSchema};
4243
use crate::config::FFI_ConfigOptions;
4344
use crate::expr::columnar_value::FFI_ColumnarValue;
45+
use crate::expr::expr_properties::FFI_ExprProperties;
4446
use crate::placement::FFI_ExpressionPlacement;
4547
use crate::util::{
4648
FFI_Result, rvec_wrapped_to_vec_datatype, vec_datatype_to_rvec_wrapped,
@@ -115,6 +117,12 @@ pub struct FFI_ScalarUDF {
115117
/// the foreign interface. See [`crate::get_library_marker_id`] and
116118
/// the crate's `README.md` for more information.
117119
pub library_marker_id: extern "C" fn() -> usize,
120+
121+
/// FFI equivalent to [`ScalarUDFImpl::preserves_lex_ordering`].
122+
pub preserves_lex_ordering: unsafe extern "C" fn(
123+
udf: &Self,
124+
inputs: SVec<FFI_ExprProperties>,
125+
) -> FFI_Result<bool>,
118126
}
119127

120128
unsafe impl Send for FFI_ScalarUDF {}
@@ -178,6 +186,19 @@ unsafe extern "C" fn placement_fn_wrapper(
178186
udf.inner().placement(&args).into()
179187
}
180188

189+
unsafe extern "C" fn preserves_lex_ordering_fn_wrapper(
190+
udf: &FFI_ScalarUDF,
191+
inputs: SVec<FFI_ExprProperties>,
192+
) -> FFI_Result<bool> {
193+
let result = inputs
194+
.into_iter()
195+
.map(ExprProperties::try_from)
196+
.collect::<Result<Vec<_>>>()
197+
.and_then(|inputs| udf.inner().preserves_lex_ordering(&inputs));
198+
199+
sresult!(result)
200+
}
201+
181202
unsafe extern "C" fn invoke_with_args_fn_wrapper(
182203
udf: &FFI_ScalarUDF,
183204
args: SVec<WrappedArray>,
@@ -276,6 +297,7 @@ impl From<Arc<ScalarUDF>> for FFI_ScalarUDF {
276297
release: release_fn_wrapper,
277298
private_data: Box::into_raw(private_data) as *mut c_void,
278299
library_marker_id: crate::get_library_marker_id,
300+
preserves_lex_ordering: preserves_lex_ordering_fn_wrapper,
279301
}
280302
}
281303
}
@@ -460,6 +482,18 @@ impl ScalarUDFImpl for ForeignScalarUDF {
460482

461483
result.into()
462484
}
485+
486+
fn preserves_lex_ordering(&self, inputs: &[ExprProperties]) -> Result<bool> {
487+
inputs
488+
.iter()
489+
.map(FFI_ExprProperties::try_from)
490+
.collect::<Result<SVec<_>>>()
491+
.and_then(|inputs| {
492+
let result =
493+
unsafe { (self.udf.preserves_lex_ordering)(&self.udf, inputs) };
494+
df_result!(result)
495+
})
496+
}
463497
}
464498

465499
#[cfg(test)]
@@ -500,6 +534,14 @@ mod tests {
500534
ExpressionPlacement::KeepInPlace
501535
}
502536
}
537+
538+
fn preserves_lex_ordering(&self, inputs: &[ExprProperties]) -> Result<bool> {
539+
if inputs.is_empty() {
540+
return internal_err!("preserves_lex_ordering requires an input");
541+
}
542+
543+
Ok(inputs.iter().all(|input| input.preserves_lex_ordering))
544+
}
503545
}
504546

505547
#[test]
@@ -572,6 +614,21 @@ mod tests {
572614
);
573615
assert_eq!(foreign_udf.placement(&[]), ExpressionPlacement::KeepInPlace);
574616

617+
let preserves = ExprProperties::new_unknown().with_preserves_lex_ordering(true);
618+
let does_not_preserve = ExprProperties::new_unknown();
619+
620+
assert!(
621+
foreign_udf
622+
.preserves_lex_ordering(std::slice::from_ref(&preserves))
623+
.unwrap()
624+
);
625+
assert!(
626+
!foreign_udf
627+
.preserves_lex_ordering(&[preserves, does_not_preserve])
628+
.unwrap()
629+
);
630+
assert!(foreign_udf.preserves_lex_ordering(&[]).is_err());
631+
575632
Ok(())
576633
}
577634
}

‎datafusion/ffi/tests/ffi_udf.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ mod tests {
2626
use datafusion::prelude::{SessionContext, col};
2727
use datafusion_execution::config::SessionConfig;
2828
use datafusion_expr::lit;
29+
use datafusion_expr::sort_properties::ExprProperties;
2930
use datafusion_ffi::tests::create_record_batch;
3031
use datafusion_ffi::tests::utils::get_module;
3132
use std::sync::Arc;
@@ -90,8 +91,7 @@ mod tests {
9091
Ok(())
9192
}
9293

93-
/// This test validates that a producer's `placement` override survives the
94-
/// FFI boundary instead of collapsing to the default `KeepInPlace`.
94+
/// Checks planning-property overrides across the FFI boundary.
9595
#[tokio::test]
9696
async fn test_scalar_udf_placement() -> Result<()> {
9797
let module = get_module()?;
@@ -112,6 +112,12 @@ mod tests {
112112
ExpressionPlacement::KeepInPlace
113113
);
114114

115+
let preserves = ExprProperties::new_unknown().with_preserves_lex_ordering(true);
116+
let does_not_preserve = ExprProperties::new_unknown();
117+
118+
assert!(foreign_func.preserves_lex_ordering(std::slice::from_ref(&preserves))?);
119+
assert!(!foreign_func.preserves_lex_ordering(&[preserves, does_not_preserve])?);
120+
115121
Ok(())
116122
}
117123

0 commit comments

Comments
 (0)