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

chore(deps): update bigdecimal from 0.4.1 to 0.4.6 #13569

Merged
merged 1 commit into from
Nov 27, 2024
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ arrow-ipc = { version = "53.3.0", default-features = false, features = [
arrow-ord = { version = "53.3.0", default-features = false }
arrow-schema = { version = "53.3.0", default-features = false }
async-trait = "0.1.73"
bigdecimal = "=0.4.1"
bigdecimal = "0.4.6"
bytes = "1.4"
chrono = { version = "0.4.38", default-features = false }
ctor = "0.2.0"
Expand Down
67 changes: 66 additions & 1 deletion datafusion/sqllogictest/src/engines/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,70 @@ pub(crate) fn decimal_to_str(value: Decimal) -> String {
}

pub(crate) fn big_decimal_to_str(value: BigDecimal) -> String {
value.round(12).normalized().to_string()
// Round the value to limit the number of decimal places
let value = value.round(12).normalized();
// Format the value to a string
format_big_decimal(value)
Copy link
Member

Choose a reason for hiding this comment

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

we could use bigdecimal to_plain_string (see akubera/bigdecimal-rs#135) -- but it's not released yet afaict

}

fn format_big_decimal(value: BigDecimal) -> String {
let (integer, scale) = value.into_bigint_and_exponent();
let mut str = integer.to_str_radix(10);
if scale <= 0 {
// Append zeros to the right of the integer part
str.extend(std::iter::repeat('0').take(scale.unsigned_abs() as usize));
str
} else {
let (sign, unsigned_len, unsigned_str) = if integer.is_negative() {
("-", str.len() - 1, &str[1..])
} else {
("", str.len(), &str[..])
};
let scale = scale as usize;
if unsigned_len <= scale {
format!("{}0.{:0>scale$}", sign, unsigned_str)
} else {
str.insert(str.len() - scale, '.');
str
}
}
}

#[cfg(test)]
mod tests {
use super::big_decimal_to_str;
use bigdecimal::{num_bigint::BigInt, BigDecimal};

macro_rules! assert_decimal_str_eq {
($integer:expr, $scale:expr, $expected:expr) => {
assert_eq!(
big_decimal_to_str(BigDecimal::from_bigint(
BigInt::from($integer),
$scale
)),
$expected
);
};
}

#[test]
fn test_big_decimal_to_str() {
assert_decimal_str_eq!(11, 3, "0.011");
assert_decimal_str_eq!(11, 2, "0.11");
assert_decimal_str_eq!(11, 1, "1.1");
assert_decimal_str_eq!(11, 0, "11");
assert_decimal_str_eq!(11, -1, "110");
assert_decimal_str_eq!(0, 0, "0");

// Negative cases
assert_decimal_str_eq!(-11, 3, "-0.011");
assert_decimal_str_eq!(-11, 2, "-0.11");
assert_decimal_str_eq!(-11, 1, "-1.1");
assert_decimal_str_eq!(-11, 0, "-11");
assert_decimal_str_eq!(-11, -1, "-110");

// Round to 12 decimal places
// 1.0000000000011 -> 1.000000000001
assert_decimal_str_eq!(10_i128.pow(13) + 11, 13, "1.000000000001");
}
}