diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index 9e6c1859f..873108ee9 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -1237,6 +1237,12 @@ pub trait Dialect: Debug + Any { fn supports_double_ampersand_operator(&self) -> bool { false } + + /// Returns true if the dialect supports casting an expression to a binary type + /// using the `BINARY ` syntax. + fn supports_binary_kw_as_cast(&self) -> bool { + false + } } /// Operators for which precedence must be defined. diff --git a/src/dialect/mysql.rs b/src/dialect/mysql.rs index 60385c5bc..355156f79 100644 --- a/src/dialect/mysql.rs +++ b/src/dialect/mysql.rs @@ -176,6 +176,12 @@ impl Dialect for MySqlDialect { fn supports_double_ampersand_operator(&self) -> bool { true } + + /// Deprecated functionality by MySQL but still supported + /// See: + fn supports_binary_kw_as_cast(&self) -> bool { + true + } } /// `LOCK TABLES` diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 8001611e0..10c04207d 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1641,6 +1641,15 @@ impl<'a> Parser<'a> { // an unary negation `NOT ('a' LIKE 'b')`. To solve this, we don't accept the // `type 'string'` syntax for the custom data types at all. DataType::Custom(..) => parser_err!("dummy", loc), + // MySQL supports using the `BINARY` keyword as a cast to binary type. + DataType::Binary(..) if self.dialect.supports_binary_kw_as_cast() => { + Ok(Expr::Cast { + kind: CastKind::Cast, + expr: Box::new(parser.parse_expr()?), + data_type: DataType::Binary(None), + format: None, + }) + } data_type => Ok(Expr::TypedString(TypedString { data_type, value: parser.parse_value()?, diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 208a56e23..84197e54b 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -18060,3 +18060,9 @@ fn test_parse_key_value_options_trailing_semicolon() { "CREATE USER u1 option1='value1' option2='value2'", ); } + +#[test] +fn test_binary_kw_as_cast() { + all_dialects_where(|d| d.supports_binary_kw_as_cast()) + .one_statement_parses_to("SELECT BINARY 1+1", "SELECT CAST(1 + 1 AS BINARY)"); +}