Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/iceberg/arrow_c_data_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,14 @@ Result<ArrowArray> ProjectBatchNanoarrow(
internal::ArrowArrayViewGuard input_view_guard(&input_view);
ICEBERG_NANOARROW_RETURN_UNEXPECTED_WITH_ERROR(
ArrowArrayViewSetArray(&input_view, &input_batch, &error), error);
// FULL validation is required here: the append path below uses
// ArrowArrayViewGet{String,Bytes}Unsafe and raw list/map offset arithmetic,
// which nanoarrow only documents as memory-safe after FULL validation
// (DEFAULT does not check that every offsets-buffer entry is non-decreasing
// and in-bounds). This matches the FULL validation already performed on the
// 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)?

error);

ArrowArray output_array;
Expand Down
39 changes: 39 additions & 0 deletions src/iceberg/test/arrow_c_data_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
/// \file arrow_c_data_util_test.cc
/// Verifies ProjectBatch behavior across registered implementations.

#include <array>
#include <cstdint>
#include <string_view>
#include <utility>
Expand Down Expand Up @@ -191,4 +192,42 @@ TEST(ProjectBatchTest, ProjectionRejectsNestedPruning) {
EXPECT_THAT(projection, IsError(ErrorKind::kInvalidArgument));
}

TEST(ProjectBatchTest, RejectsNonMonotonicStringOffsets) {
auto schema = std::make_shared<Schema>(
std::vector<SchemaField>{SchemaField::MakeRequired(1, "value", string())});

// Arrow's builders always emit monotonic offsets, so a batch with a
// corrupt offsets buffer has to be hand-assembled via the nanoarrow C ABI.
// offsets = [0, 5, 2] with a 2-byte data buffer: DEFAULT validation only
// checks the first (0) and last (2) offsets, both of which are consistent
// with the 2-byte buffer, so it accepts this array. FULL validation also
// walks the offsets buffer and rejects it because 2 < 5.
ArrowArray root;
ASSERT_EQ(ArrowArrayInitFromType(&root, NANOARROW_TYPE_STRUCT), NANOARROW_OK);
ASSERT_EQ(ArrowArrayAllocateChildren(&root, 1), NANOARROW_OK);

ArrowArray* value_array = root.children[0];
ASSERT_EQ(ArrowArrayInitFromType(value_array, NANOARROW_TYPE_STRING), NANOARROW_OK);
const std::array<int32_t, 3> raw_offsets = {0, 5, 2};
ASSERT_EQ(ArrowBufferAppend(ArrowArrayBuffer(value_array, 1), raw_offsets.data(),
raw_offsets.size() * sizeof(int32_t)),
NANOARROW_OK);
ASSERT_EQ(ArrowBufferAppend(ArrowArrayBuffer(value_array, 2), "ab", 2), NANOARROW_OK);
value_array->length = 2;
value_array->null_count = 0;
ASSERT_EQ(ArrowArrayFinishBuildingDefault(value_array, nullptr), NANOARROW_OK);

root.length = 2;
root.null_count = 0;
ASSERT_EQ(ArrowArrayFinishBuildingDefault(&root, nullptr), NANOARROW_OK);

auto projection = ProjectionContext::Make(*schema, *schema, nullptr);
ASSERT_THAT(projection, IsOk());

// No rows are selected, so the malformed offsets must be caught by batch
// validation itself rather than by appending the corrupt row.
auto result = ProjectBatch(&root, /*row_indices=*/{}, projection.value());
EXPECT_THAT(result, IsError(ErrorKind::kInvalidArrowData));
}

} // namespace iceberg::internal
Loading