Skip to content
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

Dffield bindings #185

Merged
merged 6 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions datafusion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
)

from .common import (
DFField,
DFSchema,
)

Expand All @@ -61,6 +62,7 @@
"TableScan",
"Projection",
"DFSchema",
"DFField",
]


Expand Down
3 changes: 2 additions & 1 deletion datafusion/tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
)

from datafusion.common import (
DFField,
DFSchema,
)

Expand Down Expand Up @@ -57,7 +58,7 @@ def test_class_module_is_datafusion():
for klass in [Expr, Projection, TableScan]:
assert klass.__module__ == "datafusion.expr"

for klass in [DFSchema]:
for klass in [DFField, DFSchema]:
assert klass.__module__ == "datafusion.common"


Expand Down
5 changes: 5 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ 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>()?;
m.add_class::<df_field::PyDFField>()?;
m.add_class::<data_type::PyDataType>()?;
m.add_class::<data_type::DataTypeMap>()?;
m.add_class::<data_type::PythonType>()?;
m.add_class::<data_type::SqlType>()?;
Ok(())
}
8 changes: 4 additions & 4 deletions src/common/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::errors::py_datafusion_err;
/// to map those types and provide a simple place for developers
/// to map types from one system to another.
#[derive(Debug, Clone)]
#[pyclass(name = "DataTypeMap", module = "datafusion", subclass)]
#[pyclass(name = "DataTypeMap", module = "datafusion.common", subclass)]
pub struct DataTypeMap {
#[allow(dead_code)]
arrow_type: PyDataType,
Expand Down Expand Up @@ -419,7 +419,7 @@ impl DataTypeMap {
/// Since `DataType` exists in another package we cannot make that happen here so we wrap
/// `DataType` as `PyDataType` This exists solely to satisfy those constraints.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[pyclass(name = "DataType", module = "datafusion")]
#[pyclass(name = "DataType", module = "datafusion.common")]
pub struct PyDataType {
data_type: DataType,
}
Expand All @@ -438,7 +438,7 @@ impl From<DataType> for PyDataType {

/// Represents the possible Python types that can be mapped to the SQL types
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[pyclass(name = "PythonType", module = "datafusion")]
#[pyclass(name = "PythonType", module = "datafusion.common")]
pub enum PythonType {
Array,
Bool,
Expand All @@ -458,7 +458,7 @@ pub enum PythonType {
#[allow(non_camel_case_types)]
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[pyclass(name = "SqlType", module = "datafusion")]
#[pyclass(name = "SqlType", module = "datafusion.common")]
pub enum SqlType {
ANY,
ARRAY,
Expand Down
95 changes: 80 additions & 15 deletions src/common/df_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,92 @@
// specific language governing permissions and limitations
// under the License.

use datafusion::arrow::datatypes::Field;
use datafusion::arrow::datatypes::DataType;
use datafusion_common::DFField;
use pyo3::prelude::*;

use crate::common::data_type::DataTypeMap;
use super::data_type::PyDataType;

/// PyDFField wraps an arrow-datafusion `DFField` struct type
/// and also supplies convenience methods for interacting
/// with the `DFField` instance in the context of Python
#[pyclass(name = "DFField", module = "datafusion", subclass)]
#[pyclass(name = "DFField", module = "datafusion.common", subclass)]
#[derive(Debug, Clone)]
pub struct PyDFField {
/// Optional qualifier (usually a table or relation name)
#[allow(dead_code)]
qualifier: Option<String>,
#[allow(dead_code)]
name: String,
#[allow(dead_code)]
data_type: DataTypeMap,
/// Arrow field definition
#[allow(dead_code)]
field: Field,
#[allow(dead_code)]
index: usize,
field: DFField,
}

impl From<PyDFField> for DFField {
fn from(py_field: PyDFField) -> DFField {
py_field.field
}
}

impl From<DFField> for PyDFField {
fn from(field: DFField) -> PyDFField {
PyDFField { field }
}
}

#[pymethods]
impl PyDFField {
#[new]
#[pyo3(signature = (qualifier=None, name="", data_type=DataType::Int64.into(), nullable=false))]
fn new(qualifier: Option<&str>, name: &str, data_type: PyDataType, nullable: bool) -> Self {
PyDFField {
field: DFField::new(qualifier, name, data_type.into(), nullable),
}
}

// TODO: Need bindings for Array `Field` first
// #[staticmethod]
// #[pyo3(name = "from")]
// fn py_from(field: Field) -> Self {}

// TODO: Need bindings for Array `Field` first
// #[staticmethod]
// #[pyo3(name = "from_qualified")]
// fn py_from_qualified(field: Field) -> Self {}

#[pyo3(name = "name")]
fn py_name(&self) -> PyResult<String> {
Ok(self.field.name().clone())
}

#[pyo3(name = "data_type")]
fn py_data_type(&self) -> PyResult<PyDataType> {
Ok(self.field.data_type().clone().into())
}

#[pyo3(name = "is_nullable")]
fn py_is_nullable(&self) -> PyResult<bool> {
Ok(self.field.is_nullable())
}

#[pyo3(name = "qualified_name")]
fn py_qualified_name(&self) -> PyResult<String> {
Ok(self.field.qualified_name())
}

// TODO: Need bindings for `Column` first
// #[pyo3(name = "qualified_column")]
// fn py_qualified_column(&self) -> PyResult<PyColumn> {}

// TODO: Need bindings for `Column` first
// #[pyo3(name = "unqualified_column")]
// fn py_unqualified_column(&self) -> PyResult<PyColumn> {}

#[pyo3(name = "qualifier")]
fn py_qualifier(&self) -> PyResult<Option<&String>> {
Ok(self.field.qualifier())
}

// TODO: Need bindings for Arrow `Field` first
// #[pyo3(name = "field")]
// fn py_field(&self) -> PyResult<Field> {}

#[pyo3(name = "strip_qualifier")]
fn py_strip_qualifier(&self) -> PyResult<Self> {
Ok(self.field.clone().strip_qualifier().into())
}
}