You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RangeHandler should handle all the types of its subtype's handler #3563
@Arsync can you please post a runnable code sample (not just a fragment) which shows the issue? If it reproduces without NpgsqlRange, that's even better.
Can't figure out what's going on: I've created small project to reproduce this issue from scratch.
But error message is different of my heavy solution:
Linq request in this small sample project:
System.InvalidCastException: 'Can't cast database type tstzrange to NpgsqlRange`1'
at System.Runtime.CompilerServices.CastHelpers.Unbox(Void* toTypeHnd, Object obj)
at System.Data.Common.DbDataReader.GetFieldValue[T](Int32 ordinal)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()
Linq request in my heavy solution:
Unable to cast object of type 'NpgsqlTypes.NpgsqlRange`1[NodaTime.Instant]' to type 'NpgsqlTypes.NpgsqlRange`1[NodaTime.OffsetDateTime]'.
at System.Runtime.CompilerServices.CastHelpers.Unbox(Void* toTypeHnd, Object obj)
at System.Data.Common.DbDataReader.GetFieldValue[T](Int32 ordinal)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()
Found why it throws different exceptions from big solution and updated sample project with another one Hosted app. It reproduces hosted runtime to add EFCoreSecondLevelCacheInterceptor, which changes exception message in following lines:
I've thinked about possible workarounds when found this article:
To summarize it in a few words, there are two sources of date and time data: system clocks, and the user. System generated values should be represented as Instant values. On the other hand user generated values should be represented as one of the following...
In my case properties VersionPeriod and CreationTime are system-generated values and both can be of Instant type:
@arontsang although there's semantic similarity between an interval and a range of timestamps, from a typing perspective NpgsqlRange corresponds to PostgreSQL ranges, which are a specific class of types with their own operations etc. You can already map NodaTime Interval (and Period) to PG interval.
Thinking about this some more, mapping NodaTime Interval to PG tsrange/tstzrange is something we could support - they do correspond very closely. The question in my mind is whether this should become the default mapping (minor breaking) or just an option.
publicclassA{publicNpgsqlRange<LocalDate>Period{get;set;}// User-controlled period in days with local datepublicNpgsqlRange<Instant>VersionPeriod{get;set;}// System field for versioning}
@Arsync I don't think we'd remove support for mapping NpgsqlRange<Instant> to tsrange. Mapping Interval would simply be another option for users who prefer the native NodaTime type, rather than the Npgsql type.
However, given that NodaTime's Interval does correspond very well to PG tsrange, using Interval seems like a nice API improvement.
@arontsang I like what you're doing, but as I wrote in the PR I think we should build the mapping support at the ADO layer, so that non-EF users can benefit from it too. Let me know if you're up for doing that too - and also if you need any help, tests or otherwise.
The issue with building the mapping at the ADO layer is that it would be a breaking change. Currently the ADO layer would map daterange to NpgsqlRange<LocalDate>.
@arontsang I think it should be possible to add support there by registering a DateIntervalHandler that extends RangeHandler, but also implements INpgsqlSimpleTypeHandler<DateInterval>. That would allow both type mappings to exist, while keeping the range mapping as the default; you would still be able to set a parameter's value to a DateInterval, and read back a daterange as DateInterval via NpgsqlDataReader.GetFieldValue<DateInterval>().
It's true we haven't yet done this with another range type, but I think it should be OK.
I looked into the original issue again (this got sucked into the discussion around mapping DateInterval etc).
At the ADO level, ranges over the NodaTime only support the handler's primary type (Instant for TimestampTzHandler). At some point I fixed this issue for non-NodaTime timestamptz by having a RangelHandler with two generic parameters (see this), but NodaTime's TimestampTzHandler supports 5 types :/
RangeHandler should support ranges over all its subtype's types via a better mechanism, instead of the current multiple generic handlers. It should be possible to just override Read<TAny> and handle things like that.
changed the title [-]Can't get value from tstzrange to NpgsqlRange<OffsetDateTime>[/-][+]RangeHandler should handle all the types of its subtype's handler[/+]on Feb 22, 2021
Activity
Arsync commentedon Dec 16, 2020
Seems, it happens not only with
NpgsqlRange, but with any properties of typeOffsetDateTime.roji commentedon Dec 16, 2020
@Arsync can you please post a runnable code sample (not just a fragment) which shows the issue? If it reproduces without NpgsqlRange, that's even better.
Arsync commentedon Dec 16, 2020
Can't figure out what's going on: I've created small project to reproduce this issue from scratch.
But error message is different of my heavy solution:
Linq request in this small sample project:
Linq request in my heavy solution:
Both DbContext configuration the same:
Version of
Npgsql.EntityFrameworkCore.PostgreSQL.NodaTimeis v5.0.1,NodaTimeis 3.0.3Arsync commentedon Dec 17, 2020
Found why it throws different exceptions from big solution and updated sample project with another one
Hostedapp. It reproduces hosted runtime to addEFCoreSecondLevelCacheInterceptor, which changes exception message in following lines:Test code has moved to Services/AppService.cs
There is two actions:
Can't cast database type tstzrange to NpgsqlRange'1')Unable to cast object of type 'NpgsqlTypes.NpgsqlRange'1[NodaTime.Instant]'...)Arsync commentedon Dec 17, 2020
I've thinked about possible workarounds when found this article:
In my case properties
VersionPeriodandCreationTimeare system-generated values and both can be ofInstanttype:Is it safe to map
Instanttotimestamp with time zone?Goal: performing sql queries by hands will show zoned time and C# code gets UTC time.
With this approach no need to use
NpgsqlRange<OffsetDateTime>while it throwscan't cast to NpgsqlRange.arontsang commentedon Jan 28, 2021
Shouldn't the conanical type to map to for EFcore.pg.Noda for this datebase type be NodaTime.Interval rather than NpgsqlRange?
roji commentedon Jan 28, 2021
@arontsang although there's semantic similarity between an interval and a range of timestamps, from a typing perspective NpgsqlRange corresponds to PostgreSQL ranges, which are a specific class of types with their own operations etc. You can already map NodaTime Interval (and Period) to PG interval.
roji commentedon Jan 28, 2021
Thinking about this some more, mapping NodaTime Interval to PG tsrange/tstzrange is something we could support - they do correspond very closely. The question in my mind is whether this should become the default mapping (minor breaking) or just an option.
Opened npgsql/efcore.pg#1671 to track.
Arsync commentedon Jan 28, 2021
Still have LocalDate period:
which now exists with Interval type:
Maybe it less elegant to use different types from two libraries to handle date ranges?
roji commentedon Jan 28, 2021
@Arsync I don't think we'd remove support for mapping
NpgsqlRange<Instant>to tsrange. Mapping Interval would simply be another option for users who prefer the native NodaTime type, rather than the Npgsql type.However, given that NodaTime's Interval does correspond very well to PG tsrange, using Interval seems like a nice API improvement.
arontsang commentedon Jan 28, 2021
As an aside, I am currently putting together a PR for a ClassMapper for mapping NodaTime.DateInterval into daterange.
https://github.com/npgsql/efcore.pg/pull/1670/files/63c1da141ac2385fd9ba65edbf9da0136256f28b..a65b026c1fa663ff4fd8b0f39f73eec8ba943c27
I've also implemented .Start, .End .Contains etc...
Unfortunately, I'm having some trouble with running the Unit Tests.
roji commentedon Jan 28, 2021
@arontsang I like what you're doing, but as I wrote in the PR I think we should build the mapping support at the ADO layer, so that non-EF users can benefit from it too. Let me know if you're up for doing that too - and also if you need any help, tests or otherwise.
arontsang commentedon Jan 28, 2021
roji commentedon Jan 28, 2021
@arontsang I think it should be possible to add support there by registering a DateIntervalHandler that extends RangeHandler, but also implements
INpgsqlSimpleTypeHandler<DateInterval>. That would allow both type mappings to exist, while keeping the range mapping as the default; you would still be able to set a parameter's value to a DateInterval, and read back a daterange as DateInterval viaNpgsqlDataReader.GetFieldValue<DateInterval>().It's true we haven't yet done this with another range type, but I think it should be OK.
roji commentedon Feb 22, 2021
I looked into the original issue again (this got sucked into the discussion around mapping DateInterval etc).
At the ADO level, ranges over the NodaTime only support the handler's primary type (Instant for TimestampTzHandler). At some point I fixed this issue for non-NodaTime timestamptz by having a RangelHandler with two generic parameters (see this), but NodaTime's TimestampTzHandler supports 5 types :/
RangeHandler should support ranges over all its subtype's types via a better mechanism, instead of the current multiple generic handlers. It should be possible to just override
Read<TAny>and handle things like that.[-]Can't get value from tstzrange to NpgsqlRange<OffsetDateTime>[/-][+]RangeHandler should handle all the types of its subtype's handler[/+]roji commentedon Sep 15, 2021
@arontsang FYI I did the mapping to NodaTime DateInterval in #3973, enabled by the type mapping redo in #3953.
roji commentedon Oct 3, 2021
We can do this by source-generating a RangeHandler for each base type. Do this after #3979 is resolved.
roji commentedon Oct 9, 2021
Did a quick source generation prototype (see https://github.com/roji/Npgsql/tree/archive/RangeHandlerSourceGeneration) - this approach works, but seems like a massive overkill in terms of complexity and code addition. We can simply use
NpgsqlRange<T1, T2>for two elements, or simply have a custom type deriving fromNpgsqlRange<T>and returned from the handler's CreateRangeHandler when needed (like NodaTime's DateHandler does).Am closing this - if we see any further needs we can revisit.