This is an automated email from the ASF dual-hosted git repository. github-bot pushed a commit to branch gh-readonly-queue/main/pr-2146-3880a933e4c2d1e639c1b8bcb48bea34c37801fd in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git
commit 6daa46db7e595b585bed8ae35fbceefa3448e596 Author: Yoav Cohen <[email protected]> AuthorDate: Wed Jan 14 12:41:05 2026 +0100 MySQL: Add support for casting using the BINARY keyword (#2146) --- 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 9e6c1859..873108ee 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 <expr>` 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 60385c5b..81aa9d44 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: <https://dev.mysql.com/doc/refman/8.4/en/cast-functions.html#operator_binary> + fn supports_binary_kw_as_cast(&self) -> bool { + true + } } /// `LOCK TABLES` diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 64b65391..4cee5c33 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 c7a1981e..95ad9a20 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)"); +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
