alamb commented on code in PR #4004: URL: https://github.com/apache/arrow-datafusion/pull/4004#discussion_r1008447752
########## datafusion/common/src/cast.rs: ########## @@ -0,0 +1,31 @@ +// 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 the casting functions Review Comment: ```suggestion //! This module provides DataFusion specific casting functions //! that provide error handling. They are intended to "never fail" //! but provide an error message rather than a panic, as the corresponding //! kernels in arrow-rs such as `as_boolean_array` do. ``` ########## datafusion/physical-expr/src/datetime_expressions.rs: ########## @@ -377,10 +378,10 @@ pub fn date_bin(args: &[ColumnarValue]) -> Result<ColumnarValue> { macro_rules! extract_date_part { ($ARRAY: expr, $FN:expr) => { match $ARRAY.data_type() { - DataType::Date32 => { - let array = $ARRAY.as_any().downcast_ref::<Date32Array>().unwrap(); - Ok($FN(array)?) - } + DataType::Date32 => match as_date32_array($ARRAY) { + Ok(array) => Ok($FN(array)?), + Err(e) => Err(e), + }, Review Comment: You might be able to make this even more concise like ```suggestion DataType::Date32 => $FN(match as_date32_array($ARRAY)?), ``` ########## datafusion/physical-expr/src/expressions/datetime.rs: ########## @@ -153,7 +154,10 @@ pub fn evaluate_array( ) -> Result<ColumnarValue> { let ret = match array.data_type() { DataType::Date32 => { - let array = array.as_any().downcast_ref::<Date32Array>().unwrap(); + let array = match as_date32_array(&array) { + Ok(array) => array, + Err(e) => return Err(e), + }; Review Comment: The same comment applies to the rest of the PR ########## datafusion/common/src/cast.rs: ########## @@ -0,0 +1,31 @@ +// 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 the casting functions + +use crate::DataFusionError; +use arrow::array::{Array, Date32Array}; + +// Downcast ArrayRef to Date32Array +pub fn as_date32_array(array: &dyn Array) -> Result<&Date32Array, DataFusionError> { + array.as_any().downcast_ref::<Date32Array>().ok_or_else(|| { + DataFusionError::Execution(format!( Review Comment: I recommend we make these internal errors so it is clear they are "not supposed to be" generated ```suggestion DataFusionError::Internal(format!( ``` ########## datafusion/physical-expr/src/expressions/datetime.rs: ########## @@ -153,7 +154,10 @@ pub fn evaluate_array( ) -> Result<ColumnarValue> { let ret = match array.data_type() { DataType::Date32 => { - let array = array.as_any().downcast_ref::<Date32Array>().unwrap(); + let array = match as_date32_array(&array) { + Ok(array) => array, + Err(e) => return Err(e), + }; Review Comment: I think this can be like ```suggestion let array = as_date32_array(&array)?; ``` -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
