diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index 9c74cc62af..4dc9af6af7 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -151,9 +151,7 @@ pub use self::teradata::TeradataDialect; #[cfg(feature = "derive-dialect")] pub use sqlparser_derive::derive_dialect; -use crate::ast::{ - BinaryOperator, ColumnOption, DataType, Expr, GranteesType, Ident, ObjectNamePart, Statement, -}; +use crate::ast::{ColumnOption, DataType, Expr, GranteesType, Ident, ObjectNamePart, Statement}; pub use crate::keywords; use crate::keywords::Keyword; use crate::parser::{Parser, ParserError}; @@ -2445,26 +2443,6 @@ pub fn dialect_from_str(dialect_name: impl AsRef) -> Option Option> { - if !parser.parse_keyword(Keyword::DIV) { - return None; - } - Some( - parser - .parse_subexpr(precedence) - .map(|right| Expr::BinaryOp { - left: Box::new(expr.clone()), - op: BinaryOperator::MyIntegerDivide, - right: Box::new(right), - }), - ) -} - #[cfg(test)] mod tests { use super::*; diff --git a/src/dialect/mysql.rs b/src/dialect/mysql.rs index 95bd27168e..040a52000d 100644 --- a/src/dialect/mysql.rs +++ b/src/dialect/mysql.rs @@ -16,7 +16,7 @@ // under the License. use crate::{ - ast::{Expr, LockTable, LockTableType, Statement}, + ast::{LockTable, LockTableType, Statement}, dialect::{BindPlaceholderStyle, Dialect, Precedence}, keywords::Keyword, parser::{Parser, ParserError}, @@ -110,15 +110,6 @@ impl Dialect for MySqlDialect { true } - fn parse_infix( - &self, - parser: &mut Parser, - expr: &Expr, - precedence: u8, - ) -> Option> { - super::parse_div_infix(parser, expr, precedence) - } - fn parse_statement(&self, parser: &mut Parser) -> Option> { if parser.parse_keywords(&[Keyword::LOCK, Keyword::TABLES]) { Some(parse_lock_tables(parser)) diff --git a/src/dialect/spark.rs b/src/dialect/spark.rs index 841a508c47..00d47810e8 100644 --- a/src/dialect/spark.rs +++ b/src/dialect/spark.rs @@ -15,9 +15,7 @@ // specific language governing permissions and limitations // under the License. -use crate::ast::Expr; use crate::dialect::Dialect; -use crate::parser::{Parser, ParserError}; /// A [`Dialect`] for [Apache Spark SQL](https://spark.apache.org/docs/latest/sql-ref.html). /// @@ -149,18 +147,4 @@ impl Dialect for SparkSqlDialect { fn supports_pipe_operator(&self) -> bool { true } - - /// Parse the `DIV` keyword as integer division. - /// - /// Example: `SELECT 10 DIV 3` returns `3`. - /// - /// See - fn parse_infix( - &self, - parser: &mut Parser, - expr: &Expr, - precedence: u8, - ) -> Option> { - super::parse_div_infix(parser, expr, precedence) - } } diff --git a/src/dialect/sqlite.rs b/src/dialect/sqlite.rs index 30ea60c58a..f8ae476b86 100644 --- a/src/dialect/sqlite.rs +++ b/src/dialect/sqlite.rs @@ -16,9 +16,9 @@ // under the License. #[cfg(not(feature = "std"))] -use alloc::{boxed::Box, string::String}; +use alloc::string::String; -use crate::ast::{BinaryOperator, DataType, Expr, ObjectName, Statement}; +use crate::ast::{DataType, ObjectName, Statement}; use crate::dialect::{BindPlaceholderStyle, Dialect, Precedence}; use crate::keywords::{self, Keyword}; use crate::parser::{Parser, ParserError}; @@ -84,31 +84,6 @@ impl Dialect for SQLiteDialect { parse_type_with_numeric_modifiers(parser).map(Ok) } - fn parse_infix( - &self, - parser: &mut crate::parser::Parser, - expr: &crate::ast::Expr, - precedence: u8, - ) -> Option> { - // Parse MATCH, REGEXP and GLOB as operators - // See - for (keyword, op) in [ - (Keyword::REGEXP, BinaryOperator::Regexp), - (Keyword::MATCH, BinaryOperator::Match), - (Keyword::GLOB, BinaryOperator::Glob), - ] { - if parser.parse_keyword(keyword) { - let left = Box::new(expr.clone()); - let right = Box::new(match parser.parse_subexpr(precedence) { - Ok(expr) => expr, - Err(e) => return Some(Err(e)), - }); - return Some(Ok(Expr::BinaryOp { left, op, right })); - } - } - None - } - /// `&`, `|`, `<<`, and `>>` share one left-associative precedence level. /// See fn get_next_precedence(&self, parser: &Parser) -> Option> { diff --git a/src/parser/mod.rs b/src/parser/mod.rs index f2a4b6e718..479de953be 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -4188,6 +4188,26 @@ impl<'a> Parser<'a> { } } else if let Token::Word(w) = &tok.token { match w.keyword { + Keyword::DIV if dialect_is!(dialect is MySqlDialect | SparkSqlDialect) => { + Ok(Expr::BinaryOp { + left: Box::new(expr), + op: BinaryOperator::MyIntegerDivide, + right: Box::new(self.parse_subexpr(precedence)?), + }) + } + Keyword::REGEXP | Keyword::MATCH | Keyword::GLOB if dialect_is!(dialect is SQLiteDialect) => + { + let op = match w.keyword { + Keyword::REGEXP => BinaryOperator::Regexp, + Keyword::MATCH => BinaryOperator::Match, + _ => BinaryOperator::Glob, + }; + Ok(Expr::BinaryOp { + left: Box::new(expr), + op, + right: Box::new(self.parse_subexpr(precedence)?), + }) + } Keyword::IS => { if self.parse_keyword(Keyword::NULL) { Ok(Expr::IsNull(Box::new(expr))) diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs index 3b274aed89..ef3f75f18d 100644 --- a/tests/sqlparser_mysql.rs +++ b/tests/sqlparser_mysql.rs @@ -3829,6 +3829,16 @@ fn parse_div_infix_propagates_parse_error() { assert_matches!(err, ParserError::ParserError(_)); } +#[test] +fn parse_long_div_chain() { + // DIV must take its left operand rather than copy it: a copy makes a + // chain of n operators take time quadratic in n. + let sql = format!("SELECT a{}", " DIV b".repeat(5000)); + mysql().parse_sql_statements(&sql).unwrap(); + // The right operand is an ordinary expression, not ANY/ALL. + mysql().verified_expr("a DIV ANY(b)"); +} + #[test] fn parse_div_precedence() { let div = |left: Expr, right: Expr| Expr::BinaryOp { diff --git a/tests/sqlparser_sqlite.rs b/tests/sqlparser_sqlite.rs index cef2b554a7..71adf5cb2d 100644 --- a/tests/sqlparser_sqlite.rs +++ b/tests/sqlparser_sqlite.rs @@ -1207,6 +1207,18 @@ fn parse_pattern_operators_bind_at_like_precedence() { } } +#[test] +fn parse_long_regexp_match_glob_chain() { + // Each operator must take its left operand rather than copy it: a copy + // makes a chain of n operators take time quadratic in n. + for operator in ["REGEXP", "MATCH", "GLOB"] { + let sql = format!("SELECT a{}", format!(" {operator} b").repeat(5000)); + sqlite().parse_sql_statements(&sql).unwrap(); + // The right operand is an ordinary expression, not ANY/ALL. + sqlite().verified_expr(&format!("a {operator} ANY(b)")); + } +} + #[test] fn parse_update_set_double_eq() { // SQLite treats `==` as `=` in all positions, including SET assignments.