aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Analyzer/ArrayJoinNode.h
blob: 89cb0b7b8c108b082d9b964eca4ad8305baa6023 (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
#pragma once

#include <Storages/IStorage_fwd.h>
#include <Storages/TableLockHolder.h>
#include <Storages/StorageSnapshot.h>

#include <Interpreters/Context_fwd.h>
#include <Interpreters/StorageID.h>

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

namespace DB
{

/** Array join node represents array join in query tree.
  *
  * In query tree array join expressions are represented by list query tree node.
  *
  * Example: SELECT id FROM test_table ARRAY JOIN [1, 2, 3] as a.
  *
  * Multiple expressions can be inside single array join.
  * Example: SELECT id FROM test_table ARRAY JOIN [1, 2, 3] as a, [4, 5, 6] as b.
  * Example: SELECT id FROM test_table ARRAY JOIN array_column_1 AS value_1, array_column_2 AS value_2.
  *
  * Multiple array joins can be inside JOIN TREE.
  * Example: SELECT id FROM test_table ARRAY JOIN array_column_1 ARRAY JOIN array_column_2.
  *
  * Array join can be used inside JOIN TREE with ordinary JOINS.
  * Example: SELECT t1.id FROM test_table_1 AS t1 INNER JOIN test_table_2 AS t2 ON t1.id = t2.id ARRAY JOIN [1,2,3];
  * Example: SELECT t1.id FROM test_table_1 AS t1 ARRAY JOIN [1,2,3] INNER JOIN test_table_2 AS t2 ON t1.id = t2.id;
  */
class ArrayJoinNode;
using ArrayJoinNodePtr = std::shared_ptr<ArrayJoinNode>;

class ArrayJoinNode final : public IQueryTreeNode
{
public:
    /** Construct array join node with table expression.
      * Example: SELECT id FROM test_table ARRAY JOIN [1, 2, 3] as a.
      * test_table - table expression.
      * join_expression_list - list of array join expressions.
      */
    ArrayJoinNode(QueryTreeNodePtr table_expression_, QueryTreeNodePtr join_expressions_, bool is_left_);

    /// Get table expression
    const QueryTreeNodePtr & getTableExpression() const
    {
        return children[table_expression_child_index];
    }

    /// Get table expression
    QueryTreeNodePtr & getTableExpression()
    {
        return children[table_expression_child_index];
    }

    /// Get join expressions
    const ListNode & getJoinExpressions() const
    {
        return children[join_expressions_child_index]->as<const ListNode &>();
    }

    /// Get join expressions
    ListNode & getJoinExpressions()
    {
        return children[join_expressions_child_index]->as<ListNode &>();
    }

    /// Get join expressions node
    const QueryTreeNodePtr & getJoinExpressionsNode() const
    {
        return children[join_expressions_child_index];
    }

    /// Get join expressions node
    QueryTreeNodePtr & getJoinExpressionsNode()
    {
        return children[join_expressions_child_index];
    }

    /// Returns true if array join is left, false otherwise
    bool isLeft() const
    {
        return is_left;
    }

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

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

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

    void updateTreeHashImpl(HashState & state) const override;

    QueryTreeNodePtr cloneImpl() const override;

    ASTPtr toASTImpl(const ConvertToASTOptions & options) const override;

private:
    bool is_left = false;

    static constexpr size_t table_expression_child_index = 0;
    static constexpr size_t join_expressions_child_index = 1;
    static constexpr size_t children_size = join_expressions_child_index + 1;
};

}