Skip to content

Commit

Permalink
Fix truncate() function (#1197)
Browse files Browse the repository at this point in the history
* Fix truncate() function (#188)

Signed-off-by: Margarit Hakobyan <[email protected]>
Signed-off-by: Margarit Hakobyan <[email protected]>
(cherry picked from commit 7714819)
  • Loading branch information
margarit-h authored and github-actions[bot] committed Jan 3, 2023
1 parent 753847b commit e14cca1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

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,54 @@ 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());
}

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

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

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

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

0 comments on commit e14cca1

Please sign in to comment.