Translate ToString() over enums#33706
Conversation
|
@roji @cincuranet This is ready to review again. |
roji
left a comment
There was a problem hiding this comment.
In addition to the below comments, one more general note...
Translations where we duplicate an expression multiple times are problematic, since the expression may be arbitrarily complex. For example, if instance here is a complex subquery, that subquery would get evaluated by the database N times (once for each CASE); this is bad enough for perf that we refrain from translating in most such cases (e.g. entity equality).
At least for now, we should do the same here, i.e. perform the CASE/WHEN translation only when instance is a column or a parameter - can you please add that check?
| && converter.GetType().IsGenericType | ||
| && converter.GetType().GetGenericTypeDefinition() == typeof(EnumToStringConverter<>)) | ||
| { | ||
| return _sqlExpressionFactory.MakeUnary(ExpressionType.Convert, instance, typeof(string)); |
There was a problem hiding this comment.
Since the enum is already mapped to a string in the database, an actual SQL cast shouldn't be necessary here. However, there's still a CLR type change here, and we currently lack the ability to "change" the CLR type without a corresponding SQL expression node.
Can you please leave a TODO comment here, referencing #33733?
There was a problem hiding this comment.
Yep! I did attempt to just return the instance without the cast at one point, but ran into issues that seemed larger than the scope of this issue.
| && @object!.Type.IsNullableType() | ||
| && methodCallExpression.Method.Name != nameof(Nullable<int>.GetValueOrDefault)) | ||
| && methodCallExpression.Method.Name != nameof(Nullable<int>.GetValueOrDefault) | ||
| && methodCallExpression.Method.Name != nameof(Nullable<int>.ToString)) |
There was a problem hiding this comment.
Can you explain why this change is needed?
There was a problem hiding this comment.
Calling ToString on a nullable value type like an enum returns an empty string if the value is null, rather than null: https://learn.microsoft.com/en-us/dotnet/api/system.nullable-1.tostring?view=net-8.0 Without that check, the in-memory query returns null if the value of the nullable enum is null, rather than an empty string like C# usually does.
There was a problem hiding this comment.
Looks like that might also allow ToString to be called on a reference type, since @object!.Type.IsNullableType() returns true for non-value types and we're only comparing the method names. The new check should probably include @object!.Type.IsNullableValueType()
There was a problem hiding this comment.
See my new review comment about not returning an empty string as the default case, but rather a string representation of the integer enum value (which is what .NET does). If we do that, then the NULL should simply bubble out through the conversion, which I think is the behavior we want, right? If so, then this change shouldn't be necessary.
There was a problem hiding this comment.
Yes, the change you mentioned in your other comment seems like it should make this change unnecessary. I'll remove it and let you know if that doesn't end up working.
475fd88 to
5333cd5
Compare
9f353a0 to
56841ce
Compare
| && @object!.Type.IsNullableType() | ||
| && methodCallExpression.Method.Name != nameof(Nullable<int>.GetValueOrDefault)) | ||
| && methodCallExpression.Method.Name != nameof(Nullable<int>.GetValueOrDefault) | ||
| && methodCallExpression.Method.Name != nameof(Nullable<int>.ToString)) |
There was a problem hiding this comment.
See my new review comment about not returning an empty string as the default case, but rather a string representation of the integer enum value (which is what .NET does). If we do that, then the NULL should simply bubble out through the conversion, which I think is the behavior we want, right? If so, then this change shouldn't be necessary.
roji
left a comment
There was a problem hiding this comment.
Thanks, this is starting to get close. I'll be available in the coming time to push this to completion (I was traveling quite a lot in recent weeks).
7efbfdf to
126c618
Compare
|
@roji GitHub wasn't letting me reply to this comment (#33706 (comment)), but the behavior we want is for the result to be an empty string if we call |
roji
left a comment
There was a problem hiding this comment.
Good catch with the empty-string returning behavior of Nullable.ToString().
This is almost done - see some final comments. There are also some test failures.
| AssertSql( | ||
| """ | ||
| SELECT CASE | ||
| WHEN "g"."Rank" = 0 THEN 'None' |
There was a problem hiding this comment.
This still needs to be updated (causes test failures)
roji
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround and for the patience! LGTM.
|
@roji Of course! Thanks for all your guidance. Is there anything I need to do for this to get it into a release build (I'm assuming it'll be in one of the .NET 9 previews)? |
|
Nope, it's merged and will appear in a coming preview! |
|
Awesome, thanks! |
Fixes #33635 and #20604