This is an automated email from the ASF dual-hosted git repository.

agrove pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-comet.git


The following commit(s) were added to refs/heads/main by this push:
     new b3c198450 chore: Remove some unwraps in hashing code (#1600)
b3c198450 is described below

commit b3c198450d0885a5b7c7d281df65480861ad5a5a
Author: Andy Grove <[email protected]>
AuthorDate: Thu Apr 3 11:00:53 2025 -0600

    chore: Remove some unwraps in hashing code (#1600)
    
    * remove some unwraps in hashing code
    
    * address feedback
---
 native/spark-expr/src/hash_funcs/utils.rs | 81 +++++++++++++++++++++++++++----
 1 file changed, 72 insertions(+), 9 deletions(-)

diff --git a/native/spark-expr/src/hash_funcs/utils.rs 
b/native/spark-expr/src/hash_funcs/utils.rs
index 3569e4abc..a1cee8465 100644
--- a/native/spark-expr/src/hash_funcs/utils.rs
+++ b/native/spark-expr/src/hash_funcs/utils.rs
@@ -20,7 +20,16 @@
 #[macro_export]
 macro_rules! hash_array {
     ($array_type: ident, $column: ident, $hashes: ident, $hash_method: ident) 
=> {
-        let array = $column.as_any().downcast_ref::<$array_type>().unwrap();
+        let array = $column
+            .as_any()
+            .downcast_ref::<$array_type>()
+            .unwrap_or_else(|| {
+                panic!(
+                    "Failed to downcast column to {}. Actual data type: {:?}.",
+                    stringify!($array_type),
+                    $column.data_type()
+                )
+            });
         if array.null_count() == 0 {
             for (i, hash) in $hashes.iter_mut().enumerate() {
                 *hash = $hash_method(&array.value(i), *hash);
@@ -38,7 +47,16 @@ macro_rules! hash_array {
 #[macro_export]
 macro_rules! hash_array_boolean {
     ($array_type: ident, $column: ident, $hash_input_type: ident, $hashes: 
ident, $hash_method: ident) => {
-        let array = $column.as_any().downcast_ref::<$array_type>().unwrap();
+        let array = $column
+            .as_any()
+            .downcast_ref::<$array_type>()
+            .unwrap_or_else(|| {
+                panic!(
+                    "Failed to downcast column to {}. Actual data type: {:?}.",
+                    stringify!($array_type),
+                    $column.data_type()
+                )
+            });
         if array.null_count() == 0 {
             for (i, hash) in $hashes.iter_mut().enumerate() {
                 *hash = 
$hash_method($hash_input_type::from(array.value(i)).to_le_bytes(), *hash);
@@ -57,7 +75,16 @@ macro_rules! hash_array_boolean {
 #[macro_export]
 macro_rules! hash_array_primitive {
     ($array_type: ident, $column: ident, $ty: ident, $hashes: ident, 
$hash_method: ident) => {
-        let array = $column.as_any().downcast_ref::<$array_type>().unwrap();
+        let array = $column
+            .as_any()
+            .downcast_ref::<$array_type>()
+            .unwrap_or_else(|| {
+                panic!(
+                    "Failed to downcast column to {}. Actual data type: {:?}.",
+                    stringify!($array_type),
+                    $column.data_type()
+                )
+            });
         let values = array.values();
 
         if array.null_count() == 0 {
@@ -77,7 +104,16 @@ macro_rules! hash_array_primitive {
 #[macro_export]
 macro_rules! hash_array_primitive_float {
     ($array_type: ident, $column: ident, $ty: ident, $ty2: ident, $hashes: 
ident, $hash_method: ident) => {
-        let array = $column.as_any().downcast_ref::<$array_type>().unwrap();
+        let array = $column
+            .as_any()
+            .downcast_ref::<$array_type>()
+            .unwrap_or_else(|| {
+                panic!(
+                    "Failed to downcast column to {}. Actual data type: {:?}.",
+                    stringify!($array_type),
+                    $column.data_type()
+                )
+            });
         let values = array.values();
 
         if array.null_count() == 0 {
@@ -107,17 +143,35 @@ macro_rules! hash_array_primitive_float {
 #[macro_export]
 macro_rules! hash_array_small_decimal {
     ($array_type:ident, $column: ident, $hashes: ident, $hash_method: ident) 
=> {
-        let array = $column.as_any().downcast_ref::<$array_type>().unwrap();
+        let array = $column
+            .as_any()
+            .downcast_ref::<$array_type>()
+            .unwrap_or_else(|| {
+                panic!(
+                    "Failed to downcast column to {}. Actual data type: {:?}.",
+                    stringify!($array_type),
+                    $column.data_type()
+                )
+            });
 
         if array.null_count() == 0 {
             for (i, hash) in $hashes.iter_mut().enumerate() {
-                *hash = 
$hash_method(i64::try_from(array.value(i)).unwrap().to_le_bytes(), *hash);
+                *hash = $hash_method(
+                    i64::try_from(array.value(i))
+                        .map(|v| v.to_le_bytes())
+                        .map_err(|e| 
DataFusionError::Execution(e.to_string()))?,
+                    *hash,
+                );
             }
         } else {
             for (i, hash) in $hashes.iter_mut().enumerate() {
                 if !array.is_null(i) {
-                    *hash =
-                        
$hash_method(i64::try_from(array.value(i)).unwrap().to_le_bytes(), *hash);
+                    *hash = $hash_method(
+                        i64::try_from(array.value(i))
+                            .map(|v| v.to_le_bytes())
+                            .map_err(|e| 
DataFusionError::Execution(e.to_string()))?,
+                        *hash,
+                    );
                 }
             }
         }
@@ -127,7 +181,16 @@ macro_rules! hash_array_small_decimal {
 #[macro_export]
 macro_rules! hash_array_decimal {
     ($array_type:ident, $column: ident, $hashes: ident, $hash_method: ident) 
=> {
-        let array = $column.as_any().downcast_ref::<$array_type>().unwrap();
+        let array = $column
+            .as_any()
+            .downcast_ref::<$array_type>()
+            .unwrap_or_else(|| {
+                panic!(
+                    "Failed to downcast column to {}. Actual data type: {:?}.",
+                    stringify!($array_type),
+                    $column.data_type()
+                )
+            });
 
         if array.null_count() == 0 {
             for (i, hash) in $hashes.iter_mut().enumerate() {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to