Skip to content

Translate the TimeSpan Total* members on SQL Server - #39487

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
Laurianti:translate-timespan-total-members
Nov 4, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
Laurianti:translate-timespan-total-members

Conversation

@Laurianti

Copy link
Copy Markdown
Contributor

Fixes #19632

Hours, Minutes, Seconds and the other component members are already translated with DATEPART; the Total* members were not, as noted in #19632 (comment).

A SQL Server time value is below 24 hours, so its number of ticks is computed exactly from DATEPART, as a bigint:

CAST(DATEPART(hour, t) * 3600 + DATEPART(minute, t) * 60 + DATEPART(second, t) AS bigint) * 10000000 + DATEPART(nanosecond, t) / 100

Each Total* member then divides it once, as TimeSpan does (TotalNanoseconds multiplies by 100), so the result is the same double as in .NET. Adding up the parts separately (hour + minute / 60.0 + ...) gives a different last bit for some values: for 02:00:15.4562004, TotalHours would be 2.0042933890000003 instead of 2.0042933889999999.

  • TimeSpanTranslationsTestBase gets one test per member, with thresholds that depend on the fraction of a second. SQLite and Cosmos assert that the translation fails, as for the other members.

  • TotalHours_in_projection (SQL Server) compares the projected values with .NET. With the parts added up separately it fails on the two values with a fraction of a second.

  • Outside the tests, on SQL Server 2022 the seven members give the same bits as .NET for 0, 1 tick, 01:02:03, 03:04:05.6789124, 01:00:15.456, 02:00:15.4562004, 12:30 and 23:59:59.9999999.

  • On current main, on Linux with SQL Server 2022: TimeSpanTranslationsSqlServerTest 15/15, the SQL Server Query.Translations tests 431 passed (3 skipped), TimeSpanTranslationsSqliteTest 14/14, TimeSpanTranslationsInMemoryTest 13/13. The Cosmos tests were not run locally.

  • I've read the guidelines for contributing and seen the walkthrough

  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team

  • The code builds and tests pass locally (also verified by our automated build checks)

  • Commit messages follow this format:

        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

- TotalDays, TotalHours, TotalMinutes, TotalSeconds, TotalMilliseconds, TotalMicroseconds and TotalNanoseconds over a time value
- The ticks are computed exactly from DATEPART and divided once, as in .NET, so the results are the same doubles

Fixes dotnet#19632

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Exact .NET double-result coverage is incomplete for the other TimeSpan Total* translations, leaving potential last-bit mismatches undetected.

1 open issue
What changed in this PR

This pull request adds SQL Server translation support for all TimeSpan.Total* members while preserving .NET-compatible double results.

Changes:

  • Reconstructs TimeSpan ticks from SQL Server DATEPART values and translates TotalDays, TotalHours, TotalMinutes, TotalSeconds, TotalMilliseconds, TotalMicroseconds, and TotalNanoseconds.
  • Adds shared translation tests, SQL Server baselines, and projected TotalHours precision coverage.
  • Confirms these members remain unsupported on SQLite and Cosmos.
  • Nit (3 votes): Add exact scalar-projection coverage for the remaining members, particularly TotalNanoseconds and the distinct divisor paths.

This pull request is ready to be approved.

File Summary
test/​EFCore.SqlServer.FunctionalTests/​Query/​Translations/​Temporal/​TimeSpanTranslationsSqlServerTest.cs Adds SQL baselines and projected-value validation for SQL Server translations.
test/​EFCore.Sqlite.FunctionalTests/​Query/​Translations/​Temporal/​TimeSpanTranslationsSqliteTest.cs Confirms the new members remain unsupported on SQLite.
test/​EFCore.Specification.Tests/​Query/​Translations/​Temporal/​TimeSpanTranslationsTestBase.cs Adds shared predicate tests for each Total* member.
test/​EFCore.Cosmos.FunctionalTests/​Query/​Translations/​Temporal/​TimeSpanTranslationsCosmosTest.cs Confirms the new members remain unsupported on Cosmos.
src/​EFCore.SqlServer/​Query/​Internal/​Translators/​SqlServerTimeSpanMemberTranslator.cs Implements tick-based SQL Server translation for all TimeSpan.Total* members.

Review effort: Lite

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit e3323b4 into dotnet:main Nov 4, 2026
19 of 20 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 4, 2026
@Laurianti
Laurianti deleted the translate-timespan-total-members branch November 7, 2026 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SqlServer: Translate TimeSpan members

3 participants