-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add view buffer for parquet reader #5970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f32aabc
implement sort for view types
XiangpengHao 8f1c887
add bench for binary/binary view
XiangpengHao 7a7a246
Merge branch 'apache:master' into master
XiangpengHao 6b3f1b9
Merge remote-tracking branch 'origin/master' into string-view-bench
XiangpengHao 45d7752
add view buffer, prepare for byte_view_array reader
XiangpengHao 3e243ad
make clippy happy
XiangpengHao 1b45c91
Merge remote-tracking branch 'apache/master' into parquet-string-view
alamb 25ad3c2
reuse make_view_unchecked
XiangpengHao 002b73d
Update parquet/src/arrow/buffer/view_buffer.rs
XiangpengHao 7e8ff6a
update
XiangpengHao 5846ff0
rename and inline
XiangpengHao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,3 +20,4 @@ | |
| pub mod bit_util; | ||
| pub mod dictionary_buffer; | ||
| pub mod offset_buffer; | ||
| pub mod view_buffer; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| // 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::arrow::record_reader::buffer::ValuesBuffer; | ||
| use arrow_array::{builder::make_view, make_array, ArrayRef}; | ||
| use arrow_buffer::Buffer; | ||
| use arrow_data::ArrayDataBuilder; | ||
| use arrow_schema::DataType as ArrowType; | ||
|
|
||
| /// A buffer of view type byte arrays that can be converted into | ||
| /// `GenericByteViewArray` | ||
| /// | ||
| /// Note this does not reuse `GenericByteViewBuilder` due to the need to call `pad_nulls` | ||
| /// and reuse the existing logic for Vec in the parquet crate | ||
| #[derive(Debug, Default)] | ||
alamb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pub struct ViewBuffer { | ||
| pub views: Vec<u128>, | ||
| pub buffers: Vec<Buffer>, | ||
| } | ||
|
|
||
| impl ViewBuffer { | ||
| #[allow(unused)] | ||
| pub fn append_block(&mut self, block: Buffer) -> u32 { | ||
| let block_id = self.buffers.len() as u32; | ||
| self.buffers.push(block); | ||
| block_id | ||
| } | ||
|
|
||
| /// # Safety | ||
| /// This method is only safe when: | ||
| /// - `block` is a valid index, i.e., the return value of `append_block` | ||
| /// - `offset` and `offset + len` are valid indices into the buffer | ||
| /// - The `(offset, offset + len)` is valid value for the native type. | ||
| #[allow(unused)] | ||
| pub unsafe fn append_view_unchecked(&mut self, block: u32, offset: u32, len: u32) { | ||
| let b = self.buffers.get_unchecked(block as usize); | ||
| let end = offset.saturating_add(len); | ||
| let b = b.get_unchecked(offset as usize..end as usize); | ||
|
|
||
| let view = make_view(b, block, offset); | ||
|
|
||
| self.views.push(view); | ||
| } | ||
|
|
||
| /// Converts this into an [`ArrayRef`] with the provided `data_type` and `null_buffer` | ||
| #[allow(unused)] | ||
| pub fn into_array(self, null_buffer: Option<Buffer>, data_type: &ArrowType) -> ArrayRef { | ||
| let len = self.views.len(); | ||
| let views = Buffer::from_vec(self.views); | ||
| match data_type { | ||
| ArrowType::Utf8View => { | ||
| let builder = ArrayDataBuilder::new(ArrowType::Utf8View) | ||
| .len(len) | ||
| .add_buffer(views) | ||
| .add_buffers(self.buffers) | ||
| .null_bit_buffer(null_buffer); | ||
| // We have checked that the data is utf8 when building the buffer, so it is safe | ||
| let array = unsafe { builder.build_unchecked() }; | ||
| make_array(array) | ||
| } | ||
| ArrowType::BinaryView => { | ||
| let builder = ArrayDataBuilder::new(ArrowType::BinaryView) | ||
| .len(len) | ||
| .add_buffer(views) | ||
| .add_buffers(self.buffers) | ||
| .null_bit_buffer(null_buffer); | ||
| let array = unsafe { builder.build_unchecked() }; | ||
| make_array(array) | ||
| } | ||
| _ => panic!("Unsupported data type: {:?}", data_type), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ValuesBuffer for ViewBuffer { | ||
| fn pad_nulls( | ||
| &mut self, | ||
| read_offset: usize, | ||
| values_read: usize, | ||
| levels_read: usize, | ||
| valid_mask: &[u8], | ||
| ) { | ||
| self.views | ||
| .pad_nulls(read_offset, values_read, levels_read, valid_mask); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
|
|
||
| use arrow_array::Array; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_view_buffer_empty() { | ||
| let buffer = ViewBuffer::default(); | ||
| let array = buffer.into_array(None, &ArrowType::Utf8View); | ||
| let strings = array | ||
| .as_any() | ||
| .downcast_ref::<arrow::array::StringViewArray>() | ||
| .unwrap(); | ||
| assert_eq!(strings.len(), 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_view_buffer_append_view() { | ||
| let mut buffer = ViewBuffer::default(); | ||
| let string_buffer = Buffer::from(&b"0123456789long string to test string view"[..]); | ||
| let block_id = buffer.append_block(string_buffer); | ||
|
|
||
| unsafe { | ||
| buffer.append_view_unchecked(block_id, 0, 1); | ||
| buffer.append_view_unchecked(block_id, 1, 9); | ||
| buffer.append_view_unchecked(block_id, 10, 31); | ||
| } | ||
|
|
||
| let array = buffer.into_array(None, &ArrowType::Utf8View); | ||
| let string_array = array | ||
| .as_any() | ||
| .downcast_ref::<arrow::array::StringViewArray>() | ||
| .unwrap(); | ||
| assert_eq!( | ||
| string_array.iter().collect::<Vec<_>>(), | ||
| vec![ | ||
| Some("0"), | ||
| Some("123456789"), | ||
| Some("long string to test string view"), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_view_buffer_pad_null() { | ||
| let mut buffer = ViewBuffer::default(); | ||
| let string_buffer = Buffer::from(&b"0123456789long string to test string view"[..]); | ||
| let block_id = buffer.append_block(string_buffer); | ||
|
|
||
| unsafe { | ||
| buffer.append_view_unchecked(block_id, 0, 1); | ||
| buffer.append_view_unchecked(block_id, 1, 9); | ||
| buffer.append_view_unchecked(block_id, 10, 31); | ||
| } | ||
|
|
||
| let valid = [true, false, false, true, false, false, true]; | ||
| let valid_mask = Buffer::from_iter(valid.iter().copied()); | ||
|
|
||
| buffer.pad_nulls(1, 2, valid.len() - 1, valid_mask.as_slice()); | ||
|
|
||
| let array = buffer.into_array(Some(valid_mask), &ArrowType::Utf8View); | ||
| let strings = array | ||
| .as_any() | ||
| .downcast_ref::<arrow::array::StringViewArray>() | ||
| .unwrap(); | ||
|
|
||
| assert_eq!( | ||
| strings.iter().collect::<Vec<_>>(), | ||
| vec![ | ||
| Some("0"), | ||
| None, | ||
| None, | ||
| Some("123456789"), | ||
| None, | ||
| None, | ||
| Some("long string to test string view"), | ||
| ] | ||
| ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.