Skip to content

Commit 0eb0ded

Browse files
committed
Update to bigdecimal 0.4.7
Utilize new `to_plain_string` function
1 parent bbafc70 commit 0eb0ded

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ arrow-ipc = { version = "53.3.0", default-features = false, features = [
9393
arrow-ord = { version = "53.3.0", default-features = false }
9494
arrow-schema = { version = "53.3.0", default-features = false }
9595
async-trait = "0.1.73"
96-
bigdecimal = "0.4.6"
96+
bigdecimal = "0.4.7"
9797
bytes = "1.4"
9898
chrono = { version = "0.4.38", default-features = false }
9999
ctor = "0.2.0"

datafusion/sqllogictest/src/engines/conversion.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -106,30 +106,7 @@ pub(crate) fn big_decimal_to_str(value: BigDecimal) -> String {
106106
// Round the value to limit the number of decimal places
107107
let value = value.round(12).normalized();
108108
// Format the value to a string
109-
format_big_decimal(value)
110-
}
111-
112-
fn format_big_decimal(value: BigDecimal) -> String {
113-
let (integer, scale) = value.into_bigint_and_exponent();
114-
let mut str = integer.to_str_radix(10);
115-
if scale <= 0 {
116-
// Append zeros to the right of the integer part
117-
str.extend(std::iter::repeat('0').take(scale.unsigned_abs() as usize));
118-
str
119-
} else {
120-
let (sign, unsigned_len, unsigned_str) = if integer.is_negative() {
121-
("-", str.len() - 1, &str[1..])
122-
} else {
123-
("", str.len(), &str[..])
124-
};
125-
let scale = scale as usize;
126-
if unsigned_len <= scale {
127-
format!("{}0.{:0>scale$}", sign, unsigned_str)
128-
} else {
129-
str.insert(str.len() - scale, '.');
130-
str
131-
}
132-
}
109+
value.to_plain_string()
133110
}
134111

135112
#[cfg(test)]
@@ -158,6 +135,16 @@ mod tests {
158135
assert_decimal_str_eq!(11, 0, "11");
159136
assert_decimal_str_eq!(11, -1, "110");
160137
assert_decimal_str_eq!(0, 0, "0");
138+
assert_decimal_str_eq!(
139+
12345678901234567890123456789012345678_i128,
140+
0,
141+
"12345678901234567890123456789012345678"
142+
);
143+
assert_decimal_str_eq!(
144+
12345678901234567890123456789012345678_i128,
145+
38,
146+
"0.123456789012"
147+
);
161148

162149
// Negative cases
163150
assert_decimal_str_eq!(-110, 3, "-0.11");
@@ -166,6 +153,16 @@ mod tests {
166153
assert_decimal_str_eq!(-11, 1, "-1.1");
167154
assert_decimal_str_eq!(-11, 0, "-11");
168155
assert_decimal_str_eq!(-11, -1, "-110");
156+
assert_decimal_str_eq!(
157+
-12345678901234567890123456789012345678_i128,
158+
0,
159+
"-12345678901234567890123456789012345678"
160+
);
161+
assert_decimal_str_eq!(
162+
-12345678901234567890123456789012345678_i128,
163+
38,
164+
"-0.123456789012"
165+
);
169166

170167
// Round to 12 decimal places
171168
// 1.0000000000011 -> 1.000000000001

0 commit comments

Comments
 (0)