query.h 13.6 KB
Newer Older
D
dapan1121 已提交
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/>.
 */

D
dapan1121 已提交
16 17
#ifndef _TD_QUERY_H_
#define _TD_QUERY_H_
D
dapan1121 已提交
18

dengyihao's avatar
dengyihao 已提交
19
// clang-foramt off
D
dapan1121 已提交
20 21 22 23
#ifdef __cplusplus
extern "C" {
#endif

D
dapan1121 已提交
24
#include "tarray.h"
25
#include "thash.h"
26
#include "tlog.h"
H
Haojun Liao 已提交
27
#include "tmsg.h"
28
#include "tmsgcb.h"
D
dapan1121 已提交
29
#include "systable.h"
D
dapan1121 已提交
30

S
Shengliang Guan 已提交
31
typedef enum {
D
dapan1121 已提交
32
  JOB_TASK_STATUS_NULL = 0,
D
dapan1121 已提交
33 34 35
  JOB_TASK_STATUS_INIT,
  JOB_TASK_STATUS_EXEC,
  JOB_TASK_STATUS_PART_SUCC,
D
dapan1121 已提交
36
  JOB_TASK_STATUS_FETCH,
D
dapan1121 已提交
37 38 39 40
  JOB_TASK_STATUS_SUCC,
  JOB_TASK_STATUS_FAIL,
  JOB_TASK_STATUS_DROP,
  JOB_TASK_STATUS_MAX,
S
Shengliang Guan 已提交
41
} EJobTaskType;
D
dapan1121 已提交
42

S
Shengliang Guan 已提交
43
typedef enum {
D
dapan1121 已提交
44 45
  TASK_TYPE_PERSISTENT = 1,
  TASK_TYPE_TEMP,
S
Shengliang Guan 已提交
46
} ETaskType;
D
dapan1121 已提交
47

D
dapan1121 已提交
48 49 50 51 52 53
typedef enum {
  TARGET_TYPE_MNODE = 1,
  TARGET_TYPE_VNODE,
  TARGET_TYPE_OTHER,
} ETargetType;

D
dapan1121 已提交
54 55 56
#define QUERY_POLICY_VNODE  1
#define QUERY_POLICY_HYBRID 2
#define QUERY_POLICY_QNODE  3
D
dapan1121 已提交
57
#define QUERY_POLICY_CLIENT 4
D
dapan1121 已提交
58

D
dapan1121 已提交
59 60 61
#define QUERY_RSP_POLICY_DELAY 0
#define QUERY_RSP_POLICY_QUICK 1

62
#define QUERY_MSG_MASK_SHOW_REWRITE() (1 << 0)
dengyihao's avatar
dengyihao 已提交
63
#define TEST_SHOW_REWRITE_MASK(m)     (((m)&QUERY_MSG_MASK_SHOW_REWRITE()) != 0)
64

D
dapan1121 已提交
65
typedef struct STableComInfo {
66 67 68 69
  uint8_t  numOfTags;     // the number of tags in schema
  uint8_t  precision;     // the number of precision
  col_id_t numOfColumns;  // the number of columns
  int32_t  rowSize;       // row size of the schema
D
dapan1121 已提交
70 71
} STableComInfo;

D
dapan1121 已提交
72
typedef struct SIndexMeta {
wafwerar's avatar
wafwerar 已提交
73
#if defined(WINDOWS) || defined(_TD_DARWIN_64)
wafwerar's avatar
wafwerar 已提交
74 75 76
  size_t avoidCompilationErrors;
#endif

D
dapan1121 已提交
77 78
} SIndexMeta;

D
dapan1121 已提交
79
typedef struct SExecResult {
H
Hongze Cheng 已提交
80 81 82 83 84
  int32_t  code;
  uint64_t numOfRows;
  uint64_t numOfBytes;
  int32_t  msgType;
  void*    res;
D
dapan1121 已提交
85 86
} SExecResult;

87
typedef struct STbVerInfo {
dengyihao's avatar
dengyihao 已提交
88
  char    tbFName[TSDB_TABLE_FNAME_LEN];
89 90 91 92
  int32_t sversion;
  int32_t tversion;
} STbVerInfo;

D
dapan1121 已提交
93
#pragma pack(push, 1) 
D
dapan1121 已提交
94 95 96
typedef struct SCTableMeta {
  uint64_t uid;
  uint64_t suid;
D
dapan1121 已提交
97 98
  int32_t  vgId;
  int8_t   tableType;
D
dapan1121 已提交
99
} SCTableMeta;
D
dapan1121 已提交
100
#pragma pack(pop)
D
dapan1121 已提交
101

D
dapan1121 已提交
102 103

#pragma pack(push, 1) 
D
dapan1121 已提交
104
typedef struct STableMeta {
L
Liu Jicong 已提交
105 106 107
  // BEGIN: KEEP THIS PART SAME WITH SCTableMeta
  uint64_t uid;
  uint64_t suid;
D
dapan1121 已提交
108 109
  int32_t  vgId;
  int8_t   tableType;
L
Liu Jicong 已提交
110 111 112 113
  // END: KEEP THIS PART SAME WITH SCTableMeta

  // if the table is TSDB_CHILD_TABLE, the following information is acquired from the corresponding super table meta
  // info
114 115
  int32_t       sversion;
  int32_t       tversion;
L
Liu Jicong 已提交
116 117
  STableComInfo tableInfo;
  SSchema       schema[];
D
dapan1121 已提交
118
} STableMeta;
D
dapan1121 已提交
119
#pragma pack(pop)
D
dapan1121 已提交
120

D
dapan1121 已提交
121
typedef struct SDBVgInfo {
L
Liu Jicong 已提交
122
  int32_t   vgVersion;
123 124
  int16_t   hashPrefix;
  int16_t   hashSuffix;
125
  int8_t    hashMethod;
D
dapan 已提交
126
  int32_t   numOfTable;  // DB's table num, unit is TSDB_TABLE_NUM_UNIT
D
dapan1121 已提交
127
  int64_t   stateTs;
dengyihao's avatar
dengyihao 已提交
128
  SHashObj* vgHash;  // key:vgId, value:SVgroupInfo
D
dapan1121 已提交
129
  SArray*   vgArray; // SVgroupInfo
D
dapan1121 已提交
130
} SDBVgInfo;
D
dapan1121 已提交
131 132

typedef struct SUseDbOutput {
L
Liu Jicong 已提交
133 134 135
  char       db[TSDB_DB_FNAME_LEN];
  uint64_t   dbId;
  SDBVgInfo* dbVgroup;
D
dapan1121 已提交
136 137
} SUseDbOutput;

L
Liu Jicong 已提交
138
enum { META_TYPE_NULL_TABLE = 1, META_TYPE_CTABLE, META_TYPE_TABLE, META_TYPE_BOTH_TABLE };
D
dapan1121 已提交
139

D
dapan1121 已提交
140
typedef struct STableMetaOutput {
D
dapan1121 已提交
141
  int32_t     metaType;
D
dapan1121 已提交
142
  uint64_t    dbId;
D
dapan1121 已提交
143 144 145
  char        dbFName[TSDB_DB_FNAME_LEN];
  char        ctbName[TSDB_TABLE_NAME_LEN];
  char        tbName[TSDB_TABLE_NAME_LEN];
D
dapan1121 已提交
146
  SCTableMeta ctbMeta;
L
Liu Jicong 已提交
147
  STableMeta* tbMeta;
D
dapan1121 已提交
148
} STableMetaOutput;
D
dapan1121 已提交
149

L
Liu Jicong 已提交
150
typedef struct SDataBuf {
D
dapan1121 已提交
151
  int32_t  msgType;
L
Liu Jicong 已提交
152 153 154
  void*    pData;
  uint32_t len;
  void*    handle;
D
dapan1121 已提交
155
  SEpSet*  pEpSet;
L
Liu Jicong 已提交
156 157
} SDataBuf;

D
dapan1121 已提交
158 159
typedef struct STargetInfo {
  ETargetType type;
dengyihao's avatar
dengyihao 已提交
160
  char*       dbFName;  // used to update db's vgroup epset
D
dapan1121 已提交
161 162 163
  int32_t     vgId;
} STargetInfo;

D
dapan1121 已提交
164
typedef struct SBoundColInfo {
165
  int16_t* pColIndex;  // bound index => schema index
D
dapan1121 已提交
166 167 168 169 170
  int32_t  numOfCols;
  int32_t  numOfBound;
} SBoundColInfo;

typedef struct STableDataCxt {
171 172
  STableMeta*    pMeta;
  STSchema*      pSchema;
D
dapan1121 已提交
173
  SBoundColInfo  boundColsInfo;
174 175
  SArray*        pValues;
  SSubmitTbData* pData;
D
dapan1121 已提交
176 177 178 179 180
  TSKEY          lastTs;
  bool           ordered;
  bool           duplicateTs;
} STableDataCxt;

D
dapan1121 已提交
181
typedef int32_t (*__async_send_cb_fn_t)(void* param, SDataBuf* pMsg, int32_t code);
L
Liu Jicong 已提交
182 183
typedef int32_t (*__async_exec_fn_t)(void* param);

D
dapan1121 已提交
184
typedef struct SRequestConnInfo {
dengyihao's avatar
dengyihao 已提交
185 186 187 188
  void*    pTrans;
  uint64_t requestId;
  int64_t  requestObjRefId;
  SEpSet   mgmtEps;
D
dapan1121 已提交
189 190
} SRequestConnInfo;

H
Hongze Cheng 已提交
191
typedef void (*__freeFunc)(void* param);
D
dapan1121 已提交
192

193
// todo add creator/destroyer function
L
Liu Jicong 已提交
194
typedef struct SMsgSendInfo {
dengyihao's avatar
dengyihao 已提交
195 196
  __async_send_cb_fn_t fp;      // async callback function
  STargetInfo          target;  // for update epset
D
dapan1121 已提交
197
  __freeFunc           paramFreeFp;
L
Liu Jicong 已提交
198 199 200 201 202
  void*                param;
  uint64_t             requestId;
  uint64_t             requestObjRefId;
  int32_t              msgType;
  SDataBuf             msgInfo;
L
Liu Jicong 已提交
203 204
} SMsgSendInfo;

D
dapan1121 已提交
205
typedef struct SQueryNodeStat {
206
  int32_t tableNum;  // vg table number, unit is TSDB_TABLE_NUM_UNIT
D
dapan1121 已提交
207 208
} SQueryNodeStat;

L
Liu Jicong 已提交
209 210 211 212 213 214 215 216 217 218 219 220
int32_t initTaskQueue();
int32_t cleanupTaskQueue();

/**
 *
 * @param execFn      The asynchronously execution function
 * @param execParam   The parameters of the execFn
 * @param code        The response code during execution the execFn
 * @return
 */
int32_t taosAsyncExec(__async_exec_fn_t execFn, void* execParam, int32_t* code);

D
dapan1121 已提交
221 222
void destroySendMsgInfo(SMsgSendInfo* pMsgBody);

dengyihao's avatar
dengyihao 已提交
223 224
void destroyAhandle(void* ahandle);

D
dapan1121 已提交
225
int32_t asyncSendMsgToServerExt(void* pTransporter, SEpSet* epSet, int64_t* pTransporterId, SMsgSendInfo* pInfo,
226
                                bool persistHandle, void* ctx);
D
dapan1121 已提交
227

L
Liu Jicong 已提交
228 229 230 231 232 233 234 235 236
/**
 * Asynchronously send message to server, after the response received, the callback will be incured.
 *
 * @param pTransporter
 * @param epSet
 * @param pTransporterId
 * @param pInfo
 * @return
 */
D
dapan1121 已提交
237
int32_t asyncSendMsgToServer(void* pTransporter, SEpSet* epSet, int64_t* pTransporterId, SMsgSendInfo* pInfo);
L
Liu Jicong 已提交
238

L
Liu Jicong 已提交
239
int32_t queryBuildUseDbOutput(SUseDbOutput* pOut, SUseDbRsp* usedbRsp);
X
Xiaoyu Wang 已提交
240

241
void initQueryModuleMsgHandle();
D
dapan1121 已提交
242

L
Liu Jicong 已提交
243
const SSchema* tGetTbnameColumnSchema();
L
Liu Jicong 已提交
244
bool           tIsValidSchema(struct SSchema* pSchema, int32_t numOfCols, int32_t numOfTags);
L
Liu Jicong 已提交
245

H
Hongze Cheng 已提交
246
int32_t queryCreateCTableMetaFromMsg(STableMetaRsp* msg, SCTableMeta* pMeta);
L
Liu Jicong 已提交
247
int32_t queryCreateTableMetaFromMsg(STableMetaRsp* msg, bool isSuperTable, STableMeta** pMeta);
248
char*   jobTaskStatusStr(int32_t status);
D
dapan1121 已提交
249

250
SSchema createSchema(int8_t type, int32_t bytes, col_id_t colId, const char* name);
dengyihao's avatar
dengyihao 已提交
251

D
dapan1121 已提交
252
void    destroyQueryExecRes(SExecResult* pRes);
dengyihao's avatar
dengyihao 已提交
253 254
int32_t dataConverToStr(char* str, int type, void* buf, int32_t bufSize, int32_t* len);
char*   parseTagDatatoJson(void* p);
D
dapan1121 已提交
255 256
int32_t cloneTableMeta(STableMeta* pSrc, STableMeta** pDst);
int32_t cloneDbVgInfo(SDBVgInfo* pSrc, SDBVgInfo** pDst);
D
dapan1121 已提交
257
int32_t cloneSVreateTbReq(SVCreateTbReq* pSrc, SVCreateTbReq** pDst);
258
void    freeVgInfo(SDBVgInfo* vgInfo);
D
dapan1121 已提交
259
void    freeDbCfgInfo(SDbCfgInfo *pInfo);
D
dapan1121 已提交
260

dengyihao's avatar
dengyihao 已提交
261
extern int32_t (*queryBuildMsg[TDMT_MAX])(void* input, char** msg, int32_t msgSize, int32_t* msgLen,
wafwerar's avatar
wafwerar 已提交
262
                                          void* (*mallocFp)(int64_t));
L
Liu Jicong 已提交
263
extern int32_t (*queryProcessMsgRsp[TDMT_MAX])(void* output, char* msg, int32_t msgSize);
D
dapan1121 已提交
264

L
Liu Jicong 已提交
265 266 267
#define SET_META_TYPE_NULL(t)       (t) = META_TYPE_NULL_TABLE
#define SET_META_TYPE_CTABLE(t)     (t) = META_TYPE_CTABLE
#define SET_META_TYPE_TABLE(t)      (t) = META_TYPE_TABLE
D
dapan1121 已提交
268 269
#define SET_META_TYPE_BOTH_TABLE(t) (t) = META_TYPE_BOTH_TABLE

270
#define NEED_CLIENT_RM_TBLMETA_ERROR(_code)                                                   \
271
  ((_code) == TSDB_CODE_PAR_TABLE_NOT_EXIST || (_code) == TSDB_CODE_TDB_TABLE_NOT_EXIST ||    \
272
   (_code) == TSDB_CODE_PAR_INVALID_COLUMNS_NUM || (_code) == TSDB_CODE_PAR_INVALID_COLUMN || \
dengyihao's avatar
dengyihao 已提交
273
   (_code) == TSDB_CODE_PAR_TAGS_NOT_MATCHED || (_code) == TSDB_CODE_PAR_VALUE_TOO_LONG ||    \
D
dapan1121 已提交
274
   (_code) == TSDB_CODE_PAR_INVALID_DROP_COL || ((_code) == TSDB_CODE_TDB_INVALID_TABLE_ID))
275 276
#define NEED_CLIENT_REFRESH_VG_ERROR(_code) \
  ((_code) == TSDB_CODE_VND_HASH_MISMATCH || (_code) == TSDB_CODE_VND_INVALID_VGROUP_ID)
D
dapan1121 已提交
277
#define NEED_CLIENT_REFRESH_TBLMETA_ERROR(_code) ((_code) == TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER)
278 279 280
#define NEED_CLIENT_HANDLE_ERROR(_code)                                          \
  (NEED_CLIENT_RM_TBLMETA_ERROR(_code) || NEED_CLIENT_REFRESH_VG_ERROR(_code) || \
   NEED_CLIENT_REFRESH_TBLMETA_ERROR(_code))
281

dengyihao's avatar
dengyihao 已提交
282 283 284 285 286 287
#define SYNC_UNKNOWN_LEADER_REDIRECT_ERROR(_code)                                    \
  ((_code) == TSDB_CODE_SYN_NOT_LEADER || (_code) == TSDB_CODE_SYN_INTERNAL_ERROR || \
   (_code) == TSDB_CODE_VND_STOPPED || (_code) == TSDB_CODE_APP_IS_STARTING || (_code) == TSDB_CODE_APP_IS_STOPPING)
#define SYNC_SELF_LEADER_REDIRECT_ERROR(_code) \
  ((_code) == TSDB_CODE_SYN_NOT_LEADER || (_code) == TSDB_CODE_SYN_RESTORING || (_code) == TSDB_CODE_SYN_INTERNAL_ERROR)
#define SYNC_OTHER_LEADER_REDIRECT_ERROR(_code) ((_code) == TSDB_CODE_MNODE_NOT_FOUND)
288

A
Alex Duan 已提交
289
#define NO_RET_REDIRECT_ERROR(_code) ((_code) == TSDB_CODE_RPC_BROKEN_LINK || (_code) == TSDB_CODE_RPC_NETWORK_UNAVAIL || (_code) == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED)
290

dengyihao's avatar
dengyihao 已提交
291
#define NEED_REDIRECT_ERROR(_code)                                              \
292 293
  (NO_RET_REDIRECT_ERROR(_code) || SYNC_UNKNOWN_LEADER_REDIRECT_ERROR(_code) || \
   SYNC_SELF_LEADER_REDIRECT_ERROR(_code) || SYNC_OTHER_LEADER_REDIRECT_ERROR(_code))
H
Hongze Cheng 已提交
294

dengyihao's avatar
dengyihao 已提交
295
#define NEED_CLIENT_RM_TBLMETA_REQ(_type)                                                                  \
wmmhello's avatar
wmmhello 已提交
296 297
  ((_type) == TDMT_VND_CREATE_TABLE || (_type) == TDMT_MND_CREATE_STB || (_type) == TDMT_VND_DROP_TABLE || \
   (_type) == TDMT_MND_DROP_STB)
D
dapan 已提交
298

dengyihao's avatar
dengyihao 已提交
299 300 301
#define NEED_SCHEDULER_REDIRECT_ERROR(_code)                                              \
  (SYNC_UNKNOWN_LEADER_REDIRECT_ERROR(_code) || SYNC_SELF_LEADER_REDIRECT_ERROR(_code) || \
   SYNC_OTHER_LEADER_REDIRECT_ERROR(_code))
D
dapan1121 已提交
302

303
#define REQUEST_TOTAL_EXEC_TIMES 2
D
dapan1121 已提交
304

D
dapan1121 已提交
305 306 307 308
#define IS_INFORMATION_SCHEMA_DB(_name) ((*(_name) == 'i') && (0 == strcmp(_name, TSDB_INFORMATION_SCHEMA_DB)))
#define IS_PERFORMANCE_SCHEMA_DB(_name) ((*(_name) == 'p') && (0 == strcmp(_name, TSDB_PERFORMANCE_SCHEMA_DB)))

#define IS_SYS_DBNAME(_dbname) (IS_INFORMATION_SCHEMA_DB(_dbname) || IS_PERFORMANCE_SCHEMA_DB(_dbname))
D
dapan1121 已提交
309

H
Hongze Cheng 已提交
310 311 312
#define qFatal(...)                                                     \
  do {                                                                  \
    if (qDebugFlag & DEBUG_FATAL) {                                     \
D
dapan1121 已提交
313
      taosPrintLog("QRY FATAL ", DEBUG_FATAL, qDebugFlag, __VA_ARGS__); \
H
Hongze Cheng 已提交
314
    }                                                                   \
L
Liu Jicong 已提交
315
  } while (0)
H
Hongze Cheng 已提交
316 317 318
#define qError(...)                                                     \
  do {                                                                  \
    if (qDebugFlag & DEBUG_ERROR) {                                     \
D
dapan1121 已提交
319
      taosPrintLog("QRY ERROR ", DEBUG_ERROR, qDebugFlag, __VA_ARGS__); \
H
Hongze Cheng 已提交
320
    }                                                                   \
L
Liu Jicong 已提交
321
  } while (0)
H
Hongze Cheng 已提交
322 323 324
#define qWarn(...)                                                    \
  do {                                                                \
    if (qDebugFlag & DEBUG_WARN) {                                    \
D
dapan1121 已提交
325
      taosPrintLog("QRY WARN ", DEBUG_WARN, qDebugFlag, __VA_ARGS__); \
H
Hongze Cheng 已提交
326
    }                                                                 \
L
Liu Jicong 已提交
327
  } while (0)
H
Hongze Cheng 已提交
328 329 330
#define qInfo(...)                                               \
  do {                                                           \
    if (qDebugFlag & DEBUG_INFO) {                               \
D
dapan1121 已提交
331
      taosPrintLog("QRY ", DEBUG_INFO, qDebugFlag, __VA_ARGS__); \
H
Hongze Cheng 已提交
332
    }                                                            \
L
Liu Jicong 已提交
333
  } while (0)
H
Hongze Cheng 已提交
334 335 336
#define qDebug(...)                                               \
  do {                                                            \
    if (qDebugFlag & DEBUG_DEBUG) {                               \
D
dapan1121 已提交
337
      taosPrintLog("QRY ", DEBUG_DEBUG, qDebugFlag, __VA_ARGS__); \
H
Hongze Cheng 已提交
338
    }                                                             \
L
Liu Jicong 已提交
339
  } while (0)
H
Hongze Cheng 已提交
340 341 342
#define qTrace(...)                                               \
  do {                                                            \
    if (qDebugFlag & DEBUG_TRACE) {                               \
D
dapan1121 已提交
343
      taosPrintLog("QRY ", DEBUG_TRACE, qDebugFlag, __VA_ARGS__); \
H
Hongze Cheng 已提交
344
    }                                                             \
L
Liu Jicong 已提交
345
  } while (0)
H
Hongze Cheng 已提交
346 347 348
#define qDebugL(...)                                                     \
  do {                                                                   \
    if (qDebugFlag & DEBUG_DEBUG) {                                      \
D
dapan1121 已提交
349
      taosPrintLongString("QRY ", DEBUG_DEBUG, qDebugFlag, __VA_ARGS__); \
H
Hongze Cheng 已提交
350
    }                                                                    \
L
Liu Jicong 已提交
351
  } while (0)
D
dapan1121 已提交
352

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
#define QRY_ERR_RET(c)                \
  do {                                \
    int32_t _code = c;                \
    if (_code != TSDB_CODE_SUCCESS) { \
      terrno = _code;                 \
      return _code;                   \
    }                                 \
  } while (0)
#define QRY_RET(c)                    \
  do {                                \
    int32_t _code = c;                \
    if (_code != TSDB_CODE_SUCCESS) { \
      terrno = _code;                 \
    }                                 \
    return _code;                     \
  } while (0)
#define QRY_ERR_JRET(c)              \
  do {                               \
    code = c;                        \
    if (code != TSDB_CODE_SUCCESS) { \
      terrno = code;                 \
      goto _return;                  \
    }                                \
  } while (0)
D
dapan1121 已提交
377

D
dapan1121 已提交
378 379 380 381
#ifdef __cplusplus
}
#endif

D
dapan1121 已提交
382
#endif /*_TD_QUERY_H_*/
dengyihao's avatar
dengyihao 已提交
383
       // clang-foramt on