Skip to content

Commit

Permalink
Fix(spark): handle MAP(..) without arguments correctly (#2063)
Browse files Browse the repository at this point in the history
* Fix(spark): handle MAP(..) without arguments correctly

* Add MAP() spark -> trino test
  • Loading branch information
georgesittas authored Aug 15, 2023
1 parent d92a5b7 commit 56a3d89
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
10 changes: 7 additions & 3 deletions sqlglot/dialects/spark2.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ def _create_sql(self: Hive.Generator, e: exp.Create) -> str:


def _map_sql(self: Hive.Generator, expression: exp.Map) -> str:
keys = self.sql(expression.args["keys"])
values = self.sql(expression.args["values"])
return f"MAP_FROM_ARRAYS({keys}, {values})"
keys = expression.args.get("keys")
values = expression.args.get("values")

if not keys or not values:
return "MAP()"

return f"MAP_FROM_ARRAYS({self.sql(keys)}, {self.sql(values)})"


def _parse_as_cast(to_type: str) -> t.Callable[[t.List], exp.Expression]:
Expand Down
17 changes: 17 additions & 0 deletions tests/dialects/test_spark.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,23 @@ def test_spark(self):
"SELECT STR_TO_MAP('a:1,b:2,c:3', ',', ':')",
)

self.validate_all(
"MAP(1, 2, 3, 4)",
write={
"spark": "MAP(1, 2, 3, 4)",
"trino": "MAP(ARRAY[1, 3], ARRAY[2, 4])",
},
)
self.validate_all(
"MAP()",
read={
"spark": "MAP()",
"trino": "MAP()",
},
write={
"trino": "MAP(ARRAY[], ARRAY[])",
},
)
self.validate_all(
"SELECT STR_TO_MAP('a:1,b:2,c:3', ',', ':')",
read={
Expand Down

0 comments on commit 56a3d89

Please sign in to comment.