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

taosdata / TDengine / #4879

11 Dec 2025 02:43AM UTC coverage: 64.544% (-0.03%) from 64.569%
#4879

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

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

3883 existing lines in 125 files now uncovered.

163565 of 253417 relevant lines covered (64.54%)

105600506.39 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

254
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
15,408,869✔
255
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
15,408,869✔
256
    return -1;
×
257
  }
258
  int32_t code = -1;
15,429,263✔
259
  int32_t vgId = TD_VID(pTq->pVnode);
15,429,263✔
260
  int64_t id = pHandle->pWalReader->readerId;
15,431,941✔
261

262
  int64_t offset = *fetchOffset;
15,431,857✔
263
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
15,432,728✔
264
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
15,431,499✔
265
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
15,430,892✔
266

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

271
  while (offset <= appliedVer) {
15,849,962✔
272
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
14,921,250✔
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,921,956✔
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,922,174✔
283
      code = walFetchBody(pHandle->pWalReader);
14,499,162✔
284
      goto END;
14,498,771✔
285
    } else {
286
      if (pHandle->fetchMeta != WITH_DATA) {
422,970✔
287
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
10,516✔
288
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
10,516✔
289
          code = walFetchBody(pHandle->pWalReader);
7,390✔
290
          if (code < 0) {
7,390✔
291
            goto END;
×
292
          }
293

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

314
END:
928,712✔
315
  *fetchOffset = offset;
15,434,735✔
316
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
15,433,677✔
317
          ", applied:%" PRId64 ", 0x%" PRIx64,
318
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
319
  return code;
15,433,992✔
320
}
321

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

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

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

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

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

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

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

373
  return pReader;
126,704✔
374
}
375

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

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

385
  if (pReader->pSchemaWrapper) {
126,704✔
386
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
75,942✔
387
  }
388

389
  taosMemoryFree(pReader->extSchema);
126,704✔
390
  if (pReader->pColIdList) {
126,704✔
391
    taosArrayDestroy(pReader->pColIdList);
107,151✔
392
  }
393

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

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

405
int32_t tqReaderSeek(STqReader* pReader, int64_t ver, const char* id) {
202,360✔
406
  if (pReader == NULL) {
202,360✔
407
    return TSDB_CODE_INVALID_PARA;
×
408
  }
409
  if (walReaderSeekVer(pReader->pWalReader, ver) < 0) {
202,360✔
410
    return terrno;
8,276✔
411
  }
412
  tqDebug("wal reader seek to ver:%" PRId64 " %s", ver, id);
194,154✔
413
  return 0;
194,584✔
414
}
415

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

422
  int64_t st = taosGetTimestampMs();
35,117,564✔
423
  while (1) {
35,827,413✔
424
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
70,944,977✔
425
    while (pReader->nextBlk < numOfBlocks) {
74,464,205✔
426
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
35,825,589✔
427
              pReader->msg.ver);
428

429
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
35,825,589✔
430
      if (pSubmitTbData == NULL) {
35,822,315✔
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;
561✔
434
      }
435
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
35,822,315✔
436
        pReader->nextBlk += 1;
1,272✔
437
        continue;
1,272✔
438
      }
439
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
35,819,311✔
440
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
32,301,229✔
441
        SSDataBlock* pRes = NULL;
32,301,229✔
442
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
32,297,959✔
443
        if (code == TSDB_CODE_SUCCESS) {
32,304,005✔
444
          return true;
32,304,005✔
445
        }
446
      } else {
447
        pReader->nextBlk += 1;
3,521,912✔
448
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
3,521,912✔
449
      }
450
    }
451

452
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
38,639,345✔
453
    pReader->msg.msgStr = NULL;
38,638,734✔
454

455
    int64_t elapsed = taosGetTimestampMs() - st;
38,639,606✔
456
    if (elapsed > 1000 || elapsed < 0) {
38,639,606✔
UNCOV
457
      return false;
×
458
    }
459

460
    // try next message in wal file
461
    if (walNextValidMsg(pWalReader, false) < 0) {
38,639,668✔
462
      return false;
2,811,532✔
463
    }
464

465
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
35,826,054✔
466
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
35,828,210✔
467
    int64_t ver = pWalReader->pHead->head.version;
35,827,869✔
468
    SDecoder decoder = {0};
35,827,379✔
469
    if (tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL, &decoder) != 0) {
35,826,232✔
470
      tDecoderClear(&decoder);
×
471
      return false;
×
472
    }
473
    tDecoderClear(&decoder);
35,822,367✔
474
    pReader->nextBlk = 0;
35,825,701✔
475
  }
476
}
477

478
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList, SDecoder* decoder) {
50,323,320✔
479
  if (pReader == NULL) {
50,323,320✔
480
    return TSDB_CODE_INVALID_PARA;
×
481
  }
482
  pReader->msg.msgStr = msgStr;
50,323,320✔
483
  pReader->msg.msgLen = msgLen;
50,326,127✔
484
  pReader->msg.ver = ver;
50,324,564✔
485

486
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
50,325,936✔
487

488
  tDecoderInit(decoder, pReader->msg.msgStr, pReader->msg.msgLen);
50,325,936✔
489
  int32_t code = tDecodeSubmitReq(decoder, &pReader->submit, rawList);
50,323,439✔
490

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

495
  return code;
50,325,177✔
496
}
497

498
void tqReaderClearSubmitMsg(STqReader* pReader) {
28,976,758✔
499
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
28,976,758✔
500
  pReader->nextBlk = 0;
28,980,284✔
501
  pReader->msg.msgStr = NULL;
28,981,439✔
502
}
28,984,395✔
503

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

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

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

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

533
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
5,608,194✔
534
  while (pReader->nextBlk < blockSz) {
5,910,416✔
535
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
2,955,712✔
536
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
2,955,798✔
537
    uid = pSubmitTbData->uid;
2,955,798✔
538
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
2,955,798✔
539
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
2,955,798✔
540

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

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

549
END:
2,954,356✔
550
  tqTrace("%s:%d return:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
5,607,930✔
551
  return code;
5,607,580✔
552
}
553

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

559
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
23,072,157✔
560
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
23,072,157✔
561
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
23,079,138✔
562

563
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
23,079,138✔
564
  while (pReader->nextBlk < blockSz) {
23,082,544✔
565
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
11,544,051✔
566
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
11,543,967✔
567
    uid = pSubmitTbData->uid;
11,543,967✔
568
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
11,545,222✔
569
    TSDB_CHECK_NULL(ret, code, lino, END, true);
11,544,276✔
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,541,534✔
574
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
11,541,568✔
575

576
END:
11,541,568✔
577
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
23,085,844✔
578
  return code;
23,066,286✔
579
}
580

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

588
  int32_t cnt = 0;
14,158,220✔
589
  for (int32_t i = 0; i < pSrc->nCols; i++) {
64,573,478✔
590
    cnt += mask[i];
50,410,444✔
591
  }
592

593
  pDst->nCols = cnt;
14,160,184✔
594
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
14,159,379✔
595
  if (pDst->pSchema == NULL) {
14,150,254✔
596
    return TAOS_GET_TERRNO(terrno);
×
597
  }
598

599
  int32_t j = 0;
14,150,568✔
600
  for (int32_t i = 0; i < pSrc->nCols; i++) {
64,595,163✔
601
    if (mask[i]) {
50,420,183✔
602
      pDst->pSchema[j++] = pSrc->pSchema[i];
50,435,488✔
603
      SColumnInfoData colInfo =
50,442,867✔
604
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
50,440,615✔
605
      if (extSrc != NULL) {
50,428,612✔
606
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
2,616✔
607
      }
608
      code = blockDataAppendColInfo(pBlock, &colInfo);
50,428,612✔
609
      if (code != 0) {
50,441,672✔
610
        return code;
×
611
      }
612
    }
613
  }
614
  return 0;
14,162,561✔
615
}
616

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

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

634
  int32_t numOfCols = taosArrayGetSize(pColIdList);
65,291✔
635

636
  if (numOfCols == 0) {  // all columns are required
65,291✔
637
    for (int32_t i = 0; i < pSchema->nCols; ++i) {
×
UNCOV
638
      SSchema*        pColSchema = &pSchema->pSchema[i];
×
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) {
65,291✔
652
      numOfCols = pSchema->nCols;
68✔
653
    }
654

655
    int32_t i = 0;
65,291✔
656
    int32_t j = 0;
65,291✔
657
    while (i < pSchema->nCols && j < numOfCols) {
830,612✔
658
      SSchema* pColSchema = &pSchema->pSchema[i];
765,424✔
659
      col_id_t colIdSchema = pColSchema->colId;
765,424✔
660

661
      col_id_t* pColIdNeed = (col_id_t*)taosArrayGet(pColIdList, j);
765,321✔
662
      if (pColIdNeed == NULL) {
765,854✔
663
        break;
×
664
      }
665
      if (colIdSchema < *pColIdNeed) {
765,854✔
666
        i++;
44,442✔
667
      } else if (colIdSchema > *pColIdNeed) {
721,412✔
668
        j++;
×
669
      } else {
670
        SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
721,206✔
671
        if (IS_DECIMAL_TYPE(pColSchema->type) && pReader->extSchema != NULL) {
721,412✔
672
          decimalFromTypeMod(pReader->extSchema[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
55,790✔
673
        }
674
        int32_t code = blockDataAppendColInfo(pBlock, &colInfo);
721,412✔
675
        if (code != TSDB_CODE_SUCCESS) {
721,309✔
676
          return -1;
×
677
        }
678
        i++;
721,309✔
679
        j++;
721,309✔
680
      }
681
    }
682
  }
683

684
  return TSDB_CODE_SUCCESS;
65,291✔
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)) {
720,105,199✔
733
      char val[65535 + 2] = {0};
722,023,544✔
734
      if (pColVal->value.pData != NULL) {
722,037,357✔
735
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
720,661,443✔
736
      }
737
      varDataSetLen(val, pColVal->value.nData);
722,087,229✔
738
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
721,887,812✔
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,292,731✔
751
  if (pReader == NULL || pRes == NULL) {
32,292,731✔
752
    return TSDB_CODE_INVALID_PARA;
×
753
  }
754
  tqDebug("tq reader retrieve data block %p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
32,301,136✔
755
  int32_t        code = 0;
32,307,057✔
756
  int32_t        line = 0;
32,307,057✔
757
  STSchema*      pTSchema = NULL;
32,307,057✔
758
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk++);
32,307,057✔
759
  TSDB_CHECK_NULL(pSubmitTbData, code, line, END, terrno);
32,306,138✔
760
  SSDataBlock* pBlock = pReader->pResBlock;
32,306,138✔
761
  *pRes = pBlock;
32,306,244✔
762

763
  blockDataCleanup(pBlock);
32,306,244✔
764

765
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
32,305,396✔
766
  int32_t sversion = pSubmitTbData->sver;
32,305,714✔
767
  int64_t suid = pSubmitTbData->suid;
32,305,608✔
768
  int64_t uid = pSubmitTbData->uid;
32,305,926✔
769
  pReader->lastTs = pSubmitTbData->ctimeMs;
32,306,032✔
770

771
  pBlock->info.id.uid = uid;
32,306,032✔
772
  pBlock->info.version = pReader->msg.ver;
32,305,290✔
773

774
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
32,305,556✔
775
      (pReader->cachedSchemaVer != sversion)) {
32,240,173✔
776
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
65,557✔
777
    taosMemoryFree(pReader->extSchema);
65,291✔
778
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
65,291✔
779
    if (pReader->pSchemaWrapper == NULL) {
65,291✔
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,188✔
788
    pReader->cachedSchemaSuid = suid;
65,188✔
789
    pReader->cachedSchemaVer = sversion;
65,291✔
790

791
    if (pReader->cachedSchemaVer != pReader->pSchemaWrapper->version) {
65,291✔
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,188✔
797
    TSDB_CHECK_CODE(code, line, END);
65,291✔
798
    pBlock = pReader->pResBlock;
65,291✔
799
    *pRes = pBlock;
65,291✔
800
  }
801

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

811
  code = blockDataEnsureCapacity(pBlock, numOfRows);
32,305,600✔
812
  TSDB_CHECK_CODE(code, line, END);
32,305,732✔
813
  pBlock->info.rows = numOfRows;
32,305,732✔
814
  int32_t colActual = blockDataGetNumOfCols(pBlock);
32,305,838✔
815

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

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

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

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

866
    for (int32_t i = 0; i < numOfRows; i++) {
1,650,743,314✔
867
      SRow* pRow = taosArrayGetP(pRows, i);
1,549,570,198✔
868
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
1,545,548,446✔
869
      int32_t sourceIdx = 0;
1,545,368,103✔
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,452,522✔
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,452,522✔
882
            continue;
137,452,522✔
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:
101,173,116✔
904
  if (code != 0) {
82,477,457✔
905
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
906
  }
907
  taosMemoryFreeClear(pTSchema);
32,302,376✔
908
  return code;
32,303,808✔
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,143,558✔
940
                               char* assigned, int32_t numOfRows, int32_t curRow, int32_t* lastRow) {
941
  int32_t         code = 0;
14,143,558✔
942
  SSchemaWrapper* pSW = NULL;
14,143,558✔
943
  SSDataBlock*    block = NULL;
14,151,120✔
944
  if (taosArrayGetSize(blocks) > 0) {
14,151,120✔
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,154,643✔
952
  TQ_NULL_GO_TO_END(block);
14,146,128✔
953

954
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
14,146,128✔
955
  TQ_NULL_GO_TO_END(pSW);
14,153,696✔
956

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

961
  block->info.id.uid = pSubmitTbData->uid;
14,162,395✔
962
  block->info.version = pReader->msg.ver;
14,158,637✔
963
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
14,161,209✔
964
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
14,159,260✔
965
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
14,158,794✔
966
  pSW = NULL;
14,158,794✔
967

968
  taosMemoryFreeClear(block);
14,158,794✔
969

970
END:
14,156,961✔
971
  if (code != 0) {
14,156,361✔
972
    tqError("processBuildNew failed, code:%d", code);
×
973
  }
974
  tDeleteSchemaWrapper(pSW);
14,156,361✔
975
  blockDataFreeRes(block);
14,151,170✔
976
  taosMemoryFree(block);
14,153,192✔
977
  return code;
14,150,031✔
978
}
979
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
5,444✔
980
  int32_t code = 0;
5,444✔
981
  int32_t curRow = 0;
5,444✔
982
  int32_t lastRow = 0;
5,444✔
983

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

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

998
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
15,626,340✔
999
      int32_t k = 0;
11,293,732✔
1000
      for (; k < numOfCols; k++) {
21,928,496✔
1001
        pCol = taosArrayGet(pCols, k);
19,856,696✔
1002
        TQ_NULL_GO_TO_END(pCol);
19,165,964✔
1003
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
19,165,964✔
1004
          SColVal colVal = {0};
10,635,816✔
1005
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
11,160,956✔
1006
          PROCESS_VAL
11,682,400✔
1007
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
11,809,156✔
1008
          break;
10,871,996✔
1009
        }
1010
      }
1011
      if (k >= numOfCols) {
11,738,148✔
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,710,812✔
1019
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
5,472✔
1020
    }
1021

1022
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
3,710,812✔
1023
    TQ_NULL_GO_TO_END(pBlock);
3,952,956✔
1024

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

1028
    int32_t targetIdx = 0;
3,952,956✔
1029
    int32_t sourceIdx = 0;
3,952,956✔
1030
    int32_t colActual = blockDataGetNumOfCols(pBlock);
3,952,956✔
1031
    while (targetIdx < colActual && sourceIdx < numOfCols) {
15,470,616✔
1032
      pCol = taosArrayGet(pCols, sourceIdx);
11,522,112✔
1033
      TQ_NULL_GO_TO_END(pCol);
10,877,496✔
1034
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
10,877,496✔
1035
      TQ_NULL_GO_TO_END(pColData);
11,054,792✔
1036
      SColVal colVal = {0};
11,054,792✔
1037
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
10,317,244✔
1038
      SET_DATA
11,865,952✔
1039
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
11,618,628✔
1040
    }
1041

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

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

1060
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
14,137,351✔
1061
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
14,143,768✔
1062
  TQ_NULL_GO_TO_END(assigned);
14,146,723✔
1063

1064
  int32_t curRow = 0;
14,146,723✔
1065
  int32_t lastRow = 0;
14,146,723✔
1066
  SArray* pRows = pSubmitTbData->aRowP;
14,142,673✔
1067
  int32_t numOfRows = taosArrayGetSize(pRows);
14,150,850✔
1068
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
14,149,589✔
1069
  TQ_NULL_GO_TO_END(pTSchema);
14,151,434✔
1070
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
14,151,434✔
1071

1072
  for (int32_t i = 0; i < numOfRows; i++) {
281,479,252✔
1073
    bool  buildNew = false;
267,379,897✔
1074
    SRow* pRow = taosArrayGetP(pRows, i);
267,379,897✔
1075
    TQ_NULL_GO_TO_END(pRow);
267,117,116✔
1076

1077
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
1,471,601,141✔
1078
      SColVal colVal = {0};
1,202,350,538✔
1079
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
1,202,302,267✔
1080
      PROCESS_VAL
1,204,164,542✔
1081
      tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], pTSchema->numOfCols);
1,204,586,403✔
1082
    }
1083

1084
    if (buildNew) {
266,205,155✔
1085
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
14,156,487✔
1086
    }
1087

1088
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
266,196,501✔
1089
    TQ_NULL_GO_TO_END(pBlock);
267,755,676✔
1090

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

1094
    int32_t targetIdx = 0;
267,755,676✔
1095
    int32_t sourceIdx = 0;
267,755,676✔
1096
    int32_t colActual = blockDataGetNumOfCols(pBlock);
267,755,676✔
1097
    while (targetIdx < colActual && sourceIdx < pTSchema->numOfCols) {
1,469,026,985✔
1098
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
1,201,323,207✔
1099
      TQ_NULL_GO_TO_END(pColData);
1,200,599,757✔
1100
      SColVal          colVal = {0};
1,200,599,757✔
1101
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
1,198,815,605✔
1102
      SET_DATA
1,201,872,075✔
1103
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
1,202,199,139✔
1104
    }
1105

1106
    curRow++;
267,340,795✔
1107
  }
1108
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
14,099,355✔
1109
  if (pLastBlock != NULL) {
14,157,408✔
1110
    pLastBlock->info.rows = curRow - lastRow;
14,157,450✔
1111
  }
1112

1113
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
14,153,432✔
1114
          (int)taosArrayGetSize(blocks));
1115
END:
15,389,301✔
1116
  if (code != TSDB_CODE_SUCCESS) {
14,153,805✔
1117
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1118
  }
1119
  taosMemoryFreeClear(pTSchema);
14,143,984✔
1120
  taosMemoryFree(assigned);
14,151,508✔
1121
  return code;
14,147,193✔
1122
}
1123

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

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

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

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

1154
END:
450✔
1155
  if (code != 0) {
450✔
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;
450✔
1160
}
1161

1162
int32_t tqRetrieveTaosxBlock(STqReader* pReader, SMqDataRsp* pRsp, SArray* blocks, SArray* schemas,
14,198,100✔
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,198,100✔
1165
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
14,198,100✔
1166
  if (pSubmitTbData == NULL) {
14,198,952✔
1167
    return terrno;
×
1168
  }
1169
  pReader->nextBlk++;
14,198,952✔
1170

1171
  if (pSubmitTbDataRet) {
14,196,699✔
1172
    *pSubmitTbDataRet = pSubmitTbData;
14,197,986✔
1173
  }
1174

1175
  if (fetchMeta == ONLY_META) {
14,196,569✔
1176
    if (pSubmitTbData->pCreateTbReq != NULL) {
429✔
1177
      if (pRsp->createTableReq == NULL) {
213✔
1178
        pRsp->createTableReq = taosArrayInit(0, POINTER_BYTES);
177✔
1179
        if (pRsp->createTableReq == NULL) {
177✔
1180
          return terrno;
×
1181
        }
1182
      }
1183
      if (taosArrayPush(pRsp->createTableReq, &pSubmitTbData->pCreateTbReq) == NULL) {
426✔
1184
        return terrno;
×
1185
      }
1186
      pSubmitTbData->pCreateTbReq = NULL;
213✔
1187
    }
1188
    return 0;
429✔
1189
  }
1190

1191
  int32_t sversion = pSubmitTbData->sver;
14,196,140✔
1192
  int64_t uid = pSubmitTbData->uid;
14,197,329✔
1193
  pReader->lastBlkUid = uid;
14,197,614✔
1194

1195
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
14,197,993✔
1196
  taosMemoryFreeClear(pReader->extSchema);
14,196,605✔
1197
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
14,195,075✔
1198
  if (pReader->pSchemaWrapper == NULL) {
14,193,930✔
1199
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
35,856✔
1200
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1201
    pReader->cachedSchemaSuid = 0;
35,856✔
1202
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
35,856✔
1203
  }
1204

1205
  if (pSubmitTbData->pCreateTbReq != NULL) {
14,151,163✔
1206
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
450✔
1207
    if (code != 0) {
450✔
1208
      return code;
×
1209
    }
1210
  } else if (rawList != NULL) {
14,156,257✔
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,156,707✔
1219
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
5,472✔
1220
  } else {
1221
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
14,143,155✔
1222
  }
1223
}
1224

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

1233
int32_t tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
111,670✔
1234
  if (pReader == NULL || tbUidList == NULL) {
111,670✔
1235
    return TSDB_CODE_SUCCESS;
×
1236
  }
1237
  if (pReader->tbIdHash) {
111,742✔
1238
    taosHashClear(pReader->tbIdHash);
1,064✔
1239
  } else {
1240
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
110,678✔
1241
    if (pReader->tbIdHash == NULL) {
110,830✔
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,748,108✔
1248
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
2,637,153✔
1249
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
2,636,955✔
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));
111,894✔
1256
  return TSDB_CODE_SUCCESS;
111,894✔
1257
}
1258

1259
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
48,843✔
1260
  if (pReader == NULL || pTableUidList == NULL) {
48,843✔
1261
    return;
×
1262
  }
1263
  if (pReader->tbIdHash == NULL) {
48,843✔
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);
48,843✔
1272
  for (int i = 0; i < numOfTables; i++) {
76,691✔
1273
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
27,848✔
1274
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
27,848✔
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) {
586✔
1296
  if (pReader == NULL || tbUidList == NULL) {
586✔
1297
    return;
×
1298
  }
1299
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
760✔
1300
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
174✔
1301
    if (pKey && taosHashRemove(pReader->tbIdHash, pKey, sizeof(int64_t)) != 0) {
174✔
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) {
62,790,636✔
1308
  if (pTq == NULL) {
62,790,636✔
1309
    return 0;  // mounted vnode may have no tq
×
1310
  }
1311
  if (tbUidList == NULL) {
62,790,636✔
1312
    return TSDB_CODE_INVALID_PARA;
×
1313
  }
1314
  void*   pIter = NULL;
62,790,636✔
1315
  int32_t vgId = TD_VID(pTq->pVnode);
62,790,636✔
1316

1317
  // update the table list for each consumer handle
1318
  taosWLockLatch(&pTq->lock);
62,792,123✔
1319
  while (1) {
184,795✔
1320
    pIter = taosHashIterate(pTq->pHandle, pIter);
62,975,936✔
1321
    if (pIter == NULL) {
62,975,936✔
1322
      break;
62,791,141✔
1323
    }
1324

1325
    STqHandle* pTqHandle = (STqHandle*)pIter;
184,795✔
1326
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
184,795✔
1327
      int32_t code = qUpdateTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList, isAdd);
49,429✔
1328
      if (code != 0) {
49,429✔
1329
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1330
        continue;
×
1331
      }
1332
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
135,366✔
1333
      if (!isAdd) {
134,302✔
1334
        int32_t sz = taosArrayGetSize(tbUidList);
44,966✔
1335
        for (int32_t i = 0; i < sz; i++) {
44,966✔
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,064✔
1345
      if (isAdd) {
1,064✔
1346
        SArray* list = NULL;
1,064✔
1347
        int     ret = qGetTableList(pTqHandle->execHandle.execTb.suid, pTq->pVnode, pTqHandle->execHandle.execTb.node,
1,064✔
1348
                                    &list, pTqHandle->execHandle.task);
1349
        if (ret == 0) {
1,064✔
1350
          ret = tqReaderSetTbUidList(pTqHandle->execHandle.pTqReader, list, NULL);
1,064✔
1351
        }                            
1352
        if (ret != TDB_CODE_SUCCESS) {
1,064✔
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,064✔
1362
      } else {
1363
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
×
1364
      }
1365
    }
1366
  }
1367
  taosWUnLockLatch(&pTq->lock);
62,791,141✔
1368
  return 0;
62,791,632✔
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) {
107,075✔
1392
  int32_t            code = TSDB_CODE_SUCCESS;
107,075✔
1393
  int32_t            lino = 0;
107,075✔
1394
  SVTSourceScanInfo* pScanInfo = NULL;
107,075✔
1395
  SHashObj*          pVirtualTables = NULL;
107,075✔
1396
  SHashObj*          pPhysicalTables = NULL;
107,075✔
1397
  void*              pIter = NULL;
107,075✔
1398
  void*              px = NULL;
107,075✔
1399

1400
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
107,075✔
1401

1402
  pScanInfo = &pReader->vtSourceScanInfo;
107,075✔
1403
  taosHashCleanup(pScanInfo->pPhysicalTables);
107,035✔
1404
  pScanInfo->pPhysicalTables = NULL;
107,075✔
1405
  taosLRUCacheCleanup(pScanInfo->pPhyTblSchemaCache);
107,075✔
1406
  pScanInfo->pPhyTblSchemaCache = NULL;
107,035✔
1407
  pScanInfo->nextVirtualTableIdx = -1;
107,035✔
1408
  pScanInfo->metaFetch = 0;
107,075✔
1409
  pScanInfo->cacheHit = 0;
107,075✔
1410

1411
  pVirtualTables = pScanInfo->pVirtualTables;
107,075✔
1412
  if (taosHashGetSize(pVirtualTables) == 0 || taosArrayGetSize(pReader->pColIdList) == 0) {
107,035✔
1413
    goto _end;
107,035✔
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) {
107,035✔
UNCOV
1471
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1472
  }
1473
  if (pIter != NULL) {
107,035✔
UNCOV
1474
    taosHashCancelIterate(pReader->tbIdHash, pIter);
×
1475
  }
1476
  if (pPhysicalTables != NULL) {
107,035✔
UNCOV
1477
    taosHashCleanup(pPhysicalTables);
×
1478
  }
1479
  return code;
107,035✔
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