Skip to main content

arrow_json/reader/
boolean_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::sync::Arc;
6
7use arrow_array::ArrayRef;
8use arrow_array::builder::BooleanBuilder;
9use arrow_schema::ArrowError;
10
11use crate::reader::tape::{Tape, TapeElement};
12use crate::reader::{ArrayDecoder, DecoderContext};
13
14#[derive(Default)]
15pub struct BooleanArrayDecoder {
16    ignore_type_conflicts: bool,
17}
18impl BooleanArrayDecoder {
19    pub fn new(ctx: &DecoderContext) -> Self {
20        Self {
21            ignore_type_conflicts: ctx.ignore_type_conflicts(),
22        }
23    }
24}
25
26impl ArrayDecoder for BooleanArrayDecoder {
27    fn decode(&mut self, tape: &Tape<'_>, pos: &[u32]) -> Result<ArrayRef, ArrowError> {
28        let mut builder = BooleanBuilder::with_capacity(pos.len());
29        for p in pos {
30            match tape.get(*p) {
31                TapeElement::Null => builder.append_null(),
32                TapeElement::True => builder.append_value(true),
33                TapeElement::False => builder.append_value(false),
34                _ if self.ignore_type_conflicts => builder.append_null(),
35                _ => return Err(tape.error(*p, "boolean")),
36            }
37        }
38
39        Ok(Arc::new(builder.finish()))
40    }
41}