From e6171c8ab86b4156c3911a22005d9fc5a8279340 Mon Sep 17 00:00:00 2001 From: Yoav Cohen Date: Tue, 30 Dec 2025 15:27:59 +0100 Subject: [PATCH 1/2] MySQL: Add support for casting using the BINARY keyword --- src/dialect/mod.rs | 6 ++++++ src/dialect/mysql.rs | 6 ++++++ src/parser/mod.rs | 9 +++++++++ tests/sqlparser_common.rs | 6 ++++++ 4 files changed, 27 insertions(+) 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 64b653910..4cee5c33e 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1645,6 +1645,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 c7a1981e9..95ad9a209 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -18061,3 +18061,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)"); +} From ef2dc999b78e94f4d76e806bd7ce11a4323c2ec6 Mon Sep 17 00:00:00 2001 From: Yoav Cohen Date: Wed, 14 Jan 2026 12:17:50 +0100 Subject: [PATCH 2/2] fmt --- src/dialect/mysql.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dialect/mysql.rs b/src/dialect/mysql.rs index 355156f79..81aa9d445 100644 --- a/src/dialect/mysql.rs +++ b/src/dialect/mysql.rs @@ -176,7 +176,7 @@ 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 {