Skip to content

Commit f5588d8

Browse files
committed
fix simplify connectors
1 parent c39a968 commit f5588d8

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

sqlglot/optimizer/simplify.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,9 @@ def _simplify_connectors(expression, left, right):
734734
return exp.null()
735735
if always_true(left) and always_true(right):
736736
return exp.true()
737-
if always_true(left) and right.is_type(exp.DataType.Type.BOOLEAN):
737+
if always_true(left):
738738
return right
739-
if always_true(right) and left.is_type(exp.DataType.Type.BOOLEAN):
739+
if always_true(right):
740740
return left
741741
return self._simplify_comparison(expression, left, right)
742742
elif isinstance(expression, exp.Or):
@@ -748,14 +748,18 @@ def _simplify_connectors(expression, left, right):
748748
or (always_false(left) and is_null(right))
749749
):
750750
return exp.null()
751-
if is_false(left) and right.is_type(exp.DataType.Type.BOOLEAN):
751+
if is_false(left):
752752
return right
753-
if is_false(right) and left.is_type(exp.DataType.Type.BOOLEAN):
753+
if is_false(right):
754754
return left
755755
return self._simplify_comparison(expression, left, right, or_=True)
756756

757757
if isinstance(expression, exp.Connector):
758-
return self._flat_simplify(expression, _simplify_connectors, root)
758+
expression = self._flat_simplify(expression, _simplify_connectors, root)
759+
if not isinstance(
760+
expression, (exp.Connector, exp.Boolean, exp.Null)
761+
) and not expression.is_type(exp.DataType.Type.BOOLEAN):
762+
expression = expression.and_(exp.true(), copy=False)
759763
return expression
760764

761765
@annotate_types_on_change

tests/fixtures/optimizer/simplify.sql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,28 +175,28 @@ A AND (A OR B);
175175
A AND TRUE;
176176

177177
A AND D AND E AND (B OR A);
178-
A AND D AND E AND TRUE;
178+
A AND D AND E;
179179

180180
D AND A AND E AND (B OR A);
181-
A AND D AND E AND TRUE;
181+
A AND D AND E;
182182

183183
(A OR B) AND A;
184184
A AND TRUE;
185185

186186
C AND D AND (A OR B) AND E AND F AND A;
187-
A AND C AND D AND E AND F AND TRUE;
187+
A AND C AND D AND E AND F;
188188

189189
A OR (A AND B);
190-
A OR FALSE;
190+
A AND TRUE;
191191

192192
(A AND B) OR A;
193-
A OR FALSE;
193+
A AND TRUE;
194194

195195
A AND (NOT A OR B);
196-
A AND (B OR FALSE);
196+
A AND B;
197197

198198
(NOT A OR B) AND A;
199-
A AND (B OR FALSE);
199+
A AND B;
200200

201201
A OR (NOT A AND B);
202202
A OR (B AND TRUE);

0 commit comments

Comments
 (0)