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

taosdata / TDengine / #4979

09 Mar 2026 09:42AM UTC coverage: 68.512% (+0.07%) from 68.439%
#4979

push

travis-ci

web-flow
doc: update user manual. (#34693)

211961 of 309380 relevant lines covered (68.51%)

134839524.93 hits per line

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

75.31
/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,500✔
27
  int32_t code = 0;
4,500✔
28
  int32_t lino = 0;
4,500✔
29
  int32_t        needRebuild = 0;
4,500✔
30
  SVCreateTbReq* pCreateReq = NULL;
4,500✔
31
  SVCreateTbBatchReq reqNew = {0};
4,500✔
32
  void* buf = NULL;
4,500✔
33
  SVCreateTbBatchReq req = {0};
4,500✔
34
  code = tDecodeSVCreateTbBatchReq(dcoder, &req);
4,500✔
35
  if (code < 0) {
4,500✔
36
    lino = __LINE__;
×
37
    goto end;
×
38
  }
39

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

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

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

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

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

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

123
end:
3,108✔
124
  taosArrayDestroy(req.pMultiTag);
3,108✔
125
  metaReaderClear(&mr);  
3,108✔
126
  if (code < 0) {
3,108✔
127
    tqError("processAlterTbMsg failed, code:%d, line:%d", code, lino);
×
128
  }
129
} 
3,108✔
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) {
165,000✔
205
  int32_t code = 0;
165,000✔
206
  int32_t lino = 0;
165,000✔
207
  if (pHandle == NULL || pHead == NULL) {
165,000✔
208
    return false;
×
209
  }
210
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
165,000✔
211
    return true;
155,321✔
212
  }
213

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

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

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

228
  if (msgType == TDMT_VND_CREATE_STB || msgType == TDMT_VND_ALTER_STB) {
11,750✔
229
    SVCreateStbReq req = {0};
2,071✔
230
    if (tDecodeSVCreateStbReq(&dcoder, &req) < 0) {
2,071✔
231
      goto end;
×
232
    }
233
    realTbSuid = req.suid;
2,071✔
234
  } else if (msgType == TDMT_VND_DROP_STB) {
7,608✔
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,608✔
241
    processCreateTbMsg(&dcoder, pHead, pReader, &realTbSuid, tbSuid);
4,500✔
242
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
3,108✔
243
    processAlterTbMsg(&dcoder, pReader, &realTbSuid);
3,108✔
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,679✔
255
  tDecoderClear(&dcoder);
9,679✔
256
  bool tmp = tbSuid == realTbSuid;
9,679✔
257
  tqDebug("%s suid:%" PRId64 " realSuid:%" PRId64 " return:%d", __FUNCTION__, tbSuid, realTbSuid, tmp);
9,679✔
258
  return tmp;
9,679✔
259
}
260

261
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
51,030,225✔
262
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
51,030,225✔
263
    return -1;
×
264
  }
265
  int32_t code = -1;
51,160,256✔
266
  int32_t vgId = TD_VID(pTq->pVnode);
51,160,256✔
267
  int64_t id = pHandle->pWalReader->readerId;
51,182,344✔
268

269
  int64_t offset = *fetchOffset;
51,174,834✔
270
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
51,181,215✔
271
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
51,179,839✔
272
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
51,172,213✔
273

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

278
  while (offset <= appliedVer) {
53,772,451✔
279
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
49,038,057✔
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,035,457✔
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,036,185✔
290
      code = walFetchBody(pHandle->pWalReader);
46,298,830✔
291
      goto END;
46,298,516✔
292
    } else {
293
      if (pHandle->fetchMeta != WITH_DATA) {
2,739,231✔
294
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
206,934✔
295
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
206,934✔
296
          code = walFetchBody(pHandle->pWalReader);
165,269✔
297
          if (code < 0) {
165,000✔
298
            goto END;
×
299
          }
300

301
          pHead = &(pHandle->pWalReader->pHead->head);
165,000✔
302
          if (isValValidForTable(pHandle, pHead)) {
165,000✔
303
            code = 0;
159,750✔
304
            goto END;
159,750✔
305
          } else {
306
            offset++;
5,250✔
307
            code = -1;
5,250✔
308
            continue;
5,250✔
309
          }
310
        }
311
      }
312
      code = walSkipFetchBody(pHandle->pWalReader);
2,573,962✔
313
      if (code < 0) {
2,573,652✔
314
        goto END;
×
315
      }
316
      offset++;
2,573,652✔
317
    }
318
    code = -1;
2,573,652✔
319
  }
320

321
END:
4,734,394✔
322
  *fetchOffset = offset;
51,192,660✔
323
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
51,192,198✔
324
          ", applied:%" PRId64 ", 0x%" PRIx64,
325
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
326
  return code;
51,194,551✔
327
}
328

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

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

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

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

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

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

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

385
  return pReader;
457,213✔
386
}
387

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

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

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

403
  taosMemoryFree(pReader);
456,931✔
404
}
405

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

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

421
  void* data = taosHashGet(pReader->pTableTagCacheForTmq, &uid, LONG_BYTES);
116,601,736✔
422
  if (data == NULL) {
116,654,389✔
423
    SStorageAPI api = {0}; 
39,021,639✔
424
    initStorageAPI(&api);
39,021,996✔
425
    code = cacheTag(pReader->pVnode, pReader->pTableTagCacheForTmq, pExprInfo, numOfExpr, &api, uid, 0, &pReader->tagCachelock);
39,022,802✔
426
    TSDB_CHECK_CODE(code, lino, END);
39,021,965✔
427
  }
428

429
  END:
77,632,750✔
430
  if (code != TSDB_CODE_SUCCESS) {
116,649,257✔
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;
116,601,817✔
435
}
436

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

441
  void* data = taosHashGet(pReader->pTableTagCacheForTmq, &uid, LONG_BYTES);
789,220✔
442
  if (data == NULL) {
789,220✔
443
    return;
788,565✔
444
  }
445

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

451
  END:
655✔
452
  if (code != TSDB_CODE_SUCCESS) {
655✔
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) {
116,596,406✔
458
  if (pReader == NULL || pBlock == NULL) {
116,596,406✔
459
    return TSDB_CODE_INVALID_PARA;
×
460
  }
461
  int32_t code = TSDB_CODE_SUCCESS;
116,635,244✔
462
  int32_t lino = 0;
116,635,244✔
463
  
464
  code = getTableTagCache(pReader, pPseudoExpr, numOfPseudoExpr, uid);
116,635,244✔
465
  TSDB_CHECK_CODE(code, lino, END);
116,628,272✔
466

467
  code = fillTag(pReader->pTableTagCacheForTmq, pPseudoExpr, numOfPseudoExpr, uid, pBlock, numOfRows, pBlock->info.rows - numOfRows, 1, &pReader->tagCachelock);
116,628,272✔
468
  TSDB_CHECK_CODE(code, lino, END);
116,653,613✔
469

470
END:
116,653,613✔
471
  if (code != 0) {
116,653,613✔
472
    tqError("tqRetrievePseudoCols failed, line:%d, msg:%s", lino, tstrerror(code));
×
473
  }
474
  return code;
116,632,661✔
475
}
476

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

485
  int64_t st = taosGetTimestampMs();
23,184,704✔
486
  while (1) {
139,092,110✔
487
    code = walNextValidMsg(pWalReader, false);
162,276,814✔
488
    if (code != 0) {
162,224,092✔
489
      break;
22,337,876✔
490
    }
491

492
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
139,886,216✔
493
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
139,950,023✔
494
    int64_t ver = pWalReader->pHead->head.version;
139,952,158✔
495
    SDecoder decoder = {0};
139,961,313✔
496
    code = tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL, &decoder);
139,952,643✔
497
    tDecoderClear(&decoder);
139,871,699✔
498
    if (code != 0) {
139,897,341✔
499
      return code;
×
500
    }
501
    pReader->nextBlk = 0;
139,897,341✔
502

503
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
139,937,031✔
504
    while (pReader->nextBlk < numOfBlocks) {
279,902,547✔
505
      tqDebug("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
139,939,568✔
506
              pReader->msg.ver);
507

508
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
139,951,718✔
509
      if (pSubmitTbData == NULL) {
140,008,342✔
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) {
140,008,342✔
515
        pReader->nextBlk += 1;
28,514✔
516
        continue;
28,514✔
517
      }
518
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
139,940,312✔
519
        tqDebug("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
116,669,505✔
520
        int32_t numOfRows = pRes->info.rows;
116,569,816✔
521
        code = tqRetrieveCols(pReader, pRes, pCol2SlotId);
116,668,072✔
522
        if (code != TSDB_CODE_SUCCESS) {
116,558,038✔
523
          return code;
×
524
        }
525
        code = tqRetrievePseudoCols(pReader, pRes, numOfRows, pSubmitTbData->uid, pPseudoExpr, numOfPseudoExpr);
116,558,038✔
526
        if (code != TSDB_CODE_SUCCESS) {
116,640,075✔
527
          return code;
×
528
        }
529

530
      }
531
      pReader->nextBlk += 1;
139,951,747✔
532
    }
533

534
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
139,930,347✔
535
    pReader->msg.msgStr = NULL;
139,952,234✔
536

537
    if (pRes->info.rows >= minPollRows || (enableReplay && pRes->info.rows > 0)){
139,942,066✔
538
      break;
539
    }
540
    int64_t elapsed = taosGetTimestampMs() - st;
139,326,407✔
541
    if (elapsed > timeout || elapsed < 0) {
139,326,407✔
542
      code = TSDB_CODE_TMQ_FETCH_TIMEOUT;
264,205✔
543
      terrno = code;
264,205✔
544
      break;
247,420✔
545
    }
546
  }
547
  return code;
23,185,026✔
548
}
549

550
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList, SDecoder* decoder) {
186,172,755✔
551
  if (pReader == NULL) {
186,172,755✔
552
    return TSDB_CODE_INVALID_PARA;
×
553
  }
554
  pReader->msg.msgStr = msgStr;
186,172,755✔
555
  pReader->msg.msgLen = msgLen;
186,214,908✔
556
  pReader->msg.ver = ver;
186,232,189✔
557

558
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
186,252,893✔
559

560
  tDecoderInit(decoder, pReader->msg.msgStr, pReader->msg.msgLen);
186,252,893✔
561
  int32_t code = tDecodeSubmitReq(decoder, &pReader->submit, rawList);
186,243,447✔
562

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

567
  return code;
186,174,478✔
568
}
569

570
void tqReaderClearSubmitMsg(STqReader* pReader) {
92,484,835✔
571
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
92,484,835✔
572
  pReader->nextBlk = 0;
92,503,117✔
573
  pReader->msg.msgStr = NULL;
92,500,145✔
574
}
92,537,303✔
575

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

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

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

598
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
23,675,026✔
599
  while (pReader->nextBlk < blockSz) {
24,867,652✔
600
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
12,435,447✔
601
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
12,436,131✔
602
    uid = pSubmitTbData->uid;
12,436,131✔
603
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
12,436,131✔
604
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
12,437,837✔
605

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

611
  tqReaderClearSubmitMsg(pReader);
12,431,875✔
612
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
12,429,453✔
613

614
END:
12,429,453✔
615
  tqTrace("%s:%d return:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
23,672,258✔
616
  return code;
23,669,860✔
617
}
618

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

624
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
67,640,200✔
625
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
67,640,200✔
626
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
67,674,822✔
627

628
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
67,674,822✔
629
  while (pReader->nextBlk < blockSz) {
67,721,616✔
630
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
33,873,294✔
631
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
33,878,983✔
632
    uid = pSubmitTbData->uid;
33,878,983✔
633
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
33,879,980✔
634
    TSDB_CHECK_NULL(ret, code, lino, END, true);
33,879,409✔
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);
33,851,260✔
639
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
33,844,454✔
640

641
END:
33,844,454✔
642
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
67,723,863✔
643
  return code;
67,668,342✔
644
}
645

646
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask,
44,832,641✔
647
                    SExtSchema* extSrc) {
648
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
44,832,641✔
649
    return TSDB_CODE_INVALID_PARA;
×
650
  }
651
  int32_t code = 0;
44,888,305✔
652

653
  int32_t cnt = 0;
44,888,305✔
654
  for (int32_t i = 0; i < pSrc->nCols; i++) {
251,528,692✔
655
    cnt += mask[i];
206,641,165✔
656
  }
657

658
  pDst->nCols = cnt;
44,870,896✔
659
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
44,902,996✔
660
  if (pDst->pSchema == NULL) {
44,837,357✔
661
    return TAOS_GET_TERRNO(terrno);
×
662
  }
663

664
  int32_t j = 0;
44,835,625✔
665
  for (int32_t i = 0; i < pSrc->nCols; i++) {
251,552,297✔
666
    if (mask[i]) {
206,588,050✔
667
      pDst->pSchema[j++] = pSrc->pSchema[i];
206,663,215✔
668
      SColumnInfoData colInfo =
206,684,745✔
669
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
206,683,616✔
670
      if (extSrc != NULL) {
206,651,590✔
671
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
58,642✔
672
      }
673
      code = blockDataAppendColInfo(pBlock, &colInfo);
206,651,590✔
674
      if (code != 0) {
206,703,473✔
675
        return code;
×
676
      }
677
    }
678
  }
679
  return 0;
44,911,864✔
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);
340✔
757
  }
758
  return code;
2,147,483,647✔
759
}
760

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

769
  SArray* pColArray = taosArrayInit(4, INT_BYTES * 2);
116,656,227✔
770
  TSDB_CHECK_NULL(pColArray, code, line, END, terrno);
116,626,290✔
771

772
  int32_t sourceIdx = -1;
116,626,290✔
773
  int32_t rowIndex = 0;
116,626,290✔
774
  SRow* pRow = taosArrayGetP(pRows, rowIndex);
116,626,290✔
775
  TSDB_CHECK_NULL(pRow, code, line, END, terrno);
116,648,013✔
776
  while (++sourceIdx < pReader->pTSchema->numOfCols) {
1,001,652,582✔
777
    SColVal colVal = {0};
885,274,945✔
778
    code = tRowGet(pRow, pReader->pTSchema, sourceIdx, &colVal);
885,193,282✔
779
    TSDB_CHECK_CODE(code, line, END);
885,631,965✔
780
    void* pSlotId = taosHashGet(pCol2SlotId, &colVal.cid, sizeof(colVal.cid));
885,631,965✔
781
    if (pSlotId == NULL) {
886,593,259✔
782
      continue;
354,703,336✔
783
    }
784
    int32_t pData[2] = {sourceIdx, *(int16_t*)pSlotId};
531,889,923✔
785
    TSDB_CHECK_NULL(taosArrayPush(pColArray, pData), code, line, END, terrno);
532,061,292✔
786
    code = setBlockData(pBlock, pData[1], pBlock->info.rows + rowIndex, &colVal, pBlobSet);
532,061,292✔
787
    TSDB_CHECK_CODE(code, line, END);
530,673,718✔
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:
113,534,904✔
807
  taosArrayDestroy(pColArray);
103,767,886✔
808
  return code;
116,625,609✔
809
}
810

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

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

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

839
static int32_t checkSchema(STqReader* pReader, SSubmitTbData* pSubmitTbData) {
116,661,699✔
840
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
116,661,699✔
841
  int32_t sversion = pSubmitTbData->sver;
116,668,144✔
842
  int64_t suid = pSubmitTbData->suid;
116,669,572✔
843
  int64_t uid = pSubmitTbData->uid;
116,668,860✔
844
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
116,658,526✔
845
      (pReader->cachedSchemaVer != sversion)) {
116,522,188✔
846
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
134,912✔
847
    taosMemoryFreeClear(pReader->extSchema);
146,703✔
848
    taosMemoryFreeClear(pReader->pTSchema);
146,703✔
849
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnode->pMeta, uid, sversion, 1, &pReader->extSchema, 0);
146,703✔
850
    if (pReader->pSchemaWrapper == NULL) {
146,490✔
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);
146,490✔
856
    if (pReader->pTSchema == NULL) {
147,018✔
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;
146,394✔
862
    pReader->cachedSchemaSuid = suid;
146,658✔
863
    pReader->cachedSchemaVer = sversion;
146,754✔
864
  }
865
  return TSDB_CODE_SUCCESS;
116,662,922✔
866
}
867

868
static int32_t tqRetrieveCols(STqReader* pReader, SSDataBlock* pBlock, SHashObj* pCol2SlotId) {
116,667,735✔
869
  if (pReader == NULL || pBlock == NULL) {
116,667,735✔
870
    return TSDB_CODE_INVALID_PARA;
×
871
  }
872
  tqDebug("tq reader retrieve data block %p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
116,668,437✔
873
  int32_t        code = 0;
116,547,041✔
874
  int32_t        line = 0;
116,547,041✔
875
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
116,547,041✔
876
  TSDB_CHECK_NULL(pSubmitTbData, code, line, END, terrno);
116,665,945✔
877
  pReader->lastTs = pSubmitTbData->ctimeMs;
116,665,945✔
878

879
  int32_t numOfRows = 0;
116,667,727✔
880
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
116,667,727✔
881
    SColData* pCol = taosArrayGet(pSubmitTbData->aCol, 0);
1,312✔
882
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
1,312✔
883
    numOfRows = pCol->nVal;
1,312✔
884
  } else {
885
    numOfRows = taosArrayGetSize(pSubmitTbData->aRowP);
116,661,787✔
886
  }
887

888
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows + numOfRows);
116,663,495✔
889
  TSDB_CHECK_CODE(code, line, END);
116,663,823✔
890

891
  code = checkSchema(pReader, pSubmitTbData);
116,663,823✔
892
  TSDB_CHECK_CODE(code, line, END);
116,651,207✔
893

894
  // convert and scan one block
895
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
116,651,207✔
896
    SArray* pCols = pSubmitTbData->aCol;
1,312✔
897
    code = processSubmitCol(pCols, pBlock, pCol2SlotId, pSubmitTbData->pBlobSet);
1,312✔
898
    TSDB_CHECK_CODE(code, line, END);
1,312✔
899
  } else {
900
    SArray*         pRows = pSubmitTbData->aRowP;
116,661,966✔
901
    code = processSubmitRow(pRows, pBlock, pCol2SlotId, pReader, pSubmitTbData->pBlobSet);
116,661,903✔
902
    TSDB_CHECK_CODE(code, line, END);
116,611,720✔
903
  }
904
  pBlock->info.rows += numOfRows;
116,613,032✔
905
END:
116,623,656✔
906
  if (code != 0) {
116,623,656✔
907
    tqError("tqRetrieveCols failed, line:%d, msg:%s", line, tstrerror(code));
×
908
  }
909
  return code;
116,617,494✔
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,
44,822,150✔
941
                               char* assigned, int32_t numOfRows, int32_t curRow, int32_t* lastRow) {
942
  int32_t         code = 0;
44,822,150✔
943
  SSchemaWrapper* pSW = NULL;
44,822,150✔
944
  SSDataBlock*    block = NULL;
44,825,390✔
945
  if (taosArrayGetSize(blocks) > 0) {
44,825,390✔
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));
44,862,994✔
953
  TQ_NULL_GO_TO_END(block);
44,802,508✔
954

955
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
44,802,508✔
956
  TQ_NULL_GO_TO_END(pSW);
44,846,176✔
957

958
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pReader->pSchemaWrapper, assigned, pReader->extSchema));
44,846,176✔
959
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
44,909,010✔
960
          (int32_t)taosArrayGetSize(block->pDataBlock));
961

962
  block->info.id.uid = pSubmitTbData->uid;
44,909,010✔
963
  block->info.version = pReader->msg.ver;
44,889,835✔
964
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
44,902,881✔
965
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
44,890,204✔
966
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
44,876,908✔
967
  pSW = NULL;
44,876,908✔
968

969
  taosMemoryFreeClear(block);
44,876,908✔
970

971
END:
44,901,304✔
972
  if (code != 0) {
44,894,438✔
973
    tqError("processBuildNew failed, code:%d", code);
×
974
  }
975
  tDeleteSchemaWrapper(pSW);
44,894,438✔
976
  blockDataFreeRes(block);
44,842,540✔
977
  taosMemoryFree(block);
44,838,572✔
978
  return code;
44,880,718✔
979
}
980
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
588,442✔
981
  int32_t code = 0;
588,442✔
982
  int32_t curRow = 0;
588,442✔
983
  int32_t lastRow = 0;
588,442✔
984

985
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
588,442✔
986
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
588,121✔
987
  TQ_NULL_GO_TO_END(assigned);
588,442✔
988

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

999
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
333,718,263✔
1000
      int32_t k = 0;
249,611,206✔
1001
      for (; k < numOfCols; k++) {
497,169,753✔
1002
        pCol = taosArrayGet(pCols, k);
486,004,548✔
1003
        TQ_NULL_GO_TO_END(pCol);
485,365,110✔
1004
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
485,365,110✔
1005
          SColVal colVal = {0};
248,600,538✔
1006
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
250,085,345✔
1007
          PROCESS_VAL
250,300,321✔
1008
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
251,107,359✔
1009
          break;
251,252,265✔
1010
        }
1011
      }
1012
      if (k >= numOfCols) {
249,376,667✔
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) {
82,496,901✔
1020
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
588,442✔
1021
    }
1022

1023
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
82,496,901✔
1024
    TQ_NULL_GO_TO_END(pBlock);
84,527,065✔
1025

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

1029
    int32_t targetIdx = 0;
84,527,065✔
1030
    int32_t sourceIdx = 0;
84,527,065✔
1031
    int32_t colActual = blockDataGetNumOfCols(pBlock);
84,527,065✔
1032
    while (targetIdx < colActual && sourceIdx < numOfCols) {
334,354,130✔
1033
      pCol = taosArrayGet(pCols, sourceIdx);
250,137,786✔
1034
      TQ_NULL_GO_TO_END(pCol);
249,330,217✔
1035
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
249,330,217✔
1036
      TQ_NULL_GO_TO_END(pColData);
247,850,346✔
1037
      SColVal colVal = {0};
247,850,346✔
1038
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
248,677,160✔
1039
      SET_DATA
250,559,352✔
1040
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
252,262,470✔
1041
    }
1042

1043
    curRow++;
84,216,344✔
1044
  }
1045
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
463,190✔
1046
  pLastBlock->info.rows = curRow - lastRow;
588,442✔
1047
  tqTrace("vgId:%d, tqProcessColData end, col num: %d, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId,
588,442✔
1048
          numOfCols, numOfRows, (int)taosArrayGetSize(blocks));
1049
END:
15,492,410✔
1050
  if (code != TSDB_CODE_SUCCESS) {
588,442✔
1051
    tqError("vgId:%d, process col data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1052
  }
1053
  taosMemoryFree(assigned);
588,442✔
1054
  return code;
588,442✔
1055
}
1056

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

1061
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
44,198,969✔
1062
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
44,222,389✔
1063
  TQ_NULL_GO_TO_END(assigned);
44,265,937✔
1064

1065
  int32_t curRow = 0;
44,265,937✔
1066
  int32_t lastRow = 0;
44,265,937✔
1067
  SArray* pRows = pSubmitTbData->aRowP;
44,249,351✔
1068
  int32_t numOfRows = taosArrayGetSize(pRows);
44,290,373✔
1069
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
44,275,903✔
1070
  TQ_NULL_GO_TO_END(pTSchema);
44,300,547✔
1071
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
44,300,547✔
1072

1073
  for (int32_t i = 0; i < numOfRows; i++) {
1,267,055,496✔
1074
    bool  buildNew = false;
1,222,796,618✔
1075
    SRow* pRow = taosArrayGetP(pRows, i);
1,222,796,618✔
1076
    TQ_NULL_GO_TO_END(pRow);
1,222,061,669✔
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,209,851,114✔
1086
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
44,319,168✔
1087
    }
1088

1089
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
1,209,812,023✔
1090
    TQ_NULL_GO_TO_END(pBlock);
1,224,125,191✔
1091

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

1095
    int32_t targetIdx = 0;
1,224,125,191✔
1096
    int32_t sourceIdx = 0;
1,224,125,191✔
1097
    int32_t colActual = blockDataGetNumOfCols(pBlock);
1,224,125,191✔
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,222,846,435✔
1108
  }
1109
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
44,258,878✔
1110
  if (pLastBlock != NULL) {
44,314,575✔
1111
    pLastBlock->info.rows = curRow - lastRow;
44,315,155✔
1112
  }
1113

1114
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
44,315,898✔
1115
          (int)taosArrayGetSize(blocks));
1116
END:
50,739,271✔
1117
  if (code != TSDB_CODE_SUCCESS) {
44,292,654✔
1118
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1119
  }
1120
  taosMemoryFreeClear(pTSchema);
44,269,164✔
1121
  taosMemoryFree(assigned);
44,267,268✔
1122
  return code;
44,276,880✔
1123
}
1124

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

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

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

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

1155
END:
9,269✔
1156
  if (code != 0) {
9,269✔
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;
9,269✔
1161
}
1162

1163
int32_t tqRetrieveTaosxBlock(STqReader* pReader, SMqDataRsp* pRsp, SArray* blocks, SArray* schemas,
45,110,479✔
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,110,479✔
1166
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
45,110,479✔
1167
  if (pSubmitTbData == NULL) {
45,120,729✔
1168
    return terrno;
×
1169
  }
1170
  pReader->nextBlk++;
45,120,729✔
1171

1172
  if (pSubmitTbDataRet) {
45,114,815✔
1173
    *pSubmitTbDataRet = pSubmitTbData;
45,118,523✔
1174
  }
1175

1176
  if (fetchMeta == ONLY_META) {
45,115,745✔
1177
    if (pSubmitTbData->pCreateTbReq != NULL) {
6,860✔
1178
      if (pRsp->createTableReq == NULL) {
2,018✔
1179
        pRsp->createTableReq = taosArrayInit(0, POINTER_BYTES);
1,211✔
1180
        if (pRsp->createTableReq == NULL) {
1,211✔
1181
          return terrno;
×
1182
        }
1183
      }
1184
      if (taosArrayPush(pRsp->createTableReq, &pSubmitTbData->pCreateTbReq) == NULL) {
4,036✔
1185
        return terrno;
×
1186
      }
1187
      pSubmitTbData->pCreateTbReq = NULL;
2,018✔
1188
    }
1189
    return 0;
6,860✔
1190
  }
1191

1192
  int32_t sversion = pSubmitTbData->sver;
45,108,885✔
1193
  int64_t uid = pSubmitTbData->uid;
45,111,325✔
1194
  pReader->lastBlkUid = uid;
45,111,649✔
1195

1196
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
45,112,579✔
1197
  taosMemoryFreeClear(pReader->extSchema);
45,098,982✔
1198
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnode->pMeta, uid, sversion, 1, &pReader->extSchema, 0);
45,108,678✔
1199
  if (pReader->pSchemaWrapper == NULL) {
45,065,402✔
1200
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
207,134✔
1201
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1202
    pReader->cachedSchemaSuid = 0;
207,459✔
1203
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
207,459✔
1204
  }
1205

1206
  if (pSubmitTbData->pCreateTbReq != NULL) {
44,849,162✔
1207
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
9,269✔
1208
    if (code != 0) {
9,269✔
1209
      return code;
×
1210
    }
1211
  } else if (rawList != NULL) {
44,838,825✔
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) {
44,848,094✔
1220
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
588,442✔
1221
  } else {
1222
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
44,237,164✔
1223
  }
1224
}
1225

1226
int32_t tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
358,670✔
1227
  if (pReader == NULL || tbUidList == NULL) {
358,670✔
1228
    return TSDB_CODE_SUCCESS;
×
1229
  }
1230
  if (pReader->tbIdHash) {
359,000✔
1231
    taosHashClear(pReader->tbIdHash);
×
1232
  } else {
1233
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
358,723✔
1234
    if (pReader->tbIdHash == NULL) {
359,000✔
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,810,088✔
1241
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
9,451,090✔
1242
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
9,451,090✔
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));
358,365✔
1249
  return TSDB_CODE_SUCCESS;
359,000✔
1250
}
1251

1252
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
440,361✔
1253
  if (pReader == NULL || pTableUidList == NULL) {
440,361✔
1254
    return;
×
1255
  }
1256
  if (pReader->tbIdHash == NULL) {
440,361✔
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);
440,361✔
1265
  for (int i = 0; i < numOfTables; i++) {
1,093,856✔
1266
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
653,495✔
1267
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
653,495✔
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);
653,495✔
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) {
2,508✔
1290
  if (pReader == NULL || tbUidList == NULL) {
2,508✔
1291
    return;
×
1292
  }
1293
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
3,576✔
1294
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
1,068✔
1295
    int32_t code = taosHashRemove(pReader->tbIdHash, pKey, sizeof(int64_t));
1,068✔
1296
    if (code != 0) {
1,068✔
1297
      tqWarn("%s failed to remove table uid:%" PRId64 " from hash, msg:%s", __func__, pKey != NULL ? *pKey : 0, tstrerror(code));
356✔
1298
    }
1299
  }
1300
}
1301

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

1312
  // update the table list for each consumer handle
1313
  taosWLockLatch(&pTq->lock);
2,502,565✔
1314
  while (1) {
329,300✔
1315
    pIter = taosHashIterate(pTq->pHandle, pIter);
2,831,433✔
1316
    if (pIter == NULL) {
2,830,146✔
1317
      break;
2,500,846✔
1318
    }
1319

1320
    STqHandle* pTqHandle = (STqHandle*)pIter;
329,300✔
1321
    tqDebug("%s subKey:%s, consumer:0x%" PRIx64 " delete table list", __func__, pTqHandle->subKey, pTqHandle->consumerId);
329,300✔
1322
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
329,300✔
1323
      int32_t code = qDeleteTableListForTmqScanner(pTqHandle->execHandle.task, tbUidList);
1,440✔
1324
      if (code != 0) {
1,440✔
1325
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1326
        continue;
×
1327
      }
1328
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
327,860✔
1329
      int32_t sz = taosArrayGetSize(tbUidList);
327,860✔
1330
      for (int32_t i = 0; i < sz; i++) {
327,860✔
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,500,846✔
1343
  return 0;
2,502,565✔
1344
}
1345

1346
static int32_t addTableListForStableTmq(STqHandle* pTqHandle, STQ* pTq, SArray* tbUidList) {
12,012✔
1347
  int     ret = qFilterTableList(pTq->pVnode, tbUidList, pTqHandle->execHandle.execTb.node,
12,012✔
1348
                      pTqHandle->execHandle.task, pTqHandle->execHandle.execTb.suid);
12,012✔
1349
  if (ret != TDB_CODE_SUCCESS) {
12,012✔
1350
    tqError("tqAddTbUidList error:%d handle %s consumer:0x%" PRIx64, ret, pTqHandle->subKey,
×
1351
            pTqHandle->consumerId);
1352
    return ret;
×
1353
  }
1354
  tqDebug("%s handle %s consumer:0x%" PRIx64 " add %d tables to tqReader", __func__, pTqHandle->subKey,
12,012✔
1355
          pTqHandle->consumerId, (int32_t)taosArrayGetSize(tbUidList));
1356
  tqReaderAddTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
12,012✔
1357
  return 0;
12,012✔
1358
}
1359

1360
int32_t tqAddTbUidList(STQ* pTq, SArray* tbUidList) {
64,119,149✔
1361
  if (pTq == NULL) {
64,119,149✔
1362
    return 0;  // mounted vnode may have no tq
×
1363
  }
1364
  if (tbUidList == NULL) {
64,119,149✔
1365
    return TSDB_CODE_INVALID_PARA;
×
1366
  }
1367
  void*   pIter = NULL;
64,119,149✔
1368
  int32_t vgId = TD_VID(pTq->pVnode);
64,119,149✔
1369
  int32_t code = 0;
64,120,179✔
1370

1371
  // update the table list for each consumer handle
1372
  taosWLockLatch(&pTq->lock);
64,120,179✔
1373
  while (1) {
1,093,904✔
1374
    pIter = taosHashIterate(pTq->pHandle, pIter);
65,213,580✔
1375
    if (pIter == NULL) {
65,214,124✔
1376
      break;
64,120,220✔
1377
    }
1378

1379
    STqHandle* pTqHandle = (STqHandle*)pIter;
1,093,904✔
1380
    tqDebug("%s subKey:%s, consumer:0x%" PRIx64 " add table list", __func__, pTqHandle->subKey, pTqHandle->consumerId);
1,093,904✔
1381
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
1,093,904✔
1382
      code = qAddTableListForTmqScanner(pTqHandle->execHandle.task, tbUidList);
428,349✔
1383
      if (code != 0) {
428,349✔
1384
        tqError("add table list for query tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
1385
        break;
×
1386
      }
1387
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
665,555✔
1388
      code = addTableListForStableTmq(pTqHandle, pTq, tbUidList);
10,944✔
1389
      if (code != 0) {
10,944✔
1390
        tqError("add table list for stable tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
1391
        break;
×
1392
      }
1393
    }
1394
  }
1395
  taosHashCancelIterate(pTq->pHandle, pIter);
64,120,220✔
1396
  taosWUnLockLatch(&pTq->lock);
64,120,220✔
1397

1398
  return code;
64,120,220✔
1399
}
1400

1401
int32_t tqUpdateTbUidList(STQ* pTq, SArray* tbUidList, SArray* cidList) {
7,699,511✔
1402
  if (pTq == NULL) {
7,699,511✔
1403
    return 0;  // mounted vnode may have no tq
×
1404
  }
1405
  if (tbUidList == NULL) {
7,699,511✔
1406
    return TSDB_CODE_INVALID_PARA;
×
1407
  }
1408
  void*   pIter = NULL;
7,699,511✔
1409
  int32_t vgId = TD_VID(pTq->pVnode);
7,699,511✔
1410
  int32_t code = 0;
7,699,511✔
1411
  // update the table list for each consumer handle
1412
  taosWLockLatch(&pTq->lock);
7,699,511✔
1413
  while (1) {
2,791✔
1414
    pIter = taosHashIterate(pTq->pHandle, pIter);
7,702,302✔
1415
    if (pIter == NULL) {
7,702,302✔
1416
      break;
7,699,511✔
1417
    }
1418

1419
    STqHandle* pTqHandle = (STqHandle*)pIter;
2,791✔
1420
    tqDebug("%s subKey:%s, consumer:0x%" PRIx64 " update table list", __func__, pTqHandle->subKey, pTqHandle->consumerId);
2,791✔
1421
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
2,791✔
1422
      SNode* pTagCond = getTagCondNodeForQueryTmq(pTqHandle->execHandle.task);
655✔
1423
      bool ret = checkCidInTagCondition(pTagCond, cidList);
655✔
1424
      if (ret){
655✔
1425
        code = qUpdateTableListForTmqScanner(pTqHandle->execHandle.task, tbUidList);
×
1426
        if (code != 0) {
×
1427
          tqError("update table list for query tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
1428
          break;
×
1429
        }
1430
      }
1431
      qUpdateTableTagCacheForTmq(pTqHandle->execHandle.task, tbUidList, cidList);
655✔
1432
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
2,136✔
1433
      SNode* pTagCond = getTagCondNodeForStableTmq(pTqHandle->execHandle.execTb.node);
2,136✔
1434
      bool ret = checkCidInTagCondition(pTagCond, cidList);
2,136✔
1435
      if (ret){
2,136✔
1436
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
1,068✔
1437
        code = addTableListForStableTmq(pTqHandle, pTq, tbUidList);
1,068✔
1438
        if (code != 0) {
1,068✔
1439
          tqError("update table list for stable tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
1440
          break;
×
1441
        }
1442
      }
1443
    }
1444
  }
1445

1446
  taosHashCancelIterate(pTq->pHandle, pIter);
7,699,511✔
1447
  taosWUnLockLatch(&pTq->lock);
7,699,511✔
1448

1449
  return code;
7,699,511✔
1450
}
1451

1452
static void destroySourceScanTables(void* ptr) {
×
1453
  SArray** pTables = ptr;
×
1454
  if (pTables && *pTables) {
×
1455
    taosArrayDestroy(*pTables);
×
1456
    *pTables = NULL;
×
1457
  }
1458
}
×
1459

1460
static int32_t compareSVTColInfo(const void* p1, const void* p2) {
×
1461
  SVTColInfo* pCol1 = (SVTColInfo*)p1;
×
1462
  SVTColInfo* pCol2 = (SVTColInfo*)p2;
×
1463
  if (pCol1->vColId == pCol2->vColId) {
×
1464
    return 0;
×
1465
  } else if (pCol1->vColId < pCol2->vColId) {
×
1466
    return -1;
×
1467
  } else {
1468
    return 1;
×
1469
  }
1470
}
1471

1472
static void freeTableSchemaCache(const void* key, size_t keyLen, void* value, void* ud) {
×
1473
  if (value) {
×
1474
    SSchemaWrapper* pSchemaWrapper = value;
×
1475
    tDeleteSchemaWrapper(pSchemaWrapper);
1476
  }
1477
}
×
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