Skip to main content

datum_sql/connect/
mod.rs

1//! Connector adapters for `datum-sql`.
2//!
3//! These adapters keep connector metadata outside Arrow row data. The per-batch
4//! envelope carries only source position and schema revision; time, watermarks,
5//! barriers, and checkpoint epochs belong to later control-envelope work.
6
7use arrow::record_batch::RecordBatch;
8
9#[cfg(feature = "cdc")]
10pub mod cdc;
11#[cfg(feature = "mq")]
12pub mod mq;
13
14/// Side metadata for one emitted SQL batch.
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct BatchEnvelope<P> {
17    source_position: P,
18    schema_revision: u64,
19}
20
21impl<P> BatchEnvelope<P> {
22    /// Creates a batch envelope from source position metadata and schema revision.
23    #[must_use]
24    pub const fn new(source_position: P, schema_revision: u64) -> Self {
25        Self {
26            source_position,
27            schema_revision,
28        }
29    }
30
31    /// Returns the source position metadata.
32    #[must_use]
33    pub const fn source_position(&self) -> &P {
34        &self.source_position
35    }
36
37    /// Returns the schema revision associated with the batch.
38    #[must_use]
39    pub const fn schema_revision(&self) -> u64 {
40        self.schema_revision
41    }
42
43    /// Consumes the envelope and returns the source position metadata.
44    #[must_use]
45    pub fn into_source_position(self) -> P {
46        self.source_position
47    }
48}
49
50/// A data batch paired with connector-side metadata.
51#[derive(Debug, Clone, PartialEq)]
52pub struct EnvelopedBatch<T, P> {
53    batch: T,
54    envelope: BatchEnvelope<P>,
55}
56
57impl<T, P> EnvelopedBatch<T, P> {
58    /// Creates a batch with connector-side metadata.
59    #[must_use]
60    pub const fn new(batch: T, envelope: BatchEnvelope<P>) -> Self {
61        Self { batch, envelope }
62    }
63
64    /// Returns the payload batch.
65    #[must_use]
66    pub const fn batch(&self) -> &T {
67        &self.batch
68    }
69
70    /// Returns the connector metadata envelope.
71    #[must_use]
72    pub const fn envelope(&self) -> &BatchEnvelope<P> {
73        &self.envelope
74    }
75
76    /// Consumes this value and returns only the payload batch.
77    #[must_use]
78    pub fn into_batch(self) -> T {
79        self.batch
80    }
81
82    /// Consumes this value and returns payload plus envelope.
83    #[must_use]
84    pub fn into_parts(self) -> (T, BatchEnvelope<P>) {
85        (self.batch, self.envelope)
86    }
87}
88
89/// Append-only Arrow batch with connector-side metadata.
90pub type EnvelopedRecordBatch<P> = EnvelopedBatch<RecordBatch, P>;