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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use std::any::Any;
use std::sync::Arc;
use crate::array::array_decimal::Decimal256Array;
use crate::array::ArrayRef;
use crate::array::Decimal128Array;
use crate::array::{ArrayBuilder, FixedSizeBinaryBuilder};
use crate::error::{ArrowError, Result};
use crate::datatypes::{
validate_decimal256_precision_with_lt_bytes, validate_decimal_precision,
};
use crate::util::decimal::Decimal256;
#[derive(Debug)]
pub struct Decimal128Builder {
builder: FixedSizeBinaryBuilder,
precision: usize,
scale: usize,
value_validation: bool,
}
#[derive(Debug)]
pub struct Decimal256Builder {
builder: FixedSizeBinaryBuilder,
precision: usize,
scale: usize,
value_validation: bool,
}
impl Decimal128Builder {
const BYTE_LENGTH: i32 = 16;
pub fn new(capacity: usize, precision: usize, scale: usize) -> Self {
Self {
builder: FixedSizeBinaryBuilder::new(capacity, Self::BYTE_LENGTH),
precision,
scale,
value_validation: true,
}
}
pub unsafe fn disable_value_validation(&mut self) {
self.value_validation = false;
}
#[inline]
pub fn append_value(&mut self, value: impl Into<i128>) -> Result<()> {
let value = value.into();
if self.value_validation {
validate_decimal_precision(value, self.precision)?
}
let value_as_bytes: [u8; 16] = value.to_le_bytes();
self.builder.append_value(value_as_bytes.as_slice())
}
#[inline]
pub fn append_null(&mut self) {
self.builder.append_null()
}
#[inline]
pub fn append_option(&mut self, value: Option<impl Into<i128>>) -> Result<()> {
match value {
None => {
self.append_null();
Ok(())
}
Some(value) => self.append_value(value),
}
}
pub fn finish(&mut self) -> Decimal128Array {
Decimal128Array::from_fixed_size_binary_array(
self.builder.finish(),
self.precision,
self.scale,
)
}
}
impl ArrayBuilder for Decimal128Builder {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn into_box_any(self: Box<Self>) -> Box<dyn Any> {
self
}
fn len(&self) -> usize {
self.builder.len()
}
fn is_empty(&self) -> bool {
self.builder.is_empty()
}
fn finish(&mut self) -> ArrayRef {
Arc::new(self.finish())
}
}
impl Decimal256Builder {
const BYTE_LENGTH: i32 = 32;
pub fn new(capacity: usize, precision: usize, scale: usize) -> Self {
Self {
builder: FixedSizeBinaryBuilder::new(capacity, Self::BYTE_LENGTH),
precision,
scale,
value_validation: true,
}
}
pub unsafe fn disable_value_validation(&mut self) {
self.value_validation = false;
}
#[inline]
pub fn append_value(&mut self, value: &Decimal256) -> Result<()> {
let value = if self.value_validation {
let raw_bytes = value.raw_value();
validate_decimal256_precision_with_lt_bytes(raw_bytes, self.precision)?;
value
} else {
value
};
if self.precision != value.precision() || self.scale != value.scale() {
return Err(ArrowError::InvalidArgumentError(
"Decimal value does not have the same precision or scale as Decimal256Builder".to_string()
));
}
let value_as_bytes = value.raw_value();
if Self::BYTE_LENGTH != value_as_bytes.len() as i32 {
return Err(ArrowError::InvalidArgumentError(
"Byte slice does not have the same length as Decimal256Builder value lengths".to_string()
));
}
self.builder.append_value(value_as_bytes)
}
#[inline]
pub fn append_null(&mut self) {
self.builder.append_null()
}
#[inline]
pub fn append_option(&mut self, value: Option<&Decimal256>) -> Result<()> {
match value {
None => {
self.append_null();
Ok(())
}
Some(value) => self.append_value(value),
}
}
pub fn finish(&mut self) -> Decimal256Array {
Decimal256Array::from_fixed_size_binary_array(
self.builder.finish(),
self.precision,
self.scale,
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use num::{BigInt, Num};
use crate::array::array_decimal::Decimal128Array;
use crate::array::{array_decimal, Array};
use crate::datatypes::DataType;
use crate::util::decimal::{Decimal128, Decimal256};
#[test]
fn test_decimal_builder() {
let mut builder = Decimal128Builder::new(30, 38, 6);
builder.append_value(8_887_000_000_i128).unwrap();
builder.append_null();
builder.append_value(-8_887_000_000_i128).unwrap();
builder.append_option(None::<i128>).unwrap();
builder.append_option(Some(8_887_000_000_i128)).unwrap();
let decimal_array: Decimal128Array = builder.finish();
assert_eq!(&DataType::Decimal128(38, 6), decimal_array.data_type());
assert_eq!(5, decimal_array.len());
assert_eq!(2, decimal_array.null_count());
assert_eq!(32, decimal_array.value_offset(2));
assert_eq!(16, decimal_array.value_length());
}
#[test]
fn test_decimal_builder_with_decimal128() {
let mut builder = Decimal128Builder::new(30, 38, 6);
builder
.append_value(Decimal128::new_from_i128(30, 38, 8_887_000_000_i128))
.unwrap();
builder.append_null();
builder
.append_value(Decimal128::new_from_i128(30, 38, -8_887_000_000_i128))
.unwrap();
let decimal_array: Decimal128Array = builder.finish();
assert_eq!(&DataType::Decimal128(38, 6), decimal_array.data_type());
assert_eq!(3, decimal_array.len());
assert_eq!(1, decimal_array.null_count());
assert_eq!(32, decimal_array.value_offset(2));
assert_eq!(16, decimal_array.value_length());
}
#[test]
fn test_decimal256_builder() {
let mut builder = Decimal256Builder::new(30, 40, 6);
let mut bytes = [0_u8; 32];
bytes[0..16].clone_from_slice(&8_887_000_000_i128.to_le_bytes());
let value = Decimal256::try_new_from_bytes(40, 6, &bytes).unwrap();
builder.append_value(&value).unwrap();
builder.append_null();
bytes = [255; 32];
let value = Decimal256::try_new_from_bytes(40, 6, &bytes).unwrap();
builder.append_value(&value).unwrap();
bytes = [0; 32];
bytes[0..16].clone_from_slice(&0_i128.to_le_bytes());
bytes[15] = 128;
let value = Decimal256::try_new_from_bytes(40, 6, &bytes).unwrap();
builder.append_value(&value).unwrap();
builder.append_option(None::<&Decimal256>).unwrap();
builder.append_option(Some(&value)).unwrap();
let decimal_array: Decimal256Array = builder.finish();
assert_eq!(&DataType::Decimal256(40, 6), decimal_array.data_type());
assert_eq!(6, decimal_array.len());
assert_eq!(2, decimal_array.null_count());
assert_eq!(64, decimal_array.value_offset(2));
assert_eq!(32, decimal_array.value_length());
assert_eq!(decimal_array.value(0).to_string(), "8887.000000");
assert!(decimal_array.is_null(1));
assert_eq!(decimal_array.value(2).to_string(), "-0.000001");
assert_eq!(
decimal_array.value(3).to_string(),
"170141183460469231731687303715884.105728"
);
}
#[test]
#[should_panic(
expected = "Decimal value does not have the same precision or scale as Decimal256Builder"
)]
fn test_decimal256_builder_unmatched_precision_scale() {
let mut builder = Decimal256Builder::new(30, 10, 6);
let mut bytes = [0_u8; 32];
bytes[0..16].clone_from_slice(&8_887_000_000_i128.to_le_bytes());
let value = Decimal256::try_new_from_bytes(40, 6, &bytes).unwrap();
builder.append_value(&value).unwrap();
}
#[test]
#[should_panic(
expected = "9999999999999999999999999999999999999999999999999999999999999999999999999999 is too large to store in a Decimal256 of precision 75. Max is 999999999999999999999999999999999999999999999999999999999999999999999999999"
)]
fn test_decimal256_builder_out_of_range_precision_scale() {
let mut builder = Decimal256Builder::new(30, 75, 6);
let big_value = BigInt::from_str_radix("9999999999999999999999999999999999999999999999999999999999999999999999999999", 10).unwrap();
let value = Decimal256::from_big_int(&big_value, 75, 6).unwrap();
builder.append_value(&value).unwrap();
}
#[test]
#[should_panic(
expected = "9999999999999999999999999999999999999999999999999999999999999999999999999999 is too large to store in a Decimal256 of precision 75. Max is 999999999999999999999999999999999999999999999999999999999999999999999999999"
)]
fn test_decimal256_data_validation() {
let mut builder = Decimal256Builder::new(30, 75, 6);
unsafe {
builder.disable_value_validation();
}
let big_value = BigInt::from_str_radix("9999999999999999999999999999999999999999999999999999999999999999999999999999", 10).unwrap();
let value = Decimal256::from_big_int(&big_value, 75, 6).unwrap();
builder
.append_value(&value)
.expect("should not validate invalid value at builder");
let array = builder.finish();
let array_data = array_decimal::DecimalArray::data(&array);
array_data.validate_values().unwrap();
}
}