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

taosdata / TDengine / #4861

22 Nov 2025 07:23AM UTC coverage: 64.274% (-0.06%) from 64.335%
#4861

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

0 of 79 new or added lines in 2 files covered. (0.0%)

820 existing lines in 129 files now uncovered.

154466 of 240326 relevant lines covered (64.27%)

112821527.61 hits per line

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

61.71
/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) {
11,310✔
22
  if (pHandle == NULL || pHead == NULL) {
11,310✔
23
    return false;
×
24
  }
25
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
11,310✔
26
    return true;
10,995✔
27
  }
28

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

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

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

43
  if (msgType == TDMT_VND_CREATE_STB || msgType == TDMT_VND_ALTER_STB) {
360✔
44
    SVCreateStbReq req = {0};
45✔
45
    if (tDecodeSVCreateStbReq(&dcoder, &req) < 0) {
45✔
46
      goto end;
×
47
    }
48
    realTbSuid = req.suid;
45✔
49
  } else if (msgType == TDMT_VND_DROP_STB) {
270✔
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) {
270✔
56
    SVCreateTbBatchReq req = {0};
135✔
57
    if (tDecodeSVCreateTbBatchReq(&dcoder, &req) < 0) {
135✔
58
      goto end;
×
59
    }
60

61
    int32_t        needRebuild = 0;
135✔
62
    SVCreateTbReq* pCreateReq = NULL;
135✔
63
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
270✔
64
      pCreateReq = req.pReqs + iReq;
135✔
65
      if (pCreateReq->type == TSDB_CHILD_TABLE && pCreateReq->ctb.suid == tbSuid &&
225✔
66
          taosHashGet(pReader->tbIdHash, &pCreateReq->uid, sizeof(int64_t)) != NULL) {  
90✔
67
        needRebuild++;
45✔
68
      }
69
    }
70
    if (needRebuild == 0) {
135✔
71
      // do nothing
72
    } else if (needRebuild == req.nReqs) {
45✔
73
      realTbSuid = tbSuid;
45✔
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);
135✔
121
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
135✔
122
    SVAlterTbReq req = {0};
135✔
123

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

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

131
    if (metaGetTableEntryByName(&mr, req.tbName) < 0) {
135✔
132
      metaReaderClear(&mr);
×
133
      goto end;
×
134
    }
135
    if (taosHashGet(pReader->tbIdHash, &mr.me.uid, sizeof(int64_t)) != NULL) {
135✔
136
      realTbSuid = mr.me.ctbEntry.suid;
90✔
137
    }
138
    metaReaderClear(&mr);
135✔
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:
315✔
210
  tDecoderClear(&dcoder);
315✔
211
  bool tmp = tbSuid == realTbSuid;
315✔
212
  tqDebug("%s suid:%" PRId64 " realSuid:%" PRId64 " return:%d", __FUNCTION__, tbSuid, realTbSuid, tmp);
315✔
213
  return tmp;
315✔
214
}
215

216
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
15,168,061✔
217
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
15,168,061✔
218
    return -1;
×
219
  }
220
  int32_t code = -1;
15,182,300✔
221
  int32_t vgId = TD_VID(pTq->pVnode);
15,182,300✔
222
  int64_t id = pHandle->pWalReader->readerId;
15,184,231✔
223

224
  int64_t offset = *fetchOffset;
15,183,872✔
225
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
15,184,149✔
226
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
15,184,908✔
227
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
15,184,558✔
228

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

233
  while (offset <= appliedVer) {
15,609,512✔
234
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
14,697,332✔
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,
14,697,054✔
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) {
14,696,042✔
245
      code = walFetchBody(pHandle->pWalReader);
14,262,933✔
246
      goto END;
14,262,890✔
247
    } else {
248
      if (pHandle->fetchMeta != WITH_DATA) {
434,487✔
249
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
16,805✔
250
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
16,805✔
251
          code = walFetchBody(pHandle->pWalReader);
11,330✔
252
          if (code < 0) {
11,330✔
253
            goto END;
×
254
          }
255

256
          pHead = &(pHandle->pWalReader->pHead->head);
11,330✔
257
          if (isValValidForTable(pHandle, pHead)) {
11,330✔
258
            code = 0;
11,175✔
259
            goto END;
11,175✔
260
          } else {
261
            offset++;
135✔
262
            code = -1;
135✔
263
            continue;
135✔
264
          }
265
        }
266
      }
267
      code = walSkipFetchBody(pHandle->pWalReader);
423,157✔
268
      if (code < 0) {
423,157✔
269
        goto END;
×
270
      }
271
      offset++;
423,157✔
272
    }
273
    code = -1;
423,157✔
274
  }
275

276
END:
912,180✔
277
  *fetchOffset = offset;
15,186,245✔
278
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
15,186,029✔
279
          ", applied:%" PRId64 ", 0x%" PRIx64,
280
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
281
  return code;
15,185,650✔
282
}
283

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

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

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

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

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

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

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

335
  return pReader;
128,748✔
336
}
337

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

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

347
  if (pReader->pSchemaWrapper) {
128,748✔
348
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
74,209✔
349
  }
350

351
  taosMemoryFree(pReader->extSchema);
128,748✔
352
  if (pReader->pColIdList) {
128,748✔
353
    taosArrayDestroy(pReader->pColIdList);
108,899✔
354
  }
355

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

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

367
int32_t tqReaderSeek(STqReader* pReader, int64_t ver, const char* id) {
236,645✔
368
  if (pReader == NULL) {
236,645✔
369
    return TSDB_CODE_INVALID_PARA;
×
370
  }
371
  if (walReaderSeekVer(pReader->pWalReader, ver) < 0) {
236,645✔
372
    return terrno;
8,662✔
373
  }
374
  tqDebug("wal reader seek to ver:%" PRId64 " %s", ver, id);
227,844✔
375
  return 0;
228,458✔
376
}
377

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

384
  int64_t st = taosGetTimestampMs();
37,278,100✔
385
  while (1) {
39,215,496✔
386
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
76,493,596✔
387
    while (pReader->nextBlk < numOfBlocks) {
81,708,216✔
388
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
39,215,528✔
389
              pReader->msg.ver);
390

391
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
39,215,528✔
392
      if (pSubmitTbData == NULL) {
39,211,295✔
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;
567✔
396
      }
397
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
39,211,295✔
398
        pReader->nextBlk += 1;
2,120✔
399
        continue;
2,120✔
400
      }
401
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
39,393,082✔
402
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
34,183,132✔
403
        SSDataBlock* pRes = NULL;
34,183,132✔
404
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
34,171,277✔
405
        if (code == TSDB_CODE_SUCCESS) {
34,182,639✔
406
          return true;
33,995,439✔
407
        }
408
      } else {
409
        pReader->nextBlk += 1;
5,029,820✔
410
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
5,029,820✔
411
      }
412
    }
413

414
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
42,496,001✔
415
    pReader->msg.msgStr = NULL;
42,494,771✔
416

417
    int64_t elapsed = taosGetTimestampMs() - st;
42,495,174✔
418
    if (elapsed > 1000 || elapsed < 0) {
42,495,174✔
419
      return false;
×
420
    }
421

422
    // try next message in wal file
423
    if (walNextValidMsg(pWalReader, false) < 0) {
42,495,174✔
424
      return false;
3,279,220✔
425
    }
426

427
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
39,214,524✔
428
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
39,215,166✔
429
    int64_t ver = pWalReader->pHead->head.version;
39,214,179✔
430
    SDecoder decoder = {0};
39,214,540✔
431
    if (tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL, &decoder) != 0) {
39,212,840✔
432
      tDecoderClear(&decoder);
×
433
      return false;
×
434
    }
435
    tDecoderClear(&decoder);
39,210,929✔
436
    pReader->nextBlk = 0;
39,214,893✔
437
  }
438
}
439

440
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList, SDecoder* decoder) {
53,474,226✔
441
  if (pReader == NULL) {
53,474,226✔
442
    return TSDB_CODE_INVALID_PARA;
×
443
  }
444
  pReader->msg.msgStr = msgStr;
53,474,226✔
445
  pReader->msg.msgLen = msgLen;
53,474,904✔
446
  pReader->msg.ver = ver;
53,476,186✔
447

448
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
53,475,458✔
449

450
  tDecoderInit(decoder, pReader->msg.msgStr, pReader->msg.msgLen);
53,475,458✔
451
  int32_t code = tDecodeSubmitReq(decoder, &pReader->submit, rawList);
53,476,556✔
452

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

457
  return code;
53,471,161✔
458
}
459

460
void tqReaderClearSubmitMsg(STqReader* pReader) {
28,508,761✔
461
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
28,508,761✔
462
  pReader->nextBlk = 0;
28,506,793✔
463
  pReader->msg.msgStr = NULL;
28,512,382✔
464
}
28,513,621✔
465

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

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

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

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

495
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
5,422,987✔
496
  while (pReader->nextBlk < blockSz) {
5,732,409✔
497
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
2,866,301✔
498
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
2,866,744✔
499
    uid = pSubmitTbData->uid;
2,866,744✔
500
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
2,866,744✔
501
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
2,866,914✔
502

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

508
  tqReaderClearSubmitMsg(pReader);
2,865,495✔
509
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
2,865,492✔
510

511
END:
2,865,492✔
512
  tqTrace("%s:%d return:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
5,423,072✔
513
  return code;
5,422,735✔
514
}
515

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

521
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
22,786,724✔
522
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
22,786,724✔
523
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
22,789,625✔
524

525
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
22,789,625✔
526
  while (pReader->nextBlk < blockSz) {
22,790,033✔
527
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
11,398,474✔
528
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
11,398,727✔
529
    uid = pSubmitTbData->uid;
11,398,727✔
530
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
11,398,684✔
531
    TSDB_CHECK_NULL(ret, code, lino, END, true);
11,398,291✔
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);
11,394,520✔
536
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
11,392,425✔
537

538
END:
11,392,425✔
539
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
22,790,716✔
540
  return code;
22,777,391✔
541
}
542

543
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask,
13,908,439✔
544
                    SExtSchema* extSrc) {
545
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
13,908,439✔
546
    return TSDB_CODE_INVALID_PARA;
×
547
  }
548
  int32_t code = 0;
13,911,497✔
549

550
  int32_t cnt = 0;
13,911,497✔
551
  for (int32_t i = 0; i < pSrc->nCols; i++) {
63,502,183✔
552
    cnt += mask[i];
49,585,990✔
553
  }
554

555
  pDst->nCols = cnt;
13,916,078✔
556
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
13,917,240✔
557
  if (pDst->pSchema == NULL) {
13,911,864✔
558
    return TAOS_GET_TERRNO(terrno);
×
559
  }
560

561
  int32_t j = 0;
13,909,947✔
562
  for (int32_t i = 0; i < pSrc->nCols; i++) {
63,511,362✔
563
    if (mask[i]) {
49,585,762✔
564
      pDst->pSchema[j++] = pSrc->pSchema[i];
49,598,912✔
565
      SColumnInfoData colInfo =
49,598,194✔
566
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
49,598,649✔
567
      if (extSrc != NULL) {
49,594,126✔
568
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
4,360✔
569
      }
570
      code = blockDataAppendColInfo(pBlock, &colInfo);
49,594,126✔
571
      if (code != 0) {
49,600,138✔
572
        return code;
×
573
      }
574
    }
575
  }
576
  return 0;
13,918,597✔
577
}
578

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

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

596
  int32_t numOfCols = taosArrayGetSize(pColIdList);
63,120✔
597

598
  if (numOfCols == 0) {  // all columns are required
63,120✔
599
    for (int32_t i = 0; i < pSchema->nCols; ++i) {
×
UNCOV
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) {
63,120✔
614
      numOfCols = pSchema->nCols;
72✔
615
    }
616

617
    int32_t i = 0;
63,120✔
618
    int32_t j = 0;
63,120✔
619
    while (i < pSchema->nCols && j < numOfCols) {
818,706✔
620
      SSchema* pColSchema = &pSchema->pSchema[i];
755,586✔
621
      col_id_t colIdSchema = pColSchema->colId;
755,586✔
622

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

646
  return TSDB_CODE_SUCCESS;
63,120✔
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)) {
713,893,698✔
695
      char val[65535 + 2] = {0};
749,154,399✔
696
      if (pColVal->value.pData != NULL) {
748,250,859✔
697
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
748,804,387✔
698
      }
699
      varDataSetLen(val, pColVal->value.nData);
748,438,989✔
700
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
749,336,810✔
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) {
34,170,499✔
713
  if (pReader == NULL || pRes == NULL) {
34,170,499✔
714
    return TSDB_CODE_INVALID_PARA;
×
715
  }
716
  tqDebug("tq reader retrieve data block %p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
34,181,970✔
717
  int32_t        code = 0;
34,186,120✔
718
  int32_t        line = 0;
34,186,120✔
719
  STSchema*      pTSchema = NULL;
34,186,120✔
720
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk++);
34,186,120✔
721
  TSDB_CHECK_NULL(pSubmitTbData, code, line, END, terrno);
34,185,871✔
722
  SSDataBlock* pBlock = pReader->pResBlock;
34,185,871✔
723
  *pRes = pBlock;
34,185,871✔
724

725
  blockDataCleanup(pBlock);
34,184,935✔
726

727
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
34,185,976✔
728
  int32_t sversion = pSubmitTbData->sver;
34,186,080✔
729
  int64_t suid = pSubmitTbData->suid;
34,186,288✔
730
  int64_t uid = pSubmitTbData->uid;
34,186,184✔
731
  pReader->lastTs = pSubmitTbData->ctimeMs;
34,186,080✔
732

733
  pBlock->info.id.uid = uid;
34,186,184✔
734
  pBlock->info.version = pReader->msg.ver;
34,185,339✔
735

736
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
34,185,547✔
737
      (pReader->cachedSchemaVer != sversion)) {
33,935,624✔
738
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
244,691✔
739
    taosMemoryFree(pReader->extSchema);
250,320✔
740
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
250,320✔
741
    if (pReader->pSchemaWrapper == NULL) {
250,320✔
742
      tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", uid:%" PRId64
187,200✔
743
             "version %d, possibly dropped table",
744
             vgId, suid, uid, pReader->cachedSchemaVer);
745
      pReader->cachedSchemaSuid = 0;
187,200✔
746
      return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
187,200✔
747
    }
748

749
    pReader->cachedSchemaUid = uid;
63,120✔
750
    pReader->cachedSchemaSuid = suid;
63,120✔
751
    pReader->cachedSchemaVer = sversion;
63,120✔
752

753
    if (pReader->cachedSchemaVer != pReader->pSchemaWrapper->version) {
63,120✔
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);
63,120✔
759
    TSDB_CHECK_CODE(code, line, END);
63,120✔
760
    pBlock = pReader->pResBlock;
63,120✔
761
    *pRes = pBlock;
63,120✔
762
  }
763

764
  int32_t numOfRows = 0;
33,998,571✔
765
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
33,998,571✔
766
    SColData* pCol = taosArrayGet(pSubmitTbData->aCol, 0);
152✔
767
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
152✔
768
    numOfRows = pCol->nVal;
152✔
769
  } else {
770
    numOfRows = taosArrayGetSize(pSubmitTbData->aRowP);
33,998,419✔
771
  }
772

773
  code = blockDataEnsureCapacity(pBlock, numOfRows);
33,998,779✔
774
  TSDB_CHECK_CODE(code, line, END);
33,998,568✔
775
  pBlock->info.rows = numOfRows;
33,998,568✔
776
  int32_t colActual = blockDataGetNumOfCols(pBlock);
33,998,568✔
777

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

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

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

808
          if (isBlob == 0) {
304✔
809
            code = doSetVal(pColData, i, &colVal);
304✔
810
          } else {
811
            code = doSetBlobVal(pColData, i, &colVal, pSubmitTbData->pBlobSet);
×
812
          }
813
          TSDB_CHECK_CODE(code, line, END);
304✔
814
        }
815
        sourceIdx++;
152✔
816
        targetIdx++;
152✔
817
      } else {
818
        colDataSetNNULL(pColData, 0, numOfRows);
76✔
819
        targetIdx++;
76✔
820
      }
821
    }
822
  } else {
823
    SArray*         pRows = pSubmitTbData->aRowP;
33,996,859✔
824
    SSchemaWrapper* pWrapper = pReader->pSchemaWrapper;
33,997,904✔
825
    pTSchema = tBuildTSchema(pWrapper->pSchema, pWrapper->nCols, pWrapper->version);
33,998,523✔
826
    TSDB_CHECK_NULL(pTSchema, code, line, END, terrno);
33,998,177✔
827

828
    for (int32_t i = 0; i < numOfRows; i++) {
1,580,047,076✔
829
      SRow* pRow = taosArrayGetP(pRows, i);
1,467,299,485✔
830
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
1,463,947,592✔
831
      int32_t sourceIdx = 0;
1,466,904,741✔
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) {
147,491,277✔
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++;
147,491,277✔
844
            continue;
147,491,277✔
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:
112,747,591✔
866
  if (code != 0) {
70,140,400✔
867
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
868
  }
869
  taosMemoryFreeClear(pTSchema);
33,997,464✔
870
  return code;
33,996,028✔
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,
13,905,545✔
902
                               char* assigned, int32_t numOfRows, int32_t curRow, int32_t* lastRow) {
903
  int32_t         code = 0;
13,905,545✔
904
  SSchemaWrapper* pSW = NULL;
13,905,545✔
905
  SSDataBlock*    block = NULL;
13,910,929✔
906
  if (taosArrayGetSize(blocks) > 0) {
13,910,929✔
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));
13,913,197✔
914
  TQ_NULL_GO_TO_END(block);
13,908,040✔
915

916
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
13,908,040✔
917
  TQ_NULL_GO_TO_END(pSW);
13,909,409✔
918

919
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pReader->pSchemaWrapper, assigned, pReader->extSchema));
13,909,409✔
920
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
13,918,085✔
921
          (int32_t)taosArrayGetSize(block->pDataBlock));
922

923
  block->info.id.uid = pSubmitTbData->uid;
13,918,085✔
924
  block->info.version = pReader->msg.ver;
13,916,510✔
925
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
13,917,436✔
926
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
13,916,192✔
927
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
13,915,722✔
928
  pSW = NULL;
13,915,722✔
929

930
  taosMemoryFreeClear(block);
13,915,722✔
931

932
END:
13,918,374✔
933
  if (code != 0) {
13,916,960✔
934
    tqError("processBuildNew failed, code:%d", code);
×
935
  }
936
  tDeleteSchemaWrapper(pSW);
13,916,960✔
937
  blockDataFreeRes(block);
13,911,998✔
938
  taosMemoryFree(block);
13,912,339✔
939
  return code;
13,914,941✔
940
}
941
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
5,020✔
942
  int32_t code = 0;
5,020✔
943
  int32_t curRow = 0;
5,020✔
944
  int32_t lastRow = 0;
5,020✔
945

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

950
  SArray*   pCols = pSubmitTbData->aCol;
5,020✔
951
  SColData* pCol = taosArrayGet(pCols, 0);
5,020✔
952
  TQ_NULL_GO_TO_END(pCol);
5,020✔
953
  int32_t numOfRows = pCol->nVal;
5,020✔
954
  int32_t numOfCols = taosArrayGetSize(pCols);
5,020✔
955
  tqTrace("vgId:%d, tqProcessColData start, col num: %d, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfCols,
5,020✔
956
          numOfRows);
957
  for (int32_t i = 0; i < numOfRows; i++) {
2,815,067✔
958
    bool buildNew = false;
2,803,029✔
959

960
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
10,508,106✔
961
      int32_t k = 0;
7,689,687✔
962
      for (; k < numOfCols; k++) {
14,847,938✔
963
        pCol = taosArrayGet(pCols, k);
13,204,149✔
964
        TQ_NULL_GO_TO_END(pCol);
13,143,365✔
965
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
13,143,365✔
966
          SColVal colVal = {0};
7,462,115✔
967
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
7,626,806✔
968
          PROCESS_VAL
7,731,003✔
969
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
7,886,385✔
970
          break;
7,727,987✔
971
        }
972
      }
973
      if (k >= numOfCols) {
7,705,077✔
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) {
2,427,421✔
981
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
5,020✔
982
    }
983

984
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
2,427,421✔
985
    TQ_NULL_GO_TO_END(pBlock);
2,780,293✔
986

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

990
    int32_t targetIdx = 0;
2,780,293✔
991
    int32_t sourceIdx = 0;
2,780,293✔
992
    int32_t colActual = blockDataGetNumOfCols(pBlock);
2,780,293✔
993
    while (targetIdx < colActual && sourceIdx < numOfCols) {
10,885,537✔
994
      pCol = taosArrayGet(pCols, sourceIdx);
8,075,490✔
995
      TQ_NULL_GO_TO_END(pCol);
7,571,325✔
996
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
7,571,325✔
997
      TQ_NULL_GO_TO_END(pColData);
7,254,732✔
998
      SColVal colVal = {0};
7,254,732✔
999
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
7,459,211✔
1000
      SET_DATA
7,636,488✔
1001
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
7,413,594✔
1002
    }
1003

1004
    curRow++;
2,810,047✔
1005
  }
1006
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
12,038✔
1007
  pLastBlock->info.rows = curRow - lastRow;
5,020✔
1008
  tqTrace("vgId:%d, tqProcessColData end, col num: %d, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId,
5,020✔
1009
          numOfCols, numOfRows, (int)taosArrayGetSize(blocks));
1010
END:
1,623,753✔
1011
  if (code != TSDB_CODE_SUCCESS) {
5,020✔
1012
    tqError("vgId:%d, process col data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1013
  }
1014
  taosMemoryFree(assigned);
5,020✔
1015
  return code;
5,020✔
1016
}
1017

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

1022
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
13,898,331✔
1023
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
13,903,197✔
1024
  TQ_NULL_GO_TO_END(assigned);
13,903,768✔
1025

1026
  int32_t curRow = 0;
13,903,768✔
1027
  int32_t lastRow = 0;
13,903,768✔
1028
  SArray* pRows = pSubmitTbData->aRowP;
13,901,630✔
1029
  int32_t numOfRows = taosArrayGetSize(pRows);
13,909,281✔
1030
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
13,908,629✔
1031
  TQ_NULL_GO_TO_END(pTSchema);
13,911,404✔
1032
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
13,911,404✔
1033

1034
  for (int32_t i = 0; i < numOfRows; i++) {
283,029,039✔
1035
    bool  buildNew = false;
269,133,049✔
1036
    SRow* pRow = taosArrayGetP(pRows, i);
269,133,049✔
1037
    TQ_NULL_GO_TO_END(pRow);
268,904,028✔
1038

1039
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
1,496,921,689✔
1040
      SColVal colVal = {0};
1,227,211,973✔
1041
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
1,226,034,629✔
1042
      PROCESS_VAL
1,227,285,195✔
1043
      tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], pTSchema->numOfCols);
1,227,513,583✔
1044
    }
1045

1046
    if (buildNew) {
266,543,520✔
1047
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
13,911,841✔
1048
    }
1049

1050
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
266,541,298✔
1051
    TQ_NULL_GO_TO_END(pBlock);
269,114,544✔
1052

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

1056
    int32_t targetIdx = 0;
269,114,544✔
1057
    int32_t sourceIdx = 0;
269,114,544✔
1058
    int32_t colActual = blockDataGetNumOfCols(pBlock);
269,114,544✔
1059
    while (targetIdx < colActual && sourceIdx < pTSchema->numOfCols) {
1,494,501,763✔
1060
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
1,225,485,109✔
1061
      TQ_NULL_GO_TO_END(pColData);
1,225,630,770✔
1062
      SColVal          colVal = {0};
1,225,630,770✔
1063
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
1,224,930,809✔
1064
      SET_DATA
1,226,438,424✔
1065
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
1,226,528,523✔
1066
    }
1067

1068
    curRow++;
269,129,912✔
1069
  }
1070
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
13,895,990✔
1071
  if (pLastBlock != NULL) {
13,912,018✔
1072
    pLastBlock->info.rows = curRow - lastRow;
13,912,832✔
1073
  }
1074

1075
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
13,913,046✔
1076
          (int)taosArrayGetSize(blocks));
1077
END:
14,775,451✔
1078
  if (code != TSDB_CODE_SUCCESS) {
13,906,285✔
1079
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1080
  }
1081
  taosMemoryFreeClear(pTSchema);
13,905,208✔
1082
  taosMemoryFree(assigned);
13,904,462✔
1083
  return code;
13,906,715✔
1084
}
1085

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

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

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

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

1116
END:
708✔
1117
  if (code != 0) {
708✔
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;
708✔
1122
}
1123

1124
int32_t tqRetrieveTaosxBlock(STqReader* pReader, SMqDataRsp* pRsp, SArray* blocks, SArray* schemas,
13,955,192✔
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);
13,955,192✔
1127
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
13,955,192✔
1128
  if (pSubmitTbData == NULL) {
13,955,863✔
1129
    return terrno;
×
1130
  }
1131
  pReader->nextBlk++;
13,955,863✔
1132

1133
  if (pSubmitTbDataRet) {
13,955,096✔
1134
    *pSubmitTbDataRet = pSubmitTbData;
13,955,692✔
1135
  }
1136

1137
  if (fetchMeta == ONLY_META) {
13,955,267✔
1138
    if (pSubmitTbData->pCreateTbReq != NULL) {
440✔
1139
      if (pRsp->createTableReq == NULL) {
80✔
1140
        pRsp->createTableReq = taosArrayInit(0, POINTER_BYTES);
20✔
1141
        if (pRsp->createTableReq == NULL) {
20✔
1142
          return terrno;
×
1143
        }
1144
      }
1145
      if (taosArrayPush(pRsp->createTableReq, &pSubmitTbData->pCreateTbReq) == NULL) {
160✔
1146
        return terrno;
×
1147
      }
1148
      pSubmitTbData->pCreateTbReq = NULL;
80✔
1149
    }
1150
    return 0;
440✔
1151
  }
1152

1153
  int32_t sversion = pSubmitTbData->sver;
13,954,827✔
1154
  int64_t uid = pSubmitTbData->uid;
13,954,171✔
1155
  pReader->lastBlkUid = uid;
13,954,273✔
1156

1157
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
13,955,256✔
1158
  taosMemoryFreeClear(pReader->extSchema);
13,954,022✔
1159
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
13,955,437✔
1160
  if (pReader->pSchemaWrapper == NULL) {
13,952,574✔
1161
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
37,454✔
1162
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1163
    pReader->cachedSchemaSuid = 0;
37,454✔
1164
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
37,454✔
1165
  }
1166

1167
  if (pSubmitTbData->pCreateTbReq != NULL) {
13,909,095✔
1168
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
708✔
1169
    if (code != 0) {
708✔
1170
      return code;
×
1171
    }
1172
  } else if (rawList != NULL) {
13,912,719✔
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) {
13,913,427✔
1181
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
5,020✔
1182
  } else {
1183
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
13,903,943✔
1184
  }
1185
}
1186

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

1195
int32_t tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
113,427✔
1196
  if (pReader == NULL || tbUidList == NULL) {
113,427✔
1197
    return TSDB_CODE_SUCCESS;
×
1198
  }
1199
  if (pReader->tbIdHash) {
113,427✔
1200
    taosHashClear(pReader->tbIdHash);
1,052✔
1201
  } else {
1202
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
112,375✔
1203
    if (pReader->tbIdHash == NULL) {
112,375✔
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,782,588✔
1210
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
2,669,094✔
1211
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
2,669,120✔
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));
113,427✔
1218
  return TSDB_CODE_SUCCESS;
113,427✔
1219
}
1220

1221
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
49,394✔
1222
  if (pReader == NULL || pTableUidList == NULL) {
49,394✔
1223
    return;
×
1224
  }
1225
  if (pReader->tbIdHash == NULL) {
49,394✔
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);
49,394✔
1234
  for (int i = 0; i < numOfTables; i++) {
78,051✔
1235
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
28,657✔
1236
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
28,657✔
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) {
596✔
1258
  if (pReader == NULL || tbUidList == NULL) {
596✔
1259
    return;
×
1260
  }
1261
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
776✔
1262
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
180✔
1263
    if (pKey && taosHashRemove(pReader->tbIdHash, pKey, sizeof(int64_t)) != 0) {
180✔
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) {
62,583,289✔
1270
  if (pTq == NULL) {
62,583,289✔
1271
    return 0;  // mounted vnode may have no tq
×
1272
  }
1273
  if (tbUidList == NULL) {
62,583,289✔
1274
    return TSDB_CODE_INVALID_PARA;
×
1275
  }
1276
  void*   pIter = NULL;
62,583,289✔
1277
  int32_t vgId = TD_VID(pTq->pVnode);
62,583,289✔
1278

1279
  // update the table list for each consumer handle
1280
  taosWLockLatch(&pTq->lock);
62,584,621✔
1281
  while (1) {
188,956✔
1282
    pIter = taosHashIterate(pTq->pHandle, pIter);
62,772,545✔
1283
    if (pIter == NULL) {
62,772,611✔
1284
      break;
62,583,655✔
1285
    }
1286

1287
    STqHandle* pTqHandle = (STqHandle*)pIter;
188,956✔
1288
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
188,956✔
1289
      int32_t code = qUpdateTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList, isAdd);
49,990✔
1290
      if (code != 0) {
49,990✔
1291
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1292
        continue;
×
1293
      }
1294
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
138,966✔
1295
      if (!isAdd) {
137,914✔
1296
        int32_t sz = taosArrayGetSize(tbUidList);
46,324✔
1297
        for (int32_t i = 0; i < sz; i++) {
46,324✔
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,052✔
1307
      if (isAdd) {
1,052✔
1308
        SArray* list = NULL;
1,052✔
1309
        int     ret = qGetTableList(pTqHandle->execHandle.execTb.suid, pTq->pVnode, pTqHandle->execHandle.execTb.node,
1,052✔
1310
                                    &list, pTqHandle->execHandle.task);
1311
        if (ret == 0) {
1,052✔
1312
          ret = tqReaderSetTbUidList(pTqHandle->execHandle.pTqReader, list, NULL);
1,052✔
1313
        }                            
1314
        if (ret != TDB_CODE_SUCCESS) {
1,052✔
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,052✔
1324
      } else {
1325
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
×
1326
      }
1327
    }
1328
  }
1329
  taosWUnLockLatch(&pTq->lock);
62,583,655✔
1330
  return 0;
62,584,665✔
1331
}
1332

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

1341
static int32_t compareSVTColInfo(const void* p1, const void* p2) {
×
1342
  SVTColInfo* pCol1 = (SVTColInfo*)p1;
×
1343
  SVTColInfo* pCol2 = (SVTColInfo*)p2;
×
1344
  if (pCol1->vColId == pCol2->vColId) {
×
1345
    return 0;
×
1346
  } else if (pCol1->vColId < pCol2->vColId) {
×
1347
    return -1;
×
1348
  } else {
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;
×
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);
×
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;
×
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));
×
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;
×
1390
    int32_t    iterIn = 0;
×
1391
    void*      pxIn = tSimpleHashIterate(pPhysicalTables, NULL, &iterIn);
×
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);
×
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);
×
1405
          pAPI->metaReaderFn.initReader(&metaReader, vnode, META_READER_LOCK, &pAPI->metaFn);
×
1406
          code = pAPI->metaReaderFn.getTableEntryByUid(&metaReader, suid);
×
1407
          TSDB_CHECK_CODE(code, lino, _end);
×
1408
          pAPI->metaReaderFn.readerReleaseLock(&metaReader);
×
1409
          schema = &metaReader.me.stbEntry.schemaRow;
×
1410
          break;
×
1411
        }
1412
        case TSDB_NORMAL_TABLE: {
×
1413
          schema = &metaReader.me.ntbEntry.schemaRow;
×
1414
          break;
×
1415
        }
1416
        default: {
×
1417
          tqError("invalid table type: %d", metaReader.me.type);
×
1418
          code = TSDB_CODE_INVALID_PARA;
×
1419
          TSDB_CHECK_CODE(code, lino, _end);
×
1420
        }
1421
      }
1422

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

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);
×
1433
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
×
1434
            break;
×
1435
          }
1436
        }
1437
      }
1438

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

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

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

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

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

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

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

1480
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
108,870✔
1481

1482
  pScanInfo = &pReader->vtSourceScanInfo;
108,870✔
1483
  taosHashCleanup(pScanInfo->pPhysicalTables);
108,830✔
1484
  pScanInfo->pPhysicalTables = NULL;
108,830✔
1485
  taosLRUCacheCleanup(pScanInfo->pPhyTblSchemaCache);
108,830✔
1486
  pScanInfo->pPhyTblSchemaCache = NULL;
108,775✔
1487
  pScanInfo->nextVirtualTableIdx = -1;
108,899✔
1488
  pScanInfo->metaFetch = 0;
108,899✔
1489
  pScanInfo->cacheHit = 0;
108,899✔
1490

1491
  pVirtualTables = pScanInfo->pVirtualTables;
108,762✔
1492
  if (taosHashGetSize(pVirtualTables) == 0 || taosArrayGetSize(pReader->pColIdList) == 0) {
108,831✔
1493
    goto _end;
108,713✔
1494
  }
1495

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

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

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

1549
_end:
×
1550
  if (code != TSDB_CODE_SUCCESS) {
108,713✔
1551
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1552
  }
1553
  if (pIter != NULL) {
108,802✔
1554
    taosHashCancelIterate(pReader->tbIdHash, pIter);
×
1555
  }
1556
  if (pPhysicalTables != NULL) {
108,899✔
1557
    taosHashCleanup(pPhysicalTables);
×
1558
  }
1559
  return code;
108,802✔
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
  }
1567
}
×
1568

1569
bool tqNextVTableSourceBlockImpl(STqReader* pReader, const char* idstr) {
×
1570
  int32_t            code = TSDB_CODE_SUCCESS;
×
1571
  int32_t            lino = 0;
×
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

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

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

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

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

1613
bool tqReaderIsQueriedSourceTable(STqReader* pReader, uint64_t uid) {
×
1614
  if (pReader == NULL) {
×
1615
    return false;
×
1616
  }
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