Skip to content

Commit d0d4691

Browse files
committed
Remove the magic threshold for DIVIDE and MOD
- additionally update expressions.rst to clarify how DIVIDE handles division by zero Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 7ce3b87 commit d0d4691

File tree

4 files changed

+6
-7
lines changed

4 files changed

+6
-7
lines changed

core/src/main/java/org/opensearch/sql/calcite/udf/mathUDF/DivideFunction.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ public Object eval(Object... args) {
2020
Number dividend = (Number) args[0];
2121
Number divisor = (Number) args[1];
2222

23-
if (Math.abs(divisor.doubleValue()) < MathUtils.EPSILON) {
23+
if (divisor.doubleValue() == 0) {
2424
return null;
2525
}
2626

27-
double result = dividend.doubleValue() / divisor.doubleValue();
2827
if (MathUtils.isIntegral(dividend) && MathUtils.isIntegral(divisor)) {
29-
return MathUtils.coerceToWidestIntegralType(dividend, divisor, (long) result);
28+
long result = dividend.longValue() / divisor.longValue();
29+
return MathUtils.coerceToWidestIntegralType(dividend, divisor, result);
3030
}
31+
double result = dividend.doubleValue() / divisor.doubleValue();
3132
return MathUtils.coerceToWidestFloatingType(dividend, divisor, result);
3233
}
3334
}

core/src/main/java/org/opensearch/sql/calcite/udf/mathUDF/ModFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Object eval(Object... args) {
3333
arg0.getClass().getSimpleName(), arg1.getClass().getSimpleName()));
3434
}
3535

36-
if (Math.abs(num1.doubleValue()) < MathUtils.EPSILON) {
36+
if (num1.doubleValue() == 0) {
3737
return null;
3838
}
3939

core/src/main/java/org/opensearch/sql/calcite/utils/MathUtils.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
package org.opensearch.sql.calcite.utils;
77

88
public class MathUtils {
9-
public static final double EPSILON = 0.0000001;
10-
119
public static boolean isIntegral(Number n) {
1210
return n instanceof Byte || n instanceof Short || n instanceof Integer || n instanceof Long;
1311
}

docs/user/ppl/functions/expressions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Arithmetic expression is an expression formed by numeric literals and binary ari
2828
1. ``+``: Add.
2929
2. ``-``: Subtract.
3030
3. ``*``: Multiply.
31-
4. ``/``: Divide. For integers, the result is an integer with fractional part discarded.
31+
4. ``/``: Divide. For integers, the result is an integer with fractional part discarded. Returns NULL when dividing by zero.
3232
5. ``%``: Modulo. This can be used with integers only with remainder of the division as result.
3333

3434
Precedence

0 commit comments

Comments
 (0)