clickhouse.rs icon indicating copy to clipboard operation
clickhouse.rs copied to clipboard

Support for serializing maps

Open AdvaithD opened this issue 1 year ago • 3 comments

I encountered an issue with the clickhouse crate when trying to serialize a struct that uses #[serde(flatten)] to combine multiple fields into a single row. It appears that the serialize_map function is not implemented, as indicated by the todo!() macro in src/rowbinary/ser.rs at line 168, column 9.

Example:

pub struct Xyz<T: Serialize + Row> {
    #[serde(flatten)]
    pub first_field: Metadata,
    #[serde(flatten)]
    pub second_field: T,
}

From the source (src/rowbinary/ser.rs:168.9)

    #[inline]
    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
        todo!();
    }

When attempting to serialize this struct, my binary panics because serialize_map is not yet supported by the crate. Is there a workaround or a planned update to support this functionality?

AdvaithD avatar Jan 11 '24 19:01 AdvaithD

Hi, what result do you expect? What is the definition of the table and Metadata?

loyd avatar Jan 12 '24 07:01 loyd

Hello, I have been trying to add metadata to rows also via #[serde(flatten)]. The patten I would expect is as follows:

#[derive(Row, Serialize)]
struct Metadata {
    foo: String,
    bar: String,
}

#[derive(Row, Serialize)]
struct MyRow {
    #[serde(flatten)]
    metadata: Metadata,
    data: String,
}

This would be expected to represent a row with columns (foo, bar, data).

I may be misunderstanding serde, but to me this seems like how the attribute should behave.

For reference I intend to use this in a templated struct to apply metadata (source/timestamp) to rows during their final submissions. But the example above is better to show the behaviour I expect.

Thanks

Edit: I realise this may be related to struct flattening, not map flattening as the issue relates, but this may be to do with terminology in serde.

Yen avatar Feb 14 '24 17:02 Yen