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
//! Joins: an equi-join as a lookup join, an interval join as a stream join, and their columns.

use super::*;

#[test]
fn a_plain_equi_join_lowers_to_the_5b_lookup_join() {
    let s =
        &compile_stages("INSERT INTO out SELECT bids.auction, side.value FROM bids JOIN side ON bids.key = side.key")
            .unwrap()[0];
    // inputs[0]=stream, inputs[1]=table (FROM … JOIN …).
    assert_eq!(s.inputs, vec!["bids".to_string(), "side".to_string()]);
    // `side.value` is the table's `value` — kept by the join as `side_value`, whatever the stream
    // carries — and projected back under its SQL name
    assert_eq!(
        s.steps,
        vec![
            json!({"op": "lookupJoin", "joinKey": "key", "rightRename": {"value": "side_value"}}),
            json!({"op": "applyExpression", "column": "value", "expression": "side_value"}),
            json!({"op": "select", "columns": ["auction", "value"]}),
        ]
    );
}

#[test]
fn a_join_with_a_time_interval_lowers_to_the_stream_join() {
    let s = &compile_stages(
        "INSERT INTO out SELECT a.x, b.y FROM a JOIN b \
             ON a.k = b.k AND a.ts BETWEEN b.ts - INTERVAL '5' SECOND AND b.ts + INTERVAL '5' SECOND",
    )
    .unwrap()[0];
    assert_eq!(s.inputs, vec!["a".to_string(), "b".to_string()]);
    assert_eq!(
        s.steps[0],
        json!({"op": "streamJoin", "joinKey": "k", "timeColumn": "ts", "windowMs": 5000, "keyBy": true, "rightRename": {"y": "b_y"}})
    );
}

#[test]
fn a_qualified_column_in_a_join_reads_its_own_side() {
    // REGRESSION (streams#55): `a.b` reduced to `b` — in a JOIN, `p.ts` and `a.ts` were both the
    // left's `ts`, and the right's value was unreachable, with no error.
    let s = &compile_stages(
        "INSERT INTO out SELECT p.ts AS person_ts, a.ts AS auction_ts, a.id FROM p JOIN a \
             ON p.id = a.id AND p.ts BETWEEN a.ts - INTERVAL '5' SECOND AND a.ts + INTERVAL '5' SECOND \
             WHERE a.price > 10",
    )
    .unwrap()[0];
    assert_eq!(
        s.steps,
        vec![
            json!({"op": "streamJoin", "joinKey": "id", "timeColumn": "ts", "windowMs": 5000, "keyBy": true,
                   "rightRename": {"price": "a_price", "ts": "a_ts"}}),
            json!({"op": "filter", "expression": "(a_price > 10)"}),
            json!({"op": "applyExpression", "column": "person_ts", "expression": "ts"}),
            json!({"op": "applyExpression", "column": "auction_ts", "expression": "a_ts"}),
            json!({"op": "select", "columns": ["person_ts", "auction_ts", "id"]}),
        ],
        "the left's ts, the right's ts (renamed by the join), the key once"
    );
}

#[test]
fn a_join_refuses_what_it_cannot_resolve() {
    let join = |select: &str| {
        compile_stages(&format!(
            "INSERT INTO out SELECT {select} FROM p JOIN a ON p.id = a.id \
                 AND p.ts BETWEEN a.ts - INTERVAL '5' SECOND AND a.ts + INTERVAL '5' SECOND"
        ))
        .expect_err("refused")
    };
    let err = join("p.ts, a.ts");
    assert!(err.contains("two SELECT columns are named `ts`"), "{err}");
    let err = join("x.ts");
    assert!(
        err.contains("`x` is not a table of this JOIN") && err.contains("`p`, `a`"),
        "{err}"
    );
    let err = join("a.*");
    assert!(err.contains("`a.*` in a JOIN is not supported"), "{err}");
    let err = compile_stages(
        "INSERT INTO out SELECT e.ts FROM e JOIN e ON e.id = e.id \
             AND e.ts BETWEEN e.ts - INTERVAL '5' SECOND AND e.ts + INTERVAL '5' SECOND",
    )
    .expect_err("refused");
    assert!(err.contains("both sides of the JOIN answer to `e`"), "{err}");
}

#[test]
fn a_join_key_with_mismatched_column_names_is_a_loud_error() {
    assert!(compile("INSERT INTO o SELECT * FROM a JOIN b ON a.x = b.y")
        .unwrap_err()
        .contains("share a name"));
}