Skip to content

Commit

Permalink
chore: fix decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
v3g42 committed Mar 7, 2024
1 parent 0100965 commit e78bd2b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 3 additions & 1 deletion dozer-sink-clickhouse/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::client::ClickhouseClient;
use crate::errors::ClickhouseSinkError::{self, SinkTableDoesNotExist};
use crate::types::DECIMAL_SCALE;
use clickhouse_rs::types::Complex;
use clickhouse_rs::{Block, ClientHandle};
use dozer_types::errors::internal::BoxedError;
Expand Down Expand Up @@ -175,6 +176,7 @@ impl ClickhouseSchema {
}

pub fn map_field_to_type(field: &FieldDefinition) -> String {
let decimal = format!("Decimal(10, {})", DECIMAL_SCALE);
let typ: &str = match field.typ {
FieldType::UInt => "UInt64",
FieldType::U128 => "UInt128",
Expand All @@ -185,7 +187,7 @@ pub fn map_field_to_type(field: &FieldDefinition) -> String {
FieldType::String => "String",
FieldType::Text => "String",
FieldType::Binary => "Array(UInt8)",
FieldType::Decimal => "Decimal(10, 0)",
FieldType::Decimal => &decimal,
FieldType::Timestamp => "DateTime64(3)",
FieldType::Date => "Date",
FieldType::Json => "JSON",
Expand Down
8 changes: 6 additions & 2 deletions dozer-sink-clickhouse/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use either::Either;

use clickhouse_rs::types::{FromSql, Value, ValueRef};

pub const DECIMAL_SCALE: u8 = 4;
pub struct ValueWrapper(pub Value);

impl<'a> FromSql<'a> for ValueWrapper {
Expand Down Expand Up @@ -263,10 +264,13 @@ pub async fn insert_multi(
nullable,
b,
Decimal,
f64,
clickhouse_rs::types::Decimal,
column_values,
n,
|f: &rust_decimal::Decimal| -> Option<f64> { f.to_f64() }
|f: &rust_decimal::Decimal| -> Option<clickhouse_rs::types::Decimal> {
f.to_f64()
.map(|f| clickhouse_rs::types::Decimal::of(f, DECIMAL_SCALE))
}
)
}
FieldType::Timestamp => {
Expand Down

0 comments on commit e78bd2b

Please sign in to comment.