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/04/04 20:13:42 UTC

[PR] Add Table Functions to FFI Crate [datafusion]

timsaucer opened a new pull request, #15581:
URL: https://github.com/apache/datafusion/pull/15581

   ## Which issue does this PR close?
   
   This addresses part of #14562
   
   ## Rationale for this change
   
   We currently have support for user defined scalar functions. Aggregates and Windows are underway. This PR adds user defined table functions.
   
   ## What changes are included in this PR?
   
   - Implement the user FFI crate requirements.
   - Adds one round trip unit test.
   - Adds one full integration test where different shared library is used.
   
   ## Are these changes tested?
   
   Yes, tests added to the PR.
   
   ## Are there any user-facing changes?
   
   None, but additional APIs are now available.
   


-- 
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] Add Table Functions to FFI Crate [datafusion]

Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer commented on code in PR #15581:
URL: https://github.com/apache/datafusion/pull/15581#discussion_r2040329018


##########
datafusion/ffi/tests/ffi_udtf.rs:
##########
@@ -0,0 +1,100 @@
+// 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.
+
+/// Add an additional module here for convenience to scope this to only
+/// when the feature integtation-tests is built
+#[cfg(feature = "integration-tests")]
+mod tests {
+
+    use std::sync::Arc;
+
+    use arrow::array::{create_array, ArrayRef};
+    use arrow::datatypes::DataType;
+    use datafusion::error::{DataFusionError, Result};
+    use datafusion::logical_expr::ScalarUDF;
+    use datafusion::prelude::SessionContext;
+
+    use datafusion_ffi::tests::create_record_batch;
+    use datafusion_ffi::tests::utils::get_module;
+    use datafusion_ffi::udf::ForeignScalarUDF;
+    use datafusion_ffi::udtf::ForeignTableFunction;
+
+    /// This test validates that we can load an external module and use a scalar
+    /// udf defined in it via the foreign function interface. In this case we are
+    /// using the abs() function as our scalar UDF.
+    #[tokio::test]
+    async fn test_user_defined_table_function() -> Result<()> {
+        let module = get_module()?;
+
+        let ffi_table_func = module
+            .create_table_function()
+            .ok_or(DataFusionError::NotImplemented(
+            "External table function provider failed to implement create_table_function"
+                .to_string(),
+        ))?();
+        let foreign_table_func: ForeignTableFunction = ffi_table_func.into();
+
+        let udtf = Arc::new(foreign_table_func);
+
+        let ctx = SessionContext::default();
+        ctx.register_udtf("my_range", udtf);
+
+        let result = ctx
+            .sql("SELECT * FROM my_range(5)")
+            .await?
+            .collect()
+            .await?;
+        let expected = create_array!(Int64, [0, 1, 2, 3, 4]) as ArrayRef;
+
+        assert!(result.len() == 1);
+        assert!(result[0].column(0) == &expected);
+
+        Ok(())
+    }
+
+    /// This test validates nullary input UDFs
+    #[tokio::test]
+    async fn test_nullary_scalar_udf() -> Result<()> {

Review Comment:
   Sorry, should be good to go now.



-- 
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] Add Table Functions to FFI Crate [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #15581:
URL: https://github.com/apache/datafusion/pull/15581#issuecomment-2798809598

   Very exciting -- thanks @timsaucer 


-- 
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] Add Table Functions to FFI Crate [datafusion]

Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer merged PR #15581:
URL: https://github.com/apache/datafusion/pull/15581


-- 
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] Add Table Functions to FFI Crate [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #15581:
URL: https://github.com/apache/datafusion/pull/15581#discussion_r2040253122


##########
datafusion/ffi/tests/ffi_udtf.rs:
##########
@@ -0,0 +1,100 @@
+// 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.
+
+/// Add an additional module here for convenience to scope this to only
+/// when the feature integtation-tests is built
+#[cfg(feature = "integration-tests")]
+mod tests {
+
+    use std::sync::Arc;
+
+    use arrow::array::{create_array, ArrayRef};
+    use arrow::datatypes::DataType;
+    use datafusion::error::{DataFusionError, Result};
+    use datafusion::logical_expr::ScalarUDF;
+    use datafusion::prelude::SessionContext;
+
+    use datafusion_ffi::tests::create_record_batch;
+    use datafusion_ffi::tests::utils::get_module;
+    use datafusion_ffi::udf::ForeignScalarUDF;
+    use datafusion_ffi::udtf::ForeignTableFunction;
+
+    /// This test validates that we can load an external module and use a scalar
+    /// udf defined in it via the foreign function interface. In this case we are
+    /// using the abs() function as our scalar UDF.
+    #[tokio::test]
+    async fn test_user_defined_table_function() -> Result<()> {
+        let module = get_module()?;
+
+        let ffi_table_func = module
+            .create_table_function()
+            .ok_or(DataFusionError::NotImplemented(
+            "External table function provider failed to implement create_table_function"
+                .to_string(),
+        ))?();
+        let foreign_table_func: ForeignTableFunction = ffi_table_func.into();
+
+        let udtf = Arc::new(foreign_table_func);
+
+        let ctx = SessionContext::default();
+        ctx.register_udtf("my_range", udtf);
+
+        let result = ctx
+            .sql("SELECT * FROM my_range(5)")
+            .await?
+            .collect()
+            .await?;
+        let expected = create_array!(Int64, [0, 1, 2, 3, 4]) as ArrayRef;
+
+        assert!(result.len() == 1);
+        assert!(result[0].column(0) == &expected);
+
+        Ok(())
+    }
+
+    /// This test validates nullary input UDFs
+    #[tokio::test]
+    async fn test_nullary_scalar_udf() -> Result<()> {

Review Comment:
   Do you want to handle this comment on this PR @timsaucer  or shall we merge the PR and handle it later?



-- 
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] Add Table Functions to FFI Crate [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #15581:
URL: https://github.com/apache/datafusion/pull/15581#issuecomment-2790026143

   Merged up from main to get a clean CI run (hopefully)


-- 
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] Add Table Functions to FFI Crate [datafusion]

Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer commented on PR #15581:
URL: https://github.com/apache/datafusion/pull/15581#issuecomment-2798145855

   I’ll resolve those clippy warnings next time I’m at my computer 


-- 
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] Add Table Functions to FFI Crate [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #15581:
URL: https://github.com/apache/datafusion/pull/15581#discussion_r2031022220


##########
datafusion/ffi/tests/ffi_udtf.rs:
##########
@@ -0,0 +1,100 @@
+// 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.
+
+/// Add an additional module here for convenience to scope this to only
+/// when the feature integtation-tests is built
+#[cfg(feature = "integration-tests")]
+mod tests {
+
+    use std::sync::Arc;
+
+    use arrow::array::{create_array, ArrayRef};
+    use arrow::datatypes::DataType;
+    use datafusion::error::{DataFusionError, Result};
+    use datafusion::logical_expr::ScalarUDF;
+    use datafusion::prelude::SessionContext;
+
+    use datafusion_ffi::tests::create_record_batch;
+    use datafusion_ffi::tests::utils::get_module;
+    use datafusion_ffi::udf::ForeignScalarUDF;
+    use datafusion_ffi::udtf::ForeignTableFunction;
+
+    /// This test validates that we can load an external module and use a scalar
+    /// udf defined in it via the foreign function interface. In this case we are
+    /// using the abs() function as our scalar UDF.
+    #[tokio::test]
+    async fn test_user_defined_table_function() -> Result<()> {
+        let module = get_module()?;
+
+        let ffi_table_func = module
+            .create_table_function()
+            .ok_or(DataFusionError::NotImplemented(
+            "External table function provider failed to implement create_table_function"
+                .to_string(),
+        ))?();
+        let foreign_table_func: ForeignTableFunction = ffi_table_func.into();
+
+        let udtf = Arc::new(foreign_table_func);
+
+        let ctx = SessionContext::default();
+        ctx.register_udtf("my_range", udtf);
+
+        let result = ctx
+            .sql("SELECT * FROM my_range(5)")
+            .await?
+            .collect()
+            .await?;
+        let expected = create_array!(Int64, [0, 1, 2, 3, 4]) as ArrayRef;
+
+        assert!(result.len() == 1);
+        assert!(result[0].column(0) == &expected);
+
+        Ok(())
+    }
+
+    /// This test validates nullary input UDFs
+    #[tokio::test]
+    async fn test_nullary_scalar_udf() -> Result<()> {

Review Comment:
   A test for nullary udfs seems out of place with tests for user defined table functions -- is this maybe a copy/paste error?



-- 
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