dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.78k stars 3.19k forks source link

Remove useless Convert node when the type mapping doesn't change #25597

Open roji opened 3 years ago

roji commented 3 years ago

When comparing a property with a value-converted literal/parameter, if the literal/parameter's type is a subclass of the properties, a Convert node is introduced. If both nodes end up the same type mapping, the convert should be removable.

As an example, see test Comparison_with_value_converted_subclass in GearsOfWars, where comparing an IPAddress with a ReadOnlyIPAddress generates WHERE [b].[Address] = CAST(N'127.0.0.1' AS nvarchar(45)).

Split out of #25587

ajcvickers commented 3 years ago

@roji Are we sure this is useless? SQL Server can be picky about the two sides of an expression having the same type, and the type mapping for IPAddress is nvarchar(45).

roji commented 3 years ago

@ajcvickers well, the nvarchar(45) cast is inferred from the other side of the equality operator, i.e. the Address column; I think (but I could be PG-centric here) that SQL Server should be able to infer the literal type just as well without the explicit class.

Put differently, if this explicit cast is somehow necessary, then we have a bug in the simpler case where the property is compared against an IPAddress literal, rather than against a ReadOnlyIPAddress, since in that case no cast is generated...