2017-06-10 09:04:31 +00:00
|
|
|
#include <TableFunctions/ITableFunction.h>
|
|
|
|
#include <TableFunctions/TableFunctionNumbers.h>
|
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-06-10 09:04:31 +00:00
|
|
|
#include <Storages/System/StorageSystemNumbers.h>
|
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-02 05:03:28 +00:00
|
|
|
StoragePtr TableFunctionNumbers::executeImpl(const ASTPtr & ast_function, const Context & context) const
|
2017-06-10 09:04:31 +00:00
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
if (const auto * function = ast_function->as<ASTFunction>())
|
2018-06-20 08:09:09 +00:00
|
|
|
{
|
|
|
|
auto arguments = function->arguments->children;
|
2017-06-10 09:04:31 +00:00
|
|
|
|
2018-06-20 08:09:09 +00:00
|
|
|
if (arguments.size() != 1 && arguments.size() != 2)
|
|
|
|
throw Exception("Table function 'numbers' requires 'length' or 'offset, length'.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2017-06-10 09:04:31 +00:00
|
|
|
|
|
|
|
|
2018-06-20 08:09:09 +00:00
|
|
|
UInt64 offset = arguments.size() == 2 ? evaluateArgument(context, arguments[0]) : 0;
|
|
|
|
UInt64 length = arguments.size() == 2 ? evaluateArgument(context, arguments[1]) : evaluateArgument(context, arguments[0]);
|
2017-06-10 09:04:31 +00:00
|
|
|
|
2018-06-20 08:09:09 +00:00
|
|
|
auto res = StorageSystemNumbers::create(getName(), false, length, offset);
|
|
|
|
res->startup();
|
|
|
|
return res;
|
|
|
|
}
|
2019-01-09 15:44:20 +00:00
|
|
|
throw Exception("Table function 'numbers' requires 'limit' or 'offset, limit'.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2017-06-10 09:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void registerTableFunctionNumbers(TableFunctionFactory & factory)
|
|
|
|
{
|
2017-12-02 02:47:12 +00:00
|
|
|
factory.registerFunction<TableFunctionNumbers>();
|
2017-06-10 09:04:31 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 08:09:09 +00:00
|
|
|
|
|
|
|
UInt64 TableFunctionNumbers::evaluateArgument(const Context & context, ASTPtr & argument) const
|
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
return evaluateConstantExpressionOrIdentifierAsLiteral(argument, context)->as<ASTLiteral>()->value.safeGet<UInt64>();
|
2018-06-20 08:09:09 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 09:04:31 +00:00
|
|
|
}
|