From 8d7d7b65ffd1a7651ad83e8a2a136f65fe009c30 Mon Sep 17 00:00:00 2001 From: Pedro Date: Wed, 12 Nov 2014 00:11:07 +0100 Subject: [PATCH] changed import tbl to create tbl syntax --- src/build_and_run_tests.sh | 10 ++++++---- src/lib/CreateStatement.h | 35 +++++++++++++++++++++++++++++++++++ src/lib/destructors.cpp | 5 +++++ src/lib/sqlhelper.cpp | 8 +++++++- src/lib/sqlhelper.h | 1 + src/lib/sqllib.h | 1 + src/parser/bison_parser.y | 17 ++++++++++++++++- src/parser/flex_lexer.l | 2 +- src/sql_analysis.cpp | 9 +++------ 9 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 src/lib/CreateStatement.h diff --git a/src/build_and_run_tests.sh b/src/build_and_run_tests.sh index d5ed9cc..c92d588 100644 --- a/src/build_and_run_tests.sh +++ b/src/build_and_run_tests.sh @@ -15,15 +15,16 @@ echo "\n\n" ./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c NOT LIKE 's%' LIMIT 10);" ./bin/grammar_test "SELECT t1.a, t1.b, t2.c FROM \"table\" AS t1 JOIN (SELECT * FROM foo JOIN bar ON foo.id = bar.id) t2 ON t1.a = t2.b WHERE (t1.b OR NOT t1.a) AND t2.c = 12.5" -./bin/grammar_test "IMPORT FROM TBL FILE 'students.tbl' INTO \"table\"" +./bin/grammar_test "CREATE TABLE \"table\" FROM TBL FILE 'students.tbl'" # Error: Where clause in between join statement -./bin/grammar_test -f "SELECT * from \"table\" WHERE (b OR NOT a) AND a = 12.5 AS t1 JOIN table2 ON a = b" -./bin/grammar_test -f "SELECT * \"table\" WHERE (b OR NOT a) AND a = 12.5 AS t1 JOIN table2 ON a = b" +# ./bin/grammar_test -f "SELECT * from \"table\" WHERE (b OR NOT a) AND a = 12.5 AS t1 JOIN table2 ON a = b" +# ./bin/grammar_test -f "SELECT * \"table\" WHERE (b OR NOT a) AND a = 12.5 AS t1 JOIN table2 ON a = b" echo "\n\n" -./bin/analysis "SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3" +# ./bin/analysis "SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3" +# ./bin/analysis "SELECT \"AVG(grade)\" FROM (SELECT AVG(grade) FROM students GROUP BY city) t1" # ./bin/analysis "SELECT col1, col2, 'test' FROM tbl t1, foo WHERE age > 12 AND zipcode = 12345 GROUP BY col1 ORDER BY col2 DESC LIMIT 100;" # ./bin/analysis "SELECT * from tbl AS t1 JOIN table2 AS t2 ON t1.a = t2.b WHERE (b OR NOT a) AND a = 12.5" # ./bin/analysis "SELECT t1.a, t1.b, t2.c FROM tbl AS t1 JOIN (SELECT * FROM foo JOIN bar ON foo.id = bar.id) t2 ON t1.a = t2.b WHERE (t1.b OR NOT t1.a) AND t2.c = 12.5" @@ -33,5 +34,6 @@ echo "\n\n" # ./bin/analysis " # IMPORT FROM TBL FILE 'students.tbl' INTO tbl; # SELECT * FROM tbl;" +./bin/analysis "CREATE TABLE \"table\" FROM TBL FILE 'students.tbl'" echo "\n\n" \ No newline at end of file diff --git a/src/lib/CreateStatement.h b/src/lib/CreateStatement.h new file mode 100644 index 0000000..d36bb81 --- /dev/null +++ b/src/lib/CreateStatement.h @@ -0,0 +1,35 @@ +#ifndef __CREATE_STATEMENT_H__ +#define __CREATE_STATEMENT_H__ + +#include "Statement.h" + +namespace hsql { + + +typedef enum { + kTable, + kTableFromTbl, // Hyrise file format + // HANA supports a second file type CONTROL FILE + // we don't need that so far, but we leave the option + // to expand it here +} CreateType; + + +/** + * @struct CreateStatement + */ +struct CreateStatement : Statement { + CreateStatement() : Statement(kStmtCreate) {}; + virtual ~CreateStatement(); // defined in destructors.cpp + + CreateType create_type; + const char* file_path; + const char* table_name; +}; + + + +} // namespace hsql + + +#endif \ No newline at end of file diff --git a/src/lib/destructors.cpp b/src/lib/destructors.cpp index d7dfc9f..add324c 100644 --- a/src/lib/destructors.cpp +++ b/src/lib/destructors.cpp @@ -40,4 +40,9 @@ ImportStatement::~ImportStatement() { delete table_name; } +CreateStatement::~CreateStatement() { + delete file_path; + delete table_name; +} + } // namespace hsql \ No newline at end of file diff --git a/src/lib/sqlhelper.cpp b/src/lib/sqlhelper.cpp index 3c3ee8f..7a6a30f 100644 --- a/src/lib/sqlhelper.cpp +++ b/src/lib/sqlhelper.cpp @@ -65,7 +65,7 @@ void printExpression(Expr* expr, uint num_indent) { case kExprLiteralFloat: inprint(expr->fval, num_indent); break; case kExprLiteralInt: inprint(expr->ival, num_indent); break; case kExprLiteralString: inprint(expr->name, num_indent); break; - case kExprFunctionRef: /* todo */ break; + case kExprFunctionRef: inprint(expr->name, num_indent); inprint(expr->expr->name, num_indent+1); break; case kExprOperator: printOperatorExpression(expr, num_indent); break; default: fprintf(stderr, "Unrecognized expression type %d\n", expr->type); break; } @@ -105,5 +105,11 @@ void printImportStatementInfo(ImportStatement* stmt, uint num_indent) { inprint(stmt->table_name, num_indent+1); } +void printCreateStatementInfo(CreateStatement* stmt, uint num_indent) { + inprint("CreateStatment", num_indent); + inprint(stmt->table_name, num_indent+1); + inprint(stmt->file_path, num_indent+1); +} + } // namespace hsql \ No newline at end of file diff --git a/src/lib/sqlhelper.h b/src/lib/sqlhelper.h index 90bf944..734e40d 100644 --- a/src/lib/sqlhelper.h +++ b/src/lib/sqlhelper.h @@ -9,6 +9,7 @@ namespace hsql { void printSelectStatementInfo(SelectStatement* stmt, uint num_indent); void printImportStatementInfo(ImportStatement* stmt, uint num_indent); +void printCreateStatementInfo(CreateStatement* stmt, uint num_indent); } // namespace hsql diff --git a/src/lib/sqllib.h b/src/lib/sqllib.h index cb663f1..7310016 100644 --- a/src/lib/sqllib.h +++ b/src/lib/sqllib.h @@ -4,5 +4,6 @@ #include "SelectStatement.h" #include "ImportStatement.h" +#include "CreateStatement.h" #endif \ No newline at end of file diff --git a/src/parser/bison_parser.y b/src/parser/bison_parser.y index 12e456a..4d5ee5d 100644 --- a/src/parser/bison_parser.y +++ b/src/parser/bison_parser.y @@ -80,6 +80,7 @@ typedef void* yyscan_t; hsql::Statement* statement; hsql::SelectStatement* select_stmt; hsql::ImportStatement* import_stmt; + hsql::CreateStatement* create_stmt; hsql::TableRef* table; hsql::Expr* expr; @@ -119,6 +120,7 @@ typedef void* yyscan_t; %type statement %type select_statement %type import_statement +%type create_statement %type table_name opt_alias alias file_path %type from_clause table_ref table_ref_atomic table_ref_name %type
join_clause join_table @@ -180,6 +182,7 @@ statement_list: statement: select_statement { $$ = $1; } | import_statement { $$ = $1; } + | create_statement { $$ = $1; } ; @@ -198,7 +201,6 @@ import_statement: import_file_type: CSV { $$ = kImportCSV; } - | TBL { $$ = kImportTbl; } ; file_path: @@ -206,6 +208,19 @@ file_path: ; +/****************************** + ** Create Statement + ******************************/ +create_statement: + CREATE TABLE table_name FROM TBL FILE file_path { + $$ = new CreateStatement(); + $$->create_type = kTableFromTbl; + $$->file_path = $7; + $$->table_name = $3; + } + ; + + /****************************** ** Select Statement ******************************/ diff --git a/src/parser/flex_lexer.l b/src/parser/flex_lexer.l index fc37e6d..6b9f4d3 100644 --- a/src/parser/flex_lexer.l +++ b/src/parser/flex_lexer.l @@ -128,7 +128,7 @@ IS TOKEN(IS) return SQL_INT; } -\"[A-Za-z][A-Za-z0-9_]*\" { +\"[^\"\n]+\" { // Crop the leading and trailing quote char yylval->sval = hsql::substr(yytext, 1, strlen(yytext)-1); return SQL_NAME; diff --git a/src/sql_analysis.cpp b/src/sql_analysis.cpp index 3f02f59..dd2f56c 100644 --- a/src/sql_analysis.cpp +++ b/src/sql_analysis.cpp @@ -27,12 +27,9 @@ int main(int argc, char *argv[]) { for (Statement* stmt : stmt_list->vector()) { printf("Statement %d:\n", i++); switch (stmt->type) { - case kStmtSelect: - printSelectStatementInfo((SelectStatement*) stmt, 1); - break; - case kStmtImport: - printImportStatementInfo((ImportStatement*) stmt, 1); - break; + case kStmtSelect: printSelectStatementInfo((SelectStatement*) stmt, 1); break; + case kStmtImport: printImportStatementInfo((ImportStatement*) stmt, 1); break; + case kStmtCreate: printCreateStatementInfo((CreateStatement*) stmt, 1); break; default: fprintf(stderr, "\tStatement Type %u. No detailed print method available.\n", stmt->type); break;