Skip to main content

rudb_vector/
vector.rs

1//! The vector itself.
2//!
3//! `spec/07-execution.md` section 7.1 calls this the widest interface in the system, says every
4//! operator depends on it, and says changing it after twenty operators exist is expensive. So it
5//! is written before the first operator rather than after the fifth.
6//!
7//! A vector is a type, a length of at most [`VECTOR_SIZE`], a physical form, a validity
8//! representation and some data. The four forms are the ones in `spec/04-architecture.md` section
9//! 4.3: flat, constant, sequence and dictionary. Encoded, the fifth, is the M3 work and it arrives
10//! with the specialization contract rather than before it.
11//!
12//! **What is not here yet.** Buffers are owned. Section 7.1 says a vector borrowed from a buffer
13//! managed page carries a pin, and there is no buffer manager until M2, so there is nothing to pin
14//! and pretending otherwise would be an interface built against an imaginary caller. Nested types
15//! are not stored yet either, for the same reason: a `LIST(STRUCT(...))` is offsets plus child
16//! column chunks, and child column chunks are storage.
17
18use rudb_common::{Error, LogicalType, Result, Value};
19
20use crate::buffer::Buffer;
21use crate::string::{StringColumn, StringView};
22use crate::validity::Validity;
23
24/// How many values are in a full vector.
25///
26/// 1024 rather than DuckDB's 2048, per `spec/04-architecture.md` section 4.3. It is the FastLanes
27/// unit, it makes a validity mask exactly 16 `u64` words, and it keeps a vector of 16 byte string
28/// views at 16 KiB, which is the size at which several of these fit in L1 together rather than
29/// evicting each other.
30pub const VECTOR_SIZE: usize = 1024;
31
32/// Which physical form a vector is in.
33///
34/// An operator asks this once per vector and then takes the path it wants, which is the one branch
35/// per vector that the whole design is willing to spend.
36///
37/// Not exhaustive, and that is a decision rather than an oversight. `Encoded` is the fifth form
38/// and it arrives at layer three with the specialization contract. If this enum were exhaustive,
39/// the day it lands is the day every kernel in the workspace stops compiling, and the pressure at
40/// that moment would be to add an arm to each of them in a hurry rather than to think about what
41/// each one should do with an encoded vector. A required fallback arm means each kernel already
42/// has a correct answer for a form it has never seen, and specializing it is then a change that
43/// can be made one kernel at a time with a benchmark next to it.
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
45#[non_exhaustive]
46pub enum Form {
47    /// One value per position.
48    Flat,
49    /// One value, repeated.
50    Constant,
51    /// A start and a step, computed rather than stored.
52    Sequence,
53    /// Codes into a smaller vector of distinct values.
54    Dictionary,
55}
56
57/// The values of a flat vector, one Rust vector per physical type.
58///
59/// The variants are physical rather than logical, which is what lets `DATE` and `INTEGER` share
60/// storage and share a kernel. What a run of `i32` means is the vector's logical type's business.
61#[derive(Debug, Clone, PartialEq)]
62#[non_exhaustive]
63pub enum Data {
64    /// No values, for the type of an untyped `NULL`.
65    Empty,
66    /// One byte per value.
67    Bool(Buffer<bool>),
68    /// 8 bit signed.
69    Int8(Buffer<i8>),
70    /// 16 bit signed.
71    Int16(Buffer<i16>),
72    /// 32 bit signed.
73    Int32(Buffer<i32>),
74    /// 64 bit signed.
75    Int64(Buffer<i64>),
76    /// 128 bit signed.
77    Int128(Buffer<i128>),
78    /// 8 bit unsigned.
79    UInt8(Buffer<u8>),
80    /// 16 bit unsigned.
81    UInt16(Buffer<u16>),
82    /// 32 bit unsigned.
83    UInt32(Buffer<u32>),
84    /// 64 bit unsigned.
85    UInt64(Buffer<u64>),
86    /// 128 bit unsigned.
87    UInt128(Buffer<u128>),
88    /// IEEE 754 binary32.
89    Float32(Buffer<f32>),
90    /// IEEE 754 binary64.
91    Float64(Buffer<f64>),
92    /// The months, days and microseconds triple.
93    Interval(Buffer<(i32, i32, i64)>),
94    /// Strings, as 16 byte views plus the arena the long ones live in.
95    Varlen(StringColumn),
96}
97
98impl Data {
99    /// How many values are stored.
100    ///
101    /// The match below has no wildcard arm, and that is what makes this function the check that
102    /// keeps [`for_each_layout`](crate::for_each_layout) honest. A variant added to this enum
103    /// without being added to the `all` group fails to compile here, which is a line in a build log
104    /// rather than a layout quietly missing from six kernels.
105    #[must_use]
106    pub fn len(&self) -> usize {
107        macro_rules! lengths {
108            ($(($variant:ident, $native:ty, $zero:expr)),+ $(,)?) => {
109                match self {
110                    Self::Empty => 0,
111                    $(Self::$variant(values) => values.len(),)+
112                }
113            };
114        }
115        crate::for_each_layout!(all, lengths)
116    }
117
118    /// Whether there are no values.
119    #[must_use]
120    pub fn is_empty(&self) -> bool {
121        self.len() == 0
122    }
123
124    /// An integer at `index`, widened, for any of the signed integer layouts.
125    ///
126    /// Used by the decimal path, which needs the unscaled value out of whichever width the width
127    /// and scale picked, and by anything else that would otherwise repeat the same five arms.
128    #[must_use]
129    pub fn signed_at(&self, index: usize) -> Option<i128> {
130        macro_rules! widened {
131            ($(($variant:ident, $native:ty, $zero:expr)),+ $(,)?) => {
132                match self {
133                    $(Self::$variant(v) => v.get(index).map(|&x| i128::from(x)),)+
134                    _ => None,
135                }
136            };
137        }
138        crate::for_each_layout!(signed, widened)
139    }
140
141    /// An unsigned integer at `index`, widened.
142    #[must_use]
143    pub fn unsigned_at(&self, index: usize) -> Option<u128> {
144        macro_rules! widened {
145            ($(($variant:ident, $native:ty, $zero:expr)),+ $(,)?) => {
146                match self {
147                    $(Self::$variant(v) => v.get(index).map(|&x| u128::from(x)),)+
148                    _ => None,
149                }
150            };
151        }
152        crate::for_each_layout!(unsigned, widened)
153    }
154
155    /// The string at `index`, for a `Varlen`.
156    #[must_use]
157    pub fn str_at(&self, index: usize) -> Option<&str> {
158        match self {
159            Self::Varlen(column) => column.get(index),
160            _ => None,
161        }
162    }
163}
164
165/// A type, a length, a validity representation and some data.
166#[derive(Debug, Clone, PartialEq)]
167pub struct Vector {
168    ty: LogicalType,
169    len: usize,
170    validity: Validity,
171    body: Body,
172}
173
174/// What the vector holds, which is what its form is decided by.
175#[derive(Debug, Clone, PartialEq)]
176enum Body {
177    Flat(Data),
178    Constant(Box<Value>),
179    Sequence { start: i64, step: i64 },
180    Dictionary { codes: Vec<u32>, values: Box<Vector> },
181}
182
183impl Vector {
184    /// A flat vector of `data`, all valid.
185    ///
186    /// # Errors
187    ///
188    /// If the data's physical layout is not the one the type calls for. That check is here rather
189    /// than left to the caller because a vector whose type and layout disagree is a wrong answer
190    /// waiting to be read out, and it costs one comparison at construction to prevent.
191    pub fn flat(ty: LogicalType, data: Data) -> Result<Self> {
192        let len = data.len();
193        if !matches!(data, Data::Empty) && layout_of(&data) != ty.physical() {
194            return Err(Error::internal(format!(
195                "a {ty} vector cannot hold {:?} data",
196                layout_of(&data)
197            )));
198        }
199        Ok(Self { ty, len, validity: Validity::AllValid, body: Body::Flat(data) })
200    }
201
202    /// A flat vector built from single values, with the nulls among them turning into validity.
203    ///
204    /// The slow way in, and the only way in that anything outside this crate has. It is what an
205    /// `INSERT`, a `VALUES` clause and a test build a column with, all of which arrive holding
206    /// values rather than a run of `i32`. Nothing on a scan path calls it: a scan produces a run of
207    /// data directly and hands it to [`Self::flat`].
208    ///
209    /// # Errors
210    ///
211    /// If a value is not one the type can hold, or if the type is one that cannot be stored flat
212    /// yet, which today means the nested types.
213    pub fn from_values(ty: LogicalType, values: &[Value]) -> Result<Self> {
214        let mut data = empty_data_for(&ty)?;
215        for value in values {
216            push_value(&mut data, value)?;
217        }
218        let validity = Validity::from_iter(values.len(), |index| !values[index].is_null());
219        Ok(Self { ty, len: values.len(), validity, body: Body::Flat(data) })
220    }
221
222    /// A vector of `len` copies of one value.
223    ///
224    /// Costs one value regardless of the length, which is what makes a literal in a predicate free
225    /// and what makes a projection of a constant free.
226    #[must_use]
227    pub fn constant(ty: LogicalType, value: Value, len: usize) -> Self {
228        let validity = if value.is_null() { Validity::AllInvalid } else { Validity::AllValid };
229        Self { ty, len, validity, body: Body::Constant(Box::new(value)) }
230    }
231
232    /// A vector of `len` values starting at `start` and stepping by `step`.
233    ///
234    /// This is what a row identifier column is, and it costs sixteen bytes rather than eight
235    /// kilobytes. A scan that produces row ids for a later fetch produces one of these.
236    #[must_use]
237    pub fn sequence(start: i64, step: i64, len: usize) -> Self {
238        Self {
239            ty: LogicalType::BigInt,
240            len,
241            validity: Validity::AllValid,
242            body: Body::Sequence { start, step },
243        }
244    }
245
246    /// A vector of codes into a smaller vector of distinct values.
247    ///
248    /// The form the whole M3 thesis rests on. A dictionary vector handed to a group by is an
249    /// integer column, and an aggregate over one is an aggregate over integers no matter what the
250    /// logical type says.
251    ///
252    /// A dictionary over a dictionary is composed into one level here rather than left as two, so
253    /// the form has a depth of one always and a kernel that reads [`Self::dictionary_parts`] is
254    /// reading the values rather than another layer of codes. Two filters over the same chunk build
255    /// the second case and four conjuncts pushed down separately build four of it.
256    ///
257    /// The cost of leaving them stacked turned out to be a cliff rather than a slope. Every loop in
258    /// `rudb-kernels` reaches for the values behind the codes with [`Self::data`], a dictionary
259    /// pointing at a dictionary has no data to hand back, so the second level does not make the
260    /// kernels slower, it turns them off and drops the work onto the row at a time path that exists
261    /// to be correct rather than fast. Measured on server3 over a chunk of two numeric columns and a
262    /// consumer of two vectorized passes, one level reads at 3.5 nanoseconds a row and two levels at
263    /// 104, and the third and fourth levels cost almost nothing more because the first one had
264    /// already given up everything there was to give. Composing is one pass over the outer codes,
265    /// which the range check above is already making.
266    ///
267    /// The one dictionary that is not composed past is one carrying a validity of its own. A
268    /// dictionary is built all valid and only [`Self::with_validity`] can change that, so such a
269    /// vector is saying that its nulls are at this level rather than in the values it points at, and
270    /// composing past it would drop them.
271    ///
272    /// # Errors
273    ///
274    /// If any code is past the end of the value vector.
275    pub fn dictionary(codes: Vec<u32>, values: Vector) -> Result<Self> {
276        if let Some(&bad) = codes.iter().find(|&&code| code as usize >= values.len()) {
277            return Err(Error::internal(format!(
278                "dictionary code {bad} is past the end of a {} value dictionary",
279                values.len()
280            )));
281        }
282        let (codes, values) = compose(codes, values);
283        Ok(Self {
284            ty: values.ty.clone(),
285            len: codes.len(),
286            validity: Validity::AllValid,
287            body: Body::Dictionary { codes, values: Box::new(values) },
288        })
289    }
290
291    /// The same vector with a different validity.
292    #[must_use]
293    pub fn with_validity(mut self, validity: Validity) -> Self {
294        self.validity = validity;
295        self
296    }
297
298    /// What kind of values these are.
299    #[must_use]
300    pub fn logical_type(&self) -> &LogicalType {
301        &self.ty
302    }
303
304    /// How many values there are.
305    #[must_use]
306    pub fn len(&self) -> usize {
307        self.len
308    }
309
310    /// Whether there are no values.
311    #[must_use]
312    pub fn is_empty(&self) -> bool {
313        self.len == 0
314    }
315
316    /// Which of the values are not null.
317    #[must_use]
318    pub fn validity(&self) -> &Validity {
319        &self.validity
320    }
321
322    /// Which physical form this vector is in.
323    #[must_use]
324    pub fn form(&self) -> Form {
325        match self.body {
326            Body::Flat(_) => Form::Flat,
327            Body::Constant(_) => Form::Constant,
328            Body::Sequence { .. } => Form::Sequence,
329            Body::Dictionary { .. } => Form::Dictionary,
330        }
331    }
332
333    /// The data, for a flat vector, and `None` for any other form.
334    ///
335    /// A kernel that wants a slice asks for it and takes the flat path if it gets one. A kernel
336    /// that can do better on a constant or a dictionary checks [`Self::form`] first.
337    #[must_use]
338    pub fn data(&self) -> Option<&Data> {
339        match &self.body {
340            Body::Flat(data) => Some(data),
341            _ => None,
342        }
343    }
344
345    /// The one value, for a constant vector, and `None` for any other form.
346    ///
347    /// A kernel comparing a column against a literal wants the literal once rather than 1024
348    /// times, and [`Self::value_at`] on a constant clones it on every call because it has to be
349    /// able to hand back a `Value` for any form. This is the accessor that lets the specialized
350    /// path hoist the clone out of the loop.
351    #[must_use]
352    pub fn constant_value(&self) -> Option<&Value> {
353        match &self.body {
354            Body::Constant(value) => Some(value.as_ref()),
355            _ => None,
356        }
357    }
358
359    /// The codes and the values, for a dictionary vector, and `None` for any other form.
360    ///
361    /// The reason a kernel needs this rather than reading the dictionary through
362    /// [`Self::value_at`] is the entire argument for the form existing. A filter against a
363    /// dictionary column of 1024 rows and 40 distinct values is 40 comparisons and 1024 lookups,
364    /// not 1024 comparisons, and there is no way to write that loop without seeing the codes.
365    ///
366    /// Note what the validity of the returned vector means. A dictionary keeps its nulls in the
367    /// vector it points at, and the dictionary's own validity says nothing about them, so a caller
368    /// deciding whether row `i` is null has to ask the value vector about `codes[i]` rather than
369    /// asking this vector about `i`. [`Self::flatten`] has the same note on it for the same
370    /// reason, because getting this wrong is a null that survives being selected and comes out as
371    /// a zero.
372    #[must_use]
373    pub fn dictionary_parts(&self) -> Option<(&[u32], &Self)> {
374        match &self.body {
375            Body::Dictionary { codes, values } => Some((codes, values.as_ref())),
376            _ => None,
377        }
378    }
379
380    /// The start and the step, for a sequence vector, and `None` for any other form.
381    #[must_use]
382    pub fn sequence_parts(&self) -> Option<(i64, i64)> {
383        match self.body {
384            Body::Sequence { start, step } => Some((start, step)),
385            _ => None,
386        }
387    }
388
389    /// The value at `index`, as a single value.
390    ///
391    /// This is the slow path on purpose. It is what a result set is read out with and what a test
392    /// asserts on, and an operator that calls it per row is an operator that has already lost the
393    /// argument the vector interface exists to win.
394    #[must_use]
395    pub fn value_at(&self, index: usize) -> Value {
396        if index >= self.len || !self.validity.is_valid(index) {
397            return Value::Null;
398        }
399        match &self.body {
400            Body::Constant(value) => value.as_ref().clone(),
401            Body::Sequence { start, step } => Value::BigInt(start + step * index as i64),
402            Body::Dictionary { codes, values } => match codes.get(index) {
403                Some(&code) => values.value_at(code as usize),
404                None => Value::Null,
405            },
406            Body::Flat(data) => value_from(&self.ty, data, index),
407        }
408    }
409
410    /// Every value in order, as single values.
411    pub fn iter(&self) -> impl Iterator<Item = Value> + '_ {
412        (0..self.len).map(|index| self.value_at(index))
413    }
414
415    /// The same values in flat form.
416    ///
417    /// Flattening a vector that is already flat is free. Flattening any other form costs a copy,
418    /// which is exactly why the other forms exist and why nothing on the hot path should call
419    /// this. It is here for the operators that genuinely cannot do better and for the tests that
420    /// check the other forms against it.
421    ///
422    /// # Errors
423    ///
424    /// If the type is one this crate cannot store flat yet, which today means the nested types.
425    pub fn flatten(&self) -> Result<Self> {
426        if let Body::Flat(_) = self.body {
427            return Ok(self.clone());
428        }
429        self.copied((0..self.len).collect(), false)
430    }
431
432    /// The values at the given positions, copied, in a form that does not point back at this vector.
433    ///
434    /// This is the copying counterpart to [`Self::dictionary`], and the two are the two halves of
435    /// the decision `spec/07-execution.md` section 7.1 describes. Which half is right is measured
436    /// rather than argued, and [`Chunk::compact`](crate::Chunk::compact) is where the measurement
437    /// is written down.
438    ///
439    /// A dictionary chain is walked to its leaf first and the codes composed on the way down, so the
440    /// copy runs once over the data rather than once per level, and a position that is null at any
441    /// level comes out null here. The copy is a typed loop per physical layout rather than a `Value`
442    /// per row, which is the whole point of it and is what [`Self::flatten`] now goes through too.
443    ///
444    /// # Errors
445    ///
446    /// If the type has no flat layout, which today means the nested types.
447    pub fn gather(&self, indices: &[u32]) -> Result<Self> {
448        self.copied(indices.iter().map(|&index| index as usize).collect(), true)
449    }
450
451    /// The copy both [`Self::gather`] and [`Self::flatten`] are.
452    ///
453    /// `constants_stay` is the one thing the two want differently. A gather of a constant is a
454    /// shorter constant and copying it out would be a thousand writes of the same value for nothing,
455    /// but flattening promises flat form to a caller that is about to read the data slice, so for
456    /// that one the constant has to be written out.
457    fn copied(&self, at: Vec<usize>, constants_stay: bool) -> Result<Self> {
458        let rows = at.len();
459        let (at, leaf) = self.resolve(at);
460        let live: Vec<bool> = at.iter().map(|&index| index != NOWHERE).collect();
461        let validity = Validity::from_run(&live);
462        let body = match &leaf.body {
463            // Every position holds the same value, so the only thing the gather can change is the
464            // length and which positions are null. A gather with no null in it is still a constant.
465            Body::Constant(value) => {
466                if constants_stay && matches!(validity, Validity::AllValid) {
467                    return Ok(Self::constant(self.ty.clone(), value.as_ref().clone(), rows));
468                }
469                let mut data = empty_data_for(&self.ty)?;
470                for &index in &at {
471                    push_value(&mut data, if index == NOWHERE { &Value::Null } else { value })?;
472                }
473                Body::Flat(data)
474            }
475            // A sequence is arithmetic rather than storage, so the gather is the arithmetic done at
476            // the positions asked for, and a null writes the zero every other layout writes.
477            Body::Sequence { start, step } => Body::Flat(Data::Int64(
478                at.iter()
479                    .map(|&index| if index == NOWHERE { 0 } else { start + step * index as i64 })
480                    .collect(),
481            )),
482            // A flat body with no values is the untyped null, so every position asked for is null
483            // whatever was asked for. Going through the copy would build a run of no values and
484            // call it `rows` long, which is a vector whose length and data disagree.
485            Body::Flat(Data::Empty) => {
486                return Ok(Self::constant(self.ty.clone(), Value::Null, rows));
487            }
488            Body::Flat(data) => Body::Flat(copy_of(data, &at)),
489            // Unreachable, because `resolve` stops at the first body that is not a dictionary.
490            Body::Dictionary { .. } => {
491                return Err(Error::internal("a dictionary survived being resolved"));
492            }
493        };
494        Ok(Self { ty: self.ty.clone(), len: rows, validity, body })
495    }
496
497    /// Where each wanted position lives in the first body that is not a dictionary, and that body.
498    ///
499    /// A position that is null anywhere on the way down, or past the end of anything on the way
500    /// down, comes back as [`NOWHERE`]. That single sentinel is what keeps the copy loop from
501    /// carrying a validity mask alongside the positions it is already walking.
502    fn resolve(&self, mut at: Vec<usize>) -> (Vec<usize>, &Self) {
503        let mut source = self;
504        loop {
505            for slot in &mut at {
506                if *slot >= source.len || !source.validity.is_valid(*slot) {
507                    *slot = NOWHERE;
508                }
509            }
510            let Body::Dictionary { codes, values } = &source.body else {
511                return (at, source);
512            };
513            for slot in &mut at {
514                *slot = match codes.get(*slot) {
515                    Some(&code) => code as usize,
516                    None => NOWHERE,
517                };
518            }
519            source = values.as_ref();
520        }
521    }
522}
523
524/// One level of dictionary out of however many levels were handed to [`Vector::dictionary`].
525///
526/// Every dictionary in the system is built through that constructor and every one of them comes
527/// through here first, so the invariant this maintains is that the vector a dictionary points at is
528/// never itself a dictionary that could have been composed away. That makes the work a single `if`
529/// rather than a loop: the inner vector was already composed when it was built, so composing the
530/// outer codes through it leaves the result no deeper than the inner vector already was.
531///
532/// The codes are indexed rather than fetched with `get`, because the caller has already walked the
533/// whole outer array to check that every code is in range and the inner array is exactly as long as
534/// the vector those codes were checked against.
535fn compose(codes: Vec<u32>, values: Vector) -> (Vec<u32>, Vector) {
536    // A dictionary carrying a validity of its own is one whose nulls live at this level rather than
537    // in the values, which is the one thing composition cannot carry down with it.
538    if !matches!(values.validity, Validity::AllValid) {
539        return (codes, values);
540    }
541    let Vector { ty, len, validity, body } = values;
542    match body {
543        Body::Dictionary { codes: inner, values: leaf } => {
544            debug_assert!(
545                !matches!(leaf.body, Body::Dictionary { .. })
546                    || !matches!(leaf.validity, Validity::AllValid),
547                "a dictionary was stacked on a dictionary without going through the constructor"
548            );
549            (codes.iter().map(|&code| inner[code as usize]).collect(), *leaf)
550        }
551        body => (codes, Vector { ty, len, validity, body }),
552    }
553}
554
555/// The position of a value that is not anywhere, because it is null or out of range.
556///
557/// `usize::MAX` rather than an `Option<usize>`, because the copy loop's bounds check rejects it for
558/// free and an `Option` would put a second branch next to the one already there.
559const NOWHERE: usize = usize::MAX;
560
561/// A run of data copied at the given positions, with a zero wherever the position is [`NOWHERE`].
562///
563/// A zero and not a skip, because every layout here is a parallel array to a validity mask and a
564/// short one would put every value after the first null at the wrong index. It is the same rule
565/// [`push_value`] follows for a null.
566fn copy_of(data: &Data, at: &[usize]) -> Data {
567    macro_rules! copied {
568        ($(($variant:ident, $native:ty, $zero:expr)),+ $(,)?) => {
569            match data {
570                Data::Empty => Data::Empty,
571                $(Data::$variant(values) => {
572                    let mut out = Buffer::with_capacity(at.len());
573                    for &index in at {
574                        // One bounds check rather than a null test and a bounds check, because
575                        // `NOWHERE` is past the end of every slice there can be.
576                        out.push(values.get(index).copied().unwrap_or($zero));
577                    }
578                    Data::$variant(out)
579                })+
580                // The one layout where a gather is a copy of bytes rather than a copy of fixed
581                // width slots, and the reason compaction is a decision rather than a default on a
582                // string column.
583                Data::Varlen(values) => {
584                    let mut out = StringColumn::with_capacity(at.len());
585                    // The bytes are known before any of them are copied, because a view carries its
586                    // length and the wanted positions are already in hand, so the arena is one
587                    // allocation rather than a run of doublings that each copy what the last one
588                    // copied.
589                    let views = values.views();
590                    out.reserve_bytes(
591                        at.iter()
592                            .filter_map(|&index| views.get(index))
593                            .filter(|view| !view.is_inline())
594                            .map(StringView::len)
595                            .sum(),
596                    );
597                    for &index in at {
598                        out.push(values.get(index).unwrap_or(""));
599                    }
600                    Data::Varlen(out)
601                }
602            }
603        };
604    }
605    crate::for_each_layout!(fixed, copied)
606}
607
608/// The physical layout a run of data is in, for the check that it matches its type.
609///
610/// The two enums name their variants the same way on purpose, so this is one generated arm rather
611/// than sixteen chances to pair the wrong two up.
612fn layout_of(data: &Data) -> rudb_common::PhysicalType {
613    use rudb_common::PhysicalType as P;
614    macro_rules! layouts {
615        ($(($variant:ident, $native:ty, $zero:expr)),+ $(,)?) => {
616            match data {
617                Data::Empty => P::Empty,
618                $(Data::$variant(_) => P::$variant,)+
619            }
620        };
621    }
622    crate::for_each_layout!(all, layouts)
623}
624
625/// One value out of a run of data, given what the run means.
626///
627/// The match is on the logical type rather than on the data, because the data cannot tell a `DATE`
628/// from an `INTEGER` and that is the whole reason the two are kept apart.
629fn value_from(ty: &LogicalType, data: &Data, index: usize) -> Value {
630    let signed = || data.signed_at(index);
631    let unsigned = || data.unsigned_at(index);
632    let value = match ty {
633        LogicalType::Boolean => match data {
634            Data::Bool(v) => v.get(index).map(|&x| Value::Boolean(x)),
635            _ => None,
636        },
637        LogicalType::TinyInt => signed().and_then(|x| i8::try_from(x).ok()).map(Value::TinyInt),
638        LogicalType::SmallInt => signed().and_then(|x| i16::try_from(x).ok()).map(Value::SmallInt),
639        LogicalType::Integer => signed().and_then(|x| i32::try_from(x).ok()).map(Value::Integer),
640        LogicalType::BigInt => signed().and_then(|x| i64::try_from(x).ok()).map(Value::BigInt),
641        LogicalType::HugeInt => signed().map(Value::HugeInt),
642        LogicalType::UTinyInt => unsigned().and_then(|x| u8::try_from(x).ok()).map(Value::UTinyInt),
643        LogicalType::USmallInt => {
644            unsigned().and_then(|x| u16::try_from(x).ok()).map(Value::USmallInt)
645        }
646        LogicalType::UInteger => {
647            unsigned().and_then(|x| u32::try_from(x).ok()).map(Value::UInteger)
648        }
649        LogicalType::UBigInt => unsigned().and_then(|x| u64::try_from(x).ok()).map(Value::UBigInt),
650        LogicalType::UHugeInt => unsigned().map(Value::UHugeInt),
651        LogicalType::Float => match data {
652            Data::Float32(v) => v.get(index).map(|&x| Value::Float(x)),
653            _ => None,
654        },
655        LogicalType::Double => match data {
656            Data::Float64(v) => v.get(index).map(|&x| Value::Double(x)),
657            _ => None,
658        },
659        LogicalType::Decimal { width, scale } => {
660            signed().map(|unscaled| Value::Decimal { unscaled, width: *width, scale: *scale })
661        }
662        LogicalType::Varchar => data.str_at(index).map(|s| Value::Varchar(s.to_string())),
663        LogicalType::Blob | LogicalType::Bit => {
664            data.str_at(index).map(|s| Value::Blob(s.as_bytes().to_vec()))
665        }
666        LogicalType::Date => signed().and_then(|x| i32::try_from(x).ok()).map(Value::Date),
667        LogicalType::Time | LogicalType::TimeTz => {
668            signed().and_then(|x| i64::try_from(x).ok()).map(Value::Time)
669        }
670        LogicalType::Timestamp
671        | LogicalType::TimestampS
672        | LogicalType::TimestampMs
673        | LogicalType::TimestampNs
674        | LogicalType::TimestampTz => {
675            signed().and_then(|x| i64::try_from(x).ok()).map(Value::Timestamp)
676        }
677        LogicalType::Interval => match data {
678            Data::Interval(v) => {
679                v.get(index).map(|&(months, days, micros)| Value::Interval { months, days, micros })
680            }
681            _ => None,
682        },
683        _ => None,
684    };
685    value.unwrap_or(Value::Null)
686}
687
688/// An empty run of data of the right layout for a type.
689fn empty_data_for(ty: &LogicalType) -> Result<Data> {
690    use rudb_common::PhysicalType as P;
691    macro_rules! empties {
692        ($(($variant:ident, $native:ty, $zero:expr)),+ $(,)?) => {
693            match ty.physical() {
694                P::Empty => Data::Empty,
695                $(P::$variant => Data::$variant(Buffer::new()),)+
696                P::Varlen => Data::Varlen(StringColumn::new()),
697                other => {
698                    return Err(Error::not_implemented(format!(
699                        "a flat vector of {other:?} data, which arrives with the storage layer"
700                    )));
701                }
702            }
703        };
704    }
705    Ok(crate::for_each_layout!(fixed, empties))
706}
707
708/// Appends one value to a run of data, or a zero of the right shape when it is null.
709///
710/// The zero matters. A null still occupies a position, the validity mask is what says it is null,
711/// and a run of data with a hole in it would put every value after the hole in the wrong place.
712fn push_value(data: &mut Data, value: &Value) -> Result<()> {
713    macro_rules! push {
714        ($vec:expr, $variant:path, $zero:expr) => {
715            match value {
716                Value::Null => $vec.push($zero),
717                $variant(x) => $vec.push(*x),
718                other => {
719                    return Err(Error::internal(format!(
720                        "{other:?} does not belong in this vector"
721                    )));
722                }
723            }
724        };
725    }
726    // A decimal is stored as its unscaled integer in whatever width its precision needs, which
727    // `LogicalType::physical` decides and which is why the same `Value::Decimal` is at home in four
728    // different runs. The narrowing cannot fail for a value the binder produced, because the width
729    // that chose the run is the width in the value, but it is checked rather than assumed because
730    // an unchecked cast here would silently store a different number.
731    macro_rules! decimal {
732        ($vec:expr, $ty:ty, $unscaled:expr) => {
733            match <$ty>::try_from(*$unscaled) {
734                Ok(x) => $vec.push(x),
735                Err(_) => {
736                    return Err(Error::internal(format!(
737                        "an unscaled decimal of {} does not fit the run its precision chose",
738                        $unscaled
739                    )));
740                }
741            }
742        };
743    }
744    match data {
745        Data::Empty => {}
746        Data::Bool(v) => push!(v, Value::Boolean, false),
747        Data::Int8(v) => push!(v, Value::TinyInt, 0),
748        Data::Int16(v) => match value {
749            Value::Null => v.push(0),
750            Value::SmallInt(x) => v.push(*x),
751            Value::Decimal { unscaled, .. } => decimal!(v, i16, unscaled),
752            other => return Err(Error::internal(format!("{other:?} is not a 16 bit value"))),
753        },
754        Data::Int32(v) => match value {
755            Value::Null => v.push(0),
756            Value::Integer(x) | Value::Date(x) => v.push(*x),
757            Value::Decimal { unscaled, .. } => decimal!(v, i32, unscaled),
758            other => return Err(Error::internal(format!("{other:?} is not a 32 bit value"))),
759        },
760        Data::Int64(v) => match value {
761            Value::Null => v.push(0),
762            Value::BigInt(x) | Value::Time(x) | Value::Timestamp(x) => v.push(*x),
763            Value::Decimal { unscaled, .. } => decimal!(v, i64, unscaled),
764            other => return Err(Error::internal(format!("{other:?} is not a 64 bit value"))),
765        },
766        Data::Int128(v) => match value {
767            Value::Null => v.push(0),
768            Value::HugeInt(x) => v.push(*x),
769            Value::Decimal { unscaled, .. } => v.push(*unscaled),
770            other => return Err(Error::internal(format!("{other:?} is not a 128 bit value"))),
771        },
772        Data::UInt8(v) => push!(v, Value::UTinyInt, 0),
773        Data::UInt16(v) => push!(v, Value::USmallInt, 0),
774        Data::UInt32(v) => push!(v, Value::UInteger, 0),
775        Data::UInt64(v) => push!(v, Value::UBigInt, 0),
776        Data::UInt128(v) => push!(v, Value::UHugeInt, 0),
777        Data::Float32(v) => push!(v, Value::Float, 0.0),
778        Data::Float64(v) => push!(v, Value::Double, 0.0),
779        Data::Interval(v) => match value {
780            Value::Null => v.push((0, 0, 0)),
781            Value::Interval { months, days, micros } => v.push((*months, *days, *micros)),
782            other => return Err(Error::internal(format!("{other:?} is not an interval"))),
783        },
784        Data::Varlen(column) => match value {
785            Value::Null => {
786                column.push("");
787            }
788            Value::Varchar(text) => {
789                column.push(text);
790            }
791            // A blob is bytes and this column is text, so the only blobs that survive a round trip
792            // here are the ones that happen to be valid UTF-8. Real blob storage is a byte column
793            // and it arrives with the storage layer at M2 rather than being faked now.
794            Value::Blob(bytes) => match std::str::from_utf8(bytes) {
795                Ok(text) => {
796                    column.push(text);
797                }
798                Err(_) => {
799                    return Err(Error::not_implemented(
800                        "a blob that is not valid UTF-8, which needs the byte column from M2",
801                    ));
802                }
803            },
804            other => return Err(Error::internal(format!("{other:?} is not a string"))),
805        },
806    }
807    Ok(())
808}
809
810#[cfg(test)]
811mod tests {
812    use rudb_common::{LogicalType, Value};
813
814    use super::{Data, Form, VECTOR_SIZE, Vector};
815    use crate::string::StringColumn;
816    use crate::validity::Validity;
817
818    fn integers(values: &[i32]) -> Vector {
819        Vector::flat(LogicalType::Integer, Data::Int32(values.to_vec().into())).unwrap()
820    }
821
822    #[test]
823    fn the_vector_size_is_the_one_the_design_is_built_around() {
824        // 1024 and not DuckDB's 2048. A validity mask is 16 u64 words and a vector of string views
825        // is 16 KiB, both of which are consequences of this number rather than coincidences.
826        assert_eq!(VECTOR_SIZE, 1024);
827        assert_eq!(VECTOR_SIZE / 64, 16);
828    }
829
830    #[test]
831    fn a_flat_vector_reads_back_what_was_put_in_it() {
832        let vector = integers(&[1, 2, 3]);
833        assert_eq!(vector.form(), Form::Flat);
834        assert_eq!(vector.len(), 3);
835        assert_eq!(vector.value_at(1), Value::Integer(2));
836        assert_eq!(
837            vector.iter().collect::<Vec<_>>(),
838            vec![Value::Integer(1), Value::Integer(2), Value::Integer(3)]
839        );
840    }
841
842    #[test]
843    fn a_vector_built_from_values_reads_the_same_values_back() {
844        let vector = Vector::from_values(
845            LogicalType::Varchar,
846            &[
847                Value::Varchar("a".to_string()),
848                Value::Null,
849                Value::Varchar("a string too long to sit inside a view".to_string()),
850            ],
851        )
852        .expect("strings and a null");
853        assert_eq!(vector.len(), 3);
854        assert_eq!(vector.value_at(0), Value::Varchar("a".to_string()));
855        assert_eq!(vector.value_at(1), Value::Null);
856        assert_eq!(
857            vector.value_at(2),
858            Value::Varchar("a string too long to sit inside a view".to_string())
859        );
860    }
861
862    /// A null still occupies a position. If it did not then every value after it would read back
863    /// one place to the left, which is the kind of bug that looks like a storage bug for a week.
864    #[test]
865    fn a_null_in_the_middle_does_not_move_the_values_after_it() {
866        let vector = Vector::from_values(
867            LogicalType::Integer,
868            &[Value::Integer(1), Value::Null, Value::Integer(3)],
869        )
870        .expect("integers and a null");
871        assert_eq!(vector.value_at(2), Value::Integer(3));
872        assert!(vector.validity().has_nulls(3), "the middle one is null");
873    }
874
875    #[test]
876    fn a_value_the_type_cannot_hold_is_refused() {
877        let wrong = Vector::from_values(LogicalType::Integer, &[Value::Varchar("x".to_string())]);
878        assert!(wrong.is_err(), "a string is not an integer");
879    }
880
881    #[test]
882    fn a_type_that_does_not_match_its_layout_is_refused_at_construction() {
883        // One comparison here against a wrong answer read out three layers later.
884        let wrong = Vector::flat(LogicalType::Varchar, Data::Int32(vec![1].into()));
885        assert!(wrong.is_err());
886        let right = Vector::flat(LogicalType::Date, Data::Int32(vec![1].into()));
887        assert!(right.is_ok(), "a date is stored in an i32 and that has to be allowed");
888    }
889
890    #[test]
891    fn a_constant_vector_costs_one_value_whatever_its_length() {
892        let vector = Vector::constant(LogicalType::Integer, Value::Integer(7), VECTOR_SIZE);
893        assert_eq!(vector.form(), Form::Constant);
894        assert_eq!(vector.len(), VECTOR_SIZE);
895        assert_eq!(vector.value_at(0), Value::Integer(7));
896        assert_eq!(vector.value_at(VECTOR_SIZE - 1), Value::Integer(7));
897        assert_eq!(vector.value_at(VECTOR_SIZE), Value::Null, "past the end is null, not a panic");
898    }
899
900    #[test]
901    fn a_constant_null_is_all_invalid_without_being_told() {
902        let vector = Vector::constant(LogicalType::Integer, Value::Null, 8);
903        assert_eq!(vector.validity(), &Validity::AllInvalid);
904        assert_eq!(vector.value_at(3), Value::Null);
905    }
906
907    #[test]
908    fn a_sequence_vector_is_sixteen_bytes_of_row_identifiers() {
909        let vector = Vector::sequence(100, 1, VECTOR_SIZE);
910        assert_eq!(vector.form(), Form::Sequence);
911        assert_eq!(vector.value_at(0), Value::BigInt(100));
912        assert_eq!(vector.value_at(923), Value::BigInt(1023));
913        let stepped = Vector::sequence(0, 5, 4);
914        assert_eq!(
915            stepped.iter().collect::<Vec<_>>(),
916            vec![Value::BigInt(0), Value::BigInt(5), Value::BigInt(10), Value::BigInt(15)]
917        );
918    }
919
920    #[test]
921    fn a_dictionary_vector_reads_through_its_codes() {
922        let mut column = StringColumn::new();
923        column.push("red");
924        column.push("green");
925        let values = Vector::flat(LogicalType::Varchar, Data::Varlen(column)).unwrap();
926        let vector = Vector::dictionary(vec![0, 1, 1, 0], values).unwrap();
927        assert_eq!(vector.form(), Form::Dictionary);
928        assert_eq!(vector.logical_type(), &LogicalType::Varchar);
929        assert_eq!(vector.value_at(2), Value::Varchar("green".into()));
930        assert_eq!(vector.len(), 4);
931    }
932
933    #[test]
934    fn a_dictionary_code_past_the_end_is_refused() {
935        // The alternative is a silent read of the wrong value, which is the failure mode the
936        // entire M3 design has to be careful about.
937        let values = integers(&[1, 2]);
938        assert!(Vector::dictionary(vec![0, 2], values).is_err());
939    }
940
941    #[test]
942    fn every_form_flattens_to_the_same_values_it_reads_out() {
943        // This is the shape of the equivalence testing in spec/16-testing.md section 16.2, in
944        // miniature and long before there is an encoded kernel to point it at. A form that reads
945        // out one way and flattens another is the exact bug that testing exists to catch.
946        let mut column = StringColumn::new();
947        column.push("alpha");
948        column.push("beta");
949        let dictionary = Vector::dictionary(
950            vec![1, 0, 1],
951            Vector::flat(LogicalType::Varchar, Data::Varlen(column)).unwrap(),
952        )
953        .unwrap();
954        let cases = [
955            Vector::constant(LogicalType::Integer, Value::Integer(3), 5),
956            Vector::sequence(7, -2, 5),
957            dictionary,
958        ];
959        for vector in cases {
960            let flat = vector.flatten().unwrap();
961            assert_eq!(flat.form(), Form::Flat);
962            assert_eq!(flat.len(), vector.len());
963            for index in 0..vector.len() {
964                assert_eq!(flat.value_at(index), vector.value_at(index), "at {index}");
965            }
966        }
967    }
968
969    #[test]
970    fn a_null_still_occupies_a_position_after_flattening() {
971        // The reason push_value writes a zero for a null rather than skipping it. A run of data
972        // with a hole in it puts every value after the hole in the wrong place, and the validity
973        // mask is what says the position is null.
974        let vector = Vector::sequence(0, 1, 4).with_validity(Validity::from_iter(4, |i| i != 1));
975        let flat = vector.flatten().unwrap();
976        assert_eq!(flat.value_at(0), Value::BigInt(0));
977        assert_eq!(flat.value_at(1), Value::Null);
978        assert_eq!(flat.value_at(2), Value::BigInt(2));
979        assert_eq!(flat.value_at(3), Value::BigInt(3));
980    }
981
982    /// A dictionary holds its nulls in the vector it points at, so its own validity is all valid
983    /// and reading that instead of the values turns a null into whatever zero means for the type.
984    /// A filter over a nullable column produces exactly this vector, so the bug reaches a result
985    /// set as `LEFT JOIN` padding that comes back as zeros.
986    #[test]
987    fn a_null_behind_a_dictionary_survives_flattening() {
988        let values =
989            Vector::from_values(LogicalType::Integer, &[Value::Integer(3), Value::Null]).unwrap();
990        let dictionary = Vector::dictionary(vec![1, 0, 1], values).unwrap();
991        let flat = dictionary.flatten().unwrap();
992        assert_eq!(flat.value_at(0), Value::Null);
993        assert_eq!(flat.value_at(1), Value::Integer(3));
994        assert_eq!(flat.value_at(2), Value::Null);
995    }
996
997    /// The property that makes `gather` usable at all: it has to be the same function as reading the
998    /// wanted positions one at a time, over every form, or compaction changes answers.
999    #[test]
1000    fn gathering_reads_what_reading_one_position_at_a_time_reads() {
1001        let mut column = StringColumn::new();
1002        column.push("alpha");
1003        column.push("beta");
1004        column.push("gamma");
1005        let cases = [
1006            integers(&[10, 20, 30, 40]),
1007            integers(&[10, 20, 30, 40]).with_validity(Validity::from_iter(4, |i| i != 2)),
1008            Vector::constant(LogicalType::Integer, Value::Integer(9), 4),
1009            Vector::sequence(100, -7, 4),
1010            Vector::sequence(100, -7, 4).with_validity(Validity::from_iter(4, |i| i % 2 == 0)),
1011            Vector::dictionary(
1012                vec![2, 0, 1, 2],
1013                Vector::flat(LogicalType::Varchar, Data::Varlen(column)).unwrap(),
1014            )
1015            .unwrap(),
1016            Vector::dictionary(
1017                vec![1, 0, 1, 0],
1018                Vector::from_values(LogicalType::Integer, &[Value::Integer(5), Value::Null])
1019                    .unwrap(),
1020            )
1021            .unwrap(),
1022        ];
1023        let wanted = [3_u32, 0, 2, 2, 1];
1024        for vector in cases {
1025            let gathered = vector.gather(&wanted).unwrap();
1026            assert_eq!(gathered.len(), wanted.len());
1027            assert_eq!(gathered.logical_type(), vector.logical_type());
1028            for (slot, &index) in wanted.iter().enumerate() {
1029                assert_eq!(
1030                    gathered.value_at(slot),
1031                    vector.value_at(index as usize),
1032                    "slot {slot} of {:?}",
1033                    vector.form()
1034                );
1035            }
1036        }
1037    }
1038
1039    /// A gather past the end is not an error, because the selection that produced the indices is
1040    /// checked by its caller and the one thing that must not happen here is a read of the wrong
1041    /// value. An index nothing answers is null, which is what an outer join pad needs anyway.
1042    #[test]
1043    fn gathering_a_position_that_is_not_there_is_a_null_and_not_a_wrong_value() {
1044        let vector = integers(&[1, 2, 3]);
1045        let gathered = vector.gather(&[2, 9]).unwrap();
1046        assert_eq!(gathered.value_at(0), Value::Integer(3));
1047        assert_eq!(gathered.value_at(1), Value::Null);
1048    }
1049
1050    /// The vector with nothing in it at all, which is what an untyped `NULL` is stored as. Every
1051    /// position asked for is past its end, so the answer is nulls and the length has to be the
1052    /// length that was asked for rather than the length that was there.
1053    #[test]
1054    fn gathering_from_a_vector_of_no_values_is_that_many_nulls() {
1055        let vector = Vector::flat(LogicalType::Null, Data::Empty).unwrap();
1056        let gathered = vector.gather(&[0, 1, 2]).unwrap();
1057        assert_eq!(gathered.len(), 3);
1058        assert_eq!(gathered.value_at(0), Value::Null);
1059        assert_eq!(gathered.value_at(2), Value::Null);
1060    }
1061
1062    /// Every position holds the same value, so a gather with no hole in it has nothing to copy and
1063    /// the result is the constant again rather than a run of a thousand copies of it.
1064    #[test]
1065    fn gathering_a_constant_stays_a_constant() {
1066        let vector = Vector::constant(LogicalType::Integer, Value::Integer(4), 100);
1067        let gathered = vector.gather(&[7, 7, 99]).unwrap();
1068        assert_eq!(gathered.form(), Form::Constant);
1069        assert_eq!(gathered.len(), 3);
1070        assert_eq!(gathered.value_at(2), Value::Integer(4));
1071    }
1072
1073    /// A dictionary over a dictionary is what a second filter over an already filtered chunk builds,
1074    /// and the gather has to walk to the bottom of that chain rather than one step down it. The
1075    /// constructor composes the ordinary chain away, so the one built here is the kind it cannot,
1076    /// which is a level holding nulls of its own.
1077    #[test]
1078    fn gathering_walks_a_dictionary_over_a_dictionary_to_the_values() {
1079        let inner = Vector::dictionary(vec![2, 1, 0], integers(&[7, 8, 9]))
1080            .unwrap()
1081            .with_validity(Validity::from_iter(3, |index| index != 2));
1082        let outer = Vector::dictionary(vec![1, 2], inner).unwrap();
1083        let gathered = outer.gather(&[0, 1]).unwrap();
1084        assert_eq!(gathered.form(), Form::Flat);
1085        assert_eq!(gathered.value_at(0), Value::Integer(8));
1086        assert_eq!(gathered.value_at(1), Value::Null);
1087    }
1088
1089    /// Two filters over one chunk build a dictionary over a dictionary, four conjuncts pushed down
1090    /// separately build four levels of it, and every level is a dependent load on every later read
1091    /// of every row plus a code array that cannot be freed. Composing at construction is one pass
1092    /// over the codes the range check was walking anyway.
1093    #[test]
1094    fn a_dictionary_over_a_dictionary_is_composed_into_one_level() {
1095        let inner = Vector::dictionary(vec![2, 1, 0], integers(&[7, 8, 9])).unwrap();
1096        let outer = Vector::dictionary(vec![1, 2], inner).unwrap();
1097        let (codes, values) = outer.dictionary_parts().unwrap();
1098        assert_eq!(codes, [1, 0]);
1099        assert_eq!(values.form(), Form::Flat);
1100        assert_eq!(outer.value_at(0), Value::Integer(8));
1101        assert_eq!(outer.value_at(1), Value::Integer(7));
1102    }
1103
1104    /// The invariant stated as the thing it is there for, which is that the depth does not grow with
1105    /// the number of filters. Four levels stacked one at a time are one level at the end of it.
1106    #[test]
1107    fn stacking_dictionaries_does_not_make_them_deeper() {
1108        let mut vector = integers(&[10, 20, 30, 40]);
1109        for _ in 0..4 {
1110            vector = Vector::dictionary(vec![3, 2, 1, 0], vector).unwrap();
1111        }
1112        let (codes, values) = vector.dictionary_parts().unwrap();
1113        assert_eq!(values.form(), Form::Flat);
1114        assert_eq!(codes, [0, 1, 2, 3]);
1115        assert_eq!(
1116            vector.iter().collect::<Vec<_>>(),
1117            integers(&[10, 20, 30, 40]).iter().collect::<Vec<_>>()
1118        );
1119    }
1120
1121    /// Composing has to carry the nulls down with it. The values hold them, the codes point at them,
1122    /// and a composed code that lands on a null position is still a null.
1123    #[test]
1124    fn composing_a_dictionary_keeps_the_nulls_its_values_hold() {
1125        let values =
1126            Vector::from_values(LogicalType::Integer, &[Value::Integer(3), Value::Null]).unwrap();
1127        let inner = Vector::dictionary(vec![1, 0, 1], values).unwrap();
1128        let outer = Vector::dictionary(vec![0, 1], inner).unwrap();
1129        assert_eq!(outer.dictionary_parts().unwrap().1.form(), Form::Flat);
1130        assert_eq!(outer.value_at(0), Value::Null);
1131        assert_eq!(outer.value_at(1), Value::Integer(3));
1132    }
1133
1134    /// The one level composition cannot go past. A dictionary that was given a validity of its own is
1135    /// saying its nulls are at that level rather than in the values, and pointing the outer codes
1136    /// straight at the values would read through the holes instead of stopping at them.
1137    #[test]
1138    fn a_dictionary_holding_its_own_nulls_is_not_composed_past() {
1139        let inner = Vector::dictionary(vec![0, 1, 2], integers(&[1, 2, 3]))
1140            .unwrap()
1141            .with_validity(Validity::from_iter(3, |index| index != 1));
1142        let outer = Vector::dictionary(vec![1, 2, 0], inner).unwrap();
1143        assert_eq!(outer.dictionary_parts().unwrap().1.form(), Form::Dictionary);
1144        assert_eq!(outer.value_at(0), Value::Null);
1145        assert_eq!(outer.value_at(1), Value::Integer(3));
1146        assert_eq!(outer.value_at(2), Value::Integer(1));
1147    }
1148
1149    #[test]
1150    fn flattening_a_flat_vector_is_the_same_vector() {
1151        let vector = integers(&[1, 2, 3]);
1152        assert_eq!(vector.flatten().unwrap(), vector);
1153    }
1154
1155    #[test]
1156    fn a_decimal_reads_its_width_and_scale_from_the_type_and_not_the_data() {
1157        let ty = LogicalType::decimal(9, 2).unwrap();
1158        let vector = Vector::flat(ty, Data::Int32(vec![1234].into())).unwrap();
1159        assert_eq!(vector.value_at(0), Value::Decimal { unscaled: 1234, width: 9, scale: 2 });
1160        assert_eq!(vector.value_at(0).to_string(), "12.34");
1161    }
1162
1163    #[test]
1164    fn a_decimal_writes_into_whichever_of_the_four_runs_its_precision_chose() {
1165        // The read path worked at every width and the write path only accepted the 128 bit run, so
1166        // `SELECT 2.5` produced a value nothing could store. All four widths round trip now.
1167        for (width, scale, unscaled) in
1168            [(4u8, 1u8, 25i128), (9, 2, 1234), (18, 3, 123_456), (38, 4, 1_234_567)]
1169        {
1170            let ty = LogicalType::decimal(width, scale).unwrap();
1171            let value = Value::Decimal { unscaled, width, scale };
1172            let vector = Vector::from_values(ty, &[value.clone(), Value::Null]).unwrap();
1173            assert_eq!(vector.value_at(0), value, "a decimal of width {width}");
1174            assert_eq!(vector.value_at(1), Value::Null, "a null decimal of width {width}");
1175        }
1176    }
1177
1178    #[test]
1179    fn a_decimal_too_wide_for_the_run_its_type_chose_is_an_error_and_not_a_wrong_number() {
1180        // Only reachable by hand, since a value's width is what picked the run. Truncating here
1181        // would store a different number and say nothing about it.
1182        let ty = LogicalType::decimal(4, 1).unwrap();
1183        let value = Value::Decimal { unscaled: 1_000_000, width: 4, scale: 1 };
1184        let error = Vector::from_values(ty, &[value]).unwrap_err();
1185        assert!(error.to_string().contains("does not fit"), "{error}");
1186    }
1187}