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