Repository navigation
feat(encoding): cap the dictionary size a page may be encoded with - #9939
Conversation
3d2bbd0 to
5979028
Compare
There was a problem hiding this comment.
✅ Gate recommendation: approve.
The metadata-only cap still bounds eager loading of large variable-width dictionaries and preserves the existing structural fallback and file format. Fixed-width dictionary behavior is preserved. The focused checks found no blocker.
Please mark this PR with the breaking-change label.
|
The default cap changes which pages get a dictionary (dictionaries above 8 MiB of values are no longer written), so this should carry the |
wjones127
left a comment
There was a problem hiding this comment.
This makes sense to me. Might give @westonpace a chance to comment if he wants, as he's more familiar with the encoding logic. The 200MB page you describe is interesting; I do wonder if there's other things that will be sub-optimal for the encoder one those.
| .metadata | ||
| .get(DICT_MAX_SIZE_META_KEY) | ||
| .and_then(|val| val.parse().ok()) | ||
| .unwrap_or(DEFAULT_DICT_MAX_SIZE_BYTES); |
There was a problem hiding this comment.
question (non-blocking): Why silently fall back to the 8 MiB default when dict-max-size fails to parse? If this matches how dict-divisor and dict-size-ratio are parsed that's fine. Otherwise a typo like 8MiB quietly keeps the default, so a log::warn! may be worth it.
There was a problem hiding this comment.
It matches dict-divisor and dict-size-ratio: both parse with .parse().ok() and fall back silently, so I kept the three options consistent. Also rebased onto main, where the encoder moved to primitive/structural_encoder.rs.
| return None; | ||
| } | ||
| dictionary_buffer.extend_from_slice(key); | ||
| if dictionary_buffer.len() > max_dict_bytes { |
There was a problem hiding this comment.
praise: Checking the cap right after the append keeps it exact and aborts early, so we never build a huge dictionary just to discard it. Good that it's in the generic fn and covers both the u32 and u64 offset paths.
The dictionary heuristic only looked at cardinality and at the encoded size relative to the raw page, so a page of large values (images, documents) with enough duplicates was dictionary encoded with a dictionary of tens or hundreds of megabytes. The reader loads a page's dictionary whole the first time the page is touched, which turns every point read into a read of the whole dictionary: on a table of JPEG thumbnails about 1% of pages were such pages, with 6-131 MB dictionaries, and a single-row take hit 95 MB over 6 requests. Add a byte budget for variable-width dictionary values, `lance-encoding:dict-max-size` (field metadata), 8 MiB by default, enforced exactly while the dictionary is built so that pages over the budget fall back to the regular structural encoding.
5979028 to
9f60001
Compare
…ze-cap # Conflicts: # rust/lance-encoding/src/encodings/logical/primitive/structural_encoder.rs
…efore appending A present but unparsable lance-encoding:dict-max-size fell back to the default silently; it now panics like dict-size-ratio does. The byte cap is compared before the value is appended, so an aborted attempt never grows the discarded buffer past the cap.
Problem
should_dictionary_encodegates dictionary encoding on cardinality (num_values / dict-divisor, capped at 100k) and on the encoded size relative to the raw page (dict-size-ratio). Nothing bounds the dictionary itself, so a page of large values with enough duplicates gets a dictionary of tens or hundreds of megabytes.The reader loads a page's dictionary whole on first touch (
MiniBlockScheduler::init_layoutadds the dictionary buffer to the page initialization), so every point read into such a page reads the whole dictionary. Observed on a 2.1 table of JPEG thumbnails (large_binary, ~50 KB per value, 4096 rows per page): in 64 sampled fragments 48 of 4,479 pages (about 1% of pages and of rows) were dictionary pages with 137–2019 distinct values and 6–131 MB dictionaries (mean 54 MB). A single-row take that landed in one of them read 95 MB over 6 requests; a batch of 128 rows per fragment read 157 MiB for 27 MiB of values. The decoded dictionary is then cached in the metadata cache and evicts other files' metadata.Change
DictEncodingBudgetcarries amax_dict_bytesbudget from field metadatalance-encoding:dict-max-size, 8 MiB by default.dict::dictionary_encodeenforces it exactly while building a variable-width dictionary; a page over the budget falls back to the regular structural encoding, as it already does for the entry and encoded-size budgets. Fixed-width dictionaries (at most 100k entries of 8 or 16 bytes) cannot reach the default and are left alone.8 MiB keeps the dictionaries that help scans (categorical strings, ids: at most 100k entries) untouched and bounds the first-touch read of a dictionary page to a few milliseconds in-region. The docs row for the new key in
docs/src/format/file/encoding.mdis left for a follow-up since that directory is gated by the format vote.Tests
dict::tests::test_dictionary_encode_respects_dict_byte_limit: a 25,600-byte dictionary is refused under a 16 KiB budget and built under a 25,600-byte one.test_encode_skips_dictionary_above_max_dict_size: the same array is dictionary encoded under the default budget and written without a dictionary when the field setslance-encoding:dict-max-sizebelow the dictionary size.cargo fmt --all,cargo clippy --all --tests --benches -- -D warningsclean.