Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,12 @@ pub trait Dialect: Debug + Any {
fn supports_quote_delimited_string(&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
}
}

/// This represents the operators for which precedence must be defined
Expand Down
6 changes: 6 additions & 0 deletions src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ impl Dialect for MySqlDialect {
fn supports_cross_join_constraint(&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`
Expand Down
9 changes: 9 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,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()?,
Expand Down
6 changes: 6 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17972,3 +17972,9 @@ fn parse_select_parenthesized_wildcard() {
assert_eq!(select2.projection.len(), 1);
assert!(matches!(select2.projection[0], SelectItem::Wildcard(_)));
}

#[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)");
}