Skip to content
Merged
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
Binary file not shown.
24 changes: 21 additions & 3 deletions rust/src/models/decoders/parquet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! used internally by higher-level Table or Array readers.

use crate::error::IoError;
use minarrow::Vec64;
use minarrow::{Vec64, vec64};
use std::io::Read;

// ************************ Primitive decoders ************************
Expand Down Expand Up @@ -180,6 +180,11 @@ pub fn decode_dictionary_indices_rle(buf: &[u8], len: usize) -> Result<Vec64<u32
return Err(IoError::Format("empty dictionary index stream".to_owned()));
}
let bit_width = buf[0];
// A bit width of 0 encodes a dictionary with one entry, and every index
// is 0.
if bit_width == 0 {
return Ok(vec64![0; len]);
}
if !(1..=32).contains(&bit_width) {
return Err(IoError::Format(format!("invalid bit width {bit_width}")));
}
Expand Down Expand Up @@ -387,14 +392,27 @@ mod tests {
decode_dictionary_indices_rle(&[], 1),
Err(IoError::Format(_))
));
// bad bit_width
let buf = &[0u8];
// A bit width above 32 is outside the format.
let buf = &[33u8, 6, 0];
assert!(matches!(
decode_dictionary_indices_rle(buf, 1),
Err(IoError::Format(_))
));
}

#[test]
fn test_decode_dictionary_rle_zero_bit_width() {
// bit_width=0 for a one-entry dictionary, header=(3<<1)=6 with a
// zero-byte value: every index is 0.
let buf = &[0u8, 6];
let out = decode_dictionary_indices_rle(buf, 3).unwrap();
assert_eq!(out.0, vec![0, 0, 0]);

// The bit width byte on its own decodes to index 0 for every value.
let out = decode_dictionary_indices_rle(&[0u8], 2).unwrap();
assert_eq!(out.0, vec![0, 0]);
}

#[test]
fn test_decode_dictionary_roundtrip_via_encode() {
let indices = vec![0, 1, 1, 2, 2, 2, 2, 2, 3, 3];
Expand Down
23 changes: 22 additions & 1 deletion rust/tests/pyarrow_parquet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
//! with `generate_pyarrow_parquet_files.py`. They cover the page layouts
//! parquet-cpp writes by default (Snappy, dictionary encoding, DataPageV1,
//! multiple row groups) plus the DataPageV2 layouts with and without
//! dictionaries.
//! dictionaries. One further fixture, written by Polars, covers dictionaries
//! with one entry, which Polars encodes with an index bit width of 0.

#[cfg(feature = "parquet")]
mod pyarrow_parquet_tests {
Expand Down Expand Up @@ -330,4 +331,24 @@ mod pyarrow_parquet_tests {
[Some(85.5), Some(92.0), Some(78.3), Some(95.1), Some(88.7)]
);
}

/// Dictionary-encoded columns with one dictionary entry, written by
/// Polars, which stores their indices with a bit width of 0. The fixture
/// was produced from `rust/` with Polars 1.44.2:
///
/// ```text
/// python -c "import polars as pl; pl.DataFrame({'zeros': pl.Series([0, 0, 0, 0, 0], dtype=pl.Int32), 'label': ['a', 'a', 'a', 'a', 'a'], 'sparse': pl.Series([7, None, 7, None, 7], dtype=pl.Int64)}).write_parquet('pyarrow-roundtrip/polars_single_entry_dictionary.parquet', compression='uncompressed')"
/// ```
#[test]
fn reads_polars_single_entry_dictionary_columns() {
let table =
load_parquet_table(open("polars_single_entry_dictionary.parquet")).expect("read");
assert_eq!(table.n_rows, 5);
assert_eq!(int32_values(&table, "zeros"), [Some(0); 5]);
assert_eq!(string_values(&table, "label"), [(); 5].map(|_| Some("a".to_owned())));
assert_eq!(
int64_values(&table, "sparse"),
[Some(7), None, Some(7), None, Some(7)]
);
}
}
Loading