Repository: tobymao/sqlglot

Issue:
No response when SQL optimize with rule of pushdown_predicates
```python
from sqlglot.optimizer import optimize
from sqlglot.optimizer.qualify import qualify
from sqlglot.optimizer.pushdown_predicates import pushdown_predicates

sql = """
SELECT
*
FROM t0
LEFT JOIN t1 ON t0.device_id = t1.device_id AND t0.time < t1.time AND t0.time <= t1.time
"""

RULES = [
            qualify,
            pushdown_predicates,
        ]
sql = optimize(sql, dialect='trino', rules=RULES).sql(pretty=True)

print(sql)
```

There will be no response when running code above. After keyboard interrupt, it will show trackback as followed:
```
^CTraceback (most recent call last):
  File "test.py", line 16, in <module>
    sql = optimize(sql, dialect='trino', rules=RULES).sql(pretty=True)
  File "/Users/anaconda3/envs/sqlPredictEnv/lib/python3.8/site-packages/sqlglot/optimizer/optimizer.py", line 92, in optimize
    expression = rule(expression, **rule_kwargs)
  File "/Users/anaconda3/envs/sqlPredictEnv/lib/python3.8/site-packages/sqlglot/optimizer/pushdown_predicates.py", line 45, in pushdown_predicates
    pushdown(join.args.get("on"), {name: scope.selected_sources[name]}, scope_ref_count)
  File "/Users/anaconda3/envs/sqlPredictEnv/lib/python3.8/site-packages/sqlglot/optimizer/pushdown_predicates.py", line 54, in pushdown
    condition = condition.replace(simplify(condition))
  File "/Users/anaconda3/envs/sqlPredictEnv/lib/python3.8/site-packages/sqlglot/optimizer/simplify.py", line 73, in simplify
    expression = while_changing(expression, _simplify)
  File "/Users/anaconda3/envs/sqlPredictEnv/lib/python3.8/site-packages/sqlglot/helper.py", line 209, in while_changing
    for n, *_ in expression.walk():
  File "/Users/powerlee/anaconda3/envs/sqlPredictEnv/lib/python3.8/site-packages/sqlglot/expressions.py", line 399, in walk
    yield from self.bfs(prune=prune)
  File "/Users/powerlee/anaconda3/envs/sqlPredictEnv/lib/python3.8/site-packages/sqlglot/expressions.py", line 437, in bfs
    queue.append((v, item, k))
KeyboardInterrupt
```


Discussion:
Thanks for the report @powerAmore! Will take a look soon.

Resolving patch:
diff --git a/sqlglot/optimizer/simplify.py b/sqlglot/optimizer/simplify.py
--- a/sqlglot/optimizer/simplify.py
+++ b/sqlglot/optimizer/simplify.py
@@ -350,7 +350,8 @@ def absorb_and_eliminate(expression, root=True):
 def simplify_literals(expression, root=True):
     if isinstance(expression, exp.Binary) and not isinstance(expression, exp.Connector):
         return _flat_simplify(expression, _simplify_binary, root)
-    elif isinstance(expression, exp.Neg):
+
+    if isinstance(expression, exp.Neg):
         this = expression.this
         if this.is_number:
             value = this.name
@@ -645,7 +646,7 @@ def _flat_simplify(expression, simplifier, root=True):
             for b in queue:
                 result = simplifier(expression, a, b)
 
-                if result:
+                if result and result is not expression:
                     queue.remove(b)
                     queue.appendleft(result)
                     break


Test patch:
diff --git a/tests/fixtures/optimizer/simplify.sql b/tests/fixtures/optimizer/simplify.sql
--- a/tests/fixtures/optimizer/simplify.sql
+++ b/tests/fixtures/optimizer/simplify.sql
@@ -615,6 +615,9 @@ TRUE;
 x = 2018 OR x <> 2018;
 x <> 2018 OR x = 2018;
 
+t0.x = t1.x AND t0.y < t1.y AND t0.y <= t1.y;
+t0.x = t1.x AND t0.y < t1.y AND t0.y <= t1.y;
+
 --------------------------------------
 -- Coalesce
 --------------------------------------