-
Notifications
You must be signed in to change notification settings - Fork 148
feat: Add FFI_TableProviderFactory support #1396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davisp
wants to merge
3
commits into
apache:main
Choose a base branch
from
davisp:feat/ffi-table-provider-factory
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+291
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
examples/datafusion-ffi-example/python/tests/_test_table_provider_factory.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # 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. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from datafusion import SessionContext | ||
| from datafusion_ffi_example import MyTableProviderFactory | ||
|
|
||
|
|
||
| def test_table_provider_factory_ffi() -> None: | ||
| ctx = SessionContext() | ||
| table = MyTableProviderFactory() | ||
|
|
||
| ctx.register_table_factory("MY_FORMAT", table) | ||
|
|
||
| # Create a new external table | ||
| ctx.sql(""" | ||
| CREATE EXTERNAL TABLE | ||
| foo | ||
| STORED AS my_format | ||
| LOCATION ''; | ||
| """).collect() | ||
|
|
||
| # Query the pre-populated table | ||
| result = ctx.sql("SELECT * FROM foo;").collect() | ||
| assert len(result) == 2 | ||
| assert result[0].num_columns == 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
examples/datafusion-ffi-example/src/table_provider_factory.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // 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::sync::Arc; | ||
|
|
||
| use async_trait::async_trait; | ||
| use datafusion_catalog::{Session, TableProvider, TableProviderFactory}; | ||
| use datafusion_common::error::Result as DataFusionResult; | ||
| use datafusion_expr::CreateExternalTable; | ||
| use datafusion_ffi::table_provider_factory::FFI_TableProviderFactory; | ||
| use pyo3::types::PyCapsule; | ||
| use pyo3::{Bound, PyAny, PyResult, Python, pyclass, pymethods}; | ||
|
|
||
| use crate::catalog_provider; | ||
| use crate::utils::ffi_logical_codec_from_pycapsule; | ||
|
|
||
| #[derive(Debug)] | ||
| pub(crate) struct ExampleTableProviderFactory {} | ||
|
|
||
| impl ExampleTableProviderFactory { | ||
| fn new() -> Self { | ||
| Self {} | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl TableProviderFactory for ExampleTableProviderFactory { | ||
| async fn create( | ||
| &self, | ||
| _state: &dyn Session, | ||
| _cmd: &CreateExternalTable, | ||
| ) -> DataFusionResult<Arc<dyn TableProvider>> { | ||
| Ok(catalog_provider::my_table()) | ||
| } | ||
| } | ||
|
|
||
| #[pyclass( | ||
| name = "MyTableProviderFactory", | ||
| module = "datafusion_ffi_example", | ||
| subclass | ||
| )] | ||
| #[derive(Debug)] | ||
| pub struct MyTableProviderFactory { | ||
| inner: Arc<ExampleTableProviderFactory>, | ||
| } | ||
|
|
||
| impl Default for MyTableProviderFactory { | ||
| fn default() -> Self { | ||
| let inner = Arc::new(ExampleTableProviderFactory::new()); | ||
| Self { inner } | ||
| } | ||
| } | ||
|
|
||
| #[pymethods] | ||
| impl MyTableProviderFactory { | ||
| #[new] | ||
| pub fn new() -> Self { | ||
| Self::default() | ||
| } | ||
|
|
||
| pub fn __datafusion_table_provider_factory__<'py>( | ||
| &self, | ||
| py: Python<'py>, | ||
| codec: Bound<PyAny>, | ||
| ) -> PyResult<Bound<'py, PyCapsule>> { | ||
| let name = cr"datafusion_table_provider_factory".into(); | ||
| let codec = ffi_logical_codec_from_pycapsule(codec)?; | ||
| let factory = Arc::clone(&self.inner) as Arc<dyn TableProviderFactory + Send>; | ||
| let factory = FFI_TableProviderFactory::new_with_ffi_codec(factory, None, codec); | ||
|
|
||
| PyCapsule::new(py, factory, Some(name)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No core test coverage is added for
SessionContext.register_table_factory. The new test is underexamples/..., but the main pytesttestpaths(pyproject.toml) only includepython/testsandpython/datafusion, so this behavior likely won’t run in CI. Consider adding a unit/integration test underpython/teststhat registers a factory and exercisesCREATE EXTERNAL TABLEend-to-end.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how I missed it earlier, but the only reason I didn't initially provide the ability for Python based implementations was because I didn't want to make this PR messy attempting to make FFI bindings for the
CreateExternalTablelogical expression. Something made me look a bit harder and realized its already been wrapped so I've gone back and added it in this commit.Also, the robot didn't manage to realize that the FFI example bindings are tested in CI so that part was a non-issue.