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.
enum | int | sql name | rust type |
---|---|---|---|
Null | 0 | NULL | None |
Bool | 1 | BOOL | bool |
TinyInt | 2 | TINYINT | i8 |
SmallInt | 3 | SMALLINT | i16 |
Int | 4 | INT | i32 |
BitInt | 5 | BIGINT | i64 |
Float | 6 | FLOAT | f32 |
Double | 7 | DOUBLE | f64 |
VarChar | 8 | BINARY/VARCHAR | str/String |
Timestamp | 9 | TIMESTAMP | i64 |
NChar | 10 | NCHAR | str/String |
UTinyInt | 11 | TINYINT UNSIGNED | u8 |
USmallInt | 12 | SMALLINT UNSIGNED | u16 |
UInt | 13 | INT UNSIGNED | u32 |
UBigInt | 14 | BIGINT UNSIGNED | u64 |
Json | 15 | JSON | serde_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
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§
source§impl Ty
impl Ty
sourcepub const fn is_var_type(&self) -> bool
pub const fn is_var_type(&self) -> bool
Var type is one of Ty::VarChar, Ty::VarBinary, Ty::NChar.
Examples found in repository?
More examples
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())
}
}
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()
)),
}
}
pub const fn is_json(&self) -> bool
sourcepub const fn is_primitive(&self) -> bool
pub const fn is_primitive(&self) -> bool
Is one of boolean/integers/float/double/decimal
Examples found in repository?
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
}
sourcepub const fn fixed_length(&self) -> usize
pub const fn fixed_length(&self) -> usize
Get fixed length if the type is primitive.
Examples found in repository?
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
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
}
sourcepub const fn name(&self) -> &'static str
pub const fn name(&self) -> &'static str
The sql name of type.
Examples found in repository?
More examples
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())
}
}
pub const fn lowercase_name(&self) -> &'static str
Trait Implementations§
source§impl<'de> Deserialize<'de> for Ty
impl<'de> Deserialize<'de> for Ty
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
impl Copy for Ty
impl Eq for Ty
impl StructuralEq for Ty
impl StructuralPartialEq for Ty
Auto Trait Implementations§
impl RefUnwindSafe for Ty
impl Send for Ty
impl Sync for Ty
impl Unpin for Ty
impl UnwindSafe for Ty
Blanket Implementations§
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
self
, then passes self.as_mut()
into the pipe
function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds.