Skip to content

Fixed runtime error #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading