forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableFunctionLoop.cpp
More file actions
180 lines (162 loc) · 7.31 KB
/
Copy pathTableFunctionLoop.cpp
File metadata and controls
180 lines (162 loc) · 7.31 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
#include "config.h"
#include <Access/Common/AccessFlags.h>
#include <TableFunctions/ITableFunction.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <Interpreters/Context.h>
#include <Interpreters/DatabaseCatalog.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier.h>
#include <Common/Exception.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Storages/checkAndGetLiteralArgument.h>
#include <Storages/StorageLoop.h>
#include <TableFunctions/registerTableFunctions.h>
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int UNKNOWN_TABLE;
}
namespace
{
class TableFunctionLoop : public ITableFunction
{
public:
static constexpr auto name = "loop";
std::string getName() const override { return name; }
private:
StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const String & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override;
const char * getStorageEngineName() const override { return "Loop"; }
ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override;
void parseArguments(const ASTPtr & ast_function, ContextPtr context) override;
// save the inner table function AST
ASTPtr inner_table_function_ast;
// save database and table
std::string loop_database_name;
std::string loop_table_name;
};
}
void TableFunctionLoop::parseArguments(const ASTPtr & ast_function, ContextPtr context)
{
const auto & args_func = ast_function->as<ASTFunction &>();
if (!args_func.arguments)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function 'loop' must have arguments.");
auto & args = args_func.arguments->children;
if (args.empty())
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "No arguments provided for table function 'loop'");
if (args.size() == 1)
{
if (const auto * id = args[0]->as<ASTIdentifier>())
{
String id_name = id->name();
size_t dot_pos = id_name.find('.');
if (id_name.find('.', dot_pos + 1) != String::npos)
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "There are more than one dot");
if (dot_pos != String::npos)
{
loop_database_name = id_name.substr(0, dot_pos);
loop_table_name = id_name.substr(dot_pos + 1);
}
else
{
loop_table_name = id_name;
}
}
else if (const auto * /*func*/ _ = args[0]->as<ASTFunction>())
{
inner_table_function_ast = args[0];
}
else
{
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Expected identifier or function for argument 1 of function 'loop', got {}", args[0]->getID());
}
}
// loop(database, table)
else if (args.size() == 2)
{
args[0] = evaluateConstantExpressionForDatabaseName(args[0], context);
args[1] = evaluateConstantExpressionOrIdentifierAsLiteral(args[1], context);
loop_database_name = checkAndGetLiteralArgument<String>(args[0], "database");
loop_table_name = checkAndGetLiteralArgument<String>(args[1], "table");
}
else
{
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function 'loop' must have 1 or 2 arguments.");
}
}
ColumnsDescription TableFunctionLoop::getActualTableStructure(ContextPtr context, bool is_insert_query) const
{
if (inner_table_function_ast)
{
auto inner_table_function = TableFunctionFactory::instance().get(inner_table_function_ast, context);
/// Enforce the inner function's source access (as execute() does), not the raw structure.
return inner_table_function->getActualTableStructureWithAccess(context, is_insert_query);
}
String database_name = loop_database_name;
if (database_name.empty())
database_name = context->getCurrentDatabase();
/// Reading the schema requires SHOW COLUMNS, same as a direct DESCRIBE of the table.
context->checkAccess(AccessType::SHOW_COLUMNS, database_name, loop_table_name);
auto database = DatabaseCatalog::instance().getDatabase(database_name);
auto storage = database->tryGetTable(loop_table_name, context);
if (!storage)
throw Exception(ErrorCodes::UNKNOWN_TABLE, "Table '{}' not found in database '{}'", loop_table_name, database_name);
auto metadata_snapshot = storage->getInMemoryMetadataPtr(context, false);
return metadata_snapshot->getColumns();
}
StoragePtr TableFunctionLoop::executeImpl(
const ASTPtr & /*ast_function*/,
ContextPtr context,
const std::string & table_name,
ColumnsDescription cached_columns,
bool is_insert_query) const
{
StoragePtr storage;
if (!inner_table_function_ast)
{
String database_name = loop_database_name;
if (database_name.empty())
database_name = context->getCurrentDatabase();
auto database = DatabaseCatalog::instance().getDatabase(database_name);
storage = database->tryGetTable(loop_table_name, context);
if (!storage)
throw Exception(ErrorCodes::UNKNOWN_TABLE, "Table '{}' not found in database '{}'", loop_table_name, database_name);
context->checkAccess(AccessType::SELECT, database_name, loop_table_name);
}
else
{
auto inner_table_function = TableFunctionFactory::instance().get(inner_table_function_ast, context);
storage = inner_table_function->execute(
inner_table_function_ast,
context,
table_name,
std::move(cached_columns),
/*use_global_context=*/false,
/*is_insert_query=*/is_insert_query);
}
auto res = std::make_shared<StorageLoop>(
StorageID(getDatabaseName(), table_name),
storage,
inner_table_function_ast ? inner_table_function_ast->clone() : nullptr
);
res->startup();
return res;
}
void registerTableFunctionLoop(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionLoop>(
{
.description=R"(The table function can be used to continuously output query results in an infinite loop.)",
.examples{{"loop", "SELECT * FROM loop((numbers(3)) LIMIT 7", "0"
"1"
"2"
"0"
"1"
"2"
"0"}},
.category = FunctionDocumentation::Category::TableFunction
});
}
}