[tsql, teradata, redshift] SELECT top FROM t still raises ParseError after #9295
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 74/100
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 tstill 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'sTOP(x)to function-call parsing.SELECT fetch FROM tis a separate root cause (FETCHclause). Mentioned only so it is not mistaken for part of this report.
Official Documentation
- Snowflake reserved keywords (
TOPabsent): https://docs.snowflake.com/en/sql-reference/reserved-keywords - Snowflake
TOP <n>: https://docs.snowflake.com/en/sql-reference/constructs/top_n - T-SQL
TOP: https://learn.microsoft.com/en-us/sql/t-sql/queries/top-transact-sql
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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tobymao/sqlglot
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 66/100
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 65/100
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 86/100
py-econometrics/pyfixest#2072 ·
Maintainers usually reply within 1 day
-
~planning docs
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
Kromatic-Innovation/athenaeum#3084 ·
Maintainers usually reply within 1 day
-
Bosch Room Thermostat II number entities offer -500 to 3000 °C setpoint limits and a -32768 to 32767 °C outdoor temperaturePossibly taken @zigpy-review-bot claimed this today. Openbot: code audit
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
zigpy/zha-device-handlers#5769 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 Half a day Newbie friendliness 80/100
R0Y00/Xbox2Windows#254 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Kludex/python-multipart#375 ·
Maintainers usually reply within 1 day