Skip to content

Order by concurrency_key before distinct #539

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

Merged
merged 1 commit into from
Mar 24, 2025

Conversation

andyundso
Copy link
Contributor

Microsoft SQL Server requires a deterministic order when using limit (aka you have to provide ORDER BY). By default, in the MSSQL adapter, we inject a ORDER BY [primary_key] clause to achieve so, if not any order has been specified in the query.

This now leads to an issue in Solid Queue, where concurrency_key is selected and MSSQL complains that id is not in its SELECT clause.

I first looked into fixing this in the SQL adapter, but then found a test in activerecord itself (test_pluck_and_distinct) that orders first before calling distinct. So I would suggest to align Solid Queue here and order by concurrency_key prior to calling distinct.

I am aware that the existing code works with SQlite, MySQL and PostgreSQL as these do not require to pass an ORDER BY clause with LIMIT. But I don't think this small addition will cause any troubles on the other DBMS systems.

Microsoft SQL Server requires a deterministic order when using `limit` (aka you have to provide `ORDER BY`). By default, in the MSSQL adapter, we inject a `ORDER BY [primary_key]` clause to achieve so, if not any `order` has been specified in the query.

This now leads to an issue in Solid Queue, where `concurrency_key` is selected and MSSQL complains that `id` is not in its `SELECT` clause.

I first looked into fixing this in the SQL adapter, but then found a test in `activerecord` itself (`test_pluck_and_distinct`) that orders first before calling `distinct`. So I would suggest to align Solid Queue here and order by `concurrency_key` prior to calling `distinct`.

I am aware that the existing code works with SQlite, MySQL and PostgreSQL as these do not require to pass an `ORDER BY` clause with `LIMIT`. I don't think this small addition will cause any troubles on the other DBMS systems.
@rosa
Copy link
Member

rosa commented Mar 24, 2025

Aha, good catch! Yes, I don't think this will cause any trouble with the other DBMS. Thank you!

@rosa rosa merged commit b022dae into rails:main Mar 24, 2025
34 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.

2 participants