Repository navigation
feat(executor): restore a public state view bytes API (view_bytes, from_views) - #1542
Merged
Merged
Conversation
…om_views) A runtime that persists states and reads them back in place needs to write a state column's view bytes and to build a state column over stored bytes again. #1521 removed StateColumn::from_mapped, state_view_bytes and family_has_mapped_form because no workspace crate called them; this restores the capability as a smaller public API: - StateColumn::view_bytes(index): a state's view layout, borrowed when the column already holds it that way - StateColumn::from_views(family, bytes, offsets): states read in place, checked, held outside the run - values::family_has_view_layout(family) Sketch views reuse MappedSketches (one check shared with packed); exact states use a fixed little-endian record.
Document that a keyed exact state has no view bytes, say why a gathered view column reports no stored size, and refuse malformed exact records in the public-API test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
state_view_bytesprivate and deletedStateColumn::from_mapped,family_has_mapped_formand the stored exact-record form, because no crate in the workspace called them.asap_executor::values.StateColumn::view_bytes(index)gives a state's view layout.StateColumn::from_views(family, bytes, offsets)reads stored views where they lie.family_has_view_layout(family)says whether a family has a view layout. Sketch views reuseMappedSketches(the newMappedSketches::oversharescheck_viewswithpacked). Exact states use a fixed little-endian record.view_bytes, packed by the caller into oneVec<u64>buffer and read back withfrom_views. Every state's bytes lie inside the caller's buffer, and p10 / p50 / p99 plus pairwise merges equal the originals. The same holds for KLL (k = 1024), Count-Min (3 × 64) and all six exact kinds, also through aSummaryMergeplan over the rebuilt column. A batch over the column is charged none of its states. No plan, cost or answer changes.Review guide
main. Not stacked.crates/executor/src/values/state_column.rs:StateColumn::from_views,StateColumn::view_bytes, theExactRecords::Mappedvariant.crates/executor/src/values/mapped_states.rs:ViewLayout,family_has_view_layout,check_offsets/check_views,MappedSketches::over,exact_record_bytes,MappedRecords.crates/integration-tests/tests/state_views.rs: the public-API round trip.from_views. The view readers (accumulator,merge_into,ddsketch_quantile)expectvalid views, so every view handed in must be checked once, up front. Also review the memory charge: the column is held outside the run, and its stored size is the bytes the offsets span plus the offsets.ExactRecordsmatch arms,mapped_column's new stored-size argument (0 at its existing call), the compaction skip inBatchnow also covering exact records held outside the run, and the module doc ofmapped_states.rs.Behavior: before → after
The caller is a runtime that stores the sealed tumbling windows of a plan and, after a restart, hands them back to plans as stored windows.
main)ExactAccumulatoris serde-able; sketch objects are not, apart from their sketchlib types)column.view_bytes(i)for each state: borrowed when the column already holds views (a sealed DDSketch column, or a column read withfrom_views), else writtenStateColumn::from_views(family, Buffer, ScalarBuffer<u32>): zero-copy over the caller's buffer, for example an ArrowBufferover a mapped file. The view is checked once, then read in place by quantile reads and merges. The run is charged nothing for the statesfamily_has_view_layoutisfalse,view_bytesisNone, andfrom_viewsis bad input. The caller keeps such states as objects#1521 removed this path because no crate in the workspace called it (its row "
StateColumn::from_mapped… accepted (no caller in the workspace) → API removed"). That path had a real caller outside the workspace, so under "delete legacy only when replaced" it comes back here as a smaller public API. The removed duplicates stay removed:pack_wordsand the old construction-time view check are nowMappedSketches::packedand the sharedcheck_views.The executor defines no file format. A caller lays out the states, offsets, padding, compression and headers however it likes. The executor only fixes each state's own bytes:
asap_sketchlib's view layout for sketches, and the exact record documented onexact_record_bytes.Interfaces
Permalinks are at the PR head.
crates/executor/src/values/state_column.rsimpl StateColumn { + /// The column of `family` states a runtime stored as view bytes + /// ([`Self::view_bytes`]) and hands back over bytes it holds: state `i` + /// at `bytes[offsets[i]..offsets[i + 1]]`, read where it lies (a buffer + /// over a mapped file stays mapped). A sketch view must start 8-byte + /// aligned and may end in zero padding; exact records lie back to back + /// from the start of `bytes` (`offsets[i]` is `i` times the record's + /// size). Each state is checked here, once. The run is not charged for + /// the states (they are held outside it); their stored size is the + /// bytes the offsets span plus the offsets. Fails with + /// [`crate::Error::Invalid`] for a family with no view layout + /// ([`super::family_has_view_layout`]), offsets out of order or past + /// `bytes`, or a state that is not a view of `family`. + pub fn from_views( + family: FieldDataType, + bytes: arrow::buffer::Buffer, + offsets: arrow::buffer::ScalarBuffer<u32>, + ) -> Result<Self, crate::Error>; + /// State `index` in its view layout, the bytes [`Self::from_views`] + /// reads back: a sketch's `asap_sketchlib` view (read back, it must + /// start at an 8-byte-aligned address), or an exact state's fixed + /// record. Borrowed where the column already holds it in that layout, + /// else written. `None` for a family with no view layout + /// ([`super::family_has_view_layout`]) or a keyed exact state. + pub fn view_bytes(&self, index: usize) -> Option<Cow<'_, [u8]>>; }crates/executor/src/values/mapped_states.rs(re-exported asasap_executor::values::family_has_view_layout)What #1521 removed, and what replaces it (paths abridged):
The view bytes now come from a column, not from a lone state object. That way a column that already holds views lends them without re-encoding.
Usage (from
state_views.rs, abridged):Algorithm changes
None. Validation, view reads and merges are the existing
MappedSketchescode. The exact record is the layout that #1521 removed, unchanged.Decisions and open questions
from_viewsreports its stored size (the bytes the offsets span plus the offsets). refactor: one home for chain cover windows and view packing; delete the mapped-bytes input path #1521'sfrom_mappedreported 0.ExactAccumulator's serde form. Serde would rebuild one object per state, which is not in place, and the column would no longer be the compact exact-record storage that operators read.view_bytestakes a column and a state index rather than a lone state, so a column that already holds views lends them (Cow::Borrowed) instead of re-encoding them.Verification
All runs used
cargo1.100, at this PR's head onmainf17bd9924.maincargo fmt --all --checkcargo clippy --workspace --all-targets --all-features --locked -- -D warningscargo test -p asap-executorcargo test -p asap-integration-testsstate_views::)cargo test -p asap-stage3-plan-selection --libcargo test --release -p asap-planner-cli --test corpus_snapshots -- --include-ignored(corpus + sketch snapshots)cargo test --release -p asap-planner-cli --test goldensNew tests in
crates/integration-tests/tests/state_views.rs(public API only). Onmain(f17bd9924) the file does not compile: 8 errors, all for the missing API (1 × unresolved importfamily_has_view_layout, 4 × nofrom_views, 3 × noview_bytes).ddsketch_kll_and_count_min_states_read_back_in_place: writes sealed DDSketch and KLL columns and a Count-Min object column as views, then reads them back over a caller buffer. Each state's bytes lie inside that buffer and equal the written view up to zero padding. Quantiles, the Count-Min total and point counts, and merges of adjacent states equal the originals. A batch of the column is charged none of its states (its stored size minus its charged memory is the column's stored size).exact_states_read_back_in_place: all six exact kinds (SUM, COUNT, MIN, MAX, counter, irate) as objects with one empty state, plus sealed exact records. Each reads back in place and serializes equal.a_merge_over_states_read_back_in_place_answers_as_over_the_originals: aSummaryMergeby series over a batch of the rebuilt column (this merges through the in-place view reads) gives the same DDSketch and KLL quantiles, Count-Min totals and exact sums as over the original column.malformed_views_are_refused: these are bad input: DDSketch views read as KLL, an end offset past the bytes, a family with no view layout (HLL), an exact record with a presence word of 2, a record off its slot, and exact parameters of another kind.Unit test in
crates/executor/src/values/mapped_states.rs:exact_records_read_back_as_written: each exact kind's record, present and absent (NaN included), has its kind's size and decodes to the state it was written from.Follow-ups