Skip to content

Commit 3beecc0

Browse files
authored
Correct order of arguments in LIMIT x,y , restrict to MySql and generic dialects (#642)
* 613 Fixing MySQL LIMIT syntax * 613 Reducing logic to real case scenario * 613 Adding syntax to generic dialect
1 parent fb5a974 commit 3beecc0

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

src/parser.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ use core::fmt;
2424

2525
use log::debug;
2626

27+
use IsLateral::*;
28+
use IsOptional::*;
29+
2730
use crate::ast::*;
2831
use crate::dialect::*;
2932
use crate::keywords::{self, Keyword};
@@ -57,15 +60,11 @@ pub enum IsOptional {
5760
Mandatory,
5861
}
5962

60-
use IsOptional::*;
61-
6263
pub enum IsLateral {
6364
Lateral,
6465
NotLateral,
6566
}
6667

67-
use IsLateral::*;
68-
6968
pub enum WildcardExpr {
7069
Expr(Expr),
7170
QualifiedWildcard(ObjectName),
@@ -3762,9 +3761,18 @@ impl<'a> Parser<'a> {
37623761
offset = Some(self.parse_offset()?)
37633762
}
37643763

3765-
if offset.is_none() && self.consume_token(&Token::Comma) {
3766-
// mysql style LIMIT 10, offset 5
3767-
offset = Some(self.parse_offset()?)
3764+
if dialect_of!(self is GenericDialect | MySqlDialect)
3765+
&& limit.is_some()
3766+
&& offset.is_none()
3767+
&& self.consume_token(&Token::Comma)
3768+
{
3769+
// MySQL style LIMIT x,y => LIMIT y OFFSET x.
3770+
// Check <https://dev.mysql.com/doc/refman/8.0/en/select.html> for more details.
3771+
offset = Some(Offset {
3772+
value: limit.unwrap(),
3773+
rows: OffsetRows::None,
3774+
});
3775+
limit = Some(self.parse_expr()?);
37683776
}
37693777
}
37703778

@@ -5197,9 +5205,10 @@ impl Word {
51975205

51985206
#[cfg(test)]
51995207
mod tests {
5200-
use super::*;
52015208
use crate::test_utils::{all_dialects, TestedDialects};
52025209

5210+
use super::*;
5211+
52035212
#[test]
52045213
fn test_prev_index() {
52055214
let sql = "SELECT version";

tests/sqlparser_common.rs

-8
Original file line numberDiff line numberDiff line change
@@ -1593,14 +1593,6 @@ fn parse_limit_accepts_all() {
15931593
);
15941594
}
15951595

1596-
#[test]
1597-
fn parse_limit_my_sql_syntax() {
1598-
one_statement_parses_to(
1599-
"SELECT id, fname, lname FROM customer LIMIT 5, 10",
1600-
"SELECT id, fname, lname FROM customer LIMIT 5 OFFSET 10",
1601-
);
1602-
}
1603-
16041596
#[test]
16051597
fn parse_cast() {
16061598
let sql = "SELECT CAST(id AS BIGINT) FROM customer";

tests/sqlparser_mysql.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,14 @@ fn parse_set_names() {
10651065
assert_eq!(stmt, Statement::SetNamesDefault {});
10661066
}
10671067

1068+
#[test]
1069+
fn parse_limit_my_sql_syntax() {
1070+
mysql_and_generic().one_statement_parses_to(
1071+
"SELECT id, fname, lname FROM customer LIMIT 5, 10",
1072+
"SELECT id, fname, lname FROM customer LIMIT 10 OFFSET 5",
1073+
);
1074+
}
1075+
10681076
fn mysql() -> TestedDialects {
10691077
TestedDialects {
10701078
dialects: vec![Box::new(MySqlDialect {})],

0 commit comments

Comments
 (0)