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

#include <Common/SettingsChanges.h>

#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>
#include <Analyzer/TableExpressionModifiers.h>

namespace DB
{

namespace ErrorCodes
{
    extern const int LOGICAL_ERROR;
}

/** Table function node represents table function in query tree.
  * Example: SELECT a FROM table_function(arguments...).
  *
  * In query tree table function arguments are represented by ListNode.
  *
  * Table function resolution must be done during query analysis pass.
  */
class ITableFunction;
using TableFunctionPtr = std::shared_ptr<ITableFunction>;

class TableFunctionNode;
using TableFunctionNodePtr = std::shared_ptr<TableFunctionNode>;

class TableFunctionNode : public IQueryTreeNode
{
public:
    /// Construct table function node with table function name
    explicit TableFunctionNode(String table_function_name);

    /// Get table function name
    const String & getTableFunctionName() const
    {
        return table_function_name;
    }

    /// Get arguments
    const ListNode & getArguments() const
    {
        return children[arguments_child_index]->as<const ListNode &>();
    }

    /// Get arguments
    ListNode & getArguments()
    {
        return children[arguments_child_index]->as<ListNode &>();
    }

    /// Get arguments node
    const QueryTreeNodePtr & getArgumentsNode() const
    {
        return children[arguments_child_index];
    }

    /// Get arguments node
    QueryTreeNodePtr & getArgumentsNode()
    {
        return children[arguments_child_index];
    }

    /// Returns true, if table function is resolved, false otherwise
    bool isResolved() const
    {
        return storage != nullptr && table_function != nullptr;
    }

    /// Get table function, returns nullptr if table function node is not resolved
    const TableFunctionPtr & getTableFunction() const
    {
        return table_function;
    }

    /// Get storage, returns nullptr if table function node is not resolved
    const StoragePtr & getStorage() const
    {
        return storage;
    }

    /// Get storage, throws exception if table function node is not resolved
    const StoragePtr & getStorageOrThrow() const
    {
        if (!storage)
            throw Exception(ErrorCodes::LOGICAL_ERROR, "Table function node is not resolved");

        return storage;
    }

    /// Resolve table function with table function, storage and context
    void resolve(TableFunctionPtr table_function_value, StoragePtr storage_value, ContextPtr context, std::vector<size_t> unresolved_arguments_indexes_);

    /// Get storage id, throws exception if function node is not resolved
    const StorageID & getStorageID() const;

    /// Get storage snapshot, throws exception if function node is not resolved
    const StorageSnapshotPtr & getStorageSnapshot() const;

    const std::vector<size_t> & getUnresolvedArgumentIndexes() const
    {
        return unresolved_arguments_indexes;
    }

    /// Return true if table function node has table expression modifiers, false otherwise
    bool hasTableExpressionModifiers() const
    {
        return table_expression_modifiers.has_value();
    }

    /// Get table expression modifiers
    const std::optional<TableExpressionModifiers> & getTableExpressionModifiers() const
    {
        return table_expression_modifiers;
    }

    /// Get table expression modifiers
    std::optional<TableExpressionModifiers> & getTableExpressionModifiers()
    {
        return table_expression_modifiers;
    }

    /// Get settings changes passed to table function
    const SettingsChanges & getSettingsChanges() const
    {
        return settings_changes;
    }

    /// Set settings changes passed as last argument to table function
    void setSettingsChanges(SettingsChanges settings_changes_)
    {
        settings_changes = std::move(settings_changes_);
    }

    /// Set table expression modifiers
    void setTableExpressionModifiers(TableExpressionModifiers table_expression_modifiers_value)
    {
        table_expression_modifiers = std::move(table_expression_modifiers_value);
    }

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

    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:
    String table_function_name;
    TableFunctionPtr table_function;
    StoragePtr storage;
    StorageID storage_id;
    StorageSnapshotPtr storage_snapshot;
    std::vector<size_t> unresolved_arguments_indexes;
    std::optional<TableExpressionModifiers> table_expression_modifiers;
    SettingsChanges settings_changes;

    static constexpr size_t arguments_child_index = 0;
    static constexpr size_t children_size = arguments_child_index + 1;
};

}