Skip to content

Commit 5cff8ad

Browse files
committed
MySQL: Add support for casting using the BINARY keyword
1 parent 0cf85d3 commit 5cff8ad

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
@@ -1216,6 +1216,12 @@ pub trait Dialect: Debug + Any {
12161216
fn supports_quote_delimited_string(&self) -> bool {
12171217
false
12181218
}
1219+
1220+
/// Returns true if the dialect supports casting an expression to a binary type
1221+
/// using the `BINARY <expr>` syntax.
1222+
fn supports_binary_kw_as_cast(&self) -> bool {
1223+
false
1224+
}
12191225
}
12201226

12211227
/// This represents the 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
@@ -167,6 +167,12 @@ impl Dialect for MySqlDialect {
167167
fn supports_cross_join_constraint(&self) -> bool {
168168
true
169169
}
170+
171+
/// Deprecated functionality by MySQL but still supported
172+
/// See: <https://dev.mysql.com/doc/refman/8.4/en/cast-functions.html#operator_binary>
173+
fn supports_binary_kw_as_cast(&self) -> bool {
174+
true
175+
}
170176
}
171177

172178
/// `LOCK TABLES`

src/parser/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,15 @@ impl<'a> Parser<'a> {
16091609
// an unary negation `NOT ('a' LIKE 'b')`. To solve this, we don't accept the
16101610
// `type 'string'` syntax for the custom data types at all.
16111611
DataType::Custom(..) => parser_err!("dummy", loc),
1612+
// MySQL supports using the `BINARY` keyword as a cast to binary type.
1613+
DataType::Binary(..) if self.dialect.supports_binary_kw_as_cast() => {
1614+
Ok(Expr::Cast {
1615+
kind: CastKind::Cast,
1616+
expr: Box::new(parser.parse_expr()?),
1617+
data_type: DataType::Binary(None),
1618+
format: None,
1619+
})
1620+
}
16121621
data_type => Ok(Expr::TypedString(TypedString {
16131622
data_type,
16141623
value: parser.parse_value()?,

tests/sqlparser_common.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17972,3 +17972,9 @@ fn parse_select_parenthesized_wildcard() {
1797217972
assert_eq!(select2.projection.len(), 1);
1797317973
assert!(matches!(select2.projection[0], SelectItem::Wildcard(_)));
1797417974
}
17975+
17976+
#[test]
17977+
fn test_binary_kw_as_cast() {
17978+
all_dialects_where(|d| d.supports_binary_kw_as_cast())
17979+
.one_statement_parses_to("SELECT BINARY 1+1", "SELECT CAST(1 + 1 AS BINARY)");
17980+
}

0 commit comments

Comments
 (0)