Skip to content

Commit 7220cfe

Browse files
Added RINT function to V2 engine (#240) (#1433)
Added RINT to V2 engine, updated documentation, added unit and IT tests Signed-off-by: Matthew Wells <matthew.wells@improving.com>
1 parent 8dad71f commit 7220cfe

7 files changed

Lines changed: 138 additions & 4 deletions

File tree

core/src/main/java/org/opensearch/sql/expression/DSL.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ public static FunctionExpression rand(Expression... expressions) {
218218
return compile(FunctionProperties.None, BuiltinFunctionName.RAND, expressions);
219219
}
220220

221+
public static FunctionExpression rint(Expression... expressions) {
222+
return compile(FunctionProperties.None, BuiltinFunctionName.RINT, expressions);
223+
}
224+
221225
public static FunctionExpression round(Expression... expressions) {
222226
return compile(FunctionProperties.None, BuiltinFunctionName.ROUND, expressions);
223227
}

core/src/main/java/org/opensearch/sql/expression/function/BuiltinFunctionName.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public enum BuiltinFunctionName {
3939
POW(FunctionName.of("pow")),
4040
POWER(FunctionName.of("power")),
4141
RAND(FunctionName.of("rand")),
42+
RINT(FunctionName.of("rint")),
4243
ROUND(FunctionName.of("round")),
4344
SIGN(FunctionName.of("sign")),
4445
SINH(FunctionName.of("sinh")),

core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunction.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public static void register(BuiltinFunctionRepository repository) {
6969
repository.register(mod());
7070
repository.register(pow());
7171
repository.register(power());
72+
repository.register(rint());
7273
repository.register(round());
7374
repository.register(sign());
7475
repository.register(sinh());
@@ -411,6 +412,17 @@ private static DefaultFunctionResolver rand() {
411412
);
412413
}
413414

415+
/**
416+
* Definition of rint(x) function.
417+
* Returns the closest whole integer value to x
418+
* The supported signature is
419+
* BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
420+
*/
421+
private static DefaultFunctionResolver rint() {
422+
return baseMathFunction(BuiltinFunctionName.RINT.getName(),
423+
v -> new ExprDoubleValue(Math.rint(v.doubleValue())), DOUBLE);
424+
}
425+
414426
/**
415427
* Definition of round(x)/round(x, d) function.
416428
* Rounds the argument x to d decimal places, d defaults to 0 if not specified.

core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunctionTest.java

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ public void expm1_short_value(Short value) {
642642
}
643643

644644
/**
645-
* Test expm1 with short value.
645+
* Test expm1 with byte value.
646646
*/
647647
@ParameterizedTest(name = "expm1({0})")
648648
@ValueSource(bytes = {
@@ -1570,6 +1570,92 @@ public void pow_null_missing() {
15701570
assertTrue(power.valueOf(valueEnv()).isMissing());
15711571
}
15721572

1573+
/**
1574+
* Test rint with byte value.
1575+
*/
1576+
@ParameterizedTest(name = "rint({0})")
1577+
@ValueSource(bytes = {
1578+
-1, 0, 1, Byte.MAX_VALUE, Byte.MIN_VALUE})
1579+
public void rint_byte_value(Byte value) {
1580+
FunctionExpression rint = DSL.rint(DSL.literal(value));
1581+
assertThat(
1582+
rint.valueOf(valueEnv()),
1583+
allOf(hasType(DOUBLE), hasValue(Math.rint(value))));
1584+
assertEquals(String.format("rint(%s)", value), rint.toString());
1585+
}
1586+
1587+
/**
1588+
* Test rint with short value.
1589+
*/
1590+
@ParameterizedTest(name = "rint({0})")
1591+
@ValueSource(shorts = {
1592+
-1, 0, 1, Short.MAX_VALUE, Short.MIN_VALUE})
1593+
public void rint_short_value(Short value) {
1594+
FunctionExpression rint = DSL.rint(DSL.literal(value));
1595+
assertThat(
1596+
rint.valueOf(valueEnv()),
1597+
allOf(hasType(DOUBLE), hasValue(Math.rint(value))));
1598+
assertEquals(String.format("rint(%s)", value), rint.toString());
1599+
}
1600+
1601+
/**
1602+
* Test rint with integer value.
1603+
*/
1604+
@ParameterizedTest(name = "rint({0})")
1605+
@ValueSource(ints = {
1606+
-1, 0, 1, Integer.MAX_VALUE, Integer.MIN_VALUE})
1607+
public void rint_int_value(Integer value) {
1608+
FunctionExpression rint = DSL.rint(DSL.literal(value));
1609+
assertThat(
1610+
rint.valueOf(valueEnv()),
1611+
allOf(hasType(DOUBLE), hasValue(Math.rint(value))));
1612+
assertEquals(String.format("rint(%s)", value), rint.toString());
1613+
}
1614+
1615+
/**
1616+
* Test rint with long value.
1617+
*/
1618+
@ParameterizedTest(name = "rint({0})")
1619+
@ValueSource(longs = {
1620+
-1L, 0L, 1L, Long.MAX_VALUE, Long.MIN_VALUE})
1621+
public void rint_long_value(Long value) {
1622+
FunctionExpression rint = DSL.rint(DSL.literal(value));
1623+
assertThat(
1624+
rint.valueOf(valueEnv()),
1625+
allOf(hasType(DOUBLE), hasValue(Math.rint(value))));
1626+
assertEquals(String.format("rint(%s)", value), rint.toString());
1627+
}
1628+
1629+
/**
1630+
* Test rint with float value.
1631+
*/
1632+
@ParameterizedTest(name = "rint({0})")
1633+
@ValueSource(floats = {
1634+
-1F, -0.75F, -0.5F, 0F, 0.5F, 0.500000001F,
1635+
0.75F, 1F, 1.9999F, 42.42F, Float.MAX_VALUE, Float.MIN_VALUE})
1636+
public void rint_float_value(Float value) {
1637+
FunctionExpression rint = DSL.rint(DSL.literal(value));
1638+
assertThat(
1639+
rint.valueOf(valueEnv()),
1640+
allOf(hasType(DOUBLE), hasValue(Math.rint(value))));
1641+
assertEquals(String.format("rint(%s)", value), rint.toString());
1642+
}
1643+
1644+
/**
1645+
* Test rint with double value.
1646+
*/
1647+
@ParameterizedTest(name = "rint({0})")
1648+
@ValueSource(doubles = {
1649+
-1F, -0.75F, -0.5F, 0F, 0.5F, 0.500000001F,
1650+
0.75F, 1F, 1.9999F, 42.42F, Double.MAX_VALUE, Double.MIN_VALUE})
1651+
public void rint_double_value(Double value) {
1652+
FunctionExpression rint = DSL.rint(DSL.literal(value));
1653+
assertThat(
1654+
rint.valueOf(valueEnv()),
1655+
allOf(hasType(DOUBLE), hasValue(Math.rint(value))));
1656+
assertEquals(String.format("rint(%s)", value), rint.toString());
1657+
}
1658+
15731659
/**
15741660
* Test round with integer value.
15751661
*/

docs/user/dql/functions.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,21 @@ RINT
712712
Description
713713
>>>>>>>>>>>
714714

715-
Specifications:
715+
Usage: RINT(NUMBER T) returns T rounded to the closest whole integer number
716+
717+
Argument type: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE
716718

717-
1. RINT(NUMBER T) -> T
719+
Return type: DOUBLE
720+
721+
Example::
722+
723+
os> SELECT RINT(1.7);
724+
fetched rows / total rows = 1/1
725+
+-------------+
726+
| RINT(1.7) |
727+
|-------------|
728+
| 2.0 |
729+
+-------------+
718730

719731

720732
ROUND

integ-test/src/test/java/org/opensearch/sql/sql/MathematicalFunctionIT.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ public void testMod() throws IOException {
113113
verifyDataRows(result, rows(1.1));
114114
}
115115

116+
@Test
117+
public void testRint() throws IOException {
118+
JSONObject result = executeQuery("select rint(56.78)");
119+
verifySchema(result, schema("rint(56.78)", null, "double"));
120+
verifyDataRows(result, rows(57.0));
121+
122+
result = executeQuery("select rint(-56)");
123+
verifySchema(result, schema("rint(-56)", null, "double"));
124+
verifyDataRows(result, rows(-56.0));
125+
126+
result = executeQuery("select rint(3.5)");
127+
verifySchema(result, schema("rint(3.5)", null, "double"));
128+
verifyDataRows(result, rows(4.0));
129+
130+
result = executeQuery("select rint(-3.5)");
131+
verifySchema(result, schema("rint(-3.5)", null, "double"));
132+
verifyDataRows(result, rows(-4.0));
133+
}
134+
116135
@Test
117136
public void testRound() throws IOException {
118137
JSONObject result = executeQuery("select round(56.78)");

sql/src/main/antlr/OpenSearchSQLParser.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ aggregationFunctionName
419419

420420
mathematicalFunctionName
421421
: ABS | CBRT | CEIL | CEILING | CONV | CRC32 | E | EXP | EXPM1 | FLOOR | LN | LOG | LOG10 | LOG2 | MOD | PI | POW | POWER
422-
| RAND | ROUND | SIGN | SQRT | TRUNCATE
422+
| RAND | RINT | ROUND | SIGN | SQRT | TRUNCATE
423423
| trigonometricFunctionName
424424
;
425425

0 commit comments

Comments
 (0)