From 6311d670bf460cad93ebe7a7534f93f42fb88c54 Mon Sep 17 00:00:00 2001 From: Dmitry Patsura Date: Fri, 5 Aug 2022 13:25:57 +0300 Subject: [PATCH] feat: Allow to use >> and << binary operators in Generic dialect (#553) --- src/parser.rs | 4 ++-- tests/sqlparser_common.rs | 5 +++-- tests/sqlparser_postgres.rs | 11 ++++++----- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 03eee7070..813dfe77a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1111,10 +1111,10 @@ impl<'a> Parser<'a> { Token::Caret => Some(BinaryOperator::BitwiseXor), Token::Ampersand => Some(BinaryOperator::BitwiseAnd), Token::Div => Some(BinaryOperator::Divide), - Token::ShiftLeft if dialect_of!(self is PostgreSqlDialect) => { + Token::ShiftLeft if dialect_of!(self is PostgreSqlDialect | GenericDialect) => { Some(BinaryOperator::PGBitwiseShiftLeft) } - Token::ShiftRight if dialect_of!(self is PostgreSqlDialect) => { + Token::ShiftRight if dialect_of!(self is PostgreSqlDialect | GenericDialect) => { Some(BinaryOperator::PGBitwiseShiftRight) } Token::Sharp if dialect_of!(self is PostgreSqlDialect) => { diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 3a473bd90..49088b52b 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -24,7 +24,8 @@ mod test_utils; use matches::assert_matches; use sqlparser::ast::*; use sqlparser::dialect::{ - AnsiDialect, GenericDialect, MsSqlDialect, PostgreSqlDialect, SQLiteDialect, SnowflakeDialect, + AnsiDialect, ClickHouseDialect, GenericDialect, HiveDialect, MsSqlDialect, PostgreSqlDialect, + SQLiteDialect, SnowflakeDialect, }; use sqlparser::keywords::ALL_KEYWORDS; use sqlparser::parser::{Parser, ParserError}; @@ -492,7 +493,7 @@ fn test_eof_after_as() { #[test] fn test_no_infix_error() { - let res = Parser::parse_sql(&GenericDialect {}, "ASSERT-URA<<"); + let res = Parser::parse_sql(&ClickHouseDialect {}, "ASSERT-URA<<"); assert_eq!( ParserError::ParserError("No infix parser for token ShiftLeft".to_string()), res.unwrap_err() diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 754fb3fdf..551489c81 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -1033,13 +1033,14 @@ fn parse_prepare() { #[test] fn parse_pg_bitwise_binary_ops() { let bitwise_ops = &[ - ("#", BinaryOperator::PGBitwiseXor), - (">>", BinaryOperator::PGBitwiseShiftRight), - ("<<", BinaryOperator::PGBitwiseShiftLeft), + // Sharp char cannot be used with Generic Dialect, it conflicts with identifiers + ("#", BinaryOperator::PGBitwiseXor, pg()), + (">>", BinaryOperator::PGBitwiseShiftRight, pg_and_generic()), + ("<<", BinaryOperator::PGBitwiseShiftLeft, pg_and_generic()), ]; - for (str_op, op) in bitwise_ops { - let select = pg().verified_only_select(&format!("SELECT a {} b", &str_op)); + for (str_op, op, dialects) in bitwise_ops { + let select = dialects.verified_only_select(&format!("SELECT a {} b", &str_op)); assert_eq!( SelectItem::UnnamedExpr(Expr::BinaryOp { left: Box::new(Expr::Identifier(Ident::new("a"))),