Skip to main content

arrow_json/reader/
run_end_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::ops::Range;
7use std::slice::from_ref;
8use std::sync::Arc;
9
10use arrow_array::types::RunEndIndexType;
11use arrow_array::{ArrayRef, RunArray, UInt32Array, new_empty_array};
12use arrow_buffer::{ArrowNativeType, RunEndBuffer, ScalarBuffer};
13use arrow_ord::partition::partition;
14use arrow_schema::{ArrowError, DataType};
15use arrow_select::take::take;
16
17use crate::reader::tape::Tape;
18use crate::reader::{ArrayDecoder, DecoderContext};
19
20pub struct RunEndEncodedArrayDecoder<R> {
21    data_type: DataType,
22    decoder: Box<dyn ArrayDecoder>,
23    values_nullable: bool,
24    phantom: PhantomData<R>,
25}
26
27impl<R: RunEndIndexType> RunEndEncodedArrayDecoder<R> {
28    pub fn new(
29        ctx: &DecoderContext,
30        data_type: &DataType,
31        is_nullable: bool,
32    ) -> Result<Self, ArrowError> {
33        let DataType::RunEndEncoded(_, values_field) = data_type else {
34            unreachable!()
35        };
36        let values_nullable = values_field.is_nullable() && is_nullable;
37        let decoder = ctx.make_decoder(values_field, values_nullable)?;
38
39        Ok(Self {
40            data_type: data_type.clone(),
41            decoder,
42            values_nullable,
43            phantom: Default::default(),
44        })
45    }
46}
47
48impl<R: RunEndIndexType + Send> ArrayDecoder for RunEndEncodedArrayDecoder<R> {
49    fn decode(&mut self, tape: &Tape<'_>, pos: &[u32]) -> Result<ArrayRef, ArrowError> {
50        let len = pos.len();
51        if len == 0 {
52            return Ok(new_empty_array(&self.data_type));
53        }
54
55        let flat_array = self.decoder.decode(tape, pos)?;
56        if !self.values_nullable && flat_array.logical_null_count() != 0 {
57            return Err(ArrowError::JsonError(
58                "Encountered nulls in non-nullable values of RunEndEncoded".to_string(),
59            ));
60        }
61
62        let partitions = partition(from_ref(&flat_array))?;
63        let size = partitions.len();
64        let mut run_ends = Vec::with_capacity(size);
65        let mut indices = Vec::with_capacity(size);
66
67        for Range { start, end } in partitions.ranges() {
68            let run_end = R::Native::from_usize(end).ok_or_else(|| {
69                ArrowError::JsonError(format!(
70                    "Run end value {end} exceeds {:?} range",
71                    R::DATA_TYPE
72                ))
73            })?;
74            run_ends.push(run_end);
75            indices.push(start);
76        }
77
78        let indices = UInt32Array::from_iter_values(indices.into_iter().map(|i| i as u32));
79        let values = take(flat_array.as_ref(), &indices, None)?;
80
81        // SAFETY: run_ends are strictly increasing with the last value equal to len
82        let run_ends = unsafe { RunEndBuffer::new_unchecked(ScalarBuffer::from(run_ends), 0, len) };
83
84        // SAFETY: run_ends are valid and values has the same length as run_ends
85        let array =
86            unsafe { RunArray::<R>::new_unchecked(self.data_type.clone(), run_ends, values) };
87        Ok(Arc::new(array))
88    }
89}