rudb_vector/chunk.rs
1//! A batch of columns, which is the unit every operator passes to the next one.
2//!
3//! A chunk is some vectors of the same length plus that length. It is not a table and it is not a
4//! result set: it is at most [`VECTOR_SIZE`] rows, because the whole point of the number in
5//! `spec/04-architecture.md` section 4.3 is that a batch of this width stays in L1 while an
6//! operator works on it, and a type that can hold ten times that many rows is a type that lets an
7//! operator quietly stop being vectorized.
8//!
9//! The row count is stored rather than derived, which matters for the one case that looks like a
10//! mistake and is not. `SELECT count(*) FROM t` scans no columns, so the chunk the scan produces
11//! has no vectors in it and still has to say how many rows went past, and a chunk that derived its
12//! length from its first column would say zero.
13
14use rudb_common::{Error, LogicalType, Result, Value};
15
16use crate::selection::Selection;
17use crate::vector::{VECTOR_SIZE, Vector};
18
19/// A batch of columns of equal length.
20#[derive(Debug, Clone, PartialEq)]
21pub struct Chunk {
22 columns: Vec<Vector>,
23 rows: usize,
24}
25
26/// The scheduler's half of the data plane contract, imposed now rather than at layer eight.
27///
28/// `spec/engine/03-data-plane.md` section 3.10. A chunk is what one thread hands another, so a chunk
29/// is `Send`, and that is not free: it rules out an `Rc` anywhere in a vector, it rules out a borrow
30/// of thread local state, and it is what the pin handle in [`Buffer`](crate::Buffer) is protecting
31/// against a lifetime parameter.
32///
33/// It is a static assertion rather than a comment because the failure mode is quiet. Every one of
34/// those mistakes compiles perfectly well on its own and is only a problem the day a chunk is put in
35/// a queue, which is eight layers from here and far too late to be told. This way the build breaks
36/// on the commit that introduces it.
37const _: () = {
38 const fn assert_send<T: Send>() {}
39 assert_send::<Chunk>();
40};
41
42impl Chunk {
43 /// A chunk of `columns`, taking the row count from the first of them.
44 ///
45 /// # Errors
46 ///
47 /// If the columns are not all the same length, or if there are more rows than [`VECTOR_SIZE`].
48 pub fn new(columns: Vec<Vector>) -> Result<Self> {
49 let rows = columns.first().map_or(0, Vector::len);
50 Self::with_rows(columns, rows)
51 }
52
53 /// A chunk of `columns` that is `rows` long, for the case where there are no columns to take
54 /// the count from.
55 ///
56 /// # Errors
57 ///
58 /// If any column is not `rows` long, or if `rows` is more than [`VECTOR_SIZE`].
59 pub fn with_rows(columns: Vec<Vector>, rows: usize) -> Result<Self> {
60 if rows > VECTOR_SIZE {
61 return Err(Error::internal(format!(
62 "a chunk of {rows} rows is longer than the {VECTOR_SIZE} row vector"
63 )));
64 }
65 for (index, column) in columns.iter().enumerate() {
66 if column.len() != rows {
67 return Err(Error::internal(format!(
68 "column {index} of a chunk is {} rows and the chunk is {rows}",
69 column.len()
70 )));
71 }
72 }
73 Ok(Self { columns, rows })
74 }
75
76 /// A chunk of the given types with no rows in it.
77 ///
78 /// What a scan of an empty table returns and what an operator returns when it is done. The
79 /// types are kept, because a consumer asks a chunk what its columns are before it asks whether
80 /// there are any.
81 #[must_use]
82 pub fn empty(types: &[LogicalType]) -> Self {
83 let columns =
84 types.iter().map(|ty| Vector::constant(ty.clone(), Value::Null, 0)).collect::<Vec<_>>();
85 Self { columns, rows: 0 }
86 }
87
88 /// The columns.
89 #[must_use]
90 pub fn columns(&self) -> &[Vector] {
91 &self.columns
92 }
93
94 /// One column.
95 ///
96 /// # Errors
97 ///
98 /// If there is no column at `index`.
99 pub fn column(&self, index: usize) -> Result<&Vector> {
100 self.columns.get(index).ok_or_else(|| {
101 Error::internal(format!(
102 "column {index} of a chunk that has {} columns",
103 self.columns.len()
104 ))
105 })
106 }
107
108 /// The columns, given up.
109 #[must_use]
110 pub fn into_columns(self) -> Vec<Vector> {
111 self.columns
112 }
113
114 /// How many columns.
115 #[must_use]
116 pub fn width(&self) -> usize {
117 self.columns.len()
118 }
119
120 /// How many rows.
121 #[must_use]
122 pub fn len(&self) -> usize {
123 self.rows
124 }
125
126 /// Whether there are no rows.
127 #[must_use]
128 pub fn is_empty(&self) -> bool {
129 self.rows == 0
130 }
131
132 /// How many bytes of memory this chunk is holding.
133 ///
134 /// What the memory limit charges for a chunk somebody kept. A chunk handed from one operator to
135 /// the next and dropped is not charged at all, because charging it would count the same
136 /// megabyte once per level of the tree, and the levels of the tree are not where a query runs
137 /// out of memory.
138 ///
139 /// Every size in this workspace counts the thing itself as well as what it owns, so a column's
140 /// own bytes are already in its own number and are not added again here.
141 #[must_use]
142 pub fn footprint(&self) -> usize {
143 size_of::<Self>() + self.columns.iter().map(Vector::footprint).sum::<usize>()
144 }
145
146 /// This chunk with every column's payload held as a page, so that a copy of it is free.
147 ///
148 /// For a chunk that is going to be stored and handed out many times, which is what an in memory
149 /// table's chunks are. See [`Vector::into_pages`] for what it does to each form.
150 #[must_use]
151 pub fn into_pages(self) -> Self {
152 Self {
153 columns: self.columns.into_iter().map(Vector::into_pages).collect(),
154 rows: self.rows,
155 }
156 }
157
158 /// The type of each column.
159 #[must_use]
160 pub fn types(&self) -> Vec<LogicalType> {
161 self.columns.iter().map(|column| column.logical_type().clone()).collect()
162 }
163
164 /// Validate every storage-backed value reachable from this chunk.
165 pub fn validate_external(&self) -> Result<()> {
166 self.columns.iter().try_for_each(Vector::validate_external)
167 }
168
169 /// The value at a row and a column, or null if either is past the end.
170 ///
171 /// The slow path, same as [`Vector::value_at`]. It is what a result set is read out with and
172 /// what a test asserts on.
173 #[must_use]
174 pub fn value_at(&self, row: usize, column: usize) -> Value {
175 match self.columns.get(column) {
176 Some(held) => held.value_at(row),
177 None => Value::Null,
178 }
179 }
180
181 /// The value at a row and column, preserving storage read and validation failures.
182 pub fn try_value_at(&self, row: usize, column: usize) -> Result<Value> {
183 match self.columns.get(column) {
184 Some(held) => held.try_value_at(row),
185 None => Ok(Value::Null),
186 }
187 }
188
189 /// One row, left to right.
190 pub fn row(&self, row: usize) -> impl Iterator<Item = Value> + '_ {
191 self.columns.iter().map(move |column| column.value_at(row))
192 }
193
194 /// The rows a selection kept, without moving any of the values.
195 ///
196 /// Every column becomes a dictionary vector whose codes are the selection, which is the form
197 /// `spec/07-execution.md` section 7.1 asks a filter to produce rather than compacting. It takes
198 /// the chunk by value because that is what makes it free: the payload is moved into the new
199 /// vector rather than copied, so a filter that keeps one row in a thousand still costs the
200 /// selection and nothing else.
201 ///
202 /// A column that is already a stable dictionary is the one exception, and it composes the two
203 /// levels of codes instead of stacking them. Stacking is just as cheap here and it hides the
204 /// thing that matters: a stable dictionary is a promise that codes from separate chunks name the
205 /// same values, and the aggregate, the group key store and the string kernels all read that
206 /// promise off the outermost body. Wrapping it in a second dictionary breaks the promise, so a
207 /// `GROUP BY SearchPhrase` behind a `WHERE SearchPhrase <> ''` fell off the code path and hashed
208 /// strings instead, which measured at 30 ms of processor time against 4 ms for the same group by
209 /// with nothing in front of it. Composing costs one lookup per kept row and keeps the promise.
210 ///
211 /// # Errors
212 ///
213 /// If the selection points past the end of the chunk.
214 pub fn select(self, selection: &Selection) -> Result<Self> {
215 if let Some(bad) = selection.iter().find(|&index| index >= self.rows) {
216 return Err(Error::internal(format!(
217 "a selection keeps row {bad} of a chunk that has {} rows",
218 self.rows
219 )));
220 }
221 let rows = selection.len();
222 let codes = selection.indices();
223 let mut columns = Vec::with_capacity(self.columns.len());
224 for column in self.columns {
225 if column.stable_dictionary_parts().is_some() {
226 columns.push(column.gather(codes)?);
227 } else {
228 columns.push(Vector::dictionary(codes.to_vec(), column)?);
229 }
230 }
231 Self::with_rows(columns, rows)
232 }
233
234 /// The rows a selection kept, copied, so that nothing downstream reads through an indirection.
235 ///
236 /// The copying counterpart to [`Self::select`], and the two exist because neither one is right
237 /// twice. Which one to call is measured rather than argued, and the measurement says something
238 /// other than what the argument does, so here is both.
239 ///
240 /// The argument is that selecting pays nothing now and one redirection on every later read of
241 /// every kept row, while compacting pays a copy now and nothing afterwards, so the deciding
242 /// variable is selectivity: keep a few rows and select, keep most of them and compact. The
243 /// measurement says the deciding variable is not selectivity at all, it is how many times the
244 /// rows are read again afterwards, and selectivity barely moves the line. On server3, over a
245 /// chunk of two integer columns, compacting loses to selecting at every selectivity from one
246 /// percent to a hundred when there is one later pass over the kept rows, and beats it at every
247 /// selectivity from one percent to a hundred when there are sixteen. With four later passes the
248 /// two are within a few percent of each other everywhere. Put a varchar column in the chunk and
249 /// compaction loses almost everywhere, because copying string bytes is most of what it costs and
250 /// the dictionary it avoids is most of what it saves.
251 ///
252 /// Which is why nothing in the streaming pipeline calls this yet. A filter today feeds an
253 /// aggregate or a projection and that is one pass or two, and end to end on two million rows
254 /// `SELECT sum(a), sum(b), count(*) FROM t WHERE a > ?` measures the same either way at one
255 /// percent selectivity and fifty percent slower compacting at fifty percent selectivity. The
256 /// operators that will want this are the ones that hold chunks rather than pass them on, the
257 /// hash join build side and the sort, because a chunk that is kept alive as a selection keeps
258 /// the whole chunk it was selected from alive with it, and that is a hundred to one on memory
259 /// rather than a few percent on time.
260 ///
261 /// Takes the chunk by value like [`Self::select`] does, even though the payload is copied rather
262 /// than moved, because a caller that still wanted the original after compacting it would be
263 /// holding both copies and should say so.
264 ///
265 /// # Errors
266 ///
267 /// If the selection points past the end of the chunk, or if a column has a type there is no
268 /// vector for, which today means `ARRAY` and `UNION`.
269 pub fn compact(self, selection: &Selection) -> Result<Self> {
270 if let Some(bad) = selection.iter().find(|&index| index >= self.rows) {
271 return Err(Error::internal(format!(
272 "a selection keeps row {bad} of a chunk that has {} rows",
273 self.rows
274 )));
275 }
276 let rows = selection.len();
277 let indices = selection.indices();
278 let mut columns = Vec::with_capacity(self.columns.len());
279 for column in &self.columns {
280 columns.push(column.gather(indices)?);
281 }
282 Self::with_rows(columns, rows)
283 }
284
285 /// The columns at the given positions, in that order.
286 ///
287 /// A position may appear twice, which is what `SELECT x, x FROM t` is, and the second one costs
288 /// a copy. Every other position is moved.
289 ///
290 /// # Errors
291 ///
292 /// If a position is past the end of the chunk.
293 pub fn project(self, positions: &[usize]) -> Result<Self> {
294 let width = self.columns.len();
295 if let Some(&bad) = positions.iter().find(|&&position| position >= width) {
296 return Err(Error::internal(format!(
297 "column {bad} of a chunk that has {width} columns"
298 )));
299 }
300 let rows = self.rows;
301 let mut sources: Vec<Option<Vector>> = self.columns.into_iter().map(Some).collect();
302 let mut columns = Vec::with_capacity(positions.len());
303 for (at, &position) in positions.iter().enumerate() {
304 let last_use = !positions[at + 1..].contains(&position);
305 let taken = if last_use { sources[position].take() } else { sources[position].clone() };
306 match taken {
307 Some(column) => columns.push(column),
308 // Only reachable if the last-use bookkeeping above is wrong, since a position is
309 // taken on its last appearance and cloned on every earlier one.
310 None => {
311 return Err(Error::internal(format!("column {position} was taken twice")));
312 }
313 }
314 }
315 Self::with_rows(columns, rows)
316 }
317
318 /// The same rows with every column in flat form.
319 ///
320 /// Costs a copy per column that was not already flat. It is here for the result set at the top
321 /// of a query, where the dictionary vectors a filter left behind would otherwise be handed to a
322 /// caller who has to understand them.
323 ///
324 /// # Errors
325 ///
326 /// If a column has a type there is no vector for, which today means `ARRAY` and `UNION`. A `LIST`
327 /// and a `MAP` flatten to themselves and a `STRUCT` to a struct of flattened fields, since none of
328 /// the three has a data slice for a caller to read and there is nothing flatter to become.
329 pub fn flatten(&self) -> Result<Self> {
330 let mut columns = Vec::with_capacity(self.columns.len());
331 for column in &self.columns {
332 // flatten: this is the chunk wide version of the vector call and it exists so that the
333 // one caller at the top of a query can say it once instead of per column. Whether the
334 // copy is deserved is decided where this is called from, which today is one line in
335 // `rudb::database`, and that line says why.
336 columns.push(column.flatten()?);
337 }
338 Self::with_rows(columns, self.rows)
339 }
340
341 /// The same rows in flat form, taking the chunk rather than borrowing it.
342 ///
343 /// The same answer [`Self::flatten`] gives and it costs less for the column that is already
344 /// flat, which is most of them: that column is moved out of this chunk and into the new one
345 /// rather than copied. Borrowing had no way to do that, so flattening a chunk of four flat
346 /// columns of eight thousand rows copied every value for nothing, and at the top of a query of
347 /// six million rows that was a hundred and sixty megabytes copied to produce the bytes it
348 /// already had.
349 ///
350 /// # Errors
351 ///
352 /// The same as [`Self::flatten`].
353 pub fn into_flat(self) -> Result<Self> {
354 let rows = self.rows;
355 let mut columns = Vec::with_capacity(self.columns.len());
356 for column in self.columns {
357 columns.push(column.into_flat()?);
358 }
359 Self::with_rows(columns, rows)
360 }
361}
362
363#[cfg(test)]
364mod tests {
365 use std::sync::Arc;
366
367 use rudb_common::LogicalType;
368
369 use super::*;
370 use crate::vector::{Data, Form};
371
372 fn integers(values: &[i32]) -> Vector {
373 Vector::flat(LogicalType::Integer, Data::Int32(values.to_vec().into()))
374 .expect("integers are an i32 layout")
375 }
376
377 #[test]
378 fn a_chunk_takes_its_length_from_its_columns() {
379 let chunk = Chunk::new(vec![integers(&[1, 2, 3]), integers(&[4, 5, 6])])
380 .expect("two columns of three");
381 assert_eq!(chunk.len(), 3);
382 assert_eq!(chunk.width(), 2);
383 assert_eq!(chunk.value_at(2, 1), Value::Integer(6));
384 }
385
386 #[test]
387 fn a_ragged_chunk_is_caught() {
388 let error = Chunk::new(vec![integers(&[1, 2, 3]), integers(&[4])])
389 .expect_err("a chunk is not ragged");
390 assert!(error.message().contains("column 1"), "{error}");
391 }
392
393 /// `SELECT count(*) FROM t` scans no columns and the row count still has to survive, which is
394 /// the reason the length is a field rather than the first column's length.
395 #[test]
396 fn a_chunk_with_no_columns_can_still_have_rows() {
397 let chunk = Chunk::with_rows(Vec::new(), 900).expect("no columns and nine hundred rows");
398 assert_eq!(chunk.len(), 900);
399 assert_eq!(chunk.width(), 0);
400 assert!(!chunk.is_empty(), "nine hundred rows is not empty");
401 }
402
403 #[test]
404 fn a_chunk_longer_than_a_vector_is_caught() {
405 let error = Chunk::with_rows(Vec::new(), VECTOR_SIZE + 1).expect_err("too long");
406 assert!(error.message().contains("longer than"), "{error}");
407 }
408
409 #[test]
410 fn an_empty_chunk_keeps_its_types() {
411 let chunk = Chunk::empty(&[LogicalType::Integer, LogicalType::Varchar]);
412 assert_eq!(chunk.len(), 0);
413 assert_eq!(chunk.types(), vec![LogicalType::Integer, LogicalType::Varchar]);
414 }
415
416 #[test]
417 fn selecting_keeps_the_rows_it_selected_and_no_others() {
418 let chunk = Chunk::new(vec![integers(&[10, 20, 30, 40]), integers(&[1, 2, 3, 4])])
419 .expect("four rows");
420 let kept = Selection::from_predicate(4, |index| index % 2 == 1);
421 let chunk = chunk.select(&kept).expect("rows one and three exist");
422 assert_eq!(chunk.len(), 2);
423 assert_eq!(chunk.row(0).collect::<Vec<_>>(), vec![Value::Integer(20), Value::Integer(2)]);
424 assert_eq!(chunk.row(1).collect::<Vec<_>>(), vec![Value::Integer(40), Value::Integer(4)]);
425 }
426
427 /// The reason `select` takes the chunk by value. If it copied the payload then a filter would
428 /// cost the same as a compaction and the selection would be a pure loss.
429 #[test]
430 fn selecting_leaves_the_values_where_they_were() {
431 let chunk = Chunk::new(vec![integers(&[10, 20, 30, 40])]).expect("four rows");
432 let kept = Selection::from_predicate(4, |index| index == 0);
433 let chunk = chunk.select(&kept).expect("row zero exists");
434 assert_eq!(chunk.column(0).expect("one column").form(), Form::Dictionary);
435 }
436
437 /// The promise a stable dictionary makes is about the outermost body, so a filter in front of a
438 /// group by has to compose the codes rather than stack a second dictionary on top of them.
439 #[test]
440 fn selecting_a_stable_dictionary_composes_the_codes_instead_of_stacking_them() {
441 let values = Arc::new(integers(&[10, 20, 30]));
442 let column = Vector::stable_dictionary(vec![2, 0, 1, 2], values).expect("three codes");
443 let chunk = Chunk::new(vec![column]).expect("four rows");
444 let kept = Selection::from_predicate(4, |index| index % 2 == 1);
445 let chunk = chunk.select(&kept).expect("rows one and three exist");
446 let column = chunk.column(0).expect("one column");
447 let (codes, values) = column.stable_dictionary_parts().expect("still a stable dictionary");
448 assert_eq!(codes, [0, 2]);
449 assert_eq!(values.len(), 3);
450 assert_eq!(column.value_at(0), Value::Integer(10));
451 assert_eq!(column.value_at(1), Value::Integer(30));
452 }
453
454 #[test]
455 fn a_selection_past_the_end_is_caught() {
456 let chunk = Chunk::new(vec![integers(&[1, 2])]).expect("two rows");
457 let mut kept = Selection::empty();
458 kept.push(7);
459 let error = chunk.select(&kept).expect_err("row seven does not exist");
460 assert!(error.message().contains("row 7"), "{error}");
461 }
462
463 /// The two halves of section 7.1's decision have to answer the same question the same way, or
464 /// the threshold between them is a place where a query changes its answer.
465 #[test]
466 fn compacting_keeps_the_same_rows_selecting_does_and_leaves_no_indirection() {
467 let chunk = Chunk::new(vec![integers(&[10, 20, 30, 40]), integers(&[1, 2, 3, 4])])
468 .expect("four rows");
469 let kept = Selection::from_predicate(4, |index| index % 2 == 1);
470 let selected = chunk.clone().select(&kept).expect("rows one and three exist");
471 let compacted = chunk.compact(&kept).expect("rows one and three exist");
472 assert_eq!(compacted.len(), selected.len());
473 for row in 0..compacted.len() {
474 assert_eq!(
475 compacted.row(row).collect::<Vec<_>>(),
476 selected.row(row).collect::<Vec<_>>()
477 );
478 }
479 assert_eq!(compacted.column(0).expect("one column").form(), Form::Flat);
480 }
481
482 #[test]
483 fn a_selection_past_the_end_is_caught_by_compacting_too() {
484 let chunk = Chunk::new(vec![integers(&[1, 2])]).expect("two rows");
485 let mut kept = Selection::empty();
486 kept.push(7);
487 let error = chunk.compact(&kept).expect_err("row seven does not exist");
488 assert!(error.message().contains("row 7"), "{error}");
489 }
490
491 #[test]
492 fn projecting_reorders_and_can_repeat_a_column() {
493 let chunk = Chunk::new(vec![integers(&[1, 2]), integers(&[3, 4])]).expect("two by two");
494 let chunk = chunk.project(&[1, 0, 1]).expect("both columns exist");
495 assert_eq!(chunk.width(), 3);
496 assert_eq!(
497 chunk.row(0).collect::<Vec<_>>(),
498 vec![Value::Integer(3), Value::Integer(1), Value::Integer(3)]
499 );
500 }
501
502 #[test]
503 fn projecting_a_column_that_is_not_there_is_caught() {
504 let chunk = Chunk::new(vec![integers(&[1, 2])]).expect("one column");
505 let error = chunk.project(&[0, 4]).expect_err("there is no column four");
506 assert!(error.message().contains("column 4"), "{error}");
507 }
508
509 #[test]
510 fn flattening_a_selected_chunk_gives_the_same_values() {
511 let chunk = Chunk::new(vec![integers(&[10, 20, 30])]).expect("three rows");
512 let kept = Selection::from_predicate(3, |index| index != 1);
513 let selected = chunk.select(&kept).expect("rows zero and two exist");
514 let flat = selected.flatten().expect("integers flatten");
515 assert_eq!(flat.column(0).expect("one column").form(), Form::Flat);
516 for row in 0..flat.len() {
517 assert_eq!(flat.value_at(row, 0), selected.value_at(row, 0), "row {row}");
518 }
519 }
520
521 /// Taking the chunk rather than borrowing it, which is the same flatten and is the one that
522 /// gets to move a column that is already flat instead of copying it.
523 #[test]
524 fn flattening_a_chunk_of_mixed_forms_moves_the_column_that_is_already_flat() {
525 let flat = integers(&[10, 20, 30]);
526 let address = |vector: &Vector| match vector.data() {
527 Some(Data::Int32(values)) => values.as_slice().as_ptr() as usize,
528 _ => panic!("the layout changed under the test"),
529 };
530 let stored = address(&flat);
531 let coded = Vector::dictionary(vec![2, 1, 0], integers(&[1, 2, 3])).expect("three codes");
532 let chunk = Chunk::new(vec![flat, coded]).expect("three rows of two columns");
533 let want: Vec<Vec<_>> = (0..3).map(|row| chunk.row(row).collect()).collect();
534 let flattened = chunk.into_flat().expect("integers flatten");
535 assert_eq!(address(flattened.column(0).expect("the first column")), stored);
536 for column in flattened.columns() {
537 assert_eq!(column.form(), Form::Flat);
538 }
539 let got: Vec<Vec<_>> = (0..3).map(|row| flattened.row(row).collect()).collect();
540 assert_eq!(got, want);
541 }
542
543 #[test]
544 fn a_chunk_costs_what_its_columns_cost() {
545 let chunk = Chunk::new(vec![integers(&[1; 1000]), integers(&[2; 1000])])
546 .expect("two columns of a thousand");
547 let columns: usize = chunk.columns().iter().map(Vector::footprint).sum();
548 assert_eq!(chunk.footprint(), size_of::<Chunk>() + columns);
549 assert!(chunk.footprint() >= 8000, "two thousand i32: {}", chunk.footprint());
550 }
551}