arrow_json/reader/
decimal_array.rs1use std::marker::PhantomData;
6use std::sync::Arc;
7
8use arrow_array::ArrayRef;
9use arrow_array::builder::PrimitiveBuilder;
10use arrow_array::types::DecimalType;
11use arrow_cast::parse::parse_decimal;
12use arrow_schema::ArrowError;
13
14use crate::reader::tape::{Tape, TapeElement};
15use crate::reader::{ArrayDecoder, DecoderContext};
16
17pub struct DecimalArrayDecoder<D: DecimalType> {
18 precision: u8,
19 scale: i8,
20 ignore_type_conflicts: bool,
21 phantom: PhantomData<fn(D) -> D>,
23}
24
25impl<D: DecimalType> DecimalArrayDecoder<D> {
26 pub fn new(ctx: &DecoderContext, precision: u8, scale: i8) -> Self {
27 Self {
28 precision,
29 scale,
30 ignore_type_conflicts: ctx.ignore_type_conflicts(),
31 phantom: PhantomData,
32 }
33 }
34}
35
36impl<D> ArrayDecoder for DecimalArrayDecoder<D>
37where
38 D: DecimalType,
39{
40 fn decode(&mut self, tape: &Tape<'_>, pos: &[u32]) -> Result<ArrayRef, ArrowError> {
41 let mut builder = PrimitiveBuilder::<D>::with_capacity(pos.len());
42
43 #[expect(unused_assignments)] let mut anchor = String::default();
45 for p in pos {
46 let value = match tape.get(*p) {
47 TapeElement::Null => {
48 builder.append_null();
49 continue;
50 }
51 TapeElement::String(idx) | TapeElement::Number(idx) => tape.get_string(idx),
52 TapeElement::I64(high) => match tape.get(*p + 1) {
53 TapeElement::I32(low) => {
54 anchor = (((high as i64) << 32) | (low as u32) as i64).to_string();
55 anchor.as_str()
56 }
57 _ => unreachable!(),
58 },
59 TapeElement::I32(val) => {
60 anchor = val.to_string();
61 anchor.as_str()
62 }
63 TapeElement::F64(high) => match tape.get(*p + 1) {
64 TapeElement::F32(low) => {
65 anchor = f64::from_bits(((high as u64) << 32) | low as u64).to_string();
66 anchor.as_str()
67 }
68 _ => unreachable!(),
69 },
70 TapeElement::F32(val) => {
71 anchor = f32::from_bits(val).to_string();
72 anchor.as_str()
73 }
74 _ if self.ignore_type_conflicts => {
75 builder.append_null();
76 continue;
77 }
78 _ => return Err(tape.error(*p, "decimal")),
79 };
80
81 match parse_decimal::<D>(value, self.precision, self.scale) {
82 Ok(value) => builder.append_value(value),
83 Err(_) if self.ignore_type_conflicts => builder.append_null(),
84 Err(e) => return Err(e),
85 }
86 }
87
88 Ok(Arc::new(
89 builder
90 .finish()
91 .with_precision_and_scale(self.precision, self.scale)?,
92 ))
93 }
94}