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

taosdata / TDengine / #4653

06 Aug 2025 01:23AM UTC coverage: 60.5% (-0.5%) from 60.986%
#4653

push

travis-ci

web-flow
fix: [TD-37190]: disable ignore_nodata_trigger when window type is not interval/sliding or period. (#32405)

138499 of 291677 branches covered (47.48%)

Branch coverage included in aggregate %.

52 of 106 new or added lines in 1 file covered. (49.06%)

1648 existing lines in 119 files now uncovered.

209836 of 284084 relevant lines covered (73.86%)

4732834.84 hits per line

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

53.0
/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) {
44,037✔
39
  SRequestObj* pReq = acquireRequest(rId);
44,037✔
40
  if (pReq != NULL) {
44,041✔
41
    pReq->isQuery = true;
44,039✔
42
    (void)releaseRequest(rId);
44,039✔
43
  }
44
}
44,039✔
45

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

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

56
  return true;
18,956✔
57
}
58

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

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

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

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

71
bool chkRequestKilled(void* param) {
2,108,427✔
72
  bool         killed = false;
2,108,427✔
73
  SRequestObj* pRequest = acquireRequest((int64_t)param);
2,108,427✔
74
  if (NULL == pRequest || pRequest->killed) {
2,109,058!
75
    killed = true;
×
76
  }
77

78
  (void)releaseRequest((int64_t)param);
2,109,058✔
79

80
  return killed;
2,108,762✔
81
}
82

83
void cleanupAppInfo() {
2,517✔
84
  taosHashCleanup(appInfo.pInstMap);
2,517✔
85
  taosHashCleanup(appInfo.pInstMapByClusterId);
2,517✔
86
  tscInfo("cluster instance map cleaned");
2,517!
87
}
2,517✔
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,
7,436✔
93
                              uint16_t port, int connType, STscObj** pObj) {
94
  TSC_ERR_RET(taos_init());
7,436!
95
  if (!validateUserName(user)) {
7,435!
96
    TSC_ERR_RET(TSDB_CODE_TSC_INVALID_USER_LENGTH);
×
97
  }
98
  int32_t code = 0;
7,435✔
99

100
  char localDb[TSDB_DB_NAME_LEN] = {0};
7,435✔
101
  if (db != NULL && strlen(db) > 0) {
7,435✔
102
    if (!validateDbName(db)) {
4,088!
103
      TSC_ERR_RET(TSDB_CODE_TSC_INVALID_DB_LENGTH);
×
104
    }
105

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

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

116
    taosEncryptPass_c((uint8_t*)pass, strlen(pass), secretEncrypt);
7,433✔
117
  } else {
118
    tstrncpy(secretEncrypt, auth, tListLen(secretEncrypt));
3✔
119
  }
120

121
  SCorEpSet epSet = {0};
7,436✔
122
  if (ip) {
7,436✔
123
    TSC_ERR_RET(initEpSetFromCfg(ip, NULL, &epSet));
1,867✔
124
  } else {
125
    TSC_ERR_RET(initEpSetFromCfg(tsFirst, tsSecond, &epSet));
5,569!
126
  }
127

128
  if (port) {
7,432✔
129
    epSet.epSet.eps[0].port = port;
44✔
130
    epSet.epSet.eps[1].port = port;
44✔
131
  }
132

133
  char* key = getClusterKey(user, secretEncrypt, ip, port);
7,432✔
134
  if (NULL == key) {
7,433!
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,
7,433!
138
          user, db, key);
139
  for (int32_t i = 0; i < epSet.epSet.numOfEps; ++i) {
20,435✔
140
    tscInfo("ep:%d, %s:%u", i, epSet.epSet.eps[i].fqdn, epSet.epSet.eps[i].port);
13,002!
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;
7,433✔
152
  code = taosThreadMutexLock(&appInfo.mutex);
7,433✔
153
  if (TSDB_CODE_SUCCESS != code) {
7,433!
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));
7,433✔
159
  SAppInstInfo* p = NULL;
7,433✔
160
  if (pInst == NULL) {
7,433✔
161
    p = taosMemoryCalloc(1, sizeof(struct SAppInstInfo));
2,451!
162
    if (NULL == p) {
2,451!
163
      TSC_ERR_JRET(terrno);
×
164
    }
165
    p->mgmtEp = epSet;
2,451✔
166
    code = taosThreadMutexInit(&p->qnodeMutex, NULL);
2,451✔
167
    if (TSDB_CODE_SUCCESS != code) {
2,451!
168
      taosMemoryFree(p);
×
169
      TSC_ERR_JRET(code);
×
170
    }
171
    code = openTransporter(user, secretEncrypt, tsNumOfCores / 2, &p->pTransporter);
2,451✔
172
    if (TSDB_CODE_SUCCESS != code) {
2,451!
173
      taosMemoryFree(p);
×
174
      TSC_ERR_JRET(code);
×
175
    }
176
    code = appHbMgrInit(p, key, &p->pAppHbMgr);
2,451✔
177
    if (TSDB_CODE_SUCCESS != code) {
2,451!
178
      destroyAppInst(&p);
×
179
      TSC_ERR_JRET(code);
×
180
    }
181
    code = taosHashPut(appInfo.pInstMap, key, strlen(key), &p, POINTER_BYTES);
2,451✔
182
    if (TSDB_CODE_SUCCESS != code) {
2,451!
183
      destroyAppInst(&p);
×
184
      TSC_ERR_JRET(code);
×
185
    }
186
    p->instKey = key;
2,451✔
187
    key = NULL;
2,451✔
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);
2,451!
189

190
    pInst = &p;
2,451✔
191
  } else {
192
    if (NULL == *pInst || NULL == (*pInst)->pAppHbMgr) {
4,982!
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);
4,982✔
198
  }
199

200
_return:
7,433✔
201

202
  if (TSDB_CODE_SUCCESS != code) {
7,433!
203
    (void)taosThreadMutexUnlock(&appInfo.mutex);
×
204
    taosMemoryFreeClear(key);
×
205
    return code;
×
206
  } else {
207
    code = taosThreadMutexUnlock(&appInfo.mutex);
7,433✔
208
    taosMemoryFreeClear(key);
7,433!
209
    if (TSDB_CODE_SUCCESS != code) {
7,433!
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);
7,433✔
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) {
918✔
227
  if (param == NULL) return;
918!
228
  if (TSDB_CODE_SUCCESS != tsem_destroy(&param->sem)) {
918!
229
    tscError("failed to destroy semaphore in freeQueryParam");
×
230
  }
231
  taosMemoryFree(param);
918!
232
}
233

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

242
  (*pRequest)->sqlstr = taosMemoryMalloc(sqlLen + 1);
631,360!
243
  if ((*pRequest)->sqlstr == NULL) {
631,321!
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);
631,321✔
251
  (*pRequest)->sqlstr[sqlLen] = 0;
631,360✔
252
  (*pRequest)->sqlLen = sqlLen;
631,360✔
253
  (*pRequest)->validateOnly = validateSql;
631,360✔
254
  (*pRequest)->stmtBindVersion = 0;
631,360✔
255

256
  ((SSyncQueryParam*)(*pRequest)->body.interParam)->userParam = param;
631,360✔
257

258
  STscObj* pTscObj = (*pRequest)->pTscObj;
631,360✔
259
  int32_t  err = taosHashPut(pTscObj->pRequests, &(*pRequest)->self, sizeof((*pRequest)->self), &(*pRequest)->self,
631,360✔
260
                             sizeof((*pRequest)->self));
261
  if (err) {
631,384!
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;
631,384✔
270
  if (tsQueryUseNodeAllocator && !qIsInsertValuesSql((*pRequest)->sqlstr, (*pRequest)->sqlLen)) {
631,384✔
271
    if (TSDB_CODE_SUCCESS !=
92,470!
272
        nodesCreateAllocator((*pRequest)->requestId, tsQueryNodeChunkSize, &((*pRequest)->allocatorRefId))) {
92,471✔
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);
631,379✔
282
  return TSDB_CODE_SUCCESS;
631,348✔
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) {
11,112✔
298
  STscObj* pTscObj = pRequest->pTscObj;
11,112✔
299

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

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

329
  code = qParseSql(&cxt, pQuery);
11,116✔
330
  if (TSDB_CODE_SUCCESS == code) {
11,110✔
331
    if ((*pQuery)->haveResultSet) {
11,091!
332
      code = setResSchemaInfo(&pRequest->body.resInfo, (*pQuery)->pResSchema, (*pQuery)->numOfResCols,
×
333
                              (*pQuery)->pResExtSchema, pRequest->stmtBindVersion > 0);
×
334
      setResPrecision(&pRequest->body.resInfo, (*pQuery)->precision);
×
335
    }
336
  }
337

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

344
  taosArrayDestroy(cxt.pTableMetaPos);
11,113✔
345
  taosArrayDestroy(cxt.pTableVgroupPos);
11,121✔
346

347
  return code;
11,118✔
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 = setQueryResultFromRsp(&pRequest->body.resInfo, pRsp, pRequest->body.resInfo.convertUcs4,
×
357
                                 pRequest->stmtBindVersion > 0);
×
358
  }
359

360
  return code;
×
361
}
362

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

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

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

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

383
static SAppInstInfo* getAppInfo(SRequestObj* pRequest) { return pRequest->pTscObj->pAppInfo; }
1,235,944✔
384

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

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

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

402
  if (pRequest->code != TSDB_CODE_SUCCESS) {
4,050✔
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(
4,049✔
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);
4,050✔
413
}
414

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

421
  // drop table if exists not_exists_table
422
  if (NULL == pQuery->pCmdMsg) {
10,739!
UNCOV
423
    doRequestCallback(pRequest, 0);
×
UNCOV
424
    return TSDB_CODE_SUCCESS;
×
425
  }
426

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

432
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
10,739✔
433
  SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest);
10,734✔
434

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

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

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

450
  return node1->load > node2->load;
14✔
451
}
452

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

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

469
  return TSDB_CODE_SUCCESS;
2,201✔
470
}
471

472
int32_t qnodeRequired(SRequestObj* pRequest, bool* required) {
619,189✔
473
  if (QUERY_POLICY_VNODE == tsQueryPolicy || QUERY_POLICY_CLIENT == tsQueryPolicy) {
619,189!
474
    *required = false;
619,189✔
475
    return TSDB_CODE_SUCCESS;
619,189✔
476
  }
477

UNCOV
478
  int32_t       code = TSDB_CODE_SUCCESS;
×
UNCOV
479
  SAppInstInfo* pInfo = pRequest->pTscObj->pAppInfo;
×
UNCOV
480
  *required = false;
×
481

UNCOV
482
  TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex));
×
UNCOV
483
  *required = (NULL == pInfo->pQnodeList);
×
UNCOV
484
  TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex));
×
UNCOV
485
  return TSDB_CODE_SUCCESS;
×
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) {
21,091✔
521
  pRequest->type = pQuery->msgType;
21,091✔
522
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
21,091✔
523

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

535
  return qCreateQueryPlan(&cxt, pPlan, pNodeList);
21,112✔
536
}
537

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

545
  pResInfo->numOfCols = numOfCols;
39,223✔
546
  if (pResInfo->fields != NULL) {
39,223✔
547
    taosMemoryFree(pResInfo->fields);
14!
548
  }
549
  if (pResInfo->userFields != NULL) {
39,223✔
550
    taosMemoryFree(pResInfo->userFields);
14!
551
  }
552
  pResInfo->fields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD_E));
39,223!
553
  if (NULL == pResInfo->fields) return terrno;
39,203!
554
  pResInfo->userFields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD));
39,203!
555
  if (NULL == pResInfo->userFields) {
39,213!
556
    taosMemoryFree(pResInfo->fields);
×
557
    return terrno;
×
558
  }
559
  if (numOfCols != pResInfo->numOfCols) {
39,213!
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) {
199,987✔
565
    pResInfo->fields[i].type = pSchema[i].type;
160,766✔
566

567
    pResInfo->userFields[i].type = pSchema[i].type;
160,766✔
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);
160,766✔
570
    pResInfo->fields[i].bytes = calcTypeBytesFromSchemaBytes(pSchema[i].type, pSchema[i].bytes, isStmt);
160,770✔
571
    if (IS_DECIMAL_TYPE(pSchema[i].type) && pExtSchema) {
160,775!
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));
160,774✔
576
    tstrncpy(pResInfo->userFields[i].name, pSchema[i].name, tListLen(pResInfo->userFields[i].name));
160,774✔
577
  }
578
  return TSDB_CODE_SUCCESS;
39,221✔
579
}
580

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

587
  pResInfo->precision = precision;
35,869✔
588
}
589

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

597
  int32_t dbNum = taosArrayGetSize(pDbVgList);
54,567✔
598
  for (int32_t i = 0; i < dbNum; ++i) {
108,661✔
599
    SArray* pVg = taosArrayGetP(pDbVgList, i);
54,078✔
600
    if (NULL == pVg) {
54,099!
601
      continue;
×
602
    }
603
    int32_t vgNum = taosArrayGetSize(pVg);
54,099✔
604
    if (vgNum <= 0) {
54,095✔
605
      continue;
189✔
606
    }
607

608
    for (int32_t j = 0; j < vgNum; ++j) {
253,356✔
609
      SVgroupInfo* pInfo = taosArrayGet(pVg, j);
199,456✔
610
      if (NULL == pInfo) {
199,486!
611
        taosArrayDestroy(nodeList);
×
612
        return TSDB_CODE_OUT_OF_RANGE;
×
613
      }
614
      SQueryNodeLoad load = {0};
199,486✔
615
      load.addr.nodeId = pInfo->vgId;
199,486✔
616
      load.addr.epSet = pInfo->epSet;
199,486✔
617

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

625
  int32_t vnodeNum = taosArrayGetSize(nodeList);
54,583✔
626
  if (vnodeNum > 0) {
54,607✔
627
    tscDebug("0x%" PRIx64 " %s policy, use vnode list, num:%d", pRequest->requestId, policy, vnodeNum);
53,709✔
628
    goto _return;
53,709✔
629
  }
630

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

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

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

649
_return:
85✔
650

651
  *pNodeList = nodeList;
54,584✔
652

653
  return TSDB_CODE_SUCCESS;
54,584✔
654
}
655

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

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

UNCOV
697
  *pNodeList = nodeList;
×
698

UNCOV
699
  return TSDB_CODE_SUCCESS;
×
700
}
701

702
void freeVgList(void* list) {
21,027✔
703
  SArray* pList = *(SArray**)list;
21,027✔
704
  taosArrayDestroy(pList);
21,027✔
705
}
21,053✔
706

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

713
  switch (tsQueryPolicy) {
33,495!
714
    case QUERY_POLICY_VNODE:
33,494✔
715
    case QUERY_POLICY_CLIENT: {
716
      if (pResultMeta) {
33,494!
717
        pDbVgList = taosArrayInit(4, POINTER_BYTES);
33,498✔
718
        if (NULL == pDbVgList) {
33,495!
719
          code = terrno;
×
720
          goto _return;
×
721
        }
722
        int32_t dbNum = taosArrayGetSize(pResultMeta->pDbVgroup);
33,495✔
723
        for (int32_t i = 0; i < dbNum; ++i) {
66,554✔
724
          SMetaRes* pRes = taosArrayGet(pResultMeta->pDbVgroup, i);
33,059✔
725
          if (pRes->code || NULL == pRes->pRes) {
33,058!
726
            continue;
×
727
          }
728

729
          if (NULL == taosArrayPush(pDbVgList, &pRes->pRes)) {
66,125!
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);
33,495✔
774
      break;
33,495✔
775
    }
UNCOV
776
    case QUERY_POLICY_HYBRID:
×
777
    case QUERY_POLICY_QNODE: {
UNCOV
778
      if (pResultMeta && taosArrayGetSize(pResultMeta->pQnodeList) > 0) {
×
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 {
UNCOV
790
        SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
×
UNCOV
791
        TSC_ERR_JRET(taosThreadMutexLock(&pInst->qnodeMutex));
×
UNCOV
792
        if (pInst->pQnodeList) {
×
UNCOV
793
          pQnodeList = taosArrayDup(pInst->pQnodeList, NULL);
×
UNCOV
794
          if (NULL == pQnodeList) {
×
795
            code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
796
            goto _return;
×
797
          }
798
        }
UNCOV
799
        TSC_ERR_JRET(taosThreadMutexUnlock(&pInst->qnodeMutex));
×
800
      }
801

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

810
_return:
33,495✔
811
  taosArrayDestroyEx(pDbVgList, fp);
33,495✔
812
  taosArrayDestroy(pQnodeList);
33,498✔
813

814
  return code;
33,499✔
815
}
816

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

822
  switch (tsQueryPolicy) {
21,043!
823
    case QUERY_POLICY_VNODE:
21,058✔
824
    case QUERY_POLICY_CLIENT: {
825
      int32_t dbNum = taosArrayGetSize(pRequest->dbList);
21,058✔
826
      if (dbNum > 0) {
21,105✔
827
        SCatalog*     pCtg = NULL;
21,039✔
828
        SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo;
21,039✔
829
        code = catalogGetHandle(pInst->clusterId, &pCtg);
21,039✔
830
        if (code != TSDB_CODE_SUCCESS) {
20,993!
831
          goto _return;
×
832
        }
833

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

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

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

860
      code = buildVnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pDbVgList);
21,129✔
861
      break;
21,097✔
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:
21,097✔
876

877
  taosArrayDestroyEx(pDbVgList, freeVgList);
21,097✔
878
  taosArrayDestroy(pQnodeList);
21,063✔
879

880
  return code;
21,074✔
881
}
882

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

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

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

909
  destroyQueryExecRes(&pRequest->body.resInfo.execRes);
21,119✔
910
  (void)memcpy(&pRequest->body.resInfo.execRes, &res, sizeof(res));
21,116✔
911

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

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

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

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

932
  pRequest->code = res.code;
21,118✔
933
  terrno = res.code;
21,118✔
934
  return pRequest->code;
21,116✔
935
}
936

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

944
  int32_t tbNum = taosArrayGetSize(pRsp->aCreateTbRsp);
32,169✔
945
  for (int32_t i = 0; i < tbNum; ++i) {
67,208✔
946
    SVCreateTbRsp* pTbRsp = (SVCreateTbRsp*)taosArrayGet(pRsp->aCreateTbRsp, i);
34,998✔
947
    if (pTbRsp->pMeta) {
34,994✔
948
      TSC_ERR_RET(handleCreateTbExecRes(pTbRsp->pMeta, pCatalog));
32,703!
949
    }
950
  }
951

952
  return TSDB_CODE_SUCCESS;
32,210✔
953
}
954

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

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

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

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

988
  code = catalogChkTbMetaVersion(pCatalog, &conn, pArray);
27,581✔
989

990
_return:
27,576✔
991

992
  taosArrayDestroy(pArray);
27,576✔
993
  return code;
27,578✔
994
}
995

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

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

1004
int32_t handleQueryExecRsp(SRequestObj* pRequest) {
617,502✔
1005
  if (NULL == pRequest->body.resInfo.execRes.res) {
617,502✔
1006
    return pRequest->code;
9,274✔
1007
  }
1008

1009
  SCatalog*     pCatalog = NULL;
608,228✔
1010
  SAppInstInfo* pAppInfo = getAppInfo(pRequest);
608,228✔
1011

1012
  int32_t code = catalogGetHandle(pAppInfo->clusterId, &pCatalog);
608,267✔
1013
  if (code) {
608,248!
1014
    return code;
×
1015
  }
1016

1017
  SEpSet       epset = getEpSet_s(&pAppInfo->mgmtEp);
608,248✔
1018
  SExecResult* pRes = &pRequest->body.resInfo.execRes;
608,301✔
1019

1020
  switch (pRes->msgType) {
608,301✔
1021
    case TDMT_VND_ALTER_TABLE:
924✔
1022
    case TDMT_MND_ALTER_STB: {
1023
      code = handleAlterTbExecRes(pRes->res, pCatalog);
924✔
1024
      break;
924✔
1025
    }
1026
    case TDMT_VND_CREATE_TABLE: {
32,004✔
1027
      SArray* pList = (SArray*)pRes->res;
32,004✔
1028
      int32_t num = taosArrayGetSize(pList);
32,004✔
1029
      for (int32_t i = 0; i < num; ++i) {
68,586✔
1030
        void* res = taosArrayGetP(pList, i);
36,582✔
1031
        // handleCreateTbExecRes will handle res == null
1032
        code = handleCreateTbExecRes(res, pCatalog);
36,577✔
1033
      }
1034
      break;
32,004✔
1035
    }
1036
    case TDMT_MND_CREATE_STB: {
364✔
1037
      code = handleCreateTbExecRes(pRes->res, pCatalog);
364✔
1038
      break;
364✔
1039
    }
1040
    case TDMT_VND_SUBMIT: {
547,380✔
1041
      (void)atomic_add_fetch_64((int64_t*)&pAppInfo->summary.insertBytes, pRes->numOfBytes);
547,380✔
1042

1043
      code = handleSubmitExecRes(pRequest, pRes->res, pCatalog, &epset);
547,395✔
1044
      break;
547,410✔
1045
    }
1046
    case TDMT_SCH_QUERY:
27,575✔
1047
    case TDMT_SCH_MERGE_QUERY: {
1048
      code = handleQueryExecRes(pRequest, pRes->res, pCatalog, &epset);
27,575✔
1049
      break;
27,578✔
1050
    }
1051
    default:
54✔
1052
      tscError("req:0x%" PRIx64 ", invalid exec result for request type:%d, QID:0x%" PRIx64, pRequest->self,
54!
1053
               pRequest->type, pRequest->requestId);
1054
      code = TSDB_CODE_APP_ERROR;
×
1055
  }
1056

1057
  return code;
608,280✔
1058
}
1059

1060
static bool incompletaFileParsing(SNode* pStmt) {
594,857✔
1061
  return QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pStmt) ? false : ((SVnodeModifyOpStmt*)pStmt)->fileProcessing;
594,857!
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) {
2,804✔
1083
  if (pRequest->relation.userRefId == pRequest->self || 0 == pRequest->relation.userRefId) {
2,804!
1084
    // return to client
1085
    doRequestCallback(pRequest, pRequest->code);
2,804✔
1086
    return;
2,804✔
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) {
595,988✔
1206
  SSqlCallbackWrapper* pWrapper = param;
595,988✔
1207
  SRequestObj*         pRequest = pWrapper->pRequest;
595,988✔
1208
  STscObj*             pTscObj = pRequest->pTscObj;
595,988✔
1209

1210
  pRequest->code = code;
595,988✔
1211
  if (pResult) {
595,988!
1212
    destroyQueryExecRes(&pRequest->body.resInfo.execRes);
596,002✔
1213
    (void)memcpy(&pRequest->body.resInfo.execRes, pResult, sizeof(*pResult));
596,004✔
1214
  }
1215

1216
  int32_t type = pRequest->type;
595,990✔
1217
  if (TDMT_VND_SUBMIT == type || TDMT_VND_DELETE == type || TDMT_VND_CREATE_TABLE == type) {
595,990✔
1218
    if (pResult) {
560,927!
1219
      pRequest->body.resInfo.numOfRows += pResult->numOfRows;
560,930✔
1220

1221
      // record the insert rows
1222
      if (TDMT_VND_SUBMIT == type) {
560,930✔
1223
        SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
526,442✔
1224
        (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, pResult->numOfRows);
526,442✔
1225
      }
1226
    }
1227
    schedulerFreeJob(&pRequest->body.queryJob, 0);
560,945✔
1228
  }
1229

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

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

1244
  tscTrace("req:0x%" PRIx64 ", scheduler exec cb, request type:%s", pRequest->self, TMSG_INFO(pRequest->type));
595,935!
1245
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) {
595,935!
1246
    if (TSDB_CODE_SUCCESS != removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type))) {
1,657!
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;
595,945✔
1252
  int32_t code1 = handleQueryExecRsp(pRequest);
595,945✔
1253
  if (pRequest->code == TSDB_CODE_SUCCESS && pRequest->code != code1) {
595,957!
1254
    pRequest->code = code1;
×
1255
  }
1256

1257
  if (pRequest->code == TSDB_CODE_SUCCESS && NULL != pRequest->pQuery &&
1,190,814!
1258
      incompletaFileParsing(pRequest->pQuery->pRoot)) {
594,855✔
1259
    continueInsertFromCsv(pWrapper, pRequest);
×
1260
    return;
×
1261
  }
1262

1263
  if (pRequest->relation.nextRefId) {
595,971!
1264
    handlePostSubQuery(pWrapper);
×
1265
  } else {
1266
    destorySqlCallbackWrapper(pWrapper);
595,971✔
1267
    pRequest->pWrapper = NULL;
595,951✔
1268

1269
    // return to client
1270
    doRequestCallback(pRequest, code);
595,951✔
1271
  }
1272
}
1273

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

1278
  if (pQuery->pRoot) {
21,549✔
1279
    pRequest->stmtType = pQuery->pRoot->type;
21,106✔
1280
  }
1281

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

1292
  pRequest->body.execMode = pQuery->execMode;
21,598✔
1293
  switch (pQuery->execMode) {
21,598!
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:
481✔
1305
      if (!pRequest->validateOnly) {
481!
1306
        code = execDdlQuery(pRequest, pQuery);
481✔
1307
      }
1308
      break;
481✔
1309
    case QUERY_EXEC_MODE_SCHEDULE: {
21,117✔
1310
      SArray* pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
21,117✔
1311
      if (NULL == pMnodeList) {
21,092!
1312
        code = terrno;
×
1313
        break;
×
1314
      }
1315
      SQueryPlan* pDag = NULL;
21,092✔
1316
      code = getPlan(pRequest, pQuery, &pDag, pMnodeList);
21,092✔
1317
      if (TSDB_CODE_SUCCESS == code) {
21,048!
1318
        pRequest->body.subplanNum = pDag->numOfSubplans;
21,062✔
1319
        if (!pRequest->validateOnly) {
21,062!
1320
          SArray* pNodeList = NULL;
21,112✔
1321
          code = buildSyncExecNodeList(pRequest, &pNodeList, pMnodeList);
21,112✔
1322
          if (TSDB_CODE_SUCCESS == code) {
21,077!
1323
            code = scheduleQuery(pRequest, pDag, pNodeList);
21,086✔
1324
          }
1325
          taosArrayDestroy(pNodeList);
21,088✔
1326
        }
1327
      }
1328
      taosArrayDestroy(pMnodeList);
21,043✔
1329
      break;
21,125✔
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) {
21,606!
1339
    qDestroyQuery(pQuery);
×
1340
  }
1341

1342
  if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) {
21,606!
1343
    int ret = removeMeta(pRequest->pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type));
117!
1344
    if (TSDB_CODE_SUCCESS != ret) {
117!
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) {
21,606✔
1351
    code = handleQueryExecRsp(pRequest);
21,593✔
1352
  }
1353

1354
  if (TSDB_CODE_SUCCESS != code) {
21,618✔
1355
    pRequest->code = code;
110✔
1356
  }
1357

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

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

1372
  if (!pRequest->parseOnly) {
596,774!
1373
    pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad));
596,777✔
1374
    if (NULL == pMnodeList) {
596,681!
1375
      code = terrno;
×
1376
    }
1377
    SPlanContext cxt = {.queryId = pRequest->requestId,
1,193,435✔
1378
                        .acctId = pRequest->pTscObj->acctId,
596,681✔
1379
                        .mgmtEpSet = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp),
596,681✔
1380
                        .pAstRoot = pQuery->pRoot,
596,754✔
1381
                        .showRewrite = pQuery->showRewrite,
596,754✔
1382
                        .isView = pWrapper->pParseCtx->isView,
596,754✔
1383
                        .isAudit = pWrapper->pParseCtx->isAudit,
596,754✔
1384
                        .pMsg = pRequest->msgBuf,
596,754✔
1385
                        .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1386
                        .pUser = pRequest->pTscObj->user,
596,754✔
1387
                        .sysInfo = pRequest->pTscObj->sysInfo,
596,754✔
1388
                        .timezone = pRequest->pTscObj->optionInfo.timezone,
596,754✔
1389
                        .allocatorId = pRequest->allocatorRefId};
596,754✔
1390
    if (TSDB_CODE_SUCCESS == code) {
596,754!
1391
      code = qCreateQueryPlan(&cxt, &pDag, pMnodeList);
596,775✔
1392
    }
1393
    if (code) {
596,668✔
1394
      tscError("req:0x%" PRIx64 ", failed to create query plan, code:%s 0x%" PRIx64, pRequest->self, tstrerror(code),
559!
1395
               pRequest->requestId);
1396
    } else {
1397
      pRequest->body.subplanNum = pDag->numOfSubplans;
596,109✔
1398
      TSWAP(pRequest->pPostPlan, pDag->pPostPlan);
596,109✔
1399
    }
1400
  }
1401

1402
  pRequest->metric.execStart = taosGetTimestampUs();
596,752✔
1403
  pRequest->metric.planCostUs = pRequest->metric.execStart - st;
596,752✔
1404

1405
  if (TSDB_CODE_SUCCESS == code && !pRequest->validateOnly) {
1,192,718✔
1406
    SArray* pNodeList = NULL;
595,976✔
1407
    if (QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pQuery->pRoot)) {
595,976✔
1408
      code = buildAsyncExecNodeList(pRequest, &pNodeList, pMnodeList, pResultMeta);
33,502✔
1409
    }
1410

1411
    SRequestConnInfo conn = {.pTrans = getAppInfo(pRequest)->pTransporter,
595,971✔
1412
                             .requestId = pRequest->requestId,
596,003✔
1413
                             .requestObjRefId = pRequest->self};
596,003✔
1414
    SSchedulerReq    req = {
1,191,906✔
1415
           .syncReq = false,
1416
           .localReq = (tsQueryPolicy == QUERY_POLICY_CLIENT),
596,003✔
1417
           .pConn = &conn,
1418
           .pNodeList = pNodeList,
1419
           .pDag = pDag,
1420
           .allocatorRefId = pRequest->allocatorRefId,
596,003✔
1421
           .sql = pRequest->sqlstr,
596,003✔
1422
           .startTs = pRequest->metric.start,
596,003✔
1423
           .execFp = schedulerExecCb,
1424
           .cbParam = pWrapper,
1425
           .chkKillFp = chkRequestKilled,
1426
           .chkKillParam = (void*)pRequest->self,
596,003✔
1427
           .pExecRes = NULL,
1428
           .source = pRequest->source,
596,003✔
1429
           .pWorkerCb = getTaskPoolWorkerCb(),
596,003✔
1430
    };
1431
    if (TSDB_CODE_SUCCESS == code) {
595,903!
1432
      code = schedulerExecJob(&req, &pRequest->body.queryJob);
595,911✔
1433
    }
1434

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

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

1449
  // todo not to be released here
1450
  taosArrayDestroy(pMnodeList);
596,729✔
1451

1452
  return code;
596,742✔
1453
}
1454

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

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

1463
  pRequest->body.execMode = pQuery->execMode;
611,682✔
1464
  if (QUERY_EXEC_MODE_SCHEDULE != pRequest->body.execMode) {
611,682✔
1465
    destorySqlCallbackWrapper(pWrapper);
14,994✔
1466
    pRequest->pWrapper = NULL;
14,993✔
1467
  }
1468

1469
  if (pQuery->pRoot && !pRequest->inRetry) {
611,681!
1470
    STscObj*            pTscObj = pRequest->pTscObj;
611,689✔
1471
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
611,689✔
1472
    if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type &&
611,689✔
1473
        (0 == ((SVnodeModifyOpStmt*)pQuery->pRoot)->sqlNodeType)) {
562,518✔
1474
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1);
526,435✔
1475
    } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) {
85,254✔
1476
      (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1);
33,048✔
1477
    }
1478
  }
1479

1480
  switch (pQuery->execMode) {
611,777!
1481
    case QUERY_EXEC_MODE_LOCAL:
4,077✔
1482
      asyncExecLocalCmd(pRequest, pQuery);
4,077✔
1483
      break;
4,077✔
1484
    case QUERY_EXEC_MODE_RPC:
10,740✔
1485
      code = asyncExecDdlQuery(pRequest, pQuery);
10,740✔
1486
      break;
10,741✔
1487
    case QUERY_EXEC_MODE_SCHEDULE: {
596,784✔
1488
      code = asyncExecSchQuery(pRequest, pQuery, pResultMeta, pWrapper);
596,784✔
1489
      break;
596,712✔
1490
    }
1491
    case QUERY_EXEC_MODE_EMPTY_RESULT:
176✔
1492
      pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
176✔
1493
      doRequestCallback(pRequest, 0);
176✔
1494
      break;
176✔
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,279✔
1546
  SCatalog* pCatalog = NULL;
2,279✔
1547
  int32_t   tbNum = taosArrayGetSize(tbList);
2,279✔
1548
  int32_t   code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
2,279✔
1549
  if (code != TSDB_CODE_SUCCESS) {
2,279!
1550
    return code;
×
1551
  }
1552

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

1570
  return TSDB_CODE_SUCCESS;
2,279✔
1571
}
1572

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

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

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

1587
    int32_t code = taosGetFqdnPortFromEp(firstEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
7,436✔
1588
    if (code != TSDB_CODE_SUCCESS) {
7,435!
1589
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1590
      return terrno;
×
1591
    }
1592
    // uint32_t addr = 0;
1593
    SIpAddr addr = {0};
7,435✔
1594
    code = taosGetIpFromFqdn(tsEnableIpv6, mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
7,435✔
1595
    if (code) {
7,435✔
1596
      tscError("failed to resolve firstEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn,
3!
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++;
7,432✔
1601
    }
1602
  }
1603

1604
  if (secondEp && secondEp[0] != 0) {
7,434!
1605
    if (strlen(secondEp) >= TSDB_EP_LEN) {
5,568!
1606
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
1607
      return terrno;
×
1608
    }
1609

1610
    int32_t code = taosGetFqdnPortFromEp(secondEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]);
5,568✔
1611
    if (code != TSDB_CODE_SUCCESS) {
5,568!
1612
      return code;
×
1613
    }
1614
    SIpAddr addr = {0};
5,568✔
1615
    code = taosGetIpFromFqdn(tsEnableIpv6, mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr);
5,568✔
1616
    if (code) {
5,568!
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++;
5,568✔
1622
    }
1623
  }
1624

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

1630
  return 0;
7,431✔
1631
}
1632

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

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

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

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

1662
  // int64_t transporterId = 0;
1663
  SEpSet epset = getEpSet_s(&(*pTscObj)->pAppInfo->mgmtEp);
7,430✔
1664
  code = asyncSendMsgToServer((*pTscObj)->pAppInfo->pTransporter, &epset, NULL, body);
7,433✔
1665
  if (TSDB_CODE_SUCCESS != code) {
7,433!
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)) {
7,433!
1671
    destroyTscObj(*pTscObj);
×
1672
    tscError("failed to wait sem, code:%s", terrstr());
×
1673
    return terrno;
×
1674
  }
1675
  if (pRequest->code != TSDB_CODE_SUCCESS) {
7,433✔
1676
    const char* errorMsg = (code == TSDB_CODE_RPC_FQDN_ERROR) ? taos_errstr(pRequest) : tstrerror(pRequest->code);
30!
1677
    tscError("failed to connect to server, reason: %s", errorMsg);
30!
1678

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

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

1698
  (*pMsgSendInfo)->msgType = TDMT_MND_CONNECT;
7,433✔
1699

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

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

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

1714
  char* db = getDbOfConnection(pObj);
7,433✔
1715
  if (db != NULL) {
7,433✔
1716
    tstrncpy(connectReq.db, db, sizeof(connectReq.db));
4,087✔
1717
  } else if (terrno) {
3,346!
1718
    taosMemoryFree(*pMsgSendInfo);
×
1719
    return terrno;
×
1720
  }
1721
  taosMemoryFreeClear(db);
7,432!
1722

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

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

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

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

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

1750
void updateTargetEpSet(SMsgSendInfo* pSendInfo, STscObj* pTscObj, SRpcMsg* pMsg, SEpSet* pEpSet) {
870,538✔
1751
  if (NULL == pEpSet) {
870,538✔
1752
    return;
844,516✔
1753
  }
1754

1755
  switch (pSendInfo->target.type) {
26,022!
1756
    case TARGET_TYPE_MNODE:
×
1757
      if (NULL == pTscObj) {
×
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);
×
1764
      SEpSet* pOrig = &originEpset;
×
1765
      SEp*    pOrigEp = &pOrig->eps[pOrig->inUse];
×
1766
      SEp*    pNewEp = &pEpSet->eps[pEpSet->inUse];
×
1767
      tscDebug("mnode epset updated from %d/%d=>%s:%d to %d/%d=>%s:%d in client", pOrig->inUse, pOrig->numOfEps,
×
1768
               pOrigEp->fqdn, pOrigEp->port, pEpSet->inUse, pEpSet->numOfEps, pNewEp->fqdn, pNewEp->port);
1769
      updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, pEpSet);
×
1770
      break;
26,023✔
1771
    case TARGET_TYPE_VNODE: {
25,612✔
1772
      if (NULL == pTscObj) {
25,612!
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;
25,612✔
1779
      int32_t   code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog);
25,612✔
1780
      if (code != TSDB_CODE_SUCCESS) {
25,607!
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);
25,607✔
1787
      if (code != TSDB_CODE_SUCCESS) {
25,614!
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);
25,614!
1793
      break;
25,612✔
1794
    }
1795
    default:
410✔
1796
      tscDebug("epset changed, not updated, msgType %s", TMSG_INFO(pMsg->msgType));
410!
1797
      break;
411✔
1798
  }
1799
}
1800

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

1810
  STscObj* pTscObj = NULL;
870,568✔
1811

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

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

1819
  if (pSendInfo->requestObjRefId != 0) {
870,561✔
1820
    SRequestObj* pRequest = (SRequestObj*)taosAcquireRef(clientReqRefPool, pSendInfo->requestObjRefId);
806,079✔
1821
    if (pRequest) {
806,067✔
1822
      if (pRequest->self != pSendInfo->requestObjRefId) {
805,335!
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;
805,335✔
1835
    }
1836
  }
1837

1838
  updateTargetEpSet(pSendInfo, pTscObj, pMsg, pEpSet);
870,549✔
1839

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

1847
  if (pMsg->contLen > 0) {
870,537✔
1848
    buf.pData = taosMemoryCalloc(1, pMsg->contLen);
853,023!
1849
    if (buf.pData == NULL) {
853,012!
1850
      pMsg->code = terrno;
×
1851
    } else {
1852
      (void)memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
853,012✔
1853
    }
1854
  }
1855

1856
  (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
870,526✔
1857

1858
  if (pTscObj) {
870,506✔
1859
    int32_t code = taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId);
805,299✔
1860
    if (TSDB_CODE_SUCCESS != code) {
805,297!
1861
      tscError("doProcessMsgFromServer taosReleaseRef failed");
×
1862
      terrno = code;
×
1863
      pMsg->code = code;
×
1864
    }
1865
  }
1866

1867
  rpcFreeCont(pMsg->pCont);
870,504✔
1868
  destroySendMsgInfo(pSendInfo);
870,537✔
1869
  return TSDB_CODE_SUCCESS;
870,497✔
1870
}
1871

1872
int32_t doProcessMsgFromServer(void* param) {
870,756✔
1873
  AsyncArg* arg = (AsyncArg*)param;
870,756✔
1874
  int32_t   code = doProcessMsgFromServerImpl(&arg->msg, arg->pEpset);
870,756✔
1875
  taosMemoryFree(arg);
870,673!
1876
  return code;
870,686✔
1877
}
1878

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

1883
  tscDebug("msg callback, ahandle %p", pMsg->info.ahandle);
870,613✔
1884

1885
  if (pEpSet != NULL) {
870,640✔
1886
    tEpSet = taosMemoryCalloc(1, sizeof(SEpSet));
26,019!
1887
    if (NULL == tEpSet) {
26,011!
1888
      code = terrno;
×
1889
      pMsg->code = terrno;
×
1890
      goto _exit;
×
1891
    }
1892
    (void)memcpy((void*)tEpSet, (void*)pEpSet, sizeof(SEpSet));
26,011✔
1893
  }
1894

1895
  // pMsg is response msg
1896
  if (pMsg->msgType == TDMT_MND_CONNECT + 1) {
870,632✔
1897
    // restore origin code
1898
    if (pMsg->code == TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED) {
7,433!
1899
      pMsg->code = TSDB_CODE_RPC_NETWORK_UNAVAIL;
×
1900
    } else if (pMsg->code == TSDB_CODE_RPC_SOMENODE_BROKEN_LINK) {
7,433!
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) {
863,199!
1906
      pMsg->code = TSDB_CODE_RPC_SOMENODE_NOT_CONNECTED;
×
1907
    }
1908
  }
1909

1910
  AsyncArg* arg = taosMemoryCalloc(1, sizeof(AsyncArg));
870,632!
1911
  if (NULL == arg) {
870,681!
1912
    code = terrno;
×
1913
    pMsg->code = code;
×
1914
    goto _exit;
×
1915
  }
1916

1917
  arg->msg = *pMsg;
870,681✔
1918
  arg->pEpset = tEpSet;
870,681✔
1919

1920
  if ((code = taosAsyncExec(doProcessMsgFromServer, arg, NULL)) != 0) {
870,681✔
1921
    pMsg->code = code;
12✔
1922
    taosMemoryFree(arg);
12!
1923
    goto _exit;
×
1924
  }
1925
  return;
870,721✔
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) {
3✔
1936
  tscInfo("try to connect to %s:%u by auth, user:%s db:%s", ip, port, user, db);
3!
1937
  if (user == NULL) {
3!
1938
    user = TSDB_DEFAULT_USER;
×
1939
  }
1940

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

1946
  STscObj* pObj = NULL;
3✔
1947
  int32_t  code = taos_connect_internal(ip, user, NULL, auth, db, port, CONN_TYPE__QUERY, &pObj);
3✔
1948
  if (TSDB_CODE_SUCCESS == code) {
3✔
1949
    int64_t* rid = taosMemoryCalloc(1, sizeof(int64_t));
1!
1950
    if (NULL == rid) {
1!
1951
      tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db);
×
1952
    }
1953
    *rid = pObj->id;
1✔
1954
    return (TAOS*)rid;
1✔
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,039,682✔
1975
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
17,164,698✔
1976
    SResultColumn* pCol = &pResultInfo->pCol[i];
14,125,045✔
1977

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

1981
    if (IS_VAR_DATA_TYPE(type)) {
14,125,016!
1982
      if (!IS_VAR_NULL_TYPE(type, schemaBytes) && pCol->offset[pResultInfo->current] != -1) {
5,888,900!
1983
        char* pStart = pResultInfo->pCol[i].offset[pResultInfo->current] + pResultInfo->pCol[i].pData;
2,836,860✔
1984

1985
        if (IS_STR_DATA_BLOB(type)) {
2,836,860!
1986
          pResultInfo->length[i] = blobDataLen(pStart);
9✔
1987
          pResultInfo->row[i] = blobDataVal(pStart);
9✔
1988
        } else {
1989
          pResultInfo->length[i] = varDataLen(pStart);
2,836,851✔
1990
          pResultInfo->row[i] = varDataVal(pStart);
2,836,851✔
1991
        }
1992
      } else {
1993
        pResultInfo->row[i] = NULL;
215,180✔
1994
        pResultInfo->length[i] = 0;
215,180✔
1995
      }
1996
    } else {
1997
      if (!colDataIsNull_f(pCol, pResultInfo->current)) {
11,072,976✔
1998
        pResultInfo->row[i] = pResultInfo->pCol[i].pData + schemaBytes * pResultInfo->current;
9,347,024✔
1999
        pResultInfo->length[i] = schemaBytes;
9,347,024✔
2000
      } else {
2001
        pResultInfo->row[i] = NULL;
1,725,952✔
2002
        pResultInfo->length[i] = 0;
1,725,952✔
2003
      }
2004
    }
2005
  }
2006
}
3,039,653✔
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->stmtBindVersion > 0);
×
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) {
30,607✔
2059
  tsem_t* sem = param;
30,607✔
2060
  if (TSDB_CODE_SUCCESS != tsem_post(sem)) {
30,607!
2061
    tscError("failed to post sem, code:%s", terrstr());
×
2062
  }
2063
}
30,608✔
2064

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

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

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

2094
  if (pResultInfo->numOfRows == 0 || pRequest->code != TSDB_CODE_SUCCESS) {
3,035,774!
2095
    return NULL;
1,220✔
2096
  } else {
2097
    if (setupOneRowPtr) {
3,034,554✔
2098
      doSetOneRowPtr(pResultInfo);
3,034,480✔
2099
      pResultInfo->current += 1;
3,034,472✔
2100
    }
2101

2102
    return pResultInfo->row;
3,034,546✔
2103
  }
2104
}
2105

2106
static int32_t doPrepareResPtr(SReqResultInfo* pResInfo) {
42,109✔
2107
  if (pResInfo->row == NULL) {
42,109✔
2108
    pResInfo->row = taosMemoryCalloc(pResInfo->numOfCols, POINTER_BYTES);
36,380!
2109
    pResInfo->pCol = taosMemoryCalloc(pResInfo->numOfCols, sizeof(SResultColumn));
36,380!
2110
    pResInfo->length = taosMemoryCalloc(pResInfo->numOfCols, sizeof(int32_t));
36,380!
2111
    pResInfo->convertBuf = taosMemoryCalloc(pResInfo->numOfCols, POINTER_BYTES);
36,380!
2112

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

2122
  return TSDB_CODE_SUCCESS;
42,109✔
2123
}
2124

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

2130
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
216,611✔
2131
    int32_t type = pResultInfo->fields[i].type;
174,602✔
2132
    int32_t schemaBytes =
2133
        calcSchemaBytesFromTypeBytes(pResultInfo->fields[i].type, pResultInfo->fields[i].bytes, isStmt);
174,602✔
2134

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

2142
      pResultInfo->convertBuf[i] = p;
4,554✔
2143

2144
      SResultColumn* pCol = &pResultInfo->pCol[i];
4,554✔
2145
      for (int32_t j = 0; j < pResultInfo->numOfRows; ++j) {
634,522✔
2146
        if (pCol->offset[j] != -1) {
629,961✔
2147
          char* pStart = pCol->offset[j] + pCol->pData;
550,428✔
2148

2149
          int32_t len = taosUcs4ToMbsEx((TdUcs4*)varDataVal(pStart), varDataLen(pStart), varDataVal(p), conv);
550,428✔
2150
          if (len < 0 || len > schemaBytes || (p + len) >= (pResultInfo->convertBuf[i] + colLength[i])) {
550,434!
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);
550,435✔
2160
          pCol->offset[j] = (p - pResultInfo->convertBuf[i]);
550,435✔
2161
          p += (len + VARSTR_HEADER_SIZE);
550,435✔
2162
        }
2163
      }
2164

2165
      pResultInfo->pCol[i].pData = pResultInfo->convertBuf[i];
4,561✔
2166
      pResultInfo->row[i] = pResultInfo->pCol[i].pData;
4,561✔
2167
    }
2168
  }
2169
  taosReleaseConv(idx, conv, C2M, pResultInfo->charsetCxt);
42,009✔
2170
  return TSDB_CODE_SUCCESS;
42,004✔
2171
}
2172

2173
static int32_t convertDecimalType(SReqResultInfo* pResultInfo) {
42,003✔
2174
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
216,619✔
2175
    TAOS_FIELD_E* pFieldE = pResultInfo->fields + i;
174,616✔
2176
    TAOS_FIELD*   pField = pResultInfo->userFields + i;
174,616✔
2177
    int32_t       type = pFieldE->type;
174,616✔
2178
    int32_t       bufLen = 0;
174,616✔
2179
    char*         p = NULL;
174,616✔
2180
    if (!IS_DECIMAL_TYPE(type) || !pResultInfo->pCol[i].pData) {
174,616✔
2181
      continue;
174,612✔
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;
42,003✔
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) {
42,109✔
2285
  int32_t numOfRows = pResultInfo->numOfRows;
42,109✔
2286
  int32_t numOfCols = pResultInfo->numOfCols;
42,109✔
2287
  bool    needConvert = false;
42,109✔
2288
  for (int32_t i = 0; i < numOfCols; ++i) {
217,138✔
2289
    if (pResultInfo->fields[i].type == TSDB_DATA_TYPE_JSON) {
175,058✔
2290
      needConvert = true;
29✔
2291
      break;
29✔
2292
    }
2293
  }
2294

2295
  if (!needConvert) {
42,109✔
2296
    return TSDB_CODE_SUCCESS;
42,080✔
2297
  }
2298

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

2301
  char*   p = (char*)pResultInfo->pData;
29✔
2302
  int32_t blockVersion = *(int32_t*)p;
29✔
2303
  int32_t dataLen = estimateJsonLen(pResultInfo);
29✔
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) {
43,851✔
2439
  bool convertForDecimal = convertUcs4;
43,851✔
2440
  if (pResultInfo == NULL || pResultInfo->numOfCols <= 0 || pResultInfo->fields == NULL) {
43,851!
2441
    tscError("setResultDataPtr paras error");
×
2442
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2443
  }
2444

2445
  if (pResultInfo->numOfRows == 0) {
43,852✔
2446
    return TSDB_CODE_SUCCESS;
1,743✔
2447
  }
2448

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

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

2463
  char* p = (char*)pResultInfo->pData;
42,109✔
2464

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

2469
  int32_t dataLen = *(int32_t*)p;
42,109✔
2470
  p += sizeof(int32_t);
42,109✔
2471

2472
  int32_t rows = *(int32_t*)p;
42,109✔
2473
  p += sizeof(int32_t);
42,109✔
2474

2475
  int32_t cols = *(int32_t*)p;
42,109✔
2476
  p += sizeof(int32_t);
42,109✔
2477

2478
  if (rows != pResultInfo->numOfRows || cols != pResultInfo->numOfCols) {
42,109!
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;
42,109✔
2485
  p += sizeof(int32_t);
42,109✔
2486

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

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

2495
    int32_t bytes = *(int32_t*)p;
175,110✔
2496
    p += sizeof(int32_t);
175,110✔
2497

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

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

2506
  char* pStart = p;
42,107✔
2507
  for (int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
217,207✔
2508
    if ((pStart - pResultInfo->pData) >= dataLen) {
175,101!
2509
      tscError("setResultDataPtr invalid offset over dataLen %d", dataLen);
×
2510
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2511
    }
2512
    if (blockVersion == BLOCK_VERSION_1) {
175,101✔
2513
      colLength[i] = htonl(colLength[i]);
164,717✔
2514
    }
2515
    if (colLength[i] >= dataLen) {
175,101!
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)) {
175,101!
2520
      tscError("invalid type %d", pResultInfo->fields[i].type);
1!
2521
      return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2522
    }
2523
    if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
175,100!
2524
      pResultInfo->pCol[i].offset = (int32_t*)pStart;
43,018✔
2525
      pStart += pResultInfo->numOfRows * sizeof(int32_t);
43,018✔
2526
    } else {
2527
      pResultInfo->pCol[i].nullbitmap = pStart;
132,082✔
2528
      pStart += BitmapLen(pResultInfo->numOfRows);
132,082✔
2529
    }
2530

2531
    pResultInfo->pCol[i].pData = pStart;
175,100✔
2532
    pResultInfo->length[i] =
350,200✔
2533
        calcSchemaBytesFromTypeBytes(pResultInfo->fields[i].type, pResultInfo->fields[i].bytes, isStmt);
175,100✔
2534
    pResultInfo->row[i] = pResultInfo->pCol[i].pData;
175,100✔
2535

2536
    pStart += colLength[i];
175,100✔
2537
  }
2538

2539
  p = pStart;
42,106✔
2540
  // bool blankFill = *(bool*)p;
2541
  p += sizeof(bool);
42,106✔
2542
  int32_t offset = p - pResultInfo->pData;
42,106✔
2543
  if (offset > dataLen) {
42,106!
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) {
42,106✔
2550
    code = doConvertUCS4(pResultInfo, colLength, isStmt);
42,003✔
2551
  }
2552
#endif
2553
  if (TSDB_CODE_SUCCESS == code && convertForDecimal) {
42,107!
2554
    code = convertDecimalType(pResultInfo);
42,004✔
2555
  }
2556
  return code;
42,109✔
2557
}
2558

2559
char* getDbOfConnection(STscObj* pObj) {
646,162✔
2560
  terrno = TSDB_CODE_SUCCESS;
646,162✔
2561
  char* p = NULL;
646,204✔
2562
  (void)taosThreadMutexLock(&pObj->mutex);
646,204✔
2563
  size_t len = strlen(pObj->db);
646,229✔
2564
  if (len > 0) {
646,229✔
2565
    p = taosStrndup(pObj->db, tListLen(pObj->db));
536,195!
2566
    if (p == NULL) {
536,170!
2567
      tscError("failed to taosStrndup db name");
×
2568
    }
2569
  }
2570

2571
  (void)taosThreadMutexUnlock(&pObj->mutex);
646,204✔
2572
  return p;
646,239✔
2573
}
2574

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

2581
  (void)taosThreadMutexLock(&pTscObj->mutex);
2,552✔
2582
  tstrncpy(pTscObj->db, db, tListLen(pTscObj->db));
2,552✔
2583
  (void)taosThreadMutexUnlock(&pTscObj->mutex);
2,552✔
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,
40,500✔
2597
                              bool isStmt) {
2598
  if (pResultInfo == NULL || pRsp == NULL) {
40,500!
2599
    tscError("setQueryResultFromRsp paras is null");
×
2600
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
2601
  }
2602

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

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

2613
  if (pRsp->compressed) {
40,500!
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) {
40,500✔
2636
    int32_t compLen = *(int32_t*)pRsp->data;
38,757✔
2637
    int32_t rawLen = *(int32_t*)(pRsp->data + sizeof(int32_t));
38,757✔
2638

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

2641
    if (pRsp->compressed && compLen < rawLen) {
38,757!
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;
38,757✔
2655
      pResultInfo->payloadLen = htonl(pRsp->compLen);
38,757✔
2656
      if (pRsp->compLen != pRsp->payloadLen) {
38,757!
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;
40,500✔
2665

2666
  int32_t code = setResultDataPtr(pResultInfo, convertUcs4, isStmt);
40,500✔
2667
  return code;
40,499✔
2668
}
2669

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

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

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

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

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

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

2716
  tstrncpy(epSet.eps[0].fqdn, fqdn, TSDB_FQDN_LEN);
4✔
2717
  epSet.eps[0].port = (uint16_t)port;
4✔
2718
  int32_t ret = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp);
4✔
2719
  if (TSDB_CODE_SUCCESS != ret) {
4!
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) {
4!
2725
    tscError("failed to send server status req since %s", terrstr());
1!
2726
    goto _OVER;
1✔
2727
  }
2728

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

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

2739
_OVER:
×
2740
  if (clientRpc != NULL) {
4!
2741
    rpcClose(clientRpc);
4✔
2742
  }
2743
  if (rpcRsp.pCont != NULL) {
4✔
2744
    rpcFreeCont(rpcRsp.pCont);
3✔
2745
  }
2746
  return code;
4✔
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) {
618,049✔
2957
  SSyncQueryParam* pParam = param;
618,049✔
2958
  pParam->pRequest = res;
618,049✔
2959

2960
  if (pParam->pRequest) {
618,049!
2961
    pParam->pRequest->code = code;
618,057✔
2962
    clientOperateReport(pParam->pRequest);
618,057✔
2963
  }
2964

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

2970
void taosAsyncQueryImpl(uint64_t connId, const char* sql, __taos_async_fn_t fp, void* param, bool validateOnly,
617,234✔
2971
                        int8_t source) {
2972
  if (sql == NULL || NULL == fp) {
617,234!
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);
617,262✔
2982
  if (sqlLen > (size_t)TSDB_MAX_ALLOWED_SQL_LEN) {
617,262!
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);
617,262✔
2990

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

2999
  pRequest->source = source;
617,266✔
3000
  pRequest->body.queryFp = fp;
617,266✔
3001
  doAsyncQuery(pRequest, false);
617,266✔
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) {
617,094✔
3039
  if (NULL == taos) {
617,094!
3040
    terrno = TSDB_CODE_TSC_DISCONNECTED;
×
3041
    return NULL;
×
3042
  }
3043

3044
  SSyncQueryParam* param = taosMemoryCalloc(1, sizeof(SSyncQueryParam));
617,094!
3045
  if (NULL == param) {
617,116!
3046
    return NULL;
×
3047
  }
3048
  int32_t code = tsem_init(&param->sem, 0, 0);
617,116✔
3049
  if (TSDB_CODE_SUCCESS != code) {
617,071!
3050
    taosMemoryFree(param);
×
3051
    return NULL;
×
3052
  }
3053

3054
  taosAsyncQueryImpl(*(int64_t*)taos, sql, syncQueryFn, param, validateOnly, source);
617,071✔
3055
  code = tsem_wait(&param->sem);
617,051✔
3056
  if (TSDB_CODE_SUCCESS != code) {
617,132!
3057
    taosMemoryFree(param);
×
3058
    return NULL;
×
3059
  }
3060
  code = tsem_destroy(&param->sem);
617,132✔
3061
  if (TSDB_CODE_SUCCESS != code) {
617,135!
3062
    tscError("failed to destroy semaphore since %s", tstrerror(code));
×
3063
  }
3064

3065
  SRequestObj* pRequest = NULL;
617,133✔
3066
  if (param->pRequest != NULL) {
617,133!
3067
    param->pRequest->syncQuery = true;
617,133✔
3068
    pRequest = param->pRequest;
617,133✔
3069
    param->pRequest->inCallback = false;
617,133✔
3070
  }
3071
  taosMemoryFree(param);
617,133!
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;
617,145✔
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) {
38,053✔
3115
  SRequestObj* pRequest = (SRequestObj*)param;
38,053✔
3116

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

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

3122
  pResultInfo->pData = pResult;
38,053✔
3123
  pResultInfo->numOfRows = 0;
38,053✔
3124

3125
  if (code != TSDB_CODE_SUCCESS) {
38,053!
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) {
38,053!
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,
76,105✔
3139
                                         pResultInfo->convertUcs4, pRequest->stmtBindVersion > 0);
38,053✔
3140
  if (pRequest->code != TSDB_CODE_SUCCESS) {
38,052!
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(
38,052✔
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;
38,052✔
3150
    SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary;
38,052✔
3151
    (void)atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen);
38,052✔
3152
  }
3153

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

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

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

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

3170
  // all data has returned to App already, no need to try again
3171
  if (pResultInfo->completed) {
46,791✔
3172
    // it is a local executed query, no need to do async fetch
3173
    if (QUERY_EXEC_MODE_SCHEDULE != pRequest->body.execMode) {
8,738✔
3174
      if (pResultInfo->localResultFetched) {
2,726✔
3175
        pResultInfo->numOfRows = 0;
1,363✔
3176
        pResultInfo->current = 0;
1,363✔
3177
      } else {
3178
        pResultInfo->localResultFetched = true;
1,363✔
3179
      }
3180
    } else {
3181
      pResultInfo->numOfRows = 0;
6,012✔
3182
    }
3183

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

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

3194
  int32_t code = schedulerFetchRows(pRequest->body.queryJob, &req);
38,053✔
3195
  if (TSDB_CODE_SUCCESS != code) {
38,053!
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) {
618,188✔
3202
  pRequest->inCallback = true;
618,188✔
3203
  int64_t this = pRequest->self;
618,188✔
3204
  if (tsQueryTbNotExistAsEmpty && TD_RES_QUERY(&pRequest->resType) && pRequest->isQuery &&
618,188!
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,
618,188✔
3211
           pRequest);
3212

3213
  if (pRequest->body.queryFp != NULL) {
618,188!
3214
    pRequest->body.queryFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, code);
618,211✔
3215
  }
3216

3217
  SRequestObj* pReq = acquireRequest(this);
618,202✔
3218
  if (pReq != NULL) {
618,230✔
3219
    pReq->inCallback = false;
618,204✔
3220
    (void)releaseRequest(this);
618,204✔
3221
  }
3222
}
618,226✔
3223

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