[tsql, teradata, redshift] SELECT top FROM t still raises ParseError after #9295

Open Beginner friendly
#9,300 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
74/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
compilers

Research direction

Start in sqlglot/parser.py at _parse_limit (~line 6035), and use the Snowflake override in sqlglot/parsers/snowflake.py (~line 1239) as the model for guarding TOP. Check the tokenizers in sqlglot/dialects/tsql.py (~188), teradata.py (~74) and redshift.py (~57) to confirm TOP is an ID_VAR token. Reproduce with sqlglot.parse_one("SELECT top FROM t", read="tsql"), then add dialect tests asserting top, top.x and top, x round-trip on all three dialects while existing TOP 5, TOP (5), TOP 10 PERCENT and Redshift TOP %s forms stay unchanged; run the dialect tests and pytest tests.

Written by the indexing model from the issue text.

Description

Before you file an issue

  • read dialect: tsql, teradata, redshift
  • write dialect: same as read
  • still present on main @ 089891f2521780e2c5b243005017af9db62f25a6 (2026-11-21), i.e. after #9295 (a014c534)

Fully reproducible code snippet

#9295 fixed #9284 for Snowflake and Oracle. The generic Parser._parse_limit(top=True) still matches TOP unconditionally, so the other three dialects that tokenize it behave as before:

import sqlglot

for dialect in ("tsql", "teradata", "redshift"):
    sqlglot.parse_one("SELECT top FROM t", read=dialect)
    # ParseError: Required keyword: 'expression' missing for <class 'sqlglot.expressions.query.Limit'>

    print(dialect, sqlglot.parse_one("SELECT top, x FROM t", read=dialect).sql(dialect=dialect))
    # tsql      SELECT TOP (x) FROM t    projections: []
    # teradata  SELECT TOP (x) FROM t    projections: []
    # redshift  SELECT FROM t LIMIT x    projections: []

SELECT top.x FROM t raises the same ParseError on all three. Each of them tokenizes "TOP": TokenType.TOP (sqlglot/dialects/tsql.py:188, teradata.py:74, redshift.py:57), and Parser.ID_VAR_TOKENS includes TokenType.TOP, so top is meant to be usable as an identifier — the same argument #9284 made for Snowflake.

Where it comes from

_parse_limit (sqlglot/parser.py:6035) matches TOP, then builds exp.Limit(expression=None) when no count follows. Snowflake now has a guard in its own override (sqlglot/parsers/snowflake.py:1239) and Oracle stopped tokenizing TOP; the base parser has neither.

Retreating to the saved token index when no count is parsed covers all three, and unlike a "next token must be NUMBER" guard it leaves Redshift's TOP %s / TOP %(n)s placeholders alone:

     def _parse_limit(
         ...
     ) -> exp.Expr | None:
+        index = self._index
         if skip_limit_token or self._match(TokenType.TOP if top else TokenType.LIMIT):
             ...
                 expression = (
                     self._parse_term() or self._parse_select()
                     if limit_paren
                     else self._parse_number()
                 )
 
+                if expression is None:
+                    # TOP is also a valid identifier, e.g. SELECT top FROM t
+                    self._retreat(index)
+                    return this
+
                 if limit_paren:
                     self._match_r_paren()

Verified on 089891f: with the patch, top, top.x, top, x round-trip on tsql/teradata/redshift; every existing TOP form is unchanged (TOP 5, TOP (5), TOP (@n), TOP 10 PERCENT, WITH TIES, DISTINCT TOP 3, set operations, ORDER BY/OFFSET/FETCH, Redshift TOP %s); pytest tests gives 1666 passed, 25818 subtests passed. The three new dialect tests fail on 089891f without the patch.

Not covered here

  • SELECT top(5), x FROM t still loses the projection (SELECT TOP (5, x)). TOP (expr) is valid T-SQL and Snowflake accepts only a constant, so this needs a dialect split; #9295 already sends Snowflake's TOP(x) to function-call parsing.
  • SELECT fetch FROM t is a separate root cause (FETCH clause). Mentioned only so it is not mistaken for part of this report.

Official Documentation


Prepared with AI assistance; every snippet above was run against main @ 089891f.

Dominant language
Python
Stars
9.8k
Forks
1.4k
Avg merge
10h 52m
Merged PRs (30d)
303

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tobymao/sqlglot

All issues in tobymao/sqlglot

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.