fix(tsql): Retain limit clause in subquery expression.#5227
Merged
georgesittas merged 3 commits intotobymao:mainfrom Jun 16, 2025
MarcusRisanger:fix/tsql-subquery-limit
Merged
fix(tsql): Retain limit clause in subquery expression.#5227georgesittas merged 3 commits intotobymao:mainfrom MarcusRisanger:fix/tsql-subquery-limit
georgesittas merged 3 commits intotobymao:mainfrom
MarcusRisanger:fix/tsql-subquery-limit
Conversation
Collaborator
|
Hey @MarcusRisanger 👋 Thanks a lot for digging into this! I think we may be able to simplify this a bit. I see that the +++ b/sqlglot/dialects/tsql.py
@@ -1224,7 +1224,6 @@ class TSQL(Dialect):
# to amend the AST by moving the CTEs to the CREATE VIEW statement's query.
ctas_expression.set("with", with_.pop())
- sql = super().create_sql(expression)
table = expression.find(exp.Table)
@@ -1243,6 +1242,8 @@ class TSQL(Dialect):
select_into.limit(0, copy=False)
sql = self.sql(select_into)
+ else:
+ sql = super().create_sql(expression)
Wanna give it a shot? Feel free to update the PR and I can merge shortly. |
Contributor
Author
|
Very nice @georgesittas - this is the difference between knowing the code base and just blindly implementing change based on debugging call stacks... 🚀 |
georgesittas
approved these changes
Jun 16, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes issue: TobikoData/sqlmesh#4649
Subqueries are parsed twice (at least for tsql) by
select_sql, which causes subqueries usingLIMITorTOPto drop these on the second run, when converting CTAS statement toSELECT .. INTO ...The subquery expression object suffers a side-effect in
Generator.select_sqlmethod where anylimitexpression is popped from the expression and propagated within the method as a string component.This simple fix reinjects the original
limitexpression to any query that HAD alimitexpression prior toselect_sql, but lacks it afterward.