Skip to content

Optimize the binaryToDecimal function in the DecimalUtils class #3146

Description

@qian0817

Describe the enhancement requested

https://github.com/apache/parquet-java/blob/master/parquet-pig/src/main/java/org/apache/parquet/pig/convert/DecimalUtils.java

publicstaticBigDecimalbinaryToDecimal(Binaryvalue, intprecision, intscale) {
/* * Precision <= 18 checks for the max number of digits for an unscaled long, * else treat with big integer conversion */if (precision <= 18) {
ByteBufferbuffer = value.toByteBuffer();
byte[] bytes = buffer.array();
intstart = buffer.arrayOffset() + buffer.position();
intend = buffer.arrayOffset() + buffer.limit();
longunscaled = 0L;
inti = start;
while (i < end) {
unscaled = (unscaled << 8 | bytes[i] & 0xff);
i++;
}
intbits = 8 * (end - start);
longunscaledNew = (unscaled << (64 - bits)) >> (64 - bits);
if (unscaledNew <= -pow(10, 18) || unscaledNew >= pow(10, 18)) {
returnnewBigDecimal(unscaledNew);
} else {
returnBigDecimal.valueOf(unscaledNew / pow(10, scale));
}
} else {
returnnewBigDecimal(newBigInteger(value.getBytes()), scale);
}
}

If precision is less than 18, the condition unscaledNew <= -pow(10, 18) || unscaledNew >= pow(10, 18) can not be true, so we can remove the judgment logic here. Additionally, using BigDecimal.valueOf(unscaledNew, scale) is preferable over using BigDecimal.valueOf(unscaledNew / pow(10, scale)), as it does not convert the unscaled value to double.

Component(s)

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions