Skip to content

fix(core)!: a number is stored as its value, and an xsd:float at single precision - #2494

Open
bplatz wants to merge 3 commits into
nextfrom
fix/numeric-values-canonical
Open

bplatz wants to merge 3 commits into
nextfrom
fix/numeric-values-canonical

Conversation

@bplatz

@bplatz bplatz commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

A decimal now reads back without the trailing zeros it was written with, and a double's -0.0 reads back as 0.0, from the moment it is written. The index has always stored them that way. But a value written since the last index build was held as written, so a query printed 1.50 until the ledger was indexed and 1.5 after, with no write in between. Matching was already by value; only the printed form changed.

This also records decision D-11 (#1696): Fluree stores a number as its value, not its lexical form, so 01, +1 and 1 are one term. Integers and doubles already behaved this way. This PR brings decimals and -0.0 into line and documents the rule as a deviation.

It also settles #1776: an xsd:float is stored at single precision, the value XSD defines, so one float term has one spelling everywhere.

What changes

  • FlakeValue::decimal and FlakeValue::double build a value as it is stored: a normalized BigDecimal, and -0.0 folded to 0.0. Every place a number is read from text or a JSON number now goes through them:
    • write coercion (coerce_value, coerce_string_value, JSON numbers), which every JSON-LD, Turtle, TriG and bulk-import write uses;
    • bare Turtle and JSON-LD numbers (convert_native_literal), and import's raw doubles;
    • SPARQL literals in queries and in update templates, JSON-LD query constants, and Cypher constants;
    • the commit decoder, so a commit an earlier release wrote with 1.50 reads as 1.5;
    • flake deserialization (JSON and transport).
  • Arithmetic keeps the scale it computes (1.50 + 1.50 is 3.0), as before.
  • A graph source's cells keep the table's form: a decimal its column's scale, and a double its -0.0. A database's SUM, MIN and MAX return that form, so the SQL lane and the scan lane build the same terms.
  • An xsd:float is stored at single precision (Float rendering keys on the Double variant, not the declared xsd:float datatype — one term, three spellings #1776). It was stored at double precision, and each reader spelled it from whichever half it looked at. "0.1234567890123"^^xsd:float serialized as 1.234567890123E-1, STR() of an expression gave 1.2345679E-1, and a GROUP BY key 0.12345679. "3.4e39"^^xsd:float was finite, though its cast is INF.
    • FlakeValue::float stores a float as the double nearest its shortest decimal form (float_as_stored_double). Every printer of the double prints the float's canonical form, and as f32 gives the float back. A float written as the shortest form of a float value, such as 0.5 or 3.14, is stored as before. So a bare numeric constant still matches it, and an index an earlier release built differs only for floats with more digits than single precision keeps.
    • Every f32 was checked once, outside the suite: the stored double converts back, prints the float's shortest form and keeps float order. The exceptions are ±7.038531e-26, whose nearest double converts to the next float. Those two are stored one double step toward the float. canonical_xsd_float is the canonical form of the stored double, so STR() and the serializers still agree on them.
    • Write coercion and query constants go through it. The commit retag reads a float committed at double precision as the float, so novelty, a reload and a ledger's first 5.0 index build (a full rebuild from commits) all hold it at single precision.
    • A float an expression computes (a cast, arithmetic, a group key) is minted as the stored value, not as text. It joins, groups and prints as a stored float: xsd:float("0.5") matches a stored 0.5, ?f + 0 prints 1.0E0 (was 1), and JSON-LD prints {"@value": 2.5, "@type": "xsd:float"} (was a string @value). xsd:string() keeps the XPath cast form. stored_float_f64 and STR()'s fast path, which read the double off the binding to agree with the serializer, are gone.
  • Docs:
    • a deviation in compatibility.md (RDF 1.1), and behaviour-change entries for decimals and for floats. The float entry covers the window before a ledger's first 5.0 index build, when an earlier index still holds a float with more digits than single precision keeps;
    • a note in datatypes.md;
    • the D-11 answer in sparql-compliance.md.
  • W3C registers: the comments on group01, the D6 expression tests and the numeric-lexical Turtle/TriG tests now say they are a decided deviation. No entry was added or removed.

Cost

Write path: one normalized() per decimal, one comparison per double, and one format and parse per float. Staging and the index encoder already normalize every decimal they hash or key. The commit decoder adds one datatype comparison per double it reads. A float an expression mints takes one format and parse, in place of the text it formatted and allocated before. On the read path, the only other cost is parsing a query constant.

Tests

  • it_numeric_storage_canonical:
    • every write lane renders the same as written and once indexed. The lanes are SPARQL INSERT DATA and INSERT … WHERE, JSON-LD insert and upsert, Turtle insert and upsert, TriG, graph sync, Transaction, push and bulk import. The values are "1.50", "100.00", "-0.00", a bare 1.50, "-0.0"^^xsd:double, a bare -0.0e0, "0.1234567890123"^^xsd:float and "3.4e39"^^xsd:float (a JSON number in the JSON-LD lanes);
    • a reload from commits;
    • an update storing a constant its WHERE binds (VALUES, BIND, JSON-LD values);
    • SPARQL and JSON-LD query constants, a float lexical and a JSON number included, and 1.50 + 1.50 keeping scale 1;
    • a JSON-LD query before and after indexing;
    • a_computed_float_is_the_stored_float: a cast joins the stored float, and arithmetic and a group key print as it does, in SPARQL and in JSON-LD.
  • op_codec::decoding_reads_a_number_as_its_value: a commit holding 1.50, 100.00, -0.0 and a float at double precision, as an earlier release wrote them, decodes to the stored value. A double keeps its precision.
  • raw_reader::a_raw_float_reads_at_single_precision: the same through the raw reader that index builds use.
  • fluree-graph-ir: a strided walk over f32 bit patterns, and the two exceptions.
  • Flipped: fix(query): STR() on a double returns the canonical XSD lexical form #1750's STR() float tests, a_float_beyond_its_range_is_stored_as_inf (was …_stored_at_double_precision), and three tests that pinned a computed float's string @value in JSON-LD.
  • it_decimal_exactness's novelty-vs-indexed differential now compares exactly. Its canon_decimal_bindings helper, which hid this difference, is gone.

Non-vacuity:

  • Against next without the fix, the new tests fail: every lane rendered 1.50 / -0.0E0 as written and 1.5 / 0.0E0 once indexed.
  • Each of these, reverted on its own, fails a new test: the commit decoder, convert_native_literal, the SPARQL update template decimal, and the SPARQL query double.
  • Floats, each reverted on its own:
    • float coercion parsing at double precision: every write lane fails as written. The serializer prints 1.234567890123E-1 and 3.4E39, and STR() prints 1.2345679E-1 and INF;
    • the commit retag: both decode tests fail;
    • a computed float minted as text: a_computed_float_is_the_stored_float fails, and the cast joins nothing.
  • The query AST's decimal arms in fluree-db-query/src/parse/lower.rs go through the constructor for uniformity, but reverting them changes nothing observable. They lower pattern constants in a SPARQL update's WHERE, which match by value and are never stored.

Fixes #1696
Fixes #2090. This is its last open item, the decimal scale and -0.0. Its re-upsert and sub-microsecond items were fixed by #2109, and its big-integer items by #2477. Its xsd:float precision item moved to #1776, settled here.
Fixes #1776

…ores

A decimal written since the last index build kept the scale it was
written with, and a double kept the sign of -0.0, so a query printed
1.50 or -0.0E0 until the ledger was indexed and 1.5 or 0.0E0 after,
with no write in between. Matching was already by value; only the
printed form changed.

FlakeValue::decimal (normalized) and FlakeValue::double (-0.0 as 0.0)
build a number as the index keys it, and every place a number is read
from text or a JSON number goes through them: write coercion, bare
Turtle and JSON-LD numbers, SPARQL, JSON-LD and Cypher constants,
import's raw doubles, flake deserialization, and the commit decoder,
which reads a commit an earlier release wrote with 1.50 as 1.5.
Arithmetic keeps the scale it computes.

This settles D-11 as a documented deviation: Fluree stores a number as
its value, not its lexical form, so 01, +1 and 1 are one term.
compatibility.md, datatypes.md and sparql-compliance.md say so, and the
W3C register entries that tell such terms apart say they are decided.
A graph source's cells reached terms through the same coercion a write
uses, so they picked up the write's normalization: a decimal(10,2)
column's 99.50 read as 99.5, and a double's -0.0 as 0.0. A database's
SUM, MIN and MAX return the table's form, so the SQL lane printed 104.50
where the scan lane printed 104.5, and a pinned decimal read back without
its reported scale.

The literal encoder now parses a decimal or double cell as the table
holds it, as it did before. compatibility.md already said a graph
source's decimal keeps its column's scale; it now says the same of -0.0.

The set-semantics suite compares a graph source with a ledger holding
the same triples. A ledger stores 1.00 as 1, so its GROUP BY over a
decimal compares the printed values as numbers. The Python round trip
expects a ledger's 1.50 back as 1.5.
An xsd:float was stored at double precision, tagged only by its
datatype, and each reader took its spelling from whichever half it
looked at. "0.1234567890123"^^xsd:float serialized as
1.234567890123E-1 while arithmetic and STR() of an expression used the
float, 1.2345679E-1; a GROUP BY key minted a third spelling,
0.12345679; and "3.4e39"^^xsd:float was a finite value that its own
cast made INF.

A float is now stored as its single-precision value, held as the
double nearest the float's shortest decimal form
(float_as_stored_double). A printer of the double prints the float's
canonical form, `as f32` gives the float back, and order is kept. A
float written as the shortest form of a float value, such as 0.5 or
3.14, is stored as before, so a bare numeric constant still matches it.
Every f32 was checked: for +-7.038531e-26 the nearest double converts
to the next float, so they are stored one double step toward it.
canonical_xsd_float is the canonical form of the stored double, so
STR() and the serializers agree for those two as well.

FlakeValue::float builds the value, and write coercion goes through
it for a lexical, a JSON number, an integer or a double under
xsd:float, as do query constants. The commit retag reads a float that
an earlier release committed at double precision as the float, so
novelty, a reload and a ledger's first 5.0 index build, which rebuilds
from commits, all hold it at single precision.

A float an expression computes (a cast, arithmetic, a group key) is
minted as the stored value rather than as text. It joins, groups and
prints as a stored float: xsd:float("0.5") matches a stored 0.5, and
?f + 0 prints 1.0E0, not 1. xsd:string() keeps the XPath cast form.
stored_float_f64 and STR()'s fast path, which read the double off the
binding to agree with the serializer, are gone.

compatibility.md gives the behaviour change and the window before a
ledger's first 5.0 index build, when an earlier index still holds a
float with more digits than single precision keeps.
@bplatz bplatz changed the title fix(core): a decimal and a double are read as the values the index stores fix(core)!: a number is stored as its value, and an xsd:float at single precision Nov 25, 2026
@bplatz bplatz added the breaking-change Backwards-incompatible change; drives the Breaking Changes release-notes section label Nov 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change Backwards-incompatible change; drives the Breaking Changes release-notes section bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant