Skip to content

Commit

Permalink
Fix(parser): disallow no paren functions when parsing table parts (to…
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored and adrianisk committed Jun 21, 2023
1 parent 8af84e5 commit 5e2c9fb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
9 changes: 7 additions & 2 deletions sqlglot/dialects/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,14 @@ def _parse_join(self, skip_join_token: bool = False) -> t.Optional[exp.Join]:
return join

def _parse_function(
self, functions: t.Optional[t.Dict[str, t.Callable]] = None, anonymous: bool = False
self,
functions: t.Optional[t.Dict[str, t.Callable]] = None,
anonymous: bool = False,
optional_parens: bool = True,
) -> t.Optional[exp.Expression]:
func = super()._parse_function(functions, anonymous)
func = super()._parse_function(
functions=functions, anonymous=anonymous, optional_parens=optional_parens
)

if isinstance(func, exp.Anonymous):
params = self._parse_func_params(func)
Expand Down
12 changes: 8 additions & 4 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class Parser(metaclass=_Parser):
TokenType.AUTO_INCREMENT,
TokenType.BEGIN,
TokenType.CACHE,
TokenType.CASE,
TokenType.COLLATE,
TokenType.COMMAND,
TokenType.COMMENT,
Expand Down Expand Up @@ -2261,7 +2262,7 @@ def _parse_index(

def _parse_table_part(self, schema: bool = False) -> t.Optional[exp.Expression]:
return (
(not schema and self._parse_function())
(not schema and self._parse_function(optional_parens=False))
or self._parse_id_var(any_token=False)
or self._parse_string_as_identifier()
or self._parse_placeholder()
Expand Down Expand Up @@ -3137,18 +3138,21 @@ def _parse_field(
)

def _parse_function(
self, functions: t.Optional[t.Dict[str, t.Callable]] = None, anonymous: bool = False
self,
functions: t.Optional[t.Dict[str, t.Callable]] = None,
anonymous: bool = False,
optional_parens: bool = True,
) -> t.Optional[exp.Expression]:
if not self._curr:
return None

token_type = self._curr.token_type

if self._match_set(self.NO_PAREN_FUNCTION_PARSERS):
if optional_parens and self._match_set(self.NO_PAREN_FUNCTION_PARSERS):
return self.NO_PAREN_FUNCTION_PARSERS[token_type](self)

if not self._next or self._next.token_type != TokenType.L_PAREN:
if token_type in self.NO_PAREN_FUNCTIONS:
if optional_parens and token_type in self.NO_PAREN_FUNCTIONS:
self._advance()
return self.expression(self.NO_PAREN_FUNCTIONS[token_type])

Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/identity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -830,3 +830,7 @@ SELECT PERCENTILE_CONT(x, 0.5 RESPECT NULLS) OVER ()
SELECT PERCENTILE_CONT(x, 0.5 IGNORE NULLS) OVER ()
WITH my_cte AS (SELECT 'a' AS desc) SELECT desc AS description FROM my_cte
WITH my_cte AS (SELECT 'a' AS asc) SELECT asc AS description FROM my_cte
SELECT * FROM case
SELECT * FROM schema.case
SELECT * FROM current_date
SELECT * FROM schema.current_date

0 comments on commit 5e2c9fb

Please sign in to comment.