@@ -40,7 +40,6 @@ struct ParseValue {
4040 return ty;
4141 }
4242 Value getValue () const {
43- int type_value = static_cast <int >(ty);
4443 // llvm::errs() << "ty: " << type_value << "\n";
4544 // llvm::errs() << "value: " << value << "\n";
4645 // llvm::errs() << "attr: " << attr << "\n";
@@ -57,7 +56,8 @@ struct ParseValue {
5756enum class ParseMode {
5857 None,
5958 Column,
60- Table
59+ Table,
60+ Bool,
6161};
6262
6363
@@ -80,11 +80,6 @@ class SQLParser {
8080
8181 SQLParser (Location loc, OpBuilder &builder, std::string sql, int i) : loc(loc), builder(builder),
8282 sql (sql), i(i) {
83- // llvm::errs() << "last three " << sql.substr(sql.size()-6, sql.size()) << "\n";
84- // if (!sql.substr(sql.size()-2, sql.size()).compare("\\00")){
85- // llvm::errs() << "triggers trim" << "\n";
86- // sql = sql.substr(0, sql.size()-1);
87- // }
8883 }
8984
9085 std::string peek () {
@@ -179,60 +174,67 @@ class SQLParser {
179174 Value table = nullptr ;
180175 while (true ) {
181176 peekStr = peek ();
182- // if (hasColumns) {
183- if (peekStr == " FROM" ) {
184- pop ();
185- table = parseNext (ParseMode::Table).getValue ();
186- hasColumns = false ;
177+ if (hasColumns) {
178+ if (peekStr == " FROM" ) {
179+ pop ();
180+ table = parseNext (ParseMode::Table).getValue ();
181+ hasColumns = false ;
182+ break ;
183+ }
184+ ParseValue col = parseNext (ParseMode::Column);
185+ if (col.getType () == ParseType::Nothing) {
186+ hasColumns = false ;
187+ break ;
188+ } else {
189+ columns.push_back (col.getValue ());
190+ }
191+ if (peekStr == " ," ) pop ();
192+ } else if (peekStr == " WHERE" ) {
193+ pop ();
194+ hasWhere = true ;
195+ } else {
187196 break ;
197+ // assert(0 && " additional clauses like limit/etc not yet handled");
188198 }
189- ParseValue col = parseNext (ParseMode::Column);
190- if (col.getType () == ParseType::Nothing) {
191- hasColumns = false ;
192- break ;
193- } else {
194- columns.push_back (col.getValue ());
195- }
196- if (peekStr == " ," ) pop ();
197- // } else if (peekStr == "WHERE") {
198- // pop();
199- // hasWhere = true;
200- // } else if (hasWhere){
201- // // do something here
202- // break;
203- // } else {
204- // break;
205- // // assert(0 && " additional clauses like limit/etc not yet handled");
206- // }
207199 }
208- if (table)
209- return ParseValue (builder.create <sql::SelectOp>(loc, ExprType::get (builder.getContext ()), columns, table).getResult ());
210- else
211- return ParseValue (builder.create <sql::SelectOp>(loc, ExprType::get (builder.getContext ()), columns).getResult ());
200+ if (!table)
201+ table = builder.create <sql::TableOp>(loc, ExprType::get (builder.getContext ()), builder.getStringAttr (" " )).getResult ();
202+ return ParseValue (builder.create <sql::SelectOp>(loc, ExprType::get (builder.getContext ()), columns, table).getResult ());
212203 } else if (is_number (&peekStr)){
213204 pop ();
214205 return ParseValue (builder.create <IntOp>(loc, ExprType::get (builder.getContext ()), builder.getStringAttr (peekStr)).getResult ());
215206 } else if (mode == ParseMode::Column) {
216207 // do we need this??
217- // if (peekStr == "*") {
218- // pop();
219-
220- // return ParseValue(builder.create<ColumnOp>(loc, ExprType::get(builder.getContext()), );
221- // }
208+ if (peekStr == " *" ) {
209+ pop ();
210+ return ParseValue (builder.create <AllColumnsOp>(loc, ExprType::get (builder.getContext ()), builder.getStringAttr (peekStr)).getResult ());
211+ }
222212 pop ();
223213 return ParseValue (builder.create <ColumnOp>(loc, ExprType::get (builder.getContext ()), builder.getStringAttr (peekStr)).getResult ());
224214 } else if (mode == ParseMode::Table) {
225215 pop ();
226- return ParseValue (builder.create <TableOp>(loc,ExprType::get (builder.getContext ()) , builder.getStringAttr (peekStr)).getResult ());
227- }
216+ return ParseValue (builder.create <TableOp>(loc,ExprType::get (builder.getContext ()), builder.getStringAttr (peekStr)).getResult ());
217+ } else if (mode == ParseMode::Bool) {
218+ // col = peekStr;
219+ pop ();
220+
221+ } else if (peekStr == " (" ) {
222+ pop ();
223+ ParseValue res = parseNext (ParseMode::None);
224+ assert (peek () == " )" );
225+ pop ();
226+ return res;
227+ } else if (peekStr == " )" ) {
228+ return ParseValue ();
229+ }
228230 llvm::errs () << " Unknown token to parse: " << peekStr << " \n " ;
229231 llvm_unreachable (" Unknown token to parse" );
230232 }
231233
232234};
233235
234236std::vector<std::string> SQLParser::reservedWords = {
235- " (" , " )" , " >=" , " <=" , " !=" , " ," , " =" , " >" , " <" , " SELECT" , " INSERT INTO" , " VALUES" , " UPDATE" , " DELETE FROM" , " WHERE" , " FROM" , " SET" , " AS"
237+ " (" , " )" , " >=" , " <=" , " !=" , " ," , " =" , " >" , " <" , " SELECT" , " DISTINCT " , " INSERT INTO" , " VALUES" , " UPDATE" , " DELETE FROM" , " WHERE" , " FROM" , " SET" , " AS"
236238};
237239
238240
0 commit comments