From 097373deabdf76146f5760df3aa64406b0dc6a7f Mon Sep 17 00:00:00 2001 From: Siew Kam Onn Date: Tue, 29 Oct 2024 13:10:00 +0800 Subject: [PATCH 1/2] feat: add `empty` function as alias of array_empty --- docs/source/user-guide/common-operations/expressions.rst | 2 +- python/datafusion/functions.py | 8 ++++++++ python/tests/test_functions.py | 4 ++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/source/user-guide/common-operations/expressions.rst b/docs/source/user-guide/common-operations/expressions.rst index 23430d359..b2a83c89f 100644 --- a/docs/source/user-guide/common-operations/expressions.rst +++ b/docs/source/user-guide/common-operations/expressions.rst @@ -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 diff --git a/python/datafusion/functions.py b/python/datafusion/functions.py index e67ba4ae4..469651c75 100644 --- a/python/datafusion/functions.py +++ b/python/datafusion/functions.py @@ -125,6 +125,7 @@ "decode", "degrees", "digest", + "empty", "encode", "ends_with", "exp", @@ -1522,6 +1523,13 @@ 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, diff --git a/python/tests/test_functions.py b/python/tests/test_functions.py index 37943e57c..c65c633a4 100644 --- a/python/tests/test_functions.py +++ b/python/tests/test_functions.py @@ -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], From d62f049d5f32378131d1b37a29b4b73d98749e02 Mon Sep 17 00:00:00 2001 From: Siew Kam Onn Date: Tue, 29 Oct 2024 17:09:24 +0800 Subject: [PATCH 2/2] fix: correct typo in null_treatment parameter documentation --- python/datafusion/functions.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/python/datafusion/functions.py b/python/datafusion/functions.py index 469651c75..907f801af 100644 --- a/python/datafusion/functions.py +++ b/python/datafusion/functions.py @@ -1524,9 +1524,7 @@ def cardinality(array: Expr) -> Expr: def empty(array: Expr) -> Expr: - """ - This is an alias for :py:func:`array_empty`. - """ + """This is an alias for :py:func:`array_empty`.""" return array_empty(array) @@ -2148,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 @@ -2180,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 @@ -2214,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