Skip to content

Support TriG on insert via a flake-level fast path #1849

Open

Description

@bplatz

Summary

TriG is supported only through upsert. On insert, a TriG payload is refused, and how it's refused depends on which entry point you use. On most of them the error is a Turtle syntax error rather than "TriG isn't supported here".

Probed on main + #1832 (e4e4ecc25), payload @prefix ex: <http://example.org/> . GRAPH <http://example.org/g> { ex:alice ex:knows ex:bob . }:

Entry point Result
Fluree::insert_turtle(ledger, trig) Turtle parse error: … expected subject, found 'GRAPH'
fluree.graph(id).transact().insert_turtle(trig).commit() same
same, compact form <g> { … } Turtle parse error: … expected predicate, found '{'
fluree.graph(id).transact().insert_turtle(trig).stage() parses the TriG, then fails Insert must contain at least one predicate or @type … on the empty default graph
Server POST /insert, Content-Type: application/trig clean 400: "TriG format … is not supported on the insert endpoint" (routes/transact.rs, deliberate)
Server POST /insert, TriG body sent as text/turtle Turtle parse error, as in the first row
CLI fluree insert -f data.trig Turtle parse error (documented in docs/transactions/turtle.md)

Why

InsertTurtle has a direct flake fast path that streams the body through fluree-graph-turtle, which has no GRAPH production:

  • tx_builder.rs: OwnedTransactBuilder::execute and ::stage (// Direct flake path for InsertTurtle)
  • tx_builder.rs: stage_under_lock and stage_plan (OpPlan::InsertTurtle), the ledger-handle commit lanes

TransactOperation::to_json_with_trig_meta does have an InsertTurtle arm that runs parse_trig_phase1, and it looks like TriG-on-insert was meant to work. But every lane except GraphTransactBuilder::stage short-circuits before reaching it. That lane reaches it and then trips on the JSON-LD insert validation, so stage() and commit() disagree for the same payload.

The same gap covers GRAPH <#txn-meta> { } commit metadata, which only works on upsert.

Options

  1. Support TriG on insert. Keep the streaming fast path for plain Turtle. When the payload has graph blocks, route it through the existing TriG phase-1 + IR path with TxnType::Insert, as upsert does. Detection needs to stay cheap: the content type or file extension where there is one, otherwise a lexer pre-scan for GRAPH / <iri> { / <#txn-meta>. The empty-default-graph validation failure on stage() would need fixing too.
  2. Refuse consistently. Detect graph blocks on every insert_turtle lane and return the same "TriG requires upsert" error the server gives for application/trig, including when the body comes as text/turtle and in the CLI.

Option 1 is what the edge-annotation docs originally implied (insert was listed as a TriG path until #1832 corrected it). Option 2 is the smaller change. Either way, the dead-looking InsertTurtle arm in to_json_with_trig_meta should end up either reachable or removed.

Done when

  • All entry points above behave the same for a TriG payload on insert: either all accept it, or all refuse it with one TriG-specific message.
  • docs/transactions/turtle.md (endpoint table and "TriG goes through upsert, not insert"), docs/concepts/edge-annotations.md and docs/guides/cookbook-edge-annotations.md match.
  • A test covers each builder lane (Fluree::insert_turtle, graph builder commit and stage).

Follow-up from #1832 (listed under "Noted, not fixed here").

Activity

  1. added
    P3
    triage:needs-decision
    area:transact
    on Sep 15, 2026
  2. bplatz commented on Sep 15, 2026

    @bplatz
    ContributorAuthor

    Decision: support TriG on insert (option 1), as a flake-level fast path, not by sending insert through the upsert template/IR lane. Proposed shape below, from reading the existing pieces; nothing is implemented yet.

    Pieces that already exist

    • stage_flakes (fluree-db-transact/src/stage.rs) already stages named-graph flakes (Flake.g = Some) when StageOptions.graph_sids is provided, and runs enforce_single_target_reifiers over them.
    • StageResult already carries graph_delta and txn_meta. stage_turtle_insert just returns them empty.
    • import_trig_commit (fluree-db-transact/src/import.rs) is the model to follow. It runs parse_trig_phase1, streams the default-graph Turtle through a sink, and turns each NamedGraphBlock straight into flakes: expand_term / expand_object, per-block graph_sid, and reified_triple_bundle(Some(graph_sid), …) for TriG-star. resolve_trig_meta handles <#txn-meta>.

    Proposed stage_trig_insert

    1. Split: run parse_trig_phase1(input), giving default-graph Turtle, named-graph blocks and raw txn-meta.
    2. Default graph: stream the Turtle through FlakeSink, as stage_turtle_insert does today.
    3. Named graphs: turn each block into flakes with the same term expansion import uses. Blank-node labels must skolemize with the sink's txn_id scheme, so a _:b used in the default graph and inside a block is the same node, which is TriG's document scope.
    4. Graphs: resolve or allocate g_ids against the ledger's graph registry, building graph_delta and graph_sids. Refuse reserved system graphs by full IRI. That guard currently sits in the template stage() path (fix: @json opacity, var-length edge identity, commit-id spelling, reserved-graph contract #1838), and stage_flakes doesn't go through it.
    5. Commit metadata: resolve_trig_meta gives StageResult.txn_meta.
    6. Stage: call stage_flakes with graph_sids, policy and tracker, and pass graph_delta / graph_sids to SHACL instead of None.

    This also takes care of the insert half of #1848: TriG-star bundles on insert would come from the shared reified_triple_bundle instead of the hand-built templates in tx.rs. Upsert keeps its template path.

    Routing, so plain Turtle stays on today's path

    • TransactOperation::InsertTurtle: try the streaming parse first. If it fails at a TriG-only construct (GRAPH, <iri> {), rebuild the namespace registry and rerun through stage_trig_insert. Plain Turtle pays nothing; a TriG payload pays one partial parse.
    • Where the format is known up front (server Content-Type: application/trig, CLI .trig): go straight to stage_trig_insert. Drop the server's 400 for TriG on /insert.
    • Both lanes in tx_builder.rs (OwnedTransactBuilder::execute / ::stage, stage_under_lock / stage_plan) and GraphTransactBuilder::stage should end at the same function, so stage() and commit() agree.

    Tests

    • A parity test: the same TriG payload through insert and upsert on fresh ledgers gives the same named-graph contents, annotations included.
    • Blank-node sharing between the default graph and a block.
    • <#txn-meta> on insert.
    • Reserved-graph refusal by full IRI.
    • Each builder lane.
    • A MustFire-style routing check that plain Turtle never takes the TriG path.
    • An insert_formats bench case for TriG next to the existing Turtle one.
  3. removed
    triage:needs-decision
    on Sep 15, 2026
  4. changed the title [-]insert refuses TriG inconsistently: parse error on most lanes, 400 on the server, a different error on stage()[/-] [+]Support TriG on insert via a flake-level fast path[/+] on Sep 15, 2026
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

    P3area:transact

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions