Skip to content

fix for queries with both order by and limit #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions src/sqlparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,23 +1166,20 @@ impl Parser {
// look for optional ASC / DESC specifier
let asc = match self.peek_token() {
Some(Token::Keyword(k)) => {
self.next_token(); // consume it
match k.to_uppercase().as_ref() {
"ASC" => true,
"DESC" => false,
_ => {
return parser_err!(format!(
"Invalid modifier for ORDER BY expression: {:?}",
k
))
}
"ASC" => {
self.next_token();
true
},
"DESC" => {
self.next_token();
false
},
_ => true
}
}
Some(Token::Comma) => true,
Some(other) => {
return parser_err!(format!("Unexpected token after ORDER BY expr: {:?}", other))
}
None => true,
_ => true,
};

expr_list.push(SQLOrderByExpr::new(Box::new(expr), asc));
Expand Down
27 changes: 27 additions & 0 deletions tests/sqlparser_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,33 @@ fn parse_select_order_by() {
}
}

#[test]
fn parse_select_order_by_limit() {
let sql = String::from(
"SELECT id, fname, lname FROM customer WHERE id < 5 ORDER BY lname ASC, fname DESC LIMIT 2",
);
let ast = parse_sql(&sql);
match ast {
ASTNode::SQLSelect { order_by, limit, .. } => {
assert_eq!(
Some(vec![
SQLOrderByExpr {
expr: Box::new(ASTNode::SQLIdentifier("lname".to_string())),
asc: true,
},
SQLOrderByExpr {
expr: Box::new(ASTNode::SQLIdentifier("fname".to_string())),
asc: false,
},
]),
order_by
);
assert_eq!(Some(Box::new(ASTNode::SQLValue(Value::Long(2)))), limit);
}
_ => assert!(false),
}
}

#[test]
fn parse_select_group_by() {
let sql = String::from("SELECT id, fname, lname FROM customer GROUP BY lname, fname");
Expand Down