Skip to content

fix(query): seed selective object VALUES before star payload joins - #1824

Merged
bplatz merged 3 commits into
mainfrom
fix/values-object-star-seeding
Sep 14, 2026
Merged

bplatz merged 3 commits into
mainfrom
fix/values-object-star-seeding

Conversation

@bplatz

@bplatz bplatz commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Two multi-row VALUES clauses binding the endpoints of an edge star were applied after the triple joins, scanning the predicate extent and materializing snapshot payloads before reducing the result to two edges. This change seeds one small reference table, probes its matching predicate, and joins the other VALUES as soon as their variables are available, before unconstrained payload columns.

The optimization requires at most 64 fully bound reference rows and a predicted reduction greater than 16× over the smallest predicate scan. It retains real VALUES joins to preserve duplicates, UNDEF, and multi-column correlations. Constant-object anchors, existing subject seeds, history, and multi-graph default unions retain their existing paths; existing fused stars keep their fusion.

Adds physical-plan and scan-fuel regression coverage on indexed and novelty data, semantic and admission tests, documentation, and a registered benchmark with singleton and broad-set controls. The regression test fails against the original planner on excessive scan fuel. On 129,000 synthetic indexed edges, the optimized in-process benchmark improved from 139 ms to 0.121 ms (approximately 1,150×), matching the FILTER IN workaround. Repeated optimized control runs showed no reproducible slowdown.

Validation on the perf/wal-group-commit base:

  • cargo fmt --all -- --check
  • cargo test -p fluree-db-query --lib object_values_seed — 3 passed
  • cargo test -p fluree-db-api --test it_values_object_bounds — 10 passed
  • cargo test -p fluree-bench-support --test workspace_reconcile — 1 passed

The benchmark is query_hot_values_star; its medium scale reproduces the report's approximate corpus size.

Follow-up: #1834 (repeated-object-var stars fall to the generic planner, which can fetch a functional payload before an existence check)

Two multi-row VALUES tables on edge endpoints were applied after the
triple joins, scanning and materializing the predicate extent before
filtering to a handful of edges.

Seed one small, fully bound reference table when predicate NDV predicts
a substantial reduction, then join other VALUES as their variables become
available before unconstrained payload columns. Keep real VALUES joins
to preserve duplicates, UNDEF, and multi-column correlation. Preserve
constant-anchored fused stars, subject seeds, history, and default unions.

Add plan and fuel regression coverage on indexed and novelty data,
semantic and admission guards, documentation, and a benchmark with
singleton and broad-set controls. On 129k synthetic indexed edges, the
optimized benchmark improved from 139 ms to 0.121 ms.

Validation on perf/wal-group-commit:
- cargo fmt --all -- --check
- cargo test -p fluree-db-query --lib object_values_seed
- cargo test -p fluree-db-api --test it_values_object_bounds
- cargo test -p fluree-bench-support --test workspace_reconcile
@bplatz
bplatz requested review from aaj3f and zonotope September 12, 2026 13:11

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

@bplatz this is good and a nice fix/improvement. No particular notes from me. Full review below:


This repairs the report's cliff by extending the seeded-chain path the planner already has (a small object table is just a subject seed one hop over) instead of lowering VALUES to a filter, and it keeps the real ValuesOperator joins so nothing about duplicates, UNDEF or multi-column correlation changes. I rebuilt the base and ran the registered bench against it on a quiet machine: 143.6 ms → 119.7 µs at the report's 129k-edge scale (your 139 ms → 0.121 ms holds to the digit), ~26 ms → 63 µs at 24k, and the three control shapes are within noise of the base at both scales, so the heuristic isn't taxing the queries it declines. Eight extra semantic cases (self-loops, the same var in two tables, a repeated endpoint in a two-column table, absent seed rows, OPTIONAL around the second endpoint, a constant anchor keeping the fused star) all match a FILTER oracle on both storage paths. The only notes are that the admission constants and the history gate have no test behind them (mutating them leaves everything green), and that the payload-last sort doesn't count the seed var itself.

Adherence to repo commitments:

  • Patterns/abstractions: ✔ extends build_sequential_triple_chain and apply_values; keeps ValuesOperator; no parallel construct; subject-seed and singleton folds untouched.
  • Performance (speed first, memory second): ✔ planner hot path; admitted shape 400–1,200× faster, declined shapes at parity in the PR's own controls, base vs head. Bench registered and reconciled. No performance-degradation risk.
  • Testing: ✔ three unit + one broad integration test on both storage paths, all wired ([[test]] it_values_object_bounds) and seen by name; ⚠️ constants/temporal gates unpinned (one of four mutations caught).
  • Conventions: ✔ fix(query): subject with a full body; docs section added; cargo fmt --check clean; clippy -D warnings --all-targets on fluree-db-query/fluree-db-api clean (see verification). No CI ran on this PR (stacked base) — retarget to main before merge (it applies cleanly today).

Verified locally at branch HEAD 1b0fb373a: it_values_object_bounds 10 passed; fluree-db-query --lib 1564 passed; workspace_reconcile 1 passed; four gate mutations (one caught); eight-case oracle probe green; dev-fast bench head vs base at small ×2 and medium ×1; merge-tree onto origin/main clean, no #1821 dependency.

Approving so you can merge when ready, but maybe worth pinning the 16×/64-row contract in a unit test first — and retarget to main (or wait for #1821) so the workflow actually runs once.

/// substantial reduction. Seed ONE table: multiplying independent VALUES first
/// would turn two selective sets into a potentially large cartesian product.
const OBJECT_VALUES_SEED_MAX_ROWS: usize = 64;
const OBJECT_VALUES_SEED_MIN_GAIN: f64 = 16.0;

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.

fluree-db-query/src/execute/where_plan.rs:1074 — 🔵 optional / testing. The two tuning constants and the temporal gates have no teeth. Claude caught this by mutation: OBJECT_VALUES_SEED_MIN_GAIN 16 → 1, OBJECT_VALUES_SEED_MAX_ROWS 64 → 1000, and removing !planning.is_history() at :2299 each leave every unit and integration test green (the row-cap unit case builds MAX_ROWS + 1 rows, so it tracks whatever the constant is). Only the constant-anchor exclusion at :1109 is pinned. Since the doc section in performance.md states "16-fold" and "64 rows" as the contract, a unit case on each side of the threshold (work == baseline/16 ± 1 → None/Some), one with 64 vs 65 literal rows, and a plan-shape assertion under PlanningContext::history() would cost a few lines. Minor and non-blocking — but if you agree it's right, I'd rather see it in this PR than lost in the backlog.

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.

Addressed in 22a1654

let driver = triples.remove(ti);
// Test the other endpoints before fetching unrestrained
// payload columns. Stable sort preserves other ties.
triples.sort_by_key(|tp| {

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.

fluree-db-query/src/execute/where_plan.rs:2312 — 🔵 optional / perf. The sort key treats a triple as constrained if its object is a constant or belongs to a remaining VALUES table, but not if its object is the seed var itself. With VALUES ?x { A B } ?ev ns:snap ?snap ; ns:entity1 ?x ; ns:entity2 ?x (self-loop shape) the plan is seed → entity1 → snap → entity2, i.e. the 256-byte payload is fetched before the second endpoint is checked — correct (my probe returns the right two rows both storage paths), just not the "probe before payload" the PR is about. || tp.o.as_var() == Some(seed_var) in the closure (the seed's var is known at that point) closes it. I don't think this shape is common, so non-blocking, but it's a one-liner.

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.

Addressed in 22a1654

The self-loop shape never reaches this sort: the seed branch is gated on is_property_join, which requires distinct object vars, so ?ev ns:entity1 ?x ; ns:entity2 ?x is declined and planned by the generic path (where the payload and the second endpoint tie on estimated rows). The seed_var term would be unreachable, so instead I pinned the invariant in object_values_seed_excludes_repeated_object_vars and dropped the equally unreachable o_bound() term. Admitting repeated endpoints to the seeded path is possible, but it would change routing for a new shape family, so I'd rather keep it out of this PR.

compound patterns are rewrite boundaries because MINUS/EXISTS semantics can
change when a shared variable disappears before the retained VALUES binds it.

### Multi-row VALUES in stars

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.

docs/design/performance.md:238 — ⚪ nit. "when predicate statistics predict at least a 16-fold reduction over its smallest predicate scan" — worth adding that the comparison is rows × (1 + count/ndv) against min(estimate) / 16, i.e. duplicate seed rows are charged, since that is the number a user tuning a report would need.

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.

Addressed in 22a1654

&hash_planner,
));

// Retain the real VALUES join, including duplicates and wildcard cells.

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.

fluree-db-query/src/execute/where_plan.rs:3367 — praise. Joining a pending table only once all its vars are bound, and adding pending vars to the live set at :3345, is exactly what keeps the two-column correlated table and the projection-pruning case honest; both survive my probes and the PR's own.

"",
),
(
"singleton_anchor",

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.

fluree-db-api/benches/query_hot_values_star.rs:87 — praise. The singleton_anchor and broad_values controls are the right ones to ship with a planner heuristic — they are what let me say the non-admitted shapes are at parity (43.4 vs 43.3–44.0 µs, 16.2–16.8 vs 16.2–16.3 ms) rather than guess.

Base automatically changed from perf/wal-group-commit to main September 14, 2026 18:20
Boundary cases for the 64-row cap and the 16-fold gain threshold, and a
plan-shape check that history planning keeps the unseeded plan. Document
the admission formula, including that duplicate rows are charged.

Drop the constant-object term from the payload-last sort: the seed already
declines any star with a bound object. The seed var cannot recur in another
triple either, since property-join eligibility requires distinct object
vars; a test pins that invariant.
@bplatz
bplatz merged commit 411a449 into main Sep 14, 2026
23 of 24 checks passed
@bplatz
bplatz deleted the fix/values-object-star-seeding branch September 14, 2026 19:19
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.

2 participants