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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use serde::Deserialize;
use serde_json::Value;
use sqlx_core::{
    decode::Decode,
    encode::{Encode, IsNull},
    error::BoxDynError,
    types::Type,
};

use crate::{
    arguments::ExaBuffer,
    database::Exasol,
    type_info::{Decimal, ExaDataType, ExaTypeInfo},
    value::ExaValueRef,
};

/// Numbers below this threshold must be serialized/deserialized as integers.
/// The ones above must be treated as strings.
const MAX_U64_NUMERIC: u64 = 1_000_000_000_000_000_000;
const MAX_U128_NUMERIC: u128 = 1_000_000_000_000_000_000;

impl Type<Exasol> for u8 {
    fn type_info() -> ExaTypeInfo {
        ExaDataType::Decimal(Decimal::new(Decimal::MAX_8BIT_PRECISION, 0)).into()
    }

    fn compatible(ty: &ExaTypeInfo) -> bool {
        <Self as Type<Exasol>>::type_info().compatible(ty)
    }
}

impl Encode<'_, Exasol> for u8 {
    fn encode_by_ref(&self, buf: &mut ExaBuffer) -> Result<IsNull, BoxDynError> {
        buf.append(self)?;
        Ok(IsNull::No)
    }

    fn produces(&self) -> Option<ExaTypeInfo> {
        let precision = self.checked_ilog10().unwrap_or_default() + 1;
        Some(ExaDataType::Decimal(Decimal::new(precision, 0)).into())
    }

    fn size_hint(&self) -> usize {
        Decimal::MAX_8BIT_PRECISION as usize
    }
}

impl Decode<'_, Exasol> for u8 {
    fn decode(value: ExaValueRef<'_>) -> Result<Self, BoxDynError> {
        <Self as Deserialize>::deserialize(value.value).map_err(From::from)
    }
}

impl Type<Exasol> for u16 {
    fn type_info() -> ExaTypeInfo {
        ExaDataType::Decimal(Decimal::new(Decimal::MAX_16BIT_PRECISION, 0)).into()
    }

    fn compatible(ty: &ExaTypeInfo) -> bool {
        <Self as Type<Exasol>>::type_info().compatible(ty)
    }
}

impl Encode<'_, Exasol> for u16 {
    fn encode_by_ref(&self, buf: &mut ExaBuffer) -> Result<IsNull, BoxDynError> {
        buf.append(self)?;
        Ok(IsNull::No)
    }

    fn produces(&self) -> Option<ExaTypeInfo> {
        let precision = self.checked_ilog10().unwrap_or_default() + 1;
        Some(ExaDataType::Decimal(Decimal::new(precision, 0)).into())
    }

    fn size_hint(&self) -> usize {
        Decimal::MAX_16BIT_PRECISION as usize
    }
}

impl Decode<'_, Exasol> for u16 {
    fn decode(value: ExaValueRef<'_>) -> Result<Self, BoxDynError> {
        <Self as Deserialize>::deserialize(value.value).map_err(From::from)
    }
}

impl Type<Exasol> for u32 {
    fn type_info() -> ExaTypeInfo {
        ExaDataType::Decimal(Decimal::new(Decimal::MAX_32BIT_PRECISION, 0)).into()
    }

    fn compatible(ty: &ExaTypeInfo) -> bool {
        <Self as Type<Exasol>>::type_info().compatible(ty)
    }
}

impl Encode<'_, Exasol> for u32 {
    fn encode_by_ref(&self, buf: &mut ExaBuffer) -> Result<IsNull, BoxDynError> {
        buf.append(self)?;
        Ok(IsNull::No)
    }

    fn produces(&self) -> Option<ExaTypeInfo> {
        let precision = self.checked_ilog10().unwrap_or_default() + 1;
        Some(ExaDataType::Decimal(Decimal::new(precision, 0)).into())
    }

    fn size_hint(&self) -> usize {
        Decimal::MAX_32BIT_PRECISION as usize
    }
}

impl Decode<'_, Exasol> for u32 {
    fn decode(value: ExaValueRef<'_>) -> Result<Self, BoxDynError> {
        <Self as Deserialize>::deserialize(value.value).map_err(From::from)
    }
}

impl Type<Exasol> for u64 {
    fn type_info() -> ExaTypeInfo {
        ExaDataType::Decimal(Decimal::new(Decimal::MAX_64BIT_PRECISION, 0)).into()
    }

    fn compatible(ty: &ExaTypeInfo) -> bool {
        <Self as Type<Exasol>>::type_info().compatible(ty)
    }
}

impl Encode<'_, Exasol> for u64 {
    fn encode_by_ref(&self, buf: &mut ExaBuffer) -> Result<IsNull, BoxDynError> {
        if self < &MAX_U64_NUMERIC {
            buf.append(self)?;
        } else {
            // Large numbers get serialized as strings
            buf.append(format_args!("{self}"))?;
        };

        Ok(IsNull::No)
    }

    fn produces(&self) -> Option<ExaTypeInfo> {
        let precision = self.checked_ilog10().unwrap_or_default() + 1;
        Some(ExaDataType::Decimal(Decimal::new(precision, 0)).into())
    }

    fn size_hint(&self) -> usize {
        Decimal::MAX_64BIT_PRECISION as usize
    }
}

impl Decode<'_, Exasol> for u64 {
    fn decode(value: ExaValueRef<'_>) -> Result<Self, BoxDynError> {
        match value.value {
            Value::Number(n) => <Self as Deserialize>::deserialize(n).map_err(From::from),
            Value::String(s) => serde_json::from_str(s).map_err(From::from),
            v => Err(format!("invalid u64 value: {v}").into()),
        }
    }
}

impl Type<Exasol> for u128 {
    fn type_info() -> ExaTypeInfo {
        ExaDataType::Decimal(Decimal::new(Decimal::MAX_128BIT_PRECISION, 0)).into()
    }

    fn compatible(ty: &ExaTypeInfo) -> bool {
        <Self as Type<Exasol>>::type_info().compatible(ty)
    }
}

impl Encode<'_, Exasol> for u128 {
    fn encode_by_ref(&self, buf: &mut ExaBuffer) -> Result<IsNull, BoxDynError> {
        if self < &MAX_U128_NUMERIC {
            buf.append(self)?;
        } else {
            // Large numbers get serialized as strings
            buf.append(format_args!("{self}"))?;
        };

        Ok(IsNull::No)
    }

    fn produces(&self) -> Option<ExaTypeInfo> {
        let precision = self.checked_ilog10().unwrap_or_default() + 1;
        Some(ExaDataType::Decimal(Decimal::new(precision, 0)).into())
    }

    fn size_hint(&self) -> usize {
        Decimal::MAX_128BIT_PRECISION as usize
    }
}

impl Decode<'_, Exasol> for u128 {
    fn decode(value: ExaValueRef<'_>) -> Result<Self, BoxDynError> {
        match value.value {
            Value::Number(n) => <Self as Deserialize>::deserialize(n).map_err(From::from),
            Value::String(s) => serde_json::from_str(s).map_err(From::from),
            v => Err(format!("invalid u128 value: {v}").into()),
        }
    }
}