-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy pathJoinNode.cpp
More file actions
321 lines (263 loc) · 11.1 KB
/
Copy pathJoinNode.cpp
File metadata and controls
321 lines (263 loc) · 11.1 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <Analyzer/IQueryTreeNode.h>
#include <Analyzer/JoinNode.h>
#include <Analyzer/ColumnNode.h>
#include <Analyzer/ListNode.h>
#include <Analyzer/Utils.h>
#include <IO/Operators.h>
#include <IO/WriteBuffer.h>
#include <IO/WriteHelpers.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTSubquery.h>
#include <Parsers/ASTTablesInSelectQuery.h>
#include <Common/assert_cast.h>
#include <Common/SipHash.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
JoinNode::JoinNode(QueryTreeNodePtr left_table_expression_,
QueryTreeNodePtr right_table_expression_,
QueryTreeNodePtr join_expression_,
JoinLocality locality_,
JoinStrictness strictness_,
JoinKind kind_,
bool is_using_join_expression_)
: ITableExpressionNode(children_size)
, locality(locality_)
, strictness(strictness_)
, kind(kind_)
, is_using_join_expression(is_using_join_expression_)
{
children[left_table_expression_child_index] = std::move(left_table_expression_);
children[right_table_expression_child_index] = std::move(right_table_expression_);
children[join_expression_child_index] = std::move(join_expression_);
}
/// There is a special workaround for the case when ARRAY JOIN alias is used in USING statement.
/// Example: ... ARRAY JOIN arr AS dummy INNER JOIN system.one USING (dummy);
///
/// In case of ARRAY JOIN, the column is renamed, so the query tree will look like:
/// JOIN EXPRESSION
/// LIST
/// COLUMN id: 16, column_name: dummy
/// EXPRESSION
/// LIST
/// COLUMN id: 18, column_name: __array_join_exp_1
/// COLUMN id: 19, column_name: dummy
///
/// Previously, when we convert QueryTree back to ast, the query would look like:
/// ARRAY JOIN arr AS __array_join_exp_1 ALL INNER JOIN system.one USING (__array_join_exp_1)
/// Which is incorrect query (which is broken in distributed case) because system.one do not have __array_join_exp_1.
///
/// In order to mitigate this, the syntax 'USING (__array_join_exp_1 AS dummy)' is introduced,
/// which means that '__array_join_exp_1' is taken from left, 'dummy' is taken from right,
/// and the USING column name is also 'dummy'
///
/// See 03448_analyzer_array_join_alias_in_join_using_bug
static ASTPtr tryMakeUsingColumnASTWithAlias(const QueryTreeNodePtr & node)
{
const auto * column_node = node->as<ColumnNode>();
if (!column_node)
return nullptr;
const auto & expr = column_node->getExpression();
if (!expr)
return nullptr;
const auto * expr_list_node = expr->as<ListNode>();
if (!expr_list_node)
return nullptr;
if (expr_list_node->getNodes().size() != 2)
return nullptr;
const auto * lhs_column_node = expr_list_node->getNodes()[0]->as<ColumnNode>();
const auto * rhs_column_node = expr_list_node->getNodes()[1]->as<ColumnNode>();
if (!lhs_column_node || !rhs_column_node)
return nullptr;
/// If USING column resolved from projection, keep its name
if (lhs_column_node->hasExpression())
return nullptr;
if (lhs_column_node->getColumnName() == rhs_column_node->getColumnName())
return nullptr;
auto node_ast = make_intrusive<ASTIdentifier>(lhs_column_node->getColumnName());
node_ast->setAlias(rhs_column_node->getColumnName());
return node_ast;
}
static ASTPtr makeUsingAST(const QueryTreeNodePtr & node)
{
const auto & list_node = node->as<ListNode &>();
auto expr_list = make_intrusive<ASTExpressionList>();
expr_list->children.reserve(list_node.getNodes().size());
for (const auto & child : list_node.getNodes())
{
ASTPtr node_ast = tryMakeUsingColumnASTWithAlias(child);
if (!node_ast)
node_ast = child->toAST();
expr_list->children.push_back(std::move(node_ast));
}
return expr_list;
}
ASTPtr JoinNode::toASTTableJoin() const
{
auto join_ast = make_intrusive<ASTTableJoin>();
join_ast->locality = locality;
join_ast->strictness = strictness;
join_ast->kind = kind;
join_ast->is_natural = is_natural && !hasJoinExpression();
if (children[join_expression_child_index])
{
if (is_using_join_expression)
{
join_ast->using_expression_list = makeUsingAST(children[join_expression_child_index]);
join_ast->children.push_back(join_ast->using_expression_list);
}
else
{
join_ast->on_expression = children[join_expression_child_index]->toAST();
join_ast->children.push_back(join_ast->on_expression);
}
}
return join_ast;
}
void JoinNode::dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, size_t indent) const
{
buffer << std::string(indent, ' ') << "JOIN id: " << format_state.getNodeId(this);
if (locality != JoinLocality::Unspecified)
buffer << ", locality: " << toString(locality);
if (strictness != JoinStrictness::Unspecified)
buffer << ", strictness: " << toString(strictness);
buffer << ", kind: " << toString(kind);
/// Use the raw node accessors: in an unresolved tree (e.g. EXPLAIN QUERY TREE
/// with run_passes = 0) the children are still identifiers, not table expressions.
buffer << '\n' << std::string(indent + 2, ' ') << "LEFT TABLE EXPRESSION\n";
getLeftTableExpressionNode()->dumpTreeImpl(buffer, format_state, indent + 4);
buffer << '\n' << std::string(indent + 2, ' ') << "RIGHT TABLE EXPRESSION\n";
getRightTableExpressionNode()->dumpTreeImpl(buffer, format_state, indent + 4);
if (getJoinExpression())
{
buffer << '\n' << std::string(indent + 2, ' ') << "JOIN EXPRESSION\n";
getJoinExpression()->dumpTreeImpl(buffer, format_state, indent + 4);
}
}
bool JoinNode::isEqualImpl(const IQueryTreeNode & rhs, CompareOptions) const
{
const auto & rhs_typed = assert_cast<const JoinNode &>(rhs);
return locality == rhs_typed.locality && strictness == rhs_typed.strictness && kind == rhs_typed.kind &&
is_using_join_expression == rhs_typed.is_using_join_expression &&
is_natural == rhs_typed.is_natural;
}
void JoinNode::updateTreeHashImpl(HashState & state, CompareOptions) const
{
state.update(locality);
state.update(strictness);
state.update(kind);
state.update(is_using_join_expression);
state.update(is_natural);
}
QueryTreeNodePtr JoinNode::cloneImpl() const
{
auto clone = std::make_shared<JoinNode>(
getLeftTableExpressionNode(),
getRightTableExpressionNode(),
getJoinExpression(),
locality, strictness, kind, is_using_join_expression);
clone->is_natural = is_natural;
return clone;
}
ASTPtr JoinNode::toASTImpl(const ConvertToASTOptions & options) const
{
ASTPtr tables_in_select_query_ast = make_intrusive<ASTTablesInSelectQuery>();
addTableExpressionOrJoinIntoTablesInSelectQuery(tables_in_select_query_ast, children[left_table_expression_child_index], options);
size_t join_table_index = tables_in_select_query_ast->children.size();
auto join_ast = toASTTableJoin();
addTableExpressionOrJoinIntoTablesInSelectQuery(tables_in_select_query_ast, children[right_table_expression_child_index], options);
auto & table_element = tables_in_select_query_ast->children.at(join_table_index)->as<ASTTablesInSelectQueryElement &>();
table_element.children.push_back(std::move(join_ast));
table_element.table_join = table_element.children.back();
return tables_in_select_query_ast;
}
void JoinNode::crossToInner(const QueryTreeNodePtr & join_expression_)
{
if (kind != JoinKind::Cross && kind != JoinKind::Comma)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot rewrite {} to INNER JOIN, expected CROSS", toString(kind));
if (children[join_expression_child_index])
throw Exception(ErrorCodes::LOGICAL_ERROR, "Join expression is expected to be empty for CROSS JOIN, got '{}'",
children[join_expression_child_index]->formatConvertedASTForErrorMessage());
kind = JoinKind::Inner;
strictness = JoinStrictness::All;
children[join_expression_child_index] = join_expression_;
}
CrossJoinNode::CrossJoinNode(QueryTreeNodePtr table_expression)
: ITableExpressionNode(1)
{
children = {std::move(table_expression)};
}
CrossJoinNode::CrossJoinNode(QueryTreeNodes table_expressions, JoinTypes join_types_)
: ITableExpressionNode(table_expressions.size())
, join_types(std::move(join_types_))
{
children = std::move(table_expressions);
if (children.size() != join_types.size() + 1)
throw Exception(ErrorCodes::LOGICAL_ERROR,
"Invalid number of join tables for CrossJoinNode. Expected {} got {}",
join_types.size() + 1, children.size());
}
void CrossJoinNode::appendTable(QueryTreeNodePtr table_expression, CrossJoinNode::JoinType join_type)
{
children.push_back(std::move(table_expression));
join_types.push_back(join_type);
}
void CrossJoinNode::dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, size_t indent) const
{
buffer << std::string(indent, ' ') << "CROSS JOIN id: " << format_state.getNodeId(this);
for (const auto & child : children)
{
buffer << '\n' << std::string(indent + 2, ' ') << "TABLE EXPRESSION\n";
child->dumpTreeImpl(buffer, format_state, indent + 4);
}
}
bool CrossJoinNode::isEqualImpl(const IQueryTreeNode & rhs, CompareOptions) const
{
const auto & rhs_typed = assert_cast<const CrossJoinNode &>(rhs);
if (rhs_typed.join_types.size() != join_types.size())
return false;
for (size_t i = 0; i < join_types.size(); ++i)
if (!(join_types[i].is_comma == rhs_typed.join_types[i].is_comma &&
join_types[i].locality == rhs_typed.join_types[i].locality))
return false;
return true;
}
void CrossJoinNode::updateTreeHashImpl(HashState & state, CompareOptions) const
{
state.update(join_types.size());
for (const auto & join_type : join_types)
{
state.update(join_type.is_comma);
state.update(join_type.locality);
}
}
QueryTreeNodePtr CrossJoinNode::cloneImpl() const
{
return std::make_shared<CrossJoinNode>(children, join_types);
}
ASTPtr CrossJoinNode::toASTImpl(const ConvertToASTOptions & options) const
{
ASTPtr tables_in_select_query_ast = make_intrusive<ASTTablesInSelectQuery>();
for (size_t i = 0; i < children.size(); ++i)
{
const auto & child = children[i];
size_t join_table_index = tables_in_select_query_ast->children.size();
addTableExpressionOrJoinIntoTablesInSelectQuery(tables_in_select_query_ast, child, options);
if (i > 0)
{
auto join_ast = make_intrusive<ASTTableJoin>();
join_ast->locality = join_types[i - 1].locality;
join_ast->strictness = JoinStrictness::Unspecified;
join_ast->kind = join_types[i - 1].is_comma ? JoinKind::Comma : JoinKind::Cross;
auto & table_element = tables_in_select_query_ast->children.at(join_table_index)->as<ASTTablesInSelectQueryElement &>();
table_element.children.push_back(std::move(join_ast));
table_element.table_join = table_element.children.back();
}
}
return tables_in_select_query_ast;
}
}