diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-09-07 11:39:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-09-07 11:39:48 +0000 |
commit | f73e9af131151f1e058446361c35b05c4c90bf10 (patch) | |
tree | ed425b89f12d3f5e4709290bdc03d876f365bc97 /sqlglot/optimizer/simplify.py | |
parent | Releasing debian version 17.12.0-1. (diff) | |
download | sqlglot-f73e9af131151f1e058446361c35b05c4c90bf10.tar.xz sqlglot-f73e9af131151f1e058446361c35b05c4c90bf10.zip |
Merging upstream version 18.2.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/optimizer/simplify.py')
-rw-r--r-- | sqlglot/optimizer/simplify.py | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/sqlglot/optimizer/simplify.py b/sqlglot/optimizer/simplify.py index e550603..3974ea4 100644 --- a/sqlglot/optimizer/simplify.py +++ b/sqlglot/optimizer/simplify.py @@ -69,10 +69,10 @@ def simplify(expression): node = flatten(node) node = simplify_connectors(node, root) node = remove_compliments(node, root) + node = simplify_coalesce(node) node.parent = expression.parent node = simplify_literals(node, root) node = simplify_parens(node) - node = simplify_coalesce(node) if root: expression.replace(node) @@ -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 @@ -430,13 +431,14 @@ def simplify_parens(expression): if not isinstance(this, exp.Select) and ( not isinstance(parent, (exp.Condition, exp.Binary)) - or isinstance(this, exp.Predicate) + or isinstance(parent, exp.Paren) or not isinstance(this, exp.Binary) + or (isinstance(this, exp.Predicate) and not isinstance(parent, exp.Predicate)) or (isinstance(this, exp.Add) and isinstance(parent, exp.Add)) or (isinstance(this, exp.Mul) and isinstance(parent, exp.Mul)) or (isinstance(this, exp.Mul) and isinstance(parent, (exp.Add, exp.Sub))) ): - return expression.this + return this return expression @@ -488,18 +490,20 @@ def simplify_coalesce(expression): coalesce = coalesce if coalesce.expressions else coalesce.this # This expression is more complex than when we started, but it will get simplified further - return exp.or_( - exp.and_( - coalesce.is_(exp.null()).not_(copy=False), - expression.copy(), - copy=False, - ), - exp.and_( - coalesce.is_(exp.null()), - type(expression)(this=arg.copy(), expression=other.copy()), + return exp.paren( + exp.or_( + exp.and_( + coalesce.is_(exp.null()).not_(copy=False), + expression.copy(), + copy=False, + ), + exp.and_( + coalesce.is_(exp.null()), + type(expression)(this=arg.copy(), expression=other.copy()), + copy=False, + ), copy=False, - ), - copy=False, + ) ) @@ -642,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 |