Skip to content

Commit 8e2e08a

Browse files
authored
Merge pull request #87 from psqlpy-python/feature/runtime_error_with_empty_array
Fixed runtime error
2 parents e8f118a + fedc36d commit 8e2e08a

File tree

2 files changed

+77
-50
lines changed

2 files changed

+77
-50
lines changed

python/tests/test_value_converter.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,3 +1001,22 @@ async def test_incorrect_dimensions_array(
10011001
],
10021002
],
10031003
)
1004+
1005+
1006+
async def test_empty_array(
1007+
psql_pool: ConnectionPool,
1008+
) -> None:
1009+
await psql_pool.execute("DROP TABLE IF EXISTS test_earr")
1010+
await psql_pool.execute(
1011+
"CREATE TABLE test_earr (id serial NOT NULL PRIMARY KEY, e_array text[] NOT NULL DEFAULT array[]::text[])",
1012+
)
1013+
1014+
await psql_pool.execute("INSERT INTO test_earr(id) VALUES(2);")
1015+
1016+
res = await psql_pool.execute(
1017+
"SELECT * FROM test_earr WHERE id = 2",
1018+
)
1019+
1020+
json_result = res.result()
1021+
assert json_result
1022+
assert not json_result[0]["e_array"]

src/value_converter.rs

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -835,35 +835,39 @@ fn _pythondto_array_to_serde(
835835
dimension_index: usize,
836836
mut lower_bound: usize,
837837
) -> RustPSQLDriverPyResult<Value> {
838-
let current_dimension = dimensions.get(dimension_index).unwrap();
839-
840-
let possible_next_dimension = dimensions.get(dimension_index + 1);
841-
match possible_next_dimension {
842-
Some(next_dimension) => {
843-
let mut final_list: Value = Value::Array(vec![]);
844-
845-
for _ in 0..current_dimension.len as usize {
846-
if dimensions.get(dimension_index + 1).is_some() {
847-
let inner_pylist = _pythondto_array_to_serde(
848-
dimensions,
849-
&data[lower_bound..next_dimension.len as usize + lower_bound],
850-
dimension_index + 1,
851-
0,
852-
)?;
853-
match final_list {
854-
Value::Array(ref mut array) => array.push(inner_pylist),
855-
_ => unreachable!(),
856-
}
857-
lower_bound += next_dimension.len as usize;
858-
};
859-
}
838+
let current_dimension = dimensions.get(dimension_index);
839+
840+
if let Some(current_dimension) = current_dimension {
841+
let possible_next_dimension = dimensions.get(dimension_index + 1);
842+
match possible_next_dimension {
843+
Some(next_dimension) => {
844+
let mut final_list: Value = Value::Array(vec![]);
845+
846+
for _ in 0..current_dimension.len as usize {
847+
if dimensions.get(dimension_index + 1).is_some() {
848+
let inner_pylist = _pythondto_array_to_serde(
849+
dimensions,
850+
&data[lower_bound..next_dimension.len as usize + lower_bound],
851+
dimension_index + 1,
852+
0,
853+
)?;
854+
match final_list {
855+
Value::Array(ref mut array) => array.push(inner_pylist),
856+
_ => unreachable!(),
857+
}
858+
lower_bound += next_dimension.len as usize;
859+
};
860+
}
860861

861-
Ok(final_list)
862-
}
863-
None => {
864-
return data.iter().map(|x| x.to_serde_value()).collect();
862+
return Ok(final_list);
863+
}
864+
None => {
865+
return data.iter().map(|x| x.to_serde_value()).collect();
866+
}
865867
}
866868
}
869+
870+
Ok(Value::Array(vec![]))
867871
}
868872

869873
/// Convert rust array to python list.
@@ -899,33 +903,37 @@ fn _postgres_array_to_py<T>(
899903
where
900904
T: ToPyObject,
901905
{
902-
let current_dimension = dimensions.get(dimension_index).unwrap();
903-
904-
let possible_next_dimension = dimensions.get(dimension_index + 1);
905-
match possible_next_dimension {
906-
Some(next_dimension) => {
907-
let final_list = PyList::empty_bound(py);
908-
909-
for _ in 0..current_dimension.len as usize {
910-
if dimensions.get(dimension_index + 1).is_some() {
911-
let inner_pylist = _postgres_array_to_py(
912-
py,
913-
dimensions,
914-
&data[lower_bound..next_dimension.len as usize + lower_bound],
915-
dimension_index + 1,
916-
0,
917-
);
918-
final_list.append(inner_pylist).unwrap();
919-
lower_bound += next_dimension.len as usize;
920-
};
921-
}
906+
let current_dimension = dimensions.get(dimension_index);
907+
908+
if let Some(current_dimension) = current_dimension {
909+
let possible_next_dimension = dimensions.get(dimension_index + 1);
910+
match possible_next_dimension {
911+
Some(next_dimension) => {
912+
let final_list = PyList::empty_bound(py);
913+
914+
for _ in 0..current_dimension.len as usize {
915+
if dimensions.get(dimension_index + 1).is_some() {
916+
let inner_pylist = _postgres_array_to_py(
917+
py,
918+
dimensions,
919+
&data[lower_bound..next_dimension.len as usize + lower_bound],
920+
dimension_index + 1,
921+
0,
922+
);
923+
final_list.append(inner_pylist).unwrap();
924+
lower_bound += next_dimension.len as usize;
925+
};
926+
}
922927

923-
final_list.unbind()
924-
}
925-
None => {
926-
return PyList::new_bound(py, data).unbind();
928+
return final_list.unbind();
929+
}
930+
None => {
931+
return PyList::new_bound(py, data).unbind();
932+
}
927933
}
928934
}
935+
936+
return PyList::empty_bound(py).unbind();
929937
}
930938

931939
#[allow(clippy::too_many_lines)]

0 commit comments

Comments
 (0)