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>
|
2021-02-10 19:07:52 +00:00
|
|
|
#include <Interpreters/convertFieldToType.h>
|
2020-02-10 15:50:12 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2020-10-14 12:19:29 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2019-12-15 06:34:43 +00:00
|
|
|
#include "registerTableFunctions.h"
|
2017-06-10 09:04:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2021-02-10 19:07:52 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2017-06-10 09:04:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 12:19:29 +00:00
|
|
|
|
|
|
|
template <bool multithreaded>
|
2021-04-10 23:33:54 +00:00
|
|
|
ColumnsDescription TableFunctionNumbers<multithreaded>::getActualTableStructure(ContextPtr /*context*/) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
|
|
|
/// NOTE: https://bugs.llvm.org/show_bug.cgi?id=47418
|
|
|
|
return ColumnsDescription{{{"number", std::make_shared<DataTypeUInt64>()}}};
|
|
|
|
}
|
|
|
|
|
2020-08-26 20:56:30 +00:00
|
|
|
template <bool multithreaded>
|
2021-04-10 23:33:54 +00:00
|
|
|
StoragePtr TableFunctionNumbers<multithreaded>::executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/) const
|
2020-08-26 20:56:30 +00:00
|
|
|
{
|
2020-08-31 17:45:21 +00:00
|
|
|
if (const auto * function = ast_function->as<ASTFunction>())
|
|
|
|
{
|
|
|
|
auto arguments = function->arguments->children;
|
2020-08-26 20:56:30 +00:00
|
|
|
|
2020-08-31 17:45:21 +00:00
|
|
|
if (arguments.size() != 1 && arguments.size() != 2)
|
|
|
|
throw Exception("Table function '" + getName() + "' requires 'length' or 'offset, length'.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2017-06-10 09:04:31 +00:00
|
|
|
|
2020-08-31 17:45:21 +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
|
|
|
|
2020-08-31 17:45:21 +00:00
|
|
|
auto res = StorageSystemNumbers::create(StorageID(getDatabaseName(), table_name), multithreaded, length, offset, false);
|
|
|
|
res->startup();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
throw Exception("Table function '" + getName() + "' requires 'limit' or 'offset, limit'.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2017-06-10 09:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void registerTableFunctionNumbers(TableFunctionFactory & factory)
|
|
|
|
{
|
2019-08-19 12:27:51 +00:00
|
|
|
factory.registerFunction<TableFunctionNumbers<true>>();
|
|
|
|
factory.registerFunction<TableFunctionNumbers<false>>();
|
2017-06-10 09:04:31 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 12:27:51 +00:00
|
|
|
template <bool multithreaded>
|
2021-04-10 23:33:54 +00:00
|
|
|
UInt64 TableFunctionNumbers<multithreaded>::evaluateArgument(ContextPtr context, ASTPtr & argument) const
|
2018-06-20 08:09:09 +00:00
|
|
|
{
|
2021-02-10 19:07:52 +00:00
|
|
|
const auto & [field, type] = evaluateConstantExpression(argument, context);
|
|
|
|
|
|
|
|
if (!isNativeNumber(type))
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} expression, must be numeric type", type->getName());
|
|
|
|
|
|
|
|
Field converted = convertFieldToType(field, DataTypeUInt64());
|
|
|
|
if (converted.isNull())
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "The value {} is not representable as UInt64", applyVisitor(FieldVisitorToString(), field));
|
|
|
|
|
|
|
|
return converted.safeGet<UInt64>();
|
2018-06-20 08:09:09 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 09:04:31 +00:00
|
|
|
}
|