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 31c5adf
Showing 1 changed file with 9 additions and 8 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,23 +36,24 @@ 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
*/
@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object leftObj = this.left.parse(sourceData, rowIndex, 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 31c5adf

Please sign in to comment.