SORT BY 子句
適用於:Databricks SQL
Databricks Runtime
傳回以使用者指定順序排序之每個 Spark 資料分割內的結果數據列。
當數據分散到多個Spark分割區時,SORT BY
可能會傳回部分排序的結果。
若要明確控制資料分割成 Spark 分割區的方式,請使用 REPARTITION hint
。
這與 ORDER BY 子句不同,不論 Spark 如何分割數據,都保證完全排序的輸出。
語法
SORT BY { expression [ sort_direction nulls_sort_oder ] } [, ...]
sort_direction
[ ASC | DEC ]
nulls_sort_order
[ NULLS FIRST | NULLS LAST ]
參數
-
任何類型的表達式,用於建立傳回結果時的分區局部順序。
如果表達式是一個字面 INT 值,則會被解釋為選取清單中的欄位位置。
sort_direction
指定依表達式排序的排序順序。
-
ASC
:此表達式的排序方向為遞增。 -
DESC
:此表達式的排序順序為遞減。
如果未明確指定排序方向,則預設數據列會以遞增方式排序。
-
nulls_sort_order
選擇性地指定是否在非 NULL 值之前/之後傳回 NULL 值。 如果未
null_sort_order
指定 ,則如果排序順序為 ,則 NULL 會先排序,如果排序順序為ASC
DESC
,則為 NULLS 排序最後一次。-
NULLS FIRST
:不論排序順序為何,都會先傳回NULL值。 -
NULLS LAST
:不論排序順序為何,NULL 值都會在最後被傳回。
-
指定多個表達式排序時,會由左至右進行。 Spark 分割區中的所有數據列都會依第一個表達式排序。 如果第一個表達式有重複的值,則會使用第二個表達式來解析重複專案群組內的順序等等。 如果表達式在所有順序之間有重複的值,則產生的順序不具決定性。
範例
> CREATE TEMP VIEW person (zip_code, name, age)
AS VALUES (94588, 'Zen Hui', 50),
(94588, 'Dan Li', 18),
(94588, 'Anil K', 27),
(94588, 'John V', NULL),
(94511, 'David K', 42),
(94511, 'Aryan B.', 18),
(94511, 'Lalit B.', NULL);
-- Use `REPARTITION` hint to partition the data by `zip_code` to
-- examine the `SORT BY` behavior. This is used in rest of the
-- examples.
-- Sort rows by `name` within each partition in ascending manner
> SELECT /*+ REPARTITION(zip_code) */ name, age, zip_code FROM person
SORT BY name;
Anil K 27 94588
Dan Li 18 94588
John V NULL 94588
Zen Hui 50 94588
Aryan B. 18 94511
David K 42 94511
Lalit B. NULL 94511
-- Sort rows within each partition using column position.
> SELECT /*+ REPARTITION(zip_code) */ name, age, zip_code FROM person
SORT BY 1;
Anil K 27 94588
Dan Li 18 94588
John V null 94588
Zen Hui 50 94588
Aryan B. 18 94511
David K 42 94511
Lalit B. null 94511
-- Sort rows within partition in ascending manner keeping null values to be last.
> SELECT /*+ REPARTITION(zip_code) */ age, name, zip_code FROM person
SORT BY age NULLS LAST;
18 Dan Li 94588
27 Anil K 94588
50 Zen Hui 94588
NULL John V 94588
18 Aryan B. 94511
42 David K 94511
NULL Lalit B. 94511
-- Sort rows by age within each partition in descending manner, which defaults to NULL LAST.
> SELECT /*+ REPARTITION(zip_code) */ age, name, zip_code FROM person
SORT BY age DESC;
50 Zen Hui 94588
27 Anil K 94588
18 Dan Li 94588
NULL John V 94588
42 David K 94511
18 Aryan B. 94511
NULL Lalit B. 94511
-- Sort rows by age within each partition in descending manner keeping null values to be first.
> SELECT /*+ REPARTITION(zip_code) */ age, name, zip_code FROM person
SORT BY age DESC NULLS FIRST;
NULL John V 94588
50 Zen Hui 94588
27 Anil K 94588
18 Dan Li 94588
NULL Lalit B. 94511
42 David K 94511
18 Aryan B. 94511
-- Sort rows within each partition based on more than one column with each column having
-- different sort direction.
> SELECT /*+ REPARTITION(zip_code) */ name, age, zip_code FROM person
SORT BY name ASC, age DESC;
Anil K 27 94588
Dan Li 18 94588
John V null 94588
Zen Hui 50 94588
Aryan B. 18 94511
David K 42 94511
Lalit B. null 94511