Skip to content

Commit fca63de

Browse files
committed
fix(postgres): preserve NOT LIKE quantifiers during generation
1 parent b770115 commit fca63de

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

sqlglot/generator.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3725,6 +3725,18 @@ def neg_sql(self, expression: exp.Neg) -> str:
37253725
return f"-{sep}{this_sql}"
37263726

37273727
def not_sql(self, expression: exp.Not) -> str:
3728+
this = expression.this
3729+
if (
3730+
isinstance(this, (exp.Like, exp.ILike))
3731+
and isinstance(this.expression, (exp.All, exp.Any))
3732+
and self.SUPPORTS_LIKE_QUANTIFIERS
3733+
):
3734+
op = "ILIKE" if isinstance(this, exp.ILike) else "LIKE"
3735+
quantifier = "ALL" if isinstance(this.expression, exp.All) else "ANY"
3736+
return (
3737+
f"{self.sql(this, 'this')} "
3738+
f"NOT {op} {quantifier} {self.sql(this.expression, 'this')}"
3739+
)
37283740
return f"NOT {self.sql(expression, 'this')}"
37293741

37303742
def alias_sql(self, expression: exp.Alias) -> str:

tests/dialects/test_postgres.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,20 @@ def test_postgres(self):
267267
"x !~~* 'y'",
268268
"NOT x ILIKE 'y'",
269269
)
270+
self.validate_identity("SELECT 'testa 1' NOT LIKE ALL (ARRAY['testa%', 'testb%'])")
271+
self.validate_identity("SELECT 'testa 1' NOT LIKE ANY (ARRAY['testa%', 'testb%'])")
272+
self.validate_identity("SELECT 'testa 1' NOT ILIKE ALL (ARRAY['testa%', 'testb%'])")
273+
self.validate_identity("SELECT 'testa 1' NOT ILIKE ANY (ARRAY['testa%', 'testb%'])")
274+
self.validate_identity("SELECT NOT ('testa 1' LIKE ALL (ARRAY['testa%', 'testb%']))")
275+
self.validate_identity(
276+
"SELECT NOT ('testa 1' LIKE ANY (ARRAY['testa%', 'testb%']))",
277+
"SELECT NOT ('testa 1' LIKE ANY(ARRAY['testa%', 'testb%']))",
278+
)
279+
self.validate_identity("SELECT NOT ('testa 1' ILIKE ALL (ARRAY['testa%', 'testb%']))")
280+
self.validate_identity(
281+
"SELECT NOT ('testa 1' ILIKE ANY (ARRAY['testa%', 'testb%']))",
282+
"SELECT NOT ('testa 1' ILIKE ANY(ARRAY['testa%', 'testb%']))",
283+
)
270284
self.validate_identity(
271285
"'45 days'::interval day",
272286
"CAST('45 days' AS INTERVAL DAY)",

0 commit comments

Comments
 (0)