This is an automated email from the ASF dual-hosted git repository.

agrove pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion-python.git


The following commit(s) were added to refs/heads/main by this push:
     new 1457b2e  Add bindings for DFSchema (#183)
1457b2e is described below

commit 1457b2eed54ab40e87d63211c23db724bf93453d
Author: Jeremy Dyer <[email protected]>
AuthorDate: Wed Feb 15 17:51:50 2023 -0500

    Add bindings for DFSchema (#183)
---
 datafusion/__init__.py              |  5 +++++
 datafusion/common.py                | 23 +++++++++++++++++++++++
 datafusion/tests/test_imports.py    |  7 +++++++
 src/{common/mod.rs => common.rs}    |  9 +++++++++
 src/common/{mod.rs => df_schema.rs} | 37 +++++++++++++++++++++++++++++++++++--
 src/lib.rs                          |  5 +++++
 6 files changed, 84 insertions(+), 2 deletions(-)

diff --git a/datafusion/__init__.py b/datafusion/__init__.py
index 0b4e896..6784eea 100644
--- a/datafusion/__init__.py
+++ b/datafusion/__init__.py
@@ -35,6 +35,10 @@ from ._internal import (
     ScalarUDF,
 )
 
+from .common import (
+    DFSchema,
+)
+
 from .expr import (
     Expr,
     TableScan,
@@ -54,6 +58,7 @@ __all__ = [
     "column",
     "literal",
     "TableScan",
+    "DFSchema",
 ]
 
 
diff --git a/datafusion/common.py b/datafusion/common.py
new file mode 100644
index 0000000..dd56640
--- /dev/null
+++ b/datafusion/common.py
@@ -0,0 +1,23 @@
+# 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 ._internal import common
+
+
+def __getattr__(name):
+    return getattr(common, name)
diff --git a/datafusion/tests/test_imports.py b/datafusion/tests/test_imports.py
index 1e8c796..66db592 100644
--- a/datafusion/tests/test_imports.py
+++ b/datafusion/tests/test_imports.py
@@ -26,6 +26,10 @@ from datafusion import (
     functions,
 )
 
+from datafusion.common import (
+    DFSchema,
+)
+
 from datafusion.expr import (
     Expr,
     TableScan,
@@ -49,6 +53,9 @@ def test_class_module_is_datafusion():
     ]:
         assert klass.__module__ == "datafusion"
 
+    for klass in [DFSchema]:
+        assert klass.__module__ == "datafusion.common"
+
     for klass in [Expr, TableScan]:
         assert klass.__module__ == "datafusion.expr"
 
diff --git a/src/common/mod.rs b/src/common.rs
similarity index 73%
copy from src/common/mod.rs
copy to src/common.rs
index 2d37f68..ba4438e 100644
--- a/src/common/mod.rs
+++ b/src/common.rs
@@ -15,5 +15,14 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use pyo3::prelude::*;
+
 pub mod data_type;
 pub mod df_field;
+pub mod df_schema;
+
+/// Initializes the `common` module to match the pattern of 
`datafusion-common` 
https://docs.rs/datafusion-common/18.0.0/datafusion_common/index.html
+pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
+    m.add_class::<df_schema::PyDFSchema>()?;
+    Ok(())
+}
diff --git a/src/common/mod.rs b/src/common/df_schema.rs
similarity index 52%
rename from src/common/mod.rs
rename to src/common/df_schema.rs
index 2d37f68..c46a771 100644
--- a/src/common/mod.rs
+++ b/src/common/df_schema.rs
@@ -15,5 +15,38 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub mod data_type;
-pub mod df_field;
+use std::sync::Arc;
+
+use datafusion_common::DFSchema;
+use pyo3::prelude::*;
+
+#[derive(Debug, Clone)]
+#[pyclass(name = "DFSchema", module = "datafusion.common", subclass)]
+pub struct PyDFSchema {
+    schema: Arc<DFSchema>,
+}
+
+impl From<PyDFSchema> for DFSchema {
+    fn from(schema: PyDFSchema) -> DFSchema {
+        (*schema.schema).clone()
+    }
+}
+
+impl From<DFSchema> for PyDFSchema {
+    fn from(schema: DFSchema) -> PyDFSchema {
+        PyDFSchema {
+            schema: Arc::new(schema),
+        }
+    }
+}
+
+#[pymethods]
+impl PyDFSchema {
+    #[pyo3(name = "empty")]
+    #[staticmethod]
+    fn py_empty() -> PyResult<Self> {
+        Ok(Self {
+            schema: Arc::new(DFSchema::empty()),
+        })
+    }
+}
diff --git a/src/lib.rs b/src/lib.rs
index b16ef75..f6d404e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -70,6 +70,11 @@ fn _internal(py: Python, m: &PyModule) -> PyResult<()> {
     m.add_class::<sql::logical::PyLogicalPlan>()?;
     m.add_class::<physical_plan::PyExecutionPlan>()?;
 
+    // Register `common` as a submodule. Matching `datafusion-common` 
https://docs.rs/datafusion-common/latest/datafusion_common/
+    let common = PyModule::new(py, "common")?;
+    common::init_module(common)?;
+    m.add_submodule(common)?;
+
     // Register `expr` as a submodule. Matching `datafusion-expr` 
https://docs.rs/datafusion-expr/latest/datafusion_expr/
     let expr = PyModule::new(py, "expr")?;
     expr::init_module(expr)?;

Reply via email to