8000 Add array_empty (#931) · konjac/datafusion-python@0bc2f31 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0bc2f31

Browse files
authored
Add array_empty (apache#931)
1 parent 7007e02 commit 0bc2f31

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

docs/source/user-guide/common-operations/expressions.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,25 @@ approaches.
7777
df = ctx.from_pydict({"a": [[1, 2, 3], [4, 5, 6]]})
7878
df.select(col("a")[0].alias("a0"))
7979
80-
8180
.. warning::
8281

8382
Indexing an element of an array via ``[]`` starts at index 0 whereas
8483
:py:func:`~datafusion.functions.array_element` starts at index 1.
8584

85+
To check if an array is empty, you can use the function :py:func:`datafusion.functions.array_empty`.
86+
This function returns a boolean indicating whether the array is empty.
87+
88+
.. ipython:: python
89+
90+
from datafusion import SessionContext, col
91+
from datafusion.functions import array_empty
92+
93+
ctx = SessionContext()
94+
df = ctx.from_pydict({"a": [[], [1, 2, 3]]})
95+
df.select(array_empty(col("a")).alias("is_empty"))
96+
97+
In this example, the `is_empty` column will contain `True` for the first row and `False` for the second row.
98+
8699
Structs
87100
-------
88101

python/datafusion/functions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"array_dims",
5252
"array_distinct",
5353
"array_element",
54+
"array_empty",
5455
"array_except",
5556
"array_extract",
5657
"array_has",
@@ -1160,6 +1161,11 @@ def array_element(array: Expr, n: Expr) -> Expr:
11601161
return Expr(f.array_element(array.expr, n.expr))
11611162

11621163

1164+
def array_empty(array: Expr) -> Expr:
1165+
"""Returns a boolean indicating whether the array is empty."""
1166+
return Expr(f.array_empty(array.expr))
1167+
1168+
11631169
def array_extract(array: Expr, n: Expr) -> Expr:
11641170
"""Extracts the element with the index n from the array.
11651171

python/tests/test_functions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ def py_flatten(arr):
309309
lambda col: f.array_element(col, literal(1)),
310310
lambda data: [r[0] for r in data],
311311
],
312+
[
313+
lambda col: f.array_empty(col),
314+
lambda data: [len(r) == 0 for r in data],
315+
],
312316
[
313317
lambda col: f.array_extract(col, literal(1)),
314318
lambda data: [r[0] for r in data],

src/functions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ array_fn!(array_to_string, array delimiter);
572572
array_fn!(array_dims, array);
573573
array_fn!(array_distinct, array);
574574
array_fn!(array_element, array element);
575+
array_fn!(array_empty, array);
575576
array_fn!(array_length, array);
576577
array_fn!(array_has, first_array second_array);
577578
array_fn!(array_has_all, first_array second_array);
@@ -1003,6 +1004,7 @@ pub(crate) fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
10031004
m.add_wrapped(wrap_pyfunction!(array_dims))?;
10041005
m.add_wrapped(wrap_pyfunction!(array_distinct))?;
10051006
m.add_wrapped(wrap_pyfunction!(array_element))?;
1007+
m.add_wrapped(wrap_pyfunction!(array_empty))?;
10061008
m.add_wrapped(wrap_pyfunction!(array_length))?;
10071009
m.add_wrapped(wrap_pyfunction!(array_has))?;
10081010
m.add_wrapped(wrap_pyfunction!(array_has_all))?;

0 commit comments

Comments
 (0)
0