Skip to content

Redesigned range type - #3168

Closed
YohDeadfall wants to merge 2 commits into
mainfrom
range-types
Closed

YohDeadfall wants to merge 2 commits into
mainfrom
range-types

Conversation

@YohDeadfall

Copy link
Copy Markdown
Contributor

This change was split from a large improvement of type handlers am working on to simplify review process. The idea of the new design to minimize API surface and make it more fluent and modern. The bound type represents a discrimination union which could have only three possible values: infinite, inclusive and exclusive. While it exists it isn't used inside the range to store values, but the range can return bounds as separate parts.

Fixes #2314

@YohDeadfall
YohDeadfall requested a review from roji as a code owner September 20, 2020 13:56
@YohDeadfall
YohDeadfall force-pushed the range-types branch 2 times, most recently from a600c52 to 08468c0 Compare September 20, 2020 14:13
@roji

roji commented Feb 1, 2021 •

Copy link
Copy Markdown
Member

I promise I've reviewed this while trying to keep an open mind, and I think there are some good things here. I still largely think what I wrote in npgsql/efcore.pg#785 (comment) - we've received one user complaint over the years, where the user acknowledged this is API nitpicking. I agree the current API isn't ideal, but I don't think it's real problem that we need to spend time on, which we could use for real, valuable features. I'm also not a fan of introducing big breaking changes just to make the API slightly better.

  • I think that introducing these Bound types is a heavy solution to the problem, that makes the API more complicated in some important cases (any additional type is additional complexity for users). Specifically, I think that in the common case, users don't store infinite values. Today, to get the value I can just do range.LowerBound, but this new proposal forces me to do range.LowerBound.Value; that seems like making the API worse for the common case. We can still have the new Bound types if we really think they're useful (I'm not so sure), but keep direct access to the members (this would also be non-breaking).
  • I do like the idea of the accessors throwing when the bound is infinite. Although this is technically a breaking change, it's very unlikely anyone was successfully using that accessor in that scenario (the value is meaningless). It would help users catch buggy code.
  • I'm not sure I see a reason for removing Parse. I know we want to remove client-side logic for custom date/time types - but that's different, since date/time logic is extremely complicated and we have bugs. With range parsing, things are pretty simple and I don't remember us having any bugs, so it seems like a gratuitous breaking change. See NpgsqlRange.Parse use culture-specific parsing #3599 for an example of a bug. The current implementation uses type converters to convert from the string representation, but the .NET string representation can be quite different from the PostgreSQL representation.

Another (breaking) proposal

If I were designing the new API today (and breaking everyone), I'd go down the route of using null to represent infinity as previously discussed:

  • NpgsqlRange<TLower, TUpper> would allow users to have NpgsqlRange<int, int?>, which represents a range with a non-infinite lower bound and a possibly infinite (infinitable) upper bound.
  • We could still have NpgsqlRange<T>, where the infinitability of both ranges is the same: NpgsqlRange<int> doesn't allow lower/upper infinite bounds, NpgsqlRange<int?> allows them on both sides. We'd have an implicit cast operator from NpgsqlRange<T> to NpgsqlRange<T,T>.
    • This makes the value accessors typed exactly right: if I use a generic type that allows infinite on a bound, I get back a nullable
  • We'd have a check in the static constructor to throw on non-sensical cases such as NpgsqlRange<string, int>.
  • If you try to read a range with an infinite bound into a range type that doesn't allow it, the type handler throws.
  • If you use non-generic read (NpgsqlDataReader.GetValue), you get NpgsqlRange<int?> (big breaking change).
  • We'd still have to deal with empty (probably check and throw from the accessors).
  • If we do this right, for the common case where infinity is needed, this could be minimally-breaking for current code (and non-breaking for many usages).
  • For reference types, this requires the NRT language feature to be enabled. I don't think that's too bad since ranges over references types are very rare.

There are probably some complications with this - especially with how the type handler would be implemented. But from a user perspective this seems like a nice API, and you don't have to have to deal with the additional bounds types.

@vonzshik @Brar it would be good to have your thoughts on all of this.

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

I like the idea with nullable types, but the idea was to represent PostgreSQL range types as closer to backend types as possible. Therefore, I have to make the bound type since there are two types of them: inclusive and exclusive. Nullable types lack of it, but with them API is more friendlier except that there still will be four constructor parameters with which am not happy.

I specially not implemented parsing exactly for the same reason you mentioned. Besides, don't forget that the element types also must be parseable which won't work for new date time types.

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

We'd have a check in the static constructor to throw on non-sensical cases such as NpgsqlRange<string, int>

Never use static constructors for that cases. It runs only once even if it throws. Such cases must be handled by members.

@roji

roji commented Feb 1, 2021

Copy link
Copy Markdown
Member

I like the idea with nullable types, but the idea was to represent PostgreSQL range types as closer to backend types as possible. Therefore, I have to make the bound type since there are two types of them: inclusive and exclusive. Nullable types lack of it, but with them API is more friendlier except that there still will be four constructor parameters with which am not happy.

I don't see how the representation with NpgsqlRangeBound is closer to the backend types than the proposed one with nullability... PG has no "range_bound" type or similar.

It's true that we'd still have to allow people to work with inclusiveness, but I don't see the problem with a four-parameter constructor. And again, with your proposal, people have to now do range.LowerBound.Value all the time, which makes the API less usable.

I specially not implemented parsing exactly for the same reason you mentioned. Besides, don't forget that the element types also must be parseable which won't work for new date time types.

I guess I don't think that Parse is very important - the main point is to not introduce breaking changes without a good reason (and this will continue to work for e.g. int).

If we end up redoing the whole API and introducing big breaking changes anyway, I won't be against removing Parse. If we don't do such changes (or do changes which aren't breaking), then I don't see a reason to remove it.

We'd have a check in the static constructor to throw on non-sensical cases such as NpgsqlRange<string, int>

Never use static constructors for that cases. It runs only once even if it throws. Such cases must be handled by members.

So, what exactly is the problem :) Yes, it's good for it to run only once (this way they don't need to run every time in the members), and it's good for it to throw, no?

@roji

roji commented Feb 1, 2021

Copy link
Copy Markdown
Member

BTW just in case it wasn't clear... At the moment I don't think we should do my nullability-based proposal - I don't think we should do anything (except possibly to change the accessors to throw when the bound is infinite).

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

And again, with your proposal, people have to now do range.LowerBound.Value all the time, which makes the API less usable.

That's what I don't like in my proposal. Okay, will use nullable types and four parameters in constructor, at least it's better what we have no.

So, what exactly is the problem :) Yes, it's good for it to run only once (this way they don't need to run every time in the members), and it's good for it to throw, no?

I mean that if an exception happens in try-catch and it doesn't crash the application, the next time it will work fine. That's why vector types from .NET throw in members instead of a static constructor. There's no runtime cost at doing it each time the user accesses a member since JIT will emit either a throw branch or fine branch. It's just a good design rule would like to tell you (:

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

BTW just in case it wasn't clear... At the moment I don't think we should do my nullability-based proposal - I don't think we should do anything (except possibly to change the accessors to throw when the bound is infinite).

Why so? There will be other breaking changes, so it's a good time to push them all and not one by one.

@roji

roji commented Feb 1, 2021

Copy link
Copy Markdown
Member

Why so? There will be other breaking changes, so it's a good time to push them all and not one by one.

Just because I think we should only break stuff if there's a good reason to, and here it's just some API nitpicking which only one user has complained about... This would have an impact on everyone using ranges, and for what...

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

Do I correctly understand that you're not interested in taking a new design at all and that it won't happen?

@vonzshik

vonzshik commented Feb 1, 2021

Copy link
Copy Markdown
Contributor

Am I missing something, or we're rewriting the range API so:

  1. remove the ugliness of the constructor (1, true, false, 0, false, true)
  2. we can combine the parts of the range with the others to create other ranges

@YohDeadfall

YohDeadfall commented Feb 1, 2021 •

Copy link
Copy Markdown
Contributor Author

@vonzshik, you're right, but mostly I was interested in the first point and other usability improvements. And the proposed implementation stores data exactly as PostgreSQL does, so write and read operations only serialize elements. It was done as a part of new type handling, but spun it of for simplicity.

@roji

roji commented Feb 1, 2021

Copy link
Copy Markdown
Member

Do I correctly understand that you're not interested in taking a new design at all and that it won't happen?

My personal opinion is that there's not enough reason to completely break people by inventing a new API - the problems aren't serious enough and almost nobody cares. I think it's more reasonable to improve it without breaking, if that makes sense.

But I know we have differing opinions here. If the others (@vonzshik, @Brar) think that we should spend time on this and introduce a new, breaking API, then I will be OK with it. I think at that point we can also discuss which exact new API shape we want to have (i.e. with a new bounds type like in your proposal, or nullability-based).

Does that sound good?

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

Of others will be involved too, am fine with it. Just to point out, not a single dev complained about the API, there were other participants. Me too thinks that it's one of ugliest things I saw (sorry, but it's really so and it was hard to use during times when I was an user too).

@roji

roji commented Feb 1, 2021

Copy link
Copy Markdown
Member

Me too thinks that it's one of ugliest things I saw

For a single constructor with 6 parameters - that sounds like a bit of an exaggeration :) But let's see if the others thinks this is worth our time and the breaking changes being proposed.

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

My intention here to make it usable and construct in a fluent way without taking so much parameters and so. Even before contributing here I wasn't able to understand how it was possible to write such a thing, why not enum but many bools. Times changed a bit, so there're named parameters, but still it forces you to write all 6 parameters to have a half open range. I felt like the type says me "go away, am not a right thing to be used here". I mean that frequently used API should be very simple to use while unsafe and dangerous things should be unwelcome to prevent their usage.

@roji

roji commented Feb 1, 2021

Copy link
Copy Markdown
Member

As I said above, if you think we should expose the enum publicly and accept that in a new constructor, that may be possible and it wouldn't be a breaking change. Many problems can be solved without causing trouble for existing users.

Speaking of API usability, I'd personally be quite turned off by a range API that forces me to do range.LowerBound.Value each time I just want to get the values I put into it.

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

Agreed on the last sentence, there's an additional member to be accessed. It was done according to C# guidelines saying that implicit operators must not throw. At the same time a bound is nothing more than a discriminated union which are fine in other languages. Unfortunately, there's no way to write them in C# and don't think that it would happen in the observable future.

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

@khellang, your opinion is welcome here!

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

It's good that I had no time yesterday on the issue and that I spent a lot of time on Rust for two reasons:

  • Got some points of knowledge of Rust and metaprogramming in it (yeah, I love compile time execution).
  • Was able to refocus and came here with a fresh mind.

So... Despite my first impression on two generic parameters, am not sure it suits well in .NET as well as other languages. Yeah, the idea deals with the excessive member access, but at the same time it makes type handling trickier, the resulting type depends on data which is the same bad idea we had recently consider numerics. It also doesn't follow discriminant unions and the idea of decomposition and single responsibility.

In the proposal I made, a bound is a thing which combines all data about it into a single object and behaves exactly as the nullable type. Unfortunately, .NET means runtime compilation, so there's no way to decide what kind the cast operator should have, implicit or explicit, like it's possible in C++ (since C++ 20 it's even possible to make a conditionally explicit cast without templates).

Therefore, I don't think that I even will try your idea. While it looks better at first, afterward there's a lot of issues to deal with.

@roji

roji commented Feb 2, 2021

Copy link
Copy Markdown
Member

it makes type handling trickier

That could be the case - though type handling generally shouldn't limit the APIs we present to users - it's there to serve us, not the other way around. I wouldn't assume it's problematic without trying, in any case.

the resulting type depends on data which is the same bad idea we had recently consider numerics.

I don't think the resulting type depends on data. If users use the generic GetFieldValue, they are telling us which type they want to read (like in all other cases). If the use the non-generic GetValue, we just always return an NpgsqlRange with infinitable bounds (e.g. NpgsqlRange<int?>), since that can accommodate any data. I wrote that above in the proposal.

It also doesn't follow discriminant unions and the idea of decomposition and single responsibility.

I don't really understand what this means :)

But again, I prefer that we first make the decision if we really want to break everyone over this, before we discuss the merits of each breaking proposal. Let's wait for more opinions.

@YohDeadfall

YohDeadfall commented Feb 2, 2021 •

Copy link
Copy Markdown
Contributor Author

I don't think the resulting type depends on data.

It's while GetFieldValue is used. The same thing was with arrays @FransBouma reported to us.

I don't really understand what this means :)

I mean we can't follow PostgreSQL ranges as they are due to language limitations and other reasons, so there's a need of specifying a kind of a bound. Instead of doing it directly on a range, the logic moved to a separate type which is Nullable like type (it's a discriminant union too, btw, written using the existing syntax and somewhere backed up by JIT, it's kind of a special type) and that type describes the bound well. It groups all properties and its instance can be passed to methods, stored separately and so on without making a value tuple or used made type. Take a look at Range and Index which represent a bound.

Therefore, I conclude that am satisfied with mine proposal since it shares the same ideas as other types in .NET as well in other languages/platforms. It's clean enough even if it adds one more accessor to get a value.

But again, I prefer that we first make the decision if we really want to break everyone over this, before we discuss the merits of each breaking proposal. Let's wait for more opinions.

Am not forcing you to approve it, while it would be a pleasure to see you being not so conservative and more willing to improve even such a thing. I care a lot about usability, good design even if it looks minor, you know. Maybe I should be less careless in case of compatibility... Anyway, it was just to summarize my morning thoughts on it and share them with reviewers.

@vonzshik

vonzshik commented Feb 2, 2021

Copy link
Copy Markdown
Contributor

My not-so structured thoughts on this pr:

The proposed API does look correct. That is, left and right boundaries are separate entities, which we can view, use and mix. I do like this idea.
But, I'm not sure it's the right thing to do. In my opinion, the main point of this kind of API is to be as simple as possible (due to it being just a representation of a database object). And yet, by adding a new entities, we actually make it a little bit harder to use.

One of the points of any major release is we're allowed to introduce breaking changes. And I have nothing against them in the API, that is if they are limiting us in some way. But that's not what I see here - we're rewriting the API so it would look nice (I mean, it should be very simple to add a custom API, which would hide all this ugliness, so you never have to deal with it). That's cool and all, but not something I'm thrilled as a user.

All in all, I'm for the way, when on the next major release we deprecate the previous API and it's removed with the next one.

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

All in all, I'm for the way, when on the next major release we deprecate the previous API and it's removed with the next one.

It's possible only in COM world where you just add a suffix to an interface/type. There you can't do so without having another type or a new place to put it in.

@roji

roji commented Feb 2, 2021

Copy link
Copy Markdown
Member

@YohDeadfall I think @vonzshik means having both APIs in parallel in the next version, with the current one obsoleted, and then remove the obsoleted one in the release afterwards.

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

I understand, but can't see how it's possible at all. In COM all interfaced are resolved by IIDs, but in .NET by names and it's not possible to have two types with the same name.

@roji

roji commented Feb 2, 2021

Copy link
Copy Markdown
Member

It would mean two different type names...

@vonzshik

vonzshik commented Feb 2, 2021

Copy link
Copy Markdown
Contributor

Or namespaces. But from what I've seen, there should be a simpler way. I'll try to take a look in the next few days.

@NinoFloris

Copy link
Copy Markdown
Member

@YohDeadfall I'm going over old PRs to close some of them out. I don't think we ever got to anything concrete here right?

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

Yep, correct. I have nor time neither intentions to continue on my previous work.

@NinoFloris
NinoFloris deleted the range-types branch August 28, 2023 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Range API improvement

4 participants