Skip to content

Commit 2d7d9a8

Browse files
yoavcloudayman-sigma
authored andcommitted
MySQL: Add support for casting using the BINARY keyword (apache#2146)
1 parent 49dfbfb commit 2d7d9a8

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

src/dialect/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,12 @@ pub trait Dialect: Debug + Any {
12421242
fn supports_double_ampersand_operator(&self) -> bool {
12431243
false
12441244
}
1245+
1246+
/// Returns true if the dialect supports casting an expression to a binary type
1247+
/// using the `BINARY <expr>` syntax.
1248+
fn supports_binary_kw_as_cast(&self) -> bool {
1249+
false
1250+
}
12451251
}
12461252

12471253
/// Operators for which precedence must be defined.

src/dialect/mysql.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ impl Dialect for MySqlDialect {
176176
fn supports_double_ampersand_operator(&self) -> bool {
177177
true
178178
}
179+
180+
/// Deprecated functionality by MySQL but still supported
181+
/// See: <https://dev.mysql.com/doc/refman/8.4/en/cast-functions.html#operator_binary>
182+
fn supports_binary_kw_as_cast(&self) -> bool {
183+
true
184+
}
179185
}
180186

181187
/// `LOCK TABLES`

src/parser/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,15 @@ impl<'a> Parser<'a> {
16451645
// an unary negation `NOT ('a' LIKE 'b')`. To solve this, we don't accept the
16461646
// `type 'string'` syntax for the custom data types at all.
16471647
DataType::Custom(..) => parser_err!("dummy", loc),
1648+
// MySQL supports using the `BINARY` keyword as a cast to binary type.
1649+
DataType::Binary(..) if self.dialect.supports_binary_kw_as_cast() => {
1650+
Ok(Expr::Cast {
1651+
kind: CastKind::Cast,
1652+
expr: Box::new(parser.parse_expr()?),
1653+
data_type: DataType::Binary(None),
1654+
format: None,
1655+
})
1656+
}
16481657
data_type => Ok(Expr::TypedString(TypedString {
16491658
data_type,
16501659
value: parser.parse_value()?,

tests/sqlparser_common.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18087,3 +18087,9 @@ fn test_parse_key_value_options_trailing_semicolon() {
1808718087
"CREATE USER u1 option1='value1' option2='value2'",
1808818088
);
1808918089
}
18090+
18091+
#[test]
18092+
fn test_binary_kw_as_cast() {
18093+
all_dialects_where(|d| d.supports_binary_kw_as_cast())
18094+
.one_statement_parses_to("SELECT BINARY 1+1", "SELECT CAST(1 + 1 AS BINARY)");
18095+
}

0 commit comments

Comments
 (0)