Skip to main content

uqa_execution/batch/
batches.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7use super::{
8    OwnedPhysicalRow, PhysicalRow, ResultRow, RowFragment, RowMaterializer, RowSchema,
9    DEFAULT_BATCH_SIZE,
10};
11
12/// A schema and bounded vector of physical rows flowing between operators.
13#[derive(Debug, Clone, PartialEq)]
14pub struct Batch {
15    pub schema: RowSchema,
16    pub rows: Vec<PhysicalRow>,
17}
18
19impl Batch {
20    /// Compatibility constructor for named rows entering the physical engine. The resulting batch is positional immediately; maps do not flow to the next operator.
21    pub fn new(schema: RowSchema, rows: Vec<ResultRow>) -> Self {
22        let rows = rows
23            .into_iter()
24            .map(|row| PhysicalRow::from_result_row(&schema, row))
25            .collect();
26        Self { schema, rows }
27    }
28
29    pub fn from_physical_rows(schema: RowSchema, rows: Vec<PhysicalRow>) -> Self {
30        debug_assert!(rows.iter().all(|row| {
31            row.fragments.iter().map(RowFragment::len).sum::<usize>() == schema.physical_width()
32        }));
33        Self { schema, rows }
34    }
35
36    pub fn empty(schema: RowSchema) -> Self {
37        Self {
38            schema,
39            rows: Vec::new(),
40        }
41    }
42
43    pub fn len(&self) -> usize {
44        self.rows.len()
45    }
46
47    pub fn is_empty(&self) -> bool {
48        self.rows.is_empty()
49    }
50
51    pub fn into_result_rows(self) -> Vec<ResultRow> {
52        let schema = self.schema;
53        if schema.is_identity_layout() {
54            return self
55                .rows
56                .into_iter()
57                .map(|row| RowMaterializer::new(&schema).materialize_identity_result_row(row))
58                .collect();
59        }
60        RowMaterializer::new(&schema).materialize_remapped_result_rows(self.rows)
61    }
62
63    /// Consume a batch without materializing named maps.
64    pub fn into_owned_rows(self) -> Vec<OwnedPhysicalRow> {
65        let schema = self.schema;
66        self.rows
67            .into_iter()
68            .map(|row| OwnedPhysicalRow::new(schema.clone(), row))
69            .collect()
70    }
71
72    /// Split named rows into batches of at most [`DEFAULT_BATCH_SIZE`].
73    pub fn chunked(schema: RowSchema, rows: Vec<ResultRow>) -> Vec<Batch> {
74        if rows.is_empty() {
75            return vec![Batch::empty(schema)];
76        }
77        let mut out = Vec::with_capacity(rows.len().div_ceil(DEFAULT_BATCH_SIZE));
78        let mut buf = Vec::with_capacity(DEFAULT_BATCH_SIZE);
79        for row in rows {
80            buf.push(row);
81            if buf.len() == DEFAULT_BATCH_SIZE {
82                out.push(Batch::new(schema.clone(), std::mem::take(&mut buf)));
83                buf.reserve(DEFAULT_BATCH_SIZE);
84            }
85        }
86        if !buf.is_empty() {
87            out.push(Batch::new(schema, buf));
88        }
89        out
90    }
91}