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

Adding Support for json_populate_record and jsonb_populate_record #4325

Merged
merged 2 commits into from
Nov 17, 2024

Conversation

prkbuilds
Copy link
Member

Using the traits already defined and have written the test_cases with my understanding of the functions, but open to suggestions and learnings!

@weiznich weiznich requested a review from a team October 28, 2024 07:11
@prkbuilds
Copy link
Member Author

just for context the PR is for the issue #4216

Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

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

I'm sorry to take that long to review this PR and I'm also sorry to bring up an issue, but it seems like the nullable bounds are not correct yet. I've added a more detailed comment in the specific location on how this can be fixed.

Comment on lines 2395 to 2399
fn jsonb_populate_record<
B: RecordOrNullableRecord + SingleValue,
J: JsonbOrNullableJsonb + CombinedNullableValue<Jsonb, B>
>(base: B, from_json: J) -> J::Out;
Copy link
Member

Choose a reason for hiding this comment

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

I've played a bit around on the postgresql command line and it seems like the output type of this function is only null if both inputs are null. The CombineNullableValue helper trait expresses a constraint that says "if one of the inputs is null the output is null", so that's slightly different. We need to have a similar trait, but with slightly different bounds. Essentially we need a new trait with the same structure, but that replace the usage of OneIsNullable in the bounds with AllIsNullable. The trait implementation of CombineNullableValue is here:

impl<T, O, Out> CombinedNullableValue<O, Out> for T
where
T: SingleValue,
O: SingleValue,
T::IsNull: OneIsNullable<O::IsNull>,
<T::IsNull as OneIsNullable<O::IsNull>>::Out: MaybeNullableType<Out>,
<<T::IsNull as OneIsNullable<O::IsNull>>::Out as MaybeNullableType<Out>>::Out: SingleValue,
{
type Out = <<T::IsNull as OneIsNullable<O::IsNull>>::Out as MaybeNullableType<Out>>::Out;
}
, that can be just copied + adjusted for the new trait.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks @weiznich ! This is great to know and really informative, I totally understand what needs to be done, I'll implement the required change are add the changes. and thanks for telling a good way to test the nullability, will definitely keep that in mind next time.

@prkbuilds prkbuilds force-pushed the pg_json_populate_record branch 3 times, most recently from ed5c870 to f10ddd3 Compare November 13, 2024 20:00
@prkbuilds
Copy link
Member Author

I'm not sure if the naming convention is the best for the new Nullable Trait implementation. I think this will take care of Nullablility, but let me know if there is anything I might have missed.

Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

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

Thanks for the update. This looks good beside the name of the new trait.

@@ -3726,4 +3726,19 @@ pub(in crate::pg) mod private {

impl<T> RecordOrNullableRecord for Record<T> {}
impl<T> RecordOrNullableRecord for Nullable<Record<T>> {}

pub trait CombinedOneNullableValue<O, Out>: SingleValue {
Copy link
Member

Choose a reason for hiding this comment

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

Can we name this CombinedAllNullableValue or something like that instead to transport that this only evaluates to Nullable if all inputs are Nullable?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @weiznich , sure I will change the name accordingly. And yes they allarenullable only returns nullable value when both values are null, completing the logic we need for this trait.

Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

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

Thanks for the update

@weiznich weiznich added this pull request to the merge queue Nov 17, 2024
Merged via the queue into diesel-rs:master with commit 27421b9 Nov 17, 2024
48 checks passed
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.

2 participants