Repository navigation
Restructure query output type hierarchy - #1220
Conversation
This mirrors the actual query ast and makes the invalid state of a wildcard select and a Some graph_select unrepresentable. Also updates nomenclature and changes the name of the "graph crawl" concept to "hydration" to match other databases and ORMs
| JsonValue::Object(map) => { | ||
| let spec = parse_graph_select_object(map, ctx, query)?; | ||
| query.graph_select = Some(spec); | ||
| let spec = parse_hydration_object(map, ctx, query)?; |
There was a problem hiding this comment.
The "at most one hydration" invariant is broken for the array-form select. parse_hydration_object checks query.hydration(), but query.select isn't assigned until after this loop completes — so on the second JsonValue::Object iteration, query.hydration() still reads the stale (empty) query.select and the duplicate check passes.
Repro: select: ["?a", {"?b": ["*"]}, {"?c": ["*"]}] parses without error and lowers to Projection::Tuple([Var(a), Hydration(b), Hydration(c)]). Projection::hydration() uses find_map, so the second hydration is silently dropped at format time.
Pre-refactor this case errored, because the old code set query.graph_select = Some(spec) immediately inside the loop. Smallest fix is a local guard against columns here before pushing:
if columns.iter().any(|c| matches!(c, UnresolvedColumn::Hydration(_))) {
return Err(ParseError::InvalidSelect(
"only one graph-select object allowed per query".to_string(),
));
}Worth a focused parser test for the multi-object array case. The doc on Projection::hydration() ("at most one; enforced by the parser") is currently aspirational without this.
There was a problem hiding this comment.
@bplatz I decided to allow multiple hydrations (or graph crawls) in the select clause instead. I couldn't find a performance or architectural reason not to, and I think a new user would expect to be able to do it and would be surprised by the error.
Here is the implementing commit range. I'm happy to roll it back if you disagree that this is something we should support.
There was a problem hiding this comment.
@bplatz I decided to allow multiple hydrations (or graph crawls) in the select clause instead. I couldn't find a performance or architectural reason not to, and I think a new user would expect to be able to do it and would be surprised by the error.
Here is the implementing commit range. I'm happy to roll it back if you disagree that this is something we should support.
Great!
bplatz
left a comment
There was a problem hiding this comment.
Besides one comment, looks good!
Summary
This branch builds on #1219 and restructures the query output type hierarchy in
fluree-db-queryso that combinations the engine cannot produce are also combinations the type system cannot construct. Eliminates the standalonegraph_selectfield that could legally coexist with aSELECT *, replaces a parallel(vars, has_wildcard, hydration_spec)representation with a singleProjectionenum that mirrors the actual SPARQL semantics, and renames a handful of types and methods so that the names match what they do. Net effect: 53 files changed, ~1,248 insertions / ~1,203 deletions, with no behavior changes.Motivation
QueryOutput::Select { vars: Vec<VarId>, ... }carried only the bound-variable list. Hydration (the "graph crawl" / nested JSON-LD expansion) lived as a separategraph_select: Option<HydrationSpec>field onQuery. Nothing in the type system preventedSELECT *(wildcard) from coexisting with a hydration spec, even though the formatter rejects that combination at runtime — and the parser had to encode wildcard-vs-explicit selection through ahas_wildcard: boolflag duplicated alongside the actual selection list.NestedSelectSpechad the same issue at every level of nested expansion: ahas_wildcardflag plus aVec<SelectionSpec>that the formatter had to cross-check.QueryOutput::Booleanwas the only variant whose name didn't match standard SPARQL terminology (everywhere else,Construct,Select,Ask).Key changes
QueryOutput::SelectcarriesProjectionandMultiplicity. ReplacesSelect { vars: Vec<VarId>, ... }+graph_select: Option<HydrationSpec>withSelect { projection: Projection, multiplicity: Multiplicity }. New types:Projection { Wildcard, Tuple(Vec<Column>), Scalar(Column) }— the column structure of the projection.Wildcardis now a variant, not a flag.Column { Var(VarId), Hydration(HydrationSpec) }— what each output column produces. MixedVar+Hydrationcolumns in one row are representable by construction.Multiplicity { All, One }— replaces the priorselectOnediscrimination.NestedSelectSpecbecomesenum { Wildcard { refinements, reverse }, Explicit { forward, reverse } }. Wildcard-vs-explicit is encoded structurally rather than via ahas_wildcard: boolplus aVec<SelectionSpec>whose contents are interpreted differently depending on the flag. New helpers (is_wildcard(),select_predicate(),includes_id(),reverse()) flatten what would otherwise be deep matches in the hydration formatter.SelectionSpecdeleted, replaced byForwardItem { Id, Property { predicate, sub_spec } }. The@idselection is now its own variant rather than a special-casedWildcardflag.UnresolvedProjection,UnresolvedColumn,UnresolvedNestedSelectSpec,UnresolvedForwardItemnow have the same shape as their IR counterparts, so lowering is a structural translation rather than a multi-source assembly.SelectMode::Wildcarddropped. Wildcard is now reachable viaProjection::Wildcarddirectly; the parser-internalSelectModeenum no longer has to carry it.QueryOutput::Booleanrenamed toAskto match SPARQL terminology and the rest of the variant family.QueryOutput::select_vars()getter →projected_vars()QueryOutput::select_vars_or_empty()→projected_vars_or_empty()QueryOutput::select(vars)constructor →select_vars(vars)(free of the getter-name collision)QueryOutput::select_one(vars)→select_one_var(vars)Behavior
No semantic changes. Pure type-system tightening: states the engine never produced are no longer expressible. The hydration formatter, planner detect-helpers, and SPARQL/JSON-LD lower paths are migrated to the new shape. All in-workspace consumers (
fluree-db-api/src/{format,view,query,graph_source}/*,fluree-db-cli/src/{output,commands}/*,fluree-db-sparql/src/lower/*, the test suites) are updated.One incidental cleanup: the 13 detect-helper functions in
fluree-db-query/src/execute/operator_tree.rspreviously calledquery.output.projected_vars()twice each (once for anis_none()guard, once to bind the variables). The newprojected_vars()returns an ownedVec<VarId>(forced by theVec<Column>storage), so the double call was a double allocation. The earlyis_none()guard was redundant with the existing?later in each function — removed.