ttypes.h 13.9 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _TD_COMMON_TTYPE_H_
#define _TD_COMMON_TTYPE_H_
H
Haojun Liao 已提交
18 19 20 21 22 23

#ifdef __cplusplus
extern "C" {
#endif

#include "taosdef.h"
H
Hongze Cheng 已提交
24
#include "types.h"
H
Haojun Liao 已提交
25 26

// ----------------- For variable data types such as TSDB_DATA_TYPE_BINARY and TSDB_DATA_TYPE_NCHAR
C
Cary Xu 已提交
27
typedef uint32_t TDRowLenT;
C
update  
Cary Xu 已提交
28
typedef uint8_t  TDRowValT;
C
Cary Xu 已提交
29
typedef uint64_t TDRowVerT;
X
Xiaoyu Wang 已提交
30
typedef int16_t  col_id_t;
C
Cary Xu 已提交
31
typedef int8_t   col_type_t;
32
typedef int32_t  col_bytes_t;
dengyihao's avatar
dengyihao 已提交
33
typedef int32_t  schema_ver_t;
C
Cary Xu 已提交
34
typedef int32_t  func_id_t;
H
Haojun Liao 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47

#pragma pack(push, 1)
typedef struct {
  VarDataLenT len;
  uint8_t     data;
} SBinaryNullT;

typedef struct {
  VarDataLenT len;
  uint32_t    data;
} SNCharNullT;
#pragma pack(pop)

S
Shengliang Guan 已提交
48 49 50 51
#define varDataTLen(v)         (sizeof(VarDataLenT) + varDataLen(v))
#define varDataCopy(dst, v)    memcpy((dst), (void *)(v), varDataTLen(v))
#define varDataLenByData(v)    (*(VarDataLenT *)(((char *)(v)) - VARSTR_HEADER_SIZE))
#define varDataSetLen(v, _len) (((VarDataLenT *)(v))[0] = (VarDataLenT)(_len))
dengyihao's avatar
dengyihao 已提交
52 53 54
#define IS_VAR_DATA_TYPE(t) \
  (((t) == TSDB_DATA_TYPE_VARCHAR) || ((t) == TSDB_DATA_TYPE_NCHAR) || ((t) == TSDB_DATA_TYPE_JSON))
#define IS_STR_DATA_TYPE(t) (((t) == TSDB_DATA_TYPE_VARCHAR) || ((t) == TSDB_DATA_TYPE_NCHAR))
H
Haojun Liao 已提交
55

S
Shengliang Guan 已提交
56 57
#define varDataNetLen(v)  (htons(((VarDataLenT *)(v))[0]))
#define varDataNetTLen(v) (sizeof(VarDataLenT) + varDataNetLen(v))
H
Haojun Liao 已提交
58 59

// this data type is internally used only in 'in' query to hold the values
S
Shengliang Guan 已提交
60 61
#define TSDB_DATA_TYPE_POINTER_ARRAY (1000)
#define TSDB_DATA_TYPE_VALUE_ARRAY   (1001)
H
Haojun Liao 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

#define GET_TYPED_DATA(_v, _finalType, _type, _data) \
  do {                                               \
    switch (_type) {                                 \
      case TSDB_DATA_TYPE_BOOL:                      \
      case TSDB_DATA_TYPE_TINYINT:                   \
        (_v) = (_finalType)GET_INT8_VAL(_data);      \
        break;                                       \
      case TSDB_DATA_TYPE_UTINYINT:                  \
        (_v) = (_finalType)GET_UINT8_VAL(_data);     \
        break;                                       \
      case TSDB_DATA_TYPE_SMALLINT:                  \
        (_v) = (_finalType)GET_INT16_VAL(_data);     \
        break;                                       \
      case TSDB_DATA_TYPE_USMALLINT:                 \
        (_v) = (_finalType)GET_UINT16_VAL(_data);    \
        break;                                       \
S
Shengliang Guan 已提交
79
      case TSDB_DATA_TYPE_TIMESTAMP:                 \
H
Haojun Liao 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      case TSDB_DATA_TYPE_BIGINT:                    \
        (_v) = (_finalType)(GET_INT64_VAL(_data));   \
        break;                                       \
      case TSDB_DATA_TYPE_UBIGINT:                   \
        (_v) = (_finalType)(GET_UINT64_VAL(_data));  \
        break;                                       \
      case TSDB_DATA_TYPE_FLOAT:                     \
        (_v) = (_finalType)GET_FLOAT_VAL(_data);     \
        break;                                       \
      case TSDB_DATA_TYPE_DOUBLE:                    \
        (_v) = (_finalType)GET_DOUBLE_VAL(_data);    \
        break;                                       \
      case TSDB_DATA_TYPE_UINT:                      \
        (_v) = (_finalType)GET_UINT32_VAL(_data);    \
        break;                                       \
D
dapan1121 已提交
95
      case TSDB_DATA_TYPE_INT:                       \
H
Haojun Liao 已提交
96 97
        (_v) = (_finalType)GET_INT32_VAL(_data);     \
        break;                                       \
D
dapan1121 已提交
98 99 100
      default:                                       \
        (_v) = (_finalType)varDataLen(_data);        \
        break;                                       \
H
Haojun Liao 已提交
101 102 103 104 105 106 107
    }                                                \
  } while (0)

#define SET_TYPED_DATA(_v, _type, _data)       \
  do {                                         \
    switch (_type) {                           \
      case TSDB_DATA_TYPE_BOOL:                \
S
Shengliang Guan 已提交
108
        *(bool *)(_v) = (bool)(_data);         \
D
dapan1121 已提交
109
        break;                                 \
H
Haojun Liao 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122
      case TSDB_DATA_TYPE_TINYINT:             \
        *(int8_t *)(_v) = (int8_t)(_data);     \
        break;                                 \
      case TSDB_DATA_TYPE_UTINYINT:            \
        *(uint8_t *)(_v) = (uint8_t)(_data);   \
        break;                                 \
      case TSDB_DATA_TYPE_SMALLINT:            \
        *(int16_t *)(_v) = (int16_t)(_data);   \
        break;                                 \
      case TSDB_DATA_TYPE_USMALLINT:           \
        *(uint16_t *)(_v) = (uint16_t)(_data); \
        break;                                 \
      case TSDB_DATA_TYPE_BIGINT:              \
D
dapan1121 已提交
123
      case TSDB_DATA_TYPE_TIMESTAMP:           \
H
Haojun Liao 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137
        *(int64_t *)(_v) = (int64_t)(_data);   \
        break;                                 \
      case TSDB_DATA_TYPE_UBIGINT:             \
        *(uint64_t *)(_v) = (uint64_t)(_data); \
        break;                                 \
      case TSDB_DATA_TYPE_FLOAT:               \
        *(float *)(_v) = (float)(_data);       \
        break;                                 \
      case TSDB_DATA_TYPE_DOUBLE:              \
        *(double *)(_v) = (double)(_data);     \
        break;                                 \
      case TSDB_DATA_TYPE_UINT:                \
        *(uint32_t *)(_v) = (uint32_t)(_data); \
        break;                                 \
D
dapan1121 已提交
138
      case TSDB_DATA_TYPE_INT:                 \
H
Haojun Liao 已提交
139 140
        *(int32_t *)(_v) = (int32_t)(_data);   \
        break;                                 \
D
dapan1121 已提交
141 142
      default:                                 \
        break;                                 \
H
Haojun Liao 已提交
143 144 145
    }                                          \
  } while (0)

dengyihao's avatar
dengyihao 已提交
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
#define NUM_TO_STRING(_inputType, _input, _outputBytes, _output)                       \
  do {                                                                                 \
    switch (_inputType) {                                                              \
      case TSDB_DATA_TYPE_TINYINT:                                                     \
        snprintf(_output, (int32_t)(_outputBytes), "%d", *(int8_t *)(_input));         \
        break;                                                                         \
      case TSDB_DATA_TYPE_UTINYINT:                                                    \
        snprintf(_output, (int32_t)(_outputBytes), "%d", *(uint8_t *)(_input));        \
        break;                                                                         \
      case TSDB_DATA_TYPE_SMALLINT:                                                    \
        snprintf(_output, (int32_t)(_outputBytes), "%d", *(int16_t *)(_input));        \
        break;                                                                         \
      case TSDB_DATA_TYPE_USMALLINT:                                                   \
        snprintf(_output, (int32_t)(_outputBytes), "%d", *(uint16_t *)(_input));       \
        break;                                                                         \
      case TSDB_DATA_TYPE_TIMESTAMP:                                                   \
      case TSDB_DATA_TYPE_BIGINT:                                                      \
        snprintf(_output, (int32_t)(_outputBytes), "%" PRId64, *(int64_t *)(_input));  \
        break;                                                                         \
      case TSDB_DATA_TYPE_UBIGINT:                                                     \
        snprintf(_output, (int32_t)(_outputBytes), "%" PRIu64, *(uint64_t *)(_input)); \
        break;                                                                         \
      case TSDB_DATA_TYPE_FLOAT:                                                       \
        snprintf(_output, (int32_t)(_outputBytes), "%f", *(float *)(_input));          \
        break;                                                                         \
      case TSDB_DATA_TYPE_DOUBLE:                                                      \
        snprintf(_output, (int32_t)(_outputBytes), "%f", *(double *)(_input));         \
        break;                                                                         \
      case TSDB_DATA_TYPE_UINT:                                                        \
        snprintf(_output, (int32_t)(_outputBytes), "%u", *(uint32_t *)(_input));       \
        break;                                                                         \
      default:                                                                         \
        snprintf(_output, (int32_t)(_outputBytes), "%d", *(int32_t *)(_input));        \
        break;                                                                         \
    }                                                                                  \
G
Ganlin Zhao 已提交
181 182
  } while (0)

183

184
//TODO: use varchar(0) to represent NULL type
185
#define IS_VAR_NULL_TYPE(_t, _b)     ((_t) == TSDB_DATA_TYPE_VARCHAR && (_b) == 0)
186
#define IS_NULL_TYPE(_t)             ((_t) == TSDB_DATA_TYPE_NULL)
187

H
Haojun Liao 已提交
188 189 190
#define IS_SIGNED_NUMERIC_TYPE(_t)   ((_t) >= TSDB_DATA_TYPE_TINYINT && (_t) <= TSDB_DATA_TYPE_BIGINT)
#define IS_UNSIGNED_NUMERIC_TYPE(_t) ((_t) >= TSDB_DATA_TYPE_UTINYINT && (_t) <= TSDB_DATA_TYPE_UBIGINT)
#define IS_FLOAT_TYPE(_t)            ((_t) == TSDB_DATA_TYPE_FLOAT || (_t) == TSDB_DATA_TYPE_DOUBLE)
191
#define IS_INTEGER_TYPE(_t)          ((IS_SIGNED_NUMERIC_TYPE(_t)) || (IS_UNSIGNED_NUMERIC_TYPE(_t)))
H
Haojun Liao 已提交
192 193

#define IS_NUMERIC_TYPE(_t) ((IS_SIGNED_NUMERIC_TYPE(_t)) || (IS_UNSIGNED_NUMERIC_TYPE(_t)) || (IS_FLOAT_TYPE(_t)))
dengyihao's avatar
dengyihao 已提交
194 195
#define IS_MATHABLE_TYPE(_t) \
  (IS_NUMERIC_TYPE(_t) || (_t) == (TSDB_DATA_TYPE_BOOL) || (_t) == (TSDB_DATA_TYPE_TIMESTAMP))
H
Haojun Liao 已提交
196

wmmhello's avatar
wmmhello 已提交
197 198 199 200 201 202 203 204
#define IS_VALID_TINYINT(_t)   ((_t) >= INT8_MIN && (_t) <= INT8_MAX)
#define IS_VALID_SMALLINT(_t)  ((_t) >= INT16_MIN && (_t) <= INT16_MAX)
#define IS_VALID_INT(_t)       ((_t) >= INT32_MIN && (_t) <= INT32_MAX)
#define IS_VALID_BIGINT(_t)    ((_t) >= INT64_MIN && (_t) <= INT64_MAX)
#define IS_VALID_UTINYINT(_t)  ((_t) >= 0 && (_t) <= UINT8_MAX)
#define IS_VALID_USMALLINT(_t) ((_t) >= 0 && (_t) <= UINT16_MAX)
#define IS_VALID_UINT(_t)      ((_t) >= 0 && (_t) <= UINT32_MAX)
#define IS_VALID_UBIGINT(_t)   ((_t) >= 0 && (_t) <= UINT64_MAX)
S
Shengliang Guan 已提交
205 206 207 208 209
#define IS_VALID_FLOAT(_t)     ((_t) >= -FLT_MAX && (_t) <= FLT_MAX)
#define IS_VALID_DOUBLE(_t)    ((_t) >= -DBL_MAX && (_t) <= DBL_MAX)

#define IS_CONVERT_AS_SIGNED(_t) \
  (IS_SIGNED_NUMERIC_TYPE(_t) || (_t) == (TSDB_DATA_TYPE_BOOL) || (_t) == (TSDB_DATA_TYPE_TIMESTAMP))
D
dapan1121 已提交
210 211
#define IS_CONVERT_AS_UNSIGNED(_t) (IS_UNSIGNED_NUMERIC_TYPE(_t) || (_t) == (TSDB_DATA_TYPE_BOOL))

212
// TODO remove this function
H
Haojun Liao 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
static FORCE_INLINE bool isNull(const void *val, int32_t type) {
  switch (type) {
    case TSDB_DATA_TYPE_BOOL:
      return *(uint8_t *)val == TSDB_DATA_BOOL_NULL;
    case TSDB_DATA_TYPE_TINYINT:
      return *(uint8_t *)val == TSDB_DATA_TINYINT_NULL;
    case TSDB_DATA_TYPE_SMALLINT:
      return *(uint16_t *)val == TSDB_DATA_SMALLINT_NULL;
    case TSDB_DATA_TYPE_INT:
      return *(uint32_t *)val == TSDB_DATA_INT_NULL;
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP:
      return *(uint64_t *)val == TSDB_DATA_BIGINT_NULL;
    case TSDB_DATA_TYPE_FLOAT:
      return *(uint32_t *)val == TSDB_DATA_FLOAT_NULL;
    case TSDB_DATA_TYPE_DOUBLE:
      return *(uint64_t *)val == TSDB_DATA_DOUBLE_NULL;
    case TSDB_DATA_TYPE_NCHAR:
S
Shengliang Guan 已提交
231
      return varDataLen(val) == sizeof(int32_t) && *(uint32_t *)varDataVal(val) == TSDB_DATA_NCHAR_NULL;
H
Haojun Liao 已提交
232
    case TSDB_DATA_TYPE_BINARY:
S
Shengliang Guan 已提交
233
      return varDataLen(val) == sizeof(int8_t) && *(uint8_t *)varDataVal(val) == TSDB_DATA_BINARY_NULL;
H
Haojun Liao 已提交
234
    case TSDB_DATA_TYPE_UTINYINT:
S
Shengliang Guan 已提交
235
      return *(uint8_t *)val == TSDB_DATA_UTINYINT_NULL;
H
Haojun Liao 已提交
236
    case TSDB_DATA_TYPE_USMALLINT:
S
Shengliang Guan 已提交
237
      return *(uint16_t *)val == TSDB_DATA_USMALLINT_NULL;
H
Haojun Liao 已提交
238
    case TSDB_DATA_TYPE_UINT:
S
Shengliang Guan 已提交
239
      return *(uint32_t *)val == TSDB_DATA_UINT_NULL;
H
Haojun Liao 已提交
240
    case TSDB_DATA_TYPE_UBIGINT:
S
Shengliang Guan 已提交
241
      return *(uint64_t *)val == TSDB_DATA_UBIGINT_NULL;
H
Haojun Liao 已提交
242 243 244 245 246 247 248 249 250 251

    default:
      return false;
  };
}

typedef struct tDataTypeDescriptor {
  int16_t type;
  int16_t nameLen;
  int32_t bytes;
dengyihao's avatar
dengyihao 已提交
252
  char *  name;
H
Haojun Liao 已提交
253 254
  int64_t minValue;
  int64_t maxValue;
S
Shengliang Guan 已提交
255 256 257 258
  int32_t (*compFunc)(const char *const input, int32_t inputSize, const int32_t nelements, char *const output,
                      int32_t outputSize, char algorithm, char *const buffer, int32_t bufferSize);
  int32_t (*decompFunc)(const char *const input, int32_t compressedSize, const int32_t nelements, char *const output,
                        int32_t outputSize, char algorithm, char *const buffer, int32_t bufferSize);
C
Cary Xu 已提交
259 260
  void (*statisFunc)(int8_t bitmapMode, const void *pBitmap, const void *pData, int32_t numofrow, int64_t *min,
                     int64_t *max, int64_t *sum, int16_t *minindex, int16_t *maxindex, int16_t *numofnull);
H
Haojun Liao 已提交
261 262
} tDataTypeDescriptor;

263
extern tDataTypeDescriptor tDataTypes[TSDB_DATA_TYPE_MAX];
H
Haojun Liao 已提交
264 265 266

bool isValidDataType(int32_t type);

S
Shengliang Guan 已提交
267 268 269
void        setVardataNull(void *val, int32_t type);
void        setNull(void *val, int32_t type, int32_t bytes);
void        setNullN(void *val, int32_t type, int32_t bytes, int32_t numOfElems);
H
Haojun Liao 已提交
270 271
const void *getNullValue(int32_t type);

S
Shengliang Guan 已提交
272 273 274 275 276
void  assignVal(char *val, const char *src, int32_t len, int32_t type);
void  tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size, void *buf);
void  operateVal(void *dst, void *s1, void *s2, int32_t optr, int32_t type);
void *getDataMin(int32_t type);
void *getDataMax(int32_t type);
H
Haojun Liao 已提交
277 278

#define SET_DOUBLE_NULL(v) (*(uint64_t *)(v) = TSDB_DATA_DOUBLE_NULL)
D
dapan1121 已提交
279
#define SET_BIGINT_NULL(v) (*(uint64_t *)(v) = TSDB_DATA_BIGINT_NULL)
H
Haojun Liao 已提交
280 281 282 283 284

#ifdef __cplusplus
}
#endif

dengyihao's avatar
dengyihao 已提交
285
#endif /*_TD_COMMON_TTYPE_H_*/