Skip to content

Commit 7eef805

Browse files
committed
return an error for nested arrays and more test coverage
Signed-off-by: Somtochi Onyekwere <[email protected]>
1 parent 61ab77c commit 7eef805

File tree

2 files changed

+70
-57
lines changed

2 files changed

+70
-57
lines changed

crates/corro-pg/src/codec.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ fn extract_array_elements(
235235
let mut current = String::new();
236236
let mut in_quotes = false;
237237
let mut escape_next = false;
238-
let mut depth = 0; // For nested arrays
239238
let mut seen_content = false; // helpful for tracking when the last element is an empty string
240239

241240
for ch in input.chars() {
@@ -252,14 +251,12 @@ fn extract_array_elements(
252251
// Don't include the quotes in the output
253252
}
254253
'{' if !in_quotes && !escape_next => {
255-
depth += 1;
256-
current.push(ch);
254+
return Err("Nested arrays are not supported".into());
257255
}
258256
'}' if !in_quotes && !escape_next => {
259-
depth -= 1;
260-
current.push(ch);
257+
return Err("Nested arrays are not supported".into());
261258
}
262-
',' if !in_quotes && depth == 0 && !escape_next => {
259+
',' if !in_quotes && !escape_next => {
263260
// End of current element
264261
if !current.trim().eq_ignore_ascii_case("NULL") {
265262
elements.push(std::mem::take(&mut current));

crates/corro-pg/tests/tests.rs

Lines changed: 67 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -945,39 +945,49 @@ async fn test_unnest_vtab() {
945945
// Test single array unnest with int type
946946
{
947947
let col1 = vec![1i64, 2, 3, 4, 1337, 12312312312];
948-
let rows = client
949-
.query(
950-
"SELECT CAST(value0 AS int) FROM unnest(CAST($1 AS int[]))",
951-
&[&col1],
952-
)
953-
.await
954-
.unwrap();
955-
assert_eq!(rows.len(), col1.len());
956-
for (i, row) in rows.iter().enumerate() {
957-
let val: i64 = row.get(0);
958-
assert_eq!(val, col1[i]);
948+
for format in [Format::Text, Format::Binary] {
949+
for func in ["unnest", "corro_unnest"] {
950+
let sql_vec = SqlVec {
951+
inner: &col1,
952+
format,
953+
};
954+
let rows = client
955+
.query(
956+
&format!("SELECT CAST(value0 AS int) FROM {func}(CAST($1 AS int[]))"),
957+
&[&sql_vec],
958+
)
959+
.await
960+
.unwrap();
961+
assert_eq!(rows.len(), col1.len());
962+
for (i, row) in rows.iter().enumerate() {
963+
let val: i64 = row.get(0);
964+
assert_eq!(val, col1[i]);
965+
}
966+
}
959967
}
960968
}
961969

962970
// Test single array unnest with text type
963971
{
964972
for format in [Format::Text, Format::Binary] {
965-
let col1 = vec!["a", "b", "c", "d", "e", "f", ""];
966-
let sql_vec = SqlVec {
967-
inner: &col1,
968-
format,
969-
};
970-
let rows = client
971-
.query(
972-
"SELECT CAST(value0 AS text) FROM unnest(CAST($1 AS text[]))",
973-
&[&sql_vec],
974-
)
975-
.await
976-
.unwrap();
977-
assert_eq!(rows.len(), col1.len());
978-
for (i, row) in rows.iter().enumerate() {
979-
let val: String = row.get(0);
980-
assert_eq!(val, col1[i]);
973+
for func in ["unnest", "corro_unnest"] {
974+
let col1 = vec!["a", "b", "c", "d", "e", "f", ""];
975+
let sql_vec = SqlVec {
976+
inner: &col1,
977+
format,
978+
};
979+
let rows = client
980+
.query(
981+
&format!("SELECT CAST(value0 AS text) FROM {func}(CAST($1 AS text[]))"),
982+
&[&sql_vec],
983+
)
984+
.await
985+
.unwrap();
986+
assert_eq!(rows.len(), col1.len());
987+
for (i, row) in rows.iter().enumerate() {
988+
let val: String = row.get(0);
989+
assert_eq!(val, col1[i]);
990+
}
981991
}
982992
}
983993
}
@@ -986,20 +996,24 @@ async fn test_unnest_vtab() {
986996
{
987997
let col1 = vec![1.0, 2.0, 3.0, 4.0, 1337.0, 12312312312.0];
988998
for format in [Format::Text, Format::Binary] {
989-
let sql_vec = SqlVec {
990-
inner: &col1,
991-
format,
992-
};
993-
let rows = client
994-
.query(
995-
"SELECT CAST(value0 AS float) FROM unnest(CAST($1 AS float[]))",
996-
&[&sql_vec],
997-
)
998-
.await
999-
.unwrap();
1000-
for (i, row) in rows.iter().enumerate() {
1001-
let val: f64 = row.get(0);
1002-
assert_eq!(val, col1[i]);
999+
for func in ["unnest", "corro_unnest"] {
1000+
let sql_vec = SqlVec {
1001+
inner: &col1,
1002+
format,
1003+
};
1004+
let rows = client
1005+
.query(
1006+
&format!(
1007+
"SELECT CAST(value0 AS float) FROM {func}(CAST($1 AS float[]))"
1008+
),
1009+
&[&sql_vec],
1010+
)
1011+
.await
1012+
.unwrap();
1013+
for (i, row) in rows.iter().enumerate() {
1014+
let val: f64 = row.get(0);
1015+
assert_eq!(val, col1[i]);
1016+
}
10031017
}
10041018
}
10051019
}
@@ -1008,16 +1022,18 @@ async fn test_unnest_vtab() {
10081022
// TODO: pgwire's text encoding for blob[] is currently broken but we'd work for proper clients
10091023
{
10101024
let col1 = vec![b"a", b"b", b"c", b"d", b"e", b"f"];
1011-
let rows = client
1012-
.query(
1013-
"SELECT CAST(value0 AS blob) FROM unnest(CAST($1 AS blob[]))",
1014-
&[&col1],
1015-
)
1016-
.await
1017-
.unwrap();
1018-
for (i, row) in rows.iter().enumerate() {
1019-
let val: Vec<u8> = row.get(0);
1020-
assert_eq!(val, col1[i]);
1025+
for func in ["unnest", "corro_unnest"] {
1026+
let rows = client
1027+
.query(
1028+
&format!("SELECT CAST(value0 AS blob) FROM {func}(CAST($1 AS blob[]))"),
1029+
&[&col1],
1030+
)
1031+
.await
1032+
.unwrap();
1033+
for (i, row) in rows.iter().enumerate() {
1034+
let val: Vec<u8> = row.get(0);
1035+
assert_eq!(val, col1[i]);
1036+
}
10211037
}
10221038
}
10231039

0 commit comments

Comments
 (0)