Skip to content

Commit

Permalink
Fix: ensure pivot can be used as a table name (#1734)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Jun 6, 2023
1 parent a30a828 commit e058513
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
16 changes: 10 additions & 6 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1912,8 +1912,16 @@ def _parse_select(

this = self._parse_query_modifiers(this)
elif (table or nested) and self._match(TokenType.L_PAREN):
this = self._parse_table() if table else self._parse_select(nested=True)
this = self._parse_set_operations(self._parse_query_modifiers(this))
if self._match(TokenType.PIVOT):
this = self._parse_simplified_pivot()
elif self._match(TokenType.FROM):
this = exp.select("*").from_(
t.cast(exp.From, self._parse_from(skip_from_token=True))
)
else:
this = self._parse_table() if table else self._parse_select(nested=True)
this = self._parse_set_operations(self._parse_query_modifiers(this))

self._match_r_paren()

# early return so that subquery unions aren't parsed again
Expand All @@ -1926,10 +1934,6 @@ def _parse_select(
expressions=self._parse_csv(self._parse_value),
alias=self._parse_table_alias(),
)
elif self._match(TokenType.PIVOT):
this = self._parse_simplified_pivot()
elif self._match(TokenType.FROM):
this = exp.select("*").from_(t.cast(exp.From, self._parse_from(skip_from_token=True)))
else:
this = None

Expand Down
1 change: 1 addition & 0 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class TestBigQuery(Validator):
dialect = "bigquery"

def test_bigquery(self):
self.validate_identity("SELECT * FROM pivot CROSS JOIN foo")
self.validate_identity("SAFE_CAST(x AS STRING)")
self.validate_identity("SELECT * FROM a-b-c.mydataset.mytable")
self.validate_identity("SELECT * FROM abc-def-ghi")
Expand Down
1 change: 1 addition & 0 deletions tests/dialects/test_presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ def test_presto(self):
self.validate_identity("START TRANSACTION ISOLATION LEVEL REPEATABLE READ")
self.validate_identity("APPROX_PERCENTILE(a, b, c, d)")

self.validate_all("VALUES 1, 2, 3", write={"presto": "VALUES (1), (2), (3)"})
self.validate_all("INTERVAL '1 day'", write={"trino": "INTERVAL '1' day"})
self.validate_all("(5 * INTERVAL '7' day)", read={"": "INTERVAL '5' week"})
self.validate_all("(5 * INTERVAL '7' day)", read={"": "INTERVAL '5' WEEKS"})
Expand Down
3 changes: 3 additions & 0 deletions tests/dialects/test_spark.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ def test_spark(self):
self.validate_identity("TRIM(TRAILING 'SL' FROM 'SSparkSQLS')")
self.validate_identity("SPLIT(str, pattern, lim)")

self.validate_all(
"SELECT * FROM ((VALUES 1))", write={"spark": "SELECT * FROM (VALUES (1))"}
)
self.validate_all(
"SELECT CAST(STRUCT('fooo') AS STRUCT<a: VARCHAR(2)>)",
write={"spark": "SELECT CAST(STRUCT('fooo') AS STRUCT<a: STRING>)"},
Expand Down

0 comments on commit e058513

Please sign in to comment.