Skip to content

Property-join semijoin demotion drops row multiplicity when the object var is unprojected (COUNT(*) undercounts; SELECT drops rows) #1700

Closed

Description

@aaj3f

Surfaced while pinning ground truth for #1391 and reproduced standalone at a824bbfd1. Two-pattern join where a subject carries multiple objects: COUNT(*) on the generic pipeline returns the number of qualifying subjects, not the number of joined rows. The fast path returns the correct answer, so the divergence has the opposite polarity from the ones we usually chase — and that has a consequence beyond the bug itself, which is the part I'd flag hardest (see below).

Repro

@prefix ex: <http://example.org/> .
ex:g1 a ex:Gadget ; ex:tag "a" , "b" , "c" .
ex:g2 a ex:Gadget ; ex:tag "d" , "e" .
fluree create cs2 --from d2.ttl
fluree index cs2                     # must be indexed — see below

Q='PREFIX ex: <http://example.org/> SELECT (COUNT(*) AS ?c) WHERE { ?s a ex:Gadget . ?s ex:tag ?o }'

fluree query cs2 --sparql "$Q"                                     # → 5   ✅ (fast path)
FLUREE_DISABLE_QUERY_FAST_PATHS=1 fluree query cs2 --sparql "$Q"   # → 2   ❌ (generic)

The same pattern projected as rows (SELECT ?s ?o) returns 5 on both lanes, and COUNT(?o) returns 5 on both. Only COUNT(*) on the generic lane is wrong, and 2 is exactly the number of distinct subjects. A second fixture (3 widgets, one carrying two names: 4 rows, 3 subjects) gives 4 on the fast path and 3 on the generic one — same rule.

Two conditions matter for reproducing: the ledger must be indexed (on a never-indexed ledger both lanes return 4/5 correctly), and it needs a subject with more than one object on the joined predicate, since that's the multiplicity that gets collapsed.

Why I think this is worth more than its row count

FLUREE_DISABLE_QUERY_FAST_PATHS is our standard oracle — it's how fast-path suites establish ground truth, and the whole differential-testing pattern assumes the generic pipeline is the reference implementation. For COUNT(*) over a multiplicity-bearing join, that assumption is false right now: a differential test comparing fast-vs-generic on this shape would either be green because both were compared against the wrong number, or would "fail" against a fast path that's actually correct and get fixed the wrong way. That's the same class of problem as the fast-path test-guard blind spots we've hit before, but one level up — the oracle itself.

So beyond fixing the count, it seems worth grepping for existing tests that assert COUNT(*) through the kill switch and re-checking what they pinned.

Notes toward a diagnosis

I haven't traced the generic lane's aggregate path, so this is where the investigation starts rather than a conclusion. It looks like COUNT(*) is being answered from a subject-level cardinality rather than from the joined solution sequence — same family as the star top-k multiplicity bug fixed in #1666, but that one was on a fast path and this is the generic count-plan lane. Per the parity rule the JSON-LD surface should be checked for the same shape, and the fix wants a regression test on both surfaces that pins the multi-object case specifically — a single-object-per-subject fixture cannot tell the two answers apart, which is presumably how this survived.

Unrelated to #1391 (reproduces with an empty novelty window and identically with fast paths disabled); the #1391 work routed around it by using COUNT(?o) for ground truth.

Activity

  1. added
    bug
    P1
    area:query
    on Aug 26, 2026
  2. aaj3f commented on Aug 26, 2026

    @aaj3f
    ContributorAuthor

    Correcting my own framing here, because the fix work found this is materially worse than I filed it — and worse in a way that makes the title misleading.

    It isn't an aggregate defect at all. The rows are gone before the aggregate runs. PropertyJoinOperator demotes a predicate to an existence-only semijoin when its object variable is absent from the block's needed-vars set (property_join.rs:422-427: let emit = needed_vars.is_none_or(|n| n.contains(v))), recording that the subject has some object and dropping the per-subject object list. That's a set operation, so N objects collapse to one row. COUNT(*) names no variables, so ?o is in nothing's needed set and the count comes back as the subject count. The demotion was applied unconditionally at both call sites (where_plan.rs:1260, :3138).

    And it is not limited to COUNT(*). On the same fixture, with the object variable unprojected:

    • SELECT ?s returns 2 rows where the truth is 5
    • grouped COUNT(*) returns 1, 1 where the truth is 3, 2
    • COUNT(*) over a two-variable-object star returns 2 where the truth is 8 — a 4× undercount

    Those are wrong identically on both lanes, which is the part worth dwelling on: my original report only caught this because two shapes happened to have a COUNT(*) fast path that got them right, producing a visible 5-vs-2 disagreement. Everywhere else the two lanes agreed with each other and were both wrong. No differential test could ever have found it.

    So I should also soften what I said about the oracle. FLUREE_DISABLE_QUERY_FAST_PATHS isn't broken for COUNT(*) in general — the real story is a shared planner defect below both lanes, which the fast path happened to sidestep on two shapes. The lesson stands but it's a different one: fast-vs-generic agreement is not evidence of correctness when the defect is in the plan they share.

    One genuinely good outcome from the sweep of existing tests: nothing was mispinned. The COUNT(*) case in it_differential_fastpath.rs is single-triple, and the star cases in it_repeated_var_fast_path_guards.rs assert hand-computed values rather than the other lane. The wrong answers were unpinned, not mispinned — no expectation edits were needed anywhere, including the W3C suite.

    Fix is up as #1706. The correct license already existed and was simply never read — where_dedup_safe, for which CountAll.duplicate_insensitive() is already false — so it threads that in rather than inventing a new rule, and SELECT DISTINCT ?s / COUNT(DISTINCT ?s) keep the pruned plan.

  3. changed the title [-]`COUNT(*)` counts distinct subjects instead of joined rows on the generic pipeline — and the fast path is the one that's right[/-] [+]Property-join semijoin demotion drops row multiplicity when the object var is unprojected (COUNT(*) undercounts; SELECT drops rows)[/+] on Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1area:querybug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions