Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 8a24cf2

Browse files
dilipbiswalsrowen
authored andcommitted
[SPARK-30588][DOC] Document CLUSTER BY Clause of SELECT statement in SQL Reference
### What changes were proposed in this pull request? Document CLUSTER BY clause of SELECT statement in SQL Reference Guide. ### Why are the changes needed? Currently Spark lacks documentation on the supported SQL constructs causing confusion among users who sometimes have to look at the code to understand the usage. This is aimed at addressing this issue. ### Does this PR introduce any user-facing change? Yes. **Before:** There was no documentation for this. **After.** <img width="972" alt="Screen Shot 2020-01-20 at 2 59 05 PM" src="https://user-images.githubusercontent.com/14225158/72762704-7528de80-3b95-11ea-9d34-8fa0ab63d4c0.png"> <img width="972" alt="Screen Shot 2020-01-20 at 2 59 19 PM" src="https://user-images.githubusercontent.com/14225158/72762710-78bc6580-3b95-11ea-8279-2848d3b9e619.png"> ### How was this patch tested? Tested using jykyll build --serve Closes apache#27297 from dilipbiswal/sql-ref-select-clusterby. Authored-by: Dilip Biswal <[email protected]> Signed-off-by: Sean Owen <[email protected]>
1 parent 5781e57 commit 8a24cf2

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
layout: global
3+
title: CLUSTER BY Clause
4+
displayTitle: CLUSTER BY Clause
5+
license: |
6+
Licensed to the Apache Software Foundation (ASF) under one or more
7+
contributor license agreements. See the NOTICE file distributed with
8+
this work for additional information regarding copyright ownership.
9+
The ASF licenses this file to You under the Apache License, Version 2.0
10+
(the "License"); you may not use this file except in compliance with
11+
the License. You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
---
21+
The <code>CLUSTER BY</code> clause is used to first repartition the data based
22+
on the input expressions and then sort the data within each partition. This is
23+
semantically equivalent to performing a <code>DISTRIBUTE BY</code> followed by
24+
a <code>SORT BY</code>. This clause only ensures that the resultant rows are
25+
sorted within each partition and does not guarantee a total order of output.
26+
27+
### Syntax
28+
{% highlight sql %}
29+
CLUSTER BY { expression [ , ... ] }
30+
{% endhighlight %}
31+
32+
### Parameters
33+
<dl>
34+
<dt><code><em>expression</em></code></dt>
35+
<dd>
36+
Specifies combination of one or more values, operators and SQL functions that results in a value.
37+
</dd>
38+
</dl>
39+
40+
### Examples
41+
{% highlight sql %}
42+
CREATE TABLE person (name STRING, age INT);
43+
INSERT INTO person VALUES
44+
('Zen Hui', 25),
45+
('Anil B', 18),
46+
('Shone S', 16),
47+
('Mike A', 25),
48+
('John A', 18),
49+
('Jack N', 16);
50+
51+
-- Reduce the number of shuffle partitions to 2 to illustrate the behavior of `CLUSTER BY`.
52+
-- It's easier to see the clustering and sorting behavior with less number of partitions.
53+
SET spark.sql.shuffle.partitions = 2;
54+
55+
-- Select the rows with no ordering. Please note that without any sort directive, the results
56+
-- of the query is not deterministic. It's included here to show the difference in behavior
57+
-- of a query when `CLUSTER BY` is not used vs when it's used. The query below produces rows
58+
-- where age column is not sorted.
59+
SELECT age, name FROM person;
60+
61+
+---+-------+
62+
|age|name |
63+
+---+-------+
64+
|16 |Shone S|
65+
|25 |Zen Hui|
66+
|16 |Jack N |
67+
|25 |Mike A |
68+
|18 |John A |
69+
|18 |Anil B |
70+
+---+-------+
71+
72+
-- Produces rows clustered by age. Persons with same age are clustered together.
73+
-- In the query below, persons with age 18 and 25 are in first partition and the
74+
-- persons with age 16 are in the second partition. The rows are sorted based
75+
-- on age within each partition.
76+
SELECT age, name FROM person CLUSTER BY age;
77+
78+
+---+-------+
79+
|age|name |
80+
+---+-------+
81+
|18 |John A |
82+
|18 |Anil B |
83+
|25 |Zen Hui|
84+
|25 |Mike A |
85+
|16 |Shone S|
86+
|16 |Jack N |
87+
+---+-------+
88+
{% endhighlight %}

0 commit comments

Comments
 (0)