Skip to content

Commit

Permalink
Merge pull request #87 from psqlpy-python/feature/runtime_error_with_…
Browse files Browse the repository at this point in the history
…empty_array

Fixed runtime error
  • Loading branch information
chandr-andr authored Sep 21, 2024
2 parents e8f118a + fedc36d commit 8e2e08a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 50 deletions.
19 changes: 19 additions & 0 deletions python/tests/test_value_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,3 +1001,22 @@ async def test_incorrect_dimensions_array(
],
],
)


async def test_empty_array(
psql_pool: ConnectionPool,
) -> None:
await psql_pool.execute("DROP TABLE IF EXISTS test_earr")
await psql_pool.execute(
"CREATE TABLE test_earr (id serial NOT NULL PRIMARY KEY, e_array text[] NOT NULL DEFAULT array[]::text[])",
)

await psql_pool.execute("INSERT INTO test_earr(id) VALUES(2);")

res = await psql_pool.execute(
"SELECT * FROM test_earr WHERE id = 2",
)

json_result = res.result()
assert json_result
assert not json_result[0]["e_array"]
108 changes: 58 additions & 50 deletions src/value_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,35 +835,39 @@ fn _pythondto_array_to_serde(
dimension_index: usize,
mut lower_bound: usize,
) -> RustPSQLDriverPyResult<Value> {
let current_dimension = dimensions.get(dimension_index).unwrap();

let possible_next_dimension = dimensions.get(dimension_index + 1);
match possible_next_dimension {
Some(next_dimension) => {
let mut final_list: Value = Value::Array(vec![]);

for _ in 0..current_dimension.len as usize {
if dimensions.get(dimension_index + 1).is_some() {
let inner_pylist = _pythondto_array_to_serde(
dimensions,
&data[lower_bound..next_dimension.len as usize + lower_bound],
dimension_index + 1,
0,
)?;
match final_list {
Value::Array(ref mut array) => array.push(inner_pylist),
_ => unreachable!(),
}
lower_bound += next_dimension.len as usize;
};
}
let current_dimension = dimensions.get(dimension_index);

if let Some(current_dimension) = current_dimension {
let possible_next_dimension = dimensions.get(dimension_index + 1);
match possible_next_dimension {
Some(next_dimension) => {
let mut final_list: Value = Value::Array(vec![]);

for _ in 0..current_dimension.len as usize {
if dimensions.get(dimension_index + 1).is_some() {
let inner_pylist = _pythondto_array_to_serde(
dimensions,
&data[lower_bound..next_dimension.len as usize + lower_bound],
dimension_index + 1,
0,
)?;
match final_list {
Value::Array(ref mut array) => array.push(inner_pylist),
_ => unreachable!(),
}
lower_bound += next_dimension.len as usize;
};
}

Ok(final_list)
}
None => {
return data.iter().map(|x| x.to_serde_value()).collect();
return Ok(final_list);
}
None => {
return data.iter().map(|x| x.to_serde_value()).collect();
}
}
}

Ok(Value::Array(vec![]))
}

/// Convert rust array to python list.
Expand Down Expand Up @@ -899,33 +903,37 @@ fn _postgres_array_to_py<T>(
where
T: ToPyObject,
{
let current_dimension = dimensions.get(dimension_index).unwrap();

let possible_next_dimension = dimensions.get(dimension_index + 1);
match possible_next_dimension {
Some(next_dimension) => {
let final_list = PyList::empty_bound(py);

for _ in 0..current_dimension.len as usize {
if dimensions.get(dimension_index + 1).is_some() {
let inner_pylist = _postgres_array_to_py(
py,
dimensions,
&data[lower_bound..next_dimension.len as usize + lower_bound],
dimension_index + 1,
0,
);
final_list.append(inner_pylist).unwrap();
lower_bound += next_dimension.len as usize;
};
}
let current_dimension = dimensions.get(dimension_index);

if let Some(current_dimension) = current_dimension {
let possible_next_dimension = dimensions.get(dimension_index + 1);
match possible_next_dimension {
Some(next_dimension) => {
let final_list = PyList::empty_bound(py);

for _ in 0..current_dimension.len as usize {
if dimensions.get(dimension_index + 1).is_some() {
let inner_pylist = _postgres_array_to_py(
py,
dimensions,
&data[lower_bound..next_dimension.len as usize + lower_bound],
dimension_index + 1,
0,
);
final_list.append(inner_pylist).unwrap();
lower_bound += next_dimension.len as usize;
};
}

final_list.unbind()
}
None => {
return PyList::new_bound(py, data).unbind();
return final_list.unbind();
}
None => {
return PyList::new_bound(py, data).unbind();
}
}
}

return PyList::empty_bound(py).unbind();
}

#[allow(clippy::too_many_lines)]
Expand Down

0 comments on commit 8e2e08a

Please sign in to comment.