Repository: tobymao/sqlglot

Issue:
running simplify multiple times fails
**Fully reproducible code snippet**
Please include a fully reproducible code snippet or the input sql, dialect, and expected output.

```
>>> from sqlglot.optimizer.scope import exp
>>> from sqlglot.optimizer.simplify import simplify                                                                                 
>>> from sqlglot import parse_one
>>> ast=parse_one("SELECT a,c,b from table1 where 1 = 1;")
>>> w = ast.find(exp.Where)
>>> w2 = simplify(w)
>>> w2 = simplify(w)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lib/python3.9/site-packages/sqlglot/optimizer/simplify.py", line 109, in simplify
    expression = while_changing(expression, _simplify)
  File "lib/python3.9/site-packages/sqlglot/helper.py", line 211, in while_changing
    expression = func(expression)
  File "lib/python3.9/site-packages/sqlglot/optimizer/simplify.py", line 106, in _simplify
    expression.replace(node)
  File "lib/python3.9/site-packages/sqlglot/expressions.py", line 624, in replace
    value = parent.args[key]
KeyError: 'where'
>>> 
```

in my code i run simplify multiple times as I reduce some elements of a query. in this case, elements in the where clause.

as you see in the sample code, first time works fine but then the where clause was removed and the code fails. 
This used to work before, so I guess some check that they key exists has been removed.


Discussion:
tobymao: i think the root issue though is that the expression's parent is not None 

Patch:
diff --git a/sqlglot/expressions.py b/sqlglot/expressions.py
index 7a8fef4e..21269329 100644
--- a/sqlglot/expressions.py
+++ b/sqlglot/expressions.py
@@ -621,7 +621,7 @@ class Expression(metaclass=_Expression):
             return expression
 
         key = self.arg_key
-        value = parent.args[key]
+        value = parent.args.get(key)
 
         if isinstance(value, list):
             index = self.index
@@ -639,7 +639,7 @@ class Expression(metaclass=_Expression):
                 else:
                     value[index] = expression
                     parent._set_parent(key, expression, index=index)
-        else:
+        elif value is not None:
             if expression is None:
                 parent.args.pop(key)
             else:


Test patch:
diff --git a/tests/test_optimizer.py b/tests/test_optimizer.py
index 71c65fae..984ec22b 100644
--- a/tests/test_optimizer.py
+++ b/tests/test_optimizer.py
@@ -342,6 +342,9 @@ class TestOptimizer(unittest.TestCase):
     def test_simplify(self):
         self.check_file("simplify", simplify, set_dialect=True)
 
+        expression = parse_one("SELECT a, c, b FROM table1 WHERE 1 = 1")
+        self.assertEqual(simplify(simplify(expression.find(exp.Where))).sql(), "WHERE TRUE")
+
         expression = parse_one("TRUE AND TRUE AND TRUE")
         self.assertEqual(exp.true(), optimizer.simplify.simplify(expression))
         self.assertEqual(exp.true(), optimizer.simplify.simplify(expression.this))