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
Mapping PostgreSQL composites to .NET value tuples #2097
That's a very cool idea, and it also connects with another idea I had: to map PostgreSQL composites to value tuples. For some reason I got a bit excited for 4.0 and implemented support for mapping composites as dynamic - but doing value tuple seems much more useful/interesting. It may be better to first look at what this means at the Npgsql ADO.NET level, before thinking about it in EF Core.
If we do end up mapping composites to value tuple, then ROW(...) very neatly fits as the literal SQL representation, which is definitely cool. PostgreSQL also allows you to treat a row of a regular table as a composite type - SELECT * FROM data WHERE data.* = ROW(1,2) - but I can't really see EF Core generating any SQL even close to that. However, composite literals would still be useful for filtering on a composite column of a table, i.e. WHERE my_table.my_column = ROW(1,2).
We need to keep in mind that there are already mappings to specific shapes of ValueTuple. Ideally, work on row constructors/composites will not block the use of ValueTuple for other types.
For example, cidr can now map to (IPAddress, int), avoiding the provider-specific type NpgsqlInet.
The title of this issue should be "mapping PostgreSQL composites to .NET value tuples". Row constructors are simply one way (among others) to instantiate a composite.
Note that there are two possible things we can do here:
Allow explicit mapping of composites to value types (basic)
Make the default mapping of composites value types, so if a user doesn't use the explicit mapping API, reading a composite returns a value type. This is a breaking change, but I think is probably the best behavior one can expect from a .NET PostgreSQL driver.
Make the default mapping of composites value types, so if a user doesn't use the explicit mapping API, reading a composite returns a value type. This is a breaking change, but I think is probably the best behavior one can expect from a .NET PostgreSQL driver.
This sounds good. On the design, do we update the reader API with a series of methods for value tuples of 1/2/3/4/etc generic params? (Unless I've overlooked it, I don't think value tuples implement a common base type.)
On the design, do we update the reader API with a series of methods for value tuples of 1/2/3/4/etc generic params? (Unless I've overlooked it, I don't think value tuples implement a common base type.)
I don't think so. I'd expect user code to look like this:
vartup1=reader.GetFieldValue<(intid,stringname)>(0);vartup2=((intid,stringname))reader[0];// Returns the tuple by default, boxes
Note that the 2nd form breaks backwards compatibility as the current behavior is to return a dynamic ExpandoObject instead (again, unless an explicit mapping has been set up with an API, which takes precedence in any case). I'm personally fine with this breakage for 4.1.
Note: think about PostgreSQL records in this context (see #2154). These may be a bit more appropriate for mapping to value tuple than composites, which are strongly-typed and need to be defined in advance (much like CLR classes/structs). It could be interesting to look into possible conversions between composite and record types.
Note: at the type level ValueTuples have no member names whatsoever, only Item1, Item2. This means that we can't match CLR names to PostgreSQL composite field names, as we currently do. We can still do positional binding, where it's the user's responsibility to have tuple members in the exact same order as they are defined in the database, but this seems like it could be risky.
If the tuple's field types don't match the composite's, we can raised an exception, no problem. But if there are several fields with the same type, it's easy to imagine values getting written/read into the wrong field... I also think that not many users really understood that value tuples have no names under the hood, and this could open the door to lots of confusion and bugs.
@austindrenski@YohDeadfall would be good to hear some opinions. If we think it's too dangerous we can also drop the current dynamic support for unmapped composites, which I don't think it used very much (and which I really dislike).
But if there are several fields with the same type, it's easy to imagine values getting written/read into the wrong field... I also think that not many users really understood that value tuples have no names under the hood, and this could open the door to lots of confusion and bugs.
Sigh, I think you're right on this one. As you mentioned above, ValueTuple seems to be a better fit for modeling PostgreSQL records anyways.
would be good to hear some opinions. If we think it's too dangerous we can also drop the current dynamic support for unmapped composites, which I don't think it used very much (and which I really dislike).
I agree that this sounds like it would create too much confusion. I'm also on board with dropping all dynamic support across the board, here included.
Sorry, completely forgot about it. With the ROW construction in PostgreSQL it's possible to shoot your legs too, so I see no reason why we should prevent users from doing the same thing.
@YohDeadfall I think the situation is a bit different... ROW is just a literal representation (and you're right that there's no names), but the PostgreSQL type definition is still a composite type with field names. On the .NET side, ValueTuple never has any names - it's not just an issue with the literal representation, but with the actual type.
Imagine some PostgreSQL composite type consisting of 2 strings. If we allow mapping this to a ValueTuple<string, string>, it means that the order of definition of the PostgreSQL composite type fields determines which goes where - this seems very dangerous to me.
ROW is just a literal representation (and you're right that there's no names), but the PostgreSQL type definition is still a composite type with field names.
Nope, it's just a tuple (:
SELECT (ROW('no', 'column', 'names', 'here')).*;
-- ERROR: record type has not been registered-- pg_typeof(any) returns record
I think, users should understand what they are writing too and choose right tools for that. It's dangerous, but it perfectly fits some scenarios where you don't want to write a whole POCO for one SQL query. Maybe we can skip this tuple handling request since there are extremely limited number of cases.
By the way, there's a proposal which should bring us records like in F#. See dotnet/csharplang#39.
ROW is just a literal representation (and you're right that there's no names), but the PostgreSQL type definition is still a composite type with field names.
Nope, it's just a tuple (:
I know. The issue is that in PostgreSQL you also have the composite type, which is strongly typed and has defined names. On the .NET side there would only be ValueTuple, which does not.
I'll say it differently... This proposal is about having Npgsql read and write composite types (again, which have names) as .NET ValueTuples (which do not) - it seems like a mismatch. The fact that ROW() exists in PostgreSQL doesn't necessarily seem very relevant...
I'm not saying this wouldn't be useful. But the potential for trouble seems to far outweigh the benefits. As you say, the number of cases where this would help seems limited, but I suspect a lot of people would try to use it in unjustified cases (i.e. where a POCO would be better) and shoot themselves in the foot.
Activity
austindrenski commentedon Aug 5, 2018
@roji wrote:
moved from npgsql/efcore.pg#563
austindrenski commentedon Aug 5, 2018
We need to keep in mind that there are already mappings to specific shapes of
ValueTuple. Ideally, work on row constructors/composites will not block the use ofValueTuplefor other types.For example,
cidrcan now map to(IPAddress, int), avoiding the provider-specific typeNpgsqlInet.roji commentedon Aug 6, 2018
The title of this issue should be "mapping PostgreSQL composites to .NET value tuples". Row constructors are simply one way (among others) to instantiate a composite.
Note that there are two possible things we can do here:
[-]Support mapping ValueTuple with row constructors[/-][+]Mapping PostgreSQL composites to .NET value tuples[/+]austindrenski commentedon Aug 6, 2018
This sounds good. On the design, do we update the reader API with a series of methods for value tuples of 1/2/3/4/etc generic params? (Unless I've overlooked it, I don't think value tuples implement a common base type.)
YohDeadfall commentedon Aug 6, 2018
They are structs, so they can't have a base type. But they implement non-generic
ITuple.roji commentedon Aug 6, 2018
I don't think so. I'd expect user code to look like this:
Note that the 2nd form breaks backwards compatibility as the current behavior is to return a dynamic ExpandoObject instead (again, unless an explicit mapping has been set up with an API, which takes precedence in any case). I'm personally fine with this breakage for 4.1.
roji commentedon Nov 18, 2018
Note: think about PostgreSQL records in this context (see #2154). These may be a bit more appropriate for mapping to value tuple than composites, which are strongly-typed and need to be defined in advance (much like CLR classes/structs). It could be interesting to look into possible conversions between composite and record types.
roji commentedon Mar 1, 2019
Note: at the type level ValueTuples have no member names whatsoever, only
Item1,Item2. This means that we can't match CLR names to PostgreSQL composite field names, as we currently do. We can still do positional binding, where it's the user's responsibility to have tuple members in the exact same order as they are defined in the database, but this seems like it could be risky.If the tuple's field types don't match the composite's, we can raised an exception, no problem. But if there are several fields with the same type, it's easy to imagine values getting written/read into the wrong field... I also think that not many users really understood that value tuples have no names under the hood, and this could open the door to lots of confusion and bugs.
@austindrenski @YohDeadfall would be good to hear some opinions. If we think it's too dangerous we can also drop the current dynamic support for unmapped composites, which I don't think it used very much (and which I really dislike).
austindrenski commentedon Mar 3, 2019
Sigh, I think you're right on this one. As you mentioned above,
ValueTupleseems to be a better fit for modeling PostgreSQL records anyways.I agree that this sounds like it would create too much confusion. I'm also on board with dropping all dynamic support across the board, here included.
roji commentedon Mar 8, 2019
Thanks @austindrenski, any thoughts @YohDeadfall?
roji commentedon Mar 18, 2019
@YohDeadfall ping
YohDeadfall commentedon Mar 18, 2019
Sorry, completely forgot about it. With the
ROWconstruction in PostgreSQL it's possible to shoot your legs too, so I see no reason why we should prevent users from doing the same thing.roji commentedon Mar 19, 2019
@YohDeadfall I think the situation is a bit different...
ROWis just a literal representation (and you're right that there's no names), but the PostgreSQL type definition is still a composite type with field names. On the .NET side, ValueTuple never has any names - it's not just an issue with the literal representation, but with the actual type.Imagine some PostgreSQL composite type consisting of 2 strings. If we allow mapping this to a
ValueTuple<string, string>, it means that the order of definition of the PostgreSQL composite type fields determines which goes where - this seems very dangerous to me.roji commentedon Mar 19, 2019
It also doesn't seem that much to ask users to define a small type (class or struct) to be able to interact with PostgreSQL composite types...
YohDeadfall commentedon Mar 19, 2019
Nope, it's just a tuple (:
I think, users should understand what they are writing too and choose right tools for that. It's dangerous, but it perfectly fits some scenarios where you don't want to write a whole POCO for one SQL query. Maybe we can skip this tuple handling request since there are extremely limited number of cases.
By the way, there's a proposal which should bring us records like in F#. See dotnet/csharplang#39.
roji commentedon Mar 19, 2019
I know. The issue is that in PostgreSQL you also have the composite type, which is strongly typed and has defined names. On the .NET side there would only be ValueTuple, which does not.
I'll say it differently... This proposal is about having Npgsql read and write composite types (again, which have names) as .NET ValueTuples (which do not) - it seems like a mismatch. The fact that
ROW()exists in PostgreSQL doesn't necessarily seem very relevant...I'm not saying this wouldn't be useful. But the potential for trouble seems to far outweigh the benefits. As you say, the number of cases where this would help seems limited, but I suspect a lot of people would try to use it in unjustified cases (i.e. where a POCO would be better) and shoot themselves in the foot.
YohDeadfall commentedon Mar 20, 2019
Then closing this issue. If someone requests this we can change our decision.