Skip to content

Commit 5f6791b

Browse files
Michael LeeMichael Lee
authored andcommitted
address PR comments
1 parent f3bcce7 commit 5f6791b

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

sqlglot/dialects/databricks.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
from sqlglot import exp, transforms, jsonpath, parser
77
from sqlglot.dialects.dialect import (
8-
array_append_sql,
9-
build_array_append_with_null_propagation,
108
date_delta_sql,
119
build_date_delta,
1210
timestamptrunc_sql,
@@ -57,7 +55,6 @@ class Parser(Spark.Parser):
5755

5856
FUNCTIONS = {
5957
**Spark.Parser.FUNCTIONS,
60-
"ARRAY_APPEND": build_array_append_with_null_propagation,
6158
"GETDATE": exp.CurrentTimestamp.from_arg_list,
6259
"DATEADD": build_date_delta(exp.DateAdd),
6360
"DATE_ADD": build_date_delta(exp.DateAdd),
@@ -105,7 +102,6 @@ class Generator(Spark.Generator):
105102

106103
TRANSFORMS = {
107104
**Spark.Generator.TRANSFORMS,
108-
exp.ArrayAppend: array_append_sql,
109105
exp.DateAdd: date_delta_sql("DATEADD"),
110106
exp.DateDiff: date_delta_sql("DATEDIFF"),
111107
exp.DatetimeAdd: lambda self, e: self.func(

sqlglot/dialects/dialect.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,22 +1356,41 @@ def build_array_append_with_null_propagation(args: t.List) -> exp.ArrayAppend:
13561356
)
13571357

13581358

1359-
def array_append_sql(self: Generator, expression: exp.ArrayAppend) -> str:
1359+
def array_append_with_null_propagation_sql(self: Generator, expression: exp.ArrayAppend) -> str:
13601360
"""
13611361
Transpile ARRAY_APPEND to dialects that propagate NULL values by default.
13621362
When transpiling from a dialect that does not propagate NULLs like DuckDB/Postgres,
13631363
explicitly handle the NULL case using COALESCE.
13641364
"""
1365-
if expression.args.get("null_propagation"):
1366-
return self.func("ARRAY_APPEND", expression.this, expression.expression)
1365+
this = expression.this
1366+
if not expression.args.get("null_propagation"):
1367+
this = exp.Coalesce(expressions=[expression.this, exp.Array(expressions=[])])
13671368

13681369
return self.func(
13691370
"ARRAY_APPEND",
1370-
exp.Coalesce(expressions=[expression.this, exp.Array(expressions=[])]),
1371+
this,
13711372
expression.expression,
13721373
)
13731374

13741375

1376+
def array_append_without_null_propagation_sql(self: Generator, expression: exp.ArrayAppend) -> str:
1377+
"""
1378+
Transpile ARRAY_APPEND to dialects that do not propagate NULL values by default.
1379+
When transpiling from a dialect that propagates NULLs like Databricks/Spark/Snowflake,
1380+
explicitly handle the NULL case using CASE.
1381+
"""
1382+
func_sql = self.func("LIST_APPEND", expression.this, expression.expression)
1383+
1384+
if not expression.args.get("null_propagation"):
1385+
return func_sql
1386+
1387+
if_expr = exp.If(
1388+
this=exp.Is(this=expression.this, expression=exp.Null()), true=exp.Null(), false=func_sql
1389+
)
1390+
1391+
return self.sql(if_expr)
1392+
1393+
13751394
def var_map_sql(
13761395
self: Generator, expression: exp.Map | exp.VarMap, map_func_name: str = "MAP"
13771396
) -> str:

sqlglot/dialects/snowflake.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from sqlglot.dialects.dialect import (
77
Dialect,
88
NormalizationStrategy,
9-
array_append_sql,
9+
array_append_with_null_propagation_sql,
1010
build_array_append_with_null_propagation,
1111
build_timetostr_or_tochar,
1212
build_like,
@@ -1497,7 +1497,7 @@ class Generator(generator.Generator):
14971497
exp.ArgMin: rename_func("MIN_BY"),
14981498
exp.Array: transforms.preprocess([transforms.inherit_struct_field_names]),
14991499
exp.ArrayConcat: lambda self, e: self.arrayconcat_sql(e, name="ARRAY_CAT"),
1500-
exp.ArrayAppend: array_append_sql,
1500+
exp.ArrayAppend: array_append_with_null_propagation_sql,
15011501
exp.ArrayContains: lambda self, e: self.func(
15021502
"ARRAY_CONTAINS",
15031503
e.expression

sqlglot/dialects/spark.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from sqlglot import exp
66
from sqlglot.dialects.dialect import (
7-
array_append_sql,
7+
array_append_with_null_propagation_sql,
88
build_array_append_with_null_propagation,
99
rename_func,
1010
build_like,
@@ -208,7 +208,7 @@ class Generator(Spark2.Generator):
208208
exp.ArrayConstructCompact: lambda self, e: self.func(
209209
"ARRAY_COMPACT", self.func("ARRAY", *e.expressions)
210210
),
211-
exp.ArrayAppend: array_append_sql,
211+
exp.ArrayAppend: array_append_with_null_propagation_sql,
212212
exp.BitwiseAndAgg: rename_func("BIT_AND"),
213213
exp.BitwiseOrAgg: rename_func("BIT_OR"),
214214
exp.BitwiseXorAgg: rename_func("BIT_XOR"),

0 commit comments

Comments
 (0)