diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 16:13:01 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 16:13:01 +0000 |
commit | a7044b672667f2a0b48bd0b326b5a55b0815ef79 (patch) | |
tree | 4fb5238d47fb4709d47f766a74b8bbaa9c6f17d8 /sqlglot/optimizer/simplify.py | |
parent | Releasing debian version 23.12.1-1. (diff) | |
download | sqlglot-a7044b672667f2a0b48bd0b326b5a55b0815ef79.tar.xz sqlglot-a7044b672667f2a0b48bd0b326b5a55b0815ef79.zip |
Merging upstream version 23.13.1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/optimizer/simplify.py')
-rw-r--r-- | sqlglot/optimizer/simplify.py | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/sqlglot/optimizer/simplify.py b/sqlglot/optimizer/simplify.py index 9910572..e2051cf 100644 --- a/sqlglot/optimizer/simplify.py +++ b/sqlglot/optimizer/simplify.py @@ -224,6 +224,8 @@ def flatten(expression): def simplify_connectors(expression, root=True): def _simplify_connectors(expression, left, right): if left == right: + if isinstance(expression, exp.Xor): + return exp.false() return left if isinstance(expression, exp.And): if is_false(left) or is_false(right): @@ -365,10 +367,17 @@ def uniq_sort(expression, root=True): C AND A AND B AND B -> A AND B AND C """ if isinstance(expression, exp.Connector) and (root or not expression.same_parent): - result_func = exp.and_ if isinstance(expression, exp.And) else exp.or_ flattened = tuple(expression.flatten()) - deduped = {gen(e): e for e in flattened} - arr = tuple(deduped.items()) + + if isinstance(expression, exp.Xor): + result_func = exp.xor + # Do not deduplicate XOR as A XOR A != A if A == True + deduped = None + arr = tuple((gen(e), e) for e in flattened) + else: + result_func = exp.and_ if isinstance(expression, exp.And) else exp.or_ + deduped = {gen(e): e for e in flattened} + arr = tuple(deduped.items()) # check if the operands are already sorted, if not sort them # A AND C AND B -> A AND B AND C @@ -378,7 +387,7 @@ def uniq_sort(expression, root=True): break else: # we didn't have to sort but maybe we need to dedup - if len(deduped) < len(flattened): + if deduped and len(deduped) < len(flattened): expression = result_func(*deduped.values(), copy=False) return expression |