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

taosdata / TDengine / #4951

06 Feb 2026 07:29AM UTC coverage: 66.887% (+0.04%) from 66.849%
#4951

push

travis-ci

web-flow
merge: from main to 3.0 #34521

765 of 1081 new or added lines in 28 files covered. (70.77%)

6419 existing lines in 131 files now uncovered.

205810 of 307696 relevant lines covered (66.89%)

127232277.37 hits per line

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

74.07
/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 "tmsg.h"
19
#include "tq.h"
20

21
static void processCreateTbMsg(SDecoder* dcoder, SWalCont* pHead, STqReader* pReader, int64_t* realTbSuid, int64_t tbSuid) {
1,258✔
22
  int32_t code = 0;
1,258✔
23
  int32_t lino = 0;
1,258✔
24
  int32_t        needRebuild = 0;
1,258✔
25
  SVCreateTbReq* pCreateReq = NULL;
1,258✔
26
  SVCreateTbBatchReq reqNew = {0};
1,258✔
27
  void* buf = NULL;
1,258✔
28
  SVCreateTbBatchReq req = {0};
1,258✔
29
  code = tDecodeSVCreateTbBatchReq(dcoder, &req);
1,258✔
30
  if (code < 0) {
1,258✔
31
    lino = __LINE__;
×
32
    goto end;
×
33
  }
34

35
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
2,829✔
36
    pCreateReq = req.pReqs + iReq;
1,571✔
37
    if (pCreateReq->type == TSDB_CHILD_TABLE && pCreateReq->ctb.suid == tbSuid &&
2,827✔
38
        taosHashGet(pReader->tbIdHash, &pCreateReq->uid, sizeof(int64_t)) != NULL) {  
1,256✔
39
      needRebuild++;
628✔
40
    }
41
  }
42
  if (needRebuild == 0) {
1,258✔
43
    // do nothing
44
  } else if (needRebuild == req.nReqs) {
628✔
45
    *realTbSuid = tbSuid;
315✔
46
  } else {
47
    *realTbSuid = tbSuid;
313✔
48
    reqNew.pArray = taosArrayInit(req.nReqs, sizeof(struct SVCreateTbReq));
313✔
49
    if (reqNew.pArray == NULL) {
313✔
50
      code = terrno;
×
51
      lino = __LINE__;
×
52
      goto end;
×
53
    }
54
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
939✔
55
      pCreateReq = req.pReqs + iReq;
626✔
56
      if (pCreateReq->type == TSDB_CHILD_TABLE && pCreateReq->ctb.suid == tbSuid &&
1,252✔
57
          taosHashGet(pReader->tbIdHash, &pCreateReq->uid, sizeof(int64_t)) != NULL) {
626✔
58
        reqNew.nReqs++;
313✔
59
        if (taosArrayPush(reqNew.pArray, pCreateReq) == NULL) {
626✔
60
          code = terrno;
×
61
          lino = __LINE__;
×
62
          goto end;
×
63
        }
64
      }
65
    }
66

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

86
end:
1,258✔
87
  taosMemoryFree(buf);
1,258✔
88
  taosArrayDestroy(reqNew.pArray);
1,258✔
89
  tDeleteSVCreateTbBatchReq(&req);
1,258✔
90
  if (code < 0) {
1,258✔
91
    tqError("processCreateTbMsg failed, code:%d, line:%d", code, lino);
×
92
  }
93
}
1,258✔
94

95
static void processAlterTbMsg(SDecoder* dcoder, STqReader* pReader, int64_t* realTbSuid) {
945✔
96
  SVAlterTbReq req = {0};
945✔
97
  SMetaReader mr = {0};
945✔
98
  int32_t lino = 0;
945✔
99
  int32_t code = tDecodeSVAlterTbReq(dcoder, &req);
945✔
100
  if (code < 0) {
945✔
101
    lino = __LINE__;
×
102
    goto end;
×
103
  }
104

105
  metaReaderDoInit(&mr, pReader->pVnodeMeta, META_READER_LOCK);
945✔
106

107
  code = metaGetTableEntryByName(&mr, req.tbName);
945✔
108
  if (code < 0) {
945✔
109
    lino = __LINE__;
×
110
    goto end;
×
111
  }
112
  if (taosHashGet(pReader->tbIdHash, &mr.me.uid, sizeof(int64_t)) != NULL) {
945✔
113
    *realTbSuid = mr.me.ctbEntry.suid;
630✔
114
  }
115

116
end:
945✔
117
  taosArrayDestroy(req.pMultiTag);
945✔
118
  metaReaderClear(&mr);  
945✔
119
  if (code < 0) {
945✔
120
    tqError("processAlterTbMsg failed, code:%d, line:%d", code, lino);
×
121
  }
122
} 
945✔
123

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

135
  int32_t      needRebuild = 0;
×
136
  SVDropTbReq* pDropReq = NULL;
×
137
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
138
    pDropReq = req.pReqs + iReq;
×
139

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

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

189
end:
×
190
  taosMemoryFree(buf);
×
191
  taosArrayDestroy(reqNew.pArray);
×
192
  if (code < 0) {
×
193
    tqError("processDropTbMsg failed, code:%d, line:%d", code, lino);
×
194
  }
195
}
×
196

197
bool isValValidForTable(STqHandle* pHandle, SWalCont* pHead) {
151,486✔
198
  int32_t code = 0;
151,486✔
199
  int32_t lino = 0;
151,486✔
200
  if (pHandle == NULL || pHead == NULL) {
151,486✔
201
    return false;
×
202
  }
203
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
151,486✔
204
    return true;
148,655✔
205
  }
206

207
  STqExecHandle* pExec = &pHandle->execHandle;
2,831✔
208
  STqReader* pReader = pExec->pTqReader;
2,831✔
209

210
  int16_t msgType = pHead->msgType;
2,831✔
211
  char*   body = pHead->body;
2,831✔
212
  int32_t bodyLen = pHead->bodyLen;
2,831✔
213

214
  int64_t  tbSuid = pHandle->execHandle.execTb.suid;
2,831✔
215
  int64_t  realTbSuid = 0;
2,831✔
216
  SDecoder dcoder = {0};
2,831✔
217
  void*    data = POINTER_SHIFT(body, sizeof(SMsgHead));
2,831✔
218
  int32_t  len = bodyLen - sizeof(SMsgHead);
2,831✔
219
  tDecoderInit(&dcoder, data, len);
2,831✔
220

221
  if (msgType == TDMT_VND_CREATE_STB || msgType == TDMT_VND_ALTER_STB) {
3,459✔
222
    SVCreateStbReq req = {0};
628✔
223
    if (tDecodeSVCreateStbReq(&dcoder, &req) < 0) {
628✔
224
      goto end;
×
225
    }
226
    realTbSuid = req.suid;
628✔
227
  } else if (msgType == TDMT_VND_DROP_STB) {
2,203✔
228
    SVDropStbReq req = {0};
×
229
    if (tDecodeSVDropStbReq(&dcoder, &req) < 0) {
×
230
      goto end;
×
231
    }
232
    realTbSuid = req.suid;
×
233
  } else if (msgType == TDMT_VND_CREATE_TABLE) {
2,203✔
234
    processCreateTbMsg(&dcoder, pHead, pReader, &realTbSuid, tbSuid);
1,258✔
235
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
945✔
236
    processAlterTbMsg(&dcoder, pReader, &realTbSuid);
945✔
237
  } else if (msgType == TDMT_VND_DROP_TABLE) {
×
238
    processDropTbMsg(&dcoder, pHead, pReader, &realTbSuid, tbSuid);
×
239
  } else if (msgType == TDMT_VND_DELETE) {
×
240
    SDeleteRes req = {0};
×
241
    if (tDecodeDeleteRes(&dcoder, &req) < 0) {
×
242
      goto end;
×
243
    }
244
    realTbSuid = req.suid;
×
245
  }
246

247
end:
2,831✔
248
  tDecoderClear(&dcoder);
2,831✔
249
  bool tmp = tbSuid == realTbSuid;
2,831✔
250
  tqDebug("%s suid:%" PRId64 " realSuid:%" PRId64 " return:%d", __FUNCTION__, tbSuid, realTbSuid, tmp);
2,831✔
251
  return tmp;
2,831✔
252
}
253

254
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
50,604,506✔
255
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
50,604,506✔
256
    return -1;
×
257
  }
258
  int32_t code = -1;
50,673,183✔
259
  int32_t vgId = TD_VID(pTq->pVnode);
50,673,183✔
260
  int64_t id = pHandle->pWalReader->readerId;
50,670,810✔
261

262
  int64_t offset = *fetchOffset;
50,677,325✔
263
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
50,676,878✔
264
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
50,679,542✔
265
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
50,677,314✔
266

267
  tqDebug("vgId:%d, start to fetch wal, index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64 ", applied:%" PRId64
50,673,230✔
268
          ", 0x%" PRIx64,
269
          vgId, offset, lastVer, committedVer, appliedVer, id);
270

271
  while (offset <= appliedVer) {
53,758,823✔
272
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
47,970,829✔
273
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", (epoch %d) vgId:%d offset %" PRId64
×
274
              ", no more log to return, QID:0x%" PRIx64 " 0x%" PRIx64,
275
              pHandle->consumerId, pHandle->epoch, vgId, offset, reqId, id);
276
      goto END;
×
277
    }
278

279
    tqDebug("vgId:%d, consumer:0x%" PRIx64 " taosx get msg ver %" PRId64 ", type:%s, QID:0x%" PRIx64 " 0x%" PRIx64,
47,971,413✔
280
            vgId, pHandle->consumerId, offset, TMSG_INFO(pHandle->pWalReader->pHead->head.msgType), reqId, id);
281

282
    if (pHandle->pWalReader->pHead->head.msgType == TDMT_VND_SUBMIT) {
47,971,680✔
283
      code = walFetchBody(pHandle->pWalReader);
44,746,590✔
284
      goto END;
44,746,590✔
285
    } else {
286
      if (pHandle->fetchMeta != WITH_DATA) {
3,224,869✔
287
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
191,262✔
288
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
191,262✔
289
          code = walFetchBody(pHandle->pWalReader);
151,486✔
290
          if (code < 0) {
151,486✔
291
            goto END;
×
292
          }
293

294
          pHead = &(pHandle->pWalReader->pHead->head);
151,486✔
295
          if (isValValidForTable(pHandle, pHead)) {
151,486✔
296
            code = 0;
150,541✔
297
            goto END;
150,541✔
298
          } else {
299
            offset++;
945✔
300
            code = -1;
945✔
301
            continue;
945✔
302
          }
303
        }
304
      }
305
      code = walSkipFetchBody(pHandle->pWalReader);
3,073,383✔
306
      if (code < 0) {
3,073,091✔
307
        goto END;
×
308
      }
309
      offset++;
3,073,091✔
310
    }
311
    code = -1;
3,073,091✔
312
  }
313

314
END:
5,787,994✔
315
  *fetchOffset = offset;
50,685,125✔
316
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
50,683,911✔
317
          ", applied:%" PRId64 ", 0x%" PRIx64,
318
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
319
  return code;
50,684,787✔
320
}
321

322
bool tqGetTablePrimaryKey(STqReader* pReader) {
5,886,495✔
323
  if (pReader == NULL) {
5,886,495✔
324
    return false;
×
325
  }
326
  return pReader->hasPrimaryKey;
5,886,495✔
327
}
328

329
void tqSetTablePrimaryKey(STqReader* pReader, int64_t uid) {
47,485✔
330
  tqDebug("%s:%p uid:%" PRId64, __FUNCTION__, pReader, uid);
47,485✔
331

332
  if (pReader == NULL) {
47,802✔
333
    return;
×
334
  }
335
  bool            ret = false;
47,802✔
336
  SSchemaWrapper* schema = metaGetTableSchema(pReader->pVnodeMeta, uid, -1, 1, NULL, 0);
47,802✔
337
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
47,802✔
338
    ret = true;
902✔
339
  }
340
  tDeleteSchemaWrapper(schema);
341
  pReader->hasPrimaryKey = ret;
47,489✔
342
}
343

344
STqReader* tqReaderOpen(SVnode* pVnode) {
424,079✔
345
  tqDebug("%s:%p", __FUNCTION__, pVnode);
424,079✔
346
  if (pVnode == NULL) {
426,656✔
347
    return NULL;
×
348
  }
349
  STqReader* pReader = taosMemoryCalloc(1, sizeof(STqReader));
426,656✔
350
  if (pReader == NULL) {
426,656✔
351
    return NULL;
×
352
  }
353

354
  pReader->pWalReader = walOpenReader(pVnode->pWal, 0);
426,656✔
355
  if (pReader->pWalReader == NULL) {
426,656✔
356
    taosMemoryFree(pReader);
×
357
    return NULL;
×
358
  }
359

360
  pReader->pVnodeMeta = pVnode->pMeta;
426,656✔
361
  pReader->pColIdList = NULL;
426,656✔
362
  pReader->cachedSchemaVer = 0;
426,656✔
363
  pReader->cachedSchemaSuid = 0;
426,656✔
364
  pReader->pSchemaWrapper = NULL;
426,656✔
365
  pReader->tbIdHash = NULL;
426,656✔
366
  pReader->pResBlock = NULL;
426,656✔
367

368
  return pReader;
426,656✔
369
}
370

371
void tqReaderClose(STqReader* pReader) {
430,484✔
372
  tqDebug("%s:%p", __FUNCTION__, pReader);
430,484✔
373
  if (pReader == NULL) return;
431,055✔
374

375
  // close wal reader
376
  if (pReader->pWalReader) {
426,656✔
377
    walCloseReader(pReader->pWalReader);
426,656✔
378
  }
379

380
  if (pReader->pSchemaWrapper) {
426,343✔
381
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
191,094✔
382
  }
383

384
  taosMemoryFree(pReader->extSchema);
426,343✔
385
  if (pReader->pColIdList) {
426,343✔
386
    taosArrayDestroy(pReader->pColIdList);
309,961✔
387
  }
388

389
  // free hash
390
  blockDataDestroy(pReader->pResBlock);
426,656✔
391
  taosHashCleanup(pReader->tbIdHash);
426,656✔
392
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
426,656✔
393

394
  taosMemoryFree(pReader);
426,376✔
395
}
396

397
int32_t tqReaderSeek(STqReader* pReader, int64_t ver, const char* id) {
965,269✔
398
  if (pReader == NULL) {
965,269✔
399
    return TSDB_CODE_INVALID_PARA;
×
400
  }
401
  if (walReaderSeekVer(pReader->pWalReader, ver) < 0) {
965,269✔
402
    return terrno;
18,798✔
403
  }
404
  tqDebug("wal reader seek to ver:%" PRId64 " %s", ver, id);
947,058✔
405
  return 0;
947,058✔
406
}
407

408
bool tqNextBlockInWal(STqReader* pReader, const char* id, int sourceExcluded) {
110,412,820✔
409
  if (pReader == NULL) {
110,412,820✔
410
    return false;
×
411
  }
412
  SWalReader* pWalReader = pReader->pWalReader;
110,412,820✔
413

414
  int64_t st = taosGetTimestampMs();
110,415,783✔
415
  while (1) {
114,260,853✔
416
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
224,676,636✔
417
    while (pReader->nextBlk < numOfBlocks) {
249,761,301✔
418
      tqDebug("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
114,272,060✔
419
              pReader->msg.ver);
420

421
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
114,284,682✔
422
      if (pSubmitTbData == NULL) {
114,281,950✔
423
        tqError("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
×
424
                pReader->msg.ver);
425
        return false;
153✔
426
      }
427
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
114,281,950✔
428
        pReader->nextBlk += 1;
28,302✔
429
        continue;
28,302✔
430
      }
431
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
114,253,363✔
432
        tqDebug("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
89,186,987✔
433
        SSDataBlock* pRes = NULL;
89,186,462✔
434
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
89,186,724✔
435
        if (code == TSDB_CODE_SUCCESS) {
89,178,106✔
436
          return true;
89,178,106✔
437
        }
438
      } else {
439
        pReader->nextBlk += 1;
25,067,437✔
440
        tqDebug("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
25,067,437✔
441
      }
442
    }
443

444
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
135,494,986✔
445
    pReader->msg.msgStr = NULL;
135,496,041✔
446

447
    int64_t elapsed = taosGetTimestampMs() - st;
135,496,391✔
448
    if (elapsed > 1000 || elapsed < 0) {
135,496,391✔
449
      return false;
×
450
    }
451

452
    // try next message in wal file
453
    if (walNextValidMsg(pWalReader, false) < 0) {
135,496,566✔
454
      return false;
21,229,934✔
455
    }
456

457
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
114,261,040✔
458
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
114,263,909✔
459
    int64_t ver = pWalReader->pHead->head.version;
114,263,245✔
460
    SDecoder decoder = {0};
114,263,760✔
461
    if (tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL, &decoder) != 0) {
114,262,675✔
462
      tDecoderClear(&decoder);
×
463
      return false;
×
464
    }
465
    tDecoderClear(&decoder);
114,255,549✔
466
    pReader->nextBlk = 0;
114,259,906✔
467
  }
468
}
469

470
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList, SDecoder* decoder) {
159,007,361✔
471
  if (pReader == NULL) {
159,007,361✔
472
    return TSDB_CODE_INVALID_PARA;
×
473
  }
474
  pReader->msg.msgStr = msgStr;
159,007,361✔
475
  pReader->msg.msgLen = msgLen;
159,009,512✔
476
  pReader->msg.ver = ver;
159,008,194✔
477

478
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
159,006,655✔
479

480
  tDecoderInit(decoder, pReader->msg.msgStr, pReader->msg.msgLen);
159,006,655✔
481
  int32_t code = tDecodeSubmitReq(decoder, &pReader->submit, rawList);
159,005,832✔
482

483
  if (code != 0) {
159,005,683✔
484
    tqError("DecodeSSubmitReq2 error, msgLen:%d, ver:%" PRId64, msgLen, ver);
×
485
  }
486

487
  return code;
158,995,642✔
488
}
489

490
void tqReaderClearSubmitMsg(STqReader* pReader) {
89,424,583✔
491
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
89,424,583✔
492
  pReader->nextBlk = 0;
89,436,917✔
493
  pReader->msg.msgStr = NULL;
89,452,620✔
494
}
89,461,676✔
495

496
SWalReader* tqGetWalReader(STqReader* pReader) {
133,135,780✔
497
  if (pReader == NULL) {
133,135,780✔
498
    return NULL;
×
499
  }
500
  return pReader->pWalReader;
133,135,780✔
501
}
502

503
SSDataBlock* tqGetResultBlock(STqReader* pReader) {
110,400,099✔
504
  if (pReader == NULL) {
110,400,099✔
505
    return NULL;
×
506
  }
507
  return pReader->pResBlock;
110,400,099✔
508
}
509

510
int64_t tqGetResultBlockTime(STqReader* pReader) {
110,407,288✔
511
  if (pReader == NULL) {
110,407,288✔
512
    return 0;
×
513
  }
514
  return pReader->lastTs;
110,407,288✔
515
}
516

517
bool tqNextBlockImpl(STqReader* pReader, const char* idstr) {
23,098,618✔
518
  int32_t code = false;
23,098,618✔
519
  int32_t lino = 0;
23,098,618✔
520
  int64_t uid = 0;
23,098,618✔
521
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
23,098,618✔
522
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
23,098,618✔
523
  TSDB_CHECK_NULL(pReader->tbIdHash, code, lino, END, true);
23,100,308✔
524

525
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
23,101,322✔
526
  while (pReader->nextBlk < blockSz) {
24,257,386✔
527
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
12,131,838✔
528
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
12,131,838✔
529
    uid = pSubmitTbData->uid;
12,131,838✔
530
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
12,131,838✔
531
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
12,131,904✔
532

533
    tqTrace("iterator data block in hash continue, progress:%d/%d, total queried tables:%d, uid:%" PRId64,
1,158,974✔
534
            pReader->nextBlk, blockSz, taosHashGetSize(pReader->tbIdHash), uid);
535
    pReader->nextBlk++;
1,158,974✔
536
  }
537

538
  tqReaderClearSubmitMsg(pReader);
12,125,820✔
539
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
12,127,444✔
540

541
END:
12,127,444✔
542
  tqTrace("%s:%d return:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
23,100,374✔
543
  return code;
23,099,022✔
544
}
545

546
bool tqNextDataBlockFilterOut(STqReader* pReader, SHashObj* filterOutUids) {
65,218,414✔
547
  int32_t code = false;
65,218,414✔
548
  int32_t lino = 0;
65,218,414✔
549
  int64_t uid = 0;
65,218,414✔
550

551
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
65,218,414✔
552
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
65,218,414✔
553
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
65,239,412✔
554

555
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
65,239,412✔
556
  while (pReader->nextBlk < blockSz) {
65,244,244✔
557
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
32,630,377✔
558
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
32,634,805✔
559
    uid = pSubmitTbData->uid;
32,634,805✔
560
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
32,635,141✔
561
    TSDB_CHECK_NULL(ret, code, lino, END, true);
32,632,513✔
562
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64, pReader->nextBlk, blockSz, uid);
×
563
    pReader->nextBlk++;
×
564
  }
565
  tqReaderClearSubmitMsg(pReader);
32,611,170✔
566
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
32,604,838✔
567

568
END:
32,604,838✔
569
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
65,237,351✔
570
  return code;
65,197,956✔
571
}
572

573
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask,
43,365,924✔
574
                    SExtSchema* extSrc) {
575
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
43,365,924✔
576
    return TSDB_CODE_INVALID_PARA;
×
577
  }
578
  int32_t code = 0;
43,371,904✔
579

580
  int32_t cnt = 0;
43,371,904✔
581
  for (int32_t i = 0; i < pSrc->nCols; i++) {
242,782,170✔
582
    cnt += mask[i];
199,402,740✔
583
  }
584

585
  pDst->nCols = cnt;
43,397,452✔
586
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
43,401,582✔
587
  if (pDst->pSchema == NULL) {
43,377,411✔
588
    return TAOS_GET_TERRNO(terrno);
×
589
  }
590

591
  int32_t j = 0;
43,379,894✔
592
  for (int32_t i = 0; i < pSrc->nCols; i++) {
242,848,905✔
593
    if (mask[i]) {
199,407,867✔
594
      pDst->pSchema[j++] = pSrc->pSchema[i];
199,453,828✔
595
      SColumnInfoData colInfo =
199,448,829✔
596
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
199,451,252✔
597
      if (extSrc != NULL) {
199,436,732✔
598
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
58,206✔
599
      }
600
      code = blockDataAppendColInfo(pBlock, &colInfo);
199,436,732✔
601
      if (code != 0) {
199,464,833✔
602
        return code;
×
603
      }
604
    }
605
  }
606
  return 0;
43,405,536✔
607
}
608

609
static int32_t buildResSDataBlock(STqReader* pReader, SSchemaWrapper* pSchema, const SArray* pColIdList) {
126,732✔
610
  if (pReader == NULL || pSchema == NULL || pColIdList == NULL) {
126,732✔
611
    return TSDB_CODE_INVALID_PARA;
×
612
  }
613
  blockDataDestroy(pReader->pResBlock);
126,732✔
614
  int32_t code = createDataBlock(&pReader->pResBlock);
126,383✔
615
  if (code) {
126,732✔
616
    return code;
×
617
  }
618
  SSDataBlock* pBlock = pReader->pResBlock;
126,732✔
619

620
  int32_t numOfCols = taosArrayGetSize(pColIdList);
126,732✔
621
  if (numOfCols == 0) {  // all columns are required
126,732✔
622
    for (int32_t i = 0; i < pSchema->nCols; ++i) {
×
623
      SSchema*        pColSchema = &pSchema->pSchema[i];
×
624
      SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
×
625

626
      if (IS_DECIMAL_TYPE(pColSchema->type) && pReader->extSchema != NULL) {
×
627
        decimalFromTypeMod(pReader->extSchema[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
×
628
      }
629
      int32_t code = blockDataAppendColInfo(pBlock, &colInfo);
×
630
      if (code != TSDB_CODE_SUCCESS) {
×
631
        blockDataFreeRes(pBlock);
×
632
        return code;
×
633
      }
634
    }
635
  } else {
636
    int32_t i = 0;
126,732✔
637
    int32_t j = 0;
126,732✔
638
    while (i < pSchema->nCols && j < numOfCols) {
944,839✔
639
      SSchema* pColSchema = &pSchema->pSchema[i];
818,107✔
640
      col_id_t colIdSchema = pColSchema->colId;
818,107✔
641

642
      col_id_t* pColIdNeed = (col_id_t*)taosArrayGet(pColIdList, j);
818,107✔
643
      if (pColIdNeed == NULL) {
817,853✔
644
        break;
×
645
      }
646
      if (colIdSchema < *pColIdNeed) {
817,853✔
647
        i++;
42,546✔
648
      } else if (colIdSchema > *pColIdNeed) {
775,307✔
649
        j++;
×
650
      } else {
651
        SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
775,307✔
652
        if (IS_DECIMAL_TYPE(pColSchema->type) && pReader->extSchema != NULL) {
775,561✔
653
          decimalFromTypeMod(pReader->extSchema[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
33,020✔
654
        }
655
        int32_t code = blockDataAppendColInfo(pBlock, &colInfo);
775,561✔
656
        if (code != TSDB_CODE_SUCCESS) {
775,561✔
657
          blockDataFreeRes(pBlock);
×
658
          return code;
×
659
        }
660
        i++;
775,561✔
661
        j++;
775,561✔
662
      }
663
    }
664
  }
665

666
  return TSDB_CODE_SUCCESS;
126,732✔
667
}
668

669
static int32_t doSetBlobVal(SColumnInfoData* pColumnInfoData, int32_t idx, SColVal* pColVal, SBlobSet* pBlobRow2) {
×
670
  int32_t code = 0;
×
671
  if (pColumnInfoData == NULL || pColVal == NULL || pBlobRow2 == NULL) {
×
672
    return TSDB_CODE_INVALID_PARA;
×
673
  }
674
  // TODO(yhDeng)
675
  if (COL_VAL_IS_VALUE(pColVal)) {
×
676
    char* val = taosMemCalloc(1, pColVal->value.nData + sizeof(BlobDataLenT));
×
677
    if (val == NULL) {
×
678
      return terrno;
×
679
    }
680

681
    uint64_t seq = 0;
×
682
    int32_t  len = 0;
×
683
    if (pColVal->value.pData != NULL) {
×
684
      if (tGetU64(pColVal->value.pData, &seq) < 0){
×
685
        TAOS_CHECK_RETURN(TSDB_CODE_INVALID_PARA);
×
686
      }
687
      SBlobItem item = {0};
×
688
      code = tBlobSetGet(pBlobRow2, seq, &item);
×
689
      if (code != 0) {
×
690
        taosMemoryFree(val);
×
691
        terrno = code;
×
692
        uError("tq set blob val, idx:%d, get blob item failed, seq:%" PRIu64 ", code:%d", idx, seq, code);
×
693
        return code;
×
694
      }
695

696
      val = taosMemRealloc(val, item.len + sizeof(BlobDataLenT));
×
697
      (void)memcpy(blobDataVal(val), item.data, item.len);
×
698
      len = item.len;
×
699
    }
700

701
    blobDataSetLen(val, len);
×
702
    code = colDataSetVal(pColumnInfoData, idx, val, false);
×
703

704
    taosMemoryFree(val);
×
705
  } else {
706
    colDataSetNULL(pColumnInfoData, idx);
×
707
  }
708
  return code;
×
709
}
710
static int32_t doSetVal(SColumnInfoData* pColumnInfoData, int32_t rowIndex, SColVal* pColVal) {
2,147,483,647✔
711
  int32_t code = TSDB_CODE_SUCCESS;
2,147,483,647✔
712

713
  if (IS_VAR_DATA_TYPE(pColVal->value.type)) {
2,147,483,647✔
714
    if (COL_VAL_IS_VALUE(pColVal)) {
2,147,483,647✔
715
      char val[65535 + 2] = {0};
2,147,483,647✔
716
      if (pColVal->value.pData != NULL) {
2,147,483,647✔
717
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
2,147,483,647✔
718
      }
719
      varDataSetLen(val, pColVal->value.nData);
2,147,483,647✔
720
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
2,147,483,647✔
721
    } else {
722
      colDataSetNULL(pColumnInfoData, rowIndex);
×
723
    }
724
  } else {
725
    code = colDataSetVal(pColumnInfoData, rowIndex, VALUE_GET_DATUM(&pColVal->value, pColVal->value.type),
2,147,483,647✔
726
                         !COL_VAL_IS_VALUE(pColVal));
2,147,483,647✔
727
  }
728

729
  return code;
2,147,483,647✔
730
}
731

732
static int32_t checkAndSetDataBlock(STqReader* pReader, SSubmitTbData* pSubmitTbData) {
89,185,674✔
733
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
89,185,674✔
734
  int32_t sversion = pSubmitTbData->sver;
89,186,561✔
735
  int64_t suid = pSubmitTbData->suid;
89,186,724✔
736
  int64_t uid = pSubmitTbData->uid;
89,186,549✔
737
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
89,186,899✔
738
      (pReader->cachedSchemaVer != sversion)) {
89,059,992✔
739
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
119,732✔
740
    taosMemoryFree(pReader->extSchema);
126,732✔
741
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
126,732✔
742
    if (pReader->pSchemaWrapper == NULL) {
126,732✔
743
      tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", uid:%" PRId64 ",version %d, possibly dropped table",
×
744
              vgId, suid, uid, pReader->cachedSchemaVer);
745
      pReader->cachedSchemaSuid = 0;
×
746
      return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
×
747
    }
748

749
    pReader->cachedSchemaUid = uid;
126,383✔
750
    pReader->cachedSchemaSuid = suid;
126,383✔
751
    pReader->cachedSchemaVer = sversion;
126,732✔
752

753
    return buildResSDataBlock(pReader, pReader->pSchemaWrapper, pReader->pColIdList);
126,732✔
754
  }
755
  return TSDB_CODE_SUCCESS;
89,058,067✔
756
}
757

758
int32_t tqRetrieveDataBlock(STqReader* pReader, SSDataBlock** pRes, const char* id) {
89,184,811✔
759
  if (pReader == NULL || pRes == NULL) {
89,184,811✔
760
    return TSDB_CODE_INVALID_PARA;
×
761
  }
762
  tqDebug("tq reader retrieve data block %p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
89,186,374✔
763
  int32_t        code = 0;
89,182,874✔
764
  int32_t        line = 0;
89,182,874✔
765
  STSchema*      pTSchema = NULL;
89,182,874✔
766
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk++);
89,182,874✔
767
  TSDB_CHECK_NULL(pSubmitTbData, code, line, END, terrno);
89,186,199✔
768
  pReader->lastTs = pSubmitTbData->ctimeMs;
89,186,199✔
769

770
  code = checkAndSetDataBlock(pReader, pSubmitTbData);
89,186,374✔
771
  TSDB_CHECK_CODE(code, line, END);
89,186,549✔
772

773
  int32_t numOfRows = 0;
89,186,549✔
774
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
89,186,549✔
775
    SColData* pCol = taosArrayGet(pSubmitTbData->aCol, 0);
1,288✔
776
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
1,288✔
777
    numOfRows = pCol->nVal;
1,288✔
778
  } else {
779
    numOfRows = taosArrayGetSize(pSubmitTbData->aRowP);
89,185,261✔
780
  }
781

782
  SSDataBlock* pBlock = pReader->pResBlock;
89,186,199✔
783
  *pRes = pBlock;
89,186,199✔
784
  blockDataCleanup(pBlock);
89,186,199✔
785
  pBlock->info.id.uid = pSubmitTbData->uid;
89,184,974✔
786
  pBlock->info.version = pReader->msg.ver;
89,181,824✔
787
  code = blockDataEnsureCapacity(pBlock, numOfRows);
89,186,024✔
788
  TSDB_CHECK_CODE(code, line, END);
89,186,263✔
789
  pBlock->info.rows = numOfRows;
89,186,263✔
790
  int32_t colActual = blockDataGetNumOfCols(pBlock);
89,186,100✔
791

792
  // convert and scan one block
793
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
89,186,031✔
794
    SArray* pCols = pSubmitTbData->aCol;
1,288✔
795
    int32_t numOfCols = taosArrayGetSize(pCols);
1,288✔
796
    int32_t targetIdx = 0;
1,288✔
797
    int32_t sourceIdx = 0;
1,288✔
798
    while (targetIdx < colActual) {
5,796✔
799
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
4,508✔
800
      TSDB_CHECK_NULL(pColData, code, line, END, terrno);
4,508✔
801
      if (sourceIdx >= numOfCols) {
4,508✔
802
        tqError("lostdata tqRetrieveDataBlock sourceIdx:%d >= numOfCols:%d", sourceIdx, numOfCols);
1,288✔
803
        colDataSetNNULL(pColData, 0, numOfRows);
1,288✔
804
        targetIdx++;
1,288✔
805
        continue;
1,288✔
806
      }
807

808
      uint8_t isBlob = IS_STR_DATA_BLOB(pColData->info.type) ? 1 : 0;
3,220✔
809

810
      SColData* pCol = taosArrayGet(pCols, sourceIdx);
3,220✔
811
      TSDB_CHECK_NULL(pCol, code, line, END, terrno);
3,220✔
812
      SColVal colVal = {0};
3,220✔
813
      tqTrace("lostdata colActual:%d, sourceIdx:%d, targetIdx:%d, numOfCols:%d, source cid:%d, dst cid:%d", colActual,
3,220✔
814
              sourceIdx, targetIdx, numOfCols, pCol->cid, pColData->info.colId);
815
      if (pCol->cid < pColData->info.colId) {
3,220✔
816
        sourceIdx++;
1,288✔
817
      } else if (pCol->cid == pColData->info.colId) {
1,932✔
818
        for (int32_t i = 0; i < pCol->nVal; i++) {
3,864✔
819
          code = tColDataGetValue(pCol, i, &colVal);
2,576✔
820
          TSDB_CHECK_CODE(code, line, END);
2,576✔
821

822
          if (isBlob == 0) {
2,576✔
823
            code = doSetVal(pColData, i, &colVal);
2,576✔
824
          } else {
825
            code = doSetBlobVal(pColData, i, &colVal, pSubmitTbData->pBlobSet);
×
826
          }
827
          TSDB_CHECK_CODE(code, line, END);
2,576✔
828
        }
829
        sourceIdx++;
1,288✔
830
        targetIdx++;
1,288✔
831
      } else {
832
        colDataSetNNULL(pColData, 0, numOfRows);
644✔
833
        targetIdx++;
644✔
834
      }
835
    }
836
  } else {
837
    SArray*         pRows = pSubmitTbData->aRowP;
89,181,780✔
838
    SSchemaWrapper* pWrapper = pReader->pSchemaWrapper;
89,183,455✔
839
    pTSchema = tBuildTSchema(pWrapper->pSchema, pWrapper->nCols, pWrapper->version);
89,184,067✔
840
    TSDB_CHECK_NULL(pTSchema, code, line, END, terrno);
89,181,216✔
841

842
    for (int32_t i = 0; i < numOfRows; i++) {
2,147,483,647✔
843
      SRow* pRow = taosArrayGetP(pRows, i);
2,147,483,647✔
844
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
2,147,483,647✔
845
      int32_t sourceIdx = 0;
2,147,483,647✔
846
      for (int32_t j = 0; j < colActual; j++) {
2,147,483,647✔
847
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, j);
2,147,483,647✔
848
        TSDB_CHECK_NULL(pColData, code, line, END, terrno);
2,147,483,647✔
849

850
        uint8_t isBlob = IS_STR_DATA_BLOB(pColData->info.type) ? 1 : 0;
2,147,483,647✔
851
        while (1) {
216,188,280✔
852
          SColVal colVal = {0};
2,147,483,647✔
853
          code = tRowGet(pRow, pTSchema, sourceIdx, &colVal);
2,147,483,647✔
854
          TSDB_CHECK_CODE(code, line, END);
2,147,483,647✔
855

856
          if (colVal.cid < pColData->info.colId) {
2,147,483,647✔
857
            sourceIdx++;
216,190,359✔
858
            continue;
216,190,359✔
859
          } else if (colVal.cid == pColData->info.colId) {
2,147,483,647✔
860
            if (isBlob == 0) {
2,147,483,647✔
861
              code = doSetVal(pColData, i, &colVal);
2,147,483,647✔
862
            } else {
863
              code = doSetBlobVal(pColData, i, &colVal, pSubmitTbData->pBlobSet);
×
864
            }
865

866
            TSDB_CHECK_CODE(code, line, END);
2,147,483,647✔
867

868
            sourceIdx++;
2,147,483,647✔
869
            break;
2,147,483,647✔
870
          } else {
871
            colDataSetNULL(pColData, i);
×
872
            break;
×
873
          }
874
        }
875
      }
876
    }
877
  }
878

879
END:
128,187,566✔
880
  if (code != 0) {
146,660,949✔
881
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
882
  }
883
  taosMemoryFreeClear(pTSchema);
89,177,077✔
884
  return code;
89,178,507✔
885
}
886

887
#define PROCESS_VAL                                      \
888
  if (curRow == 0) {                                     \
889
    assigned[j] = !COL_VAL_IS_NONE(&colVal);             \
890
    buildNew = true;                                     \
891
  } else {                                               \
892
    bool currentRowAssigned = !COL_VAL_IS_NONE(&colVal); \
893
    if (currentRowAssigned != assigned[j]) {             \
894
      assigned[j] = currentRowAssigned;                  \
895
      buildNew = true;                                   \
896
    }                                                    \
897
  }
898

899
#define SET_DATA                                                                                    \
900
  if (colVal.cid < pColData->info.colId) {                                                          \
901
    sourceIdx++;                                                                                    \
902
  } else if (colVal.cid == pColData->info.colId) {                                                  \
903
    if (IS_STR_DATA_BLOB(pColData->info.type)) {                                                    \
904
      TQ_ERR_GO_TO_END(doSetBlobVal(pColData, curRow - lastRow, &colVal, pSubmitTbData->pBlobSet)); \
905
    } else {                                                                                        \
906
      TQ_ERR_GO_TO_END(doSetVal(pColData, curRow - lastRow, &colVal));                              \
907
    }                                                                                               \
908
    sourceIdx++;                                                                                    \
909
    targetIdx++;                                                                                    \
910
  } else {                                                                                          \
911
    colDataSetNULL(pColData, curRow - lastRow);                                                     \
912
    targetIdx++;                                                                                    \
913
  }
914

915
static int32_t processBuildNew(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas,
43,356,417✔
916
                               char* assigned, int32_t numOfRows, int32_t curRow, int32_t* lastRow) {
917
  int32_t         code = 0;
43,356,417✔
918
  SSchemaWrapper* pSW = NULL;
43,356,417✔
919
  SSDataBlock*    block = NULL;
43,371,621✔
920
  if (taosArrayGetSize(blocks) > 0) {
43,371,621✔
921
    SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
×
922
    TQ_NULL_GO_TO_END(pLastBlock);
×
923
    pLastBlock->info.rows = curRow - *lastRow;
×
924
    *lastRow = curRow;
×
925
  }
926

927
  block = taosMemoryCalloc(1, sizeof(SSDataBlock));
43,387,389✔
928
  TQ_NULL_GO_TO_END(block);
43,363,427✔
929

930
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
43,363,427✔
931
  TQ_NULL_GO_TO_END(pSW);
43,372,762✔
932

933
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pReader->pSchemaWrapper, assigned, pReader->extSchema));
43,372,762✔
934
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
43,405,198✔
935
          (int32_t)taosArrayGetSize(block->pDataBlock));
936

937
  block->info.id.uid = pSubmitTbData->uid;
43,405,198✔
938
  block->info.version = pReader->msg.ver;
43,396,945✔
939
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
43,399,056✔
940
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
43,397,818✔
941
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
43,396,244✔
942
  pSW = NULL;
43,396,244✔
943

944
  taosMemoryFreeClear(block);
43,396,244✔
945

946
END:
43,402,699✔
947
  if (code != 0) {
43,399,712✔
948
    tqError("processBuildNew failed, code:%d", code);
×
949
  }
950
  tDeleteSchemaWrapper(pSW);
43,399,712✔
951
  blockDataFreeRes(block);
43,382,046✔
952
  taosMemoryFree(block);
43,377,390✔
953
  return code;
43,395,502✔
954
}
955
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
589,976✔
956
  int32_t code = 0;
589,976✔
957
  int32_t curRow = 0;
589,976✔
958
  int32_t lastRow = 0;
589,976✔
959

960
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
589,976✔
961
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
589,976✔
962
  TQ_NULL_GO_TO_END(assigned);
589,661✔
963

964
  SArray*   pCols = pSubmitTbData->aCol;
589,661✔
965
  SColData* pCol = taosArrayGet(pCols, 0);
589,976✔
966
  TQ_NULL_GO_TO_END(pCol);
589,661✔
967
  int32_t numOfRows = pCol->nVal;
589,661✔
968
  int32_t numOfCols = taosArrayGetSize(pCols);
589,976✔
969
  tqTrace("vgId:%d, tqProcessColData start, col num: %d, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfCols,
589,661✔
970
          numOfRows);
971
  for (int32_t i = 0; i < numOfRows; i++) {
99,244,199✔
972
    bool buildNew = false;
98,248,931✔
973

974
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
393,991,848✔
975
      int32_t k = 0;
294,243,257✔
976
      for (; k < numOfCols; k++) {
587,589,979✔
977
        pCol = taosArrayGet(pCols, k);
580,179,061✔
978
        TQ_NULL_GO_TO_END(pCol);
580,412,126✔
979
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
580,412,126✔
980
          SColVal colVal = {0};
294,037,671✔
981
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
294,526,831✔
982
          PROCESS_VAL
296,050,548✔
983
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
296,080,630✔
984
          break;
290,636,835✔
985
        }
986
      }
987
      if (k >= numOfCols) {
295,742,917✔
988
        // this column is not in the current row, so we set it to NULL
989
        assigned[j] = 0;
×
990
        buildNew = true;
×
991
      }
992
    }
993

994
    if (buildNew) {
98,166,742✔
995
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
589,976✔
996
    }
997

998
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
98,167,055✔
999
    TQ_NULL_GO_TO_END(pBlock);
98,682,168✔
1000

1001
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
98,682,168✔
1002
            (int32_t)taosArrayGetSize(blocks));
1003

1004
    int32_t targetIdx = 0;
98,682,168✔
1005
    int32_t sourceIdx = 0;
98,682,168✔
1006
    int32_t colActual = blockDataGetNumOfCols(pBlock);
98,682,168✔
1007
    while (targetIdx < colActual && sourceIdx < numOfCols) {
392,810,716✔
1008
      pCol = taosArrayGet(pCols, sourceIdx);
294,156,491✔
1009
      TQ_NULL_GO_TO_END(pCol);
294,114,932✔
1010
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
294,114,932✔
1011
      TQ_NULL_GO_TO_END(pColData);
288,407,627✔
1012
      SColVal colVal = {0};
288,407,627✔
1013
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
293,863,888✔
1014
      SET_DATA
295,856,231✔
1015
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
294,092,747✔
1016
    }
1017

1018
    curRow++;
98,654,225✔
1019
  }
1020
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
995,268✔
1021
  pLastBlock->info.rows = curRow - lastRow;
590,289✔
1022
  tqTrace("vgId:%d, tqProcessColData end, col num: %d, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId,
590,289✔
1023
          numOfCols, numOfRows, (int)taosArrayGetSize(blocks));
1024
END:
9,638,168✔
1025
  if (code != TSDB_CODE_SUCCESS) {
590,289✔
1026
    tqError("vgId:%d, process col data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1027
  }
1028
  taosMemoryFree(assigned);
590,289✔
1029
  return code;
590,289✔
1030
}
1031

1032
int32_t tqProcessRowData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
42,774,161✔
1033
  int32_t   code = 0;
42,774,161✔
1034
  STSchema* pTSchema = NULL;
42,774,161✔
1035

1036
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
42,774,161✔
1037
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
42,798,835✔
1038
  TQ_NULL_GO_TO_END(assigned);
42,792,805✔
1039

1040
  int32_t curRow = 0;
42,792,805✔
1041
  int32_t lastRow = 0;
42,792,805✔
1042
  SArray* pRows = pSubmitTbData->aRowP;
42,781,837✔
1043
  int32_t numOfRows = taosArrayGetSize(pRows);
42,803,419✔
1044
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
42,800,370✔
1045
  TQ_NULL_GO_TO_END(pTSchema);
42,804,083✔
1046
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
42,804,083✔
1047

1048
  for (int32_t i = 0; i < numOfRows; i++) {
1,250,224,669✔
1049
    bool  buildNew = false;
1,207,261,252✔
1050
    SRow* pRow = taosArrayGetP(pRows, i);
1,207,261,252✔
1051
    TQ_NULL_GO_TO_END(pRow);
1,207,122,591✔
1052

1053
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
2,147,483,647✔
1054
      SColVal colVal = {0};
2,147,483,647✔
1055
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
2,147,483,647✔
1056
      PROCESS_VAL
2,147,483,647✔
1057
      tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], pTSchema->numOfCols);
2,147,483,647✔
1058
    }
1059

1060
    if (buildNew) {
1,202,717,833✔
1061
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
42,812,617✔
1062
    }
1063

1064
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
1,202,702,827✔
1065
    TQ_NULL_GO_TO_END(pBlock);
1,207,583,371✔
1066

1067
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
1,207,583,371✔
1068
            (int32_t)taosArrayGetSize(blocks));
1069

1070
    int32_t targetIdx = 0;
1,207,583,371✔
1071
    int32_t sourceIdx = 0;
1,207,583,371✔
1072
    int32_t colActual = blockDataGetNumOfCols(pBlock);
1,207,583,371✔
1073
    while (targetIdx < colActual && sourceIdx < pTSchema->numOfCols) {
2,147,483,647✔
1074
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
2,147,483,647✔
1075
      TQ_NULL_GO_TO_END(pColData);
2,147,483,647✔
1076
      SColVal          colVal = {0};
2,147,483,647✔
1077
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
2,147,483,647✔
1078
      SET_DATA
2,147,483,647✔
1079
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
2,147,483,647✔
1080
    }
1081

1082
    curRow++;
1,207,454,698✔
1083
  }
1084
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
42,963,417✔
1085
  if (pLastBlock != NULL) {
42,811,613✔
1086
    pLastBlock->info.rows = curRow - lastRow;
42,813,657✔
1087
  }
1088

1089
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
42,813,695✔
1090
          (int)taosArrayGetSize(blocks));
1091
END:
45,849,399✔
1092
  if (code != TSDB_CODE_SUCCESS) {
42,802,501✔
1093
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1094
  }
1095
  taosMemoryFreeClear(pTSchema);
42,792,513✔
1096
  taosMemoryFree(assigned);
42,785,769✔
1097
  return code;
42,795,869✔
1098
}
1099

1100
static int32_t buildCreateTbInfo(SMqDataRsp* pRsp, SVCreateTbReq* pCreateTbReq) {
8,150✔
1101
  int32_t code = 0;
8,150✔
1102
  int32_t lino = 0;
8,150✔
1103
  void*   createReq = NULL;
8,150✔
1104
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
8,150✔
1105
  TSDB_CHECK_NULL(pCreateTbReq, code, lino, END, TSDB_CODE_INVALID_PARA);
8,150✔
1106

1107
  if (pRsp->createTableNum == 0) {
8,150✔
1108
    pRsp->createTableLen = taosArrayInit(0, sizeof(int32_t));
4,412✔
1109
    TSDB_CHECK_NULL(pRsp->createTableLen, code, lino, END, terrno);
4,412✔
1110
    pRsp->createTableReq = taosArrayInit(0, sizeof(void*));
4,412✔
1111
    TSDB_CHECK_NULL(pRsp->createTableReq, code, lino, END, terrno);
4,412✔
1112
  }
1113

1114
  uint32_t len = 0;
8,150✔
1115
  tEncodeSize(tEncodeSVCreateTbReq, pCreateTbReq, len, code);
8,150✔
1116
  TSDB_CHECK_CODE(code, lino, END);
8,150✔
1117
  createReq = taosMemoryCalloc(1, len);
8,150✔
1118
  TSDB_CHECK_NULL(createReq, code, lino, END, terrno);
8,150✔
1119

1120
  SEncoder encoder = {0};
8,150✔
1121
  tEncoderInit(&encoder, createReq, len);
8,150✔
1122
  code = tEncodeSVCreateTbReq(&encoder, pCreateTbReq);
8,150✔
1123
  tEncoderClear(&encoder);
8,150✔
1124
  TSDB_CHECK_CODE(code, lino, END);
8,150✔
1125
  TSDB_CHECK_NULL(taosArrayPush(pRsp->createTableLen, &len), code, lino, END, terrno);
16,300✔
1126
  TSDB_CHECK_NULL(taosArrayPush(pRsp->createTableReq, &createReq), code, lino, END, terrno);
16,300✔
1127
  pRsp->createTableNum++;
8,150✔
1128
  tqTrace("build create table info msg success");
8,150✔
1129

1130
END:
8,150✔
1131
  if (code != 0) {
8,150✔
1132
    tqError("%s failed at %d, failed to build create table info msg:%s", __FUNCTION__, lino, tstrerror(code));
×
1133
    taosMemoryFree(createReq);
×
1134
  }
1135
  return code;
8,150✔
1136
}
1137

1138
int32_t tqRetrieveTaosxBlock(STqReader* pReader, SMqDataRsp* pRsp, SArray* blocks, SArray* schemas,
43,603,980✔
1139
                             SSubmitTbData** pSubmitTbDataRet, SArray* rawList, int8_t fetchMeta) {
1140
  tqTrace("tq reader retrieve data block msg pointer:%p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
43,603,980✔
1141
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
43,603,980✔
1142
  if (pSubmitTbData == NULL) {
43,608,431✔
1143
    return terrno;
×
1144
  }
1145
  pReader->nextBlk++;
43,608,431✔
1146

1147
  if (pSubmitTbDataRet) {
43,607,171✔
1148
    *pSubmitTbDataRet = pSubmitTbData;
43,608,723✔
1149
  }
1150

1151
  if (fetchMeta == ONLY_META) {
43,607,463✔
1152
    if (pSubmitTbData->pCreateTbReq != NULL) {
6,774✔
1153
      if (pRsp->createTableReq == NULL) {
1,968✔
1154
        pRsp->createTableReq = taosArrayInit(0, POINTER_BYTES);
1,167✔
1155
        if (pRsp->createTableReq == NULL) {
1,167✔
1156
          return terrno;
×
1157
        }
1158
      }
1159
      if (taosArrayPush(pRsp->createTableReq, &pSubmitTbData->pCreateTbReq) == NULL) {
3,936✔
1160
        return terrno;
×
1161
      }
1162
      pSubmitTbData->pCreateTbReq = NULL;
1,968✔
1163
    }
1164
    return 0;
6,774✔
1165
  }
1166

1167
  int32_t sversion = pSubmitTbData->sver;
43,600,689✔
1168
  int64_t uid = pSubmitTbData->uid;
43,600,714✔
1169
  pReader->lastBlkUid = uid;
43,595,228✔
1170

1171
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
43,599,137✔
1172
  taosMemoryFreeClear(pReader->extSchema);
43,594,506✔
1173
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
43,601,252✔
1174
  if (pReader->pSchemaWrapper == NULL) {
43,588,585✔
1175
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
198,503✔
1176
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1177
    pReader->cachedSchemaSuid = 0;
198,503✔
1178
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
198,503✔
1179
  }
1180

1181
  if (pSubmitTbData->pCreateTbReq != NULL) {
43,374,247✔
1182
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
8,150✔
1183
    if (code != 0) {
8,150✔
1184
      return code;
×
1185
    }
1186
  } else if (rawList != NULL) {
43,380,660✔
1187
    if (taosArrayPush(schemas, &pReader->pSchemaWrapper) == NULL) {
×
1188
      return terrno;
×
1189
    }
1190
    pReader->pSchemaWrapper = NULL;
×
1191
    return 0;
×
1192
  }
1193

1194
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
43,388,810✔
1195
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
589,976✔
1196
  } else {
1197
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
42,767,858✔
1198
  }
1199
}
1200

1201
int32_t tqReaderSetColIdList(STqReader* pReader, SArray* pColIdList, const char* id) {
309,950✔
1202
  if (pReader != NULL) {
309,950✔
1203
    pReader->pColIdList = pColIdList;
309,950✔
1204
  }
1205
  return TSDB_CODE_SUCCESS;
310,274✔
1206
}
1207

1208
int32_t tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
330,487✔
1209
  if (pReader == NULL || tbUidList == NULL) {
330,487✔
1210
    return TSDB_CODE_SUCCESS;
×
1211
  }
1212
  if (pReader->tbIdHash) {
330,487✔
1213
    taosHashClear(pReader->tbIdHash);
×
1214
  } else {
1215
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
330,164✔
1216
    if (pReader->tbIdHash == NULL) {
330,811✔
1217
      tqError("s-task:%s failed to init hash table", id);
×
1218
      return terrno;
×
1219
    }
1220
  }
1221

1222
  for (int i = 0; i < taosArrayGetSize(tbUidList); i++) {
9,555,628✔
1223
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
9,224,855✔
1224
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
9,195,719✔
1225
      tqError("s-task:%s failed to add table uid:%" PRId64 " to hash", id, *pKey);
×
1226
      continue;
×
1227
    }
1228
  }
1229

1230
  tqDebug("s-task:%s %d tables are set to be queried target table", id, (int32_t)taosArrayGetSize(tbUidList));
330,487✔
1231
  return TSDB_CODE_SUCCESS;
330,811✔
1232
}
1233

1234
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
399,384✔
1235
  if (pReader == NULL || pTableUidList == NULL) {
399,384✔
1236
    return;
×
1237
  }
1238
  if (pReader->tbIdHash == NULL) {
399,384✔
1239
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
×
1240
    if (pReader->tbIdHash == NULL) {
×
1241
      tqError("failed to init hash table");
×
1242
      return;
×
1243
    }
1244
  }
1245

1246
  int32_t numOfTables = taosArrayGetSize(pTableUidList);
399,384✔
1247
  for (int i = 0; i < numOfTables; i++) {
799,273✔
1248
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
399,889✔
1249
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
399,889✔
1250
      tqError("failed to add table uid:%" PRId64 " to hash", *pKey);
×
1251
      continue;
×
1252
    }
1253
    tqDebug("%s add table uid:%" PRId64 " to hash", __func__, *pKey);
399,889✔
1254
  }
1255
}
1256

1257
bool tqReaderIsQueriedTable(STqReader* pReader, uint64_t uid) {
×
1258
  if (pReader == NULL) {
×
1259
    return false;
×
1260
  }
1261
  return taosHashGet(pReader->tbIdHash, &uid, sizeof(uint64_t)) != NULL;
×
1262
}
1263

1264
bool tqCurrentBlockConsumed(const STqReader* pReader) {
×
1265
  if (pReader == NULL) {
×
1266
    return false;
×
1267
  }
1268
  return pReader->msg.msgStr == NULL;
×
1269
}
1270

1271
void tqReaderRemoveTbUidList(STqReader* pReader, const SArray* tbUidList) {
2,428✔
1272
  if (pReader == NULL || tbUidList == NULL) {
2,428✔
1273
    return;
×
1274
  }
1275
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
3,460✔
1276
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
1,032✔
1277
    int32_t code = taosHashRemove(pReader->tbIdHash, pKey, sizeof(int64_t));
1,032✔
1278
    if (code != 0) {
1,032✔
1279
      tqWarn("%s failed to remove table uid:%" PRId64 " from hash, msg:%s", __func__, pKey != NULL ? *pKey : 0, tstrerror(code));
344✔
1280
    }
1281
  }
1282
}
1283

1284
int32_t tqDeleteTbUidList(STQ* pTq, SArray* tbUidList) {
2,200,020✔
1285
  if (pTq == NULL) {
2,200,020✔
1286
    return 0;  // mounted vnode may have no tq
×
1287
  }
1288
  if (tbUidList == NULL) {
2,200,020✔
1289
    return TSDB_CODE_INVALID_PARA;
×
1290
  }
1291
  void*   pIter = NULL;
2,200,020✔
1292
  int32_t vgId = TD_VID(pTq->pVnode);
2,200,020✔
1293

1294
  // update the table list for each consumer handle
1295
  taosWLockLatch(&pTq->lock);
2,200,020✔
1296
  while (1) {
312,160✔
1297
    pIter = taosHashIterate(pTq->pHandle, pIter);
2,512,180✔
1298
    if (pIter == NULL) {
2,512,180✔
1299
      break;
2,200,020✔
1300
    }
1301

1302
    STqHandle* pTqHandle = (STqHandle*)pIter;
312,160✔
1303
    tqDebug("%s subKey:%s, consumer:0x%" PRIx64 " delete table list", __func__, pTqHandle->subKey, pTqHandle->consumerId);
312,160✔
1304
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
312,160✔
1305
      int32_t code = qDeleteTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList);
1,396✔
1306
      if (code != 0) {
1,396✔
1307
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1308
        continue;
×
1309
      }
1310
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
310,764✔
1311
      int32_t sz = taosArrayGetSize(tbUidList);
310,764✔
1312
      for (int32_t i = 0; i < sz; i++) {
310,764✔
NEW
1313
        int64_t* tbUid = (int64_t*)taosArrayGet(tbUidList, i);
×
NEW
1314
        if (tbUid &&
×
NEW
1315
            taosHashPut(pTqHandle->execHandle.execDb.pFilterOutTbUid, tbUid, sizeof(int64_t), NULL, 0) != 0) {
×
NEW
1316
          tqError("failed to add table uid:%" PRId64 " to hash", *tbUid);
×
NEW
1317
          continue;
×
1318
        }
1319
      }
1320
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
×
NEW
1321
      tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
×
1322
    }
1323
  }
1324
  taosWUnLockLatch(&pTq->lock);
2,200,020✔
1325
  return 0;
2,200,020✔
1326
}
1327

1328
static int32_t addTableListForStableTmq(STqHandle* pTqHandle, STQ* pTq, SArray* tbUidList) {
5,847✔
1329
  int     ret = qFilterTableList(pTq->pVnode, tbUidList, pTqHandle->execHandle.execTb.node,
5,847✔
1330
                      pTqHandle->execHandle.task, pTqHandle->execHandle.execTb.suid);
5,847✔
1331
  if (ret != TDB_CODE_SUCCESS) {
5,847✔
NEW
1332
    tqError("tqAddTbUidList error:%d handle %s consumer:0x%" PRIx64, ret, pTqHandle->subKey,
×
1333
            pTqHandle->consumerId);
NEW
1334
    return ret;
×
1335
  }
1336
  tqDebug("%s handle %s consumer:0x%" PRIx64 " add %d tables to tqReader", __func__, pTqHandle->subKey,
5,847✔
1337
          pTqHandle->consumerId, (int32_t)taosArrayGetSize(tbUidList));
1338
  tqReaderAddTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
5,847✔
1339
  return 0;
5,847✔
1340
}
1341

1342
int32_t tqAddTbUidList(STQ* pTq, SArray* tbUidList) {
52,463,901✔
1343
  if (pTq == NULL) {
52,463,901✔
NEW
1344
    return 0;  // mounted vnode may have no tq
×
1345
  }
1346
  if (tbUidList == NULL) {
52,463,901✔
NEW
1347
    return TSDB_CODE_INVALID_PARA;
×
1348
  }
1349
  void*   pIter = NULL;
52,463,901✔
1350
  int32_t vgId = TD_VID(pTq->pVnode);
52,463,901✔
1351
  int32_t code = 0;
52,464,034✔
1352

1353
  // update the table list for each consumer handle
1354
  taosWLockLatch(&pTq->lock);
52,464,034✔
1355
  while (1) {
1,011,825✔
1356
    pIter = taosHashIterate(pTq->pHandle, pIter);
53,476,225✔
1357
    if (pIter == NULL) {
53,475,859✔
1358
      break;
52,464,034✔
1359
    }
1360

1361
    STqHandle* pTqHandle = (STqHandle*)pIter;
1,011,825✔
1362
    tqDebug("%s subKey:%s, consumer:0x%" PRIx64 " add table list", __func__, pTqHandle->subKey, pTqHandle->consumerId);
1,011,825✔
1363
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
1,011,825✔
1364
      code = qAddTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList);
393,537✔
1365
      if (code != 0) {
393,537✔
NEW
1366
        tqError("add table list for query tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
NEW
1367
        break;
×
1368
      }
1369
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
618,288✔
1370
      code = addTableListForStableTmq(pTqHandle, pTq, tbUidList);
4,815✔
1371
      if (code != 0) {
4,815✔
NEW
1372
        tqError("add table list for stable tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
NEW
1373
        break;
×
1374
      }
1375
    }
1376
  }
1377
  taosHashCancelIterate(pTq->pHandle, pIter);
52,464,034✔
1378
  taosWUnLockLatch(&pTq->lock);
52,463,212✔
1379

1380
  return code;
52,464,400✔
1381
}
1382

1383
int32_t tqUpdateTbUidList(STQ* pTq, SArray* tbUidList, SArray* cidList) {
7,442,726✔
1384
  if (pTq == NULL) {
7,442,726✔
NEW
1385
    return 0;  // mounted vnode may have no tq
×
1386
  }
1387
  if (tbUidList == NULL) {
7,442,726✔
NEW
1388
    return TSDB_CODE_INVALID_PARA;
×
1389
  }
1390
  void*   pIter = NULL;
7,442,726✔
1391
  int32_t vgId = TD_VID(pTq->pVnode);
7,442,726✔
1392
  int32_t code = 0;
7,442,726✔
1393
  // update the table list for each consumer handle
1394
  taosWLockLatch(&pTq->lock);
7,442,726✔
1395
  while (1) {
2,709✔
1396
    pIter = taosHashIterate(pTq->pHandle, pIter);
7,445,435✔
1397
    if (pIter == NULL) {
7,445,435✔
1398
      break;
7,442,726✔
1399
    }
1400

1401
    STqHandle* pTqHandle = (STqHandle*)pIter;
2,709✔
1402
    tqDebug("%s subKey:%s, consumer:0x%" PRIx64 " update table list", __func__, pTqHandle->subKey, pTqHandle->consumerId);
2,709✔
1403
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
2,709✔
1404
      SNode* pTagCond = getTagCondNodeForQueryTmq(pTqHandle->execHandle.task);
645✔
1405
      bool ret = checkCidInTagCondition(pTagCond, cidList);
645✔
1406
      if (ret){
645✔
NEW
1407
        code = qUpdateTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList);
×
NEW
1408
        if (code != 0) {
×
NEW
1409
          tqError("update table list for query tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
NEW
1410
          break;
×
1411
        }
1412
      }
1413
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
2,064✔
1414
      SNode* pTagCond = getTagCondNodeForStableTmq(pTqHandle->execHandle.execTb.node);
2,064✔
1415
      bool ret = checkCidInTagCondition(pTagCond, cidList);
2,064✔
1416
      if (ret){
2,064✔
1417
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
1,032✔
1418
        code = addTableListForStableTmq(pTqHandle, pTq, tbUidList);
1,032✔
1419
        if (code != 0) {
1,032✔
NEW
1420
          tqError("update table list for stable tmq error for %s, msg:%s", pTqHandle->subKey, tstrerror(code));
×
NEW
1421
          break;
×
1422
        }
1423
      }
1424
    }
1425
  }
1426

1427
  taosHashCancelIterate(pTq->pHandle, pIter);
7,442,726✔
1428
  taosWUnLockLatch(&pTq->lock);
7,442,726✔
1429

1430
  return code;
7,442,726✔
1431
}
1432

UNCOV
1433
static void destroySourceScanTables(void* ptr) {
×
UNCOV
1434
  SArray** pTables = ptr;
×
UNCOV
1435
  if (pTables && *pTables) {
×
UNCOV
1436
    taosArrayDestroy(*pTables);
×
UNCOV
1437
    *pTables = NULL;
×
1438
  }
UNCOV
1439
}
×
1440

UNCOV
1441
static int32_t compareSVTColInfo(const void* p1, const void* p2) {
×
UNCOV
1442
  SVTColInfo* pCol1 = (SVTColInfo*)p1;
×
UNCOV
1443
  SVTColInfo* pCol2 = (SVTColInfo*)p2;
×
UNCOV
1444
  if (pCol1->vColId == pCol2->vColId) {
×
1445
    return 0;
×
1446
  } else if (pCol1->vColId < pCol2->vColId) {
×
UNCOV
1447
    return -1;
×
1448
  } else {
UNCOV
1449
    return 1;
×
1450
  }
1451
}
1452

UNCOV
1453
static void freeTableSchemaCache(const void* key, size_t keyLen, void* value, void* ud) {
×
UNCOV
1454
  if (value) {
×
UNCOV
1455
    SSchemaWrapper* pSchemaWrapper = value;
×
1456
    tDeleteSchemaWrapper(pSchemaWrapper);
1457
  }
UNCOV
1458
}
×
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