#![cfg(feature = "std")]
use std::process::Command;
fn sqlite3_available() -> bool {
Command::new("sqlite3").arg("--version").output().is_ok()
}
fn out(bin: &str, sql: &str) -> String {
let o = Command::new(bin).arg(":memory:").arg(sql).output().unwrap();
let mut s = String::from_utf8_lossy(&o.stdout).into_owned();
s.push_str(&String::from_utf8_lossy(&o.stderr));
s.trim_end().to_string()
}
#[test]
fn window_query_emits_rows_in_window_order() {
if !sqlite3_available() {
eprintln!("sqlite3 CLI not found; skipping");
return;
}
let g = env!("CARGO_BIN_EXE_graphitesql");
let cases = [
"CREATE TABLE t(x); INSERT INTO t VALUES(5),(3),(8); \
SELECT x, row_number() OVER (ORDER BY x) FROM t",
"CREATE TABLE t(x); INSERT INTO t VALUES(5),(3),(8); \
SELECT x, sum(x) OVER (ORDER BY x) FROM t",
"CREATE TABLE t(x); INSERT INTO t VALUES(5),(3),(8); \
SELECT x, sum(x) OVER (ORDER BY x DESC) FROM t",
"CREATE TABLE t(x,g); INSERT INTO t VALUES(5,'b'),(3,'a'),(8,'b'),(1,'a'); \
SELECT x,g, sum(x) OVER (PARTITION BY g ORDER BY x) FROM t",
"CREATE TABLE t(x,g); INSERT INTO t VALUES(5,'b'),(3,'a'),(8,'b'); \
SELECT x,g, sum(x) OVER (PARTITION BY g) FROM t",
"CREATE TABLE t(x,y); INSERT INTO t VALUES(5,20),(3,30),(8,10); \
SELECT x,y, sum(x) OVER (ORDER BY x), sum(y) OVER (ORDER BY y) FROM t",
"CREATE TABLE t(x,y); INSERT INTO t VALUES(5,20),(3,30),(8,10); \
SELECT x,y, sum(y) OVER (ORDER BY y), sum(x) OVER (ORDER BY x) FROM t",
"CREATE TABLE t(x); INSERT INTO t VALUES(5),(3),(8); \
SELECT x, first_value(x) OVER w, last_value(x) OVER w FROM t \
WINDOW w AS (ORDER BY x)",
"CREATE TABLE t(x); INSERT INTO t VALUES(5),(3),(8); \
SELECT x, lag(x) OVER w, lead(x) OVER w FROM t WINDOW w AS (ORDER BY x DESC)",
"CREATE TABLE t(x); INSERT INTO t VALUES(NULL),(3),(NULL),(8); \
SELECT x, count(*) OVER (ORDER BY x) FROM t",
"CREATE TABLE t(x); INSERT INTO t VALUES(5),(3),(8); \
SELECT x, count(*) OVER () FROM t",
"CREATE TABLE t(x); INSERT INTO t VALUES(5),(3),(8),(3); \
SELECT DISTINCT x, sum(x) OVER (ORDER BY x) FROM t",
"CREATE TABLE t(x); INSERT INTO t VALUES(5),(3),(8); \
SELECT x, sum(x) OVER (ORDER BY x) FROM t ORDER BY x DESC",
"CREATE TABLE t(x); INSERT INTO t VALUES(5),(3),(8); \
SELECT x, sum(x) OVER (ORDER BY x) FROM t LIMIT 2",
];
for sql in cases {
assert_eq!(out("sqlite3", sql), out(g, sql), "for {sql}");
}
}