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
12 changes: 8 additions & 4 deletions vortex-array/src/builders/dict/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ impl<Code: UnsignedPType> BytesDictBuilder<Code> {
) {
HashTableEntry::Occupied(occupied) => Some(*occupied.get()),
HashTableEntry::Vacant(vacant) => {
if self.views.len() >= self.max_dict_len {
// Admit the first entry even if it exceeds the budget, so the caller makes progress.
let is_first_entry = self.views.is_empty();

if !is_first_entry && self.views.len() >= self.max_dict_len {
return None;
}

Expand All @@ -146,7 +149,7 @@ impl<Code: UnsignedPType> BytesDictBuilder<Code> {
size_of::<BinaryView>() + val.len()
};

if self.dict_bytes() + additional_bytes > self.max_dict_bytes {
if !is_first_entry && self.dict_bytes() + additional_bytes > self.max_dict_bytes {
return None;
}

Expand All @@ -171,8 +174,9 @@ impl<Code: UnsignedPType> BytesDictBuilder<Code> {
return Some(*code);
}

if self.views.len() >= self.max_dict_len
|| self.dict_bytes() + size_of::<BinaryView>() > self.max_dict_bytes
if !self.views.is_empty()
&& (self.views.len() >= self.max_dict_len
|| self.dict_bytes() + size_of::<BinaryView>() > self.max_dict_bytes)
{
return None;
}
Expand Down
5 changes: 3 additions & 2 deletions vortex-array/src/builders/dict/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ where
match self.lookup.entry(NativeValue(v)) {
Entry::Occupied(o) => Some(*o.get()),
Entry::Vacant(vac) => {
if self.values.len() >= self.max_dict_len {
// Admit the first entry even if it exceeds the budget, so the caller makes progress.
if !self.values.is_empty() && self.values.len() >= self.max_dict_len {
return None;
}
let next_code = Code::from_usize(self.values.len()).unwrap_or_else(|| {
Expand All @@ -121,7 +122,7 @@ where
return Some(*code);
}

if self.values.len() >= self.max_dict_len {
if !self.values.is_empty() && self.values.len() >= self.max_dict_len {
return None;
}

Expand Down
55 changes: 55 additions & 0 deletions vortex-layout/src/layouts/dict/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,12 @@ mod tests {
use vortex_array::dtype::Nullability::NonNullable;
use vortex_array::dtype::PType;
use vortex_array::session::ArraySession;
use vortex_error::VortexResult;
use vortex_session::VortexSession;

use super::DictChunkLabeler;
use super::DictStreamState;
use super::DictionaryChunk;
use super::DictionaryTransformer;
use super::dict_encode_stream;
use crate::sequence::SequenceId;
Expand Down Expand Up @@ -695,4 +699,55 @@ mod tests {
"codes stream should use U16 dtype for dictionaries with >255 entries"
);
}

fn encoded_row_count(chunks: &[DictionaryChunk]) -> usize {
chunks
.iter()
.map(|chunk| match chunk {
DictionaryChunk::Codes { codes, .. } => codes.len(),
DictionaryChunk::Values(_) => 0,
})
.sum()
}

/// A 49-byte value plus its 16-byte `BinaryView` exceeds the 64-byte budget of an empty
/// dictionary. Encoding must still make progress instead of looping forever.
#[test]
fn test_encode_value_larger_than_dictionary_budget_terminates() -> VortexResult<()> {
let mut state = DictStreamState {
encoder: None,
constraints: DictConstraints {
max_bytes: 64,
max_len: 100,
},
};

let value = "x".repeat(49);
let chunk = VarBinArray::from(vec![value.as_str()]).into_array();
let mut labeler = DictChunkLabeler::new(SequenceId::root().advance());
let encoded = state.encode(&mut labeler, chunk, &mut SESSION.create_execution_ctx())?;

assert_eq!(encoded_row_count(&encoded), 1);
Ok(())
}

#[test]
fn test_encode_splits_oversized_value_into_its_own_dictionary() -> VortexResult<()> {
let mut state = DictStreamState {
encoder: None,
constraints: DictConstraints {
max_bytes: 64,
max_len: 100,
},
};

let big = "x".repeat(49);
let chunk = VarBinArray::from(vec!["a", big.as_str(), "b"]).into_array();
let mut labeler = DictChunkLabeler::new(SequenceId::root().advance());
let encoded = state.encode(&mut labeler, chunk, &mut SESSION.create_execution_ctx())?;
let drained = state.drain_values(&mut labeler);

assert_eq!(encoded_row_count(&encoded) + encoded_row_count(&drained), 3);
Ok(())
}
}
Loading