From 71e80cc17ec37c7b1a1656d5ac70fdba2396160b Mon Sep 17 00:00:00 2001 From: Pedro Date: Mon, 3 Nov 2014 23:57:42 +0100 Subject: [PATCH] implemented join grammar --- src/Makefile | 2 +- src/build_and_run_tests.sh | 6 ++++-- src/parser/Makefile | 2 +- src/parser/SQLParser.cpp | 4 ++-- src/parser/bison_parser.y | 20 ++++++++++++++++---- src/sql_grammar_test.cpp | 9 ++++++--- 6 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/Makefile b/src/Makefile index 2f9b925..a096629 100644 --- a/src/Makefile +++ b/src/Makefile @@ -39,5 +39,5 @@ parser/bison_parser.cpp: clean: - rm -f *.o *~ $(EXECUTION_BIN) $(TESTS_BIN) + rm -f *.o *~ $(EXECUTION_BIN) $(TESTS_BIN) bin/grammar_test make clean -C parser/ diff --git a/src/build_and_run_tests.sh b/src/build_and_run_tests.sh index 244ea1c..8101981 100644 --- a/src/build_and_run_tests.sh +++ b/src/build_and_run_tests.sh @@ -14,8 +14,10 @@ make grammar_test echo "\n\n" ./bin/grammar_test "SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3 LIMIT 10" -./bin/grammar_test "(SELECT col1, col2, 'test' FROM table, foo WHERE age > 12 AND zipcode = 12345 GROUP BY col1);" +./bin/grammar_test "SELECT col1, col2, 'test' FROM table, foo WHERE age > 12 AND zipcode = 12345 GROUP BY col1;" ./bin/grammar_test "SELECT age FROM table, (SELECT * FROM table2) ORDER BY age DESC" -./bin/grammar_test "(SELECT * from table WHERE (b OR NOT a) AND a = 12.5) JOIN table2 ON a = b" +./bin/grammar_test "SELECT * from table WHERE (b OR NOT a) AND a = 12.5 JOIN table2 ON a = b" + +./bin/grammar_test -f "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3 LIMIT 10)" echo "\n\n" \ No newline at end of file diff --git a/src/parser/Makefile b/src/parser/Makefile index a015c4e..ce92703 100644 --- a/src/parser/Makefile +++ b/src/parser/Makefile @@ -2,7 +2,7 @@ all: bison_parser.cpp flex_lexer.cpp bison_parser.cpp: bison_parser.y - bison bison_parser.y + bison bison_parser.y -v flex_lexer.cpp: flex_lexer.l flex flex_lexer.l diff --git a/src/parser/SQLParser.cpp b/src/parser/SQLParser.cpp index 058299c..91951d1 100644 --- a/src/parser/SQLParser.cpp +++ b/src/parser/SQLParser.cpp @@ -20,7 +20,7 @@ Statement* SQLParser::parseSQLString(const char *text) { if (hsql_lex_init(&scanner)) { // couldn't initialize - fprintf(stderr, "Error when initializing!\n"); + fprintf(stderr, "[Error] SQLParser: Error when initializing lexer!\n"); return NULL; } @@ -28,7 +28,7 @@ Statement* SQLParser::parseSQLString(const char *text) { if (hsql_parse(&stmt, scanner)) { // error parsing - fprintf(stderr, "Error when parsing!\n"); + // fprintf(stderr, "Error when parsing!\n"); return NULL; } diff --git a/src/parser/bison_parser.y b/src/parser/bison_parser.y index c118828..0d8fc71 100644 --- a/src/parser/bison_parser.y +++ b/src/parser/bison_parser.y @@ -20,7 +20,7 @@ using namespace hsql; int yyerror(Statement **expression, yyscan_t scanner, const char *msg) { - fprintf(stderr, "[Error] SQL Parser: %s\n", msg); + // fprintf(stderr, "[Error] SQL Parser: %s\n", msg); return 0; } @@ -107,7 +107,7 @@ typedef void* yyscan_t; %type select_statement %type join_statement %type table_name -%type from_clause table_ref table_ref_atomic +%type
from_clause table_ref table_ref_atomic table_ref_atomic_opt_paren %type expr scalar_expr unary_expr binary_expr function_expr star_expr %type column_name literal int_literal num_literal %type comp_expr where_clause @@ -167,7 +167,7 @@ statement: ******************************/ join_statement: - select_statement JOIN table_ref ON join_condition + table_ref_atomic_opt_paren JOIN table_ref_atomic_opt_paren ON join_condition { $$ = new JoinStatement(); } @@ -194,7 +194,6 @@ select_statement: s->limit = $7; $$ = s; } - | '(' select_statement ')' { $$ = $2; } ; @@ -334,11 +333,24 @@ table_ref_atomic: } ; + table_ref_commalist: table_ref_atomic { $$ = new List($1); } | table_ref_commalist ',' table_ref_atomic { $1->push_back($3); $$ = $1; } ; + +/* For join statements, where a select statement is allowed to be used without parenthesis */ +table_ref_atomic_opt_paren: + table_ref_atomic + | select_statement { + auto tbl = new TableRef(kTableSelect); + tbl->select = $1; + $$ = tbl; + } + ; + + table_name: NAME | NAME '.' NAME diff --git a/src/sql_grammar_test.cpp b/src/sql_grammar_test.cpp index 611dc54..56c65d5 100644 --- a/src/sql_grammar_test.cpp +++ b/src/sql_grammar_test.cpp @@ -12,7 +12,10 @@ int main(int argc, char *argv[]) { return -1; } - for (int n = 1; n < argc; ++n) { + bool expectFalse = (std::string("-f").compare(std::string(argv[1])) == 0); + + int n = (expectFalse) ? 2 : 1; + for (; n < argc; ++n) { char* sql = argv[n]; // Measuring the parsing time @@ -25,11 +28,11 @@ int main(int argc, char *argv[]) { end = std::chrono::system_clock::now(); std::chrono::duration elapsed_seconds = end-start; - if (stmt == NULL) { + if (expectFalse != (stmt == NULL)) { fprintf(stderr, "-> Failed (%.3fms)! \"%s\"\n", elapsed_seconds.count()*1000, sql); continue; } else { - fprintf(stderr, "Success (%.3fms)! \"%s\"\n", elapsed_seconds.count()*1000, sql); + printf("Success (%.3fms%s)! \"%s\"\n", elapsed_seconds.count()*1000, (expectFalse) ? ", expected error" : "", sql); } }