Skip to content

NumericDecodeUtils.decodeBinary(byteBuf) returns wrong result for BigDecimal between 1 and -1 #556

Closed
@alphaville76

Description

@alphaville76

Bug Report

Versions

  • Driver: master
  • Database:
  • Java: 11
  • OS: Linux

Current Behavior

The BigDecimal 0.99 is decoded as 9900.00. The problem occurs for each big decimal between -1 and 1.
In the other cases works correctly.

Steps to reproduce

To reproduce, please run
short[] array = new short[]{1, -1, 0, 2, 9900}

        ByteBuf byteBuf = TestByteBufAllocator.TEST.buffer();
        //BigDecimal 0.99
        short[] array = new short[]{1, -1, 0, 2, 9900};
        for (int i = 0; i < array.length; i++) {
            byteBuf.writeShort(array[i]);
        }

        BigDecimal bigDecimal = NumericDecodeUtils.decodeBinary(byteBuf);

        System.out.println(bigDecimal);

Expected behavior/code

0.99 istead of 9900.00

Possible Solution

The problem is that a negative weight is not handled.
As stated in https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/numeric.c#L263

Note: the first digit of a NumericVar's value is assumed to be multiplied
by NBASE ** weight. Another way to say it is that there are weight+1
digits before the decimal point. It is possible to have weight < 0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions