• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4113

17 May 2025 06:43AM UTC coverage: 62.054% (-0.8%) from 62.857%
#4113

push

travis-ci

web-flow
Merge pull request #31115 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

154737 of 318088 branches covered (48.65%)

Branch coverage included in aggregate %.

175 of 225 new or added lines in 20 files covered. (77.78%)

5853 existing lines in 216 files now uncovered.

239453 of 317147 relevant lines covered (75.5%)

15121865.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

16
#include "cJSON.h"
17
#include "clientInt.h"
18
#include "clientLog.h"
19
#include "clientMonitor.h"
20
#include "command.h"
21
#include "scheduler.h"
22
#include "tdatablock.h"
23
#include "tdataformat.h"
24
#include "tdef.h"
25
#include "tglobal.h"
26
#include "tmsgtype.h"
27
#include "tpagedbuf.h"
28
#include "tref.h"
29
#include "tsched.h"
30
#include "tversion.h"
31
#include "decimal.h"
32

33
static int32_t initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSet);
34
static int32_t buildConnectMsg(SRequestObj* pRequest, SMsgSendInfo** pMsgSendInfo);
35

36
void setQueryRequest(int64_t rId) {
1,582,549✔
37
  SRequestObj* pReq = acquireRequest(rId);
1,582,549✔
38
  if (pReq != NULL) {
1,582,552✔
39
    pReq->isQuery = true;
1,582,550✔
40
    (void)releaseRequest(rId);
1,582,550✔
41
  }
42
}
1,582,544✔
43

44
static bool stringLengthCheck(const char* str, size_t maxsize) {
65,616✔
45
  if (str == NULL) {
65,616!
46
    return false;
×
47
  }
48

49
  size_t len = strlen(str);
65,616✔
50
  if (len <= 0 || len > maxsize) {
65,616!
51
    return false;
×
52
  }
53

54
  return true;
65,680✔
55
}
56

57
static bool validateUserName(const char* user) { return stringLengthCheck(user, TSDB_USER_LEN - 1); }
32,092✔
58

59
static bool validatePassword(const char* passwd) { return stringLengthCheck(passwd, TSDB_PASSWORD_MAX_LEN); }
32,066✔
60

61
static bool validateDbName(const char* db) { return stringLengthCheck(db, TSDB_DB_NAME_LEN - 1); }
1,547✔
62

63
static char* getClusterKey(const char* user, const char* auth, const char* ip, int32_t port) {
32,173✔
64
  char key[512] = {0};
32,173✔
65
  (void)snprintf(key, sizeof(key), "%s:%s:%s:%d", user, auth, ip, port);
32,173✔
66
  return taosStrdup(key);
32,173!
67
}
68

69
bool chkRequestKilled(void* param) {
35,953,608✔
70
  bool         killed = false;
35,953,608✔
71
  SRequestObj* pRequest = acquireRequest((int64_t)param);
35,953,608✔
72
  if (NULL == pRequest || pRequest->killed) {
36,494,584!
73
    killed = true;
×
74
  }
75

76
  (void)releaseRequest((int64_t)param);
36,494,584✔
77

78
  return killed;
36,396,554✔
79
}
80

81
void cleanupAppInfo() {
17,914✔
82
  taosHashCleanup(appInfo.pInstMap);
17,914✔
83
  taosHashCleanup(appInfo.pInstMapByClusterId);
17,914✔
84
  tscInfo("cluster instance map cleaned");
17,914!
85
}
17,914✔
86

87
static int32_t taosConnectImpl(const char* user, const char* auth, const char* db, __taos_async_fn_t fp, void* param,
88
                               SAppInstInfo* pAppInfo, int connType, STscObj** pTscObj);
89

90
int32_t taos_connect_internal(const char* ip, const char* user, const char* pass, const char* auth, const char* db,
32,017✔
91
                              uint16_t port, int connType, STscObj** pObj) {
92
  TSC_ERR_RET(taos_init());
32,017!
93
  if (!validateUserName(user)) {
32,115!
94
    TSC_ERR_RET(TSDB_CODE_TSC_INVALID_USER_LENGTH);
×
95
  }
96

97
  char localDb[TSDB_DB_NAME_LEN] = {0};
32,052✔
98
  if (db != NULL && strlen(db) > 0) {
32,052✔
99
    if (!validateDbName(db)) {
1,547!
100
      TSC_ERR_RET(TSDB_CODE_TSC_INVALID_DB_LENGTH);
×
101
    }
102

103
    tstrncpy(localDb, db, sizeof(localDb));
1,547✔
104
    (void)strdequote(localDb);
1,547✔
105
  }
106

107
  char secretEncrypt[TSDB_PASSWORD_LEN + 1] = {0};
32,085✔
108
  if (auth == NULL) {
32,085✔
109
    if (!validatePassword(pass)) {
32,081!
110
      TSC_ERR_RET(TSDB_CODE_TSC_INVALID_PASS_LENGTH);
×
111
    }
112

113
    taosEncryptPass_c((uint8_t*)pass, strlen(pass), secretEncrypt);
32,083✔
114
  } else {
115
    tstrncpy(secretEncrypt, auth, tListLen(secretEncrypt));
4✔
116
  }
117

118
  SCorEpSet epSet = {0};
32,062✔
119
  if (ip) {
32,062✔
120
    TSC_ERR_RET(initEpSetFromCfg(ip, NULL, &epSet));
10,450✔
121
  } else {
122
    TSC_ERR_RET(initEpSetFromCfg(tsFirst, tsSecond, &epSet));
21,612!
123
  }
124

125
  if (port) {
32,173✔
126
    epSet.epSet.eps[0].port = port;
627✔
127
    epSet.epSet.eps[1].port = port;
627✔
128
  }
129

130
  char* key = getClusterKey(user, secretEncrypt, ip, port);
32,173✔
131
  if (NULL == key) {
32,171!
132
    TSC_ERR_RET(terrno);
×
133
  }
134
  tscInfo("connecting to server, numOfEps:%d inUse:%d user:%s db:%s key:%s", epSet.epSet.numOfEps, epSet.epSet.inUse,
32,171!
135
          user, db, key);
136
  for (int32_t i = 0; i < epSet.epSet.numOfEps; ++i) {
85,955✔
137
    tscInfo("ep:%d, %s:%u", i, epSet.epSet.eps[i].fqdn, epSet.epSet.eps[i].port);
53,781!
138
  }
139

140
  SAppInstInfo** pInst = NULL;
32,174✔
141
  int32_t        code = taosThreadMutexLock(&appInfo.mutex);
32,174✔
142
  if (TSDB_CODE_SUCCESS != code) {
32,174!
143
    tscError("failed to lock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
144
    TSC_ERR_RET(code);
×
145
  }
146

147
  pInst = taosHashGet(appInfo.pInstMap, key, strlen(key));
32,174✔
148
  SAppInstInfo* p = NULL;
32,174✔
149
  if (pInst == NULL) {
32,174✔
150
    p = taosMemoryCalloc(1, sizeof(struct SAppInstInfo));
18,169!
151
    if (NULL == p) {
18,169!
152
      TSC_ERR_JRET(terrno);
×
153
    }
154
    p->mgmtEp = epSet;
18,169✔
155
    code = taosThreadMutexInit(&p->qnodeMutex, NULL);
18,169✔
156
    if (TSDB_CODE_SUCCESS != code) {
18,169!
157
      taosMemoryFree(p);
×
158
      TSC_ERR_JRET(code);
×
159
    }
160
    code = openTransporter(user, secretEncrypt, tsNumOfCores / 2, &p->pTransporter);
18,169✔
161
    if (TSDB_CODE_SUCCESS != code) {
18,169!
162
      taosMemoryFree(p);
×
163
      TSC_ERR_JRET(code);
×
164
    }
165
    code = appHbMgrInit(p, key, &p->pAppHbMgr);
18,169✔
166
    if (TSDB_CODE_SUCCESS != code) {
18,169!
167
      destroyAppInst(&p);
×
168
      TSC_ERR_JRET(code);
×
169
    }
170
    code = taosHashPut(appInfo.pInstMap, key, strlen(key), &p, POINTER_BYTES);
18,169✔
171
    if (TSDB_CODE_SUCCESS != code) {
18,169!
172
      destroyAppInst(&p);
×
173
      TSC_ERR_JRET(code);
×
174
    }
175
    p->instKey = key;
18,169✔
176
    key = NULL;
18,169✔
177
    tscInfo("new app inst mgr:%p, user:%s, ip:%s, port:%d", p, user, epSet.epSet.eps[0].fqdn, epSet.epSet.eps[0].port);
18,169!
178

179
    pInst = &p;
18,169✔
180
  } else {
181
    if (NULL == *pInst || NULL == (*pInst)->pAppHbMgr) {
14,005!
182
      tscError("*pInst:%p, pAppHgMgr:%p", *pInst, (*pInst) ? (*pInst)->pAppHbMgr : NULL);
×
183
      TSC_ERR_JRET(TSDB_CODE_TSC_INTERNAL_ERROR);
×
184
    }
185
    // reset to 0 in case of conn with duplicated user key but its user has ever been dropped.
186
    atomic_store_8(&(*pInst)->pAppHbMgr->connHbFlag, 0);
14,005✔
187
  }
188

189
_return:
32,174✔
190

191
  if (TSDB_CODE_SUCCESS != code) {
32,174!
192
    (void)taosThreadMutexUnlock(&appInfo.mutex);
×
193
    taosMemoryFreeClear(key);
×
194
    return code;
×
195
  } else {
196
    code = taosThreadMutexUnlock(&appInfo.mutex);
32,174✔
197
    taosMemoryFreeClear(key);
32,174!
198
    if (TSDB_CODE_SUCCESS != code) {
32,174!
199
      tscError("failed to unlock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
200
      return code;
×
201
    }
202
    return taosConnectImpl(user, &secretEncrypt[0], localDb, NULL, NULL, *pInst, connType, pObj);
32,174✔
203
  }
204
}
205

206
// SAppInstInfo* getAppInstInfo(const char* clusterKey) {
207
//   SAppInstInfo** ppAppInstInfo = taosHashGet(appInfo.pInstMap, clusterKey, strlen(clusterKey));
208
//   if (ppAppInstInfo != NULL && *ppAppInstInfo != NULL) {
209
//     return *ppAppInstInfo;
210
//   } else {
211
//     return NULL;
212
//   }
213
// }
214

215
void freeQueryParam(SSyncQueryParam* param) {
933✔
216
  if (param == NULL) return;
933!
217
  if (TSDB_CODE_SUCCESS != tsem_destroy(&param->sem)) {
933!
218
    tscError("failed to destroy semaphore in freeQueryParam");
×
219
  }
220
  taosMemoryFree(param);
933!
221
}
222

223
int32_t buildRequest(uint64_t connId, const char* sql, int sqlLen, void* param, bool validateSql,
11,012,219✔
224
                     SRequestObj** pRequest, int64_t reqid) {
225
  int32_t code = createRequest(connId, TSDB_SQL_SELECT, reqid, pRequest);
11,012,219✔
226
  if (TSDB_CODE_SUCCESS != code) {
11,024,694!
227
    tscError("failed to malloc sqlObj, %s", sql);
×
228
    return code;
×
229
  }
230

231
  (*pRequest)->sqlstr = taosMemoryMalloc(sqlLen + 1);
11,024,694!
232
  if ((*pRequest)->sqlstr == NULL) {
11,007,515!
233
    tscError("req:0x%" PRIx64 ", failed to prepare sql string buffer, %s", (*pRequest)->self, sql);
×
234
    destroyRequest(*pRequest);
×
235
    *pRequest = NULL;
×
236
    return terrno;
×
237
  }
238

239
  (void)strntolower((*pRequest)->sqlstr, sql, (int32_t)sqlLen);
11,007,515✔
240
  (*pRequest)->sqlstr[sqlLen] = 0;
11,025,022✔
241
  (*pRequest)->sqlLen = sqlLen;
11,025,022✔
242
  (*pRequest)->validateOnly = validateSql;
11,025,022✔
243
  (*pRequest)->isStmtBind = false;
11,025,022✔
244

245
  ((SSyncQueryParam*)(*pRequest)->body.interParam)->userParam = param;
11,025,022✔
246

247
  STscObj* pTscObj = (*pRequest)->pTscObj;
11,025,022✔
248
  int32_t  err = taosHashPut(pTscObj->pRequests, &(*pRequest)->self, sizeof((*pRequest)->self), &(*pRequest)->self,
11,025,022✔
249
                             sizeof((*pRequest)->self));
250
  if (err) {
11,004,855!
251
    tscError("req:0x%" PRId64 ", failed to add to request container, QID:0x%" PRIx64 ", conn:%" PRId64 ", %s",
×
252
             (*pRequest)->self, (*pRequest)->requestId, pTscObj->id, sql);
253
    destroyRequest(*pRequest);
×
254
    *pRequest = NULL;
×
255
    return terrno;
×
256
  }
257

258
  (*pRequest)->allocatorRefId = -1;
11,004,855✔
259
  if (tsQueryUseNodeAllocator && !qIsInsertValuesSql((*pRequest)->sqlstr, (*pRequest)->sqlLen)) {
11,004,855!
260
    if (TSDB_CODE_SUCCESS !=
1,373,856!
261
        nodesCreateAllocator((*pRequest)->requestId, tsQueryNodeChunkSize, &((*pRequest)->allocatorRefId))) {
1,373,857✔
262
      tscError("req:0x%" PRId64 ", failed to create node allocator, QID:0x%" PRIx64 ", conn:%" PRId64 ", %s", (*pRequest)->self,
×
263
               (*pRequest)->requestId, pTscObj->id, sql);
264
      destroyRequest(*pRequest);
×
265
      *pRequest = NULL;
×
266
      return terrno;
×
267
    }
268
  }
269

270
  tscDebug("req:0x%" PRIx64 ", build request, QID:0x%" PRIx64, (*pRequest)->self, (*pRequest)->requestId);
11,016,802✔
271
  return TSDB_CODE_SUCCESS;
11,015,315✔
272
}
273

274
int32_t buildPreviousRequest(SRequestObj* pRequest, const char* sql, SRequestObj** pNewRequest) {
462✔
275
  int32_t code =
276
      buildRequest(pRequest->pTscObj->id, sql, strlen(sql), pRequest, pRequest->validateOnly, pNewRequest, 0);
462✔
277
  if (TSDB_CODE_SUCCESS == code) {
462!
278
    pRequest->relation.prevRefId = (*pNewRequest)->self;
462✔
279
    (*pNewRequest)->relation.nextRefId = pRequest->self;
462✔
280
    (*pNewRequest)->relation.userRefId = pRequest->self;
462✔
281
    (*pNewRequest)->isSubReq = true;
462✔
282
  }
283
  return code;
462✔
284
}
285

286
int32_t parseSql(SRequestObj* pRequest, bool topicQuery, SQuery** pQuery, SStmtCallback* pStmtCb) {
20,711✔
287
  STscObj* pTscObj = pRequest->pTscObj;
20,711✔
288

289
  SParseContext cxt = {.requestId = pRequest->requestId,
20,711✔
290
                       .requestRid = pRequest->self,
20,711✔
291
                       .acctId = pTscObj->acctId,
20,711✔
292
                       .db = pRequest->pDb,
20,711✔
293
                       .topicQuery = topicQuery,
294
                       .pSql = pRequest->sqlstr,
20,711✔
295
                       .sqlLen = pRequest->sqlLen,
20,711✔
296
                       .pMsg = pRequest->msgBuf,
20,711✔
297
                       .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
298
                       .pTransporter = pTscObj->pAppInfo->pTransporter,
20,711✔
299
                       .pStmtCb = pStmtCb,
300
                       .pUser = pTscObj->user,
20,711✔
301
                       .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
20,711✔
302
                       .enableSysInfo = pTscObj->sysInfo,
20,711✔
303
                       .svrVer = pTscObj->sVer,
20,711✔
304
                       .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
20,711✔
305
                       .isStmtBind = pRequest->isStmtBind,
20,711✔
306
                       .setQueryFp = setQueryRequest,
307
                       .timezone = pTscObj->optionInfo.timezone,
20,711✔
308
                       .charsetCxt = pTscObj->optionInfo.charsetCxt,};
20,711✔
309

310
  cxt.mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
20,711✔
311
  int32_t code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &cxt.pCatalog);
20,748✔
312
  if (code != TSDB_CODE_SUCCESS) {
20,735!
313
    return code;
×
314
  }
315

316
  code = qParseSql(&cxt, pQuery);
20,735✔
317
  if (TSDB_CODE_SUCCESS == code) {
20,720!
318
    if ((*pQuery)->haveResultSet) {
20,724!
319
      code = setResSchemaInfo(&pRequest->body.resInfo, (*pQuery)->pResSchema, (*pQuery)->numOfResCols, (*pQuery)->pResExtSchema, pRequest->isStmtBind);
×
320
      setResPrecision(&pRequest->body.resInfo, (*pQuery)->precision);
×
321
    }
322
  }
323

324
  if (TSDB_CODE_SUCCESS == code || NEED_CLIENT_HANDLE_ERROR(code)) {
20,720!
325
    TSWAP(pRequest->dbList, (*pQuery)->pDbList);
20,720✔
326
    TSWAP(pRequest->tableList, (*pQuery)->pTableList);
20,720✔
327
    TSWAP(pRequest->targetTableList, (*pQuery)->pTargetTableList);
20,720✔
328
  }
329

330
  taosArrayDestroy(cxt.pTableMetaPos);
20,720✔
331
  taosArrayDestroy(cxt.pTableVgroupPos);
20,736✔
332

333
  return code;
20,735✔
334
}
335

336
int32_t execLocalCmd(SRequestObj* pRequest, SQuery* pQuery) {
×
337
  SRetrieveTableRsp* pRsp = NULL;
×
338
  int8_t             biMode = atomic_load_8(&pRequest->pTscObj->biMode);
×
339
  int32_t code = qExecCommand(&pRequest->pTscObj->id, pRequest->pTscObj->sysInfo, pQuery->pRoot, &pRsp, biMode, pRequest->pTscObj->optionInfo.charsetCxt);
×
340
  if (TSDB_CODE_SUCCESS == code && NULL != pRsp) {
×
341
    code = setQueryResultFromRsp(&pRequest->body.resInfo, pRsp, pRequest->body.resInfo.convertUcs4, pRequest->isStmtBind);
×
342
  }
343

344
  return code;
×
345
}
346

347
int32_t execDdlQuery(SRequestObj* pRequest, SQuery* pQuery) {
673✔
348
  // drop table if exists not_exists_table
349
  if (NULL == pQuery->pCmdMsg) {
673!
350
    return TSDB_CODE_SUCCESS;
×
351
  }
352

353
  SCmdMsgInfo* pMsgInfo = pQuery->pCmdMsg;
673✔
354
  pRequest->type = pMsgInfo->msgType;
673✔
355
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsgInfo->pMsg, .len = pMsgInfo->msgLen, .handle = NULL};
673✔
356
  pMsgInfo->pMsg = NULL;  // pMsg transferred to SMsgSendInfo management
673✔
357

358
  STscObj*      pTscObj = pRequest->pTscObj;
673✔
359
  SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest);
673✔
360

361
  // int64_t transporterId = 0;
362
  TSC_ERR_RET(asyncSendMsgToServer(pTscObj->pAppInfo->pTransporter, &pMsgInfo->epSet, NULL, pSendMsg));
671!
363
  TSC_ERR_RET(tsem_wait(&pRequest->body.rspSem));
678!
364
  return TSDB_CODE_SUCCESS;
678✔
365
}
366

367
static SAppInstInfo* getAppInfo(SRequestObj* pRequest) { return pRequest->pTscObj->pAppInfo; }
21,234,188✔
368

369
void asyncExecLocalCmd(SRequestObj* pRequest, SQuery* pQuery) {
131,270✔
370
  SRetrieveTableRsp* pRsp = NULL;
131,270✔
371
  if (pRequest->validateOnly) {
131,270✔
372
    doRequestCallback(pRequest, 0);
27✔
373
    return;
27✔
374
  }
375

376
  int32_t code = qExecCommand(&pRequest->pTscObj->id, pRequest->pTscObj->sysInfo, pQuery->pRoot, &pRsp,
131,243✔
377
                              atomic_load_8(&pRequest->pTscObj->biMode), pRequest->pTscObj->optionInfo.charsetCxt);
131,243✔
378
  if (TSDB_CODE_SUCCESS == code && NULL != pRsp) {
131,243✔
379
    code = setQueryResultFromRsp(&pRequest->body.resInfo, pRsp, pRequest->body.resInfo.convertUcs4, pRequest->isStmtBind);
121,162✔
380
  }
381

382
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
131,241✔
383
  pRequest->code = code;
131,241✔
384

385
  if (pRequest->code != TSDB_CODE_SUCCESS) {
131,241✔
386
    pResultInfo->numOfRows = 0;
3✔
387
    tscError("req:0x%" PRIx64 ", fetch results failed, code:%s, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
3!
388
             pRequest->requestId);
389
  } else {
390
    tscDebug("req:0x%" PRIx64 ", fetch results, numOfRows:%" PRId64 " total Rows:%" PRId64 ", complete:%d, QID:0x%" PRIx64,
131,238✔
391
             pRequest->self, pResultInfo->numOfRows, pResultInfo->totalRows, pResultInfo->completed,
392
             pRequest->requestId);
393
  }
394

395
  doRequestCallback(pRequest, code);
131,242✔
396
}
397

398
int32_t asyncExecDdlQuery(SRequestObj* pRequest, SQuery* pQuery) {
34,065✔
399
  if (pRequest->validateOnly) {
34,065!
400
    doRequestCallback(pRequest, 0);
×
401
    return TSDB_CODE_SUCCESS;
×
402
  }
403

404
  // drop table if exists not_exists_table
405
  if (NULL == pQuery->pCmdMsg) {
34,065✔
406
    doRequestCallback(pRequest, 0);
1✔
407
    return TSDB_CODE_SUCCESS;
1✔
408
  }
409

410
  SCmdMsgInfo* pMsgInfo = pQuery->pCmdMsg;
34,064✔
411
  pRequest->type = pMsgInfo->msgType;
34,064✔
412
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsgInfo->pMsg, .len = pMsgInfo->msgLen, .handle = NULL};
34,064✔
413
  pMsgInfo->pMsg = NULL;  // pMsg transferred to SMsgSendInfo management
34,064✔
414

415
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
34,064✔
416
  SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest);
34,043✔
417

418
  int32_t code = asyncSendMsgToServer(pAppInfo->pTransporter, &pMsgInfo->epSet, NULL, pSendMsg);
34,074✔
419
  if (code) {
34,101!
420
    doRequestCallback(pRequest, code);
×
421
  }
422
  return code;
34,101✔
423
}
424

425
int compareQueryNodeLoad(const void* elem1, const void* elem2) {
199,026✔
426
  SQueryNodeLoad* node1 = (SQueryNodeLoad*)elem1;
199,026✔
427
  SQueryNodeLoad* node2 = (SQueryNodeLoad*)elem2;
199,026✔
428

429
  if (node1->load < node2->load) {
199,026!
430
    return -1;
×
431
  }
432

433
  return node1->load > node2->load;
199,026✔
434
}
435

436
int32_t updateQnodeList(SAppInstInfo* pInfo, SArray* pNodeList) {
71,270✔
437
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
71,270!
438
  if (pInfo->pQnodeList) {
71,270✔
439
    taosArrayDestroy(pInfo->pQnodeList);
70,648✔
440
    pInfo->pQnodeList = NULL;
70,648✔
441
    tscDebug("QnodeList cleared in cluster 0x%" PRIx64, pInfo->clusterId);
70,648✔
442
  }
443

444
  if (pNodeList) {
71,270!
445
    pInfo->pQnodeList = taosArrayDup(pNodeList, NULL);
71,270✔
446
    taosArraySort(pInfo->pQnodeList, compareQueryNodeLoad);
71,270✔
447
    tscDebug("QnodeList updated in cluster 0x%" PRIx64 ", num:%ld", pInfo->clusterId,
71,270✔
448
             taosArrayGetSize(pInfo->pQnodeList));
449
  }
450
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
71,270!
451

452
  return TSDB_CODE_SUCCESS;
71,270✔
453
}
454

455
int32_t qnodeRequired(SRequestObj* pRequest, bool* required) {
10,987,556✔
456
  if (QUERY_POLICY_VNODE == tsQueryPolicy || QUERY_POLICY_CLIENT == tsQueryPolicy) {
10,987,556✔
457
    *required = false;
10,338,304✔
458
    return TSDB_CODE_SUCCESS;
10,338,304✔
459
  }
460

461
  int32_t       code = TSDB_CODE_SUCCESS;
649,252✔
462
  SAppInstInfo* pInfo = pRequest->pTscObj->pAppInfo;
649,252✔
463
  *required = false;
649,252✔
464

465
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
649,252!
466
  *required = (NULL == pInfo->pQnodeList);
649,252✔
467
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
649,252!
468
  return TSDB_CODE_SUCCESS;
649,252✔
469
}
470

471
int32_t getQnodeList(SRequestObj* pRequest, SArray** pNodeList) {
×
472
  SAppInstInfo* pInfo = pRequest->pTscObj->pAppInfo;
×
473
  int32_t       code = 0;
×
474

475
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
×
476
  if (pInfo->pQnodeList) {
×
477
    *pNodeList = taosArrayDup(pInfo->pQnodeList, NULL);
×
478
  }
479
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
×
480
  if (NULL == *pNodeList) {
×
481
    SCatalog* pCatalog = NULL;
×
482
    code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
×
483
    if (TSDB_CODE_SUCCESS == code) {
×
484
      *pNodeList = taosArrayInit(5, sizeof(SQueryNodeLoad));
×
485
      if (NULL == pNodeList) {
×
486
        TSC_ERR_RET(terrno);
×
487
      }
488
      SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
×
489
                               .requestId = pRequest->requestId,
×
490
                               .requestObjRefId = pRequest->self,
×
491
                               .mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp)};
×
492
      code = catalogGetQnodeList(pCatalog, &conn, *pNodeList);
×
493
    }
494

495
    if (TSDB_CODE_SUCCESS == code && *pNodeList) {
×
496
      code = updateQnodeList(pInfo, *pNodeList);
×
497
    }
498
  }
499

500
  return code;
×
501
}
502

503
int32_t getPlan(SRequestObj* pRequest, SQuery* pQuery, SQueryPlan** pPlan, SArray* pNodeList) {
63,972✔
504
  pRequest->type = pQuery->msgType;
63,972✔
505
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
63,972✔
506

507
  SPlanContext cxt = {.queryId = pRequest->requestId,
128,023✔
508
                      .acctId = pRequest->pTscObj->acctId,
63,982✔
509
                      .mgmtEpSet = getEpSet_s(&pAppInfo->mgmtEp),
63,982✔
510
                      .pAstRoot = pQuery->pRoot,
64,041✔
511
                      .showRewrite = pQuery->showRewrite,
64,041✔
512
                      .pMsg = pRequest->msgBuf,
64,041✔
513
                      .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
514
                      .pUser = pRequest->pTscObj->user,
64,041✔
515
                      .timezone = pRequest->pTscObj->optionInfo.timezone,
64,041✔
516
                      .sysInfo = pRequest->pTscObj->sysInfo};
64,041✔
517

518
  return qCreateQueryPlan(&cxt, pPlan, pNodeList);
64,041✔
519
}
520

521
int32_t setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t numOfCols, const SExtSchema* pExtSchema, bool isStmt) {
1,358,902✔
522
  if (pResInfo == NULL || pSchema == NULL || numOfCols <= 0) {
1,358,902!
523
    tscError("invalid paras, pResInfo == NULL || pSchema == NULL || numOfCols <= 0");
×
524
    return TSDB_CODE_INVALID_PARA;
×
525
  }
526

527
  pResInfo->numOfCols = numOfCols;
1,358,950✔
528
  if (pResInfo->fields != NULL) {
1,358,950✔
529
    taosMemoryFree(pResInfo->fields);
51!
530
  }
531
  if (pResInfo->userFields != NULL) {
1,358,950✔
532
    taosMemoryFree(pResInfo->userFields);
51!
533
  }
534
  pResInfo->fields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD_E));
1,358,950!
535
  if (NULL == pResInfo->fields) return terrno;
1,358,946!
536
  pResInfo->userFields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD));
1,358,946!
537
  if (NULL == pResInfo->userFields) {
1,358,914!
538
    taosMemoryFree(pResInfo->fields);
×
539
    return terrno;
×
540
  }
541
  if (numOfCols != pResInfo->numOfCols) {
1,358,914!
542
    tscError("numOfCols:%d != pResInfo->numOfCols:%d", numOfCols, pResInfo->numOfCols);
×
543
    return TSDB_CODE_FAILED;
×
544
  }
545

546
  for (int32_t i = 0; i < pResInfo->numOfCols; ++i) {
5,634,278✔
547
    pResInfo->fields[i].type = pSchema[i].type;
4,275,393✔
548

549
    pResInfo->userFields[i].type = pSchema[i].type;
4,275,393✔
550
    // userFields must convert to type bytes, no matter isStmt or not
551
    pResInfo->userFields[i].bytes = calcTypeBytesFromSchemaBytes(pSchema[i].type, pSchema[i].bytes, false);
4,275,393✔
552
    pResInfo->fields[i].bytes = calcTypeBytesFromSchemaBytes(pSchema[i].type, pSchema[i].bytes, isStmt);
4,275,374✔
553
    if (IS_DECIMAL_TYPE(pSchema[i].type) && pExtSchema) {
4,275,368!
554
      decimalFromTypeMod(pExtSchema[i].typeMod, &pResInfo->fields[i].precision, &pResInfo->fields[i].scale);
8,624✔
555
    }
556

557
    tstrncpy(pResInfo->fields[i].name, pSchema[i].name, tListLen(pResInfo->fields[i].name));
4,275,364✔
558
    tstrncpy(pResInfo->userFields[i].name, pSchema[i].name, tListLen(pResInfo->userFields[i].name));
4,275,364✔
559
  }
560
  return TSDB_CODE_SUCCESS;
1,358,885✔
561
}
562

563
void setResPrecision(SReqResultInfo* pResInfo, int32_t precision) {
1,026,842✔
564
  if (precision != TSDB_TIME_PRECISION_MILLI && precision != TSDB_TIME_PRECISION_MICRO &&
1,026,842!
565
      precision != TSDB_TIME_PRECISION_NANO) {
566
    return;
×
567
  }
568

569
  pResInfo->precision = precision;
1,026,842✔
570
}
571

572
int32_t buildVnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SArray* pDbVgList) {
750,836✔
573
  SArray* nodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
750,836✔
574
  if (NULL == nodeList) {
750,954!
575
    return terrno;
×
576
  }
577
  char* policy = (tsQueryPolicy == QUERY_POLICY_VNODE) ? "vnode" : "client";
750,975✔
578

579
  int32_t dbNum = taosArrayGetSize(pDbVgList);
750,975✔
580
  for (int32_t i = 0; i < dbNum; ++i) {
1,477,754✔
581
    SArray* pVg = taosArrayGetP(pDbVgList, i);
726,736✔
582
    if (NULL == pVg) {
726,735!
583
      continue;
×
584
    }
585
    int32_t vgNum = taosArrayGetSize(pVg);
726,735✔
586
    if (vgNum <= 0) {
726,729✔
587
      continue;
624✔
588
    }
589

590
    for (int32_t j = 0; j < vgNum; ++j) {
3,690,144✔
591
      SVgroupInfo* pInfo = taosArrayGet(pVg, j);
2,963,997✔
592
      if (NULL == pInfo) {
2,963,833!
593
        taosArrayDestroy(nodeList);
×
594
        return TSDB_CODE_OUT_OF_RANGE;
×
595
      }
596
      SQueryNodeLoad load = {0};
2,963,833✔
597
      load.addr.nodeId = pInfo->vgId;
2,963,833✔
598
      load.addr.epSet = pInfo->epSet;
2,963,833✔
599

600
      if (NULL == taosArrayPush(nodeList, &load)) {
2,964,039!
601
        taosArrayDestroy(nodeList);
×
602
        return terrno;
×
603
      }
604
    }
605
  }
606

607
  int32_t vnodeNum = taosArrayGetSize(nodeList);
751,018✔
608
  if (vnodeNum > 0) {
751,016✔
609
    tscDebug("0x%" PRIx64 " %s policy, use vnode list, num:%d", pRequest->requestId, policy, vnodeNum);
725,363✔
610
    goto _return;
725,355✔
611
  }
612

613
  int32_t mnodeNum = taosArrayGetSize(pMnodeList);
25,653✔
614
  if (mnodeNum <= 0) {
25,662!
615
    tscDebug("0x%" PRIx64 " %s policy, empty node list", pRequest->requestId, policy);
×
616
    goto _return;
×
617
  }
618

619
  void* pData = taosArrayGet(pMnodeList, 0);
25,662✔
620
  if (NULL == pData) {
25,659!
621
    taosArrayDestroy(nodeList);
×
622
    return TSDB_CODE_OUT_OF_RANGE;
×
623
  }
624
  if (NULL == taosArrayAddBatch(nodeList, pData, mnodeNum)) {
25,659!
625
    taosArrayDestroy(nodeList);
×
626
    return terrno;
×
627
  }
628

629
  tscDebug("0x%" PRIx64 " %s policy, use mnode list, num:%d", pRequest->requestId, policy, mnodeNum);
25,660✔
630

631
_return:
21,623✔
632

633
  *pNodeList = nodeList;
750,982✔
634

635
  return TSDB_CODE_SUCCESS;
750,982✔
636
}
637

638
int32_t buildQnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SArray* pQnodeList) {
294,029✔
639
  SArray* nodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
294,029✔
640
  if (NULL == nodeList) {
294,029!
641
    return terrno;
×
642
  }
643

644
  int32_t qNodeNum = taosArrayGetSize(pQnodeList);
294,029✔
645
  if (qNodeNum > 0) {
294,029✔
646
    void* pData = taosArrayGet(pQnodeList, 0);
293,707✔
647
    if (NULL == pData) {
293,707!
648
      taosArrayDestroy(nodeList);
×
649
      return TSDB_CODE_OUT_OF_RANGE;
×
650
    }
651
    if (NULL == taosArrayAddBatch(nodeList, pData, qNodeNum)) {
293,707!
652
      taosArrayDestroy(nodeList);
×
653
      return terrno;
×
654
    }
655
    tscDebug("0x%" PRIx64 " qnode policy, use qnode list, num:%d", pRequest->requestId, qNodeNum);
293,707✔
656
    goto _return;
293,707✔
657
  }
658

659
  int32_t mnodeNum = taosArrayGetSize(pMnodeList);
322✔
660
  if (mnodeNum <= 0) {
322✔
661
    tscDebug("0x%" PRIx64 " qnode policy, empty node list", pRequest->requestId);
4!
662
    goto _return;
4✔
663
  }
664

665
  void* pData = taosArrayGet(pMnodeList, 0);
318✔
666
  if (NULL == pData) {
318!
667
    taosArrayDestroy(nodeList);
×
668
    return TSDB_CODE_OUT_OF_RANGE;
×
669
  }
670
  if (NULL == taosArrayAddBatch(nodeList, pData, mnodeNum)) {
318!
671
    taosArrayDestroy(nodeList);
×
672
    return terrno;
×
673
  }
674

675
  tscDebug("0x%" PRIx64 " qnode policy, use mnode list, num:%d", pRequest->requestId, mnodeNum);
318!
676

677
_return:
×
678

679
  *pNodeList = nodeList;
294,029✔
680

681
  return TSDB_CODE_SUCCESS;
294,029✔
682
}
683

684
void freeVgList(void* list) {
43,687✔
685
  SArray* pList = *(SArray**)list;
43,687✔
686
  taosArrayDestroy(pList);
43,687✔
687
}
43,741✔
688

689
int32_t buildAsyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SMetaData* pResultMeta) {
980,891✔
690
  SArray* pDbVgList = NULL;
980,891✔
691
  SArray* pQnodeList = NULL;
980,891✔
692
  FDelete fp = NULL;
980,891✔
693
  int32_t code = 0;
980,891✔
694

695
  switch (tsQueryPolicy) {
980,891!
696
    case QUERY_POLICY_VNODE:
686,893✔
697
    case QUERY_POLICY_CLIENT: {
698
      if (pResultMeta) {
686,893!
699
        pDbVgList = taosArrayInit(4, POINTER_BYTES);
686,923✔
700
        if (NULL == pDbVgList) {
686,960!
701
          code = terrno;
×
702
          goto _return;
×
703
        }
704
        int32_t dbNum = taosArrayGetSize(pResultMeta->pDbVgroup);
686,960✔
705
        for (int32_t i = 0; i < dbNum; ++i) {
1,369,922✔
706
          SMetaRes* pRes = taosArrayGet(pResultMeta->pDbVgroup, i);
683,015✔
707
          if (pRes->code || NULL == pRes->pRes) {
682,998!
708
            continue;
×
709
          }
710

711
          if (NULL == taosArrayPush(pDbVgList, &pRes->pRes)) {
1,366,068!
712
            code = terrno;
×
713
            goto _return;
×
714
          }
715
        }
716
      } else {
717
        fp = freeVgList;
×
718

719
        int32_t dbNum = taosArrayGetSize(pRequest->dbList);
×
720
        if (dbNum > 0) {
×
721
          SCatalog*     pCtg = NULL;
×
722
          SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
×
723
          code = catalogGetHandle(pInst->clusterId, &pCtg);
×
724
          if (code != TSDB_CODE_SUCCESS) {
×
725
            goto _return;
×
726
          }
727

728
          pDbVgList = taosArrayInit(dbNum, POINTER_BYTES);
×
729
          if (NULL == pDbVgList) {
×
730
            code = terrno;
×
731
            goto _return;
×
732
          }
733
          SArray* pVgList = NULL;
×
734
          for (int32_t i = 0; i < dbNum; ++i) {
×
735
            char*            dbFName = taosArrayGet(pRequest->dbList, i);
×
736
            SRequestConnInfo conn = {.pTrans = pInst->pTransporter,
×
737
                                     .requestId = pRequest->requestId,
×
738
                                     .requestObjRefId = pRequest->self,
×
739
                                     .mgmtEps = getEpSet_s(&pInst->mgmtEp)};
×
740

741
            // catalogGetDBVgList will handle dbFName == null.
742
            code = catalogGetDBVgList(pCtg, &conn, dbFName, &pVgList);
×
743
            if (code) {
×
744
              goto _return;
×
745
            }
746

747
            if (NULL == taosArrayPush(pDbVgList, &pVgList)) {
×
748
              code = terrno;
×
749
              goto _return;
×
750
            }
751
          }
752
        }
753
      }
754

755
      code = buildVnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pDbVgList);
686,907✔
756
      break;
686,962✔
757
    }
758
    case QUERY_POLICY_HYBRID:
294,025✔
759
    case QUERY_POLICY_QNODE: {
760
      if (pResultMeta && taosArrayGetSize(pResultMeta->pQnodeList) > 0) {
299,921✔
761
        SMetaRes* pRes = taosArrayGet(pResultMeta->pQnodeList, 0);
5,896✔
762
        if (pRes->code) {
5,896!
763
          pQnodeList = NULL;
×
764
        } else {
765
          pQnodeList = taosArrayDup((SArray*)pRes->pRes, NULL);
5,896✔
766
          if (NULL == pQnodeList) {
5,896!
767
            code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
768
            goto _return;
×
769
          }
770
        }
771
      } else {
772
        SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
288,130✔
773
        TSC_ERR_JRET(taosThreadMutexLock(&pInst->qnodeMutex));
288,130!
774
        if (pInst->pQnodeList) {
288,133!
775
          pQnodeList = taosArrayDup(pInst->pQnodeList, NULL);
288,133✔
776
          if (NULL == pQnodeList) {
288,133!
777
            code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
778
            goto _return;
×
779
          }
780
        }
781
        TSC_ERR_JRET(taosThreadMutexUnlock(&pInst->qnodeMutex));
288,133!
782
      }
783

784
      code = buildQnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pQnodeList);
294,029✔
785
      break;
294,029✔
786
    }
787
    default:
×
788
      tscError("unknown query policy: %d", tsQueryPolicy);
×
789
      return TSDB_CODE_APP_ERROR;
×
790
  }
791

792
_return:
980,991✔
793
  taosArrayDestroyEx(pDbVgList, fp);
980,991✔
794
  taosArrayDestroy(pQnodeList);
980,995✔
795

796
  return code;
981,005✔
797
}
798

799
int32_t buildSyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList) {
63,944✔
800
  SArray* pDbVgList = NULL;
63,944✔
801
  SArray* pQnodeList = NULL;
63,944✔
802
  int32_t code = 0;
63,944✔
803

804
  switch (tsQueryPolicy) {
63,944!
805
    case QUERY_POLICY_VNODE:
63,957✔
806
    case QUERY_POLICY_CLIENT: {
807
      int32_t dbNum = taosArrayGetSize(pRequest->dbList);
63,957✔
808
      if (dbNum > 0) {
64,019✔
809
        SCatalog*     pCtg = NULL;
43,713✔
810
        SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
43,713✔
811
        code = catalogGetHandle(pInst->clusterId, &pCtg);
43,713✔
812
        if (code != TSDB_CODE_SUCCESS) {
43,695!
813
          goto _return;
×
814
        }
815

816
        pDbVgList = taosArrayInit(dbNum, POINTER_BYTES);
43,695✔
817
        if (NULL == pDbVgList) {
43,737!
UNCOV
818
          code = terrno;
×
819
          goto _return;
×
820
        }
821
        SArray* pVgList = NULL;
43,739✔
822
        for (int32_t i = 0; i < dbNum; ++i) {
87,471✔
823
          char*            dbFName = taosArrayGet(pRequest->dbList, i);
43,673✔
824
          SRequestConnInfo conn = {.pTrans = pInst->pTransporter,
43,681✔
825
                                   .requestId = pRequest->requestId,
43,681✔
826
                                   .requestObjRefId = pRequest->self,
43,681✔
827
                                   .mgmtEps = getEpSet_s(&pInst->mgmtEp)};
43,681✔
828

829
          // catalogGetDBVgList will handle dbFName == null.
830
          code = catalogGetDBVgList(pCtg, &conn, dbFName, &pVgList);
43,752✔
831
          if (code) {
43,737!
832
            goto _return;
×
833
          }
834

835
          if (NULL == taosArrayPush(pDbVgList, &pVgList)) {
43,732!
836
            code = terrno;
×
837
            goto _return;
×
838
          }
839
        }
840
      }
841

842
      code = buildVnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pDbVgList);
64,104✔
843
      break;
64,016✔
844
    }
845
    case QUERY_POLICY_HYBRID:
×
846
    case QUERY_POLICY_QNODE: {
847
      TSC_ERR_JRET(getQnodeList(pRequest, &pQnodeList));
×
848

849
      code = buildQnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pQnodeList);
×
850
      break;
×
851
    }
852
    default:
×
853
      tscError("unknown query policy: %d", tsQueryPolicy);
×
854
      return TSDB_CODE_APP_ERROR;
×
855
  }
856

857
_return:
64,016✔
858

859
  taosArrayDestroyEx(pDbVgList, freeVgList);
64,016✔
860
  taosArrayDestroy(pQnodeList);
64,013✔
861

862
  return code;
64,027✔
863
}
864

865
int32_t scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList) {
63,967✔
866
  void* pTransporter = pRequest->pTscObj->pAppInfo->pTransporter;
63,967✔
867

868
  SExecResult      res = {0};
63,967✔
869
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
63,967✔
870
                           .requestId = pRequest->requestId,
63,967✔
871
                           .requestObjRefId = pRequest->self};
63,967✔
872
  SSchedulerReq    req = {
127,943✔
873
         .syncReq = true,
874
         .localReq = (tsQueryPolicy == QUERY_POLICY_CLIENT),
63,967✔
875
         .pConn = &conn,
876
         .pNodeList = pNodeList,
877
         .pDag = pDag,
878
         .sql = pRequest->sqlstr,
63,967✔
879
         .startTs = pRequest->metric.start,
63,967✔
880
         .execFp = NULL,
881
         .cbParam = NULL,
882
         .chkKillFp = chkRequestKilled,
883
         .chkKillParam = (void*)pRequest->self,
63,967✔
884
         .pExecRes = &res,
885
         .source = pRequest->source,
63,967✔
886
         .pWorkerCb = getTaskPoolWorkerCb(),
63,967✔
887
  };
888

889
  int32_t code = schedulerExecJob(&req, &pRequest->body.queryJob);
63,976✔
890

891
  destroyQueryExecRes(&pRequest->body.resInfo.execRes);
64,037✔
892
  (void)memcpy(&pRequest->body.resInfo.execRes, &res, sizeof(res));
64,037✔
893

894
  if (code != TSDB_CODE_SUCCESS) {
64,037!
895
    schedulerFreeJob(&pRequest->body.queryJob, 0);
×
896

897
    pRequest->code = code;
×
898
    terrno = code;
×
899
    return pRequest->code;
8✔
900
  }
901

902
  if (TDMT_VND_SUBMIT == pRequest->type || TDMT_VND_DELETE == pRequest->type ||
64,037!
903
      TDMT_VND_CREATE_TABLE == pRequest->type) {
167✔
904
    pRequest->body.resInfo.numOfRows = res.numOfRows;
64,006✔
905
    if (TDMT_VND_SUBMIT == pRequest->type) {
64,006✔
906
      STscObj*            pTscObj = pRequest->pTscObj;
63,883✔
907
      SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
63,883✔
908
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, res.numOfRows);
63,883✔
909
    }
910

911
    schedulerFreeJob(&pRequest->body.queryJob, 0);
64,015✔
912
  }
913

914
  pRequest->code = res.code;
64,057✔
915
  terrno = res.code;
64,057✔
916
  return pRequest->code;
64,035✔
917
}
918

919
int32_t handleSubmitExecRes(SRequestObj* pRequest, void* res, SCatalog* pCatalog, SEpSet* epset) {
9,665,858✔
920
  SArray*      pArray = NULL;
9,665,858✔
921
  SSubmitRsp2* pRsp = (SSubmitRsp2*)res;
9,665,858✔
922
  if (NULL == pRsp->aCreateTbRsp) {
9,665,858✔
923
    return TSDB_CODE_SUCCESS;
9,603,593✔
924
  }
925

926
  int32_t tbNum = taosArrayGetSize(pRsp->aCreateTbRsp);
62,265✔
927
  for (int32_t i = 0; i < tbNum; ++i) {
137,604✔
928
    SVCreateTbRsp* pTbRsp = (SVCreateTbRsp*)taosArrayGet(pRsp->aCreateTbRsp, i);
71,794✔
929
    if (pTbRsp->pMeta) {
71,790✔
930
      TSC_ERR_RET(handleCreateTbExecRes(pTbRsp->pMeta, pCatalog));
50,595✔
931
    }
932
  }
933

934
  return TSDB_CODE_SUCCESS;
65,810✔
935
}
936

937
int32_t handleQueryExecRes(SRequestObj* pRequest, void* res, SCatalog* pCatalog, SEpSet* epset) {
765,894✔
938
  int32_t code = 0;
765,894✔
939
  SArray* pArray = NULL;
765,894✔
940
  SArray* pTbArray = (SArray*)res;
765,894✔
941
  int32_t tbNum = taosArrayGetSize(pTbArray);
765,894✔
942
  if (tbNum <= 0) {
765,899!
943
    return TSDB_CODE_SUCCESS;
×
944
  }
945

946
  pArray = taosArrayInit(tbNum, sizeof(STbSVersion));
765,899✔
947
  if (NULL == pArray) {
765,909!
UNCOV
948
    return terrno;
×
949
  }
950

951
  for (int32_t i = 0; i < tbNum; ++i) {
2,164,049✔
952
    STbVerInfo* tbInfo = taosArrayGet(pTbArray, i);
1,398,141✔
953
    if (NULL == tbInfo) {
1,398,134!
954
      code = terrno;
×
955
      goto _return;
×
956
    }
957
    STbSVersion tbSver = {.tbFName = tbInfo->tbFName, .sver = tbInfo->sversion, .tver = tbInfo->tversion, .rver = tbInfo->rversion};
1,398,134✔
958
    if (NULL == taosArrayPush(pArray, &tbSver)) {
1,398,140!
959
      code = terrno;
×
960
      goto _return;
×
961
    }
962
  }
963

964
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
765,908✔
965
                           .requestId = pRequest->requestId,
765,908✔
966
                           .requestObjRefId = pRequest->self,
765,908✔
967
                           .mgmtEps = *epset};
968

969
  code = catalogChkTbMetaVersion(pCatalog, &conn, pArray);
765,908✔
970

971
_return:
765,901✔
972

973
  taosArrayDestroy(pArray);
765,901✔
974
  return code;
765,910✔
975
}
976

977
int32_t handleAlterTbExecRes(void* res, SCatalog* pCatalog) {
3,978✔
978
  return catalogUpdateTableMeta(pCatalog, (STableMetaRsp*)res);
3,978✔
979
}
980

981
int32_t handleCreateTbExecRes(void* res, SCatalog* pCatalog) {
196,980✔
982
  return catalogAsyncUpdateTableMeta(pCatalog, (STableMetaRsp*)res);
196,980✔
983
}
984

985
int32_t handleQueryExecRsp(SRequestObj* pRequest) {
10,738,900✔
986
  if (NULL == pRequest->body.resInfo.execRes.res) {
10,738,900✔
987
    return pRequest->code;
228,613✔
988
  }
989

990
  SCatalog*     pCatalog = NULL;
10,510,287✔
991
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
10,510,287✔
992

993
  int32_t code = catalogGetHandle(pAppInfo->clusterId, &pCatalog);
10,510,720✔
994
  if (code) {
10,522,825!
995
    return code;
×
996
  }
997

998
  SEpSet       epset = getEpSet_s(&pAppInfo->mgmtEp);
10,522,825✔
999
  SExecResult* pRes = &pRequest->body.resInfo.execRes;
10,543,661✔
1000

1001
  switch (pRes->msgType) {
10,543,661!
1002
    case TDMT_VND_ALTER_TABLE:
741✔
1003
    case TDMT_MND_ALTER_STB: {
1004
      code = handleAlterTbExecRes(pRes->res, pCatalog);
741✔
1005
      break;
741✔
1006
    }
1007
    case TDMT_VND_CREATE_TABLE: {
106,554✔
1008
      SArray* pList = (SArray*)pRes->res;
106,554✔
1009
      int32_t num = taosArrayGetSize(pList);
106,554✔
1010
      for (int32_t i = 0; i < num; ++i) {
246,644✔
1011
        void* res = taosArrayGetP(pList, i);
140,092✔
1012
        // handleCreateTbExecRes will handle res == null
1013
        code = handleCreateTbExecRes(res, pCatalog);
140,080✔
1014
      }
1015
      break;
106,552✔
1016
    }
1017
    case TDMT_MND_CREATE_STB: {
458✔
1018
      code = handleCreateTbExecRes(pRes->res, pCatalog);
458✔
1019
      break;
458✔
1020
    }
1021
    case TDMT_VND_SUBMIT: {
9,672,372✔
1022
      (void)atomic_add_fetch_64((int64_t*)&pAppInfo->summary.insertBytes, pRes->numOfBytes);
9,672,372✔
1023

1024
      code = handleSubmitExecRes(pRequest, pRes->res, pCatalog, &epset);
9,676,269✔
1025
      break;
9,664,755✔
1026
    }
1027
    case TDMT_SCH_QUERY:
765,901✔
1028
    case TDMT_SCH_MERGE_QUERY: {
1029
      code = handleQueryExecRes(pRequest, pRes->res, pCatalog, &epset);
765,901✔
1030
      break;
765,905✔
1031
    }
UNCOV
1032
    default:
×
UNCOV
1033
      tscError("req:0x%" PRIx64 ", invalid exec result for request type:%d, QID:0x%" PRIx64, pRequest->self, pRequest->type,
×
1034
               pRequest->requestId);
1035
      code = TSDB_CODE_APP_ERROR;
×
1036
  }
1037

1038
  return code;
10,538,411✔
1039
}
1040

1041
static bool incompletaFileParsing(SNode* pStmt) {
10,706,547✔
1042
  return QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pStmt) ? false : ((SVnodeModifyOpStmt*)pStmt)->fileProcessing;
10,706,547✔
1043
}
1044

1045
void continuePostSubQuery(SRequestObj* pRequest, SSDataBlock* pBlock) {
437✔
1046
  SSqlCallbackWrapper* pWrapper = pRequest->pWrapper;
437✔
1047

1048
  int32_t code = nodesAcquireAllocator(pWrapper->pParseCtx->allocatorId);
437✔
1049
  if (TSDB_CODE_SUCCESS == code) {
437!
1050
    int64_t analyseStart = taosGetTimestampUs();
437✔
1051
    code = qContinueParsePostQuery(pWrapper->pParseCtx, pRequest->pQuery, pBlock);
437✔
1052
    pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
437✔
1053
  }
1054

1055
  if (TSDB_CODE_SUCCESS == code) {
437!
1056
    code = qContinuePlanPostQuery(pRequest->pPostPlan);
437✔
1057
  }
1058

1059
  code = nodesReleaseAllocator(pWrapper->pParseCtx->allocatorId);
437✔
1060
  handleQueryAnslyseRes(pWrapper, NULL, code);
437✔
1061
}
437✔
1062

1063
void returnToUser(SRequestObj* pRequest) {
66,800✔
1064
  if (pRequest->relation.userRefId == pRequest->self || 0 == pRequest->relation.userRefId) {
66,800!
1065
    // return to client
1066
    doRequestCallback(pRequest, pRequest->code);
66,799✔
1067
    return;
66,799✔
1068
  }
1069

1070
  SRequestObj* pUserReq = acquireRequest(pRequest->relation.userRefId);
1✔
1071
  if (pUserReq) {
1!
1072
    pUserReq->code = pRequest->code;
1✔
1073
    // return to client
1074
    doRequestCallback(pUserReq, pUserReq->code);
1✔
1075
    (void)releaseRequest(pRequest->relation.userRefId);
1✔
1076
    return;
1✔
1077
  } else {
1078
    tscError("req:0x%" PRIx64 ", user ref 0x%" PRIx64 " is not there, QID:0x%" PRIx64, pRequest->self,
×
1079
             pRequest->relation.userRefId, pRequest->requestId);
1080
  }
1081
}
1082

1083
static int32_t createResultBlock(TAOS_RES* pRes, int32_t numOfRows, SSDataBlock** pBlock) {
437✔
1084
  int64_t     lastTs = 0;
437✔
1085
  TAOS_FIELD* pResFields = taos_fetch_fields(pRes);
437✔
1086
  int32_t     numOfFields = taos_num_fields(pRes);
437✔
1087

1088
  int32_t code = createDataBlock(pBlock);
437✔
1089
  if (code) {
437!
1090
    return code;
×
1091
  }
1092

1093
  for (int32_t i = 0; i < numOfFields; ++i) {
1,748✔
1094
    SColumnInfoData colInfoData = createColumnInfoData(pResFields[i].type, pResFields[i].bytes, i + 1);
1,311✔
1095
    code = blockDataAppendColInfo(*pBlock, &colInfoData);
1,311✔
1096
    if (TSDB_CODE_SUCCESS != code) {
1,311!
1097
      blockDataDestroy(*pBlock);
×
1098
      return code;
×
1099
    }
1100
  }
1101

1102
  code = blockDataEnsureCapacity(*pBlock, numOfRows);
437✔
1103
  if (TSDB_CODE_SUCCESS != code) {
437!
1104
    blockDataDestroy(*pBlock);
×
1105
    return code;
×
1106
  }
1107

1108
  for (int32_t i = 0; i < numOfRows; ++i) {
1,327✔
1109
    TAOS_ROW pRow = taos_fetch_row(pRes);
890✔
1110
    if (NULL == pRow[0] || NULL == pRow[1] || NULL == pRow[2]) {
890!
1111
      tscError("invalid data from vnode");
×
1112
      blockDataDestroy(*pBlock);
×
1113
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
1114
    }
1115
    int64_t ts = *(int64_t*)pRow[0];
890✔
1116
    if (lastTs < ts) {
890✔
1117
      lastTs = ts;
508✔
1118
    }
1119

1120
    for (int32_t j = 0; j < numOfFields; ++j) {
3,560✔
1121
      SColumnInfoData* pColInfoData = taosArrayGet((*pBlock)->pDataBlock, j);
2,670✔
1122
      code = colDataSetVal(pColInfoData, i, pRow[j], false);
2,670✔
1123
      if (TSDB_CODE_SUCCESS != code) {
2,670!
1124
        blockDataDestroy(*pBlock);
×
1125
        return code;
×
1126
      }
1127
    }
1128

1129
    tscInfo("[create stream with histroy] lastKey:%" PRId64 " vgId:%d, vgVer:%" PRId64, ts, *(int32_t*)pRow[1], *(int64_t*)pRow[2]);
890!
1130
  }
1131

1132
  (*pBlock)->info.window.ekey = lastTs;
437✔
1133
  (*pBlock)->info.rows = numOfRows;
437✔
1134

1135
  tscInfo("[create stream with histroy] lastKey:%" PRId64 " numOfRows:%d from all vgroups", lastTs, numOfRows);
437!
1136
  return TSDB_CODE_SUCCESS;
437✔
1137
}
1138

1139
void postSubQueryFetchCb(void* param, TAOS_RES* res, int32_t rowNum) {
438✔
1140
  SRequestObj* pRequest = (SRequestObj*)res;
438✔
1141
  if (pRequest->code) {
438✔
1142
    returnToUser(pRequest);
1✔
1143
    return;
1✔
1144
  }
1145

1146
  SSDataBlock* pBlock = NULL;
437✔
1147
  pRequest->code = createResultBlock(res, rowNum, &pBlock);
437✔
1148
  if (TSDB_CODE_SUCCESS != pRequest->code) {
437!
1149
    tscError("req:0x%" PRIx64 ", create result block failed, QID:0x%" PRIx64 " %s", pRequest->self, pRequest->requestId,
×
1150
             tstrerror(pRequest->code));
1151
    returnToUser(pRequest);
×
1152
    return;
×
1153
  }
1154

1155
  SRequestObj* pNextReq = acquireRequest(pRequest->relation.nextRefId);
437✔
1156
  if (pNextReq) {
437!
1157
    continuePostSubQuery(pNextReq, pBlock);
437✔
1158
    (void)releaseRequest(pRequest->relation.nextRefId);
437✔
1159
  } else {
1160
    tscError("req:0x%" PRIx64 ", next req ref 0x%" PRIx64 " is not there, QID:0x%" PRIx64, pRequest->self,
×
1161
             pRequest->relation.nextRefId, pRequest->requestId);
1162
  }
1163

1164
  blockDataDestroy(pBlock);
437✔
1165
}
1166

1167
void handlePostSubQuery(SSqlCallbackWrapper* pWrapper) {
438✔
1168
  SRequestObj* pRequest = pWrapper->pRequest;
438✔
1169
  if (TD_RES_QUERY(pRequest)) {
438!
1170
    taosAsyncFetchImpl(pRequest, postSubQueryFetchCb, pWrapper);
438✔
1171
    return;
438✔
1172
  }
1173

1174
  SRequestObj* pNextReq = acquireRequest(pRequest->relation.nextRefId);
×
1175
  if (pNextReq) {
×
1176
    continuePostSubQuery(pNextReq, NULL);
×
1177
    (void)releaseRequest(pRequest->relation.nextRefId);
×
1178
  } else {
1179
    tscError("req:0x%" PRIx64 ", next req ref 0x%" PRIx64 " is not there, QID:0x%" PRIx64, pRequest->self,
×
1180
             pRequest->relation.nextRefId, pRequest->requestId);
1181
  }
1182
}
1183

1184
// todo refacto the error code  mgmt
1185
void schedulerExecCb(SExecResult* pResult, void* param, int32_t code) {
10,650,994✔
1186
  SSqlCallbackWrapper* pWrapper = param;
10,650,994✔
1187
  SRequestObj*         pRequest = pWrapper->pRequest;
10,650,994✔
1188
  STscObj*             pTscObj = pRequest->pTscObj;
10,650,994✔
1189

1190
  pRequest->code = code;
10,650,994✔
1191
  if (pResult) {
10,650,994✔
1192
    destroyQueryExecRes(&pRequest->body.resInfo.execRes);
10,647,941✔
1193
    (void)memcpy(&pRequest->body.resInfo.execRes, pResult, sizeof(*pResult));
10,659,723✔
1194
  }
1195

1196
  int32_t type = pRequest->type;
10,662,776✔
1197
  if (TDMT_VND_SUBMIT == type || TDMT_VND_DELETE == type || TDMT_VND_CREATE_TABLE == type) {
10,662,776✔
1198
    if (pResult) {
9,756,238!
1199
      pRequest->body.resInfo.numOfRows += pResult->numOfRows;
9,756,541✔
1200

1201
      // record the insert rows
1202
      if (TDMT_VND_SUBMIT == type) {
9,756,541✔
1203
        SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
9,566,227✔
1204
        (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, pResult->numOfRows);
9,566,227✔
1205
      }
1206
    }
1207
    schedulerFreeJob(&pRequest->body.queryJob, 0);
9,800,862✔
1208
  }
1209

1210
  taosMemoryFree(pResult);
10,694,932!
1211
  tscDebug("req:0x%" PRIx64 ", enter scheduler exec cb, code:%s, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
10,710,961✔
1212
           pRequest->requestId);
1213

1214
  if (code != TSDB_CODE_SUCCESS && NEED_CLIENT_HANDLE_ERROR(code) && pRequest->sqlstr != NULL) {
10,690,915!
1215
    tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%s, tryCount:%d, QID:0x%" PRIx64, pRequest->self,
269✔
1216
             tstrerror(code), pRequest->retry, pRequest->requestId);
1217
    if (TSDB_CODE_SUCCESS != removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type))) {
269!
1218
      tscError("req:0x%" PRIx64 ", remove meta failed, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
×
1219
    }
1220
    restartAsyncQuery(pRequest, code);
269✔
1221
    return;
269✔
1222
  }
1223

1224
  tscTrace("req:0x%" PRIx64 ", scheduler exec cb, request type:%s", pRequest->self, TMSG_INFO(pRequest->type));
10,690,646!
1225
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) {
10,690,646!
1226
    if (TSDB_CODE_SUCCESS != removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type))) {
12,061!
1227
      tscError("req:0x%" PRIx64 ", remove meta failed, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
×
1228
    }
1229
  }
1230

1231
  pRequest->metric.execCostUs = taosGetTimestampUs() - pRequest->metric.execStart;
10,682,731✔
1232
  int32_t code1 = handleQueryExecRsp(pRequest);
10,682,731✔
1233
  if (pRequest->code == TSDB_CODE_SUCCESS && pRequest->code != code1) {
10,701,594!
1234
    pRequest->code = code1;
×
1235
  }
1236

1237
  if (pRequest->code == TSDB_CODE_SUCCESS && NULL != pRequest->pQuery &&
21,407,809!
1238
      incompletaFileParsing(pRequest->pQuery->pRoot)) {
10,706,567✔
1239
    continueInsertFromCsv(pWrapper, pRequest);
×
1240
    return;
2✔
1241
  }
1242

1243
  if (pRequest->relation.nextRefId) {
10,707,982✔
1244
    handlePostSubQuery(pWrapper);
438✔
1245
  } else {
1246
    destorySqlCallbackWrapper(pWrapper);
10,707,544✔
1247
    pRequest->pWrapper = NULL;
10,712,680✔
1248

1249
    // return to client
1250
    doRequestCallback(pRequest, code);
10,712,680✔
1251
  }
1252
}
1253

1254
void launchQueryImpl(SRequestObj* pRequest, SQuery* pQuery, bool keepQuery, void** res) {
64,659✔
1255
  int32_t code = 0;
64,659✔
1256
  int32_t subplanNum = 0;
64,659✔
1257

1258
  if (pQuery->pRoot) {
64,659✔
1259
    pRequest->stmtType = pQuery->pRoot->type;
64,005✔
1260
  }
1261

1262
  if (pQuery->pRoot && !pRequest->inRetry) {
64,659!
1263
    STscObj*            pTscObj = pRequest->pTscObj;
64,012✔
1264
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
64,012✔
1265
    if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type) {
64,012!
1266
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1);
64,016✔
UNCOV
1267
    } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) {
×
1268
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1);
3✔
1269
    }
1270
  }
1271

1272
  pRequest->body.execMode = pQuery->execMode;
64,710✔
1273
  switch (pQuery->execMode) {
64,710!
1274
    case QUERY_EXEC_MODE_LOCAL:
×
1275
      if (!pRequest->validateOnly) {
×
1276
        if (NULL == pQuery->pRoot) {
×
1277
          terrno = TSDB_CODE_INVALID_PARA;
×
1278
          code = terrno;
×
1279
        } else {
1280
          code = execLocalCmd(pRequest, pQuery);
×
1281
        }
1282
      }
1283
      break;
×
1284
    case QUERY_EXEC_MODE_RPC:
673✔
1285
      if (!pRequest->validateOnly) {
673!
1286
        code = execDdlQuery(pRequest, pQuery);
674✔
1287
      }
1288
      break;
678✔
1289
    case QUERY_EXEC_MODE_SCHEDULE: {
64,037✔
1290
      SArray* pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
64,037✔
1291
      if (NULL == pMnodeList) {
64,004!
1292
        code = terrno;
×
1293
        break;
×
1294
      }
1295
      SQueryPlan* pDag = NULL;
64,004✔
1296
      code = getPlan(pRequest, pQuery, &pDag, pMnodeList);
64,004✔
1297
      if (TSDB_CODE_SUCCESS == code) {
63,997!
1298
        pRequest->body.subplanNum = pDag->numOfSubplans;
64,008✔
1299
        if (!pRequest->validateOnly) {
64,008!
1300
          SArray* pNodeList = NULL;
64,019✔
1301
          code = buildSyncExecNodeList(pRequest, &pNodeList, pMnodeList);
64,019✔
1302
          if (TSDB_CODE_SUCCESS == code) {
64,012!
1303
            code = scheduleQuery(pRequest, pDag, pNodeList);
64,029✔
1304
          }
1305
          taosArrayDestroy(pNodeList);
64,011✔
1306
        }
1307
      }
1308
      taosArrayDestroy(pMnodeList);
64,031✔
1309
      break;
64,060✔
1310
    }
1311
    case QUERY_EXEC_MODE_EMPTY_RESULT:
×
1312
      pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
×
1313
      break;
×
1314
    default:
×
1315
      break;
×
1316
  }
1317

1318
  if (!keepQuery) {
64,738!
1319
    qDestroyQuery(pQuery);
×
1320
  }
1321

1322
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) {
64,738!
1323
    int ret = removeMeta(pRequest->pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type));
223!
1324
    if (TSDB_CODE_SUCCESS != ret) {
223!
1325
      tscError("req:0x%" PRIx64 ", remove meta failed,code:%d, QID:0x%" PRIx64, pRequest->self, ret, pRequest->requestId);
×
1326
    }
1327
  }
1328

1329
  if (TSDB_CODE_SUCCESS == code) {
64,738✔
1330
    code = handleQueryExecRsp(pRequest);
64,729✔
1331
  }
1332

1333
  if (TSDB_CODE_SUCCESS != code) {
64,743✔
1334
    pRequest->code = code;
137✔
1335
  }
1336

1337
  if (res) {
64,743!
1338
    *res = pRequest->body.resInfo.execRes.res;
×
1339
    pRequest->body.resInfo.execRes.res = NULL;
×
1340
  }
1341
}
64,743✔
1342

1343
static int32_t asyncExecSchQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResultMeta,
10,689,121✔
1344
                                 SSqlCallbackWrapper* pWrapper) {
1345
  int32_t code = TSDB_CODE_SUCCESS;
10,689,121✔
1346
  pRequest->type = pQuery->msgType;
10,689,121✔
1347
  SArray*     pMnodeList = NULL;
10,689,121✔
1348
  SQueryPlan* pDag = NULL;
10,689,121✔
1349
  int64_t     st = taosGetTimestampUs();
10,678,835✔
1350

1351
  if (!pRequest->parseOnly) {
10,678,835!
1352
    pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
10,682,925✔
1353
    if (NULL == pMnodeList) {
10,680,189!
1354
      code = terrno;
×
1355
    }
1356
    SPlanContext cxt = {.queryId = pRequest->requestId,
21,360,496✔
1357
                        .acctId = pRequest->pTscObj->acctId,
10,680,189✔
1358
                        .mgmtEpSet = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp),
10,680,189✔
1359
                        .pAstRoot = pQuery->pRoot,
10,680,307✔
1360
                        .showRewrite = pQuery->showRewrite,
10,680,307✔
1361
                        .isView = pWrapper->pParseCtx->isView,
10,680,307✔
1362
                        .isAudit = pWrapper->pParseCtx->isAudit,
10,680,307✔
1363
                        .pMsg = pRequest->msgBuf,
10,680,307✔
1364
                        .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1365
                        .pUser = pRequest->pTscObj->user,
10,680,307✔
1366
                        .sysInfo = pRequest->pTscObj->sysInfo,
10,680,307✔
1367
                        .timezone = pRequest->pTscObj->optionInfo.timezone,
10,680,307✔
1368
                        .allocatorId = pRequest->allocatorRefId};
10,680,307✔
1369
    if (TSDB_CODE_SUCCESS == code) {
10,680,307!
1370
      code = qCreateQueryPlan(&cxt, &pDag, pMnodeList);
10,691,101✔
1371
    }
1372
    if (code) {
10,665,881✔
1373
      tscError("req:0x%" PRIx64 ", failed to create query plan, code:%s 0x%" PRIx64, pRequest->self, tstrerror(code),
674!
1374
               pRequest->requestId);
1375
    } else {
1376
      pRequest->body.subplanNum = pDag->numOfSubplans;
10,665,207✔
1377
      TSWAP(pRequest->pPostPlan, pDag->pPostPlan);
10,665,207✔
1378
    }
1379
  }
1380

1381
  pRequest->metric.execStart = taosGetTimestampUs();
10,646,577✔
1382
  pRequest->metric.planCostUs = pRequest->metric.execStart - st;
10,646,577✔
1383

1384
  if (TSDB_CODE_SUCCESS == code && !pRequest->validateOnly) {
21,321,599!
1385
    SArray* pNodeList = NULL;
10,667,546✔
1386
    if (QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pQuery->pRoot)) {
10,667,546✔
1387
      code = buildAsyncExecNodeList(pRequest, &pNodeList, pMnodeList, pResultMeta);
980,989✔
1388
    }
1389

1390
    SRequestConnInfo conn = {.pTrans = getAppInfo(pRequest)->pTransporter,
10,667,563✔
1391
                             .requestId = pRequest->requestId,
10,644,158✔
1392
                             .requestObjRefId = pRequest->self};
10,644,158✔
1393
    SSchedulerReq    req = {
21,277,935✔
1394
           .syncReq = false,
1395
           .localReq = (tsQueryPolicy == QUERY_POLICY_CLIENT),
10,644,158✔
1396
           .pConn = &conn,
1397
           .pNodeList = pNodeList,
1398
           .pDag = pDag,
1399
           .allocatorRefId = pRequest->allocatorRefId,
10,644,158✔
1400
           .sql = pRequest->sqlstr,
10,644,158✔
1401
           .startTs = pRequest->metric.start,
10,644,158✔
1402
           .execFp = schedulerExecCb,
1403
           .cbParam = pWrapper,
1404
           .chkKillFp = chkRequestKilled,
1405
           .chkKillParam = (void*)pRequest->self,
10,644,158✔
1406
           .pExecRes = NULL,
1407
           .source = pRequest->source,
10,644,158✔
1408
           .pWorkerCb = getTaskPoolWorkerCb(),
10,644,158✔
1409
    };
1410
    if (TSDB_CODE_SUCCESS == code) {
10,633,777!
1411
      code = schedulerExecJob(&req, &pRequest->body.queryJob);
10,635,676✔
1412
    }
1413

1414
    taosArrayDestroy(pNodeList);
10,680,992✔
1415
  } else {
1416
    qDestroyQueryPlan(pDag);
×
1417
    tscDebug("req:0x%" PRIx64 ", plan not executed, code:%s 0x%" PRIx64, pRequest->self, tstrerror(code),
895!
1418
             pRequest->requestId);
1419
    destorySqlCallbackWrapper(pWrapper);
895✔
1420
    pRequest->pWrapper = NULL;
895✔
1421
    if (TSDB_CODE_SUCCESS != code) {
895✔
1422
      pRequest->code = terrno;
674✔
1423
    }
1424

1425
    doRequestCallback(pRequest, code);
895✔
1426
  }
1427

1428
  // todo not to be released here
1429
  taosArrayDestroy(pMnodeList);
10,675,917✔
1430

1431
  return code;
10,673,821✔
1432
}
1433

1434
void launchAsyncQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResultMeta, SSqlCallbackWrapper* pWrapper) {
10,798,516✔
1435
  int32_t code = 0;
10,798,516✔
1436

1437
  if (pRequest->parseOnly) {
10,798,516✔
1438
    doRequestCallback(pRequest, 0);
625✔
1439
    return;
625✔
1440
  }
1441

1442
  pRequest->body.execMode = pQuery->execMode;
10,797,891✔
1443
  if (QUERY_EXEC_MODE_SCHEDULE != pRequest->body.execMode) {
10,797,891✔
1444
    destorySqlCallbackWrapper(pWrapper);
168,052✔
1445
    pRequest->pWrapper = NULL;
168,050✔
1446
  }
1447

1448
  if (pQuery->pRoot && !pRequest->inRetry) {
10,797,889!
1449
    STscObj*            pTscObj = pRequest->pTscObj;
10,808,606✔
1450
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
10,808,606✔
1451
    if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type &&
10,808,606✔
1452
        (0 == ((SVnodeModifyOpStmt*)pQuery->pRoot)->sqlNodeType)) {
9,693,685✔
1453
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1);
9,578,031✔
1454
    } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) {
1,230,575✔
1455
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1);
859,542✔
1456
    }
1457
  }
1458

1459
  switch (pQuery->execMode) {
10,876,672!
1460
    case QUERY_EXEC_MODE_LOCAL:
131,271✔
1461
      asyncExecLocalCmd(pRequest, pQuery);
131,271✔
1462
      break;
131,271✔
1463
    case QUERY_EXEC_MODE_RPC:
34,081✔
1464
      code = asyncExecDdlQuery(pRequest, pQuery);
34,081✔
1465
      break;
34,101✔
1466
    case QUERY_EXEC_MODE_SCHEDULE: {
10,708,616✔
1467
      code = asyncExecSchQuery(pRequest, pQuery, pResultMeta, pWrapper);
10,708,616✔
1468
      break;
10,664,980✔
1469
    }
1470
    case QUERY_EXEC_MODE_EMPTY_RESULT:
2,704✔
1471
      pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
2,704✔
1472
      doRequestCallback(pRequest, 0);
2,704✔
1473
      break;
2,704✔
1474
    default:
×
1475
      tscError("req:0x%" PRIx64 ", invalid execMode %d", pRequest->self, pQuery->execMode);
×
1476
      doRequestCallback(pRequest, -1);
×
1477
      break;
×
1478
  }
1479
}
1480

1481
int32_t refreshMeta(STscObj* pTscObj, SRequestObj* pRequest) {
16✔
1482
  SCatalog* pCatalog = NULL;
16✔
1483
  int32_t   code = 0;
16✔
1484
  int32_t   dbNum = taosArrayGetSize(pRequest->dbList);
16✔
1485
  int32_t   tblNum = taosArrayGetSize(pRequest->tableList);
16✔
1486

1487
  if (dbNum <= 0 && tblNum <= 0) {
16!
1488
    return TSDB_CODE_APP_ERROR;
16✔
1489
  }
1490

1491
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
×
1492
  if (code != TSDB_CODE_SUCCESS) {
×
1493
    return code;
×
1494
  }
1495

1496
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
×
1497
                           .requestId = pRequest->requestId,
×
1498
                           .requestObjRefId = pRequest->self,
×
1499
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
×
1500

1501
  for (int32_t i = 0; i < dbNum; ++i) {
×
1502
    char* dbFName = taosArrayGet(pRequest->dbList, i);
×
1503

1504
    // catalogRefreshDBVgInfo will handle dbFName == null.
1505
    code = catalogRefreshDBVgInfo(pCatalog, &conn, dbFName);
×
1506
    if (code != TSDB_CODE_SUCCESS) {
×
1507
      return code;
×
1508
    }
1509
  }
1510

1511
  for (int32_t i = 0; i < tblNum; ++i) {
×
1512
    SName* tableName = taosArrayGet(pRequest->tableList, i);
×
1513

1514
    // catalogRefreshTableMeta will handle tableName == null.
1515
    code = catalogRefreshTableMeta(pCatalog, &conn, tableName, -1);
×
1516
    if (code != TSDB_CODE_SUCCESS) {
×
1517
      return code;
×
1518
    }
1519
  }
1520

1521
  return code;
×
1522
}
1523

1524
int32_t removeMeta(STscObj* pTscObj, SArray* tbList, bool isView) {
14,248✔
1525
  SCatalog* pCatalog = NULL;
14,248✔
1526
  int32_t   tbNum = taosArrayGetSize(tbList);
14,248✔
1527
  int32_t   code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
14,248✔
1528
  if (code != TSDB_CODE_SUCCESS) {
14,248!
1529
    return code;
×
1530
  }
1531

1532
  if (isView) {
14,248✔
1533
    for (int32_t i = 0; i < tbNum; ++i) {
830✔
1534
      SName* pViewName = taosArrayGet(tbList, i);
415✔
1535
      char   dbFName[TSDB_DB_FNAME_LEN];
1536
      if (NULL == pViewName) {
415!
1537
        continue;
×
1538
      }
1539
      (void)tNameGetFullDbName(pViewName, dbFName);
415✔
1540
      TSC_ERR_RET(catalogRemoveViewMeta(pCatalog, dbFName, 0, pViewName->tname, 0));
415!
1541
    }
1542
  } else {
1543
    for (int32_t i = 0; i < tbNum; ++i) {
18,234✔
1544
      SName* pTbName = taosArrayGet(tbList, i);
4,401✔
1545
      TSC_ERR_RET(catalogRemoveTableMeta(pCatalog, pTbName));
4,401!
1546
    }
1547
  }
1548

1549
  return TSDB_CODE_SUCCESS;
14,248✔
1550
}
1551

1552
int32_t initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSet) {
31,816✔
1553
  pEpSet->version = 0;
31,816✔
1554

1555
  // init mnode ip set
1556
  SEpSet* mgmtEpSet = &(pEpSet->epSet);
31,816✔
1557
  mgmtEpSet->numOfEps = 0;
31,816✔
1558
  mgmtEpSet->inUse = 0;
31,816✔
1559

1560
  if (firstEp && firstEp[0] != 0) {
31,816!
1561
    if (strlen(firstEp) >= TSDB_EP_LEN) {
32,113!
1562
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1563
      return -1;
×
1564
    }
1565

1566
    int32_t code = taosGetFqdnPortFromEp(firstEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
32,113✔
1567
    if (code != TSDB_CODE_SUCCESS) {
31,523!
1568
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1569
      return terrno;
×
1570
    }
1571
    uint32_t addr = 0;
31,523✔
1572
    code = taosGetIpv4FromFqdn(mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
31,523✔
1573
    if (code) {
32,178✔
1574
      tscError("failed to resolve firstEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn,
5✔
1575
               tstrerror(TSDB_CODE_TSC_INVALID_FQDN));
1576
      (void)memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps]));
4✔
1577
    } else {
1578
      mgmtEpSet->numOfEps++;
32,173✔
1579
    }
1580
  }
1581

1582
  if (secondEp && secondEp[0] != 0) {
31,880!
1583
    if (strlen(secondEp) >= TSDB_EP_LEN) {
21,608!
1584
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1585
      return terrno;
×
1586
    }
1587

1588
    int32_t code = taosGetFqdnPortFromEp(secondEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
21,608✔
1589
    if (code != TSDB_CODE_SUCCESS) {
21,608!
1590
      return code;
×
1591
    }
1592
    uint32_t addr = 0;
21,608✔
1593
    code = taosGetIpv4FromFqdn(mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
21,608✔
1594
    if (code) {
21,608!
1595
      tscError("failed to resolve secondEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn,
×
1596
               tstrerror(TSDB_CODE_TSC_INVALID_FQDN));
1597
      (void)memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps]));
×
1598
    } else {
1599
      mgmtEpSet->numOfEps++;
21,608✔
1600
    }
1601
  }
1602

1603
  if (mgmtEpSet->numOfEps == 0) {
31,880✔
1604
    terrno = TSDB_CODE_RPC_NETWORK_UNAVAIL;
4✔
1605
    return TSDB_CODE_RPC_NETWORK_UNAVAIL;
4✔
1606
  }
1607

1608
  return 0;
31,876✔
1609
}
1610

1611
int32_t taosConnectImpl(const char* user, const char* auth, const char* db, __taos_async_fn_t fp, void* param,
32,174✔
1612
                        SAppInstInfo* pAppInfo, int connType, STscObj** pTscObj) {
1613
  *pTscObj = NULL;
32,174✔
1614
  int32_t code = createTscObj(user, auth, db, connType, pAppInfo, pTscObj);
32,174✔
1615
  if (TSDB_CODE_SUCCESS != code) {
32,174!
1616
    return code;
×
1617
  }
1618

1619
  SRequestObj* pRequest = NULL;
32,174✔
1620
  code = createRequest((*pTscObj)->id, TDMT_MND_CONNECT, 0, &pRequest);
32,174✔
1621
  if (TSDB_CODE_SUCCESS != code) {
32,168!
1622
    destroyTscObj(*pTscObj);
×
1623
    return code;
×
1624
  }
1625

1626
  pRequest->sqlstr = taosStrdup("taos_connect");
32,168!
1627
  if (pRequest->sqlstr) {
32,164!
1628
    pRequest->sqlLen = strlen(pRequest->sqlstr);
32,164✔
1629
  } else {
1630
    return terrno;
×
1631
  }
1632

1633
  SMsgSendInfo* body = NULL;
32,164✔
1634
  code = buildConnectMsg(pRequest, &body);
32,164✔
1635
  if (TSDB_CODE_SUCCESS != code) {
32,138!
1636
    destroyTscObj(*pTscObj);
×
1637
    return code;
×
1638
  }
1639

1640
  // int64_t transporterId = 0;
1641
  code = asyncSendMsgToServer((*pTscObj)->pAppInfo->pTransporter, &(*pTscObj)->pAppInfo->mgmtEp.epSet, NULL, body);
32,138✔
1642
  if (TSDB_CODE_SUCCESS != code) {
32,172!
1643
    destroyTscObj(*pTscObj);
×
1644
    tscError("failed to send connect msg to server, code:%s", tstrerror(code));
×
1645
    return code;
×
1646
  }
1647
  if (TSDB_CODE_SUCCESS != tsem_wait(&pRequest->body.rspSem)) {
32,172!
1648
    destroyTscObj(*pTscObj);
×
1649
    tscError("failed to wait sem, code:%s", terrstr());
×
1650
    return terrno;
×
1651
  }
1652
  if (pRequest->code != TSDB_CODE_SUCCESS) {
32,171✔
1653
    const char* errorMsg = (code == TSDB_CODE_RPC_FQDN_ERROR) ? taos_errstr(pRequest) : tstrerror(pRequest->code);
113!
1654
    tscError("failed to connect to server, reason: %s", errorMsg);
113!
1655

1656
    terrno = pRequest->code;
113✔
1657
    destroyRequest(pRequest);
113✔
1658
    taos_close_internal(*pTscObj);
113✔
1659
    *pTscObj = NULL;
113✔
1660
    return terrno;
113✔
1661
  } else {
1662
    tscInfo("conn:0x%" PRIx64 ", connection is opening, connId:%u, dnodeConn:%p, QID:0x%" PRIx64, (*pTscObj)->id,
32,058✔
1663
             (*pTscObj)->connId, (*pTscObj)->pAppInfo->pTransporter, pRequest->requestId);
1664
    destroyRequest(pRequest);
32,065✔
1665
  }
1666
  return code;
32,045✔
1667
}
1668

1669
static int32_t buildConnectMsg(SRequestObj* pRequest, SMsgSendInfo** pMsgSendInfo) {
32,162✔
1670
  *pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
32,162!
1671
  if (*pMsgSendInfo == NULL) {
32,169!
1672
    return terrno;
×
1673
  }
1674

1675
  (*pMsgSendInfo)->msgType = TDMT_MND_CONNECT;
32,169✔
1676

1677
  (*pMsgSendInfo)->requestObjRefId = pRequest->self;
32,169✔
1678
  (*pMsgSendInfo)->requestId = pRequest->requestId;
32,169✔
1679
  (*pMsgSendInfo)->fp = getMsgRspHandle((*pMsgSendInfo)->msgType);
32,169✔
1680
  (*pMsgSendInfo)->param = taosMemoryCalloc(1, sizeof(pRequest->self));
32,166!
1681
  if (NULL == (*pMsgSendInfo)->param) {
32,167!
1682
    taosMemoryFree(*pMsgSendInfo);
×
1683
    return terrno;
×
1684
  }
1685

1686
  *(int64_t*)(*pMsgSendInfo)->param = pRequest->self;
32,167✔
1687

1688
  SConnectReq connectReq = {0};
32,167✔
1689
  STscObj*    pObj = pRequest->pTscObj;
32,167✔
1690

1691
  char* db = getDbOfConnection(pObj);
32,167✔
1692
  if (db != NULL) {
32,170✔
1693
    tstrncpy(connectReq.db, db, sizeof(connectReq.db));
1,547✔
1694
  } else if (terrno) {
30,623!
1695
    taosMemoryFree(*pMsgSendInfo);
×
1696
    return terrno;
×
1697
  }
1698
  taosMemoryFreeClear(db);
32,173!
1699

1700
  connectReq.connType = pObj->connType;
32,173✔
1701
  connectReq.pid = appInfo.pid;
32,173✔
1702
  connectReq.startTime = appInfo.startTime;
32,173✔
1703

1704
  tstrncpy(connectReq.app, appInfo.appName, sizeof(connectReq.app));
32,173✔
1705
  tstrncpy(connectReq.user, pObj->user, sizeof(connectReq.user));
32,173✔
1706
  tstrncpy(connectReq.passwd, pObj->pass, sizeof(connectReq.passwd));
32,173✔
1707
  tstrncpy(connectReq.sVer, td_version, sizeof(connectReq.sVer));
32,173✔
1708

1709
  int32_t contLen = tSerializeSConnectReq(NULL, 0, &connectReq);
32,173✔
1710
  void*   pReq = taosMemoryMalloc(contLen);
32,145!
1711
  if (NULL == pReq) {
32,162!
1712
    taosMemoryFree(*pMsgSendInfo);
×
1713
    return terrno;
×
1714
  }
1715

1716
  if (-1 == tSerializeSConnectReq(pReq, contLen, &connectReq)) {
32,162✔
1717
    taosMemoryFree(*pMsgSendInfo);
2!
1718
    taosMemoryFree(pReq);
×
1719
    return terrno;
×
1720
  }
1721

1722
  (*pMsgSendInfo)->msgInfo.len = contLen;
32,136✔
1723
  (*pMsgSendInfo)->msgInfo.pData = pReq;
32,136✔
1724
  return TSDB_CODE_SUCCESS;
32,136✔
1725
}
1726

1727
void updateTargetEpSet(SMsgSendInfo* pSendInfo, STscObj* pTscObj, SRpcMsg* pMsg, SEpSet* pEpSet) {
14,579,562✔
1728
  if (NULL == pEpSet) {
14,579,562✔
1729
    return;
13,147,842✔
1730
  }
1731

1732
  switch (pSendInfo->target.type) {
1,431,720✔
1733
    case TARGET_TYPE_MNODE:
28✔
1734
      if (NULL == pTscObj) {
28!
1735
        tscError("mnode epset changed but not able to update it, msg:%s, reqObjRefId:%" PRIx64,
×
1736
                 TMSG_INFO(pMsg->msgType), pSendInfo->requestObjRefId);
1737
        return;
×
1738
      }
1739

1740
      SEpSet* pOrig = &pTscObj->pAppInfo->mgmtEp.epSet;
28✔
1741
      SEp*    pOrigEp = &pOrig->eps[pOrig->inUse];
28✔
1742
      SEp*    pNewEp = &pEpSet->eps[pEpSet->inUse];
28✔
1743
      tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in client", pOrig->inUse, pOrig->numOfEps,
28!
1744
               pOrigEp->fqdn, pOrigEp->port, pEpSet->inUse, pEpSet->numOfEps, pNewEp->fqdn, pNewEp->port);
1745
      updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, pEpSet);
28✔
1746
      break;
28✔
1747
    case TARGET_TYPE_VNODE: {
803,484✔
1748
      if (NULL == pTscObj) {
803,484✔
1749
        tscError("vnode epset changed but not able to update it, msg:%s, reqObjRefId:%" PRIx64,
1!
1750
                 TMSG_INFO(pMsg->msgType), pSendInfo->requestObjRefId);
1751
        return;
1✔
1752
      }
1753

1754
      SCatalog* pCatalog = NULL;
803,483✔
1755
      int32_t   code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
803,483✔
1756
      if (code != TSDB_CODE_SUCCESS) {
803,472!
1757
        tscError("fail to get catalog handle, clusterId:0x%" PRIx64 ", error:%s", pTscObj->pAppInfo->clusterId,
×
1758
                 tstrerror(code));
1759
        return;
×
1760
      }
1761

1762
      code = catalogUpdateVgEpSet(pCatalog, pSendInfo->target.dbFName, pSendInfo->target.vgId, pEpSet);
803,472✔
1763
      if (code != TSDB_CODE_SUCCESS) {
803,489!
1764
        tscError("fail to update catalog vg epset, clusterId:0x%" PRIx64 ", error:%s", pTscObj->pAppInfo->clusterId,
×
1765
                 tstrerror(code));
1766
        return;
×
1767
      }
1768
      taosMemoryFreeClear(pSendInfo->target.dbFName);
803,492!
1769
      break;
803,485✔
1770
    }
1771
    default:
628,208✔
1772
      tscDebug("epset changed, not updated, msgType %s", TMSG_INFO(pMsg->msgType));
628,208!
1773
      break;
628,959✔
1774
  }
1775
}
1776

1777
int32_t doProcessMsgFromServerImpl(SRpcMsg* pMsg, SEpSet* pEpSet) {
14,588,528✔
1778
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
14,588,528✔
1779
  if (pMsg->info.ahandle == NULL) {
14,588,528✔
1780
    tscError("doProcessMsgFromServer pMsg->info.ahandle == NULL");
451!
1781
    rpcFreeCont(pMsg->pCont);
451✔
1782
    taosMemoryFree(pEpSet);
451!
1783
    return TSDB_CODE_TSC_INTERNAL_ERROR;
451✔
1784
  }
1785

1786
  STscObj* pTscObj = NULL;
14,588,077✔
1787

1788
  STraceId* trace = &pMsg->info.traceId;
14,588,077✔
1789
  char      tbuf[40] = {0};
14,588,077✔
1790
  TRACE_TO_STR(trace, tbuf);
14,588,077!
1791

1792
  tscDebug("QID:%s, process message from server, handle:%p, message:%s, size:%d, code:%s", tbuf, pMsg->info.handle,
14,588,447!
1793
           TMSG_INFO(pMsg->msgType), pMsg->contLen, tstrerror(pMsg->code));
1794

1795
  if (pSendInfo->requestObjRefId != 0) {
14,588,595✔
1796
    SRequestObj* pRequest = (SRequestObj*)taosAcquireRef(clientReqRefPool, pSendInfo->requestObjRefId);
13,063,776✔
1797
    if (pRequest) {
13,058,467✔
1798
      if (pRequest->self != pSendInfo->requestObjRefId) {
13,055,923!
1799
        tscError("doProcessMsgFromServer req:0x%" PRId64 " != pSendInfo->requestObjRefId:0x%" PRId64,
×
1800
                 pRequest->self, pSendInfo->requestObjRefId);
1801

1802
        if (TSDB_CODE_SUCCESS != taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId)) {
×
1803
          tscError("doProcessMsgFromServer taosReleaseRef failed");
×
1804
        }
1805
        rpcFreeCont(pMsg->pCont);
×
1806
        taosMemoryFree(pEpSet);
×
1807
        destroySendMsgInfo(pSendInfo);
×
1808
        return TSDB_CODE_TSC_INTERNAL_ERROR;
×
1809
      }
1810
      pTscObj = pRequest->pTscObj;
13,055,923✔
1811
    }
1812
  }
1813

1814
  updateTargetEpSet(pSendInfo, pTscObj, pMsg, pEpSet);
14,583,286✔
1815

1816
  SDataBuf buf = {.msgType = pMsg->msgType,
14,578,978✔
1817
                  .len = pMsg->contLen,
14,578,978✔
1818
                  .pData = NULL,
1819
                  .handle = pMsg->info.handle,
14,578,978✔
1820
                  .handleRefId = pMsg->info.refId,
14,578,978✔
1821
                  .pEpSet = pEpSet};
1822

1823
  if (pMsg->contLen > 0) {
14,578,978✔
1824
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
14,441,762!
1825
    if (buf.pData == NULL) {
14,441,656!
1826
      pMsg->code = terrno;
×
1827
    } else {
1828
      (void)memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
14,441,656✔
1829
    }
1830
  }
1831

1832
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
14,578,872✔
1833

1834
  if (pTscObj) {
14,572,353✔
1835
    int32_t code = taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId);
13,045,576✔
1836
    if (TSDB_CODE_SUCCESS != code) {
13,052,713!
1837
      tscError("doProcessMsgFromServer taosReleaseRef failed");
×
1838
      terrno = code;
×
1839
      pMsg->code = code;
×
1840
    }
1841
  }
1842

1843
  rpcFreeCont(pMsg->pCont);
14,579,490✔
1844
  destroySendMsgInfo(pSendInfo);
14,583,756✔
1845
  return TSDB_CODE_SUCCESS;
14,584,784✔
1846
}
1847

1848
int32_t doProcessMsgFromServer(void* param) {
14,590,730✔
1849
  AsyncArg* arg = (AsyncArg*)param;
14,590,730✔
1850
  int32_t   code = doProcessMsgFromServerImpl(&arg->msg, arg->pEpset);
14,590,730✔
1851
  taosMemoryFree(arg);
14,584,517!
1852
  return code;
14,587,968✔
1853
}
1854

1855
void processMsgFromServer(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
14,567,376✔
1856
  int32_t code = 0;
14,567,376✔
1857
  SEpSet* tEpSet = NULL;
14,567,376✔
1858

1859
  tscDebug("msg callback, ahandle %p", pMsg->info.ahandle);
14,567,376✔
1860

1861
  if (pEpSet != NULL) {
14,565,406✔
1862
    tEpSet = taosMemoryCalloc(1, sizeof(SEpSet));
1,432,474!
1863
    if (NULL == tEpSet) {
1,432,466!
1864
      code = terrno;
×
1865
      pMsg->code = terrno;
×
1866
      goto _exit;
×
1867
    }
1868
    (void)memcpy((void*)tEpSet, (void*)pEpSet, sizeof(SEpSet));
1,432,466✔
1869
  }
1870

1871
  // pMsg is response msg
1872
  if (pMsg->msgType == TDMT_MND_CONNECT + 1) {
14,565,398✔
1873
    // restore origin code
1874
    if (pMsg->code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
32,150!
1875
      pMsg->code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
×
1876
    } else if (pMsg->code == TSDB_CODE_RPC_SOMENODE_BROKEN_LINK) {
32,150!
1877
      pMsg->code = TSDB_CODE_RPC_BROKEN_LINK;
×
1878
    }
1879
  } else {
1880
    // uniform to one error code: TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED
1881
    if (pMsg->code == TSDB_CODE_RPC_SOMENODE_BROKEN_LINK) {
14,533,248!
1882
      pMsg->code = TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED;
×
1883
    }
1884
  }
1885

1886
  AsyncArg* arg = taosMemoryCalloc(1, sizeof(AsyncArg));
14,565,398!
1887
  if (NULL == arg) {
14,565,392!
1888
    code = terrno;
×
1889
    pMsg->code = code;
×
1890
    goto _exit;
×
1891
  }
1892

1893
  arg->msg = *pMsg;
14,565,392✔
1894
  arg->pEpset = tEpSet;
14,565,392✔
1895

1896
  if ((code = taosAsyncExec(doProcessMsgFromServer, arg, NULL)) != 0) {
14,565,392!
UNCOV
1897
    pMsg->code = code;
×
UNCOV
1898
    taosMemoryFree(arg);
×
1899
    goto _exit;
×
1900
  }
1901
  return;
14,584,339✔
1902
  
1903
_exit:
×
1904
  tscError("failed to sched msg to tsc since %s", tstrerror(code));
×
1905
  code = doProcessMsgFromServerImpl(pMsg, tEpSet);
×
1906
  if (code != 0) {
×
1907
    tscError("failed to sched msg to tsc, tsc ready quit");
×
1908
  }
1909
}
1910

1911
TAOS* taos_connect_auth(const char* ip, const char* user, const char* auth, const char* db, uint16_t port) {
4✔
1912
  tscInfo("try to connect to %s:%u by auth, user:%s db:%s", ip, port, user, db);
4!
1913
  if (user == NULL) {
4!
1914
    user = TSDB_DEFAULT_USER;
×
1915
  }
1916

1917
  if (auth == NULL) {
4!
1918
    tscError("No auth info is given, failed to connect to server");
×
1919
    return NULL;
×
1920
  }
1921

1922
  STscObj* pObj = NULL;
4✔
1923
  int32_t  code = taos_connect_internal(ip, user, NULL, auth, db, port, CONN_TYPE__QUERY, &pObj);
4✔
1924
  if (TSDB_CODE_SUCCESS == code) {
4✔
1925
    int64_t* rid = taosMemoryCalloc(1, sizeof(int64_t));
1!
1926
    if (NULL == rid) {
1!
1927
      tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db);
×
1928
    }
1929
    *rid = pObj->id;
1✔
1930
    return (TAOS*)rid;
1✔
1931
  }
1932

1933
  return NULL;
3✔
1934
}
1935

1936
// TAOS* taos_connect_l(const char* ip, int ipLen, const char* user, int userLen, const char* pass, int passLen,
1937
//                      const char* db, int dbLen, uint16_t port) {
1938
//   char ipStr[TSDB_EP_LEN] = {0};
1939
//   char dbStr[TSDB_DB_NAME_LEN] = {0};
1940
//   char userStr[TSDB_USER_LEN] = {0};
1941
//   char passStr[TSDB_PASSWORD_LEN] = {0};
1942
//
1943
//   tstrncpy(ipStr, ip, TMIN(TSDB_EP_LEN - 1, ipLen));
1944
//   tstrncpy(userStr, user, TMIN(TSDB_USER_LEN - 1, userLen));
1945
//   tstrncpy(passStr, pass, TMIN(TSDB_PASSWORD_LEN - 1, passLen));
1946
//   tstrncpy(dbStr, db, TMIN(TSDB_DB_NAME_LEN - 1, dbLen));
1947
//   return taos_connect(ipStr, userStr, passStr, dbStr, port);
1948
// }
1949

1950
void doSetOneRowPtr(SReqResultInfo* pResultInfo) {
34,948,839✔
1951
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
171,396,058✔
1952
    SResultColumn* pCol = &pResultInfo->pCol[i];
136,460,770✔
1953

1954
    int32_t type = pResultInfo->fields[i].type;
136,460,770✔
1955
    int32_t schemaBytes = calcSchemaBytesFromTypeBytes(type, pResultInfo->userFields[i].bytes, false);
136,460,770✔
1956

1957
    if (IS_VAR_DATA_TYPE(type)) {
136,447,219✔
1958
      if (!IS_VAR_NULL_TYPE(type, schemaBytes) && pCol->offset[pResultInfo->current] != -1) {
26,145,145!
1959
        char* pStart = pResultInfo->pCol[i].offset[pResultInfo->current] + pResultInfo->pCol[i].pData;
24,595,134✔
1960

1961
        pResultInfo->length[i] = varDataLen(pStart);
24,595,134✔
1962
        pResultInfo->row[i] = varDataVal(pStart);
24,595,134✔
1963
      } else {
1964
        pResultInfo->row[i] = NULL;
1,550,011✔
1965
        pResultInfo->length[i] = 0;
1,550,011✔
1966
      }
1967
    } else {
1968
      if (!colDataIsNull_f(pCol->nullbitmap, pResultInfo->current)) {
110,302,074✔
1969
        pResultInfo->row[i] = pResultInfo->pCol[i].pData + schemaBytes * pResultInfo->current;
104,852,590✔
1970
        pResultInfo->length[i] = schemaBytes;
104,852,590✔
1971
      } else {
1972
        pResultInfo->row[i] = NULL;
5,449,484✔
1973
        pResultInfo->length[i] = 0;
5,449,484✔
1974
      }
1975
    }
1976
  }
1977
}
34,935,288✔
1978

1979
void* doFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4) {
×
1980
  if (pRequest == NULL) {
×
1981
    return NULL;
×
1982
  }
1983

1984
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
×
1985
  if (pResultInfo->pData == NULL || pResultInfo->current >= pResultInfo->numOfRows) {
×
1986
    // All data has returned to App already, no need to try again
1987
    if (pResultInfo->completed) {
×
1988
      pResultInfo->numOfRows = 0;
×
1989
      return NULL;
×
1990
    }
1991

1992
    SReqResultInfo* pResInfo = &pRequest->body.resInfo;
×
1993
    SSchedulerReq   req = {.syncReq = true, .pFetchRes = (void**)&pResInfo->pData};
×
1994

1995
    pRequest->code = schedulerFetchRows(pRequest->body.queryJob, &req);
×
1996
    if (pRequest->code != TSDB_CODE_SUCCESS) {
×
1997
      pResultInfo->numOfRows = 0;
×
1998
      return NULL;
×
1999
    }
2000

2001
    pRequest->code =
×
2002
        setQueryResultFromRsp(&pRequest->body.resInfo, (const SRetrieveTableRsp*)pResInfo->pData, convertUcs4, pRequest->isStmtBind);
×
2003
    if (pRequest->code != TSDB_CODE_SUCCESS) {
×
2004
      pResultInfo->numOfRows = 0;
×
2005
      return NULL;
×
2006
    }
2007

2008
    tscDebug("req:0x%" PRIx64 ", fetch results, numOfRows:%" PRId64 " total Rows:%" PRId64 ", complete:%d, QID:0x%" PRIx64,
×
2009
             pRequest->self, pResInfo->numOfRows, pResInfo->totalRows, pResInfo->completed, pRequest->requestId);
2010

2011
    STscObj*            pTscObj = pRequest->pTscObj;
×
2012
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
×
2013
    (void)atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen);
×
2014

2015
    if (pResultInfo->numOfRows == 0) {
×
2016
      return NULL;
×
2017
    }
2018
  }
2019

2020
  if (setupOneRowPtr) {
×
2021
    doSetOneRowPtr(pResultInfo);
×
2022
    pResultInfo->current += 1;
×
2023
  }
2024

2025
  return pResultInfo->row;
×
2026
}
2027

2028
static void syncFetchFn(void* param, TAOS_RES* res, int32_t numOfRows) {
972,952✔
2029
  tsem_t* sem = param;
972,952✔
2030
  if (TSDB_CODE_SUCCESS != tsem_post(sem)) {
972,952!
2031
    tscError("failed to post sem, code:%s", terrstr());
×
2032
  }
2033
}
972,959✔
2034

2035
void* doAsyncFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4) {
20,472,290✔
2036
  if (pRequest == NULL) {
20,472,290!
2037
    return NULL;
×
2038
  }
2039

2040
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
20,472,290✔
2041
  if (pResultInfo->pData == NULL || pResultInfo->current >= pResultInfo->numOfRows) {
20,472,290✔
2042
    // All data has returned to App already, no need to try again
2043
    if (pResultInfo->completed) {
1,646,099✔
2044
      pResultInfo->numOfRows = 0;
673,151✔
2045
      return NULL;
673,151✔
2046
    }
2047

2048
    // convert ucs4 to native multi-bytes string
2049
    pResultInfo->convertUcs4 = convertUcs4;
972,948✔
2050
    tsem_t sem;
2051
    if (TSDB_CODE_SUCCESS != tsem_init(&sem, 0, 0)) {
972,948!
2052
      tscError("failed to init sem, code:%s", terrstr());
×
2053
    }
2054
    taos_fetch_rows_a(pRequest, syncFetchFn, &sem);
972,949✔
2055
    if (TSDB_CODE_SUCCESS != tsem_wait(&sem)) {
972,960!
2056
      tscError("failed to wait sem, code:%s", terrstr());
×
2057
    }
2058
    if (TSDB_CODE_SUCCESS != tsem_destroy(&sem)) {
972,952!
2059
      tscError("failed to destroy sem, code:%s", terrstr());
×
2060
    }
2061
    pRequest->inCallback = false;
972,948✔
2062
  }
2063

2064
  if (pResultInfo->numOfRows == 0 || pRequest->code != TSDB_CODE_SUCCESS) {
19,799,139!
2065
    return NULL;
62,207✔
2066
  } else {
2067
    if (setupOneRowPtr) {
19,736,932✔
2068
      doSetOneRowPtr(pResultInfo);
18,902,003✔
2069
      pResultInfo->current += 1;
18,888,032✔
2070
    }
2071

2072
    return pResultInfo->row;
19,722,961✔
2073
  }
2074
}
2075

2076
static int32_t doPrepareResPtr(SReqResultInfo* pResInfo) {
1,453,362✔
2077
  if (pResInfo->row == NULL) {
1,453,362✔
2078
    pResInfo->row = taosMemoryCalloc(pResInfo->numOfCols, POINTER_BYTES);
1,237,216!
2079
    pResInfo->pCol = taosMemoryCalloc(pResInfo->numOfCols, sizeof(SResultColumn));
1,237,256!
2080
    pResInfo->length = taosMemoryCalloc(pResInfo->numOfCols, sizeof(int32_t));
1,237,251!
2081
    pResInfo->convertBuf = taosMemoryCalloc(pResInfo->numOfCols, POINTER_BYTES);
1,237,253!
2082

2083
    if (pResInfo->row == NULL || pResInfo->pCol == NULL || pResInfo->length == NULL || pResInfo->convertBuf == NULL) {
1,237,256✔
2084
      taosMemoryFree(pResInfo->row);
6!
2085
      taosMemoryFree(pResInfo->pCol);
×
2086
      taosMemoryFree(pResInfo->length);
×
2087
      taosMemoryFree(pResInfo->convertBuf);
×
2088
      return terrno;
×
2089
    }
2090
  }
2091

2092
  return TSDB_CODE_SUCCESS;
1,453,396✔
2093
}
2094

2095
static int32_t doConvertUCS4(SReqResultInfo* pResultInfo, int32_t* colLength, bool isStmt) {
1,453,143✔
2096
  int32_t idx = -1;
1,453,143✔
2097
  iconv_t conv = taosAcquireConv(&idx, C2M, pResultInfo->charsetCxt);
1,453,143✔
2098
  if (conv == (iconv_t)-1) return TSDB_CODE_TSC_INTERNAL_ERROR;
1,453,181!
2099

2100
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
6,151,765✔
2101
    int32_t type = pResultInfo->fields[i].type;
4,698,607✔
2102
    int32_t schemaBytes = calcSchemaBytesFromTypeBytes(pResultInfo->fields[i].type, pResultInfo->fields[i].bytes, isStmt);
4,698,607✔
2103

2104
    if (type == TSDB_DATA_TYPE_NCHAR && colLength[i] > 0) {
4,698,591✔
2105
      char* p = taosMemoryRealloc(pResultInfo->convertBuf[i], colLength[i]);
204,955!
2106
      if (p == NULL) {
204,955!
2107
        taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
×
2108
        return terrno;
×
2109
      }
2110

2111
      pResultInfo->convertBuf[i] = p;
204,955✔
2112

2113
      SResultColumn* pCol = &pResultInfo->pCol[i];
204,955✔
2114
      for (int32_t j = 0; j < pResultInfo->numOfRows; ++j) {
32,528,395✔
2115
        if (pCol->offset[j] != -1) {
32,323,447✔
2116
          char* pStart = pCol->offset[j] + pCol->pData;
29,380,985✔
2117

2118
          int32_t len = taosUcs4ToMbsEx((TdUcs4*)varDataVal(pStart), varDataLen(pStart), varDataVal(p), conv);
29,380,985✔
2119
          if (len < 0 || len > schemaBytes || (p + len) >= (pResultInfo->convertBuf[i] + colLength[i])) {
29,380,959!
2120
            tscError(
×
2121
                "doConvertUCS4 error, invalid data. len:%d, bytes:%d, (p + len):%p, (pResultInfo->convertBuf[i] + "
2122
                "colLength[i]):%p",
2123
                len, schemaBytes, (p + len), (pResultInfo->convertBuf[i] + colLength[i]));
2124
            taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
×
2125
            return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2126
          }
2127

2128
          varDataSetLen(p, len);
29,380,978✔
2129
          pCol->offset[j] = (p - pResultInfo->convertBuf[i]);
29,380,978✔
2130
          p += (len + VARSTR_HEADER_SIZE);
29,380,978✔
2131
        }
2132
      }
2133

2134
      pResultInfo->pCol[i].pData = pResultInfo->convertBuf[i];
204,948✔
2135
      pResultInfo->row[i] = pResultInfo->pCol[i].pData;
204,948✔
2136
    }
2137
  }
2138
  taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
1,453,158✔
2139
  return TSDB_CODE_SUCCESS;
1,453,195✔
2140
}
2141

2142
static int32_t convertDecimalType(SReqResultInfo* pResultInfo) {
1,453,186✔
2143
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
6,152,119✔
2144
    TAOS_FIELD_E* pFieldE = pResultInfo->fields + i;
4,698,933✔
2145
    TAOS_FIELD* pField = pResultInfo->userFields + i;
4,698,933✔
2146
    int32_t type = pFieldE->type;
4,698,933✔
2147
    int32_t bufLen = 0;
4,698,933✔
2148
    char* p = NULL;
4,698,933✔
2149
    if (!IS_DECIMAL_TYPE(type) || !pResultInfo->pCol[i].pData) {
4,698,933!
2150
      continue;
4,690,865✔
2151
    } else {
2152
      bufLen = 64;
8,068✔
2153
      p = taosMemoryRealloc(pResultInfo->convertBuf[i], bufLen * pResultInfo->numOfRows);
8,068!
2154
      pFieldE->bytes = bufLen;
8,068✔
2155
      pField->bytes = bufLen;
8,068✔
2156
    }
2157
    if (!p) return terrno;
8,068!
2158
    pResultInfo->convertBuf[i] = p;
8,068✔
2159

2160
    for (int32_t j = 0; j < pResultInfo->numOfRows; ++j) {
6,500,043✔
2161
      int32_t code = decimalToStr((DecimalWord*)(pResultInfo->pCol[i].pData + j * tDataTypes[type].bytes), type,
6,491,975✔
2162
                                  pFieldE->precision, pFieldE->scale, p, bufLen);
6,491,975✔
2163
      p += bufLen;
6,491,975✔
2164
      if (TSDB_CODE_SUCCESS != code) {
6,491,975!
2165
        return code;
×
2166
      }
2167
    }
2168
    pResultInfo->pCol[i].pData = pResultInfo->convertBuf[i];
8,068✔
2169
    pResultInfo->row[i] = pResultInfo->pCol[i].pData;
8,068✔
2170
  }
2171
  return 0;
1,453,186✔
2172
}
2173

2174
int32_t getVersion1BlockMetaSize(const char* p, int32_t numOfCols) {
892✔
2175
  return sizeof(int32_t) + sizeof(int32_t) + sizeof(int32_t) * 3 + sizeof(uint64_t) +
892✔
2176
         numOfCols * (sizeof(int8_t) + sizeof(int32_t));
2177
}
2178

2179
static int32_t estimateJsonLen(SReqResultInfo* pResultInfo) {
446✔
2180
  char*   p = (char*)pResultInfo->pData;
446✔
2181
  int32_t blockVersion = *(int32_t*)p;
446✔
2182

2183
  int32_t numOfRows = pResultInfo->numOfRows;
446✔
2184
  int32_t numOfCols = pResultInfo->numOfCols;
446✔
2185

2186
  // | version | total length | total rows | total columns | flag seg| block group id | column schema | each column
2187
  // length |
2188
  int32_t cols = *(int32_t*)(p + sizeof(int32_t) * 3);
446✔
2189
  if (numOfCols != cols) {
446!
2190
    tscError("estimateJsonLen error: numOfCols:%d != cols:%d", numOfCols, cols);
×
2191
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2192
  }
2193

2194
  int32_t  len = getVersion1BlockMetaSize(p, numOfCols);
446✔
2195
  int32_t* colLength = (int32_t*)(p + len);
446✔
2196
  len += sizeof(int32_t) * numOfCols;
446✔
2197

2198
  char* pStart = p + len;
446✔
2199
  for (int32_t i = 0; i < numOfCols; ++i) {
2,141✔
2200
    int32_t colLen = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength[i]) : colLength[i];
1,695!
2201

2202
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
1,695✔
2203
      int32_t* offset = (int32_t*)pStart;
523✔
2204
      int32_t  lenTmp = numOfRows * sizeof(int32_t);
523✔
2205
      len += lenTmp;
523✔
2206
      pStart += lenTmp;
523✔
2207

2208
      int32_t estimateColLen = 0;
523✔
2209
      for (int32_t j = 0; j < numOfRows; ++j) {
3,235✔
2210
        if (offset[j] == -1) {
2,712✔
2211
          continue;
211✔
2212
        }
2213
        char* data = offset[j] + pStart;
2,501✔
2214

2215
        int32_t jsonInnerType = *data;
2,501✔
2216
        char*   jsonInnerData = data + CHAR_BYTES;
2,501✔
2217
        if (jsonInnerType == TSDB_DATA_TYPE_NULL) {
2,501✔
2218
          estimateColLen += (VARSTR_HEADER_SIZE + strlen(TSDB_DATA_NULL_STR_L));
72✔
2219
        } else if (tTagIsJson(data)) {
2,429✔
2220
          estimateColLen += (VARSTR_HEADER_SIZE + ((const STag*)(data))->len);
667✔
2221
        } else if (jsonInnerType == TSDB_DATA_TYPE_NCHAR) {  // value -> "value"
1,762✔
2222
          estimateColLen += varDataTLen(jsonInnerData) + CHAR_BYTES * 2;
1,466✔
2223
        } else if (jsonInnerType == TSDB_DATA_TYPE_DOUBLE) {
296✔
2224
          estimateColLen += (VARSTR_HEADER_SIZE + 32);
216✔
2225
        } else if (jsonInnerType == TSDB_DATA_TYPE_BOOL) {
80!
2226
          estimateColLen += (VARSTR_HEADER_SIZE + 5);
80✔
2227
        } else {
2228
          tscError("estimateJsonLen error: invalid type:%d", jsonInnerType);
×
2229
          return -1;
×
2230
        }
2231
      }
2232
      len += TMAX(colLen, estimateColLen);
523✔
2233
    } else if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
1,172!
2234
      int32_t lenTmp = numOfRows * sizeof(int32_t);
396✔
2235
      len += (lenTmp + colLen);
396✔
2236
      pStart += lenTmp;
396✔
2237
    } else {
2238
      int32_t lenTmp = BitmapLen(pResultInfo->numOfRows);
776✔
2239
      len += (lenTmp + colLen);
776✔
2240
      pStart += lenTmp;
776✔
2241
    }
2242
    pStart += colLen;
1,695✔
2243
  }
2244

2245
  // Ensure the complete structure of the block, including the blankfill field,
2246
  // even though it is not used on the client side.
2247
  len += sizeof(bool);
446✔
2248
  return len;
446✔
2249
}
2250

2251
static int32_t doConvertJson(SReqResultInfo* pResultInfo) {
1,453,390✔
2252
  int32_t numOfRows = pResultInfo->numOfRows;
1,453,390✔
2253
  int32_t numOfCols = pResultInfo->numOfCols;
1,453,390✔
2254
  bool    needConvert = false;
1,453,390✔
2255
  for (int32_t i = 0; i < numOfCols; ++i) {
6,152,881✔
2256
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
4,699,937✔
2257
      needConvert = true;
446✔
2258
      break;
446✔
2259
    }
2260
  }
2261

2262
  if (!needConvert) {
1,453,390✔
2263
    return TSDB_CODE_SUCCESS;
1,452,951✔
2264
  }
2265

2266
  tscDebug("start to convert form json format string");
439!
2267

2268
  char*   p = (char*)pResultInfo->pData;
439✔
2269
  int32_t blockVersion = *(int32_t*)p;
439✔
2270
  int32_t dataLen = estimateJsonLen(pResultInfo);
439✔
2271
  if (dataLen <= 0) {
446!
2272
    tscError("doConvertJson error: estimateJsonLen failed");
×
2273
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2274
  }
2275

2276
  taosMemoryFreeClear(pResultInfo->convertJson);
446!
2277
  pResultInfo->convertJson = taosMemoryCalloc(1, dataLen);
446!
2278
  if (pResultInfo->convertJson == NULL) return terrno;
446!
2279
  char* p1 = pResultInfo->convertJson;
446✔
2280

2281
  int32_t totalLen = 0;
446✔
2282
  int32_t cols = *(int32_t*)(p + sizeof(int32_t) * 3);
446✔
2283
  if (numOfCols != cols) {
446!
2284
    tscError("doConvertJson error: numOfCols:%d != cols:%d", numOfCols, cols);
×
2285
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2286
  }
2287

2288
  int32_t len = getVersion1BlockMetaSize(p, numOfCols);
446✔
2289
  (void)memcpy(p1, p, len);
446✔
2290

2291
  p += len;
446✔
2292
  p1 += len;
446✔
2293
  totalLen += len;
446✔
2294

2295
  len = sizeof(int32_t) * numOfCols;
446✔
2296
  int32_t* colLength = (int32_t*)p;
446✔
2297
  int32_t* colLength1 = (int32_t*)p1;
446✔
2298
  (void)memcpy(p1, p, len);
446✔
2299
  p += len;
446✔
2300
  p1 += len;
446✔
2301
  totalLen += len;
446✔
2302

2303
  char* pStart = p;
446✔
2304
  char* pStart1 = p1;
446✔
2305
  for (int32_t i = 0; i < numOfCols; ++i) {
2,141✔
2306
    int32_t colLen = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength[i]) : colLength[i];
1,695!
2307
    int32_t colLen1 = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength1[i]) : colLength1[i];
1,695!
2308
    if (colLen >= dataLen) {
1,695!
2309
      tscError("doConvertJson error: colLen:%d >= dataLen:%d", colLen, dataLen);
×
2310
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2311
    }
2312
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
1,695✔
2313
      int32_t* offset = (int32_t*)pStart;
523✔
2314
      int32_t* offset1 = (int32_t*)pStart1;
523✔
2315
      len = numOfRows * sizeof(int32_t);
523✔
2316
      (void)memcpy(pStart1, pStart, len);
523✔
2317
      pStart += len;
523✔
2318
      pStart1 += len;
523✔
2319
      totalLen += len;
523✔
2320

2321
      len = 0;
523✔
2322
      for (int32_t j = 0; j < numOfRows; ++j) {
3,235✔
2323
        if (offset[j] == -1) {
2,712✔
2324
          continue;
211✔
2325
        }
2326
        char* data = offset[j] + pStart;
2,501✔
2327

2328
        int32_t jsonInnerType = *data;
2,501✔
2329
        char*   jsonInnerData = data + CHAR_BYTES;
2,501✔
2330
        char    dst[TSDB_MAX_JSON_TAG_LEN] = {0};
2,501✔
2331
        if (jsonInnerType == TSDB_DATA_TYPE_NULL) {
2,501✔
2332
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%s", TSDB_DATA_NULL_STR_L);
72✔
2333
          varDataSetLen(dst, strlen(varDataVal(dst)));
72✔
2334
        } else if (tTagIsJson(data)) {
2,429✔
2335
          char* jsonString = NULL;
667✔
2336
          parseTagDatatoJson(data, &jsonString, pResultInfo->charsetCxt);
667✔
2337
          if (jsonString == NULL) {
667!
2338
            tscError("doConvertJson error: parseTagDatatoJson failed");
×
2339
            return terrno;
×
2340
          }
2341
          STR_TO_VARSTR(dst, jsonString);
667✔
2342
          taosMemoryFree(jsonString);
667!
2343
        } else if (jsonInnerType == TSDB_DATA_TYPE_NCHAR) {  // value -> "value"
1,762✔
2344
          *(char*)varDataVal(dst) = '\"';
1,466✔
2345
          int32_t length = taosUcs4ToMbs((TdUcs4*)varDataVal(jsonInnerData), varDataLen(jsonInnerData),
1,466✔
2346
                                         varDataVal(dst) + CHAR_BYTES, pResultInfo->charsetCxt);
2347
          if (length <= 0) {
1,466✔
2348
            tscError("charset:%s to %s. convert failed.", DEFAULT_UNICODE_ENCODEC,
4!
2349
              pResultInfo->charsetCxt != NULL ? ((SConvInfo *)(pResultInfo->charsetCxt))->charset : tsCharset);
2350
            length = 0;
4✔
2351
          }
2352
          varDataSetLen(dst, length + CHAR_BYTES * 2);
1,466✔
2353
          *(char*)POINTER_SHIFT(varDataVal(dst), length + CHAR_BYTES) = '\"';
1,466✔
2354
        } else if (jsonInnerType == TSDB_DATA_TYPE_DOUBLE) {
296✔
2355
          double jsonVd = *(double*)(jsonInnerData);
216✔
2356
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%.9lf", jsonVd);
216✔
2357
          varDataSetLen(dst, strlen(varDataVal(dst)));
216✔
2358
        } else if (jsonInnerType == TSDB_DATA_TYPE_BOOL) {
80!
2359
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%s",
80✔
2360
                         (*((char*)jsonInnerData) == 1) ? "true" : "false");
80✔
2361
          varDataSetLen(dst, strlen(varDataVal(dst)));
80✔
2362
        } else {
2363
          tscError("doConvertJson error: invalid type:%d", jsonInnerType);
×
2364
          return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2365
        }
2366

2367
        offset1[j] = len;
2,501✔
2368
        (void)memcpy(pStart1 + len, dst, varDataTLen(dst));
2,501✔
2369
        len += varDataTLen(dst);
2,501✔
2370
      }
2371
      colLen1 = len;
523✔
2372
      totalLen += colLen1;
523✔
2373
      colLength1[i] = (blockVersion == BLOCK_VERSION_1) ? htonl(len) : len;
523!
2374
    } else if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
1,172!
2375
      len = numOfRows * sizeof(int32_t);
396✔
2376
      (void)memcpy(pStart1, pStart, len);
396✔
2377
      pStart += len;
396✔
2378
      pStart1 += len;
396✔
2379
      totalLen += len;
396✔
2380
      totalLen += colLen;
396✔
2381
      (void)memcpy(pStart1, pStart, colLen);
396✔
2382
    } else {
2383
      len = BitmapLen(pResultInfo->numOfRows);
776✔
2384
      (void)memcpy(pStart1, pStart, len);
776✔
2385
      pStart += len;
776✔
2386
      pStart1 += len;
776✔
2387
      totalLen += len;
776✔
2388
      totalLen += colLen;
776✔
2389
      (void)memcpy(pStart1, pStart, colLen);
776✔
2390
    }
2391
    pStart += colLen;
1,695✔
2392
    pStart1 += colLen1;
1,695✔
2393
  }
2394

2395
  // Ensure the complete structure of the block, including the blankfill field,
2396
  // even though it is not used on the client side.
2397
  // (void)memcpy(pStart1, pStart, sizeof(bool));
2398
  totalLen += sizeof(bool);
446✔
2399

2400
  *(int32_t*)(pResultInfo->convertJson + 4) = totalLen;
446✔
2401
  pResultInfo->pData = pResultInfo->convertJson;
446✔
2402
  return TSDB_CODE_SUCCESS;
446✔
2403
}
2404

2405
int32_t setResultDataPtr(SReqResultInfo* pResultInfo, bool convertUcs4, bool isStmt) {
1,521,780✔
2406
  bool convertForDecimal = convertUcs4;
1,521,780✔
2407
  if (pResultInfo == NULL || pResultInfo->numOfCols <= 0 || pResultInfo->fields == NULL) {
1,521,780!
2408
    tscError("setResultDataPtr paras error");
×
2409
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2410
  }
2411

2412
  if (pResultInfo->numOfRows == 0) {
1,521,788✔
2413
    return TSDB_CODE_SUCCESS;
68,425✔
2414
  }
2415

2416
  if (pResultInfo->pData == NULL) {
1,453,363!
2417
    tscError("setResultDataPtr error: pData is NULL");
×
2418
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2419
  }
2420

2421
  int32_t code = doPrepareResPtr(pResultInfo);
1,453,363✔
2422
  if (code != TSDB_CODE_SUCCESS) {
1,453,398!
2423
    return code;
×
2424
  }
2425
  code = doConvertJson(pResultInfo);
1,453,398✔
2426
  if (code != TSDB_CODE_SUCCESS) {
1,453,387!
2427
    return code;
×
2428
  }
2429

2430
  char* p = (char*)pResultInfo->pData;
1,453,387✔
2431

2432
  // version:
2433
  int32_t blockVersion = *(int32_t*)p;
1,453,387✔
2434
  p += sizeof(int32_t);
1,453,387✔
2435

2436
  int32_t dataLen = *(int32_t*)p;
1,453,387✔
2437
  p += sizeof(int32_t);
1,453,387✔
2438

2439
  int32_t rows = *(int32_t*)p;
1,453,387✔
2440
  p += sizeof(int32_t);
1,453,387✔
2441

2442
  int32_t cols = *(int32_t*)p;
1,453,387✔
2443
  p += sizeof(int32_t);
1,453,387✔
2444

2445
  if (rows != pResultInfo->numOfRows || cols != pResultInfo->numOfCols) {
1,453,387✔
2446
    tscError("setResultDataPtr paras error:rows;%d numOfRows:%" PRId64 " cols:%d numOfCols:%d", rows,
4!
2447
             pResultInfo->numOfRows, cols, pResultInfo->numOfCols);
2448
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2449
  }
2450

2451
  int32_t hasColumnSeg = *(int32_t*)p;
1,453,383✔
2452
  p += sizeof(int32_t);
1,453,383✔
2453

2454
  uint64_t groupId = taosGetUInt64Aligned((uint64_t*)p);
1,453,383✔
2455
  p += sizeof(uint64_t);
1,453,383✔
2456

2457
  // check fields
2458
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
6,153,278✔
2459
    int8_t type = *(int8_t*)p;
4,699,891✔
2460
    p += sizeof(int8_t);
4,699,891✔
2461

2462
    int32_t bytes = *(int32_t*)p;
4,699,891✔
2463
    p += sizeof(int32_t);
4,699,891✔
2464

2465
    if (IS_DECIMAL_TYPE(type) && pResultInfo->fields[i].precision == 0) {
4,699,891!
2466
      extractDecimalTypeInfoFromBytes(&bytes, &pResultInfo->fields[i].precision, &pResultInfo->fields[i].scale);
×
2467
    }
2468
  }
2469

2470
  int32_t* colLength = (int32_t*)p;
1,453,387✔
2471
  p += sizeof(int32_t) * pResultInfo->numOfCols;
1,453,387✔
2472

2473
  char* pStart = p;
1,453,387✔
2474
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
6,153,001✔
2475
    if ((pStart - pResultInfo->pData) >= dataLen) {
4,699,639!
2476
      tscError("setResultDataPtr invalid offset over dataLen %d", dataLen);
×
2477
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2478
    }
2479
    if (blockVersion == BLOCK_VERSION_1) {
4,699,639✔
2480
      colLength[i] = htonl(colLength[i]);
2,870,326✔
2481
    }
2482
    if (colLength[i] >= dataLen) {
4,699,639!
2483
      tscError("invalid colLength %d, dataLen %d", colLength[i], dataLen);
×
2484
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2485
    }
2486
    if (IS_INVALID_TYPE(pResultInfo->fields[i].type)) {
4,699,639!
2487
      tscError("invalid type %d", pResultInfo->fields[i].type);
×
2488
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2489
    }
2490
    if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
4,699,639✔
2491
      pResultInfo->pCol[i].offset = (int32_t*)pStart;
1,203,168✔
2492
      pStart += pResultInfo->numOfRows * sizeof(int32_t);
1,203,168✔
2493
    } else {
2494
      pResultInfo->pCol[i].nullbitmap = pStart;
3,496,471✔
2495
      pStart += BitmapLen(pResultInfo->numOfRows);
3,496,471✔
2496
    }
2497

2498
    pResultInfo->pCol[i].pData = pStart;
4,699,639✔
2499
    pResultInfo->length[i] = calcSchemaBytesFromTypeBytes(pResultInfo->fields[i].type, pResultInfo->fields[i].bytes, isStmt);
4,699,639✔
2500
    pResultInfo->row[i] = pResultInfo->pCol[i].pData;
4,699,614✔
2501

2502
    pStart += colLength[i];
4,699,614✔
2503
  }
2504

2505
  p = pStart;
1,453,362✔
2506
  // bool blankFill = *(bool*)p;
2507
  p += sizeof(bool);
1,453,362✔
2508
  int32_t offset = p - pResultInfo->pData;
1,453,362✔
2509
  if (offset > dataLen) {
1,453,362!
2510
    tscError("invalid offset %d, dataLen %d", offset, dataLen);
×
2511
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2512
  }
2513

2514
#ifndef DISALLOW_NCHAR_WITHOUT_ICONV
2515
  if (convertUcs4) {
1,453,362✔
2516
    code = doConvertUCS4(pResultInfo, colLength, isStmt);
1,453,153✔
2517
  }
2518
#endif
2519
  if (TSDB_CODE_SUCCESS == code && convertForDecimal) {
1,453,404!
2520
    code = convertDecimalType(pResultInfo);
1,453,197✔
2521
  }
2522
  return code;
1,453,365✔
2523
}
2524

2525
char* getDbOfConnection(STscObj* pObj) {
11,080,198✔
2526
  terrno = TSDB_CODE_SUCCESS;
11,080,198✔
2527
  char* p = NULL;
11,080,135✔
2528
  (void)taosThreadMutexLock(&pObj->mutex);
11,080,135✔
2529
  size_t len = strlen(pObj->db);
11,092,724✔
2530
  if (len > 0) {
11,092,724✔
2531
    p = taosStrndup(pObj->db, tListLen(pObj->db));
10,650,941!
2532
    if (p == NULL) {
10,642,265!
2533
      tscError("failed to taosStrndup db name");
×
2534
    }
2535
  }
2536

2537
  (void)taosThreadMutexUnlock(&pObj->mutex);
11,084,048✔
2538
  return p;
11,091,906✔
2539
}
2540

2541
void setConnectionDB(STscObj* pTscObj, const char* db) {
8,287✔
2542
  if (db == NULL || pTscObj == NULL) {
8,287!
2543
    tscError("setConnectionDB para is NULL");
×
2544
    return;
×
2545
  }
2546

2547
  (void)taosThreadMutexLock(&pTscObj->mutex);
8,293✔
2548
  tstrncpy(pTscObj->db, db, tListLen(pTscObj->db));
8,296✔
2549
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
8,296✔
2550
}
2551

2552
void resetConnectDB(STscObj* pTscObj) {
×
2553
  if (pTscObj == NULL) {
×
2554
    return;
×
2555
  }
2556

2557
  (void)taosThreadMutexLock(&pTscObj->mutex);
×
2558
  pTscObj->db[0] = 0;
×
2559
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
×
2560
}
2561

2562
int32_t setQueryResultFromRsp(SReqResultInfo* pResultInfo, const SRetrieveTableRsp* pRsp, bool convertUcs4, bool isStmt) {
1,189,742✔
2563
  if (pResultInfo == NULL || pRsp == NULL) {
1,189,742!
2564
    tscError("setQueryResultFromRsp paras is null");
×
2565
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2566
  }
2567

2568
  taosMemoryFreeClear(pResultInfo->pRspMsg);
1,189,759!
2569
  pResultInfo->pRspMsg = (const char*)pRsp;
1,189,759✔
2570
  pResultInfo->numOfRows = htobe64(pRsp->numOfRows);
1,189,759✔
2571
  pResultInfo->current = 0;
1,189,752✔
2572
  pResultInfo->completed = (pRsp->completed == 1);
1,189,752✔
2573
  pResultInfo->precision = pRsp->precision;
1,189,752✔
2574

2575
  // decompress data if needed
2576
  int32_t payloadLen = htonl(pRsp->payloadLen);
1,189,752✔
2577

2578
  if (pRsp->compressed) {
1,189,752✔
2579
    if (pResultInfo->decompBuf == NULL) {
650✔
2580
      pResultInfo->decompBuf = taosMemoryMalloc(payloadLen);
15!
2581
      if (pResultInfo->decompBuf == NULL) {
15!
2582
        tscError("failed to prepare the decompress buffer, size:%d", payloadLen);
×
2583
        return terrno;
×
2584
      }
2585
      pResultInfo->decompBufSize = payloadLen;
15✔
2586
    } else {
2587
      if (pResultInfo->decompBufSize < payloadLen) {
635✔
2588
        char* p = taosMemoryRealloc(pResultInfo->decompBuf, payloadLen);
20!
2589
        if (p == NULL) {
20!
2590
          tscError("failed to prepare the decompress buffer, size:%d", payloadLen);
×
2591
          return terrno;
×
2592
        }
2593

2594
        pResultInfo->decompBuf = p;
20✔
2595
        pResultInfo->decompBufSize = payloadLen;
20✔
2596
      }
2597
    }
2598
  }
2599

2600
  if (payloadLen > 0) {
1,189,752✔
2601
    int32_t compLen = *(int32_t*)pRsp->data;
1,121,321✔
2602
    int32_t rawLen = *(int32_t*)(pRsp->data + sizeof(int32_t));
1,121,321✔
2603

2604
    char* pStart = (char*)pRsp->data + sizeof(int32_t) * 2;
1,121,321✔
2605

2606
    if (pRsp->compressed && compLen < rawLen) {
1,121,321!
2607
      int32_t len = tsDecompressString(pStart, compLen, 1, pResultInfo->decompBuf, rawLen, ONE_STAGE_COMP, NULL, 0);
650✔
2608
      if (len < 0) {
650!
2609
        tscError("tsDecompressString failed");
×
2610
        return terrno ? terrno : TSDB_CODE_FAILED;
×
2611
      }
2612
      if (len != rawLen) {
650!
2613
        tscError("tsDecompressString failed, len:%d != rawLen:%d", len, rawLen);
×
2614
        return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2615
      }
2616
      pResultInfo->pData = pResultInfo->decompBuf;
650✔
2617
      pResultInfo->payloadLen = rawLen;
650✔
2618
    } else {
2619
      pResultInfo->pData = pStart;
1,120,671✔
2620
      pResultInfo->payloadLen = htonl(pRsp->compLen);
1,120,671✔
2621
      if (pRsp->compLen != pRsp->payloadLen) {
1,120,671!
2622
        tscError("pRsp->compLen:%d != pRsp->payloadLen:%d", pRsp->compLen, pRsp->payloadLen);
×
2623
        return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2624
      }
2625
    }
2626
  }
2627

2628
  // TODO handle the compressed case
2629
  pResultInfo->totalRows += pResultInfo->numOfRows;
1,189,752✔
2630

2631
  int32_t code = setResultDataPtr(pResultInfo, convertUcs4, isStmt);
1,189,752✔
2632
  return code;
1,189,762✔
2633
}
2634

2635
TSDB_SERVER_STATUS taos_check_server_status(const char* fqdn, int port, char* details, int maxlen) {
5✔
2636
  TSDB_SERVER_STATUS code = TSDB_SRV_STATUS_UNAVAILABLE;
5✔
2637
  void*              clientRpc = NULL;
5✔
2638
  SServerStatusRsp   statusRsp = {0};
5✔
2639
  SEpSet             epSet = {.inUse = 0, .numOfEps = 1};
5✔
2640
  SRpcMsg  rpcMsg = {.info.ahandle = (void*)0x9527, .info.notFreeAhandle = 1, .msgType = TDMT_DND_SERVER_STATUS};
5✔
2641
  SRpcMsg  rpcRsp = {0};
5✔
2642
  SRpcInit rpcInit = {0};
5✔
2643
  char     pass[TSDB_PASSWORD_LEN + 1] = {0};
5✔
2644

2645
  rpcInit.label = "CHK";
5✔
2646
  rpcInit.numOfThreads = 1;
5✔
2647
  rpcInit.cfp = NULL;
5✔
2648
  rpcInit.sessions = 16;
5✔
2649
  rpcInit.connType = TAOS_CONN_CLIENT;
5✔
2650
  rpcInit.idleTime = tsShellActivityTimer * 1000;
5✔
2651
  rpcInit.compressSize = tsCompressMsgSize;
5✔
2652
  rpcInit.user = "_dnd";
5✔
2653

2654
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
5✔
2655
  connLimitNum = TMAX(connLimitNum, 10);
5✔
2656
  connLimitNum = TMIN(connLimitNum, 500);
5✔
2657
  rpcInit.connLimitNum = connLimitNum;
5✔
2658
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
5✔
2659
  rpcInit.readTimeout = tsReadTimeout;
5✔
2660
  if (TSDB_CODE_SUCCESS != taosVersionStrToInt(td_version, &rpcInit.compatibilityVer)) {
5!
2661
    tscError("faild to convert taos version from str to int, errcode:%s", terrstr());
×
2662
    goto _OVER;
×
2663
  }
2664

2665
  clientRpc = rpcOpen(&rpcInit);
5✔
2666
  if (clientRpc == NULL) {
5!
2667
    code = terrno;
×
2668
    tscError("failed to init server status client since %s", tstrerror(code));
×
2669
    goto _OVER;
×
2670
  }
2671

2672
  if (fqdn == NULL) {
5!
2673
    fqdn = tsLocalFqdn;
5✔
2674
  }
2675

2676
  if (port == 0) {
5!
2677
    port = tsServerPort;
5✔
2678
  }
2679

2680
  tstrncpy(epSet.eps[0].fqdn, fqdn, TSDB_FQDN_LEN);
5✔
2681
  epSet.eps[0].port = (uint16_t)port;
5✔
2682
  int32_t ret = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
5✔
2683
  if (TSDB_CODE_SUCCESS != ret) {
5!
2684
    tscError("failed to send recv since %s", tstrerror(ret));
×
2685
    goto _OVER;
×
2686
  }
2687

2688
  if (rpcRsp.code != 0 || rpcRsp.contLen <= 0 || rpcRsp.pCont == NULL) {
5!
2689
    tscError("failed to send server status req since %s", terrstr());
1!
2690
    goto _OVER;
1✔
2691
  }
2692

2693
  if (tDeserializeSServerStatusRsp(rpcRsp.pCont, rpcRsp.contLen, &statusRsp) != 0) {
4!
2694
    tscError("failed to parse server status rsp since %s", terrstr());
×
2695
    goto _OVER;
×
2696
  }
2697

2698
  code = statusRsp.statusCode;
4✔
2699
  if (details != NULL) {
4!
2700
    tstrncpy(details, statusRsp.details, maxlen);
4✔
2701
  }
2702

2703
_OVER:
×
2704
  if (clientRpc != NULL) {
5!
2705
    rpcClose(clientRpc);
5✔
2706
  }
2707
  if (rpcRsp.pCont != NULL) {
5✔
2708
    rpcFreeCont(rpcRsp.pCont);
4✔
2709
  }
2710
  return code;
5✔
2711
}
2712

2713
int32_t appendTbToReq(SHashObj* pHash, int32_t pos1, int32_t len1, int32_t pos2, int32_t len2, const char* str,
4✔
2714
                      int32_t acctId, char* db) {
2715
  SName name = {0};
4✔
2716

2717
  if (len1 <= 0) {
4!
2718
    return -1;
×
2719
  }
2720

2721
  const char* dbName = db;
4✔
2722
  const char* tbName = NULL;
4✔
2723
  int32_t     dbLen = 0;
4✔
2724
  int32_t     tbLen = 0;
4✔
2725
  if (len2 > 0) {
4!
2726
    dbName = str + pos1;
×
2727
    dbLen = len1;
×
2728
    tbName = str + pos2;
×
2729
    tbLen = len2;
×
2730
  } else {
2731
    dbLen = strlen(db);
4✔
2732
    tbName = str + pos1;
4✔
2733
    tbLen = len1;
4✔
2734
  }
2735

2736
  if (dbLen <= 0 || tbLen <= 0) {
4!
2737
    return -1;
×
2738
  }
2739

2740
  if (tNameSetDbName(&name, acctId, dbName, dbLen)) {
4!
2741
    return -1;
×
2742
  }
2743

2744
  if (tNameAddTbName(&name, tbName, tbLen)) {
4!
2745
    return -1;
×
2746
  }
2747

2748
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
4✔
2749
  (void)snprintf(dbFName, TSDB_DB_FNAME_LEN, "%d.%.*s", acctId, dbLen, dbName);
4✔
2750

2751
  STablesReq* pDb = taosHashGet(pHash, dbFName, strlen(dbFName));
4✔
2752
  if (pDb) {
4!
2753
    if (NULL == taosArrayPush(pDb->pTables, &name)) {
×
2754
      return terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
2755
    }
2756
  } else {
2757
    STablesReq db;
2758
    db.pTables = taosArrayInit(20, sizeof(SName));
4✔
2759
    if (NULL == db.pTables) {
4!
2760
      return terrno;
×
2761
    }
2762
    tstrncpy(db.dbFName, dbFName, TSDB_DB_FNAME_LEN);
4✔
2763
    if (NULL == taosArrayPush(db.pTables, &name)) {
8!
2764
      return terrno;
×
2765
    }
2766
    TSC_ERR_RET(taosHashPut(pHash, dbFName, strlen(dbFName), &db, sizeof(db)));
4!
2767
  }
2768

2769
  return TSDB_CODE_SUCCESS;
4✔
2770
}
2771

2772
int32_t transferTableNameList(const char* tbList, int32_t acctId, char* dbName, SArray** pReq) {
4✔
2773
  SHashObj* pHash = taosHashInit(3, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
4✔
2774
  if (NULL == pHash) {
4!
2775
    return terrno;
×
2776
  }
2777

2778
  bool    inEscape = false;
4✔
2779
  int32_t code = 0;
4✔
2780
  void*   pIter = NULL;
4✔
2781

2782
  int32_t vIdx = 0;
4✔
2783
  int32_t vPos[2];
2784
  int32_t vLen[2];
2785

2786
  (void)memset(vPos, -1, sizeof(vPos));
4✔
2787
  (void)memset(vLen, 0, sizeof(vLen));
4✔
2788

2789
  for (int32_t i = 0;; ++i) {
18✔
2790
    if (0 == *(tbList + i)) {
18✔
2791
      if (vPos[vIdx] >= 0 && vLen[vIdx] <= 0) {
4!
2792
        vLen[vIdx] = i - vPos[vIdx];
4✔
2793
      }
2794

2795
      code = appendTbToReq(pHash, vPos[0], vLen[0], vPos[1], vLen[1], tbList, acctId, dbName);
4✔
2796
      if (code) {
4!
2797
        goto _return;
×
2798
      }
2799

2800
      break;
4✔
2801
    }
2802

2803
    if ('`' == *(tbList + i)) {
14!
2804
      inEscape = !inEscape;
×
2805
      if (!inEscape) {
×
2806
        if (vPos[vIdx] >= 0) {
×
2807
          vLen[vIdx] = i - vPos[vIdx];
×
2808
        } else {
2809
          goto _return;
×
2810
        }
2811
      }
2812

2813
      continue;
×
2814
    }
2815

2816
    if (inEscape) {
14!
2817
      if (vPos[vIdx] < 0) {
×
2818
        vPos[vIdx] = i;
×
2819
      }
2820
      continue;
×
2821
    }
2822

2823
    if ('.' == *(tbList + i)) {
14!
2824
      if (vPos[vIdx] < 0) {
×
2825
        goto _return;
×
2826
      }
2827
      if (vLen[vIdx] <= 0) {
×
2828
        vLen[vIdx] = i - vPos[vIdx];
×
2829
      }
2830
      vIdx++;
×
2831
      if (vIdx >= 2) {
×
2832
        goto _return;
×
2833
      }
2834
      continue;
×
2835
    }
2836

2837
    if (',' == *(tbList + i)) {
14!
2838
      if (vPos[vIdx] < 0) {
×
2839
        goto _return;
×
2840
      }
2841
      if (vLen[vIdx] <= 0) {
×
2842
        vLen[vIdx] = i - vPos[vIdx];
×
2843
      }
2844

2845
      code = appendTbToReq(pHash, vPos[0], vLen[0], vPos[1], vLen[1], tbList, acctId, dbName);
×
2846
      if (code) {
×
2847
        goto _return;
×
2848
      }
2849

2850
      (void)memset(vPos, -1, sizeof(vPos));
×
2851
      (void)memset(vLen, 0, sizeof(vLen));
×
2852
      vIdx = 0;
×
2853
      continue;
×
2854
    }
2855

2856
    if (' ' == *(tbList + i) || '\r' == *(tbList + i) || '\t' == *(tbList + i) || '\n' == *(tbList + i)) {
14!
2857
      if (vPos[vIdx] >= 0 && vLen[vIdx] <= 0) {
×
2858
        vLen[vIdx] = i - vPos[vIdx];
×
2859
      }
2860
      continue;
×
2861
    }
2862

2863
    if (('a' <= *(tbList + i) && 'z' >= *(tbList + i)) || ('A' <= *(tbList + i) && 'Z' >= *(tbList + i)) ||
14!
2864
        ('0' <= *(tbList + i) && '9' >= *(tbList + i)) || ('_' == *(tbList + i))) {
1!
2865
      if (vLen[vIdx] > 0) {
14!
2866
        goto _return;
×
2867
      }
2868
      if (vPos[vIdx] < 0) {
14✔
2869
        vPos[vIdx] = i;
4✔
2870
      }
2871
      continue;
14✔
2872
    }
2873

2874
    goto _return;
×
2875
  }
2876

2877
  int32_t dbNum = taosHashGetSize(pHash);
4✔
2878
  *pReq = taosArrayInit(dbNum, sizeof(STablesReq));
4✔
2879
  if (NULL == pReq) {
4!
2880
    TSC_ERR_JRET(terrno);
×
2881
  }
2882
  pIter = taosHashIterate(pHash, NULL);
4✔
2883
  while (pIter) {
8✔
2884
    STablesReq* pDb = (STablesReq*)pIter;
4✔
2885
    if (NULL == taosArrayPush(*pReq, pDb)) {
8!
2886
      TSC_ERR_JRET(terrno);
×
2887
    }
2888
    pIter = taosHashIterate(pHash, pIter);
4✔
2889
  }
2890

2891
  taosHashCleanup(pHash);
4✔
2892

2893
  return TSDB_CODE_SUCCESS;
4✔
2894

2895
_return:
×
2896

2897
  terrno = TSDB_CODE_TSC_INVALID_OPERATION;
×
2898

2899
  pIter = taosHashIterate(pHash, NULL);
×
2900
  while (pIter) {
×
2901
    STablesReq* pDb = (STablesReq*)pIter;
×
2902
    taosArrayDestroy(pDb->pTables);
×
2903
    pIter = taosHashIterate(pHash, pIter);
×
2904
  }
2905

2906
  taosHashCleanup(pHash);
×
2907

2908
  return terrno;
×
2909
}
2910

2911
void syncCatalogFn(SMetaData* pResult, void* param, int32_t code) {
4✔
2912
  SSyncQueryParam* pParam = param;
4✔
2913
  pParam->pRequest->code = code;
4✔
2914

2915
  if (TSDB_CODE_SUCCESS != tsem_post(&pParam->sem)) {
4!
2916
    tscError("failed to post semaphore since %s", tstrerror(terrno));
×
2917
  }
2918
}
4✔
2919

2920
void syncQueryFn(void* param, void* res, int32_t code) {
10,975,205✔
2921
  SSyncQueryParam* pParam = param;
10,975,205✔
2922
  pParam->pRequest = res;
10,975,205✔
2923

2924
  if (pParam->pRequest) {
10,975,205✔
2925
    pParam->pRequest->code = code;
10,973,124✔
2926
    clientOperateReport(pParam->pRequest);
10,973,124✔
2927
  }
2928

2929
  if (TSDB_CODE_SUCCESS != tsem_post(&pParam->sem)) {
10,976,966!
2930
    tscError("failed to post semaphore since %s", tstrerror(terrno));
×
2931
  }
2932
}
10,983,581✔
2933

2934
void taosAsyncQueryImpl(uint64_t connId, const char* sql, __taos_async_fn_t fp, void* param, bool validateOnly,
10,968,177✔
2935
                        int8_t source) {
2936
  if (sql == NULL || NULL == fp) {
10,968,177!
2937
    terrno = TSDB_CODE_INVALID_PARA;
×
2938
    if (fp) {
×
2939
      fp(param, NULL, terrno);
×
2940
    }
2941

UNCOV
2942
    return;
×
2943
  }
2944

2945
  size_t sqlLen = strlen(sql);
10,977,253✔
2946
  if (sqlLen > (size_t)TSDB_MAX_ALLOWED_SQL_LEN) {
10,977,253!
UNCOV
2947
    tscError("conn:0x%" PRIx64 ", sql string exceeds max length:%d", connId, TSDB_MAX_ALLOWED_SQL_LEN);
×
UNCOV
2948
    terrno = TSDB_CODE_TSC_EXCEED_SQL_LIMIT;
×
UNCOV
2949
    fp(param, NULL, terrno);
×
UNCOV
2950
    return;
×
2951
  }
2952

2953
  tscDebug("conn:0x%" PRIx64 ", taos_query execute, sql:%s", connId, sql);
10,977,253✔
2954

2955
  SRequestObj* pRequest = NULL;
10,977,253✔
2956
  int32_t      code = buildRequest(connId, sql, sqlLen, param, validateOnly, &pRequest, 0);
10,977,253✔
2957
  if (code != TSDB_CODE_SUCCESS) {
10,967,582!
2958
    terrno = code;
×
2959
    fp(param, NULL, terrno);
×
2960
    return;
×
2961
  }
2962

2963
  pRequest->source = source;
10,967,582✔
2964
  pRequest->body.queryFp = fp;
10,967,582✔
2965
  doAsyncQuery(pRequest, false);
10,967,582✔
2966
}
2967

2968
void taosAsyncQueryImplWithReqid(uint64_t connId, const char* sql, __taos_async_fn_t fp, void* param, bool validateOnly,
×
2969
                                 int64_t reqid) {
2970
  if (sql == NULL || NULL == fp) {
×
2971
    terrno = TSDB_CODE_INVALID_PARA;
×
2972
    if (fp) {
×
2973
      fp(param, NULL, terrno);
×
2974
    }
2975

2976
    return;
×
2977
  }
2978

2979
  size_t sqlLen = strlen(sql);
×
2980
  if (sqlLen > (size_t)TSDB_MAX_ALLOWED_SQL_LEN) {
×
2981
    tscError("conn:0x%" PRIx64 ", QID:0x%" PRIx64 ", sql string exceeds max length:%d", connId, reqid,
×
2982
             TSDB_MAX_ALLOWED_SQL_LEN);
2983
    terrno = TSDB_CODE_TSC_EXCEED_SQL_LIMIT;
×
2984
    fp(param, NULL, terrno);
×
2985
    return;
×
2986
  }
2987

2988
  tscDebug("conn:0x%" PRIx64 ", taos_query execute, QID:0x%" PRIx64 ", sql:%s", connId, reqid, sql);
×
2989

2990
  SRequestObj* pRequest = NULL;
×
2991
  int32_t      code = buildRequest(connId, sql, sqlLen, param, validateOnly, &pRequest, reqid);
×
2992
  if (code != TSDB_CODE_SUCCESS) {
×
2993
    terrno = code;
×
2994
    fp(param, NULL, terrno);
×
2995
    return;
×
2996
  }
2997

2998
  pRequest->body.queryFp = fp;
×
2999
  doAsyncQuery(pRequest, false);
×
3000
}
3001

3002
TAOS_RES* taosQueryImpl(TAOS* taos, const char* sql, bool validateOnly, int8_t source) {
10,966,457✔
3003
  if (NULL == taos) {
10,966,457✔
3004
    terrno = TSDB_CODE_TSC_DISCONNECTED;
5✔
3005
    return NULL;
5✔
3006
  }
3007

3008
  SSyncQueryParam* param = taosMemoryCalloc(1, sizeof(SSyncQueryParam));
10,966,452!
3009
  if (NULL == param) {
10,975,628!
3010
    return NULL;
×
3011
  }
3012
  int32_t code = tsem_init(&param->sem, 0, 0);
10,975,628✔
3013
  if (TSDB_CODE_SUCCESS != code) {
10,974,636!
3014
    taosMemoryFree(param);
×
3015
    return NULL;
×
3016
  }
3017

3018
  taosAsyncQueryImpl(*(int64_t*)taos, sql, syncQueryFn, param, validateOnly, source);
10,974,636✔
3019
  code = tsem_wait(&param->sem);
10,926,551✔
3020
  if (TSDB_CODE_SUCCESS != code) {
10,981,508!
3021
    taosMemoryFree(param);
×
3022
    return NULL;
×
3023
  }
3024
  code = tsem_destroy(&param->sem);
10,981,508✔
3025
  if (TSDB_CODE_SUCCESS != code) {
10,978,756!
3026
    tscError("failed to destroy semaphore since %s", tstrerror(code));
×
3027
  }
3028

3029
  SRequestObj* pRequest = NULL;
10,977,441✔
3030
  if (param->pRequest != NULL) {
10,977,441!
3031
    param->pRequest->syncQuery = true;
10,977,441✔
3032
    pRequest = param->pRequest;
10,977,441✔
3033
    param->pRequest->inCallback = false;
10,977,441✔
3034
  }
3035
  taosMemoryFree(param);
10,977,441!
3036

3037
  // tscDebug("QID:0x%" PRIx64 ", taos_query end, conn:0x%" PRIx64 ", res:%p", pRequest ? pRequest->requestId : 0,
3038
  //          *(int64_t*)taos, pRequest);
3039

3040
  return pRequest;
10,971,427✔
3041
}
3042

3043
TAOS_RES* taosQueryImplWithReqid(TAOS* taos, const char* sql, bool validateOnly, int64_t reqid) {
×
3044
  if (NULL == taos) {
×
3045
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
3046
    return NULL;
×
3047
  }
3048

3049
  SSyncQueryParam* param = taosMemoryCalloc(1, sizeof(SSyncQueryParam));
×
3050
  if (param == NULL) {
×
3051
    return NULL;
×
3052
  }
3053
  int32_t code = tsem_init(&param->sem, 0, 0);
×
3054
  if (TSDB_CODE_SUCCESS != code) {
×
3055
    taosMemoryFree(param);
×
3056
    return NULL;
×
3057
  }
3058

3059
  taosAsyncQueryImplWithReqid(*(int64_t*)taos, sql, syncQueryFn, param, validateOnly, reqid);
×
3060
  code = tsem_wait(&param->sem);
×
3061
  if (TSDB_CODE_SUCCESS != code) {
×
3062
    taosMemoryFree(param);
×
3063
    return NULL;
×
3064
  }
3065
  SRequestObj* pRequest = NULL;
×
3066
  if (param->pRequest != NULL) {
×
3067
    param->pRequest->syncQuery = true;
×
3068
    pRequest = param->pRequest;
×
3069
  }
3070
  taosMemoryFree(param);
×
3071

3072
  // tscDebug("QID:0x%" PRIx64 ", taos_query end, conn:0x%" PRIx64 ", res:%p", pRequest ? pRequest->requestId : 0,
3073
  //   *(int64_t*)taos, pRequest);
3074

3075
  return pRequest;
×
3076
}
3077

3078
static void fetchCallback(void* pResult, void* param, int32_t code) {
1,068,378✔
3079
  SRequestObj* pRequest = (SRequestObj*)param;
1,068,378✔
3080

3081
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
1,068,378✔
3082

3083
  tscDebug("req:0x%" PRIx64 ", enter scheduler fetch cb, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code, tstrerror(code),
1,068,378✔
3084
           pRequest->requestId);
3085

3086
  pResultInfo->pData = pResult;
1,068,368✔
3087
  pResultInfo->numOfRows = 0;
1,068,368✔
3088

3089
  if (code != TSDB_CODE_SUCCESS) {
1,068,368!
3090
    pRequest->code = code;
×
3091
    taosMemoryFreeClear(pResultInfo->pData);
×
3092
    pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, code);
×
3093
    return;
×
3094
  }
3095

3096
  if (pRequest->code != TSDB_CODE_SUCCESS) {
1,068,368!
3097
    taosMemoryFreeClear(pResultInfo->pData);
×
3098
    pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, pRequest->code);
×
3099
    return;
×
3100
  }
3101

3102
  pRequest->code =
1,068,381✔
3103
      setQueryResultFromRsp(pResultInfo, (const SRetrieveTableRsp*)pResultInfo->pData, pResultInfo->convertUcs4, pRequest->isStmtBind);
1,068,368✔
3104
  if (pRequest->code != TSDB_CODE_SUCCESS) {
1,068,381!
3105
    pResultInfo->numOfRows = 0;
×
3106
    tscError("req:0x%" PRIx64 ", fetch results failed, code:%s, QID:0x%" PRIx64, pRequest->self, tstrerror(pRequest->code),
×
3107
             pRequest->requestId);
3108
  } else {
3109
    tscDebug("req:0x%" PRIx64 ", fetch results, numOfRows:%" PRId64 " total Rows:%" PRId64 ", complete:%d, QID:0x%" PRIx64,
1,068,381✔
3110
             pRequest->self, pResultInfo->numOfRows, pResultInfo->totalRows, pResultInfo->completed,
3111
             pRequest->requestId);
3112

3113
    STscObj*            pTscObj = pRequest->pTscObj;
1,068,381✔
3114
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
1,068,381✔
3115
    (void)atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen);
1,068,381✔
3116
  }
3117

3118
  pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, pResultInfo->numOfRows);
1,068,392✔
3119
}
3120

3121
void taosAsyncFetchImpl(SRequestObj* pRequest, __taos_async_fn_t fp, void* param) {
1,153,697✔
3122
  pRequest->body.fetchFp = fp;
1,153,697✔
3123
  ((SSyncQueryParam*)pRequest->body.interParam)->userParam = param;
1,153,697✔
3124

3125
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
1,153,697✔
3126

3127
  // this query has no results or error exists, return directly
3128
  if (taos_num_fields(pRequest) == 0 || pRequest->code != TSDB_CODE_SUCCESS) {
1,153,697!
3129
    pResultInfo->numOfRows = 0;
2✔
3130
    pRequest->body.fetchFp(param, pRequest, pResultInfo->numOfRows);
2✔
3131
    return;
85,299✔
3132
  }
3133

3134
  // all data has returned to App already, no need to try again
3135
  if (pResultInfo->completed) {
1,153,693✔
3136
    // it is a local executed query, no need to do async fetch
3137
    if (QUERY_EXEC_MODE_SCHEDULE != pRequest->body.execMode) {
85,298✔
3138
      if (pResultInfo->localResultFetched) {
2,732✔
3139
        pResultInfo->numOfRows = 0;
1,366✔
3140
        pResultInfo->current = 0;
1,366✔
3141
      } else {
3142
        pResultInfo->localResultFetched = true;
1,366✔
3143
      }
3144
    } else {
3145
      pResultInfo->numOfRows = 0;
82,566✔
3146
    }
3147

3148
    pRequest->body.fetchFp(param, pRequest, pResultInfo->numOfRows);
85,298✔
3149
    return;
85,298✔
3150
  }
3151

3152
  SSchedulerReq req = {
1,068,395✔
3153
      .syncReq = false,
3154
      .fetchFp = fetchCallback,
3155
      .cbParam = pRequest,
3156
  };
3157

3158
  int32_t code = schedulerFetchRows(pRequest->body.queryJob, &req);
1,068,395✔
3159
  if (TSDB_CODE_SUCCESS != code) {
1,068,396!
3160
    tscError("0x%" PRIx64 " failed to schedule fetch rows", pRequest->requestId);
×
3161
    // pRequest->body.fetchFp(param, pRequest, code);
3162
  }
3163
}
3164

3165
void doRequestCallback(SRequestObj* pRequest, int32_t code) {
10,973,906✔
3166
  pRequest->inCallback = true;
10,973,906✔
3167
  int64_t this = pRequest->self;
10,973,906✔
3168
  if (tsQueryTbNotExistAsEmpty && TD_RES_QUERY(&pRequest->resType) && pRequest->isQuery &&
10,973,906!
3169
      (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_TDB_TABLE_NOT_EXIST)) {
316!
3170
    code = TSDB_CODE_SUCCESS;
×
3171
    pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
×
3172
  }
3173

3174
  tscDebug("QID:0x%" PRIx64 ", taos_query end, req:0x%" PRIx64 ", res:%p", pRequest->requestId, pRequest->self,
10,973,906✔
3175
           pRequest);
3176

3177
  if (pRequest->body.queryFp != NULL) {
10,973,906!
3178
    pRequest->body.queryFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, code);
10,974,775✔
3179
  }
3180

3181
  SRequestObj* pReq = acquireRequest(this);
10,982,196✔
3182
  if (pReq != NULL) {
10,980,389✔
3183
    pReq->inCallback = false;
10,978,706✔
3184
    (void)releaseRequest(this);
10,978,706✔
3185
  }
3186
}
10,972,384✔
3187

3188
int32_t clientParseSql(void* param, const char* dbName, const char* sql, bool parseOnly, const char* effectiveUser,
933✔
3189
                       SParseSqlRes* pRes) {
3190
#ifndef TD_ENTERPRISE
3191
  return TSDB_CODE_SUCCESS;
3192
#else
3193
  return clientParseSqlImpl(param, dbName, sql, parseOnly, effectiveUser, pRes);
933✔
3194
#endif
3195
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc