Skip to content

feat: Support MySQL's DIV operator #876

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 3 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions src/ast/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub enum BinaryOperator {
BitwiseOr,
BitwiseAnd,
BitwiseXor,
MyIntegerDivide,
PGBitwiseXor,
PGBitwiseShiftLeft,
PGBitwiseShiftRight,
Expand Down Expand Up @@ -122,6 +123,7 @@ impl fmt::Display for BinaryOperator {
BinaryOperator::BitwiseOr => f.write_str("|"),
BinaryOperator::BitwiseAnd => f.write_str("&"),
BinaryOperator::BitwiseXor => f.write_str("^"),
BinaryOperator::MyIntegerDivide => f.write_str("DIV"),
BinaryOperator::PGBitwiseXor => f.write_str("#"),
BinaryOperator::PGBitwiseShiftLeft => f.write_str("<<"),
BinaryOperator::PGBitwiseShiftRight => f.write_str(">>"),
Expand Down
27 changes: 26 additions & 1 deletion src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::dialect::Dialect;
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;

use crate::{
ast::{BinaryOperator, Expr},
dialect::Dialect,
keywords::Keyword,
};

/// [MySQL](https://www.mysql.com/)
#[derive(Debug)]
Expand All @@ -35,4 +42,22 @@ impl Dialect for MySqlDialect {
fn is_delimited_identifier_start(&self, ch: char) -> bool {
ch == '`'
}

fn parse_infix(
&self,
_parser: &mut crate::parser::Parser,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normally this would not have the leading _

So it perhaps would be better like

        _parser: &mut crate::parser::Parser,

I'll fix it as a follow on PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5fba8c9

_expr: &crate::ast::Expr,
_precedence: u8,
) -> Option<Result<crate::ast::Expr, crate::parser::ParserError>> {
// Parse DIV as an operator
if _parser.parse_keyword(Keyword::DIV) {
Some(Ok(Expr::BinaryOp {
left: Box::new(_expr.clone()),
op: BinaryOperator::MyIntegerDivide,
right: Box::new(_parser.parse_expr().unwrap()),
}))
} else {
None
}
}
}
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ define_keywords!(
DISCONNECT,
DISTINCT,
DISTRIBUTE,
DIV,
DO,
DOUBLE,
DOW,
Expand Down
3 changes: 3 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1980,6 +1980,8 @@ impl<'a> Parser<'a> {
const AND_PREC: u8 = 10;
const OR_PREC: u8 = 5;

const DIV_OP_PREC: u8 = 40;

/// Get the precedence of the next token
pub fn get_next_precedence(&self) -> Result<u8, ParserError> {
// allow the dialect to override precedence logic
Expand Down Expand Up @@ -2029,6 +2031,7 @@ impl<'a> Parser<'a> {
Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(Self::LIKE_PREC),
Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(Self::LIKE_PREC),
Token::Word(w) if w.keyword == Keyword::OPERATOR => Ok(Self::BETWEEN_PREC),
Token::Word(w) if w.keyword == Keyword::DIV => Ok(Self::DIV_OP_PREC),
Token::Eq
| Token::Lt
| Token::LtEq
Expand Down
5 changes: 5 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,3 +1407,8 @@ fn parse_string_introducers() {
mysql().one_statement_parses_to("SELECT _utf8mb4'abc'", "SELECT _utf8mb4 'abc'");
mysql().verified_stmt("SELECT _binary 'abc', _utf8mb4 'abc'");
}

#[test]
fn parse_div_infix() {
mysql().verified_stmt(r#"SELECT 5 DIV 2"#);
}