fv-streams-sql 0.9.0

The SQL front door for FusionVault Streams: a streaming SQL statement parses to a small typed logical plan and lowers to the same topology the TOML frontend produces — one runtime, two front doors. No DataFusion planner, no fork; expressions compile through the value dialect as the steps already do.
Documentation
//! CTEs: each `WITH` query parsed to its own plan, and bound where the statement reads it by name.

use super::{object_name, plan_query, Plan};

/// Parse a `WITH` clause into `(name, plan)` in declaration order. Each CTE body is planned and then
/// bound against the CTEs declared before it (SQL scoping), so later stages can read earlier ones.
/// Recursive CTEs (`WITH RECURSIVE`) are not supported — there is no streaming fixpoint.
pub(super) fn parse_ctes(with: Option<&sqlparser::ast::With>) -> Result<Vec<(String, Plan)>, String> {
    let Some(with) = with else { return Ok(Vec::new()) };
    if with.recursive {
        return Err("WITH RECURSIVE is not supported — a streaming CTE has no fixpoint".into());
    }
    let mut bound: Vec<(String, Plan)> = Vec::new();
    for cte in &with.cte_tables {
        let name = object_name(&cte.alias.name.value);
        let plan = bind_ctes(plan_query(&cte.query)?, &bound);
        bound.push((name, plan));
    }
    Ok(bound)
}

/// Replace every `Source(name)` that names a CTE with a `Named { name, <cte plan> }` boundary, so
/// lowering materialises it as its own stage. Recurses through the whole plan; a non-CTE `Source`
/// (a base dataset) is left untouched.
pub(super) fn bind_ctes(plan: Plan, ctes: &[(String, Plan)]) -> Plan {
    if ctes.is_empty() {
        return plan;
    }
    let boxed = |p: Box<Plan>| Box::new(bind_ctes(*p, ctes));
    match plan {
        Plan::Source(name) => match ctes.iter().find(|(n, _)| *n == name) {
            Some((_, cte)) => Plan::Named {
                name,
                input: Box::new(cte.clone()),
            },
            None => Plan::Source(name),
        },
        Plan::Named { name, input } => Plan::Named {
            name,
            input: boxed(input),
        },
        Plan::Join {
            left,
            right,
            join_key,
            kind,
            right_rename,
        } => Plan::Join {
            left: boxed(left),
            right: boxed(right),
            join_key,
            kind,
            right_rename,
        },
        Plan::Filter { input, predicate } => Plan::Filter {
            input: boxed(input),
            predicate,
        },
        Plan::Project { input, star, items } => Plan::Project {
            input: boxed(input),
            star,
            items,
        },
        Plan::Aggregate {
            input,
            time_column,
            window,
            group_by,
            pre,
            aggs,
            post,
            having,
            trigger,
        } => Plan::Aggregate {
            input: boxed(input),
            time_column,
            window,
            group_by,
            having,
            pre,
            aggs,
            post,
            trigger,
        },
        Plan::TopN {
            input,
            group_by,
            order_by,
            tie_by,
            descending,
            n,
        } => Plan::TopN {
            input: boxed(input),
            group_by,
            order_by,
            tie_by,
            descending,
            n,
        },
        Plan::Sink { input, name } => Plan::Sink {
            input: boxed(input),
            name,
        },
    }
}