Skip to main content

arrow_json/reader/
string_view_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::fmt::Write;
6use std::sync::Arc;
7
8use arrow_array::ArrayRef;
9use arrow_array::builder::GenericByteViewBuilder;
10use arrow_array::types::StringViewType;
11use arrow_schema::ArrowError;
12
13use crate::reader::tape::{Tape, TapeElement};
14use crate::reader::{ArrayDecoder, DecoderContext};
15
16const TRUE: &str = "true";
17const FALSE: &str = "false";
18
19pub struct StringViewArrayDecoder {
20    coerce_primitive: bool,
21    ignore_type_conflicts: bool,
22}
23
24impl StringViewArrayDecoder {
25    pub fn new(ctx: &DecoderContext) -> Self {
26        Self {
27            coerce_primitive: ctx.coerce_primitive(),
28            ignore_type_conflicts: ctx.ignore_type_conflicts(),
29        }
30    }
31}
32
33impl ArrayDecoder for StringViewArrayDecoder {
34    fn decode(&mut self, tape: &Tape<'_>, pos: &[u32]) -> Result<ArrayRef, ArrowError> {
35        let coerce = self.coerce_primitive;
36        let mut data_capacity = 0;
37        for &p in pos {
38            // note that StringView is different that StringArray in that only
39            // "long" strings (longer than 12 bytes) are stored in the buffer.
40            // "short" strings are inlined into a fixed length structure.
41            match tape.get(p) {
42                TapeElement::String(idx) => {
43                    let s = tape.get_string(idx);
44                    // Only increase capacity if the string length is greater than 12 bytes
45                    if s.len() > 12 {
46                        data_capacity += s.len();
47                    }
48                }
49                TapeElement::Null => {
50                    // Do not increase capacity for null values
51                }
52                // For booleans, do not increase capacity (both "true" and "false" are less than
53                // 12 bytes)
54                TapeElement::True if coerce => {}
55                TapeElement::False if coerce => {}
56                // For Number, use the same strategy as for strings
57                TapeElement::Number(idx) if coerce => {
58                    let s = tape.get_string(idx);
59                    if s.len() > 12 {
60                        data_capacity += s.len();
61                    }
62                }
63                // For I64, only add capacity if the absolute value is greater than 999,999,999,999
64                // (the largest number that can fit in 12 bytes)
65                TapeElement::I64(_) if coerce => {
66                    match tape.get(p + 1) {
67                        TapeElement::I32(_) => {
68                            let TapeElement::I64(high) = tape.get(p) else {
69                                unreachable!()
70                            };
71                            let TapeElement::I32(low) = tape.get(p + 1) else {
72                                unreachable!()
73                            };
74                            let val = ((high as i64) << 32) | (low as u32) as i64;
75                            if val.abs() > 999_999_999_999 {
76                                // Only allocate capacity based on the string representation if the number is large
77                                data_capacity += val.to_string().len();
78                            }
79                        }
80                        _ => unreachable!(),
81                    }
82                }
83                // For I32, do not increase capacity (the longest string representation is <= 12 bytes)
84                TapeElement::I32(_) if coerce => {}
85                // For F32 and F64, keep the existing estimate
86                TapeElement::F32(_) if coerce => {
87                    data_capacity += 10;
88                }
89                TapeElement::F64(_) if coerce => {
90                    data_capacity += 10;
91                }
92                _ if self.ignore_type_conflicts => {} // treat type conflicts like nulls
93                _ => {
94                    return Err(tape.error(p, "string"));
95                }
96            }
97        }
98
99        let mut builder = GenericByteViewBuilder::<StringViewType>::with_capacity(data_capacity);
100        // Temporary buffer to avoid per-iteration allocation for numeric types
101        let mut tmp_buf = String::new();
102
103        for &p in pos {
104            match tape.get(p) {
105                TapeElement::String(idx) => {
106                    builder.append_value(tape.get_string(idx));
107                }
108                TapeElement::Null => {
109                    builder.append_null();
110                }
111                TapeElement::True if coerce => {
112                    builder.append_value(TRUE);
113                }
114                TapeElement::False if coerce => {
115                    builder.append_value(FALSE);
116                }
117                TapeElement::Number(idx) if coerce => {
118                    builder.append_value(tape.get_string(idx));
119                }
120                TapeElement::I64(high) if coerce => match tape.get(p + 1) {
121                    TapeElement::I32(low) => {
122                        let val = ((high as i64) << 32) | (low as u32) as i64;
123                        tmp_buf.clear();
124                        // Reuse the temporary buffer instead of allocating a new String
125                        write!(&mut tmp_buf, "{val}").unwrap();
126                        builder.append_value(&tmp_buf);
127                    }
128                    _ => unreachable!(),
129                },
130                TapeElement::I32(n) if coerce => {
131                    tmp_buf.clear();
132                    write!(&mut tmp_buf, "{n}").unwrap();
133                    builder.append_value(&tmp_buf);
134                }
135                TapeElement::F32(n) if coerce => {
136                    tmp_buf.clear();
137                    write!(&mut tmp_buf, "{n}").unwrap();
138                    builder.append_value(&tmp_buf);
139                }
140                TapeElement::F64(high) if coerce => match tape.get(p + 1) {
141                    TapeElement::F32(low) => {
142                        let val = f64::from_bits(((high as u64) << 32) | (low as u64));
143                        tmp_buf.clear();
144                        write!(&mut tmp_buf, "{val}").unwrap();
145                        builder.append_value(&tmp_buf);
146                    }
147                    _ => unreachable!(),
148                },
149                _ if self.ignore_type_conflicts => {
150                    builder.append_null();
151                }
152                _ => unreachable!(),
153            }
154        }
155
156        Ok(Arc::new(builder.finish()))
157    }
158}