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

taosdata / TDengine / #4976

06 Mar 2026 09:48AM UTC coverage: 68.446% (+0.08%) from 68.37%
#4976

push

travis-ci

web-flow
feat(TDgpt): support multiple input data columns for anomaly detection. (#34606)

0 of 93 new or added lines in 9 files covered. (0.0%)

5718 existing lines in 144 files now uncovered.

211146 of 308486 relevant lines covered (68.45%)

136170362.0 hits per line

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

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

40
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
2,868✔
41
    pCreateReq = req.pReqs + iReq;
1,593✔
42
    if (pCreateReq->type == TSDB_CHILD_TABLE && pCreateReq->ctb.suid == tbSuid &&
2,867✔
43
        taosHashGet(pReader->tbIdHash, &pCreateReq->uid, sizeof(int64_t)) != NULL) {  
1,274✔
44
      needRebuild++;
637✔
45
    }
46
  }
47
  if (needRebuild == 0) {
1,275✔
48
    // do nothing
49
  } else if (needRebuild == req.nReqs) {
637✔
50
    *realTbSuid = tbSuid;
319✔
51
  } else {
52
    *realTbSuid = tbSuid;
318✔
53
    reqNew.pArray = taosArrayInit(req.nReqs, sizeof(struct SVCreateTbReq));
318✔
54
    if (reqNew.pArray == NULL) {
318✔
55
      code = terrno;
×
56
      lino = __LINE__;
×
57
      goto end;
×
58
    }
59
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
954✔
60
      pCreateReq = req.pReqs + iReq;
636✔
61
      if (pCreateReq->type == TSDB_CHILD_TABLE && pCreateReq->ctb.suid == tbSuid &&
1,272✔
62
          taosHashGet(pReader->tbIdHash, &pCreateReq->uid, sizeof(int64_t)) != NULL) {
636✔
63
        reqNew.nReqs++;
318✔
64
        if (taosArrayPush(reqNew.pArray, pCreateReq) == NULL) {
636✔
65
          code = terrno;
×
66
          lino = __LINE__;
×
67
          goto end;
×
68
        }
69
      }
70
    }
71

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

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

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

110
  metaReaderDoInit(&mr, pReader->pVnode->pMeta, META_READER_LOCK);
957✔
111

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

121
end:
957✔
122
  taosArrayDestroy(req.pMultiTag);
957✔
123
  metaReaderClear(&mr);  
957✔
124
  if (code < 0) {
957✔
125
    tqError("processAlterTbMsg failed, code:%d, line:%d", code, lino);
×
126
  }
127
} 
957✔
128

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

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

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

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

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

202
bool isValValidForTable(STqHandle* pHandle, SWalCont* pHead) {
150,708✔
203
  int32_t code = 0;
150,708✔
204
  int32_t lino = 0;
150,708✔
205
  if (pHandle == NULL || pHead == NULL) {
150,708✔
206
    return false;
×
207
  }
208
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
150,708✔
209
    return true;
147,839✔
210
  }
211

212
  STqExecHandle* pExec = &pHandle->execHandle;
2,869✔
213
  STqReader* pReader = pExec->pTqReader;
2,869✔
214

215
  int16_t msgType = pHead->msgType;
2,869✔
216
  char*   body = pHead->body;
2,869✔
217
  int32_t bodyLen = pHead->bodyLen;
2,869✔
218

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

226
  if (msgType == TDMT_VND_CREATE_STB || msgType == TDMT_VND_ALTER_STB) {
3,506✔
227
    SVCreateStbReq req = {0};
637✔
228
    if (tDecodeSVCreateStbReq(&dcoder, &req) < 0) {
637✔
229
      goto end;
×
230
    }
231
    realTbSuid = req.suid;
637✔
232
  } else if (msgType == TDMT_VND_DROP_STB) {
2,232✔
233
    SVDropStbReq req = {0};
×
234
    if (tDecodeSVDropStbReq(&dcoder, &req) < 0) {
×
235
      goto end;
×
236
    }
237
    realTbSuid = req.suid;
×
238
  } else if (msgType == TDMT_VND_CREATE_TABLE) {
2,232✔
239
    processCreateTbMsg(&dcoder, pHead, pReader, &realTbSuid, tbSuid);
1,275✔
240
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
957✔
241
    processAlterTbMsg(&dcoder, pReader, &realTbSuid);
957✔
242
  } else if (msgType == TDMT_VND_DROP_TABLE) {
×
243
    processDropTbMsg(&dcoder, pHead, pReader, &realTbSuid, tbSuid);
×
244
  } else if (msgType == TDMT_VND_DELETE) {
×
245
    SDeleteRes req = {0};
×
246
    if (tDecodeDeleteRes(&dcoder, &req) < 0) {
×
247
      goto end;
×
248
    }
249
    realTbSuid = req.suid;
×
250
  }
251

252
end:
2,869✔
253
  tDecoderClear(&dcoder);
2,869✔
254
  bool tmp = tbSuid == realTbSuid;
2,869✔
255
  tqDebug("%s suid:%" PRId64 " realSuid:%" PRId64 " return:%d", __FUNCTION__, tbSuid, realTbSuid, tmp);
2,869✔
256
  return tmp;
2,869✔
257
}
258

259
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
50,741,080✔
260
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
50,741,080✔
261
    return -1;
×
262
  }
263
  int32_t code = -1;
50,836,490✔
264
  int32_t vgId = TD_VID(pTq->pVnode);
50,836,490✔
265
  int64_t id = pHandle->pWalReader->readerId;
50,855,401✔
266

267
  int64_t offset = *fetchOffset;
50,852,089✔
268
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
50,862,512✔
269
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
50,856,529✔
270
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
50,851,697✔
271

272
  tqDebug("vgId:%d, start to fetch wal, index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64 ", applied:%" PRId64
50,847,876✔
273
          ", 0x%" PRIx64,
274
          vgId, offset, lastVer, committedVer, appliedVer, id);
275

276
  while (offset <= appliedVer) {
53,735,224✔
277
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
48,374,730✔
278
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", (epoch %d) vgId:%d offset %" PRId64
×
279
              ", no more log to return, QID:0x%" PRIx64 " 0x%" PRIx64,
280
              pHandle->consumerId, pHandle->epoch, vgId, offset, reqId, id);
281
      goto END;
×
282
    }
283

284
    tqDebug("vgId:%d, consumer:0x%" PRIx64 " taosx get msg ver %" PRId64 ", type:%s, QID:0x%" PRIx64 " 0x%" PRIx64,
48,372,682✔
285
            vgId, pHandle->consumerId, offset, TMSG_INFO(pHandle->pWalReader->pHead->head.msgType), reqId, id);
286

287
    if (pHandle->pWalReader->pHead->head.msgType == TDMT_VND_SUBMIT) {
48,377,391✔
288
      code = walFetchBody(pHandle->pWalReader);
45,358,070✔
289
      goto END;
45,357,135✔
290
    } else {
291
      if (pHandle->fetchMeta != WITH_DATA) {
3,018,527✔
292
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
189,661✔
293
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
189,661✔
294
          code = walFetchBody(pHandle->pWalReader);
150,708✔
295
          if (code < 0) {
150,708✔
296
            goto END;
×
297
          }
298

299
          pHead = &(pHandle->pWalReader->pHead->head);
150,708✔
300
          if (isValValidForTable(pHandle, pHead)) {
150,708✔
301
            code = 0;
149,751✔
302
            goto END;
149,751✔
303
          } else {
304
            offset++;
957✔
305
            code = -1;
957✔
306
            continue;
957✔
307
          }
308
        }
309
      }
310
      code = walSkipFetchBody(pHandle->pWalReader);
2,867,819✔
311
      if (code < 0) {
2,867,819✔
312
        goto END;
×
313
      }
314
      offset++;
2,867,819✔
315
    }
316
    code = -1;
2,867,819✔
317
  }
318

319
END:
5,360,494✔
320
  *fetchOffset = offset;
50,867,380✔
321
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
50,866,423✔
322
          ", applied:%" PRId64 ", 0x%" PRIx64,
323
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
324
  return code;
50,867,062✔
325
}
326

327
bool tqGetTablePrimaryKey(STqReader* pReader) {
6,001,462✔
328
  if (pReader == NULL) {
6,001,462✔
329
    return false;
×
330
  }
331
  return pReader->hasPrimaryKey;
6,001,462✔
332
}
333

334
void tqSetTablePrimaryKey(STqReader* pReader, int64_t uid) {
46,386✔
335
  tqDebug("%s:%p uid:%" PRId64, __FUNCTION__, pReader, uid);
46,386✔
336

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

349
static void freeTagCache(void* pData){
1,654,072✔
350
  if (pData == NULL) return;
1,654,072✔
351
  SArray* tagCache = *(SArray**)pData;
1,654,072✔
352
  taosArrayDestroyP(tagCache, taosMemFree);
1,654,072✔
353
}
354

355
STqReader* tqReaderOpen(SVnode* pVnode) {
439,539✔
356
  tqDebug("%s:%p", __FUNCTION__, pVnode);
439,539✔
357
  if (pVnode == NULL) {
444,072✔
358
    return NULL;
×
359
  }
360
  STqReader* pReader = taosMemoryCalloc(1, sizeof(STqReader));
444,072✔
361
  if (pReader == NULL) {
444,072✔
362
    return NULL;
×
363
  }
364

365
  pReader->pWalReader = walOpenReader(pVnode->pWal, 0);
444,072✔
366
  if (pReader->pWalReader == NULL) {
444,072✔
367
    taosMemoryFree(pReader);
×
368
    return NULL;
×
369
  }
370

371
  pReader->pVnode = pVnode;
443,763✔
372
  pReader->pSchemaWrapper = NULL;
444,072✔
373
  pReader->tbIdHash = NULL;
444,072✔
374
  pReader->pTableTagCacheForTmq = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
443,724✔
375
  if (pReader->pTableTagCacheForTmq == NULL) {
444,072✔
376
    walCloseReader(pReader->pWalReader);
×
377
    taosMemoryFree(pReader);
×
378
    return NULL;
×
379
  }
380
  taosHashSetFreeFp(pReader->pTableTagCacheForTmq, freeTagCache);
444,072✔
381
  taosInitRWLatch(&pReader->tagCachelock);
443,763✔
382

383
  return pReader;
444,072✔
384
}
385

386
void tqReaderClose(STqReader* pReader) {
448,555✔
387
  tqDebug("%s:%p", __FUNCTION__, pReader);
448,555✔
388
  if (pReader == NULL) return;
448,555✔
389

390
  // close wal reader
391
  walCloseReader(pReader->pWalReader);
444,072✔
392
  taosHashCleanup(pReader->pTableTagCacheForTmq);
444,072✔
393
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
444,072✔
394
  taosMemoryFree(pReader->pTSchema);
444,072✔
395
  taosMemoryFree(pReader->extSchema);
444,072✔
396

397
  // free hash
398
  taosHashCleanup(pReader->tbIdHash);
444,072✔
399
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
443,766✔
400

401
  taosMemoryFree(pReader);
444,072✔
402
}
403

404
int32_t tqReaderSeek(STqReader* pReader, int64_t ver, const char* id) {
902,494✔
405
  if (pReader == NULL) {
902,494✔
406
    return TSDB_CODE_INVALID_PARA;
×
407
  }
408
  if (walReaderSeekVer(pReader->pWalReader, ver) < 0) {
902,494✔
409
    return terrno;
23,817✔
410
  }
411
  tqDebug("wal reader seek to ver:%" PRId64 " %s", ver, id);
878,983✔
412
  return 0;
878,983✔
413
}
414

415
static int32_t getTableTagCache(STqReader* pReader, SExprInfo* pExprInfo, int32_t numOfExpr, int64_t uid) {
103,918,873✔
416
  int32_t code = 0;
103,918,873✔
417
  int32_t lino = 0;
103,918,873✔
418

419
  void* data = taosHashGet(pReader->pTableTagCacheForTmq, &uid, LONG_BYTES);
103,918,873✔
420
  if (data == NULL) {
103,968,610✔
421
    SStorageAPI api = {0}; 
37,700,323✔
422
    initStorageAPI(&api);
37,700,611✔
423
    code = cacheTag(pReader->pVnode, pReader->pTableTagCacheForTmq, pExprInfo, numOfExpr, &api, uid, 0, &pReader->tagCachelock);
37,699,068✔
424
    TSDB_CHECK_CODE(code, lino, END);
37,700,899✔
425
  }
426

427
  END:
66,268,287✔
428
  if (code != TSDB_CODE_SUCCESS) {
103,966,983✔
429
    tqError("%s failed at %d, failed to add tbName to response:%s, uid:%"PRId64, __FUNCTION__, lino, tstrerror(code), uid);
×
430
  }
431
  
432
  return code;
103,924,511✔
433
}
434

435
void tqUpdateTableTagCache(STqReader* pReader, SExprInfo* pExprInfo, int32_t numOfExpr, int64_t uid, col_id_t colId) {
709,702✔
436
  int32_t code = 0;
709,702✔
437
  int32_t lino = 0;
709,702✔
438

439
  void* data = taosHashGet(pReader->pTableTagCacheForTmq, &uid, LONG_BYTES);
709,702✔
440
  if (data == NULL) {
709,702✔
441
    return;
709,054✔
442
  }
443

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

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

455
static int32_t tqRetrievePseudoCols(STqReader* pReader, SSDataBlock* pBlock, int32_t numOfRows, int64_t uid, SExprInfo* pPseudoExpr, int32_t numOfPseudoExpr) {
103,905,298✔
456
  if (pReader == NULL || pBlock == NULL) {
103,905,298✔
457
    return TSDB_CODE_INVALID_PARA;
×
458
  }
459
  int32_t code = TSDB_CODE_SUCCESS;
103,954,079✔
460
  int32_t lino = 0;
103,954,079✔
461
  
462
  code = getTableTagCache(pReader, pPseudoExpr, numOfPseudoExpr, uid);
103,954,079✔
463
  TSDB_CHECK_CODE(code, lino, END);
103,938,338✔
464

465
  code = fillTag(pReader->pTableTagCacheForTmq, pPseudoExpr, numOfPseudoExpr, uid, pBlock, numOfRows, pBlock->info.rows - numOfRows, 1, &pReader->tagCachelock);
103,938,338✔
466
  TSDB_CHECK_CODE(code, lino, END);
103,967,294✔
467

468
END:
103,967,294✔
469
  if (code != 0) {
103,967,294✔
470
    tqError("tqRetrievePseudoCols failed, line:%d, msg:%s", lino, tstrerror(code));
×
471
  }
472
  return code;
103,961,406✔
473
}
474

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

483
  int64_t st = taosGetTimestampMs();
23,357,274✔
484
  while (1) {
129,261,009✔
485
    code = walNextValidMsg(pWalReader, false);
152,618,283✔
486
    if (code != 0) {
152,619,839✔
487
      break;
22,678,060✔
488
    }
489

490
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
129,941,779✔
491
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
129,985,031✔
492
    int64_t ver = pWalReader->pHead->head.version;
129,971,241✔
493
    SDecoder decoder = {0};
129,982,778✔
494
    code = tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL, &decoder);
129,971,512✔
495
    tDecoderClear(&decoder);
129,941,857✔
496
    if (code != 0) {
129,923,856✔
497
      return code;
×
498
    }
499
    pReader->nextBlk = 0;
129,923,856✔
500

501
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
129,942,933✔
502
    while (pReader->nextBlk < numOfBlocks) {
259,939,209✔
503
      tqDebug("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
129,961,785✔
504
              pReader->msg.ver);
505

506
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
130,002,638✔
507
      if (pSubmitTbData == NULL) {
130,011,316✔
508
        tqError("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
×
509
                pReader->msg.ver);
510
        return terrno;
×
511
      }
512
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
130,011,316✔
513
        pReader->nextBlk += 1;
28,196✔
514
        continue;
28,196✔
515
      }
516
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
129,982,774✔
517
        tqDebug("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
103,979,838✔
518
        int32_t numOfRows = pRes->info.rows;
103,975,686✔
519
        code = tqRetrieveCols(pReader, pRes, pCol2SlotId);
103,978,108✔
520
        if (code != TSDB_CODE_SUCCESS) {
103,913,413✔
521
          return code;
×
522
        }
523
        code = tqRetrievePseudoCols(pReader, pRes, numOfRows, pSubmitTbData->uid, pPseudoExpr, numOfPseudoExpr);
103,913,413✔
524
        if (code != TSDB_CODE_SUCCESS) {
103,957,294✔
525
          return code;
×
526
        }
527

528
      }
529
      pReader->nextBlk += 1;
129,963,614✔
530
    }
531

532
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
129,980,113✔
533
    pReader->msg.msgStr = NULL;
129,960,357✔
534

535
    if (pRes->info.rows >= minPollRows || (enableReplay && pRes->info.rows > 0)){
129,957,653✔
536
      break;
537
    }
538
    int64_t elapsed = taosGetTimestampMs() - st;
129,370,320✔
539
    if (elapsed > timeout || elapsed < 0) {
129,370,320✔
540
      code = TSDB_CODE_TMQ_FETCH_TIMEOUT;
112,558✔
541
      terrno = code;
112,558✔
542
      break;
95,150✔
543
    }
544
  }
545
  return code;
23,358,714✔
546
}
547

548
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList, SDecoder* decoder) {
175,280,722✔
549
  if (pReader == NULL) {
175,280,722✔
550
    return TSDB_CODE_INVALID_PARA;
×
551
  }
552
  pReader->msg.msgStr = msgStr;
175,280,722✔
553
  pReader->msg.msgLen = msgLen;
175,306,935✔
554
  pReader->msg.ver = ver;
175,309,509✔
555

556
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
175,322,340✔
557

558
  tDecoderInit(decoder, pReader->msg.msgStr, pReader->msg.msgLen);
175,322,340✔
559
  int32_t code = tDecodeSubmitReq(decoder, &pReader->submit, rawList);
175,309,304✔
560

561
  if (code != 0) {
175,305,485✔
562
    tqError("DecodeSSubmitReq2 error, msgLen:%d, ver:%" PRId64, msgLen, ver);
×
563
  }
564

565
  return code;
175,278,886✔
566
}
567

568
void tqReaderClearSubmitMsg(STqReader* pReader) {
90,632,918✔
569
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
90,632,918✔
570
  pReader->nextBlk = 0;
90,647,195✔
571
  pReader->msg.msgStr = NULL;
90,655,048✔
572
}
90,672,816✔
573

574
SWalReader* tqGetWalReader(STqReader* pReader) {
47,456,667✔
575
  if (pReader == NULL) {
47,456,667✔
576
    return NULL;
×
577
  }
578
  return pReader->pWalReader;
47,456,667✔
579
}
580

581
int64_t tqGetResultBlockTime(STqReader* pReader) {
23,359,345✔
582
  if (pReader == NULL) {
23,359,345✔
583
    return 0;
×
584
  }
585
  return pReader->lastTs;
23,359,345✔
586
}
587

588
bool tqNextBlockImpl(STqReader* pReader, const char* idstr) {
23,324,596✔
589
  int32_t code = false;
23,324,596✔
590
  int32_t lino = 0;
23,324,596✔
591
  int64_t uid = 0;
23,324,596✔
592
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
23,324,596✔
593
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
23,324,596✔
594
  TSDB_CHECK_NULL(pReader->tbIdHash, code, lino, END, true);
23,325,609✔
595

596
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
23,325,952✔
597
  while (pReader->nextBlk < blockSz) {
24,476,082✔
598
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
12,240,393✔
599
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
12,241,390✔
600
    uid = pSubmitTbData->uid;
12,241,390✔
601
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
12,241,063✔
602
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
12,241,390✔
603

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

609
  tqReaderClearSubmitMsg(pReader);
12,235,575✔
610
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
12,236,261✔
611

612
END:
12,236,261✔
613
  tqTrace("%s:%d return:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
23,325,568✔
614
  return code;
23,324,269✔
615
}
616

617
bool tqNextDataBlockFilterOut(STqReader* pReader, SHashObj* filterOutUids) {
66,191,545✔
618
  int32_t code = false;
66,191,545✔
619
  int32_t lino = 0;
66,191,545✔
620
  int64_t uid = 0;
66,191,545✔
621

622
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
66,191,545✔
623
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
66,191,545✔
624
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
66,217,987✔
625

626
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
66,217,987✔
627
  while (pReader->nextBlk < blockSz) {
66,230,964✔
628
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
33,131,708✔
629
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
33,136,765✔
630
    uid = pSubmitTbData->uid;
33,136,765✔
631
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
33,136,765✔
632
    TSDB_CHECK_NULL(ret, code, lino, END, true);
33,135,670✔
633
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64, pReader->nextBlk, blockSz, uid);
×
634
    pReader->nextBlk++;
×
635
  }
636
  tqReaderClearSubmitMsg(pReader);
33,108,765✔
637
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
33,099,573✔
638

639
END:
33,099,573✔
640
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
66,235,243✔
641
  return code;
66,161,479✔
642
}
643

644
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask,
43,964,967✔
645
                    SExtSchema* extSrc) {
646
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
43,964,967✔
647
    return TSDB_CODE_INVALID_PARA;
×
648
  }
649
  int32_t code = 0;
43,992,631✔
650

651
  int32_t cnt = 0;
43,992,631✔
652
  for (int32_t i = 0; i < pSrc->nCols; i++) {
246,209,738✔
653
    cnt += mask[i];
202,171,539✔
654
  }
655

656
  pDst->nCols = cnt;
44,027,689✔
657
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
44,016,879✔
658
  if (pDst->pSchema == NULL) {
43,967,674✔
659
    return TAOS_GET_TERRNO(terrno);
×
660
  }
661

662
  int32_t j = 0;
43,963,894✔
663
  for (int32_t i = 0; i < pSrc->nCols; i++) {
246,261,109✔
664
    if (mask[i]) {
202,215,498✔
665
      pDst->pSchema[j++] = pSrc->pSchema[i];
202,278,006✔
666
      SColumnInfoData colInfo =
202,274,197✔
667
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
202,284,308✔
668
      if (extSrc != NULL) {
202,243,268✔
669
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
57,988✔
670
      }
671
      code = blockDataAppendColInfo(pBlock, &colInfo);
202,243,268✔
672
      if (code != 0) {
202,287,310✔
673
        return code;
×
674
      }
675
    }
676
  }
677
  return 0;
44,020,433✔
678
}
679

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

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

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

712
    blobDataSetLen(val, len);
×
713
    code = colDataSetVal(pColumnInfoData, idx, val, false);
×
714

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

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

740
  return code;
2,147,483,647✔
741
}
742

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

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

759
static int32_t processSubmitRow(SArray*         pRows,
103,970,237✔
760
                                SSDataBlock*    pBlock,
761
                                SHashObj*       pCol2SlotId,
762
                                STqReader*      pReader,
763
                                SBlobSet*       pBlobSet) {
764
  int32_t        code = 0;
103,970,237✔
765
  int32_t        line = 0;
103,970,237✔
766

767
  SArray* pColArray = taosArrayInit(4, INT_BYTES * 2);
103,970,237✔
768
  TSDB_CHECK_NULL(pColArray, code, line, END, terrno);
103,975,762✔
769

770
  int32_t sourceIdx = -1;
103,975,762✔
771
  int32_t rowIndex = 0;
103,975,762✔
772
  SRow* pRow = taosArrayGetP(pRows, rowIndex);
103,975,762✔
773
  TSDB_CHECK_NULL(pRow, code, line, END, terrno);
103,972,337✔
774
  while (++sourceIdx < pReader->pTSchema->numOfCols) {
886,707,521✔
775
    SColVal colVal = {0};
782,955,793✔
776
    code = tRowGet(pRow, pReader->pTSchema, sourceIdx, &colVal);
782,815,294✔
777
    TSDB_CHECK_CODE(code, line, END);
782,942,975✔
778
    void* pSlotId = taosHashGet(pCol2SlotId, &colVal.cid, sizeof(colVal.cid));
782,942,975✔
779
    if (pSlotId == NULL) {
783,362,895✔
780
      continue;
323,346,073✔
781
    }
782
    int32_t pData[2] = {sourceIdx, *(int16_t*)pSlotId};
460,016,822✔
783
    TSDB_CHECK_NULL(taosArrayPush(pColArray, pData), code, line, END, terrno);
460,125,503✔
784
    code = setBlockData(pBlock, pData[1], pBlock->info.rows + rowIndex, &colVal, pBlobSet);
460,125,503✔
785
    TSDB_CHECK_CODE(code, line, END);
459,624,459✔
786
  }
787
  
788
  for (rowIndex = 1; rowIndex < taosArrayGetSize(pRows); rowIndex++) {
2,147,483,647✔
789
    SRow* pRow = taosArrayGetP(pRows, rowIndex);
2,147,483,647✔
790
    TSDB_CHECK_NULL(pRow, code, line, END, terrno);
2,147,483,647✔
791
    for (int32_t j = 0; j < taosArrayGetSize(pColArray); j++) {
2,147,483,647✔
792
      int32_t* pData = taosArrayGet(pColArray, j);
2,147,483,647✔
793
      TSDB_CHECK_NULL(pData, code, line, END, terrno);
2,147,483,647✔
794

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

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

804
  END:
100,523,158✔
805
  taosArrayDestroy(pColArray);
74,735,340✔
806
  return code;
103,938,949✔
807
}
808

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

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

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

837
static int32_t checkSchema(STqReader* pReader, SSubmitTbData* pSubmitTbData) {
103,945,919✔
838
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
103,945,919✔
839
  int32_t sversion = pSubmitTbData->sver;
103,977,405✔
840
  int64_t suid = pSubmitTbData->suid;
103,977,416✔
841
  int64_t uid = pSubmitTbData->uid;
103,963,922✔
842
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
103,976,032✔
843
      (pReader->cachedSchemaVer != sversion)) {
103,832,511✔
844
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
144,210✔
845
    taosMemoryFreeClear(pReader->extSchema);
143,884✔
846
    taosMemoryFreeClear(pReader->pTSchema);
143,884✔
847
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnode->pMeta, uid, sversion, 1, &pReader->extSchema, 0);
143,884✔
848
    if (pReader->pSchemaWrapper == NULL) {
144,202✔
849
      tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", uid:%" PRId64 ",version %d, possibly dropped table",
×
850
              vgId, suid, uid, pReader->cachedSchemaVer);
851
      return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
×
852
    }
853
    pReader->pTSchema = tBuildTSchema(pReader->pSchemaWrapper->pSchema, pReader->pSchemaWrapper->nCols, pReader->pSchemaWrapper->version);
143,934✔
854
    if (pReader->pTSchema == NULL) {
144,202✔
855
      tqWarn("vgId:%d, cannot build schema for table: suid:%" PRId64 ", uid:%" PRId64 ",version %d",
×
856
              vgId, suid, uid, pReader->cachedSchemaVer);
857
      return terrno;
×
858
    }
859
    pReader->cachedSchemaUid = uid;
144,202✔
860
    pReader->cachedSchemaSuid = suid;
144,202✔
861
    pReader->cachedSchemaVer = sversion;
144,202✔
862
  }
863
  return TSDB_CODE_SUCCESS;
103,975,689✔
864
}
865

866
static int32_t tqRetrieveCols(STqReader* pReader, SSDataBlock* pBlock, SHashObj* pCol2SlotId) {
103,975,022✔
867
  if (pReader == NULL || pBlock == NULL) {
103,975,022✔
868
    return TSDB_CODE_INVALID_PARA;
×
869
  }
870
  tqDebug("tq reader retrieve data block %p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
103,977,416✔
871
  int32_t        code = 0;
103,976,724✔
872
  int32_t        line = 0;
103,976,724✔
873
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
103,976,724✔
874
  TSDB_CHECK_NULL(pSubmitTbData, code, line, END, terrno);
103,976,035✔
875
  pReader->lastTs = pSubmitTbData->ctimeMs;
103,976,035✔
876

877
  int32_t numOfRows = 0;
103,978,454✔
878
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
103,978,454✔
879
    SColData* pCol = taosArrayGet(pSubmitTbData->aCol, 0);
1,300✔
880
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
1,300✔
881
    numOfRows = pCol->nVal;
1,300✔
882
  } else {
883
    numOfRows = taosArrayGetSize(pSubmitTbData->aRowP);
103,971,967✔
884
  }
885

886
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows + numOfRows);
103,972,918✔
887
  TSDB_CHECK_CODE(code, line, END);
103,971,177✔
888

889
  code = checkSchema(pReader, pSubmitTbData);
103,971,177✔
890
  TSDB_CHECK_CODE(code, line, END);
103,976,381✔
891

892
  // convert and scan one block
893
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
103,976,381✔
894
    SArray* pCols = pSubmitTbData->aCol;
1,300✔
895
    code = processSubmitCol(pCols, pBlock, pCol2SlotId, pSubmitTbData->pBlobSet);
1,300✔
896
    TSDB_CHECK_CODE(code, line, END);
1,300✔
897
  } else {
898
    SArray*         pRows = pSubmitTbData->aRowP;
103,975,081✔
899
    code = processSubmitRow(pRows, pBlock, pCol2SlotId, pReader, pSubmitTbData->pBlobSet);
103,975,416✔
900
    TSDB_CHECK_CODE(code, line, END);
103,907,930✔
901
  }
902
  pBlock->info.rows += numOfRows;
103,909,230✔
903
END:
103,956,182✔
904
  if (code != 0) {
103,956,182✔
905
    tqError("tqRetrieveCols failed, line:%d, msg:%s", line, tstrerror(code));
×
906
  }
907
  return code;
103,936,379✔
908
}
909

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

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

938
static int32_t processBuildNew(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas,
43,937,143✔
939
                               char* assigned, int32_t numOfRows, int32_t curRow, int32_t* lastRow) {
940
  int32_t         code = 0;
43,937,143✔
941
  SSchemaWrapper* pSW = NULL;
43,937,143✔
942
  SSDataBlock*    block = NULL;
43,972,817✔
943
  if (taosArrayGetSize(blocks) > 0) {
43,972,817✔
944
    SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
×
945
    TQ_NULL_GO_TO_END(pLastBlock);
×
946
    pLastBlock->info.rows = curRow - *lastRow;
×
947
    *lastRow = curRow;
×
948
  }
949

950
  block = taosMemoryCalloc(1, sizeof(SSDataBlock));
43,985,666✔
951
  TQ_NULL_GO_TO_END(block);
43,943,077✔
952

953
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
43,943,077✔
954
  TQ_NULL_GO_TO_END(pSW);
43,963,042✔
955

956
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pReader->pSchemaWrapper, assigned, pReader->extSchema));
43,963,042✔
957
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
44,018,065✔
958
          (int32_t)taosArrayGetSize(block->pDataBlock));
959

960
  block->info.id.uid = pSubmitTbData->uid;
44,018,065✔
961
  block->info.version = pReader->msg.ver;
44,004,808✔
962
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
44,016,269✔
963
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
44,007,498✔
964
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
44,001,957✔
965
  pSW = NULL;
44,001,957✔
966

967
  taosMemoryFreeClear(block);
44,001,957✔
968

969
END:
44,015,953✔
970
  if (code != 0) {
44,006,098✔
971
    tqError("processBuildNew failed, code:%d", code);
×
972
  }
973
  tDeleteSchemaWrapper(pSW);
44,006,098✔
974
  blockDataFreeRes(block);
43,969,817✔
975
  taosMemoryFree(block);
43,968,184✔
976
  return code;
44,002,630✔
977
}
978
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
597,650✔
979
  int32_t code = 0;
597,650✔
980
  int32_t curRow = 0;
597,650✔
981
  int32_t lastRow = 0;
597,650✔
982

983
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
597,969✔
984
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
598,924✔
985
  TQ_NULL_GO_TO_END(assigned);
598,287✔
986

987
  SArray*   pCols = pSubmitTbData->aCol;
598,287✔
988
  SColData* pCol = taosArrayGet(pCols, 0);
598,287✔
989
  TQ_NULL_GO_TO_END(pCol);
597,968✔
990
  int32_t numOfRows = pCol->nVal;
597,968✔
991
  int32_t numOfCols = taosArrayGetSize(pCols);
598,606✔
992
  tqTrace("vgId:%d, tqProcessColData start, col num: %d, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfCols,
598,287✔
993
          numOfRows);
994
  for (int32_t i = 0; i < numOfRows; i++) {
100,384,446✔
995
    bool buildNew = false;
99,708,779✔
996

997
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
397,914,781✔
998
      int32_t k = 0;
297,227,381✔
999
      for (; k < numOfCols; k++) {
590,035,413✔
1000
        pCol = taosArrayGet(pCols, k);
577,092,096✔
1001
        TQ_NULL_GO_TO_END(pCol);
575,889,878✔
1002
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
575,889,878✔
1003
          SColVal colVal = {0};
295,962,536✔
1004
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
296,717,532✔
1005
          PROCESS_VAL
300,019,209✔
1006
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
300,236,225✔
1007
          break;
299,268,141✔
1008
        }
1009
      }
1010
      if (k >= numOfCols) {
298,206,002✔
1011
        // this column is not in the current row, so we set it to NULL
1012
        assigned[j] = 0;
×
1013
        buildNew = true;
×
1014
      }
1015
    }
1016

1017
    if (buildNew) {
99,231,312✔
1018
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
598,924✔
1019
    }
1020

1021
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
99,230,994✔
1022
    TQ_NULL_GO_TO_END(pBlock);
100,076,370✔
1023

1024
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
100,076,370✔
1025
            (int32_t)taosArrayGetSize(blocks));
1026

1027
    int32_t targetIdx = 0;
100,076,370✔
1028
    int32_t sourceIdx = 0;
100,076,370✔
1029
    int32_t colActual = blockDataGetNumOfCols(pBlock);
100,076,370✔
1030
    while (targetIdx < colActual && sourceIdx < numOfCols) {
396,191,261✔
1031
      pCol = taosArrayGet(pCols, sourceIdx);
296,405,102✔
1032
      TQ_NULL_GO_TO_END(pCol);
296,289,658✔
1033
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
296,289,658✔
1034
      TQ_NULL_GO_TO_END(pColData);
292,871,846✔
1035
      SColVal colVal = {0};
292,871,846✔
1036
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
295,678,854✔
1037
      SET_DATA
300,006,433✔
1038
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
296,689,629✔
1039
    }
1040

1041
    curRow++;
99,786,159✔
1042
  }
1043
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
675,667✔
1044
  pLastBlock->info.rows = curRow - lastRow;
598,924✔
1045
  tqTrace("vgId:%d, tqProcessColData end, col num: %d, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId,
598,924✔
1046
          numOfCols, numOfRows, (int)taosArrayGetSize(blocks));
1047
END:
17,230,606✔
1048
  if (code != TSDB_CODE_SUCCESS) {
598,924✔
1049
    tqError("vgId:%d, process col data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1050
  }
1051
  taosMemoryFree(assigned);
598,924✔
1052
  return code;
598,605✔
1053
}
1054

1055
int32_t tqProcessRowData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
43,326,310✔
1056
  int32_t   code = 0;
43,326,310✔
1057
  STSchema* pTSchema = NULL;
43,326,310✔
1058

1059
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
43,326,310✔
1060
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
43,350,698✔
1061
  TQ_NULL_GO_TO_END(assigned);
43,372,846✔
1062

1063
  int32_t curRow = 0;
43,372,846✔
1064
  int32_t lastRow = 0;
43,372,846✔
1065
  SArray* pRows = pSubmitTbData->aRowP;
43,350,450✔
1066
  int32_t numOfRows = taosArrayGetSize(pRows);
43,406,891✔
1067
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
43,394,925✔
1068
  TQ_NULL_GO_TO_END(pTSchema);
43,406,764✔
1069
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
43,406,764✔
1070

1071
  for (int32_t i = 0; i < numOfRows; i++) {
1,259,132,567✔
1072
    bool  buildNew = false;
1,215,742,225✔
1073
    SRow* pRow = taosArrayGetP(pRows, i);
1,215,742,225✔
1074
    TQ_NULL_GO_TO_END(pRow);
1,215,127,958✔
1075

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

1083
    if (buildNew) {
1,207,185,932✔
1084
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
43,419,388✔
1085
    }
1086

1087
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
1,207,167,189✔
1088
    TQ_NULL_GO_TO_END(pBlock);
1,215,940,379✔
1089

1090
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
1,215,940,379✔
1091
            (int32_t)taosArrayGetSize(blocks));
1092

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

1105
    curRow++;
1,215,797,299✔
1106
  }
1107
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
43,390,342✔
1108
  if (pLastBlock != NULL) {
43,408,637✔
1109
    pLastBlock->info.rows = curRow - lastRow;
43,414,557✔
1110
  }
1111

1112
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
43,419,589✔
1113
          (int)taosArrayGetSize(blocks));
1114
END:
48,706,334✔
1115
  if (code != TSDB_CODE_SUCCESS) {
43,388,457✔
1116
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1117
  }
1118
  taosMemoryFreeClear(pTSchema);
43,376,086✔
1119
  taosMemoryFree(assigned);
43,369,487✔
1120
  return code;
43,394,247✔
1121
}
1122

1123
static int32_t buildCreateTbInfo(SMqDataRsp* pRsp, SVCreateTbReq* pCreateTbReq) {
8,462✔
1124
  int32_t code = 0;
8,462✔
1125
  int32_t lino = 0;
8,462✔
1126
  void*   createReq = NULL;
8,462✔
1127
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
8,462✔
1128
  TSDB_CHECK_NULL(pCreateTbReq, code, lino, END, TSDB_CODE_INVALID_PARA);
8,462✔
1129

1130
  if (pRsp->createTableNum == 0) {
8,462✔
1131
    pRsp->createTableLen = taosArrayInit(0, sizeof(int32_t));
4,738✔
1132
    TSDB_CHECK_NULL(pRsp->createTableLen, code, lino, END, terrno);
4,738✔
1133
    pRsp->createTableReq = taosArrayInit(0, sizeof(void*));
4,738✔
1134
    TSDB_CHECK_NULL(pRsp->createTableReq, code, lino, END, terrno);
4,738✔
1135
  }
1136

1137
  uint32_t len = 0;
8,462✔
1138
  tEncodeSize(tEncodeSVCreateTbReq, pCreateTbReq, len, code);
8,462✔
1139
  TSDB_CHECK_CODE(code, lino, END);
8,462✔
1140
  createReq = taosMemoryCalloc(1, len);
8,462✔
1141
  TSDB_CHECK_NULL(createReq, code, lino, END, terrno);
8,462✔
1142

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

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

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

1170
  if (pSubmitTbDataRet) {
44,217,697✔
1171
    *pSubmitTbDataRet = pSubmitTbData;
44,224,020✔
1172
  }
1173

1174
  if (fetchMeta == ONLY_META) {
44,220,112✔
1175
    if (pSubmitTbData->pCreateTbReq != NULL) {
6,761✔
1176
      if (pRsp->createTableReq == NULL) {
1,973✔
1177
        pRsp->createTableReq = taosArrayInit(0, POINTER_BYTES);
1,175✔
1178
        if (pRsp->createTableReq == NULL) {
1,175✔
1179
          return terrno;
×
1180
        }
1181
      }
1182
      if (taosArrayPush(pRsp->createTableReq, &pSubmitTbData->pCreateTbReq) == NULL) {
3,946✔
1183
        return terrno;
×
1184
      }
1185
      pSubmitTbData->pCreateTbReq = NULL;
1,973✔
1186
    }
1187
    return 0;
6,761✔
1188
  }
1189

1190
  int32_t sversion = pSubmitTbData->sver;
44,213,351✔
1191
  int64_t uid = pSubmitTbData->uid;
44,211,387✔
1192
  pReader->lastBlkUid = uid;
44,209,564✔
1193

1194
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
44,217,836✔
1195
  taosMemoryFreeClear(pReader->extSchema);
44,201,339✔
1196
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnode->pMeta, uid, sversion, 1, &pReader->extSchema, 0);
44,211,926✔
1197
  if (pReader->pSchemaWrapper == NULL) {
44,179,118✔
1198
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
200,494✔
1199
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1200
    pReader->cachedSchemaSuid = 0;
200,494✔
1201
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
200,494✔
1202
  }
1203

1204
  if (pSubmitTbData->pCreateTbReq != NULL) {
43,980,151✔
1205
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
8,462✔
1206
    if (code != 0) {
8,462✔
1207
      return code;
×
1208
    }
1209
  } else if (rawList != NULL) {
43,958,130✔
1210
    if (taosArrayPush(schemas, &pReader->pSchemaWrapper) == NULL) {
×
1211
      return terrno;
×
1212
    }
1213
    pReader->pSchemaWrapper = NULL;
×
1214
    return 0;
×
1215
  }
1216

1217
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
43,966,592✔
1218
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
597,650✔
1219
  } else {
1220
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
43,357,978✔
1221
  }
1222
}
1223

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

1238
  for (int i = 0; i < taosArrayGetSize(tbUidList); i++) {
9,659,182✔
1239
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
9,310,867✔
1240
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
9,310,867✔
1241
      tqError("s-task:%s failed to add table uid:%" PRId64 " to hash", id, *pKey);
×
1242
      continue;
×
1243
    }
1244
  }
1245

1246
  tqDebug("s-task:%s %d tables are set to be queried target table", id, (int32_t)taosArrayGetSize(tbUidList));
348,315✔
1247
  return TSDB_CODE_SUCCESS;
348,315✔
1248
}
1249

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

1262
  int32_t numOfTables = taosArrayGetSize(pTableUidList);
411,972✔
1263
  for (int i = 0; i < numOfTables; i++) {
956,921✔
1264
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
544,949✔
1265
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
544,949✔
1266
      tqError("failed to add table uid:%" PRId64 " to hash", *pKey);
×
1267
      continue;
×
1268
    }
1269
    tqDebug("%s add table uid:%" PRId64 " to hash", __func__, *pKey);
544,949✔
1270
  }
1271
}
1272

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

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

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

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

1310
  // update the table list for each consumer handle
1311
  taosWLockLatch(&pTq->lock);
2,235,036✔
1312
  while (1) {
315,180✔
1313
    pIter = taosHashIterate(pTq->pHandle, pIter);
2,550,216✔
1314
    if (pIter == NULL) {
2,550,216✔
1315
      break;
2,235,036✔
1316
    }
1317

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

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

1358
int32_t tqAddTbUidList(STQ* pTq, SArray* tbUidList) {
62,315,160✔
1359
  if (pTq == NULL) {
62,315,160✔
1360
    return 0;  // mounted vnode may have no tq
×
1361
  }
1362
  if (tbUidList == NULL) {
62,315,160✔
1363
    return TSDB_CODE_INVALID_PARA;
×
1364
  }
1365
  void*   pIter = NULL;
62,315,160✔
1366
  int32_t vgId = TD_VID(pTq->pVnode);
62,315,160✔
1367
  int32_t code = 0;
62,315,776✔
1368

1369
  // update the table list for each consumer handle
1370
  taosWLockLatch(&pTq->lock);
62,315,776✔
1371
  while (1) {
1,032,656✔
1372
    pIter = taosHashIterate(pTq->pHandle, pIter);
63,348,334✔
1373
    if (pIter == NULL) {
63,348,660✔
1374
      break;
62,316,004✔
1375
    }
1376

1377
    STqHandle* pTqHandle = (STqHandle*)pIter;
1,032,656✔
1378
    tqDebug("%s subKey:%s, consumer:0x%" PRIx64 " add table list", __func__, pTqHandle->subKey, pTqHandle->consumerId);
1,032,656✔
1379
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
1,032,656✔
1380
      code = qAddTableListForTmqScanner(pTqHandle->execHandle.task, tbUidList);
406,078✔
1381
      if (code != 0) {
406,078✔
1382
        tqError("add table list for query tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
1383
        break;
×
1384
      }
1385
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
626,578✔
1386
      code = addTableListForStableTmq(pTqHandle, pTq, tbUidList);
4,835✔
1387
      if (code != 0) {
4,835✔
1388
        tqError("add table list for stable tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
1389
        break;
×
1390
      }
1391
    }
1392
  }
1393
  taosHashCancelIterate(pTq->pHandle, pIter);
62,316,004✔
1394
  taosWUnLockLatch(&pTq->lock);
62,315,954✔
1395

1396
  return code;
62,316,678✔
1397
}
1398

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

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

1444
  taosHashCancelIterate(pTq->pHandle, pIter);
7,519,790✔
1445
  taosWUnLockLatch(&pTq->lock);
7,519,790✔
1446

1447
  return code;
7,519,790✔
1448
}
1449

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

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

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