zhuqi-lucas commented on code in PR #7454: URL: https://github.com/apache/arrow-rs/pull/7454#discussion_r2084736260
########## parquet/src/arrow/async_reader/arrow_reader.rs: ########## @@ -0,0 +1,756 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::collections::hash_map::Entry; +use std::collections::HashMap; +use std::sync::{Mutex, MutexGuard}; +use std::{collections::VecDeque, sync::Arc}; + +use arrow_array::{cast::AsArray, Array, RecordBatch, RecordBatchReader}; +use arrow_array::{ArrayRef, BooleanArray}; +use arrow_buffer::BooleanBufferBuilder; +use arrow_schema::{ArrowError, DataType, Schema, SchemaRef}; +use arrow_select::filter::{filter, prep_null_mask_filter}; + +use crate::basic::PageType; +use crate::column::page::{Page, PageMetadata, PageReader}; +use crate::errors::ParquetError; +use crate::{ + arrow::{ + array_reader::ArrayReader, + arrow_reader::{RowFilter, RowSelection, RowSelector}, + }, + file::reader::{ChunkReader, SerializedPageReader}, +}; + +pub struct FilteredParquetRecordBatchReader { + batch_size: usize, + array_reader: Box<dyn ArrayReader>, + predicate_readers: Vec<Box<dyn ArrayReader>>, + schema: SchemaRef, + selection: Option<RowSelection>, + row_filter: Option<RowFilter>, +} + +fn read_selection( + reader: &mut dyn ArrayReader, + selection: &RowSelection, +) -> Result<ArrayRef, ParquetError> { + match selection { + RowSelection::Ranges(selectors) => { + for selector in selectors { + if selector.skip { + reader.skip_records(selector.row_count)?; + } else { + reader.read_records(selector.row_count)?; + } + } + reader.consume_batch() + } + + RowSelection::BitMap(bitmap) => { + let to_read = bitmap.len(); + reader.read_records(to_read)?; + let array = reader.consume_batch()?; + + let filtered_array = + filter(&array, bitmap).map_err(|e| ParquetError::General(e.to_string()))?; + + Ok(filtered_array) + } + } +} + +fn take_next_selection( + selection: &mut Option<RowSelection>, + to_select: usize, +) -> Option<RowSelection> { + let current = selection.as_ref()?.clone(); + + if let RowSelection::BitMap(bitmap) = current { + let take = bitmap.len().min(to_select); + let prefix = bitmap.slice(0, take); + let suffix_len = bitmap.len() - take; + let suffix = if suffix_len > 0 { + Some(bitmap.slice(take, suffix_len)) + } else { + None + }; + *selection = suffix.map(RowSelection::BitMap); + return Some(RowSelection::BitMap(prefix)); + } + + let RowSelection::Ranges(runs) = current else { + unreachable!() + }; + let mut queue: VecDeque<RowSelector> = runs.into(); + let mut taken = Vec::new(); + let mut count = 0; + + while let Some(run) = queue.pop_front() { + if run.skip { + taken.push(run); + continue; + } + let room = to_select.saturating_sub(count); + if room == 0 { + queue.push_front(run); + break; + } + if run.row_count <= room { + taken.push(run); + count += run.row_count; + } else { + taken.push(RowSelector::select(room)); + queue.push_front(RowSelector::select(run.row_count - room)); + break; + } + } + + *selection = if queue.is_empty() { + None + } else { + Some(RowSelection::Ranges(queue.into_iter().collect())) + }; + + Some(RowSelection::Ranges(taken)) +} + +impl FilteredParquetRecordBatchReader { + pub(crate) fn new( + batch_size: usize, + array_reader: Box<dyn ArrayReader>, + selection: Option<RowSelection>, + filter_readers: Vec<Box<dyn ArrayReader>>, + row_filter: Option<RowFilter>, + ) -> Self { + let schema = match array_reader.get_data_type() { + DataType::Struct(ref fields) => Schema::new(fields.clone()), + _ => unreachable!("Struct array reader's data type is not struct!"), + }; + + Self { + batch_size, + array_reader, + predicate_readers: filter_readers, + schema: Arc::new(schema), + selection, + row_filter, + } + } + + pub(crate) fn take_filter(&mut self) -> Option<RowFilter> { + self.row_filter.take() + } + + fn create_bitmap_from_ranges(&mut self, runs: &[RowSelector]) -> BooleanArray { Review Comment: Thank you @alamb , very good point, i was also thinking if we can return the bitmap from the predicate filter, we will have better performance. I will try to do this improvement also. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org