Fuzz JSON table function
Create a table function `fuzzJSON`
An example query:
```
CREATE NAMED COLLECTION json_fuzzer AS json_str={};
SELECT *
FROM fuzzJSON(json_fuzzer, json_str = '{"students" : ["Alice", "Bob"]}', random_seed = 666, max_output_length = 128, probability = 0.9)
LIMIT 3
Query id: 7f802052-efb0-41b4-87fa-03b7dd290e9d
┌─json──────────────────────────────────────────────────────────────────────────────────┐
│ {"ade0yX":[9200406572736542991, true, "sm"]} │
│ {"students":["Alice", "eSN3WO#a6NYTBe0$#OWwyIQ"], "cVoP2BuQugQ":17384271928263249403} │
│ {"students":["Alice", "Bob", "T+-k4+PJGkL%XRRaF2BoeN@&A"]} │
└───────────────────────────────────────────────────────────────────────────────────────┘
```
Next step:
* Generate invalid string
Fixes #35962
add Object('json')
use named collection
2023-11-03 03:59:45 +00:00
|
|
|
#include <TableFunctions/TableFunctionFuzzJSON.h>
|
|
|
|
|
2023-11-28 19:54:55 +00:00
|
|
|
#if USE_RAPIDJSON || USE_SIMDJSON
|
Fuzz JSON table function
Create a table function `fuzzJSON`
An example query:
```
CREATE NAMED COLLECTION json_fuzzer AS json_str={};
SELECT *
FROM fuzzJSON(json_fuzzer, json_str = '{"students" : ["Alice", "Bob"]}', random_seed = 666, max_output_length = 128, probability = 0.9)
LIMIT 3
Query id: 7f802052-efb0-41b4-87fa-03b7dd290e9d
┌─json──────────────────────────────────────────────────────────────────────────────────┐
│ {"ade0yX":[9200406572736542991, true, "sm"]} │
│ {"students":["Alice", "eSN3WO#a6NYTBe0$#OWwyIQ"], "cVoP2BuQugQ":17384271928263249403} │
│ {"students":["Alice", "Bob", "T+-k4+PJGkL%XRRaF2BoeN@&A"]} │
└───────────────────────────────────────────────────────────────────────────────────────┘
```
Next step:
* Generate invalid string
Fixes #35962
add Object('json')
use named collection
2023-11-03 03:59:45 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Storages/checkAndGetLiteralArgument.h>
|
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableFunctionFuzzJSON::parseArguments(const ASTPtr & ast_function, ContextPtr context)
|
|
|
|
{
|
|
|
|
ASTs & args_func = ast_function->children;
|
|
|
|
|
|
|
|
if (args_func.size() != 1)
|
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function '{}' must have arguments", getName());
|
|
|
|
|
|
|
|
auto args = args_func.at(0)->children;
|
|
|
|
configuration = StorageFuzzJSON::getConfiguration(args, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnsDescription TableFunctionFuzzJSON::getActualTableStructure(ContextPtr /*context*/, bool /*is_insert_query*/) const
|
|
|
|
{
|
|
|
|
return ColumnsDescription{{"json", std::make_shared<DataTypeString>()}};
|
|
|
|
}
|
|
|
|
|
|
|
|
StoragePtr TableFunctionFuzzJSON::executeImpl(
|
|
|
|
const ASTPtr & /*ast_function*/,
|
|
|
|
ContextPtr context,
|
|
|
|
const std::string & table_name,
|
|
|
|
ColumnsDescription /*cached_columns*/,
|
|
|
|
bool is_insert_query) const
|
|
|
|
{
|
|
|
|
ColumnsDescription columns = getActualTableStructure(context, is_insert_query);
|
|
|
|
auto res = std::make_shared<StorageFuzzJSON>(
|
|
|
|
StorageID(getDatabaseName(), table_name),
|
|
|
|
columns,
|
|
|
|
/* comment */ String{},
|
|
|
|
configuration);
|
|
|
|
res->startup();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerTableFunctionFuzzJSON(TableFunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<TableFunctionFuzzJSON>(
|
|
|
|
{.documentation
|
|
|
|
= {.description = "Perturbs a JSON string with random variations.",
|
|
|
|
.returned_value = "A table object with a a single column containing perturbed JSON strings."},
|
|
|
|
.allow_readonly = true});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-11-28 19:54:55 +00:00
|
|
|
#endif
|