use spg_engine::{Engine, QueryResult};
fn text(e: &mut Engine, sql: &str) -> String {
match e.execute(sql).unwrap() {
QueryResult::Rows { rows, .. } => match &rows[0].values[0] {
spg_storage::Value::Text(s) => s.to_string(),
v => format!("{v:?}"),
},
_ => panic!("expected rows"),
}
}
#[test]
fn sum_avg_bigint_widen_to_numeric() {
let mut e = Engine::new();
e.execute("CREATE TABLE t (i int, b bigint)").unwrap();
e.execute("INSERT INTO t VALUES (1, 1000000000000), (2, 2000000000000), (3, 3000000000000)")
.unwrap();
assert_eq!(text(&mut e, "SELECT pg_typeof(sum(i)) FROM t"), "bigint");
assert_eq!(text(&mut e, "SELECT pg_typeof(sum(b)) FROM t"), "numeric");
assert_eq!(text(&mut e, "SELECT sum(b)::text FROM t"), "6000000000000");
assert_eq!(text(&mut e, "SELECT pg_typeof(avg(i)) FROM t"), "numeric");
assert_eq!(text(&mut e, "SELECT pg_typeof(avg(b)) FROM t"), "numeric");
e.execute("CREATE TABLE big (v bigint)").unwrap();
e.execute("INSERT INTO big VALUES (9000000000000000000), (9000000000000000000)")
.unwrap();
assert_eq!(
text(&mut e, "SELECT sum(v)::text FROM big"),
"18000000000000000000"
);
}