Skip to content

Commit d1fd6e8

Browse files
authored
Merge pull request #66 from influxdata/crepererum/simplify_array_conversion
refactor: simplify array conversion
2 parents 8793ce4 + 6c79882 commit d1fd6e8

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

guests/python/src/conversion.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,37 +52,35 @@ impl PythonType {
5252
Self::Bool => {
5353
let array = as_boolean_array(array)?;
5454

55-
let it = (0..array.len()).map(move |idx| {
56-
if array.is_null(idx) {
57-
Ok(None)
58-
} else {
59-
let val = array.value(idx);
60-
match val.into_bound_py_any(py) {
61-
Ok(val) => Ok(Some(val)),
62-
Err(e) => Err(exec_datafusion_err!(
63-
"cannot convert Rust `bool` value to Python: {e}"
64-
)),
65-
}
66-
}
55+
let it = array.into_iter().map(move |maybe_val| {
56+
maybe_val
57+
.map(|val| {
58+
val.into_bound_py_any(py).map_err(|e| {
59+
exec_datafusion_err!(
60+
"cannot convert Rust `bool` value to Python: {e}"
61+
)
62+
})
63+
})
64+
.transpose()
6765
});
66+
6867
Ok(Box::new(it))
6968
}
7069
Self::Int => {
7170
let array = as_int64_array(array)?;
7271

73-
let it = (0..array.len()).map(move |idx| {
74-
if array.is_null(idx) {
75-
Ok(None)
76-
} else {
77-
let val = array.value(idx);
78-
match val.into_bound_py_any(py) {
79-
Ok(val) => Ok(Some(val)),
80-
Err(e) => Err(exec_datafusion_err!(
81-
"cannot convert Rust `i64` value to Python: {e}"
82-
)),
83-
}
84-
}
72+
let it = array.into_iter().map(move |maybe_val| {
73+
maybe_val
74+
.map(|val| {
75+
val.into_bound_py_any(py).map_err(|e| {
76+
exec_datafusion_err!(
77+
"cannot convert Rust `i64` value to Python: {e}"
78+
)
79+
})
80+
})
81+
.transpose()
8582
});
83+
8684
Ok(Box::new(it))
8785
}
8886
}

0 commit comments

Comments
 (0)