You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2019/03/12 08:12:35 UTC

[GitHub] [incubator-druid] asdf2014 commented on a change in pull request #7224: Add ROUND function in druid-sql.

asdf2014 commented on a change in pull request #7224: Add ROUND function in druid-sql.
URL: https://github.com/apache/incubator-druid/pull/7224#discussion_r264559548
 
 

 ##########
 File path: core/src/main/java/org/apache/druid/math/expr/Function.java
 ##########
 @@ -441,18 +443,43 @@ protected ExprEval eval(double param)
     }
   }
 
-  class Round extends SingleParamMath
+  class Round implements Function
   {
+    @Override
+    public ExprEval apply(List<Expr> args, Expr.ObjectBinding bindings)
+    {
+      if (args.size() == 1) {
+        Expr expr1 = args.get(0);
+        return eval(expr1.eval(bindings));
+      } else if (args.size() == 2) {
+        Expr expr1 = args.get(0);
+        Expr expr2 = args.get(1);
+        return eval(expr1.eval(bindings), expr2.eval(bindings));
+      } else {
+        throw new IAE("Function[%s] needs 1 or 2 arguments", name());
+      }
+    }
+
     @Override
     public String name()
     {
       return "round";
     }
 
-    @Override
-    protected ExprEval eval(double param)
+    protected ExprEval eval(ExprEval param)
+    {
+      return eval(param, ExprEval.of(0));
+    }
+
+    protected ExprEval eval(ExprEval param, ExprEval scale)
     {
-      return ExprEval.of(Math.round(param));
+      if (param.type() == ExprType.LONG) {
+        Long value = BigDecimal.valueOf(param.asLong()).setScale(scale.asInt(), RoundingMode.HALF_UP).longValue();
+        return ExprEval.of(value);
+      } else {
 
 Review comment:
   We should consider `ExprType.STRING` situation here.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org