Repository: diesel-rs/diesel
File: diesel/src/sqlite/expression/functions.rs (Rust)

Code before:
    /// assert_eq!(result, json!(["dog", "spider"]));
    ///
    /// let result = animals.select(json_group_array(legs)).get_result::<serde_json::Value>(connection)?;
    /// assert_eq!(result, json!([4, 8]));
    ///
    /// let result = animals.select(json_group_array(name)).get_result::<serde_json::Value>(connection)?;
    /// assert_eq!(result, json!(["Jack", null]));
    ///
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See also
    /// - [`json_group_array`] will return data in JSON format instead of JSONB.
    /// - [`jsonb_group_object`] will return JSONB object instead of array.
    #[cfg(feature = "sqlite")]
    #[aggregate]
    fn jsonb_group_array<E: SqlType + SingleValue>(elements: E) -> Jsonb;

    /// The json_group_object(NAME,VALUE) function returns a JSON object comprised of all NAME/VALUE pairs in
    /// the aggregation.
    ///
    /// # Corner cases
    ///
    /// This function does not always return a valid JSON. For instance:
    /// 1. If `names` contains `NULL` entries, the corresponding keys will be omitted, but the values will
    /// remain: `{"key_1": 0, : 1, "key_3": 2}`.
    /// 2. If `names` contains duplicate elements, the result will include all of them:
    /// `{"key": 1, "key": 2, "key": 3}`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # include!("../../doctest_setup.rs");
    /// #
    /// # fn main() {
    /// #     #[cfg(feature = "serde_json")]
    /// #     run_test().unwrap();
    /// # }
    /// #
    /// # #[cfg(feature = "serde_json")]
    /// # fn run_test() -> QueryResult<()> {
    /// #     use diesel::dsl::*;
    /// #     use diesel::sql_types::Text;
    /// #     use serde_json::json;
    /// #     use schema::animals::dsl::*;
    /// #
    /// #     let connection = &mut establish_connection();
    /// #
    /// #     let version = diesel::select(sql::<Text>("sqlite_version();"))
    /// #         .get_result::<String>(connection)?;

Review comment (suggestion):
I think we should be able to statically assert that this cannot happen by just requiring that the keys field is not nullable. That can be done by changing the bound to `N: SqlType<IsNull = is_nullable::NotNull>`. 

Code after:
    /// assert_eq!(result, json!(["dog", "spider"]));
    ///
    /// let result = animals.select(json_group_array(legs)).get_result::<serde_json::Value>(connection)?;
    /// assert_eq!(result, json!([4, 8]));
    ///
    /// let result = animals.select(json_group_array(name)).get_result::<serde_json::Value>(connection)?;
    /// assert_eq!(result, json!(["Jack", null]));
    ///
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See also
    /// - [`json_group_array`] will return data in JSON format instead of JSONB.
    /// - [`jsonb_group_object`] will return JSONB object instead of array.
    #[cfg(feature = "sqlite")]
    #[aggregate]
    fn jsonb_group_array<E: SqlType + SingleValue>(elements: E) -> Jsonb;

    /// The json_group_object(NAME,VALUE) function returns a JSON object comprised of all NAME/VALUE pairs in
    /// the aggregation.
    ///
    /// A potential edge case in this function arises when `names` contains duplicate elements.
    /// In such case, the result will include all duplicates (e.g., `{"key": 1, "key": 2, "key": 3}`).
    /// Note that any duplicate entries in the resulting JSON will be removed during deserialization.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # include!("../../doctest_setup.rs");
    /// #
    /// # fn main() {
    /// #     #[cfg(feature = "serde_json")]
    /// #     run_test().unwrap();
    /// # }
    /// #
    /// # #[cfg(feature = "serde_json")]
    /// # fn run_test() -> QueryResult<()> {
    /// #     use diesel::dsl::*;
    /// #     use diesel::sql_types::Text;
    /// #     use serde_json::json;
    /// #     use schema::animals::dsl::*;
    /// #
    /// #     let connection = &mut establish_connection();
    /// #
    /// #     let version = diesel::select(sql::<Text>("sqlite_version();"))
    /// #         .get_result::<String>(connection)?;
    /// #
    /// #     let version_components: Vec<&str> = version.split('.').collect();
    /// #     let major: u32 = version_components[0].parse().unwrap();
    /// #     let minor: u32 = version_components[1].parse().unwrap();