Skip to content

Cross-ledger explicit projection silently drops foreign-namespace predicates #1295

Closed

Description

@Jackamus29

Summary

PR #1267 made cross-ledger hydration expansion work — a ref into another ledger now hydrates instead of collapsing to a bare {"@id": ...}. But it only fixed the wildcard (["*"]) path. With an explicit nested projection, every predicate on the foreign subject whose namespace code differs between the primary ledger and the subject's home ledger is silently dropped. Only @id and predicates in reserved namespaces (rdfs, rdf, xsd, owl, …) reliably survive, because those codes are fixed across all ledgers.

Confirmed on v4.0.5. Follow-up to #1259 / #1267.

Reproduction

Two ledgers sharing schema.org: catalog (primary) holds a schema:Product whose schema:author ref points into a separate people ledger. The person carries p:fullName (http://people.example/) — a predicate whose namespace is allocated a different code in each ledger (see the seed in the test below). The projection crosses catalog → people.

{
  "@context": {"schema": "http://schema.org/",
               "cat": "http://catalog.example/",
               "p": "http://people.example/",
               "id": "@id", "type": "@type"},
  "from": ["test/catalog:main", "test/people:main"],
  "select": { "?item": ["@id", { "schema:author": ["@id", "schema:name", "p:fullName"] }] },
  "where": { "@id": "?item", "type": "schema:Product" }
}

Actual — p:fullName is gone:

{ "schema:author": { "@id": "p:ada", "schema:name": "Ada Lovelace" } }
  • Wildcard works — "schema:author": ["*"] returns p:fullName too.
  • Single-ledger explicit works — the same ["@id","schema:name","p:fullName"] projection against test/people:main alone returns all three.

So the fault is specific to the explicit projection × cross-ledger combination. (schema:name survives only because schema.org happens to share a code across the two ledgers in this seed; @id and any reserved-namespace predicate like rdfs:label always survive.)

Reproduction test

Drops into fluree-db-api/tests/it_multi_ledger_formatting.rs and uses the existing suite helpers (genesis_ledger, MemoryFluree, FlureeBuilder). It is red on v4.0.5 — the final assertion fails with left: None, right: Some("Augusta Ada King").

/// Seed a primary `catalog` ledger whose product REFERENCES a person in a
/// separate `people` ledger, where the person carries a predicate (`p:fullName`)
/// in a namespace that is allocated a DIFFERENT code in each ledger:
///   - catalog: people.example registered last (via the `schema:author` ref) → high code
///   - people:  people.example registered first (the subject `p:ada`)        → low code
/// This is the cross-ledger explicit-projection drop condition.
async fn seed_divergent_predicate_ref_ledgers(fluree: &MemoryFluree) {
    fluree
        .insert(
            genesis_ledger(fluree, "test/catalog:main"),
            &json!({
                "@context": {
                    "schema": "http://schema.org/",
                    "cat": "http://catalog.example/",
                    "p": "http://people.example/",
                    "id": "@id", "type": "@type",
                },
                "@graph": [
                    {"@id": "cat:item1", "@type": "schema:Product",
                     "schema:author": {"@id": "p:ada"}}
                ]
            }),
        )
        .await
        .expect("insert catalog");

    fluree
        .insert(
            genesis_ledger(fluree, "test/people:main"),
            &json!({
                "@context": {
                    "schema": "http://schema.org/",
                    "p": "http://people.example/",
                    "id": "@id", "type": "@type",
                },
                "@graph": [
                    {"@id": "p:ada", "@type": "schema:Person",
                     "schema:name": "Ada Lovelace", "p:fullName": "Augusta Ada King"}
                ]
            }),
        )
        .await
        .expect("insert people");
}

/// Cross-ledger NESTED EXPLICIT projection must return ALL projected predicates
/// of the foreign subject — not just `@id` and reserved-namespace ones.
#[tokio::test]
async fn cross_graph_nested_explicit_projection_divergent_predicate() {
    let fluree = FlureeBuilder::memory().build_memory();
    seed_divergent_predicate_ref_ledgers(&fluree).await;

    let q = json!({
        "@context": {
            "schema": "http://schema.org/",
            "cat": "http://catalog.example/",
            "p": "http://people.example/",
            "id": "@id", "type": "@type",
        },
        "from": ["test/catalog:main", "test/people:main"],
        // EXPLICIT projection (not ["*"]) crossing catalog -> people:
        "select": { "?item": ["@id",
            { "schema:author": ["@id", "schema:name", "p:fullName"] }
        ]},
        "where": { "@id": "?item", "type": "schema:Product" }
    });

    let value = fluree
        .query_from()
        .jsonld(&q)
        .execute_formatted()
        .await
        .expect("execute_formatted should not error");

    let item = value.as_array().and_then(|a| a.first()).expect("one item");
    let author = item.get("schema:author").expect("author present");

    assert_eq!(author.get("@id").and_then(|v| v.as_str()), Some("p:ada"));
    // Reserved/shared-code predicate survives even pre-fix:
    assert_eq!(
        author.get("schema:name").and_then(|v| v.as_str()),
        Some("Ada Lovelace"),
    );
    // THE REGRESSION: divergent-namespace predicate must be present, not dropped.
    assert_eq!(
        author.get("p:fullName").and_then(|v| v.as_str()),
        Some("Augusta Ada King"),
        "cross-ledger explicit projection dropped a non-primary-namespace predicate: {value:#}"
    );
}

Root cause

A dataset query is parsed/lowered once, against the primary ledger's namespace dictionary (fluree-db-api/src/view/dataset_query.rs:95 — "using primary db for namespace resolution"). So every predicate in the lowered NestedSelectSpec is a Sid { namespace_code, name } carrying the primary ledger's code.

Cross-ledger expansion in expand_ref (fluree-db-api/src/format/hydration.rs:960) re-encodes the subject IRI for the target view (encode_iri_strict, :993) but passes the projection level — with its primary-dict predicate Sids — through unchanged (:998). On the target view those stale Sids hit two gates, both keyed on the full Sid:

  1. the per-predicate fetch filter (predicate_filter_for_level, :150; applied at :849), and
  2. the select_predicate match in format_subject (:868).

User-namespace codes are allocated lazily, per ledger, in first-use order (fluree-db-core/src/namespaces.rs:9), so Sid(<primary code>, "fullName") ≠ Sid(<home code>, "fullName") → no match → silently dropped. rdfs:label is Sid(4, "label") in every ledger (RDFS = 4 is reserved, fluree-vocab/src/lib.rs:1424), so it survives — which is why a reserved-namespace predicate can look special-cased. It isn't; it's just namespace-code-stable.

The WHERE/executor path already handles this — it stamps Binding::IriMatch with per-ledger provenance and re-encodes across ledgers (fluree-db-query/src/dataset.rs:57-61). The projection-hydration path never got the same treatment.

Why #1267's tests didn't catch it

Every cross-ledger expansion test added in #1267 uses ["*"] + depth (it_multi_ledger_formatting.rs:133,181,319,372), where the predicate-Sid mismatch is invisible (wildcard sets no filter and matches all predicates). The one explicit nested test (:460-495) is a fromNamed+GRAPH query whose ref stays inside its named graph and whose assertions only check the root @id, not foreign predicate completeness. There's even a NOTE (staged) at format/hydration.rs:474-476 acknowledging "correct only when the two ledgers allocated matching namespace codes."

Direction (gesture, not a prescription)

The wildcard path already works, so the explicit-projection path is where to look. The general shape: the projection hydrator should reconcile predicate identity against the ledger that actually stores the subject, rather than relying on the Sids resolved once against the primary dict — the executor already does an analogous per-ledger reconciliation for WHERE bindings (fluree-db-query/src/dataset.rs:57-61), so there's an existing pattern to follow. The same divergence likely affects a non-primary hydration root under an explicit projection, not just nested refs. This looks contained to fluree-db-api's formatter with no QueryResult change.

Activity

  1. changed the title [-]Cross-ledger nested explicit projection silently drops predicates whose namespace code differs from the primary ledger (follow-up to #1267)[/-] [+]Cross-ledger explicit projection silently drops foreign-namespace predicates[/+] on Jun 6, 2026
  2. Jackamus29 commented on Jun 30, 2026

    @Jackamus29
    ContributorAuthor

    Still reproduces on v4.1.1 — plus a second mechanism at the projection root

    Re-confirmed on v4.1.1; nothing in the 4.1.x work touched it. Both the original drop and the "non-primary hydration root" this issue's Direction section predicted are now covered by build-verified tests in fluree-db-api/tests/it_multi_ledger_formatting.rs, using the existing seed_divergent_ledgers (movies/books/authors) and seed_divergent_predicate_ledgers (catalog/people) fixtures.

    1. The core drop still reproduces

    A cross-ledger explicit projection still silently drops a predicate whose namespace code differs between the primary and home ledgers, while a reserved/aligned-namespace predicate survives.
    Test: cross_graph_nested_explicit_projection_divergent_predicate — p:fullName (divergent ns) dropped, schema:name (aligned) kept. Red on v4.1.1.

    2. The predicted "non-primary hydration root" is real — and it's a distinct mechanism

    When a top-level result variable is bound as the object of a primary-ledger triple and its subject actually lives in the other ledger, the projection comes back @id-only — even a predicate whose namespace code is identical across both ledgers is dropped:

    // from: [test/movies:main, test/books:main]   (movies is primary)
    // ?b is bound as the object of `movie:m1 schema:isBasedOn ?b` (matched in movies),
    // but book:b1 actually lives in the books ledger.
    "select": { "?b": ["@id", "schema:name", "schema:isbn"] },
    "where":  { "@id": "?m", "schema:isBasedOn": "?b" }
    // → [ { "@id": "book:b1" } ]   — schema:name / schema:isbn dropped

    Because schema.org is allocated the same code across these ledgers, a per-predicate Sid mismatch (the mechanism behind drop 1) cannot explain schema:name disappearing. The whole subject is being hydrated against the primary view — where book:b1 has no triples, so only @id survives — instead of its home view.

    The contrast that isolates it, using the same subject book:b1:

    how book:b1 (books-ledger) is reached, under from: [movies, books] result
    bound in its home graph (?b a schema:Book) ✅ full props (schema:name, …)
    bound as object of a primary-ledger triple (movie:m1 schema:isBasedOn ?b) ❌ @id-only

    Tests: cross_graph_root_bound_in_home_graph_hydrates (passes) vs. cross_graph_root_bound_as_object_hydrates_in_home_ledger (fails @id-only). Both at v4.1.1.

    Why it matters for the fix

    This belongs to the same "reconcile identity against the ledger that actually stores the subject" direction the issue sketches, but it's a second, adjacent mechanism: the object-bound root binding carries no home-ledger provenance, so the formatter falls back to the primary view. So the root case needs the bound subject's home view selected for hydration, not only its predicate Sids reconciled. A fix for #1295 should cover both surfaces.

    Happy to open a PR with the three tests referenced above.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions