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
//! SELECT: projections, filters, renames, the value dialect's operators, and the top-N idiom.

use super::*;

#[test]
fn a_filter_projection_lowers_to_the_same_steps_the_toml_would() {
    let stages =
        compile_stages("INSERT INTO out SELECT auction, price * 0.908 AS eur FROM bids WHERE type = 'bid'").unwrap();
    assert_eq!(stages.len(), 1);
    let s = &stages[0];
    assert_eq!(s.inputs, vec!["bids".to_string()]);
    assert_eq!(s.output, "out");
    assert_eq!(
        s.steps,
        vec![
            json!({"op": "filter", "expression": "(type == 'bid')"}),
            json!({"op": "applyExpression", "column": "eur", "expression": "(price * 0.908)"}),
            json!({"op": "select", "columns": ["auction", "eur"]}),
        ]
    );
}

#[test]
fn select_star_is_a_pass_through_no_steps() {
    let s = &compile_stages("INSERT INTO out SELECT * FROM bids").unwrap()[0];
    assert!(s.steps.is_empty());
    assert_eq!(s.inputs, vec!["bids".to_string()]);
    assert_eq!(s.output, "out");
}

#[test]
fn star_plus_a_computed_column_keeps_all_columns_the_q1_shape() {
    // `SELECT *, <expr> AS name` = keep every column + add one (Nexmark q1) → applyExpression, no select.
    let s = &compile_stages("INSERT INTO out SELECT *, price * 0.908 AS priceEur FROM events WHERE type = 'bid'")
        .unwrap()[0];
    assert_eq!(
        s.steps,
        vec![
            json!({"op": "filter", "expression": "(type == 'bid')"}),
            json!({"op": "applyExpression", "column": "priceEur", "expression": "(price * 0.908)"}),
        ]
    );
    // a bare column with `*` is redundant → a loud error.
    assert!(compile("INSERT INTO o SELECT *, auction FROM b").is_err());
}

#[test]
fn boolean_comparison_and_case_render_in_the_value_dialect() {
    let s = &compile_stages(
        "INSERT INTO out SELECT CASE WHEN price < 100 THEN 1 ELSE 0 END AS band FROM b WHERE a > 1 AND c = 2",
    )
    .unwrap()[0];
    // SQL AND/= → dialect &&/== ; CASE → ternary.
    assert_eq!(
        s.steps[0],
        json!({"op": "filter", "expression": "((a > 1) && (c == 2))"})
    );
    assert_eq!(
        s.steps[1],
        json!({"op": "applyExpression", "column": "band", "expression": "((price < 100) ? 1 : 0)"})
    );
}

#[test]
fn a_bare_rename_and_a_function_call_lower_correctly() {
    let s = &compile_stages("INSERT INTO out SELECT bidder AS who, round(price * 0.9) AS p FROM b").unwrap()[0];
    assert_eq!(
        s.steps[0],
        json!({"op": "applyExpression", "column": "who", "expression": "bidder"})
    );
    assert_eq!(
        s.steps[1],
        json!({"op": "applyExpression", "column": "p", "expression": "round((price * 0.9))"})
    );
    assert_eq!(s.steps[2], json!({"op": "select", "columns": ["who", "p"]}));
}

#[test]
fn a_qualified_wildcard_is_star_and_qualified_columns_drop_their_table() {
    let stages = compile_stages("INSERT INTO out SELECT b.* FROM b WHERE b.type = 'bid'").unwrap();
    assert_eq!(stages[0].steps.len(), 1, "b.* keeps every column: only the filter");
    assert_eq!(stages[0].steps[0]["expression"], "(type == 'bid')");
    let stages = compile_stages("INSERT INTO out SELECT b.auction, b.price * 2 AS twice FROM b").unwrap();
    let ops: Vec<&str> = stages[0].steps.iter().map(|s| s["op"].as_str().unwrap()).collect();
    assert_eq!(ops, vec!["applyExpression", "select"]);
}

#[test]
fn in_between_and_like_lower_to_the_dialect() {
    let stages = compile_stages(
        "INSERT INTO out SELECT * FROM b WHERE auction IN (1, 2, 3) AND price BETWEEN 10 AND 20 \
             AND channel LIKE 'Goo%le_' AND NOT (name ILIKE '%x') AND bidder NOT IN (7)",
    )
    .unwrap();
    let e = stages[0].steps[0]["expression"].as_str().unwrap().to_string();
    assert!(e.contains("(auction in [1, 2, 3])"), "{e}");
    assert!(e.contains("((price >= 10) && (price <= 20))"), "{e}");
    assert!(e.contains("regexMatch(channel, '^Goo.*le.$')"), "{e}");
    assert!(e.contains("!((regexMatch(name, '(?i)^.*x$')))"), "{e}");
    assert!(e.contains("!(bidder in [7])"), "{e}");
    let err = compile_stages("INSERT INTO out SELECT * FROM b WHERE channel LIKE name").unwrap_err();
    assert!(err.contains("string-literal pattern"), "{err}");
}

#[test]
fn row_number_top_n_lowers_to_the_topn_operator() {
    let s = &compile_stages(
        "INSERT INTO out SELECT auction, price FROM ( \
               SELECT auction, price, ROW_NUMBER() OVER (PARTITION BY auction ORDER BY price DESC) AS rn FROM bids \
             ) WHERE rn <= 1",
    )
    .unwrap()[0];
    assert_eq!(s.inputs, vec!["bids".to_string()]);
    assert_eq!(
        s.steps[0],
        json!({"op": "topN", "n": 1, "orderBy": "price", "direction": "desc", "keyBy": true, "groupBy": ["auction"]})
    );
    assert_eq!(
        s.steps.last().unwrap(),
        &json!({"op": "select", "columns": ["auction", "price"]})
    );
    // ORDER BY ASC and n=10 top-N.
    let asc = &compile_stages(
        "INSERT INTO o SELECT k FROM (SELECT k, v, ROW_NUMBER() OVER (PARTITION BY k ORDER BY v) AS rn FROM t) WHERE rn <= 10",
    )
    .unwrap()[0];
    assert_eq!(asc.steps[0]["direction"], "asc");
    assert_eq!(asc.steps[0]["n"], 10);
}