-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flattening in Row derive macro + documentation of attributes
Following issue #30, this implements the serde-like `flatten` attribute on the `Row` derive macro, allowing to compose rows as follows: ``` struct Row { #[klickhouse(flatten)] user: User, credits: u32 } let users: Vec<Row> = ch.query_collect("SELECT age, name, credits FROM ...").await?; ``` The commit also documents the serde- and clickhouse-specific attributes supported by the derive macro.
- Loading branch information
Showing
7 changed files
with
202 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use klickhouse::Row; | ||
|
||
#[derive(klickhouse::Row, Debug, Default, PartialEq, Clone)] | ||
pub struct TestRow { | ||
field: u32, | ||
#[klickhouse(flatten)] | ||
subrow: SubRow, | ||
field2: u32, | ||
} | ||
|
||
#[derive(klickhouse::Row, Debug, Default, PartialEq, Clone)] | ||
pub struct SubRow { | ||
a: u32, | ||
b: f32, | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_client() { | ||
env_logger::builder() | ||
.filter_level(log::LevelFilter::Info) | ||
.init(); | ||
|
||
assert!(TestRow::column_names() | ||
.unwrap() | ||
.into_iter() | ||
.zip(["field", "a", "b"]) | ||
.all(|(x, y)| x == y)); | ||
|
||
let client = super::get_client().await; | ||
|
||
super::prepare_table( | ||
"test_flatten", | ||
"field UInt32, | ||
field2 UInt32, | ||
a UInt32, | ||
b Float32", | ||
&client, | ||
) | ||
.await; | ||
|
||
let row = TestRow { | ||
field: 1, | ||
field2: 4, | ||
subrow: SubRow { a: 2, b: 3.0 }, | ||
}; | ||
|
||
client | ||
.insert_native_block("INSERT INTO test_flatten FORMAT Native", vec![row.clone()]) | ||
.await | ||
.unwrap(); | ||
|
||
tokio::time::sleep(std::time::Duration::from_secs(1)).await; | ||
|
||
let row2 = client | ||
.query_one::<TestRow>("SELECT * FROM test_flatten") | ||
.await | ||
.unwrap(); | ||
assert_eq!(row, row2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters