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:
[email protected]