Skip to content

perf(index): record zone map seed null positions as runs - #9860

Merged
LuQQiu merged 2 commits into
lance-format:mainfrom
LuQQiu:lu/ent2893-seed-null-runs
Oct 10, 2026
Merged

LuQQiu merged 2 commits into
lance-format:mainfrom
LuQQiu:lu/ent2893-seed-null-runs

Conversation

@LuQQiu

@LuQQiu LuQQiu commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Why this matters

A newly added column may start with every value NULL, then be populated later by a refresh or feature-computation job. Partially populated columns can also contain long consecutive runs of NULLs. The main target of this PR is the all-null initial state: collecting write seeds should be cheap when the column does not contain real values yet.

Previously, the seed writer inserted every null row position individually, and the serialized bitmap could remain large even when every row was NULL. For 1M all-null Int64 rows, the complete seed buffer was about 518 KiB. This PR reduces it to 6.6 KiB while preserving the exact null positions. In the measured 4M-row append, time drops from 77.7 ms to 35.7 ms (54% less).

What this changes

ZoneMap write seeds record column statistics and the exact row positions of nulls while data is written. This PR makes collecting and storing consecutive null positions cheaper. It keeps the null bitmap and the existing zone design (8,192 rows in the measurements).

Step Before After
Collect null positions Check each row and insert each null position separately. Find consecutive null ranges in Arrow's validity buffer and insert each range together.
Serialize the bitmap Serialize without requesting run compression. Call RoaringBitmap::optimize() so consecutive positions can use compact run containers.

For example, 900 consecutive nulls previously required 900 individual inserts. They now require one range insert; serialization can represent that range compactly.

Before/after measurements

Both versions have seeds enabled. Append time measures writing 4M rows, including data-file writes and seed collection. Seed size is the complete serialized seed buffer per 1M rows, including column statistics and the null bitmap—not just the bitmap.

Int64 input Append before Append after Seed size before Seed size after
All null (main target) 77.7 ms 35.7 ms 518.2 KiB 6.6 KiB
50% consecutive nulls 113.7 ms 97.1 ms 262.0 KiB 6.1 KiB
50% random nulls 219.0 ms 209.2 ms 518.2 KiB 518.2 KiB
10% random nulls 148.5 ms 147.8 ms 499.3 KiB 499.3 KiB
1% random nulls 118.8 ms 114.1 ms 67.7 KiB 67.7 KiB
No nulls 62.8 ms 63.3 ms 5.7 KiB 5.7 KiB
  • All null: append time drops by about 54%; seed size drops from 518.2 to 6.6 KiB per 1M rows.
  • Consecutive nulls: append time drops by about 15%; seed size drops from 262.0 to 6.1 KiB.
  • Random nulls: storage size stays unchanged. Append improvements are smaller; the 10% case is effectively unchanged.
  • No nulls: effectively unchanged.

Medians of five repetitions per case, identical generated inputs, 8192 rows per zone, and 1M rows per data file. Measurements used an AMD EPYC 9V65 machine with local NVMe storage and release-with-debug. Small timing differences should not be treated as guaranteed speedups.

Compatibility and validation

  • Exact null positions and min/max/null-count semantics are unchanged.
  • The seed-buffer layout, metadata key, and reader code are unchanged. Compression uses the existing Roaring format.
  • Seed defaults and the existing hex/IPC metadata storage are unchanged.
  • This PR optimizes the seed writer; it does not add seed collection to column-add/refresh paths or enable seeds by default. The measurements cover null-heavy writes, not an end-to-end refresh.
  • A regression test verifies null positions across sliced batches and zone boundaries.
  • For the measured cases, seed-built statistics matched scan-built statistics; query row-ID multisets matched with and without the index. Seed harvesting was confirmed in all five repetitions.

Benchmark harness: zone_map_seeds.rs.

Track null positions in zone map write seeds by scanning the validity
buffer for runs and inserting ranges instead of one bitmap insert per
null row, and run-optimize the bitmap before serializing it, so all-null
and clustered-null columns cost almost nothing to seed.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@github-actions github-actions Bot added A-index Vector index, linalg, tokenizer performance labels Oct 10, 2026
@LuQQiu
LuQQiu marked this pull request as ready for review October 10, 2026 02:47

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@LuQQiu
LuQQiu marked this pull request as draft October 10, 2026 02:47
@LuQQiu

LuQQiu commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

Benchmark: before vs after this change

Same harness and rig as #9792 (zone_map_seeds bench; AMD EPYC 9V65, local NVMe RAID 0, release-with-debug, 8192 rows per zone, 1M rows per data file, 4M appended rows, 1M for 2 KiB text, 5 repetitions, medians, every run in a fresh process, byte-identical input before and after). "Seed writer ns/row" and "finish ms/file" are the micro layer (the writer alone); "append" is the e2e layer (elapsed append with seeds on relative to seeds off); "update" is optimize_indices from a fresh session. Before = main at 518dc4c (one bitmap insert per null row), after = this PR. Seed-built and scan-built zone maps were compared zone by zone and filter row ids with/without the index on every case (18/18 passed); harvest confirmed by the tracing events on 5/5 repetitions of every seeds-on case.

Column Nulls Seed writer ns/row before → after finish ms/file before → after Seed KiB per 1M rows before → after Append with seeds vs off, before → after Update with seeds ms before → after
Int64 no nulls 2.0 → 2.0 0.07 → 0.06 6 → 6 +18% → +19% 4 → 5 (harvest 5/5)
Int64 1% random 4.0 → 2.6 0.51 → 0.50 68 → 68 +18% → +12% 5 → 5 (harvest 5/5)
Int64 10% random 6.8 → 6.3 4.20 → 4.22 499 → 499 +44% → +39% 8 → 9 (harvest 5/5)
Int64 50% random 15.2 → 11.0 3.80 → 3.94 518 → 518 +48% → +42% 9 → 9 (harvest 5/5)
Int64 all null 8.1 → 0.7 3.63 → 0.07 518 → 7 +142% → +4% 9 → 5 (harvest 5/5)
Int64 50% clustered 4.6 → 1.4 1.97 → 0.06 262 → 6 +26% → +8% 7 → 4 (harvest 5/5)
Utf8 32-char no nulls 7.0 → 7.0 0.11 → 0.11 12 → 12 +5% → +4% 5 → 5 (harvest 5/5)
Utf8 32-char 1% random 9.1 → 7.7 0.52 → 0.52 74 → 74 +6% → +5% 5 → 7 (harvest 5/5)
Utf8 32-char 10% random 11.7 → 11.0 4.32 → 4.31 506 → 506 +10% → +10% 9 → 9 (harvest 5/5)
Utf8 32-char 50% random 17.9 → 13.7 3.93 → 4.00 525 → 525 +14% → +11% 9 → 10 (harvest 5/5)
Utf8 32-char all null 8.6 → 0.7 3.67 → 0.08 517 → 6 +117% → +8% 8 → 4 (harvest 5/5)
Utf8 32-char 50% clustered 7.4 → 3.9 1.97 → 0.10 265 → 9 +10% → +3% 8 → 5 (harvest 5/5)
Utf8 ~2 KiB no nulls 45.4 → 45.2 0.35 → 0.32 403 → 403 +0% → +0% 11 → 12 (harvest 5/5)
Utf8 ~2 KiB 1% random 52.5 → 50.0 0.79 → 0.77 464 → 464 -0% → +0% 11 → 11 (harvest 5/5)
Utf8 ~2 KiB 10% random 49.6 → 49.0 4.33 → 4.26 899 → 899 +0% → +0% 10 → 10 (harvest 5/5)
Utf8 ~2 KiB 50% random 42.9 → 38.6 3.93 → 3.96 916 → 916 +0% → +0% 10 → 10 (harvest 5/5)
Utf8 ~2 KiB all null 8.7 → 0.9 3.68 → 0.20 517 → 6 +16% → +3% 5 → 4 (harvest 5/5)
Utf8 ~2 KiB 50% clustered 21.8 → 20.5 2.01 → 0.26 461 → 205 +0% → +0% 9 → 8 (harvest 5/5)

How to read it:

  • Columns without nulls are unchanged: the run scan only runs on chunks that contain nulls.
  • All-null and clustered-null columns stop paying for the bitmap. The writer drops to the min/max cost (0.7–1.4 ns/row), finish() drops from 2–4 ms to about 0.1 ms per file, the seed shrinks from 262–518 KiB to 6–9 KiB per million rows (run containers), and the append overhead on an Int64 column goes from +142% / +26% to +4% / +8%.
  • Randomly scattered nulls get the cheaper run scan (Int64, 50% nulls: 15.2 → 11.0 ns/row; append +48% → +42%) but their bitmap keeps its size: at ≥6% density a run-optimized Roaring bitmap is still ~126 KiB per million rows, and the hex encoding plus the duplicated IPC schema still multiply it by 4 on disk, which is also where the remaining ~4 ms per file of finish() goes. Shrinking that needs an encoding change, which this PR deliberately does not make.
  • Utf8 ~2 KiB, 50% clustered keeps 205 KiB because that seed's bulk is the two 2 KiB extrema per zone, not the bitmap.

Raw results: nrnew/results.jsonl (after) and full1/nb1 (before) from the #9792 runs; happy to attach them if useful.

@LuQQiu
LuQQiu marked this pull request as ready for review October 10, 2026 02:55
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 10, 2026
`ZoneMapSeedWriter::record_null_runs` treated a chunk with no validity
buffer as all null, which inverts Arrow's rule and the behavior of the
per-row loop it replaced. The branch is unreachable from `observe_batch`
today because the caller only enters on a positive physical null count,
but the method is callable on its own, so return early instead and cover
it in the null-runs test.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@LuQQiu

LuQQiu commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

Review fix pushed as a follow-up commit (no history rewrite).

record_null_runs had its no-validity-buffer branch inverted: when chunk.nulls() returned None it inserted every row of the chunk as null, with a comment claiming the opposite of Arrow's rule (no validity buffer means no nulls). The branch was unreachable from observe_batch, which only calls it when null_count() > 0, and in arrow-array 58.4 that count is derived from the physical buffer, so a positive count implies the buffer exists. The per-row loop this PR replaced used the physical is_null, which records nothing in that case. The branch now returns early with no inserts, matching the old behavior, and the test asserts that calling record_null_runs directly on a chunk without a validity buffer leaves the bitmap empty.

No behavior change for any path reachable today; the format and the measurements above are unaffected.

@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 10, 2026

@lance-gatekeeper lance-gatekeeper Bot 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.

✅ Gate recommendation: approve.

The author’s follow-up corrects the helper’s no-validity-buffer case and adds direct regression coverage. The caller already excludes that case, so the measured write path is unchanged.

The null-heavy write optimization remains worthwhile and preserves exact positions across slices and zone boundaries, with the existing seed format and reader compatibility. No blocking issues found.

@zhangyue19921010 zhangyue19921010 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM 👍 Verified locally.

  • A randomized property test covering unaligned slices, cross-container runs, and various rows_per_zone values showed the seed's null bitmap read back exactly as written.
  • Performance is as expected: for all-null columns, observe_batch dropped from 11.2 to 1.1 ns/row and the seed from ~518 KiB to ~6.6 KiB; columns with no nulls are essentially unchanged.

One minor, non-blocking observation: when nulls are scattered one row at a time (e.g. every other row), insert_range doesn't reduce the number of calls and costs more per call than insert, so it was slightly slower in my local run (8.56 → 11.80 ns/row). That pattern is uncommon, so this can be a follow-up.

@LuQQiu

LuQQiu commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

Thank you @zhangyue19921010 for reviewing the PR!

@LuQQiu
LuQQiu merged commit 0201394 into lance-format:main Oct 10, 2026
LuQQiu added a commit that referenced this pull request Oct 12, 2026
…tistics (#9874)

Stacked on #9860. Until that PR merges, GitHub also shows its two
commits; this PR's own change is the top commit `84d629c46`. Part of
ENT-2893.

## Why this change?

For nested columns such as `List`, `FixedSizeList`, `Struct` and `Map`,
ZoneMap currently records **whether the whole row is null**, not min/max
values inside the collection.

For example:

| Row value | Counts as a null row? |
|---|---|
| `NULL` | Yes |
| `[]` | No |
| `[NULL, 1]` | No |
| `[NaN, 1.0]` | No |

However, the old implementation still visited the elements inside lists
to calculate statistics that ZoneMap then threw away. A vector with 768
elements could therefore require inspecting those elements just to
record that the vector itself was not null.

This unnecessary work made writing seeds very expensive for list and
vector columns.

## What changes?

`ZoneMapProcessor` now uses two paths:

- **Scalar columns:** keep the existing min/max, null-count and
NaN-count calculation.
- **Nested columns:** count null rows from the parent validity buffer
without visiting child values.

The same processor is used when building a ZoneMap index, updating it by
scanning new data, and collecting write seeds. All three paths avoid the
unnecessary child traversal.

For nested columns, the output stays the same:

- Exact parent-level `null_count`.
- `min` and `max` remain typed NULLs: value bounds are unavailable.
- `nan_count` remains 0; NaNs inside collections are not counted.
- Exact null-row positions are still recorded separately.

**This PR does not enable seeds by default, add nested min/max support,
change zone sizes, or change seed/index formats.** It also does not
change the shared `StatisticsAccumulator` used by other consumers.

## Performance: before vs. after

These measurements compare this PR against #9860's head, so they isolate
the benefit of skipping child statistics. “Seeds on” means appending to
a table whose ZoneMap index has `use_seeds = true`.

### Total append time

Lower is better. Each cell is the median of five runs. Null cases have
10% random **parent** nulls.

| Column | Rows appended | Seeds OFF, after change | Seeds ON, before
change | Seeds ON, after change | Extra append time from seeds, after
change |
|---|---:|---:|---:|---:|---:|
| FixedSizeList: 768 Float32 values per row | 1M | 1,468 ms | 4,574 ms |
1,506 ms | +2.6% |
| List<Int32> | 4M | 209 ms | 5,602 ms | 211 ms | +0.9% |
| Struct | 4M | 398 ms | 398 ms | 400 ms | +0.5% |
| FixedSizeList: 768 Float32 values, 10% null rows | 1M | 1,694 ms |
4,552 ms | 1,614 ms | Within measurement noise |
| List<Int32>, 10% null rows | 4M | 275 ms | 5,129 ms | 309 ms | +12.3%
|

The last column compares seeds ON vs. OFF **in the after-change build**.
The lower time for the null-vector case is measurement variation, not
evidence that seeds speed up writing.

The main improvement is in List and FixedSizeList:

- **1M vectors:** append with seeds falls from about **4.6 seconds to
1.5 seconds**.
- **4M lists:** append with seeds falls from about **5.6 seconds to 0.21
seconds**.
- **Struct:** essentially unchanged; its old statistics path was already
cheap.
- **Random nulls still have a cost:** exact null-position tracking and
bitmap serialization remain. For the nullable list case, that is about
**34 ms extra per 4M rows**.

### Seed collection alone

This measures only the seed writer's batch-observation step, not the
full append or final serialization:

| Column | Before | After |
|---|---:|---:|
| FixedSizeList<Float32, 768>, no null rows | 3,073 ns/row | 0.4 ns/row
|
| List<Int32>, no null rows | 1,277 ns/row | 0.1 ns/row |
| FixedSizeList<Float32, 768>, 10% null rows | 2,810 ns/row | 4.0 ns/row
|
| List<Int32>, 10% null rows | 1,170 ns/row | 3.7 ns/row |

**The optimization reduces CPU work, not seed size.** All 70 seed
outputs from the benchmark were byte-identical before and after, checked
by SHA-256 and decoded-statistics comparisons.

Measurement setup: before = `ef18a0aa8`, after = `84d629c46`; EPYC 9V65
machine with NVMe RAID0; `release-with-debug`; fresh process per
case/mode/repetition; identical inputs; five repetitions. The one-off
harness is on fork branch `lu/ent2893-zonemap-seed-bench-tool` and is
not included in this PR.

## Tests

- **Parent nulls vs. child nulls:** List, LargeList, FixedSizeList,
Struct and Map count only null rows; empty collections and null/NaN
children do not inflate the count.
- **Validation and reset:** reject mismatched input types, accumulate
across chunks, and reset between zones.
- **Seed round trip:** batching and slicing across zone boundaries
produce identical seed bytes, zone counts and exact null positions.
- **Index training:** a List column returns exact `IS NULL` results and
does not prune using unavailable value bounds.
- **Append + index update:** a FixedSizeList column is indexed, appended
to, and optimized; `IS NULL` and `IS NOT NULL` results match an
index-disabled scan.

## Verification

Reported local checks:

```text
cargo fmt --all -- --check
cargo clippy --all --tests --benches -- -D warnings
cargo test -p lance-index --lib zonemap
cargo test -p lance --lib zone_map
```

Python `test_vector_and_zonemap_on_fsl_column` was not run locally.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-index Vector index, linalg, tokenizer K-approved Latest Gatekeeper recommendation permits acceptance. performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants