morrySnow commented on code in PR #40744: URL: https://github.com/apache/doris/pull/40744#discussion_r1766236980
########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DecimalV3Literal.java: ########## @@ -84,6 +84,12 @@ public DecimalV3Literal roundFloor(int newScale) { value.setScale(newScale, RoundingMode.FLOOR)); } + public DecimalV3Literal round(int newScale) { + return new DecimalV3Literal(DecimalV3Type + .createDecimalV3Type(((DecimalV3Type) dataType).getPrecision(), newScale), Review Comment: > when new scale is larger than original one, it would throw an Invocationexception when calling create Decimal Type in java with message: precision should not smaller than scale and ExpressionEvaluator would catch this exception and go back to original expression. But actually when new scalar bigger than old one, we can return original one when folding constant. Same as be calculation logic we should not rely on exception, exception is very expensive, so we should process it without use exception ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java: ########## @@ -596,4 +650,472 @@ public static Expression divideDecimalV3(DecimalV3Literal first, DecimalV3Litera return new DecimalV3Literal(DecimalV3Type.createDecimalV3TypeLooseCheck( t1.getPrecision(), t1.getScale() - t2.getScale()), result); } + + /** + * coalesce + */ + @ExecFunction(name = "coalesce") + public static Expression coalesce(Literal first, Literal... second) { + if (!(first instanceof NullLiteral)) { + return first; + } + for (Literal secondLiteral : second) { + if (!(secondLiteral instanceof NullLiteral)) { + return secondLiteral; + } + } + return first; + } + + /** + * round + */ + @ExecFunction(name = "round") + public static Expression round(DecimalV3Literal first) { + return first.round(0); + } + + /** + * round + */ + @ExecFunction(name = "round") + public static Expression round(DecimalV3Literal first, IntegerLiteral second) { + return first.round(second.getValue()); + } + + /** + * round + */ + @ExecFunction(name = "round") + public static Expression round(DoubleLiteral first) { + DecimalV3Literal middleResult = new DecimalV3Literal(new BigDecimal(Double.toString(first.getValue()))); + return new DoubleLiteral(middleResult.round(0).getDouble()); + } + + /** + * round + */ + @ExecFunction(name = "round") + public static Expression round(DoubleLiteral first, IntegerLiteral second) { + DecimalV3Literal middleResult = new DecimalV3Literal(new BigDecimal(Double.toString(first.getValue()))); + return new DoubleLiteral(middleResult.round(second.getValue()).getDouble()); + } + + /** + * ceil + */ + @ExecFunction(name = "ceil") + public static Expression ceil(DecimalV3Literal first) { + return first.roundCeiling(0); + } + + /** + * ceil + */ + @ExecFunction(name = "ceil") + public static Expression ceil(DecimalV3Literal first, IntegerLiteral second) { + return first.roundCeiling(second.getValue()); Review Comment: move if into `roundCeiling` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java: ########## @@ -596,4 +650,515 @@ public static Expression divideDecimalV3(DecimalV3Literal first, DecimalV3Litera return new DecimalV3Literal(DecimalV3Type.createDecimalV3TypeLooseCheck( t1.getPrecision(), t1.getScale() - t2.getScale()), result); } + + /** + * coalesce + */ + @ExecFunction(name = "coalesce") + public static Expression coalesce(Literal first, Literal... second) { + if (!(first instanceof NullLiteral)) { + return first; + } + for (Literal secondLiteral : second) { + if (!(secondLiteral instanceof NullLiteral)) { + return secondLiteral; + } + } + return first; + } + + /** + * round + */ + @ExecFunction(name = "round") + public static Expression round(DecimalV3Literal first) { + return first.round(0); + } + + /** + * round + */ + @ExecFunction(name = "round") + public static Expression round(DecimalV3Literal first, IntegerLiteral second) { + if (second.getValue() >= first.getValue().scale()) { + return first; + } + return first.round(second.getValue()); + } + + /** + * round + */ + @ExecFunction(name = "round") + public static Expression round(DoubleLiteral first) { + DecimalV3Literal middleResult = new DecimalV3Literal(new BigDecimal(Double.toString(first.getValue()))); + return new DoubleLiteral(middleResult.round(0).getDouble()); + } + + /** + * round + */ + @ExecFunction(name = "round") + public static Expression round(DoubleLiteral first, IntegerLiteral second) { + DecimalV3Literal middleResult = new DecimalV3Literal(new BigDecimal(Double.toString(first.getValue()))); + if (second.getValue() >= middleResult.getValue().scale()) { + return first; + } + return new DoubleLiteral(middleResult.round(second.getValue()).getDouble()); + } + + /** + * ceil + */ + @ExecFunction(name = "ceil") + public static Expression ceil(DecimalV3Literal first) { + return first.roundCeiling(0); + } + + /** + * ceil + */ + @ExecFunction(name = "ceil") + public static Expression ceil(DecimalV3Literal first, IntegerLiteral second) { + if (second.getValue() >= first.getValue().scale()) { + return first; + } + return first.roundCeiling(second.getValue()); + } + + /** + * ceil + */ + @ExecFunction(name = "ceil") + public static Expression ceil(DoubleLiteral first) { + DecimalV3Literal middleResult = new DecimalV3Literal(new BigDecimal(Double.toString(first.getValue()))); + return new DoubleLiteral(middleResult.roundCeiling(0).getDouble()); + } + + /** + * ceil + */ + @ExecFunction(name = "ceil") + public static Expression ceil(DoubleLiteral first, IntegerLiteral second) { + DecimalV3Literal middleResult = new DecimalV3Literal(new BigDecimal(Double.toString(first.getValue()))); + if (second.getValue() >= middleResult.getValue().scale()) { + return first; + } + return new DoubleLiteral(middleResult.roundCeiling(second.getValue()).getDouble()); + } + + /** + * floor + */ + @ExecFunction(name = "floor") + public static Expression floor(DecimalV3Literal first) { + return first.roundFloor(0); + } + + /** + * floor + */ + @ExecFunction(name = "floor") + public static Expression floor(DecimalV3Literal first, IntegerLiteral second) { + if (second.getValue() >= first.getValue().scale()) { + return first; + } + return first.roundFloor(second.getValue()); + } + + /** + * floor + */ + @ExecFunction(name = "floor") + public static Expression floor(DoubleLiteral first) { + DecimalV3Literal middleResult = new DecimalV3Literal(new BigDecimal(Double.toString(first.getValue()))); + return new DoubleLiteral(middleResult.roundFloor(0).getDouble()); + } + + /** + * floor + */ + @ExecFunction(name = "floor") + public static Expression floor(DoubleLiteral first, IntegerLiteral second) { + DecimalV3Literal middleResult = new DecimalV3Literal(new BigDecimal(Double.toString(first.getValue()))); + if (second.getValue() >= middleResult.getValue().scale()) { + return first; + } + return new DoubleLiteral(middleResult.roundFloor(second.getValue()).getDouble()); + } + + /** + * exp + */ + @ExecFunction(name = "exp") + public static Expression exp(DoubleLiteral first) { + return new DoubleLiteral(Math.exp(first.getValue())); + } + + /** + * ln + */ + @ExecFunction(name = "ln") + public static Expression ln(DoubleLiteral first) { + if (first.getValue() <= 0) { + return new NullLiteral(first.getDataType()); + } + return new DoubleLiteral(Math.log(first.getValue())); + } + + /** + * log + */ + @ExecFunction(name = "log") + public static Expression log(DoubleLiteral first, DoubleLiteral second) { + if (first.getValue() <= 0) { + return new NullLiteral(first.getDataType()); + } + return new DoubleLiteral(Math.log(first.getValue()) / Math.log(second.getValue())); + } + + /** + * log2 + */ + @ExecFunction(name = "log2") + public static Expression log2(DoubleLiteral first) { + if (first.getValue() <= 0) { + return new NullLiteral(first.getDataType()); + } + return new DoubleLiteral(Math.log(first.getValue()) / Math.log(2.0)); + } + + /** + * log10 + */ + @ExecFunction(name = "log10") + public static Expression log10(DoubleLiteral first) { + if (first.getValue() <= 0) { + return new NullLiteral(first.getDataType()); + } + return new DoubleLiteral(Math.log10(first.getValue())); + } + + /** + * sqrt + */ + @ExecFunction(name = "sqrt") + public static Expression sqrt(DoubleLiteral first) { + if (first.getValue() < 0) { + return new NullLiteral(first.getDataType()); + } + return new DoubleLiteral(Math.sqrt(first.getValue())); + } + + /** + * power + */ + @ExecFunction(name = "power") + public static Expression power(DoubleLiteral first, DoubleLiteral second) { + return new DoubleLiteral(Math.pow(first.getValue(), second.getValue())); Review Comment: pow could return infinity or NaN in many cases, u should process them correctly: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#pow-double-double- please check all function one by one according to java doc ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java: ########## @@ -596,4 +650,515 @@ public static Expression divideDecimalV3(DecimalV3Literal first, DecimalV3Litera return new DecimalV3Literal(DecimalV3Type.createDecimalV3TypeLooseCheck( t1.getPrecision(), t1.getScale() - t2.getScale()), result); } + + /** + * coalesce + */ + @ExecFunction(name = "coalesce") + public static Expression coalesce(Literal first, Literal... second) { + if (!(first instanceof NullLiteral)) { + return first; + } + for (Literal secondLiteral : second) { + if (!(secondLiteral instanceof NullLiteral)) { + return secondLiteral; + } + } + return first; + } + + /** + * round + */ + @ExecFunction(name = "round") + public static Expression round(DecimalV3Literal first) { + return first.round(0); + } + + /** + * round + */ + @ExecFunction(name = "round") + public static Expression round(DecimalV3Literal first, IntegerLiteral second) { + if (second.getValue() >= first.getValue().scale()) { + return first; + } Review Comment: this `if` statement should move into `DecimalV3Literal#round` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org