efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Query: optimize CASE statement - remove conditions that are never hit

Open maumar opened this issue 6 years ago • 2 comments
trafficstars

CASE
        WHEN CAST(0 AS bit) THEN x
        ELSE y
    END

we can simply remove the WHEN block since it's condition is never met so the result would be just y

If all the WHEN blocks have been removed and there is no ELSE condition we can return NULL

maumar avatar Nov 15 '19 20:11 maumar

@maumar - Is this already done?

smitpatel avatar Mar 16 '20 13:03 smitpatel

Yes, this should already be optimized by https://github.com/dotnet/efcore/blob/ecd6104d7db803a976116a49d5855bce85634051/src/EFCore.Relational/Query/SqlNullabilityProcessor.cs#L525-L532

EDIT: clauses whose test is NULL instead of FALSE are currently left in; I'm working on it.

ranma42 avatar Jun 23 '24 15:06 ranma42