-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
SQL Server: Translate Math.Max/Min in non-aggregate context #27794
Comments
I though TSQL did have an Min and Max operation, but are Aggregate functions that can be used in normal queries in a special way. |
Oh yes, I meant that no scalar version of that function exists |
SQL Server is introducing the GREATEST and LEAST functions, which are standard across relational databases (see announcements). These are already available on Azure SQL, and will apparently be available in the next version of on-premise SQL Server. So while they aren't generally available (except on Azure), I'm not sure it makes sense to introduce the CASE/WHEN translation proposed above (we'll probably want to add GREATEST/LEAST translations when they're available). Note that it should be easy to do the above mapping with user-defined function mapping. |
@roji I hadn't heard about this, that's great! Yes, my proposal more of a hack, it makes sense if you'd want to wait for those to come avaliable instead |
Wait so SQL Server 2022 is a thing now? |
It will certainly be a thing at some point... |
Am I missing some deeper logic? The code below would do just that 'out of the box' in ef.core.
Ok, I'll give you that 'Math.Max(Column1, Column2)' is a little bit shorter, but not by that much. |
@MKatGH you're correct, it would just be for convenience purposes. string.IsNullOrEmpty for example is also trivial to express out of the box, but there is a translator for that. I don't really have a strong opinion for or against it, it's just a mere suggestion |
Note also #31681 which is for translating to GREATEST/LEAST. |
Closes dotnet#27794 Closes dotnet#31681 Closes dotnet#32332
Closes dotnet#27794 Closes dotnet#31681 Closes dotnet#32332
Closes dotnet#27794 Closes dotnet#31681 Closes dotnet#32332
Closes dotnet#27794 Closes dotnet#31681 Closes dotnet#32332
Closes dotnet#27794 Closes dotnet#31681 Closes dotnet#32332
Closes dotnet#27794 Closes dotnet#31681 Closes dotnet#32332
Today Math.Max / Math.Min is not being translated for SQL Server - only for sqlite as introduced in #10533
Transact-SQL does not have an MIN or MAX function, so there's no 1-to-1 mapping, but it is however possible to translate it into a
CASE WHEN
statement.For example
is equivalent to
CASE WHEN Column1 >= Column2 THEN Column1 ELSE Column2 END
As a suggestion, I made a reference implementation by adding the following translation to
SqlServerMathTranslator.cs
For Math.Max it looks like this:
and Math.Min:
Link to my branch of the example
The text was updated successfully, but these errors were encountered: