Skip to content

Commit

Permalink
Fix(mysql): add support some logical operators (#1899)
Browse files Browse the repository at this point in the history
* feat: add support for && in mysql praser

* feat(mysql): add support for XOR
  • Loading branch information
brosoul authored Jul 7, 2023
1 parent a68fe63 commit 900ad7e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sqlglot/dialects/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ class Parser(parser.Parser):
TokenType.VALUES,
}

CONJUNCTION = {
**parser.Parser.CONJUNCTION,
TokenType.DAMP: exp.And,
TokenType.XOR: exp.Xor,
}

TABLE_ALIAS_TOKENS = (
parser.Parser.TABLE_ALIAS_TOKENS - parser.Parser.TABLE_INDEX_HINT_TOKENS
)
Expand Down
4 changes: 4 additions & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3517,6 +3517,10 @@ class Or(Connector):
pass


class Xor(Connector):
pass


class BitwiseAnd(Binary):
pass

Expand Down
3 changes: 3 additions & 0 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2016,6 +2016,9 @@ def add_sql(self, expression: exp.Add) -> str:
def and_sql(self, expression: exp.And) -> str:
return self.connector_sql(expression, "AND")

def xor_sql(self, expression: exp.And) -> str:
return self.connector_sql(expression, "XOR")

def connector_sql(self, expression: exp.Connector, op: str) -> str:
if not self.pretty:
return self.binary(expression, op)
Expand Down
2 changes: 2 additions & 0 deletions sqlglot/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class TokenType(AutoName):
PARAMETER = auto()
SESSION_PARAMETER = auto()
DAMP = auto()
XOR = auto()

BLOCK_START = auto()
BLOCK_END = auto()
Expand Down Expand Up @@ -590,6 +591,7 @@ class Tokenizer(metaclass=_Tokenizer):
"OFFSET": TokenType.OFFSET,
"ON": TokenType.ON,
"OR": TokenType.OR,
"XOR": TokenType.XOR,
"ORDER BY": TokenType.ORDER_BY,
"ORDINALITY": TokenType.ORDINALITY,
"OUTER": TokenType.OUTER,
Expand Down
2 changes: 2 additions & 0 deletions tests/dialects/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def test_ddl(self):
)

def test_identity(self):
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")
self.validate_identity("SELECT /*+ MERGE(dt) */ * FROM (SELECT * FROM t1) AS dt")
self.validate_identity("SELECT /*+ INDEX(t, i) */ c1 FROM t WHERE c2 = 'value'")
Expand Down

0 comments on commit 900ad7e

Please sign in to comment.