You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "irenjj (via GitHub)" <gi...@apache.org> on 2025/02/02 12:14:32 UTC

[PR] fix: first none in `ListArray` panics in `cast_with_options` [arrow-rs]

irenjj opened a new pull request, #7065:
URL: https://github.com/apache/arrow-rs/pull/7065

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #7043
   
   # Rationale for this change
    
   <!--
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are there any user-facing changes?
   
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please call them out.
   -->
   


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


Re: [PR] fix: first none in `ListArray` panics in `cast_with_options` [arrow-rs]

Posted by "irenjj (via GitHub)" <gi...@apache.org>.
irenjj commented on PR #7065:
URL: https://github.com/apache/arrow-rs/pull/7065#issuecomment-2639738016

   Thanks @Jefffrey , I've made the requested changes. Could you help review it again?


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


Re: [PR] fix: first none in `ListArray` panics in `cast_with_options` [arrow-rs]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #7065:
URL: https://github.com/apache/arrow-rs/pull/7065#discussion_r1946592305


##########
arrow-cast/src/cast/list.rs:
##########
@@ -88,6 +88,25 @@ where
     let mut mutable = MutableArrayData::new(vec![&values], nullable, cap);
     // The end position in values of the last incorrectly-sized list slice
     let mut last_pos = 0;
+
+    // Need to flag when previous vector(s) are empty/None to distinguish from 'All slices were correct length' cases.
+    let is_prev_empty = if array.offsets().len() < 2 {
+        false
+    } else {
+        let first_offset = array
+            .offsets()
+            .first()
+            .ok_or_else(|| ArrowError::ComputeError("Failed to get the first offset".into()))?
+            .as_usize();
+        let second_offset = array
+            .offsets()
+            .get(1)
+            .ok_or_else(|| ArrowError::ComputeError("Failed to get the second offset".into()))?
+            .as_usize();

Review Comment:
   ```suggestion
           let first_offset = array.offsets()[0].as_usize();
           let second_offset = array.offsets()[1].as_usize();
   ```
   
   nit: safe to use index as case where len < 2 is guarded above, so it can't panic



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


Re: [PR] fix: first none in `ListArray` panics in `cast_with_options` [arrow-rs]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #7065:
URL: https://github.com/apache/arrow-rs/pull/7065#discussion_r1940758109


##########
arrow-cast/src/cast/list.rs:
##########
@@ -88,11 +88,16 @@ where
     let mut mutable = MutableArrayData::new(vec![&values], nullable, cap);
     // The end position in values of the last incorrectly-sized list slice
     let mut last_pos = 0;
+    let mut null_first = false;

Review Comment:
   I don't believe it's about nulls being first, rather about a potentially empty list being first. Consider this test case which also panics during cast:
   
   ```rust
       #[test]
       fn test456() {
           let array = Arc::new(ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
               Some(vec![]),
               Some(vec![Some(1), Some(2)]),
           ])) as ArrayRef;
           let data_type =
               DataType::FixedSizeList(FieldRef::new(Field::new("item", DataType::Int64, true)), 2);
           let opt = CastOptions::default();
           let r = cast_with_options(&array, &data_type, &opt).unwrap();
   
           let fixed_array = Arc::new(FixedSizeListArray::from_iter_primitive::<Int64Type, _, _>(
               vec![None, Some(vec![Some(1), Some(2)])],
               2,
           )) as ArrayRef;
           assert_eq!(*fixed_array, *r);
       }
   ```
   
   This distinction is important as nullability isn't determined by the gap between offsets, rather it's determined by the null buffer.



##########
arrow-cast/src/cast/list.rs:
##########
@@ -112,16 +117,15 @@ where
         }
     }
 
-    let values = match last_pos {
-        0 => array.values().slice(0, cap), // All slices were the correct length
-        _ => {
-            if mutable.len() != cap {
-                // Remaining slices were all correct length
-                let remaining = cap - mutable.len();
-                mutable.extend(0, last_pos, last_pos + remaining)
-            }
-            make_array(mutable.freeze())
+    let values = if last_pos == 0 && !null_first {
+        array.values().slice(0, cap) // All slices were the correct length
+    } else {
+        if mutable.len() != cap {
+            // Remaining slices were all correct length
+            let remaining = cap - mutable.len();
+            mutable.extend(0, last_pos, last_pos + remaining)
         }
+        make_array(mutable.freeze())
     };

Review Comment:
   ```suggestion
       let values = match last_pos {
           0 if !null_first => array.values().slice(0, cap), // All slices were the correct length
           _ => {
               if mutable.len() != cap {
                   // Remaining slices were all correct length
                   let remaining = cap - mutable.len();
                   mutable.extend(0, last_pos, last_pos + remaining)
               }
               make_array(mutable.freeze())
           }
       };
   ```
   
   nit: using guard in the match arm would reduce the diff happening here



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


Re: [PR] fix: first none in `ListArray` panics in `cast_with_options` [arrow-rs]

Posted by "irenjj (via GitHub)" <gi...@apache.org>.
irenjj commented on code in PR #7065:
URL: https://github.com/apache/arrow-rs/pull/7065#discussion_r1942246993


##########
arrow-cast/src/cast/list.rs:
##########
@@ -88,11 +88,16 @@ where
     let mut mutable = MutableArrayData::new(vec![&values], nullable, cap);
     // The end position in values of the last incorrectly-sized list slice
     let mut last_pos = 0;
+    let mut null_first = false;

Review Comment:
   Thanks @Jefffrey for your review, in my local testing, `null_first` can identify both None and empty vec (the name `null_first` might be a bit misleading). My idea is to use `null_first` to prevent entering the `let values = match last_pos { 0 => }` situation.



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


Re: [PR] fix: first none/empty list in `ListArray` panics in `cast_with_options` [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #7065:
URL: https://github.com/apache/arrow-rs/pull/7065


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


Re: [PR] fix: first none in `ListArray` panics in `cast_with_options` [arrow-rs]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #7065:
URL: https://github.com/apache/arrow-rs/pull/7065#discussion_r1944361803


##########
arrow-cast/src/cast/list.rs:
##########
@@ -88,11 +88,16 @@ where
     let mut mutable = MutableArrayData::new(vec![&values], nullable, cap);
     // The end position in values of the last incorrectly-sized list slice
     let mut last_pos = 0;
+    let mut null_first = false;

Review Comment:
   I think I understand what's happening now. It seems its an edge case of the algorithm used here, where a 0 sized list element precedes a list of the proper size, e.g.
   
   ```
   Cast to fixed sized 2:
   [
       [],
       [],
       [1, 2],
   ]
   ```
   
   Where `last_pos` is never updated from 0 because `end_pos` for the first two elements is `0` and `last_pos` doesn't get updated when the list element already matches the fixed size to cast to.
   
   (Sorry if this was already obvious, just needed to air my thoughts out)
   
   Whilst this fix is correct, I'd like some more documentation, and especially rename `null_first` to something more accurate (as the problem is with empty size lists too), to explain this edge case so it's obvious to anyone else modifying the code here in the future.
   
   We could also look into moving the:
   
   ```rust
   if start_pos == 0 && end_pos == 0 {
   ```
   
   Check outside of the for loop as we only need to check it once upfront.



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


Re: [PR] fix: first none/empty list in `ListArray` panics in `cast_with_options` [arrow-rs]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #7065:
URL: https://github.com/apache/arrow-rs/pull/7065#issuecomment-2645826603

   Thanks @irenjj  and @Jefffrey 


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