Skip to content

SPARQL: an expression in the SELECT of a grouped query emits one row per input binding instead of one per group #1978

Closed

Description

@aaj3f

I think this is a sibling of #1362 that #1374's fix doesn't reach. #1362 was an expression as the group key (GROUP BY DATATYPE(?v)), and grouping on expressions collapses correctly now. This one groups on a plain variable and puts the expression in the projection, and there the grouped rows come back once per input binding. The aggregate values in each row are right — they're just repeated.

Repro (db main @ ac1e8a61b, fluree CLI built from it, --direct; the same happens through fluree/solo's POST /v1/fluree/query/{ledger}):

fluree create gbt
fluree insert gbt '@prefix ex: <http://example.org/> .
ex:e1 ex:area "Net" . ex:e2 ex:area "Net" . ex:e3 ex:area "Net" . ex:e4 ex:area "Local" . ex:e5 ex:area "Local" .'

# plain projection: 2 rows, as expected
fluree query --direct gbt 'PREFIX ex: <http://example.org/>
SELECT ?a (COUNT(?e) AS ?n) WHERE { ?e ex:area ?a } GROUP BY ?a'

# expression in the projection: 5 rows (one per binding), each carrying its group's count
fluree query --direct gbt 'PREFIX ex: <http://example.org/>
SELECT (IF(?a = "Net", "network", "other") AS ?seg) (COUNT(?e) AS ?n) WHERE { ?e ex:area ?a } GROUP BY ?a'
# | 3 | network |  ×3   and   | 2 | other | ×2      → (5 rows)

# same with CONCAT
fluree query --direct gbt 'PREFIX ex: <http://example.org/>
SELECT (CONCAT(?a, "!") AS ?x) (COUNT(?e) AS ?n) WHERE { ?e ex:area ?a } GROUP BY ?a'   # → 5 rows

Expected: 2 rows in each case (SPARQL 1.1 §18.2.4.1 — the projection is evaluated once per group, over the group's key and aggregates). Oxigraph returns 2.

At scale it's the same shape: on a ~9M-triple graph, grouping about 2,700 records by a three-valued string property with an IF(...) label in the SELECT returns 2,709 rows (3 distinct) where Oxigraph returns 3, and a 4-group query returned 900.

Why it matters. It's silent. The numbers in each row are correct, so it looks like an answer, but any "top N", LIMIT, row count or client-side sum over the result is wrong. We hit it through an LLM agent writing analytics queries over a customer graph — labeling a group key with IF(...)/CONCAT(...) in the SELECT is exactly the shape those agents produce, and the tool response budget then fills with duplicates.

I haven't traced where the projection is evaluated relative to the group-by operator (Claude's reading of the symptom is that the projected expression is computed against the pre-aggregation solutions and then joined back to the group rows, but I haven't confirmed that in code). The failing shapes above would make good regression tests next to #1374's.

Found on the rev fluree/solo pins (ffeeec554) and confirmed at ac1e8a61b.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2area:sparqlbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions