2121import java .math .RoundingMode ;
2222
2323/**
24- * Custom implementation of {@link BigDecimalSplitter} to ensures safe and precise division of BigDecimal values while
25- * calculating split points for NUMERIC and DECIMAL types.
24+ * Safe implementation of {@link BigDecimalSplitter} to ensure precise division of BigDecimal values while calculating
25+ * split points for NUMERIC and DECIMAL types.
26+ *
27+ * <p>Problem: The default {@link BigDecimalSplitter} implementation may return 0 when the numerator is smaller than the
28+ * denominator (e.g., 1 / 4 = 0), due to the lack of a defined scale for division. Since the result (0) is smaller than
29+ * {@link BigDecimalSplitter#MIN_INCREMENT} (i.e. {@code 10000 * Double.MIN_VALUE}), the split size defaults to
30+ * {@code MIN_INCREMENT}, leading to an excessive number of splits (~10M) and potential OOM errors.</p>
31+ *
32+ * <p>Fix: This implementation derives scale from column metadata, adds a buffer of 5 decimal places, and uses
33+ * {@link RoundingMode#HALF_UP} as the rounding mode.</p
34+ *
35+ * <p>Note: This class is used by {@link DataDrivenETLDBInputFormat}.</p>
2636 */
27- public class CustomBigDecimalSplitter extends BigDecimalSplitter {
37+ public class SafeBigDecimalSplitter extends BigDecimalSplitter {
2838
39+ /* An additional buffer of +5 digits is applied to preserve accuracy during division. */
2940 public static final int SCALE_BUFFER = 5 ;
3041 /**
3142 * Performs safe division with correct scale handling.
@@ -37,7 +48,7 @@ public class CustomBigDecimalSplitter extends BigDecimalSplitter {
3748 */
3849 @ Override
3950 protected BigDecimal tryDivide (BigDecimal numerator , BigDecimal denominator ) {
40- // Derive scale from numerator/denominator + buffer
51+ // Determine the required scale for the division and add a buffer to ensure accuracy
4152 int effectiveScale = Math .max (numerator .scale (), denominator .scale ()) + SCALE_BUFFER ;
4253 return numerator .divide (denominator , effectiveScale , RoundingMode .HALF_UP );
4354 }
0 commit comments