Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding limit to a UNION SQL outputs incorrect sql in "tsql" dialect #4786

Open
aman-wisdom opened this issue Feb 22, 2025 · 0 comments
Open

Comments

@aman-wisdom
Copy link

aman-wisdom commented Feb 22, 2025

Description
The issue exists in the latest version of sqlglot 26.6.0. When applying limit operation on a Union node and converting to T-SQL, we get an expression which is invalid in T-SQL.

Fully reproducible code snippet

>>> parse_one("select count(*) from a union select count(*) from b", dialect="tsql").limit(5).sql(dialect="tsql")
'SELECT TOP 5 * FROM (SELECT COUNT(*) FROM a UNION SELECT COUNT(*) FROM b) AS _l_0'

While this looks correct, it throws syntax error as shown in the image below. Note that the union query works as a standalone but not when placed within parantheses. (See error message at the bottom of the image.)

Image

Official Documentation
Couldn't find the official documentation explaining this situation, but it should be reproducible anywhere I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant