Skip to content

Commit 18dc375

Browse files
authored
Merge pull request apache#9 from mshauneu/main
add DIV operator
2 parents 599d75a + 5e44b4a commit 18dc375

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

src/ast/operator.rs

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub enum BinaryOperator {
6060
Minus,
6161
Multiply,
6262
Divide,
63+
Div,
6364
Modulo,
6465
StringConcat,
6566
Gt,
@@ -95,6 +96,7 @@ impl fmt::Display for BinaryOperator {
9596
BinaryOperator::Minus => "-",
9697
BinaryOperator::Multiply => "*",
9798
BinaryOperator::Divide => "/",
99+
BinaryOperator::Div => "DIV",
98100
BinaryOperator::Modulo => "%",
99101
BinaryOperator::StringConcat => "||",
100102
BinaryOperator::Gt => ">",

src/keywords.rs

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ define_keywords!(
179179
DISCONNECT,
180180
DISTINCT,
181181
DISTRIBUTE,
182+
DIV,
182183
DOUBLE,
183184
DROP,
184185
DYNAMIC,

src/parser.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ impl<'a> Parser<'a> {
893893
Token::Pipe => Some(BinaryOperator::BitwiseOr),
894894
Token::Caret => Some(BinaryOperator::BitwiseXor),
895895
Token::Ampersand => Some(BinaryOperator::BitwiseAnd),
896-
Token::Div => Some(BinaryOperator::Divide),
896+
Token::Divide => Some(BinaryOperator::Divide),
897897
Token::ShiftLeft if dialect_of!(self is PostgreSqlDialect) => {
898898
Some(BinaryOperator::PGBitwiseShiftLeft)
899899
}
@@ -908,6 +908,7 @@ impl<'a> Parser<'a> {
908908
Token::ExclamationMarkTilde => Some(BinaryOperator::PGRegexNotMatch),
909909
Token::ExclamationMarkTildeAsterisk => Some(BinaryOperator::PGRegexNotIMatch),
910910
Token::Word(w) => match w.keyword {
911+
Keyword::DIV => Some(BinaryOperator::Div),
911912
Keyword::AND => Some(BinaryOperator::And),
912913
Keyword::OR => Some(BinaryOperator::Or),
913914
Keyword::LIKE => Some(BinaryOperator::Like),
@@ -1077,6 +1078,7 @@ impl<'a> Parser<'a> {
10771078
Token::Word(w) if w.keyword == Keyword::BETWEEN => Ok(Self::BETWEEN_PREC),
10781079
Token::Word(w) if w.keyword == Keyword::LIKE => Ok(Self::BETWEEN_PREC),
10791080
Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(Self::BETWEEN_PREC),
1081+
Token::Word(w) if w.keyword == Keyword::DIV => Ok(40),
10801082
Token::Eq
10811083
| Token::Lt
10821084
| Token::LtEq
@@ -1093,7 +1095,7 @@ impl<'a> Parser<'a> {
10931095
Token::Caret | Token::Sharp | Token::ShiftRight | Token::ShiftLeft => Ok(22),
10941096
Token::Ampersand => Ok(23),
10951097
Token::Plus | Token::Minus => Ok(Self::PLUS_MINUS_PREC),
1096-
Token::Mul | Token::Div | Token::Mod | Token::StringConcat => Ok(40),
1098+
Token::Mul | Token::Divide | Token::Mod | Token::StringConcat => Ok(40),
10971099
Token::DoubleColon => Ok(50),
10981100
Token::ExclamationMark => Ok(50),
10991101
Token::LBracket | Token::RBracket => Ok(10),

src/tokenizer.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub enum Token {
8181
/// Multiplication operator `*`
8282
Mul,
8383
/// Division operator `/`
84-
Div,
84+
Divide,
8585
/// Modulo Operator `%`
8686
Mod,
8787
/// String concatenation `||`
@@ -166,7 +166,7 @@ impl fmt::Display for Token {
166166
Token::Plus => f.write_str("+"),
167167
Token::Minus => f.write_str("-"),
168168
Token::Mul => f.write_str("*"),
169-
Token::Div => f.write_str("/"),
169+
Token::Divide => f.write_str("/"),
170170
Token::StringConcat => f.write_str("||"),
171171
Token::Mod => f.write_str("%"),
172172
Token::LParen => f.write_str("("),
@@ -504,7 +504,7 @@ impl<'a> Tokenizer<'a> {
504504
})))
505505
}
506506
// a regular '/' operator
507-
_ => Ok(Some(Token::Div)),
507+
_ => Ok(Some(Token::Divide)),
508508
}
509509
}
510510
'+' => self.consume_and_return(chars, Token::Plus),

tests/sqlparser_common.rs

+19
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,25 @@ fn parse_compound_expr_2() {
602602
);
603603
}
604604

605+
#[test]
606+
fn parse_compound_expr_3() {
607+
use self::BinaryOperator::*;
608+
use self::Expr::*;
609+
let sql = "a + b DIV c";
610+
assert_eq!(
611+
BinaryOp {
612+
left: Box::new(Identifier(Ident::new("a"))),
613+
op: Plus,
614+
right: Box::new(BinaryOp {
615+
left: Box::new(Identifier(Ident::new("b"))),
616+
op: Div,
617+
right: Box::new(Identifier(Ident::new("c")))
618+
})
619+
},
620+
verified_expr(sql)
621+
);
622+
}
623+
605624
#[test]
606625
fn parse_unary_math() {
607626
use self::Expr::*;

0 commit comments

Comments
 (0)