Skip to content

Commit

Permalink
Fix(mysql): binary x parsing closes #2130
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Aug 31, 2023
1 parent f3fee3a commit 632ad59
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions sqlglot/dialects/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,17 @@ def _parse_set_item_names(self) -> exp.Expression:

return self.expression(exp.SetItem, this=charset, collate=collate, kind="NAMES")

def _parse_type(self) -> t.Optional[exp.Expression]:
# mysql binary is special and can work anywhere, even in order by operations
# it operates like a no paren func
if self._match(TokenType.BINARY, advance=False):
data_type = self._parse_types(check_func=True, allow_identifiers=False)

if isinstance(data_type, exp.DataType):
return self.expression(exp.Cast, this=self._parse_column(), to=data_type)

return super()._parse_type()

class Generator(generator.Generator):
LOCKING_READS_SUPPORTED = True
NULL_ORDERING_SUPPORTED = False
Expand Down
3 changes: 3 additions & 0 deletions tests/dialects/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def test_ddl(self):
)

def test_identity(self):
self.validate_identity(
"SELECT * FROM x ORDER BY BINARY a", "SELECT * FROM x ORDER BY CAST(a AS BINARY)"
)
self.validate_identity("SELECT 1 XOR 0")
self.validate_identity("SELECT 1 && 0", "SELECT 1 AND 0")
self.validate_identity("SELECT /*+ BKA(t1) NO_BKA(t2) */ * FROM t1 INNER JOIN t2")
Expand Down

0 comments on commit 632ad59

Please sign in to comment.