You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What's the idea behind converting values to string (using either object.ToString() or Convert.ToString(object))? Should the resulting string be compatible with the C# or the server-side string representation of the object?
For example, a C# bool is stored in a SqlServer bit column. Should conversion to string result in "0" and "1" or "true" and "false"?
"true" or "false". C# representation. It could be achieved via conditional for bool case or for other case where it is not possible it should not try to convert to string on server. Especially in scenarios where there are value converters applied.
I don't follow. SqlServerObjectToStringTranslator translates boolValue.ToString() into "CONVERT(VARCHAR(5), boolValue)". I believe the result of this is "0" and "1", not "true" and "false". I do not see any "conditional for bool case".
You sound uncertain, and I too could be wrong (just because I don't see a "conditional for bool case" doesn't mean it's not there). Could you have someone look at the source code, confirm whether this is indeed a bug and explain what the fix should be? If it's a bug, then there's probably a missing test.
changed the title [-]Question about query translation in SqlServerObjectToStringTranslator and SqlServerConvertTranslator[/-][+]SqlServerObjectToStringTranslator converts bool value to server side incorrectly[/+]on Dec 18, 2018
@smitpatel That's the issue where I pointed out an inconsistency between dateTime.ToString() and Convert.ToString(dateTime), but it does not describe the expected string format.
Additionally, DateTimeOffset has a bigger problem, because there's no Convert.ToString(DateTimeOffset) method. You may want to split this up into three separate bugs. For now, I'm looking for guidance regarding expected string formats.
@MaxG117 - For expected string format you can read discussion here #7364
Essentially, we are not going to do anything other than default unless there is really strong demand for it.
#14193 - Will track to make all possible conversions (initiated through instance.ToString or Convert.ToString), work in default format. It is an enhancement.
This issue is bug - at the very least, the bool values needs to be stopped from going to server translation.
We are not going to look at all formatters. Any specific formatter would be high user feedback based.
If you want your custom formatter to be used server side, then you can add DbFunction in EF Core which would map to server with your specified formatter and we will translate into it during query.
For expected string format you can read discussion here #7364
Actually I think #6264 is more relevant to my question.
This issue is bug - at the very least, the bool values needs to be stopped from going to server translation
I believe it is not ideal to stop bool values from going to server translation. For example, if a bool is used in a Where clause, the client would end up fetching and processing the entire result set. I realize nobody in their right mind should be converting booleans to strings in the Where clause (or anywhere, really), but as long as conversion of boolean to string is supported, it may as well be translated on the server.
By the way, a similar fix should also go into SqlServerConvertTranslator.cs. In my provider's implementation I added typeof(Boolean) to _supportedTypes and changed the Translate method to look something like the following. I also improved on the original by removing an unnecessary CONVERT that was being added for Convert.ToString(String).
publicSqlExpressionTranslate(SqlExpressioninstance,MethodInfomethod,IReadOnlyList<SqlExpression>arguments){if(_supportedMethods.Contains(method)){if(method.Name==nameof(Convert.ToString)){if(arguments[0]?.Type==typeof(string)){// avoid unnecessarily conversion of string to stringreturnarguments[0];}elseif(arguments[0]?.Type==typeof(bool)){// Convert a bool to its string representationreturn_sqlExpressionFactory.Case(new[]{newCaseWhenClause(_sqlExpressionFactory.Equal(arguments[0],_sqlExpressionFactory.Constant(false)),_sqlExpressionFactory.Constant(false.ToString()))},_sqlExpressionFactory.Constant(true.ToString()));}}// original return here:return_sqlExpressionFactory.Function("CONVERT",new[]{_sqlExpressionFactory.Fragment(_typeMapping[method.Name]),arguments[0]},method.ReturnType);}else{returnnull;}}
Activity
smitpatel commentedon Dec 18, 2018
"true" or "false". C# representation. It could be achieved via conditional for bool case or for other case where it is not possible it should not try to convert to string on server. Especially in scenarios where there are value converters applied.
MaxG117 commentedon Dec 18, 2018
I don't follow. SqlServerObjectToStringTranslator translates
boolValue.ToString()into "CONVERT(VARCHAR(5), boolValue)". I believe the result of this is "0" and "1", not "true" and "false". I do not see any "conditional for bool case".smitpatel commentedon Dec 18, 2018
Then it's a bug.
MaxG117 commentedon Dec 18, 2018
You sound uncertain, and I too could be wrong (just because I don't see a "conditional for bool case" doesn't mean it's not there). Could you have someone look at the source code, confirm whether this is indeed a bug and explain what the fix should be? If it's a bug, then there's probably a missing test.
smitpatel commentedon Dec 18, 2018
outputs following SQL
Which of course produces incorrect results. EF Core 2.2.0
[-]Question about query translation in SqlServerObjectToStringTranslator and SqlServerConvertTranslator[/-][+]SqlServerObjectToStringTranslator converts bool value to server side incorrectly[/+]MaxG117 commentedon Dec 19, 2018
What about other types, like DateTime? Should dateTime.ToString() and Convert.ToString(dateTime) also return the C# representation of those strings?
smitpatel commentedon Dec 19, 2018
@MaxG117 - Isn't that covered in #14193 already?
MaxG117 commentedon Dec 19, 2018
@smitpatel That's the issue where I pointed out an inconsistency between dateTime.ToString() and Convert.ToString(dateTime), but it does not describe the expected string format.
Additionally, DateTimeOffset has a bigger problem, because there's no Convert.ToString(DateTimeOffset) method. You may want to split this up into three separate bugs. For now, I'm looking for guidance regarding expected string formats.
smitpatel commentedon Dec 19, 2018
@MaxG117 - For expected string format you can read discussion here #7364
Essentially, we are not going to do anything other than default unless there is really strong demand for it.
#14193 - Will track to make all possible conversions (initiated through instance.ToString or Convert.ToString), work in default format. It is an enhancement.
This issue is bug - at the very least, the bool values needs to be stopped from going to server translation.
We are not going to look at all formatters. Any specific formatter would be high user feedback based.
If you want your custom formatter to be used server side, then you can add DbFunction in EF Core which would map to server with your specified formatter and we will translate into it during query.
MaxG117 commentedon Dec 20, 2018
@smitpatel
Actually I think #6264 is more relevant to my question.
I believe it is not ideal to stop bool values from going to server translation. For example, if a bool is used in a
Whereclause, the client would end up fetching and processing the entire result set. I realize nobody in their right mind should be converting booleans to strings in theWhereclause (or anywhere, really), but as long as conversion of boolean to string is supported, it may as well be translated on the server.smitpatel commentedon Dec 21, 2018
Yes, it is not ideal. But doing client eval rather than giving incorrect result is much better. Incorrect results could cause data corruption.
MaxG117 commentedon Dec 21, 2018
Why would translating a boolean to a string on the server produce incorrect results?
By the way, your example above:
wouldn't work, because
true.ToString()returns "True" (capitalized).I'm suggesting doing everything on the server, so that this query:
produces the following SQL:
13 remaining items
MaxG117 commentedon Apr 29, 2021
By the way, a similar fix should also go into SqlServerConvertTranslator.cs. In my provider's implementation I added
typeof(Boolean)to_supportedTypesand changed theTranslatemethod to look something like the following. I also improved on the original by removing an unnecessaryCONVERTthat was being added for Convert.ToString(String).