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

taosdata / TDengine / #3580

14 Jan 2025 02:54AM UTC coverage: 63.556% (+0.6%) from 62.976%
#3580

push

travis-ci

web-flow
Merge pull request #29504 from taosdata/chore/codeowners

chore: add codeowners

140725 of 284535 branches covered (49.46%)

Branch coverage included in aggregate %.

219148 of 281694 relevant lines covered (77.8%)

19019641.27 hits per line

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

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

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

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

222
  while (offset <= appliedVer) {
145,402✔
223
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
130,868!
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,
130,867!
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) {
130,875✔
234
      code = walFetchBody(pHandle->pWalReader);
120,705✔
235
      goto END;
120,699✔
236
    } else {
237
      if (pHandle->fetchMeta != WITH_DATA) {
10,170✔
238
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
704✔
239
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
704✔
240
          code = walFetchBody(pHandle->pWalReader);
561✔
241
          if (code < 0) {
561!
242
            goto END;
×
243
          }
244

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

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

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

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

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

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

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

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

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

323
  return pReader;
8,764✔
324
}
325

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

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

335
  if (pReader->pSchemaWrapper) {
8,640✔
336
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
1,967!
337
  }
338

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

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

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

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

364
  while (1) {
6,866✔
365
    TAOS_CHECK_RETURN(walNextValidMsg(pReader));
529,140✔
366

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

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

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

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

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

442
  int64_t st = taosGetTimestampMs();
523,488✔
443
  while (1) {
545,858✔
444
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
1,069,346✔
445
    while (pReader->nextBlk < numOfBlocks) {
1,357,253✔
446
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
767,475✔
447
              pReader->msg.ver);
448

449
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
767,475✔
450
      if (pSubmitTbData == NULL) {
767,469!
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) {
767,471✔
456
        pReader->nextBlk += 1;
110✔
457
        continue;
110✔
458
      }
459
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
767,355!
460
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
479,562✔
461
        SSDataBlock* pRes = NULL;
479,562✔
462
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
479,562✔
463
        if (code == TSDB_CODE_SUCCESS) {
479,551!
464
          return true;
479,557✔
465
        }
466
      } else {
467
        pReader->nextBlk += 1;
287,830✔
468
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
287,830!
469
      }
470
    }
471

472
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
589,778✔
473
    pReader->msg.msgStr = NULL;
589,807✔
474

475
    int64_t elapsed = taosGetTimestampMs() - st;
589,806✔
476
    if (elapsed > 1000 || elapsed < 0) {
589,806✔
477
      return false;
13✔
478
    }
479

480
    // try next message in wal file
481
    if (walNextValidMsg(pWalReader) < 0) {
589,793✔
482
      return false;
43,933✔
483
    }
484

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

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

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

506
  tDecoderInit(&decoder, pReader->msg.msgStr, pReader->msg.msgLen);
1,112,745✔
507
  int32_t code = tDecodeSubmitReq(&decoder, &pReader->submit);
1,112,733✔
508
  tDecoderClear(&decoder);
1,112,632✔
509

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

514
  return code;
1,112,738✔
515
}
516

517
SWalReader* tqGetWalReader(STqReader* pReader) {
574,300✔
518
  if (pReader == NULL) {
574,300!
519
    return NULL;
×
520
  }
521
  return pReader->pWalReader;
574,300✔
522
}
523

524
SSDataBlock* tqGetResultBlock(STqReader* pReader) {
523,472✔
525
  if (pReader == NULL) {
523,472!
526
    return NULL;
×
527
  }
528
  return pReader->pResBlock;
523,472✔
529
}
530

531
int64_t tqGetResultBlockTime(STqReader* pReader) {
523,463✔
532
  if (pReader == NULL) {
523,463!
533
    return 0;
×
534
  }
535
  return pReader->lastTs;
523,463✔
536
}
537

538
bool tqNextBlockImpl(STqReader* pReader, const char* idstr) {
891,154✔
539
  int32_t code = false;
891,154✔
540
  int32_t lino = 0;
891,154✔
541
  int64_t uid = 0;
891,154✔
542
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
891,154!
543
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
891,154!
544
  TSDB_CHECK_NULL(pReader->tbIdHash, code, lino, END, true);
891,154!
545

546
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
891,154✔
547
  while (pReader->nextBlk < blockSz) {
1,005,796✔
548
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
523,269✔
549
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
523,267!
550
    uid = pSubmitTbData->uid;
523,267✔
551
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
523,267✔
552
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
523,301✔
553

554
    tqDebug("iterator data block in hash continue, progress:%d/%d, total queried tables:%d, uid:%"PRId64, pReader->nextBlk, blockSz, taosHashGetSize(pReader->tbIdHash), uid);
114,629✔
555
    pReader->nextBlk++;
114,642✔
556
  }
557

558
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
482,527✔
559
  pReader->nextBlk = 0;
482,572✔
560
  pReader->msg.msgStr = NULL;
482,572✔
561
  tqDebug("iterator data block end, block progress:%d/%d, uid:%"PRId64, pReader->nextBlk, blockSz, uid);
482,572✔
562

563
END:
52,731✔
564
  tqDebug("%s:%d return:%s, uid:%"PRId64, __FUNCTION__, lino, code?"true":"false", uid);
891,246✔
565
  return code;
891,227✔
566
}
567

568
bool tqNextDataBlockFilterOut(STqReader* pReader, SHashObj* filterOutUids) {
168,649✔
569
  int32_t code = false;
168,649✔
570
  int32_t lino = 0;
168,649✔
571
  int64_t uid = 0;
168,649✔
572

573
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
168,649!
574
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
168,649!
575
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
168,649!
576

577
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
168,649✔
578
  while (pReader->nextBlk < blockSz) {
168,644✔
579
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
84,372✔
580
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
84,370!
581
    uid = pSubmitTbData->uid;
84,370✔
582
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
84,370✔
583
    TSDB_CHECK_NULL(ret, code, lino, END, true);
84,371!
584
    tqDebug("iterator data block in hash continue, progress:%d/%d, uid:%" PRId64 "", pReader->nextBlk, blockSz, uid);
×
585
    pReader->nextBlk++;
×
586
  }
587

588
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
84,272✔
589
  pReader->nextBlk = 0;
84,294✔
590
  pReader->msg.msgStr = NULL;
84,294✔
591
  tqDebug("iterator data block end, block progress:%d/%d, uid:%"PRId64, pReader->nextBlk, blockSz, uid);
84,294!
592

593
END:
×
594
  tqDebug("%s:%d return:%s, uid:%"PRId64, __FUNCTION__, lino, code?"true":"false", uid);
168,670✔
595
  return code;
168,671✔
596
}
597

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

604
  int32_t cnt = 0;
116,848✔
605
  for (int32_t i = 0; i < pSrc->nCols; i++) {
695,133✔
606
    cnt += mask[i];
578,285✔
607
  }
608

609
  pDst->nCols = cnt;
116,848✔
610
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
116,848!
611
  if (pDst->pSchema == NULL) {
116,839!
612
    return TAOS_GET_TERRNO(terrno);
×
613
  }
614

615
  int32_t j = 0;
116,839✔
616
  for (int32_t i = 0; i < pSrc->nCols; i++) {
694,914✔
617
    if (mask[i]) {
578,060✔
618
      pDst->pSchema[j++] = pSrc->pSchema[i];
578,051✔
619
      SColumnInfoData colInfo =
620
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
578,051✔
621
      code = blockDataAppendColInfo(pBlock, &colInfo);
578,216✔
622
      if (code != 0) {
578,066!
623
        return code;
×
624
      }
625
    }
626
  }
627
  return 0;
116,854✔
628
}
629

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

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

647
  int32_t numOfCols = taosArrayGetSize(pColIdList);
1,860✔
648

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

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

665
    int32_t i = 0;
1,859✔
666
    int32_t j = 0;
1,859✔
667
    while (i < pSchema->nCols && j < numOfCols) {
14,804✔
668
      SSchema* pColSchema = &pSchema->pSchema[i];
12,949✔
669
      col_id_t colIdSchema = pColSchema->colId;
12,949✔
670

671
      col_id_t* pColIdNeed = (col_id_t*)taosArrayGet(pColIdList, j);
12,949✔
672
      if (pColIdNeed == NULL) {
12,950!
673
        break;
×
674
      }
675
      if (colIdSchema < *pColIdNeed) {
12,950✔
676
        i++;
1,621✔
677
      } else if (colIdSchema > *pColIdNeed) {
11,329!
678
        j++;
×
679
      } else {
680
        SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
11,329✔
681
        int32_t         code = blockDataAppendColInfo(pBlock, &colInfo);
11,347✔
682
        if (code != TSDB_CODE_SUCCESS) {
11,324!
683
          return -1;
×
684
        }
685
        i++;
11,324✔
686
        j++;
11,324✔
687
      }
688
    }
689
  }
690

691
  return TSDB_CODE_SUCCESS;
1,855✔
692
}
693

694
static int32_t doSetVal(SColumnInfoData* pColumnInfoData, int32_t rowIndex, SColVal* pColVal) {
185,142,770✔
695
  int32_t code = TSDB_CODE_SUCCESS;
185,142,770✔
696

697
  if (IS_VAR_DATA_TYPE(pColVal->value.type)) {
193,453,499!
698
    char val[65535 + 2] = {0};
7,834,537✔
699
    if (COL_VAL_IS_VALUE(pColVal)) {
7,834,537!
700
      if (pColVal->value.pData != NULL) {
8,315,295!
701
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
8,318,777✔
702
      }
703
      varDataSetLen(val, pColVal->value.nData);
8,315,295✔
704
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
8,315,295✔
705
    } else {
706
      colDataSetNULL(pColumnInfoData, rowIndex);
×
707
    }
708
  } else {
709
    code = colDataSetVal(pColumnInfoData, rowIndex, (void*)&pColVal->value.val, !COL_VAL_IS_VALUE(pColVal));
177,308,233✔
710
  }
711

712
  return code;
185,430,098✔
713
}
714

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

728
  blockDataCleanup(pBlock);
855,338✔
729

730
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
855,297✔
731
  int32_t sversion = pSubmitTbData->sver;
855,297✔
732
  int64_t suid = pSubmitTbData->suid;
855,297✔
733
  int64_t uid = pSubmitTbData->uid;
855,297✔
734
  pReader->lastTs = pSubmitTbData->ctimeMs;
855,297✔
735

736
  pBlock->info.id.uid = uid;
855,297✔
737
  pBlock->info.version = pReader->msg.ver;
855,297✔
738

739
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
855,297✔
740
      (pReader->cachedSchemaVer != sversion)) {
853,425!
741
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
1,872✔
742

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

752
    pReader->cachedSchemaUid = uid;
1,859✔
753
    pReader->cachedSchemaSuid = suid;
1,859✔
754
    pReader->cachedSchemaVer = sversion;
1,859✔
755

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

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

776
  code = blockDataEnsureCapacity(pBlock, numOfRows);
855,288✔
777
  TSDB_CHECK_CODE(code, line, END);
855,289!
778
  pBlock->info.rows = numOfRows;
855,289✔
779
  int32_t colActual = blockDataGetNumOfCols(pBlock);
855,289✔
780

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

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

824
    for (int32_t i = 0; i < numOfRows; i++) {
62,772,982✔
825
      SRow* pRow = taosArrayGetP(pRows, i);
62,541,704✔
826
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
62,516,926✔
827
      int32_t sourceIdx = 0;
62,487,129✔
828
      for (int32_t j = 0; j < colActual; j++) {
232,154,106✔
829
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, j);
170,236,425✔
830
        TSDB_CHECK_NULL(pColData, code, line, END, terrno);
170,044,431!
831

832
        while (1) {
19,968,791✔
833
          SColVal colVal = {0};
190,013,222✔
834
          code = tRowGet(pRow, pTSchema, sourceIdx, &colVal);
190,013,222✔
835
          TSDB_CHECK_CODE(code, line, END);
189,396,787!
836

837
          if (colVal.cid < pColData->info.colId) {
189,396,787✔
838
            sourceIdx++;
19,968,791✔
839
            continue;
19,968,791✔
840
          } else if (colVal.cid == pColData->info.colId) {
169,427,996!
841
            code = doSetVal(pColData, i, &colVal);
170,254,145✔
842
            TSDB_CHECK_CODE(code, line, END);
170,493,126!
843
            sourceIdx++;
170,493,126✔
844
            break;
169,666,977✔
845
          } else {
846
            colDataSetNULL(pColData, i);
×
847
            break;
×
848
          }
849
        }
850
      }
851
    }
852
  }
853

854
END:
231,278✔
855
  if (code != 0) {
231,282!
856
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
857
  }
858
  taosMemoryFreeClear(pTSchema);
855,265!
859
  return code;
855,311✔
860
}
861

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

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

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

896
  block = taosMemoryCalloc(1, sizeof(SSDataBlock));
116,846!
897
  TQ_NULL_GO_TO_END(block);
116,845!
898

899
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
116,845!
900
  TQ_NULL_GO_TO_END(pSW);
116,847!
901

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

906
  block->info.id.uid = pSubmitTbData->uid;
116,833✔
907
  block->info.version = pReader->msg.ver;
116,833✔
908
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
116,833!
909
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
116,827!
910
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
116,819!
911
  pSW = NULL;
116,819✔
912
  taosMemoryFreeClear(block);
116,819!
913

914
END:
×
915
  if (code != 0) {
116,842!
916
    tqError("processBuildNew failed, code:%d", code);
×
917
  }
918
  tDeleteSchemaWrapper(pSW);
116,842!
919
  blockDataFreeRes(block);
116,840✔
920
  taosMemoryFree(block);
116,835!
921
  return code;
116,833✔
922
}
923
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
113✔
924
  int32_t code = 0;
113✔
925
  int32_t curRow = 0;
113✔
926
  int32_t lastRow = 0;
113✔
927

928
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
113✔
929
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
113!
930
  TQ_NULL_GO_TO_END(assigned);
113!
931

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

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

949
    if (buildNew) {
185✔
950
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, pSchemaWrapper, assigned, numOfRows,
113!
951
                                       curRow, &lastRow));
952
    }
953

954
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
185✔
955
    TQ_NULL_GO_TO_END(pBlock);
185!
956

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

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

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

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

990
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
116,709✔
991
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
116,709!
992
  TQ_NULL_GO_TO_END(assigned);
116,713!
993

994
  int32_t curRow = 0;
116,713✔
995
  int32_t lastRow = 0;
116,713✔
996
  SArray* pRows = pSubmitTbData->aRowP;
116,713✔
997
  int32_t numOfRows = taosArrayGetSize(pRows);
116,713✔
998
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
116,709✔
999
  TQ_NULL_GO_TO_END(pTSchema);
116,710!
1000
  tqDebug("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
116,710✔
1001

1002
  for (int32_t i = 0; i < numOfRows; i++) {
3,478,357✔
1003
    bool  buildNew = false;
3,354,012✔
1004
    SRow* pRow = taosArrayGetP(pRows, i);
3,354,012✔
1005
    TQ_NULL_GO_TO_END(pRow);
3,350,790!
1006

1007
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
18,178,126✔
1008
      SColVal colVal = {0};
14,846,036✔
1009
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
14,846,036!
1010
      PROCESS_VAL
14,828,554!
1011
    }
1012

1013
    if (buildNew) {
3,332,090✔
1014
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, pSchemaWrapper, assigned, numOfRows,
116,731!
1015
                                       curRow, &lastRow));
1016
    }
1017

1018
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
3,332,078✔
1019
    TQ_NULL_GO_TO_END(pBlock);
3,352,623!
1020

1021
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
3,352,623!
1022
            (int32_t)taosArrayGetSize(blocks));
1023

1024
    int32_t targetIdx = 0;
3,352,623✔
1025
    int32_t sourceIdx = 0;
3,352,623✔
1026
    int32_t colActual = blockDataGetNumOfCols(pBlock);
3,352,623✔
1027
    while (targetIdx < colActual) {
18,178,319✔
1028
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
14,816,696✔
1029
      SColVal          colVal = {0};
14,805,156✔
1030
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
14,805,156!
1031
      SET_DATA
14,755,373!
1032
    }
1033

1034
    curRow++;
3,361,623✔
1035
  }
1036
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
124,345✔
1037
  pLastBlock->info.rows = curRow - lastRow;
116,691✔
1038

1039
  tqDebug("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows, (int)taosArrayGetSize(blocks));
116,691!
1040
END:
×
1041
  if (code != TSDB_CODE_SUCCESS) {
116,731!
1042
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1043
  }
1044
  taosMemoryFreeClear(pTSchema);
116,731!
1045
  taosMemoryFree(assigned);
116,733!
1046
  return code;
116,734✔
1047
}
1048

1049
int32_t tqRetrieveTaosxBlock(STqReader* pReader, SArray* blocks, SArray* schemas, SSubmitTbData** pSubmitTbDataRet, int64_t *createTime) {
117,272✔
1050
  tqDebug("tq reader retrieve data block msg pointer:%p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
117,272!
1051
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
117,275✔
1052
  if (pSubmitTbData == NULL) {
117,274!
1053
    return terrno;
×
1054
  }
1055
  pReader->nextBlk++;
117,274✔
1056

1057
  if (pSubmitTbDataRet) {
117,274!
1058
    *pSubmitTbDataRet = pSubmitTbData;
117,274✔
1059
  }
1060

1061
  int32_t sversion = pSubmitTbData->sver;
117,274✔
1062
  int64_t uid = pSubmitTbData->uid;
117,274✔
1063
  pReader->lastBlkUid = uid;
117,274✔
1064

1065
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
117,274✔
1066
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, createTime);
117,274✔
1067
  if (pReader->pSchemaWrapper == NULL) {
117,261✔
1068
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
441✔
1069
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1070
    pReader->cachedSchemaSuid = 0;
426✔
1071
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
426✔
1072
  }
1073

1074
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
116,820✔
1075
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
113✔
1076
  } else {
1077
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
116,707✔
1078
  }
1079
}
1080

1081
void tqReaderSetColIdList(STqReader* pReader, SArray* pColIdList) {
8,458✔
1082
  if (pReader == NULL){
8,458!
1083
    return;
×
1084
  }
1085
  pReader->pColIdList = pColIdList;
8,458✔
1086
}
1087

1088
void tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
8,538✔
1089
  if (pReader == NULL || tbUidList == NULL) {
8,538!
1090
    return;
×
1091
  }
1092
  if (pReader->tbIdHash) {
8,538✔
1093
    taosHashClear(pReader->tbIdHash);
14✔
1094
  } else {
1095
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
8,524✔
1096
    if (pReader->tbIdHash == NULL) {
8,525!
1097
      tqError("s-task:%s failed to init hash table", id);
×
1098
      return;
×
1099
    }
1100
  }
1101

1102
  for (int i = 0; i < taosArrayGetSize(tbUidList); i++) {
363,700✔
1103
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
355,155✔
1104
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
355,146!
1105
      tqError("s-task:%s failed to add table uid:%" PRId64 " to hash", id, *pKey);
×
1106
      continue;
×
1107
    }
1108
  }
1109

1110
  tqDebug("s-task:%s %d tables are set to be queried target table", id, (int32_t)taosArrayGetSize(tbUidList));
8,538✔
1111
}
1112

1113
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
20,586✔
1114
  if (pReader == NULL || pTableUidList == NULL) {
20,586!
1115
    return;
×
1116
  }
1117
  if (pReader->tbIdHash == NULL) {
20,589!
1118
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
×
1119
    if (pReader->tbIdHash == NULL) {
×
1120
      tqError("failed to init hash table");
×
1121
      return;
×
1122
    }
1123
  }
1124

1125
  int32_t numOfTables = taosArrayGetSize(pTableUidList);
20,589✔
1126
  for (int i = 0; i < numOfTables; i++) {
21,522✔
1127
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
934✔
1128
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
934!
1129
      tqError("failed to add table uid:%" PRId64 " to hash", *pKey);
×
1130
      continue;
×
1131
    }
1132
  }
1133
}
1134

1135
bool tqReaderIsQueriedTable(STqReader* pReader, uint64_t uid) {
2,416✔
1136
  if (pReader == NULL) {
2,416!
1137
    return false;
×
1138
  }
1139
  return taosHashGet(pReader->tbIdHash, &uid, sizeof(uint64_t)) != NULL;
2,416✔
1140
}
1141

1142
bool tqCurrentBlockConsumed(const STqReader* pReader) {
850,098✔
1143
  if (pReader == NULL) {
850,098!
1144
    return false;
×
1145
  }
1146
  return pReader->msg.msgStr == NULL;
850,098✔
1147
}
1148

1149
void tqReaderRemoveTbUidList(STqReader* pReader, const SArray* tbUidList) {
774✔
1150
  if (pReader == NULL || tbUidList == NULL) {
774!
1151
    return;
×
1152
  }
1153
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
778✔
1154
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
4✔
1155
    if (pKey && taosHashRemove(pReader->tbIdHash, pKey, sizeof(int64_t)) != 0) {
4!
1156
      tqError("failed to remove table uid:%" PRId64 " from hash", *pKey);
1!
1157
    }
1158
  }
1159
}
1160

1161
int32_t tqUpdateTbUidList(STQ* pTq, const SArray* tbUidList, bool isAdd) {
107,408✔
1162
  if (pTq == NULL || tbUidList == NULL) {
107,408!
1163
    return TSDB_CODE_INVALID_PARA;
×
1164
  }
1165
  void*   pIter = NULL;
107,411✔
1166
  int32_t vgId = TD_VID(pTq->pVnode);
107,411✔
1167

1168
  // update the table list for each consumer handle
1169
  taosWLockLatch(&pTq->lock);
107,411✔
1170
  while (1) {
4,106✔
1171
    pIter = taosHashIterate(pTq->pHandle, pIter);
111,519✔
1172
    if (pIter == NULL) {
111,519✔
1173
      break;
107,413✔
1174
    }
1175

1176
    STqHandle* pTqHandle = (STqHandle*)pIter;
4,106✔
1177
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
4,106✔
1178
      int32_t code = qUpdateTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList, isAdd);
995✔
1179
      if (code != 0) {
995!
1180
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1181
        continue;
×
1182
      }
1183
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
3,111✔
1184
      if (!isAdd) {
3,097✔
1185
        int32_t sz = taosArrayGetSize(tbUidList);
1,004✔
1186
        for (int32_t i = 0; i < sz; i++) {
1,004!
1187
          int64_t* tbUid = (int64_t*)taosArrayGet(tbUidList, i);
×
1188
          if (tbUid &&
×
1189
              taosHashPut(pTqHandle->execHandle.execDb.pFilterOutTbUid, tbUid, sizeof(int64_t), NULL, 0) != 0) {
×
1190
            tqError("failed to add table uid:%" PRId64 " to hash", *tbUid);
×
1191
            continue;
×
1192
          }
1193
        }
1194
      }
1195
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
14!
1196
      if (isAdd) {
14!
1197
        SArray* list = NULL;
14✔
1198
        int     ret = qGetTableList(pTqHandle->execHandle.execTb.suid, pTq->pVnode, pTqHandle->execHandle.execTb.node,
14✔
1199
                                    &list, pTqHandle->execHandle.task);
1200
        if (ret != TDB_CODE_SUCCESS) {
14!
1201
          tqError("qGetTableList in tqUpdateTbUidList error:%d handle %s consumer:0x%" PRIx64, ret, pTqHandle->subKey,
×
1202
                  pTqHandle->consumerId);
1203
          taosArrayDestroy(list);
×
1204
          taosHashCancelIterate(pTq->pHandle, pIter);
×
1205
          taosWUnLockLatch(&pTq->lock);
×
1206

1207
          return ret;
×
1208
        }
1209
        tqReaderSetTbUidList(pTqHandle->execHandle.pTqReader, list, NULL);
14✔
1210
        taosArrayDestroy(list);
14✔
1211
      } else {
1212
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
×
1213
      }
1214
    }
1215
  }
1216
  taosWUnLockLatch(&pTq->lock);
107,413✔
1217

1218
  // update the table list handle for each stream scanner/wal reader
1219
  streamMetaWLock(pTq->pStreamMeta);
107,411✔
1220
  while (1) {
41,839✔
1221
    pIter = taosHashIterate(pTq->pStreamMeta->pTasksMap, pIter);
149,251✔
1222
    if (pIter == NULL) {
149,242✔
1223
      break;
107,408✔
1224
    }
1225

1226
    int64_t      refId = *(int64_t*)pIter;
41,834✔
1227
    SStreamTask* pTask = taosAcquireRef(streamTaskRefPool, refId);
41,834✔
1228
    if (pTask != NULL) {
41,845!
1229
      int32_t taskId = pTask->id.taskId;
41,845✔
1230

1231
      if ((pTask->info.taskLevel == TASK_LEVEL__SOURCE) && (pTask->exec.pExecutor != NULL)) {
41,845✔
1232
        int32_t code = qUpdateTableListForStreamScanner(pTask->exec.pExecutor, tbUidList, isAdd);
20,414✔
1233
        if (code != 0) {
20,407✔
1234
          tqError("vgId:%d, s-task:0x%x update qualified table error for stream task", vgId, taskId);
56!
1235
        }
1236
      }
1237
      int32_t ret = taosReleaseRef(streamTaskRefPool, refId);
41,838✔
1238
      if (ret) {
41,839!
1239
        tqError("vgId:%d release task refId failed, refId:%" PRId64, vgId, refId);
×
1240
      }
1241
    }
1242
  }
1243

1244
  streamMetaWUnLock(pTq->pStreamMeta);
107,408✔
1245
  return 0;
107,412✔
1246
}
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