Skip to content

[Iceberg] Fix manifest bounds being padded with trailing 0x00 bytes#38580

Merged
ahmedabu98 merged 4 commits into
apache:masterfrom
dejii:fix/iceberg-manifest-bound-byte-padding
May 22, 2026
Merged

[Iceberg] Fix manifest bounds being padded with trailing 0x00 bytes#38580
ahmedabu98 merged 4 commits into
apache:masterfrom
dejii:fix/iceberg-manifest-bound-byte-padding

Conversation

@dejii
Copy link
Copy Markdown
Contributor

@dejii dejii commented May 21, 2026

Summary

SerializableDataFile.toByteArrayMap used ByteBuffer.array() to extract bound bytes. array() returns the full backing array, ignoring the buffer's [position, limit) content window.

Iceberg's Conversions.toByteBuffer delegates to JDK CharsetEncoder.encode(CharBuffer), which sizes its allocation by (int)(chars * averageBytesPerChar). For UTF-8 the average is 1.1, so a 15-char string lands in a 16-byte buffer with position=0, limit=15, capacity=16. The 16th byte is the zero-initialized tail of the backing array.

Reading via .array() leaks that tail into the byte[] form, and on the return trip ByteBuffer.wrap(byte[]) treats it as content. The manifest ends up with a lower bound that is strictly greater than the real minimum byte-lexicographically. Query engines using Iceberg metadata for predicate pushdown decide value < lower_bound for any equality filter on that minimum and prune every file, returning 0 rows.

Why the bug stays quiet for most queries

All three must hold:

  1. Encoded length 10–N bytes (N = truncate(N) from metrics config, default 16). Below 10, the JDK encoder allocates exact-size buffers; above N, Iceberg trims the string first, so the bound holds a truncated form that a full-value query naturally sorts past.
  2. Value is the file's actual lower bound on that column. Other values never compare against the padding byte.
  3. Predicate is eligible for bound pruning (=, IN, etc.).

Fix

  • Fix by copying the buffer's [position, limit) window. Added a regression test that builds a DataFile with bounds matching that shape, roundtrips through SerializableDataFile, and asserts the resulting byte[] match the encoded content rather than the inflated backing array.
  • New writes will produce correct bounds. Tables already written by the buggy sink keep broken bounds until their manifests are rewritten

Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a bug where manifest bounds in Iceberg tables were being incorrectly serialized due to the use of ByteBuffer.array(). This method returns the entire backing array, which often includes trailing zero-initialized bytes when the buffer is over-allocated by encoders. These leaked bytes corrupted the lower bounds, causing query engines to incorrectly prune files during predicate pushdown. The fix ensures that only the relevant content window is copied, and a regression test has been added to verify the fix.

Highlights

  • Fixing manifest bound corruption: Updated SerializableDataFile to correctly extract byte arrays from ByteBuffers by respecting the [position, limit) window instead of using the full backing array.
  • Regression testing: Added a new test case in SerializableDataFileTest that explicitly reproduces the over-allocated buffer scenario to ensure bounds are correctly serialized.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request fixes a bug in SerializableDataFile where ByteBuffer.array() was used to extract data, potentially leaking trailing bytes from the backing array into manifest bounds. A new toByteArray helper method now correctly copies only the active portion of the buffer. A corresponding unit test was added to verify that bounds are copied based on the buffer's limit rather than the backing array's capacity. I have no feedback to provide.

@github-actions
Copy link
Copy Markdown
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

@dejii
Copy link
Copy Markdown
Contributor Author

dejii commented May 21, 2026

R: @ahmedabu98

@github-actions
Copy link
Copy Markdown
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

@dejii
Copy link
Copy Markdown
Contributor Author

dejii commented May 21, 2026

assign set of reviewers

@github-actions
Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @kennknowles for label java.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Copy link
Copy Markdown
Contributor

@ahmedabu98 ahmedabu98 left a comment

Choose a reason for hiding this comment

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

Great job finding this one. Left one comment

dejii added a commit to dejii/beam that referenced this pull request May 21, 2026
Per review on apache#38580, exercise the real production path that produces
the buggy buffer shape (capacity > limit) rather than hand-constructing
the buffer. The original synthetic version decoupled the test from
JDK/Iceberg encoder behavior, but the bug can't exist if those don't
over-allocate, so the decoupling argument doesn't add much.
@dejii dejii force-pushed the fix/iceberg-manifest-bound-byte-padding branch from d9d6ba3 to 0b677ad Compare May 21, 2026 18:09
@dejii
Copy link
Copy Markdown
Contributor Author

dejii commented May 21, 2026

Seems like a transient CI failure:

> Could not GET 'https://repo.maven.apache.org/maven2/org/eclipse/platform/org.eclipse.text/3.9.0/org.eclipse.text-3.9.0.jar'. Received status code 403 from server: Forbidden

@dejii dejii requested a review from ahmedabu98 May 22, 2026 13:29
Copy link
Copy Markdown
Contributor

@ahmedabu98 ahmedabu98 left a comment

Choose a reason for hiding this comment

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

LGTM, thx for fixing this!

@ahmedabu98 ahmedabu98 merged commit ef5f89f into apache:master May 22, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants