Skip to content

Mapping PostgreSQL composites to .NET value tuples #2097

Closed

Description

@austindrenski

moved from npgsql/efcore.pg#563

Could we map otherwise unrecogized instances of ValueTuple to row constructors?

var test = ("red", 4, 2.2);
ctx.SomeEntity.Where(x => (x.Color, x.Price, x.Tax) == test);
SELECT *
FROM "SomeEntity" as x
WHERE ROW(x."Color", x."Price", x."Tax") = @__test_0;

/cc @roji

Related

https://www.postgresql.org/docs/10/static/functions-comparisons.html#ROW-WISE-COMPARISON

Activity

  1. austindrenski commented on Aug 5, 2018

    @austindrenski
    ContributorAuthor

    @roji wrote:

    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).

    So I'd option an issue on http://github.com/npgsql/npgsql/ to track composite mapping to value tuples and concentrate on that first. Once that's done, we can (finally) look at composite support in the EF Core provider (#22), and do the ROW(...) literal representation as part of that. Sounds good?

    moved from npgsql/efcore.pg#563

  2. austindrenski commented on Aug 5, 2018

    @austindrenski
    ContributorAuthor

    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.

  3. roji commented on Aug 6, 2018

    @roji
    Member

    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.
  4. changed the title [-]Support mapping ValueTuple with row constructors[/-] [+]Mapping PostgreSQL composites to .NET value tuples[/+] on Aug 6, 2018
  5. austindrenski commented on Aug 6, 2018

    @austindrenski
    ContributorAuthor

    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.)

  6. YohDeadfall commented on Aug 6, 2018

    @YohDeadfall
    Contributor

    They are structs, so they can't have a base type. But they implement non-generic ITuple.

  7. roji commented on Aug 6, 2018

    @roji
    Member

    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:

    var tup1 = reader.GetFieldValue<(int id, string name)>(0);
    var tup2 = ((int id, string name))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.

  8. added this to the 4.1 milestone on Aug 14, 2018
  9. roji commented on Nov 18, 2018

    @roji
    Member

    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.

  10. roji commented on Mar 1, 2019

    @roji
    Member

    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).

  11. austindrenski commented on Mar 3, 2019

    @austindrenski
    ContributorAuthor

    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.

  12. roji commented on Mar 8, 2019

    @roji
    Member

    Thanks @austindrenski, any thoughts @YohDeadfall?

  13. roji commented on Mar 18, 2019

    @roji
    Member
  14. YohDeadfall commented on Mar 18, 2019

    @YohDeadfall
    Contributor

    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.

  15. roji commented on Mar 19, 2019

    @roji
    Member

    @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.

  16. roji commented on Mar 19, 2019

    @roji
    Member

    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...

  17. YohDeadfall commented on Mar 19, 2019

    @YohDeadfall
    Contributor

    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.

  18. roji commented on Mar 19, 2019

    @roji
    Member

    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.

  19. YohDeadfall commented on Mar 20, 2019

    @YohDeadfall
    Contributor

    Then closing this issue. If someone requests this we can change our decision.

  20. removed this from the 4.1 milestone on Jun 1, 2019
  21. 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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions