Skip to content

Support unmapped composites as dynamic #1746

Closed

Description

@roji

Npgsql currently supports unmapped enums as simple strings. This removes the need to define a CLR enum and can help in some dynamic scenarios where the database schema isn't known ahead of time.

Try to implement the same for composite types, reading and writing them as type dynamic.

The main blocker here is that unlike all other types, composite definitions are loaded lazily by Npgsql, rather than at first-connection-time. This was done since a composite type is created for each table, leading to a quick explosion of composite types and an unreasonable start time (see #1126). However, it's easy to distinguish real composite types from table types by joining the pg_type entry with pg_class and checking relkind (see https://www.postgresql.org/docs/current/static/catalog-pg-class.html). So we could switch back to eager loading for composites and then return dynamic.

The only casualty here would be mapping tables as composite types (#990), since we'd stop loading type definitions for those. We could leave lazy/late type loading just for this case, but it a little too rare/exotic and it seems better to simplify Npgsql to be eager-loading only.

Inspired by #1736.

Activity

  1. added this to the milestone on Dec 13, 2017
  2. self-assigned this
    on Dec 13, 2017
  3. roji commented on Dec 13, 2017

    @roji
    MemberAuthor

    One issue here would be how to write unmapped composites. In the general case, Npgsql either infers the PostgreSQL type from the provided CLR type - but with a dynamic object obviously that isn't possible - or allow the user to specify the PostgreSQL type via NpgsqlDbType and SpecificType (for enums and composites). However, the whole point here is to avoid having to define a CLR type, so there's nothing to set SpecificType to.

    We could add a simple string property to NpgsqlParameter (e.g. TypeName) which Npgsql would use. That's not amazing - a whole property just for writing unmapped enum/composite - but it's possible.

    More in general, a simple string to specify the database type seems much more appropriate than the ADO.NET standard DbType/NpgsqlDbType enums, which are closed and do not work for more sophisticated types (enums/composites). Will open an issue about this general problem in ADO.NET.

  4. roji commented on Dec 13, 2017

    @roji
    MemberAuthor

    General ADO.NET issue for adding string-based alternative to NpgsqlDbType: https://github.com/dotnet/corefx/issues/25891. We can still implement this as an Npgsql-specific feature without waiting for ADO.NET adoption.

    This even makes general sense over NpgsqlDbType, allowing extension to support new types without getting stuck on NpgsqlDbType which, being an enum, isn't extensible.

  5. Brar commented on Dec 14, 2017

    @Brar
    Member

    We could leave lazy/late type loading just for this case, but it a little too rare/exotic and it seems better to simplify Npgsql to be eager-loading only.

    Please think about this twice.

    IMO the ability to map tables as composite types is a widely unknown feature of Npgsql that provides very slim and easy ORM functionality.
    I could demostrate you quite a few cases where this is actually really useful.

  6. roji commented on Dec 14, 2017

    @roji
    MemberAuthor

    We could leave lazy/late type loading just for this case, but it a little too rare/exotic and it seems better to simplify Npgsql to be eager-loading only.

    Please think about this twice.

    IMO the ability to map tables as composite types is a widely unknown feature of Npgsql that provides very slim and easy ORM functionality.
    I could demostrate you quite a few cases where this is actually really useful.

    I agree it's a pretty cool feature, it's just that it really seems really, really rarely used.

    To be frank, changing the type loading system to be 100% eager would also simplify things inside Npgsql - composites are now the only types loaded lazily, and it's kind of a wart on the system. And the only reason composites are loaded lazily is because there can be so many of them, because a composite type exists per table.

    One last option here, which in general I try to avoid as possible, is to simply add a parameter, Load All Composites, which would be false by default. Thus, everything will always be loaded eagerly, and if you happen to want to use the table-as-composite feature, you'd have to add this parameter.

    In any case, I couldn't resist and have started working on this issue, and it's going quite well - I already have tests parsing unmapped composites as dynamic ExpandoObjects. It's a real pleasure to see and to use.

  7. Brar commented on Dec 14, 2017

    @Brar
    Member

    One last option here, which in general I try to avoid as possible, is to simply add a parameter, Load All Composites, which would be false by default. Thus, everything will always be loaded eagerly, and if you happen to want to use the table-as-composite feature, you'd have to add this parameter.

    👍

    In any case, I couldn't resist and have started working on this issue, and it's going quite well - I already have tests parsing unmapped composites as dynamic ExpandoObjects. It's a real pleasure to see and to use.

    Great to hear :)

  8. dmitry-a-morozov commented on Dec 15, 2017

    @dmitry-a-morozov
    Contributor

    Looks good.
    As I mentioned before SqlClient solves #1746 (comment) via read-write UdtTypeName property. Yes, maybe it makes standardized it on ADO.NET level.
    Only unfortunate thing that it will be confusing to have all: EnumType, SpecificType, DbType, PostgresType and TypeName (or UdtTypeName) on NpgsqlParameter. :(
    I know that EnumType is deprecated but still adds to perception complexity.

  9. roji commented on Dec 15, 2017

    @roji
    MemberAuthor

    @dmitry-a-morozov - ah, thanks for the added information: I didn't know about UdtTypeName...

    I think if we implement UdtTypeName (or TypeName) we can safely obsolete SpecificType as well. I hope that will at least reduce the confusion around those two (which we can also remove in a later release). Then there's the question of PostgresType, which we proposed to add to NpgsqlParameter only for derivation (so as an "output property"), although we could also allow it to be set by the user to determine the outgoing parameter name as well.

    Yeah, we definitely will have to add some clear documentation on all this.

  10. dmitry-a-morozov commented on Dec 15, 2017

    @dmitry-a-morozov
    Contributor

    I think UdtTypeName is more like DataTypeName e.g. full data type name as in database including schema. Where is SpecificType is CLR mapped type. They are complementary not redundant, correct?

    Having only UdtTypeName is sufficient for my purposes, no need for PostgresType.

    Interesting code snippet related to the discussion.
    http://referencesource.microsoft.com/#System.Data/System/Data/SqlClient/SqlCommandBuilder.cs,160

  11. roji commented on Dec 15, 2017

    @roji
    MemberAuthor

    @dmitry-a-morozov yes, UdtTypeName is a string property with a full database-specific type name, where SpecificType is an CLR type that has already been mapped to the type name. But at the end of the day, what we need is a simple way for users to tell Npgsql which database type to send. If there's a simple, direct string-based way to do so (i.e. UdtTypeName), then SpecificType really provides no additional value. It's just another layer of indirection, since in any case the CLR type has to have already been mapped to... the string name of the database type...

    So I'm leaning towards obsoleting SpecificType in favor of UdtTypeName (or whatever we choose to call it).

  12. roji commented on Dec 15, 2017

    @roji
    MemberAuthor

    Thanks for the code snipped from SqlClient. I'm thinking to do thinks a little bit different, and to allow specifying the type as a string for all database types, not just UDT ones (which seem to correspond PostgreSQL composites). I don't see any reason to restrict this mechanism only to composites, and it could actually be useful for other types added by extensions, and which aren't present in the closed NpgsqlDbType enum. This also means that the property should be named something like TypeName rather than UdtTypeName, because it isn't necessarily related to user-defined types.

  13. dmitry-a-morozov commented on Dec 15, 2017

    @dmitry-a-morozov
    Contributor

    Makes sense. Will TypeName be mandatory for parameters of simple types like int, decimal, string etc?

  14. roji commented on Dec 16, 2017

    @roji
    MemberAuthor

    @dmitry-a-morozov definitely not - that would break all existing Npgsql code. The idea would be for TypeName to be (yet) another way for users to specify which type they want to write. Npgsql would look in the following order:

    • If TypeName is set, use that
    • Otherwise, if NpgsqlDbType is set, use that. If it's set to composite or enum, also look at SpecificType (although this would be obsoleted)
    • Otherwise, if DbType is set, use that (for database-independent programming)
    • Otherwise, infer from the CLR type of Value (this actually works quite well for mapped composites and enums, so SpecificType is actually kinda useless).
  15. 1 remaining item

  16. roji commented on Dec 17, 2017

    @roji
    MemberAuthor

    In PostgreSQL, array names are simply the type name with an underscore prepended to it. So an array of text is _text, and an array of your custom enum my_enum is _my_enum.

    FYI as we're adding the PostgresType property to NpgsqlParameter, I'm considering allowing that to be used as well to specify the type of the outgoing parameter. If we do that, then TypeName would be a thin wrapper which simply looks up the PostgresType by name and sets it.

  17. added a commit that references this issue on Jan 18, 2018
  18. roji commented on Jan 18, 2018

    @roji
    MemberAuthor

    The bulk of the work has been merged - along with some other changes to the type mapping system.

    • Composites can be read and written as any CLR type without mapping them first.
    • Mapping is still optional to make the CLR type the default when reading, and also to allow omitting specifying DataTypeName on the parameter when writing.
    • Support added for reading and writing unmapped composites as ExpandoObjects, i.e. when NpgsqlDataReader.GetValue() is called on an unmapped composites.
    • Composite type definitions are now eagerly loaded, just like all other types.
    • By default, only "free-standing" composite types are loaded; composite types corresponding to PostgreSQL tables are only loaded if the Load All Composites parameter is set.

    Leaving open this issue for the following remaining work:

    • Remaining work: add two-way name translation to INpgsqlNameTranslator, necessary for reading unmapped composites dynamically. Right now dynamically-read composites don't have their names translated.
    • Expose rich information about composite and enum types from PostgresType (Expose rich information about composite and enum types from PostgresType #1779).
    • Note that reading arrays of unmapped composites doesn't work, and would require significant changes in ArrayHandler. Will wait to see if people actually request this.
  19. roji commented on May 12, 2018

    @roji
    MemberAuthor

    Split off the remaining name translation work to #1929

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions