tcommon.h 7.2 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
enum {
  TMQ_CONF__RESET_OFFSET__LATEST = -1,
  TMQ_CONF__RESET_OFFSET__EARLIEAST = -2,
  TMQ_CONF__RESET_OFFSET__NONE = -3,
};
L
Liu Jicong 已提交
33

L
Liu Jicong 已提交
34
enum {
L
Liu Jicong 已提交
35 36
  TMQ_MSG_TYPE__DUMMY = 0,
  TMQ_MSG_TYPE__POLL_RSP,
L
Liu Jicong 已提交
37 38 39
  TMQ_MSG_TYPE__EP_RSP,
};

40 41
typedef struct {
  uint32_t  numOfTables;
L
Liu Jicong 已提交
42 43
  SArray*   pGroupList;
  SHashObj* map;  // speedup acquire the tableQueryInfo by table uid
44 45
} STableGroupInfo;

46 47 48 49 50 51 52 53 54 55 56
typedef struct SColumnDataAgg {
  int16_t colId;
  int64_t sum;
  int64_t max;
  int64_t min;
  int16_t maxIndex;
  int16_t minIndex;
  int16_t numOfNull;
} SColumnDataAgg;

typedef struct SDataBlockInfo {
L
Liu Jicong 已提交
57 58 59 60 61 62 63 64 65 66
  STimeWindow window;
  int32_t     rows;
  int32_t     rowSize;
  int16_t     numOfCols;
  int16_t     hasVarCol;
  union {
    int64_t uid;
    int64_t blockId;
  };
  int64_t groupId;  // no need to serialize
67 68 69
} SDataBlockInfo;

typedef struct SSDataBlock {
L
Liu Jicong 已提交
70 71
  SColumnDataAgg* pBlockAgg;
  SArray*         pDataBlock;  // SArray<SColumnInfoData>
H
Haojun Liao 已提交
72
  SDataBlockInfo  info;
73 74
} SSDataBlock;

H
Haojun Liao 已提交
75
typedef struct SVarColAttr {
L
Liu Jicong 已提交
76
  int32_t* offset;    // start position for each entry in the list
H
Haojun Liao 已提交
77 78 79 80
  uint32_t length;    // used buffer size that contain the valid data
  uint32_t allocLen;  // allocated buffer size
} SVarColAttr;

81 82
// pBlockAgg->numOfNull == info.rows, all data are null
// pBlockAgg->numOfNull == 0, no data are null.
83
typedef struct SColumnInfoData {
L
Liu Jicong 已提交
84 85 86
  SColumnInfo info;     // TODO filter info needs to be removed
  bool        hasNull;  // if current column data has null value.
  char*       pData;    // the corresponding block data in memory
H
Haojun Liao 已提交
87
  union {
L
Liu Jicong 已提交
88
    char*       nullbitmap;  // bitmap, one bit for each item in the list
H
Haojun Liao 已提交
89 90
    SVarColAttr varmeta;
  };
91 92
} SColumnInfoData;

H
Haojun Liao 已提交
93 94 95
void*   blockDataDestroy(SSDataBlock* pBlock);
int32_t tEncodeDataBlock(void** buf, const SSDataBlock* pBlock);
void*   tDecodeDataBlock(const void* buf, SSDataBlock* pBlock);
H
Haojun Liao 已提交
96

L
Liu Jicong 已提交
97
int32_t tEncodeDataBlocks(void** buf, const SArray* blocks);
L
Liu Jicong 已提交
98
void*   tDecodeDataBlocks(const void* buf, SArray** blocks);
L
Liu Jicong 已提交
99

L
Liu Jicong 已提交
100 101 102 103 104 105 106
static FORCE_INLINE void blockDestroyInner(SSDataBlock* pBlock) {
  // WARNING: do not use info.numOfCols,
  // sometimes info.numOfCols != array size
  int32_t numOfOutput = taosArrayGetSize(pBlock->pDataBlock);
  for (int32_t i = 0; i < numOfOutput; ++i) {
    SColumnInfoData* pColInfoData = (SColumnInfoData*)taosArrayGet(pBlock->pDataBlock, i);
    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
wafwerar's avatar
wafwerar 已提交
107
      taosMemoryFreeClear(pColInfoData->varmeta.offset);
L
Liu Jicong 已提交
108
    } else {
wafwerar's avatar
wafwerar 已提交
109
      taosMemoryFreeClear(pColInfoData->nullbitmap);
L
Liu Jicong 已提交
110 111
    }

wafwerar's avatar
wafwerar 已提交
112
    taosMemoryFreeClear(pColInfoData->pData);
L
Liu Jicong 已提交
113
  }
L
Liu Jicong 已提交
114 115

  taosArrayDestroy(pBlock->pDataBlock);
wafwerar's avatar
wafwerar 已提交
116
  taosMemoryFreeClear(pBlock->pBlockAgg);
L
Liu Jicong 已提交
117 118
}

L
Liu Jicong 已提交
119 120
static FORCE_INLINE void tDeleteSSDataBlock(SSDataBlock* pBlock) { blockDestroyInner(pBlock); }

L
Liu Jicong 已提交
121
static FORCE_INLINE int32_t tEncodeSMqPollRsp(void** buf, const SMqPollRsp* pRsp) {
H
Haojun Liao 已提交
122 123
  int32_t tlen = 0;
  int32_t sz = 0;
L
Liu Jicong 已提交
124
  // tlen += taosEncodeFixedI64(buf, pRsp->consumerId);
H
Haojun Liao 已提交
125 126 127 128 129
  tlen += taosEncodeFixedI64(buf, pRsp->reqOffset);
  tlen += taosEncodeFixedI64(buf, pRsp->rspOffset);
  tlen += taosEncodeFixedI32(buf, pRsp->skipLogNum);
  tlen += taosEncodeFixedI32(buf, pRsp->numOfTopics);
  if (pRsp->numOfTopics == 0) return tlen;
L
Liu Jicong 已提交
130
  tlen += tEncodeSSchemaWrapper(buf, pRsp->schema);
H
Haojun Liao 已提交
131 132 133 134 135
  if (pRsp->pBlockData) {
    sz = taosArrayGetSize(pRsp->pBlockData);
  }
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
L
Liu Jicong 已提交
136
    SSDataBlock* pBlock = (SSDataBlock*)taosArrayGet(pRsp->pBlockData, i);
H
Haojun Liao 已提交
137 138 139 140 141
    tlen += tEncodeDataBlock(buf, pBlock);
  }
  return tlen;
}

L
Liu Jicong 已提交
142
static FORCE_INLINE void* tDecodeSMqPollRsp(void* buf, SMqPollRsp* pRsp) {
H
Haojun Liao 已提交
143
  int32_t sz;
L
Liu Jicong 已提交
144
  // buf = taosDecodeFixedI64(buf, &pRsp->consumerId);
H
Haojun Liao 已提交
145 146 147 148 149
  buf = taosDecodeFixedI64(buf, &pRsp->reqOffset);
  buf = taosDecodeFixedI64(buf, &pRsp->rspOffset);
  buf = taosDecodeFixedI32(buf, &pRsp->skipLogNum);
  buf = taosDecodeFixedI32(buf, &pRsp->numOfTopics);
  if (pRsp->numOfTopics == 0) return buf;
wafwerar's avatar
wafwerar 已提交
150
  pRsp->schema = (SSchemaWrapper*)taosMemoryCalloc(1, sizeof(SSchemaWrapper));
L
Liu Jicong 已提交
151 152
  if (pRsp->schema == NULL) return NULL;
  buf = tDecodeSSchemaWrapper(buf, pRsp->schema);
H
Haojun Liao 已提交
153 154 155 156 157 158 159 160 161 162
  buf = taosDecodeFixedI32(buf, &sz);
  pRsp->pBlockData = taosArrayInit(sz, sizeof(SSDataBlock));
  for (int32_t i = 0; i < sz; i++) {
    SSDataBlock block = {0};
    tDecodeDataBlock(buf, &block);
    taosArrayPush(pRsp->pBlockData, &block);
  }
  return buf;
}

L
Liu Jicong 已提交
163
static FORCE_INLINE void tDeleteSMqConsumeRsp(SMqPollRsp* pRsp) {
L
Liu Jicong 已提交
164 165
  if (pRsp->schema) {
    if (pRsp->schema->nCols) {
wafwerar's avatar
wafwerar 已提交
166
      taosMemoryFreeClear(pRsp->schema->pSchema);
H
Haojun Liao 已提交
167
    }
wafwerar's avatar
wafwerar 已提交
168
    taosMemoryFree(pRsp->schema);
H
Haojun Liao 已提交
169
  }
L
Liu Jicong 已提交
170
  taosArrayDestroyEx(pRsp->pBlockData, (void (*)(void*))blockDestroyInner);
L
Liu Jicong 已提交
171
  pRsp->pBlockData = NULL;
H
Haojun Liao 已提交
172 173
}

174 175
//======================================================================================================================
// the following structure shared by parser and executor
176
typedef struct SColumn {
H
Haojun Liao 已提交
177 178 179 180 181
  union {
    uint64_t uid;
    int64_t  dataBlockId;
  };

H
Haojun Liao 已提交
182 183
  int16_t colId;
  int16_t slotId;
H
Haojun Liao 已提交
184

H
Haojun Liao 已提交
185 186
  char    name[TSDB_COL_NAME_LEN];
  int8_t  flag;  // column type: normal column, tag, or user-input column (integer/float/string)
H
Haojun Liao 已提交
187 188 189 190
  int16_t type;
  int32_t bytes;
  uint8_t precision;
  uint8_t scale;
191 192
} SColumn;

193
typedef struct SLimit {
H
Haojun Liao 已提交
194 195
  int64_t limit;
  int64_t offset;
196 197 198
} SLimit;

typedef struct SOrder {
H
Haojun Liao 已提交
199 200
  uint32_t order;
  SColumn  col;
201 202 203
} SOrder;

typedef struct SGroupbyExpr {
L
Liu Jicong 已提交
204
  SArray* columnInfo;  // SArray<SColIndex>, group by columns information
H
Haojun Liao 已提交
205
  bool    groupbyTag;  // group by tag or column
206 207
} SGroupbyExpr;

H
Haojun Liao 已提交
208 209
typedef struct SFunctParam {
  int32_t  type;
L
Liu Jicong 已提交
210
  SColumn* pCol;
H
Haojun Liao 已提交
211 212
  SVariant param;
} SFunctParam;
H
Haojun Liao 已提交
213

H
Haojun Liao 已提交
214
// the structure for sql function in select clause
H
Haojun Liao 已提交
215 216 217 218 219 220 221 222 223 224
typedef struct SResSchame {
  int8_t  type;
  int32_t colId;
  int32_t bytes;
  int32_t precision;
  int32_t scale;
  char    name[TSDB_COL_NAME_LEN];
} SResSchema;

// TODO move away to executor.h
H
Haojun Liao 已提交
225
typedef struct SExprBasicInfo {
H
Haojun Liao 已提交
226
  SResSchema   resSchema;
H
Haojun Liao 已提交
227
  int16_t      numOfParams;  // argument value of each function
L
Liu Jicong 已提交
228
  SFunctParam* pParam;
H
Haojun Liao 已提交
229
} SExprBasicInfo;
230 231

typedef struct SExprInfo {
L
Liu Jicong 已提交
232 233
  struct SExprBasicInfo base;
  struct tExprNode*     pExpr;
234 235
} SExprInfo;

236 237 238 239 240
typedef struct SStateWindow {
  SColumn col;
} SStateWindow;

typedef struct SSessionWindow {
H
Haojun Liao 已提交
241
  int64_t gap;  // gap between two session window(in microseconds)
242 243 244
  SColumn col;
} SSessionWindow;

L
Liu Jicong 已提交
245
#define QUERY_ASC_FORWARD_STEP  1
246 247 248 249
#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
Haojun Liao 已提交
250 251 252 253
#ifdef __cplusplus
}
#endif

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