Skip to main content

rudb_native/
lib.rs

1//! Rudb's single-file columnar snapshot format.
2//!
3//! A committed directory names independently readable column pages. The first version handles
4//! scalar columns and one table; the file header already has two generation slots so an unfinished
5//! replacement directory cannot hide the last complete one.
6//!
7//! # Parts and stripes
8//!
9//! A part is one appended chunk, which is a thousand rows, and it is the unit a scan decodes and
10//! hands to the pipeline. A stripe is sixty four parts, and it is the unit the directory describes
11//! and the unit the file is laid out in: one page per column per stripe, holding that column's
12//! sixty four part payloads end to end.
13//!
14//! The two are separate because they are sized by different pressures. A part wants to be small
15//! because it is a vector and vectors live in cache. A stripe wants to be large because everything
16//! the directory holds is per stripe and the directory is one buffer that has to be read and
17//! decoded before a single row can be answered. A hundred million rows of the hundred and five
18//! column ClickBench table is ninety seven thousand parts, and a directory with a page entry and a
19//! pair of bounds per part per column is several hundred megabytes, which is what made that load
20//! fail before this split existed. Sixty four parts to a stripe divides that by sixty four.
21//!
22//! Where the parts of a page start is not in the directory either, for the same reason. Each
23//! stripe writes one index page holding a length and a checksum per part per column, and a reader
24//! preads the sixty four entries belonging to the column it wants. A scan reads the whole column
25//! page once and slices it; a sparse row fetch reads the index entries and then only the part it
26//! needs.
27
28#![forbid(unsafe_code)]
29
30use std::cmp::Ordering;
31use std::collections::{HashMap, VecDeque};
32use std::fs::{File, OpenOptions};
33use std::io::{Read, Seek, SeekFrom};
34use std::mem::{size_of, size_of_val};
35use std::path::Path;
36use std::sync::atomic::{AtomicUsize, Ordering as Atomic};
37use std::sync::{Arc, Mutex, OnceLock};
38
39use rudb_common::bounds::{Bound, Op, scaled_as};
40use rudb_common::{Error, Field, LogicalType, Result, Value};
41use rudb_encoding::{bitpack, chooser, integer, string};
42use rudb_storage::sieve::Sieve;
43use rudb_storage::{Probe, Range, Zone};
44use rudb_vector::string::StringColumn;
45use rudb_vector::validity::Validity;
46use rudb_vector::{Buffer, Chunk, Data, Packed, TextSource, Vector};
47
48const MAGIC: &[u8; 8] = b"RUDBNV10";
49const DIRECTORY: &[u8; 8] = b"RUDBDI10";
50const FORMAT: u32 = 21;
51const HEADER: u64 = 80;
52const SLOT_BYTES: usize = 28;
53const MAX_PAGE: usize = 256 * 1024 * 1024;
54const MAX_DIRECTORY: usize = 128 * 1024 * 1024;
55const FREQUENCIES: &[u8; 8] = b"RUDBFQ2\0";
56const FREQUENCY_CANDIDATES: usize = 32_768;
57const FREQUENCY_ENTRIES: usize = 512;
58const FREQUENCY_BUILD_RANK: usize = 10;
59const FREQUENCY_ORDINALS: usize = 65_536;
60/// The most threads the two per column passes at the end of a commit are spread over.
61///
62/// A table like `hits` has ninety numeric columns, so on a machine with more cores than this the
63/// cap is what decides how long the frequencies take rather than the columns are. It is here at all
64/// because each worker holds a candidate table and a decoded part, and a hundred of those at once
65/// on a narrow machine would be worse than waiting.
66const MAX_FREQUENCY_WORKERS: usize = 32;
67
68/// The most threads one stripe's encode is spread over.
69///
70/// Higher than the frequency cap because this is the load itself rather than a pass at the end of
71/// it, and the work is one column of sixty four parts, which is large enough that a thread that
72/// takes one is not a thread that was started for nothing. A machine with more cores than this has
73/// the rest of them on the Parquet read, which is still one thread and is the other half of #808.
74const MAX_ENCODE_WORKERS: usize = 32;
75
76/// The most bytes one column of one part may spend on a membership sieve.
77///
78/// A part is a thousand rows, so a filter sized for every one of them being distinct is about
79/// thirteen hundred bytes and this never binds in practice. It is here so that a part that somehow
80/// arrives much wider than a vector cannot put an unbounded index in the file. What does bind is the
81/// rule in `encode_column` that a sieve may not be as large as the part it indexes, which is a cap
82/// per column rather than one number for the whole file.
83const SIEVE_BUDGET: usize = 8 * 1024;
84
85/// The most bytes one end of a per part range may spend on a string.
86///
87/// A bound is allowed to be wider than the truth and never narrower, so a long string is cut down to
88/// this many bytes for the low end and cut down and then stepped up for the high end. The reason for
89/// a cap at all is that there are nine hundred and seventy four parts of a hundred and five columns
90/// in a million rows of ClickBench and `URL` runs to hundreds of bytes, so keeping every end whole
91/// would put more in the directory than the skipping is worth. Twenty four bytes is past the point
92/// where two URLs of the same site still look alike.
93const PART_BOUND_BYTES: usize = 24;
94
95fn io(error: std::io::Error) -> Error {
96    Error::io(error.to_string())
97}
98
99fn invalid(message: &str) -> Error {
100    Error::invalid_input(format!("invalid rudb native file: {message}"))
101}
102
103/// Adds a sequence of byte counts without an overflow the caller has to think about.
104fn sum(counts: impl Iterator<Item = u64>) -> u64 {
105    counts.fold(0, u64::saturating_add)
106}
107
108/// One column's span out of a per column list, or zero when the list is shorter than the column.
109fn span_bytes(spans: &[Span], at: usize) -> u64 {
110    spans.get(at).map_or(0, |span| u64::from(span.length))
111}
112
113/// One column's page out of a per column list, or zero when that column has no page at all.
114fn page_bytes(pages: &[Option<Page>], at: usize) -> u64 {
115    pages.get(at).and_then(Option::as_ref).map_or(0, Page::bytes)
116}
117
118fn checksum(bytes: &[u8]) -> u64 {
119    const P1: u64 = 11_400_714_785_074_694_791;
120    const P2: u64 = 14_029_467_366_897_019_727;
121    const P3: u64 = 1_609_587_929_392_839_161;
122    const P4: u64 = 9_650_029_242_287_828_579;
123    const P5: u64 = 2_870_177_450_012_600_261;
124    let round = |state: u64, word: u64| {
125        state.wrapping_add(word.wrapping_mul(P2)).rotate_left(31).wrapping_mul(P1)
126    };
127    let merge = |state: u64, lane: u64| (state ^ round(0, lane)).wrapping_mul(P1).wrapping_add(P4);
128    let word =
129        |at: usize| u64::from_le_bytes(bytes[at..at + 8].try_into().expect("eight checksum bytes"));
130
131    let mut at = 0;
132    let mut hash = if bytes.len() >= 32 {
133        let mut one = P1.wrapping_add(P2);
134        let mut two = P2;
135        let mut three = 0;
136        let mut four = 0_u64.wrapping_sub(P1);
137        while at + 32 <= bytes.len() {
138            one = round(one, word(at));
139            two = round(two, word(at + 8));
140            three = round(three, word(at + 16));
141            four = round(four, word(at + 24));
142            at += 32;
143        }
144        let combined = one
145            .rotate_left(1)
146            .wrapping_add(two.rotate_left(7))
147            .wrapping_add(three.rotate_left(12))
148            .wrapping_add(four.rotate_left(18));
149        merge(merge(merge(merge(combined, one), two), three), four)
150    } else {
151        P5
152    };
153    hash = hash.wrapping_add(bytes.len() as u64);
154    while at + 8 <= bytes.len() {
155        hash ^= round(0, word(at));
156        hash = hash.rotate_left(27).wrapping_mul(P1).wrapping_add(P4);
157        at += 8;
158    }
159    if at + 4 <= bytes.len() {
160        let tail = u32::from_le_bytes(bytes[at..at + 4].try_into().expect("four checksum bytes"));
161        hash ^= u64::from(tail).wrapping_mul(P1);
162        hash = hash.rotate_left(23).wrapping_mul(P2).wrapping_add(P3);
163        at += 4;
164    }
165    while at < bytes.len() {
166        hash ^= u64::from(bytes[at]).wrapping_mul(P5);
167        hash = hash.rotate_left(11).wrapping_mul(P1);
168        at += 1;
169    }
170    hash ^= hash >> 33;
171    hash = hash.wrapping_mul(P2);
172    hash ^= hash >> 29;
173    hash = hash.wrapping_mul(P3);
174    hash ^ (hash >> 32)
175}
176
177#[derive(Debug, Clone, Copy)]
178struct Slot {
179    offset: u64,
180    length: u32,
181    generation: u64,
182    hash: u64,
183}
184
185impl Slot {
186    fn bytes(self) -> [u8; SLOT_BYTES] {
187        let mut result = [0; SLOT_BYTES];
188        result[..8].copy_from_slice(&self.offset.to_le_bytes());
189        result[8..12].copy_from_slice(&self.length.to_le_bytes());
190        result[12..20].copy_from_slice(&self.generation.to_le_bytes());
191        result[20..28].copy_from_slice(&self.hash.to_le_bytes());
192        result
193    }
194
195    fn read(bytes: &[u8]) -> Self {
196        Self {
197            offset: u64::from_le_bytes(bytes[..8].try_into().expect("eight bytes")),
198            length: u32::from_le_bytes(bytes[8..12].try_into().expect("four bytes")),
199            generation: u64::from_le_bytes(bytes[12..20].try_into().expect("eight bytes")),
200            hash: u64::from_le_bytes(bytes[20..28].try_into().expect("eight bytes")),
201        }
202    }
203}
204
205#[derive(Debug, Clone, Copy)]
206struct Page {
207    offset: u64,
208    length: u32,
209    hash: u64,
210}
211
212impl Page {
213    /// How much of the file this page takes, for [`Reader::layout`].
214    fn bytes(&self) -> u64 {
215        u64::from(self.length)
216    }
217}
218
219#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
220enum FrequencyValue {
221    Null,
222    Integer(i128),
223    Code(u32),
224}
225
226#[derive(Debug, Clone)]
227struct FrequencyEntry {
228    value: FrequencyValue,
229    count: u64,
230}
231
232/// Exact leading frequencies for one column.
233///
234/// Values outside `entries` occur at most `omitted_max` times. This lets a count-descending TopN
235/// use the synopsis only when its last winner is strictly above every omitted value.
236#[derive(Debug, Clone)]
237struct FrequencySummary {
238    entries: Vec<FrequencyEntry>,
239    omitted_max: u64,
240    ordinals: Vec<u64>,
241}
242
243/// Sparse row ordinals covered by a numeric frequency candidate set.
244#[derive(Debug, Clone, PartialEq, Eq)]
245pub struct FrequencyOccurrences {
246    /// Upper bound for the frequency of every value absent from the fetched rows.
247    pub omitted_max: u64,
248    /// Table-wide row ordinals in ascending order.
249    pub ordinals: Vec<u64>,
250}
251
252/// Where one column's page for one stripe sits in the file.
253///
254/// A column page has no checksum of its own because every part inside it carries one, and the
255/// stripe's index page holds those. Checking a part on the way out of the page covers exactly the
256/// bytes a reader is about to decode, and covers them once whether the reader took the whole page
257/// or pulled one part out of the middle of it.
258#[derive(Debug, Clone, Copy, Default)]
259struct Span {
260    offset: u64,
261    length: u32,
262}
263
264/// One independently readable stripe of a table.
265#[derive(Debug, Clone)]
266pub struct Stripe {
267    rows: usize,
268    /// Rows in each part, in source order. Kept in the directory so that mapping a row ordinal to a
269    /// part, which every sparse fetch does, never reads the file.
270    parts: Vec<u32>,
271    /// The index page: one section per column, holding a length and a checksum for every part and
272    /// then a checksum of the section itself, so that a reader can pread one column's section and
273    /// still know it is intact.
274    index: Span,
275    pages: Vec<Span>,
276    memberships: Vec<Option<Page>>,
277    /// One page per column holding the membership sieve of every part of the stripe, for the
278    /// columns that have one. A column whose parts all declined a sieve has no page at all.
279    sieves: Vec<Option<Page>>,
280    /// One page per column holding the two ends and the null count of every part of the stripe.
281    ///
282    /// The stripe's own `zone` below covers sixty four times as many rows, and on a column that is
283    /// not the one the rows are ordered by that is the difference between skipping half the file and
284    /// skipping all but three percent of it. On ClickBench 24 the cutoff the answer settles at
285    /// leaves eight stripes of sixteen alive and thirty parts of nine hundred and seventy four.
286    ///
287    /// A page per column rather than one page for the stripe, so that a query that compares one
288    /// column reads the ends of that column and not of the hundred and four beside it. Read lazily
289    /// for the same reason, like the sieves.
290    part_ranges: Vec<Option<Page>>,
291    zone: Zone,
292}
293
294impl Stripe {
295    /// Number of rows in this stripe.
296    #[must_use]
297    pub fn rows(&self) -> usize {
298        self.rows
299    }
300
301    /// Number of parts in this stripe.
302    #[must_use]
303    pub fn parts(&self) -> usize {
304        self.parts.len()
305    }
306}
307
308/// The committed table directory.
309#[derive(Debug, Clone)]
310pub struct Table {
311    name: String,
312    fields: Vec<Field>,
313    stripes: Vec<Stripe>,
314    rows: usize,
315    dictionaries: Vec<Option<Page>>,
316    frequencies: Vec<Option<FrequencySummary>>,
317    /// How many distinct values each column holds, for the columns that know.
318    ///
319    /// A dictionary entry is made the first time a value is seen and nothing ever removes one, so
320    /// the size of the dictionary is the number of distinct values in the column. That is the whole
321    /// story for a column with no null in it, and the wrong number by one for a column with a null
322    /// in it, because a null row is written as the code for the empty string and makes an entry the
323    /// dictionary would not otherwise have. The writer knows which case it is, since it counts the
324    /// non-null rows that use each code while it builds the frequency summary, and the reader cannot
325    /// work it out from the dictionary alone. So the writer settles it here.
326    distincts: Vec<Option<u64>>,
327}
328
329impl Table {
330    /// The SQL table name held by this snapshot.
331    #[must_use]
332    pub fn name(&self) -> &str {
333        &self.name
334    }
335
336    /// Columns in their SQL order.
337    #[must_use]
338    pub fn fields(&self) -> &[Field] {
339        &self.fields
340    }
341
342    /// Committed row count.
343    #[must_use]
344    pub fn rows(&self) -> usize {
345        self.rows
346    }
347
348    /// Independently readable stripes.
349    #[must_use]
350    pub fn stripes(&self) -> &[Stripe] {
351        &self.stripes
352    }
353}
354
355/// Where one column's bytes went, taken from the directory rather than by reading pages.
356#[derive(Debug, Clone)]
357pub struct ColumnLayout {
358    /// The column's name, so a report does not have to carry the field list beside this.
359    pub name: String,
360    /// The type, spelled the way the catalog spells it.
361    pub kind: String,
362    /// Every stripe's page of this column added up, which is the encoded data itself.
363    pub pages: u64,
364    /// Every stripe's exact code membership page for this column.
365    pub memberships: u64,
366    /// Every stripe's membership sieve page for this column.
367    pub sieves: u64,
368    /// Every stripe's per part range page for this column.
369    pub part_ranges: u64,
370    /// The table wide dictionary of this column, if it has one.
371    pub dictionary: u64,
372}
373
374impl ColumnLayout {
375    /// Everything this column costs, which is what the file would lose if the column went.
376    #[must_use]
377    pub fn total(&self) -> u64 {
378        self.pages
379            .saturating_add(self.memberships)
380            .saturating_add(self.sieves)
381            .saturating_add(self.part_ranges)
382            .saturating_add(self.dictionary)
383    }
384}
385
386/// Where a whole file's bytes went.
387///
388/// Every number here comes out of the committed directory, so taking it costs one directory read
389/// however large the file is. That is the point: a 45 GB table has to be able to say where it went
390/// without being read, or nobody will ask.
391///
392/// The parts that are not a column are kept apart rather than shared out over the columns. The
393/// stripe index page holds a section per column and could be split, and the directory and the
394/// header cannot be, so splitting one of the three and not the others would read as if the columns
395/// accounted for everything. They do not, and the gap is the thing worth looking at.
396#[derive(Debug, Clone)]
397pub struct Layout {
398    /// The size of the file on disk.
399    pub file: u64,
400    /// Committed rows.
401    pub rows: usize,
402    /// Committed stripes.
403    pub stripes: usize,
404    /// Committed parts, which is how many chunks a scan reads.
405    pub parts: usize,
406    /// One entry per column, in the table's column order.
407    pub columns: Vec<ColumnLayout>,
408    /// Every stripe's index page, which carries a length and a checksum for every part of every
409    /// column and is charged per stripe rather than per column.
410    pub indexes: u64,
411    /// The committed directory itself, the one that was read to build this.
412    pub directory: u64,
413    /// The fixed header, which holds the magic, the format and the two directory slots.
414    pub header: u64,
415}
416
417impl Layout {
418    /// Everything the columns cost together.
419    #[must_use]
420    pub fn columns_total(&self) -> u64 {
421        self.columns.iter().map(ColumnLayout::total).fold(0, u64::saturating_add)
422    }
423
424    /// What the file holds that this does not account for.
425    ///
426    /// A committed file is written once and never rewritten in place, so an earlier directory and
427    /// the pages of an earlier snapshot are still in it. That is the honest place for them: they
428    /// are bytes on disk that no column owns.
429    #[must_use]
430    pub fn unaccounted(&self) -> u64 {
431        self.file
432            .saturating_sub(self.columns_total())
433            .saturating_sub(self.indexes)
434            .saturating_sub(self.directory)
435            .saturating_sub(self.header)
436    }
437}
438
439/// Appends pages and commits a new directory for one table.
440#[derive(Debug)]
441struct GlobalDictionary {
442    primary: HashMap<u64, u32>,
443    collisions: HashMap<u64, Vec<u32>>,
444    offsets: Vec<u32>,
445    payload: Vec<u8>,
446    counts: Vec<u64>,
447    nulls: u64,
448}
449
450impl GlobalDictionary {
451    fn new() -> Self {
452        Self {
453            primary: HashMap::new(),
454            collisions: HashMap::new(),
455            offsets: vec![0],
456            payload: Vec::new(),
457            counts: Vec::new(),
458            nulls: 0,
459        }
460    }
461
462    fn bytes(&self, code: u32) -> Option<&[u8]> {
463        let start = *self.offsets.get(code as usize)? as usize;
464        let end = *self.offsets.get(code as usize + 1)? as usize;
465        self.payload.get(start..end)
466    }
467
468    fn code(&mut self, text: &str) -> Result<u32> {
469        let hash = checksum(text.as_bytes());
470        if let Some(&code) = self.primary.get(&hash) {
471            if self.bytes(code) == Some(text.as_bytes()) {
472                return Ok(code);
473            }
474            if let Some(codes) = self.collisions.get(&hash) {
475                if let Some(code) =
476                    codes.iter().copied().find(|&code| self.bytes(code) == Some(text.as_bytes()))
477                {
478                    return Ok(code);
479                }
480            }
481            let code = self.insert(text)?;
482            self.collisions.entry(hash).or_default().push(code);
483            return Ok(code);
484        }
485        let code = self.insert(text)?;
486        self.primary.insert(hash, code);
487        Ok(code)
488    }
489
490    fn insert(&mut self, text: &str) -> Result<u32> {
491        let code = u32::try_from(self.offsets.len() - 1)
492            .map_err(|_| invalid("global dictionary has too many values"))?;
493        self.payload.extend_from_slice(text.as_bytes());
494        self.offsets.push(
495            u32::try_from(self.payload.len())
496                .map_err(|_| invalid("global dictionary payload exceeds 4 GiB"))?,
497        );
498        self.counts.push(0);
499        Ok(code)
500    }
501
502    /// This dictionary's values in sorted order, each as the first eight bytes of the value and the
503    /// code that holds it, so entry `rank` describes the value that sits at `rank` when the values
504    /// are sorted by their bytes.
505    ///
506    /// Codes themselves stay in first appearance order, which is what lets the writer hand one out
507    /// the moment it sees a value rather than waiting for the last stripe, and which also keeps a
508    /// stripe's codes close together because the data is clustered. This is what puts the values
509    /// back in order for anything that needs it, and it is separate from the codes so that getting
510    /// it costs a sort of the distinct values at the end rather than a rewrite of every code page.
511    ///
512    /// The sort compares the first eight bytes as one integer before it compares the values, which
513    /// settles almost every pair without touching the payload. Padding with zero on the right is
514    /// order preserving for byte strings, because a shorter value differs from a longer one that
515    /// starts the same way at a position where the shorter one has run out, and zero is below every
516    /// byte that could be there. A pair the head cannot settle falls through to the bytes.
517    ///
518    /// The heads are kept rather than thrown away once the sort is over, because a reader searching
519    /// this order wants exactly the same comparison and for exactly the same reason. Eight bytes an
520    /// entry of file is what buys a binary search that reads no values at all in the ordinary case.
521    fn ranked(&self) -> Vec<(u64, u32)> {
522        let count = self.offsets.len() - 1;
523        let mut ranked = (0..count)
524            .map(|code| {
525                let code = code as u32;
526                (head(self.bytes(code).unwrap_or_default()), code)
527            })
528            .collect::<Vec<_>>();
529        ranked.sort_unstable_by(|left, right| {
530            left.0.cmp(&right.0).then_with(|| self.bytes(left.1).cmp(&self.bytes(right.1)))
531        });
532        ranked
533    }
534
535    fn observe(&mut self, code: u32, null: bool) -> Result<()> {
536        if null {
537            self.nulls = self.nulls.saturating_add(1);
538            return Ok(());
539        }
540        let count = self
541            .counts
542            .get_mut(code as usize)
543            .ok_or_else(|| invalid("global dictionary count code is out of range"))?;
544        *count = count.saturating_add(1);
545        Ok(())
546    }
547}
548
549/// Appends pages and commits a new directory for one table.
550#[derive(Debug)]
551pub struct Writer {
552    file: File,
553    /// Where the next write goes, counted here rather than asked of the file.
554    ///
555    /// The file's own cursor is not ours. Building the numeric frequencies reads pages back through
556    /// [`read_at`], and a positional read is only positional about where it reads from: `pread`
557    /// leaves the cursor alone, and the call Windows has for it moves the cursor to the end of what
558    /// it read. A writer that asked the file where it was would then write the directory over a
559    /// page it had already written, which is what it did.
560    at: u64,
561    table: Table,
562    generation: u64,
563    /// The first and the last source position in every stripe, in the order the stripes were
564    /// written.
565    order: Vec<((u64, u64), (u64, u64))>,
566    next_order: u64,
567    dictionaries: Vec<Option<GlobalDictionary>>,
568    pending: Vec<PendingChunk>,
569}
570
571/// A chunk that has arrived and is waiting for the rest of its stripe.
572///
573/// The rows are kept rather than the pages they encode to, which is the whole of #808's first half.
574/// Encoding on arrival put every column of every part on the thread that called `append_at`, and
575/// that thread is the only one the load has. Encoding at the flush instead means a stripe's worth
576/// of work is on the table at once, and a stripe splits by column into a hundred and five pieces
577/// that share nothing.
578#[derive(Debug)]
579struct PendingChunk {
580    order: (u64, u64),
581    chunk: Chunk,
582}
583
584/// One column's share of a stripe, which is what one encode worker produces.
585///
586/// Indexed by part, so a stripe is a column of these and the write loop reads down one of them.
587/// That is also the order the loop wanted: `flush_pending` walks a column at a time and lays its
588/// parts next to each other, and it used to reach across a row of parts to do it.
589#[derive(Debug)]
590struct ColumnStripe {
591    pages: Vec<Vec<u8>>,
592    codes: Vec<Option<Vec<u32>>>,
593    sieves: Vec<Option<Sieve>>,
594    ranges: Vec<Range>,
595}
596
597/// Roughly what encoding a column of this type costs, for ordering the encode queue.
598///
599/// Only the order matters and only roughly. A string column hashes and copies every value into a
600/// dictionary and is in a different class from everything else, and among the fixed widths the wide
601/// ones carry more bytes through the cascade than the narrow ones. Anything finer than that would
602/// be a cost model, and the queue already absorbs a wrong guess: it only has to avoid finishing on
603/// a column nobody else can help with.
604fn weight(ty: &LogicalType) -> usize {
605    match ty {
606        LogicalType::Varchar | LogicalType::Blob => 64,
607        LogicalType::BigInt
608        | LogicalType::UBigInt
609        | LogicalType::Timestamp
610        | LogicalType::Double
611        | LogicalType::Decimal { .. } => 8,
612        LogicalType::Integer | LogicalType::UInteger | LogicalType::Date | LogicalType::Float => 4,
613        LogicalType::SmallInt | LogicalType::USmallInt => 2,
614        _ => 1,
615    }
616}
617
618/// Parts in one stripe.
619///
620/// Sixty four thousand rows is the smallest stripe that keeps the ClickBench directory in single
621/// digit megabytes at a hundred million rows, and it puts a four byte column's page at a quarter of
622/// a megabyte, which is the size a sequential read wants. Larger stripes buy a smaller directory
623/// and cost a sparse fetch, which has to read a page index before it can reach one part.
624pub const STRIPE_PARTS: usize = 64;
625
626/// Bytes one part takes in a stripe's index page: four for the length, eight for the checksum.
627const INDEX_ENTRY: usize = size_of::<u32>() + size_of::<u64>();
628
629/// Bytes one column's section of a stripe's index page takes, including its own trailing checksum.
630fn index_section(parts: usize) -> Result<usize> {
631    parts
632        .checked_mul(INDEX_ENTRY)
633        .and_then(|bytes| bytes.checked_add(size_of::<u64>()))
634        .ok_or_else(|| invalid("index page length overflow"))
635}
636
637impl Writer {
638    /// Creates a new v10 file and its first table.
639    ///
640    /// # Errors
641    ///
642    /// If the file exists, a field has no scalar encoding, or the path cannot be written.
643    pub fn create(
644        path: impl AsRef<Path>,
645        name: impl Into<String>,
646        fields: Vec<Field>,
647    ) -> Result<Self> {
648        for field in &fields {
649            type_tag(&field.ty)?;
650        }
651        let file =
652            OpenOptions::new().write(true).read(true).create_new(true).open(path).map_err(io)?;
653        let mut header = [0; HEADER as usize];
654        header[..8].copy_from_slice(MAGIC);
655        header[8..12].copy_from_slice(&FORMAT.to_le_bytes());
656        write_at(&file, 0, &header)?;
657        Ok(Self {
658            file,
659            at: HEADER,
660            dictionaries: fields
661                .iter()
662                .map(|field| (field.ty == LogicalType::Varchar).then(GlobalDictionary::new))
663                .collect(),
664            table: Table {
665                name: name.into(),
666                dictionaries: vec![None; fields.len()],
667                distincts: vec![None; fields.len()],
668                fields,
669                stripes: Vec::new(),
670                rows: 0,
671                frequencies: Vec::new(),
672            },
673            generation: 1,
674            order: Vec::new(),
675            next_order: 0,
676            pending: Vec::with_capacity(STRIPE_PARTS),
677        })
678    }
679
680    /// Appends bytes at the end of the file and moves the writer's own offset past them.
681    ///
682    /// Every write in here goes through this, so that [`Writer::at`] is the only answer to where
683    /// anything is and the file's cursor is never consulted for it.
684    fn put(&mut self, bytes: &[u8]) -> Result<()> {
685        write_at(&self.file, self.at, bytes)?;
686        self.at = self
687            .at
688            .checked_add(bytes.len() as u64)
689            .ok_or_else(|| invalid("native file length overflow"))?;
690        Ok(())
691    }
692
693    /// Writes one chunk as independently readable column pages.
694    ///
695    /// # Errors
696    ///
697    /// If its width or types differ from the declared table, or a page exceeds its bound.
698    pub fn append(&mut self, chunk: &Chunk) -> Result<()> {
699        let order = (self.next_order, 0);
700        self.next_order = self.next_order.saturating_add(1);
701        self.append_at(order, chunk)
702    }
703
704    /// Writes one chunk and records its source position for directory ordering.
705    ///
706    /// Pages may be encoded by parallel pipeline instances and reach the file in completion order.
707    /// The stripe they land in is sorted by this key at commit, and [`Self::finish`] rejects a
708    /// sequence whose parts do not come out in source order once the stripes are sorted, because a
709    /// stripe groups whatever arrived together and cannot put a late part back where it belongs.
710    ///
711    /// # Errors
712    ///
713    /// The same as [`Self::append`].
714    pub fn append_at(&mut self, order: (u64, u64), chunk: &Chunk) -> Result<()> {
715        if chunk.is_empty() {
716            return Ok(());
717        }
718        self.admit(chunk)?;
719        if self.pending.last().is_some_and(|last| last.order > order) {
720            self.flush_pending()?;
721        }
722        // Cloned rather than encoded, and a clone of a chunk that owns its buffers is a copy of
723        // them. Sixty four parts of a hundred and five columns is tens of megabytes held for the
724        // length of a stripe and a few seconds of memory traffic over a whole ClickBench load,
725        // against the hundreds of seconds of encode this is what lets off one thread.
726        self.pending.push(PendingChunk { order, chunk: chunk.clone() });
727        if self.pending.len() == STRIPE_PARTS {
728            self.flush_pending()?;
729        }
730        Ok(())
731    }
732
733    /// Writes a run of chunks as one stripe of its own.
734    ///
735    /// [`Self::append_at`] decides where a stripe ends by watching the orders go past, which works
736    /// when one caller hands over every chunk in source order and does not when several do. A
737    /// writer being fed by more than one pipeline instance sees the orders interleave, and a stripe
738    /// that ends every time two of them cross is a stripe of one or two parts.
739    ///
740    /// So the grouping moves to the caller. Whoever is buffering hands over a run it already knows
741    /// is contiguous and in order, and gets a stripe holding exactly that run. The orders still
742    /// have to come out in source order once the stripes are sorted, which [`Self::finish`] checks,
743    /// so the runs from different callers may interleave with each other but may not overlap.
744    ///
745    /// # Errors
746    ///
747    /// The same as [`Self::append`], and if the run is longer than [`STRIPE_PARTS`].
748    pub fn append_stripe(&mut self, parts: Vec<((u64, u64), Chunk)>) -> Result<()> {
749        if parts.len() > STRIPE_PARTS {
750            return Err(invalid("a stripe was handed more parts than it holds"));
751        }
752        // Whatever an earlier caller left behind is its own stripe rather than the front of this
753        // one, because the two runs are from different places in the source and a stripe is a run.
754        self.flush_pending()?;
755        for (order, chunk) in parts {
756            if chunk.is_empty() {
757                continue;
758            }
759            self.admit(&chunk)?;
760            self.pending.push(PendingChunk { order, chunk });
761        }
762        self.flush_pending()
763    }
764
765    /// Checks a chunk against the declared table and counts its rows in.
766    fn admit(&mut self, chunk: &Chunk) -> Result<()> {
767        if chunk.width() != self.table.fields.len() {
768            return Err(invalid("chunk width differs from table schema"));
769        }
770        for (index, field) in self.table.fields.iter().enumerate() {
771            if chunk.column(index)?.logical_type() != &field.ty {
772                return Err(invalid("chunk type differs from table schema"));
773            }
774        }
775        self.table.rows = self
776            .table
777            .rows
778            .checked_add(chunk.len())
779            .ok_or_else(|| invalid("row count overflow"))?;
780        Ok(())
781    }
782
783    /// Encodes one column's parts of a stripe, with the column's dictionary to itself.
784    ///
785    /// Nothing here is shared with another column. The dictionary belongs to this one, the sieve
786    /// reads only this one, and the page bytes go in a vector of this one's own. That is why the
787    /// fan out below can hand a whole column to a thread and take a plain `&mut` on the dictionary
788    /// rather than making it something several threads can grow at once, which is the harder half
789    /// of #808 and is still open.
790    fn encode_column(
791        index: usize,
792        held: &[PendingChunk],
793        mut dictionary: Option<&mut GlobalDictionary>,
794    ) -> Result<ColumnStripe> {
795        let mut stripe = ColumnStripe {
796            pages: Vec::with_capacity(held.len()),
797            codes: Vec::with_capacity(held.len()),
798            sieves: Vec::with_capacity(held.len()),
799            ranges: Vec::with_capacity(held.len()),
800        };
801        for pending in held {
802            let column = pending.chunk.column(index)?;
803            let (bytes, unique) = encode(column, dictionary.as_deref_mut())?;
804            if bytes.len() > MAX_PAGE {
805                return Err(invalid("column page exceeds the configured bound"));
806            }
807            // The range is built first because the sieve reads it rather than walking the column a
808            // second time to find out how wide it is.
809            let range = Range::of(column);
810            // A column with a global dictionary already has an exact membership index per stripe,
811            // so an approximate one beside it would cost a hash of every string in the table to
812            // answer a question that is already answered. What it would buy is the finer grain, a
813            // part rather than a stripe, and that is worth coming back for on its own.
814            //
815            // A sieve at least as large as the part it indexes is not written. A reader reads the
816            // sieve to decide whether to read the part, so when the sieve is the larger of the two
817            // it has already spent more than the read it is trying to avoid, and that holds even if
818            // it rejects every time. It is a necessary condition rather than the whole rule, which
819            // is that a sieve pays when its bytes are under the rejection rate times the part's,
820            // but the rejection rate depends on what a query probes for and the writer does not
821            // know that. The necessary half needs two numbers that are both in hand here.
822            let sieve = match dictionary {
823                Some(_) => None,
824                None => Sieve::of(column, &range, SIEVE_BUDGET)
825                    .filter(|sieve| sieve.len() < bytes.len()),
826            };
827            stripe.pages.push(bytes);
828            stripe.codes.push(unique);
829            stripe.sieves.push(sieve);
830            stripe.ranges.push(range);
831        }
832        Ok(stripe)
833    }
834
835    /// Encodes a whole stripe, one column to a worker.
836    ///
837    /// The columns are handed out through a queue rather than dealt in equal piles, because they
838    /// are nothing like equal: `URL` on ClickBench is a global dictionary of sixty one million
839    /// strings and `IsMobile` is a byte. A pile that happened to hold the four large string columns
840    /// would be the whole stripe and the other workers would be waiting on it. The queue is sorted
841    /// so the expensive ones are taken first, which is the classic answer to a last job that runs
842    /// longer than everything after it.
843    fn encode_columns(&mut self, held: &[PendingChunk]) -> Result<Vec<ColumnStripe>> {
844        let width = self.table.fields.len();
845        let workers = std::thread::available_parallelism()
846            .map_or(1, usize::from)
847            .min(MAX_ENCODE_WORKERS)
848            .min(width);
849        if workers <= 1 || held.len() <= 1 {
850            return self
851                .dictionaries
852                .iter_mut()
853                .enumerate()
854                .map(|(index, dictionary)| Self::encode_column(index, held, dictionary.as_mut()))
855                .collect();
856        }
857        // The dictionaries are moved out and back rather than borrowed, because a worker that takes
858        // the next column off a queue cannot be holding a borrow of the vector the queue came from.
859        let mut jobs: Vec<(usize, Option<GlobalDictionary>)> =
860            std::mem::take(&mut self.dictionaries).into_iter().enumerate().collect();
861        // Popped from the back, so the expensive columns go last in the vector.
862        jobs.sort_by_key(|(index, _)| weight(&self.table.fields[*index].ty));
863        let queue = Mutex::new(jobs);
864        let pieces = std::thread::scope(|scope| {
865            (0..workers)
866                .map(|_| {
867                    scope.spawn(|| {
868                        let mut mine = Vec::new();
869                        loop {
870                            let taken = queue
871                                .lock()
872                                .map_err(|_| Error::internal("a native encode worker panicked"))?
873                                .pop();
874                            let Some((index, mut dictionary)) = taken else { break };
875                            let encoded = Self::encode_column(index, held, dictionary.as_mut())?;
876                            mine.push((index, dictionary, encoded));
877                        }
878                        Ok(mine)
879                    })
880                })
881                .collect::<Vec<_>>()
882                .into_iter()
883                .map(|handle| {
884                    handle.join().map_err(|_| Error::internal("a native encode worker panicked"))?
885                })
886                .collect::<Result<Vec<_>>>()
887        })?;
888        let mut dictionaries: Vec<Option<GlobalDictionary>> = (0..width).map(|_| None).collect();
889        let mut encoded: Vec<Option<ColumnStripe>> = (0..width).map(|_| None).collect();
890        for piece in pieces {
891            for (index, dictionary, stripe) in piece {
892                dictionaries[index] = dictionary;
893                encoded[index] = Some(stripe);
894            }
895        }
896        self.dictionaries = dictionaries;
897        encoded
898            .into_iter()
899            .map(|stripe| stripe.ok_or_else(|| Error::internal("a column was never encoded")))
900            .collect()
901    }
902
903    /// Writes the buffered parts as one stripe, each column's parts contiguous on disk.
904    fn flush_pending(&mut self) -> Result<()> {
905        if self.pending.is_empty() {
906            return Ok(());
907        }
908        let width = self.table.fields.len();
909        // Held here rather than read off the writer, because writing a page needs the writer and
910        // the borrow checker is right that those are two different uses of it.
911        let mut held = std::mem::take(&mut self.pending);
912        let parts = held.len();
913        let encoded = self.encode_columns(&held)?;
914        let mut pages = Vec::with_capacity(width);
915        let mut memberships = vec![None; width];
916        let mut ranges = Vec::with_capacity(width);
917        let mut index = Vec::with_capacity(width.saturating_mul(index_section(parts)?));
918        for stripe in &encoded {
919            let offset = self.at;
920            let section = index.len();
921            let mut length = 0_usize;
922            for bytes in &stripe.pages {
923                write_at(&self.file, self.at + length as u64, bytes)?;
924                put_u32(
925                    &mut index,
926                    u32::try_from(bytes.len()).map_err(|_| invalid("part length overflow"))?,
927                );
928                put_u64(&mut index, checksum(bytes));
929                length = length
930                    .checked_add(bytes.len())
931                    .ok_or_else(|| invalid("column page length overflow"))?;
932            }
933            let hash = checksum(&index[section..]);
934            put_u64(&mut index, hash);
935            if length > MAX_PAGE {
936                return Err(invalid("column page exceeds the configured bound"));
937            }
938            self.at = self
939                .at
940                .checked_add(length as u64)
941                .ok_or_else(|| invalid("native file length overflow"))?;
942            pages.push(Span {
943                offset,
944                length: u32::try_from(length).map_err(|_| invalid("page length overflow"))?,
945            });
946            ranges.push(merged_range(stripe.ranges.iter().cloned()));
947        }
948        for (membership, stripe) in memberships.iter_mut().zip(&encoded) {
949            if stripe.codes.iter().all(Option::is_none) {
950                continue;
951            }
952            let lists = stripe
953                .codes
954                .iter()
955                .map(|codes| codes.clone().unwrap_or_default())
956                .collect::<Vec<_>>();
957            let bytes = encode_membership(&merged_codes(lists));
958            let offset = self.at;
959            self.put(&bytes)?;
960            *membership = Some(Page {
961                offset,
962                length: u32::try_from(bytes.len())
963                    .map_err(|_| invalid("membership page length overflow"))?,
964                hash: checksum(&bytes),
965            });
966        }
967        let mut sieves = vec![None; width];
968        for (page, stripe) in sieves.iter_mut().zip(&encoded) {
969            if stripe.sieves.iter().all(Option::is_none) {
970                continue;
971            }
972            let bytes = encode_sieves(stripe.sieves.iter())?;
973            let offset = self.at;
974            self.put(&bytes)?;
975            *page = Some(Page {
976                offset,
977                length: u32::try_from(bytes.len())
978                    .map_err(|_| invalid("sieve page length overflow"))?,
979                hash: checksum(&bytes),
980            });
981        }
982        // A stripe of one part has the same rows in it as that part, so its own bounds are already
983        // the part's and a page here would say what the directory says. Everywhere else the page is
984        // written unless it comes to more than the column it indexes, which is the rule the sieves
985        // go by and for the same reason: a reader reads this to decide whether to read the column,
986        // so a page larger than the column has spent more than the read it is avoiding.
987        let mut part_ranges = vec![None; width];
988        if parts > 1 {
989            for ((page, stripe), span) in part_ranges.iter_mut().zip(&encoded).zip(&pages) {
990                let bytes = encode_part_ranges(&stripe.ranges)?;
991                if bytes.len() >= span.length as usize {
992                    continue;
993                }
994                let offset = self.at;
995                self.put(&bytes)?;
996                *page = Some(Page {
997                    offset,
998                    length: u32::try_from(bytes.len())
999                        .map_err(|_| invalid("part range page length overflow"))?,
1000                    hash: checksum(&bytes),
1001                });
1002            }
1003        }
1004        let offset = self.at;
1005        self.put(&index)?;
1006        let index = Span {
1007            offset,
1008            length: u32::try_from(index.len())
1009                .map_err(|_| invalid("index page length overflow"))?,
1010        };
1011        let mut rows = 0_usize;
1012        let mut lengths = Vec::with_capacity(parts);
1013        let mut span = None;
1014        for pending in held.drain(..) {
1015            let part = pending.chunk.len();
1016            rows = rows.checked_add(part).ok_or_else(|| invalid("row count overflow"))?;
1017            lengths.push(u32::try_from(part).map_err(|_| invalid("part row count overflow"))?);
1018            span = Some(
1019                span.map_or((pending.order, pending.order), |(first, _)| (first, pending.order)),
1020            );
1021        }
1022        self.order.push(span.ok_or_else(|| invalid("a stripe was flushed with no parts"))?);
1023        self.table.stripes.push(Stripe {
1024            rows,
1025            parts: lengths,
1026            index,
1027            pages,
1028            memberships,
1029            sieves,
1030            part_ranges,
1031            zone: Zone::from_ranges(ranges),
1032        });
1033        // Back where it came from, empty, so the next stripe buffers into the same allocation.
1034        self.pending = held;
1035        Ok(())
1036    }
1037
1038    /// Finds exact heavy hitters without keeping a hash table for every numeric column while the
1039    /// load is live. The pages are already in the target file, so one column at a time uses a
1040    /// bounded Misra-Gries candidate table and then recounts only those candidates.
1041    fn numeric_frequency(&self, column: usize) -> Result<Option<FrequencySummary>> {
1042        let ty = &self.table.fields[column].ty;
1043        if !matches!(
1044            ty,
1045            LogicalType::TinyInt
1046                | LogicalType::SmallInt
1047                | LogicalType::Integer
1048                | LogicalType::BigInt
1049                | LogicalType::UTinyInt
1050                | LogicalType::USmallInt
1051                | LogicalType::UInteger
1052                | LogicalType::UBigInt
1053                | LogicalType::Date
1054                | LogicalType::Timestamp
1055        ) {
1056            return Ok(None);
1057        }
1058        let mut candidates: HashMap<FrequencyValue, u32> = HashMap::new();
1059        let mut decrements = 0_u64;
1060        self.visit_numeric(column, |_, value| {
1061            if let Some(count) = candidates.get_mut(&value) {
1062                *count = count.saturating_add(1);
1063            } else if candidates.len() < FREQUENCY_CANDIDATES {
1064                candidates.insert(value, 1);
1065            } else {
1066                candidates.retain(|_, count| {
1067                    *count -= 1;
1068                    *count != 0
1069                });
1070                decrements = decrements.saturating_add(1);
1071            }
1072        })?;
1073        let (exact, ordinals) = if decrements == 0 {
1074            (
1075                candidates
1076                    .into_iter()
1077                    .map(|(value, count)| (value, u64::from(count)))
1078                    .collect::<HashMap<_, _>>(),
1079                Vec::new(),
1080            )
1081        } else {
1082            let mut lower = candidates.values().copied().collect::<Vec<_>>();
1083            lower.sort_unstable_by(|left, right| right.cmp(left));
1084            if lower.len() < FREQUENCY_BUILD_RANK
1085                || u64::from(lower[FREQUENCY_BUILD_RANK - 1]) <= decrements
1086            {
1087                return Ok(None);
1088            }
1089            let mut exact =
1090                candidates.into_keys().map(|value| (value, 0_u64)).collect::<HashMap<_, _>>();
1091            let mut ordinals = Vec::new();
1092            let mut exceeded = false;
1093            self.visit_numeric(column, |ordinal, value| {
1094                if let Some(count) = exact.get_mut(&value) {
1095                    *count = count.saturating_add(1);
1096                    if !exceeded {
1097                        if ordinals.len() < FREQUENCY_ORDINALS {
1098                            ordinals.push(ordinal);
1099                        } else {
1100                            ordinals.clear();
1101                            exceeded = true;
1102                        }
1103                    }
1104                }
1105            })?;
1106            (exact, ordinals)
1107        };
1108        let mut entries = exact
1109            .into_iter()
1110            .map(|(value, count)| FrequencyEntry { value, count })
1111            .collect::<Vec<_>>();
1112        entries.sort_unstable_by(|left, right| {
1113            right.count.cmp(&left.count).then_with(|| frequency_order(left.value, right.value))
1114        });
1115        let omitted_max =
1116            entries.get(FREQUENCY_ENTRIES).map_or(decrements, |entry| decrements.max(entry.count));
1117        entries.truncate(FREQUENCY_ENTRIES);
1118        Ok(Some(FrequencySummary { entries, omitted_max, ordinals }))
1119    }
1120
1121    fn visit_numeric(
1122        &self,
1123        column: usize,
1124        mut visit: impl FnMut(u64, FrequencyValue),
1125    ) -> Result<()> {
1126        let ty = &self.table.fields[column].ty;
1127        let mut start = 0_u64;
1128        for stripe in &self.table.stripes {
1129            let spans = read_index(&self.file, stripe, column)?;
1130            let page = stripe.pages[column];
1131            let mut bytes = vec![0; page.length as usize];
1132            read_at(&self.file, page.offset, &mut bytes)?;
1133            for (span, &rows) in spans.iter().zip(&stripe.parts) {
1134                let part = part_bytes(&bytes, *span)?;
1135                if checksum(part) != span.hash {
1136                    return Err(invalid("column page checksum differs while building frequencies"));
1137                }
1138                let rows = rows as usize;
1139                let vector = decode(ty, rows, part, None)?;
1140                // row at a time: frequency construction visits decoded values to update bounded candidates.
1141                for row in 0..rows {
1142                    let value = if vector.is_null_at(row) {
1143                        FrequencyValue::Null
1144                    } else {
1145                        // An unsigned column has no signed reading, and the documented fallback is
1146                        // the value itself. Every unsigned width the format stores fits in the
1147                        // `i128` a candidate is keyed by, so nothing is lost on the way through.
1148                        let widened = match vector.signed_at(row) {
1149                            Some(value) => Some(value),
1150                            None => match vector.value_at(row) {
1151                                Value::UTinyInt(value) => Some(i128::from(value)),
1152                                Value::USmallInt(value) => Some(i128::from(value)),
1153                                Value::UInteger(value) => Some(i128::from(value)),
1154                                Value::UBigInt(value) => Some(i128::from(value)),
1155                                _ => None,
1156                            },
1157                        };
1158                        FrequencyValue::Integer(widened.ok_or_else(|| {
1159                            invalid("numeric frequency page did not contain an integer value")
1160                        })?)
1161                    };
1162                    visit(start.saturating_add(row as u64), value);
1163                }
1164                start = start.saturating_add(rows as u64);
1165            }
1166        }
1167        Ok(())
1168    }
1169
1170    /// Builds independent numeric synopses concurrently after all column pages are committed.
1171    ///
1172    /// The columns go through a queue rather than being cut into equal runs, because they are not
1173    /// equally expensive and they are not shuffled. A `BIGINT` column carries eight times the bytes
1174    /// of a `TINYINT` through the decode, and a run of them sits together in a schema the way it
1175    /// sits together in `hits`, so a worker that was handed the wrong six columns finishes long
1176    /// after one that was handed the right six and the whole phase waits for it.
1177    fn numeric_frequencies(&self) -> Result<Vec<Option<FrequencySummary>>> {
1178        let mut columns = self
1179            .table
1180            .fields
1181            .iter()
1182            .enumerate()
1183            .filter_map(|(column, field)| {
1184                matches!(
1185                    field.ty,
1186                    LogicalType::TinyInt
1187                        | LogicalType::SmallInt
1188                        | LogicalType::Integer
1189                        | LogicalType::BigInt
1190                        | LogicalType::UTinyInt
1191                        | LogicalType::USmallInt
1192                        | LogicalType::UInteger
1193                        | LogicalType::UBigInt
1194                        | LogicalType::Date
1195                        | LogicalType::Timestamp
1196                )
1197                .then_some(column)
1198            })
1199            .collect::<Vec<_>>();
1200        let workers = std::thread::available_parallelism()
1201            .map_or(1, usize::from)
1202            .min(MAX_FREQUENCY_WORKERS)
1203            .min(columns.len());
1204        if workers <= 1 {
1205            let mut frequencies = vec![None; self.table.fields.len()];
1206            for column in columns {
1207                frequencies[column] = self.numeric_frequency(column)?;
1208            }
1209            return Ok(frequencies);
1210        }
1211        // Popped from the back, so the expensive columns are the ones taken first and the cheap ones
1212        // are what is left to fill in behind them.
1213        columns.sort_by_key(|&column| weight(&self.table.fields[column].ty));
1214        let queue = Mutex::new(columns);
1215        let pieces = std::thread::scope(|scope| {
1216            (0..workers)
1217                .map(|_| {
1218                    scope.spawn(|| {
1219                        let mut mine = Vec::new();
1220                        loop {
1221                            let taken = queue
1222                                .lock()
1223                                .map_err(|_| Error::internal("a native frequency worker panicked"))?
1224                                .pop();
1225                            let Some(column) = taken else { break };
1226                            mine.push((column, self.numeric_frequency(column)?));
1227                        }
1228                        Ok(mine)
1229                    })
1230                })
1231                .collect::<Vec<_>>()
1232                .into_iter()
1233                .map(|handle| {
1234                    handle
1235                        .join()
1236                        .map_err(|_| Error::internal("a native frequency worker panicked"))?
1237                })
1238                .collect::<Result<Vec<_>>>()
1239        })?;
1240        let mut frequencies = vec![None; self.table.fields.len()];
1241        for piece in pieces {
1242            for (column, summary) in piece {
1243                frequencies[column] = summary;
1244            }
1245        }
1246        Ok(frequencies)
1247    }
1248
1249    /// Commits the directory and syncs the file before publishing its header slot.
1250    ///
1251    /// # Errors
1252    ///
1253    /// If directory encoding, writing, or syncing fails.
1254    pub fn finish(mut self) -> Result<Table> {
1255        self.flush_pending()?;
1256        let mut stripes = std::mem::take(&mut self.order)
1257            .into_iter()
1258            .zip(std::mem::take(&mut self.table.stripes))
1259            .collect::<Vec<_>>();
1260        stripes.sort_by_key(|(order, _)| order.0);
1261        let mut previous: Option<(u64, u64)> = None;
1262        for ((first, last), _) in &stripes {
1263            if previous.is_some_and(|previous| previous >= *first) {
1264                return Err(invalid("chunks did not arrive in source order"));
1265            }
1266            previous = Some(*last);
1267        }
1268        self.table.stripes = stripes.into_iter().map(|(_, stripe)| stripe).collect();
1269        self.table.frequencies = self.numeric_frequencies()?;
1270        let dictionaries = std::mem::take(&mut self.dictionaries);
1271        let orders = rankings(&dictionaries)?;
1272        for (index, (dictionary, order)) in dictionaries.into_iter().zip(orders).enumerate() {
1273            let Some(dictionary) = dictionary else { continue };
1274            // A code nothing counted is a code no non-null row of this column holds, which is the
1275            // empty string a null was written as and nothing else, because a code is only ever made
1276            // by a row asking for one.
1277            self.table.distincts[index] =
1278                Some(dictionary.counts.iter().filter(|count| **count != 0).count() as u64);
1279            self.table.frequencies[index] = Some(code_frequency(&dictionary));
1280            let encoded = encode_global_dictionary(dictionary, &order)?;
1281            let offset = self.at;
1282            self.put(&encoded.index)?;
1283            self.put(&encoded.ranks)?;
1284            for block in &encoded.payload {
1285                self.put(block)?;
1286            }
1287            let payload_len =
1288                encoded.payload.iter().try_fold(0_usize, |len, block| len.checked_add(block.len()));
1289            let length = payload_len
1290                .and_then(|len| len.checked_add(encoded.index.len()))
1291                .and_then(|len| len.checked_add(encoded.ranks.len()))
1292                .ok_or_else(|| invalid("dictionary page length overflow"))?;
1293            self.table.dictionaries[index] = Some(Page {
1294                offset,
1295                length: u32::try_from(length)
1296                    .map_err(|_| invalid("dictionary page length overflow"))?,
1297                hash: checksum(&encoded.index),
1298            });
1299        }
1300        let directory = encode_directory(&self.table)?;
1301        if directory.len() > MAX_DIRECTORY {
1302            return Err(invalid("directory exceeds the configured bound"));
1303        }
1304        let offset = self.at;
1305        self.put(&directory)?;
1306        self.file.sync_all().map_err(io)?;
1307        let slot = Slot {
1308            offset,
1309            length: u32::try_from(directory.len())
1310                .map_err(|_| invalid("directory length overflow"))?,
1311            generation: self.generation,
1312            hash: checksum(&directory),
1313        };
1314        // The one write that is not an append, and the last one. It goes back over the slot in the
1315        // header, so it names its offset rather than going through `put`, and `at` does not move.
1316        write_at(&self.file, 16, &slot.bytes())?;
1317        self.file.sync_all().map_err(io)?;
1318        Ok(self.table)
1319    }
1320}
1321
1322/// Reads committed native column pages without holding the table in memory.
1323#[derive(Debug, Clone)]
1324pub struct Reader {
1325    file: Arc<File>,
1326    table: Arc<Table>,
1327    dictionaries: Arc<Vec<OnceLock<Arc<Vector>>>>,
1328    /// Held while a global dictionary is being opened, one per column.
1329    ///
1330    /// The [`OnceLock`] above says whether one has been opened, which is the question a reader that
1331    /// already has it needs answered and is free. It does not say whether one is being opened, and
1332    /// the difference matters because every worker of a scan wants the same dictionary at the same
1333    /// moment. Without this they all miss, all read the page, all verify it and all decode it, and
1334    /// all but one throw the answer away. ClickBench 38 reads the URL dictionary, which is 515,958
1335    /// entries, and was paying for it twice.
1336    loading: Arc<Vec<Mutex<()>>>,
1337    /// How many global dictionaries have been opened. A scan of a dictionary column should open its
1338    /// dictionary once however many workers it has, and the test that says so is the only thing
1339    /// keeping it that way.
1340    opened: Arc<AtomicUsize>,
1341    /// The membership sieves of one stripe of one column, by column and then by stripe, read the
1342    /// first time a probe asks about them. A query filters on one or two columns and never looks at
1343    /// the rest, so reading these at open would be the whole index for the sake of a fraction of it.
1344    sieves: Arc<Vec<Vec<SieveSlot>>>,
1345    /// The per part ranges of one stripe of one column, by column and then by stripe, read the
1346    /// first time something compares that column and kept after that.
1347    part_ranges: Arc<Vec<Vec<RangeSlot>>>,
1348    /// Which stripe and which part of it every part of the table is, by table wide part number.
1349    places: Arc<Vec<Place>>,
1350    cache: Arc<Vec<Mutex<Cached>>>,
1351    /// How many whole stripe pages have been read, which is what the sharing above is judged on. A
1352    /// scan of a column should read each of its stripes once however many workers it has.
1353    pages: Arc<AtomicUsize>,
1354    /// How many index sections have been read. A scan of a column should read each of its stripes
1355    /// once here too, and the test that says so is the only thing keeping it that way.
1356    indexes: Arc<AtomicUsize>,
1357    /// How many stripes of one column the page cache keeps. See [`CACHED_STRIPES_PER_COLUMN`] for
1358    /// what sets it and [`Reader::keep_stripes`] for who raises it.
1359    kept: Arc<AtomicUsize>,
1360    /// The file's size when it was opened, for [`Reader::layout`].
1361    size: u64,
1362    /// The committed directory's size, for [`Reader::layout`].
1363    directory: u64,
1364    /// What opening the file cost, which is a number rather than a claim.
1365    opening: Opening,
1366}
1367
1368/// What [`Reader::open`] read before it returned.
1369///
1370/// `spec/stats/04-in-memory.md` section 4.2 says opening a table reads the header and the directory
1371/// and nothing else, and once that document's statistics are in the file the tempting change is to
1372/// load a column summary or two on the way past, because they are small and the next query will
1373/// want them. A hundred milliseconds of that is a hundred milliseconds nobody asked for, and an
1374/// embedded database is opened by processes that are about to run one trivial query.
1375///
1376/// So the claim gets a number. Both of these are fixed by the schema and the stripe count and are
1377/// independent of how many rows the file holds, and the test that says so is what stops the
1378/// tempting change from landing quietly.
1379#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
1380pub struct Opening {
1381    /// How many times the file was read. The header, then each directory slot that looked valid
1382    /// enough to check, so three at the most.
1383    pub reads: u32,
1384    /// How many bytes those reads asked for.
1385    pub bytes: u64,
1386}
1387
1388/// What a reader has read, while it was being opened and since.
1389#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
1390pub struct Reads {
1391    /// What opening cost, before any query had been planned.
1392    pub opening: Opening,
1393    /// Whole stripe pages read since.
1394    pub pages: usize,
1395    /// Index sections read since.
1396    pub indexes: usize,
1397    /// Global dictionaries opened since. One per dictionary column that a query touched, however
1398    /// many workers touched it, which is a claim only a test can keep true.
1399    pub dictionaries: usize,
1400}
1401
1402/// Where one table wide part number lands.
1403#[derive(Debug, Clone, Copy)]
1404struct Place {
1405    stripe: u32,
1406    part: u32,
1407    rows: u32,
1408}
1409
1410/// One part's bytes inside one column page.
1411#[derive(Debug, Clone, Copy)]
1412struct PartSpan {
1413    start: usize,
1414    length: usize,
1415    hash: u64,
1416}
1417
1418/// What a reader holds for one stripe of one column.
1419///
1420/// The index is small and is loaded whether the caller wants the whole page or one part of it. The
1421/// page is loaded only by a scan, because a sparse fetch that wants a thousand rows out of sixty
1422/// four thousand would be reading sixty four times what it uses.
1423#[derive(Debug, Clone)]
1424struct CachedColumn {
1425    stripe: usize,
1426    index: Arc<Vec<PartSpan>>,
1427    page: Option<Arc<Vec<u8>>>,
1428}
1429
1430/// One column's stripes a reader holds, and which of them somebody is reading right now.
1431///
1432/// The pages are one slot per stripe of the table rather than a list of the ones being kept, so
1433/// finding a page is an index and not a walk. That matters because the walk happened under the
1434/// lock, once per part per column, and a scan that gives a whole stripe to each of thirty two
1435/// workers keeps enough pages that walking them was the longest thing the lock was held for. The
1436/// slots cost a pointer per stripe per column, which on the ClickBench file is eight kilobytes
1437/// against the forty megabytes of pages they point at. `order` is which of them are filled, oldest
1438/// first, because that is the one thing the slots cannot say by themselves.
1439///
1440/// `loading` is what keeps a scan from reading the same page once per worker. It is a list and not
1441/// a set because it holds at most one stripe per worker on the column and is walked far less often
1442/// than a hash of it would be built.
1443///
1444/// `index` is every index this reader has ever read for the column, one slot per stripe, and it is
1445/// never evicted. An index is a few hundred bytes and a page is a quarter of a megabyte, so the two
1446/// do not belong under the same budget. Riding in the page cache meant a worker that came back to a
1447/// stripe after its page had been evicted read the index again with it, which on the full
1448/// ClickBench file was about thirteen hundred reads out of a hundred and fourteen thousand.
1449#[derive(Debug, Default)]
1450struct Cached {
1451    pages: Vec<Option<Arc<Vec<u8>>>>,
1452    order: VecDeque<usize>,
1453    loading: Vec<usize>,
1454    index: Vec<Option<Arc<Vec<PartSpan>>>>,
1455}
1456
1457/// Stripes of one column a reader keeps the bytes of, when nobody has asked for more.
1458///
1459/// This has to hold at least as many stripes as a column has workers in it at once, or the workers
1460/// evict each other's pages and read them again. Four is what a scan that hands parts out in order
1461/// needs, because then every worker is within a few parts of every other and at most a couple of
1462/// stripes are open at a time. A scan that hands a whole stripe to each worker has one stripe open
1463/// per worker for the length of that stripe, and it says so with [`Reader::keep_stripes`] rather
1464/// than paying for sixteen slots on every table that is read one part at a time.
1465///
1466/// It multiplies by the page size, which is a quarter of a megabyte for a four byte column, and by
1467/// the number of columns a query touches.
1468const CACHED_STRIPES_PER_COLUMN: usize = 4;
1469
1470/// The sieves of one stripe of one column, once somebody has asked for them.
1471type SieveSlot = OnceLock<Arc<Vec<Option<Sieve>>>>;
1472
1473type RangeSlot = OnceLock<Arc<Vec<Range>>>;
1474
1475#[derive(Debug)]
1476struct NativeText {
1477    file: Arc<File>,
1478    /// How many values the dictionary holds.
1479    values: usize,
1480    /// Where each value ends inside its payload block, packed at `offset_bits` in runs of
1481    /// [`TEXT_OFFSET_RUN`].
1482    ///
1483    /// Ends rather than starts, because then a block of 1,024 values is 1,024 numbers rather than
1484    /// 1,025: the start of a value is the end of the one before it, and the first value of a block
1485    /// starts at zero by construction. Relative to the block rather than to the payload, because a
1486    /// reader decodes a whole block and slices it, so an offset into the payload is a number it
1487    /// would have to subtract a base from anyway.
1488    offsets: Vec<u8>,
1489    /// Bits one offset is packed at, which is what the largest block of this column spans and is the
1490    /// same for every block of it.
1491    offset_bits: usize,
1492    /// How many entries the sorted order has, which is the value count.
1493    ranks: usize,
1494    /// Where the sorted order starts in the file. It is read a block at a time and only when
1495    /// something searches it, so a query that never compares this column against a literal never
1496    /// touches it at all.
1497    rank_at: u64,
1498    /// Where each block of the sorted order ends, as a byte offset from `rank_at`. A block is packed
1499    /// at whatever width its own heads need, so unlike the entries it replaced its length is not
1500    /// arithmetic on the block number.
1501    rank_ends: Vec<u64>,
1502    rank_hashes: Vec<u64>,
1503    rank_blocks: Vec<OnceLock<Result<Vec<u8>>>>,
1504    /// Bits one code is packed at, which is what the value count needs and is the same for every
1505    /// block of the column.
1506    code_bits: usize,
1507    /// The sorted order turned round, built the first time a reader asks for it.
1508    ///
1509    /// Four bytes per value against the four the offsets already hold, so a column that has this is
1510    /// carrying half again what it carried before rather than something of a new order. It is built
1511    /// only when something asks, which is a grouped min or max over this column and nothing else,
1512    /// and that reader was going to read the payload of this column once per row otherwise.
1513    code_ranks: OnceLock<Option<Vec<u32>>>,
1514    payload: u64,
1515    /// Where each block of the payload ends in the file, as a byte offset from `payload`. The
1516    /// blocks are stored back to back, so a block starts where the one before it ended.
1517    ends: Vec<u64>,
1518    hashes: Vec<u64>,
1519    /// The payload, read and decoded a block at a time and kept after that.
1520    blocks: Vec<OnceLock<Result<Vec<u8>>>>,
1521    /// How many decoded payload bytes this column keeps before a sweep stops keeping what it reads.
1522    /// [`TEXT_KEEP_BUDGET`] everywhere but in the test of the ceiling.
1523    keep_budget: usize,
1524    /// Roughly how many decoded payload bytes are being kept, which is what [`TEXT_KEEP_BUDGET`]
1525    /// is measured against.
1526    ///
1527    /// Roughly, because two threads that keep the same block at the same time both add its length
1528    /// while [`OnceLock`] keeps one of the two. That makes the count read high and the budget bind
1529    /// a little early, which is the harmless direction, and it costs one relaxed add a block rather
1530    /// than a lock on the path every scan of a string column goes through.
1531    payload_kept: AtomicUsize,
1532}
1533
1534/// How many values of a dictionary go in one block of the payload.
1535///
1536/// The block is the unit the string cascade encodes, the unit a checksum covers, and the unit a
1537/// reader has to decode to get at a single value, so it is the one number the payload format turns
1538/// on. Blocking by values rather than by bytes is what keeps a value out of two blocks at once: the
1539/// block holding a code is `code / TEXT_PAYLOAD_VALUES` and nothing has to be stitched.
1540///
1541/// A probe on the five ClickBench columns that have a dictionary worth the name, written up on
1542/// #347, measured the ratio and the decode speed at 128, 256, 512, 1,024 and 4,096 values. Both get
1543/// better all the way up, because front coding and the LZ matcher have more to look back at and
1544/// because the per chunk setup is spread over more values. What stops it is the point read: a query
1545/// that wants ten values has to decode ten blocks, so the block is what a lookup costs. At 1,024
1546/// values a block is between 67 KB and 394 KB decoded across those five columns, and the ratios are
1547/// 2.3 to 4.5. Going up to 4,096 buys two to six percent more and makes a block as much as 1.5 MB.
1548/// Going down to 512 gives up five to nine percent.
1549const TEXT_PAYLOAD_VALUES: usize = 1024;
1550
1551/// How many decoded payload bytes one dictionary keeps before a sweep stops keeping what it reads.
1552///
1553/// A sweep of the whole dictionary decodes every block whatever it does, and the only question is
1554/// whether it hangs on to them. Keeping all of them is 4.2 GB on ClickBench `URL` at a hundred
1555/// million rows, which is what #997 was right to stop. Keeping none of them means the next query
1556/// asking the same thing decodes all of it again, and on the same column at a million rows that
1557/// took a `LIKE` from 2.7 ms to 16.2 ms, because the decode used to be paid once by a session and
1558/// is now paid by every statement in it. Neither end is the answer. A bound is.
1559///
1560/// So a sweep keeps what it decodes until the column is holding this much and decodes without
1561/// keeping after that. At a million rows the five ClickBench string columns decode to between 8 MB
1562/// and 85 MB, so they sit inside it and a repeated `LIKE` reads a decoded block rather than a
1563/// stored one. At a hundred million rows `URL` fills it and the rest of that column is read and
1564/// dropped, which is the old cost on the part that does not fit and none of the old footprint.
1565///
1566/// Two hundred and fifty six megabytes a column is a number and not a policy, and the policy is
1567/// what should replace it: this wants to be a buffer pool over the whole database, sized against
1568/// the memory limit the session was given, with the blocks of every column competing for it and the
1569/// least useful one evicted. That is F2 work. What is here is the part of it that can be written
1570/// without an eviction order, which is a ceiling.
1571const TEXT_KEEP_BUDGET: usize = 256 * 1024 * 1024;
1572
1573/// How many offsets go in one packed run.
1574///
1575/// A payload block holds 1,024 values and `bitpack::pack_tail` takes fewer than 1,024 at a time,
1576/// since a whole unit of that many belongs in the transposed layout instead. So the offsets of a
1577/// block go in two runs. Five hundred and twelve values at any width is a whole number of bytes, so
1578/// a run starts where a multiply says it does and nothing is padded.
1579const TEXT_OFFSET_RUN: usize = 512;
1580
1581/// Bytes at the front of a global dictionary index: the value count, the values a payload block
1582/// holds, the block count and the bits an offset is packed at.
1583const DICTIONARY_HEADER: usize = 16;
1584
1585/// How many entries of a dictionary's sorted order sit in one block that is read and checked as a
1586/// unit.
1587///
1588/// Five hundred and twelve entries is between two and three kilobytes on the ClickBench string
1589/// columns, which is well under a page. A binary search over half a million entries makes nineteen
1590/// probes, and the first ten land in ten different blocks while the last nine land in the one block
1591/// that holds the answer, so the whole search reads about thirty kilobytes of a megabyte of order. A
1592/// smaller block would save a little on the early probes, cost a checksum and an end list four times
1593/// as long, and give the heads less to share a base with. A larger one would read more than it uses
1594/// on every probe.
1595const TEXT_RANK_BLOCK: usize = 512;
1596
1597/// Bytes at the front of a rank block, which is the base of its heads and the width they are packed
1598/// at.
1599///
1600/// An entry used to be twelve bytes flat, eight for the head and four for the code, and on the five
1601/// ClickBench columns that have a dictionary worth the name that was 744 MB of a 12.2 GB file. Both
1602/// halves of it are nearly empty. The heads are the first eight bytes of the values in sorted order,
1603/// so a block of five hundred and twelve of them spans a tiny slice of the column, and on a column of
1604/// URLs they are all `http://w` and the block holds one distinct head. The codes are positions in a
1605/// dictionary of eighteen million, which is twenty five bits and not thirty two.
1606///
1607/// So a block now writes the smallest head in it, the bits the largest is above that, and the heads
1608/// and the codes packed at the width each needs. A block where every head agrees costs nine bytes
1609/// and the codes.
1610const RANK_BLOCK_HEADER: usize = size_of::<u64>() + 1;
1611
1612impl NativeText {
1613    /// One block of the payload, read and decoded the first time anything asks for a value in it.
1614    ///
1615    /// The bytes handed back are the values of the block laid end to end, which is what the offsets
1616    /// describe, so a caller slices it with the offsets it already has. Where the block sits in the
1617    /// file is the only thing the caller cannot work out for itself, because the stored form is
1618    /// shorter than the decoded one and by a different amount in every block.
1619    fn payload_block(&self, block: usize) -> Result<Option<&[u8]>> {
1620        let Some(slot) = self.blocks.get(block) else { return Ok(None) };
1621        let bytes = slot.get_or_init(|| self.decode_block(block)).as_ref().map_err(Clone::clone)?;
1622        Ok(Some(bytes.as_slice()))
1623    }
1624
1625    /// Reads and decodes one block of the payload, without deciding who keeps it.
1626    ///
1627    /// [`Self::payload_block`] keeps it forever, which is what a point read wants and what a walk
1628    /// of the whole dictionary must not do. Both call this and they differ in nothing else.
1629    fn decode_block(&self, block: usize) -> Result<Vec<u8>> {
1630        let start = if block == 0 { 0 } else { self.ends[block - 1] };
1631        let end = self.ends[block];
1632        let len = end
1633            .checked_sub(start)
1634            .ok_or_else(|| invalid("global dictionary block ends before it starts"))?;
1635        let mut stored = vec![
1636            0;
1637            usize::try_from(len).map_err(|_| invalid(
1638                "global dictionary block does not fit in memory"
1639            ))?
1640        ];
1641        read_at(&self.file, self.payload + start, &mut stored)?;
1642        if checksum(&stored) != self.hashes[block] {
1643            return Err(invalid("global dictionary payload checksum differs"));
1644        }
1645        let first = block * TEXT_PAYLOAD_VALUES;
1646        let last = (first + TEXT_PAYLOAD_VALUES).min(self.values);
1647        let want = self.end_within(last - 1)? as usize;
1648        let values = string::decode_flat(&stored)?;
1649        if values.len() != last - first {
1650            return Err(invalid("global dictionary block holds the wrong value count"));
1651        }
1652        let bytes = values.into_bytes();
1653        if bytes.len() != want {
1654            return Err(invalid("global dictionary block decodes to the wrong length"));
1655        }
1656        Ok(bytes)
1657    }
1658
1659    /// Where the value at `index` ends inside its payload block.
1660    fn end_within(&self, index: usize) -> Result<u32> {
1661        let run = index / TEXT_OFFSET_RUN;
1662        let bytes = self
1663            .offsets
1664            .get(run * TEXT_OFFSET_RUN / 8 * self.offset_bits..)
1665            .ok_or_else(|| invalid("global dictionary offsets are short"))?;
1666        let end = bitpack::tail_at(bytes, self.offset_bits, index % TEXT_OFFSET_RUN)
1667            .map_err(|_| invalid("global dictionary offsets are short"))?;
1668        u32::try_from(end).map_err(|_| invalid("global dictionary offset is past the payload"))
1669    }
1670
1671    /// Where every value in `first..last` ends inside its payload block, in one pass over the runs.
1672    ///
1673    /// [`Self::end_within`] answers for one value and pays for it twice over: it shifts a window to
1674    /// the bit the value starts at, and the copy that fills that window is a length the compiler does
1675    /// not know, so it is a call to `memcpy` rather than a load. A sweep asked for two of those per
1676    /// value, one for the end and one for the start that is the end before it, and on the ClickBench
1677    /// `URL` dictionary of eighteen million that was most of the half second a `LIKE` over it took.
1678    ///
1679    /// [`bitpack::unpack_tail`] walks the run instead, which makes the window a fixed sixteen bytes
1680    /// and so an unaligned load, and reads the bit position off a counter. A run is five hundred and
1681    /// twelve values and a block is two of them, so a block of a thousand and twenty four values
1682    /// costs two calls here and nothing per value.
1683    fn ends_within(&self, first: usize, last: usize) -> Result<Vec<u64>> {
1684        let mut ends = Vec::with_capacity(last.saturating_sub(first));
1685        let mut at = first;
1686        while at < last {
1687            let run = at / TEXT_OFFSET_RUN;
1688            let stop = ((run + 1) * TEXT_OFFSET_RUN).min(last);
1689            let held = self.values.saturating_sub(run * TEXT_OFFSET_RUN).min(TEXT_OFFSET_RUN);
1690            let bytes = self
1691                .offsets
1692                .get(run * TEXT_OFFSET_RUN / 8 * self.offset_bits..)
1693                .ok_or_else(|| invalid("global dictionary offsets are short"))?;
1694            let run_ends = bitpack::unpack_tail(bytes, self.offset_bits, held)
1695                .map_err(|_| invalid("global dictionary offsets are short"))?;
1696            let within = run_ends
1697                .get(at % TEXT_OFFSET_RUN..stop - run * TEXT_OFFSET_RUN)
1698                .ok_or_else(|| invalid("global dictionary offsets are short"))?;
1699            ends.extend_from_slice(within);
1700            at = stop;
1701        }
1702        Ok(ends)
1703    }
1704
1705    /// Where the value at `index` starts inside its payload block, which is where the value before
1706    /// it ended unless it is the first of the block.
1707    fn start_within(&self, index: usize) -> Result<u32> {
1708        if index % TEXT_PAYLOAD_VALUES == 0 { Ok(0) } else { self.end_within(index - 1) }
1709    }
1710
1711    /// Where the value at `index` starts and ends inside its payload block.
1712    fn span_within(&self, index: usize) -> Result<(u32, u32)> {
1713        let end = self.end_within(index)?;
1714        let start = self.start_within(index)?;
1715        if start > end {
1716            return Err(invalid("global dictionary value ends before it starts"));
1717        }
1718        Ok((start, end))
1719    }
1720
1721    /// The block of the sorted order that holds `rank`, and where in it that rank sits.
1722    ///
1723    /// The block is read from the file and checked against the hash the index carries for it the
1724    /// first time anything asks, and kept after that, the same way a payload block is. A search
1725    /// makes about as many probes as the order has bits, so the whole search reads a handful of
1726    /// these and never the rest.
1727    fn rank_parts(&self, rank: usize) -> Result<(&[u8], usize)> {
1728        let slot = self
1729            .rank_blocks
1730            .get(rank / TEXT_RANK_BLOCK)
1731            .ok_or_else(|| invalid("global dictionary rank is past the order"))?;
1732        let block = slot
1733            .get_or_init(|| {
1734                let which = rank / TEXT_RANK_BLOCK;
1735                let start = if which == 0 { 0 } else { self.rank_ends[which - 1] };
1736                let end = self.rank_ends[which];
1737                let mut bytes = vec![0; (end - start) as usize];
1738                read_at(&self.file, self.rank_at + start, &mut bytes)?;
1739                if checksum(&bytes)
1740                    != *self
1741                        .rank_hashes
1742                        .get(rank / TEXT_RANK_BLOCK)
1743                        .ok_or_else(|| invalid("global dictionary rank block has no checksum"))?
1744                {
1745                    return Err(invalid("global dictionary rank checksum differs"));
1746                }
1747                Ok(bytes)
1748            })
1749            .as_ref()
1750            .map_err(Clone::clone)?;
1751        Ok((block.as_slice(), rank % TEXT_RANK_BLOCK))
1752    }
1753
1754    /// The first eight bytes of the value at `rank`, as the integer a comparison reads.
1755    fn head_at(&self, rank: usize) -> Result<u64> {
1756        let (block, within) = self.rank_parts(rank)?;
1757        let (base, width, packed) = rank_heads(block)?;
1758        let above = bitpack::tail_at(packed, width, within)
1759            .map_err(|_| invalid("global dictionary rank block is short of heads"))?;
1760        Ok(base.wrapping_add(above))
1761    }
1762
1763    /// The packed codes of one rank block, which follow the heads on the next byte boundary.
1764    fn rank_codes<'block>(&self, block: &'block [u8], count: usize) -> Result<&'block [u8]> {
1765        let (_, width, packed) = rank_heads(block)?;
1766        packed
1767            .get(bitpack::tail_len(count, width)..)
1768            .ok_or_else(|| invalid("global dictionary rank block is short of codes"))
1769    }
1770
1771    /// How many entries the block holding `rank` has, which is a full block except at the end.
1772    fn rank_block_len(&self, rank: usize) -> usize {
1773        let first = rank / TEXT_RANK_BLOCK * TEXT_RANK_BLOCK;
1774        TEXT_RANK_BLOCK.min(self.ranks - first)
1775    }
1776}
1777
1778/// The base, the width and the packed bytes of one rank block's heads.
1779fn rank_heads(block: &[u8]) -> Result<(u64, usize, &[u8])> {
1780    let header = block
1781        .get(..RANK_BLOCK_HEADER)
1782        .ok_or_else(|| invalid("global dictionary rank block is short"))?;
1783    let base = u64::from_le_bytes(header[..8].try_into().expect("eight bytes"));
1784    let width = header[8] as usize;
1785    if width > 64 {
1786        return Err(invalid("global dictionary rank block packs heads past a word"));
1787    }
1788    Ok((base, width, &block[RANK_BLOCK_HEADER..]))
1789}
1790
1791/// Bits one offset of a dictionary takes, which is what its widest payload block spans.
1792///
1793/// One width for the whole column rather than one a block. A block is 1,024 values of the same
1794/// column, so the blocks of a column are within a factor of two of each other on every ClickBench
1795/// string column, and a width a block would save a fraction of a bit and cost a byte a block plus
1796/// the arithmetic that finds where a block starts.
1797fn offset_width(offsets: &[u32]) -> usize {
1798    let values = offsets.len() - 1;
1799    let mut span = 0;
1800    for first in (0..values).step_by(TEXT_PAYLOAD_VALUES) {
1801        let last = (first + TEXT_PAYLOAD_VALUES).min(values);
1802        span = span.max(offsets[last] - offsets[first]);
1803    }
1804    (u32::BITS - span.leading_zeros()) as usize
1805}
1806
1807/// How many bytes `values` offsets take at `bits`, which is what the reader has to know before it
1808/// has read any of them.
1809fn offset_bytes(values: usize, bits: usize) -> usize {
1810    let full = values / TEXT_OFFSET_RUN;
1811    let rest = values % TEXT_OFFSET_RUN;
1812    full * TEXT_OFFSET_RUN / 8 * bits + bitpack::tail_len(rest, bits)
1813}
1814
1815/// The end of every value within its payload block, packed a run at a time.
1816fn encode_offsets(offsets: &[u32], bits: usize, out: &mut Vec<u8>) -> Result<()> {
1817    let values = offsets.len() - 1;
1818    let mut run = Vec::with_capacity(TEXT_OFFSET_RUN);
1819    for first in (0..values).step_by(TEXT_OFFSET_RUN) {
1820        let last = (first + TEXT_OFFSET_RUN).min(values);
1821        let base = offsets[first / TEXT_PAYLOAD_VALUES * TEXT_PAYLOAD_VALUES];
1822        run.clear();
1823        run.extend((first..last).map(|value| u64::from(offsets[value + 1] - base)));
1824        bitpack::pack_tail(&run, bits, out)
1825            .map_err(|_| invalid("global dictionary offsets do not pack"))?;
1826    }
1827    Ok(())
1828}
1829
1830/// How many bits a code of a dictionary of `values` entries takes.
1831fn code_width(values: usize) -> usize {
1832    match u64::try_from(values).unwrap_or(u64::MAX) {
1833        0 | 1 => 0,
1834        last => (u64::BITS - (last - 1).leading_zeros()) as usize,
1835    }
1836}
1837
1838impl TextSource for NativeText {
1839    fn len(&self) -> usize {
1840        self.values
1841    }
1842
1843    fn bytes_at(&self, index: usize) -> Result<Option<&[u8]>> {
1844        if index >= self.values {
1845            return Ok(None);
1846        }
1847        let (start, end) = self.span_within(index)?;
1848        if start == end {
1849            return Ok(Some(&[]));
1850        }
1851        // A block holds a fixed number of values rather than a fixed number of bytes, so the value
1852        // is in one block and the offsets already say where in it.
1853        let block = index / TEXT_PAYLOAD_VALUES;
1854        let Some(bytes) = self.payload_block(block)? else { return Ok(None) };
1855        Ok(bytes.get(start as usize..end as usize))
1856    }
1857
1858    fn bytes_len_at(&self, index: usize) -> Result<Option<usize>> {
1859        if index >= self.values {
1860            return Ok(None);
1861        }
1862        let (start, end) = self.span_within(index)?;
1863        Ok(Some((end - start) as usize))
1864    }
1865
1866    /// The rest of the block holding `first`, decoded into a buffer that may die with the call.
1867    ///
1868    /// A block is the unit this format decodes, so a walk that wants every value is going to decode
1869    /// every block whatever it does. The question is whether it keeps them, and both answers are
1870    /// wrong on their own. [`Self::payload_block`] keeps every block it is asked for, so a reader
1871    /// that walked the whole dictionary through `bytes_at` ended up holding the whole dictionary
1872    /// decoded, 4.2 GB on ClickBench `URL`. Keeping none of them makes the next statement asking
1873    /// the same question decode all of it again, which on the same column at a million rows is a
1874    /// `LIKE` going from 2.7 ms to 16.2 ms.
1875    ///
1876    /// So a sweep keeps what it decodes while the column is under [`TEXT_KEEP_BUDGET`] and drops it
1877    /// after that. A block already in hand is used where it is there and costs nothing either way.
1878    fn sweep(
1879        &self,
1880        first: usize,
1881        limit: usize,
1882        body: &mut dyn FnMut(usize, &[u8]) -> Result<()>,
1883    ) -> Result<usize> {
1884        let limit = limit.min(self.values);
1885        if first >= limit {
1886            return Ok(first);
1887        }
1888        let block = first / TEXT_PAYLOAD_VALUES;
1889        let last = ((block + 1) * TEXT_PAYLOAD_VALUES).min(limit);
1890        let decoded;
1891        let bytes: &[u8] = match self.blocks.get(block).and_then(OnceLock::get) {
1892            Some(Ok(kept)) => kept,
1893            _ if self.payload_kept.load(Atomic::Relaxed) < self.keep_budget => {
1894                let kept = self
1895                    .payload_block(block)?
1896                    .ok_or_else(|| invalid("global dictionary block is past the payload"))?;
1897                self.payload_kept.fetch_add(kept.len(), Atomic::Relaxed);
1898                kept
1899            }
1900            _ => {
1901                decoded = self.decode_block(block)?;
1902                &decoded
1903            }
1904        };
1905        let ends = self.ends_within(first, last)?;
1906        if ends.len() != last - first {
1907            return Err(invalid("global dictionary offsets are short"));
1908        }
1909        let mut start = u64::from(self.start_within(first)?);
1910        // row at a time: the caller is handed one value after another, and what it does with one is
1911        // its own business, so there is no shape here for anything but a walk.
1912        for (index, &end) in (first..last).zip(&ends) {
1913            let value = usize::try_from(start)
1914                .ok()
1915                .zip(usize::try_from(end).ok())
1916                .and_then(|(from, to)| bytes.get(from..to))
1917                .ok_or_else(|| invalid("global dictionary value is past its block"))?;
1918            body(index, value)?;
1919            start = end;
1920        }
1921        Ok(last)
1922    }
1923
1924    fn ranks(&self) -> Option<usize> {
1925        (self.ranks > 0).then_some(self.ranks)
1926    }
1927
1928    fn compare_rank(&self, rank: usize, wanted: &[u8]) -> Result<Ordering> {
1929        // The head settles the probe unless the two values start with the same eight bytes, and
1930        // only then is a value read. On a column of URLs that is the difference between a search
1931        // that touches one block of the payload and a search that touches nineteen of them.
1932        let settled = self.head_at(rank)?.cmp(&head(wanted));
1933        if settled != Ordering::Equal {
1934            return Ok(settled);
1935        }
1936        let code = self.code_at_rank(rank)?;
1937        let bytes = self
1938            .bytes_at(code as usize)?
1939            .ok_or_else(|| invalid("global dictionary order names a code it does not have"))?;
1940        Ok(bytes.cmp(wanted))
1941    }
1942
1943    fn code_at_rank(&self, rank: usize) -> Result<u32> {
1944        let (block, within) = self.rank_parts(rank)?;
1945        let codes = self.rank_codes(block, self.rank_block_len(rank))?;
1946        let code = bitpack::tail_at(codes, self.code_bits, within)
1947            .map_err(|_| invalid("global dictionary rank block is short of codes"))?;
1948        let code = u32::try_from(code)
1949            .map_err(|_| invalid("global dictionary order names a code it does not have"))?;
1950        if code as usize >= self.len() {
1951            return Err(invalid("global dictionary order names a code it does not have"));
1952        }
1953        Ok(code)
1954    }
1955
1956    fn code_ranks(&self) -> Option<&[u32]> {
1957        // The order is a permutation of the positions, so inverting it needs every position to be
1958        // named exactly once. Anything else and the slice would have holes, and a caller indexing
1959        // it by a code would read a rank that belongs to nothing.
1960        if self.ranks == 0 || self.ranks != self.len() {
1961            return None;
1962        }
1963        self.code_ranks
1964            .get_or_init(|| {
1965                let mut ranks = vec![u32::MAX; self.ranks];
1966                // A block at a time rather than a rank at a time, because reading it per rank pays
1967                // for the bounds check, the division and the lock on every one of them.
1968                for first in (0..self.ranks).step_by(TEXT_RANK_BLOCK) {
1969                    let (block, _) = self.rank_parts(first).ok()?;
1970                    let count = self.rank_block_len(first);
1971                    let codes = self.rank_codes(block, count).ok()?;
1972                    for (within, code) in bitpack::unpack_tail(codes, self.code_bits, count)
1973                        .ok()?
1974                        .into_iter()
1975                        .enumerate()
1976                    {
1977                        let code = usize::try_from(code).ok()?;
1978                        *ranks.get_mut(code)? = u32::try_from(first + within).ok()?;
1979                    }
1980                }
1981                if ranks.contains(&u32::MAX) {
1982                    return None;
1983                }
1984                Some(ranks)
1985            })
1986            .as_deref()
1987    }
1988
1989    fn footprint(&self) -> usize {
1990        self.offsets.capacity()
1991            + self
1992                .code_ranks
1993                .get()
1994                .and_then(Option::as_ref)
1995                .map_or(0, |ranks| ranks.capacity() * size_of::<u32>())
1996            + self.rank_hashes.capacity() * size_of::<u64>()
1997            + self.rank_ends.capacity() * size_of::<u64>()
1998            + self.rank_blocks.capacity() * size_of::<OnceLock<Result<Vec<u8>>>>()
1999            + self
2000                .rank_blocks
2001                .iter()
2002                .filter_map(OnceLock::get)
2003                .filter_map(|result| result.as_ref().ok())
2004                .map(Vec::capacity)
2005                .sum::<usize>()
2006            + self.blocks.capacity() * size_of::<OnceLock<Result<Vec<u8>>>>()
2007            + self.hashes.capacity() * size_of::<u64>()
2008            + self.ends.capacity() * size_of::<u64>()
2009            + self
2010                .blocks
2011                .iter()
2012                .filter_map(OnceLock::get)
2013                .filter_map(|result| result.as_ref().ok())
2014                .map(Vec::capacity)
2015                .sum::<usize>()
2016    }
2017}
2018
2019/// Every table wide part number in order, with the stripe it belongs to.
2020fn places(table: &Table) -> Result<Vec<Place>> {
2021    let mut places = Vec::with_capacity(table.stripes.len().saturating_mul(STRIPE_PARTS));
2022    for (at, stripe) in table.stripes.iter().enumerate() {
2023        let index = u32::try_from(at).map_err(|_| invalid("too many stripes"))?;
2024        for (part, &rows) in stripe.parts.iter().enumerate() {
2025            places.push(Place {
2026                stripe: index,
2027                part: u32::try_from(part).map_err(|_| invalid("too many parts in a stripe"))?,
2028                rows,
2029            });
2030        }
2031    }
2032    Ok(places)
2033}
2034
2035/// Reads one column's section of a stripe's index page.
2036///
2037/// The section carries its own checksum, so a reader that wants one column out of a hundred and
2038/// five preads a few hundred bytes and still knows that what it got is what was written.
2039fn read_index(file: &File, stripe: &Stripe, column: usize) -> Result<Vec<PartSpan>> {
2040    let parts = stripe.parts.len();
2041    let section = index_section(parts)?;
2042    let at = column.checked_mul(section).ok_or_else(|| invalid("index page offset overflow"))?;
2043    let end = at.checked_add(section).ok_or_else(|| invalid("index page offset overflow"))?;
2044    if end > stripe.index.length as usize {
2045        return Err(invalid("index page is shorter than its columns"));
2046    }
2047    let page = stripe.pages.get(column).ok_or_else(|| invalid("stripe page is missing"))?;
2048    let mut bytes = vec![0; section];
2049    let offset = stripe
2050        .index
2051        .offset
2052        .checked_add(at as u64)
2053        .ok_or_else(|| invalid("index page offset overflow"))?;
2054    read_at(file, offset, &mut bytes)?;
2055    let entries = section - size_of::<u64>();
2056    let stored = u64::from_le_bytes(bytes[entries..].try_into().expect("eight bytes"));
2057    if checksum(&bytes[..entries]) != stored {
2058        // With where it was read from, because the two ways this fires look identical from the
2059        // message alone: a file somebody damaged, and a file we wrote to the wrong offset.
2060        return Err(invalid(&format!(
2061            "index page section checksum differs, column {column} of {parts} parts at {offset}, \
2062             wanted {stored:016x} and got {:016x}",
2063            checksum(&bytes[..entries]),
2064        )));
2065    }
2066    let mut spans = Vec::with_capacity(parts);
2067    let mut start = 0_usize;
2068    for part in 0..parts {
2069        let at = part * INDEX_ENTRY;
2070        let length = u32::from_le_bytes(bytes[at..at + 4].try_into().expect("four bytes")) as usize;
2071        let hash = u64::from_le_bytes(bytes[at + 4..at + 12].try_into().expect("eight bytes"));
2072        spans.push(PartSpan { start, length, hash });
2073        start = start.checked_add(length).ok_or_else(|| invalid("column page length overflow"))?;
2074    }
2075    if start != page.length as usize {
2076        return Err(invalid("column page length differs from its index"));
2077    }
2078    Ok(spans)
2079}
2080
2081/// One part's bytes out of a whole column page.
2082fn part_bytes(page: &[u8], span: PartSpan) -> Result<&[u8]> {
2083    let end = span.start.checked_add(span.length).ok_or_else(|| invalid("part range overflow"))?;
2084    page.get(span.start..end).ok_or_else(|| invalid("part exceeds its column page"))
2085}
2086
2087/// Puts one stripe of one column in the cache, dropping the stripe that has been there longest.
2088///
2089/// The index goes in its own slot and stays. Only the page is under the budget, and `kept` is how
2090/// many pages that budget is.
2091fn remember(cached: &mut Cached, held: &CachedColumn, kept: usize) {
2092    if let Some(slot) = cached.index.get_mut(held.stripe) {
2093        if slot.is_none() {
2094            *slot = Some(Arc::clone(&held.index));
2095        }
2096    }
2097    let Some(page) = held.page.clone() else { return };
2098    let Some(slot) = cached.pages.get_mut(held.stripe) else { return };
2099    if slot.is_none() {
2100        cached.order.push_back(held.stripe);
2101    }
2102    *slot = Some(page);
2103    while cached.order.len() > kept.max(1) {
2104        let Some(oldest) = cached.order.pop_front() else { break };
2105        if let Some(slot) = cached.pages.get_mut(oldest) {
2106            *slot = None;
2107        }
2108    }
2109}
2110
2111impl Reader {
2112    /// Opens the highest valid directory slot.
2113    ///
2114    /// # Errors
2115    ///
2116    /// If the file has no valid committed directory or a directory pointer is out of bounds.
2117    pub fn open(path: impl AsRef<Path>) -> Result<Self> {
2118        let mut file = File::open(path).map_err(io)?;
2119        let size = file.metadata().map_err(io)?.len();
2120        if size < HEADER {
2121            return Err(invalid("file is shorter than its header"));
2122        }
2123        let mut header = [0; HEADER as usize];
2124        file.read_exact(&mut header).map_err(io)?;
2125        let mut opening = Opening { reads: 1, bytes: HEADER };
2126        let version = u32::from_le_bytes([header[8], header[9], header[10], header[11]]);
2127        // The two halves are worth telling apart. A wrong magic is a file that was never ours and
2128        // the answer is to look at the path. A wrong version is our own file from another build,
2129        // and the number this build wants is the only thing that tells the reader whether to
2130        // rebuild the file or to go back to the binary that wrote it.
2131        if &header[..8] != MAGIC {
2132            return Err(invalid("the header does not begin with a rudb native magic"));
2133        }
2134        if version != FORMAT {
2135            return Err(invalid(&format!(
2136                "the file is format {version} and this build reads format {FORMAT}, so it has to \
2137                 be written again"
2138            )));
2139        }
2140        let mut selected = None;
2141        for start in [16, 16 + SLOT_BYTES] {
2142            let slot = Slot::read(&header[start..start + SLOT_BYTES]);
2143            if slot.generation == 0 || slot.length == 0 || slot.length as usize > MAX_DIRECTORY {
2144                continue;
2145            }
2146            let Some(end) = slot.offset.checked_add(u64::from(slot.length)) else { continue };
2147            if slot.offset < HEADER || end > size {
2148                continue;
2149            }
2150            let mut bytes = vec![0; slot.length as usize];
2151            file.seek(SeekFrom::Start(slot.offset)).map_err(io)?;
2152            file.read_exact(&mut bytes).map_err(io)?;
2153            opening.reads += 1;
2154            opening.bytes += u64::from(slot.length);
2155            if checksum(&bytes) == slot.hash
2156                && selected
2157                    .as_ref()
2158                    .is_none_or(|(old, _): &(Slot, Vec<u8>)| old.generation < slot.generation)
2159            {
2160                selected = Some((slot, bytes));
2161            }
2162        }
2163        let (slot, bytes) =
2164            selected.ok_or_else(|| invalid("no committed directory slot is valid"))?;
2165        let table = decode_directory(&bytes, size)?;
2166        let places = places(&table)?;
2167        let dictionaries = (0..table.fields.len()).map(|_| OnceLock::new()).collect();
2168        let table_fields = table.fields.len();
2169        let stripes = table.stripes.len();
2170        let cache = (0..table.fields.len())
2171            .map(|_| {
2172                Mutex::new(Cached {
2173                    pages: (0..stripes).map(|_| None).collect(),
2174                    index: (0..stripes).map(|_| None).collect(),
2175                    ..Cached::default()
2176                })
2177            })
2178            .collect::<Vec<_>>();
2179        let sieves: Vec<Vec<SieveSlot>> = (0..table.fields.len())
2180            .map(|_| table.stripes.iter().map(|_| OnceLock::new()).collect())
2181            .collect();
2182        let part_ranges: Vec<Vec<RangeSlot>> = (0..table.fields.len())
2183            .map(|_| table.stripes.iter().map(|_| OnceLock::new()).collect())
2184            .collect();
2185        Ok(Self {
2186            file: Arc::new(file),
2187            table: Arc::new(table),
2188            dictionaries: Arc::new(dictionaries),
2189            loading: Arc::new((0..table_fields).map(|_| Mutex::new(())).collect()),
2190            opened: Arc::new(AtomicUsize::new(0)),
2191            sieves: Arc::new(sieves),
2192            part_ranges: Arc::new(part_ranges),
2193            places: Arc::new(places),
2194            cache: Arc::new(cache),
2195            pages: Arc::new(AtomicUsize::new(0)),
2196            indexes: Arc::new(AtomicUsize::new(0)),
2197            kept: Arc::new(AtomicUsize::new(CACHED_STRIPES_PER_COLUMN)),
2198            size,
2199            directory: u64::from(slot.length),
2200            opening,
2201        })
2202    }
2203
2204    /// What this reader has read so far, and what opening it cost.
2205    ///
2206    /// Public because the claim of `spec/stats/04-in-memory.md` section 4.2 is about this number
2207    /// and a claim nobody can check is a comment. A caller that wants to know whether opening a
2208    /// file touched the data asks here, and gets an answer that does not depend on what the page
2209    /// cache happened to hold.
2210    #[must_use]
2211    pub fn reads(&self) -> Reads {
2212        Reads {
2213            opening: self.opening,
2214            pages: self.pages.load(Atomic::Relaxed),
2215            indexes: self.indexes.load(Atomic::Relaxed),
2216            dictionaries: self.opened.load(Atomic::Relaxed),
2217        }
2218    }
2219
2220    /// Where the file's bytes went, from the directory alone.
2221    ///
2222    /// No page is read, so this costs the same on a 45 GB table as on an empty one. See [`Layout`]
2223    /// for what is charged where and for why the three things that are not columns stay separate.
2224    #[must_use]
2225    pub fn layout(&self) -> Layout {
2226        let table = &self.table;
2227        let stripes = table.stripes.as_slice();
2228        let columns = table
2229            .fields
2230            .iter()
2231            .enumerate()
2232            .map(|(at, field)| ColumnLayout {
2233                name: field.name.clone(),
2234                kind: field.ty.to_string(),
2235                pages: sum(stripes.iter().map(|stripe| span_bytes(&stripe.pages, at))),
2236                memberships: sum(stripes.iter().map(|stripe| page_bytes(&stripe.memberships, at))),
2237                sieves: sum(stripes.iter().map(|stripe| page_bytes(&stripe.sieves, at))),
2238                part_ranges: sum(stripes.iter().map(|stripe| page_bytes(&stripe.part_ranges, at))),
2239                dictionary: page_bytes(&table.dictionaries, at),
2240            })
2241            .collect();
2242        Layout {
2243            file: self.size,
2244            rows: table.rows,
2245            stripes: stripes.len(),
2246            parts: self.places.len(),
2247            columns,
2248            indexes: sum(stripes.iter().map(|stripe| u64::from(stripe.index.length))),
2249            directory: self.directory,
2250            header: HEADER,
2251        }
2252    }
2253
2254    /// How many parts the table has, which is how many chunks a scan of it reads.
2255    #[must_use]
2256    pub fn parts(&self) -> usize {
2257        self.places.len()
2258    }
2259
2260    /// The parts of each stripe, in table wide part numbers.
2261    ///
2262    /// A scan that wants one worker to own the page it reads hands work out in these runs. The
2263    /// stripes are contiguous in part numbering and all but the last hold sixty four parts, but a
2264    /// stripe can be flushed early when rows arrive out of order, so the runs are read off the
2265    /// directory rather than worked out from a constant.
2266    #[must_use]
2267    pub fn stripe_parts(&self) -> Vec<std::ops::Range<usize>> {
2268        let mut runs = Vec::with_capacity(self.table.stripes.len());
2269        let mut start = 0;
2270        for stripe in &self.table.stripes {
2271            let end = start + stripe.parts.len();
2272            runs.push(start..end);
2273            start = end;
2274        }
2275        runs
2276    }
2277
2278    /// Asks the page cache to keep `stripes` stripes of every column instead of the default.
2279    ///
2280    /// This only ever raises the number. A scan that gives each worker a whole stripe has one page
2281    /// per column per worker open at once, and a cache smaller than that is worse than no cache at
2282    /// all: every worker's page is evicted by the others before it has finished its stripe, so it
2283    /// reads a quarter of a megabyte for every part it takes out of it.
2284    pub fn keep_stripes(&self, stripes: usize) {
2285        self.kept.fetch_max(stripes, Atomic::Relaxed);
2286    }
2287
2288    /// Rows in one part, or zero when the part number is past the table.
2289    #[must_use]
2290    pub fn part_rows(&self, at: usize) -> usize {
2291        self.places.get(at).map_or(0, |place| place.rows as usize)
2292    }
2293
2294    /// The committed table directory.
2295    #[must_use]
2296    pub fn table(&self) -> &Table {
2297        &self.table
2298    }
2299
2300    /// Exact leading frequencies when the stored synopsis proves a count-descending prefix.
2301    ///
2302    /// The returned list can be longer than `top`. Keeping the stored tail lets a later TopN apply
2303    /// additional ordering keys without losing a value tied with the requested boundary.
2304    ///
2305    /// # Errors
2306    ///
2307    /// If the column is outside the schema or a stored value does not fit its declared type.
2308    pub fn top_frequencies(&self, column: usize, top: usize) -> Result<Option<Vec<(Value, u64)>>> {
2309        let field = self
2310            .table
2311            .fields
2312            .get(column)
2313            .ok_or_else(|| invalid("frequency column index out of range"))?;
2314        let Some(summary) = self.table.frequencies.get(column).and_then(Option::as_ref) else {
2315            return Ok(None);
2316        };
2317        if top == 0 || summary.entries.len() < top {
2318            return Ok(None);
2319        }
2320        let boundary = summary.entries[top - 1].count;
2321        if boundary <= summary.omitted_max {
2322            return Ok(None);
2323        }
2324        self.decode_frequencies(column, &field.ty, &summary.entries).map(Some)
2325    }
2326
2327    /// Every value of one column with the number of rows holding it, when the synopsis is complete.
2328    ///
2329    /// The heavy hitter pass keeps a bounded set of candidates and decrements them all when it runs
2330    /// out of room, so what it usually ends with is the leading values and a bound on everything it
2331    /// dropped. `omitted_max` of zero says that never happened: no candidate was ever decremented and
2332    /// the entries did not overflow the stored budget, so the list is every distinct value of the
2333    /// column with an exact count, and a null counts as a value of its own rather than being skipped.
2334    ///
2335    /// That makes a whole class of question answerable without reading a row. How many rows hold a
2336    /// value, how many do not, and what a `GROUP BY` of that column with a count over it produces are
2337    /// all in here. It is only ever true of a column with few enough distinct values, which is the
2338    /// case worth having, because that is exactly the column a grouping or an equality filter would
2339    /// otherwise walk every row to answer.
2340    ///
2341    /// `None` when the column has no synopsis, or has one that dropped anything.
2342    ///
2343    /// # Errors
2344    ///
2345    /// If the column is outside the schema or a stored value does not fit its declared type.
2346    pub fn exact_frequencies(&self, column: usize) -> Result<Option<Vec<(Value, u64)>>> {
2347        let field = self
2348            .table
2349            .fields
2350            .get(column)
2351            .ok_or_else(|| invalid("frequency column index out of range"))?;
2352        let Some(summary) = self.table.frequencies.get(column).and_then(Option::as_ref) else {
2353            return Ok(None);
2354        };
2355        if summary.omitted_max > 0 {
2356            return Ok(None);
2357        }
2358        self.decode_frequencies(column, &field.ty, &summary.entries).map(Some)
2359    }
2360
2361    /// Turns stored frequency entries into values of the column's own type.
2362    fn decode_frequencies(
2363        &self,
2364        column: usize,
2365        ty: &LogicalType,
2366        entries: &[FrequencyEntry],
2367    ) -> Result<Vec<(Value, u64)>> {
2368        let dictionary = if *ty == LogicalType::Varchar { self.dictionary(column)? } else { None };
2369        let mut out = Vec::with_capacity(entries.len());
2370        for entry in entries {
2371            let value = match entry.value {
2372                FrequencyValue::Null => Value::Null,
2373                FrequencyValue::Integer(value) => match *ty {
2374                    LogicalType::TinyInt => Value::TinyInt(
2375                        i8::try_from(value)
2376                            .map_err(|_| invalid("frequency TINYINT is out of range"))?,
2377                    ),
2378                    LogicalType::UTinyInt => Value::UTinyInt(
2379                        u8::try_from(value)
2380                            .map_err(|_| invalid("frequency UTINYINT is out of range"))?,
2381                    ),
2382                    LogicalType::USmallInt => Value::USmallInt(
2383                        u16::try_from(value)
2384                            .map_err(|_| invalid("frequency USMALLINT is out of range"))?,
2385                    ),
2386                    LogicalType::UInteger => Value::UInteger(
2387                        u32::try_from(value)
2388                            .map_err(|_| invalid("frequency UINTEGER is out of range"))?,
2389                    ),
2390                    LogicalType::UBigInt => Value::UBigInt(
2391                        u64::try_from(value)
2392                            .map_err(|_| invalid("frequency UBIGINT is out of range"))?,
2393                    ),
2394                    LogicalType::SmallInt => Value::SmallInt(
2395                        i16::try_from(value)
2396                            .map_err(|_| invalid("frequency SMALLINT is out of range"))?,
2397                    ),
2398                    LogicalType::Integer => Value::Integer(
2399                        i32::try_from(value)
2400                            .map_err(|_| invalid("frequency INTEGER is out of range"))?,
2401                    ),
2402                    LogicalType::BigInt => Value::BigInt(
2403                        i64::try_from(value)
2404                            .map_err(|_| invalid("frequency BIGINT is out of range"))?,
2405                    ),
2406                    LogicalType::Date => Value::Date(
2407                        i32::try_from(value)
2408                            .map_err(|_| invalid("frequency DATE is out of range"))?,
2409                    ),
2410                    LogicalType::Timestamp => Value::Timestamp(
2411                        i64::try_from(value)
2412                            .map_err(|_| invalid("frequency TIMESTAMP is out of range"))?,
2413                    ),
2414                    _ => return Err(invalid("integer frequency belongs to another type")),
2415                },
2416                FrequencyValue::Code(code) => dictionary
2417                    .as_ref()
2418                    .ok_or_else(|| invalid("frequency code has no dictionary"))?
2419                    .try_value_at(code as usize)?,
2420            };
2421            out.push((value, entry.count));
2422        }
2423        Ok(out)
2424    }
2425
2426    /// Sparse rows belonging to the bounded numeric frequency candidate set.
2427    ///
2428    /// The list is omitted when collecting it would exceed the fixed storage budget. A composite
2429    /// aggregate may accept a result over these rows only when its requested boundary is strictly
2430    /// greater than `omitted_max`.
2431    ///
2432    /// # Errors
2433    ///
2434    /// If the column is outside the schema.
2435    pub fn frequency_occurrences(&self, column: usize) -> Result<Option<FrequencyOccurrences>> {
2436        self.table
2437            .fields
2438            .get(column)
2439            .ok_or_else(|| invalid("frequency column index out of range"))?;
2440        let Some(summary) = self.table.frequencies.get(column).and_then(Option::as_ref) else {
2441            return Ok(None);
2442        };
2443        if summary.ordinals.is_empty() {
2444            return Ok(None);
2445        }
2446        Ok(Some(FrequencyOccurrences {
2447            omitted_max: summary.omitted_max,
2448            ordinals: summary.ordinals.clone(),
2449        }))
2450    }
2451
2452    /// How many distinct values one column holds, counting a null as no value.
2453    ///
2454    /// A string column of this format is written against one dictionary that covers the whole table.
2455    /// A code is handed out the first time a value is seen and nothing ever removes one, so the
2456    /// number of codes is the number of distinct values exactly rather than an estimate. That makes
2457    /// `COUNT(DISTINCT column)` over a whole table a question the directory already knows the answer
2458    /// to, and the alternative is a hash table with a row per distinct value built from a pass over
2459    /// every row.
2460    ///
2461    /// A null in the column used to make this `None` and no longer does. A null row is written as
2462    /// the code for the empty string, so a nullable column's dictionary can hold an empty string
2463    /// that no row of it actually has, and the dictionary on its own does not say which case it is.
2464    /// The writer does know, because it counts the non-null rows that use each code on its way to
2465    /// the frequency summary, so it records how many codes any row holds and the directory carries
2466    /// that number. This reads it rather than the size of the dictionary, which also means the
2467    /// dictionary page is not opened to answer.
2468    ///
2469    /// `None` for a column the file has no dictionary for, which is every column that is not a
2470    /// string. A sketch would answer that approximately and SQL asked for the exact number.
2471    ///
2472    /// # Errors
2473    ///
2474    /// If the column is outside the schema.
2475    pub fn distinct_values(&self, column: usize) -> Result<Option<u64>> {
2476        self.table
2477            .distincts
2478            .get(column)
2479            .copied()
2480            .ok_or_else(|| invalid("distinct column index out of range"))
2481    }
2482
2483    /// How many rows of one column are null, added up over the stripes.
2484    ///
2485    /// Every stripe records this exactly when it is written, because a null count is not a bound
2486    /// that is allowed to be wide the way a minimum and a maximum are: a filter that reads one too
2487    /// many is slow and a `COUNT` that reads one too many is wrong. Adding up a few hundred numbers
2488    /// already in memory is what makes `COUNT(column)` over a whole table free.
2489    ///
2490    /// # Errors
2491    ///
2492    /// If the column is outside the schema.
2493    pub fn null_count(&self, column: usize) -> Result<u64> {
2494        if column >= self.table.fields.len() {
2495            return Err(invalid("null count column index out of range"));
2496        }
2497        let mut nulls = 0_u64;
2498        for stripe in &self.table.stripes {
2499            let range = stripe
2500                .zone
2501                .column(column)
2502                .ok_or_else(|| invalid("stripe zone is narrower than the schema"))?;
2503            nulls = nulls
2504                .checked_add(range.nulls as u64)
2505                .ok_or_else(|| invalid("null count overflow"))?;
2506        }
2507        Ok(nulls)
2508    }
2509
2510    /// The smallest and the largest value of one string column, from the order beside its values.
2511    ///
2512    /// The dictionary holds exactly the values the column holds, so the first and the last of them
2513    /// in sorted order are the column's minimum and maximum. Two reads of a rank block settle what
2514    /// otherwise walks a million rows.
2515    ///
2516    /// `None` when the column is not a string, when the file was written before version 9 and so has
2517    /// no order, when the column has no values at all, or when it has a null in it, which is the
2518    /// placeholder again: the empty string a null is written as would sort ahead of every real
2519    /// value and be reported as the minimum.
2520    ///
2521    /// # Errors
2522    ///
2523    /// If the column is outside the schema, or a rank names a code the dictionary does not have.
2524    pub fn text_extremes(&self, column: usize) -> Result<Option<(Value, Value)>> {
2525        if self.null_count(column)? > 0 {
2526            return Ok(None);
2527        }
2528        let Some(dictionary) = self.dictionary(column)? else { return Ok(None) };
2529        let Some(ranks) = dictionary.ranks() else { return Ok(None) };
2530        if ranks == 0 {
2531            return Ok(None);
2532        }
2533        let low = text_at_rank(&dictionary, 0)?;
2534        let high = text_at_rank(&dictionary, ranks - 1)?;
2535        Ok(Some((low, high)))
2536    }
2537
2538    /// The smallest and the largest value of one column, when every stripe wrote exact ends.
2539    ///
2540    /// A stripe's ends are allowed to be wider than the truth, because a bound that rules out a
2541    /// chunk that could not match is still correct when it rules out nothing. That is what makes
2542    /// them cheap to write for a bit packed or a dictionary column, and it is also what stops them
2543    /// answering a `MIN`. So each stripe says which of the two it wrote, and this answers only when
2544    /// all of them walked their rows.
2545    ///
2546    /// `None` for a column with no ends, for an empty table, and for a column any stripe of which
2547    /// guessed. Nulls need no special case, because the ends skip them the same way `MIN` does.
2548    ///
2549    /// One case is given up on that did not have to be. A stripe merges the ends of its sixty four
2550    /// parts, and a part with no ends at all erases the merged ones, because a part whose rows are
2551    /// not covered by the stripe's ends is a stripe that would skip rows it should keep. A part of
2552    /// nothing but nulls has no rows to cover and so did not need to erase anything, but the merge
2553    /// cannot tell that part from a part whose layout it could not read. So a column with a chunk
2554    /// of nothing but nulls in the middle of it goes and reads the rows. That is slow and right,
2555    /// and the fix is a row count per part rather than anything here.
2556    ///
2557    /// # Errors
2558    ///
2559    /// If the column is outside the schema.
2560    pub fn exact_extremes(&self, column: usize) -> Result<Option<(Bound, Bound)>> {
2561        if column >= self.table.fields.len() {
2562            return Err(invalid("extremes column index out of range"));
2563        }
2564        let mut low: Option<Bound> = None;
2565        let mut high: Option<Bound> = None;
2566        for stripe in &self.table.stripes {
2567            let range = stripe
2568                .zone
2569                .column(column)
2570                .ok_or_else(|| invalid("stripe zone is narrower than the schema"))?;
2571            if !range.exact {
2572                return Ok(None);
2573            }
2574            // A stripe of nothing but nulls has no ends and says nothing about the column's, which
2575            // is why this skips it rather than giving up on the whole column. A stripe that has
2576            // rows and still has no end is a layout whose values this cannot see, and skipping that
2577            // one would answer with an end taken from the other stripes, so it gives up instead.
2578            let (Some(small), Some(large)) = (range.low.as_ref(), range.high.as_ref()) else {
2579                if stripe.rows > range.nulls {
2580                    return Ok(None);
2581                }
2582                continue;
2583            };
2584            low = Some(low.map_or_else(|| small.clone(), |held| held.smaller(small.clone())));
2585            high = Some(high.map_or_else(|| large.clone(), |held| held.larger(large.clone())));
2586        }
2587        Ok(low.zip(high))
2588    }
2589
2590    /// The sum of one integer column and how many rows went into it, when every stripe wrote one.
2591    ///
2592    /// The count beside the sum is the non-null rows, because that is what a `SUM` adds up and what
2593    /// an `AVG` divides by, and a caller that had to work it out from the row count and the null
2594    /// count would be doing the same walk twice.
2595    ///
2596    /// `None` for anything that is not an integer column, for a file written by something that did
2597    /// not record it, and when adding the stripes together would overflow.
2598    ///
2599    /// # Errors
2600    ///
2601    /// If the column is outside the schema.
2602    pub fn exact_sum(&self, column: usize) -> Result<Option<(i128, u64)>> {
2603        if column >= self.table.fields.len() {
2604            return Err(invalid("sum column index out of range"));
2605        }
2606        let mut total = 0_i128;
2607        let mut rows = 0_u64;
2608        for stripe in &self.table.stripes {
2609            let range = stripe
2610                .zone
2611                .column(column)
2612                .ok_or_else(|| invalid("stripe zone is narrower than the schema"))?;
2613            let Some(part) = range.sum else { return Ok(None) };
2614            let Some(sum) = total.checked_add(part) else { return Ok(None) };
2615            total = sum;
2616            rows = rows.saturating_add(stripe.rows as u64 - range.nulls as u64);
2617        }
2618        Ok(Some((total, rows)))
2619    }
2620
2621    /// The global dictionary of a column, opened once however many workers ask for it at once.
2622    ///
2623    /// The unlocked look is first because it is the answer every time after the first and it costs a
2624    /// load. Everybody who misses it queues on [`Self::loading`] and looks again on the way in, so
2625    /// the one who arrived first does the reading and the rest take what it left. Waiting is the
2626    /// cheaper thing to do: the work behind the lock is a page read, a checksum and the decode of a
2627    /// dictionary that can hold half a million entries, and the alternative is every worker of the
2628    /// scan doing all of it and all but one dropping the result on the floor.
2629    fn dictionary(&self, column: usize) -> Result<Option<Arc<Vector>>> {
2630        let Some(page) = self.table.dictionaries[column] else { return Ok(None) };
2631        if let Some(dictionary) = self.dictionaries[column].get() {
2632            return Ok(Some(Arc::clone(dictionary)));
2633        }
2634        let _queued = self.loading[column].lock().map_err(|_| invalid("a poisoned dictionary"))?;
2635        if let Some(dictionary) = self.dictionaries[column].get() {
2636            return Ok(Some(Arc::clone(dictionary)));
2637        }
2638        self.opened.fetch_add(1, Atomic::Relaxed);
2639        let dictionary = Arc::new(open_global_dictionary(
2640            Arc::clone(&self.file),
2641            page,
2642            &self.table.fields[column].ty,
2643            TEXT_KEEP_BUDGET,
2644        )?);
2645        let _ = self.dictionaries[column].set(Arc::clone(&dictionary));
2646        Ok(Some(dictionary))
2647    }
2648
2649    /// Reads only the named columns from one part.
2650    ///
2651    /// The whole stripe page each column lives in is read and kept, because a scan asks for the
2652    /// parts of a stripe one after another and this is what turns sixty four reads into one.
2653    ///
2654    /// # Errors
2655    ///
2656    /// If a part, column, page, or checksum is invalid.
2657    pub fn read(&self, part: usize, columns: &[usize]) -> Result<Chunk> {
2658        self.read_impl(part, columns, true)
2659    }
2660
2661    /// Reads named columns from one part without keeping the stripe page it came out of.
2662    ///
2663    /// This is for sparse row fetches after a selective TopN or filter, which reach a few parts of
2664    /// a stripe rather than all of them. A caller that will read most of a stripe should use
2665    /// [`Self::read`] instead, because this reads and discards the page index every time.
2666    ///
2667    /// # Errors
2668    ///
2669    /// If a part, column, page, or checksum is invalid.
2670    pub fn read_sparse(&self, part: usize, columns: &[usize]) -> Result<Chunk> {
2671        self.read_impl(part, columns, false)
2672    }
2673
2674    /// Whether an exact global-code membership index proves that the stripe holding a part cannot
2675    /// contain any of the sorted candidate codes.
2676    ///
2677    /// # Errors
2678    ///
2679    /// If the part, column, index page, checksum, or delta stream is invalid.
2680    pub fn skips_codes(&self, part: usize, column: usize, candidates: &[u32]) -> Result<bool> {
2681        if candidates.is_empty() {
2682            return Ok(true);
2683        }
2684        if candidates.windows(2).any(|pair| pair[0] >= pair[1]) {
2685            return Err(Error::internal("native code candidates are not sorted and unique"));
2686        }
2687        let stripe = self.stripe_of(part)?;
2688        let Some(page) = stripe.memberships.get(column).copied().flatten() else {
2689            return Ok(false);
2690        };
2691        let mut bytes = vec![0; page.length as usize];
2692        read_at(&self.file, page.offset, &mut bytes)?;
2693        if checksum(&bytes) != page.hash {
2694            return Err(invalid("membership page checksum differs"));
2695        }
2696        let codes = decode_membership(&bytes)?;
2697        let mut left = 0;
2698        let mut right = 0;
2699        while left < codes.len() && right < candidates.len() {
2700            match codes[left].cmp(&candidates[right]) {
2701                Ordering::Less => left += 1,
2702                Ordering::Greater => right += 1,
2703                Ordering::Equal => return Ok(false),
2704            }
2705        }
2706        Ok(true)
2707    }
2708
2709    fn stripe_of(&self, part: usize) -> Result<&Stripe> {
2710        let place = self.places.get(part).ok_or_else(|| invalid("part index out of range"))?;
2711        self.table
2712            .stripes
2713            .get(place.stripe as usize)
2714            .ok_or_else(|| invalid("stripe index out of range"))
2715    }
2716
2717    /// The page index of one column of one stripe, and its page when the caller wants all of it.
2718    ///
2719    /// A scan hands parts out in order, so every worker on a column crosses into a new stripe within
2720    /// a few parts of the others and they all want the same page at the same moment. This used to
2721    /// let all of them read it, which cost the scan as many copies of every page as it had workers.
2722    /// On the full ClickBench file a `MIN(EventDate), MAX(EventDate)` moved 3.2 GB off the disk to
2723    /// look at 400 MB of column.
2724    ///
2725    /// A worker that finds the page it wants already being read neither waits for it nor reads it
2726    /// again. It comes back with the index alone, which sends [`Reader::read_impl`] down the path
2727    /// that reads the one part it came for, a few kilobytes against a quarter of a megabyte, and it
2728    /// picks the page up from the cache on its next part. Waiting would be the other way to avoid
2729    /// the duplicate read and it is worse: the pages that matter are the wide string ones, they take
2730    /// milliseconds to copy even warm, and every other worker would be stopped for all of it.
2731    ///
2732    /// The file is never read under the lock.
2733    fn held(&self, at: usize, stripe: &Stripe, column: usize, whole: bool) -> Result<CachedColumn> {
2734        let cache = self.cache.get(column).ok_or_else(|| invalid("column index out of range"))?;
2735        let mut cached = cache.lock().map_err(|_| invalid("column page cache is poisoned"))?;
2736        let known = cached.index.get(at).and_then(Clone::clone);
2737        let page = cached.pages.get(at).and_then(Clone::clone);
2738        if let Some(index) = known.clone() {
2739            if !whole || page.is_some() {
2740                return Ok(CachedColumn { stripe: at, index, page });
2741            }
2742        }
2743        if cached.loading.contains(&at) {
2744            drop(cached);
2745            // The index is almost always already here, because somebody read this stripe to get
2746            // into the loading list in the first place, so this branch usually costs no read at
2747            // all and the one part read in `read_impl` is all the losing worker pays for.
2748            if let Some(index) = known {
2749                return Ok(CachedColumn { stripe: at, index, page: None });
2750            }
2751            let held = self.page_of(stripe, column, at, false, None)?;
2752            let mut cached = cache.lock().map_err(|_| invalid("column page cache is poisoned"))?;
2753            remember(&mut cached, &held, self.kept.load(Atomic::Relaxed));
2754            return Ok(held);
2755        }
2756        cached.loading.push(at);
2757        drop(cached);
2758
2759        let read = self.page_of(stripe, column, at, whole, known);
2760
2761        // The stripe leaves the loading list and its page enters the cache under one lock. Doing
2762        // them separately would leave a moment where another worker sees neither and reads the
2763        // page a second time, which is the whole thing this is here to stop.
2764        let mut cached = cache.lock().map_err(|_| invalid("column page cache is poisoned"))?;
2765        if let Some(position) = cached.loading.iter().position(|loading| *loading == at) {
2766            cached.loading.remove(position);
2767        }
2768        let held = read?;
2769        remember(&mut cached, &held, self.kept.load(Atomic::Relaxed));
2770        Ok(held)
2771    }
2772
2773    /// Reads one stripe's index for a column, and its page when the caller wants all of it.
2774    ///
2775    /// `known` is the index when the reader has already read it, which after the first worker
2776    /// through a stripe it always has, because [`remember`] keeps every index for the life of the
2777    /// reader. Without that a scan reads the index again on every part that misses the page cache.
2778    fn page_of(
2779        &self,
2780        stripe: &Stripe,
2781        column: usize,
2782        at: usize,
2783        whole: bool,
2784        known: Option<Arc<Vec<PartSpan>>>,
2785    ) -> Result<CachedColumn> {
2786        let index = match known {
2787            Some(index) => index,
2788            None => {
2789                self.indexes.fetch_add(1, Atomic::Relaxed);
2790                Arc::new(read_index(&self.file, stripe, column)?)
2791            }
2792        };
2793        let page = if whole {
2794            self.pages.fetch_add(1, Atomic::Relaxed);
2795            let span = stripe.pages.get(column).ok_or_else(|| invalid("stripe page is missing"))?;
2796            let mut bytes = vec![0; span.length as usize];
2797            read_at(&self.file, span.offset, &mut bytes)?;
2798            Some(Arc::new(bytes))
2799        } else {
2800            None
2801        };
2802        Ok(CachedColumn { stripe: at, index, page })
2803    }
2804
2805    fn read_impl(&self, at: usize, columns: &[usize], whole: bool) -> Result<Chunk> {
2806        let place = *self.places.get(at).ok_or_else(|| invalid("part index out of range"))?;
2807        let index = place.stripe as usize;
2808        let stripe =
2809            self.table.stripes.get(index).ok_or_else(|| invalid("stripe index out of range"))?;
2810        let rows = place.rows as usize;
2811        let mut picked = Vec::with_capacity(columns.len());
2812        for &column in columns {
2813            let field = self
2814                .table
2815                .fields
2816                .get(column)
2817                .ok_or_else(|| invalid("column index out of range"))?;
2818            let page = stripe.pages.get(column).ok_or_else(|| invalid("stripe page is missing"))?;
2819            let held = self.held(index, stripe, column, whole)?;
2820            let span = *held
2821                .index
2822                .get(place.part as usize)
2823                .ok_or_else(|| invalid("part index out of range"))?;
2824            let owned;
2825            let bytes = match &held.page {
2826                Some(held) => part_bytes(held, span)?,
2827                None => {
2828                    let offset = page
2829                        .offset
2830                        .checked_add(span.start as u64)
2831                        .ok_or_else(|| invalid("part range overflow"))?;
2832                    let mut bytes = vec![0; span.length];
2833                    read_at(&self.file, offset, &mut bytes)?;
2834                    owned = bytes;
2835                    &owned
2836                }
2837            };
2838            if checksum(bytes) != span.hash {
2839                return Err(invalid(&format!(
2840                    "column page checksum differs, column {column} part {} at {}+{} of {} bytes, \
2841                     wanted {:016x} and got {:016x}",
2842                    place.part,
2843                    page.offset,
2844                    span.start,
2845                    span.length,
2846                    span.hash,
2847                    checksum(bytes),
2848                )));
2849            }
2850            let dictionary = self.dictionary(column)?;
2851            picked.push(decode(&field.ty, rows, bytes, dictionary)?);
2852        }
2853        Chunk::with_rows(picked, rows)
2854    }
2855
2856    /// Whether persisted statistics prove that a part cannot match the predicates.
2857    ///
2858    /// Three of them, asked cheapest first.
2859    ///
2860    /// The stripe's bounds are in memory already, so they are free, and they are also the coarsest:
2861    /// every part of a stripe gets the same answer and a scan that skips one part that way skips all
2862    /// sixty four. Then the part's own bounds, which are a read of one page per column per stripe
2863    /// and are sixty four times finer. Then the sieves, which are per part and answer equality, the
2864    /// test bounds are worst at: a column of identifiers has every stripe and nearly every part
2865    /// covering the whole of its type, so bounds keep them all and the sieve keeps the ones that
2866    /// really hold the value.
2867    ///
2868    /// The middle one is what an ordered comparison on a column the rows are not sorted by needs. On
2869    /// ClickBench 24 the stripe bounds leave eight stripes of sixteen alive, which is half the file,
2870    /// and the part bounds leave thirty parts of nine hundred and seventy four.
2871    #[must_use]
2872    pub fn skips(&self, part: usize, probes: &[Probe]) -> bool {
2873        let Some(place) = self.places.get(part).copied() else { return false };
2874        let Some(stripe) = self.table.stripes.get(place.stripe as usize) else { return false };
2875        if stripe.zone.skips(probes) {
2876            return true;
2877        }
2878        probes.iter().any(|probe| self.outside(place, probe) || self.sifted(place, probe))
2879    }
2880
2881    /// Whether the bounds of one part rule out one probe.
2882    ///
2883    /// The part's own two ends, which are narrower than the stripe's and cost a page read the first
2884    /// time this is asked about a column. A column with no page here answers `false`, which is the
2885    /// answer a caller got before there were any.
2886    fn outside(&self, place: Place, probe: &Probe) -> bool {
2887        match self.stripe_part_ranges(place.stripe as usize, probe.column) {
2888            Some(ranges) => ranges
2889                .get(place.part as usize)
2890                .is_some_and(|range| range.excludes(probe.op, &probe.value)),
2891            None => false,
2892        }
2893    }
2894
2895    /// The per part ranges of one stripe of one column, read once and kept.
2896    ///
2897    /// `None` when the column has no page in that stripe and when the page is damaged, on the same
2898    /// reasoning as the sieves: this is an index over data that is still there, so a caller that
2899    /// cannot read one reads the rows and gets the right answer slowly.
2900    fn stripe_part_ranges(&self, stripe: usize, column: usize) -> Option<&[Range]> {
2901        let slot = self.part_ranges.get(column)?.get(stripe)?;
2902        if let Some(held) = slot.get() {
2903            return Some(held);
2904        }
2905        let page = self.table.stripes.get(stripe)?.part_ranges.get(column).copied().flatten()?;
2906        let mut bytes = vec![0; page.length as usize];
2907        read_at(&self.file, page.offset, &mut bytes).ok()?;
2908        if checksum(&bytes) != page.hash {
2909            return None;
2910        }
2911        let ranges = Arc::new(decode_part_ranges(&bytes).ok()?);
2912        let _ = slot.set(ranges);
2913        slot.get().map(|held| held.as_slice())
2914    }
2915
2916    /// Whether persisted statistics prove that every row of a part matches the predicates.
2917    ///
2918    /// Only the bounds, and only the stripe's. The sieves say nothing here, because a sieve that
2919    /// holds a value is a sieve that may be holding somebody else's hash, so it can rule a part out
2920    /// and can never wave one through.
2921    ///
2922    /// The stripe's bounds are wider than the part's and its null count covers sixty four parts
2923    /// rather than one, and both of those are the safe direction. A stretch where everything passes
2924    /// contains no narrower stretch where something fails, and a stripe with no nulls in it has no
2925    /// nulls in any of its parts. So this answers `false` for parts it could have waved through if
2926    /// the directory recorded bounds that finely, which costs a comparison and never costs rows.
2927    #[must_use]
2928    pub fn certain(&self, part: usize, probes: &[Probe]) -> bool {
2929        let Some(place) = self.places.get(part).copied() else { return false };
2930        let Some(stripe) = self.table.stripes.get(place.stripe as usize) else { return false };
2931        stripe.zone.certain(probes)
2932    }
2933
2934    /// Whether the bounds of one stripe prove that none of its parts can match the predicates.
2935    ///
2936    /// The cheap half of [`Self::skips`], asked about a whole stripe at once. The bounds live in the
2937    /// directory and are already in memory, so this answers without touching the file, and that is
2938    /// the reason it is worth having on its own: a caller that wants to know roughly where the work
2939    /// is before it starts any workers can ask this about sixteen stripes for nothing, where asking
2940    /// [`Self::skips`] about nine hundred parts would read and decode a sieve page per stripe first.
2941    ///
2942    /// It keeps stripes that [`Self::skips`] would rule out part by part, which is the right way for
2943    /// it to be wrong: the parts are still checked when they are read.
2944    #[must_use]
2945    pub fn stripe_skips(&self, stripe: usize, probes: &[Probe]) -> bool {
2946        self.table.stripes.get(stripe).is_some_and(|held| held.zone.skips(probes))
2947    }
2948
2949    /// Whether the sieve of one part rules out one probe.
2950    ///
2951    /// Only equality. An ordered comparison is what the bounds are for and a sieve says nothing
2952    /// about it, and a read that cannot answer keeps the part, which is the answer a caller with no
2953    /// sieve gets anyway.
2954    fn sifted(&self, place: Place, probe: &Probe) -> bool {
2955        if probe.op != Op::Equal {
2956            return false;
2957        }
2958        match self.stripe_sieves(place.stripe as usize, probe.column) {
2959            Some(sieves) => sieves
2960                .get(place.part as usize)
2961                .and_then(Option::as_ref)
2962                .is_some_and(|sieve| sieve.excludes(&probe.value)),
2963            None => false,
2964        }
2965    }
2966
2967    /// The sieves of one stripe of one column, read once and kept.
2968    ///
2969    /// `None` when the column has no sieves in that stripe, when the page is damaged, and when the
2970    /// bytes are not a page this version can read. A sieve is an index over data that is still there
2971    /// and a caller that cannot read one reads the rows, so this is the one place in the file where
2972    /// a bad checksum is a slow query rather than an error.
2973    fn stripe_sieves(&self, stripe: usize, column: usize) -> Option<&[Option<Sieve>]> {
2974        let slot = self.sieves.get(column)?.get(stripe)?;
2975        if let Some(held) = slot.get() {
2976            return Some(held);
2977        }
2978        let page = self.table.stripes.get(stripe)?.sieves.get(column).copied().flatten()?;
2979        let mut bytes = vec![0; page.length as usize];
2980        read_at(&self.file, page.offset, &mut bytes).ok()?;
2981        if checksum(&bytes) != page.hash {
2982            return None;
2983        }
2984        let sieves = Arc::new(decode_sieves(&bytes).ok()?);
2985        let _ = slot.set(sieves);
2986        slot.get().map(|held| held.as_slice())
2987    }
2988}
2989
2990/// The value sitting at one position of a dictionary's sorted order.
2991fn text_at_rank(dictionary: &Vector, rank: usize) -> Result<Value> {
2992    let code = dictionary.code_at_rank(rank)? as usize;
2993    let text = dictionary
2994        .try_text_at(code)?
2995        .ok_or_else(|| invalid("global dictionary order names a code it does not have"))?;
2996    Ok(Value::Varchar(text.into()))
2997}
2998
2999/// Writes one span of a file at an offset, without depending on where the cursor is.
3000///
3001/// The writer owns an offset of its own and passes it in here, so that nothing it writes depends on
3002/// a cursor that a read is entitled to move. Both of these can come back short and both loop.
3003#[cfg(unix)]
3004fn write_at(file: &File, mut offset: u64, mut bytes: &[u8]) -> Result<()> {
3005    use std::os::unix::fs::FileExt;
3006    while !bytes.is_empty() {
3007        let written = file.write_at(bytes, offset).map_err(io)?;
3008        if written == 0 {
3009            return Err(invalid("a write to the native file wrote nothing"));
3010        }
3011        offset += written as u64;
3012        bytes = &bytes[written..];
3013    }
3014    Ok(())
3015}
3016
3017/// The same write, on the call Windows spells differently.
3018#[cfg(windows)]
3019fn write_at(file: &File, mut offset: u64, mut bytes: &[u8]) -> Result<()> {
3020    use std::os::windows::fs::FileExt;
3021    while !bytes.is_empty() {
3022        let written = file.seek_write(bytes, offset).map_err(io)?;
3023        if written == 0 {
3024            return Err(invalid("a write to the native file wrote nothing"));
3025        }
3026        offset += written as u64;
3027        bytes = &bytes[written..];
3028    }
3029    Ok(())
3030}
3031
3032/// Somewhere that is neither, where the cursor is all there is.
3033#[cfg(not(any(unix, windows)))]
3034fn write_at(file: &File, offset: u64, bytes: &[u8]) -> Result<()> {
3035    use std::io::Write;
3036    let mut file = file.try_clone().map_err(io)?;
3037    file.seek(SeekFrom::Start(offset)).map_err(io)?;
3038    file.write_all(bytes).map_err(io)
3039}
3040
3041/// Reads one span of a file at an offset, without moving a cursor anybody else can see.
3042///
3043/// Every reader of a table shares one [`File`] behind an [`Arc`], and a grouped aggregate reads its
3044/// pages from several threads at once, so this has to be positional. Seeking and then reading is
3045/// two calls with a gap in the middle, and in that gap another thread's seek lands and the read
3046/// comes back with somebody else's bytes.
3047///
3048/// Both of these can come back short, so both loop. A read of zero bytes before the span is filled
3049/// means the file stops earlier than the directory said it does.
3050#[cfg(unix)]
3051fn read_at(file: &File, mut offset: u64, mut bytes: &mut [u8]) -> Result<()> {
3052    use std::os::unix::fs::FileExt;
3053    while !bytes.is_empty() {
3054        let read = file.read_at(bytes, offset).map_err(io)?;
3055        if read == 0 {
3056            return Err(invalid("column page ends before its declared length"));
3057        }
3058        offset += read as u64;
3059        bytes = &mut bytes[read..];
3060    }
3061    Ok(())
3062}
3063
3064/// The same read, on the call Windows spells differently.
3065///
3066/// `seek_read` is one `ReadFile` carrying the offset with it, so two of them cannot interleave the
3067/// way a seek and a read can. It does leave the shared cursor somewhere afterwards, which is why
3068/// nothing in this file may read that cursor.
3069#[cfg(windows)]
3070fn read_at(file: &File, mut offset: u64, mut bytes: &mut [u8]) -> Result<()> {
3071    use std::os::windows::fs::FileExt;
3072    while !bytes.is_empty() {
3073        let read = file.seek_read(bytes, offset).map_err(io)?;
3074        if read == 0 {
3075            return Err(invalid("column page ends before its declared length"));
3076        }
3077        offset += read as u64;
3078        bytes = &mut bytes[read..];
3079    }
3080    Ok(())
3081}
3082
3083/// Somewhere that is neither, where the cursor is all there is.
3084///
3085/// This one does race, and there is no way to write it so it does not. Nothing we build for runs
3086/// here, so it exists to keep the crate compiling rather than to be correct under threads.
3087#[cfg(not(any(unix, windows)))]
3088fn read_at(file: &File, offset: u64, bytes: &mut [u8]) -> Result<()> {
3089    let mut file = file.try_clone().map_err(io)?;
3090    file.seek(SeekFrom::Start(offset)).map_err(io)?;
3091    file.read_exact(bytes).map_err(io)
3092}
3093
3094fn type_tag(ty: &LogicalType) -> Result<u8> {
3095    match ty {
3096        LogicalType::SmallInt => Ok(1),
3097        LogicalType::Integer => Ok(2),
3098        LogicalType::BigInt => Ok(3),
3099        LogicalType::Varchar => Ok(4),
3100        LogicalType::Date => Ok(5),
3101        LogicalType::Timestamp => Ok(6),
3102        LogicalType::Boolean => Ok(7),
3103        LogicalType::TinyInt => Ok(8),
3104        LogicalType::UTinyInt => Ok(9),
3105        LogicalType::USmallInt => Ok(10),
3106        LogicalType::UInteger => Ok(11),
3107        LogicalType::UBigInt => Ok(12),
3108        _ => Err(Error::not_implemented(format!("native storage for {ty}"))),
3109    }
3110}
3111
3112fn tag_type(tag: u8) -> Result<LogicalType> {
3113    match tag {
3114        1 => Ok(LogicalType::SmallInt),
3115        2 => Ok(LogicalType::Integer),
3116        3 => Ok(LogicalType::BigInt),
3117        4 => Ok(LogicalType::Varchar),
3118        5 => Ok(LogicalType::Date),
3119        6 => Ok(LogicalType::Timestamp),
3120        7 => Ok(LogicalType::Boolean),
3121        8 => Ok(LogicalType::TinyInt),
3122        9 => Ok(LogicalType::UTinyInt),
3123        10 => Ok(LogicalType::USmallInt),
3124        11 => Ok(LogicalType::UInteger),
3125        12 => Ok(LogicalType::UBigInt),
3126        _ => Err(invalid("column type tag is unknown")),
3127    }
3128}
3129
3130fn put_u16(out: &mut Vec<u8>, value: u16) {
3131    out.extend_from_slice(&value.to_le_bytes());
3132}
3133fn put_u32(out: &mut Vec<u8>, value: u32) {
3134    out.extend_from_slice(&value.to_le_bytes());
3135}
3136fn put_u64(out: &mut Vec<u8>, value: u64) {
3137    out.extend_from_slice(&value.to_le_bytes());
3138}
3139fn put_var_u64(out: &mut Vec<u8>, mut value: u64) {
3140    while value >= 0x80 {
3141        out.push((value as u8 & 0x7f) | 0x80);
3142        value >>= 7;
3143    }
3144    out.push(value as u8);
3145}
3146
3147fn frequency_order(left: FrequencyValue, right: FrequencyValue) -> Ordering {
3148    match (left, right) {
3149        (FrequencyValue::Null, FrequencyValue::Null) => Ordering::Equal,
3150        (FrequencyValue::Null, _) => Ordering::Less,
3151        (_, FrequencyValue::Null) => Ordering::Greater,
3152        (FrequencyValue::Integer(left), FrequencyValue::Integer(right)) => left.cmp(&right),
3153        (FrequencyValue::Code(left), FrequencyValue::Code(right)) => left.cmp(&right),
3154        (FrequencyValue::Integer(_), FrequencyValue::Code(_)) => Ordering::Less,
3155        (FrequencyValue::Code(_), FrequencyValue::Integer(_)) => Ordering::Greater,
3156    }
3157}
3158
3159fn code_frequency(dictionary: &GlobalDictionary) -> FrequencySummary {
3160    let mut entries = dictionary
3161        .counts
3162        .iter()
3163        .enumerate()
3164        .filter(|(_, count)| **count != 0)
3165        .map(|(code, &count)| FrequencyEntry { value: FrequencyValue::Code(code as u32), count })
3166        .collect::<Vec<_>>();
3167    if dictionary.nulls != 0 {
3168        entries.push(FrequencyEntry { value: FrequencyValue::Null, count: dictionary.nulls });
3169    }
3170    entries.sort_unstable_by(|left, right| {
3171        right.count.cmp(&left.count).then_with(|| frequency_order(left.value, right.value))
3172    });
3173    let omitted_max = entries.get(FREQUENCY_ENTRIES).map_or(0, |entry| entry.count);
3174    entries.truncate(FREQUENCY_ENTRIES);
3175    FrequencySummary { entries, omitted_max, ordinals: Vec::new() }
3176}
3177
3178fn encode_directory(table: &Table) -> Result<Vec<u8>> {
3179    let mut out = DIRECTORY.to_vec();
3180    let name = table.name.as_bytes();
3181    put_u16(&mut out, u16::try_from(name.len()).map_err(|_| invalid("table name too long"))?);
3182    out.extend_from_slice(name);
3183    put_u16(&mut out, u16::try_from(table.fields.len()).map_err(|_| invalid("too many columns"))?);
3184    for field in &table.fields {
3185        let name = field.name.as_bytes();
3186        put_u16(&mut out, u16::try_from(name.len()).map_err(|_| invalid("column name too long"))?);
3187        out.extend_from_slice(name);
3188        out.push(type_tag(&field.ty)?);
3189        out.push(u8::from(field.not_null));
3190    }
3191    for dictionary in &table.dictionaries {
3192        match dictionary {
3193            None => out.push(0),
3194            Some(page) => {
3195                out.push(1);
3196                put_u64(&mut out, page.offset);
3197                put_u32(&mut out, page.length);
3198                put_u64(&mut out, page.hash);
3199            }
3200        }
3201    }
3202    for distinct in &table.distincts {
3203        match distinct {
3204            None => out.push(0),
3205            Some(count) => {
3206                out.push(1);
3207                put_u64(&mut out, *count);
3208            }
3209        }
3210    }
3211    put_u64(&mut out, u64::try_from(table.rows).map_err(|_| invalid("row count overflow"))?);
3212    put_u32(&mut out, u32::try_from(table.stripes.len()).map_err(|_| invalid("too many stripes"))?);
3213    for stripe in &table.stripes {
3214        put_u32(
3215            &mut out,
3216            u32::try_from(stripe.parts.len()).map_err(|_| invalid("too many parts in a stripe"))?,
3217        );
3218        for &rows in &stripe.parts {
3219            put_u32(&mut out, rows);
3220        }
3221        put_u64(&mut out, stripe.index.offset);
3222        put_u32(&mut out, stripe.index.length);
3223        for page in &stripe.pages {
3224            put_u64(&mut out, page.offset);
3225            put_u32(&mut out, page.length);
3226        }
3227        for (field, membership) in table.fields.iter().zip(&stripe.memberships) {
3228            if field.ty != LogicalType::Varchar {
3229                continue;
3230            }
3231            let page =
3232                membership.ok_or_else(|| invalid("string page has no code membership index"))?;
3233            put_u64(&mut out, page.offset);
3234            put_u32(&mut out, page.length);
3235            put_u64(&mut out, page.hash);
3236        }
3237        for sieve in &stripe.sieves {
3238            match sieve {
3239                None => out.push(0),
3240                Some(page) => {
3241                    out.push(1);
3242                    put_u64(&mut out, page.offset);
3243                    put_u32(&mut out, page.length);
3244                    put_u64(&mut out, page.hash);
3245                }
3246            }
3247        }
3248        for held in &stripe.part_ranges {
3249            match held {
3250                None => out.push(0),
3251                Some(page) => {
3252                    out.push(1);
3253                    put_u64(&mut out, page.offset);
3254                    put_u32(&mut out, page.length);
3255                    put_u64(&mut out, page.hash);
3256                }
3257            }
3258        }
3259        for range in stripe.zone.columns() {
3260            put_bound(&mut out, range.low.as_ref())?;
3261            put_bound(&mut out, range.high.as_ref())?;
3262            put_u32(
3263                &mut out,
3264                u32::try_from(range.nulls).map_err(|_| invalid("null count overflow"))?,
3265            );
3266            out.push(u8::from(range.exact));
3267            match range.sum {
3268                None => out.push(0),
3269                Some(total) => {
3270                    out.push(1);
3271                    out.extend_from_slice(&total.to_le_bytes());
3272                }
3273            }
3274        }
3275    }
3276    out.extend_from_slice(FREQUENCIES);
3277    put_u16(
3278        &mut out,
3279        u16::try_from(table.frequencies.len())
3280            .map_err(|_| invalid("too many frequency columns"))?,
3281    );
3282    for summary in &table.frequencies {
3283        let Some(summary) = summary else {
3284            out.push(0);
3285            continue;
3286        };
3287        out.push(1);
3288        put_u64(&mut out, summary.omitted_max);
3289        put_u32(
3290            &mut out,
3291            u32::try_from(summary.entries.len())
3292                .map_err(|_| invalid("too many frequency entries"))?,
3293        );
3294        for entry in &summary.entries {
3295            match entry.value {
3296                FrequencyValue::Null => out.push(0),
3297                FrequencyValue::Integer(value) => {
3298                    out.push(1);
3299                    out.extend_from_slice(&value.to_le_bytes());
3300                }
3301                FrequencyValue::Code(value) => {
3302                    out.push(2);
3303                    put_u32(&mut out, value);
3304                }
3305            }
3306            put_u64(&mut out, entry.count);
3307        }
3308        put_u32(
3309            &mut out,
3310            u32::try_from(summary.ordinals.len())
3311                .map_err(|_| invalid("too many frequency ordinals"))?,
3312        );
3313        let mut previous = 0_u64;
3314        for (at, &ordinal) in summary.ordinals.iter().enumerate() {
3315            let delta = if at == 0 {
3316                ordinal
3317            } else {
3318                ordinal
3319                    .checked_sub(previous)
3320                    .ok_or_else(|| invalid("frequency ordinals are not ordered"))?
3321            };
3322            if at != 0 && delta == 0 {
3323                return Err(invalid("frequency ordinals are not unique"));
3324            }
3325            put_var_u64(&mut out, delta);
3326            previous = ordinal;
3327        }
3328    }
3329    Ok(out)
3330}
3331
3332struct Cursor<'a> {
3333    bytes: &'a [u8],
3334    at: usize,
3335}
3336impl<'a> Cursor<'a> {
3337    fn take(&mut self, len: usize) -> Result<&'a [u8]> {
3338        let end = self.at.checked_add(len).ok_or_else(|| invalid("directory offset overflow"))?;
3339        let bytes =
3340            self.bytes.get(self.at..end).ok_or_else(|| invalid("directory is truncated"))?;
3341        self.at = end;
3342        Ok(bytes)
3343    }
3344    fn u8(&mut self) -> Result<u8> {
3345        Ok(self.take(1)?[0])
3346    }
3347    fn u16(&mut self) -> Result<u16> {
3348        Ok(u16::from_le_bytes(self.take(2)?.try_into().expect("two bytes")))
3349    }
3350    fn u32(&mut self) -> Result<u32> {
3351        Ok(u32::from_le_bytes(self.take(4)?.try_into().expect("four bytes")))
3352    }
3353    fn u64(&mut self) -> Result<u64> {
3354        Ok(u64::from_le_bytes(self.take(8)?.try_into().expect("eight bytes")))
3355    }
3356    fn var_u64(&mut self) -> Result<u64> {
3357        let mut value = 0_u64;
3358        for shift in (0..=63).step_by(7) {
3359            let byte = self.u8()?;
3360            let part = u64::from(byte & 0x7f);
3361            if shift == 63 && part > 1 {
3362                return Err(invalid("frequency ordinal varint overflows"));
3363            }
3364            value |= part << shift;
3365            if byte & 0x80 == 0 {
3366                return Ok(value);
3367            }
3368        }
3369        Err(invalid("frequency ordinal varint is too long"))
3370    }
3371    fn bound(&mut self) -> Result<Option<Bound>> {
3372        Ok(match self.u8()? {
3373            0 => None,
3374            1 => Some(Bound::Int(i128::from_le_bytes(
3375                self.take(16)?.try_into().expect("sixteen bytes"),
3376            ))),
3377            2 => Some(Bound::Real(f64::from_le_bytes(
3378                self.take(8)?.try_into().expect("eight bytes"),
3379            ))),
3380            3 => {
3381                let length = self.u32()? as usize;
3382                Some(Bound::Bytes(self.take(length)?.to_vec()))
3383            }
3384            4 => {
3385                let unscaled =
3386                    i128::from_le_bytes(self.take(16)?.try_into().expect("sixteen bytes"));
3387                Some(Bound::Scaled { unscaled, scale: self.u8()? })
3388            }
3389            _ => return Err(invalid("bound tag differs")),
3390        })
3391    }
3392    fn text(&mut self) -> Result<String> {
3393        let len = self.u16()? as usize;
3394        String::from_utf8(self.take(len)?.to_vec()).map_err(|_| invalid("name is not UTF-8"))
3395    }
3396}
3397
3398fn decode_directory(bytes: &[u8], size: u64) -> Result<Table> {
3399    let mut cur = Cursor { bytes, at: 0 };
3400    if cur.take(8)? != DIRECTORY {
3401        return Err(invalid("directory magic differs"));
3402    }
3403    let name = cur.text()?;
3404    let width = cur.u16()? as usize;
3405    let mut fields = Vec::with_capacity(width);
3406    for _ in 0..width {
3407        let name = cur.text()?;
3408        let ty = tag_type(cur.u8()?)?;
3409        let not_null = match cur.u8()? {
3410            0 => false,
3411            1 => true,
3412            _ => return Err(invalid("nullability flag differs")),
3413        };
3414        fields.push(Field { name, ty, not_null });
3415    }
3416    let mut dictionaries = Vec::with_capacity(width);
3417    for _ in 0..width {
3418        dictionaries.push(match cur.u8()? {
3419            0 => None,
3420            1 => {
3421                let page = Page { offset: cur.u64()?, length: cur.u32()?, hash: cur.u64()? };
3422                let end = page
3423                    .offset
3424                    .checked_add(u64::from(page.length))
3425                    .ok_or_else(|| invalid("dictionary page offset overflow"))?;
3426                // A global dictionary covers a whole column, not one bounded stripe. Its lazy
3427                // payload is intentionally allowed to grow past `MAX_PAGE`; only ordinary column
3428                // pages are capped there. `Writer::finish` has already bounded this length by the
3429                // on-disk `u32`, and the range check below keeps it inside the file.
3430                if page.offset < HEADER || end > size {
3431                    return Err(invalid("dictionary page range is outside the file"));
3432                }
3433                Some(page)
3434            }
3435            _ => return Err(invalid("dictionary page tag differs")),
3436        });
3437    }
3438    let mut distincts = Vec::with_capacity(width);
3439    for _ in 0..width {
3440        distincts.push(match cur.u8()? {
3441            0 => None,
3442            1 => Some(cur.u64()?),
3443            _ => return Err(invalid("distinct count tag differs")),
3444        });
3445    }
3446    let rows = usize::try_from(cur.u64()?).map_err(|_| invalid("row count does not fit"))?;
3447    let count = cur.u32()? as usize;
3448    let mut stripes = Vec::with_capacity(count);
3449    let mut total = 0_usize;
3450    for _ in 0..count {
3451        let count = cur.u32()? as usize;
3452        if count == 0 || count > STRIPE_PARTS {
3453            return Err(invalid("stripe part count is outside its bound"));
3454        }
3455        let mut parts = Vec::with_capacity(count);
3456        let mut stripe_rows = 0_usize;
3457        for _ in 0..count {
3458            let rows = cur.u32()?;
3459            if rows == 0 {
3460                return Err(invalid("empty part"));
3461            }
3462            parts.push(rows);
3463            stripe_rows = stripe_rows
3464                .checked_add(rows as usize)
3465                .ok_or_else(|| invalid("stripe row count overflow"))?;
3466        }
3467        total =
3468            total.checked_add(stripe_rows).ok_or_else(|| invalid("stripe row count overflow"))?;
3469        let index = Span { offset: cur.u64()?, length: cur.u32()? };
3470        let section = index_section(count)?;
3471        let wanted = section
3472            .checked_mul(width)
3473            .and_then(|bytes| u32::try_from(bytes).ok())
3474            .ok_or_else(|| invalid("index page length overflow"))?;
3475        let end = index
3476            .offset
3477            .checked_add(u64::from(index.length))
3478            .ok_or_else(|| invalid("index page offset overflow"))?;
3479        if index.offset < HEADER || end > size || index.length != wanted {
3480            return Err(invalid("index page range is outside the file"));
3481        }
3482        let mut pages = Vec::with_capacity(width);
3483        for _ in 0..width {
3484            let offset = cur.u64()?;
3485            let length = cur.u32()?;
3486            let end = offset
3487                .checked_add(u64::from(length))
3488                .ok_or_else(|| invalid("page offset overflow"))?;
3489            if offset < HEADER || end > size || length as usize > MAX_PAGE {
3490                return Err(invalid("page range is outside the file"));
3491            }
3492            pages.push(Span { offset, length });
3493        }
3494        let mut memberships = vec![None; width];
3495        for (column, field) in fields.iter().enumerate() {
3496            if field.ty != LogicalType::Varchar {
3497                continue;
3498            }
3499            let page = Page { offset: cur.u64()?, length: cur.u32()?, hash: cur.u64()? };
3500            let end = page
3501                .offset
3502                .checked_add(u64::from(page.length))
3503                .ok_or_else(|| invalid("membership page offset overflow"))?;
3504            if page.offset < HEADER || end > size || page.length as usize > MAX_PAGE {
3505                return Err(invalid("membership page range is outside the file"));
3506            }
3507            memberships[column] = Some(page);
3508        }
3509        let mut sieves = vec![None; width];
3510        for sieve in sieves.iter_mut().take(width) {
3511            match cur.u8()? {
3512                0 => continue,
3513                1 => {}
3514                _ => return Err(invalid("a sieve page has an unknown tag")),
3515            }
3516            let page = Page { offset: cur.u64()?, length: cur.u32()?, hash: cur.u64()? };
3517            let end = page
3518                .offset
3519                .checked_add(u64::from(page.length))
3520                .ok_or_else(|| invalid("sieve page offset overflow"))?;
3521            if page.offset < HEADER || end > size || page.length as usize > MAX_PAGE {
3522                return Err(invalid("sieve page range is outside the file"));
3523            }
3524            *sieve = Some(page);
3525        }
3526        let mut part_ranges = vec![None; width];
3527        for held in part_ranges.iter_mut().take(width) {
3528            match cur.u8()? {
3529                0 => continue,
3530                1 => {}
3531                _ => return Err(invalid("a part range page has an unknown tag")),
3532            }
3533            let page = Page { offset: cur.u64()?, length: cur.u32()?, hash: cur.u64()? };
3534            let end = page
3535                .offset
3536                .checked_add(u64::from(page.length))
3537                .ok_or_else(|| invalid("part range page offset overflow"))?;
3538            if page.offset < HEADER || end > size || page.length as usize > MAX_PAGE {
3539                return Err(invalid("part range page range is outside the file"));
3540            }
3541            *held = Some(page);
3542        }
3543        let mut ranges = Vec::with_capacity(width);
3544        for column in 0..width {
3545            let low = cur.bound()?;
3546            let high = cur.bound()?;
3547            let nulls = cur.u32()? as usize;
3548            if nulls > stripe_rows {
3549                return Err(invalid("null count exceeds stripe rows"));
3550            }
3551            let exact = cur.u8()? != 0;
3552            let sum = match cur.u8()? {
3553                0 => None,
3554                1 => Some(i128::from_le_bytes(
3555                    cur.take(16)?.try_into().map_err(|_| invalid("a stripe sum is truncated"))?,
3556                )),
3557                _ => return Err(invalid("a stripe sum has an unknown tag")),
3558            };
3559            // Files written before the ends of a decimal or a timestamp column carried their power
3560            // of ten hold a bare integer here, and that integer is the one the column holds, which
3561            // is what the power is over. So the type puts it back on the way in and an old file
3562            // prunes as well as a new one. A file that already wrote the power keeps it, because
3563            // this leaves anything that is not an integer alone.
3564            let ty = &fields.get(column).ok_or_else(|| invalid("a stripe range has no column"))?.ty;
3565            let low = low.map(|bound| scaled_as(bound, ty));
3566            let high = high.map(|bound| scaled_as(bound, ty));
3567            ranges.push(Range { low, high, nulls, exact, sum });
3568        }
3569        stripes.push(Stripe {
3570            rows: stripe_rows,
3571            parts,
3572            index,
3573            pages,
3574            memberships,
3575            sieves,
3576            part_ranges,
3577            zone: Zone::from_ranges(ranges),
3578        });
3579    }
3580    if total != rows {
3581        return Err(invalid("table row count differs from stripes"));
3582    }
3583    let frequencies = if cur.at == bytes.len() {
3584        vec![None; width]
3585    } else {
3586        if cur.take(8)? != FREQUENCIES {
3587            return Err(invalid("directory extension magic differs"));
3588        }
3589        if cur.u16()? as usize != width {
3590            return Err(invalid("frequency column count differs"));
3591        }
3592        let mut frequencies = Vec::with_capacity(width);
3593        for field in &fields {
3594            let summary = match cur.u8()? {
3595                0 => None,
3596                1 => {
3597                    let omitted_max = cur.u64()?;
3598                    let count = cur.u32()? as usize;
3599                    if count > FREQUENCY_ENTRIES {
3600                        return Err(invalid("frequency entry count exceeds its bound"));
3601                    }
3602                    let mut entries = Vec::with_capacity(count);
3603                    // row at a time: directory decoding validates each persisted bounded frequency entry.
3604                    for _ in 0..count {
3605                        let value = match cur.u8()? {
3606                            0 => FrequencyValue::Null,
3607                            1 => FrequencyValue::Integer(i128::from_le_bytes(
3608                                cur.take(16)?.try_into().expect("sixteen bytes"),
3609                            )),
3610                            2 => FrequencyValue::Code(cur.u32()?),
3611                            _ => return Err(invalid("frequency value tag differs")),
3612                        };
3613                        let valid = matches!(
3614                            (&field.ty, value),
3615                            (_, FrequencyValue::Null)
3616                                | (LogicalType::Varchar, FrequencyValue::Code(_))
3617                                | (
3618                                    LogicalType::TinyInt
3619                                        | LogicalType::SmallInt
3620                                        | LogicalType::Integer
3621                                        | LogicalType::BigInt
3622                                        | LogicalType::UTinyInt
3623                                        | LogicalType::USmallInt
3624                                        | LogicalType::UInteger
3625                                        | LogicalType::UBigInt
3626                                        | LogicalType::Date
3627                                        | LogicalType::Timestamp,
3628                                    FrequencyValue::Integer(_),
3629                                )
3630                        );
3631                        if !valid {
3632                            return Err(invalid("frequency value does not match its column"));
3633                        }
3634                        let count = cur.u64()?;
3635                        if count == 0 || count > rows as u64 {
3636                            return Err(invalid("frequency count is outside the table"));
3637                        }
3638                        entries.push(FrequencyEntry { value, count });
3639                    }
3640                    if entries.windows(2).any(|pair| pair[0].count < pair[1].count) {
3641                        return Err(invalid("frequency entries are not descending"));
3642                    }
3643                    let ordinals = {
3644                        let ordinal_count = cur.u32()? as usize;
3645                        if ordinal_count > FREQUENCY_ORDINALS || ordinal_count > rows {
3646                            return Err(invalid("frequency ordinal count exceeds its bound"));
3647                        }
3648                        let mut ordinals = Vec::with_capacity(ordinal_count);
3649                        let mut previous = 0_u64;
3650                        for at in 0..ordinal_count {
3651                            let delta = cur.var_u64()?;
3652                            if at != 0 && delta == 0 {
3653                                return Err(invalid("frequency ordinals are not increasing"));
3654                            }
3655                            let ordinal = if at == 0 {
3656                                delta
3657                            } else {
3658                                previous
3659                                    .checked_add(delta)
3660                                    .ok_or_else(|| invalid("frequency ordinal overflows"))?
3661                            };
3662                            if ordinal >= rows as u64 {
3663                                return Err(invalid("frequency ordinal is outside the table"));
3664                            }
3665                            ordinals.push(ordinal);
3666                            previous = ordinal;
3667                        }
3668                        ordinals
3669                    };
3670                    Some(FrequencySummary { entries, omitted_max, ordinals })
3671                }
3672                _ => return Err(invalid("frequency summary tag differs")),
3673            };
3674            frequencies.push(summary);
3675        }
3676        frequencies
3677    };
3678    if cur.at != bytes.len() {
3679        return Err(invalid("directory has trailing bytes"));
3680    }
3681    Ok(Table { name, fields, stripes, rows, dictionaries, distincts, frequencies })
3682}
3683
3684fn put_bound(out: &mut Vec<u8>, bound: Option<&Bound>) -> Result<()> {
3685    match bound {
3686        None => out.push(0),
3687        Some(Bound::Int(value)) => {
3688            out.push(1);
3689            out.extend_from_slice(&value.to_le_bytes());
3690        }
3691        Some(Bound::Real(value)) => {
3692            out.push(2);
3693            out.extend_from_slice(&value.to_le_bytes());
3694        }
3695        Some(Bound::Bytes(value)) => {
3696            out.push(3);
3697            put_u32(out, u32::try_from(value.len()).map_err(|_| invalid("bound length overflow"))?);
3698            out.extend_from_slice(value);
3699        }
3700        Some(Bound::Scaled { unscaled, scale }) => {
3701            out.push(4);
3702            out.extend_from_slice(&unscaled.to_le_bytes());
3703            out.push(*scale);
3704        }
3705    }
3706    Ok(())
3707}
3708
3709/// Which cascades are worth trying on a run of dictionary codes.
3710///
3711/// The exhaustive chooser encodes every candidate at every level of a cascade three deep and keeps
3712/// the smallest, which on a part of 1024 codes is around a hundred full encodes to decide something
3713/// three candidates were always going to win. It is the right default for a crate that does not
3714/// know what it is looking at. Here we do know. Codes are counted from zero in the order the values
3715/// were first seen, so a part of them is one value, or a narrow band, or a few long runs, and those
3716/// are constant, frame of reference and run length. Nothing else has ever come first on this data.
3717///
3718/// A dictionary of dictionary codes is the one candidate that can never pay, because the codes are
3719/// already the dictionary, and it is also the most expensive one to try. Below the top level the
3720/// streams are an RLE's run values and run lengths, which are integers in their own right with no
3721/// runs left in them, so only the two flat candidates go down there.
3722///
3723/// This is size given up for time on purpose, and the ablation is this chooser against
3724/// [`chooser::EXHAUSTIVE`] on the same file.
3725#[derive(Debug)]
3726struct Codes;
3727
3728impl chooser::Chooser for Codes {
3729    fn name(&self) -> &'static str {
3730        "codes"
3731    }
3732
3733    fn narrow_strings(
3734        &self,
3735        _values: &[&[u8]],
3736        offered: &[string::Kind],
3737        _depth: u8,
3738    ) -> Vec<string::Kind> {
3739        // Never reached, because nothing here encodes strings through the cascade. The trait asks
3740        // for it and the honest answer to a question we have no opinion on is the whole list.
3741        offered.to_vec()
3742    }
3743
3744    fn narrow_integers(
3745        &self,
3746        _values: &[i64],
3747        offered: &[integer::Kind],
3748        depth: u8,
3749    ) -> Vec<integer::Kind> {
3750        let keep: &[integer::Kind] = if depth == 0 {
3751            &[integer::Kind::Constant, integer::Kind::Packed, integer::Kind::Rle]
3752        } else {
3753            &[integer::Kind::Constant, integer::Kind::Packed]
3754        };
3755        let narrowed: Vec<integer::Kind> =
3756            offered.iter().copied().filter(|kind| keep.contains(kind)).collect();
3757        // The contract is a non empty subset, and a chunk that offers none of the three is a chunk
3758        // this has no opinion about rather than one that cannot be written.
3759        if narrowed.is_empty() { offered.to_vec() } else { narrowed }
3760    }
3761}
3762
3763/// Which cascades are worth trying on a part of plain integers.
3764///
3765/// Wider than [`Codes`] because the values are not codes and carry whatever shape the column has.
3766/// A timestamp column climbs, so delta is the one that matters and is the reason this exists at
3767/// all: three timestamp columns in ClickBench were coming out at exactly eight bytes a row with
3768/// nothing asked of them. The same three columns are why the stride is here, since a timestamp
3769/// loaded from a source that recorded whole seconds is microseconds with twenty zero bits under
3770/// every value. A column that is one value with a handful of exceptions is sparse. What is still
3771/// left out is the dictionary, for the same reason as in [`Codes`]: it is the most
3772/// expensive candidate to try and this file already puts the columns that want one through a
3773/// dictionary of their own before they ever reach here.
3774#[derive(Debug)]
3775struct Fixed;
3776
3777impl chooser::Chooser for Fixed {
3778    fn name(&self) -> &'static str {
3779        "fixed"
3780    }
3781
3782    fn narrow_strings(
3783        &self,
3784        _values: &[&[u8]],
3785        offered: &[string::Kind],
3786        _depth: u8,
3787    ) -> Vec<string::Kind> {
3788        offered.to_vec()
3789    }
3790
3791    fn narrow_integers(
3792        &self,
3793        _values: &[i64],
3794        offered: &[integer::Kind],
3795        depth: u8,
3796    ) -> Vec<integer::Kind> {
3797        let keep: &[integer::Kind] = if depth == 0 {
3798            &[
3799                integer::Kind::Constant,
3800                integer::Kind::Packed,
3801                integer::Kind::Delta,
3802                integer::Kind::Rle,
3803                integer::Kind::Sparse,
3804                integer::Kind::Strided,
3805            ]
3806        } else {
3807            &[integer::Kind::Constant, integer::Kind::Packed, integer::Kind::Delta]
3808        };
3809        let narrowed: Vec<integer::Kind> =
3810            offered.iter().copied().filter(|kind| keep.contains(kind)).collect();
3811        if narrowed.is_empty() { offered.to_vec() } else { narrowed }
3812    }
3813}
3814
3815/// Every value of an integer part as an `i64`, or `None` for a part this cannot widen without
3816/// losing one.
3817///
3818/// `UBIGINT` is the only integer type left out, because half its range does not fit and a page that
3819/// silently wrapped would be worse than a page that stays plain. Booleans and strings are not
3820/// integers and have their own ways of being small.
3821fn widened(data: &Data) -> Option<Vec<i64>> {
3822    match data {
3823        Data::Int8(values) => Some(values.iter().map(|value| i64::from(*value)).collect()),
3824        Data::UInt8(values) => Some(values.iter().map(|value| i64::from(*value)).collect()),
3825        Data::Int16(values) => Some(values.iter().map(|value| i64::from(*value)).collect()),
3826        Data::UInt16(values) => Some(values.iter().map(|value| i64::from(*value)).collect()),
3827        Data::Int32(values) => Some(values.iter().map(|value| i64::from(*value)).collect()),
3828        Data::UInt32(values) => Some(values.iter().map(|value| i64::from(*value)).collect()),
3829        Data::Int64(values) => Some(values.to_vec()),
3830        _ => None,
3831    }
3832}
3833
3834/// An integer type a cascaded page can be read back into, and how to tell whether a value fits.
3835///
3836/// This exists so that the check and the conversion can be two loops instead of one. `TryFrom` puts
3837/// them together, which is the right shape for one value and the wrong one for a page: a fallible
3838/// conversion a value at a time is a branch a value at a time, the branch decides whether the loop
3839/// keeps going, and a loop like that is one no compiler will widen.
3840trait Narrow: Copy {
3841    /// How wide this type is, and what to add to a value to put its range at the bottom of a `u64`.
3842    ///
3843    /// Half the width for a signed type, which is what moves its smallest value to zero, and nothing
3844    /// for an unsigned one, whose smallest value is already there.
3845    const BIASED: (u32, u64);
3846
3847    /// The value narrowed, which the caller has already shown fits.
3848    fn narrow(value: i64) -> Self;
3849}
3850
3851/// The bits of `value` a `T` cannot hold, and zero when the value fits.
3852///
3853/// The question is asked this way round because the answers or together. A page fits when every
3854/// residue in it is zero, so the loop is an or into an accumulator and the decision is one test
3855/// after it, where asking whether each value is between a floor and a ceiling gives an answer that
3856/// does not combine and turns into a running minimum and maximum.
3857///
3858/// Biasing and shifting is what the answer is made of, rather than anything that reads more like the
3859/// question, because those are the operations a machine has four of. A 64 bit integer minimum is
3860/// AVX-512. So is a 64 bit arithmetic shift right, which is how the sign extension this could be
3861/// written as would have to be done. An add and a logical shift right are AVX2 and are on every
3862/// machine this runs on, so this is the form that gets four values a cycle instead of one.
3863///
3864/// Adding the bias moves the type's range to `0..=2^bits`, wrapping, so everything in range shifts
3865/// away to nothing and everything outside it leaves something behind. A negative value under an
3866/// unsigned type is caught by the same shift, because a negative `i64` read as a `u64` is enormous.
3867#[allow(clippy::cast_sign_loss, reason = "a residue is a bit pattern and not a number")]
3868fn residue<T: Narrow>(value: i64) -> u64 {
3869    let (bits, bias) = T::BIASED;
3870    (value as u64).wrapping_add(bias) >> bits
3871}
3872
3873/// Says a primitive integer narrows with `as`, and where the bottom of its range is.
3874///
3875/// `as` is a truncation and is the right operation here only because [`fit`] has already found every
3876/// residue zero, and it is what makes the second loop a narrowing store with no branch in it.
3877macro_rules! narrows {
3878    ($($ty:ty => $bias:expr),* $(,)?) => {$(
3879        impl Narrow for $ty {
3880            const BIASED: (u32, u64) = (<$ty>::BITS, $bias);
3881
3882            #[allow(
3883                clippy::cast_possible_truncation,
3884                clippy::cast_sign_loss,
3885                reason = "the caller has checked the bits this truncates away"
3886            )]
3887            fn narrow(value: i64) -> Self {
3888                value as Self
3889            }
3890        }
3891    )*};
3892}
3893
3894narrows! {
3895    i8 => 1 << 7,
3896    u8 => 0,
3897    i16 => 1 << 15,
3898    u16 => 0,
3899    i32 => 1 << 31,
3900    u32 => 0,
3901}
3902
3903/// Narrows a page's values, refusing the page if any of them does not fit.
3904///
3905/// The check first and the conversion second, rather than a fallible conversion a value at a time.
3906/// Both loops here are ones a compiler widens: [`residue`] is three instructions a lane and a
3907/// narrowing store is one. The version before this was a `TryFrom` and a `collect` into a `Result`,
3908/// which is a compare, a branch and a short circuit a value at a time, and on ClickBench 39 it was
3909/// seven percent of the query. The version after that kept a running minimum and maximum, which is
3910/// the obvious way to ask and needs a 64 bit integer minimum that AVX2 does not have, so it stayed
3911/// a value at a time and was still ten percent of the same query.
3912///
3913/// An empty page has nothing to refuse, which falls out of the accumulator starting at zero rather
3914/// than needing a case of its own.
3915fn fit<T: Narrow>(values: &[i64]) -> Result<Vec<T>> {
3916    let mut spilled = 0u64;
3917    for value in values {
3918        spilled |= residue::<T>(*value);
3919    }
3920    if spilled != 0 {
3921        return Err(invalid("page value is not of its type"));
3922    }
3923    Ok(values.iter().map(|value| T::narrow(*value)).collect())
3924}
3925
3926/// The same values back in the width the column is declared at.
3927///
3928/// A value that does not fit is a page that disagrees with the directory about what the column is,
3929/// which is a damaged file rather than a caller error, so it is refused rather than truncated.
3930fn narrowed(ty: &LogicalType, values: Vec<i64>) -> Result<Data> {
3931    Ok(match ty {
3932        LogicalType::TinyInt => Data::Int8(fit::<i8>(&values)?.into()),
3933        LogicalType::UTinyInt => Data::UInt8(fit::<u8>(&values)?.into()),
3934        LogicalType::SmallInt => Data::Int16(fit::<i16>(&values)?.into()),
3935        LogicalType::USmallInt => Data::UInt16(fit::<u16>(&values)?.into()),
3936        LogicalType::Integer | LogicalType::Date => Data::Int32(fit::<i32>(&values)?.into()),
3937        LogicalType::UInteger => Data::UInt32(fit::<u32>(&values)?.into()),
3938        LogicalType::BigInt | LogicalType::Timestamp => Data::Int64(values.into()),
3939        _ => return Err(invalid("cascade codec belongs to a page that is not integers")),
3940    })
3941}
3942
3943/// How many bytes a part of this type costs written out plainly, which is what the cascade has to
3944/// beat before it is worth the decode.
3945fn plain_width(ty: &LogicalType) -> Option<usize> {
3946    Some(match ty {
3947        LogicalType::TinyInt | LogicalType::UTinyInt => 1,
3948        LogicalType::SmallInt | LogicalType::USmallInt => 2,
3949        LogicalType::Integer | LogicalType::UInteger | LogicalType::Date => 4,
3950        LogicalType::BigInt | LogicalType::Timestamp => 8,
3951        _ => return None,
3952    })
3953}
3954
3955/// A part's plain integers through the cascade, or `None` when nothing it offers is worth it.
3956///
3957/// What it has to beat is whatever the page would otherwise have cost, which is the bit packed form
3958/// where there is one and the plain width where there is not. Both are cheaper to decode than a
3959/// cascade, so a tie goes to them.
3960fn cascaded(
3961    flat: &Vector,
3962    ty: &LogicalType,
3963    packed: Option<&Packed<'_>>,
3964) -> Result<Option<Vec<u8>>> {
3965    let (Some(width), Some(data)) = (plain_width(ty), flat.data()) else { return Ok(None) };
3966    let Some(values) = widened(data) else { return Ok(None) };
3967    let plain = values.len().saturating_mul(width);
3968    let best = match packed {
3969        // The tag, the base, the word count and the words, which is what the codec 2 branch writes.
3970        Some(packed) => plain.min(21 + size_of_val(packed.words())),
3971        None => plain,
3972    };
3973    let out = integer::encode_with(&values, &Fixed)?;
3974    Ok((out.len() < best).then_some(out))
3975}
3976
3977/// A part's dictionary codes through the integer cascade, or `None` when the cascade did not pay.
3978///
3979/// Until now this stream was a `u32` a row with nothing asked of it, and on ClickBench that was
3980/// 400,185,326 bytes for every one of the 28 varchar columns, the same count for `URL` as for a
3981/// column holding the empty string in nearly every row. Codes are dense integers counted from zero
3982/// and a part holds 1024 of them, which is the shape frame of reference is best at, and a column
3983/// with one value everywhere comes back a constant costing nothing per row rather than four bytes.
3984///
3985/// The result is taken only when it is smaller than the plain form. A cascade is allowed to come
3986/// out larger on a part whose codes are genuinely wide, `URL` has about sixty million distinct
3987/// values, and there is no reason to pay for the decode when it does.
3988fn encoded_codes(codes: &[u32]) -> Result<Option<Vec<u8>>> {
3989    let wide: Vec<i64> = codes.iter().map(|code| i64::from(*code)).collect();
3990    let coded = integer::encode_with(&wide, &Codes)?;
3991    let plain = codes.len().saturating_mul(size_of::<u32>());
3992    Ok((coded.len() < plain).then_some(coded))
3993}
3994
3995fn encode(
3996    vector: &Vector,
3997    global: Option<&mut GlobalDictionary>,
3998) -> Result<(Vec<u8>, Option<Vec<u32>>)> {
3999    let ty = vector.logical_type();
4000    // flatten: the file writer needs a uniform scalar page and does it once per loaded chunk.
4001    let flat = vector.flatten()?;
4002    let mut out = Vec::new();
4003    let mut global_codes = None;
4004    if let Some(global) = global {
4005        let mut codes = Vec::with_capacity(flat.len());
4006        for row in 0..flat.len() {
4007            let text = flat.text_at(row).unwrap_or("");
4008            let code = global.code(text)?;
4009            global.observe(code, flat.is_null_at(row))?;
4010            codes.push(code);
4011        }
4012        global_codes = Some(codes);
4013    }
4014    let membership = global_codes.as_deref().map(unique_codes);
4015    let dictionary = if global_codes.is_none() && ty == &LogicalType::Varchar {
4016        string_dictionary(&flat)?
4017    } else {
4018        None
4019    };
4020    let packed_vector = if dictionary.is_none() && global_codes.is_none() {
4021        Some(flat.bit_packed()?)
4022    } else {
4023        None
4024    };
4025    let packed = packed_vector.as_ref().and_then(Vector::packed_parts);
4026    let coded = match global_codes.as_deref() {
4027        Some(codes) => encoded_codes(codes)?,
4028        None => None,
4029    };
4030    // Only where nothing else has claimed the page, which is the plain integer case. A packed part
4031    // is still on the table because the cascade has to beat it too: the bit pack takes a part only
4032    // when it halves it, so a column that shrinks by a third was coming out whole.
4033    let cascade = if dictionary.is_none() && global_codes.is_none() {
4034        cascaded(&flat, ty, packed.as_ref())?
4035    } else {
4036        None
4037    };
4038    out.push(if coded.is_some() {
4039        4
4040    } else if cascade.is_some() {
4041        5
4042    } else if global_codes.is_some() {
4043        3
4044    } else if dictionary.is_some() {
4045        1
4046    } else if packed.is_some() {
4047        2
4048    } else {
4049        0
4050    });
4051    let nulls = flat.validity();
4052    let flag = match nulls {
4053        Validity::AllValid => 0,
4054        Validity::AllInvalid => 1,
4055        Validity::Mask(_) => 2,
4056    };
4057    out.push(flag);
4058    if flag == 2 {
4059        for group in (0..vector.len()).step_by(8) {
4060            let mut bits = 0_u8;
4061            for bit in 0..8 {
4062                if group + bit < vector.len() && !flat.is_null_at(group + bit) {
4063                    bits |= 1 << bit;
4064                }
4065            }
4066            out.push(bits);
4067        }
4068    }
4069    if let Some(coded) = coded {
4070        out.extend_from_slice(&coded);
4071        return Ok((out, membership));
4072    }
4073    if let Some(cascade) = cascade {
4074        out.extend_from_slice(&cascade);
4075        return Ok((out, membership));
4076    }
4077    if let Some(codes) = global_codes {
4078        for code in codes {
4079            put_u32(&mut out, code);
4080        }
4081        return Ok((out, membership));
4082    }
4083    if let Some(dictionary) = dictionary {
4084        out.extend_from_slice(&dictionary);
4085        return Ok((out, membership));
4086    }
4087    if let Some(packed) = packed {
4088        if packed.offset() != 0 {
4089            return Err(invalid("writer received a sliced packed vector"));
4090        }
4091        out.push(u8::try_from(packed.width()).map_err(|_| invalid("packed width overflow"))?);
4092        out.extend_from_slice(&packed.base().to_le_bytes());
4093        put_u32(
4094            &mut out,
4095            u32::try_from(packed.words().len()).map_err(|_| invalid("too many packed words"))?,
4096        );
4097        for word in packed.words() {
4098            put_u64(&mut out, *word);
4099        }
4100        return Ok((out, membership));
4101    }
4102    let data = flat.data().ok_or_else(|| invalid("scalar column did not flatten"))?;
4103    match (ty, data) {
4104        (LogicalType::TinyInt, Data::Int8(values)) => {
4105            for value in &**values {
4106                out.extend_from_slice(&value.to_le_bytes());
4107            }
4108        }
4109        (LogicalType::UTinyInt, Data::UInt8(values)) => {
4110            for value in &**values {
4111                out.extend_from_slice(&value.to_le_bytes());
4112            }
4113        }
4114        (LogicalType::SmallInt, Data::Int16(values)) => {
4115            for value in &**values {
4116                out.extend_from_slice(&value.to_le_bytes());
4117            }
4118        }
4119        (LogicalType::USmallInt, Data::UInt16(values)) => {
4120            for value in &**values {
4121                out.extend_from_slice(&value.to_le_bytes());
4122            }
4123        }
4124        (LogicalType::UInteger, Data::UInt32(values)) => {
4125            for value in &**values {
4126                out.extend_from_slice(&value.to_le_bytes());
4127            }
4128        }
4129        (LogicalType::UBigInt, Data::UInt64(values)) => {
4130            for value in &**values {
4131                out.extend_from_slice(&value.to_le_bytes());
4132            }
4133        }
4134        (LogicalType::Integer | LogicalType::Date, Data::Int32(values)) => {
4135            for value in &**values {
4136                out.extend_from_slice(&value.to_le_bytes());
4137            }
4138        }
4139        (LogicalType::BigInt | LogicalType::Timestamp, Data::Int64(values)) => {
4140            for value in &**values {
4141                out.extend_from_slice(&value.to_le_bytes());
4142            }
4143        }
4144        (LogicalType::Boolean, Data::Bool(values)) => {
4145            for value in &**values {
4146                out.push(u8::from(*value));
4147            }
4148        }
4149        (LogicalType::Varchar, Data::Varlen(values)) => {
4150            let mut bytes = Vec::new();
4151            put_u32(&mut out, 0);
4152            for row in 0..vector.len() {
4153                let value = values.bytes(row).ok_or_else(|| invalid("string view is invalid"))?;
4154                bytes.extend_from_slice(value);
4155                put_u32(
4156                    &mut out,
4157                    u32::try_from(bytes.len())
4158                        .map_err(|_| invalid("string payload exceeds 4GiB"))?,
4159                );
4160            }
4161            out.extend_from_slice(&bytes);
4162        }
4163        _ => return Err(Error::not_implemented(format!("native page for {ty}"))),
4164    }
4165    Ok((out, membership))
4166}
4167
4168fn put_varint(out: &mut Vec<u8>, mut value: u32) {
4169    while value >= 0x80 {
4170        out.push((value as u8 & 0x7f) | 0x80);
4171        value >>= 7;
4172    }
4173    out.push(value as u8);
4174}
4175
4176/// The distinct codes of one part, which is what a stripe's membership index is merged from.
4177fn unique_codes(codes: &[u32]) -> Vec<u32> {
4178    let mut unique = codes.to_vec();
4179    unique.sort_unstable();
4180    unique.dedup();
4181    unique
4182}
4183
4184/// The union of the sorted distinct codes of every part in a stripe.
4185///
4186/// Pairwise up a tree rather than one long list concatenated and sorted. Both are the same order of
4187/// work on paper and the tree is the one that does not sort what is already in order: sixty four
4188/// sorted lists become one in six passes over the values.
4189fn merged_codes(lists: Vec<Vec<u32>>) -> Vec<u32> {
4190    let mut lists = lists;
4191    while lists.len() > 1 {
4192        let mut next = Vec::with_capacity(lists.len().div_ceil(2));
4193        for pair in lists.chunks(2) {
4194            match pair {
4195                [left, right] => next.push(merged_pair(left, right)),
4196                [only] => next.push(only.clone()),
4197                _ => {}
4198            }
4199        }
4200        lists = next;
4201    }
4202    lists.pop().unwrap_or_default()
4203}
4204
4205fn merged_pair(left: &[u32], right: &[u32]) -> Vec<u32> {
4206    let mut out = Vec::with_capacity(left.len().saturating_add(right.len()));
4207    let mut at = 0;
4208    let mut to = 0;
4209    while at < left.len() && to < right.len() {
4210        match left[at].cmp(&right[to]) {
4211            Ordering::Less => {
4212                out.push(left[at]);
4213                at += 1;
4214            }
4215            Ordering::Greater => {
4216                out.push(right[to]);
4217                to += 1;
4218            }
4219            Ordering::Equal => {
4220                out.push(left[at]);
4221                at += 1;
4222                to += 1;
4223            }
4224        }
4225    }
4226    out.extend_from_slice(&left[at..]);
4227    out.extend_from_slice(&right[to..]);
4228    out
4229}
4230
4231/// The widest bounds and the total null count of a stripe, from the bounds of its parts.
4232///
4233/// A bound that is missing from any part is missing from the stripe, because a missing bound means
4234/// nothing is known and a stripe that holds an unknown cannot claim one.
4235fn merged_range(ranges: impl Iterator<Item = Range>) -> Range {
4236    let mut merged = Range::default();
4237    let mut first = true;
4238    for range in ranges {
4239        merged.nulls = merged.nulls.saturating_add(range.nulls);
4240        // Both of these have to survive every part, so one part that could not say anything makes
4241        // the stripe unable to say it either. A sum is dropped on overflow rather than wrapped,
4242        // which leaves the stripe with exact ends and no total, which is a true thing to say.
4243        merged.sum = match (merged.sum.take(), range.sum) {
4244            (Some(held), Some(next)) if !first => held.checked_add(next),
4245            (_, next) if first => next,
4246            _ => None,
4247        };
4248        merged.exact = if first { range.exact } else { merged.exact && range.exact };
4249        if first {
4250            merged.low = range.low;
4251            merged.high = range.high;
4252            first = false;
4253            continue;
4254        }
4255        merged.low = match (merged.low.take(), range.low) {
4256            (Some(held), Some(next)) => Some(held.smaller(next)),
4257            _ => None,
4258        };
4259        merged.high = match (merged.high.take(), range.high) {
4260            (Some(held), Some(next)) => Some(held.larger(next)),
4261            _ => None,
4262        };
4263    }
4264    merged
4265}
4266
4267/// One stripe's sieves for one column: the part count, a length for each part, then their bytes.
4268///
4269/// One page for the whole stripe rather than one per part, because a part's sieve is a few hundred
4270/// bytes and sixty four of those are sixty four directory entries and sixty four reads for something
4271/// a scan walks straight through. A part with no sieve writes a length of zero and costs four bytes.
4272/// `bound` cut down to [`PART_BOUND_BYTES`], still a bound of the side it was.
4273///
4274/// A prefix of a string sorts at or before the string, so cutting one down leaves a low end that is
4275/// still a low end. A high end has to go the other way, so the cut prefix is stepped up at the last
4276/// byte that can carry it, and a prefix of nothing but `0xFF` has no such byte and gives up the
4277/// bound rather than claiming one that is too small. Anything that is not a string is already a
4278/// fixed width and is left alone.
4279fn shortened(bound: Option<Bound>, high: bool) -> Option<Bound> {
4280    match bound {
4281        Some(Bound::Bytes(mut value)) if value.len() > PART_BOUND_BYTES => {
4282            value.truncate(PART_BOUND_BYTES);
4283            if !high {
4284                return Some(Bound::Bytes(value));
4285            }
4286            while let Some(last) = value.pop() {
4287                if last < u8::MAX {
4288                    value.push(last + 1);
4289                    return Some(Bound::Bytes(value));
4290                }
4291            }
4292            None
4293        }
4294        other => other,
4295    }
4296}
4297
4298/// The ranges of one column's parts of one stripe, as a page.
4299///
4300/// The two ends and the null count, and not `exact` or the total. Those two answer a `MIN` or a
4301/// `SUM` out of the directory, and the directory already answers those per stripe, where the same
4302/// number costs sixty times less to keep. What a part range is for is skipping the part, and
4303/// skipping needs the ends. So a range read back from here says it is not exact, which is true of a
4304/// string end that was cut down anyway.
4305fn encode_part_ranges(ranges: &[Range]) -> Result<Vec<u8>> {
4306    let mut out = Vec::new();
4307    put_u32(
4308        &mut out,
4309        u32::try_from(ranges.len()).map_err(|_| invalid("too many parts in a stripe"))?,
4310    );
4311    for range in ranges {
4312        put_bound(&mut out, shortened(range.low.clone(), false).as_ref())?;
4313        put_bound(&mut out, shortened(range.high.clone(), true).as_ref())?;
4314        put_u32(&mut out, u32::try_from(range.nulls).map_err(|_| invalid("null count overflow"))?);
4315    }
4316    Ok(out)
4317}
4318
4319/// The ranges one encoded page holds, one entry per part of the stripe.
4320fn decode_part_ranges(bytes: &[u8]) -> Result<Vec<Range>> {
4321    let mut cur = Cursor { bytes, at: 0 };
4322    let parts = cur.u32()? as usize;
4323    let mut out = Vec::new();
4324    for _ in 0..parts {
4325        let low = cur.bound()?;
4326        let high = cur.bound()?;
4327        let nulls = cur.u32()? as usize;
4328        out.push(Range { low, high, nulls, exact: false, sum: None });
4329    }
4330    Ok(out)
4331}
4332
4333fn encode_sieves<'a>(sieves: impl Iterator<Item = &'a Option<Sieve>>) -> Result<Vec<u8>> {
4334    let held: Vec<&Option<Sieve>> = sieves.collect();
4335    let mut out = Vec::new();
4336    put_u32(
4337        &mut out,
4338        u32::try_from(held.len()).map_err(|_| invalid("too many parts in a stripe"))?,
4339    );
4340    for sieve in &held {
4341        let length = sieve.as_ref().map_or(0, Sieve::len);
4342        put_u32(&mut out, u32::try_from(length).map_err(|_| invalid("sieve length overflow"))?);
4343    }
4344    // flatten: a part with no sieve wrote a length of zero above and contributes no bytes here.
4345    for sieve in held.into_iter().flatten() {
4346        out.extend_from_slice(&sieve.to_bytes());
4347    }
4348    Ok(out)
4349}
4350
4351/// The sieves one encoded page holds, one entry per part of the stripe.
4352///
4353/// A part whose bytes are not a sieve this version understands comes back as `None`, which is a part
4354/// that gets read. That is how a file written by a later version of the sieve stays readable rather
4355/// than being a corrupt page.
4356fn decode_sieves(bytes: &[u8]) -> Result<Vec<Option<Sieve>>> {
4357    let parts = u32::from_le_bytes(
4358        bytes
4359            .get(..4)
4360            .ok_or_else(|| invalid("sieve page is truncated"))?
4361            .try_into()
4362            .map_err(|_| invalid("sieve page is truncated"))?,
4363    ) as usize;
4364    let mut lengths = Vec::with_capacity(parts);
4365    for part in 0..parts {
4366        let at = 4 + part * 4;
4367        let field = bytes.get(at..at + 4).ok_or_else(|| invalid("sieve page is truncated"))?;
4368        lengths.push(u32::from_le_bytes(
4369            field.try_into().map_err(|_| invalid("sieve page is truncated"))?,
4370        ) as usize);
4371    }
4372    let mut at = 4 + parts * 4;
4373    let mut out = Vec::with_capacity(parts);
4374    for length in lengths {
4375        if length == 0 {
4376            out.push(None);
4377            continue;
4378        }
4379        let end = at.checked_add(length).ok_or_else(|| invalid("sieve page is truncated"))?;
4380        let field = bytes.get(at..end).ok_or_else(|| invalid("sieve page is truncated"))?;
4381        out.push(Sieve::from_bytes(field));
4382        at = end;
4383    }
4384    if at != bytes.len() {
4385        return Err(invalid("sieve page has trailing bytes"));
4386    }
4387    Ok(out)
4388}
4389
4390/// One stripe's membership index: the code count and then the codes as ascending deltas.
4391///
4392/// The codes have to be sorted and distinct already, which is what [`unique_codes`] and
4393/// [`merged_codes`] hand over. Anything else decodes as different codes, so neither of those two is
4394/// a step a caller can skip.
4395fn encode_membership(unique: &[u32]) -> Vec<u8> {
4396    let mut out = Vec::with_capacity(unique.len().saturating_mul(2).saturating_add(5));
4397    put_varint(&mut out, u32::try_from(unique.len()).unwrap_or(u32::MAX));
4398    let mut previous = 0;
4399    for (at, &code) in unique.iter().enumerate() {
4400        put_varint(&mut out, if at == 0 { code } else { code - previous });
4401        previous = code;
4402    }
4403    out
4404}
4405
4406fn take_varint(bytes: &[u8], at: &mut usize) -> Result<u32> {
4407    let mut value = 0_u32;
4408    for shift in (0..35).step_by(7) {
4409        let byte = *bytes.get(*at).ok_or_else(|| invalid("membership varint is truncated"))?;
4410        *at += 1;
4411        let part = u32::from(byte & 0x7f);
4412        if shift == 28 && part > 0x0f {
4413            return Err(invalid("membership varint overflow"));
4414        }
4415        value = value
4416            .checked_add(
4417                part.checked_shl(shift).ok_or_else(|| invalid("membership varint overflow"))?,
4418            )
4419            .ok_or_else(|| invalid("membership varint overflow"))?;
4420        if byte & 0x80 == 0 {
4421            return Ok(value);
4422        }
4423    }
4424    Err(invalid("membership varint is too long"))
4425}
4426
4427fn decode_membership(bytes: &[u8]) -> Result<Vec<u32>> {
4428    let mut at = 0;
4429    let count = take_varint(bytes, &mut at)? as usize;
4430    let mut codes = Vec::with_capacity(count);
4431    let mut previous = 0_u32;
4432    for index in 0..count {
4433        let delta = take_varint(bytes, &mut at)?;
4434        let code = if index == 0 {
4435            delta
4436        } else {
4437            previous.checked_add(delta).ok_or_else(|| invalid("membership code overflow"))?
4438        };
4439        if index > 0 && code <= previous {
4440            return Err(invalid("membership codes are not increasing"));
4441        }
4442        codes.push(code);
4443        previous = code;
4444    }
4445    if at != bytes.len() {
4446        return Err(invalid("membership page has trailing bytes"));
4447    }
4448    Ok(codes)
4449}
4450
4451fn string_dictionary(vector: &Vector) -> Result<Option<Vec<u8>>> {
4452    let mut by_text = HashMap::new();
4453    let mut values = Vec::new();
4454    let mut codes = Vec::with_capacity(vector.len());
4455    let mut plain_bytes = 0_usize;
4456    for row in 0..vector.len() {
4457        let text = vector.text_at(row).unwrap_or("");
4458        plain_bytes = plain_bytes.saturating_add(text.len());
4459        let code = match by_text.get(text) {
4460            Some(&code) => code,
4461            None => {
4462                let code = u32::try_from(values.len())
4463                    .map_err(|_| invalid("too many dictionary values"))?;
4464                by_text.insert(text, code);
4465                values.push(text);
4466                code
4467            }
4468        };
4469        codes.push(code);
4470    }
4471    let dictionary_bytes = values.iter().map(|value| value.len()).sum::<usize>();
4472    let encoded = 8_usize
4473        .saturating_add((values.len() + 1).saturating_mul(4))
4474        .saturating_add(dictionary_bytes)
4475        .saturating_add(codes.len().saturating_mul(4));
4476    let plain = (vector.len() + 1).saturating_mul(4).saturating_add(plain_bytes);
4477    if encoded >= plain {
4478        return Ok(None);
4479    }
4480    let mut out = Vec::with_capacity(encoded);
4481    put_u32(
4482        &mut out,
4483        u32::try_from(values.len()).map_err(|_| invalid("too many dictionary values"))?,
4484    );
4485    put_u32(
4486        &mut out,
4487        u32::try_from(dictionary_bytes).map_err(|_| invalid("dictionary payload exceeds 4GiB"))?,
4488    );
4489    let mut offset = 0_u32;
4490    put_u32(&mut out, offset);
4491    for value in &values {
4492        offset = offset
4493            .checked_add(
4494                u32::try_from(value.len()).map_err(|_| invalid("dictionary value is too long"))?,
4495            )
4496            .ok_or_else(|| invalid("dictionary payload exceeds 4GiB"))?;
4497        put_u32(&mut out, offset);
4498    }
4499    for value in values {
4500        out.extend_from_slice(value.as_bytes());
4501    }
4502    for code in codes {
4503        put_u32(&mut out, code);
4504    }
4505    Ok(Some(out))
4506}
4507
4508struct EncodedDictionary {
4509    index: Vec<u8>,
4510    ranks: Vec<u8>,
4511    /// The payload as the blocks it is written as, kept apart rather than joined because joining
4512    /// them is a second copy of a thing that is already gigabytes on the columns that matter.
4513    payload: Vec<Vec<u8>>,
4514}
4515
4516/// The first eight bytes of a value as an integer that sorts the way the bytes sort.
4517fn head(bytes: &[u8]) -> u64 {
4518    let mut word = [0; 8];
4519    let take = bytes.len().min(8);
4520    word[..take].copy_from_slice(&bytes[..take]);
4521    u64::from_be_bytes(word)
4522}
4523
4524/// The sorted order of every global dictionary, one entry per column and empty where there is no
4525/// dictionary.
4526///
4527/// One column's sort has nothing to do with another's, and a table like `hits` has fifteen string
4528/// columns, so this runs across threads the way the numeric synopses above do. It is the only part
4529/// of committing a file that is more than bookkeeping, and doing it serially would show up as a
4530/// pause at the end of a load that thirty two threads had been busy with until then.
4531fn rankings(dictionaries: &[Option<GlobalDictionary>]) -> Result<Vec<Vec<(u64, u32)>>> {
4532    let present =
4533        dictionaries.iter().enumerate().filter(|(_, held)| held.is_some()).map(|(at, _)| at);
4534    let present = present.collect::<Vec<_>>();
4535    let mut orders = vec![Vec::new(); dictionaries.len()];
4536    let workers = std::thread::available_parallelism()
4537        .map_or(1, usize::from)
4538        .min(MAX_FREQUENCY_WORKERS)
4539        .min(present.len());
4540    if workers <= 1 {
4541        for at in present {
4542            if let Some(dictionary) = &dictionaries[at] {
4543                orders[at] = dictionary.ranked();
4544            }
4545        }
4546        return Ok(orders);
4547    }
4548    let width = present.len().div_ceil(workers);
4549    let pieces = std::thread::scope(|scope| {
4550        present
4551            .chunks(width)
4552            .map(|columns| {
4553                scope.spawn(|| {
4554                    columns
4555                        .iter()
4556                        .filter_map(|&at| dictionaries[at].as_ref().map(|held| (at, held.ranked())))
4557                        .collect::<Vec<_>>()
4558                })
4559            })
4560            .collect::<Vec<_>>()
4561            .into_iter()
4562            .map(|handle| {
4563                handle.join().map_err(|_| Error::internal("a dictionary sort worker panicked"))
4564            })
4565            .collect::<Result<Vec<_>>>()
4566    })?;
4567    for piece in pieces {
4568        for (at, order) in piece {
4569            orders[at] = order;
4570        }
4571    }
4572    Ok(orders)
4573}
4574
4575fn encode_global_dictionary(
4576    dictionary: GlobalDictionary,
4577    order: &[(u64, u32)],
4578) -> Result<EncodedDictionary> {
4579    let values = dictionary.offsets.len() - 1;
4580    if order.len() != values {
4581        return Err(invalid("global dictionary order does not cover its values"));
4582    }
4583    let blocks = values.div_ceil(TEXT_PAYLOAD_VALUES);
4584    let payload = encode_payload(&dictionary)?;
4585    if payload.len() != blocks {
4586        return Err(invalid("global dictionary payload is not the blocks it says it is"));
4587    }
4588    let (ranks, rank_ends) = encode_ranks(order, code_width(values))?;
4589    let rank_blocks = values.div_ceil(TEXT_RANK_BLOCK);
4590    let offset_bits = offset_width(&dictionary.offsets);
4591    let mut index = Vec::with_capacity(
4592        DICTIONARY_HEADER + offset_bytes(values, offset_bits) + (blocks + rank_blocks) * 16,
4593    );
4594    put_u32(
4595        &mut index,
4596        u32::try_from(values).map_err(|_| invalid("global dictionary has too many values"))?,
4597    );
4598    put_u32(&mut index, TEXT_PAYLOAD_VALUES as u32);
4599    put_u32(
4600        &mut index,
4601        u32::try_from(blocks).map_err(|_| invalid("global dictionary has too many blocks"))?,
4602    );
4603    put_u32(&mut index, offset_bits as u32);
4604    encode_offsets(&dictionary.offsets, offset_bits, &mut index)?;
4605    // Where each block ends, so a reader can find one. The stored blocks are shorter than the
4606    // decoded ones and by a different amount each, so this is the one thing the offsets above no
4607    // longer say.
4608    let mut at = 0_u64;
4609    for block in &payload {
4610        at = at
4611            .checked_add(block.len() as u64)
4612            .ok_or_else(|| invalid("global dictionary payload overflow"))?;
4613        put_u64(&mut index, at);
4614    }
4615    for block in &payload {
4616        put_u64(&mut index, checksum(block));
4617    }
4618    // The same two lists for the sorted order. A rank block is packed at whatever width its own
4619    // heads need, so where one ends is no longer arithmetic on the block number.
4620    if rank_ends.len() != rank_blocks {
4621        return Err(invalid("global dictionary order is not the blocks it says it is"));
4622    }
4623    for end in &rank_ends {
4624        put_u64(&mut index, *end);
4625    }
4626    let mut at = 0_usize;
4627    for end in &rank_ends {
4628        let end = usize::try_from(*end).map_err(|_| invalid("global dictionary order overflow"))?;
4629        put_u64(&mut index, checksum(&ranks[at..end]));
4630        at = end;
4631    }
4632    Ok(EncodedDictionary { index, ranks, payload })
4633}
4634
4635/// How many blocks of the payload the shape is settled on.
4636///
4637/// Eight blocks is 8,192 values, which is the sample `chooser::Sampled` draws and is that size for
4638/// the same reason. They are spread across the dictionary rather than taken off the front, because
4639/// a dictionary is in the order values were first seen and the front of it is the first morsel of
4640/// the load.
4641const PAYLOAD_SAMPLE_BLOCKS: usize = 8;
4642
4643/// The shapes the payload encoder picks between.
4644///
4645/// Narrow on purpose. The exhaustive search encodes every candidate at every level and runs at two
4646/// to six megabytes a second on this data, which over the twelve gigabytes of dictionary `hits`
4647/// carries is about an hour of processor time, so it cannot be what a load does. Each of these
4648/// settles the outer level and the one below it, which is where almost all of that hour goes, and
4649/// leaves the levels under them to the exhaustive search where the chunks are small enough for it
4650/// to cost nothing.
4651///
4652/// Measured on the five ClickBench columns that have a dictionary worth the name, at 1,024 values a
4653/// block, against the exhaustive search over the same blocks:
4654///
4655/// | column | exhaustive | FRONT then LZ | LZ then FSST | LZ then PLAIN |
4656/// |---|---|---|---|---|
4657/// | 2 | 2.923 at 4.3 MB/s | 2.587 at 21.2 | 2.593 at 36.1 | 2.538 at 53.6 |
4658/// | 13 | 3.093 at 3.1 | 3.029 at 36.4 | 2.921 at 35.7 | 2.770 at 82.9 |
4659/// | 14 | 2.330 at 2.1 | 2.283 at 24.3 | 2.213 at 23.5 | 2.113 at 67.6 |
4660/// | 39 | 2.459 at 5.3 | 2.147 at 10.6 | 2.145 at 29.3 | 2.088 at 43.1 |
4661/// | 56 | 4.694 at 6.3 | 4.381 at 51.0 | 4.172 at 50.6 | 3.983 at 86.8 |
4662///
4663/// The best of the three per column is 98 percent of the exhaustive ratio for a tenth of the time.
4664/// `FSST` and `PLAIN` on their own are in the list as a floor rather than to win. `FSST` is the
4665/// right answer for text that does not share prefixes with its neighbours, and `PLAIN` is there so
4666/// that a column nothing compresses is found out in the sample and written at a gigabyte a second
4667/// rather than searched for an answer that does not exist.
4668fn payload_shapes() -> Vec<chooser::Settled> {
4669    let integers = vec![integer::Kind::Packed];
4670    [
4671        vec![string::Kind::Front, string::Kind::Lz],
4672        vec![string::Kind::Lz, string::Kind::Fsst],
4673        vec![string::Kind::Lz, string::Kind::Plain],
4674        vec![string::Kind::Fsst],
4675        vec![string::Kind::Plain],
4676    ]
4677    .into_iter()
4678    .map(|strings| chooser::Settled::new(strings, integers.clone()))
4679    .collect()
4680}
4681
4682/// The payload as encoded blocks of [`TEXT_PAYLOAD_VALUES`] values each.
4683///
4684/// Across threads because this is the only part of committing a file that is real work rather than
4685/// bookkeeping. The blocks are the same size and cost about the same, so an index each is enough of
4686/// a queue and there is nothing to weight the way the numeric synopses are weighted.
4687fn encode_payload(dictionary: &GlobalDictionary) -> Result<Vec<Vec<u8>>> {
4688    let values = dictionary.offsets.len() - 1;
4689    let blocks = values.div_ceil(TEXT_PAYLOAD_VALUES);
4690    let run = |block: usize| {
4691        let first = block * TEXT_PAYLOAD_VALUES;
4692        let last = (first + TEXT_PAYLOAD_VALUES).min(values);
4693        (first..last)
4694            .map(|value| {
4695                let from = dictionary.offsets[value] as usize;
4696                let to = dictionary.offsets[value + 1] as usize;
4697                &dictionary.payload[from..to]
4698            })
4699            .collect::<Vec<_>>()
4700    };
4701    // A dictionary small enough to be the sample is small enough to search in full, and searching
4702    // it costs less than deciding not to.
4703    let shape = (blocks > PAYLOAD_SAMPLE_BLOCKS).then(|| settle_shape(&run, blocks)).transpose()?;
4704    let one = |block: usize| match &shape {
4705        Some(shape) => string::encode_with(&run(block), shape),
4706        None => string::encode(&run(block)),
4707    };
4708    let workers = std::thread::available_parallelism()
4709        .map_or(1, usize::from)
4710        .min(MAX_FREQUENCY_WORKERS)
4711        .min(blocks);
4712    if workers <= 1 {
4713        return (0..blocks).map(one).collect();
4714    }
4715    let next = AtomicUsize::new(0);
4716    let pieces = std::thread::scope(|scope| {
4717        (0..workers)
4718            .map(|_| {
4719                scope.spawn(|| {
4720                    let mut mine = Vec::new();
4721                    loop {
4722                        let block = next.fetch_add(1, Atomic::Relaxed);
4723                        if block >= blocks {
4724                            break;
4725                        }
4726                        mine.push((block, one(block)?));
4727                    }
4728                    Ok(mine)
4729                })
4730            })
4731            .collect::<Vec<_>>()
4732            .into_iter()
4733            .map(|handle| {
4734                handle.join().map_err(|_| Error::internal("a dictionary encode worker panicked"))?
4735            })
4736            .collect::<Result<Vec<_>>>()
4737    })?;
4738    let mut payload = vec![Vec::new(); blocks];
4739    for piece in pieces {
4740        for (block, bytes) in piece {
4741            payload[block] = bytes;
4742        }
4743    }
4744    Ok(payload)
4745}
4746
4747/// Which of [`payload_shapes`] comes out smallest over a sample of the blocks.
4748///
4749/// Every shape is encoded over the same sample and the smallest wins, which is the exhaustive
4750/// search moved up a level: over shapes of a column rather than over candidates of a chunk. The
4751/// sample is spread across the dictionary so that the first and last blocks are both in it, because
4752/// a dictionary written in first seen order has its common values at the front and its long tail at
4753/// the back, and those do not compress alike.
4754fn settle_shape<'a>(
4755    run: &dyn Fn(usize) -> Vec<&'a [u8]>,
4756    blocks: usize,
4757) -> Result<chooser::Settled> {
4758    let last = blocks - 1;
4759    let sample = (0..PAYLOAD_SAMPLE_BLOCKS)
4760        .map(|region| run(region * last / (PAYLOAD_SAMPLE_BLOCKS - 1)))
4761        .collect::<Vec<_>>();
4762    let mut best: Option<(chooser::Settled, usize)> = None;
4763    for shape in payload_shapes() {
4764        let mut size = 0;
4765        for block in &sample {
4766            size += string::encode_with(block, &shape)?.len();
4767        }
4768        if best.as_ref().is_none_or(|(_, smallest)| size < *smallest) {
4769            best = Some((shape, size));
4770        }
4771    }
4772    best.map(|(shape, _)| shape)
4773        .ok_or_else(|| invalid("no shape applies to a global dictionary payload"))
4774}
4775
4776/// The sorted order laid out the way a reader reads it, in blocks of [`TEXT_RANK_BLOCK`] entries.
4777///
4778/// Each block holds its heads first and then its codes, rather than pairing them, because a search
4779/// asks for a head at every probe and for a code about once a search. Keeping the heads together
4780/// means a probe touches eight bytes of a block rather than twelve spread over it, and the last few
4781/// probes of a search, which are the ones that land in the same block, touch the same cache line.
4782fn encode_ranks(order: &[(u64, u32)], code_bits: usize) -> Result<(Vec<u8>, Vec<u64>)> {
4783    let mut out = Vec::with_capacity(order.len() * 4);
4784    let mut ends = Vec::with_capacity(order.len().div_ceil(TEXT_RANK_BLOCK));
4785    let mut heads = Vec::with_capacity(TEXT_RANK_BLOCK);
4786    let mut codes = Vec::with_capacity(TEXT_RANK_BLOCK);
4787    for block in order.chunks(TEXT_RANK_BLOCK) {
4788        // The order is sorted by value and a head is a prefix of a value, so the heads of a block
4789        // rise, the smallest is the first and the largest is the last.
4790        let base = block.first().map_or(0, |&(head, _)| head);
4791        let span = block.last().map_or(0, |&(head, _)| head.wrapping_sub(base));
4792        let width = (u64::BITS - span.leading_zeros()) as usize;
4793        heads.clear();
4794        codes.clear();
4795        for &(head, code) in block {
4796            heads.push(head.wrapping_sub(base));
4797            codes.push(u64::from(code));
4798        }
4799        put_u64(&mut out, base);
4800        out.push(width as u8);
4801        bitpack::pack_tail(&heads, width, &mut out)
4802            .map_err(|_| invalid("global dictionary heads do not pack"))?;
4803        bitpack::pack_tail(&codes, code_bits, &mut out)
4804            .map_err(|_| invalid("global dictionary codes do not pack"))?;
4805        ends.push(out.len() as u64);
4806    }
4807    Ok((out, ends))
4808}
4809
4810/// Opens a column's global dictionary, which reads its index and none of its payload.
4811///
4812/// `keep_budget` is how many decoded payload bytes this dictionary may hold on to, and every
4813/// caller bar the test of the ceiling passes [`TEXT_KEEP_BUDGET`]. It is a parameter rather than
4814/// the constant read where it is used because a test of a ceiling that cannot be moved has to build
4815/// a quarter of a gigabyte of dictionary to reach it.
4816fn open_global_dictionary(
4817    file: Arc<File>,
4818    page: Page,
4819    ty: &LogicalType,
4820    keep_budget: usize,
4821) -> Result<Vector> {
4822    if ty != &LogicalType::Varchar {
4823        return Err(invalid("global dictionary belongs to a non-string column"));
4824    }
4825    let mut header = [0; DICTIONARY_HEADER];
4826    read_at(&file, page.offset, &mut header)?;
4827    let count = u32::from_le_bytes(header[0..4].try_into().expect("four bytes")) as usize;
4828    let per_block = u32::from_le_bytes(header[4..8].try_into().expect("four bytes")) as usize;
4829    let blocks = u32::from_le_bytes(header[8..12].try_into().expect("four bytes")) as usize;
4830    let offset_bits = u32::from_le_bytes(header[12..16].try_into().expect("four bytes")) as usize;
4831    if per_block != TEXT_PAYLOAD_VALUES {
4832        return Err(invalid("global dictionary block width differs"));
4833    }
4834    if blocks != count.div_ceil(TEXT_PAYLOAD_VALUES) {
4835        return Err(invalid("global dictionary block count differs from its value count"));
4836    }
4837    if offset_bits > u32::BITS as usize {
4838        return Err(invalid("global dictionary packs offsets past a payload"));
4839    }
4840    let offset_len = offset_bytes(count, offset_bits);
4841    // The sorted order is kept out of the index on purpose. The index is read and checksummed in
4842    // full the moment the column is first touched, and the order is half again the size of the
4843    // offsets, so putting it there would make every query that reads a string column pay for a
4844    // search that most of them never make.
4845    let ranks = count;
4846    let rank_blocks = ranks.div_ceil(TEXT_RANK_BLOCK);
4847    // Two words a payload block, one for where it ends in the file and one for its checksum, and the
4848    // same two a rank block.
4849    let hash_len = blocks
4850        .checked_add(rank_blocks)
4851        .and_then(|words| words.checked_mul(16))
4852        .ok_or_else(|| invalid("global dictionary block count overflow"))?;
4853    let index_len = DICTIONARY_HEADER
4854        .checked_add(offset_len)
4855        .and_then(|len| len.checked_add(hash_len))
4856        .ok_or_else(|| invalid("global dictionary header overflow"))?;
4857    if index_len > page.length as usize {
4858        return Err(invalid("global dictionary offset index exceeds its page"));
4859    }
4860    let mut index = vec![0; index_len];
4861    index[..DICTIONARY_HEADER].copy_from_slice(&header);
4862    read_at(&file, page.offset + DICTIONARY_HEADER as u64, &mut index[DICTIONARY_HEADER..])?;
4863    if checksum(&index) != page.hash {
4864        return Err(invalid("global dictionary index checksum differs"));
4865    }
4866    let offsets = index[DICTIONARY_HEADER..DICTIONARY_HEADER + offset_len].to_vec();
4867    let mut words = index[DICTIONARY_HEADER + offset_len..]
4868        .chunks_exact(8)
4869        .map(|part| u64::from_le_bytes(part.try_into().expect("eight bytes")))
4870        .collect::<Vec<_>>();
4871    let mut hashes = words.split_off(blocks);
4872    let mut rank_ends = hashes.split_off(blocks);
4873    let rank_hashes = rank_ends.split_off(rank_blocks);
4874    let ends = words;
4875    // A rank block packs its heads at whatever width its own values need, so its length is no longer
4876    // arithmetic on the block number and the reader has to be told where each one ends.
4877    if rank_ends.windows(2).any(|pair| pair[0] >= pair[1]) {
4878        return Err(invalid("global dictionary order blocks do not rise"));
4879    }
4880    let rank_len = usize::try_from(rank_ends.last().copied().unwrap_or_default())
4881        .map_err(|_| invalid("global dictionary rank overflow"))?;
4882    let body_len = index_len
4883        .checked_add(rank_len)
4884        .ok_or_else(|| invalid("global dictionary header overflow"))?;
4885    if body_len > page.length as usize {
4886        return Err(invalid("global dictionary order exceeds its page"));
4887    }
4888    // What the offsets bound is the decoded payload, and what the page holds is the stored one, so
4889    // the last block end is the only thing that ties the index to the length of the page.
4890    let stored_len = page.length as usize - body_len;
4891    if ends.last().copied().unwrap_or_default() as usize != stored_len
4892        || ends.windows(2).any(|pair| pair[0] > pair[1])
4893    {
4894        return Err(invalid("global dictionary blocks do not bound the payload"));
4895    }
4896    Vector::external_text(
4897        LogicalType::Varchar,
4898        Arc::new(NativeText {
4899            file,
4900            values: count,
4901            offsets,
4902            offset_bits,
4903            ranks,
4904            rank_at: page.offset + index_len as u64,
4905            rank_ends,
4906            rank_hashes,
4907            rank_blocks: (0..rank_blocks).map(|_| OnceLock::new()).collect(),
4908            code_bits: code_width(count),
4909            code_ranks: OnceLock::new(),
4910            payload: page.offset + body_len as u64,
4911            ends,
4912            hashes,
4913            blocks: (0..blocks).map(|_| OnceLock::new()).collect(),
4914            keep_budget,
4915            payload_kept: AtomicUsize::new(0),
4916        }),
4917    )
4918}
4919
4920fn decode(
4921    ty: &LogicalType,
4922    rows: usize,
4923    bytes: &[u8],
4924    global: Option<Arc<Vector>>,
4925) -> Result<Vector> {
4926    let mut cur = Cursor { bytes, at: 0 };
4927    let codec = cur.u8()?;
4928    let flag = cur.u8()?;
4929    let validity = match flag {
4930        0 => Validity::AllValid,
4931        1 => Validity::AllInvalid,
4932        2 => {
4933            let mask = cur.take(rows.div_ceil(8))?;
4934            Validity::from_iter(rows, |row| mask[row / 8] >> (row % 8) & 1 == 1)
4935        }
4936        _ => return Err(invalid("page validity tag differs")),
4937    };
4938    if codec == 1 {
4939        if ty != &LogicalType::Varchar {
4940            return Err(invalid("dictionary codec belongs to a non-string page"));
4941        }
4942        let count = cur.u32()? as usize;
4943        let payload_len = cur.u32()? as usize;
4944        let offset_bytes = cur.take(
4945            (count + 1)
4946                .checked_mul(4)
4947                .ok_or_else(|| invalid("dictionary offset count overflow"))?,
4948        )?;
4949        let offsets = offset_bytes
4950            .chunks_exact(4)
4951            .map(|part| u32::from_le_bytes(part.try_into().expect("four bytes")))
4952            .collect::<Vec<_>>();
4953        let payload = cur.take(payload_len)?.to_vec();
4954        if offsets.first() != Some(&0)
4955            || offsets.last().copied().map(|last| last as usize) != Some(payload.len())
4956            || offsets.windows(2).any(|pair| pair[0] > pair[1])
4957        {
4958            return Err(invalid("dictionary offsets do not bound the payload"));
4959        }
4960        let mut strings = StringColumn::over(Buffer::from_vec(payload));
4961        for pair in offsets.windows(2) {
4962            strings.push_in_place(pair[0] as usize, (pair[1] - pair[0]) as usize)?;
4963        }
4964        let mut codes = Vec::with_capacity(rows);
4965        for _ in 0..rows {
4966            codes.push(cur.u32()?);
4967        }
4968        if codes.iter().any(|code| *code as usize >= count) {
4969            return Err(invalid("dictionary code is out of range"));
4970        }
4971        if cur.at != bytes.len() {
4972            return Err(invalid("dictionary page has trailing bytes"));
4973        }
4974        let dictionary = Vector::flat(LogicalType::Varchar, Data::Varlen(strings))?;
4975        return Ok(Vector::dictionary(codes, dictionary)?.with_validity(validity));
4976    }
4977    if codec == 3 || codec == 4 {
4978        let dictionary = global.ok_or_else(|| invalid("global code page has no dictionary"))?;
4979        let codes = if codec == 4 {
4980            // The cascade holds the whole tail of the page and says how long it is itself, so the
4981            // check that nothing is left over is the one the decoder already makes.
4982            let wide = integer::decode(&bytes[cur.at..])?;
4983            if wide.len() != rows {
4984                return Err(invalid("encoded code page holds the wrong number of rows"));
4985            }
4986            // Converted in one pass and checked in the same one, rather than a fallible conversion
4987            // per code. A `Result` an element is a short circuit the loop cannot be vectorized past,
4988            // and it was costing about twelve instructions a row to narrow a number that already
4989            // fits. Every code a file holds is inside a `u32` or the file is corrupt, so the check
4990            // belongs once at the end: or the codes together and the answer has a bit set above the
4991            // low thirty two, or the sign bit, exactly when one of them did.
4992            let mut codes = Vec::with_capacity(wide.len());
4993            let mut seen = 0_i64;
4994            for &code in &wide {
4995                seen |= code;
4996                codes.push(code as u32);
4997            }
4998            if seen < 0 || seen > i64::from(u32::MAX) {
4999                return Err(invalid("code is not a code"));
5000            }
5001            codes
5002        } else {
5003            let mut codes = Vec::with_capacity(rows);
5004            for _ in 0..rows {
5005                codes.push(cur.u32()?);
5006            }
5007            if cur.at != bytes.len() {
5008                return Err(invalid("global code page has trailing bytes"));
5009            }
5010            codes
5011        };
5012        let highest = codes.iter().copied().max();
5013        return Ok(Vector::stable_dictionary_validated(codes, dictionary, highest)?
5014            .with_validity(validity));
5015    }
5016    if codec == 5 {
5017        // The cascade holds the whole tail of the page and says how long it is itself.
5018        let values = integer::decode(&bytes[cur.at..])?;
5019        if values.len() != rows {
5020            return Err(invalid("cascade page holds the wrong number of rows"));
5021        }
5022        let data = narrowed(ty, values)?;
5023        return Ok(Vector::flat(ty.clone(), data)?.with_validity(validity));
5024    }
5025    if codec == 2 {
5026        let width = u32::from(cur.u8()?);
5027        let base = i128::from_le_bytes(cur.take(16)?.try_into().expect("sixteen bytes"));
5028        let count = cur.u32()? as usize;
5029        let mut words = Vec::with_capacity(count);
5030        for _ in 0..count {
5031            words.push(cur.u64()?);
5032        }
5033        if cur.at != bytes.len() {
5034            return Err(invalid("packed page has trailing bytes"));
5035        }
5036        return Ok(Vector::packed(ty.clone(), words, width, base, rows)?.with_validity(validity));
5037    }
5038    if codec != 0 {
5039        return Err(invalid("page codec is unknown"));
5040    }
5041    let data = match ty {
5042        LogicalType::TinyInt => {
5043            let values = cur.take(rows)?;
5044            Data::Int8(values.iter().map(|item| *item as i8).collect::<Vec<_>>().into())
5045        }
5046        LogicalType::UTinyInt => Data::UInt8(cur.take(rows)?.to_vec().into()),
5047        LogicalType::SmallInt => {
5048            let values =
5049                cur.take(rows.checked_mul(2).ok_or_else(|| invalid("page size overflow"))?)?;
5050            Data::Int16(
5051                values
5052                    .chunks_exact(2)
5053                    .map(|item| i16::from_le_bytes(item.try_into().expect("two bytes")))
5054                    .collect::<Vec<_>>()
5055                    .into(),
5056            )
5057        }
5058        LogicalType::USmallInt => {
5059            let values =
5060                cur.take(rows.checked_mul(2).ok_or_else(|| invalid("page size overflow"))?)?;
5061            Data::UInt16(
5062                values
5063                    .chunks_exact(2)
5064                    .map(|item| u16::from_le_bytes(item.try_into().expect("two bytes")))
5065                    .collect::<Vec<_>>()
5066                    .into(),
5067            )
5068        }
5069        LogicalType::UInteger => {
5070            let values =
5071                cur.take(rows.checked_mul(4).ok_or_else(|| invalid("page size overflow"))?)?;
5072            Data::UInt32(
5073                values
5074                    .chunks_exact(4)
5075                    .map(|item| u32::from_le_bytes(item.try_into().expect("four bytes")))
5076                    .collect::<Vec<_>>()
5077                    .into(),
5078            )
5079        }
5080        LogicalType::UBigInt => {
5081            let values =
5082                cur.take(rows.checked_mul(8).ok_or_else(|| invalid("page size overflow"))?)?;
5083            Data::UInt64(
5084                values
5085                    .chunks_exact(8)
5086                    .map(|item| u64::from_le_bytes(item.try_into().expect("eight bytes")))
5087                    .collect::<Vec<_>>()
5088                    .into(),
5089            )
5090        }
5091        LogicalType::Integer | LogicalType::Date => {
5092            let values =
5093                cur.take(rows.checked_mul(4).ok_or_else(|| invalid("page size overflow"))?)?;
5094            Data::Int32(
5095                values
5096                    .chunks_exact(4)
5097                    .map(|item| i32::from_le_bytes(item.try_into().expect("four bytes")))
5098                    .collect::<Vec<_>>()
5099                    .into(),
5100            )
5101        }
5102        LogicalType::BigInt | LogicalType::Timestamp => {
5103            let values =
5104                cur.take(rows.checked_mul(8).ok_or_else(|| invalid("page size overflow"))?)?;
5105            Data::Int64(
5106                values
5107                    .chunks_exact(8)
5108                    .map(|item| i64::from_le_bytes(item.try_into().expect("eight bytes")))
5109                    .collect::<Vec<_>>()
5110                    .into(),
5111            )
5112        }
5113        LogicalType::Boolean => {
5114            let values = cur.take(rows)?;
5115            if values.iter().any(|value| *value > 1) {
5116                return Err(invalid("boolean page has another value"));
5117            }
5118            Data::Bool(values.iter().map(|value| *value == 1).collect::<Vec<_>>().into())
5119        }
5120        LogicalType::Varchar => {
5121            let offset_bytes = cur
5122                .take((rows + 1).checked_mul(4).ok_or_else(|| invalid("offset count overflow"))?)?;
5123            let offsets = offset_bytes
5124                .chunks_exact(4)
5125                .map(|part| u32::from_le_bytes(part.try_into().expect("four bytes")))
5126                .collect::<Vec<_>>();
5127            let payload = cur.take(bytes.len() - cur.at)?.to_vec();
5128            if offsets.first() != Some(&0)
5129                || offsets.last().copied().map(|last| last as usize) != Some(payload.len())
5130                || offsets.windows(2).any(|pair| pair[0] > pair[1])
5131            {
5132                return Err(invalid("string offsets do not bound the payload"));
5133            }
5134            let mut values = StringColumn::over(Buffer::from_vec(payload));
5135            for pair in offsets.windows(2) {
5136                values.push_in_place(pair[0] as usize, (pair[1] - pair[0]) as usize)?;
5137            }
5138            Data::Varlen(values)
5139        }
5140        _ => return Err(Error::not_implemented(format!("native page for {ty}"))),
5141    };
5142    if cur.at != bytes.len() {
5143        return Err(invalid("page has trailing bytes"));
5144    }
5145    Ok(Vector::flat(ty.clone(), data)?.with_validity(validity))
5146}
5147
5148#[cfg(test)]
5149mod tests {
5150    use std::fs;
5151    use std::io::{Seek, SeekFrom, Write};
5152    use std::path::PathBuf;
5153    use std::time::{SystemTime, UNIX_EPOCH};
5154
5155    use rudb_common::Value;
5156    use rudb_common::bounds::Op;
5157
5158    use super::*;
5159
5160    #[test]
5161    fn checksum_matches_fixed_vectors() {
5162        assert_eq!(checksum(b""), 0xef46_db37_51d8_e999);
5163        assert_eq!(checksum(b"a"), 0xd24e_c4f1_a98c_6e5b);
5164        assert_eq!(checksum(b"abc"), 0x44bc_2cf5_ad77_0999);
5165    }
5166
5167    fn path(label: &str) -> PathBuf {
5168        let stamp = SystemTime::now().duration_since(UNIX_EPOCH).expect("time advances").as_nanos();
5169        std::env::temp_dir().join(format!("rudb-native-{label}-{}-{stamp}.rdb", std::process::id()))
5170    }
5171
5172    /// A read names the offset it wants, so a cursor somebody else moved cannot reach it.
5173    #[test]
5174    fn a_read_at_an_offset_ignores_where_another_thread_left_the_cursor() {
5175        const SPANS: usize = 64;
5176        const SPAN: usize = 512;
5177        let path = path("positional");
5178        let content: Vec<u8> =
5179            (0..SPANS).flat_map(|span| std::iter::repeat_n(span as u8, SPAN)).collect();
5180        fs::write(&path, &content).expect("the file is written");
5181        let file = Arc::new(File::open(&path).expect("the file opens"));
5182        std::thread::scope(|scope| {
5183            for _ in 0..8 {
5184                let file = Arc::clone(&file);
5185                scope.spawn(move || {
5186                    for _ in 0..64 {
5187                        for span in 0..SPANS {
5188                            let mut bytes = [0_u8; SPAN];
5189                            read_at(&file, (span * SPAN) as u64, &mut bytes)
5190                                .expect("the span reads");
5191                            assert!(
5192                                bytes.iter().all(|byte| *byte == span as u8),
5193                                "span {span} came back as {}",
5194                                bytes[0],
5195                            );
5196                        }
5197                    }
5198                });
5199            }
5200        });
5201        let mut past = [0_u8; SPAN];
5202        let end = (SPANS * SPAN) as u64;
5203        let error = read_at(&file, end, &mut past).expect_err("a read past the end is refused");
5204        assert!(error.message().contains("ends before its declared length"), "{error}");
5205        drop(file);
5206        let _ = fs::remove_file(&path);
5207    }
5208
5209    /// The writer records where it put a page and puts it there, whatever the cursor is doing.
5210    ///
5211    /// The cursor is moved between the steps that record an offset, which is what reading the pages
5212    /// back to build the frequencies does on a platform with no `pread`. Without the fix the
5213    /// directory lands on top of a page and the file fails to reopen.
5214    #[test]
5215    fn a_writer_puts_a_page_where_it_said_it_did_wherever_the_cursor_has_got_to() {
5216        let path = path("cursor");
5217        let mut writer = Writer::create(
5218            &path,
5219            "items",
5220            vec![
5221                Field::required("id", LogicalType::Integer),
5222                Field::new("text", LogicalType::Varchar),
5223            ],
5224        )
5225        .expect("new file");
5226        writer.append(&sample()).expect("first part");
5227        writer.file.seek(SeekFrom::Start(0)).expect("the cursor goes back to the header");
5228        writer.append(&sample()).expect("second part");
5229        writer.file.seek(SeekFrom::Start(1)).expect("and somewhere useless again");
5230        writer.finish().expect("commit");
5231        let reader = Reader::open(&path).expect("reopen from disk");
5232        assert_eq!(reader.table().rows(), 6);
5233        let ids = reader.read(0, &[0]).expect("the integer page reads back");
5234        assert_eq!(ids.value_at(0, 0), Value::Integer(4));
5235        assert_eq!(ids.value_at(2, 0), Value::Integer(-2));
5236        let text = reader.read(1, &[1]).expect("the text page reads back");
5237        assert_eq!(text.value_at(1, 0), Value::Null);
5238        assert_eq!(text.value_at(2, 0), Value::Varchar("long text after a slash".into()));
5239        // Nothing the directory points at may run past the end of the file, which is the shape the
5240        // failure took: a page recorded at an offset the directory had already been written over.
5241        let end = reader.table().stripes().iter().flat_map(|stripe| {
5242            stripe
5243                .pages
5244                .iter()
5245                .map(|page| page.offset + u64::from(page.length))
5246                .chain(std::iter::once(stripe.index.offset + u64::from(stripe.index.length)))
5247        });
5248        let last = end.fold(HEADER, u64::max);
5249        let directory = fs::metadata(&path).expect("the file is there").len();
5250        assert!(last <= directory, "a page runs to {last} in a file of {directory} bytes");
5251        fs::remove_file(path).expect("remove scratch file");
5252    }
5253
5254    /// How long a global dictionary index is, read out of the page's own header.
5255    ///
5256    /// The tests below damage a byte of the order or of the payload, so they need to know where each
5257    /// one starts, and working it out here rather than writing a number down means adding something
5258    /// to the index does not quietly turn one of them into a test that damages the index instead.
5259    fn dictionary_index_len(header: &[u8; DICTIONARY_HEADER]) -> u64 {
5260        let count = u64::from(u32::from_le_bytes(header[0..4].try_into().expect("four bytes")));
5261        let blocks = u64::from(u32::from_le_bytes(header[8..12].try_into().expect("four bytes")));
5262        let bits = u32::from_le_bytes(header[12..16].try_into().expect("four bytes")) as usize;
5263        let rank_blocks = count.div_ceil(TEXT_RANK_BLOCK as u64);
5264        DICTIONARY_HEADER as u64
5265            + offset_bytes(count as usize, bits) as u64
5266            + (blocks + rank_blocks) * 16
5267    }
5268
5269    /// How long the sorted order is, which is where its last block ends.
5270    fn last_rank_end(file: &File, offset: u64, header: &[u8; DICTIONARY_HEADER]) -> u64 {
5271        let count = u64::from(u32::from_le_bytes(header[0..4].try_into().expect("four bytes")));
5272        let blocks = u64::from(u32::from_le_bytes(header[8..12].try_into().expect("four bytes")));
5273        let bits = u32::from_le_bytes(header[12..16].try_into().expect("four bytes")) as usize;
5274        let rank_blocks = count.div_ceil(TEXT_RANK_BLOCK as u64);
5275        let at = offset
5276            + DICTIONARY_HEADER as u64
5277            + offset_bytes(count as usize, bits) as u64
5278            + blocks * 16
5279            + (rank_blocks - 1) * 8;
5280        let mut end = [0; 8];
5281        read_at(file, at, &mut end).expect("the last rank block end");
5282        u64::from_le_bytes(end)
5283    }
5284
5285    fn sample() -> Chunk {
5286        Chunk::new(vec![
5287            Vector::from_values(
5288                LogicalType::Integer,
5289                &[Value::Integer(4), Value::Integer(9), Value::Integer(-2)],
5290            )
5291            .expect("integers"),
5292            Vector::from_values(
5293                LogicalType::Varchar,
5294                &[
5295                    Value::Varchar("alpha".into()),
5296                    Value::Null,
5297                    Value::Varchar("long text after a slash".into()),
5298                ],
5299            )
5300            .expect("strings"),
5301        ])
5302        .expect("matching rows")
5303    }
5304
5305    fn sample_ids() -> Chunk {
5306        Chunk::new(vec![
5307            Vector::flat(LogicalType::Integer, Data::Int32(vec![7, 8, 9].into()))
5308                .expect("integers"),
5309        ])
5310        .expect("one column")
5311    }
5312
5313    #[test]
5314    fn committed_file_reopens_and_reads_only_requested_columns() {
5315        let path = path("reopen");
5316        let mut writer = Writer::create(
5317            &path,
5318            "items",
5319            vec![
5320                Field::required("id", LogicalType::Integer),
5321                Field::new("text", LogicalType::Varchar),
5322            ],
5323        )
5324        .expect("new file");
5325        writer.append(&sample()).expect("first part");
5326        writer.append(&sample()).expect("second part");
5327        writer.finish().expect("commit");
5328        let reader = Reader::open(&path).expect("reopen from disk");
5329        assert_eq!(reader.table().rows(), 6);
5330        // Two appends below the stripe bound are two parts of one stripe, which is the whole point
5331        // of the split: the directory describes the stripe and the scan still reads a part.
5332        assert_eq!(reader.table().stripes().len(), 1);
5333        assert_eq!(reader.parts(), 2);
5334        assert_eq!(reader.part_rows(0), 3);
5335        assert_eq!(reader.part_rows(1), 3);
5336        let text = reader.read(1, &[1]).expect("only text page");
5337        assert_eq!(text.width(), 1);
5338        assert_eq!(text.value_at(1, 0), Value::Null);
5339        assert_eq!(text.value_at(2, 0), Value::Varchar("long text after a slash".into()));
5340        let sparse = reader.read_sparse(1, &[1]).expect("one part without its whole page");
5341        assert_eq!(sparse.width(), 1);
5342        assert_eq!(sparse.value_at(1, 0), Value::Null);
5343        assert_eq!(sparse.value_at(2, 0), Value::Varchar("long text after a slash".into()));
5344        assert!(!reader.skips_codes(0, 1, &[0]).expect("alpha is in the stripe"));
5345        assert!(!reader.skips_codes(0, 1, &[2]).expect("long text is in the stripe"));
5346        assert!(reader.skips_codes(0, 1, &[3]).expect("unknown code is absent"));
5347        let count = reader.read(0, &[]).expect("no page is needed for count");
5348        assert_eq!(count.len(), 3);
5349        assert!(reader.skips(0, &[Probe { column: 0, op: Op::Greater, value: Bound::Int(100) }]));
5350        assert!(!reader.skips(0, &[Probe { column: 0, op: Op::Greater, value: Bound::Int(0) }]));
5351        let integers = reader.top_frequencies(0, 1).expect("valid integer synopsis").expect("kept");
5352        assert_eq!(
5353            integers,
5354            vec![(Value::Integer(-2), 2), (Value::Integer(4), 2), (Value::Integer(9), 2),]
5355        );
5356        let strings = reader.top_frequencies(1, 1).expect("valid string synopsis").expect("kept");
5357        assert_eq!(strings.len(), 3);
5358        assert!(strings.contains(&(Value::Null, 2)));
5359        assert!(strings.contains(&(Value::Varchar("alpha".into()), 2)));
5360        assert!(strings.contains(&(Value::Varchar("long text after a slash".into()), 2)));
5361        fs::remove_file(path).expect("remove scratch file");
5362    }
5363
5364    /// Two pipeline instances handing over whole runs, which is what makes the native sink safe to
5365    /// instance.
5366    ///
5367    /// The runs arrive in the order the instances finished reading them rather than in source
5368    /// order, and the second one to finish is the one that read the earlier rows. Each run is still
5369    /// a stripe of its own and the table still reads back in source order, which is the whole of
5370    /// what the writer promises about ordering.
5371    #[test]
5372    fn runs_handed_over_out_of_order_still_read_back_in_source_order() {
5373        let path = path("interleaved-runs");
5374        let mut writer =
5375            Writer::create(&path, "interleaved", vec![Field::new("v", LogicalType::BigInt)])
5376                .expect("new file");
5377        for morsel in [2_u64, 0, 3, 1] {
5378            let parts = (0..4_u64)
5379                .map(|chunk| {
5380                    let first = i64::try_from(morsel * 32 + chunk * 8).expect("small");
5381                    let values =
5382                        (0..8_i64).map(|row| Value::BigInt(first + row)).collect::<Vec<_>>();
5383                    let column =
5384                        Vector::from_values(LogicalType::BigInt, &values).expect("a column");
5385                    ((morsel, chunk), Chunk::new(vec![column]).expect("one column"))
5386                })
5387                .collect::<Vec<_>>();
5388            writer.append_stripe(parts).expect("a stripe");
5389        }
5390        writer.finish().expect("commit");
5391
5392        let reader = Reader::open(&path).expect("valid directory");
5393        assert_eq!(reader.table().stripes().len(), 4, "a run is a stripe of its own");
5394        assert_eq!(reader.table().rows(), 128);
5395        for part in 0..16_usize {
5396            let read = reader.read(part, &[0]).expect("a part back");
5397            for row in 0..8_usize {
5398                let want = i64::try_from(part * 8 + row).expect("small");
5399                assert_eq!(read.value_at(row, 0), Value::BigInt(want), "part {part} row {row}");
5400            }
5401        }
5402        fs::remove_file(path).expect("remove scratch file");
5403    }
5404
5405    /// Runs from different callers may interleave and may not overlap, and the commit is what
5406    /// catches an overlap.
5407    #[test]
5408    fn runs_that_overlap_each_other_are_refused_at_commit() {
5409        let path = path("overlapping-runs");
5410        let mut writer =
5411            Writer::create(&path, "overlapping", vec![Field::new("v", LogicalType::BigInt)])
5412                .expect("new file");
5413        let one = |order: (u64, u64)| {
5414            let column =
5415                Vector::from_values(LogicalType::BigInt, &[Value::BigInt(1)]).expect("a column");
5416            (order, Chunk::new(vec![column]).expect("one column"))
5417        };
5418        // The second run sits inside the first rather than after it, which is a thing no instance
5419        // holding its own contiguous run can produce and a thing the file cannot represent.
5420        writer.append_stripe(vec![one((0, 0)), one((0, 2))]).expect("a stripe");
5421        writer.append_stripe(vec![one((0, 1))]).expect("a stripe");
5422        let error = writer.finish().expect_err("the runs overlap");
5423        assert!(error.message().contains("source order"), "{error}");
5424        fs::remove_file(path).expect("remove scratch file");
5425    }
5426
5427    /// A stripe holds [`STRIPE_PARTS`] parts, so a run longer than that is a caller bug rather than
5428    /// something to split, and the writer says so at the door instead of quietly cutting it in two.
5429    #[test]
5430    fn a_run_longer_than_a_stripe_is_refused() {
5431        let path = path("overlong-run");
5432        let mut writer =
5433            Writer::create(&path, "overlong", vec![Field::new("v", LogicalType::BigInt)])
5434                .expect("new file");
5435        let parts = (0..=STRIPE_PARTS)
5436            .map(|at| {
5437                let column = Vector::from_values(LogicalType::BigInt, &[Value::BigInt(1)])
5438                    .expect("a column");
5439                let chunk = Chunk::new(vec![column]).expect("one column");
5440                ((0, u64::try_from(at).expect("small")), chunk)
5441            })
5442            .collect::<Vec<_>>();
5443        let error = writer.append_stripe(parts).expect_err("one part too many");
5444        assert!(error.message().contains("more parts than it holds"), "{error}");
5445        fs::remove_file(path).expect("remove scratch file");
5446    }
5447
5448    /// Parts past the stripe bound start a new stripe, and every part stays addressable on its own.
5449    ///
5450    /// This is the shape the format exists for, so both ends of the split are checked here. The
5451    /// directory holds three stripes rather than a hundred and thirty one, and a read of any one
5452    /// part still answers with that part's rows rather than with its whole stripe's.
5453    #[test]
5454    fn parts_past_the_stripe_bound_start_a_new_stripe() {
5455        let path = path("stripe-bound");
5456        let mut writer = Writer::create(
5457            &path,
5458            "items",
5459            vec![
5460                Field::required("id", LogicalType::Integer),
5461                Field::new("text", LogicalType::Varchar),
5462            ],
5463        )
5464        .expect("new file");
5465        let parts = STRIPE_PARTS * 2 + 3;
5466        for part in 0..parts {
5467            let id = part as i32;
5468            let chunk = Chunk::new(vec![
5469                Vector::from_values(
5470                    LogicalType::Integer,
5471                    &[Value::Integer(id), Value::Integer(-id)],
5472                )
5473                .expect("integers"),
5474                Vector::from_values(
5475                    LogicalType::Varchar,
5476                    &[Value::Varchar(format!("value {part}")), Value::Null],
5477                )
5478                .expect("strings"),
5479            ])
5480            .expect("matching rows");
5481            writer.append(&chunk).expect("one part");
5482        }
5483        writer.finish().expect("commit");
5484
5485        let reader = Reader::open(&path).expect("reopen from disk");
5486        assert_eq!(reader.parts(), parts);
5487        assert_eq!(reader.table().rows(), parts * 2);
5488        assert_eq!(reader.table().stripes().len(), parts.div_ceil(STRIPE_PARTS));
5489        assert_eq!(reader.table().stripes()[0].parts(), STRIPE_PARTS);
5490        assert_eq!(reader.table().stripes()[0].rows(), STRIPE_PARTS * 2);
5491        assert_eq!(reader.table().stripes()[2].parts(), 3);
5492        // Backwards on purpose. The reader keeps four stripes a column, so a scan that walks the
5493        // table the other way is what catches a cache that only ever holds what it just read.
5494        for part in (0..parts).rev() {
5495            let dense = reader.read(part, &[0, 1]).expect("a whole page read");
5496            let sparse = reader.read_sparse(part, &[0, 1]).expect("one part read");
5497            for chunk in [&dense, &sparse] {
5498                assert_eq!(chunk.len(), 2, "part {part} has its own row count");
5499                assert_eq!(chunk.value_at(0, 0), Value::Integer(part as i32));
5500                assert_eq!(chunk.value_at(1, 0), Value::Integer(-(part as i32)));
5501                assert_eq!(chunk.value_at(0, 1), Value::Varchar(format!("value {part}")));
5502                assert_eq!(chunk.value_at(1, 1), Value::Null);
5503            }
5504        }
5505        // The bounds are merged over the stripe, so they answer for the range the whole stripe
5506        // covers and not for the part that was asked about.
5507        let above = [Probe { column: 0, op: Op::Greater, value: Bound::Int(100) }];
5508        assert!(reader.skips(0, &above), "the first stripe stops at 63");
5509        assert!(!reader.skips(STRIPE_PARTS * 2, &above), "the third stripe reaches 130");
5510        fs::remove_file(path).expect("remove scratch file");
5511    }
5512
5513    /// A scattered value in the column that decides `WHERE UserID = ?`.
5514    fn scattered(n: i64) -> i64 {
5515        n.wrapping_mul(-7_046_029_254_386_353_131)
5516    }
5517
5518    /// A part whose sieve does not hold the constant is skipped, and a range would skip none of them.
5519    ///
5520    /// This is ClickBench query 19 in miniature. The values are spread over the whole of `BIGINT`, so
5521    /// every stripe's bounds cover nearly all of it and rule out nothing, and the part that really
5522    /// holds the value is the only one a scan has to read.
5523    #[test]
5524    fn a_part_is_skipped_when_its_sieve_does_not_hold_the_constant() {
5525        let path = path("sieve-skip");
5526        let mut writer =
5527            Writer::create(&path, "hits", vec![Field::required("id", LogicalType::BigInt)])
5528                .expect("new file");
5529        let parts = STRIPE_PARTS + 3;
5530        // Big enough that the filter is worth its bytes. A part of eight numbers packs to under a
5531        // hundred bytes and the smallest filter there is is sixty nine, so a filter over a part
5532        // that small costs about as much to read as the rows do and is no longer written.
5533        let per_part = 128;
5534        for part in 0..parts {
5535            let held: Vec<Value> = (0..per_part)
5536                .map(|row| Value::BigInt(scattered((part * per_part + row) as i64)))
5537                .collect();
5538            let chunk =
5539                Chunk::new(vec![Vector::from_values(LogicalType::BigInt, &held).expect("numbers")])
5540                    .expect("one column");
5541            writer.append(&chunk).expect("one part");
5542        }
5543        writer.finish().expect("commit");
5544
5545        let reader = Reader::open(&path).expect("reopen from disk");
5546        let probe = |value: i64| Probe {
5547            column: 0,
5548            op: Op::Equal,
5549            value: Bound::Int(i128::from(scattered(value))),
5550        };
5551        for wanted in [0_i64, (per_part + 1) as i64, (parts * per_part - 1) as i64] {
5552            let tests = [probe(wanted)];
5553            let kept: Vec<usize> = (0..parts).filter(|&part| !reader.skips(part, &tests)).collect();
5554            let home = wanted as usize / per_part;
5555            assert!(kept.contains(&home), "the part holding {wanted} is read");
5556            // A filter answers maybe, so a part it keeps need not hold the value. Sixty seven parts
5557            // of a hundred and twenty eight numbers each, at a dozen bits a value, is about one
5558            // stray part across the whole file and that is what this leaves room for.
5559            assert!(kept.len() <= 2, "{wanted} keeps {kept:?}, which is more than one stray part");
5560        }
5561        let absent = [probe((parts * per_part) as i64 + 1)];
5562        let kept = (0..parts).filter(|&part| !reader.skips(part, &absent)).count();
5563        assert!(kept <= 1, "{kept} parts of {parts} kept a value no part holds");
5564        // The same probes against the bounds alone, which is what this replaces. A column of
5565        // scattered numbers has a range per stripe that covers nearly the whole type.
5566        let tests = [probe(0)];
5567        assert!(
5568            reader.table().stripes().iter().all(|stripe| !stripe.zone.skips(&tests)),
5569            "the bounds rule out no stripe at all"
5570        );
5571        fs::remove_file(path).expect("remove scratch file");
5572    }
5573
5574    /// A part whose own bounds rule out an ordered comparison is skipped where the stripe's keep it.
5575    ///
5576    /// This is the shape of ClickBench 24. Each part covers a narrow stretch of the column and the
5577    /// stripe covers all sixty four of them at once, so a comparison that lands inside the stripe
5578    /// rules out none of it and rules out all but a few parts.
5579    #[test]
5580    fn a_part_is_skipped_when_its_own_bounds_rule_out_a_comparison_the_stripe_keeps() {
5581        let path = path("part-range-skip");
5582        let mut writer =
5583            Writer::create(&path, "hits", vec![Field::required("at", LogicalType::BigInt)])
5584                .expect("new file");
5585        let parts = STRIPE_PARTS + 3;
5586        let per_part = 128;
5587        for part in 0..parts {
5588            // Scattered inside the part's own band rather than a run, because a run of
5589            // consecutive numbers encodes to a stride of a few bytes and then the page of ranges
5590            // costs more than reading the column it indexes, which is the case the writer declines.
5591            let held: Vec<Value> = (0..per_part)
5592                .map(|row| {
5593                    Value::BigInt((part * 1_000) as i64 + (scattered(row as i64).rem_euclid(900)))
5594                })
5595                .collect();
5596            let chunk =
5597                Chunk::new(vec![Vector::from_values(LogicalType::BigInt, &held).expect("numbers")])
5598                    .expect("one column");
5599            writer.append(&chunk).expect("one part");
5600        }
5601        writer.finish().expect("commit");
5602
5603        let reader = Reader::open(&path).expect("reopen from disk");
5604        let under = [Probe { column: 0, op: Op::Less, value: Bound::Int(3_000) }];
5605        let kept: Vec<usize> = (0..parts).filter(|&part| !reader.skips(part, &under)).collect();
5606        assert_eq!(kept, vec![0, 1, 2], "only the three parts that start under three thousand");
5607        // The same question asked of the stripe alone, which is what this replaces.
5608        assert!(!reader.stripe_skips(0, &under), "the stripe reaches from zero and keeps itself");
5609        fs::remove_file(path).expect("remove scratch file");
5610    }
5611
5612    /// The page is worth its bytes on a column with parts to tell apart and is not written on one
5613    /// that has a single part, where the stripe bounds already are the part's.
5614    #[test]
5615    fn a_stripe_of_one_part_writes_no_range_page_and_a_stripe_of_many_does() {
5616        for (parts, wanted) in [(1_usize, false), (STRIPE_PARTS, true)] {
5617            let path = path("part-range-page");
5618            let mut writer =
5619                Writer::create(&path, "hits", vec![Field::required("at", LogicalType::BigInt)])
5620                    .expect("new file");
5621            for part in 0..parts {
5622                let held: Vec<Value> = (0..128)
5623                    .map(|row| {
5624                        Value::BigInt((part * 1_000) as i64 + scattered(row as i64).rem_euclid(900))
5625                    })
5626                    .collect();
5627                let chunk = Chunk::new(vec![
5628                    Vector::from_values(LogicalType::BigInt, &held).expect("numbers"),
5629                ])
5630                .expect("one column");
5631                writer.append(&chunk).expect("one part");
5632            }
5633            writer.finish().expect("commit");
5634            let reader = Reader::open(&path).expect("reopen from disk");
5635            let bytes = reader.layout().columns[0].part_ranges;
5636            assert_eq!(bytes > 0, wanted, "{parts} parts wrote {bytes} bytes of ranges");
5637            fs::remove_file(path).expect("remove scratch file");
5638        }
5639    }
5640
5641    /// A cut down string end is still an end on the side it was, which is the only thing that keeps
5642    /// a shortened bound from turning a skip into a wrong answer.
5643    #[test]
5644    fn a_string_end_that_is_cut_down_still_covers_the_value_it_came_from() {
5645        let long = vec![b'a'; PART_BOUND_BYTES * 2];
5646        let low = shortened(Some(Bound::Bytes(long.clone())), false).expect("a low end");
5647        let high = shortened(Some(Bound::Bytes(long.clone())), true).expect("a high end");
5648        let Bound::Bytes(low) = low else { panic!("a string stays a string") };
5649        let Bound::Bytes(high) = high else { panic!("a string stays a string") };
5650        assert!(low.len() <= PART_BOUND_BYTES && high.len() <= PART_BOUND_BYTES);
5651        assert!(low.as_slice() <= long.as_slice(), "the low end is at or under the value");
5652        assert!(high.as_slice() >= long.as_slice(), "the high end is at or over the value");
5653    }
5654
5655    /// A string of nothing but the largest byte has no prefix that can be stepped up, so the high
5656    /// end is given up rather than claimed too small. No end keeps the part, which is always safe.
5657    #[test]
5658    fn a_string_end_with_no_room_to_step_up_gives_up_the_bound() {
5659        let long = vec![u8::MAX; PART_BOUND_BYTES * 2];
5660        assert_eq!(shortened(Some(Bound::Bytes(long.clone())), true), None);
5661        let low = shortened(Some(Bound::Bytes(long)), false).expect("a low end is still a prefix");
5662        assert_eq!(low, Bound::Bytes(vec![u8::MAX; PART_BOUND_BYTES]));
5663    }
5664
5665    /// A sieve bigger than the part it indexes is not written, and one smaller than it still is.
5666    ///
5667    /// Both columns hold values spread over the whole of `BIGINT`, so neither gets a bitmap and both
5668    /// reach the filter. They differ in what the part costs to read. `spread` is a thousand distinct
5669    /// numbers and packs to eight kilobytes, so a filter of about thirteen hundred bytes is a good
5670    /// trade. `repeated` is the same thousand rows over four numbers in runs and encodes to
5671    /// almost nothing, but the filter is sized for the rows rather than the values it turns out to
5672    /// hold, so it comes out larger than the data. Reading it to decide whether to read the part spends more than
5673    /// the part, every time, and that is the case this drops.
5674    #[test]
5675    fn a_sieve_larger_than_the_part_it_indexes_is_not_written() {
5676        let path = path("sieve-pays");
5677        let fields = vec![
5678            Field::required("spread", LogicalType::BigInt),
5679            Field::required("repeated", LogicalType::BigInt),
5680        ];
5681        let mut writer = Writer::create(&path, "hits", fields).expect("new file");
5682        let parts = 3;
5683        let per_part = 1024;
5684        for part in 0..parts {
5685            let base = (part * per_part) as i64;
5686            let spread: Vec<Value> =
5687                (0..per_part).map(|row| Value::BigInt(scattered(base + row as i64))).collect();
5688            let repeated: Vec<Value> =
5689                (0..per_part).map(|row| Value::BigInt(scattered((row / 256) as i64))).collect();
5690            let chunk = Chunk::new(vec![
5691                Vector::from_values(LogicalType::BigInt, &spread).expect("numbers"),
5692                Vector::from_values(LogicalType::BigInt, &repeated).expect("numbers"),
5693            ])
5694            .expect("two columns");
5695            writer.append(&chunk).expect("one part");
5696        }
5697        writer.finish().expect("commit");
5698
5699        let reader = Reader::open(&path).expect("reopen from disk");
5700        let layout = reader.layout();
5701        let spread = &layout.columns[0];
5702        let repeated = &layout.columns[1];
5703        assert!(spread.sieves > 0, "a column whose parts are worth a filter keeps one");
5704        assert_eq!(
5705            repeated.sieves, 0,
5706            "a column whose filter costs more than its parts keeps none"
5707        );
5708        // Per part this is the rule itself, so it holds over the column as well: a part without a
5709        // sieve adds to one side of this and to nothing on the other.
5710        for column in &layout.columns {
5711            assert!(
5712                column.sieves < column.pages,
5713                "{} spends {} on sieves over {} of data",
5714                column.name,
5715                column.sieves,
5716                column.pages
5717            );
5718        }
5719        // The filter that was kept still does what it is for.
5720        let absent = [Probe {
5721            column: 0,
5722            op: Op::Equal,
5723            value: Bound::Int(i128::from(scattered((parts * per_part) as i64 + 1))),
5724        }];
5725        assert!((0..parts).all(|part| reader.skips(part, &absent)), "no part holds it");
5726        fs::remove_file(path).expect("remove scratch file");
5727    }
5728
5729    /// A damaged sieve page is a part that gets read, not a query that fails.
5730    ///
5731    /// A sieve is an index over rows that are still there and still correct, so losing one costs
5732    /// time and costs no answers. That is the opposite of the membership index beside it, which is
5733    /// the only thing standing between a string page and a wrong answer.
5734    #[test]
5735    fn a_damaged_sieve_page_is_read_through_rather_than_refused() {
5736        let path = path("sieve-damaged");
5737        let mut writer =
5738            Writer::create(&path, "hits", vec![Field::required("id", LogicalType::BigInt)])
5739                .expect("new file");
5740        let rows = 128;
5741        let held: Vec<Value> = (0..rows).map(|row| Value::BigInt(scattered(row))).collect();
5742        let chunk =
5743            Chunk::new(vec![Vector::from_values(LogicalType::BigInt, &held).expect("numbers")])
5744                .expect("one column");
5745        writer.append(&chunk).expect("one part");
5746        writer.finish().expect("commit");
5747
5748        let page =
5749            Reader::open(&path).expect("reopen").table.stripes[0].sieves[0].expect("a sieve page");
5750        let mut file = OpenOptions::new().write(true).open(&path).expect("open the sieve page");
5751        file.seek(SeekFrom::Start(page.offset + u64::from(page.length) - 1)).expect("seek");
5752        file.write_all(&[0xff]).expect("damage one byte");
5753        drop(file);
5754
5755        let reader = Reader::open(&path).expect("reopen the damaged file");
5756        let absent =
5757            [Probe { column: 0, op: Op::Equal, value: Bound::Int(i128::from(scattered(99))) }];
5758        assert!(!reader.skips(0, &absent), "a sieve that cannot be read skips nothing");
5759        assert_eq!(
5760            reader.read(0, &[0]).expect("the rows are untouched").len(),
5761            usize::try_from(rows).expect("a small count")
5762        );
5763        fs::remove_file(path).expect("remove scratch file");
5764    }
5765
5766    /// Eight workers over one stripe read it once between them.
5767    ///
5768    /// This is the shape a scan actually has. Parts are handed out in order, so every worker on a
5769    /// column crosses into a stripe within a few parts of the others, and before [`Reader::held`]
5770    /// started sharing the read every one of them read the whole page. On the full ClickBench file
5771    /// that was a `MIN(EventDate), MAX(EventDate)` moving 3.2 GB off the disk to look at 400 MB of
5772    /// column, which is most of what a first touch costs.
5773    ///
5774    /// The workers that lose the race still answer, out of the part reads they do instead, which is
5775    /// what the values below are checking.
5776    #[test]
5777    fn workers_that_want_the_same_stripe_read_it_once() {
5778        let path = path("single-flight");
5779        let mut writer =
5780            Writer::create(&path, "items", vec![Field::required("id", LogicalType::Integer)])
5781                .expect("new file");
5782        for part in 0..STRIPE_PARTS {
5783            let id = part as i32;
5784            let chunk = Chunk::new(vec![
5785                Vector::from_values(
5786                    LogicalType::Integer,
5787                    &[Value::Integer(id), Value::Integer(-id)],
5788                )
5789                .expect("integers"),
5790            ])
5791            .expect("matching rows");
5792            writer.append(&chunk).expect("one part");
5793        }
5794        writer.finish().expect("commit");
5795
5796        let reader = Reader::open(&path).expect("reopen from disk");
5797        assert_eq!(reader.table().stripes().len(), 1, "one stripe is the point of the test");
5798        let barrier = std::sync::Barrier::new(8);
5799        std::thread::scope(|scope| {
5800            for worker in 0..8 {
5801                let reader = &reader;
5802                let barrier = &barrier;
5803                scope.spawn(move || {
5804                    barrier.wait();
5805                    for part in (worker..STRIPE_PARTS).step_by(8) {
5806                        let chunk = reader.read(part, &[0]).expect("a whole page read");
5807                        assert_eq!(chunk.value_at(0, 0), Value::Integer(part as i32));
5808                        assert_eq!(chunk.value_at(1, 0), Value::Integer(-(part as i32)));
5809                    }
5810                });
5811            }
5812        });
5813        assert_eq!(reader.pages.load(Atomic::Relaxed), 1, "one stripe, one page read, whoever won");
5814        fs::remove_file(path).expect("remove scratch file");
5815    }
5816
5817    /// Opening a file reads the header and the directory, and nothing that depends on the rows.
5818    ///
5819    /// `spec/stats/04-in-memory.md` section 4.2. There are no statistics in the file yet, so this
5820    /// holds today by not having anything to load, and that is exactly why it is worth pinning now.
5821    /// The change that breaks it is the reasonable looking one: summaries are a few hundred bytes,
5822    /// the next query will want them, so read them on the way past. A process that opened the
5823    /// database to run one trivial query pays for all of it and gets nothing.
5824    ///
5825    /// Two files of the same shape and a thousand times the rows in one of them, opened, and the
5826    /// two openings cost the same. The stripe count is held equal so that the directory is the same
5827    /// size in both, which leaves the rows as the only thing that changed. Anything read out of the
5828    /// data would show up here.
5829    #[test]
5830    fn opening_costs_the_same_over_a_thousand_times_the_rows() {
5831        let opened = |label: &str, rows_per_part: i32| {
5832            let path = path(label);
5833            let mut writer =
5834                Writer::create(&path, "items", vec![Field::required("id", LogicalType::Integer)])
5835                    .expect("new file");
5836            for part in 0..STRIPE_PARTS * 3 {
5837                // Scrambled rather than sequential, so that the fat file is actually fatter. A run
5838                // of consecutive integers encodes to almost nothing and would leave the two files
5839                // the same size, which would make this test pass for the wrong reason.
5840                let values = (0..rows_per_part)
5841                    .map(|row| {
5842                        Value::Integer((part as i32 * rows_per_part + row).wrapping_mul(2_654_435))
5843                    })
5844                    .collect::<Vec<_>>();
5845                let chunk = Chunk::new(vec![
5846                    Vector::from_values(LogicalType::Integer, &values).expect("integers"),
5847                ])
5848                .expect("matching rows");
5849                writer.append(&chunk).expect("one part");
5850            }
5851            writer.finish().expect("commit");
5852            let reader = Reader::open(&path).expect("reopen from disk");
5853            let size = fs::metadata(&path).expect("the file is there").len();
5854            let out = (reader.reads(), reader.table().stripes().len(), size);
5855            fs::remove_file(path).expect("remove scratch file");
5856            out
5857        };
5858
5859        let (thin, thin_stripes, thin_size) = opened("open-thin", 1);
5860        let (fat, fat_stripes, fat_size) = opened("open-fat", 1000);
5861        assert_eq!(
5862            thin_stripes, fat_stripes,
5863            "the same stripe count is what makes this a fair ask"
5864        );
5865        assert!(
5866            fat_size > thin_size * 50,
5867            "the fat file has to actually be larger, and it is {fat_size} against {thin_size}"
5868        );
5869
5870        assert_eq!(thin.opening.reads, fat.opening.reads, "the same reads either way");
5871        assert_eq!(thin.pages, 0, "opening read a page");
5872        assert_eq!(fat.pages, 0, "opening read a page");
5873        assert_eq!(thin.indexes, 0, "opening read an index");
5874        assert_eq!(fat.indexes, 0, "opening read an index");
5875        // Not exactly equal, because a directory holds offsets and a larger file has larger ones,
5876        // and a handful of bytes of varint is not somebody loading statistics. A factor is.
5877        assert!(
5878            fat.opening.bytes < thin.opening.bytes * 2,
5879            "opening the thin file read {} bytes and the fat one read {}",
5880            thin.opening.bytes,
5881            fat.opening.bytes
5882        );
5883    }
5884
5885    /// The reads a file costs to open are fixed by its shape and not by what ran before.
5886    ///
5887    /// `spec/stats/04-in-memory.md` section 4.3, which is the rule that keeps a plan reproducible:
5888    /// the plan is a function of the data, the generation and the settings, and never of what
5889    /// happened to be in cache. Opening the same file twice in the same process has to cost the
5890    /// same, because a second open that read less would be an open that was about to plan
5891    /// differently.
5892    #[test]
5893    fn two_opens_of_one_file_cost_the_same_and_the_second_is_not_cheaper() {
5894        let path = path("open-twice");
5895        let mut writer =
5896            Writer::create(&path, "items", vec![Field::required("id", LogicalType::Integer)])
5897                .expect("new file");
5898        for part in 0..STRIPE_PARTS * 3 {
5899            let chunk = Chunk::new(vec![
5900                Vector::from_values(LogicalType::Integer, &[Value::Integer(part as i32)])
5901                    .expect("integers"),
5902            ])
5903            .expect("matching rows");
5904            writer.append(&chunk).expect("one part");
5905        }
5906        writer.finish().expect("commit");
5907
5908        let first = Reader::open(&path).expect("open");
5909        // A whole scan in between, so the operating system's page cache is as warm as it gets and
5910        // anything that consulted it would show up in the second open.
5911        for part in 0..first.parts() {
5912            first.read(part, &[0]).expect("a part");
5913        }
5914        assert!(first.reads().pages > 0, "the scan has to have read something");
5915        let second = Reader::open(&path).expect("open again");
5916
5917        assert_eq!(first.reads().opening, second.reads().opening);
5918        assert_eq!(
5919            second.reads().pages,
5920            0,
5921            "the second open read a page off the back of the first"
5922        );
5923        assert_eq!(second.reads().indexes, 0, "the second open read an index it inherited");
5924        fs::remove_file(path).expect("remove scratch file");
5925    }
5926
5927    /// A scan reads a stripe's index once for the whole scan, not once per part that misses.
5928    ///
5929    /// The page cache holds four stripes and an index used to ride inside it, so a table with more
5930    /// stripes than that read the index again every time a stripe came back around. The index is a
5931    /// few hundred bytes and the page is a quarter of a megabyte, which is why they are now under
5932    /// different budgets. This is the test that keeps them there, since the saving is small enough
5933    /// that nothing in a benchmark would notice it going away again.
5934    #[test]
5935    fn an_index_is_read_once_per_stripe_however_often_the_page_is_evicted() {
5936        let path = path("index-cache");
5937        let mut writer =
5938            Writer::create(&path, "items", vec![Field::required("id", LogicalType::Integer)])
5939                .expect("new file");
5940        let parts = STRIPE_PARTS * (CACHED_STRIPES_PER_COLUMN + 2);
5941        for part in 0..parts {
5942            let id = part as i32;
5943            let chunk = Chunk::new(vec![
5944                Vector::from_values(LogicalType::Integer, &[Value::Integer(id)]).expect("integers"),
5945            ])
5946            .expect("matching rows");
5947            writer.append(&chunk).expect("one part");
5948        }
5949        writer.finish().expect("commit");
5950
5951        let reader = Reader::open(&path).expect("reopen from disk");
5952        let stripes = reader.table().stripes().len();
5953        assert!(stripes > CACHED_STRIPES_PER_COLUMN, "the page cache has to be too small for this");
5954        // Twice over, so that the second pass finds every page evicted and every index kept.
5955        for _ in 0..2 {
5956            for part in 0..parts {
5957                let chunk = reader.read(part, &[0]).expect("a part");
5958                assert_eq!(chunk.value_at(0, 0), Value::Integer(part as i32));
5959            }
5960        }
5961        assert_eq!(reader.indexes.load(Atomic::Relaxed), stripes, "one index read per stripe");
5962        assert!(
5963            reader.pages.load(Atomic::Relaxed) > stripes,
5964            "the pages are the ones that get read again, which is what makes the index count mean \
5965             something"
5966        );
5967        fs::remove_file(path).expect("remove scratch file");
5968    }
5969
5970    /// A worker per stripe reads its stripe once, once the cache has been told how many there are.
5971    ///
5972    /// This is the shape a scan has when it hands out a whole stripe per morsel rather than a part.
5973    /// Nobody races for a page any more, but every worker holds a different one for the length of a
5974    /// stripe, so a cache that keeps four pages while eight workers are in eight stripes evicts
5975    /// every one of them before its owner has finished with it, and the owner reads a quarter of a
5976    /// megabyte again for the next part. The barrier is what makes that certain rather than likely:
5977    /// without it a worker can run a whole stripe before the next one starts and never collide.
5978    #[test]
5979    fn a_worker_per_stripe_reads_its_page_once_when_the_cache_was_told_to_expect_it() {
5980        let workers = CACHED_STRIPES_PER_COLUMN + 4;
5981        let path = path("stripe-per-worker");
5982        let mut writer =
5983            Writer::create(&path, "items", vec![Field::required("id", LogicalType::Integer)])
5984                .expect("new file");
5985        for part in 0..STRIPE_PARTS * workers {
5986            let chunk = Chunk::new(vec![
5987                Vector::from_values(LogicalType::Integer, &[Value::Integer(part as i32)])
5988                    .expect("integers"),
5989            ])
5990            .expect("matching rows");
5991            writer.append(&chunk).expect("one part");
5992        }
5993        writer.finish().expect("commit");
5994
5995        let read = |told: bool| {
5996            let reader = Reader::open(&path).expect("reopen from disk");
5997            assert_eq!(reader.table().stripes().len(), workers, "a stripe per worker");
5998            if told {
5999                reader.keep_stripes(workers);
6000            }
6001            let barrier = std::sync::Barrier::new(workers);
6002            std::thread::scope(|scope| {
6003                for (worker, run) in reader.stripe_parts().into_iter().enumerate() {
6004                    let reader = &reader;
6005                    let barrier = &barrier;
6006                    scope.spawn(move || {
6007                        for part in run {
6008                            barrier.wait();
6009                            let chunk = reader.read(part, &[0]).expect("a part of my own stripe");
6010                            assert_eq!(chunk.value_at(0, 0), Value::Integer(part as i32));
6011                        }
6012                        assert!(worker < workers);
6013                    });
6014                }
6015            });
6016            reader.pages.load(Atomic::Relaxed)
6017        };
6018
6019        assert_eq!(read(true), workers, "one page read per stripe and no more");
6020        assert!(read(false) > workers, "a cache that small is read again on every part");
6021        fs::remove_file(path).expect("remove scratch file");
6022    }
6023
6024    /// A damaged index page is caught before anything decodes a part out of it.
6025    ///
6026    /// The index is the one structure a reader trusts to find bytes with, so it carries a checksum
6027    /// per column section rather than one for the page, and this is what says that check runs.
6028    #[test]
6029    fn a_damaged_index_page_is_an_error() {
6030        let path = path("damaged-index");
6031        let mut writer =
6032            Writer::create(&path, "items", vec![Field::required("id", LogicalType::Integer)])
6033                .expect("new file");
6034        writer.append(&sample_ids()).expect("first part");
6035        writer.append(&sample_ids()).expect("second part");
6036        writer.finish().expect("commit");
6037
6038        let reader = Reader::open(&path).expect("valid directory");
6039        let index = reader.table.stripes[0].index;
6040        let mut byte = [0; 1];
6041        read_at(&reader.file, index.offset, &mut byte).expect("the first part length");
6042        let mut file = OpenOptions::new().write(true).open(&path).expect("open index page");
6043        file.seek(SeekFrom::Start(index.offset)).expect("index start");
6044        file.write_all(&[!byte[0]]).expect("damage the first part length");
6045        let error = reader.read(1, &[0]).expect_err("a damaged index must not be used");
6046        assert!(error.message().contains("index page section checksum differs"), "{error}");
6047        fs::remove_file(path).expect("remove scratch file");
6048    }
6049
6050    /// Every integer width the format knows about, written and read back.
6051    ///
6052    /// The unsigned ones are the reason ClickBench can be stored at all: `hits` types `EventDate`
6053    /// as `USMALLINT`, and one unsupported column meant the whole table was refused. The extremes
6054    /// are in here on purpose, because a width that round trips through the wrong signedness only
6055    /// goes wrong at the end of its range.
6056    #[test]
6057    fn every_integer_width_round_trips_through_a_page() {
6058        let path = path("integer-widths");
6059        let columns = [
6060            (LogicalType::TinyInt, vec![Value::TinyInt(i8::MIN), Value::TinyInt(i8::MAX)]),
6061            (LogicalType::UTinyInt, vec![Value::UTinyInt(0), Value::UTinyInt(u8::MAX)]),
6062            (LogicalType::SmallInt, vec![Value::SmallInt(i16::MIN), Value::SmallInt(i16::MAX)]),
6063            (LogicalType::USmallInt, vec![Value::USmallInt(0), Value::USmallInt(u16::MAX)]),
6064            (LogicalType::Integer, vec![Value::Integer(i32::MIN), Value::Integer(i32::MAX)]),
6065            (LogicalType::UInteger, vec![Value::UInteger(0), Value::UInteger(u32::MAX)]),
6066            (LogicalType::BigInt, vec![Value::BigInt(i64::MIN), Value::BigInt(i64::MAX)]),
6067            (LogicalType::UBigInt, vec![Value::UBigInt(0), Value::UBigInt(u64::MAX)]),
6068        ];
6069        let fields = columns
6070            .iter()
6071            .enumerate()
6072            .map(|(at, (ty, _))| Field::required(format!("c{at}"), ty.clone()))
6073            .collect::<Vec<_>>();
6074        let vectors = columns
6075            .iter()
6076            .map(|(ty, values)| Vector::from_values(ty.clone(), values).expect("a vector"))
6077            .collect::<Vec<_>>();
6078        let mut writer = Writer::create(&path, "widths", fields).expect("new file");
6079        writer.append(&Chunk::new(vectors).expect("matching rows")).expect("one stripe");
6080        writer.finish().expect("commit");
6081
6082        let reader = Reader::open(&path).expect("reopen from disk");
6083        let wanted = (0..columns.len()).collect::<Vec<_>>();
6084        let read = reader.read(0, &wanted).expect("every column");
6085        assert_eq!(read.len(), 2);
6086        // row at a time: each column has its own type and its own pair of extremes.
6087        for (at, (ty, values)) in columns.iter().enumerate() {
6088            assert_eq!(read.value_at(0, at), values[0], "the low end of {ty}");
6089            assert_eq!(read.value_at(1, at), values[1], "the high end of {ty}");
6090        }
6091        fs::remove_file(path).expect("remove scratch file");
6092    }
6093
6094    #[test]
6095    fn numeric_frequency_candidates_keep_bounded_row_ordinals() {
6096        let path = path("frequency-ordinals");
6097        let mut writer =
6098            Writer::create(&path, "items", vec![Field::required("id", LogicalType::BigInt)])
6099                .expect("new file");
6100        let mut values = Vec::new();
6101        for leader in 0..10_i64 {
6102            values.extend(std::iter::repeat_n(leader, 100));
6103        }
6104        values.extend(1_000_i64..41_000);
6105        for part in values.chunks(1_024) {
6106            let vector = Vector::flat(LogicalType::BigInt, Data::Int64(part.to_vec().into()))
6107                .expect("big integers");
6108            writer.append(&Chunk::new(vec![vector]).expect("one column")).expect("one stripe");
6109        }
6110        writer.finish().expect("commit");
6111
6112        let reader = Reader::open(&path).expect("reopen from disk");
6113        let occurrences =
6114            reader.frequency_occurrences(0).expect("valid metadata").expect("bounded ordinals");
6115        assert!(occurrences.omitted_max < 100);
6116        assert!(occurrences.ordinals.len() <= FREQUENCY_ORDINALS);
6117        assert!(occurrences.ordinals.windows(2).all(|pair| pair[0] < pair[1]));
6118        assert_eq!(&occurrences.ordinals[..1_000], &(0_u64..1_000).collect::<Vec<_>>());
6119        fs::remove_file(path).expect("remove scratch file");
6120    }
6121
6122    /// The bug this is here for cost a 43 GB ClickBench table and an hour of reloading it. The
6123    /// format went from 11 to 12, every binary built after that said "magic or major version is
6124    /// unsupported" about the file, and there was no way to tell from the message whether the path
6125    /// was wrong, the file was truncated, or it was ours and simply older. The number this build
6126    /// wants is the whole answer and it was the one thing the message did not carry.
6127    #[test]
6128    fn a_file_from_another_format_says_which_format_it_is() {
6129        let older = path("older-format");
6130        let mut writer =
6131            Writer::create(&older, "items", vec![Field::new("id", LogicalType::Integer)])
6132                .expect("new file");
6133        let chunk = Chunk::new(vec![
6134            Vector::flat(LogicalType::Integer, Data::Int32(vec![1, 2, 3].into()))
6135                .expect("integers"),
6136        ])
6137        .expect("chunk");
6138        writer.append(&chunk).expect("page written");
6139        writer.finish().expect("commit");
6140
6141        let mut file = OpenOptions::new().write(true).open(&older).expect("open for the header");
6142        file.seek(SeekFrom::Start(8)).expect("the version follows the magic");
6143        file.write_all(&(FORMAT - 1).to_le_bytes()).expect("write an older version");
6144        drop(file);
6145        let complaint = Reader::open(&older).expect_err("an older format is refused").to_string();
6146        assert!(complaint.contains(&format!("format {}", FORMAT - 1)), "{complaint}");
6147        assert!(complaint.contains(&format!("format {FORMAT}")), "{complaint}");
6148
6149        let mut file = OpenOptions::new().write(true).open(&older).expect("open for the header");
6150        file.seek(SeekFrom::Start(0)).expect("the magic is first");
6151        file.write_all(b"NOTRUDB!").expect("write another engine's magic");
6152        drop(file);
6153        let complaint = Reader::open(&older).expect_err("a foreign file is refused").to_string();
6154        assert!(complaint.contains("magic"), "{complaint}");
6155        assert!(!complaint.contains("format"), "a version has nothing to do with it: {complaint}");
6156        fs::remove_file(older).expect("remove scratch file");
6157    }
6158
6159    #[test]
6160    fn an_unfinished_or_damaged_file_does_not_answer_with_partial_rows() {
6161        let unfinished = path("unfinished");
6162        let mut writer =
6163            Writer::create(&unfinished, "items", vec![Field::new("id", LogicalType::Integer)])
6164                .expect("new file");
6165        let chunk = Chunk::new(vec![
6166            Vector::flat(LogicalType::Integer, Data::Int32(vec![1, 2, 3].into()))
6167                .expect("integers"),
6168        ])
6169        .expect("chunk");
6170        writer.append(&chunk).expect("page written");
6171        drop(writer);
6172        assert!(Reader::open(&unfinished).is_err(), "no directory was committed");
6173        fs::remove_file(unfinished).expect("remove scratch file");
6174
6175        let damaged = path("damaged");
6176        let mut writer =
6177            Writer::create(&damaged, "items", vec![Field::new("id", LogicalType::Integer)])
6178                .expect("new file");
6179        writer.append(&chunk).expect("page written");
6180        writer.finish().expect("commit");
6181        let reader = Reader::open(&damaged).expect("valid directory");
6182        let mut file =
6183            OpenOptions::new().write(true).open(&damaged).expect("open for a damaged page");
6184        file.seek(SeekFrom::Start(HEADER + 1)).expect("inside first page");
6185        file.write_all(&[255]).expect("damage one byte");
6186        assert!(reader.read(0, &[0]).is_err(), "page checksum rejects corruption");
6187        fs::remove_file(damaged).expect("remove scratch file");
6188    }
6189
6190    #[test]
6191    fn damaged_lazy_dictionary_payload_is_an_error() {
6192        let path = path("damaged-dictionary");
6193        let mut writer = Writer::create(
6194            &path,
6195            "items",
6196            vec![
6197                Field::required("id", LogicalType::Integer),
6198                Field::new("text", LogicalType::Varchar),
6199            ],
6200        )
6201        .expect("new file");
6202        writer.append(&sample()).expect("stripe written");
6203        writer.finish().expect("commit");
6204
6205        let reader = Reader::open(&path).expect("valid directory");
6206        let dictionary = reader.table.dictionaries[1].expect("string dictionary page");
6207        // Read the count out of the page rather than writing it here, so that adding something
6208        // else to the index does not silently turn this into a test that damages the index.
6209        let mut header = [0; DICTIONARY_HEADER];
6210        read_at(&reader.file, dictionary.offset, &mut header).expect("dictionary header");
6211        let index_len = dictionary_index_len(&header);
6212        let rank_len = last_rank_end(&reader.file, dictionary.offset, &header);
6213        let mut file = OpenOptions::new().write(true).open(&path).expect("open dictionary page");
6214        file.seek(SeekFrom::Start(dictionary.offset + index_len + rank_len))
6215            .expect("inside dictionary payload");
6216        file.write_all(&[255]).expect("damage dictionary payload");
6217
6218        let chunk = reader.read(0, &[1]).expect("code page and dictionary index remain valid");
6219        let error =
6220            chunk.validate_external().expect_err("payload corruption must reach the caller");
6221        assert!(error.message().contains("payload checksum differs"), "{error}");
6222        fs::remove_file(path).expect("remove scratch file");
6223    }
6224
6225    /// A payload of many blocks reads and checks every block of it.
6226    ///
6227    /// The test above has a dictionary of three values, which is one block, so it says nothing
6228    /// about a reader finding the right block among many. This one has thirty thousand values,
6229    /// which is thirty blocks, and it reads a value out of the first block and a value out of the
6230    /// last and then damages the last and asks for it again.
6231    #[test]
6232    fn a_dictionary_over_many_blocks_checks_every_block_of_it() {
6233        let path = path("dictionary-blocks");
6234        let value =
6235            |row: usize| format!("{row:07} a value long enough to be worth a payload block");
6236        let parts = 30;
6237        let per_part = 1000;
6238        let mut writer =
6239            Writer::create(&path, "items", vec![Field::required("text", LogicalType::Varchar)])
6240                .expect("new file");
6241        for part in 0..parts {
6242            let values = (0..per_part)
6243                .map(|row| Value::Varchar(value(part * per_part + row)))
6244                .collect::<Vec<_>>();
6245            let chunk = Chunk::new(vec![
6246                Vector::from_values(LogicalType::Varchar, &values).expect("strings"),
6247            ])
6248            .expect("matching rows");
6249            writer.append(&chunk).expect("a part");
6250        }
6251        writer.finish().expect("commit");
6252
6253        let reader = Reader::open(&path).expect("reopen from disk");
6254        let dictionary = reader.table.dictionaries[0].expect("string dictionary page");
6255        assert!(
6256            parts * per_part > TEXT_PAYLOAD_VALUES * 4,
6257            "the dictionary has to be several blocks for this to be testing anything"
6258        );
6259        for part in [0, parts - 1] {
6260            let chunk = reader.read(part, &[0]).expect("a part");
6261            chunk.validate_external().expect("every payload block checks out");
6262            assert_eq!(chunk.value_at(0, 0), Value::Varchar(value(part * per_part)));
6263        }
6264
6265        let mut file = OpenOptions::new().write(true).open(&path).expect("open dictionary page");
6266        file.seek(SeekFrom::Start(dictionary.offset + u64::from(dictionary.length) - 4))
6267            .expect("the last bytes of the page are payload");
6268        file.write_all(&[255]).expect("damage the last payload block");
6269        let reader = Reader::open(&path).expect("the directory and the index are untouched");
6270        let chunk = reader.read(parts - 1, &[0]).expect("the code page remains valid");
6271        let error = chunk.validate_external().expect_err("the damage must reach the caller");
6272        assert!(error.message().contains("payload checksum differs"), "{error}");
6273        fs::remove_file(path).expect("remove scratch file");
6274    }
6275
6276    /// Values of different lengths read back where the offsets say they do.
6277    ///
6278    /// The offsets are packed at one width for the column, they are relative to the payload block a
6279    /// value lands in, and they go in runs of half a block, so there are two boundaries where the
6280    /// arithmetic could be off by one and neither shows up on values that are all the same length.
6281    /// This writes 5,000 values whose lengths cycle through a wide range and reads every one back,
6282    /// so the first value of a block, the last value of a run and the last value of a block are all
6283    /// covered several times over. An empty value is in the cycle because a zero length span is the
6284    /// case the reader short circuits.
6285    #[test]
6286    fn values_of_different_lengths_read_back_out_of_packed_offsets() {
6287        let path = path("dictionary-offsets");
6288        let value = |row: usize| {
6289            if row % 511 == 3 { String::new() } else { "x".repeat(row % 97) + &format!("{row:05}") }
6290        };
6291        let rows = 5_000;
6292        let mut writer =
6293            Writer::create(&path, "items", vec![Field::required("text", LogicalType::Varchar)])
6294                .expect("new file");
6295        let values = (0..rows).map(|row| Value::Varchar(value(row))).collect::<Vec<_>>();
6296        for part in values.chunks(1_000) {
6297            let chunk =
6298                Chunk::new(vec![Vector::from_values(LogicalType::Varchar, part).expect("strings")])
6299                    .expect("matching rows");
6300            writer.append(&chunk).expect("a part");
6301        }
6302        writer.finish().expect("commit");
6303
6304        let reader = Reader::open(&path).expect("reopen from disk");
6305        assert!(
6306            rows > TEXT_PAYLOAD_VALUES * 4,
6307            "the dictionary has to be several blocks for this to be testing anything"
6308        );
6309        for part in 0..rows / 1_000 {
6310            let chunk = reader.read(part, &[0]).expect("a part");
6311            for row in 0..1_000 {
6312                let row = part * 1_000 + row;
6313                assert_eq!(
6314                    chunk.value_at(row % 1_000, 0),
6315                    Value::Varchar(value(row)),
6316                    "value {row}"
6317                );
6318            }
6319        }
6320        fs::remove_file(path).expect("remove scratch file");
6321    }
6322
6323    /// Every worker of a scan wants the dictionary at the same moment and one of them fetches it.
6324    ///
6325    /// Asking a `OnceLock` whether it holds something answers the question a worker that already has
6326    /// the dictionary is asking and not the one a worker without it is asking, which is whether
6327    /// somebody is already on their way with it. Sixteen workers that all miss will all read the
6328    /// page, all verify it and all decode it, and fifteen will drop the result. Nothing about that
6329    /// is incorrect, which is why it went unnoticed, and it showed up as ClickBench 38 getting
6330    /// slower when the scan in front of it got faster and stopped staggering the arrivals.
6331    ///
6332    /// The barrier is what makes the test about that rather than about luck. Without it the first
6333    /// thread is usually finished before the last one starts and the count is one either way.
6334    #[test]
6335    fn a_global_dictionary_is_opened_once_however_many_workers_ask_at_once() {
6336        let path = path("dictionary-once");
6337        let parts = 8;
6338        let per_part = 500;
6339        let value =
6340            |row: usize| format!("{row:07} a value long enough to be worth a payload block");
6341        let mut writer =
6342            Writer::create(&path, "items", vec![Field::required("text", LogicalType::Varchar)])
6343                .expect("new file");
6344        for part in 0..parts {
6345            let values = (0..per_part)
6346                .map(|row| Value::Varchar(value(part * per_part + row)))
6347                .collect::<Vec<_>>();
6348            let chunk = Chunk::new(vec![
6349                Vector::from_values(LogicalType::Varchar, &values).expect("strings"),
6350            ])
6351            .expect("matching rows");
6352            writer.append(&chunk).expect("a part");
6353        }
6354        writer.finish().expect("commit");
6355
6356        let reader = Reader::open(&path).expect("reopen from disk");
6357        assert!(reader.table.dictionaries[0].is_some(), "the column has to have one to share");
6358        assert_eq!(reader.reads().dictionaries, 0, "opening the file does not open a dictionary");
6359
6360        let workers = 16;
6361        let gate = std::sync::Barrier::new(workers);
6362        std::thread::scope(|scope| {
6363            for worker in 0..workers {
6364                let reader = reader.clone();
6365                let gate = &gate;
6366                scope.spawn(move || {
6367                    gate.wait();
6368                    let chunk = reader.read(worker % parts, &[0]).expect("a part");
6369                    assert_eq!(
6370                        chunk.value_at(0, 0),
6371                        Value::Varchar(value((worker % parts) * per_part))
6372                    );
6373                });
6374            }
6375        });
6376
6377        assert_eq!(reader.reads().dictionaries, 1, "sixteen workers, one dictionary, one open");
6378        fs::remove_file(path).expect("remove scratch file");
6379    }
6380
6381    /// The sorted order sits outside the index the page checksum covers, because a query that
6382    /// never searches a dictionary should not read it, so it carries its own checksums and this is
6383    /// what says they are checked. A search that trusted a damaged order would give a wrong answer
6384    /// rather than a slow one.
6385    #[test]
6386    fn a_damaged_sorted_order_is_an_error() {
6387        let path = path("damaged-order");
6388        let mut writer = Writer::create(
6389            &path,
6390            "items",
6391            vec![
6392                Field::required("id", LogicalType::Integer),
6393                Field::new("text", LogicalType::Varchar),
6394            ],
6395        )
6396        .expect("new file");
6397        writer.append(&sample()).expect("stripe written");
6398        writer.finish().expect("commit");
6399
6400        let reader = Reader::open(&path).expect("valid directory");
6401        let page = reader.table.dictionaries[1].expect("string dictionary page");
6402        let mut header = [0; DICTIONARY_HEADER];
6403        read_at(&reader.file, page.offset, &mut header).expect("dictionary header");
6404        let index_len = dictionary_index_len(&header);
6405        let mut file = OpenOptions::new().write(true).open(&path).expect("open dictionary page");
6406        file.seek(SeekFrom::Start(page.offset + index_len)).expect("the first head");
6407        file.write_all(&[255]).expect("damage the order");
6408
6409        let dictionary = reader.dictionary(1).expect("read").expect("a string column has one");
6410        let error = dictionary.compare_rank(0, b"anything").expect_err("a damaged order is caught");
6411        assert!(error.message().contains("rank checksum differs"), "{error}");
6412        fs::remove_file(path).expect("remove scratch file");
6413    }
6414
6415    /// Codes stay in first appearance order and the sorted order is written beside them, so a
6416    /// reader can put the values back in order without the writer having had to know them all
6417    /// before it handed out the first code.
6418    #[test]
6419    fn a_global_dictionary_carries_the_sorted_order_of_its_values() {
6420        // Chosen so the sort cannot be decided on the first eight bytes alone. Three values share
6421        // a nine byte prefix, one is a prefix of another, and one is empty.
6422        let spellings = ["overlong1z", "b", "", "overlong1a", "overlong", "ab", "a", "overlong1"];
6423        let path = path("dictionary-order");
6424        let mut writer =
6425            Writer::create(&path, "items", vec![Field::new("text", LogicalType::Varchar)])
6426                .expect("new file");
6427        writer
6428            .append(
6429                &Chunk::new(vec![
6430                    Vector::from_values(
6431                        LogicalType::Varchar,
6432                        &spellings.map(|text| Value::Varchar(text.into())),
6433                    )
6434                    .expect("strings"),
6435                ])
6436                .expect("one column"),
6437            )
6438            .expect("stripe written");
6439        writer.finish().expect("commit");
6440
6441        let reader = Reader::open(&path).expect("valid directory");
6442        let dictionary = reader.dictionary(0).expect("read").expect("a string column has one");
6443        let count = dictionary.ranks().expect("a v10 file stores one");
6444        assert_eq!(count, spellings.len(), "every distinct value has a rank");
6445        let order = (0..count)
6446            .map(|rank| dictionary.code_at_rank(rank).expect("a code"))
6447            .collect::<Vec<_>>();
6448        let mut seen = order.clone();
6449        seen.sort_unstable();
6450        assert_eq!(seen, (0..spellings.len() as u32).collect::<Vec<_>>(), "a permutation of codes");
6451
6452        let ranked = order
6453            .iter()
6454            .map(|&code| {
6455                dictionary.try_bytes_at(code as usize).expect("read").expect("a value").to_vec()
6456            })
6457            .collect::<Vec<_>>();
6458        let mut expected = spellings.map(|text| text.as_bytes().to_vec()).to_vec();
6459        expected.sort();
6460        assert_eq!(ranked, expected, "rank order is value order");
6461
6462        // What a search asks, on the values themselves rather than through a kernel, so that a
6463        // file whose heads disagree with its bytes is caught here rather than as a wrong answer.
6464        for (rank, value) in expected.iter().enumerate() {
6465            assert_eq!(
6466                dictionary.compare_rank(rank, value).expect("compare"),
6467                Ordering::Equal,
6468                "rank {rank} is its own value"
6469            );
6470            if rank > 0 {
6471                assert_eq!(
6472                    dictionary.compare_rank(rank - 1, value).expect("compare"),
6473                    Ordering::Less,
6474                    "rank {rank} follows the one before it"
6475                );
6476            }
6477        }
6478        fs::remove_file(path).expect("remove scratch file");
6479    }
6480
6481    /// A sweep of the dictionary reads every value and keeps what it read, up to the budget.
6482    ///
6483    /// The point of the sweep is the resident size rather than the answer, so both are checked
6484    /// here. A dictionary this small is well under [`TEXT_KEEP_BUDGET`], so it keeps everything and
6485    /// a second sweep decodes nothing, which is what makes the second statement of a session asking
6486    /// the same question cost what it should. The ceiling is the other half of it and it has its own
6487    /// test below, because a ceiling that never binds is not a ceiling anybody checked.
6488    #[test]
6489    fn a_dictionary_sweep_reads_every_value_and_keeps_it_under_the_budget() {
6490        let path = path("dictionary-sweep");
6491        // Two thousand five hundred distinct values is two whole payload blocks and a part of a
6492        // third, so the sweep has to be called more than once and the last call has to stop short.
6493        let spellings = (0..2_500)
6494            .map(|index| Value::Varchar(format!("value {index:08} {}", "x".repeat(index % 40))))
6495            .collect::<Vec<_>>();
6496        let mut writer =
6497            Writer::create(&path, "items", vec![Field::new("text", LogicalType::Varchar)])
6498                .expect("new file");
6499        // A chunk is a part and a part is at most 1,024 rows, so the values go in three of them.
6500        // The dictionary is table wide and does not care where a value was written.
6501        for part in spellings.chunks(1_024) {
6502            writer
6503                .append(
6504                    &Chunk::new(vec![
6505                        Vector::from_values(LogicalType::Varchar, part).expect("strings"),
6506                    ])
6507                    .expect("one column"),
6508                )
6509                .expect("stripe written");
6510        }
6511        writer.finish().expect("commit");
6512
6513        let reader = Reader::open(&path).expect("valid directory");
6514        let dictionary = reader.dictionary(0).expect("read").expect("a string column has one");
6515        assert_eq!(dictionary.len(), spellings.len(), "every value is distinct");
6516
6517        let resting = dictionary.footprint();
6518        let mut swept: Vec<Vec<u8>> = Vec::new();
6519        let mut at = 0;
6520        let mut calls = 0;
6521        while at < dictionary.len() {
6522            let stopped = dictionary
6523                .sweep_text(at, dictionary.len(), &mut |index: usize, text: &[u8]| {
6524                    assert_eq!(index, swept.len(), "a sweep hands its values over in order");
6525                    swept.push(text.to_vec());
6526                    Ok(())
6527                })
6528                .expect("a sweep reads");
6529            assert!(stopped > at, "a sweep moves");
6530            at = stopped;
6531            calls += 1;
6532        }
6533        assert_eq!(calls, 3, "a sweep hands over one block at a time");
6534        let after = dictionary.footprint();
6535        assert!(after > resting, "a sweep under the budget keeps what it decoded");
6536
6537        let read = (0..dictionary.len())
6538            .map(|code| dictionary.try_bytes_at(code).expect("read").expect("a value").to_vec())
6539            .collect::<Vec<_>>();
6540        assert_eq!(swept, read, "a sweep answers what a point read answers");
6541        assert_eq!(dictionary.footprint(), after, "a point read of a kept block decodes nothing");
6542        fs::remove_file(path).expect("remove scratch file");
6543    }
6544
6545    /// A sweep over a block whose second run of offsets is short reads the same values as a point
6546    /// read does.
6547    ///
6548    /// The sweep decodes the offsets of a whole run at a time rather than a value at a time, and a
6549    /// run holds half a block, so the count it asks for is the run length everywhere but at the end
6550    /// of the dictionary. Two thousand five hundred values, which is what the test above writes,
6551    /// never puts a short run second in its block: the last block there begins on a run boundary and
6552    /// holds one run. Two thousand eight hundred does, so the last block is a whole run of five
6553    /// hundred and twelve followed by two hundred and forty, and an off by one in either the count
6554    /// asked for or the slice taken out of the answer shows up as a wrong value or a refusal.
6555    #[test]
6556    fn a_sweep_over_a_block_with_a_short_second_run_reads_what_a_point_read_reads() {
6557        let path = path("dictionary-sweep-short-run");
6558        let spellings = (0..2_800)
6559            .map(|index| Value::Varchar(format!("value {index:08} {}", "x".repeat(index % 40))))
6560            .collect::<Vec<_>>();
6561        let mut writer =
6562            Writer::create(&path, "items", vec![Field::new("text", LogicalType::Varchar)])
6563                .expect("new file");
6564        for part in spellings.chunks(1_024) {
6565            writer
6566                .append(
6567                    &Chunk::new(vec![
6568                        Vector::from_values(LogicalType::Varchar, part).expect("strings"),
6569                    ])
6570                    .expect("one column"),
6571                )
6572                .expect("stripe written");
6573        }
6574        writer.finish().expect("commit");
6575
6576        let reader = Reader::open(&path).expect("valid directory");
6577        let dictionary = reader.dictionary(0).expect("read").expect("a string column has one");
6578        assert_eq!(dictionary.len(), spellings.len(), "every value is distinct");
6579        let last = dictionary.len() % TEXT_PAYLOAD_VALUES;
6580        assert!(last > TEXT_OFFSET_RUN, "the last block has to reach into a second run of offsets");
6581        assert!(last < TEXT_PAYLOAD_VALUES, "and that second run has to be short of a whole one");
6582
6583        let mut swept: Vec<Vec<u8>> = Vec::new();
6584        let mut at = 0;
6585        while at < dictionary.len() {
6586            let stopped = dictionary
6587                .sweep_text(at, dictionary.len(), &mut |index: usize, text: &[u8]| {
6588                    assert_eq!(index, swept.len(), "a sweep hands its values over in order");
6589                    swept.push(text.to_vec());
6590                    Ok(())
6591                })
6592                .expect("a sweep reads");
6593            assert!(stopped > at, "a sweep moves");
6594            at = stopped;
6595        }
6596        let read = (0..dictionary.len())
6597            .map(|code| dictionary.try_bytes_at(code).expect("read").expect("a value").to_vec())
6598            .collect::<Vec<_>>();
6599        assert_eq!(swept, read, "a sweep answers what a point read answers");
6600        fs::remove_file(path).expect("remove scratch file");
6601    }
6602
6603    /// Narrowing a page takes what fits and refuses the page for anything that does not.
6604    ///
6605    /// The edges of the range on both sides and one step past each of them, for every type, because
6606    /// checking a page separately from converting it is only right if the check refuses exactly what
6607    /// `TryFrom` would have refused, and off by one there is a file that reads back a different
6608    /// number than it was given. The check is a bit pattern rather than a comparison, so it is not
6609    /// the shape a reader would guess from the bounds, which is why all six are here. The empty page
6610    /// is here because a check written the obvious way starts with the extremes the wrong way round
6611    /// and refuses it.
6612    #[test]
6613    fn narrowing_a_page_takes_what_fits_and_refuses_what_does_not() {
6614        assert_eq!(fit::<i8>(&[]).expect("an empty page fits anything"), Vec::<i8>::new());
6615        assert_eq!(fit::<i8>(&[-128, 0, 127]).expect("the edges fit"), vec![-128_i8, 0, 127]);
6616        fit::<i8>(&[128]).expect_err("one past the top does not fit");
6617        fit::<i8>(&[-129]).expect_err("one past the bottom does not fit");
6618        assert_eq!(fit::<u8>(&[0, 255]).expect("the edges fit"), vec![0_u8, 255]);
6619        fit::<u8>(&[256]).expect_err("one past the top does not fit");
6620        fit::<u8>(&[-1]).expect_err("a negative does not fit an unsigned page");
6621        assert_eq!(
6622            fit::<i16>(&[-32_768, 0, 32_767]).expect("the edges fit"),
6623            vec![-32_768_i16, 0, 32_767]
6624        );
6625        fit::<i16>(&[32_768]).expect_err("one past the top does not fit");
6626        fit::<i16>(&[-32_769]).expect_err("one past the bottom does not fit");
6627        assert_eq!(fit::<u16>(&[0, 65_535]).expect("the edges fit"), vec![0_u16, 65_535]);
6628        fit::<u16>(&[65_536]).expect_err("one past the top does not fit");
6629        fit::<u16>(&[-1]).expect_err("a negative does not fit an unsigned page");
6630        assert_eq!(
6631            fit::<i32>(&[i64::from(i32::MIN), 0, i64::from(i32::MAX)]).expect("the edges fit"),
6632            vec![i32::MIN, 0, i32::MAX]
6633        );
6634        fit::<i32>(&[i64::from(i32::MAX) + 1]).expect_err("one past the top does not fit");
6635        fit::<i32>(&[i64::from(i32::MIN) - 1]).expect_err("one past the bottom does not fit");
6636        assert_eq!(
6637            fit::<u32>(&[0, 4_294_967_295]).expect("the edges fit"),
6638            vec![0_u32, 4_294_967_295]
6639        );
6640        fit::<u32>(&[4_294_967_296]).expect_err("one past the top does not fit");
6641        fit::<u32>(&[-1]).expect_err("a negative does not fit an unsigned page");
6642
6643        // One value in a page that fits is still a page that does not, which is the thing an or
6644        // into an accumulator could get wrong in a way a page of one value would never show.
6645        fit::<i8>(&[0, 1, 2, 128, 3]).expect_err("one bad value spoils the page");
6646    }
6647
6648    /// The residue says yes to exactly what `TryFrom` says yes to.
6649    ///
6650    /// The edges above are the cases anyone would think to write down. This is the argument that
6651    /// there are no others, made by asking both questions about every value either narrow type could
6652    /// have an opinion about, and then about the values around the wide edges and the ends of an
6653    /// `i64`, which a range that size cannot reach.
6654    #[test]
6655    fn the_residue_agrees_with_a_checked_conversion_everywhere() {
6656        for value in -70_000_i64..70_000 {
6657            assert_eq!(fit::<i8>(&[value]).is_ok(), i8::try_from(value).is_ok(), "{value} as i8");
6658            assert_eq!(fit::<u8>(&[value]).is_ok(), u8::try_from(value).is_ok(), "{value} as u8");
6659            assert_eq!(fit::<i16>(&[value]).is_ok(), i16::try_from(value).is_ok(), "{value} i16");
6660            assert_eq!(fit::<u16>(&[value]).is_ok(), u16::try_from(value).is_ok(), "{value} u16");
6661        }
6662        let wide = [i64::MIN, i64::MIN + 1, i64::from(i32::MIN), 0, i64::from(u32::MAX), i64::MAX];
6663        for edge in wide {
6664            for step in -2_i64..=2 {
6665                let value = edge.saturating_add(step);
6666                assert_eq!(
6667                    fit::<i32>(&[value]).is_ok(),
6668                    i32::try_from(value).is_ok(),
6669                    "{value} as i32"
6670                );
6671                assert_eq!(
6672                    fit::<u32>(&[value]).is_ok(),
6673                    u32::try_from(value).is_ok(),
6674                    "{value} as u32"
6675                );
6676            }
6677        }
6678    }
6679
6680    /// A dictionary at its budget sweeps without keeping, and still answers what it answered.
6681    ///
6682    /// The budget is a quarter of a gigabyte in a running database, which is a fine size for a real
6683    /// column and no size at all for a test, so this opens the same dictionary a second time with a
6684    /// budget of zero. That is the shape of the hundred million row case: `URL` fills the budget
6685    /// somewhere in the middle of itself and everything past that point is read and dropped, which
6686    /// costs the decode again and holds none of it.
6687    #[test]
6688    fn a_dictionary_at_its_budget_sweeps_without_keeping() {
6689        let path = path("dictionary-budget");
6690        let spellings = (0..2_500)
6691            .map(|index| Value::Varchar(format!("value {index:08} {}", "y".repeat(index % 40))))
6692            .collect::<Vec<_>>();
6693        let mut writer =
6694            Writer::create(&path, "items", vec![Field::new("text", LogicalType::Varchar)])
6695                .expect("new file");
6696        for part in spellings.chunks(1_024) {
6697            writer
6698                .append(
6699                    &Chunk::new(vec![
6700                        Vector::from_values(LogicalType::Varchar, part).expect("strings"),
6701                    ])
6702                    .expect("one column"),
6703                )
6704                .expect("stripe written");
6705        }
6706        writer.finish().expect("commit");
6707
6708        let reader = Reader::open(&path).expect("valid directory");
6709        let page = reader.table.dictionaries[0].expect("a string column has one");
6710        let file = Arc::clone(&reader.file);
6711        let starved = open_global_dictionary(file, page, &LogicalType::Varchar, 0)
6712            .expect("a dictionary opens whatever it may keep");
6713
6714        let resting = starved.footprint();
6715        let mut swept: Vec<Vec<u8>> = Vec::new();
6716        let mut at = 0;
6717        while at < starved.len() {
6718            at = starved
6719                .sweep_text(at, starved.len(), &mut |_index: usize, text: &[u8]| {
6720                    swept.push(text.to_vec());
6721                    Ok(())
6722                })
6723                .expect("a sweep reads");
6724        }
6725        assert_eq!(swept.len(), spellings.len(), "a starved sweep still reads every value");
6726        assert_eq!(starved.footprint(), resting, "and keeps no block it decoded");
6727
6728        let generous = reader.dictionary(0).expect("read").expect("a string column has one");
6729        let read = (0..generous.len())
6730            .map(|code| generous.try_bytes_at(code).expect("read").expect("a value").to_vec())
6731            .collect::<Vec<_>>();
6732        assert_eq!(swept, read, "a starved sweep answers what a point read answers");
6733        fs::remove_file(path).expect("remove scratch file");
6734    }
6735
6736    #[test]
6737    fn damaged_membership_cannot_skip_a_string_page() {
6738        let path = path("damaged-membership");
6739        let mut writer = Writer::create(
6740            &path,
6741            "items",
6742            vec![
6743                Field::required("id", LogicalType::Integer),
6744                Field::new("text", LogicalType::Varchar),
6745            ],
6746        )
6747        .expect("new file");
6748        writer.append(&sample()).expect("stripe written");
6749        writer.finish().expect("commit");
6750
6751        let reader = Reader::open(&path).expect("valid directory");
6752        let membership = reader.table.stripes[0].memberships[1].expect("string membership");
6753        let mut file = OpenOptions::new().write(true).open(&path).expect("open membership page");
6754        file.seek(SeekFrom::Start(membership.offset)).expect("membership start");
6755        file.write_all(&[255]).expect("damage membership");
6756        let error = reader.skips_codes(0, 1, &[3]).expect_err("corruption must not skip rows");
6757        assert!(error.message().contains("membership page checksum differs"), "{error}");
6758        fs::remove_file(path).expect("remove scratch file");
6759    }
6760
6761    #[test]
6762    fn membership_delta_stream_is_sorted_exact_and_bounded() {
6763        let unique = unique_codes(&[900, 4, 4, 72, 9, u32::MAX]);
6764        assert_eq!(unique, [4, 9, 72, 900, u32::MAX]);
6765        let encoded = encode_membership(&unique);
6766        assert_eq!(
6767            decode_membership(&encoded).expect("valid membership"),
6768            [4, 9, 72, 900, u32::MAX]
6769        );
6770        // A stripe's index is the union of its parts', so a code in two of them is in it once and
6771        // the result is still one ascending run of deltas.
6772        let merged = merged_codes(vec![vec![4, 900], vec![9, 900, u32::MAX], vec![72]]);
6773        assert_eq!(merged, [4, 9, 72, 900, u32::MAX]);
6774        assert_eq!(
6775            decode_membership(&encode_membership(&merged)).expect("valid membership"),
6776            unique
6777        );
6778        assert!(decode_membership(&[1, 0x80]).is_err(), "a truncated varint is invalid");
6779        assert!(
6780            decode_membership(&[1, 0xff, 0xff, 0xff, 0xff, 0x10]).is_err(),
6781            "a value past u32 is invalid"
6782        );
6783    }
6784
6785    #[test]
6786    fn a_global_dictionary_may_be_larger_than_one_column_page() {
6787        let dictionary = Page {
6788            offset: HEADER,
6789            length: u32::try_from(MAX_PAGE + 1).expect("the page bound fits on disk"),
6790            hash: 0,
6791        };
6792        let table = Table {
6793            name: "items".to_owned(),
6794            fields: vec![Field::new("text", LogicalType::Varchar)],
6795            stripes: Vec::new(),
6796            rows: 0,
6797            dictionaries: vec![Some(dictionary)],
6798            distincts: vec![None],
6799            frequencies: vec![None],
6800        };
6801        let directory = encode_directory(&table).expect("directory");
6802        let file_size = dictionary.offset + u64::from(dictionary.length) + 1;
6803
6804        let decoded = decode_directory(&directory, file_size).expect("large lazy dictionary");
6805        assert_eq!(decoded.dictionaries[0].expect("dictionary").length, dictionary.length);
6806    }
6807
6808    #[test]
6809    fn a_column_with_one_value_everywhere_costs_almost_nothing_a_row() {
6810        let path = path("constant-codes");
6811        let mut writer =
6812            Writer::create(&path, "items", vec![Field::new("text", LogicalType::Varchar)])
6813                .expect("new file");
6814        let empty = vec![Value::Varchar(String::new()); 1024];
6815        for _ in 0..4 {
6816            let column = Vector::from_values(LogicalType::Varchar, &empty).expect("strings");
6817            writer.append(&Chunk::new(vec![column]).expect("one column")).expect("a part");
6818        }
6819        writer.finish().expect("commit");
6820
6821        let reader = Reader::open(&path).expect("valid directory");
6822        let pages = reader.layout().columns.first().expect("one column").pages;
6823        // This column used to cost four bytes a row, 16,384 of them, the same as a column of four
6824        // thousand distinct URLs would. The cascade calls each part a constant, so what is left is
6825        // a tag, a count and the value, and the row count stops being what drives the number.
6826        assert!(pages < 256, "{pages} bytes of pages for 4,096 rows of one value");
6827        let read = reader.read(3, &[0]).expect("the last part back");
6828        assert_eq!(read.value_at(0, 0), Value::Varchar(String::new()));
6829        assert_eq!(read.value_at(1023, 0), Value::Varchar(String::new()));
6830        fs::remove_file(path).expect("remove scratch file");
6831    }
6832
6833    #[test]
6834    fn a_cascade_value_too_wide_for_its_column_is_refused_rather_than_cut() {
6835        // What a damaged page looks like from here: the cascade decoded, so the bytes are not
6836        // truncated, but the values do not belong to the column the directory says they do.
6837        let over = vec![i64::from(i32::MAX) + 1];
6838        let error = narrowed(&LogicalType::Integer, over).expect_err("a page that disagrees");
6839        assert!(format!("{error}").contains("not of its type"), "{error}");
6840        assert!(narrowed(&LogicalType::BigInt, vec![i64::MIN]).is_ok(), "bigint holds all of i64");
6841        assert!(narrowed(&LogicalType::Varchar, vec![0]).is_err(), "strings are not integers");
6842    }
6843
6844    #[test]
6845    fn a_code_stream_the_cascade_cannot_shrink_is_left_alone() {
6846        // A shift register rather than a run, because an arithmetic run is the one wide shape the
6847        // cascade does shrink. This is what a column with tens of millions of distinct values hands
6848        // over: full width codes with no order to them.
6849        let mut state: u32 = 0x9e37_79b9;
6850        let spread: Vec<u32> = (0..1024)
6851            .map(|_| {
6852                state ^= state << 13;
6853                state ^= state >> 17;
6854                state ^= state << 5;
6855                state
6856            })
6857            .collect();
6858        assert_eq!(encoded_codes(&spread).expect("no failure"), None);
6859        let near: Vec<u32> = (0..1024).collect();
6860        let coded = encoded_codes(&near).expect("no failure").expect("counting up is packable");
6861        assert!(coded.len() < near.len() * 4, "{} bytes for a run of 1,024", coded.len());
6862    }
6863
6864    /// The columns of a stripe are encoded on whichever thread got to them, so the one thing that
6865    /// must not depend on which thread that was is the file. Two writes of the same rows are
6866    /// compared byte for byte rather than value for value, because a dictionary that two columns
6867    /// somehow shared would still read back correctly and would hand out its codes in the order the
6868    /// threads happened to run in, which is exactly what this is here to catch.
6869    #[test]
6870    fn two_writes_of_the_same_rows_give_the_same_bytes() {
6871        fn written(path: &PathBuf) {
6872            let fields = (0..40)
6873                .map(|column| {
6874                    let ty =
6875                        if column % 4 == 0 { LogicalType::Varchar } else { LogicalType::BigInt };
6876                    Field::new(format!("c{column}"), ty)
6877                })
6878                .collect::<Vec<_>>();
6879            let mut writer = Writer::create(path, "wide", fields).expect("new file");
6880            for part in 0..70_u64 {
6881                let columns = (0..40)
6882                    .map(|column| {
6883                        let values = (0..64_u64)
6884                            .map(|row| {
6885                                let seed = part.wrapping_mul(31).wrapping_add(row);
6886                                if column % 4 == 0 {
6887                                    Value::Varchar(format!("v{}", seed % 17))
6888                                } else {
6889                                    Value::BigInt(i64::try_from(seed % 97).expect("small"))
6890                                }
6891                            })
6892                            .collect::<Vec<_>>();
6893                        let ty = if column % 4 == 0 {
6894                            LogicalType::Varchar
6895                        } else {
6896                            LogicalType::BigInt
6897                        };
6898                        Vector::from_values(ty, &values).expect("a column")
6899                    })
6900                    .collect::<Vec<_>>();
6901                writer.append(&Chunk::new(columns).expect("forty columns")).expect("a part");
6902            }
6903            writer.finish().expect("commit");
6904        }
6905
6906        let first = path("repeatable-one");
6907        let second = path("repeatable-two");
6908        written(&first);
6909        written(&second);
6910        let left = fs::read(&first).expect("the first file");
6911        let right = fs::read(&second).expect("the second file");
6912        assert_eq!(left.len(), right.len(), "two writes of the same rows differ in length");
6913        assert!(left == right, "two writes of the same rows differ in their bytes");
6914
6915        // And the rows are still there, since a pair of identically wrong files would pass the
6916        // comparison above on its own.
6917        let reader = Reader::open(&first).expect("valid directory");
6918        assert_eq!(reader.table().rows(), 70 * 64);
6919        let read = reader.read(0, &[0, 1]).expect("the first part back");
6920        assert_eq!(read.value_at(0, 0), Value::Varchar("v0".to_owned()));
6921        assert_eq!(read.value_at(0, 1), Value::BigInt(0));
6922        fs::remove_file(first).expect("remove scratch file");
6923        fs::remove_file(second).expect("remove scratch file");
6924    }
6925}