This commit is contained in:
Alexander Kuzmenkov 2020-07-07 19:31:58 +03:00
parent ac436c79eb
commit 1d1b49340c
3 changed files with 28 additions and 52 deletions

View File

@ -240,10 +240,6 @@ void QueryFuzzer::fuzzColumnLikeExpressionList(ASTPtr ast)
fprintf(stderr, "no random col!\n");
}
}
/*
fuzz(impl->children);
*/
}
void QueryFuzzer::fuzz(ASTs & asts)
@ -259,8 +255,6 @@ void QueryFuzzer::fuzz(ASTPtr & ast)
if (!ast)
return;
//fprintf(stderr, "name: %s\n", demangle(typeid(*ast).name()).c_str());
if (auto * with_union = typeid_cast<ASTSelectWithUnionQuery *>(ast.get()))
{
fuzz(with_union->list_of_selects);
@ -321,55 +315,37 @@ void QueryFuzzer::fuzz(ASTPtr & ast)
{
fuzz(ast->children);
}
/*
if (auto * with_alias = dynamic_cast<ASTWithAlias *>(ast.get()))
{
int dice = fuzz_rand() % 20;
if (dice == 0)
{
with_alias->alias = aliases[fuzz_rand() % aliases.size()];
}
else if (dice < 5)
{
with_alias->alias = "";
}
}
*/
}
/*
* This functions collects various parts of query that we can then substitute
* to a query being fuzzed.
*
* TODO: we just stop remembering new parts after our corpus reaches certain size.
* This is boring, should implement a random replacement of existing parst with
* small probability. Do this after we add this fuzzer to CI and fix all the
* problems it can routinely find even in this boring version.
*/
void QueryFuzzer::collectFuzzInfoMain(const ASTPtr ast)
{
collectFuzzInfoRecurse(ast);
/*
with_alias.clear();
for (const auto & [name, value] : with_alias_map)
{
with_alias.push_back(value);
//fprintf(stderr, "alias %s\n", value->formatForErrorMessage().c_str());
}
*/
aliases.clear();
for (const auto & alias : aliases_set)
{
aliases.push_back(alias);
//fprintf(stderr, "alias %s\n", alias.c_str());
}
column_like.clear();
for (const auto & [name, value] : column_like_map)
{
column_like.push_back(value);
//fprintf(stderr, "column %s\n", name.c_str());
}
table_like.clear();
for (const auto & [name, value] : table_like_map)
{
table_like.push_back(value);
//fprintf(stderr, "table %s\n", name.c_str());
}
}
@ -440,19 +416,9 @@ void QueryFuzzer::collectFuzzInfoRecurse(const ASTPtr ast)
void QueryFuzzer::fuzzMain(ASTPtr & ast)
{
/*
std::cerr << "before: " << std::endl;
ast->dumpTree(std::cerr);
*/
collectFuzzInfoMain(ast);
fuzz(ast);
/*
std::cerr << "after: " << std::endl;
ast->dumpTree(std::cerr);
*/
std::cout << std::endl;
formatAST(*ast, std::cout);
std::cout << std::endl << std::endl;

View File

@ -4,6 +4,7 @@
#include <unordered_map>
#include <vector>
#include <Common/randomSeed.h>
#include <Common/Stopwatch.h>
#include <Core/Field.h>
#include <Parsers/IAST.h>
@ -11,17 +12,21 @@
namespace DB
{
/*
* This is an AST-based query fuzzer that makes random modifications to query
* AST, changing numbers, list of columns, functions, etc. It remembers part of
* queries it fuzzed previously, and can substitute these parts to new fuzzed
* queries, so you want to feed it a lot of queries to get some interesting mix
* of them. Normally we feed SQL regression tests to it.
*/
struct QueryFuzzer
{
//pcg64 fuzz_rand{static_cast<UInt64>(rand())};
pcg64 fuzz_rand{clock_gettime_ns()};
// Collection of asts with alias.
/*
std::unordered_map<std::string, ASTPtr> with_alias_map;
std::vector<ASTPtr> with_alias;
*/
pcg64 fuzz_rand{randomSeed()};
// These arrays hold parts of queries that we can substitute into the query
// we are currently fuzzing. We add some part from each new query we are asked
// to fuzz, and keep this state between queries, so the fuzzing output becomes
// more interesting over time, as the queries mix.
std::unordered_set<std::string> aliases_set;
std::vector<std::string> aliases;
@ -31,6 +36,11 @@ struct QueryFuzzer
std::unordered_map<std::string, const ASTPtr> table_like_map;
std::vector<const ASTPtr> table_like;
// This is the only function you have to call -- it will modify the passed
// ASTPtr to point to new AST with some random changes.
void fuzzMain(ASTPtr & ast);
// Variuos helper functions follow, normally you shouldn't have to call them.
Field getRandomField(int type);
Field fuzzField(Field field);
ASTPtr getRandomColumnLike();
@ -43,7 +53,6 @@ struct QueryFuzzer
void addTableLike(const ASTPtr ast);
void addColumnLike(const ASTPtr ast);
void collectFuzzInfoRecurse(const ASTPtr ast);
void fuzzMain(ASTPtr & ast);
};
}

View File

@ -17,6 +17,7 @@ SRCS(
main.cpp
client/Client.cpp
client/QueryFuzzer.cpp
client/ConnectionParameters.cpp
client/Suggest.cpp
extract-from-config/ExtractFromConfig.cpp