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:15:39 UTC

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

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