aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Analyzer/QueryNode.h
blob: 277d6404965c2dc08af84f629ff5e0116261e377 (plain) (blame)
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#pragma once

#include <Common/SettingsChanges.h>

#include <Core/NamesAndTypes.h>
#include <Core/Field.h>

#include <Analyzer/Identifier.h>
#include <Analyzer/IQueryTreeNode.h>
#include <Analyzer/ListNode.h>
#include <Analyzer/TableExpressionModifiers.h>

#include <Interpreters/Context_fwd.h>

namespace DB
{

/** Query node represents query in query tree.
  *
  * Example: SELECT * FROM test_table WHERE id == 0;
  * Example: SELECT * FROM test_table_1 AS t1 INNER JOIN test_table_2 AS t2 ON t1.id = t2.id;
  *
  * Query node consists of following sections.
  * 1. WITH section.
  * 2. PROJECTION section.
  * 3. JOIN TREE section.
  * Example: SELECT * FROM test_table_1 AS t1 INNER JOIN test_table_2 AS t2 ON t1.id = t2.id;
  * test_table_1 AS t1 INNER JOIN test_table_2 AS t2 ON t1.id = t2.id - JOIN TREE section.
  * 4. PREWHERE section.
  * 5. WHERE section.
  * 6. GROUP BY section.
  * 7. HAVING section.
  * 8. WINDOW section.
  * Example: SELECT * FROM test_table WINDOW window AS (PARTITION BY id);
  * 9. ORDER BY section.
  * 10. INTERPOLATE section.
  * Example: SELECT * FROM test_table ORDER BY id WITH FILL INTERPOLATE (value AS value + 1);
  * value AS value + 1 - INTERPOLATE section.
  * 11. LIMIT BY limit section.
  * 12. LIMIT BY offset section.
  * 13. LIMIT BY section.
  * Example: SELECT * FROM test_table LIMIT 1 AS a OFFSET 5 AS b BY id, value;
  * 1 AS a - LIMIT BY limit section.
  * 5 AS b - LIMIT BY offset section.
  * id, value - LIMIT BY section.
  * 14. LIMIT section.
  * 15. OFFSET section.
  *
  * Query node contains settings changes that must be applied before query analysis or execution.
  * Example: SELECT * FROM test_table SETTINGS prefer_column_name_to_alias = 1, join_use_nulls = 1;
  *
  * Query node can be used as CTE.
  * Example: WITH cte_subquery AS (SELECT 1) SELECT * FROM cte_subquery;
  *
  * Query node can be used as scalar subquery.
  * Example: SELECT (SELECT 1) AS scalar_subquery.
  *
  * During query analysis pass query node must be resolved with projection columns.
  */
class QueryNode;
using QueryNodePtr = std::shared_ptr<QueryNode>;

class QueryNode final : public IQueryTreeNode
{
public:
    /// Construct query node with context and changed settings
    explicit QueryNode(ContextMutablePtr context_, SettingsChanges settings_changes_);

    /// Construct query node with context
    explicit QueryNode(ContextMutablePtr context_);

    /// Get context
    ContextPtr getContext() const
    {
        return context;
    }

    /// Get mutable context
    const ContextMutablePtr & getMutableContext() const
    {
        return context;
    }

    /// Get mutable context
    ContextMutablePtr & getMutableContext()
    {
        return context;
    }

    /// Returns true if query node has settings changes, false otherwise
    bool hasSettingsChanges() const
    {
        return !settings_changes.empty();
    }

    /// Get query node settings changes
    const SettingsChanges & getSettingsChanges() const
    {
        return settings_changes;
    }

    /// Returns true if query node is subquery, false otherwise
    bool isSubquery() const
    {
        return is_subquery;
    }

    /// Set query node is subquery value
    void setIsSubquery(bool is_subquery_value)
    {
        is_subquery = is_subquery_value;
    }

    /// Returns true if query node is CTE, false otherwise
    bool isCTE() const
    {
        return is_cte;
    }

    /// Set query node is CTE
    void setIsCTE(bool is_cte_value)
    {
        is_cte = is_cte_value;
    }

    /// Get query node CTE name
    const std::string & getCTEName() const
    {
        return cte_name;
    }

    /// Set query node CTE name
    void setCTEName(std::string cte_name_value)
    {
        cte_name = std::move(cte_name_value);
    }

    /// Returns true if query node has DISTINCT, false otherwise
    bool isDistinct() const
    {
        return is_distinct;
    }

    /// Set query node DISTINCT value
    void setIsDistinct(bool is_distinct_value)
    {
        is_distinct = is_distinct_value;
    }

    /// Returns true if query node has LIMIT WITH TIES, false otherwise
    bool isLimitWithTies() const
    {
        return is_limit_with_ties;
    }

    /// Set query node LIMIT WITH TIES value
    void setIsLimitWithTies(bool is_limit_with_ties_value)
    {
        is_limit_with_ties = is_limit_with_ties_value;
    }

    /// Returns true, if query node has GROUP BY WITH TOTALS, false otherwise
    bool isGroupByWithTotals() const
    {
        return is_group_by_with_totals;
    }

    /// Set query node GROUP BY WITH TOTALS value
    void setIsGroupByWithTotals(bool is_group_by_with_totals_value)
    {
        is_group_by_with_totals = is_group_by_with_totals_value;
    }

    /// Returns true, if query node has GROUP BY with ROLLUP modifier, false otherwise
    bool isGroupByWithRollup() const
    {
        return is_group_by_with_rollup;
    }

    /// Set query node GROUP BY with ROLLUP modifier value
    void setIsGroupByWithRollup(bool is_group_by_with_rollup_value)
    {
        is_group_by_with_rollup = is_group_by_with_rollup_value;
    }

    /// Returns true, if query node has GROUP BY with CUBE modifier, false otherwise
    bool isGroupByWithCube() const
    {
        return is_group_by_with_cube;
    }

    /// Set query node GROUP BY with CUBE modifier value
    void setIsGroupByWithCube(bool is_group_by_with_cube_value)
    {
        is_group_by_with_cube = is_group_by_with_cube_value;
    }

    /// Returns true, if query node has GROUP BY with GROUPING SETS modifier, false otherwise
    bool isGroupByWithGroupingSets() const
    {
        return is_group_by_with_grouping_sets;
    }

    /// Set query node GROUP BY with GROUPING SETS modifier value
    void setIsGroupByWithGroupingSets(bool is_group_by_with_grouping_sets_value)
    {
        is_group_by_with_grouping_sets = is_group_by_with_grouping_sets_value;
    }

    /// Returns true, if query node has GROUP BY ALL modifier, false otherwise
    bool isGroupByAll() const
    {
        return is_group_by_all;
    }

    /// Set query node GROUP BY ALL modifier value
    void setIsGroupByAll(bool is_group_by_all_value)
    {
        is_group_by_all = is_group_by_all_value;
    }

    /// Returns true if query node WITH section is not empty, false otherwise
    bool hasWith() const
    {
        return !getWith().getNodes().empty();
    }

    /// Get WITH section
    const ListNode & getWith() const
    {
        return children[with_child_index]->as<const ListNode &>();
    }

    /// Get WITH section
    ListNode & getWith()
    {
        return children[with_child_index]->as<ListNode &>();
    }

    /// Get WITH section node
    const QueryTreeNodePtr & getWithNode() const
    {
        return children[with_child_index];
    }

    /// Get WITH section node
    QueryTreeNodePtr & getWithNode()
    {
        return children[with_child_index];
    }

    /// Get PROJECTION section
    const ListNode & getProjection() const
    {
        return children[projection_child_index]->as<const ListNode &>();
    }

    /// Get PROJECTION section
    ListNode & getProjection()
    {
        return children[projection_child_index]->as<ListNode &>();
    }

    /// Get PROJECTION section node
    const QueryTreeNodePtr & getProjectionNode() const
    {
        return children[projection_child_index];
    }

    /// Get PROJECTION section node
    QueryTreeNodePtr & getProjectionNode()
    {
        return children[projection_child_index];
    }

    /// Get JOIN TREE section node
    const QueryTreeNodePtr & getJoinTree() const
    {
        return children[join_tree_child_index];
    }

    /// Get JOIN TREE section node
    QueryTreeNodePtr & getJoinTree()
    {
        return children[join_tree_child_index];
    }

    /// Returns true if query node PREWHERE section is not empty, false otherwise
    bool hasPrewhere() const
    {
        return children[prewhere_child_index] != nullptr;
    }

    /// Get PREWHERE section node
    const QueryTreeNodePtr & getPrewhere() const
    {
        return children[prewhere_child_index];
    }

    /// Get PREWHERE section node
    QueryTreeNodePtr & getPrewhere()
    {
        return children[prewhere_child_index];
    }

    /// Returns true if query node WHERE section is not empty, false otherwise
    bool hasWhere() const
    {
        return children[where_child_index] != nullptr;
    }

    /// Get WHERE section node
    const QueryTreeNodePtr & getWhere() const
    {
        return children[where_child_index];
    }

    /// Get WHERE section node
    QueryTreeNodePtr & getWhere()
    {
        return children[where_child_index];
    }

    /// Returns true if query node GROUP BY section is not empty, false otherwise
    bool hasGroupBy() const
    {
        return !getGroupBy().getNodes().empty();
    }

    /// Get GROUP BY section
    const ListNode & getGroupBy() const
    {
        return children[group_by_child_index]->as<const ListNode &>();
    }

    /// Get GROUP BY section
    ListNode & getGroupBy()
    {
        return children[group_by_child_index]->as<ListNode &>();
    }

    /// Get GROUP BY section node
    const QueryTreeNodePtr & getGroupByNode() const
    {
        return children[group_by_child_index];
    }

    /// Get GROUP BY section node
    QueryTreeNodePtr & getGroupByNode()
    {
        return children[group_by_child_index];
    }

    /// Returns true if query node HAVING section is not empty, false otherwise
    bool hasHaving() const
    {
        return getHaving() != nullptr;
    }

    /// Get HAVING section node
    const QueryTreeNodePtr & getHaving() const
    {
        return children[having_child_index];
    }

    /// Get HAVING section node
    QueryTreeNodePtr & getHaving()
    {
        return children[having_child_index];
    }

    /// Returns true if query node WINDOW section is not empty, false otherwise
    bool hasWindow() const
    {
        return !getWindow().getNodes().empty();
    }

    /// Get WINDOW section
    const ListNode & getWindow() const
    {
        return children[window_child_index]->as<const ListNode &>();
    }

    /// Get WINDOW section
    ListNode & getWindow()
    {
        return children[window_child_index]->as<ListNode &>();
    }

    /// Get WINDOW section node
    const QueryTreeNodePtr & getWindowNode() const
    {
        return children[window_child_index];
    }

    /// Get WINDOW section node
    QueryTreeNodePtr & getWindowNode()
    {
        return children[window_child_index];
    }

    /// Returns true if query node ORDER BY section is not empty, false otherwise
    bool hasOrderBy() const
    {
        return !getOrderBy().getNodes().empty();
    }

    /// Get ORDER BY section
    const ListNode & getOrderBy() const
    {
        return children[order_by_child_index]->as<const ListNode &>();
    }

    /// Get ORDER BY section
    ListNode & getOrderBy()
    {
        return children[order_by_child_index]->as<ListNode &>();
    }

    /// Get ORDER BY section node
    const QueryTreeNodePtr & getOrderByNode() const
    {
        return children[order_by_child_index];
    }

    /// Get ORDER BY section node
    QueryTreeNodePtr & getOrderByNode()
    {
        return children[order_by_child_index];
    }

    /// Returns true if query node INTERPOLATE section is not empty, false otherwise
    bool hasInterpolate() const
    {
        return getInterpolate() != nullptr;
    }

    /// Get INTERPOLATE section node
    const QueryTreeNodePtr & getInterpolate() const
    {
        return children[interpolate_child_index];
    }

    /// Get INTERPOLATE section node
    QueryTreeNodePtr & getInterpolate()
    {
        return children[interpolate_child_index];
    }

    /// Returns true if query node LIMIT BY LIMIT section is not empty, false otherwise
    bool hasLimitByLimit() const
    {
        return children[limit_by_limit_child_index] != nullptr;
    }

    /// Get LIMIT BY LIMIT section node
    const QueryTreeNodePtr & getLimitByLimit() const
    {
        return children[limit_by_limit_child_index];
    }

    /// Get LIMIT BY LIMIT section node
    QueryTreeNodePtr & getLimitByLimit()
    {
        return children[limit_by_limit_child_index];
    }

    /// Returns true if query node LIMIT BY OFFSET section is not empty, false otherwise
    bool hasLimitByOffset() const
    {
        return children[limit_by_offset_child_index] != nullptr;
    }

     /// Get LIMIT BY OFFSET section node
    const QueryTreeNodePtr & getLimitByOffset() const
    {
        return children[limit_by_offset_child_index];
    }

    /// Get LIMIT BY OFFSET section node
    QueryTreeNodePtr & getLimitByOffset()
    {
        return children[limit_by_offset_child_index];
    }

    /// Returns true if query node LIMIT BY section is not empty, false otherwise
    bool hasLimitBy() const
    {
        return !getLimitBy().getNodes().empty();
    }

    /// Get LIMIT BY section
    const ListNode & getLimitBy() const
    {
        return children[limit_by_child_index]->as<const ListNode &>();
    }

    /// Get LIMIT BY section
    ListNode & getLimitBy()
    {
        return children[limit_by_child_index]->as<ListNode &>();
    }

    /// Get LIMIT BY section node
    const QueryTreeNodePtr & getLimitByNode() const
    {
        return children[limit_by_child_index];
    }

    /// Get LIMIT BY section node
    QueryTreeNodePtr & getLimitByNode()
    {
        return children[limit_by_child_index];
    }

    /// Returns true if query node LIMIT section is not empty, false otherwise
    bool hasLimit() const
    {
        return children[limit_child_index] != nullptr;
    }

    /// Get LIMIT section node
    const QueryTreeNodePtr & getLimit() const
    {
        return children[limit_child_index];
    }

    /// Get LIMIT section node
    QueryTreeNodePtr & getLimit()
    {
        return children[limit_child_index];
    }

    /// Returns true if query node OFFSET section is not empty, false otherwise
    bool hasOffset() const
    {
        return children[offset_child_index] != nullptr;
    }

    /// Get OFFSET section node
    const QueryTreeNodePtr & getOffset() const
    {
        return children[offset_child_index];
    }

    /// Get OFFSET section node
    QueryTreeNodePtr & getOffset()
    {
        return children[offset_child_index];
    }

    /// Get query node projection columns
    const NamesAndTypes & getProjectionColumns() const
    {
        return projection_columns;
    }

    /// Resolve query node projection columns
    void resolveProjectionColumns(NamesAndTypes projection_columns_value)
    {
        projection_columns = std::move(projection_columns_value);
    }

    QueryTreeNodeType getNodeType() const override
    {
        return QueryTreeNodeType::QUERY;
    }

    void dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, size_t indent) const override;

protected:
    bool isEqualImpl(const IQueryTreeNode & rhs) const override;

    void updateTreeHashImpl(HashState &) const override;

    QueryTreeNodePtr cloneImpl() const override;

    ASTPtr toASTImpl(const ConvertToASTOptions & options) const override;

private:
    bool is_subquery = false;
    bool is_cte = false;
    bool is_distinct = false;
    bool is_limit_with_ties = false;
    bool is_group_by_with_totals = false;
    bool is_group_by_with_rollup = false;
    bool is_group_by_with_cube = false;
    bool is_group_by_with_grouping_sets = false;
    bool is_group_by_all = false;

    std::string cte_name;
    NamesAndTypes projection_columns;
    ContextMutablePtr context;
    SettingsChanges settings_changes;

    static constexpr size_t with_child_index = 0;
    static constexpr size_t projection_child_index = 1;
    static constexpr size_t join_tree_child_index = 2;
    static constexpr size_t prewhere_child_index = 3;
    static constexpr size_t where_child_index = 4;
    static constexpr size_t group_by_child_index = 5;
    static constexpr size_t having_child_index = 6;
    static constexpr size_t window_child_index = 7;
    static constexpr size_t order_by_child_index = 8;
    static constexpr size_t interpolate_child_index = 9;
    static constexpr size_t limit_by_limit_child_index = 10;
    static constexpr size_t limit_by_offset_child_index = 11;
    static constexpr size_t limit_by_child_index = 12;
    static constexpr size_t limit_child_index = 13;
    static constexpr size_t offset_child_index = 14;
    static constexpr size_t children_size = offset_child_index + 1;
};

}