Repository navigation
fix: read decimal text exactly or refuse it - #166
Merged
Merged
Conversation
decimal.Parse rounds a 29th significant digit, a scale beyond 28 and
1e-30 to the nearest representable value without a word, while
PostgreSQL's numeric keeps every digit. So
DecimalRange.Parse("[1.00000000000000000000000000001,2)") was a range
containing 1 in memory and not on the server,
DecimalSet.Parse("{1.00000000000000000000000000001,1}") had one element
here and two there, a JSON array read the same way, and the JSON
document and SQL constant written back carried the rounded number. Only
excess precision was folded in: a magnitude that does not fit
overflowed, loudly, as it still does.
DecimalLiteral reads the text's digits and exponent exactly and compares
them with the parsed decimal's mantissa and scale, so every spelling of
a number decimal holds passes (1e3, +1.5, 007.50, a trailing zero beyond
the 28th place) and only a different number is refused, with
FormatException. DecimalRange.ParseValue and DecimalSet.ParseValue share
it: the range side had the same decimal.Parse call, so both are fixed
here rather than leaving DecimalRange to the range-literal group, and
RangeFormat, IsoTemporal and NodaPatterns are untouched. DecimalSet<T>,
RangeSet<DecimalRange>, the TypeConverter and the EF Core JSON document
go through them. The primitive DecimalSet reads its JSON elements
through ParseValue whenever System.Text.Json's own decimal converter
would have, from the token's text — a quoted one only under
AllowReadingFromString, as that converter would — the way the temporal
sets already do.
Closes #115
Found-By: OT
Defect-Class: LIT
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Owner
Author
|
Whole integration project ( |
This was referenced Oct 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #115
Stacked on #165 (
fix/wrapper-round-trip) → #164 → #163 → #162; the diff againstmainincludes them until they merge. Last of the six element-grammar PRs for 10.0.0; the value-semantics review ran over the whole stack before it was opened (its one finding is the second commit of #164).Problem
decimal.Parserounds a 29th significant digit, a scale beyond 28 and1e-30to the nearest representable value without a word, while PostgreSQL'snumerickeeps every digit. So for the same text the value in memory was a different number from the server's:The JSON document and the SQL constant written back carried the rounded number. Only excess precision was folded in: a magnitude that does not fit overflowed, loudly, as it still does.
Change
src/CodoMetis.ValueRanges/Internals/DecimalLiteral.cs(new): PostgreSQL'snumericgrammar as before (NumberStyles.Float, invariant), then the text's digits and exponent read exactly (BigInteger) and compared with the parsed decimal's mantissa and scale. Every spelling of a numberdecimalholds passes (1e3,+1.5,007.50,1e-28, a trailing zero beyond the 28th place); only a different number is refused, withFormatExceptionnaming the rule.DecimalRange.ParseValueandDecimalSet.ParseValueshare it. The range side is fixed here too:DecimalRangehad the samedecimal.Parsecall, so a shared helper was the single place, and the candidate rows forDecimalRangeandRangeSet<DecimalRange>are removed with the set's.RangeFormat,IsoTemporalandNodaPatternsare untouched, so nothing overlaps the range-literal group (An inverted range literal parses as empty on every entry point; PostgreSQL rejects it #114, Minimal API binds a variant-typed parameter as null for a valid literal and answers 200 #116, A discrete range read as [x,infinity) loses 9999-12-31; UpperBound disagrees between memory and server #117, A temporal literal with sub-microsecond digits is a different value than PostgreSQL reads from the same text #126).DecimalSet<T>,RangeSet<DecimalRange>, theTypeConverter, configuration binding and the EF Core JSON document all reach the twoParseValues.ValueSetJsonConverter: the primitiveDecimalSetreads its JSON elements throughParseValuewhenever System.Text.Json's own decimal converter would have (the mechanism the temporal sets use), from the token's own text — a quoted one only underAllowReadingFromString, with the gate fix: read a wrapper set's JSON element tokens as its primitive set reads them #164 added, as that converter would. A converter registered fordecimalstill wins.LiteralCorpusProbeTestsrows for Decimal text beyond 28 significant digits is rounded silently when a literal or JSON array is read #115 leavecandidates.json; theDecimalSet/DecimalRangeclone waivers name the helper. Docs:docs/serialization.md#parse-a-literalparagraph with claimJ30-decimal-read-exactly-or-refused, adocs/value-sets.mdbullet, the 10.0.0 Breaking row, and "Decimal text is read exactly or refused" indocs/migration.md.Tests
DecimalPrecisionTests(new):TextDecimalWouldRound_IsRefused_AtEveryEntryPoint(7 texts ×DecimalRangeParse/TryParse string and span,RangeSet<DecimalRange>, JSON,TypeConverter,DecimalSetandDecimalSet<TestMoney>Parse/TryParse, JSON number and quoted number),TextDecimalHoldsExactly_Reads(12 spellings, scale kept),JsonNumber_IsReadFromItsOwnText,MagnitudeBeyondDecimal_StillOverflows.LiteralCorpusProbeTests(postgres:17): thebeyond 28 digitsfamily ofDecimalRange,RangeSet<DecimalRange>andDecimalSetnow counts as a domain refusal (the library refuses the server's canonical text too), as the probe's rules define it.DecimalLiteral.csdeleted,DecimalSet.cs,DecimalRange.csandValueSetJsonConverter.csfrom the parent commit, tests kept): 7 of the 21DecimalPrecisionTestsrows fail, the acceptance rows still pass. Restored afterwards.🤖 Generated with Claude Code