Skip to content

Commit ca8131c

Browse files
committed
feat: Allow to use >> and << binary operators in Generic dialect (apache#553)
1 parent 9cd6792 commit ca8131c

3 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,10 +1111,10 @@ impl<'a> Parser<'a> {
11111111
Token::Caret => Some(BinaryOperator::BitwiseXor),
11121112
Token::Ampersand => Some(BinaryOperator::BitwiseAnd),
11131113
Token::Div => Some(BinaryOperator::Divide),
1114-
Token::ShiftLeft if dialect_of!(self is PostgreSqlDialect) => {
1114+
Token::ShiftLeft if dialect_of!(self is PostgreSqlDialect | GenericDialect) => {
11151115
Some(BinaryOperator::PGBitwiseShiftLeft)
11161116
}
1117-
Token::ShiftRight if dialect_of!(self is PostgreSqlDialect) => {
1117+
Token::ShiftRight if dialect_of!(self is PostgreSqlDialect | GenericDialect) => {
11181118
Some(BinaryOperator::PGBitwiseShiftRight)
11191119
}
11201120
Token::Sharp if dialect_of!(self is PostgreSqlDialect) => {

tests/sqlparser_common.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ mod test_utils;
2424
use matches::assert_matches;
2525
use sqlparser::ast::*;
2626
use sqlparser::dialect::{
27-
AnsiDialect, GenericDialect, MsSqlDialect, PostgreSqlDialect, SQLiteDialect, SnowflakeDialect,
27+
AnsiDialect, ClickHouseDialect, GenericDialect, MsSqlDialect, PostgreSqlDialect, SQLiteDialect,
28+
SnowflakeDialect,
2829
};
2930
use sqlparser::keywords::ALL_KEYWORDS;
3031
use sqlparser::parser::{Parser, ParserError};
@@ -492,7 +493,7 @@ fn test_eof_after_as() {
492493

493494
#[test]
494495
fn test_no_infix_error() {
495-
let res = Parser::parse_sql(&GenericDialect {}, "ASSERT-URA<<");
496+
let res = Parser::parse_sql(&ClickHouseDialect {}, "ASSERT-URA<<");
496497
assert_eq!(
497498
ParserError::ParserError("No infix parser for token ShiftLeft".to_string()),
498499
res.unwrap_err()

tests/sqlparser_postgres.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,13 +1033,14 @@ fn parse_prepare() {
10331033
#[test]
10341034
fn parse_pg_bitwise_binary_ops() {
10351035
let bitwise_ops = &[
1036-
("#", BinaryOperator::PGBitwiseXor),
1037-
(">>", BinaryOperator::PGBitwiseShiftRight),
1038-
("<<", BinaryOperator::PGBitwiseShiftLeft),
1036+
// Sharp char cannot be used with Generic Dialect, it conflicts with identifiers
1037+
("#", BinaryOperator::PGBitwiseXor, pg()),
1038+
(">>", BinaryOperator::PGBitwiseShiftRight, pg_and_generic()),
1039+
("<<", BinaryOperator::PGBitwiseShiftLeft, pg_and_generic()),
10391040
];
10401041

1041-
for (str_op, op) in bitwise_ops {
1042-
let select = pg().verified_only_select(&format!("SELECT a {} b", &str_op));
1042+
for (str_op, op, dialects) in bitwise_ops {
1043+
let select = dialects.verified_only_select(&format!("SELECT a {} b", &str_op));
10431044
assert_eq!(
10441045
SelectItem::UnnamedExpr(Expr::BinaryOp {
10451046
left: Box::new(Expr::Identifier(Ident::new("a"))),

0 commit comments

Comments
 (0)