Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-2099

Decimal precision is ignored

Add voteVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: spec
    • Labels:
      None

      Description

      According to the documentation https://avro.apache.org/docs/1.8.1/spec.html#Decimal

      The decimal logical type represents an arbitrary-precision signed decimal number of the form unscaled × 10-scale.

      Then in the schema we might have an entry like:

      {
        "type": "bytes",
        "logicalType": "decimal",
        "precision": 4,
        "scale": 2
      }
      

      However, in the java deserialization I see that the precision is ignored:

      https://github.com/apache/avro/blob/master/lang/java/avro/src/main/java/org/apache/avro/Conversions.java#L79

          @Override
          public BigDecimal fromBytes(ByteBuffer value, Schema schema, LogicalType type) {
            int scale = ((LogicalTypes.Decimal) type).getScale();
            // always copy the bytes out because BigInteger has no offset/length ctor
            byte[] bytes = new byte[value.remaining()];
            value.get(bytes);
            return new BigDecimal(new BigInteger(bytes), scale);
          }
      

      The logical type definition in the java api requires the precision to be set:

      https://github.com/apache/avro/blob/master/lang/java/avro/src/main/java/org/apache/avro/LogicalTypes.java#L116

        /** Create a Decimal LogicalType with the given precision and scale */
        public static Decimal decimal(int precision, int scale) {
          return new Decimal(precision, scale);
        }
      

      Is this a feature, that we allow arbitrary precision? If so, why do we have the precision in the API and schema, if it's ignored?

      Maybe that's some java specific issue?

      Thanks for any hints.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              KornelKielczewski Kornel Kiełczewski

              Dates

              • Created:
                Updated:

                Issue deployment