Skip to content

[WIP] improve mysql and support abtest #4

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

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
64 changes: 33 additions & 31 deletions grammar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ shared_ptr<table_ref> table_ref::factory(prod *p) {
if (d6() > 3)
return make_shared<joined_table>(p);
}
if (d6() > 3)
return make_shared<table_or_query_name>(p);
else
return make_shared<table_sample>(p);
// disable table sample since both TiDB and CRDB don't support it
return make_shared<table_or_query_name>(p);
// if (d6() > 3)
// return make_shared<table_or_query_name>(p);
// else
// return make_shared<table_sample>(p);
} catch (runtime_error &e) {
p->retry();
}
Expand Down Expand Up @@ -319,14 +321,15 @@ query_spec::query_spec(prod *p, struct scope *s, bool lateral) :

if (lateral)
scope->refs = s->refs;

from_clause = make_shared<struct from_clause>(this);
select_list = make_shared<struct select_list>(this);

set_quantifier = (d100() == 1) ? "distinct" : "";

search = bool_expr::factory(this);

// for comparing MySQL and TiDB, maybe disable limit clause here
if (d6() > 2) {
ostringstream cons;
cons << "limit " << d100() + d100();
Expand Down Expand Up @@ -467,20 +470,20 @@ shared_ptr<prod> statement_factory(struct scope *s)
{
try {
s->new_stmt();
if (d42() == 1)
return make_shared<merge_stmt>((struct prod *)0, s);
if (d42() == 1)
return make_shared<insert_stmt>((struct prod *)0, s);
else if (d42() == 1)
return make_shared<delete_returning>((struct prod *)0, s);
else if (d42() == 1) {
return make_shared<upsert_stmt>((struct prod *)0, s);
} else if (d42() == 1)
return make_shared<update_returning>((struct prod *)0, s);
else if (d6() > 4)
return make_shared<select_for_update>((struct prod *)0, s);
else if (d6() > 5)
return make_shared<common_table_expression>((struct prod *)0, s);
// if (d42() == 1)
// return make_shared<merge_stmt>((struct prod *)0, s);
// if (d42() == 1)
// return make_shared<insert_stmt>((struct prod *)0, s);
// else if (d42() == 1)
// return make_shared<delete_returning>((struct prod *)0, s);
// else if (d42() == 1)
// return make_shared<upsert_stmt>((struct prod *)0, s);
// else if (d42() == 1)
// return make_shared<update_returning>((struct prod *)0, s);
// else if (d6() > 4)
// return make_shared<select_for_update>((struct prod *)0, s);
// else if (d6() > 5)
// 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 Expand Up @@ -510,18 +513,17 @@ common_table_expression::common_table_expression(prod *parent, struct scope *s)

} while (d6() > 2);

retry:
do {
auto pick = random_pick(s->tables);
scope->tables.push_back(pick);
} while (d6() > 3);
try {
query = make_shared<query_spec>(this, scope);
} catch (runtime_error &e) {
retry();
goto retry;
}

retry:
do {
auto pick = random_pick(s->tables);
scope->tables.push_back(pick);
} while (d6() > 3);
try {
query = make_shared<query_spec>(this, scope);
} catch (runtime_error &e) {
retry();
goto retry;
}
}

void common_table_expression::out(std::ostream &out)
Expand Down
27 changes: 25 additions & 2 deletions mysql.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "mysql.hh"
#include <cstring>
#include <regex>
#include <iostream>
#include <fstream>

using namespace std;

Expand Down Expand Up @@ -33,6 +36,9 @@ void mysql_connection::parse_connection_string(std::string &conninfo) {
}

mysql_connection::mysql_connection(std::string &conninfo) {
if (conninfo == "") {
return;
}
mysql_init(&mysql);
parse_connection_string(conninfo);

Expand Down Expand Up @@ -87,7 +93,8 @@ std::string parse_column_type(const char* column_type) {
!strcmp(column_type,"BLOB") ||
!strcmp(column_type,"TINYBLOB") ||
!strcmp(column_type,"MEDIUMBLOB") ||
!strcmp(column_type,"LONGBLOB")) {
!strcmp(column_type,"LONGBLOB") ||
!strcmp(column_type,"VARBINARY")) {
return std::string("BINARY");
}

Expand All @@ -107,6 +114,9 @@ std::string parse_column_type(const char* column_type) {
schema_mysql::schema_mysql(std::string &conninfo, bool no_catalog)
: mysql_connection(conninfo)
{
if (conninfo == "") {
return;
}
(void)no_catalog;
MYSQL_RES *result;
MYSQL_ROW row;
Expand All @@ -119,6 +129,7 @@ schema_mysql::schema_mysql(std::string &conninfo, bool no_catalog)
throw std::runtime_error(mysql_error(&mysql));
}
result = mysql_store_result(&mysql);

while ((row = mysql_fetch_row(result))) {
string table_name;
string schema_name;
Expand All @@ -135,6 +146,8 @@ schema_mysql::schema_mysql(std::string &conninfo, bool no_catalog)
continue;
}

cout << row[0] << row[1] << insertable << base_table << endl;

table tab(row[0], row[1], insertable, base_table);
tables.push_back(tab);
}
Expand Down Expand Up @@ -267,8 +280,13 @@ dut_mysql::dut_mysql(std::string& conninfo)
{}

void dut_mysql::test(const std::string &stmt) {
// remove tablesample syntax which mysql don't support
std::string r_stmt = std::regex_replace(stmt, std::regex("tablesample.+?\n"), " \n");
r_stmt = std::regex_replace(r_stmt, std::regex("\n"), " ");
r_stmt = std::regex_replace(r_stmt, std::regex("\\s+"), " ");
// cout << r_stmt << endl;
MYSQL_RES *result;
int rc = mysql_real_query(&mysql, stmt.c_str(), stmt.length());
int rc = mysql_real_query(&mysql, r_stmt.c_str(), r_stmt.length());
if(rc){
unsigned int err_num = mysql_errno(&mysql);
const char* err = mysql_error(&mysql);
Expand All @@ -286,6 +304,11 @@ void dut_mysql::test(const std::string &stmt) {
else {
throw std::runtime_error(err);
}
} else {
ofstream sqlfile;
sqlfile.open("./sql/success-sql.txt", std::ios_base::app);
sqlfile << r_stmt << endl;
sqlfile.close();
}
result = mysql_store_result(&mysql);
mysql_free_result(result);
Expand Down
Loading