Skip to content

Commit

Permalink
when StatementGenerator object created from (*this), its verification…
Browse files Browse the repository at this point in the history
…_enabled is false by default

when StatementGenerator object created from (*this), its verification_enabled is false by default

fix GenerateStarexpression to return only star expression, when verification_enabled=true

fix GenerateStarexpression to return only star expression, when verification_enabled=true

fix GenerateStarexpression to return only star expression, when verification_enabled=true

clean up

fix GenerateStarexpression to return only star expression, when verification_enabled=true
  • Loading branch information
hmeriann committed Jul 17, 2024
1 parent 3d62f09 commit 6ac497d
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 213 deletions.
8 changes: 4 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[submodule "duckdb"]
path = duckdb
url = https://github.com/duckdb/duckdb.git
branch = main
[submodule "extension-ci-tools"]
path = extension-ci-tools
url = https://github.com/duckdb/extension-ci-tools
branch = main
[submodule "duckdb"]
path = duckdb
url = https://github.com/hmeriann/duckdb.git
branch = add-larger-ingestion-from-tmonster
278 changes: 138 additions & 140 deletions src/fuzzyduck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,168 +7,166 @@

namespace duckdb {

FuzzyDuck::FuzzyDuck(ClientContext &context) : context(context) {}
FuzzyDuck::FuzzyDuck(ClientContext &context) : context(context) {}

FuzzyDuck::~FuzzyDuck() {}
FuzzyDuck::~FuzzyDuck() {}

void FuzzyDuck::BeginFuzzing() {
auto &random_engine = RandomEngine::Get(context);
if (seed == 0) {
seed = random_engine.NextRandomInteger();
}
random_engine.SetSeed(seed);
if (max_queries == 0) {
throw BinderException("Provide a max_queries argument greater than 0");
}
if (!complete_log.empty()) {
auto &fs = FileSystem::GetFileSystem(context);
TryRemoveFile(complete_log);
complete_log_handle =
fs.OpenFile(complete_log, FileFlags::FILE_FLAGS_WRITE | FileFlags::FILE_FLAGS_FILE_CREATE_NEW);
}
if (enable_verification) {
RunQuery("PRAGMA enable_verification");
}
void FuzzyDuck::BeginFuzzing() {
auto &random_engine = RandomEngine::Get(context);
if (seed == 0) {
seed = random_engine.NextRandomInteger();
}

void FuzzyDuck::EndFuzzing() {
if (complete_log_handle) {
complete_log_handle->Close();
}
random_engine.SetSeed(seed);
if (max_queries == 0) {
throw BinderException("Provide a max_queries argument greater than 0");
}

void FuzzyDuck::Fuzz() {
BeginFuzzing();
for (idx_t i = 0; i < max_queries; i++) {
LogMessage("Query " + to_string(i) + "\n");
auto query = GenerateQuery();
RunQuery(std::move(query));
}
EndFuzzing();
if (!complete_log.empty()) {
auto &fs = FileSystem::GetFileSystem(context);
TryRemoveFile(complete_log);
complete_log_handle =
fs.OpenFile(complete_log, FileFlags::FILE_FLAGS_WRITE | FileFlags::FILE_FLAGS_FILE_CREATE_NEW);
}

void FuzzyDuck::FuzzAllFunctions() {
StatementGenerator generator(context);
auto queries = generator.GenerateAllFunctionCalls();

if (max_queries == 0) {
max_queries = queries.size();
}

std::default_random_engine e(seed);
std::shuffle(std::begin(queries), std::end(queries), e);
BeginFuzzing();
for (auto &query : queries) {
RunQuery(std::move(query));
}
EndFuzzing();
}

string FuzzyDuck::GenerateQuery() {
// generate statement
StatementGenerator generator(context);
generator.verification_enabled = enable_verification;
// accumulate statement(s)
auto statement = string("");
if (generator.RandomPercentage(10)) {
// multi statement
idx_t number_of_statements = generator.RandomValue(1000);
LogTask("Generating Multi-Statement query of " + to_string(number_of_statements) + " statements with seed " +
to_string(seed));
for (idx_t i = 0; i < number_of_statements; i++) {
statement += generator.GenerateStatement()->ToString() + "; ";
}
} else {
// normal statement
LogTask("Generating Single-Statement query with seed " + to_string(seed));
auto generated_statement = generator.GenerateStatement();
statement = generated_statement->ToString();
// D_ASSERT(1);
}
return statement;
if (enable_verification) {
RunQuery("PRAGMA enable_verification");
}
}

void sleep_thread(Connection *con, atomic<bool> *is_active, atomic<bool> *timed_out, idx_t timeout_duration) {
// timeout is given in seconds
// we wait 10ms per iteration, so timeout * 100 gives us the amount of
// iterations
for (size_t i = 0; i < (size_t)(timeout_duration * 100) && *is_active; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
if (*is_active) {
*timed_out = true;
con->Interrupt();
}
void FuzzyDuck::EndFuzzing() {
if (complete_log_handle) {
complete_log_handle->Close();
}
}

void FuzzyDuck::Fuzz() {
BeginFuzzing();
for (idx_t i = 0; i < max_queries; i++) {
LogMessage("Query " + to_string(i) + "\n");
auto query = GenerateQuery();
RunQuery(std::move(query));
}
EndFuzzing();
}

void FuzzyDuck::RunQuery(string query) {
LogQuery(query + ";");

Connection con(*context.db);
atomic<bool> is_active(true);
atomic<bool> timed_out(false);
std::thread interrupt_thread(sleep_thread, &con, &is_active, &timed_out, timeout);
void FuzzyDuck::FuzzAllFunctions() {
StatementGenerator generator(context);
auto queries = generator.GenerateAllFunctionCalls();

auto result = con.Query(query);
is_active = false;
interrupt_thread.join();
if (timed_out) {
LogMessage("TIMEOUT");
} else if (result->HasError()) {
LogMessage("EXECUTION ERROR: " + result->GetError());
} else {
LogMessage("EXECUTION SUCCESS!");
}
if (max_queries == 0) {
max_queries = queries.size();
}

void FuzzyDuck::TryRemoveFile(const string &path) {
auto &fs = FileSystem::GetFileSystem(context);
if (fs.FileExists(path)) {
fs.RemoveFile(path);
}
std::default_random_engine e(seed);
std::shuffle(std::begin(queries), std::end(queries), e);
BeginFuzzing();
for (auto &query : queries) {
RunQuery(std::move(query));
}
EndFuzzing();
}

string FuzzyDuck::GenerateQuery() {
// generate statement
StatementGenerator generator(context);
generator.verification_enabled = enable_verification;
// accumulate statement(s)
auto statement = string("");
if (generator.RandomPercentage(10)) {
// multi statement
idx_t number_of_statements = generator.RandomValue(1000);
LogTask("Generating Multi-Statement query of " + to_string(number_of_statements) + " statements with seed " +
to_string(seed));
for (idx_t i = 0; i < number_of_statements; i++) {
statement += generator.GenerateStatement()->ToString() + "; ";
}
} else {
// normal statement
LogTask("Generating Single-Statement query with seed " + to_string(seed));
statement = generator.GenerateStatement()->ToString();
}
return statement;
}

void sleep_thread(Connection *con, atomic<bool> *is_active, atomic<bool> *timed_out, idx_t timeout_duration) {
// timeout is given in seconds
// we wait 10ms per iteration, so timeout * 100 gives us the amount of
// iterations
for (size_t i = 0; i < (size_t)(timeout_duration * 100) && *is_active; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
if (*is_active) {
*timed_out = true;
con->Interrupt();
}
}

void FuzzyDuck::RunQuery(string query) {
LogQuery(query + ";");

Connection con(*context.db);
atomic<bool> is_active(true);
atomic<bool> timed_out(false);
std::thread interrupt_thread(sleep_thread, &con, &is_active, &timed_out, timeout);

auto result = con.Query(query);
is_active = false;
interrupt_thread.join();
if (timed_out) {
LogMessage("TIMEOUT");
} else if (result->HasError()) {
LogMessage("EXECUTION ERROR: " + result->GetError());
} else {
LogMessage("EXECUTION SUCCESS!");
}
}

void FuzzyDuck::LogMessage(const string &message) {
if (!verbose_output) {
return;
}
Printer::Print(message);
void FuzzyDuck::TryRemoveFile(const string &path) {
auto &fs = FileSystem::GetFileSystem(context);
if (fs.FileExists(path)) {
fs.RemoveFile(path);
}
}

void FuzzyDuck::LogTask(const string &message) {
if (verbose_output) {
LogMessage(message + "\n");
}
LogToCurrent(message);
void FuzzyDuck::LogMessage(const string &message) {
if (!verbose_output) {
return;
}
Printer::Print(message);
}

void FuzzyDuck::LogQuery(const string &message) {
if (verbose_output) {
LogMessage(message + "\n");
}
LogToCurrent(message);
LogToComplete(message);
void FuzzyDuck::LogTask(const string &message) {
if (verbose_output) {
LogMessage(message + "\n");
}
LogToCurrent(message);
}

void FuzzyDuck::LogToCurrent(const string &message) {
if (log.empty()) {
return;
}
auto &fs = FileSystem::GetFileSystem(context);
TryRemoveFile(log);
auto file = fs.OpenFile(log, FileFlags::FILE_FLAGS_WRITE | FileFlags::FILE_FLAGS_FILE_CREATE_NEW);
file->Write((void *)message.c_str(), message.size());
file->Sync();
file->Close();
void FuzzyDuck::LogQuery(const string &message) {
if (verbose_output) {
LogMessage(message + "\n");
}
LogToCurrent(message);
LogToComplete(message);
}

void FuzzyDuck::LogToComplete(const string &message) {
if (!complete_log_handle) {
return;
}
complete_log_handle->Write((void *)message.c_str(), message.size());
complete_log_handle->Write((void *)"\n", 1);
complete_log_handle->Sync();
void FuzzyDuck::LogToCurrent(const string &message) {
if (log.empty()) {
return;
}
auto &fs = FileSystem::GetFileSystem(context);
TryRemoveFile(log);
auto file = fs.OpenFile(log, FileFlags::FILE_FLAGS_WRITE | FileFlags::FILE_FLAGS_FILE_CREATE_NEW);
file->Write((void *)message.c_str(), message.size());
file->Sync();
file->Close();
}

void FuzzyDuck::LogToComplete(const string &message) {
if (!complete_log_handle) {
return;
}
complete_log_handle->Write((void *)message.c_str(), message.size());
complete_log_handle->Write((void *)"\n", 1);
complete_log_handle->Sync();
}

} // namespace duckdb
2 changes: 1 addition & 1 deletion src/include/statement_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class StatementGenerator {
unique_ptr<ParsedExpression> GenerateColumnRef();
unique_ptr<ParsedExpression> GenerateFunction();
unique_ptr<ParsedExpression> GenerateOperator();
unique_ptr<ParsedExpression> GenerateWindowFunction(optional_ptr<AggregateFunction> function = nullptr, bool verification_enabled = false);
unique_ptr<ParsedExpression> GenerateWindowFunction(optional_ptr<AggregateFunction> function = nullptr);
unique_ptr<ParsedExpression> GenerateConjunction();
unique_ptr<ParsedExpression> GenerateStar();
unique_ptr<ParsedExpression> GenerateLambda();
Expand Down
Loading

0 comments on commit 6ac497d

Please sign in to comment.