Skip to content
Open
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
24 changes: 1 addition & 23 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -2445,26 +2443,6 @@ pub fn dialect_from_str(dialect_name: impl AsRef<str>) -> Option<Box<dyn Dialect
}
}

/// Parse `DIV` using the caller's infix precedence.
fn parse_div_infix(
parser: &mut Parser,
expr: &Expr,
precedence: u8,
) -> Option<Result<Expr, ParserError>> {
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::*;
Expand Down
11 changes: 1 addition & 10 deletions src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -110,15 +110,6 @@ impl Dialect for MySqlDialect {
true
}

fn parse_infix(
&self,
parser: &mut Parser,
expr: &Expr,
precedence: u8,
) -> Option<Result<Expr, ParserError>> {
super::parse_div_infix(parser, expr, precedence)
}

fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
if parser.parse_keywords(&[Keyword::LOCK, Keyword::TABLES]) {
Some(parse_lock_tables(parser))
Expand Down
16 changes: 0 additions & 16 deletions src/dialect/spark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
///
Expand Down Expand Up @@ -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 <https://spark.apache.org/docs/latest/sql-ref-functions-builtin-math.html>
fn parse_infix(
&self,
parser: &mut Parser,
expr: &Expr,
precedence: u8,
) -> Option<Result<Expr, ParserError>> {
super::parse_div_infix(parser, expr, precedence)
}
}
29 changes: 2 additions & 27 deletions src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Result<crate::ast::Expr, ParserError>> {
// Parse MATCH, REGEXP and GLOB as operators
// See <https://www.sqlite.org/lang_expr.html#the_like_glob_regexp_match_and_extract_operators>
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 <https://www.sqlite.org/lang_expr.html#operators_and_parse_affecting_attributes>
fn get_next_precedence(&self, parser: &Parser) -> Option<Result<u8, ParserError>> {
Expand Down
20 changes: 20 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
10 changes: 10 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading