Skip to content

Drop unmapped enum support, but not ability to work with them as strings #2404

Closed as not planned

Description

@YohDeadfall

There is no good reason to support conversion from an unmapped enum to the corresponding CLR type, so dropping it. The user should create relationship between CLR and PostgreSQL types. Otherwise, it should be possible to work with values as strings only.

Activity

  1. added this to the 4.1 milestone on Mar 28, 2019
  2. self-assigned this
    on Mar 28, 2019
  3. roji commented on Mar 28, 2019

    @roji
    Member

    Hang on, people have actually asked for this in various places. Since enums are actually transmitted as strings over the wire, supporting unmapped enums doesn't degrade performance or complicate our lives too much (unlike unmapped composites).

  4. austindrenski commented on Mar 28, 2019

    @austindrenski
    Contributor

    There's probably some work to be done with the unmapped enum support, but dropping it altogether would pose a challenge. (Among other things, this would impact existing EF Core workloads, right?)

    But, for instance, there are a number of TODO items in the handler:

    // TODO: Avoid boxing
    return (TAny)(object)value;

    // TODO: Avoid boxing
    return _enumToLabel.TryGetValue((Enum)(object)value, out var str)
    ? base.ValidateAndGetLength(str, ref lengthCache, parameter)
    : throw new InvalidCastException($"Can't write value {value} as enum {type}");

    // TODO: This boxes the enum (again)
    protected override Task WriteWithLength<TAny>(TAny value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
    => WriteObjectWithLength(value, buf, lengthCache, parameter, async);

    // TODO: Avoid boxing
    if (!_enumToLabel.TryGetValue((Enum)(object)value, out var str))
    throw new InvalidCastException($"Can't write value {value} as enum {type}");
    return base.Write(str, buf, lengthCache, parameter, async);

  5. YohDeadfall commented on Mar 29, 2019

    @YohDeadfall
    ContributorAuthor

    What are real use cases? When isn't possible to use mapped enums instead of unmapped?

  6. Brar commented on Mar 29, 2019

    @Brar
    Member

    @YohDeadfall #1736 otherwise just search closed issues for "unmapped enum"

  7. Brar commented on Mar 29, 2019

    @Brar
    Member

    @dmitry-a-morozov you might want to chime in here and perhaps also at #2403

  8. YohDeadfall commented on Mar 29, 2019

    @YohDeadfall
    ContributorAuthor

    What was a reason to bring mapped types to Npgsql then? They are backed by unmapped type handlers which as you're saying desired by users. Does it make sense to drop mapping of types and generate handlers on the fly?

  9. YohDeadfall commented on Mar 29, 2019

    @YohDeadfall
    ContributorAuthor

    @kspeakman You are welcome here too.

  10. YohDeadfall commented on Mar 30, 2019

    @YohDeadfall
    ContributorAuthor

    Why I want to drop unmapped enum support? The reason is simple - it has no difference from mapped enum support and actually the mapped handler is backed by the unmapped one.

    Since the unmapped handler allows to pass a string as a value and read it back, we could just cut UnmappedEnumHandler to work only with strings. Is it acceptable for @dmitry-a-morozov?

    Otherwise, I see no value in mapped enums. With meta programming magic I can achieve the same performance for unmapped enums as it can be done for mapped, without any boxing. If we go this way, composite mapping can be dropped to.

  11. roji commented on Apr 1, 2019

    @roji
    Member

    @YohDeadfall I'm not sure I'm following :) Do you want to drop mapped, or unmapped enums? :)

    I think you may be saying that we can drop the mapping step, and just allow users to read and write CLR enums (and possibly composites) directly. The type handler would no longer be instantiated for one specific CLR enum type, but would rather accept any for writing and reading. If you think we can do this, retain current APIs (allow users to read/write both CLR enum types and strings) and maintain the same perf, then why not. The idea with a separate, explicit mapping step was to allow us to prepare a type handler that's optimized for the given CLR type (exactly the kind of work you're doing in the composite handler, generating delegates etc.).

    If, on the other hand, you think we should eliminate the possibility of reading/writing both CLR enums and strings, then I'm not so sure. Mapped enums (and composites) are an important feature - they allow users to use their strongly-typed CLR type all the way to the database. They can define a regular, CLR enum and use that in their application, and also write it into the database. This isn't about performance - it's about providing a good API.

    Unmapped enums indeed seem less important to me - but many users use these, and it seems that some layers (e.g. @dmitry-a-morozov's) depend on them. The advantage for layers is that you don't need to somehow generate a CLR enum in order to read/write an enum from PostgreSQL. Since the wire format for enums is a string, this also doesn't imply any perf degradation compared to a real enum type.

    Otherwise, I see no value in mapped enums. With meta programming magic I can achieve the same performance for unmapped enums as it can be done for mapped, without any boxing. If we go this way, composite mapping can be dropped to.

    This seems to be talking about the internal implementation of our handlers - I definitely don't have any problems with that.

  12. changed the title [-]Drop unmapped enum support[/-] [+]Drop unmapped enum support, but not ability to work with them as strings[/+] on Apr 1, 2019
  13. YohDeadfall commented on Apr 1, 2019

    @YohDeadfall
    ContributorAuthor

    Do you want to drop mapped, or unmapped enums?

    Just one of them. The original proposal was about cutting off reading and writing enum values as CLR types. Sorry for being unclear, updated the description.

    I think you may be saying that we can drop the mapping step

    Yes, it's a second way which I can implement. This is how serializers work.

  14. roji commented on Apr 1, 2019

    @roji
    Member

    Do you want to drop mapped, or unmapped enums?

    Just one of them. The original proposal was about cutting off reading and writing enum values as CLR types. Sorry for being unclear, updated the description.

    So re dropping the ability to work with mapped and unmapped enums, I don't see why we should. Mapped CLR enums are obviously are better way for users to interact with a database driver, simply because CLR enums are how enums are represented in any .NET application. Unmapped enums seem useful for cases where you don't want to use a CLR type (or can't), e.g. layers.

    Re dropping the mapping step, which is a totally different thing... Another point I forgot to mention above is to allow users to say which PostgreSQL enum corresponds to their CLR enum - the naming may be completely different. It's true we could achieve the same with an attribute ([PgName]) but a method-based option also makes sense - some users (like me) don't like to pollute their CLR objects with mapping attributes (see how EF Core supports both a fluent API and attributes).

  15. 13 remaining items

  16. modified the milestones: 5.0, 6.0.0 on Sep 17, 2020
  17. roji commented on Sep 17, 2020

    @roji
    Member

    Rediscuss for 6.0 whether we want to do this or not.

  18. roji commented on Sep 26, 2020

    @roji
    Member

    @YohDeadfall although this issue isn't for 5.0, I think we should be clear about what we're discussing here... If this is just about an internal refactor between the mapped/unmapped type handlers - with no impact on the user - then I personally have no problem problem with anything. This is what it seems like from your code sample above. If this is the case, we can rename this issue to "Refactor unmapped enum support" and remove the breaking change label.

    If, on the other hand, you're proposing to remove some ability which users currently have (breaking change), then I still don't understand what that ability is, and why we want to remove it.

  19. YohDeadfall commented on Oct 12, 2020

    @YohDeadfall
    ContributorAuthor

    On handlers am working on and which had no time to publish (again), there is no way to support this feature because of lack of generic dispatch.

  20. daz10000 commented on Oct 14, 2020

    @daz10000

    I'm sorry I haven't had a chance to test this assumption or comment, but from reading the thread, I think there's a good chance this will completely break enum support in https://github.com/demetrixbio/FSharp.Data.Npgsql because we handle enums generically.

  21. Brar commented on Oct 14, 2020

    @Brar
    Member

    @daz10000 I think you can try https://github.com/YohDeadfall/npgsql/tree/drop-unmapped-enum now to verify your assumptions.

    I'm still not convinced that this is going to happen.
    Especially if it breaks existing features in dependent code without providing a workaround.

    The only reason pro this that we've heard yet is from
    @YohDeadfall:

    On handlers am working on and which had no time to publish (again), there is no way to support this feature because of lack of generic dispatch.

    We haven't seen the new handler design yet but in my opinion there should be a pretty high bar for removing existing and used functionality because of a redesign of an internal API.
    I wouldn't say it can't happen but there need to be very clear and measurable advantages that would justify this.

  22. YohDeadfall commented on Oct 14, 2020

    @YohDeadfall
    ContributorAuthor

    You had a chance, I published an archive in our private channel (: But to be short, new handlers are strongly typed and optimized to have only one virtual call without generic dispatch, so each pair of CLR and PostgreSQL types has their own handler produced by a factory.

    @dmitry-a-morozov and @daz10000, do I correctly understand that the library uses unmapped enums and there is no way to the user to specify which types should be mapped?

  23. melanore commented on Oct 14, 2020

    @melanore

    @YohDeadfall, correct. The purpose of the type provider is to generate strongly typed access to db based on schema definitions. Users do not know enums that exist in db beforehand.

  24. YohDeadfall commented on Oct 14, 2020

    @YohDeadfall
    ContributorAuthor

    Does it generates wrappers around Npgsql or is it a proxy? Or would be better to ask how does it work?

  25. modified the milestones: 6.0.0, Backlog on Oct 5, 2021
  26. NinoFloris commented on Aug 15, 2023

    @NinoFloris
    Member

    This hasn't happened, and we won't be making this change. We'd likely even expand support as detailed by #3303 to align better with other ADO.NET providers

  27. removed this from the Backlog milestone on Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions