Skip to content

Commit 1a1ff8a

Browse files
committed
make RandomNumsConfig class
1 parent 628f2d8 commit 1a1ff8a

6 files changed

+165
-146
lines changed

src/fuzzyduck.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ void FuzzyDuck::BeginFuzzing() {
3232
RunQuery("PRAGMA enable_verification");
3333
}
3434
if (!randoms_config_filepath.empty()) {
35-
config = GetConfigFromFile(randoms_config_filepath.c_str());
36-
if (!config.size()) {
37-
config = GetDefaultConfig();
38-
}
35+
config = RandomNumsConfig().GetConfigFromFile(randoms_config_filepath.c_str());
3936
} else {
40-
config = GetDefaultConfig();
37+
config = RandomNumsConfig().GetDefaultConfig();
4138
}
4239
}
4340

src/include/fuzzyduck.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class FuzzyDuck {
3030
bool enable_verification = false;
3131
idx_t timeout = 30;
3232
string randoms_config_filepath;
33+
// RandomNumsConfig config;
3334
unordered_map<RandomPercentagesEnum, idx_t> config;
3435

3536
public:

src/include/random_nums_config.hpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,19 @@ enum class RandomPercentagesEnum : idx_t {
126126

127127
};
128128

129-
unordered_map<RandomPercentagesEnum, idx_t> GetDefaultConfig();
130-
unordered_map<RandomPercentagesEnum, idx_t> GetConfigFromFile(const char *json_string);
131-
string RandomPercentagesEnumToString(RandomPercentagesEnum type);
129+
class RandomNumsConfig {
130+
public:
131+
RandomNumsConfig();
132+
RandomNumsConfig(const char *config_file_path);
133+
~RandomNumsConfig();
132134

133-
} // namespace duckdb
135+
RandomPercentagesEnum percentages_selector;
136+
// unordered_map<RandomPercentagesEnum, idx_t> config;
137+
138+
unordered_map<RandomPercentagesEnum, idx_t> GetDefaultConfig();
139+
unordered_map<RandomPercentagesEnum, idx_t> GetConfigFromFile(const char *json_string);
140+
string RandomPercentagesEnumToString(RandomPercentagesEnum type);
141+
142+
};
143+
144+
}// namespace duckdb

src/include/statement_generator.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ class StatementGenerator {
4141
friend class ExpressionDepthChecker;
4242
friend class AggregateChecker;
4343
friend class WindowChecker;
44+
unordered_map<RandomPercentagesEnum, idx_t> config;
4445

4546
public:
4647
StatementGenerator(ClientContext &context);
4748
StatementGenerator(StatementGenerator &parent);
4849
~StatementGenerator();
4950

5051
public:
51-
unique_ptr<SQLStatement> GenerateStatement(unordered_map<RandomPercentagesEnum, idx_t> config);
52+
unique_ptr<SQLStatement> GenerateStatement();
5253

5354
vector<string> GenerateAllFunctionCalls();
5455

src/random_nums_config.cpp

+122-113
Original file line numberDiff line numberDiff line change
@@ -8,125 +8,134 @@ namespace duckdb {
88

99
using namespace duckdb_yyjson;
1010

11-
unordered_map<RandomPercentagesEnum, idx_t> GetDefaultConfig() {
12-
unordered_map<RandomPercentagesEnum, idx_t> default_config = {
13-
{ RandomPercentagesEnum::ATTACH, 40 },
14-
{ RandomPercentagesEnum::ATTACH_USE, 50 },
15-
{ RandomPercentagesEnum::DELETE, 40 },
16-
{ RandomPercentagesEnum::DETACH, 60 },
17-
{ RandomPercentagesEnum::SELECT, 60 },
18-
{ RandomPercentagesEnum::SET, 30 }
19-
};
20-
return default_config;
21-
}
22-
2311
unordered_map<string, RandomPercentagesEnum> StringToRandomPercentagesEnum = {
24-
{ "attach_percentage", RandomPercentagesEnum::ATTACH },
25-
{ "attach_use_percentage", RandomPercentagesEnum::ATTACH_USE },
26-
{ "attach_read_only", RandomPercentagesEnum::ATTACH_READ_ONLY },
27-
{ "detach_percentage", RandomPercentagesEnum::DETACH },
28-
{ "detach_random_name", RandomPercentagesEnum::DETACH_RANDOM_NAME },
29-
{ "set_percentage", RandomPercentagesEnum::SET },
30-
{ "set_attached_db", RandomPercentagesEnum::SET_ATTACHED_DB },
31-
{ "delete_percentage", RandomPercentagesEnum::DELETE },
32-
{ "select_percentage", RandomPercentagesEnum::SELECT },
33-
{ "select_node_perc", RandomPercentagesEnum::SELECT_NODE },
34-
{ "select_node_is_distinct_perc", RandomPercentagesEnum::SELECT_NODE_IS_DISTINCT },
35-
{ "select_node_from_table_perc", RandomPercentagesEnum::SELECT_NODE_FROM_TABLE },
36-
{ "select_node_where_perc", RandomPercentagesEnum::SELECT_NODE_WHERE },
37-
{ "select_node_having_perc", RandomPercentagesEnum::SELECT_NODE_HAVING },
38-
{ "select_node_groups_perc", RandomPercentagesEnum::SELECT_NODE_GROUPS },
39-
{ "select_node_group_by_perc", RandomPercentagesEnum::SELECT_NODE_GROUP_BY },
40-
{ "select_node_qualify_perc", RandomPercentagesEnum::SELECT_NODE_QUALIFY },
41-
{ "select_node_aggregate_perc", RandomPercentagesEnum::SELECT_NODE_AGGREGATE },
42-
{ "select_node_sample_perc", RandomPercentagesEnum::SELECT_NODE_SAMPLE },
43-
{ "select_node_sample_is_perc", RandomPercentagesEnum::SELECT_NODE_SAMPLE_IS_PERC },
44-
{ "select_node_sample_size", RandomPercentagesEnum::SELECT_NODE_SAMPLE_SIZE },
45-
{ "result_modifiers", RandomPercentagesEnum::RESULT_MODIFIERS },
46-
{ "limit_percent_modifier", RandomPercentagesEnum::LIMIT_PERCENT_MODIFIER },
47-
{ "limit_percent_modifier_limit", RandomPercentagesEnum::LIMIT_PERCENT_MODIFIER_LIMIT },
48-
{ "limit_percent_modifier_offset", RandomPercentagesEnum::LIMIT_PERCENT_MODIFIER_OFFSET },
49-
{ "limit_modifier_limit", RandomPercentagesEnum::LIMIT_MODIFIER_LIMIT },
50-
{ "limit_modifier_offset", RandomPercentagesEnum::LIMIT_MODIFIER_OFFSET },
51-
{ "create_percentage", RandomPercentagesEnum::CREATE },
52-
{ "create_generate_select", RandomPercentagesEnum::CREATE_GENERATE_SELECT },
53-
{ "create_num_cols", RandomPercentagesEnum::CREATE_NUM_COLS },
54-
{ "table_ref_base_table_ref_perc", RandomPercentagesEnum::TABLE_REF_BASE_TABLE_REF_PERC },
55-
{ "table_ref_expression_list_ref", RandomPercentagesEnum::TABLE_REF_EXPRESSION_LIST_REF },
56-
{ "table_ref_join_ref", RandomPercentagesEnum::TABLE_REF_JOIN_REF },
57-
{ "join_ref_cross", RandomPercentagesEnum::JOIN_REF_CROSS },
58-
{ "join_ref_asof", RandomPercentagesEnum::JOIN_REF_ASOF },
59-
{ "join_ref_natural", RandomPercentagesEnum::JOIN_REF_NATURAL },
60-
{ "join_ref_positional", RandomPercentagesEnum::JOIN_REF_POSITIONAL },
61-
{ "join_ref_general_expression", RandomPercentagesEnum::JOIN_REF_GENERAL_EXPRESSION },
62-
{ "expression_column_ref", RandomPercentagesEnum::EXPRESSION_COLUMN_REF },
63-
{ "expression_constant", RandomPercentagesEnum::EXPRESSION_CONSTANT },
64-
{ "expression_subquery", RandomPercentagesEnum::EXPRESSION_SUBQUERY },
65-
{ "constant_value_bigint", RandomPercentagesEnum::CONSTANT_VALUE_BIGINT },
66-
{ "constant_value_to_string", RandomPercentagesEnum::CONSTANT_VALUE_TO_STRING },
67-
{ "function_aggregate_window_function", RandomPercentagesEnum::FUNCTION_AGGREGATE_WINDOW_FUNCTION },
68-
{ "function_aggregate_order_by", RandomPercentagesEnum::FUNCTION_AGGREGATE_ORDER_BY },
69-
{ "function_aggregate_random_expression", RandomPercentagesEnum::FUNCTION_AGGREGATE_RANDOM_EXPRESSION },
70-
{ "function_aggregate_distinct", RandomPercentagesEnum::FUNCTION_AGGREGATE_DISTINCT },
71-
{ "window_function_partitions", RandomPercentagesEnum::WINDOW_FUNCTION_PARTITIONS },
72-
{ "window_function_orders", RandomPercentagesEnum::WINDOW_FUNCTION_ORDERS },
73-
{ "window_function_ignore_nulls", RandomPercentagesEnum::WINDOW_FUNCTION_IGNORE_NULLS },
74-
{ "window_function_result_offset", RandomPercentagesEnum::WINDOW_FUNCTION_RESULT_OFFSET },
75-
{ "window_function_result_default", RandomPercentagesEnum::WINDOW_FUNCTION_RESULT_DEFAULT },
76-
{ "star_relation_name", RandomPercentagesEnum::STAR_RELATION_NAME },
77-
{ "star_column_name_exclude_list", RandomPercentagesEnum::STAR_COLUMN_NAME_EXCLUDE_LIST },
78-
{ "star_column_name", RandomPercentagesEnum::STAR_COLUMN_NAME },
79-
{ "star_columns_true", RandomPercentagesEnum::STAR_COLUMNS_TRUE },
80-
{ "star_columns_true_lambda", RandomPercentagesEnum::STAR_COLUMNS_TRUE_LAMBDA },
81-
{ "relational_name_choose_current", RandomPercentagesEnum::RELATIONAL_NAME_CHOOSE_CURRENT },
82-
{ "column_names_choose_current", RandomPercentagesEnum::COLUMN_NAMES_CHOOSE_CURRENT }
12+
{ "attach_percentage", RandomPercentagesEnum::ATTACH },
13+
{ "attach_use_percentage", RandomPercentagesEnum::ATTACH_USE },
14+
{ "attach_read_only", RandomPercentagesEnum::ATTACH_READ_ONLY },
15+
{ "detach_percentage", RandomPercentagesEnum::DETACH },
16+
{ "detach_random_name", RandomPercentagesEnum::DETACH_RANDOM_NAME },
17+
{ "set_percentage", RandomPercentagesEnum::SET },
18+
{ "set_attached_db", RandomPercentagesEnum::SET_ATTACHED_DB },
19+
{ "delete_percentage", RandomPercentagesEnum::DELETE },
20+
{ "select_percentage", RandomPercentagesEnum::SELECT },
21+
{ "select_node_perc", RandomPercentagesEnum::SELECT_NODE },
22+
{ "select_node_is_distinct_perc", RandomPercentagesEnum::SELECT_NODE_IS_DISTINCT },
23+
{ "select_node_from_table_perc", RandomPercentagesEnum::SELECT_NODE_FROM_TABLE },
24+
{ "select_node_where_perc", RandomPercentagesEnum::SELECT_NODE_WHERE },
25+
{ "select_node_having_perc", RandomPercentagesEnum::SELECT_NODE_HAVING },
26+
{ "select_node_groups_perc", RandomPercentagesEnum::SELECT_NODE_GROUPS },
27+
{ "select_node_group_by_perc", RandomPercentagesEnum::SELECT_NODE_GROUP_BY },
28+
{ "select_node_qualify_perc", RandomPercentagesEnum::SELECT_NODE_QUALIFY },
29+
{ "select_node_aggregate_perc", RandomPercentagesEnum::SELECT_NODE_AGGREGATE },
30+
{ "select_node_sample_perc", RandomPercentagesEnum::SELECT_NODE_SAMPLE },
31+
{ "select_node_sample_is_perc", RandomPercentagesEnum::SELECT_NODE_SAMPLE_IS_PERC },
32+
{ "select_node_sample_size", RandomPercentagesEnum::SELECT_NODE_SAMPLE_SIZE },
33+
{ "result_modifiers", RandomPercentagesEnum::RESULT_MODIFIERS },
34+
{ "limit_percent_modifier", RandomPercentagesEnum::LIMIT_PERCENT_MODIFIER },
35+
{ "limit_percent_modifier_limit", RandomPercentagesEnum::LIMIT_PERCENT_MODIFIER_LIMIT },
36+
{ "limit_percent_modifier_offset", RandomPercentagesEnum::LIMIT_PERCENT_MODIFIER_OFFSET },
37+
{ "limit_modifier_limit", RandomPercentagesEnum::LIMIT_MODIFIER_LIMIT },
38+
{ "limit_modifier_offset", RandomPercentagesEnum::LIMIT_MODIFIER_OFFSET },
39+
{ "create_percentage", RandomPercentagesEnum::CREATE },
40+
{ "create_generate_select", RandomPercentagesEnum::CREATE_GENERATE_SELECT },
41+
{ "create_num_cols", RandomPercentagesEnum::CREATE_NUM_COLS },
42+
{ "table_ref_base_table_ref_perc", RandomPercentagesEnum::TABLE_REF_BASE_TABLE_REF_PERC },
43+
{ "table_ref_expression_list_ref", RandomPercentagesEnum::TABLE_REF_EXPRESSION_LIST_REF },
44+
{ "table_ref_join_ref", RandomPercentagesEnum::TABLE_REF_JOIN_REF },
45+
{ "join_ref_cross", RandomPercentagesEnum::JOIN_REF_CROSS },
46+
{ "join_ref_asof", RandomPercentagesEnum::JOIN_REF_ASOF },
47+
{ "join_ref_natural", RandomPercentagesEnum::JOIN_REF_NATURAL },
48+
{ "join_ref_positional", RandomPercentagesEnum::JOIN_REF_POSITIONAL },
49+
{ "join_ref_general_expression", RandomPercentagesEnum::JOIN_REF_GENERAL_EXPRESSION },
50+
{ "expression_column_ref", RandomPercentagesEnum::EXPRESSION_COLUMN_REF },
51+
{ "expression_constant", RandomPercentagesEnum::EXPRESSION_CONSTANT },
52+
{ "expression_subquery", RandomPercentagesEnum::EXPRESSION_SUBQUERY },
53+
{ "constant_value_bigint", RandomPercentagesEnum::CONSTANT_VALUE_BIGINT },
54+
{ "constant_value_to_string", RandomPercentagesEnum::CONSTANT_VALUE_TO_STRING },
55+
{ "function_aggregate_window_function", RandomPercentagesEnum::FUNCTION_AGGREGATE_WINDOW_FUNCTION },
56+
{ "function_aggregate_order_by", RandomPercentagesEnum::FUNCTION_AGGREGATE_ORDER_BY },
57+
{ "function_aggregate_random_expression", RandomPercentagesEnum::FUNCTION_AGGREGATE_RANDOM_EXPRESSION },
58+
{ "function_aggregate_distinct", RandomPercentagesEnum::FUNCTION_AGGREGATE_DISTINCT },
59+
{ "window_function_partitions", RandomPercentagesEnum::WINDOW_FUNCTION_PARTITIONS },
60+
{ "window_function_orders", RandomPercentagesEnum::WINDOW_FUNCTION_ORDERS },
61+
{ "window_function_ignore_nulls", RandomPercentagesEnum::WINDOW_FUNCTION_IGNORE_NULLS },
62+
{ "window_function_result_offset", RandomPercentagesEnum::WINDOW_FUNCTION_RESULT_OFFSET },
63+
{ "window_function_result_default", RandomPercentagesEnum::WINDOW_FUNCTION_RESULT_DEFAULT },
64+
{ "star_relation_name", RandomPercentagesEnum::STAR_RELATION_NAME },
65+
{ "star_column_name_exclude_list", RandomPercentagesEnum::STAR_COLUMN_NAME_EXCLUDE_LIST },
66+
{ "star_column_name", RandomPercentagesEnum::STAR_COLUMN_NAME },
67+
{ "star_columns_true", RandomPercentagesEnum::STAR_COLUMNS_TRUE },
68+
{ "star_columns_true_lambda", RandomPercentagesEnum::STAR_COLUMNS_TRUE_LAMBDA },
69+
{ "relational_name_choose_current", RandomPercentagesEnum::RELATIONAL_NAME_CHOOSE_CURRENT },
70+
{ "column_names_choose_current", RandomPercentagesEnum::COLUMN_NAMES_CHOOSE_CURRENT }
8371
};
8472

85-
void ParseJsonObj(yyjson_val *obj, unordered_map<RandomPercentagesEnum, idx_t> &config_from_file) {
86-
yyjson_obj_iter iter;
87-
yyjson_obj_iter_init(obj, &iter);
88-
size_t idx, max;
89-
yyjson_val *key, *val;
90-
yyjson_obj_foreach(obj, idx, max, key, val) {
91-
const char* root_key = yyjson_get_str(key);
92-
auto it = StringToRandomPercentagesEnum.find(root_key);
93-
if (it != StringToRandomPercentagesEnum.end()) {
94-
RandomPercentagesEnum perc_type = it->second;
95-
auto perc_value = yyjson_get_str(val);
96-
if (perc_value) {
97-
config_from_file[perc_type] = std::stoi(perc_value);
98-
}
99-
}
100-
if (yyjson_is_obj(val)) {
101-
ParseJsonObj(val, config_from_file);
102-
}
103-
}
73+
RandomNumsConfig::RandomNumsConfig() {
74+
GetDefaultConfig();
10475
}
10576

77+
RandomNumsConfig::RandomNumsConfig(const char *config_file_path) {
78+
GetConfigFromFile(config_file_path);
79+
}
10680

81+
RandomNumsConfig::~RandomNumsConfig() {
82+
}
10783

108-
unordered_map<RandomPercentagesEnum, idx_t> GetConfigFromFile(const char *json_string) {
109-
110-
unordered_map<RandomPercentagesEnum, idx_t> config_from_file;
111-
auto doc = yyjson_read_file(json_string, YYJSON_READ_NOFLAG, NULL, NULL);
112-
if (doc) {
113-
yyjson_val *root = yyjson_doc_get_root(doc);
114-
if (yyjson_is_obj(root)) {
115-
ParseJsonObj(root, config_from_file);
116-
}
117-
yyjson_doc_free(doc);
118-
} else {
119-
// Couldn't read JSON with percentages config
120-
yyjson_doc_free(doc);
121-
return GetDefaultConfig();
122-
}
123-
// set values of missing statement types to 0
124-
for (idx_t i = 0; i < static_cast<int>(RandomPercentagesEnum::COUNT); ++i) {
125-
RandomPercentagesEnum statement_type = static_cast<RandomPercentagesEnum>(i);
126-
if (config_from_file.find(statement_type) == config_from_file.end()) {
127-
config_from_file[statement_type] = 0;
128-
}
129-
}
130-
return config_from_file;
84+
unordered_map<RandomPercentagesEnum, idx_t> RandomNumsConfig::GetDefaultConfig() {
85+
unordered_map<RandomPercentagesEnum, idx_t> default_config = {
86+
{ RandomPercentagesEnum::ATTACH, 40 },
87+
{ RandomPercentagesEnum::ATTACH_USE, 50 },
88+
{ RandomPercentagesEnum::DELETE, 40 },
89+
{ RandomPercentagesEnum::DETACH, 60 },
90+
{ RandomPercentagesEnum::SELECT, 60 },
91+
{ RandomPercentagesEnum::SET, 30 }
92+
};
93+
return default_config;
13194
}
95+
96+
void ParseJsonObj(yyjson_val *obj, unordered_map<RandomPercentagesEnum, idx_t> &config_from_file) {
97+
yyjson_obj_iter iter;
98+
yyjson_obj_iter_init(obj, &iter);
99+
size_t idx, max;
100+
yyjson_val *key, *val;
101+
yyjson_obj_foreach(obj, idx, max, key, val) {
102+
const char* root_key = yyjson_get_str(key);
103+
auto it = StringToRandomPercentagesEnum.find(root_key);
104+
if (it != StringToRandomPercentagesEnum.end()) {
105+
RandomPercentagesEnum perc_type = it->second;
106+
auto perc_value = yyjson_get_str(val);
107+
if (perc_value) {
108+
config_from_file[perc_type] = std::stoi(perc_value);
109+
}
110+
}
111+
if (yyjson_is_obj(val)) {
112+
ParseJsonObj(val, config_from_file);
113+
}
114+
}
115+
}
116+
117+
unordered_map<RandomPercentagesEnum, idx_t> RandomNumsConfig::GetConfigFromFile(const char *json_string) {
118+
119+
unordered_map<RandomPercentagesEnum, idx_t> config_from_file;
120+
auto doc = yyjson_read_file(json_string, YYJSON_READ_NOFLAG, NULL, NULL);
121+
if (doc) {
122+
yyjson_val *root = yyjson_doc_get_root(doc);
123+
if (yyjson_is_obj(root)) {
124+
ParseJsonObj(root, config_from_file);
125+
}
126+
yyjson_doc_free(doc);
127+
} else {
128+
// Couldn't read JSON with percentages config
129+
yyjson_doc_free(doc);
130+
return GetDefaultConfig();
131+
}
132+
// set values of missing statement types to 0
133+
for (idx_t i = 0; i < static_cast<int>(RandomPercentagesEnum::COUNT); ++i) {
134+
RandomPercentagesEnum statement_type = static_cast<RandomPercentagesEnum>(i);
135+
if (config_from_file.find(statement_type) == config_from_file.end()) {
136+
config_from_file[statement_type] = 0;
137+
}
138+
}
139+
return config_from_file;
140+
}
132141
} // namespace duckdb

0 commit comments

Comments
 (0)