Skip to content

Iceberg: a table's row count comes from its manifest list when the summary has none - #2198

Merged
aaj3f merged 13 commits into
mainfrom
fix/iceberg-row-counts-from-manifests
Nov 2, 2026
Merged

aaj3f merged 13 commits into
mainfrom
fix/iceberg-row-counts-from-manifests

Conversation

@aaj3f

@aaj3f aaj3f commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

BigQuery writes Iceberg v2 snapshots whose summary is just {"operation": "overwrite"}. total-records, total-data-files and total-files-size are all optional in the spec, and BigQuery leaves every one of them out. Virtual /info took a table's count from total-records alone, so a graph source over a BigQuery table came back with table-row-counts: {} and every class and property count null. The Schema-tier preview had the same blind spot, so a table preview showed no rows or files. We found this driving a BigQuery lake end to end: the graph source queried fine (2,000,000 rows, typed values), but every count around it was unknown.

This takes the counts from the snapshot's manifest list when the summary doesn't have them. The live rows are added_rows_count + existing_rows_count summed over the data manifests (content = 0). A delete manifest in the list puts the table under mor-approximate-tables as an upper bound, the same as the summary's delete counters already do. The summary still wins when it has the total, with no extra read. The derivation is fluree_db_iceberg::ManifestListTotals plus stats::send_read_manifest_list_totals, so other callers can reuse it, and /info and Fluree::preview_iceberg_table share one cached_manifest_list_totals helper.

Perf, since /info is something the UI polls. A manifest list never changes once written, so its totals are cached by the list's location (bounded at 4,096 entries, no TTL), and a warm /info builds no storage and reads nothing. A failed read caches nothing and leaves the count null, as before. I measured on a release build with a unit-level microbenchmark: a warm per-table count went from ~360 ns (summary only) to ~445 ns (cache hit), and the cold path is ~21.5 µs plus the one read. That's noise next to the loadTable round trip /info already makes per table per call, but it seemed worth stating rather than asserting.

One parser fix rode along. The manifest-list parser only read the old Java counter names (added_data_files_count, …), while pyiceberg and BigQuery write the spec's added_files_count. So for them it reported 0 data files. It now reads both spellings, unwraps v1's ["null", T] unions, and leaves an absent counter None rather than 0. I checked whether anything else consumed that wrong 0 — scan planning, pruning, the COUNT shortcut, empty-table handling — and nothing did; they all work from manifest entries or partition summaries. So it was wrong but harmless.

Merge-on-read, since a BigQuery summary carries no delete counters. The obvious worry is that a summary-only delete guard can't see deletes on these tables, so a query would quietly return deleted rows. That isn't the case. The scan planner's second guard reads the manifest list it plans from and refuses a content = 1 entry before reading any manifest. Nothing pinned that arm on the production SendScanPlanner, though, so 76a275b adds the tests. The planner tests use the real BigQuery table: its list plans 4 files and 2,000,000 rows, and the same list plus a delete manifest is refused by plan_scan and plan_incremental. The local-fs test, using BigQuery's summary over a list with a delete manifest, refuses the query, a COUNT(*) and a materialize read. With that arm turned off, the same query returns all the rows, so the test isn't vacuous. The Schema tier is display-only, so a delete-bearing list now reports its totals as upper bounds (has_delete_files, plus the warning) rather than refusing; refusing would have broken the preview for exactly the tables this reads the list for. A summary that reports deletes still refuses the preview, as before.

The fixtures under fluree-db-iceberg/tests/fixtures/bigquery/ are the BigQuery table's metadata, manifest list and manifest, copied read-only from a test bucket (synthetic data).

After a self-review pass (adversarial, on our side before asking for review), five more commits went in:

  • a5e163821: the manifest list's content field now fails closed. It's read through its ["null","int"] union, absent/null is the spec's default (data), and anything else (1, an unknown int, a non-int) reads as deletes, so the existing guards refuse it with their typed MergeOnReadDeletes. The old test pinned 99 → Data, which is exactly the wrong way round for an input the merge-on-read guard trusts.
  • 71c8ff592: a moto test that runs the real storage closures (catalog_read_storage, build_preview_storage) over a Glue table whose summaries are all reduced to {"operation": "overwrite"}: /info and the Schema preview both get its 50 rows. Every other new test injects storage, so this is the one that covers the real path.
  • ad7cf877e: a seam marker on the totals cache (see the Iceberg on Google: a Google identity for REST catalogs, a per-source storage identity with a GCS reader, and one codec for every surface #2196 note below).
  • 9ecbf9aa3: /info's cold-path time box was one all-or-nothing timeout over the whole fan-out, so a slow manifest-list read on one table dropped every table's count, including ones whose summary had it. It's now one deadline applied per step (catalog client, each table's load, each table's list read), so an overrun loses only that table's count; a cache hit is still served without I/O.
  • d647490e7: the Schema-tier preview no longer refuses a snapshot whose summary reports deletes either. Before this, the same merge-on-read table previewed when BigQuery wrote it (deletes visible only in the manifest list) and 409'd when Spark/Java wrote it (deletes in the summary). Both now return the totals as upper bounds with has_delete_files and the warning. This is a 409 → 200 change on POST /v1/fluree/iceberg/catalog/preview with the Schema tier (the default), and the same in Fluree::preview_iceberg_table and the free preview_iceberg_table. The Stats tier and every query read still refuse.

One API note: ManifestListEntry's counter fields are renamed and now Option (an absent counter is unknown rather than 0), and total_data_files() is gone. Nothing outside the parser used them.

Still out of scope, both pre-existing: a query client gets the merge-on-read refusal as a 500 err:system/InternalError rather than a 409 (storage_query_error flattens MergeOnReadDeletes, and #2196 reworks those same tables, so I'd rather do it after that lands); and entry-level data_file.content is still not parsed (a delete file listed under a manifest labelled as data would read as data, which a conformant v2 writer can't produce).

Merge order with #2196: I'd land #2196 first. There's a seam: this keys its cache by list location alone, like main's other caches, so whichever of the two merges second needs to key it by (StorageScope, location) and build through fresh_storage. That's already done on beta/bigquery-gcs (the scoping went in at 3c6c12aac, carried to 98b228d74), with a test showing a warm /info on a BigLake inline-metadata load makes zero GCS reads and zero token requests.

Claude Code did most of the legwork here, including the fixture capture, the counterfactual and mutation checks, and the benchmark. Happy to talk through any of it.

aaj3f added 4 commits October 27, 2026 16:40
…he summary has none

BigQuery writes Iceberg v2 snapshots whose summary is only
{"operation": "overwrite"}; total-records, total-data-files and
total-files-size are optional in the spec. Virtual /info took the count
from total-records alone, so such a table had no table-row-counts entry
and every class and property count was null.

/info now falls back to the snapshot's manifest list: the live rows are
added_rows_count + existing_rows_count summed over its data manifests,
and a delete manifest in it lists the table under mor-approximate-tables
(an upper bound), as the summary's delete counters already do. The
summary is still used when it has the total, with no extra read. A
manifest list never changes once written, so its totals are cached by
its location (bounded, 4096 entries): a warm /info builds no storage and
reads nothing, and a failed read caches nothing. The derivation is
fluree_db_iceberg::ManifestListTotals and
stats::send_read_manifest_list_totals, for other callers to reuse.

The manifest-list parser read only the pre-rename Java counter names
(added_data_files_count etc.), so for pyiceberg and BigQuery, which
write the spec's added_files_count, it reported 0 data files. It now
reads both spellings, unwraps v1's ["null", T] unions, and leaves an
absent counter None rather than 0.

The fixture is the BigQuery table's metadata, manifest list and
manifest, copied read-only (synthetic data, 2,000,000 rows).
…s from the manifest list

preview_iceberg_table's Schema tier filled row_count and data_file_count
from the snapshot summary alone, so a BigQuery table (summary
{"operation": "overwrite"}) previewed with neither. When the summary
lacks either total, the tier now reads the snapshot's manifest list
(never a manifest) through the same helper and cache as virtual /info,
so Fluree::preview_iceberg_table reads a snapshot's list once. The free
preview_iceberg_table has no cache and reads it on each call.

Best-effort: a list that cannot be read leaves the fields None and the
preview succeeds. A list that shows delete manifests is refused unless
FLUREE_ICEBERG_ALLOW_MOR_DELETES is set, as the Stats tier's manifest
backstop and the summary guard refuse the same snapshot; under the
override the counts are filled, has_delete_files is set and the
upper-bound warning added. total_bytes stays summary-only on this tier:
the manifest list does not carry it.

cached_manifest_list_totals is the shared helper; /info's resolver now
goes through it.
…efuses every read

BigQuery's snapshot summary is only {"operation": "overwrite"}, with no
delete counters for the zero-I/O summary guard. The scan planner's
second guard already reads the manifest list it plans from and refuses a
content = 1 entry, before reading any manifest; nothing pinned that arm
on the production SendScanPlanner, whose tests used summary counters.

The planner tests take the real BigQuery table: its list plans 4 files
and 2,000,000 rows, and the same list plus a delete manifest is refused
by plan_scan and by plan_incremental (from genesis and from a snapshot)
with MergeOnReadDeletes, reading the list and nothing else. The local-fs
test stages the people fixture with BigQuery's summary and a delete
manifest in its list, and refuses the query, a COUNT(*) and a
materialize read; the same summary over the original list reads its
five rows.
… list as upper bounds

The Schema tier refused (409) a snapshot whose manifest list showed
delete manifests unless FLUREE_ICEBERG_ALLOW_MOR_DELETES was set. The
tier is display-only, and before it read the list the Schema view of
such a BigQuery table worked (its summary has no delete counters to
trip the summary guard), so the refusal broke the Connect schema view
for exactly the tables the list is read for.

It now fills the totals, sets stats_completeness.has_delete_files and
adds the upper-bound warning, with or without the override, as /info
lists such a table under mor-approximate-tables. A query of the
snapshot is still refused by the scan planner, which reads the same
list. The summary guard is unchanged: a summary that reports deletes
still refuses the preview without the override.
@aaj3f aaj3f added bug Something isn't working as expected area:iceberg Iceberg catalogs/credentials, R2RML, virtual datasets, materialize labels Oct 27, 2026
aaj3f added 6 commits October 28, 2026 05:55
`content` (field 517) is what the merge-on-read guards read to tell a
delete manifest from a data manifest, and its parse failed open: only a
plain `int` was read, so a `["null", "int"]` union read as data even
when it held 1, and ManifestContent::from_avro mapped every value but 1
to data. Either way a manifest the reader could not classify was
planned as data, past the guards.

The value is now read through the union, as the counters are. Absent
or null is the spec's default, 0 (v1 lists have no such field, and v1
has no delete files); 0 is data; 1 and anything else, including a
non-`int`, is deletes, so the guards refuse the snapshot with their
typed MergeOnReadDeletes error rather than read it. The planner test
pins that an unknown value (2) is refused like a delete manifest.
Every /info and Schema-tier test injected storage, so the real builders
(catalog_read_storage, and the Schema tier's build_preview_storage)
never ran under test. The moto suite now registers a Glue table,
customers_summaryless, over a copy of customers' metadata with every
snapshot summary reduced to BigQuery's {"operation": "overwrite"}, and
the same manifest list and data. It reads 50 rows; /info reports 50
from the manifest list through catalog_read_storage; and the Schema
preview reports 50 rows and 1 data file through build_preview_storage,
the totals the original summary recorded.
…lands

The totals come from an object behind the storage gate, so with
per-source storage identities the cache must be keyed by
(StorageScope, location), like table_metadata and scan_files. That key
is the one part of the #2196 seam the compiler does not enforce in
either merge order, so the field says so.
The virtual /info row counts ran under one timeout over the whole
fan-out, so a cold /info whose manifest-list reads pushed past the 10 s
budget dropped every table's count, including tables whose summary
already had one.

The budget is now one deadline applied to each step: building the
catalog client, each table's loadTable and metadata, and each table's
manifest-list read. A step still running at the deadline omits only its
table's count; a count from the summary never waits on anything after
its table's load. Every step ends by the deadline, so the fetch does
too. A warm /info is unchanged: a cache hit completes on its first poll,
which timeout_at makes before it checks the deadline, so it is served
even past it and still reads nothing.
…as upper bounds

The Schema tier reported a delete-bearing manifest list as upper bounds,
but a snapshot whose summary reports deletes still refused it (409),
because the summary guard ran for every tier before the tier split. So
whether a merge-on-read table previewed depended on its writer: a
BigQuery snapshot (no summary delete counters) previewed, a Spark or
Java one did not.

The tier is display-only, and a flagged upper bound already meets
F-AUD-1's "no silent over-count", so it now refuses neither: either
signal sets stats_completeness.has_delete_files and adds the upper-bound
warning. The summary guard runs for the Stats tier only, which still
refuses (409) unless FLUREE_ICEBERG_ALLOW_MOR_DELETES is set, and every
query read is still refused by the scan planner.

Behavior change: POST /v1/fluree/iceberg/catalog/preview with tier
"schema" (the default) now answers 200, with has_delete_files true and
the upper-bound warning, for a snapshot whose summary reports position
or equality deletes, where it answered 409. Fluree::preview_iceberg_table
and the free preview_iceberg_table change the same way. The "stats" tier
is unchanged. The CLI has no Iceberg preview command, so nothing there
changes.
… deletes

StatsTier::Schema is display-only: it never refuses a merge-on-read
snapshot, and delete files its summary or manifest list reports make its
counts upper bounds (has_delete_files plus a warning). StatsTier::Stats
answers 409 unless FLUREE_ICEBERG_ALLOW_MOR_DELETES is set. The
/v1/fluree/iceberg/catalog/preview request doc's `tier` field says the
same. Doc comments only; no behavior change.
@aaj3f
aaj3f marked this pull request as ready for review October 28, 2026 10:53
@aaj3f
aaj3f requested a review from bplatz October 28, 2026 18:42

@bplatz bplatz 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.

Approving, with feedback inline: the main one is making sure this cache gets its storage scope when it meets #2196, plus a small nit.

Checked and sound:

  • Live totals are ADDED + EXISTING over the data manifests, per the spec; a missing, negative or overflowing counter makes the total unknown rather than a confident smaller number, and a delete manifest makes it an upper bound.
  • content failing closed is safe: every production caller parses with deletes included and guards on them. Only the example uses the filtering parse.
  • The parser reads both counter spellings and unwraps v1's ["null", T] unions.
  • The per-step deadline still bounds the whole fan-out (queued tables time out at once), and an overrun now costs only its own table's count.
  • A BigQuery-style summary over a delete-bearing list is still refused on every read path, pinned by the planner tests on the real BigQuery list with a counterfactual.

/// `scan_files`. The compiler will not catch a resolution that keeps this
/// key, because #2196 scopes every cache in its accessor.
#[cfg(feature = "iceberg")]
manifest_list_totals: SyncCache<String, ManifestListTotals>,

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.

Agree with landing #2196 first. Since the compiler won't catch a merge that keeps this key, I'd make the seam enforce itself: when this rebases onto #2196, key it by (StorageScope, location) as beta/bigquery-gcs already does, and add a test that two storage scopes don't share an entry (a warm entry for one scope must miss for the other). If this one lands first instead, a checklist line in #2196's body would keep the re-key from getting lost in that merge.

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.

Agree on making the seam enforce itself. The re-key is ready on a local branch merged with #2196's head: the count cache keyed by scope.qualify(location) as the beta has it, plus virtual_count_cached_under_one_scope_is_not_served_to_another (a warm entry for one scope must miss for the other; dropping the scope from the key fails it). It goes up here as soon as #2196 merges, and #2196's body now carries a checklist line so it can't get lost in the merge.

Comment thread fluree-db-api/src/ledger_info.rs Outdated
Comment on lines +1890 to +1896
let read = crate::graph_source::cached_manifest_list_totals(
Some(cache),
table,
snapshot,
open_storage,
);
match tokio::time::timeout_at(deadline, read).await {

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.

Nit: serving a cache hit past the deadline depends on Timeout polling the inner future before it checks the deadline. That's tokio's behavior today, but doing the cache lookup before the timeout_at (and only timing the storage build and the read) would say the same thing without relying on it.

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.

Done in 63e029725: the warm lookup is a separate no-I/O cached_manifest_list_totals_hit before the timeout_at, and only the storage build and the read are timed. Fwiw no test can tell the old and new code apart, since tokio's Timeout does poll the inner future first today, but virtual_count_cache_hit_is_served_past_the_deadline keeps pinning the behavior, and now it's our code that guarantees it.

aaj3f added 2 commits October 30, 2026 08:43
…t starts

Serving a manifest-list totals cache hit past the /info deadline relied
on tokio's `Timeout` polling its inner future before checking the
deadline. The lookup now runs before `timeout_at`, and only the miss
path (building the storage and reading the list) is timed. The shared
helper is split into its cache half and its read half so the two
callers keep one implementation; the Schema-tier preview still uses the
combined form. No behavior change.
The fixture's paths named the bucket it was copied from. Every path now
names `gs://example-bucket/`: in the metadata JSON, and inside both Avro
files, whose deflate blocks are re-encoded with the new string lengths.
The manifest list's `manifest_length` is updated to the rewritten
manifest's size. Rows, file counts, snapshot ids and the schema are
unchanged, and every test reads its paths from the fixture itself.
…s-from-manifests

Brings in per-source storage identities (#2196) and the graph-source
crawl fixes (#2199).

The manifest-list totals cache is keyed by the manifest-list location
qualified by the `StorageScope` of the storage plan that read it, as
`table_metadata` and `scan_files` already are, so a total read under one
identity is never served to another.

- cache.rs: the totals accessors take the scope and key by
  `scope.qualify(location)`.
- r2rml.rs: the three totals helpers take the scope; the /info read
  decides its storage with `catalog_read_plan` and builds it with
  `catalog_read_storage(&plan, &grant)`, the query path's decision.
- iceberg_catalog.rs: `preview_iceberg_table_in` takes the identity
  context and the cache; `preview_storage_plan` is split out of
  `build_preview_storage` so the Schema-tier preview keys its totals
  by the plan's scope before building anything. The Stats-tier callers
  in iceberg_generate.rs and iceberg_validate.rs pass no cache.
- ledger_info.rs: the /info count takes a plan closure. The deadline
  bounds the plan as well as the read, because planning a Google
  identity resolves its secret and proves the identity, either of
  which can be a remote call. The plan is polled once before the
  deadline is consulted, so a plan that is ready at once (ambient,
  vended, or a warm Google identity behind a resolver that answers at
  once) still serves a warm count past the deadline.
- it_iceberg_local_fs.rs: both sides' tests are kept.
@aaj3f
aaj3f merged commit fc85ebf into main Nov 2, 2026
19 checks passed
@aaj3f
aaj3f deleted the fix/iceberg-row-counts-from-manifests branch November 2, 2026 21:14
aaj3f added a commit that referenced this pull request Nov 3, 2026
…out-mapping

Brings in #2193, #2200, #2183, #2187 and #2198. The one conflict is
it_iceberg_local_fs.rs, where this branch's unmapped-source tests and
#2198's BigQuery and merge-on-read tests were appended at the same
place; both are kept.
bplatz added a commit that referenced this pull request Nov 4, 2026
Brings #2247, #2232, #2246, #2248, #2245, #2230, #2205, #2231, #2255,
#2219, #2209, #2207, #2190, #2216, #2186, #2215, #2213, #2184, #2210,
#2206, #2198, #2187, #2183, #2200, #2193, #2199, #2196 and #2145 into next.

Resolutions:
- Novelty: main's fact-state trim (a cutoff plus a bounded sweep) and the
  dictionary's coverage watermark sit on next's graph slots and per-graph
  OrdMaps. A commit's reification links count toward coverage. Planner
  stats from segment sums decline under annotations, whose link deltas
  only the walk counts, and otherwise carry the indexed link counts.
- Stats-view identity: the index root's id reaches the snapshot through
  next's root decoder (`decode` takes the id, as `take_snapshot_metadata`
  does). `LedgerState::load_record` has a decoding-root form beside
  `load_decoding_root`.
- Transact: #2184's parallel-relationship check runs in next's link
  cascade, counting the `rdf:reifies` links on the retracted edge that the
  transaction does not retract itself. Main's bundle `edge_reifiers` is
  dropped, and `subjects_with_prior_rows` and
  `enforce_single_target_reifiers` stay deleted. The upsert pre-check
  takes main's dictionary probe and bounded seek.
- Query: geo search resolves the graph's POST tree once at open (pooled
  graphs keep their filter), an empty manifest when there is none.
  `branch_or_none` reads the dedicated branch, and the batched joins keep
  next's pooled lanes. The stats-view key hashes the root id.
- Indexer: `apply_dict_novelty` and `compact_forward_streams` take the
  cycle's compaction budget, so the term dictionary keeps its reserved
  share. `Prefetched` holds `ContentBytes`. `relocalize_sorted_commit`
  rewrites next's 20-byte types records.
- Disk cache: main's in-flight write registration, with an empty entry
  still read as crash debris that a write replaces. The process-wide
  eviction walk stays removed.
- Graph import (#2232): fenced CAS and publishes, the seed carries the
  record's namespace and fence, 32-bit graph ids and next's loaders. An
  import into a graph of an existing ledger refuses triple terms and edge
  annotations: the splice merges no triple-term dictionary yet.
- Iceberg: `create_iceberg_graph_source` goes with main (a source needs a
  mapping); the R2RML path keeps next's `model_instance`.
- CLI: main's shared import flags without the leaflet flags; `graph
  import` routes through next's process-wide server routing, and
  `upload_source_import` serves next's `Destination`. Doc sources read a
  ledger through the command's open instance.
- Tests: sibling ledger ids become distinct names, `genesis_ledger` is
  awaited, Cypher state dumps include each relationship's `rdf:reifies`
  link, and the graph-import arena test checks the refusal instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:iceberg Iceberg catalogs/credentials, R2RML, virtual datasets, materialize bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants