LiBinfeng-01 commented on code in PR #40744: URL: https://github.com/apache/doris/pull/40744#discussion_r1770560213
########## 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: done -- 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