somu-imply commented on a change in pull request #11904:
URL: https://github.com/apache/druid/pull/11904#discussion_r748567555
##########
File path: core/src/main/java/org/apache/druid/math/expr/Function.java
##########
@@ -1165,6 +1165,46 @@ protected ExprEval eval(double param)
}
}
+ class SafeDivide extends BivariateMathFunction
+ {
+ public static final String NAME = "safe_divide";
+
+ @Override
+ public String name()
+ {
+ return NAME ;
+ }
+
+ @Nullable
+ @Override
+ public ExpressionType getOutputType(Expr.InputBindingInspector inspector,
List<Expr> args)
+ {
+ return ExpressionTypeConversion.integerMathFunction(
+ args.get(0).getOutputType(inspector),
+ args.get(1).getOutputType(inspector)
+ );
+ }
+
+ @Override
+ protected ExprEval eval(final long x, final long y)
+ {
+ if(y==0) {
+ return ExprEval.of(0);
+ }
+ return ExprEval.of(x / y);
+ }
+
+ @Override
+ protected ExprEval eval(final double x, final double y)
+ {
+ if(y==0) {
+ return ExprEval.of(0);
+ }
Review comment:
Change made. Added a case and testcases where x or y is NaN or +-
Infinity
##########
File path: core/src/test/java/org/apache/druid/math/expr/FunctionTest.java
##########
@@ -732,6 +732,16 @@ public void testSizeFormatInvalidArgumentSize()
.eval(bindings);
}
+ @Test
+ public void testSafeDivide()
+ {
+ // happy path maths
+ assertExpr("safe_divide(3, 1)", 3L);
+ assertExpr("safe_divide(4.5, 2)", 2.25);
+ assertExpr("safe_divide(3, 0)", 0L);
+ assertExpr("safe_divide(3.7, 0.0)", 0L);
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]