|
| 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