question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SQL Server: Translate Math.Max and Math.Min

See original GitHub issue

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

Math.Max(Column1, Column2)

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:

var left = arguments[0];
var right = arguments[1];

var typeMapping = ExpressionExtensions.InferTypeMapping(left, right);
left = _sqlExpressionFactory.ApplyTypeMapping(left, typeMapping);
right = _sqlExpressionFactory.ApplyTypeMapping(right, typeMapping);

return _sqlExpressionFactory.Case(
    new[] { new CaseWhenClause(_sqlExpressionFactory.GreaterThanOrEqual(left, right), left) },
    right);

and Math.Min:

// same type mapping...
return _sqlExpressionFactory.Case(
    new[] { new CaseWhenClause(_sqlExpressionFactory.LessThanOrEqual(left, right), left) },
    right);

Link to my branch of the example

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
rojicommented, Apr 11, 2022

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.

0reactions
yesmeycommented, Jul 29, 2022

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a Max function in SQL Server that takes two values ...
It's easy to swap out the "MAX" with "MIN", "AVG", or "SUM". You can use any aggregate function to find the aggregate over...
Read more >
Working with the SQL MIN function in SQL Server
The SQL MIN function is an aggregate function that is used to find the minimum values in columns or rows in a table....
Read more >
Mathematical Functions (Transact-SQL) - SQL Server
Arithmetic functions, such as ABS, CEILING, DEGREES, FLOOR, POWER, RADIANS, and SIGN, return a value having the same data type as the input ......
Read more >
SQL Server MAX(): Get Maximum Value in a Column
The MAX() function is an aggregate function that returns the maximum value in the column or expression.
Read more >
SQL MIN() and MAX() Functions
The MAX() function returns the largest value of the selected column. MIN() Syntax. SELECT MIN(column_name) FROM table_name. WHERE condition;. MAX() Syntax.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found