Skip to content

Commit beede9a

Browse files
committed
fix(optimizer)!: simplify AND with static BOOLEAN type
1 parent 4e36f9d commit beede9a

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

sqlglot/optimizer/simplify.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,9 @@ def _simplify_connectors(expression, left, right):
278278
return exp.null()
279279
if always_true(left) and always_true(right):
280280
return exp.true()
281-
if always_true(left):
281+
if is_true(left):
282282
return right
283-
if always_true(right):
283+
if is_true(right):
284284
return left
285285
return _simplify_comparison(expression, left, right)
286286
elif isinstance(expression, exp.Or):
@@ -1128,7 +1128,7 @@ def remove_where_true(expression):
11281128

11291129

11301130
def always_true(expression):
1131-
return (isinstance(expression, exp.Boolean) and expression.this) or (
1131+
return is_true(expression) or (
11321132
isinstance(expression, exp.Literal) and expression.is_number and not is_zero(expression)
11331133
)
11341134

@@ -1149,6 +1149,10 @@ def is_false(a: exp.Expression) -> bool:
11491149
return type(a) is exp.Boolean and not a.this
11501150

11511151

1152+
def is_true(a: exp.Expression):
1153+
return type(a) is exp.Boolean and a.this
1154+
1155+
11521156
def is_null(a: exp.Expression) -> bool:
11531157
return type(a) is exp.Null
11541158

tests/fixtures/optimizer/simplify.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ COALESCE(x, y) <> ALL (SELECT z FROM w);
153153
SELECT NOT (2 <> ALL (SELECT 2 UNION ALL SELECT 3));
154154
SELECT 2 = ANY(SELECT 2 UNION ALL SELECT 3);
155155

156+
x AND TRUE;
157+
x;
158+
159+
TRUE AND x;
160+
x;
161+
156162
--------------------------------------
157163
-- Absorption
158164
--------------------------------------

0 commit comments

Comments
 (0)