Skip to content

Commit

Permalink
fix: fix decimal bug (#557)
Browse files Browse the repository at this point in the history
* fix decimal bug

* fix decimal bug
  • Loading branch information
sundy-li authored Jan 3, 2025
1 parent 1fa13e9 commit 5947ca0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ttc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
- 'core/**'
- 'driver/**'
- 'ttc/**'
- 'sql/**'

jobs:
docker:
Expand Down
1 change: 1 addition & 0 deletions cli/tests/00-base.result
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ with comment
"
a"
3
-9999999999999999999999999999999999999999999999999999999999999999999999999999
1
NULL
3.00 3.00 0.0000000170141183460469231731687303715884105727000 -0.0000000170141183460469231731687303715884105727000
Expand Down
2 changes: 2 additions & 0 deletions cli/tests/00-base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ select /* ignore this block */ 'with comment';
select 1; select 2; select '
a'; select 3;

select -9999999999999999999999999999999999999999999999999999999999999999999999999999;

-- issue 409
drop table if exists t;
create table t(id tuple(STRING, array(tuple(array(int), array(STRING NOT NULL)))));
Expand Down
28 changes: 22 additions & 6 deletions sql/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,14 @@ pub fn display_decimal_256(num: i256, scale: u8) -> String {
pub fn parse_decimal(text: &str, size: DecimalSize) -> Result<NumberValue> {
let mut start = 0;
let bytes = text.as_bytes();
let mut is_negative = false;

// Check if the number is negative
if bytes[start] == b'-' {
is_negative = true;
start += 1;
}

while start < text.len() && bytes[start] == b'0' {
start += 1
}
Expand Down Expand Up @@ -1016,13 +1024,21 @@ pub fn parse_decimal(text: &str, size: DecimalSize) -> Result<NumberValue> {
let precision = std::cmp::min(digits.len(), 76);
let digits = unsafe { std::str::from_utf8_unchecked(&digits[..precision]) };

if size.precision > 38 {
Ok(NumberValue::Decimal256(
i256::from_string(digits).unwrap(),
size,
))
let result = if size.precision > 38 {
NumberValue::Decimal256(i256::from_string(digits).unwrap(), size)
} else {
NumberValue::Decimal128(digits.parse::<i128>()?, size)
};

// If the number was negative, negate the result
if is_negative {
match result {
NumberValue::Decimal256(val, size) => Ok(NumberValue::Decimal256(-val, size)),
NumberValue::Decimal128(val, size) => Ok(NumberValue::Decimal128(-val, size)),
_ => Ok(result),
}
} else {
Ok(NumberValue::Decimal128(digits.parse::<i128>()?, size))
Ok(result)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ttc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repository = { workspace = true }
[dependencies]
bytes = "1"
clap = { version = "4.4", features = ["derive", "env"] }
databend-driver = { path = "../driver" }
databend-driver = { workspace = true }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = ["std"] }
tokio = { version = "1.34", features = [
Expand Down

0 comments on commit 5947ca0

Please sign in to comment.