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: decimal256 display missing zeros #290

Merged
merged 3 commits into from
Nov 3, 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
7 changes: 2 additions & 5 deletions bindings/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ impl Row {
pub fn values<'p>(&'p self, py: Python<'p>) -> PyResult<PyObject> {
let res = PyTuple::new(
py,
self.0
.values()
.into_iter()
.map(|v| Value(v.clone()).into_py(py)), // FIXME: do not clone
self.0.values().iter().map(|v| Value(v.clone()).into_py(py)), // FIXME: do not clone
);
Ok(res.into_py(py))
}
Expand Down Expand Up @@ -232,7 +229,7 @@ impl RowIterator {
fn __aiter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
slf
}
fn __anext__<'a>(&self, py: Python<'a>) -> PyResult<Option<PyObject>> {
fn __anext__(&self, py: Python<'_>) -> PyResult<Option<PyObject>> {
let streamer = self.0.clone();
let future = future_into_py(py, async move {
match streamer.lock().await.next().await {
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/00-base.result
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ a 1 true [1,2]
3
[] {}
with comment
3.00 3.00
3.00 3.00 0.0000000170141183460469231731687303715884105727000 -0.0000000170141183460469231731687303715884105727000
Asia/Shanghai
0 0.00
1 1.00
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/00-base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ select /* ignore this block */ 'with comment';
select 'in comment block';
*/

select 1.00 + 2.00, 3.00;
select 1.00 + 2.00, 3.00, 0.0000000170141183460469231731687303715884105727000, -0.0000000170141183460469231731687303715884105727000;

select/*+ SET_VAR(timezone='Asia/Shanghai') */ timezone();

Expand Down
55 changes: 38 additions & 17 deletions sql/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,25 +700,46 @@ pub fn display_decimal_256(num: i256, scale: u8) -> String {
write!(buf, "{}", num).unwrap();
} else {
let pow_scale = i256::from_i128(10i128).pow_wrapping(scale as u32);
let width = scale as usize;
// -1/10 = 0
if num >= i256::ZERO {
write!(
buf,
"{}.{:0>width$}",
num / pow_scale,
(num % pow_scale).wrapping_abs(),
width = scale as usize
)
.unwrap();
let (int_part, neg) = if num >= i256::ZERO {
(num / pow_scale, "")
} else {
write!(
buf,
"-{}.{:0>width$}",
-num / pow_scale,
(num % pow_scale).wrapping_abs(),
width = scale as usize
)
.unwrap();
(-num / pow_scale, "-")
};
let frac_part = (num % pow_scale).wrapping_abs();

match frac_part.to_i128() {
Some(frac_part) => {
write!(
buf,
"{}{}.{:0>width$}",
neg,
int_part,
frac_part,
width = width
)
.unwrap();
}
None => {
// fractional part is too big for display,
// split it into two parts.
let pow = i256::from_i128(10i128).pow_wrapping(38);
let frac_high_part = frac_part / pow;
let frac_low_part = frac_part % pow;
let frac_width = (scale - 38) as usize;

write!(
buf,
"{}{}.{:0>width$}{}",
neg,
int_part,
frac_high_part.to_i128().unwrap(),
frac_low_part.to_i128().unwrap(),
width = frac_width
)
.unwrap();
}
}
}
buf
Expand Down
Loading