Skip to content

Commit

Permalink
Fix!: dict conversion had incorrect ast
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Aug 14, 2023
1 parent 74fc28a commit 2e73a4f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5863,8 +5863,8 @@ def convert(value: t.Any, copy: bool = False) -> Expression:
return Array(expressions=[convert(v, copy=copy) for v in value])
if isinstance(value, dict):
return Map(
keys=[convert(k, copy=copy) for k in value],
values=[convert(v, copy=copy) for v in value.values()],
keys=Array(expressions=[convert(k, copy=copy) for k in value]),
values=Array(expressions=[convert(v, copy=copy) for v in value.values()]),
)
raise ValueError(f"Cannot convert {value}")

Expand Down
2 changes: 1 addition & 1 deletion tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def test_build(self):
),
(
lambda: exp.update("tbl", {"x": None, "y": {"x": 1}}),
"UPDATE tbl SET x = NULL, y = MAP('x', 1)",
"UPDATE tbl SET x = NULL, y = MAP(ARRAY('x'), ARRAY(1))",
),
(
lambda: exp.update("tbl", {"x": 1}, where="y > 0"),
Expand Down
7 changes: 6 additions & 1 deletion tests/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ def test_convert(self):
(True, "TRUE"),
((1, "2", None), "(1, '2', NULL)"),
([1, "2", None], "ARRAY(1, '2', NULL)"),
({"x": None}, "MAP('x', NULL)"),
({"x": None}, "MAP(ARRAY('x'), ARRAY(NULL))"),
(
datetime.datetime(2022, 10, 1, 1, 1, 1, 1),
"TIME_STR_TO_TIME('2022-10-01T01:01:01.000001+00:00')",
Expand All @@ -681,6 +681,11 @@ def test_convert(self):
with self.subTest(value):
self.assertEqual(exp.convert(value).sql(), expected)

self.assertEqual(
exp.convert({"test": "value"}).sql(dialect="spark"),
"MAP_FROM_ARRAYS(ARRAY('test'), ARRAY('value'))",
)

def test_comment_alias(self):
sql = """
SELECT
Expand Down

0 comments on commit 2e73a4f

Please sign in to comment.