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

taosdata / TDengine / #3620

21 Feb 2025 09:00AM UTC coverage: 63.573% (+0.2%) from 63.423%
#3620

push

travis-ci

web-flow
ci: taosBenchmark add coverage cases branch 3.0 (#29788)

* fix: add unit test for taos-tools

* fix: only .cpp include

* fix: remove no use function

* fix: restore toolsSys.c

* fix: add toolsSys case

* fix: rebuild error fixed

* fix: fix build error

* fix: support get vgroups with core and memory limit

* fix: build error for strcasecmp

* fix: add insertBasic.py case

* fix: add command line set vgroups=3

* fix: change with ns database

* toolscJson read with int replace float and add insertPrecison.py

* fix: add insertBindVGroup.json case

* fix: remove public fun removeQuotation

* fix: vgroups change method

* fix: memory leak for runInsertLimitThread slot

* insertPrecision.py word write wrong

* fix: check isFloat number

* fix: vgroups change logic error

* fix: insertBasic.py real and expect error

* fix: adjust default vgroups

* fix: adjust default vgroups modify comment

148962 of 300203 branches covered (49.62%)

Branch coverage included in aggregate %.

15 of 16 new or added lines in 1 file covered. (93.75%)

2018 existing lines in 133 files now uncovered.

233201 of 300933 relevant lines covered (77.49%)

18174406.98 hits per line

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

64.23
/source/dnode/vnode/src/tq/tqRead.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 "tmsg.h"
17
#include "tq.h"
18

19
bool isValValidForTable(STqHandle* pHandle, SWalCont* pHead) {
678✔
20
  if (pHandle == NULL || pHead == NULL) {
678!
21
    return false;
×
22
  }
23
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
678✔
24
    return true;
674✔
25
  }
26

27
  int16_t msgType = pHead->msgType;
4✔
28
  char*   body = pHead->body;
4✔
29
  int32_t bodyLen = pHead->bodyLen;
4✔
30

31
  int64_t  tbSuid = pHandle->execHandle.execTb.suid;
4✔
32
  int64_t  realTbSuid = 0;
4✔
33
  SDecoder dcoder = {0};
4✔
34
  void*    data = POINTER_SHIFT(body, sizeof(SMsgHead));
4✔
35
  int32_t  len = bodyLen - sizeof(SMsgHead);
4✔
36
  tDecoderInit(&dcoder, data, len);
4✔
37

38
  if (msgType == TDMT_VND_CREATE_STB || msgType == TDMT_VND_ALTER_STB) {
4!
39
    SVCreateStbReq req = {0};
2✔
40
    if (tDecodeSVCreateStbReq(&dcoder, &req) < 0) {
2!
41
      goto end;
×
42
    }
43
    realTbSuid = req.suid;
2✔
44
  } else if (msgType == TDMT_VND_DROP_STB) {
2!
45
    SVDropStbReq req = {0};
×
46
    if (tDecodeSVDropStbReq(&dcoder, &req) < 0) {
×
47
      goto end;
×
48
    }
49
    realTbSuid = req.suid;
×
50
  } else if (msgType == TDMT_VND_CREATE_TABLE) {
2!
51
    SVCreateTbBatchReq req = {0};
2✔
52
    if (tDecodeSVCreateTbBatchReq(&dcoder, &req) < 0) {
2!
53
      goto end;
×
54
    }
55

56
    int32_t        needRebuild = 0;
2✔
57
    SVCreateTbReq* pCreateReq = NULL;
2✔
58
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
4✔
59
      pCreateReq = req.pReqs + iReq;
2✔
60
      if (pCreateReq->type == TSDB_CHILD_TABLE && pCreateReq->ctb.suid == tbSuid) {
2!
61
        needRebuild++;
×
62
      }
63
    }
64
    if (needRebuild == 0) {
2!
65
      // do nothing
66
    } else if (needRebuild == req.nReqs) {
×
67
      realTbSuid = tbSuid;
×
68
    } else {
69
      realTbSuid = tbSuid;
×
70
      SVCreateTbBatchReq reqNew = {0};
×
71
      reqNew.pArray = taosArrayInit(req.nReqs, sizeof(struct SVCreateTbReq));
×
72
      if (reqNew.pArray == NULL) {
×
73
        tDeleteSVCreateTbBatchReq(&req);
×
74
        goto end;
×
75
      }
76
      for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
77
        pCreateReq = req.pReqs + iReq;
×
78
        if (pCreateReq->type == TSDB_CHILD_TABLE && pCreateReq->ctb.suid == tbSuid) {
×
79
          reqNew.nReqs++;
×
80
          if (taosArrayPush(reqNew.pArray, pCreateReq) == NULL) {
×
81
            taosArrayDestroy(reqNew.pArray);
×
82
            tDeleteSVCreateTbBatchReq(&req);
×
83
            goto end;
×
84
          }
85
        }
86
      }
87

88
      int     tlen = 0;
×
89
      int32_t ret = 0;
×
90
      tEncodeSize(tEncodeSVCreateTbBatchReq, &reqNew, tlen, ret);
×
91
      void* buf = taosMemoryMalloc(tlen);
×
92
      if (NULL == buf) {
×
93
        taosArrayDestroy(reqNew.pArray);
×
94
        tDeleteSVCreateTbBatchReq(&req);
×
95
        goto end;
×
96
      }
97
      SEncoder coderNew = {0};
×
98
      tEncoderInit(&coderNew, buf, tlen - sizeof(SMsgHead));
×
99
      ret = tEncodeSVCreateTbBatchReq(&coderNew, &reqNew);
×
100
      tEncoderClear(&coderNew);
×
101
      if (ret < 0) {
×
102
        taosMemoryFree(buf);
×
103
        taosArrayDestroy(reqNew.pArray);
×
104
        tDeleteSVCreateTbBatchReq(&req);
×
105
        goto end;
×
106
      }
107
      (void)memcpy(pHead->body + sizeof(SMsgHead), buf, tlen);
×
108
      pHead->bodyLen = tlen + sizeof(SMsgHead);
×
109
      taosMemoryFree(buf);
×
110
      taosArrayDestroy(reqNew.pArray);
×
111
    }
112

113
    tDeleteSVCreateTbBatchReq(&req);
2✔
114
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
×
115
    SVAlterTbReq req = {0};
×
116

117
    if (tDecodeSVAlterTbReq(&dcoder, &req) < 0) {
×
118
      goto end;
×
119
    }
120

121
    SMetaReader mr = {0};
×
122
    metaReaderDoInit(&mr, pHandle->execHandle.pTqReader->pVnodeMeta, META_READER_LOCK);
×
123

124
    if (metaGetTableEntryByName(&mr, req.tbName) < 0) {
×
125
      metaReaderClear(&mr);
×
126
      goto end;
×
127
    }
128
    realTbSuid = mr.me.ctbEntry.suid;
×
129
    metaReaderClear(&mr);
×
130
  } else if (msgType == TDMT_VND_DROP_TABLE) {
×
131
    SVDropTbBatchReq req = {0};
×
132

133
    if (tDecodeSVDropTbBatchReq(&dcoder, &req) < 0) {
×
134
      goto end;
×
135
    }
136

137
    int32_t      needRebuild = 0;
×
138
    SVDropTbReq* pDropReq = NULL;
×
139
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
140
      pDropReq = req.pReqs + iReq;
×
141

142
      if (pDropReq->suid == tbSuid) {
×
143
        needRebuild++;
×
144
      }
145
    }
146
    if (needRebuild == 0) {
×
147
      // do nothing
148
    } else if (needRebuild == req.nReqs) {
×
149
      realTbSuid = tbSuid;
×
150
    } else {
151
      realTbSuid = tbSuid;
×
152
      SVDropTbBatchReq reqNew = {0};
×
153
      reqNew.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbReq));
×
154
      if (reqNew.pArray == NULL) {
×
155
        goto end;
×
156
      }
157
      for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
158
        pDropReq = req.pReqs + iReq;
×
159
        if (pDropReq->suid == tbSuid) {
×
160
          reqNew.nReqs++;
×
161
          if (taosArrayPush(reqNew.pArray, pDropReq) == NULL) {
×
162
            taosArrayDestroy(reqNew.pArray);
×
163
            goto end;
×
164
          }
165
        }
166
      }
167

168
      int     tlen = 0;
×
169
      int32_t ret = 0;
×
170
      tEncodeSize(tEncodeSVDropTbBatchReq, &reqNew, tlen, ret);
×
171
      void* buf = taosMemoryMalloc(tlen);
×
172
      if (NULL == buf) {
×
173
        taosArrayDestroy(reqNew.pArray);
×
174
        goto end;
×
175
      }
176
      SEncoder coderNew = {0};
×
177
      tEncoderInit(&coderNew, buf, tlen - sizeof(SMsgHead));
×
178
      ret = tEncodeSVDropTbBatchReq(&coderNew, &reqNew);
×
179
      tEncoderClear(&coderNew);
×
180
      if (ret != 0) {
×
181
        taosMemoryFree(buf);
×
182
        taosArrayDestroy(reqNew.pArray);
×
183
        goto end;
×
184
      }
185
      (void)memcpy(pHead->body + sizeof(SMsgHead), buf, tlen);
×
186
      pHead->bodyLen = tlen + sizeof(SMsgHead);
×
187
      taosMemoryFree(buf);
×
188
      taosArrayDestroy(reqNew.pArray);
×
189
    }
190
  } else if (msgType == TDMT_VND_DELETE) {
×
191
    SDeleteRes req = {0};
×
192
    if (tDecodeDeleteRes(&dcoder, &req) < 0) {
×
193
      goto end;
×
194
    }
195
    realTbSuid = req.suid;
×
196
  }
197

198
end:
×
199
  tDecoderClear(&dcoder);
4✔
200
  bool tmp = tbSuid == realTbSuid;
4✔
201
  tqDebug("%s suid:%"PRId64" realSuid:%"PRId64" return:%d", __FUNCTION__, tbSuid, realTbSuid, tmp);
4!
202
  return tmp;
4✔
203
}
204

205
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
133,760✔
206
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
133,760!
207
    return -1;
×
208
  }
209
  int32_t code = -1;
133,884✔
210
  int32_t vgId = TD_VID(pTq->pVnode);
133,884✔
211
  int64_t id = pHandle->pWalReader->readerId;
133,884✔
212

213
  int64_t offset = *fetchOffset;
133,884✔
214
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
133,884✔
215
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
133,830✔
216
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
133,872✔
217

218
  tqDebug("vgId:%d, start to fetch wal, index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64 ", applied:%" PRId64
133,885!
219
          ", 0x%" PRIx64,
220
          vgId, offset, lastVer, committedVer, appliedVer, id);
221

222
  while (offset <= appliedVer) {
142,942✔
223
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
131,623!
224
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", (epoch %d) vgId:%d offset %" PRId64
×
225
              ", no more log to return,QID:0x%" PRIx64 " 0x%" PRIx64,
226
              pHandle->consumerId, pHandle->epoch, vgId, offset, reqId, id);
227
      goto END;
×
228
    }
229

230
    tqDebug("vgId:%d, consumer:0x%" PRIx64 " taosx get msg ver %" PRId64 ", type: %s,QID:0x%" PRIx64 " 0x%" PRIx64,
131,612!
231
            vgId, pHandle->consumerId, offset, TMSG_INFO(pHandle->pWalReader->pHead->head.msgType), reqId, id);
232

233
    if (pHandle->pWalReader->pHead->head.msgType == TDMT_VND_SUBMIT) {
131,621✔
234
      code = walFetchBody(pHandle->pWalReader);
122,103✔
235
      goto END;
122,084✔
236
    } else {
237
      if (pHandle->fetchMeta != WITH_DATA) {
9,518✔
238
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
833✔
239
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
833✔
240
          code = walFetchBody(pHandle->pWalReader);
678✔
241
          if (code < 0) {
678!
242
            goto END;
×
243
          }
244

245
          pHead = &(pHandle->pWalReader->pHead->head);
678✔
246
          if (isValValidForTable(pHandle, pHead)) {
678✔
247
            code = 0;
675✔
248
            goto END;
675✔
249
          } else {
250
            offset++;
3✔
251
            code = -1;
3✔
252
            continue;
3✔
253
          }
254
        }
255
      }
256
      code = walSkipFetchBody(pHandle->pWalReader);
8,840✔
257
      if (code < 0) {
8,837!
258
        goto END;
×
259
      }
260
      offset++;
8,837✔
261
    }
262
    code = -1;
8,837✔
263
  }
264

265
END:
11,319✔
266
  *fetchOffset = offset;
134,078✔
267
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64 ", applied:%" PRId64 ", 0x%" PRIx64,
134,078✔
268
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
269
  return code;
134,099✔
270
}
271

272
bool tqGetTablePrimaryKey(STqReader* pReader) {
20,299✔
273
  if (pReader == NULL) {
20,299!
274
    return false;
×
275
  }
276
  return pReader->hasPrimaryKey;
20,299✔
277
}
278

279
void tqSetTablePrimaryKey(STqReader* pReader, int64_t uid) {
179✔
280
  tqDebug("%s:%p uid:%"PRId64, __FUNCTION__ , pReader, uid);
179!
281

282
  if (pReader == NULL) {
179!
283
    return;
×
284
  }
285
  bool            ret = false;
179✔
286
  SSchemaWrapper* schema = metaGetTableSchema(pReader->pVnodeMeta, uid, -1, 1);
179✔
287
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
178!
288
    ret = true;
×
289
  }
290
  tDeleteSchemaWrapper(schema);
291
  pReader->hasPrimaryKey = ret;
179✔
292
}
293

294
STqReader* tqReaderOpen(SVnode* pVnode) {
8,732✔
295
  tqDebug("%s:%p", __FUNCTION__ , pVnode);
8,732✔
296
  if (pVnode == NULL) {
8,735!
297
    return NULL;
×
298
  }
299
  STqReader* pReader = taosMemoryCalloc(1, sizeof(STqReader));
8,735!
300
  if (pReader == NULL) {
8,735!
301
    return NULL;
×
302
  }
303

304
  pReader->pWalReader = walOpenReader(pVnode->pWal, NULL, 0);
8,735✔
305
  if (pReader->pWalReader == NULL) {
8,734!
306
    taosMemoryFree(pReader);
×
307
    return NULL;
×
308
  }
309

310
  pReader->pVnodeMeta = pVnode->pMeta;
8,734✔
311
  pReader->pColIdList = NULL;
8,734✔
312
  pReader->cachedSchemaVer = 0;
8,734✔
313
  pReader->cachedSchemaSuid = 0;
8,734✔
314
  pReader->pSchemaWrapper = NULL;
8,734✔
315
  pReader->tbIdHash = NULL;
8,734✔
316
  pReader->pResBlock = NULL;
8,734✔
317

318
  int32_t code = createDataBlock(&pReader->pResBlock);
8,734✔
319
  if (code) {
8,735!
320
    terrno = code;
×
321
  }
322

323
  return pReader;
8,735✔
324
}
325

326
void tqReaderClose(STqReader* pReader) {
8,772✔
327
  tqDebug("%s:%p", __FUNCTION__ , pReader);
8,772✔
328
  if (pReader == NULL) return;
8,775✔
329

330
  // close wal reader
331
  if (pReader->pWalReader) {
8,733!
332
    walCloseReader(pReader->pWalReader);
8,733✔
333
  }
334

335
  if (pReader->pSchemaWrapper) {
8,732✔
336
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
2,072!
337
  }
338

339
  if (pReader->pColIdList) {
8,732✔
340
    taosArrayDestroy(pReader->pColIdList);
8,426✔
341
  }
342

343
  // free hash
344
  blockDataDestroy(pReader->pResBlock);
8,732✔
345
  taosHashCleanup(pReader->tbIdHash);
8,733✔
346
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
8,733✔
347
  taosMemoryFree(pReader);
8,733!
348
}
349

350
int32_t tqReaderSeek(STqReader* pReader, int64_t ver, const char* id) {
6,560✔
351
  if (pReader == NULL) {
6,560!
352
    return TSDB_CODE_INVALID_PARA;
×
353
  }
354
  if (walReaderSeekVer(pReader->pWalReader, ver) < 0) {
6,560✔
355
    return terrno;
141✔
356
  }
357
  tqDebug("wal reader seek to ver:%" PRId64 " %s", ver, id);
6,417!
358
  return 0;
6,419✔
359
}
360

361
int32_t extractMsgFromWal(SWalReader* pReader, void** pItem, int64_t maxVer, const char* id) {
519,970✔
362
  int32_t code = 0;
519,970✔
363

364
  while (1) {
8,309✔
365
    TAOS_CHECK_RETURN(walNextValidMsg(pReader));
528,279✔
366

367
    SWalCont* pCont = &pReader->pHead->head;
453,870✔
368
    int64_t   ver = pCont->version;
453,870✔
369
    if (ver > maxVer) {
453,870✔
370
      tqDebug("maxVer in WAL:%" PRId64 " reached, current:%" PRId64 ", do not scan wal anymore, %s", maxVer, ver, id);
321✔
371
      return TSDB_CODE_SUCCESS;
321✔
372
    }
373

374
    if (pCont->msgType == TDMT_VND_SUBMIT) {
453,549✔
375
      void*   pBody = POINTER_SHIFT(pCont->body, sizeof(SSubmitReq2Msg));
442,671✔
376
      int32_t len = pCont->bodyLen - sizeof(SSubmitReq2Msg);
442,671✔
377

378
      void* data = taosMemoryMalloc(len);
442,671!
379
      if (data == NULL) {
442,668!
380
        // todo: for all stream in this vnode, keep this offset in the offset files, and wait for a moment, and then
381
        // retry
382
        tqError("vgId:%d, failed to copy submit data for stream processing, since out of memory", 0);
×
383
        return terrno;
×
384
      }
385

386
      (void)memcpy(data, pBody, len);
442,668✔
387
      SPackedData data1 = (SPackedData){.ver = ver, .msgLen = len, .msgStr = data};
442,668✔
388

389
      code = streamDataSubmitNew(&data1, STREAM_INPUT__DATA_SUBMIT, (SStreamDataSubmit**)pItem);
442,668✔
390
      if (code != 0) {
442,666!
391
        tqError("%s failed to create data submit for stream since out of memory", id);
×
392
        return code;
×
393
      }
394
    } else if (pCont->msgType == TDMT_VND_DELETE) {
10,878✔
395
      void*   pBody = POINTER_SHIFT(pCont->body, sizeof(SMsgHead));
10,708✔
396
      int32_t len = pCont->bodyLen - sizeof(SMsgHead);
10,708✔
397
      EStreamType blockType = STREAM_DELETE_DATA;
10,708✔
398
      code = tqExtractDelDataBlock(pBody, len, ver, (void**)pItem, 0, blockType);
10,708✔
399
      if (code == TSDB_CODE_SUCCESS) {
10,709!
400
        if (*pItem == NULL) {
10,709✔
401
          tqDebug("s-task:%s empty delete msg, discard it, len:%d, ver:%" PRId64, id, len, ver);
8,309✔
402
          // we need to continue check next data in the wal files.
403
          continue;
8,309✔
404
        } else {
405
          tqDebug("s-task:%s delete msg extract from WAL, len:%d, ver:%" PRId64, id, len, ver);
2,400✔
406
        }
407
      } else {
408
        terrno = code;
×
409
        tqError("s-task:%s extract delete msg from WAL failed, code:%s", id, tstrerror(code));
×
410
        return code;
×
411
      }
412

413
    } else if (pCont->msgType == TDMT_VND_DROP_TABLE && pReader->cond.scanDropCtb) {
355!
414
      void* pBody = POINTER_SHIFT(pCont->body, sizeof(SMsgHead));
185✔
415
      int32_t len = pCont->bodyLen - sizeof(SMsgHead);
185✔
416
      code = tqExtractDropCtbDataBlock(pBody, len, ver, (void**)pItem, 0);
185✔
417
      if (TSDB_CODE_SUCCESS == code) {
185!
418
        if (!*pItem) {
185!
419
          continue;
×
420
        } else {
421
          tqDebug("s-task:%s drop ctb msg extract from WAL, len:%d, ver:%"PRId64, id, len, ver);
185!
422
        }
423
      } else {
424
        terrno = code;
×
425
        return code;
×
426
      }
427
    } else {
428
      tqError("s-task:%s invalid msg type:%d, ver:%" PRId64, id, pCont->msgType, ver);
×
429
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
430
    }
431

432
    return code;
445,248✔
433
  }
434
}
435

436
bool tqNextBlockInWal(STqReader* pReader, const char* id, int sourceExcluded) {
557,061✔
437
  if (pReader == NULL) {
557,061!
438
    return false;
×
439
  }
440
  SWalReader* pWalReader = pReader->pWalReader;
557,061✔
441

442
  int64_t st = taosGetTimestampMs();
557,056✔
443
  while (1) {
693,419✔
444
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
1,250,475✔
445
    while (pReader->nextBlk < numOfBlocks) {
1,681,538✔
446
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
935,238✔
447
              pReader->msg.ver);
448

449
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
935,238✔
450
      if (pSubmitTbData == NULL) {
935,188!
UNCOV
451
        tqError("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
×
452
                pReader->msg.ver);
453
        return false;
×
454
      }
455
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
935,190✔
456
        pReader->nextBlk += 1;
132✔
457
        continue;
132✔
458
      }
459
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
935,055!
460
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
504,117✔
461
        SSDataBlock* pRes = NULL;
504,117✔
462
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
504,117✔
463
        if (code == TSDB_CODE_SUCCESS) {
504,190!
464
          return true;
504,193✔
465
        }
466
      } else {
467
        pReader->nextBlk += 1;
431,048✔
468
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
431,048!
469
      }
470
    }
471

472
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
746,300✔
473
    pReader->msg.msgStr = NULL;
746,271✔
474

475
    int64_t elapsed = taosGetTimestampMs() - st;
746,253✔
476
    if (elapsed > 1000 || elapsed < 0) {
746,253!
UNCOV
477
      return false;
×
478
    }
479

480
    // try next message in wal file
481
    if (walNextValidMsg(pWalReader) < 0) {
746,265✔
482
      return false;
52,867✔
483
    }
484

485
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
693,464✔
486
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
693,464✔
487
    int64_t ver = pWalReader->pHead->head.version;
693,464✔
488
    if (tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL) != 0) {
693,464!
489
      return false;
×
490
    }
491
    pReader->nextBlk = 0;
693,419✔
492
  }
493
}
494

495
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList) {
1,257,966✔
496
  if (pReader == NULL) {
1,257,966!
497
    return TSDB_CODE_INVALID_PARA;
×
498
  }
499
  pReader->msg.msgStr = msgStr;
1,257,966✔
500
  pReader->msg.msgLen = msgLen;
1,257,966✔
501
  pReader->msg.ver = ver;
1,257,966✔
502

503
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
1,257,966✔
504
  SDecoder decoder = {0};
1,257,966✔
505

506
  tDecoderInit(&decoder, pReader->msg.msgStr, pReader->msg.msgLen);
1,257,966✔
507
  int32_t code = tDecodeSubmitReq(&decoder, &pReader->submit, rawList);
1,257,889✔
508
  tDecoderClear(&decoder);
1,257,435✔
509

510
  if (code != 0) {
1,257,985!
511
    tqError("DecodeSSubmitReq2 error, msgLen:%d, ver:%" PRId64, msgLen, ver);
×
512
  }
513

514
  return code;
1,257,950✔
515
}
516

517
void tqReaderClearSubmitMsg(STqReader *pReader) {
685,981✔
518
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
685,981✔
519
  pReader->nextBlk = 0;
686,245✔
520
  pReader->msg.msgStr = NULL;
686,245✔
521
}
686,245✔
522

523

524
SWalReader* tqGetWalReader(STqReader* pReader) {
618,150✔
525
  if (pReader == NULL) {
618,150!
526
    return NULL;
×
527
  }
528
  return pReader->pWalReader;
618,150✔
529
}
530

531
SSDataBlock* tqGetResultBlock(STqReader* pReader) {
557,051✔
532
  if (pReader == NULL) {
557,051!
533
    return NULL;
×
534
  }
535
  return pReader->pResBlock;
557,051✔
536
}
537

538
int64_t tqGetResultBlockTime(STqReader* pReader) {
557,041✔
539
  if (pReader == NULL) {
557,041!
540
    return 0;
×
541
  }
542
  return pReader->lastTs;
557,041✔
543
}
544

545
bool tqNextBlockImpl(STqReader* pReader, const char* idstr) {
880,945✔
546
  int32_t code = false;
880,945✔
547
  int32_t lino = 0;
880,945✔
548
  int64_t uid = 0;
880,945✔
549
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
880,945!
550
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
880,945!
551
  TSDB_CHECK_NULL(pReader->tbIdHash, code, lino, END, true);
880,945!
552

553
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
880,945✔
554
  while (pReader->nextBlk < blockSz) {
1,000,892✔
555
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
521,893✔
556
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
521,868!
557
    uid = pSubmitTbData->uid;
521,868✔
558
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
521,868✔
559
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
521,903✔
560

561
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64 "", pReader->nextBlk, blockSz, uid);
119,883✔
562
    pReader->nextBlk++;
119,906✔
563
  }
564

565
  tqReaderClearSubmitMsg(pReader);
478,999✔
566
  tqTrace("iterator data block end, total block num:%d, uid:%"PRId64, blockSz, uid);
479,128✔
567

568
END:
473,344✔
569
  tqTrace("%s:%d return:%s, uid:%"PRId64, __FUNCTION__, lino, code?"true":"false", uid);
881,148✔
570
  return code;
881,102✔
571
}
572

573
bool tqNextDataBlockFilterOut(STqReader* pReader, SHashObj* filterOutUids) {
170,709✔
574
  int32_t code = false;
170,709✔
575
  int32_t lino = 0;
170,709✔
576
  int64_t uid = 0;
170,709✔
577

578
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
170,709!
579
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
170,709!
580
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
170,709!
581

582
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
170,709✔
583
  while (pReader->nextBlk < blockSz) {
170,702✔
584
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
85,486✔
585
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
85,481!
586
    uid = pSubmitTbData->uid;
85,481✔
587
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
85,481✔
588
    TSDB_CHECK_NULL(ret, code, lino, END, true);
85,481!
589
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64 "", pReader->nextBlk, blockSz, uid);
×
590
    pReader->nextBlk++;
×
591
  }
592
  tqReaderClearSubmitMsg(pReader);
85,216✔
593
  tqTrace("iterator data block end, total block num:%d, uid:%"PRId64, blockSz, uid);
85,401!
594

595
END:
85,401✔
596
  tqTrace("%s:%d get data:%s, uid:%"PRId64, __FUNCTION__, lino, code?"true":"false", uid);
170,882!
597
  return code;
170,652✔
598
}
599

600
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask) {
118,059✔
601
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
118,059!
602
    return TSDB_CODE_INVALID_PARA;
×
603
  }
604
  int32_t code = 0;
118,133✔
605

606
  int32_t cnt = 0;
118,133✔
607
  for (int32_t i = 0; i < pSrc->nCols; i++) {
705,450✔
608
    cnt += mask[i];
587,317✔
609
  }
610

611
  pDst->nCols = cnt;
118,133✔
612
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
118,133!
613
  if (pDst->pSchema == NULL) {
118,129!
614
    return TAOS_GET_TERRNO(terrno);
×
615
  }
616

617
  int32_t j = 0;
118,129✔
618
  for (int32_t i = 0; i < pSrc->nCols; i++) {
704,137✔
619
    if (mask[i]) {
585,937✔
620
      pDst->pSchema[j++] = pSrc->pSchema[i];
585,908✔
621
      SColumnInfoData colInfo =
622
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
585,908✔
623
      code = blockDataAppendColInfo(pBlock, &colInfo);
586,946✔
624
      if (code != 0) {
585,979!
625
        return code;
×
626
      }
627
    }
628
  }
629
  return 0;
118,200✔
630
}
631

632
static int32_t buildResSDataBlock(STqReader* pReader, SSchemaWrapper* pSchema, const SArray* pColIdList) {
1,908✔
633
  if (pReader == NULL || pSchema == NULL || pColIdList == NULL) {
1,908!
634
    return TSDB_CODE_INVALID_PARA;
×
635
  }
636
  SSDataBlock* pBlock = pReader->pResBlock;
1,909✔
637
  if (blockDataGetNumOfCols(pBlock) > 0) {
1,909✔
638
      blockDataDestroy(pBlock);
2✔
639
      int32_t code = createDataBlock(&pReader->pResBlock);
2✔
640
      if (code) {
2!
641
        return code;
×
642
      }
643
      pBlock = pReader->pResBlock;
2✔
644

645
      pBlock->info.id.uid = pReader->cachedSchemaUid;
2✔
646
      pBlock->info.version = pReader->msg.ver;
2✔
647
  }
648

649
  int32_t numOfCols = taosArrayGetSize(pColIdList);
1,909✔
650

651
  if (numOfCols == 0) {  // all columns are required
1,910!
652
    for (int32_t i = 0; i < pSchema->nCols; ++i) {
×
653
      SSchema*        pColSchema = &pSchema->pSchema[i];
×
654
      SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
×
655

656
      int32_t code = blockDataAppendColInfo(pBlock, &colInfo);
×
657
      if (code != TSDB_CODE_SUCCESS) {
×
658
        blockDataFreeRes(pBlock);
×
659
        return terrno;
×
660
      }
661
    }
662
  } else {
663
    if (numOfCols > pSchema->nCols) {
1,910✔
664
      numOfCols = pSchema->nCols;
2✔
665
    }
666

667
    int32_t i = 0;
1,910✔
668
    int32_t j = 0;
1,910✔
669
    while (i < pSchema->nCols && j < numOfCols) {
15,149✔
670
      SSchema* pColSchema = &pSchema->pSchema[i];
13,236✔
671
      col_id_t colIdSchema = pColSchema->colId;
13,236✔
672

673
      col_id_t* pColIdNeed = (col_id_t*)taosArrayGet(pColIdList, j);
13,236✔
674
      if (pColIdNeed == NULL) {
13,239!
675
        break;
×
676
      }
677
      if (colIdSchema < *pColIdNeed) {
13,239✔
678
        i++;
1,655✔
679
      } else if (colIdSchema > *pColIdNeed) {
11,584!
680
        j++;
×
681
      } else {
682
        SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
11,584✔
683
        int32_t         code = blockDataAppendColInfo(pBlock, &colInfo);
11,605✔
684
        if (code != TSDB_CODE_SUCCESS) {
11,584!
685
          return -1;
×
686
        }
687
        i++;
11,584✔
688
        j++;
11,584✔
689
      }
690
    }
691
  }
692

693
  return TSDB_CODE_SUCCESS;
1,913✔
694
}
695

696
static int32_t doSetVal(SColumnInfoData* pColumnInfoData, int32_t rowIndex, SColVal* pColVal) {
162,575,938✔
697
  int32_t code = TSDB_CODE_SUCCESS;
162,575,938✔
698

699
  if (IS_VAR_DATA_TYPE(pColVal->value.type)) {
170,860,917!
700
    char val[65535 + 2] = {0};
7,805,239✔
701
    if (COL_VAL_IS_VALUE(pColVal)) {
7,805,239!
702
      if (pColVal->value.pData != NULL) {
8,293,603!
703
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
8,294,989✔
704
      }
705
      varDataSetLen(val, pColVal->value.nData);
8,293,603✔
706
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
8,293,603✔
707
    } else {
708
      colDataSetNULL(pColumnInfoData, rowIndex);
×
709
    }
710
  } else {
711
    code = colDataSetVal(pColumnInfoData, rowIndex, (void*)&pColVal->value.val, !COL_VAL_IS_VALUE(pColVal));
154,770,699✔
712
  }
713

714
  return code;
162,872,651✔
715
}
716

717
int32_t tqRetrieveDataBlock(STqReader* pReader, SSDataBlock** pRes, const char* id) {
872,958✔
718
  if (pReader == NULL || pRes == NULL) {
872,958!
719
    return TSDB_CODE_INVALID_PARA;
×
720
  }
721
  tqDebug("tq reader retrieve data block %p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
873,002✔
722
  int32_t        code = 0;
873,043✔
723
  int32_t        line = 0;
873,043✔
724
  STSchema*      pTSchema = NULL;
873,043✔
725
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk++);
873,043✔
726
  TSDB_CHECK_NULL(pSubmitTbData, code, line, END, terrno);
873,049!
727
  SSDataBlock* pBlock = pReader->pResBlock;
873,049✔
728
  *pRes = pBlock;
873,049✔
729

730
  blockDataCleanup(pBlock);
873,049✔
731

732
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
872,991✔
733
  int32_t sversion = pSubmitTbData->sver;
872,991✔
734
  int64_t suid = pSubmitTbData->suid;
872,991✔
735
  int64_t uid = pSubmitTbData->uid;
872,991✔
736
  pReader->lastTs = pSubmitTbData->ctimeMs;
872,991✔
737

738
  pBlock->info.id.uid = uid;
872,991✔
739
  pBlock->info.version = pReader->msg.ver;
872,991✔
740

741
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
872,991✔
742
      (pReader->cachedSchemaVer != sversion)) {
871,064!
743
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
1,923✔
744

745
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1);
1,927✔
746
    if (pReader->pSchemaWrapper == NULL) {
1,928✔
747
      tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", uid:%" PRId64
20✔
748
             "version %d, possibly dropped table",
749
             vgId, suid, uid, pReader->cachedSchemaVer);
750
      pReader->cachedSchemaSuid = 0;
18✔
751
      return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
18✔
752
    }
753

754
    pReader->cachedSchemaUid = uid;
1,908✔
755
    pReader->cachedSchemaSuid = suid;
1,908✔
756
    pReader->cachedSchemaVer = sversion;
1,908✔
757

758
    if (pReader->cachedSchemaVer != pReader->pSchemaWrapper->version) {
1,908!
759
      tqError("vgId:%d, schema version mismatch, suid:%" PRId64 ", uid:%" PRId64 ", version:%d, cached version:%d",
×
760
              vgId, suid, uid, sversion, pReader->pSchemaWrapper->version);
761
      return TSDB_CODE_TQ_INTERNAL_ERROR;
×
762
    }
763
    code = buildResSDataBlock(pReader, pReader->pSchemaWrapper, pReader->pColIdList);
1,908✔
764
    TSDB_CHECK_CODE(code, line, END);
1,909!
765
    pBlock = pReader->pResBlock;
1,909✔
766
    *pRes = pBlock;
1,909✔
767
  }
768

769
  int32_t numOfRows = 0;
872,977✔
770
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
872,977✔
771
    SColData* pCol = taosArrayGet(pSubmitTbData->aCol, 0);
4✔
772
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
4!
773
    numOfRows = pCol->nVal;
4✔
774
  } else {
775
    numOfRows = taosArrayGetSize(pSubmitTbData->aRowP);
872,973✔
776
  }
777

778
  code = blockDataEnsureCapacity(pBlock, numOfRows);
872,978✔
779
  TSDB_CHECK_CODE(code, line, END);
872,980!
780
  pBlock->info.rows = numOfRows;
872,980✔
781
  int32_t colActual = blockDataGetNumOfCols(pBlock);
872,980✔
782

783
  // convert and scan one block
784
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
872,981✔
785
    SArray* pCols = pSubmitTbData->aCol;
4✔
786
    int32_t numOfCols = taosArrayGetSize(pCols);
4✔
787
    int32_t targetIdx = 0;
4✔
788
    int32_t sourceIdx = 0;
4✔
789
    while (targetIdx < colActual) {
18✔
790
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
14✔
791
      TSDB_CHECK_NULL(pColData, code, line, END, terrno);
14!
792
      if (sourceIdx >= numOfCols) {
14✔
793
        tqError("lostdata tqRetrieveDataBlock sourceIdx:%d >= numOfCols:%d", sourceIdx, numOfCols);
4!
794
        colDataSetNNULL(pColData, 0, numOfRows);
4!
795
        targetIdx++;
4✔
796
        continue;
4✔
797
      }
798

799
      SColData* pCol = taosArrayGet(pCols, sourceIdx);
10✔
800
      TSDB_CHECK_NULL(pCol, code, line, END, terrno);
10!
801
      SColVal colVal = {0};
10✔
802
      tqTrace("lostdata colActual:%d, sourceIdx:%d, targetIdx:%d, numOfCols:%d, source cid:%d, dst cid:%d", colActual,
10!
803
              sourceIdx, targetIdx, numOfCols, pCol->cid, pColData->info.colId);
804
      if (pCol->cid < pColData->info.colId) {
10✔
805
        sourceIdx++;
4✔
806
      } else if (pCol->cid == pColData->info.colId) {
6✔
807
        for (int32_t i = 0; i < pCol->nVal; i++) {
12✔
808
          code = tColDataGetValue(pCol, i, &colVal);
8✔
809
          TSDB_CHECK_CODE(code, line, END);
8!
810
          code = doSetVal(pColData, i, &colVal);
8✔
811
          TSDB_CHECK_CODE(code, line, END);
8!
812
        }
813
        sourceIdx++;
4✔
814
        targetIdx++;
4✔
815
      } else {
816
        colDataSetNNULL(pColData, 0, numOfRows);
2!
817
        targetIdx++;
2✔
818
      }
819
    }
820
  } else {
821
    SArray*         pRows = pSubmitTbData->aRowP;
872,977✔
822
    SSchemaWrapper* pWrapper = pReader->pSchemaWrapper;
872,977✔
823
    pTSchema = tBuildTSchema(pWrapper->pSchema, pWrapper->nCols, pWrapper->version);
872,977✔
824
    TSDB_CHECK_NULL(pTSchema, code, line, END, terrno);
873,012!
825

826
    for (int32_t i = 0; i < numOfRows; i++) {
52,668,573✔
827
      SRow* pRow = taosArrayGetP(pRows, i);
51,796,917✔
828
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
51,751,581!
829
      int32_t sourceIdx = 0;
51,758,046✔
830
      for (int32_t j = 0; j < colActual; j++) {
199,471,858✔
831
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, j);
147,676,297✔
832
        TSDB_CHECK_NULL(pColData, code, line, END, terrno);
147,604,833!
833

834
        while (1) {
9,984,348✔
835
          SColVal colVal = {0};
157,589,181✔
836
          code = tRowGet(pRow, pTSchema, sourceIdx, &colVal);
157,589,181✔
837
          TSDB_CHECK_CODE(code, line, END);
157,415,861!
838

839
          if (colVal.cid < pColData->info.colId) {
157,415,861✔
840
            sourceIdx++;
9,984,348✔
841
            continue;
9,984,348✔
842
          } else if (colVal.cid == pColData->info.colId) {
147,431,513!
843
            code = doSetVal(pColData, i, &colVal);
147,722,767✔
844
            TSDB_CHECK_CODE(code, line, END);
148,005,066!
845
            sourceIdx++;
148,005,066✔
846
            break;
147,713,812✔
847
          } else {
848
            colDataSetNULL(pColData, i);
×
849
            break;
×
850
          }
851
        }
852
      }
853
    }
854
  }
855

856
END:
871,656✔
857
  if (code != 0) {
871,660!
858
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
859
  }
860
  taosMemoryFreeClear(pTSchema);
872,977!
861
  return code;
873,028✔
862
}
863

864
#define PROCESS_VAL                                      \
865
  if (curRow == 0) {                                     \
866
    assigned[j] = !COL_VAL_IS_NONE(&colVal);             \
867
    buildNew = true;                                     \
868
  } else {                                               \
869
    bool currentRowAssigned = !COL_VAL_IS_NONE(&colVal); \
870
    if (currentRowAssigned != assigned[j]) {             \
871
      assigned[j] = currentRowAssigned;                  \
872
      buildNew = true;                                   \
873
    }                                                    \
874
  }
875

876
#define SET_DATA                                                     \
877
  if (colVal.cid < pColData->info.colId) {                           \
878
    sourceIdx++;                                                     \
879
  } else if (colVal.cid == pColData->info.colId) {                   \
880
    TQ_ERR_GO_TO_END(doSetVal(pColData, curRow - lastRow, &colVal)); \
881
    sourceIdx++;                                                     \
882
    targetIdx++;                                                     \
883
  }
884

885
static int32_t processBuildNew(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas,
117,956✔
886
                               SSchemaWrapper* pSchemaWrapper, char* assigned, int32_t numOfRows, int32_t curRow,
887
                               int32_t* lastRow) {
888
  int32_t         code = 0;
117,956✔
889
  SSchemaWrapper* pSW = NULL;
117,956✔
890
  SSDataBlock*    block = NULL;
117,956✔
891
  if (taosArrayGetSize(blocks) > 0) {
117,956!
892
    SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
×
893
    TQ_NULL_GO_TO_END(pLastBlock);
×
894
    pLastBlock->info.rows = curRow - *lastRow;
×
895
    *lastRow = curRow;
×
896
  }
897

898
  block = taosMemoryCalloc(1, sizeof(SSDataBlock));
118,024!
899
  TQ_NULL_GO_TO_END(block);
118,139!
900

901
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
118,139!
902
  TQ_NULL_GO_TO_END(pSW);
118,116!
903

904
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pSchemaWrapper, assigned));
118,116!
905
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
118,120!
906
          (int32_t)taosArrayGetSize(block->pDataBlock));
907

908
  block->info.id.uid = pSubmitTbData->uid;
118,120✔
909
  block->info.version = pReader->msg.ver;
118,120✔
910
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
118,120!
911
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
118,075!
912
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
118,056!
913
  pSW = NULL;
118,056✔
914
  taosMemoryFreeClear(block);
118,056!
915

UNCOV
916
END:
×
917
  if (code != 0) {
118,196!
918
    tqError("processBuildNew failed, code:%d", code);
×
919
  }
920
  tDeleteSchemaWrapper(pSW);
118,196!
921
  blockDataFreeRes(block);
118,174✔
922
  taosMemoryFree(block);
118,139!
923
  return code;
118,140✔
924
}
925
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
118✔
926
  int32_t code = 0;
118✔
927
  int32_t curRow = 0;
118✔
928
  int32_t lastRow = 0;
118✔
929

930
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
118✔
931
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
118!
932
  TQ_NULL_GO_TO_END(assigned);
118!
933

934
  SArray*   pCols = pSubmitTbData->aCol;
118✔
935
  SColData* pCol = taosArrayGet(pCols, 0);
118✔
936
  TQ_NULL_GO_TO_END(pCol);
118!
937
  int32_t numOfRows = pCol->nVal;
118✔
938
  int32_t numOfCols = taosArrayGetSize(pCols);
118✔
939
  tqTrace("vgId:%d, tqProcessColData start, col num: %d, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfCols, numOfRows);
118!
940
  for (int32_t i = 0; i < numOfRows; i++) {
325✔
941
    bool buildNew = false;
208✔
942

943
    for (int32_t j = 0; j < numOfCols; j++) {
1,061✔
944
      pCol = taosArrayGet(pCols, j);
853✔
945
      TQ_NULL_GO_TO_END(pCol);
853!
946
      SColVal colVal = {0};
853✔
947
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
853!
948
      PROCESS_VAL
853!
949
    }
950

951
    if (buildNew) {
208✔
952
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, pSchemaWrapper, assigned, numOfRows,
118!
953
                                       curRow, &lastRow));
954
    }
955

956
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
208✔
957
    TQ_NULL_GO_TO_END(pBlock);
208!
958

959
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
208!
960
            (int32_t)taosArrayGetSize(blocks));
961

962
    int32_t targetIdx = 0;
208✔
963
    int32_t sourceIdx = 0;
208✔
964
    int32_t colActual = blockDataGetNumOfCols(pBlock);
208✔
965
    while (targetIdx < colActual) {
1,059✔
966
      pCol = taosArrayGet(pCols, sourceIdx);
852✔
967
      TQ_NULL_GO_TO_END(pCol);
852!
968
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
852✔
969
      TQ_NULL_GO_TO_END(pColData);
852!
970
      SColVal colVal = {0};
852✔
971
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
852!
972
      SET_DATA
852!
973
    }
974

975
    curRow++;
207✔
976
  }
977
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
117✔
978
  pLastBlock->info.rows = curRow - lastRow;
118✔
979
  tqTrace("vgId:%d, tqProcessColData end, col num: %d, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfCols, numOfRows, (int)taosArrayGetSize(blocks));
118!
980
END:
118✔
981
  if (code != TSDB_CODE_SUCCESS) {
118!
982
    tqError("vgId:%d, process col data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
983
  }
984
  taosMemoryFree(assigned);
118!
985
  return code;
118✔
986
}
987

988
int32_t tqProcessRowData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
117,987✔
989
  int32_t   code = 0;
117,987✔
990
  STSchema* pTSchema = NULL;
117,987✔
991

992
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
117,987✔
993
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
117,987!
994
  TQ_NULL_GO_TO_END(assigned);
118,078!
995

996
  int32_t curRow = 0;
118,078✔
997
  int32_t lastRow = 0;
118,078✔
998
  SArray* pRows = pSubmitTbData->aRowP;
118,078✔
999
  int32_t numOfRows = taosArrayGetSize(pRows);
118,078✔
1000
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
118,012✔
1001
  TQ_NULL_GO_TO_END(pTSchema);
118,000!
1002
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
118,000!
1003

1004
  for (int32_t i = 0; i < numOfRows; i++) {
3,514,781✔
1005
    bool  buildNew = false;
3,367,216✔
1006
    SRow* pRow = taosArrayGetP(pRows, i);
3,367,216✔
1007
    TQ_NULL_GO_TO_END(pRow);
3,360,043!
1008

1009
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
18,141,060✔
1010
      SColVal colVal = {0};
14,794,368✔
1011
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
14,794,368✔
1012
      PROCESS_VAL
14,780,618!
1013
    }
1014

1015
    if (buildNew) {
3,346,692✔
1016
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, pSchemaWrapper, assigned, numOfRows,
117,929!
1017
                                       curRow, &lastRow));
1018
    }
1019

1020
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
3,346,775✔
1021
    TQ_NULL_GO_TO_END(pBlock);
3,348,599!
1022

1023
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
3,348,599!
1024
            (int32_t)taosArrayGetSize(blocks));
1025

1026
    int32_t targetIdx = 0;
3,348,599✔
1027
    int32_t sourceIdx = 0;
3,348,599✔
1028
    int32_t colActual = blockDataGetNumOfCols(pBlock);
3,348,599✔
1029
    while (targetIdx < colActual) {
18,108,414✔
1030
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
14,711,552✔
1031
      SColVal          colVal = {0};
14,675,043✔
1032
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
14,675,043!
1033
      SET_DATA
14,595,289!
1034
    }
1035

1036
    curRow++;
3,396,862✔
1037
  }
1038
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
147,565✔
1039
  pLastBlock->info.rows = curRow - lastRow;
117,889✔
1040

1041
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows, (int)taosArrayGetSize(blocks));
117,889!
1042
END:
117,889✔
1043
  if (code != TSDB_CODE_SUCCESS) {
117,899✔
1044
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
4!
1045
  }
1046
  taosMemoryFreeClear(pTSchema);
117,899!
1047
  taosMemoryFree(assigned);
118,062!
1048
  return code;
118,007✔
1049
}
1050

1051
static int32_t buildCreateTbInfo(SMqDataRsp* pRsp, SVCreateTbReq* pCreateTbReq){
1,072✔
1052
  int32_t code = 0;
1,072✔
1053
  int32_t lino = 0;
1,072✔
1054
  void*   createReq = NULL;
1,072✔
1055
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
1,072!
1056
  TSDB_CHECK_NULL(pCreateTbReq, code, lino, END, TSDB_CODE_INVALID_PARA);
1,072!
1057

1058
  if (pRsp->createTableNum == 0) {
1,072✔
1059
    pRsp->createTableLen = taosArrayInit(0, sizeof(int32_t));
83✔
1060
    TSDB_CHECK_NULL(pRsp->createTableLen, code, lino, END, terrno);
83!
1061
    pRsp->createTableReq = taosArrayInit(0, sizeof(void*));
83✔
1062
    TSDB_CHECK_NULL(pRsp->createTableReq, code, lino, END, terrno);
83!
1063
  }
1064

1065
  uint32_t len = 0;
1,072✔
1066
  tEncodeSize(tEncodeSVCreateTbReq, pCreateTbReq, len, code);
1,072!
1067
  TSDB_CHECK_CODE(code, lino, END);
1,072!
1068
  createReq = taosMemoryCalloc(1, len);
1,072!
1069
  TSDB_CHECK_NULL(createReq, code, lino, END, terrno);
1,072!
1070

1071
  SEncoder encoder = {0};
1,072✔
1072
  tEncoderInit(&encoder, createReq, len);
1,072✔
1073
  code = tEncodeSVCreateTbReq(&encoder, pCreateTbReq);
1,072✔
1074
  tEncoderClear(&encoder);
1,072✔
1075
  TSDB_CHECK_CODE(code, lino, END);
1,072!
1076
  TSDB_CHECK_NULL(taosArrayPush(pRsp->createTableLen, &len), code, lino, END, terrno);
2,144!
1077
  TSDB_CHECK_NULL(taosArrayPush(pRsp->createTableReq, &createReq), code, lino, END, terrno);
2,144!
1078
  pRsp->createTableNum++;
1,072✔
1079
  tqTrace("build create table info msg success");
1,072!
1080

1081
  END:
1,072✔
1082
  if (code != 0){
1,072!
1083
    tqError("%s failed at %d, failed to build create table info msg:%s", __FUNCTION__, lino, tstrerror(code));
×
1084
    taosMemoryFree(createReq);
×
1085
  }
1086
  return code;
1,072✔
1087
}
1088

1089
int32_t tqRetrieveTaosxBlock(STqReader* pReader, SMqDataRsp* pRsp, SArray* blocks, SArray* schemas, SSubmitTbData** pSubmitTbDataRet, SArray* rawList, int8_t fetchMeta) {
118,657✔
1090
  tqTrace("tq reader retrieve data block msg pointer:%p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
118,657!
1091
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
118,657✔
1092
  if (pSubmitTbData == NULL) {
118,653!
1093
    return terrno;
×
1094
  }
1095
  pReader->nextBlk++;
118,653✔
1096

1097
  if (pSubmitTbDataRet) {
118,653!
1098
    *pSubmitTbDataRet = pSubmitTbData;
118,653✔
1099
  }
1100

1101
  int32_t sversion = pSubmitTbData->sver;
118,653✔
1102
  int64_t uid = pSubmitTbData->uid;
118,653✔
1103
  pReader->lastBlkUid = uid;
118,653✔
1104

1105
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
118,653✔
1106
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1);
118,668✔
1107
  if (pReader->pSchemaWrapper == NULL) {
118,627✔
1108
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
482✔
1109
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1110
    pReader->cachedSchemaSuid = 0;
423✔
1111
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
423✔
1112
  }
1113

1114
  if (pSubmitTbData->pCreateTbReq != NULL) {
118,145✔
1115
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
1,072✔
1116
    if (code != 0) {
1,072!
1117
      return code;
×
1118
    }
1119
  } else if (rawList != NULL) {
117,073✔
1120
    if (taosArrayPush(schemas, &pReader->pSchemaWrapper) == NULL){
36!
1121
      return terrno;
×
1122
    }
1123
    pReader->pSchemaWrapper = NULL;
18✔
1124
    return 0;
18✔
1125
  }
1126

1127
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
118,127✔
1128
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
118✔
1129
  } else {
1130
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
118,009✔
1131
  }
1132
}
1133

1134
void tqReaderSetColIdList(STqReader* pReader, SArray* pColIdList) {
8,428✔
1135
  if (pReader == NULL){
8,428!
1136
    return;
×
1137
  }
1138
  pReader->pColIdList = pColIdList;
8,428✔
1139
}
1140

1141
void tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
8,506✔
1142
  if (pReader == NULL || tbUidList == NULL) {
8,506!
UNCOV
1143
    return;
×
1144
  }
1145
  if (pReader->tbIdHash) {
8,506✔
1146
    taosHashClear(pReader->tbIdHash);
14✔
1147
  } else {
1148
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
8,492✔
1149
    if (pReader->tbIdHash == NULL) {
8,492!
1150
      tqError("s-task:%s failed to init hash table", id);
×
1151
      return;
×
1152
    }
1153
  }
1154

1155
  for (int i = 0; i < taosArrayGetSize(tbUidList); i++) {
161,235✔
1156
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
152,722✔
1157
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
152,720!
1158
      tqError("s-task:%s failed to add table uid:%" PRId64 " to hash", id, *pKey);
×
1159
      continue;
×
1160
    }
1161
  }
1162

1163
  tqDebug("s-task:%s %d tables are set to be queried target table", id, (int32_t)taosArrayGetSize(tbUidList));
8,505✔
1164
}
1165

1166
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
20,709✔
1167
  if (pReader == NULL || pTableUidList == NULL) {
20,709!
1168
    return;
×
1169
  }
1170
  if (pReader->tbIdHash == NULL) {
20,710!
1171
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
×
1172
    if (pReader->tbIdHash == NULL) {
×
1173
      tqError("failed to init hash table");
×
1174
      return;
×
1175
    }
1176
  }
1177

1178
  int32_t numOfTables = taosArrayGetSize(pTableUidList);
20,710✔
1179
  for (int i = 0; i < numOfTables; i++) {
21,637✔
1180
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
927✔
1181
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
927!
1182
      tqError("failed to add table uid:%" PRId64 " to hash", *pKey);
×
1183
      continue;
×
1184
    }
1185
  }
1186
}
1187

1188
bool tqReaderIsQueriedTable(STqReader* pReader, uint64_t uid) {
2,423✔
1189
  if (pReader == NULL) {
2,423!
1190
    return false;
×
1191
  }
1192
  return taosHashGet(pReader->tbIdHash, &uid, sizeof(uint64_t)) != NULL;
2,423✔
1193
}
1194

1195
bool tqCurrentBlockConsumed(const STqReader* pReader) {
827,273✔
1196
  if (pReader == NULL) {
827,273!
1197
    return false;
×
1198
  }
1199
  return pReader->msg.msgStr == NULL;
827,273✔
1200
}
1201

1202
void tqReaderRemoveTbUidList(STqReader* pReader, const SArray* tbUidList) {
768✔
1203
  if (pReader == NULL || tbUidList == NULL) {
768!
1204
    return;
×
1205
  }
1206
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
774✔
1207
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
6✔
1208
    if (pKey && taosHashRemove(pReader->tbIdHash, pKey, sizeof(int64_t)) != 0) {
6!
1209
      tqError("failed to remove table uid:%" PRId64 " from hash", *pKey);
1!
1210
    }
1211
  }
1212
}
1213

1214
int32_t tqUpdateTbUidList(STQ* pTq, const SArray* tbUidList, bool isAdd) {
110,319✔
1215
  if (pTq == NULL || tbUidList == NULL) {
110,319!
1216
    return TSDB_CODE_INVALID_PARA;
×
1217
  }
1218
  void*   pIter = NULL;
110,323✔
1219
  int32_t vgId = TD_VID(pTq->pVnode);
110,323✔
1220

1221
  // update the table list for each consumer handle
1222
  taosWLockLatch(&pTq->lock);
110,323✔
1223
  while (1) {
4,113✔
1224
    pIter = taosHashIterate(pTq->pHandle, pIter);
114,438✔
1225
    if (pIter == NULL) {
114,437✔
1226
      break;
110,324✔
1227
    }
1228

1229
    STqHandle* pTqHandle = (STqHandle*)pIter;
4,113✔
1230
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
4,113✔
1231
      int32_t code = qUpdateTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList, isAdd);
1,002✔
1232
      if (code != 0) {
1,002!
1233
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1234
        continue;
×
1235
      }
1236
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
3,111✔
1237
      if (!isAdd) {
3,097✔
1238
        int32_t sz = taosArrayGetSize(tbUidList);
1,004✔
1239
        for (int32_t i = 0; i < sz; i++) {
1,004!
1240
          int64_t* tbUid = (int64_t*)taosArrayGet(tbUidList, i);
×
1241
          if (tbUid &&
×
1242
              taosHashPut(pTqHandle->execHandle.execDb.pFilterOutTbUid, tbUid, sizeof(int64_t), NULL, 0) != 0) {
×
1243
            tqError("failed to add table uid:%" PRId64 " to hash", *tbUid);
×
1244
            continue;
×
1245
          }
1246
        }
1247
      }
1248
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
14!
1249
      if (isAdd) {
14!
1250
        SArray* list = NULL;
14✔
1251
        int     ret = qGetTableList(pTqHandle->execHandle.execTb.suid, pTq->pVnode, pTqHandle->execHandle.execTb.node,
14✔
1252
                                    &list, pTqHandle->execHandle.task);
1253
        if (ret != TDB_CODE_SUCCESS) {
14!
1254
          tqError("qGetTableList in tqUpdateTbUidList error:%d handle %s consumer:0x%" PRIx64, ret, pTqHandle->subKey,
×
1255
                  pTqHandle->consumerId);
1256
          taosArrayDestroy(list);
×
1257
          taosHashCancelIterate(pTq->pHandle, pIter);
×
1258
          taosWUnLockLatch(&pTq->lock);
×
1259

1260
          return ret;
×
1261
        }
1262
        tqReaderSetTbUidList(pTqHandle->execHandle.pTqReader, list, NULL);
14✔
1263
        taosArrayDestroy(list);
14✔
1264
      } else {
1265
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
×
1266
      }
1267
    }
1268
  }
1269
  taosWUnLockLatch(&pTq->lock);
110,324✔
1270

1271
  // update the table list handle for each stream scanner/wal reader
1272
  streamMetaWLock(pTq->pStreamMeta);
110,326✔
1273
  while (1) {
41,858✔
1274
    pIter = taosHashIterate(pTq->pStreamMeta->pTasksMap, pIter);
152,182✔
1275
    if (pIter == NULL) {
152,179✔
1276
      break;
110,322✔
1277
    }
1278

1279
    int64_t      refId = *(int64_t*)pIter;
41,857✔
1280
    SStreamTask* pTask = taosAcquireRef(streamTaskRefPool, refId);
41,857✔
1281
    if (pTask != NULL) {
41,864!
1282
      int32_t taskId = pTask->id.taskId;
41,864✔
1283

1284
      if ((pTask->info.taskLevel == TASK_LEVEL__SOURCE) && (pTask->exec.pExecutor != NULL)) {
41,864✔
1285
        int32_t code = qUpdateTableListForStreamScanner(pTask->exec.pExecutor, tbUidList, isAdd);
20,480✔
1286
        if (code != 0) {
20,475✔
1287
          tqError("vgId:%d, s-task:0x%x update qualified table error for stream task", vgId, taskId);
15!
1288
        }
1289
      }
1290
      int32_t ret = taosReleaseRef(streamTaskRefPool, refId);
41,859✔
1291
      if (ret) {
41,858!
1292
        tqError("vgId:%d release task refId failed, refId:%" PRId64, vgId, refId);
×
1293
      }
1294
    }
1295
  }
1296

1297
  streamMetaWUnLock(pTq->pStreamMeta);
110,322✔
1298
  return 0;
110,319✔
1299
}
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