Skip to content

Commit

Permalink
Revert unrelated changes
Browse files Browse the repository at this point in the history
Signed-off-by: Nghia Truong <[email protected]>
  • Loading branch information
ttnghia committed Jul 20, 2024
1 parent 30b80e0 commit 90492aa
Showing 1 changed file with 45 additions and 55 deletions.
100 changes: 45 additions & 55 deletions src/main/java/com/nvidia/spark/rapids/jni/DecimalUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
* Copyright (c) 2022-2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,12 +31,10 @@ public class DecimalUtils {
* Multiply two DECIMAL128 columns together into a DECIMAL128 product rounded to the specified
* scale with overflow detection. This method considers a precision greater than 38 as overflow
* even if the number still fits in a 128-bit representation.
* <p>
*
* WARNING: This method has a bug which we match with Spark versions before 3.4.2,
* 4.0.0, 3.5.1. Consider the following example using Decimal with a precision of 38 and scale
* of 10:
* -8533444864753048107770677711.1312637916 * -12.0000000000 = 102401338377036577293248132533
* .575166
* 4.0.0, 3.5.1. Consider the following example using Decimal with a precision of 38 and scale of 10:
* -8533444864753048107770677711.1312637916 * -12.0000000000 = 102401338377036577293248132533.575166
* while the actual answer based on Java BigDecimal is 102401338377036577293248132533.575165
*
* @param a factor input, must match row count of the other factor input
Expand All @@ -55,43 +53,37 @@ public static Table multiply128(ColumnView a, ColumnView b, int productScale) {
* Multiply two DECIMAL128 columns together into a DECIMAL128 product rounded to the specified
* scale with overflow detection. This method considers a precision greater than 38 as overflow
* even if the number still fits in a 128-bit representation.
* <p>
* WARNING: With interimCast set to true, this method has a bug which we match with Spark
* versions before 3.4.2,
* 4.0.0, 3.5.1. Consider the following example using Decimal with a precision of 38 and scale
* of 10:
* -8533444864753048107770677711.1312637916 * -12.0000000000 = 102401338377036577293248132533
* .575166
*
* WARNING: With interimCast set to true, this method has a bug which we match with Spark versions before 3.4.2,
* 4.0.0, 3.5.1. Consider the following example using Decimal with a precision of 38 and scale of 10:
* -8533444864753048107770677711.1312637916 * -12.0000000000 = 102401338377036577293248132533.575166
* while the actual answer based on Java BigDecimal is 102401338377036577293248132533.575165
*
* @param a factor input, must match row count of the other factor input
* @param b factor input, must match row count of the other factor input
* @param a factor input, must match row count of the other factor input
* @param b factor input, must match row count of the other factor input
* @param productScale scale to use for the product type
* @param interimCast whether to cast the result of the division to 38 precision before
* casting it again to the final
* precision
* @param interimCast whether to cast the result of the division to 38 precision before casting it again to the final
* precision
* @return table containing a boolean column and a DECIMAL128 product column of the specified
* scale. The boolean value will be true if an overflow was detected for that row's
* DECIMAL128 product value. A null input row will result in a corresponding null output
* row.
* scale. The boolean value will be true if an overflow was detected for that row's
* DECIMAL128 product value. A null input row will result in a corresponding null output
* row.
*/
public static Table multiply128(ColumnView a, ColumnView b, int productScale,
boolean interimCast) {
public static Table multiply128(ColumnView a, ColumnView b, int productScale, boolean interimCast) {
return new Table(multiply128(a.getNativeView(), b.getNativeView(), productScale, interimCast));
}

/**
* Divide two DECIMAL128 columns and produce a DECIMAL128 quotient rounded to the specified
* scale with overflow detection. This method considers a precision greater than 38 as overflow
* even if the number still fits in a 128-bit representation.
*
* @param a factor input, must match row count of the other factor input
* @param b factor input, must match row count of the other factor input
* @param a factor input, must match row count of the other factor input
* @param b factor input, must match row count of the other factor input
* @param quotientScale scale to use for the quotient type
* @return table containing a boolean column and a DECIMAL128 quotient column of the specified
* scale. The boolean value will be true if an overflow was detected for that row's
* DECIMAL128 quotient value. A null input row will result in a corresponding null output
* row.
* scale. The boolean value will be true if an overflow was detected for that row's
* DECIMAL128 quotient value. A null input row will result in a corresponding null output
* row.
*/
public static Table divide128(ColumnView a, ColumnView b, int quotientScale) {
return new Table(divide128(a.getNativeView(), b.getNativeView(), quotientScale, false));
Expand All @@ -111,9 +103,9 @@ public static Table divide128(ColumnView a, ColumnView b, int quotientScale) {
* @param a factor input, must match row count of the other factor input
* @param b factor input, must match row count of the other factor input
* @return table containing a boolean column and a INT64 quotient column.
* The boolean value will be true if an overflow was detected for that row's
* INT64 quotient value. A null input row will result in a corresponding null output
* row.
* The boolean value will be true if an overflow was detected for that row's
* INT64 quotient value. A null input row will result in a corresponding null output
* row.
*/
public static Table integerDivide128(ColumnView a, ColumnView b) {
return new Table(divide128(a.getNativeView(), b.getNativeView(), 0, true));
Expand All @@ -123,17 +115,17 @@ public static Table integerDivide128(ColumnView a, ColumnView b) {
* Divide two DECIMAL128 columns and produce a DECIMAL128 remainder with overflow detection.
* Example:
* 451635271134476686911387864.48 % -961.110 = 775.233
* <p>
*
* Generally, this will never really overflow unless in the divide by zero case.
* But it will detect an overflow in any case.
*
* @param a factor input, must match row count of the other factor input
* @param b factor input, must match row count of the other factor input
* @param a factor input, must match row count of the other factor input
* @param b factor input, must match row count of the other factor input
* @param remainderScale scale to use for the remainder type
* @return table containing a boolean column and a DECIMAL128 remainder column.
* The boolean value will be true if an overflow was detected for that row's
* DECIMAL128 remainder value. A null input row will result in a corresponding null
* output row.
* The boolean value will be true if an overflow was detected for that row's
* DECIMAL128 remainder value. A null input row will result in a corresponding null
* output row.
*/
public static Table remainder128(ColumnView a, ColumnView b, int remainderScale) {
return new Table(remainder128(a.getNativeView(), b.getNativeView(), remainderScale));
Expand All @@ -143,17 +135,17 @@ public static Table remainder128(ColumnView a, ColumnView b, int remainderScale)
* Subtract two DECIMAL128 columns and produce a DECIMAL128 result rounded to the specified
* scale with overflow detection. This method considers a precision greater than 38 as overflow
* even if the number still fits in a 128-bit representation.
* <p>
*
* NOTE: This is very specific to Spark 3.4. This method is incompatible with previous versions
* of Spark. We don't need this for versions prior to Spark 3.4
*
* @param a input, must match row count of the other input
* @param b input, must match row count of the other input
* @param a input, must match row count of the other input
* @param b input, must match row count of the other input
* @param targetScale scale to use for the result
* @return table containing a boolean column and a DECIMAL128 result column of the specified
* scale. The boolean value will be true if an overflow was detected for that row's
* DECIMAL128 result value. A null input row will result in a corresponding null output
* row.
* scale. The boolean value will be true if an overflow was detected for that row's
* DECIMAL128 result value. A null input row will result in a corresponding null output
* row.
*/

public static Table subtract128(ColumnView a, ColumnView b, int targetScale) {
Expand All @@ -168,17 +160,17 @@ public static Table subtract128(ColumnView a, ColumnView b, int targetScale) {
* Add two DECIMAL128 columns and produce a DECIMAL128 result rounded to the specified
* scale with overflow detection. This method considers a precision greater than 38 as overflow
* even if the number still fits in a 128-bit representation.
* <p>
*
* NOTE: This is very specific to Spark 3.4. This method is incompatible with previous versions
* of Spark. We don't need this for versions prior to Spark 3.4
*
* @param a input, must match row count of the other input
* @param b input, must match row count of the other input
* @param a input, must match row count of the other input
* @param b input, must match row count of the other input
* @param targetScale scale to use for the sum
* @return table containing a boolean column and a DECIMAL128 sum column of the specified
* scale. The boolean value will be true if an overflow was detected for that row's
* DECIMAL128 result value. A null input row will result in a corresponding null output
* row.
* scale. The boolean value will be true if an overflow was detected for that row's
* DECIMAL128 result value. A null input row will result in a corresponding null output
* row.
*/
public static Table add128(ColumnView a, ColumnView b, int targetScale) {
if (java.lang.Math.abs(a.getType().getScale() - b.getType().getScale()) > 77) {
Expand Down Expand Up @@ -207,7 +199,7 @@ public CastFloatToDecimalResult(ColumnVector result, boolean hasFailure) {
/**
* Cast floating point values to decimals, matching the behavior of Spark.
*
* @param input The input column, which is either FLOAT32 or FLOAT64
* @param input The input column, which is either FLOAT32 or FLOAT64
* @param outputType The output decimal type
* @return The decimal column resulting from the cast operation and a boolean column indicating
* whether the cast operation has failed for any input rows
Expand All @@ -220,11 +212,9 @@ public static CastFloatToDecimalResult floatingPointToDecimal(ColumnView input,
return new CastFloatToDecimalResult(new ColumnVector(result[0]), result[1] != 0);
}

private static native long[] multiply128(long viewA, long viewB, int productScale,
boolean interimCast);
private static native long[] multiply128(long viewA, long viewB, int productScale, boolean interimCast);

private static native long[] divide128(long viewA, long viewB, int quotientScale,
boolean isIntegerDivide);
private static native long[] divide128(long viewA, long viewB, int quotientScale, boolean isIntegerDivide);

private static native long[] remainder128(long viewA, long viewB, int remainderScale);

Expand Down

0 comments on commit 90492aa

Please sign in to comment.