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

Add support for array_to_string and array_to_string_null array functions #4171

Merged
merged 13 commits into from
Aug 24, 2024

Conversation

insky7
Copy link
Contributor

@insky7 insky7 commented Aug 16, 2024

Attempt to achieve array_to_string function for postgres listed in Issue #4153 https://www.postgresql.org/docs/current/functions-array.html

Since it contained atleast one one optional argument of type Nullable<Text> I've defined two separate variants array_to_string_null and array_to_string as per recommendation.

First PR here and first PR ever to open source so feel free to be harsh if this does not achieve intended goals as I understood them or does not follow best practices, open to suggestions.

@weiznich weiznich requested a review from a team August 16, 2024 09:14
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 opening this PR. It looks like a first step in the right direction. It's definitively a good job for your first PR 👍

I've left a few comments that hopefully should help to address the test failures and I also added some suggestions on how to improve the code.

I also resolved the merge conflict caused by merging some other PR's, so be sure to pull the changes before continuing to work on this PR.

/// # Ok(())
/// # }
/// ```
fn array_to_string_null<Arr: ArrayOrNullableArray<Inner=Elem> + SingleValue, Elem: SingleValue>(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fn array_to_string_null<Arr: ArrayOrNullableArray<Inner=Elem> + SingleValue, Elem: SingleValue>(
#[sql_name = "array_to_string"]
fn array_to_string_null<Arr: ArrayOrNullableArray<Inner=Elem> + SingleValue, Elem: SingleValue>(

as otherwise no sql function with that name exists

Also what's about naming this function: array_to_string_with_null_string?

fn array_to_string_null<Arr: ArrayOrNullableArray<Inner=Elem> + SingleValue, Elem: SingleValue>(
arr: Arr,
delim: Text,
null_str: Nullable<Text>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
null_str: Nullable<Text>
null_str: Text

As we want to require that optional argument here

diesel/src/pg/expression/functions.rs Show resolved Hide resolved
diesel/src/pg/expression/functions.rs Outdated Show resolved Hide resolved
diesel/src/pg/expression/functions.rs Outdated Show resolved Hide resolved
diesel/src/pg/expression/helper_types.rs Outdated Show resolved Hide resolved
/// # Ok(())
/// # }
/// ```
fn array_to_string<Arr: ArrayOrNullableArray<Inner=Elem> + SingleValue, Elem: SingleValue>(
Copy link
Member

Choose a reason for hiding this comment

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

This bound can be simplified to

Suggested change
fn array_to_string<Arr: ArrayOrNullableArray<Inner=Elem> + SingleValue, Elem: SingleValue>(
fn array_to_string<Arr: ArrayOrNullableArray>(

We don't use Elem anywhere.

@insky7
Copy link
Contributor Author

insky7 commented Aug 17, 2024

Will look at the fails, TRYBUILD=overwrite cargo test inside of diesel_compile_tests was passing on my end, need to configure things correctly

/// Converts each array element to its text representation, and concatenates those separated by the delimiter string.
/// This variant omits the `null_str` argument.
/// This variant omits the `null_str` argument. See [array_to_string_null] for a variant with that argument
Copy link
Contributor

Choose a reason for hiding this comment

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

As you rename this function, new name should be used:

Suggested change
/// This variant omits the `null_str` argument. See [array_to_string_null] for a variant with that argument
/// This variant omits the `null_str` argument. See [`array_to_string_with_null_string`] for a variant with that argument

@insky7
Copy link
Contributor Author

insky7 commented Aug 19, 2024

Need some help I think... I'm a bit stuck and banging my head against a wall at this point, @weiznich can you please review and try to point me in the right direction?

@weiznich
Copy link
Member

I'm away for a few days. I will have a look as soon as I'm back.

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 updating this PR. I've left a few comments that should help to explain how to proceed with this. If something is still unclear do not hesitate to ask for more details. It's sometimes hard for me to get the level of detail for information included in such comments correct.

Comment on lines 812 to 815
fn array_to_string<Arr: ArrayOrNullableArray<Inner=Nullable<Text>> + SingleValue, T: SingleValue>(
a: Arr,
e: T
) -> Text;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fn array_to_string<Arr: ArrayOrNullableArray<Inner=Nullable<Text>> + SingleValue, T: SingleValue>(
a: Arr,
e: T
) -> Text;
fn array_to_string<Arr: ArrayOrNullableArray +SingleValue>(array: Arr, del: Text)) -> Text;

We do not need to make this function generic over the type of del here, as we only want to accept anything that is compatible with Text there. Therefore we can just write the sql side type (Text) there and do not need to care about generics.
Also we want to accept any array type there, not only arrays of the type Text, which means we do not care about the Inner associated type here.

Comment on lines 777 to 781
fn array_to_string_with_null_string<Arr: ArrayOrNullableArray<Inner=Nullable<Text>> + SingleValue, T: SingleValue, N: SingleValue>(
a: Arr,
e: T,
n: N
) -> Text;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fn array_to_string_with_null_string<Arr: ArrayOrNullableArray<Inner=Nullable<Text>> + SingleValue, T: SingleValue, N: SingleValue>(
a: Arr,
e: T,
n: N
) -> Text;
#[sql_name = "array_to_string"]
fn array_to_string_with_null_string<Arr: ArrayOrNullableArray + SingleValue>(array: Arr, del: Text, null: Text) -> Text;

For similar reasons as outlined for the array_to_string function. The #[sql_name] attribute is required to use the right sql function name in the generated sql

diesel/src/pg/expression/helper_types.rs Outdated Show resolved Hide resolved
@insky7
Copy link
Contributor Author

insky7 commented Aug 23, 2024

@weiznich
Applied your suggestions and ran the doc-tests in my local and they are finally passing.

Need to resolve these merge conflicts though, its my bad 😢

@insky7 insky7 requested a review from weiznich August 23, 2024 22:58
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, looks good now 👍

I've just done another merge as merge conflicts currently appear faster than they can be resolved. (No worries about that, it's just that a lot of PR's touch the same code)

@weiznich weiznich added this pull request to the merge queue Aug 24, 2024
Merged via the queue into diesel-rs:master with commit cd37481 Aug 24, 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.

3 participants