179289f0bf
### What changes were proposed in this pull request? This PR intends to clean up the SQL documents in `doc/sql-ref*`. Main changes are as follows; - Fixes wrong syntaxes and capitalize sub-titles - Adds some DDL queries in `Examples` so that users can run examples there - Makes query output in `Examples` follows the `Dataset.showString` (right-aligned) format - Adds/Removes spaces, Indents, or blank lines to follow the format below; ``` --- license... --- ### Description Writes what's the syntax is. ### Syntax {% highlight sql %} SELECT... WHERE... // 4 indents after the second line ... {% endhighlight %} ### Parameters <dl> <dt><code><em>Param Name</em></code></dt> <dd> Param Description </dd> ... </dl> ### Examples {% highlight sql %} -- It is better that users are able to execute example queries here. -- So, we prepare test data in the first section if possible. CREATE TABLE t (key STRING, value DOUBLE); INSERT INTO t VALUES ('a', 1.0), ('a', 2.0), ('b', 3.0), ('c', 4.0); -- query output has 2 indents and it follows the `Dataset.showString` -- format (right-aligned). SELECT * FROM t; +---+-----+ |key|value| +---+-----+ | a| 1.0| | a| 2.0| | b| 3.0| | c| 4.0| +---+-----+ -- Query statements after the second line have 4 indents. SELECT key, SUM(value) FROM t GROUP BY key; +---+----------+ |key|sum(value)| +---+----------+ | c| 4.0| | b| 3.0| | a| 3.0| +---+----------+ ... {% endhighlight %} ### Related Statements * [XXX](xxx.html) * ... ``` ### Why are the changes needed? The most changes of this PR are pretty minor, but I think the consistent formats/rules to write documents are important for long-term maintenance in our community ### Does this PR introduce any user-facing change? Yes. ### How was this patch tested? Manually checked. Closes #28151 from maropu/MakeRightAligned. Authored-by: Takeshi Yamamuro <yamamuro@apache.org> Signed-off-by: Sean Owen <srowen@gmail.com>
187 lines
6.4 KiB
Markdown
187 lines
6.4 KiB
Markdown
---
|
|
layout: global
|
|
title: SORT BY Clause
|
|
displayTitle: SORT BY Clause
|
|
license: |
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
this work for additional information regarding copyright ownership.
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
(the "License"); you may not use this file except in compliance with
|
|
the License. You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
---
|
|
|
|
### Description
|
|
|
|
The <code>SORT BY</code> clause is used to return the result rows sorted
|
|
within each partition in the user specified order. When there is more than one partition
|
|
<code>SORT BY</code> may return result that is partially ordered. This is different
|
|
than [ORDER BY](sql-ref-syntax-qry-select-orderby.html) clause which guarantees a
|
|
total order of the output.
|
|
|
|
### Syntax
|
|
|
|
{% highlight sql %}
|
|
SORT BY { expression [ sort_direction | nulls_sort_order ] [ , ... ] }
|
|
{% endhighlight %}
|
|
|
|
### Parameters
|
|
|
|
<dl>
|
|
<dt><code><em>SORT BY</em></code></dt>
|
|
<dd>
|
|
Specifies a comma-separated list of expressions along with optional parameters <code>sort_direction</code>
|
|
and <code>nulls_sort_order</code> which are used to sort the rows within each partition.
|
|
</dd>
|
|
<dt><code><em>sort_direction</em></code></dt>
|
|
<dd>
|
|
Optionally specifies whether to sort the rows in ascending or descending
|
|
order. The valid values for the sort direction are <code>ASC</code> for ascending
|
|
and <code>DESC</code> for descending. If sort direction is not explicitly specified, then by default
|
|
rows are sorted ascending. <br><br>
|
|
<b>Syntax:</b>
|
|
<code>
|
|
[ ASC | DESC ]
|
|
</code>
|
|
</dd>
|
|
<dt><code><em>nulls_sort_order</em></code></dt>
|
|
<dd>
|
|
Optionally specifies whether NULL values are returned before/after non-NULL values. If
|
|
<code>null_sort_order</code> is not specified, then NULLs sort first if sort order is
|
|
<code>ASC</code> and NULLS sort last if sort order is <code>DESC</code>.<br><br>
|
|
<ol>
|
|
<li> If <code>NULLS FIRST</code> is specified, then NULL values are returned first
|
|
regardless of the sort order.</li>
|
|
<li>If <code>NULLS LAST</code> is specified, then NULL values are returned last regardless of
|
|
the sort order. </li>
|
|
</ol><br>
|
|
<b>Syntax:</b>
|
|
<code>
|
|
[ NULLS { FIRST | LAST } ]
|
|
</code>
|
|
</dd>
|
|
</dl>
|
|
|
|
### Examples
|
|
|
|
{% highlight sql %}
|
|
CREATE TABLE person (zip_code INT, name STRING, age INT);
|
|
INSERT INTO person 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;
|
|
+--------+----+--------+
|
|
| name| age|zip_code|
|
|
+--------+----+--------+
|
|
| 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;
|
|
+--------+----+--------+
|
|
| name| age|zip_code|
|
|
+--------+----+--------+
|
|
| 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;
|
|
+----+--------+--------+
|
|
| age| name|zip_code|
|
|
+----+--------+--------+
|
|
| 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;
|
|
+----+--------+--------+
|
|
| age| name|zip_code|
|
|
+----+--------+--------+
|
|
| 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;
|
|
+----+--------+--------+
|
|
| age| name|zip_code|
|
|
+----+--------+--------+
|
|
|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;
|
|
+--------+----+--------+
|
|
| name| age|zip_code|
|
|
+--------+----+--------+
|
|
| 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|
|
|
+--------+----+--------+
|
|
{% endhighlight %}
|
|
|
|
### Related Statements
|
|
|
|
* [SELECT Main](sql-ref-syntax-qry-select.html)
|
|
* [WHERE Clause](sql-ref-syntax-qry-select-where.html)
|
|
* [GROUP BY Clause](sql-ref-syntax-qry-select-groupby.html)
|
|
* [HAVING Clause](sql-ref-syntax-qry-select-having.html)
|
|
* [ORDER BY Clause](sql-ref-syntax-qry-select-orderby.html)
|
|
* [CLUSTER BY Clause](sql-ref-syntax-qry-select-clusterby.html)
|
|
* [DISTRIBUTE BY Clause](sql-ref-syntax-qry-select-distribute-by.html)
|
|
* [LIMIT Clause](sql-ref-syntax-qry-select-limit.html)
|