We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f7e4e4a commit d37a5bbCopy full SHA for d37a5bb
sqlglot/dialects/snowflake.py
@@ -93,7 +93,9 @@ def _builder(args: t.List) -> E:
93
94
# https://docs.snowflake.com/en/sql-reference/functions/div0
95
def _build_if_from_div0(args: t.List) -> exp.If:
96
- cond = exp.EQ(this=seq_get(args, 1), expression=exp.Literal.number(0))
+ cond = exp.EQ(this=seq_get(args, 1), expression=exp.Literal.number(0)).and_(
97
+ exp.Is(this=seq_get(args, 0), expression=exp.null()).not_()
98
+ )
99
true = exp.Literal.number(0)
100
false = exp.Div(this=seq_get(args, 0), expression=seq_get(args, 1))
101
return exp.If(this=cond, true=true, false=false)
tests/dialects/test_snowflake.py
@@ -598,12 +598,12 @@ def test_snowflake(self):
598
self.validate_all(
599
"DIV0(foo, bar)",
600
write={
601
- "snowflake": "IFF(bar = 0, 0, foo / bar)",
602
- "sqlite": "IIF(bar = 0, 0, CAST(foo AS REAL) / bar)",
603
- "presto": "IF(bar = 0, 0, CAST(foo AS DOUBLE) / bar)",
604
- "spark": "IF(bar = 0, 0, foo / bar)",
605
- "hive": "IF(bar = 0, 0, foo / bar)",
606
- "duckdb": "CASE WHEN bar = 0 THEN 0 ELSE foo / bar END",
+ "snowflake": "IFF(bar = 0 AND NOT foo IS NULL, 0, foo / bar)",
+ "sqlite": "IIF(bar = 0 AND NOT foo IS NULL, 0, CAST(foo AS REAL) / bar)",
+ "presto": "IF(bar = 0 AND NOT foo IS NULL, 0, CAST(foo AS DOUBLE) / bar)",
+ "spark": "IF(bar = 0 AND NOT foo IS NULL, 0, foo / bar)",
+ "hive": "IF(bar = 0 AND NOT foo IS NULL, 0, foo / bar)",
+ "duckdb": "CASE WHEN bar = 0 AND NOT foo IS NULL THEN 0 ELSE foo / bar END",
607
},
608
)
609
0 commit comments