Commit 0607891c authored by Gaurav Kukreja's avatar Gaurav Kukreja

Query Compiler Rewritten

 - Works for all types of queries.
 - Old code exists under old-query-compiler
Signed-off-by: 's avatarGaurav Kukreja <mailme.gaurav@gmail.com>
parent 3abea237
CC=g++
CCFLAGS=-O0 -fno-stack-protector -std=c++11 -fpermissive
CCFLAGS=-O3 -fno-stack-protector -std=c++11 -fpermissive
SOURCES= data_migrate.cpp schema_methods.cpp transactions.cpp oltp.cpp schema_constructors.cpp storage.cpp query.cpp
INCLUDE=include/data_migrate.h include/main.h include/transactions.h include/oltp.h include/schema.h include/query.h
......
#include <stdio.h>
#include <ctime>
#include <sys/time.h>
#include <iostream>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <utility>
#include <unordered_map>
#include <vector>
#include "include/schema.h"
#include "include/storage.h"
class mystring {
public:
char* str;
char str[50];
mystring operator=(char *_str);
mystring operator=(uint64_t val);
......@@ -18,13 +25,13 @@ public:
};
mystring mystring::operator=(char *_str) {
str = (char*)malloc(sizeof(char)*strlen(_str));
//str = (char*)malloc(sizeof(char)*strlen(_str));
strcpy(str, _str);
return *this;
}
mystring mystring::operator=(uint64_t val) {
str = (char*)malloc(sizeof(char)*20);
//str = (char*)malloc(sizeof(char)*20);
sprintf(str, "%llu", val);
return *this;
}
......@@ -44,27 +51,42 @@ int mystring::operator==(mystring _mystr) {
}
void query2() {
for(vector<warehouse>::iterator hJ_0_iter=warehouse_vect.begin(); hJ_0_iter<warehouse_vect.end(); hJ_0_iter++)
timeval start_time, end_time, time_taken;
gettimeofday(&start_time,NULL);
unordered_map<uint64_t, uint64_t> hash_map_0;
for(vector<district>::iterator district_iter=district_vect.begin(); district_iter!=district_vect.end(); district_iter++)
{
mystring w_name;
w_name = hJ_0_iter->w_name;
mystring w_id;
w_id = hJ_0_iter->w_id;
for(vector<district>::iterator hJ_1_iter=district_vect.begin(); hJ_1_iter<district_vect.end(); hJ_1_iter++)
//hash_map_0 should be declared just above
//generate hash table here
hash_map_0.insert(make_pair(district_iter->d_id, district_iter->tuple_id));
}
for(vector<customer>::iterator customer_iter=customer_vect.begin(); customer_iter!=customer_vect.end(); customer_iter++)
{
mystring d_name;
d_name = hJ_1_iter->d_name;
mystring d_w_id;
d_w_id = hJ_1_iter->d_w_id;
mystring d_tax;
d_tax = hJ_1_iter->d_tax;
if(w_id == d_w_id && w_id == 1)
//Match with Hash Table here
if(customer_iter->c_w_id==1)
{
printf("%s\t", w_name.str);
district* district_iter=&district_vect.at(hash_map_0.at(customer_iter->c_d_id));
mystring d_name;
d_name = district_iter->d_name;
mystring c_first;
c_first = customer_iter->c_first;
mystring c_last;
c_last = customer_iter->c_last;
mystring c_id;
c_id = customer_iter->c_id;
printf("%s\t", d_name.str);
printf("%s\t", d_tax.str);
printf("%s\t", c_first.str);
printf("%s\t", c_last.str);
printf("%s\t", c_id.str);
printf("\n");
}
}
}
gettimeofday(&end_time,NULL);
time_taken.tv_sec = end_time.tv_sec - start_time.tv_sec;
time_taken.tv_usec = end_time.tv_usec - start_time.tv_usec;
cout << "Time Taken " << time_taken.tv_sec << " s " << time_taken.tv_usec << " us\n";
}
#include "class_def.h"
void create_family() {
}
\ No newline at end of file
......@@ -61,7 +61,7 @@ int main(int argc, char* argv[]) {
void *qcode_handle;
void (*query_fn)(void);
int choice;
/*
while(1)
{
printf("\nChoice :\n1. Run Query\n2.Quit\n");
......@@ -95,7 +95,7 @@ int main(int argc, char* argv[]) {
}
}
/*
*/
//while(1) {
int choice;
timeval start_time, end_time, time_taken;
......@@ -140,7 +140,6 @@ int main(int argc, char* argv[]) {
unsigned long int tps;
tps=no_iterations*1000/(time_taken.tv_sec*1000+time_taken.tv_usec/1000);
cout << "Transactions per second are " << tps << endl;
*/
return 0;
......
CC=g++
CCFLAGS=-O0
SOURCES=main.cpp query_code.cpp exp_tree.cpp
HEADERS=include/query_code.h include/exp_tree.h
run_load: query_compiler
./query_compiler
cp gen_query_code.cpp ../fakedb
query_compiler: $(SOURCES) $(HEADERS)
$(CC) $(CCFLAGS) $^ -o $@
clean:
rm -rf query_compiler
\ No newline at end of file
/*
* trial program to see how virtual inheritance works.
*/
#include <stdio.h>
#define NAME_MAX 20
class person {
char *name;
public:
person *father;
person *mother;
char name[NAME_MAX];
//void print_gender();
person();
void print_name();
void print_parents();
};
class male {
public:
void print_gender();
//void print_name();
};
class female {
public:
void print_gender();
//void print_name();
};
void male::print_gender() {
printf("Male\n");
}
void female::print_gender() {
printf("Female\n");
}
void person::person() {
father = NULL;
mother = NULL;
}
void person::print_name() {
printf("%s\n", name);
}
void person::print_parents() {
if(father != NULL) {
printf("Father of \n");
print_name();
printf("is\n");
father->print_name();
printf("Mother of \n");
print_name();
printf("is\n");
mother->print_name();
}
}
\ No newline at end of file
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include "include/schema.h"
#include "include/storage.h"
class mystring {
public:
char* str;
mystring operator=(char *_str);
mystring operator=(uint64_t val);
int operator==(uint64_t val);
int operator==(const char* _str);
int operator==(mystring _mystr);
};
mystring mystring::operator=(char *_str) {
str = (char*)malloc(sizeof(char)*strlen(_str));
strcpy(str, _str);
return *this;
}
mystring mystring::operator=(uint64_t val) {
str = (char*)malloc(sizeof(char)*20);
sprintf(str, "%llu", val);
return *this;
}
int mystring::operator==(uint64_t val) {
char temp_str[20];
sprintf(temp_str, "%llu", val);
return !strcmp(str, temp_str);
}
int mystring::operator==(const char* _str) {
return !strcmp(str, _str);
}
int mystring::operator==(mystring _mystr) {
return !strcmp(str, _mystr.str);
}
void query2() {
for(vector<warehouse>::iterator hJ_0_iter=warehouse_vect.begin(); hJ_0_iter<warehouse_vect.end(); hJ_0_iter++)
{
mystring w_name;
w_name = hJ_0_iter->w_name;
mystring w_id;
w_id = hJ_0_iter->w_id;
for(vector<district>::iterator hJ_1_iter=district_vect.begin(); hJ_1_iter<district_vect.end(); hJ_1_iter++)
{
mystring d_name;
d_name = hJ_1_iter->d_name;
mystring d_w_id;
d_w_id = hJ_1_iter->d_w_id;
mystring d_tax;
d_tax = hJ_1_iter->d_tax;
if(w_id == d_w_id && w_id == 1)
{
printf("%s\t", w_name.str);
printf("%s\t", d_name.str);
printf("%s\t", d_tax.str);
printf("\n");
}
}
}
}
#include <stdio.h>
#include <iostream>
#include "include/exp_tree.h"
#include "include/query_code.h"
int main()
{
build_exp_tree();
printf("%s:%d\n", __func__,__LINE__);
generate_query_code();
return 0;
}
\ No newline at end of file
CC=g++
CCFLAGS=-O0
CCFLAGS=-fpermissive -std=c++11
SOURCES=main.cpp query_code.cpp exp_tree.cpp
HEADERS=include/query_code.h include/exp_tree.h
SOURCE=main.cpp parse_tree.cpp input_parsetree.cpp
INCLUDE=include/parse_tree.h
run_load: query_compiler
./query_compiler
mv gen_query_code.cpp ../fakedb
query_compiler: $(SOURCES) $(HEADERS)
compile_query: $(SOURCE) $(INCLUDE)
$(CC) $(CCFLAGS) $^ -o $@
run: compile_query
./compile_query
cp gen_query_code.cpp ../fakedb/
clean:
rm -rf query_compiler
\ No newline at end of file
rm -rf compile_query *.o
\ No newline at end of file
#include <stdio.h>
#include <ctime>
#include <sys/time.h>
#include <iostream>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <utility>
#include <unordered_map>
#include <vector>
#include "include/schema.h"
#include "include/storage.h"
class mystring {
public:
char* str = NULL;
~mystring() {
if(str!=NULL)
free(str);
}
mystring operator=(char *_str);
mystring operator=(uint64_t val);
int operator==(uint64_t val);
int operator==(const char* _str);
int operator==(mystring _mystr);
};
mystring mystring::operator=(char *_str) {
str = (char*)malloc(sizeof(char)*strlen(_str));
strcpy(str, _str);
return *this;
}
mystring mystring::operator=(uint64_t val) {
str = (char*)malloc(sizeof(char)*20);
sprintf(str, "%llu", val);
return *this;
}
int mystring::operator==(uint64_t val) {
char temp_str[20];
sprintf(temp_str, "%llu", val);
return !strcmp(str, temp_str);
}
int mystring::operator==(const char* _str) {
return !strcmp(str, _str);
}
int mystring::operator==(mystring _mystr) {
return !strcmp(str, _mystr.str);
}
void query2() {
timeval start_time, end_time, time_taken;
gettimeofday(&start_time,NULL);
unordered_map<uint64_t, uint64_t> hash_map_0;
for(vector<district>::iterator district_iter=district_vect.begin(); district_iter!=district_vect.end(); district_iter++)
{
//hash_map_0 should be declared just above
//generate hash table here
hash_map_0.insert(make_pair(district_iter->d_id, district_iter->tuple_id));
}
for(vector<customer>::iterator customer_iter=customer_vect.begin(); customer_iter!=customer_vect.end(); customer_iter++)
{
//Match with Hash Table here
if(customer_iter->c_w_id==1)
{
district* district_iter=&district_vect.at(hash_map_0.at(customer_iter->c_d_id));
mystring d_name;
d_name = district_iter->d_name;
mystring c_first;
c_first = customer_iter->c_first;
mystring c_last;
c_last = customer_iter->c_last;
mystring c_id;
c_id = customer_iter->c_id;
printf("%s\t", d_name.str);
printf("%s\t", c_first.str);
printf("%s\t", c_last.str);
printf("%s\t", c_id.str);
printf("\n");
}
}
gettimeofday(&end_time,NULL);
time_taken.tv_sec = end_time.tv_sec - start_time.tv_sec;
time_taken.tv_usec = end_time.tv_usec - start_time.tv_usec;
cout << "Time Taken " << time_taken.tv_sec << " s " << time_taken.tv_usec << " us\n";
}
#ifndef _INPUT_PARSETREE_H_
#define _INPUT_PARSETREE_H_
void generate_parse_tree();
#endif //_INPUT_PARSETREE_H_
\ No newline at end of file
/*
* author: Gaurav Kukreja
*
*/
#include <string>
#include <list>
#include <vector>
using namespace std;
typedef string field_t;
//typedef string predicate_t;
class predicate_t {
public:
string pred_l_operand;
string pred_operator;
string pred_r_operand;
};
class table_field_vect_t{
public:
string table_name;
vector<field_t> field_vect;
};
class Operator {
public:
Operator *consumer;
Operator *l_input;
Operator *r_input;
//Operator Specific Fields
//We will not be able to reference to fields declared inside
// the derived classes. Try to find an alternative to do this.
vector<table_field_vect_t> table_vect;
//vector<field_t> field_list; //for PRINT, TABLE_SCAN and SELECT
vector<predicate_t> primary_pred_vect;
predicate_t hashed_pred; //for HASH_JOIN
string table_name; //for TABLE_SCAN
//Virtual Functions which will be implemented by each derived class.
virtual void produce() = 0;
virtual void consume() = 0;
};
class print_Operator : virtual public Operator {
public:
//Operator *input;
void produce();
void consume();
};
class tableScan_Operator : virtual public Operator {
public:
//Operator *input;
void produce();
void consume() {} //This needs to be defined for new to work it seems.
};
class select_Operator : virtual public Operator {
public:
//Operator *input;
void produce();
void consume();
};
class join_Operator : virtual public Operator {
public:
//Operator *l_input;
//Operator *r_input;
bool l_input_consumed; //for HASH_JOIN
string hash_key_field; //for HASH_JOIN
string hash_key_type;
int joinOp_num; //used for indexing the hash_maps created for each has operation.
void produce();
void consume();
};
/*
class table_Operator : virtual Operator {
public:
char tableName[MAX_TABNAME];
void produce();
void consume();
};
*/
/*
* Hardcoded Parse Tree
*
*/
#include <iostream>
#include <list>
#include <string>
#include "include/parse_tree.h"
using namespace std;
Operator *expr_root;
void generate_parse_tree() {
//PRINT Operation
print_Operator *print1;
print1 = new print_Operator;
expr_root = print1;
table_field_vect_t *table=new table_field_vect_t;
//tables and fields
table->table_name = "district";
table->field_vect.push_back((field_t)"d_name");
print1->table_vect.push_back(*table);
delete table;
table = new table_field_vect_t;
table->table_name = "customer";
table->field_vect.push_back((field_t)"c_first");
table->field_vect.push_back((field_t)"c_last");
table->field_vect.push_back((field_t)"c_id");
print1->table_vect.push_back(*table);
delete table;
print1->l_input = new select_Operator;
//SELECT Operator
Operator *select1;
select1 = print1->l_input;
select1->consumer = print1;
//tables and fields
table = new table_field_vect_t;
table->table_name = "district";
table->field_vect.push_back((field_t)"d_name");
select1->table_vect.push_back(*table);
delete table;
table = new table_field_vect_t;
table->table_name = "customer";
table->field_vect.push_back((field_t)"c_first");
table->field_vect.push_back((field_t)"c_last");
table->field_vect.push_back((field_t)"c_id");
select1->table_vect.push_back(*table);
delete table;
select1->l_input = new join_Operator;
//JOIN Operator
Operator *join1;
join1 = select1->l_input;
join1->consumer = select1;
join1->l_input = new tableScan_Operator;
join1->r_input = new tableScan_Operator;
predicate_t pred;
pred.pred_l_operand = "c_w_id";
pred.pred_operator = "==";
pred.pred_r_operand = "1";
join1->primary_pred_vect.push_back(pred);
join1->hashed_pred.pred_l_operand = "c_d_id";
join1->hashed_pred.pred_operator = "==";
join1->hashed_pred.pred_r_operand = "d_id";
join_Operator* _join1 = dynamic_cast<join_Operator*>(join1);
_join1->hash_key_type="uint64_t";
_join1->hash_key_field="d_id";
//left TABLESCAN Operator
Operator *tableScan1;
tableScan1 = join1->l_input;
tableScan1->consumer = join1;
tableScan1->table_name = "district";
//OVERKILL : table_name is enough for tableScan_Operator
/*
table = new table_field_vect_t;
table->table_name = "warehouse";
table->field_vect.push_back((field_t)"w_id");
table->field_vect.push_back((field_t)"w_name");
tableScan1->table_vect.push_back(*table);
delete table;
*/
//tableScan1->l_input = new table_Operator;
//right TABLESCAN Operator
Operator *tableScan2;
tableScan2 = join1->r_input;
tableScan2->consumer = join1;
tableScan2->table_name = "customer";
//OVERKILL : table_name is enough for tableScan_Operator
/*
table = new table_field_vect_t;
table->table_name = "district";
table->field_vect.push_back((field_t)"d_w_id");
table->field_vect.push_back((field_t)"d_name");
table->field_vect.push_back((field_t)"d_tax");
tableScan2->table_vect.push_back(*table);
delete table;
*/
//tableScan2->l_input = new table_Operator;
}
/*
* Hardcoded Parse Tree
*
*/
#include <iostream>
#include <list>
#include <string>
#include "include/parse_tree.h"
using namespace std;
Operator *expr_root;
void generate_parse_tree() {
//PRINT Operation
print_Operator *print1;
print1 = new print_Operator;
expr_root = print1;
table_field_vect_t *table=new table_field_vect_t;
//tables and fields
table->table_name = "warehouse";
table->field_vect.push_back((field_t)"w_name");
print1->table_vect.push_back(*table);
delete table;
table = new table_field_vect_t;
table->table_name = "district";
table->field_vect.push_back((field_t)"d_name");
table->field_vect.push_back((field_t)"d_tax");
print1->table_vect.push_back(*table);
delete table;
print1->l_input = new select_Operator;
//SELECT Operator
Operator *select1;
select1 = print1->l_input;
select1->consumer = print1;
//tables and fields
table = new table_field_vect_t;
table->table_name = "warehouse";
table->field_vect.push_back((field_t)"w_name");
select1->table_vect.push_back(*table);
delete table;
table = new table_field_vect_t;
table->table_name = "district";
table->field_vect.push_back((field_t)"d_name");
table->field_vect.push_back((field_t)"d_tax");
select1->table_vect.push_back(*table);
delete table;
select1->l_input = new join_Operator;
//JOIN Operator
Operator *join1;
join1 = select1->l_input;
join1->consumer = select1;
join1->l_input = new tableScan_Operator;
join1->r_input = new tableScan_Operator;
predicate_t pred;
pred.pred_l_operand = "d_w_id";
pred.pred_operator = "==";
pred.pred_r_operand = "1";
join1->primary_pred_vect.push_back(pred);
join1->hashed_pred.pred_l_operand = "d_w_id";
join1->hashed_pred.pred_operator = "==";
join1->hashed_pred.pred_r_operand = "w_id";
join_Operator* _join1 = dynamic_cast<join_Operator*>(join1);
_join1->hash_key_type="uint64_t";
_join1->hash_key_field="w_id";
//left TABLESCAN Operator
Operator *tableScan1;
tableScan1 = join1->l_input;
tableScan1->consumer = join1;
tableScan1->table_name = "warehouse";
//OVERKILL : table_name is enough for tableScan_Operator
/*
table = new table_field_vect_t;
table->table_name = "warehouse";
table->field_vect.push_back((field_t)"w_id");
table->field_vect.push_back((field_t)"w_name");
tableScan1->table_vect.push_back(*table);
delete table;
*/
//tableScan1->l_input = new table_Operator;
//right TABLESCAN Operator
Operator *tableScan2;
tableScan2 = join1->r_input;
tableScan2->consumer = join1;
tableScan2->table_name = "district";
//OVERKILL : table_name is enough for tableScan_Operator
/*
table = new table_field_vect_t;
table->table_name = "district";
table->field_vect.push_back((field_t)"d_w_id");
table->field_vect.push_back((field_t)"d_name");
table->field_vect.push_back((field_t)"d_tax");
tableScan2->table_vect.push_back(*table);
delete table;
*/
//tableScan2->l_input = new table_Operator;
}
#include <stdio.h>
#include <iostream>
#include <string>
#include <list>
using namespace std;
#include "include/parse_tree.h"
#include "include/input_parsetree.h"
extern Operator *expr_root;
extern int open_braces;
extern void indent();
//GLOBAL FILE* used for output file.
FILE *qcode_fp;
void generate_query_code() {
qcode_fp = fopen("gen_query_code.cpp", "w");
fprintf(qcode_fp, "#include <stdio.h>\n");
fprintf(qcode_fp, "#include <ctime>\n");
fprintf(qcode_fp, "#include <sys/time.h>\n");
fprintf(qcode_fp, "#include <iostream>\n");
fprintf(qcode_fp, "#include <string.h>\n");
fprintf(qcode_fp, "#include <stdint.h>\n");
fprintf(qcode_fp, "#include <stdlib.h>\n\n");
fprintf(qcode_fp, "#include <utility>\n");
fprintf(qcode_fp, "#include <unordered_map>\n");
fprintf(qcode_fp, "#include <vector>\n\n");
fprintf(qcode_fp, "#include \"include/schema.h\"\n");
fprintf(qcode_fp, "#include \"include/storage.h\"\n\n");
fprintf(qcode_fp, "");
fprintf(qcode_fp, "");
fprintf(qcode_fp, "class mystring {\n");
fprintf(qcode_fp, "public:\n");
fprintf(qcode_fp, "\tchar str[50];\n\n");
fprintf(qcode_fp, "\tmystring operator=(char *_str);\n");
fprintf(qcode_fp, "\tmystring operator=(uint64_t val);\n");
fprintf(qcode_fp, "\tint operator==(uint64_t val);\n");
fprintf(qcode_fp, "\tint operator==(const char* _str);\n");
fprintf(qcode_fp, "\tint operator==(mystring _mystr);\n");
fprintf(qcode_fp, "};\n\n");
fprintf(qcode_fp, "mystring mystring::operator=(char *_str) {\n");
//fprintf(qcode_fp, "\tstr = (char*)malloc(sizeof(char)*strlen(_str));\n");
fprintf(qcode_fp, "\tstrcpy(str, _str);\n");
fprintf(qcode_fp, "\treturn *this;\n");
fprintf(qcode_fp, "}\n\n");
fprintf(qcode_fp, "mystring mystring::operator=(uint64_t val) {\n");
//fprintf(qcode_fp, "\tstr = (char*)malloc(sizeof(char)*20);\n");
fprintf(qcode_fp, "\tsprintf(str, \"%%llu\", val);\n");
fprintf(qcode_fp, "\treturn *this;\n");
fprintf(qcode_fp, "}\n\n");
fprintf(qcode_fp, "int mystring::operator==(uint64_t val) {\n");
fprintf(qcode_fp, "\tchar temp_str[20];\n");
fprintf(qcode_fp, "\tsprintf(temp_str, \"%%llu\", val);\n");
fprintf(qcode_fp, "\treturn !strcmp(str, temp_str);\n");
fprintf(qcode_fp, "}\n\n");
fprintf(qcode_fp, "int mystring::operator==(const char* _str) {\n");
fprintf(qcode_fp, "\treturn !strcmp(str, _str);\n");
fprintf(qcode_fp, "}\n\n");
fprintf(qcode_fp, "int mystring::operator==(mystring _mystr) {\n");
fprintf(qcode_fp, "\treturn !strcmp(str, _mystr.str);\n");
fprintf(qcode_fp, "}\n\n");
fprintf(qcode_fp, "");
fprintf(qcode_fp, "");
indent();
fprintf(qcode_fp, "void query2() {\n");
open_braces++;
indent();
fprintf(qcode_fp, "timeval start_time, end_time, time_taken;\n\n");
indent();
fprintf(qcode_fp, "gettimeofday(&start_time,NULL);\n\n");
expr_root->produce();
indent();
fprintf(qcode_fp, "gettimeofday(&end_time,NULL);\n\n");
indent();
fprintf(qcode_fp, "time_taken.tv_sec = end_time.tv_sec - start_time.tv_sec;\n");
indent();
fprintf(qcode_fp, "time_taken.tv_usec = end_time.tv_usec - start_time.tv_usec;\n\n");
indent();
fprintf(qcode_fp, "cout << \"Time Taken \" << time_taken.tv_sec << \" s \" << time_taken.tv_usec << \" us\\n\";\n");
open_braces--;
indent();
fprintf(qcode_fp, "}\n");
fclose(qcode_fp);
}
#include "include/exp_tree.h"
#include "include/query_code.h"
int main()
{
int main() {
build_exp_tree();
printf("%s:%d\n", __func__,__LINE__);
generate_parse_tree();
generate_query_code();
......
/*
* Implementation of functions declared in parse_tree.h
*/
#include <stdio.h>
#include "include/parse_tree.h"
using namespace std;
static int num_hashOp; //Num of Hash Operators used. This is
extern FILE *qcode_fp;
int open_braces = 0;
void indent() {
for(int i=0; i<open_braces; i++)
fprintf(qcode_fp, "\t");
}
//Just calls produce to the input
void print_Operator::produce() {
l_input->produce();
}
//prints the table
void print_Operator::consume() {
vector<field_t>::iterator field_iter;
for(vector<table_field_vect_t>::iterator table_iter=table_vect.begin(); table_iter != table_vect.end(); table_iter++)
{
for(vector<field_t>::iterator field_iter=table_iter->field_vect.begin(); field_iter != table_iter->field_vect.end(); field_iter++)
{
indent();
fprintf(qcode_fp, "printf(\"%%s\\t\", %s.str);\n", (char *)(field_iter->c_str()));
}
}
indent();
fprintf(qcode_fp, "printf(\"\\n\");\n");
}
//calls produce on the input for now. Can do something important if column store is used.
void select_Operator::produce() {
l_input->produce();
}
//calls consume for the consumer.
void select_Operator::consume() {
for(vector<table_field_vect_t>::iterator table_iter=table_vect.begin(); table_iter != table_vect.end(); table_iter++)
{
for(vector<field_t>::iterator field_iter=table_iter->field_vect.begin(); field_iter != table_iter->field_vect.end(); field_iter++)
{
indent();
fprintf(qcode_fp, "mystring %s;\n", field_iter->c_str());
indent();
fprintf(qcode_fp, "%s = %s_iter->%s;\n", field_iter->c_str(), table_iter->table_name.c_str(), field_iter->c_str());
}
}
consumer->consume();
}
//this implementation is using Hash Join
//This function calls produce for the left input. The left input only produces the for statement.
//The input for joins only produce the for loops, the join operator is responsible for hashing.
//when l_input->produce() returns, first table hashing code is generated already by join_Operator->consume().
void join_Operator::produce() {
join_Operator* _join1 = dynamic_cast<join_Operator*>(this);
_join1->joinOp_num = num_hashOp++;
indent();
fprintf(qcode_fp,"unordered_map<%s, uint64_t> hash_map_%d;\n",_join1->hash_key_type.c_str(), _join1->joinOp_num);
l_input->produce();
r_input->produce();
}
//For the left input, this function writes the code, that will generate a hash tree
// by iterating on the whole table.
// Returns to produce of the consumer, which then closes the paranthesis for "for" block.
//
void join_Operator::consume() {
join_Operator* _join1 = dynamic_cast<join_Operator*>(this);
if(!_join1->l_input_consumed) {
//perform hashing on hash_key
indent();
fprintf(qcode_fp, "//hash_map_%d should be declared just above\n", _join1->joinOp_num);
indent();
fprintf(qcode_fp, "//generate hash table here\n");
indent();
fprintf(qcode_fp, "hash_map_%d.insert(make_pair(%s_iter->%s, %s_iter->tuple_id));\n", _join1->joinOp_num, l_input->table_name.c_str(), _join1->hash_key_field.c_str(), l_input->table_name.c_str());
_join1->l_input_consumed = true;
}
else {
//check for predicates using Hash Table
indent();
fprintf(qcode_fp, "//Match with Hash Table here\n");
if(!primary_pred_vect.empty())
{
vector<predicate_t>::iterator pred_iter;
for(pred_iter=primary_pred_vect.begin();pred_iter < primary_pred_vect.end();pred_iter++)
{
if(pred_iter == primary_pred_vect.begin())
{
indent();
fprintf(qcode_fp, "if(");
}
else {
fprintf(qcode_fp, " && ");
}
fprintf(qcode_fp, "%s_iter->%s", r_input->table_name.c_str(), pred_iter->pred_l_operand.c_str());
fprintf(qcode_fp, "%s", pred_iter->pred_operator.c_str()) ;
fprintf(qcode_fp, "%s", pred_iter->pred_r_operand.c_str());
}
fprintf(qcode_fp, ")\n");
indent();
fprintf(qcode_fp, "{\n");
open_braces++;
indent();
fprintf(qcode_fp, "%s* %s_iter=&%s_vect.at(hash_map_%d.at(%s_iter->%s));\n", l_input->table_name.c_str(), l_input->table_name.c_str(), l_input->table_name.c_str(), _join1->joinOp_num, r_input->table_name.c_str(), hashed_pred.pred_l_operand.c_str());
/*
indent();
fprintf(qcode_fp, "if(%s_iter == %s_vect.end()) {\n", l_input->table_name.c_str(), l_input->table_name.c_str());
open_braces++;
indent();
fprintf(qcode_fp, "continue;\n");
open_braces--;
indent();
fprintf(qcode_fp, "}\n");
*/
consumer->consume();
open_braces--;
indent();
fprintf(qcode_fp, "}\n");
}
else {
indent();
fprintf(qcode_fp, "vector<%s>::iterator %s_iter=%s_vect.at(hash_map_%d.at(%s));\n", l_input->table_name.c_str(), l_input->table_name.c_str(), l_input->table_name.c_str(), _join1->joinOp_num, hashed_pred.pred_l_operand.c_str());
/*
indent();
fprintf(qcode_fp, "if(%s_iter == %s_vect.end()) {\n", l_input->table_name.c_str(), l_input->table_name.c_str());
open_braces++;
indent();
fprintf(qcode_fp, "continue;\n");
open_braces--;
indent();
fprintf(qcode_fp, "}\n");
*/
consumer->consume();
}
}
}
//
void tableScan_Operator::produce() {
indent();
//For loop to trace the entire table to be produced here.
fprintf(qcode_fp, "for(vector<%s>::iterator %s_iter=%s_vect.begin(); %s_iter!=%s_vect.end(); %s_iter++)\n",table_name.c_str(),table_name.c_str(),table_name.c_str(),table_name.c_str(),table_name.c_str(),table_name.c_str());
indent();
fprintf(qcode_fp, "{\n");
open_braces++;
consumer->consume();
open_braces--;
indent();
fprintf(qcode_fp, "}\n");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment