8000 Add bindings for DFSchema (#183) · andygrove/datafusion-python@1457b2e · GitHub
[go: up one dir, main page]

8000 Skip to content

Commit 1457b2e

Browse files
authored
Add bindings for DFSchema (apache#183)
1 parent d51c54d commit 1457b2e

File tree

6 files changed

+101
-0
lines changed

6 files changed

+101
-0
lines changed

datafusion/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
ScalarUDF,
3636
)
3737

38+
from .common import (
39+
DFSchema,
40+
)
41+
3842
from .expr import (
3943
Expr,
4044
TableScan,
@@ -54,6 +58,7 @@
5458
"column",
5559
"literal",
5660
"TableScan",
61+
"DFSchema",
5762
]
5863

5964

datafusion/common.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
19+
from ._internal import common
20+
21+
22+
def __getattr__(name):
23+
return getattr(common, name)

datafusion/tests/test_imports.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
functions,
2727
)
2828

29+
from datafusion.common import (
30+
DFSchema,
31+
)
32+
2933
from datafusion.expr import (
3034
Expr,
3135
TableScan,
@@ -49,6 +53,9 @@ def test_class_module_is_datafusion():
4953
]:
5054
assert klass.__module__ == "datafusion"
5155

56+
for klass in [DFSchema]:
57+
assert klass.__module__ == "datafusion.common"
58+
5259
for klass in [Expr, TableScan]:
5360
assert klass.__module__ == "datafusion.expr"
5461

src/common/mod.rs renamed to src/common.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,14 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
use pyo3::prelude::*;
19+
1820
pub mod data_type;
1921
pub mod df_field;
22+
pub mod df_schema;
23+
24+
/// Initializes the `common` module to match the pattern of `datafusion-common` https://docs.rs/datafusion-common/18.0.0/datafusion_common/index.html
25+
pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
26+
m.add_class::<df_schema::PyDFSchema>()?;
27+
Ok(())
28+
}

src/common/df_schema.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use std::sync::Arc;
19+
20+
use datafusion_common::DFSchema;
21+
use pyo3::prelude::*;
22+
23+
#[derive(Debug, Clone)]
24+
#[pyclass(name = "DFSchema", module = "datafusion.common", subclass)]
25+
pub struct PyDFSchema {
26+
schema: Arc<DFSchema>,
27+
}
28+
29+
impl From<PyDFSchema> for DFSchema {
30+
fn from(schema: PyDFSchema) -> DFSchema {
31+
(*schema.schema).clone()
32+
}
33+
}
34+
35+
impl From<DFSchema> for PyDFSchema {
36+
fn from(schema: DFSchema) -> PyDFSchema {
37+
PyDFSchema {
38+
schema: Arc::new(schema),
39+
}
40+
}
41+
}
42+
43+
#[pymethods]
44+
impl PyDFSchema {
45+
#[pyo3(name = "empty")]
46+
#[staticmethod]
47+
fn py_empty() -> PyResult<Self> {
48+
Ok(Self {
49+
schema: Arc::new(DFSchema::empty()),
50+
})
51+
}
52+
}

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ fn _internal(py: Python, m: &PyModule) -> PyResult<()> {
7070
m.add_class::<sql::logical::PyLogicalPlan>()?;
7171
m.add_class::<physical_plan::PyExecutionPlan>()?;
7272

73+
// Register `common` as a submodule. Matching `datafusion-common` https://docs.rs/datafusion-common/latest/datafusion_common/
74+
let common = PyModule::new(py, "common")?;
75+
common::init_module(common)?;
76+
m.add_submodule(common)?;
77+
7378
// Register `expr` as a submodule. Matching `datafusion-expr` https://docs.rs/datafusion-expr/latest/datafusion_expr/
7479
let expr = PyModule::new(py, "expr")?;
7580
expr::init_module(expr)?;

0 commit comments

Comments
 (0)
0