[
https://issues.apache.org/jira/browse/AVRO-3254?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Nathan Daniels updated AVRO-3254:
---------------------------------
Description:
*Problem*
When serializing an `AvroDecimal`, the Decimal class's `ConvertToBaseValue`
method will throw an exception if the scale of the decimal does not exactly
match the scale defined in the schema.
The exception goes something like
`The decimal value has a scale of \{scale} which cannot be encoded against a
logical 'decimal' with a scale of \{logicalScale}`
I feel this is far too restrictive and unintuitive from a usability
perspective. For example, it is not possible to serialize the c# decimal
`1.0m` into a schema field of precision 20 and scale 2, because `1.0` has a
"scale" of 1. Instead, the user must force the decimal to be `1.00m` if they
want to serialize despite the fact that 1.0 and 1.00 are the same exact real
number. Now the user needs to be aware of the scale defined in the schema
before attempting to serialize.
*Solution?*
I propose the `Decimal` and `AvroDecimal` classes be smart enough to re-scale a
value into the scale specified in the schema if doing so would not result in
data loss. It is relatively trivial operation to do so, and would make using
`AvroDecimal` much easier.
I will open a PR with my proposed improvement, but essentially the Decimal
class will convert the AvroDecimal to the desired scale prior to serialization,
and continue to throw the exception if doing so would result in data loss.
was:
*Problem*
When serializing an `AvroDecimal`, the Decimal class's `ConvertToBaseValue`
method will throw an exception if the scale of the decimal does not exactly
match the scale defined in the schema.
The exception goes something like
`The decimal value has a scale of
{scale}
which cannot be encoded against a logical 'decimal' with a scale of
{logicalScale}
`
I feel this is far too restrictive and unintuitive from a usability
perspective. For example, it is not possible to serialize the c# decimal
`1.0m` into a schema field of precision 20 and scale 2, because `1.0` has a
"scale" of 1. Instead, the user must force the decimal to be `1.00m` if they
want to serialize despite the fact that 1.0 and 1.00 are the same exact real
number. Now the user needs to be aware of the scale defined in the schema
before attempting to serialize.
*Solution?*
I propose the `Decimal` and `AvroDecimal` classes be smart enough to re-scale a
value into the scale specified in the schema if doing so would not result in
data loss. It is relatively trivial operation to do so, and would make using
`AvroDecimal` much easier.
I will open a PR with my proposed improvement, but essentially the Decimal
class will convert the AvroDecimal to the desired scale prior to serialization,
and continue to throw the exception if doing so would result in data loss.
> .NET - Automatically scale AvroDecimal values when serializing
> --------------------------------------------------------------
>
> Key: AVRO-3254
> URL: https://issues.apache.org/jira/browse/AVRO-3254
> Project: Apache Avro
> Issue Type: Improvement
> Components: csharp
> Reporter: Nathan Daniels
> Priority: Minor
>
> *Problem*
> When serializing an `AvroDecimal`, the Decimal class's `ConvertToBaseValue`
> method will throw an exception if the scale of the decimal does not exactly
> match the scale defined in the schema.
> The exception goes something like
> `The decimal value has a scale of \{scale} which cannot be encoded against a
> logical 'decimal' with a scale of \{logicalScale}`
> I feel this is far too restrictive and unintuitive from a usability
> perspective. For example, it is not possible to serialize the c# decimal
> `1.0m` into a schema field of precision 20 and scale 2, because `1.0` has a
> "scale" of 1. Instead, the user must force the decimal to be `1.00m` if they
> want to serialize despite the fact that 1.0 and 1.00 are the same exact real
> number. Now the user needs to be aware of the scale defined in the schema
> before attempting to serialize.
> *Solution?*
> I propose the `Decimal` and `AvroDecimal` classes be smart enough to re-scale
> a value into the scale specified in the schema if doing so would not result
> in data loss. It is relatively trivial operation to do so, and would make
> using `AvroDecimal` much easier.
> I will open a PR with my proposed improvement, but essentially the Decimal
> class will convert the AvroDecimal to the desired scale prior to
> serialization, and continue to throw the exception if doing so would result
> in data loss.
--
This message was sent by Atlassian Jira
(v8.20.1#820001)