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

taosdata / TDengine / #4831

31 Oct 2025 03:37AM UTC coverage: 58.771% (-0.02%) from 58.792%
#4831

push

travis-ci

SallyHuo-TAOS
Merge remote-tracking branch 'origin/cover/3.0' into cover/3.0

# Conflicts:
#	test/ci/run.sh

149876 of 324176 branches covered (46.23%)

Branch coverage included in aggregate %.

199031 of 269498 relevant lines covered (73.85%)

239378092.09 hits per line

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

51.89
/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) {
447,457✔
22
  if (pHandle == NULL || pHead == NULL) {
447,457!
23
    return false;
×
24
  }
25
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
447,457✔
26
    return true;
441,563✔
27
  }
28

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

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

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

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

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

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

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

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

216
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
138,196,405✔
217
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
138,196,405!
218
    return -1;
×
219
  }
220
  int32_t code = -1;
138,487,561✔
221
  int32_t vgId = TD_VID(pTq->pVnode);
138,487,561✔
222
  int64_t id = pHandle->pWalReader->readerId;
138,524,106✔
223

224
  int64_t offset = *fetchOffset;
138,517,727✔
225
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
138,553,344✔
226
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
138,523,453✔
227
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
138,503,422✔
228

229
  tqDebug("vgId:%d, start to fetch wal, index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64 ", applied:%" PRId64
138,491,673!
230
          ", 0x%" PRIx64,
231
          vgId, offset, lastVer, committedVer, appliedVer, id);
232

233
  while (offset <= appliedVer) {
146,646,830✔
234
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
132,547,440!
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,
132,545,972!
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) {
132,543,593✔
245
      code = walFetchBody(pHandle->pWalReader);
124,026,546✔
246
      goto END;
124,019,682✔
247
    } else {
248
      if (pHandle->fetchMeta != WITH_DATA) {
8,524,466✔
249
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
572,685✔
250
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
572,685✔
251
          code = walFetchBody(pHandle->pWalReader);
447,457✔
252
          if (code < 0) {
447,457!
253
            goto END;
×
254
          }
255

256
          pHead = &(pHandle->pWalReader->pHead->head);
447,457✔
257
          if (isValValidForTable(pHandle, pHead)) {
447,457✔
258
            code = 0;
444,931✔
259
            goto END;
444,931✔
260
          } else {
261
            offset++;
2,526✔
262
            code = -1;
2,526✔
263
            continue;
2,526✔
264
          }
265
        }
266
      }
267
      code = walSkipFetchBody(pHandle->pWalReader);
8,077,009✔
268
      if (code < 0) {
8,077,009!
269
        goto END;
×
270
      }
271
      offset++;
8,077,009✔
272
    }
273
    code = -1;
8,077,009✔
274
  }
275

276
END:
14,099,390✔
277
  *fetchOffset = offset;
138,564,003✔
278
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
138,560,378!
279
          ", applied:%" PRId64 ", 0x%" PRIx64,
280
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
281
  return code;
138,568,135✔
282
}
283

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

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

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

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

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

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

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

335
  return pReader;
1,198,154✔
336
}
337

338
void tqReaderClose(STqReader* pReader) {
1,210,979✔
339
  tqDebug("%s:%p", __FUNCTION__, pReader);
1,210,979✔
340
  if (pReader == NULL) return;
1,211,835✔
341

342
  // close wal reader
343
  if (pReader->pWalReader) {
1,198,154!
344
    walCloseReader(pReader->pWalReader);
1,198,154✔
345
  }
346

347
  if (pReader->pSchemaWrapper) {
1,198,154✔
348
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
566,982!
349
  }
350

351
  taosMemoryFree(pReader->extSchema);
1,198,154!
352
  if (pReader->pColIdList) {
1,198,154✔
353
    taosArrayDestroy(pReader->pColIdList);
906,971✔
354
  }
355

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

361
  taosHashCleanup(pReader->vtSourceScanInfo.pVirtualTables);
1,198,154✔
362
  taosHashCleanup(pReader->vtSourceScanInfo.pPhysicalTables);
1,198,154✔
363
  taosLRUCacheCleanup(pReader->vtSourceScanInfo.pPhyTblSchemaCache);
1,198,154✔
364
  taosMemoryFree(pReader);
1,198,154!
365
}
366

367
int32_t tqReaderSeek(STqReader* pReader, int64_t ver, const char* id) {
2,261,966✔
368
  if (pReader == NULL) {
2,261,966!
369
    return TSDB_CODE_INVALID_PARA;
×
370
  }
371
  if (walReaderSeekVer(pReader->pWalReader, ver) < 0) {
2,261,966✔
372
    return terrno;
64,317✔
373
  }
374
  tqDebug("wal reader seek to ver:%" PRId64 " %s", ver, id);
2,197,649!
375
  return 0;
2,197,649✔
376
}
377

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

384
  int64_t st = taosGetTimestampMs();
245,912,981✔
385
  while (1) {
264,959,797✔
386
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
510,872,778✔
387
    while (pReader->nextBlk < numOfBlocks) {
567,857,933✔
388
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
264,987,248!
389
              pReader->msg.ver);
390

391
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
264,987,248✔
392
      if (pSubmitTbData == NULL) {
264,949,200!
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;
105✔
396
      }
397
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
264,949,200✔
398
        pReader->nextBlk += 1;
84,735✔
399
        continue;
84,735✔
400
      }
401
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
264,851,905!
402
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
207,964,780!
403
        SSDataBlock* pRes = NULL;
207,964,780✔
404
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
207,945,735✔
405
        if (code == TSDB_CODE_SUCCESS) {
207,961,048!
406
          return true;
207,961,048✔
407
        }
408
      } else {
409
        pReader->nextBlk += 1;
56,937,744✔
410
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
56,937,744!
411
      }
412
    }
413

414
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
302,892,785✔
415
    pReader->msg.msgStr = NULL;
302,887,483✔
416

417
    int64_t elapsed = taosGetTimestampMs() - st;
302,890,140✔
418
    if (elapsed > 1000 || elapsed < 0) {
302,890,140✔
419
      return false;
975✔
420
    }
421

422
    // try next message in wal file
423
    if (walNextValidMsg(pWalReader, false) < 0) {
302,889,165✔
424
      return false;
37,916,854✔
425
    }
426

427
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
264,939,206✔
428
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
264,956,411✔
429
    int64_t ver = pWalReader->pHead->head.version;
264,962,152✔
430
    SDecoder decoder = {0};
264,956,402✔
431
    if (tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL, &decoder) != 0) {
264,952,867!
432
      tDecoderClear(&decoder);
×
433
      return false;
×
434
    }
435
    tDecoderClear(&decoder);
264,902,047✔
436
    pReader->nextBlk = 0;
264,947,273✔
437
  }
438
}
439

440
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList, SDecoder* decoder) {
388,952,789✔
441
  if (pReader == NULL) {
388,952,789!
442
    return TSDB_CODE_INVALID_PARA;
×
443
  }
444
  pReader->msg.msgStr = msgStr;
388,952,789✔
445
  pReader->msg.msgLen = msgLen;
388,985,097✔
446
  pReader->msg.ver = ver;
388,975,583✔
447

448
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
388,991,308!
449

450
  tDecoderInit(decoder, pReader->msg.msgStr, pReader->msg.msgLen);
388,991,308✔
451
  int32_t code = tDecodeSubmitReq(decoder, &pReader->submit, rawList);
388,972,005✔
452

453
  if (code != 0) {
388,930,463!
454
    tqError("DecodeSSubmitReq2 error, msgLen:%d, ver:%" PRId64, msgLen, ver);
×
455
  }
456

457
  return code;
388,935,729✔
458
}
459

460
void tqReaderClearSubmitMsg(STqReader* pReader) {
247,789,935✔
461
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
247,789,935✔
462
  pReader->nextBlk = 0;
247,784,309✔
463
  pReader->msg.msgStr = NULL;
247,800,864✔
464
}
247,882,797✔
465

466
SWalReader* tqGetWalReader(STqReader* pReader) {
288,067,598✔
467
  if (pReader == NULL) {
288,067,598!
468
    return NULL;
×
469
  }
470
  return pReader->pWalReader;
288,067,598✔
471
}
472

473
SSDataBlock* tqGetResultBlock(STqReader* pReader) {
245,902,337✔
474
  if (pReader == NULL) {
245,902,337!
475
    return NULL;
×
476
  }
477
  return pReader->pResBlock;
245,902,337✔
478
}
479

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

487
bool tqNextBlockImpl(STqReader* pReader, const char* idstr) {
61,691,703✔
488
  int32_t code = false;
61,691,703✔
489
  int32_t lino = 0;
61,691,703✔
490
  int64_t uid = 0;
61,691,703✔
491
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
61,691,703!
492
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
61,691,703!
493
  TSDB_CHECK_NULL(pReader->tbIdHash, code, lino, END, true);
61,693,481!
494

495
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
61,694,366✔
496
  while (pReader->nextBlk < blockSz) {
64,807,518✔
497
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
32,409,129✔
498
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
32,409,129!
499
    uid = pSubmitTbData->uid;
32,409,129✔
500
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
32,409,129✔
501
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
32,408,244✔
502

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

508
  tqReaderClearSubmitMsg(pReader);
32,398,389✔
509
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
32,398,389!
510

511
END:
32,398,389✔
512
  tqTrace("%s:%d return:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
61,695,251!
513
  return code;
61,692,580✔
514
}
515

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

521
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
183,126,092!
522
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
183,126,092!
523
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
183,211,156!
524

525
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
183,211,156✔
526
  while (pReader->nextBlk < blockSz) {
183,235,967✔
527
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
91,672,881✔
528
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
91,666,161!
529
    uid = pSubmitTbData->uid;
91,666,161✔
530
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
91,674,561✔
531
    TSDB_CHECK_NULL(ret, code, lino, END, true);
91,671,191!
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);
91,587,739✔
536
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
91,604,569!
537

538
END:
91,604,569✔
539
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
183,275,760!
540
  return code;
183,011,590✔
541
}
542

543
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask,
120,235,019✔
544
                    SExtSchema* extSrc) {
545
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
120,235,019!
546
    return TSDB_CODE_INVALID_PARA;
×
547
  }
548
  int32_t code = 0;
120,341,716✔
549

550
  int32_t cnt = 0;
120,341,716✔
551
  for (int32_t i = 0; i < pSrc->nCols; i++) {
660,962,881✔
552
    cnt += mask[i];
540,548,793✔
553
  }
554

555
  pDst->nCols = cnt;
120,352,697✔
556
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
120,372,772!
557
  if (pDst->pSchema == NULL) {
120,260,116!
558
    return TAOS_GET_TERRNO(terrno);
×
559
  }
560

561
  int32_t j = 0;
120,261,068✔
562
  for (int32_t i = 0; i < pSrc->nCols; i++) {
661,127,698✔
563
    if (mask[i]) {
540,601,208!
564
      pDst->pSchema[j++] = pSrc->pSchema[i];
540,720,549✔
565
      SColumnInfoData colInfo =
540,828,128✔
566
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
540,806,318✔
567
      if (extSrc != NULL) {
540,678,601✔
568
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
175,119✔
569
      }
570
      code = blockDataAppendColInfo(pBlock, &colInfo);
540,678,601✔
571
      if (code != 0) {
540,833,616!
572
        return code;
×
573
      }
574
    }
575
  }
576
  return 0;
120,402,160✔
577
}
578

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

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

596
  int32_t numOfCols = taosArrayGetSize(pColIdList);
410,330✔
597

598
  if (numOfCols == 0) {  // all columns are required
410,330!
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) {
410,330✔
614
      numOfCols = pSchema->nCols;
1,665✔
615
    }
616

617
    int32_t i = 0;
410,330✔
618
    int32_t j = 0;
410,330✔
619
    while (i < pSchema->nCols && j < numOfCols) {
3,592,220✔
620
      SSchema* pColSchema = &pSchema->pSchema[i];
3,181,890✔
621
      col_id_t colIdSchema = pColSchema->colId;
3,183,202✔
622

623
      col_id_t* pColIdNeed = (col_id_t*)taosArrayGet(pColIdList, j);
3,183,202✔
624
      if (pColIdNeed == NULL) {
3,182,347!
625
        break;
×
626
      }
627
      if (colIdSchema < *pColIdNeed) {
3,182,347✔
628
        i++;
2,505✔
629
      } else if (colIdSchema > *pColIdNeed) {
3,179,842!
630
        j++;
×
631
      } else {
632
        SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
3,178,073✔
633
        if (IS_DECIMAL_TYPE(pColSchema->type) && pReader->extSchema != NULL) {
3,176,761!
634
          decimalFromTypeMod(pReader->extSchema[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
173,172✔
635
        }
636
        int32_t code = blockDataAppendColInfo(pBlock, &colInfo);
3,176,761✔
637
        if (code != TSDB_CODE_SUCCESS) {
3,179,385!
638
          return -1;
×
639
        }
640
        i++;
3,179,385✔
641
        j++;
3,179,385✔
642
      }
643
    }
644
  }
645

646
  return TSDB_CODE_SUCCESS;
410,330✔
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)) {
2,147,483,647!
695
      char val[65535 + 2] = {0};
2,147,483,647✔
696
      if (pColVal->value.pData != NULL) {
2,147,483,647!
697
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
2,147,483,647!
698
      }
699
      varDataSetLen(val, pColVal->value.nData);
2,147,483,647✔
700
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
2,147,483,647✔
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) {
207,911,641✔
713
  if (pReader == NULL || pRes == NULL) {
207,911,641!
714
    return TSDB_CODE_INVALID_PARA;
×
715
  }
716
  tqDebug("tq reader retrieve data block %p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
207,977,305✔
717
  int32_t        code = 0;
207,987,021✔
718
  int32_t        line = 0;
207,987,021✔
719
  STSchema*      pTSchema = NULL;
207,987,021✔
720
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk++);
207,987,021✔
721
  TSDB_CHECK_NULL(pSubmitTbData, code, line, END, terrno);
207,998,842!
722
  SSDataBlock* pBlock = pReader->pResBlock;
207,998,842✔
723
  *pRes = pBlock;
207,998,842✔
724

725
  blockDataCleanup(pBlock);
207,997,951✔
726

727
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
207,996,127✔
728
  int32_t sversion = pSubmitTbData->sver;
207,996,127✔
729
  int64_t suid = pSubmitTbData->suid;
207,998,842✔
730
  int64_t uid = pSubmitTbData->uid;
208,000,652✔
731
  pReader->lastTs = pSubmitTbData->ctimeMs;
207,999,747✔
732

733
  pBlock->info.id.uid = uid;
208,000,652✔
734
  pBlock->info.version = pReader->msg.ver;
207,997,937✔
735

736
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
207,998,842✔
737
      (pReader->cachedSchemaVer != sversion)) {
207,585,652✔
738
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
415,760✔
739
    taosMemoryFree(pReader->extSchema);
410,330!
740
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
410,330✔
741
    if (pReader->pSchemaWrapper == NULL) {
410,330!
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;
409,475✔
750
    pReader->cachedSchemaSuid = suid;
410,330✔
751
    pReader->cachedSchemaVer = sversion;
410,330✔
752

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

764
  int32_t numOfRows = 0;
207,997,937✔
765
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
207,997,937✔
766
    SColData* pCol = taosArrayGet(pSubmitTbData->aCol, 0);
3,340✔
767
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
3,340!
768
    numOfRows = pCol->nVal;
3,340✔
769
  } else {
770
    numOfRows = taosArrayGetSize(pSubmitTbData->aRowP);
207,989,223✔
771
  }
772

773
  code = blockDataEnsureCapacity(pBlock, numOfRows);
207,996,137✔
774
  TSDB_CHECK_CODE(code, line, END);
207,996,141!
775
  pBlock->info.rows = numOfRows;
207,996,141✔
776
  int32_t colActual = blockDataGetNumOfCols(pBlock);
207,994,331✔
777

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

794
      uint8_t isBlob = IS_STR_DATA_BLOB(pColData->info.type) ? 1 : 0;
8,350!
795

796
      SColData* pCol = taosArrayGet(pCols, sourceIdx);
8,350✔
797
      TSDB_CHECK_NULL(pCol, code, line, END, terrno);
8,350!
798
      SColVal colVal = {0};
8,350✔
799
      tqTrace("lostdata colActual:%d, sourceIdx:%d, targetIdx:%d, numOfCols:%d, source cid:%d, dst cid:%d", colActual,
8,350!
800
              sourceIdx, targetIdx, numOfCols, pCol->cid, pColData->info.colId);
801
      if (pCol->cid < pColData->info.colId) {
8,350✔
802
        sourceIdx++;
3,340✔
803
      } else if (pCol->cid == pColData->info.colId) {
5,010✔
804
        for (int32_t i = 0; i < pCol->nVal; i++) {
10,020✔
805
          code = tColDataGetValue(pCol, i, &colVal);
6,680✔
806
          TSDB_CHECK_CODE(code, line, END);
6,680!
807

808
          if (isBlob == 0) {
6,680!
809
            code = doSetVal(pColData, i, &colVal);
6,680✔
810
          } else {
811
            code = doSetBlobVal(pColData, i, &colVal, pSubmitTbData->pBlobSet);
×
812
          }
813
          TSDB_CHECK_CODE(code, line, END);
6,680!
814
        }
815
        sourceIdx++;
3,340✔
816
        targetIdx++;
3,340✔
817
      } else {
818
        colDataSetNNULL(pColData, 0, numOfRows);
1,670!
819
        targetIdx++;
1,670✔
820
      }
821
    }
822
  } else {
823
    SArray*         pRows = pSubmitTbData->aRowP;
207,991,896✔
824
    SSchemaWrapper* pWrapper = pReader->pSchemaWrapper;
207,990,086✔
825
    pTSchema = tBuildTSchema(pWrapper->pSchema, pWrapper->nCols, pWrapper->version);
207,988,262✔
826
    TSDB_CHECK_NULL(pTSchema, code, line, END, terrno);
207,987,491!
827

828
    for (int32_t i = 0; i < numOfRows; i++) {
2,147,483,647✔
829
      SRow* pRow = taosArrayGetP(pRows, i);
2,147,483,647✔
830
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
2,147,483,647✔
831
      int32_t sourceIdx = 0;
2,147,483,647✔
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) {
×
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++;
×
844
            continue;
×
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:
547,848,567✔
866
  if (code != 0) {
394,863,776!
867
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
868
  }
869
  taosMemoryFreeClear(pTSchema);
207,960,283!
870
  return code;
207,967,571✔
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,
120,174,762✔
902
                               char* assigned, int32_t numOfRows, int32_t curRow, int32_t* lastRow) {
903
  int32_t         code = 0;
120,174,762✔
904
  SSchemaWrapper* pSW = NULL;
120,174,762✔
905
  SSDataBlock*    block = NULL;
120,277,975✔
906
  if (taosArrayGetSize(blocks) > 0) {
120,277,975!
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));
120,328,421!
914
  TQ_NULL_GO_TO_END(block);
120,258,044!
915

916
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
120,258,044!
917
  TQ_NULL_GO_TO_END(pSW);
120,266,715!
918

919
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pReader->pSchemaWrapper, assigned, pReader->extSchema));
120,266,715!
920
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
120,392,319!
921
          (int32_t)taosArrayGetSize(block->pDataBlock));
922

923
  block->info.id.uid = pSubmitTbData->uid;
120,392,319✔
924
  block->info.version = pReader->msg.ver;
120,351,263✔
925
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
120,362,737!
926
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
120,365,139!
927
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
120,353,820!
928
  pSW = NULL;
120,353,820✔
929

930
  taosMemoryFreeClear(block);
120,353,820!
931

932
END:
120,329,442✔
933
  if (code != 0) {
120,314,902!
934
    tqError("processBuildNew failed, code:%d", code);
×
935
  }
936
  tDeleteSchemaWrapper(pSW);
120,314,902!
937
  blockDataFreeRes(block);
120,249,866✔
938
  taosMemoryFree(block);
120,276,633!
939
  return code;
120,270,295✔
940
}
941
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
168,042✔
942
  int32_t code = 0;
168,042✔
943
  int32_t curRow = 0;
168,042✔
944
  int32_t lastRow = 0;
168,042✔
945

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

950
  SArray*   pCols = pSubmitTbData->aCol;
168,042✔
951
  SColData* pCol = taosArrayGet(pCols, 0);
168,042✔
952
  TQ_NULL_GO_TO_END(pCol);
168,042!
953
  int32_t numOfRows = pCol->nVal;
168,042✔
954
  int32_t numOfCols = taosArrayGetSize(pCols);
168,042✔
955
  tqTrace("vgId:%d, tqProcessColData start, col num: %d, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfCols,
168,042!
956
          numOfRows);
957
  for (int32_t i = 0; i < numOfRows; i++) {
80,856,114✔
958
    bool buildNew = false;
79,781,397✔
959

960
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
302,259,435✔
961
      int32_t k = 0;
220,086,363✔
962
      for (; k < numOfCols; k++) {
426,520,770✔
963
        pCol = taosArrayGet(pCols, k);
374,172,045✔
964
        TQ_NULL_GO_TO_END(pCol);
364,606,995!
965
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
364,606,995✔
966
          SColVal colVal = {0};
207,377,238✔
967
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
211,461,813!
968
          PROCESS_VAL
222,581,988!
969
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
223,356,663!
970
          break;
219,376,038✔
971
        }
972
      }
973
      if (k >= numOfCols) {
222,478,038!
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) {
48,388,497✔
981
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
168,042!
982
    }
983

984
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
48,388,497✔
985
    TQ_NULL_GO_TO_END(pBlock);
79,429,947!
986

987
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
79,429,947!
988
            (int32_t)taosArrayGetSize(blocks));
989

990
    int32_t targetIdx = 0;
79,429,947✔
991
    int32_t sourceIdx = 0;
79,429,947✔
992
    int32_t colActual = blockDataGetNumOfCols(pBlock);
79,429,947✔
993
    while (targetIdx < colActual && sourceIdx < numOfCols) {
314,023,200!
994
      pCol = taosArrayGet(pCols, sourceIdx);
233,335,128✔
995
      TQ_NULL_GO_TO_END(pCol);
222,585,378!
996
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
222,585,378✔
997
      TQ_NULL_GO_TO_END(pColData);
211,499,853!
998
      SColVal colVal = {0};
211,499,853✔
999
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
204,559,128!
1000
      SET_DATA
223,956,528!
1001
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
236,299,353!
1002
    }
1003

1004
    curRow++;
80,688,072✔
1005
  }
1006
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
1,074,717✔
1007
  pLastBlock->info.rows = curRow - lastRow;
168,042✔
1008
  tqTrace("vgId:%d, tqProcessColData end, col num: %d, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId,
168,042!
1009
          numOfCols, numOfRows, (int)taosArrayGetSize(blocks));
1010
END:
52,854,192✔
1011
  if (code != TSDB_CODE_SUCCESS) {
168,042!
1012
    tqError("vgId:%d, process col data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1013
  }
1014
  taosMemoryFree(assigned);
168,042!
1015
  return code;
168,042✔
1016
}
1017

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

1022
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
119,983,162✔
1023
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
120,137,977!
1024
  TQ_NULL_GO_TO_END(assigned);
120,109,383!
1025

1026
  int32_t curRow = 0;
120,109,383✔
1027
  int32_t lastRow = 0;
120,109,383✔
1028
  SArray* pRows = pSubmitTbData->aRowP;
120,056,043✔
1029
  int32_t numOfRows = taosArrayGetSize(pRows);
120,189,574✔
1030
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
120,155,113✔
1031
  TQ_NULL_GO_TO_END(pTSchema);
120,191,582!
1032
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
120,191,582!
1033

1034
  for (int32_t i = 0; i < numOfRows; i++) {
2,147,483,647✔
1035
    bool  buildNew = false;
2,147,483,647✔
1036
    SRow* pRow = taosArrayGetP(pRows, i);
2,147,483,647✔
1037
    TQ_NULL_GO_TO_END(pRow);
2,147,483,647!
1038

1039
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
2,147,483,647✔
1040
      SColVal colVal = {0};
2,147,483,647✔
1041
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
2,147,483,647!
1042
      PROCESS_VAL
2,147,483,647!
1043
      tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], pTSchema->numOfCols);
2,147,483,647!
1044
    }
1045

1046
    if (buildNew) {
2,147,483,647✔
1047
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
120,226,704!
1048
    }
1049

1050
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
2,147,483,647✔
1051
    TQ_NULL_GO_TO_END(pBlock);
2,147,483,647!
1052

1053
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
2,147,483,647!
1054
            (int32_t)taosArrayGetSize(blocks));
1055

1056
    int32_t targetIdx = 0;
2,147,483,647✔
1057
    int32_t sourceIdx = 0;
2,147,483,647✔
1058
    int32_t colActual = blockDataGetNumOfCols(pBlock);
2,147,483,647✔
1059
    while (targetIdx < colActual && sourceIdx < pTSchema->numOfCols) {
2,147,483,647!
1060
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
2,147,483,647✔
1061
      TQ_NULL_GO_TO_END(pColData);
2,147,483,647!
1062
      SColVal          colVal = {0};
2,147,483,647✔
1063
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
2,147,483,647!
1064
      SET_DATA
2,147,483,647!
1065
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
2,147,483,647!
1066
    }
1067

1068
    curRow++;
2,147,483,647✔
1069
  }
1070
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
120,823,833✔
1071
  pLastBlock->info.rows = curRow - lastRow;
120,224,492✔
1072

1073
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
120,219,334!
1074
          (int)taosArrayGetSize(blocks));
1075
END:
128,657,976✔
1076
  if (code != TSDB_CODE_SUCCESS) {
120,104,599!
1077
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1078
  }
1079
  taosMemoryFreeClear(pTSchema);
120,056,121!
1080
  taosMemoryFree(assigned);
120,168,188!
1081
  return code;
120,070,841✔
1082
}
1083

1084
static int32_t buildCreateTbInfo(SMqDataRsp* pRsp, SVCreateTbReq* pCreateTbReq) {
25,165✔
1085
  int32_t code = 0;
25,165✔
1086
  int32_t lino = 0;
25,165✔
1087
  void*   createReq = NULL;
25,165✔
1088
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
25,165!
1089
  TSDB_CHECK_NULL(pCreateTbReq, code, lino, END, TSDB_CODE_INVALID_PARA);
25,165!
1090

1091
  if (pRsp->createTableNum == 0) {
25,165✔
1092
    pRsp->createTableLen = taosArrayInit(0, sizeof(int32_t));
13,867✔
1093
    TSDB_CHECK_NULL(pRsp->createTableLen, code, lino, END, terrno);
13,867!
1094
    pRsp->createTableReq = taosArrayInit(0, sizeof(void*));
13,867✔
1095
    TSDB_CHECK_NULL(pRsp->createTableReq, code, lino, END, terrno);
13,867!
1096
  }
1097

1098
  uint32_t len = 0;
25,165✔
1099
  tEncodeSize(tEncodeSVCreateTbReq, pCreateTbReq, len, code);
25,165!
1100
  TSDB_CHECK_CODE(code, lino, END);
25,165!
1101
  createReq = taosMemoryCalloc(1, len);
25,165!
1102
  TSDB_CHECK_NULL(createReq, code, lino, END, terrno);
25,165!
1103

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

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

1122
int32_t tqRetrieveTaosxBlock(STqReader* pReader, SMqDataRsp* pRsp, SArray* blocks, SArray* schemas,
120,951,783✔
1123
                             SSubmitTbData** pSubmitTbDataRet, SArray* rawList, int8_t fetchMeta) {
1124
  tqTrace("tq reader retrieve data block msg pointer:%p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
120,951,783!
1125
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
120,951,783✔
1126
  if (pSubmitTbData == NULL) {
120,965,617!
1127
    return terrno;
×
1128
  }
1129
  pReader->nextBlk++;
120,965,617✔
1130

1131
  if (pSubmitTbDataRet) {
120,941,085✔
1132
    *pSubmitTbDataRet = pSubmitTbData;
120,968,775✔
1133
  }
1134

1135
  if (fetchMeta == ONLY_META) {
120,942,765✔
1136
    if (pSubmitTbData->pCreateTbReq != NULL) {
17,754✔
1137
      if (pRsp->createTableReq == NULL) {
3,228✔
1138
        pRsp->createTableReq = taosArrayInit(0, POINTER_BYTES);
807✔
1139
        if (pRsp->createTableReq == NULL) {
807!
1140
          return terrno;
×
1141
        }
1142
      }
1143
      if (taosArrayPush(pRsp->createTableReq, &pSubmitTbData->pCreateTbReq) == NULL) {
6,456!
1144
        return terrno;
×
1145
      }
1146
      pSubmitTbData->pCreateTbReq = NULL;
3,228✔
1147
    }
1148
    return 0;
17,754✔
1149
  }
1150

1151
  int32_t sversion = pSubmitTbData->sver;
120,925,011✔
1152
  int64_t uid = pSubmitTbData->uid;
120,937,016✔
1153
  pReader->lastBlkUid = uid;
120,942,056✔
1154

1155
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
120,946,210✔
1156
  taosMemoryFreeClear(pReader->extSchema);
120,932,859!
1157
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
120,910,446✔
1158
  if (pReader->pSchemaWrapper == NULL) {
120,891,238✔
1159
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
553,437!
1160
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1161
    pReader->cachedSchemaSuid = 0;
553,437✔
1162
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
553,437✔
1163
  }
1164

1165
  if (pSubmitTbData->pCreateTbReq != NULL) {
120,294,807✔
1166
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
25,165✔
1167
    if (code != 0) {
25,165!
1168
      return code;
×
1169
    }
1170
  } else if (rawList != NULL) {
120,313,398!
1171
    if (taosArrayPush(schemas, &pReader->pSchemaWrapper) == NULL) {
×
1172
      return terrno;
×
1173
    }
1174
    pReader->pSchemaWrapper = NULL;
×
1175
    return 0;
×
1176
  }
1177

1178
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
120,338,563✔
1179
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
168,042✔
1180
  } else {
1181
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
120,056,939✔
1182
  }
1183
}
1184

1185
int32_t tqReaderSetColIdList(STqReader* pReader, SArray* pColIdList, const char* id) {
906,070✔
1186
  if (pReader == NULL) {
906,070!
1187
    return TSDB_CODE_SUCCESS;
×
1188
  }
1189
  pReader->pColIdList = pColIdList;
906,070✔
1190
  return tqCollectPhysicalTables(pReader, id);
906,971✔
1191
}
1192

1193
int32_t tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
970,365✔
1194
  if (pReader == NULL || tbUidList == NULL) {
970,365!
1195
    return TSDB_CODE_SUCCESS;
×
1196
  }
1197
  if (pReader->tbIdHash) {
971,266✔
1198
    taosHashClear(pReader->tbIdHash);
12,230✔
1199
  } else {
1200
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
958,135✔
1201
    if (pReader->tbIdHash == NULL) {
959,916!
1202
      tqError("s-task:%s failed to init hash table", id);
×
1203
      return terrno;
×
1204
    }
1205
  }
1206

1207
  for (int i = 0; i < taosArrayGetSize(tbUidList); i++) {
28,733,227✔
1208
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
27,764,337✔
1209
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
27,750,168!
1210
      tqError("s-task:%s failed to add table uid:%" PRId64 " to hash", id, *pKey);
×
1211
      continue;
×
1212
    }
1213
  }
1214

1215
  tqDebug("s-task:%s %d tables are set to be queried target table", id, (int32_t)taosArrayGetSize(tbUidList));
973,005✔
1216
  return TSDB_CODE_SUCCESS;
972,146✔
1217
}
1218

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

1231
  int32_t numOfTables = taosArrayGetSize(pTableUidList);
1,031,646✔
1232
  for (int i = 0; i < numOfTables; i++) {
1,637,877✔
1233
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
606,231✔
1234
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
606,231!
1235
      tqError("failed to add table uid:%" PRId64 " to hash", *pKey);
×
1236
      continue;
×
1237
    }
1238
  }
1239
}
1240

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

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

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

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

1277
  // update the table list for each consumer handle
1278
  taosWLockLatch(&pTq->lock);
145,434,737✔
1279
  while (1) {
3,707,813✔
1280
    pIter = taosHashIterate(pTq->pHandle, pIter);
149,141,195✔
1281
    if (pIter == NULL) {
149,139,840✔
1282
      break;
145,432,027✔
1283
    }
1284

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

1319
          return ret;
×
1320
        }
1321
        taosArrayDestroy(list);
12,230✔
1322
      } else {
1323
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
×
1324
      }
1325
    }
1326
  }
1327
  taosWUnLockLatch(&pTq->lock);
145,432,027✔
1328

1329
  // update the table list handle for each stream scanner/wal reader
1330
/* STREAMTODO
1331
  streamMetaWLock(pTq->pStreamMeta);
1332
  while (1) {
1333
    pIter = taosHashIterate(pTq->pStreamMeta->pTasksMap, pIter);
1334
    if (pIter == NULL) {
1335
      break;
1336
    }
1337

1338
    int64_t      refId = *(int64_t*)pIter;
1339
    SStreamTask* pTask = taosAcquireRef(streamTaskRefPool, refId);
1340
    if (pTask != NULL) {
1341
      int32_t taskId = pTask->id.taskId;
1342

1343
      if ((pTask->info.taskLevel == TASK_LEVEL__SOURCE) && (pTask->exec.pExecutor != NULL)) {
1344
        int32_t code = qUpdateTableListForStreamScanner(pTask->exec.pExecutor, tbUidList, isAdd);
1345
        if (code != 0) {
1346
          tqError("vgId:%d, s-task:0x%x update qualified table error for stream task", vgId, taskId);
1347
        }
1348
      }
1349
      int32_t ret = taosReleaseRef(streamTaskRefPool, refId);
1350
      if (ret) {
1351
        tqError("vgId:%d release task refId failed, refId:%" PRId64, vgId, refId);
1352
      }
1353
    }
1354
  }
1355

1356
  streamMetaWUnLock(pTq->pStreamMeta);
1357
*/  
1358
  return 0;
145,429,984✔
1359
}
1360

1361
static void destroySourceScanTables(void* ptr) {
×
1362
  SArray** pTables = ptr;
×
1363
  if (pTables && *pTables) {
×
1364
    taosArrayDestroy(*pTables);
×
1365
    *pTables = NULL;
×
1366
  }
1367
}
×
1368

1369
static int32_t compareSVTColInfo(const void* p1, const void* p2) {
×
1370
  SVTColInfo* pCol1 = (SVTColInfo*)p1;
×
1371
  SVTColInfo* pCol2 = (SVTColInfo*)p2;
×
1372
  if (pCol1->vColId == pCol2->vColId) {
×
1373
    return 0;
×
1374
  } else if (pCol1->vColId < pCol2->vColId) {
×
1375
    return -1;
×
1376
  } else {
1377
    return 1;
×
1378
  }
1379
}
1380

1381
int32_t tqReaderSetVtableInfo(STqReader* pReader, void* vnode, void* ptr, SSHashObj* pVtableInfos,
×
1382
                              SSDataBlock** ppResBlock, const char* idstr) {
1383
  int32_t            code = TSDB_CODE_SUCCESS;
×
1384
  int32_t            lino = 0;
×
1385
  SStorageAPI*       pAPI = ptr;
×
1386
  SVTSourceScanInfo* pScanInfo = NULL;
×
1387
  SHashObj*          pVirtualTables = NULL;
×
1388
  SMetaReader        metaReader = {0};
×
1389
  SVTColInfo         colInfo = {0};
×
1390
  SSchemaWrapper*    schema = NULL;
×
1391

1392
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1393
  TSDB_CHECK_NULL(vnode, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1394
  TSDB_CHECK_NULL(pAPI, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1395

1396
  pScanInfo = &pReader->vtSourceScanInfo;
×
1397
  taosHashCleanup(pScanInfo->pVirtualTables);
×
1398
  pScanInfo->pVirtualTables = NULL;
×
1399

1400
  if (tSimpleHashGetSize(pVtableInfos) == 0) {
×
1401
    goto _end;
×
1402
  }
1403

1404
  pVirtualTables = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
1405
  TSDB_CHECK_NULL(pVirtualTables, code, lino, _end, terrno);
×
1406
  taosHashSetFreeFp(pVirtualTables, destroySourceScanTables);
×
1407

1408
  int32_t iter = 0;
×
1409
  void*   px = tSimpleHashIterate(pVtableInfos, NULL, &iter);
×
1410
  while (px != NULL) {
×
1411
    int64_t vTbUid = *(int64_t*)tSimpleHashGetKey(px, NULL);
×
1412
    SArray* pColInfos = taosArrayInit(8, sizeof(SVTColInfo));
×
1413
    TSDB_CHECK_NULL(pColInfos, code, lino, _end, terrno);
×
1414
    code = taosHashPut(pVirtualTables, &vTbUid, sizeof(int64_t), &pColInfos, POINTER_BYTES);
×
1415
    TSDB_CHECK_CODE(code, lino, _end);
×
1416

1417
    SSHashObj* pPhysicalTables = *(SSHashObj**)px;
×
1418
    int32_t    iterIn = 0;
×
1419
    void*      pxIn = tSimpleHashIterate(pPhysicalTables, NULL, &iterIn);
×
1420
    while (pxIn != NULL) {
×
1421
      char* physicalTableName = tSimpleHashGetKey(pxIn, NULL);
×
1422
      pAPI->metaReaderFn.clearReader(&metaReader);
×
1423
      pAPI->metaReaderFn.initReader(&metaReader, vnode, META_READER_LOCK, &pAPI->metaFn);
×
1424
      code = pAPI->metaReaderFn.getTableEntryByName(&metaReader, physicalTableName);
×
1425
      TSDB_CHECK_CODE(code, lino, _end);
×
1426
      pAPI->metaReaderFn.readerReleaseLock(&metaReader);
×
1427
      colInfo.pTbUid = metaReader.me.uid;
×
1428

1429
      switch (metaReader.me.type) {
×
1430
        case TSDB_CHILD_TABLE: {
×
1431
          int64_t suid = metaReader.me.ctbEntry.suid;
×
1432
          pAPI->metaReaderFn.clearReader(&metaReader);
×
1433
          pAPI->metaReaderFn.initReader(&metaReader, vnode, META_READER_LOCK, &pAPI->metaFn);
×
1434
          code = pAPI->metaReaderFn.getTableEntryByUid(&metaReader, suid);
×
1435
          TSDB_CHECK_CODE(code, lino, _end);
×
1436
          pAPI->metaReaderFn.readerReleaseLock(&metaReader);
×
1437
          schema = &metaReader.me.stbEntry.schemaRow;
×
1438
          break;
×
1439
        }
1440
        case TSDB_NORMAL_TABLE: {
×
1441
          schema = &metaReader.me.ntbEntry.schemaRow;
×
1442
          break;
×
1443
        }
1444
        default: {
×
1445
          tqError("invalid table type: %d", metaReader.me.type);
×
1446
          code = TSDB_CODE_INVALID_PARA;
×
1447
          TSDB_CHECK_CODE(code, lino, _end);
×
1448
        }
1449
      }
1450

1451
      SArray* pCols = *(SArray**)pxIn;
×
1452
      int32_t ncols = taosArrayGetSize(pCols);
×
1453
      for (int32_t i = 0; i < ncols; ++i) {
×
1454
        SColIdName* pCol = taosArrayGet(pCols, i);
×
1455
        colInfo.vColId = pCol->colId;
×
1456

1457
        for (int32_t j = 0; j < schema->nCols; ++j) {
×
1458
          if (strncmp(pCol->colName, schema->pSchema[j].name, strlen(schema->pSchema[j].name)) == 0) {
×
1459
            colInfo.pColId = schema->pSchema[j].colId;
×
1460
            void* px = taosArrayPush(pColInfos, &colInfo);
×
1461
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
×
1462
            break;
×
1463
          }
1464
        }
1465
      }
1466

1467
      taosArraySort(pColInfos, compareSVTColInfo);
×
1468
      pxIn = tSimpleHashIterate(pPhysicalTables, pxIn, &iterIn);
×
1469
    }
1470

1471
    px = tSimpleHashIterate(pVtableInfos, px, &iter);
×
1472
  }
1473

1474
  pScanInfo->pVirtualTables = pVirtualTables;
×
1475
  pVirtualTables = NULL;
×
1476

1477
  // set the result data block
1478
  if (pReader->pResBlock) {
×
1479
    blockDataDestroy(pReader->pResBlock);
×
1480
  }
1481
  pReader->pResBlock = *ppResBlock;
×
1482
  *ppResBlock = NULL;
×
1483

1484
  // update reader callback for vtable source scan
1485
  pAPI->tqReaderFn.tqNextBlockImpl = tqNextVTableSourceBlockImpl;
×
1486
  pAPI->tqReaderFn.tqReaderIsQueriedTable = tqReaderIsQueriedSourceTable;
×
1487

1488
_end:
×
1489
  if (code != TSDB_CODE_SUCCESS) {
×
1490
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1491
  }
1492
  pAPI->metaReaderFn.clearReader(&metaReader);
×
1493
  if (pVirtualTables != NULL) {
×
1494
    taosHashCleanup(pVirtualTables);
×
1495
  }
1496
  return code;
×
1497
}
1498

1499
static int32_t tqCollectPhysicalTables(STqReader* pReader, const char* idstr) {
905,247✔
1500
  int32_t            code = TSDB_CODE_SUCCESS;
905,247✔
1501
  int32_t            lino = 0;
905,247✔
1502
  SVTSourceScanInfo* pScanInfo = NULL;
905,247✔
1503
  SHashObj*          pVirtualTables = NULL;
905,247✔
1504
  SHashObj*          pPhysicalTables = NULL;
905,247✔
1505
  void*              pIter = NULL;
905,247✔
1506
  void*              px = NULL;
905,247✔
1507

1508
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
905,247!
1509

1510
  pScanInfo = &pReader->vtSourceScanInfo;
905,247✔
1511
  taosHashCleanup(pScanInfo->pPhysicalTables);
906,971✔
1512
  pScanInfo->pPhysicalTables = NULL;
906,091✔
1513
  taosLRUCacheCleanup(pScanInfo->pPhyTblSchemaCache);
906,971✔
1514
  pScanInfo->pPhyTblSchemaCache = NULL;
906,070✔
1515
  pScanInfo->nextVirtualTableIdx = -1;
906,070✔
1516
  pScanInfo->metaFetch = 0;
905,247✔
1517
  pScanInfo->cacheHit = 0;
905,247✔
1518

1519
  pVirtualTables = pScanInfo->pVirtualTables;
905,247✔
1520
  if (taosHashGetSize(pVirtualTables) == 0 || taosArrayGetSize(pReader->pColIdList) == 0) {
905,247!
1521
    goto _end;
905,247✔
1522
  }
1523

1524
  pPhysicalTables = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
1525
  TSDB_CHECK_NULL(pPhysicalTables, code, lino, _end, terrno);
×
1526
  taosHashSetFreeFp(pPhysicalTables, destroySourceScanTables);
×
1527

1528
  pIter = taosHashIterate(pVirtualTables, NULL);
×
1529
  while (pIter != NULL) {
×
1530
    int64_t vTbUid = *(int64_t*)taosHashGetKey(pIter, NULL);
×
1531
    SArray* pColInfos = *(SArray**)pIter;
×
1532
    TSDB_CHECK_NULL(pColInfos, code, lino, _end, TSDB_CODE_INTERNAL_ERROR);
×
1533

1534
    // Traverse all required columns and collect corresponding physical tables
1535
    int32_t nColInfos = taosArrayGetSize(pColInfos);
×
1536
    int32_t nOutputCols = taosArrayGetSize(pReader->pColIdList);
×
1537
    for (int32_t i = 0, j = 0; i < nColInfos && j < nOutputCols;) {
×
1538
      SVTColInfo* pCol = taosArrayGet(pColInfos, i);
×
1539
      col_id_t    colIdNeed = *(col_id_t*)taosArrayGet(pReader->pColIdList, j);
×
1540
      if (pCol->vColId < colIdNeed) {
×
1541
        i++;
×
1542
      } else if (pCol->vColId > colIdNeed) {
×
1543
        j++;
×
1544
      } else {
1545
        SArray* pRelatedVTs = NULL;
×
1546
        px = taosHashGet(pPhysicalTables, &pCol->pTbUid, sizeof(int64_t));
×
1547
        if (px == NULL) {
×
1548
          pRelatedVTs = taosArrayInit(8, sizeof(int64_t));
×
1549
          TSDB_CHECK_NULL(pRelatedVTs, code, lino, _end, terrno);
×
1550
          code = taosHashPut(pPhysicalTables, &pCol->pTbUid, sizeof(int64_t), &pRelatedVTs, POINTER_BYTES);
×
1551
          if (code != TSDB_CODE_SUCCESS) {
×
1552
            taosArrayDestroy(pRelatedVTs);
×
1553
            TSDB_CHECK_CODE(code, lino, _end);
×
1554
          }
1555
        } else {
1556
          pRelatedVTs = *(SArray**)px;
×
1557
        }
1558
        if (taosArrayGetSize(pRelatedVTs) == 0 || *(int64_t*)taosArrayGetLast(pRelatedVTs) != vTbUid) {
×
1559
          px = taosArrayPush(pRelatedVTs, &vTbUid);
×
1560
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
×
1561
        }
1562
        i++;
×
1563
        j++;
×
1564
      }
1565
    }
1566
    pIter = taosHashIterate(pVirtualTables, pIter);
×
1567
  }
1568

1569
  pScanInfo->pPhysicalTables = pPhysicalTables;
×
1570
  pPhysicalTables = NULL;
×
1571

1572
  if (taosHashGetSize(pScanInfo->pPhysicalTables) > 0) {
×
1573
    pScanInfo->pPhyTblSchemaCache = taosLRUCacheInit(1024 * 128, -1, .5);
×
1574
    TSDB_CHECK_NULL(pScanInfo->pPhyTblSchemaCache, code, lino, _end, terrno);
×
1575
  }
1576

1577
_end:
×
1578
  if (code != TSDB_CODE_SUCCESS) {
905,247!
1579
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1580
  }
1581
  if (pIter != NULL) {
905,247!
1582
    taosHashCancelIterate(pReader->tbIdHash, pIter);
×
1583
  }
1584
  if (pPhysicalTables != NULL) {
905,247!
1585
    taosHashCleanup(pPhysicalTables);
×
1586
  }
1587
  return code;
905,247✔
1588
}
1589

1590
static void freeTableSchemaCache(const void* key, size_t keyLen, void* value, void* ud) {
×
1591
  if (value) {
×
1592
    SSchemaWrapper* pSchemaWrapper = value;
×
1593
    tDeleteSchemaWrapper(pSchemaWrapper);
1594
  }
1595
}
×
1596

1597
bool tqNextVTableSourceBlockImpl(STqReader* pReader, const char* idstr) {
×
1598
  int32_t            code = TSDB_CODE_SUCCESS;
×
1599
  int32_t            lino = 0;
×
1600
  SVTSourceScanInfo* pScanInfo = NULL;
×
1601

1602
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1603

1604
  pScanInfo = &pReader->vtSourceScanInfo;
×
1605
  if (pReader->msg.msgStr == NULL || taosHashGetSize(pScanInfo->pPhysicalTables) == 0) {
×
1606
    return false;
×
1607
  }
1608

1609
  if (pScanInfo->nextVirtualTableIdx >= 0) {
×
1610
    // The data still needs to be converted into the virtual table result block
1611
    return true;
×
1612
  }
1613

1614
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
×
1615
  while (pReader->nextBlk < blockSz) {
×
1616
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
×
1617
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, _end, terrno);
×
1618
    int64_t pTbUid = pSubmitTbData->uid;
×
1619
    void*   px = taosHashGet(pScanInfo->pPhysicalTables, &pTbUid, sizeof(int64_t));
×
1620
    if (px != NULL) {
×
1621
      SArray* pRelatedVTs = *(SArray**)px;
×
1622
      if (taosArrayGetSize(pRelatedVTs) > 0) {
×
1623
        pScanInfo->nextVirtualTableIdx = 0;
×
1624
        return true;
×
1625
      }
1626
    }
1627
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64, pReader->nextBlk, blockSz, pTbUid);
×
1628
    pReader->nextBlk++;
×
1629
  }
1630

1631
  tqReaderClearSubmitMsg(pReader);
×
1632
  tqTrace("iterator data block end, total block num:%d", blockSz);
×
1633

1634
_end:
×
1635
  if (code != TSDB_CODE_SUCCESS) {
×
1636
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1637
  }
1638
  return false;
×
1639
}
1640

1641
bool tqReaderIsQueriedSourceTable(STqReader* pReader, uint64_t uid) {
×
1642
  if (pReader == NULL) {
×
1643
    return false;
×
1644
  }
1645
  return taosHashGet(pReader->vtSourceScanInfo.pPhysicalTables, &uid, sizeof(uint64_t)) != NULL;
×
1646
}
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