Skip to content

Nullability in schema is not considered in VectorLoader #648

Closed

Description

@viirya

Describe the bug, including details regarding any error messages, version, and platform.

We hit an issue on using VectorLoader to load some Arrow vectors.

java.util.NoSuchElementException
        at java.base/java.util.ArrayList$Itr.next(ArrayList.java:970)
        at org.apache.arrow.vector.VectorLoader.loadBuffers(VectorLoader.java:104)
        at org.apache.arrow.vector.VectorLoader.load(VectorLoader.java:84)

The schema of the VectorSchemaRoot is Schema<_0: Utf8 not null>.
The field vector in the root is Utf8 type, not nullable. As it is Utf8 type, TypeLayout.getTypeBufferCount reports buffer count 3 for it.

The IPC ArrowRecordBatch message to load has one node: ArrowFieldNode [length=1500, nullCount=0], and two buffers:

buffer: ArrowBuf[...], address:....., capacity:..., ArrowBuf
buffer: ArrowBuf[...], address:....., capacity:..., ArrowBuf

So when VectorLoader.loadBuffers is trying to load buffers by iterating the buffer list, it assumes there are 3 buffers but actually there are only 2 buffers (null buffer doesn't exist). That's why it hits NoSuchElementException.

I think that an array that in the spec can contain a null bitmap may choose to not allocate the validity buffer (also see the doc). So the Utf8 array with 2 buffers is correct by the spec. The issue looks like that VectorLoader doesn't consider field nullability when loading buffers.

We uses Arrow Java 15.0.2 version. But as I just looked at the current code in this repo, looks like current TypeLayout has this issue still.

Activity

  1. added
    Type: bug
    on Mar 4, 2025
  2. self-assigned this
    on Mar 4, 2025
  3. viirya commented on Mar 4, 2025

    @viirya
    MemberAuthor

    I will open a PR soon.

  4. added a commit that references this issue on Mar 4, 2025
  5. lidavidm commented on Mar 4, 2025

    @lidavidm
    Member

    Field nullability is irrelevant: it's the actual number of nulls that matters. A nullable vector may still contain no nulls and this case should still apply.

      /// The number of observed nulls. Fields with null_count == 0 may choose not
      /// to write their physical validity bitmap out as a materialized buffer,
      /// instead setting the length of the bitmap buffer to 0.
      null_count: long;
    

    It seems that the problem is more that the code loading the buffers should be inserting a zero-length (or possibly null) buffer where the nullability buffer would have gone so that expectations line up.

  6. lidavidm commented on Mar 4, 2025

    @lidavidm
    Member

    (Also, note that nothing really validates field nullability so it is very possible to have a non-nullable field and yet still have nulls in the data; IPC code shouldn't rely on field nullability ever IMO.)

  7. viirya commented on Mar 4, 2025

    @viirya
    MemberAuthor

    Field nullability is irrelevant: it's the actual number of nulls that matters. A nullable vector may still contain no nulls and this case should still apply.

    Oh, yes, that's correct. It is not due to the field nullability but actually the null count in the vector. My description is not accurate.

  8. viirya commented on Mar 4, 2025

    @viirya
    MemberAuthor

    Hmm, after re-thinking it, I wonder if this is a bug actually? Although in the physical memory layout of an array, the null buffer may not be allocated, however, as the message to load is an ipc RecordBatch message, maybe the null buffer cannot be ignored in the message? I.e., at least it needs to be an empty one? If so, then the issue maybe due to the incorrect message produced by somewhere in our code (it seems not from Arrow Java).

  9. lidavidm commented on Mar 4, 2025

    @lidavidm
    Member

    AIUI, yes, there still needs to be an entry in the list of buffers in the Flatbuffer metadata, even if there is no memory allocated.

  10. viirya commented on Mar 4, 2025

    @viirya
    MemberAuthor

    Yeah, so seems the issue is that we don't put the entry in the message. The code is not from me. So I may need to take more looks. Thanks for confirming it.

  11. lidavidm commented on Mar 4, 2025

    @lidavidm
    Member

    Though, from the exception, it seems actually the issue is that there's more entries than buffers, not the other way around?

  12. lidavidm commented on Mar 4, 2025

    @lidavidm
    Member

    Ah, I see now - there's not an entry, so there's not a buffer generated, and VectorLoader iterates based on fixed knowledge of the type layout, ignoring the entries? In that case yes I think the Java library is right, albeit not very clear about what went wrong.

  13. viirya commented on Mar 4, 2025

    @viirya
    MemberAuthor

    There are 2 entries in the messages for the data and offset. The null buffer is not. The message is not produced by the Java library, I think. It comes from other internal library. I need to look where it was produced like that.

    VectorLoader needs to read 3 entries for the case, so it just hits the NoSuchElementException.

  14. viirya commented on Mar 4, 2025

    @viirya
    MemberAuthor

    Oh, the error case could be improved than an exception like current one, though. I will open a small PR for it. Sounds good?

  15. lidavidm commented on Mar 4, 2025

    @lidavidm
    Member

    That would be appreciated, yes!

  16. added this to the 18.3.0 milestone on Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions