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 #12657 - Query: logic protecting SUM from returning null doesn't work for complex scenarios #21203

Merged
merged 1 commit into from
Jun 15, 2020
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
9 changes: 7 additions & 2 deletions src/EFCore.Relational/Query/SqlNullabilityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,13 @@ protected virtual SqlExpression VisitSqlFunction(
arguments[i] = Visit(sqlFunctionExpression.Arguments[i], out _);
}


return sqlFunctionExpression.Update(instance, arguments);
return sqlFunctionExpression.IsBuiltIn
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm on the fence here - we do provide Sum translation into SqlFunction "SUM" in relational, but if there is a provider that translates it to a different function it would have to rewrite the entire visit sql function method, so that SUM doesn't get coalesce wrapped around it incorrectly. thoughts? @roji @smitpatel

Copy link
Contributor

Choose a reason for hiding this comment

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

For best solution it would be good to put at translation of sum but there is going to be some refactoring coming in when implementing more group by operation and #20633 may be needed for it.

I think this is ok compromise for now, it may not be the best solution but avoids wrong results for customers.

Copy link
Member

Choose a reason for hiding this comment

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

Of a provider translates to different function, wouldn't they just be responsible for applying the same coalscing in their own nullability processor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, its just harder than it needs to be (do the translation + call base vs rewrite entire visit sql function method) but maybe its fine

&& string.Equals(sqlFunctionExpression.Name, "SUM", StringComparison.OrdinalIgnoreCase)
? _sqlExpressionFactory.Coalesce(
sqlFunctionExpression.Update(instance, arguments),
_sqlExpressionFactory.Constant(0, sqlFunctionExpression.TypeMapping),
sqlFunctionExpression.TypeMapping)
: sqlFunctionExpression.Update(instance, arguments);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ public virtual Task Collection_select_nav_prop_sum(bool async)
elementSorter: e => e.Sum);
}

[ConditionalTheory(Skip = "Issue#12657")]
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Collection_select_nav_prop_sum_plus_one(bool async)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ public override async Task Result_operator_nav_prop_reference_optional_Sum(bool
await base.Result_operator_nav_prop_reference_optional_Sum(async);

AssertSql(
@"SELECT SUM([l0].[Level1_Required_Id])
@"SELECT COALESCE(SUM([l0].[Level1_Required_Id]), 0)
maumar marked this conversation as resolved.
Show resolved Hide resolved
FROM [LevelOne] AS [l]
LEFT JOIN [LevelTwo] AS [l0] ON [l].[Id] = [l0].[Level1_Optional_Id]");
}
Expand Down Expand Up @@ -1035,10 +1035,10 @@ public override async Task Result_operator_nav_prop_reference_optional_via_Defau
await base.Result_operator_nav_prop_reference_optional_via_DefaultIfEmpty(async);

AssertSql(
@"SELECT SUM(CASE
@"SELECT COALESCE(SUM(CASE
WHEN [l0].[Id] IS NULL THEN 0
ELSE [l0].[Level1_Required_Id]
END)
END), 0)
FROM [LevelOne] AS [l]
LEFT JOIN [LevelTwo] AS [l0] ON [l].[Id] = [l0].[Level1_Optional_Id]");
}
Expand Down Expand Up @@ -4319,7 +4319,7 @@ public override async Task Sum_with_selector_cast_using_as(bool async)
await base.Sum_with_selector_cast_using_as(async);

AssertSql(
@"SELECT SUM([l].[Id])
@"SELECT COALESCE(SUM([l].[Id]), 0)
FROM [LevelOne] AS [l]");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ public override async Task Result_operator_nav_prop_reference_optional_via_Defau
await base.Result_operator_nav_prop_reference_optional_via_DefaultIfEmpty(async);

AssertSql(
@"SELECT SUM(CASE
@"SELECT COALESCE(SUM(CASE
WHEN [t].[Id0] IS NULL THEN 0
ELSE [t].[Level1_Required_Id]
END)
END), 0)
FROM [Level1] AS [l]
LEFT JOIN (
SELECT [l0].[Id], [l0].[Date], [l0].[Name], [l0].[OneToOne_Required_PK_Date], [l0].[Level1_Optional_Id], [l0].[Level1_Required_Id], [l0].[Level2_Name], [l0].[OneToMany_Optional_Inverse2Id], [l0].[OneToMany_Required_Inverse2Id], [l0].[OneToOne_Optional_PK_Inverse2Id], [l0].[Id] AS [Id0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2593,7 +2593,7 @@ public override async Task Sum_with_optional_navigation_is_translated_to_sql(boo
await base.Sum_with_optional_navigation_is_translated_to_sql(async);

AssertSql(
@"SELECT SUM([g].[SquadId])
@"SELECT COALESCE(SUM([g].[SquadId]), 0)
FROM [Gears] AS [g]
LEFT JOIN [Tags] AS [t] ON ([g].[Nickname] = [t].[GearNickName]) AND ([g].[SquadId] = [t].[GearSquadId])
WHERE ([t].[Note] <> N'Foo') OR [t].[Note] IS NULL");
Expand Down Expand Up @@ -5348,7 +5348,7 @@ public override async Task GroupBy_Property_Include_Select_Sum(bool async)
await base.GroupBy_Property_Include_Select_Sum(async);

AssertSql(
@"SELECT SUM([g].[SquadId])
@"SELECT COALESCE(SUM([g].[SquadId]), 0)
FROM [Gears] AS [g]
GROUP BY [g].[Rank]");
}
Expand Down Expand Up @@ -6145,7 +6145,7 @@ public override async Task Complex_GroupBy_after_set_operator(bool async)
await base.Complex_GroupBy_after_set_operator(async);

AssertSql(
@"SELECT [t].[Name], [t].[Count], SUM([t].[Count]) AS [Sum]
@"SELECT [t].[Name], [t].[Count], COALESCE(SUM([t].[Count]), 0) AS [Sum]
FROM (
SELECT [c].[Name], (
SELECT COUNT(*)
Expand All @@ -6169,7 +6169,7 @@ public override async Task Complex_GroupBy_after_set_operator_using_result_selec
await base.Complex_GroupBy_after_set_operator_using_result_selector(async);

AssertSql(
@"SELECT [t].[Name], [t].[Count], SUM([t].[Count]) AS [Sum]
@"SELECT [t].[Name], [t].[Count], COALESCE(SUM([t].[Count]), 0) AS [Sum]
FROM (
SELECT [c].[Name], (
SELECT COUNT(*)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override async Task Sum_with_no_arg(bool async)
await base.Sum_with_no_arg(async);

AssertSql(
@"SELECT SUM([o].[OrderID])
@"SELECT COALESCE(SUM([o].[OrderID]), 0)
FROM [Orders] AS [o]");
}

Expand All @@ -47,7 +47,7 @@ public override async Task Sum_with_binary_expression(bool async)
await base.Sum_with_binary_expression(async);

AssertSql(
@"SELECT SUM([o].[OrderID] * 2)
@"SELECT COALESCE(SUM([o].[OrderID] * 2), 0)
FROM [Orders] AS [o]");
}

Expand All @@ -56,7 +56,7 @@ public override async Task Sum_with_arg(bool async)
await base.Sum_with_arg(async);

AssertSql(
@"SELECT SUM([o].[OrderID])
@"SELECT COALESCE(SUM([o].[OrderID]), 0)
FROM [Orders] AS [o]");
}

Expand All @@ -65,7 +65,7 @@ public override async Task Sum_with_arg_expression(bool async)
await base.Sum_with_arg_expression(async);

AssertSql(
@"SELECT SUM([o].[OrderID] + [o].[OrderID])
@"SELECT COALESCE(SUM([o].[OrderID] + [o].[OrderID]), 0)
FROM [Orders] AS [o]");
}

Expand All @@ -74,7 +74,7 @@ public override async Task Sum_with_division_on_decimal(bool async)
await base.Sum_with_division_on_decimal(async);

AssertSql(
@"SELECT SUM(CAST([o].[Quantity] AS decimal(18,2)) / 2.09)
@"SELECT COALESCE(SUM(CAST([o].[Quantity] AS decimal(18,2)) / 2.09), 0.0)
FROM [Order Details] AS [o]");
}

Expand All @@ -83,7 +83,7 @@ public override async Task Sum_with_division_on_decimal_no_significant_digits(bo
await base.Sum_with_division_on_decimal_no_significant_digits(async);

AssertSql(
@"SELECT SUM(CAST([o].[Quantity] AS decimal(18,2)) / 2.0)
@"SELECT COALESCE(SUM(CAST([o].[Quantity] AS decimal(18,2)) / 2.0), 0.0)
FROM [Order Details] AS [o]");
}

Expand All @@ -92,7 +92,7 @@ public override async Task Sum_with_coalesce(bool async)
await base.Sum_with_coalesce(async);

AssertSql(
@"SELECT SUM(COALESCE([p].[UnitPrice], 0.0))
@"SELECT COALESCE(SUM(COALESCE([p].[UnitPrice], 0.0)), 0.0)
FROM [Products] AS [p]
WHERE [p].[ProductID] < 40");
}
Expand Down Expand Up @@ -131,7 +131,7 @@ public override async Task Sum_on_float_column(bool async)
await base.Sum_on_float_column(async);

AssertSql(
@"SELECT CAST(SUM([o].[Discount]) AS real)
@"SELECT CAST(COALESCE(SUM([o].[Discount]), 0.0E0) AS real)
FROM [Order Details] AS [o]
WHERE [o].[ProductID] = 1");
}
Expand All @@ -142,7 +142,7 @@ public override async Task Sum_on_float_column_in_subquery(bool async)

AssertSql(
@"SELECT [o0].[OrderID], (
SELECT CAST(SUM([o].[Discount]) AS real)
SELECT CAST(COALESCE(SUM([o].[Discount]), 0.0E0) AS real)
FROM [Order Details] AS [o]
WHERE [o0].[OrderID] = [o].[OrderID]) AS [Sum]
FROM [Orders] AS [o0]
Expand Down Expand Up @@ -1295,7 +1295,7 @@ public override async Task Project_constant_Sum(bool async)
await base.Project_constant_Sum(async);

AssertSql(
@"SELECT SUM(1)
@"SELECT COALESCE(SUM(1), 0)
FROM [Employees] AS [e]");
}

Expand Down Expand Up @@ -1453,7 +1453,7 @@ public override async Task Sum_over_explicit_cast_over_column(bool async)
await base.Sum_over_explicit_cast_over_column(async);

AssertSql(
@"SELECT SUM(CAST([o].[OrderID] AS bigint))
@"SELECT COALESCE(SUM(CAST([o].[OrderID] AS bigint)), CAST(0 AS bigint))
FROM [Orders] AS [o]");
}

Expand Down
Loading