Repository navigation
Closed
Milestone
Description
var delays = await db
.Set<Job>()
.Select(j => (j.StartJob - j.CreateDate).TotalHours)
.ToListAsync();
Gets translated into:
SELECT [j].[StartJob] - [j].[CreateDate]
FROM [Job] AS [j]
WHERE [j].[IsDeleted] = 0
Which in turn throws:
System.Data.SqlClient.SqlException (0x80131904): Operand data type datetime2 is invalid for subtract operator.
at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__108_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.<ExecuteAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.<BufferlessMoveNext>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.<ExecuteAsync>d__7`2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext()
ClientConnectionId:c7a3cc04-ddbf-4aab-9957-7477083c909f
Error Number:8117,State:1,Class:16
This query should either be translated into DateDiff if possible or perform subtraction on client but definitely not throw.
Further technical details
EF Core version: 2.0.1
Database Provider: Microsoft.EntityFrameworkCore.SQLServer
Operating system: Windows 10 1703
IDE: Visual Studio 2017
Activity
ralmsdeveloper commentedon Jan 6, 2018
Hello @SSkovboiSS,
This feature is available in version 2.1, using EF.Functions, but if you only need the translation on the server @smitpatel wrote this here:
#10241
AndrewBoklashko commentedon Jan 6, 2018
Thank you @ralmsdeveloper, I have already found a workaround. Nevertheless this is a bug and has to be fixed, that's why I created this issue.
ralmsdeveloper commentedon Jan 6, 2018
All Right, but if you use TimeStamp, we'll be limited to Day, Hour, Minutes and Seconds.
So it was implemented in EF.Functions, I do not believe the team wants to do this, for version 2.0.1, since it is close to launching 2.1 with this solution.
AndrewBoklashko commentedon Jan 6, 2018
@ralmsdeveloper the problem here is not about client or server evaluating, this query does not work at all, because it is getting translated into totally wrong SQL. And I am also ok with fix in 2.1.
ralmsdeveloper commentedon Jan 6, 2018
Exactly because there is no implementation for this:
(j.StartJob - j.CreateDate)AndrewBoklashko commentedon Jan 6, 2018
Alright, but why current implementation tries to perform this subtraction on database? Shouldn't it just client eval instead?
ajcvickers commentedon Jan 9, 2018
This may already be fixed in the 2.1 code. @SSkovboiSS Can you test with the nightly builds, as listed on the repo homepage: https://github.com/aspnet/EntityFrameworkCore
Assigning to @smitpatel, who will close if this is already fixed.
AndrewBoklashko commentedon Jan 9, 2018
@ajcvickers sure, I will test it tomorrow and give a feedback here.
AndrewBoklashko commentedon Jan 10, 2018
Just tested 2.1.0-preview1-28042 and it still fails.
Query log;
ralmsdeveloper commentedon Jan 10, 2018
@ajcvickers,
I believe this subtraction is only available in EF.Functions.
Implemented in this commit
Hugs!
8 remaining items
divega commentedon Feb 17, 2018
Another idea would be to recognize patterns like
(d1 - d2).TotalHoursand translate them to the corresponding DATEDIFF calls. Not all of them will work. FWIW, I just remembered we discussed about doing this in EF many years ago. Would it be hard with our current implementation?I wonder also if we could establish a conversion between an int containing seconds or microseconds and a
TimeSpan. Then when we need to return a TimeSpan (e.g. the projection containsd1 - d2) we use DATEDIFF and then apply the conversion, when we need to compare two TimeSpans (e.g.d1 - d2 < ts) we just compare the seconds, etc.ralmsdeveloper commentedon Feb 17, 2018
@divega It should also be remembered that we do not have TotalYears and TotalMonths.
So there would be a need to create an extension method, which I believe to be what you do not intend to do!
divega commentedon Feb 18, 2018
@ralmsdeveloper agreed that there are things that would be missing with that approach. That is what I meant when I said "not all of them will work". That may be a reason the
DateDiffextensions methods are still compelling even if only in some databases.smitpatel commentedon Feb 22, 2018
Design meeting notes
SqlServer DateDiff functions calculates boundary crossed for particular modifier. Going from 31st Dec to 1st Jan, for year, it gives result of 1 because year boundary was crossed. Which is significantly different from (date2 - date1) computation. Because the timespan will not have 1 year. Hence we are going to move DateDiff* function extensions to SqlServer provider. Filed Query: Make EF.Functions.DateDiff SqlServer specific #11038
Addition/subtraction of DateTime or a member access on TimeSpan afterwards can be translated to server in some cases, (e.g. the query like here). There is no easy way to do so for SqlServer. Filed SQL Server Query: DateTime addition/subtraction translation to server #11039
This issue would be used to avoid server evaluation of DateTime related functions on SqlServer/Sqlite so that we do not generated incorrect SQL. It would be client evaluated since there is no suitable way to do that on server. Providers like npgsql which can translate above to server will continue to work as is.
Query: Client Eval DateTime arithmetic operation for Sqlite/SqlServer
Query: Client Eval DateTime arithmetic operation for Sqlite/SqlServer
Query: Client Eval DateTime arithmetic operation for Sqlite/SqlServer
ErikEJ commentedon Mar 2, 2018
sql-ce fyi?