Skip to content

Commit 27a9264

Browse files
authored
feat: Add flatten array function (#562)
* Updated functions.rs, test_functions.py - Flatten * Updated functions.rs - Formatting * Updated test_functions.py - Converted test array to a literal for testing * Updated test_functions.py - Linting
1 parent 697ca2c commit 27a9264

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

datafusion/tests/test_functions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ def py_arr_replace(arr, from_, to, n=None):
238238

239239
return new_arr
240240

241+
def py_flatten(arr):
242+
result = []
243+
for elem in arr:
244+
if isinstance(elem, list):
245+
result.extend(py_flatten(elem))
246+
else:
247+
result.append(elem)
248+
return result
249+
241250
col = column("arr")
242251
test_items = [
243252
[
@@ -432,6 +441,7 @@ def py_arr_replace(arr, from_, to, n=None):
432441
f.list_slice(col, literal(-1), literal(2)),
433442
lambda: [arr[-1:2] for arr in data],
434443
],
444+
[f.flatten(literal(data)), lambda: [py_flatten(data)]],
435445
]
436446

437447
for stmt, py_expr in test_items:

src/functions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ scalar_function!(array_replace_all, ArrayReplaceAll);
409409
scalar_function!(list_replace_all, ArrayReplaceAll);
410410
scalar_function!(array_slice, ArraySlice);
411411
scalar_function!(list_slice, ArraySlice);
412+
scalar_function!(flatten, Flatten);
412413

413414
aggregate_function!(approx_distinct, ApproxDistinct);
414415
aggregate_function!(approx_median, ApproxMedian);
@@ -651,6 +652,7 @@ pub(crate) fn init_module(m: &PyModule) -> PyResult<()> {
651652
m.add_wrapped(wrap_pyfunction!(list_replace_all))?;
652653
m.add_wrapped(wrap_pyfunction!(array_slice))?;
653654
m.add_wrapped(wrap_pyfunction!(list_slice))?;
655+
m.add_wrapped(wrap_pyfunction!(flatten))?;
654656

655657
Ok(())
656658
}

0 commit comments

Comments
 (0)