Skip to content

Support .NET 11 Decimal128/Decimal64/Decimal32 for DECFLOAT - #1308

Open
fdcastel wants to merge 9 commits into
FirebirdSQL:masterfrom
fdcastel:decfloat-decimal128
Open

fdcastel wants to merge 9 commits into
FirebirdSQL:masterfrom
fdcastel:decfloat-decimal128

Conversation

@fdcastel

@fdcastel fdcastel commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Part of #1302. Based on #1307 and #1303.

Summary

On net11.0, the .NET 11 IEEE 754 decimal types Decimal32, Decimal64 and Decimal128 can be used with DECFLOAT. Existing behavior is unchanged.

Their decimal interchange encoding (EncodeDecimal/DecodeDecimal, densely packed decimal) is the DECFLOAT wire encoding: the provider's DecimalCodec and .NET agree bit for bit. So all conversions are exact and go through the existing codec, with no text round trip.

Changes (all under #if NET11_0_OR_GREATER)

  • FbDecFloat: explicit conversions from Decimal32/Decimal64/Decimal128, which are exact, and to Decimal64/Decimal128, which are exact or throw OverflowException when the value has too many digits or its exponent is out of range.
  • TypeHelper.GetFbDataTypeFromType: Decimal128 maps to FbDbType.Dec34; Decimal64 and Decimal32 map to FbDbType.Dec16.
  • FbParameter.InternalValue, through which every parameter value reaches DbValue (FbCommand and FbBatchCommand, sync and async), converts IEEE decimal values to FbDecFloat, so every existing conversion handles them. A Decimal128 sent to DECFLOAT(16) works when it fits exactly and throws otherwise. Decoding rows is untouched: DbValue's constructors and SetValue are unchanged.
  • DbValue: adds GetDecimal128 and GetDecimal64.
  • FbDataReader.GetFieldValue<Decimal128> and <Decimal64> (sync and async), for DECFLOAT and the other numeric types.
  • GetValue/GetFieldType still return FbDecFloat; see Add DecFloat Type connection option to return DECFLOAT as Decimal64/Decimal128 #1309 for the opt-in.

Tests (net11.0 only)

  • FbDecFloatIeee754Tests (NoServer):
    • a seeded check over 20,000 random values per format: the provider's codec and Decimal64/Decimal128.EncodeDecimal agree bit for bit, and the conversions keep the coefficient and exponent,
    • all special values, bit for bit,
    • conversions that must throw.
  • FbDecimalIeee754SupportTests:
    • reading DECFLOAT(16)/(34) as Decimal64/Decimal128, bit-exact for limits, cohort, -0 and ±Inf; NaN and sNaN read as NaN,
    • Decimal64 from DECFLOAT(34): exact, or throws,
    • other numeric types read as Decimal128,
    • Decimal128/Decimal64/Decimal32 parameters: type inference, bit-exact round trip, and narrowing that throws while the connection stays usable,
    • insert into and select from a table.
  • FbBatchCommandTests (managed client only, like the other batch tests): Decimal64/Decimal128 batch parameters, bit-exact round trip. Verified locally against Firebird 5.0.4 with a probe that does the same: AllSuccess=True, both rows bit-exact.
  • Embedded + NoServer suite: net10.0 834 passed and net11.0 909 passed, 0 failed.

Known gap (follow-up)

Combined with #1305, GetFieldValue<Decimal128>() on a NUMERIC(38) value beyond System.Decimal throws, even when the value has 34 digits or fewer. Verified on a merge of both branches, embedded client:

cast('123456789012345678901234567890.1234' as numeric(38, 4)): GetFieldValue<Decimal128> => System.InvalidCastException: Unable to cast object of type 'FirebirdSql.Data.Common.BigDecimal' to type 'FirebirdSql.Data.Types.FbDecFloat'.
cast('1234.5' as numeric(38, 4)): GetFieldValue<Decimal128> = 1234.5

Whichever of the two PRs is merged second can add the BigDecimal case to DbValue.GetDecFloat.

…rmat

DecimalCodec.EncodeDecimal wrote coefficients with more digits than the
format allows, and exponents outside its range, without any check (only a
Debug.Assert). In Release builds the excess bits overflowed into the
combination/exponent fields and the server stored a different value, e.g.:

  12345678901234567890123456789012345 -> DECFLOAT(34) stored 8345678901234567890123456789012345
  30000000000000004E-17               -> DECFLOAT(16) stored 0.08000000000000004
  1E400                               -> DECFLOAT(16) stored 8.000000000000001E-353

The encoder now brings the value into the format's ranges without changing
its numeric value (dropping trailing zeros of the coefficient, folding the
exponent into the coefficient, clamping the exponent of zero), and throws
OverflowException when the value cannot be represented exactly (too many
significant digits, exponent overflow or underflow). Values that previously
got corrupted although representable (e.g. 1E384 in DECFLOAT(16)) now work.
The conversions formatted the value with a fixed custom format and then
removed the decimal point, assuming one was present. This threw
ArgumentOutOfRangeException for every value without a fractional part,
e.g. (FbDecFloat)1m, (FbDecFloat)100m, (FbDecFloat)2.0, and for values
smaller than the format's decimal places, e.g. (FbDecFloat)1e-20. It also
dropped the scale of decimals (1.10m became 1.1) and digits beyond 15
decimal places of doubles (7 for floats).

- decimal: converted exactly from its 96-bit integer and scale, so the
  value and its scale are preserved (1.10m -> 110E-2), including -0.
- double/float: converted to the shortest decimal that round-trips to the
  same binary value ("R" format), e.g. 0.1 -> 1E-1, 0.1 + 0.2 ->
  30000000000000004E-17, 1e-20 -> 1E-20; -0.0 -> negative zero.

A converted double may have up to 17 significant digits; sending it to a
DECFLOAT(16) throws OverflowException instead of being silently rounded
(this relies on the encoder range check of the previous commit).
Random.NextInt64() never returns a negative value, so the random doubles
were all positive. Use the full long range and assert both signs are covered.
Parameters: a DECFLOAT parameter only accepted FbDecFloat; any other value
(decimal, double, int, ...) threw InvalidCastException, even when the
parameter was created by AddWithValue with a decimal. Values of type
decimal, double, float, the integral types and BigInteger are now converted
to FbDecFloat (decimal exactly, double/float to the shortest round-trip
value). A value that does not fit the DECFLOAT precision exactly throws
OverflowException instead of being rounded.

Reader: GetDecimal, GetDouble, GetFloat, GetInt16/32/64 and GetByte (and
the matching GetFieldValue<T>) on a DECFLOAT column threw
InvalidCastException because FbDecFloat is not IConvertible. Now:

- GetDecimal: exact, keeping the scale (1.10 -> 1.10m); OverflowException
  when the value cannot be represented exactly (too large, too many digits,
  NaN, infinity).
- GetDouble/GetFloat: nearest value, like on NUMERIC columns; NaN and
  infinities are mapped, out-of-range values become infinity or zero.
- Integer getters: nearest integer, ties to even, as Convert does for
  decimal columns; OverflowException when out of range, NaN or infinity.

FbDecFloat gets the matching public explicit conversions to decimal, double
and float.
- Provider: add net11.0 to TargetFrameworks. No Microsoft.Extensions.Logging.Abstractions
  package reference is needed for net11.0: the assembly ships in the .NET 11 shared
  framework, and NuGet prunes the reference (the nuspec net11.0 group is empty).
- Tests: multi-target FirebirdSql.Data.FirebirdClient.Tests to net10.0;net11.0 so
  TFM-specific code paths are exercised on both.
- tests.ps1: copy the Firebird binaries into, and run the FirebirdClient suites
  from, every provider test TFM output.
- CI: install the .NET 11 SDK (preview quality until 11.0 GA).
.NET 11 introduces the IEEE 754 decimal floating-point types Decimal32,
Decimal64 and Decimal128. Their decimal interchange encoding
(EncodeDecimal/DecodeDecimal, densely packed decimal) is the encoding of
Firebird's DECFLOAT(16)/DECFLOAT(34): the provider's DECFLOAT codec and
.NET agree bit for bit (asserted by a seeded test over random values and all
special values), so the conversions are exact, including the exponent
(1.10 stays 1.10), negative zero, infinities and NaNs, with no text round
trip.

On net11.0 only:

- FbDecFloat: explicit conversions from Decimal32/Decimal64/Decimal128
  (exact) and to Decimal64/Decimal128 (exact, OverflowException when the
  value does not fit).
- Parameters: Decimal128 values infer FbDbType.Dec34, Decimal64 and
  Decimal32 infer FbDbType.Dec16. They can be passed to any numeric
  parameter; a value that does not fit the target exactly (e.g. a 34-digit
  Decimal128 into DECFLOAT(16)) throws OverflowException instead of being
  rounded.
- Reader: GetFieldValue<Decimal128>() and GetFieldValue<Decimal64>() for
  DECFLOAT and the other numeric types (exact, OverflowException when the
  value does not fit).

GetValue()/GetFieldType() still return FbDecFloat for DECFLOAT columns.
Every parameter value reaches DbValue through FbParameter.InternalValue
(FbCommand and FbBatchCommand, sync and async), so Decimal32/64/128 values
are converted to FbDecFloat there. DbValue no longer checks the type of every
value it stores: both clients store decoded row values in DbValue (the managed
client through the constructors, the native client through SetValue), and
those values are never IEEE decimals. DbValue's constructors and SetValue are
back to their previous code.

Adds a batch command test (Decimal64/Decimal128 parameters, bit-exact round
trip), as FbBatchCommand takes the same path.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant