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
NpgsqlRange with NodaTime's Instant infinite bounds #570
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?
To help with the constructor complexity, v2.1.0 (Npgsql v4.0.0) introduced parsing support:
// inclusive-exclusive boundedNpgsqlRange<int>.Parse("[0,10)");// unboundedNpgsqlRange<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.
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.
@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.
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.)
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).
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?
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:
publicclassTest<T>{publicreadonlyT_value;publicTest(Tvalue)=>_value=value;publicTest(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>'
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.
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).
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 possiblevarrange=newNpgsqlRange<Instant>(from,null);// possiblevarrange=newNpgsqlRange<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?
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.
Activity
austindrenski commentedon Aug 7, 2018
The overloaded constructors are conceptually mapped to the underlying PostgreSQL range types:
To help with the constructor complexity,
v2.1.0(Npgsqlv4.0.0) introduced parsing support: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.shoooe commentedon Aug 7, 2018
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:
austindrenski commentedon Aug 7, 2018
@Jefffrey Nullable types should work as things are right now. Are you saying that you can't construct
NpgsqlRange<Instant?>?The
Overlapsextension method appears to fail with due to the addedIComparable<T>constraint (which may need to be removed anyways). But that is a different problem.Adding a constructor that takes
Nullable<T>forNpgsqlRange<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
austindrenski commentedon Aug 8, 2018
IComparable<T>exceptionI 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>constructorsI took a look at constructing
NpgsqlRange<T>over aNullable<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 constrainNpgsqlRange<T>withwhere T : class, but that would involve a number of changes to the default mappings. The benefit would be that ifTwere nullable, we could further simplify the constructors, sincenullbounds would be trivial to identify.edit: I mistakenly thought that
Nullable<T>was aclass, but it is instead astruct.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 deprecateNpgsqlInetin favor of(IPAddress Address, int Subnet)with the introduction ofValueTuplein C# 7.0.)/cc @roji
roji commentedon Aug 8, 2018
I may be wrong but I think the proposal above was only to add a constructor accepting null, and not having the entire
NpgsqlRangebe overT?. The former would simply be a convenience constructor for cases where infinity is desired.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 requirewhere 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).
shoooe commentedon Aug 8, 2018
I'm confused. What's the semantic of
NpgsqlRange<T?>exactly? I thoughtNpgsqlRange(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:
It's also defined that:
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?
austindrenski commentedon Aug 8, 2018
@roji wrote:
Right, but we can't (currently) compile that:
Unless we add the
where T : struct:But that would preclude things like
NpgsqlRange<string>and might complicate future work on user-defined range types.@roji wrote:
Yes. I mistakenly thought that
Nullable<T>was a reference type. But that is not correct, it is astruct.Please disregard my previous comments on the
classconstraint.@roji wrote:
Very much in agreement.
@Jefffrey wrote:
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 asNpgsqlRange<T>. But this is not the case.NpgsqlRange<T>can be constructed forNullable<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.Regarding your original issue with
Overlaps, could you test out this build from our unstable MyGet feed?At this point, I'm not convinced that we need to make any changes to the constraints on
NpgsqlRange<T>.A
structconstraint 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.shoooe commentedon Aug 11, 2018
As soon as I'm done refactoring I'll give it a try, yes.
austindrenski commentedon Nov 7, 2018
@Jefffrey Have you had a chance to check on this?
shoooe commentedon Nov 8, 2018
No, I'm sorry. I was just looking for an easier way to specify an unbounded range than
but I settled for that.
So the original
Overlapsissue 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.