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

taosdata / TDengine / #4857

17 Nov 2025 09:53AM UTC coverage: 64.135% (-0.2%) from 64.286%
#4857

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

218 of 311 new or added lines in 32 files covered. (70.1%)

5044 existing lines in 121 files now uncovered.

151302 of 235910 relevant lines covered (64.14%)

116627960.99 hits per line

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

61.52
/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
static int32_t tqCollectPhysicalTables(STqReader* pReader, const char* idstr);
20

21
bool isValValidForTable(STqHandle* pHandle, SWalCont* pHead) {
12,582✔
22
  if (pHandle == NULL || pHead == NULL) {
12,582✔
23
    return false;
×
24
  }
25
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
12,604✔
26
    return true;
12,141✔
27
  }
28

29
  STqExecHandle* pExec = &pHandle->execHandle;
441✔
30
  STqReader* pReader = pExec->pTqReader;
441✔
31

32
  int16_t msgType = pHead->msgType;
441✔
33
  char*   body = pHead->body;
441✔
34
  int32_t bodyLen = pHead->bodyLen;
441✔
35

36
  int64_t  tbSuid = pHandle->execHandle.execTb.suid;
441✔
37
  int64_t  realTbSuid = 0;
441✔
38
  SDecoder dcoder = {0};
441✔
39
  void*    data = POINTER_SHIFT(body, sizeof(SMsgHead));
441✔
40
  int32_t  len = bodyLen - sizeof(SMsgHead);
441✔
41
  tDecoderInit(&dcoder, data, len);
441✔
42

43
  if (msgType == TDMT_VND_CREATE_STB || msgType == TDMT_VND_ALTER_STB) {
504✔
44
    SVCreateStbReq req = {0};
63✔
45
    if (tDecodeSVCreateStbReq(&dcoder, &req) < 0) {
63✔
46
      goto end;
×
47
    }
48
    realTbSuid = req.suid;
63✔
49
  } else if (msgType == TDMT_VND_DROP_STB) {
378✔
50
    SVDropStbReq req = {0};
×
51
    if (tDecodeSVDropStbReq(&dcoder, &req) < 0) {
×
52
      goto end;
×
53
    }
54
    realTbSuid = req.suid;
×
55
  } else if (msgType == TDMT_VND_CREATE_TABLE) {
378✔
56
    SVCreateTbBatchReq req = {0};
189✔
57
    if (tDecodeSVCreateTbBatchReq(&dcoder, &req) < 0) {
189✔
58
      goto end;
×
59
    }
60

61
    int32_t        needRebuild = 0;
189✔
62
    SVCreateTbReq* pCreateReq = NULL;
189✔
63
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
378✔
64
      pCreateReq = req.pReqs + iReq;
189✔
65
      if (pCreateReq->type == TSDB_CHILD_TABLE && pCreateReq->ctb.suid == tbSuid &&
315✔
66
          taosHashGet(pReader->tbIdHash, &pCreateReq->uid, sizeof(int64_t)) != NULL) {  
126✔
67
        needRebuild++;
63✔
68
      }
69
    }
70
    if (needRebuild == 0) {
189✔
71
      // do nothing
72
    } else if (needRebuild == req.nReqs) {
63✔
73
      realTbSuid = tbSuid;
63✔
74
    } else {
75
      realTbSuid = tbSuid;
×
76
      SVCreateTbBatchReq reqNew = {0};
×
77
      reqNew.pArray = taosArrayInit(req.nReqs, sizeof(struct SVCreateTbReq));
×
78
      if (reqNew.pArray == NULL) {
×
79
        tDeleteSVCreateTbBatchReq(&req);
×
80
        goto end;
×
81
      }
82
      for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
83
        pCreateReq = req.pReqs + iReq;
×
84
        if (pCreateReq->type == TSDB_CHILD_TABLE && pCreateReq->ctb.suid == tbSuid &&
×
85
            taosHashGet(pReader->tbIdHash, &pCreateReq->uid, sizeof(int64_t)) != NULL) {
×
86
          reqNew.nReqs++;
×
87
          if (taosArrayPush(reqNew.pArray, pCreateReq) == NULL) {
×
88
            taosArrayDestroy(reqNew.pArray);
×
89
            tDeleteSVCreateTbBatchReq(&req);
×
90
            goto end;
×
91
          }
92
        }
93
      }
94

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

120
    tDeleteSVCreateTbBatchReq(&req);
189✔
121
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
189✔
122
    SVAlterTbReq req = {0};
189✔
123

124
    if (tDecodeSVAlterTbReq(&dcoder, &req) < 0) {
189✔
125
      goto end;
×
126
    }
127

128
    SMetaReader mr = {0};
189✔
129
    metaReaderDoInit(&mr, pHandle->execHandle.pTqReader->pVnodeMeta, META_READER_LOCK);
189✔
130

131
    if (metaGetTableEntryByName(&mr, req.tbName) < 0) {
189✔
132
      metaReaderClear(&mr);
×
133
      goto end;
×
134
    }
135
    if (taosHashGet(pReader->tbIdHash, &mr.me.uid, sizeof(int64_t)) != NULL) {
189✔
136
      realTbSuid = mr.me.ctbEntry.suid;
126✔
137
    }
138
    metaReaderClear(&mr);
189✔
139
  } else if (msgType == TDMT_VND_DROP_TABLE) {
×
140
    SVDropTbBatchReq req = {0};
×
141

142
    if (tDecodeSVDropTbBatchReq(&dcoder, &req) < 0) {
×
143
      goto end;
×
144
    }
145

146
    int32_t      needRebuild = 0;
×
147
    SVDropTbReq* pDropReq = NULL;
×
148
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
149
      pDropReq = req.pReqs + iReq;
×
150

151
      if (pDropReq->suid == tbSuid &&
×
152
          taosHashGet(pReader->tbIdHash, &pDropReq->uid, sizeof(int64_t)) != NULL) {
×
153
        needRebuild++;
×
154
      }
155
    }
156
    if (needRebuild == 0) {
×
157
      // do nothing
158
    } else if (needRebuild == req.nReqs) {
×
159
      realTbSuid = tbSuid;
×
160
    } else {
161
      realTbSuid = tbSuid;
×
162
      SVDropTbBatchReq reqNew = {0};
×
163
      reqNew.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbReq));
×
164
      if (reqNew.pArray == NULL) {
×
165
        goto end;
×
166
      }
167
      for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
168
        pDropReq = req.pReqs + iReq;
×
169
        if (pDropReq->suid == tbSuid &&
×
170
            taosHashGet(pReader->tbIdHash, &pDropReq->uid, sizeof(int64_t)) != NULL) {
×
171
          reqNew.nReqs++;
×
172
          if (taosArrayPush(reqNew.pArray, pDropReq) == NULL) {
×
173
            taosArrayDestroy(reqNew.pArray);
×
174
            goto end;
×
175
          }
176
        }
177
      }
178

179
      int     tlen = 0;
×
180
      int32_t ret = 0;
×
181
      tEncodeSize(tEncodeSVDropTbBatchReq, &reqNew, tlen, ret);
×
182
      void* buf = taosMemoryMalloc(tlen);
×
183
      if (NULL == buf) {
×
184
        taosArrayDestroy(reqNew.pArray);
×
185
        goto end;
×
186
      }
187
      SEncoder coderNew = {0};
×
188
      tEncoderInit(&coderNew, buf, tlen - sizeof(SMsgHead));
×
189
      ret = tEncodeSVDropTbBatchReq(&coderNew, &reqNew);
×
190
      tEncoderClear(&coderNew);
×
191
      if (ret != 0) {
×
192
        taosMemoryFree(buf);
×
193
        taosArrayDestroy(reqNew.pArray);
×
194
        goto end;
×
195
      }
196
      (void)memcpy(pHead->body + sizeof(SMsgHead), buf, tlen);
×
197
      pHead->bodyLen = tlen + sizeof(SMsgHead);
×
198
      taosMemoryFree(buf);
×
199
      taosArrayDestroy(reqNew.pArray);
×
200
    }
201
  } else if (msgType == TDMT_VND_DELETE) {
×
202
    SDeleteRes req = {0};
×
203
    if (tDecodeDeleteRes(&dcoder, &req) < 0) {
×
204
      goto end;
×
205
    }
206
    realTbSuid = req.suid;
×
207
  }
208

209
end:
441✔
210
  tDecoderClear(&dcoder);
441✔
211
  bool tmp = tbSuid == realTbSuid;
441✔
212
  tqDebug("%s suid:%" PRId64 " realSuid:%" PRId64 " return:%d", __FUNCTION__, tbSuid, realTbSuid, tmp);
441✔
213
  return tmp;
441✔
214
}
215

216
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
16,697,453✔
217
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
16,697,453✔
218
    return -1;
×
219
  }
220
  int32_t code = -1;
16,766,329✔
221
  int32_t vgId = TD_VID(pTq->pVnode);
16,766,329✔
222
  int64_t id = pHandle->pWalReader->readerId;
16,773,781✔
223

224
  int64_t offset = *fetchOffset;
16,769,737✔
225
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
16,781,985✔
226
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
16,776,283✔
227
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
16,778,572✔
228

229
  tqDebug("vgId:%d, start to fetch wal, index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64 ", applied:%" PRId64
16,779,006✔
230
          ", 0x%" PRIx64,
231
          vgId, offset, lastVer, committedVer, appliedVer, id);
232

233
  while (offset <= appliedVer) {
17,296,962✔
234
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
16,227,824✔
235
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", (epoch %d) vgId:%d offset %" PRId64
×
236
              ", no more log to return, QID:0x%" PRIx64 " 0x%" PRIx64,
237
              pHandle->consumerId, pHandle->epoch, vgId, offset, reqId, id);
238
      goto END;
×
239
    }
240

241
    tqDebug("vgId:%d, consumer:0x%" PRIx64 " taosx get msg ver %" PRId64 ", type:%s, QID:0x%" PRIx64 " 0x%" PRIx64,
16,227,891✔
242
            vgId, pHandle->consumerId, offset, TMSG_INFO(pHandle->pWalReader->pHead->head.msgType), reqId, id);
243

244
    if (pHandle->pWalReader->pHead->head.msgType == TDMT_VND_SUBMIT) {
16,228,043✔
245
      code = walFetchBody(pHandle->pWalReader);
15,702,602✔
246
      goto END;
15,702,548✔
247
    } else {
248
      if (pHandle->fetchMeta != WITH_DATA) {
525,615✔
249
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
17,094✔
250
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
17,094✔
251
          code = walFetchBody(pHandle->pWalReader);
12,604✔
252
          if (code < 0) {
12,532✔
253
            goto END;
×
254
          }
255

256
          pHead = &(pHandle->pWalReader->pHead->head);
12,532✔
257
          if (isValValidForTable(pHandle, pHead)) {
12,604✔
258
            code = 0;
12,371✔
259
            goto END;
12,371✔
260
          } else {
261
            offset++;
189✔
262
            code = -1;
189✔
263
            continue;
189✔
264
          }
265
        }
266
      }
267
      code = walSkipFetchBody(pHandle->pWalReader);
513,011✔
268
      if (code < 0) {
513,069✔
269
        goto END;
×
270
      }
271
      offset++;
513,069✔
272
    }
273
    code = -1;
513,069✔
274
  }
275

276
END:
1,069,138✔
277
  *fetchOffset = offset;
16,784,057✔
278
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
16,783,448✔
279
          ", applied:%" PRId64 ", 0x%" PRIx64,
280
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
281
  return code;
16,783,492✔
282
}
283

284
bool tqGetTablePrimaryKey(STqReader* pReader) {
1,319,482✔
285
  if (pReader == NULL) {
1,319,482✔
286
    return false;
×
287
  }
288
  return pReader->hasPrimaryKey;
1,319,482✔
289
}
290

291
void tqSetTablePrimaryKey(STqReader* pReader, int64_t uid) {
11,300✔
292
  tqDebug("%s:%p uid:%" PRId64, __FUNCTION__, pReader, uid);
11,300✔
293

294
  if (pReader == NULL) {
11,300✔
295
    return;
×
296
  }
297
  bool            ret = false;
11,300✔
298
  SSchemaWrapper* schema = metaGetTableSchema(pReader->pVnodeMeta, uid, -1, 1, NULL, 0);
11,300✔
299
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
11,271✔
300
    ret = true;
×
301
  }
302
  tDeleteSchemaWrapper(schema);
303
  pReader->hasPrimaryKey = ret;
11,300✔
304
}
305

306
STqReader* tqReaderOpen(SVnode* pVnode) {
137,092✔
307
  tqDebug("%s:%p", __FUNCTION__, pVnode);
137,092✔
308
  if (pVnode == NULL) {
138,331✔
309
    return NULL;
×
310
  }
311
  STqReader* pReader = taosMemoryCalloc(1, sizeof(STqReader));
138,331✔
312
  if (pReader == NULL) {
138,331✔
313
    return NULL;
×
314
  }
315

316
  pReader->pWalReader = walOpenReader(pVnode->pWal, 0);
138,331✔
317
  if (pReader->pWalReader == NULL) {
138,331✔
318
    taosMemoryFree(pReader);
×
319
    return NULL;
×
320
  }
321

322
  pReader->pVnodeMeta = pVnode->pMeta;
138,331✔
323
  pReader->pColIdList = NULL;
138,331✔
324
  pReader->cachedSchemaVer = 0;
138,331✔
325
  pReader->cachedSchemaSuid = 0;
138,331✔
326
  pReader->pSchemaWrapper = NULL;
138,331✔
327
  pReader->tbIdHash = NULL;
138,331✔
328
  pReader->pResBlock = NULL;
138,331✔
329

330
  int32_t code = createDataBlock(&pReader->pResBlock);
138,331✔
331
  if (code) {
138,331✔
332
    terrno = code;
×
333
  }
334

335
  return pReader;
138,331✔
336
}
337

338
void tqReaderClose(STqReader* pReader) {
138,578✔
339
  tqDebug("%s:%p", __FUNCTION__, pReader);
138,578✔
340
  if (pReader == NULL) return;
138,739✔
341

342
  // close wal reader
343
  if (pReader->pWalReader) {
138,331✔
344
    walCloseReader(pReader->pWalReader);
138,331✔
345
  }
346

347
  if (pReader->pSchemaWrapper) {
138,331✔
348
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
77,994✔
349
  }
350

351
  taosMemoryFree(pReader->extSchema);
138,331✔
352
  if (pReader->pColIdList) {
138,331✔
353
    taosArrayDestroy(pReader->pColIdList);
115,412✔
354
  }
355

356
  // free hash
357
  blockDataDestroy(pReader->pResBlock);
138,331✔
358
  taosHashCleanup(pReader->tbIdHash);
138,331✔
359
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
138,309✔
360

361
  taosHashCleanup(pReader->vtSourceScanInfo.pVirtualTables);
138,331✔
362
  taosHashCleanup(pReader->vtSourceScanInfo.pPhysicalTables);
138,331✔
363
  taosLRUCacheCleanup(pReader->vtSourceScanInfo.pPhyTblSchemaCache);
138,331✔
364
  taosMemoryFree(pReader);
138,331✔
365
}
366

367
int32_t tqReaderSeek(STqReader* pReader, int64_t ver, const char* id) {
254,640✔
368
  if (pReader == NULL) {
254,640✔
369
    return TSDB_CODE_INVALID_PARA;
×
370
  }
371
  if (walReaderSeekVer(pReader->pWalReader, ver) < 0) {
254,640✔
372
    return terrno;
13,091✔
373
  }
374
  tqDebug("wal reader seek to ver:%" PRId64 " %s", ver, id);
241,497✔
375
  return 0;
241,603✔
376
}
377

378
bool tqNextBlockInWal(STqReader* pReader, const char* id, int sourceExcluded) {
39,130,245✔
379
  if (pReader == NULL) {
39,130,245✔
380
    return false;
×
381
  }
382
  SWalReader* pWalReader = pReader->pWalReader;
39,130,245✔
383

384
  int64_t st = taosGetTimestampMs();
39,130,361✔
385
  while (1) {
41,272,335✔
386
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
80,402,696✔
387
    while (pReader->nextBlk < numOfBlocks) {
85,933,680✔
388
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
41,272,527✔
389
              pReader->msg.ver);
390

391
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
41,272,527✔
392
      if (pSubmitTbData == NULL) {
41,266,850✔
393
        tqError("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
×
394
                pReader->msg.ver);
395
        return false;
594✔
396
      }
397
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
41,266,850✔
398
        pReader->nextBlk += 1;
2,310✔
399
        continue;
2,332✔
400
      }
401
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
41,260,898✔
402
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
35,735,268✔
403
        SSDataBlock* pRes = NULL;
35,735,268✔
404
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
35,733,549✔
405
        if (code == TSDB_CODE_SUCCESS) {
35,733,109✔
406
          return true;
35,733,109✔
407
        }
408
      } else {
409
        pReader->nextBlk += 1;
5,532,789✔
410
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
5,532,789✔
411
      }
412
    }
413

414
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
44,663,749✔
415
    pReader->msg.msgStr = NULL;
44,664,249✔
416

417
    int64_t elapsed = taosGetTimestampMs() - st;
44,664,673✔
418
    if (elapsed > 1000 || elapsed < 0) {
44,664,673✔
419
      return false;
6✔
420
    }
421

422
    // try next message in wal file
423
    if (walNextValidMsg(pWalReader, false) < 0) {
44,664,667✔
424
      return false;
3,389,976✔
425
    }
426

427
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
41,270,718✔
428
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
41,272,739✔
429
    int64_t ver = pWalReader->pHead->head.version;
41,271,899✔
430
    SDecoder decoder = {0};
41,272,277✔
431
    if (tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL, &decoder) != 0) {
41,270,266✔
432
      tDecoderClear(&decoder);
×
433
      return false;
×
434
    }
435
    tDecoderClear(&decoder);
41,268,035✔
436
    pReader->nextBlk = 0;
41,272,361✔
437
  }
438
}
439

440
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList, SDecoder* decoder) {
56,971,657✔
441
  if (pReader == NULL) {
56,971,657✔
442
    return TSDB_CODE_INVALID_PARA;
×
443
  }
444
  pReader->msg.msgStr = msgStr;
56,971,657✔
445
  pReader->msg.msgLen = msgLen;
56,973,821✔
446
  pReader->msg.ver = ver;
56,973,964✔
447

448
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
56,974,617✔
449

450
  tDecoderInit(decoder, pReader->msg.msgStr, pReader->msg.msgLen);
56,974,617✔
451
  int32_t code = tDecodeSubmitReq(decoder, &pReader->submit, rawList);
56,973,324✔
452

453
  if (code != 0) {
56,973,356✔
454
    tqError("DecodeSSubmitReq2 error, msgLen:%d, ver:%" PRId64, msgLen, ver);
×
455
  }
456

457
  return code;
56,969,119✔
458
}
459

460
void tqReaderClearSubmitMsg(STqReader* pReader) {
31,339,569✔
461
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
31,339,569✔
462
  pReader->nextBlk = 0;
31,322,674✔
463
  pReader->msg.msgStr = NULL;
31,333,622✔
464
}
31,329,557✔
465

466
SWalReader* tqGetWalReader(STqReader* pReader) {
43,126,679✔
467
  if (pReader == NULL) {
43,126,679✔
468
    return NULL;
×
469
  }
470
  return pReader->pWalReader;
43,126,679✔
471
}
472

473
SSDataBlock* tqGetResultBlock(STqReader* pReader) {
39,130,302✔
474
  if (pReader == NULL) {
39,130,302✔
475
    return NULL;
×
476
  }
477
  return pReader->pResBlock;
39,130,302✔
478
}
479

480
int64_t tqGetResultBlockTime(STqReader* pReader) {
39,127,430✔
481
  if (pReader == NULL) {
39,127,430✔
482
    return 0;
×
483
  }
484
  return pReader->lastTs;
39,127,430✔
485
}
486

487
bool tqNextBlockImpl(STqReader* pReader, const char* idstr) {
6,344,232✔
488
  int32_t code = false;
6,344,232✔
489
  int32_t lino = 0;
6,344,232✔
490
  int64_t uid = 0;
6,344,232✔
491
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
6,344,232✔
492
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
6,344,232✔
493
  TSDB_CHECK_NULL(pReader->tbIdHash, code, lino, END, true);
6,344,644✔
494

495
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
6,344,498✔
496
  while (pReader->nextBlk < blockSz) {
6,675,148✔
497
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
3,337,907✔
498
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
3,338,095✔
499
    uid = pSubmitTbData->uid;
3,338,095✔
500
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
3,338,095✔
501
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
3,338,101✔
502

503
    tqTrace("iterator data block in hash continue, progress:%d/%d, total queried tables:%d, uid:%" PRId64,
330,168✔
504
            pReader->nextBlk, blockSz, taosHashGetSize(pReader->tbIdHash), uid);
505
    pReader->nextBlk++;
330,168✔
506
  }
507

508
  tqReaderClearSubmitMsg(pReader);
3,336,811✔
509
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
3,337,123✔
510

511
END:
3,337,123✔
512
  tqTrace("%s:%d return:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
6,345,056✔
513
  return code;
6,344,544✔
514
}
515

516
bool tqNextDataBlockFilterOut(STqReader* pReader, SHashObj* filterOutUids) {
24,697,063✔
517
  int32_t code = false;
24,697,063✔
518
  int32_t lino = 0;
24,697,063✔
519
  int64_t uid = 0;
24,697,063✔
520

521
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
24,697,063✔
522
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
24,697,063✔
523
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
24,707,660✔
524

525
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
24,707,660✔
526
  while (pReader->nextBlk < blockSz) {
24,722,755✔
527
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
12,365,445✔
528
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
12,367,336✔
529
    uid = pSubmitTbData->uid;
12,367,336✔
530
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
12,367,405✔
531
    TSDB_CHECK_NULL(ret, code, lino, END, true);
12,367,169✔
532
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64, pReader->nextBlk, blockSz, uid);
×
533
    pReader->nextBlk++;
×
534
  }
535
  tqReaderClearSubmitMsg(pReader);
12,358,941✔
536
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
12,342,206✔
537

538
END:
12,342,206✔
539
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
24,709,375✔
540
  return code;
24,652,647✔
541
}
542

543
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask,
15,281,930✔
544
                    SExtSchema* extSrc) {
545
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
15,281,930✔
546
    return TSDB_CODE_INVALID_PARA;
×
547
  }
548
  int32_t code = 0;
15,298,814✔
549

550
  int32_t cnt = 0;
15,298,814✔
551
  for (int32_t i = 0; i < pSrc->nCols; i++) {
72,307,047✔
552
    cnt += mask[i];
56,994,411✔
553
  }
554

555
  pDst->nCols = cnt;
15,305,518✔
556
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
15,324,413✔
557
  if (pDst->pSchema == NULL) {
15,302,958✔
558
    return TAOS_GET_TERRNO(terrno);
×
559
  }
560

561
  int32_t j = 0;
15,290,102✔
562
  for (int32_t i = 0; i < pSrc->nCols; i++) {
72,337,792✔
563
    if (mask[i]) {
56,993,047✔
564
      pDst->pSchema[j++] = pSrc->pSchema[i];
57,044,182✔
565
      SColumnInfoData colInfo =
57,042,418✔
566
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
57,038,946✔
567
      if (extSrc != NULL) {
57,013,780✔
568
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
4,796✔
569
      }
570
      code = blockDataAppendColInfo(pBlock, &colInfo);
57,013,780✔
571
      if (code != 0) {
57,034,452✔
572
        return code;
×
573
      }
574
    }
575
  }
576
  return 0;
15,330,639✔
577
}
578

579
static int32_t buildResSDataBlock(STqReader* pReader, SSchemaWrapper* pSchema, const SArray* pColIdList) {
65,573✔
580
  if (pReader == NULL || pSchema == NULL || pColIdList == NULL) {
65,573✔
581
    return TSDB_CODE_INVALID_PARA;
×
582
  }
583
  SSDataBlock* pBlock = pReader->pResBlock;
65,679✔
584
  if (blockDataGetNumOfCols(pBlock) > 0) {
65,489✔
585
    blockDataDestroy(pBlock);
98✔
586
    int32_t code = createDataBlock(&pReader->pResBlock);
98✔
587
    if (code) {
98✔
588
      return code;
×
589
    }
590
    pBlock = pReader->pResBlock;
98✔
591

592
    pBlock->info.id.uid = pReader->cachedSchemaUid;
98✔
593
    pBlock->info.version = pReader->msg.ver;
98✔
594
  }
595

596
  int32_t numOfCols = taosArrayGetSize(pColIdList);
65,781✔
597

598
  if (numOfCols == 0) {  // all columns are required
65,677✔
599
    for (int32_t i = 0; i < pSchema->nCols; ++i) {
×
600
      SSchema*        pColSchema = &pSchema->pSchema[i];
×
601
      SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
×
602

603
      if (IS_DECIMAL_TYPE(pColSchema->type) && pReader->extSchema != NULL) {
×
604
        decimalFromTypeMod(pReader->extSchema[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
×
605
      }
606
      int32_t code = blockDataAppendColInfo(pBlock, &colInfo);
×
607
      if (code != TSDB_CODE_SUCCESS) {
×
608
        blockDataFreeRes(pBlock);
×
609
        return terrno;
×
610
      }
611
    }
612
  } else {
613
    if (numOfCols > pSchema->nCols) {
65,677✔
614
      numOfCols = pSchema->nCols;
200✔
615
    }
616

617
    int32_t i = 0;
65,679✔
618
    int32_t j = 0;
65,679✔
619
    while (i < pSchema->nCols && j < numOfCols) {
834,149✔
620
      SSchema* pColSchema = &pSchema->pSchema[i];
768,572✔
621
      col_id_t colIdSchema = pColSchema->colId;
768,366✔
622

623
      col_id_t* pColIdNeed = (col_id_t*)taosArrayGet(pColIdList, j);
768,388✔
624
      if (pColIdNeed == NULL) {
768,655✔
625
        break;
×
626
      }
627
      if (colIdSchema < *pColIdNeed) {
768,655✔
628
        i++;
43,943✔
629
      } else if (colIdSchema > *pColIdNeed) {
724,712✔
630
        j++;
×
631
      } else {
632
        SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
724,628✔
633
        if (IS_DECIMAL_TYPE(pColSchema->type) && pReader->extSchema != NULL) {
724,179✔
634
          decimalFromTypeMod(pReader->extSchema[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
56,008✔
635
        }
636
        int32_t code = blockDataAppendColInfo(pBlock, &colInfo);
724,179✔
637
        if (code != TSDB_CODE_SUCCESS) {
724,527✔
638
          return -1;
×
639
        }
640
        i++;
724,527✔
641
        j++;
724,527✔
642
      }
643
    }
644
  }
645

646
  return TSDB_CODE_SUCCESS;
65,781✔
647
}
648

649
static int32_t doSetBlobVal(SColumnInfoData* pColumnInfoData, int32_t idx, SColVal* pColVal, SBlobSet* pBlobRow2) {
×
650
  int32_t code = 0;
×
651
  if (pColumnInfoData == NULL || pColVal == NULL || pBlobRow2 == NULL) {
×
652
    return TSDB_CODE_INVALID_PARA;
×
653
  }
654
  // TODO(yhDeng)
655
  if (COL_VAL_IS_VALUE(pColVal)) {
×
656
    char* val = taosMemCalloc(1, pColVal->value.nData + sizeof(BlobDataLenT));
×
657
    if (val == NULL) {
×
658
      return terrno;
×
659
    }
660

661
    uint64_t seq = 0;
×
662
    int32_t  len = 0;
×
663
    if (pColVal->value.pData != NULL) {
×
664
      if (tGetU64(pColVal->value.pData, &seq) < 0){
×
665
        TAOS_CHECK_RETURN(TSDB_CODE_INVALID_PARA);
×
666
      }
667
      SBlobItem item = {0};
×
668
      code = tBlobSetGet(pBlobRow2, seq, &item);
×
669
      if (code != 0) {
×
670
        taosMemoryFree(val);
×
671
        terrno = code;
×
672
        uError("tq set blob val, idx:%d, get blob item failed, seq:%" PRIu64 ", code:%d", idx, seq, code);
×
673
        return code;
×
674
      }
675

676
      val = taosMemRealloc(val, item.len + sizeof(BlobDataLenT));
×
677
      (void)memcpy(blobDataVal(val), item.data, item.len);
×
678
      len = item.len;
×
679
    }
680

681
    blobDataSetLen(val, len);
×
682
    code = colDataSetVal(pColumnInfoData, idx, val, false);
×
683

684
    taosMemoryFree(val);
×
685
  } else {
686
    colDataSetNULL(pColumnInfoData, idx);
×
687
  }
688
  return code;
×
689
}
690
static int32_t doSetVal(SColumnInfoData* pColumnInfoData, int32_t rowIndex, SColVal* pColVal) {
2,147,483,647✔
691
  int32_t code = TSDB_CODE_SUCCESS;
2,147,483,647✔
692

693
  if (IS_VAR_DATA_TYPE(pColVal->value.type)) {
2,147,483,647✔
694
    if (COL_VAL_IS_VALUE(pColVal)) {
769,514,197✔
695
      char val[65535 + 2] = {0};
798,315,973✔
696
      if (pColVal->value.pData != NULL) {
797,886,522✔
697
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
797,807,913✔
698
      }
699
      varDataSetLen(val, pColVal->value.nData);
797,432,323✔
700
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
798,467,004✔
701
    } else {
702
      colDataSetNULL(pColumnInfoData, rowIndex);
×
703
    }
704
  } else {
705
    code = colDataSetVal(pColumnInfoData, rowIndex, VALUE_GET_DATUM(&pColVal->value, pColVal->value.type),
2,147,483,647✔
706
                         !COL_VAL_IS_VALUE(pColVal));
2,147,483,647✔
707
  }
708

709
  return code;
2,147,483,647✔
710
}
711

712
int32_t tqRetrieveDataBlock(STqReader* pReader, SSDataBlock** pRes, const char* id) {
35,723,053✔
713
  if (pReader == NULL || pRes == NULL) {
35,723,053✔
714
    return TSDB_CODE_INVALID_PARA;
×
715
  }
716
  tqDebug("tq reader retrieve data block %p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
35,737,167✔
717
  int32_t        code = 0;
35,744,801✔
718
  int32_t        line = 0;
35,744,801✔
719
  STSchema*      pTSchema = NULL;
35,744,801✔
720
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk++);
35,744,801✔
721
  TSDB_CHECK_NULL(pSubmitTbData, code, line, END, terrno);
35,741,133✔
722
  SSDataBlock* pBlock = pReader->pResBlock;
35,741,133✔
723
  *pRes = pBlock;
35,741,241✔
724

725
  blockDataCleanup(pBlock);
35,741,241✔
726

727
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
35,741,025✔
728
  int32_t sversion = pSubmitTbData->sver;
35,741,025✔
729
  int64_t suid = pSubmitTbData->suid;
35,741,133✔
730
  int64_t uid = pSubmitTbData->uid;
35,740,809✔
731
  pReader->lastTs = pSubmitTbData->ctimeMs;
35,740,917✔
732

733
  pBlock->info.id.uid = uid;
35,740,671✔
734
  pBlock->info.version = pReader->msg.ver;
35,740,269✔
735

736
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
35,736,883✔
737
      (pReader->cachedSchemaVer != sversion)) {
35,674,532✔
738
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
60,819✔
739
    taosMemoryFree(pReader->extSchema);
65,781✔
740
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
65,781✔
741
    if (pReader->pSchemaWrapper == NULL) {
65,781✔
742
      tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", uid:%" PRId64
×
743
             "version %d, possibly dropped table",
744
             vgId, suid, uid, pReader->cachedSchemaVer);
745
      pReader->cachedSchemaSuid = 0;
×
746
      return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
×
747
    }
748

749
    pReader->cachedSchemaUid = uid;
65,489✔
750
    pReader->cachedSchemaSuid = suid;
65,573✔
751
    pReader->cachedSchemaVer = sversion;
65,573✔
752

753
    if (pReader->cachedSchemaVer != pReader->pSchemaWrapper->version) {
65,573✔
754
      tqError("vgId:%d, schema version mismatch, suid:%" PRId64 ", uid:%" PRId64 ", version:%d, cached version:%d",
×
755
              vgId, suid, uid, sversion, pReader->pSchemaWrapper->version);
756
      return TSDB_CODE_TQ_INTERNAL_ERROR;
×
757
    }
758
    code = buildResSDataBlock(pReader, pReader->pSchemaWrapper, pReader->pColIdList);
65,489✔
759
    TSDB_CHECK_CODE(code, line, END);
65,781✔
760
    pBlock = pReader->pResBlock;
65,781✔
761
    *pRes = pBlock;
65,781✔
762
  }
763

764
  int32_t numOfRows = 0;
35,740,221✔
765
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
35,740,221✔
766
    SColData* pCol = taosArrayGet(pSubmitTbData->aCol, 0);
204✔
767
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
204✔
768
    numOfRows = pCol->nVal;
204✔
769
  } else {
770
    numOfRows = taosArrayGetSize(pSubmitTbData->aRowP);
35,735,715✔
771
  }
772

773
  code = blockDataEnsureCapacity(pBlock, numOfRows);
35,740,331✔
774
  TSDB_CHECK_CODE(code, line, END);
35,740,539✔
775
  pBlock->info.rows = numOfRows;
35,740,539✔
776
  int32_t colActual = blockDataGetNumOfCols(pBlock);
35,740,539✔
777

778
  // convert and scan one block
779
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
35,740,293✔
780
    SArray* pCols = pSubmitTbData->aCol;
204✔
781
    int32_t numOfCols = taosArrayGetSize(pCols);
204✔
782
    int32_t targetIdx = 0;
204✔
783
    int32_t sourceIdx = 0;
204✔
784
    while (targetIdx < colActual) {
918✔
785
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
714✔
786
      TSDB_CHECK_NULL(pColData, code, line, END, terrno);
714✔
787
      if (sourceIdx >= numOfCols) {
714✔
788
        tqError("lostdata tqRetrieveDataBlock sourceIdx:%d >= numOfCols:%d", sourceIdx, numOfCols);
204✔
789
        colDataSetNNULL(pColData, 0, numOfRows);
204✔
790
        targetIdx++;
204✔
791
        continue;
204✔
792
      }
793

794
      uint8_t isBlob = IS_STR_DATA_BLOB(pColData->info.type) ? 1 : 0;
510✔
795

796
      SColData* pCol = taosArrayGet(pCols, sourceIdx);
510✔
797
      TSDB_CHECK_NULL(pCol, code, line, END, terrno);
510✔
798
      SColVal colVal = {0};
510✔
799
      tqTrace("lostdata colActual:%d, sourceIdx:%d, targetIdx:%d, numOfCols:%d, source cid:%d, dst cid:%d", colActual,
510✔
800
              sourceIdx, targetIdx, numOfCols, pCol->cid, pColData->info.colId);
801
      if (pCol->cid < pColData->info.colId) {
510✔
802
        sourceIdx++;
204✔
803
      } else if (pCol->cid == pColData->info.colId) {
306✔
804
        for (int32_t i = 0; i < pCol->nVal; i++) {
612✔
805
          code = tColDataGetValue(pCol, i, &colVal);
408✔
806
          TSDB_CHECK_CODE(code, line, END);
408✔
807

808
          if (isBlob == 0) {
408✔
809
            code = doSetVal(pColData, i, &colVal);
408✔
810
          } else {
811
            code = doSetBlobVal(pColData, i, &colVal, pSubmitTbData->pBlobSet);
×
812
          }
813
          TSDB_CHECK_CODE(code, line, END);
408✔
814
        }
815
        sourceIdx++;
204✔
816
        targetIdx++;
204✔
817
      } else {
818
        colDataSetNNULL(pColData, 0, numOfRows);
102✔
819
        targetIdx++;
102✔
820
      }
821
    }
822
  } else {
823
    SArray*         pRows = pSubmitTbData->aRowP;
35,739,659✔
824
    SSchemaWrapper* pWrapper = pReader->pSchemaWrapper;
35,739,981✔
825
    pTSchema = tBuildTSchema(pWrapper->pSchema, pWrapper->nCols, pWrapper->version);
35,739,927✔
826
    TSDB_CHECK_NULL(pTSchema, code, line, END, terrno);
35,735,949✔
827

828
    for (int32_t i = 0; i < numOfRows; i++) {
1,677,929,318✔
829
      SRow* pRow = taosArrayGetP(pRows, i);
1,541,847,500✔
830
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
1,538,133,520✔
831
      int32_t sourceIdx = 0;
1,540,595,507✔
832
      for (int32_t j = 0; j < colActual; j++) {
2,147,483,647✔
833
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, j);
2,147,483,647✔
834
        TSDB_CHECK_NULL(pColData, code, line, END, terrno);
2,147,483,647✔
835

836
        uint8_t isBlob = IS_STR_DATA_BLOB(pColData->info.type) ? 1 : 0;
2,147,483,647✔
837
        while (1) {
148,886,100✔
838
          SColVal colVal = {0};
2,147,483,647✔
839
          code = tRowGet(pRow, pTSchema, sourceIdx, &colVal);
2,147,483,647✔
840
          TSDB_CHECK_CODE(code, line, END);
2,147,483,647✔
841

842
          if (colVal.cid < pColData->info.colId) {
2,147,483,647✔
843
            sourceIdx++;
148,886,100✔
844
            continue;
148,886,100✔
845
          } else if (colVal.cid == pColData->info.colId) {
2,147,483,647✔
846
            if (isBlob == 0) {
2,147,483,647✔
847
              code = doSetVal(pColData, i, &colVal);
2,147,483,647✔
848
            } else {
849
              code = doSetBlobVal(pColData, i, &colVal, pSubmitTbData->pBlobSet);
×
850
            }
851

852
            TSDB_CHECK_CODE(code, line, END);
2,147,483,647✔
853

854
            sourceIdx++;
2,147,483,647✔
855
            break;
2,147,483,647✔
856
          } else {
857
            colDataSetNULL(pColData, i);
×
858
            break;
×
859
          }
860
        }
861
      }
862
    }
863
  }
864

865
END:
136,081,818✔
866
  if (code != 0) {
112,216,704✔
867
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
868
  }
869
  taosMemoryFreeClear(pTSchema);
35,737,045✔
870
  return code;
35,738,118✔
871
}
872

873
#define PROCESS_VAL                                      \
874
  if (curRow == 0) {                                     \
875
    assigned[j] = !COL_VAL_IS_NONE(&colVal);             \
876
    buildNew = true;                                     \
877
  } else {                                               \
878
    bool currentRowAssigned = !COL_VAL_IS_NONE(&colVal); \
879
    if (currentRowAssigned != assigned[j]) {             \
880
      assigned[j] = currentRowAssigned;                  \
881
      buildNew = true;                                   \
882
    }                                                    \
883
  }
884

885
#define SET_DATA                                                                                    \
886
  if (colVal.cid < pColData->info.colId) {                                                          \
887
    sourceIdx++;                                                                                    \
888
  } else if (colVal.cid == pColData->info.colId) {                                                  \
889
    if (IS_STR_DATA_BLOB(pColData->info.type)) {                                                    \
890
      TQ_ERR_GO_TO_END(doSetBlobVal(pColData, curRow - lastRow, &colVal, pSubmitTbData->pBlobSet)); \
891
    } else {                                                                                        \
892
      TQ_ERR_GO_TO_END(doSetVal(pColData, curRow - lastRow, &colVal));                              \
893
    }                                                                                               \
894
    sourceIdx++;                                                                                    \
895
    targetIdx++;                                                                                    \
896
  } else {                                                                                          \
897
    colDataSetNULL(pColData, curRow - lastRow);                                                     \
898
    targetIdx++;                                                                                    \
899
  }
900

901
static int32_t processBuildNew(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas,
15,281,830✔
902
                               char* assigned, int32_t numOfRows, int32_t curRow, int32_t* lastRow) {
903
  int32_t         code = 0;
15,281,830✔
904
  SSchemaWrapper* pSW = NULL;
15,281,830✔
905
  SSDataBlock*    block = NULL;
15,292,870✔
906
  if (taosArrayGetSize(blocks) > 0) {
15,292,870✔
907
    SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
×
908
    TQ_NULL_GO_TO_END(pLastBlock);
×
909
    pLastBlock->info.rows = curRow - *lastRow;
×
910
    *lastRow = curRow;
×
911
  }
912

913
  block = taosMemoryCalloc(1, sizeof(SSDataBlock));
15,316,204✔
914
  TQ_NULL_GO_TO_END(block);
15,281,435✔
915

916
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
15,281,435✔
917
  TQ_NULL_GO_TO_END(pSW);
15,298,121✔
918

919
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pReader->pSchemaWrapper, assigned, pReader->extSchema));
15,298,121✔
920
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
15,330,581✔
921
          (int32_t)taosArrayGetSize(block->pDataBlock));
922

923
  block->info.id.uid = pSubmitTbData->uid;
15,330,581✔
924
  block->info.version = pReader->msg.ver;
15,322,642✔
925
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
15,320,654✔
926
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
15,315,471✔
927
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
15,323,302✔
928
  pSW = NULL;
15,323,302✔
929

930
  taosMemoryFreeClear(block);
15,323,302✔
931

932
END:
15,333,115✔
933
  if (code != 0) {
15,321,375✔
934
    tqError("processBuildNew failed, code:%d", code);
×
935
  }
936
  tDeleteSchemaWrapper(pSW);
15,321,375✔
937
  blockDataFreeRes(block);
15,289,543✔
938
  taosMemoryFree(block);
15,289,545✔
939
  return code;
15,304,785✔
940
}
941
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
9,894✔
942
  int32_t code = 0;
9,894✔
943
  int32_t curRow = 0;
9,894✔
944
  int32_t lastRow = 0;
9,894✔
945

946
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
9,932✔
947
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
9,932✔
948
  TQ_NULL_GO_TO_END(assigned);
9,894✔
949

950
  SArray*   pCols = pSubmitTbData->aCol;
9,894✔
951
  SColData* pCol = taosArrayGet(pCols, 0);
9,894✔
952
  TQ_NULL_GO_TO_END(pCol);
9,894✔
953
  int32_t numOfRows = pCol->nVal;
9,894✔
954
  int32_t numOfCols = taosArrayGetSize(pCols);
9,932✔
955
  tqTrace("vgId:%d, tqProcessColData start, col num: %d, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfCols,
9,894✔
956
          numOfRows);
957
  for (int32_t i = 0; i < numOfRows; i++) {
7,432,604✔
958
    bool buildNew = false;
7,345,988✔
959

960
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
28,446,816✔
961
      int32_t k = 0;
20,366,972✔
962
      for (; k < numOfCols; k++) {
40,582,496✔
963
        pCol = taosArrayGet(pCols, k);
37,077,604✔
964
        TQ_NULL_GO_TO_END(pCol);
36,200,070✔
965
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
36,200,070✔
966
          SColVal colVal = {0};
19,609,176✔
967
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
20,793,978✔
968
          PROCESS_VAL
21,314,274✔
969
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
21,323,318✔
970
          break;
21,240,896✔
971
        }
972
      }
973
      if (k >= numOfCols) {
21,100,828✔
974
        // this column is not in the current row, so we set it to NULL
975
        assigned[j] = 0;
×
976
        buildNew = true;
×
977
      }
978
    }
979

980
    if (buildNew) {
6,848,568✔
981
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
9,932✔
982
    }
983

984
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
6,848,568✔
985
    TQ_NULL_GO_TO_END(pBlock);
7,442,698✔
986

987
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
7,442,698✔
988
            (int32_t)taosArrayGetSize(blocks));
989

990
    int32_t targetIdx = 0;
7,442,698✔
991
    int32_t sourceIdx = 0;
7,442,698✔
992
    int32_t colActual = blockDataGetNumOfCols(pBlock);
7,442,698✔
993
    while (targetIdx < colActual && sourceIdx < numOfCols) {
28,603,494✔
994
      pCol = taosArrayGet(pCols, sourceIdx);
21,180,784✔
995
      TQ_NULL_GO_TO_END(pCol);
20,736,336✔
996
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
20,736,336✔
997
      TQ_NULL_GO_TO_END(pColData);
19,354,998✔
998
      SColVal colVal = {0};
19,354,998✔
999
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
20,368,382✔
1000
      SET_DATA
21,175,122✔
1001
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
21,883,784✔
1002
    }
1003

1004
    curRow++;
7,422,710✔
1005
  }
1006
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
86,616✔
1007
  pLastBlock->info.rows = curRow - lastRow;
9,932✔
1008
  tqTrace("vgId:%d, tqProcessColData end, col num: %d, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId,
9,932✔
1009
          numOfCols, numOfRows, (int)taosArrayGetSize(blocks));
1010
END:
4,216,722✔
1011
  if (code != TSDB_CODE_SUCCESS) {
9,932✔
1012
    tqError("vgId:%d, process col data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1013
  }
1014
  taosMemoryFree(assigned);
9,932✔
1015
  return code;
9,932✔
1016
}
1017

1018
int32_t tqProcessRowData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
15,265,056✔
1019
  int32_t   code = 0;
15,265,056✔
1020
  STSchema* pTSchema = NULL;
15,265,056✔
1021

1022
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
15,265,056✔
1023
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
15,300,981✔
1024
  TQ_NULL_GO_TO_END(assigned);
15,296,232✔
1025

1026
  int32_t curRow = 0;
15,296,232✔
1027
  int32_t lastRow = 0;
15,296,232✔
1028
  SArray* pRows = pSubmitTbData->aRowP;
15,281,710✔
1029
  int32_t numOfRows = taosArrayGetSize(pRows);
15,289,843✔
1030
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
15,311,775✔
1031
  TQ_NULL_GO_TO_END(pTSchema);
15,276,023✔
1032
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
15,276,023✔
1033

1034
  for (int32_t i = 0; i < numOfRows; i++) {
316,422,906✔
1035
    bool  buildNew = false;
301,127,132✔
1036
    SRow* pRow = taosArrayGetP(pRows, i);
301,127,132✔
1037
    TQ_NULL_GO_TO_END(pRow);
300,940,719✔
1038

1039
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
1,661,556,033✔
1040
      SColVal colVal = {0};
1,359,728,107✔
1041
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
1,358,665,568✔
1042
      PROCESS_VAL
1,359,404,452✔
1043
      tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], pTSchema->numOfCols);
1,360,506,311✔
1044
    }
1045

1046
    if (buildNew) {
298,424,620✔
1047
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
15,318,143✔
1048
    }
1049

1050
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
298,398,371✔
1051
    TQ_NULL_GO_TO_END(pBlock);
301,327,010✔
1052

1053
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
301,327,010✔
1054
            (int32_t)taosArrayGetSize(blocks));
1055

1056
    int32_t targetIdx = 0;
301,327,010✔
1057
    int32_t sourceIdx = 0;
301,327,010✔
1058
    int32_t colActual = blockDataGetNumOfCols(pBlock);
301,327,010✔
1059
    while (targetIdx < colActual && sourceIdx < pTSchema->numOfCols) {
1,660,008,513✔
1060
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
1,359,075,949✔
1061
      TQ_NULL_GO_TO_END(pColData);
1,358,693,929✔
1062
      SColVal          colVal = {0};
1,358,693,929✔
1063
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
1,357,055,336✔
1064
      SET_DATA
1,358,881,092✔
1065
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
1,360,450,312✔
1066
    }
1067

1068
    curRow++;
301,163,534✔
1069
  }
1070
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
15,295,774✔
1071
  if (pLastBlock != NULL) {
15,317,205✔
1072
    pLastBlock->info.rows = curRow - lastRow;
15,318,300✔
1073
  }
1074

1075
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
15,316,472✔
1076
          (int)taosArrayGetSize(blocks));
1077
END:
16,589,871✔
1078
  if (code != TSDB_CODE_SUCCESS) {
15,278,518✔
1079
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1080
  }
1081
  taosMemoryFreeClear(pTSchema);
15,271,159✔
1082
  taosMemoryFree(assigned);
15,296,908✔
1083
  return code;
15,295,161✔
1084
}
1085

1086
static int32_t buildCreateTbInfo(SMqDataRsp* pRsp, SVCreateTbReq* pCreateTbReq) {
774✔
1087
  int32_t code = 0;
774✔
1088
  int32_t lino = 0;
774✔
1089
  void*   createReq = NULL;
774✔
1090
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
774✔
1091
  TSDB_CHECK_NULL(pCreateTbReq, code, lino, END, TSDB_CODE_INVALID_PARA);
774✔
1092

1093
  if (pRsp->createTableNum == 0) {
774✔
1094
    pRsp->createTableLen = taosArrayInit(0, sizeof(int32_t));
466✔
1095
    TSDB_CHECK_NULL(pRsp->createTableLen, code, lino, END, terrno);
466✔
1096
    pRsp->createTableReq = taosArrayInit(0, sizeof(void*));
466✔
1097
    TSDB_CHECK_NULL(pRsp->createTableReq, code, lino, END, terrno);
466✔
1098
  }
1099

1100
  uint32_t len = 0;
774✔
1101
  tEncodeSize(tEncodeSVCreateTbReq, pCreateTbReq, len, code);
774✔
1102
  TSDB_CHECK_CODE(code, lino, END);
774✔
1103
  createReq = taosMemoryCalloc(1, len);
774✔
1104
  TSDB_CHECK_NULL(createReq, code, lino, END, terrno);
774✔
1105

1106
  SEncoder encoder = {0};
774✔
1107
  tEncoderInit(&encoder, createReq, len);
774✔
1108
  code = tEncodeSVCreateTbReq(&encoder, pCreateTbReq);
774✔
1109
  tEncoderClear(&encoder);
774✔
1110
  TSDB_CHECK_CODE(code, lino, END);
774✔
1111
  TSDB_CHECK_NULL(taosArrayPush(pRsp->createTableLen, &len), code, lino, END, terrno);
1,548✔
1112
  TSDB_CHECK_NULL(taosArrayPush(pRsp->createTableReq, &createReq), code, lino, END, terrno);
1,548✔
1113
  pRsp->createTableNum++;
774✔
1114
  tqTrace("build create table info msg success");
774✔
1115

1116
END:
774✔
1117
  if (code != 0) {
774✔
1118
    tqError("%s failed at %d, failed to build create table info msg:%s", __FUNCTION__, lino, tstrerror(code));
×
1119
    taosMemoryFree(createReq);
×
1120
  }
1121
  return code;
774✔
1122
}
1123

1124
int32_t tqRetrieveTaosxBlock(STqReader* pReader, SMqDataRsp* pRsp, SArray* blocks, SArray* schemas,
15,370,188✔
1125
                             SSubmitTbData** pSubmitTbDataRet, SArray* rawList, int8_t fetchMeta) {
1126
  tqTrace("tq reader retrieve data block msg pointer:%p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
15,370,188✔
1127
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
15,370,188✔
1128
  if (pSubmitTbData == NULL) {
15,375,026✔
1129
    return terrno;
×
1130
  }
1131
  pReader->nextBlk++;
15,375,026✔
1132

1133
  if (pSubmitTbDataRet) {
15,368,552✔
1134
    *pSubmitTbDataRet = pSubmitTbData;
15,373,912✔
1135
  }
1136

1137
  if (fetchMeta == ONLY_META) {
15,369,454✔
1138
    if (pSubmitTbData->pCreateTbReq != NULL) {
484✔
1139
      if (pRsp->createTableReq == NULL) {
88✔
1140
        pRsp->createTableReq = taosArrayInit(0, POINTER_BYTES);
22✔
1141
        if (pRsp->createTableReq == NULL) {
22✔
1142
          return terrno;
×
1143
        }
1144
      }
1145
      if (taosArrayPush(pRsp->createTableReq, &pSubmitTbData->pCreateTbReq) == NULL) {
176✔
1146
        return terrno;
×
1147
      }
1148
      pSubmitTbData->pCreateTbReq = NULL;
88✔
1149
    }
1150
    return 0;
484✔
1151
  }
1152

1153
  int32_t sversion = pSubmitTbData->sver;
15,368,970✔
1154
  int64_t uid = pSubmitTbData->uid;
15,369,658✔
1155
  pReader->lastBlkUid = uid;
15,371,026✔
1156

1157
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
15,371,389✔
1158
  taosMemoryFreeClear(pReader->extSchema);
15,364,460✔
1159
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
15,370,951✔
1160
  if (pReader->pSchemaWrapper == NULL) {
15,365,508✔
1161
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
43,976✔
1162
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1163
    pReader->cachedSchemaSuid = 0;
43,976✔
1164
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
43,976✔
1165
  }
1166

1167
  if (pSubmitTbData->pCreateTbReq != NULL) {
15,290,329✔
1168
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
774✔
1169
    if (code != 0) {
774✔
1170
      return code;
×
1171
    }
1172
  } else if (rawList != NULL) {
15,311,284✔
1173
    if (taosArrayPush(schemas, &pReader->pSchemaWrapper) == NULL) {
×
1174
      return terrno;
×
1175
    }
1176
    pReader->pSchemaWrapper = NULL;
×
1177
    return 0;
×
1178
  }
1179

1180
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
15,312,058✔
1181
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
9,894✔
1182
  } else {
1183
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
15,284,841✔
1184
  }
1185
}
1186

1187
int32_t tqReaderSetColIdList(STqReader* pReader, SArray* pColIdList, const char* id) {
113,855✔
1188
  if (pReader == NULL) {
113,855✔
1189
    return TSDB_CODE_SUCCESS;
×
1190
  }
1191
  pReader->pColIdList = pColIdList;
113,855✔
1192
  return tqCollectPhysicalTables(pReader, id);
115,412✔
1193
}
1194

1195
int32_t tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
120,508✔
1196
  if (pReader == NULL || tbUidList == NULL) {
120,508✔
1197
    return TSDB_CODE_SUCCESS;
×
1198
  }
1199
  if (pReader->tbIdHash) {
120,508✔
1200
    taosHashClear(pReader->tbIdHash);
1,166✔
1201
  } else {
1202
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
119,258✔
1203
    if (pReader->tbIdHash == NULL) {
119,446✔
1204
      tqError("s-task:%s failed to init hash table", id);
×
1205
      return terrno;
×
1206
    }
1207
  }
1208

1209
  for (int i = 0; i < taosArrayGetSize(tbUidList); i++) {
2,940,752✔
1210
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
2,817,492✔
1211
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
2,815,155✔
1212
      tqError("s-task:%s failed to add table uid:%" PRId64 " to hash", id, *pKey);
×
1213
      continue;
×
1214
    }
1215
  }
1216

1217
  tqDebug("s-task:%s %d tables are set to be queried target table", id, (int32_t)taosArrayGetSize(tbUidList));
120,752✔
1218
  return TSDB_CODE_SUCCESS;
120,612✔
1219
}
1220

1221
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
63,321✔
1222
  if (pReader == NULL || pTableUidList == NULL) {
63,321✔
1223
    return;
×
1224
  }
1225
  if (pReader->tbIdHash == NULL) {
63,321✔
1226
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
×
1227
    if (pReader->tbIdHash == NULL) {
×
1228
      tqError("failed to init hash table");
×
1229
      return;
×
1230
    }
1231
  }
1232

1233
  int32_t numOfTables = taosArrayGetSize(pTableUidList);
63,321✔
1234
  for (int i = 0; i < numOfTables; i++) {
99,607✔
1235
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
36,286✔
1236
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
36,286✔
1237
      tqError("failed to add table uid:%" PRId64 " to hash", *pKey);
×
1238
      continue;
×
1239
    }
1240
  }
1241
}
1242

1243
bool tqReaderIsQueriedTable(STqReader* pReader, uint64_t uid) {
×
1244
  if (pReader == NULL) {
×
1245
    return false;
×
1246
  }
1247
  return taosHashGet(pReader->tbIdHash, &uid, sizeof(uint64_t)) != NULL;
×
1248
}
1249

1250
bool tqCurrentBlockConsumed(const STqReader* pReader) {
×
1251
  if (pReader == NULL) {
×
1252
    return false;
×
1253
  }
1254
  return pReader->msg.msgStr == NULL;
×
1255
}
1256

1257
void tqReaderRemoveTbUidList(STqReader* pReader, const SArray* tbUidList) {
217✔
1258
  if (pReader == NULL || tbUidList == NULL) {
217✔
1259
    return;
×
1260
  }
1261
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
434✔
1262
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
217✔
1263
    if (pKey && taosHashRemove(pReader->tbIdHash, pKey, sizeof(int64_t)) != 0) {
217✔
1264
      tqError("failed to remove table uid:%" PRId64 " from hash", *pKey);
×
1265
    }
1266
  }
1267
}
1268

1269
int32_t tqUpdateTbUidList(STQ* pTq, const SArray* tbUidList, bool isAdd) {
64,566,808✔
1270
  if (pTq == NULL) {
64,566,808✔
1271
    return 0;  // mounted vnode may have no tq
×
1272
  }
1273
  if (tbUidList == NULL) {
64,566,808✔
1274
    return TSDB_CODE_INVALID_PARA;
×
1275
  }
1276
  void*   pIter = NULL;
64,566,808✔
1277
  int32_t vgId = TD_VID(pTq->pVnode);
64,566,808✔
1278

1279
  // update the table list for each consumer handle
1280
  taosWLockLatch(&pTq->lock);
64,567,472✔
1281
  while (1) {
243,514✔
1282
    pIter = taosHashIterate(pTq->pHandle, pIter);
64,810,644✔
1283
    if (pIter == NULL) {
64,810,986✔
1284
      break;
64,567,472✔
1285
    }
1286

1287
    STqHandle* pTqHandle = (STqHandle*)pIter;
243,514✔
1288
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
243,514✔
1289
      int32_t code = qUpdateTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList, isAdd);
63,538✔
1290
      if (code != 0) {
63,538✔
1291
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1292
        continue;
×
1293
      }
1294
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
179,976✔
1295
      if (!isAdd) {
178,810✔
1296
        int32_t sz = taosArrayGetSize(tbUidList);
60,264✔
1297
        for (int32_t i = 0; i < sz; i++) {
60,264✔
1298
          int64_t* tbUid = (int64_t*)taosArrayGet(tbUidList, i);
×
1299
          if (tbUid &&
×
1300
              taosHashPut(pTqHandle->execHandle.execDb.pFilterOutTbUid, tbUid, sizeof(int64_t), NULL, 0) != 0) {
×
1301
            tqError("failed to add table uid:%" PRId64 " to hash", *tbUid);
×
1302
            continue;
×
1303
          }
1304
        }
1305
      }
1306
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
1,166✔
1307
      if (isAdd) {
1,166✔
1308
        SArray* list = NULL;
1,166✔
1309
        int     ret = qGetTableList(pTqHandle->execHandle.execTb.suid, pTq->pVnode, pTqHandle->execHandle.execTb.node,
1,166✔
1310
                                    &list, pTqHandle->execHandle.task);
1311
        if (ret == 0) {
1,166✔
1312
          ret = tqReaderSetTbUidList(pTqHandle->execHandle.pTqReader, list, NULL);
1,166✔
1313
        }                            
1314
        if (ret != TDB_CODE_SUCCESS) {
1,166✔
1315
          tqError("qGetTableList in tqUpdateTbUidList error:%d handle %s consumer:0x%" PRIx64, ret, pTqHandle->subKey,
×
1316
                  pTqHandle->consumerId);
1317
          taosArrayDestroy(list);
×
1318
          taosHashCancelIterate(pTq->pHandle, pIter);
×
1319
          taosWUnLockLatch(&pTq->lock);
×
1320

1321
          return ret;
×
1322
        }
1323
        taosArrayDestroy(list);
1,166✔
1324
      } else {
1325
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
×
1326
      }
1327
    }
1328
  }
1329
  taosWUnLockLatch(&pTq->lock);
64,567,472✔
1330
  return 0;
64,567,643✔
1331
}
1332

UNCOV
1333
static void destroySourceScanTables(void* ptr) {
×
1334
  SArray** pTables = ptr;
×
1335
  if (pTables && *pTables) {
×
1336
    taosArrayDestroy(*pTables);
×
UNCOV
1337
    *pTables = NULL;
×
1338
  }
1339
}
×
1340

UNCOV
1341
static int32_t compareSVTColInfo(const void* p1, const void* p2) {
×
1342
  SVTColInfo* pCol1 = (SVTColInfo*)p1;
×
1343
  SVTColInfo* pCol2 = (SVTColInfo*)p2;
×
UNCOV
1344
  if (pCol1->vColId == pCol2->vColId) {
×
UNCOV
1345
    return 0;
×
1346
  } else if (pCol1->vColId < pCol2->vColId) {
×
1347
    return -1;
×
1348
  } else {
UNCOV
1349
    return 1;
×
1350
  }
1351
}
1352

1353
int32_t tqReaderSetVtableInfo(STqReader* pReader, void* vnode, void* ptr, SSHashObj* pVtableInfos,
×
1354
                              SSDataBlock** ppResBlock, const char* idstr) {
1355
  int32_t            code = TSDB_CODE_SUCCESS;
×
1356
  int32_t            lino = 0;
×
1357
  SStorageAPI*       pAPI = ptr;
×
UNCOV
1358
  SVTSourceScanInfo* pScanInfo = NULL;
×
1359
  SHashObj*          pVirtualTables = NULL;
×
1360
  SMetaReader        metaReader = {0};
×
1361
  SVTColInfo         colInfo = {0};
×
1362
  SSchemaWrapper*    schema = NULL;
×
1363

1364
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1365
  TSDB_CHECK_NULL(vnode, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1366
  TSDB_CHECK_NULL(pAPI, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1367

1368
  pScanInfo = &pReader->vtSourceScanInfo;
×
1369
  taosHashCleanup(pScanInfo->pVirtualTables);
×
UNCOV
1370
  pScanInfo->pVirtualTables = NULL;
×
1371

1372
  if (tSimpleHashGetSize(pVtableInfos) == 0) {
×
1373
    goto _end;
×
1374
  }
1375

1376
  pVirtualTables = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
1377
  TSDB_CHECK_NULL(pVirtualTables, code, lino, _end, terrno);
×
1378
  taosHashSetFreeFp(pVirtualTables, destroySourceScanTables);
×
1379

1380
  int32_t iter = 0;
×
UNCOV
1381
  void*   px = tSimpleHashIterate(pVtableInfos, NULL, &iter);
×
1382
  while (px != NULL) {
×
1383
    int64_t vTbUid = *(int64_t*)tSimpleHashGetKey(px, NULL);
×
1384
    SArray* pColInfos = taosArrayInit(8, sizeof(SVTColInfo));
×
UNCOV
1385
    TSDB_CHECK_NULL(pColInfos, code, lino, _end, terrno);
×
1386
    code = taosHashPut(pVirtualTables, &vTbUid, sizeof(int64_t), &pColInfos, POINTER_BYTES);
×
1387
    TSDB_CHECK_CODE(code, lino, _end);
×
1388

1389
    SSHashObj* pPhysicalTables = *(SSHashObj**)px;
×
UNCOV
1390
    int32_t    iterIn = 0;
×
UNCOV
1391
    void*      pxIn = tSimpleHashIterate(pPhysicalTables, NULL, &iterIn);
×
UNCOV
1392
    while (pxIn != NULL) {
×
1393
      char* physicalTableName = tSimpleHashGetKey(pxIn, NULL);
×
1394
      pAPI->metaReaderFn.clearReader(&metaReader);
×
1395
      pAPI->metaReaderFn.initReader(&metaReader, vnode, META_READER_LOCK, &pAPI->metaFn);
×
1396
      code = pAPI->metaReaderFn.getTableEntryByName(&metaReader, physicalTableName);
×
1397
      TSDB_CHECK_CODE(code, lino, _end);
×
UNCOV
1398
      pAPI->metaReaderFn.readerReleaseLock(&metaReader);
×
1399
      colInfo.pTbUid = metaReader.me.uid;
×
1400

1401
      switch (metaReader.me.type) {
×
1402
        case TSDB_CHILD_TABLE: {
×
1403
          int64_t suid = metaReader.me.ctbEntry.suid;
×
1404
          pAPI->metaReaderFn.clearReader(&metaReader);
×
UNCOV
1405
          pAPI->metaReaderFn.initReader(&metaReader, vnode, META_READER_LOCK, &pAPI->metaFn);
×
UNCOV
1406
          code = pAPI->metaReaderFn.getTableEntryByUid(&metaReader, suid);
×
UNCOV
1407
          TSDB_CHECK_CODE(code, lino, _end);
×
UNCOV
1408
          pAPI->metaReaderFn.readerReleaseLock(&metaReader);
×
1409
          schema = &metaReader.me.stbEntry.schemaRow;
×
1410
          break;
×
1411
        }
UNCOV
1412
        case TSDB_NORMAL_TABLE: {
×
1413
          schema = &metaReader.me.ntbEntry.schemaRow;
×
UNCOV
1414
          break;
×
1415
        }
1416
        default: {
×
1417
          tqError("invalid table type: %d", metaReader.me.type);
×
UNCOV
1418
          code = TSDB_CODE_INVALID_PARA;
×
UNCOV
1419
          TSDB_CHECK_CODE(code, lino, _end);
×
1420
        }
1421
      }
1422

1423
      SArray* pCols = *(SArray**)pxIn;
×
1424
      int32_t ncols = taosArrayGetSize(pCols);
×
UNCOV
1425
      for (int32_t i = 0; i < ncols; ++i) {
×
UNCOV
1426
        SColIdName* pCol = taosArrayGet(pCols, i);
×
1427
        colInfo.vColId = pCol->colId;
×
1428

UNCOV
1429
        for (int32_t j = 0; j < schema->nCols; ++j) {
×
1430
          if (strncmp(pCol->colName, schema->pSchema[j].name, strlen(schema->pSchema[j].name)) == 0) {
×
1431
            colInfo.pColId = schema->pSchema[j].colId;
×
1432
            void* px = taosArrayPush(pColInfos, &colInfo);
×
UNCOV
1433
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
×
1434
            break;
×
1435
          }
1436
        }
1437
      }
1438

UNCOV
1439
      taosArraySort(pColInfos, compareSVTColInfo);
×
UNCOV
1440
      pxIn = tSimpleHashIterate(pPhysicalTables, pxIn, &iterIn);
×
1441
    }
1442

UNCOV
1443
    px = tSimpleHashIterate(pVtableInfos, px, &iter);
×
1444
  }
1445

UNCOV
1446
  pScanInfo->pVirtualTables = pVirtualTables;
×
UNCOV
1447
  pVirtualTables = NULL;
×
1448

1449
  // set the result data block
UNCOV
1450
  if (pReader->pResBlock) {
×
UNCOV
1451
    blockDataDestroy(pReader->pResBlock);
×
1452
  }
UNCOV
1453
  pReader->pResBlock = *ppResBlock;
×
UNCOV
1454
  *ppResBlock = NULL;
×
1455

1456
  // update reader callback for vtable source scan
UNCOV
1457
  pAPI->tqReaderFn.tqNextBlockImpl = tqNextVTableSourceBlockImpl;
×
UNCOV
1458
  pAPI->tqReaderFn.tqReaderIsQueriedTable = tqReaderIsQueriedSourceTable;
×
1459

UNCOV
1460
_end:
×
UNCOV
1461
  if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
1462
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1463
  }
UNCOV
1464
  pAPI->metaReaderFn.clearReader(&metaReader);
×
UNCOV
1465
  if (pVirtualTables != NULL) {
×
1466
    taosHashCleanup(pVirtualTables);
×
1467
  }
1468
  return code;
×
1469
}
1470

1471
static int32_t tqCollectPhysicalTables(STqReader* pReader, const char* idstr) {
115,412✔
1472
  int32_t            code = TSDB_CODE_SUCCESS;
115,412✔
1473
  int32_t            lino = 0;
115,412✔
1474
  SVTSourceScanInfo* pScanInfo = NULL;
115,412✔
1475
  SHashObj*          pVirtualTables = NULL;
115,412✔
1476
  SHashObj*          pPhysicalTables = NULL;
115,412✔
1477
  void*              pIter = NULL;
115,412✔
1478
  void*              px = NULL;
115,412✔
1479

1480
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
115,412✔
1481

1482
  pScanInfo = &pReader->vtSourceScanInfo;
115,412✔
1483
  taosHashCleanup(pScanInfo->pPhysicalTables);
115,412✔
1484
  pScanInfo->pPhysicalTables = NULL;
115,362✔
1485
  taosLRUCacheCleanup(pScanInfo->pPhyTblSchemaCache);
115,362✔
1486
  pScanInfo->pPhyTblSchemaCache = NULL;
115,362✔
1487
  pScanInfo->nextVirtualTableIdx = -1;
115,362✔
1488
  pScanInfo->metaFetch = 0;
115,362✔
1489
  pScanInfo->cacheHit = 0;
115,332✔
1490

1491
  pVirtualTables = pScanInfo->pVirtualTables;
115,308✔
1492
  if (taosHashGetSize(pVirtualTables) == 0 || taosArrayGetSize(pReader->pColIdList) == 0) {
115,178✔
1493
    goto _end;
114,816✔
1494
  }
1495

UNCOV
1496
  pPhysicalTables = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
UNCOV
1497
  TSDB_CHECK_NULL(pPhysicalTables, code, lino, _end, terrno);
×
1498
  taosHashSetFreeFp(pPhysicalTables, destroySourceScanTables);
×
1499

1500
  pIter = taosHashIterate(pVirtualTables, NULL);
×
1501
  while (pIter != NULL) {
×
1502
    int64_t vTbUid = *(int64_t*)taosHashGetKey(pIter, NULL);
×
UNCOV
1503
    SArray* pColInfos = *(SArray**)pIter;
×
1504
    TSDB_CHECK_NULL(pColInfos, code, lino, _end, TSDB_CODE_INTERNAL_ERROR);
×
1505

1506
    // Traverse all required columns and collect corresponding physical tables
UNCOV
1507
    int32_t nColInfos = taosArrayGetSize(pColInfos);
×
1508
    int32_t nOutputCols = taosArrayGetSize(pReader->pColIdList);
×
UNCOV
1509
    for (int32_t i = 0, j = 0; i < nColInfos && j < nOutputCols;) {
×
UNCOV
1510
      SVTColInfo* pCol = taosArrayGet(pColInfos, i);
×
1511
      col_id_t    colIdNeed = *(col_id_t*)taosArrayGet(pReader->pColIdList, j);
×
1512
      if (pCol->vColId < colIdNeed) {
×
UNCOV
1513
        i++;
×
1514
      } else if (pCol->vColId > colIdNeed) {
×
1515
        j++;
×
1516
      } else {
UNCOV
1517
        SArray* pRelatedVTs = NULL;
×
UNCOV
1518
        px = taosHashGet(pPhysicalTables, &pCol->pTbUid, sizeof(int64_t));
×
1519
        if (px == NULL) {
×
UNCOV
1520
          pRelatedVTs = taosArrayInit(8, sizeof(int64_t));
×
1521
          TSDB_CHECK_NULL(pRelatedVTs, code, lino, _end, terrno);
×
UNCOV
1522
          code = taosHashPut(pPhysicalTables, &pCol->pTbUid, sizeof(int64_t), &pRelatedVTs, POINTER_BYTES);
×
UNCOV
1523
          if (code != TSDB_CODE_SUCCESS) {
×
1524
            taosArrayDestroy(pRelatedVTs);
×
UNCOV
1525
            TSDB_CHECK_CODE(code, lino, _end);
×
1526
          }
1527
        } else {
UNCOV
1528
          pRelatedVTs = *(SArray**)px;
×
1529
        }
UNCOV
1530
        if (taosArrayGetSize(pRelatedVTs) == 0 || *(int64_t*)taosArrayGetLast(pRelatedVTs) != vTbUid) {
×
UNCOV
1531
          px = taosArrayPush(pRelatedVTs, &vTbUid);
×
1532
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
×
1533
        }
1534
        i++;
×
UNCOV
1535
        j++;
×
1536
      }
1537
    }
UNCOV
1538
    pIter = taosHashIterate(pVirtualTables, pIter);
×
1539
  }
1540

1541
  pScanInfo->pPhysicalTables = pPhysicalTables;
×
1542
  pPhysicalTables = NULL;
×
1543

1544
  if (taosHashGetSize(pScanInfo->pPhysicalTables) > 0) {
×
UNCOV
1545
    pScanInfo->pPhyTblSchemaCache = taosLRUCacheInit(1024 * 128, -1, .5);
×
1546
    TSDB_CHECK_NULL(pScanInfo->pPhyTblSchemaCache, code, lino, _end, terrno);
×
1547
  }
1548

UNCOV
1549
_end:
×
1550
  if (code != TSDB_CODE_SUCCESS) {
114,816✔
1551
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1552
  }
1553
  if (pIter != NULL) {
114,812✔
UNCOV
1554
    taosHashCancelIterate(pReader->tbIdHash, pIter);
×
1555
  }
1556
  if (pPhysicalTables != NULL) {
115,050✔
1557
    taosHashCleanup(pPhysicalTables);
×
1558
  }
1559
  return code;
114,706✔
1560
}
1561

1562
static void freeTableSchemaCache(const void* key, size_t keyLen, void* value, void* ud) {
×
1563
  if (value) {
×
1564
    SSchemaWrapper* pSchemaWrapper = value;
×
1565
    tDeleteSchemaWrapper(pSchemaWrapper);
1566
  }
UNCOV
1567
}
×
1568

1569
bool tqNextVTableSourceBlockImpl(STqReader* pReader, const char* idstr) {
×
1570
  int32_t            code = TSDB_CODE_SUCCESS;
×
UNCOV
1571
  int32_t            lino = 0;
×
UNCOV
1572
  SVTSourceScanInfo* pScanInfo = NULL;
×
1573

1574
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1575

1576
  pScanInfo = &pReader->vtSourceScanInfo;
×
1577
  if (pReader->msg.msgStr == NULL || taosHashGetSize(pScanInfo->pPhysicalTables) == 0) {
×
1578
    return false;
×
1579
  }
1580

UNCOV
1581
  if (pScanInfo->nextVirtualTableIdx >= 0) {
×
1582
    // The data still needs to be converted into the virtual table result block
1583
    return true;
×
1584
  }
1585

UNCOV
1586
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
×
1587
  while (pReader->nextBlk < blockSz) {
×
UNCOV
1588
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
×
UNCOV
1589
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, _end, terrno);
×
UNCOV
1590
    int64_t pTbUid = pSubmitTbData->uid;
×
UNCOV
1591
    void*   px = taosHashGet(pScanInfo->pPhysicalTables, &pTbUid, sizeof(int64_t));
×
UNCOV
1592
    if (px != NULL) {
×
UNCOV
1593
      SArray* pRelatedVTs = *(SArray**)px;
×
UNCOV
1594
      if (taosArrayGetSize(pRelatedVTs) > 0) {
×
UNCOV
1595
        pScanInfo->nextVirtualTableIdx = 0;
×
UNCOV
1596
        return true;
×
1597
      }
1598
    }
UNCOV
1599
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64, pReader->nextBlk, blockSz, pTbUid);
×
UNCOV
1600
    pReader->nextBlk++;
×
1601
  }
1602

UNCOV
1603
  tqReaderClearSubmitMsg(pReader);
×
UNCOV
1604
  tqTrace("iterator data block end, total block num:%d", blockSz);
×
1605

UNCOV
1606
_end:
×
UNCOV
1607
  if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
1608
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1609
  }
UNCOV
1610
  return false;
×
1611
}
1612

UNCOV
1613
bool tqReaderIsQueriedSourceTable(STqReader* pReader, uint64_t uid) {
×
UNCOV
1614
  if (pReader == NULL) {
×
UNCOV
1615
    return false;
×
1616
  }
UNCOV
1617
  return taosHashGet(pReader->vtSourceScanInfo.pPhysicalTables, &uid, sizeof(uint64_t)) != NULL;
×
1618
}
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