Skip to content
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

fix: Try an error when using a wrongly versioned categorical in row encoding #20389

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
use hashbrown::hash_table::Entry;
use hashbrown::HashTable;
use once_cell::sync::Lazy;
use polars_error::{polars_ensure, PolarsResult};
use polars_utils::aliases::PlRandomState;
use polars_utils::pl_str::PlSmallStr;

Expand Down Expand Up @@ -190,8 +191,9 @@ impl SCacheInner {
}

#[inline]
pub(crate) fn get_current_payloads(&self) -> &[PlSmallStr] {
&self.payloads
pub(crate) fn get_current_payloads(&self, uuid: u32) -> PolarsResult<&[PlSmallStr]> {
polars_ensure!(self.uuid == uuid, ComputeError: "trying to perform operations with values from different global string cache version");
Ok(&self.payloads)
}
}

Expand Down
62 changes: 33 additions & 29 deletions crates/polars-core/src/chunked_array/ops/row_encode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use arrow::compute::utils::combine_validities_and_many;
use arrow::legacy::trusted_len::TrustedLenPush;
use polars_row::{
convert_columns, RowEncodingCategoricalContext, RowEncodingContext, RowEncodingOptions,
RowsEncoded,
Expand Down Expand Up @@ -70,7 +71,7 @@ pub fn encode_rows_vertical_par_unordered_broadcast_nulls(
))
}

pub fn get_row_encoding_dictionary(dtype: &DataType) -> Option<RowEncodingContext> {
pub fn get_row_encoding_dictionary(dtype: &DataType) -> PolarsResult<Option<RowEncodingContext>> {
match dtype {
DataType::Boolean
| DataType::UInt8
Expand All @@ -91,7 +92,7 @@ pub fn get_row_encoding_dictionary(dtype: &DataType) -> Option<RowEncodingContex
| DataType::Time
| DataType::Date
| DataType::Datetime(_, _)
| DataType::Duration(_) => None,
| DataType::Duration(_) => Ok(None),

DataType::Unknown(_) => panic!("Unsupported in row encoding"),

Expand All @@ -100,7 +101,7 @@ pub fn get_row_encoding_dictionary(dtype: &DataType) -> Option<RowEncodingContex

#[cfg(feature = "dtype-decimal")]
DataType::Decimal(precision, _) => {
Some(RowEncodingContext::Decimal(precision.unwrap_or(38)))
Ok(Some(RowEncodingContext::Decimal(precision.unwrap_or(38))))
},

#[cfg(feature = "dtype-array")]
Expand All @@ -111,24 +112,25 @@ pub fn get_row_encoding_dictionary(dtype: &DataType) -> Option<RowEncodingContex
let revmap = revmap.as_ref().unwrap();

let (num_known_categories, lexical_sort_idxs) = match revmap.as_ref() {
RevMapping::Global(map, _, _) => {
RevMapping::Global(map, _, uuid) => {
let num_known_categories = map.keys().max().copied().map_or(0, |m| m + 1);

// @TODO: This should probably be cached.
let lexical_sort_idxs =
matches!(ordering, CategoricalOrdering::Lexical).then(|| {
let read_map = crate::STRING_CACHE.read_map();
let payloads = read_map.get_current_payloads();
assert!(payloads.len() >= num_known_categories as usize);

let mut idxs = (0..num_known_categories).collect::<Vec<u32>>();
idxs.sort_by_key(|&k| payloads[k as usize].as_str());
let mut sort_idxs = vec![0; num_known_categories as usize];
for (i, idx) in idxs.into_iter().enumerate_u32() {
sort_idxs[idx as usize] = i;
}
sort_idxs
});
let lexical_sort_idxs = if matches!(ordering, CategoricalOrdering::Lexical) {
let read_map = crate::STRING_CACHE.read_map();
let payloads = read_map.get_current_payloads(*uuid)?;
assert!(payloads.len() >= num_known_categories as usize);

let mut idxs = (0..num_known_categories).collect::<Vec<u32>>();
idxs.sort_by_key(|&k| payloads[k as usize].as_str());
let mut sort_idxs = vec![0; num_known_categories as usize];
for (i, idx) in idxs.into_iter().enumerate_u32() {
sort_idxs[idx as usize] = i;
}
Some(sort_idxs)
} else {
None
};

(num_known_categories, lexical_sort_idxs)
},
Expand Down Expand Up @@ -157,14 +159,14 @@ pub fn get_row_encoding_dictionary(dtype: &DataType) -> Option<RowEncodingContex
is_enum: matches!(dtype, DataType::Enum(_, _)),
lexical_sort_idxs,
};
Some(RowEncodingContext::Categorical(ctx))
Ok(Some(RowEncodingContext::Categorical(ctx)))
},
#[cfg(feature = "dtype-struct")]
DataType::Struct(fs) => {
let mut out = Vec::new();

for (i, f) in fs.iter().enumerate() {
if let Some(dict) = get_row_encoding_dictionary(f.dtype()) {
if let Some(dict) = get_row_encoding_dictionary(f.dtype())? {
out.reserve(fs.len());
out.extend(std::iter::repeat_n(None, i));
out.push(Some(dict));
Expand All @@ -173,16 +175,18 @@ pub fn get_row_encoding_dictionary(dtype: &DataType) -> Option<RowEncodingContex
}

if out.is_empty() {
return None;
return Ok(None);
}

out.extend(
fs[out.len()..]
.iter()
.map(|f| get_row_encoding_dictionary(f.dtype())),
);
unsafe {
out.try_extend_trusted_len_unchecked(
fs[out.len()..]
.iter()
.map(|f| get_row_encoding_dictionary(f.dtype())),
)
}?;

Some(RowEncodingContext::Struct(out))
Ok(Some(RowEncodingContext::Struct(out)))
},
}
}
Expand Down Expand Up @@ -210,7 +214,7 @@ pub fn _get_rows_encoded_unordered(by: &[Column]) -> PolarsResult<RowsEncoded> {
let by = by.as_materialized_series();
let arr = by.to_physical_repr().rechunk().chunks()[0].to_boxed();
let opt = RowEncodingOptions::new_unsorted();
let dict = get_row_encoding_dictionary(by.dtype());
let dict = get_row_encoding_dictionary(by.dtype())?;

cols.push(arr);
opts.push(opt);
Expand Down Expand Up @@ -241,7 +245,7 @@ pub fn _get_rows_encoded(
let by = by.as_materialized_series();
let arr = by.to_physical_repr().rechunk().chunks()[0].to_boxed();
let opt = RowEncodingOptions::new_sorted(*desc, *null_last);
let dict = get_row_encoding_dictionary(by.dtype());
let dict = get_row_encoding_dictionary(by.dtype())?;

cols.push(arr);
opts.push(opt);
Expand Down
3 changes: 2 additions & 1 deletion crates/polars-expr/src/groups/row_encoded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ impl RowEncodedHashGrouper {
let dicts = self
.key_schema
.iter()
.map(|(_, dt)| get_row_encoding_dictionary(dt))
// @TODO: Get this unwrap out of here. That is a bit difficult to do.
.map(|(_, dt)| get_row_encoding_dictionary(dt).unwrap())
.collect::<Vec<_>>();
let fields = vec![RowEncodingOptions::new_unsorted(); key_dtypes.len()];
let key_columns =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Eval {
let mut dicts = Vec::with_capacity(self.key_columns_expr.len());
for phys_e in self.key_columns_expr.iter() {
let s = phys_e.evaluate(chunk, &context.execution_state)?;
dicts.push(get_row_encoding_dictionary(s.dtype()));
dicts.push(get_row_encoding_dictionary(s.dtype())?);
let s = s.to_physical_repr().into_owned();
let s = prepare_key(&s, chunk);
keys_columns.push(s.to_arrow(0, CompatLevel::newest()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ impl<const FIXED: bool> AggHashTable<FIXED> {
.iter_values()
.take(self.num_keys)
.map(get_row_encoding_dictionary)
// @TODO: get this unwrap out of here.
.map(|v| v.unwrap())
.collect::<Vec<_>>();
let fields = vec![Default::default(); self.num_keys];
let key_columns =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl<K: ExtraPayload> GenericBuild<K> {
let s = phys_e.evaluate(chunk, &context.execution_state)?;
let arr = s.to_physical_repr().rechunk().array_ref(0).clone();
self.join_columns.push(arr);
dicts.push(get_row_encoding_dictionary(s.dtype()));
dicts.push(get_row_encoding_dictionary(s.dtype())?);
}
let rows_encoded = polars_row::convert_columns_no_order(
self.join_columns[0].len(), // @NOTE: does not work for ZFS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl RowValues {
names.push(s.name().to_string());
}
self.join_columns_material.push(s.array_ref(0).clone());
dicts.push(get_row_encoding_dictionary(s.dtype()));
dicts.push(get_row_encoding_dictionary(s.dtype())?);
}

// We determine the indices of the columns that have to be removed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl SortSinkMultiple {
let (_, dtype) = schema.get_at_index(*i).unwrap();
get_row_encoding_dictionary(dtype)
})
.collect::<Vec<_>>();
.collect::<PolarsResult<Vec<_>>>()?;

polars_ensure!(sort_idx.iter().collect::<PlHashSet::<_>>().len() == sort_idx.len(), ComputeError: "only supports sorting by unique columns");

Expand Down
3 changes: 2 additions & 1 deletion crates/polars-python/src/series/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ impl PySeries {
let dicts = dtypes
.iter()
.map(|(_, dtype)| get_row_encoding_dictionary(&dtype.0))
.collect::<Vec<_>>();
.collect::<PolarsResult<Vec<_>>>()
.map_err(PyPolarsErr::from)?;

// Get the BinaryOffset array.
let arr = self.series.rechunk();
Expand Down
11 changes: 11 additions & 0 deletions py-polars/tests/unit/test_row_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,14 @@ def test_null(
.to_series(),
s,
)


def test_wrong_version_categorical_20364() -> None:
with pl.StringCache():
s = pl.Series("s", ["a"], pl.Categorical(ordering="lexical"))

with pytest.raises(
pl.exceptions.ComputeError,
match="trying to perform operations with values from different global string cache version",
):
s.to_frame()._row_encode([(False, False, False)])
Loading