diff --git a/core/src/main/java/org/opensearch/sql/expression/function/udf/math/ModFunction.java b/core/src/main/java/org/opensearch/sql/expression/function/udf/math/ModFunction.java index aa4cd565dca..de0cf1f5a81 100644 --- a/core/src/main/java/org/opensearch/sql/expression/function/udf/math/ModFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/function/udf/math/ModFunction.java @@ -94,6 +94,9 @@ public static Number floatingMod(Number dividend, Number divisor) { BigDecimal b0 = new BigDecimal(dividend.toString()); BigDecimal b1 = new BigDecimal(divisor.toString()); BigDecimal result = b0.remainder(b1); + if (dividend instanceof BigDecimal || divisor instanceof BigDecimal) { + return result; + } return MathUtils.coerceToWidestFloatingType(dividend, divisor, result.doubleValue()); } } diff --git a/integ-test/src/yamlRestTest/resources/rest-api-spec/test/issues/4407.yml b/integ-test/src/yamlRestTest/resources/rest-api-spec/test/issues/4407.yml new file mode 100644 index 00000000000..2549b73c570 --- /dev/null +++ b/integ-test/src/yamlRestTest/resources/rest-api-spec/test/issues/4407.yml @@ -0,0 +1,37 @@ +setup: + - do: + query.settings: + body: + transient: + plugins.calcite.enabled : true + - do: + bulk: + index: test + refresh: true + body: + - '{"index": {}}' + - '{"id": 3.1}' + +--- +teardown: + - do: + query.settings: + body: + transient: + plugins.calcite.enabled : false + +--- +"big decimal literal": + - skip: + features: + - headers + - allowed_warnings + - do: + headers: + Content-Type: 'application/json' + ppl: + body: + query: source=test | eval a = mod(3.1, 2), b = modulus(3.1, 2.0), c = mod(id, 2.0) | fields a,b,c + + - match: { total: 1 } + - match: {"datarows": [[1.1,1.1,1.1]]} diff --git a/ppl/src/test/java/org/opensearch/sql/ppl/calcite/CalcitePPLMathFunctionTest.java b/ppl/src/test/java/org/opensearch/sql/ppl/calcite/CalcitePPLMathFunctionTest.java index 0d2593486db..717ccad32f8 100644 --- a/ppl/src/test/java/org/opensearch/sql/ppl/calcite/CalcitePPLMathFunctionTest.java +++ b/ppl/src/test/java/org/opensearch/sql/ppl/calcite/CalcitePPLMathFunctionTest.java @@ -237,6 +237,17 @@ public void testMod() { verifyPPLToSparkSQL(root, expectedSparkSql); } + @Test + public void testModDecimal() { + RelNode root = getRelNode("source=EMP | eval MOD = mod(3.1, 2) | fields MOD"); + String expectedLogical = + "LogicalProject(MOD=[MOD(3.1:DECIMAL(2, 1), 2)])\n" + + " LogicalTableScan(table=[[scott, EMP]])\n"; + verifyLogical(root, expectedLogical); + String expectedSparkSql = "SELECT MOD(3.1, 2) `MOD`\nFROM `scott`.`EMP`"; + verifyPPLToSparkSQL(root, expectedSparkSql); + } + @Test public void testPi() { RelNode root = getRelNode("source=EMP | eval PI = pi() | fields PI");