Enum taos_query::common::Ty

source ·
#[repr(u8)]
#[non_exhaustive]
pub enum Ty {
Show 15 variants Bool, TinyInt, SmallInt, Int, BigInt, UTinyInt, USmallInt, UInt, UBigInt, Float, Double, Timestamp, VarChar, NChar, Json, // some variants omitted
}
Expand description

TDengine data type enumeration.

enumintsql namerust type
Null0NULLNone
Bool1BOOLbool
TinyInt2TINYINTi8
SmallInt3SMALLINTi16
Int4INTi32
BitInt5BIGINTi64
Float6FLOATf32
Double7DOUBLEf64
VarChar8BINARY/VARCHARstr/String
Timestamp9TIMESTAMPi64
NChar10NCHARstr/String
UTinyInt11TINYINT UNSIGNEDu8
USmallInt12SMALLINT UNSIGNEDu16
UInt13INT UNSIGNEDu32
UBigInt14BIGINT UNSIGNEDu64
Json15JSONserde_json::Value

Note:

  • VarChar sql name is BINARY in v2, and VARCHAR in v3.
  • Decimal/Blob/MediumBlob is not supported in 2.0/3.0 .

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Bool

The BOOL type in sql, will be represented as bool in Rust.

§

TinyInt

TINYINT type in sql, will be represented in Rust as i8.

§

SmallInt

SMALLINT type in sql, will be represented in Rust as i16.

§

Int

INT type in sql, will be represented in Rust as i32.

§

BigInt

BIGINT type in sql, will be represented in Rust as i64.

§

UTinyInt

UTinyInt, tinyint unsigned in sql, u8 in Rust.

§

USmallInt

12: USmallInt, smallint unsigned in sql, u16 in Rust.

§

UInt

13: UInt, int unsigned in sql, u32 in Rust.

§

UBigInt

14: UBigInt, bigint unsigned in sql, u64 in Rust.

§

Float

6: Float, float type in sql, will be represented in Rust as f32.

§

Double

7: Double, tinyint type in sql, will be represented in Rust as f64.

§

Timestamp

9: Timestamp, timestamp type in sql, will be represented as i64 in Rust. But can be deserialized to chrono::naive::NaiveDateTime or String.

§

VarChar

8: VarChar, binary type in sql for TDengine 2.x, varchar for TDengine 3.x, will be represented in Rust as &str or String. This type of data be deserialized to Vec.

§

NChar

10: NChar, nchar type in sql, the recommended way in TDengine to store utf-8 String.

§

Json

15: Json, json tag in sql, will be represented as serde_json::value::Value in Rust.

Implementations§

Check if the data type is null or not.

Var type is one of Ty::VarChar, Ty::VarBinary, Ty::NChar.

Examples found in repository?
src/helpers/describe.rs (line 28)
26
27
28
29
30
31
32
33
    pub fn sql_repr(&self) -> String {
        let ty = self.ty;
        if ty.is_var_type() {
            format!("{} {}({})", self.field, ty, self.length)
        } else {
            format!("{} {}", self.field, self.ty)
        }
    }
More examples
Hide additional examples
src/common/field.rs (line 122)
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    pub fn sql_repr(&self) -> String {
        let ty = self.ty();
        if ty.is_var_type() {
            format!("`{}` {}({})", self.name(), ty.name(), self.bytes())
        } else {
            format!("`{}` {}", self.name(), ty.name())
        }
    }
}

impl Display for Field {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let ty = self.ty();
        if ty.is_var_type() {
            write!(f, "`{}` {}({})", self.name(), ty.name(), self.bytes())
        } else {
            write!(f, "`{}` {}", self.name(), ty.name())
        }
    }
src/common/raw/meta.rs (line 335)
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
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.alter_type {
            AlterType::AddTag => f.write_fmt(format_args!(
                "ALTER TABLE `{}` ADD TAG {}",
                self.table_name,
                self.field.sql_repr()
            )),
            AlterType::DropTag => f.write_fmt(format_args!(
                "ALTER TABLE `{}` DROP TAG `{}`",
                self.table_name,
                self.field.name()
            )),
            AlterType::RenameTag => f.write_fmt(format_args!(
                "ALTER TABLE `{}` RENAME TAG `{}` `{}`",
                self.table_name,
                self.field.name(),
                self.col_new_name.as_ref().unwrap()
            )),
            AlterType::SetTagValue => {
                f.write_fmt(format_args!(
                    "ALTER TABLE `{}` SET TAG `{}` ",
                    self.table_name,
                    self.field.name()
                ))?;
                if self.col_value_null.unwrap_or(false) {
                    f.write_str("NULL")
                } else if self.field.ty.is_var_type() {
                    f.write_fmt(format_args!("'{}'", self.col_value.as_ref().unwrap()))
                } else {
                    f.write_fmt(format_args!("{}", self.col_value.as_ref().unwrap()))
                }
            }
            AlterType::AddColumn => f.write_fmt(format_args!(
                "ALTER TABLE `{}` ADD COLUMN {}",
                self.table_name,
                self.field.sql_repr()
            )),
            AlterType::DropColumn => f.write_fmt(format_args!(
                "ALTER TABLE `{}` DROP COLUMN `{}`",
                self.table_name,
                self.field.name()
            )),
            AlterType::ModifyColumnLength => f.write_fmt(format_args!(
                "ALTER TABLE `{}` MODIFY COLUMN {}",
                self.table_name,
                self.field.sql_repr(),
            )),
            AlterType::ModifyTagLength => f.write_fmt(format_args!(
                "ALTER TABLE `{}` MODIFY TAG {}",
                self.table_name,
                self.field.sql_repr(),
            )),
            AlterType::ModifyTableOption => todo!(),
            AlterType::RenameColumn => f.write_fmt(format_args!(
                "ALTER TABLE `{}` RENAME COLUMN `{}` `{}`",
                self.table_name,
                self.field.name(),
                self.col_new_name.as_ref().unwrap()
            )),
        }
    }

Is one of boolean/integers/float/double/decimal

Examples found in repository?
src/common/raw/views/mod.rs (line 458)
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
pub fn views_to_raw_block(views: &[ColumnView]) -> Vec<u8> {
    let mut header = super::Header::default();

    header.nrows = views.first().map(|v| v.len()).unwrap_or(0) as _;
    header.ncols = views.len() as _;

    let ncols = views.len();

    let mut bytes = Vec::new();
    bytes.extend(header.as_bytes());

    let schemas = views
        .iter()
        .map(|view| {
            let ty = view.as_ty();
            ColSchema {
                ty,
                len: ty.fixed_length() as _,
            }
        })
        .collect_vec();
    let schema_bytes = unsafe {
        std::slice::from_raw_parts(
            schemas.as_ptr() as *const u8,
            ncols * std::mem::size_of::<ColSchema>(),
        )
    };
    bytes.write_all(schema_bytes).unwrap();

    let length_offset = bytes.len();
    bytes.resize(bytes.len() + ncols * std::mem::size_of::<u32>(), 0);

    let mut lengths = Vec::with_capacity(ncols);
    lengths.resize(ncols, 0);
    for (i, view) in views.iter().enumerate() {
        let cur = bytes.len();
        let n = view.write_raw_into(&mut bytes).unwrap();
        let len = bytes.len();
        debug_assert!(cur + n == len);
        if !view.as_ty().is_primitive() {
            lengths[i] = (n - header.nrows() * 4) as _;
        } else {
            lengths[i] = (header.nrows() * view.as_ty().fixed_length()) as _;
        }
    }
    unsafe {
        (*(bytes.as_mut_ptr() as *mut super::Header)).length = bytes.len() as _;
        std::ptr::copy(
            lengths.as_ptr(),
            bytes.as_mut_ptr().offset(length_offset as isize) as *mut u32,
            lengths.len(),
        );
    }
    bytes
}

Get fixed length if the type is primitive.

Examples found in repository?
src/common/itypes.rs (line 112)
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
    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()
    }
More examples
Hide additional examples
src/stmt/column.rs (line 60)
58
59
60
61
62
63
64
65
    fn from_primitive<T: IsValue>(v: &T) -> Self {
        let mut param = RawMultiBind::new(T::TY);
        param.buffer_length = T::TY.fixed_length();
        param.buffer = box_into_raw(v.clone()) as *const T as _;
        param.length = box_into_raw(param.buffer_length) as _;
        param.is_null = box_into_raw(0) as _;
        param
    }
src/common/raw/views/mod.rs (line 436)
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
pub fn views_to_raw_block(views: &[ColumnView]) -> Vec<u8> {
    let mut header = super::Header::default();

    header.nrows = views.first().map(|v| v.len()).unwrap_or(0) as _;
    header.ncols = views.len() as _;

    let ncols = views.len();

    let mut bytes = Vec::new();
    bytes.extend(header.as_bytes());

    let schemas = views
        .iter()
        .map(|view| {
            let ty = view.as_ty();
            ColSchema {
                ty,
                len: ty.fixed_length() as _,
            }
        })
        .collect_vec();
    let schema_bytes = unsafe {
        std::slice::from_raw_parts(
            schemas.as_ptr() as *const u8,
            ncols * std::mem::size_of::<ColSchema>(),
        )
    };
    bytes.write_all(schema_bytes).unwrap();

    let length_offset = bytes.len();
    bytes.resize(bytes.len() + ncols * std::mem::size_of::<u32>(), 0);

    let mut lengths = Vec::with_capacity(ncols);
    lengths.resize(ncols, 0);
    for (i, view) in views.iter().enumerate() {
        let cur = bytes.len();
        let n = view.write_raw_into(&mut bytes).unwrap();
        let len = bytes.len();
        debug_assert!(cur + n == len);
        if !view.as_ty().is_primitive() {
            lengths[i] = (n - header.nrows() * 4) as _;
        } else {
            lengths[i] = (header.nrows() * view.as_ty().fixed_length()) as _;
        }
    }
    unsafe {
        (*(bytes.as_mut_ptr() as *mut super::Header)).length = bytes.len() as _;
        std::ptr::copy(
            lengths.as_ptr(),
            bytes.as_mut_ptr().offset(length_offset as isize) as *mut u32,
            lengths.len(),
        );
    }
    bytes
}

The sql name of type.

Examples found in repository?
src/common/ty.rs (line 359)
358
359
360
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name())
    }
More examples
Hide additional examples
src/common/field.rs (line 123)
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    pub fn sql_repr(&self) -> String {
        let ty = self.ty();
        if ty.is_var_type() {
            format!("`{}` {}({})", self.name(), ty.name(), self.bytes())
        } else {
            format!("`{}` {}", self.name(), ty.name())
        }
    }
}

impl Display for Field {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let ty = self.ty();
        if ty.is_var_type() {
            write!(f, "`{}` {}({})", self.name(), ty.name(), self.bytes())
        } else {
            write!(f, "`{}` {}", self.name(), ty.name())
        }
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts self into T using Into<T>. Read more
Causes self to use its Binary implementation when Debug-formatted.
Causes self to use its Display implementation when Debug-formatted.
Causes self to use its LowerExp implementation when Debug-formatted.
Causes self to use its LowerHex implementation when Debug-formatted.
Causes self to use its Octal implementation when Debug-formatted.
Causes self to use its Pointer implementation when Debug-formatted.
Causes self to use its UpperExp implementation when Debug-formatted.
Causes self to use its UpperHex implementation when Debug-formatted.
Formats each item in a sequence. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Pipes by value. This is generally the method you want to use. Read more
Borrows self and passes that borrow into the pipe function. Read more
Mutably borrows self and passes that borrow into the pipe function. Read more
Borrows self, then passes self.borrow() into the pipe function. Read more
Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Borrows self, then passes self.as_ref() into the pipe function.
Mutably borrows self, then passes self.as_mut() into the pipe function.
Borrows self, then passes self.deref() into the pipe function.
Mutably borrows self, then passes self.deref_mut() into the pipe function.
Immutable access to a value. Read more
Mutable access to a value. Read more
Immutable access to the Borrow<B> of a value. Read more
Mutable access to the BorrowMut<B> of a value. Read more
Immutable access to the AsRef<R> view of a value. Read more
Mutable access to the AsMut<R> view of a value. Read more
Immutable access to the Deref::Target of a value. Read more
Mutable access to the Deref::Target of a value. Read more
Calls .tap() only in debug builds, and is erased in release builds.
Calls .tap_mut() only in debug builds, and is erased in release builds.
Calls .tap_borrow() only in debug builds, and is erased in release builds.
Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
Calls .tap_ref() only in debug builds, and is erased in release builds.
Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
Calls .tap_deref() only in debug builds, and is erased in release builds.
Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
Attempts to convert self into T using TryInto<T>. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.