Skip to content

Only translate static Compare methods declared on the operand type - #39491

Merged
AndriySvyryd merged 2 commits into
dotnet:mainfrom
EnesKartal:fix/comparer-compare-translation-39402
Nov 13, 2026
Merged

AndriySvyryd merged 2 commits into
dotnet:mainfrom
EnesKartal:fix/comparer-compare-translation-39402

Conversation

@EnesKartal

Copy link
Copy Markdown
Contributor
  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks): partly, see the last paragraph
  • Commit messages follow this format
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

ComparisonTranslator translated any int-returning method named Compare with two arguments of the same type into a natural-order CASE. It never checked which type declared the method, so IComparer<T>.Compare was translated as if the comparer weren't there: StringComparer.OrdinalIgnoreCase.Compare(x.Name, "abc") == 0 became x.Name = 'abc', and a reversed Comparer<int> compared in the normal direction.

The Compare branch now only matches static methods declared on the operand type (string.Compare, decimal.Compare, DateTime.Compare, ...). CosmosComparisonTranslator gets the same check.

This is a behavior change. Instance comparers, including Comparer<T>.Default as discussed on the issue, and static Compare methods on unrelated types no longer translate: in a filter or ordering the query now fails with the usual "could not be translated" error, and in a final projection it's evaluated on the client. Nullable.Compare<T> also stops translating, since it's declared on System.Nullable rather than on T. Its old translation returned NULL when an operand was null, unlike the method itself, but please say if you'd rather keep it.

Tests are in MiscellaneousTranslationsTestBase with provider overrides, and no existing baselines changed. I ran Sqlite and InMemory locally. The SQL Server and Cosmos functional tests couldn't run on my machine (only the Cosmos override guard ran), so those baselines haven't been executed. I wrote the SQL Server ones from the Sqlite output and the Cosmos ones from ToQueryString output.

Fixes #39402

- ComparisonTranslator and CosmosComparisonTranslator matched any int-returning method named Compare with two arguments of the same type, so an IComparer<T> instance was translated as natural order and its own ordering was ignored
- Queries that use a comparer now fail translation in a filter or ordering and are evaluated on the client in a final projection

Fixes dotnet#39402
Copilot AI balanced review requested due to automatic review settings November 1, 2026 20:24
@EnesKartal
EnesKartal requested a review from a team as a code owner November 1, 2026 20:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@EnesKartal

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The declaring-type restriction lacks regression coverage for unrelated static comparers such as Nullable.Compare, leaving a behavior change insufficiently verified.

1 open issue

Review effort: Lite

Nullable.Compare<T> is a static Compare method declared on an unrelated type and must remain untranslated by both the relational and Cosmos comparison translators.
Copilot AI lite review requested due to automatic review settings November 6, 2026 18:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

SQL Server and Cosmos functional tests were not run, leaving the new provider-specific baselines and translation-failure assertions unverified and requiring human or CI validation.

1 open issue

Review effort: Lite

@AndriySvyryd AndriySvyryd self-assigned this Nov 7, 2026

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit 7ebdd78 into dotnet:main Nov 13, 2026
19 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

3 participants