1 | #include <TableFunctions/ITableFunction.h> |
2 | #include <TableFunctions/TableFunctionNumbers.h> |
3 | #include <TableFunctions/TableFunctionFactory.h> |
4 | #include <Parsers/ASTFunction.h> |
5 | #include <Parsers/ASTLiteral.h> |
6 | #include <Common/typeid_cast.h> |
7 | #include <Storages/System/StorageSystemNumbers.h> |
8 | #include <Interpreters/evaluateConstantExpression.h> |
9 | #include "registerTableFunctions.h" |
10 | |
11 | |
12 | namespace DB |
13 | { |
14 | |
15 | namespace ErrorCodes |
16 | { |
17 | extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; |
18 | } |
19 | |
20 | template <bool multithreaded> |
21 | StoragePtr TableFunctionNumbers<multithreaded>::executeImpl(const ASTPtr & ast_function, const Context & context, const std::string & table_name) const |
22 | { |
23 | if (const auto * function = ast_function->as<ASTFunction>()) |
24 | { |
25 | auto arguments = function->arguments->children; |
26 | |
27 | if (arguments.size() != 1 && arguments.size() != 2) |
28 | throw Exception("Table function '" + getName() + "' requires 'length' or 'offset, length'." , ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); |
29 | |
30 | |
31 | UInt64 offset = arguments.size() == 2 ? evaluateArgument(context, arguments[0]) : 0; |
32 | UInt64 length = arguments.size() == 2 ? evaluateArgument(context, arguments[1]) : evaluateArgument(context, arguments[0]); |
33 | |
34 | auto res = StorageSystemNumbers::create(table_name, multithreaded, length, offset, false); |
35 | res->startup(); |
36 | return res; |
37 | } |
38 | throw Exception("Table function '" + getName() + "' requires 'limit' or 'offset, limit'." , ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); |
39 | } |
40 | |
41 | void registerTableFunctionNumbers(TableFunctionFactory & factory) |
42 | { |
43 | factory.registerFunction<TableFunctionNumbers<true>>(); |
44 | factory.registerFunction<TableFunctionNumbers<false>>(); |
45 | } |
46 | |
47 | template <bool multithreaded> |
48 | UInt64 TableFunctionNumbers<multithreaded>::evaluateArgument(const Context & context, ASTPtr & argument) const |
49 | { |
50 | return evaluateConstantExpressionOrIdentifierAsLiteral(argument, context)->as<ASTLiteral &>().value.safeGet<UInt64>(); |
51 | } |
52 | |
53 | } |
54 | |