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 inferring type of decimals with leading zeros in MongoDB #19068

Merged
merged 1 commit into from
Sep 20, 2023
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 @@ -922,7 +922,10 @@ else if (value instanceof Decimal128 decimal128) {
catch (ArithmeticException e) {
return Optional.empty();
}
typeSignature = createDecimalType(decimal.precision(), decimal.scale()).getTypeSignature();
// Java's BigDecimal.precision() returns precision for the unscaled value, so it skips leading zeros for values lower than 1.
// Trino's (SQL) decimal precision must include leading zeros in values less than 1, and can never be lower than scale.
int precision = Math.max(decimal.precision(), decimal.scale());
hashhar marked this conversation as resolved.
Show resolved Hide resolved
typeSignature = createDecimalType(precision, decimal.scale()).getTypeSignature();
}
else if (value instanceof Date) {
typeSignature = TIMESTAMP_MILLIS.getTypeSignature();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ public void testDecimal()
.addRoundTrip("NumberDecimal(\"2\")", "CAST('2' AS decimal(1, 0))")
.addRoundTrip("NumberDecimal(\"2.3\")", "CAST('2.3' AS decimal(2, 1))")
.addRoundTrip("NumberDecimal(\"-2.3\")", "CAST('-2.3' AS decimal(2, 1))")
nineinchnick marked this conversation as resolved.
Show resolved Hide resolved
.addRoundTrip("NumberDecimal(\"0.03\")", "CAST('0.03' AS decimal(2, 2))")
nineinchnick marked this conversation as resolved.
Show resolved Hide resolved
.addRoundTrip("NumberDecimal(\"-0.03\")", "CAST('-0.03' AS decimal(2, 2))")
.addRoundTrip("NumberDecimal(\"1234567890123456789012345678901234\")", "CAST('1234567890123456789012345678901234' AS decimal(34, 0))") // 34 is the max precision in Decimal128
.addRoundTrip("NumberDecimal(\"1234567890123456.789012345678901234\")", "CAST('1234567890123456.789012345678901234' AS decimal(34, 18))")
.execute(getQueryRunner(), mongoCreateAndInsert(getSession(), "tpch", "test_decimal"));
Expand Down