Skip to content

perf(r2rml): COUNT(*) manifest shortcut — provider table_row_count (PR-1) - #1478

Closed
aaj3f wants to merge 7 commits into
perf/r2rml-pr0-correctnessfrom
perf/r2rml-pr1-count-manifest
Closed

aaj3f wants to merge 7 commits into
perf/r2rml-pr0-correctnessfrom
perf/r2rml-pr1-count-manifest

Conversation

@aaj3f

@aaj3f aaj3f commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Roadmap PR-1 (docs/audit/2026-07-virtual-dataset-perf/ROADMAP.md). A bare, unfiltered COUNT(*) over one class of a virtual (R2RML/Iceberg) dataset now answers from the Iceberg manifest record_count sum instead of decoding every Parquet data file. On the SF01 corpus this turns the three fact COUNTs from DNF@180s into sub-5s exact counts:

query class before after (cold) count
q036 edw:Order DNF@180s 4.43s 180,000
q037 edw:WebEvent DNF@180s 3.12s 1,000,000
q039 edw:GlJournalEntry DNF@180s 2.76s 250,000

Query-side detection shipped in the parent commit (271c8b7a6, behavior-neutral until this provider impl); this commit is the provider side.

Soundness contract

table_row_count returns Some(n) only when the manifest count provably equals a full-scan COUNT(*) (pure sound_manifest_row_count, unit-tested without I/O):

  1. No delete manifests — a merge-on-read positional/equality delete would make the sum an over-count → decline.
  2. Every subject/object non-null column provably zero-null — aggregate_column_stats' coverage gate yields Some(0) only when every data file reported the stat and they sum to zero; absent/partial stats are unknown ⇒ decline (never treated as zero); unknown column or would-be-negative total likewise declines.
  3. Empty non_null_cols = constant-subject mapping ⇒ sound with no null proof.

Any decline ⇒ Ok(None) ⇒ the caller runs today's delete/null-correct scan. Trait default is Ok(None), so non-Iceberg providers always scan. Kill switch: existing FLUREE_FUSED_R2RML_AGG (off ⇒ full-scan path, verified live).

Extraction + snapshot-pin preservation

The loadTable front-half of scan_table_inner is extracted into load_table_context, shared by scan and count. The per-query snapshot pin (IcebergCatalogSession::cached_load_table / pinned_metadata_location / store_load_table, via the query-scoped self.session) moved verbatim, so a COUNT and a scan in one query read the same pinned metadata_location ⇒ one snapshot. Scan-only concerns (the scan-start log, the Parquet disk cache) stay in scan_table_inner — a COUNT emits no phantom scan log and builds no unused cache.

Gates (all six pass)

  1. fluree-db-query full suite — green.
  2. fluree-db-api --features iceberg full suite — green; 6 new count_shortcut_* tests (delete-manifest DoD centerpiece, nullable, stats-absent, unknown-column, constant-subject, clean-table exact).
  3. fluree-db-iceberg --lib --tests — green (180). Bare cargo test -p fluree-db-iceberg fails only on the pre-existing aws-gated examples/smoke_minio.rs compile issue (proven pre-existing by stash-to-base; suggest a tiny follow-up gating the example on #[cfg(feature="aws")]).
  4. Native corpus — 54/54 ok, 0 hash mismatches (three runs; byte-identical through the refactor). Perf "violations" proven environmental: the violator count tracks machine load (14 under a concurrent cargo test → 4 idle → 10 under another build) and the violator set shuffles run-to-run; native never enters this provider.
  5. Live virtual (SF01) — table above, result_hash == native oracle on all three; fused-scan sentinel q022 unaffected (took the scan path, shortcut correctly declined — files_selected=1 + scan span present); GROUP-BY negative control q014 unaffected. Fire-vs-decline proven by span counters (fired ⇒ files_selected=0 + no scan spans), stronger than log greps.
  6. Kill switch — FLUREE_FUSED_R2RML_AGG=0 restores the scan path on q036 (DNF vs 4.43s on).

Note

q036's residual 4.4s is entirely oauth_token (0.52s) + r2rml.load_table (3.52s): PR-1 reduces fact COUNT to the cold-catalog REST floor — exactly roadmap PR-8's target.

Stacks on #1476 (PR-0 correctness) ← #1475 (bench harness) ← #1450.

aaj3f added 2 commits July 11, 2026 17:41
…until provider impl)

R2rmlTableProvider gains table_row_count (default Ok(None) — opt-in), and
the fused aggregate answers a bare COUNT (one CountRows fold, no GROUP BY,
no FILTER) from it when the provider can prove manifest count == full scan.
The non-null proof columns are parsed from the subject template STRING
(template_columns is loader-only; a with_subject_template mapping would
otherwise pass the null guard vacuously). With no provider impl yet every
call falls back to today's scan — corpus and suites unchanged.
…R-1 provider side)

FlureeR2rmlProvider implements table_row_count: resolve the SAME per-query
pinned table context the scan uses (new load_table_context helper, shared
verbatim loadTable resolution incl. the IcebergCatalogSession snapshot pin),
read only the manifest-list/manifest Avro (never a data file), and return
the record_count sum when sound_manifest_row_count proves it equals a full
scan: no delete manifests, and every subject/object non-null column provably
zero-null (absent/partial stats are unknown, never zero; unknown column or
would-be-negative total declines). Constant-subject mappings need no null
proof. Declines fall back to the always-correct scan.

Scan-only concerns (scan-start log, Parquet disk cache) stay in
scan_table_inner — a COUNT emits no phantom scan log and builds no cache.

Live SF01: fact COUNTs q036/q037/q039 DNF@180s -> 2.8-4.4s with exact
counts hash-equal to the native oracle; the residual is entirely the cold
REST floor (oauth+loadTable). Fused-scan sentinel and GROUP-BY negative
control unaffected; FLUREE_FUSED_R2RML_AGG=0 restores the scan path.
Native corpus 54/54, 0 hash mismatches across three runs.
aaj3f added 3 commits July 13, 2026 18:59
cargo fmt --all --check fails on three hunks of this PR's own delta
(fluree-db-api/src/graph_source/r2rml.rs, fused_aggregate.rs import +
manifest-shortcut block) with rustfmt from stable 1.97.0 — what CI's
dtolnay/rust-toolchain@stable resolves. CI never ran here (ci.yml
triggers only on base=main), flagged by the #1476 review stack-wide
note.
…m, as-of breadcrumbs

The four optional items from the #1478 review (the blocking fmt item
was already fixed in f8f5206):

- table_row_count is instrumented as r2rml.count_manifest (the same
  .instrument split as scan_table), recording fired=answered|declined.
  Allowlisted in fluree-bench-virtual with a rename-guard citation, so
  the vbench pathway counters show the shortcut directly instead of
  inferring it from files_selected=0 + scan-span absence. The must-fire
  invariant becomes a group — scan_table OR count_manifest — because a
  fired COUNT(*) legitimately never scans; spans_missing would
  otherwise false-flag the very pathway PR-1 added.

- sound_manifest_row_count re-sums record_count with per-file checked
  u64 arithmetic instead of trusting agg.row_count: a corrupt manifest
  with a negative per-file count, or a total that overflows, declines
  instead of wrapping past the sign check in release builds (test:
  three i64::MAX files wrap i64 to a positive total the old guard
  would have served as an "exact" count). aggregate_column_stats'
  advisory totals now saturate instead of wrapping (or
  overflow-panicking in debug builds).

- GREP: r2rml-as-of-t breadcrumbs in table_row_count_inner and
  scan_table_inner so time-travel semantics cannot land on one without
  the other — a COUNT and a scan in one query must read one snapshot.

- ROADMAP PR-8 residual: the decline-path double manifest read (the
  shortcut reads manifests, then the fallback scan's planner re-reads
  them; a first cold COUNT on a delete-bearing table pays twice) folds
  into the catalog round-trip caching program.

Gates: fmt clean; scoped clippy adds no lints; fluree-bench-virtual
33/33 (incl. the allowlist rename guard); fluree-db-iceberg
--all-features green; fluree-db-api --lib --all-features 845 passed
(count_shortcut 7/7).
@aaj3f

aaj3f commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

Consolidating at maintainer request. #1478 (PR-1 — the COUNT(*) manifest shortcut) is carried forward in #1507 (the R2RML/Iceberg virtual-dataset performance program), which brings the #1475–#1507 lineage forward as a single reviewable unit. Its verification of record is the program's corpus benchmark, published as C2-bench-wave1.md (native == virtual, 0 hash mismatches). The pre-consolidation branch tip is preserved at tag archive/pre-refactor-2026-07-21/perf_r2rml-pr1-count-manifest — no history was rewritten. Closing in favor of #1507; discussion continues there.

@aaj3f aaj3f closed this Jul 21, 2026
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.

1 participant