Ted-Jiang commented on code in PR #1762: URL: https://github.com/apache/arrow-rs/pull/1762#discussion_r884837552
########## parquet/src/file/page_index/index_reader.rs: ########## @@ -0,0 +1,169 @@ +// 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 crate::basic::Type; +use crate::data_type::Int96; +use crate::errors::ParquetError; +use crate::file::metadata::ColumnChunkMetaData; +use crate::file::page_index::index::{BooleanIndex, ByteIndex, Index, NativeIndex}; +use crate::file::reader::ChunkReader; +use parquet_format::{ColumnIndex, OffsetIndex, PageLocation}; +use std::io::{Cursor, Read}; +use std::sync::Arc; +use thrift::protocol::TCompactInputProtocol; + +/// Read on row group's all columns indexes and change into [`Index`] +/// If not the format not available return an empty vector. +pub fn read_columns_indexes<R: ChunkReader>( + reader: &R, + chunks: &[ColumnChunkMetaData], +) -> Result<Vec<Arc<dyn Index>>, ParquetError> { + let (offset, lengths) = get_index_offset_and_lengths(chunks)?; + let length = lengths.iter().sum::<usize>(); + + //read all need data into buffer + let mut reader = reader.get_read(offset, reader.len() as usize)?; + let mut data = vec![0; length]; + reader.read_exact(&mut data)?; + + let mut start = 0; Review Comment: I think i use the `lengths ` approach ########## parquet/src/file/metadata.rs: ########## @@ -60,6 +63,21 @@ impl ParquetMetaData { ParquetMetaData { file_metadata, row_groups, + page_indexes: None, + offset_indexes: None, + } + } + + pub fn new_with_page_index( + metadata: ParquetMetaData, + page_indexes: Option<Vec<Arc<dyn Index>>>, + offset_indexes: Option<Vec<Vec<PageLocation>>>, Review Comment: You mean put them into one struct, is this approach can save memory? Or maybe like construct new `offsetIndex` in next Pr. ########## parquet/src/file/page_index/index_reader.rs: ########## @@ -0,0 +1,169 @@ +// 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 crate::basic::Type; +use crate::data_type::Int96; +use crate::errors::ParquetError; +use crate::file::metadata::ColumnChunkMetaData; +use crate::file::page_index::index::{BooleanIndex, ByteIndex, Index, NativeIndex}; +use crate::file::reader::ChunkReader; +use parquet_format::{ColumnIndex, OffsetIndex, PageLocation}; +use std::io::{Cursor, Read}; +use std::sync::Arc; +use thrift::protocol::TCompactInputProtocol; + +/// Read on row group's all columns indexes and change into [`Index`] +/// If not the format not available return an empty vector. +pub fn read_columns_indexes<R: ChunkReader>( + reader: &R, + chunks: &[ColumnChunkMetaData], +) -> Result<Vec<Arc<dyn Index>>, ParquetError> { + let (offset, lengths) = get_index_offset_and_lengths(chunks)?; + let length = lengths.iter().sum::<usize>(); + + //read all need data into buffer + let mut reader = reader.get_read(offset, reader.len() as usize)?; + let mut data = vec![0; length]; + reader.read_exact(&mut data)?; + + let mut start = 0; + let data = lengths.into_iter().map(|length| { + let r = &data[start..start + length]; + start += length; + r + }); + + chunks + .iter() + .zip(data) + .map(|(chunk, data)| { + let column_type = chunk.column_type(); + deserialize(data, column_type) + }) + .collect() +} + +/// Read on row group's all indexes and change into [`Index`] +/// If not the format not available return an empty vector. +pub fn read_pages_locations<R: ChunkReader>( + reader: &R, + chunks: &[ColumnChunkMetaData], +) -> Result<Vec<Vec<PageLocation>>, ParquetError> { Review Comment: Yes, you are right. But i haven't decide how to construct this new 'OffsetIndex'. I think it's easy to solve this in next PR with filter logical. 😊 ########## parquet/src/file/page_index/index_reader.rs: ########## @@ -0,0 +1,169 @@ +// 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 crate::basic::Type; +use crate::data_type::Int96; +use crate::errors::ParquetError; +use crate::file::metadata::ColumnChunkMetaData; +use crate::file::page_index::index::{BooleanIndex, ByteIndex, Index, NativeIndex}; +use crate::file::reader::ChunkReader; +use parquet_format::{ColumnIndex, OffsetIndex, PageLocation}; +use std::io::{Cursor, Read}; +use std::sync::Arc; +use thrift::protocol::TCompactInputProtocol; + +/// Read on row group's all columns indexes and change into [`Index`] +/// If not the format not available return an empty vector. +pub fn read_columns_indexes<R: ChunkReader>( + reader: &R, + chunks: &[ColumnChunkMetaData], +) -> Result<Vec<Arc<dyn Index>>, ParquetError> { + let (offset, lengths) = get_index_offset_and_lengths(chunks)?; + let length = lengths.iter().sum::<usize>(); + + //read all need data into buffer + let mut reader = reader.get_read(offset, reader.len() as usize)?; + let mut data = vec![0; length]; + reader.read_exact(&mut data)?; + + let mut start = 0; + let data = lengths.into_iter().map(|length| { + let r = &data[start..start + length]; + start += length; + r + }); + + chunks + .iter() + .zip(data) + .map(|(chunk, data)| { + let column_type = chunk.column_type(); + deserialize(data, column_type) + }) + .collect() +} + +/// Read on row group's all indexes and change into [`Index`] +/// If not the format not available return an empty vector. +pub fn read_pages_locations<R: ChunkReader>( + reader: &R, + chunks: &[ColumnChunkMetaData], +) -> Result<Vec<Vec<PageLocation>>, ParquetError> { + let (offset, lengths) = get_location_offset_and_lengths(chunks)?; + let total_length = lengths.iter().sum::<usize>(); + + //read all need data into buffer + let mut reader = reader.get_read(offset, reader.len() as usize)?; + let mut data = vec![0; total_length]; + reader.read_exact(&mut data)?; + + let mut d = Cursor::new(data); + let mut result = vec![]; + + for _ in 0..chunks.len() { + let mut prot = TCompactInputProtocol::new(&mut d); + let offset = OffsetIndex::read_from_in_protocol(&mut prot)?; + result.push(offset.page_locations); + } + Ok(result) +} + +fn get_index_offset_and_lengths( + chunks: &[ColumnChunkMetaData], +) -> Result<(u64, Vec<usize>), ParquetError> { + let first_col_metadata = if let Some(chunk) = chunks.first() { + chunk + } else { + return Ok((0, vec![])); + }; + + let offset: u64 = if let Some(offset) = first_col_metadata.column_index_offset() { + offset.try_into().unwrap() + } else { + return Ok((0, vec![])); + }; + + let lengths = chunks + .iter() + .map(|x| x.column_index_length()) + .map(|maybe_length| { + let index_length = maybe_length.ok_or_else(|| { + ParquetError::General( + "The column_index_length must exist if offset_index_offset exists" + .to_string(), + ) + })?; + + Ok(index_length.try_into().unwrap()) + }) + .collect::<Result<Vec<_>, ParquetError>>()?; + + Ok((offset, lengths)) +} + +fn get_location_offset_and_lengths( Review Comment: Nice catch! -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
