Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -191,7 +191,13 @@ else if (type.equals(REAL)) {
type.writeLong(output, floatToIntBits(((float) ((Number) value).doubleValue())));
}
else if (type instanceof DecimalType) {
type.writeLong(output, encodeShortScaledValue(((Decimal128) value).bigDecimalValue(), ((DecimalType) type).getScale()));
Decimal128 decimal = (Decimal128) value;
if (decimal.compareTo(Decimal128.NEGATIVE_ZERO) == 0) {
type.writeLong(output, encodeShortScaledValue(BigDecimal.ZERO, ((DecimalType) type).getScale()));
}
else {
type.writeLong(output, encodeShortScaledValue(decimal.bigDecimalValue(), ((DecimalType) type).getScale()));
}
}
else if (type.equals(DATE)) {
long utcMillis = ((Date) value).getTime();
Expand All @@ -218,8 +224,14 @@ else if (javaType == double.class) {
else if (javaType == Int128.class) {
DecimalType decimalType = (DecimalType) type;
verify(!decimalType.isShort(), "The type should be long decimal");
BigDecimal decimal = ((Decimal128) value).bigDecimalValue();
type.writeObject(output, Decimals.encodeScaledValue(decimal, decimalType.getScale()));
Decimal128 decimal = (Decimal128) value;
if (decimal.compareTo(Decimal128.NEGATIVE_ZERO) == 0) {
type.writeObject(output, Decimals.encodeScaledValue(BigDecimal.ZERO, decimalType.getScale()));
}
else {
BigDecimal result = decimal.bigDecimalValue();
type.writeObject(output, Decimals.encodeScaledValue(result, decimalType.getScale()));
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I didn't touch guessFieldType method because the precision and scale are still unclear in the case of negative zero.

}
else if (javaType == Slice.class) {
writeSlice(output, type, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,37 @@ public void testSkipUnsupportedDecimal128()
assertUpdate("DROP TABLE test." + tableName);
}

@Test
public void testNegativeZeroDecimal()
{
String tableName = "test_negative_zero" + randomNameSuffix();

assertUpdate("CREATE TABLE test." + tableName + "(id int, short_decimal decimal(1), long_decimal decimal(38))");
client.getDatabase("test").getCollection(tableName)
.insertOne(new Document(ImmutableMap.<String, Object>builder()
.put("id", 1)
.put("short_decimal", Decimal128.NEGATIVE_ZERO)
.put("long_decimal", Decimal128.NEGATIVE_ZERO)
.buildOrThrow()));
client.getDatabase("test").getCollection(tableName)
.insertOne(new Document(ImmutableMap.<String, Object>builder()
.put("id", 2)
.put("short_decimal", Decimal128.parse("-0.000"))
.put("long_decimal", Decimal128.parse("-0.000"))
.buildOrThrow()));

assertThat(query("SELECT * FROM test." + tableName))
.matches("VALUES (1, CAST('0' AS decimal(1)), CAST('0' AS decimal(38))), (2, CAST('0' AS decimal(1)), CAST('0' AS decimal(38)))");

assertThat(query("SELECT id FROM test." + tableName + " WHERE short_decimal = decimal '0'"))
.matches("VALUES 1, 2");

assertThat(query("SELECT id FROM test." + tableName + " WHERE long_decimal = decimal '0'"))
.matches("VALUES 1, 2");

assertUpdate("DROP TABLE test." + tableName);
}

@Test(dataProvider = "dbRefProvider")
public void testDBRef(Object objectId, String expectedValue, String expectedType)
{
Expand Down