Repository navigation
Description
Surfaced in review of #1824. That PR plans small object VALUES tables ahead of star payload joins, but only for stars that pass is_property_join. A star that repeats an object variable is left to the generic planner, which can fetch an unconstrained payload column before a cheaper existence check.
Shape
SELECT * WHERE {
VALUES ?x { ns:A ns:B }
?ev ns:snap ?snap ; # functional payload, ~1 value per subject
ns:entity1 ?x ;
ns:entity2 ?x . # self-loop: ?x repeats
}Plan today: VALUES ?x → entity1 → snap → entity2. Every row matched by entity1 pays for a payload lookup, even if entity2 then discards it. Results are correct.
This is a constant-factor cost (at most one extra lookup per candidate row), not a cliff. It depends on textual order: writing ns:entity2 before ns:snap already produces the good plan.
Cause
reorder_patterns places triples greedily. After VALUES ?x and entity1 bind ?x and ?ev, the two remaining candidates tie exactly:
?ev ns:snap ?snapisBoundSubject:count / ndv_subjects, clamped toHIGHLY_SELECTIVE = 1.0(planner.rs:207).?ev ns:entity2 ?xisExactMatch: alwaysHIGHLY_SELECTIVE = 1.0(planner.rs:176), though it can only keep or drop rows.
None of the tie-breaks in rank_seed_candidates (planner.rs:2097) separates them, so original position decides. A multi-valued payload does not trigger this, because its estimate exceeds 1.0 and the existence check already wins.
Candidate fix
Add a tie-break directly after the cardinality comparison. Among equal estimates, prefer a candidate that binds no new variables:
.then_with(|| {
let new = |p: &Pattern| p.produced_vars().iter().any(|v| !bound_vars.contains(v));
new(&remaining[i].pattern).cmp(&new(&remaining[j].pattern))
})This follows the same reasoning as the existing "unlock a pending FILTER" tie-break: a step that binds nothing new cannot grow the stream in current-state queries.
The change was prototyped locally but not committed:
- Plan order: the self-loop plan becomes
VALUES→entity1→entity2→snap. - Tests:
cargo test -p fluree-db-query --libpassed (1568) andcargo test -p fluree-db-api --testspassed (3485, 60 binaries). - Not run:
testsuite-sparql, any benchmark, and no history-mode-specific checks.
Regression risks — check each before merging
reorder_patterns orders every SPARQL, JSON-LD, and Cypher query. Estimates of exactly 1.0 are common because every functional bound-subject lookup and every ExactMatch clamps to that value. Passing tests are not sufficient evidence here.
- BSBM Explore Q5, which depends on tie-break 3 (unlocked deferred FILTER). Q5's candidate product probes all estimate about 1 row. The FILTER tie-break puts the
±kwindow probes ahead ofrdfs:label, which was about 25% of the query at 10M products. Those candidates all bind new variables, so the new key should compare equal and fall through. Confirm that the Q5 plan is unchanged and that its timing holds. - BSBM BI-1 bowtie, which depends on tie-break 4 (unlocked object→subject hash scan, 46× on BI-1's F2). The new key ranks above it, so an existence check now goes ahead of a candidate that unlocks a large hash scan. The unlocking candidate should still be placed next. However, the intervening step changes what
HashJoinPlanner::before_stepsees as the driving estimate. Confirm that BI-1 (and the other BI queries) still choose the hash join. - Non-triple patterns that bind nothing new. A
VALUEStable whose vars are all bound, aUNION, or a subquery can bind no new variables and still multiply rows (duplicateVALUESrows, overlapping branches). The prototype applies to every pattern kind. Consider restricting the key toPattern::Triple, where the "can only reduce" argument actually holds. - History mode. A ground triple can match once per
(t, op)version there, so it is not a pure reducer. Ordering stays semantically sound, but the cost reasoning differs. Either gate the key onPlanningContext::is_history()or add a history test. - Plan-shape snapshots. Explain output, Cypher/LDBC golden plans, and plan-order unit tests that rely on original-position ties may change. Treat any such diff as a review item, not something to re-bless automatically.
Validation expected
- A plan-order unit test for the self-loop shape plus a scan-fuel integration test. Per repo practice, revert the fix and show that the tests fail. Add a twin on the JSON-LD surface if a SPARQL test is added (or vice versa).
- Before/after on registered benches (
regression-budget.json), BSBM Explore (Q5 especially) and BI, and the LDBC IC golden suite. testsuite-sparql(separate workspace; a rootcargo testdoes not reach it).
Activity
bplatz commentedon Sep 14, 2026
This is the uncommitted prototype behind the numbers in the description. It is a starting point, not a finished change: the regression risks listed above have not been checked. The test relies on
object_seed_fixtureandobject_seed_planfrom #1824, so apply it on top of that PR.Observed plan order in that test:
["left", "payload", "right"]without the tie-break and["left", "right", "payload"]with it. This version does not yet restrict the key toPattern::Triple(risk 3) or handle history mode (risk 4).