1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use crate::{
    convert::{unexpected_type, FromSql, ToSql},
    i256,
    types::Type,
    Value,
};
use anyhow::*;

/// Wrapper type for Clickhouse `FixedPoint32` type.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, Debug, Default)]
pub struct FixedPoint32<const PRECISION: u64>(pub i32);

impl<const PRECISION: u64> FixedPoint32<PRECISION> {
    pub const fn modulus(&self) -> i32 {
        10i32.pow(PRECISION as u32)
    }

    pub fn integer(&self) -> i32 {
        self.0 / 10i32.pow(PRECISION as u32)
    }

    pub fn fraction(&self) -> i32 {
        self.0 % 10i32.pow(PRECISION as u32)
    }
}

impl<const PRECISION: u64> ToSql for FixedPoint32<PRECISION> {
    fn to_sql(self) -> Result<Value> {
        Ok(Value::Decimal32(PRECISION as usize, self.0))
    }
}

impl<const PRECISION: u64> FromSql for FixedPoint32<PRECISION> {
    fn from_sql(type_: &Type, value: Value) -> Result<Self> {
        if !matches!(type_, Type::Decimal32(x) if *x == PRECISION as usize) {
            return Err(unexpected_type(type_));
        }
        match value {
            Value::Decimal32(_, x) => Ok(Self(x)),
            _ => unimplemented!(),
        }
    }
}

impl<const PRECISION: u64> Into<f64> for FixedPoint32<PRECISION> {
    fn into(self) -> f64 {
        self.integer() as f64 + (self.fraction() as f64 / self.modulus() as f64)
    }
}

/// Wrapper type for Clickhouse `FixedPoint64` type.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, Debug, Default)]
pub struct FixedPoint64<const PRECISION: u64>(pub i64);

impl<const PRECISION: u64> ToSql for FixedPoint64<PRECISION> {
    fn to_sql(self) -> Result<Value> {
        Ok(Value::Decimal64(PRECISION as usize, self.0))
    }
}

impl<const PRECISION: u64> FromSql for FixedPoint64<PRECISION> {
    fn from_sql(type_: &Type, value: Value) -> Result<Self> {
        if !matches!(type_, Type::Decimal64(x) if *x == PRECISION as usize) {
            return Err(unexpected_type(type_));
        }
        match value {
            Value::Decimal64(_, x) => Ok(Self(x)),
            _ => unimplemented!(),
        }
    }
}

impl<const PRECISION: u64> FixedPoint64<PRECISION> {
    pub const fn modulus(&self) -> i64 {
        10i64.pow(PRECISION as u32)
    }

    pub fn integer(&self) -> i64 {
        self.0 / 10i64.pow(PRECISION as u32)
    }

    pub fn fraction(&self) -> i64 {
        self.0 % 10i64.pow(PRECISION as u32)
    }
}

impl<const PRECISION: u64> Into<f64> for FixedPoint64<PRECISION> {
    fn into(self) -> f64 {
        self.integer() as f64 + (self.fraction() as f64 / self.modulus() as f64)
    }
}

/// Wrapper type for Clickhouse `FixedPoint128` type.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, Debug, Default)]
pub struct FixedPoint128<const PRECISION: u64>(pub i128);

impl<const PRECISION: u64> ToSql for FixedPoint128<PRECISION> {
    fn to_sql(self) -> Result<Value> {
        Ok(Value::Decimal128(PRECISION as usize, self.0))
    }
}

impl<const PRECISION: u64> FromSql for FixedPoint128<PRECISION> {
    fn from_sql(type_: &Type, value: Value) -> Result<Self> {
        if !matches!(type_, Type::Decimal128(x) if *x == PRECISION as usize) {
            return Err(unexpected_type(type_));
        }
        match value {
            Value::Decimal128(_, x) => Ok(Self(x)),
            _ => unimplemented!(),
        }
    }
}

impl<const PRECISION: u64> FixedPoint128<PRECISION> {
    pub const fn modulus(&self) -> i128 {
        10i128.pow(PRECISION as u32)
    }

    pub fn integer(&self) -> i128 {
        self.0 / 10i128.pow(PRECISION as u32)
    }

    pub fn fraction(&self) -> i128 {
        self.0 % 10i128.pow(PRECISION as u32)
    }
}

impl<const PRECISION: u64> Into<f64> for FixedPoint128<PRECISION> {
    fn into(self) -> f64 {
        self.integer() as f64 + (self.fraction() as f64 / self.modulus() as f64)
    }
}

/// Wrapper type for Clickhouse `FixedPoint256` type.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, Debug, Default)]
pub struct FixedPoint256<const PRECISION: u64>(pub i256);

impl<const PRECISION: u64> ToSql for FixedPoint256<PRECISION> {
    fn to_sql(self) -> Result<Value> {
        Ok(Value::Decimal256(PRECISION as usize, self.0))
    }
}

impl<const PRECISION: u64> FromSql for FixedPoint256<PRECISION> {
    fn from_sql(type_: &Type, value: Value) -> Result<Self> {
        if !matches!(type_, Type::Decimal256(x) if *x == PRECISION as usize) {
            return Err(unexpected_type(type_));
        }
        match value {
            Value::Decimal256(_, x) => Ok(Self(x)),
            _ => unimplemented!(),
        }
    }
}