-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy patharrayJoin.cpp
More file actions
276 lines (251 loc) · 10 KB
/
Copy patharrayJoin.cpp
File metadata and controls
276 lines (251 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <Functions/IFunction.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/FunctionFactory.h>
#include <DataTypes/DataTypeArray.h>
#include <Interpreters/ArrayJoinAction.h>
namespace DB
{
namespace ErrorCodes
{
extern const int FUNCTION_IS_SPECIAL;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
/** arrayJoin(arr) - a special function - it can not be executed directly;
* is used only to get the result type of the corresponding expression.
*/
class FunctionArrayJoin final : public IFunction
{
public:
static constexpr auto name = "arrayJoin";
static FunctionPtr create(ContextPtr)
{
return std::make_shared<FunctionArrayJoin>();
}
/// Get the function name.
String getName() const override
{
return name;
}
size_t getNumberOfArguments() const override
{
return 1;
}
/** It could return many different values for single argument. */
bool isDeterministic() const override
{
return false;
}
bool isDeterministicInScopeOfQuery() const override
{
return false;
}
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
const auto & arr = getArrayJoinDataType(arguments[0]);
if (!arr)
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Argument for function {} must be Array or Map", getName());
return arr->getNestedType();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t /*input_rows_count*/) const override
{
throw Exception(ErrorCodes::FUNCTION_IS_SPECIAL, "Function {} must not be executed directly.", getName());
}
/// Because of function cannot be executed directly.
bool isSuitableForConstantFolding() const override
{
return false;
}
};
REGISTER_FUNCTION(ArrayJoin)
{
FunctionDocumentation::Description description = R"(
The `arrayJoin` function takes a row that contains an array and unfolds it, generating multiple rows – one for each element in the array.
This is in contrast to Regular Functions in ClickHouse which map input values to output values within the same row,
and Aggregate Functions which take a group of rows and "compress" or "reduce" them into a single summary row
(or a single value within a summary row if used with `GROUP BY`).
All the values in the columns are simply copied, except the values in the column where this function is applied;
these are replaced with the corresponding array value.
`unnest` (since version 26.5) is a case-insensitive alias of `arrayJoin` in function-call form (`SELECT unnest(arr)`). PostgreSQL table-source syntax is not supported (`FROM unnest(...)`, `CROSS JOIN UNNEST(...)`, and `LATERAL`). Use the `ARRAY JOIN` clause for those queries.
`arrayJoin`, including via its `unnest` alias, cannot be used in a `JOIN ON` condition that is evaluated during the join, because such a condition must
preserve the number of rows. A condition that applies to one side only, and an equality key over `arrayJoin`, are
extracted before the join and are unaffected. A non-disjunctive `ALL INNER JOIN` condition is also unaffected,
because there the condition is applied after the join instead.
Where the expansion depends on one side only, move it into an `ARRAY JOIN` in a subquery before the join; a condition whose `arrayJoin` argument reads columns from both sides has to be restructured.
)";
FunctionDocumentation::Syntax syntax = "arrayJoin(arr)";
FunctionDocumentation::Arguments arguments = {
{"arr", "An array to unfold.", {"Array(T)"}}
};
FunctionDocumentation::ReturnedValue returned_value = {"Returns a set of rows unfolded from `arr`."};
FunctionDocumentation::Examples examples = {
{"Basic usage", R"(SELECT arrayJoin([1, 2, 3] AS src) AS dst, 'Hello', src)", R"(
┌─dst─┬─'Hello'─┬─src─────┐
│ 1 │ Hello │ [1,2,3] │
│ 2 │ Hello │ [1,2,3] │
│ 3 │ Hello │ [1,2,3] │
└─────┴─────────┴─────────┘
)"},
{"arrayJoin affects all sections of the query", R"(
-- The arrayJoin function affects all sections of the query, including the WHERE section. Notice the result 2, even though the subquery returned 1 row.
SELECT sum(1) AS impressions
FROM
(
SELECT ['Istanbul', 'Berlin', 'Bobruisk'] AS cities
)
WHERE arrayJoin(cities) IN ['Istanbul', 'Berlin'];
)", R"(
┌─impressions─┐
│ 2 │
└─────────────┘
)"},
{"Using multiple arrayJoin functions", R"(
-- A query can use multiple arrayJoin functions. In this case, the transformation is performed multiple times and the rows are multiplied.
SELECT
sum(1) AS impressions,
arrayJoin(cities) AS city,
arrayJoin(browsers) AS browser
FROM
(
SELECT
['Istanbul', 'Berlin', 'Bobruisk'] AS cities,
['Firefox', 'Chrome', 'Chrome'] AS browsers
)
GROUP BY
2,
3
ORDER BY
city,
browser
)", R"(
┌─impressions─┬─city─────┬─browser─┐
│ 2 │ Berlin │ Chrome │
│ 1 │ Berlin │ Firefox │
│ 2 │ Bobruisk │ Chrome │
│ 1 │ Bobruisk │ Firefox │
│ 2 │ Istanbul │ Chrome │
│ 1 │ Istanbul │ Firefox │
└─────────────┴──────────┴─────────┘
)"
},
{"Unexpected results due to optimizations", R"(
-- Using multiple arrayJoin with the same expression may not produce the expected result due to optimizations.
-- For these cases, consider modifying the repeated array expression with extra operations that do not affect join result.
-- e.g. arrayJoin(arraySort(arr)), arrayJoin(arrayConcat(arr, []))
SELECT
arrayJoin(dice) as first_throw,
/* arrayJoin(dice) as second_throw */ -- is technically correct, but will annihilate result set
arrayJoin(arrayConcat(dice, [])) as second_throw -- intentionally changed expression to force re-evaluation
FROM (
SELECT [1, 2, 3, 4, 5, 6] as dice
);
)", R"(
┌─first_throw─┬─second_throw─┐
│ 1 │ 1 │
│ 1 │ 2 │
│ 1 │ 3 │
│ 1 │ 4 │
│ 1 │ 5 │
│ 1 │ 6 │
│ 2 │ 1 │
│ 2 │ 2 │
│ 2 │ 3 │
│ 2 │ 4 │
│ 2 │ 5 │
│ 2 │ 6 │
│ 3 │ 1 │
│ 3 │ 2 │
│ 3 │ 3 │
│ 3 │ 4 │
│ 3 │ 5 │
│ 3 │ 6 │
│ 4 │ 1 │
│ 4 │ 2 │
│ 4 │ 3 │
│ 4 │ 4 │
│ 4 │ 5 │
│ 4 │ 6 │
│ 5 │ 1 │
│ 5 │ 2 │
│ 5 │ 3 │
│ 5 │ 4 │
│ 5 │ 5 │
│ 5 │ 6 │
│ 6 │ 1 │
│ 6 │ 2 │
│ 6 │ 3 │
│ 6 │ 4 │
│ 6 │ 5 │
│ 6 │ 6 │
└─────────────┴──────────────┘
)"
},
{"Using the ARRAY JOIN syntax", R"(
-- Note the ARRAY JOIN syntax in the `SELECT` query below, which provides broader possibilities.
-- ARRAY JOIN allows you to convert multiple arrays with the same number of elements at a time.
SELECT
sum(1) AS impressions,
city,
browser
FROM
(
SELECT
['Istanbul', 'Berlin', 'Bobruisk'] AS cities,
['Firefox', 'Chrome', 'Chrome'] AS browsers
)
ARRAY JOIN
cities AS city,
browsers AS browser
GROUP BY
2,
3
ORDER BY
2,
3
)", R"(
┌─impressions─┬─city─────┬─browser─┐
│ 1 │ Berlin │ Chrome │
│ 1 │ Bobruisk │ Chrome │
│ 1 │ Istanbul │ Firefox │
└─────────────┴──────────┴─────────┘
)"
},
{"Using Tuple", R"(
-- You can also use Tuple
SELECT
sum(1) AS impressions,
(arrayJoin(arrayZip(cities, browsers)) AS t).1 AS city,
t.2 AS browser
FROM
(
SELECT
['Istanbul', 'Berlin', 'Bobruisk'] AS cities,
['Firefox', 'Chrome', 'Chrome'] AS browsers
)
GROUP BY
2,
3
ORDER BY
2,
3
)", R"(
┌─impressions─┬─city─────┬─browser─┐
│ 1 │ Berlin │ Chrome │
│ 1 │ Bobruisk │ Chrome │
│ 1 │ Istanbul │ Firefox │
└─────────────┴──────────┴─────────┘
)"
}
};
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
FunctionDocumentation documentation = {description, syntax, arguments, {}, returned_value, examples, introduced_in, category};
factory.registerFunction<FunctionArrayJoin>(documentation);
/// PostgreSQL/SQL-standard alias. `unnest(arr)` in a SELECT clause behaves
/// the same as `arrayJoin(arr)` - one row per array element. Note that
/// PostgreSQL's `LATERAL`/`CROSS JOIN UNNEST(...)` table-source syntax is
/// not supported by this alias and still requires `ARRAY JOIN`.
factory.registerAlias("unnest", "arrayJoin", FunctionFactory::Case::Insensitive);
}
}