Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option flag to exclude certain statements #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions grammar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -465,22 +465,39 @@ upsert_stmt::upsert_stmt(prod *p, struct scope *s, table *v)

shared_ptr<prod> statement_factory(struct scope *s)
{
const auto check_d42 = [s](const std::string& stmt) {
return !s->excluded_stmts.count(stmt) && d42() == 1;
};

const auto check_d6 = [s](const int value, const std::string& stmt) {
return !s->excluded_stmts.count(stmt) && d6() > value;
};

try {
s->new_stmt();
if (d42() == 1)
if (check_d42("merge")) {
return make_shared<merge_stmt>((struct prod *)0, s);
if (d42() == 1)
}

if (check_d42("insert")) {
return make_shared<insert_stmt>((struct prod *)0, s);
else if (d42() == 1)
}
else if (check_d42("delete")) {
return make_shared<delete_returning>((struct prod *)0, s);
else if (d42() == 1) {
}
else if (check_d42("upsert")) {
return make_shared<upsert_stmt>((struct prod *)0, s);
} else if (d42() == 1)
}
else if (check_d42("update")) {
return make_shared<update_returning>((struct prod *)0, s);
else if (d6() > 4)
}
else if (check_d6(4, "select_for_update")) {
return make_shared<select_for_update>((struct prod *)0, s);
else if (d6() > 5)
}
else if (check_d6(5, "cte")) {
return make_shared<common_table_expression>((struct prod *)0, s);
}

return make_shared<query_spec>((struct prod *)0, s);
} catch (runtime_error &e) {
return statement_factory(s);
Expand Down
2 changes: 2 additions & 0 deletions relmodel.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string>
#include <vector>
#include <map>
#include <unordered_set>
#include <utility>
#include <memory>
#include <cassert>
Expand Down Expand Up @@ -80,6 +81,7 @@ struct scope {
struct scope *parent;
vector<named_relation*> tables; // available to table_ref productions
vector<named_relation*> refs; // available to column_ref productions
std::unordered_set<string> excluded_stmts;
struct schema *schema;
shared_ptr<map<string,unsigned int> > stmt_seq; // sequence for stmt-unique identifiers
scope(struct scope *parent = 0) : parent(parent) {
Expand Down
62 changes: 41 additions & 21 deletions sqlsmith.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <iostream>
#include <chrono>
#include <algorithm>

#ifndef HAVE_BOOST_REGEX
#include <regex>
Expand Down Expand Up @@ -62,8 +63,8 @@ int main(int argc, char *argv[])
cerr << PACKAGE_NAME " " GITREV << endl;

map<string,string> options;
regex optregex("--(help|log-to|verbose|target|sqlite|monetdb|version|dump-all-graphs|dump-all-queries|seed|dry-run|max-queries|rng-state|exclude-catalog)(?:=((?:.|\n)*))?");
regex optregex("--(help|log-to|verbose|target|sqlite|monetdb|version|dump-all-graphs|dump-all-queries|seed|dry-run|max-queries|rng-state|exclude-catalog|exclude-stmts)(?:=((?:.|\n)*))?");

for(char **opt = argv+1 ;opt < argv+argc; opt++) {
smatch match;
string s(*opt);
Expand All @@ -75,26 +76,30 @@ int main(int argc, char *argv[])
}
}

const std::string option_exclude_stmts = "exclude-stmts";

if (options.count("help")) {
cerr <<
" --target=connstr postgres database to send queries to" << endl <<
" --target=connstr postgres database to send queries to" << endl <<
#ifdef HAVE_LIBSQLITE3
" --sqlite=URI SQLite database to send queries to" << endl <<
" --sqlite=URI SQLite database to send queries to" << endl <<
#endif
#ifdef HAVE_MONETDB
" --monetdb=connstr MonetDB database to send queries to" <<endl <<
" --monetdb=connstr MonetDB database to send queries to" <<endl <<
#endif
" --log-to=connstr log errors to postgres database" << endl <<
" --seed=int seed RNG with specified int instead of PID" << endl <<
" --dump-all-queries print queries as they are generated" << endl <<
" --dump-all-graphs dump generated ASTs" << endl <<
" --dry-run print queries instead of executing them" << endl <<
" --exclude-catalog don't generate queries using catalog relations" << endl <<
" --max-queries=long terminate after generating this many queries" << endl <<
" --rng-state=string deserialize dumped rng state" << endl <<
" --verbose emit progress output" << endl <<
" --version print version information and exit" << endl <<
" --help print available command line options and exit" << endl;
" --log-to=connstr log errors to postgres database" << endl <<
" --seed=int seed RNG with specified int instead of PID" << endl <<
" --dump-all-queries print queries as they are generated" << endl <<
" --dump-all-graphs dump generated ASTs" << endl <<
" --dry-run print queries instead of executing them" << endl <<
" --exclude-catalog don't generate queries using catalog relations" << endl <<
" --" << option_exclude_stmts << "=string comma-separated list of statements that should not be generated" << endl <<
" choices: MERGE, INSERT, DELETE, UPSERT, UPDATE, SELECT_FOR_UPDATE, CTE" << endl <<
" --max-queries=long terminate after generating this many queries" << endl <<
" --rng-state=string deserialize dumped rng state" << endl <<
" --verbose emit progress output" << endl <<
" --version print version information and exit" << endl <<
" --help print available command line options and exit" << endl;
return 0;
} else if (options.count("version")) {
return 0;
Expand Down Expand Up @@ -126,6 +131,20 @@ int main(int argc, char *argv[])
long queries_generated = 0;
schema->fill_scope(scope);

if (options.count(option_exclude_stmts)) {
const auto& excluded_stmts = options[option_exclude_stmts];
boost::regex re("[a-zA-Z_]+");
boost::sregex_iterator it(excluded_stmts.begin(), excluded_stmts.end(), re);
boost::sregex_iterator itEnd;
for(; it != itEnd; ++it) {
std::string stmt = it->str();
std::cout << "Excluding: " << stmt << "\n";

std::transform(stmt.begin(), stmt.end(), stmt.begin(), ::tolower);
scope.excluded_stmts.emplace(stmt);
}
}

if (options.count("rng-state")) {
istringstream(options["rng-state"]) >> smith::rng;
} else {
Expand All @@ -147,7 +166,7 @@ int main(int argc, char *argv[])
loggers.push_back(l);
signal(SIGINT, cerr_log_handler);
}

if (options.count("dump-all-graphs"))
loggers.push_back(make_shared<ast_logger>());

Expand All @@ -166,11 +185,12 @@ int main(int argc, char *argv[])
if (options.count("max-queries")
&& (queries_generated >= stol(options["max-queries"])))
return 0;

}
}

shared_ptr<dut_base> dut;

if (options.count("sqlite")) {
#ifdef HAVE_LIBSQLITE3
dut = make_shared<dut_sqlite>(options["sqlite"]);
Expand All @@ -180,7 +200,7 @@ int main(int argc, char *argv[])
#endif
}
else if(options.count("monetdb")) {
#ifdef HAVE_MONETDB
#ifdef HAVE_MONETDB
dut = make_shared<dut_monetdb>(options["monetdb"]);
#else
cerr << "Sorry, " PACKAGE_NAME " was compiled without MonetDB support." << endl;
Expand All @@ -201,13 +221,13 @@ int main(int argc, char *argv[])
global_cerr_logger->report();
return 0;
}

/* Invoke top-level production to generate AST */
shared_ptr<prod> gen = statement_factory(&scope);

for (auto l : loggers)
l->generated(*gen);

/* Generate SQL from AST */
ostringstream s;
gen->out(s);
Expand Down