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

taosdata / TDengine / #4556

21 Jul 2025 05:44AM UTC coverage: 56.496% (+2.0%) from 54.508%
#4556

push

travis-ci

web-flow
Merge pull request #32061 from taosdata/new_testcases

skip tsim cases

138681 of 315239 branches covered (43.99%)

Branch coverage included in aggregate %.

209114 of 300373 relevant lines covered (69.62%)

17248409.04 hits per line

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

54.33
/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 "decimal.h"
22
#include "scheduler.h"
23
#include "tdatablock.h"
24
#include "tdataformat.h"
25
#include "tdef.h"
26
#include "tglobal.h"
27
#include "tmisce.h"
28
#include "tmsg.h"
29
#include "tmsgtype.h"
30
#include "tpagedbuf.h"
31
#include "tref.h"
32
#include "tsched.h"
33
#include "tversion.h"
34

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

38
void setQueryRequest(int64_t rId) {
160,086✔
39
  SRequestObj* pReq = acquireRequest(rId);
160,086✔
40
  if (pReq != NULL) {
160,103✔
41
    pReq->isQuery = true;
160,101✔
42
    (void)releaseRequest(rId);
160,101✔
43
  }
44
}
160,084✔
45

46
static bool stringLengthCheck(const char* str, size_t maxsize) {
36,289✔
47
  if (str == NULL) {
36,289!
48
    return false;
×
49
  }
50

51
  size_t len = strlen(str);
36,289✔
52
  if (len <= 0 || len > maxsize) {
36,289!
53
    return false;
×
54
  }
55

56
  return true;
36,355✔
57
}
58

59
static bool validateUserName(const char* user) { return stringLengthCheck(user, TSDB_USER_LEN - 1); }
15,571✔
60

61
static bool validatePassword(const char* passwd) { return stringLengthCheck(passwd, TSDB_PASSWORD_MAX_LEN); }
15,564✔
62

63
static bool validateDbName(const char* db) { return stringLengthCheck(db, TSDB_DB_NAME_LEN - 1); }
5,282✔
64

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

71
bool chkRequestKilled(void* param) {
26,329,610✔
72
  bool         killed = false;
26,329,610✔
73
  SRequestObj* pRequest = acquireRequest((int64_t)param);
26,329,610✔
74
  if (NULL == pRequest || pRequest->killed) {
26,743,954!
75
    killed = true;
×
76
  }
77

78
  (void)releaseRequest((int64_t)param);
26,743,954✔
79

80
  return killed;
26,665,449✔
81
}
82

83
void cleanupAppInfo() {
4,495✔
84
  taosHashCleanup(appInfo.pInstMap);
4,495✔
85
  taosHashCleanup(appInfo.pInstMapByClusterId);
4,495✔
86
  tscInfo("cluster instance map cleaned");
4,495!
87
}
4,495✔
88

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

92
int32_t taos_connect_internal(const char* ip, const char* user, const char* pass, const char* auth, const char* db,
15,541✔
93
                              uint16_t port, int connType, STscObj** pObj) {
94
  TSC_ERR_RET(taos_init());
15,541!
95
  if (!validateUserName(user)) {
15,588!
96
    TSC_ERR_RET(TSDB_CODE_TSC_INVALID_USER_LENGTH);
×
97
  }
98
  int32_t code = 0;
15,571✔
99

100
  char localDb[TSDB_DB_NAME_LEN] = {0};
15,571✔
101
  if (db != NULL && strlen(db) > 0) {
15,571✔
102
    if (!validateDbName(db)) {
5,284!
103
      TSC_ERR_RET(TSDB_CODE_TSC_INVALID_DB_LENGTH);
×
104
    }
105

106
    tstrncpy(localDb, db, sizeof(localDb));
5,294✔
107
    (void)strdequote(localDb);
5,294✔
108
  }
109

110
  char secretEncrypt[TSDB_PASSWORD_LEN + 1] = {0};
15,582✔
111
  if (auth == NULL) {
15,582✔
112
    if (!validatePassword(pass)) {
15,578!
113
      TSC_ERR_RET(TSDB_CODE_TSC_INVALID_PASS_LENGTH);
×
114
    }
115

116
    taosEncryptPass_c((uint8_t*)pass, strlen(pass), secretEncrypt);
15,563✔
117
  } else {
118
    tstrncpy(secretEncrypt, auth, tListLen(secretEncrypt));
4✔
119
  }
120

121
  SCorEpSet epSet = {0};
15,515✔
122
  if (ip) {
15,515✔
123
    TSC_ERR_RET(initEpSetFromCfg(ip, NULL, &epSet));
8,770✔
124
  } else {
125
    TSC_ERR_RET(initEpSetFromCfg(tsFirst, tsSecond, &epSet));
6,745!
126
  }
127

128
  if (port) {
15,459✔
129
    epSet.epSet.eps[0].port = port;
151✔
130
    epSet.epSet.eps[1].port = port;
151✔
131
  }
132

133
  char* key = getClusterKey(user, secretEncrypt, ip, port);
15,459✔
134
  if (NULL == key) {
15,434!
135
    TSC_ERR_RET(terrno);
×
136
  }
137
  tscInfo("connecting to server, numOfEps:%d inUse:%d user:%s db:%s key:%s", epSet.epSet.numOfEps, epSet.epSet.inUse,
15,434!
138
          user, db, key);
139
  for (int32_t i = 0; i < epSet.epSet.numOfEps; ++i) {
37,963✔
140
    tscInfo("ep:%d, %s:%u", i, epSet.epSet.eps[i].fqdn, epSet.epSet.eps[i].port);
22,355✔
141
  }
142
  // for (int32_t i = 0; i < epSet.epSet.numOfEps; i++) {
143
  //   if ((code = taosValidFqdn(tsEnableIpv6, epSet.epSet.eps[i].fqdn)) != 0) {
144
  //     taosMemFree(key);
145
  //     tscError("ipv6 flag %d, the local FQDN %s does not resolve to the ip address since %s", tsEnableIpv6,
146
  //              epSet.epSet.eps[i].fqdn, tstrerror(code));
147
  //     TSC_ERR_RET(code);
148
  //   }
149
  // }
150

151
  SAppInstInfo** pInst = NULL;
15,608✔
152
  code = taosThreadMutexLock(&appInfo.mutex);
15,608✔
153
  if (TSDB_CODE_SUCCESS != code) {
15,617!
154
    tscError("failed to lock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
155
    TSC_ERR_RET(code);
×
156
  }
157

158
  pInst = taosHashGet(appInfo.pInstMap, key, strlen(key));
15,617✔
159
  SAppInstInfo* p = NULL;
15,617✔
160
  if (pInst == NULL) {
15,617✔
161
    p = taosMemoryCalloc(1, sizeof(struct SAppInstInfo));
4,538!
162
    if (NULL == p) {
4,538!
163
      TSC_ERR_JRET(terrno);
×
164
    }
165
    p->mgmtEp = epSet;
4,538✔
166
    code = taosThreadMutexInit(&p->qnodeMutex, NULL);
4,538✔
167
    if (TSDB_CODE_SUCCESS != code) {
4,538!
168
      taosMemoryFree(p);
×
169
      TSC_ERR_JRET(code);
×
170
    }
171
    code = openTransporter(user, secretEncrypt, tsNumOfCores / 2, &p->pTransporter);
4,538✔
172
    if (TSDB_CODE_SUCCESS != code) {
4,538!
173
      taosMemoryFree(p);
×
174
      TSC_ERR_JRET(code);
×
175
    }
176
    code = appHbMgrInit(p, key, &p->pAppHbMgr);
4,538✔
177
    if (TSDB_CODE_SUCCESS != code) {
4,538!
178
      destroyAppInst(&p);
×
179
      TSC_ERR_JRET(code);
×
180
    }
181
    code = taosHashPut(appInfo.pInstMap, key, strlen(key), &p, POINTER_BYTES);
4,538✔
182
    if (TSDB_CODE_SUCCESS != code) {
4,538!
183
      destroyAppInst(&p);
×
184
      TSC_ERR_JRET(code);
×
185
    }
186
    p->instKey = key;
4,538✔
187
    key = NULL;
4,538✔
188
    tscInfo("new app inst mgr:%p, user:%s, ip:%s, port:%d", p, user, epSet.epSet.eps[0].fqdn, epSet.epSet.eps[0].port);
4,538!
189

190
    pInst = &p;
4,538✔
191
  } else {
192
    if (NULL == *pInst || NULL == (*pInst)->pAppHbMgr) {
11,079!
193
      tscError("*pInst:%p, pAppHgMgr:%p", *pInst, (*pInst) ? (*pInst)->pAppHbMgr : NULL);
×
194
      TSC_ERR_JRET(TSDB_CODE_TSC_INTERNAL_ERROR);
×
195
    }
196
    // reset to 0 in case of conn with duplicated user key but its user has ever been dropped.
197
    atomic_store_8(&(*pInst)->pAppHbMgr->connHbFlag, 0);
11,079✔
198
  }
199

200
_return:
15,617✔
201

202
  if (TSDB_CODE_SUCCESS != code) {
15,617!
203
    (void)taosThreadMutexUnlock(&appInfo.mutex);
×
204
    taosMemoryFreeClear(key);
×
205
    return code;
×
206
  } else {
207
    code = taosThreadMutexUnlock(&appInfo.mutex);
15,617✔
208
    taosMemoryFreeClear(key);
15,614!
209
    if (TSDB_CODE_SUCCESS != code) {
15,616!
210
      tscError("failed to unlock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
211
      return code;
×
212
    }
213
    return taosConnectImpl(user, &secretEncrypt[0], localDb, NULL, NULL, *pInst, connType, pObj);
15,616✔
214
  }
215
}
216

217
// SAppInstInfo* getAppInstInfo(const char* clusterKey) {
218
//   SAppInstInfo** ppAppInstInfo = taosHashGet(appInfo.pInstMap, clusterKey, strlen(clusterKey));
219
//   if (ppAppInstInfo != NULL && *ppAppInstInfo != NULL) {
220
//     return *ppAppInstInfo;
221
//   } else {
222
//     return NULL;
223
//   }
224
// }
225

226
void freeQueryParam(SSyncQueryParam* param) {
915✔
227
  if (param == NULL) return;
915!
228
  if (TSDB_CODE_SUCCESS != tsem_destroy(&param->sem)) {
915!
229
    tscError("failed to destroy semaphore in freeQueryParam");
×
230
  }
231
  taosMemoryFree(param);
915!
232
}
233

234
int32_t buildRequest(uint64_t connId, const char* sql, int sqlLen, void* param, bool validateSql,
8,758,140✔
235
                     SRequestObj** pRequest, int64_t reqid) {
236
  int32_t code = createRequest(connId, TSDB_SQL_SELECT, reqid, pRequest);
8,758,140✔
237
  if (TSDB_CODE_SUCCESS != code) {
8,775,332!
238
    tscError("failed to malloc sqlObj, %s", sql);
×
239
    return code;
×
240
  }
241

242
  (*pRequest)->sqlstr = taosMemoryMalloc(sqlLen + 1);
8,775,332!
243
  if ((*pRequest)->sqlstr == NULL) {
8,763,182!
244
    tscError("req:0x%" PRIx64 ", failed to prepare sql string buffer, %s", (*pRequest)->self, sql);
×
245
    destroyRequest(*pRequest);
×
246
    *pRequest = NULL;
×
247
    return terrno;
×
248
  }
249

250
  (void)strntolower((*pRequest)->sqlstr, sql, (int32_t)sqlLen);
8,763,182✔
251
  (*pRequest)->sqlstr[sqlLen] = 0;
8,773,703✔
252
  (*pRequest)->sqlLen = sqlLen;
8,773,703✔
253
  (*pRequest)->validateOnly = validateSql;
8,773,703✔
254
  (*pRequest)->isStmtBind = false;
8,773,703✔
255

256
  ((SSyncQueryParam*)(*pRequest)->body.interParam)->userParam = param;
8,773,703✔
257

258
  STscObj* pTscObj = (*pRequest)->pTscObj;
8,773,703✔
259
  int32_t  err = taosHashPut(pTscObj->pRequests, &(*pRequest)->self, sizeof((*pRequest)->self), &(*pRequest)->self,
8,773,703✔
260
                             sizeof((*pRequest)->self));
261
  if (err) {
8,758,156!
262
    tscError("req:0x%" PRId64 ", failed to add to request container, QID:0x%" PRIx64 ", conn:%" PRId64 ", %s",
×
263
             (*pRequest)->self, (*pRequest)->requestId, pTscObj->id, sql);
264
    destroyRequest(*pRequest);
×
265
    *pRequest = NULL;
×
266
    return terrno;
×
267
  }
268

269
  (*pRequest)->allocatorRefId = -1;
8,758,156✔
270
  if (tsQueryUseNodeAllocator && !qIsInsertValuesSql((*pRequest)->sqlstr, (*pRequest)->sqlLen)) {
8,758,156!
271
    if (TSDB_CODE_SUCCESS !=
271,277!
272
        nodesCreateAllocator((*pRequest)->requestId, tsQueryNodeChunkSize, &((*pRequest)->allocatorRefId))) {
271,277✔
273
      tscError("req:0x%" PRId64 ", failed to create node allocator, QID:0x%" PRIx64 ", conn:%" PRId64 ", %s",
×
274
               (*pRequest)->self, (*pRequest)->requestId, pTscObj->id, sql);
275
      destroyRequest(*pRequest);
×
276
      *pRequest = NULL;
×
277
      return terrno;
×
278
    }
279
  }
280

281
  tscDebug("req:0x%" PRIx64 ", build request, QID:0x%" PRIx64, (*pRequest)->self, (*pRequest)->requestId);
8,767,584✔
282
  return TSDB_CODE_SUCCESS;
8,767,509✔
283
}
284

285
int32_t buildPreviousRequest(SRequestObj* pRequest, const char* sql, SRequestObj** pNewRequest) {
×
286
  int32_t code =
287
      buildRequest(pRequest->pTscObj->id, sql, strlen(sql), pRequest, pRequest->validateOnly, pNewRequest, 0);
×
288
  if (TSDB_CODE_SUCCESS == code) {
×
289
    pRequest->relation.prevRefId = (*pNewRequest)->self;
×
290
    (*pNewRequest)->relation.nextRefId = pRequest->self;
×
291
    (*pNewRequest)->relation.userRefId = pRequest->self;
×
292
    (*pNewRequest)->isSubReq = true;
×
293
  }
294
  return code;
×
295
}
296

297
int32_t parseSql(SRequestObj* pRequest, bool topicQuery, SQuery** pQuery, SStmtCallback* pStmtCb) {
20,790✔
298
  STscObj* pTscObj = pRequest->pTscObj;
20,790✔
299

300
  SParseContext cxt = {
20,790✔
301
      .requestId = pRequest->requestId,
20,790✔
302
      .requestRid = pRequest->self,
20,790✔
303
      .acctId = pTscObj->acctId,
20,790✔
304
      .db = pRequest->pDb,
20,790✔
305
      .topicQuery = topicQuery,
306
      .pSql = pRequest->sqlstr,
20,790✔
307
      .sqlLen = pRequest->sqlLen,
20,790✔
308
      .pMsg = pRequest->msgBuf,
20,790✔
309
      .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
310
      .pTransporter = pTscObj->pAppInfo->pTransporter,
20,790✔
311
      .pStmtCb = pStmtCb,
312
      .pUser = pTscObj->user,
20,790✔
313
      .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
20,790✔
314
      .enableSysInfo = pTscObj->sysInfo,
20,790✔
315
      .svrVer = pTscObj->sVer,
20,790✔
316
      .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
20,790✔
317
      .isStmtBind = pRequest->isStmtBind,
20,790✔
318
      .setQueryFp = setQueryRequest,
319
      .timezone = pTscObj->optionInfo.timezone,
20,790✔
320
      .charsetCxt = pTscObj->optionInfo.charsetCxt,
20,790✔
321
  };
322

323
  cxt.mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
20,790✔
324
  int32_t code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &cxt.pCatalog);
20,800✔
325
  if (code != TSDB_CODE_SUCCESS) {
20,798!
326
    return code;
×
327
  }
328

329
  code = qParseSql(&cxt, pQuery);
20,798✔
330
  if (TSDB_CODE_SUCCESS == code) {
20,787✔
331
    if ((*pQuery)->haveResultSet) {
20,767!
332
      code = setResSchemaInfo(&pRequest->body.resInfo, (*pQuery)->pResSchema, (*pQuery)->numOfResCols,
×
333
                              (*pQuery)->pResExtSchema, pRequest->isStmtBind);
×
334
      setResPrecision(&pRequest->body.resInfo, (*pQuery)->precision);
×
335
    }
336
  }
337

338
  if (TSDB_CODE_SUCCESS == code || NEED_CLIENT_HANDLE_ERROR(code)) {
20,792!
339
    TSWAP(pRequest->dbList, (*pQuery)->pDbList);
20,777✔
340
    TSWAP(pRequest->tableList, (*pQuery)->pTableList);
20,777✔
341
    TSWAP(pRequest->targetTableList, (*pQuery)->pTargetTableList);
20,777✔
342
  }
343

344
  taosArrayDestroy(cxt.pTableMetaPos);
20,792✔
345
  taosArrayDestroy(cxt.pTableVgroupPos);
20,794✔
346

347
  return code;
20,793✔
348
}
349

350
int32_t execLocalCmd(SRequestObj* pRequest, SQuery* pQuery) {
×
351
  SRetrieveTableRsp* pRsp = NULL;
×
352
  int8_t             biMode = atomic_load_8(&pRequest->pTscObj->biMode);
×
353
  int32_t code = qExecCommand(&pRequest->pTscObj->id, pRequest->pTscObj->sysInfo, pQuery->pRoot, &pRsp, biMode,
×
354
                              pRequest->pTscObj->optionInfo.charsetCxt);
×
355
  if (TSDB_CODE_SUCCESS == code && NULL != pRsp) {
×
356
    code =
357
        setQueryResultFromRsp(&pRequest->body.resInfo, pRsp, pRequest->body.resInfo.convertUcs4, pRequest->isStmtBind);
×
358
  }
359

360
  return code;
×
361
}
362

363
int32_t execDdlQuery(SRequestObj* pRequest, SQuery* pQuery) {
605✔
364
  // drop table if exists not_exists_table
365
  if (NULL == pQuery->pCmdMsg) {
605!
366
    return TSDB_CODE_SUCCESS;
×
367
  }
368

369
  SCmdMsgInfo* pMsgInfo = pQuery->pCmdMsg;
605✔
370
  pRequest->type = pMsgInfo->msgType;
605✔
371
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsgInfo->pMsg, .len = pMsgInfo->msgLen, .handle = NULL};
605✔
372
  pMsgInfo->pMsg = NULL;  // pMsg transferred to SMsgSendInfo management
605✔
373

374
  STscObj*      pTscObj = pRequest->pTscObj;
605✔
375
  SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest);
605✔
376

377
  // int64_t transporterId = 0;
378
  TSC_ERR_RET(asyncSendMsgToServer(pTscObj->pAppInfo->pTransporter, &pMsgInfo->epSet, NULL, pSendMsg));
607!
379
  TSC_ERR_RET(tsem_wait(&pRequest->body.rspSem));
607!
380
  return TSDB_CODE_SUCCESS;
607✔
381
}
382

383
static SAppInstInfo* getAppInfo(SRequestObj* pRequest) { return pRequest->pTscObj->pAppInfo; }
17,338,767✔
384

385
void asyncExecLocalCmd(SRequestObj* pRequest, SQuery* pQuery) {
5,109✔
386
  SRetrieveTableRsp* pRsp = NULL;
5,109✔
387
  if (pRequest->validateOnly) {
5,109✔
388
    doRequestCallback(pRequest, 0);
27✔
389
    return;
27✔
390
  }
391

392
  int32_t code = qExecCommand(&pRequest->pTscObj->id, pRequest->pTscObj->sysInfo, pQuery->pRoot, &pRsp,
5,082✔
393
                              atomic_load_8(&pRequest->pTscObj->biMode), pRequest->pTscObj->optionInfo.charsetCxt);
5,082✔
394
  if (TSDB_CODE_SUCCESS == code && NULL != pRsp) {
5,081✔
395
    code =
396
        setQueryResultFromRsp(&pRequest->body.resInfo, pRsp, pRequest->body.resInfo.convertUcs4, pRequest->isStmtBind);
2,828✔
397
  }
398

399
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
5,081✔
400
  pRequest->code = code;
5,081✔
401

402
  if (pRequest->code != TSDB_CODE_SUCCESS) {
5,081✔
403
    pResultInfo->numOfRows = 0;
1✔
404
    tscError("req:0x%" PRIx64 ", fetch results failed, code:%s, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
1!
405
             pRequest->requestId);
406
  } else {
407
    tscDebug(
5,080✔
408
        "req:0x%" PRIx64 ", fetch results, numOfRows:%" PRId64 " total Rows:%" PRId64 ", complete:%d, QID:0x%" PRIx64,
409
        pRequest->self, pResultInfo->numOfRows, pResultInfo->totalRows, pResultInfo->completed, pRequest->requestId);
410
  }
411

412
  doRequestCallback(pRequest, code);
5,081✔
413
}
414

415
int32_t asyncExecDdlQuery(SRequestObj* pRequest, SQuery* pQuery) {
15,305✔
416
  if (pRequest->validateOnly) {
15,305!
417
    doRequestCallback(pRequest, 0);
×
418
    return TSDB_CODE_SUCCESS;
×
419
  }
420

421
  // drop table if exists not_exists_table
422
  if (NULL == pQuery->pCmdMsg) {
15,305✔
423
    doRequestCallback(pRequest, 0);
1✔
424
    return TSDB_CODE_SUCCESS;
1✔
425
  }
426

427
  SCmdMsgInfo* pMsgInfo = pQuery->pCmdMsg;
15,304✔
428
  pRequest->type = pMsgInfo->msgType;
15,304✔
429
  pRequest->body.requestMsg = (SDataBuf){.pData = pMsgInfo->pMsg, .len = pMsgInfo->msgLen, .handle = NULL};
15,304✔
430
  pMsgInfo->pMsg = NULL;  // pMsg transferred to SMsgSendInfo management
15,304✔
431

432
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
15,304✔
433
  SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest);
15,280✔
434

435
  int32_t code = asyncSendMsgToServer(pAppInfo->pTransporter, &pMsgInfo->epSet, NULL, pSendMsg);
15,308✔
436
  if (code) {
15,362!
437
    doRequestCallback(pRequest, code);
×
438
  }
439
  return code;
15,362✔
440
}
441

442
int compareQueryNodeLoad(const void* elem1, const void* elem2) {
246,406✔
443
  SQueryNodeLoad* node1 = (SQueryNodeLoad*)elem1;
246,406✔
444
  SQueryNodeLoad* node2 = (SQueryNodeLoad*)elem2;
246,406✔
445

446
  if (node1->load < node2->load) {
246,406!
447
    return -1;
×
448
  }
449

450
  return node1->load > node2->load;
246,406✔
451
}
452

453
int32_t updateQnodeList(SAppInstInfo* pInfo, SArray* pNodeList) {
70,002✔
454
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
70,002!
455
  if (pInfo->pQnodeList) {
70,002✔
456
    taosArrayDestroy(pInfo->pQnodeList);
69,953✔
457
    pInfo->pQnodeList = NULL;
69,953✔
458
    tscDebug("QnodeList cleared in cluster 0x%" PRIx64, pInfo->clusterId);
69,953✔
459
  }
460

461
  if (pNodeList) {
70,002!
462
    pInfo->pQnodeList = taosArrayDup(pNodeList, NULL);
70,002✔
463
    taosArraySort(pInfo->pQnodeList, compareQueryNodeLoad);
70,002✔
464
    tscDebug("QnodeList updated in cluster 0x%" PRIx64 ", num:%ld", pInfo->clusterId,
70,002✔
465
             taosArrayGetSize(pInfo->pQnodeList));
466
  }
467
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
70,002!
468

469
  return TSDB_CODE_SUCCESS;
70,002✔
470
}
471

472
int32_t qnodeRequired(SRequestObj* pRequest, bool* required) {
8,727,678✔
473
  if (QUERY_POLICY_VNODE == tsQueryPolicy || QUERY_POLICY_CLIENT == tsQueryPolicy) {
8,727,678✔
474
    *required = false;
8,724,102✔
475
    return TSDB_CODE_SUCCESS;
8,724,102✔
476
  }
477

478
  int32_t       code = TSDB_CODE_SUCCESS;
3,576✔
479
  SAppInstInfo* pInfo = pRequest->pTscObj->pAppInfo;
3,576✔
480
  *required = false;
3,576✔
481

482
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
3,576!
483
  *required = (NULL == pInfo->pQnodeList);
3,576✔
484
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
3,576!
485
  return TSDB_CODE_SUCCESS;
3,576✔
486
}
487

488
int32_t getQnodeList(SRequestObj* pRequest, SArray** pNodeList) {
×
489
  SAppInstInfo* pInfo = pRequest->pTscObj->pAppInfo;
×
490
  int32_t       code = 0;
×
491

492
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
×
493
  if (pInfo->pQnodeList) {
×
494
    *pNodeList = taosArrayDup(pInfo->pQnodeList, NULL);
×
495
  }
496
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
×
497
  if (NULL == *pNodeList) {
×
498
    SCatalog* pCatalog = NULL;
×
499
    code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog);
×
500
    if (TSDB_CODE_SUCCESS == code) {
×
501
      *pNodeList = taosArrayInit(5, sizeof(SQueryNodeLoad));
×
502
      if (NULL == pNodeList) {
×
503
        TSC_ERR_RET(terrno);
×
504
      }
505
      SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
×
506
                               .requestId = pRequest->requestId,
×
507
                               .requestObjRefId = pRequest->self,
×
508
                               .mgmtEps = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp)};
×
509
      code = catalogGetQnodeList(pCatalog, &conn, *pNodeList);
×
510
    }
511

512
    if (TSDB_CODE_SUCCESS == code && *pNodeList) {
×
513
      code = updateQnodeList(pInfo, *pNodeList);
×
514
    }
515
  }
516

517
  return code;
×
518
}
519

520
int32_t getPlan(SRequestObj* pRequest, SQuery* pQuery, SQueryPlan** pPlan, SArray* pNodeList) {
50,894✔
521
  pRequest->type = pQuery->msgType;
50,894✔
522
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
50,894✔
523

524
  SPlanContext cxt = {.queryId = pRequest->requestId,
101,848✔
525
                      .acctId = pRequest->pTscObj->acctId,
50,906✔
526
                      .mgmtEpSet = getEpSet_s(&pAppInfo->mgmtEp),
50,906✔
527
                      .pAstRoot = pQuery->pRoot,
50,942✔
528
                      .showRewrite = pQuery->showRewrite,
50,942✔
529
                      .pMsg = pRequest->msgBuf,
50,942✔
530
                      .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
531
                      .pUser = pRequest->pTscObj->user,
50,942✔
532
                      .timezone = pRequest->pTscObj->optionInfo.timezone,
50,942✔
533
                      .sysInfo = pRequest->pTscObj->sysInfo};
50,942✔
534

535
  return qCreateQueryPlan(&cxt, pPlan, pNodeList);
50,942✔
536
}
537

538
int32_t setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t numOfCols,
157,612✔
539
                         const SExtSchema* pExtSchema, bool isStmt) {
540
  if (pResInfo == NULL || pSchema == NULL || numOfCols <= 0) {
157,612!
541
    tscError("invalid paras, pResInfo == NULL || pSchema == NULL || numOfCols <= 0");
×
542
    return TSDB_CODE_INVALID_PARA;
×
543
  }
544

545
  pResInfo->numOfCols = numOfCols;
157,645✔
546
  if (pResInfo->fields != NULL) {
157,645✔
547
    taosMemoryFree(pResInfo->fields);
16!
548
  }
549
  if (pResInfo->userFields != NULL) {
157,645✔
550
    taosMemoryFree(pResInfo->userFields);
16!
551
  }
552
  pResInfo->fields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD_E));
157,645!
553
  if (NULL == pResInfo->fields) return terrno;
157,619!
554
  pResInfo->userFields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD));
157,619!
555
  if (NULL == pResInfo->userFields) {
157,592!
556
    taosMemoryFree(pResInfo->fields);
×
557
    return terrno;
×
558
  }
559
  if (numOfCols != pResInfo->numOfCols) {
157,592!
560
    tscError("numOfCols:%d != pResInfo->numOfCols:%d", numOfCols, pResInfo->numOfCols);
×
561
    return TSDB_CODE_FAILED;
×
562
  }
563

564
  for (int32_t i = 0; i < pResInfo->numOfCols; ++i) {
521,394✔
565
    pResInfo->fields[i].type = pSchema[i].type;
363,752✔
566

567
    pResInfo->userFields[i].type = pSchema[i].type;
363,752✔
568
    // userFields must convert to type bytes, no matter isStmt or not
569
    pResInfo->userFields[i].bytes = calcTypeBytesFromSchemaBytes(pSchema[i].type, pSchema[i].bytes, false);
363,752✔
570
    pResInfo->fields[i].bytes = calcTypeBytesFromSchemaBytes(pSchema[i].type, pSchema[i].bytes, isStmt);
363,765✔
571
    if (IS_DECIMAL_TYPE(pSchema[i].type) && pExtSchema) {
363,786!
572
      decimalFromTypeMod(pExtSchema[i].typeMod, &pResInfo->fields[i].precision, &pResInfo->fields[i].scale);
5✔
573
    }
574

575
    tstrncpy(pResInfo->fields[i].name, pSchema[i].name, tListLen(pResInfo->fields[i].name));
363,802✔
576
    tstrncpy(pResInfo->userFields[i].name, pSchema[i].name, tListLen(pResInfo->userFields[i].name));
363,802✔
577
  }
578
  return TSDB_CODE_SUCCESS;
157,642✔
579
}
580

581
void setResPrecision(SReqResultInfo* pResInfo, int32_t precision) {
152,245✔
582
  if (precision != TSDB_TIME_PRECISION_MILLI && precision != TSDB_TIME_PRECISION_MICRO &&
152,245!
583
      precision != TSDB_TIME_PRECISION_NANO) {
584
    return;
×
585
  }
586

587
  pResInfo->precision = precision;
152,245✔
588
}
589

590
int32_t buildVnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SArray* pDbVgList) {
249,510✔
591
  SArray* nodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
249,510✔
592
  if (NULL == nodeList) {
249,607!
593
    return terrno;
×
594
  }
595
  char* policy = (tsQueryPolicy == QUERY_POLICY_VNODE) ? "vnode" : "client";
249,618✔
596

597
  int32_t dbNum = taosArrayGetSize(pDbVgList);
249,618✔
598
  for (int32_t i = 0; i < dbNum; ++i) {
478,606✔
599
    SArray* pVg = taosArrayGetP(pDbVgList, i);
228,983✔
600
    if (NULL == pVg) {
229,010!
601
      continue;
×
602
    }
603
    int32_t vgNum = taosArrayGetSize(pVg);
229,010✔
604
    if (vgNum <= 0) {
229,010✔
605
      continue;
412✔
606
    }
607

608
    for (int32_t j = 0; j < vgNum; ++j) {
2,071,517✔
609
      SVgroupInfo* pInfo = taosArrayGet(pVg, j);
1,842,931✔
610
      if (NULL == pInfo) {
1,842,809!
611
        taosArrayDestroy(nodeList);
×
612
        return TSDB_CODE_OUT_OF_RANGE;
×
613
      }
614
      SQueryNodeLoad load = {0};
1,842,809✔
615
      load.addr.nodeId = pInfo->vgId;
1,842,809✔
616
      load.addr.epSet = pInfo->epSet;
1,842,809✔
617

618
      if (NULL == taosArrayPush(nodeList, &load)) {
1,842,919!
619
        taosArrayDestroy(nodeList);
×
620
        return terrno;
×
621
      }
622
    }
623
  }
624

625
  int32_t vnodeNum = taosArrayGetSize(nodeList);
249,623✔
626
  if (vnodeNum > 0) {
249,626✔
627
    tscDebug("0x%" PRIx64 " %s policy, use vnode list, num:%d", pRequest->requestId, policy, vnodeNum);
228,394✔
628
    goto _return;
228,387✔
629
  }
630

631
  int32_t mnodeNum = taosArrayGetSize(pMnodeList);
21,232✔
632
  if (mnodeNum <= 0) {
21,239!
633
    tscDebug("0x%" PRIx64 " %s policy, empty node list", pRequest->requestId, policy);
×
634
    goto _return;
×
635
  }
636

637
  void* pData = taosArrayGet(pMnodeList, 0);
21,239✔
638
  if (NULL == pData) {
21,237!
639
    taosArrayDestroy(nodeList);
×
640
    return TSDB_CODE_OUT_OF_RANGE;
×
641
  }
642
  if (NULL == taosArrayAddBatch(nodeList, pData, mnodeNum)) {
21,237!
643
    taosArrayDestroy(nodeList);
×
644
    return terrno;
×
645
  }
646

647
  tscDebug("0x%" PRIx64 " %s policy, use mnode list, num:%d", pRequest->requestId, policy, mnodeNum);
21,238✔
648

649
_return:
20,127✔
650

651
  *pNodeList = nodeList;
249,609✔
652

653
  return TSDB_CODE_SUCCESS;
249,609✔
654
}
655

656
int32_t buildQnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SArray* pQnodeList) {
2,992✔
657
  SArray* nodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
2,992✔
658
  if (NULL == nodeList) {
2,992!
659
    return terrno;
×
660
  }
661

662
  int32_t qNodeNum = taosArrayGetSize(pQnodeList);
2,992✔
663
  if (qNodeNum > 0) {
2,992!
664
    void* pData = taosArrayGet(pQnodeList, 0);
2,992✔
665
    if (NULL == pData) {
2,992!
666
      taosArrayDestroy(nodeList);
×
667
      return TSDB_CODE_OUT_OF_RANGE;
×
668
    }
669
    if (NULL == taosArrayAddBatch(nodeList, pData, qNodeNum)) {
2,992!
670
      taosArrayDestroy(nodeList);
×
671
      return terrno;
×
672
    }
673
    tscDebug("0x%" PRIx64 " qnode policy, use qnode list, num:%d", pRequest->requestId, qNodeNum);
2,992!
674
    goto _return;
2,992✔
675
  }
676

677
  int32_t mnodeNum = taosArrayGetSize(pMnodeList);
×
678
  if (mnodeNum <= 0) {
×
679
    tscDebug("0x%" PRIx64 " qnode policy, empty node list", pRequest->requestId);
×
680
    goto _return;
×
681
  }
682

683
  void* pData = taosArrayGet(pMnodeList, 0);
×
684
  if (NULL == pData) {
×
685
    taosArrayDestroy(nodeList);
×
686
    return TSDB_CODE_OUT_OF_RANGE;
×
687
  }
688
  if (NULL == taosArrayAddBatch(nodeList, pData, mnodeNum)) {
×
689
    taosArrayDestroy(nodeList);
×
690
    return terrno;
×
691
  }
692

693
  tscDebug("0x%" PRIx64 " qnode policy, use mnode list, num:%d", pRequest->requestId, mnodeNum);
×
694

695
_return:
×
696

697
  *pNodeList = nodeList;
2,992✔
698

699
  return TSDB_CODE_SUCCESS;
2,992✔
700
}
701

702
void freeVgList(void* list) {
30,872✔
703
  SArray* pList = *(SArray**)list;
30,872✔
704
  taosArrayDestroy(pList);
30,872✔
705
}
30,887✔
706

707
int32_t buildAsyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SMetaData* pResultMeta) {
201,636✔
708
  SArray* pDbVgList = NULL;
201,636✔
709
  SArray* pQnodeList = NULL;
201,636✔
710
  FDelete fp = NULL;
201,636✔
711
  int32_t code = 0;
201,636✔
712

713
  switch (tsQueryPolicy) {
201,636!
714
    case QUERY_POLICY_VNODE:
198,670✔
715
    case QUERY_POLICY_CLIENT: {
716
      if (pResultMeta) {
198,670!
717
        pDbVgList = taosArrayInit(4, POINTER_BYTES);
198,691✔
718
        if (NULL == pDbVgList) {
198,646!
719
          code = terrno;
×
720
          goto _return;
×
721
        }
722
        int32_t dbNum = taosArrayGetSize(pResultMeta->pDbVgroup);
198,646✔
723
        for (int32_t i = 0; i < dbNum; ++i) {
396,724✔
724
          SMetaRes* pRes = taosArrayGet(pResultMeta->pDbVgroup, i);
198,107✔
725
          if (pRes->code || NULL == pRes->pRes) {
198,088!
726
            continue;
×
727
          }
728

729
          if (NULL == taosArrayPush(pDbVgList, &pRes->pRes)) {
396,251!
730
            code = terrno;
×
731
            goto _return;
×
732
          }
733
        }
734
      } else {
735
        fp = freeVgList;
×
736

737
        int32_t dbNum = taosArrayGetSize(pRequest->dbList);
×
738
        if (dbNum > 0) {
×
739
          SCatalog*     pCtg = NULL;
×
740
          SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
×
741
          code = catalogGetHandle(pInst->clusterId, &pCtg);
×
742
          if (code != TSDB_CODE_SUCCESS) {
×
743
            goto _return;
×
744
          }
745

746
          pDbVgList = taosArrayInit(dbNum, POINTER_BYTES);
×
747
          if (NULL == pDbVgList) {
×
748
            code = terrno;
×
749
            goto _return;
×
750
          }
751
          SArray* pVgList = NULL;
×
752
          for (int32_t i = 0; i < dbNum; ++i) {
×
753
            char*            dbFName = taosArrayGet(pRequest->dbList, i);
×
754
            SRequestConnInfo conn = {.pTrans = pInst->pTransporter,
×
755
                                     .requestId = pRequest->requestId,
×
756
                                     .requestObjRefId = pRequest->self,
×
757
                                     .mgmtEps = getEpSet_s(&pInst->mgmtEp)};
×
758

759
            // catalogGetDBVgList will handle dbFName == null.
760
            code = catalogGetDBVgList(pCtg, &conn, dbFName, &pVgList);
×
761
            if (code) {
×
762
              goto _return;
×
763
            }
764

765
            if (NULL == taosArrayPush(pDbVgList, &pVgList)) {
×
766
              code = terrno;
×
767
              goto _return;
×
768
            }
769
          }
770
        }
771
      }
772

773
      code = buildVnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pDbVgList);
198,617✔
774
      break;
198,682✔
775
    }
776
    case QUERY_POLICY_HYBRID:
2,992✔
777
    case QUERY_POLICY_QNODE: {
778
      if (pResultMeta && taosArrayGetSize(pResultMeta->pQnodeList) > 0) {
2,992!
779
        SMetaRes* pRes = taosArrayGet(pResultMeta->pQnodeList, 0);
×
780
        if (pRes->code) {
×
781
          pQnodeList = NULL;
×
782
        } else {
783
          pQnodeList = taosArrayDup((SArray*)pRes->pRes, NULL);
×
784
          if (NULL == pQnodeList) {
×
785
            code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
786
            goto _return;
×
787
          }
788
        }
789
      } else {
790
        SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
2,992✔
791
        TSC_ERR_JRET(taosThreadMutexLock(&pInst->qnodeMutex));
2,992!
792
        if (pInst->pQnodeList) {
2,992!
793
          pQnodeList = taosArrayDup(pInst->pQnodeList, NULL);
2,992✔
794
          if (NULL == pQnodeList) {
2,992!
795
            code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
796
            goto _return;
×
797
          }
798
        }
799
        TSC_ERR_JRET(taosThreadMutexUnlock(&pInst->qnodeMutex));
2,992!
800
      }
801

802
      code = buildQnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pQnodeList);
2,992✔
803
      break;
2,992✔
804
    }
805
    default:
×
806
      tscError("unknown query policy: %d", tsQueryPolicy);
×
807
      return TSDB_CODE_APP_ERROR;
×
808
  }
809

810
_return:
201,674✔
811
  taosArrayDestroyEx(pDbVgList, fp);
201,674✔
812
  taosArrayDestroy(pQnodeList);
201,690✔
813

814
  return code;
201,694✔
815
}
816

817
int32_t buildSyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList) {
50,836✔
818
  SArray* pDbVgList = NULL;
50,836✔
819
  SArray* pQnodeList = NULL;
50,836✔
820
  int32_t code = 0;
50,836✔
821

822
  switch (tsQueryPolicy) {
50,836!
823
    case QUERY_POLICY_VNODE:
50,851✔
824
    case QUERY_POLICY_CLIENT: {
825
      int32_t dbNum = taosArrayGetSize(pRequest->dbList);
50,851✔
826
      if (dbNum > 0) {
50,922✔
827
        SCatalog*     pCtg = NULL;
30,871✔
828
        SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
30,871✔
829
        code = catalogGetHandle(pInst->clusterId, &pCtg);
30,871✔
830
        if (code != TSDB_CODE_SUCCESS) {
30,865!
831
          goto _return;
×
832
        }
833

834
        pDbVgList = taosArrayInit(dbNum, POINTER_BYTES);
30,865✔
835
        if (NULL == pDbVgList) {
30,854!
836
          code = terrno;
×
837
          goto _return;
×
838
        }
839
        SArray* pVgList = NULL;
30,855✔
840
        for (int32_t i = 0; i < dbNum; ++i) {
61,743✔
841
          char*            dbFName = taosArrayGet(pRequest->dbList, i);
30,846✔
842
          SRequestConnInfo conn = {.pTrans = pInst->pTransporter,
30,862✔
843
                                   .requestId = pRequest->requestId,
30,862✔
844
                                   .requestObjRefId = pRequest->self,
30,862✔
845
                                   .mgmtEps = getEpSet_s(&pInst->mgmtEp)};
30,862✔
846

847
          // catalogGetDBVgList will handle dbFName == null.
848
          code = catalogGetDBVgList(pCtg, &conn, dbFName, &pVgList);
30,885✔
849
          if (code) {
30,880!
850
            goto _return;
×
851
          }
852

853
          if (NULL == taosArrayPush(pDbVgList, &pVgList)) {
30,888!
854
            code = terrno;
×
855
            goto _return;
×
856
          }
857
        }
858
      }
859

860
      code = buildVnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pDbVgList);
50,948✔
861
      break;
50,926✔
862
    }
863
    case QUERY_POLICY_HYBRID:
×
864
    case QUERY_POLICY_QNODE: {
865
      TSC_ERR_JRET(getQnodeList(pRequest, &pQnodeList));
×
866

867
      code = buildQnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pQnodeList);
×
868
      break;
×
869
    }
870
    default:
×
871
      tscError("unknown query policy: %d", tsQueryPolicy);
×
872
      return TSDB_CODE_APP_ERROR;
×
873
  }
874

875
_return:
50,926✔
876

877
  taosArrayDestroyEx(pDbVgList, freeVgList);
50,926✔
878
  taosArrayDestroy(pQnodeList);
50,912✔
879

880
  return code;
50,924✔
881
}
882

883
int32_t scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList) {
50,891✔
884
  void* pTransporter = pRequest->pTscObj->pAppInfo->pTransporter;
50,891✔
885

886
  SExecResult      res = {0};
50,891✔
887
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
50,891✔
888
                           .requestId = pRequest->requestId,
50,891✔
889
                           .requestObjRefId = pRequest->self};
50,891✔
890
  SSchedulerReq    req = {
101,789✔
891
         .syncReq = true,
892
         .localReq = (tsQueryPolicy == QUERY_POLICY_CLIENT),
50,891✔
893
         .pConn = &conn,
894
         .pNodeList = pNodeList,
895
         .pDag = pDag,
896
         .sql = pRequest->sqlstr,
50,891✔
897
         .startTs = pRequest->metric.start,
50,891✔
898
         .execFp = NULL,
899
         .cbParam = NULL,
900
         .chkKillFp = chkRequestKilled,
901
         .chkKillParam = (void*)pRequest->self,
50,891✔
902
         .pExecRes = &res,
903
         .source = pRequest->source,
50,891✔
904
         .pWorkerCb = getTaskPoolWorkerCb(),
50,891✔
905
  };
906

907
  int32_t code = schedulerExecJob(&req, &pRequest->body.queryJob);
50,898✔
908

909
  destroyQueryExecRes(&pRequest->body.resInfo.execRes);
50,916✔
910
  (void)memcpy(&pRequest->body.resInfo.execRes, &res, sizeof(res));
50,922✔
911

912
  if (code != TSDB_CODE_SUCCESS) {
50,922!
913
    schedulerFreeJob(&pRequest->body.queryJob, 0);
×
914

915
    pRequest->code = code;
×
916
    terrno = code;
×
917
    return pRequest->code;
7✔
918
  }
919

920
  if (TDMT_VND_SUBMIT == pRequest->type || TDMT_VND_DELETE == pRequest->type ||
50,922!
921
      TDMT_VND_CREATE_TABLE == pRequest->type) {
8✔
922
    pRequest->body.resInfo.numOfRows = res.numOfRows;
50,915✔
923
    if (TDMT_VND_SUBMIT == pRequest->type) {
50,915!
924
      STscObj*            pTscObj = pRequest->pTscObj;
50,935✔
925
      SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
50,935✔
926
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, res.numOfRows);
50,935✔
927
    }
928

929
    schedulerFreeJob(&pRequest->body.queryJob, 0);
50,921✔
930
  }
931

932
  pRequest->code = res.code;
50,936✔
933
  terrno = res.code;
50,936✔
934
  return pRequest->code;
50,927✔
935
}
936

937
int32_t handleSubmitExecRes(SRequestObj* pRequest, void* res, SCatalog* pCatalog, SEpSet* epset) {
8,503,967✔
938
  SArray*      pArray = NULL;
8,503,967✔
939
  SSubmitRsp2* pRsp = (SSubmitRsp2*)res;
8,503,967✔
940
  if (NULL == pRsp->aCreateTbRsp) {
8,503,967✔
941
    return TSDB_CODE_SUCCESS;
8,445,267✔
942
  }
943

944
  int32_t tbNum = taosArrayGetSize(pRsp->aCreateTbRsp);
58,700✔
945
  for (int32_t i = 0; i < tbNum; ++i) {
127,476✔
946
    SVCreateTbRsp* pTbRsp = (SVCreateTbRsp*)taosArrayGet(pRsp->aCreateTbRsp, i);
65,139✔
947
    if (pTbRsp->pMeta) {
65,135✔
948
      TSC_ERR_RET(handleCreateTbExecRes(pTbRsp->pMeta, pCatalog));
42,814!
949
    }
950
  }
951

952
  return TSDB_CODE_SUCCESS;
62,337✔
953
}
954

955
int32_t handleQueryExecRes(SRequestObj* pRequest, void* res, SCatalog* pCatalog, SEpSet* epset) {
142,719✔
956
  int32_t code = 0;
142,719✔
957
  SArray* pArray = NULL;
142,719✔
958
  SArray* pTbArray = (SArray*)res;
142,719✔
959
  int32_t tbNum = taosArrayGetSize(pTbArray);
142,719✔
960
  if (tbNum <= 0) {
142,725!
961
    return TSDB_CODE_SUCCESS;
×
962
  }
963

964
  pArray = taosArrayInit(tbNum, sizeof(STbSVersion));
142,725✔
965
  if (NULL == pArray) {
142,728!
966
    return terrno;
×
967
  }
968

969
  for (int32_t i = 0; i < tbNum; ++i) {
329,243✔
970
    STbVerInfo* tbInfo = taosArrayGet(pTbArray, i);
186,509✔
971
    if (NULL == tbInfo) {
186,508!
972
      code = terrno;
×
973
      goto _return;
×
974
    }
975
    STbSVersion tbSver = {
186,508✔
976
        .tbFName = tbInfo->tbFName, .sver = tbInfo->sversion, .tver = tbInfo->tversion, .rver = tbInfo->rversion};
186,508✔
977
    if (NULL == taosArrayPush(pArray, &tbSver)) {
186,514!
978
      code = terrno;
×
979
      goto _return;
×
980
    }
981
  }
982

983
  SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter,
142,734✔
984
                           .requestId = pRequest->requestId,
142,734✔
985
                           .requestObjRefId = pRequest->self,
142,734✔
986
                           .mgmtEps = *epset};
987

988
  code = catalogChkTbMetaVersion(pCatalog, &conn, pArray);
142,734✔
989

990
_return:
142,713✔
991

992
  taosArrayDestroy(pArray);
142,713✔
993
  return code;
142,731✔
994
}
995

996
int32_t handleAlterTbExecRes(void* res, SCatalog* pCatalog) {
2,728✔
997
  return catalogUpdateTableMeta(pCatalog, (STableMetaRsp*)res);
2,728✔
998
}
999

1000
int32_t handleCreateTbExecRes(void* res, SCatalog* pCatalog) {
98,771✔
1001
  return catalogAsyncUpdateTableMeta(pCatalog, (STableMetaRsp*)res);
98,771✔
1002
}
1003

1004
int32_t handleQueryExecRsp(SRequestObj* pRequest) {
8,722,676✔
1005
  if (NULL == pRequest->body.resInfo.execRes.res) {
8,722,676✔
1006
    return pRequest->code;
62,826✔
1007
  }
1008

1009
  SCatalog*     pCatalog = NULL;
8,659,850✔
1010
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
8,659,850✔
1011

1012
  int32_t code = catalogGetHandle(pAppInfo->clusterId, &pCatalog);
8,660,686✔
1013
  if (code) {
8,668,954!
1014
    return code;
×
1015
  }
1016

1017
  SEpSet       epset = getEpSet_s(&pAppInfo->mgmtEp);
8,668,954✔
1018
  SExecResult* pRes = &pRequest->body.resInfo.execRes;
8,688,934✔
1019

1020
  switch (pRes->msgType) {
8,688,934!
1021
    case TDMT_VND_ALTER_TABLE:
1,325✔
1022
    case TDMT_MND_ALTER_STB: {
1023
      code = handleAlterTbExecRes(pRes->res, pCatalog);
1,325✔
1024
      break;
1,325✔
1025
    }
1026
    case TDMT_VND_CREATE_TABLE: {
34,565✔
1027
      SArray* pList = (SArray*)pRes->res;
34,565✔
1028
      int32_t num = taosArrayGetSize(pList);
34,565✔
1029
      for (int32_t i = 0; i < num; ++i) {
88,864✔
1030
        void* res = taosArrayGetP(pList, i);
54,299✔
1031
        // handleCreateTbExecRes will handle res == null
1032
        code = handleCreateTbExecRes(res, pCatalog);
54,304✔
1033
      }
1034
      break;
34,565✔
1035
    }
1036
    case TDMT_MND_CREATE_STB: {
491✔
1037
      code = handleCreateTbExecRes(pRes->res, pCatalog);
491✔
1038
      break;
491✔
1039
    }
1040
    case TDMT_VND_SUBMIT: {
8,510,459✔
1041
      (void)atomic_add_fetch_64((int64_t*)&pAppInfo->summary.insertBytes, pRes->numOfBytes);
8,510,459✔
1042

1043
      code = handleSubmitExecRes(pRequest, pRes->res, pCatalog, &epset);
8,512,270✔
1044
      break;
8,504,419✔
1045
    }
1046
    case TDMT_SCH_QUERY:
142,733✔
1047
    case TDMT_SCH_MERGE_QUERY: {
1048
      code = handleQueryExecRes(pRequest, pRes->res, pCatalog, &epset);
142,733✔
1049
      break;
142,717✔
1050
    }
1051
    default:
×
1052
      tscError("req:0x%" PRIx64 ", invalid exec result for request type:%d, QID:0x%" PRIx64, pRequest->self,
×
1053
               pRequest->type, pRequest->requestId);
1054
      code = TSDB_CODE_APP_ERROR;
×
1055
  }
1056

1057
  return code;
8,683,517✔
1058
}
1059

1060
static bool incompletaFileParsing(SNode* pStmt) {
8,700,051✔
1061
  return QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pStmt) ? false : ((SVnodeModifyOpStmt*)pStmt)->fileProcessing;
8,700,051!
1062
}
1063

1064
void continuePostSubQuery(SRequestObj* pRequest, SSDataBlock* pBlock) {
×
1065
  SSqlCallbackWrapper* pWrapper = pRequest->pWrapper;
×
1066

1067
  int32_t code = nodesAcquireAllocator(pWrapper->pParseCtx->allocatorId);
×
1068
  if (TSDB_CODE_SUCCESS == code) {
×
1069
    int64_t analyseStart = taosGetTimestampUs();
×
1070
    code = qContinueParsePostQuery(pWrapper->pParseCtx, pRequest->pQuery, pBlock);
×
1071
    pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart;
×
1072
  }
1073

1074
  if (TSDB_CODE_SUCCESS == code) {
×
1075
    code = qContinuePlanPostQuery(pRequest->pPostPlan);
×
1076
  }
1077

1078
  code = nodesReleaseAllocator(pWrapper->pParseCtx->allocatorId);
×
1079
  handleQueryAnslyseRes(pWrapper, NULL, code);
×
1080
}
×
1081

1082
void returnToUser(SRequestObj* pRequest) {
3,065✔
1083
  if (pRequest->relation.userRefId == pRequest->self || 0 == pRequest->relation.userRefId) {
3,065!
1084
    // return to client
1085
    doRequestCallback(pRequest, pRequest->code);
3,065✔
1086
    return;
3,065✔
1087
  }
1088

1089
  SRequestObj* pUserReq = acquireRequest(pRequest->relation.userRefId);
×
1090
  if (pUserReq) {
×
1091
    pUserReq->code = pRequest->code;
×
1092
    // return to client
1093
    doRequestCallback(pUserReq, pUserReq->code);
×
1094
    (void)releaseRequest(pRequest->relation.userRefId);
×
1095
    return;
×
1096
  } else {
1097
    tscError("req:0x%" PRIx64 ", user ref 0x%" PRIx64 " is not there, QID:0x%" PRIx64, pRequest->self,
×
1098
             pRequest->relation.userRefId, pRequest->requestId);
1099
  }
1100
}
1101

1102
static int32_t createResultBlock(TAOS_RES* pRes, int32_t numOfRows, SSDataBlock** pBlock) {
×
1103
  int64_t     lastTs = 0;
×
1104
  TAOS_FIELD* pResFields = taos_fetch_fields(pRes);
×
1105
  int32_t     numOfFields = taos_num_fields(pRes);
×
1106

1107
  int32_t code = createDataBlock(pBlock);
×
1108
  if (code) {
×
1109
    return code;
×
1110
  }
1111

1112
  for (int32_t i = 0; i < numOfFields; ++i) {
×
1113
    SColumnInfoData colInfoData = createColumnInfoData(pResFields[i].type, pResFields[i].bytes, i + 1);
×
1114
    code = blockDataAppendColInfo(*pBlock, &colInfoData);
×
1115
    if (TSDB_CODE_SUCCESS != code) {
×
1116
      blockDataDestroy(*pBlock);
×
1117
      return code;
×
1118
    }
1119
  }
1120

1121
  code = blockDataEnsureCapacity(*pBlock, numOfRows);
×
1122
  if (TSDB_CODE_SUCCESS != code) {
×
1123
    blockDataDestroy(*pBlock);
×
1124
    return code;
×
1125
  }
1126

1127
  for (int32_t i = 0; i < numOfRows; ++i) {
×
1128
    TAOS_ROW pRow = taos_fetch_row(pRes);
×
1129
    if (NULL == pRow[0] || NULL == pRow[1] || NULL == pRow[2]) {
×
1130
      tscError("invalid data from vnode");
×
1131
      blockDataDestroy(*pBlock);
×
1132
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
1133
    }
1134
    int64_t ts = *(int64_t*)pRow[0];
×
1135
    if (lastTs < ts) {
×
1136
      lastTs = ts;
×
1137
    }
1138

1139
    for (int32_t j = 0; j < numOfFields; ++j) {
×
1140
      SColumnInfoData* pColInfoData = taosArrayGet((*pBlock)->pDataBlock, j);
×
1141
      code = colDataSetVal(pColInfoData, i, pRow[j], false);
×
1142
      if (TSDB_CODE_SUCCESS != code) {
×
1143
        blockDataDestroy(*pBlock);
×
1144
        return code;
×
1145
      }
1146
    }
1147

1148
    tscInfo("[create stream with histroy] lastKey:%" PRId64 " vgId:%d, vgVer:%" PRId64, ts, *(int32_t*)pRow[1],
×
1149
            *(int64_t*)pRow[2]);
1150
  }
1151

1152
  (*pBlock)->info.window.ekey = lastTs;
×
1153
  (*pBlock)->info.rows = numOfRows;
×
1154

1155
  tscInfo("[create stream with histroy] lastKey:%" PRId64 " numOfRows:%d from all vgroups", lastTs, numOfRows);
×
1156
  return TSDB_CODE_SUCCESS;
×
1157
}
1158

1159
void postSubQueryFetchCb(void* param, TAOS_RES* res, int32_t rowNum) {
×
1160
  SRequestObj* pRequest = (SRequestObj*)res;
×
1161
  if (pRequest->code) {
×
1162
    returnToUser(pRequest);
×
1163
    return;
×
1164
  }
1165

1166
  SSDataBlock* pBlock = NULL;
×
1167
  pRequest->code = createResultBlock(res, rowNum, &pBlock);
×
1168
  if (TSDB_CODE_SUCCESS != pRequest->code) {
×
1169
    tscError("req:0x%" PRIx64 ", create result block failed, QID:0x%" PRIx64 " %s", pRequest->self, pRequest->requestId,
×
1170
             tstrerror(pRequest->code));
1171
    returnToUser(pRequest);
×
1172
    return;
×
1173
  }
1174

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

1184
  blockDataDestroy(pBlock);
×
1185
}
1186

1187
void handlePostSubQuery(SSqlCallbackWrapper* pWrapper) {
×
1188
  SRequestObj* pRequest = pWrapper->pRequest;
×
1189
  if (TD_RES_QUERY(pRequest)) {
×
1190
    taosAsyncFetchImpl(pRequest, postSubQueryFetchCb, pWrapper);
×
1191
    return;
×
1192
  }
1193

1194
  SRequestObj* pNextReq = acquireRequest(pRequest->relation.nextRefId);
×
1195
  if (pNextReq) {
×
1196
    continuePostSubQuery(pNextReq, NULL);
×
1197
    (void)releaseRequest(pRequest->relation.nextRefId);
×
1198
  } else {
1199
    tscError("req:0x%" PRIx64 ", next req ref 0x%" PRIx64 " is not there, QID:0x%" PRIx64, pRequest->self,
×
1200
             pRequest->relation.nextRefId, pRequest->requestId);
1201
  }
1202
}
1203

1204
// todo refacto the error code  mgmt
1205
void schedulerExecCb(SExecResult* pResult, void* param, int32_t code) {
8,655,870✔
1206
  SSqlCallbackWrapper* pWrapper = param;
8,655,870✔
1207
  SRequestObj*         pRequest = pWrapper->pRequest;
8,655,870✔
1208
  STscObj*             pTscObj = pRequest->pTscObj;
8,655,870✔
1209

1210
  pRequest->code = code;
8,655,870✔
1211
  if (pResult) {
8,655,870!
1212
    destroyQueryExecRes(&pRequest->body.resInfo.execRes);
8,658,613✔
1213
    (void)memcpy(&pRequest->body.resInfo.execRes, pResult, sizeof(*pResult));
8,664,781✔
1214
  }
1215

1216
  int32_t type = pRequest->type;
8,662,038✔
1217
  if (TDMT_VND_SUBMIT == type || TDMT_VND_DELETE == type || TDMT_VND_CREATE_TABLE == type) {
8,662,038✔
1218
    if (pResult) {
8,510,540!
1219
      pRequest->body.resInfo.numOfRows += pResult->numOfRows;
8,515,851✔
1220

1221
      // record the insert rows
1222
      if (TDMT_VND_SUBMIT == type) {
8,515,851✔
1223
        SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
8,430,263✔
1224
        (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, pResult->numOfRows);
8,430,263✔
1225
      }
1226
    }
1227
    schedulerFreeJob(&pRequest->body.queryJob, 0);
8,544,288✔
1228
  }
1229

1230
  taosMemoryFree(pResult);
8,691,947!
1231
  tscDebug("req:0x%" PRIx64 ", enter scheduler exec cb, code:%s, QID:0x%" PRIx64, pRequest->self, tstrerror(code),
8,701,993✔
1232
           pRequest->requestId);
1233

1234
  if (code != TSDB_CODE_SUCCESS && NEED_CLIENT_HANDLE_ERROR(code) && pRequest->sqlstr != NULL) {
8,684,892!
1235
    tscDebug("req:0x%" PRIx64 ", client retry to handle the error, code:%s, tryCount:%d, QID:0x%" PRIx64,
213✔
1236
             pRequest->self, tstrerror(code), pRequest->retry, pRequest->requestId);
1237
    if (TSDB_CODE_SUCCESS != removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type))) {
213!
1238
      tscError("req:0x%" PRIx64 ", remove meta failed, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
×
1239
    }
1240
    restartAsyncQuery(pRequest, code);
212✔
1241
    return;
212✔
1242
  }
1243

1244
  tscTrace("req:0x%" PRIx64 ", scheduler exec cb, request type:%s", pRequest->self, TMSG_INFO(pRequest->type));
8,684,679!
1245
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) {
8,684,679!
1246
    if (TSDB_CODE_SUCCESS != removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type))) {
1,740!
1247
      tscError("req:0x%" PRIx64 ", remove meta failed, QID:0x%" PRIx64, pRequest->self, pRequest->requestId);
×
1248
    }
1249
  }
1250

1251
  pRequest->metric.execCostUs = taosGetTimestampUs() - pRequest->metric.execStart;
8,677,488✔
1252
  int32_t code1 = handleQueryExecRsp(pRequest);
8,677,488✔
1253
  if (pRequest->code == TSDB_CODE_SUCCESS && pRequest->code != code1) {
8,694,601!
1254
    pRequest->code = code1;
×
1255
  }
1256

1257
  if (pRequest->code == TSDB_CODE_SUCCESS && NULL != pRequest->pQuery &&
17,393,477!
1258
      incompletaFileParsing(pRequest->pQuery->pRoot)) {
8,699,538✔
1259
    continueInsertFromCsv(pWrapper, pRequest);
×
1260
    return;
×
1261
  }
1262

1263
  if (pRequest->relation.nextRefId) {
8,700,376!
1264
    handlePostSubQuery(pWrapper);
×
1265
  } else {
1266
    destorySqlCallbackWrapper(pWrapper);
8,700,376✔
1267
    pRequest->pWrapper = NULL;
8,703,655✔
1268

1269
    // return to client
1270
    doRequestCallback(pRequest, code);
8,703,655✔
1271
  }
1272
}
1273

1274
void launchQueryImpl(SRequestObj* pRequest, SQuery* pQuery, bool keepQuery, void** res) {
51,483✔
1275
  int32_t code = 0;
51,483✔
1276
  int32_t subplanNum = 0;
51,483✔
1277

1278
  if (pQuery->pRoot) {
51,483✔
1279
    pRequest->stmtType = pQuery->pRoot->type;
50,916✔
1280
  }
1281

1282
  if (pQuery->pRoot && !pRequest->inRetry) {
51,483!
1283
    STscObj*            pTscObj = pRequest->pTscObj;
50,923✔
1284
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
50,923✔
1285
    if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type) {
50,923✔
1286
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1);
50,926✔
1287
    } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) {
5!
1288
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1);
7✔
1289
    }
1290
  }
1291

1292
  pRequest->body.execMode = pQuery->execMode;
51,546✔
1293
  switch (pQuery->execMode) {
51,546!
1294
    case QUERY_EXEC_MODE_LOCAL:
×
1295
      if (!pRequest->validateOnly) {
×
1296
        if (NULL == pQuery->pRoot) {
×
1297
          terrno = TSDB_CODE_INVALID_PARA;
×
1298
          code = terrno;
×
1299
        } else {
1300
          code = execLocalCmd(pRequest, pQuery);
×
1301
        }
1302
      }
1303
      break;
×
1304
    case QUERY_EXEC_MODE_RPC:
607✔
1305
      if (!pRequest->validateOnly) {
607!
1306
        code = execDdlQuery(pRequest, pQuery);
607✔
1307
      }
1308
      break;
607✔
1309
    case QUERY_EXEC_MODE_SCHEDULE: {
50,939✔
1310
      SArray* pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
50,939✔
1311
      if (NULL == pMnodeList) {
50,892!
1312
        code = terrno;
×
1313
        break;
×
1314
      }
1315
      SQueryPlan* pDag = NULL;
50,892✔
1316
      code = getPlan(pRequest, pQuery, &pDag, pMnodeList);
50,892✔
1317
      if (TSDB_CODE_SUCCESS == code) {
50,859!
1318
        pRequest->body.subplanNum = pDag->numOfSubplans;
50,878✔
1319
        if (!pRequest->validateOnly) {
50,878!
1320
          SArray* pNodeList = NULL;
50,914✔
1321
          code = buildSyncExecNodeList(pRequest, &pNodeList, pMnodeList);
50,914✔
1322
          if (TSDB_CODE_SUCCESS == code) {
50,918!
1323
            code = scheduleQuery(pRequest, pDag, pNodeList);
50,930✔
1324
          }
1325
          taosArrayDestroy(pNodeList);
50,902✔
1326
        }
1327
      }
1328
      taosArrayDestroy(pMnodeList);
50,870✔
1329
      break;
50,947✔
1330
    }
1331
    case QUERY_EXEC_MODE_EMPTY_RESULT:
×
1332
      pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
×
1333
      break;
×
1334
    default:
×
1335
      break;
×
1336
  }
1337

1338
  if (!keepQuery) {
51,554!
1339
    qDestroyQuery(pQuery);
×
1340
  }
1341

1342
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) {
51,554!
1343
    int ret = removeMeta(pRequest->pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type));
116!
1344
    if (TSDB_CODE_SUCCESS != ret) {
116!
1345
      tscError("req:0x%" PRIx64 ", remove meta failed,code:%d, QID:0x%" PRIx64, pRequest->self, ret,
×
1346
               pRequest->requestId);
1347
    }
1348
  }
1349

1350
  if (TSDB_CODE_SUCCESS == code) {
51,554✔
1351
    code = handleQueryExecRsp(pRequest);
51,542✔
1352
  }
1353

1354
  if (TSDB_CODE_SUCCESS != code) {
51,568✔
1355
    pRequest->code = code;
108✔
1356
  }
1357

1358
  if (res) {
51,568!
1359
    *res = pRequest->body.resInfo.execRes.res;
×
1360
    pRequest->body.resInfo.execRes.res = NULL;
×
1361
  }
1362
}
51,568✔
1363

1364
static int32_t asyncExecSchQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResultMeta,
8,678,266✔
1365
                                 SSqlCallbackWrapper* pWrapper) {
1366
  int32_t code = TSDB_CODE_SUCCESS;
8,678,266✔
1367
  pRequest->type = pQuery->msgType;
8,678,266✔
1368
  SArray*     pMnodeList = NULL;
8,678,266✔
1369
  SQueryPlan* pDag = NULL;
8,678,266✔
1370
  int64_t     st = taosGetTimestampUs();
8,672,941✔
1371

1372
  if (!pRequest->parseOnly) {
8,672,941!
1373
    pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
8,676,830✔
1374
    if (NULL == pMnodeList) {
8,672,233!
1375
      code = terrno;
×
1376
    }
1377
    SPlanContext cxt = {.queryId = pRequest->requestId,
17,343,781✔
1378
                        .acctId = pRequest->pTscObj->acctId,
8,672,233✔
1379
                        .mgmtEpSet = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp),
8,672,233✔
1380
                        .pAstRoot = pQuery->pRoot,
8,671,548✔
1381
                        .showRewrite = pQuery->showRewrite,
8,671,548✔
1382
                        .isView = pWrapper->pParseCtx->isView,
8,671,548✔
1383
                        .isAudit = pWrapper->pParseCtx->isAudit,
8,671,548✔
1384
                        .pMsg = pRequest->msgBuf,
8,671,548✔
1385
                        .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1386
                        .pUser = pRequest->pTscObj->user,
8,671,548✔
1387
                        .sysInfo = pRequest->pTscObj->sysInfo,
8,671,548✔
1388
                        .timezone = pRequest->pTscObj->optionInfo.timezone,
8,671,548✔
1389
                        .allocatorId = pRequest->allocatorRefId};
8,671,548✔
1390
    if (TSDB_CODE_SUCCESS == code) {
8,671,548!
1391
      code = qCreateQueryPlan(&cxt, &pDag, pMnodeList);
8,676,941✔
1392
    }
1393
    if (code) {
8,632,101✔
1394
      tscError("req:0x%" PRIx64 ", failed to create query plan, code:%s 0x%" PRIx64, pRequest->self, tstrerror(code),
560!
1395
               pRequest->requestId);
1396
    } else {
1397
      pRequest->body.subplanNum = pDag->numOfSubplans;
8,631,541✔
1398
      TSWAP(pRequest->pPostPlan, pDag->pPostPlan);
8,631,541✔
1399
    }
1400
  }
1401

1402
  pRequest->metric.execStart = taosGetTimestampUs();
8,624,764✔
1403
  pRequest->metric.planCostUs = pRequest->metric.execStart - st;
8,624,764✔
1404

1405
  if (TSDB_CODE_SUCCESS == code && !pRequest->validateOnly) {
17,290,619!
1406
    SArray* pNodeList = NULL;
8,649,631✔
1407
    if (QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pQuery->pRoot)) {
8,649,631✔
1408
      code = buildAsyncExecNodeList(pRequest, &pNodeList, pMnodeList, pResultMeta);
201,692✔
1409
    }
1410

1411
    SRequestConnInfo conn = {.pTrans = getAppInfo(pRequest)->pTransporter,
8,649,629✔
1412
                             .requestId = pRequest->requestId,
8,651,284✔
1413
                             .requestObjRefId = pRequest->self};
8,651,284✔
1414
    SSchedulerReq    req = {
17,270,277✔
1415
           .syncReq = false,
1416
           .localReq = (tsQueryPolicy == QUERY_POLICY_CLIENT),
8,651,284✔
1417
           .pConn = &conn,
1418
           .pNodeList = pNodeList,
1419
           .pDag = pDag,
1420
           .allocatorRefId = pRequest->allocatorRefId,
8,651,284✔
1421
           .sql = pRequest->sqlstr,
8,651,284✔
1422
           .startTs = pRequest->metric.start,
8,651,284✔
1423
           .execFp = schedulerExecCb,
1424
           .cbParam = pWrapper,
1425
           .chkKillFp = chkRequestKilled,
1426
           .chkKillParam = (void*)pRequest->self,
8,651,284✔
1427
           .pExecRes = NULL,
1428
           .source = pRequest->source,
8,651,284✔
1429
           .pWorkerCb = getTaskPoolWorkerCb(),
8,651,284✔
1430
    };
1431
    if (TSDB_CODE_SUCCESS == code) {
8,618,993!
1432
      code = schedulerExecJob(&req, &pRequest->body.queryJob);
8,619,517✔
1433
    }
1434

1435
    taosArrayDestroy(pNodeList);
8,671,216✔
1436
  } else {
1437
    qDestroyQueryPlan(pDag);
×
1438
    tscDebug("req:0x%" PRIx64 ", plan not executed, code:%s 0x%" PRIx64, pRequest->self, tstrerror(code),
761!
1439
             pRequest->requestId);
1440
    destorySqlCallbackWrapper(pWrapper);
761✔
1441
    pRequest->pWrapper = NULL;
761✔
1442
    if (TSDB_CODE_SUCCESS != code) {
761✔
1443
      pRequest->code = terrno;
560✔
1444
    }
1445

1446
    doRequestCallback(pRequest, code);
761✔
1447
  }
1448

1449
  // todo not to be released here
1450
  taosArrayDestroy(pMnodeList);
8,666,616✔
1451

1452
  return code;
8,668,475✔
1453
}
1454

1455
void launchAsyncQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResultMeta, SSqlCallbackWrapper* pWrapper) {
8,646,517✔
1456
  int32_t code = 0;
8,646,517✔
1457

1458
  if (pRequest->parseOnly) {
8,646,517✔
1459
    doRequestCallback(pRequest, 0);
627✔
1460
    return;
627✔
1461
  }
1462

1463
  pRequest->body.execMode = pQuery->execMode;
8,645,890✔
1464
  if (QUERY_EXEC_MODE_SCHEDULE != pRequest->body.execMode) {
8,645,890✔
1465
    destorySqlCallbackWrapper(pWrapper);
20,643✔
1466
    pRequest->pWrapper = NULL;
20,597✔
1467
  }
1468

1469
  if (pQuery->pRoot && !pRequest->inRetry) {
8,645,844!
1470
    STscObj*            pTscObj = pRequest->pTscObj;
8,661,509✔
1471
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
8,661,509✔
1472
    if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type &&
8,661,509✔
1473
        (0 == ((SVnodeModifyOpStmt*)pQuery->pRoot)->sqlNodeType)) {
8,480,436✔
1474
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1);
8,446,797✔
1475
    } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) {
214,712✔
1476
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1);
149,021✔
1477
    }
1478
  }
1479

1480
  switch (pQuery->execMode) {
8,720,789!
1481
    case QUERY_EXEC_MODE_LOCAL:
5,109✔
1482
      asyncExecLocalCmd(pRequest, pQuery);
5,109✔
1483
      break;
5,109✔
1484
    case QUERY_EXEC_MODE_RPC:
15,305✔
1485
      code = asyncExecDdlQuery(pRequest, pQuery);
15,305✔
1486
      break;
15,362✔
1487
    case QUERY_EXEC_MODE_SCHEDULE: {
8,700,167✔
1488
      code = asyncExecSchQuery(pRequest, pQuery, pResultMeta, pWrapper);
8,700,167✔
1489
      break;
8,656,064✔
1490
    }
1491
    case QUERY_EXEC_MODE_EMPTY_RESULT:
208✔
1492
      pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
208✔
1493
      doRequestCallback(pRequest, 0);
208✔
1494
      break;
208✔
1495
    default:
×
1496
      tscError("req:0x%" PRIx64 ", invalid execMode %d", pRequest->self, pQuery->execMode);
×
1497
      doRequestCallback(pRequest, -1);
×
1498
      break;
×
1499
  }
1500
}
1501

1502
int32_t refreshMeta(STscObj* pTscObj, SRequestObj* pRequest) {
12✔
1503
  SCatalog* pCatalog = NULL;
12✔
1504
  int32_t   code = 0;
12✔
1505
  int32_t   dbNum = taosArrayGetSize(pRequest->dbList);
12✔
1506
  int32_t   tblNum = taosArrayGetSize(pRequest->tableList);
12✔
1507

1508
  if (dbNum <= 0 && tblNum <= 0) {
12!
1509
    return TSDB_CODE_APP_ERROR;
12✔
1510
  }
1511

1512
  code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
×
1513
  if (code != TSDB_CODE_SUCCESS) {
×
1514
    return code;
×
1515
  }
1516

1517
  SRequestConnInfo conn = {.pTrans = pTscObj->pAppInfo->pTransporter,
×
1518
                           .requestId = pRequest->requestId,
×
1519
                           .requestObjRefId = pRequest->self,
×
1520
                           .mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp)};
×
1521

1522
  for (int32_t i = 0; i < dbNum; ++i) {
×
1523
    char* dbFName = taosArrayGet(pRequest->dbList, i);
×
1524

1525
    // catalogRefreshDBVgInfo will handle dbFName == null.
1526
    code = catalogRefreshDBVgInfo(pCatalog, &conn, dbFName);
×
1527
    if (code != TSDB_CODE_SUCCESS) {
×
1528
      return code;
×
1529
    }
1530
  }
1531

1532
  for (int32_t i = 0; i < tblNum; ++i) {
×
1533
    SName* tableName = taosArrayGet(pRequest->tableList, i);
×
1534

1535
    // catalogRefreshTableMeta will handle tableName == null.
1536
    code = catalogRefreshTableMeta(pCatalog, &conn, tableName, -1);
×
1537
    if (code != TSDB_CODE_SUCCESS) {
×
1538
      return code;
×
1539
    }
1540
  }
1541

1542
  return code;
×
1543
}
1544

1545
int32_t removeMeta(STscObj* pTscObj, SArray* tbList, bool isView) {
2,743✔
1546
  SCatalog* pCatalog = NULL;
2,743✔
1547
  int32_t   tbNum = taosArrayGetSize(tbList);
2,743✔
1548
  int32_t   code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
2,744✔
1549
  if (code != TSDB_CODE_SUCCESS) {
2,744!
1550
    return code;
×
1551
  }
1552

1553
  if (isView) {
2,744✔
1554
    for (int32_t i = 0; i < tbNum; ++i) {
786✔
1555
      SName* pViewName = taosArrayGet(tbList, i);
393✔
1556
      char   dbFName[TSDB_DB_FNAME_LEN];
1557
      if (NULL == pViewName) {
393!
1558
        continue;
×
1559
      }
1560
      (void)tNameGetFullDbName(pViewName, dbFName);
393✔
1561
      TSC_ERR_RET(catalogRemoveViewMeta(pCatalog, dbFName, 0, pViewName->tname, 0));
393!
1562
    }
1563
  } else {
1564
    for (int32_t i = 0; i < tbNum; ++i) {
3,028✔
1565
      SName* pTbName = taosArrayGet(tbList, i);
677✔
1566
      TSC_ERR_RET(catalogRemoveTableMeta(pCatalog, pTbName));
677!
1567
    }
1568
  }
1569

1570
  return TSDB_CODE_SUCCESS;
2,744✔
1571
}
1572

1573
int32_t initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSet) {
15,289✔
1574
  pEpSet->version = 0;
15,289✔
1575

1576
  // init mnode ip set
1577
  SEpSet* mgmtEpSet = &(pEpSet->epSet);
15,289✔
1578
  mgmtEpSet->numOfEps = 0;
15,289✔
1579
  mgmtEpSet->inUse = 0;
15,289✔
1580

1581
  if (firstEp && firstEp[0] != 0) {
15,289!
1582
    if (strlen(firstEp) >= TSDB_EP_LEN) {
15,565!
1583
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1584
      return -1;
×
1585
    }
1586

1587
    int32_t code = taosGetFqdnPortFromEp(firstEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
15,565✔
1588
    if (code != TSDB_CODE_SUCCESS) {
15,549!
1589
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1590
      return terrno;
×
1591
    }
1592
    // uint32_t addr = 0;
1593
    SIpAddr addr = {0};
15,549✔
1594
    code = taosGetIpFromFqdn(tsEnableIpv6, mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
15,549✔
1595
    if (code) {
15,484✔
1596
      tscError("failed to resolve firstEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn,
11✔
1597
               tstrerror(TSDB_CODE_TSC_INVALID_FQDN));
1598
      (void)memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps]));
3✔
1599
    } else {
1600
      mgmtEpSet->numOfEps++;
15,473✔
1601
    }
1602
  }
1603

1604
  if (secondEp && secondEp[0] != 0) {
15,200!
1605
    if (strlen(secondEp) >= TSDB_EP_LEN) {
6,740!
1606
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1607
      return terrno;
×
1608
    }
1609

1610
    int32_t code = taosGetFqdnPortFromEp(secondEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
6,740✔
1611
    if (code != TSDB_CODE_SUCCESS) {
6,740!
1612
      return code;
×
1613
    }
1614
    SIpAddr addr = {0};
6,740✔
1615
    code = taosGetIpFromFqdn(tsEnableIpv6, mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
6,740✔
1616
    if (code) {
6,739!
1617
      tscError("failed to resolve secondEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn,
×
1618
               tstrerror(TSDB_CODE_TSC_INVALID_FQDN));
1619
      (void)memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps]));
×
1620
    } else {
1621
      mgmtEpSet->numOfEps++;
6,739✔
1622
    }
1623
  }
1624

1625
  if (mgmtEpSet->numOfEps == 0) {
15,199✔
1626
    terrno = TSDB_CODE_RPC_NETWORK_UNAVAIL;
3✔
1627
    return TSDB_CODE_RPC_NETWORK_UNAVAIL;
3✔
1628
  }
1629

1630
  return 0;
15,196✔
1631
}
1632

1633
int32_t taosConnectImpl(const char* user, const char* auth, const char* db, __taos_async_fn_t fp, void* param,
15,615✔
1634
                        SAppInstInfo* pAppInfo, int connType, STscObj** pTscObj) {
1635
  *pTscObj = NULL;
15,615✔
1636
  int32_t code = createTscObj(user, auth, db, connType, pAppInfo, pTscObj);
15,615✔
1637
  if (TSDB_CODE_SUCCESS != code) {
15,613!
1638
    return code;
×
1639
  }
1640

1641
  SRequestObj* pRequest = NULL;
15,613✔
1642
  code = createRequest((*pTscObj)->id, TDMT_MND_CONNECT, 0, &pRequest);
15,613✔
1643
  if (TSDB_CODE_SUCCESS != code) {
15,604!
1644
    destroyTscObj(*pTscObj);
×
1645
    return code;
×
1646
  }
1647

1648
  pRequest->sqlstr = taosStrdup("taos_connect");
15,604!
1649
  if (pRequest->sqlstr) {
15,599!
1650
    pRequest->sqlLen = strlen(pRequest->sqlstr);
15,599✔
1651
  } else {
1652
    return terrno;
×
1653
  }
1654

1655
  SMsgSendInfo* body = NULL;
15,599✔
1656
  code = buildConnectMsg(pRequest, &body);
15,599✔
1657
  if (TSDB_CODE_SUCCESS != code) {
15,553!
1658
    destroyTscObj(*pTscObj);
×
1659
    return code;
×
1660
  }
1661

1662
  // int64_t transporterId = 0;
1663
  SEpSet epset = getEpSet_s(&(*pTscObj)->pAppInfo->mgmtEp);
15,553✔
1664
  code = asyncSendMsgToServer((*pTscObj)->pAppInfo->pTransporter, &epset, NULL, body);
15,589✔
1665
  if (TSDB_CODE_SUCCESS != code) {
15,611!
1666
    destroyTscObj(*pTscObj);
×
1667
    tscError("failed to send connect msg to server, code:%s", tstrerror(code));
×
1668
    return code;
×
1669
  }
1670
  if (TSDB_CODE_SUCCESS != tsem_wait(&pRequest->body.rspSem)) {
15,611✔
1671
    destroyTscObj(*pTscObj);
2✔
1672
    tscError("failed to wait sem, code:%s", terrstr());
×
1673
    return terrno;
×
1674
  }
1675
  if (pRequest->code != TSDB_CODE_SUCCESS) {
15,612✔
1676
    const char* errorMsg = (code == TSDB_CODE_RPC_FQDN_ERROR) ? taos_errstr(pRequest) : tstrerror(pRequest->code);
100!
1677
    tscError("failed to connect to server, reason: %s", errorMsg);
100!
1678

1679
    terrno = pRequest->code;
100✔
1680
    destroyRequest(pRequest);
100✔
1681
    taos_close_internal(*pTscObj);
100✔
1682
    *pTscObj = NULL;
100✔
1683
    return terrno;
100✔
1684
  } else {
1685
    tscInfo("conn:0x%" PRIx64 ", connection is opening, connId:%u, dnodeConn:%p, QID:0x%" PRIx64, (*pTscObj)->id,
15,512!
1686
            (*pTscObj)->connId, (*pTscObj)->pAppInfo->pTransporter, pRequest->requestId);
1687
    destroyRequest(pRequest);
15,515✔
1688
  }
1689
  return code;
15,497✔
1690
}
1691

1692
static int32_t buildConnectMsg(SRequestObj* pRequest, SMsgSendInfo** pMsgSendInfo) {
15,585✔
1693
  *pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
15,585!
1694
  if (*pMsgSendInfo == NULL) {
15,605!
1695
    return terrno;
×
1696
  }
1697

1698
  (*pMsgSendInfo)->msgType = TDMT_MND_CONNECT;
15,605✔
1699

1700
  (*pMsgSendInfo)->requestObjRefId = pRequest->self;
15,605✔
1701
  (*pMsgSendInfo)->requestId = pRequest->requestId;
15,605✔
1702
  (*pMsgSendInfo)->fp = getMsgRspHandle((*pMsgSendInfo)->msgType);
15,605✔
1703
  (*pMsgSendInfo)->param = taosMemoryCalloc(1, sizeof(pRequest->self));
15,603!
1704
  if (NULL == (*pMsgSendInfo)->param) {
15,605!
1705
    taosMemoryFree(*pMsgSendInfo);
×
1706
    return terrno;
×
1707
  }
1708

1709
  *(int64_t*)(*pMsgSendInfo)->param = pRequest->self;
15,605✔
1710

1711
  SConnectReq connectReq = {0};
15,605✔
1712
  STscObj*    pObj = pRequest->pTscObj;
15,605✔
1713

1714
  char* db = getDbOfConnection(pObj);
15,605✔
1715
  if (db != NULL) {
15,606✔
1716
    tstrncpy(connectReq.db, db, sizeof(connectReq.db));
5,327✔
1717
  } else if (terrno) {
10,279!
1718
    taosMemoryFree(*pMsgSendInfo);
×
1719
    return terrno;
×
1720
  }
1721
  taosMemoryFreeClear(db);
15,606!
1722

1723
  connectReq.connType = pObj->connType;
15,605✔
1724
  connectReq.pid = appInfo.pid;
15,605✔
1725
  connectReq.startTime = appInfo.startTime;
15,605✔
1726

1727
  tstrncpy(connectReq.app, appInfo.appName, sizeof(connectReq.app));
15,605✔
1728
  tstrncpy(connectReq.user, pObj->user, sizeof(connectReq.user));
15,605✔
1729
  tstrncpy(connectReq.passwd, pObj->pass, sizeof(connectReq.passwd));
15,605✔
1730
  tstrncpy(connectReq.sVer, td_version, sizeof(connectReq.sVer));
15,605✔
1731

1732
  int32_t contLen = tSerializeSConnectReq(NULL, 0, &connectReq);
15,605✔
1733
  void*   pReq = taosMemoryMalloc(contLen);
15,568!
1734
  if (NULL == pReq) {
15,593!
1735
    taosMemoryFree(*pMsgSendInfo);
×
1736
    return terrno;
×
1737
  }
1738

1739
  if (-1 == tSerializeSConnectReq(pReq, contLen, &connectReq)) {
15,593✔
1740
    taosMemoryFree(*pMsgSendInfo);
11!
1741
    taosMemoryFree(pReq);
×
1742
    return terrno;
×
1743
  }
1744

1745
  (*pMsgSendInfo)->msgInfo.len = contLen;
15,556✔
1746
  (*pMsgSendInfo)->msgInfo.pData = pReq;
15,556✔
1747
  return TSDB_CODE_SUCCESS;
15,556✔
1748
}
1749

1750
void updateTargetEpSet(SMsgSendInfo* pSendInfo, STscObj* pTscObj, SRpcMsg* pMsg, SEpSet* pEpSet) {
9,382,901✔
1751
  if (NULL == pEpSet) {
9,382,901✔
1752
    return;
9,351,598✔
1753
  }
1754

1755
  switch (pSendInfo->target.type) {
31,303✔
1756
    case TARGET_TYPE_MNODE:
2✔
1757
      if (NULL == pTscObj) {
2!
1758
        tscError("mnode epset changed but not able to update it, msg:%s, reqObjRefId:%" PRIx64,
×
1759
                 TMSG_INFO(pMsg->msgType), pSendInfo->requestObjRefId);
1760
        return;
×
1761
      }
1762

1763
      SEpSet  originEpset = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
2✔
1764
      SEpSet* pOrig = &originEpset;
2✔
1765
      SEp*    pOrigEp = &pOrig->eps[pOrig->inUse];
2✔
1766
      SEp*    pNewEp = &pEpSet->eps[pEpSet->inUse];
2✔
1767
      tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in client", pOrig->inUse, pOrig->numOfEps,
2!
1768
               pOrigEp->fqdn, pOrigEp->port, pEpSet->inUse, pEpSet->numOfEps, pNewEp->fqdn, pNewEp->port);
1769
      updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, pEpSet);
2✔
1770
      break;
32,143✔
1771
    case TARGET_TYPE_VNODE: {
27,777✔
1772
      if (NULL == pTscObj) {
27,777!
1773
        tscError("vnode epset changed but not able to update it, msg:%s, reqObjRefId:%" PRIx64,
×
1774
                 TMSG_INFO(pMsg->msgType), pSendInfo->requestObjRefId);
1775
        return;
×
1776
      }
1777

1778
      SCatalog* pCatalog = NULL;
27,777✔
1779
      int32_t   code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
27,777✔
1780
      if (code != TSDB_CODE_SUCCESS) {
27,778!
1781
        tscError("fail to get catalog handle, clusterId:0x%" PRIx64 ", error:%s", pTscObj->pAppInfo->clusterId,
×
1782
                 tstrerror(code));
1783
        return;
×
1784
      }
1785

1786
      code = catalogUpdateVgEpSet(pCatalog, pSendInfo->target.dbFName, pSendInfo->target.vgId, pEpSet);
27,778✔
1787
      if (code != TSDB_CODE_SUCCESS) {
27,781!
1788
        tscError("fail to update catalog vg epset, clusterId:0x%" PRIx64 ", error:%s", pTscObj->pAppInfo->clusterId,
×
1789
                 tstrerror(code));
1790
        return;
×
1791
      }
1792
      taosMemoryFreeClear(pSendInfo->target.dbFName);
27,781!
1793
      break;
27,780✔
1794
    }
1795
    default:
3,524✔
1796
      tscDebug("epset changed, not updated, msgType %s", TMSG_INFO(pMsg->msgType));
3,524!
1797
      break;
4,361✔
1798
  }
1799
}
1800

1801
int32_t doProcessMsgFromServerImpl(SRpcMsg* pMsg, SEpSet* pEpSet) {
9,392,156✔
1802
  SMsgSendInfo* pSendInfo = (SMsgSendInfo*)pMsg->info.ahandle;
9,392,156✔
1803
  if (pMsg->info.ahandle == NULL) {
9,392,156✔
1804
    tscError("doProcessMsgFromServer pMsg->info.ahandle == NULL");
150!
1805
    rpcFreeCont(pMsg->pCont);
150✔
1806
    taosMemoryFree(pEpSet);
150!
1807
    return TSDB_CODE_TSC_INTERNAL_ERROR;
150✔
1808
  }
1809

1810
  STscObj* pTscObj = NULL;
9,392,006✔
1811

1812
  STraceId* trace = &pMsg->info.traceId;
9,392,006✔
1813
  char      tbuf[40] = {0};
9,392,006✔
1814
  TRACE_TO_STR(trace, tbuf);
9,392,006!
1815

1816
  tscDebug("QID:%s, process message from server, handle:%p, message:%s, size:%d, code:%s", tbuf, pMsg->info.handle,
9,388,789!
1817
           TMSG_INFO(pMsg->msgType), pMsg->contLen, tstrerror(pMsg->code));
1818

1819
  if (pSendInfo->requestObjRefId != 0) {
9,388,813✔
1820
    SRequestObj* pRequest = (SRequestObj*)taosAcquireRef(clientReqRefPool, pSendInfo->requestObjRefId);
9,149,177✔
1821
    if (pRequest) {
9,146,576✔
1822
      if (pRequest->self != pSendInfo->requestObjRefId) {
9,143,715!
1823
        tscError("doProcessMsgFromServer req:0x%" PRId64 " != pSendInfo->requestObjRefId:0x%" PRId64, pRequest->self,
×
1824
                 pSendInfo->requestObjRefId);
1825

1826
        if (TSDB_CODE_SUCCESS != taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId)) {
×
1827
          tscError("doProcessMsgFromServer taosReleaseRef failed");
×
1828
        }
1829
        rpcFreeCont(pMsg->pCont);
×
1830
        taosMemoryFree(pEpSet);
×
1831
        destroySendMsgInfo(pSendInfo);
×
1832
        return TSDB_CODE_TSC_INTERNAL_ERROR;
×
1833
      }
1834
      pTscObj = pRequest->pTscObj;
9,143,715✔
1835
    }
1836
  }
1837

1838
  updateTargetEpSet(pSendInfo, pTscObj, pMsg, pEpSet);
9,386,212✔
1839

1840
  SDataBuf buf = {.msgType = pMsg->msgType,
9,382,878✔
1841
                  .len = pMsg->contLen,
9,382,878✔
1842
                  .pData = NULL,
1843
                  .handle = pMsg->info.handle,
9,382,878✔
1844
                  .handleRefId = pMsg->info.refId,
9,382,878✔
1845
                  .pEpSet = pEpSet};
1846

1847
  if (pMsg->contLen > 0) {
9,382,878✔
1848
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
9,309,849!
1849
    if (buf.pData == NULL) {
9,313,850!
1850
      pMsg->code = terrno;
×
1851
    } else {
1852
      (void)memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
9,313,850✔
1853
    }
1854
  }
1855

1856
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
9,386,879✔
1857

1858
  if (pTscObj) {
9,376,435✔
1859
    int32_t code = taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId);
9,133,481✔
1860
    if (TSDB_CODE_SUCCESS != code) {
9,141,777!
1861
      tscError("doProcessMsgFromServer taosReleaseRef failed");
×
1862
      terrno = code;
×
1863
      pMsg->code = code;
×
1864
    }
1865
  }
1866

1867
  rpcFreeCont(pMsg->pCont);
9,384,731✔
1868
  destroySendMsgInfo(pSendInfo);
9,390,210✔
1869
  return TSDB_CODE_SUCCESS;
9,390,335✔
1870
}
1871

1872
int32_t doProcessMsgFromServer(void* param) {
9,393,183✔
1873
  AsyncArg* arg = (AsyncArg*)param;
9,393,183✔
1874
  int32_t   code = doProcessMsgFromServerImpl(&arg->msg, arg->pEpset);
9,393,183✔
1875
  taosMemoryFree(arg);
9,389,157!
1876
  return code;
9,391,849✔
1877
}
1878

1879
void processMsgFromServer(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
9,366,595✔
1880
  int32_t code = 0;
9,366,595✔
1881
  SEpSet* tEpSet = NULL;
9,366,595✔
1882

1883
  tscDebug("msg callback, ahandle %p", pMsg->info.ahandle);
9,366,595✔
1884

1885
  if (pEpSet != NULL) {
9,365,862✔
1886
    tEpSet = taosMemoryCalloc(1, sizeof(SEpSet));
32,141!
1887
    if (NULL == tEpSet) {
32,133!
1888
      code = terrno;
×
1889
      pMsg->code = terrno;
×
1890
      goto _exit;
×
1891
    }
1892
    (void)memcpy((void*)tEpSet, (void*)pEpSet, sizeof(SEpSet));
32,133✔
1893
  }
1894

1895
  // pMsg is response msg
1896
  if (pMsg->msgType == TDMT_MND_CONNECT + 1) {
9,365,854✔
1897
    // restore origin code
1898
    if (pMsg->code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
15,597!
1899
      pMsg->code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
×
1900
    } else if (pMsg->code == TSDB_CODE_RPC_SOMENODE_BROKEN_LINK) {
15,597!
1901
      pMsg->code = TSDB_CODE_RPC_BROKEN_LINK;
×
1902
    }
1903
  } else {
1904
    // uniform to one error code: TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED
1905
    if (pMsg->code == TSDB_CODE_RPC_SOMENODE_BROKEN_LINK) {
9,350,257!
1906
      pMsg->code = TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED;
×
1907
    }
1908
  }
1909

1910
  AsyncArg* arg = taosMemoryCalloc(1, sizeof(AsyncArg));
9,365,854!
1911
  if (NULL == arg) {
9,370,202!
1912
    code = terrno;
×
1913
    pMsg->code = code;
×
1914
    goto _exit;
×
1915
  }
1916

1917
  arg->msg = *pMsg;
9,370,202✔
1918
  arg->pEpset = tEpSet;
9,370,202✔
1919

1920
  if ((code = taosAsyncExec(doProcessMsgFromServer, arg, NULL)) != 0) {
9,370,202✔
1921
    pMsg->code = code;
856✔
1922
    taosMemoryFree(arg);
856!
1923
    goto _exit;
×
1924
  }
1925
  return;
9,385,815✔
1926

1927
_exit:
×
1928
  tscError("failed to sched msg to tsc since %s", tstrerror(code));
×
1929
  code = doProcessMsgFromServerImpl(pMsg, tEpSet);
×
1930
  if (code != 0) {
×
1931
    tscError("failed to sched msg to tsc, tsc ready quit");
×
1932
  }
1933
}
1934

1935
TAOS* taos_connect_auth(const char* ip, const char* user, const char* auth, const char* db, uint16_t port) {
4✔
1936
  tscInfo("try to connect to %s:%u by auth, user:%s db:%s", ip, port, user, db);
4!
1937
  if (user == NULL) {
4!
1938
    user = TSDB_DEFAULT_USER;
×
1939
  }
1940

1941
  if (auth == NULL) {
4!
1942
    tscError("No auth info is given, failed to connect to server");
×
1943
    return NULL;
×
1944
  }
1945

1946
  STscObj* pObj = NULL;
4✔
1947
  int32_t  code = taos_connect_internal(ip, user, NULL, auth, db, port, CONN_TYPE__QUERY, &pObj);
4✔
1948
  if (TSDB_CODE_SUCCESS == code) {
4✔
1949
    int64_t* rid = taosMemoryCalloc(1, sizeof(int64_t));
2!
1950
    if (NULL == rid) {
2!
1951
      tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db);
×
1952
    }
1953
    *rid = pObj->id;
2✔
1954
    return (TAOS*)rid;
2✔
1955
  }
1956

1957
  return NULL;
2✔
1958
}
1959

1960
// TAOS* taos_connect_l(const char* ip, int ipLen, const char* user, int userLen, const char* pass, int passLen,
1961
//                      const char* db, int dbLen, uint16_t port) {
1962
//   char ipStr[TSDB_EP_LEN] = {0};
1963
//   char dbStr[TSDB_DB_NAME_LEN] = {0};
1964
//   char userStr[TSDB_USER_LEN] = {0};
1965
//   char passStr[TSDB_PASSWORD_LEN] = {0};
1966
//
1967
//   tstrncpy(ipStr, ip, TMIN(TSDB_EP_LEN - 1, ipLen));
1968
//   tstrncpy(userStr, user, TMIN(TSDB_USER_LEN - 1, userLen));
1969
//   tstrncpy(passStr, pass, TMIN(TSDB_PASSWORD_LEN - 1, passLen));
1970
//   tstrncpy(dbStr, db, TMIN(TSDB_DB_NAME_LEN - 1, dbLen));
1971
//   return taos_connect(ipStr, userStr, passStr, dbStr, port);
1972
// }
1973

1974
void doSetOneRowPtr(SReqResultInfo* pResultInfo) {
3,836,514✔
1975
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
22,428,387✔
1976
    SResultColumn* pCol = &pResultInfo->pCol[i];
18,591,858✔
1977

1978
    int32_t type = pResultInfo->fields[i].type;
18,591,858✔
1979
    int32_t schemaBytes = calcSchemaBytesFromTypeBytes(type, pResultInfo->userFields[i].bytes, false);
18,591,858✔
1980

1981
    if (IS_VAR_DATA_TYPE(type)) {
18,591,873!
1982
      if (!IS_VAR_NULL_TYPE(type, schemaBytes) && pCol->offset[pResultInfo->current] != -1) {
7,521,821!
1983
        char* pStart = pResultInfo->pCol[i].offset[pResultInfo->current] + pResultInfo->pCol[i].pData;
3,452,052✔
1984

1985
        if (IS_STR_DATA_BLOB(type)) {
3,452,052!
1986
          pResultInfo->length[i] = blobDataLen(pStart);
×
1987
          pResultInfo->row[i] = blobDataVal(pStart);
×
1988
        } else {
1989
          pResultInfo->length[i] = varDataLen(pStart);
3,452,053✔
1990
          pResultInfo->row[i] = varDataVal(pStart);
3,452,053✔
1991
        }
1992
      } else {
1993
        pResultInfo->row[i] = NULL;
617,717✔
1994
        pResultInfo->length[i] = 0;
617,717✔
1995
      }
1996
    } else {
1997
      if (!colDataIsNull_f(pCol->nullbitmap, pResultInfo->current)) {
14,522,104✔
1998
        pResultInfo->row[i] = pResultInfo->pCol[i].pData + schemaBytes * pResultInfo->current;
11,619,173✔
1999
        pResultInfo->length[i] = schemaBytes;
11,619,173✔
2000
      } else {
2001
        pResultInfo->row[i] = NULL;
2,902,931✔
2002
        pResultInfo->length[i] = 0;
2,902,931✔
2003
      }
2004
    }
2005
  }
2006
}
3,836,529✔
2007

2008
void* doFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4) {
×
2009
  if (pRequest == NULL) {
×
2010
    return NULL;
×
2011
  }
2012

2013
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
×
2014
  if (pResultInfo->pData == NULL || pResultInfo->current >= pResultInfo->numOfRows) {
×
2015
    // All data has returned to App already, no need to try again
2016
    if (pResultInfo->completed) {
×
2017
      pResultInfo->numOfRows = 0;
×
2018
      return NULL;
×
2019
    }
2020

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

2024
    pRequest->code = schedulerFetchRows(pRequest->body.queryJob, &req);
×
2025
    if (pRequest->code != TSDB_CODE_SUCCESS) {
×
2026
      pResultInfo->numOfRows = 0;
×
2027
      return NULL;
×
2028
    }
2029

2030
    pRequest->code = setQueryResultFromRsp(&pRequest->body.resInfo, (const SRetrieveTableRsp*)pResInfo->pData,
×
2031
                                           convertUcs4, pRequest->isStmtBind);
×
2032
    if (pRequest->code != TSDB_CODE_SUCCESS) {
×
2033
      pResultInfo->numOfRows = 0;
×
2034
      return NULL;
×
2035
    }
2036

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

2041
    STscObj*            pTscObj = pRequest->pTscObj;
×
2042
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
×
2043
    (void)atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen);
×
2044

2045
    if (pResultInfo->numOfRows == 0) {
×
2046
      return NULL;
×
2047
    }
2048
  }
2049

2050
  if (setupOneRowPtr) {
×
2051
    doSetOneRowPtr(pResultInfo);
×
2052
    pResultInfo->current += 1;
×
2053
  }
2054

2055
  return pResultInfo->row;
×
2056
}
2057

2058
static void syncFetchFn(void* param, TAOS_RES* res, int32_t numOfRows) {
143,053✔
2059
  tsem_t* sem = param;
143,053✔
2060
  if (TSDB_CODE_SUCCESS != tsem_post(sem)) {
143,053!
2061
    tscError("failed to post sem, code:%s", terrstr());
×
2062
  }
2063
}
143,053✔
2064

2065
void* doAsyncFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4) {
3,669,898✔
2066
  if (pRequest == NULL) {
3,669,898!
2067
    return NULL;
×
2068
  }
2069

2070
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
3,669,898✔
2071
  if (pResultInfo->pData == NULL || pResultInfo->current >= pResultInfo->numOfRows) {
3,669,898✔
2072
    // All data has returned to App already, no need to try again
2073
    if (pResultInfo->completed) {
172,629✔
2074
      pResultInfo->numOfRows = 0;
29,587✔
2075
      return NULL;
29,587✔
2076
    }
2077

2078
    // convert ucs4 to native multi-bytes string
2079
    pResultInfo->convertUcs4 = convertUcs4;
143,042✔
2080
    tsem_t sem;
2081
    if (TSDB_CODE_SUCCESS != tsem_init(&sem, 0, 0)) {
143,042!
2082
      tscError("failed to init sem, code:%s", terrstr());
×
2083
    }
2084
    taos_fetch_rows_a(pRequest, syncFetchFn, &sem);
143,050✔
2085
    if (TSDB_CODE_SUCCESS != tsem_wait(&sem)) {
143,051!
2086
      tscError("failed to wait sem, code:%s", terrstr());
×
2087
    }
2088
    if (TSDB_CODE_SUCCESS != tsem_destroy(&sem)) {
143,050!
2089
      tscError("failed to destroy sem, code:%s", terrstr());
×
2090
    }
2091
    pRequest->inCallback = false;
143,044✔
2092
  }
2093

2094
  if (pResultInfo->numOfRows == 0 || pRequest->code != TSDB_CODE_SUCCESS) {
3,640,313!
2095
    return NULL;
1,608✔
2096
  } else {
2097
    if (setupOneRowPtr) {
3,638,705✔
2098
      doSetOneRowPtr(pResultInfo);
3,631,313✔
2099
      pResultInfo->current += 1;
3,631,290✔
2100
    }
2101

2102
    return pResultInfo->row;
3,638,682✔
2103
  }
2104
}
2105

2106
static int32_t doPrepareResPtr(SReqResultInfo* pResInfo) {
162,253✔
2107
  if (pResInfo->row == NULL) {
162,253✔
2108
    pResInfo->row = taosMemoryCalloc(pResInfo->numOfCols, POINTER_BYTES);
153,992!
2109
    pResInfo->pCol = taosMemoryCalloc(pResInfo->numOfCols, sizeof(SResultColumn));
154,007!
2110
    pResInfo->length = taosMemoryCalloc(pResInfo->numOfCols, sizeof(int32_t));
154,006!
2111
    pResInfo->convertBuf = taosMemoryCalloc(pResInfo->numOfCols, POINTER_BYTES);
154,008!
2112

2113
    if (pResInfo->row == NULL || pResInfo->pCol == NULL || pResInfo->length == NULL || pResInfo->convertBuf == NULL) {
154,009!
2114
      taosMemoryFree(pResInfo->row);
6!
2115
      taosMemoryFree(pResInfo->pCol);
×
2116
      taosMemoryFree(pResInfo->length);
×
2117
      taosMemoryFree(pResInfo->convertBuf);
×
2118
      return terrno;
×
2119
    }
2120
  }
2121

2122
  return TSDB_CODE_SUCCESS;
162,264✔
2123
}
2124

2125
static int32_t doConvertUCS4(SReqResultInfo* pResultInfo, int32_t* colLength, bool isStmt) {
162,159✔
2126
  int32_t idx = -1;
162,159✔
2127
  iconv_t conv = taosAcquireConv(&idx, C2M, pResultInfo->charsetCxt);
162,159✔
2128
  if (conv == (iconv_t)-1) return TSDB_CODE_TSC_INTERNAL_ERROR;
162,168!
2129

2130
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
554,571✔
2131
    int32_t type = pResultInfo->fields[i].type;
392,394✔
2132
    int32_t schemaBytes =
2133
        calcSchemaBytesFromTypeBytes(pResultInfo->fields[i].type, pResultInfo->fields[i].bytes, isStmt);
392,394✔
2134

2135
    if (type == TSDB_DATA_TYPE_NCHAR && colLength[i] > 0) {
392,403✔
2136
      char* p = taosMemoryRealloc(pResultInfo->convertBuf[i], colLength[i]);
12,405!
2137
      if (p == NULL) {
12,405!
2138
        taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
×
2139
        return terrno;
×
2140
      }
2141

2142
      pResultInfo->convertBuf[i] = p;
12,405✔
2143

2144
      SResultColumn* pCol = &pResultInfo->pCol[i];
12,405✔
2145
      for (int32_t j = 0; j < pResultInfo->numOfRows; ++j) {
5,652,256✔
2146
        if (pCol->offset[j] != -1) {
5,639,851✔
2147
          char* pStart = pCol->offset[j] + pCol->pData;
3,302,321✔
2148

2149
          int32_t len = taosUcs4ToMbsEx((TdUcs4*)varDataVal(pStart), varDataLen(pStart), varDataVal(p), conv);
3,302,321✔
2150
          if (len < 0 || len > schemaBytes || (p + len) >= (pResultInfo->convertBuf[i] + colLength[i])) {
3,302,321!
2151
            tscError(
×
2152
                "doConvertUCS4 error, invalid data. len:%d, bytes:%d, (p + len):%p, (pResultInfo->convertBuf[i] + "
2153
                "colLength[i]):%p",
2154
                len, schemaBytes, (p + len), (pResultInfo->convertBuf[i] + colLength[i]));
2155
            taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
×
2156
            return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2157
          }
2158

2159
          varDataSetLen(p, len);
3,302,321✔
2160
          pCol->offset[j] = (p - pResultInfo->convertBuf[i]);
3,302,321✔
2161
          p += (len + VARSTR_HEADER_SIZE);
3,302,321✔
2162
        }
2163
      }
2164

2165
      pResultInfo->pCol[i].pData = pResultInfo->convertBuf[i];
12,405✔
2166
      pResultInfo->row[i] = pResultInfo->pCol[i].pData;
12,405✔
2167
    }
2168
  }
2169
  taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
162,177✔
2170
  return TSDB_CODE_SUCCESS;
162,168✔
2171
}
2172

2173
static int32_t convertDecimalType(SReqResultInfo* pResultInfo) {
162,160✔
2174
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
554,548✔
2175
    TAOS_FIELD_E* pFieldE = pResultInfo->fields + i;
392,388✔
2176
    TAOS_FIELD*   pField = pResultInfo->userFields + i;
392,388✔
2177
    int32_t       type = pFieldE->type;
392,388✔
2178
    int32_t       bufLen = 0;
392,388✔
2179
    char*         p = NULL;
392,388✔
2180
    if (!IS_DECIMAL_TYPE(type) || !pResultInfo->pCol[i].pData) {
392,388!
2181
      continue;
392,384✔
2182
    } else {
2183
      bufLen = 64;
4✔
2184
      p = taosMemoryRealloc(pResultInfo->convertBuf[i], bufLen * pResultInfo->numOfRows);
4!
2185
      pFieldE->bytes = bufLen;
4✔
2186
      pField->bytes = bufLen;
4✔
2187
    }
2188
    if (!p) return terrno;
4!
2189
    pResultInfo->convertBuf[i] = p;
4✔
2190

2191
    for (int32_t j = 0; j < pResultInfo->numOfRows; ++j) {
8✔
2192
      int32_t code = decimalToStr((DecimalWord*)(pResultInfo->pCol[i].pData + j * tDataTypes[type].bytes), type,
4✔
2193
                                  pFieldE->precision, pFieldE->scale, p, bufLen);
4✔
2194
      p += bufLen;
4✔
2195
      if (TSDB_CODE_SUCCESS != code) {
4!
2196
        return code;
×
2197
      }
2198
    }
2199
    pResultInfo->pCol[i].pData = pResultInfo->convertBuf[i];
4✔
2200
    pResultInfo->row[i] = pResultInfo->pCol[i].pData;
4✔
2201
  }
2202
  return 0;
162,160✔
2203
}
2204

2205
int32_t getVersion1BlockMetaSize(const char* p, int32_t numOfCols) {
58✔
2206
  return sizeof(int32_t) + sizeof(int32_t) + sizeof(int32_t) * 3 + sizeof(uint64_t) +
58✔
2207
         numOfCols * (sizeof(int8_t) + sizeof(int32_t));
2208
}
2209

2210
static int32_t estimateJsonLen(SReqResultInfo* pResultInfo) {
29✔
2211
  char*   p = (char*)pResultInfo->pData;
29✔
2212
  int32_t blockVersion = *(int32_t*)p;
29✔
2213

2214
  int32_t numOfRows = pResultInfo->numOfRows;
29✔
2215
  int32_t numOfCols = pResultInfo->numOfCols;
29✔
2216

2217
  // | version | total length | total rows | total columns | flag seg| block group id | column schema | each column
2218
  // length |
2219
  int32_t cols = *(int32_t*)(p + sizeof(int32_t) * 3);
29✔
2220
  if (numOfCols != cols) {
29!
2221
    tscError("estimateJsonLen error: numOfCols:%d != cols:%d", numOfCols, cols);
×
2222
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2223
  }
2224

2225
  int32_t  len = getVersion1BlockMetaSize(p, numOfCols);
29✔
2226
  int32_t* colLength = (int32_t*)(p + len);
29✔
2227
  len += sizeof(int32_t) * numOfCols;
29✔
2228

2229
  char* pStart = p + len;
29✔
2230
  for (int32_t i = 0; i < numOfCols; ++i) {
142✔
2231
    int32_t colLen = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength[i]) : colLength[i];
113!
2232

2233
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
113✔
2234
      int32_t* offset = (int32_t*)pStart;
57✔
2235
      int32_t  lenTmp = numOfRows * sizeof(int32_t);
57✔
2236
      len += lenTmp;
57✔
2237
      pStart += lenTmp;
57✔
2238

2239
      int32_t estimateColLen = 0;
57✔
2240
      for (int32_t j = 0; j < numOfRows; ++j) {
354✔
2241
        if (offset[j] == -1) {
297✔
2242
          continue;
32✔
2243
        }
2244
        char* data = offset[j] + pStart;
265✔
2245

2246
        int32_t jsonInnerType = *data;
265✔
2247
        char*   jsonInnerData = data + CHAR_BYTES;
265✔
2248
        if (jsonInnerType == TSDB_DATA_TYPE_NULL) {
265!
2249
          estimateColLen += (VARSTR_HEADER_SIZE + strlen(TSDB_DATA_NULL_STR_L));
×
2250
        } else if (tTagIsJson(data)) {
265✔
2251
          estimateColLen += (VARSTR_HEADER_SIZE + ((const STag*)(data))->len);
1✔
2252
        } else if (jsonInnerType == TSDB_DATA_TYPE_NCHAR) {  // value -> "value"
264!
2253
          estimateColLen += varDataTLen(jsonInnerData) + CHAR_BYTES * 2;
264✔
2254
        } else if (jsonInnerType == TSDB_DATA_TYPE_DOUBLE) {
×
2255
          estimateColLen += (VARSTR_HEADER_SIZE + 32);
×
2256
        } else if (jsonInnerType == TSDB_DATA_TYPE_BOOL) {
×
2257
          estimateColLen += (VARSTR_HEADER_SIZE + 5);
×
2258
        } else if (IS_STR_DATA_BLOB(jsonInnerType)) {
×
2259
          estimateColLen += (BLOBSTR_HEADER_SIZE + 32);
×
2260
        } else {
2261
          tscError("estimateJsonLen error: invalid type:%d", jsonInnerType);
×
2262
          return -1;
×
2263
        }
2264
      }
2265
      len += TMAX(colLen, estimateColLen);
57✔
2266
    } else if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
56!
2267
      int32_t lenTmp = numOfRows * sizeof(int32_t);
×
2268
      len += (lenTmp + colLen);
×
2269
      pStart += lenTmp;
×
2270
    } else {
2271
      int32_t lenTmp = BitmapLen(pResultInfo->numOfRows);
56✔
2272
      len += (lenTmp + colLen);
56✔
2273
      pStart += lenTmp;
56✔
2274
    }
2275
    pStart += colLen;
113✔
2276
  }
2277

2278
  // Ensure the complete structure of the block, including the blankfill field,
2279
  // even though it is not used on the client side.
2280
  len += sizeof(bool);
29✔
2281
  return len;
29✔
2282
}
2283

2284
static int32_t doConvertJson(SReqResultInfo* pResultInfo) {
162,269✔
2285
  int32_t numOfRows = pResultInfo->numOfRows;
162,269✔
2286
  int32_t numOfCols = pResultInfo->numOfCols;
162,269✔
2287
  bool    needConvert = false;
162,269✔
2288
  for (int32_t i = 0; i < numOfCols; ++i) {
555,089✔
2289
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
392,849✔
2290
      needConvert = true;
29✔
2291
      break;
29✔
2292
    }
2293
  }
2294

2295
  if (!needConvert) {
162,269✔
2296
    return TSDB_CODE_SUCCESS;
162,241✔
2297
  }
2298

2299
  tscDebug("start to convert form json format string");
28✔
2300

2301
  char*   p = (char*)pResultInfo->pData;
28✔
2302
  int32_t blockVersion = *(int32_t*)p;
28✔
2303
  int32_t dataLen = estimateJsonLen(pResultInfo);
28✔
2304
  if (dataLen <= 0) {
29!
2305
    tscError("doConvertJson error: estimateJsonLen failed");
×
2306
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2307
  }
2308

2309
  taosMemoryFreeClear(pResultInfo->convertJson);
29!
2310
  pResultInfo->convertJson = taosMemoryCalloc(1, dataLen);
29!
2311
  if (pResultInfo->convertJson == NULL) return terrno;
29!
2312
  char* p1 = pResultInfo->convertJson;
29✔
2313

2314
  int32_t totalLen = 0;
29✔
2315
  int32_t cols = *(int32_t*)(p + sizeof(int32_t) * 3);
29✔
2316
  if (numOfCols != cols) {
29!
2317
    tscError("doConvertJson error: numOfCols:%d != cols:%d", numOfCols, cols);
×
2318
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2319
  }
2320

2321
  int32_t len = getVersion1BlockMetaSize(p, numOfCols);
29✔
2322
  (void)memcpy(p1, p, len);
29✔
2323

2324
  p += len;
29✔
2325
  p1 += len;
29✔
2326
  totalLen += len;
29✔
2327

2328
  len = sizeof(int32_t) * numOfCols;
29✔
2329
  int32_t* colLength = (int32_t*)p;
29✔
2330
  int32_t* colLength1 = (int32_t*)p1;
29✔
2331
  (void)memcpy(p1, p, len);
29✔
2332
  p += len;
29✔
2333
  p1 += len;
29✔
2334
  totalLen += len;
29✔
2335

2336
  char* pStart = p;
29✔
2337
  char* pStart1 = p1;
29✔
2338
  for (int32_t i = 0; i < numOfCols; ++i) {
142✔
2339
    int32_t colLen = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength[i]) : colLength[i];
113!
2340
    int32_t colLen1 = (blockVersion == BLOCK_VERSION_1) ? htonl(colLength1[i]) : colLength1[i];
113!
2341
    if (colLen >= dataLen) {
113!
2342
      tscError("doConvertJson error: colLen:%d >= dataLen:%d", colLen, dataLen);
×
2343
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2344
    }
2345
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
113✔
2346
      int32_t* offset = (int32_t*)pStart;
57✔
2347
      int32_t* offset1 = (int32_t*)pStart1;
57✔
2348
      len = numOfRows * sizeof(int32_t);
57✔
2349
      (void)memcpy(pStart1, pStart, len);
57✔
2350
      pStart += len;
57✔
2351
      pStart1 += len;
57✔
2352
      totalLen += len;
57✔
2353

2354
      len = 0;
57✔
2355
      for (int32_t j = 0; j < numOfRows; ++j) {
354✔
2356
        if (offset[j] == -1) {
297✔
2357
          continue;
32✔
2358
        }
2359
        char* data = offset[j] + pStart;
265✔
2360

2361
        int32_t jsonInnerType = *data;
265✔
2362
        char*   jsonInnerData = data + CHAR_BYTES;
265✔
2363
        char    dst[TSDB_MAX_JSON_TAG_LEN] = {0};
265✔
2364
        if (jsonInnerType == TSDB_DATA_TYPE_NULL) {
265!
2365
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%s", TSDB_DATA_NULL_STR_L);
×
2366
          varDataSetLen(dst, strlen(varDataVal(dst)));
×
2367
        } else if (tTagIsJson(data)) {
265✔
2368
          char* jsonString = NULL;
1✔
2369
          parseTagDatatoJson(data, &jsonString, pResultInfo->charsetCxt);
1✔
2370
          if (jsonString == NULL) {
1!
2371
            tscError("doConvertJson error: parseTagDatatoJson failed");
×
2372
            return terrno;
×
2373
          }
2374
          STR_TO_VARSTR(dst, jsonString);
1✔
2375
          taosMemoryFree(jsonString);
1!
2376
        } else if (jsonInnerType == TSDB_DATA_TYPE_NCHAR) {  // value -> "value"
264!
2377
          *(char*)varDataVal(dst) = '\"';
264✔
2378
          int32_t length = taosUcs4ToMbs((TdUcs4*)varDataVal(jsonInnerData), varDataLen(jsonInnerData),
264✔
2379
                                         varDataVal(dst) + CHAR_BYTES, pResultInfo->charsetCxt);
2380
          if (length <= 0) {
264!
2381
            tscError("charset:%s to %s. convert failed.", DEFAULT_UNICODE_ENCODEC,
×
2382
                     pResultInfo->charsetCxt != NULL ? ((SConvInfo*)(pResultInfo->charsetCxt))->charset : tsCharset);
2383
            length = 0;
×
2384
          }
2385
          varDataSetLen(dst, length + CHAR_BYTES * 2);
264✔
2386
          *(char*)POINTER_SHIFT(varDataVal(dst), length + CHAR_BYTES) = '\"';
264✔
2387
        } else if (jsonInnerType == TSDB_DATA_TYPE_DOUBLE) {
×
2388
          double jsonVd = *(double*)(jsonInnerData);
×
2389
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%.9lf", jsonVd);
×
2390
          varDataSetLen(dst, strlen(varDataVal(dst)));
×
2391
        } else if (jsonInnerType == TSDB_DATA_TYPE_BOOL) {
×
2392
          (void)snprintf(varDataVal(dst), TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE, "%s",
×
2393
                         (*((char*)jsonInnerData) == 1) ? "true" : "false");
×
2394
          varDataSetLen(dst, strlen(varDataVal(dst)));
×
2395
        } else {
2396
          tscError("doConvertJson error: invalid type:%d", jsonInnerType);
×
2397
          return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2398
        }
2399

2400
        offset1[j] = len;
265✔
2401
        (void)memcpy(pStart1 + len, dst, varDataTLen(dst));
265✔
2402
        len += varDataTLen(dst);
265✔
2403
      }
2404
      colLen1 = len;
57✔
2405
      totalLen += colLen1;
57✔
2406
      colLength1[i] = (blockVersion == BLOCK_VERSION_1) ? htonl(len) : len;
57!
2407
    } else if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
56!
2408
      len = numOfRows * sizeof(int32_t);
×
2409
      (void)memcpy(pStart1, pStart, len);
×
2410
      pStart += len;
×
2411
      pStart1 += len;
×
2412
      totalLen += len;
×
2413
      totalLen += colLen;
×
2414
      (void)memcpy(pStart1, pStart, colLen);
×
2415
    } else {
2416
      len = BitmapLen(pResultInfo->numOfRows);
56✔
2417
      (void)memcpy(pStart1, pStart, len);
56✔
2418
      pStart += len;
56✔
2419
      pStart1 += len;
56✔
2420
      totalLen += len;
56✔
2421
      totalLen += colLen;
56✔
2422
      (void)memcpy(pStart1, pStart, colLen);
56✔
2423
    }
2424
    pStart += colLen;
113✔
2425
    pStart1 += colLen1;
113✔
2426
  }
2427

2428
  // Ensure the complete structure of the block, including the blankfill field,
2429
  // even though it is not used on the client side.
2430
  // (void)memcpy(pStart1, pStart, sizeof(bool));
2431
  totalLen += sizeof(bool);
29✔
2432

2433
  *(int32_t*)(pResultInfo->convertJson + 4) = totalLen;
29✔
2434
  pResultInfo->pData = pResultInfo->convertJson;
29✔
2435
  return TSDB_CODE_SUCCESS;
29✔
2436
}
2437

2438
int32_t setResultDataPtr(SReqResultInfo* pResultInfo, bool convertUcs4, bool isStmt) {
164,697✔
2439
  bool convertForDecimal = convertUcs4;
164,697✔
2440
  if (pResultInfo == NULL || pResultInfo->numOfCols <= 0 || pResultInfo->fields == NULL) {
164,697!
2441
    tscError("setResultDataPtr paras error");
×
2442
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2443
  }
2444

2445
  if (pResultInfo->numOfRows == 0) {
164,703✔
2446
    return TSDB_CODE_SUCCESS;
2,444✔
2447
  }
2448

2449
  if (pResultInfo->pData == NULL) {
162,259!
2450
    tscError("setResultDataPtr error: pData is NULL");
×
2451
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2452
  }
2453

2454
  int32_t code = doPrepareResPtr(pResultInfo);
162,259✔
2455
  if (code != TSDB_CODE_SUCCESS) {
162,265!
2456
    return code;
×
2457
  }
2458
  code = doConvertJson(pResultInfo);
162,265✔
2459
  if (code != TSDB_CODE_SUCCESS) {
162,268!
2460
    return code;
×
2461
  }
2462

2463
  char* p = (char*)pResultInfo->pData;
162,268✔
2464

2465
  // version:
2466
  int32_t blockVersion = *(int32_t*)p;
162,268✔
2467
  p += sizeof(int32_t);
162,268✔
2468

2469
  int32_t dataLen = *(int32_t*)p;
162,268✔
2470
  p += sizeof(int32_t);
162,268✔
2471

2472
  int32_t rows = *(int32_t*)p;
162,268✔
2473
  p += sizeof(int32_t);
162,268✔
2474

2475
  int32_t cols = *(int32_t*)p;
162,268✔
2476
  p += sizeof(int32_t);
162,268✔
2477

2478
  if (rows != pResultInfo->numOfRows || cols != pResultInfo->numOfCols) {
162,268!
2479
    tscError("setResultDataPtr paras error:rows;%d numOfRows:%" PRId64 " cols:%d numOfCols:%d", rows,
×
2480
             pResultInfo->numOfRows, cols, pResultInfo->numOfCols);
2481
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2482
  }
2483

2484
  int32_t hasColumnSeg = *(int32_t*)p;
162,269✔
2485
  p += sizeof(int32_t);
162,269✔
2486

2487
  uint64_t groupId = taosGetUInt64Aligned((uint64_t*)p);
162,269✔
2488
  p += sizeof(uint64_t);
162,269✔
2489

2490
  // check fields
2491
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
555,176✔
2492
    int8_t type = *(int8_t*)p;
392,905✔
2493
    p += sizeof(int8_t);
392,905✔
2494

2495
    int32_t bytes = *(int32_t*)p;
392,905✔
2496
    p += sizeof(int32_t);
392,905✔
2497

2498
    if (IS_DECIMAL_TYPE(type) && pResultInfo->fields[i].precision == 0) {
392,905!
2499
      extractDecimalTypeInfoFromBytes(&bytes, &pResultInfo->fields[i].precision, &pResultInfo->fields[i].scale);
×
2500
    }
2501
  }
2502

2503
  int32_t* colLength = (int32_t*)p;
162,271✔
2504
  p += sizeof(int32_t) * pResultInfo->numOfCols;
162,271✔
2505

2506
  char* pStart = p;
162,271✔
2507
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
555,171✔
2508
    if ((pStart - pResultInfo->pData) >= dataLen) {
392,895!
2509
      tscError("setResultDataPtr invalid offset over dataLen %d", dataLen);
×
2510
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2511
    }
2512
    if (blockVersion == BLOCK_VERSION_1) {
392,895✔
2513
      colLength[i] = htonl(colLength[i]);
376,508✔
2514
    }
2515
    if (colLength[i] >= dataLen) {
392,895!
2516
      tscError("invalid colLength %d, dataLen %d", colLength[i], dataLen);
×
2517
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2518
    }
2519
    if (IS_INVALID_TYPE(pResultInfo->fields[i].type)) {
392,895!
2520
      tscError("invalid type %d", pResultInfo->fields[i].type);
×
2521
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2522
    }
2523
    if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
392,896!
2524
      pResultInfo->pCol[i].offset = (int32_t*)pStart;
65,408✔
2525
      pStart += pResultInfo->numOfRows * sizeof(int32_t);
65,408✔
2526
    } else {
2527
      pResultInfo->pCol[i].nullbitmap = pStart;
327,488✔
2528
      pStart += BitmapLen(pResultInfo->numOfRows);
327,488✔
2529
    }
2530

2531
    pResultInfo->pCol[i].pData = pStart;
392,896✔
2532
    pResultInfo->length[i] =
785,796✔
2533
        calcSchemaBytesFromTypeBytes(pResultInfo->fields[i].type, pResultInfo->fields[i].bytes, isStmt);
392,896✔
2534
    pResultInfo->row[i] = pResultInfo->pCol[i].pData;
392,900✔
2535

2536
    pStart += colLength[i];
392,900✔
2537
  }
2538

2539
  p = pStart;
162,276✔
2540
  // bool blankFill = *(bool*)p;
2541
  p += sizeof(bool);
162,276✔
2542
  int32_t offset = p - pResultInfo->pData;
162,276✔
2543
  if (offset > dataLen) {
162,276!
2544
    tscError("invalid offset %d, dataLen %d", offset, dataLen);
×
2545
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2546
  }
2547

2548
#ifndef DISALLOW_NCHAR_WITHOUT_ICONV
2549
  if (convertUcs4) {
162,276✔
2550
    code = doConvertUCS4(pResultInfo, colLength, isStmt);
162,168✔
2551
  }
2552
#endif
2553
  if (TSDB_CODE_SUCCESS == code && convertForDecimal) {
162,277✔
2554
    code = convertDecimalType(pResultInfo);
162,169✔
2555
  }
2556
  return code;
162,262✔
2557
}
2558

2559
char* getDbOfConnection(STscObj* pObj) {
8,797,719✔
2560
  terrno = TSDB_CODE_SUCCESS;
8,797,719✔
2561
  char* p = NULL;
8,796,314✔
2562
  (void)taosThreadMutexLock(&pObj->mutex);
8,796,314✔
2563
  size_t len = strlen(pObj->db);
8,805,071✔
2564
  if (len > 0) {
8,805,071✔
2565
    p = taosStrndup(pObj->db, tListLen(pObj->db));
8,673,013!
2566
    if (p == NULL) {
8,662,317!
2567
      tscError("failed to taosStrndup db name");
×
2568
    }
2569
  }
2570

2571
  (void)taosThreadMutexUnlock(&pObj->mutex);
8,794,375✔
2572
  return p;
8,804,200✔
2573
}
2574

2575
void setConnectionDB(STscObj* pTscObj, const char* db) {
4,458✔
2576
  if (db == NULL || pTscObj == NULL) {
4,458!
2577
    tscError("setConnectionDB para is NULL");
×
2578
    return;
×
2579
  }
2580

2581
  (void)taosThreadMutexLock(&pTscObj->mutex);
4,460✔
2582
  tstrncpy(pTscObj->db, db, tListLen(pTscObj->db));
4,459✔
2583
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
4,459✔
2584
}
2585

2586
void resetConnectDB(STscObj* pTscObj) {
×
2587
  if (pTscObj == NULL) {
×
2588
    return;
×
2589
  }
2590

2591
  (void)taosThreadMutexLock(&pTscObj->mutex);
×
2592
  pTscObj->db[0] = 0;
×
2593
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
×
2594
}
2595

2596
int32_t setQueryResultFromRsp(SReqResultInfo* pResultInfo, const SRetrieveTableRsp* pRsp, bool convertUcs4,
159,349✔
2597
                              bool isStmt) {
2598
  if (pResultInfo == NULL || pRsp == NULL) {
159,349!
2599
    tscError("setQueryResultFromRsp paras is null");
×
2600
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2601
  }
2602

2603
  taosMemoryFreeClear(pResultInfo->pRspMsg);
159,360!
2604
  pResultInfo->pRspMsg = (const char*)pRsp;
159,360✔
2605
  pResultInfo->numOfRows = htobe64(pRsp->numOfRows);
159,360✔
2606
  pResultInfo->current = 0;
159,350✔
2607
  pResultInfo->completed = (pRsp->completed == 1);
159,350✔
2608
  pResultInfo->precision = pRsp->precision;
159,350✔
2609

2610
  // decompress data if needed
2611
  int32_t payloadLen = htonl(pRsp->payloadLen);
159,350✔
2612

2613
  if (pRsp->compressed) {
159,350!
2614
    if (pResultInfo->decompBuf == NULL) {
×
2615
      pResultInfo->decompBuf = taosMemoryMalloc(payloadLen);
×
2616
      if (pResultInfo->decompBuf == NULL) {
×
2617
        tscError("failed to prepare the decompress buffer, size:%d", payloadLen);
×
2618
        return terrno;
×
2619
      }
2620
      pResultInfo->decompBufSize = payloadLen;
×
2621
    } else {
2622
      if (pResultInfo->decompBufSize < payloadLen) {
×
2623
        char* p = taosMemoryRealloc(pResultInfo->decompBuf, payloadLen);
×
2624
        if (p == NULL) {
×
2625
          tscError("failed to prepare the decompress buffer, size:%d", payloadLen);
×
2626
          return terrno;
×
2627
        }
2628

2629
        pResultInfo->decompBuf = p;
×
2630
        pResultInfo->decompBufSize = payloadLen;
×
2631
      }
2632
    }
2633
  }
2634

2635
  if (payloadLen > 0) {
159,350✔
2636
    int32_t compLen = *(int32_t*)pRsp->data;
156,903✔
2637
    int32_t rawLen = *(int32_t*)(pRsp->data + sizeof(int32_t));
156,903✔
2638

2639
    char* pStart = (char*)pRsp->data + sizeof(int32_t) * 2;
156,903✔
2640

2641
    if (pRsp->compressed && compLen < rawLen) {
156,903!
2642
      int32_t len = tsDecompressString(pStart, compLen, 1, pResultInfo->decompBuf, rawLen, ONE_STAGE_COMP, NULL, 0);
×
2643
      if (len < 0) {
×
2644
        tscError("tsDecompressString failed");
×
2645
        return terrno ? terrno : TSDB_CODE_FAILED;
×
2646
      }
2647
      if (len != rawLen) {
×
2648
        tscError("tsDecompressString failed, len:%d != rawLen:%d", len, rawLen);
×
2649
        return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2650
      }
2651
      pResultInfo->pData = pResultInfo->decompBuf;
×
2652
      pResultInfo->payloadLen = rawLen;
×
2653
    } else {
2654
      pResultInfo->pData = pStart;
156,903✔
2655
      pResultInfo->payloadLen = htonl(pRsp->compLen);
156,903✔
2656
      if (pRsp->compLen != pRsp->payloadLen) {
156,903!
2657
        tscError("pRsp->compLen:%d != pRsp->payloadLen:%d", pRsp->compLen, pRsp->payloadLen);
×
2658
        return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2659
      }
2660
    }
2661
  }
2662

2663
  // TODO handle the compressed case
2664
  pResultInfo->totalRows += pResultInfo->numOfRows;
159,350✔
2665

2666
  int32_t code = setResultDataPtr(pResultInfo, convertUcs4, isStmt);
159,350✔
2667
  return code;
159,349✔
2668
}
2669

2670
TSDB_SERVER_STATUS taos_check_server_status(const char* fqdn, int port, char* details, int maxlen) {
7✔
2671
  TSDB_SERVER_STATUS code = TSDB_SRV_STATUS_UNAVAILABLE;
7✔
2672
  void*              clientRpc = NULL;
7✔
2673
  SServerStatusRsp   statusRsp = {0};
7✔
2674
  SEpSet             epSet = {.inUse = 0, .numOfEps = 1};
7✔
2675
  SRpcMsg  rpcMsg = {.info.ahandle = (void*)0x9527, .info.notFreeAhandle = 1, .msgType = TDMT_DND_SERVER_STATUS};
7✔
2676
  SRpcMsg  rpcRsp = {0};
7✔
2677
  SRpcInit rpcInit = {0};
7✔
2678
  char     pass[TSDB_PASSWORD_LEN + 1] = {0};
7✔
2679

2680
  rpcInit.label = "CHK";
7✔
2681
  rpcInit.numOfThreads = 1;
7✔
2682
  rpcInit.cfp = NULL;
7✔
2683
  rpcInit.sessions = 16;
7✔
2684
  rpcInit.connType = TAOS_CONN_CLIENT;
7✔
2685
  rpcInit.idleTime = tsShellActivityTimer * 1000;
7✔
2686
  rpcInit.compressSize = tsCompressMsgSize;
7✔
2687
  rpcInit.user = "_dnd";
7✔
2688

2689
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
7✔
2690
  connLimitNum = TMAX(connLimitNum, 10);
7✔
2691
  connLimitNum = TMIN(connLimitNum, 500);
7✔
2692
  rpcInit.connLimitNum = connLimitNum;
7✔
2693
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
7✔
2694
  rpcInit.readTimeout = tsReadTimeout;
7✔
2695
  rpcInit.ipv6 = tsEnableIpv6;
7✔
2696
  if (TSDB_CODE_SUCCESS != taosVersionStrToInt(td_version, &rpcInit.compatibilityVer)) {
7!
2697
    tscError("faild to convert taos version from str to int, errcode:%s", terrstr());
×
2698
    goto _OVER;
×
2699
  }
2700

2701
  clientRpc = rpcOpen(&rpcInit);
7✔
2702
  if (clientRpc == NULL) {
7!
2703
    code = terrno;
×
2704
    tscError("failed to init server status client since %s", tstrerror(code));
×
2705
    goto _OVER;
×
2706
  }
2707

2708
  if (fqdn == NULL) {
7!
2709
    fqdn = tsLocalFqdn;
7✔
2710
  }
2711

2712
  if (port == 0) {
7!
2713
    port = tsServerPort;
7✔
2714
  }
2715

2716
  tstrncpy(epSet.eps[0].fqdn, fqdn, TSDB_FQDN_LEN);
7✔
2717
  epSet.eps[0].port = (uint16_t)port;
7✔
2718
  int32_t ret = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
7✔
2719
  if (TSDB_CODE_SUCCESS != ret) {
7!
2720
    tscError("failed to send recv since %s", tstrerror(ret));
×
2721
    goto _OVER;
×
2722
  }
2723

2724
  if (rpcRsp.code != 0 || rpcRsp.contLen <= 0 || rpcRsp.pCont == NULL) {
7!
2725
    tscError("failed to send server status req since %s", terrstr());
2!
2726
    goto _OVER;
2✔
2727
  }
2728

2729
  if (tDeserializeSServerStatusRsp(rpcRsp.pCont, rpcRsp.contLen, &statusRsp) != 0) {
5!
2730
    tscError("failed to parse server status rsp since %s", terrstr());
×
2731
    goto _OVER;
×
2732
  }
2733

2734
  code = statusRsp.statusCode;
5✔
2735
  if (details != NULL) {
5!
2736
    tstrncpy(details, statusRsp.details, maxlen);
5✔
2737
  }
2738

2739
_OVER:
×
2740
  if (clientRpc != NULL) {
7!
2741
    rpcClose(clientRpc);
7✔
2742
  }
2743
  if (rpcRsp.pCont != NULL) {
7✔
2744
    rpcFreeCont(rpcRsp.pCont);
5✔
2745
  }
2746
  return code;
7✔
2747
}
2748

2749
int32_t appendTbToReq(SHashObj* pHash, int32_t pos1, int32_t len1, int32_t pos2, int32_t len2, const char* str,
×
2750
                      int32_t acctId, char* db) {
2751
  SName name = {0};
×
2752

2753
  if (len1 <= 0) {
×
2754
    return -1;
×
2755
  }
2756

2757
  const char* dbName = db;
×
2758
  const char* tbName = NULL;
×
2759
  int32_t     dbLen = 0;
×
2760
  int32_t     tbLen = 0;
×
2761
  if (len2 > 0) {
×
2762
    dbName = str + pos1;
×
2763
    dbLen = len1;
×
2764
    tbName = str + pos2;
×
2765
    tbLen = len2;
×
2766
  } else {
2767
    dbLen = strlen(db);
×
2768
    tbName = str + pos1;
×
2769
    tbLen = len1;
×
2770
  }
2771

2772
  if (dbLen <= 0 || tbLen <= 0) {
×
2773
    return -1;
×
2774
  }
2775

2776
  if (tNameSetDbName(&name, acctId, dbName, dbLen)) {
×
2777
    return -1;
×
2778
  }
2779

2780
  if (tNameAddTbName(&name, tbName, tbLen)) {
×
2781
    return -1;
×
2782
  }
2783

2784
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
×
2785
  (void)snprintf(dbFName, TSDB_DB_FNAME_LEN, "%d.%.*s", acctId, dbLen, dbName);
×
2786

2787
  STablesReq* pDb = taosHashGet(pHash, dbFName, strlen(dbFName));
×
2788
  if (pDb) {
×
2789
    if (NULL == taosArrayPush(pDb->pTables, &name)) {
×
2790
      return terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
2791
    }
2792
  } else {
2793
    STablesReq db;
2794
    db.pTables = taosArrayInit(20, sizeof(SName));
×
2795
    if (NULL == db.pTables) {
×
2796
      return terrno;
×
2797
    }
2798
    tstrncpy(db.dbFName, dbFName, TSDB_DB_FNAME_LEN);
×
2799
    if (NULL == taosArrayPush(db.pTables, &name)) {
×
2800
      return terrno;
×
2801
    }
2802
    TSC_ERR_RET(taosHashPut(pHash, dbFName, strlen(dbFName), &db, sizeof(db)));
×
2803
  }
2804

2805
  return TSDB_CODE_SUCCESS;
×
2806
}
2807

2808
int32_t transferTableNameList(const char* tbList, int32_t acctId, char* dbName, SArray** pReq) {
×
2809
  SHashObj* pHash = taosHashInit(3, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
×
2810
  if (NULL == pHash) {
×
2811
    return terrno;
×
2812
  }
2813

2814
  bool    inEscape = false;
×
2815
  int32_t code = 0;
×
2816
  void*   pIter = NULL;
×
2817

2818
  int32_t vIdx = 0;
×
2819
  int32_t vPos[2];
2820
  int32_t vLen[2];
2821

2822
  (void)memset(vPos, -1, sizeof(vPos));
×
2823
  (void)memset(vLen, 0, sizeof(vLen));
×
2824

2825
  for (int32_t i = 0;; ++i) {
×
2826
    if (0 == *(tbList + i)) {
×
2827
      if (vPos[vIdx] >= 0 && vLen[vIdx] <= 0) {
×
2828
        vLen[vIdx] = i - vPos[vIdx];
×
2829
      }
2830

2831
      code = appendTbToReq(pHash, vPos[0], vLen[0], vPos[1], vLen[1], tbList, acctId, dbName);
×
2832
      if (code) {
×
2833
        goto _return;
×
2834
      }
2835

2836
      break;
×
2837
    }
2838

2839
    if ('`' == *(tbList + i)) {
×
2840
      inEscape = !inEscape;
×
2841
      if (!inEscape) {
×
2842
        if (vPos[vIdx] >= 0) {
×
2843
          vLen[vIdx] = i - vPos[vIdx];
×
2844
        } else {
2845
          goto _return;
×
2846
        }
2847
      }
2848

2849
      continue;
×
2850
    }
2851

2852
    if (inEscape) {
×
2853
      if (vPos[vIdx] < 0) {
×
2854
        vPos[vIdx] = i;
×
2855
      }
2856
      continue;
×
2857
    }
2858

2859
    if ('.' == *(tbList + i)) {
×
2860
      if (vPos[vIdx] < 0) {
×
2861
        goto _return;
×
2862
      }
2863
      if (vLen[vIdx] <= 0) {
×
2864
        vLen[vIdx] = i - vPos[vIdx];
×
2865
      }
2866
      vIdx++;
×
2867
      if (vIdx >= 2) {
×
2868
        goto _return;
×
2869
      }
2870
      continue;
×
2871
    }
2872

2873
    if (',' == *(tbList + i)) {
×
2874
      if (vPos[vIdx] < 0) {
×
2875
        goto _return;
×
2876
      }
2877
      if (vLen[vIdx] <= 0) {
×
2878
        vLen[vIdx] = i - vPos[vIdx];
×
2879
      }
2880

2881
      code = appendTbToReq(pHash, vPos[0], vLen[0], vPos[1], vLen[1], tbList, acctId, dbName);
×
2882
      if (code) {
×
2883
        goto _return;
×
2884
      }
2885

2886
      (void)memset(vPos, -1, sizeof(vPos));
×
2887
      (void)memset(vLen, 0, sizeof(vLen));
×
2888
      vIdx = 0;
×
2889
      continue;
×
2890
    }
2891

2892
    if (' ' == *(tbList + i) || '\r' == *(tbList + i) || '\t' == *(tbList + i) || '\n' == *(tbList + i)) {
×
2893
      if (vPos[vIdx] >= 0 && vLen[vIdx] <= 0) {
×
2894
        vLen[vIdx] = i - vPos[vIdx];
×
2895
      }
2896
      continue;
×
2897
    }
2898

2899
    if (('a' <= *(tbList + i) && 'z' >= *(tbList + i)) || ('A' <= *(tbList + i) && 'Z' >= *(tbList + i)) ||
×
2900
        ('0' <= *(tbList + i) && '9' >= *(tbList + i)) || ('_' == *(tbList + i))) {
×
2901
      if (vLen[vIdx] > 0) {
×
2902
        goto _return;
×
2903
      }
2904
      if (vPos[vIdx] < 0) {
×
2905
        vPos[vIdx] = i;
×
2906
      }
2907
      continue;
×
2908
    }
2909

2910
    goto _return;
×
2911
  }
2912

2913
  int32_t dbNum = taosHashGetSize(pHash);
×
2914
  *pReq = taosArrayInit(dbNum, sizeof(STablesReq));
×
2915
  if (NULL == pReq) {
×
2916
    TSC_ERR_JRET(terrno);
×
2917
  }
2918
  pIter = taosHashIterate(pHash, NULL);
×
2919
  while (pIter) {
×
2920
    STablesReq* pDb = (STablesReq*)pIter;
×
2921
    if (NULL == taosArrayPush(*pReq, pDb)) {
×
2922
      TSC_ERR_JRET(terrno);
×
2923
    }
2924
    pIter = taosHashIterate(pHash, pIter);
×
2925
  }
2926

2927
  taosHashCleanup(pHash);
×
2928

2929
  return TSDB_CODE_SUCCESS;
×
2930

2931
_return:
×
2932

2933
  terrno = TSDB_CODE_TSC_INVALID_OPERATION;
×
2934

2935
  pIter = taosHashIterate(pHash, NULL);
×
2936
  while (pIter) {
×
2937
    STablesReq* pDb = (STablesReq*)pIter;
×
2938
    taosArrayDestroy(pDb->pTables);
×
2939
    pIter = taosHashIterate(pHash, pIter);
×
2940
  }
2941

2942
  taosHashCleanup(pHash);
×
2943

2944
  return terrno;
×
2945
}
2946

2947
void syncCatalogFn(SMetaData* pResult, void* param, int32_t code) {
×
2948
  SSyncQueryParam* pParam = param;
×
2949
  pParam->pRequest->code = code;
×
2950

2951
  if (TSDB_CODE_SUCCESS != tsem_post(&pParam->sem)) {
×
2952
    tscError("failed to post semaphore since %s", tstrerror(terrno));
×
2953
  }
2954
}
×
2955

2956
void syncQueryFn(void* param, void* res, int32_t code) {
8,723,380✔
2957
  SSyncQueryParam* pParam = param;
8,723,380✔
2958
  pParam->pRequest = res;
8,723,380✔
2959

2960
  if (pParam->pRequest) {
8,723,380✔
2961
    pParam->pRequest->code = code;
8,722,049✔
2962
    clientOperateReport(pParam->pRequest);
8,722,049✔
2963
  }
2964

2965
  if (TSDB_CODE_SUCCESS != tsem_post(&pParam->sem)) {
8,724,242!
2966
    tscError("failed to post semaphore since %s", tstrerror(terrno));
×
2967
  }
2968
}
8,730,286✔
2969

2970
void taosAsyncQueryImpl(uint64_t connId, const char* sql, __taos_async_fn_t fp, void* param, bool validateOnly,
8,715,034✔
2971
                        int8_t source) {
2972
  if (sql == NULL || NULL == fp) {
8,715,034!
2973
    terrno = TSDB_CODE_INVALID_PARA;
×
2974
    if (fp) {
×
2975
      fp(param, NULL, terrno);
×
2976
    }
2977

2978
    return;
×
2979
  }
2980

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

2989
  tscDebug("conn:0x%" PRIx64 ", taos_query execute, sql:%s", connId, sql);
8,724,935✔
2990

2991
  SRequestObj* pRequest = NULL;
8,724,935✔
2992
  int32_t      code = buildRequest(connId, sql, sqlLen, param, validateOnly, &pRequest, 0);
8,724,935✔
2993
  if (code != TSDB_CODE_SUCCESS) {
8,719,675!
2994
    terrno = code;
×
2995
    fp(param, NULL, terrno);
×
2996
    return;
×
2997
  }
2998

2999
  pRequest->source = source;
8,719,675✔
3000
  pRequest->body.queryFp = fp;
8,719,675✔
3001
  doAsyncQuery(pRequest, false);
8,719,675✔
3002
}
3003

3004
void taosAsyncQueryImplWithReqid(uint64_t connId, const char* sql, __taos_async_fn_t fp, void* param, bool validateOnly,
×
3005
                                 int64_t reqid) {
3006
  if (sql == NULL || NULL == fp) {
×
3007
    terrno = TSDB_CODE_INVALID_PARA;
×
3008
    if (fp) {
×
3009
      fp(param, NULL, terrno);
×
3010
    }
3011

3012
    return;
×
3013
  }
3014

3015
  size_t sqlLen = strlen(sql);
×
3016
  if (sqlLen > (size_t)TSDB_MAX_ALLOWED_SQL_LEN) {
×
3017
    tscError("conn:0x%" PRIx64 ", QID:0x%" PRIx64 ", sql string exceeds max length:%d", connId, reqid,
×
3018
             TSDB_MAX_ALLOWED_SQL_LEN);
3019
    terrno = TSDB_CODE_TSC_EXCEED_SQL_LIMIT;
×
3020
    fp(param, NULL, terrno);
×
3021
    return;
×
3022
  }
3023

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

3026
  SRequestObj* pRequest = NULL;
×
3027
  int32_t      code = buildRequest(connId, sql, sqlLen, param, validateOnly, &pRequest, reqid);
×
3028
  if (code != TSDB_CODE_SUCCESS) {
×
3029
    terrno = code;
×
3030
    fp(param, NULL, terrno);
×
3031
    return;
×
3032
  }
3033

3034
  pRequest->body.queryFp = fp;
×
3035
  doAsyncQuery(pRequest, false);
×
3036
}
3037

3038
TAOS_RES* taosQueryImpl(TAOS* taos, const char* sql, bool validateOnly, int8_t source) {
8,712,053✔
3039
  if (NULL == taos) {
8,712,053!
3040
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
3041
    return NULL;
×
3042
  }
3043

3044
  SSyncQueryParam* param = taosMemoryCalloc(1, sizeof(SSyncQueryParam));
8,712,053!
3045
  if (NULL == param) {
8,724,707!
3046
    return NULL;
×
3047
  }
3048
  int32_t code = tsem_init(&param->sem, 0, 0);
8,724,707✔
3049
  if (TSDB_CODE_SUCCESS != code) {
8,721,281!
3050
    taosMemoryFree(param);
×
3051
    return NULL;
×
3052
  }
3053

3054
  taosAsyncQueryImpl(*(int64_t*)taos, sql, syncQueryFn, param, validateOnly, source);
8,721,281✔
3055
  code = tsem_wait(&param->sem);
8,673,979✔
3056
  if (TSDB_CODE_SUCCESS != code) {
8,728,434!
3057
    taosMemoryFree(param);
×
3058
    return NULL;
×
3059
  }
3060
  code = tsem_destroy(&param->sem);
8,728,434✔
3061
  if (TSDB_CODE_SUCCESS != code) {
8,725,595!
3062
    tscError("failed to destroy semaphore since %s", tstrerror(code));
×
3063
  }
3064

3065
  SRequestObj* pRequest = NULL;
8,725,994✔
3066
  if (param->pRequest != NULL) {
8,725,994!
3067
    param->pRequest->syncQuery = true;
8,725,994✔
3068
    pRequest = param->pRequest;
8,725,994✔
3069
    param->pRequest->inCallback = false;
8,725,994✔
3070
  }
3071
  taosMemoryFree(param);
8,725,994!
3072

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

3076
  return pRequest;
8,720,761✔
3077
}
3078

3079
TAOS_RES* taosQueryImplWithReqid(TAOS* taos, const char* sql, bool validateOnly, int64_t reqid) {
×
3080
  if (NULL == taos) {
×
3081
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
3082
    return NULL;
×
3083
  }
3084

3085
  SSyncQueryParam* param = taosMemoryCalloc(1, sizeof(SSyncQueryParam));
×
3086
  if (param == NULL) {
×
3087
    return NULL;
×
3088
  }
3089
  int32_t code = tsem_init(&param->sem, 0, 0);
×
3090
  if (TSDB_CODE_SUCCESS != code) {
×
3091
    taosMemoryFree(param);
×
3092
    return NULL;
×
3093
  }
3094

3095
  taosAsyncQueryImplWithReqid(*(int64_t*)taos, sql, syncQueryFn, param, validateOnly, reqid);
×
3096
  code = tsem_wait(&param->sem);
×
3097
  if (TSDB_CODE_SUCCESS != code) {
×
3098
    taosMemoryFree(param);
×
3099
    return NULL;
×
3100
  }
3101
  SRequestObj* pRequest = NULL;
×
3102
  if (param->pRequest != NULL) {
×
3103
    param->pRequest->syncQuery = true;
×
3104
    pRequest = param->pRequest;
×
3105
  }
3106
  taosMemoryFree(param);
×
3107

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

3111
  return pRequest;
×
3112
}
3113

3114
static void fetchCallback(void* pResult, void* param, int32_t code) {
156,425✔
3115
  SRequestObj* pRequest = (SRequestObj*)param;
156,425✔
3116

3117
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
156,425✔
3118

3119
  tscDebug("req:0x%" PRIx64 ", enter scheduler fetch cb, code:%d - %s, QID:0x%" PRIx64, pRequest->self, code,
156,425✔
3120
           tstrerror(code), pRequest->requestId);
3121

3122
  pResultInfo->pData = pResult;
156,423✔
3123
  pResultInfo->numOfRows = 0;
156,423✔
3124

3125
  if (code != TSDB_CODE_SUCCESS) {
156,423!
3126
    pRequest->code = code;
×
3127
    taosMemoryFreeClear(pResultInfo->pData);
×
3128
    pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, code);
×
3129
    return;
×
3130
  }
3131

3132
  if (pRequest->code != TSDB_CODE_SUCCESS) {
156,423!
3133
    taosMemoryFreeClear(pResultInfo->pData);
×
3134
    pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, pRequest->code);
×
3135
    return;
×
3136
  }
3137

3138
  pRequest->code = setQueryResultFromRsp(pResultInfo, (const SRetrieveTableRsp*)pResultInfo->pData,
312,852✔
3139
                                         pResultInfo->convertUcs4, pRequest->isStmtBind);
156,423✔
3140
  if (pRequest->code != TSDB_CODE_SUCCESS) {
156,429!
3141
    pResultInfo->numOfRows = 0;
×
3142
    tscError("req:0x%" PRIx64 ", fetch results failed, code:%s, QID:0x%" PRIx64, pRequest->self,
×
3143
             tstrerror(pRequest->code), pRequest->requestId);
3144
  } else {
3145
    tscDebug(
156,429✔
3146
        "req:0x%" PRIx64 ", fetch results, numOfRows:%" PRId64 " total Rows:%" PRId64 ", complete:%d, QID:0x%" PRIx64,
3147
        pRequest->self, pResultInfo->numOfRows, pResultInfo->totalRows, pResultInfo->completed, pRequest->requestId);
3148

3149
    STscObj*            pTscObj = pRequest->pTscObj;
156,429✔
3150
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
156,429✔
3151
    (void)atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen);
156,429✔
3152
  }
3153

3154
  pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, pResultInfo->numOfRows);
156,442✔
3155
}
3156

3157
void taosAsyncFetchImpl(SRequestObj* pRequest, __taos_async_fn_t fp, void* param) {
168,976✔
3158
  pRequest->body.fetchFp = fp;
168,976✔
3159
  ((SSyncQueryParam*)pRequest->body.interParam)->userParam = param;
168,976✔
3160

3161
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
168,976✔
3162

3163
  // this query has no results or error exists, return directly
3164
  if (taos_num_fields(pRequest) == 0 || pRequest->code != TSDB_CODE_SUCCESS) {
168,976!
3165
    pResultInfo->numOfRows = 0;
×
3166
    pRequest->body.fetchFp(param, pRequest, pResultInfo->numOfRows);
×
3167
    return;
12,535✔
3168
  }
3169

3170
  // all data has returned to App already, no need to try again
3171
  if (pResultInfo->completed) {
168,976✔
3172
    // it is a local executed query, no need to do async fetch
3173
    if (QUERY_EXEC_MODE_SCHEDULE != pRequest->body.execMode) {
12,535✔
3174
      if (pResultInfo->localResultFetched) {
2,734✔
3175
        pResultInfo->numOfRows = 0;
1,367✔
3176
        pResultInfo->current = 0;
1,367✔
3177
      } else {
3178
        pResultInfo->localResultFetched = true;
1,367✔
3179
      }
3180
    } else {
3181
      pResultInfo->numOfRows = 0;
9,801✔
3182
    }
3183

3184
    pRequest->body.fetchFp(param, pRequest, pResultInfo->numOfRows);
12,535✔
3185
    return;
12,535✔
3186
  }
3187

3188
  SSchedulerReq req = {
156,441✔
3189
      .syncReq = false,
3190
      .fetchFp = fetchCallback,
3191
      .cbParam = pRequest,
3192
  };
3193

3194
  int32_t code = schedulerFetchRows(pRequest->body.queryJob, &req);
156,441✔
3195
  if (TSDB_CODE_SUCCESS != code) {
156,437!
3196
    tscError("0x%" PRIx64 " failed to schedule fetch rows", pRequest->requestId);
×
3197
    // pRequest->body.fetchFp(param, pRequest, code);
3198
  }
3199
}
3200

3201
void doRequestCallback(SRequestObj* pRequest, int32_t code) {
8,722,551✔
3202
  pRequest->inCallback = true;
8,722,551✔
3203
  int64_t this = pRequest->self;
8,722,551✔
3204
  if (tsQueryTbNotExistAsEmpty && TD_RES_QUERY(&pRequest->resType) && pRequest->isQuery &&
8,722,551!
3205
      (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_TDB_TABLE_NOT_EXIST)) {
×
3206
    code = TSDB_CODE_SUCCESS;
×
3207
    pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
×
3208
  }
3209

3210
  tscDebug("QID:0x%" PRIx64 ", taos_query end, req:0x%" PRIx64 ", res:%p", pRequest->requestId, pRequest->self,
8,722,551✔
3211
           pRequest);
3212

3213
  if (pRequest->body.queryFp != NULL) {
8,722,551!
3214
    pRequest->body.queryFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, code);
8,723,404✔
3215
  }
3216

3217
  SRequestObj* pReq = acquireRequest(this);
8,729,596✔
3218
  if (pReq != NULL) {
8,730,853✔
3219
    pReq->inCallback = false;
8,727,470✔
3220
    (void)releaseRequest(this);
8,727,470✔
3221
  }
3222
}
8,724,986✔
3223

3224
int32_t clientParseSql(void* param, const char* dbName, const char* sql, bool parseOnly, const char* effectiveUser,
915✔
3225
                       SParseSqlRes* pRes) {
3226
#ifndef TD_ENTERPRISE
3227
  return TSDB_CODE_SUCCESS;
3228
#else
3229
  return clientParseSqlImpl(param, dbName, sql, parseOnly, effectiveUser, pRes);
915✔
3230
#endif
3231
}
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