taos-query 0.12.4

Driver for TDengine - a timeseries database and analysis platform
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use std::{
    fmt,
    ops::{Deref, DerefMut},
    str::FromStr,
};

use serde::{
    de::{self, MapAccess, SeqAccess, Visitor},
    Deserialize, Deserializer, Serialize,
};

use crate::common::Ty;

/// Compress options for column, supported since TDengine 3.3.0.0 .
///
/// The `encode` field is the encoding method for the column, it can be one of the following values:
/// - `disabled`
/// - `delta-i`
/// - `delta-d`
/// - `simple8b`
///
/// The `compress` field is the compression method for the column, it can be one of the following values:
/// - `none`
/// - `lz4`
/// - `gzip`
/// - `zstd`
///
/// The `level` field is the compression level for the column, it can be one of the following values:
/// - `low`
/// - `medium`
/// - `high`
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct CompressOptions {
    pub encode: String,
    pub compress: String,
    pub level: String,
}

impl CompressOptions {
    pub fn new(
        encode: impl Into<String>,
        compress: impl Into<String>,
        level: impl Into<String>,
    ) -> Self {
        Self {
            encode: encode.into(),
            compress: compress.into(),
            level: level.into(),
        }
    }
}

impl fmt::Display for CompressOptions {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "ENCODE '{}' COMPRESS '{}' LEVEL '{}'",
            self.encode, self.compress, self.level
        )
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Described {
    pub field: String,
    #[serde(rename = "type")]
    pub ty: Ty,
    pub length: usize,
    #[serde(default)]
    pub note: Option<String>,
    #[serde(flatten, default)]
    pub compression: Option<CompressOptions>,
}

impl Described {
    /// Represent the data type in sql.
    ///
    /// For example: "INT", "VARCHAR(100)".
    pub fn sql_repr(&self) -> String {
        let ty = self.ty;
        match (self.is_primary_key(), ty.is_var_type(), &self.compression) {
            (true, true, None) => format!("`{}` {}({}) PRIMARY KEY", self.field, ty, self.length),
            (true, false, None) => format!("`{}` {} PRIMARY KEY", self.field, self.ty),
            (true, true, Some(t)) => {
                format!("`{}` {}({}) {} PRIMARY KEY", self.field, ty, self.length, t)
            }
            (true, false, Some(t)) => {
                format!("`{}` {} {} PRIMARY KEY", self.field, ty, t)
            }

            (false, true, None) => format!("`{}` {}({})", self.field, ty, self.length),
            (false, false, None) => format!("`{}` {}", self.field, self.ty),
            (false, true, Some(t)) => {
                format!("`{}` {}({}) {}", self.field, ty, self.length, t)
            }
            (false, false, Some(t)) => {
                format!("`{}` {} {}", self.field, ty, t)
            }
        }
    }

    /// Create a new column description without primary-key/compression feature.
    pub fn new(field: impl Into<String>, ty: Ty, length: impl Into<Option<usize>>) -> Self {
        let field = field.into();
        let length = length.into();
        let length = length.unwrap_or_else(|| {
            if ty.is_var_type() {
                32
            } else {
                ty.fixed_length()
            }
        });
        Self {
            field,
            ty,
            length,
            note: None,
            compression: None,
        }
    }

    /// Return true if the field is primary key.
    pub fn is_primary_key(&self) -> bool {
        self.note.as_deref() == Some("PRIMARY KEY")
    }

    /// Return true if the field is tag.
    pub fn is_tag(&self) -> bool {
        self.note.as_deref() == Some("TAG")
    }
}
#[derive(Debug, Serialize, PartialEq, Eq, Clone)]
#[serde(untagged)]
pub enum ColumnMeta {
    Column(Described),
    Tag(Described),
}

impl Deref for ColumnMeta {
    type Target = Described;

    fn deref(&self) -> &Self::Target {
        match self {
            ColumnMeta::Column(v) => v,
            ColumnMeta::Tag(v) => v,
        }
    }
}

impl DerefMut for ColumnMeta {
    fn deref_mut(&mut self) -> &mut Self::Target {
        match self {
            ColumnMeta::Column(v) => v,
            ColumnMeta::Tag(v) => v,
        }
    }
}

#[inline(always)]
fn empty_as_none(s: &str) -> Option<String> {
    if s.is_empty() {
        None
    } else {
        Some(s.to_string())
    }
}
unsafe impl Send for ColumnMeta {}
impl<'de> Deserialize<'de> for ColumnMeta {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        enum Meta {
            Field,
            Type,
            Length,
            Note,
            Encode,
            Compress,
            Level,
        }

        impl<'de> Deserialize<'de> for Meta {
            fn deserialize<D>(deserializer: D) -> Result<Meta, D::Error>
            where
                D: Deserializer<'de>,
            {
                struct FieldVisitor;

                impl<'de> Visitor<'de> for FieldVisitor {
                    type Value = Meta;

                    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                        formatter.write_str(
                            "one of `field`, `type`, `length`, `note`, `encode`, `compress`, `level`",
                        )
                    }

                    fn visit_str<E>(self, value: &str) -> Result<Meta, E>
                    where
                        E: de::Error,
                    {
                        match value.to_lowercase().as_str() {
                            "field" => Ok(Meta::Field),
                            "type" => Ok(Meta::Type),
                            "length" => Ok(Meta::Length),
                            "note" => Ok(Meta::Note),
                            "encode" => Ok(Meta::Encode),
                            "compress" => Ok(Meta::Compress),
                            "level" => Ok(Meta::Level),
                            _ => Err(de::Error::unknown_field(value, FIELDS)),
                        }
                    }
                }

                deserializer.deserialize_identifier(FieldVisitor)
            }
        }

        struct MetaVisitor;

        impl<'de> Visitor<'de> for MetaVisitor {
            type Value = ColumnMeta;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("struct ColumnMeta")
            }

            fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>
            where
                V: SeqAccess<'de>,
            {
                let field = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(0, &self))?;
                let ty = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(1, &self))
                    .and_then(|s| Ty::from_str(s).map_err(de::Error::custom))?;
                let length = seq
                    .next_element()?
                    .ok_or_else(|| de::Error::invalid_length(2, &self))?;
                let note: Option<String> = seq
                    .next_element::<Option<&str>>()?
                    .and_then(|opt| opt)
                    .and_then(empty_as_none);

                // let is_primary_key = &note == "PRIMARY KEY";

                let encode: Option<String> = seq
                    .next_element::<Option<&str>>()?
                    .and_then(|opt| opt)
                    .and_then(empty_as_none);
                let compress: Option<String> = seq
                    .next_element::<Option<&str>>()?
                    .and_then(|opt| opt)
                    .and_then(empty_as_none);
                let level: Option<String> = seq
                    .next_element::<Option<&str>>()?
                    .and_then(|opt| opt)
                    .and_then(empty_as_none);

                let compression = if let (Some(encode), Some(compress), Some(level)) =
                    (encode, compress, level)
                {
                    Some(CompressOptions::new(encode, compress, level))
                } else {
                    None
                };
                let desc = Described {
                    field,
                    ty,
                    length,
                    note,
                    compression,
                };
                if !desc.is_tag() {
                    Ok(ColumnMeta::Column(desc))
                } else {
                    Ok(ColumnMeta::Tag(desc))
                }
            }

            fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
            where
                V: MapAccess<'de>,
            {
                let mut field = None;
                let mut ty = None;
                let mut length = None;
                let mut note = None;
                let mut encode = None;
                let mut compress = None;
                let mut level = None;
                while let Some(key) = map.next_key()? {
                    match key {
                        Meta::Field => {
                            if field.is_some() {
                                return Err(de::Error::duplicate_field("field"));
                            }
                            field = Some(map.next_value()?);
                        }
                        Meta::Type => {
                            if ty.is_some() {
                                return Err(de::Error::duplicate_field("type"));
                            }
                            let t: Ty = map.next_value()?;
                            ty = Some(t);
                        }
                        Meta::Length => {
                            if length.is_some() {
                                return Err(de::Error::duplicate_field("length"));
                            }
                            length = Some(map.next_value()?);
                        }
                        Meta::Note => {
                            if note.is_some() {
                                return Err(de::Error::duplicate_field("note"));
                            }
                            note = map.next_value::<Option<&str>>()?.and_then(empty_as_none);
                        }
                        Meta::Encode => {
                            if encode.is_some() {
                                return Err(de::Error::duplicate_field("encode"));
                            }
                            encode = map.next_value::<Option<&str>>()?.and_then(empty_as_none);
                        }
                        Meta::Compress => {
                            if compress.is_some() {
                                return Err(de::Error::duplicate_field("compress"));
                            }
                            compress = map.next_value::<Option<&str>>()?.and_then(empty_as_none);
                        }
                        Meta::Level => {
                            if level.is_some() {
                                return Err(de::Error::duplicate_field("level"));
                            }
                            level = map.next_value::<Option<&str>>()?.and_then(empty_as_none);
                        }
                    }
                }
                let field = field.ok_or_else(|| de::Error::missing_field("field"))?;
                let ty = ty.ok_or_else(|| de::Error::missing_field("type"))?;
                let length = length.ok_or_else(|| de::Error::missing_field("length"))?;
                let note = note.map(|s| s.to_string());
                let compression = if let (Some(encode), Some(compress), Some(level)) =
                    (encode, compress, level)
                {
                    Some(CompressOptions::new(encode, compress, level))
                } else {
                    None
                };
                let desc = Described {
                    field,
                    ty,
                    length,
                    note,
                    compression,
                };
                if !desc.is_tag() {
                    Ok(ColumnMeta::Column(desc))
                } else {
                    Ok(ColumnMeta::Tag(desc))
                }
            }
        }

        const FIELDS: &[&str] = &[
            "field", "type", "length", "note", "encode", "compress", "level",
        ];
        deserializer.deserialize_struct("ColumnMeta", FIELDS, MetaVisitor)
    }
}
impl ColumnMeta {
    pub fn field(&self) -> &str {
        match self {
            ColumnMeta::Column(desc) | ColumnMeta::Tag(desc) => desc.field.as_str(),
        }
    }
    pub fn ty(&self) -> Ty {
        match self {
            ColumnMeta::Column(desc) | ColumnMeta::Tag(desc) => desc.ty,
        }
    }
    pub fn length(&self) -> usize {
        match self {
            ColumnMeta::Column(desc) | ColumnMeta::Tag(desc) => desc.length,
        }
    }
    pub fn note(&self) -> &str {
        match self {
            ColumnMeta::Tag(_) => "TAG",
            _ => "",
        }
    }
    pub fn is_tag(&self) -> bool {
        matches!(self, ColumnMeta::Tag(_))
    }
}

#[test]
fn serde_meta() {
    // ordinary column
    let meta = ColumnMeta::Column(Described {
        field: "name".to_string(),
        ty: Ty::BigInt,
        length: 8,
        note: None,
        compression: None,
    });

    let sql = meta.deref().sql_repr();

    assert_eq!(sql, "`name` BIGINT");

    let a = serde_json::to_string(&meta).unwrap();

    let d: ColumnMeta = serde_json::from_str(&a).unwrap();

    assert_eq!(meta, d);

    // primary key column
    let meta = ColumnMeta::Column(Described {
        field: "name".to_string(),
        ty: Ty::BigInt,
        length: 8,
        note: Some("PRIMARY KEY".to_string()),
        compression: None,
    });
    let sql = meta.deref().sql_repr();

    assert_eq!(sql, "`name` BIGINT PRIMARY KEY");

    let a = serde_json::to_string(&meta).unwrap();

    let d: ColumnMeta = serde_json::from_str(&a).unwrap();

    assert_eq!(meta, d);

    // with compression
    let meta = ColumnMeta::Column(Described {
        field: "name".to_string(),
        ty: Ty::BigInt,
        length: 8,
        note: None,
        compression: Some(CompressOptions::new("delta-i", "lz4", "medium")),
    });
    let sql = meta.deref().sql_repr();

    assert_eq!(
        sql,
        "`name` BIGINT ENCODE 'delta-i' COMPRESS 'lz4' LEVEL 'medium'"
    );

    let a = serde_json::to_string(&meta).unwrap();

    let d: ColumnMeta = serde_json::from_str(&a).unwrap();

    assert_eq!(meta, d);

    // primary key with compression
    let meta = ColumnMeta::Column(Described {
        field: "name".to_string(),
        ty: Ty::BigInt,
        length: 8,
        note: Some("PRIMARY KEY".to_string()),
        compression: Some(CompressOptions::new("delta-i", "lz4", "medium")),
    });
    let sql = meta.deref().sql_repr();

    assert_eq!(
        sql,
        "`name` BIGINT ENCODE 'delta-i' COMPRESS 'lz4' LEVEL 'medium' PRIMARY KEY"
    );

    let a = serde_json::to_string(&meta).unwrap();

    let d: ColumnMeta = serde_json::from_str(&a).unwrap();

    assert_eq!(meta, d);

    // deserialize from sequence.
    let a = r#"["name", "BIGINT", 8, null, null, null, null]"#;
    let d: ColumnMeta = serde_json::from_str(a).unwrap();
    assert_eq!(
        d,
        ColumnMeta::Column(Described {
            field: "name".to_string(),
            ty: Ty::BigInt,
            length: 8,
            note: None,
            compression: None,
        })
    );
}