Skip to content

Commit 1e3fbf2

Browse files
Dilli-Babu-Godarigodaridillibabu
authored andcommitted
Add array_sort() and array_sort_desc() with lambda support for key extraction
This commit introduces a new overloaded functions 1. array_sort() that accepts an array and a lambda expression to extract sort keys, then sorts the array in ascending order based on those keys. 2. array_sort_desc() that accepts an array and a lambda expression to extract sort keys, then sorts the array in descending order based on those keys. Such as, array_sort(ARRAY['hello', 'hi', 'world'], x -> length(x)) -- Returns: ['hi', 'hello', 'world'] array_sort(ARRAY[row('apples', 23), row('bananas', 12)], x -> x[2]) -- Returns: [row('bananas', 12), row('apples', 23)] array_sort_desc(ARRAY['hello', 'hi', 'world'], x -> length(x)) -- Returns: ['hello', 'world', 'hi'] array_sort_desc(ARRAY[row('apples', 23), row('bananas', 12)], x -> x[2]) -- Returns: [row('apples', 23), row('bananas', 12)] The implementation leverages the same code generation approach to optimize key extraction based on element and key types.
1 parent ecf1344 commit 1e3fbf2

6 files changed

Lines changed: 820 additions & 4 deletions

File tree

presto-docs/src/main/sphinx/functions/array.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ Array Functions
198198
-1,
199199
IF(cardinality(x) = cardinality(y), 0, 1))); -- [[1, 2], [2, 3, 1], [4, 2, 1, 4]]
200200

201+
.. function:: array_sort(array(T), function(T,U)) -> array(T)
202+
203+
Sorts and returns the ``array`` using a lambda function to extract sorting keys. The function is applied
204+
to each element of the array to produce a key, and the array is sorted based on these keys in ascending order.
205+
Null array elements and null keys are placed at the end. ::
206+
207+
SELECT array_sort(ARRAY['pear', 'apple', 'banana', 'kiwi'], x -> length(x)); -- ['pear', 'kiwi', 'apple', 'banana']
208+
SELECT array_sort(ARRAY[5, 20, 3, 9, 100], x -> x); -- [3, 5, 9, 20, 100]
209+
SELECT array_sort(ARRAY['apple', NULL, 'banana', NULL], x -> length(x)); -- ['apple', 'banana', NULL, NULL]
210+
SELECT array_sort(ARRAY[CAST(0.0 AS DOUBLE), CAST('NaN' AS DOUBLE), CAST('Infinity' AS DOUBLE), CAST('-Infinity' AS DOUBLE)], x -> x); -- [-Infinity, 0.0, Infinity, NaN]
211+
SELECT array_sort(ARRAY[ROW('a', 3), ROW('b', 1), ROW('c', 2)], x -> x[2]); -- [ROW('b', 1), ROW('c', 2), ROW('a', 3)]
212+
201213
.. function:: array_sort_desc(x) -> array
202214

203215
Returns the ``array`` sorted in the descending order. Elements of the ``array`` must be orderable.
@@ -207,6 +219,18 @@ Array Functions
207219
SELECT array_sort_desc(ARRAY [null, 100, null, 1, 10, 50]); -- [100, 50, 10, 1, null, null]
208220
SELECT array_sort_desc(ARRAY [ARRAY ["a", null], null, ARRAY ["a"]); -- [["a", null], ["a"], null]
209221

222+
.. function:: array_sort_desc(array(T), function(T,U)) -> array(T)
223+
224+
Sorts and returns the ``array`` in descending order using a lambda function to extract sorting keys.
225+
The function is applied to each element of the array to produce a key, and the array is sorted based
226+
on these keys in descending order. Null array elements and null keys are placed at the end. ::
227+
228+
SELECT array_sort_desc(ARRAY['pear', 'apple', 'banana', 'kiwi'], x -> length(x)); -- ['banana', 'apple', 'pear', 'kiwi']
229+
SELECT array_sort_desc(ARRAY[5, 20, 3, 9, 100], x -> x); -- [100, 20, 9, 5, 3]
230+
SELECT array_sort_desc(ARRAY['apple', NULL, 'banana', NULL], x -> length(x)); -- ['banana', 'apple', NULL, NULL]
231+
SELECT array_sort_desc(ARRAY[CAST(0.0 AS DOUBLE), CAST('NaN' AS DOUBLE), CAST('Infinity' AS DOUBLE), CAST('-Infinity' AS DOUBLE)], x -> x); -- [NaN, Infinity, 0.0, -Infinity]
232+
SELECT array_sort_desc(ARRAY[ROW('a', 3), ROW('b', 1), ROW('c', 2)], x -> x[2]); -- [ROW('a', 3), ROW('c', 2), ROW('b', 1)]
233+
210234
.. function:: array_split_into_chunks(array(T), int) -> array(array(T))
211235

212236
Returns an ``array`` of arrays splitting the input ``array`` into chunks of given length.

presto-main-base/src/main/java/com/facebook/presto/metadata/BuiltInTypeAndFunctionNamespaceManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
import com.facebook.presto.operator.aggregation.sketch.kll.KllSketchAggregationFunction;
109109
import com.facebook.presto.operator.aggregation.sketch.kll.KllSketchWithKAggregationFunction;
110110
import com.facebook.presto.operator.aggregation.sketch.theta.ThetaSketchAggregationFunction;
111+
import com.facebook.presto.operator.scalar.AbstractArraySortByKeyFunction;
111112
import com.facebook.presto.operator.scalar.ArrayAllMatchFunction;
112113
import com.facebook.presto.operator.scalar.ArrayAnyMatchFunction;
113114
import com.facebook.presto.operator.scalar.ArrayCardinalityFunction;
@@ -883,6 +884,8 @@ private List<? extends SqlFunction> getBuiltInFunctions(FunctionsConfig function
883884
.scalar(ArrayGreaterThanOrEqualOperator.class)
884885
.scalar(ArrayElementAtFunction.class)
885886
.scalar(ArraySortFunction.class)
887+
.function(AbstractArraySortByKeyFunction.ArraySortByKeyFunction.ARRAY_SORT_BY_KEY_FUNCTION)
888+
.function(AbstractArraySortByKeyFunction.ArraySortDescByKeyFunction.ARRAY_SORT_DESC_BY_KEY_FUNCTION)
886889
.scalar(MapSubsetFunction.class)
887890
.scalar(ArraySortComparatorFunction.class)
888891
.scalar(ArrayShuffleFunction.class)

0 commit comments

Comments
 (0)