Skip to content

Commit

Permalink
Fix: replace between before normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed May 23, 2023
1 parent ea130b4 commit 903dde0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 4 additions & 3 deletions sqlglot/optimizer/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sqlglot.errors import OptimizeError
from sqlglot.generator import cached_generator
from sqlglot.helper import while_changing
from sqlglot.optimizer.simplify import flatten, uniq_sort
from sqlglot.optimizer.simplify import flatten, rewrite_between, uniq_sort

logger = logging.getLogger("sqlglot")

Expand Down Expand Up @@ -34,7 +34,10 @@ def normalize(expression: exp.Expression, dnf: bool = False, max_distance: int =
if isinstance(node, exp.Connector):
if normalized(node, dnf=dnf):
continue
root = node is expression
original = node.copy()

node.transform(rewrite_between, copy=False)
distance = normalization_distance(node, dnf=dnf)

if distance > max_distance:
Expand All @@ -43,8 +46,6 @@ def normalize(expression: exp.Expression, dnf: bool = False, max_distance: int =
)
return expression

root = node is expression
original = node.copy()
try:
node = node.replace(
while_changing(node, lambda e: distributive_law(e, dnf, max_distance, generate))
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/optimizer/normalize.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ A OR ((((B OR C) AND (B OR D)) OR C) AND (((B OR C) AND (B OR D)) OR D));

SELECT * FROM x WHERE (A AND B) OR C;
SELECT * FROM x WHERE (A OR C) AND (B OR C);

dt2 between '2022-01-01 12:00:00' and '2022-12-31' and dt2 >= '2022-05-01 12:00:00' or dt2 = '2021-06-01 12:00:00';
(dt2 <= '2022-12-31' OR dt2 = '2021-06-01 12:00:00') AND (dt2 = '2021-06-01 12:00:00' OR dt2 >= '2022-01-01 12:00:00') AND (dt2 = '2021-06-01 12:00:00' OR dt2 >= '2022-05-01 12:00:00')

0 comments on commit 903dde0

Please sign in to comment.