arrow_data/
decimal.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Defines maximum and minimum values for `decimal256` and `decimal128` types for varying precisions.
19//!
20//! Also provides functions to validate if a given decimal value is within the valid range of the decimal type.
21
22use arrow_buffer::i256;
23use arrow_schema::ArrowError;
24
25pub use arrow_schema::{
26    DECIMAL128_MAX_PRECISION, DECIMAL128_MAX_SCALE, DECIMAL256_MAX_PRECISION, DECIMAL256_MAX_SCALE,
27    DECIMAL_DEFAULT_SCALE,
28};
29
30/// MAX decimal256 value of little-endian format for each precision.
31/// Each element is the max value of signed 256-bit integer for the specified precision which
32/// is encoded to the 32-byte width format of little-endian.
33/// The first element is unused and is inserted so that we can look up using
34/// precision as the index without the need to subtract 1 first.
35pub(crate) const MAX_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION: [i256; 77] = [
36    i256::from_i128(0_i128), // unused first element
37    i256::from_le_bytes([
38        9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39        0, 0,
40    ]),
41    i256::from_le_bytes([
42        99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43        0, 0,
44    ]),
45    i256::from_le_bytes([
46        231, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47        0, 0,
48    ]),
49    i256::from_le_bytes([
50        15, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51        0, 0,
52    ]),
53    i256::from_le_bytes([
54        159, 134, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55        0, 0, 0,
56    ]),
57    i256::from_le_bytes([
58        63, 66, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59        0, 0, 0,
60    ]),
61    i256::from_le_bytes([
62        127, 150, 152, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63        0, 0, 0, 0,
64    ]),
65    i256::from_le_bytes([
66        255, 224, 245, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67        0, 0, 0, 0,
68    ]),
69    i256::from_le_bytes([
70        255, 201, 154, 59, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71        0, 0, 0, 0,
72    ]),
73    i256::from_le_bytes([
74        255, 227, 11, 84, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75        0, 0, 0, 0,
76    ]),
77    i256::from_le_bytes([
78        255, 231, 118, 72, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79        0, 0, 0, 0,
80    ]),
81    i256::from_le_bytes([
82        255, 15, 165, 212, 232, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83        0, 0, 0, 0, 0,
84    ]),
85    i256::from_le_bytes([
86        255, 159, 114, 78, 24, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87        0, 0, 0, 0,
88    ]),
89    i256::from_le_bytes([
90        255, 63, 122, 16, 243, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91        0, 0, 0, 0, 0,
92    ]),
93    i256::from_le_bytes([
94        255, 127, 198, 164, 126, 141, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95        0, 0, 0, 0, 0, 0,
96    ]),
97    i256::from_le_bytes([
98        255, 255, 192, 111, 242, 134, 35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99        0, 0, 0, 0, 0, 0,
100    ]),
101    i256::from_le_bytes([
102        255, 255, 137, 93, 120, 69, 99, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103        0, 0, 0, 0, 0,
104    ]),
105    i256::from_le_bytes([
106        255, 255, 99, 167, 179, 182, 224, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107        0, 0, 0, 0, 0, 0,
108    ]),
109    i256::from_le_bytes([
110        255, 255, 231, 137, 4, 35, 199, 138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111        0, 0, 0, 0, 0, 0,
112    ]),
113    i256::from_le_bytes([
114        255, 255, 15, 99, 45, 94, 199, 107, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
115        0, 0, 0, 0, 0, 0,
116    ]),
117    i256::from_le_bytes([
118        255, 255, 159, 222, 197, 173, 201, 53, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
119        0, 0, 0, 0, 0, 0, 0,
120    ]),
121    i256::from_le_bytes([
122        255, 255, 63, 178, 186, 201, 224, 25, 30, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123        0, 0, 0, 0, 0, 0, 0,
124    ]),
125    i256::from_le_bytes([
126        255, 255, 127, 246, 74, 225, 199, 2, 45, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
127        0, 0, 0, 0, 0, 0, 0,
128    ]),
129    i256::from_le_bytes([
130        255, 255, 255, 160, 237, 204, 206, 27, 194, 211, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131        0, 0, 0, 0, 0, 0, 0, 0,
132    ]),
133    i256::from_le_bytes([
134        255, 255, 255, 73, 72, 1, 20, 22, 149, 69, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
135        0, 0, 0, 0, 0, 0,
136    ]),
137    i256::from_le_bytes([
138        255, 255, 255, 227, 210, 12, 200, 220, 210, 183, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
139        0, 0, 0, 0, 0, 0, 0, 0,
140    ]),
141    i256::from_le_bytes([
142        255, 255, 255, 231, 60, 128, 208, 159, 60, 46, 59, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
143        0, 0, 0, 0, 0, 0, 0, 0,
144    ]),
145    i256::from_le_bytes([
146        255, 255, 255, 15, 97, 2, 37, 62, 94, 206, 79, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
147        0, 0, 0, 0, 0, 0, 0,
148    ]),
149    i256::from_le_bytes([
150        255, 255, 255, 159, 202, 23, 114, 109, 174, 15, 30, 67, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
151        0, 0, 0, 0, 0, 0, 0, 0,
152    ]),
153    i256::from_le_bytes([
154        255, 255, 255, 63, 234, 237, 116, 70, 208, 156, 44, 159, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
155        0, 0, 0, 0, 0, 0, 0, 0, 0,
156    ]),
157    i256::from_le_bytes([
158        255, 255, 255, 127, 38, 75, 145, 192, 34, 32, 190, 55, 126, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159        0, 0, 0, 0, 0, 0, 0, 0, 0,
160    ]),
161    i256::from_le_bytes([
162        255, 255, 255, 255, 128, 239, 172, 133, 91, 65, 109, 45, 238, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163        0, 0, 0, 0, 0, 0, 0, 0, 0,
164    ]),
165    i256::from_le_bytes([
166        255, 255, 255, 255, 9, 91, 193, 56, 147, 141, 68, 198, 77, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0,
167        0, 0, 0, 0, 0, 0, 0, 0, 0,
168    ]),
169    i256::from_le_bytes([
170        255, 255, 255, 255, 99, 142, 141, 55, 192, 135, 173, 190, 9, 237, 1, 0, 0, 0, 0, 0, 0, 0,
171        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172    ]),
173    i256::from_le_bytes([
174        255, 255, 255, 255, 231, 143, 135, 43, 130, 77, 199, 114, 97, 66, 19, 0, 0, 0, 0, 0, 0, 0,
175        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176    ]),
177    i256::from_le_bytes([
178        255, 255, 255, 255, 15, 159, 75, 179, 21, 7, 201, 123, 206, 151, 192, 0, 0, 0, 0, 0, 0, 0,
179        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
180    ]),
181    i256::from_le_bytes([
182        255, 255, 255, 255, 159, 54, 244, 0, 217, 70, 218, 213, 16, 238, 133, 7, 0, 0, 0, 0, 0, 0,
183        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
184    ]),
185    i256::from_le_bytes([
186        255, 255, 255, 255, 63, 34, 138, 9, 122, 196, 134, 90, 168, 76, 59, 75, 0, 0, 0, 0, 0, 0,
187        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
188    ]),
189    i256::from_le_bytes([
190        255, 255, 255, 255, 127, 86, 101, 95, 196, 172, 67, 137, 147, 254, 80, 240, 2, 0, 0, 0, 0,
191        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192    ]),
193    i256::from_le_bytes([
194        255, 255, 255, 255, 255, 96, 245, 185, 171, 191, 164, 92, 195, 241, 41, 99, 29, 0, 0, 0, 0,
195        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
196    ]),
197    i256::from_le_bytes([
198        255, 255, 255, 255, 255, 201, 149, 67, 181, 124, 111, 158, 161, 113, 163, 223, 37, 1, 0, 0,
199        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
200    ]),
201    i256::from_le_bytes([
202        255, 255, 255, 255, 255, 227, 217, 163, 20, 223, 90, 48, 80, 112, 98, 188, 122, 11, 0, 0,
203        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
204    ]),
205    i256::from_le_bytes([
206        255, 255, 255, 255, 255, 231, 130, 102, 206, 182, 140, 227, 33, 99, 216, 91, 203, 114, 0,
207        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
208    ]),
209    i256::from_le_bytes([
210        255, 255, 255, 255, 255, 15, 29, 1, 16, 36, 127, 227, 82, 223, 115, 150, 241, 123, 4, 0, 0,
211        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
212    ]),
213    i256::from_le_bytes([
214        255, 255, 255, 255, 255, 159, 34, 11, 160, 104, 247, 226, 60, 185, 134, 224, 111, 215, 44,
215        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
216    ]),
217    i256::from_le_bytes([
218        255, 255, 255, 255, 255, 63, 90, 111, 64, 22, 170, 221, 96, 60, 67, 197, 94, 106, 192, 1,
219        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
220    ]),
221    i256::from_le_bytes([
222        255, 255, 255, 255, 255, 127, 134, 89, 132, 222, 164, 168, 200, 91, 160, 180, 179, 39, 132,
223        17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
224    ]),
225    i256::from_le_bytes([
226        255, 255, 255, 255, 255, 255, 64, 127, 43, 177, 112, 150, 214, 149, 67, 14, 5, 141, 41,
227        175, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
228    ]),
229    i256::from_le_bytes([
230        255, 255, 255, 255, 255, 255, 137, 248, 178, 235, 102, 224, 97, 218, 163, 142, 50, 130,
231        159, 215, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232    ]),
233    i256::from_le_bytes([
234        255, 255, 255, 255, 255, 255, 99, 181, 253, 52, 5, 196, 210, 135, 102, 146, 249, 21, 59,
235        108, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
236    ]),
237    i256::from_le_bytes([
238        255, 255, 255, 255, 255, 255, 231, 21, 233, 17, 52, 168, 59, 78, 1, 184, 191, 219, 78, 58,
239        172, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
240    ]),
241    i256::from_le_bytes([
242        255, 255, 255, 255, 255, 255, 15, 219, 26, 179, 8, 146, 84, 14, 13, 48, 125, 149, 20, 71,
243        186, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
244    ]),
245    i256::from_le_bytes([
246        255, 255, 255, 255, 255, 255, 159, 142, 12, 255, 86, 180, 77, 143, 130, 224, 227, 214, 205,
247        198, 70, 11, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
248    ]),
249    i256::from_le_bytes([
250        255, 255, 255, 255, 255, 255, 63, 146, 125, 246, 101, 11, 9, 153, 25, 197, 230, 100, 10,
251        196, 195, 112, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0,
252    ]),
253    i256::from_le_bytes([
254        255, 255, 255, 255, 255, 255, 127, 182, 231, 160, 251, 113, 90, 250, 255, 178, 3, 241, 103,
255        168, 165, 103, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0,
256    ]),
257    i256::from_le_bytes([
258        255, 255, 255, 255, 255, 255, 255, 32, 13, 73, 212, 115, 136, 199, 255, 253, 36, 106, 15,
259        148, 120, 12, 20, 4, 0, 0, 0, 0, 0, 0, 0, 0,
260    ]),
261    i256::from_le_bytes([
262        255, 255, 255, 255, 255, 255, 255, 73, 131, 218, 74, 134, 84, 203, 253, 235, 113, 37, 154,
263        200, 181, 124, 200, 40, 0, 0, 0, 0, 0, 0, 0, 0,
264    ]),
265    i256::from_le_bytes([
266        255, 255, 255, 255, 255, 255, 255, 227, 32, 137, 236, 62, 77, 241, 233, 55, 115, 118, 5,
267        214, 25, 223, 212, 151, 1, 0, 0, 0, 0, 0, 0, 0,
268    ]),
269    i256::from_le_bytes([
270        255, 255, 255, 255, 255, 255, 255, 231, 72, 91, 61, 117, 4, 109, 35, 47, 128, 160, 54, 92,
271        2, 183, 80, 238, 15, 0, 0, 0, 0, 0, 0, 0,
272    ]),
273    i256::from_le_bytes([
274        255, 255, 255, 255, 255, 255, 255, 15, 217, 144, 101, 148, 44, 66, 98, 215, 1, 69, 34, 154,
275        23, 38, 39, 79, 159, 0, 0, 0, 0, 0, 0, 0,
276    ]),
277    i256::from_le_bytes([
278        255, 255, 255, 255, 255, 255, 255, 159, 122, 168, 247, 203, 189, 149, 214, 105, 18, 178,
279        86, 5, 236, 124, 135, 23, 57, 6, 0, 0, 0, 0, 0, 0,
280    ]),
281    i256::from_le_bytes([
282        255, 255, 255, 255, 255, 255, 255, 63, 202, 148, 172, 247, 105, 217, 97, 34, 184, 244, 98,
283        53, 56, 225, 74, 235, 58, 62, 0, 0, 0, 0, 0, 0,
284    ]),
285    i256::from_le_bytes([
286        255, 255, 255, 255, 255, 255, 255, 127, 230, 207, 189, 172, 35, 126, 210, 87, 49, 143, 221,
287        21, 50, 204, 236, 48, 77, 110, 2, 0, 0, 0, 0, 0,
288    ]),
289    i256::from_le_bytes([
290        255, 255, 255, 255, 255, 255, 255, 255, 0, 31, 106, 191, 100, 237, 56, 110, 237, 151, 167,
291        218, 244, 249, 63, 233, 3, 79, 24, 0, 0, 0, 0, 0,
292    ]),
293    i256::from_le_bytes([
294        255, 255, 255, 255, 255, 255, 255, 255, 9, 54, 37, 122, 239, 69, 57, 78, 70, 239, 139, 138,
295        144, 195, 127, 28, 39, 22, 243, 0, 0, 0, 0, 0,
296    ]),
297    i256::from_le_bytes([
298        255, 255, 255, 255, 255, 255, 255, 255, 99, 28, 116, 197, 90, 187, 60, 14, 191, 88, 119,
299        105, 165, 163, 253, 28, 135, 221, 126, 9, 0, 0, 0, 0,
300    ]),
301    i256::from_le_bytes([
302        255, 255, 255, 255, 255, 255, 255, 255, 231, 27, 137, 182, 139, 81, 95, 142, 118, 119, 169,
303        30, 118, 100, 232, 33, 71, 167, 244, 94, 0, 0, 0, 0,
304    ]),
305    i256::from_le_bytes([
306        255, 255, 255, 255, 255, 255, 255, 255, 15, 23, 91, 33, 117, 47, 185, 143, 161, 170, 158,
307        50, 157, 236, 19, 83, 199, 136, 142, 181, 3, 0, 0, 0,
308    ]),
309    i256::from_le_bytes([
310        255, 255, 255, 255, 255, 255, 255, 255, 159, 230, 142, 77, 147, 218, 59, 157, 79, 170, 50,
311        250, 35, 62, 199, 62, 201, 87, 145, 23, 37, 0, 0, 0,
312    ]),
313    i256::from_le_bytes([
314        255, 255, 255, 255, 255, 255, 255, 255, 63, 2, 149, 7, 193, 137, 86, 36, 28, 167, 250, 197,
315        103, 109, 200, 115, 220, 109, 173, 235, 114, 1, 0, 0,
316    ]),
317    i256::from_le_bytes([
318        255, 255, 255, 255, 255, 255, 255, 255, 127, 22, 210, 75, 138, 97, 97, 107, 25, 135, 202,
319        187, 13, 70, 212, 133, 156, 74, 198, 52, 125, 14, 0, 0,
320    ]),
321    i256::from_le_bytes([
322        255, 255, 255, 255, 255, 255, 255, 255, 255, 224, 52, 246, 102, 207, 205, 49, 254, 70, 233,
323        85, 137, 188, 74, 58, 29, 234, 190, 15, 228, 144, 0, 0,
324    ]),
325    i256::from_le_bytes([
326        255, 255, 255, 255, 255, 255, 255, 255, 255, 201, 16, 158, 5, 26, 10, 242, 237, 197, 28,
327        91, 93, 93, 235, 70, 36, 37, 117, 157, 232, 168, 5, 0,
328    ]),
329    i256::from_le_bytes([
330        255, 255, 255, 255, 255, 255, 255, 255, 255, 227, 167, 44, 56, 4, 101, 116, 75, 187, 31,
331        143, 165, 165, 49, 197, 106, 115, 147, 38, 22, 153, 56, 0,
332    ]),
333    i256::from_le_bytes([
334        255, 255, 255, 255, 255, 255, 255, 255, 255, 231, 142, 190, 49, 42, 242, 139, 242, 80, 61,
335        151, 119, 120, 240, 179, 43, 130, 194, 129, 221, 250, 53, 2,
336    ]),
337    i256::from_le_bytes([
338        255, 255, 255, 255, 255, 255, 255, 255, 255, 15, 149, 113, 241, 165, 117, 119, 121, 41,
339        101, 232, 171, 180, 100, 7, 181, 21, 153, 17, 167, 204, 27, 22,
340    ]),
341];
342
343/// MIN decimal256 value of little-endian format for each precision.
344/// Each element is the min value of signed 256-bit integer for the specified precision which
345/// is encoded to the 76-byte width format of little-endian.
346/// The first element is unused and is inserted so that we can look up using
347/// precision as the index without the need to subtract 1 first.
348pub(crate) const MIN_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION: [i256; 77] = [
349    i256::from_i128(0_i128), // unused first element
350    i256::from_le_bytes([
351        247, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
352        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
353    ]),
354    i256::from_le_bytes([
355        157, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
356        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
357    ]),
358    i256::from_le_bytes([
359        25, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
360        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
361    ]),
362    i256::from_le_bytes([
363        241, 216, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
364        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
365    ]),
366    i256::from_le_bytes([
367        97, 121, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
368        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
369    ]),
370    i256::from_le_bytes([
371        193, 189, 240, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
372        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
373    ]),
374    i256::from_le_bytes([
375        129, 105, 103, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
376        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
377    ]),
378    i256::from_le_bytes([
379        1, 31, 10, 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
380        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
381    ]),
382    i256::from_le_bytes([
383        1, 54, 101, 196, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
384        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
385    ]),
386    i256::from_le_bytes([
387        1, 28, 244, 171, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
388        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
389    ]),
390    i256::from_le_bytes([
391        1, 24, 137, 183, 232, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
392        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
393    ]),
394    i256::from_le_bytes([
395        1, 240, 90, 43, 23, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
396        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
397    ]),
398    i256::from_le_bytes([
399        1, 96, 141, 177, 231, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
400        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
401    ]),
402    i256::from_le_bytes([
403        1, 192, 133, 239, 12, 165, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
404        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
405    ]),
406    i256::from_le_bytes([
407        1, 128, 57, 91, 129, 114, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
408        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
409    ]),
410    i256::from_le_bytes([
411        1, 0, 63, 144, 13, 121, 220, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
412        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
413    ]),
414    i256::from_le_bytes([
415        1, 0, 118, 162, 135, 186, 156, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
416        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
417    ]),
418    i256::from_le_bytes([
419        1, 0, 156, 88, 76, 73, 31, 242, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
420        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
421    ]),
422    i256::from_le_bytes([
423        1, 0, 24, 118, 251, 220, 56, 117, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
424        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
425    ]),
426    i256::from_le_bytes([
427        1, 0, 240, 156, 210, 161, 56, 148, 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
428        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
429    ]),
430    i256::from_le_bytes([
431        1, 0, 96, 33, 58, 82, 54, 202, 201, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
432        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
433    ]),
434    i256::from_le_bytes([
435        1, 0, 192, 77, 69, 54, 31, 230, 225, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
436        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
437    ]),
438    i256::from_le_bytes([
439        1, 0, 128, 9, 181, 30, 56, 253, 210, 234, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
440        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
441    ]),
442    i256::from_le_bytes([
443        1, 0, 0, 95, 18, 51, 49, 228, 61, 44, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
444        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
445    ]),
446    i256::from_le_bytes([
447        1, 0, 0, 182, 183, 254, 235, 233, 106, 186, 247, 255, 255, 255, 255, 255, 255, 255, 255,
448        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
449    ]),
450    i256::from_le_bytes([
451        1, 0, 0, 28, 45, 243, 55, 35, 45, 72, 173, 255, 255, 255, 255, 255, 255, 255, 255, 255,
452        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
453    ]),
454    i256::from_le_bytes([
455        1, 0, 0, 24, 195, 127, 47, 96, 195, 209, 196, 252, 255, 255, 255, 255, 255, 255, 255, 255,
456        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
457    ]),
458    i256::from_le_bytes([
459        1, 0, 0, 240, 158, 253, 218, 193, 161, 49, 176, 223, 255, 255, 255, 255, 255, 255, 255,
460        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
461    ]),
462    i256::from_le_bytes([
463        1, 0, 0, 96, 53, 232, 141, 146, 81, 240, 225, 188, 254, 255, 255, 255, 255, 255, 255, 255,
464        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
465    ]),
466    i256::from_le_bytes([
467        1, 0, 0, 192, 21, 18, 139, 185, 47, 99, 211, 96, 243, 255, 255, 255, 255, 255, 255, 255,
468        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
469    ]),
470    i256::from_le_bytes([
471        1, 0, 0, 128, 217, 180, 110, 63, 221, 223, 65, 200, 129, 255, 255, 255, 255, 255, 255, 255,
472        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
473    ]),
474    i256::from_le_bytes([
475        1, 0, 0, 0, 127, 16, 83, 122, 164, 190, 146, 210, 17, 251, 255, 255, 255, 255, 255, 255,
476        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
477    ]),
478    i256::from_le_bytes([
479        1, 0, 0, 0, 246, 164, 62, 199, 108, 114, 187, 57, 178, 206, 255, 255, 255, 255, 255, 255,
480        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
481    ]),
482    i256::from_le_bytes([
483        1, 0, 0, 0, 156, 113, 114, 200, 63, 120, 82, 65, 246, 18, 254, 255, 255, 255, 255, 255,
484        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
485    ]),
486    i256::from_le_bytes([
487        1, 0, 0, 0, 24, 112, 120, 212, 125, 178, 56, 141, 158, 189, 236, 255, 255, 255, 255, 255,
488        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
489    ]),
490    i256::from_le_bytes([
491        1, 0, 0, 0, 240, 96, 180, 76, 234, 248, 54, 132, 49, 104, 63, 255, 255, 255, 255, 255, 255,
492        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
493    ]),
494    i256::from_le_bytes([
495        1, 0, 0, 0, 96, 201, 11, 255, 38, 185, 37, 42, 239, 17, 122, 248, 255, 255, 255, 255, 255,
496        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
497    ]),
498    i256::from_le_bytes([
499        1, 0, 0, 0, 192, 221, 117, 246, 133, 59, 121, 165, 87, 179, 196, 180, 255, 255, 255, 255,
500        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
501    ]),
502    i256::from_le_bytes([
503        1, 0, 0, 0, 128, 169, 154, 160, 59, 83, 188, 118, 108, 1, 175, 15, 253, 255, 255, 255, 255,
504        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
505    ]),
506    i256::from_le_bytes([
507        1, 0, 0, 0, 0, 159, 10, 70, 84, 64, 91, 163, 60, 14, 214, 156, 226, 255, 255, 255, 255,
508        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
509    ]),
510    i256::from_le_bytes([
511        1, 0, 0, 0, 0, 54, 106, 188, 74, 131, 144, 97, 94, 142, 92, 32, 218, 254, 255, 255, 255,
512        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
513    ]),
514    i256::from_le_bytes([
515        1, 0, 0, 0, 0, 28, 38, 92, 235, 32, 165, 207, 175, 143, 157, 67, 133, 244, 255, 255, 255,
516        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
517    ]),
518    i256::from_le_bytes([
519        1, 0, 0, 0, 0, 24, 125, 153, 49, 73, 115, 28, 222, 156, 39, 164, 52, 141, 255, 255, 255,
520        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
521    ]),
522    i256::from_le_bytes([
523        1, 0, 0, 0, 0, 240, 226, 254, 239, 219, 128, 28, 173, 32, 140, 105, 14, 132, 251, 255, 255,
524        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
525    ]),
526    i256::from_le_bytes([
527        1, 0, 0, 0, 0, 96, 221, 244, 95, 151, 8, 29, 195, 70, 121, 31, 144, 40, 211, 255, 255, 255,
528        255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
529    ]),
530    i256::from_le_bytes([
531        1, 0, 0, 0, 0, 192, 165, 144, 191, 233, 85, 34, 159, 195, 188, 58, 161, 149, 63, 254, 255,
532        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
533    ]),
534    i256::from_le_bytes([
535        1, 0, 0, 0, 0, 128, 121, 166, 123, 33, 91, 87, 55, 164, 95, 75, 76, 216, 123, 238, 255,
536        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
537    ]),
538    i256::from_le_bytes([
539        1, 0, 0, 0, 0, 0, 191, 128, 212, 78, 143, 105, 41, 106, 188, 241, 250, 114, 214, 80, 255,
540        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
541    ]),
542    i256::from_le_bytes([
543        1, 0, 0, 0, 0, 0, 118, 7, 77, 20, 153, 31, 158, 37, 92, 113, 205, 125, 96, 40, 249, 255,
544        255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
545    ]),
546    i256::from_le_bytes([
547        1, 0, 0, 0, 0, 0, 156, 74, 2, 203, 250, 59, 45, 120, 153, 109, 6, 234, 196, 147, 187, 255,
548        255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
549    ]),
550    i256::from_le_bytes([
551        1, 0, 0, 0, 0, 0, 24, 234, 22, 238, 203, 87, 196, 177, 254, 71, 64, 36, 177, 197, 83, 253,
552        255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
553    ]),
554    i256::from_le_bytes([
555        1, 0, 0, 0, 0, 0, 240, 36, 229, 76, 247, 109, 171, 241, 242, 207, 130, 106, 235, 184, 69,
556        229, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
557    ]),
558    i256::from_le_bytes([
559        1, 0, 0, 0, 0, 0, 96, 113, 243, 0, 169, 75, 178, 112, 125, 31, 28, 41, 50, 57, 185, 244,
560        254, 255, 255, 255, 255, 255, 255, 255, 255, 255,
561    ]),
562    i256::from_le_bytes([
563        1, 0, 0, 0, 0, 0, 192, 109, 130, 9, 154, 244, 246, 102, 230, 58, 25, 155, 245, 59, 60, 143,
564        245, 255, 255, 255, 255, 255, 255, 255, 255, 255,
565    ]),
566    i256::from_le_bytes([
567        1, 0, 0, 0, 0, 0, 128, 73, 24, 95, 4, 142, 165, 5, 0, 77, 252, 14, 152, 87, 90, 152, 151,
568        255, 255, 255, 255, 255, 255, 255, 255, 255,
569    ]),
570    i256::from_le_bytes([
571        1, 0, 0, 0, 0, 0, 0, 223, 242, 182, 43, 140, 119, 56, 0, 2, 219, 149, 240, 107, 135, 243,
572        235, 251, 255, 255, 255, 255, 255, 255, 255, 255,
573    ]),
574    i256::from_le_bytes([
575        1, 0, 0, 0, 0, 0, 0, 182, 124, 37, 181, 121, 171, 52, 2, 20, 142, 218, 101, 55, 74, 131,
576        55, 215, 255, 255, 255, 255, 255, 255, 255, 255,
577    ]),
578    i256::from_le_bytes([
579        1, 0, 0, 0, 0, 0, 0, 28, 223, 118, 19, 193, 178, 14, 22, 200, 140, 137, 250, 41, 230, 32,
580        43, 104, 254, 255, 255, 255, 255, 255, 255, 255,
581    ]),
582    i256::from_le_bytes([
583        1, 0, 0, 0, 0, 0, 0, 24, 183, 164, 194, 138, 251, 146, 220, 208, 127, 95, 201, 163, 253,
584        72, 175, 17, 240, 255, 255, 255, 255, 255, 255, 255,
585    ]),
586    i256::from_le_bytes([
587        1, 0, 0, 0, 0, 0, 0, 240, 38, 111, 154, 107, 211, 189, 157, 40, 254, 186, 221, 101, 232,
588        217, 216, 176, 96, 255, 255, 255, 255, 255, 255, 255,
589    ]),
590    i256::from_le_bytes([
591        1, 0, 0, 0, 0, 0, 0, 96, 133, 87, 8, 52, 66, 106, 41, 150, 237, 77, 169, 250, 19, 131, 120,
592        232, 198, 249, 255, 255, 255, 255, 255, 255,
593    ]),
594    i256::from_le_bytes([
595        1, 0, 0, 0, 0, 0, 0, 192, 53, 107, 83, 8, 150, 38, 158, 221, 71, 11, 157, 202, 199, 30,
596        181, 20, 197, 193, 255, 255, 255, 255, 255, 255,
597    ]),
598    i256::from_le_bytes([
599        1, 0, 0, 0, 0, 0, 0, 128, 25, 48, 66, 83, 220, 129, 45, 168, 206, 112, 34, 234, 205, 51,
600        19, 207, 178, 145, 253, 255, 255, 255, 255, 255,
601    ]),
602    i256::from_le_bytes([
603        1, 0, 0, 0, 0, 0, 0, 0, 255, 224, 149, 64, 155, 18, 199, 145, 18, 104, 88, 37, 11, 6, 192,
604        22, 252, 176, 231, 255, 255, 255, 255, 255,
605    ]),
606    i256::from_le_bytes([
607        1, 0, 0, 0, 0, 0, 0, 0, 246, 201, 218, 133, 16, 186, 198, 177, 185, 16, 116, 117, 111, 60,
608        128, 227, 216, 233, 12, 255, 255, 255, 255, 255,
609    ]),
610    i256::from_le_bytes([
611        1, 0, 0, 0, 0, 0, 0, 0, 156, 227, 139, 58, 165, 68, 195, 241, 64, 167, 136, 150, 90, 92, 2,
612        227, 120, 34, 129, 246, 255, 255, 255, 255,
613    ]),
614    i256::from_le_bytes([
615        1, 0, 0, 0, 0, 0, 0, 0, 24, 228, 118, 73, 116, 174, 160, 113, 137, 136, 86, 225, 137, 155,
616        23, 222, 184, 88, 11, 161, 255, 255, 255, 255,
617    ]),
618    i256::from_le_bytes([
619        1, 0, 0, 0, 0, 0, 0, 0, 240, 232, 164, 222, 138, 208, 70, 112, 94, 85, 97, 205, 98, 19,
620        236, 172, 56, 119, 113, 74, 252, 255, 255, 255,
621    ]),
622    i256::from_le_bytes([
623        1, 0, 0, 0, 0, 0, 0, 0, 96, 25, 113, 178, 108, 37, 196, 98, 176, 85, 205, 5, 220, 193, 56,
624        193, 54, 168, 110, 232, 218, 255, 255, 255,
625    ]),
626    i256::from_le_bytes([
627        1, 0, 0, 0, 0, 0, 0, 0, 192, 253, 106, 248, 62, 118, 169, 219, 227, 88, 5, 58, 152, 146,
628        55, 140, 35, 146, 82, 20, 141, 254, 255, 255,
629    ]),
630    i256::from_le_bytes([
631        1, 0, 0, 0, 0, 0, 0, 0, 128, 233, 45, 180, 117, 158, 158, 148, 230, 120, 53, 68, 242, 185,
632        43, 122, 99, 181, 57, 203, 130, 241, 255, 255,
633    ]),
634    i256::from_le_bytes([
635        1, 0, 0, 0, 0, 0, 0, 0, 0, 31, 203, 9, 153, 48, 50, 206, 1, 185, 22, 170, 118, 67, 181,
636        197, 226, 21, 65, 240, 27, 111, 255, 255,
637    ]),
638    i256::from_le_bytes([
639        1, 0, 0, 0, 0, 0, 0, 0, 0, 54, 239, 97, 250, 229, 245, 13, 18, 58, 227, 164, 162, 162, 20,
640        185, 219, 218, 138, 98, 23, 87, 250, 255,
641    ]),
642    i256::from_le_bytes([
643        1, 0, 0, 0, 0, 0, 0, 0, 0, 28, 88, 211, 199, 251, 154, 139, 180, 68, 224, 112, 90, 90, 206,
644        58, 149, 140, 108, 217, 233, 102, 199, 255,
645    ]),
646    i256::from_le_bytes([
647        1, 0, 0, 0, 0, 0, 0, 0, 0, 24, 113, 65, 206, 213, 13, 116, 13, 175, 194, 104, 136, 135, 15,
648        76, 212, 125, 61, 126, 34, 5, 202, 253,
649    ]),
650    i256::from_le_bytes([
651        1, 0, 0, 0, 0, 0, 0, 0, 0, 240, 106, 142, 14, 90, 138, 136, 134, 214, 154, 23, 84, 75, 155,
652        248, 74, 234, 102, 238, 88, 51, 228, 233,
653    ]),
654];
655
656/// `MAX_DECIMAL_FOR_EACH_PRECISION[p-1]` holds the maximum `i128` value that can
657/// be stored in [arrow_schema::DataType::Decimal128] value of precision `p`
658#[allow(dead_code)] // no longer used but is part of our public API
659pub const MAX_DECIMAL_FOR_EACH_PRECISION: [i128; 38] = [
660    9,
661    99,
662    999,
663    9999,
664    99999,
665    999999,
666    9999999,
667    99999999,
668    999999999,
669    9999999999,
670    99999999999,
671    999999999999,
672    9999999999999,
673    99999999999999,
674    999999999999999,
675    9999999999999999,
676    99999999999999999,
677    999999999999999999,
678    9999999999999999999,
679    99999999999999999999,
680    999999999999999999999,
681    9999999999999999999999,
682    99999999999999999999999,
683    999999999999999999999999,
684    9999999999999999999999999,
685    99999999999999999999999999,
686    999999999999999999999999999,
687    9999999999999999999999999999,
688    99999999999999999999999999999,
689    999999999999999999999999999999,
690    9999999999999999999999999999999,
691    99999999999999999999999999999999,
692    999999999999999999999999999999999,
693    9999999999999999999999999999999999,
694    99999999999999999999999999999999999,
695    999999999999999999999999999999999999,
696    9999999999999999999999999999999999999,
697    99999999999999999999999999999999999999,
698];
699
700/// `MIN_DECIMAL_FOR_EACH_PRECISION[p-1]` holds the minimum `i128` value that can
701/// be stored in a [arrow_schema::DataType::Decimal128] value of precision `p`
702#[allow(dead_code)] // no longer used but is part of our public API
703pub const MIN_DECIMAL_FOR_EACH_PRECISION: [i128; 38] = [
704    -9,
705    -99,
706    -999,
707    -9999,
708    -99999,
709    -999999,
710    -9999999,
711    -99999999,
712    -999999999,
713    -9999999999,
714    -99999999999,
715    -999999999999,
716    -9999999999999,
717    -99999999999999,
718    -999999999999999,
719    -9999999999999999,
720    -99999999999999999,
721    -999999999999999999,
722    -9999999999999999999,
723    -99999999999999999999,
724    -999999999999999999999,
725    -9999999999999999999999,
726    -99999999999999999999999,
727    -999999999999999999999999,
728    -9999999999999999999999999,
729    -99999999999999999999999999,
730    -999999999999999999999999999,
731    -9999999999999999999999999999,
732    -99999999999999999999999999999,
733    -999999999999999999999999999999,
734    -9999999999999999999999999999999,
735    -99999999999999999999999999999999,
736    -999999999999999999999999999999999,
737    -9999999999999999999999999999999999,
738    -99999999999999999999999999999999999,
739    -999999999999999999999999999999999999,
740    -9999999999999999999999999999999999999,
741    -99999999999999999999999999999999999999,
742];
743
744/// `MAX_DECIMAL_FOR_EACH_PRECISION_ONE_BASED[p]` holds the maximum `i128` value that can
745/// be stored in [arrow_schema::DataType::Decimal128] value of precision `p`.
746/// The first element is unused and is inserted so that we can look up using
747/// precision as the index without the need to subtract 1 first.
748pub(crate) const MAX_DECIMAL_FOR_EACH_PRECISION_ONE_BASED: [i128; 39] = [
749    0, // unused first element
750    9,
751    99,
752    999,
753    9999,
754    99999,
755    999999,
756    9999999,
757    99999999,
758    999999999,
759    9999999999,
760    99999999999,
761    999999999999,
762    9999999999999,
763    99999999999999,
764    999999999999999,
765    9999999999999999,
766    99999999999999999,
767    999999999999999999,
768    9999999999999999999,
769    99999999999999999999,
770    999999999999999999999,
771    9999999999999999999999,
772    99999999999999999999999,
773    999999999999999999999999,
774    9999999999999999999999999,
775    99999999999999999999999999,
776    999999999999999999999999999,
777    9999999999999999999999999999,
778    99999999999999999999999999999,
779    999999999999999999999999999999,
780    9999999999999999999999999999999,
781    99999999999999999999999999999999,
782    999999999999999999999999999999999,
783    9999999999999999999999999999999999,
784    99999999999999999999999999999999999,
785    999999999999999999999999999999999999,
786    9999999999999999999999999999999999999,
787    99999999999999999999999999999999999999,
788];
789
790/// `MIN_DECIMAL_FOR_EACH_PRECISION[p]` holds the minimum `i128` value that can
791/// be stored in a [arrow_schema::DataType::Decimal128] value of precision `p`.
792/// The first element is unused and is inserted so that we can look up using
793/// precision as the index without the need to subtract 1 first.
794pub(crate) const MIN_DECIMAL_FOR_EACH_PRECISION_ONE_BASED: [i128; 39] = [
795    0, // unused first element
796    -9,
797    -99,
798    -999,
799    -9999,
800    -99999,
801    -999999,
802    -9999999,
803    -99999999,
804    -999999999,
805    -9999999999,
806    -99999999999,
807    -999999999999,
808    -9999999999999,
809    -99999999999999,
810    -999999999999999,
811    -9999999999999999,
812    -99999999999999999,
813    -999999999999999999,
814    -9999999999999999999,
815    -99999999999999999999,
816    -999999999999999999999,
817    -9999999999999999999999,
818    -99999999999999999999999,
819    -999999999999999999999999,
820    -9999999999999999999999999,
821    -99999999999999999999999999,
822    -999999999999999999999999999,
823    -9999999999999999999999999999,
824    -99999999999999999999999999999,
825    -999999999999999999999999999999,
826    -9999999999999999999999999999999,
827    -99999999999999999999999999999999,
828    -999999999999999999999999999999999,
829    -9999999999999999999999999999999999,
830    -99999999999999999999999999999999999,
831    -999999999999999999999999999999999999,
832    -9999999999999999999999999999999999999,
833    -99999999999999999999999999999999999999,
834];
835
836/// Validates that the specified `i128` value can be properly
837/// interpreted as a Decimal number with precision `precision`
838#[inline]
839pub fn validate_decimal_precision(value: i128, precision: u8) -> Result<(), ArrowError> {
840    if precision > DECIMAL128_MAX_PRECISION {
841        return Err(ArrowError::InvalidArgumentError(format!(
842            "Max precision of a Decimal128 is {DECIMAL128_MAX_PRECISION}, but got {precision}",
843        )));
844    }
845    if value > MAX_DECIMAL_FOR_EACH_PRECISION_ONE_BASED[precision as usize] {
846        Err(ArrowError::InvalidArgumentError(format!(
847            "{value} is too large to store in a Decimal128 of precision {precision}. Max is {}",
848            MAX_DECIMAL_FOR_EACH_PRECISION_ONE_BASED[precision as usize]
849        )))
850    } else if value < MIN_DECIMAL_FOR_EACH_PRECISION_ONE_BASED[precision as usize] {
851        Err(ArrowError::InvalidArgumentError(format!(
852            "{value} is too small to store in a Decimal128 of precision {precision}. Min is {}",
853            MIN_DECIMAL_FOR_EACH_PRECISION_ONE_BASED[precision as usize]
854        )))
855    } else {
856        Ok(())
857    }
858}
859
860/// Determines whether the specified `i128` value can be properly
861/// interpreted as a Decimal number with precision `precision`
862#[inline]
863pub fn is_validate_decimal_precision(value: i128, precision: u8) -> bool {
864    precision <= DECIMAL128_MAX_PRECISION
865        && value >= MIN_DECIMAL_FOR_EACH_PRECISION_ONE_BASED[precision as usize]
866        && value <= MAX_DECIMAL_FOR_EACH_PRECISION_ONE_BASED[precision as usize]
867}
868
869/// Validates that the specified `i256` of value can be properly
870/// interpreted as a Decimal256 number with precision `precision`
871#[inline]
872pub fn validate_decimal256_precision(value: i256, precision: u8) -> Result<(), ArrowError> {
873    if precision > DECIMAL256_MAX_PRECISION {
874        return Err(ArrowError::InvalidArgumentError(format!(
875            "Max precision of a Decimal256 is {DECIMAL256_MAX_PRECISION}, but got {precision}",
876        )));
877    }
878    if value > MAX_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION[precision as usize] {
879        Err(ArrowError::InvalidArgumentError(format!(
880            "{value:?} is too large to store in a Decimal256 of precision {precision}. Max is {:?}",
881            MAX_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION[precision as usize]
882        )))
883    } else if value < MIN_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION[precision as usize] {
884        Err(ArrowError::InvalidArgumentError(format!(
885            "{value:?} is too small to store in a Decimal256 of precision {precision}. Min is {:?}",
886            MIN_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION[precision as usize]
887        )))
888    } else {
889        Ok(())
890    }
891}
892
893/// Determines whether the specified `i256` value can be properly
894/// interpreted as a Decimal256 number with precision `precision`
895#[inline]
896pub fn is_validate_decimal256_precision(value: i256, precision: u8) -> bool {
897    precision <= DECIMAL256_MAX_PRECISION
898        && value >= MIN_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION[precision as usize]
899        && value <= MAX_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION[precision as usize]
900}