Skip to content

Commit 633aeb9

Browse files
authored
Merge pull request apache#26 from virattara/fix_order_limit
fix for queries with both order by and limit
2 parents 038698c + 9f47e8a commit 633aeb9

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/sqlparser.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,23 +1173,20 @@ impl Parser {
11731173
// look for optional ASC / DESC specifier
11741174
let asc = match self.peek_token() {
11751175
Some(Token::Keyword(k)) => {
1176-
self.next_token(); // consume it
11771176
match k.to_uppercase().as_ref() {
1178-
"ASC" => true,
1179-
"DESC" => false,
1180-
_ => {
1181-
return parser_err!(format!(
1182-
"Invalid modifier for ORDER BY expression: {:?}",
1183-
k
1184-
))
1185-
}
1177+
"ASC" => {
1178+
self.next_token();
1179+
true
1180+
},
1181+
"DESC" => {
1182+
self.next_token();
1183+
false
1184+
},
1185+
_ => true
11861186
}
11871187
}
11881188
Some(Token::Comma) => true,
1189-
Some(other) => {
1190-
return parser_err!(format!("Unexpected token after ORDER BY expr: {:?}", other))
1191-
}
1192-
None => true,
1189+
_ => true,
11931190
};
11941191

11951192
expr_list.push(SQLOrderByExpr::new(Box::new(expr), asc));

tests/sqlparser_generic.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,33 @@ fn parse_select_order_by() {
200200
}
201201
}
202202

203+
#[test]
204+
fn parse_select_order_by_limit() {
205+
let sql = String::from(
206+
"SELECT id, fname, lname FROM customer WHERE id < 5 ORDER BY lname ASC, fname DESC LIMIT 2",
207+
);
208+
let ast = parse_sql(&sql);
209+
match ast {
210+
ASTNode::SQLSelect { order_by, limit, .. } => {
211+
assert_eq!(
212+
Some(vec![
213+
SQLOrderByExpr {
214+
expr: Box::new(ASTNode::SQLIdentifier("lname".to_string())),
215+
asc: true,
216+
},
217+
SQLOrderByExpr {
218+
expr: Box::new(ASTNode::SQLIdentifier("fname".to_string())),
219+
asc: false,
220+
},
221+
]),
222+
order_by
223+
);
224+
assert_eq!(Some(Box::new(ASTNode::SQLValue(Value::Long(2)))), limit);
225+
}
226+
_ => assert!(false),
227+
}
228+
}
229+
203230
#[test]
204231
fn parse_select_group_by() {
205232
let sql = String::from("SELECT id, fname, lname FROM customer GROUP BY lname, fname");

0 commit comments

Comments
 (0)