Skip to content
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

Add empty scalar function (alias of array_empty), fix a small typo #938

Merged
merged 2 commits into from
Nov 1, 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
2 changes: 1 addition & 1 deletion docs/source/user-guide/common-operations/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ approaches.
Indexing an element of an array via ``[]`` starts at index 0 whereas
:py:func:`~datafusion.functions.array_element` starts at index 1.

To check if an array is empty, you can use the function :py:func:`datafusion.functions.array_empty`.
To check if an array is empty, you can use the function :py:func:`datafusion.functions.array_empty` or `datafusion.functions.empty`.
This function returns a boolean indicating whether the array is empty.

.. ipython:: python
Expand Down
12 changes: 9 additions & 3 deletions python/datafusion/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"decode",
"degrees",
"digest",
"empty",
"encode",
"ends_with",
"exp",
Expand Down Expand Up @@ -1522,6 +1523,11 @@ def cardinality(array: Expr) -> Expr:
return Expr(f.cardinality(array.expr))


def empty(array: Expr) -> Expr:
"""This is an alias for :py:func:`array_empty`."""
return array_empty(array)


# aggregate functions
def approx_distinct(
expression: Expr,
Expand Down Expand Up @@ -2140,7 +2146,7 @@ def first_value(
expression: Argument to perform bitwise calculation on
filter: If provided, only compute against rows for which the filter is True
order_by: Set the ordering of the expression to evaluate
null_treatment: Assign whether to respect or ignull null values.
null_treatment: Assign whether to respect or ignore null values.
"""
order_by_raw = sort_list_to_raw_sort_list(order_by)
filter_raw = filter.expr if filter is not None else None
Expand Down Expand Up @@ -2172,7 +2178,7 @@ def last_value(
expression: Argument to perform bitwise calculation on
filter: If provided, only compute against rows for which the filter is True
order_by: Set the ordering of the expression to evaluate
null_treatment: Assign whether to respect or ignull null values.
null_treatment: Assign whether to respect or ignore null values.
"""
order_by_raw = sort_list_to_raw_sort_list(order_by)
filter_raw = filter.expr if filter is not None else None
Expand Down Expand Up @@ -2206,7 +2212,7 @@ def nth_value(
n: Index of value to return. Starts at 1.
filter: If provided, only compute against rows for which the filter is True
order_by: Set the ordering of the expression to evaluate
null_treatment: Assign whether to respect or ignull null values.
null_treatment: Assign whether to respect or ignore null values.
"""
order_by_raw = sort_list_to_raw_sort_list(order_by)
filter_raw = filter.expr if filter is not None else None
Expand Down
4 changes: 4 additions & 0 deletions python/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ def py_flatten(arr):
lambda col: f.array_empty(col),
lambda data: [len(r) == 0 for r in data],
],
[
lambda col: f.empty(col),
lambda data: [len(r) == 0 for r in data],
],
[
lambda col: f.array_extract(col, literal(1)),
lambda data: [r[0] for r in data],
Expand Down
Loading