Skip to content

fix(arrow): use FULL nanoarrow validation in ProjectBatch - #1043

Open
gty404 wants to merge 1 commit into
apache:mainfrom
gty404:fix/arrow-projection-full-validation
Open

gty404 wants to merge 1 commit into
apache:mainfrom
gty404:fix/arrow-projection-full-validation

Conversation

@gty404

@gty404 gty404 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • ProjectBatchNanoarrow validated incoming batches at NANOARROW_VALIDATION_LEVEL_DEFAULT before using ArrowArrayViewGet{String,Bytes}Unsafe and raw list/map offset arithmetic per row. nanoarrow documents these Unsafe accessors as memory-safe only after FULL validation: DEFAULT only checks the first/last offsets, not that every intermediate offset is non-decreasing and in-bounds.
  • A non-monotonic intermediate offset can produce a negative size that overflows to a huge size_t in the append path, or an out-of-range offset whose bytes get copied into the returned output batch.
  • Raise the validation level to NANOARROW_VALIDATION_LEVEL_FULL, matching what manifest_reader.cc already does for the same untrusted-batch boundary, closing the inconsistency between the two call sites.

Test plan

  • Added ProjectBatchTest.RejectsNonMonotonicStringOffsets, which hand-assembles (via the nanoarrow C ABI, since Arrow's builders always emit monotonic offsets) a string array with offsets [0, 5, 2]: the first/last offsets are consistent with the 2-byte data buffer, so DEFAULT validation accepts it, but FULL validation correctly rejects it as non-monotonic.
  • Verified the new test fails without the fix (stashed the one-line change, rebuilt, confirmed ProjectBatch returned a value instead of an error) and passes with it.
  • ./src/iceberg/test/data_test — 103/103 passed.
  • pre-commit run on the changed files — passed.

ProjectBatchNanoarrow validated incoming batches at
NANOARROW_VALIDATION_LEVEL_DEFAULT before using
ArrowArrayViewGet{String,Bytes}Unsafe and raw list/map offset
arithmetic per row. nanoarrow documents these Unsafe accessors as
memory-safe only after FULL validation: DEFAULT only checks the
first/last offsets, not that every intermediate offset is
non-decreasing and in-bounds. A non-monotonic offset can produce a
negative size that overflows to a huge size_t in the append path, or
an out-of-range offset that gets copied into the output batch.

Raise the validation level to FULL, matching what
manifest_reader.cc already does for the same untrusted-batch
boundary.
@gty404
gty404 force-pushed the fix/arrow-projection-full-validation branch from b2b000b to 195d475 Compare October 20, 2026 01:53
// equivalent untrusted-batch boundary in manifest_reader.cc.
ICEBERG_NANOARROW_RETURN_UNEXPECTED_WITH_ERROR(
ArrowArrayViewValidate(&input_view, NANOARROW_VALIDATION_LEVEL_DEFAULT, &error),
ArrowArrayViewValidate(&input_view, NANOARROW_VALIDATION_LEVEL_FULL, &error),

@shangxinli shangxinli Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ProjectBatch runs per batch on the scan path via file_scan_task_reader.cc:118, so FULL validation now walks every offsets buffer on every batch. Unlike the manifest_reader case these come from our own readers - is that cost intended?

@gty404 gty404 Oct 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, the cost is real but small in absolute terms. I benchmarked ProjectBatch (nanoarrow path) on a 50k-row batch (int32 + string column) with DEFAULT vs FULL validation, averaged over 200 runs:

selectivity DEFAULT FULL overhead
100% rows alive ~4074 us/batch ~4219 us/batch +3.6%
1% rows alive (heavy delete filtering) ~44.5 us/batch ~82.2 us/batch +85% (~1.85x)

So in the worst case (most rows filtered out by deletes, which is exactly where the row-append cost no longer dominates), FULL validation roughly doubles this step's time - but the absolute delta is ~38us for a 50k-row batch (~0.75ns/row), which should be negligible next to I/O/decode for a real scan.

Given nanoarrow has no bounds-checked "safe" counterpart to ArrowArrayViewGetStringUnsafe/GetBytesUnsafe in this pinned version, FULL batch-level validation is the only way to satisfy contract today. @wgtmac curious for your take: keep this as-is given the cost is small and bounded, or is there a reason to treat the in-tree reader path differently (e.g. skip FULL validation when the batch is known to come from our own Avro/Parquet readers)?

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants