@@ -136,50 +136,53 @@ Here, the sample of 10% is taken from the second half of data.
### ARRAY JOIN Clause {#select-array-join-clause}
Allows executing JOIN with an array or nested data structure. The intent is similar to the 'arrayJoin' function, but its functionality is broader.
`ARRAY JOIN` is essentially `INNER JOIN` with an array. Example:
Allows executing `JOIN` with an array or nested data structure. Allows you to perform `JOIN` both with the external array and with the inner array in the table. The intent is similar to the [arrayJoin](functions/array_functions.md#array_functions-join) function, but its functionality is broader.
You can specify only a single `ARRAY JOIN` clause in a query.
0 rows in set. Elapsed: 0.001 sec.
When running the `ARRAY JOIN`, there is an optimization of the query execution order. Although the `ARRAY JOIN` must be always specified before the `WHERE/PREWHERE` clause, it can be performed as before the `WHERE/PREWHERE` (if its result is needed in this clause), as after completing it (to reduce the volume of calculations). The processing order is controlled by the query optimizer.
-`ARRAY JOIN` - Executing `JOIN` with an array or nested data structure. Empty arrays are not included in the result.
-`LEFT ARRAY JOIN` - Unlike `ARRAY JOIN`, when using the `LEFT ARRAY JOIN` the result contains the rows with empty arrays. The value for an empty array is set to default value for an array element type (usually 0, empty string or NULL).
Ok.
Examples below demonstrate the usage of the `ARRAY JOIN` clause. Let's create a table with an [Array](../data_types/array.md) type column and insert values into it:
The first example shows using the `ARRAY JOIN` clause:
``` sql
SELECTs,arr
FROMarrays_test
ARRAY JOIN arr
ARRAYJOINarr;
```
```
┌─s─────┬─arr─┐
│ Hello │ 1 │
│ Hello │ 2 │
...
...
@@ -187,19 +190,59 @@ ARRAY JOIN arr
│ World │ 4 │
│ World │ 5 │
└───────┴─────┘
```
5 rows in set. Elapsed: 0.001 sec.
The second example shows using the `LEFT ARRAY JOIN` clause:
``` sql
SELECTs,arr
FROMarrays_test
LEFTARRAYJOINarr;
```
```
┌─s───────────┬─arr─┐
│ Hello │ 1 │
│ Hello │ 2 │
│ World │ 3 │
│ World │ 4 │
│ World │ 5 │
│ Goodbye │ 0 │
└─────────────┴─────┘
```
The next example demonstrates using the `ARRAY JOIN` with the external array:
An alias can be specified for an array in the ARRAY JOIN clause. In this case, an array item can be accessed by this alias, but the array itself by the original name. Example:
``` sql
SELECTs,arr_external
FROMarrays_test
ARRAYJOIN[1,2,3]ASarr_external;
```
```
:) SELECT s, arr, a FROM arrays_test ARRAY JOIN arr AS a
┌─s───────────┬─arr_external─┐
│ Hello │ 1 │
│ Hello │ 2 │
│ Hello │ 3 │
│ World │ 1 │
│ World │ 2 │
│ World │ 3 │
│ Goodbye │ 1 │
│ Goodbye │ 2 │
│ Goodbye │ 3 │
└─────────────┴──────────────┘
```
#### Using Aliases
An alias can be specified for an array in the `ARRAY JOIN` clause. In this case, an array item can be accessed by this alias, but the array itself by the original name. Example:
``` sql
SELECTs,arr,a
FROMarrays_test
ARRAY JOIN arr AS a
ARRAYJOINarrASa;
```
```
┌─s─────┬─arr─────┬─a─┐
│ Hello │ [1,2] │ 1 │
│ Hello │ [1,2] │ 2 │
...
...
@@ -207,19 +250,17 @@ ARRAY JOIN arr AS a
│ World │ [3,4,5] │ 4 │
│ World │ [3,4,5] │ 5 │
└───────┴─────────┴───┘
5 rows in set. Elapsed: 0.001 sec.
```
Multiple arrays of the same size can be comma-separated in the ARRAY JOIN clause. In this case, JOIN is performed with them simultaneously (the direct sum, not the direct product). Example:
```
:) SELECT s, arr, a, num, mapped FROM arrays_test ARRAY JOIN arr AS a, arrayEnumerate(arr) AS num, arrayMap(x -> x + 1, arr) AS mapped
Multiple arrays of the same size can be comma-separated in the `ARRAY JOIN` clause. In this case, `JOIN` is performed with them simultaneously (the direct sum, not the cartesian product). Example:
``` sql
SELECTs,arr,a,num,mapped
FROMarrays_test
ARRAY JOIN arr AS a, arrayEnumerate(arr) AS num, arrayMap(lambda(tuple(x), plus(x, 1)), arr) AS mapped
:) SELECT s, nest.x, nest.y FROM nested_test ARRAY JOIN nest
``` sql
SELECTs,`nest.x`,`nest.y`
FROMnested_test
ARRAY JOIN nest
ARRAYJOINnest;
```
```
┌─s─────┬─nest.x─┬─nest.y─┐
│ Hello │ 1 │ 10 │
│ Hello │ 2 │ 20 │
...
...
@@ -298,19 +325,17 @@ ARRAY JOIN nest
│ World │ 4 │ 40 │
│ World │ 5 │ 50 │
└───────┴────────┴────────┘
5 rows in set. Elapsed: 0.001 sec.
```
When specifying names of nested data structures in ARRAY JOIN, the meaning is the same as ARRAY JOIN with all the array elements that it consists of. Example:
When specifying names of nested data structures in `ARRAY JOIN`, the meaning is the same as `ARRAY JOIN` with all the array elements that it consists of. Examples are listed below:
The query can only specify a single ARRAY JOIN clause.
The corresponding conversion can be performed before the WHERE/PREWHERE clause (if its result is needed in this clause), or after completing WHERE/PREWHERE (to reduce the volume of calculations).
### JOIN Clause {#select-join}
Joins the data in the normal [SQL JOIN](https://en.wikipedia.org/wiki/Join_(SQL)) sense.