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
201
202
203
204
205
206
207
208
209
210
211
212
use std::ops::Range;

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,
};

const MIN_I64_NUMERIC: i64 = -999_999_999_999_999_999;
const MAX_I64_NUMERIC: i64 = 1_000_000_000_000_000_000;
const MIN_I128_NUMERIC: i128 = -999_999_999_999_999_999;
const MAX_I128_NUMERIC: i128 = 1_000_000_000_000_000_000;

/// Numbers within this range must be serialized/deserialized as integers.
/// The ones above/under these thresholds are treated as strings.
const NUMERIC_I64_RANGE: Range<i64> = MIN_I64_NUMERIC..MAX_I64_NUMERIC;
const NUMERIC_I128_RANGE: Range<i128> = MIN_I128_NUMERIC..MAX_I128_NUMERIC;

impl Type<Exasol> for i8 {
    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 i8 {
    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.unsigned_abs().checked_ilog10().unwrap_or_default() + 1;
        Some(ExaDataType::Decimal(Decimal::new(precision, 0)).into())
    }

    fn size_hint(&self) -> usize {
        // sign + max num digits
        1 + Decimal::MAX_8BIT_PRECISION as usize
    }
}

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

impl Type<Exasol> for i16 {
    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 i16 {
    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.unsigned_abs().checked_ilog10().unwrap_or_default() + 1;
        Some(ExaDataType::Decimal(Decimal::new(precision, 0)).into())
    }

    fn size_hint(&self) -> usize {
        // sign + max num digits
        1 + Decimal::MAX_16BIT_PRECISION as usize
    }
}

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

impl Type<Exasol> for i32 {
    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 i32 {
    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.unsigned_abs().checked_ilog10().unwrap_or_default() + 1;
        Some(ExaDataType::Decimal(Decimal::new(precision, 0)).into())
    }

    fn size_hint(&self) -> usize {
        // sign + max num digits
        1 + Decimal::MAX_32BIT_PRECISION as usize
    }
}

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

impl Type<Exasol> for i64 {
    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 i64 {
    fn encode_by_ref(&self, buf: &mut ExaBuffer) -> Result<IsNull, BoxDynError> {
        if NUMERIC_I64_RANGE.contains(self) {
            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.unsigned_abs().checked_ilog10().unwrap_or_default() + 1;
        Some(ExaDataType::Decimal(Decimal::new(precision, 0)).into())
    }

    fn size_hint(&self) -> usize {
        // sign + max num digits
        1 + Decimal::MAX_64BIT_PRECISION as usize
    }
}

impl Decode<'_, Exasol> for i64 {
    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 i64 value: {v}").into()),
        }
    }
}

impl Type<Exasol> for i128 {
    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 i128 {
    fn encode_by_ref(&self, buf: &mut ExaBuffer) -> Result<IsNull, BoxDynError> {
        if NUMERIC_I128_RANGE.contains(self) {
            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.unsigned_abs().checked_ilog10().unwrap_or_default() + 1;
        Some(ExaDataType::Decimal(Decimal::new(precision, 0)).into())
    }

    fn size_hint(&self) -> usize {
        // sign + max num digits
        1 + Decimal::MAX_128BIT_PRECISION as usize
    }
}

impl Decode<'_, Exasol> for i128 {
    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 i128 value: {v}").into()),
        }
    }
}