Skip to content

Commit a6d5626

Browse files
authored
Mod function should return decimal instead of float when handle the operands are decimal literal (#4407)
* Mod function should return decimal instead of float when handle the operands are decimal literal Signed-off-by: Lantao Jin <[email protected]> * add IT Signed-off-by: Lantao Jin <[email protected]> --------- Signed-off-by: Lantao Jin <[email protected]>
1 parent d567ec8 commit a6d5626

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

core/src/main/java/org/opensearch/sql/expression/function/udf/math/ModFunction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ public static Number floatingMod(Number dividend, Number divisor) {
9494
BigDecimal b0 = new BigDecimal(dividend.toString());
9595
BigDecimal b1 = new BigDecimal(divisor.toString());
9696
BigDecimal result = b0.remainder(b1);
97+
if (dividend instanceof BigDecimal || divisor instanceof BigDecimal) {
98+
return result;
99+
}
97100
return MathUtils.coerceToWidestFloatingType(dividend, divisor, result.doubleValue());
98101
}
99102
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
setup:
2+
- do:
3+
query.settings:
4+
body:
5+
transient:
6+
plugins.calcite.enabled : true
7+
- do:
8+
bulk:
9+
index: test
10+
refresh: true
11+
body:
12+
- '{"index": {}}'
13+
- '{"id": 3.1}'
14+
15+
---
16+
teardown:
17+
- do:
18+
query.settings:
19+
body:
20+
transient:
21+
plugins.calcite.enabled : false
22+
23+
---
24+
"big decimal literal":
25+
- skip:
26+
features:
27+
- headers
28+
- allowed_warnings
29+
- do:
30+
headers:
31+
Content-Type: 'application/json'
32+
ppl:
33+
body:
34+
query: source=test | eval a = mod(3.1, 2), b = modulus(3.1, 2.0), c = mod(id, 2.0) | fields a,b,c
35+
36+
- match: { total: 1 }
37+
- match: {"datarows": [[1.1,1.1,1.1]]}

ppl/src/test/java/org/opensearch/sql/ppl/calcite/CalcitePPLMathFunctionTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ public void testMod() {
237237
verifyPPLToSparkSQL(root, expectedSparkSql);
238238
}
239239

240+
@Test
241+
public void testModDecimal() {
242+
RelNode root = getRelNode("source=EMP | eval MOD = mod(3.1, 2) | fields MOD");
243+
String expectedLogical =
244+
"LogicalProject(MOD=[MOD(3.1:DECIMAL(2, 1), 2)])\n"
245+
+ " LogicalTableScan(table=[[scott, EMP]])\n";
246+
verifyLogical(root, expectedLogical);
247+
String expectedSparkSql = "SELECT MOD(3.1, 2) `MOD`\nFROM `scott`.`EMP`";
248+
verifyPPLToSparkSQL(root, expectedSparkSql);
249+
}
250+
240251
@Test
241252
public void testPi() {
242253
RelNode root = getRelNode("source=EMP | eval PI = pi() | fields PI");

0 commit comments

Comments
 (0)