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

taosdata / TDengine / #4913

06 Jan 2026 01:30AM UTC coverage: 64.884% (-0.004%) from 64.888%
#4913

push

travis-ci

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

180 of 319 new or added lines in 14 files covered. (56.43%)

571 existing lines in 128 files now uncovered.

195016 of 300563 relevant lines covered (64.88%)

117540852.85 hits per line

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

73.53
/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,207✔
22
  int32_t code = 0;
1,207✔
23
  int32_t lino = 0;
1,207✔
24
  int32_t        needRebuild = 0;
1,207✔
25
  SVCreateTbReq* pCreateReq = NULL;
1,207✔
26
  SVCreateTbBatchReq reqNew = {0};
1,207✔
27
  void* buf = NULL;
1,207✔
28
  SVCreateTbBatchReq req = {0};
1,207✔
29
  code = tDecodeSVCreateTbBatchReq(dcoder, &req);
1,207✔
30
  if (code < 0) {
1,207✔
31
    lino = __LINE__;
×
32
    goto end;
×
33
  }
34

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

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

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

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

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

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

116
end:
906✔
117
  taosArrayDestroy(req.pMultiTag);
906✔
118
  metaReaderClear(&mr);  
906✔
119
  if (code < 0) {
906✔
120
    tqError("processAlterTbMsg failed, code:%d, line:%d", code, lino);
×
121
  }
122
} 
906✔
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) {
144,777✔
198
  int32_t code = 0;
144,777✔
199
  int32_t lino = 0;
144,777✔
200
  if (pHandle == NULL || pHead == NULL) {
144,777✔
UNCOV
201
    return false;
×
202
  }
203
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
144,777✔
204
    return true;
142,061✔
205
  }
206

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

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

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

221
  if (msgType == TDMT_VND_CREATE_STB || msgType == TDMT_VND_ALTER_STB) {
3,319✔
222
    SVCreateStbReq req = {0};
603✔
223
    if (tDecodeSVCreateStbReq(&dcoder, &req) < 0) {
603✔
224
      goto end;
×
225
    }
226
    realTbSuid = req.suid;
603✔
227
  } else if (msgType == TDMT_VND_DROP_STB) {
2,113✔
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,113✔
234
    processCreateTbMsg(&dcoder, pHead, pReader, &realTbSuid, tbSuid);
1,207✔
235
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
906✔
236
    processAlterTbMsg(&dcoder, pReader, &realTbSuid);
906✔
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,716✔
248
  tDecoderClear(&dcoder);
2,716✔
249
  bool tmp = tbSuid == realTbSuid;
2,716✔
250
  tqDebug("%s suid:%" PRId64 " realSuid:%" PRId64 " return:%d", __FUNCTION__, tbSuid, realTbSuid, tmp);
2,716✔
251
  return tmp;
2,716✔
252
}
253

254
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
47,714,139✔
255
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
47,714,139✔
256
    return -1;
×
257
  }
258
  int32_t code = -1;
47,783,183✔
259
  int32_t vgId = TD_VID(pTq->pVnode);
47,783,183✔
260
  int64_t id = pHandle->pWalReader->readerId;
47,789,857✔
261

262
  int64_t offset = *fetchOffset;
47,785,685✔
263
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
47,796,091✔
264
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
47,793,862✔
265
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
47,794,726✔
266

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

271
  while (offset <= appliedVer) {
50,371,874✔
272
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
45,720,036✔
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,
45,719,492✔
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) {
45,718,841✔
283
      code = walFetchBody(pHandle->pWalReader);
43,006,365✔
284
      goto END;
43,001,640✔
285
    } else {
286
      if (pHandle->fetchMeta != WITH_DATA) {
2,713,960✔
287
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
186,242✔
288
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
186,242✔
289
          code = walFetchBody(pHandle->pWalReader);
144,777✔
290
          if (code < 0) {
144,522✔
291
            goto END;
×
292
          }
293

294
          pHead = &(pHandle->pWalReader->pHead->head);
144,522✔
295
          if (isValValidForTable(pHandle, pHead)) {
144,777✔
296
            code = 0;
143,616✔
297
            goto END;
143,616✔
298
          } else {
299
            offset++;
906✔
300
            code = -1;
906✔
301
            continue;
906✔
302
          }
303
        }
304
      }
305
      code = walSkipFetchBody(pHandle->pWalReader);
2,569,183✔
306
      if (code < 0) {
2,569,183✔
307
        goto END;
×
308
      }
309
      offset++;
2,569,183✔
310
    }
311
    code = -1;
2,569,183✔
312
  }
313

314
END:
4,651,838✔
315
  *fetchOffset = offset;
47,797,094✔
316
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
47,799,684✔
317
          ", applied:%" PRId64 ", 0x%" PRIx64,
318
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
319
  return code;
47,801,746✔
320
}
321

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

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

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

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

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

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

368
  return pReader;
420,314✔
369
}
370

371
void tqReaderClose(STqReader* pReader) {
424,311✔
372
  tqDebug("%s:%p", __FUNCTION__, pReader);
424,311✔
373
  if (pReader == NULL) return;
424,566✔
374

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

380
  if (pReader->pSchemaWrapper) {
420,314✔
381
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
191,986✔
382
  }
383

384
  taosMemoryFree(pReader->extSchema);
420,314✔
385
  if (pReader->pColIdList) {
420,314✔
386
    taosArrayDestroy(pReader->pColIdList);
312,032✔
387
  }
388

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

394
  taosMemoryFree(pReader);
420,314✔
395
}
396

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

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

414
  int64_t st = taosGetTimestampMs();
116,161,718✔
415
  while (1) {
115,737,058✔
416
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
231,898,776✔
417
    while (pReader->nextBlk < numOfBlocks) {
250,046,594✔
418
      tqDebug("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
115,744,435✔
419
              pReader->msg.ver);
420

421
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
115,755,940✔
422
      if (pSubmitTbData == NULL) {
115,755,868✔
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;
162✔
426
      }
427
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
115,755,868✔
428
        pReader->nextBlk += 1;
27,030✔
429
        continue;
27,030✔
430
      }
431
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
115,726,547✔
432
        tqDebug("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
97,589,895✔
433
        SSDataBlock* pRes = NULL;
97,589,326✔
434
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
97,589,595✔
435
        if (code == TSDB_CODE_SUCCESS) {
97,578,653✔
436
          return true;
97,578,653✔
437
        }
438
      } else {
439
        pReader->nextBlk += 1;
18,139,271✔
440
        tqDebug("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
18,139,271✔
441
      }
442
    }
443

444
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
134,314,127✔
445
    pReader->msg.msgStr = NULL;
134,312,069✔
446

447
    int64_t elapsed = taosGetTimestampMs() - st;
134,314,051✔
448
    if (elapsed > 1000 || elapsed < 0) {
134,314,051✔
449
      return false;
972✔
450
    }
451

452
    // try next message in wal file
453
    if (walNextValidMsg(pWalReader, false) < 0) {
134,313,079✔
454
      return false;
18,572,821✔
455
    }
456

457
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
115,730,856✔
458
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
115,732,394✔
459
    int64_t ver = pWalReader->pHead->head.version;
115,733,724✔
460
    SDecoder decoder = {0};
115,729,904✔
461
    if (tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL, &decoder) != 0) {
115,731,186✔
462
      tDecoderClear(&decoder);
×
463
      return false;
×
464
    }
465
    tDecoderClear(&decoder);
115,726,990✔
466
    pReader->nextBlk = 0;
115,735,435✔
467
  }
468
}
469

470
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList, SDecoder* decoder) {
158,734,725✔
471
  if (pReader == NULL) {
158,734,725✔
472
    return TSDB_CODE_INVALID_PARA;
×
473
  }
474
  pReader->msg.msgStr = msgStr;
158,734,725✔
475
  pReader->msg.msgLen = msgLen;
158,738,951✔
476
  pReader->msg.ver = ver;
158,739,756✔
477

478
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
158,739,339✔
479

480
  tDecoderInit(decoder, pReader->msg.msgStr, pReader->msg.msgLen);
158,739,339✔
481
  int32_t code = tDecodeSubmitReq(decoder, &pReader->submit, rawList);
158,730,950✔
482

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

487
  return code;
158,725,014✔
488
}
489

490
void tqReaderClearSubmitMsg(STqReader* pReader) {
85,946,327✔
491
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
85,946,327✔
492
  pReader->nextBlk = 0;
85,939,321✔
493
  pReader->msg.msgStr = NULL;
85,940,858✔
494
}
85,957,797✔
495

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

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

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

517
bool tqNextBlockImpl(STqReader* pReader, const char* idstr) {
22,416,390✔
518
  int32_t code = false;
22,416,390✔
519
  int32_t lino = 0;
22,416,390✔
520
  int64_t uid = 0;
22,416,390✔
521
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
22,416,390✔
522
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
22,416,390✔
523
  TSDB_CHECK_NULL(pReader->tbIdHash, code, lino, END, true);
22,418,030✔
524

525
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
22,421,930✔
526
  while (pReader->nextBlk < blockSz) {
23,553,560✔
527
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
11,778,128✔
528
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
11,779,422✔
529
    uid = pSubmitTbData->uid;
11,779,422✔
530
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
11,779,422✔
531
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
11,779,100✔
532

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

538
  tqReaderClearSubmitMsg(pReader);
11,773,148✔
539
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
11,774,132✔
540

541
END:
11,774,132✔
542
  tqTrace("%s:%d return:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
22,420,946✔
543
  return code;
22,418,067✔
544
}
545

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

551
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
62,450,804✔
552
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
62,450,804✔
553
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
62,463,112✔
554

555
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
62,463,112✔
556
  while (pReader->nextBlk < blockSz) {
62,469,115✔
557
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
31,245,227✔
558
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
31,247,530✔
559
    uid = pSubmitTbData->uid;
31,247,530✔
560
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
31,247,819✔
561
    TSDB_CHECK_NULL(ret, code, lino, END, true);
31,247,195✔
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);
31,227,170✔
566
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
31,213,028✔
567

568
END:
31,213,028✔
569
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
62,460,223✔
570
  return code;
62,414,348✔
571
}
572

573
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask,
41,646,315✔
574
                    SExtSchema* extSrc) {
575
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
41,646,315✔
576
    return TSDB_CODE_INVALID_PARA;
×
577
  }
578
  int32_t code = 0;
41,668,192✔
579

580
  int32_t cnt = 0;
41,668,192✔
581
  for (int32_t i = 0; i < pSrc->nCols; i++) {
234,452,618✔
582
    cnt += mask[i];
192,769,386✔
583
  }
584

585
  pDst->nCols = cnt;
41,671,651✔
586
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
41,691,401✔
587
  if (pDst->pSchema == NULL) {
41,654,549✔
588
    return TAOS_GET_TERRNO(terrno);
×
589
  }
590

591
  int32_t j = 0;
41,649,869✔
592
  for (int32_t i = 0; i < pSrc->nCols; i++) {
234,490,561✔
593
    if (mask[i]) {
192,768,689✔
594
      pDst->pSchema[j++] = pSrc->pSchema[i];
192,810,056✔
595
      SColumnInfoData colInfo =
192,812,902✔
596
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
192,827,909✔
597
      if (extSrc != NULL) {
192,814,485✔
598
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
55,590✔
599
      }
600
      code = blockDataAppendColInfo(pBlock, &colInfo);
192,814,485✔
601
      if (code != 0) {
192,828,094✔
602
        return code;
×
603
      }
604
    }
605
  }
606
  return 0;
41,695,402✔
607
}
608

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

620
  int32_t numOfCols = taosArrayGetSize(pColIdList);
135,956✔
621
  if (numOfCols == 0) {  // all columns are required
135,956✔
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;
135,956✔
637
    int32_t j = 0;
135,956✔
638
    while (i < pSchema->nCols && j < numOfCols) {
1,025,947✔
639
      SSchema* pColSchema = &pSchema->pSchema[i];
890,032✔
640
      col_id_t colIdSchema = pColSchema->colId;
890,032✔
641

642
      col_id_t* pColIdNeed = (col_id_t*)taosArrayGet(pColIdList, j);
890,032✔
643
      if (pColIdNeed == NULL) {
890,032✔
644
        break;
×
645
      }
646
      if (colIdSchema < *pColIdNeed) {
890,032✔
647
        i++;
41,838✔
648
      } else if (colIdSchema > *pColIdNeed) {
848,194✔
649
        j++;
×
650
      } else {
651
        SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
848,194✔
652
        if (IS_DECIMAL_TYPE(pColSchema->type) && pReader->extSchema != NULL) {
848,194✔
653
          decimalFromTypeMod(pReader->extSchema[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
31,698✔
654
        }
655
        int32_t code = blockDataAppendColInfo(pBlock, &colInfo);
848,194✔
656
        if (code != TSDB_CODE_SUCCESS) {
848,194✔
657
          blockDataFreeRes(pBlock);
41✔
658
          return code;
×
659
        }
660
        i++;
848,153✔
661
        j++;
848,153✔
662
      }
663
    }
664
  }
665

666
  return TSDB_CODE_SUCCESS;
135,956✔
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) {
97,586,960✔
733
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
97,586,960✔
734
  int32_t sversion = pSubmitTbData->sver;
97,588,897✔
735
  int64_t suid = pSubmitTbData->suid;
97,589,567✔
736
  int64_t uid = pSubmitTbData->uid;
97,589,567✔
737
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
97,589,902✔
738
      (pReader->cachedSchemaVer != sversion)) {
97,452,292✔
739
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
132,048✔
740
    taosMemoryFree(pReader->extSchema);
135,956✔
741
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
135,956✔
742
    if (pReader->pSchemaWrapper == NULL) {
135,579✔
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;
135,243✔
750
    pReader->cachedSchemaSuid = suid;
135,579✔
751
    pReader->cachedSchemaVer = sversion;
135,915✔
752

753
    return buildResSDataBlock(pReader, pReader->pSchemaWrapper, pReader->pColIdList);
135,579✔
754
  }
755
  return TSDB_CODE_SUCCESS;
97,452,606✔
756
}
757

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

770
  code = checkAndSetDataBlock(pReader, pSubmitTbData);
97,587,965✔
771
  TSDB_CHECK_CODE(code, line, END);
97,588,562✔
772

773
  int32_t numOfRows = 0;
97,588,562✔
774
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
97,588,562✔
775
    SColData* pCol = taosArrayGet(pSubmitTbData->aCol, 0);
1,224✔
776
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
1,224✔
777
    numOfRows = pCol->nVal;
1,224✔
778
  } else {
779
    numOfRows = taosArrayGetSize(pSubmitTbData->aRowP);
97,587,673✔
780
  }
781

782
  SSDataBlock* pBlock = pReader->pResBlock;
97,586,351✔
783
  *pRes = pBlock;
97,588,227✔
784
  blockDataCleanup(pBlock);
97,588,227✔
785
  pBlock->info.id.uid = pSubmitTbData->uid;
97,586,552✔
786
  pBlock->info.version = pReader->msg.ver;
97,588,223✔
787
  code = blockDataEnsureCapacity(pBlock, numOfRows);
97,587,892✔
788
  TSDB_CHECK_CODE(code, line, END);
97,586,217✔
789
  pBlock->info.rows = numOfRows;
97,586,217✔
790
  int32_t colActual = blockDataGetNumOfCols(pBlock);
97,585,882✔
791

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

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

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

822
          if (isBlob == 0) {
2,448✔
823
            code = doSetVal(pColData, i, &colVal);
2,448✔
824
          } else {
825
            code = doSetBlobVal(pColData, i, &colVal, pSubmitTbData->pBlobSet);
×
826
          }
827
          TSDB_CHECK_CODE(code, line, END);
2,448✔
828
        }
829
        sourceIdx++;
1,224✔
830
        targetIdx++;
1,224✔
831
      } else {
832
        colDataSetNNULL(pColData, 0, numOfRows);
612✔
833
        targetIdx++;
612✔
834
      }
835
    }
836
  } else {
837
    SArray*         pRows = pSubmitTbData->aRowP;
97,582,241✔
838
    SSchemaWrapper* pWrapper = pReader->pSchemaWrapper;
97,585,057✔
839
    pTSchema = tBuildTSchema(pWrapper->pSchema, pWrapper->nCols, pWrapper->version);
97,583,717✔
840
    TSDB_CHECK_NULL(pTSchema, code, line, END, terrno);
97,583,179✔
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) {
223,548,006✔
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++;
223,548,006✔
858
            continue;
223,548,006✔
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:
159,346,017✔
880
  if (code != 0) {
145,626,839✔
881
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
882
  }
883
  taosMemoryFreeClear(pTSchema);
97,581,167✔
884
  return code;
97,573,980✔
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,
41,639,694✔
916
                               char* assigned, int32_t numOfRows, int32_t curRow, int32_t* lastRow) {
917
  int32_t         code = 0;
41,639,694✔
918
  SSchemaWrapper* pSW = NULL;
41,639,694✔
919
  SSDataBlock*    block = NULL;
41,653,463✔
920
  if (taosArrayGetSize(blocks) > 0) {
41,653,463✔
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));
41,677,963✔
928
  TQ_NULL_GO_TO_END(block);
41,632,973✔
929

930
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
41,632,973✔
931
  TQ_NULL_GO_TO_END(pSW);
41,661,861✔
932

933
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pReader->pSchemaWrapper, assigned, pReader->extSchema));
41,661,861✔
934
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
41,694,168✔
935
          (int32_t)taosArrayGetSize(block->pDataBlock));
936

937
  block->info.id.uid = pSubmitTbData->uid;
41,694,168✔
938
  block->info.version = pReader->msg.ver;
41,687,374✔
939
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
41,688,134✔
940
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
41,682,968✔
941
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
41,682,039✔
942
  pSW = NULL;
41,682,039✔
943

944
  taosMemoryFreeClear(block);
41,682,039✔
945

946
END:
41,700,893✔
947
  if (code != 0) {
41,688,014✔
948
    tqError("processBuildNew failed, code:%d", code);
×
949
  }
950
  tDeleteSchemaWrapper(pSW);
41,688,014✔
951
  blockDataFreeRes(block);
41,655,510✔
952
  taosMemoryFree(block);
41,650,588✔
953
  return code;
41,679,837✔
954
}
955
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
72,405✔
956
  int32_t code = 0;
72,405✔
957
  int32_t curRow = 0;
72,405✔
958
  int32_t lastRow = 0;
72,405✔
959

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

964
  SArray*   pCols = pSubmitTbData->aCol;
72,405✔
965
  SColData* pCol = taosArrayGet(pCols, 0);
72,405✔
966
  TQ_NULL_GO_TO_END(pCol);
72,405✔
967
  int32_t numOfRows = pCol->nVal;
72,405✔
968
  int32_t numOfCols = taosArrayGetSize(pCols);
72,405✔
969
  tqTrace("vgId:%d, tqProcessColData start, col num: %d, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfCols,
72,405✔
970
          numOfRows);
971
  for (int32_t i = 0; i < numOfRows; i++) {
45,262,351✔
972
    bool buildNew = false;
45,130,850✔
973

974
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
180,076,543✔
975
      int32_t k = 0;
133,888,235✔
976
      for (; k < numOfCols; k++) {
267,775,184✔
977
        pCol = taosArrayGet(pCols, k);
262,088,042✔
978
        TQ_NULL_GO_TO_END(pCol);
262,479,964✔
979
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
262,479,964✔
980
          SColVal colVal = {0};
134,048,348✔
981
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
132,626,906✔
982
          PROCESS_VAL
135,592,773✔
983
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
135,663,884✔
984
          break;
135,434,520✔
985
        }
986
      }
987
      if (k >= numOfCols) {
134,945,693✔
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) {
44,576,286✔
995
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
72,150✔
996
    }
997

998
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
44,576,541✔
999
    TQ_NULL_GO_TO_END(pBlock);
45,234,404✔
1000

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

1004
    int32_t targetIdx = 0;
45,234,404✔
1005
    int32_t sourceIdx = 0;
45,234,404✔
1006
    int32_t colActual = blockDataGetNumOfCols(pBlock);
45,234,404✔
1007
    while (targetIdx < colActual && sourceIdx < numOfCols) {
179,118,134✔
1008
      pCol = taosArrayGet(pCols, sourceIdx);
133,928,188✔
1009
      TQ_NULL_GO_TO_END(pCol);
133,626,689✔
1010
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
133,626,689✔
1011
      TQ_NULL_GO_TO_END(pColData);
133,296,037✔
1012
      SColVal colVal = {0};
133,296,037✔
1013
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
133,368,186✔
1014
      SET_DATA
132,560,333✔
1015
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
135,449,779✔
1016
    }
1017

1018
    curRow++;
45,189,946✔
1019
  }
1020
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
131,501✔
1021
  pLastBlock->info.rows = curRow - lastRow;
72,405✔
1022
  tqTrace("vgId:%d, tqProcessColData end, col num: %d, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId,
72,405✔
1023
          numOfCols, numOfRows, (int)taosArrayGetSize(blocks));
1024
END:
7,548,335✔
1025
  if (code != TSDB_CODE_SUCCESS) {
72,405✔
1026
    tqError("vgId:%d, process col data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1027
  }
1028
  taosMemoryFree(assigned);
72,405✔
1029
  return code;
72,405✔
1030
}
1031

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

1036
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
41,548,998✔
1037
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
41,577,144✔
1038
  TQ_NULL_GO_TO_END(assigned);
41,579,305✔
1039

1040
  int32_t curRow = 0;
41,579,305✔
1041
  int32_t lastRow = 0;
41,579,305✔
1042
  SArray* pRows = pSubmitTbData->aRowP;
41,565,581✔
1043
  int32_t numOfRows = taosArrayGetSize(pRows);
41,596,439✔
1044
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
41,602,937✔
1045
  TQ_NULL_GO_TO_END(pTSchema);
41,605,629✔
1046
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
41,605,629✔
1047

1048
  for (int32_t i = 0; i < numOfRows; i++) {
1,174,782,762✔
1049
    bool  buildNew = false;
1,133,174,882✔
1050
    SRow* pRow = taosArrayGetP(pRows, i);
1,133,174,882✔
1051
    TQ_NULL_GO_TO_END(pRow);
1,132,427,371✔
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,123,670,611✔
1061
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
41,619,677✔
1062
    }
1063

1064
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
1,123,651,946✔
1065
    TQ_NULL_GO_TO_END(pBlock);
1,133,402,666✔
1066

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

1070
    int32_t targetIdx = 0;
1,133,402,666✔
1071
    int32_t sourceIdx = 0;
1,133,402,666✔
1072
    int32_t colActual = blockDataGetNumOfCols(pBlock);
1,133,402,666✔
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,133,230,067✔
1083
  }
1084
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
41,607,880✔
1085
  if (pLastBlock != NULL) {
41,619,038✔
1086
    pLastBlock->info.rows = curRow - lastRow;
41,619,038✔
1087
  }
1088

1089
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
41,618,632✔
1090
          (int)taosArrayGetSize(blocks));
1091
END:
48,665,200✔
1092
  if (code != TSDB_CODE_SUCCESS) {
41,611,267✔
1093
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1094
  }
1095
  taosMemoryFreeClear(pTSchema);
41,584,720✔
1096
  taosMemoryFree(assigned);
41,593,060✔
1097
  return code;
41,592,716✔
1098
}
1099

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

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

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

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

1130
END:
8,095✔
1131
  if (code != 0) {
8,095✔
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,095✔
1136
}
1137

1138
int32_t tqRetrieveTaosxBlock(STqReader* pReader, SMqDataRsp* pRsp, SArray* blocks, SArray* schemas,
41,888,628✔
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);
41,888,628✔
1141
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
41,888,628✔
1142
  if (pSubmitTbData == NULL) {
41,890,901✔
1143
    return terrno;
×
1144
  }
1145
  pReader->nextBlk++;
41,890,901✔
1146

1147
  if (pSubmitTbDataRet) {
41,887,316✔
1148
    *pSubmitTbDataRet = pSubmitTbData;
41,891,963✔
1149
  }
1150

1151
  if (fetchMeta == ONLY_META) {
41,888,222✔
1152
    if (pSubmitTbData->pCreateTbReq != NULL) {
6,504✔
1153
      if (pRsp->createTableReq == NULL) {
1,914✔
1154
        pRsp->createTableReq = taosArrayInit(0, POINTER_BYTES);
1,149✔
1155
        if (pRsp->createTableReq == NULL) {
1,149✔
1156
          return terrno;
×
1157
        }
1158
      }
1159
      if (taosArrayPush(pRsp->createTableReq, &pSubmitTbData->pCreateTbReq) == NULL) {
3,828✔
1160
        return terrno;
×
1161
      }
1162
      pSubmitTbData->pCreateTbReq = NULL;
1,914✔
1163
    }
1164
    return 0;
6,504✔
1165
  }
1166

1167
  int32_t sversion = pSubmitTbData->sver;
41,881,718✔
1168
  int64_t uid = pSubmitTbData->uid;
41,882,046✔
1169
  pReader->lastBlkUid = uid;
41,883,897✔
1170

1171
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
41,881,546✔
1172
  taosMemoryFreeClear(pReader->extSchema);
41,880,351✔
1173
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
41,885,209✔
1174
  if (pReader->pSchemaWrapper == NULL) {
41,864,733✔
1175
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
192,587✔
1176
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1177
    pReader->cachedSchemaSuid = 0;
192,587✔
1178
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
192,587✔
1179
  }
1180

1181
  if (pSubmitTbData->pCreateTbReq != NULL) {
41,648,546✔
1182
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
8,095✔
1183
    if (code != 0) {
8,095✔
1184
      return code;
×
1185
    }
1186
  } else if (rawList != NULL) {
41,668,233✔
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) {
41,676,328✔
1195
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
72,405✔
1196
  } else {
1197
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
41,562,961✔
1198
  }
1199
}
1200

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

1208
int32_t tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
331,718✔
1209
  if (pReader == NULL || tbUidList == NULL) {
331,718✔
1210
    return TSDB_CODE_SUCCESS;
×
1211
  }
1212
  if (pReader->tbIdHash) {
331,718✔
1213
    taosHashClear(pReader->tbIdHash);
×
1214
  } else {
1215
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
331,718✔
1216
    if (pReader->tbIdHash == NULL) {
331,718✔
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,573,441✔
1223
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
9,237,915✔
1224
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
9,234,691✔
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,590✔
1231
  return TSDB_CODE_SUCCESS;
331,718✔
1232
}
1233

1234
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
351,897✔
1235
  if (pReader == NULL || pTableUidList == NULL) {
351,897✔
1236
    return;
×
1237
  }
1238
  if (pReader->tbIdHash == NULL) {
351,897✔
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);
351,897✔
1247
  for (int i = 0; i < numOfTables; i++) {
551,659✔
1248
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
199,762✔
1249
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
199,762✔
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);
199,762✔
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) {
1,957✔
1272
  if (pReader == NULL || tbUidList == NULL) {
1,957✔
1273
    return;
×
1274
  }
1275
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
2,570✔
1276
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
613✔
1277
    int32_t code = taosHashRemove(pReader->tbIdHash, pKey, sizeof(int64_t));
613✔
1278
    if (code != 0) {
613✔
1279
      tqWarn("%s failed to remove table uid:%" PRId64 " from hash, msg:%s", __func__, pKey != NULL ? *pKey : 0, tstrerror(code));
×
1280
    }
1281
  }
1282
}
1283

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

1294
  // update the table list for each consumer handle
1295
  taosWLockLatch(&pTq->lock);
77,158,120✔
1296
  while (1) {
1,267,649✔
1297
    pIter = taosHashIterate(pTq->pHandle, pIter);
78,425,767✔
1298
    if (pIter == NULL) {
78,424,848✔
1299
      break;
77,157,199✔
1300
    }
1301

1302
    STqHandle* pTqHandle = (STqHandle*)pIter;
1,267,649✔
1303
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
1,267,649✔
1304
      int32_t code = qUpdateTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList, isAdd);
349,516✔
1305
      if (code != 0) {
349,516✔
1306
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1307
        continue;
×
1308
      }
1309
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
918,133✔
1310
      if (!isAdd) {
913,795✔
1311
        int32_t sz = taosArrayGetSize(tbUidList);
307,544✔
1312
        for (int32_t i = 0; i < sz; i++) {
307,544✔
1313
          int64_t* tbUid = (int64_t*)taosArrayGet(tbUidList, i);
×
1314
          if (tbUid &&
×
1315
              taosHashPut(pTqHandle->execHandle.execDb.pFilterOutTbUid, tbUid, sizeof(int64_t), NULL, 0) != 0) {
×
1316
            tqError("failed to add table uid:%" PRId64 " to hash", *tbUid);
×
1317
            continue;
×
1318
          }
1319
        }
1320
      }
1321
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
4,338✔
1322
      if (isAdd) {
4,338✔
1323
        SArray* list = taosArrayDup(tbUidList, NULL);
4,338✔
1324
        if (list == NULL) {
4,338✔
1325
          tqError("taosArrayDup failed in tqUpdateTbUidList");
×
1326
          taosHashCancelIterate(pTq->pHandle, pIter);
×
1327
          taosWUnLockLatch(&pTq->lock);
×
1328
          return terrno;
×
1329
        }
1330
        int     ret = qSubFilterTableList(pTq->pVnode, list, pTqHandle->execHandle.execTb.node,
4,338✔
1331
                            pTqHandle->execHandle.task, pTqHandle->execHandle.execTb.suid);
4,338✔
1332
        if (ret != TDB_CODE_SUCCESS) {
4,338✔
1333
          tqError("qGetTableList in tqUpdateTbUidList error:%d handle %s consumer:0x%" PRIx64, ret, pTqHandle->subKey,
×
1334
                  pTqHandle->consumerId);
1335
          taosArrayDestroy(list);
×
1336
          taosHashCancelIterate(pTq->pHandle, pIter);
×
1337
          taosWUnLockLatch(&pTq->lock);
×
1338

1339
          return ret;
×
1340
        }
1341
        tqDebug("%s handle %s consumer:0x%" PRIx64 " add %d tables to tqReader", __func__, pTqHandle->subKey,
4,338✔
1342
                pTqHandle->consumerId, (int32_t)taosArrayGetSize(list));
1343
        tqReaderAddTbUidList(pTqHandle->execHandle.pTqReader, list);
4,338✔
1344
        taosArrayDestroy(list);
4,338✔
1345
      } else {
1346
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
×
1347
      }
1348
    }
1349
  }
1350
  taosWUnLockLatch(&pTq->lock);
77,157,199✔
1351
  return 0;
77,157,200✔
1352
}
1353

1354
static void destroySourceScanTables(void* ptr) {
×
1355
  SArray** pTables = ptr;
×
1356
  if (pTables && *pTables) {
×
1357
    taosArrayDestroy(*pTables);
×
1358
    *pTables = NULL;
×
1359
  }
1360
}
×
1361

1362
static int32_t compareSVTColInfo(const void* p1, const void* p2) {
×
1363
  SVTColInfo* pCol1 = (SVTColInfo*)p1;
×
1364
  SVTColInfo* pCol2 = (SVTColInfo*)p2;
×
1365
  if (pCol1->vColId == pCol2->vColId) {
×
1366
    return 0;
×
1367
  } else if (pCol1->vColId < pCol2->vColId) {
×
1368
    return -1;
×
1369
  } else {
1370
    return 1;
×
1371
  }
1372
}
1373

1374
static void freeTableSchemaCache(const void* key, size_t keyLen, void* value, void* ud) {
×
1375
  if (value) {
×
1376
    SSchemaWrapper* pSchemaWrapper = value;
×
1377
    tDeleteSchemaWrapper(pSchemaWrapper);
1378
  }
1379
}
×
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