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
use derive_more::{Deref, DerefMut, Display, From};

use rust_decimal::prelude::*;
use serde::{Deserialize, Serialize};
use serde_json::Value as Json;

use super::{Ty, Value};

pub type INull = ();
pub type IBool = bool;
pub type ITinyInt = i8;
pub type ISmallInt = i16;
pub type IInt = i32;
pub type IBigInt = i64;
pub type IUTinyInt = u8;
pub type IUSmallInt = u16;
pub type IUInt = u32;
pub type IUBigInt = u64;
pub type IFloat = f32;
pub type IDouble = f64;
pub type IJson = Json;
pub type IDecimal = Decimal;

#[derive(Debug, Clone, Copy, Deref, DerefMut, Deserialize, Serialize, Display, From)]
pub struct ITimestamp(pub i64);

#[derive(Debug, Deref, DerefMut, Clone, Deserialize, Serialize)]
pub struct IVarChar(String);

impl AsRef<str> for IVarChar {
    fn as_ref(&self) -> &str {
        self.0.as_str()
    }
}
impl AsRef<[u8]> for IVarChar {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

/// Alias of [IVarChar].
pub type IBinary = IVarChar;

#[derive(Debug, Deref, DerefMut, Clone, From, Deserialize, Serialize)]
pub struct INChar(String);

impl AsRef<str> for INChar {
    fn as_ref(&self) -> &str {
        self.0.as_str()
    }
}
impl AsRef<[u8]> for INChar {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

#[derive(Debug, Deref, DerefMut, Clone, From, Deserialize, Serialize)]
pub struct IVarBinary(Vec<u8>);
#[allow(dead_code)]
pub struct IGeometry(Vec<u8>);

#[derive(Debug, Deref, DerefMut, Clone, From, Deserialize, Serialize)]
pub struct IMediumBlob(Vec<u8>);

#[derive(Debug, Deref, DerefMut, Clone, From, Deserialize, Serialize)]
pub struct IBlob(Vec<u8>);

impl From<String> for IVarChar {
    fn from(v: String) -> Self {
        Self(v)
    }
}
impl From<&str> for IVarChar {
    fn from(v: &str) -> Self {
        Self(v.to_string())
    }
}

impl From<IVarChar> for String {
    fn from(v: IVarChar) -> Self {
        v.0
    }
}

impl From<INChar> for String {
    fn from(v: INChar) -> Self {
        v.0
    }
}

// impl From<IJson> for String {
//     fn from(v: IJson) -> Self {
//         v.0.to_string()
//     }
// }

impl IVarChar {
    pub const fn new() -> Self {
        Self(String::new())
    }
    pub fn with_capacity(cap: usize) -> Self {
        Self(String::with_capacity(cap))
    }
}
pub trait IsValue: Sized + Clone {
    const TY: Ty;

    fn is_null(&self) -> bool {
        false
    }

    fn is_primitive(&self) -> bool {
        std::mem::size_of::<Self>() == Self::TY.fixed_length()
    }

    fn fixed_length(&self) -> usize {
        std::mem::size_of::<Self>()
    }

    fn as_timestamp(&self) -> i64 {
        debug_assert!(Self::TY == Ty::Timestamp);
        unimplemented!()
    }

    fn as_var_char(&self) -> &str {
        debug_assert!(Self::TY == Ty::VarChar);
        unimplemented!()
    }

    fn as_nchar(&self) -> &str {
        debug_assert!(Self::TY == Ty::NChar);
        unimplemented!()
    }

    fn as_medium_blob(&self) -> &[u8] {
        debug_assert!(Self::TY == Ty::MediumBlob);
        unimplemented!()
    }

    fn as_blob(&self) -> &[u8] {
        debug_assert!(Self::TY == Ty::Blob);
        unimplemented!()
    }
}

impl<T> IsValue for Option<T>
where
    T: IsValue,
{
    const TY: Ty = T::TY;

    fn is_null(&self) -> bool {
        self.is_none()
    }

    fn is_primitive(&self) -> bool {
        self.as_ref().unwrap().is_primitive()
    }

    fn as_timestamp(&self) -> i64 {
        self.as_ref().unwrap().as_timestamp()
    }

    fn as_var_char(&self) -> &str {
        self.as_ref().unwrap().as_var_char()
    }
    fn as_nchar(&self) -> &str {
        self.as_ref().unwrap().as_nchar()
    }
    fn as_medium_blob(&self) -> &[u8] {
        self.as_ref().unwrap().as_medium_blob()
    }
    fn as_blob(&self) -> &[u8] {
        self.as_ref().unwrap().as_blob()
    }
}

pub trait IValue: Sized {
    const TY: Ty;

    type Inner: Sized;

    fn is_null(&self) -> bool {
        false
    }

    fn into_value(self) -> Value;

    fn into_inner(self) -> Self::Inner;
}

impl IValue for INull {
    const TY: Ty = Ty::Null;

    fn is_null(&self) -> bool {
        true
    }
    fn into_value(self) -> Value {
        Value::Null(Ty::Null)
    }

    type Inner = ();

    fn into_inner(self) -> Self::Inner {}
}

/// Primitive type to TDengine data type.
macro_rules! impl_prim {
    ($($ty:ident = $inner:ty)*) => {
        $(paste::paste! {
            impl IValue for [<I $ty>] {
                const TY: Ty = Ty::$ty;
                type Inner = $inner;

                #[inline]
                fn is_null(&self) -> bool {
                    false
                }

                #[inline]
                fn into_value(self) -> Value {
                    Value::$ty(self)
                }

                #[inline]
                fn into_inner(self) -> Self::Inner {
                    self
                }
            }
        })*
    };
}

impl_prim!(
    Bool = bool
    TinyInt = i8
    SmallInt =  i16
    Int = i32
    BigInt = i64
    UTinyInt = u8
    USmallInt = u16
    UInt = u32
    UBigInt = u64
    Float = f32
    Double = f64
    Decimal = Decimal
    Json = Json
);

pub trait IsPrimitive: Copy {
    const TY: Ty;
    fn is_primitive(&self) -> bool {
        std::mem::size_of::<Self>() == Self::TY.fixed_length()
    }
}

macro_rules! impl_is_primitive {
    ($($ty:ident) *) => {
        $(paste::paste! {
            impl IsPrimitive for [<I $ty>] {
                const TY: Ty = Ty::$ty;
            }
            impl IsValue for [<I $ty>] {
                const TY: Ty = Ty::$ty;
            }
        })*
    };
}

impl_is_primitive!(
    Bool TinyInt SmallInt Int BigInt
    UTinyInt USmallInt UInt UBigInt
    Float Double Decimal
);

impl IsValue for ITimestamp {
    const TY: Ty = Ty::Timestamp;

    #[inline]
    fn as_timestamp(&self) -> i64 {
        self.0
    }
}

impl IsValue for IVarChar {
    const TY: Ty = Ty::VarChar;

    #[inline]
    fn as_var_char(&self) -> &str {
        &self.0
    }
}

impl IsValue for INChar {
    const TY: Ty = Ty::NChar;

    #[inline]
    fn as_nchar(&self) -> &str {
        &self.0
    }
}

pub trait IsVarChar {
    fn as_var_char(&self) -> &str;
}

impl IsVarChar for IVarChar {
    fn as_var_char(&self) -> &str {
        &self.0
    }
}

pub trait IsNChar {
    fn as_nchar(&self) -> &str;
}

impl IsNChar for INChar {
    fn as_nchar(&self) -> &str {
        &self.0
    }
}

pub trait IsJson {
    fn to_json(&self) -> String;
}

impl IsJson for IJson {
    fn to_json(&self) -> String {
        self.to_string()
    }
}

pub trait IsMediumBlob {
    fn as_medium_blob(&self) -> &[u8];
}

impl IsMediumBlob for IMediumBlob {
    fn as_medium_blob(&self) -> &[u8] {
        &self.0
    }
}

pub trait IsBlob {
    fn as_blob(&self) -> &[u8];
}

impl IsBlob for IBlob {
    fn as_blob(&self) -> &[u8] {
        &self.0
    }
}

impl IValue for ITimestamp {
    const TY: Ty = Ty::Timestamp;

    type Inner = i64;

    fn into_value(self) -> Value {
        todo!()
    }

    fn into_inner(self) -> Self::Inner {
        self.0
    }
}

// macro_rules! impl_wrapper_struct {
//     ($($ty:ident)*) => {
//         $(paste::paste! {
//             impl IValue for [<I $ty>] {
//                 const TY: Ty = Ty::$ty;
//                 #[inline]
//                 fn into_value(self) -> Value {
//                     Value::$ty(self.0)
//                 }
//             }
//         })*
//     };
//     ($($ty:ident, $inner:ty;)*) => {
//         $(paste::paste! {
//             #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Deserialize, Serialize)]
//             pub struct [<I $ty>]($inner);

//             impl Deref for [<I $ty>] {
//                 type Target = $inner;

//                 #[inline]
//                 fn deref(&self) -> &Self::Target {
//                         &self.0
//                 }
//             }

//             impl IValue for [<I $ty>] {
//                 const TY: Ty = Ty::$ty;
//                 #[inline]
//                 fn into_value(self) -> Value {
//                     Value::$ty(self.0)
//                 }
//             }
//         })*
//     };
// }