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

taosdata / TDengine / #4858

17 Nov 2025 09:53AM UTC coverage: 64.048% (-0.09%) from 64.135%
#4858

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

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

4856 existing lines in 134 files now uncovered.

151096 of 235910 relevant lines covered (64.05%)

115767925.69 hits per line

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

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

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

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

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

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

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

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

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

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

216
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
15,250,193✔
217
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
15,250,193✔
218
    return -1;
×
219
  }
220
  int32_t code = -1;
15,263,772✔
221
  int32_t vgId = TD_VID(pTq->pVnode);
15,263,772✔
222
  int64_t id = pHandle->pWalReader->readerId;
15,263,950✔
223

224
  int64_t offset = *fetchOffset;
15,265,358✔
225
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
15,265,468✔
226
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
15,264,906✔
227
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
15,264,996✔
228

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

233
  while (offset <= appliedVer) {
15,684,278✔
234
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
14,766,595✔
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,766,196✔
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,766,355✔
245
      code = walFetchBody(pHandle->pWalReader);
14,339,606✔
246
      goto END;
14,339,483✔
247
    } else {
248
      if (pHandle->fetchMeta != WITH_DATA) {
426,860✔
249
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
13,765✔
250
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
13,765✔
251
          code = walFetchBody(pHandle->pWalReader);
10,214✔
252
          if (code < 0) {
10,214✔
253
            goto END;
×
254
          }
255

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

276
END:
917,683✔
277
  *fetchOffset = offset;
15,267,242✔
278
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
15,267,160✔
279
          ", applied:%" PRId64 ", 0x%" PRIx64,
280
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
281
  return code;
15,267,535✔
282
}
283

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

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

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

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

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

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

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

335
  return pReader;
127,916✔
336
}
337

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

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

347
  if (pReader->pSchemaWrapper) {
127,916✔
348
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
74,048✔
349
  }
350

351
  taosMemoryFree(pReader->extSchema);
127,916✔
352
  if (pReader->pColIdList) {
127,916✔
353
    taosArrayDestroy(pReader->pColIdList);
107,982✔
354
  }
355

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

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

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

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

384
  int64_t st = taosGetTimestampMs();
36,230,023✔
385
  while (1) {
36,941,816✔
386
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
73,171,839✔
387
    while (pReader->nextBlk < numOfBlocks) {
77,078,451✔
388
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
36,939,834✔
389
              pReader->msg.ver);
390

391
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
36,939,834✔
392
      if (pSubmitTbData == NULL) {
36,935,501✔
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;
561✔
396
      }
397
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
36,935,501✔
398
        pReader->nextBlk += 1;
1,908✔
399
        continue;
1,908✔
400
      }
401
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
36,931,600✔
402
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
33,027,129✔
403
        SSDataBlock* pRes = NULL;
33,027,129✔
404
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
33,018,532✔
405
        if (code == TSDB_CODE_SUCCESS) {
33,028,153✔
406
          return true;
33,028,153✔
407
        }
408
      } else {
409
        pReader->nextBlk += 1;
3,911,178✔
410
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
3,911,178✔
411
      }
412
    }
413

414
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
40,142,303✔
415
    pReader->msg.msgStr = NULL;
40,141,358✔
416

417
    int64_t elapsed = taosGetTimestampMs() - st;
40,141,478✔
418
    if (elapsed > 1000 || elapsed < 0) {
40,141,478✔
UNCOV
419
      return false;
×
420
    }
421

422
    // try next message in wal file
423
    if (walNextValidMsg(pWalReader, false) < 0) {
40,142,518✔
424
      return false;
3,199,234✔
425
    }
426

427
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
36,940,307✔
428
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
36,942,355✔
429
    int64_t ver = pWalReader->pHead->head.version;
36,942,084✔
430
    SDecoder decoder = {0};
36,942,717✔
431
    if (tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL, &decoder) != 0) {
36,941,450✔
432
      tDecoderClear(&decoder);
×
433
      return false;
×
434
    }
435
    tDecoderClear(&decoder);
36,938,981✔
436
    pReader->nextBlk = 0;
36,940,655✔
437
  }
438
}
439

440
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList, SDecoder* decoder) {
51,278,941✔
441
  if (pReader == NULL) {
51,278,941✔
442
    return TSDB_CODE_INVALID_PARA;
×
443
  }
444
  pReader->msg.msgStr = msgStr;
51,278,941✔
445
  pReader->msg.msgLen = msgLen;
51,281,256✔
446
  pReader->msg.ver = ver;
51,281,596✔
447

448
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
51,280,784✔
449

450
  tDecoderInit(decoder, pReader->msg.msgStr, pReader->msg.msgLen);
51,280,784✔
451
  int32_t code = tDecodeSubmitReq(decoder, &pReader->submit, rawList);
51,281,784✔
452

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

457
  return code;
51,271,182✔
458
}
459

460
void tqReaderClearSubmitMsg(STqReader* pReader) {
28,666,044✔
461
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
28,666,044✔
462
  pReader->nextBlk = 0;
28,661,639✔
463
  pReader->msg.msgStr = NULL;
28,667,171✔
464
}
28,669,081✔
465

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

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

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

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

495
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
5,579,937✔
496
  while (pReader->nextBlk < blockSz) {
5,885,354✔
497
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
2,942,794✔
498
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
2,943,055✔
499
    uid = pSubmitTbData->uid;
2,943,055✔
500
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
2,943,055✔
501
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
2,943,492✔
502

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

508
  tqReaderClearSubmitMsg(pReader);
2,941,860✔
509
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
2,942,039✔
510

511
END:
2,942,039✔
512
  tqTrace("%s:%d return:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
5,579,850✔
513
  return code;
5,579,499✔
514
}
515

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

521
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
22,788,854✔
522
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
22,788,854✔
523
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
22,791,574✔
524

525
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
22,791,574✔
526
  while (pReader->nextBlk < blockSz) {
22,791,630✔
527
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
11,397,619✔
528
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
11,398,142✔
529
    uid = pSubmitTbData->uid;
11,398,142✔
530
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
11,398,142✔
531
    TSDB_CHECK_NULL(ret, code, lino, END, true);
11,398,282✔
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,804✔
536
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
11,393,548✔
537

538
END:
11,393,548✔
539
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
22,791,830✔
540
  return code;
22,780,044✔
541
}
542

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

550
  int32_t cnt = 0;
13,991,793✔
551
  for (int32_t i = 0; i < pSrc->nCols; i++) {
63,926,243✔
552
    cnt += mask[i];
49,931,133✔
553
  }
554

555
  pDst->nCols = cnt;
13,990,665✔
556
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
13,996,563✔
557
  if (pDst->pSchema == NULL) {
13,993,390✔
558
    return TAOS_GET_TERRNO(terrno);
×
559
  }
560

561
  int32_t j = 0;
13,989,936✔
562
  for (int32_t i = 0; i < pSrc->nCols; i++) {
63,934,799✔
563
    if (mask[i]) {
49,931,590✔
564
      pDst->pSchema[j++] = pSrc->pSchema[i];
49,939,973✔
565
      SColumnInfoData colInfo =
49,939,586✔
566
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
49,941,303✔
567
      if (extSrc != NULL) {
49,934,159✔
568
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
3,924✔
569
      }
570
      code = blockDataAppendColInfo(pBlock, &colInfo);
49,934,159✔
571
      if (code != 0) {
49,943,416✔
572
        return code;
×
573
      }
574
    }
575
  }
576
  return 0;
13,998,195✔
577
}
578

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

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

596
  int32_t numOfCols = taosArrayGetSize(pColIdList);
62,980✔
597

598
  if (numOfCols == 0) {  // all columns are required
62,980✔
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) {
62,980✔
614
      numOfCols = pSchema->nCols;
70✔
615
    }
616

617
    int32_t i = 0;
62,980✔
618
    int32_t j = 0;
62,980✔
619
    while (i < pSchema->nCols && j < numOfCols) {
816,270✔
620
      SSchema* pColSchema = &pSchema->pSchema[i];
752,947✔
621
      col_id_t colIdSchema = pColSchema->colId;
752,847✔
622

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

646
  return TSDB_CODE_SUCCESS;
62,980✔
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)) {
727,249,508✔
695
      char val[65535 + 2] = {0};
756,248,385✔
696
      if (pColVal->value.pData != NULL) {
755,272,301✔
697
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
755,650,434✔
698
      }
699
      varDataSetLen(val, pColVal->value.nData);
755,527,982✔
700
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
756,383,458✔
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) {
33,015,450✔
713
  if (pReader == NULL || pRes == NULL) {
33,015,450✔
714
    return TSDB_CODE_INVALID_PARA;
×
715
  }
716
  tqDebug("tq reader retrieve data block %p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
33,026,430✔
717
  int32_t        code = 0;
33,032,763✔
718
  int32_t        line = 0;
33,032,763✔
719
  STSchema*      pTSchema = NULL;
33,032,763✔
720
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk++);
33,032,763✔
721
  TSDB_CHECK_NULL(pSubmitTbData, code, line, END, terrno);
33,031,314✔
722
  SSDataBlock* pBlock = pReader->pResBlock;
33,031,314✔
723
  *pRes = pBlock;
33,031,419✔
724

725
  blockDataCleanup(pBlock);
33,031,104✔
726

727
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
33,030,594✔
728
  int32_t sversion = pSubmitTbData->sver;
33,031,104✔
729
  int64_t suid = pSubmitTbData->suid;
33,031,314✔
730
  int64_t uid = pSubmitTbData->uid;
33,031,314✔
731
  pReader->lastTs = pSubmitTbData->ctimeMs;
33,031,209✔
732

733
  pBlock->info.id.uid = uid;
33,031,524✔
734
  pBlock->info.version = pReader->msg.ver;
33,031,314✔
735

736
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
33,031,524✔
737
      (pReader->cachedSchemaVer != sversion)) {
32,967,774✔
738
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
63,230✔
739
    taosMemoryFree(pReader->extSchema);
62,976✔
740
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
62,976✔
741
    if (pReader->pSchemaWrapper == NULL) {
62,980✔
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;
62,876✔
750
    pReader->cachedSchemaSuid = suid;
62,876✔
751
    pReader->cachedSchemaVer = sversion;
62,876✔
752

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

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

773
  code = blockDataEnsureCapacity(pBlock, numOfRows);
33,030,790✔
774
  TSDB_CHECK_CODE(code, line, END);
33,030,789✔
775
  pBlock->info.rows = numOfRows;
33,030,789✔
776
  int32_t colActual = blockDataGetNumOfCols(pBlock);
33,030,894✔
777

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

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

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

808
          if (isBlob == 0) {
312✔
809
            code = doSetVal(pColData, i, &colVal);
312✔
810
          } else {
811
            code = doSetBlobVal(pColData, i, &colVal, pSubmitTbData->pBlobSet);
×
812
          }
813
          TSDB_CHECK_CODE(code, line, END);
312✔
814
        }
815
        sourceIdx++;
156✔
816
        targetIdx++;
156✔
817
      } else {
818
        colDataSetNNULL(pColData, 0, numOfRows);
78✔
819
        targetIdx++;
78✔
820
      }
821
    }
822
  } else {
823
    SArray*         pRows = pSubmitTbData->aRowP;
33,030,352✔
824
    SSchemaWrapper* pWrapper = pReader->pSchemaWrapper;
33,030,739✔
825
    pTSchema = tBuildTSchema(pWrapper->pSchema, pWrapper->nCols, pWrapper->version);
33,030,843✔
826
    TSDB_CHECK_NULL(pTSchema, code, line, END, terrno);
33,029,887✔
827

828
    for (int32_t i = 0; i < numOfRows; i++) {
1,612,370,209✔
829
      SRow* pRow = taosArrayGetP(pRows, i);
1,481,593,517✔
830
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
1,477,869,072✔
831
      int32_t sourceIdx = 0;
1,482,588,083✔
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) {
134,846,375✔
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++;
134,845,650✔
844
            continue;
134,845,650✔
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:
130,776,692✔
866
  if (code != 0) {
83,625,293✔
867
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
868
  }
869
  taosMemoryFreeClear(pTSchema);
33,029,708✔
870
  return code;
33,028,970✔
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,985,275✔
902
                               char* assigned, int32_t numOfRows, int32_t curRow, int32_t* lastRow) {
903
  int32_t         code = 0;
13,985,275✔
904
  SSchemaWrapper* pSW = NULL;
13,985,275✔
905
  SSDataBlock*    block = NULL;
13,988,682✔
906
  if (taosArrayGetSize(blocks) > 0) {
13,988,682✔
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,991,328✔
914
  TQ_NULL_GO_TO_END(block);
13,989,350✔
915

916
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
13,989,350✔
917
  TQ_NULL_GO_TO_END(pSW);
13,991,115✔
918

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

923
  block->info.id.uid = pSubmitTbData->uid;
13,998,113✔
924
  block->info.version = pReader->msg.ver;
13,997,241✔
925
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
13,996,486✔
926
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
13,995,558✔
927
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
13,995,888✔
928
  pSW = NULL;
13,995,888✔
929

930
  taosMemoryFreeClear(block);
13,995,888✔
931

932
END:
13,997,649✔
933
  if (code != 0) {
13,995,359✔
934
    tqError("processBuildNew failed, code:%d", code);
×
935
  }
936
  tDeleteSchemaWrapper(pSW);
13,995,359✔
937
  blockDataFreeRes(block);
13,990,834✔
938
  taosMemoryFree(block);
13,990,482✔
939
  return code;
13,993,282✔
940
}
941
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
6,108✔
942
  int32_t code = 0;
6,108✔
943
  int32_t curRow = 0;
6,108✔
944
  int32_t lastRow = 0;
6,108✔
945

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

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

960
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
14,745,016✔
961
      int32_t k = 0;
10,472,860✔
962
      for (; k < numOfCols; k++) {
21,011,866✔
963
        pCol = taosArrayGet(pCols, k);
19,500,118✔
964
        TQ_NULL_GO_TO_END(pCol);
17,182,166✔
965
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
17,182,166✔
966
          SColVal colVal = {0};
10,806,396✔
967
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
11,134,836✔
968
          PROCESS_VAL
11,394,452✔
969
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
11,477,724✔
970
          break;
10,777,416✔
971
        }
972
      }
973
      if (k >= numOfCols) {
10,691,960✔
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) {
4,311,776✔
981
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
6,108✔
982
    }
983

984
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
4,311,776✔
985
    TQ_NULL_GO_TO_END(pBlock);
4,010,244✔
986

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

990
    int32_t targetIdx = 0;
4,010,244✔
991
    int32_t sourceIdx = 0;
4,010,244✔
992
    int32_t colActual = blockDataGetNumOfCols(pBlock);
4,010,244✔
993
    while (targetIdx < colActual && sourceIdx < numOfCols) {
15,633,786✔
994
      pCol = taosArrayGet(pCols, sourceIdx);
11,570,930✔
995
      TQ_NULL_GO_TO_END(pCol);
11,248,286✔
996
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
11,248,286✔
997
      TQ_NULL_GO_TO_END(pColData);
10,792,922✔
998
      SColVal colVal = {0};
10,792,922✔
999
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
10,923,346✔
1000
      SET_DATA
10,659,278✔
1001
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
11,847,738✔
1002
    }
1003

1004
    curRow++;
4,062,856✔
1005
  }
1006
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
15,908✔
1007
  pLastBlock->info.rows = curRow - lastRow;
6,108✔
1008
  tqTrace("vgId:%d, tqProcessColData end, col num: %d, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId,
6,108✔
1009
          numOfCols, numOfRows, (int)taosArrayGetSize(blocks));
1010
END:
2,387,144✔
1011
  if (code != TSDB_CODE_SUCCESS) {
6,108✔
1012
    tqError("vgId:%d, process col data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1013
  }
1014
  taosMemoryFree(assigned);
6,108✔
1015
  return code;
6,108✔
1016
}
1017

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

1022
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
13,981,633✔
1023
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
13,985,045✔
1024
  TQ_NULL_GO_TO_END(assigned);
13,985,634✔
1025

1026
  int32_t curRow = 0;
13,985,634✔
1027
  int32_t lastRow = 0;
13,985,634✔
1028
  SArray* pRows = pSubmitTbData->aRowP;
13,984,172✔
1029
  int32_t numOfRows = taosArrayGetSize(pRows);
13,989,545✔
1030
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
13,989,766✔
1031
  TQ_NULL_GO_TO_END(pTSchema);
13,985,968✔
1032
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
13,985,968✔
1033

1034
  for (int32_t i = 0; i < numOfRows; i++) {
287,559,441✔
1035
    bool  buildNew = false;
273,596,771✔
1036
    SRow* pRow = taosArrayGetP(pRows, i);
273,596,771✔
1037
    TQ_NULL_GO_TO_END(pRow);
273,527,407✔
1038

1039
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
1,520,263,099✔
1040
      SColVal colVal = {0};
1,245,865,385✔
1041
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
1,245,183,160✔
1042
      PROCESS_VAL
1,246,616,939✔
1043
      tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], pTSchema->numOfCols);
1,246,459,218✔
1044
    }
1045

1046
    if (buildNew) {
271,115,270✔
1047
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
13,988,549✔
1048
    }
1049

1050
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
271,113,213✔
1051
    TQ_NULL_GO_TO_END(pBlock);
273,572,930✔
1052

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

1056
    int32_t targetIdx = 0;
273,572,930✔
1057
    int32_t sourceIdx = 0;
273,572,930✔
1058
    int32_t colActual = blockDataGetNumOfCols(pBlock);
273,572,930✔
1059
    while (targetIdx < colActual && sourceIdx < pTSchema->numOfCols) {
1,517,566,908✔
1060
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
1,244,093,784✔
1061
      TQ_NULL_GO_TO_END(pColData);
1,244,633,275✔
1062
      SColVal          colVal = {0};
1,244,633,275✔
1063
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
1,243,862,460✔
1064
      SET_DATA
1,245,029,922✔
1065
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
1,245,951,113✔
1066
    }
1067

1068
    curRow++;
273,579,994✔
1069
  }
1070
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
13,962,670✔
1071
  if (pLastBlock != NULL) {
13,990,644✔
1072
    pLastBlock->info.rows = curRow - lastRow;
13,991,054✔
1073
  }
1074

1075
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
13,990,636✔
1076
          (int)taosArrayGetSize(blocks));
1077
END:
15,411,304✔
1078
  if (code != TSDB_CODE_SUCCESS) {
13,985,217✔
1079
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1080
  }
1081
  taosMemoryFreeClear(pTSchema);
13,982,934✔
1082
  taosMemoryFree(assigned);
13,984,205✔
1083
  return code;
13,987,099✔
1084
}
1085

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

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

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

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

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

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

1133
  if (pSubmitTbDataRet) {
14,035,255✔
1134
    *pSubmitTbDataRet = pSubmitTbData;
14,035,859✔
1135
  }
1136

1137
  if (fetchMeta == ONLY_META) {
14,035,460✔
1138
    if (pSubmitTbData->pCreateTbReq != NULL) {
396✔
1139
      if (pRsp->createTableReq == NULL) {
72✔
1140
        pRsp->createTableReq = taosArrayInit(0, POINTER_BYTES);
18✔
1141
        if (pRsp->createTableReq == NULL) {
18✔
1142
          return terrno;
×
1143
        }
1144
      }
1145
      if (taosArrayPush(pRsp->createTableReq, &pSubmitTbData->pCreateTbReq) == NULL) {
144✔
1146
        return terrno;
×
1147
      }
1148
      pSubmitTbData->pCreateTbReq = NULL;
72✔
1149
    }
1150
    return 0;
396✔
1151
  }
1152

1153
  int32_t sversion = pSubmitTbData->sver;
14,035,064✔
1154
  int64_t uid = pSubmitTbData->uid;
14,035,000✔
1155
  pReader->lastBlkUid = uid;
14,034,865✔
1156

1157
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
14,035,240✔
1158
  taosMemoryFreeClear(pReader->extSchema);
14,034,698✔
1159
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
14,035,782✔
1160
  if (pReader->pSchemaWrapper == NULL) {
14,033,081✔
1161
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
38,084✔
1162
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1163
    pReader->cachedSchemaSuid = 0;
38,084✔
1164
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
38,084✔
1165
  }
1166

1167
  if (pSubmitTbData->pCreateTbReq != NULL) {
13,991,648✔
1168
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
619✔
1169
    if (code != 0) {
619✔
1170
      return code;
×
1171
    }
1172
  } else if (rawList != NULL) {
13,993,875✔
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,994,494✔
1181
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
6,108✔
1182
  } else {
1183
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
13,984,402✔
1184
  }
1185
}
1186

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

1195
int32_t tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
112,539✔
1196
  if (pReader == NULL || tbUidList == NULL) {
112,539✔
1197
    return TSDB_CODE_SUCCESS;
×
1198
  }
1199
  if (pReader->tbIdHash) {
112,615✔
1200
    taosHashClear(pReader->tbIdHash);
1,058✔
1201
  } else {
1202
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
111,413✔
1203
    if (pReader->tbIdHash == NULL) {
111,557✔
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,752,010✔
1210
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
2,637,294✔
1211
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
2,637,637✔
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));
112,656✔
1218
  return TSDB_CODE_SUCCESS;
112,615✔
1219
}
1220

1221
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
50,380✔
1222
  if (pReader == NULL || pTableUidList == NULL) {
50,380✔
1223
    return;
×
1224
  }
1225
  if (pReader->tbIdHash == NULL) {
50,380✔
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);
50,380✔
1234
  for (int i = 0; i < numOfTables; i++) {
79,272✔
1235
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
28,892✔
1236
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
28,892✔
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,988,807✔
1270
  if (pTq == NULL) {
62,988,807✔
1271
    return 0;  // mounted vnode may have no tq
×
1272
  }
1273
  if (tbUidList == NULL) {
62,988,807✔
1274
    return TSDB_CODE_INVALID_PARA;
×
1275
  }
1276
  void*   pIter = NULL;
62,988,807✔
1277
  int32_t vgId = TD_VID(pTq->pVnode);
62,988,807✔
1278

1279
  // update the table list for each consumer handle
1280
  taosWLockLatch(&pTq->lock);
62,988,896✔
1281
  while (1) {
185,064✔
1282
    pIter = taosHashIterate(pTq->pHandle, pIter);
63,173,803✔
1283
    if (pIter == NULL) {
63,173,390✔
1284
      break;
62,988,326✔
1285
    }
1286

1287
    STqHandle* pTqHandle = (STqHandle*)pIter;
185,064✔
1288
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
185,064✔
1289
      int32_t code = qUpdateTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList, isAdd);
50,976✔
1290
      if (code != 0) {
50,976✔
1291
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1292
        continue;
×
1293
      }
1294
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
134,088✔
1295
      if (!isAdd) {
133,030✔
1296
        int32_t sz = taosArrayGetSize(tbUidList);
44,710✔
1297
        for (int32_t i = 0; i < sz; i++) {
44,710✔
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,058✔
1307
      if (isAdd) {
1,058✔
1308
        SArray* list = NULL;
1,058✔
1309
        int     ret = qGetTableList(pTqHandle->execHandle.execTb.suid, pTq->pVnode, pTqHandle->execHandle.execTb.node,
1,058✔
1310
                                    &list, pTqHandle->execHandle.task);
1311
        if (ret == 0) {
1,058✔
1312
          ret = tqReaderSetTbUidList(pTqHandle->execHandle.pTqReader, list, NULL);
1,058✔
1313
        }                            
1314
        if (ret != TDB_CODE_SUCCESS) {
1,058✔
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,058✔
1324
      } else {
1325
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
×
1326
      }
1327
    }
1328
  }
1329
  taosWUnLockLatch(&pTq->lock);
62,988,326✔
1330
  return 0;
62,988,964✔
1331
}
1332

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1480
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
107,906✔
1481

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
1613
bool tqReaderIsQueriedSourceTable(STqReader* pReader, uint64_t uid) {
×
UNCOV
1614
  if (pReader == NULL) {
×
UNCOV
1615
    return false;
×
1616
  }
UNCOV
1617
  return taosHashGet(pReader->vtSourceScanInfo.pPhysicalTables, &uid, sizeof(uint64_t)) != NULL;
×
1618
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc