Skip to content

Commit

Permalink
parse all unrecognized creatables as commands fixes #1095
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Feb 5, 2023
1 parent 049caca commit 3a13fdf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 29 deletions.
25 changes: 0 additions & 25 deletions sqlglot/dialects/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,29 +211,6 @@ class Tokenizer(tokens.Tokenizer):
HEX_STRINGS = [("x'", "'"), ("X'", "'")]
BYTE_STRINGS = [("e'", "'"), ("E'", "'")]

CREATABLES = (
"AGGREGATE",
"CAST",
"CONVERSION",
"COLLATION",
"DEFAULT CONVERSION",
"CONSTRAINT",
"DOMAIN",
"EXTENSION",
"FOREIGN",
"FUNCTION",
"OPERATOR",
"POLICY",
"ROLE",
"RULE",
"SEQUENCE",
"TEXT",
"TRIGGER",
"TYPE",
"UNLOGGED",
"USER",
)

KEYWORDS = {
**tokens.Tokenizer.KEYWORDS,
"~~": TokenType.LIKE,
Expand All @@ -259,8 +236,6 @@ class Tokenizer(tokens.Tokenizer):
"TEMP": TokenType.TEMPORARY,
"UUID": TokenType.UUID,
"CSTRING": TokenType.PSEUDO_TYPE,
**{f"CREATE {kind}": TokenType.COMMAND for kind in CREATABLES},
**{f"DROP {kind}": TokenType.COMMAND for kind in CREATABLES},
}
QUOTES = ["'", "$$"]
SINGLE_TOKENS = {
Expand Down
13 changes: 9 additions & 4 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,15 +902,15 @@ def _parse_statement(self) -> t.Optional[exp.Expression]:
return expression

def _parse_drop(self, default_kind: t.Optional[str] = None) -> t.Optional[exp.Expression]:
start = self._prev
temporary = self._match(TokenType.TEMPORARY)
materialized = self._match(TokenType.MATERIALIZED)
kind = self._match_set(self.CREATABLES) and self._prev.text
if not kind:
if default_kind:
kind = default_kind
else:
self.raise_error(f"Expected {self.CREATABLES}")
return None
return self._parse_as_command(start)

return self.expression(
exp.Drop,
Expand All @@ -930,6 +930,7 @@ def _parse_exists(self, not_: bool = False) -> t.Optional[bool]:
)

def _parse_create(self) -> t.Optional[exp.Expression]:
start = self._prev
replace = self._match_pair(TokenType.OR, TokenType.REPLACE)
set_ = self._match(TokenType.SET) # Teradata
multiset = self._match_text_seq("MULTISET") # Teradata
Expand All @@ -947,8 +948,7 @@ def _parse_create(self) -> t.Optional[exp.Expression]:
create_token = self._match_set(self.CREATABLES) and self._prev

if not create_token:
self.raise_error(f"Expected {self.CREATABLES}")
return None
return self._parse_as_command(start)

exists = self._parse_exists(not_=True)
this = None
Expand Down Expand Up @@ -3504,6 +3504,11 @@ def _parse_merge(self) -> exp.Expression:
def _parse_set(self) -> exp.Expression:
return self.expression(exp.Set, expressions=self._parse_csv(self._parse_set_item))

def _parse_as_command(self, start: Token) -> exp.Command:
while self._curr:
self._advance()
return exp.Command(this=self._find_sql(start, self._prev))

def _find_parser(
self, parsers: t.Dict[str, t.Callable], trie: t.Dict
) -> t.Optional[t.Callable]:
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/identity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS my_idx ON tbl (a, b)
CREATE SCHEMA x
CREATE SCHEMA IF NOT EXISTS y
CREATE PROCEDURE IF NOT EXISTS a.b.c() AS 'DECLARE BEGIN; END'
CREATE OR REPLACE STAGE
DESCRIBE x
DROP INDEX a.b.c
DROP FUNCTION a.b.c (INT)
Expand Down

0 comments on commit 3a13fdf

Please sign in to comment.