Skip to content

Add SqlServer translation for DateOnly.ToDateTime(timeOnly) - #35194

Merged
roji merged 1 commit into
dotnet:mainfrom
mseada94:dateonly-to-datetime
Dec 15, 2024
Merged

roji merged 1 commit into
dotnet:mainfrom
mseada94:dateonly-to-datetime

Conversation

@mseada94

@mseada94 mseada94 commented Nov 25, 2024 •

Copy link
Copy Markdown
Contributor

Fixes #35076

  • 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

I created the PR as draft

  • The translation is done as expected
  • TODO: I have an issue in comparison between datetime and string, I need to cast const value as datetime please guide where to look
  • TODO: add support for DateOnly as constant with TimeOnly Property

@mseada94
mseada94 requested a review from a team as a code owner November 25, 2024 00:02
@mseada94

Copy link
Copy Markdown
Contributor Author

The translation is done as expected but I have issue in the where comparison which compare date with datetime

Error Message on test: 

Microsoft.Data.SqlClient.SqlException : Conversion failed when converting date and/or time from character string.

SQL sent to the database:

SELECT [m].[Id], [m].[BriefingDocument], [m].[BriefingDocumentFileExtension], [m].[CodeName], [m].[Date], [m].[Difficulty], [m].[Duration], [m].[Rating], [m].[Time], [m].[Timeline]
FROM [Missions] AS [m]
WHERE DATETIMEFROMPARTS(DATEPART(year, [m].[Date]), DATEPART(month, [m].[Date]), DATEPART(day, [m].[Date]), DATEPART(hour, [m].[Time]), DATEPART(minute, [m].[Time]), DATEPART(second, [m].[Time]), DATEPART(millisecond, [m].[Time])) = '1990-11-10T10:15:50.5000000'

@mseada94

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

@mseada94
mseada94 marked this pull request as draft November 25, 2024 00:06

@roji roji 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.

I think the translation should be to DATETIME2FROMPARTS (which returns SQL Server datetime2), rather than DATETIMEFROMPARTS (which returns the discouraged datetime.

We also need to refrain from doing the translation when either the DateOnly or the TimeOnly are a complex SQL expression (anything other than a column/constant/parameter), for performance reasons; see #35076 (comment).

@roji roji self-assigned this Nov 25, 2024
mseada94 added a commit to mseada94/efcore that referenced this pull request Nov 25, 2024
    - use array literal
    - use DATETIME2FROMPARTS
    - use time 7 fractions instead of milisecond
    - add support for constant DateOnly

Fixes dotnet#35194
mseada94 added a commit to mseada94/efcore that referenced this pull request Nov 25, 2024
@mseada94
mseada94 requested a review from roji November 25, 2024 08:58
@mseada94
mseada94 marked this pull request as ready for review November 25, 2024 09:45
Fixes dotnet#35076

Co-authored-by: Shay Rojansky <roji@roji.org>
@roji
roji force-pushed the dateonly-to-datetime branch from 1a76f6f to e891a51 Compare December 15, 2024 21:29

@roji roji 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.

@mseada94 thanks for your contribution... In the interest of time, I've rebased this on top of the latest main - porting the test to the new translations test suite - and done a bit of fixup/cleanup. I'll merge once everything's green.

@roji
roji enabled auto-merge (squash) December 15, 2024 21:55
@roji
roji merged commit 44b38a8 into dotnet:main Dec 15, 2024
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.

SQL Server: translate dateOnly.ToDateTime(timeOnly) to DATETIMEFROMPARTS

2 participants