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

Use right hand type mapping for more binary expressions when left is an object #34729

Merged
merged 2 commits into from
Oct 21, 2024
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
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Query/SqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private SqlExpression ApplyTypeMappingOnSqlBinary(
case ExpressionType.ExclusiveOr:
{
inferredTypeMapping = typeMapping ?? ExpressionExtensions.InferTypeMapping(left, right);
resultType = inferredTypeMapping?.ClrType ?? left.Type;
resultType = inferredTypeMapping?.ClrType ?? (left.Type != typeof(object) ? left.Type : right.Type);
resultTypeMapping = inferredTypeMapping;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5289,6 +5289,34 @@ public virtual async Task ToPageAsync_in_subquery_throws()

#endregion ToPageAsync

public override async Task Ternary_Not_Null_Contains(bool async)
{
await AssertTranslationFailed(() => base.Ternary_Not_Null_Contains(async));

AssertSql();
}

public override async Task Ternary_Not_Null_endsWith_Non_Numeric_First_Part(bool async)
{
await AssertTranslationFailed(() => base.Ternary_Not_Null_endsWith_Non_Numeric_First_Part(async));

AssertSql();
}

public override async Task Ternary_Null_Equals_Non_Numeric_First_Part(bool async)
{
await AssertTranslationFailed(() => base.Ternary_Null_Equals_Non_Numeric_First_Part(async));

AssertSql();
}

public override async Task Ternary_Null_StartsWith(bool async)
{
await AssertTranslationFailed(() => base.Ternary_Null_StartsWith(async));

AssertSql();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5858,4 +5858,36 @@ public virtual Task Where_nanosecond_and_microsecond_component(bool async)
// TODO: this is basically just about translation, we don't have data with nanoseconds and microseconds
ss => ss.Set<Order>().Where(o => o.OrderDate.Value.Nanosecond != 0 && o.OrderDate.Value.Microsecond != 0),
assertEmpty: true);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Ternary_Not_Null_Contains(bool async)
=> AssertFirstOrDefault(
async,
ss => ss.Set<Order>().OrderBy(x => x.OrderID).Select(x => x != null ? x.OrderID + "" : null),
x => x.Contains("1"));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Ternary_Not_Null_endsWith_Non_Numeric_First_Part(bool async)
=> AssertFirstOrDefault(
async,
ss => ss.Set<Order>().OrderBy(x => x.OrderID).Select(x => x != null ? "" + x.OrderID + "" : null),
x => x.EndsWith("1"));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Ternary_Null_Equals_Non_Numeric_First_Part(bool async)
=> AssertFirstOrDefault(
async,
ss => ss.Set<Order>().OrderBy(x => x.OrderID).Select(x => x == null ? null : "" + x.OrderID + ""),
x => x == "1");

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Ternary_Null_StartsWith(bool async)
=> AssertFirstOrDefault(
async,
ss => ss.Set<Order>().OrderBy(x => x.OrderID).Select(x => x == null ? null : x.OrderID + ""),
x => x.StartsWith("1"));
}
Original file line number Diff line number Diff line change
Expand Up @@ -7464,6 +7464,58 @@ FROM [Orders] AS [o]
""");
}

public override async Task Ternary_Not_Null_Contains(bool async)
{
await base.Ternary_Not_Null_Contains(async);

AssertSql(
"""
SELECT TOP(1) CAST([o].[OrderID] AS nvarchar(max)) + N''
FROM [Orders] AS [o]
WHERE CAST([o].[OrderID] AS nvarchar(max)) + N'' LIKE N'%1%'
ORDER BY [o].[OrderID]
""");
}

public override async Task Ternary_Not_Null_endsWith_Non_Numeric_First_Part(bool async)
{
await base.Ternary_Not_Null_endsWith_Non_Numeric_First_Part(async);

AssertSql(
"""
SELECT TOP(1) N'' + CAST([o].[OrderID] AS nvarchar(max)) + N''
FROM [Orders] AS [o]
WHERE N'' + CAST([o].[OrderID] AS nvarchar(max)) + N'' LIKE N'%1'
ORDER BY [o].[OrderID]
""");
}

public override async Task Ternary_Null_Equals_Non_Numeric_First_Part(bool async)
{
await base.Ternary_Null_Equals_Non_Numeric_First_Part(async);

AssertSql(
"""
SELECT TOP(1) N'' + CAST([o].[OrderID] AS nvarchar(max)) + N''
FROM [Orders] AS [o]
WHERE N'' + CAST([o].[OrderID] AS nvarchar(max)) + N'' = N'1'
ORDER BY [o].[OrderID]
""");
}

public override async Task Ternary_Null_StartsWith(bool async)
{
await base.Ternary_Null_StartsWith(async);

AssertSql(
"""
SELECT TOP(1) CAST([o].[OrderID] AS nvarchar(max)) + N''
FROM [Orders] AS [o]
WHERE CAST([o].[OrderID] AS nvarchar(max)) + N'' LIKE N'1%'
ORDER BY [o].[OrderID]
""");
}

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

Expand Down