tcommon.h 8.6 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
// TODO remove it
L
Liu Jicong 已提交
29 30
enum {
  TMQ_CONF__RESET_OFFSET__NONE = -3,
L
Liu Jicong 已提交
31 32
  TMQ_CONF__RESET_OFFSET__EARLIEAST = -2,
  TMQ_CONF__RESET_OFFSET__LATEST = -1,
L
Liu Jicong 已提交
33
};
L
Liu Jicong 已提交
34

L
Liu Jicong 已提交
35 36 37 38 39 40 41 42
// 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 已提交
43
  || x == TDMT_VND_DELETE         \
L
Liu Jicong 已提交
44 45 46
)
// clang-format on

47 48
typedef struct {
  uint64_t groupId;
49
  TSKEY    ts;
50 51
} SWinKey;

52
static inline int sWinKeyCmprImpl(const void* pKey1, const void* pKey2) {
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  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;
}

71 72 73 74
static inline int winKeyCmpr(const void* pKey1, int kLen1, const void* pKey2, int kLen2) {
  return sWinKeyCmprImpl(pKey1, pKey2);
}

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
typedef struct {
  uint64_t groupId;
  TSKEY    ts;
  int32_t  exprIdx;
} STupleKey;

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 已提交
106
enum {
L
Liu Jicong 已提交
107 108
  TMQ_MSG_TYPE__DUMMY = 0,
  TMQ_MSG_TYPE__POLL_RSP,
L
Liu Jicong 已提交
109
  TMQ_MSG_TYPE__POLL_META_RSP,
L
Liu Jicong 已提交
110
  TMQ_MSG_TYPE__EP_RSP,
L
Liu Jicong 已提交
111
  TMQ_MSG_TYPE__TAOSX_RSP,
L
Liu Jicong 已提交
112
  TMQ_MSG_TYPE__END_RSP,
L
Liu Jicong 已提交
113 114
};

L
Liu Jicong 已提交
115 116 117
enum {
  STREAM_INPUT__DATA_SUBMIT = 1,
  STREAM_INPUT__DATA_BLOCK,
118
  STREAM_INPUT__MERGED_SUBMIT,
L
Liu Jicong 已提交
119
  STREAM_INPUT__TQ_SCAN,
L
Liu Jicong 已提交
120
  STREAM_INPUT__DATA_RETRIEVE,
L
Liu Jicong 已提交
121
  STREAM_INPUT__GET_RES,
L
Liu Jicong 已提交
122
  STREAM_INPUT__CHECKPOINT,
L
Liu Jicong 已提交
123
  STREAM_INPUT__REF_DATA_BLOCK,
L
Liu Jicong 已提交
124
  STREAM_INPUT__DESTROY,
L
Liu Jicong 已提交
125 126
};

5
54liuyao 已提交
127 128 129
typedef enum EStreamType {
  STREAM_NORMAL = 1,
  STREAM_INVERT,
5
54liuyao 已提交
130
  STREAM_CLEAR,
5
54liuyao 已提交
131
  STREAM_INVALID,
5
54liuyao 已提交
132
  STREAM_GET_ALL,
133 134
  STREAM_DELETE_RESULT,
  STREAM_DELETE_DATA,
L
Liu Jicong 已提交
135
  STREAM_RETRIEVE,
L
Liu Jicong 已提交
136 137
  STREAM_PULL_DATA,
  STREAM_PULL_OVER,
5
54liuyao 已提交
138 139
} EStreamType;

140
typedef struct {
wmmhello's avatar
wmmhello 已提交
141
  SArray*   pGroupList;
wmmhello's avatar
wmmhello 已提交
142
  SArray*   pTableList;
L
Liu Jicong 已提交
143
  SHashObj* map;  // speedup acquire the tableQueryInfo by table uid
144
  bool      needSortTableByGroupId;
D
dapan1121 已提交
145
  uint64_t  suid;
wmmhello's avatar
wmmhello 已提交
146
} STableListInfo;
147

H
Hongze Cheng 已提交
148
#pragma pack(push, 1)
149 150 151
typedef struct SColumnDataAgg {
  int16_t colId;
  int16_t numOfNull;
152 153 154
  int64_t sum;
  int64_t max;
  int64_t min;
155
} SColumnDataAgg;
H
Hongze Cheng 已提交
156
#pragma pack(pop)
157 158

typedef struct SDataBlockInfo {
L
fix  
Liu Jicong 已提交
159
  STimeWindow window;
160
  int32_t     rows;  // todo hide this attribute
L
fix  
Liu Jicong 已提交
161
  int32_t     rowSize;
162
  uint64_t    uid;      // the uid of table, from which current data block comes
163
  uint16_t    blockId;  // block id, generated by physical planner
164 165
  uint64_t    groupId;  // no need to serialize
  int16_t     hasVarCol;
166
  uint32_t    capacity;
L
Liu Jicong 已提交
167
  // TODO: optimize and remove following
168 169 170 171 172
  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
173 174 175
} SDataBlockInfo;

typedef struct SSDataBlock {
176 177 178
  SColumnDataAgg** pBlockAgg;
  SArray*          pDataBlock;  // SArray<SColumnInfoData>
  SDataBlockInfo   info;
179 180
} SSDataBlock;

L
Liu Jicong 已提交
181 182 183
enum {
  FETCH_TYPE__DATA = 1,
  FETCH_TYPE__META,
L
Liu Jicong 已提交
184
  FETCH_TYPE__SEP,
L
Liu Jicong 已提交
185 186 187 188
  FETCH_TYPE__NONE,
};

typedef struct {
L
Liu Jicong 已提交
189 190
  int8_t       fetchType;
  STqOffsetVal offset;
L
Liu Jicong 已提交
191 192 193 194 195 196
  union {
    SSDataBlock data;
    void*       meta;
  };
} SFetchRet;

H
Haojun Liao 已提交
197
typedef struct SVarColAttr {
L
Liu Jicong 已提交
198
  int32_t* offset;    // start position for each entry in the list
H
Haojun Liao 已提交
199 200 201 202
  uint32_t length;    // used buffer size that contain the valid data
  uint32_t allocLen;  // allocated buffer size
} SVarColAttr;

203 204
// pBlockAgg->numOfNull == info.rows, all data are null
// pBlockAgg->numOfNull == 0, no data are null.
205
typedef struct SColumnInfoData {
206
  SColumnInfo info;     // column info
L
Liu Jicong 已提交
207 208
  bool        hasNull;  // if current column data has null value.
  char*       pData;    // the corresponding block data in memory
H
Haojun Liao 已提交
209
  union {
L
Liu Jicong 已提交
210
    char*       nullbitmap;  // bitmap, one bit for each item in the list
H
Haojun Liao 已提交
211 212
    SVarColAttr varmeta;
  };
213 214
} SColumnInfoData;

215
typedef struct SQueryTableDataCond {
H
more  
Hongze Cheng 已提交
216
  uint64_t     suid;
217 218
  int32_t      order;  // desc|asc order to iterate the data block
  int32_t      numOfCols;
H
more  
Hongze Cheng 已提交
219
  SColumnInfo* colList;
L
Liu Jicong 已提交
220
  int32_t      type;  // data block load type:
H
Haojun Liao 已提交
221 222 223
  STimeWindow  twindows;
  int64_t      startVersion;
  int64_t      endVersion;
224 225
} SQueryTableDataCond;

H
Haojun Liao 已提交
226 227
int32_t tEncodeDataBlock(void** buf, const SSDataBlock* pBlock);
void*   tDecodeDataBlock(const void* buf, SSDataBlock* pBlock);
H
Haojun Liao 已提交
228

L
Liu Jicong 已提交
229
int32_t tEncodeDataBlocks(void** buf, const SArray* blocks);
L
Liu Jicong 已提交
230
void*   tDecodeDataBlocks(const void* buf, SArray** blocks);
L
fix  
Liu Jicong 已提交
231
void    colDataDestroy(SColumnInfoData* pColData);
L
Liu Jicong 已提交
232

233 234
//======================================================================================================================
// the following structure shared by parser and executor
235
typedef struct SColumn {
H
Haojun Liao 已提交
236 237 238 239 240
  union {
    uint64_t uid;
    int64_t  dataBlockId;
  };

H
Haojun Liao 已提交
241 242
  int16_t colId;
  int16_t slotId;
H
Haojun Liao 已提交
243

H
Haojun Liao 已提交
244
  char    name[TSDB_COL_NAME_LEN];
245
  int16_t colType;  // column type: normal column, tag, or window column
H
Haojun Liao 已提交
246 247 248 249
  int16_t type;
  int32_t bytes;
  uint8_t precision;
  uint8_t scale;
250 251
} SColumn;

H
Haojun Liao 已提交
252
typedef struct STableBlockDistInfo {
253
  uint32_t rowSize;
L
Liu Jicong 已提交
254 255
  uint16_t numOfFiles;
  uint32_t numOfTables;
256
  uint32_t numOfBlocks;
L
Liu Jicong 已提交
257 258 259 260
  uint64_t totalSize;
  uint64_t totalRows;
  int32_t  maxRows;
  int32_t  minRows;
261 262
  int32_t  defMinRows;
  int32_t  defMaxRows;
L
Liu Jicong 已提交
263
  int32_t  firstSeekTimeUs;
264
  uint32_t numOfInmemRows;
L
Liu Jicong 已提交
265
  uint32_t numOfSmallBlocks;
266
  int32_t  blockRowsHisto[20];
H
Haojun Liao 已提交
267
} STableBlockDistInfo;
268

269 270 271
int32_t tSerializeBlockDistInfo(void* buf, int32_t bufLen, const STableBlockDistInfo* pInfo);
int32_t tDeserializeBlockDistInfo(void* buf, int32_t bufLen, STableBlockDistInfo* pInfo);

G
Ganlin Zhao 已提交
272
enum {
H
Haojun Liao 已提交
273
  FUNC_PARAM_TYPE_VALUE = 0x1,
L
Liu Jicong 已提交
274
  FUNC_PARAM_TYPE_COLUMN = 0x2,
G
Ganlin Zhao 已提交
275 276
};

H
Haojun Liao 已提交
277 278
typedef struct SFunctParam {
  int32_t  type;
L
Liu Jicong 已提交
279
  SColumn* pCol;
H
Haojun Liao 已提交
280 281
  SVariant param;
} SFunctParam;
H
Haojun Liao 已提交
282

H
Haojun Liao 已提交
283
// the structure for sql function in select clause
H
Haojun Liao 已提交
284 285
typedef struct SResSchame {
  int8_t  type;
286
  int32_t slotId;
H
Haojun Liao 已提交
287 288 289 290 291 292
  int32_t bytes;
  int32_t precision;
  int32_t scale;
  char    name[TSDB_COL_NAME_LEN];
} SResSchema;

H
Haojun Liao 已提交
293
typedef struct SExprBasicInfo {
H
Haojun Liao 已提交
294
  SResSchema   resSchema;
H
Haojun Liao 已提交
295
  int16_t      numOfParams;  // argument value of each function
L
Liu Jicong 已提交
296
  SFunctParam* pParam;
H
Haojun Liao 已提交
297
} SExprBasicInfo;
298 299

typedef struct SExprInfo {
L
Liu Jicong 已提交
300 301
  struct SExprBasicInfo base;
  struct tExprNode*     pExpr;
302 303
} SExprInfo;

wmmhello's avatar
wmmhello 已提交
304
typedef struct {
H
more  
Hongze Cheng 已提交
305 306 307 308
  const char* key;
  int32_t     keyLen;
  uint8_t     type;
  union {
wmmhello's avatar
wmmhello 已提交
309
    const char* value;
wmmhello's avatar
wmmhello 已提交
310 311 312 313
    int64_t     i;
    uint64_t    u;
    double      d;
    float       f;
wmmhello's avatar
wmmhello 已提交
314
  };
H
more  
Hongze Cheng 已提交
315
  int32_t length;
wmmhello's avatar
wmmhello 已提交
316 317
} SSmlKv;

L
Liu Jicong 已提交
318
#define QUERY_ASC_FORWARD_STEP  1
319 320 321 322
#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 已提交
323
#define SORT_QSORT_T              0x1
324 325 326 327 328 329 330 331 332
#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;

333 334 335 336 337 338 339 340 341
// 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

H
Haojun Liao 已提交
342 343 344 345
#ifdef __cplusplus
}
#endif

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