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

taosdata / TDengine / #4884

14 Dec 2025 03:48AM UTC coverage: 60.617% (-4.1%) from 64.74%
#4884

push

travis-ci

web-flow
test: update coverage workflow time (#33918)

156854 of 258761 relevant lines covered (60.62%)

75258957.81 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

254
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
3,169,578✔
255
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
3,169,578✔
256
    return -1;
×
257
  }
258
  int32_t code = -1;
3,186,980✔
259
  int32_t vgId = TD_VID(pTq->pVnode);
3,186,980✔
260
  int64_t id = pHandle->pWalReader->readerId;
3,185,398✔
261

262
  int64_t offset = *fetchOffset;
3,186,528✔
263
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
3,188,110✔
264
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
3,187,658✔
265
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
3,187,658✔
266

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

271
  while (offset <= appliedVer) {
3,193,215✔
272
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
3,190,199✔
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,
3,190,199✔
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) {
3,190,199✔
283
      code = walFetchBody(pHandle->pWalReader);
3,185,772✔
284
      goto END;
3,185,772✔
285
    } else {
286
      if (pHandle->fetchMeta != WITH_DATA) {
4,427✔
287
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
×
288
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
×
289
          code = walFetchBody(pHandle->pWalReader);
×
290
          if (code < 0) {
×
291
            goto END;
×
292
          }
293

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

314
END:
3,016✔
315
  *fetchOffset = offset;
3,188,788✔
316
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
3,188,788✔
317
          ", applied:%" PRId64 ", 0x%" PRIx64,
318
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
319
  return code;
3,188,788✔
320
}
321

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

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

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

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

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

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

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

373
  return pReader;
32,149✔
374
}
375

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

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

385
  if (pReader->pSchemaWrapper) {
32,149✔
386
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
21,180✔
387
  }
388

389
  taosMemoryFree(pReader->extSchema);
32,149✔
390
  if (pReader->pColIdList) {
32,149✔
391
    taosArrayDestroy(pReader->pColIdList);
31,619✔
392
  }
393

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

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

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

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

422
  int64_t st = taosGetTimestampMs();
10,772,018✔
423
  while (1) {
10,694,570✔
424
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
21,466,588✔
425
    while (pReader->nextBlk < numOfBlocks) {
21,464,666✔
426
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
10,694,036✔
427
              pReader->msg.ver);
428

429
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
10,694,036✔
430
      if (pSubmitTbData == NULL) {
10,692,434✔
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;
×
434
      }
435
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
10,692,434✔
436
        pReader->nextBlk += 1;
×
437
        continue;
×
438
      }
439
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
10,691,900✔
440
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
10,694,890✔
441
        SSDataBlock* pRes = NULL;
10,694,890✔
442
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
10,690,084✔
443
        if (code == TSDB_CODE_SUCCESS) {
10,693,288✔
444
          return true;
10,693,288✔
445
        }
446
      } else {
447
        pReader->nextBlk += 1;
214✔
448
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
214✔
449
      }
450
    }
451

452
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
10,772,232✔
453
    pReader->msg.msgStr = NULL;
10,772,232✔
454

455
    int64_t elapsed = taosGetTimestampMs() - st;
10,772,232✔
456
    if (elapsed > 1000 || elapsed < 0) {
10,772,232✔
457
      return false;
×
458
    }
459

460
    // try next message in wal file
461
    if (walNextValidMsg(pWalReader, false) < 0) {
10,772,232✔
462
      return false;
77,662✔
463
    }
464

465
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
10,694,570✔
466
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
10,694,570✔
467
    int64_t ver = pWalReader->pHead->head.version;
10,694,570✔
468
    SDecoder decoder = {0};
10,694,036✔
469
    if (tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL, &decoder) != 0) {
10,694,036✔
470
      tDecoderClear(&decoder);
×
471
      return false;
×
472
    }
473
    tDecoderClear(&decoder);
10,693,502✔
474
    pReader->nextBlk = 0;
10,694,036✔
475
  }
476
}
477

478
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList, SDecoder* decoder) {
13,880,342✔
479
  if (pReader == NULL) {
13,880,342✔
480
    return TSDB_CODE_INVALID_PARA;
×
481
  }
482
  pReader->msg.msgStr = msgStr;
13,880,342✔
483
  pReader->msg.msgLen = msgLen;
13,880,342✔
484
  pReader->msg.ver = ver;
13,879,808✔
485

486
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
13,880,117✔
487

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

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

495
  return code;
13,878,596✔
496
}
497

498
void tqReaderClearSubmitMsg(STqReader* pReader) {
6,349,396✔
499
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
6,349,396✔
500
  pReader->nextBlk = 0;
6,351,882✔
501
  pReader->msg.msgStr = NULL;
6,357,532✔
502
}
6,364,086✔
503

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

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

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

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

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

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

546
  tqReaderClearSubmitMsg(pReader);
×
547
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
×
548

549
END:
×
550
  tqTrace("%s:%d return:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
×
551
  return code;
×
552
}
553

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

559
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
6,346,910✔
560
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
6,346,910✔
561
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
6,366,120✔
562

563
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
6,366,120✔
564
  while (pReader->nextBlk < blockSz) {
6,366,346✔
565
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
3,184,190✔
566
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
3,185,320✔
567
    uid = pSubmitTbData->uid;
3,185,320✔
568
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
3,185,320✔
569
    TSDB_CHECK_NULL(ret, code, lino, END, true);
3,184,868✔
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);
3,183,060✔
574
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
3,185,320✔
575

576
END:
3,185,320✔
577
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
6,370,188✔
578
  return code;
6,345,102✔
579
}
580

581
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask,
3,171,986✔
582
                    SExtSchema* extSrc) {
583
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
3,171,986✔
584
    return TSDB_CODE_INVALID_PARA;
×
585
  }
586
  int32_t code = 0;
3,178,540✔
587

588
  int32_t cnt = 0;
3,178,540✔
589
  for (int32_t i = 0; i < pSrc->nCols; i++) {
9,540,066✔
590
    cnt += mask[i];
6,360,396✔
591
  }
592

593
  pDst->nCols = cnt;
3,184,416✔
594
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
3,184,868✔
595
  if (pDst->pSchema == NULL) {
3,179,218✔
596
    return TAOS_GET_TERRNO(terrno);
×
597
  }
598

599
  int32_t j = 0;
3,175,150✔
600
  for (int32_t i = 0; i < pSrc->nCols; i++) {
9,545,490✔
601
    if (mask[i]) {
6,354,972✔
602
      pDst->pSchema[j++] = pSrc->pSchema[i];
6,369,662✔
603
      SColumnInfoData colInfo =
6,363,786✔
604
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
6,361,752✔
605
      if (extSrc != NULL) {
6,358,362✔
606
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
×
607
      }
608
      code = blockDataAppendColInfo(pBlock, &colInfo);
6,358,362✔
609
      if (code != 0) {
6,366,724✔
610
        return code;
×
611
      }
612
    }
613
  }
614
  return 0;
3,185,320✔
615
}
616

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

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

634
  int32_t numOfCols = taosArrayGetSize(pColIdList);
20,709✔
635

636
  if (numOfCols == 0) {  // all columns are required
20,709✔
637
    for (int32_t i = 0; i < pSchema->nCols; ++i) {
×
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) {
20,709✔
652
      numOfCols = pSchema->nCols;
×
653
    }
654

655
    int32_t i = 0;
20,709✔
656
    int32_t j = 0;
20,709✔
657
    while (i < pSchema->nCols && j < numOfCols) {
355,236✔
658
      SSchema* pColSchema = &pSchema->pSchema[i];
334,527✔
659
      col_id_t colIdSchema = pColSchema->colId;
334,527✔
660

661
      col_id_t* pColIdNeed = (col_id_t*)taosArrayGet(pColIdList, j);
334,527✔
662
      if (pColIdNeed == NULL) {
334,527✔
663
        break;
×
664
      }
665
      if (colIdSchema < *pColIdNeed) {
334,527✔
666
        i++;
32,040✔
667
      } else if (colIdSchema > *pColIdNeed) {
302,487✔
668
        j++;
×
669
      } else {
670
        SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
302,487✔
671
        if (IS_DECIMAL_TYPE(pColSchema->type) && pReader->extSchema != NULL) {
302,487✔
672
          decimalFromTypeMod(pReader->extSchema[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
27,820✔
673
        }
674
        int32_t code = blockDataAppendColInfo(pBlock, &colInfo);
302,487✔
675
        if (code != TSDB_CODE_SUCCESS) {
302,487✔
676
          return -1;
×
677
        }
678
        i++;
302,487✔
679
        j++;
302,487✔
680
      }
681
    }
682
  }
683

684
  return TSDB_CODE_SUCCESS;
20,709✔
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) {
1,918,108,374✔
729
  int32_t code = TSDB_CODE_SUCCESS;
1,918,108,374✔
730

731
  if (IS_VAR_DATA_TYPE(pColVal->value.type)) {
1,918,108,374✔
732
    if (COL_VAL_IS_VALUE(pColVal)) {
5,746,193✔
733
      char val[65535 + 2] = {0};
12,583,050✔
734
      if (pColVal->value.pData != NULL) {
12,581,982✔
735
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
12,583,050✔
736
      }
737
      varDataSetLen(val, pColVal->value.nData);
12,583,050✔
738
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
12,583,050✔
739
    } else {
740
      colDataSetNULL(pColumnInfoData, rowIndex);
×
741
    }
742
  } else {
743
    code = colDataSetVal(pColumnInfoData, rowIndex, VALUE_GET_DATUM(&pColVal->value, pColVal->value.type),
1,997,061,564✔
744
                         !COL_VAL_IS_VALUE(pColVal));
1,994,546,645✔
745
  }
746

747
  return code;
1,968,289,359✔
748
}
749

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

763
  blockDataCleanup(pBlock);
10,694,356✔
764

765
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
10,694,356✔
766
  int32_t sversion = pSubmitTbData->sver;
10,694,356✔
767
  int64_t suid = pSubmitTbData->suid;
10,694,356✔
768
  int64_t uid = pSubmitTbData->uid;
10,694,356✔
769
  pReader->lastTs = pSubmitTbData->ctimeMs;
10,694,356✔
770

771
  pBlock->info.id.uid = uid;
10,694,356✔
772
  pBlock->info.version = pReader->msg.ver;
10,694,356✔
773

774
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
10,694,356✔
775
      (pReader->cachedSchemaVer != sversion)) {
10,673,647✔
776
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
21,243✔
777
    taosMemoryFree(pReader->extSchema);
20,709✔
778
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
20,709✔
779
    if (pReader->pSchemaWrapper == NULL) {
20,709✔
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;
20,709✔
788
    pReader->cachedSchemaSuid = suid;
20,709✔
789
    pReader->cachedSchemaVer = sversion;
20,709✔
790

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

802
  int32_t numOfRows = 0;
10,694,356✔
803
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
10,694,356✔
804
    SColData* pCol = taosArrayGet(pSubmitTbData->aCol, 0);
×
805
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
×
806
    numOfRows = pCol->nVal;
×
807
  } else {
808
    numOfRows = taosArrayGetSize(pSubmitTbData->aRowP);
10,693,822✔
809
  }
810

811
  code = blockDataEnsureCapacity(pBlock, numOfRows);
10,694,356✔
812
  TSDB_CHECK_CODE(code, line, END);
10,693,822✔
813
  pBlock->info.rows = numOfRows;
10,693,822✔
814
  int32_t colActual = blockDataGetNumOfCols(pBlock);
10,693,822✔
815

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

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

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

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

866
    for (int32_t i = 0; i < numOfRows; i++) {
552,955,385✔
867
      SRow* pRow = taosArrayGetP(pRows, i);
500,614,213✔
868
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
497,706,856✔
869
      int32_t sourceIdx = 0;
499,989,856✔
870
      for (int32_t j = 0; j < colActual; j++) {
2,147,483,647✔
871
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, j);
1,922,925,115✔
872
        TSDB_CHECK_NULL(pColData, code, line, END, terrno);
1,921,613,667✔
873

874
        uint8_t isBlob = IS_STR_DATA_BLOB(pColData->info.type) ? 1 : 0;
1,921,613,667✔
875
        while (1) {
99,313,320✔
876
          SColVal colVal = {0};
2,037,635,860✔
877
          code = tRowGet(pRow, pTSchema, sourceIdx, &colVal);
2,048,359,372✔
878
          TSDB_CHECK_CODE(code, line, END);
2,029,941,735✔
879

880
          if (colVal.cid < pColData->info.colId) {
2,029,941,735✔
881
            sourceIdx++;
99,312,786✔
882
            continue;
99,312,786✔
883
          } else if (colVal.cid == pColData->info.colId) {
1,963,075,581✔
884
            if (isBlob == 0) {
1,968,400,956✔
885
              code = doSetVal(pColData, i, &colVal);
1,968,400,956✔
886
            } else {
887
              code = doSetBlobVal(pColData, i, &colVal, pSubmitTbData->pBlobSet);
×
888
            }
889

890
            TSDB_CHECK_CODE(code, line, END);
1,936,687,138✔
891

892
            sourceIdx++;
1,936,687,138✔
893
            break;
1,936,687,138✔
894
          } else {
895
            colDataSetNULL(pColData, i);
×
896
            break;
×
897
          }
898
        }
899
      }
900
    }
901
  }
902

903
END:
52,341,172✔
904
  if (code != 0) {
33,685,375✔
905
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
906
  }
907
  taosMemoryFreeClear(pTSchema);
10,693,822✔
908
  return code;
10,693,822✔
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,
3,168,370✔
940
                               char* assigned, int32_t numOfRows, int32_t curRow, int32_t* lastRow) {
941
  int32_t         code = 0;
3,168,370✔
942
  SSchemaWrapper* pSW = NULL;
3,168,370✔
943
  SSDataBlock*    block = NULL;
3,173,794✔
944
  if (taosArrayGetSize(blocks) > 0) {
3,173,794✔
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));
3,182,156✔
952
  TQ_NULL_GO_TO_END(block);
3,173,794✔
953

954
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
3,173,794✔
955
  TQ_NULL_GO_TO_END(pSW);
3,170,404✔
956

957
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pReader->pSchemaWrapper, assigned, pReader->extSchema));
3,170,404✔
958
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
3,185,094✔
959
          (int32_t)taosArrayGetSize(block->pDataBlock));
960

961
  block->info.id.uid = pSubmitTbData->uid;
3,185,094✔
962
  block->info.version = pReader->msg.ver;
3,184,416✔
963
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
3,184,868✔
964
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
3,177,410✔
965
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
3,181,026✔
966
  pSW = NULL;
3,181,026✔
967

968
  taosMemoryFreeClear(block);
3,181,026✔
969

970
END:
3,184,868✔
971
  if (code != 0) {
3,181,930✔
972
    tqError("processBuildNew failed, code:%d", code);
×
973
  }
974
  tDeleteSchemaWrapper(pSW);
3,181,930✔
975
  blockDataFreeRes(block);
3,172,890✔
976
  taosMemoryFree(block);
3,169,274✔
977
  return code;
3,183,512✔
978
}
979
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
×
980
  int32_t code = 0;
×
981
  int32_t curRow = 0;
×
982
  int32_t lastRow = 0;
×
983

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

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

998
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
×
999
      int32_t k = 0;
×
1000
      for (; k < numOfCols; k++) {
×
1001
        pCol = taosArrayGet(pCols, k);
×
1002
        TQ_NULL_GO_TO_END(pCol);
×
1003
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
×
1004
          SColVal colVal = {0};
×
1005
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
×
1006
          PROCESS_VAL
×
1007
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
×
1008
          break;
×
1009
        }
1010
      }
1011
      if (k >= numOfCols) {
×
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) {
×
1019
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
×
1020
    }
1021

1022
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
×
1023
    TQ_NULL_GO_TO_END(pBlock);
×
1024

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

1028
    int32_t targetIdx = 0;
×
1029
    int32_t sourceIdx = 0;
×
1030
    int32_t colActual = blockDataGetNumOfCols(pBlock);
×
1031
    while (targetIdx < colActual && sourceIdx < numOfCols) {
×
1032
      pCol = taosArrayGet(pCols, sourceIdx);
×
1033
      TQ_NULL_GO_TO_END(pCol);
×
1034
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
×
1035
      TQ_NULL_GO_TO_END(pColData);
×
1036
      SColVal colVal = {0};
×
1037
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
×
1038
      SET_DATA
×
1039
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
×
1040
    }
1041

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

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

1060
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
3,171,534✔
1061
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
3,174,924✔
1062
  TQ_NULL_GO_TO_END(assigned);
3,172,212✔
1063

1064
  int32_t curRow = 0;
3,172,212✔
1065
  int32_t lastRow = 0;
3,172,212✔
1066
  SArray* pRows = pSubmitTbData->aRowP;
3,167,918✔
1067
  int32_t numOfRows = taosArrayGetSize(pRows);
3,184,190✔
1068
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
3,182,834✔
1069
  TQ_NULL_GO_TO_END(pTSchema);
3,173,116✔
1070
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
3,173,116✔
1071

1072
  for (int32_t i = 0; i < numOfRows; i++) {
6,349,848✔
1073
    bool  buildNew = false;
3,164,302✔
1074
    SRow* pRow = taosArrayGetP(pRows, i);
3,164,302✔
1075
    TQ_NULL_GO_TO_END(pRow);
3,183,512✔
1076

1077
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
9,535,546✔
1078
      SColVal colVal = {0};
6,363,334✔
1079
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
6,365,594✔
1080
      PROCESS_VAL
6,355,198✔
1081
      tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], pTSchema->numOfCols);
6,367,402✔
1082
    }
1083

1084
    if (buildNew) {
3,164,980✔
1085
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
3,185,094✔
1086
    }
1087

1088
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
3,162,042✔
1089
    TQ_NULL_GO_TO_END(pBlock);
3,176,506✔
1090

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

1094
    int32_t targetIdx = 0;
3,176,506✔
1095
    int32_t sourceIdx = 0;
3,176,506✔
1096
    int32_t colActual = blockDataGetNumOfCols(pBlock);
3,176,506✔
1097
    while (targetIdx < colActual && sourceIdx < pTSchema->numOfCols) {
9,544,360✔
1098
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
6,358,588✔
1099
      TQ_NULL_GO_TO_END(pColData);
6,359,718✔
1100
      SColVal          colVal = {0};
6,359,718✔
1101
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
6,354,068✔
1102
      SET_DATA
6,369,888✔
1103
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
6,365,368✔
1104
    }
1105

1106
    curRow++;
3,185,320✔
1107
  }
1108
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
3,185,546✔
1109
  if (pLastBlock != NULL) {
3,185,094✔
1110
    pLastBlock->info.rows = curRow - lastRow;
3,185,094✔
1111
  }
1112

1113
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
3,185,320✔
1114
          (int)taosArrayGetSize(blocks));
1115
END:
3,179,218✔
1116
  if (code != TSDB_CODE_SUCCESS) {
3,172,664✔
1117
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1118
  }
1119
  taosMemoryFreeClear(pTSchema);
3,168,370✔
1120
  taosMemoryFree(assigned);
3,172,212✔
1121
  return code;
3,177,862✔
1122
}
1123

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

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

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

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

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

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

1171
  if (pSubmitTbDataRet) {
3,182,382✔
1172
    *pSubmitTbDataRet = pSubmitTbData;
3,185,094✔
1173
  }
1174

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

1191
  int32_t sversion = pSubmitTbData->sver;
3,182,608✔
1192
  int64_t uid = pSubmitTbData->uid;
3,183,286✔
1193
  pReader->lastBlkUid = uid;
3,183,512✔
1194

1195
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
3,183,964✔
1196
  taosMemoryFreeClear(pReader->extSchema);
3,184,190✔
1197
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
3,185,094✔
1198
  if (pReader->pSchemaWrapper == NULL) {
3,179,670✔
1199
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
×
1200
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1201
    pReader->cachedSchemaSuid = 0;
×
1202
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
×
1203
  }
1204

1205
  if (pSubmitTbData->pCreateTbReq != NULL) {
3,170,856✔
1206
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
×
1207
    if (code != 0) {
×
1208
      return code;
×
1209
    }
1210
  } else if (rawList != NULL) {
3,181,930✔
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) {
3,181,930✔
1219
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
×
1220
  } else {
1221
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
3,173,794✔
1222
  }
1223
}
1224

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

1233
int32_t tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
31,619✔
1234
  if (pReader == NULL || tbUidList == NULL) {
31,619✔
1235
    return TSDB_CODE_SUCCESS;
×
1236
  }
1237
  if (pReader->tbIdHash) {
31,619✔
1238
    taosHashClear(pReader->tbIdHash);
×
1239
  } else {
1240
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
31,403✔
1241
    if (pReader->tbIdHash == NULL) {
31,619✔
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++) {
579,803✔
1248
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
548,184✔
1249
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
547,959✔
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));
31,619✔
1256
  return TSDB_CODE_SUCCESS;
31,619✔
1257
}
1258

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

1317
  // update the table list for each consumer handle
1318
  taosWLockLatch(&pTq->lock);
36,138,079✔
1319
  while (1) {
×
1320
    pIter = taosHashIterate(pTq->pHandle, pIter);
36,138,079✔
1321
    if (pIter == NULL) {
36,138,079✔
1322
      break;
36,138,079✔
1323
    }
1324

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

1400
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
31,619✔
1401

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1533
bool tqReaderIsQueriedSourceTable(STqReader* pReader, uint64_t uid) {
×
1534
  if (pReader == NULL) {
×
1535
    return false;
×
1536
  }
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