2020-03-10 14:54:22 +00:00
|
|
|
#include <TableFunctions/ITableFunction.h>
|
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
2022-06-23 20:04:06 +00:00
|
|
|
#include <Storages/checkAndGetLiteralArgument.h>
|
2020-03-10 14:54:22 +00:00
|
|
|
#include <Storages/System/StorageSystemZeros.h>
|
2020-10-14 12:19:29 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2020-03-10 14:54:22 +00:00
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include "registerTableFunctions.h"
|
2020-03-10 13:14:49 +00:00
|
|
|
|
2020-03-10 14:54:22 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2023-11-06 03:33:23 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2020-03-10 14:54:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 03:33:23 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/* zeros(limit), zeros_mt(limit)
|
|
|
|
* - the same as SELECT zero FROM system.zeros LIMIT limit.
|
|
|
|
* Used for testing purposes, as a simple example of table function.
|
|
|
|
*/
|
|
|
|
template <bool multithreaded>
|
|
|
|
class TableFunctionZeros : public ITableFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = multithreaded ? "zeros_mt" : "zeros";
|
|
|
|
std::string getName() const override { return name; }
|
|
|
|
bool hasStaticStructure() const override { return true; }
|
|
|
|
private:
|
|
|
|
StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override;
|
|
|
|
const char * getStorageTypeName() const override { return "SystemZeros"; }
|
|
|
|
|
|
|
|
UInt64 evaluateArgument(ContextPtr context, ASTPtr & argument) const;
|
|
|
|
|
|
|
|
ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override;
|
|
|
|
};
|
|
|
|
|
2020-10-14 12:19:29 +00:00
|
|
|
|
|
|
|
template <bool multithreaded>
|
2023-07-06 08:56:07 +00:00
|
|
|
ColumnsDescription TableFunctionZeros<multithreaded>::getActualTableStructure(ContextPtr /*context*/, bool /*is_insert_query*/) const
|
2020-10-14 12:19:29 +00:00
|
|
|
{
|
|
|
|
/// NOTE: https://bugs.llvm.org/show_bug.cgi?id=47418
|
|
|
|
return ColumnsDescription{{{"zero", std::make_shared<DataTypeUInt8>()}}};
|
|
|
|
}
|
|
|
|
|
2020-08-26 20:56:30 +00:00
|
|
|
template <bool multithreaded>
|
2023-07-06 08:56:07 +00:00
|
|
|
StoragePtr TableFunctionZeros<multithreaded>::executeImpl(const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/, bool /*is_insert_query*/) 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
|
|
|
|
2024-04-12 07:10:25 +00:00
|
|
|
if (arguments.size() > 1)
|
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function '{}' cannot have more than one params", getName());
|
2020-03-10 14:54:22 +00:00
|
|
|
|
2024-04-12 07:10:25 +00:00
|
|
|
if (!arguments.empty())
|
2024-03-27 07:08:11 +00:00
|
|
|
{
|
|
|
|
UInt64 length = evaluateArgument(context, arguments[0]);
|
2020-03-10 14:54:22 +00:00
|
|
|
|
2024-03-27 07:08:11 +00:00
|
|
|
auto res = std::make_shared<StorageSystemZeros>(StorageID(getDatabaseName(), table_name), multithreaded, length);
|
|
|
|
res->startup();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// zero-argument, the same as system.zeros
|
|
|
|
auto res = std::make_shared<StorageSystemZeros>(StorageID(getDatabaseName(), table_name), multithreaded);
|
|
|
|
res->startup();
|
|
|
|
return res;
|
|
|
|
}
|
2020-08-31 17:45:21 +00:00
|
|
|
}
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function '{}' requires 'limit'.", getName());
|
2020-03-10 14:54:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 03:33:23 +00:00
|
|
|
template <bool multithreaded>
|
|
|
|
UInt64 TableFunctionZeros<multithreaded>::evaluateArgument(ContextPtr context, ASTPtr & argument) const
|
|
|
|
{
|
|
|
|
return checkAndGetLiteralArgument<UInt64>(evaluateConstantExpressionOrIdentifierAsLiteral(argument, context), "length");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-10 14:54:22 +00:00
|
|
|
void registerTableFunctionZeros(TableFunctionFactory & factory)
|
|
|
|
{
|
2023-05-06 15:01:24 +00:00
|
|
|
factory.registerFunction<TableFunctionZeros<true>>({.documentation = {
|
|
|
|
.description=R"(
|
|
|
|
Generates a stream of zeros (a table with one column 'zero' of type 'UInt8') of specified size.
|
|
|
|
This table function is used in performance tests, where you want to spend as little time as possible to data generation while testing some other parts of queries.
|
|
|
|
In contrast to the `zeros_mt`, this table function is using single thread for data generation.
|
|
|
|
Example:
|
|
|
|
[example:1]
|
|
|
|
This query will test the speed of `randomPrintableASCII` function using single thread.
|
|
|
|
See also the `system.zeros` table.)",
|
|
|
|
.examples={{"1", "SELECT count() FROM zeros(100000000) WHERE NOT ignore(randomPrintableASCII(10))", ""}}
|
|
|
|
}});
|
|
|
|
|
|
|
|
factory.registerFunction<TableFunctionZeros<false>>({.documentation = {
|
|
|
|
.description=R"(
|
|
|
|
Generates a stream of zeros (a table with one column 'zero' of type 'UInt8') of specified size.
|
|
|
|
This table function is used in performance tests, where you want to spend as little time as possible to data generation while testing some other parts of queries.
|
|
|
|
In contrast to the `zeros`, this table function is using multiple threads for data generation, according to the `max_threads` setting.
|
|
|
|
Example:
|
|
|
|
[example:1]
|
|
|
|
This query will test the speed of `randomPrintableASCII` function using multiple threads.
|
|
|
|
See also the `system.zeros` table.
|
|
|
|
)",
|
|
|
|
.examples={{"1", "SELECT count() FROM zeros_mt(1000000000) WHERE NOT ignore(randomPrintableASCII(10))", ""}}
|
2023-11-06 03:33:23 +00:00
|
|
|
}});
|
2020-03-10 14:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|