Skip to content

Commit 4bc6857

Browse files
feat(snowflake)!: Transpilation support for STRIP_NULL_VALUE transpilation (tobymao#7403)
* feat(snowflake)!: Transpilation support for STRIP_NULL_VALUE transpilation. * feat(snowflake)!: Transpilation support for STRIP_NULL_VALUE transpilation.
1 parent 3f4e92e commit 4bc6857

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

sqlglot/expressions/json.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ class JSONStripNulls(Expression, Func):
194194
_sql_names = ["JSON_STRIP_NULLS"]
195195

196196

197+
class StripNullValue(Expression, Func):
198+
pass
199+
200+
197201
class JSONTable(Expression, Func):
198202
arg_types = {
199203
"this": True,

sqlglot/generators/duckdb.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,6 +2551,13 @@ def parsejson_sql(self, expression: exp.ParseJSON) -> str:
25512551
)
25522552
return self.func("JSON", arg)
25532553

2554+
def stripnullvalue_sql(self, expression: exp.StripNullValue) -> str:
2555+
return self.sql(
2556+
exp.case()
2557+
.when(exp.func("json_type", expression.this).eq("NULL"), exp.null())
2558+
.else_(expression.this)
2559+
)
2560+
25542561
@unsupported_args("decimals")
25552562
def trunc_sql(self, expression: exp.Trunc) -> str:
25562563
return self.func("TRUNC", expression.this)

tests/dialects/test_duckdb.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,18 @@ def test_duckdb(self):
327327
"snowflake": """SELECT GET_PATH(GET_PATH(PARSE_JSON('{"fruit": {"foo": "banana"}}'), 'fruit'), 'foo')""",
328328
},
329329
)
330+
self.validate_all(
331+
"CASE WHEN JSON_TYPE(x) = 'NULL' THEN NULL ELSE x END",
332+
read={
333+
"snowflake": "STRIP_NULL_VALUE(x)",
334+
},
335+
)
336+
self.validate_all(
337+
"""SELECT CASE WHEN JSON_TYPE(JSON('{"a": null}') -> '$.a') = 'NULL' THEN NULL ELSE JSON('{"a": null}') -> '$.a' END""",
338+
read={
339+
"snowflake": """SELECT STRIP_NULL_VALUE(GET_PATH(PARSE_JSON('{"a": null}'), 'a'))""",
340+
},
341+
)
330342
self.validate_all(
331343
"SELECT {'bla': column1, 'foo': column2, 'bar': column3} AS data FROM source_table",
332344
read={

0 commit comments

Comments
 (0)