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.
1718use 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;
2526/// 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}
3334struct EmptyArrayReader {
35 data_type: ArrowType,
36 remaining_rows: usize,
37 need_consume_records: usize,
38}
3940impl EmptyArrayReader {
41pub fn new(row_count: usize) -> Self {
42Self {
43 data_type: ArrowType::Struct(Fields::empty()),
44 remaining_rows: row_count,
45 need_consume_records: 0,
46 }
47 }
48}
4950impl ArrayReader for EmptyArrayReader {
51fn as_any(&self) -> &dyn Any {
52self
53}
5455fn get_data_type(&self) -> &ArrowType {
56&self.data_type
57 }
5859fn read_records(&mut self, batch_size: usize) -> Result<usize> {
60let len = self.remaining_rows.min(batch_size);
61self.remaining_rows -= len;
62self.need_consume_records += len;
63Ok(len)
64 }
6566fn consume_batch(&mut self) -> Result<ArrayRef> {
67let data = ArrayDataBuilder::new(self.data_type.clone())
68 .len(self.need_consume_records)
69 .build()
70 .unwrap();
71self.need_consume_records = 0;
72Ok(Arc::new(StructArray::from(data)))
73 }
7475fn skip_records(&mut self, num_records: usize) -> Result<usize> {
76let skipped = self.remaining_rows.min(num_records);
77self.remaining_rows -= skipped;
78Ok(skipped)
79 }
8081fn get_def_levels(&self) -> Option<&[i16]> {
82None
83}
8485fn get_rep_levels(&self) -> Option<&[i16]> {
86None
87}
88}