When we run HangFire and placed a trace using SQL Server Profile against the DB, we noticed the warning “Null value is eliminated by an aggregate or other SET operation”.
Traced the error back to this code:
;MERGE [HangFire].[AggregatedCounter] WITH (HOLDLOCK) AS [Target]
USING
(
SELECT
[Key]
, SUM([Value]) as [Value]
, MAX([ExpireAt]) AS [ExpireAt]
FROM @RecordsToAggregate
GROUP BY
[Key]
) AS [Source]
(
[Key]
, [Value]
, [ExpireAt]
)
ON [Target].[Key] = [Source].[Key]
WHEN MATCHED THEN
UPDATE SET
[Target].[Value] = [Target].[Value] + [Source].[Value]
, [Target].[ExpireAt] =
(
SELECT MAX([ExpireAt])
FROM
(
VALUES
([Source].ExpireAt)
, ([Target].[ExpireAt])
)
AS MaxExpireAt
(
[ExpireAt]
)
)
WHEN NOT MATCHED THEN
INSERT ([Key], [Value], [ExpireAt])
VALUES ([Source].[Key], [Source].[Value], [Source].[ExpireAt])
;
To fix:
;MERGE [HangFire].[AggregatedCounter] WITH (HOLDLOCK) AS [Target]
USING
(
SELECT
[Key]
, SUM([Value]) as [Value]
– dadeniji - when ExpiryAt is null, replace with 1900-01-01
–, MAX([ExpireAt]) AS [ExpireAt]
, MAX(isNull([ExpireAt], ‘1900-01-01’)) AS [ExpireAt]
FROM @RecordsToAggregate
GROUP BY
[Key]
) AS [Source]
(
[Key]
, [Value]
, [ExpireAt]
)
ON [Target].[Key] = [Source].[Key]
WHEN MATCHED THEN
UPDATE SET
[Target].[Value] = [Target].[Value] + [Source].[Value]
, [Target].[ExpireAt] =
(
SELECT MAX([ExpireAt])
FROM
(
VALUES
([Source].ExpireAt)
, ([Target].[ExpireAt])
)
AS MaxExpireAt
(
[ExpireAt]
)
)
WHEN NOT MATCHED THEN
INSERT ([Key], [Value], [ExpireAt])
VALUES ([Source].[Key], [Source].[Value], [Source].[ExpireAt])
;
Blogged about it here:
HangFire – SQL Server – “Warning: Null value is eliminated by an aggregate or other SET operation.”
https://learningintheopen.org/2017/04/24/hangfire-sql-server-warning-null-value-is-eliminated-by-an-aggregate-or-other-set-operation/