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

taosdata / TDengine / #3627

02 Mar 2025 11:16PM UTC coverage: 63.596% (-0.2%) from 63.764%
#3627

push

travis-ci

GitHub
Merge pull request #29973 from taosdata/doc/internal

148665 of 299855 branches covered (49.58%)

Branch coverage included in aggregate %.

233076 of 300407 relevant lines covered (77.59%)

17543856.65 hits per line

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

64.88
/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) {
669✔
20
  if (pHandle == NULL || pHead == NULL) {
669!
21
    return false;
×
22
  }
23
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
669✔
24
    return true;
665✔
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) {
134,578✔
206
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
134,578!
207
    return -1;
×
208
  }
209
  int32_t code = -1;
134,648✔
210
  int32_t vgId = TD_VID(pTq->pVnode);
134,648✔
211
  int64_t id = pHandle->pWalReader->readerId;
134,648✔
212

213
  int64_t offset = *fetchOffset;
134,648✔
214
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
134,648✔
215
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
134,628✔
216
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
134,654✔
217

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

222
  while (offset <= appliedVer) {
143,645✔
223
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
131,595!
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,592!
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,589✔
234
      code = walFetchBody(pHandle->pWalReader);
122,069✔
235
      goto END;
122,055✔
236
    } else {
237
      if (pHandle->fetchMeta != WITH_DATA) {
9,520✔
238
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
827✔
239
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
827✔
240
          code = walFetchBody(pHandle->pWalReader);
669✔
241
          if (code < 0) {
669!
242
            goto END;
×
243
          }
244

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

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

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

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

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

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

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

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

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

323
  return pReader;
8,752✔
324
}
325

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

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

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

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

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

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

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

364
  while (1) {
9,082✔
365
    TAOS_CHECK_RETURN(walNextValidMsg(pReader));
527,186✔
366

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

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

378
      void* data = taosMemoryMalloc(len);
442,934!
379
      if (data == NULL) {
442,950!
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,950✔
387
      SPackedData data1 = (SPackedData){.ver = ver, .msgLen = len, .msgStr = data};
442,950✔
388

389
      code = streamDataSubmitNew(&data1, STREAM_INPUT__DATA_SUBMIT, (SStreamDataSubmit**)pItem);
442,950✔
390
      if (code != 0) {
442,910!
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) {
11,668✔
395
      void*   pBody = POINTER_SHIFT(pCont->body, sizeof(SMsgHead));
11,495✔
396
      int32_t len = pCont->bodyLen - sizeof(SMsgHead);
11,495✔
397
      EStreamType blockType = STREAM_DELETE_DATA;
11,495✔
398
      code = tqExtractDelDataBlock(pBody, len, ver, (void**)pItem, 0, blockType);
11,495✔
399
      if (code == TSDB_CODE_SUCCESS) {
11,494!
400
        if (*pItem == NULL) {
11,494✔
401
          tqDebug("s-task:%s empty delete msg, discard it, len:%d, ver:%" PRId64, id, len, ver);
9,082✔
402
          // we need to continue check next data in the wal files.
403
          continue;
9,082✔
404
        } else {
405
          tqDebug("s-task:%s delete msg extract from WAL, len:%d, ver:%" PRId64, id, len, ver);
2,412✔
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) {
358!
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,500✔
433
  }
434
}
435

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

442
  int64_t st = taosGetTimestampMs();
530,195✔
443
  while (1) {
597,452✔
444
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
1,127,647✔
445
    while (pReader->nextBlk < numOfBlocks) {
1,476,508✔
446
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
831,222✔
447
              pReader->msg.ver);
448

449
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
831,222✔
450
      if (pSubmitTbData == NULL) {
831,184✔
451
        tqError("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
6!
452
                pReader->msg.ver);
453
        return false;
×
454
      }
455
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
831,178✔
456
        pReader->nextBlk += 1;
128✔
457
        continue;
128✔
458
      }
459
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
831,049!
460
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
482,331✔
461
        SSDataBlock* pRes = NULL;
482,331✔
462
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
482,331✔
463
        if (code == TSDB_CODE_SUCCESS) {
482,388!
464
          return true;
482,389✔
465
        }
466
      } else {
467
        pReader->nextBlk += 1;
348,834✔
468
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
348,834!
469
      }
470
    }
471

472
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
645,286✔
473
    pReader->msg.msgStr = NULL;
645,292✔
474

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

480
    // try next message in wal file
481
    if (walNextValidMsg(pWalReader) < 0) {
645,271✔
482
      return false;
47,804✔
483
    }
484

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

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

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

506
  tDecoderInit(&decoder, pReader->msg.msgStr, pReader->msg.msgLen);
1,162,879✔
507
  int32_t code = tDecodeSubmitReq(&decoder, &pReader->submit, rawList);
1,162,234✔
508
  tDecoderClear(&decoder);
1,162,561✔
509

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

514
  return code;
1,163,000✔
515
}
516

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

523

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

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

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

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

553
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
880,497✔
554
  while (pReader->nextBlk < blockSz) {
1,003,574✔
555
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
523,617✔
556
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
523,600!
557
    uid = pSubmitTbData->uid;
523,600✔
558
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
523,600✔
559
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
523,635✔
560

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

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

568
END:
474,326✔
569
  tqTrace("%s:%d return:%s, uid:%"PRId64, __FUNCTION__, lino, code?"true":"false", uid);
880,974✔
570
  return code;
880,690✔
571
}
572

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

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

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

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

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

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

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

617
  int32_t j = 0;
118,048✔
618
  for (int32_t i = 0; i < pSrc->nCols; i++) {
704,085✔
619
    if (mask[i]) {
586,025✔
620
      pDst->pSchema[j++] = pSrc->pSchema[i];
586,006✔
621
      SColumnInfoData colInfo =
622
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
586,006✔
623
      code = blockDataAppendColInfo(pBlock, &colInfo);
586,956✔
624
      if (code != 0) {
586,018!
625
        return code;
×
626
      }
627
    }
628
  }
629
  return 0;
118,060✔
630
}
631

632
static int32_t buildResSDataBlock(STqReader* pReader, SSchemaWrapper* pSchema, const SArray* pColIdList) {
1,917✔
633
  if (pReader == NULL || pSchema == NULL || pColIdList == NULL) {
1,917!
634
    return TSDB_CODE_INVALID_PARA;
×
635
  }
636
  SSDataBlock* pBlock = pReader->pResBlock;
1,918✔
637
  if (blockDataGetNumOfCols(pBlock) > 0) {
1,918✔
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,917✔
650

651
  if (numOfCols == 0) {  // all columns are required
1,918!
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,918✔
664
      numOfCols = pSchema->nCols;
2✔
665
    }
666

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

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

693
  return TSDB_CODE_SUCCESS;
1,924✔
694
}
695

696
static int32_t doSetVal(SColumnInfoData* pColumnInfoData, int32_t rowIndex, SColVal* pColVal) {
181,902,451✔
697
  int32_t code = TSDB_CODE_SUCCESS;
181,902,451✔
698

699
  if (IS_VAR_DATA_TYPE(pColVal->value.type)) {
189,961,243!
700
    char val[65535 + 2] = {0};
7,701,950✔
701
    if (COL_VAL_IS_VALUE(pColVal)) {
7,701,950!
702
      if (pColVal->value.pData != NULL) {
8,066,448!
703
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
8,069,253✔
704
      }
705
      varDataSetLen(val, pColVal->value.nData);
8,066,448✔
706
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
8,066,448✔
707
    } else {
708
      colDataSetNULL(pColumnInfoData, rowIndex);
×
709
    }
710
  } else {
711
    code = colDataSetVal(pColumnInfoData, rowIndex, (void*)&pColVal->value.val, !COL_VAL_IS_VALUE(pColVal));
174,200,501✔
712
  }
713

714
  return code;
181,904,555✔
715
}
716

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

730
  blockDataCleanup(pBlock);
850,098✔
731

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

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

741
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
850,061✔
742
      (pReader->cachedSchemaVer != sversion)) {
848,144✔
743
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
1,919✔
744

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

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

758
    if (pReader->cachedSchemaVer != pReader->pSchemaWrapper->version) {
1,917!
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,917✔
764
    TSDB_CHECK_CODE(code, line, END);
1,916!
765
    pBlock = pReader->pResBlock;
1,916✔
766
    *pRes = pBlock;
1,916✔
767
  }
768

769
  int32_t numOfRows = 0;
850,058✔
770
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
850,058✔
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);
850,054✔
776
  }
777

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

783
  // convert and scan one block
784
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
850,055✔
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;
850,051✔
822
    SSchemaWrapper* pWrapper = pReader->pSchemaWrapper;
850,051✔
823
    pTSchema = tBuildTSchema(pWrapper->pSchema, pWrapper->nCols, pWrapper->version);
850,051✔
824
    TSDB_CHECK_NULL(pTSchema, code, line, END, terrno);
850,068!
825

826
    for (int32_t i = 0; i < numOfRows; i++) {
62,491,767✔
827
      SRow* pRow = taosArrayGetP(pRows, i);
61,554,660✔
828
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
61,532,891✔
829
      int32_t sourceIdx = 0;
61,523,893✔
830
      for (int32_t j = 0; j < colActual; j++) {
228,488,813✔
831
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, j);
166,847,115✔
832
        TSDB_CHECK_NULL(pColData, code, line, END, terrno);
166,738,854!
833

834
        while (1) {
20,025,166✔
835
          SColVal colVal = {0};
186,764,020✔
836
          code = tRowGet(pRow, pTSchema, sourceIdx, &colVal);
186,764,020✔
837
          TSDB_CHECK_CODE(code, line, END);
186,538,769!
838

839
          if (colVal.cid < pColData->info.colId) {
186,538,769✔
840
            sourceIdx++;
20,025,166✔
841
            continue;
20,025,166✔
842
          } else if (colVal.cid == pColData->info.colId) {
166,513,603!
843
            code = doSetVal(pColData, i, &colVal);
166,718,699✔
844
            TSDB_CHECK_CODE(code, line, END);
167,170,016!
845
            sourceIdx++;
167,170,016✔
846
            break;
166,964,920✔
847
          } else {
848
            colDataSetNULL(pColData, i);
×
849
            break;
×
850
          }
851
        }
852
      }
853
    }
854
  }
855

856
END:
937,107✔
857
  if (code != 0) {
937,111!
858
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
859
  }
860
  taosMemoryFreeClear(pTSchema);
850,034!
861
  return code;
850,083✔
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,922✔
886
                               SSchemaWrapper* pSchemaWrapper, char* assigned, int32_t numOfRows, int32_t curRow,
887
                               int32_t* lastRow) {
888
  int32_t         code = 0;
117,922✔
889
  SSchemaWrapper* pSW = NULL;
117,922✔
890
  SSDataBlock*    block = NULL;
117,922✔
891
  if (taosArrayGetSize(blocks) > 0) {
117,922!
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));
117,981!
899
  TQ_NULL_GO_TO_END(block);
118,120!
900

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

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

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

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

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

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

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

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

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

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

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

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

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

1004
  for (int32_t i = 0; i < numOfRows; i++) {
3,487,749✔
1005
    bool  buildNew = false;
3,355,816✔
1006
    SRow* pRow = taosArrayGetP(pRows, i);
3,355,816✔
1007
    TQ_NULL_GO_TO_END(pRow);
3,354,099!
1008

1009
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
18,170,091✔
1010
      SColVal colVal = {0};
14,835,905✔
1011
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
14,835,905✔
1012
      PROCESS_VAL
14,818,162!
1013
    }
1014

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

1020
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
3,334,260✔
1021
    TQ_NULL_GO_TO_END(pBlock);
3,345,038!
1022

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

1026
    int32_t targetIdx = 0;
3,345,038✔
1027
    int32_t sourceIdx = 0;
3,345,038✔
1028
    int32_t colActual = blockDataGetNumOfCols(pBlock);
3,345,038✔
1029
    while (targetIdx < colActual) {
18,081,020✔
1030
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
14,711,259✔
1031
      SColVal          colVal = {0};
14,683,231✔
1032
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
14,683,231!
1033
      SET_DATA
14,615,207!
1034
    }
1035

1036
    curRow++;
3,369,761✔
1037
  }
1038
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
131,933✔
1039
  pLastBlock->info.rows = curRow - lastRow;
117,870✔
1040

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

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

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

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

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

1081
  END:
42✔
1082
  if (code != 0){
42!
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;
42✔
1087
}
1088

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

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

1101
  if (fetchMeta == ONLY_META) {
118,618✔
1102
    if (pSubmitTbData->pCreateTbReq != NULL) {
22✔
1103
      if (pRsp->createTableReq == NULL){
4✔
1104
        pRsp->createTableReq = taosArrayInit(0, POINTER_BYTES);
1✔
1105
        if (pRsp->createTableReq == NULL){
1!
1106
          return terrno;
×
1107
        }
1108
      }
1109
      if (taosArrayPush(pRsp->createTableReq, &pSubmitTbData->pCreateTbReq) == NULL){
8!
1110
        return terrno;
×
1111
      }
1112
      pSubmitTbData->pCreateTbReq = NULL;
4✔
1113
    }
1114
    return 0;
22✔
1115
  }
1116

1117
  int32_t sversion = pSubmitTbData->sver;
118,596✔
1118
  int64_t uid = pSubmitTbData->uid;
118,596✔
1119
  pReader->lastBlkUid = uid;
118,596✔
1120

1121
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
118,596✔
1122
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1);
118,603✔
1123
  if (pReader->pSchemaWrapper == NULL) {
118,567✔
1124
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
401!
1125
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1126
    pReader->cachedSchemaSuid = 0;
415✔
1127
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
415✔
1128
  }
1129

1130
  if (pSubmitTbData->pCreateTbReq != NULL) {
118,166✔
1131
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
42✔
1132
    if (code != 0) {
42!
1133
      return code;
×
1134
    }
1135
  } else if (rawList != NULL) {
118,124✔
1136
    if (taosArrayPush(schemas, &pReader->pSchemaWrapper) == NULL){
34!
1137
      return terrno;
×
1138
    }
1139
    pReader->pSchemaWrapper = NULL;
17✔
1140
    return 0;
17✔
1141
  }
1142

1143
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
118,149✔
1144
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
114✔
1145
  } else {
1146
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
118,035✔
1147
  }
1148
}
1149

1150
void tqReaderSetColIdList(STqReader* pReader, SArray* pColIdList) {
8,446✔
1151
  if (pReader == NULL){
8,446!
1152
    return;
×
1153
  }
1154
  pReader->pColIdList = pColIdList;
8,446✔
1155
}
1156

1157
void tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
8,527✔
1158
  if (pReader == NULL || tbUidList == NULL) {
8,527!
1159
    return;
×
1160
  }
1161
  if (pReader->tbIdHash) {
8,527✔
1162
    taosHashClear(pReader->tbIdHash);
17✔
1163
  } else {
1164
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
8,510✔
1165
    if (pReader->tbIdHash == NULL) {
8,511!
1166
      tqError("s-task:%s failed to init hash table", id);
×
1167
      return;
×
1168
    }
1169
  }
1170

1171
  for (int i = 0; i < taosArrayGetSize(tbUidList); i++) {
163,215✔
1172
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
154,584✔
1173
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
154,589!
1174
      tqError("s-task:%s failed to add table uid:%" PRId64 " to hash", id, *pKey);
×
1175
      continue;
×
1176
    }
1177
  }
1178

1179
  tqDebug("s-task:%s %d tables are set to be queried target table", id, (int32_t)taosArrayGetSize(tbUidList));
8,527✔
1180
}
1181

1182
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
20,939✔
1183
  if (pReader == NULL || pTableUidList == NULL) {
20,939!
1184
    return;
×
1185
  }
1186
  if (pReader->tbIdHash == NULL) {
20,940!
1187
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
×
1188
    if (pReader->tbIdHash == NULL) {
×
1189
      tqError("failed to init hash table");
×
1190
      return;
×
1191
    }
1192
  }
1193

1194
  int32_t numOfTables = taosArrayGetSize(pTableUidList);
20,940✔
1195
  for (int i = 0; i < numOfTables; i++) {
21,903✔
1196
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
962✔
1197
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
962!
1198
      tqError("failed to add table uid:%" PRId64 " to hash", *pKey);
×
1199
      continue;
×
1200
    }
1201
  }
1202
}
1203

1204
bool tqReaderIsQueriedTable(STqReader* pReader, uint64_t uid) {
2,435✔
1205
  if (pReader == NULL) {
2,435!
1206
    return false;
×
1207
  }
1208
  return taosHashGet(pReader->tbIdHash, &uid, sizeof(uint64_t)) != NULL;
2,435✔
1209
}
1210

1211
bool tqCurrentBlockConsumed(const STqReader* pReader) {
828,635✔
1212
  if (pReader == NULL) {
828,635!
1213
    return false;
×
1214
  }
1215
  return pReader->msg.msgStr == NULL;
828,635✔
1216
}
1217

1218
void tqReaderRemoveTbUidList(STqReader* pReader, const SArray* tbUidList) {
763✔
1219
  if (pReader == NULL || tbUidList == NULL) {
763!
1220
    return;
×
1221
  }
1222
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
769✔
1223
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
6✔
1224
    if (pKey && taosHashRemove(pReader->tbIdHash, pKey, sizeof(int64_t)) != 0) {
6!
1225
      tqError("failed to remove table uid:%" PRId64 " from hash", *pKey);
1!
1226
    }
1227
  }
1228
}
1229

1230
int32_t tqUpdateTbUidList(STQ* pTq, const SArray* tbUidList, bool isAdd) {
107,588✔
1231
  if (pTq == NULL || tbUidList == NULL) {
107,588!
1232
    return TSDB_CODE_INVALID_PARA;
×
1233
  }
1234
  void*   pIter = NULL;
107,590✔
1235
  int32_t vgId = TD_VID(pTq->pVnode);
107,590✔
1236

1237
  // update the table list for each consumer handle
1238
  taosWLockLatch(&pTq->lock);
107,590✔
1239
  while (1) {
4,172✔
1240
    pIter = taosHashIterate(pTq->pHandle, pIter);
111,762✔
1241
    if (pIter == NULL) {
111,762✔
1242
      break;
107,590✔
1243
    }
1244

1245
    STqHandle* pTqHandle = (STqHandle*)pIter;
4,172✔
1246
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
4,172✔
1247
      int32_t code = qUpdateTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList, isAdd);
1,058✔
1248
      if (code != 0) {
1,058!
1249
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1250
        continue;
×
1251
      }
1252
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
3,114✔
1253
      if (!isAdd) {
3,097✔
1254
        int32_t sz = taosArrayGetSize(tbUidList);
1,004✔
1255
        for (int32_t i = 0; i < sz; i++) {
1,004!
1256
          int64_t* tbUid = (int64_t*)taosArrayGet(tbUidList, i);
×
1257
          if (tbUid &&
×
1258
              taosHashPut(pTqHandle->execHandle.execDb.pFilterOutTbUid, tbUid, sizeof(int64_t), NULL, 0) != 0) {
×
1259
            tqError("failed to add table uid:%" PRId64 " to hash", *tbUid);
×
1260
            continue;
×
1261
          }
1262
        }
1263
      }
1264
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
17!
1265
      if (isAdd) {
17!
1266
        SArray* list = NULL;
17✔
1267
        int     ret = qGetTableList(pTqHandle->execHandle.execTb.suid, pTq->pVnode, pTqHandle->execHandle.execTb.node,
17✔
1268
                                    &list, pTqHandle->execHandle.task);
1269
        if (ret != TDB_CODE_SUCCESS) {
17!
1270
          tqError("qGetTableList in tqUpdateTbUidList error:%d handle %s consumer:0x%" PRIx64, ret, pTqHandle->subKey,
×
1271
                  pTqHandle->consumerId);
1272
          taosArrayDestroy(list);
×
1273
          taosHashCancelIterate(pTq->pHandle, pIter);
×
1274
          taosWUnLockLatch(&pTq->lock);
×
1275

1276
          return ret;
×
1277
        }
1278
        tqReaderSetTbUidList(pTqHandle->execHandle.pTqReader, list, NULL);
17✔
1279
        taosArrayDestroy(list);
17✔
1280
      } else {
1281
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
×
1282
      }
1283
    }
1284
  }
1285
  taosWUnLockLatch(&pTq->lock);
107,590✔
1286

1287
  // update the table list handle for each stream scanner/wal reader
1288
  streamMetaWLock(pTq->pStreamMeta);
107,586✔
1289
  while (1) {
42,196✔
1290
    pIter = taosHashIterate(pTq->pStreamMeta->pTasksMap, pIter);
149,786✔
1291
    if (pIter == NULL) {
149,784✔
1292
      break;
107,591✔
1293
    }
1294

1295
    int64_t      refId = *(int64_t*)pIter;
42,193✔
1296
    SStreamTask* pTask = taosAcquireRef(streamTaskRefPool, refId);
42,193✔
1297
    if (pTask != NULL) {
42,196!
1298
      int32_t taskId = pTask->id.taskId;
42,196✔
1299

1300
      if ((pTask->info.taskLevel == TASK_LEVEL__SOURCE) && (pTask->exec.pExecutor != NULL)) {
42,196✔
1301
        int32_t code = qUpdateTableListForStreamScanner(pTask->exec.pExecutor, tbUidList, isAdd);
20,652✔
1302
        if (code != 0) {
20,649✔
1303
          tqError("vgId:%d, s-task:0x%x update qualified table error for stream task", vgId, taskId);
19!
1304
        }
1305
      }
1306
      int32_t ret = taosReleaseRef(streamTaskRefPool, refId);
42,193✔
1307
      if (ret) {
42,197!
1308
        tqError("vgId:%d release task refId failed, refId:%" PRId64, vgId, refId);
×
1309
      }
1310
    }
1311
  }
1312

1313
  streamMetaWUnLock(pTq->pStreamMeta);
107,591✔
1314
  return 0;
107,590✔
1315
}
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