[snowflake] SELECT top FROM t raises ParseError; top is not a reserved keyword
Before you file an issue
- read dialect:
snowflake(alsotsql,oracle,teradata,redshift) - write dialect:
snowflake - still present on
main@b812ff95afa2ab7ab925f95edd5d97c4b761cec9(2026-11-19)
Fully reproducible code snippet
import sqlglot
sqlglot.parse_one("SELECT top FROM t", read="snowflake")
# ParseError: Required keyword: 'expression' missing for <class 'sqlglot.expressions.query.Limit'>.
sqlglot.parse_one("SELECT top.x FROM t", read="snowflake")
# same ParseError
sqlglot.parse_one("SELECT top, x FROM t", read="snowflake")
# no error, but the projection is silently dropped:
# 'SELECT FROM t LIMIT x'
sqlglot.parse_one("SELECT TOP 5 x FROM t", read="snowflake").sql("snowflake")
# 'SELECT x FROM t LIMIT 5' (control: unchanged)
Expected: the three top queries round-trip unchanged. TOP is not a reserved keyword in Snowflake, and sqlglot already lists TokenType.TOP in Parser.ID_VAR_TOKENS (sqlglot/parser.py:851), so top is meant to be usable as an identifier.
The same happens on tsql, oracle, teradata and redshift — the five dialects that map "TOP": TokenType.TOP. postgres, duckdb and mysql don't tokenize TOP, so they are unaffected.
Where it comes from
Parser._parse_limit(top=True) (sqlglot/parser.py:6008) matches TOP unconditionally, then builds exp.Limit(expression=None) when no number follows, which fails validation. SELECT top, x is worse: the existing comma handling reads it as LIMIT offset, count, so x becomes the limit and the whole projection list disappears without an error.
A retreat to the saved token index when no TOP count is parsed fixes both shapes: the three Snowflake tests are red on main and green with it, while every existing TOP form is unchanged (TOP 5, TOP (5), TOP @n, TOP (SELECT 1), PERCENT, WITH TIES, DISTINCT TOP 3, TOP 5 DISTINCT, set operations, subqueries, ORDER BY/OFFSET/FETCH). I have that patch locally (6 lines in _parse_limit) if you want it.
One more shape not covered by that patch
SELECT top(x) FROM t still parses as SELECT FROM t LIMIT x. Snowflake's TOP accepts only a non-negative integer constant, so top(x) should be a function call; but TOP (expr) is valid T-SQL, so covering it needs a dialect split. Mentioning it so it isn't mistaken for fixed.
Official Documentation
- Snowflake reserved keywords (
TOPis absent): https://docs.snowflake.com/en/sql-reference/reserved-keywords - Snowflake
TOP <n>: "n must be a non-negative integer constant": https://docs.snowflake.com/en/sql-reference/constructs/top_n
Prepared with AI assistance; every snippet above was run against main @ b812ff95.
Ran your exact b812ff95 source. One more fixture for the top(x) residual:
SELECT top(5) FROM t
-- snowflake output: SELECT FROM t LIMIT 5
-- projections: []
A guard that only validates the parsed count as a non-negative integer still accepts 5, so numeric-argument calls need a fixture too. Snowflake TOP 5 x and T-SQL TOP (5) x / TOP (@n) x preserved x in my four-case runnable check.
Could you share the six-line patch? I can review its retreat path against these cases.
Thanks — reproduced all three of your cases at b812ff95 with the patch applied.
This patch is not a non-negative-integer guard, so top(5) never reaches the retreat: _parse_term yields 5, expression is not None, and the line is still read as a TOP count. That is the residual listed at the end of the report, not something this patch closes. Your point stands for the numeric-guard variant of the fix.
Your modifier controls hold here too: TOP 5 x (snowflake), TOP (5) x and TOP (@n) x (tsql) all keep x.
One shape worth adding to your set — the comma handling makes the numeric call worse than the single-column case:
-- snowflake
SELECT top(5), x FROM t
-- output: SELECT FROM t LIMIT 5, x
-- projections: []
5 lands in offset and x in count, so the projection and the limit are both wrong.
The patch, 6 lines in _parse_limit against b812ff95:
@@ -6011,6 +6011,7 @@ class Parser:
top: bool = False,
skip_limit_token: bool = False,
) -> exp.Expr | None:
+ index = self._index
if skip_limit_token or self._match(TokenType.TOP if top else TokenType.LIMIT):
comments = self._prev_comments
if top:
@@ -6021,6 +6022,11 @@ class Parser:
else self._parse_number()
)
+ if expression is None:
+ # TOP is also a valid identifier, e.g. SELECT top FROM t
+ self._retreat(index)
+ return None
+
if limit_paren:
self._match_r_paren()
_retreat restores the token index, _prev and _prev_comments, so comments in front of top are not lost: SELECT /* c */ top FROM t round-trips as /* c */ SELECT top FROM t, same as the control without top.
Next: I can open a PR with this patch plus the three Snowflake regressions (top, top.x, top, x) once the maintainers want one. The top(...) shape needs a dialect split — T-SQL accepts TOP (expr), Snowflake's TOP takes an integer constant — so I'd keep it out of that PR and leave the call to them.
Thanks for sharing the cut. I ran the six-line retreat on b812ff95: top, top.x and top, x recover their projections; the inline comment survives; all three modifier controls remain unchanged. Your top(5), x example also reproduces the unchanged empty projection / LIMIT 5, x residual, matching the scope you described.
I also see georgesittas's #9295 has since merged with the separate Snowflake call handling. Thanks for the concrete patch and comma fixture.