Skip to content

Turtle parser drops the fragment resolving <#name> against @base, collapsing multi-TriplesMap R2RML mappings to one table #1395

Description

@aaj3f

Summary

A multi-TriplesMap R2RML mapping written in the idiomatic @base + <#Name>
style collapses into a single TriplesMap / single logical table. Registering a
16-TriplesMap star-schema mapping reports TriplesMaps: 1, and queries scan only
the first table (with a union projection of every column across all maps);
classes from non-first TriplesMaps return 0 rows.

The root cause is a Turtle-parser RFC 3986 §5.3 violation, not an R2RML
limitation.
Multi-table R2RML — per-table scans and cross-table rr:refObjectMap
joins — is already fully implemented and covered by passing offline tests. This is
a small, surgical fix.

Root cause

fluree-graph-turtle/src/parser.rs::resolve_iri drops the fragment when
resolving a relative reference against an @base:

  • the #-only branch (~parser.rs:919) discards the reference fragment (returns None);
  • the result assembly (~parser.rs:945-957) never appends #<fragment>;
  • helpers parse_iri_components (:1010) return a 4-tuple with no fragment, and split_path_query (:1043) strips everything after #.

So <#DimDate>, <#DimProduct>, … all resolve to the same IRI (the base with
its fragment stripped), violating RFC 3986 §5.3 (a same-document fragment ref must
resolve to base-minus-its-fragment + # + the reference's fragment).

How the collapse produces the exact symptoms

Once all 16 a rr:TriplesMap triples share one subject IRI:

  1. loader/extractor.rs:42-58 extracts the same IRI 16×; CompiledR2rmlMapping::add_triples_map keys by IRI → 1 entry → triples_map_count = 1.
  2. The merged subject carries all 16 rr:logicalTable / rr:subjectMap / rr:predicateObjectMap edges.
  3. extract_logical_table / extract_subject_map use first-wins (extractor.rs:84,126) → first table DW.DIM_DATE, first class edw:Date (so ?p a edw:Product matches nothing → 0 rows).
  4. POM extraction uses all (extractor.rs:179) → union of ~130 columns → the all-columns projection scan of DW.DIM_DATE seen in the logs.

Why existing tests miss it

Every R2RML fixture uses absolute TriplesMap IRIs (e.g.
<http://example.org/mapping#AirlineMapping>), which short-circuit IRI resolution
entirely. The base-resolution unit tests cover relative paths and the empty ref
<>, but none test a <#fragment> ref — the exact idiomatic R2RML style
(@base + <#Name>, as the W3C R2RML spec itself writes its examples).

Multi-table support is proven present: fluree-db-query/src/r2rml/operator.rs:338-565
fans out one independent per-table scan per matched TriplesMap (not a union) and
executes rr:refObjectMap cross-table joins; green tests exist at
fluree-db-api/tests/it_graph_source_r2rml.rs:990 (2-map compile), :1889
(cross-table join execution, 4 rows), :1985 (composite key).

Design / fix

  • Phase 1 (the fix): in parser.rs::resolve_iri, implement RFC 3986 §5.3
    fragment handling for all branches — thread the fragment through
    parse_iri_components / parse_hier_part / split_path_query and append
    #<fragment> in the result. <#X>-only → keep base scheme/authority/path/query +
    ref fragment; path#frag → carry that fragment; ref with no fragment → no
    fragment (don't inherit the base's); <> → base minus its fragment.
  • Phase 2 (hardening): make a silent collapse impossible — extractor.rs /
    CompiledR2rmlMapping::add_triples_map should error (or at least warn) when
    two rr:TriplesMap subjects resolve to the same IRI, instead of merging
    (first-wins for table/subject, union for POMs) into plausible-but-wrong data.
  • Phase 3 (UX): report Tables: N (names…) alongside TriplesMaps: N at
    registration (graph_source/result.rs, CLI commands/iceberg.rs) so a user
    mapping 16 tables immediately sees 1 if anything is wrong.
  • Phase 4 (optional perf): wire the operator's prebuilt
    find_maps_for_class/predicate/pattern indexes (mapping/compiled.rs:99-171,
    currently used only in tests) for O(1) map selection at star-schema scale.

Tests

  • Parser unit tests for @base <http://ex/edw> + <#A>/<#B> → two distinct
    IRIs (the regression guard); <rel#frag>, fragmented base, <> per RFC 3986
    §5.3. Consider wiring the vendored W3C Turtle eval suite (ignored by default).
  • An idiomatic @base + <#X> multi-table fixture compiled offline (assert
    triples_maps.len() == N, distinct tables, correct per-table projection, joins);
    drive via the existing MultiTableMockProvider.
  • Hardening test: colliding-IRI doc → error, not a merged map.

Risk / compat

resolve_iri is the resolution path for all relative IRIs in Turtle ingestion
(used by the R2RML loader and fluree-db-memory/turtle_io.rs). Today fragments are
always dropped, so any @base+fragment Turtle is already parsed wrong — the fix
is strictly more correct. Grep Turtle/golden fixtures for fragment-less IRIs before
landing. Absolute-IRI and prefixed-name inputs are unaffected (existing tests stay
green). Phase-2 hardening may surface a previously "working" (silently corrupt)
mapping as an error — correct outcome; can ship as a warning first.

Critical files

  • fluree-graph-turtle/src/parser.rs — resolve_iri (~858-958), parse_iri_components (~1010), split_path_query (~1043) — the fix
  • fluree-db-r2rml/src/loader/extractor.rs (collapse propagation; dup-IRI hardening)
  • fluree-db-r2rml/src/mapping/compiled.rs (add_triples_map dedup; unused selection indexes)
  • fluree-db-query/src/r2rml/operator.rs (proves multi-table works; Phase-4)
  • fluree-db-api/tests/it_graph_source_r2rml.rs (add @base/<#X> fixtures)

Stacking

Part of the fluree/db ↔ Snowflake-managed-Iceberg consumption series (see #1394).
Phase 1 is the foundation — bottom of the stack (PR-1), independent, size S.
Phase 2/3 share files with the R2RML media-type fix and should stack after it.
Overall size S–M.

Context: surfaced while reading a live 16-table Snowflake-managed Iceberg star
schema (8 dims + 8 facts) through fluree/db via Horizon REST + R2RML.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions