diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-07-24 08:03:45 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-07-24 08:03:45 +0000 |
commit | ca57dc468e5d8d0920e964d45ad25271ae6e633d (patch) | |
tree | 319d8bffcb5c3e9afe9e62beca9ef401480578d2 /sqlglot/parser.py | |
parent | Adding upstream version 17.4.1. (diff) | |
download | sqlglot-ca57dc468e5d8d0920e964d45ad25271ae6e633d.tar.xz sqlglot-ca57dc468e5d8d0920e964d45ad25271ae6e633d.zip |
Adding upstream version 17.7.0.upstream/17.7.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/parser.py')
-rw-r--r-- | sqlglot/parser.py | 64 |
1 files changed, 46 insertions, 18 deletions
diff --git a/sqlglot/parser.py b/sqlglot/parser.py index 508a273..5adec77 100644 --- a/sqlglot/parser.py +++ b/sqlglot/parser.py @@ -327,6 +327,7 @@ class Parser(metaclass=_Parser): TokenType.PRIMARY_KEY, TokenType.RANGE, TokenType.REPLACE, + TokenType.RLIKE, TokenType.ROW, TokenType.UNNEST, TokenType.VAR, @@ -338,6 +339,7 @@ class Parser(metaclass=_Parser): TokenType.TIMESTAMP, TokenType.TIMESTAMPTZ, TokenType.WINDOW, + TokenType.XOR, *TYPE_TOKENS, *SUBQUERY_PREDICATES, } @@ -505,7 +507,6 @@ class Parser(metaclass=_Parser): TokenType.DESC: lambda self: self._parse_describe(), TokenType.DESCRIBE: lambda self: self._parse_describe(), TokenType.DROP: lambda self: self._parse_drop(), - TokenType.END: lambda self: self._parse_commit_or_rollback(), TokenType.FROM: lambda self: exp.select("*").from_( t.cast(exp.From, self._parse_from(skip_from_token=True)) ), @@ -716,7 +717,7 @@ class Parser(metaclass=_Parser): FUNCTIONS_WITH_ALIASED_ARGS = {"STRUCT"} - FUNCTION_PARSERS: t.Dict[str, t.Callable] = { + FUNCTION_PARSERS = { "ANY_VALUE": lambda self: self._parse_any_value(), "CAST": lambda self: self._parse_cast(self.STRICT_CAST), "CONCAT": lambda self: self._parse_concat(), @@ -1144,6 +1145,7 @@ class Parser(metaclass=_Parser): return self.expression( exp.Drop, + comments=start.comments, exists=self._parse_exists(), this=self._parse_table(schema=True), kind=kind, @@ -1233,11 +1235,14 @@ class Parser(metaclass=_Parser): expression = self._parse_ddl_select() if create_token.token_type == TokenType.TABLE: + # exp.Properties.Location.POST_EXPRESSION + extend_props(self._parse_properties()) + indexes = [] while True: index = self._parse_index() - # exp.Properties.Location.POST_EXPRESSION and POST_INDEX + # exp.Properties.Location.POST_INDEX extend_props(self._parse_properties()) if not index: @@ -1384,7 +1389,6 @@ class Parser(metaclass=_Parser): def _parse_with_property( self, ) -> t.Optional[exp.Expression] | t.List[t.Optional[exp.Expression]]: - self._match(TokenType.WITH) if self._match(TokenType.L_PAREN, advance=False): return self._parse_wrapped_csv(self._parse_property) @@ -1781,7 +1785,17 @@ class Parser(metaclass=_Parser): return None if self._match_text_seq("SERDE"): - return self.expression(exp.RowFormatSerdeProperty, this=self._parse_string()) + this = self._parse_string() + + serde_properties = None + if self._match(TokenType.SERDE_PROPERTIES): + serde_properties = self.expression( + exp.SerdeProperties, expressions=self._parse_wrapped_csv(self._parse_property) + ) + + return self.expression( + exp.RowFormatSerdeProperty, this=this, serde_properties=serde_properties + ) self._match_text_seq("DELIMITED") @@ -2251,7 +2265,9 @@ class Parser(metaclass=_Parser): self._match_set(self.JOIN_KINDS) and self._prev, ) - def _parse_join(self, skip_join_token: bool = False) -> t.Optional[exp.Join]: + def _parse_join( + self, skip_join_token: bool = False, parse_bracket: bool = False + ) -> t.Optional[exp.Join]: if self._match(TokenType.COMMA): return self.expression(exp.Join, this=self._parse_table()) @@ -2275,7 +2291,7 @@ class Parser(metaclass=_Parser): if outer_apply: side = Token(TokenType.LEFT, "LEFT") - kwargs: t.Dict[str, t.Any] = {"this": self._parse_table()} + kwargs: t.Dict[str, t.Any] = {"this": self._parse_table(parse_bracket=parse_bracket)} if method: kwargs["method"] = method.text @@ -2411,6 +2427,7 @@ class Parser(metaclass=_Parser): schema: bool = False, joins: bool = False, alias_tokens: t.Optional[t.Collection[TokenType]] = None, + parse_bracket: bool = False, ) -> t.Optional[exp.Expression]: lateral = self._parse_lateral() if lateral: @@ -2430,7 +2447,9 @@ class Parser(metaclass=_Parser): subquery.set("pivots", self._parse_pivots()) return subquery - this: exp.Expression = self._parse_table_parts(schema=schema) + bracket = parse_bracket and self._parse_bracket(None) + bracket = self.expression(exp.Table, this=bracket) if bracket else None + this: exp.Expression = bracket or self._parse_table_parts(schema=schema) if schema: return self._parse_schema(this=this) @@ -2758,8 +2777,15 @@ class Parser(metaclass=_Parser): self, this: t.Optional[exp.Expression] = None, top: bool = False ) -> t.Optional[exp.Expression]: if self._match(TokenType.TOP if top else TokenType.LIMIT): - limit_paren = self._match(TokenType.L_PAREN) - expression = self._parse_number() if top else self._parse_term() + comments = self._prev_comments + if top: + limit_paren = self._match(TokenType.L_PAREN) + expression = self._parse_number() + + if limit_paren: + self._match_r_paren() + else: + expression = self._parse_term() if self._match(TokenType.COMMA): offset = expression @@ -2767,10 +2793,9 @@ class Parser(metaclass=_Parser): else: offset = None - limit_exp = self.expression(exp.Limit, this=this, expression=expression, offset=offset) - - if limit_paren: - self._match_r_paren() + limit_exp = self.expression( + exp.Limit, this=this, expression=expression, offset=offset, comments=comments + ) return limit_exp @@ -2803,7 +2828,7 @@ class Parser(metaclass=_Parser): if not self._match(TokenType.OFFSET): return this - count = self._parse_number() + count = self._parse_term() self._match_set((TokenType.ROW, TokenType.ROWS)) return self.expression(exp.Offset, this=this, expression=count) @@ -3320,7 +3345,7 @@ class Parser(metaclass=_Parser): else: this = self.expression(exp.Anonymous, this=this, expressions=args) - self._match_r_paren(this) + self._match(TokenType.R_PAREN, expression=this) return self._parse_window(this) def _parse_function_parameter(self) -> t.Optional[exp.Expression]: @@ -4076,7 +4101,10 @@ class Parser(metaclass=_Parser): self, this: t.Optional[exp.Expression], alias: bool = False ) -> t.Optional[exp.Expression]: if self._match_pair(TokenType.FILTER, TokenType.L_PAREN): - this = self.expression(exp.Filter, this=this, expression=self._parse_where()) + self._match(TokenType.WHERE) + this = self.expression( + exp.Filter, this=this, expression=self._parse_where(skip_where_token=True) + ) self._match_r_paren() # T-SQL allows the OVER (...) syntax after WITHIN GROUP. @@ -4351,7 +4379,7 @@ class Parser(metaclass=_Parser): self._parse_set_operations(self._parse_select(nested=True, parse_subquery_alias=False)) ) - def _parse_transaction(self) -> exp.Transaction: + def _parse_transaction(self) -> exp.Transaction | exp.Command: this = None if self._match_texts(self.TRANSACTION_KIND): this = self._prev.text |