Skip to main content

fv_streams_sql/
lib.rs

1//! # fv-streams-sql — the SQL front door
2//!
3//! A streaming SQL statement parses to a **small typed logical plan** ([`plan::Plan`]) and lowers to
4//! the *same* [`Topology`] of [`StageDef`](fv_streams_types::StageDef)s the TOML frontend produces (whose `steps` are the engine's
5//! own JSON vocabulary). One runtime, two front doors: SQL inherits checkpointing, the exchange,
6//! exactness and `--join` because it emits the identical steps a hand-written pipeline would.
7//!
8//! Deliberately **not** DataFusion's `LogicalPlan`/physical layer (design §10): our own IR models
9//! streaming (DataFusion's does not), so there is no fork risk, the errors are streaming-aware and
10//! ours, and the IR is the seam a future optimiser plugs into. Scalar expressions lower to the value
11//! dialect strings the `expression` steps already carry, compiled through `fv-value-datafusion` at
12//! build time exactly as the TOML path does.
13//!
14//! ```
15//! let topo = fv_streams_sql::compile(
16//!     "INSERT INTO out SELECT auction, price * 0.908 AS eur FROM bids WHERE type = 'bid'",
17//! ).unwrap();
18//! assert_eq!(topo.stages.len(), 1);
19//! ```
20
21use fv_streams_types::Topology;
22
23mod expr;
24mod lower;
25mod plan;
26
27pub use plan::Plan;
28pub mod reference;
29
30/// Compile one streaming SQL statement into a [`Topology`]. Datasets (the `FROM`/`INSERT INTO`
31/// targets) are referenced by name; their bindings (topic, connector, schema) stay in the pipeline
32/// config, exactly as the TOML frontend resolves them. Fails LOUDLY with a streaming-aware message
33/// on anything outside the supported subset.
34pub fn compile(sql: &str) -> Result<Topology, String> {
35    let plan = plan::parse(sql)?;
36    let plan = optimize(plan);
37    let stages = lower::lower(&plan)?;
38    Ok(Topology { stages })
39}
40
41/// The optimiser seam: passes over the typed IR (filter pushdown, projection pruning, …) go here —
42/// streaming-aware because the IR is. Empty today; the boundary exists so a mature optimiser is
43/// additive, on our own IR, never a rewrite (design §10).
44fn optimize(plan: Plan) -> Plan {
45    plan
46}
47
48/// Lower a plan to `StageDef`s without wrapping in a `Topology` — for tests that assert the steps.
49#[cfg(test)]
50pub(crate) fn compile_stages(sql: &str) -> Result<Vec<fv_streams_types::StageDef>, String> {
51    lower::lower(&optimize(plan::parse(sql)?))
52}
53
54#[cfg(test)]
55mod tests;