Copilot commented on code in PR #1396:
URL: 
https://github.com/apache/datafusion-python/pull/1396#discussion_r2916087017


##########
src/context.rs:
##########
@@ -659,6 +660,34 @@ impl PySessionContext {
         Ok(())
     }
 
+    pub fn register_table_factory(
+        &self,
+        format: &str,
+        factory: Bound<'_, PyAny>,
+    ) -> PyDataFusionResult<()> {
+        let py = factory.py();
+        let codec_capsule = create_logical_extension_capsule(py, 
self.logical_codec.as_ref())?;
+
+        let capsule = factory
+            .getattr("__datafusion_table_provider_factory__")?
+            .call1((codec_capsule,))?;
+        let capsule = capsule.cast::<PyCapsule>().map_err(py_datafusion_err)?;

Review Comment:
   `register_table_factory` always calls 
`factory.__datafusion_table_provider_factory__` and therefore fails if the 
caller passes an already-exported PyCapsule (unlike 
`register_catalog_provider_list` / `register_catalog_provider`, which accept 
either an exportable object or a capsule). Consider mirroring the existing 
pattern: if the object has the export method, call it with the codec capsule; 
otherwise, try casting directly to `PyCapsule` and validate it.
   ```suggestion
           // Support both exportable factory objects and already-exported 
PyCapsules,
           // mirroring the pattern used by catalog registration functions.
           let capsule = if 
factory.hasattr("__datafusion_table_provider_factory__")? {
               let exporter = 
factory.getattr("__datafusion_table_provider_factory__")?;
               let capsule_obj = exporter.call1((codec_capsule,))?;
               capsule_obj.cast::<PyCapsule>().map_err(py_datafusion_err)?
           } else {
               factory.cast::<PyCapsule>().map_err(py_datafusion_err)?
           };
   ```



##########
python/datafusion/context.py:
##########
@@ -830,6 +831,20 @@ def deregister_table(self, name: str) -> None:
         """Remove a table from the session."""
         self.ctx.deregister_table(name)
 
+    def register_table_factory(
+        self, format: str, factory: TableProviderFactoryExportable
+    ) -> None:
+        """Register a :py:class:`~datafusion.TableProviderFactoryExportable`.
+
+        The registered factory can be reference from SQL DDL statements 
executed
+        against this context.
+
+        Args:
+            format: The value to be used in `STORED AS ${format}` clause.
+            factory: A PyCapsule that implements 
TableProviderFactoryExportable"

Review Comment:
   Docstring issues: “can be reference” should be “can be referenced”, and the 
`factory` arg description is inaccurate (the method takes an object 
implementing `TableProviderFactoryExportable`, not a PyCapsule) and includes a 
stray trailing quote. Please update the docstring to match the actual expected 
input and fix the typo/quote.
   ```suggestion
           The registered factory can be referenced from SQL DDL statements 
executed
           against this context.
   
           Args:
               format: The value to be used in `STORED AS ${format}` clause.
               factory: An object implementing 
:class:`TableProviderFactoryExportable`.
   ```



##########
examples/datafusion-ffi-example/python/tests/_test_table_provider_factory.py:
##########
@@ -0,0 +1,43 @@
+# 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
+
+import pyarrow as pa
+import pytest
+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 '';
+    """)

Review Comment:
   `CREATE EXTERNAL TABLE` is executed via a DataFrame returned from 
`ctx.sql(...)`; in this codebase DDL statements require `.collect()` to 
actually apply side effects (see e.g. `python/tests/test_expr.py` where `CREATE 
TABLE ...` is followed by `.collect()`). Without collecting here, the external 
table may never be created before the subsequent `SELECT` runs.
   ```suggestion
       """).collect()
   ```



##########
python/datafusion/context.py:
##########
@@ -830,6 +831,20 @@ def deregister_table(self, name: str) -> None:
         """Remove a table from the session."""
         self.ctx.deregister_table(name)
 
+    def register_table_factory(
+        self, format: str, factory: TableProviderFactoryExportable
+    ) -> None:
+        """Register a :py:class:`~datafusion.TableProviderFactoryExportable`.
+
+        The registered factory can be reference from SQL DDL statements 
executed
+        against this context.
+
+        Args:
+            format: The value to be used in `STORED AS ${format}` clause.
+            factory: A PyCapsule that implements 
TableProviderFactoryExportable"
+        """
+        self.ctx.register_table_factory(format, factory)

Review Comment:
   No core test coverage is added for `SessionContext.register_table_factory`. 
The new test is under `examples/...`, but the main pytest `testpaths` 
(pyproject.toml) only include `python/tests` and `python/datafusion`, so this 
behavior likely won’t run in CI. Consider adding a unit/integration test under 
`python/tests` that registers a factory and exercises `CREATE EXTERNAL TABLE` 
end-to-end.



##########
examples/datafusion-ffi-example/python/tests/_test_table_provider_factory.py:
##########
@@ -0,0 +1,43 @@
+# 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
+
+import pyarrow as pa
+import pytest

Review Comment:
   Unused imports `pyarrow as pa` and `pytest` will be flagged by the repo's 
Ruff configuration for `examples/*` (no per-file ignore for F401). Please 
remove them or use them in the test.
   ```suggestion
   
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to