Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dictionary (hstore, json, and jsonb) query support #3285

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yinzara
Copy link

@yinzara yinzara commented Sep 19, 2024

This PR adds support for querying on hstore type columns which can map to either Dictionary<string, string> or ImmutableDictionary<string, string> in C# and for querying any other Dictionary<,> or ImmutableDictionary<,> for json and jsonb types.

See npgsql/doc#369 for full documentation.

Fixes #212

@yinzara yinzara force-pushed the feature/212 branch 2 times, most recently from 8f6dad8 to 49431a8 Compare September 19, 2024 19:21
Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

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

Thanks @yinzara, looks pretty good overall! See comments below.

@roji
Copy link
Member

roji commented Sep 22, 2024

I attempted Keys and Values and could never get it to work correctly.

If you translate Keys via akeys, that simply returns an array, so shouldn't be complicated (note that the provider will automatically call unnest on that array if you start composing arbitrary operators on it). If you translate via skeys which returns a set, things are definitely more complicated.

@yinzara yinzara force-pushed the feature/212 branch 21 times, most recently from df2adf1 to b4e4966 Compare October 4, 2024 07:15
@yinzara yinzara force-pushed the feature/212 branch 5 times, most recently from 961a7df to 79293d7 Compare October 11, 2024 09:16
@yinzara
Copy link
Author

yinzara commented Oct 11, 2024

@roji
I think I've got another incarnation that's worth your eyes to at least give some guidance if you think I'm headed in the right direction. I still have a few more tests to write if you agree.

This PR is now focused 100% on all forms of Dictionary<string,TValue> and ImmutableDictionary<string,TValue> based querying no matter if it's hstore, json or jsonb no matter their TValue types.

I removed all of the DBFunction extensions that could ever have a translation that would be supported without the DBFunctions. A future PR (or maybe if I get ansy) can address doing the expression visitor changes to support the other queries.

I now have a test case with a Dictionary<string, Dictionary<string, string>> NestedDictionary {get;set;} that shows how the functionality even works recursively :-) and another test case with Dictionary<string, int> to show it works with numbers as well.

Finally I made the new DictionaryTranslator not look up any TypeMappings inside the constructor so it's very efficient to create new instances of it with no overhead.

This does not fix dotnet/efcore#26903 as I was thinking it might, but it still seems quite useful overall and eventually it could support it.

@yinzara
Copy link
Author

yinzara commented Nov 22, 2024

I know it's been a while since you've had time to look at this. Any chance you could take a peak and at least tell me if you are ok with my direction now? If so I'll update all the test cases and make sure it's ready for merge.

@@ -135,6 +135,7 @@ protected override void Print(ExpressionPrinter expressionPrinter)
PgExpressionType.JsonExists => "?",
PgExpressionType.JsonExistsAny => "?|",
PgExpressionType.JsonExistsAll => "?&",
PgExpressionType.JsonValueForKeyAsText => "->>",
Copy link
Author

Choose a reason for hiding this comment

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

I realize this isn't necessary now and I'll make sure it's removed before the PR merges.

@roji
Copy link
Member

roji commented Nov 23, 2024

@yinzara I'll do my best to look soon, but it's a very busy time in the post 9.0 phase; so it may take a little while. Don't hesitate to ping me again in a month or two if you see no progress here.

@yinzara yinzara changed the base branch from main to hotfix/9.0.4 February 20, 2025 01:14
@yinzara
Copy link
Author

yinzara commented Feb 20, 2025

I've now rebased this PR off of the hotfix/9.4 release as the main branch has an SDK that has not been released publically yet.

All tests are now passing in this PR and I think it's ready for a real review.

Would this go into a maintenance release of 9 or will this PR eventually have to be targeted against main before it can be merged so that it only comes out with 10?

@roji
Copy link
Member

roji commented Feb 20, 2025

@yinzara sorry this is taking so long to merge. In principle, as this is a new feature, it needs to go into 10 and not into 9; aside from a few exceptions, only bug fixes go into patch releases. So can you please rebase back on top of main? Re the preview SDK, it should be pretty easy to set up - you can use the dotnet-install scripts to download the specific version of the SDK specified in the project's global.json, and use that.

@yinzara yinzara changed the base branch from hotfix/9.0.4 to main February 20, 2025 17:05
@yinzara
Copy link
Author

yinzara commented Feb 20, 2025

I've rebased against main. Thank you! Happy to make any other changes you'd like.

SqlExpression? instance,
MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
Copy link
Author

Choose a reason for hiding this comment

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

Here I attempted to "return null quickly" as much as possible so that you did the least amount of operations before returning null. The logic below was what I found to be the best combination while still being readable and efficient.

/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public SqlExpression? Keys(SqlExpression instance)
Copy link
Author

@yinzara yinzara Feb 20, 2025

Choose a reason for hiding this comment

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

I choose to make these methods public and not private so they could be used externally to this translator eventually (i.e. in some expression visitor translator for more complex translations). Honestly internal probably would have been enough but I didn't see that used in a lot of places.

All public methods accept arguments of either json/jsonb or hstore arguments and return the translation or null if the arguments are not json/jsonb/hstore.

Thoughts?

@@ -40,10 +41,11 @@ public NpgsqlMemberTranslatorProvider(
new NpgsqlDateTimeMemberTranslator(typeMappingSource, sqlExpressionFactory),
new NpgsqlJsonDomTranslator(typeMappingSource, sqlExpressionFactory, model),
new NpgsqlLTreeTranslator(typeMappingSource, sqlExpressionFactory, model),
new DictionaryTranslator(typeMappingSource, sqlExpressionFactory, model),
Copy link
Author

Choose a reason for hiding this comment

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

Needs to be before the JsonPocoTranslator as it needs to intercept methods on Dictionary types that are json types.

@yinzara yinzara force-pushed the feature/212 branch 2 times, most recently from 9bcdc30 to c76f04d Compare February 20, 2025 18:24
@yinzara
Copy link
Author

yinzara commented Feb 20, 2025

I have now verified that all tests are passing locally after the rebase to main and all unused code and formatting changes have been removed.
I believe this is ready for a final review and approval for testing in the build system.

@yinzara yinzara requested a review from roji February 20, 2025 18:44
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.

Translate hstore access to SQL
3 participants