You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "timsaucer (via GitHub)" <gi...@apache.org> on 2025/02/10 12:31:44 UTC
[PR] initial commit for scalar udf in ffi crate [datafusion]
timsaucer opened a new pull request, #14579:
URL: https://github.com/apache/datafusion/pull/14579
## Which issue does this PR close?
Addresses part of https://github.com/apache/datafusion/issues/14562 - specifically scalar udfs
## Rationale for this change
This is a pure addition were we expose `ScalarUDF` across the FFI boundary. This enables new functionality about creating reusable libraries, such as in python.
## What changes are included in this PR?
Addition of scalar udf FFI wrappers
## Are these changes tested?
TODO:
- [ ] Create additional unit tests inside FFI crate
- [ ] Expand the existing integration test to include scalar udf
- [ ] Create a proof of concept in datafusion-python and test
## Are there any user-facing changes?
This is a pure addition.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Add ScalarUDF support in FFI crate [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #14579:
URL: https://github.com/apache/datafusion/pull/14579#discussion_r1957153878
##########
datafusion/ffi/src/signature.rs:
##########
@@ -0,0 +1,295 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::num::NonZeroUsize;
+
+use abi_stable::{std_types::RVec, StableAbi};
+use arrow::datatypes::DataType;
+use arrow::ffi::FFI_ArrowSchema;
+use datafusion::{
+ error::DataFusionError,
+ logical_expr::{
+ ArrayFunctionSignature, Signature, TypeSignature, TypeSignatureClass, Volatility,
+ },
+};
+
+use crate::arrow_wrappers::WrappedSchema;
+use datafusion::error::Result;
+
+#[repr(C)]
+#[derive(StableAbi)]
+#[allow(non_camel_case_types)]
+pub struct FFI_Signature {
Review Comment:
I wonder if we should keep this API simpler as I think the actual signature API is about to change in the near future. For example, the work from @jayzhan211 and @shehabgamin in the following PR
- https://github.com/apache/datafusion/pull/14440
One thought I had was to to only support the equivalent of [`TypeSignature::UserDefined`](https://docs.rs/datafusion/latest/datafusion/logical_expr/enum.TypeSignature.html#variant.UserDefined)
While this would make it somewhat less conveniente to implement user defined functions I think it would result in much smaller FFI API and also be much less likely to change overtime
Maybe once the TypeSignature / Coecion code has settled down we could add more to the FFI interface
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Add ScalarUDF support in FFI crate [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #14579:
URL: https://github.com/apache/datafusion/pull/14579#discussion_r1961592529
##########
datafusion/ffi/src/udf.rs:
##########
@@ -0,0 +1,346 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::{ffi::c_void, sync::Arc};
+
+use abi_stable::{
+ std_types::{RResult, RString, RVec},
+ StableAbi,
+};
+use arrow::datatypes::DataType;
+use arrow::{
+ array::ArrayRef,
+ error::ArrowError,
+ ffi::{from_ffi, to_ffi, FFI_ArrowSchema},
+};
+use datafusion::{
+ error::DataFusionError,
+ logical_expr::type_coercion::functions::data_types_with_scalar_udf,
+};
+use datafusion::{
+ error::Result,
+ logical_expr::{
+ ColumnarValue, ScalarFunctionArgs, ScalarUDF, ScalarUDFImpl, Signature,
+ },
+};
+
+use crate::{
+ arrow_wrappers::{WrappedArray, WrappedSchema},
+ df_result, rresult, rresult_return,
+ util::{rvec_wrapped_to_vec_datatype, vec_datatype_to_rvec_wrapped},
+ volatility::FFI_Volatility,
+};
+
+/// A stable struct for sharing a [`ScalarUDF`] across FFI boundaries.
+#[repr(C)]
+#[derive(Debug, StableAbi)]
+#[allow(non_camel_case_types)]
+pub struct FFI_ScalarUDF {
+ /// Return the udf name.
+ pub name: RString,
+
+ pub aliases: RVec<RString>,
+
+ pub volatility: FFI_Volatility,
+
+ pub return_type: unsafe extern "C" fn(
Review Comment:
Eventually it would be great to document these a bit more -- for example here we can mention that we only expose the return type method to simplify the signature
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Add ScalarUDF support in FFI crate [datafusion]
Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer merged PR #14579:
URL: https://github.com/apache/datafusion/pull/14579
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Add ScalarUDF support in FFI crate [datafusion]
Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer commented on code in PR #14579:
URL: https://github.com/apache/datafusion/pull/14579#discussion_r1959772360
##########
datafusion/ffi/src/signature.rs:
##########
@@ -0,0 +1,295 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::num::NonZeroUsize;
+
+use abi_stable::{std_types::RVec, StableAbi};
+use arrow::datatypes::DataType;
+use arrow::ffi::FFI_ArrowSchema;
+use datafusion::{
+ error::DataFusionError,
+ logical_expr::{
+ ArrayFunctionSignature, Signature, TypeSignature, TypeSignatureClass, Volatility,
+ },
+};
+
+use crate::arrow_wrappers::WrappedSchema;
+use datafusion::error::Result;
+
+#[repr(C)]
+#[derive(StableAbi)]
+#[allow(non_camel_case_types)]
+pub struct FFI_Signature {
Review Comment:
Does this now match what you were looking for?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] feat: Add ScalarUDF support in FFI crate [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #14579:
URL: https://github.com/apache/datafusion/pull/14579#issuecomment-2671329643
woohoo!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org