Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix truncate() function #188

Merged
merged 10 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -500,26 +500,30 @@ private static DefaultFunctionResolver truncate() {
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(x, y) -> new ExprLongValue(
new BigDecimal(x.integerValue()).setScale(y.integerValue(),
RoundingMode.DOWN).longValue())),
new BigDecimal(String.valueOf(x.integerValue())).setScale(y.integerValue(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new BigDecimal(String.valueOf(x.integerValue())).setScale(y.integerValue(),
BigDecimal.valueOf(x.integerValue()).setScale(y.integerValue(),

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 21619b4

x.integerValue() > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING)
.longValue())),
LONG, INTEGER, INTEGER),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(x, y) -> new ExprLongValue(
new BigDecimal(x.integerValue()).setScale(y.integerValue(),
RoundingMode.DOWN).longValue())),
new BigDecimal(String.valueOf(x.longValue())).setScale(y.integerValue(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new BigDecimal(String.valueOf(x.longValue())).setScale(y.integerValue(),
BigDecimal.valueOf(x.longValue()).setScale(y.integerValue(),

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 21619b4

x.longValue() > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING)
.longValue())),
LONG, LONG, INTEGER),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(x, y) -> new ExprDoubleValue(
new BigDecimal(x.floatValue()).setScale(y.integerValue(),
RoundingMode.DOWN).doubleValue())),
new BigDecimal(String.valueOf(x.floatValue())).setScale(y.integerValue(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new BigDecimal(String.valueOf(x.floatValue())).setScale(y.integerValue(),
BigDecimal.valueOf(x.floatValue()).setScale(y.integerValue(),

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 21619b4

x.floatValue() > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING)
.doubleValue())),
DOUBLE, FLOAT, INTEGER),
FunctionDSL.impl(
FunctionDSL.nullMissingHandling(
(x, y) -> new ExprDoubleValue(
new BigDecimal(x.doubleValue()).setScale(y.integerValue(),
RoundingMode.DOWN).doubleValue())),
new BigDecimal(String.valueOf(x.doubleValue())).setScale(y.integerValue(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new BigDecimal(String.valueOf(x.doubleValue())).setScale(y.integerValue(),
new BigDecimal.valueOf(x.doubleValue()).setScale(y.integerValue(),

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 21619b4

x.doubleValue() > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING)
.doubleValue())),
DOUBLE, DOUBLE, INTEGER));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -1721,51 +1721,55 @@ 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(new BigDecimal(String.valueOf(value)).setScale(1,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
hasValue(new BigDecimal(String.valueOf(value)).setScale(1,
hasValue(BigDecimal.valueOf(value).setScale(1,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 21619b4

value > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).longValue())));
assertEquals(String.format("truncate(%s, 1)", value), truncate.toString());
}

/**
* 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(new BigDecimal(String.valueOf(value)).setScale(1,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
hasValue(new BigDecimal(String.valueOf(value)).setScale(1,
hasValue(BigDecimal.valueOf(value).setScale(1,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 21619b4

value > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).longValue())));
assertEquals(String.format("truncate(%s, 1)", value), truncate.toString());
}

/**
* 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(new BigDecimal(String.valueOf(value)).setScale(1,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
hasValue(new BigDecimal(String.valueOf(value)).setScale(1,
hasValue(BigDecimal.valueOf(value).setScale(1,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 21619b4

value > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).doubleValue())));
assertEquals(String.format("truncate(%s, 1)", value), truncate.toString());
}

/**
* 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})
acarbonetto marked this conversation as resolved.
Show resolved Hide resolved
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(new BigDecimal(String.valueOf(value)).setScale(1,
MaxKsyunz marked this conversation as resolved.
Show resolved Hide resolved
value > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).doubleValue())));
assertEquals(String.format("truncate(%s, 1)", value), truncate.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
MaxKsyunz marked this conversation as resolved.
Show resolved Hide resolved
"src/test/resources/ddouble.json"),;

private final String name;
private final String type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,7 @@ public class MathematicalFunctionIT extends SQLIntegTestCase {
public void init() throws Exception {
super.init();
loadIndex(Index.BANK);
loadIndex(Index.DDOUBLE);
}

@Test
Expand Down Expand Up @@ -142,6 +144,25 @@ public void testTruncate() throws IOException {
result = executeQuery("select truncate(-56, -1)");
verifySchema(result, schema("truncate(-56, -1)", null, "long"));
verifyDataRows(result, rows(-50));

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
Expand Down
40 changes: 40 additions & 0 deletions integ-test/src/test/resources/ddouble.json
Original file line number Diff line number Diff line change
@@ -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}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"mappings" : {
"properties" : {
"key" : {
"type" : "keyword"
},
"val" : {
"type" : "double"
}
}
}
}