Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@jas88
Copy link
Owner

@jas88 jas88 commented Oct 29, 2025

Summary

Fixes CI failures in PR #39 and other dependabot PRs by removing CAST to Date that was stripping time component from datetime comparisons.

Problem

RemoteDatabaseAttacherTests were failing with timezone-sensitive errors:

  • Expected: 3 rows
  • Got: 2 rows

Root Cause

RemoteAttacher.cs:107 had:

const string dateConvert = "Date";
// Used in: WHERE CAST(column as Date) > DATEADD(...)

The Bug:

  • CAST to Date strips hours/minutes/seconds
  • When test runs near midnight, withinDate crosses day boundary
  • Example: "2025-10-28 23:00:00" becomes "2025-10-28"
  • Comparison with DATEADD fails due to loss of time precision

Fix

Removed all CAST(column as Date) operations:

  • Before: WHERE CAST(date_seen as Date) > DATEADD(DAY, -1, GETDATE())
  • After: WHERE date_seen > DATEADD(DAY, -1, GETDATE())

Column is already DateTime type, so CAST is unnecessary and harmful.

Affected Code

All AttacherHistoricalDurations cases:

  • Past24Hours, Past7Days, PastMonth, PastYear
  • SinceLastUse, Custom, DeltaReading

Impact

Fixes:

Tests:

  • TestRemoteDatabaseAttacherWithDateFilter(MicrosoftSQLServer, *, Past24Hours)
  • TestRemoteDatabaseAttacherWithDateFilter(MySql, *, Past24Hours)
  • All other duration combinations now have accurate time-based filtering

Copilot AI review requested due to automatic review settings October 29, 2025 01:32
@jas88 jas88 enabled auto-merge (rebase) October 29, 2025 01:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR removes date casting from SQL filter generation to preserve time components during date comparisons. The change addresses timezone-sensitive failures that occur when datetime values cross midnight boundaries.

  • Removes CAST({column} as Date) operations from all date column references in SQL WHERE clauses
  • Adds explanatory comments about the rationale for preserving time components
  • Affects filtering logic across multiple fetch duration modes (Past24Hours, Past7Days, PastMonth, PastYear, SinceLastUse, Custom, and DeltaReading)
Comments suppressed due to low confidence (3)

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

  • The GetCorrectDateAddForDatabaseType method still casts to Date type (e.g., line 71 for PostgreSQL, line 79 for SQL Server, line 81 for MySQL, and TRUNC for Oracle which truncates to date). This creates inconsistent comparisons where the column now preserves its time component but the comparison value is still truncated to date-only. For example, a datetime column value of '2024-01-15 23:30:00' would be compared against '2024-01-15 00:00:00', which doesn't achieve the intended purpose of preserving time accuracy. All date casts in this method should be removed to maintain consistency with the changes in SqlHistoricalDataFilter.
    private static string GetCorrectDateAddForDatabaseType(DatabaseType dbType, string addType, string amount)
    {
        switch (dbType)
        {
            case DatabaseType.PostgreSql:
                return $"cast((NOW() AT TIME ZONE 'UTC' + interval '{amount} {addType}S') as Date)";
            case DatabaseType.Oracle:
                if (addType == "DAY") return $"TRUNC(DateAdd(SYS_EXTRACT_UTC(SYSTIMESTAMP),,{amount}))";
                if (addType == "WEEK") return $"TRUNC(DateAdd(SYS_EXTRACT_UTC(SYSTIMESTAMP),,{amount} *7))";
                if (addType == "MONTH") return $"TRUNC(DateAdd(SYS_EXTRACT_UTC(SYSTIMESTAMP),,,{amount}))";
                if (addType == "YEAR") return $"TRUNC(DateAdd(SYS_EXTRACT_UTC(SYSTIMESTAMP),,,,{amount}))";
                return $"TRUNC(DateAdd(SYS_EXTRACT_UTC(SYSTIMESTAMP),,{amount}))";
            case DatabaseType.MicrosoftSQLServer:
                return $"CAST(DATEADD({addType}, {amount}, GETUTCDATE()) as Date)";
            case DatabaseType.MySql:
                return $"DATE(DATE_ADD(UTC_TIMESTAMP(), INTERVAL {amount} {addType}))";
            default:
                throw new InvalidOperationException("Unknown Database Type");
        }
    }

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

  • The ConvertDateString method still converts to Date type for most database types (lines 94, 96, 98, 100), which truncates the time component. Since RemoteTableDateFormat is defined as 'yyyy-MM-dd HH:mm:ss.fff' (line 43) and includes time information, this conversion negates the purpose of preserving time components. For databases other than PostgreSQL, the conversion should produce datetime/timestamp values instead of date-only values to match the format string and maintain consistency with the column comparison changes.
    private string ConvertDateString(DatabaseType dbType, string dateString)
    {
        switch (dbType)
        {
            case DatabaseType.PostgreSql:
                return $"'{dateString}'";
            case DatabaseType.Oracle:
                return $"TO_DATE('{dateString}')";
            case DatabaseType.MicrosoftSQLServer:
                return $"convert(Date,'{dateString}')";
            case DatabaseType.MySql:
                return $"convert('{dateString}',Date)";
            default:
                return $"convert(Date,'{dateString}')";
        }

    }

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

  • Corrected spelling of 'colunn' to 'column'.
    [DemandsInitialization("Option name for the Date colunn table within RDMP if it differs from the remote table e.g Due to joins")]

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jas88 jas88 force-pushed the fix/remote-attacher-datetime-cast branch 2 times, most recently from ed91a3d to e889d10 Compare October 29, 2025 12:58
jas88 added 2 commits October 29, 2025 07:59
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)
Update unit test assertions to match the corrected SQL that no longer
casts the datetime column to Date.

All tests now expect:
  WHERE date > DATEADD(...)

Instead of:
  WHERE CAST(date as Date) > DATEADD(...)

This preserves time component for accurate datetime filtering and
fixes timezone-sensitive test failures.

Tests updated:
- TestRemoteAttacherParameter (4 cases)
- TestRemoteAttacherParameterSinceLastUse
- TestRemoteAttacherParameterCustomRange (3 cases)
- TestRemoteAttacherParameterDeltaReading (2 cases)

All 14 RemoteAttacherTests now passing.
@jas88 jas88 force-pushed the fix/remote-attacher-datetime-cast branch from e889d10 to f762efa Compare October 29, 2025 12:59
@coveralls
Copy link

coveralls commented Oct 29, 2025

Pull Request Test Coverage Report for Build 18908693538

Details

  • 9 of 17 (52.94%) changed or added relevant lines in 2 files are covered.
  • 8 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.001%) to 36.346%

Changes Missing Coverage Covered Lines Changed/Added Lines %
Rdmp.Core.Tests/DataLoad/Engine/Integration/RemoteAttacherTests.cs 0 8 0.0%
Files with Coverage Reduction New Missed Lines %
Rdmp.Core.Tests/DataLoad/Engine/Integration/RemoteAttacherTests.cs 8 0.0%
Totals Coverage Status
Change from base Build 18894539431: 0.001%
Covered Lines: 32592
Relevant Lines: 95915

💛 - Coveralls

@jas88 jas88 merged commit 6887c3e into main Oct 29, 2025
5 checks passed
@jas88 jas88 deleted the fix/remote-attacher-datetime-cast branch October 29, 2025 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants