Repository navigation
Description
Summary
A SPARQL sub-SELECT ({ SELECT ... }) used as an operand of a set/graph operation is mis-parsed, silently producing wrong results. Two symptoms were reported against fluree/server:4.1.1 from the fluree/azure-chat NL→SPARQL work:
- fluree/azure-chat#42 —
{ SELECT … } UNION { SELECT … }returns only one arm (or 0 rows), not the union. - fluree/azure-chat#43 —
{ SELECT … } MINUS { SELECT … }performs no subtraction and returns the full left operand.
Both are the same root cause in the SPARQL parser, and the defect is broader than those two shapes. Reproduced on main (HEAD 15e2a4b9), on both novelty and durable-reindexed state.
Root cause
Parser::parse_group_graph_pattern (fluree-db-sparql/src/parse/query/pattern.rs) only recognises a { SELECT ... } sub-SELECT inside its own nested-{ branch. Every other position that opens a group — OPTIONAL, GRAPH, SERVICE, the right arm of MINUS, each arm of UNION (via parse_union_continuation), EXISTS/NOT EXISTS (via the free parse_group_graph_pattern), and a bare WHERE { SELECT … } — consumes the { itself and then calls parse_group_graph_pattern positioned at the SELECT, where the leading SELECT is treated as an unexpected token.
Consequences:
- UNION — after parsing the left
{ SELECT … }the parser never checked for a trailingUNION(unlike the plain-group branch right below it), so theUNIONtoken hit"UNION must follow a pattern"and was discarded. The query collapsed into two independent subqueries lowered as a conjunction; at execution that is a correlated join on the shared projected variable, whose values are disjoint → only one arm survives (the smaller-cardinality one; ties → the left) or 0 rows. It is not "only the last arm," as azure-chat#42 hypothesised. - MINUS / OPTIONAL / GRAPH / SERVICE — the sub-SELECT operand was parsed as a bare BGP with the
(expr AS ?v)projection dropped. For MINUS the right arm then shares no bound variable with the left → nothing is subtracted; for OPTIONAL the projected/aggregate variable is lost.
The IR from lowering confirms it: {SELECT} UNION {SELECT} lowered to [Subquery, Subquery] (no Union node); {SELECT} MINUS {SELECT} lowered to [Subquery, Minus(Triple×4)] (right arm = raw triples, Bind(?n) projection gone).
Reproduction
Data: 3 ex:Person (Alice/Bob/Carol), 2 ex:Company (Acme/Globex), 5 ex:Country, 2 ex:Maker (in France + Japan).
azure-chat#42 (UNION):
PREFIX ex: <http://example.org/>
SELECT DISTINCT ?n WHERE {
{ SELECT (?pn AS ?n) WHERE { ?p a ex:Person ; ex:name ?pn } }
UNION
{ SELECT (?cn AS ?n) WHERE { ?c a ex:Company ; ex:name ?cn } }
}Expected 5 (Alice, Bob, Carol, Acme, Globex); actual 2 (Acme, Globex).
azure-chat#43 (MINUS):
PREFIX ex: <http://example.org/>
SELECT DISTINCT ?n WHERE {
{ SELECT (?cn AS ?n) WHERE { ?c a ex:Country ; ex:cname ?cn } }
MINUS
{ SELECT (?mn AS ?n) WHERE { ?c2 a ex:Country ; ex:cname ?mn . ?m a ex:Maker ; ex:inCountry ?c2 } }
}Expected 3 (Brazil, Canada, Germany); actual 5 (no subtraction).
Broader symptoms (same cause): { plain } UNION { SELECT … } drops the sub-SELECT arm; OPTIONAL { SELECT (COUNT(…) AS ?c) … } loses ?c and duplicates rows.
Fix
Centralise sub-SELECT detection at the top of parse_group_graph_pattern (the opening { is already consumed by the caller), so every position that admits a group admits the grammar's GroupGraphPattern ::= '{' ( SubSelect | GroupGraphPatternSub ) '}'; and run the trailing-UNION check for a sub-SELECT left arm too. The fix is entirely in the parser — fixed queries now compile to the already-optimised UnionOperator / sub-SELECT-MINUS plans (no query-engine change, no hot-path cost).
Validation
- W3C SPARQL suite (before → after): syntax-query 81 → 83 (
syntax-SELECTscope1, a positive{SELECT} UNION {SELECT}syntax test, now passes), subquery 11 → 12 (sq12, a bareWHERE { SELECT … }in CONSTRUCT, now passes), negation 12 → 12 (the W3C negation suite has no sub-SELECT-as-MINUS-operand test — newfluree-db-apiintegration tests fill that gap). Zero regressions. - New AST-level parser tests + end-to-end integration tests (UNION both arms + mixed + order-independence, MINUS, OPTIONAL) covering the azure-chat scenarios.
- JSON-LD query surface is unaffected (separate parser;
{query}-in-union already worked and still passes).
A PR fixing this is on the way.
Activity
docs(sparql): guard against regressing the sub-SELECT set-op fix
fix(sparql): parse sub-SELECT as a set-operation operand