Skip to content

Commit f82504b

Browse files
committed
Allow order by to define a list of expressions to order by
1 parent 36adab7 commit f82504b

5 files changed

Lines changed: 52 additions & 14 deletions

File tree

‎src/parser/bison_parser.y‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

504506
select_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-
525527
from_clause:
526528
FROM table_ref { $$ = $2; }
527529
;
@@ -546,16 +548,31 @@ opt_having:
546548
| /* empty */ { $$ = NULL; }
547549

548550
opt_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+
553564
opt_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

560577
opt_limit:
561578
LIMIT int_literal { $$ = new LimitDescription($2->ival, kNoOffset); delete $2; }

‎src/sql/SelectStatement.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace hsql {
6363
GroupByDescription* groupBy;
6464

6565
SelectStatement* unionSelect;
66-
OrderDescription* order;
66+
std::vector<OrderDescription*>* order;
6767
LimitDescription* limit;
6868
};
6969

‎src/sqlhelper.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ namespace hsql {
139139

140140
if (stmt->order != NULL) {
141141
inprint("OrderBy:", numIndent + 1);
142-
printExpression(stmt->order->expr, numIndent + 2);
143-
if (stmt->order->type == kOrderAsc) inprint("ascending", numIndent + 2);
142+
printExpression(stmt->order->at(0)->expr, numIndent + 2);
143+
if (stmt->order->at(0)->type == kOrderAsc) inprint("ascending", numIndent + 2);
144144
else inprint("descending", numIndent + 2);
145145
}
146146

‎test/select_tests.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,23 @@ TEST(SelectGroupDistinctTest) {
7373
}
7474

7575

76+
TEST(OrderByTest) {
77+
TEST_PARSE_SINGLE_SQL(
78+
"SELECT grade, city FROM students ORDER BY grade, city DESC;",
79+
kStmtSelect,
80+
SelectStatement,
81+
result,
82+
stmt);
83+
84+
ASSERT_NULL(stmt->whereClause);
85+
ASSERT_NOTNULL(stmt->order);
86+
87+
ASSERT_EQ(stmt->order->size(), 2);
88+
ASSERT_EQ(stmt->order->at(0)->type, kOrderAsc);
89+
ASSERT_STREQ(stmt->order->at(0)->expr->name, "grade");
90+
91+
ASSERT_EQ(stmt->order->at(1)->type, kOrderDesc);
92+
ASSERT_STREQ(stmt->order->at(1)->expr->name, "city");
93+
94+
delete result;
95+
}

‎test/valid_queries.sql‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ SELECT * FROM "table" LIMIT 10 OFFSET 10; SELECT * FROM second;
88
SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY col1;
99
SELECT * FROM (SELECT * FROM t1);
1010
SELECT * FROM t1 UNION (SELECT * FROM t2 UNION SELECT * FROM t3) ORDER BY col1;
11+
SELECT TOP 10 * FROM t1 ORDER BY col1, col2;
1112
# JOIN
1213
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
1314
SELECT * FROM t1 JOIN t2 ON c1 = c2;

0 commit comments

Comments
 (0)