clickhouse_format/output/
json_compact_strings_each_row.rs

1use std::collections::HashMap;
2
3use serde::de::DeserializeOwned;
4
5use crate::format_name::FormatName;
6
7use super::{json_compact_each_row::JsonCompactEachRowOutput, Output, OutputResult};
8
9type Inner<T> = JsonCompactEachRowOutput<T>;
10
11pub struct JsonCompactStringsEachRowOutput<T> {
12    inner: Inner<T>,
13}
14impl<T> JsonCompactStringsEachRowOutput<T> {
15    pub fn new(names: Vec<String>) -> Self {
16        Self {
17            inner: Inner::new(names),
18        }
19    }
20}
21
22pub type GeneralJsonCompactStringsEachRowOutput =
23    JsonCompactStringsEachRowOutput<HashMap<String, String>>;
24
25impl<T> Output for JsonCompactStringsEachRowOutput<T>
26where
27    T: DeserializeOwned,
28{
29    type Row = <Inner<T> as Output>::Row;
30    type Info = <Inner<T> as Output>::Info;
31
32    type Error = <Inner<T> as Output>::Error;
33
34    fn format_name() -> crate::format_name::FormatName {
35        FormatName::JsonCompactStringsEachRow
36    }
37
38    fn deserialize(&self, slice: &[u8]) -> OutputResult<Self::Row, Self::Info, Self::Error> {
39        self.inner.deserialize(slice)
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    use std::{fs, path::PathBuf};
48
49    use crate::test_helpers::{TestStringsRow, TEST_STRINGS_ROW_1};
50
51    #[test]
52    fn simple() -> Result<(), Box<dyn std::error::Error>> {
53        let file_path = PathBuf::new().join("tests/files/JSONCompactStringsEachRow.txt");
54        let content = fs::read_to_string(&file_path)?;
55
56        assert_eq!(
57            GeneralJsonCompactStringsEachRowOutput::format_name(),
58            file_path
59                .file_stem()
60                .unwrap()
61                .to_string_lossy()
62                .parse()
63                .unwrap()
64        );
65
66        let (rows, _info): (_, ()) = GeneralJsonCompactStringsEachRowOutput::new(vec![
67            "array1".into(),
68            "array2".into(),
69            "tuple1".into(),
70            "tuple2".into(),
71            "map1".into(),
72        ])
73        .deserialize(content.as_bytes())?;
74        assert_eq!(rows.first().unwrap().get("tuple1").unwrap(), "(1,'a')");
75
76        let (rows, _info): (_, ()) = JsonCompactStringsEachRowOutput::<TestStringsRow>::new(vec![
77            "array1".into(),
78            "array2".into(),
79            "tuple1".into(),
80            "tuple2".into(),
81            "map1".into(),
82        ])
83        .deserialize(content.as_bytes())?;
84        assert_eq!(rows.first().unwrap(), &*TEST_STRINGS_ROW_1);
85
86        Ok(())
87    }
88}