Repository navigation
Expand file tree
/
Copy pathCollectSets.cpp
More file actions
198 lines (166 loc) · 8.2 KB
/
Copy pathCollectSets.cpp
File metadata and controls
198 lines (166 loc) · 8.2 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
#include <Planner/CollectSets.h>
#include <Storages/StorageSet.h>
#if CLICKHOUSE_CLOUD
#include <Storages/StorageSharedSetJoin.h>
#endif
#include <Analyzer/TableFunctionNode.h>
#include <Analyzer/ConstantNode.h>
#include <Analyzer/FunctionNode.h>
#include <Analyzer/InDepthQueryTreeVisitor.h>
#include <Analyzer/SetUtils.h>
#include <Analyzer/TableNode.h>
#include <Analyzer/Utils.h>
#include <Core/Settings.h>
#include <DataTypes/DataTypeTuple.h>
#include <Interpreters/Set.h>
#include <Planner/Planner.h>
#include <Planner/PlannerContext.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <unordered_set>
namespace DB
{
namespace Setting
{
extern const SettingsBool transform_null_in;
extern const SettingsBool validate_enum_literals_in_operators;
}
namespace ErrorCodes
{
extern const int UNSUPPORTED_METHOD;
extern const int LOGICAL_ERROR;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
namespace
{
class CollectSetsVisitor : public ConstInDepthQueryTreeVisitor<CollectSetsVisitor>
{
public:
explicit CollectSetsVisitor(PlannerContext & planner_context_)
: planner_context(planner_context_)
{}
void visitImpl(const QueryTreeNodePtr & node)
{
if (const auto * table_node = node->as<TableFunctionNode>())
{
const auto & table_function_name = table_node->getTableFunctionName();
const auto & context = planner_context.getQueryContext();
TableFunctionPtr table_function_ptr = TableFunctionFactory::instance().tryGet(table_function_name, context);
auto skip_analysis_arguments_indexes = table_function_ptr->skipAnalysisForArguments(node, context);
const auto & table_function_arguments = table_node->getArguments().getNodes();
size_t table_function_arguments_size = table_function_arguments.size();
for (size_t table_function_argument_index = 0; table_function_argument_index < table_function_arguments_size; ++table_function_argument_index)
{
const auto & table_function_argument = table_function_arguments[table_function_argument_index];
auto skip_argument_index_it = std::find(skip_analysis_arguments_indexes.begin(), skip_analysis_arguments_indexes.end(), table_function_argument_index);
if (skip_argument_index_it != skip_analysis_arguments_indexes.end())
{
skip_children.insert(table_function_argument);
continue;
}
}
}
if (const auto * constant_node = node->as<ConstantNode>())
/// Collect sets from source expression as well.
/// Most likely we will not build them, but those sets could be requested during analysis.
if (constant_node->hasSourceExpression())
collectSets(constant_node->getSourceExpression(), planner_context);
auto * function_node = node->as<FunctionNode>();
if (!function_node || !isNameOfInFunction(function_node->getFunctionName()))
return;
if (function_node->getArguments().getNodes().size() < 2)
throw Exception(
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
"Function '{}' is expected to have at least 2 arguments, got {}",
function_node->getFunctionName(),
function_node->getArguments().getNodes().size());
auto in_first_argument = function_node->getArguments().getNodes().at(0);
auto in_second_argument = function_node->getArguments().getNodes().at(1);
auto in_second_argument_node_type = in_second_argument->getNodeType();
const auto & settings = planner_context.getQueryContext()->getSettingsRef();
auto & sets = planner_context.getPreparedSets();
/// Tables and table functions are replaced with subquery at Analysis stage, except special Set table.
auto * second_argument_table = in_second_argument->as<TableNode>();
StorageSet * storage_set = second_argument_table != nullptr ? dynamic_cast<StorageSet *>(second_argument_table->getStorage().get()) : nullptr;
if (storage_set)
{
/// Handle storage_set as ready set.
auto set_key = in_second_argument->getTreeHash({.ignore_cte = true});
if (sets.findStorage(set_key))
return;
auto ast = in_second_argument->toAST();
sets.addFromStorage(set_key, std::move(ast), storage_set->getSet(), second_argument_table->getStorageID());
}
else if (const auto * constant_node = in_second_argument->as<ConstantNode>())
{
auto set = getSetElementsForConstantValue(
in_first_argument->getResultType(), constant_node->getValue(), constant_node->getResultType(),
GetSetElementParams{
.transform_null_in = settings[Setting::transform_null_in],
.forbid_unknown_enum_values = settings[Setting::validate_enum_literals_in_operators],
});
if (set.empty())
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Function '{}' second argument evaluated to Block with no columns",
function_node->getFunctionName());
DataTypes set_element_types;
set_element_types.reserve(set.size());
/// Get the `set_element_types` from `set` instead of `in_first_argument` because
/// inside `getSetElementsForConstantValue`, we already do necessary transformation including
/// getting `dictionaryType` from `DataTypeLowCardinality`. Therefore, we can skip some steps here if
/// we directly use `set` to get the `set_element_types`.
for (const auto & elem : set)
set_element_types.push_back(elem.type);
set_element_types = Set::getElementTypes(std::move(set_element_types), settings[Setting::transform_null_in]);
auto set_key = in_second_argument->getTreeHash({.ignore_cte = true});
if (sets.findTuple(set_key, set_element_types))
return;
auto ast = in_second_argument->toAST();
#if CLICKHOUSE_CLOUD
if (storage_set->getName() == "SharedSet")
sets.addFromStorage(set_key, std::move(ast), static_cast<StorageSharedSet *>(storage_set)->getSet(planner_context.getQueryContext()), second_argument_table->getStorageID());
else
#endif
sets.addFromTuple(set_key, std::move(ast), std::move(set), settings);
}
else if (in_second_argument_node_type == QueryTreeNodeType::QUERY ||
in_second_argument_node_type == QueryTreeNodeType::UNION ||
in_second_argument_node_type == QueryTreeNodeType::TABLE)
{
auto set_key = in_second_argument->getTreeHash({.ignore_cte = true});
if (sets.findSubquery(set_key))
return;
auto subquery_to_execute = in_second_argument;
if (in_second_argument->as<TableNode>())
subquery_to_execute = buildSubqueryToReadColumnsFromTableExpression(subquery_to_execute, planner_context.getQueryContext());
auto ast = in_second_argument->toAST({ .set_subquery_cte_name = false });
sets.addFromSubquery(set_key, std::move(ast), std::move(subquery_to_execute), settings);
}
else
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD,
"Function '{}' is supported only if second argument is constant or table expression",
function_node->getFunctionName());
}
}
bool needChildVisit(const QueryTreeNodePtr &, const QueryTreeNodePtr & child_node)
{
if (skip_children.contains(child_node))
{
skip_children.erase(child_node);
return false;
}
auto child_node_type = child_node->getNodeType();
return !(child_node_type == QueryTreeNodeType::QUERY || child_node_type == QueryTreeNodeType::UNION);
}
private:
PlannerContext & planner_context;
std::unordered_set<QueryTreeNodePtr> skip_children;
};
}
void collectSets(const QueryTreeNodePtr & node, PlannerContext & planner_context)
{
CollectSetsVisitor visitor(planner_context);
visitor.visit(node);
}
}