Skip to content

Commit c31947b

Browse files
authored
feat(postgres): Support generation of exp.CountIf (#4709)
* feat(postgres): Support generation of exp.CountIf * Switch to dialect.py impl
1 parent 23283ca commit c31947b

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

sqlglot/dialects/postgres.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
trim_sql,
3434
ts_or_ds_add_cast,
3535
strposition_sql,
36+
count_if_to_sum,
3637
)
3738
from sqlglot.generator import unsupported_args
3839
from sqlglot.helper import is_int, seq_get
@@ -621,6 +622,7 @@ class Generator(generator.Generator):
621622
exp.Levenshtein: _levenshtein_sql,
622623
exp.JSONObjectAgg: rename_func("JSON_OBJECT_AGG"),
623624
exp.JSONBObjectAgg: rename_func("JSONB_OBJECT_AGG"),
625+
exp.CountIf: count_if_to_sum,
624626
}
625627

626628
TRANSFORMS.pop(exp.CommentColumnConstraint)

tests/dialects/test_dialect.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,6 +2633,8 @@ def test_count_if(self):
26332633
"snowflake": "SELECT COUNT_IF(col % 2 = 0) FROM foo",
26342634
"sqlite": "SELECT SUM(IIF(col % 2 = 0, 1, 0)) FROM foo",
26352635
"tsql": "SELECT COUNT_IF(col % 2 = 0) FROM foo",
2636+
"postgres": "SELECT SUM(CASE WHEN col % 2 = 0 THEN 1 ELSE 0 END) FROM foo",
2637+
"redshift": "SELECT SUM(CASE WHEN col % 2 = 0 THEN 1 ELSE 0 END) FROM foo",
26362638
},
26372639
)
26382640
self.validate_all(

0 commit comments

Comments
 (0)