fix(redshift): parser issue for TOP N DISTINCT - #7594
Conversation
SQLGlot Integration Test ResultsComparing:
By Dialect
Overallmain: 113210 total, 112112 passed (pass rate: 99.0%), sqlglot version: sqlglot:fix/redshift-top_n_distinct: 101037 total, 101037 passed (pass rate: 100.0%), sqlglot version: Transitions: Dialect pair changes: 0 previous results not found, 3 current results not found ✅ 42 test(s) passed |
|
@fivetran-kwoodbeck Yeah, PR description makes sense. Is it possible to put the fix in the base parser for this ? and avoid doing this post change on the AST. When we parse the I think TOP is used in redshift, t-sql, oracle, snowflake, and teradata. So, if we add the logic for this in the base parser and cover these dialects (avoid breaking anything) we can push it there. |
|
@geooo109 I didn't realize other dialects supported it, then yes, it makes sense to put it into the base parser. That will be cleaner overall, will update. |
f11597a to
2b606fe
Compare
Redshift supports
SELECT TOP N DISTINCT ...where TOP N precedesDISTINCT. The base parser checks forDISTINCTimmediately afterSELECTbefore consumingTOP NThe fix adds two overrides to
RedshiftParser. _parse_limitdetects aDISTINCTtoken immediately followingTOP N, consumes it, and temporarily stashes anexp.Distinct()on theexp.Limitnode as a carrier. Redshift's_parse_select_querythen promotes that value up toexp.Select.distinctand removes it from the limit once theexp.Selectis fully built.I don't see a more elegant way to handle it because
distinctis resolved as a local variable in the base_parse_select_querybefore_parse_limitis ever called. There's no obvious way to reach back, so the limit node acts as a handoff point between the two.