Skip to main content

arrow_json/reader/
string_array.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use std::marker::PhantomData;
6use std::sync::Arc;
7
8use arrow_array::builder::GenericStringBuilder;
9use arrow_array::{ArrayRef, GenericStringArray, OffsetSizeTrait};
10use arrow_schema::ArrowError;
11use itoa;
12use ryu;
13
14use crate::reader::tape::{Tape, TapeElement};
15use crate::reader::{ArrayDecoder, DecoderContext};
16
17const TRUE: &str = "true";
18const FALSE: &str = "false";
19
20pub struct StringArrayDecoder<O: OffsetSizeTrait> {
21    coerce_primitive: bool,
22    ignore_type_conflicts: bool,
23    phantom: PhantomData<O>,
24}
25
26impl<O: OffsetSizeTrait> StringArrayDecoder<O> {
27    pub fn new(ctx: &DecoderContext) -> Self {
28        Self {
29            coerce_primitive: ctx.coerce_primitive(),
30            ignore_type_conflicts: ctx.ignore_type_conflicts(),
31            phantom: Default::default(),
32        }
33    }
34}
35
36impl<O: OffsetSizeTrait> ArrayDecoder for StringArrayDecoder<O> {
37    fn decode(&mut self, tape: &Tape<'_>, pos: &[u32]) -> Result<ArrayRef, ArrowError> {
38        let coerce_primitive = self.coerce_primitive;
39
40        let mut data_capacity = 0;
41        for p in pos {
42            match tape.get(*p) {
43                TapeElement::String(idx) => {
44                    data_capacity += tape.get_string(idx).len();
45                }
46                TapeElement::Null => {}
47                TapeElement::True if coerce_primitive => {
48                    data_capacity += TRUE.len();
49                }
50                TapeElement::False if coerce_primitive => {
51                    data_capacity += FALSE.len();
52                }
53                TapeElement::Number(idx) if coerce_primitive => {
54                    data_capacity += tape.get_string(idx).len();
55                }
56                TapeElement::I64(_)
57                | TapeElement::I32(_)
58                | TapeElement::F64(_)
59                | TapeElement::F32(_)
60                    if coerce_primitive =>
61                {
62                    // An arbitrary estimate
63                    data_capacity += 10;
64                }
65                _ if self.ignore_type_conflicts => {}
66                _ => {
67                    return Err(tape.error(*p, "string"));
68                }
69            }
70        }
71
72        if O::from_usize(data_capacity).is_none() {
73            return Err(ArrowError::JsonError(format!(
74                "offset overflow decoding {}",
75                GenericStringArray::<O>::DATA_TYPE
76            )));
77        }
78
79        let mut builder = GenericStringBuilder::<O>::with_capacity(pos.len(), data_capacity);
80
81        let mut float_formatter = ryu::Buffer::new();
82        let mut int_formatter = itoa::Buffer::new();
83
84        for p in pos {
85            match tape.get(*p) {
86                TapeElement::String(idx) => {
87                    builder.append_value(tape.get_string(idx));
88                }
89                TapeElement::Null => builder.append_null(),
90                TapeElement::True if coerce_primitive => {
91                    builder.append_value(TRUE);
92                }
93                TapeElement::False if coerce_primitive => {
94                    builder.append_value(FALSE);
95                }
96                TapeElement::Number(idx) if coerce_primitive => {
97                    builder.append_value(tape.get_string(idx));
98                }
99                TapeElement::I64(high) if coerce_primitive => match tape.get(p + 1) {
100                    TapeElement::I32(low) => {
101                        let val = ((high as i64) << 32) | (low as u32) as i64;
102                        builder.append_value(int_formatter.format(val));
103                    }
104                    _ => unreachable!(),
105                },
106                TapeElement::I32(n) if coerce_primitive => {
107                    builder.append_value(int_formatter.format(n));
108                }
109                TapeElement::F32(n) if coerce_primitive => {
110                    builder.append_value(float_formatter.format_finite(f32::from_bits(n)));
111                }
112                TapeElement::F64(high) if coerce_primitive => match tape.get(p + 1) {
113                    TapeElement::F32(low) => {
114                        let val = f64::from_bits(((high as u64) << 32) | low as u64);
115                        builder.append_value(float_formatter.format_finite(val));
116                    }
117                    _ => unreachable!(),
118                },
119                _ if self.ignore_type_conflicts => builder.append_null(),
120                _ => unreachable!(),
121            }
122        }
123
124        Ok(Arc::new(builder.finish()))
125    }
126}