Skip to content

Map NodaTime Interval to tstzrange #4070

Closed

Description

@roji

I somehow managed to miss NodaTime's Interval type. This is a perfect match for PostgreSQL tstzrange, similarly to how we map DateInterval to PostgreSQL daterange.

Activity

  1. added this to the 6.0.0 milestone on Oct 25, 2021
  2. self-assigned this
    on Oct 25, 2021
  3. madmox commented on Dec 22, 2021

    @madmox

    I'm unsure if this is the appropriate place to report this, but it seems tstzrange arrays (tstzrange[]) are still mapped to NpgsqlRange<Instant>[] instead of Interval[] in 6.0.2. Is it done on purpose?

  4. roji commented on Dec 22, 2021

    @roji
    MemberAuthor

    @madmox interesting... NodaTime Interval[] is mapped to PG tstzmultirange (and vice versa). However, I did not make special mappings to make PG tstzrange[] (non-multirange!) map back to NodaTime Interval[]. I guess my assumption was that now that multiranges are available, tstzrange[] would no longer be used much - though it's true some people are using pre-14 PostgreSQL where multirange isn't yet supported.

    Do you have any particular reason for using tstzrange[] instead of tstzmultirange (aside from the PG version)? In any case, you can open this as a separate issue and I'll look into it.

  5. madmox commented on Dec 22, 2021

    @madmox

    PostgreSQL on Amazon RDS doesn't (yet) support PostgreSQL 14 engines (except in us-east-2, it seems), most recent option in our region is 13.3.

    Actually, I didn't event know multiranges were a thing in PostgreSQL until today, when I upgraded to npgsql 6 and found out about the tstzrange[] issue. Although reading PostgreSQL documentation, if I understand it correctly, the "non-contiguous" constraint on multirange types might be an issue for us as we store a list of periods that might overlap.

  6. roji commented on Dec 22, 2021

    @roji
    MemberAuthor

    @madmox the non-contiguous aspect of multiranges may indeed be a reason to continue using arrays, true.

    If using NodaTime Interval[] is important for you, please open a new issue - I can't promise I'll get around to it immediately though.

  7. madmox commented on Dec 22, 2021

    @madmox

    I can easily get around the issue because I use Dapper on top of npgsql and already have type handlers so that Dapper correctly maps properties with non default types, so the fix is not top-priority. I even updated the handler so that it accepts both Interval[] and NpgsqlRange<Instant>[], in case the fix is integrated later and I forget about the issue when upgrading my nugets.

    public class IntervalListHandler : SqlMapper.TypeHandler<List<Interval>>
    {
        public override List<Interval> Parse(object value)
        {
            // Looks like npgsql incorrectly maps tstzrange[] to NpgsqlRange<Instant>[]
            // although it correctly maps tstzrange to NodaTime.Interval.
            // See https://github.com/npgsql/npgsql/issues/4070 for details.
    
            if (value is NpgsqlRange<Instant>[])
            {
                var typedValue = (NpgsqlRange<Instant>[])value;
                return typedValue?.Select(x => new Interval(x.LowerBound, x.UpperBound)).ToList();
            }
            else
            {
                var typedValue = (Interval[])value;
                return typedValue?.ToList();
            }
        }
    
        public override void SetValue(IDbDataParameter parameter, List<Interval> value)
        {
            parameter.Value = value;
            ((NpgsqlParameter)parameter).NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Range | NpgsqlDbType.TimestampTz;
        }
    }

    But I opened the issue :)

  8. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions