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
Npgsql.NpgsqlCommand.DeriveParametersForQuery fails on un-mapped Enums #1736
at Npgsql.TypeMapping.ConnectorTypeMapper.GetNpgsqlTypeByOid(UInt32 oid)
at Npgsql.NpgsqlCommand.DeriveParametersForQuery()
at Npgsql.NpgsqlCommand.DeriveParameters()
at Npgsql.NpgsqlCommandBuilder.DeriveParameters(NpgsqlCommand command)
...
Other details:
I think it fails for mapped enums too with
System.InvalidOperationException: Invalid parameter type: 55253
at Npgsql.TypeMapping.ConnectorTypeMapper.GetNpgsqlTypeByOid(UInt32 oid)
at Npgsql.NpgsqlCommand.DeriveParametersForQuery()
at Npgsql.NpgsqlCommand.DeriveParameters(
@dmitry-a-morozov FYI it's not a good idea to work with 3.3.0-command-caching (although I know it's the one that appears on the top of the nuget list) - work with the latest 3.3.0-ci instead.
Yes I'll have a look.This is probably a problem that also exists for parameter derivation from functions (also for composite types) I remember a todo comment somewhere.
@Brar It also fails for array types like interger[] too with
System.InvalidOperationException
HResult=0x80131509
Message=No mapping found for PostgreSQL type _int4
Source=Npgsql
StackTrace:
at Npgsql.TypeMapping.ConnectorTypeMapper.GetNpgsqlTypeByOid(UInt32 oid)
at Npgsql.NpgsqlCommand.DeriveParametersForQuery()
at Npgsql.NpgsqlCommand.DeriveParameters()
at Npgsql.NpgsqlCommandBuilder.DeriveParameters(NpgsqlCommand command)
@Brar It works for arrays. For example, a parameter of integer[] has correctly inferred to value NpgsqlDbType.Array | NpgsqlDbType.Integer of NpgsqlParameter.NpgsqlDbType property.
Not so much for enumerations. I hoped to get NpgsqlParameter.NpgsqlDbType set to NpgsqlDbType.Enum and NpgsqlParameter.SpecificType set to a name of custom enumeration but instead I get NpgsqlParameter.NpgsqlDbType equals to NpgsqlDbType.Text and NpgsqlParameter.SpecificType is null.
I hoped to get NpgsqlParameter.NpgsqlDbType set to NpgsqlDbType.Enum and NpgsqlParameter.SpecificType set to a name of custom enumeration but instead I get NpgsqlParameter.NpgsqlDbType equals to NpgsqlDbType.Text and NpgsqlParameter.SpecificType is null.
I could easily fix NpgsqlParameter.NpgsqlDbType, as it is a manual assignment but I can't see a way to infer NpgsqlParameter.SpecificType for an unmapped enum. There simply isn't a way how we could know the type to infer from inside Npgsql code. @roji - is the behavior cited above (set NpgsqlParameter.NpgsqlDbType to NpgsqlDbType.Enum also for unmapped Enums) ok/intended? Could we leave NpgsqlParameter.SpecificType empty until an actual enum value is set to the parameter and infer it at this time?
@Brar You're right NpgsqlParameter.SpecificType property cannot be set because it requires actual .NET type. My bad, I thought it's of type string. Is there way to store enum name somewhere in NpgsqlParameter instance as it appears in database?
Hmm... Npgsql does support reading/writing unmapped enums as text. For reading this is pretty straightforward, but for writing IIRC you need to leave NpgsqlDbType to Unknown (i.e. if you set it to NpgsqlDbType.Text you'll get a PostgreSQL cast error).
This makes it a bit tricky... If the enum has been mapped, we should of course infer everything (NpgsqlDbType, SpecificType) because we can. But what to do about the unmapped case? IIRC trying to send a parameter with NpgsqlDbType set to Enum, but without also setting SpecificType results in a failure - you must either set both or set none - so it doesn't make much sense to me to do this. I think maybe it's just best to leave at NpgsqlDbType Unknown...
If not, maybe it's better to leave NpgsqlDbType at Unknown
For reading this is pretty straightforward, but for writing IIRC you need to leave NpgsqlDbType to Unknown (i.e. if you set it to NpgsqlDbType.Text you'll get a PostgreSQL cast error).
Well, it obviously depends on what value you are actually trying to write or, in other words, what your client side representation of the PostgreSQL enum is. If it is an actual .Net enum you probably can't send it as NpgsqlDbType.Text but chances are, that your client side representation is just simple string values as it is the case in the test DeriveParametersCommandTypeTextUnmappedEnum in #1740 which would certainly work. After all there probably is a reason why you didn't map the enum in the first place (i. e. targeting multiple RDBMS where most of them don't have a concept of mappable enums).
Anyways, this are just my assumptions of probable use cases and I could certainly live with returning NpgsqlDbType.Unknown for unmapped enums.
What do you think of the feature request from @dmitry-a-morozov about somehow storing the database type name in instances of NpgsqlParameter.
While it is certainly possible to add a Property to NpgsqlParameter and set it when deriving parameters it will add complexity and possible corner cases, which is why I'm uncertain if the use case justifies the addition of this feature.
Perhaps we should get to know more about the actual use case.
Well, it obviously depends on what value you are actually trying to write or, in other words, what your client side representation of the PostgreSQL enum is. If it is an actual .Net enum you probably can't send it as NpgsqlDbType.Text but chances are, that your client side representation is just simple string values as it is the case in the test DeriveParametersCommandTypeTextUnmappedEnum in #1740 which would certainly work. After all there probably is a reason why you didn't map the enum in the first place (i. e. targeting multiple RDBMS where most of them don't have a concept of mappable enums).
If I understand you correctly, then I pretty much agree. If there is an actual .NET enum that it should be mapped before derivation, in which case there's no problem. Otherwise, if a mapping hasn't been set up, I think it's safe to assume that interaction will be done via text, and the one thing I expect from the derivation process is to set up my NpgsqlParameter so that setting the value (i.e. to a string) would just work.
What do you think of the feature request from @dmitry-a-morozov about somehow storing the database type name in instances of NpgsqlParameter.
While of course this is possible, it seems really odd to add a string property to NpgsqlParameter for the sole reason of... deriving parameter types for unmapped enums...! That seems like the most corner case scenario ever.
Once again, I think we should always keep in mind what a program could do with the information provided by the deriving process. What could a program possibly do with a string type name, except maybe show it to the user? It seems quite useless to me.
Ok, I've implemented NpgsqlParameter.PostgresType as discussed here (see #1740 ) except lazily loading of composite types, which will take some more time.
@dmitry-a-morozov thanks ;)
Actually mapped composite types should already work perfectly. It's only unmapped composite types that currently don't get loaded by Npgsql.
After reading carefully I've found another suggestion that isn't implemented yet:
However, this makes me think that Npgsql could be providing you with more information about the types (e.g. enum values, composite fields and their types), to save you the trouble of going and getting that information from the database yourself. It's a matter of making the PostgresType type hierarchy expose richer information.
While I made the composite type fields public in my PR I don't see any way to access enum values yet.
This might also justify another pull request.
@roji I agree with @dmitry-a-morozov about opening a separate PR for the remaining work and possibly even a new issue for discussion. After all, this one is marked as bug, which should be resolved by now and the more we discuss the more features creep into the PR which will probably increase the time you need to review it.
@dmitry-a-morozov, no, there's no fallback for unmapped composites, and in fact having one isn't really possible. Enums are represented in the binary wire encoding as a simple string, which is why we can have the enum-as-string fallback - the user simply sends a string flagged as Unknown and postgresql performs an implicit cast.
Composites, on the other hand, have a complex binary representation (number of fields, type code for each string, etc.). We would indeed have to add Npgsql support for handling unmapped composites, e.g. as dictionaries or even dynamic. That would be yet another PR.
OK, great. As suggested above, let's do this step by step. Providing a PostgresType for composites is one thing, exposing more info from PostgresType is another, and having Npgsql support unmapped composites is yet another...
@Brar and @dmitry-a-morozov, check out #1746 for supporting unmapped composites as dynamic. I'm still not 100% what I want to do there but I'll probably end up implementing it. As it touches composite support in a more fundamental manner it's probably a good idea for me to tackle, although @Brar if you want a go at that issue as well let me know.
As it touches composite support in a more fundamental manner it's probably a good idea for me to tackle, although @Brar if you want a go at that issue as well let me know.
Please go ahead. I'm currently working on nullable arrays which is hard enough for now.
Activity
roji commentedon Dec 2, 2017
@Brar, want to take a look?
@dmitry-a-morozov FYI it's not a good idea to work with 3.3.0-command-caching (although I know it's the one that appears on the top of the nuget list) - work with the latest 3.3.0-ci instead.
dmitry-a-morozov commentedon Dec 2, 2017
I was using 3.3.0-ci but switched to 3.3.0-command-caching hoping that it has the problem fixed
Brar commentedon Dec 2, 2017
dmitry-a-morozov commentedon Dec 4, 2017
@Brar It also fails for array types like
interger[]too withBrar commentedon Dec 4, 2017
Thanks @dmitry-a-morozov for reporting this. I'll look at all the non-commonplace-types while at it.
Brar commentedon Dec 4, 2017
@dmitry-a-morozov please test #1740 if it solves the problems for you.
dmitry-a-morozov commentedon Dec 5, 2017
@Brar It works for arrays. For example, a parameter of
integer[]has correctly inferred to valueNpgsqlDbType.Array | NpgsqlDbType.IntegerofNpgsqlParameter.NpgsqlDbTypeproperty.Not so much for enumerations. I hoped to get
NpgsqlParameter.NpgsqlDbTypeset toNpgsqlDbType.EnumandNpgsqlParameter.SpecificTypeset to a name of custom enumeration but instead I getNpgsqlParameter.NpgsqlDbTypeequals toNpgsqlDbType.TextandNpgsqlParameter.SpecificTypeisnull.Brar commentedon Dec 5, 2017
I could easily fix
NpgsqlParameter.NpgsqlDbType, as it is a manual assignment but I can't see a way to inferNpgsqlParameter.SpecificTypefor an unmapped enum. There simply isn't a way how we could know the type to infer from inside Npgsql code.@roji - is the behavior cited above (set
NpgsqlParameter.NpgsqlDbTypetoNpgsqlDbType.Enumalso for unmapped Enums) ok/intended? Could we leaveNpgsqlParameter.SpecificTypeempty until an actual enum value is set to the parameter and infer it at this time?dmitry-a-morozov commentedon Dec 5, 2017
@Brar You're right
NpgsqlParameter.SpecificTypeproperty cannot be set because it requires actual .NET type. My bad, I thought it's of typestring. Is there way to store enum name somewhere inNpgsqlParameterinstance as it appears in database?roji commentedon Dec 5, 2017
Hmm... Npgsql does support reading/writing unmapped enums as text. For reading this is pretty straightforward, but for writing IIRC you need to leave NpgsqlDbType to
Unknown(i.e. if you set it toNpgsqlDbType.Textyou'll get a PostgreSQL cast error).This makes it a bit tricky... If the enum has been mapped, we should of course infer everything (
NpgsqlDbType,SpecificType) because we can. But what to do about the unmapped case? IIRC trying to send a parameter with NpgsqlDbType set toEnum, but without also settingSpecificTyperesults in a failure - you must either set both or set none - so it doesn't make much sense to me to do this. I think maybe it's just best to leave at NpgsqlDbTypeUnknown...If not, maybe it's better to leave NpgsqlDbType at
UnknownBrar commentedon Dec 5, 2017
Well, it obviously depends on what value you are actually trying to write or, in other words, what your client side representation of the PostgreSQL enum is. If it is an actual .Net enum you probably can't send it as NpgsqlDbType.Text but chances are, that your client side representation is just simple string values as it is the case in the test
DeriveParametersCommandTypeTextUnmappedEnumin #1740 which would certainly work. After all there probably is a reason why you didn't map the enum in the first place (i. e. targeting multiple RDBMS where most of them don't have a concept of mappable enums).Anyways, this are just my assumptions of probable use cases and I could certainly live with returning
NpgsqlDbType.Unknownfor unmapped enums.What do you think of the feature request from @dmitry-a-morozov about somehow storing the database type name in instances of
NpgsqlParameter.While it is certainly possible to add a Property to
NpgsqlParameterand set it when deriving parameters it will add complexity and possible corner cases, which is why I'm uncertain if the use case justifies the addition of this feature.Perhaps we should get to know more about the actual use case.
dmitry-a-morozov commentedon Dec 5, 2017
Primary use case:
I'm building F# type provider for postgres similar to one I did for Sql Server
http://fsprojects.github.io/FSharp.Data.SqlClient/
It's like code generation on steriods.
Based on a query command type generated at design time with Execute/AsyncExecute methods. During execution NpgsqlCommand is created, proper parameters set up and values passed into Execute mapped to parameter values.
So, I'm going backward - parameters and enums created based on meta-information received from DeriveParameters.
I do think that having
stringtype property that contains user defined type is useful because an expectation that there real type exists is a little restrictive. It will mirror behavior ofNpgsqlDataReaderwhere this info available for output columns. Also that how SqlClient solves the issue:https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlparameter.udttypename?view=netframework-4.7.1&viewFallbackFrom=netstandard-2.0
@Brar My understanding that mapped enums still fail. Is it correct?
roji commentedon Dec 5, 2017
If I understand you correctly, then I pretty much agree. If there is an actual .NET enum that it should be mapped before derivation, in which case there's no problem. Otherwise, if a mapping hasn't been set up, I think it's safe to assume that interaction will be done via text, and the one thing I expect from the derivation process is to set up my NpgsqlParameter so that setting the value (i.e. to a string) would just work.
While of course this is possible, it seems really odd to add a string property to NpgsqlParameter for the sole reason of... deriving parameter types for unmapped enums...! That seems like the most corner case scenario ever.
Once again, I think we should always keep in mind what a program could do with the information provided by the deriving process. What could a program possibly do with a string type name, except maybe show it to the user? It seems quite useless to me.
16 remaining items
Brar commentedon Dec 10, 2017
Ok, I've implemented NpgsqlParameter.PostgresType as discussed here (see #1740 ) except lazily loading of composite types, which will take some more time.
dmitry-a-morozov commentedon Dec 10, 2017
@Brar Great job !
I suggest create a separate PR for composite types so I won't be blocked on enums.
Brar commentedon Dec 10, 2017
@dmitry-a-morozov thanks ;)
Actually mapped composite types should already work perfectly. It's only unmapped composite types that currently don't get loaded by Npgsql.
After reading carefully I've found another suggestion that isn't implemented yet:
While I made the composite type fields public in my PR I don't see any way to access enum values yet.
This might also justify another pull request.
@roji I agree with @dmitry-a-morozov about opening a separate PR for the remaining work and possibly even a new issue for discussion. After all, this one is marked as bug, which should be resolved by now and the more we discuss the more features creep into the PR which will probably increase the time you need to review it.
roji commentedon Dec 11, 2017
Great job @Brar! I'll try to review and merge asap. Ok for handling the unmapped composites in a separate PR.
roji commentedon Dec 11, 2017
@dmitry-a-morozov, no, there's no fallback for unmapped composites,
and in fact having one isn't really possible. Enums are represented in the binary wire encoding as a simple string, which is why we can have the enum-as-string fallback - the user simply sends a string flagged asUnknownand postgresql performs an implicit cast.Composites, on the other hand, have a complex binary representation (number of fields, type code for each string, etc.). We would indeed have to add Npgsql support for handling unmapped composites, e.g. as dictionaries or even dynamic. That would be yet another PR.
dmitry-a-morozov commentedon Dec 11, 2017
@roji That exactly what I thought - supporting dictionaries or dynamic !
roji commentedon Dec 11, 2017
OK, great. As suggested above, let's do this step by step. Providing a PostgresType for composites is one thing, exposing more info from PostgresType is another, and having Npgsql support unmapped composites is yet another...
dmitry-a-morozov commentedon Dec 11, 2017
Agree 100%. I don't have immediate need for composite types so take you take to think it through. No rush.
Brar commentedon Dec 11, 2017
Ok, I've rebased the PR and squashed everything into one commit to make it easier to review.
Have fun @roji ;)
roji commentedon Dec 12, 2017
Thanks @Brar, will try to review as soon as I can...
roji commentedon Dec 13, 2017
@Brar and @dmitry-a-morozov, check out #1746 for supporting unmapped composites as dynamic. I'm still not 100% what I want to do there but I'll probably end up implementing it. As it touches composite support in a more fundamental manner it's probably a good idea for me to tackle, although @Brar if you want a go at that issue as well let me know.
Brar commentedon Dec 14, 2017
Please go ahead. I'm currently working on nullable arrays which is hard enough for now.
dmitry-a-morozov commentedon Dec 15, 2017
@roji Contrary to your comment here #1736 (comment) I cannot find nuget package on myget unstable for example for build https://ci.appveyor.com/project/roji/npgsql/build/3.3.0-467. Do I look in wrong place?