Skip to content

Fix DeriveParameters for arrays, enums and composite types - #1740

Merged
roji merged 1 commit into
npgsql:devfrom
Brar:FixDeriveOddParameterTypes
Mar 18, 2018
Merged

roji merged 1 commit into
npgsql:devfrom
Brar:FixDeriveOddParameterTypes

Conversation

@Brar

@Brar Brar commented Dec 4, 2017

Copy link
Copy Markdown
Member

This should fix #1736

@Brar
Brar force-pushed the FixDeriveOddParameterTypes branch 2 times, most recently from 771b0e1 to 5174932 Compare December 11, 2017 21:04
@dmitry-a-morozov

Copy link
Copy Markdown
Contributor

@Brar Works great so far !
There is small confusion though. After call to Npgsql.NpgsqlCommand.DeriveParametersForQuery NpgsqlDbType property of parameter for un-mapped enums set to NpgsqlDbType.Text which is not correct and is not backward compatible. To successfully execute after values are set it must be changed to NpgsqlDbType.Unknown. Otherwise it fails. Not a blocker for me but I think worthwhile to fix.

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

@Brar, finally had time to review - thanks for this PR. You can see some minor comments and some larger questions (e.g. should be actually derive SpecificType given that we're obsoleting it).

Note that I already have some work in progress, so it's possible I'll just merge and then continue based on your work.

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.

How about renaming this to PostgresCompositeField for brevity? Also, please move this underneath PostgresCompositeType, which is the main type in this file.

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.

The TypeOID is already exposed publicly via PostgresType.OID. This property is still necessary for the two-pass loading of the composite type definition, but I think it should be kept internal.

Note that in my work to expose richer information, the composite field contains a reference to its PostgresType, allowing user to perform full reflection on PostgreSQL composite types. But you can leave that one for me.

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.

Note that in my implementation composite fields are loaded eagerly, so there's no real need for this Lazy. But leave it as-is, I will make the proper changes later.

Comment thread src/Npgsql/NpgsqlCommand.cs Outdated

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.

Nit: use var

Comment thread src/Npgsql/NpgsqlCommand.cs Outdated

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.

Nit: remove curlies

Comment thread src/Npgsql/NpgsqlCommand.cs Outdated

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.

Thanks, good improvement on exception handling

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.

Nit: remove private

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.

Please inverse the check. This would remove a level of nesting from the main code of the method.

Comment thread src/Npgsql/NpgsqlCommand.cs Outdated

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.

We indeed want all the parameter's properties to be set to the right values. However, a few comments:

  1. As we are obsoleting SpecificType in 3.3, I don't think we should go through the effort of deriving it.
  2. Note the way things work between NpgsqlDbType and DbType - setting one also sets the other. I think the same logic should be extended. In other words, in this piece of code we should simply set PostgresType. Then, code in the PostgresType setter should make sure the other fields are set. In fact, I'm still hesitating on allowing users to set PostgresType directly (and to have TypeName as a simply string-based shortcut which does the same thing).

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.

As written above, I don't think we should support deriving SpecificType, so most of this isn't needed. As it would make the whole method quite small some refactoring may be appropriate.

@Brar

Brar commented Dec 19, 2017

Copy link
Copy Markdown
Member Author

@roji given the fact that you are intending to change quite some functionality that this PR depends on, I'm not sure if it wouldn't be better to first merge your changes and after this, let me rebase my PR.
I'd rather not submit a half-baken PR that depends on functionality that is yet to merge.
Not setting SpecificType for instance would break the tests in my current PR as it currently is required to successfully execute statements containing composite types
We certainly could do it the other way around (merge the PR first and then you can change it accordingly) but I'd rather submit it in a way that works in the current state, so that you are free to do whatever you feel necessary afterwards. Also it would be more work for you in this case, which - for Npgsql as a Project - doesn't scale as well as putting slightly more work on my list.

@roji

roji commented Dec 19, 2017

Copy link
Copy Markdown
Member

@Brar as you prefer, I'll try to merge my work soon so you can rebase, hope it won't be too much breakage for you.

@dmitry-a-morozov

Copy link
Copy Markdown
Contributor

@Brar Can you help me to write a query that proves that DeriveParameters works for array of enums?
Thanks.

@Brar

Brar commented Dec 29, 2017

Copy link
Copy Markdown
Member Author

@Brar Can you help me to write a query that proves that DeriveParameters works for array of enums?
Thanks.

@dmitry-a-morozov please have a look at the tests in my pull request especially DeriveParametersCommandTypeTextUnmappedEnum and DeriveParametersCommandTypeTextMappedEnum the second parameter in both tests is an array of enums.

@dmitry-a-morozov

Copy link
Copy Markdown
Contributor

Hello @roji and @Brar. I hope you guys enjoyed holiday season.
Do you plan to move forward with this PR.
@roji is work on composites complete?
https://github.com/npgsql/npgsql/commits/composites-as-dynamic

@roji

roji commented Jan 17, 2018

Copy link
Copy Markdown
Member

@dmitry-a-morozov and @Brar, the composite and enum work is almost complete, I'll probably merge later today or tomorrow.

@Brar

Brar commented Jan 17, 2018

Copy link
Copy Markdown
Member Author

@roji OK, then I'll try to rebase this PR this weekend and afterwards address the remaining comments from your review above.

@roji

roji commented Jan 18, 2018

Copy link
Copy Markdown
Member

@Brar and @dmitry-a-morozov, have merged my work - please see these notes. Note that I haven't yet exposed information about enum and composite fields, that's planned quite soon in #1779 (@Brar, I don't think you depend on this in any way, unlike @dmitry-a-morozov).

@Brar
Brar force-pushed the FixDeriveOddParameterTypes branch from 5174932 to 36995b2 Compare January 19, 2018 17:04
@Brar

Brar commented Jan 19, 2018

Copy link
Copy Markdown
Member Author

@roji I've updated the PR.
I don't know why AppVeyor fails though.

@roji

roji commented Jan 22, 2018

Copy link
Copy Markdown
Member

Thanks @Brar, I'll try to find time as soon as possible to review. Am now working full time on Npgsql but there's a lot of other work going on.

@dmitry-a-morozov

Copy link
Copy Markdown
Contributor

Hi @roji @Brar.
No pressure but I'm letting you know that as of now my workflow flow completely broker.
I had to take git sub-module dependency on https://github.com/Brar/npgsql/commits/FixDeriveOddParameterTypes because I could not find nuget package for particular PR on appveyor (see my comment #1736 (comment))
Now that the branch is broken after re-base I'm stuck.
Please advice.

@roji

roji commented Jan 22, 2018

Copy link
Copy Markdown
Member

@dmitry-a-morozov, I'd advise keeping a submodule reference to the git commit before the rebase until I get around to looking at things (just go into the submodule directory and do git reset --hard <sha1> to the commit that worked for you.

@dmitry-a-morozov

Copy link
Copy Markdown
Contributor

I don't follow your suggestion.
In this branch
https://github.com/Brar/npgsql/commits/FixDeriveOddParameterTypes
Commit Brar@56d4034
worked for me until it got re-based.
Re-base re-wrote the history. Referring to particular commit doesn't help.

@roji

roji commented Jan 23, 2018

Copy link
Copy Markdown
Member

@dmitry-a-morozov commits don't get rebased - only branches do. If you rewrite the history by rebasing, a new commit - with a new SHA1 - gets created, since the calculated SHA1 includes the reference to the parent commit (which is now different because we rebased...).

I think that before the rebase this PR was on commit 51749324d261df7bfd69bb4787d07f4be903b4d7 (but I'm not sure, @Brar may be able to help). Try referencing that in your submodule.

@Brar

Brar commented Jan 23, 2018

Copy link
Copy Markdown
Member Author

Now that the branch is broken after re-base I'm stuck.

@dmitry-a-morozov what exactly is broken in https://github.com/Brar/npgsql/tree/FixDeriveOddParameterTypes ?
Does it have to do with @roji s changes and the incomplete information about enum and composite fields:

Note that I haven't yet exposed information about enum and composite fields, that's planned quite soon in #1779 (@Brar, I don't think you depend on this in any way, unlike @dmitry-a-morozov).

I'm asking because the branch itself shouldn't be broken. It builds on my machine and passes all tests as expected and so it should as a submodule on your machine unless I got something wrong. There's just some AppVeyor build oddity once again that prevents the AppVeyor build and by that also the generation of artifacts such as Nuget packages.

@dmitry-a-morozov

Copy link
Copy Markdown
Contributor

@roji My understanding that git sub-module is always tied to particular commit.
5174932 is commit that my npgsql sub-module was originally referring too.

Anyway when I try to pull 5174932 I get

error: Server does not allow request for unadvertised object 51749324d261df7bfd69bb4787d07f4be903b4d7
Fetched in submodule path 'src/npgsql', but it did not contain 51749324d261df7bfd69bb4787d07f4be903b4d7. Direct fetching
 of that commit failed.

@dmitry-a-morozov

Copy link
Copy Markdown
Contributor

@Brar My code relied on NpgsqlParameter.PostgresType property. It seems to be missing now.
I can workaround this issue but it was unexpected.

@Brar

Brar commented Jan 23, 2018 •

Copy link
Copy Markdown
Member Author

@dmitry-a-morozov

My code relied on NpgsqlParameter.PostgresType property. It seems to be missing now.

This is inteded and the way forward.
You will have to use the DataTypeName property in the future.
If it doesn't provide enough information you're actually blocked by #1779 until @roji gets a round tuit.

@dmitry-a-morozov

Copy link
Copy Markdown
Contributor

NpgsqlDbType.Enum is gone too?

@roji

roji commented Jan 23, 2018

Copy link
Copy Markdown
Member

Yes, NpgsqlDbType.Enum is gone, as is NpgsqlDbType.Composite. They don't really make much sense, they don't clearly identify a single PostgreSQL type - there can be many enum and composite types. This is why they had to be used in conjunction with SpecificType, which provided a CLR type mapped to an enum or composite, and which allowed resolving the PostgreSQL type.

All this complicated setup makes no sense - it's much better to simply specify the PostgreSQL type as a string (via DataTypeName), and this is also what allows to write enums and composites without mapping them beforehand - a capability that's important for stuff like type providers...

@dmitry-a-morozov

Copy link
Copy Markdown
Contributor

Got it. Make sense.
What is the best way to know that parameter type is enum?
By eliminating standard types?

@roji

roji commented Jan 23, 2018

Copy link
Copy Markdown
Member

If you're deriving parameters with @Brar's new feature, I think the idea is to also derive the PostgresType property on NpgsqlParameter (I'm not sure whether that's implemented already or not).

Npgsql already contains a type hierarchy for describing PostgreSQL types, in the Npgsql.PostgresTypes namespace. These types inherit from PostgresType, and are supposed to provide everything you may need to know about a type. For example, PostgresArrayType contains an Element property which references the PostgresType of the element contained in the array. In fact, the idea would be for PostgresEnumType and PostgresCompositeType to expose all the relevant information about their contained fields, so you wouldn't have to go select PostgreSQL catalog tables yourself (but this isn't yet implemented).

So to answer your question, once @Brar completes his part, you should be able to check whether NpgsqlParameter.PostgresType is an instance of PostgresEnumType or not.

@dmitry-a-morozov

Copy link
Copy Markdown
Contributor

See @Brar comment above
#1740 (comment)
My understanding that there will be no PostgresType property. Only DataTypeName.

@roji

roji commented Jan 23, 2018

Copy link
Copy Markdown
Member

Ah, I didn't notice the comment. @Brar, I think it would be a good idea to at least have PostgresType for derivation - even if we don't support it as a method for specifying the type (non-derivation).

@roji
roji force-pushed the dev branch 3 times, most recently from 5151389 to 8d35b8a Compare February 24, 2018 06:40
@Brar
Brar force-pushed the FixDeriveOddParameterTypes branch from bd697c4 to dcdffa3 Compare February 28, 2018 16:08
@roji
roji force-pushed the dev branch 2 times, most recently from 349e870 to 0fcf927 Compare March 7, 2018 16:26
@Brar
Brar force-pushed the FixDeriveOddParameterTypes branch from dcdffa3 to 9bfa2b7 Compare March 9, 2018 15:37
@Brar

Brar commented Mar 9, 2018

Copy link
Copy Markdown
Member Author

Rebased and fixed some minor issues as it didn't apply properly after fixing #1779

@Brar

Brar commented Mar 9, 2018

Copy link
Copy Markdown
Member Author

As I see it the failing test Npgsql.Tests.SystemTransactionTests.CloseConnection() is rather a consequence of the rebase than something that has to do with the code changed by this PR.

@roji

roji commented Mar 9, 2018

Copy link
Copy Markdown
Member

Thanks @Brar, the CloseConnection() is my fault and should already be fixed.

@Brar
Brar force-pushed the FixDeriveOddParameterTypes branch from 9bfa2b7 to eca83c8 Compare March 13, 2018 20:56
@Brar

Brar commented Mar 13, 2018

Copy link
Copy Markdown
Member Author

Rebased once again to make sure everything works after be9caf5

@Brar

Brar commented Mar 13, 2018

Copy link
Copy Markdown
Member Author

I start wondering when the AppVeyor build will ever not fail :-(

@roji

roji commented Mar 13, 2018

Copy link
Copy Markdown
Member

Yeah, it's really quite frustrating - "msbuild" is not recognized, really?

Keeping the builds working is a lot of work and I've been neglecting it lately. If Travis works I'm generally happy.

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

Finally got around to this :)

Looks pretty good, there's mainly a factoring issue (and a small correctness issue) around GetTypeInfoByOid/TryGetTypeInfoFromMapping.

I should be available and very reactive in the next few days so let's try to get this merged soon.

Comment thread src/Npgsql/NpgsqlCommand.cs Outdated

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.

I'd rather GetTypeInfoByOid() didn't return dataTypeName, which can simply be accessed on postgresType. At least in theory that method is generic, and the name is useful only in this specific context of parameter derivation. It would also simplify GetTypeInfoByOid() and make clear exactly what the dataTypeName is and where its coming from (i.e. that it's just a property on the postgresType.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed and fixed.

Comment thread src/Npgsql/NpgsqlParameter.cs Outdated

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.

Nit: please remove

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed.

Comment thread src/Npgsql/NpgsqlParameter.cs Outdated

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.

Maybe make this more explicit, explaining derivation in a few words and referencing CommandBuilder with <see .../> etc. pg_type probably doesn't mean a lot to most people.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Extended the comment as suggested.

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.

As mentioned above, am not clear on this... It seems it will throw for PG types that have a mapping, but no NpgsqlDbType, and that aren't enum/composite. For one thing, this will be the case of any mappings added via the new external plugin API, since NpgsqlDbType cannot be updated. In other word, it's totally fine for a mapping to not have an NpgsqlDbType - we can still send parameters by populating DataTypeName. Even if this weren't the case, these methods aren't necessarily for use only by parameter derivation (even if they currently are) - there's no reason to limit them by throwing an exception if no NpgsqlDbType is found.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed and fixed.

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.

I'd recommend simplifying this and maybe changing the relationship between the two methods (TryGetTypeInfoFromMapping and GetTypeInfoByOid):

  • Enum/Composite is spread across the two: TryGetTypeInfoFromMappingchecks for the factory type and returns true+null, which then makes GetTypeInfoByOid go ahead and check for Postgres{Enum,Composite}Type.
  • NpgsqlDbType is determined by TryGetTypeInfoFromMapping, except for the array case where it's constructed by GetTypeInfoByOid.
  • See comment below - the assumption seems to be that we can handle types with NpgsqlDbType and enums/composites, but other types without NpgsqlDbType aren't handled (exception thrown). I don't think this is correct - it's perfectly fine for mappings to not have an NpgsqlDbType (external plugins). Once NpgsqlDbType becomes optional, I think these two methods may change enough to warrant refactoring them into one method - but not sure.
  • type here is an ambiguous name, should be npgsqlDbType

Do you want to take another look at this? I can also try to come up with a proposal if you want.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed and fixed.
TryGetTypeInfoFromMapping is now a tiny local helper function TryGetMapping

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.

Probably rename this test to DeriveFunctionParametersVarious?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed.
I took the opportunity to homogenize all the test names and added one more test that documents the currently non-working behavior of parameters that are arrays of composite types.

@roji

roji commented Mar 14, 2018

Copy link
Copy Markdown
Member

BTW, @Brar, I tried adding you as a contributor to the github project (to be able to assign you and stuff like that), but the invitation is still listed as pending. Did you receive it, would you like me to resend?

If you don't want this of course let me know.

@Brar

Brar commented Mar 15, 2018 •

Copy link
Copy Markdown
Member Author

@roji regarding the invitation: I can't remember getting any kind of notification and didn't see any hint in my GitHub profile or any other page I visit regularly. Anyways, this brougt me to actively search for the invitation and I finally found the hint on the Npgsql project page.

I'll try to work through your comments today and address them accordingly.

@roji

roji commented Mar 15, 2018

Copy link
Copy Markdown
Member

@Brar ok great, I can see you in the contributors team now.

@Brar
Brar force-pushed the FixDeriveOddParameterTypes branch from eca83c8 to 16f039e Compare March 17, 2018 21:34
@Brar

Brar commented Mar 17, 2018

Copy link
Copy Markdown
Member Author

@roji I agree with all your comments above and tried to modify the code accordingly.
I think it's ready for your final review.

@roji
roji merged commit 3a2eeaf into npgsql:dev Mar 18, 2018
@roji

roji commented Mar 18, 2018

Copy link
Copy Markdown
Member

Merged, thanks @Brar! And right on time too, I'm about to release 4.0.0-preview1.

I added a small commit (df678a7) to fix two small issues, take a look and let me know if you have any objections.

@roji roji added this to the 4.0 milestone Mar 18, 2018
@roji roji added the feature label Mar 18, 2018
@Brar
Brar deleted the FixDeriveOddParameterTypes branch March 18, 2018 13:51
@Brar

Brar commented Mar 18, 2018

Copy link
Copy Markdown
Member Author

Thanks, @roji
df678a7 is perfectly ok for me

@roji

roji commented Mar 18, 2018

Copy link
Copy Markdown
Member

OK great, thanks for the contribution @Brar!

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.

Npgsql.NpgsqlCommand.DeriveParametersForQuery fails on un-mapped Enums

3 participants