Skip to content

Commit

Permalink
Correct order of arguments in LIMIT x,y , restrict to MySql and gen…
Browse files Browse the repository at this point in the history
…eric dialects (#642)

* 613 Fixing MySQL LIMIT syntax

* 613 Reducing logic to real case scenario

* 613 Adding syntax to generic dialect
  • Loading branch information
AugustoFKL committed Oct 1, 2022
1 parent fb5a974 commit 3beecc0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
25 changes: 17 additions & 8 deletions src/parser.rs
Expand Up @@ -24,6 +24,9 @@ use core::fmt;

use log::debug;

use IsLateral::*;
use IsOptional::*;

use crate::ast::*;
use crate::dialect::*;
use crate::keywords::{self, Keyword};
Expand Down Expand Up @@ -57,15 +60,11 @@ pub enum IsOptional {
Mandatory,
}

use IsOptional::*;

pub enum IsLateral {
Lateral,
NotLateral,
}

use IsLateral::*;

pub enum WildcardExpr {
Expr(Expr),
QualifiedWildcard(ObjectName),
Expand Down Expand Up @@ -3762,9 +3761,18 @@ impl<'a> Parser<'a> {
offset = Some(self.parse_offset()?)
}

if offset.is_none() && self.consume_token(&Token::Comma) {
// mysql style LIMIT 10, offset 5
offset = Some(self.parse_offset()?)
if dialect_of!(self is GenericDialect | MySqlDialect)
&& limit.is_some()
&& offset.is_none()
&& self.consume_token(&Token::Comma)
{
// MySQL style LIMIT x,y => LIMIT y OFFSET x.
// Check <https://dev.mysql.com/doc/refman/8.0/en/select.html> for more details.
offset = Some(Offset {
value: limit.unwrap(),
rows: OffsetRows::None,
});
limit = Some(self.parse_expr()?);
}
}

Expand Down Expand Up @@ -5197,9 +5205,10 @@ impl Word {

#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::{all_dialects, TestedDialects};

use super::*;

#[test]
fn test_prev_index() {
let sql = "SELECT version";
Expand Down
8 changes: 0 additions & 8 deletions tests/sqlparser_common.rs
Expand Up @@ -1593,14 +1593,6 @@ fn parse_limit_accepts_all() {
);
}

#[test]
fn parse_limit_my_sql_syntax() {
one_statement_parses_to(
"SELECT id, fname, lname FROM customer LIMIT 5, 10",
"SELECT id, fname, lname FROM customer LIMIT 5 OFFSET 10",
);
}

#[test]
fn parse_cast() {
let sql = "SELECT CAST(id AS BIGINT) FROM customer";
Expand Down
8 changes: 8 additions & 0 deletions tests/sqlparser_mysql.rs
Expand Up @@ -1065,6 +1065,14 @@ fn parse_set_names() {
assert_eq!(stmt, Statement::SetNamesDefault {});
}

#[test]
fn parse_limit_my_sql_syntax() {
mysql_and_generic().one_statement_parses_to(
"SELECT id, fname, lname FROM customer LIMIT 5, 10",
"SELECT id, fname, lname FROM customer LIMIT 10 OFFSET 5",
);
}

fn mysql() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(MySqlDialect {})],
Expand Down

0 comments on commit 3beecc0

Please sign in to comment.