Skip to content

Commit

Permalink
fix(cubestore): Decimals without integral part are ignoring sign duri…
Browse files Browse the repository at this point in the history
…ng to_string()
  • Loading branch information
paveltiunov committed Feb 2, 2022
1 parent ab68378 commit b02b1a6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
28 changes: 28 additions & 0 deletions rust/cubestore/cubestore-sql-tests/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub fn sql_tests() -> Vec<(&'static str, TestFn)> {
t("insert", insert),
t("select_test", select_test),
t("negative_numbers", negative_numbers),
t("negative_decimal", negative_decimal),
t("custom_types", custom_types),
t("group_by_boolean", group_by_boolean),
t("group_by_decimal", group_by_decimal),
Expand Down Expand Up @@ -256,6 +257,33 @@ async fn negative_numbers(service: Box<dyn SqlClient>) {
assert_eq!(result.get_rows()[0], Row::new(vec![TableValue::Int(-153)]));
}

async fn negative_decimal(service: Box<dyn SqlClient>) {
let _ = service.exec_query("CREATE SCHEMA foo").await.unwrap();

let _ = service
.exec_query("CREATE TABLE foo.values (decimal_value decimal)")
.await
.unwrap();

service
.exec_query("INSERT INTO foo.values (decimal_value) VALUES (-0.12345)")
.await
.unwrap();

let result = service
.exec_query("SELECT * from foo.values")
.await
.unwrap();

assert_eq!(
match &result.get_rows()[0].values()[0] {
TableValue::Decimal(d) => d.to_string(5),
x => panic!("Expected decimal but found: {:?}", x),
},
"-0.12345"
);
}

async fn custom_types(service: Box<dyn SqlClient>) {
service.exec_query("CREATE SCHEMA foo").await.unwrap();

Expand Down
15 changes: 15 additions & 0 deletions rust/cubestore/cubestore/src/import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,3 +670,18 @@ impl Ingestion {
Ok(())
}
}

#[cfg(test)]
mod tests {
extern crate test;

use crate::import::parse_decimal;

#[test]
fn parse_decimal_test() {
assert_eq!(
parse_decimal("-0.12345", 5).unwrap().to_string(5),
"-0.12345",
);
}
}
9 changes: 7 additions & 2 deletions rust/cubestore/cubestore/src/util/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ impl Decimal {
pub fn to_string(&self, scale: u8) -> String {
let n = 10_i64.pow(scale as u32);
let v = self.raw_value();
let integral = v / n;
let integral = (v / n).abs();
let fractional = (v % n).abs();
format!("{}.{}", integral, fractional)
format!(
"{}{}.{}",
if v.signum() < 0 { "-" } else { "" },
integral,
fractional
)
}
}

Expand Down

0 comments on commit b02b1a6

Please sign in to comment.