Skip to content

feat: Deletion vector reader - #866

Merged
zeroshade merged 8 commits into
apache:mainfrom
Shreyas220:deletion_vector-reader
May 8, 2026
Merged

zeroshade merged 8 commits into
apache:mainfrom
Shreyas220:deletion_vector-reader

Conversation

@Shreyas220

Copy link
Copy Markdown
Contributor

No description provided.

@Shreyas220
Shreyas220 requested a review from zeroshade as a code owner April 9, 2026 11:28

@laskoviymishka laskoviymishka left a comment

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.

nice work!

this is a solid foundation for DV read support: envelope parsing is correct, puffin ReadAt integration is clean, and the Java golden test data is a great choice for cross-impl validation.

main concerns:

  1. OOM from sparse keys — the gap-filling loop in DeserializeRoaringPositionBitmap can allocate billions of empty bitmaps with adversarial input (keys [0, 0xFFFFFFFF]). needs a cap or a map-based representation
  2. Serialize writes empty gap bitmaps — Java only writes non-empty keys, so Go-serialized output won't match. reader-only for now but it's public — either fix or unexport
  3. ReadDV cardinality trap — always passes dvFile.Count() which is 0 when unset, rejecting valid DVs
  4. dvMockDataFile/strPtr missing — tests won't compile, looks like a helper file was left out of the PR
  5. 4 golden files unused — especially all-container-types-position-index.bin which exercises run containers, the main cross-library bug source

left some comments.

Also general observation - we should add for serialize / write puffin x-engine compatibility e2e tests, this probably beyond this PR scope, but need to keep this in mind once we start to enable DV support.

Comment thread table/deletion_vector.go Resolved
Comment thread table/dv/deletion_vector_test.go Resolved
Comment thread manifest.go Resolved
Comment thread table/roaring_bitmap.go Resolved
Comment thread table/dv/roaring_bitmap.go Resolved
Comment thread table/dv/roaring_bitmap.go Resolved
Comment thread table/dv/roaring_bitmap.go Resolved
Comment thread table/deletion_vector.go Resolved
Comment thread table/dv/deletion_vector.go Resolved
Comment thread table/roaring_bitmap_test.go Resolved
const (
// DVMagicNumber is the magic number for deletion vectors.
// Spec bytes: D1 D3 39 64 (big-endian) = 0x6439D3D1 (little-endian uint32)
DVMagicNumber uint32 = 0x6439D3D1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Will defining the constant this way work for big-endian architectures or do we need to add a conditional compilation etc? Can we just use the actual uint32 value to avoid endian differences?

@Shreyas220 Shreyas220 Apr 29, 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.

Comment thread table/dv/deletion_vector.go Resolved
Comment on lines +46 to +48
// - Magic (4 bytes, little-endian): must be 0x6439D3D1
// - Bitmap (variable): roaring bitmap in Iceberg portable format
// - CRC-32 (4 bytes, big-endian): checksum over magic + bitmap

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it's super weird that the spec jumps back and forth between big and little endian representations....

}
defer f.Close()

reader, err := puffin.NewReader(f)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

does the puffin reader need a defer reader.Close()?

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.

puffin reader doesn't own the file IO , it gets it from caller so i would like the caller of puffin file be responsible for closing not puffin reader

let me know if you think otherwise

Comment thread table/dv/roaring_bitmap.go Resolved
Comment thread table/dv/roaring_bitmap.go Resolved
Comment thread table/dv/roaring_bitmap.go Resolved
Comment thread table/dv/roaring_bitmap.go Resolved
Comment thread table/dv/roaring_bitmap.go Resolved
Comment thread manifest.go Resolved

@laskoviymishka laskoviymishka left a comment

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.

:shipit: no major concerns from my side.

A few minors to keep in mind for future work on DV:

  1. uint64 vs signed-with-guards. If we move to uint64 later, the pos < 0 guards and count < 0 checks go away. Probably the cleaner shape long-term.
  2. X-engine compat e2e tests for serialize/write once DV write support lands. Currently reader-only, so low risk today, but we'll want Spark/Java-produced DVs decoded + Go-produced DVs read back by Spark before enabling write. This is a way bigger item, so need to do a separate research
  3. maxBitmapCount is currently a package constant. If we ever hit a real table that legitimately needs more, it becomes a config knob conversation, worth a brief note in the code on where the number came from.

@Shreyas220
Shreyas220 force-pushed the deletion_vector-reader branch from 2abd5bb to 2d06d13 Compare April 29, 2026 20:18
laskoviymishka added a commit to laskoviymishka/iceberg-go that referenced this pull request May 8, 2026
- add writer-byte-identity check: regenerate the envelope in-memory and
  assert byte-equality against the on-disk fixture. without this the test
  was reader-only round-trip and writer drift would silently calcify into
  the next regen. this is the load-bearing wire-format pin.
- regen path self-validates via puffin.NewReader before overwriting, so a
  writer change that produces an unreadable file fails on regen rather
  than at the next CI run.
- tighten Fields assertion: assert.Equal []int32{} (not assert.Empty);
  Java's parser rejects 'fields': null, so a future writer regression
  emitting null instead of [] must fail.
- drop inner DV magic check: belongs to the deletion-vector codec
  (apache#866), not to the puffin envelope test. avoids two tests asserting
  the same constant in different packages.
- rename TestReadDeletionVectorPuffin -> TestDeletionVectorPuffinWireFormat;
  signal intent in test-list output.
- soften godoc + README: 'Java-equivalent wire format' rather than
  'Java-produced'. property values (referenced-data-file, cardinality,
  created-by) are fixture choices not Java bytes; only the inner roaring
  payload and the envelope structure are Java-canonical.
- README adds fixture details (50 bytes, positions 1/3/5/7/9, cardinality 5)
  and a diff-before-commit reminder for the regen recipe.
laskoviymishka added a commit to laskoviymishka/iceberg-go that referenced this pull request May 8, 2026
Pins the puffin envelope shape around a deletion-vector-v1 blob
without depending on the in-flight roaring decoder (apache#866). The
fixture is two files:

  - puffin/testdata/deletion-vector-v1-payload.bin — a Java-produced
    64-bit roaring DV payload lifted directly from apache/iceberg
    test resources (small-alternating-values-position-index.bin,
    50 bytes; bitmap encodes positions 1, 3, 5, 7, 9).

  - puffin/testdata/deletion-vector-v1.puffin — the same payload
    wrapped by puffin.Writer with the spec-canonical metadata
    (snapshot-id=-1, sequence-number=-1, deletion-vector-v1 blob
    type, referenced-data-file and cardinality properties).

The test asserts both layers. Reader side: blob count, type,
spec-mandated invariants, properties, and that ReadBlob round-trips
the inner payload byte-for-byte equal to the standalone Java fixture.
Writer side: regenerates the envelope in-memory and asserts byte
equality against the on-disk fixture. Without the writer-side check
the test was reader-only round-trip and writer drift would silently
calcify into the next regeneration.

Honest framing: this is a Go-writer wire-format pin with a Java-
equivalent inner payload, not a strong Java cross-impl pin. The
basic envelope shape is cross-checked by TestWriterBitIdenticalWithJava,
but that test does not exercise empty Fields arrays or multi-key
blob Properties — both of which this fixture relies on — and JSON
key ordering of blob Properties is encoder-defined.

A regen test gated on REGEN_FIXTURES=1 reproduces the .puffin from
the inner payload and self-validates by reading the blob back before
overwriting the on-disk file.

Closes apache#1008.
@laskoviymishka
laskoviymishka force-pushed the deletion_vector-reader branch from 2d06d13 to 036db40 Compare May 8, 2026 09:20
@laskoviymishka

Copy link
Copy Markdown
Contributor

@zeroshade, I rebased onto main, it should be mergeable now.

All issues from reviews addressed in the current state: uint64 typing across Set / Contains / count, []byte parameter on DeserializeRoaringPositionBitmap, slices.Sorted(maps.Keys(...)), for i := range count, plus the PuffinFile-only-with-EntryContentPosDeletes gate in the manifest builder.

Three threads from your review never got formally closed — would love your read on whether these are good as-is:

  1. DVMagicNumber = 0x6439D3D1 — matches Java’s 1681511377 byte-for-byte. uint32 has no inherent endianness, so the source form is style. Happy to switch to the decimal literal if you want source-level parity with Java.
  2. length field excluding CRC — documented inline now as “size of magic + bitmap data, excluding CRC-32”, per spec.
  3. Puffin reader Close — the reader doesn’t own the file IO; the caller passes it in, and ReadDV does defer f.Close() at the file level. Happy to add a no-op Close() on the reader if you’d prefer the symmetry.

Context for the nudge: C2 (#996, scanner integration) and C3 (#997, DV writer) are both blocked on this landing. I plan to focus on this next week, would love to start as soon as possible.

Once #866 is in, scanner should be a small wiring PR, and the DV writer becomes a real first-non-Java client item for v3.

Can we merge as-is?

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is good to merge as-is and we can see if anything comes up while we continue the work

@zeroshade

Copy link
Copy Markdown
Member

Thanks much!

@zeroshade
zeroshade merged commit 774c8ad into apache:main May 8, 2026
14 checks passed
laskoviymishka added a commit to laskoviymishka/iceberg-go that referenced this pull request May 10, 2026
Closes apache#996. v3 tables with deletion vectors are now readable end-to-end.
Before this change, arrow_scanner.go's GetRecords returned ErrNotImplemen-
ted for any task carrying a DV manifest entry, and scan planning paired
DVs with their data files but never read the bitmaps. This wires those
plans through to the row-filter pipeline.

Scanner read path. readAllDeletionVectors mirrors readAllDeleteFiles in
shape — concurrent reads via errgroup, returns a perFilePosDeletes map
keyed by referenced data file path. A compile-time signature assertion
in the test file pins that both loaders share the perFilePosDeletes
return type, so the merge in GetRecords (`deletesPerFile[file] =
append(...)`) is type-safe by construction. The downstream collapse into
a set[int64] inside recordsFromTask / producePosDeletesFromTask absorbs
both sources without pipeline changes.

Dedup is by referenced-data-file path, not by puffin file path: the
spec permits one puffin file to carry multiple DV blobs (one per data
file), and keying by the puffin path would silently drop all but the
first. The same loop rejects (a) DV entries missing referenced_data_file
or content_offset / content_size_in_bytes, (b) two distinct DV blobs
(different puffin path OR content offset) targeting the same data file
— the latter mirrors Java's DeleteFileIndex "Can't index multiple DVs
for %s" ValidationException, since silently unioning two bitmaps is an
over-deletion bug. Same-blob dedup across multiple tasks is a no-op
(deliberate divergence from Java's stricter rejection — re-reading the
same blob is wasteful, not incorrect).

Validation runs entirely up front before any goroutine launches. An
earlier draft mixed validation into the goroutine-dispatch loop, which
created a close-on-closed-channel panic when a bad entry was reached
after one or more g.Go calls had dispatched workers still trying to send
to the result channel. The current shape — populate uniqueDVs in a
serial pre-pass, then fan out — makes that state unreachable by
construction. On goroutine error, accumulated *arrow.Chunked values are
released before returning, since arrow.NewChunked allocations are not
freed by GC alone.

bitmapToInt64Chunked materializes a bitmap as a chunked Int64 array via
the new dv.RoaringPositionBitmap.Iter() iter.Seq[uint64] method. Lazy
iteration avoids the intermediate []uint64 allocation that would double
memory pressure for large DVs. The Reserve hint is clamped to math.Max-
Int so a 32-bit int build (or a future raised puffin blob-size limit)
can't wrap the Cardinality int64 cast to a negative reservation. The
int64(p) position cast trusts dv.DeserializeDV as the bytes-to-bitmap
boundary — positions ≥ 2^63 would wrap, but the spec bounds positions
by the data file's row count, well under that limit.

scanner.go: a data file with an applicable DV no longer also has its
Parquet position-delete files matched. Spec §Scan Planning predicate
"There is no deletion vector that must be applied to the data file" is
a precondition for pos-delete application, and Java's DeleteFileIndex.
forDataFile enforces it. The gap was latent before this PR because
GetRecords errored on DVs; making DVs readable also makes pairing
DV+pos-deletes for the same file observable. For spec-compliant tables
the set-union absorbs the overlap, but a writer whose DV omits prior
pos-delete positions would silently over-delete.

dv package: RoaringPositionBitmap gains Iter() (primary, lazy) and
ToArray() (convenience wrapper via slices.AppendSeq). The Iter contract
is single-goroutine / no-mutation-mid-iteration — the closure captures
the bitmap by pointer and roaring.Bitmap doesn't support concurrent
Set/Iterator.

Tests build a real puffin file via puffin.Writer rather than vendoring
fixture bytes, so this PR is independent of apache#1041's golden fixture.
Coverage: position decoding and dedup keying by referenced data file,
identical-DV dedup across multiple tasks, no-tasks no-op, the four
rejection paths (missing referenced_data_file, missing content_offset,
multiple distinct DVs for same data file, nil-ref-before-goroutine-
dispatch panic regression), and the compile-time signature assertion.
TODO(apache#1041) marks the hand-built DV envelope in writeDVPuffinFixture for
replacement once that PR exports dv.SerializeDV.

End-to-end coverage (DV positions actually filtered out of GetRecords
output for a real Parquet data file) is deferred — would need v3 table-
metadata scaffolding outside this PR's scope. Several adjacent gaps are
also deferred as follow-ups: DV sequence-number guard in PlanFiles
(pre-existing from apache#855), tighter ReadDV cardinality validation (pre-
existing from apache#866), Puffin reader DV snapshot-id/sequence-number guard
(pre-existing from apache#676), and the parallel arrow-leak in readAllDelete-
Files (pre-existing).
zeroshade pushed a commit that referenced this pull request May 12, 2026
Pins the puffin envelope shape around a deletion-vector-v1 blob without
depending on the in-flight roaring decoder (#866). The fixture is two
files:

- puffin/testdata/deletion-vector-v1-payload.bin — a Java-produced
64-bit roaring DV payload lifted directly from apache/iceberg test
resources (small-alternating-values-position-index.bin, 50 bytes; bitmap
encodes positions 1, 3, 5, 7, 9).

- puffin/testdata/deletion-vector-v1.puffin — the same payload wrapped
by puffin.Writer with the spec-canonical metadata (snapshot-id=-1,
sequence-number=-1, deletion-vector-v1 blob type, referenced-data-file
and cardinality properties).

The test asserts both layers. Reader side: blob count, type,
spec-mandated invariants, properties, and that ReadBlob round-trips the
inner payload byte-for-byte equal to the standalone Java fixture. Writer
side: regenerates the envelope in-memory and asserts byte equality
against the on-disk fixture. Without the writer-side check the test was
reader-only round-trip and writer drift would silently calcify into the
next regeneration.

Honest framing: this is a Go-writer wire-format pin with a Java-
equivalent inner payload, not a strong Java cross-impl pin. The basic
envelope shape is cross-checked by TestWriterBitIdenticalWithJava, but
that test does not exercise empty Fields arrays or multi-key blob
Properties — both of which this fixture relies on — and JSON key
ordering of blob Properties is encoder-defined.

A regen test gated on REGEN_FIXTURES=1 reproduces the .puffin from the
inner payload and self-validates by reading the blob back before
overwriting the on-disk file.

Closes #1008.
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.

3 participants