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

Fix to #25842 - Cosmos: Translate string.Equals with StringComparison.Ordinal #25875

Merged
merged 1 commit into from
Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/EFCore.Cosmos/Query/Internal/StringMethodTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,19 @@ public StringMethodTranslator(ISqlExpressionFactory sqlExpressionFactory)
|| _stringComparisonWithComparisonTypeArgumentStatic.Equals(method))
{
var comparisonTypeArgument = arguments[^1];

if (comparisonTypeArgument is SqlConstantExpression constantComparisonTypeArgument
&& constantComparisonTypeArgument.Value is StringComparison comparisonTypeArgumentValue
&& comparisonTypeArgumentValue == StringComparison.OrdinalIgnoreCase)
&& (comparisonTypeArgumentValue == StringComparison.OrdinalIgnoreCase
|| comparisonTypeArgumentValue == StringComparison.Ordinal))
{

return _stringComparisonWithComparisonTypeArgumentInstance.Equals(method)
? TranslateSystemFunction("STRINGEQUALS", typeof(bool), instance!, arguments[0], _sqlExpressionFactory.Constant(true))
: TranslateSystemFunction("STRINGEQUALS", typeof(bool), arguments[0], arguments[1], _sqlExpressionFactory.Constant(true));
? comparisonTypeArgumentValue == StringComparison.OrdinalIgnoreCase
? TranslateSystemFunction("STRINGEQUALS", typeof(bool), instance!, arguments[0], _sqlExpressionFactory.Constant(true))
: TranslateSystemFunction("STRINGEQUALS", typeof(bool), instance!, arguments[0])
: comparisonTypeArgumentValue == StringComparison.OrdinalIgnoreCase
? TranslateSystemFunction("STRINGEQUALS", typeof(bool), arguments[0], arguments[1], _sqlExpressionFactory.Constant(true))
: TranslateSystemFunction("STRINGEQUALS", typeof(bool), arguments[0], arguments[1]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,36 @@ FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND STRINGEQUALS(c[""CustomerID""], ""alFkI"", true))");
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Case_sensitive_string_comparison_instance(bool async)
{
await AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => c.CustomerID.Equals("ALFKI", StringComparison.Ordinal)),
entryCount: 1);

AssertSql(
@"SELECT c
FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND STRINGEQUALS(c[""CustomerID""], ""ALFKI""))");
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Case_sensitive_string_comparison_static(bool async)
{
await AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => string.Equals(c.CustomerID, "ALFKI", StringComparison.Ordinal)),
entryCount: 1);

AssertSql(
@"SELECT c
FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND STRINGEQUALS(c[""CustomerID""], ""ALFKI""))");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down