Skip to main content

rudb_encoding/
integer.rs

1//! The single column integer encodings and the cascade over them.
2//!
3//! `spec/06-compression.md` section 6.2 lists the encoding set and section 6.3 says the ratios are
4//! in the cascade rather than in any one encoding. This module is both: the seven candidate shapes
5//! for an integer column, each of which encodes its own output by calling back into the chooser, so
6//! that RLE over a dictionary over a bit packed code array is a thing that happens by construction
7//! rather than a case somebody wrote out.
8//!
9//! Everything here works on `i64`. A narrower column is widened on the way in and nothing is lost
10//! by it, because every encoding's size comes from the range of the values rather than from the
11//! declared width of the type: a `SMALLINT` column of values 100 to 130 packs to 5 bits whether it
12//! arrived as `i16` or as `i64`. The one place the widening would cost something is a raw copy, and
13//! there is no raw copy, because a bit packed unit at width 64 is exactly that and the chooser
14//! reaches it on its own when nothing else fits.
15//!
16//! ## The unit
17//!
18//! Bit packing is per 1024 values, per [`crate::bitpack`]. Everything else is per chunk, where a
19//! chunk is however many values the caller passes in and is meant to be a row group. The two
20//! granularities are the point rather than an accident. A frame of reference base that is chosen
21//! per 1024 values tracks a column that drifts, which is what a timestamp column and an
22//! autoincrementing key both do, and one base per row group would pay the whole range of the row
23//! group on every value. A dictionary, on the other hand, is worth more the larger the unit it
24//! covers, which is the argument section 6.5 takes all the way to a dictionary per table.
25//!
26//! ## The serialized form
27//!
28//! A chunk is a tag byte, a value count, and a body whose shape depends on the tag. Bodies that
29//! contain another array of integers contain a whole chunk, tag and all, which is what makes the
30//! decoder a fold and what makes the cascade free: nothing in `Rle` knows what its run lengths are
31//! encoded as. The header is fixed width little endian rather than a varint, because 5 bytes per
32//! chunk against a chunk that holds a row group is not worth the branch on the decode path.
33//!
34//! ## What the chooser does, and what it will have to do instead
35//!
36//! It encodes every candidate and keeps the smallest. That is the honest baseline for M1, which is
37//! a measurement of what the format can do rather than of how fast a writer can decide, and it is
38//! not what a write path can afford. Section 6.3 describes the real thing: evaluate the candidates
39//! on a systematic sample, not the first N rows, because column data is frequently clustered and
40//! the first 1024 rows of a sorted column look constant. Building that first would mean the numbers
41//! this milestone produces are the sampler's numbers rather than the format's, and there would be
42//! no way to tell how much the sampler is leaving behind.
43
44use std::collections::BTreeMap;
45use std::time::Instant;
46
47use rudb_common::{Error, Result};
48
49use crate::chooser::{Chooser, EXHAUSTIVE};
50use crate::reader::Reader;
51use crate::tally::{self, Family};
52
53use crate::bitpack::{self, VALUES};
54
55/// How deep a cascade is allowed to go.
56///
57/// Three levels is what section 6.3 says captures most of what a general compressor would find:
58/// dictionary, then bit packed codes, then nothing left worth doing. The limit exists because the
59/// chooser is exhaustive and a cascade that could nest forever would be exponential, and because a
60/// fourth level has never once been the smallest candidate in anything measured so far.
61const MAX_DEPTH: u8 = 3;
62
63/// How many values a run writes at once, whatever the run is.
64///
65/// A run length decode used to write a value at a time for the length of the run, which reads well
66/// and is the wrong shape for the data: a clustered join key runs two or three long, so the loop
67/// spent its time mispredicting its own exit and the branch cost more than the stores did. Writing
68/// a fixed eight and then moving on by the run's real length has no exit to predict, and whatever
69/// of the eight was surplus is overwritten by the run that follows, because every run writes at
70/// least its own length. Eight because it is two vector stores on every machine this runs on and
71/// longer than nearly every run in a column worth run length encoding at all.
72const RUN: usize = 8;
73
74/// The average run length from which a run length chunk is decoded into reserved room rather than
75/// a zeroed one. Past it the zeroing is most of the writes, and below it the fixed width write of
76/// [`RUN`] is the cheaper loop.
77const LONG_RUN: usize = 64;
78
79/// What a chunk is encoded as. The discriminant is the tag byte in the serialized form and is part
80/// of the format, so the numbers are written down rather than left to the compiler.
81#[derive(Debug, Clone, Copy, PartialEq, Eq)]
82pub enum Kind {
83    /// One value repeated. The whole chunk is the tag, the count and the value.
84    Constant = 0,
85    /// Frame of reference then bit packed, per 1024 values. Covers plain bit packing at base zero
86    /// and a raw copy at width 64.
87    Packed = 1,
88    /// Differences between neighbours, zigzagged so a decreasing column is as cheap as an
89    /// increasing one, then encoded as a chunk in its own right.
90    Delta = 2,
91    /// Run values and run lengths, each encoded as a chunk in its own right.
92    Rle = 3,
93    /// A dictionary of the distinct values and an array of codes into it, both encoded as chunks in
94    /// their own right.
95    Dict = 4,
96    /// One dominant value with an exception list of positions and values.
97    Sparse = 5,
98    /// A base and a common step, with the number of steps to each value encoded as a chunk in its
99    /// own right.
100    Strided = 6,
101}
102
103impl Kind {
104    /// Every kind, in tag order.
105    pub const ALL: [Self; 7] = [
106        Self::Constant,
107        Self::Packed,
108        Self::Delta,
109        Self::Rle,
110        Self::Dict,
111        Self::Sparse,
112        Self::Strided,
113    ];
114
115    fn tag(self) -> u8 {
116        self as u8
117    }
118
119    fn from_tag(tag: u8) -> Result<Self> {
120        match tag {
121            0 => Ok(Self::Constant),
122            1 => Ok(Self::Packed),
123            2 => Ok(Self::Delta),
124            3 => Ok(Self::Rle),
125            4 => Ok(Self::Dict),
126            5 => Ok(Self::Sparse),
127            6 => Ok(Self::Strided),
128            other => Err(Error::internal(format!("unknown encoding tag {other}"))),
129        }
130    }
131
132    /// The name that goes in a report.
133    #[must_use]
134    pub fn name(self) -> &'static str {
135        match self {
136            Self::Constant => "CONSTANT",
137            Self::Packed => "FOR+BITPACK",
138            Self::Delta => "DELTA",
139            Self::Rle => "RLE",
140            Self::Dict => "DICT",
141            Self::Sparse => "SPARSE",
142            Self::Strided => "STRIDE",
143        }
144    }
145}
146
147/// Encodes a chunk of integers, choosing the cascade that comes out smallest.
148///
149/// # Errors
150///
151/// If the chunk is longer than `u32::MAX`, or if an encoding produces something its own decoder
152/// would not accept, which is an internal inconsistency rather than a caller error.
153pub fn encode(values: &[i64]) -> Result<Vec<u8>> {
154    encode_with(values, &EXHAUSTIVE)
155}
156
157/// [`encode`] with somebody else deciding which candidates are worth encoding in full.
158///
159/// A chooser narrows the list and nothing else. It cannot offer a candidate that does not apply, so
160/// whatever it picks still has to encode the whole chunk and still has to decode, and the worst a
161/// bad one can do is come out bigger than [`encode`] would have.
162///
163/// # Errors
164///
165/// As [`encode`].
166pub fn encode_with(values: &[i64], chooser: &dyn Chooser) -> Result<Vec<u8>> {
167    encode_at(values, 0, chooser)
168}
169
170/// Decodes a chunk written by [`encode`].
171///
172/// # Errors
173///
174/// If the bytes are truncated, carry an unknown tag, or describe a chunk whose parts do not agree
175/// with each other.
176pub fn decode(bytes: &[u8]) -> Result<Vec<i64>> {
177    let mut reader = Reader::new(bytes);
178    let values = with_decoding(|scratch| decode_chunk(&mut reader, scratch))?;
179    if reader.remaining() != 0 {
180        return Err(Error::internal(format!(
181            "{} bytes left over after decoding a chunk",
182            reader.remaining()
183        )));
184    }
185    Ok(values)
186}
187
188/// Decodes one encoded chunk straight into the integer type a column is declared at.
189///
190/// The same values [`decode`] gives, without the `i64` in between. A reader that wants a `SMALLINT`
191/// column used to fill eight bytes a row with zeros, write every value into them, and then copy the
192/// lot into two bytes a row, which on ClickBench Q1 was a fifth of the query. Constant, packed,
193/// sparse and run length chunks are written in the target type directly. The other kinds decode as
194/// before and are narrowed after, since they are rare at the top of a column.
195///
196/// # Errors
197///
198/// As [`decode`], or if a value does not fit in `T`, which is a chunk that disagrees with the type
199/// it was written for.
200pub fn decode_as<T: Lane>(bytes: &[u8]) -> Result<Vec<T>> {
201    let mut reader = Reader::new(bytes);
202    let values = with_decoding(|scratch| decode_chunk_as(&mut reader, scratch))?;
203    if reader.remaining() != 0 {
204        return Err(Error::internal(format!(
205            "{} bytes left over after decoding a chunk",
206            reader.remaining()
207        )));
208    }
209    Ok(values)
210}
211
212/// An integer type a chunk can be decoded straight into. See [`decode_as`].
213pub trait Lane: Copy + Default {
214    /// The value in this type, or `None` when it does not fit.
215    fn fit(value: i64) -> Option<Self>;
216
217    /// The value in this type, for a value already known to fit.
218    fn wrap(value: i64) -> Self;
219}
220
221macro_rules! lanes {
222    ($($ty:ty),* $(,)?) => {$(
223        impl Lane for $ty {
224            fn fit(value: i64) -> Option<Self> {
225                Self::try_from(value).ok()
226            }
227
228            #[allow(
229                clippy::cast_possible_truncation,
230                clippy::cast_sign_loss,
231                clippy::unnecessary_cast,
232                reason = "only called on a value the caller has checked fits"
233            )]
234            fn wrap(value: i64) -> Self {
235                value as Self
236            }
237        }
238    )*};
239}
240
241lanes!(i8, u8, i16, u16, i32, u32, i64, u64);
242
243/// One value in the type the chunk is being decoded into, or the error for a value that is not.
244fn lane<T: Lane>(value: i64) -> Result<T> {
245    T::fit(value).ok_or_else(|| Error::internal(format!("{value} is outside the chunk's type")))
246}
247
248/// Counts values in one encoded chunk without expanding sparse or run-length chunks into rows.
249///
250/// The result is computed from the encoded row values when called. It is not a stored histogram.
251/// Other encodings use the ordinary decoder until they have a useful count form of their own.
252///
253/// # Errors
254///
255/// As [`decode`], or if a sparse position or run length is outside the chunk.
256pub fn tally(bytes: &[u8]) -> Result<(usize, Vec<(i64, u64)>)> {
257    let mut counts = BTreeMap::<i64, u64>::new();
258    let rows = fold(bytes, |value, count| {
259        *counts.entry(value).or_default() += count;
260        Ok(())
261    })?;
262    Ok((rows, counts.into_iter().collect()))
263}
264
265/// Visits the values of one encoded chunk with their runtime row counts. Sparse chunks written
266/// with sorted exception positions need no per-chunk count map. An older or malformed chunk with
267/// repeated positions keeps the decoder's last-write-wins behavior.
268///
269/// # Errors
270///
271/// As [`decode`], or if the callback rejects a count.
272pub fn fold(bytes: &[u8], mut emit: impl FnMut(i64, u64) -> Result<()>) -> Result<usize> {
273    let mut reader = Reader::new(bytes);
274    let kind = Kind::from_tag(reader.u8()?)?;
275    let count = reader.u32()? as usize;
276    match kind {
277        Kind::Constant => {
278            let value = reader.i64()?;
279            if count != 0 {
280                emit(value, count as u64)?;
281            }
282        }
283        Kind::Sparse => {
284            let dominant = reader.i64()?;
285            let exception_count = reader.u32()? as usize;
286            let (positions, values) = with_decoding(|scratch| -> Result<_> {
287                Ok((decode_chunk(&mut reader, scratch)?, decode_chunk(&mut reader, scratch)?))
288            })?;
289            if positions.len() != exception_count || values.len() != exception_count {
290                return Err(Error::internal("a sparse chunk disagrees about its exception count"));
291            }
292            let mut ordered = true;
293            let mut previous = None;
294            for &position in &positions {
295                let position = usize::try_from(position)
296                    .ok()
297                    .filter(|&position| position < count)
298                    .ok_or_else(|| Error::internal("a sparse exception is outside the chunk"))?;
299                if previous.is_some_and(|last| position <= last) {
300                    ordered = false;
301                }
302                previous = Some(position);
303            }
304            if ordered {
305                if count != exception_count {
306                    emit(dominant, (count - exception_count) as u64)?;
307                }
308                for value in values {
309                    emit(value, 1)?;
310                }
311            } else {
312                // The ordinary decoder lets a later exception overwrite an earlier one at the
313                // same position. Keep that rule for chunks the writer would not normally produce.
314                let mut exceptions = BTreeMap::<usize, i64>::new();
315                for (position, value) in positions.into_iter().zip(values) {
316                    exceptions.insert(position as usize, value);
317                }
318                if count != exceptions.len() {
319                    emit(dominant, (count - exceptions.len()) as u64)?;
320                }
321                for value in exceptions.into_values() {
322                    emit(value, 1)?;
323                }
324            }
325        }
326        Kind::Rle => {
327            let (values, lengths) = with_decoding(|scratch| -> Result<_> {
328                Ok((decode_chunk(&mut reader, scratch)?, decode_chunk(&mut reader, scratch)?))
329            })?;
330            if values.len() != lengths.len() {
331                return Err(Error::internal("an RLE chunk has more runs than run lengths"));
332            }
333            let mut rows = 0_usize;
334            for (value, length) in values.into_iter().zip(lengths) {
335                let length = usize::try_from(length)
336                    .map_err(|_| Error::internal("a negative RLE run length"))?;
337                rows = rows
338                    .checked_add(length)
339                    .filter(|&rows| rows <= count)
340                    .ok_or_else(|| Error::internal("an RLE run ends past its chunk"))?;
341                if length != 0 {
342                    emit(value, length as u64)?;
343                }
344            }
345            check_count(rows, count)?;
346        }
347        _ => {
348            // Re-read the header through the existing decoder for the other cascade shapes.
349            reader = Reader::new(bytes);
350            let values = with_decoding(|scratch| decode_chunk(&mut reader, scratch))?;
351            check_count(values.len(), count)?;
352            for value in values {
353                emit(value, 1)?;
354            }
355        }
356    }
357    if reader.remaining() != 0 {
358        return Err(Error::internal(format!(
359            "{} bytes left over after counting a chunk",
360            reader.remaining()
361        )));
362    }
363    Ok(count)
364}
365
366/// How few of a packed unit's values a selected decode has to want before it finds each one on its
367/// own rather than unpacking the unit, as one in this many.
368const SPARSE: usize = 32;
369
370/// Decodes selected row positions from a chunk written by [`encode`].
371///
372/// Positions must be sorted and unique. Packed chunks read only the words holding those positions,
373/// and run length chunks walk their run boundaries without expanding the output. Other cascade
374/// shapes use the full decoder and select afterward until they have a point form of their own.
375///
376/// # Errors
377///
378/// As [`decode`], or if a position is outside the chunk or the positions are not strictly
379/// increasing.
380pub fn decode_selected(bytes: &[u8], positions: &[usize]) -> Result<Vec<i64>> {
381    if positions.windows(2).any(|pair| pair[0] >= pair[1]) {
382        return Err(Error::internal("selected integer positions are not sorted and unique"));
383    }
384    let mut reader = Reader::new(bytes);
385    let values = with_decoding(|scratch| decode_selected_chunk(&mut reader, positions, scratch))?;
386    if reader.remaining() != 0 {
387        return Err(Error::internal(format!(
388            "{} bytes left over after decoding selected values",
389            reader.remaining()
390        )));
391    }
392    Ok(values)
393}
394
395/// Whether [`decode_selected`] reads a few rows of this chunk for less than decoding all of it.
396///
397/// True for the kinds whose rows can be found without the rows before them: a constant, packed
398/// units, and strides or dictionary codes over packed units. A run length chunk walks every run to
399/// find where a row is, and a delta chunk adds up every delta before it, so for those a caller that
400/// wants a few rows does better decoding the chunk the usual way and picking them out.
401#[must_use]
402pub fn pointed(bytes: &[u8]) -> bool {
403    let simple = |bytes: &[u8]| {
404        bytes
405            .first()
406            .and_then(|&tag| Kind::from_tag(tag).ok())
407            .is_some_and(|kind| matches!(kind, Kind::Constant | Kind::Packed))
408    };
409    match bytes.first().and_then(|&tag| Kind::from_tag(tag).ok()) {
410        Some(Kind::Constant | Kind::Packed) => true,
411        // The tag, the count, the base and the stride come before the steps.
412        Some(Kind::Strided) => bytes.get(1 + 4 + 8 + 8..).is_some_and(simple),
413        // The dictionary is read whole whatever the rows, so only the codes need a point form.
414        Some(Kind::Dict) => {
415            let mut reader = Reader::new(bytes.get(1 + 4..).unwrap_or_default());
416            skip_chunk(&mut reader).is_ok() && simple(reader.rest())
417        }
418        _ => false,
419    }
420}
421
422/// Decodes a chunk that sits at the front of a longer buffer, and says how many bytes it took.
423///
424/// A string column holds integer chunks inside its own body, and the reader on that side cannot
425/// know where the nested chunk ends until it has been read. A chunk is self delimiting, so this is
426/// the same work [`decode`] does without the check that nothing follows.
427///
428/// # Errors
429///
430/// As [`decode`], except that trailing bytes are what the caller asked about rather than an error.
431pub fn decode_prefix(bytes: &[u8]) -> Result<(Vec<i64>, usize)> {
432    let mut reader = Reader::new(bytes);
433    let values = with_decoding(|scratch| decode_chunk(&mut reader, scratch))?;
434    Ok((values, reader.used()))
435}
436
437/// [`describe`] over a chunk at the front of a longer buffer, and how many bytes it took.
438///
439/// # Errors
440///
441/// As [`decode_prefix`].
442pub fn describe_prefix(bytes: &[u8]) -> Result<(String, usize)> {
443    let mut reader = Reader::new(bytes);
444    let text = describe_chunk(&mut reader)?;
445    Ok((text, reader.used()))
446}
447
448/// The size in bytes of every candidate, for a report that wants to say what the cascade was
449/// chosen over rather than only what it chose. A candidate that does not apply is absent.
450///
451/// # Errors
452///
453/// As [`encode`].
454pub fn candidate_sizes(values: &[i64]) -> Result<Vec<(Kind, usize)>> {
455    let mut sizes = Vec::new();
456    for kind in candidates(values, 0, &EXHAUSTIVE) {
457        if let Some(bytes) = encode_as(kind, values, 0, &EXHAUSTIVE)? {
458            sizes.push((kind, bytes.len()));
459        }
460    }
461    Ok(sizes)
462}
463
464/// Which candidates [`encode`] would try on this chunk, in the order it tries them.
465///
466/// The chooser is exhaustive, so this is also the list of encodes it pays for to return one of
467/// them. A caller measuring where the encode time goes needs the list separately from the sizes,
468/// because a candidate that is offered and turns out not to apply still costs whatever it spent
469/// finding that out.
470#[must_use]
471pub fn offered(values: &[i64]) -> Vec<Kind> {
472    candidates(values, 0, &EXHAUSTIVE)
473}
474
475/// One candidate on its own, which is what the chooser calls once per entry in [`offered`].
476///
477/// `None` when the encoding does not apply. This is here so that the time the chooser spends can be
478/// attributed to the candidate that spent it, which is the measurement F2 wants before anybody
479/// replaces the exhaustive search with a sampled one. It is not how a writer encodes a chunk:
480/// [`encode`] is, and picking a kind by hand gives up the only thing the chooser is for.
481///
482/// # Errors
483///
484/// As [`encode`].
485pub fn encode_only(kind: Kind, values: &[i64]) -> Result<Option<Vec<u8>>> {
486    encode_as(kind, values, 0, &EXHAUSTIVE)
487}
488
489/// How big one candidate comes out, which is all a sampling chooser needs from it.
490///
491/// The bytes are thrown away, so this says nothing [`encode_only`] does not. It is `pub(crate)` and
492/// separate so that the sampler in [`crate::chooser`] is not handing back buffers it will not read.
493pub(crate) fn size_as(kind: Kind, values: &[i64], depth: u8) -> Result<Option<usize>> {
494    Ok(encode_as(kind, values, depth, &EXHAUSTIVE)?.map(|bytes| bytes.len()))
495}
496
497/// The kind at every level of an encoded chunk, in the order the encoder chose them.
498///
499/// The order is the one [`encode_with`] asks its chooser in: a level, then everything under its
500/// first inner chunk, then everything under its second. So a chooser that hands these back one per
501/// question gets the same cascade on a chunk that offers the same kinds, without searching any of
502/// it. That is what a writer with many small parts of one column wants, because the search is
503/// most of what the encode costs and neighbouring parts nearly always come out the same shape.
504///
505/// # Errors
506///
507/// As [`decode`].
508pub fn shape(bytes: &[u8]) -> Result<Vec<Kind>> {
509    let mut reader = Reader::new(bytes);
510    let mut kinds = Vec::new();
511    shape_chunk(&mut reader, &mut kinds)?;
512    Ok(kinds)
513}
514
515/// The cascade a chunk was encoded as, as a line of text like `DICT(PACKED, PACKED)`.
516///
517/// # Errors
518///
519/// As [`decode`].
520pub fn describe(bytes: &[u8]) -> Result<String> {
521    let mut reader = Reader::new(bytes);
522    describe_chunk(&mut reader)
523}
524
525fn encode_at(values: &[i64], depth: u8, chooser: &dyn Chooser) -> Result<Vec<u8>> {
526    let started = Instant::now();
527    let offered = candidates(values, depth, chooser);
528    let narrowed = chooser.narrow_integers(values, &offered, depth);
529    // Only the top level is counted, so that a cascade's time is counted once. See `tally`.
530    let counted = depth == 0;
531    if counted {
532        tally::chose(Family::Integer, started);
533    }
534    let mut best: Option<(Kind, Vec<u8>)> = None;
535    for kind in narrowed {
536        let encoded = if counted {
537            tally::offer(Family::Integer, kind.tag(), || encode_as(kind, values, depth, chooser))?
538        } else {
539            encode_as(kind, values, depth, chooser)?
540        };
541        let Some(bytes) = encoded else {
542            continue;
543        };
544        if best.as_ref().is_none_or(|(_, current)| bytes.len() < current.len()) {
545            best = Some((kind, bytes));
546        }
547    }
548    // `Packed` applies to every input including the empty one, so the chooser always has at least
549    // one candidate and this cannot be reached without a bug in `candidates`.
550    let (kind, bytes) = best.ok_or_else(|| Error::internal("no encoding applied to the chunk"))?;
551    if counted {
552        tally::kept(Family::Integer, kind.tag());
553    }
554    Ok(bytes)
555}
556
557/// Which candidates are worth encoding for this input.
558///
559/// The filters here are not the cost model. They are the cases where the encoding cannot be
560/// expressed at all, or is provably larger than `Packed` on the same data, so that the exhaustive
561/// chooser does not spend a dictionary build on a column of 100,000 distinct values to discover
562/// what its distinct count already said.
563///
564/// A kind the chooser says it will never keep is not tested for at all. The test for a dictionary
565/// sorts a copy of the chunk, and this runs at every level of the cascade, so a chooser that never
566/// keeps a dictionary was paying for a sort per level to find out something it would ignore.
567fn candidates(values: &[i64], depth: u8, chooser: &dyn Chooser) -> Vec<Kind> {
568    let mut kinds = vec![Kind::Packed];
569    if depth >= MAX_DEPTH {
570        return kinds;
571    }
572    let Some(profile) = Profile::of(values) else {
573        return kinds;
574    };
575    if profile.runs == 1 {
576        // Nothing else can beat 13 bytes, so this is the whole answer rather than a candidate.
577        return vec![Kind::Constant];
578    }
579    let considered = |kind| chooser.considers_integer(kind, depth);
580    // Every neighbouring difference is no wider than the whole range, so a range that fits in an
581    // `i64` answers for all of them and only a chunk holding both ends of the type walks the pairs.
582    let fits = profile.max.checked_sub(profile.min).is_some();
583    if considered(Kind::Delta)
584        && (if fits { profile.deltas_pay(values) } else { deltas_fit(values) })
585    {
586        kinds.push(Kind::Delta);
587    }
588    if considered(Kind::Rle) && profile.runs * 4 <= values.len() * 3 {
589        kinds.push(Kind::Rle);
590    }
591    // A dictionary's codes are as wide as its distinct count, so one whose codes are no narrower
592    // than the values has only added a dictionary. On TPC-H SF1 it was offered 1,124 times, kept 16
593    // times and cost 30% of the integer cascade's time before this.
594    if considered(Kind::Dict) && profile.width() > 1 {
595        let distinct = spread_of(values).0;
596        if distinct * 2 <= values.len() && width_of(distinct as u64 - 1) < profile.width() {
597            kinds.push(Kind::Dict);
598        }
599    }
600    // A value in four rows out of five leaves a fifth for everything else, and each of those rows
601    // starts at most two runs, so a chunk with more runs than that has no such value and the vote
602    // is not taken.
603    if considered(Kind::Sparse)
604        && (profile.runs - 1) * 5 <= values.len() * 2
605        && majority(values).is_some_and(|(_, count)| count * 10 >= values.len() * 8)
606    {
607        kinds.push(Kind::Sparse);
608    }
609    if considered(Kind::Strided) && stride_from(values, profile.min).is_some() {
610        kinds.push(Kind::Strided);
611    }
612    kinds
613}
614
615/// What one pass over a chunk says about it, which is most of what the candidate tests ask.
616///
617/// The tests used to walk the chunk once each: once to see whether it was one value, once for the
618/// deltas, once to count runs, twice for the vote and once more for the smallest value under the
619/// stride. That is six passes on every chunk at every level of the cascade, and on ClickBench `hits`
620/// they were most of the tenth of the load's CPU that `encode_at` came to, since a replayed part
621/// still asks every question the fallback would. This is one pass, and the vote is only taken where
622/// the run count leaves room for it.
623///
624/// The same pass looks at the differences too, since it has both neighbours in hand. `DELTA` was
625/// offered on every chunk whose range fit, and on the `hits_0` load it was 3,325 offers, 20.6% of
626/// the integer cascade's time and never kept once. What it stores is the zigzagged differences, so
627/// their spread says how wide they pack and their runs say whether they would run-length code.
628struct Profile {
629    min: i64,
630    max: i64,
631    /// Runs of equal neighbours, which is one for a chunk of a single value.
632    runs: usize,
633    /// The smallest and largest zigzagged difference between neighbours. They wrap where the range
634    /// does not fit in an `i64`, and nothing reads them then.
635    delta_low: u64,
636    delta_high: u64,
637    /// Runs of equal differences, which is one for a chunk of fewer than three values.
638    delta_runs: usize,
639}
640
641impl Profile {
642    fn of(values: &[i64]) -> Option<Self> {
643        let first = *values.first()?;
644        let (mut min, mut max, mut breaks) = (first, first, 0usize);
645        let mut last = values.get(1).map_or(0, |second| second.wrapping_sub(first));
646        let (mut delta_low, mut delta_high, mut turns) = (u64::MAX, 0u64, 0usize);
647        for (before, after) in values.iter().zip(&values[1..]) {
648            min = min.min(*after);
649            max = max.max(*after);
650            breaks += usize::from(before != after);
651            let delta = after.wrapping_sub(*before);
652            let zigzagged = zigzag(delta);
653            delta_low = delta_low.min(zigzagged);
654            delta_high = delta_high.max(zigzagged);
655            turns += usize::from(delta != last);
656            last = delta;
657        }
658        Some(Self { min, max, runs: breaks + 1, delta_low, delta_high, delta_runs: turns + 1 })
659    }
660
661    /// How many bits `Packed` needs for a value of this chunk at most, from the whole range.
662    fn width(&self) -> u32 {
663        width_of(self.max.wrapping_sub(self.min) as u64)
664    }
665
666    /// Whether the differences are worth encoding, for a chunk whose range fits in an `i64`.
667    ///
668    /// They are when they pack narrower than the values, which is a sorted key or a slowly moving
669    /// counter, or when they run-length code and the values do not, which is a column that climbs
670    /// in steps, or when the first few take only a handful of values, which is a column that walks
671    /// a cycle and whose differences make a dictionary of a few entries. Anything else comes out no
672    /// smaller than `Packed` or `Rle` on the values.
673    fn deltas_pay(&self, values: &[i64]) -> bool {
674        let len = values.len();
675        let narrower = width_of(self.delta_high.wrapping_sub(self.delta_low)) < self.width();
676        // A chunk that run-length codes has differences that are nearly all zero, so they repeat
677        // and there are few of them, and `Rle` on the values still beats them.
678        let unruly = self.runs * 4 > len * 3;
679        let repeat = self.delta_runs * 4 <= len.saturating_sub(1) * 3;
680        narrower || (unruly && (repeat || few_deltas(values)))
681    }
682}
683
684/// Whether the first [`FEW_DELTAS_SEEN`] differences take no more than [`FEW_DELTAS`] values.
685///
686/// It stops at the first difference past that many, which on a chunk with nothing cyclic in it is
687/// a handful of pairs in.
688fn few_deltas(values: &[i64]) -> bool {
689    let mut seen = [0i64; FEW_DELTAS];
690    let mut count = 0;
691    for pair in values.windows(2).take(FEW_DELTAS_SEEN) {
692        let delta = pair[1].wrapping_sub(pair[0]);
693        if seen[..count].contains(&delta) {
694            continue;
695        }
696        if count == FEW_DELTAS {
697            return false;
698        }
699        seen[count] = delta;
700        count += 1;
701    }
702    true
703}
704
705/// How many distinct differences [`few_deltas`] allows.
706const FEW_DELTAS: usize = 4;
707
708/// How many differences [`few_deltas`] looks at.
709const FEW_DELTAS_SEEN: usize = 64;
710
711/// The bits a value up to `range` takes, which is zero for a range of zero.
712fn width_of(range: u64) -> u32 {
713    u64::BITS - range.leading_zeros()
714}
715
716/// `None` when the encoding does not apply to this input, which the caller treats as a candidate
717/// that did not run rather than as a failure.
718fn encode_as(
719    kind: Kind,
720    values: &[i64],
721    depth: u8,
722    chooser: &dyn Chooser,
723) -> Result<Option<Vec<u8>>> {
724    let mut out = Vec::new();
725    put_u8(&mut out, kind.tag());
726    put_u32(&mut out, u32::try_from(values.len()).map_err(|_| too_long(values.len()))?);
727    match kind {
728        Kind::Constant => {
729            let Some(first) = values.first() else {
730                return Ok(None);
731            };
732            if values.iter().any(|value| value != first) {
733                return Ok(None);
734            }
735            put_i64(&mut out, *first);
736        }
737        Kind::Packed => encode_packed(values, &mut out)?,
738        Kind::Delta => {
739            // An empty chunk has no first value to hang the differences off. The search never asks
740            // for one because `candidates` rules it out, but `encode_only` goes straight past that
741            // and used to index into the chunk anyway.
742            let (Some(first), Some(deltas)) = (values.first(), deltas(values)) else {
743                return Ok(None);
744            };
745            put_i64(&mut out, *first);
746            out.extend_from_slice(&encode_at(&deltas, depth + 1, chooser)?);
747        }
748        Kind::Rle => {
749            let (run_values, run_lengths) = runs(values);
750            if run_values.is_empty() {
751                return Ok(None);
752            }
753            out.extend_from_slice(&encode_at(&run_values, depth + 1, chooser)?);
754            out.extend_from_slice(&encode_at(&run_lengths, depth + 1, chooser)?);
755        }
756        Kind::Dict => {
757            let dictionary = distinct_values(values);
758            if dictionary.is_empty() {
759                return Ok(None);
760            }
761            let codes = codes_over(values, &dictionary);
762            out.extend_from_slice(&encode_at(&dictionary, depth + 1, chooser)?);
763            out.extend_from_slice(&encode_at(&codes, depth + 1, chooser)?);
764        }
765        Kind::Sparse => {
766            // The majority is the most frequent value whenever there is one, and a chunk the search
767            // offers this for always has one. `encode_only` can ask about any chunk, so the sort is
768            // still there for a chunk with no majority.
769            let Some((value, _)) = majority(values).or_else(|| spread_of(values).1) else {
770                return Ok(None);
771            };
772            let mut positions = Vec::new();
773            let mut exceptions = Vec::new();
774            for (index, other) in values.iter().enumerate() {
775                if *other != value {
776                    positions.push(index as i64);
777                    exceptions.push(*other);
778                }
779            }
780            put_i64(&mut out, value);
781            put_u32(
782                &mut out,
783                u32::try_from(positions.len()).map_err(|_| too_long(positions.len()))?,
784            );
785            out.extend_from_slice(&encode_at(&positions, depth + 1, chooser)?);
786            out.extend_from_slice(&encode_at(&exceptions, depth + 1, chooser)?);
787        }
788        Kind::Strided => {
789            let (Some(base), Some(stride)) = (values.iter().min().copied(), stride_of(values))
790            else {
791                return Ok(None);
792            };
793            let mut steps = Vec::with_capacity(values.len());
794            for value in values {
795                let step = offset_from(*value, base) / stride;
796                // A step count the recursion cannot hold. An offset is at most 65 bits because both
797                // ends came from an `i64`, and only a stride of one leaves it that wide, which is a
798                // stride this never offers. Refused rather than wrapped, because a candidate that
799                // does not apply is one the chooser skips.
800                let Ok(step) = i64::try_from(step) else {
801                    return Ok(None);
802                };
803                steps.push(step);
804            }
805            put_i64(&mut out, base);
806            put_u64(&mut out, stride);
807            out.extend_from_slice(&encode_at(&steps, depth + 1, chooser)?);
808        }
809    }
810    Ok(Some(out))
811}
812
813/// Frame of reference and bit packing, one base and one width per 1024 values.
814///
815/// A base per unit rather than per chunk is most of what makes this work on real columns. A
816/// timestamp column over a day drifts across a range that needs 47 bits, and the same column inside
817/// any one unit spans a few seconds and needs 12. One base per row group would pay the 47 on every
818/// value.
819///
820/// A unit shorter than 1024 values, which is the last one of any chunk whose length is not a
821/// multiple of the unit and is the only one of every short array in a cascade, goes through
822/// [`bitpack::pack_tail`] instead. The transposed layout has no partial form and would charge a
823/// five entry dictionary for 1024 entries.
824fn encode_packed(values: &[i64], out: &mut Vec<u8>) -> Result<()> {
825    // The same three buffers for every unit, for the reason written on `Decoding` on the other side.
826    // The chooser encodes every candidate it is offered before it picks one, so this loop runs more
827    // often on the way in than the decoding loop does on the way out.
828    let mut offsets: Vec<u64> = Vec::with_capacity(VALUES);
829    // Held at the width 64 length for the reason written on `Decoding`, so a narrower unit writes
830    // the front of it and the resize per unit goes away.
831    let mut packed: Vec<u64> = vec![0; bitpack::packed_len::<u64>(64)];
832    let mut transposed = bitpack::Scratch::<u64>::new();
833    for unit in values.chunks(VALUES) {
834        let base = unit.iter().copied().min().unwrap_or(0);
835        offsets.clear();
836        offsets.extend(unit.iter().map(|value| offset_from(*value, base)));
837        let width = bitpack::required_width(&offsets);
838        put_i64(out, base);
839        put_u8(out, u8::try_from(width).map_err(|_| Error::internal("impossible width"))?);
840        if unit.len() == VALUES {
841            let words = bitpack::packed_len::<u64>(width);
842            bitpack::pack_with(&offsets, width, &mut packed[..words], &mut transposed)?;
843            for word in &packed[..words] {
844                put_u64(out, *word);
845            }
846        } else {
847            bitpack::pack_tail(&offsets, width, out)?;
848        }
849    }
850    Ok(())
851}
852
853/// The buffer a decode reuses from one unit of 1024 values to the next.
854///
855/// This used to be allocated inside the loop, and because it was allocated with a value rather than
856/// grown, the allocator zeroed it and then the decode overwrote every byte. In a ClickBench profile
857/// that zeroing was the single largest item, ahead of the unpacking it was making room for, because
858/// a scan pays it once per 1024 rows of every packed integer column it reads.
859///
860/// It is threaded through the recursion rather than made per call because a chunk is a cascade. A
861/// dictionary of deltas is three nested decodes, and each of them would otherwise make its own.
862///
863/// It starts empty and is grown on the first unit that needs it, to its largest size rather than to
864/// the size that unit wants, so that every unit after the first finds it the right length already
865/// and nothing is zeroed or resized again.
866///
867/// There used to be a second buffer here holding one unit of unpacked offsets, which the decode
868/// then walked to add the frame of reference base back on. The unpackers take the base now and
869/// write into the chunk directly, so that buffer and the pass over it are both gone.
870///
871/// It lives on the thread rather than in the caller, which is worth saying why. A chunk is a row
872/// group, and a row group in the native format is about a thousand rows, which is one unit. So there
873/// is no second unit in a chunk to reuse anything and holding this per call is strictly worse than
874/// allocating per unit was: it was tried, and it cost more in the growing than it saved in the
875/// zeroing. What there are many of is chunks, one per part per column, and the thread that reads
876/// them reads them one after another. That is the loop the reuse belongs to, and reaching it by
877/// passing a buffer down would mean a parameter through every page decoder in the storage layer for
878/// a buffer none of them has an opinion about.
879struct Decoding {
880    /// The packed words of one unit, as read off the wire. Held at the width 64 length, which is the
881    /// largest a unit can be, so a narrower unit uses the front of it.
882    packed: Vec<u64>,
883}
884
885thread_local! {
886    /// The buffers this thread decodes through. See [`Decoding`].
887    static DECODING: std::cell::RefCell<Decoding> =
888        const { std::cell::RefCell::new(Decoding::new()) };
889}
890
891/// Runs a decode over this thread's buffers.
892///
893/// Nothing inside a decode calls back into one, so the borrow is never already taken. It is asked
894/// for rather than assumed anyway, and a decode that somehow arrives while another is running gets
895/// buffers of its own rather than a panic, because the alternative is a crash in a reader on a
896/// path nobody exercised.
897fn with_decoding<T>(run: impl FnOnce(&mut Decoding) -> T) -> T {
898    DECODING.with(|cell| match cell.try_borrow_mut() {
899        Ok(mut scratch) => run(&mut scratch),
900        Err(_) => run(&mut Decoding::new()),
901    })
902}
903
904impl Decoding {
905    /// A buffer that has not made room for anything yet.
906    const fn new() -> Self {
907        Self { packed: Vec::new() }
908    }
909
910    /// Makes room for one unit. A no op every time after the first.
911    fn ready(&mut self) {
912        if self.packed.len() != bitpack::packed_len::<u64>(64) {
913            self.packed.resize(bitpack::packed_len::<u64>(64), 0);
914        }
915    }
916}
917
918fn decode_chunk(reader: &mut Reader<'_>, scratch: &mut Decoding) -> Result<Vec<i64>> {
919    let kind = Kind::from_tag(reader.u8()?)?;
920    let count = reader.u32()? as usize;
921    match kind {
922        Kind::Constant => Ok(vec![reader.i64()?; count]),
923        Kind::Packed => {
924            // One buffer for the chunk, and every value written into it once. Both unpackers take
925            // the frame of reference base and put the value it belongs to where it goes, so there
926            // is no unit of raw offsets in between and no second pass to fold the base back in.
927            let mut values = vec![0i64; count];
928            scratch.ready();
929            let mut done = 0;
930            while done < count {
931                let base = reader.i64()?;
932                let width = reader.u8()? as usize;
933                let wanted = (count - done).min(VALUES);
934                let into = &mut values[done..done + wanted];
935                if wanted == VALUES {
936                    let words = bitpack::packed_len::<u64>(width);
937                    for word in &mut scratch.packed[..words] {
938                        *word = reader.u64()?;
939                    }
940                    bitpack::unpack_mapped(&scratch.packed[..words], width, into, |offset| {
941                        value_from(offset, base)
942                    })?;
943                } else {
944                    let bytes = reader.bytes(bitpack::tail_len(wanted, width))?;
945                    bitpack::unpack_tail_into(bytes, width, into, |offset| {
946                        value_from(offset, base)
947                    })?;
948                }
949                done += wanted;
950            }
951            Ok(values)
952        }
953        Kind::Delta => {
954            let first = reader.i64()?;
955            let deltas = decode_chunk(reader, scratch)?;
956            let mut values = Vec::with_capacity(count);
957            values.push(first);
958            let mut current = first;
959            for delta in deltas {
960                current = current.wrapping_add(unzigzag(delta as u64));
961                values.push(current);
962            }
963            check_count(values.len(), count)?;
964            Ok(values)
965        }
966        Kind::Rle => {
967            let run_values = decode_chunk(reader, scratch)?;
968            let run_lengths = decode_chunk(reader, scratch)?;
969            if run_values.len() != run_lengths.len() {
970                return Err(Error::internal("an RLE chunk has more runs than run lengths"));
971            }
972            // A chunk whose runs are long on average, the way a sorted column's are thousands of
973            // rows each, has every run appended into reserved room, so that each value is written
974            // once by the run it belongs to. Zeroing the chunk first was a second write of all of
975            // it. Short runs are cheaper the other way, with room for one run past the end so that
976            // the write below never has to ask how much of its fixed width landed inside the chunk,
977            // and appending those cost a few percent more on ClickBench 15, 17 and 31.
978            let long = run_values.len().saturating_mul(LONG_RUN) <= count;
979            let mut values = if long { Vec::with_capacity(count) } else { vec![0; count + RUN] };
980            let mut at = 0usize;
981            for (value, length) in run_values.into_iter().zip(run_lengths) {
982                let length = usize::try_from(length)
983                    .map_err(|_| Error::internal("a negative RLE run length"))?;
984                let end = at
985                    .checked_add(length)
986                    .filter(|end| *end <= count)
987                    .ok_or_else(|| Error::internal("an RLE run ends past its chunk"))?;
988                if long {
989                    values.resize(end, value);
990                } else {
991                    let short =
992                        if length <= RUN { values[at..].first_chunk_mut::<RUN>() } else { None };
993                    match short {
994                        Some(window) => window.fill(value),
995                        None => values[at..end].fill(value),
996                    }
997                }
998                at = end;
999            }
1000            check_count(at, count)?;
1001            values.truncate(count);
1002            Ok(values)
1003        }
1004        Kind::Dict => {
1005            let dictionary = decode_chunk(reader, scratch)?;
1006            let codes = decode_chunk(reader, scratch)?;
1007            let mut values = Vec::with_capacity(count);
1008            for code in codes {
1009                let index =
1010                    usize::try_from(code).ok().and_then(|index| dictionary.get(index)).ok_or_else(
1011                        || Error::internal(format!("code {code} is not in the dictionary")),
1012                    )?;
1013                values.push(*index);
1014            }
1015            check_count(values.len(), count)?;
1016            Ok(values)
1017        }
1018        Kind::Sparse => {
1019            let value = reader.i64()?;
1020            let exception_count = reader.u32()? as usize;
1021            let positions = decode_chunk(reader, scratch)?;
1022            let exceptions = decode_chunk(reader, scratch)?;
1023            if positions.len() != exception_count || exceptions.len() != exception_count {
1024                return Err(Error::internal("a sparse chunk disagrees about its exception count"));
1025            }
1026            let mut values = vec![value; count];
1027            for (position, exception) in positions.into_iter().zip(exceptions) {
1028                let position = usize::try_from(position)
1029                    .ok()
1030                    .filter(|position| *position < count)
1031                    .ok_or_else(|| {
1032                        Error::internal(format!("exception at {position} is outside the chunk"))
1033                    })?;
1034                values[position] = exception;
1035            }
1036            Ok(values)
1037        }
1038        Kind::Strided => {
1039            let base = reader.i64()?;
1040            let stride = reader.u64()?;
1041            let steps = decode_chunk(reader, scratch)?;
1042            check_count(steps.len(), count)?;
1043            let mut values = Vec::with_capacity(count);
1044            for step in steps {
1045                let step = u64::try_from(step)
1046                    .map_err(|_| Error::internal("a negative number of strides"))?;
1047                values.push(value_from(step.wrapping_mul(stride), base));
1048            }
1049            Ok(values)
1050        }
1051    }
1052}
1053
1054/// [`decode_chunk`] into `T`. See [`decode_as`].
1055fn decode_chunk_as<T: Lane>(reader: &mut Reader<'_>, scratch: &mut Decoding) -> Result<Vec<T>> {
1056    let Some(&tag) = reader.rest().first() else {
1057        return Err(Error::internal("a chunk ended before its encoding tag"));
1058    };
1059    if !matches!(Kind::from_tag(tag)?, Kind::Constant | Kind::Packed | Kind::Sparse | Kind::Rle) {
1060        return decode_chunk(reader, scratch)?.into_iter().map(lane).collect();
1061    }
1062    let kind = Kind::from_tag(reader.u8()?)?;
1063    let count = reader.u32()? as usize;
1064    match kind {
1065        Kind::Constant => Ok(vec![lane(reader.i64()?)?; count]),
1066        Kind::Packed => {
1067            let mut values = vec![T::default(); count];
1068            scratch.ready();
1069            let mut wide = [0i64; VALUES];
1070            let mut done = 0;
1071            while done < count {
1072                let base = reader.i64()?;
1073                let width = reader.u8()? as usize;
1074                let wanted = (count - done).min(VALUES);
1075                let into = &mut values[done..done + wanted];
1076                let bytes = if wanted == VALUES {
1077                    let words = bitpack::packed_len::<u64>(width);
1078                    for word in &mut scratch.packed[..words] {
1079                        *word = reader.u64()?;
1080                    }
1081                    None
1082                } else {
1083                    Some(reader.bytes(bitpack::tail_len(wanted, width))?)
1084                };
1085                // Every value of a block is between its base and the base plus the widest offset its
1086                // width holds, so when both ends fit the whole block does and the unpack writes the
1087                // target type with no check a value. A block that could hold more than the type,
1088                // which a width rounded up past the range can, is unpacked wide and checked.
1089                let mask = if width >= 64 { u64::MAX } else { (1u64 << width) - 1 };
1090                let top = i64::try_from(i128::from(base) + i128::from(mask)).ok();
1091                if T::fit(base).is_some() && top.and_then(T::fit).is_some() {
1092                    let map = |offset| T::wrap(value_from(offset, base));
1093                    match bytes {
1094                        None => {
1095                            let words = bitpack::packed_len::<u64>(width);
1096                            bitpack::unpack_mapped(&scratch.packed[..words], width, into, map)?;
1097                        }
1098                        Some(bytes) => bitpack::unpack_tail_into(bytes, width, into, map)?,
1099                    }
1100                } else {
1101                    let wide = &mut wide[..wanted];
1102                    let map = |offset| value_from(offset, base);
1103                    match bytes {
1104                        None => {
1105                            let words = bitpack::packed_len::<u64>(width);
1106                            bitpack::unpack_mapped(&scratch.packed[..words], width, wide, map)?;
1107                        }
1108                        Some(bytes) => bitpack::unpack_tail_into(bytes, width, wide, map)?,
1109                    }
1110                    for (value, &held) in into.iter_mut().zip(wide.iter()) {
1111                        *value = lane(held)?;
1112                    }
1113                }
1114                done += wanted;
1115            }
1116            Ok(values)
1117        }
1118        Kind::Rle => {
1119            let run_values = decode_chunk(reader, scratch)?;
1120            let run_lengths = decode_chunk(reader, scratch)?;
1121            if run_values.len() != run_lengths.len() {
1122                return Err(Error::internal("an RLE chunk has more runs than run lengths"));
1123            }
1124            // The two shapes [`decode_chunk`] has, for the reasons it gives.
1125            let long = run_values.len().saturating_mul(LONG_RUN) <= count;
1126            let mut values =
1127                if long { Vec::with_capacity(count) } else { vec![T::default(); count + RUN] };
1128            let mut at = 0usize;
1129            for (value, length) in run_values.into_iter().zip(run_lengths) {
1130                let value = lane::<T>(value)?;
1131                let length = usize::try_from(length)
1132                    .map_err(|_| Error::internal("a negative RLE run length"))?;
1133                let end = at
1134                    .checked_add(length)
1135                    .filter(|end| *end <= count)
1136                    .ok_or_else(|| Error::internal("an RLE run ends past its chunk"))?;
1137                if long {
1138                    values.resize(end, value);
1139                } else {
1140                    let short =
1141                        if length <= RUN { values[at..].first_chunk_mut::<RUN>() } else { None };
1142                    match short {
1143                        Some(window) => window.fill(value),
1144                        None => values[at..end].fill(value),
1145                    }
1146                }
1147                at = end;
1148            }
1149            check_count(at, count)?;
1150            values.truncate(count);
1151            Ok(values)
1152        }
1153        Kind::Sparse => {
1154            let value = lane::<T>(reader.i64()?)?;
1155            let exception_count = reader.u32()? as usize;
1156            let positions = decode_chunk(reader, scratch)?;
1157            let exceptions = decode_chunk(reader, scratch)?;
1158            if positions.len() != exception_count || exceptions.len() != exception_count {
1159                return Err(Error::internal("a sparse chunk disagrees about its exception count"));
1160            }
1161            let mut values = vec![value; count];
1162            for (position, exception) in positions.into_iter().zip(exceptions) {
1163                let position = usize::try_from(position)
1164                    .ok()
1165                    .filter(|position| *position < count)
1166                    .ok_or_else(|| {
1167                        Error::internal(format!("exception at {position} is outside the chunk"))
1168                    })?;
1169                values[position] = lane(exception)?;
1170            }
1171            Ok(values)
1172        }
1173        Kind::Delta | Kind::Dict | Kind::Strided => {
1174            Err(Error::internal("a chunk kind that decodes wide reached the narrow decoder"))
1175        }
1176    }
1177}
1178
1179fn decode_selected_chunk(
1180    reader: &mut Reader<'_>,
1181    positions: &[usize],
1182    scratch: &mut Decoding,
1183) -> Result<Vec<i64>> {
1184    let Some(&tag) = reader.rest().first() else {
1185        return Err(Error::internal("a chunk ended before its encoding tag"));
1186    };
1187    let kind = Kind::from_tag(tag)?;
1188    if !matches!(kind, Kind::Constant | Kind::Packed | Kind::Rle | Kind::Strided | Kind::Dict) {
1189        let values = decode_chunk(reader, scratch)?;
1190        return positions
1191            .iter()
1192            .map(|&position| {
1193                values.get(position).copied().ok_or_else(|| {
1194                    Error::internal(format!(
1195                        "selected integer position {position} is outside {} values",
1196                        values.len()
1197                    ))
1198                })
1199            })
1200            .collect();
1201    }
1202
1203    let decoded = Kind::from_tag(reader.u8()?)?;
1204    debug_assert_eq!(decoded, kind);
1205    let count = reader.u32()? as usize;
1206    if positions.last().is_some_and(|&position| position >= count) {
1207        return Err(Error::internal(format!(
1208            "selected integer position {} is outside {count} values",
1209            positions.last().expect("a last position exists")
1210        )));
1211    }
1212    match kind {
1213        Kind::Constant => {
1214            let value = reader.i64()?;
1215            Ok(vec![value; positions.len()])
1216        }
1217        Kind::Packed => {
1218            let mut out = Vec::with_capacity(positions.len());
1219            let mut from = 0;
1220            let mut done = 0;
1221            while done < count {
1222                let base = reader.i64()?;
1223                let width = reader.u8()? as usize;
1224                let wanted = (count - done).min(VALUES);
1225                let upto = positions.partition_point(|&position| position < done + wanted);
1226                if wanted == VALUES && (upto - from) * SPARSE > VALUES {
1227                    // Enough of the unit is wanted that unpacking all of it is cheaper than
1228                    // finding each value on its own.
1229                    let words = bitpack::packed_len::<u64>(width);
1230                    scratch.ready();
1231                    for word in &mut scratch.packed[..words] {
1232                        *word = reader.u64()?;
1233                    }
1234                    let mut unit = [0_i64; VALUES];
1235                    bitpack::unpack_mapped(&scratch.packed[..words], width, &mut unit, |offset| {
1236                        value_from(offset, base)
1237                    })?;
1238                    out.extend(positions[from..upto].iter().map(|&position| unit[position - done]));
1239                } else if wanted == VALUES {
1240                    let bytes = reader.bytes(bitpack::packed_len::<u64>(width) * 8)?;
1241                    for &position in &positions[from..upto] {
1242                        let offset = bitpack::unpack_u64_at(bytes, width, position - done)?;
1243                        out.push(value_from(offset, base));
1244                    }
1245                } else {
1246                    let bytes = reader.bytes(bitpack::tail_len(wanted, width))?;
1247                    for &position in &positions[from..upto] {
1248                        let offset = bitpack::tail_at(bytes, width, position - done)?;
1249                        out.push(value_from(offset, base));
1250                    }
1251                }
1252                from = upto;
1253                done += wanted;
1254            }
1255            Ok(out)
1256        }
1257        Kind::Rle => {
1258            let run_value_bytes = reader.rest();
1259            let mut run_value_reader = Reader::new(run_value_bytes);
1260            let run_value_count = skip_chunk(&mut run_value_reader)?;
1261            let run_value_len = run_value_reader.used();
1262            reader.skip(run_value_len)?;
1263            let run_lengths = decode_chunk(reader, scratch)?;
1264            if run_value_count != run_lengths.len() {
1265                return Err(Error::internal("an RLE chunk has more runs than run lengths"));
1266            }
1267            let mut wanted_runs = Vec::new();
1268            let mut selected_per_run = Vec::new();
1269            let mut selected = 0;
1270            let mut at = 0usize;
1271            for (run, length) in run_lengths.into_iter().enumerate() {
1272                let length = usize::try_from(length)
1273                    .map_err(|_| Error::internal("a negative RLE run length"))?;
1274                let end = at
1275                    .checked_add(length)
1276                    .filter(|end| *end <= count)
1277                    .ok_or_else(|| Error::internal("an RLE run ends past its chunk"))?;
1278                let before = selected;
1279                while selected < positions.len() && positions[selected] < end {
1280                    if positions[selected] < at {
1281                        return Err(Error::internal("selected integer positions went backwards"));
1282                    }
1283                    selected += 1;
1284                }
1285                if selected != before {
1286                    wanted_runs.push(run);
1287                    selected_per_run.push(selected - before);
1288                }
1289                at = end;
1290            }
1291            check_count(at, count)?;
1292            if selected != positions.len() {
1293                return Err(Error::internal("an RLE chunk ended before a selected position"));
1294            }
1295            let run_values = decode_selected(&run_value_bytes[..run_value_len], &wanted_runs)?;
1296            let mut out = Vec::with_capacity(positions.len());
1297            for (value, repeat) in run_values.into_iter().zip(selected_per_run) {
1298                out.extend(std::iter::repeat_n(value, repeat));
1299            }
1300            Ok(out)
1301        }
1302        // The steps and the codes are chunks of their own, read at the same rows, and the dictionary
1303        // is read whole since a code can point anywhere in it.
1304        Kind::Strided => {
1305            let base = reader.i64()?;
1306            let stride = reader.u64()?;
1307            let steps = decode_selected_chunk(reader, positions, scratch)?;
1308            steps
1309                .into_iter()
1310                .map(|step| {
1311                    let step = u64::try_from(step)
1312                        .map_err(|_| Error::internal("a negative number of strides"))?;
1313                    Ok(value_from(step.wrapping_mul(stride), base))
1314                })
1315                .collect()
1316        }
1317        Kind::Dict => {
1318            let dictionary = decode_chunk(reader, scratch)?;
1319            let codes = decode_selected_chunk(reader, positions, scratch)?;
1320            codes
1321                .into_iter()
1322                .map(|code| {
1323                    usize::try_from(code)
1324                        .ok()
1325                        .and_then(|index| dictionary.get(index))
1326                        .copied()
1327                        .ok_or_else(|| {
1328                            Error::internal(format!("code {code} is not in the dictionary"))
1329                        })
1330                })
1331                .collect()
1332        }
1333        _ => unreachable!("unsupported kinds used the full decoder"),
1334    }
1335}
1336
1337/// Advances over one encoded chunk without materializing its values and returns its row count.
1338fn skip_chunk(reader: &mut Reader<'_>) -> Result<usize> {
1339    let kind = Kind::from_tag(reader.u8()?)?;
1340    let count = reader.u32()? as usize;
1341    match kind {
1342        Kind::Constant => reader.skip(8)?,
1343        Kind::Packed => skip_packed(reader, count)?,
1344        Kind::Delta => {
1345            reader.skip(8)?;
1346            skip_chunk(reader)?;
1347        }
1348        Kind::Rle | Kind::Dict => {
1349            skip_chunk(reader)?;
1350            skip_chunk(reader)?;
1351        }
1352        Kind::Sparse => {
1353            reader.skip(12)?;
1354            skip_chunk(reader)?;
1355            skip_chunk(reader)?;
1356        }
1357        Kind::Strided => {
1358            reader.skip(16)?;
1359            skip_chunk(reader)?;
1360        }
1361    }
1362    Ok(count)
1363}
1364
1365/// Advances over the units of a `Packed` body of `count` values.
1366fn skip_packed(reader: &mut Reader<'_>, count: usize) -> Result<()> {
1367    let mut done = 0;
1368    while done < count {
1369        reader.skip(8)?;
1370        let width = reader.u8()? as usize;
1371        if width > 64 {
1372            return Err(Error::internal(format!("a packed integer width of {width} is past 64")));
1373        }
1374        let wanted = (count - done).min(VALUES);
1375        let bytes = if wanted == VALUES {
1376            bitpack::packed_len::<u64>(width)
1377                .checked_mul(8)
1378                .ok_or_else(|| Error::internal("packed integer size overflow"))?
1379        } else {
1380            bitpack::tail_len(wanted, width)
1381        };
1382        reader.skip(bytes)?;
1383        done += wanted;
1384    }
1385    Ok(())
1386}
1387
1388/// [`shape`] for one chunk and everything inside it.
1389fn shape_chunk(reader: &mut Reader<'_>, kinds: &mut Vec<Kind>) -> Result<()> {
1390    let kind = Kind::from_tag(reader.u8()?)?;
1391    let count = reader.u32()? as usize;
1392    kinds.push(kind);
1393    match kind {
1394        Kind::Constant => reader.skip(8)?,
1395        Kind::Packed => skip_packed(reader, count)?,
1396        Kind::Delta => {
1397            reader.skip(8)?;
1398            shape_chunk(reader, kinds)?;
1399        }
1400        Kind::Rle | Kind::Dict => {
1401            shape_chunk(reader, kinds)?;
1402            shape_chunk(reader, kinds)?;
1403        }
1404        Kind::Sparse => {
1405            reader.skip(12)?;
1406            shape_chunk(reader, kinds)?;
1407            shape_chunk(reader, kinds)?;
1408        }
1409        Kind::Strided => {
1410            reader.skip(16)?;
1411            shape_chunk(reader, kinds)?;
1412        }
1413    }
1414    Ok(())
1415}
1416
1417fn describe_chunk(reader: &mut Reader<'_>) -> Result<String> {
1418    let kind = Kind::from_tag(reader.u8()?)?;
1419    let count = reader.u32()? as usize;
1420    Ok(match kind {
1421        Kind::Constant => {
1422            reader.i64()?;
1423            "CONSTANT".to_string()
1424        }
1425        Kind::Packed => {
1426            let mut widths = Vec::new();
1427            let mut seen = 0;
1428            while seen < count {
1429                reader.i64()?;
1430                let width = reader.u8()? as usize;
1431                let wanted = (count - seen).min(VALUES);
1432                if wanted == VALUES {
1433                    for _ in 0..bitpack::packed_len::<u64>(width) {
1434                        reader.u64()?;
1435                    }
1436                } else {
1437                    reader.bytes(bitpack::tail_len(wanted, width))?;
1438                }
1439                widths.push(width);
1440                seen += wanted;
1441            }
1442            let low = widths.iter().copied().min().unwrap_or(0);
1443            let high = widths.iter().copied().max().unwrap_or(0);
1444            // Square brackets rather than round ones, so that a reader and a test can both take a
1445            // parenthesis to mean one more level of cascade and nothing else.
1446            if low == high {
1447                format!("FOR+BITPACK[{low}]")
1448            } else {
1449                format!("FOR+BITPACK[{low}..{high}]")
1450            }
1451        }
1452        Kind::Delta => {
1453            reader.i64()?;
1454            format!("DELTA({})", describe_chunk(reader)?)
1455        }
1456        Kind::Rle => {
1457            let values = describe_chunk(reader)?;
1458            let lengths = describe_chunk(reader)?;
1459            format!("RLE({values}, {lengths})")
1460        }
1461        Kind::Dict => {
1462            let dictionary = describe_chunk(reader)?;
1463            let codes = describe_chunk(reader)?;
1464            format!("DICT({dictionary}, {codes})")
1465        }
1466        Kind::Sparse => {
1467            reader.i64()?;
1468            reader.u32()?;
1469            let positions = describe_chunk(reader)?;
1470            let exceptions = describe_chunk(reader)?;
1471            format!("SPARSE({positions}, {exceptions})")
1472        }
1473        Kind::Strided => {
1474            reader.i64()?;
1475            let stride = reader.u64()?;
1476            format!("STRIDE[{stride}]({})", describe_chunk(reader)?)
1477        }
1478    })
1479}
1480
1481/// The step every value of the chunk is a whole number of, or `None` when there is not one worth
1482/// having.
1483///
1484/// This is the greatest common divisor of every value's distance from the smallest one. A timestamp
1485/// column loaded from a source that recorded whole seconds holds microseconds that are all multiples
1486/// of a million, and without this the frame of reference pays twenty bits a value to write down the
1487/// twenty zero bits at the bottom of every one of them.
1488///
1489/// The walk stops the moment the divisor reaches one, which is what makes this affordable to ask on
1490/// every chunk. Two values that share no factor are enough to answer, and on a column of arbitrary
1491/// numbers that is almost always the first pair.
1492fn stride_of(values: &[i64]) -> Option<u64> {
1493    stride_from(values, values.iter().min().copied()?)
1494}
1495
1496/// [`stride_of`] for a chunk whose smallest value is already known.
1497fn stride_from(values: &[i64], base: i64) -> Option<u64> {
1498    let mut divisor = 0u64;
1499    for value in values {
1500        divisor = gcd(divisor, offset_from(*value, base));
1501        if divisor == 1 {
1502            return None;
1503        }
1504    }
1505    // Zero is every value being the base, which `Constant` already holds for nothing, and one is
1506    // the frame of reference on its own with two extra words of header.
1507    (divisor > 1).then_some(divisor)
1508}
1509
1510/// Binary GCD, which is the one without a division in it.
1511fn gcd(mut left: u64, mut right: u64) -> u64 {
1512    if left == 0 {
1513        return right;
1514    }
1515    if right == 0 {
1516        return left;
1517    }
1518    let shift = (left | right).trailing_zeros();
1519    left >>= left.trailing_zeros();
1520    loop {
1521        right >>= right.trailing_zeros();
1522        if left > right {
1523            std::mem::swap(&mut left, &mut right);
1524        }
1525        right -= left;
1526        if right == 0 {
1527            return left << shift;
1528        }
1529    }
1530}
1531
1532/// The distance from the frame of reference base, which is always representable in a `u64` because
1533/// both ends came from an `i64` and the width of the difference is at most 65 bits minus the sign.
1534fn offset_from(value: i64, base: i64) -> u64 {
1535    (i128::from(value) - i128::from(base)) as u64
1536}
1537
1538fn value_from(offset: u64, base: i64) -> i64 {
1539    (i128::from(base) + i128::from(offset)) as i64
1540}
1541
1542/// Zigzag, so that a column that counts down packs as narrowly as one that counts up. Without it a
1543/// delta of -1 is 64 bits of ones.
1544fn zigzag(value: i64) -> u64 {
1545    ((value << 1) ^ (value >> 63)) as u64
1546}
1547
1548fn unzigzag(value: u64) -> i64 {
1549    ((value >> 1) as i64) ^ -((value & 1) as i64)
1550}
1551
1552/// The zigzagged differences, or `None` if any difference is too wide to be one.
1553///
1554/// A column holding both `i64::MIN` and `i64::MAX` has a difference that does not fit in an `i64`,
1555/// and rather than widening every delta array to 128 bits for a case that does not occur in data,
1556/// the encoding declines to apply. `Packed` covers it.
1557/// Whether every neighbouring difference fits in an `i64`, which is the only thing the candidate
1558/// list needs to know about deltas.
1559///
1560/// The candidate list used to answer this by building the whole delta array and checking that it
1561/// came back, which is an allocation and a pass over the chunk thrown away on every chunk, and then
1562/// `Kind::Delta` built it again. This is the same pass with nothing kept.
1563fn deltas_fit(values: &[i64]) -> bool {
1564    values.windows(2).all(|pair| pair[1].checked_sub(pair[0]).is_some())
1565}
1566
1567fn deltas(values: &[i64]) -> Option<Vec<i64>> {
1568    let mut deltas = Vec::with_capacity(values.len().saturating_sub(1));
1569    for pair in values.windows(2) {
1570        let difference = pair[1].checked_sub(pair[0])?;
1571        deltas.push(zigzag(difference) as i64);
1572    }
1573    Some(deltas)
1574}
1575
1576/// The value and the length of every run of equal neighbours.
1577///
1578/// Each run is found by walking to its end and pushed once. This used to push the first value of a
1579/// run and then add one to the last length for every value after it, which kept both vectors'
1580/// lengths in memory across the whole loop and was the hottest loop left in `encode_at` once the
1581/// candidate tests became one pass.
1582fn runs(values: &[i64]) -> (Vec<i64>, Vec<i64>) {
1583    let mut run_values: Vec<i64> = Vec::new();
1584    let mut run_lengths: Vec<i64> = Vec::new();
1585    let mut start = 0;
1586    while let Some(&value) = values.get(start) {
1587        let length = values[start..].iter().take_while(|other| **other == value).count();
1588        run_values.push(value);
1589        run_lengths.push(length as i64);
1590        start += length;
1591    }
1592    (run_values, run_lengths)
1593}
1594
1595/// The distinct values in sorted order.
1596///
1597/// Sorted rather than in order of first appearance, because an ordered dictionary is what lets a
1598/// range predicate become a code range instead of a code set, per section 6.7, and because the
1599/// codes of a clustered column then run in order and delta encode.
1600/// How many distinct values there are and which one occurs most often, from one sort.
1601///
1602/// Both questions are about the histogram of the chunk and neither needs the histogram itself, so
1603/// one sorted copy and one walk over it answers both. They used to be two functions that each sorted
1604/// their own copy and threw it away, which is a chunk sorted twice on every chunk at every level of
1605/// the cascade before a single candidate has been encoded.
1606///
1607/// No hash map, because the sort is what makes the walk a scan of equal runs, and a hash map would
1608/// pay a lookup per value to learn the same thing.
1609fn spread_of(values: &[i64]) -> (usize, Option<(i64, usize)>) {
1610    let mut sorted = values.to_vec();
1611    sorted.sort_unstable();
1612    let mut distinct = 0;
1613    let mut best: Option<(i64, usize)> = None;
1614    let mut index = 0;
1615    while index < sorted.len() {
1616        let value = sorted[index];
1617        let mut end = index;
1618        while end < sorted.len() && sorted[end] == value {
1619            end += 1;
1620        }
1621        distinct += 1;
1622        let count = end - index;
1623        if best.is_none_or(|(_, seen)| count > seen) {
1624            best = Some((value, count));
1625        }
1626        index = end;
1627    }
1628    (distinct, best)
1629}
1630
1631/// The value more than half of the chunk holds, and how many times, found in two passes without
1632/// sorting anything.
1633///
1634/// This is the vote that keeps one candidate and a lead: a value that holds more than half the
1635/// chunk outlasts every other value put together, so it is the candidate left at the end, and the
1636/// second pass checks that the candidate really does hold more than half. When it does it is the
1637/// value [`spread_of`] would name as the most frequent, since a value over half the chunk has no tie.
1638fn majority(values: &[i64]) -> Option<(i64, usize)> {
1639    let mut candidate = *values.first()?;
1640    let mut lead = 0usize;
1641    for value in values {
1642        if lead == 0 {
1643            candidate = *value;
1644            lead = 1;
1645        } else if *value == candidate {
1646            lead += 1;
1647        } else {
1648            lead -= 1;
1649        }
1650    }
1651    let count = values.iter().filter(|value| **value == candidate).count();
1652    (count * 2 > values.len()).then_some((candidate, count))
1653}
1654
1655/// The distinct values in sorted order, for the same reason the string dictionary is sorted: an
1656/// ordered dictionary turns a range predicate into a code range rather than a code set.
1657fn distinct_values(values: &[i64]) -> Vec<i64> {
1658    let mut distinct = values.to_vec();
1659    distinct.sort_unstable();
1660    distinct.dedup();
1661    distinct
1662}
1663
1664/// Where each value sits in the dictionary.
1665///
1666/// The string side builds its dictionary and its codes together from one sort of a permutation,
1667/// because the alternative there is a copy of every value onto the heap and a `memcmp` per level of
1668/// a binary search per row. This side was changed to match and it measured slower, so it was changed
1669/// back. An integer dictionary only exists when the distinct count is at most half the row count, so
1670/// the search is over something small and cache resident, the comparison is one integer rather than
1671/// a string, and carrying the source index through the sort means sorting a padded sixteen byte pair
1672/// instead of an eight byte value. The search is cheaper than the wider sort.
1673fn codes_over(values: &[i64], dictionary: &[i64]) -> Vec<i64> {
1674    values
1675        .iter()
1676        .map(|value| {
1677            dictionary
1678                .binary_search(value)
1679                .expect("the dictionary is the distinct values of this chunk") as i64
1680        })
1681        .collect()
1682}
1683
1684fn check_count(actual: usize, expected: usize) -> Result<()> {
1685    if actual == expected {
1686        Ok(())
1687    } else {
1688        Err(Error::internal(format!(
1689            "a chunk says it holds {expected} values and decoded to {actual}"
1690        )))
1691    }
1692}
1693
1694fn too_long(len: usize) -> Error {
1695    Error::internal(format!("a chunk of {len} values is longer than the format allows"))
1696}
1697
1698fn put_u8(out: &mut Vec<u8>, value: u8) {
1699    out.push(value);
1700}
1701
1702fn put_u32(out: &mut Vec<u8>, value: u32) {
1703    out.extend_from_slice(&value.to_le_bytes());
1704}
1705
1706fn put_u64(out: &mut Vec<u8>, value: u64) {
1707    out.extend_from_slice(&value.to_le_bytes());
1708}
1709
1710fn put_i64(out: &mut Vec<u8>, value: i64) {
1711    out.extend_from_slice(&value.to_le_bytes());
1712}
1713
1714#[cfg(test)]
1715mod tests {
1716    use super::*;
1717
1718    fn round_trip(values: &[i64]) -> Vec<u8> {
1719        let bytes = encode(values).unwrap();
1720        assert_eq!(decode(&bytes).unwrap(), values, "{}", describe(&bytes).unwrap());
1721        bytes
1722    }
1723
1724    fn kind_of(bytes: &[u8]) -> Kind {
1725        Kind::from_tag(bytes[0]).unwrap()
1726    }
1727
1728    /// The same xorshift the bit packing tests use, for the same reason.
1729    struct Random(u64);
1730
1731    impl Random {
1732        fn new() -> Self {
1733            Self(0x9e37_79b9_7f4a_7c15)
1734        }
1735
1736        fn next(&mut self) -> u64 {
1737            self.0 ^= self.0 << 13;
1738            self.0 ^= self.0 >> 7;
1739            self.0 ^= self.0 << 17;
1740            self.0
1741        }
1742    }
1743
1744    #[test]
1745    fn the_dictionary_is_sorted_and_the_codes_point_back_at_the_values() {
1746        let values = vec![30i64, 10, 30, 20, 10, -5];
1747        let dictionary = distinct_values(&values);
1748        let codes = codes_over(&values, &dictionary);
1749        assert_eq!(dictionary, vec![-5, 10, 20, 30]);
1750        assert_eq!(codes, vec![3, 1, 3, 2, 1, 0]);
1751        for (code, value) in codes.iter().zip(&values) {
1752            assert_eq!(dictionary[*code as usize], *value);
1753        }
1754    }
1755
1756    #[test]
1757    fn one_sort_gives_the_distinct_count_and_the_most_frequent_value() {
1758        let values = vec![7i64, 7, 7, 1, 2, 2];
1759        assert_eq!(spread_of(&values), (3, Some((7, 3))));
1760        assert_eq!(spread_of(&[]), (0, None));
1761        assert_eq!(spread_of(&[9]), (1, Some((9, 1))));
1762
1763        // A tie goes to the value that sorts first, which is arbitrary but has to be stable,
1764        // because Sparse writes the dominant value into the chunk and the size depends on it.
1765        assert_eq!(spread_of(&[4i64, 4, 8, 8]), (2, Some((4, 2))));
1766    }
1767
1768    #[test]
1769    fn the_majority_is_the_most_frequent_value_whenever_there_is_one() {
1770        let chunks: Vec<Vec<i64>> = vec![
1771            vec![],
1772            vec![3],
1773            vec![1, 2],
1774            vec![1, 1, 2],
1775            vec![2, 1, 1],
1776            vec![4, 4, 8, 8],
1777            vec![7, 1, 7, 2, 7, 3, 7],
1778            vec![1, 2, 3, 9, 9, 9, 9],
1779            (0..1000).map(|index| if index % 5 == 0 { index } else { -4 }).collect(),
1780            (0..1000).map(|index| index % 3).collect(),
1781        ];
1782        for chunk in chunks {
1783            let (_, dominant) = spread_of(&chunk);
1784            let expected = dominant.filter(|(_, count)| count * 2 > chunk.len());
1785            assert_eq!(majority(&chunk), expected, "{chunk:?}");
1786        }
1787    }
1788
1789    /// The one pass offers exactly what the separate tests offered, including on the chunks where
1790    /// the shortcuts in it are the whole answer: a range too wide for an `i64`, and a chunk with too
1791    /// many runs to have a value in four rows out of five.
1792    #[test]
1793    fn the_one_pass_offers_what_the_separate_tests_offered() {
1794        let mut random = Random::new();
1795        let mut chunks: Vec<Vec<i64>> = vec![
1796            vec![],
1797            vec![5],
1798            vec![5, 5, 5],
1799            vec![i64::MIN, i64::MAX],
1800            vec![i64::MAX, i64::MIN, i64::MAX],
1801            vec![i64::MIN, 0, i64::MAX],
1802            vec![-1, i64::MAX],
1803            (0..1000).map(|index| if index % 5 == 0 { index } else { -4 }).collect(),
1804            (0..1000).map(|index| if index % 4 == 0 { index } else { -4 }).collect(),
1805            (0..1000).map(|index| index / 7).collect(),
1806            (0..1000).map(|index| index * 1_000_000).collect(),
1807        ];
1808        for _ in 0..200 {
1809            let len = (random.next() % 300) as usize;
1810            let spread = 1 + random.next() % 8;
1811            let common = (random.next() % 5) as i64;
1812            chunks.push(
1813                (0..len)
1814                    .map(|_| {
1815                        let draw = random.next();
1816                        if draw % 10 < spread { (draw >> 8) as i64 % 50 } else { common }
1817                    })
1818                    .collect(),
1819            );
1820        }
1821        for chunk in chunks {
1822            let mut expected = vec![Kind::Packed];
1823            if !chunk.is_empty() {
1824                if chunk.iter().all(|value| *value == chunk[0]) {
1825                    expected = vec![Kind::Constant];
1826                } else {
1827                    let runs = 1 + chunk.windows(2).filter(|pair| pair[0] != pair[1]).count();
1828                    let low = *chunk.iter().min().unwrap();
1829                    let high = *chunk.iter().max().unwrap();
1830                    let bits = |range: u128| 128 - range.leading_zeros();
1831                    let width = bits((i128::from(high) - i128::from(low)) as u128);
1832                    let zigzags: Vec<u128> = chunk
1833                        .windows(2)
1834                        .map(|pair| u128::from(zigzag(pair[1].wrapping_sub(pair[0]))))
1835                        .collect();
1836                    let spread = zigzags.iter().max().unwrap() - zigzags.iter().min().unwrap();
1837                    let turns = 1 + zigzags.windows(2).filter(|pair| pair[0] != pair[1]).count();
1838                    let mut first: Vec<u128> = zigzags.iter().take(64).copied().collect();
1839                    first.sort_unstable();
1840                    first.dedup();
1841                    let pays = bits(spread) < width
1842                        || (runs * 4 > chunk.len() * 3
1843                            && (turns * 4 <= (chunk.len() - 1) * 3 || first.len() <= 4));
1844                    if deltas_fit(&chunk) && pays {
1845                        expected.push(Kind::Delta);
1846                    }
1847                    if runs * 4 <= chunk.len() * 3 {
1848                        expected.push(Kind::Rle);
1849                    }
1850                    let distinct = spread_of(&chunk).0;
1851                    if distinct * 2 <= chunk.len() && bits(distinct as u128 - 1) < width {
1852                        expected.push(Kind::Dict);
1853                    }
1854                    if majority(&chunk).is_some_and(|(_, count)| count * 10 >= chunk.len() * 8) {
1855                        expected.push(Kind::Sparse);
1856                    }
1857                    if stride_of(&chunk).is_some() {
1858                        expected.push(Kind::Strided);
1859                    }
1860                }
1861            }
1862            assert_eq!(candidates(&chunk, 0, &EXHAUSTIVE), expected, "{chunk:?}");
1863        }
1864    }
1865
1866    #[test]
1867    fn runs_are_every_stretch_of_equal_neighbours_in_order() {
1868        assert_eq!(runs(&[]), (vec![], vec![]));
1869        assert_eq!(runs(&[4]), (vec![4], vec![1]));
1870        assert_eq!(runs(&[1, 1, 2, 1, 1, 1]), (vec![1, 2, 1], vec![2, 1, 3]));
1871    }
1872
1873    #[test]
1874    fn deltas_that_do_not_fit_are_refused_before_they_are_built() {
1875        assert!(deltas_fit(&[1i64, 2, 3]));
1876        assert!(deltas_fit(&[i64::MAX, i64::MAX]));
1877        assert!(!deltas_fit(&[i64::MIN, i64::MAX]));
1878        assert_eq!(deltas_fit(&[i64::MIN, i64::MAX]), deltas(&[i64::MIN, i64::MAX]).is_some());
1879        assert_eq!(deltas_fit(&[1i64, 2, 3]), deltas(&[1i64, 2, 3]).is_some());
1880    }
1881
1882    #[test]
1883    fn what_the_chooser_returns_is_the_smallest_of_what_it_was_offered() {
1884        // `offered` and `encode_only` are what `cargo xtask encode` splits the chooser's seconds
1885        // with, so they have to describe the chooser that actually runs rather than a second copy
1886        // of its rules that drifts. This is the assertion that keeps the two the same thing.
1887        let mut random = Random::new();
1888        let noise: Vec<i64> = (0..2000).map(|_| (random.next() % 5000) as i64).collect();
1889        let runs: Vec<i64> = (0..2000).map(|index: i64| index / 100).collect();
1890        let climbing: Vec<i64> = (0..2000).map(|index| 1_700_000_000 + index).collect();
1891        for values in [noise, runs, climbing, vec![7; 300], Vec::new()] {
1892            let chosen = encode(&values).unwrap();
1893            let mut smallest: Option<Vec<u8>> = None;
1894            for kind in offered(&values) {
1895                let Some(bytes) = encode_only(kind, &values).unwrap() else {
1896                    continue;
1897                };
1898                if smallest.as_ref().is_none_or(|best| bytes.len() < best.len()) {
1899                    smallest = Some(bytes);
1900                }
1901            }
1902            assert_eq!(smallest.as_deref(), Some(chosen.as_slice()), "{}", values.len());
1903        }
1904    }
1905
1906    #[test]
1907    fn a_column_of_whole_seconds_in_microseconds_pays_nothing_for_the_zeroes() {
1908        // What three ClickBench columns are. `epoch_ms(EventTime * 1000)` on a source that recorded
1909        // whole seconds gives microseconds with twenty zero bits under every value, and a frame of
1910        // reference over a part that spans a working day needs 36 bits to write them down.
1911        let mut random = Random::new();
1912        let day = 1_374_000_000_000_000i64;
1913        let values: Vec<i64> =
1914            (0..100_000).map(|_| day + (random.next() % 68_400) as i64 * 1_000_000).collect();
1915        let bytes = round_trip(&values);
1916        assert_eq!(kind_of(&bytes), Kind::Strided);
1917        assert!(describe(&bytes).unwrap().starts_with("STRIDE[1000000]"), "{:?}", describe(&bytes));
1918        // 17 bits a value for the range of seconds, against the 36 the microseconds need.
1919        let strided = 100_000 * 17 / 8;
1920        assert!(bytes.len() < strided + 2000, "{} bytes for {strided} of payload", bytes.len());
1921
1922        let plain = encode_only(Kind::Packed, &values).unwrap().expect("packing always applies");
1923        assert!(
1924            bytes.len() * 2 < plain.len(),
1925            "{} strided against {} packed",
1926            bytes.len(),
1927            plain.len()
1928        );
1929    }
1930
1931    #[test]
1932    fn a_stride_is_the_common_factor_of_the_distances_from_the_smallest_value() {
1933        assert_eq!(stride_of(&[10i64, 20, 40]), Some(10));
1934        // The base is the smallest value and not zero, so a column that does not start on a
1935        // multiple of its own step still has one.
1936        assert_eq!(stride_of(&[7i64, 17, 37]), Some(10));
1937        assert_eq!(stride_of(&[10i64, 20, 23]), None);
1938        // Every value the same is `Constant`'s case and this declines it rather than dividing by a
1939        // stride of zero.
1940        assert_eq!(stride_of(&[5i64; 100]), None);
1941        assert_eq!(stride_of(&[]), None);
1942        // The two ends of the type, where the distance needs 65 bits and only a `u64` holds it.
1943        assert_eq!(stride_of(&[i64::MIN, i64::MAX]), Some(u64::MAX));
1944    }
1945
1946    #[test]
1947    fn a_stride_across_the_whole_of_the_type_round_trips() {
1948        // The distance is 65 bits, so the step count is one and the offset it comes back as is a
1949        // number no `i64` holds. This is the arithmetic the encoder has to do in `u64`.
1950        for values in [vec![i64::MIN, i64::MAX], vec![i64::MIN, 0, i64::MAX]] {
1951            let bytes = round_trip(&values);
1952            assert_eq!(decode(&bytes).unwrap(), values);
1953        }
1954    }
1955
1956    #[test]
1957    fn a_column_with_no_common_factor_is_not_offered_a_stride() {
1958        let mut random = Random::new();
1959        let values: Vec<i64> = (0..2000).map(|_| (random.next() % 1_000_000) as i64).collect();
1960        assert!(!offered(&values).contains(&Kind::Strided));
1961        assert!(encode_only(Kind::Strided, &values).unwrap().is_none());
1962    }
1963
1964    #[test]
1965    fn an_empty_chunk_round_trips() {
1966        let bytes = round_trip(&[]);
1967        assert_eq!(bytes.len(), 5);
1968    }
1969
1970    #[test]
1971    fn a_constant_column_costs_thirteen_bytes_however_long_it_is() {
1972        let bytes = round_trip(&vec![42; 1_000_000]);
1973        assert_eq!(kind_of(&bytes), Kind::Constant);
1974        assert_eq!(bytes.len(), 13);
1975    }
1976
1977    #[test]
1978    fn a_narrow_range_is_packed_at_the_width_of_the_range_and_not_of_the_type() {
1979        // 100_000 values between 1000 and 1063 is 6 bits each, plus 9 bytes of header per 1024.
1980        let mut random = Random::new();
1981        let values: Vec<i64> = (0..100_000).map(|_| 1000 + (random.next() % 64) as i64).collect();
1982        let bytes = round_trip(&values);
1983        assert_eq!(kind_of(&bytes), Kind::Packed);
1984        let packed = 100_000 * 6 / 8;
1985        assert!(bytes.len() < packed + 2000, "{} bytes for {packed} of payload", bytes.len());
1986        assert!(bytes.len() > packed, "{} bytes cannot hold {packed}", bytes.len());
1987    }
1988
1989    #[test]
1990    fn a_counter_becomes_deltas_and_then_a_constant() {
1991        // The classic case and the reason DELTA exists. A million consecutive integers is a
1992        // difference of 1 a million times, which is a constant chunk under the delta.
1993        let values: Vec<i64> = (0..1_000_000).collect();
1994        let bytes = round_trip(&values);
1995        assert_eq!(kind_of(&bytes), Kind::Delta);
1996        assert_eq!(describe(&bytes).unwrap(), "DELTA(CONSTANT)");
1997        assert!(bytes.len() < 40, "{} bytes for a counter", bytes.len());
1998    }
1999
2000    #[test]
2001    fn a_column_that_counts_down_is_as_cheap_as_one_that_counts_up() {
2002        // What zigzag is for. Without it every delta is -1, which is 64 bits of ones.
2003        let up: Vec<i64> = (0..100_000).collect();
2004        let down: Vec<i64> = (0..100_000).rev().collect();
2005        assert_eq!(round_trip(&up).len(), round_trip(&down).len());
2006    }
2007
2008    #[test]
2009    fn long_runs_become_rle() {
2010        let mut values = Vec::new();
2011        for run in 0..1000 {
2012            values.extend(std::iter::repeat_n(run % 7, 200));
2013        }
2014        let bytes = round_trip(&values);
2015        assert_eq!(kind_of(&bytes), Kind::Rle);
2016        assert!(bytes.len() < 2000, "{} bytes for 1000 runs", bytes.len());
2017    }
2018
2019    #[test]
2020    fn a_low_cardinality_column_becomes_a_dictionary() {
2021        // Values that are far apart so that packing them directly is 30 bits each, and only 40 of
2022        // them so that the codes are 6 bits each. The dictionary has to win by a factor of five.
2023        //
2024        // Drawn at random rather than laid out at a fixed interval, because a fixed interval is a
2025        // stride and STRIDE writes the same codes without a dictionary to point them at.
2026        let mut random = Random::new();
2027        let dictionary: Vec<i64> =
2028            (0..40).map(|_| 1_000_000_000 + (random.next() % (1 << 30)) as i64).collect();
2029        let values: Vec<i64> =
2030            (0..100_000).map(|_| dictionary[(random.next() % 40) as usize]).collect();
2031        let bytes = round_trip(&values);
2032        assert_eq!(kind_of(&bytes), Kind::Dict);
2033        assert!(bytes.len() < 100_000, "{} bytes", bytes.len());
2034    }
2035
2036    #[test]
2037    fn a_nearly_constant_column_becomes_sparse() {
2038        let mut values = vec![0i64; 100_000];
2039        for index in 0..300 {
2040            values[index * 331] = 1 << 40;
2041        }
2042        let bytes = round_trip(&values);
2043        assert_eq!(kind_of(&bytes), Kind::Sparse);
2044        assert!(bytes.len() < 3000, "{} bytes for 300 exceptions", bytes.len());
2045    }
2046
2047    #[test]
2048    fn encoded_counts_match_decoded_rows_across_integer_shapes() {
2049        let mut sparse = vec![0_i64; 4096];
2050        for (index, value) in [(7, -3), (91, 12), (1001, -3), (3000, 12)] {
2051            sparse[index] = value;
2052        }
2053        let mut runs = Vec::new();
2054        for value in [0, 7, 0, -5] {
2055            runs.extend(std::iter::repeat_n(value, 500));
2056        }
2057        let mut random = Random::new();
2058        let packed = (0..2000).map(|_| (random.next() % 251) as i64).collect::<Vec<_>>();
2059        for values in [vec![0_i64; 1024], sparse, runs, packed] {
2060            let bytes = encode(&values).unwrap();
2061            let (rows, counts) = tally(&bytes).unwrap();
2062            let mut expected = BTreeMap::<i64, u64>::new();
2063            for value in decode(&bytes).unwrap() {
2064                *expected.entry(value).or_default() += 1;
2065            }
2066            assert_eq!(rows, values.len());
2067            assert_eq!(counts, expected.into_iter().collect::<Vec<_>>());
2068        }
2069    }
2070
2071    #[test]
2072    fn folded_sparse_exceptions_keep_the_last_value_at_a_repeated_position() {
2073        let mut bytes = vec![Kind::Sparse.tag()];
2074        put_u32(&mut bytes, 10);
2075        put_i64(&mut bytes, 0);
2076        put_u32(&mut bytes, 2);
2077        bytes.extend(encode(&[7, 7]).unwrap());
2078        bytes.extend(encode(&[3, 5]).unwrap());
2079
2080        let mut counts = BTreeMap::<i64, u64>::new();
2081        assert_eq!(
2082            fold(&bytes, |value, count| {
2083                *counts.entry(value).or_default() += count;
2084                Ok(())
2085            })
2086            .unwrap(),
2087            10
2088        );
2089        assert_eq!(counts, BTreeMap::from([(0, 9), (5, 1)]));
2090        assert_eq!(decode(&bytes).unwrap()[7], 5);
2091    }
2092
2093    #[test]
2094    fn the_cascade_goes_more_than_one_level_deep() {
2095        // The whole point of section 6.3. A dictionary over a clustered column produces codes that
2096        // run in long stretches, and the run lengths of those are themselves compressible.
2097        let mut values = Vec::new();
2098        for index in 0..2000i64 {
2099            values.extend(std::iter::repeat_n(1_000_000 + (index % 5) * 104_729, 100));
2100        }
2101        let bytes = round_trip(&values);
2102        let shape = describe(&bytes).unwrap();
2103        assert!(shape.contains('('), "{shape} is not a cascade");
2104        assert!(bytes.len() < 4000, "{} bytes: {shape}", bytes.len());
2105    }
2106
2107    #[test]
2108    fn random_data_is_packed_at_full_width_and_costs_what_it_costs() {
2109        // The case where nothing works, which has to come out at eight bytes a value plus change
2110        // rather than at eight bytes a value plus a dictionary of every value in the column.
2111        let mut random = Random::new();
2112        let values: Vec<i64> = (0..10_000).map(|_| random.next() as i64).collect();
2113        let bytes = round_trip(&values);
2114        assert_eq!(kind_of(&bytes), Kind::Packed);
2115        assert!(bytes.len() < 10_000 * 8 + 1000, "{} bytes", bytes.len());
2116    }
2117
2118    #[test]
2119    fn the_extremes_of_the_type_survive() {
2120        // Every offset and every delta in here overflows something if the arithmetic is done in 64
2121        // bits, which is why it is done in 128.
2122        let values = vec![i64::MIN, i64::MAX, 0, -1, i64::MIN, i64::MAX];
2123        round_trip(&values);
2124        round_trip(&[i64::MIN; 3]);
2125        round_trip(&[i64::MIN, i64::MIN + 1]);
2126    }
2127
2128    #[test]
2129    fn a_chunk_that_is_not_a_multiple_of_the_unit_round_trips() {
2130        for len in [1, 2, 1023, 1024, 1025, 2047, 2049] {
2131            let values: Vec<i64> = (0..len).map(|index| (index * 31 % 97) as i64).collect();
2132            round_trip(&values);
2133        }
2134    }
2135
2136    #[test]
2137    fn units_of_different_widths_in_one_chunk_do_not_read_each_others_leftovers() {
2138        // A decode reuses its buffers from one unit to the next instead of getting a zeroed one
2139        // each time, so a unit that wrote fewer bits than the unit before it would come back with
2140        // the older unit's values in the bits it did not write. Each run of 1024 here needs a
2141        // different width and the widths go up and down, and the last run repeats the first, which
2142        // is the pair that would agree by accident if the reuse were wrong in the obvious way.
2143        //
2144        // The values are random rather than written out because this has to stay one packed chunk
2145        // of six units to be testing anything, and the first version of it was arithmetic and got
2146        // cascaded into a delta of runs where every nested array was under a unit long. That was
2147        // caught by gating a panic on the second unit and rerunning, which this version reaches and
2148        // the old one did not, and the assertion on the shape below is there so it stays reached.
2149        let mut random = Random::new();
2150        let mut values = Vec::new();
2151        for width in [40u32, 3, 61, 1, 17, 40] {
2152            for _ in 0..1024 {
2153                values.push((random.next() & ((1u64 << width) - 1)) as i64);
2154            }
2155        }
2156        let bytes = encode(&values).unwrap();
2157        let described = describe(&bytes).unwrap();
2158        assert!(described.starts_with("FOR+BITPACK"), "expected one packed chunk, got {described}");
2159        assert_eq!(decode(&bytes).unwrap(), values, "{described}");
2160    }
2161
2162    #[test]
2163    fn a_cascade_decodes_the_same_through_a_shared_scratch_as_through_its_own() {
2164        // The scratch is threaded through the recursion, so a dictionary of deltas is three nested
2165        // decodes sharing one set of buffers. Nothing in the nesting arms holds a buffer across the
2166        // call it makes, and this is the test that says so: a chunk long enough to cascade and wide
2167        // enough to bit pack at more than one level, decoded whole.
2168        let mut values = Vec::new();
2169        for index in 0..8192i64 {
2170            values.push(1_600_000_000 + index / 4 + (index % 7) * 1_000);
2171        }
2172        let bytes = encode(&values).unwrap();
2173        let described = describe(&bytes).unwrap();
2174        assert!(described.contains('('), "expected a cascade, got {described}");
2175        assert_eq!(decode(&bytes).unwrap(), values, "{described}");
2176    }
2177
2178    #[test]
2179    fn selected_positions_agree_with_a_full_decode_for_every_kind_with_a_point_form() {
2180        let positions = [0, 1, 17, 1023, 1024, 4097, 8191];
2181        let packed: Vec<i64> = (0..8192).map(|index| index * 31 % 1_000_003).collect();
2182        let mut runs = Vec::new();
2183        for run in 0..160i64 {
2184            runs.extend(std::iter::repeat_n(run * 13, (run as usize % 71) + 2));
2185        }
2186        runs.resize(8192, -7);
2187        let strided: Vec<i64> = (0..8192).map(|index| 500 + index * 7 % 5003 * 100).collect();
2188        let coded: Vec<i64> =
2189            (0..8192).map(|index| [-9_000_000_000, 3, 77, 1 << 40][index % 4]).collect();
2190
2191        for (kind, values) in [
2192            (Kind::Packed, packed),
2193            (Kind::Rle, runs),
2194            (Kind::Strided, strided),
2195            (Kind::Dict, coded),
2196        ] {
2197            let bytes = encode_only(kind, &values).unwrap().expect("encoding applies");
2198            let selected = decode_selected(&bytes, &positions).unwrap();
2199            let expected = positions.iter().map(|&position| values[position]).collect::<Vec<_>>();
2200            assert_eq!(selected, expected, "{}", kind.name());
2201            let shape = describe(&bytes).unwrap();
2202            let simple = !shape.contains("RLE") && !shape.contains("DELTA");
2203            assert_eq!(pointed(&bytes), simple, "{shape}");
2204        }
2205    }
2206
2207    #[test]
2208    fn selected_positions_must_be_ordered_and_inside_the_chunk() {
2209        let bytes = encode_only(Kind::Packed, &(0..2048).collect::<Vec<_>>())
2210            .unwrap()
2211            .expect("packed applies");
2212        assert!(decode_selected(&bytes, &[7, 7]).is_err());
2213        assert!(decode_selected(&bytes, &[8, 3]).is_err());
2214        assert!(decode_selected(&bytes, &[2048]).is_err());
2215    }
2216
2217    #[test]
2218    fn a_partial_unit_costs_its_own_values_and_not_a_whole_unit() {
2219        // Three values that need 40 bits each. In the transposed layout a unit is 1024 values
2220        // whether it holds them or not, so this would be 5 KB, and every nested array in a cascade
2221        // is this short. It is 15 bytes of payload and 14 of header.
2222        let values = vec![1i64 << 39, (1 << 39) + 7, 1 << 38];
2223        let bytes = encode_only(Kind::Packed, &values).unwrap().unwrap();
2224        assert_eq!(bytes.len(), 5 + 9 + 15);
2225        assert_eq!(decode(&bytes).unwrap(), values);
2226    }
2227
2228    #[test]
2229    fn the_frame_of_reference_is_per_unit_and_not_per_chunk() {
2230        // A column that drifts, which is what a timestamp column and a clustered key both do. Each
2231        // unit here spans 1023 and packs at 10 bits, and a base per chunk would pay the 22 bits the
2232        // whole chunk spans on every value in it.
2233        let values: Vec<i64> =
2234            (0..4096i64).map(|index| (index / 1024) * 1_000_000 + (index % 1024)).collect();
2235        let bytes = encode_only(Kind::Packed, &values).unwrap().unwrap();
2236        assert_eq!(describe(&bytes).unwrap(), "FOR+BITPACK[10]");
2237        assert_eq!(decode(&bytes).unwrap(), values);
2238    }
2239
2240    #[test]
2241    fn every_candidate_that_applies_decodes_to_the_input() {
2242        // The chooser only ever hands back the smallest, so without this the other five are only
2243        // tested when they happen to win. Any of them being wrong is a wrong answer that appears
2244        // when a column's distribution shifts.
2245        let mut values = vec![5i64; 3000];
2246        for (index, value) in values.iter_mut().enumerate() {
2247            if index % 500 == 0 {
2248                *value = index as i64;
2249            }
2250        }
2251        let applicable = candidates(&values, 0, &EXHAUSTIVE);
2252        assert!(applicable.len() >= 4, "{applicable:?}");
2253        for kind in applicable {
2254            let bytes = encode_only(kind, &values).unwrap().unwrap();
2255            assert_eq!(decode(&bytes).unwrap(), values, "{}", kind.name());
2256        }
2257    }
2258
2259    /// The test above only asks the kinds `candidates` offered, so between them the two cover the
2260    /// encoders on input the search would give them and nothing else. `encode_only` does not go
2261    /// through `candidates` at all, so every one of its callers can hand an encoder a shape the
2262    /// filter would have refused, and the empty chunk is the shape that used to panic.
2263    #[test]
2264    fn every_kind_that_applies_decodes_to_what_it_was_given() {
2265        let shapes: Vec<Vec<i64>> = vec![
2266            Vec::new(),
2267            vec![5; 1024],
2268            vec![i64::MIN, i64::MAX, 0, -1],
2269            (0..1024).map(|at| at * 7).collect(),
2270            (0..1024).map(|at| at % 17).collect(),
2271            (0..1024).map(|at| if at % 100 == 0 { at } else { 3 }).collect(),
2272            (0..1024).map(|at| -at * 1_000_003).collect(),
2273            (0..1024_i64)
2274                .map(|at| {
2275                    at.wrapping_mul(6_364_136_223_846_793_005)
2276                        .wrapping_add(1_442_695_040_888_963_407)
2277                })
2278                .collect(),
2279        ];
2280        let kinds =
2281            [Kind::Constant, Kind::Packed, Kind::Delta, Kind::Rle, Kind::Dict, Kind::Sparse];
2282        for values in &shapes {
2283            for kind in kinds {
2284                let Some(bytes) = encode_only(kind, values).unwrap() else {
2285                    continue;
2286                };
2287                assert_eq!(
2288                    &decode(&bytes).unwrap(),
2289                    values,
2290                    "{} over {} values",
2291                    kind.name(),
2292                    values.len()
2293                );
2294            }
2295        }
2296    }
2297
2298    #[test]
2299    fn the_chooser_picks_the_smallest_candidate_rather_than_the_first_that_applies() {
2300        let mut values = vec![5i64; 3000];
2301        values[1500] = 9;
2302        let chosen = encode(&values).unwrap();
2303        for (_, size) in candidate_sizes(&values).unwrap() {
2304            assert!(chosen.len() <= size);
2305        }
2306    }
2307
2308    #[test]
2309    fn a_truncated_chunk_is_an_error_and_not_a_panic() {
2310        let bytes = encode(&[1, 2, 3, 4, 5]).unwrap();
2311        for len in 0..bytes.len() {
2312            let error = decode(&bytes[..len]).unwrap_err();
2313            assert!(error.message().contains("chunk"), "{error}");
2314        }
2315    }
2316
2317    #[test]
2318    fn trailing_bytes_are_an_error() {
2319        let mut bytes = encode(&[1, 2, 3]).unwrap();
2320        bytes.push(0);
2321        let error = decode(&bytes).unwrap_err();
2322        assert!(error.message().contains("left over"), "{error}");
2323    }
2324
2325    #[test]
2326    fn an_unknown_tag_is_an_error() {
2327        let error = decode(&[99, 0, 0, 0, 0]).unwrap_err();
2328        assert!(error.message().contains("unknown encoding tag"), "{error}");
2329    }
2330
2331    #[test]
2332    fn a_dictionary_code_outside_the_dictionary_is_an_error() {
2333        // A corrupted or malicious chunk must not index out of bounds, and this is the one place in
2334        // the decoder where a number that came off the disk is used as an index. Built by hand
2335        // rather than by corrupting a real chunk, because a byte offset into an encoding that the
2336        // chooser is free to change is a test that breaks for the wrong reason.
2337        let mut bytes = vec![Kind::Dict.tag()];
2338        put_u32(&mut bytes, 1);
2339        bytes.extend_from_slice(&encode(&[10]).unwrap());
2340        bytes.extend_from_slice(&encode(&[5]).unwrap());
2341        let error = decode(&bytes).unwrap_err();
2342        assert!(error.message().contains("not in the dictionary"), "{error}");
2343    }
2344
2345    #[test]
2346    fn a_negative_run_length_is_an_error() {
2347        // The other number off the disk that the decoder would otherwise trust, and the one that
2348        // would turn into an allocation of nine quintillion values.
2349        let mut bytes = vec![Kind::Rle.tag()];
2350        put_u32(&mut bytes, 4);
2351        bytes.extend_from_slice(&encode(&[7]).unwrap());
2352        bytes.extend_from_slice(&encode(&[-4]).unwrap());
2353        let error = decode(&bytes).unwrap_err();
2354        assert!(error.message().contains("negative"), "{error}");
2355    }
2356
2357    /// A run that ends past the chunk it is in is an error and not a write past the end.
2358    #[test]
2359    fn a_run_that_runs_past_its_chunk_is_an_error() {
2360        // The decode writes a fixed eight values per run and moves on by the run's own length, so
2361        // the buffer carries eight values of slack and a run that claims more rows than the chunk
2362        // holds would be the one way to reach past it. It is refused before the write rather than
2363        // caught by the count afterwards.
2364        let mut bytes = vec![Kind::Rle.tag()];
2365        put_u32(&mut bytes, 4);
2366        bytes.extend_from_slice(&encode(&[7]).unwrap());
2367        bytes.extend_from_slice(&encode(&[9]).unwrap());
2368        let error = decode(&bytes).unwrap_err();
2369        assert!(error.message().contains("past its chunk"), "{error}");
2370    }
2371
2372    /// Runs of every length around the eight that a run is written in, in one chunk.
2373    #[test]
2374    fn runs_shorter_and_longer_than_the_width_they_are_written_in_all_come_back() {
2375        // A run of one, several shorter than eight, one of exactly eight and two longer, with the
2376        // shortest run last so that the surplus of the write before it has nothing after it to be
2377        // overwritten by. The values differ from each other, because a surplus that was left in
2378        // place would be invisible against a neighbour holding the same value.
2379        let lengths = [1, 3, 7, 8, 9, 40, 2, 1];
2380        let mut values = Vec::new();
2381        for (at, length) in lengths.iter().enumerate() {
2382            let value = i64::try_from(at).expect("eight runs") * 1000 - 3;
2383            values.extend(std::iter::repeat_n(value, *length));
2384        }
2385        let bytes = encode(&values).expect("encodes");
2386        assert_eq!(decode(&bytes).expect("decodes"), values, "runs around the write width");
2387        // And the same rows a run at a time, which is the run length encoder's worst case and the
2388        // shape a column with no runs in it decodes as.
2389        let singles: Vec<i64> = (0..300).map(|index| index * 7 % 11).collect();
2390        let bytes = encode(&singles).expect("encodes");
2391        assert_eq!(decode(&bytes).expect("decodes"), singles, "no run longer than one");
2392    }
2393
2394    #[test]
2395    fn the_cascade_depth_is_bounded() {
2396        // Without the limit a chooser that finds a dictionary of a dictionary of a dictionary would
2397        // recurse until the values ran out, and the encode time of a wide column would be a
2398        // surprise rather than a number.
2399        let values: Vec<i64> = (0..50_000).map(|index| (index / 100) % 250).collect();
2400        let bytes = round_trip(&values);
2401        let shape = describe(&bytes).unwrap();
2402        let depth = shape.matches('(').count();
2403        assert!(depth <= MAX_DEPTH as usize, "{shape} is {depth} deep");
2404    }
2405
2406    #[test]
2407    fn candidate_sizes_reports_what_the_chooser_looked_at() {
2408        let values: Vec<i64> = (0..5000).map(|index| index % 17 * 1000).collect();
2409        let sizes = candidate_sizes(&values).unwrap();
2410        assert!(sizes.iter().any(|(kind, _)| *kind == Kind::Dict));
2411        assert!(sizes.iter().any(|(kind, _)| *kind == Kind::Packed));
2412        assert!(sizes.iter().all(|(_, size)| *size > 0));
2413    }
2414
2415    #[test]
2416    fn a_chunk_can_be_read_from_the_front_of_a_longer_buffer() {
2417        // What a string column does. It writes an integer chunk of lengths into the middle of its
2418        // own body and has to find the end of it again on the way back.
2419        let first = encode(&[1, 2, 3]).unwrap();
2420        let second: Vec<i64> = (0..3000).map(|index| index % 11).collect();
2421        let second_bytes = encode(&second).unwrap();
2422        let mut joined = first.clone();
2423        joined.extend_from_slice(&second_bytes);
2424        joined.extend_from_slice(b"and then something else");
2425
2426        let (values, used) = decode_prefix(&joined).unwrap();
2427        assert_eq!(values, vec![1, 2, 3]);
2428        assert_eq!(used, first.len());
2429        let (more, used_again) = decode_prefix(&joined[used..]).unwrap();
2430        assert_eq!(more, second);
2431        assert_eq!(used_again, second_bytes.len());
2432
2433        let (text, described) = describe_prefix(&joined).unwrap();
2434        assert_eq!(described, first.len());
2435        assert_eq!(text, describe(&first).unwrap());
2436    }
2437
2438    #[test]
2439    fn a_truncated_chunk_is_still_an_error_when_read_as_a_prefix() {
2440        let bytes = encode(&(0..2000).collect::<Vec<i64>>()).unwrap();
2441        for len in 0..bytes.len() {
2442            assert!(decode_prefix(&bytes[..len]).is_err(), "{len} bytes decoded");
2443        }
2444    }
2445
2446    /// Every kind decodes into a narrow type as the same values [`decode`] gives, whichever kind
2447    /// the chunk is at the top.
2448    #[test]
2449    fn decoding_into_a_narrow_type_agrees_with_the_wide_decoder() {
2450        let columns: Vec<Vec<i64>> = vec![
2451            vec![7; 3000],
2452            (0..3000).map(|i| (i * 37) % 200 - 100).collect(),
2453            (0..3000).map(|i| if i % 97 == 0 { i % 50 } else { 0 }).collect(),
2454            (0..3000).map(|i| i / 250).collect(),
2455            (0..3000).map(|i| i * 3 + 11).collect(),
2456            (0..3000).map(|i| [5, -9, 120][i as usize % 3]).collect(),
2457        ];
2458        let kinds = [
2459            Kind::Constant,
2460            Kind::Packed,
2461            Kind::Delta,
2462            Kind::Rle,
2463            Kind::Dict,
2464            Kind::Sparse,
2465            Kind::Strided,
2466        ];
2467        for values in &columns {
2468            for kind in kinds {
2469                let Some(bytes) = encode_only(kind, values).unwrap() else { continue };
2470                let wide = decode(&bytes).unwrap();
2471                let as_i16: Vec<i64> =
2472                    decode_as::<i16>(&bytes).unwrap().into_iter().map(i64::from).collect();
2473                let as_i32: Vec<i64> =
2474                    decode_as::<i32>(&bytes).unwrap().into_iter().map(i64::from).collect();
2475                assert_eq!(as_i16, wide, "{kind:?} as i16");
2476                assert_eq!(as_i32, wide, "{kind:?} as i32");
2477                assert_eq!(decode_as::<i64>(&bytes).unwrap(), wide, "{kind:?} as i64");
2478            }
2479            let chosen = encode(values).unwrap();
2480            let narrow: Vec<i64> =
2481                decode_as::<i16>(&chosen).unwrap().into_iter().map(i64::from).collect();
2482            assert_eq!(narrow, decode(&chosen).unwrap(), "the chosen cascade as i16");
2483        }
2484    }
2485
2486    /// A value is refused exactly where `TryFrom` refuses it, at both edges of every type.
2487    #[test]
2488    fn decoding_into_a_narrow_type_takes_what_fits_and_refuses_what_does_not() {
2489        fn check<T: Lane + TryFrom<i64> + PartialEq + std::fmt::Debug>(edges: [i64; 2]) {
2490            for edge in edges {
2491                for value in [edge - 1, edge, edge + 1] {
2492                    for kind in [Kind::Constant, Kind::Packed, Kind::Rle, Kind::Sparse] {
2493                        let values = [value, value, edges[0].max(0).min(edges[1]), value];
2494                        let Some(bytes) = encode_only(kind, &values).unwrap() else { continue };
2495                        let fits = values.iter().all(|&value| T::try_from(value).is_ok());
2496                        assert_eq!(decode_as::<T>(&bytes).is_ok(), fits, "{value} {kind:?}");
2497                    }
2498                }
2499            }
2500        }
2501        check::<i8>([-128, 127]);
2502        check::<u8>([0, 255]);
2503        check::<i16>([-32_768, 32_767]);
2504        check::<u16>([0, 65_535]);
2505        check::<i32>([i64::from(i32::MIN), i64::from(i32::MAX)]);
2506        check::<u32>([0, i64::from(u32::MAX)]);
2507    }
2508
2509    /// A packed block whose width reaches past the type while every value in it still fits, which
2510    /// is the block that has to be checked a value at a time rather than by its two ends.
2511    #[test]
2512    fn a_packed_block_wider_than_its_type_still_decodes_when_its_values_fit() {
2513        let values: Vec<i64> = (0..1500).map(|i| if i % 2 == 0 { -5 } else { 32_767 }).collect();
2514        let bytes = encode_only(Kind::Packed, &values).unwrap().expect("packing always applies");
2515        let narrow: Vec<i64> =
2516            decode_as::<i16>(&bytes).unwrap().into_iter().map(i64::from).collect();
2517        assert_eq!(narrow, values);
2518        let over: Vec<i64> = values.iter().map(|&value| value + 1).collect();
2519        let bytes = encode_only(Kind::Packed, &over).unwrap().expect("packing always applies");
2520        assert!(decode_as::<i16>(&bytes).is_err(), "32768 is not an i16");
2521    }
2522}