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/10 17:21:59 UTC

[GitHub] [arrow-datafusion] ozankabak commented on a diff in pull request #4168: Improve Error Handling and Readibility for downcasting `Decimal128Array`

ozankabak commented on code in PR #4168:
URL: https://github.com/apache/arrow-datafusion/pull/4168#discussion_r1019409860


##########
datafusion/common/src/scalar.rs:
##########
@@ -1881,13 +1881,22 @@ impl ScalarValue {
         index: usize,
         precision: u8,
         scale: u8,
-    ) -> ScalarValue {
-        let array = array.as_any().downcast_ref::<Decimal128Array>().unwrap();
-        if array.is_null(index) {
-            ScalarValue::Decimal128(None, precision, scale)
-        } else {
-            ScalarValue::Decimal128(Some(array.value(index)), precision, scale)
-        }
+    ) -> Result<ScalarValue> {
+        let array = match as_decimal128_array(array) {

Review Comment:
   Can you not do this with:
   ```rust
   let array = as_decimal128_array(array)?;
   if array.is_null(index) {
       Ok(ScalarValue::Decimal128(None, precision, scale))
   } else {
       let value = array.value(index);
       Ok(ScalarValue::Decimal128(Some(value), precision, scale))
   }
   ```



##########
datafusion/common/src/scalar.rs:
##########
@@ -2073,14 +2082,14 @@ impl ScalarValue {
         value: &Option<i128>,
         precision: u8,
         scale: u8,
-    ) -> bool {
-        let array = array.as_any().downcast_ref::<Decimal128Array>().unwrap();
+    ) -> Result<bool> {
+        let array = as_decimal128_array(array)?;
         if array.precision() != precision || array.scale() != scale {
-            return false;
+            return Ok(false);
         }
         match value {

Review Comment:
   Using `match` expressions with binary types like `Result` or `Option` is typically less idiomatic than using the `if let` expression. With this in mind, I suggest:
   ```rust
   let is_null = array.is_null(index);
   if let Some(v) = value {
     Ok(!is_null && array.value(index) == *v)
   } else {
     Ok(is_null)
   }
   ```



-- 
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