Skip to content
Open
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
12 changes: 12 additions & 0 deletions pykusto/_src/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,18 @@ def array_slice(array: ArrayType, start: NumberType, end: NumberType) -> _ArrayE
"""
return _ArrayExpression(KQL(f'array_slice({_to_kql(array)}, {_to_kql(start)}, {_to_kql(end)})'))

@staticmethod
def array_sort_asc(array1: ArrayType, nulls_last: BooleanType = None, *more_arrays: ArrayType) -> _ArrayExpression:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the order of the variables is a bit confusing, due to the mismatch between KQL and python.
My suggestion is to use instead:
def array_sort_asc(to_sort: Union[ArrayType, List[ArrayType]], nulls_last: BooleanType = None) -> _ArrayExpression:...

and then handle to_sort according to type.
WDYT?
also @ymost

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little known Python 3 feature: you can put a keyword argument after a varargs argument, and then it becomes a keyword-only argument.
So we can do this:
def array_sort_asc(*arrays: ArrayType, nulls_last: BooleanType = True) -> _ArrayExpression:
The only down side is that the user will be forced to use keyword notation (e.g. nulls_last=False ).
Also note that per the Kusto default, our default should be 'True'.

"""
https://docs.microsoft.com/en-us/azure/data-explorer/kusto/query/arraysortascfunction
"""
res = f'array_sort_asc({_to_kql(array1)}'
if len(more_arrays) > 0:
res = res + ', ' + ', '.join(_to_kql(array) for array in more_arrays)
if nulls_last is not None:
res = res + ', ' + f'{_to_kql(nulls_last)}'
return _ArrayExpression(KQL(res + ')'))

@staticmethod
def array_split(array: ArrayType, indices: Union[NumberType, ArrayType]) -> _ArrayExpression:
"""
Expand Down
16 changes: 16 additions & 0 deletions test/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,22 @@ def test_array_slice(self):
Query().extend(foo=f.array_slice(t.arrayField, t.numField, t.numField2)).render()
)

def test_array_sort_asc(self):
self.assertEqual(
' | extend foo = array_sort_asc(arrayField1, arrayField2, boolField)',
Query().extend(foo=f.array_sort_asc(t.arrayField1, t.boolField, t.arrayField2)).render())
self.assertEqual(
' | extend foo = array_sort_asc(arrayField1, arrayField2)',
Query().extend(foo=f.array_sort_asc(t.arrayField1, t.arrayField2)).render())
self.assertEqual(
' | extend foo = array_sort_asc(arrayField1, boolField)',
Query().extend(foo=f.array_sort_asc(t.arrayField1, t.boolField)).render()
)
self.assertEqual(
' | extend foo = array_sort_asc(arrayField1)',
Query().extend(foo=f.array_sort_asc(t.arrayField1)).render()
)

def test_array_split(self):
self.assertEqual(
' | extend foo = array_split(arrayField, numField)',
Expand Down