From d7825f4ecac1c5c6e9123ec101432ad0dd929db2 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Tue, 3 Jan 2023 11:35:55 -0800 Subject: [PATCH] Fix truncate() function (#1197) (#1213) * Fix truncate() function (#188) Signed-off-by: Margarit Hakobyan Signed-off-by: Margarit Hakobyan (cherry picked from commit 771481911f7af1f2d00ef2c4a09021613628454c) Co-authored-by: Margarit Hakobyan --- .../arthmetic/MathematicalFunction.java | 16 ++++----- .../arthmetic/MathematicalFunctionTest.java | 35 ++++++++++--------- .../sql/sql/MathematicalFunctionIT.java | 24 +++++++++++++ 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunction.java b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunction.java index 0e4df086fb..4424243860 100644 --- a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunction.java @@ -500,26 +500,26 @@ private static DefaultFunctionResolver truncate() { FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprLongValue( - new BigDecimal(x.integerValue()).setScale(y.integerValue(), - RoundingMode.DOWN).longValue())), + BigDecimal.valueOf(x.integerValue()).setScale(y.integerValue(), + RoundingMode.DOWN).longValue())), LONG, INTEGER, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprLongValue( - new BigDecimal(x.integerValue()).setScale(y.integerValue(), - RoundingMode.DOWN).longValue())), + BigDecimal.valueOf(x.longValue()).setScale(y.integerValue(), + RoundingMode.DOWN).longValue())), LONG, LONG, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprDoubleValue( - new BigDecimal(x.floatValue()).setScale(y.integerValue(), - RoundingMode.DOWN).doubleValue())), + BigDecimal.valueOf(x.floatValue()).setScale(y.integerValue(), + RoundingMode.DOWN).doubleValue())), DOUBLE, FLOAT, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprDoubleValue( - new BigDecimal(x.doubleValue()).setScale(y.integerValue(), - RoundingMode.DOWN).doubleValue())), + BigDecimal.valueOf(x.doubleValue()).setScale(y.integerValue(), + RoundingMode.DOWN).doubleValue())), DOUBLE, DOUBLE, INTEGER)); } diff --git a/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunctionTest.java index 3d7cdaeb41..d6d15e9315 100644 --- a/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathematicalFunctionTest.java @@ -192,12 +192,12 @@ public void ceil_int_value(Integer value) { assertThat( ceil.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.ceil(value)))); - assertEquals(String.format("ceil(%s)", value.toString()), ceil.toString()); + assertEquals(String.format("ceil(%s)", value), ceil.toString()); FunctionExpression ceiling = DSL.ceiling(DSL.literal(value)); assertThat( ceiling.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.ceil(value)))); - assertEquals(String.format("ceiling(%s)", value.toString()), ceiling.toString()); + assertEquals(String.format("ceiling(%s)", value), ceiling.toString()); } /** @@ -209,12 +209,12 @@ public void ceil_long_value(Long value) { FunctionExpression ceil = DSL.ceil(DSL.literal(value)); assertThat( ceil.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.ceil(value)))); - assertEquals(String.format("ceil(%s)", value.toString()), ceil.toString()); + assertEquals(String.format("ceil(%s)", value), ceil.toString()); FunctionExpression ceiling = DSL.ceiling(DSL.literal(value)); assertThat( ceiling.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.ceil(value)))); - assertEquals(String.format("ceiling(%s)", value.toString()), ceiling.toString()); + assertEquals(String.format("ceiling(%s)", value), ceiling.toString()); } /** @@ -226,12 +226,12 @@ public void ceil_float_value(Float value) { FunctionExpression ceil = DSL.ceil(DSL.literal(value)); assertThat( ceil.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.ceil(value)))); - assertEquals(String.format("ceil(%s)", value.toString()), ceil.toString()); + assertEquals(String.format("ceil(%s)", value), ceil.toString()); FunctionExpression ceiling = DSL.ceiling(DSL.literal(value)); assertThat( ceiling.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.ceil(value)))); - assertEquals(String.format("ceiling(%s)", value.toString()), ceiling.toString()); + assertEquals(String.format("ceiling(%s)", value), ceiling.toString()); } /** @@ -243,12 +243,12 @@ public void ceil_double_value(Double value) { FunctionExpression ceil = DSL.ceil(DSL.literal(value)); assertThat( ceil.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.ceil(value)))); - assertEquals(String.format("ceil(%s)", value.toString()), ceil.toString()); + assertEquals(String.format("ceil(%s)", value), ceil.toString()); FunctionExpression ceiling = DSL.ceiling(DSL.literal(value)); assertThat( ceiling.valueOf(valueEnv()), allOf(hasType(INTEGER), hasValue((int) Math.ceil(value)))); - assertEquals(String.format("ceiling(%s)", value.toString()), ceiling.toString()); + assertEquals(String.format("ceiling(%s)", value), ceiling.toString()); } /** @@ -1721,12 +1721,12 @@ public void sqrt_missing_value() { * Test truncate with integer value. */ @ParameterizedTest(name = "truncate({0}, {1})") - @ValueSource(ints = {2, -2}) + @ValueSource(ints = {2, -2, Integer.MAX_VALUE, Integer.MIN_VALUE}) public void truncate_int_value(Integer value) { FunctionExpression truncate = DSL.truncate(DSL.literal(value), DSL.literal(1)); assertThat( truncate.valueOf(valueEnv()), allOf(hasType(LONG), - hasValue(new BigDecimal(value).setScale(1, RoundingMode.DOWN).longValue()))); + hasValue(BigDecimal.valueOf(value).setScale(1, RoundingMode.DOWN).longValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } @@ -1734,12 +1734,12 @@ public void truncate_int_value(Integer value) { * Test truncate with long value. */ @ParameterizedTest(name = "truncate({0}, {1})") - @ValueSource(longs = {2L, -2L}) + @ValueSource(longs = {2L, -2L, Long.MAX_VALUE, Long.MIN_VALUE}) public void truncate_long_value(Long value) { FunctionExpression truncate = DSL.truncate(DSL.literal(value), DSL.literal(1)); assertThat( truncate.valueOf(valueEnv()), allOf(hasType(LONG), - hasValue(new BigDecimal(value).setScale(1, RoundingMode.DOWN).longValue()))); + hasValue(BigDecimal.valueOf(value).setScale(1, RoundingMode.DOWN).longValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } @@ -1747,12 +1747,12 @@ public void truncate_long_value(Long value) { * Test truncate with float value. */ @ParameterizedTest(name = "truncate({0}, {1})") - @ValueSource(floats = {2F, -2F}) + @ValueSource(floats = {2F, -2F, Float.MAX_VALUE, Float.MIN_VALUE}) public void truncate_float_value(Float value) { FunctionExpression truncate = DSL.truncate(DSL.literal(value), DSL.literal(1)); assertThat( truncate.valueOf(valueEnv()), allOf(hasType(DOUBLE), - hasValue(new BigDecimal(value).setScale(1, RoundingMode.DOWN).doubleValue()))); + hasValue(BigDecimal.valueOf(value).setScale(1, RoundingMode.DOWN).doubleValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } @@ -1760,12 +1760,15 @@ public void truncate_float_value(Float value) { * Test truncate with double value. */ @ParameterizedTest(name = "truncate({0}, {1})") - @ValueSource(doubles = {2D, -2D}) + @ValueSource(doubles = {2D, -9.223372036854776e+18D, -2147483649.0D, -2147483648.0D, + -32769.0D, -32768.0D, -34.84D, -2.0D, -1.2D, -1.0D, 0.0D, 1.0D, + 1.3D, 2.0D, 1004.3D, 32767.0D, 32768.0D, 2147483647.0D, 2147483648.0D, + 9.223372036854776e+18D, Double.MAX_VALUE, Double.MIN_VALUE}) public void truncate_double_value(Double value) { FunctionExpression truncate = DSL.truncate(DSL.literal(value), DSL.literal(1)); assertThat( truncate.valueOf(valueEnv()), allOf(hasType(DOUBLE), - hasValue(new BigDecimal(value).setScale(1, RoundingMode.DOWN).doubleValue()))); + hasValue(BigDecimal.valueOf(value).setScale(1, RoundingMode.DOWN).doubleValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/MathematicalFunctionIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/MathematicalFunctionIT.java index efa16ba9d7..f2d1bb7d28 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/MathematicalFunctionIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/MathematicalFunctionIT.java @@ -142,6 +142,30 @@ public void testTruncate() throws IOException { result = executeQuery("select truncate(-56, -1)"); verifySchema(result, schema("truncate(-56, -1)", null, "long")); verifyDataRows(result, rows(-50)); + + result = executeQuery("select truncate(33.33344, -1)"); + verifySchema(result, schema("truncate(33.33344, -1)", null, "double")); + verifyDataRows(result, rows(30.0)); + + result = executeQuery("select truncate(33.33344, 2)"); + verifySchema(result, schema("truncate(33.33344, 2)", null, "double")); + verifyDataRows(result, rows(33.33)); + + result = executeQuery("select truncate(33.33344, 100)"); + verifySchema(result, schema("truncate(33.33344, 100)", null, "double")); + verifyDataRows(result, rows(33.33344)); + + result = executeQuery("select truncate(33.33344, 0)"); + verifySchema(result, schema("truncate(33.33344, 0)", null, "double")); + verifyDataRows(result, rows(33.0)); + + result = executeQuery("select truncate(33.33344, 4)"); + verifySchema(result, schema("truncate(33.33344, 4)", null, "double")); + verifyDataRows(result, rows(33.3334)); + + result = executeQuery(String.format("select truncate(%s, 6)", Math.PI)); + verifySchema(result, schema(String.format("truncate(%s, 6)", Math.PI), null, "double")); + verifyDataRows(result, rows(3.141592)); } @Test