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

taosdata / TDengine / #3625

26 Feb 2025 10:19AM UTC coverage: 63.633% (+0.1%) from 63.485%
#3625

push

travis-ci

web-flow
Merge pull request #29914 from taosdata/feat/TS-5613-3.0

feat:[TS-5613]support bool in cast

148738 of 299799 branches covered (49.61%)

Branch coverage included in aggregate %.

233124 of 300297 relevant lines covered (77.63%)

17654074.26 hits per line

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

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

213
  int64_t offset = *fetchOffset;
131,892✔
214
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
131,892✔
215
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
131,881✔
216
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
131,938✔
217

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

222
  while (offset <= appliedVer) {
140,729✔
223
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
129,287!
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,
129,272!
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) {
129,282✔
234
      code = walFetchBody(pHandle->pWalReader);
120,077✔
235
      goto END;
120,061✔
236
    } else {
237
      if (pHandle->fetchMeta != WITH_DATA) {
9,205✔
238
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
850✔
239
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
850✔
240
          code = walFetchBody(pHandle->pWalReader);
670✔
241
          if (code < 0) {
670!
242
            goto END;
×
243
          }
244

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

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

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

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

282
  if (pReader == NULL) {
180!
283
    return;
×
284
  }
285
  bool            ret = false;
180✔
286
  SSchemaWrapper* schema = metaGetTableSchema(pReader->pVnodeMeta, uid, -1, 1);
180✔
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;
180✔
292
}
293

294
STqReader* tqReaderOpen(SVnode* pVnode) {
8,720✔
295
  tqDebug("%s:%p", __FUNCTION__ , pVnode);
8,720✔
296
  if (pVnode == NULL) {
8,736!
297
    return NULL;
×
298
  }
299
  STqReader* pReader = taosMemoryCalloc(1, sizeof(STqReader));
8,736!
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,770✔
327
  tqDebug("%s:%p", __FUNCTION__ , pReader);
8,770✔
328
  if (pReader == NULL) return;
8,772✔
329

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

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

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

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

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

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

364
  while (1) {
8,675✔
365
    TAOS_CHECK_RETURN(walNextValidMsg(pReader));
527,998✔
366

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

374
    if (pCont->msgType == TDMT_VND_SUBMIT) {
455,490✔
375
      void*   pBody = POINTER_SHIFT(pCont->body, sizeof(SSubmitReq2Msg));
444,242✔
376
      int32_t len = pCont->bodyLen - sizeof(SSubmitReq2Msg);
444,242✔
377

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

389
      code = streamDataSubmitNew(&data1, STREAM_INPUT__DATA_SUBMIT, (SStreamDataSubmit**)pItem);
444,249✔
390
      if (code != 0) {
444,251✔
391
        tqError("%s failed to create data submit for stream since out of memory", id);
1!
392
        return code;
×
393
      }
394
    } else if (pCont->msgType == TDMT_VND_DELETE) {
11,248✔
395
      void*   pBody = POINTER_SHIFT(pCont->body, sizeof(SMsgHead));
11,070✔
396
      int32_t len = pCont->bodyLen - sizeof(SMsgHead);
11,070✔
397
      EStreamType blockType = STREAM_DELETE_DATA;
11,070✔
398
      code = tqExtractDelDataBlock(pBody, len, ver, (void**)pItem, 0, blockType);
11,070✔
399
      if (code == TSDB_CODE_SUCCESS) {
11,070!
400
        if (*pItem == NULL) {
11,070✔
401
          tqDebug("s-task:%s empty delete msg, discard it, len:%d, ver:%" PRId64, id, len, ver);
8,675✔
402
          // we need to continue check next data in the wal files.
403
          continue;
8,675✔
404
        } else {
405
          tqDebug("s-task:%s delete msg extract from WAL, len:%d, ver:%" PRId64, id, len, ver);
2,395✔
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) {
363!
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;
446,828✔
433
  }
434
}
435

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

442
  int64_t st = taosGetTimestampMs();
523,728✔
443
  while (1) {
515,425✔
444
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
1,039,153✔
445
    while (pReader->nextBlk < numOfBlocks) {
1,303,221✔
446
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
741,123✔
447
              pReader->msg.ver);
448

449
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
741,123✔
450
      if (pSubmitTbData == NULL) {
741,100!
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) {
741,104✔
456
        pReader->nextBlk += 1;
129✔
457
        continue;
129✔
458
      }
459
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
740,974!
460
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
477,033✔
461
        SSDataBlock* pRes = NULL;
477,033✔
462
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
477,033✔
463
        if (code == TSDB_CODE_SUCCESS) {
477,082!
464
          return true;
477,083✔
465
        }
466
      } else {
467
        pReader->nextBlk += 1;
264,054✔
468
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
264,054!
469
      }
470
    }
471

472
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
562,098✔
473
    pReader->msg.msgStr = NULL;
562,129✔
474

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

480
    // try next message in wal file
481
    if (walNextValidMsg(pWalReader) < 0) {
562,102✔
482
      return false;
46,648✔
483
    }
484

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

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

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

506
  tDecoderInit(&decoder, pReader->msg.msgStr, pReader->msg.msgLen);
1,080,194✔
507
  int32_t code = tDecodeSubmitReq(&decoder, &pReader->submit, rawList);
1,080,132✔
508
  tDecoderClear(&decoder);
1,079,860✔
509

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

514
  return code;
1,080,201✔
515
}
516

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

523

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

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

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

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

553
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
881,059✔
554
  while (pReader->nextBlk < blockSz) {
1,004,964✔
555
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
523,909✔
556
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
523,895!
557
    uid = pSubmitTbData->uid;
523,895✔
558
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
523,895✔
559
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
523,963✔
560

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

565
  tqReaderClearSubmitMsg(pReader);
481,055✔
566
  tqTrace("iterator data block end, total block num:%d, uid:%"PRId64, blockSz, uid);
481,143✔
567

568
END:
475,385✔
569
  tqTrace("%s:%d return:%s, uid:%"PRId64, __FUNCTION__, lino, code?"true":"false", uid);
881,211✔
570
  return code;
881,162✔
571
}
572

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

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

582
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
166,976✔
583
  while (pReader->nextBlk < blockSz) {
166,967✔
584
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
83,654✔
585
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
83,649!
586
    uid = pSubmitTbData->uid;
83,649✔
587
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
83,649✔
588
    TSDB_CHECK_NULL(ret, code, lino, END, true);
83,654!
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);
83,313✔
593
  tqTrace("iterator data block end, total block num:%d, uid:%"PRId64, blockSz, uid);
83,564!
594

595
END:
83,564✔
596
  tqTrace("%s:%d get data:%s, uid:%"PRId64, __FUNCTION__, lino, code?"true":"false", uid);
167,218!
597
  return code;
167,078✔
598
}
599

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

606
  int32_t cnt = 0;
116,090✔
607
  for (int32_t i = 0; i < pSrc->nCols; i++) {
688,278✔
608
    cnt += mask[i];
572,188✔
609
  }
610

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

617
  int32_t j = 0;
116,033✔
618
  for (int32_t i = 0; i < pSrc->nCols; i++) {
687,307✔
619
    if (mask[i]) {
571,224✔
620
      pDst->pSchema[j++] = pSrc->pSchema[i];
571,196✔
621
      SColumnInfoData colInfo =
622
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
571,196✔
623
      code = blockDataAppendColInfo(pBlock, &colInfo);
572,042✔
624
      if (code != 0) {
571,246!
625
        return code;
×
626
      }
627
    }
628
  }
629
  return 0;
116,083✔
630
}
631

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

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

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

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

693
  return TSDB_CODE_SUCCESS;
1,917✔
694
}
695

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

699
  if (IS_VAR_DATA_TYPE(pColVal->value.type)) {
190,091,631!
700
    char val[65535 + 2] = {0};
7,763,529✔
701
    if (COL_VAL_IS_VALUE(pColVal)) {
7,763,529!
702
      if (pColVal->value.pData != NULL) {
8,192,068!
703
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
8,195,169✔
704
      }
705
      varDataSetLen(val, pColVal->value.nData);
8,192,068✔
706
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
8,192,068✔
707
    } else {
708
      colDataSetNULL(pColumnInfoData, rowIndex);
×
709
    }
710
  } else {
711
    code = colDataSetVal(pColumnInfoData, rowIndex, (void*)&pColVal->value.val, !COL_VAL_IS_VALUE(pColVal));
174,145,994✔
712
  }
713

714
  return code;
181,993,958✔
715
}
716

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

730
  blockDataCleanup(pBlock);
844,207✔
731

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

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

741
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
844,162✔
742
      (pReader->cachedSchemaVer != sversion)) {
842,241✔
743
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
1,925✔
744

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

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

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

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

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

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

826
    for (int32_t i = 0; i < numOfRows; i++) {
62,197,735✔
827
      SRow* pRow = taosArrayGetP(pRows, i);
61,556,172✔
828
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
61,514,744✔
829
      int32_t sourceIdx = 0;
61,512,021✔
830
      for (int32_t j = 0; j < colActual; j++) {
228,628,572✔
831
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, j);
167,275,001✔
832
        TSDB_CHECK_NULL(pColData, code, line, END, terrno);
167,177,428!
833

834
        while (1) {
19,995,626✔
835
          SColVal colVal = {0};
187,173,054✔
836
          code = tRowGet(pRow, pTSchema, sourceIdx, &colVal);
187,173,054✔
837
          TSDB_CHECK_CODE(code, line, END);
186,971,391!
838

839
          if (colVal.cid < pColData->info.colId) {
186,971,391✔
840
            sourceIdx++;
19,995,626✔
841
            continue;
19,995,626✔
842
          } else if (colVal.cid == pColData->info.colId) {
166,975,765!
843
            code = doSetVal(pColData, i, &colVal);
167,327,379✔
844
            TSDB_CHECK_CODE(code, line, END);
167,468,165!
845
            sourceIdx++;
167,468,165✔
846
            break;
167,116,551✔
847
          } else {
848
            colDataSetNULL(pColData, i);
×
849
            break;
×
850
          }
851
        }
852
      }
853
    }
854
  }
855

856
END:
641,563✔
857
  if (code != 0) {
641,567!
858
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
859
  }
860
  taosMemoryFreeClear(pTSchema);
844,144!
861
  return code;
844,182✔
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,
115,878✔
886
                               SSchemaWrapper* pSchemaWrapper, char* assigned, int32_t numOfRows, int32_t curRow,
887
                               int32_t* lastRow) {
888
  int32_t         code = 0;
115,878✔
889
  SSchemaWrapper* pSW = NULL;
115,878✔
890
  SSDataBlock*    block = NULL;
115,878✔
891
  if (taosArrayGetSize(blocks) > 0) {
115,878!
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));
115,960!
899
  TQ_NULL_GO_TO_END(block);
116,114!
900

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

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

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

916
END:
×
917
  if (code != 0) {
116,151!
918
    tqError("processBuildNew failed, code:%d", code);
×
919
  }
920
  tDeleteSchemaWrapper(pSW);
116,151!
921
  blockDataFreeRes(block);
116,079✔
922
  taosMemoryFree(block);
116,083!
923
  return code;
116,102✔
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++) {
317✔
941
    bool buildNew = false;
204✔
942

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

951
    if (buildNew) {
203✔
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);
203✔
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,039✔
966
      pCol = taosArrayGet(pCols, sourceIdx);
836✔
967
      TQ_NULL_GO_TO_END(pCol);
836!
968
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
836✔
969
      TQ_NULL_GO_TO_END(pColData);
836!
970
      SColVal colVal = {0};
836✔
971
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
836!
972
      SET_DATA
836!
973
    }
974

975
    curRow++;
203✔
976
  }
977
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
113✔
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) {
115,938✔
989
  int32_t   code = 0;
115,938✔
990
  STSchema* pTSchema = NULL;
115,938✔
991

992
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
115,938✔
993
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
115,938!
994
  TQ_NULL_GO_TO_END(assigned);
116,037!
995

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

1004
  for (int32_t i = 0; i < numOfRows; i++) {
3,481,260✔
1005
    bool  buildNew = false;
3,345,252✔
1006
    SRow* pRow = taosArrayGetP(pRows, i);
3,345,252✔
1007
    TQ_NULL_GO_TO_END(pRow);
3,338,901!
1008

1009
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
17,949,603✔
1010
      SColVal colVal = {0};
14,626,651✔
1011
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
14,626,651✔
1012
      PROCESS_VAL
14,613,099!
1013
    }
1014

1015
    if (buildNew) {
3,322,952✔
1016
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, pSchemaWrapper, assigned, numOfRows,
115,875!
1017
                                       curRow, &lastRow));
1018
    }
1019

1020
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
3,323,044✔
1021
    TQ_NULL_GO_TO_END(pBlock);
3,336,913!
1022

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

1026
    int32_t targetIdx = 0;
3,336,913✔
1027
    int32_t sourceIdx = 0;
3,336,913✔
1028
    int32_t colActual = blockDataGetNumOfCols(pBlock);
3,336,913✔
1029
    while (targetIdx < colActual) {
17,865,924✔
1030
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
14,500,568✔
1031
      SColVal          colVal = {0};
14,468,470✔
1032
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
14,468,470!
1033
      SET_DATA
14,398,257!
1034
    }
1035

1036
    curRow++;
3,365,356✔
1037
  }
1038
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
136,008✔
1039
  pLastBlock->info.rows = curRow - lastRow;
115,793✔
1040

1041
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows, (int)taosArrayGetSize(blocks));
115,793!
1042
END:
115,793✔
1043
  if (code != TSDB_CODE_SUCCESS) {
115,790✔
1044
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
5!
1045
  }
1046
  taosMemoryFreeClear(pTSchema);
115,790!
1047
  taosMemoryFree(assigned);
116,031!
1048
  return code;
116,010✔
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) {
116,621✔
1090
  tqTrace("tq reader retrieve data block msg pointer:%p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
116,621!
1091
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
116,621✔
1092
  if (pSubmitTbData == NULL) {
116,620!
1093
    return terrno;
×
1094
  }
1095
  pReader->nextBlk++;
116,623✔
1096

1097
  if (pSubmitTbDataRet) {
116,623✔
1098
    *pSubmitTbDataRet = pSubmitTbData;
116,622✔
1099
  }
1100

1101
  if (fetchMeta == ONLY_META) {
116,623✔
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;
116,601✔
1118
  int64_t uid = pSubmitTbData->uid;
116,601✔
1119
  pReader->lastBlkUid = uid;
116,601✔
1120

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

1130
  if (pSubmitTbData->pCreateTbReq != NULL) {
116,156✔
1131
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
42✔
1132
    if (code != 0) {
42!
1133
      return code;
×
1134
    }
1135
  } else if (rawList != NULL) {
116,114✔
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) {
116,139✔
1144
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
114✔
1145
  } else {
1146
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
116,025✔
1147
  }
1148
}
1149

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

1157
void tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
8,515✔
1158
  if (pReader == NULL || tbUidList == NULL) {
8,515!
1159
    return;
×
1160
  }
1161
  if (pReader->tbIdHash) {
8,515✔
1162
    taosHashClear(pReader->tbIdHash);
17✔
1163
  } else {
1164
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
8,498✔
1165
    if (pReader->tbIdHash == NULL) {
8,501!
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++) {
126,978✔
1172
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
118,431✔
1173
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
118,414!
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,517✔
1180
}
1181

1182
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
21,125✔
1183
  if (pReader == NULL || pTableUidList == NULL) {
21,125!
1184
    return;
×
1185
  }
1186
  if (pReader->tbIdHash == NULL) {
21,127!
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);
21,127✔
1195
  for (int i = 0; i < numOfTables; i++) {
22,087✔
1196
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
960✔
1197
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
960!
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,417✔
1205
  if (pReader == NULL) {
2,417!
1206
    return false;
×
1207
  }
1208
  return taosHashGet(pReader->tbIdHash, &uid, sizeof(uint64_t)) != NULL;
2,417✔
1209
}
1210

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

1218
void tqReaderRemoveTbUidList(STqReader* pReader, const SArray* tbUidList) {
769✔
1219
  if (pReader == NULL || tbUidList == NULL) {
769!
1220
    return;
×
1221
  }
1222
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
775✔
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) {
106,817✔
1231
  if (pTq == NULL || tbUidList == NULL) {
106,817!
1232
    return TSDB_CODE_INVALID_PARA;
×
1233
  }
1234
  void*   pIter = NULL;
106,819✔
1235
  int32_t vgId = TD_VID(pTq->pVnode);
106,819✔
1236

1237
  // update the table list for each consumer handle
1238
  taosWLockLatch(&pTq->lock);
106,819✔
1239
  while (1) {
4,072✔
1240
    pIter = taosHashIterate(pTq->pHandle, pIter);
110,890✔
1241
    if (pIter == NULL) {
110,890✔
1242
      break;
106,818✔
1243
    }
1244

1245
    STqHandle* pTqHandle = (STqHandle*)pIter;
4,072✔
1246
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
4,072✔
1247
      int32_t code = qUpdateTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList, isAdd);
1,048✔
1248
      if (code != 0) {
1,048!
1249
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1250
        continue;
×
1251
      }
1252
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
3,024✔
1253
      if (!isAdd) {
3,007✔
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);
106,818✔
1286

1287
  // update the table list handle for each stream scanner/wal reader
1288
  streamMetaWLock(pTq->pStreamMeta);
106,818✔
1289
  while (1) {
42,719✔
1290
    pIter = taosHashIterate(pTq->pStreamMeta->pTasksMap, pIter);
149,537✔
1291
    if (pIter == NULL) {
149,536✔
1292
      break;
106,818✔
1293
    }
1294

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

1300
      if ((pTask->info.taskLevel == TASK_LEVEL__SOURCE) && (pTask->exec.pExecutor != NULL)) {
42,723✔
1301
        int32_t code = qUpdateTableListForStreamScanner(pTask->exec.pExecutor, tbUidList, isAdd);
20,851✔
1302
        if (code != 0) {
20,849✔
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,721✔
1307
      if (ret) {
42,719!
1308
        tqError("vgId:%d release task refId failed, refId:%" PRId64, vgId, refId);
×
1309
      }
1310
    }
1311
  }
1312

1313
  streamMetaWUnLock(pTq->pStreamMeta);
106,818✔
1314
  return 0;
106,818✔
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