Skip to content
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

Translate ToString() over enums #33706

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Danevandy99
Copy link

@Danevandy99 Danevandy99 commented May 13, 2024

Fixes #33635 and #20604

  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

@Danevandy99 Danevandy99 changed the title Fixes #33635 - Translate ToString on enums with number store types as a CASE/WHEN expression Fixes #33635 and #20604 May 16, 2024
@Danevandy99
Copy link
Author

@roji @cincuranet This is ready to review again.

@roji roji changed the title Fixes #33635 and #20604 Translate ToString() over enums May 16, 2024
Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

var converter = instance.TypeMapping?.Converter;

if (converter is not null
&& converter.GetType().IsGenericType
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract the converter type once to a variable instead of calling GetType() repeatedly.

Can also have a single "converter is not null && converterType.IsGenericType" rather than repeating twice.

@@ -101,6 +102,33 @@ public SqlServerObjectToStringTranslator(ISqlExpressionFactory sqlExpressionFact
_sqlExpressionFactory.Constant(true.ToString()));
}

if (instance.Type.IsEnum)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it should go into the relational layer rather than be a SQL Server-specific translation, as nothing here seems SQL Server-specific (and so it would work the same way on SQLite, PostgreSQL...). We have StringMethodTranslator in relational where this would fit well.

I'd leave a comment here pointing to StringMethodTranslator (and also in SqliteObjectToStringTranslator).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing methods handled by StringMethodTranslator are those called on string objects or on the string class (such as string.IsNullOrEmpty). Would it be better to rename EnumHasFlagTranslator to EnumMethodTranslator and include this code there? This would align with the fact that we are translating the ToString method called on an enum, similar to how we translate the HasFlag method on an enum.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, sounds good!

&& converter.GetType().IsGenericType
&& converter.GetType().GetGenericTypeDefinition() == typeof(EnumToStringConverter<>))
{
return _sqlExpressionFactory.MakeUnary(ExpressionType.Convert, instance, typeof(string));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

if (methodCallExpression.Object != null
&& @object!.Type.IsNullableType()
&& methodCallExpression.Method.Name != nameof(Nullable<int>.GetValueOrDefault))
&& methodCallExpression.Method.Name != nameof(Nullable<int>.GetValueOrDefault)
&& methodCallExpression.Method.Name != nameof(Nullable<int>.ToString))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this change is needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

@Danevandy99 Danevandy99 requested a review from roji May 18, 2024 20:51
@Danevandy99 Danevandy99 force-pushed the translate-to-string-on-enums-with-number-store-types-to-case-expression branch from 475fd88 to 5333cd5 Compare May 27, 2024 21:52
@Danevandy99 Danevandy99 changed the base branch from release/8.0 to main May 27, 2024 21:53
@Danevandy99 Danevandy99 force-pushed the translate-to-string-on-enums-with-number-store-types-to-case-expression branch from 9f353a0 to 56841ce Compare May 27, 2024 22:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Translate ToString on enums with number store types as a CASE/WHEN expression
2 participants