void insertIntoTable(string tableName, vector<string> fieldNames, vector<string> fieldValues) { if(!schemaManager.relationExists(tableName)) { cout<<"Illegal Tablename"<<endl; return; } Relation *relation = schemaManager.getRelation(tableName); Tuple tuple = relation->createTuple(); Schema schema = relation->getSchema(); vector<string>::iterator it,it1; for(it = fieldNames.begin(),it1 = fieldValues.begin();it!=fieldNames.end();it++, it1++) { string str=*it,str1=*it1; str = removeSpaces(str); int type = schema.getFieldType(str); if(!type) { str1 = removeSpaces(str1); if(isNumber(str1)) { tuple.setField(str,stoi(str1)); } else { cout<<"Data type is not supported\n"; return; } } else { regex exp("\\ *\"(.*)\""); cmatch match; if(regex_match(str1.c_str(),match,exp)) { str1 = match[1]; if(str1.length()>20) { cout<<"Data type is not supported\n"; return; } else tuple.setField(str,str1); } else { cout<<"Data type is not supported\n"; return; } } } insertTuple(tableName, tuple); cout<<disk.getDiskIOs()<<endl; }
void join(Tuple tuple1, Tuple tuple2, string tableName1, string tableName2, string whereCondition, bool multi, vector<string> attributes) { Relation *relation = schemaManager.getRelation(tableName2+"_join"); Tuple tuple =relation->createTuple(); if(!multi) { for(int i=0;i<tuple1.getNumOfFields();i++) { if(tuple1.getSchema().getFieldType(i) == INT) tuple.setField(tableName1+"."+tuple1.getSchema().getFieldName(i), tuple1.getField(i).integer); else tuple.setField(tableName1+"."+tuple1.getSchema().getFieldName(i), *(tuple1.getField(i).str) ); } } else { for(int i=0;i<tuple1.getNumOfFields();i++) { if(tuple1.getSchema().getFieldType(i) == INT) tuple.setField(tuple1.getSchema().getFieldName(i), tuple1.getField(i).integer); else tuple.setField(tuple1.getSchema().getFieldName(i), *(tuple1.getField(i).str) ); } } for(int i=0;i<tuple2.getNumOfFields();i++) { if(tuple2.getSchema().getFieldType(i) == INT) tuple.setField(tableName2+"."+tuple2.getSchema().getFieldName(i), tuple2.getField(i).integer); else tuple.setField(tableName2+"."+tuple2.getSchema().getFieldName(i), *(tuple2.getField(i).str) ); } if((attributes.size()==1 && attributes[0]=="*") || multi) { if(whereConditionEvaluator(whereCondition, tuple)) insertTuple(tableName2+"_join", tuple); } else { Relation *relation1 = schemaManager.getRelation(tableName2+"_joinp"); Tuple tuplep = relation1->createTuple(); for(int i=0;i<attributes.size();i++) { if(tuplep.getSchema().getFieldType(attributes[i]) == INT) tuplep.setField(attributes[i], tuple.getField(attributes[i]).integer); else tuplep.setField(attributes[i], *(tuple.getField(attributes[i]).str)); } if(whereConditionEvaluator(whereCondition, tuple)) insertTuple(tableName2+"_joinp", tuplep); } }
vector<Tuple> Qtree::exec(bool print, string *table_name){ vector<Tuple> ret ; #ifdef DEBUG this->print(0); #endif if(this->type == INS){ vector<Tuple> temp = this->left->exec( false, NULL ) ; if(temp.size() != 0){ Schema sins_from = temp[0].getSchema() ; vector<enum FIELD_TYPE> field_types_from = sins_from.getFieldTypes() ; vector<string> field_names_from = sins_from.getFieldNames() ; if(field_types_from.size() == this->info.size() - 1){ Schema sins_to = p->schema_manager.getSchema( this->info[0] ) ; vector<enum FIELD_TYPE> field_types_to ; vector<union Field> fields ; vector<string>::iterator it0 = this->info.begin() ; vector<enum FIELD_TYPE>::iterator it1 = field_types_from.begin(); vector<string>::iterator it2 = field_names_from.begin(); vector<string> STRv; vector<int> INTv ; string table_n = (*it0) ; vector<string> field_names_to ; it0 ++ ; for( ; it0 != this->info.end() ; it0 ++, it1++){ unsigned long found = it0->rfind('.') ; string s_table ; if(found == std::string::npos){ s_table = string( table_n + "." + (*it0) ) ; }else{ s_table = string( it0->substr( it0->rfind('.') + 1 ) ) ; } if( sins_to.fieldNameExists( *it0 ) ){ field_names_to.push_back(string( *it0) ) ; if(sins_to.getFieldType( *it0) == *it1 ){ }else{ perror( ": Type mismatch"); return ret; } }else{ if(sins_to.fieldNameExists(s_table) ) { field_names_to.push_back(string( s_table ) ) ; if(sins_to.getFieldType( s_table) == *it1 ){ }else{ perror( ": Type mismatch"); return ret; } } else{ perror( "exec: No such field"); } } } for(vector<Tuple>::iterator it_tuple = temp.begin(); it_tuple != temp.end(); it_tuple ++) { for(it1 = field_types_from.begin(), it2 = field_names_from.begin() ; it1 != field_types_from.end() ; it1++, it2++){ if(*it1 == INT){ INTv.push_back( it_tuple->getField( *it2).integer ) ; }else{ STRv.push_back( *(it_tuple->getField( *it2).str) ) ; } } p->insert(table_n, field_names_to, STRv, INTv) ; INTv.clear(); STRv.clear() ; } }else{ perror("Size mismatch"); return ret; } }else{ return ret; } }else if(this->type == TAU){ string table_n; if(this->left->type == TABLE && (output_s.empty() || output_s.top() == NULL) ){ Schema s = p->schema_manager.getSchema( this->left->info[0] ) ; string s_table ; unsigned long found = this->info[0].rfind('.') ; table_n = this->left->info[0] ; if(found == std::string::npos){ s_table = string( table_n + "." + this->info[0] ) ; }else{ s_table = string( this->info[0].substr( this->info[0].rfind('.') + 1 ) ) ; } if( s.fieldNameExists( this->info[0] ) ){ ret = p->SortTwoPass(table_n, this->info[0]) ; }else if(s.fieldNameExists(s_table) ) { ret = p->SortTwoPass(table_n, s_table) ; }else{ perror("No such field"); return ret ; } }else{ vector<Tuple> temp = this->left->exec( false, &table_n ) ; if(table_name != NULL) { (*table_name ) = string( this->info[0] ) ;} if(temp.size() != 0){ Schema s = temp[0].getSchema() ; string temp_table_name = "temp_table" ; while(p->schema_manager.relationExists(temp_table_name) ){ temp_table_name += "-a" ; } p->CreateTable(temp_table_name, temp ) ; temp_relations.push_back( temp_table_name ) ; unsigned long found = this->info[0].rfind('.') ; string s_table ; if(found == std::string::npos){ s_table = string( table_n + "." + this->info[0] ) ; }else{ s_table = string( this->info[0].substr( this->info[0].rfind('.') + 1 ) ) ; } if( s.fieldNameExists( this->info[0] ) ){ ret = p->SortTwoPass(temp_table_name, this->info[0]) ; }else if(s.fieldNameExists(s_table) ) { ret = p->SortTwoPass(temp_table_name, s_table) ; }else{ perror("No such field"); return ret ; } }else{ return ret; } } }else if(this->type == DELTA ){ string table_n; if(this->left->type == TABLE){ table_n = this->left->info[0] ; ret = p->dupTwoPass(table_n) ; }else{ vector<Tuple> temp = this->left->exec( false , &table_n) ; if(table_name != NULL) { (*table_name ) = string( this->info[0] ) ;} if(temp.size() != 0){ Schema s = temp[0].getSchema() ; string temp_table_name = "temp_table" ; while(p->schema_manager.relationExists(temp_table_name) ){ temp_table_name += "-a" ; } p->CreateTable(temp_table_name, temp ); temp_relations.push_back(temp_table_name ) ; ret = p->dupTwoPass(temp_table_name) ; }else{ return ret; } } }else if(this->type == PI ){ string table_n; vector<Tuple> temp = this->left->exec( false, &table_n ) ; if(table_name != NULL) { (*table_name ) = string( this->info[0] ) ;} if(temp.size() != 0){ Schema s = temp[0].getSchema() ; vector<string> field_names ; vector<enum FIELD_TYPE> field_types ; for(vector<string>::iterator it= this->info.begin(); it != this->info.end(); it++){ unsigned long found = it->rfind('.') ; string s_table ; if(found == std::string::npos){ s_table = string( table_n + "." + (*it) ) ; }else{ s_table = string( it->substr( it->rfind('.') + 1 ) ) ; } if( s.fieldNameExists( *it ) ){ field_names.push_back(string(*it) ) ; field_types.push_back(s.getFieldType( *it) ) ; }else{ if(s.fieldNameExists(s_table) ) { field_names.push_back(string( s_table ) ) ; field_types.push_back( s.getFieldType( s_table ) ); } else{ perror( "exec: No such field"); } } } string temp_table_name = "temp_table" ; Relation *rlt = NULL; while(p->schema_manager.relationExists(temp_table_name) ){ temp_table_name += "-a" ; } rlt = p->CreateTable(temp_table_name, field_names, field_types) ; temp_relations.push_back(temp_table_name ) ; for(vector<Tuple>::iterator tit = temp.begin(); tit != temp.end(); tit++){ Tuple t = rlt->createTuple() ; for(vector<string>::iterator it = field_names.begin(); it != field_names.end() ; it++){ union Field f= tit->getField(*it) ; if( s.getFieldType(*it) == INT ){ t.setField( *it, f.integer ) ; }else{ t.setField( *it, *(f.str)) ; } } ret.push_back( t ) ; } }else{ return ret; } }else if(this->type == PRODUCT){ vector<string> ptables; vector<Relation *> relations ; map<string, Qexpression *> sigma_operation ; vector<string> commons ; map<string, bool> joined_keys; vector<string>::iterator it = ptables.begin(); ptables.insert(ptables.end(), this->info.begin(), this->info.end() ); if(output_s.empty() ){ }else if(output_s.top()->type == INTEGER || output_s.top()->type == LITERAL ){ Tuple *t = NULL; if(output_s.top()->judge(*t) ){ /* WHERE clasuse always true */ while(! output_s.empty() ){ output_s.top()->free() ;output_s.pop();} }else{ /* empty results */ return ret; } }else{ Qexpression *optimized = output_s.top()->optimize_sigma(&sigma_operation) ; output_s.pop(); if(optimized != NULL){ output_s.push(optimized) ;} #ifdef DEBUG for(map<string, Qexpression *>::iterator it = sigma_operation.begin(); it != sigma_operation.end(); it ++){ cout << it->first << "->" << endl; it->second->print(0); } #endif if( ! output_s.empty() ){ optimized = output_s.top()->optimize_join(commons, joined_keys) ; output_s.pop(); if(optimized != NULL){ output_s.push(optimized) ; }else{ while(! output_s.empty() ){output_s.top()->free() ; output_s.pop();} } if(! output_s.empty()){ #ifdef DEBUG output_s.top()->print(0); #endif } } #ifdef DEBUG cerr << "commons: "; for(vector<string>::iterator it = commons.begin(); it != commons.end(); it++){ cerr<< *it << " " ; } cerr << endl ; #endif } vector<string> to_drop ; for(vector<string>::iterator it = ptables.begin(); it != ptables.end(); ){ if(sigma_operation[*it] == NULL){ it++; }else{ Relation *temp_relation; vector<Tuple> tuples = p->singleTableSelect( *it , sigma_operation[*it] ) ; if(tuples.size() != 0){ temp_relation = p->CreateTable( ( *it) + "-SIGMA", tuples) ; }else{ vector<string> field_names = p->schema_manager.getRelation(*it)->getSchema().getFieldNames(); vector<enum FIELD_TYPE> field_types = p->schema_manager.getRelation(*it)->getSchema().getFieldTypes() ; temp_relation = p->CreateTable( (*it) + "-SIGMA" , field_names, field_types ) ; } to_drop.push_back( temp_relation->getRelationName() ) ; it = ptables.erase(it) ;ptables.insert( it, temp_relation->getRelationName() ) ; } } if(ptables.size() == 2){ if(ptables[0] <= ptables[1]){ ret = p->JoinTwoPass(ptables[0], ptables[1], commons ) ; }else{ ret = p->JoinTwoPass(ptables[1], ptables[0], commons ) ; } }else{ ret = p->JoinTables(ptables, commons) ; } for(vector<string>::iterator it = to_drop.begin(); it != to_drop.end(); it++){ p->DropTable(*it) ; } if(output_s.empty() ){ }else{ string temp_table_name = "temp_table"; while(p->schema_manager.relationExists(temp_table_name)) { temp_table_name += "-a"; } p->CreateTable( temp_table_name, ret ) ; temp_relations.push_back(temp_table_name) ; ret = p->singleTableSelect(temp_table_name, output_s.top() ) ; } }else if(this->type == TABLE){ if(table_name != NULL) { (*table_name ) = string( this->info[0] ) ;} ret = p->singleTableSelect(this->info[0], output_s.empty() ? NULL : output_s.top() ); }else{ return ret; } if(ret.size() != 0 && print){ vector<string> field_names = ret[0].getSchema( ).getFieldNames() ; cout << "-----------------" << endl ; for(vector<string>::iterator it = field_names.begin(); it != field_names.end(); it++){ cout<< *it << ' ' ; } cout << endl << "-----------------" << endl ; for(vector<Tuple>::iterator it = ret.begin(); it != ret.end(); it ++ ){ cout << (*it) << endl; }cout << "-----------------" << endl ; } return ret; }
string projection(vector<string> attributes, string tableName, string whereCondition) { Relation *relation = schemaManager.getRelation(tableName); Schema tableSchema = relation->getSchema(); vector<string> fieldNames; vector<enum FIELD_TYPE> fieldTypes; vector<string>::iterator it; int flag=-1; bool print=true; for(it=attributes.begin();it!=attributes.end();it++) { for(int i=0;i<tableSchema.getNumOfFields();i++) { string temp = *it; if(tableSchema.getFieldName(i)==temp || tableName+"."+tableSchema.getFieldName(i) == temp) flag=i; } if(flag!=-1) { fieldNames.push_back(tableSchema.getFieldName(flag)); fieldTypes.push_back(tableSchema.getFieldType(flag)); flag = -1; } } if(attributes.size()==1 && attributes[0] == "*") { if(whereCondition.empty()) return tableName; fieldNames = tableSchema.getFieldNames(); fieldTypes = tableSchema.getFieldTypes(); } Schema dupSchema(fieldNames,fieldTypes); Relation *relationDup = schemaManager.createRelation(tableName.append("_dup"), dupSchema); Tuple tuple = relationDup->createTuple(); vector<Tuple>::iterator it1; Block *block = mainMemory.getBlock(9); block->clear(); int index=0; for(int i=0;i<relation->getNumOfBlocks();i++) { relation->getBlock(i,0); vector<Tuple> t = mainMemory.getBlock(0)->getTuples(); for(it1=t.begin();it1!=t.end();it1++) { if(!it1->isNull()){ for(int j=0;j<fieldNames.size();j++) { if(fieldTypes[j]==INT) tuple.setField(fieldNames[j],it1->getField(fieldNames[j]).integer); else tuple.setField(fieldNames[j],*(it1->getField(fieldNames[j]).str)); } bool ttp = whereConditionEvaluator(whereCondition, *it1); if(ttp) { if(!block->isFull()) block->appendTuple(tuple); else { relationDup->setBlock(index,9); index++; block->clear(); block->appendTuple(tuple); } } } } } if(index!=relationDup->getNumOfBlocks()-1) relationDup->setBlock(index, 9); return tableName; }