从补丁 198 开始,Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息,请参阅博客文章
ARRAY_SORT 函数
按升序或降序创建输入数组的排序版本。您可以指定 NULL 值应在结果中出现的位置。该函数是 NULL 安全的,这意味着它将 NULL 视为已知对象。
语法
ARRAY_SORT( array [, sort_ascending [, nulls_first]] )
参数
- array
-
一个 SUPER 表达式,用于指定要排序的数组。
- sort_ascending
-
一个布尔值,用于指定是按升序还是降序对数组进行排序:
- Specify TRUE to sort the elements in ascending order.
- Specify FALSE to sort the elements in descending order.
默认值为 TRUE。
- nulls_first
-
一个用于指定 NULL 定位的布尔值:
- Specify TRUE to place NULLs at the beginning of the sorted array.
- Specify FALSE to place NULLs at the end of the sorted array.
返回类型
ARRAY_SORT 函数返回 SUPER 类型。
注意
当对包含混合数据类型的数组进行排序时,将根据以下类型优先顺序对元素进行排序:
- Boolean values
- Numeric values
- String values
- Arrays
- Objects/Dictionaries
在每个类型类别中,元素根据其自然顺序进行排序(例如,数字按数值大小排序,字符串按字母顺序排序)。
示例
以下示例显示 ARRAY_SORT 函数。
-- Ascending order (default) SELECT ARRAY_SORT(ARRAY('b', 'a', 0, NULL, 1, false)); array_sort -------------------------- [false,0,1,"a","b",null] (1 row) -- Descending order SELECT ARRAY_SORT(ARRAY('b', 'a', 0, NULL, 1, false), False); array_sort -------------------------- [null,"b","a",1,0,false] (1 row) -- Descending order with NULLs at the end of the sorted array SELECT ARRAY_SORT(ARRAY('b', 'a', 0, NULL, 1, false), False, False); array_sort -------------------------- ["b","a",1,0,false,null] (1 row)