|
| 1 | +use clickhouse_derive::Row; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | + |
| 4 | +use clickhouse::sql::Identifier; |
| 5 | +use clickhouse::{error::Result, Client}; |
| 6 | + |
| 7 | +// See also: https://clickhouse.com/docs/en/sql-reference/data-types/variant |
| 8 | + |
| 9 | +#[tokio::main] |
| 10 | +async fn main() -> Result<()> { |
| 11 | + let table_name = "chrs_data_types_variant"; |
| 12 | + let client = Client::default().with_url("http://localhost:8123"); |
| 13 | + |
| 14 | + // No matter the order of the definition on the Variant types in the DDL, this particular Variant will always be sorted as follows: |
| 15 | + // Variant(Array(UInt16), Bool, FixedString(6), Float32, Float64, Int128, Int16, Int32, Int64, Int8, String, UInt128, UInt16, UInt32, UInt64, UInt8) |
| 16 | + client |
| 17 | + .query( |
| 18 | + " |
| 19 | + CREATE OR REPLACE TABLE ? |
| 20 | + ( |
| 21 | + `id` UInt64, |
| 22 | + `var` Variant( |
| 23 | + Array(UInt16), |
| 24 | + Bool, |
| 25 | + Date, |
| 26 | + FixedString(6), |
| 27 | + Float32, Float64, |
| 28 | + Int128, Int16, Int32, Int64, Int8, |
| 29 | + String, |
| 30 | + UInt128, UInt16, UInt32, UInt64, UInt8 |
| 31 | + ) |
| 32 | + ) |
| 33 | + ENGINE = MergeTree |
| 34 | + ORDER BY id", |
| 35 | + ) |
| 36 | + .bind(Identifier(table_name)) |
| 37 | + .with_option("allow_experimental_variant_type", "1") |
| 38 | + // This is required only if we are mixing similar types in the Variant definition |
| 39 | + // In this case, this is various Int/UInt types, Float32/Float64, and String/FixedString |
| 40 | + // Omit this option if there are no similar types in the definition |
| 41 | + .with_option("allow_suspicious_variant_types", "1") |
| 42 | + .execute() |
| 43 | + .await?; |
| 44 | + |
| 45 | + let mut insert = client.insert(table_name)?; |
| 46 | + let rows_to_insert = get_rows(); |
| 47 | + for row in rows_to_insert { |
| 48 | + insert.write(&row).await?; |
| 49 | + } |
| 50 | + insert.end().await?; |
| 51 | + |
| 52 | + let rows = client |
| 53 | + .query("SELECT ?fields FROM ?") |
| 54 | + .bind(Identifier(table_name)) |
| 55 | + .fetch_all::<MyRow>() |
| 56 | + .await?; |
| 57 | + |
| 58 | + println!("{rows:#?}"); |
| 59 | + Ok(()) |
| 60 | +} |
| 61 | + |
| 62 | +fn get_rows() -> Vec<MyRow> { |
| 63 | + vec![ |
| 64 | + MyRow { |
| 65 | + id: 1, |
| 66 | + var: MyRowVariant::Array(vec![1, 2]), |
| 67 | + }, |
| 68 | + MyRow { |
| 69 | + id: 2, |
| 70 | + var: MyRowVariant::Boolean(true), |
| 71 | + }, |
| 72 | + MyRow { |
| 73 | + id: 3, |
| 74 | + var: MyRowVariant::Date( |
| 75 | + time::Date::from_calendar_date(2021, time::Month::January, 1).unwrap(), |
| 76 | + ), |
| 77 | + }, |
| 78 | + MyRow { |
| 79 | + id: 4, |
| 80 | + var: MyRowVariant::FixedString(*b"foobar"), |
| 81 | + }, |
| 82 | + MyRow { |
| 83 | + id: 5, |
| 84 | + var: MyRowVariant::Float32(100.5), |
| 85 | + }, |
| 86 | + MyRow { |
| 87 | + id: 6, |
| 88 | + var: MyRowVariant::Float64(200.1), |
| 89 | + }, |
| 90 | + MyRow { |
| 91 | + id: 7, |
| 92 | + var: MyRowVariant::Int8(2), |
| 93 | + }, |
| 94 | + MyRow { |
| 95 | + id: 8, |
| 96 | + var: MyRowVariant::Int16(3), |
| 97 | + }, |
| 98 | + MyRow { |
| 99 | + id: 9, |
| 100 | + var: MyRowVariant::Int32(4), |
| 101 | + }, |
| 102 | + MyRow { |
| 103 | + id: 10, |
| 104 | + var: MyRowVariant::Int64(5), |
| 105 | + }, |
| 106 | + MyRow { |
| 107 | + id: 11, |
| 108 | + var: MyRowVariant::Int128(6), |
| 109 | + }, |
| 110 | + MyRow { |
| 111 | + id: 12, |
| 112 | + var: MyRowVariant::String("my_string".to_string()), |
| 113 | + }, |
| 114 | + MyRow { |
| 115 | + id: 13, |
| 116 | + var: MyRowVariant::UInt8(7), |
| 117 | + }, |
| 118 | + MyRow { |
| 119 | + id: 14, |
| 120 | + var: MyRowVariant::UInt16(8), |
| 121 | + }, |
| 122 | + MyRow { |
| 123 | + id: 15, |
| 124 | + var: MyRowVariant::UInt32(9), |
| 125 | + }, |
| 126 | + MyRow { |
| 127 | + id: 16, |
| 128 | + var: MyRowVariant::UInt64(10), |
| 129 | + }, |
| 130 | + MyRow { |
| 131 | + id: 17, |
| 132 | + var: MyRowVariant::UInt128(11), |
| 133 | + }, |
| 134 | + ] |
| 135 | +} |
| 136 | + |
| 137 | +// As the inner Variant types are _always_ sorted alphabetically, |
| 138 | +// Rust enum variants should be defined in the _exactly_ same order as it is in the data type; |
| 139 | +// their names are irrelevant, only the order of the types matters. |
| 140 | +// This enum represents Variant(Array(UInt16), Bool, Date, FixedString(6), Float32, Float64, Int128, Int16, Int32, Int64, Int8, String, UInt128, UInt16, UInt32, UInt64, UInt8) |
| 141 | +#[derive(Debug, PartialEq, Serialize, Deserialize)] |
| 142 | +enum MyRowVariant { |
| 143 | + Array(Vec<i16>), |
| 144 | + Boolean(bool), |
| 145 | + // attributes should work in this case, too |
| 146 | + #[serde(with = "clickhouse::serde::time::date")] |
| 147 | + Date(time::Date), |
| 148 | + // NB: by default, fetched as raw bytes |
| 149 | + FixedString([u8; 6]), |
| 150 | + Float32(f32), |
| 151 | + Float64(f64), |
| 152 | + Int128(i128), |
| 153 | + Int16(i16), |
| 154 | + Int32(i32), |
| 155 | + Int64(i64), |
| 156 | + Int8(i8), |
| 157 | + String(String), |
| 158 | + UInt128(u128), |
| 159 | + UInt16(i16), |
| 160 | + UInt32(u32), |
| 161 | + UInt64(u64), |
| 162 | + UInt8(i8), |
| 163 | +} |
| 164 | + |
| 165 | +#[derive(Debug, PartialEq, Row, Serialize, Deserialize)] |
| 166 | +struct MyRow { |
| 167 | + id: u64, |
| 168 | + var: MyRowVariant, |
| 169 | +} |
0 commit comments