Skip to content

fix: return NULL from array_length below an empty nested list - #28038

Open
bss-k wants to merge 1 commit into
apache:mainfrom
bss-k:fix-array-length-empty-nested
Open

bss-k wants to merge 1 commit into
apache:mainfrom
bss-k:fix-array-length-empty-nested

Conversation

@bss-k

@bss-k bss-k commented Dec 5, 2026

Copy link
Copy Markdown

Which issue does this PR close?

Rationale for this change

array_length panics with index out of bounds: the len is 1 but the index is 1 when a row is an empty list of lists and the requested dimension is deeper than the empty level:

select array_length(arrow_cast(make_array(), 'List(List(Int64))'), 2);

It should return NULL, like array_length(make_array(), 2) already does.

What changes are included in this PR?

compute_array_length steps into deeper dimensions with .value(0), which panics on an empty list. It now returns NULL when the current level is empty and the requested dimension is deeper:

         if current_dimension == dimension {
             return Ok(Some(value.len() as u64));
         }
+        if value.is_empty() {
+            return Ok(None);
+        }

The check comes after the dimension match, so the length of an empty level itself is still 0 (for example array_length([], 1) = 0). One check covers List, LargeList and FixedSizeList.

This is separate from #27860, which is about whether a zero length should become NULL. That PR does not touch the .value(0) call that panics here.

What is the testing strategy for this PR?

New sqllogictest case in array/array_length.slt:

Input Dimension Expected
empty List(List(Int64)) 1 0 (unchanged)
empty List(List(Int64)) 2 NULL
empty LargeList(List(Int64)) 2 NULL
[[]] as List(List(List(Int64))) 3 NULL

Without the fix this query panics with index out of bounds: the len is 1 but the index is 1. With the fix it passes, and the other array/*.slt files and the length unit tests still pass.

Are there any user-facing changes?

array_length returns NULL instead of panicking for these inputs. No API changes.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Dec 5, 2026

@Areesha-Rizwan412 Areesha-Rizwan412 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified at head a6e0bac against #28020.

The guard placement is right: in compute_array_length the current_dimension == dimension check still returns value.len() first, so an empty list at the requested dimension returns 0, and the new value.is_empty() check only fires when descending further, returning NULL instead of panicking on .value(0). Since the guard sits before the type match, it covers all three descent sites (ListArray, LargeListArray, FixedSizeListArray) in one place.

That matches the existing behavior for array_length(make_array(), 2) returning NULL, and the new sqllogictest covers List, LargeList, and a three-level nesting, which pins the deeper-dimension case from the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

array_length panics on an empty nested list with dimension 2

2 participants