Support for NULL as random seed; reordered parameters for convenience

This commit is contained in:
Alexey Milovidov 2020-03-07 23:35:55 +03:00
parent 50e2e338ed
commit eb2a78c6b0
5 changed files with 28 additions and 27 deletions

View File

@ -352,10 +352,10 @@ private:
StorageGenerateRandom::StorageGenerateRandom(const StorageID & table_id_, const ColumnsDescription & columns_,
UInt64 max_array_length_, UInt64 max_string_length_, UInt64 random_seed_)
UInt64 max_array_length_, UInt64 max_string_length_, std::optional<UInt64> random_seed_)
: IStorage(table_id_), max_array_length(max_array_length_), max_string_length(max_string_length_)
{
random_seed = random_seed_ ? random_seed_ : randomSeed();
random_seed = random_seed_ ? *random_seed_ : randomSeed();
setColumns(columns_);
}
@ -367,25 +367,25 @@ void registerStorageGenerateRandom(StorageFactory & factory)
ASTs & engine_args = args.engine_args;
if (engine_args.size() > 3)
throw Exception("Storage GenerateRandom requires at most three arguments: "\
"max_array_length, max_string_length, random_seed.",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
throw Exception("Storage GenerateRandom requires at most three arguments: "
"random_seed, max_string_length, max_array_length.",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
UInt64 max_array_length_ = 10;
UInt64 max_string_length_ = 10;
UInt64 random_seed_ = 0; // zero for random
std::optional<UInt64> random_seed;
UInt64 max_string_length = 10;
UInt64 max_array_length = 10;
/// Parsing second argument if present
if (engine_args.size() >= 1)
max_array_length_ = engine_args[0]->as<ASTLiteral &>().value.safeGet<UInt64>();
random_seed = engine_args[2]->as<ASTLiteral &>().value.safeGet<UInt64>();
if (engine_args.size() >= 2)
max_string_length_ = engine_args[1]->as<ASTLiteral &>().value.safeGet<UInt64>();
max_string_length = engine_args[0]->as<ASTLiteral &>().value.safeGet<UInt64>();
if (engine_args.size() == 3)
random_seed_ = engine_args[2]->as<ASTLiteral &>().value.safeGet<UInt64>();
max_array_length = engine_args[1]->as<ASTLiteral &>().value.safeGet<UInt64>();
return StorageGenerateRandom::create(args.table_id, args.columns, max_array_length_, max_string_length_, random_seed_);
return StorageGenerateRandom::create(args.table_id, args.columns, max_array_length, max_string_length, random_seed);
});
}

View File

@ -1,5 +1,6 @@
#pragma once
#include <optional>
#include <ext/shared_ptr_helper.h>
#include <Storages/IStorage.h>
@ -29,7 +30,7 @@ private:
protected:
StorageGenerateRandom(const StorageID & table_id_, const ColumnsDescription & columns_,
UInt64 max_array_length, UInt64 max_string_length, UInt64 random_seed);
UInt64 max_array_length, UInt64 max_string_length, std::optional<UInt64> random_seed);
};
}

View File

@ -36,30 +36,30 @@ StoragePtr TableFunctionGenerateRandom::executeImpl(const ASTPtr & ast_function,
if (args.size() < 1)
throw Exception("Table function '" + getName() + "' requires at least one argument: "
" structure(, max_array_length, max_string_length, random_seed).",
" structure, [random_seed, max_string_length, max_array_length].",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
if (args.size() > 4)
throw Exception("Table function '" + getName() + "' requires at most four arguments: "
" structure, max_array_length, max_string_length, random_seed.",
" structure, [random_seed, max_string_length, max_array_length].",
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 max_array_length = 10;
UInt64 max_string_length = 10;
UInt64 random_seed = 0; // zero for random
UInt64 max_array_length = 10;
std::optional<UInt64> random_seed = 0; // zero for random
/// Parsing second argument if present
if (args.size() >= 2)
max_array_length = args[1]->as<ASTLiteral &>().value.safeGet<UInt64>();
random_seed = args[3]->as<ASTLiteral &>().value.safeGet<UInt64>();
if (args.size() >= 3)
max_string_length = args[2]->as<ASTLiteral &>().value.safeGet<UInt64>();
max_string_length = args[1]->as<ASTLiteral &>().value.safeGet<UInt64>();
if (args.size() == 4)
random_seed = args[3]->as<ASTLiteral &>().value.safeGet<UInt64>();
max_array_length = args[2]->as<ASTLiteral &>().value.safeGet<UInt64>();
ColumnsDescription columns = parseColumnsListFromString(structure, context);

View File

@ -10,7 +10,7 @@ Usage examples:
## Usage in ClickHouse Server
```sql
ENGINE = GenerateRandom(max_array_length, max_string_length, random_seed)
ENGINE = GenerateRandom(random_seed, max_string_length, max_array_length)
```
The `max_array_length` and `max_string_length` parameters specify maximum length of all
@ -25,7 +25,7 @@ It supports all [DataTypes](../../data_types/index.md) that can be stored in a t
**1.** Set up the `generate_engine_table` table:
```sql
CREATE TABLE generate_engine_table (name String, value UInt32) ENGINE=GenerateRandom(3, 5, 1)
CREATE TABLE generate_engine_table (name String, value UInt32) ENGINE = GenerateRandom(1, 5, 3)
```
**2.** Query the data:

View File

@ -5,7 +5,7 @@ Allows to populate test tables with data.
Supports all data types that can be stored in table except `LowCardinality` and `AggregateFunction`.
```sql
generateRandom('name TypeName[, name TypeName]...', 'limit'[, 'max_array_length'[, 'max_string_length'[, 'random_seed']]]);
generateRandom('name TypeName[, name TypeName]...', [, 'random_seed'[, 'max_string_length'[, 'max_array_length']]]);
```
**Parameters**
@ -15,7 +15,7 @@ generateRandom('name TypeName[, name TypeName]...', 'limit'[, 'max_array_length'
- `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.
- `random_seed` — Specify random seed manually to produce stable results. If NULL — seed is randomly generated.
**Returned Value**
@ -25,7 +25,7 @@ A table object with requested schema.
```sql
SELECT * FROM generateRandom('a Array(Int8), d Decimal32(4), c Tuple(DateTime64(3), UUID)', 3, 2, 10, 1);
SELECT * FROM generateRandom('a Array(Int8), d Decimal32(4), c Tuple(DateTime64(3), UUID)', 1, 10, 2);
```
```text
┌─a────────┬────────────d─┬─c──────────────────────────────────────────────────────────────────┐