You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/10/08 15:03:58 UTC

[GitHub] [arrow] alamb opened a new pull request #8400: ARROW-10236: Unify type casting logic in DataFusion

alamb opened a new pull request #8400:
URL: https://github.com/apache/arrow/pull/8400


   This is a proposed approach - if people like this approach I will write proper tests (to ensure that the cast kernel support and this function remain in sync).
   
   This PR brings DataFusion to parity with the type casting supported by arrow and allows DataFusion to plan all casts that are supported by the arrow cast kernel
   
   Previously the notions of coercion and casting were somewhat conflated. I have tried to clarify them in https://github.com/apache/arrow/pull/8399 and this PR. See also https://github.com/apache/arrow/pull/8340#discussion_r501257096 for more discussion.
   
   I personally want this functionality so when I add support for `DictionaryArray`  casts in Arrow (https://github.com/apache/arrow/pull/8346)  they can also be used in DataFusion.
   
   Codewise, I am concerned about the duplication in logic between this and cast.rs. However, the test I have in mind will ensure they don't get out of sync
   
   Questions for reviewers:
   1) Would this code / test be better to include in the arrow cast kernel (cast.rs) itself? To keep the "support" matrix code closer to the implementation?
   


----------------------------------------------------------------
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.

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



[GitHub] [arrow] jorgecarleitao commented on a change in pull request #8400: ARROW-10236: [Rust][DataFusion] Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on a change in pull request #8400:
URL: https://github.com/apache/arrow/pull/8400#discussion_r501802894



##########
File path: rust/datafusion/src/physical_plan/type_casting.rs
##########
@@ -0,0 +1,218 @@
+// 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.
+
+//! This module provides a way of checking what type casts are
+//! supported at planning time for DataFusion. Since DataFusion uses
+//! the Arrow `cast` compute kernel, the supported casts are the same
+//! as the Arrow casts.
+//!
+//! The rules in this module are designed to be redundant with the
+//! rules in the Arrow `cast` kernel. The redundancy is needed so that
+//! DataFusion can generate an error at plan time rather than during
+//! execution (which could happen many hours after execution starts,
+//! when the query finally reaches that point)
+//!
+
+use arrow::datatypes::*;
+
+/// Return true if a value of type `from_type` can be cast into a
+/// value of `to_type`. Note that such as cast may be lossy. For
+/// lossless type conversions, see the `type_coercion` module
+///
+/// See the module level documentation for more detail on casting
+pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
+    use self::DataType::*;
+    if from_type == to_type {
+        return true;
+    }
+
+    // Note this is meant to mirror the structure in arrow/src/compute/kernels/cast.rs
+    match (from_type, to_type) {

Review comment:
       This is something that I have been thinking about: in DataFusion, because we use dynamically typed arrays, we often have to annotate which types are supported by each arrow kernel / operation.
   
   Thus, we need to duplicate these `match`es to enumerate the valid types accepted by the kernels, so that we error during planning.
   
   I wonder if these functions shouldn't be closer to the implementation (i.e. in the `arrow::compute::kernels`), or, alternatively, if we could find an idiom that would allow us to write these `match`es one time (again, in compute::kernel).
   
   It seems to me that the pattern is:
   
   for compute
   ```
   match array.data_type() {
      ... => compute(array)
   }
   ```
   for datatypes:
   
   ```
   match datatype {
       ... => return Some(return_datatype)
   }
   ```
   
   one idea would be to use
   
   ```
   match datatype {
       ... => return Some((closure_that_computes, return_datatype))
   }
   ```
   
   so that both use-cases could be written in a single match (and reduce the risk of mis-typing / change in one place without a change in another place).
   
   This comment is not specific to this PR, which I need to go through: I was just curious about your thoughts on this, since you have been touching in a couple of these recently.




----------------------------------------------------------------
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.

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



[GitHub] [arrow] alamb commented on a change in pull request #8400: ARROW-10236: [Rust][DataFusion] Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #8400:
URL: https://github.com/apache/arrow/pull/8400#discussion_r501812661



##########
File path: rust/datafusion/src/physical_plan/type_casting.rs
##########
@@ -0,0 +1,218 @@
+// 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.
+
+//! This module provides a way of checking what type casts are
+//! supported at planning time for DataFusion. Since DataFusion uses
+//! the Arrow `cast` compute kernel, the supported casts are the same
+//! as the Arrow casts.
+//!
+//! The rules in this module are designed to be redundant with the
+//! rules in the Arrow `cast` kernel. The redundancy is needed so that
+//! DataFusion can generate an error at plan time rather than during
+//! execution (which could happen many hours after execution starts,
+//! when the query finally reaches that point)
+//!
+
+use arrow::datatypes::*;
+
+/// Return true if a value of type `from_type` can be cast into a
+/// value of `to_type`. Note that such as cast may be lossy. For
+/// lossless type conversions, see the `type_coercion` module
+///
+/// See the module level documentation for more detail on casting
+pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
+    use self::DataType::*;
+    if from_type == to_type {
+        return true;
+    }
+
+    // Note this is meant to mirror the structure in arrow/src/compute/kernels/cast.rs
+    match (from_type, to_type) {

Review comment:
       @jorgecarleitao  -- I think the idea of using a single `match` in cast.rs is a good one. I'll try and whip up a proposed PR with that pattern for discussion (though probably not until tomorrow morning US Eastern time, or the weekend)




----------------------------------------------------------------
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.

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



[GitHub] [arrow] github-actions[bot] commented on pull request #8400: ARROW-10236: Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #8400:
URL: https://github.com/apache/arrow/pull/8400#issuecomment-705633631


   https://issues.apache.org/jira/browse/ARROW-10236


----------------------------------------------------------------
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.

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



[GitHub] [arrow] nevi-me commented on a change in pull request #8400: ARROW-10236: [Rust][DataFusion] Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
nevi-me commented on a change in pull request #8400:
URL: https://github.com/apache/arrow/pull/8400#discussion_r501850814



##########
File path: rust/datafusion/src/physical_plan/type_casting.rs
##########
@@ -0,0 +1,218 @@
+// 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.
+
+//! This module provides a way of checking what type casts are
+//! supported at planning time for DataFusion. Since DataFusion uses
+//! the Arrow `cast` compute kernel, the supported casts are the same
+//! as the Arrow casts.
+//!
+//! The rules in this module are designed to be redundant with the
+//! rules in the Arrow `cast` kernel. The redundancy is needed so that
+//! DataFusion can generate an error at plan time rather than during
+//! execution (which could happen many hours after execution starts,
+//! when the query finally reaches that point)
+//!
+
+use arrow::datatypes::*;
+
+/// Return true if a value of type `from_type` can be cast into a
+/// value of `to_type`. Note that such as cast may be lossy. For
+/// lossless type conversions, see the `type_coercion` module
+///
+/// See the module level documentation for more detail on casting
+pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
+    use self::DataType::*;
+    if from_type == to_type {
+        return true;
+    }
+
+    // Note this is meant to mirror the structure in arrow/src/compute/kernels/cast.rs
+    match (from_type, to_type) {

Review comment:
       I was also going to suggest if we move `can_cast_types` to Arrow




----------------------------------------------------------------
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.

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



[GitHub] [arrow] alamb commented on pull request #8400: ARROW-10236: [Rust][DataFusion] Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
alamb commented on pull request #8400:
URL: https://github.com/apache/arrow/pull/8400#issuecomment-708523165


   https://github.com/apache/arrow/pull/8460 is the follow on


----------------------------------------------------------------
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.

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



[GitHub] [arrow] alamb commented on a change in pull request #8400: ARROW-10236: Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #8400:
URL: https://github.com/apache/arrow/pull/8400#discussion_r501795062



##########
File path: rust/datafusion/src/logical_plan/mod.rs
##########
@@ -323,12 +324,13 @@ impl Expr {
     ///
     /// # Errors
     ///
-    /// This function errors when it is impossible to cast the expression to the target [arrow::datatypes::DataType].
+    /// This function errors when it is impossible to cast the
+    /// expression to the target [arrow::datatypes::DataType].
     pub fn cast_to(&self, cast_to_type: &DataType, schema: &Schema) -> Result<Expr> {
         let this_type = self.get_type(schema)?;
         if this_type == *cast_to_type {
             Ok(self.clone())
-        } else if can_coerce_from(cast_to_type, &this_type) {
+        } else if can_cast_types(&this_type, cast_to_type) {

Review comment:
       the idea is to use a common `can_cast_types` function to detect valid casts at plan time, and make `can_cast_types` consistent with the arrow cast kernel

##########
File path: rust/datafusion/src/physical_plan/type_casting.rs
##########
@@ -0,0 +1,218 @@
+// 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.
+
+//! This module provides a way of checking what type casts are
+//! supported at planning time for DataFusion. Since DataFusion uses
+//! the Arrow `cast` compute kernel, the supported casts are the same
+//! as the Arrow casts.
+//!
+//! The rules in this module are designed to be redundant with the
+//! rules in the Arrow `cast` kernel. The redundancy is needed so that
+//! DataFusion can generate an error at plan time rather than during
+//! execution (which could happen many hours after execution starts,
+//! when the query finally reaches that point)
+//!
+
+use arrow::datatypes::*;
+
+/// Return true if a value of type `from_type` can be cast into a
+/// value of `to_type`. Note that such as cast may be lossy. For
+/// lossless type conversions, see the `type_coercion` module
+///
+/// See the module level documentation for more detail on casting
+pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
+    use self::DataType::*;
+    if from_type == to_type {
+        return true;
+    }
+
+    // Note this is meant to mirror the structure in arrow/src/compute/kernels/cast.rs
+    match (from_type, to_type) {

Review comment:
       @jorgecarleitao  -- I think the idea of using a single `match` in cast.rs is a good one. I'll try and whip up a proposed PR with that pattern for discussion (though probably not until tomorrow morning US Eastern time, or the weekend)




----------------------------------------------------------------
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.

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



[GitHub] [arrow] alamb closed pull request #8400: ARROW-10236: [Rust][DataFusion] Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
alamb closed pull request #8400:
URL: https://github.com/apache/arrow/pull/8400


   


----------------------------------------------------------------
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.

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



[GitHub] [arrow] alamb commented on a change in pull request #8400: ARROW-10236: Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #8400:
URL: https://github.com/apache/arrow/pull/8400#discussion_r501795062



##########
File path: rust/datafusion/src/logical_plan/mod.rs
##########
@@ -323,12 +324,13 @@ impl Expr {
     ///
     /// # Errors
     ///
-    /// This function errors when it is impossible to cast the expression to the target [arrow::datatypes::DataType].
+    /// This function errors when it is impossible to cast the
+    /// expression to the target [arrow::datatypes::DataType].
     pub fn cast_to(&self, cast_to_type: &DataType, schema: &Schema) -> Result<Expr> {
         let this_type = self.get_type(schema)?;
         if this_type == *cast_to_type {
             Ok(self.clone())
-        } else if can_coerce_from(cast_to_type, &this_type) {
+        } else if can_cast_types(&this_type, cast_to_type) {

Review comment:
       the idea is to use a common `can_cast_types` function to detect valid casts at plan time, and make `can_cast_types` consistent with the arrow cast kernel




----------------------------------------------------------------
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.

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



[GitHub] [arrow] nevi-me commented on a change in pull request #8400: ARROW-10236: [Rust][DataFusion] Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
nevi-me commented on a change in pull request #8400:
URL: https://github.com/apache/arrow/pull/8400#discussion_r501850814



##########
File path: rust/datafusion/src/physical_plan/type_casting.rs
##########
@@ -0,0 +1,218 @@
+// 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.
+
+//! This module provides a way of checking what type casts are
+//! supported at planning time for DataFusion. Since DataFusion uses
+//! the Arrow `cast` compute kernel, the supported casts are the same
+//! as the Arrow casts.
+//!
+//! The rules in this module are designed to be redundant with the
+//! rules in the Arrow `cast` kernel. The redundancy is needed so that
+//! DataFusion can generate an error at plan time rather than during
+//! execution (which could happen many hours after execution starts,
+//! when the query finally reaches that point)
+//!
+
+use arrow::datatypes::*;
+
+/// Return true if a value of type `from_type` can be cast into a
+/// value of `to_type`. Note that such as cast may be lossy. For
+/// lossless type conversions, see the `type_coercion` module
+///
+/// See the module level documentation for more detail on casting
+pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
+    use self::DataType::*;
+    if from_type == to_type {
+        return true;
+    }
+
+    // Note this is meant to mirror the structure in arrow/src/compute/kernels/cast.rs
+    match (from_type, to_type) {

Review comment:
       I was also going to suggest if we move `can_cast_types` to Arrow

##########
File path: rust/datafusion/src/physical_plan/type_casting.rs
##########
@@ -0,0 +1,218 @@
+// 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.
+
+//! This module provides a way of checking what type casts are
+//! supported at planning time for DataFusion. Since DataFusion uses
+//! the Arrow `cast` compute kernel, the supported casts are the same
+//! as the Arrow casts.
+//!
+//! The rules in this module are designed to be redundant with the
+//! rules in the Arrow `cast` kernel. The redundancy is needed so that
+//! DataFusion can generate an error at plan time rather than during
+//! execution (which could happen many hours after execution starts,
+//! when the query finally reaches that point)
+//!
+
+use arrow::datatypes::*;
+
+/// Return true if a value of type `from_type` can be cast into a
+/// value of `to_type`. Note that such as cast may be lossy. For
+/// lossless type conversions, see the `type_coercion` module
+///
+/// See the module level documentation for more detail on casting
+pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
+    use self::DataType::*;
+    if from_type == to_type {
+        return true;
+    }
+
+    // Note this is meant to mirror the structure in arrow/src/compute/kernels/cast.rs
+    match (from_type, to_type) {
+        (Struct(_), _) => false,
+        (_, Struct(_)) => false,
+        (List(list_from), List(list_to)) => can_cast_types(list_from, list_to),
+        (List(_), _) => false,
+        (_, List(list_to)) => can_cast_types(from_type, list_to),
+        (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
+            can_cast_types(from_value_type, to_value_type)
+        }
+        (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
+        (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
+
+        (_, Boolean) => is_numeric_type(from_type),
+        (Boolean, _) => is_numeric_type(from_type) || from_type == &Utf8,
+        (Utf8, _) => is_numeric_type(to_type),
+        (_, Utf8) => is_numeric_type(from_type) || from_type == &Binary,
+
+        // start numeric casts
+        (UInt8, UInt16) => true,
+        (UInt8, UInt32) => true,
+        (UInt8, UInt64) => true,
+        (UInt8, Int8) => true,
+        (UInt8, Int16) => true,
+        (UInt8, Int32) => true,
+        (UInt8, Int64) => true,
+        (UInt8, Float32) => true,
+        (UInt8, Float64) => true,
+
+        (UInt16, UInt8) => true,
+        (UInt16, UInt32) => true,
+        (UInt16, UInt64) => true,
+        (UInt16, Int8) => true,
+        (UInt16, Int16) => true,
+        (UInt16, Int32) => true,
+        (UInt16, Int64) => true,
+        (UInt16, Float32) => true,
+        (UInt16, Float64) => true,
+
+        (UInt32, UInt8) => true,
+        (UInt32, UInt16) => true,
+        (UInt32, UInt64) => true,
+        (UInt32, Int8) => true,
+        (UInt32, Int16) => true,
+        (UInt32, Int32) => true,
+        (UInt32, Int64) => true,
+        (UInt32, Float32) => true,
+        (UInt32, Float64) => true,
+
+        (UInt64, UInt8) => true,
+        (UInt64, UInt16) => true,
+        (UInt64, UInt32) => true,
+        (UInt64, Int8) => true,
+        (UInt64, Int16) => true,
+        (UInt64, Int32) => true,
+        (UInt64, Int64) => true,
+        (UInt64, Float32) => true,
+        (UInt64, Float64) => true,
+
+        (Int8, UInt8) => true,
+        (Int8, UInt16) => true,
+        (Int8, UInt32) => true,
+        (Int8, UInt64) => true,
+        (Int8, Int16) => true,
+        (Int8, Int32) => true,
+        (Int8, Int64) => true,
+        (Int8, Float32) => true,
+        (Int8, Float64) => true,
+
+        (Int16, UInt8) => true,
+        (Int16, UInt16) => true,
+        (Int16, UInt32) => true,
+        (Int16, UInt64) => true,
+        (Int16, Int8) => true,
+        (Int16, Int32) => true,
+        (Int16, Int64) => true,
+        (Int16, Float32) => true,
+        (Int16, Float64) => true,
+
+        (Int32, UInt8) => true,
+        (Int32, UInt16) => true,
+        (Int32, UInt32) => true,
+        (Int32, UInt64) => true,
+        (Int32, Int8) => true,
+        (Int32, Int16) => true,
+        (Int32, Int64) => true,
+        (Int32, Float32) => true,
+        (Int32, Float64) => true,
+
+        (Int64, UInt8) => true,
+        (Int64, UInt16) => true,
+        (Int64, UInt32) => true,
+        (Int64, UInt64) => true,
+        (Int64, Int8) => true,
+        (Int64, Int16) => true,
+        (Int64, Int32) => true,
+        (Int64, Float32) => true,
+        (Int64, Float64) => true,
+
+        (Float32, UInt8) => true,
+        (Float32, UInt16) => true,
+        (Float32, UInt32) => true,
+        (Float32, UInt64) => true,
+        (Float32, Int8) => true,
+        (Float32, Int16) => true,
+        (Float32, Int32) => true,
+        (Float32, Int64) => true,
+        (Float32, Float64) => true,
+
+        (Float64, UInt8) => true,
+        (Float64, UInt16) => true,
+        (Float64, UInt32) => true,
+        (Float64, UInt64) => true,
+        (Float64, Int8) => true,
+        (Float64, Int16) => true,
+        (Float64, Int32) => true,
+        (Float64, Int64) => true,
+        (Float64, Float32) => true,
+        // end numeric casts
+
+        // temporal casts
+        (Int32, Date32(_)) => true,
+        (Int32, Time32(_)) => true,
+        (Date32(_), Int32) => true,
+        (Time32(_), Int32) => true,
+        (Int64, Date64(_)) => true,
+        (Int64, Time64(_)) => true,
+        (Date64(_), Int64) => true,
+        (Time64(_), Int64) => true,
+        (Date32(DateUnit::Day), Date64(DateUnit::Millisecond)) => true,
+        (Date64(DateUnit::Millisecond), Date32(DateUnit::Day)) => true,
+        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => true,
+        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => true,
+        (Time32(_), Time64(_)) => true,
+        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => true,
+        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => true,
+        (Time64(_), Time32(to_unit)) => match to_unit {
+            TimeUnit::Second => true,
+            TimeUnit::Millisecond => true,
+            _ => false,
+        },
+        (Timestamp(_, _), Int64) => true,
+        (Int64, Timestamp(_, _)) => true,
+        (Timestamp(_, _), Timestamp(_, _)) => true,
+        (Timestamp(_, _), Date32(_)) => true,
+        (Timestamp(_, _), Date64(_)) => true,
+        // date64 to timestamp might not make sense,
+
+        // end temporal casts
+        (_, _) => false,
+    }
+}
+
+fn is_numeric_type(t: &DataType) -> bool {

Review comment:
       This is also something that can live in perhaps `arrow::util` or `arrow::datatype` (and be implemented as a trait member of `DataType`).
   I needed to check if a datatype was listy or stringy, and the boilerplate was tedious.




----------------------------------------------------------------
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.

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



[GitHub] [arrow] github-actions[bot] commented on pull request #8400: ARROW-10236: Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #8400:
URL: https://github.com/apache/arrow/pull/8400#issuecomment-705633631


   https://issues.apache.org/jira/browse/ARROW-10236


----------------------------------------------------------------
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.

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



[GitHub] [arrow] alamb commented on a change in pull request #8400: ARROW-10236: [Rust][DataFusion] Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #8400:
URL: https://github.com/apache/arrow/pull/8400#discussion_r504278902



##########
File path: rust/datafusion/src/physical_plan/type_casting.rs
##########
@@ -0,0 +1,218 @@
+// 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.
+
+//! This module provides a way of checking what type casts are
+//! supported at planning time for DataFusion. Since DataFusion uses
+//! the Arrow `cast` compute kernel, the supported casts are the same
+//! as the Arrow casts.
+//!
+//! The rules in this module are designed to be redundant with the
+//! rules in the Arrow `cast` kernel. The redundancy is needed so that
+//! DataFusion can generate an error at plan time rather than during
+//! execution (which could happen many hours after execution starts,
+//! when the query finally reaches that point)
+//!
+
+use arrow::datatypes::*;
+
+/// Return true if a value of type `from_type` can be cast into a
+/// value of `to_type`. Note that such as cast may be lossy. For
+/// lossless type conversions, see the `type_coercion` module
+///
+/// See the module level documentation for more detail on casting
+pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
+    use self::DataType::*;
+    if from_type == to_type {
+        return true;
+    }
+
+    // Note this is meant to mirror the structure in arrow/src/compute/kernels/cast.rs
+    match (from_type, to_type) {

Review comment:
       @jorgecarleitao  and @nevi-me  -- here is the the pattern I came up with:
   
   ```rust
   pub fn cast(array: &ArrayRef, to_type: &DataType) -> Result<ArrayRef> {
       let from_type = array.data_type();
       let func = get_cast_function(from_type, to_type)?;
       func(array)
   }
   
   /// Returns true if the cast from `array.array` type is possible, false otherwise
   pub fn can_cast(array: &ArrayRef, to_type: &DataType, ) -> bool {
       let from_type = array.data_type();
       get_cast_function(from_type, to_type).is_ok()
   }
   
   
   type CastFunction = Fn(&ArrayRef) -> Result<ArrayRef>;
   
   /// Returns a function that can cast `array` to `to_type`.
   ///
   /// All type checking for supported types should be done in
   /// get_cast_func itself. Thus, if Ok(func) is returned, func(array)
   /// should be able to succeed for arrays. In other words, Err(_)
   /// should be returned by `get_cast_function`, NOT `func(array)` if
   /// the types are incompatible.
   ///
   /// can_cast relies on this functionality
   fn get_cast_function(from_type: &DataType, to_type: &DataType) -> Result<CastFunction> {
       use DataType::*;
   
       // clone array if types are the same
       if from_type == to_type {
           return Ok(|array| { Ok(array.clone()) })
       }
   
       match (from_type, to_type) {
           (Struct(_), _) => Err(ArrowError::ComputeError(
               "Cannot cast from struct to other types".to_string(),
           )),
           (_, Struct(_)) => Err(ArrowError::ComputeError(
               "Cannot cast to struct from other types".to_string(),
           )),
           (List(_), List(ref to)) => Ok(|array| {
               let data = array.data_ref();
               let underlying_array = make_array(data.child_data()[0].clone());
               let cast_array = cast(&underlying_array, &to)?;
               let array_data = ArrayData::new(
                   *to.clone(),
                   array.len(),
                   Some(cast_array.null_count()),
                   cast_array
                       .data()
                       .null_bitmap()
                       .clone()
                       .map(|bitmap| bitmap.bits),
                   array.offset(),
                   // reuse offset buffer
                   data.buffers().to_vec(),
                   vec![cast_array.data()],
               );
               let list = ListArray::from(Arc::new(array_data));
               Ok(Arc::new(list) as ArrayRef)
           }),
           (List(_), _) => Err(ArrowError::ComputeError(
               "Cannot cast list to non-list data types".to_string(),
           )),
           (_, List(ref to)) => {
               // cast primitive to list's primitive
               let cast_func = get_cast_function(from_type, &to)?;
               Ok(move |array| {
                   let cast_array = cast_func(array, &to)?;
                   // create offsets, where if array.len() = 2, we have [0,1,2]
                   let offsets: Vec<i32> = (0..=array.len() as i32).collect();
                   let value_offsets = Buffer::from(offsets[..].to_byte_slice());
                   let list_data = ArrayData::new(
                       *to.clone(),
                       array.len(),
                       Some(cast_array.null_count()),
                       cast_array
                           .data()
                           .null_bitmap()
                           .clone()
                           .map(|bitmap| bitmap.bits),
                       0,
                       vec![value_offsets],
                       vec![cast_array.data()],
                   );
                   let list_array = Arc::new(ListArray::from(Arc::new(list_data))) as ArrayRef;
   
                   Ok(list_array)
               })
           }
   ...
   ```
   
   While I think it would work, I am not sure I will have enough time. 
   
   Instead, I will get a PR up that moves the `can_cast_types` (with duplicate `match`) to arrow and then try a separate refactoring PR to unify the two match arms




----------------------------------------------------------------
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.

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



[GitHub] [arrow] alamb commented on pull request #8400: ARROW-10236: [Rust][DataFusion] Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
alamb commented on pull request #8400:
URL: https://github.com/apache/arrow/pull/8400#issuecomment-707238239


   Thanks for the nudge @jorgecarleitao  -- my plan is to move the code into arrow itself (and I was going to try some ideas to avoid the duplicate `case` statement). I haven't had as much time to work on this as I had hoped recently.


----------------------------------------------------------------
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.

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



[GitHub] [arrow] nevi-me commented on a change in pull request #8400: ARROW-10236: [Rust][DataFusion] Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
nevi-me commented on a change in pull request #8400:
URL: https://github.com/apache/arrow/pull/8400#discussion_r501851945



##########
File path: rust/datafusion/src/physical_plan/type_casting.rs
##########
@@ -0,0 +1,218 @@
+// 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.
+
+//! This module provides a way of checking what type casts are
+//! supported at planning time for DataFusion. Since DataFusion uses
+//! the Arrow `cast` compute kernel, the supported casts are the same
+//! as the Arrow casts.
+//!
+//! The rules in this module are designed to be redundant with the
+//! rules in the Arrow `cast` kernel. The redundancy is needed so that
+//! DataFusion can generate an error at plan time rather than during
+//! execution (which could happen many hours after execution starts,
+//! when the query finally reaches that point)
+//!
+
+use arrow::datatypes::*;
+
+/// Return true if a value of type `from_type` can be cast into a
+/// value of `to_type`. Note that such as cast may be lossy. For
+/// lossless type conversions, see the `type_coercion` module
+///
+/// See the module level documentation for more detail on casting
+pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
+    use self::DataType::*;
+    if from_type == to_type {
+        return true;
+    }
+
+    // Note this is meant to mirror the structure in arrow/src/compute/kernels/cast.rs
+    match (from_type, to_type) {
+        (Struct(_), _) => false,
+        (_, Struct(_)) => false,
+        (List(list_from), List(list_to)) => can_cast_types(list_from, list_to),
+        (List(_), _) => false,
+        (_, List(list_to)) => can_cast_types(from_type, list_to),
+        (Dictionary(_, from_value_type), Dictionary(_, to_value_type)) => {
+            can_cast_types(from_value_type, to_value_type)
+        }
+        (Dictionary(_, value_type), _) => can_cast_types(value_type, to_type),
+        (_, Dictionary(_, value_type)) => can_cast_types(from_type, value_type),
+
+        (_, Boolean) => is_numeric_type(from_type),
+        (Boolean, _) => is_numeric_type(from_type) || from_type == &Utf8,
+        (Utf8, _) => is_numeric_type(to_type),
+        (_, Utf8) => is_numeric_type(from_type) || from_type == &Binary,
+
+        // start numeric casts
+        (UInt8, UInt16) => true,
+        (UInt8, UInt32) => true,
+        (UInt8, UInt64) => true,
+        (UInt8, Int8) => true,
+        (UInt8, Int16) => true,
+        (UInt8, Int32) => true,
+        (UInt8, Int64) => true,
+        (UInt8, Float32) => true,
+        (UInt8, Float64) => true,
+
+        (UInt16, UInt8) => true,
+        (UInt16, UInt32) => true,
+        (UInt16, UInt64) => true,
+        (UInt16, Int8) => true,
+        (UInt16, Int16) => true,
+        (UInt16, Int32) => true,
+        (UInt16, Int64) => true,
+        (UInt16, Float32) => true,
+        (UInt16, Float64) => true,
+
+        (UInt32, UInt8) => true,
+        (UInt32, UInt16) => true,
+        (UInt32, UInt64) => true,
+        (UInt32, Int8) => true,
+        (UInt32, Int16) => true,
+        (UInt32, Int32) => true,
+        (UInt32, Int64) => true,
+        (UInt32, Float32) => true,
+        (UInt32, Float64) => true,
+
+        (UInt64, UInt8) => true,
+        (UInt64, UInt16) => true,
+        (UInt64, UInt32) => true,
+        (UInt64, Int8) => true,
+        (UInt64, Int16) => true,
+        (UInt64, Int32) => true,
+        (UInt64, Int64) => true,
+        (UInt64, Float32) => true,
+        (UInt64, Float64) => true,
+
+        (Int8, UInt8) => true,
+        (Int8, UInt16) => true,
+        (Int8, UInt32) => true,
+        (Int8, UInt64) => true,
+        (Int8, Int16) => true,
+        (Int8, Int32) => true,
+        (Int8, Int64) => true,
+        (Int8, Float32) => true,
+        (Int8, Float64) => true,
+
+        (Int16, UInt8) => true,
+        (Int16, UInt16) => true,
+        (Int16, UInt32) => true,
+        (Int16, UInt64) => true,
+        (Int16, Int8) => true,
+        (Int16, Int32) => true,
+        (Int16, Int64) => true,
+        (Int16, Float32) => true,
+        (Int16, Float64) => true,
+
+        (Int32, UInt8) => true,
+        (Int32, UInt16) => true,
+        (Int32, UInt32) => true,
+        (Int32, UInt64) => true,
+        (Int32, Int8) => true,
+        (Int32, Int16) => true,
+        (Int32, Int64) => true,
+        (Int32, Float32) => true,
+        (Int32, Float64) => true,
+
+        (Int64, UInt8) => true,
+        (Int64, UInt16) => true,
+        (Int64, UInt32) => true,
+        (Int64, UInt64) => true,
+        (Int64, Int8) => true,
+        (Int64, Int16) => true,
+        (Int64, Int32) => true,
+        (Int64, Float32) => true,
+        (Int64, Float64) => true,
+
+        (Float32, UInt8) => true,
+        (Float32, UInt16) => true,
+        (Float32, UInt32) => true,
+        (Float32, UInt64) => true,
+        (Float32, Int8) => true,
+        (Float32, Int16) => true,
+        (Float32, Int32) => true,
+        (Float32, Int64) => true,
+        (Float32, Float64) => true,
+
+        (Float64, UInt8) => true,
+        (Float64, UInt16) => true,
+        (Float64, UInt32) => true,
+        (Float64, UInt64) => true,
+        (Float64, Int8) => true,
+        (Float64, Int16) => true,
+        (Float64, Int32) => true,
+        (Float64, Int64) => true,
+        (Float64, Float32) => true,
+        // end numeric casts
+
+        // temporal casts
+        (Int32, Date32(_)) => true,
+        (Int32, Time32(_)) => true,
+        (Date32(_), Int32) => true,
+        (Time32(_), Int32) => true,
+        (Int64, Date64(_)) => true,
+        (Int64, Time64(_)) => true,
+        (Date64(_), Int64) => true,
+        (Time64(_), Int64) => true,
+        (Date32(DateUnit::Day), Date64(DateUnit::Millisecond)) => true,
+        (Date64(DateUnit::Millisecond), Date32(DateUnit::Day)) => true,
+        (Time32(TimeUnit::Second), Time32(TimeUnit::Millisecond)) => true,
+        (Time32(TimeUnit::Millisecond), Time32(TimeUnit::Second)) => true,
+        (Time32(_), Time64(_)) => true,
+        (Time64(TimeUnit::Microsecond), Time64(TimeUnit::Nanosecond)) => true,
+        (Time64(TimeUnit::Nanosecond), Time64(TimeUnit::Microsecond)) => true,
+        (Time64(_), Time32(to_unit)) => match to_unit {
+            TimeUnit::Second => true,
+            TimeUnit::Millisecond => true,
+            _ => false,
+        },
+        (Timestamp(_, _), Int64) => true,
+        (Int64, Timestamp(_, _)) => true,
+        (Timestamp(_, _), Timestamp(_, _)) => true,
+        (Timestamp(_, _), Date32(_)) => true,
+        (Timestamp(_, _), Date64(_)) => true,
+        // date64 to timestamp might not make sense,
+
+        // end temporal casts
+        (_, _) => false,
+    }
+}
+
+fn is_numeric_type(t: &DataType) -> bool {

Review comment:
       This is also something that can live in perhaps `arrow::util` or `arrow::datatype` (and be implemented as a trait member of `DataType`).
   I needed to check if a datatype was listy or stringy, and the boilerplate was tedious.




----------------------------------------------------------------
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.

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



[GitHub] [arrow] jorgecarleitao commented on a change in pull request #8400: ARROW-10236: [Rust][DataFusion] Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on a change in pull request #8400:
URL: https://github.com/apache/arrow/pull/8400#discussion_r501802894



##########
File path: rust/datafusion/src/physical_plan/type_casting.rs
##########
@@ -0,0 +1,218 @@
+// 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.
+
+//! This module provides a way of checking what type casts are
+//! supported at planning time for DataFusion. Since DataFusion uses
+//! the Arrow `cast` compute kernel, the supported casts are the same
+//! as the Arrow casts.
+//!
+//! The rules in this module are designed to be redundant with the
+//! rules in the Arrow `cast` kernel. The redundancy is needed so that
+//! DataFusion can generate an error at plan time rather than during
+//! execution (which could happen many hours after execution starts,
+//! when the query finally reaches that point)
+//!
+
+use arrow::datatypes::*;
+
+/// Return true if a value of type `from_type` can be cast into a
+/// value of `to_type`. Note that such as cast may be lossy. For
+/// lossless type conversions, see the `type_coercion` module
+///
+/// See the module level documentation for more detail on casting
+pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
+    use self::DataType::*;
+    if from_type == to_type {
+        return true;
+    }
+
+    // Note this is meant to mirror the structure in arrow/src/compute/kernels/cast.rs
+    match (from_type, to_type) {

Review comment:
       This is something that I have been thinking about: in DataFusion, because we use dynamically typed arrays, we often have to annotate which types are supported by each arrow kernel / operation.
   
   Thus, we need to duplicate these `match`es to enumerate the valid types accepted by the kernels, so that we error during planning.
   
   I wonder if these functions shouldn't be closer to the implementation (i.e. in the `arrow::compute::kernels`), or, alternatively, if we could find an idiom that would allow us to write these `match`es one time (again, in compute::kernel).
   
   It seems to me that the pattern is:
   
   for compute
   ```
   match array.data_type() {
      ... => compute(array)
   }
   ```
   for datatypes:
   
   ```
   match datatype {
       ... => return Some(return_datatype)
   }
   ```
   
   one idea would be to use
   
   ```
   match datatype {
       ... => return Some((closure_that_computes, return_datatype))
   }
   ```
   
   so that both use-cases could be written in a single match (and reduce the risk of mis-typing / change in one place without a change in another place).
   
   This comment is not specific to this PR, which I need to go through: I was just curious about your thoughts on this, since you have been touching in a couple of these recently.




----------------------------------------------------------------
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.

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



[GitHub] [arrow] alamb commented on pull request #8400: ARROW-10236: [Rust][DataFusion] Unify type casting logic in DataFusion

Posted by GitBox <gi...@apache.org>.
alamb commented on pull request #8400:
URL: https://github.com/apache/arrow/pull/8400#issuecomment-707637676


   To keep the PR list clean, I will close this one and open a new PR with the alternate proposed implementation


----------------------------------------------------------------
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.

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