From 2ee64f335a026cdacd6900c906cee1c8bf731b8f Mon Sep 17 00:00:00 2001 From: Margarit Hakobyan Date: Fri, 9 Dec 2022 15:28:30 -0800 Subject: [PATCH 01/10] Fix truncate() function Signed-off-by: Margarit Hakobyan --- .../operator/arthmetic/MathUtil.java | 32 +++++++++++++++++++ .../arthmetic/MathematicalFunction.java | 12 +++---- .../arthmetic/MathematicalFunctionTest.java | 16 +++++----- .../sql/sql/MathematicalFunctionIT.java | 8 +++++ 4 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java diff --git a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java new file mode 100644 index 0000000000..704950f241 --- /dev/null +++ b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java @@ -0,0 +1,32 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + + +package org.opensearch.sql.expression.operator.arthmetic; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import lombok.experimental.UtilityClass; + +@UtilityClass +public class MathUtil { + + /** + * Truncates a number to required decimal places. + * + * @param numberToTruncate number to be truncated + * @param numberOfDecimals required decimal places + * @return truncated number as {@link BigDecimal} + */ + public static BigDecimal truncateNumber(double numberToTruncate, int numberOfDecimals) { + if (numberToTruncate > 0) { + return new BigDecimal(String.valueOf(numberToTruncate)) + .setScale(numberOfDecimals, RoundingMode.FLOOR); + } else { + return new BigDecimal(String + .valueOf(numberToTruncate)).setScale(numberOfDecimals, RoundingMode.CEILING); + } + } +} 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..e77239c667 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,22 @@ private static DefaultFunctionResolver truncate() { FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprLongValue( - new BigDecimal(x.integerValue()).setScale(y.integerValue(), - RoundingMode.DOWN).longValue())), + MathUtil.truncateNumber(x.integerValue(), y.integerValue()).longValue())), LONG, INTEGER, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprLongValue( - new BigDecimal(x.integerValue()).setScale(y.integerValue(), - RoundingMode.DOWN).longValue())), + MathUtil.truncateNumber(x.longValue(), y.integerValue()).longValue())), LONG, LONG, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprDoubleValue( - new BigDecimal(x.floatValue()).setScale(y.integerValue(), - RoundingMode.DOWN).doubleValue())), + MathUtil.truncateNumber(x.floatValue(), y.integerValue()).doubleValue())), DOUBLE, FLOAT, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprDoubleValue( - new BigDecimal(x.doubleValue()).setScale(y.integerValue(), - RoundingMode.DOWN).doubleValue())), + MathUtil.truncateNumber(x.doubleValue(), y.integerValue()).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..a74439437d 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 @@ -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(MathUtil.truncateNumber(value, 1).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(MathUtil.truncateNumber(value, 1).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(MathUtil.truncateNumber(value, 1).doubleValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } @@ -1760,12 +1760,12 @@ public void truncate_float_value(Float value) { * Test truncate with double value. */ @ParameterizedTest(name = "truncate({0}, {1})") - @ValueSource(doubles = {2D, -2D}) + @ValueSource(doubles = {2D, -1.2D, 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(MathUtil.truncateNumber(value, 1).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..ff2776dfd7 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,14 @@ 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(-1.2, 1)"); + verifySchema(result, schema("truncate(-1.2, 1)", null, "double")); + verifyDataRows(result, rows(-1.2)); + + result = executeQuery("select truncate(1004.3, 1)"); + verifySchema(result, schema("truncate(1004.3, 1)", null, "double")); + verifyDataRows(result, rows(1004.3)); } @Test From ca87242cfd21b7594176c8a95447144b957b8358 Mon Sep 17 00:00:00 2001 From: Margarit Hakobyan Date: Mon, 12 Dec 2022 16:13:08 -0800 Subject: [PATCH 02/10] Address PR review feedback Signed-off-by: Margarit Hakobyan --- .../operator/arthmetic/MathUtil.java | 51 +++++++++++++++---- .../arthmetic/MathematicalFunction.java | 8 +-- .../operator/arthmetic/MathUtilTest.java | 40 +++++++++++++++ .../arthmetic/MathematicalFunctionTest.java | 24 ++++----- 4 files changed, 96 insertions(+), 27 deletions(-) create mode 100644 core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathUtilTest.java diff --git a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java index 704950f241..ce6f6f2d13 100644 --- a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java +++ b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java @@ -8,25 +8,54 @@ import java.math.BigDecimal; import java.math.RoundingMode; -import lombok.experimental.UtilityClass; -@UtilityClass public class MathUtil { /** - * Truncates a number to required decimal places. + * Truncates a double number to required decimal places. * * @param numberToTruncate number to be truncated * @param numberOfDecimals required decimal places * @return truncated number as {@link BigDecimal} */ - public static BigDecimal truncateNumber(double numberToTruncate, int numberOfDecimals) { - if (numberToTruncate > 0) { - return new BigDecimal(String.valueOf(numberToTruncate)) - .setScale(numberOfDecimals, RoundingMode.FLOOR); - } else { - return new BigDecimal(String - .valueOf(numberToTruncate)).setScale(numberOfDecimals, RoundingMode.CEILING); - } + public static double truncateDouble(double numberToTruncate, int numberOfDecimals) { + return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, + numberToTruncate > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).doubleValue(); + } + + /** + * Truncates a float number to required decimal places. + * + * @param numberToTruncate number to be truncated + * @param numberOfDecimals required decimal places + * @return truncated number as {@link BigDecimal} + */ + public static double truncateFloat(float numberToTruncate, int numberOfDecimals) { + return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, + numberToTruncate > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).doubleValue(); + } + + /** + * Truncates an int number to required decimal places. + * + * @param numberToTruncate number to be truncated + * @param numberOfDecimals required decimal places + * @return truncated number as {@link BigDecimal} + */ + public static long truncateInt(int numberToTruncate, int numberOfDecimals) { + return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, + numberToTruncate > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).longValue(); + } + + /** + * Truncates a long number to required decimal places. + * + * @param numberToTruncate number to be truncated + * @param numberOfDecimals required decimal places + * @return truncated number as {@link BigDecimal} + */ + public static long truncateLong(long numberToTruncate, int numberOfDecimals) { + return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, + numberToTruncate > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).longValue(); } } 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 e77239c667..220801f23e 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,22 +500,22 @@ private static DefaultFunctionResolver truncate() { FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprLongValue( - MathUtil.truncateNumber(x.integerValue(), y.integerValue()).longValue())), + MathUtil.truncateInt(x.integerValue(), y.integerValue()))), LONG, INTEGER, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprLongValue( - MathUtil.truncateNumber(x.longValue(), y.integerValue()).longValue())), + MathUtil.truncateLong(x.longValue(), y.integerValue()))), LONG, LONG, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprDoubleValue( - MathUtil.truncateNumber(x.floatValue(), y.integerValue()).doubleValue())), + MathUtil.truncateFloat(x.floatValue(), y.integerValue()))), DOUBLE, FLOAT, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprDoubleValue( - MathUtil.truncateNumber(x.doubleValue(), y.integerValue()).doubleValue())), + MathUtil.truncateDouble(x.doubleValue(), y.integerValue()))), DOUBLE, DOUBLE, INTEGER)); } diff --git a/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathUtilTest.java b/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathUtilTest.java new file mode 100644 index 0000000000..dac190b601 --- /dev/null +++ b/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathUtilTest.java @@ -0,0 +1,40 @@ +package org.opensearch.sql.expression.operator.arthmetic; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +/** + * Test class for {@link MathUtil}. + */ +class MathUtilTest { + + @ParameterizedTest + @ValueSource(doubles = {11.2D, 22.5678D, -1.2D}) + void testTruncateDouble(final Double value) { + String result = Double.toString(MathUtil.truncateDouble(value, 1)); + assertEquals(Double.toString(value).substring(0,4), result); + } + + @ParameterizedTest(name = "truncate({0}, {1})") + @ValueSource(floats = {11.2F, 22.5678F, -1.2F}) + void testTruncateFloat(final Float value) { + String result = Double.toString(MathUtil.truncateFloat(value, 1)); + assertEquals(Float.toString(value).substring(0,4), result); + } + + @ParameterizedTest + @ValueSource(longs = {2056L, -777L}) + void testTruncateLong(final Long value) { + String result = Long.toString(MathUtil.truncateLong(value, 1)); + assertEquals(Long.toString(value).substring(0,4), result); + } + + @ParameterizedTest + @ValueSource(ints = {11, 22, -7}) + void testTruncateInt(final int value) { + String result = Long.toString(MathUtil.truncateInt(value, 1)); + assertEquals(Integer.toString(value).substring(0,2), result); + } +} \ No newline at end of file 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 a74439437d..1249cc77ab 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()); } /** @@ -1726,7 +1726,7 @@ 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(MathUtil.truncateNumber(value, 1).longValue()))); + hasValue(MathUtil.truncateInt(value, 1)))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } @@ -1739,7 +1739,7 @@ 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(MathUtil.truncateNumber(value, 1).longValue()))); + hasValue(MathUtil.truncateLong(value, 1)))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } @@ -1752,7 +1752,7 @@ 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(MathUtil.truncateNumber(value, 1).doubleValue()))); + hasValue(MathUtil.truncateFloat(value, 1)))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } @@ -1765,7 +1765,7 @@ 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(MathUtil.truncateNumber(value, 1).doubleValue()))); + hasValue(MathUtil.truncateDouble(value, 1)))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } From 10ae05332f2d989d558fb3d337d055f1ec20f71e Mon Sep 17 00:00:00 2001 From: Margarit Hakobyan Date: Mon, 12 Dec 2022 16:15:09 -0800 Subject: [PATCH 03/10] Edit a javadoc Signed-off-by: Margarit Hakobyan --- .../sql/expression/operator/arthmetic/MathUtil.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java index ce6f6f2d13..ae95374032 100644 --- a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java +++ b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java @@ -16,7 +16,7 @@ public class MathUtil { * * @param numberToTruncate number to be truncated * @param numberOfDecimals required decimal places - * @return truncated number as {@link BigDecimal} + * @return truncated number as double */ public static double truncateDouble(double numberToTruncate, int numberOfDecimals) { return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, @@ -28,7 +28,7 @@ public static double truncateDouble(double numberToTruncate, int numberOfDecimal * * @param numberToTruncate number to be truncated * @param numberOfDecimals required decimal places - * @return truncated number as {@link BigDecimal} + * @return truncated number as double */ public static double truncateFloat(float numberToTruncate, int numberOfDecimals) { return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, @@ -40,7 +40,7 @@ public static double truncateFloat(float numberToTruncate, int numberOfDecimals) * * @param numberToTruncate number to be truncated * @param numberOfDecimals required decimal places - * @return truncated number as {@link BigDecimal} + * @return truncated number as long */ public static long truncateInt(int numberToTruncate, int numberOfDecimals) { return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, @@ -52,7 +52,7 @@ public static long truncateInt(int numberToTruncate, int numberOfDecimals) { * * @param numberToTruncate number to be truncated * @param numberOfDecimals required decimal places - * @return truncated number as {@link BigDecimal} + * @return truncated number as long */ public static long truncateLong(long numberToTruncate, int numberOfDecimals) { return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, From a9e18e53fe9bdcceef0959732a6e7f0eae3a1d3c Mon Sep 17 00:00:00 2001 From: Margarit Hakobyan Date: Tue, 13 Dec 2022 09:57:22 -0800 Subject: [PATCH 04/10] Add/fix tests Signed-off-by: Margarit Hakobyan --- .../operator/arthmetic/MathUtil.java | 4 ++ .../operator/arthmetic/MathUtilTest.java | 12 ++++-- .../sql/legacy/SQLIntegTestCase.java | 6 ++- .../opensearch/sql/legacy/TestsConstants.java | 1 + .../sql/sql/MathematicalFunctionIT.java | 27 +++++++++---- integ-test/src/test/resources/ddouble.json | 40 +++++++++++++++++++ .../ddouble_index_mapping.json | 12 ++++++ 7 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 integ-test/src/test/resources/ddouble.json create mode 100644 integ-test/src/test/resources/indexDefinitions/ddouble_index_mapping.json diff --git a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java index ae95374032..47c28c6000 100644 --- a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java +++ b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java @@ -11,6 +11,10 @@ public class MathUtil { + private MathUtil() { + // Utility class + } + /** * Truncates a double number to required decimal places. * diff --git a/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathUtilTest.java b/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathUtilTest.java index dac190b601..29fdc76351 100644 --- a/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathUtilTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathUtilTest.java @@ -1,6 +1,10 @@ package org.opensearch.sql.expression.operator.arthmetic; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.opensearch.sql.expression.operator.arthmetic.MathUtil.truncateDouble; +import static org.opensearch.sql.expression.operator.arthmetic.MathUtil.truncateFloat; +import static org.opensearch.sql.expression.operator.arthmetic.MathUtil.truncateInt; +import static org.opensearch.sql.expression.operator.arthmetic.MathUtil.truncateLong; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -13,28 +17,28 @@ class MathUtilTest { @ParameterizedTest @ValueSource(doubles = {11.2D, 22.5678D, -1.2D}) void testTruncateDouble(final Double value) { - String result = Double.toString(MathUtil.truncateDouble(value, 1)); + String result = Double.toString(truncateDouble(value, 1)); assertEquals(Double.toString(value).substring(0,4), result); } @ParameterizedTest(name = "truncate({0}, {1})") @ValueSource(floats = {11.2F, 22.5678F, -1.2F}) void testTruncateFloat(final Float value) { - String result = Double.toString(MathUtil.truncateFloat(value, 1)); + String result = Double.toString(truncateFloat(value, 1)); assertEquals(Float.toString(value).substring(0,4), result); } @ParameterizedTest @ValueSource(longs = {2056L, -777L}) void testTruncateLong(final Long value) { - String result = Long.toString(MathUtil.truncateLong(value, 1)); + String result = Long.toString(truncateLong(value, 1)); assertEquals(Long.toString(value).substring(0,4), result); } @ParameterizedTest @ValueSource(ints = {11, 22, -7}) void testTruncateInt(final int value) { - String result = Long.toString(MathUtil.truncateInt(value, 1)); + String result = Long.toString(truncateInt(value, 1)); assertEquals(Integer.toString(value).substring(0,2), result); } } \ No newline at end of file diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java b/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java index 80348b2a8b..2229b8826e 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java @@ -588,7 +588,11 @@ public enum Index { WILDCARD(TestsConstants.TEST_INDEX_WILDCARD, "wildcard", getMappingFile("wildcard_index_mappings.json"), - "src/test/resources/wildcard.json"),; + "src/test/resources/wildcard.json"), + DDOUBLE(TestsConstants.TEST_INDEX_DDOUBLE, + "ddouble", + getMappingFile("ddouble_index_mapping.json"), + "src/test/resources/ddouble.json"),; private final String name; private final String type; diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/TestsConstants.java b/integ-test/src/test/java/org/opensearch/sql/legacy/TestsConstants.java index aff269fcce..0aa543ed90 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/TestsConstants.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/TestsConstants.java @@ -54,6 +54,7 @@ public class TestsConstants { public final static String TEST_INDEX_NULL_MISSING = TEST_INDEX + "_null_missing"; public final static String TEST_INDEX_CALCS = TEST_INDEX + "_calcs"; public final static String TEST_INDEX_WILDCARD = TEST_INDEX + "_wildcard"; + public final static String TEST_INDEX_DDOUBLE = TEST_INDEX + "_ddouble"; public final static String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; public final static String TS_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; 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 ff2776dfd7..4b6e3c7a58 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 @@ -7,6 +7,7 @@ package org.opensearch.sql.sql; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK; +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DDOUBLE; import static org.opensearch.sql.legacy.plugin.RestSqlAction.QUERY_API_ENDPOINT; import static org.opensearch.sql.util.MatcherUtils.rows; import static org.opensearch.sql.util.MatcherUtils.schema; @@ -29,6 +30,7 @@ public class MathematicalFunctionIT extends SQLIntegTestCase { public void init() throws Exception { super.init(); loadIndex(Index.BANK); + loadIndex(Index.DDOUBLE); } @Test @@ -143,13 +145,24 @@ public void testTruncate() throws IOException { verifySchema(result, schema("truncate(-56, -1)", null, "long")); verifyDataRows(result, rows(-50)); - result = executeQuery("select truncate(-1.2, 1)"); - verifySchema(result, schema("truncate(-1.2, 1)", null, "double")); - verifyDataRows(result, rows(-1.2)); - - result = executeQuery("select truncate(1004.3, 1)"); - verifySchema(result, schema("truncate(1004.3, 1)", null, "double")); - verifyDataRows(result, rows(1004.3)); + String query = "select val, truncate(val, 1) from %s"; + JSONObject response = executeJdbcRequest(String.format(query, TEST_INDEX_DDOUBLE)); + verifySchema(response, schema("val", null, "double"), + schema("truncate(val, 1)", null, "double")); + assertEquals(20, response.getInt("total")); + + verifyDataRows(response, + rows(null, null), rows(-9.223372036854776e+18, -9.223372036854776e+18), + rows(-2147483649.0, -2147483649.0), rows(-2147483648.0, -2147483648.0), + rows(-32769.0, -32769.0), rows(-32768.0, -32768.0), + rows(-34.84, -34.8), rows(-2.0, -2.0), + rows(-1.2, -1.2), rows(-1.0, -1.0), + rows(0.0 , 0.0), rows(1.0, 1.0), + rows(1.3, 1.3), rows(2.0, 2.0), + rows(1004.3, 1004.3), rows(32767.0 , 32767.0 ), + rows(32768.0 , 32768.0 ), rows(2147483647.0, 2147483647.0), + rows(2147483648.0, 2147483648.0), + rows(9.223372036854776e+18, 9.223372036854776e+18)); } @Test diff --git a/integ-test/src/test/resources/ddouble.json b/integ-test/src/test/resources/ddouble.json new file mode 100644 index 0000000000..964da9d5c5 --- /dev/null +++ b/integ-test/src/test/resources/ddouble.json @@ -0,0 +1,40 @@ +{ "index" : { "_id" : "1" } } +{"key": "null", "val": null} +{ "index" : { "_id" : "2" } } +{"key": "001: min long", "val": -9.223372036854776e+18} +{ "index" : { "_id" : "3" } } +{"key": "002: min int minus one", "val": -2147483649.0} +{ "index" : { "_id" : "4" } } +{"key": "003: min int", "val": -2147483648.0} +{ "index" : { "_id" : "5" } } +{"key": "004: min short minus one", "val": -32769.0} +{ "index" : { "_id" : "6" } } +{"key": "005: min short", "val": -32768.0} +{ "index" : { "_id" : "7" } } +{"key": "006: pgtest float8 value 2", "val": -34.84} +{ "index" : { "_id" : "8" } } +{"key": "007: -two", "val": -2.0} +{ "index" : { "_id" : "9" } } +{"key": "008: -1.2", "val": -1.2} +{ "index" : { "_id" : "10" } } +{"key": "009: -one", "val": -1.0} +{ "index" : { "_id" : "11" } } +{"key": "010: zero", "val": 0.0} +{ "index" : { "_id" : "12" } } +{"key": "011: one", "val": 1.0} +{ "index" : { "_id" : "13" } } +{"key": "012: 1.3", "val": 1.3} +{ "index" : { "_id" : "14" } } +{"key": "013: two", "val": 2.0} +{ "index" : { "_id" : "15" } } +{"key": "014: pgtest float8 value 1", "val": 1004.3} +{ "index" : { "_id" : "16" } } +{"key": "015: max short", "val": 32767.0} +{ "index" : { "_id" : "17" } } +{"key": "016: max short plus one", "val": 32768.0} +{ "index" : { "_id" : "18" } } +{"key": "017: max int", "val": 2147483647.0} +{ "index" : { "_id" : "19" } } +{"key": "018: max int plus one", "val": 2147483648.0} +{ "index" : { "_id" : "20" } } +{"key": "019: max long", "val": 9.223372036854776e+18} diff --git a/integ-test/src/test/resources/indexDefinitions/ddouble_index_mapping.json b/integ-test/src/test/resources/indexDefinitions/ddouble_index_mapping.json new file mode 100644 index 0000000000..55894a2c2a --- /dev/null +++ b/integ-test/src/test/resources/indexDefinitions/ddouble_index_mapping.json @@ -0,0 +1,12 @@ +{ + "mappings" : { + "properties" : { + "key" : { + "type" : "keyword" + }, + "val" : { + "type" : "double" + } + } + } +} From ba76b90305270378d67a959ba65ae0b2746b745a Mon Sep 17 00:00:00 2001 From: Margarit Hakobyan Date: Tue, 13 Dec 2022 13:51:58 -0800 Subject: [PATCH 05/10] Address PR review feedback Signed-off-by: Margarit Hakobyan --- .../operator/arthmetic/MathUtil.java | 65 ------------------- .../arthmetic/MathematicalFunction.java | 16 +++-- .../operator/arthmetic/MathUtilTest.java | 44 ------------- .../arthmetic/MathematicalFunctionTest.java | 12 ++-- 4 files changed, 20 insertions(+), 117 deletions(-) delete mode 100644 core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java delete mode 100644 core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathUtilTest.java diff --git a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java b/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java deleted file mode 100644 index 47c28c6000..0000000000 --- a/core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - - -package org.opensearch.sql.expression.operator.arthmetic; - -import java.math.BigDecimal; -import java.math.RoundingMode; - -public class MathUtil { - - private MathUtil() { - // Utility class - } - - /** - * Truncates a double number to required decimal places. - * - * @param numberToTruncate number to be truncated - * @param numberOfDecimals required decimal places - * @return truncated number as double - */ - public static double truncateDouble(double numberToTruncate, int numberOfDecimals) { - return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, - numberToTruncate > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).doubleValue(); - } - - /** - * Truncates a float number to required decimal places. - * - * @param numberToTruncate number to be truncated - * @param numberOfDecimals required decimal places - * @return truncated number as double - */ - public static double truncateFloat(float numberToTruncate, int numberOfDecimals) { - return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, - numberToTruncate > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).doubleValue(); - } - - /** - * Truncates an int number to required decimal places. - * - * @param numberToTruncate number to be truncated - * @param numberOfDecimals required decimal places - * @return truncated number as long - */ - public static long truncateInt(int numberToTruncate, int numberOfDecimals) { - return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, - numberToTruncate > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).longValue(); - } - - /** - * Truncates a long number to required decimal places. - * - * @param numberToTruncate number to be truncated - * @param numberOfDecimals required decimal places - * @return truncated number as long - */ - public static long truncateLong(long numberToTruncate, int numberOfDecimals) { - return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, - numberToTruncate > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).longValue(); - } -} 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 220801f23e..35667225ad 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,22 +500,30 @@ private static DefaultFunctionResolver truncate() { FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprLongValue( - MathUtil.truncateInt(x.integerValue(), y.integerValue()))), + new BigDecimal(String.valueOf(x.integerValue())).setScale(y.integerValue(), + x.integerValue() > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING) + .longValue())), LONG, INTEGER, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprLongValue( - MathUtil.truncateLong(x.longValue(), y.integerValue()))), + new BigDecimal(String.valueOf(x.longValue())).setScale(y.integerValue(), + x.longValue() > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING) + .longValue())), LONG, LONG, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprDoubleValue( - MathUtil.truncateFloat(x.floatValue(), y.integerValue()))), + new BigDecimal(String.valueOf(x.floatValue())).setScale(y.integerValue(), + x.floatValue() > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING) + .doubleValue())), DOUBLE, FLOAT, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprDoubleValue( - MathUtil.truncateDouble(x.doubleValue(), y.integerValue()))), + new BigDecimal(String.valueOf(x.doubleValue())).setScale(y.integerValue(), + x.doubleValue() > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING) + .doubleValue())), DOUBLE, DOUBLE, INTEGER)); } diff --git a/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathUtilTest.java b/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathUtilTest.java deleted file mode 100644 index 29fdc76351..0000000000 --- a/core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathUtilTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.opensearch.sql.expression.operator.arthmetic; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.opensearch.sql.expression.operator.arthmetic.MathUtil.truncateDouble; -import static org.opensearch.sql.expression.operator.arthmetic.MathUtil.truncateFloat; -import static org.opensearch.sql.expression.operator.arthmetic.MathUtil.truncateInt; -import static org.opensearch.sql.expression.operator.arthmetic.MathUtil.truncateLong; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -/** - * Test class for {@link MathUtil}. - */ -class MathUtilTest { - - @ParameterizedTest - @ValueSource(doubles = {11.2D, 22.5678D, -1.2D}) - void testTruncateDouble(final Double value) { - String result = Double.toString(truncateDouble(value, 1)); - assertEquals(Double.toString(value).substring(0,4), result); - } - - @ParameterizedTest(name = "truncate({0}, {1})") - @ValueSource(floats = {11.2F, 22.5678F, -1.2F}) - void testTruncateFloat(final Float value) { - String result = Double.toString(truncateFloat(value, 1)); - assertEquals(Float.toString(value).substring(0,4), result); - } - - @ParameterizedTest - @ValueSource(longs = {2056L, -777L}) - void testTruncateLong(final Long value) { - String result = Long.toString(truncateLong(value, 1)); - assertEquals(Long.toString(value).substring(0,4), result); - } - - @ParameterizedTest - @ValueSource(ints = {11, 22, -7}) - void testTruncateInt(final int value) { - String result = Long.toString(truncateInt(value, 1)); - assertEquals(Integer.toString(value).substring(0,2), result); - } -} \ No newline at end of file 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 1249cc77ab..8901bff451 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 @@ -1726,7 +1726,8 @@ 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(MathUtil.truncateInt(value, 1)))); + hasValue(new BigDecimal(String.valueOf(value)).setScale(1, + value > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).longValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } @@ -1739,7 +1740,8 @@ 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(MathUtil.truncateLong(value, 1)))); + hasValue(new BigDecimal(String.valueOf(value)).setScale(1, + value > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).longValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } @@ -1752,7 +1754,8 @@ 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(MathUtil.truncateFloat(value, 1)))); + hasValue(new BigDecimal(String.valueOf(value)).setScale(1, + value > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).doubleValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } @@ -1765,7 +1768,8 @@ 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(MathUtil.truncateDouble(value, 1)))); + hasValue(new BigDecimal(String.valueOf(value)).setScale(1, + value > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).doubleValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } From d91ec92a90b89209ff53d79ac963e52d70056b7c Mon Sep 17 00:00:00 2001 From: Margarit Hakobyan Date: Wed, 14 Dec 2022 08:41:01 -0800 Subject: [PATCH 06/10] Added more tests Signed-off-by: Margarit Hakobyan --- .../sql/sql/MathematicalFunctionIT.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) 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 4b6e3c7a58..4afa498bf2 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 @@ -145,6 +145,30 @@ public void testTruncate() throws IOException { 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)); + String query = "select val, truncate(val, 1) from %s"; JSONObject response = executeJdbcRequest(String.format(query, TEST_INDEX_DDOUBLE)); verifySchema(response, schema("val", null, "double"), From 21619b4d0a798e8791d775d11da8c2678290094c Mon Sep 17 00:00:00 2001 From: Margarit Hakobyan Date: Wed, 14 Dec 2022 15:34:46 -0800 Subject: [PATCH 07/10] Addressed more PR review feedback Signed-off-by: Margarit Hakobyan --- .../operator/arthmetic/MathematicalFunction.java | 8 ++++---- .../operator/arthmetic/MathematicalFunctionTest.java | 8 ++++---- .../java/org/opensearch/sql/legacy/SQLIntegTestCase.java | 6 +++--- .../java/org/opensearch/sql/legacy/TestsConstants.java | 2 +- .../org/opensearch/sql/sql/MathematicalFunctionIT.java | 6 +++--- .../src/test/resources/{ddouble.json => double.json} | 0 ...ouble_index_mapping.json => double_index_mapping.json} | 0 7 files changed, 15 insertions(+), 15 deletions(-) rename integ-test/src/test/resources/{ddouble.json => double.json} (100%) rename integ-test/src/test/resources/indexDefinitions/{ddouble_index_mapping.json => double_index_mapping.json} (100%) 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 35667225ad..31e958fa3b 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,28 +500,28 @@ private static DefaultFunctionResolver truncate() { FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprLongValue( - new BigDecimal(String.valueOf(x.integerValue())).setScale(y.integerValue(), + BigDecimal.valueOf(x.integerValue()).setScale(y.integerValue(), x.integerValue() > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING) .longValue())), LONG, INTEGER, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprLongValue( - new BigDecimal(String.valueOf(x.longValue())).setScale(y.integerValue(), + BigDecimal.valueOf(x.longValue()).setScale(y.integerValue(), x.longValue() > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING) .longValue())), LONG, LONG, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprDoubleValue( - new BigDecimal(String.valueOf(x.floatValue())).setScale(y.integerValue(), + BigDecimal.valueOf(x.floatValue()).setScale(y.integerValue(), x.floatValue() > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING) .doubleValue())), DOUBLE, FLOAT, INTEGER), FunctionDSL.impl( FunctionDSL.nullMissingHandling( (x, y) -> new ExprDoubleValue( - new BigDecimal(String.valueOf(x.doubleValue())).setScale(y.integerValue(), + BigDecimal.valueOf(x.doubleValue()).setScale(y.integerValue(), x.doubleValue() > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING) .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 8901bff451..b48558ff9b 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 @@ -1726,7 +1726,7 @@ 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(String.valueOf(value)).setScale(1, + hasValue(BigDecimal.valueOf(value).setScale(1, value > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).longValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } @@ -1740,7 +1740,7 @@ 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(String.valueOf(value)).setScale(1, + hasValue(BigDecimal.valueOf(value).setScale(1, value > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).longValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } @@ -1754,7 +1754,7 @@ 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(String.valueOf(value)).setScale(1, + hasValue(BigDecimal.valueOf(value).setScale(1, value > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).doubleValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } @@ -1768,7 +1768,7 @@ 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(String.valueOf(value)).setScale(1, + hasValue(BigDecimal.valueOf(value).setScale(1, value > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).doubleValue()))); assertEquals(String.format("truncate(%s, 1)", value), truncate.toString()); } diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java b/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java index 2229b8826e..3edc3b210c 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java @@ -589,10 +589,10 @@ public enum Index { "wildcard", getMappingFile("wildcard_index_mappings.json"), "src/test/resources/wildcard.json"), - DDOUBLE(TestsConstants.TEST_INDEX_DDOUBLE, + DOUBLE(TestsConstants.TEST_INDEX_DOUBLE, "ddouble", - getMappingFile("ddouble_index_mapping.json"), - "src/test/resources/ddouble.json"),; + getMappingFile("double_index_mapping.json"), + "src/test/resources/double.json"),; private final String name; private final String type; diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/TestsConstants.java b/integ-test/src/test/java/org/opensearch/sql/legacy/TestsConstants.java index 0aa543ed90..af41fb73af 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/TestsConstants.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/TestsConstants.java @@ -54,7 +54,7 @@ public class TestsConstants { public final static String TEST_INDEX_NULL_MISSING = TEST_INDEX + "_null_missing"; public final static String TEST_INDEX_CALCS = TEST_INDEX + "_calcs"; public final static String TEST_INDEX_WILDCARD = TEST_INDEX + "_wildcard"; - public final static String TEST_INDEX_DDOUBLE = TEST_INDEX + "_ddouble"; + public final static String TEST_INDEX_DOUBLE = TEST_INDEX + "_double"; public final static String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; public final static String TS_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; 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 4afa498bf2..d343e10072 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 @@ -7,7 +7,7 @@ package org.opensearch.sql.sql; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK; -import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DDOUBLE; +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DOUBLE; import static org.opensearch.sql.legacy.plugin.RestSqlAction.QUERY_API_ENDPOINT; import static org.opensearch.sql.util.MatcherUtils.rows; import static org.opensearch.sql.util.MatcherUtils.schema; @@ -30,7 +30,7 @@ public class MathematicalFunctionIT extends SQLIntegTestCase { public void init() throws Exception { super.init(); loadIndex(Index.BANK); - loadIndex(Index.DDOUBLE); + loadIndex(Index.DOUBLE); } @Test @@ -170,7 +170,7 @@ public void testTruncate() throws IOException { verifyDataRows(result, rows(3.141592)); String query = "select val, truncate(val, 1) from %s"; - JSONObject response = executeJdbcRequest(String.format(query, TEST_INDEX_DDOUBLE)); + JSONObject response = executeJdbcRequest(String.format(query, TEST_INDEX_DOUBLE)); verifySchema(response, schema("val", null, "double"), schema("truncate(val, 1)", null, "double")); assertEquals(20, response.getInt("total")); diff --git a/integ-test/src/test/resources/ddouble.json b/integ-test/src/test/resources/double.json similarity index 100% rename from integ-test/src/test/resources/ddouble.json rename to integ-test/src/test/resources/double.json diff --git a/integ-test/src/test/resources/indexDefinitions/ddouble_index_mapping.json b/integ-test/src/test/resources/indexDefinitions/double_index_mapping.json similarity index 100% rename from integ-test/src/test/resources/indexDefinitions/ddouble_index_mapping.json rename to integ-test/src/test/resources/indexDefinitions/double_index_mapping.json From 8599dbd96727332d72445e5749b2694454906c90 Mon Sep 17 00:00:00 2001 From: Margarit Hakobyan Date: Wed, 14 Dec 2022 15:42:51 -0800 Subject: [PATCH 08/10] minor fix Signed-off-by: Margarit Hakobyan --- .../test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java b/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java index 3edc3b210c..714bf225dc 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java @@ -590,7 +590,7 @@ public enum Index { getMappingFile("wildcard_index_mappings.json"), "src/test/resources/wildcard.json"), DOUBLE(TestsConstants.TEST_INDEX_DOUBLE, - "ddouble", + "double", getMappingFile("double_index_mapping.json"), "src/test/resources/double.json"),; From 562209428e5926b49c3270e4693354bcb870afda Mon Sep 17 00:00:00 2001 From: Margarit Hakobyan Date: Mon, 19 Dec 2022 11:36:56 -0800 Subject: [PATCH 09/10] Clean up, apdate a unit test Signed-off-by: Margarit Hakobyan --- .../arthmetic/MathematicalFunctionTest.java | 5 ++- .../sql/legacy/SQLIntegTestCase.java | 6 +-- .../opensearch/sql/legacy/TestsConstants.java | 1 - .../sql/sql/MathematicalFunctionIT.java | 21 ---------- integ-test/src/test/resources/double.json | 40 ------------------- .../double_index_mapping.json | 12 ------ 6 files changed, 5 insertions(+), 80 deletions(-) delete mode 100644 integ-test/src/test/resources/double.json delete mode 100644 integ-test/src/test/resources/indexDefinitions/double_index_mapping.json 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 b48558ff9b..a3c053c585 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 @@ -1763,7 +1763,10 @@ public void truncate_float_value(Float value) { * Test truncate with double value. */ @ParameterizedTest(name = "truncate({0}, {1})") - @ValueSource(doubles = {2D, -1.2D, Double.MAX_VALUE, Double.MIN_VALUE}) + @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( diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java b/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java index 714bf225dc..6bda397fce 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java @@ -588,11 +588,7 @@ public enum Index { WILDCARD(TestsConstants.TEST_INDEX_WILDCARD, "wildcard", getMappingFile("wildcard_index_mappings.json"), - "src/test/resources/wildcard.json"), - DOUBLE(TestsConstants.TEST_INDEX_DOUBLE, - "double", - getMappingFile("double_index_mapping.json"), - "src/test/resources/double.json"),; + "src/test/resources/wildcard.json"),; private final String name; private final String type; diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/TestsConstants.java b/integ-test/src/test/java/org/opensearch/sql/legacy/TestsConstants.java index af41fb73af..aff269fcce 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/TestsConstants.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/TestsConstants.java @@ -54,7 +54,6 @@ public class TestsConstants { public final static String TEST_INDEX_NULL_MISSING = TEST_INDEX + "_null_missing"; public final static String TEST_INDEX_CALCS = TEST_INDEX + "_calcs"; public final static String TEST_INDEX_WILDCARD = TEST_INDEX + "_wildcard"; - public final static String TEST_INDEX_DOUBLE = TEST_INDEX + "_double"; public final static String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; public final static String TS_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; 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 d343e10072..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 @@ -7,7 +7,6 @@ package org.opensearch.sql.sql; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK; -import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DOUBLE; import static org.opensearch.sql.legacy.plugin.RestSqlAction.QUERY_API_ENDPOINT; import static org.opensearch.sql.util.MatcherUtils.rows; import static org.opensearch.sql.util.MatcherUtils.schema; @@ -30,7 +29,6 @@ public class MathematicalFunctionIT extends SQLIntegTestCase { public void init() throws Exception { super.init(); loadIndex(Index.BANK); - loadIndex(Index.DOUBLE); } @Test @@ -168,25 +166,6 @@ public void testTruncate() throws IOException { 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)); - - String query = "select val, truncate(val, 1) from %s"; - JSONObject response = executeJdbcRequest(String.format(query, TEST_INDEX_DOUBLE)); - verifySchema(response, schema("val", null, "double"), - schema("truncate(val, 1)", null, "double")); - assertEquals(20, response.getInt("total")); - - verifyDataRows(response, - rows(null, null), rows(-9.223372036854776e+18, -9.223372036854776e+18), - rows(-2147483649.0, -2147483649.0), rows(-2147483648.0, -2147483648.0), - rows(-32769.0, -32769.0), rows(-32768.0, -32768.0), - rows(-34.84, -34.8), rows(-2.0, -2.0), - rows(-1.2, -1.2), rows(-1.0, -1.0), - rows(0.0 , 0.0), rows(1.0, 1.0), - rows(1.3, 1.3), rows(2.0, 2.0), - rows(1004.3, 1004.3), rows(32767.0 , 32767.0 ), - rows(32768.0 , 32768.0 ), rows(2147483647.0, 2147483647.0), - rows(2147483648.0, 2147483648.0), - rows(9.223372036854776e+18, 9.223372036854776e+18)); } @Test diff --git a/integ-test/src/test/resources/double.json b/integ-test/src/test/resources/double.json deleted file mode 100644 index 964da9d5c5..0000000000 --- a/integ-test/src/test/resources/double.json +++ /dev/null @@ -1,40 +0,0 @@ -{ "index" : { "_id" : "1" } } -{"key": "null", "val": null} -{ "index" : { "_id" : "2" } } -{"key": "001: min long", "val": -9.223372036854776e+18} -{ "index" : { "_id" : "3" } } -{"key": "002: min int minus one", "val": -2147483649.0} -{ "index" : { "_id" : "4" } } -{"key": "003: min int", "val": -2147483648.0} -{ "index" : { "_id" : "5" } } -{"key": "004: min short minus one", "val": -32769.0} -{ "index" : { "_id" : "6" } } -{"key": "005: min short", "val": -32768.0} -{ "index" : { "_id" : "7" } } -{"key": "006: pgtest float8 value 2", "val": -34.84} -{ "index" : { "_id" : "8" } } -{"key": "007: -two", "val": -2.0} -{ "index" : { "_id" : "9" } } -{"key": "008: -1.2", "val": -1.2} -{ "index" : { "_id" : "10" } } -{"key": "009: -one", "val": -1.0} -{ "index" : { "_id" : "11" } } -{"key": "010: zero", "val": 0.0} -{ "index" : { "_id" : "12" } } -{"key": "011: one", "val": 1.0} -{ "index" : { "_id" : "13" } } -{"key": "012: 1.3", "val": 1.3} -{ "index" : { "_id" : "14" } } -{"key": "013: two", "val": 2.0} -{ "index" : { "_id" : "15" } } -{"key": "014: pgtest float8 value 1", "val": 1004.3} -{ "index" : { "_id" : "16" } } -{"key": "015: max short", "val": 32767.0} -{ "index" : { "_id" : "17" } } -{"key": "016: max short plus one", "val": 32768.0} -{ "index" : { "_id" : "18" } } -{"key": "017: max int", "val": 2147483647.0} -{ "index" : { "_id" : "19" } } -{"key": "018: max int plus one", "val": 2147483648.0} -{ "index" : { "_id" : "20" } } -{"key": "019: max long", "val": 9.223372036854776e+18} diff --git a/integ-test/src/test/resources/indexDefinitions/double_index_mapping.json b/integ-test/src/test/resources/indexDefinitions/double_index_mapping.json deleted file mode 100644 index 55894a2c2a..0000000000 --- a/integ-test/src/test/resources/indexDefinitions/double_index_mapping.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "mappings" : { - "properties" : { - "key" : { - "type" : "keyword" - }, - "val" : { - "type" : "double" - } - } - } -} From 3cde7ce6a6311b5d9a915ea9b8fc20afea69970e Mon Sep 17 00:00:00 2001 From: Margarit Hakobyan Date: Mon, 19 Dec 2022 11:42:31 -0800 Subject: [PATCH 10/10] minor fix Signed-off-by: Margarit Hakobyan --- .../test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java b/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java index 6bda397fce..80348b2a8b 100644 --- a/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java +++ b/integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java @@ -588,7 +588,7 @@ public enum Index { WILDCARD(TestsConstants.TEST_INDEX_WILDCARD, "wildcard", getMappingFile("wildcard_index_mappings.json"), - "src/test/resources/wildcard.json"),; + "src/test/resources/wildcard.json"),; private final String name; private final String type;