Skip to content

Commit

Permalink
fix: Parse UNNEST as a function in base dialect (#3650)
Browse files Browse the repository at this point in the history
  • Loading branch information
VaggelisD authored Jun 13, 2024
1 parent 88c4965 commit 4c82c0d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 5 additions & 4 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,21 @@ class Parser(metaclass=_Parser):
to=exp.DataType(this=exp.DataType.Type.TEXT),
),
"GLOB": lambda args: exp.Glob(this=seq_get(args, 1), expression=seq_get(args, 0)),
"HEX": build_hex,
"JSON_EXTRACT": build_extract_json_with_path(exp.JSONExtract),
"JSON_EXTRACT_SCALAR": build_extract_json_with_path(exp.JSONExtractScalar),
"JSON_EXTRACT_PATH_TEXT": build_extract_json_with_path(exp.JSONExtractScalar),
"LIKE": build_like,
"LOG": build_logarithm,
"LOG2": lambda args: exp.Log(this=exp.Literal.number(2), expression=seq_get(args, 0)),
"LOG10": lambda args: exp.Log(this=exp.Literal.number(10), expression=seq_get(args, 0)),
"LOWER": build_lower,
"MOD": build_mod,
"TIME_TO_TIME_STR": lambda args: exp.Cast(
this=seq_get(args, 0),
to=exp.DataType(this=exp.DataType.Type.TEXT),
),
"TO_HEX": build_hex,
"TS_OR_DS_TO_DATE_STR": lambda args: exp.Substring(
this=exp.Cast(
this=seq_get(args, 0),
Expand All @@ -170,11 +173,9 @@ class Parser(metaclass=_Parser):
start=exp.Literal.number(1),
length=exp.Literal.number(10),
),
"VAR_MAP": build_var_map,
"LOWER": build_lower,
"UNNEST": lambda args: exp.Unnest(expressions=ensure_list(seq_get(args, 0))),
"UPPER": build_upper,
"HEX": build_hex,
"TO_HEX": build_hex,
"VAR_MAP": build_var_map,
}

NO_PAREN_FUNCTIONS = {
Expand Down
7 changes: 7 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ def test_structs(self):
def test_float(self):
self.assertEqual(parse_one(".2"), parse_one("0.2"))

def test_unnest(self):
unnest_sql = "UNNEST(foo)"
expr = parse_one(unnest_sql)
self.assertIsInstance(expr, exp.Unnest)
self.assertIsInstance(expr.expressions, list)
self.assertEqual(expr.sql(), unnest_sql)

def test_unnest_projection(self):
expr = parse_one("SELECT foo IN UNNEST(bla) AS bar")
self.assertIsInstance(expr.selects[0], exp.Alias)
Expand Down

0 comments on commit 4c82c0d

Please sign in to comment.