Skip to content

Translations to LEAST/GREATEST, Math.Min/Max#32338

Merged
roji merged 2 commits intodotnet:mainfrom
roji:GreatestLeast
Nov 28, 2023
Merged

Translations to LEAST/GREATEST, Math.Min/Max#32338
roji merged 2 commits intodotnet:mainfrom
roji:GreatestLeast

Conversation

@roji
Copy link
Member

@roji roji commented Nov 18, 2023

This PR:

  1. Adds translations for Math.Min/Max in non-aggregate context (#27794), using the SQL Server 2022 LEAST/GREATEST functions.
  2. Adds EF.Functions.Least/Greatest at the relational level (#31681), to allow invoking them directly; this is for types which don't have Math.Min/Max overloads (dates, strings...), and also to specify arbitrary-length lists without having to nest Math.Min/Max invocations
  3. Pattern matches for Math.Min/Max over an inline primitive collection (new[] { x, y }.Max()), simplifying the translation via LEAST/GREATEST (#32332). This is done conditional on the SQL Server compatibility level.

Design notes:

  • LEAST/GREATEST require SQL Server 2022; when introducing the SQL Server compatibility level, we decided to make it 2022 by default. This means that this PR breaks users of older databases who don't have their compatibility level set explicitly (change 3 specifically). We may want to revisit the default. (revisited, changed to 2019)
  • In SQLite, EF.Functions.Least/Greatest translate to MIN/MAX, though we usually try to have EF.Functions method names correpond to the actual SQL function invoked. However, it's good to have these methods at the relational level, and all databases except for SQLite have LEAST/GREATEST.

Closes #27794
Closes #31681
Closes #32332

@roji roji requested a review from a team November 18, 2023 03:13
@roji roji force-pushed the GreatestLeast branch 4 times, most recently from e080f41 to a85f8ed Compare November 18, 2023 16:56
@roji roji removed the needs-design label Nov 27, 2023
@maumar
Copy link
Contributor

maumar commented Nov 27, 2023

@roji should we also recognize Math.Max(x, Math.Max(y, z)) and convert to GREATEST and same for min?

@roji
Copy link
Member Author

roji commented Nov 28, 2023

I love it!

@roji
Copy link
Member Author

roji commented Nov 28, 2023

@maumar pushed another commit to do this, take a look.

@roji roji merged commit d79cd19 into dotnet:main Nov 28, 2023
@roji roji deleted the GreatestLeast branch November 28, 2023 11:29
roji added a commit to roji/efcore that referenced this pull request Nov 30, 2023
roji added a commit to roji/efcore that referenced this pull request Nov 30, 2023
roji added a commit to roji/efcore that referenced this pull request Jul 22, 2024
roji added a commit to roji/efcore that referenced this pull request Jul 22, 2024
@roji
Copy link
Member Author

roji commented Jul 22, 2024

See nullability-related follow-up work in #32458

roji added a commit to roji/efcore that referenced this pull request Jul 22, 2024
roji added a commit to roji/efcore that referenced this pull request Jul 22, 2024
roji added a commit that referenced this pull request Jul 23, 2024
@roji roji changed the title Work on LEAST/GREATEST, Math.Min/Max Translations to LEAST/GREATEST, Math.Min/Max Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants