Skip to content

Type handler redo - #3956

Merged
roji merged 15 commits into
mainfrom
TypeResolvers
Sep 8, 2021
Merged

roji merged 15 commits into
mainfrom
TypeResolvers

Conversation

@roji

@roji roji commented Aug 30, 2021

Copy link
Copy Markdown
Member

For #3953, any/all feedback welcome!

This is currently in a half-half state: reading and writing does go through the new system, but the old system is still in place to make sure everything compiles and most tests run. I suggest looking at BuiltInTypeHandlerResolver to get a sense of it, and work out from there.

I reimplemented enum handling (which was a bit tricky but OK). But one problematic aspect is converting NpgsqlDbType/DbType/ClrType/DataTypeName on NpgsqlParameter (i.e. when you set DbType on NpgsqlParameter, all the rest are supposed to be populated accordingly). The problem with this is that it's supposed to work without any connection (i.e. free-floating NpgsqlParameter instance). Up to now this has been implemented at the global type mapper level, but we no longer have type mappings to support that. We could still have an API on ITypeHandlerResolverFactory to do these translations, but then we'd have to basically repeat all the type mappings again: once to get a type handler (for normal operation), once to get the conversion (for NpgsqlParameter). Basically I don't have any good ideas how to do this yet.

@roji
roji requested a review from vonzshik as a code owner August 30, 2021 19:29
@roji

roji commented Aug 31, 2021

Copy link
Copy Markdown
Member Author

@vonzshik don't worry too much about compilation issues and small fixes - this isn't ready for that kind of review yet anyway, I'll clean all that up before marking this ready for review.

The thing I'm a bit stuck on is what I wrote above; right now I don't see how I can avoid duplicating lots of resolution code in order to support the necessary conversions between NpgsqlDbType/DbType/ClrType/DataTypeName for NpgsqlParameter, so in effect we'd have two duplicate paths: one to get a type handler given e.g. a NpgsqlDbType, and another to get "type mapping info" given a NpgsqlDbType, which would allow NpgsqlParameter to know which DbType/ClrType/DataTypeName corresponds. Anything else seems like it would have to degrade perf in the type handler path (which is the critical path), which I don't really want to do for the far less important NpgsqlParameter conversions. Let me know if you have any bright ideas.

@roji

roji commented Sep 4, 2021

Copy link
Copy Markdown
Member Author

OK, this is now almost ready - there are a couple failing tests around some exotic features, but I'm pushing to get general feedback/review.

Overall I hoped this would end up nicer than it did. I do think it still considerably improves the codebase, but am open if any of you guys don't like this - don't be afraid to say.

  • Perf isn't better, but also isn't worse. There are still caching dictionaries in front of the resolver lookups, so in effect things are exactly the same as they were before; going into the resolvers on every lookup was overall not good for perf.
  • There are no more type handler factories, and lots of tricky logic to manage mappings is now gone - that's the good part. Handlers are just instantiated directly in resolvers, in that sense everything is much simpler and more flexible.
  • The main thing a resolver does is provide a handler given a string data type name. There are then various translations from other things (NpgsqlDbType, DbType, CLR type) to the data type name.
  • However, there are still various scenarios beyond resolving a handler which make life difficult:
    • Converting between NpgsqlDbType/DbType/DataTypeName/ClrType on NpgsqlParameter
    • Deriving parameters for queries and functions
    • Getting the schema of a database via GetSchema (including all the types detected in the database)
  • As a result, we still have a concept of type mapping for the above things, but that doesn't participate in type handler resolution.

Let me know what you think...

@NinoFloris NinoFloris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no more type handler factories, and lots of tricky logic to manage mappings is now gone - that's the good part. Handlers are just instantiated directly in resolvers, in that sense everything is much simpler and more flexible.

This is really nice to see, cleans up this area a lot.

However, there are still various scenarios beyond resolving a handler which make life difficult:

And this is pretty ugly indeed...

From your earlier comment

The thing I'm a bit stuck on is what I wrote above; right now I don't see how I can avoid duplicating lots of resolution code in order to support the necessary conversions between NpgsqlDbType/DbType/ClrType/DataTypeName for NpgsqlParameter

Is this auto filling of the other 'type' fields an ado.net spec thing? Does something like dapper depend on it? I would be completely fine explaining to users 'since pg has an extensible type system we can't say much about these things until we can talk to the pg instance via a connection'. We could always add a static method on some support class ResolveTypeInfo(param, conn) if it's really needed in weird corner cases and ask people to call that?

@roji

roji commented Sep 5, 2021

Copy link
Copy Markdown
Member Author

Is this auto filling of the other 'type' fields an ado.net spec thing? Does something like dapper depend on it?

I'm not quite sure how standard/mandatory this is or who is using it... FWIW here is a place in EFCore.PG where we depend on it in order to generate the array NpgsqlDbType value given a DbType.

I would be completely fine explaining to users 'since pg has an extensible type system we can't say much about these things until we can talk to the pg instance via a connection'. We could always add a static method on some support class ResolveTypeInfo(param, conn) if it's really needed in weird corner cases and ask people to call that?

Would that significantly help the situation though? Even if we did this only when a connection is set, we'd still need to have the logic resolving a mapping (as opposed to a handler), so the resolver would still need to look very similar to today, except that that static methods would be instance methods right?

Also, doing this only when there's a connection would mean that NpgsqlParameter needs to be able to access the connection, which isn't the case today (it knows its NpgsqlParameterCollection, but the latter doesn't know in which command/connection it is).

@roji

roji commented Sep 5, 2021

Copy link
Copy Markdown
Member Author

@NinoFloris note the removal of the write lock in a later commit here.. ConnectorTypeMapper now only ever updates a single ConcurrentDictionary at a time (as part of the simplification here), so an additional write lock doesn't seem necessary.

roji added 3 commits September 5, 2021 20:16
The multiplexing type mapper still references the original connector
instance from which it was created (even if it's already dispoed and
never used), and the resolvers reference that connector as well, and
its DatabaseInfo.

So when reloading types, we need to replace that connector's
DatabaseInfo as well.
@roji roji changed the title Draft on type handler redo Type handler redo Sep 5, 2021
@roji roji linked an issue Sep 5, 2021 that may be closed by this pull request
1 task done
@roji

roji commented Sep 6, 2021

Copy link
Copy Markdown
Member Author

Finished the last two disabled tests, so AFAIK all current functionality should be preserved after this PR.

@roji
roji requested a review from vonzshik September 8, 2021 14:48
@roji
roji requested a review from vonzshik September 8, 2021 15:00
@roji
roji enabled auto-merge (squash) September 8, 2021 16:01
@roji
roji merged commit 2f07854 into main Sep 8, 2021
@roji
roji deleted the TypeResolvers branch September 8, 2021 16:03
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.

New code-based type handler resolution architecture

3 participants