diff --git a/parquet/src/arrow/array_reader/byte_array.rs b/parquet/src/arrow/array_reader/byte_array.rs index 17c125e085f6..755a758744b4 100644 --- a/parquet/src/arrow/array_reader/byte_array.rs +++ b/parquet/src/arrow/array_reader/byte_array.rs @@ -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}; @@ -211,6 +211,8 @@ impl ColumnValueDecoder for ByteArrayColumnValueDecoder { )); } + 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, @@ -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 @@ -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::::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}"); + } } diff --git a/parquet/src/arrow/array_reader/byte_array_dictionary.rs b/parquet/src/arrow/array_reader/byte_array_dictionary.rs index 0db34d07ff9b..888d602d0b9b 100644 --- a/parquet/src/arrow/array_reader/byte_array_dictionary.rs +++ b/parquet/src/arrow/array_reader/byte_array_dictionary.rs @@ -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; @@ -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)?; } @@ -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() { @@ -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::::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}"); + } } diff --git a/parquet/src/arrow/array_reader/byte_view_array.rs b/parquet/src/arrow/array_reader/byte_view_array.rs index 6e0a0c3e920b..919bafb33088 100644 --- a/parquet/src/arrow/array_reader/byte_view_array.rs +++ b/parquet/src/arrow/array_reader/byte_view_array.rs @@ -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}; @@ -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 = @@ -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}"); + } } diff --git a/parquet/src/arrow/arrow_reader/tests/mod.rs b/parquet/src/arrow/arrow_reader/tests/mod.rs index 9504213814c4..ad58d8ae9830 100644 --- a/parquet/src/arrow/arrow_reader/tests/mod.rs +++ b/parquet/src/arrow/arrow_reader/tests/mod.rs @@ -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 = (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::, _>>().unwrap_err(); + assert!(err.to_string().contains("dictionary page"), "{err}"); +} diff --git a/parquet/src/column/reader/decoder.rs b/parquet/src/column/reader/decoder.rs index be8750dc7493..08b629a5ea36 100644 --- a/parquet/src/column/reader/decoder.rs +++ b/parquet/src/column/reader/decoder.rs @@ -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( @@ -334,6 +358,12 @@ impl ColumnValueDecoder for ColumnValueDecoderImpl { } else { buf }; + check_dictionary_page_values( + self.descr.physical_type(), + self.descr.type_length(), + buf.len(), + num_values as usize, + )?; let mut dictionary = PlainDecoder::::new(self.descr.type_length()); dictionary.set_data(buf, num_values as usize)?; @@ -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(); + } + } }