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

taosdata / TDengine / #4878

11 Dec 2025 02:43AM UTC coverage: 64.569% (-0.02%) from 64.586%
#4878

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

307 of 617 new or added lines in 24 files covered. (49.76%)

3821 existing lines in 123 files now uncovered.

163630 of 253417 relevant lines covered (64.57%)

107598827.89 hits per line

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

69.11
/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 "tmsg.h"
17
#include "tq.h"
18

19
static int32_t tqCollectPhysicalTables(STqReader* pReader, const char* idstr);
20

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

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

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

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

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

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

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

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

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

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

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

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

254
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
15,403,521✔
255
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
15,403,521✔
256
    return -1;
×
257
  }
258
  int32_t code = -1;
15,416,666✔
259
  int32_t vgId = TD_VID(pTq->pVnode);
15,416,666✔
260
  int64_t id = pHandle->pWalReader->readerId;
15,418,216✔
261

262
  int64_t offset = *fetchOffset;
15,418,344✔
263
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
15,419,440✔
264
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
15,418,477✔
265
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
15,417,814✔
266

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

271
  while (offset <= appliedVer) {
15,857,473✔
272
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
14,873,422✔
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,
14,872,851✔
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) {
14,873,031✔
283
      code = walFetchBody(pHandle->pWalReader);
14,422,598✔
284
      goto END;
14,422,508✔
285
    } else {
286
      if (pHandle->fetchMeta != WITH_DATA) {
450,824✔
287
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
18,162✔
288
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
18,162✔
289
          code = walFetchBody(pHandle->pWalReader);
13,454✔
290
          if (code < 0) {
13,454✔
291
            goto END;
×
292
          }
293

294
          pHead = &(pHandle->pWalReader->pHead->head);
13,454✔
295
          if (isValValidForTable(pHandle, pHead)) {
13,454✔
296
            code = 0;
13,316✔
297
            goto END;
13,316✔
298
          } else {
299
            offset++;
138✔
300
            code = -1;
138✔
301
            continue;
138✔
302
          }
303
        }
304
      }
305
      code = walSkipFetchBody(pHandle->pWalReader);
437,370✔
306
      if (code < 0) {
437,370✔
307
        goto END;
×
308
      }
309
      offset++;
437,370✔
310
    }
311
    code = -1;
437,370✔
312
  }
313

314
END:
984,051✔
315
  *fetchOffset = offset;
15,419,875✔
316
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
15,419,961✔
317
          ", applied:%" PRId64 ", 0x%" PRIx64,
318
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
319
  return code;
15,419,965✔
320
}
321

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

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

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

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

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

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

368
  int32_t code = createDataBlock(&pReader->pResBlock);
129,661✔
369
  if (code) {
129,661✔
370
    terrno = code;
×
371
  }
372

373
  return pReader;
129,661✔
374
}
375

376
void tqReaderClose(STqReader* pReader) {
129,771✔
377
  tqDebug("%s:%p", __FUNCTION__, pReader);
129,771✔
378
  if (pReader == NULL) return;
129,841✔
379

380
  // close wal reader
381
  if (pReader->pWalReader) {
129,661✔
382
    walCloseReader(pReader->pWalReader);
129,661✔
383
  }
384

385
  if (pReader->pSchemaWrapper) {
129,661✔
386
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
77,509✔
387
  }
388

389
  taosMemoryFree(pReader->extSchema);
129,661✔
390
  if (pReader->pColIdList) {
129,661✔
391
    taosArrayDestroy(pReader->pColIdList);
109,047✔
392
  }
393

394
  // free hash
395
  blockDataDestroy(pReader->pResBlock);
129,661✔
396
  taosHashCleanup(pReader->tbIdHash);
129,661✔
397
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
129,661✔
398

399
  taosHashCleanup(pReader->vtSourceScanInfo.pVirtualTables);
129,661✔
400
  taosHashCleanup(pReader->vtSourceScanInfo.pPhysicalTables);
129,661✔
401
  taosLRUCacheCleanup(pReader->vtSourceScanInfo.pPhyTblSchemaCache);
129,604✔
402
  taosMemoryFree(pReader);
129,604✔
403
}
404

405
int32_t tqReaderSeek(STqReader* pReader, int64_t ver, const char* id) {
231,920✔
406
  if (pReader == NULL) {
231,920✔
407
    return TSDB_CODE_INVALID_PARA;
×
408
  }
409
  if (walReaderSeekVer(pReader->pWalReader, ver) < 0) {
231,920✔
410
    return terrno;
9,253✔
411
  }
412
  tqDebug("wal reader seek to ver:%" PRId64 " %s", ver, id);
222,691✔
413
  return 0;
222,691✔
414
}
415

416
bool tqNextBlockInWal(STqReader* pReader, const char* id, int sourceExcluded) {
35,414,852✔
417
  if (pReader == NULL) {
35,414,852✔
418
    return false;
×
419
  }
420
  SWalReader* pWalReader = pReader->pWalReader;
35,414,852✔
421

422
  int64_t st = taosGetTimestampMs();
35,414,958✔
423
  while (1) {
36,993,436✔
424
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
72,408,394✔
425
    while (pReader->nextBlk < numOfBlocks) {
77,213,081✔
426
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
36,992,311✔
427
              pReader->msg.ver);
428

429
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
36,992,311✔
430
      if (pSubmitTbData == NULL) {
36,991,891✔
431
        tqError("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
×
432
                pReader->msg.ver);
433
        return false;
558✔
434
      }
435
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
36,991,891✔
436
        pReader->nextBlk += 1;
2,438✔
437
        continue;
2,438✔
438
      }
439
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
36,988,149✔
440
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
32,182,970✔
441
        SSDataBlock* pRes = NULL;
32,182,970✔
442
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
32,183,015✔
443
        if (code == TSDB_CODE_SUCCESS) {
32,185,641✔
444
          return true;
32,185,641✔
445
        }
446
      } else {
447
        pReader->nextBlk += 1;
4,805,792✔
448
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
4,805,792✔
449
      }
450
    }
451

452
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
40,221,744✔
453
    pReader->msg.msgStr = NULL;
40,221,003✔
454

455
    int64_t elapsed = taosGetTimestampMs() - st;
40,221,835✔
456
    if (elapsed > 1000 || elapsed < 0) {
40,221,835✔
457
      return false;
1,484✔
458
    }
459

460
    // try next message in wal file
461
    if (walNextValidMsg(pWalReader, false) < 0) {
40,220,351✔
462
      return false;
3,226,692✔
463
    }
464

465
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
36,993,984✔
466
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
36,994,524✔
467
    int64_t ver = pWalReader->pHead->head.version;
36,994,374✔
468
    SDecoder decoder = {0};
36,994,539✔
469
    if (tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL, &decoder) != 0) {
36,992,471✔
470
      tDecoderClear(&decoder);
×
471
      return false;
×
472
    }
473
    tDecoderClear(&decoder);
36,989,893✔
474
    pReader->nextBlk = 0;
36,993,362✔
475
  }
476
}
477

478
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList, SDecoder* decoder) {
51,414,911✔
479
  if (pReader == NULL) {
51,414,911✔
480
    return TSDB_CODE_INVALID_PARA;
×
481
  }
482
  pReader->msg.msgStr = msgStr;
51,414,911✔
483
  pReader->msg.msgLen = msgLen;
51,416,683✔
484
  pReader->msg.ver = ver;
51,415,893✔
485

486
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
51,416,489✔
487

488
  tDecoderInit(decoder, pReader->msg.msgStr, pReader->msg.msgLen);
51,416,489✔
489
  int32_t code = tDecodeSubmitReq(decoder, &pReader->submit, rawList);
51,416,069✔
490

491
  if (code != 0) {
51,414,451✔
492
    tqError("DecodeSSubmitReq2 error, msgLen:%d, ver:%" PRId64, msgLen, ver);
×
493
  }
494

495
  return code;
51,414,035✔
496
}
497

498
void tqReaderClearSubmitMsg(STqReader* pReader) {
28,831,719✔
499
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
28,831,719✔
500
  pReader->nextBlk = 0;
28,831,210✔
501
  pReader->msg.msgStr = NULL;
28,831,026✔
502
}
28,836,393✔
503

504
SWalReader* tqGetWalReader(STqReader* pReader) {
39,207,274✔
505
  if (pReader == NULL) {
39,207,274✔
506
    return NULL;
×
507
  }
508
  return pReader->pWalReader;
39,207,274✔
509
}
510

511
SSDataBlock* tqGetResultBlock(STqReader* pReader) {
35,413,728✔
512
  if (pReader == NULL) {
35,413,728✔
513
    return NULL;
×
514
  }
515
  return pReader->pResBlock;
35,413,728✔
516
}
517

518
int64_t tqGetResultBlockTime(STqReader* pReader) {
35,410,802✔
519
  if (pReader == NULL) {
35,410,802✔
520
    return 0;
×
521
  }
522
  return pReader->lastTs;
35,410,802✔
523
}
524

525
bool tqNextBlockImpl(STqReader* pReader, const char* idstr) {
5,637,632✔
526
  int32_t code = false;
5,637,632✔
527
  int32_t lino = 0;
5,637,632✔
528
  int64_t uid = 0;
5,637,632✔
529
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
5,637,632✔
530
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
5,637,632✔
531
  TSDB_CHECK_NULL(pReader->tbIdHash, code, lino, END, true);
5,637,812✔
532

533
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
5,637,981✔
534
  while (pReader->nextBlk < blockSz) {
5,953,843✔
535
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
2,977,435✔
536
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
2,977,165✔
537
    uid = pSubmitTbData->uid;
2,977,165✔
538
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
2,977,075✔
539
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
2,977,626✔
540

541
    tqTrace("iterator data block in hash continue, progress:%d/%d, total queried tables:%d, uid:%" PRId64,
316,042✔
542
            pReader->nextBlk, blockSz, taosHashGetSize(pReader->tbIdHash), uid);
543
    pReader->nextBlk++;
316,042✔
544
  }
545

546
  tqReaderClearSubmitMsg(pReader);
2,976,408✔
547
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
2,976,678✔
548

549
END:
2,976,678✔
550
  tqTrace("%s:%d return:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
5,638,262✔
551
  return code;
5,638,082✔
552
}
553

554
bool tqNextDataBlockFilterOut(STqReader* pReader, SHashObj* filterOutUids) {
22,880,087✔
555
  int32_t code = false;
22,880,087✔
556
  int32_t lino = 0;
22,880,087✔
557
  int64_t uid = 0;
22,880,087✔
558

559
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
22,880,087✔
560
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
22,880,087✔
561
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
22,887,163✔
562

563
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
22,887,163✔
564
  while (pReader->nextBlk < blockSz) {
22,889,780✔
565
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
11,446,556✔
566
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
11,446,443✔
567
    uid = pSubmitTbData->uid;
11,446,443✔
568
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
11,447,005✔
569
    TSDB_CHECK_NULL(ret, code, lino, END, true);
11,447,238✔
570
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64, pReader->nextBlk, blockSz, uid);
×
571
    pReader->nextBlk++;
×
572
  }
573
  tqReaderClearSubmitMsg(pReader);
11,443,504✔
574
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
11,442,099✔
575

576
END:
11,442,099✔
577
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
22,889,337✔
578
  return code;
22,875,880✔
579
}
580

581
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask,
14,059,504✔
582
                    SExtSchema* extSrc) {
583
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
14,059,504✔
584
    return TSDB_CODE_INVALID_PARA;
×
585
  }
586
  int32_t code = 0;
14,065,388✔
587

588
  int32_t cnt = 0;
14,065,388✔
589
  for (int32_t i = 0; i < pSrc->nCols; i++) {
64,625,392✔
590
    cnt += mask[i];
50,557,979✔
591
  }
592

593
  pDst->nCols = cnt;
14,068,177✔
594
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
14,067,945✔
595
  if (pDst->pSchema == NULL) {
14,063,013✔
596
    return TAOS_GET_TERRNO(terrno);
×
597
  }
598

599
  int32_t j = 0;
14,060,800✔
600
  for (int32_t i = 0; i < pSrc->nCols; i++) {
64,637,730✔
601
    if (mask[i]) {
50,562,237✔
602
      pDst->pSchema[j++] = pSrc->pSchema[i];
50,568,561✔
603
      SColumnInfoData colInfo =
50,576,721✔
604
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
50,574,471✔
605
      if (extSrc != NULL) {
50,567,929✔
606
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
5,014✔
607
      }
608
      code = blockDataAppendColInfo(pBlock, &colInfo);
50,567,929✔
609
      if (code != 0) {
50,575,301✔
610
        return code;
×
611
      }
612
    }
613
  }
614
  return 0;
14,069,576✔
615
}
616

617
static int32_t buildResSDataBlock(STqReader* pReader, SSchemaWrapper* pSchema, const SArray* pColIdList) {
66,014✔
618
  if (pReader == NULL || pSchema == NULL || pColIdList == NULL) {
66,014✔
619
    return TSDB_CODE_INVALID_PARA;
95✔
620
  }
621
  SSDataBlock* pBlock = pReader->pResBlock;
65,919✔
622
  if (blockDataGetNumOfCols(pBlock) > 0) {
66,014✔
623
    blockDataDestroy(pBlock);
79✔
624
    int32_t code = createDataBlock(&pReader->pResBlock);
79✔
625
    if (code) {
79✔
626
      return code;
×
627
    }
628
    pBlock = pReader->pResBlock;
79✔
629

630
    pBlock->info.id.uid = pReader->cachedSchemaUid;
79✔
631
    pBlock->info.version = pReader->msg.ver;
79✔
632
  }
633

634
  int32_t numOfCols = taosArrayGetSize(pColIdList);
66,014✔
635

636
  if (numOfCols == 0) {  // all columns are required
66,014✔
637
    for (int32_t i = 0; i < pSchema->nCols; ++i) {
×
638
      SSchema*        pColSchema = &pSchema->pSchema[i];
95✔
639
      SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
×
640

641
      if (IS_DECIMAL_TYPE(pColSchema->type) && pReader->extSchema != NULL) {
×
642
        decimalFromTypeMod(pReader->extSchema[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
×
643
      }
644
      int32_t code = blockDataAppendColInfo(pBlock, &colInfo);
×
645
      if (code != TSDB_CODE_SUCCESS) {
×
646
        blockDataFreeRes(pBlock);
×
647
        return terrno;
×
648
      }
649
    }
650
  } else {
651
    if (numOfCols > pSchema->nCols) {
66,014✔
652
      numOfCols = pSchema->nCols;
174✔
653
    }
654

655
    int32_t i = 0;
65,919✔
656
    int32_t j = 0;
65,919✔
657
    while (i < pSchema->nCols && j < numOfCols) {
838,748✔
658
      SSchema* pColSchema = &pSchema->pSchema[i];
772,734✔
659
      col_id_t colIdSchema = pColSchema->colId;
772,829✔
660

661
      col_id_t* pColIdNeed = (col_id_t*)taosArrayGet(pColIdList, j);
772,285✔
662
      if (pColIdNeed == NULL) {
772,188✔
663
        break;
×
664
      }
665
      if (colIdSchema < *pColIdNeed) {
772,188✔
666
        i++;
44,583✔
667
      } else if (colIdSchema > *pColIdNeed) {
727,605✔
668
        j++;
×
669
      } else {
670
        SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
728,054✔
671
        if (IS_DECIMAL_TYPE(pColSchema->type) && pReader->extSchema != NULL) {
728,305✔
672
          decimalFromTypeMod(pReader->extSchema[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
56,354✔
673
        }
674
        int32_t code = blockDataAppendColInfo(pBlock, &colInfo);
728,408✔
675
        if (code != TSDB_CODE_SUCCESS) {
728,246✔
676
          return -1;
×
677
        }
678
        i++;
728,246✔
679
        j++;
728,246✔
680
      }
681
    }
682
  }
683

684
  return TSDB_CODE_SUCCESS;
66,014✔
685
}
686

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

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

714
      val = taosMemRealloc(val, item.len + sizeof(BlobDataLenT));
×
715
      (void)memcpy(blobDataVal(val), item.data, item.len);
×
716
      len = item.len;
×
717
    }
718

719
    blobDataSetLen(val, len);
×
720
    code = colDataSetVal(pColumnInfoData, idx, val, false);
×
721

722
    taosMemoryFree(val);
×
723
  } else {
724
    colDataSetNULL(pColumnInfoData, idx);
×
725
  }
726
  return code;
×
727
}
728
static int32_t doSetVal(SColumnInfoData* pColumnInfoData, int32_t rowIndex, SColVal* pColVal) {
2,147,483,647✔
729
  int32_t code = TSDB_CODE_SUCCESS;
2,147,483,647✔
730

731
  if (IS_VAR_DATA_TYPE(pColVal->value.type)) {
2,147,483,647✔
732
    if (COL_VAL_IS_VALUE(pColVal)) {
709,276,815✔
733
      char val[65535 + 2] = {0};
737,352,966✔
734
      if (pColVal->value.pData != NULL) {
737,357,558✔
735
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
736,301,088✔
736
      }
737
      varDataSetLen(val, pColVal->value.nData);
737,635,426✔
738
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
737,310,202✔
739
    } else {
740
      colDataSetNULL(pColumnInfoData, rowIndex);
×
741
    }
742
  } else {
743
    code = colDataSetVal(pColumnInfoData, rowIndex, VALUE_GET_DATUM(&pColVal->value, pColVal->value.type),
2,147,483,647✔
744
                         !COL_VAL_IS_VALUE(pColVal));
2,147,483,647✔
745
  }
746

747
  return code;
2,147,483,647✔
748
}
749

750
int32_t tqRetrieveDataBlock(STqReader* pReader, SSDataBlock** pRes, const char* id) {
32,180,266✔
751
  if (pReader == NULL || pRes == NULL) {
32,180,266✔
752
    return TSDB_CODE_INVALID_PARA;
×
753
  }
754
  tqDebug("tq reader retrieve data block %p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
32,185,298✔
755
  int32_t        code = 0;
32,189,277✔
756
  int32_t        line = 0;
32,189,277✔
757
  STSchema*      pTSchema = NULL;
32,189,277✔
758
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk++);
32,189,277✔
759
  TSDB_CHECK_NULL(pSubmitTbData, code, line, END, terrno);
32,188,266✔
760
  SSDataBlock* pBlock = pReader->pResBlock;
32,188,266✔
761
  *pRes = pBlock;
32,188,160✔
762

763
  blockDataCleanup(pBlock);
32,188,266✔
764

765
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
32,187,842✔
766
  int32_t sversion = pSubmitTbData->sver;
32,187,630✔
767
  int64_t suid = pSubmitTbData->suid;
32,187,842✔
768
  int64_t uid = pSubmitTbData->uid;
32,188,372✔
769
  pReader->lastTs = pSubmitTbData->ctimeMs;
32,188,372✔
770

771
  pBlock->info.id.uid = uid;
32,188,266✔
772
  pBlock->info.version = pReader->msg.ver;
32,187,736✔
773

774
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
32,187,736✔
775
      (pReader->cachedSchemaVer != sversion)) {
32,121,499✔
776
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
66,422✔
777
    taosMemoryFree(pReader->extSchema);
66,014✔
778
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
66,014✔
779
    if (pReader->pSchemaWrapper == NULL) {
66,014✔
780
      tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", uid:%" PRId64
×
781
             "version %d, possibly dropped table",
782
             vgId, suid, uid, pReader->cachedSchemaVer);
783
      pReader->cachedSchemaSuid = 0;
×
784
      return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
×
785
    }
786

787
    pReader->cachedSchemaUid = uid;
65,219✔
788
    pReader->cachedSchemaSuid = suid;
66,014✔
789
    pReader->cachedSchemaVer = sversion;
65,565✔
790

791
    if (pReader->cachedSchemaVer != pReader->pSchemaWrapper->version) {
65,565✔
792
      tqError("vgId:%d, schema version mismatch, suid:%" PRId64 ", uid:%" PRId64 ", version:%d, cached version:%d",
×
793
              vgId, suid, uid, sversion, pReader->pSchemaWrapper->version);
794
      return TSDB_CODE_TQ_INTERNAL_ERROR;
×
795
    }
796
    code = buildResSDataBlock(pReader, pReader->pSchemaWrapper, pReader->pColIdList);
65,565✔
797
    TSDB_CHECK_CODE(code, line, END);
66,014✔
798
    pBlock = pReader->pResBlock;
66,014✔
799
    *pRes = pBlock;
66,014✔
800
  }
801

802
  int32_t numOfRows = 0;
32,187,630✔
803
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
32,187,630✔
804
    SColData* pCol = taosArrayGet(pSubmitTbData->aCol, 0);
164✔
805
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
164✔
806
    numOfRows = pCol->nVal;
164✔
807
  } else {
808
    numOfRows = taosArrayGetSize(pSubmitTbData->aRowP);
32,187,572✔
809
  }
810

811
  code = blockDataEnsureCapacity(pBlock, numOfRows);
32,187,488✔
812
  TSDB_CHECK_CODE(code, line, END);
32,187,736✔
813
  pBlock->info.rows = numOfRows;
32,187,736✔
814
  int32_t colActual = blockDataGetNumOfCols(pBlock);
32,186,995✔
815

816
  // convert and scan one block
817
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
32,187,038✔
818
    SArray* pCols = pSubmitTbData->aCol;
164✔
819
    int32_t numOfCols = taosArrayGetSize(pCols);
164✔
820
    int32_t targetIdx = 0;
164✔
821
    int32_t sourceIdx = 0;
164✔
822
    while (targetIdx < colActual) {
738✔
823
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
574✔
824
      TSDB_CHECK_NULL(pColData, code, line, END, terrno);
574✔
825
      if (sourceIdx >= numOfCols) {
574✔
826
        tqError("lostdata tqRetrieveDataBlock sourceIdx:%d >= numOfCols:%d", sourceIdx, numOfCols);
164✔
827
        colDataSetNNULL(pColData, 0, numOfRows);
164✔
828
        targetIdx++;
164✔
829
        continue;
164✔
830
      }
831

832
      uint8_t isBlob = IS_STR_DATA_BLOB(pColData->info.type) ? 1 : 0;
410✔
833

834
      SColData* pCol = taosArrayGet(pCols, sourceIdx);
410✔
835
      TSDB_CHECK_NULL(pCol, code, line, END, terrno);
410✔
836
      SColVal colVal = {0};
410✔
837
      tqTrace("lostdata colActual:%d, sourceIdx:%d, targetIdx:%d, numOfCols:%d, source cid:%d, dst cid:%d", colActual,
410✔
838
              sourceIdx, targetIdx, numOfCols, pCol->cid, pColData->info.colId);
839
      if (pCol->cid < pColData->info.colId) {
410✔
840
        sourceIdx++;
164✔
841
      } else if (pCol->cid == pColData->info.colId) {
246✔
842
        for (int32_t i = 0; i < pCol->nVal; i++) {
492✔
843
          code = tColDataGetValue(pCol, i, &colVal);
328✔
844
          TSDB_CHECK_CODE(code, line, END);
328✔
845

846
          if (isBlob == 0) {
328✔
847
            code = doSetVal(pColData, i, &colVal);
328✔
848
          } else {
849
            code = doSetBlobVal(pColData, i, &colVal, pSubmitTbData->pBlobSet);
×
850
          }
851
          TSDB_CHECK_CODE(code, line, END);
328✔
852
        }
853
        sourceIdx++;
164✔
854
        targetIdx++;
164✔
855
      } else {
856
        colDataSetNNULL(pColData, 0, numOfRows);
82✔
857
        targetIdx++;
82✔
858
      }
859
    }
860
  } else {
861
    SArray*         pRows = pSubmitTbData->aRowP;
32,186,196✔
862
    SSchemaWrapper* pWrapper = pReader->pSchemaWrapper;
32,186,937✔
863
    pTSchema = tBuildTSchema(pWrapper->pSchema, pWrapper->nCols, pWrapper->version);
32,186,937✔
864
    TSDB_CHECK_NULL(pTSchema, code, line, END, terrno);
32,186,851✔
865

866
    for (int32_t i = 0; i < numOfRows; i++) {
1,659,523,974✔
867
      SRow* pRow = taosArrayGetP(pRows, i);
1,562,517,908✔
868
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
1,563,850,292✔
869
      int32_t sourceIdx = 0;
1,561,328,757✔
870
      for (int32_t j = 0; j < colActual; j++) {
2,147,483,647✔
871
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, j);
2,147,483,647✔
872
        TSDB_CHECK_NULL(pColData, code, line, END, terrno);
2,147,483,647✔
873

874
        uint8_t isBlob = IS_STR_DATA_BLOB(pColData->info.type) ? 1 : 0;
2,147,483,647✔
875
        while (1) {
137,818,590✔
876
          SColVal colVal = {0};
2,147,483,647✔
877
          code = tRowGet(pRow, pTSchema, sourceIdx, &colVal);
2,147,483,647✔
878
          TSDB_CHECK_CODE(code, line, END);
2,147,483,647✔
879

880
          if (colVal.cid < pColData->info.colId) {
2,147,483,647✔
881
            sourceIdx++;
137,818,590✔
882
            continue;
137,818,590✔
883
          } else if (colVal.cid == pColData->info.colId) {
2,147,483,647✔
884
            if (isBlob == 0) {
2,147,483,647✔
885
              code = doSetVal(pColData, i, &colVal);
2,147,483,647✔
886
            } else {
887
              code = doSetBlobVal(pColData, i, &colVal, pSubmitTbData->pBlobSet);
×
888
            }
889

890
            TSDB_CHECK_CODE(code, line, END);
2,147,483,647✔
891

892
            sourceIdx++;
2,147,483,647✔
893
            break;
2,147,483,647✔
894
          } else {
895
            colDataSetNULL(pColData, i);
×
896
            break;
×
897
          }
898
        }
899
      }
900
    }
901
  }
902

903
END:
97,006,066✔
904
  if (code != 0) {
59,251,614✔
905
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
906
  }
907
  taosMemoryFreeClear(pTSchema);
32,184,250✔
908
  return code;
32,186,516✔
909
}
910

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

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

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

951
  block = taosMemoryCalloc(1, sizeof(SSDataBlock));
14,063,592✔
952
  TQ_NULL_GO_TO_END(block);
14,062,451✔
953

954
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
14,062,451✔
955
  TQ_NULL_GO_TO_END(pSW);
14,063,988✔
956

957
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pReader->pSchemaWrapper, assigned, pReader->extSchema));
14,063,988✔
958
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
14,069,056✔
959
          (int32_t)taosArrayGetSize(block->pDataBlock));
960

961
  block->info.id.uid = pSubmitTbData->uid;
14,069,056✔
962
  block->info.version = pReader->msg.ver;
14,065,762✔
963
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
14,068,579✔
964
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
14,068,135✔
965
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
14,067,498✔
966
  pSW = NULL;
14,067,498✔
967

968
  taosMemoryFreeClear(block);
14,067,498✔
969

970
END:
14,065,107✔
971
  if (code != 0) {
14,064,884✔
972
    tqError("processBuildNew failed, code:%d", code);
×
973
  }
974
  tDeleteSchemaWrapper(pSW);
14,064,884✔
975
  blockDataFreeRes(block);
14,062,292✔
976
  taosMemoryFree(block);
14,064,232✔
977
  return code;
14,062,949✔
978
}
979
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
6,788✔
980
  int32_t code = 0;
6,788✔
981
  int32_t curRow = 0;
6,788✔
982
  int32_t lastRow = 0;
6,788✔
983

984
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
6,788✔
985
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
6,788✔
986
  TQ_NULL_GO_TO_END(assigned);
6,788✔
987

988
  SArray*   pCols = pSubmitTbData->aCol;
6,788✔
989
  SColData* pCol = taosArrayGet(pCols, 0);
6,788✔
990
  TQ_NULL_GO_TO_END(pCol);
6,788✔
991
  int32_t numOfRows = pCol->nVal;
6,788✔
992
  int32_t numOfCols = taosArrayGetSize(pCols);
6,788✔
993
  tqTrace("vgId:%d, tqProcessColData start, col num: %d, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfCols,
6,788✔
994
          numOfRows);
995
  for (int32_t i = 0; i < numOfRows; i++) {
4,059,754✔
996
    bool buildNew = false;
3,958,281✔
997

998
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
15,877,728✔
999
      int32_t k = 0;
11,393,271✔
1000
      for (; k < numOfCols; k++) {
22,108,489✔
1001
        pCol = taosArrayGet(pCols, k);
19,400,023✔
1002
        TQ_NULL_GO_TO_END(pCol);
18,408,049✔
1003
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
18,408,049✔
1004
          SColVal colVal = {0};
10,841,633✔
1005
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
11,074,764✔
1006
          PROCESS_VAL
11,709,777✔
1007
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
11,926,465✔
1008
          break;
11,593,081✔
1009
        }
1010
      }
1011
      if (k >= numOfCols) {
11,919,447✔
1012
        // this column is not in the current row, so we set it to NULL
1013
        assigned[j] = 0;
×
1014
        buildNew = true;
×
1015
      }
1016
    }
1017

1018
    if (buildNew) {
3,742,637✔
1019
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
6,788✔
1020
    }
1021

1022
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
3,742,637✔
1023
    TQ_NULL_GO_TO_END(pBlock);
4,136,573✔
1024

1025
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
4,136,573✔
1026
            (int32_t)taosArrayGetSize(blocks));
1027

1028
    int32_t targetIdx = 0;
4,136,573✔
1029
    int32_t sourceIdx = 0;
4,136,573✔
1030
    int32_t colActual = blockDataGetNumOfCols(pBlock);
4,136,573✔
1031
    while (targetIdx < colActual && sourceIdx < numOfCols) {
15,819,468✔
1032
      pCol = taosArrayGet(pCols, sourceIdx);
11,766,502✔
1033
      TQ_NULL_GO_TO_END(pCol);
11,285,015✔
1034
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
11,285,015✔
1035
      TQ_NULL_GO_TO_END(pColData);
10,853,437✔
1036
      SColVal colVal = {0};
10,853,437✔
1037
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
10,538,236✔
1038
      SET_DATA
11,914,460✔
1039
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
11,506,401✔
1040
    }
1041

1042
    curRow++;
4,052,966✔
1043
  }
1044
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
101,473✔
1045
  pLastBlock->info.rows = curRow - lastRow;
6,788✔
1046
  tqTrace("vgId:%d, tqProcessColData end, col num: %d, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId,
6,788✔
1047
          numOfCols, numOfRows, (int)taosArrayGetSize(blocks));
1048
END:
2,441,175✔
1049
  if (code != TSDB_CODE_SUCCESS) {
6,788✔
1050
    tqError("vgId:%d, process col data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1051
  }
1052
  taosMemoryFree(assigned);
6,788✔
1053
  return code;
6,788✔
1054
}
1055

1056
int32_t tqProcessRowData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
14,051,913✔
1057
  int32_t   code = 0;
14,051,913✔
1058
  STSchema* pTSchema = NULL;
14,051,913✔
1059

1060
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
14,051,913✔
1061
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
14,055,950✔
1062
  TQ_NULL_GO_TO_END(assigned);
14,054,690✔
1063

1064
  int32_t curRow = 0;
14,054,690✔
1065
  int32_t lastRow = 0;
14,054,690✔
1066
  SArray* pRows = pSubmitTbData->aRowP;
14,051,451✔
1067
  int32_t numOfRows = taosArrayGetSize(pRows);
14,059,873✔
1068
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
14,058,680✔
1069
  TQ_NULL_GO_TO_END(pTSchema);
14,057,652✔
1070
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
14,057,652✔
1071

1072
  for (int32_t i = 0; i < numOfRows; i++) {
296,150,147✔
1073
    bool  buildNew = false;
282,133,358✔
1074
    SRow* pRow = taosArrayGetP(pRows, i);
282,133,358✔
1075
    TQ_NULL_GO_TO_END(pRow);
282,093,744✔
1076

1077
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
1,564,530,520✔
1078
      SColVal colVal = {0};
1,280,384,967✔
1079
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
1,279,909,627✔
1080
      PROCESS_VAL
1,281,652,392✔
1081
      tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], pTSchema->numOfCols);
1,282,622,060✔
1082
    }
1083

1084
    if (buildNew) {
280,734,697✔
1085
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
14,062,040✔
1086
    }
1087

1088
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
280,730,575✔
1089
    TQ_NULL_GO_TO_END(pBlock);
282,476,353✔
1090

1091
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
282,476,353✔
1092
            (int32_t)taosArrayGetSize(blocks));
1093

1094
    int32_t targetIdx = 0;
282,476,353✔
1095
    int32_t sourceIdx = 0;
282,476,353✔
1096
    int32_t colActual = blockDataGetNumOfCols(pBlock);
282,476,353✔
1097
    while (targetIdx < colActual && sourceIdx < pTSchema->numOfCols) {
1,562,522,299✔
1098
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
1,279,733,585✔
1099
      TQ_NULL_GO_TO_END(pColData);
1,278,729,636✔
1100
      SColVal          colVal = {0};
1,278,729,636✔
1101
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
1,278,117,972✔
1102
      SET_DATA
1,279,791,614✔
1103
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
1,281,084,367✔
1104
    }
1105

1106
    curRow++;
282,100,378✔
1107
  }
1108
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
14,016,789✔
1109
  if (pLastBlock != NULL) {
14,062,573✔
1110
    pLastBlock->info.rows = curRow - lastRow;
14,062,573✔
1111
  }
1112

1113
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
14,060,122✔
1114
          (int)taosArrayGetSize(blocks));
1115
END:
14,857,602✔
1116
  if (code != TSDB_CODE_SUCCESS) {
14,060,634✔
1117
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1118
  }
1119
  taosMemoryFreeClear(pTSchema);
14,056,486✔
1120
  taosMemoryFree(assigned);
14,059,267✔
1121
  return code;
14,054,636✔
1122
}
1123

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

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

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

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

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

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

1171
  if (pSubmitTbDataRet) {
14,107,814✔
1172
    *pSubmitTbDataRet = pSubmitTbData;
14,108,588✔
1173
  }
1174

1175
  if (fetchMeta == ONLY_META) {
14,108,037✔
1176
    if (pSubmitTbData->pCreateTbReq != NULL) {
674✔
1177
      if (pRsp->createTableReq == NULL) {
260✔
1178
        pRsp->createTableReq = taosArrayInit(0, POINTER_BYTES);
191✔
1179
        if (pRsp->createTableReq == NULL) {
191✔
1180
          return terrno;
×
1181
        }
1182
      }
1183
      if (taosArrayPush(pRsp->createTableReq, &pSubmitTbData->pCreateTbReq) == NULL) {
520✔
1184
        return terrno;
×
1185
      }
1186
      pSubmitTbData->pCreateTbReq = NULL;
260✔
1187
    }
1188
    return 0;
674✔
1189
  }
1190

1191
  int32_t sversion = pSubmitTbData->sver;
14,107,363✔
1192
  int64_t uid = pSubmitTbData->uid;
14,107,843✔
1193
  pReader->lastBlkUid = uid;
14,108,362✔
1194

1195
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
14,108,488✔
1196
  taosMemoryFreeClear(pReader->extSchema);
14,107,459✔
1197
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
14,106,711✔
1198
  if (pReader->pSchemaWrapper == NULL) {
14,106,157✔
1199
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
39,091✔
1200
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1201
    pReader->cachedSchemaSuid = 0;
39,091✔
1202
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
39,091✔
1203
  }
1204

1205
  if (pSubmitTbData->pCreateTbReq != NULL) {
14,064,155✔
1206
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
794✔
1207
    if (code != 0) {
794✔
1208
      return code;
×
1209
    }
1210
  } else if (rawList != NULL) {
14,065,647✔
1211
    if (taosArrayPush(schemas, &pReader->pSchemaWrapper) == NULL) {
×
1212
      return terrno;
×
1213
    }
1214
    pReader->pSchemaWrapper = NULL;
×
1215
    return 0;
×
1216
  }
1217

1218
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
14,066,441✔
1219
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
6,788✔
1220
  } else {
1221
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
14,054,082✔
1222
  }
1223
}
1224

1225
int32_t tqReaderSetColIdList(STqReader* pReader, SArray* pColIdList, const char* id) {
108,685✔
1226
  if (pReader == NULL) {
108,685✔
1227
    return TSDB_CODE_SUCCESS;
×
1228
  }
1229
  pReader->pColIdList = pColIdList;
108,685✔
1230
  return tqCollectPhysicalTables(pReader, id);
109,047✔
1231
}
1232

1233
int32_t tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
113,267✔
1234
  if (pReader == NULL || tbUidList == NULL) {
113,267✔
1235
    return TSDB_CODE_SUCCESS;
×
1236
  }
1237
  if (pReader->tbIdHash) {
113,362✔
1238
    taosHashClear(pReader->tbIdHash);
1,090✔
1239
  } else {
1240
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
112,410✔
1241
    if (pReader->tbIdHash == NULL) {
112,590✔
1242
      tqError("s-task:%s failed to init hash table", id);
×
1243
      return terrno;
×
1244
    }
1245
  }
1246

1247
  for (int i = 0; i < taosArrayGetSize(tbUidList); i++) {
2,794,669✔
1248
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
2,681,038✔
1249
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
2,680,764✔
1250
      tqError("s-task:%s failed to add table uid:%" PRId64 " to hash", id, *pKey);
×
1251
      continue;
×
1252
    }
1253
  }
1254

1255
  tqDebug("s-task:%s %d tables are set to be queried target table", id, (int32_t)taosArrayGetSize(tbUidList));
113,721✔
1256
  return TSDB_CODE_SUCCESS;
113,721✔
1257
}
1258

1259
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
50,792✔
1260
  if (pReader == NULL || pTableUidList == NULL) {
50,792✔
1261
    return;
×
1262
  }
1263
  if (pReader->tbIdHash == NULL) {
50,792✔
1264
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
×
1265
    if (pReader->tbIdHash == NULL) {
×
1266
      tqError("failed to init hash table");
×
1267
      return;
×
1268
    }
1269
  }
1270

1271
  int32_t numOfTables = taosArrayGetSize(pTableUidList);
50,792✔
1272
  for (int i = 0; i < numOfTables; i++) {
80,182✔
1273
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
29,390✔
1274
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
29,390✔
1275
      tqError("failed to add table uid:%" PRId64 " to hash", *pKey);
×
1276
      continue;
×
1277
    }
1278
  }
1279
}
1280

1281
bool tqReaderIsQueriedTable(STqReader* pReader, uint64_t uid) {
×
1282
  if (pReader == NULL) {
×
1283
    return false;
×
1284
  }
1285
  return taosHashGet(pReader->tbIdHash, &uid, sizeof(uint64_t)) != NULL;
×
1286
}
1287

1288
bool tqCurrentBlockConsumed(const STqReader* pReader) {
×
1289
  if (pReader == NULL) {
×
1290
    return false;
×
1291
  }
1292
  return pReader->msg.msgStr == NULL;
×
1293
}
1294

1295
void tqReaderRemoveTbUidList(STqReader* pReader, const SArray* tbUidList) {
187✔
1296
  if (pReader == NULL || tbUidList == NULL) {
187✔
1297
    return;
×
1298
  }
1299
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
374✔
1300
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
187✔
1301
    if (pKey && taosHashRemove(pReader->tbIdHash, pKey, sizeof(int64_t)) != 0) {
187✔
1302
      tqError("failed to remove table uid:%" PRId64 " from hash", *pKey);
×
1303
    }
1304
  }
1305
}
1306

1307
int32_t tqUpdateTbUidList(STQ* pTq, const SArray* tbUidList, bool isAdd) {
63,220,027✔
1308
  if (pTq == NULL) {
63,220,027✔
1309
    return 0;  // mounted vnode may have no tq
×
1310
  }
1311
  if (tbUidList == NULL) {
63,220,027✔
1312
    return TSDB_CODE_INVALID_PARA;
×
1313
  }
1314
  void*   pIter = NULL;
63,220,027✔
1315
  int32_t vgId = TD_VID(pTq->pVnode);
63,220,027✔
1316

1317
  // update the table list for each consumer handle
1318
  taosWLockLatch(&pTq->lock);
63,222,272✔
1319
  while (1) {
191,158✔
1320
    pIter = taosHashIterate(pTq->pHandle, pIter);
63,412,778✔
1321
    if (pIter == NULL) {
63,412,934✔
1322
      break;
63,221,776✔
1323
    }
1324

1325
    STqHandle* pTqHandle = (STqHandle*)pIter;
191,158✔
1326
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
191,158✔
1327
      int32_t code = qUpdateTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList, isAdd);
50,979✔
1328
      if (code != 0) {
50,979✔
1329
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1330
        continue;
×
1331
      }
1332
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
140,179✔
1333
      if (!isAdd) {
139,089✔
1334
        int32_t sz = taosArrayGetSize(tbUidList);
46,720✔
1335
        for (int32_t i = 0; i < sz; i++) {
46,720✔
1336
          int64_t* tbUid = (int64_t*)taosArrayGet(tbUidList, i);
×
1337
          if (tbUid &&
×
1338
              taosHashPut(pTqHandle->execHandle.execDb.pFilterOutTbUid, tbUid, sizeof(int64_t), NULL, 0) != 0) {
×
1339
            tqError("failed to add table uid:%" PRId64 " to hash", *tbUid);
×
1340
            continue;
×
1341
          }
1342
        }
1343
      }
1344
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
1,090✔
1345
      if (isAdd) {
1,090✔
1346
        SArray* list = NULL;
1,090✔
1347
        int     ret = qGetTableList(pTqHandle->execHandle.execTb.suid, pTq->pVnode, pTqHandle->execHandle.execTb.node,
1,090✔
1348
                                    &list, pTqHandle->execHandle.task);
1349
        if (ret == 0) {
1,090✔
1350
          ret = tqReaderSetTbUidList(pTqHandle->execHandle.pTqReader, list, NULL);
1,090✔
1351
        }                            
1352
        if (ret != TDB_CODE_SUCCESS) {
1,090✔
1353
          tqError("qGetTableList in tqUpdateTbUidList error:%d handle %s consumer:0x%" PRIx64, ret, pTqHandle->subKey,
×
1354
                  pTqHandle->consumerId);
1355
          taosArrayDestroy(list);
×
1356
          taosHashCancelIterate(pTq->pHandle, pIter);
×
1357
          taosWUnLockLatch(&pTq->lock);
×
1358

1359
          return ret;
×
1360
        }
1361
        taosArrayDestroy(list);
1,090✔
1362
      } else {
1363
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
×
1364
      }
1365
    }
1366
  }
1367
  taosWUnLockLatch(&pTq->lock);
63,221,776✔
1368
  return 0;
63,222,272✔
1369
}
1370

1371
static void destroySourceScanTables(void* ptr) {
×
1372
  SArray** pTables = ptr;
×
1373
  if (pTables && *pTables) {
×
1374
    taosArrayDestroy(*pTables);
×
1375
    *pTables = NULL;
×
1376
  }
1377
}
×
1378

1379
static int32_t compareSVTColInfo(const void* p1, const void* p2) {
×
1380
  SVTColInfo* pCol1 = (SVTColInfo*)p1;
×
1381
  SVTColInfo* pCol2 = (SVTColInfo*)p2;
×
1382
  if (pCol1->vColId == pCol2->vColId) {
×
1383
    return 0;
×
1384
  } else if (pCol1->vColId < pCol2->vColId) {
×
1385
    return -1;
×
1386
  } else {
1387
    return 1;
×
1388
  }
1389
}
1390

1391
static int32_t tqCollectPhysicalTables(STqReader* pReader, const char* idstr) {
108,952✔
1392
  int32_t            code = TSDB_CODE_SUCCESS;
108,952✔
1393
  int32_t            lino = 0;
108,952✔
1394
  SVTSourceScanInfo* pScanInfo = NULL;
108,952✔
1395
  SHashObj*          pVirtualTables = NULL;
108,952✔
1396
  SHashObj*          pPhysicalTables = NULL;
108,952✔
1397
  void*              pIter = NULL;
108,952✔
1398
  void*              px = NULL;
108,952✔
1399

1400
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
108,952✔
1401

1402
  pScanInfo = &pReader->vtSourceScanInfo;
108,952✔
1403
  taosHashCleanup(pScanInfo->pPhysicalTables);
108,952✔
1404
  pScanInfo->pPhysicalTables = NULL;
108,975✔
1405
  taosLRUCacheCleanup(pScanInfo->pPhyTblSchemaCache);
108,952✔
1406
  pScanInfo->pPhyTblSchemaCache = NULL;
108,880✔
1407
  pScanInfo->nextVirtualTableIdx = -1;
108,880✔
1408
  pScanInfo->metaFetch = 0;
108,952✔
1409
  pScanInfo->cacheHit = 0;
109,047✔
1410

1411
  pVirtualTables = pScanInfo->pVirtualTables;
109,047✔
1412
  if (taosHashGetSize(pVirtualTables) == 0 || taosArrayGetSize(pReader->pColIdList) == 0) {
109,047✔
1413
    goto _end;
108,952✔
1414
  }
1415

1416
  pPhysicalTables = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
1417
  TSDB_CHECK_NULL(pPhysicalTables, code, lino, _end, terrno);
×
UNCOV
1418
  taosHashSetFreeFp(pPhysicalTables, destroySourceScanTables);
×
1419

UNCOV
1420
  pIter = taosHashIterate(pVirtualTables, NULL);
×
UNCOV
1421
  while (pIter != NULL) {
×
UNCOV
1422
    int64_t vTbUid = *(int64_t*)taosHashGetKey(pIter, NULL);
×
UNCOV
1423
    SArray* pColInfos = *(SArray**)pIter;
×
UNCOV
1424
    TSDB_CHECK_NULL(pColInfos, code, lino, _end, TSDB_CODE_INTERNAL_ERROR);
×
1425

1426
    // Traverse all required columns and collect corresponding physical tables
UNCOV
1427
    int32_t nColInfos = taosArrayGetSize(pColInfos);
×
UNCOV
1428
    int32_t nOutputCols = taosArrayGetSize(pReader->pColIdList);
×
UNCOV
1429
    for (int32_t i = 0, j = 0; i < nColInfos && j < nOutputCols;) {
×
UNCOV
1430
      SVTColInfo* pCol = taosArrayGet(pColInfos, i);
×
UNCOV
1431
      col_id_t    colIdNeed = *(col_id_t*)taosArrayGet(pReader->pColIdList, j);
×
UNCOV
1432
      if (pCol->vColId < colIdNeed) {
×
UNCOV
1433
        i++;
×
UNCOV
1434
      } else if (pCol->vColId > colIdNeed) {
×
UNCOV
1435
        j++;
×
1436
      } else {
UNCOV
1437
        SArray* pRelatedVTs = NULL;
×
UNCOV
1438
        px = taosHashGet(pPhysicalTables, &pCol->pTbUid, sizeof(int64_t));
×
UNCOV
1439
        if (px == NULL) {
×
UNCOV
1440
          pRelatedVTs = taosArrayInit(8, sizeof(int64_t));
×
UNCOV
1441
          TSDB_CHECK_NULL(pRelatedVTs, code, lino, _end, terrno);
×
UNCOV
1442
          code = taosHashPut(pPhysicalTables, &pCol->pTbUid, sizeof(int64_t), &pRelatedVTs, POINTER_BYTES);
×
UNCOV
1443
          if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
1444
            taosArrayDestroy(pRelatedVTs);
×
UNCOV
1445
            TSDB_CHECK_CODE(code, lino, _end);
×
1446
          }
1447
        } else {
UNCOV
1448
          pRelatedVTs = *(SArray**)px;
×
1449
        }
UNCOV
1450
        if (taosArrayGetSize(pRelatedVTs) == 0 || *(int64_t*)taosArrayGetLast(pRelatedVTs) != vTbUid) {
×
UNCOV
1451
          px = taosArrayPush(pRelatedVTs, &vTbUid);
×
UNCOV
1452
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
×
1453
        }
UNCOV
1454
        i++;
×
UNCOV
1455
        j++;
×
1456
      }
1457
    }
UNCOV
1458
    pIter = taosHashIterate(pVirtualTables, pIter);
×
1459
  }
1460

UNCOV
1461
  pScanInfo->pPhysicalTables = pPhysicalTables;
×
UNCOV
1462
  pPhysicalTables = NULL;
×
1463

UNCOV
1464
  if (taosHashGetSize(pScanInfo->pPhysicalTables) > 0) {
×
UNCOV
1465
    pScanInfo->pPhyTblSchemaCache = taosLRUCacheInit(1024 * 128, -1, .5);
×
UNCOV
1466
    TSDB_CHECK_NULL(pScanInfo->pPhyTblSchemaCache, code, lino, _end, terrno);
×
1467
  }
1468

UNCOV
1469
_end:
×
1470
  if (code != TSDB_CODE_SUCCESS) {
108,952✔
UNCOV
1471
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1472
  }
1473
  if (pIter != NULL) {
108,952✔
UNCOV
1474
    taosHashCancelIterate(pReader->tbIdHash, pIter);
×
1475
  }
1476
  if (pPhysicalTables != NULL) {
108,952✔
UNCOV
1477
    taosHashCleanup(pPhysicalTables);
×
1478
  }
1479
  return code;
109,047✔
1480
}
1481

UNCOV
1482
static void freeTableSchemaCache(const void* key, size_t keyLen, void* value, void* ud) {
×
UNCOV
1483
  if (value) {
×
UNCOV
1484
    SSchemaWrapper* pSchemaWrapper = value;
×
1485
    tDeleteSchemaWrapper(pSchemaWrapper);
1486
  }
UNCOV
1487
}
×
1488

UNCOV
1489
bool tqNextVTableSourceBlockImpl(STqReader* pReader, const char* idstr) {
×
UNCOV
1490
  int32_t            code = TSDB_CODE_SUCCESS;
×
UNCOV
1491
  int32_t            lino = 0;
×
UNCOV
1492
  SVTSourceScanInfo* pScanInfo = NULL;
×
1493

UNCOV
1494
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1495

UNCOV
1496
  pScanInfo = &pReader->vtSourceScanInfo;
×
UNCOV
1497
  if (pReader->msg.msgStr == NULL || taosHashGetSize(pScanInfo->pPhysicalTables) == 0) {
×
UNCOV
1498
    return false;
×
1499
  }
1500

UNCOV
1501
  if (pScanInfo->nextVirtualTableIdx >= 0) {
×
1502
    // The data still needs to be converted into the virtual table result block
UNCOV
1503
    return true;
×
1504
  }
1505

UNCOV
1506
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
×
UNCOV
1507
  while (pReader->nextBlk < blockSz) {
×
UNCOV
1508
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
×
UNCOV
1509
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, _end, terrno);
×
UNCOV
1510
    int64_t pTbUid = pSubmitTbData->uid;
×
UNCOV
1511
    void*   px = taosHashGet(pScanInfo->pPhysicalTables, &pTbUid, sizeof(int64_t));
×
UNCOV
1512
    if (px != NULL) {
×
UNCOV
1513
      SArray* pRelatedVTs = *(SArray**)px;
×
UNCOV
1514
      if (taosArrayGetSize(pRelatedVTs) > 0) {
×
UNCOV
1515
        pScanInfo->nextVirtualTableIdx = 0;
×
UNCOV
1516
        return true;
×
1517
      }
1518
    }
UNCOV
1519
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64, pReader->nextBlk, blockSz, pTbUid);
×
UNCOV
1520
    pReader->nextBlk++;
×
1521
  }
1522

UNCOV
1523
  tqReaderClearSubmitMsg(pReader);
×
UNCOV
1524
  tqTrace("iterator data block end, total block num:%d", blockSz);
×
1525

UNCOV
1526
_end:
×
UNCOV
1527
  if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
1528
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1529
  }
UNCOV
1530
  return false;
×
1531
}
1532

UNCOV
1533
bool tqReaderIsQueriedSourceTable(STqReader* pReader, uint64_t uid) {
×
UNCOV
1534
  if (pReader == NULL) {
×
UNCOV
1535
    return false;
×
1536
  }
UNCOV
1537
  return taosHashGet(pReader->vtSourceScanInfo.pPhysicalTables, &uid, sizeof(uint64_t)) != NULL;
×
1538
}
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