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
tstzrange[] and daterange[] aren't mapped to NodaTime Interval[] and DateInterval[] by default #4234
Npgsql.NodaTime 6.0 fixed the tstzrange to NodaTime.Interval mapping, but it does not map tstzrange[] to Interval[] as it should (it is still mapped as NpgsqlRange<Instant>[]).
Note that it correctly maps tstzmultirange to Interval[], but there are some scenarios where it is not possible to use tstzmultirange instead of tstzrange[], like if you don't have access to PostgreSQL 14+ (it is not yet available on all managed hosting providers), or if you want to store non-contiguous ranges in the field (overlapping ranges).
It can also write NodaTime Interval[] to PG tstzrange[], although since tstzmultirange is the default option, you need to explicitly specify tstzrange[] as follows:
This seems to be the right behavior: the multirange types should generally be preferred as the default, while giving users the option to use the array instead.
I'm using Dapper on top of Npgsql, so I'm not sure how it uses Npgsl internally to perform the query and read the results. What I do is :
My code:
usingDapper;usingNodaTime;usingNpgsql;usingSystem;usingSystem.Data;NpgsqlConnection.GlobalTypeMapper.UseNodaTime();SqlMapper.AddTypeHandler(newIntervaArrayHandler());usingvarconnection=newNpgsqlConnection("xxx");varp=new{Id=1};stringsql=$@"SELECT * FROM foo WHERE id = @Id;";Fooresult=awaitconnection.QuerySingleAsync<Foo>(sql,p);Console.WriteLine(result.Ranges.Length);publicclassFoo{publicintId{get;set;}publicInterval[]Ranges{get;set;}}publicclassIntervaArrayHandler:SqlMapper.TypeHandler<Interval[]>{publicoverrideInterval[]Parse(objectvalue){return(Interval[])value;}publicoverridevoidSetValue(IDbDataParameterparameter,Interval[]value){parameter.Value=value;}}
The code fails in the handler, in the IntervaArrayHandler.Parse(), because value is of type NpgsqlRange<Instant>[]:
System.Data.DataException
HResult=0x80131501
Message=Error parsing column 1 (ranges=NpgsqlTypes.NpgsqlRange`1[NodaTime.Instant][] - Object)
Source=Dapper
StackTrace:
at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in /_/Dapper/SqlMapper.cs:line 3706
at Dapper.SqlMapper.ReadRow[T](CacheInfo info, Identity identity, CommandDefinition& command, Type effectiveType, IDataReader reader) in /_/Dapper/SqlMapper.cs:line 1248
at Dapper.SqlMapper.<QueryRowAsync>d__34`1.MoveNext() in /_/Dapper/SqlMapper.Async.cs:line 480
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at <Program>$.<<Main>$>d__0.MoveNext() in [...]\Program.cs:line 12
This exception was originally thrown at this call stack:
IntervaArrayHandler.Parse(object) in Program.cs
[External Code]
Inner Exception 1:
InvalidCastException: Unable to cast object of type 'NpgsqlTypes.NpgsqlRange`1[NodaTime.Instant][]' to type 'NodaTime.Interval[]'.
Right, so this is about the default being read for tstzrange[] - it's currently NpgsqlRange<Instant>[] whereas it should be Interval[]. I'll make that change for 6.0.3.
The same is also true for daterange[] which should be mapped to DateInterval[] by default.
changed the title [-]tstzrange[] is not mapped to NodaTime.Interval[][/-][+]tstzrange[] and daterange[] aren't mapped to NodaTime Interval[] and DateInterval[] by default[/+]on Dec 29, 2021
PostgreSQL supports inclusive and exclusive bounds on range types. However, Interval doesn't seem to have such a concept and is always inclusive on the lower bound and always exclusive on the upper bound.
Activity
roji commentedon Dec 22, 2021
@madmox I ran a small test, and Npgsql can in fact read PG
tstzrange[]as NodaTimeInterval[]:It can also write NodaTime
Interval[]to PGtstzrange[], although since tstzmultirange is the default option, you need to explicitly specifytstzrange[]as follows:This seems to be the right behavior: the multirange types should generally be preferred as the default, while giving users the option to use the array instead.
Have I missed something?
madmox commentedon Dec 22, 2021
I'm using Dapper on top of Npgsql, so I'm not sure how it uses Npgsl internally to perform the query and read the results. What I do is :
My code:
The code fails in the handler, in the
IntervaArrayHandler.Parse(), becausevalueis of typeNpgsqlRange<Instant>[]:Map specialized NodaTime range types by default
roji commentedon Dec 29, 2021
Right, so this is about the default being read for
tstzrange[]- it's currentlyNpgsqlRange<Instant>[]whereas it should beInterval[]. I'll make that change for 6.0.3.The same is also true for
daterange[]which should be mapped toDateInterval[]by default.[-]tstzrange[] is not mapped to NodaTime.Interval[][/-][+]tstzrange[] and daterange[] aren't mapped to NodaTime Interval[] and DateInterval[] by default[/+]Map specialized NodaTime range types by default (#4252)
Map specialized NodaTime range types by default (#4252)
alitas commentedon Apr 21, 2022
PostgreSQL supports inclusive and exclusive bounds on range types. However,
Intervaldoesn't seem to have such a concept and is always inclusive on the lower bound and always exclusive on the upper bound.Documentation: https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-INCLUSIVITY
roji commentedon Apr 21, 2022
@alitas yep, that's right. You always still have the option of mapping tstzrange to NpgsqlRange instead of Interval if you need that kind of control.