Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute data buffer length by using start and end values in offset buffer #5741

Merged
merged 5 commits into from May 12, 2024

Conversation

viirya
Copy link
Member

@viirya viirya commented May 10, 2024

Which issue does this PR close?

Closes #5756.

Rationale for this change

Encountered an issue when importing empty variable-size binary layout array (e.g., string) from Java Arrow.

There is difference between Java Arrow and arrow-rs when computing the length of data buffer: apache/arrow#41610 (comment)

This is how Java Arrow imports an Utf8 array:

try (ArrowBuf offsets = importOffsets(type, VarCharVector.OFFSET_WIDTH)) {
      final int start = offsets.getInt(0);
      final int end = offsets.getInt(fieldNode.getLength() * (long) VarCharVector.OFFSET_WIDTH);
      final int len = end - start;
      ...
}

So even the offset buffer is not initialized, for empty array with one element offset buffer, end - start is always 0 that is the length of data buffer. That is why the added roundtrip tests are passed.

But in arrow-rs, it takes the last value of the offset buffer as the length of data buffer, i.e., end. If the value is not initialized to zero, the computed length of data buffer is incorrect.

That is what I found for the first offset value from the spec:

Generally the first slot in the offsets array is 0, and the last slot is the length of the values array.
When serializing this layout, we recommend normalizing the offsets to start at 0.

It looks like the first value doesn't have to be 0, although generally it is. So seems Java Arrow's approach is (more) correct.

What changes are included in this PR?

Changing the way to compute the length of data buffer when importing an empty variable-size binary layout array.

Are there any user-facing changes?

No

@github-actions github-actions bot added the arrow Changes to the arrow crate label May 10, 2024
@jhorstmann
Copy link
Contributor

There are comments a few lines above the changes about the len of the data buffer, which should probably be updated.

Other than that the change looks good to me, for slices of StringArray/BinaryArray the first offset would be != 0.

@viirya
Copy link
Member Author

viirya commented May 10, 2024

Thank you @jhorstmann. I've updated the code comment.

@viirya
Copy link
Member Author

viirya commented May 10, 2024

Hmm, our CI pipeline (Archery test With other arrows) seems broken:

  = note: /opt/conda/envs/arrow/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/../../../../x86_64-conda-linux-gnu/bin/ld: /root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-d2ef02247056996e.rlib(std-d2ef02247056996e.std.e4dfbc2c3f4b09f1-cgu.0.rcgu.o): in function `std::sys::pal::unix::stack_overflow::imp::sigstack_size':
          /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys/pal/unix/stack_overflow.rs:216: undefined reference to `getauxval'
          /opt/conda/envs/arrow/bin/../lib/gcc/x86_64-conda-linux-gnu/12.3.0/../../../../x86_64-conda-linux-gnu/bin/ld: /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys/pal/unix/stack_overflow.rs:216: undefined reference to `getauxval'
          collect2: error: ld returned 1 exit status

Created #5742

@alamb
Copy link
Contributor

alamb commented May 11, 2024

The Ci failure is covered by #5719

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Is it possible to write a test for this fix? Otherwise I worry we may end up breaking it in future releases without knowing

@viirya
Copy link
Member Author

viirya commented May 11, 2024

Is it possible to write a test for this fix? Otherwise I worry we may end up breaking it in future releases without knowing

I added a unit test that simulates an empty string array with a non-zero offset from a producer and checks the buffer_len call returns correct data buffer length.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Makes sense to me -- thank you @viirya and @jhorstmann

@viirya viirya merged commit cd39b8c into apache:master May 12, 2024
24 of 25 checks passed
@viirya
Copy link
Member Author

viirya commented May 12, 2024

Merged. Thanks @jhorstmann @alamb

@viirya viirya deleted the fix_offset_value branch May 12, 2024 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compute data buffer length by using start and end values in offset buffer
3 participants