forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableFunctionGenerateRandom.cpp
99 lines (76 loc) · 3.29 KB
/
TableFunctionGenerateRandom.cpp
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
#include <Common/typeid_cast.h>
#include <Common/Exception.h>
#include <Core/Block.h>
#include <Storages/StorageGenerateRandom.h>
#include <Storages/checkAndGetLiteralArgument.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTFunction.h>
#include <TableFunctions/ITableFunction.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <TableFunctions/TableFunctionGenerateRandom.h>
#include <Interpreters/parseColumnsListForTableFunction.h>
#include "registerTableFunctions.h"
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int LOGICAL_ERROR;
}
void TableFunctionGenerateRandom::parseArguments(const ASTPtr & ast_function, ContextPtr /*context*/)
{
ASTs & args_func = ast_function->children;
if (args_func.size() != 1)
throw Exception("Table function '" + getName() + "' must have arguments.", ErrorCodes::LOGICAL_ERROR);
ASTs & args = args_func.at(0)->children;
if (args.empty())
throw Exception("Table function '" + getName() + "' requires at least one argument: "
" structure, [random_seed, max_string_length, max_array_length].",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
if (args.size() > 4)
throw Exception("Table function '" + getName() + "' requires at most four arguments: "
" structure, [random_seed, max_string_length, max_array_length].",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
// All the arguments must be literals.
for (const auto & arg : args)
{
if (!arg->as<const ASTLiteral>())
{
throw Exception(fmt::format(
"All arguments of table function '{}' must be literals. "
"Got '{}' instead", getName(), arg->formatForErrorMessage()),
ErrorCodes::BAD_ARGUMENTS);
}
}
/// Parsing first argument as table structure and creating a sample block
structure = checkAndGetLiteralArgument<String>(args[0], "structure");
if (args.size() >= 2)
{
const auto & literal = args[1]->as<const ASTLiteral &>();
if (!literal.value.isNull())
random_seed = checkAndGetLiteralArgument<UInt64>(literal, "random_seed");
}
if (args.size() >= 3)
max_string_length = checkAndGetLiteralArgument<UInt64>(args[2], "max_string_length");
if (args.size() == 4)
max_array_length = checkAndGetLiteralArgument<UInt64>(args[3], "max_string_length");
}
ColumnsDescription TableFunctionGenerateRandom::getActualTableStructure(ContextPtr context) const
{
return parseColumnsListFromString(structure, context);
}
StoragePtr TableFunctionGenerateRandom::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/) const
{
auto columns = getActualTableStructure(context);
auto res = std::make_shared<StorageGenerateRandom>(
StorageID(getDatabaseName(), table_name), columns, String{}, max_array_length, max_string_length, random_seed);
res->startup();
return res;
}
void registerTableFunctionGenerate(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionGenerateRandom>();
}
}