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 2022/11/18 15:20:38 UTC

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #4261: improve error messages while downcasting `UInt32Array`, `UInt64Array` and `BooleanArray`

alamb commented on code in PR #4261:
URL: https://github.com/apache/arrow-datafusion/pull/4261#discussion_r1026563763


##########
datafusion/common/src/cast.rs:
##########
@@ -116,3 +116,36 @@ pub fn as_string_array(array: &dyn Array) -> Result<&StringArray, DataFusionErro
         ))
     })
 }
+
+// Downcast ArrayRef to UInt32Array
+pub fn as_uint32_array(array: &dyn Array) -> Result<&UInt32Array, DataFusionError> {
+    array.as_any().downcast_ref::<UInt32Array>().ok_or_else(|| {
+        DataFusionError::Internal(format!(
+            "Expected a UInt32Array, got: {}",
+            array.data_type()
+        ))
+    })
+}
+
+// Downcast ArrayRef to UInt64Array
+pub fn as_uint64_array(array: &dyn Array) -> Result<&UInt64Array, DataFusionError> {
+    array.as_any().downcast_ref::<UInt64Array>().ok_or_else(|| {
+        DataFusionError::Internal(format!(
+            "Expected a UInt64Array, got: {}",
+            array.data_type()
+        ))
+    })
+}
+
+// Downcast ArrayRef to BooleanArray
+pub fn as_boolean_array(array: &dyn Array) -> Result<&BooleanArray, DataFusionError> {
+    array
+        .as_any()
+        .downcast_ref::<BooleanArray>()
+        .ok_or_else(|| {
+            DataFusionError::Internal(format!(
+                "Expected a BooleanArray, got: {}",
+                array.data_type()
+            ))
+        })
+}

Review Comment:
   Well, that is embarrassing 🤦 
   
   Interestingly the `downcast_value` doesn't appear to be used in that many places (around 5 at this time0:
   https://github.com/search?q=repo%3Aapache%2Farrow-datafusion+downcast_value&type=code
   
    I would prefer not to roll back the PRs as they have already simplified the code non trivially.
   
   What is important in my opinion is to use a standard pattern to do this downcasting. I don't have a huge preference between `downcast_value` and `as_boolean_array` , though the `as_boolean_array` might be more discoverable in an IDE that autocompletes
   
   If we are worried about code duplication, perhaps we can use `downcast_value` to implement the `as_boolean_array`, type methods



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org