implemented table alias

This commit is contained in:
Pedro 2014-11-04 01:42:09 +01:00
parent b06ea85ff5
commit afe8dafef5
5 changed files with 52 additions and 26 deletions

View File

@ -11,17 +11,17 @@ make grammar_test
echo "\n\n" 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 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 AS t 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 age FROM table AS t1, (SELECT * FROM table2) AS t2 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 AS t1 JOIN table2 ON a = b"
./bin/grammar_test -f "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3 LIMIT 10)" ./bin/grammar_test "(SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3 LIMIT 10)"
echo "\n\n" 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 col1, col2, 'test' FROM table, foo WHERE age > 12 AND zipcode = 12345 GROUP BY col1 ORDER BY col2 DESC LIMIT 100;" ./bin/analysis "SELECT col1, col2, 'test' FROM table t1, foo WHERE age > 12 AND zipcode = 12345 GROUP BY col1 ORDER BY col2 DESC LIMIT 100;"
./bin/analysis "SELECT * from table WHERE (b OR NOT a) AND a = 12.5 JOIN table2 ON t1.a = t2.b" ./bin/analysis "SELECT * from table WHERE (b OR NOT a) AND a = 12.5 AS t1 JOIN table2 AS t2 ON t1.a = t2.b"
# ./bin/analysis "SELECT * from table WHERE (b OR NOT a) AND a = 12.5 JOIN table2 ON a = b" # ./bin/analysis "SELECT * from table WHERE (b OR NOT a) AND a = 12.5 JOIN table2 ON a = b"
echo "\n\n" echo "\n\n"

View File

@ -26,6 +26,7 @@ struct TableRef {
TableRefType type; TableRefType type;
char* name; char* name;
char* alias;
SelectStatement* select; SelectStatement* select;
JoinStatement* join; JoinStatement* join;
List<TableRef*>* list; List<TableRef*>* list;

View File

@ -28,6 +28,10 @@ void printTableRefInfo(TableRef* table, uint num_indent) {
for (TableRef* tbl : table->list->_vector) printTableRefInfo(tbl, num_indent); for (TableRef* tbl : table->list->_vector) printTableRefInfo(tbl, num_indent);
break; break;
} }
if (table->alias != NULL) {
inprint("Alias", num_indent+1);
inprint(table->alias, num_indent+2);
}
} }
void printOperatorExpression(Expr* expr, uint num_indent) { void printOperatorExpression(Expr* expr, uint num_indent) {
@ -86,11 +90,13 @@ void printSelectStatementInfo(SelectStatement* stmt, uint num_indent) {
void printJoinStatementInfo(JoinStatement* stmt, uint num_indent) { void printJoinStatementInfo(JoinStatement* stmt, uint num_indent) {
inprint("JoinStatement", num_indent); inprint("JoinStatement", num_indent);
inprint("Left Table", num_indent+1); inprint("JoinType:", num_indent+1);
inprintU(stmt->join_type, num_indent+2);
inprint("Left Table:", num_indent+1);
printTableRefInfo(stmt->left, num_indent+2); printTableRefInfo(stmt->left, num_indent+2);
inprint("Right Table", num_indent+1); inprint("Right Table:", num_indent+1);
printTableRefInfo(stmt->right, num_indent+2); printTableRefInfo(stmt->right, num_indent+2);
inprint("Join Condition", num_indent+1); inprint("Join Condition:", num_indent+1);
printExpression(stmt->join_condition, num_indent+2); printExpression(stmt->join_condition, num_indent+2);
} }

View File

@ -107,8 +107,9 @@ typedef void* yyscan_t;
%type <statement> statement %type <statement> statement
%type <select_stmt> select_statement %type <select_stmt> select_statement
%type <join_stmt> join_statement %type <join_stmt> join_statement
%type <sval> table_name %type <sval> table_name opt_alias alias
%type <table> from_clause table_ref table_ref_atomic table_ref_atomic_opt_paren %type <table> from_clause table_ref table_ref_atomic table_ref_name
%type <table> join_table
%type <expr> expr scalar_expr unary_expr binary_expr function_expr star_expr %type <expr> expr scalar_expr unary_expr binary_expr function_expr star_expr
%type <expr> column_name literal int_literal num_literal %type <expr> column_name literal int_literal num_literal
%type <expr> comp_expr where_clause join_condition %type <expr> comp_expr where_clause join_condition
@ -137,6 +138,7 @@ typedef void* yyscan_t;
%left '^' %left '^'
/* Unary Operators */ /* Unary Operators */
%right UMINUS
%left '[' ']' %left '[' ']'
%left '(' ')' %left '(' ')'
%left '.' %left '.'
@ -168,7 +170,7 @@ statement:
******************************/ ******************************/
join_statement: join_statement:
table_ref_atomic_opt_paren JOIN table_ref_atomic_opt_paren ON join_condition join_table JOIN join_table ON join_condition
{ {
$$ = new JoinStatement(); $$ = new JoinStatement();
$$->left = $1; $$->left = $1;
@ -178,6 +180,17 @@ join_statement:
} }
; ;
join_table:
select_statement alias {
auto tbl = new TableRef(kTableSelect);
tbl->select = $1;
tbl->alias = $2;
$$ = tbl;
}
| table_ref_name;
join_condition: join_condition:
expr expr
; ;
@ -199,6 +212,7 @@ select_statement:
s->limit = $7; s->limit = $7;
$$ = s; $$ = s;
} }
| '(' select_statement ')' { $$ = $2; }
; ;
@ -326,15 +340,13 @@ table_ref:
} }
; ;
table_ref_atomic: table_ref_atomic:
table_name { table_ref_name
auto tbl = new TableRef(kTableName); | '(' select_statement ')' alias {
tbl->name = $1;
$$ = tbl;
}
| '(' select_statement ')' {
auto tbl = new TableRef(kTableSelect); auto tbl = new TableRef(kTableSelect);
tbl->select = $2; tbl->select = $2;
tbl->alias = $4;
$$ = tbl; $$ = tbl;
} }
; ;
@ -346,23 +358,29 @@ table_ref_commalist:
; ;
/* For join statements, where a select statement is allowed to be used without parenthesis */ table_ref_name:
table_ref_atomic_opt_paren: table_name opt_alias {
table_ref_atomic auto tbl = new TableRef(kTableName);
| select_statement { tbl->name = $1;
auto tbl = new TableRef(kTableSelect); tbl->alias = $2;
tbl->select = $1;
$$ = tbl; $$ = tbl;
} }
; ;
table_name: table_name:
NAME NAME
| NAME '.' NAME | NAME '.' NAME
; ;
alias:
AS NAME { $$ = $2; }
| NAME
;
opt_alias:
alias
| /* empty */ { $$ = NULL; }
opt_semicolon: opt_semicolon:
';' ';'

View File

@ -57,6 +57,7 @@ WHERE TOKEN(WHERE)
NOT TOKEN(NOT) NOT TOKEN(NOT)
AND TOKEN(AND) AND TOKEN(AND)
OR TOKEN(OR) OR TOKEN(OR)
AS TOKEN(AS)
LIMIT TOKEN(LIMIT) LIMIT TOKEN(LIMIT)
ORDER TOKEN(ORDER) ORDER TOKEN(ORDER)