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

taosdata / TDengine / #4995

18 Mar 2026 06:26AM UTC coverage: 71.996% (-0.2%) from 72.244%
#4995

push

travis-ci

web-flow
merge: from main to 3.0 branch #34835

2 of 4 new or added lines in 2 files covered. (50.0%)

5312 existing lines in 167 files now uncovered.

244665 of 339830 relevant lines covered (72.0%)

135013613.72 hits per line

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

74.8
/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 "taoserror.h"
17
#include "tarray.h"
18
#include "tdef.h"
19
#include "thash.h"
20
#include "tmsg.h"
21
#include "tpriv.h"
22
#include "tq.h"
23

24
static int32_t tqRetrieveCols(STqReader *pReader, SSDataBlock *pRes, SHashObj* pCol2SlotId);
25

26
static void processCreateTbMsg(SDecoder* dcoder, SWalCont* pHead, STqReader* pReader, int64_t* realTbSuid, int64_t tbSuid) {
4,479✔
27
  int32_t code = 0;
4,479✔
28
  int32_t lino = 0;
4,479✔
29
  int32_t        needRebuild = 0;
4,479✔
30
  SVCreateTbReq* pCreateReq = NULL;
4,479✔
31
  SVCreateTbBatchReq reqNew = {0};
4,479✔
32
  void* buf = NULL;
4,479✔
33
  SVCreateTbBatchReq req = {0};
4,479✔
34
  code = tDecodeSVCreateTbBatchReq(dcoder, &req);
4,479✔
35
  if (code < 0) {
4,479✔
36
    lino = __LINE__;
×
37
    goto end;
×
38
  }
39

40
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
9,285✔
41
    pCreateReq = req.pReqs + iReq;
4,806✔
42
    if ((pCreateReq->type == TSDB_CHILD_TABLE || pCreateReq->type == TSDB_VIRTUAL_CHILD_TABLE) && 
4,806✔
43
         pCreateReq->ctb.suid == tbSuid &&
4,732✔
44
         taosHashGet(pReader->tbIdHash, &pCreateReq->uid, sizeof(int64_t)) != NULL) {  
2,366✔
45
      needRebuild++;
1,711✔
46
    }
47
  }
48
  if (needRebuild == 0) {
4,479✔
49
    // do nothing
50
  } else if (needRebuild == req.nReqs) {
1,711✔
51
    *realTbSuid = tbSuid;
1,384✔
52
  } else {
53
    *realTbSuid = tbSuid;
327✔
54
    reqNew.pArray = taosArrayInit(req.nReqs, sizeof(struct SVCreateTbReq));
327✔
55
    if (reqNew.pArray == NULL) {
327✔
56
      code = terrno;
×
57
      lino = __LINE__;
×
58
      goto end;
×
59
    }
60
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
981✔
61
      pCreateReq = req.pReqs + iReq;
654✔
62
      if ((pCreateReq->type == TSDB_CHILD_TABLE || pCreateReq->type == TSDB_VIRTUAL_CHILD_TABLE) &&
654✔
63
          pCreateReq->ctb.suid == tbSuid &&
1,308✔
64
          taosHashGet(pReader->tbIdHash, &pCreateReq->uid, sizeof(int64_t)) != NULL) {
654✔
65
        reqNew.nReqs++;
327✔
66
        if (taosArrayPush(reqNew.pArray, pCreateReq) == NULL) {
654✔
67
          code = terrno;
×
68
          lino = __LINE__;
×
69
          goto end;
×
70
        }
71
      }
72
    }
73

74
    int     tlen = 0;
327✔
75
    tEncodeSize(tEncodeSVCreateTbBatchReq, &reqNew, tlen, code);
327✔
76
    buf = taosMemoryMalloc(tlen);
327✔
77
    if (NULL == buf || code < 0) {
327✔
78
      lino = __LINE__;
×
79
      goto end;
×
80
    }
81
    SEncoder coderNew = {0};
327✔
82
    tEncoderInit(&coderNew, buf, tlen);
327✔
83
    code = tEncodeSVCreateTbBatchReq(&coderNew, &reqNew);
327✔
84
    tEncoderClear(&coderNew);
327✔
85
    if (code < 0) {
327✔
86
      lino = __LINE__;
×
87
      goto end;
×
88
    }
89
    (void)memcpy(pHead->body + sizeof(SMsgHead), buf, tlen);
327✔
90
    pHead->bodyLen = tlen + sizeof(SMsgHead);
327✔
91
  }
92

93
end:
4,479✔
94
  taosMemoryFree(buf);
4,479✔
95
  taosArrayDestroy(reqNew.pArray);
4,479✔
96
  tDeleteSVCreateTbBatchReq(&req);
4,479✔
97
  if (code < 0) {
4,479✔
98
    tqError("processCreateTbMsg failed, code:%d, line:%d", code, lino);
×
99
  }
100
}
4,479✔
101

102
static void processAlterTbMsg(SDecoder* dcoder, STqReader* pReader, int64_t* realTbSuid) {
2,768✔
103
  SVAlterTbReq req = {0};
2,768✔
104
  SMetaReader mr = {0};
2,768✔
105
  int32_t lino = 0;
2,768✔
106
  int32_t code = tDecodeSVAlterTbReq(dcoder, &req);
2,768✔
107
  if (code < 0) {
2,768✔
108
    lino = __LINE__;
×
109
    goto end;
×
110
  }
111

112
  metaReaderDoInit(&mr, pReader->pVnode->pMeta, META_READER_LOCK);
2,768✔
113

114
  code = metaGetTableEntryByName(&mr, req.tbName);
2,768✔
115
  if (code < 0) {
2,768✔
116
    lino = __LINE__;
656✔
117
    goto end;
656✔
118
  }
119
  if (taosHashGet(pReader->tbIdHash, &mr.me.uid, sizeof(int64_t)) != NULL) {
2,112✔
120
    *realTbSuid = mr.me.ctbEntry.suid;
704✔
121
  }
122

123
end:
2,768✔
124
  taosArrayDestroy(req.pMultiTag);
2,768✔
125
  metaReaderClear(&mr);  
2,768✔
126
  if (code < 0) {
2,768✔
127
    tqError("processAlterTbMsg failed, code:%d, line:%d", code, lino);
656✔
128
  }
129
} 
2,768✔
130

131
static void processDropTbMsg(SDecoder* dcoder, SWalCont* pHead, STqReader* pReader, int64_t* realTbSuid, int64_t tbSuid) {
×
132
  SVDropTbBatchReq req = {0};
×
133
  SVDropTbBatchReq reqNew = {0};
×
134
  void* buf = NULL;
×
135
  int32_t lino = 0;
×
136
  int32_t code = tDecodeSVDropTbBatchReq(dcoder, &req);
×
137
  if (code < 0) {
×
138
    lino = __LINE__;
×
139
    goto end;
×
140
  }
141

142
  int32_t      needRebuild = 0;
×
143
  SVDropTbReq* pDropReq = NULL;
×
144
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
145
    pDropReq = req.pReqs + iReq;
×
146

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

177
    int     tlen = 0;
×
178
    tEncodeSize(tEncodeSVDropTbBatchReq, &reqNew, tlen, code);
×
179
    buf = taosMemoryMalloc(tlen);
×
180
    if (NULL == buf || code < 0) {
×
181
      lino = __LINE__;
×
182
      goto end;
×
183
    }
184
    SEncoder coderNew = {0};
×
185
    tEncoderInit(&coderNew, buf, tlen);
×
186
    code = tEncodeSVDropTbBatchReq(&coderNew, &reqNew);
×
187
    tEncoderClear(&coderNew);
×
188
    if (code != 0) {
×
189
      lino = __LINE__;
×
190
      goto end;
×
191
    }
192
    (void)memcpy(pHead->body + sizeof(SMsgHead), buf, tlen);
×
193
    pHead->bodyLen = tlen + sizeof(SMsgHead);
×
194
  }
195

196
end:
×
197
  taosMemoryFree(buf);
×
198
  taosArrayDestroy(reqNew.pArray);
×
199
  if (code < 0) {
×
200
    tqError("processDropTbMsg failed, code:%d, line:%d", code, lino);
×
201
  }
202
}
×
203

204
bool isValValidForTable(STqHandle* pHandle, SWalCont* pHead) {
18,400✔
205
  int32_t code = 0;
18,400✔
206
  int32_t lino = 0;
18,400✔
207
  if (pHandle == NULL || pHead == NULL) {
18,400✔
208
    return false;
×
209
  }
210
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
18,400✔
211
    return true;
9,090✔
212
  }
213

214
  STqExecHandle* pExec = &pHandle->execHandle;
9,310✔
215
  STqReader* pReader = pExec->pTqReader;
9,310✔
216

217
  int16_t msgType = pHead->msgType;
9,310✔
218
  char*   body = pHead->body;
9,310✔
219
  int32_t bodyLen = pHead->bodyLen;
9,310✔
220

221
  int64_t  tbSuid = pHandle->execHandle.execTb.suid;
9,310✔
222
  int64_t  realTbSuid = 0;
9,310✔
223
  SDecoder dcoder = {0};
9,310✔
224
  void*    data = POINTER_SHIFT(body, sizeof(SMsgHead));
9,310✔
225
  int32_t  len = bodyLen - sizeof(SMsgHead);
9,310✔
226
  tDecoderInit(&dcoder, data, len);
9,310✔
227

228
  if (msgType == TDMT_VND_CREATE_STB || msgType == TDMT_VND_ALTER_STB) {
11,373✔
229
    SVCreateStbReq req = {0};
2,063✔
230
    if (tDecodeSVCreateStbReq(&dcoder, &req) < 0) {
2,063✔
231
      goto end;
×
232
    }
233
    realTbSuid = req.suid;
2,063✔
234
  } else if (msgType == TDMT_VND_DROP_STB) {
7,247✔
235
    SVDropStbReq req = {0};
×
236
    if (tDecodeSVDropStbReq(&dcoder, &req) < 0) {
×
237
      goto end;
×
238
    }
239
    realTbSuid = req.suid;
×
240
  } else if (msgType == TDMT_VND_CREATE_TABLE) {
7,247✔
241
    processCreateTbMsg(&dcoder, pHead, pReader, &realTbSuid, tbSuid);
4,479✔
242
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
2,768✔
243
    processAlterTbMsg(&dcoder, pReader, &realTbSuid);
2,768✔
244
  } else if (msgType == TDMT_VND_DROP_TABLE) {
×
245
    processDropTbMsg(&dcoder, pHead, pReader, &realTbSuid, tbSuid);
×
246
  } else if (msgType == TDMT_VND_DELETE) {
×
247
    SDeleteRes req = {0};
×
248
    if (tDecodeDeleteRes(&dcoder, &req) < 0) {
×
249
      goto end;
×
250
    }
251
    realTbSuid = req.suid;
×
252
  }
253

254
end:
9,310✔
255
  tDecoderClear(&dcoder);
9,310✔
256
  bool tmp = tbSuid == realTbSuid;
9,310✔
257
  tqDebug("%s suid:%" PRId64 " realSuid:%" PRId64 " return:%d", __FUNCTION__, tbSuid, realTbSuid, tmp);
9,310✔
258
  return tmp;
9,310✔
259
}
260

261
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
51,799,891✔
262
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
51,799,891✔
263
    return -1;
×
264
  }
265
  int32_t code = -1;
51,869,862✔
266
  int32_t vgId = TD_VID(pTq->pVnode);
51,869,862✔
267
  int64_t id = pHandle->pWalReader->readerId;
51,884,089✔
268

269
  int64_t offset = *fetchOffset;
51,883,355✔
270
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
51,889,567✔
271
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
51,887,729✔
272
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
51,881,289✔
273

274
  tqDebug("vgId:%d, start to fetch wal, index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64 ", applied:%" PRId64
51,884,632✔
275
          ", 0x%" PRIx64,
276
          vgId, offset, lastVer, committedVer, appliedVer, id);
277

278
  while (offset <= appliedVer) {
54,834,823✔
279
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
49,637,314✔
280
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", (epoch %d) vgId:%d offset %" PRId64
×
281
              ", no more log to return, QID:0x%" PRIx64 " 0x%" PRIx64,
282
              pHandle->consumerId, pHandle->epoch, vgId, offset, reqId, id);
283
      goto END;
×
284
    }
285

286
    tqDebug("vgId:%d, consumer:0x%" PRIx64 " taosx get msg ver %" PRId64 ", type:%s, QID:0x%" PRIx64 " 0x%" PRIx64,
49,637,620✔
287
            vgId, pHandle->consumerId, offset, TMSG_INFO(pHandle->pWalReader->pHead->head.msgType), reqId, id);
288

289
    if (pHandle->pWalReader->pHead->head.msgType == TDMT_VND_SUBMIT) {
49,637,926✔
290
      code = walFetchBody(pHandle->pWalReader);
46,683,280✔
291
      goto END;
46,682,034✔
292
    } else {
293
      if (pHandle->fetchMeta != WITH_DATA) {
2,954,952✔
294
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
30,878✔
295
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
30,878✔
296
          code = walFetchBody(pHandle->pWalReader);
18,400✔
297
          if (code < 0) {
18,400✔
298
            goto END;
×
299
          }
300

301
          pHead = &(pHandle->pWalReader->pHead->head);
18,400✔
302
          if (isValValidForTable(pHandle, pHead)) {
18,400✔
303
            code = 0;
12,864✔
304
            goto END;
12,864✔
305
          } else {
306
            offset++;
5,536✔
307
            code = -1;
5,536✔
308
            continue;
5,536✔
309
          }
310
        }
311
      }
312
      code = walSkipFetchBody(pHandle->pWalReader);
2,936,552✔
313
      if (code < 0) {
2,936,246✔
314
        goto END;
×
315
      }
316
      offset++;
2,936,246✔
317
    }
318
    code = -1;
2,936,246✔
319
  }
320

321
END:
5,197,509✔
322
  *fetchOffset = offset;
51,892,407✔
323
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
51,892,407✔
324
          ", applied:%" PRId64 ", 0x%" PRIx64,
325
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
326
  return code;
51,893,301✔
327
}
328

329
bool tqGetTablePrimaryKey(STqReader* pReader) {
6,082,087✔
330
  if (pReader == NULL) {
6,082,087✔
331
    return false;
×
332
  }
333
  return pReader->hasPrimaryKey;
6,082,087✔
334
}
335

336
void tqSetTablePrimaryKey(STqReader* pReader, int64_t uid) {
46,953✔
337
  tqDebug("%s:%p uid:%" PRId64, __FUNCTION__, pReader, uid);
46,953✔
338

339
  if (pReader == NULL) {
46,953✔
340
    return;
×
341
  }
342
  bool            ret = false;
46,953✔
343
  SSchemaWrapper* schema = metaGetTableSchema(pReader->pVnode->pMeta, uid, -1, 1, NULL, 0);
46,953✔
344
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
46,638✔
345
    ret = true;
949✔
346
  }
347
  tDeleteSchemaWrapper(schema);
348
  pReader->hasPrimaryKey = ret;
46,953✔
349
}
350

351
static void freeTagCache(void* pData){
1,672,156✔
352
  if (pData == NULL) return;
1,672,156✔
353
  SArray* tagCache = *(SArray**)pData;
1,672,156✔
354
  taosArrayDestroyP(tagCache, taosMemFree);
1,672,156✔
355
}
356

357
STqReader* tqReaderOpen(SVnode* pVnode) {
428,357✔
358
  tqDebug("%s:%p", __FUNCTION__, pVnode);
428,357✔
359
  if (pVnode == NULL) {
429,627✔
360
    return NULL;
×
361
  }
362
  STqReader* pReader = taosMemoryCalloc(1, sizeof(STqReader));
429,627✔
363
  if (pReader == NULL) {
429,627✔
364
    return NULL;
×
365
  }
366

367
  pReader->pWalReader = walOpenReader(pVnode->pWal, 0);
429,627✔
368
  if (pReader->pWalReader == NULL) {
429,627✔
369
    taosMemoryFree(pReader);
×
370
    return NULL;
×
371
  }
372

373
  pReader->pVnode = pVnode;
429,627✔
374
  pReader->pSchemaWrapper = NULL;
429,627✔
375
  pReader->tbIdHash = NULL;
429,627✔
376
  pReader->pTableTagCacheForTmq = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
429,627✔
377
  if (pReader->pTableTagCacheForTmq == NULL) {
429,627✔
378
    walCloseReader(pReader->pWalReader);
×
379
    taosMemoryFree(pReader);
×
380
    return NULL;
×
381
  }
382
  taosHashSetFreeFp(pReader->pTableTagCacheForTmq, freeTagCache);
429,627✔
383
  taosInitRWLatch(&pReader->tagCachelock);
429,627✔
384

385
  return pReader;
429,627✔
386
}
387

388
void tqReaderClose(STqReader* pReader) {
433,577✔
389
  tqDebug("%s:%p", __FUNCTION__, pReader);
433,577✔
390
  if (pReader == NULL) return;
434,219✔
391

392
  // close wal reader
393
  walCloseReader(pReader->pWalReader);
429,627✔
394
  taosHashCleanup(pReader->pTableTagCacheForTmq);
429,627✔
395
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
429,627✔
396
  taosMemoryFree(pReader->pTSchema);
429,627✔
397
  taosMemoryFree(pReader->extSchema);
429,627✔
398

399
  // free hash
400
  taosHashCleanup(pReader->tbIdHash);
429,627✔
401
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
429,627✔
402

403
  taosMemoryFree(pReader);
429,627✔
404
}
405

406
int32_t tqReaderSeek(STqReader* pReader, int64_t ver, const char* id) {
958,111✔
407
  if (pReader == NULL) {
958,111✔
408
    return TSDB_CODE_INVALID_PARA;
×
409
  }
410
  if (walReaderSeekVer(pReader->pWalReader, ver) < 0) {
958,111✔
411
    return terrno;
23,607✔
412
  }
413
  tqDebug("wal reader seek to ver:%" PRId64 " %s", ver, id);
934,854✔
414
  return 0;
934,854✔
415
}
416

417
static int32_t getTableTagCache(STqReader* pReader, SExprInfo* pExprInfo, int32_t numOfExpr, int64_t uid) {
104,413,366✔
418
  int32_t code = 0;
104,413,366✔
419
  int32_t lino = 0;
104,413,366✔
420

421
  void* data = taosHashGet(pReader->pTableTagCacheForTmq, &uid, LONG_BYTES);
104,413,366✔
422
  if (data == NULL) {
104,474,319✔
423
    SStorageAPI api = {0}; 
38,395,114✔
424
    initStorageAPI(&api);
38,395,750✔
425
    code = cacheTag(pReader->pVnode, pReader->pTableTagCacheForTmq, pExprInfo, numOfExpr, &api, uid, 0, &pReader->tagCachelock);
38,384,805✔
426
    TSDB_CHECK_CODE(code, lino, END);
38,395,109✔
427
  }
428

429
  END:
66,079,205✔
430
  if (code != TSDB_CODE_SUCCESS) {
104,440,873✔
431
    tqError("%s failed at %d, failed to add tbName to response:%s, uid:%"PRId64, __FUNCTION__, lino, tstrerror(code), uid);
×
432
  }
433
  
434
  return code;
104,399,021✔
435
}
436

437
void tqUpdateTableTagCache(STqReader* pReader, SExprInfo* pExprInfo, int32_t numOfExpr, int64_t uid, col_id_t colId) {
535,493✔
438
  int32_t code = 0;
535,493✔
439
  int32_t lino = 0;
535,493✔
440

441
  void* data = taosHashGet(pReader->pTableTagCacheForTmq, &uid, LONG_BYTES);
535,493✔
442
  if (data == NULL) {
535,493✔
443
    return;
534,826✔
444
  }
445

446
  SStorageAPI api = {0}; 
667✔
447
  initStorageAPI(&api);
667✔
448
  code = cacheTag(pReader->pVnode, pReader->pTableTagCacheForTmq, pExprInfo, numOfExpr, &api, uid, colId, &pReader->tagCachelock);
667✔
449
  TSDB_CHECK_CODE(code, lino, END);
667✔
450

451
  END:
667✔
452
  if (code != TSDB_CODE_SUCCESS) {
667✔
453
    tqError("%s failed at %d, failed to update tag cache code:%s, uid:%"PRId64, __FUNCTION__, lino, tstrerror(code), uid);
×
454
  }
455
}
456

457
static int32_t tqRetrievePseudoCols(STqReader* pReader, SSDataBlock* pBlock, int32_t numOfRows, int64_t uid, SExprInfo* pPseudoExpr, int32_t numOfPseudoExpr) {
104,429,770✔
458
  if (pReader == NULL || pBlock == NULL) {
104,429,770✔
459
    return TSDB_CODE_INVALID_PARA;
×
460
  }
461
  int32_t code = TSDB_CODE_SUCCESS;
104,452,081✔
462
  int32_t lino = 0;
104,452,081✔
463
  
464
  code = getTableTagCache(pReader, pPseudoExpr, numOfPseudoExpr, uid);
104,452,081✔
465
  TSDB_CHECK_CODE(code, lino, END);
104,402,204✔
466

467
  code = fillTag(pReader->pTableTagCacheForTmq, pPseudoExpr, numOfPseudoExpr, uid, pBlock, numOfRows, pBlock->info.rows - numOfRows, 1, &pReader->tagCachelock);
104,402,204✔
468
  TSDB_CHECK_CODE(code, lino, END);
104,454,333✔
469

470
END:
104,454,333✔
471
  if (code != 0) {
104,454,333✔
472
    tqError("tqRetrievePseudoCols failed, line:%d, msg:%s", lino, tstrerror(code));
×
473
  }
474
  return code;
104,427,150✔
475
}
476

477
int32_t tqNextBlockInWal(STqReader* pReader, SSDataBlock* pRes, SHashObj* pCol2SlotId, SExprInfo* pPseudoExpr, int32_t numOfPseudoExpr,
24,809,499✔
478
                         int sourceExcluded, int32_t minPollRows, int64_t timeout, int8_t enableReplay) {
479
  int32_t code = 0;
24,809,499✔
480
  if (pReader == NULL) {
24,809,499✔
481
    return TSDB_CODE_INVALID_PARA;
×
482
  }
483
  SWalReader* pWalReader = pReader->pWalReader;
24,809,499✔
484

485
  int64_t st = taosGetTimestampMs();
24,808,795✔
486
  while (1) {
122,695,596✔
487
    code = walNextValidMsg(pWalReader, false);
147,504,391✔
488
    if (code != 0) {
147,472,916✔
489
      break;
24,036,149✔
490
    }
491

492
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
123,436,767✔
493
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
123,499,500✔
494
    int64_t ver = pWalReader->pHead->head.version;
123,495,707✔
495
    SDecoder decoder = {0};
123,478,745✔
496
    code = tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL, &decoder);
123,478,876✔
497
    tDecoderClear(&decoder);
123,480,158✔
498
    if (code != 0) {
123,443,167✔
499
      return code;
×
500
    }
501
    pReader->nextBlk = 0;
123,443,167✔
502

503
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
123,508,571✔
504
    while (pReader->nextBlk < numOfBlocks) {
246,967,783✔
505
      tqDebug("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
123,470,680✔
506
              pReader->msg.ver);
507

508
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
123,528,459✔
509
      if (pSubmitTbData == NULL) {
123,525,142✔
510
        tqError("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
×
511
                pReader->msg.ver);
512
        return terrno;
×
513
      }
514
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
123,525,142✔
UNCOV
515
        pReader->nextBlk += 1;
×
UNCOV
516
        continue;
×
517
      }
518
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
123,522,342✔
519
        tqDebug("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
104,480,855✔
520
        int32_t numOfRows = pRes->info.rows;
104,480,227✔
521
        code = tqRetrieveCols(pReader, pRes, pCol2SlotId);
104,480,227✔
522
        if (code != TSDB_CODE_SUCCESS) {
104,421,825✔
523
          return code;
×
524
        }
525
        code = tqRetrievePseudoCols(pReader, pRes, numOfRows, pSubmitTbData->uid, pPseudoExpr, numOfPseudoExpr);
104,421,825✔
526
        if (code != TSDB_CODE_SUCCESS) {
104,422,760✔
527
          return code;
×
528
        }
529

530
      }
531
      pReader->nextBlk += 1;
123,467,408✔
532
    }
533

534
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
123,431,163✔
535
    pReader->msg.msgStr = NULL;
123,448,343✔
536

537
    if (pRes->info.rows >= minPollRows || (enableReplay && pRes->info.rows > 0)){
123,463,540✔
538
      break;
539
    }
540
    int64_t elapsed = taosGetTimestampMs() - st;
122,906,860✔
541
    if (elapsed > timeout || elapsed < 0) {
122,906,860✔
542
      code = TSDB_CODE_TMQ_FETCH_TIMEOUT;
240,904✔
543
      terrno = code;
240,904✔
544
      break;
181,300✔
545
    }
546
  }
547
  return code;
24,808,780✔
548
}
549

550
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList, SDecoder* decoder) {
170,123,677✔
551
  if (pReader == NULL) {
170,123,677✔
552
    return TSDB_CODE_INVALID_PARA;
×
553
  }
554
  pReader->msg.msgStr = msgStr;
170,123,677✔
555
  pReader->msg.msgLen = msgLen;
170,138,819✔
556
  pReader->msg.ver = ver;
170,173,451✔
557

558
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
170,168,286✔
559

560
  tDecoderInit(decoder, pReader->msg.msgStr, pReader->msg.msgLen);
170,168,286✔
561
  int32_t code = tDecodeSubmitReq(decoder, &pReader->submit, rawList);
170,176,151✔
562

563
  if (code != 0) {
170,184,235✔
564
    tqError("DecodeSSubmitReq2 error, msgLen:%d, ver:%" PRId64, msgLen, ver);
×
565
  }
566

567
  return code;
170,149,235✔
568
}
569

570
void tqReaderClearSubmitMsg(STqReader* pReader) {
93,294,546✔
571
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
93,294,546✔
572
  pReader->nextBlk = 0;
93,311,724✔
573
  pReader->msg.msgStr = NULL;
93,322,418✔
574
}
93,334,949✔
575

576
SWalReader* tqGetWalReader(STqReader* pReader) {
50,412,681✔
577
  if (pReader == NULL) {
50,412,681✔
578
    return NULL;
×
579
  }
580
  return pReader->pWalReader;
50,412,681✔
581
}
582

583
int64_t tqGetResultBlockTime(STqReader* pReader) {
24,809,149✔
584
  if (pReader == NULL) {
24,809,149✔
585
    return 0;
×
586
  }
587
  return pReader->lastTs;
24,809,149✔
588
}
589

590
bool tqNextBlockImpl(STqReader* pReader, const char* idstr) {
23,888,352✔
591
  int32_t code = false;
23,888,352✔
592
  int32_t lino = 0;
23,888,352✔
593
  int64_t uid = 0;
23,888,352✔
594
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
23,888,352✔
595
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
23,888,352✔
596
  TSDB_CHECK_NULL(pReader->tbIdHash, code, lino, END, true);
23,890,112✔
597

598
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
23,890,464✔
599
  while (pReader->nextBlk < blockSz) {
25,077,890✔
600
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
12,540,639✔
601
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
12,540,991✔
602
    uid = pSubmitTbData->uid;
12,540,991✔
603
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
12,540,991✔
604
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
12,540,917✔
605

606
    tqTrace("iterator data block in hash continue, progress:%d/%d, total queried tables:%d, uid:%" PRId64,
1,186,722✔
607
            pReader->nextBlk, blockSz, taosHashGetSize(pReader->tbIdHash), uid);
608
    pReader->nextBlk++;
1,186,722✔
609
  }
610

611
  tqReaderClearSubmitMsg(pReader);
12,535,417✔
612
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
12,536,269✔
613

614
END:
12,536,269✔
615
  tqTrace("%s:%d return:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
23,890,464✔
616
  return code;
23,888,556✔
617
}
618

619
bool tqNextDataBlockFilterOut(STqReader* pReader, SHashObj* filterOutUids) {
68,235,941✔
620
  int32_t code = false;
68,235,941✔
621
  int32_t lino = 0;
68,235,941✔
622
  int64_t uid = 0;
68,235,941✔
623

624
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
68,235,941✔
625
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
68,235,941✔
626
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
68,254,161✔
627

628
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
68,254,161✔
629
  while (pReader->nextBlk < blockSz) {
68,277,172✔
630
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
34,140,696✔
631
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
34,145,901✔
632
    uid = pSubmitTbData->uid;
34,145,901✔
633
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
34,145,901✔
634
    TSDB_CHECK_NULL(ret, code, lino, END, true);
34,144,988✔
635
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64, pReader->nextBlk, blockSz, uid);
×
636
    pReader->nextBlk++;
×
637
  }
638
  tqReaderClearSubmitMsg(pReader);
34,131,307✔
639
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
34,126,272✔
640

641
END:
34,126,272✔
642
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
68,271,260✔
643
  return code;
68,219,507✔
644
}
645

646
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask,
45,237,544✔
647
                    SExtSchema* extSrc) {
648
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
45,237,544✔
649
    return TSDB_CODE_INVALID_PARA;
×
650
  }
651
  int32_t code = 0;
45,264,962✔
652

653
  int32_t cnt = 0;
45,264,962✔
654
  for (int32_t i = 0; i < pSrc->nCols; i++) {
253,673,828✔
655
    cnt += mask[i];
208,396,831✔
656
  }
657

658
  pDst->nCols = cnt;
45,269,532✔
659
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
45,289,241✔
660
  if (pDst->pSchema == NULL) {
45,257,546✔
661
    return TAOS_GET_TERRNO(terrno);
×
662
  }
663

664
  int32_t j = 0;
45,248,198✔
665
  for (int32_t i = 0; i < pSrc->nCols; i++) {
253,715,913✔
666
    if (mask[i]) {
208,388,332✔
667
      pDst->pSchema[j++] = pSrc->pSchema[i];
208,436,628✔
668
      SColumnInfoData colInfo =
208,443,994✔
669
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
208,448,737✔
670
      if (extSrc != NULL) {
208,416,111✔
UNCOV
671
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
×
672
      }
673
      code = blockDataAppendColInfo(pBlock, &colInfo);
208,416,111✔
674
      if (code != 0) {
208,444,082✔
675
        return code;
×
676
      }
677
    }
678
  }
679
  return 0;
45,295,058✔
680
}
681

682
static int32_t doSetBlobVal(SColumnInfoData* pColumnInfoData, int32_t idx, SColVal* pColVal, SBlobSet* pBlobRow2) {
×
683
  int32_t code = 0;
×
684
  if (pColumnInfoData == NULL || pColVal == NULL || pBlobRow2 == NULL) {
×
685
    return TSDB_CODE_INVALID_PARA;
×
686
  }
687
  // TODO(yhDeng)
688
  if (COL_VAL_IS_VALUE(pColVal)) {
×
689
    char* val = taosMemCalloc(1, pColVal->value.nData + sizeof(BlobDataLenT));
×
690
    if (val == NULL) {
×
691
      return terrno;
×
692
    }
693

694
    uint64_t seq = 0;
×
695
    int32_t  len = 0;
×
696
    if (pColVal->value.pData != NULL) {
×
697
      if (tGetU64(pColVal->value.pData, &seq) < 0){
×
698
        TAOS_CHECK_RETURN(TSDB_CODE_INVALID_PARA);
×
699
      }
700
      SBlobItem item = {0};
×
701
      code = tBlobSetGet(pBlobRow2, seq, &item);
×
702
      if (code != 0) {
×
703
        taosMemoryFree(val);
×
704
        terrno = code;
×
705
        uError("tq set blob val, idx:%d, get blob item failed, seq:%" PRIu64 ", code:%d", idx, seq, code);
×
706
        return code;
×
707
      }
708

709
      val = taosMemRealloc(val, item.len + sizeof(BlobDataLenT));
×
710
      (void)memcpy(blobDataVal(val), item.data, item.len);
×
711
      len = item.len;
×
712
    }
713

714
    blobDataSetLen(val, len);
×
715
    code = colDataSetVal(pColumnInfoData, idx, val, false);
×
716

717
    taosMemoryFree(val);
×
718
  } else {
719
    colDataSetNULL(pColumnInfoData, idx);
×
720
  }
721
  return code;
×
722
}
723
static int32_t doSetVal(SColumnInfoData* pColumnInfoData, int32_t rowIndex, SColVal* pColVal) {
2,147,483,647✔
724
  int32_t code = TSDB_CODE_SUCCESS;
2,147,483,647✔
725

726
  if (IS_VAR_DATA_TYPE(pColVal->value.type)) {
2,147,483,647✔
727
    if (COL_VAL_IS_VALUE(pColVal)) {
2,147,483,647✔
728
      char val[65535 + 2] = {0};
2,147,483,647✔
729
      if (pColVal->value.pData != NULL) {
2,147,483,647✔
730
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
2,147,483,647✔
731
      }
732
      varDataSetLen(val, pColVal->value.nData);
2,147,483,647✔
733
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
2,147,483,647✔
734
    } else {
735
      colDataSetNULL(pColumnInfoData, rowIndex);
×
736
    }
737
  } else {
738
    code = colDataSetVal(pColumnInfoData, rowIndex, VALUE_GET_DATUM(&pColVal->value, pColVal->value.type),
2,147,483,647✔
739
                         !COL_VAL_IS_VALUE(pColVal));
2,147,483,647✔
740
  }
741

742
  return code;
2,147,483,647✔
743
}
744

745
static int32_t setBlockData(SSDataBlock* pBlock, int32_t slotId, int32_t rowIndex, SColVal* colVal, SBlobSet* pBlobSet) {
2,147,483,647✔
746
  int32_t        code = 0;
2,147,483,647✔
747
  SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, slotId);
2,147,483,647✔
748
  if (pColData == NULL) {
2,147,483,647✔
749
    return terrno;
×
750
  }
751

752
  uint8_t isBlob = IS_STR_DATA_BLOB(pColData->info.type) ? 1 : 0;
2,147,483,647✔
753
  if (isBlob == 0) {
2,147,483,647✔
754
    code = doSetVal(pColData, rowIndex, colVal);
2,147,483,647✔
755
  } else {
756
    code = doSetBlobVal(pColData, rowIndex, colVal, pBlobSet);
×
757
  }
758
  return code;
2,147,483,647✔
759
}
760

761
static int32_t processSubmitRow(SArray*         pRows,
104,460,361✔
762
                                SSDataBlock*    pBlock,
763
                                SHashObj*       pCol2SlotId,
764
                                STqReader*      pReader,
765
                                SBlobSet*       pBlobSet) {
766
  int32_t        code = 0;
104,460,361✔
767
  int32_t        line = 0;
104,460,361✔
768

769
  SArray* pColArray = taosArrayInit(4, INT_BYTES * 2);
104,460,361✔
770
  TSDB_CHECK_NULL(pColArray, code, line, END, terrno);
104,479,515✔
771

772
  int32_t sourceIdx = -1;
104,479,515✔
773
  int32_t rowIndex = 0;
104,479,515✔
774
  SRow* pRow = taosArrayGetP(pRows, rowIndex);
104,479,515✔
775
  TSDB_CHECK_NULL(pRow, code, line, END, terrno);
104,475,665✔
776
  while (++sourceIdx < pReader->pTSchema->numOfCols) {
856,497,394✔
777
    SColVal colVal = {0};
752,180,520✔
778
    code = tRowGet(pRow, pReader->pTSchema, sourceIdx, &colVal);
752,135,791✔
779
    TSDB_CHECK_CODE(code, line, END);
752,224,720✔
780
    void* pSlotId = taosHashGet(pCol2SlotId, &colVal.cid, sizeof(colVal.cid));
752,224,720✔
781
    if (pSlotId == NULL) {
752,503,366✔
782
      continue;
273,572,053✔
783
    }
784
    int32_t pData[2] = {sourceIdx, *(int16_t*)pSlotId};
478,931,313✔
785
    TSDB_CHECK_NULL(taosArrayPush(pColArray, pData), code, line, END, terrno);
478,889,353✔
786
    code = setBlockData(pBlock, pData[1], pBlock->info.rows + rowIndex, &colVal, pBlobSet);
478,889,353✔
787
    TSDB_CHECK_CODE(code, line, END);
478,592,898✔
788
  }
789
  
790
  for (rowIndex = 1; rowIndex < taosArrayGetSize(pRows); rowIndex++) {
2,147,483,647✔
791
    SRow* pRow = taosArrayGetP(pRows, rowIndex);
2,147,483,647✔
792
    TSDB_CHECK_NULL(pRow, code, line, END, terrno);
2,147,483,647✔
793
    for (int32_t j = 0; j < taosArrayGetSize(pColArray); j++) {
2,147,483,647✔
794
      int32_t* pData = taosArrayGet(pColArray, j);
2,147,483,647✔
795
      TSDB_CHECK_NULL(pData, code, line, END, terrno);
2,147,483,647✔
796

797
      SColVal colVal = {0};
2,147,483,647✔
798
      code = tRowGet(pRow, pReader->pTSchema, pData[0], &colVal);
2,147,483,647✔
799
      TSDB_CHECK_CODE(code, line, END);
2,147,483,647✔
800

801
      code = setBlockData(pBlock, pData[1], pBlock->info.rows + rowIndex, &colVal, pBlobSet);
2,147,483,647✔
802
      TSDB_CHECK_CODE(code, line, END);
2,147,483,647✔
803
    }
804
  }
805

806
  END:
101,416,202✔
807
  taosArrayDestroy(pColArray);
98,473,002✔
808
  return code;
104,454,725✔
809
}
810

811
static int32_t processSubmitCol(SArray*         pCols,
1,340✔
812
                                SSDataBlock*    pBlock,
813
                                SHashObj*       pCol2SlotId,
814
                                SBlobSet*       pBlobSet) {
815
  int32_t        code = 0;
1,340✔
816
  int32_t        line = 0;
1,340✔
817

818
  for (int32_t i = 0; i < taosArrayGetSize(pCols); i++) {
4,020✔
819
    SColData* pCol = taosArrayGet(pCols, i);
2,680✔
820
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
2,680✔
821
    void* pSlotId = taosHashGet(pCol2SlotId, &pCol->cid, sizeof(pCol->cid));
2,680✔
822
    if (pSlotId == NULL) {
2,680✔
823
      continue;
1,340✔
824
    }
825
    SColVal colVal = {0};
1,340✔
826
    for (int32_t row = 0; row < pCol->nVal; row++) {
4,020✔
827
      code = tColDataGetValue(pCol, row, &colVal);
2,680✔
828
      TSDB_CHECK_CODE(code, line, END);
2,680✔
829

830
      code = setBlockData(pBlock, *(int16_t*)pSlotId, pBlock->info.rows + row, &colVal, pBlobSet);
2,680✔
831
      TSDB_CHECK_CODE(code, line, END);
2,680✔
832
    }
833
  }
834
  
835
  END:
1,340✔
836
  return code;
1,340✔
837
}
838

839
static int32_t checkSchema(STqReader* pReader, SSubmitTbData* pSubmitTbData) {
104,469,283✔
840
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
104,469,283✔
841
  int32_t sversion = pSubmitTbData->sver;
104,479,433✔
842
  int64_t suid = pSubmitTbData->suid;
104,481,205✔
843
  int64_t uid = pSubmitTbData->uid;
104,477,685✔
844
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
104,480,485✔
845
      (pReader->cachedSchemaVer != sversion)) {
104,336,250✔
846
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
147,735✔
847
    taosMemoryFreeClear(pReader->extSchema);
145,635✔
848
    taosMemoryFreeClear(pReader->pTSchema);
145,635✔
849
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnode->pMeta, uid, sversion, 1, &pReader->extSchema, 0);
145,635✔
850
    if (pReader->pSchemaWrapper == NULL) {
145,635✔
851
      tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", uid:%" PRId64 ",version %d, possibly dropped table",
×
852
              vgId, suid, uid, pReader->cachedSchemaVer);
853
      return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
×
854
    }
855
    pReader->pTSchema = tBuildTSchema(pReader->pSchemaWrapper->pSchema, pReader->pSchemaWrapper->nCols, pReader->pSchemaWrapper->version);
145,331✔
856
    if (pReader->pTSchema == NULL) {
145,357✔
857
      tqWarn("vgId:%d, cannot build schema for table: suid:%" PRId64 ", uid:%" PRId64 ",version %d",
×
858
              vgId, suid, uid, pReader->cachedSchemaVer);
859
      return terrno;
×
860
    }
861
    pReader->cachedSchemaUid = uid;
145,635✔
862
    pReader->cachedSchemaSuid = suid;
145,357✔
863
    pReader->cachedSchemaVer = sversion;
145,331✔
864
  }
865
  return TSDB_CODE_SUCCESS;
104,476,733✔
866
}
867

868
static int32_t tqRetrieveCols(STqReader* pReader, SSDataBlock* pBlock, SHashObj* pCol2SlotId) {
104,443,475✔
869
  if (pReader == NULL || pBlock == NULL) {
104,443,475✔
870
    return TSDB_CODE_INVALID_PARA;
×
871
  }
872
  tqDebug("tq reader retrieve data block %p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
104,480,577✔
873
  int32_t        code = 0;
104,480,505✔
874
  int32_t        line = 0;
104,480,505✔
875
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
104,480,505✔
876
  TSDB_CHECK_NULL(pSubmitTbData, code, line, END, terrno);
104,470,005✔
877
  pReader->lastTs = pSubmitTbData->ctimeMs;
104,470,005✔
878

879
  int32_t numOfRows = 0;
104,482,255✔
880
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
104,482,255✔
881
    SColData* pCol = taosArrayGet(pSubmitTbData->aCol, 0);
1,340✔
882
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
1,340✔
883
    numOfRows = pCol->nVal;
1,340✔
884
  } else {
885
    numOfRows = taosArrayGetSize(pSubmitTbData->aRowP);
104,452,899✔
886
  }
887

888
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows + numOfRows);
104,481,905✔
889
  TSDB_CHECK_CODE(code, line, END);
104,477,665✔
890

891
  code = checkSchema(pReader, pSubmitTbData);
104,477,665✔
892
  TSDB_CHECK_CODE(code, line, END);
104,468,961✔
893

894
  // convert and scan one block
895
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
104,468,961✔
896
    SArray* pCols = pSubmitTbData->aCol;
1,340✔
897
    code = processSubmitCol(pCols, pBlock, pCol2SlotId, pSubmitTbData->pBlobSet);
1,340✔
898
    TSDB_CHECK_CODE(code, line, END);
1,340✔
899
  } else {
900
    SArray*         pRows = pSubmitTbData->aRowP;
104,478,821✔
901
    code = processSubmitRow(pRows, pBlock, pCol2SlotId, pReader, pSubmitTbData->pBlobSet);
104,476,719✔
902
    TSDB_CHECK_CODE(code, line, END);
104,434,156✔
903
  }
904
  pBlock->info.rows += numOfRows;
104,435,496✔
905
END:
104,456,913✔
906
  if (code != 0) {
104,456,913✔
907
    tqError("tqRetrieveCols failed, line:%d, msg:%s", line, tstrerror(code));
×
908
  }
909
  return code;
104,435,717✔
910
}
911

912
#define PROCESS_VAL                                      \
913
  if (curRow == 0) {                                     \
914
    assigned[j] = !COL_VAL_IS_NONE(&colVal);             \
915
    buildNew = true;                                     \
916
  } else {                                               \
917
    bool currentRowAssigned = !COL_VAL_IS_NONE(&colVal); \
918
    if (currentRowAssigned != assigned[j]) {             \
919
      assigned[j] = currentRowAssigned;                  \
920
      buildNew = true;                                   \
921
    }                                                    \
922
  }
923

924
#define SET_DATA                                                                                    \
925
  if (colVal.cid < pColData->info.colId) {                                                          \
926
    sourceIdx++;                                                                                    \
927
  } else if (colVal.cid == pColData->info.colId) {                                                  \
928
    if (IS_STR_DATA_BLOB(pColData->info.type)) {                                                    \
929
      TQ_ERR_GO_TO_END(doSetBlobVal(pColData, curRow - lastRow, &colVal, pSubmitTbData->pBlobSet)); \
930
    } else {                                                                                        \
931
      TQ_ERR_GO_TO_END(doSetVal(pColData, curRow - lastRow, &colVal));                              \
932
    }                                                                                               \
933
    sourceIdx++;                                                                                    \
934
    targetIdx++;                                                                                    \
935
  } else {                                                                                          \
936
    colDataSetNULL(pColData, curRow - lastRow);                                                     \
937
    targetIdx++;                                                                                    \
938
  }
939

940
static int32_t processBuildNew(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas,
45,233,888✔
941
                               char* assigned, int32_t numOfRows, int32_t curRow, int32_t* lastRow) {
942
  int32_t         code = 0;
45,233,888✔
943
  SSchemaWrapper* pSW = NULL;
45,233,888✔
944
  SSDataBlock*    block = NULL;
45,251,379✔
945
  if (taosArrayGetSize(blocks) > 0) {
45,251,379✔
946
    SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
×
947
    TQ_NULL_GO_TO_END(pLastBlock);
×
948
    pLastBlock->info.rows = curRow - *lastRow;
×
949
    *lastRow = curRow;
×
950
  }
951

952
  block = taosMemoryCalloc(1, sizeof(SSDataBlock));
45,264,192✔
953
  TQ_NULL_GO_TO_END(block);
45,225,500✔
954

955
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
45,225,500✔
956
  TQ_NULL_GO_TO_END(pSW);
45,234,794✔
957

958
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pReader->pSchemaWrapper, assigned, pReader->extSchema));
45,234,794✔
959
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
45,294,094✔
960
          (int32_t)taosArrayGetSize(block->pDataBlock));
961

962
  block->info.id.uid = pSubmitTbData->uid;
45,294,094✔
963
  block->info.version = pReader->msg.ver;
45,281,637✔
964
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
45,289,070✔
965
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
45,283,904✔
966
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
45,283,104✔
967
  pSW = NULL;
45,283,104✔
968

969
  taosMemoryFreeClear(block);
45,283,104✔
970

971
END:
45,290,233✔
972
  if (code != 0) {
45,281,834✔
973
    tqError("processBuildNew failed, code:%d", code);
×
974
  }
975
  tDeleteSchemaWrapper(pSW);
45,281,834✔
976
  blockDataFreeRes(block);
45,248,809✔
977
  taosMemoryFree(block);
45,252,805✔
978
  return code;
45,273,564✔
979
}
980
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
838,017✔
981
  int32_t code = 0;
838,017✔
982
  int32_t curRow = 0;
838,017✔
983
  int32_t lastRow = 0;
838,017✔
984

985
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
838,017✔
986
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
838,344✔
987
  TQ_NULL_GO_TO_END(assigned);
838,344✔
988

989
  SArray*   pCols = pSubmitTbData->aCol;
838,344✔
990
  SColData* pCol = taosArrayGet(pCols, 0);
838,344✔
991
  TQ_NULL_GO_TO_END(pCol);
838,017✔
992
  int32_t numOfRows = pCol->nVal;
838,017✔
993
  int32_t numOfCols = taosArrayGetSize(pCols);
838,344✔
994
  tqTrace("vgId:%d, tqProcessColData start, col num: %d, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfCols,
838,344✔
995
          numOfRows);
996
  for (int32_t i = 0; i < numOfRows; i++) {
113,997,905✔
997
    bool buildNew = false;
113,101,936✔
998

999
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
452,370,842✔
1000
      int32_t k = 0;
337,678,978✔
1001
      for (; k < numOfCols; k++) {
672,845,800✔
1002
        pCol = taosArrayGet(pCols, k);
665,460,505✔
1003
        TQ_NULL_GO_TO_END(pCol);
663,649,703✔
1004
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
663,649,703✔
1005
          SColVal colVal = {0};
337,081,001✔
1006
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
337,790,930✔
1007
          PROCESS_VAL
339,839,833✔
1008
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
339,911,426✔
1009
          break;
337,621,820✔
1010
        }
1011
      }
1012
      if (k >= numOfCols) {
339,268,906✔
1013
        // this column is not in the current row, so we set it to NULL
1014
        assigned[j] = 0;
×
1015
        buildNew = true;
×
1016
      }
1017
    }
1018

1019
    if (buildNew) {
111,827,512✔
1020
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
838,344✔
1021
    }
1022

1023
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
111,827,512✔
1024
    TQ_NULL_GO_TO_END(pBlock);
113,230,025✔
1025

1026
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
113,230,025✔
1027
            (int32_t)taosArrayGetSize(blocks));
1028

1029
    int32_t targetIdx = 0;
113,230,025✔
1030
    int32_t sourceIdx = 0;
113,230,025✔
1031
    int32_t colActual = blockDataGetNumOfCols(pBlock);
113,230,025✔
1032
    while (targetIdx < colActual && sourceIdx < numOfCols) {
451,126,959✔
1033
      pCol = taosArrayGet(pCols, sourceIdx);
337,967,398✔
1034
      TQ_NULL_GO_TO_END(pCol);
337,107,090✔
1035
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
337,107,090✔
1036
      TQ_NULL_GO_TO_END(pColData);
334,271,256✔
1037
      SColVal colVal = {0};
334,271,256✔
1038
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
336,910,933✔
1039
      SET_DATA
339,718,421✔
1040
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
338,786,946✔
1041
    }
1042

1043
    curRow++;
113,159,561✔
1044
  }
1045
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
895,969✔
1046
  pLastBlock->info.rows = curRow - lastRow;
838,344✔
1047
  tqTrace("vgId:%d, tqProcessColData end, col num: %d, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId,
838,344✔
1048
          numOfCols, numOfRows, (int)taosArrayGetSize(blocks));
1049
END:
7,774,562✔
1050
  if (code != TSDB_CODE_SUCCESS) {
838,344✔
1051
    tqError("vgId:%d, process col data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1052
  }
1053
  taosMemoryFree(assigned);
838,344✔
1054
  return code;
838,344✔
1055
}
1056

1057
int32_t tqProcessRowData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
44,374,027✔
1058
  int32_t   code = 0;
44,374,027✔
1059
  STSchema* pTSchema = NULL;
44,374,027✔
1060

1061
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
44,374,027✔
1062
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
44,404,868✔
1063
  TQ_NULL_GO_TO_END(assigned);
44,394,195✔
1064

1065
  int32_t curRow = 0;
44,394,195✔
1066
  int32_t lastRow = 0;
44,394,195✔
1067
  SArray* pRows = pSubmitTbData->aRowP;
44,382,192✔
1068
  int32_t numOfRows = taosArrayGetSize(pRows);
44,424,878✔
1069
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
44,419,682✔
1070
  TQ_NULL_GO_TO_END(pTSchema);
44,443,082✔
1071
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
44,443,082✔
1072

1073
  for (int32_t i = 0; i < numOfRows; i++) {
1,288,135,593✔
1074
    bool  buildNew = false;
1,243,609,165✔
1075
    SRow* pRow = taosArrayGetP(pRows, i);
1,243,609,165✔
1076
    TQ_NULL_GO_TO_END(pRow);
1,244,171,406✔
1077

1078
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
2,147,483,647✔
1079
      SColVal colVal = {0};
2,147,483,647✔
1080
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
2,147,483,647✔
1081
      PROCESS_VAL
2,147,483,647✔
1082
      tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], pTSchema->numOfCols);
2,147,483,647✔
1083
    }
1084

1085
    if (buildNew) {
1,237,397,734✔
1086
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
44,454,452✔
1087
    }
1088

1089
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
1,237,377,365✔
1090
    TQ_NULL_GO_TO_END(pBlock);
1,244,824,549✔
1091

1092
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
1,244,824,549✔
1093
            (int32_t)taosArrayGetSize(blocks));
1094

1095
    int32_t targetIdx = 0;
1,244,824,549✔
1096
    int32_t sourceIdx = 0;
1,244,824,549✔
1097
    int32_t colActual = blockDataGetNumOfCols(pBlock);
1,244,824,549✔
1098
    while (targetIdx < colActual && sourceIdx < pTSchema->numOfCols) {
2,147,483,647✔
1099
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
2,147,483,647✔
1100
      TQ_NULL_GO_TO_END(pColData);
2,147,483,647✔
1101
      SColVal          colVal = {0};
2,147,483,647✔
1102
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
2,147,483,647✔
1103
      SET_DATA
2,147,483,647✔
1104
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
2,147,483,647✔
1105
    }
1106

1107
    curRow++;
1,243,739,102✔
1108
  }
1109
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
44,526,428✔
1110
  if (pLastBlock != NULL) {
44,444,487✔
1111
    pLastBlock->info.rows = curRow - lastRow;
44,447,547✔
1112
  }
1113

1114
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
44,451,275✔
1115
          (int)taosArrayGetSize(blocks));
1116
END:
48,866,037✔
1117
  if (code != TSDB_CODE_SUCCESS) {
44,424,075✔
1118
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1119
  }
1120
  taosMemoryFreeClear(pTSchema);
44,418,201✔
1121
  taosMemoryFree(assigned);
44,396,037✔
1122
  return code;
44,434,499✔
1123
}
1124

1125
static int32_t buildCreateTbInfo(SMqDataRsp* pRsp, SVCreateTbReq* pCreateTbReq) {
2,021✔
1126
  int32_t code = 0;
2,021✔
1127
  int32_t lino = 0;
2,021✔
1128
  void*   createReq = NULL;
2,021✔
1129
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
2,021✔
1130
  TSDB_CHECK_NULL(pCreateTbReq, code, lino, END, TSDB_CODE_INVALID_PARA);
2,021✔
1131

1132
  if (pRsp->createTableNum == 0) {
2,021✔
1133
    pRsp->createTableLen = taosArrayInit(0, sizeof(int32_t));
1,669✔
1134
    TSDB_CHECK_NULL(pRsp->createTableLen, code, lino, END, terrno);
1,669✔
1135
    pRsp->createTableReq = taosArrayInit(0, sizeof(void*));
1,669✔
1136
    TSDB_CHECK_NULL(pRsp->createTableReq, code, lino, END, terrno);
1,669✔
1137
  }
1138

1139
  uint32_t len = 0;
2,021✔
1140
  tEncodeSize(tEncodeSVCreateTbReq, pCreateTbReq, len, code);
2,021✔
1141
  TSDB_CHECK_CODE(code, lino, END);
2,021✔
1142
  createReq = taosMemoryCalloc(1, len);
2,021✔
1143
  TSDB_CHECK_NULL(createReq, code, lino, END, terrno);
2,021✔
1144

1145
  SEncoder encoder = {0};
2,021✔
1146
  tEncoderInit(&encoder, createReq, len);
2,021✔
1147
  code = tEncodeSVCreateTbReq(&encoder, pCreateTbReq);
2,021✔
1148
  tEncoderClear(&encoder);
2,021✔
1149
  TSDB_CHECK_CODE(code, lino, END);
2,021✔
1150
  TSDB_CHECK_NULL(taosArrayPush(pRsp->createTableLen, &len), code, lino, END, terrno);
4,042✔
1151
  TSDB_CHECK_NULL(taosArrayPush(pRsp->createTableReq, &createReq), code, lino, END, terrno);
4,042✔
1152
  pRsp->createTableNum++;
2,021✔
1153
  tqTrace("build create table info msg success");
2,021✔
1154

1155
END:
2,021✔
1156
  if (code != 0) {
2,021✔
1157
    tqError("%s failed at %d, failed to build create table info msg:%s", __FUNCTION__, lino, tstrerror(code));
×
1158
    taosMemoryFree(createReq);
×
1159
  }
1160
  return code;
2,021✔
1161
}
1162

1163
int32_t tqRetrieveTaosxBlock(STqReader* pReader, SMqDataRsp* pRsp, SArray* blocks, SArray* schemas,
45,491,593✔
1164
                             SSubmitTbData** pSubmitTbDataRet, SArray* rawList, int8_t fetchMeta) {
1165
  tqTrace("tq reader retrieve data block msg pointer:%p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
45,491,593✔
1166
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
45,491,593✔
1167
  if (pSubmitTbData == NULL) {
45,500,508✔
1168
    return terrno;
×
1169
  }
1170
  pReader->nextBlk++;
45,500,508✔
1171

1172
  if (pSubmitTbDataRet) {
45,495,273✔
1173
    *pSubmitTbDataRet = pSubmitTbData;
45,498,978✔
1174
  }
1175

1176
  if (fetchMeta == ONLY_META) {
45,496,191✔
1177
    if (pSubmitTbData->pCreateTbReq != NULL) {
918✔
1178
      if (pRsp->createTableReq == NULL) {
918✔
1179
        pRsp->createTableReq = taosArrayInit(0, POINTER_BYTES);
918✔
1180
        if (pRsp->createTableReq == NULL) {
918✔
1181
          return terrno;
×
1182
        }
1183
      }
1184
      if (taosArrayPush(pRsp->createTableReq, &pSubmitTbData->pCreateTbReq) == NULL) {
1,836✔
1185
        return terrno;
×
1186
      }
1187
      pSubmitTbData->pCreateTbReq = NULL;
918✔
1188
    }
1189
    return 0;
918✔
1190
  }
1191

1192
  int32_t sversion = pSubmitTbData->sver;
45,495,273✔
1193
  int64_t uid = pSubmitTbData->uid;
45,495,319✔
1194
  pReader->lastBlkUid = uid;
45,489,770✔
1195

1196
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
45,498,106✔
1197
  taosMemoryFreeClear(pReader->extSchema);
45,478,870✔
1198
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnode->pMeta, uid, sversion, 1, &pReader->extSchema, 0);
45,491,942✔
1199
  if (pReader->pSchemaWrapper == NULL) {
45,455,919✔
1200
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
206,086✔
1201
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1202
    pReader->cachedSchemaSuid = 0;
206,086✔
1203
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
206,414✔
1204
  }
1205

1206
  if (pSubmitTbData->pCreateTbReq != NULL) {
45,246,674✔
1207
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
2,021✔
1208
    if (code != 0) {
2,021✔
1209
      return code;
×
1210
    }
1211
  } else if (rawList != NULL) {
45,249,279✔
1212
    if (taosArrayPush(schemas, &pReader->pSchemaWrapper) == NULL) {
×
1213
      return terrno;
×
1214
    }
1215
    pReader->pSchemaWrapper = NULL;
×
1216
    return 0;
×
1217
  }
1218

1219
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
45,251,300✔
1220
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
838,017✔
1221
  } else {
1222
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
44,379,936✔
1223
  }
1224
}
1225

1226
int32_t tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
347,278✔
1227
  if (pReader == NULL || tbUidList == NULL) {
347,278✔
1228
    return TSDB_CODE_SUCCESS;
×
1229
  }
1230
  if (pReader->tbIdHash) {
347,278✔
1231
    taosHashClear(pReader->tbIdHash);
×
1232
  } else {
1233
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
347,278✔
1234
    if (pReader->tbIdHash == NULL) {
347,278✔
1235
      tqError("s-task:%s failed to init hash table", id);
×
1236
      return terrno;
×
1237
    }
1238
  }
1239

1240
  for (int i = 0; i < taosArrayGetSize(tbUidList); i++) {
9,877,560✔
1241
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
9,528,438✔
1242
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
9,519,726✔
1243
      tqError("s-task:%s failed to add table uid:%" PRId64 " to hash", id, *pKey);
×
1244
      continue;
×
1245
    }
1246
  }
1247

1248
  tqDebug("s-task:%s %d tables are set to be queried target table", id, (int32_t)taosArrayGetSize(tbUidList));
347,278✔
1249
  return TSDB_CODE_SUCCESS;
347,278✔
1250
}
1251

1252
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
409,929✔
1253
  if (pReader == NULL || pTableUidList == NULL) {
409,929✔
1254
    return;
×
1255
  }
1256
  if (pReader->tbIdHash == NULL) {
409,929✔
1257
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
×
1258
    if (pReader->tbIdHash == NULL) {
×
1259
      tqError("failed to init hash table");
×
1260
      return;
×
1261
    }
1262
  }
1263

1264
  int32_t numOfTables = taosArrayGetSize(pTableUidList);
409,929✔
1265
  for (int i = 0; i < numOfTables; i++) {
828,805✔
1266
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
418,876✔
1267
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
418,876✔
1268
      tqError("failed to add table uid:%" PRId64 " to hash", *pKey);
×
1269
      continue;
×
1270
    }
1271
    tqDebug("%s add table uid:%" PRId64 " to hash", __func__, *pKey);
418,876✔
1272
  }
1273
}
1274

1275
bool tqReaderIsQueriedTable(STqReader* pReader, uint64_t uid) {
×
1276
  if (pReader == NULL) {
×
1277
    return false;
×
1278
  }
1279
  return taosHashGet(pReader->tbIdHash, &uid, sizeof(uint64_t)) != NULL;
×
1280
}
1281

1282
bool tqCurrentBlockConsumed(const STqReader* pReader) {
×
1283
  if (pReader == NULL) {
×
1284
    return false;
×
1285
  }
1286
  return pReader->msg.msgStr == NULL;
×
1287
}
1288

1289
void tqReaderRemoveTbUidList(STqReader* pReader, const SArray* tbUidList) {
1,050✔
1290
  if (pReader == NULL || tbUidList == NULL) {
1,050✔
1291
    return;
×
1292
  }
1293
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
2,100✔
1294
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
1,050✔
1295
    int32_t code = taosHashRemove(pReader->tbIdHash, pKey, sizeof(int64_t));
1,050✔
1296
    if (code != 0) {
1,050✔
1297
      tqWarn("%s failed to remove table uid:%" PRId64 " from hash, msg:%s", __func__, pKey != NULL ? *pKey : 0, tstrerror(code));
350✔
1298
    }
1299
  }
1300
}
1301

1302
int32_t tqDeleteTbUidList(STQ* pTq, SArray* tbUidList) {
2,429,982✔
1303
  if (pTq == NULL) {
2,429,982✔
1304
    return 0;  // mounted vnode may have no tq
×
1305
  }
1306
  if (tbUidList == NULL) {
2,429,982✔
1307
    return TSDB_CODE_INVALID_PARA;
×
1308
  }
1309
  void*   pIter = NULL;
2,429,982✔
1310
  int32_t vgId = TD_VID(pTq->pVnode);
2,429,982✔
1311

1312
  // update the table list for each consumer handle
1313
  taosWLockLatch(&pTq->lock);
2,430,564✔
1314
  while (1) {
325,268✔
1315
    pIter = taosHashIterate(pTq->pHandle, pIter);
2,755,832✔
1316
    if (pIter == NULL) {
2,755,832✔
1317
      break;
2,430,564✔
1318
    }
1319

1320
    STqHandle* pTqHandle = (STqHandle*)pIter;
325,268✔
1321
    tqDebug("%s subKey:%s, consumer:0x%" PRIx64 " delete table list", __func__, pTqHandle->subKey, pTqHandle->consumerId);
325,268✔
1322
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
325,268✔
1323
      int32_t code = qDeleteTableListForTmqScanner(pTqHandle->execHandle.task, tbUidList);
×
1324
      if (code != 0) {
×
1325
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1326
        continue;
×
1327
      }
1328
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
325,268✔
1329
      int32_t sz = taosArrayGetSize(tbUidList);
325,268✔
1330
      for (int32_t i = 0; i < sz; i++) {
325,268✔
1331
        int64_t* tbUid = (int64_t*)taosArrayGet(tbUidList, i);
×
1332
        if (tbUid &&
×
1333
            taosHashPut(pTqHandle->execHandle.execDb.pFilterOutTbUid, tbUid, sizeof(int64_t), NULL, 0) != 0) {
×
1334
          tqError("failed to add table uid:%" PRId64 " to hash", *tbUid);
×
1335
          continue;
×
1336
        }
1337
      }
1338
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
×
1339
      tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
×
1340
    }
1341
  }
1342
  taosWUnLockLatch(&pTq->lock);
2,430,564✔
1343
  return 0;
2,430,564✔
1344
}
1345

1346
static SArray* copyUidList(SArray* tbUidList) {
13,874✔
1347
  SArray* tbUidListCopy = taosArrayInit(4, sizeof(int64_t));
13,874✔
1348
  if (tbUidListCopy == NULL) {
13,874✔
1349
    return NULL;
×
1350
  }
1351

1352
  if (taosArrayAddAll(tbUidListCopy, tbUidList) == NULL) {
13,874✔
1353
    taosArrayDestroy(tbUidListCopy);
×
1354
    tqError("copy table uid list failed");
×
1355
    return NULL;
×
1356
  }
1357
  return tbUidListCopy;
13,874✔
1358
}
1359

1360
static int32_t addTableListForStableTmq(STqHandle* pTqHandle, STQ* pTq, SArray* tbUidList) {
13,874✔
1361
  int32_t code = 0;
13,874✔
1362
  SArray* tbUidListCopy = copyUidList(tbUidList);
13,874✔
1363
  if (tbUidListCopy == NULL) {
13,874✔
1364
    code = terrno;
×
1365
    goto END;
×
1366
  }
1367
  code = qFilterTableList(pTq->pVnode, tbUidListCopy, pTqHandle->execHandle.execTb.node,
13,874✔
1368
                      pTqHandle->execHandle.task, pTqHandle->execHandle.execTb.suid);
13,874✔
1369
  if (code != TDB_CODE_SUCCESS) {
13,874✔
1370
    tqError("tqAddTbUidList error:%d handle %s consumer:0x%" PRIx64, code, pTqHandle->subKey,
×
1371
            pTqHandle->consumerId);
1372
    goto END;
×
1373
  }
1374
  tqDebug("%s handle %s consumer:0x%" PRIx64 " add %d tables to tqReader", __func__, pTqHandle->subKey,
13,874✔
1375
          pTqHandle->consumerId, (int32_t)taosArrayGetSize(tbUidListCopy));
1376
  tqReaderAddTbUidList(pTqHandle->execHandle.pTqReader, tbUidListCopy);
13,874✔
1377

1378
END:
13,874✔
1379
  taosArrayDestroy(tbUidListCopy);
13,874✔
1380
  return code;
13,874✔
1381
}
1382

1383
int32_t tqAddTbUidList(STQ* pTq, SArray* tbUidList) {
57,068,059✔
1384
  if (pTq == NULL) {
57,068,059✔
1385
    return 0;  // mounted vnode may have no tq
×
1386
  }
1387
  if (tbUidList == NULL) {
57,068,059✔
1388
    return TSDB_CODE_INVALID_PARA;
×
1389
  }
1390
  void*   pIter = NULL;
57,068,059✔
1391
  int32_t vgId = TD_VID(pTq->pVnode);
57,068,059✔
1392
  int32_t code = 0;
57,068,446✔
1393

1394
  // update the table list for each consumer handle
1395
  taosWLockLatch(&pTq->lock);
57,068,446✔
1396
  while (1) {
1,054,854✔
1397
    pIter = taosHashIterate(pTq->pHandle, pIter);
58,122,913✔
1398
    if (pIter == NULL) {
58,122,913✔
1399
      break;
57,068,059✔
1400
    }
1401

1402
    STqHandle* pTqHandle = (STqHandle*)pIter;
1,054,854✔
1403
    tqDebug("%s subKey:%s, consumer:0x%" PRIx64 " add table list", __func__, pTqHandle->subKey, pTqHandle->consumerId);
1,054,854✔
1404
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
1,054,854✔
1405
      code = qAddTableListForTmqScanner(pTqHandle->execHandle.task, tbUidList);
396,055✔
1406
      if (code != 0) {
396,055✔
1407
        tqError("add table list for query tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
1408
        break;
×
1409
      }
1410
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
658,799✔
1411
      code = addTableListForStableTmq(pTqHandle, pTq, tbUidList);
12,824✔
1412
      if (code != 0) {
12,824✔
1413
        tqError("add table list for stable tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
1414
        break;
×
1415
      }
1416
    }
1417
  }
1418
  taosHashCancelIterate(pTq->pHandle, pIter);
57,068,059✔
1419
  taosWUnLockLatch(&pTq->lock);
57,068,022✔
1420

1421
  return code;
57,068,483✔
1422
}
1423

1424
int32_t tqUpdateTbUidList(STQ* pTq, SArray* tbUidList, SArray* cidList) {
8,031,015✔
1425
  if (pTq == NULL) {
8,031,015✔
1426
    return 0;  // mounted vnode may have no tq
×
1427
  }
1428
  if (tbUidList == NULL) {
8,031,015✔
1429
    return TSDB_CODE_INVALID_PARA;
×
1430
  }
1431
  void*   pIter = NULL;
8,031,015✔
1432
  int32_t vgId = TD_VID(pTq->pVnode);
8,031,015✔
1433
  int32_t code = 0;
8,031,015✔
1434
  // update the table list for each consumer handle
1435
  taosWLockLatch(&pTq->lock);
8,031,015✔
1436
  while (1) {
2,767✔
1437
    pIter = taosHashIterate(pTq->pHandle, pIter);
8,033,782✔
1438
    if (pIter == NULL) {
8,033,782✔
1439
      break;
8,031,015✔
1440
    }
1441

1442
    STqHandle* pTqHandle = (STqHandle*)pIter;
2,767✔
1443
    tqDebug("%s subKey:%s, consumer:0x%" PRIx64 " update table list", __func__, pTqHandle->subKey, pTqHandle->consumerId);
2,767✔
1444
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
2,767✔
1445
      SNode* pTagCond = getTagCondNodeForQueryTmq(pTqHandle->execHandle.task);
667✔
1446
      bool ret = checkCidInTagCondition(pTagCond, cidList);
667✔
1447
      if (ret){
667✔
1448
        code = qUpdateTableListForTmqScanner(pTqHandle->execHandle.task, tbUidList);
×
1449
        if (code != 0) {
×
1450
          tqError("update table list for query tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
1451
          break;
×
1452
        }
1453
      }
1454
      qUpdateTableTagCacheForTmq(pTqHandle->execHandle.task, tbUidList, cidList);
667✔
1455
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
2,100✔
1456
      SNode* pTagCond = getTagCondNodeForStableTmq(pTqHandle->execHandle.execTb.node);
2,100✔
1457
      bool ret = checkCidInTagCondition(pTagCond, cidList);
2,100✔
1458
      if (ret){
2,100✔
1459
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
1,050✔
1460
        code = addTableListForStableTmq(pTqHandle, pTq, tbUidList);
1,050✔
1461
        if (code != 0) {
1,050✔
1462
          tqError("update table list for stable tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
1463
          break;
×
1464
        }
1465
      }
1466
    }
1467
  }
1468

1469
  taosHashCancelIterate(pTq->pHandle, pIter);
8,031,015✔
1470
  taosWUnLockLatch(&pTq->lock);
8,031,015✔
1471

1472
  return code;
8,031,015✔
1473
}
1474

1475
static void destroySourceScanTables(void* ptr) {
×
1476
  SArray** pTables = ptr;
×
1477
  if (pTables && *pTables) {
×
1478
    taosArrayDestroy(*pTables);
×
1479
    *pTables = NULL;
×
1480
  }
1481
}
×
1482

1483
static int32_t compareSVTColInfo(const void* p1, const void* p2) {
×
1484
  SVTColInfo* pCol1 = (SVTColInfo*)p1;
×
1485
  SVTColInfo* pCol2 = (SVTColInfo*)p2;
×
1486
  if (pCol1->vColId == pCol2->vColId) {
×
1487
    return 0;
×
1488
  } else if (pCol1->vColId < pCol2->vColId) {
×
1489
    return -1;
×
1490
  } else {
1491
    return 1;
×
1492
  }
1493
}
1494

1495
static void freeTableSchemaCache(const void* key, size_t keyLen, void* value, void* ud) {
×
1496
  if (value) {
×
1497
    SSchemaWrapper* pSchemaWrapper = value;
×
1498
    tDeleteSchemaWrapper(pSchemaWrapper);
1499
  }
1500
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc