|
| 1 | +use chrono::Duration; |
| 2 | +use clickhouse::Client; |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | + |
| 5 | +#[derive(Debug, Serialize, Deserialize, clickhouse::Row)] |
| 6 | +struct TimeExample { |
| 7 | + #[serde(with = "clickhouse::serde::time::time")] |
| 8 | + time_field: time::Duration, |
| 9 | + |
| 10 | + #[serde(with = "clickhouse::serde::time::time::option")] |
| 11 | + time_optional: Option<time::Duration>, |
| 12 | + |
| 13 | + #[serde(with = "clickhouse::serde::time::time64::secs")] |
| 14 | + time64_seconds: time::Duration, |
| 15 | + |
| 16 | + #[serde(with = "clickhouse::serde::time::time64::millis")] |
| 17 | + time64_millis: time::Duration, |
| 18 | + |
| 19 | + #[serde(with = "clickhouse::serde::time::time64::micros")] |
| 20 | + time64_micros: time::Duration, |
| 21 | + |
| 22 | + #[serde(with = "clickhouse::serde::time::time64::nanos")] |
| 23 | + time64_nanos: time::Duration, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Debug, Serialize, Deserialize, clickhouse::Row)] |
| 27 | +struct TimeExampleChrono { |
| 28 | + #[serde(with = "clickhouse::serde::chrono::time")] |
| 29 | + time_field: Duration, |
| 30 | + |
| 31 | + #[serde(with = "clickhouse::serde::chrono::time::option")] |
| 32 | + time_optional: Option<Duration>, |
| 33 | + |
| 34 | + #[serde(with = "clickhouse::serde::chrono::time64::secs")] |
| 35 | + time64_seconds: Duration, |
| 36 | + |
| 37 | + #[serde(with = "clickhouse::serde::chrono::time64::millis")] |
| 38 | + time64_millis: Duration, |
| 39 | + |
| 40 | + #[serde(with = "clickhouse::serde::chrono::time64::micros")] |
| 41 | + time64_micros: Duration, |
| 42 | + |
| 43 | + #[serde(with = "clickhouse::serde::chrono::time64::nanos")] |
| 44 | + time64_nanos: Duration, |
| 45 | +} |
| 46 | + |
| 47 | +#[tokio::main] |
| 48 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 49 | + let client = Client::default(); |
| 50 | + |
| 51 | + let create_table_sql = r#" |
| 52 | + CREATE TABLE IF NOT EXISTS time_example ( |
| 53 | + time_field Time, |
| 54 | + time_optional Nullable(Time), |
| 55 | + time64_seconds Time64(0), |
| 56 | + time64_millis Time64(3), |
| 57 | + time64_micros Time64(6), |
| 58 | + time64_nanos Time64(9) |
| 59 | + ) ENGINE = MergeTree() |
| 60 | + ORDER BY time_field |
| 61 | + "#; |
| 62 | + |
| 63 | + client.query(create_table_sql).execute().await?; |
| 64 | + |
| 65 | + // Insert data using time crate |
| 66 | + let time_example = TimeExample { |
| 67 | + time_field: time::Duration::seconds(12 * 3600 + 34 * 60 + 56), |
| 68 | + time_optional: Some(time::Duration::seconds(23 * 3600 + 59 * 60 + 59)), |
| 69 | + time64_seconds: time::Duration::seconds(3600 + 2 * 60 + 3), |
| 70 | + time64_millis: time::Duration::seconds(4 * 3600 + 5 * 60 + 6) |
| 71 | + + time::Duration::milliseconds(123), |
| 72 | + time64_micros: time::Duration::seconds(7 * 3600 + 8 * 60 + 9) |
| 73 | + + time::Duration::microseconds(456_789), |
| 74 | + time64_nanos: time::Duration::seconds(10 * 3600 + 11 * 60 + 12) |
| 75 | + + time::Duration::nanoseconds(123_456_789), |
| 76 | + }; |
| 77 | + |
| 78 | + let mut insert = client.insert::<TimeExample>("time_example")?; |
| 79 | + insert.write(&time_example).await?; |
| 80 | + insert.end().await?; |
| 81 | + |
| 82 | + // Insert data using chrono crate |
| 83 | + let time_example_chrono = TimeExampleChrono { |
| 84 | + time_field: Duration::seconds(13 * 3600 + 45 * 60), |
| 85 | + time_optional: Some(Duration::seconds(1)), |
| 86 | + time64_seconds: Duration::seconds(2 * 3600 + 3 * 60 + 4), |
| 87 | + time64_millis: Duration::seconds(5 * 3600 + 6 * 60 + 7) + Duration::milliseconds(456), |
| 88 | + time64_micros: Duration::seconds(8 * 3600 + 9 * 60 + 10) + Duration::microseconds(789_012), |
| 89 | + time64_nanos: Duration::seconds(11 * 3600 + 12 * 60 + 13) |
| 90 | + + Duration::nanoseconds(987_654_321), |
| 91 | + }; |
| 92 | + |
| 93 | + let mut insert = client.insert::<TimeExampleChrono>("time_example")?; |
| 94 | + insert.write(&time_example_chrono).await?; |
| 95 | + insert.end().await?; |
| 96 | + |
| 97 | + // Insert chrono edge cases |
| 98 | + let edge_cases = vec![ |
| 99 | + Duration::seconds(-999 * 3600 - 59 * 60 - 59), // Min |
| 100 | + Duration::zero(), // Midnight |
| 101 | + Duration::seconds(999 * 3600 + 59 * 60 + 59), // Max |
| 102 | + ]; |
| 103 | + |
| 104 | + for (i, edge) in edge_cases.into_iter().enumerate() { |
| 105 | + let data = TimeExampleChrono { |
| 106 | + time_field: edge, |
| 107 | + time_optional: Some(edge), |
| 108 | + time64_seconds: edge, |
| 109 | + time64_millis: edge, |
| 110 | + time64_micros: edge, |
| 111 | + time64_nanos: edge, |
| 112 | + }; |
| 113 | + let mut insert = client.insert::<TimeExampleChrono>("time_example")?; |
| 114 | + insert.write(&data).await?; |
| 115 | + insert.end().await?; |
| 116 | + println!("Inserted edge case #{i}: {edge:?}"); |
| 117 | + } |
| 118 | + |
| 119 | + // Query the data |
| 120 | + let rows: Vec<TimeExample> = client |
| 121 | + .query("SELECT * FROM time_example ORDER BY time_field") |
| 122 | + .fetch_all() |
| 123 | + .await?; |
| 124 | + for time_example in rows { |
| 125 | + println!("Time example: {time_example:?}"); |
| 126 | + } |
| 127 | + |
| 128 | + println!("Time and Time64 types example completed successfully!"); |
| 129 | + |
| 130 | + Ok(()) |
| 131 | +} |
0 commit comments