Merge branch 'qoega/random-table-function-small' of github.com:qoega/clickhouse into qoega/random-table-function-small

This commit is contained in:
Yatsishin Ilya 2020-02-07 22:03:01 +03:00
commit 118d4e0e20
18 changed files with 964 additions and 3 deletions

View File

@ -12,7 +12,7 @@ namespace DB
* Mostly the same as Int64.
* But also tagged with interval kind.
*
* Intended isage is for temporary elements in expressions,
* Intended usage is for temporary elements in expressions,
* not for storing values in tables.
*/
class DataTypeInterval final : public DataTypeNumberBase<Int64>

View File

@ -257,7 +257,7 @@ template class DataTypeNumberBase<UInt8>;
template class DataTypeNumberBase<UInt16>;
template class DataTypeNumberBase<UInt32>;
template class DataTypeNumberBase<UInt64>;
template class DataTypeNumberBase<UInt128>;
template class DataTypeNumberBase<UInt128>; // used only in UUID
template class DataTypeNumberBase<Int8>;
template class DataTypeNumberBase<Int16>;
template class DataTypeNumberBase<Int32>;

View File

@ -0,0 +1,464 @@
#include <Common/typeid_cast.h>
#include <Common/Exception.h>
#include <Core/Block.h>
#include <Storages/StorageValues.h>
#include <DataTypes/DataTypeTuple.h>
#include <DataTypes/DataTypeEnum.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeDateTime64.h>
#include <DataTypes/DataTypeDecimalBase.h>
#include <DataTypes/DataTypeArray.h>
#include <Columns/ColumnArray.h>
#include <Columns/ColumnFixedString.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnVector.h>
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnTuple.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTFunction.h>
#include <Common/randomSeed.h>
#include <pcg_random.hpp>
#include <TableFunctions/ITableFunction.h>
#include <TableFunctions/TableFunctionRandom.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <TableFunctions/parseColumnsListForTableFunction.h>
#include "registerTableFunctions.h"
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int BAD_ARGUMENTS;
extern const int BAD_TYPE_OF_FIELD;
extern const int LOGICAL_ERROR;
}
void fillColumnWithRandomData(IColumn & column, DataTypePtr type, UInt64 limit,
UInt64 max_array_length, UInt64 max_string_length, UInt64 random_seed)
{
TypeIndex idx = type->getTypeId();
if (!random_seed)
random_seed = randomSeed();
(void) max_string_length;
switch (idx)
{
case TypeIndex::Nothing:
throw Exception("Random Generator not implemented for type 'Nothing'.", ErrorCodes::NOT_IMPLEMENTED);
case TypeIndex::UInt8:
{
auto & data = typeid_cast<ColumnVector<UInt8> &>(column).getData();
data.resize(limit);
pcg32 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
data[i] = static_cast<UInt8>(generator());
}
break;
}
case TypeIndex::UInt16:
{
auto & data = typeid_cast<ColumnVector<UInt16> &>(column).getData();
data.resize(limit);
pcg32 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
data[i] = static_cast<UInt16>(generator());
}
break;
}
case TypeIndex::UInt32:
{
auto & data = typeid_cast<ColumnVector<UInt32> &>(column).getData();
data.resize(limit);
pcg32 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
data[i] = static_cast<UInt32>(generator());
}
break;
}
case TypeIndex::UInt64:
{
auto & data = typeid_cast<ColumnVector<UInt64> &>(column).getData();
data.resize(limit);
pcg64 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
data[i] = static_cast<UInt64>(generator());
}
break;
}
case TypeIndex::UInt128:
throw Exception("There is no DataType 'UInt128' support.", ErrorCodes::NOT_IMPLEMENTED);
case TypeIndex::Int8:
{
auto & data = typeid_cast<ColumnVector<Int8> &>(column).getData();
data.resize(limit);
pcg32 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
data[i] = static_cast<Int8>(generator());
}
break;
}
case TypeIndex::Int16:
{
auto & data = typeid_cast<ColumnVector<Int16> &>(column).getData();
data.resize(limit);
pcg32 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
data[i] = static_cast<Int16>(generator());
}
break;
}
case TypeIndex::Int32:
{
auto & data = typeid_cast<ColumnVector<Int32> &>(column).getData();
data.resize(limit);
pcg32 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
data[i] = static_cast<Int32>(generator());
}
break;
}
case TypeIndex::Int64:
{
auto & data = typeid_cast<ColumnVector<Int64> &>(column).getData();
data.resize(limit);
pcg64 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
data[i] = static_cast<Int64>(generator());
}
break;
}
case TypeIndex::Int128:
throw Exception("There is no DataType 'Int128' support.", ErrorCodes::NOT_IMPLEMENTED);
case TypeIndex::Float32:
{
auto & data = typeid_cast<ColumnVector<Float32> &>(column).getData();
data.resize(limit);
pcg32 generator(random_seed);
double d = 1.0;
for (UInt64 i = 0; i < limit; ++i)
{
d = std::numeric_limits<float>::max();
data[i] = (d / pcg32::max()) * generator();
}
break;
}
case TypeIndex::Float64:
{
auto & data = typeid_cast<ColumnVector<Float64> &>(column).getData();
data.resize(limit);
pcg64 generator(random_seed);
double d = 1.0;
for (UInt64 i = 0; i < limit; ++i)
{
d = std::numeric_limits<double>::max();
data[i] = (d / pcg64::max()) * generator();
}
break;
}
case TypeIndex::Date:
{
auto & data = typeid_cast<ColumnVector<UInt16> &>(column).getData();
data.resize(limit);
pcg32 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
data[i] = static_cast<UInt16>(generator());
}
break;
}
case TypeIndex::DateTime:
{
auto & data = typeid_cast<ColumnVector<UInt32> &>(column).getData();
data.resize(limit);
pcg32 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
data[i] = static_cast<UInt32>(generator());
}
break;
}
case TypeIndex::DateTime64:
{
UInt32 scale;
if (auto * ptype = typeid_cast<const DataTypeDateTime64 *>(type.get()))
scale = ptype->getScale();
else
throw Exception("Static cast to DataTypeDateTime64 failed ", ErrorCodes::BAD_TYPE_OF_FIELD);
auto & data = typeid_cast<ColumnDecimal<Decimal64> &>(column).getData();
data.resize(limit);
pcg32 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
UInt32 fractional = static_cast<UInt32>(generator()) % intExp10(scale);
UInt32 whole = static_cast<UInt32>(generator());
DateTime64 dt = DecimalUtils::decimalFromComponents<DateTime64>(whole, fractional, scale);
data[i] = dt;
}
break;
}
case TypeIndex::String:
{
auto & column_string = typeid_cast<ColumnString &>(column);
auto & offsets = column_string.getOffsets();
auto & chars = column_string.getChars();
UInt64 offset = 0;
{
pcg32 generator(random_seed);
offsets.resize(limit);
for (UInt64 i = 0; i < limit; ++i)
{
offset += 1 + static_cast<UInt64>(generator()) % max_string_length;
offsets[i] = offset - 1;
}
chars.resize(offset);
for (UInt64 i = 0; i < offset; ++i)
{
chars[i] = 32 + generator() % 95;
}
// add terminating zero char
for (auto & i : offsets)
{
chars[i] = 0;
}
}
break;
}
case TypeIndex::FixedString:
{
auto & column_string = typeid_cast<ColumnFixedString &>(column);
size_t len = column_string.sizeOfValueIfFixed();
auto & chars = column_string.getChars();
UInt64 num_chars = static_cast<UInt64>(len) * limit;
{
pcg32 generator(random_seed);
chars.resize(num_chars);
for (UInt64 i = 0; i < num_chars; ++i)
{
chars[i] = static_cast<UInt8>(generator());
}
}
break;
}
case TypeIndex::Enum8:
{
auto values = typeid_cast<const DataTypeEnum<Int8> *>(type.get())->getValues();
auto & data = typeid_cast<ColumnVector<Int8> &>(column).getData();
data.resize(limit);
pcg32 generator(random_seed);
UInt8 size = values.size();
UInt8 off;
for (UInt64 i = 0; i < limit; ++i)
{
off = static_cast<UInt8>(generator()) % size;
data[i] = values[off].second;
}
break;
}
case TypeIndex::Enum16:
{
auto values = typeid_cast<const DataTypeEnum<Int16> *>(type.get())->getValues();
auto & data = typeid_cast<ColumnVector<Int16> &>(column).getData();
data.resize(limit);
pcg32 generator(random_seed);
UInt16 size = values.size();
UInt8 off;
for (UInt64 i = 0; i < limit; ++i)
{
off = static_cast<UInt16>(generator()) % size;
data[i] = values[off].second;
}
break;
}
case TypeIndex::Decimal32:
{
auto & data = typeid_cast<ColumnDecimal<Decimal32> &>(column).getData();
data.resize(limit);
pcg32 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
data[i] = static_cast<Int32>(generator());
}
break;
}
case TypeIndex::Decimal64:
{
auto & data = typeid_cast<ColumnDecimal<Decimal64> &>(column).getData();
data.resize(limit);
pcg64 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
data[i] = static_cast<UInt64>(generator());
}
break;
}
case TypeIndex::Decimal128:
{
auto & data = typeid_cast<ColumnDecimal<Decimal128> &>(column).getData();
data.resize(limit);
pcg64 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
Int128 x = static_cast<Int128>(generator()) << 64 | static_cast<Int128>(generator());
data[i] = x;
}
}
break;
case TypeIndex::UUID:
{
auto & data = typeid_cast<ColumnVector<UInt128> &>(column).getData();
data.resize(limit);
pcg64 generator(random_seed);
for (UInt64 i = 0; i < limit; ++i)
{
auto x = UInt128(generator(), generator());
data[i] = x;
}
}
break;
case TypeIndex::Array:
{
auto & column_array = typeid_cast<ColumnArray &>(column);
auto nested_type = typeid_cast<const DataTypeArray *>(type.get())->getNestedType();
auto & offsets = column_array.getOffsets();
IColumn & data = column_array.getData();
UInt64 offset = 0;
{
pcg32 generator(random_seed);
offsets.resize(limit);
for (UInt64 i = 0; i < limit; ++i)
{
offset += static_cast<UInt64>(generator()) % max_array_length;
offsets[i] = offset;
}
}
fillColumnWithRandomData(data, nested_type, offset, max_array_length, max_string_length, random_seed);
break;
}
case TypeIndex::Tuple:
{
auto &column_tuple = typeid_cast<ColumnTuple &>(column);
auto elements = typeid_cast<const DataTypeTuple *>(type.get())->getElements();
for (size_t i = 0; i < column_tuple.tupleSize(); ++i)
{
fillColumnWithRandomData(column_tuple.getColumn(i), elements[i], limit, max_array_length, max_string_length, random_seed);
}
break;
}
case TypeIndex::Set:
throw Exception("Type 'Set' can not be stored in a table.", ErrorCodes::LOGICAL_ERROR);
case TypeIndex::Interval:
throw Exception("Type 'Interval' can not be stored in a table.", ErrorCodes::LOGICAL_ERROR);
case TypeIndex::Nullable:
{
auto & column_nullable = typeid_cast<ColumnNullable &>(column);
auto nested_type = typeid_cast<const DataTypeNullable *>(type.get())->getNestedType();
auto & null_map = column_nullable.getNullMapData();
IColumn & nested_column = column_nullable.getNestedColumn();
fillColumnWithRandomData(nested_column, nested_type, limit, max_array_length, max_string_length, random_seed);
pcg32 generator(random_seed);
null_map.resize(limit);
for (UInt64 i = 0; i < limit; ++i)
{
null_map[i] = generator() < 1024;
}
break;
}
case TypeIndex::Function:
throw Exception("Type 'Funclion' can not be stored in a table.", ErrorCodes::LOGICAL_ERROR);
case TypeIndex::AggregateFunction:
throw Exception("Random Generator not implemented for type 'AggregateFunction'.", ErrorCodes::NOT_IMPLEMENTED);
case TypeIndex::LowCardinality:
throw Exception("Random Generator not implemented for type 'LowCardinality'.", ErrorCodes::NOT_IMPLEMENTED);
}
}
StoragePtr TableFunctionRandom::executeImpl(const ASTPtr & ast_function, const Context & context, const std::string & table_name) const
{
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.size() > 5)
throw Exception("Table function '" + getName() + "' requires at most five arguments: "\
" structure, limit, max_array_length, max_string_length, random_seed.",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
/// Parsing first argument as table structure and creating a sample block
std::string structure = args[0]->as<ASTLiteral &>().value.safeGet<String>();
UInt64 limit = 1;
UInt64 max_array_length = 10;
UInt64 max_string_length = 10;
UInt64 random_seed = 0; // zero for random
/// Parsing second argument if present
if (args.size() >= 2)
limit = args[1]->as<ASTLiteral &>().value.safeGet<UInt64>();
if (!limit)
throw Exception("Table function '" + getName() + "' limit should not be 0.", ErrorCodes::BAD_ARGUMENTS);
if (args.size() >= 3)
max_array_length = args[1]->as<ASTLiteral &>().value.safeGet<UInt64>();
if (args.size() >= 4)
max_string_length = args[1]->as<ASTLiteral &>().value.safeGet<UInt64>();
if (args.size() == 5)
random_seed = args[1]->as<ASTLiteral &>().value.safeGet<UInt64>();
ColumnsDescription columns = parseColumnsListFromString(structure, context);
Block res_block;
for (const auto & name_type : columns.getOrdinary())
{
MutableColumnPtr column = name_type.type->createColumn();
res_block.insert({std::move(column), name_type.type, name_type.name});
}
for (auto & ctn : res_block.getColumnsWithTypeAndName())
{
fillColumnWithRandomData(ctn.column->assumeMutableRef(), ctn.type, limit, max_array_length, max_string_length, random_seed);
}
auto res = StorageValues::create(StorageID(getDatabaseName(), table_name), columns, res_block);
res->startup();
return res;
}
void registerTableFunctionRandom(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionRandom>(TableFunctionFactory::CaseInsensitive);
}
}

View File

@ -0,0 +1,20 @@
#pragma once
#include <TableFunctions/ITableFunction.h>
namespace DB
{
/* random(structure, limit) - creates a temporary storage filling columns with random data
* random is case-insensitive table function
*/
class TableFunctionRandom : public ITableFunction
{
public:
static constexpr auto name = "generate";
std::string getName() const override { return name; }
private:
StoragePtr executeImpl(const ASTPtr & ast_function, const Context & context, const std::string & table_name) const override;
};
}

View File

@ -15,6 +15,7 @@ void registerTableFunctions()
registerTableFunctionURL(factory);
registerTableFunctionValues(factory);
registerTableFunctionInput(factory);
registerTableFunctionRandom(factory);
#if USE_AWS_S3
registerTableFunctionS3(factory);

View File

@ -12,6 +12,7 @@ void registerTableFunctionFile(TableFunctionFactory & factory);
void registerTableFunctionURL(TableFunctionFactory & factory);
void registerTableFunctionValues(TableFunctionFactory & factory);
void registerTableFunctionInput(TableFunctionFactory & factory);
void registerTableFunctionRandom(TableFunctionFactory & factory);
#if USE_AWS_S3
void registerTableFunctionS3(TableFunctionFactory & factory);

View File

@ -0,0 +1,240 @@
Enum8(\'hello\' = 1, \'world\' = 5)
world
hello
world
hello
hello
hello
world
hello
hello
hello
-
Array(Nullable(Enum8(\'hello\' = 1, \'world\' = 5)))
['world','hello','world','hello','hello','hello','world','hello','hello']
['hello','world','world','hello','world','world']
['hello','world','hello','hello','world','world','world']
['world','world','world','world','world','world','hello','hello']
['world','hello']
['hello','hello']
['world']
['hello','hello']
['hello','hello']
['hello','world','hello','hello','world','world','world','world']
-
Nullable(Enum16(\'o\' = -200, \'h\' = 1, \'w\' = 5))
o
w
w
w
h
w
h
h
w
o
-
UInt64 Int64 UInt32 Int32 UInt16 Int16 UInt8 Int8
2254772619926532955 2254772619926532955 1234817989 1234817989 54213 -11323 197 -59
9120028858397505560 9120028858397505560 1171957426 1171957426 42674 -22862 178 -78
4555697903102013946 4555697903102013946 275100647 275100647 46055 -19481 231 -25
5784362079052877875 5784362079052877875 1033685688 1033685688 51896 -13640 184 -72
11035971995277520997 -7410772078432030619 180895192 180895192 15832 15832 216 -40
7901646768096461004 7901646768096461004 135557292 135557292 28844 28844 172 -84
6733841386518201279 6733841386518201279 716914271 716914271 15967 15967 95 95
7736560050027905187 7736560050027905187 1012211222 1012211222 7702 7702 22 22
2199287578947862030 2199287578947862030 2185722662 -2109244634 31526 31526 38 38
3019483913099890467 3019483913099890467 2647224658 -1647742638 29010 29010 82 82
-
Date DateTime DateTime(\'Europe/Moscow\')
2106-02-07 2009-02-16 23:59:49 2009-02-16 23:59:49
2086-11-02 2007-02-20 10:43:46 2007-02-20 10:43:46
2096-02-04 1978-09-20 03:50:47 1978-09-20 03:50:47
2106-02-07 2002-10-04 02:54:48 2002-10-04 02:54:48
2013-05-07 1975-09-25 19:39:52 1975-09-25 19:39:52
2048-12-21 1974-04-19 01:48:12 1974-04-19 01:48:12
2013-09-19 1992-09-19 18:51:11 1992-09-19 18:51:11
1991-02-02 2002-01-28 12:47:02 2002-01-28 12:47:02
2056-04-25 2039-04-06 20:11:02 2039-04-06 20:11:02
2049-06-05 2053-11-20 07:10:58 2053-11-20 07:10:58
-
DateTime64(3) DateTime64(6) DateTime64(6, \'Europe/Moscow\')
2007-02-20 10:43:46.989 2007-02-20 10:43:46.817989 2007-02-20 10:43:46.817989
2002-10-04 02:54:48.647 2002-10-04 02:54:48.100647 2002-10-04 02:54:48.100647
1974-04-19 01:48:12.192 1974-04-19 01:48:12.895192 1974-04-19 01:48:12.895192
2002-01-28 12:47:02.271 2002-01-28 12:47:02.914271 2002-01-28 12:47:02.914271
2053-11-20 07:10:58.662 2053-11-20 07:10:58.722662 2053-11-20 07:10:58.722662
1986-04-08 19:07:15.849 1986-04-08 19:07:15.510849 1986-04-08 19:07:15.510849
2081-03-06 04:00:55.914 2081-03-06 04:00:55.448914 2081-03-06 04:00:55.448914
1979-01-20 20:39:20.939 1979-01-20 20:39:20.162939 1979-01-20 20:39:20.162939
2063-07-18 01:46:10.215 2063-07-18 01:46:10.908215 2063-07-18 01:46:10.908215
1996-11-02 14:35:41.110 1996-11-02 14:35:41.183110 1996-11-02 14:35:41.183110
-
Float32 Float64
9.783235e37 2.1973467205491123e307
9.285203e37 8.887754501811354e307
2.1795718e37 4.4396706606805647e307
8.1897013e37 5.637042481600483e307
1.4331993e37 1.07549012514996e308
1.0739954e37 7.700402896226395e307
5.67998e37 6.562339881458101e307
8.019563e37 7.539520705557441e307
1.7317079e38 2.143274805821858e307
2.0973474e38 2.9425818885529257e307
-
Decimal32(4) Decimal64(8) Decimal64(8)
123481.7989 22547726199.26532955 4159321346419233104838.6879832895010840
117195.7426 91200288583.97505560 8403779329565810688767.7049545291714611
27510.0647 45556979031.02013946 -13670461591942827725055.0250490776469300
103368.5688 57843620790.52877875 12421744869005473959544.2499747955622051
18089.5192 -74107720784.32030619 4056969511333950153663.4915186231430947
13555.7292 79016467680.96461004 -8819413736166121578589.4583420666183888
71691.4271 67338413865.18201279 13058329479868658041313.8432372419860363
101221.1222 77365600500.27905187 -4693380431928321782727.0243506636623202
-210924.4634 21992875789.47862030 13765369952377767241248.9441272127848016
-164774.2638 30194839130.99890467 -13890064946313418575619.0315227826809939
-
UUID
7e90d9ed-3e18-3818-1f4a-8fc063ff735b
50462ea9-fe2f-d033-3f39-171b126331fa
6da849de-f100-c0cc-9927-a60f01acf065
6b5dc860-1d64-40a3-5d73-6910493dc3bf
29e75c9e-fd53-4f23-1e85-7066961dbe0e
b60faa27-fe38-ff30-bda6-6d4f737b3622
297f7b2f-ec54-178b-623d-6d8244222885
556e7945-df65-729e-dcb0-e0ca3a435f2e
8d6df922-9588-7e50-678f-236036acd439
bdb39798-af19-13ad-9780-b53edc0f4a21
-
Tuple(Int32, Int64)
(1234817989,2254772619926532955)
(1171957426,9120028858397505560)
(275100647,4555697903102013946)
(1033685688,5784362079052877875)
(180895192,-7410772078432030619)
(135557292,7901646768096461004)
(716914271,6733841386518201279)
(1012211222,7736560050027905187)
(-2109244634,2199287578947862030)
(-1647742638,3019483913099890467)
-
Array(Int8)
[-59,-78,-25,-72,-40,-84,95,22,38]
[82,65,35,-110,-57,-69]
[72,119,-78,-58,13,39,-71]
[81,107,-11,-63,-59,69,-80,-122]
[87,-76]
[22,-84]
[-45]
[-40,84]
[-104,-86]
[-36,123,44,60,5,25,-5,-127]
-
Array(Nullable(Int32))
[1234817989,1171957426,275100647,1033685688,180895192,135557292,716914271,1012211222,-2109244634]
[-1647742638,319510849,513356835,-1966518382,-786518841,269162939]
[285701960,1943908215,-1343029326,1474183110,846934541,1007818023,-1664171079]
[195050577,371018347,734173429,2001591233,-1812297275,1172704837,-728923984,774864518]
[-462583209,-1520633676]
[-638906858,1986832300]
[378774483]
[-1399152424,-953863084]
[733724312,-23652950]
[371735004,462118779,148602156,-1055384004,-1041274619,247762201,522289659,822210177]
-
Array(Nullable(UUID))
['7e90d9ed-3e18-3818-1f4a-8fc063ff735b','50462ea9-fe2f-d033-3f39-171b126331fa','6da849de-f100-c0cc-9927-a60f01acf065','6b5dc860-1d64-40a3-5d73-6910493dc3bf','29e75c9e-fd53-4f23-1e85-7066961dbe0e','b60faa27-fe38-ff30-bda6-6d4f737b3622','297f7b2f-ec54-178b-623d-6d8244222885','556e7945-df65-729e-dcb0-e0ca3a435f2e','8d6df922-9588-7e50-678f-236036acd439']
['bdb39798-af19-13ad-9780-b53edc0f4a21','5ca17a81-ab30-2b25-c798-10de3635d333','a766c8bc-be3b-a400-1c75-6bca44383f17','167b3c3c-66d3-5a18-d907-2738ac937ed6','aaf2f78f-d92d-f3ce-b1e8-dec2de293c9f','5a460194-f0be-04dd-9cd2-5f9f3c0df43d']
['c78566cc-f112-f7d5-10a4-718dab8c49c6','9a1805ce-8d1f-b583-02ac-2bf55634a5a8','f831fa23-87f5-c29a-8037-a13d200408f2','ecbb216a-dbd7-48c7-a99c-437311212691','2f211de3-ea53-af08-ef09-86ff50310353','c77b4d76-9b12-b917-7780-64a7653bef7b','b0b3e454-dae7-bef7-a160-7e6f691a0ff0']
['eecd18dc-5852-84fd-71c1-b47ac0eb42b5','f339b5da-c55c-037b-72bb-f2729ec509ec','84c6ef50-9f4c-45eb-26e5-bce543f759b0','a8c5d648-f236-e754-305f-cbffc3662033','9e879501-a713-e63d-3a0d-329ff89784e9','c0783c4f-d81f-4f55-54bd-a20cd5cda08a','ed32b485-0648-bdc8-43f5-49d13e5bd5bf','63303c7a-fa5f-2644-7eb6-ac4f06e0ff48']
['5af43b6e-67ca-62c9-17b9-a4a5fef8a3f9','c210e199-2065-50db-3f52-4d8e320d00dc']
['1fcb5a9e-82f3-9f97-005c-592e50819f3d','3effe804-94a7-9260-29cf-228db3254a34']
['f0878b54-f5e7-2d0e-6c08-b54b8cf8b96d']
['6b71c084-6cf0-204d-7122-e162ab8ba84a','39ede902-3205-610c-51c1-de1a24c718d6']
['e7c99a0c-ad54-4afe-f09d-67791106d667','5da402c8-f4d5-0dc9-6206-0fecee137c66']
['2070622f-c9d8-2203-df1d-0d54d6399c9b','b8ac67ea-31b9-9c3e-f23e-f5b937979b0e','afedd53b-6ea2-04f4-e48a-fe739e227439','283efb3f-16ae-beba-d7f1-ab4749287623','add23ae2-d111-3ccb-ea27-0407d32fa407','f1bbff94-d3cf-fbc2-c43e-9fff2980a1d1','88ad4f5d-29c1-5b06-a0cd-54e60a2d07ec','17abe221-5b8b-19e0-5e93-413f2eb95363']
-
Tuple(Int32, Array(Int64))
(1234817989,[2254772619926532955,9120028858397505560,4555697903102013946,5784362079052877875,-7410772078432030619,7901646768096461004,6733841386518201279,7736560050027905187,2199287578947862030])
(1171957426,[3019483913099890467,-4781013766399904222,-5327852745410412752,7078934595552553093,2990244123355912075,-2544286630298820818])
(275100647,[6155991081669718686,7462222003717329977,-8255668614967296432,-7529819295378879967,-4777308097681484883,-4064480117123591373,6674750820081216293])
(1033685688,[2050663721809231639,-6384194708780112896,-2808232718275215658,1619954721090656792,-5627002805867168609,-6128563945701772338,-7146544521171569603,6504888450989032669])
(180895192,[1199208254069819846,-4069733657855461419])
(135557292,[192577216783361448,-7343112807738526333])
(716914271,[-9207713629233477390])
(1012211222,[-562393447932771686,-6225026423445182831])
(-2109244634,[-1388479317275096889,-1222297392734207149])
(-1647742638,[3396028458740199176,8610993157653131131,-4072576266223306473,-6818310818869145616,-5713972449102020873,8197031236106666677,-1239306987803343619,8267468115072584172])
-
FixedString(4)
Ųç¸
ج_
&RA#
ǻH
w²Æ\r
\'¹Qk
õÁÅE
°†W´
¬ÓØ
T˜ªÜ
-
String
String
String
String
String
String
String
String
String
String
)/VC)%f9
\0ih|;B
\0J"Z,kd
\0m"m]$35
\00
\0(
\0
\0g
\0>
\0XjbW:s<
-
Nullable(String)
)/VC)%f9
\0ih|;B
\0J"Z,kd
\0m"m]$35
\00
\0(
\0
\0g
\0>
\0XjbW:s<
-
Array(String)
['(|ZVAg2F','\0GXjbW','\0<^guT(','\0y M$lZ0','\03','\0p','\0','\0i','\0P']
['\0"}YRG%B','\0T3(E^> p','\0JTaj','\0)*3','\0k%=p','\0Yub$81`X']
['','\0\\<BC','','','\0','\0KV','\0+A~-7g']
['\0vqv(A%','\07OTo)?','\0=l;OP;)J','\0pb','\0','\0Y>p]|]','\05','\0k$C/pnA']
['\0ryz{*p','']
['\07`mjt*G','']
['\0~g']
['\0k','\0 ']
['\0F','\0&h<Bz']
['\0n3;','\0bX(o2]uC','\0up_X\'','\0s','\05j|iS,','\0','\0(y.aRsVz','\0T:64 ]']
-
[77] -124167.6723 ('2061-04-17 21:59:44.573','44ca66ef-335f-7835-3f72-f405ec3e13c8')
[32,110] -141397.7312 ('1979-02-09 03:43:48.526','e5257bd8-b80f-fa73-9824-86d15a5da308')
[68] -67417.0770 ('2080-03-12 14:17:31.269','05bafa6b-3e92-9f15-1104-25e5413f10a6')
-
[-59,-78,-25,-72,-40,-84,95,22,38] 1234817989 )/VC)%f9 123481.7989 o 2.1973467205491123e307 ('2106-02-07','2009-02-16 23:59:49','2007-02-20 10:43:46.989','7e90d9ed-3e18-3818-1f4a-8fc063ff735b') Ų
[82,65,35,-110,-57,-69] 1171957426 \0ih|;B 117195.7426 w 8.887754501811354e307 ('2086-11-02','2007-02-20 10:43:46','2002-10-04 02:54:48.647','50462ea9-fe2f-d033-3f39-171b126331fa') ç¸
[72,119,-78,-58,13,39,-71] 275100647 \0J"Z,kd 27510.0647 w 4.4396706606805647e307 ('2096-02-04','1978-09-20 03:50:47','1974-04-19 01:48:12.192','6da849de-f100-c0cc-9927-a60f01acf065') ج
[81,107,-11,-63,-59,69,-80,-122] 1033685688 \0m"m]$35 103368.5688 w 5.637042481600483e307 ('2106-02-07','2002-10-04 02:54:48','2002-01-28 12:47:02.271','6b5dc860-1d64-40a3-5d73-6910493dc3bf') _
[87,-76] 180895192 \00 18089.5192 h 1.07549012514996e308 ('2013-05-07','1975-09-25 19:39:52','2053-11-20 07:10:58.662','29e75c9e-fd53-4f23-1e85-7066961dbe0e') &R
[22,-84] 135557292 \0( 13555.7292 w 7.700402896226395e307 ('2048-12-21','1974-04-19 01:48:12','1986-04-08 19:07:15.849','b60faa27-fe38-ff30-bda6-6d4f737b3622') A#
[-45] 716914271 \0 71691.4271 h 6.562339881458101e307 ('2013-09-19','1992-09-19 18:51:11','2081-03-06 04:00:55.914','297f7b2f-ec54-178b-623d-6d8244222885') ’Ç
[-40,84] 1012211222 \0g 101221.1222 h 7.539520705557441e307 ('1991-02-02','2002-01-28 12:47:02','1979-01-20 20:39:20.939','556e7945-df65-729e-dcb0-e0ca3a435f2e') »H
[-104,-86] 2185722662 \0> -210924.4634 w 2.143274805821858e307 ('2056-04-25','2039-04-06 20:11:02','2063-07-18 01:46:10.215','8d6df922-9588-7e50-678f-236036acd439') w²
[-36,123,44,60,5,25,-5,-127] 2647224658 \0XjbW:s< -164774.2638 o 2.9425818885529257e307 ('2049-06-05','2053-11-20 07:10:58','1996-11-02 14:35:41.110','bdb39798-af19-13ad-9780-b53edc0f4a21') Æ\r
-

View File

@ -0,0 +1,152 @@
SELECT
toTypeName(i)
FROM generate('i Enum8(\'hello\' = 1, \'world\' = 5)', 1);
SELECT
i
FROM generate('i Enum8(\'hello\' = 1, \'world\' = 5)', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(i)
FROM generate('i Array(Nullable(Enum8(\'hello\' = 1, \'world\' = 5)))', 1);
SELECT
i
FROM generate('i Array(Nullable(Enum8(\'hello\' = 1, \'world\' = 5)))', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(i)s
FROM generate('i Nullable(Enum16(\'h\' = 1, \'w\' = 5 , \'o\' = -200)))', 1);
SELECT
i
FROM generate('i Nullable(Enum16(\'h\' = 1, \'w\' = 5 , \'o\' = -200)))', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(ui64), toTypeName(i64),
toTypeName(ui32), toTypeName(i32),
toTypeName(ui16), toTypeName(i16),
toTypeName(ui8), toTypeName(i8)
FROM generate('ui64 UInt64, i64 Int64, ui32 UInt32, i32 Int32, ui16 UInt16, i16 Int16, ui8 UInt8, i8 Int8', 1);
SELECT
ui64, i64,
ui32, i32,
ui16, i16,
ui8, i8
FROM generate('ui64 UInt64, i64 Int64, ui32 UInt32, i32 Int32, ui16 UInt16, i16 Int16, ui8 UInt8, i8 Int8', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(d), toTypeName(dt), toTypeName(dtm)
FROM generate('d Date, dt DateTime, dtm DateTime(\'Europe/Moscow\')', 1);
SELECT
d, dt, dtm
FROM generate('d Date, dt DateTime, dtm DateTime(\'Europe/Moscow\')', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(dt64), toTypeName(dts64), toTypeName(dtms64)
FROM generate('dt64 DateTime64, dts64 DateTime64(6), dtms64 DateTime64(6 ,\'Europe/Moscow\')', 1);
SELECT
dt64, dts64, dtms64
FROM generate('dt64 DateTime64, dts64 DateTime64(6), dtms64 DateTime64(6 ,\'Europe/Moscow\')', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(f32), toTypeName(f64)
FROM generate('f32 Float32, f64 Float64', 1);
SELECT
f32, f64
FROM generate('f32 Float32, f64 Float64', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(d32), toTypeName(d64), toTypeName(d64)
FROM generate('d32 Decimal32(4), d64 Decimal64(8), d128 Decimal128(16)', 1);
SELECT
d32, d64, d128
FROM generate('d32 Decimal32(4), d64 Decimal64(8), d128 Decimal128(16)', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(i)
FROM generate('i UUID', 1);
SELECT
i
FROM generate('i UUID', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(i)
FROM generate('i Tuple(Int32, Int64)', 1);
SELECT
i
FROM generate('i Tuple(Int32, Int64)', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(i)
FROM generate('i Array(Int8)', 1);
SELECT
i
FROM generate('i Array(Int8)', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(i)
FROM generate('i Array(Nullable(Int32))', 1);
SELECT
i
FROM generate('i Array(Nullable(Int32))', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(i)
FROM generate('i Array(Nullable(UUID))', 1);
SELECT
i
FROM generate('i Array(Nullable(UUID))', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(i)
FROM generate('i Tuple(Int32, Array(Int64))', 1);
SELECT
i
FROM generate('i Tuple(Int32, Array(Int64))', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(i)
FROM generate('i FixedString(4)', 1);
SELECT
i
FROM generate('i FixedString(4)', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(i)
FROM generate('i String', 10);
SELECT
i
FROM generate('i String', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(i)
FROM generate('i Nullable(String)', 1);
SELECT
i
FROM generate('i Nullable(String)', 10, 10, 10, 1);
SELECT '-';
SELECT
toTypeName(i)
FROM generate('i Array(String)', 1);
SELECT
i
FROM generate('i Array(String)', 10, 10, 10, 1);
SELECT '-';
DROP TABLE IF EXISTS test_table;
CREATE TABLE test_table(a Array(Int8), d Decimal32(4), c Tuple(DateTime64(3), UUID)) ENGINE=Memory;
INSERT INTO test_table SELECT * FROM generate('a Array(Int8), d Decimal32(4), c Tuple(DateTime64(3), UUID)', 3, 2, 10, 1);
SELECT * FROM test_table;
DROP TABLE IF EXISTS test_table;
SELECT '-';
DROP TABLE IF EXISTS test_table_2;
CREATE TABLE test_table_2(a Array(Int8), b UInt32, c Nullable(String), d Decimal32(4), e Nullable(Enum16('h' = 1, 'w' = 5 , 'o' = -200)), f Float64, g Tuple(Date, DateTime, DateTime64, UUID), h FixedString(2)) ENGINE=Memory;
INSERT INTO test_table_2 SELECT * FROM generate('a Array(Int8), b UInt32, c Nullable(String), d Decimal32(4), e Nullable(Enum16(\'h\' = 1, \'w\' = 5 , \'o\' = -200)), f Float64, g Tuple(Date, DateTime, DateTime64, UUID), h FixedString(2)', 10, 3, 5, 10);
SELECT * FROM test_table_2;
SELECT '-';
DROP TABLE IF EXISTS test_table_2;

View File

@ -0,0 +1,38 @@
# generate
Generates random data with given schema.
Allows to populate test tables with data.
Supports all data types that can be stored in table except `LowCardinality` and `AggregateFunction`.
```sql
generate('name TypeName[, name TypeName]...', 'limit'[, 'max_array_length'[, 'max_string_length'[, 'random_seed']]]);
```
**Parameters**
- `name` — Name of corresponding column.
- `TypeName` — Type of corresponding column.
- `limit` — Number of rows to generate.
- `max_array_length` — Maximum array length for all generated arrays. Defaults to `10`.
- `max_string_length` — Maximum string length for all generated strings. Defaults to `10`.
- `random_seed` — Specify random seed manually to produce stable results. Defaults to `0` — seed is randomly generated.
**Returned Value**
A table object with requested schema.
## Usage Example
```sql
SELECT * FROM generate('a Array(Int8), d Decimal32(4), c Tuple(DateTime64(3), UUID)', 3, 2, 10, 1);
```
```text
┌─a────────┬────────────d─┬─c──────────────────────────────────────────────────────────────────┐
│ [77] │ -124167.6723 │ ('2061-04-17 21:59:44.573','3f72f405-ec3e-13c8-44ca-66ef335f7835') │
│ [32,110] │ -141397.7312 │ ('1979-02-09 03:43:48.526','982486d1-5a5d-a308-e525-7bd8b80ffa73') │
│ [68] │ -67417.0770 │ ('2080-03-12 14:17:31.269','110425e5-413f-10a6-05ba-fa6b3e929f15') │
└──────────┴──────────────┴────────────────────────────────────────────────────────────────────┘
```
[Original article](https://clickhouse.tech/docs/en/query_language/table_functions/generate/) <!--hide-->

View File

@ -0,0 +1 @@
../../../en/query_language/table_functions/generate.md

View File

@ -0,0 +1 @@
../../../en/query_language/table_functions/generate.md

View File

@ -0,0 +1,37 @@
# generate
Генерирует случайные данные с заданной схемой.
Позволяет заполнять тестовые таблицы данными.
Поддерживает все типы данных, которые могут храниться в таблице, за исключением `LowCardinality` и `AggregateFunction`.
```sql
generate('name TypeName[, name TypeName]...', 'limit'[, 'max_array_length'[, 'max_string_length'[, 'random_seed']]]);
```
**Входные параметры**
- `name` — название соответствующего столбца.
- `TypeName` — тип соответствующего столбца.
- `limit` — количество строк для генерации.
- `max_array_length` — максимальная длина массива для всех сгенерированных массивов. По умолчанию `10`.
- `max_string_length` — максимальная длина строки для всех генерируемых строк. По умолчанию `10`.
- `random_seed` — укажите состояние генератора случайных чисел вручную, чтобы получить стабильные результаты. По умолчанию `0` - генератор инициализируется случайным состоянием.
**Возвращаемое значение**
Объект таблицы с запрошенной схемой.
## Пример
```sql
SELECT * FROM generate('a Array(Int8), d Decimal32(4), c Tuple(DateTime64(3), UUID)', 3, 2, 10, 1);
```
```text
┌─a────────┬────────────d─┬─c──────────────────────────────────────────────────────────────────┐
│ [77] │ -124167.6723 │ ('2061-04-17 21:59:44.573','3f72f405-ec3e-13c8-44ca-66ef335f7835') │
│ [32,110] │ -141397.7312 │ ('1979-02-09 03:43:48.526','982486d1-5a5d-a308-e525-7bd8b80ffa73') │
│ [68] │ -67417.0770 │ ('2080-03-12 14:17:31.269','110425e5-413f-10a6-05ba-fa6b3e929f15') │
└──────────┴──────────────┴────────────────────────────────────────────────────────────────────┘
```
[Оригинальная статья](https://clickhouse.tech/docs/ru/query_language/table_functions/generate/) <!--hide-->

View File

@ -142,6 +142,7 @@ nav:
- 'odbc': 'query_language/table_functions/odbc.md'
- 'hdfs': 'query_language/table_functions/hdfs.md'
- 'input': 'query_language/table_functions/input.md'
- 'generate': 'query_language/table_functions/generate.md'
- 'Dictionaries':
- 'Introduction': 'query_language/dicts/index.md'
- 'External Dictionaries':

View File

@ -168,6 +168,7 @@ nav:
- 'odbc': 'query_language/table_functions/odbc.md'
- 'hdfs': 'query_language/table_functions/hdfs.md'
- 'input': 'query_language/table_functions/input.md'
- 'generate': 'query_language/table_functions/generate.md'
- 'Dictionaries':
- 'Introduction': 'query_language/dicts/index.md'
- 'External Dictionaries':

View File

@ -140,7 +140,8 @@ nav:
- 'odbc': 'query_language/table_functions/odbc.md'
- 'hdfs': 'query_language/table_functions/hdfs.md'
- 'input': 'query_language/table_functions/input.md'
- 'Dictionaries':
- 'generate': 'query_language/table_functions/generate.md'
- 'Dictionaries':
- 'Introduction': 'query_language/dicts/index.md'
- 'External Dictionaries':
- 'General Description': 'query_language/dicts/external_dicts.md'

View File

@ -141,6 +141,7 @@ nav:
- 'odbc': 'query_language/table_functions/odbc.md'
- 'hdfs': 'query_language/table_functions/hdfs.md'
- 'input': 'query_language/table_functions/input.md'
- 'generate': 'query_language/table_functions/generate.md'
- 'Словари':
- 'Введение': 'query_language/dicts/index.md'
- 'Внешние словари':

View File

@ -167,6 +167,7 @@ nav:
- 'odbc': 'query_language/table_functions/odbc.md'
- 'hdfs': 'query_language/table_functions/hdfs.md'
- 'input': 'query_language/table_functions/input.md'
- 'generate': 'query_language/table_functions/generate.md'
- '字典':
- '介绍': 'query_language/dicts/index.md'
- '外部字典':

View File

@ -0,0 +1 @@
../../../en/query_language/table_functions/generate.md