You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "jdye64 (via GitHub)" <gi...@apache.org> on 2023/02/06 16:18:53 UTC

[GitHub] [arrow-datafusion-python] jdye64 opened a new pull request, #173: Arrow type bindings

jdye64 opened a new pull request, #173:
URL: https://github.com/apache/arrow-datafusion-python/pull/173

   # Which issue does this PR close?
   
   Closes #172


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion-python] jdye64 commented on a diff in pull request #173: Arrow type bindings

Posted by "jdye64 (via GitHub)" <gi...@apache.org>.
jdye64 commented on code in PR #173:
URL: https://github.com/apache/arrow-datafusion-python/pull/173#discussion_r1103120368


##########
src/common/data_type.rs:
##########
@@ -0,0 +1,511 @@
+// 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::DataFusionError;
+use pyo3::prelude::*;
+
+use crate::errors::py_datafusion_err;
+
+/// These bindings are tying together several disparate systems.
+/// You have SQL types for the SQL strings and RDBMS systems itself.
+/// Rust types for the DataFusion code
+/// Arrow types which represents the underlying arrow format
+/// Python types which represent the type in Python
+/// It is important to keep all of those types in a single
+/// and managable location. Therefore this structure exists
+/// 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)]
+pub struct DataTypeMap {
+    #[allow(dead_code)]
+    arrow_type: PyDataType,
+    #[allow(dead_code)]
+    python_type: PythonType,
+    #[allow(dead_code)]
+    sql_type: SqlType,
+}
+
+impl DataTypeMap {
+    fn new(arrow_type: DataType, python_type: PythonType, sql_type: SqlType) -> Self {
+        DataTypeMap {
+            arrow_type: PyDataType {
+                data_type: arrow_type,
+            },
+            python_type,
+            sql_type,
+        }
+    }
+
+    pub fn map_from_arrow_type(arrow_type: &DataType) -> Result<DataTypeMap, PyErr> {
+        match arrow_type {
+            DataType::Null => Ok(DataTypeMap::new(
+                DataType::Null,
+                PythonType::None,
+                SqlType::NULL,
+            )),
+            DataType::Boolean => Ok(DataTypeMap::new(
+                DataType::Boolean,
+                PythonType::Bool,
+                SqlType::BOOLEAN,
+            )),
+            DataType::Int8 => Ok(DataTypeMap::new(
+                DataType::Int8,
+                PythonType::Int,
+                SqlType::TINYINT,
+            )),
+            DataType::Int16 => Ok(DataTypeMap::new(
+                DataType::Int16,
+                PythonType::Int,
+                SqlType::SMALLINT,
+            )),
+            DataType::Int32 => Ok(DataTypeMap::new(
+                DataType::Int32,
+                PythonType::Int,
+                SqlType::INTEGER,
+            )),
+            DataType::Int64 => Ok(DataTypeMap::new(
+                DataType::Int64,
+                PythonType::Int,
+                SqlType::BIGINT,
+            )),
+            DataType::UInt8 => Ok(DataTypeMap::new(
+                DataType::UInt8,
+                PythonType::Int,
+                SqlType::TINYINT,
+            )),
+            DataType::UInt16 => Ok(DataTypeMap::new(
+                DataType::UInt16,
+                PythonType::Int,
+                SqlType::SMALLINT,
+            )),
+            DataType::UInt32 => Ok(DataTypeMap::new(
+                DataType::UInt32,
+                PythonType::Int,
+                SqlType::INTEGER,
+            )),
+            DataType::UInt64 => Ok(DataTypeMap::new(
+                DataType::UInt64,
+                PythonType::Int,
+                SqlType::BIGINT,
+            )),
+            DataType::Float16 => Ok(DataTypeMap::new(
+                DataType::Float16,
+                PythonType::Float,
+                SqlType::FLOAT,
+            )),
+            DataType::Float32 => Ok(DataTypeMap::new(
+                DataType::Float32,
+                PythonType::Float,
+                SqlType::FLOAT,
+            )),
+            DataType::Float64 => Ok(DataTypeMap::new(
+                DataType::Float64,
+                PythonType::Float,
+                SqlType::FLOAT,
+            )),
+            DataType::Timestamp(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Date32 => Ok(DataTypeMap::new(
+                DataType::Date32,
+                PythonType::Datetime,
+                SqlType::DATE,
+            )),
+            DataType::Date64 => Ok(DataTypeMap::new(
+                DataType::Date64,
+                PythonType::Datetime,
+                SqlType::DATE,
+            )),
+            DataType::Time32(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Time64(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Duration(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Interval(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Binary => Ok(DataTypeMap::new(
+                DataType::Binary,
+                PythonType::Bytes,
+                SqlType::BINARY,
+            )),
+            DataType::FixedSizeBinary(_) => Err(py_datafusion_err(
+                DataFusionError::NotImplemented(format!("{:?}", arrow_type)),
+            )),
+            DataType::LargeBinary => Ok(DataTypeMap::new(
+                DataType::LargeBinary,
+                PythonType::Bytes,
+                SqlType::BINARY,
+            )),
+            DataType::Utf8 => Ok(DataTypeMap::new(
+                DataType::Utf8,
+                PythonType::Str,
+                SqlType::VARCHAR,
+            )),
+            DataType::LargeUtf8 => Ok(DataTypeMap::new(
+                DataType::LargeUtf8,
+                PythonType::Str,
+                SqlType::VARCHAR,
+            )),
+            DataType::List(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(format!(
+                "{:?}",
+                arrow_type
+            )))),
+            DataType::FixedSizeList(_, _) => Err(py_datafusion_err(
+                DataFusionError::NotImplemented(format!("{:?}", arrow_type)),
+            )),
+            DataType::LargeList(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Struct(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Union(_, _, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Dictionary(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Decimal128(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Decimal256(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Map(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::RunEndEncoded(_, _) => Err(py_datafusion_err(
+                DataFusionError::NotImplemented(format!("{:?}", arrow_type)),
+            )),
+        }
+    }
+}
+
+#[pymethods]
+impl DataTypeMap {
+    #[new]
+    pub fn py_new(arrow_type: PyDataType, python_type: PythonType, sql_type: SqlType) -> Self {
+        DataTypeMap {
+            arrow_type,
+            python_type,
+            sql_type,
+        }
+    }
+
+    #[staticmethod]
+    #[pyo3(name = "arrow")]
+    pub fn py_map_from_arrow_type(arrow_type: &PyDataType) -> PyResult<DataTypeMap> {
+        DataTypeMap::map_from_arrow_type(&arrow_type.data_type)
+    }
+
+    #[staticmethod]
+    #[pyo3(name = "sql")]
+    pub fn map_from_sql_type(sql_type: &SqlType) -> PyResult<DataTypeMap> {
+        match sql_type {
+            SqlType::ANY => Err(py_datafusion_err(DataFusionError::NotImplemented(format!(
+                "{:?}",
+                sql_type
+            )))),
+            SqlType::ARRAY => Err(py_datafusion_err(DataFusionError::NotImplemented(format!(
+                "{:?}",
+                sql_type
+            )))),
+            SqlType::BIGINT => Ok(DataTypeMap::new(
+                DataType::Int64,
+                PythonType::Float,

Review Comment:
   I should have left a comment on this. This also wasn't what I thought but seems like a BIGINT wouldn't fit in a Python int and that a float should be used? I really don't know I was just going off of this [information](https://learn.microsoft.com/en-us/sql/machine-learning/python/python-libraries-and-data-types?view=sql-server-ver16) what do you think @martin-g 



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion-python] andygrove commented on pull request #173: Arrow type bindings

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #173:
URL: https://github.com/apache/arrow-datafusion-python/pull/173#issuecomment-1425997994

   > @andygrove just saw this. Yes. Question, what kind of error do we want to thrown when something happens like this you think? Should we add one for "UnImplemented"?
   
   Yes, in the core DataFusion crate we have `DataFusionError::NotImplemented(String)` so would be good to be consistent with that.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion-python] martin-g commented on pull request #173: Arrow type bindings

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on PR #173:
URL: https://github.com/apache/arrow-datafusion-python/pull/173#issuecomment-1426209014

   > CI is failing due to rate limits trying to consume the protoc github actions plugin
   
   See https://github.com/arduino/setup-protoc/issues/63#issuecomment-1366430802 


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion-python] andygrove merged pull request #173: Arrow type bindings

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove merged PR #173:
URL: https://github.com/apache/arrow-datafusion-python/pull/173


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion-python] jdye64 commented on pull request #173: Arrow type bindings

Posted by "jdye64 (via GitHub)" <gi...@apache.org>.
jdye64 commented on PR #173:
URL: https://github.com/apache/arrow-datafusion-python/pull/173#issuecomment-1424974548

   @andygrove just saw this. Yes. Question, what kind of error do we want to thrown when something happens like this you think? Should we add one for "UnImplemented"?


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion-python] andygrove commented on pull request #173: Arrow type bindings

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #173:
URL: https://github.com/apache/arrow-datafusion-python/pull/173#issuecomment-1421293138

   LGTM overall, but can we return `Err` rather than use `todo!` and `unimplemented!`?


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion-python] jdye64 commented on pull request #173: Arrow type bindings

Posted by "jdye64 (via GitHub)" <gi...@apache.org>.
jdye64 commented on PR #173:
URL: https://github.com/apache/arrow-datafusion-python/pull/173#issuecomment-1426188149

   CI is failing due to rate limits trying to consume the `protoc` github actions plugin. Will make another commit later in hopes this is resolved. If this continues to happen we might want to investigate moving to a manual protoc installation. That is much more messy but might be necessary if this keeps happening. Feel like that should happen in another PR however.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion-python] martin-g commented on a diff in pull request #173: Arrow type bindings

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #173:
URL: https://github.com/apache/arrow-datafusion-python/pull/173#discussion_r1103125722


##########
src/common/data_type.rs:
##########
@@ -0,0 +1,511 @@
+// 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::DataFusionError;
+use pyo3::prelude::*;
+
+use crate::errors::py_datafusion_err;
+
+/// These bindings are tying together several disparate systems.
+/// You have SQL types for the SQL strings and RDBMS systems itself.
+/// Rust types for the DataFusion code
+/// Arrow types which represents the underlying arrow format
+/// Python types which represent the type in Python
+/// It is important to keep all of those types in a single
+/// and managable location. Therefore this structure exists
+/// 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)]
+pub struct DataTypeMap {
+    #[allow(dead_code)]
+    arrow_type: PyDataType,
+    #[allow(dead_code)]
+    python_type: PythonType,
+    #[allow(dead_code)]
+    sql_type: SqlType,
+}
+
+impl DataTypeMap {
+    fn new(arrow_type: DataType, python_type: PythonType, sql_type: SqlType) -> Self {
+        DataTypeMap {
+            arrow_type: PyDataType {
+                data_type: arrow_type,
+            },
+            python_type,
+            sql_type,
+        }
+    }
+
+    pub fn map_from_arrow_type(arrow_type: &DataType) -> Result<DataTypeMap, PyErr> {
+        match arrow_type {
+            DataType::Null => Ok(DataTypeMap::new(
+                DataType::Null,
+                PythonType::None,
+                SqlType::NULL,
+            )),
+            DataType::Boolean => Ok(DataTypeMap::new(
+                DataType::Boolean,
+                PythonType::Bool,
+                SqlType::BOOLEAN,
+            )),
+            DataType::Int8 => Ok(DataTypeMap::new(
+                DataType::Int8,
+                PythonType::Int,
+                SqlType::TINYINT,
+            )),
+            DataType::Int16 => Ok(DataTypeMap::new(
+                DataType::Int16,
+                PythonType::Int,
+                SqlType::SMALLINT,
+            )),
+            DataType::Int32 => Ok(DataTypeMap::new(
+                DataType::Int32,
+                PythonType::Int,
+                SqlType::INTEGER,
+            )),
+            DataType::Int64 => Ok(DataTypeMap::new(
+                DataType::Int64,
+                PythonType::Int,
+                SqlType::BIGINT,
+            )),
+            DataType::UInt8 => Ok(DataTypeMap::new(
+                DataType::UInt8,
+                PythonType::Int,
+                SqlType::TINYINT,
+            )),
+            DataType::UInt16 => Ok(DataTypeMap::new(
+                DataType::UInt16,
+                PythonType::Int,
+                SqlType::SMALLINT,
+            )),
+            DataType::UInt32 => Ok(DataTypeMap::new(
+                DataType::UInt32,
+                PythonType::Int,
+                SqlType::INTEGER,
+            )),
+            DataType::UInt64 => Ok(DataTypeMap::new(
+                DataType::UInt64,
+                PythonType::Int,
+                SqlType::BIGINT,
+            )),
+            DataType::Float16 => Ok(DataTypeMap::new(
+                DataType::Float16,
+                PythonType::Float,
+                SqlType::FLOAT,
+            )),
+            DataType::Float32 => Ok(DataTypeMap::new(
+                DataType::Float32,
+                PythonType::Float,
+                SqlType::FLOAT,
+            )),
+            DataType::Float64 => Ok(DataTypeMap::new(
+                DataType::Float64,
+                PythonType::Float,
+                SqlType::FLOAT,
+            )),
+            DataType::Timestamp(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Date32 => Ok(DataTypeMap::new(
+                DataType::Date32,
+                PythonType::Datetime,
+                SqlType::DATE,
+            )),
+            DataType::Date64 => Ok(DataTypeMap::new(
+                DataType::Date64,
+                PythonType::Datetime,
+                SqlType::DATE,
+            )),
+            DataType::Time32(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Time64(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Duration(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Interval(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Binary => Ok(DataTypeMap::new(
+                DataType::Binary,
+                PythonType::Bytes,
+                SqlType::BINARY,
+            )),
+            DataType::FixedSizeBinary(_) => Err(py_datafusion_err(
+                DataFusionError::NotImplemented(format!("{:?}", arrow_type)),
+            )),
+            DataType::LargeBinary => Ok(DataTypeMap::new(
+                DataType::LargeBinary,
+                PythonType::Bytes,
+                SqlType::BINARY,
+            )),
+            DataType::Utf8 => Ok(DataTypeMap::new(
+                DataType::Utf8,
+                PythonType::Str,
+                SqlType::VARCHAR,
+            )),
+            DataType::LargeUtf8 => Ok(DataTypeMap::new(
+                DataType::LargeUtf8,
+                PythonType::Str,
+                SqlType::VARCHAR,
+            )),
+            DataType::List(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(format!(
+                "{:?}",
+                arrow_type
+            )))),
+            DataType::FixedSizeList(_, _) => Err(py_datafusion_err(
+                DataFusionError::NotImplemented(format!("{:?}", arrow_type)),
+            )),
+            DataType::LargeList(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Struct(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Union(_, _, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Dictionary(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Decimal128(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Decimal256(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Map(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::RunEndEncoded(_, _) => Err(py_datafusion_err(
+                DataFusionError::NotImplemented(format!("{:?}", arrow_type)),
+            )),
+        }
+    }
+}
+
+#[pymethods]
+impl DataTypeMap {
+    #[new]
+    pub fn py_new(arrow_type: PyDataType, python_type: PythonType, sql_type: SqlType) -> Self {
+        DataTypeMap {
+            arrow_type,
+            python_type,
+            sql_type,
+        }
+    }
+
+    #[staticmethod]
+    #[pyo3(name = "arrow")]
+    pub fn py_map_from_arrow_type(arrow_type: &PyDataType) -> PyResult<DataTypeMap> {
+        DataTypeMap::map_from_arrow_type(&arrow_type.data_type)
+    }
+
+    #[staticmethod]
+    #[pyo3(name = "sql")]
+    pub fn map_from_sql_type(sql_type: &SqlType) -> PyResult<DataTypeMap> {
+        match sql_type {
+            SqlType::ANY => Err(py_datafusion_err(DataFusionError::NotImplemented(format!(
+                "{:?}",
+                sql_type
+            )))),
+            SqlType::ARRAY => Err(py_datafusion_err(DataFusionError::NotImplemented(format!(
+                "{:?}",
+                sql_type
+            )))),
+            SqlType::BIGINT => Ok(DataTypeMap::new(
+                DataType::Int64,
+                PythonType::Float,

Review Comment:
   I don't have time now to read the Python docs but I noticed that it inconsistent with https://github.com/apache/arrow-datafusion-python/pull/173/files#diff-a109fe9b61a82ff498f40948cfa040798d7d40213c7df7669df526f914b22a04R82-R86 and added this 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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion-python] martin-g commented on a diff in pull request #173: Arrow type bindings

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #173:
URL: https://github.com/apache/arrow-datafusion-python/pull/173#discussion_r1103099360


##########
src/sql/exceptions.rs:
##########
@@ -0,0 +1,42 @@
+// 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 std::fmt::Debug;
+
+use pyo3::{create_exception, PyErr};
+
+// Identifies exceptions that occur while attempting to generate a `LogicalPlan` from a SQL string
+create_exception!(rust, ParsingException, pyo3::exceptions::PyException);
+
+// Identifies exceptions that occur during attempts to optimization an existing `LogicalPlan`
+create_exception!(rust, OptimizationException, pyo3::exceptions::PyException);
+
+pub fn py_type_err(e: impl Debug + std::fmt::Display) -> PyErr {

Review Comment:
   nit: extract `use std::fmt::Display` as the one for `Debug`



##########
src/common/data_type.rs:
##########
@@ -0,0 +1,511 @@
+// 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::DataFusionError;
+use pyo3::prelude::*;
+
+use crate::errors::py_datafusion_err;
+
+/// These bindings are tying together several disparate systems.
+/// You have SQL types for the SQL strings and RDBMS systems itself.
+/// Rust types for the DataFusion code
+/// Arrow types which represents the underlying arrow format
+/// Python types which represent the type in Python
+/// It is important to keep all of those types in a single
+/// and managable location. Therefore this structure exists
+/// 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)]
+pub struct DataTypeMap {
+    #[allow(dead_code)]
+    arrow_type: PyDataType,
+    #[allow(dead_code)]
+    python_type: PythonType,
+    #[allow(dead_code)]
+    sql_type: SqlType,
+}
+
+impl DataTypeMap {
+    fn new(arrow_type: DataType, python_type: PythonType, sql_type: SqlType) -> Self {
+        DataTypeMap {
+            arrow_type: PyDataType {
+                data_type: arrow_type,
+            },
+            python_type,
+            sql_type,
+        }
+    }
+
+    pub fn map_from_arrow_type(arrow_type: &DataType) -> Result<DataTypeMap, PyErr> {
+        match arrow_type {
+            DataType::Null => Ok(DataTypeMap::new(
+                DataType::Null,
+                PythonType::None,
+                SqlType::NULL,
+            )),
+            DataType::Boolean => Ok(DataTypeMap::new(
+                DataType::Boolean,
+                PythonType::Bool,
+                SqlType::BOOLEAN,
+            )),
+            DataType::Int8 => Ok(DataTypeMap::new(
+                DataType::Int8,
+                PythonType::Int,
+                SqlType::TINYINT,
+            )),
+            DataType::Int16 => Ok(DataTypeMap::new(
+                DataType::Int16,
+                PythonType::Int,
+                SqlType::SMALLINT,
+            )),
+            DataType::Int32 => Ok(DataTypeMap::new(
+                DataType::Int32,
+                PythonType::Int,
+                SqlType::INTEGER,
+            )),
+            DataType::Int64 => Ok(DataTypeMap::new(
+                DataType::Int64,
+                PythonType::Int,
+                SqlType::BIGINT,
+            )),
+            DataType::UInt8 => Ok(DataTypeMap::new(
+                DataType::UInt8,
+                PythonType::Int,
+                SqlType::TINYINT,
+            )),
+            DataType::UInt16 => Ok(DataTypeMap::new(
+                DataType::UInt16,
+                PythonType::Int,
+                SqlType::SMALLINT,
+            )),
+            DataType::UInt32 => Ok(DataTypeMap::new(
+                DataType::UInt32,
+                PythonType::Int,
+                SqlType::INTEGER,
+            )),
+            DataType::UInt64 => Ok(DataTypeMap::new(
+                DataType::UInt64,
+                PythonType::Int,
+                SqlType::BIGINT,
+            )),
+            DataType::Float16 => Ok(DataTypeMap::new(
+                DataType::Float16,
+                PythonType::Float,
+                SqlType::FLOAT,
+            )),
+            DataType::Float32 => Ok(DataTypeMap::new(
+                DataType::Float32,
+                PythonType::Float,
+                SqlType::FLOAT,
+            )),
+            DataType::Float64 => Ok(DataTypeMap::new(
+                DataType::Float64,
+                PythonType::Float,
+                SqlType::FLOAT,
+            )),
+            DataType::Timestamp(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Date32 => Ok(DataTypeMap::new(
+                DataType::Date32,
+                PythonType::Datetime,
+                SqlType::DATE,
+            )),
+            DataType::Date64 => Ok(DataTypeMap::new(
+                DataType::Date64,
+                PythonType::Datetime,
+                SqlType::DATE,
+            )),
+            DataType::Time32(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Time64(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Duration(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Interval(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Binary => Ok(DataTypeMap::new(
+                DataType::Binary,
+                PythonType::Bytes,
+                SqlType::BINARY,
+            )),
+            DataType::FixedSizeBinary(_) => Err(py_datafusion_err(
+                DataFusionError::NotImplemented(format!("{:?}", arrow_type)),
+            )),
+            DataType::LargeBinary => Ok(DataTypeMap::new(
+                DataType::LargeBinary,
+                PythonType::Bytes,
+                SqlType::BINARY,
+            )),
+            DataType::Utf8 => Ok(DataTypeMap::new(
+                DataType::Utf8,
+                PythonType::Str,
+                SqlType::VARCHAR,
+            )),
+            DataType::LargeUtf8 => Ok(DataTypeMap::new(
+                DataType::LargeUtf8,
+                PythonType::Str,
+                SqlType::VARCHAR,
+            )),
+            DataType::List(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(format!(
+                "{:?}",
+                arrow_type
+            )))),
+            DataType::FixedSizeList(_, _) => Err(py_datafusion_err(
+                DataFusionError::NotImplemented(format!("{:?}", arrow_type)),
+            )),
+            DataType::LargeList(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Struct(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Union(_, _, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Dictionary(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Decimal128(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Decimal256(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Map(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::RunEndEncoded(_, _) => Err(py_datafusion_err(
+                DataFusionError::NotImplemented(format!("{:?}", arrow_type)),
+            )),
+        }
+    }
+}
+
+#[pymethods]
+impl DataTypeMap {
+    #[new]
+    pub fn py_new(arrow_type: PyDataType, python_type: PythonType, sql_type: SqlType) -> Self {
+        DataTypeMap {
+            arrow_type,
+            python_type,
+            sql_type,
+        }
+    }
+
+    #[staticmethod]
+    #[pyo3(name = "arrow")]
+    pub fn py_map_from_arrow_type(arrow_type: &PyDataType) -> PyResult<DataTypeMap> {
+        DataTypeMap::map_from_arrow_type(&arrow_type.data_type)
+    }
+
+    #[staticmethod]
+    #[pyo3(name = "sql")]
+    pub fn map_from_sql_type(sql_type: &SqlType) -> PyResult<DataTypeMap> {
+        match sql_type {
+            SqlType::ANY => Err(py_datafusion_err(DataFusionError::NotImplemented(format!(
+                "{:?}",
+                sql_type
+            )))),
+            SqlType::ARRAY => Err(py_datafusion_err(DataFusionError::NotImplemented(format!(
+                "{:?}",
+                sql_type
+            )))),
+            SqlType::BIGINT => Ok(DataTypeMap::new(
+                DataType::Int64,
+                PythonType::Float,

Review Comment:
   ```suggestion
                   PythonType::Int,
   ```



##########
src/common/data_type.rs:
##########
@@ -0,0 +1,511 @@
+// 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::DataFusionError;
+use pyo3::prelude::*;
+
+use crate::errors::py_datafusion_err;
+
+/// These bindings are tying together several disparate systems.
+/// You have SQL types for the SQL strings and RDBMS systems itself.
+/// Rust types for the DataFusion code
+/// Arrow types which represents the underlying arrow format
+/// Python types which represent the type in Python
+/// It is important to keep all of those types in a single
+/// and managable location. Therefore this structure exists
+/// 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)]
+pub struct DataTypeMap {
+    #[allow(dead_code)]
+    arrow_type: PyDataType,
+    #[allow(dead_code)]
+    python_type: PythonType,
+    #[allow(dead_code)]
+    sql_type: SqlType,
+}
+
+impl DataTypeMap {
+    fn new(arrow_type: DataType, python_type: PythonType, sql_type: SqlType) -> Self {
+        DataTypeMap {
+            arrow_type: PyDataType {
+                data_type: arrow_type,
+            },
+            python_type,
+            sql_type,
+        }
+    }
+
+    pub fn map_from_arrow_type(arrow_type: &DataType) -> Result<DataTypeMap, PyErr> {
+        match arrow_type {
+            DataType::Null => Ok(DataTypeMap::new(
+                DataType::Null,
+                PythonType::None,
+                SqlType::NULL,
+            )),
+            DataType::Boolean => Ok(DataTypeMap::new(
+                DataType::Boolean,
+                PythonType::Bool,
+                SqlType::BOOLEAN,
+            )),
+            DataType::Int8 => Ok(DataTypeMap::new(
+                DataType::Int8,
+                PythonType::Int,
+                SqlType::TINYINT,
+            )),
+            DataType::Int16 => Ok(DataTypeMap::new(
+                DataType::Int16,
+                PythonType::Int,
+                SqlType::SMALLINT,
+            )),
+            DataType::Int32 => Ok(DataTypeMap::new(
+                DataType::Int32,
+                PythonType::Int,
+                SqlType::INTEGER,
+            )),
+            DataType::Int64 => Ok(DataTypeMap::new(
+                DataType::Int64,
+                PythonType::Int,
+                SqlType::BIGINT,
+            )),
+            DataType::UInt8 => Ok(DataTypeMap::new(
+                DataType::UInt8,
+                PythonType::Int,
+                SqlType::TINYINT,
+            )),
+            DataType::UInt16 => Ok(DataTypeMap::new(
+                DataType::UInt16,
+                PythonType::Int,
+                SqlType::SMALLINT,
+            )),
+            DataType::UInt32 => Ok(DataTypeMap::new(
+                DataType::UInt32,
+                PythonType::Int,
+                SqlType::INTEGER,
+            )),
+            DataType::UInt64 => Ok(DataTypeMap::new(
+                DataType::UInt64,
+                PythonType::Int,
+                SqlType::BIGINT,
+            )),
+            DataType::Float16 => Ok(DataTypeMap::new(
+                DataType::Float16,
+                PythonType::Float,
+                SqlType::FLOAT,
+            )),
+            DataType::Float32 => Ok(DataTypeMap::new(
+                DataType::Float32,
+                PythonType::Float,
+                SqlType::FLOAT,
+            )),
+            DataType::Float64 => Ok(DataTypeMap::new(
+                DataType::Float64,
+                PythonType::Float,
+                SqlType::FLOAT,
+            )),
+            DataType::Timestamp(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Date32 => Ok(DataTypeMap::new(
+                DataType::Date32,
+                PythonType::Datetime,
+                SqlType::DATE,
+            )),
+            DataType::Date64 => Ok(DataTypeMap::new(
+                DataType::Date64,
+                PythonType::Datetime,
+                SqlType::DATE,
+            )),
+            DataType::Time32(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Time64(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Duration(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Interval(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Binary => Ok(DataTypeMap::new(
+                DataType::Binary,
+                PythonType::Bytes,
+                SqlType::BINARY,
+            )),
+            DataType::FixedSizeBinary(_) => Err(py_datafusion_err(
+                DataFusionError::NotImplemented(format!("{:?}", arrow_type)),
+            )),
+            DataType::LargeBinary => Ok(DataTypeMap::new(
+                DataType::LargeBinary,
+                PythonType::Bytes,
+                SqlType::BINARY,
+            )),
+            DataType::Utf8 => Ok(DataTypeMap::new(
+                DataType::Utf8,
+                PythonType::Str,
+                SqlType::VARCHAR,
+            )),
+            DataType::LargeUtf8 => Ok(DataTypeMap::new(
+                DataType::LargeUtf8,
+                PythonType::Str,
+                SqlType::VARCHAR,
+            )),
+            DataType::List(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(format!(
+                "{:?}",
+                arrow_type
+            )))),
+            DataType::FixedSizeList(_, _) => Err(py_datafusion_err(
+                DataFusionError::NotImplemented(format!("{:?}", arrow_type)),
+            )),
+            DataType::LargeList(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Struct(_) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Union(_, _, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Dictionary(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Decimal128(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Decimal256(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::Map(_, _) => Err(py_datafusion_err(DataFusionError::NotImplemented(
+                format!("{:?}", arrow_type),
+            ))),
+            DataType::RunEndEncoded(_, _) => Err(py_datafusion_err(
+                DataFusionError::NotImplemented(format!("{:?}", arrow_type)),
+            )),
+        }
+    }
+}
+
+#[pymethods]
+impl DataTypeMap {
+    #[new]
+    pub fn py_new(arrow_type: PyDataType, python_type: PythonType, sql_type: SqlType) -> Self {
+        DataTypeMap {
+            arrow_type,
+            python_type,
+            sql_type,
+        }
+    }
+
+    #[staticmethod]
+    #[pyo3(name = "arrow")]
+    pub fn py_map_from_arrow_type(arrow_type: &PyDataType) -> PyResult<DataTypeMap> {
+        DataTypeMap::map_from_arrow_type(&arrow_type.data_type)
+    }
+
+    #[staticmethod]
+    #[pyo3(name = "sql")]
+    pub fn map_from_sql_type(sql_type: &SqlType) -> PyResult<DataTypeMap> {

Review Comment:
   for consistency:
   ```suggestion
       pub fn py_map_from_sql_type(sql_type: &SqlType) -> PyResult<DataTypeMap> {
   ```



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org