parquet/arrow/array_reader/
empty_array.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::arrow::array_reader::ArrayReader;
19use crate::errors::Result;
20use arrow_array::{ArrayRef, StructArray};
21use arrow_data::ArrayDataBuilder;
22use arrow_schema::{DataType as ArrowType, Fields};
23use std::any::Any;
24use std::sync::Arc;
25
26/// Returns an [`ArrayReader`] that yields [`StructArray`] with no columns
27/// but with row counts that correspond to the amount of data in the file
28///
29/// This is useful for when projection eliminates all columns within a collection
30pub fn make_empty_array_reader(row_count: usize) -> Box<dyn ArrayReader> {
31    Box::new(EmptyArrayReader::new(row_count))
32}
33
34struct EmptyArrayReader {
35    data_type: ArrowType,
36    remaining_rows: usize,
37    need_consume_records: usize,
38}
39
40impl EmptyArrayReader {
41    pub fn new(row_count: usize) -> Self {
42        Self {
43            data_type: ArrowType::Struct(Fields::empty()),
44            remaining_rows: row_count,
45            need_consume_records: 0,
46        }
47    }
48}
49
50impl ArrayReader for EmptyArrayReader {
51    fn as_any(&self) -> &dyn Any {
52        self
53    }
54
55    fn get_data_type(&self) -> &ArrowType {
56        &self.data_type
57    }
58
59    fn read_records(&mut self, batch_size: usize) -> Result<usize> {
60        let len = self.remaining_rows.min(batch_size);
61        self.remaining_rows -= len;
62        self.need_consume_records += len;
63        Ok(len)
64    }
65
66    fn consume_batch(&mut self) -> Result<ArrayRef> {
67        let data = ArrayDataBuilder::new(self.data_type.clone())
68            .len(self.need_consume_records)
69            .build()
70            .unwrap();
71        self.need_consume_records = 0;
72        Ok(Arc::new(StructArray::from(data)))
73    }
74
75    fn skip_records(&mut self, num_records: usize) -> Result<usize> {
76        let skipped = self.remaining_rows.min(num_records);
77        self.remaining_rows -= skipped;
78        Ok(skipped)
79    }
80
81    fn get_def_levels(&self) -> Option<&[i16]> {
82        None
83    }
84
85    fn get_rep_levels(&self) -> Option<&[i16]> {
86        None
87    }
88}