Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions parquet/src/arrow/array_reader/byte_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::arrow::buffer::offset_buffer::OffsetBuffer;
use crate::arrow::decoder::{DeltaByteArrayDecoder, DictIndexDecoder};
use crate::arrow::record_reader::GenericRecordReader;
use crate::arrow::schema::parquet_to_arrow_field;
use crate::basic::{ConvertedType, Encoding};
use crate::basic::{ConvertedType, Encoding, Type};
use crate::column::page::PageIterator;
use crate::column::reader::decoder::ColumnValueDecoder;
use crate::column::reader::decoder::{ColumnValueDecoder, check_dictionary_page_values};
use crate::data_type::Int32Type;
use crate::encodings::decoding::{Decoder, DeltaBitPackDecoder};
use crate::errors::{ParquetError, Result};
Expand Down Expand Up @@ -211,6 +211,8 @@ impl<I: OffsetSizeTrait> ColumnValueDecoder for ByteArrayColumnValueDecoder<I> {
));
}

check_dictionary_page_values(Type::BYTE_ARRAY, 0, buf.len(), num_values as usize)?;

let mut buffer = OffsetBuffer::with_capacity(0);
let mut decoder = ByteArrayDecoderPlain::new(
buf,
Expand Down Expand Up @@ -754,7 +756,7 @@ mod tests {
let mut decoder = ByteArrayColumnValueDecoder::new(&column_desc);

decoder
.set_dict(encoded_dictionary, 4, Encoding::RLE_DICTIONARY, false)
.set_dict(encoded_dictionary, 0, Encoding::RLE_DICTIONARY, false)
.unwrap();

// test nulls read
Expand Down Expand Up @@ -931,4 +933,19 @@ mod tests {
assert_eq!(decoder.skip(1).unwrap(), 1);
assert_eq!(decoder.read(&mut output, 1).unwrap(), 0);
}

#[test]
fn test_dictionary_page_declaring_too_many_values() {
let column_desc = utf8_column();
let mut decoder = ByteArrayColumnValueDecoder::<i32>::new(&column_desc);
let err = decoder
.set_dict(
Bytes::from_static(b"12345678"),
i32::MAX as u32,
Encoding::PLAIN,
false,
)
.unwrap_err();
assert!(err.to_string().contains("holds at most"), "{err}");
}
}
22 changes: 20 additions & 2 deletions parquet/src/arrow/array_reader/byte_array_dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ use crate::arrow::record_reader::GenericRecordReader;
use crate::arrow::schema::parquet_to_arrow_field;
use crate::basic::{ConvertedType, Encoding, Type};
use crate::column::page::PageIterator;
use crate::column::reader::decoder::{ColumnValueDecoder, normalize_fixed_len_byte_array_payload};
use crate::column::reader::decoder::{
ColumnValueDecoder, check_dictionary_page_values, normalize_fixed_len_byte_array_payload,
};
use crate::encodings::rle::{MAX_RLE_DICTIONARY_BIT_WIDTH, RleDecoder};
use crate::errors::{ParquetError, Result};
use crate::schema::types::ColumnDescPtr;
Expand Down Expand Up @@ -398,6 +400,7 @@ where
));
}
} else {
check_dictionary_page_values(Type::BYTE_ARRAY, 0, buf.len(), len)?;
ByteArrayDecoderPlain::new(buf, len, Some(len), self.validate_utf8)
.read(&mut buffer, usize::MAX)?;
}
Expand Down Expand Up @@ -924,7 +927,7 @@ mod tests {
let mut decoder = DictionaryDecoder::new(&column_desc);

decoder
.set_dict(encoded_dictionary, 4, Encoding::PLAIN_DICTIONARY, false)
.set_dict(encoded_dictionary, 0, Encoding::PLAIN_DICTIONARY, false)
.unwrap();

for (encoding, page) in pages.clone() {
Expand Down Expand Up @@ -1008,4 +1011,19 @@ mod tests {
"Parquet error: Invalid or corrupted RLE value 255 exceeds bit width 1"
);
}

#[test]
fn test_dictionary_page_declaring_too_many_values() {
let column_desc = utf8_column();
let mut decoder = DictionaryDecoder::<i32, i32>::new(&column_desc);
let err = decoder
.set_dict(
Bytes::from_static(b"12345678"),
i32::MAX as u32,
Encoding::PLAIN,
false,
)
.unwrap_err();
assert!(err.to_string().contains("holds at most"), "{err}");
}
}
21 changes: 19 additions & 2 deletions parquet/src/arrow/array_reader/byte_view_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::arrow::buffer::view_buffer::ViewBuffer;
use crate::arrow::decoder::{DeltaByteArrayDecoder, DictIndexDecoder};
use crate::arrow::record_reader::GenericRecordReader;
use crate::arrow::schema::parquet_to_arrow_field;
use crate::basic::{ConvertedType, Encoding};
use crate::basic::{ConvertedType, Encoding, Type};
use crate::column::page::PageIterator;
use crate::column::reader::decoder::ColumnValueDecoder;
use crate::column::reader::decoder::{ColumnValueDecoder, check_dictionary_page_values};
use crate::data_type::Int32Type;
use crate::encodings::decoding::{Decoder, DeltaBitPackDecoder};
use crate::errors::{ParquetError, Result};
Expand Down Expand Up @@ -171,6 +171,8 @@ impl ColumnValueDecoder for ByteViewArrayColumnValueDecoder {
));
}

check_dictionary_page_values(Type::BYTE_ARRAY, 0, buf.len(), num_values as usize)?;

let num_values = num_values as usize;
let mut buffer = ViewBuffer::with_capacity(num_values);
let mut decoder =
Expand Down Expand Up @@ -878,4 +880,19 @@ mod tests {
assert_eq!(decoder.offset, 0);
assert_eq!(decoder.max_remaining_values, 2);
}

#[test]
fn test_dictionary_page_declaring_too_many_values() {
let column_desc = utf8_column();
let mut decoder = ByteViewArrayColumnValueDecoder::new(&column_desc);
let err = decoder
.set_dict(
Bytes::from_static(b"12345678"),
i32::MAX as u32,
Encoding::PLAIN,
false,
)
.unwrap_err();
assert!(err.to_string().contains("holds at most"), "{err}");
}
}
17 changes: 17 additions & 0 deletions parquet/src/arrow/arrow_reader/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,20 @@ fn generate_file_with_row_numbers(rng: &mut impl Rng) -> (Bytes, ParquetMetaData

(Bytes::from(buf), metadata)
}

#[test]
fn test_dictionary_page_declaring_too_many_values() {
// 120 bytes, one INT64 row. Its 8-byte dictionary page declares i32::MAX values.
// https://github.com/apache/arrow-rs/issues/12471
let hex = "504152311504151015104c15feffffff0f15001200002a000000000000001500150415042c1502151015061506000000021502192c4806736368656d6115020015042500180163001602191c191c26081c150419350006101918016315001602165a165a263c26080000165a160200003f00000050415231";
let bytes: Vec<u8> = (0..hex.len())
.step_by(2)
.map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap())
.collect();
let reader = ParquetRecordBatchReaderBuilder::try_new(Bytes::from(bytes))
.unwrap()
.build()
.unwrap();
let err = reader.collect::<Result<Vec<_>, _>>().unwrap_err();
assert!(err.to_string().contains("dictionary page"), "{err}");
}
55 changes: 55 additions & 0 deletions parquet/src/column/reader/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,30 @@ pub(crate) fn normalize_fixed_len_byte_array_payload(
)
}

/// Reject a dictionary page whose declared `num_values` exceeds what its
/// decompressed bytes can hold at the smallest plain width, before any
/// allocation is sized from the count.
pub(crate) fn check_dictionary_page_values(
physical_type: Type,
type_length: i32,
num_bytes: usize,
num_values: usize,
) -> Result<()> {
let max_values = match physical_type {
Type::BOOLEAN => num_bytes.saturating_mul(8),
Type::INT32 | Type::FLOAT | Type::BYTE_ARRAY => num_bytes / 4,
Type::INT64 | Type::DOUBLE => num_bytes / 8,
Type::INT96 => num_bytes / 12,
Type::FIXED_LEN_BYTE_ARRAY => num_bytes / type_length.max(1) as usize,
};
if num_values > max_values {
return Err(general_err!(
"dictionary page declares {num_values} values but holds at most {max_values}"
));
}
Ok(())
}

/// Prepare a complete FLBA data-page value section after extracting the levels.
/// PLAIN requires exact size validation; other compliant encodings are unchanged.
pub(crate) fn normalize_fixed_len_byte_array_data(
Expand Down Expand Up @@ -334,6 +358,12 @@ impl<T: DataType> ColumnValueDecoder for ColumnValueDecoderImpl<T> {
} else {
buf
};
check_dictionary_page_values(
self.descr.physical_type(),
self.descr.type_length(),
buf.len(),
num_values as usize,
)?;
let mut dictionary = PlainDecoder::<T>::new(self.descr.type_length());
dictionary.set_data(buf, num_values as usize)?;

Expand Down Expand Up @@ -926,4 +956,29 @@ mod tests {
}
}
}

#[test]
fn dictionary_page_value_counts() {
// 8 bytes hold one INT64 value, two INT32 values, 64 BOOLEAN values.
for (physical_type, type_length, num_values) in [
(Type::INT64, 0, 2),
(Type::INT32, 0, 3),
(Type::BOOLEAN, 0, 65),
(Type::BYTE_ARRAY, 0, 3),
(Type::FIXED_LEN_BYTE_ARRAY, 4, 3),
] {
let err = check_dictionary_page_values(physical_type, type_length, 8, num_values)
.unwrap_err();
assert!(err.to_string().contains("holds at most"), "{err}");
}
for (physical_type, type_length, num_values) in [
(Type::INT64, 0, 1),
(Type::INT32, 0, 2),
(Type::BOOLEAN, 0, 64),
(Type::BYTE_ARRAY, 0, 2),
(Type::FIXED_LEN_BYTE_ARRAY, 4, 2),
] {
check_dictionary_page_values(physical_type, type_length, 8, num_values).unwrap();
}
}
}