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(),
spg_storage::Value::Int(n) => n.to_string(),
v => format!("{v:?}"),
},
_ => panic!("rows"),
}
}
#[test]
fn array_2d_element_subscript() {
let mut e = Engine::new();
assert_eq!(text(&mut e, "SELECT (ARRAY[[1,2],[3,4]])[1][2]"), "2");
assert_eq!(text(&mut e, "SELECT (ARRAY[[1,2],[3,4]])[2][1]"), "3");
assert_eq!(
text(&mut e, "SELECT (ARRAY[['a','b'],['c','d']])[2][2]"),
"d"
);
assert_eq!(
text(&mut e, "SELECT ((ARRAY[[9999999999,2],[3,4]])[1][1])::text"),
"9999999999"
);
assert_eq!(text(&mut e, "SELECT (ARRAY[[1,2],[3,4]])[1]"), "Null");
assert_eq!(text(&mut e, "SELECT (ARRAY[[1,2],[3,4]])[3][1]"), "Null");
assert_eq!(text(&mut e, "SELECT (ARRAY[[1,2],[3,4]])[1][9]"), "Null");
assert_eq!(text(&mut e, "SELECT (ARRAY[7,8,9])[2]"), "8");
assert_eq!(text(&mut e, "SELECT (ARRAY[7,8,9])[9]"), "Null");
}
#[test]
fn array_2d_construct_and_introspect() {
let mut e = Engine::new();
assert_eq!(
text(&mut e, "SELECT (ARRAY[[1,2],[3,4]])::text"),
"{{1,2},{3,4}}"
);
assert_eq!(
text(&mut e, "SELECT (ARRAY[[1,2,3],[4,5,6]])::text"),
"{{1,2,3},{4,5,6}}"
);
assert_eq!(
text(&mut e, "SELECT (ARRAY[['a','b'],['c','d']])::text"),
"{{a,b},{c,d}}"
);
assert_eq!(
text(&mut e, "SELECT array_dims(ARRAY[[1,2],[3,4]])"),
"[1:2][1:2]"
);
assert_eq!(text(&mut e, "SELECT array_ndims(ARRAY[[1,2],[3,4]])"), "2");
assert_eq!(text(&mut e, "SELECT cardinality(ARRAY[[1,2],[3,4]])"), "4");
assert_eq!(
text(&mut e, "SELECT array_length(ARRAY[[1,2,3],[4,5,6]],1)"),
"2"
);
assert_eq!(
text(&mut e, "SELECT array_length(ARRAY[[1,2,3],[4,5,6]],2)"),
"3"
);
assert_eq!(
text(&mut e, "SELECT array_upper(ARRAY[[1,2,3],[4,5,6]],2)"),
"3"
);
assert_eq!(
text(&mut e, "SELECT array_lower(ARRAY[[1,2,3],[4,5,6]],1)"),
"1"
);
assert!(e.execute("SELECT ARRAY[[1,2],[3]]").is_err());
assert_eq!(text(&mut e, "SELECT array_ndims(ARRAY[1,2,3])"), "1");
assert_eq!(text(&mut e, "SELECT array_dims(ARRAY[1,2,3])"), "[1:3]");
assert_eq!(text(&mut e, "SELECT ('[1,2,3]'::vector)::text"), "[1,2,3]");
}