Skip to content

Commit beeb673

Browse files
committed
Fix RemoteAttacher date filtering to preserve time component
Remove CAST to Date which was stripping time component and causing timezone-sensitive test failures in RemoteDatabaseAttacherTests. Issue: - SqlHistoricalDataFilter was using CAST(column as Date) - This strips hours/minutes/seconds from datetime values - When test runs near midnight, causes off-by-one day errors - Test expects 3 rows but gets 2 due to date boundary issues Example failure: - withinDate = "2025-10-28 23:00:00" (1 hour ago, Oct 28) - CAST to Date = "2025-10-28" (time stripped) - DATEADD(DAY, -1, GETDATE()) = "2025-10-28" at midnight - Comparison: "2025-10-28" > "2025-10-28" = FALSE (row excluded) Fix: - Remove all CAST(column as Date) operations - Use datetime column directly for time-sensitive comparisons - Preserves full precision for accurate date filtering Affected cases: - Past24Hours, Past7Days, PastMonth, PastYear - SinceLastUse, Custom, DeltaReading This fixes CI failures in PR #39 and other dependabot PRs running RemoteDatabaseAttacherTests at various times of day. Tests affected: - TestRemoteDatabaseAttacherWithDateFilter(*, *, Past24Hours)
1 parent 2213ebc commit beeb673

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

Rdmp.Core/DataLoad/Modules/Attachers/RemoteAttacher.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,31 +104,32 @@ private string ConvertDateString(DatabaseType dbType, string dateString)
104104

105105
public string SqlHistoricalDataFilter(ILoadMetadata loadMetadata, DatabaseType dbType, string column)
106106
{
107-
const string dateConvert = "Date";
107+
// Don't cast to Date - preserve time component for accurate filtering
108+
// Casting to Date causes timezone-sensitive failures when time crosses midnight
108109

109110
switch (HistoricalFetchDuration)
110111
{
111112
case AttacherHistoricalDurations.Past24Hours:
112-
return $" WHERE CAST({column} as {dateConvert}) > {GetCorrectDateAddForDatabaseType(dbType, "DAY", "-1")}";
113+
return $" WHERE {column} > {GetCorrectDateAddForDatabaseType(dbType, "DAY", "-1")}";
113114
case AttacherHistoricalDurations.Past7Days:
114-
return $" WHERE CAST({column} as {dateConvert}) > {GetCorrectDateAddForDatabaseType(dbType, "WEEK", "-1")}";
115+
return $" WHERE {column} > {GetCorrectDateAddForDatabaseType(dbType, "WEEK", "-1")}";
115116
case AttacherHistoricalDurations.PastMonth:
116-
return $" WHERE CAST({column} as {dateConvert}) > {GetCorrectDateAddForDatabaseType(dbType, "MONTH", "-1")}";
117+
return $" WHERE {column} > {GetCorrectDateAddForDatabaseType(dbType, "MONTH", "-1")}";
117118
case AttacherHistoricalDurations.PastYear:
118-
return $" WHERE CAST({column} as {dateConvert}) > {GetCorrectDateAddForDatabaseType(dbType, "YEAR", "-1")}";
119+
return $" WHERE {column} > {GetCorrectDateAddForDatabaseType(dbType, "YEAR", "-1")}";
119120
case AttacherHistoricalDurations.SinceLastUse:
120-
return loadMetadata.LastLoadTime is not null ? $" WHERE CAST({column} as {dateConvert}) > {ConvertDateString(dbType, loadMetadata.LastLoadTime.GetValueOrDefault().ToString(RemoteTableDateFormat))}" : "";
121+
return loadMetadata.LastLoadTime is not null ? $" WHERE {column} > {ConvertDateString(dbType, loadMetadata.LastLoadTime.GetValueOrDefault().ToString(RemoteTableDateFormat))}" : "";
121122
case AttacherHistoricalDurations.Custom:
122123
if (CustomFetchDurationStartDate == DateTime.MinValue && CustomFetchDurationEndDate != DateTime.MinValue)
123124
{
124125
//end only
125-
return $" WHERE CAST({column} as {dateConvert}) <= {ConvertDateString(dbType, CustomFetchDurationEndDate.ToString(RemoteTableDateFormat))}";
126+
return $" WHERE {column} <= {ConvertDateString(dbType, CustomFetchDurationEndDate.ToString(RemoteTableDateFormat))}";
126127
}
127128

128129
if (CustomFetchDurationStartDate != DateTime.MinValue && CustomFetchDurationEndDate == DateTime.MinValue)
129130
{
130131
//start only
131-
return $" WHERE CAST({column} as {dateConvert}) >= {ConvertDateString(dbType, CustomFetchDurationStartDate.ToString(RemoteTableDateFormat))}";
132+
return $" WHERE {column} >= {ConvertDateString(dbType, CustomFetchDurationStartDate.ToString(RemoteTableDateFormat))}";
132133
}
133134

134135
if (CustomFetchDurationStartDate == DateTime.MinValue && CustomFetchDurationEndDate == DateTime.MinValue)
@@ -137,12 +138,12 @@ public string SqlHistoricalDataFilter(ILoadMetadata loadMetadata, DatabaseType d
137138
return "";
138139
}
139140

140-
return $" WHERE CAST({column} as {dateConvert}) >= {ConvertDateString(dbType, CustomFetchDurationStartDate.ToString(RemoteTableDateFormat))} AND CAST({column} as {dateConvert}) <= {ConvertDateString(dbType, CustomFetchDurationEndDate.ToString(RemoteTableDateFormat))}";
141+
return $" WHERE {column} >= {ConvertDateString(dbType, CustomFetchDurationStartDate.ToString(RemoteTableDateFormat))} AND {column} <= {ConvertDateString(dbType, CustomFetchDurationEndDate.ToString(RemoteTableDateFormat))}";
141142
case AttacherHistoricalDurations.DeltaReading:
142143
if (DeltaReadingStartDate == DateTime.MinValue) return "";
143144
var startDate = DeltaReadingStartDate.AddDays(-DeltaReadingLookBackDays);
144145
var endDate = DeltaReadingStartDate.AddDays(DeltaReadingLookForwardDays);
145-
return $" WHERE CAST({column} as {dateConvert}) >= {ConvertDateString(dbType, startDate.ToString(RemoteTableDateFormat))} AND CAST({column} as {dateConvert}) < {ConvertDateString(dbType, endDate.ToString(RemoteTableDateFormat))}";
146+
return $" WHERE {column} >= {ConvertDateString(dbType, startDate.ToString(RemoteTableDateFormat))} AND {column} < {ConvertDateString(dbType, endDate.ToString(RemoteTableDateFormat))}";
146147
default:
147148
return "";
148149
}

0 commit comments

Comments
 (0)