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

fix: allow Limit() without Order() with MSSQL #1009

Merged
merged 3 commits into from
Oct 26, 2024

Conversation

CL-Jeremy
Copy link
Contributor

Implements workaround from https://stackoverflow.com/a/36156953 for all backends supporting feature.OffsetFetch from ISO/ANSI SQL:2008, which specifies ORDER BY as a requirement. This should be tested further if more backends are to be added (such as Oracle in #995 – this feature is not enable there, and there seems to be an impact noted by https://stackoverflow.com/q/48620590 and https://stackoverflow.com/q/59864964).

Closes #811

query_select.go Outdated
// Allows Limit() without Order() with MSSQL as per https://stackoverflow.com/a/36156953
if q.limit > 0 && fmter.Dialect().Features().Has(feature.OffsetFetch) && len(q.order) == 0 {
b = append(b, "0 AS _temp_sort, "...)
q.order = []schema.QueryWithArgs{schema.UnsafeIdent("_temp_sort")}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is query generation and it should not change the original SelectQuery, because someone may be reusing them.

You probably need to add another block of code to generate order without changing the SelectQuery.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that if I have a SelectQuery in a variable a and then do b := a.Limit(5), the value of a would change, and that would be a problem? Isn't that also the case without the proposed change? I cannot seem to find an analog for this case to compare to. Do you mean something like ORDER BY after LIMIT, or is there another case where there could be something appended to a select query after Limit()? An example would be much appreciated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CL-Jeremy I've fixed this myself. See the commits in this branch.

@vmihailenco vmihailenco merged commit 1a46ddc into uptrace:master Oct 26, 2024
3 of 4 checks passed
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

Successfully merging this pull request may close these issues.

Erro dialect mssql witch NEXT in the FETCH
2 participants