Skip to main content

relate_executor/
consumer.rs

1//! The terminal batch consumer (ADR-0682): the final `RETURN` of a
2//! streaming statement hands each bounded batch of result rows to a
3//! caller-supplied consumer instead of appending to the part's `Vec<Row>`.
4//! The pull itself is the back-pressure: the terminal pulls the next batch
5//! only after the consumer returns from the previous one, and a consumer
6//! answering [`BatchFlow::Stop`] ends the walk and cancels the statement.
7
8use relate_core::Result;
9
10/// Rows per result batch (ADR-0555's batch bound): the terminal's consumer
11/// deliveries and the materialized path's replay chunking share the one
12/// bound the inter-clause stream already batches by.
13pub const BATCH_ROWS: usize = 1024;
14
15/// What the consumer wants after a batch: the next one, or the end of the
16/// walk. `Stop` is `RowStream::stop` for the statement: the terminal stops
17/// its input stream and the statement fails with `Error::Cancelled`
18/// (`"query cancelled"`), exactly as a cancelled token ends it.
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub enum BatchFlow {
21    /// Hand over the next batch.
22    Continue,
23    /// End the walk; the statement is cancelled.
24    Stop,
25}
26
27/// The caller-supplied sink for a streaming statement's result (ADR-0682).
28/// The engine calls [`RowConsumer::columns`] exactly once, before the first
29/// batch (a `RETURN *` learns its columns only at its first batch, so the
30/// columns may arrive together with it), then [`RowConsumer::batch`] once
31/// per bounded batch of result rows, in row order. The consumer runs on the
32/// statement's own thread — the connection's query worker, or the caller's
33/// thread where the statement itself runs inline — never concurrently with
34/// the pull that produced the batch.
35///
36/// `Send`: the statement runs on the connection's worker thread, so the
37/// consumer crosses to it.
38pub trait RowConsumer: Send {
39    /// The result columns, once, before the first batch.
40    fn columns(&mut self, columns: &[String]) -> Result<()>;
41    /// One bounded batch of result rows, never more than [`BATCH_ROWS`]:
42    /// each row carries one value per announced column, in the columns
43    /// announcement's order.
44    fn batch(&mut self, rows: Vec<Vec<relate_core::Value>>) -> Result<BatchFlow>;
45}
46
47/// The engine-side handle a statement carries for its consumer: wraps the
48/// caller's [`RowConsumer`] with whether it has been engaged (columns were
49/// announced), so the boundary can tell a statement whose rows streamed
50/// from one whose result still sits in its `QueryResult` — the materialized
51/// forms (writes, `EXPLAIN`, catalog commands, `UNION` folds, breakers)
52/// replay their rows through the same sink after the run.
53pub struct StreamSink<'a> {
54    consumer: &'a mut dyn RowConsumer,
55    engaged: bool,
56}
57
58impl<'a> StreamSink<'a> {
59    /// Wrap the caller's consumer for one statement.
60    pub fn new(consumer: &'a mut dyn RowConsumer) -> Self {
61        Self {
62            consumer,
63            engaged: false,
64        }
65    }
66
67    /// Whether the columns announcement went out: the streaming terminal
68    /// engaged the consumer, so its `QueryResult` carries no rows.
69    pub fn engaged(&self) -> bool {
70        self.engaged
71    }
72
73    /// Announce the columns, once. Marks the sink engaged.
74    pub(crate) fn columns(&mut self, columns: &[String]) -> Result<()> {
75        self.engaged = true;
76        self.consumer.columns(columns)
77    }
78
79    /// Hand over one batch. Marks the sink engaged.
80    pub(crate) fn batch(&mut self, rows: Vec<Vec<relate_core::Value>>) -> Result<BatchFlow> {
81        self.engaged = true;
82        self.consumer.batch(rows)
83    }
84}