Skip to content

Commit

Permalink
[INLONG-11214][SDK] Modify the problem of incomplete division in Divi…
Browse files Browse the repository at this point in the history
…sionParser
  • Loading branch information
ZKpLo committed Sep 27, 2024
1 parent e0d7f8d commit 5927004
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import net.sf.jsqlparser.expression.operators.arithmetic.Division;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
* DivisionParser
*
*/
@TransformParser(values = Division.class)
public class DivisionParser implements ValueParser {
Expand All @@ -36,13 +36,16 @@ public class DivisionParser implements ValueParser {

private ValueParser right;

private final int DEFAULT_SCALE_DIFFERENCE = 4;

public DivisionParser(Division expr) {
this.left = OperatorTools.buildParser(expr.getLeftExpression());
this.right = OperatorTools.buildParser(expr.getRightExpression());
}

/**
* parse
*
* @param sourceData
* @param rowIndex
* @return
Expand All @@ -53,6 +56,11 @@ public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object rightObj = this.right.parse(sourceData, rowIndex, context);
BigDecimal leftValue = OperatorTools.parseBigDecimal(leftObj);
BigDecimal rightValue = OperatorTools.parseBigDecimal(rightObj);
return leftValue.divide(rightValue);
try {
return leftValue.divide(rightValue);
} catch (Exception e) {
int scale = Math.max(leftValue.scale(), rightValue.scale()) + DEFAULT_SCALE_DIFFERENCE;
return leftValue.divide(rightValue, scale, RoundingMode.HALF_UP);
}
}
}

0 comments on commit 5927004

Please sign in to comment.