@@ -131,6 +131,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch
131131 std::vector<hsql::ColumnDefinition*>* column_vec;
132132 std::vector<hsql::UpdateClause*>* update_vec;
133133 std::vector<hsql::Expr*>* expr_vec;
134+ std::vector<hsql::OrderDescription*>* order_vec;
134135}
135136
136137
@@ -146,7 +147,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch
146147 }
147148 }
148149 delete ($$);
149- } <str_vec> <table_vec> <column_vec> <update_vec> <expr_vec>
150+ } <str_vec> <table_vec> <column_vec> <update_vec> <expr_vec> <order_vec>
150151%destructor { delete ($$); } <* >
151152
152153
@@ -197,8 +198,8 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch
197198%type <expr> expr scalar_expr unary_expr binary_expr function_expr star_expr expr_alias placeholder_expr
198199%type <expr> column_name literal int_literal num_literal string_literal
199200%type <expr> comp_expr opt_where join_condition opt_having
200- %type <order > opt_order
201- %type <limit > opt_limit
201+ %type <limit > opt_limit opt_top
202+ %type <order > order_desc
202203%type <order_type> opt_order_type
203204%type <column_t> column_def
204205%type <update_t> update_clause
@@ -207,6 +208,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const ch
207208%type <str_vec> ident_commalist opt_column_list
208209%type <expr_vec> expr_list select_list literal_list
209210%type <table_vec> table_ref_commalist
211+ %type <order_vec> opt_order order_list
210212%type <update_vec> update_clause_commalist
211213%type <column_vec> column_def_commalist
212214
@@ -502,13 +504,14 @@ set_operator:
502504 ;
503505
504506select_clause :
505- SELECT opt_distinct select_list from_clause opt_where opt_group {
507+ SELECT opt_top opt_distinct select_list from_clause opt_where opt_group {
506508 $$ = new SelectStatement();
507- $$ ->selectDistinct = $2 ;
508- $$ ->selectList = $3 ;
509- $$ ->fromTable = $4 ;
510- $$ ->whereClause = $5 ;
511- $$ ->groupBy = $6 ;
509+ $$ ->limit = $2 ;
510+ $$ ->selectDistinct = $3 ;
511+ $$ ->selectList = $4 ;
512+ $$ ->fromTable = $5 ;
513+ $$ ->whereClause = $6 ;
514+ $$ ->groupBy = $7 ;
512515 }
513516 ;
514517
@@ -521,7 +524,6 @@ select_list:
521524 expr_list
522525 ;
523526
524-
525527from_clause :
526528 FROM table_ref { $$ = $2 ; }
527529 ;
@@ -546,16 +548,31 @@ opt_having:
546548 | /* empty */ { $$ = NULL ; }
547549
548550opt_order :
549- ORDER BY expr opt_order_type { $$ = new OrderDescription( $4 , $3 ) ; }
551+ ORDER BY order_list { $$ = $3 ; }
550552 | /* empty */ { $$ = NULL ; }
551553 ;
552554
555+ order_list :
556+ order_desc { $$ = new std::vector<OrderDescription*>(); $$ ->push_back ($1 ); }
557+ | order_list ' ,' order_desc { $1 ->push_back ($3 ); $$ = $1 ; }
558+ ;
559+
560+ order_desc :
561+ expr opt_order_type { $$ = new OrderDescription($2 , $1 ); }
562+ ;
563+
553564opt_order_type :
554565 ASC { $$ = kOrderAsc ; }
555566 | DESC { $$ = kOrderDesc ; }
556567 | /* empty */ { $$ = kOrderAsc ; }
557568 ;
558569
570+ // TODO: TOP and LIMIT can take more than just int literals.
571+
572+ opt_top :
573+ TOP int_literal { $$ = new LimitDescription($2 ->ival, kNoOffset ); delete $2 ; }
574+ | /* empty */ { $$ = NULL ; }
575+ ;
559576
560577opt_limit :
561578 LIMIT int_literal { $$ = new LimitDescription($2 ->ival, kNoOffset ); delete $2 ; }
0 commit comments