Skip to content

NpgsqlRange with NodaTime's Instant infinite bounds #570

Closed

Description

@shoooe

How can I express a range that starts at instant X but is then not bounded "on the right"? I've tried with:

var range = new NpgsqlRange<Instant>(from, null);

But that because null is not of type Instant. I have then tried with:

var range = new NpgsqlRange<Instant?>(from, null);

but whenever I try to use it, for example with Overlaps I get:

The type 'NodaTime.Instant?' cannot be used as type parameter 'T' in the generic type or method 'NpgsqlRangeExtensions.Overlaps(NpgsqlRange, NpgsqlRange)'. The nullable type 'NodaTime.Instant?' does not satisfy the constraint of 'System.IComparable<NodaTime.Instant?>'. Nullable types can not satisfy any interface constraints.

So how can I express an unbounded range?


I've just realised that there's an NpgsqlRange<T> constructor that takes 6 parameters, two of which are lowerBoundInfinite and upperBoundInfinite but it's not clear to me what would go in the required two Instant values (lowerBound and upperBound).

Shouldn't maybe the NpgsqlRange constructor accept T? as parameters and define the respective upper/lower bound to be infinite if a null is passed?

Activity

  1. austindrenski commented on Aug 7, 2018

    @austindrenski
    Contributor

    The overloaded constructors are conceptually mapped to the underlying PostgreSQL range types:

    To help with the constructor complexity, v2.1.0 (Npgsql v4.0.0) introduced parsing support:

    // inclusive-exclusive bounded
    NpgsqlRange<int>.Parse("[0,10)");
    
    // unbounded
    NpgsqlRange<int>.Parse("[0,)");
    NpgsqlRange<int>.Parse("(null,0]");
    
    // unbounded with a special value '-infinity'/'infinity'
    NpgsqlRange<int>.Parse("(-infinity,0]");
    NpgsqlRange<int>.Parse("[0,infinity)");

    As for the exception related to IComparable<T>, I'll have to take a look. It may well be necessary to drop the comparable constraint.

  2. self-assigned this
    on Aug 7, 2018
  3. shoooe commented on Aug 7, 2018

    @shoooe
    Author

    Could you consider to provide an additional constructor that takes nullable types? The semantic seems fairly clear.
    Consider that in my case I have two Instant? coming as input and I'd like to construct a range out of those that can be open from both sides.

    What I find myself doing now is this:

    var range = new NpgsqlRange<Instant>(
        from.GetValueOrDefault(), true, !from.HasValue,
        to.GetValueOrDefault(), true, !to.HasValue
    );
  4. austindrenski commented on Aug 7, 2018

    @austindrenski
    Contributor

    @Jefffrey Nullable types should work as things are right now. Are you saying that you can't construct NpgsqlRange<Instant?>?

    The Overlaps extension method appears to fail with due to the added IComparable<T> constraint (which may need to be removed anyways). But that is a different problem.

    Adding a constructor that takes Nullable<T> for NpgsqlRange<T> would be challenging. For one thing, that would preclude constructing ranges of nullable types (string? ought to throw a compiler error).

    Don't get me wrong, we have our own reservations about the current state of NpgsqlRange<T> and there's ongoing work to improve it. But I'm not sure that adding another constructor is the answer.

    Related

    npgsql/npgsql#1943

  5. austindrenski commented on Aug 8, 2018

    @austindrenski
    Contributor

    IComparable<T> exception

    I opened #571 which removes the IComparable<T> constraint from the extension methods. Since the underlying type doesn't implement the constraint, the extension methods probably shouldn't either.

    The method constraints were introduced as part of some earlier work exploring client-implementations of those methods. This work was shelved due to some discrepancies between the CLR and PostgreSQL, but it seems that those constraints remained.

    NpgsqlRange<T> constructors

    I took a look at constructing NpgsqlRange<T> over a Nullable<T> (e.g. NpgsqlRange<DateTime?>) and it can be constructed in the CLR, but an exception is thrown upon writing to the backend. (It looks like the subtype mappings throw on write.)

    It might be reasonable to constrain NpgsqlRange<T> with where T : class, but that would involve a number of changes to the default mappings. The benefit would be that if T were nullable, we could further simplify the constructors, since null bounds would be trivial to identify.

    edit: I mistakenly thought that Nullable<T> was a class, but it is instead a struct.

    Its also worth noting that there has been discussion of the range construct proposed for C# 8.0. If ranges ship with 8.0, the provider-specific NpgsqlRange<T> might be deprecated depending on how the final language feature works. (We were recently able to deprecate NpgsqlInet in favor of (IPAddress Address, int Subnet) with the introduction of ValueTuple in C# 7.0.)

    /cc @roji

  6. roji commented on Aug 8, 2018

    @roji
    Member

    I took a look at constructing NpgsqlRange over a Nullable (e.g. NpgsqlRange<DateTime?>) and it can be constructed in the CLR, but an exception is thrown upon writing to the backend. (It looks like the subtype mappings throw on write.)

    I may be wrong but I think the proposal above was only to add a constructor accepting null, and not having the entire NpgsqlRange be over T?. The former would simply be a convenience constructor for cases where infinity is desired.

    It might be reasonable to constrain NpgsqlRange with where T : class, but that would involve a number of changes to the default mappings. The benefit would be that if T were nullable, we could further simplify the constructors, since null bounds would be trivial to identify.

    If we constrain T to class, wouldn't that make ranges of ints impossible? We could conversely have a constructor accepting T?, but that would require where T : struct. We could in theory have two range types (one for reference types, one for value types) but that really doesn't make sense to me, especially if we do it only to introduce a convenience constructor for infinity values...

    The fundamental problem is the inability in C# to generically have a single class/method which supports a nullable "anything" (i.e. either a reference type or a nullable value type).

  7. shoooe commented on Aug 8, 2018

    @shoooe
    Author

    I'm confused. What's the semantic of NpgsqlRange<T?> exactly? I thought NpgsqlRange (and the corresponding Postgres range type) wasn't defined for nullable values. Like, how is [3, NULL) different from [3, infinity)?

    From §8.17.4 of the PostgreSQL documentation:

    The lower bound of a range can be omitted, meaning that all points less than the upper bound are included in the range. Likewise, if the upper bound of the range is omitted, then all points greater than the lower bound are included in the range. If both lower and upper bounds are omitted, all values of the element type are considered to be in the range.

    This is equivalent to considering that the lower bound is "minus infinity", or the upper bound is "plus infinity", respectively.

    It's also defined that:

    Also, some element types have a notion of "infinity", but that is just another value so far as the range type mechanisms are concerned. For example, in timestamp ranges, [today,] means the same thing as [today,). But [today,infinity] means something different from [today,infinity) — the latter excludes the special timestamp value infinity.

    which is why I think it's helpful to have both a nullable constructor and an explicit infinity constructor.

    Still I don't understand Postgres design decisions. Why introduce a difference between infinity and null for ranges?

  8. austindrenski commented on Aug 8, 2018

    @austindrenski
    Contributor

    @roji wrote:

    I may be wrong but I think the proposal above was only to add a constructor accepting null, and not having the entire NpgsqlRange be over T?. The former would simply be a convenience constructor for cases where infinity is desired.

    Right, but we can't (currently) compile that:

    public class Test<T> 
    {
        public readonly T _value; 
        public Test(T value) => _value = value; 
        public Test(T? value) => _value = value ?? default(T); 
    }
    // error CS0453: 
    //   The type 'T' must be a non-nullable value type in order to use it as parameter 'T' in the 
    //   generic type or method 'Nullable<T>'

    Unless we add the where T : struct:

    public class Test<T> where T : struct
    {
        public readonly T _value; 
        public Test(T value) => _value = value; 
        public Test(T? value) => _value = value ?? default(T); 
    }

    But that would preclude things like NpgsqlRange<string> and might complicate future work on user-defined range types.


    @roji wrote:

    If we constrain T to class, wouldn't that make ranges of ints impossible? We could conversely have a constructor accepting T?, but that would require where T : struct.

    Yes. I mistakenly thought that Nullable<T> was a reference type. But that is not correct, it is a struct.

    Please disregard my previous comments on the class constraint.

    @roji wrote:

    The fundamental problem is the inability in C# to generically have a single class/method which supports a nullable "anything" (i.e. either a reference type or a nullable value type).

    Very much in agreement.


    @Jefffrey wrote:

    I'm confused. What's the semantic of NpgsqlRange<T?> exactly? I thought NpgsqlRange (and the corresponding Postgres range type) wasn't defined for nullable values. Like, how is [3, NULL) different from [3, infinity)?

    When I suggested that you try NpgsqlRange<T?>, I was hoping that our type mappings would let you send it with the same type handler as NpgsqlRange<T>. But this is not the case.

    NpgsqlRange<T> can be constructed for Nullable<T>, but it can't be read/written from/to the database.

    When you construct NpgsqlRange<T> with a flag for an infinite/undefined boundary, the actual value passed in for that boundary is never used for comparison and never saved to the database. But as you're finding, you still need to provide it.

    // not possible
    var range = new NpgsqlRange<Instant>(from, null);
    
    // possible
    var range = new NpgsqlRange<Instant>(from, true, !from.HasValue, default(Instant), true, true);

    Regarding your original issue with Overlaps, could you test out this build from our unstable MyGet feed?

    dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL --version 2.2.0-ci.759 --source https://www.myget.org/F/npgsql-unstable/api/v3/index.json

    At this point, I'm not convinced that we need to make any changes to the constraints on NpgsqlRange<T>.

    A struct constraint would allow for a convenience constructor...but I'm not sure that's valuable enough to close off the possibility of ranges over reference types.

  9. shoooe commented on Aug 11, 2018

    @shoooe
    Author

    Regarding your original issue with Overlaps, could you test out this build from our unstable MyGet feed?

    As soon as I'm done refactoring I'll give it a try, yes.

  10. austindrenski commented on Nov 7, 2018

    @austindrenski
    Contributor

    @Jefffrey Have you had a chance to check on this?

  11. shoooe commented on Nov 8, 2018

    @shoooe
    Author

    No, I'm sorry. I was just looking for an easier way to specify an unbounded range than

    var range = new NpgsqlRange<Instant>(
        from.GetValueOrDefault(), true, !from.HasValue,
        to.GetValueOrDefault(), true, !to.HasValue
    );
    

    but I settled for that.

    So the original Overlaps issue is no longer an issue for me.

    Unfortunately I moved on from there, and I don't have much time available at the moment. Maybe in the future.

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

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions