Skip to content

Index build silently destroys temporal and xsd:long literals (xsd:date → 1970-01-01), and the resulting triples cannot be retracted by literal match #1987

Closed

Description

@lgazo

Summary

Building an index replaces the values of xsd:date, xsd:dateTime, xsd:time, xsd:gYear
and xsd:long literals with wrong values. The commit files retain the correct data — only
indexed reads are affected, so a ledger looks perfectly healthy until its first index build,
at which point every temporal value in it becomes wrong.

Two properties make this worse than a normal read bug:

  1. It is silent. No error, no warning. xsd:date returns 1970-01-01, which is a
    plausible-looking date. Triple counts, graph counts, and every other datatype are
    unaffected, so integrity checks pass.
  2. The affected triples become un-retractable. DELETE DATA with the exact literal
    returns success and removes nothing (see part 2 below), so the natural repair —
    delete and re-insert — silently no-ops.

Verified on 4.1.4 and 4.2.1. Upgrading 4.1.4 → 4.2.1 did not fix it.

Reproduction (no server required)

fluree init --direct
fluree create --direct dt/x
fluree update --direct -l dt/x -e 'INSERT DATA {
  <https://ex.org/a> <https://ex.org/d> "2026-09-08"^^<http://www.w3.org/2001/XMLSchema#date> }'

fluree query --direct -l dt/x -e 'SELECT ?o WHERE { ?s <https://ex.org/d> ?o }'
#   2026-09-08          <-- correct (novelty)

fluree index --direct dt/x

fluree query --direct -l dt/x -e 'SELECT ?o WHERE { ?s <https://ex.org/d> ?o }'
#   1970-01-01          <-- WRONG (indexed)

fluree reindex (full replay from commit history) produces a byte-identical index root
to fluree index, so replaying from commits is not a workaround. The server-side
POST /v1/fluree/reindex endpoint is also affected.

Affected datatypes

One subject, twelve typed literals, read before and after fluree index on 4.2.1:

datatype before index after index
xsd:date 2026-09-08 1970-01-01 corrupted
xsd:dateTime 2026-09-08T12:34:56Z <dateTime -9223372036854775807> corrupted
xsd:time 12:34:56 00:00:00.000002 corrupted
xsd:gYear 2026 0003 corrupted
xsd:long 20705 -9223372036854775803 corrupted
xsd:duration P3D P3D ok on 4.2.1; row vanished entirely on 4.1.4
xsd:string 2026-09-08 2026-09-08 ok
plain literal 2026-09-08 2026-09-08 ok
xsd:integer 20705 20705 ok
xsd:decimal 12.5 12.5 ok
xsd:boolean true true ok
"hello"@en hello hello ok

Note xsd:integer survives while xsd:long does not.

Suspected cause

The corrupt values are small ordinals — 0003, 00:00:00.000002,
-9223372036854775803 (= i64::MIN + 5) — rather than arbitrary garbage. That looks like a
dictionary/reference ID being decoded as the target type instead of being resolved back to
its value. This is the same area as #1729 ("Indexed reads flatten non-xsd:string
string-dictionary datatypes", root-caused to object_binding.rs:105-112), which is already
fixed in 4.2.1 — so this appears to be a sibling gap on the temporal/long decode path
rather than the same defect.

Also closely related in shape: #1737 ("Numeric subtype leniency holds on novelty but not once
indexed"), which notes that testing covered novelty only and missed the index divergence.
That is exactly the failure class here, but #1737 explicitly scopes itself to numeric
subtypes and does not cover temporal types.

Part 2 — affected literals cannot be retracted

Once a temporal literal exists, DELETE DATA naming it exactly cannot remove it:

DELETE DATA { GRAPH <...> {
  <...subject> <...date> "2026-09-22"^^<http://www.w3.org/2001/XMLSchema#date> . } }

Returns success with a new t, removes nothing. Observed on a real 45-triple migration: 40
retracted, 5 did not; two identical passes missed the same 5. This happens with no index
present
, so it is not a consequence of a corrupt index — literal matching for these
datatypes fails on its own. DELETE WHERE, DELETE {} WHERE {}, WITH … DELETE naming the
literal, and the JSON update delete clause all behave the same. Plain-string literals delete
normally.

Workaround that does work — bind the object as a variable and filter on datatype(),
never naming the literal:

WITH <https://example.org/graph/g1>
DELETE { ?s <https://example.org/date> ?d }
WHERE  { ?s <https://example.org/date> ?d
         FILTER(datatype(?d) = <http://www.w3.org/2001/XMLSchema#date>) }

GRAPH ?g is rejected in SPARQL Update DELETE templates
(GRAPH ?g is not supported in SPARQL Update DELETE templates), so this needs one statement
per named graph.

Impact

A production knowledge ledger silently lost the correct value of every xsd:date it held.
It was only noticed because I recognised that 1970 dates were implausible. Recovery
required stopping the server, deleting the index directory and its nameservice root pointer
so the engine would serve from commits, dumping the true values, and migrating every date to
a plain string literal — because any future index build re-corrupts typed dates, and
reindex_min_bytes defaults to ~every commit.

Environment

  • Fluree 4.1.4 and 4.2.1 (both affected)
  • Linux x86_64
  • File storage, single node, no Raft
  • Reproduced both via the CLI with --direct and through the HTTP server

Activity

  1. bplatz commented on Sep 29, 2026

    @bplatz
    Contributor

    Reproduced on v4.2.2 (ba984c7f5, current main). Most of this starts on the write path: SPARQL UPDATE stores these literals as strings, and both parts of the report follow from that.

    Root cause: SPARQL UPDATE doesn't parse typed literals

    SPARQL UPDATE turns a typed literal into a value with coerce_typed_flake_value (fluree-db-transact/src/lower_sparql_update.rs:2133). It knows only xsd:integer, xsd:double, xsd:decimal, xsd:boolean and f:embeddingVector. Anything else falls back to FlakeValue::String(lexical) carrying the declared datatype, which is a string flake labeled xsd:date, xsd:long, and so on.

    Every other write path parses the lexical and stores a real Date / DateTime / Long:

    • Turtle through value_convert::convert_string_literal
    • JSON-LD and TriG templates through fluree_db_core::coerce::coerce_string_value
    • the SPARQL query side through lower_typed_literal The TriG template path has a comment warning about exactly this (fluree-db-api/src/tx.rs:2445-2457).

    That is why the table in the report splits the way it does: xsd:integer, xsd:decimal and xsd:boolean survive because the SPARQL path parses them, and xsd:long doesn't because it isn't on the list.

    • Part 1 (index build): the indexer takes the object key from the value variant but the object type from the datatype. A String payload becomes (ObjKind::LEX_ID, string-dict id) (fluree-db-indexer/src/run_index/resolve/resolver.rs:885-891; the datatype argument is unused there). OTypeRegistry::resolve then maps LEX_ID through resolve_by_dt (fluree-db-core/src/o_type_registry.rs:106-112), which gives XSD_INT, XSD_DATE_TIME and so on. The reader dispatches on that type alone (fluree-db-binary-index/src/read/binary_index_store.rs:1354), so a small dictionary id N is decoded as an int, a date or a time:

      • int / long / dateTime: i64::MIN + N
      • date: day N → 1970-01-03
      • gYear: year N → 0007
      • float: NaN

      Plain xsd:duration survives only because its decoder reads the string dictionary. The query-time novelty overlay builds the same (datatype type, string id) pair (value_to_otype_okey, fluree-db-query/src/binary_scan.rs:3765-3773). So once a ledger has any index, a newly written value reads back wrong immediately, with no rebuild. Novelty reads are correct only before the first index build.

    • Part 2 (can't retract): DELETE DATA builds its retraction the same way, as (String(lexical), xsd:date). That never matches a value stored as a real Date, which is anything written through Turtle, TriG or JSON-LD or computed by BIND. This happens with no index present, which matches what you saw:

    fluree insert x '<http://example.org/a> <http://example.org/d> "2026-01-01T09:15:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> .'
    fluree update x 'DELETE DATA { <http://example.org/a> <http://example.org/d> "2026-01-01T09:15:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> }'
    # Committed, 1 retraction (of a string flake that doesn't exist); the value is still there

    Same literal, SPARQL UPDATE vs Turtle

    Written with INSERT DATA and with fluree insert (Turtle), read with STR() before and after fluree index:

    datatype Turtle (indexed) SPARQL (before first index) SPARQL (indexed)
    xsd:integer, xsd:decimal, xsd:double, xsd:boolean, xsd:string, xsd:anyURI, xsd:token ok ok ok
    xsd:int / long / short / byte / nonNegativeInteger / unsignedInt 42 42 -9223372036854775807
    xsd:float 1.5E0 1.5 NaN
    xsd:dateTime 2026-01-01T09:15:00Z 2026-01-01T09:15:00Z <dateTime -9223372036854775805>
    xsd:date 2026-01-01 2026-01-01 1970-01-03
    xsd:time 09:15:00 09:15:00 00:00:00.00001
    xsd:gYear 2026 2026 0007
    xsd:gYearMonth 2026-01 2026-01 1431655766-01
    xsd:dayTimeDuration PT1H PT1H -P106751991DT4H54.775804S
    xsd:yearMonthDuration P1M P1M P11M
    xsd:duration P1D P1D P1D

    (STR() is itself unbound on the gYear/duration types, #1994, so those rows were read directly.)

    Even before the first index build, the string-stored values already behave differently:

    • An exact-match lookup ?s ex:d "…"^^xsd:dateTime misses them.
    • FILTER(?n > 3) misses an xsd:int.
    • HOURS() is unbound.
    • A DELETE DATA written in SPARQL won't retract a Turtle- or JSON-LD-written value, as above.

    After indexing, FILTER(?d > …) and FILTER(?d = …) stop matching rows they matched before, with no write in between.

    Turtle isn't entirely safe either. An invalid lexical such as "not-a-date"^^xsd:dateTime is accepted as a string with that datatype by Turtle insert as well as SPARQL UPDATE (JSON-LD rejects it), and it is corrupted the same way at index time.

    What a fix needs

    1. Lowering: coerce_typed_flake_value should call coerce_string_value, as tx.rs:2452 does. Its WHERE-side twin coerce_typed_value falls back the same way, but the query lowering re-coerces that side (fluree-db-query/src/parse/lower.rs:602), which is why DELETE WHERE patterns match canonically.
    2. Existing data: ledgers already written through SPARQL UPDATE hold these string flakes in their commits, and reindex replays commits, so (1) alone won't repair them. That matches your finding that reindex gives a byte-identical root. The index side should also refuse to decode a string payload as a native type. Options:
      • The LEX_ID arm of OTypeRegistry::resolve and value_to_otype_okey could coerce the lexical, or fall back to a string-dictionary type.
      • validate_value_dt_pair (fluree-db-transact/src/generate/flakes.rs:512), which coerce.rs calls the single bottleneck for value/datatype rules, could reject String paired with a natively encoded datatype on new writes. That doesn't help commits already on disk, though.
    3. Invalid lexicals: reject them at write time (the query side already errors on an invalid typed literal), or store them as strings that index and read back as strings.

    Workarounds until then

    Checked on v4.2.2 with an index built. The BIND insert keeps xsd:date, xsd:dateTime, xsd:long and xsd:int correctly typed through indexing:

    • Write typed literals through JSON-LD or Turtle, or through SPARQL BIND, which goes through the query-side lowering:
      INSERT { ex:a ex:date ?d } WHERE { BIND("2026-09-08"^^xsd:date AS ?d) }
    • Delete through a bound variable rather than the literal:
      DELETE { ex:a ex:date ?d } WHERE { BIND("2026-09-08"^^xsd:date AS ?d) ex:a ex:date ?d }
      Your FILTER(datatype(?d) = …) form works for the same reason.

    Related: #1974, where xsd:int is indexed under a custom datatype during an in-flight index build. It has a different trigger but the same kind of dictionary-id-read-as-value symptom.

  2. added
    bug
    P1
    area:transact
    on Sep 29, 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

    P1area:transactbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions