1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// 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 datafusion::arrow::datatypes::DataType;
use datafusion_common::DFField;
use pyo3::prelude::*;

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.common", subclass)]
#[derive(Debug, Clone)]
pub struct PyDFField {
    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())
    }
}