tcommon.h 9.7 KB
Newer Older
H
Haojun Liao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
Shengliang Guan 已提交
16 17
#ifndef _TD_COMMON_DEF_H_
#define _TD_COMMON_DEF_H_
H
Haojun Liao 已提交
18

H
Haojun Liao 已提交
19
#include "taosdef.h"
H
Haojun Liao 已提交
20
#include "tarray.h"
L
Liu Jicong 已提交
21
#include "tmsg.h"
22
#include "tvariant.h"
S
Shengliang Guan 已提交
23 24 25 26 27

#ifdef __cplusplus
extern "C" {
#endif

L
Liu Jicong 已提交
28 29 30 31 32 33 34 35
// clang-format off
#define IS_META_MSG(x) ( \
     x == TDMT_VND_CREATE_STB     \
  || x == TDMT_VND_ALTER_STB      \
  || x == TDMT_VND_DROP_STB       \
  || x == TDMT_VND_CREATE_TABLE   \
  || x == TDMT_VND_ALTER_TABLE    \
  || x == TDMT_VND_DROP_TABLE     \
wmmhello's avatar
wmmhello 已提交
36
  || x == TDMT_VND_DELETE         \
L
Liu Jicong 已提交
37 38 39
)
// clang-format on

5
54liuyao 已提交
40
typedef struct SWinKey {
41
  uint64_t groupId;
42
  TSKEY    ts;
43 44
} SWinKey;

5
54liuyao 已提交
45 46 47 48 49 50
typedef struct SSessionKey {
  STimeWindow win;
  uint64_t    groupId;
} SSessionKey;

static inline int winKeyCmprImpl(const void* pKey1, const void* pKey2) {
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  SWinKey* pWin1 = (SWinKey*)pKey1;
  SWinKey* pWin2 = (SWinKey*)pKey2;

  if (pWin1->groupId > pWin2->groupId) {
    return 1;
  } else if (pWin1->groupId < pWin2->groupId) {
    return -1;
  }

  if (pWin1->ts > pWin2->ts) {
    return 1;
  } else if (pWin1->ts < pWin2->ts) {
    return -1;
  }

  return 0;
}

69
static inline int winKeyCmpr(const void* pKey1, int kLen1, const void* pKey2, int kLen2) {
5
54liuyao 已提交
70
  return winKeyCmprImpl(pKey1, pKey2);
71 72
}

73 74 75 76 77 78
typedef struct {
  uint64_t groupId;
  TSKEY    ts;
  int32_t  exprIdx;
} STupleKey;

H
Haojun Liao 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
typedef struct STuplePos {
  union {
    struct {
      int32_t pageId;
      int32_t offset;
    };
    STupleKey streamTupleKey;
  };
} STuplePos;

typedef struct SFirstLastRes {
  bool hasResult;
  // used for last_row function only, isNullRes in SResultRowEntry can not be passed to downstream.So,
  // this attribute is required
  bool      isNull;
  int32_t   bytes;
  int64_t   ts;
  STuplePos pos;
  char      buf[];
} SFirstLastRes;

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
static inline int STupleKeyCmpr(const void* pKey1, int kLen1, const void* pKey2, int kLen2) {
  STupleKey* pTuple1 = (STupleKey*)pKey1;
  STupleKey* pTuple2 = (STupleKey*)pKey2;

  if (pTuple1->groupId > pTuple2->groupId) {
    return 1;
  } else if (pTuple1->groupId < pTuple2->groupId) {
    return -1;
  }

  if (pTuple1->ts > pTuple2->ts) {
    return 1;
  } else if (pTuple1->ts < pTuple2->ts) {
    return -1;
  }

  if (pTuple1->exprIdx > pTuple2->exprIdx) {
    return 1;
  } else if (pTuple1->exprIdx < pTuple2->exprIdx) {
    return -1;
  }

  return 0;
}

L
Liu Jicong 已提交
125
enum {
L
Liu Jicong 已提交
126 127
  TMQ_MSG_TYPE__DUMMY = 0,
  TMQ_MSG_TYPE__POLL_RSP,
L
Liu Jicong 已提交
128
  TMQ_MSG_TYPE__POLL_META_RSP,
L
Liu Jicong 已提交
129
  TMQ_MSG_TYPE__EP_RSP,
L
Liu Jicong 已提交
130
  TMQ_MSG_TYPE__TAOSX_RSP,
L
Liu Jicong 已提交
131
  TMQ_MSG_TYPE__END_RSP,
L
Liu Jicong 已提交
132 133
};

L
Liu Jicong 已提交
134 135 136
enum {
  STREAM_INPUT__DATA_SUBMIT = 1,
  STREAM_INPUT__DATA_BLOCK,
137
  STREAM_INPUT__MERGED_SUBMIT,
L
Liu Jicong 已提交
138
  STREAM_INPUT__TQ_SCAN,
L
Liu Jicong 已提交
139
  STREAM_INPUT__DATA_RETRIEVE,
L
Liu Jicong 已提交
140
  STREAM_INPUT__GET_RES,
L
Liu Jicong 已提交
141
  STREAM_INPUT__CHECKPOINT,
L
Liu Jicong 已提交
142
  STREAM_INPUT__REF_DATA_BLOCK,
L
Liu Jicong 已提交
143
  STREAM_INPUT__DESTROY,
L
Liu Jicong 已提交
144 145
};

5
54liuyao 已提交
146 147 148
typedef enum EStreamType {
  STREAM_NORMAL = 1,
  STREAM_INVERT,
5
54liuyao 已提交
149
  STREAM_CLEAR,
5
54liuyao 已提交
150
  STREAM_INVALID,
5
54liuyao 已提交
151
  STREAM_GET_ALL,
152 153
  STREAM_DELETE_RESULT,
  STREAM_DELETE_DATA,
L
Liu Jicong 已提交
154
  STREAM_RETRIEVE,
L
Liu Jicong 已提交
155 156
  STREAM_PULL_DATA,
  STREAM_PULL_OVER,
157
  STREAM_FILL_OVER,
5
54liuyao 已提交
158
  STREAM_CREATE_CHILD_TABLE,
5
54liuyao 已提交
159 160
} EStreamType;

H
Hongze Cheng 已提交
161
#pragma pack(push, 1)
162 163 164
typedef struct SColumnDataAgg {
  int16_t colId;
  int16_t numOfNull;
165 166 167
  int64_t sum;
  int64_t max;
  int64_t min;
168
} SColumnDataAgg;
H
Hongze Cheng 已提交
169
#pragma pack(pop)
170

H
Haojun Liao 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184
typedef struct SBlockID {
  // The uid of table, from which current data block comes. And it is always 0, if current block is the
  // result of calculation.
  uint64_t uid;

  // Block id, acquired and assigned from executor, which created according to the hysical planner. Block id is used
  // to mark the stage of exec task.
  uint64_t blockId;

  // Generated by group/partition by [value|tags]. Created and assigned by table-scan operator, group-by operator,
  // and partition by operator.
  uint64_t groupId;
} SBlockID;

185
typedef struct SDataBlockInfo {
L
fix  
Liu Jicong 已提交
186 187
  STimeWindow window;
  int32_t     rowSize;
D
dapan1121 已提交
188
  int64_t     rows;  // todo hide this attribute
189
  uint32_t    capacity;
H
Haojun Liao 已提交
190 191
  SBlockID    id;
  int16_t     hasVarCol;
L
Liu Jicong 已提交
192
  int16_t     dataLoad;  // denote if the data is loaded or not
H
Haojun Liao 已提交
193

L
Liu Jicong 已提交
194
  // TODO: optimize and remove following
195 196 197 198 199
  int64_t     version;    // used for stream, and need serialization
  int32_t     childId;    // used for stream, do not serialize
  EStreamType type;       // used for stream, do not serialize
  STimeWindow calWin;     // used for stream, do not serialize
  TSKEY       watermark;  // used for stream
200

dengyihao's avatar
dengyihao 已提交
201
  char parTbName[TSDB_TABLE_NAME_LEN];  // used for stream partition
202 203 204
} SDataBlockInfo;

typedef struct SSDataBlock {
205 206 207
  SColumnDataAgg** pBlockAgg;
  SArray*          pDataBlock;  // SArray<SColumnInfoData>
  SDataBlockInfo   info;
208 209
} SSDataBlock;

L
Liu Jicong 已提交
210
enum {
wmmhello's avatar
wmmhello 已提交
211
  FETCH_TYPE__DATA = 0,
L
Liu Jicong 已提交
212 213 214 215
  FETCH_TYPE__NONE,
};

typedef struct {
L
Liu Jicong 已提交
216
  int8_t       fetchType;
L
Liu Jicong 已提交
217 218 219 220 221 222
  union {
    SSDataBlock data;
    void*       meta;
  };
} SFetchRet;

H
Haojun Liao 已提交
223
typedef struct SVarColAttr {
L
Liu Jicong 已提交
224
  int32_t* offset;    // start position for each entry in the list
H
Haojun Liao 已提交
225 226 227 228
  uint32_t length;    // used buffer size that contain the valid data
  uint32_t allocLen;  // allocated buffer size
} SVarColAttr;

229 230
// pBlockAgg->numOfNull == info.rows, all data are null
// pBlockAgg->numOfNull == 0, no data are null.
231
typedef struct SColumnInfoData {
L
Liu Jicong 已提交
232
  char* pData;  // the corresponding block data in memory
H
Haojun Liao 已提交
233
  union {
L
Liu Jicong 已提交
234
    char*       nullbitmap;  // bitmap, one bit for each item in the list
H
Haojun Liao 已提交
235 236
    SVarColAttr varmeta;
  };
L
Liu Jicong 已提交
237 238
  SColumnInfo info;     // column info
  bool        hasNull;  // if current column data has null value.
239 240
} SColumnInfoData;

241
typedef struct SQueryTableDataCond {
H
more  
Hongze Cheng 已提交
242
  uint64_t     suid;
L
Liu Jicong 已提交
243
  int32_t      order;  // desc|asc order to iterate the data block
244
  int32_t      numOfCols;
H
more  
Hongze Cheng 已提交
245
  SColumnInfo* colList;
L
Liu Jicong 已提交
246 247
  int32_t*     pSlotList;  // the column output destation slot, and it may be null
  int32_t      type;       // data block load type:
H
Haojun Liao 已提交
248 249 250
  STimeWindow  twindows;
  int64_t      startVersion;
  int64_t      endVersion;
251 252
} SQueryTableDataCond;

H
Haojun Liao 已提交
253 254
int32_t tEncodeDataBlock(void** buf, const SSDataBlock* pBlock);
void*   tDecodeDataBlock(const void* buf, SSDataBlock* pBlock);
H
Haojun Liao 已提交
255

L
Liu Jicong 已提交
256
int32_t tEncodeDataBlocks(void** buf, const SArray* blocks);
L
Liu Jicong 已提交
257
void*   tDecodeDataBlocks(const void* buf, SArray** blocks);
L
fix  
Liu Jicong 已提交
258
void    colDataDestroy(SColumnInfoData* pColData);
L
Liu Jicong 已提交
259

260 261
//======================================================================================================================
// the following structure shared by parser and executor
262
typedef struct SColumn {
H
Haojun Liao 已提交
263 264 265 266 267
  union {
    uint64_t uid;
    int64_t  dataBlockId;
  };

H
Haojun Liao 已提交
268 269
  int16_t colId;
  int16_t slotId;
H
Haojun Liao 已提交
270

H
Haojun Liao 已提交
271
  char    name[TSDB_COL_NAME_LEN];
272
  int16_t colType;  // column type: normal column, tag, or window column
H
Haojun Liao 已提交
273 274 275 276
  int16_t type;
  int32_t bytes;
  uint8_t precision;
  uint8_t scale;
277 278
} SColumn;

H
Haojun Liao 已提交
279
typedef struct STableBlockDistInfo {
280
  uint32_t rowSize;
L
Liu Jicong 已提交
281 282
  uint16_t numOfFiles;
  uint32_t numOfTables;
283
  uint32_t numOfBlocks;
L
Liu Jicong 已提交
284 285 286 287
  uint64_t totalSize;
  uint64_t totalRows;
  int32_t  maxRows;
  int32_t  minRows;
288 289
  int32_t  defMinRows;
  int32_t  defMaxRows;
L
Liu Jicong 已提交
290
  int32_t  firstSeekTimeUs;
291
  uint32_t numOfInmemRows;
L
Liu Jicong 已提交
292
  uint32_t numOfSmallBlocks;
dengyihao's avatar
dengyihao 已提交
293
  uint32_t numOfVgroups;
294
  int32_t  blockRowsHisto[20];
H
Haojun Liao 已提交
295
} STableBlockDistInfo;
296

297 298 299
int32_t tSerializeBlockDistInfo(void* buf, int32_t bufLen, const STableBlockDistInfo* pInfo);
int32_t tDeserializeBlockDistInfo(void* buf, int32_t bufLen, STableBlockDistInfo* pInfo);

G
Ganlin Zhao 已提交
300
enum {
H
Haojun Liao 已提交
301
  FUNC_PARAM_TYPE_VALUE = 0x1,
L
Liu Jicong 已提交
302
  FUNC_PARAM_TYPE_COLUMN = 0x2,
G
Ganlin Zhao 已提交
303 304
};

H
Haojun Liao 已提交
305 306
typedef struct SFunctParam {
  int32_t  type;
L
Liu Jicong 已提交
307
  SColumn* pCol;
H
Haojun Liao 已提交
308 309
  SVariant param;
} SFunctParam;
H
Haojun Liao 已提交
310

H
Haojun Liao 已提交
311
// the structure for sql function in select clause
H
Haojun Liao 已提交
312 313
typedef struct SResSchame {
  int8_t  type;
314
  int32_t slotId;
H
Haojun Liao 已提交
315 316 317 318 319 320
  int32_t bytes;
  int32_t precision;
  int32_t scale;
  char    name[TSDB_COL_NAME_LEN];
} SResSchema;

H
Haojun Liao 已提交
321
typedef struct SExprBasicInfo {
H
Haojun Liao 已提交
322
  SResSchema   resSchema;
H
Haojun Liao 已提交
323
  int16_t      numOfParams;  // argument value of each function
L
Liu Jicong 已提交
324
  SFunctParam* pParam;
H
Haojun Liao 已提交
325
} SExprBasicInfo;
326 327

typedef struct SExprInfo {
L
Liu Jicong 已提交
328 329
  struct SExprBasicInfo base;
  struct tExprNode*     pExpr;
330 331
} SExprInfo;

wmmhello's avatar
wmmhello 已提交
332
typedef struct {
H
more  
Hongze Cheng 已提交
333
  const char* key;
dengyihao's avatar
dengyihao 已提交
334
  size_t      keyLen;
H
more  
Hongze Cheng 已提交
335 336
  uint8_t     type;
  union {
wmmhello's avatar
wmmhello 已提交
337
    const char* value;
wmmhello's avatar
wmmhello 已提交
338 339 340 341
    int64_t     i;
    uint64_t    u;
    double      d;
    float       f;
wmmhello's avatar
wmmhello 已提交
342
  };
343
  size_t length;
wmmhello's avatar
wmmhello 已提交
344 345
  bool keyEscaped;
  bool valueEscaped;
wmmhello's avatar
wmmhello 已提交
346 347
} SSmlKv;

L
Liu Jicong 已提交
348
#define QUERY_ASC_FORWARD_STEP  1
349 350 351 352
#define QUERY_DESC_FORWARD_STEP -1

#define GET_FORWARD_DIRECTION_FACTOR(ord) (((ord) == TSDB_ORDER_ASC) ? QUERY_ASC_FORWARD_STEP : QUERY_DESC_FORWARD_STEP)

H
more  
Hongze Cheng 已提交
353
#define SORT_QSORT_T              0x1
354 355 356 357 358 359 360 361 362
#define SORT_SPILLED_MERGE_SORT_T 0x2
typedef struct SSortExecInfo {
  int32_t sortMethod;
  int32_t sortBuffer;
  int32_t loops;       // loop count
  int32_t writeBytes;  // write io bytes
  int32_t readBytes;   // read io bytes
} SSortExecInfo;

363
typedef struct STUidTagInfo {
H
Haojun Liao 已提交
364 365 366
  char*    name;
  uint64_t uid;
  void*    pTagVal;
367
} STUidTagInfo;
H
Haojun Liao 已提交
368

369 370 371 372 373 374 375 376
// stream special block column

#define START_TS_COLUMN_INDEX           0
#define END_TS_COLUMN_INDEX             1
#define UID_COLUMN_INDEX                2
#define GROUPID_COLUMN_INDEX            3
#define CALCULATE_START_TS_COLUMN_INDEX 4
#define CALCULATE_END_TS_COLUMN_INDEX   5
377
#define TABLE_NAME_COLUMN_INDEX         6
378

5
54liuyao 已提交
379
// stream create table block column
dengyihao's avatar
dengyihao 已提交
380 381 382
#define UD_TABLE_NAME_COLUMN_INDEX 0
#define UD_GROUPID_COLUMN_INDEX    1
#define UD_TAG_COLUMN_INDEX        2
5
54liuyao 已提交
383

384 385
int32_t taosGenCrashJsonMsg(int signum, char **pMsg, int64_t clusterId, int64_t startTime);

H
Haojun Liao 已提交
386 387 388 389
#ifdef __cplusplus
}
#endif

L
Liu Jicong 已提交
390
#endif /*_TD_COMMON_DEF_H_*/