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

taosdata / TDengine / #4712

06 Sep 2025 04:27PM UTC coverage: 58.144% (-1.0%) from 59.134%
#4712

push

travis-ci

GitHub
test: update case description (#32878)

133123 of 291691 branches covered (45.64%)

Branch coverage included in aggregate %.

201244 of 283375 relevant lines covered (71.02%)

5637899.03 hits per line

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

49.03
/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
bool isValValidForTable(STqHandle* pHandle, SWalCont* pHead) {
545✔
22
  if (pHandle == NULL || pHead == NULL) {
545!
23
    return false;
×
24
  }
25
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
545!
26
    return true;
545✔
27
  }
28

29
  int16_t msgType = pHead->msgType;
×
30
  char*   body = pHead->body;
×
31
  int32_t bodyLen = pHead->bodyLen;
×
32

33
  int64_t  tbSuid = pHandle->execHandle.execTb.suid;
×
34
  int64_t  realTbSuid = 0;
×
35
  SDecoder dcoder = {0};
×
36
  void*    data = POINTER_SHIFT(body, sizeof(SMsgHead));
×
37
  int32_t  len = bodyLen - sizeof(SMsgHead);
×
38
  tDecoderInit(&dcoder, data, len);
×
39

40
  if (msgType == TDMT_VND_CREATE_STB || msgType == TDMT_VND_ALTER_STB) {
×
41
    SVCreateStbReq req = {0};
×
42
    if (tDecodeSVCreateStbReq(&dcoder, &req) < 0) {
×
43
      goto end;
×
44
    }
45
    realTbSuid = req.suid;
×
46
  } else if (msgType == TDMT_VND_DROP_STB) {
×
47
    SVDropStbReq req = {0};
×
48
    if (tDecodeSVDropStbReq(&dcoder, &req) < 0) {
×
49
      goto end;
×
50
    }
51
    realTbSuid = req.suid;
×
52
  } else if (msgType == TDMT_VND_CREATE_TABLE) {
×
53
    SVCreateTbBatchReq req = {0};
×
54
    if (tDecodeSVCreateTbBatchReq(&dcoder, &req) < 0) {
×
55
      goto end;
×
56
    }
57

58
    int32_t        needRebuild = 0;
×
59
    SVCreateTbReq* pCreateReq = NULL;
×
60
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
61
      pCreateReq = req.pReqs + iReq;
×
62
      if (pCreateReq->type == TSDB_CHILD_TABLE && pCreateReq->ctb.suid == tbSuid) {
×
63
        needRebuild++;
×
64
      }
65
    }
66
    if (needRebuild == 0) {
×
67
      // do nothing
68
    } else if (needRebuild == req.nReqs) {
×
69
      realTbSuid = tbSuid;
×
70
    } else {
71
      realTbSuid = tbSuid;
×
72
      SVCreateTbBatchReq reqNew = {0};
×
73
      reqNew.pArray = taosArrayInit(req.nReqs, sizeof(struct SVCreateTbReq));
×
74
      if (reqNew.pArray == NULL) {
×
75
        tDeleteSVCreateTbBatchReq(&req);
×
76
        goto end;
×
77
      }
78
      for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
79
        pCreateReq = req.pReqs + iReq;
×
80
        if (pCreateReq->type == TSDB_CHILD_TABLE && pCreateReq->ctb.suid == tbSuid) {
×
81
          reqNew.nReqs++;
×
82
          if (taosArrayPush(reqNew.pArray, pCreateReq) == NULL) {
×
83
            taosArrayDestroy(reqNew.pArray);
×
84
            tDeleteSVCreateTbBatchReq(&req);
×
85
            goto end;
×
86
          }
87
        }
88
      }
89

90
      int     tlen = 0;
×
91
      int32_t ret = 0;
×
92
      tEncodeSize(tEncodeSVCreateTbBatchReq, &reqNew, tlen, ret);
×
93
      void* buf = taosMemoryMalloc(tlen);
×
94
      if (NULL == buf) {
×
95
        taosArrayDestroy(reqNew.pArray);
×
96
        tDeleteSVCreateTbBatchReq(&req);
×
97
        goto end;
×
98
      }
99
      SEncoder coderNew = {0};
×
100
      tEncoderInit(&coderNew, buf, tlen - sizeof(SMsgHead));
×
101
      ret = tEncodeSVCreateTbBatchReq(&coderNew, &reqNew);
×
102
      tEncoderClear(&coderNew);
×
103
      if (ret < 0) {
×
104
        taosMemoryFree(buf);
×
105
        taosArrayDestroy(reqNew.pArray);
×
106
        tDeleteSVCreateTbBatchReq(&req);
×
107
        goto end;
×
108
      }
109
      (void)memcpy(pHead->body + sizeof(SMsgHead), buf, tlen);
×
110
      pHead->bodyLen = tlen + sizeof(SMsgHead);
×
111
      taosMemoryFree(buf);
×
112
      taosArrayDestroy(reqNew.pArray);
×
113
    }
114

115
    tDeleteSVCreateTbBatchReq(&req);
×
116
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
×
117
    SVAlterTbReq req = {0};
×
118

119
    if (tDecodeSVAlterTbReq(&dcoder, &req) < 0) {
×
120
      goto end;
×
121
    }
122

123
    SMetaReader mr = {0};
×
124
    metaReaderDoInit(&mr, pHandle->execHandle.pTqReader->pVnodeMeta, META_READER_LOCK);
×
125

126
    if (metaGetTableEntryByName(&mr, req.tbName) < 0) {
×
127
      metaReaderClear(&mr);
×
128
      goto end;
×
129
    }
130
    realTbSuid = mr.me.ctbEntry.suid;
×
131
    metaReaderClear(&mr);
×
132
  } else if (msgType == TDMT_VND_DROP_TABLE) {
×
133
    SVDropTbBatchReq req = {0};
×
134

135
    if (tDecodeSVDropTbBatchReq(&dcoder, &req) < 0) {
×
136
      goto end;
×
137
    }
138

139
    int32_t      needRebuild = 0;
×
140
    SVDropTbReq* pDropReq = NULL;
×
141
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
142
      pDropReq = req.pReqs + iReq;
×
143

144
      if (pDropReq->suid == tbSuid) {
×
145
        needRebuild++;
×
146
      }
147
    }
148
    if (needRebuild == 0) {
×
149
      // do nothing
150
    } else if (needRebuild == req.nReqs) {
×
151
      realTbSuid = tbSuid;
×
152
    } else {
153
      realTbSuid = tbSuid;
×
154
      SVDropTbBatchReq reqNew = {0};
×
155
      reqNew.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbReq));
×
156
      if (reqNew.pArray == NULL) {
×
157
        goto end;
×
158
      }
159
      for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
160
        pDropReq = req.pReqs + iReq;
×
161
        if (pDropReq->suid == tbSuid) {
×
162
          reqNew.nReqs++;
×
163
          if (taosArrayPush(reqNew.pArray, pDropReq) == NULL) {
×
164
            taosArrayDestroy(reqNew.pArray);
×
165
            goto end;
×
166
          }
167
        }
168
      }
169

170
      int     tlen = 0;
×
171
      int32_t ret = 0;
×
172
      tEncodeSize(tEncodeSVDropTbBatchReq, &reqNew, tlen, ret);
×
173
      void* buf = taosMemoryMalloc(tlen);
×
174
      if (NULL == buf) {
×
175
        taosArrayDestroy(reqNew.pArray);
×
176
        goto end;
×
177
      }
178
      SEncoder coderNew = {0};
×
179
      tEncoderInit(&coderNew, buf, tlen - sizeof(SMsgHead));
×
180
      ret = tEncodeSVDropTbBatchReq(&coderNew, &reqNew);
×
181
      tEncoderClear(&coderNew);
×
182
      if (ret != 0) {
×
183
        taosMemoryFree(buf);
×
184
        taosArrayDestroy(reqNew.pArray);
×
185
        goto end;
×
186
      }
187
      (void)memcpy(pHead->body + sizeof(SMsgHead), buf, tlen);
×
188
      pHead->bodyLen = tlen + sizeof(SMsgHead);
×
189
      taosMemoryFree(buf);
×
190
      taosArrayDestroy(reqNew.pArray);
×
191
    }
192
  } else if (msgType == TDMT_VND_DELETE) {
×
193
    SDeleteRes req = {0};
×
194
    if (tDecodeDeleteRes(&dcoder, &req) < 0) {
×
195
      goto end;
×
196
    }
197
    realTbSuid = req.suid;
×
198
  }
199

200
end:
×
201
  tDecoderClear(&dcoder);
×
202
  bool tmp = tbSuid == realTbSuid;
×
203
  tqDebug("%s suid:%" PRId64 " realSuid:%" PRId64 " return:%d", __FUNCTION__, tbSuid, realTbSuid, tmp);
×
204
  return tmp;
×
205
}
206

207
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
143,889✔
208
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
143,889!
209
    return -1;
×
210
  }
211
  int32_t code = -1;
144,112✔
212
  int32_t vgId = TD_VID(pTq->pVnode);
144,112✔
213
  int64_t id = pHandle->pWalReader->readerId;
144,112✔
214

215
  int64_t offset = *fetchOffset;
144,112✔
216
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
144,112✔
217
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
144,022✔
218
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
144,117✔
219

220
  tqDebug("vgId:%d, start to fetch wal, index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64 ", applied:%" PRId64
144,164!
221
          ", 0x%" PRIx64,
222
          vgId, offset, lastVer, committedVer, appliedVer, id);
223

224
  while (offset <= appliedVer) {
153,250✔
225
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
143,482!
226
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", (epoch %d) vgId:%d offset %" PRId64
×
227
              ", no more log to return, QID:0x%" PRIx64 " 0x%" PRIx64,
228
              pHandle->consumerId, pHandle->epoch, vgId, offset, reqId, id);
229
      goto END;
×
230
    }
231

232
    tqDebug("vgId:%d, consumer:0x%" PRIx64 " taosx get msg ver %" PRId64 ", type:%s, QID:0x%" PRIx64 " 0x%" PRIx64,
143,477!
233
            vgId, pHandle->consumerId, offset, TMSG_INFO(pHandle->pWalReader->pHead->head.msgType), reqId, id);
234

235
    if (pHandle->pWalReader->pHead->head.msgType == TDMT_VND_SUBMIT) {
143,480✔
236
      code = walFetchBody(pHandle->pWalReader);
134,012✔
237
      goto END;
133,994✔
238
    } else {
239
      if (pHandle->fetchMeta != WITH_DATA) {
9,468✔
240
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
671✔
241
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
671✔
242
          code = walFetchBody(pHandle->pWalReader);
545✔
243
          if (code < 0) {
545!
244
            goto END;
×
245
          }
246

247
          pHead = &(pHandle->pWalReader->pHead->head);
545✔
248
          if (isValValidForTable(pHandle, pHead)) {
545!
249
            code = 0;
545✔
250
            goto END;
545✔
251
          } else {
252
            offset++;
×
253
            code = -1;
×
254
            continue;
×
255
          }
256
        }
257
      }
258
      code = walSkipFetchBody(pHandle->pWalReader);
8,923✔
259
      if (code < 0) {
8,925!
260
        goto END;
×
261
      }
262
      offset++;
8,925✔
263
    }
264
    code = -1;
8,925✔
265
  }
266

267
END:
9,768✔
268
  *fetchOffset = offset;
144,307✔
269
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
144,307✔
270
          ", applied:%" PRId64 ", 0x%" PRIx64,
271
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
272
  return code;
144,318✔
273
}
274

275
bool tqGetTablePrimaryKey(STqReader* pReader) {
19,277✔
276
  if (pReader == NULL) {
19,277!
277
    return false;
×
278
  }
279
  return pReader->hasPrimaryKey;
19,277✔
280
}
281

282
void tqSetTablePrimaryKey(STqReader* pReader, int64_t uid) {
143✔
283
  tqDebug("%s:%p uid:%" PRId64, __FUNCTION__, pReader, uid);
143!
284

285
  if (pReader == NULL) {
143!
286
    return;
×
287
  }
288
  bool            ret = false;
143✔
289
  SSchemaWrapper* schema = metaGetTableSchema(pReader->pVnodeMeta, uid, -1, 1, NULL);
143✔
290
  if (schema && schema->nCols >= 2 && schema->pSchema[1].flags & COL_IS_KEY) {
143!
291
    ret = true;
×
292
  }
293
  tDeleteSchemaWrapper(schema);
294
  pReader->hasPrimaryKey = ret;
143✔
295
}
296

297
STqReader* tqReaderOpen(SVnode* pVnode) {
1,327✔
298
  tqDebug("%s:%p", __FUNCTION__, pVnode);
1,327!
299
  if (pVnode == NULL) {
1,333!
300
    return NULL;
×
301
  }
302
  STqReader* pReader = taosMemoryCalloc(1, sizeof(STqReader));
1,333!
303
  if (pReader == NULL) {
1,333!
304
    return NULL;
×
305
  }
306

307
  pReader->pWalReader = walOpenReader(pVnode->pWal, 0);
1,333✔
308
  if (pReader->pWalReader == NULL) {
1,333!
309
    taosMemoryFree(pReader);
×
310
    return NULL;
×
311
  }
312

313
  pReader->pVnodeMeta = pVnode->pMeta;
1,333✔
314
  pReader->pColIdList = NULL;
1,333✔
315
  pReader->cachedSchemaVer = 0;
1,333✔
316
  pReader->cachedSchemaSuid = 0;
1,333✔
317
  pReader->pSchemaWrapper = NULL;
1,333✔
318
  pReader->tbIdHash = NULL;
1,333✔
319
  pReader->pResBlock = NULL;
1,333✔
320

321
  int32_t code = createDataBlock(&pReader->pResBlock);
1,333✔
322
  if (code) {
1,333!
323
    terrno = code;
×
324
  }
325

326
  return pReader;
1,333✔
327
}
328

329
void tqReaderClose(STqReader* pReader) {
1,350✔
330
  tqDebug("%s:%p", __FUNCTION__, pReader);
1,350!
331
  if (pReader == NULL) return;
1,350✔
332

333
  // close wal reader
334
  if (pReader->pWalReader) {
1,333!
335
    walCloseReader(pReader->pWalReader);
1,333✔
336
  }
337

338
  if (pReader->pSchemaWrapper) {
1,333✔
339
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
552!
340
  }
341

342
  taosMemoryFree(pReader->extSchema);
1,333!
343
  if (pReader->pColIdList) {
1,333✔
344
    taosArrayDestroy(pReader->pColIdList);
1,036✔
345
  }
346

347
  // free hash
348
  blockDataDestroy(pReader->pResBlock);
1,333✔
349
  taosHashCleanup(pReader->tbIdHash);
1,333✔
350
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
1,333✔
351

352
  taosHashCleanup(pReader->vtSourceScanInfo.pVirtualTables);
1,332✔
353
  taosHashCleanup(pReader->vtSourceScanInfo.pPhysicalTables);
1,332✔
354
  taosLRUCacheCleanup(pReader->vtSourceScanInfo.pPhyTblSchemaCache);
1,332✔
355
  taosMemoryFree(pReader);
1,332!
356
}
357

358
int32_t tqReaderSeek(STqReader* pReader, int64_t ver, const char* id) {
1,678✔
359
  if (pReader == NULL) {
1,678!
360
    return TSDB_CODE_INVALID_PARA;
×
361
  }
362
  if (walReaderSeekVer(pReader->pWalReader, ver) < 0) {
1,678✔
363
    return terrno;
107✔
364
  }
365
  tqDebug("wal reader seek to ver:%" PRId64 " %s", ver, id);
1,570!
366
  return 0;
1,571✔
367
}
368

369
bool tqNextBlockInWal(STqReader* pReader, const char* id, int sourceExcluded) {
265,460✔
370
  if (pReader == NULL) {
265,460!
371
    return false;
×
372
  }
373
  SWalReader* pWalReader = pReader->pWalReader;
265,460✔
374

375
  int64_t st = taosGetTimestampMs();
265,460✔
376
  while (1) {
286,094✔
377
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
551,554✔
378
    while (pReader->nextBlk < numOfBlocks) {
598,607✔
379
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
286,146!
380
              pReader->msg.ver);
381

382
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
286,146✔
383
      if (pSubmitTbData == NULL) {
286,141!
384
        tqError("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
×
385
                pReader->msg.ver);
386
        return false;
×
387
      }
388
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
286,143✔
389
        pReader->nextBlk += 1;
106✔
390
        continue;
106✔
391
      }
392
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
286,034!
393
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
239,094!
394
        SSDataBlock* pRes = NULL;
239,094✔
395
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
239,094✔
396
        if (code == TSDB_CODE_SUCCESS) {
239,088!
397
          return true;
239,091✔
398
        }
399
      } else {
400
        pReader->nextBlk += 1;
46,954✔
401
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
46,954!
402
      }
403
    }
404

405
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
312,461✔
406
    pReader->msg.msgStr = NULL;
312,462✔
407

408
    int64_t elapsed = taosGetTimestampMs() - st;
312,462✔
409
    if (elapsed > 1000 || elapsed < 0) {
312,462!
410
      return false;
×
411
    }
412

413
    // try next message in wal file
414
    if (walNextValidMsg(pWalReader, false) < 0) {
312,462✔
415
      return false;
26,354✔
416
    }
417

418
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
286,101✔
419
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
286,101✔
420
    int64_t ver = pWalReader->pHead->head.version;
286,101✔
421
    if (tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL) != 0) {
286,101!
422
      return false;
×
423
    }
424
    pReader->nextBlk = 0;
286,094✔
425
  }
426
}
427

428
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList) {
420,092✔
429
  if (pReader == NULL) {
420,092!
430
    return TSDB_CODE_INVALID_PARA;
×
431
  }
432
  pReader->msg.msgStr = msgStr;
420,092✔
433
  pReader->msg.msgLen = msgLen;
420,092✔
434
  pReader->msg.ver = ver;
420,092✔
435

436
  tqTrace("tq reader set msg pointer:%p, msg len:%d", msgStr, msgLen);
420,092!
437
  SDecoder decoder = {0};
420,092✔
438

439
  tDecoderInit(&decoder, pReader->msg.msgStr, pReader->msg.msgLen);
420,092✔
440
  int32_t code = tDecodeSubmitReq(&decoder, &pReader->submit, rawList);
420,071✔
441
  tDecoderClear(&decoder);
419,949✔
442

443
  if (code != 0) {
420,095!
444
    tqError("DecodeSSubmitReq2 error, msgLen:%d, ver:%" PRId64, msgLen, ver);
×
445
  }
446

447
  return code;
420,081✔
448
}
449

450
void tqReaderClearSubmitMsg(STqReader* pReader) {
267,497✔
451
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
267,497✔
452
  pReader->nextBlk = 0;
267,609✔
453
  pReader->msg.msgStr = NULL;
267,609✔
454
}
267,609✔
455

456
SWalReader* tqGetWalReader(STqReader* pReader) {
295,048✔
457
  if (pReader == NULL) {
295,048!
458
    return NULL;
×
459
  }
460
  return pReader->pWalReader;
295,048✔
461
}
462

463
SSDataBlock* tqGetResultBlock(STqReader* pReader) {
265,431✔
464
  if (pReader == NULL) {
265,431!
465
    return NULL;
×
466
  }
467
  return pReader->pResBlock;
265,431✔
468
}
469

470
int64_t tqGetResultBlockTime(STqReader* pReader) {
265,422✔
471
  if (pReader == NULL) {
265,422!
472
    return 0;
×
473
  }
474
  return pReader->lastTs;
265,422✔
475
}
476

477
bool tqNextBlockImpl(STqReader* pReader, const char* idstr) {
69,819✔
478
  int32_t code = false;
69,819✔
479
  int32_t lino = 0;
69,819✔
480
  int64_t uid = 0;
69,819✔
481
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
69,819!
482
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
69,819!
483
  TSDB_CHECK_NULL(pReader->tbIdHash, code, lino, END, true);
69,819!
484

485
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
69,819✔
486
  while (pReader->nextBlk < blockSz) {
73,336✔
487
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
36,692✔
488
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
36,692!
489
    uid = pSubmitTbData->uid;
36,692✔
490
    void* ret = taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t));
36,692✔
491
    TSDB_CHECK_CONDITION(ret == NULL, code, lino, END, true);
36,692✔
492

493
    tqTrace("iterator data block in hash continue, progress:%d/%d, total queried tables:%d, uid:%" PRId64,
3,514!
494
            pReader->nextBlk, blockSz, taosHashGetSize(pReader->tbIdHash), uid);
495
    pReader->nextBlk++;
3,514✔
496
  }
497

498
  tqReaderClearSubmitMsg(pReader);
36,644✔
499
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
36,665!
500

501
END:
36,665✔
502
  tqTrace("%s:%d return:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
69,843!
503
  return code;
69,850✔
504
}
505

506
bool tqNextDataBlockFilterOut(STqReader* pReader, SHashObj* filterOutUids) {
194,385✔
507
  int32_t code = false;
194,385✔
508
  int32_t lino = 0;
194,385✔
509
  int64_t uid = 0;
194,385✔
510

511
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
194,385!
512
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
194,385!
513
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
194,385!
514

515
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
194,385✔
516
  while (pReader->nextBlk < blockSz) {
194,383✔
517
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
97,380✔
518
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
97,379!
519
    uid = pSubmitTbData->uid;
97,379✔
520
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
97,379✔
521
    TSDB_CHECK_NULL(ret, code, lino, END, true);
97,387!
522
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64, pReader->nextBlk, blockSz, uid);
×
523
    pReader->nextBlk++;
×
524
  }
525
  tqReaderClearSubmitMsg(pReader);
97,003✔
526
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
97,281!
527

528
END:
97,281✔
529
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
194,669!
530
  return code;
194,485✔
531
}
532

533
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask,
130,268✔
534
                    SExtSchema* extSrc) {
535
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
130,268!
536
    return TSDB_CODE_INVALID_PARA;
×
537
  }
538
  int32_t code = 0;
130,355✔
539

540
  int32_t cnt = 0;
130,355✔
541
  for (int32_t i = 0; i < pSrc->nCols; i++) {
741,080✔
542
    cnt += mask[i];
610,725✔
543
  }
544

545
  pDst->nCols = cnt;
130,355✔
546
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
130,355!
547
  if (pDst->pSchema == NULL) {
130,291!
548
    return TAOS_GET_TERRNO(terrno);
×
549
  }
550

551
  int32_t j = 0;
130,291✔
552
  for (int32_t i = 0; i < pSrc->nCols; i++) {
740,180✔
553
    if (mask[i]) {
609,792✔
554
      pDst->pSchema[j++] = pSrc->pSchema[i];
609,763✔
555
      SColumnInfoData colInfo =
556
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
609,763✔
557
      if (extSrc != NULL) {
610,702✔
558
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
218✔
559
      }
560
      code = blockDataAppendColInfo(pBlock, &colInfo);
610,702✔
561
      if (code != 0) {
609,860!
562
        return code;
×
563
      }
564
    }
565
  }
566
  return 0;
130,388✔
567
}
568

569
static int32_t buildResSDataBlock(STqReader* pReader, SSchemaWrapper* pSchema, const SArray* pColIdList) {
412✔
570
  if (pReader == NULL || pSchema == NULL || pColIdList == NULL) {
412!
571
    return TSDB_CODE_INVALID_PARA;
×
572
  }
573
  SSDataBlock* pBlock = pReader->pResBlock;
412✔
574
  if (blockDataGetNumOfCols(pBlock) > 0) {
412✔
575
    blockDataDestroy(pBlock);
2✔
576
    int32_t code = createDataBlock(&pReader->pResBlock);
2✔
577
    if (code) {
2!
578
      return code;
×
579
    }
580
    pBlock = pReader->pResBlock;
2✔
581

582
    pBlock->info.id.uid = pReader->cachedSchemaUid;
2✔
583
    pBlock->info.version = pReader->msg.ver;
2✔
584
  }
585

586
  int32_t numOfCols = taosArrayGetSize(pColIdList);
412✔
587

588
  if (numOfCols == 0) {  // all columns are required
412!
589
    for (int32_t i = 0; i < pSchema->nCols; ++i) {
×
590
      SSchema*        pColSchema = &pSchema->pSchema[i];
×
591
      SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
×
592

593
      if (IS_DECIMAL_TYPE(pColSchema->type) && pReader->extSchema != NULL) {
×
594
        decimalFromTypeMod(pReader->extSchema[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
×
595
      }
596
      int32_t code = blockDataAppendColInfo(pBlock, &colInfo);
×
597
      if (code != TSDB_CODE_SUCCESS) {
×
598
        blockDataFreeRes(pBlock);
×
599
        return terrno;
×
600
      }
601
    }
602
  } else {
603
    if (numOfCols > pSchema->nCols) {
412✔
604
      numOfCols = pSchema->nCols;
2✔
605
    }
606

607
    int32_t i = 0;
412✔
608
    int32_t j = 0;
412✔
609
    while (i < pSchema->nCols && j < numOfCols) {
3,278✔
610
      SSchema* pColSchema = &pSchema->pSchema[i];
2,866✔
611
      col_id_t colIdSchema = pColSchema->colId;
2,866✔
612

613
      col_id_t* pColIdNeed = (col_id_t*)taosArrayGet(pColIdList, j);
2,866✔
614
      if (pColIdNeed == NULL) {
2,866!
615
        break;
×
616
      }
617
      if (colIdSchema < *pColIdNeed) {
2,866✔
618
        i++;
3✔
619
      } else if (colIdSchema > *pColIdNeed) {
2,863!
620
        j++;
×
621
      } else {
622
        SColumnInfoData colInfo = createColumnInfoData(pColSchema->type, pColSchema->bytes, pColSchema->colId);
2,863✔
623
        if (IS_DECIMAL_TYPE(pColSchema->type) && pReader->extSchema != NULL) {
2,863!
624
          decimalFromTypeMod(pReader->extSchema[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
132✔
625
        }
626
        int32_t code = blockDataAppendColInfo(pBlock, &colInfo);
2,863✔
627
        if (code != TSDB_CODE_SUCCESS) {
2,863!
628
          return -1;
×
629
        }
630
        i++;
2,863✔
631
        j++;
2,863✔
632
      }
633
    }
634
  }
635

636
  return TSDB_CODE_SUCCESS;
412✔
637
}
638

639
static int32_t doSetBlobVal(SColumnInfoData* pColumnInfoData, int32_t idx, SColVal* pColVal, SBlobSet* pBlobRow2) {
×
640
  int32_t code = 0;
×
641
  if (pColumnInfoData == NULL || pColVal == NULL || pBlobRow2 == NULL) {
×
642
    return TSDB_CODE_INVALID_PARA;
×
643
  }
644
  // TODO(yhDeng)
645
  if (COL_VAL_IS_VALUE(pColVal)) {
×
646
    char* val = taosMemCalloc(1, pColVal->value.nData + sizeof(BlobDataLenT));
×
647
    if (val == NULL) {
×
648
      return terrno;
×
649
    }
650

651
    uint64_t seq = 0;
×
652
    int32_t  len = 0;
×
653
    if (pColVal->value.pData != NULL) {
×
654
      tGetU64(pColVal->value.pData, &seq);
×
655
      SBlobItem item = {0};
×
656
      code = tBlobSetGet(pBlobRow2, seq, &item);
×
657
      if (code != 0) {
×
658
        taosMemoryFree(val);
×
659
        terrno = code;
×
660
        uError("tq set blob val, idx:%d, get blob item failed, seq:%" PRIu64 ", code:%d", idx, seq, code);
×
661
        return code;
×
662
      }
663

664
      val = taosMemRealloc(val, item.len + sizeof(BlobDataLenT));
×
665
      (void)memcpy(blobDataVal(val), item.data, item.len);
×
666
      len = item.len;
×
667
    }
668

669
    blobDataSetLen(val, len);
×
670
    code = colDataSetVal(pColumnInfoData, idx, val, false);
×
671

672
    taosMemoryFree(val);
×
673
  } else {
674
    colDataSetNULL(pColumnInfoData, idx);
×
675
  }
676
  return code;
×
677
}
678
static int32_t doSetVal(SColumnInfoData* pColumnInfoData, int32_t rowIndex, SColVal* pColVal) {
33,398,192✔
679
  int32_t code = TSDB_CODE_SUCCESS;
33,398,192✔
680

681
  if (IS_VAR_DATA_TYPE(pColVal->value.type)) {
33,398,192!
682
    if (COL_VAL_IS_VALUE(pColVal)) {
7,243,492!
683
      char val[65535 + 2] = {0};
7,613,336✔
684
      if (pColVal->value.pData != NULL) {
7,613,336!
685
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
7,707,221✔
686
      }
687
      varDataSetLen(val, pColVal->value.nData);
7,613,336✔
688
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
7,613,336✔
689
    } else {
690
      colDataSetNULL(pColumnInfoData, rowIndex);
×
691
    }
692
  } else {
693
    code = colDataSetVal(pColumnInfoData, rowIndex, VALUE_GET_DATUM(&pColVal->value, pColVal->value.type),
26,154,700!
694
                         !COL_VAL_IS_VALUE(pColVal));
26,154,700!
695
  }
696

697
  return code;
33,256,845✔
698
}
699

700
int32_t tqRetrieveDataBlock(STqReader* pReader, SSDataBlock** pRes, const char* id) {
239,071✔
701
  if (pReader == NULL || pRes == NULL) {
239,071!
702
    return TSDB_CODE_INVALID_PARA;
×
703
  }
704
  tqDebug("tq reader retrieve data block %p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
239,101!
705
  int32_t        code = 0;
239,101✔
706
  int32_t        line = 0;
239,101✔
707
  STSchema*      pTSchema = NULL;
239,101✔
708
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk++);
239,101✔
709
  TSDB_CHECK_NULL(pSubmitTbData, code, line, END, terrno);
239,100!
710
  SSDataBlock* pBlock = pReader->pResBlock;
239,100✔
711
  *pRes = pBlock;
239,100✔
712

713
  blockDataCleanup(pBlock);
239,100✔
714

715
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
239,061✔
716
  int32_t sversion = pSubmitTbData->sver;
239,061✔
717
  int64_t suid = pSubmitTbData->suid;
239,061✔
718
  int64_t uid = pSubmitTbData->uid;
239,061✔
719
  pReader->lastTs = pSubmitTbData->ctimeMs;
239,061✔
720

721
  pBlock->info.id.uid = uid;
239,061✔
722
  pBlock->info.version = pReader->msg.ver;
239,061✔
723

724
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
239,061✔
725
      (pReader->cachedSchemaVer != sversion)) {
238,652!
726
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
406✔
727
    taosMemoryFree(pReader->extSchema);
412!
728
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema);
412✔
729
    if (pReader->pSchemaWrapper == NULL) {
412!
730
      tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", uid:%" PRId64
×
731
             "version %d, possibly dropped table",
732
             vgId, suid, uid, pReader->cachedSchemaVer);
733
      pReader->cachedSchemaSuid = 0;
×
734
      return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
×
735
    }
736

737
    pReader->cachedSchemaUid = uid;
412✔
738
    pReader->cachedSchemaSuid = suid;
412✔
739
    pReader->cachedSchemaVer = sversion;
412✔
740

741
    if (pReader->cachedSchemaVer != pReader->pSchemaWrapper->version) {
412!
742
      tqError("vgId:%d, schema version mismatch, suid:%" PRId64 ", uid:%" PRId64 ", version:%d, cached version:%d",
×
743
              vgId, suid, uid, sversion, pReader->pSchemaWrapper->version);
744
      return TSDB_CODE_TQ_INTERNAL_ERROR;
×
745
    }
746
    code = buildResSDataBlock(pReader, pReader->pSchemaWrapper, pReader->pColIdList);
412✔
747
    TSDB_CHECK_CODE(code, line, END);
412!
748
    pBlock = pReader->pResBlock;
412✔
749
    *pRes = pBlock;
412✔
750
  }
751

752
  int32_t numOfRows = 0;
239,067✔
753
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
239,067✔
754
    SColData* pCol = taosArrayGet(pSubmitTbData->aCol, 0);
4✔
755
    TSDB_CHECK_NULL(pCol, code, line, END, terrno);
4!
756
    numOfRows = pCol->nVal;
4✔
757
  } else {
758
    numOfRows = taosArrayGetSize(pSubmitTbData->aRowP);
239,063✔
759
  }
760

761
  code = blockDataEnsureCapacity(pBlock, numOfRows);
239,066✔
762
  TSDB_CHECK_CODE(code, line, END);
239,065!
763
  pBlock->info.rows = numOfRows;
239,065✔
764
  int32_t colActual = blockDataGetNumOfCols(pBlock);
239,065✔
765

766
  // convert and scan one block
767
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
239,069✔
768
    SArray* pCols = pSubmitTbData->aCol;
4✔
769
    int32_t numOfCols = taosArrayGetSize(pCols);
4✔
770
    int32_t targetIdx = 0;
4✔
771
    int32_t sourceIdx = 0;
4✔
772
    while (targetIdx < colActual) {
18✔
773
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
14✔
774
      TSDB_CHECK_NULL(pColData, code, line, END, terrno);
14!
775
      if (sourceIdx >= numOfCols) {
14✔
776
        tqError("lostdata tqRetrieveDataBlock sourceIdx:%d >= numOfCols:%d", sourceIdx, numOfCols);
4!
777
        colDataSetNNULL(pColData, 0, numOfRows);
4!
778
        targetIdx++;
4✔
779
        continue;
4✔
780
      }
781

782
      uint8_t isBlob = IS_STR_DATA_BLOB(pColData->info.type) ? 1 : 0;
10!
783

784
      SColData* pCol = taosArrayGet(pCols, sourceIdx);
10✔
785
      TSDB_CHECK_NULL(pCol, code, line, END, terrno);
10!
786
      SColVal colVal = {0};
10✔
787
      tqTrace("lostdata colActual:%d, sourceIdx:%d, targetIdx:%d, numOfCols:%d, source cid:%d, dst cid:%d", colActual,
10!
788
              sourceIdx, targetIdx, numOfCols, pCol->cid, pColData->info.colId);
789
      if (pCol->cid < pColData->info.colId) {
10✔
790
        sourceIdx++;
4✔
791
      } else if (pCol->cid == pColData->info.colId) {
6✔
792
        for (int32_t i = 0; i < pCol->nVal; i++) {
12✔
793
          code = tColDataGetValue(pCol, i, &colVal);
8✔
794
          TSDB_CHECK_CODE(code, line, END);
8!
795

796
          if (isBlob == 0) {
8!
797
            code = doSetVal(pColData, i, &colVal);
8✔
798
          } else {
799
            code = doSetBlobVal(pColData, i, &colVal, pSubmitTbData->pBlobSet);
×
800
          }
801
          TSDB_CHECK_CODE(code, line, END);
8!
802
        }
803
        sourceIdx++;
4✔
804
        targetIdx++;
4✔
805
      } else {
806
        colDataSetNNULL(pColData, 0, numOfRows);
2!
807
        targetIdx++;
2✔
808
      }
809
    }
810
  } else {
811
    SArray*         pRows = pSubmitTbData->aRowP;
239,065✔
812
    SSchemaWrapper* pWrapper = pReader->pSchemaWrapper;
239,065✔
813
    pTSchema = tBuildTSchema(pWrapper->pSchema, pWrapper->nCols, pWrapper->version);
239,065✔
814
    TSDB_CHECK_NULL(pTSchema, code, line, END, terrno);
239,084!
815

816
    for (int32_t i = 0; i < numOfRows; i++) {
7,170,471✔
817
      SRow* pRow = taosArrayGetP(pRows, i);
6,662,432✔
818
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
6,623,232!
819
      int32_t sourceIdx = 0;
6,624,023✔
820
      for (int32_t j = 0; j < colActual; j++) {
29,439,313✔
821
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, j);
22,507,926✔
822
        TSDB_CHECK_NULL(pColData, code, line, END, terrno);
22,409,771!
823

824
        uint8_t isBlob = IS_STR_DATA_BLOB(pColData->info.type) ? 1 : 0;
22,428,176!
825
        while (1) {
×
826
          SColVal colVal = {0};
22,428,176✔
827
          code = tRowGet(pRow, pTSchema, sourceIdx, &colVal);
22,428,176✔
828
          TSDB_CHECK_CODE(code, line, END);
22,499,085!
829

830
          if (colVal.cid < pColData->info.colId) {
22,499,085!
831
            sourceIdx++;
×
832
            continue;
×
833
          } else if (colVal.cid == pColData->info.colId) {
22,499,085!
834
            if (isBlob == 0) {
22,552,094!
835
              code = doSetVal(pColData, i, &colVal);
22,563,766✔
836
            } else {
837
              code = doSetBlobVal(pColData, i, &colVal, pSubmitTbData->pBlobSet);
×
838
            }
839

840
            TSDB_CHECK_CODE(code, line, END);
22,868,299!
841

842
            sourceIdx++;
22,868,299✔
843
            break;
22,815,290✔
844
          } else {
845
            colDataSetNULL(pColData, i);
×
846
            break;
×
847
          }
848
        }
849
      }
850
    }
851
  }
852

853
END:
508,039✔
854
  if (code != 0) {
508,043!
855
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
856
  }
857
  taosMemoryFreeClear(pTSchema);
239,064!
858
  return code;
239,090✔
859
}
860

861
#define PROCESS_VAL                                      \
862
  if (curRow == 0) {                                     \
863
    assigned[j] = !COL_VAL_IS_NONE(&colVal);             \
864
    buildNew = true;                                     \
865
  } else {                                               \
866
    bool currentRowAssigned = !COL_VAL_IS_NONE(&colVal); \
867
    if (currentRowAssigned != assigned[j]) {             \
868
      assigned[j] = currentRowAssigned;                  \
869
      buildNew = true;                                   \
870
    }                                                    \
871
  }
872

873
#define SET_DATA                                                                                    \
874
  if (colVal.cid < pColData->info.colId) {                                                          \
875
    sourceIdx++;                                                                                    \
876
  } else if (colVal.cid == pColData->info.colId) {                                                  \
877
    if (IS_STR_DATA_BLOB(pColData->info.type)) {                                                    \
878
      TQ_ERR_GO_TO_END(doSetBlobVal(pColData, curRow - lastRow, &colVal, pSubmitTbData->pBlobSet)); \
879
    } else {                                                                                        \
880
      TQ_ERR_GO_TO_END(doSetVal(pColData, curRow - lastRow, &colVal));                              \
881
    }                                                                                               \
882
    sourceIdx++;                                                                                    \
883
    targetIdx++;                                                                                    \
884
  } else {                                                                                          \
885
    colDataSetNULL(pColData, curRow - lastRow);                                                     \
886
    targetIdx++;                                                                                    \
887
  }
888

889
static int32_t processBuildNew(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas,
130,205✔
890
                               char* assigned, int32_t numOfRows, int32_t curRow, int32_t* lastRow) {
891
  int32_t         code = 0;
130,205✔
892
  SSchemaWrapper* pSW = NULL;
130,205✔
893
  SSDataBlock*    block = NULL;
130,205✔
894
  if (taosArrayGetSize(blocks) > 0) {
130,205!
895
    SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
×
896
    TQ_NULL_GO_TO_END(pLastBlock);
×
897
    pLastBlock->info.rows = curRow - *lastRow;
×
898
    *lastRow = curRow;
×
899
  }
900

901
  block = taosMemoryCalloc(1, sizeof(SSDataBlock));
130,267!
902
  TQ_NULL_GO_TO_END(block);
130,361!
903

904
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
130,361!
905
  TQ_NULL_GO_TO_END(pSW);
130,380!
906

907
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pReader->pSchemaWrapper, assigned, pReader->extSchema));
130,380!
908
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
130,302!
909
          (int32_t)taosArrayGetSize(block->pDataBlock));
910

911
  block->info.id.uid = pSubmitTbData->uid;
130,302✔
912
  block->info.version = pReader->msg.ver;
130,302✔
913
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
130,302!
914
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
130,301!
915
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
130,269!
916
  pSW = NULL;
130,269✔
917

918
  taosMemoryFreeClear(block);
130,269!
919

920
END:
×
921
  if (code != 0) {
130,370!
922
    tqError("processBuildNew failed, code:%d", code);
×
923
  }
924
  tDeleteSchemaWrapper(pSW);
130,370!
925
  blockDataFreeRes(block);
130,314✔
926
  taosMemoryFree(block);
130,286!
927
  return code;
130,321✔
928
}
929
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
106✔
930
  int32_t code = 0;
106✔
931
  int32_t curRow = 0;
106✔
932
  int32_t lastRow = 0;
106✔
933

934
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
106✔
935
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
106!
936
  TQ_NULL_GO_TO_END(assigned);
106!
937

938
  SArray*   pCols = pSubmitTbData->aCol;
106✔
939
  SColData* pCol = taosArrayGet(pCols, 0);
106✔
940
  TQ_NULL_GO_TO_END(pCol);
106!
941
  int32_t numOfRows = pCol->nVal;
106✔
942
  int32_t numOfCols = taosArrayGetSize(pCols);
106✔
943
  tqTrace("vgId:%d, tqProcessColData start, col num: %d, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfCols,
106!
944
          numOfRows);
945
  for (int32_t i = 0; i < numOfRows; i++) {
301✔
946
    bool buildNew = false;
196✔
947

948
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
1,011✔
949
      int32_t k = 0;
810✔
950
      for (; k < numOfCols; k++) {
2,161✔
951
        pCol = taosArrayGet(pCols, k);
2,155✔
952
        TQ_NULL_GO_TO_END(pCol);
2,155!
953
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
2,159✔
954
          SColVal colVal = {0};
808✔
955
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
808!
956
          PROCESS_VAL
809!
957
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
809!
958
          break;
809✔
959
        }
960
      }
961
      if (k >= numOfCols) {
815!
962
        // this column is not in the current row, so we set it to NULL
963
        assigned[j] = 0;
×
964
        buildNew = true;
×
965
      }
966
    }
967

968
    if (buildNew) {
201✔
969
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
105!
970
    }
971

972
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
202✔
973
    TQ_NULL_GO_TO_END(pBlock);
196!
974

975
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
196!
976
            (int32_t)taosArrayGetSize(blocks));
977

978
    int32_t targetIdx = 0;
196✔
979
    int32_t sourceIdx = 0;
196✔
980
    int32_t colActual = blockDataGetNumOfCols(pBlock);
196✔
981
    while (targetIdx < colActual && sourceIdx < numOfCols) {
1,000!
982
      pCol = taosArrayGet(pCols, sourceIdx);
805✔
983
      TQ_NULL_GO_TO_END(pCol);
805!
984
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
805✔
985
      TQ_NULL_GO_TO_END(pColData);
805!
986
      SColVal colVal = {0};
805✔
987
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
805!
988
      SET_DATA
805!
989
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
804!
990
    }
991

992
    curRow++;
195✔
993
  }
994
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
105✔
995
  pLastBlock->info.rows = curRow - lastRow;
106✔
996
  tqTrace("vgId:%d, tqProcessColData end, col num: %d, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId,
106!
997
          numOfCols, numOfRows, (int)taosArrayGetSize(blocks));
998
END:
106✔
999
  if (code != TSDB_CODE_SUCCESS) {
106!
1000
    tqError("vgId:%d, process col data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1001
  }
1002
  taosMemoryFree(assigned);
106!
1003
  return code;
106✔
1004
}
1005

1006
int32_t tqProcessRowData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
130,141✔
1007
  int32_t   code = 0;
130,141✔
1008
  STSchema* pTSchema = NULL;
130,141✔
1009

1010
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
130,141✔
1011
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
130,141!
1012
  TQ_NULL_GO_TO_END(assigned);
130,263!
1013

1014
  int32_t curRow = 0;
130,263✔
1015
  int32_t lastRow = 0;
130,263✔
1016
  SArray* pRows = pSubmitTbData->aRowP;
130,263✔
1017
  int32_t numOfRows = taosArrayGetSize(pRows);
130,263✔
1018
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
130,225✔
1019
  TQ_NULL_GO_TO_END(pTSchema);
130,231!
1020
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
130,231!
1021

1022
  for (int32_t i = 0; i < numOfRows; i++) {
2,480,366✔
1023
    bool  buildNew = false;
2,322,350✔
1024
    SRow* pRow = taosArrayGetP(pRows, i);
2,322,350✔
1025
    TQ_NULL_GO_TO_END(pRow);
2,309,011!
1026

1027
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
12,409,201✔
1028
      SColVal colVal = {0};
10,048,294✔
1029
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
10,048,294!
1030
      PROCESS_VAL
10,082,632!
1031
      tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], pTSchema->numOfCols);
10,082,632!
1032
    }
1033

1034
    if (buildNew) {
2,360,907✔
1035
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
130,222!
1036
    }
1037

1038
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
2,360,890✔
1039
    TQ_NULL_GO_TO_END(pBlock);
2,304,645!
1040

1041
    tqTrace("vgId:%d, taosx scan, block num: %d", pReader->pWalReader->pWal->cfg.vgId,
2,304,645!
1042
            (int32_t)taosArrayGetSize(blocks));
1043

1044
    int32_t targetIdx = 0;
2,304,645✔
1045
    int32_t sourceIdx = 0;
2,304,645✔
1046
    int32_t colActual = blockDataGetNumOfCols(pBlock);
2,304,645✔
1047
    while (targetIdx < colActual && sourceIdx < pTSchema->numOfCols) {
12,713,835!
1048
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
10,363,646✔
1049
      TQ_NULL_GO_TO_END(pColData);
10,314,280!
1050
      SColVal          colVal = {0};
10,314,280✔
1051
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
10,314,280!
1052
      SET_DATA
10,281,450!
1053
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
10,418,593!
1054
    }
1055

1056
    curRow++;
2,350,189✔
1057
  }
1058
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
158,016✔
1059
  pLastBlock->info.rows = curRow - lastRow;
130,105✔
1060

1061
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
130,105!
1062
          (int)taosArrayGetSize(blocks));
1063
END:
130,105✔
1064
  if (code != TSDB_CODE_SUCCESS) {
130,097!
1065
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1066
  }
1067
  taosMemoryFreeClear(pTSchema);
130,097!
1068
  taosMemoryFree(assigned);
130,237!
1069
  return code;
130,189✔
1070
}
1071

1072
static int32_t buildCreateTbInfo(SMqDataRsp* pRsp, SVCreateTbReq* pCreateTbReq) {
28✔
1073
  int32_t code = 0;
28✔
1074
  int32_t lino = 0;
28✔
1075
  void*   createReq = NULL;
28✔
1076
  TSDB_CHECK_NULL(pRsp, code, lino, END, TSDB_CODE_INVALID_PARA);
28!
1077
  TSDB_CHECK_NULL(pCreateTbReq, code, lino, END, TSDB_CODE_INVALID_PARA);
28!
1078

1079
  if (pRsp->createTableNum == 0) {
28✔
1080
    pRsp->createTableLen = taosArrayInit(0, sizeof(int32_t));
14✔
1081
    TSDB_CHECK_NULL(pRsp->createTableLen, code, lino, END, terrno);
14!
1082
    pRsp->createTableReq = taosArrayInit(0, sizeof(void*));
14✔
1083
    TSDB_CHECK_NULL(pRsp->createTableReq, code, lino, END, terrno);
14!
1084
  }
1085

1086
  uint32_t len = 0;
28✔
1087
  tEncodeSize(tEncodeSVCreateTbReq, pCreateTbReq, len, code);
28!
1088
  TSDB_CHECK_CODE(code, lino, END);
28!
1089
  createReq = taosMemoryCalloc(1, len);
28!
1090
  TSDB_CHECK_NULL(createReq, code, lino, END, terrno);
28!
1091

1092
  SEncoder encoder = {0};
28✔
1093
  tEncoderInit(&encoder, createReq, len);
28✔
1094
  code = tEncodeSVCreateTbReq(&encoder, pCreateTbReq);
28✔
1095
  tEncoderClear(&encoder);
28✔
1096
  TSDB_CHECK_CODE(code, lino, END);
28!
1097
  TSDB_CHECK_NULL(taosArrayPush(pRsp->createTableLen, &len), code, lino, END, terrno);
56!
1098
  TSDB_CHECK_NULL(taosArrayPush(pRsp->createTableReq, &createReq), code, lino, END, terrno);
56!
1099
  pRsp->createTableNum++;
28✔
1100
  tqTrace("build create table info msg success");
28!
1101

1102
END:
28✔
1103
  if (code != 0) {
28!
1104
    tqError("%s failed at %d, failed to build create table info msg:%s", __FUNCTION__, lino, tstrerror(code));
×
1105
    taosMemoryFree(createReq);
×
1106
  }
1107
  return code;
28✔
1108
}
1109

1110
int32_t tqRetrieveTaosxBlock(STqReader* pReader, SMqDataRsp* pRsp, SArray* blocks, SArray* schemas,
130,568✔
1111
                             SSubmitTbData** pSubmitTbDataRet, SArray* rawList, int8_t fetchMeta) {
1112
  tqTrace("tq reader retrieve data block msg pointer:%p, index:%d", pReader->msg.msgStr, pReader->nextBlk);
130,568!
1113
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
130,568✔
1114
  if (pSubmitTbData == NULL) {
130,569!
1115
    return terrno;
×
1116
  }
1117
  pReader->nextBlk++;
130,570✔
1118

1119
  if (pSubmitTbDataRet) {
130,570!
1120
    *pSubmitTbDataRet = pSubmitTbData;
130,570✔
1121
  }
1122

1123
  if (fetchMeta == ONLY_META) {
130,570✔
1124
    if (pSubmitTbData->pCreateTbReq != NULL) {
22✔
1125
      if (pRsp->createTableReq == NULL) {
4✔
1126
        pRsp->createTableReq = taosArrayInit(0, POINTER_BYTES);
1✔
1127
        if (pRsp->createTableReq == NULL) {
1!
1128
          return terrno;
×
1129
        }
1130
      }
1131
      if (taosArrayPush(pRsp->createTableReq, &pSubmitTbData->pCreateTbReq) == NULL) {
8!
1132
        return terrno;
×
1133
      }
1134
      pSubmitTbData->pCreateTbReq = NULL;
4✔
1135
    }
1136
    return 0;
22✔
1137
  }
1138

1139
  int32_t sversion = pSubmitTbData->sver;
130,548✔
1140
  int64_t uid = pSubmitTbData->uid;
130,548✔
1141
  pReader->lastBlkUid = uid;
130,548✔
1142

1143
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
130,548✔
1144
  taosMemoryFreeClear(pReader->extSchema);
130,556!
1145
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema);
130,556✔
1146
  if (pReader->pSchemaWrapper == NULL) {
130,470✔
1147
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
116!
1148
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1149
    pReader->cachedSchemaSuid = 0;
150✔
1150
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
150✔
1151
  }
1152

1153
  if (pSubmitTbData->pCreateTbReq != NULL) {
130,354✔
1154
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
28✔
1155
    if (code != 0) {
28!
1156
      return code;
×
1157
    }
1158
  } else if (rawList != NULL) {
130,326!
1159
    if (taosArrayPush(schemas, &pReader->pSchemaWrapper) == NULL) {
×
1160
      return terrno;
×
1161
    }
1162
    pReader->pSchemaWrapper = NULL;
×
1163
    return 0;
×
1164
  }
1165

1166
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
130,354✔
1167
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
106✔
1168
  } else {
1169
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
130,248✔
1170
  }
1171
}
1172

1173
int32_t tqReaderSetColIdList(STqReader* pReader, SArray* pColIdList, const char* id) {
1,034✔
1174
  if (pReader == NULL) {
1,034!
1175
    return TSDB_CODE_SUCCESS;
×
1176
  }
1177
  pReader->pColIdList = pColIdList;
1,034✔
1178
  return tqCollectPhysicalTables(pReader, id);
1,034✔
1179
}
1180

1181
int32_t tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
1,110✔
1182
  if (pReader == NULL || tbUidList == NULL) {
1,110!
1183
    return TSDB_CODE_SUCCESS;
×
1184
  }
1185
  if (pReader->tbIdHash) {
1,110✔
1186
    taosHashClear(pReader->tbIdHash);
14✔
1187
  } else {
1188
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
1,096✔
1189
    if (pReader->tbIdHash == NULL) {
1,097!
1190
      tqError("s-task:%s failed to init hash table", id);
×
1191
      return terrno;
×
1192
    }
1193
  }
1194

1195
  for (int i = 0; i < taosArrayGetSize(tbUidList); i++) {
29,000✔
1196
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
27,887✔
1197
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
27,880!
1198
      tqError("s-task:%s failed to add table uid:%" PRId64 " to hash", id, *pKey);
×
1199
      continue;
×
1200
    }
1201
  }
1202

1203
  tqDebug("s-task:%s %d tables are set to be queried target table", id, (int32_t)taosArrayGetSize(tbUidList));
1,111!
1204
  return TSDB_CODE_SUCCESS;
1,111✔
1205
}
1206

1207
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
202✔
1208
  if (pReader == NULL || pTableUidList == NULL) {
202!
1209
    return;
×
1210
  }
1211
  if (pReader->tbIdHash == NULL) {
202!
1212
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
×
1213
    if (pReader->tbIdHash == NULL) {
×
1214
      tqError("failed to init hash table");
×
1215
      return;
×
1216
    }
1217
  }
1218

1219
  int32_t numOfTables = taosArrayGetSize(pTableUidList);
202✔
1220
  for (int i = 0; i < numOfTables; i++) {
422✔
1221
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
220✔
1222
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
220!
1223
      tqError("failed to add table uid:%" PRId64 " to hash", *pKey);
×
1224
      continue;
×
1225
    }
1226
  }
1227
}
1228

1229
bool tqReaderIsQueriedTable(STqReader* pReader, uint64_t uid) {
×
1230
  if (pReader == NULL) {
×
1231
    return false;
×
1232
  }
1233
  return taosHashGet(pReader->tbIdHash, &uid, sizeof(uint64_t)) != NULL;
×
1234
}
1235

1236
bool tqCurrentBlockConsumed(const STqReader* pReader) {
×
1237
  if (pReader == NULL) {
×
1238
    return false;
×
1239
  }
1240
  return pReader->msg.msgStr == NULL;
×
1241
}
1242

1243
void tqReaderRemoveTbUidList(STqReader* pReader, const SArray* tbUidList) {
7✔
1244
  if (pReader == NULL || tbUidList == NULL) {
7!
1245
    return;
×
1246
  }
1247
  for (int32_t i = 0; i < taosArrayGetSize(tbUidList); i++) {
10✔
1248
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
3✔
1249
    if (pKey && taosHashRemove(pReader->tbIdHash, pKey, sizeof(int64_t)) != 0) {
3!
1250
      tqError("failed to remove table uid:%" PRId64 " from hash", *pKey);
×
1251
    }
1252
  }
1253
}
1254

1255
int32_t tqUpdateTbUidList(STQ* pTq, const SArray* tbUidList, bool isAdd) {
155,136✔
1256
  if (pTq == NULL) {
155,136!
1257
    return 0;  // mounted vnode may have no tq
×
1258
  }
1259
  if (tbUidList == NULL) {
155,136!
1260
    return TSDB_CODE_INVALID_PARA;
×
1261
  }
1262
  void*   pIter = NULL;
155,136✔
1263
  int32_t vgId = TD_VID(pTq->pVnode);
155,136✔
1264

1265
  // update the table list for each consumer handle
1266
  taosWLockLatch(&pTq->lock);
155,136✔
1267
  while (1) {
3,376✔
1268
    pIter = taosHashIterate(pTq->pHandle, pIter);
158,516✔
1269
    if (pIter == NULL) {
158,515✔
1270
      break;
155,138✔
1271
    }
1272

1273
    STqHandle* pTqHandle = (STqHandle*)pIter;
3,377✔
1274
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
3,377✔
1275
      int32_t code = qUpdateTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList, isAdd);
209✔
1276
      if (code != 0) {
209!
1277
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1278
        continue;
×
1279
      }
1280
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
3,168✔
1281
      if (!isAdd) {
3,153✔
1282
        int32_t sz = taosArrayGetSize(tbUidList);
1,060✔
1283
        for (int32_t i = 0; i < sz; i++) {
1,060!
1284
          int64_t* tbUid = (int64_t*)taosArrayGet(tbUidList, i);
×
1285
          if (tbUid &&
×
1286
              taosHashPut(pTqHandle->execHandle.execDb.pFilterOutTbUid, tbUid, sizeof(int64_t), NULL, 0) != 0) {
×
1287
            tqError("failed to add table uid:%" PRId64 " to hash", *tbUid);
×
1288
            continue;
×
1289
          }
1290
        }
1291
      }
1292
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__TABLE) {
15✔
1293
      if (isAdd) {
14!
1294
        SArray* list = NULL;
14✔
1295
        int     ret = qGetTableList(pTqHandle->execHandle.execTb.suid, pTq->pVnode, pTqHandle->execHandle.execTb.node,
14✔
1296
                                    &list, pTqHandle->execHandle.task);
1297
        if (ret != TDB_CODE_SUCCESS) {
14!
1298
          tqError("qGetTableList in tqUpdateTbUidList error:%d handle %s consumer:0x%" PRIx64, ret, pTqHandle->subKey,
×
1299
                  pTqHandle->consumerId);
1300
          taosArrayDestroy(list);
×
1301
          taosHashCancelIterate(pTq->pHandle, pIter);
×
1302
          taosWUnLockLatch(&pTq->lock);
×
1303

1304
          return ret;
×
1305
        }
1306
        tqReaderSetTbUidList(pTqHandle->execHandle.pTqReader, list, NULL);
14✔
1307
        taosArrayDestroy(list);
14✔
1308
      } else {
1309
        tqReaderRemoveTbUidList(pTqHandle->execHandle.pTqReader, tbUidList);
×
1310
      }
1311
    }
1312
  }
1313
  taosWUnLockLatch(&pTq->lock);
155,138✔
1314

1315
  // update the table list handle for each stream scanner/wal reader
1316
/* STREAMTODO
1317
  streamMetaWLock(pTq->pStreamMeta);
1318
  while (1) {
1319
    pIter = taosHashIterate(pTq->pStreamMeta->pTasksMap, pIter);
1320
    if (pIter == NULL) {
1321
      break;
1322
    }
1323

1324
    int64_t      refId = *(int64_t*)pIter;
1325
    SStreamTask* pTask = taosAcquireRef(streamTaskRefPool, refId);
1326
    if (pTask != NULL) {
1327
      int32_t taskId = pTask->id.taskId;
1328

1329
      if ((pTask->info.taskLevel == TASK_LEVEL__SOURCE) && (pTask->exec.pExecutor != NULL)) {
1330
        int32_t code = qUpdateTableListForStreamScanner(pTask->exec.pExecutor, tbUidList, isAdd);
1331
        if (code != 0) {
1332
          tqError("vgId:%d, s-task:0x%x update qualified table error for stream task", vgId, taskId);
1333
        }
1334
      }
1335
      int32_t ret = taosReleaseRef(streamTaskRefPool, refId);
1336
      if (ret) {
1337
        tqError("vgId:%d release task refId failed, refId:%" PRId64, vgId, refId);
1338
      }
1339
    }
1340
  }
1341

1342
  streamMetaWUnLock(pTq->pStreamMeta);
1343
*/  
1344
  return 0;
155,128✔
1345
}
1346

1347
static void destroySourceScanTables(void* ptr) {
×
1348
  SArray** pTables = ptr;
×
1349
  if (pTables && *pTables) {
×
1350
    taosArrayDestroy(*pTables);
×
1351
    *pTables = NULL;
×
1352
  }
1353
}
×
1354

1355
static int32_t compareSVTColInfo(const void* p1, const void* p2) {
×
1356
  SVTColInfo* pCol1 = (SVTColInfo*)p1;
×
1357
  SVTColInfo* pCol2 = (SVTColInfo*)p2;
×
1358
  if (pCol1->vColId == pCol2->vColId) {
×
1359
    return 0;
×
1360
  } else if (pCol1->vColId < pCol2->vColId) {
×
1361
    return -1;
×
1362
  } else {
1363
    return 1;
×
1364
  }
1365
}
1366

1367
int32_t tqReaderSetVtableInfo(STqReader* pReader, void* vnode, void* ptr, SSHashObj* pVtableInfos,
×
1368
                              SSDataBlock** ppResBlock, const char* idstr) {
1369
  int32_t            code = TSDB_CODE_SUCCESS;
×
1370
  int32_t            lino = 0;
×
1371
  SStorageAPI*       pAPI = ptr;
×
1372
  SVTSourceScanInfo* pScanInfo = NULL;
×
1373
  SHashObj*          pVirtualTables = NULL;
×
1374
  SMetaReader        metaReader = {0};
×
1375
  SVTColInfo         colInfo = {0};
×
1376
  SSchemaWrapper*    schema = NULL;
×
1377

1378
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1379
  TSDB_CHECK_NULL(vnode, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1380
  TSDB_CHECK_NULL(pAPI, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1381

1382
  pScanInfo = &pReader->vtSourceScanInfo;
×
1383
  taosHashCleanup(pScanInfo->pVirtualTables);
×
1384
  pScanInfo->pVirtualTables = NULL;
×
1385

1386
  if (tSimpleHashGetSize(pVtableInfos) == 0) {
×
1387
    goto _end;
×
1388
  }
1389

1390
  pVirtualTables = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
1391
  TSDB_CHECK_NULL(pVirtualTables, code, lino, _end, terrno);
×
1392
  taosHashSetFreeFp(pVirtualTables, destroySourceScanTables);
×
1393

1394
  int32_t iter = 0;
×
1395
  void*   px = tSimpleHashIterate(pVtableInfos, NULL, &iter);
×
1396
  while (px != NULL) {
×
1397
    int64_t vTbUid = *(int64_t*)tSimpleHashGetKey(px, NULL);
×
1398
    SArray* pColInfos = taosArrayInit(8, sizeof(SVTColInfo));
×
1399
    TSDB_CHECK_NULL(pColInfos, code, lino, _end, terrno);
×
1400
    code = taosHashPut(pVirtualTables, &vTbUid, sizeof(int64_t), &pColInfos, POINTER_BYTES);
×
1401
    TSDB_CHECK_CODE(code, lino, _end);
×
1402

1403
    SSHashObj* pPhysicalTables = *(SSHashObj**)px;
×
1404
    int32_t    iterIn = 0;
×
1405
    void*      pxIn = tSimpleHashIterate(pPhysicalTables, NULL, &iterIn);
×
1406
    while (pxIn != NULL) {
×
1407
      char* physicalTableName = tSimpleHashGetKey(pxIn, NULL);
×
1408
      pAPI->metaReaderFn.clearReader(&metaReader);
×
1409
      pAPI->metaReaderFn.initReader(&metaReader, vnode, META_READER_LOCK, &pAPI->metaFn);
×
1410
      code = pAPI->metaReaderFn.getTableEntryByName(&metaReader, physicalTableName);
×
1411
      TSDB_CHECK_CODE(code, lino, _end);
×
1412
      pAPI->metaReaderFn.readerReleaseLock(&metaReader);
×
1413
      colInfo.pTbUid = metaReader.me.uid;
×
1414

1415
      switch (metaReader.me.type) {
×
1416
        case TSDB_CHILD_TABLE: {
×
1417
          int64_t suid = metaReader.me.ctbEntry.suid;
×
1418
          pAPI->metaReaderFn.clearReader(&metaReader);
×
1419
          pAPI->metaReaderFn.initReader(&metaReader, vnode, META_READER_LOCK, &pAPI->metaFn);
×
1420
          code = pAPI->metaReaderFn.getTableEntryByUid(&metaReader, suid);
×
1421
          TSDB_CHECK_CODE(code, lino, _end);
×
1422
          pAPI->metaReaderFn.readerReleaseLock(&metaReader);
×
1423
          schema = &metaReader.me.stbEntry.schemaRow;
×
1424
          break;
×
1425
        }
1426
        case TSDB_NORMAL_TABLE: {
×
1427
          schema = &metaReader.me.ntbEntry.schemaRow;
×
1428
          break;
×
1429
        }
1430
        default: {
×
1431
          tqError("invalid table type: %d", metaReader.me.type);
×
1432
          code = TSDB_CODE_INVALID_PARA;
×
1433
          TSDB_CHECK_CODE(code, lino, _end);
×
1434
        }
1435
      }
1436

1437
      SArray* pCols = *(SArray**)pxIn;
×
1438
      int32_t ncols = taosArrayGetSize(pCols);
×
1439
      for (int32_t i = 0; i < ncols; ++i) {
×
1440
        SColIdName* pCol = taosArrayGet(pCols, i);
×
1441
        colInfo.vColId = pCol->colId;
×
1442

1443
        for (int32_t j = 0; j < schema->nCols; ++j) {
×
1444
          if (strncmp(pCol->colName, schema->pSchema[j].name, strlen(schema->pSchema[j].name)) == 0) {
×
1445
            colInfo.pColId = schema->pSchema[j].colId;
×
1446
            void* px = taosArrayPush(pColInfos, &colInfo);
×
1447
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
×
1448
            break;
×
1449
          }
1450
        }
1451
      }
1452

1453
      taosArraySort(pColInfos, compareSVTColInfo);
×
1454
      pxIn = tSimpleHashIterate(pPhysicalTables, pxIn, &iterIn);
×
1455
    }
1456

1457
    px = tSimpleHashIterate(pVtableInfos, px, &iter);
×
1458
  }
1459

1460
  pScanInfo->pVirtualTables = pVirtualTables;
×
1461
  pVirtualTables = NULL;
×
1462

1463
  // set the result data block
1464
  if (pReader->pResBlock) {
×
1465
    blockDataDestroy(pReader->pResBlock);
×
1466
  }
1467
  pReader->pResBlock = *ppResBlock;
×
1468
  *ppResBlock = NULL;
×
1469

1470
  // update reader callback for vtable source scan
1471
  pAPI->tqReaderFn.tqNextBlockImpl = tqNextVTableSourceBlockImpl;
×
1472
  pAPI->tqReaderFn.tqReaderIsQueriedTable = tqReaderIsQueriedSourceTable;
×
1473

1474
_end:
×
1475
  if (code != TSDB_CODE_SUCCESS) {
×
1476
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1477
  }
1478
  pAPI->metaReaderFn.clearReader(&metaReader);
×
1479
  if (pVirtualTables != NULL) {
×
1480
    taosHashCleanup(pVirtualTables);
×
1481
  }
1482
  return code;
×
1483
}
1484

1485
static int32_t tqCollectPhysicalTables(STqReader* pReader, const char* idstr) {
1,035✔
1486
  int32_t            code = TSDB_CODE_SUCCESS;
1,035✔
1487
  int32_t            lino = 0;
1,035✔
1488
  SVTSourceScanInfo* pScanInfo = NULL;
1,035✔
1489
  SHashObj*          pVirtualTables = NULL;
1,035✔
1490
  SHashObj*          pPhysicalTables = NULL;
1,035✔
1491
  void*              pIter = NULL;
1,035✔
1492
  void*              px = NULL;
1,035✔
1493

1494
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
1,035!
1495

1496
  pScanInfo = &pReader->vtSourceScanInfo;
1,035✔
1497
  taosHashCleanup(pScanInfo->pPhysicalTables);
1,035✔
1498
  pScanInfo->pPhysicalTables = NULL;
1,035✔
1499
  taosLRUCacheCleanup(pScanInfo->pPhyTblSchemaCache);
1,035✔
1500
  pScanInfo->pPhyTblSchemaCache = NULL;
1,035✔
1501
  pScanInfo->nextVirtualTableIdx = -1;
1,035✔
1502
  pScanInfo->metaFetch = 0;
1,035✔
1503
  pScanInfo->cacheHit = 0;
1,035✔
1504

1505
  pVirtualTables = pScanInfo->pVirtualTables;
1,035✔
1506
  if (taosHashGetSize(pVirtualTables) == 0 || taosArrayGetSize(pReader->pColIdList) == 0) {
1,035!
1507
    goto _end;
1,036✔
1508
  }
1509

1510
  pPhysicalTables = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
1511
  TSDB_CHECK_NULL(pPhysicalTables, code, lino, _end, terrno);
×
1512
  taosHashSetFreeFp(pPhysicalTables, destroySourceScanTables);
×
1513

1514
  pIter = taosHashIterate(pVirtualTables, NULL);
×
1515
  while (pIter != NULL) {
×
1516
    int64_t vTbUid = *(int64_t*)taosHashGetKey(pIter, NULL);
×
1517
    SArray* pColInfos = *(SArray**)pIter;
×
1518
    TSDB_CHECK_NULL(pColInfos, code, lino, _end, TSDB_CODE_INTERNAL_ERROR);
×
1519

1520
    // Traverse all required columns and collect corresponding physical tables
1521
    int32_t nColInfos = taosArrayGetSize(pColInfos);
×
1522
    int32_t nOutputCols = taosArrayGetSize(pReader->pColIdList);
×
1523
    for (int32_t i = 0, j = 0; i < nColInfos && j < nOutputCols;) {
×
1524
      SVTColInfo* pCol = taosArrayGet(pColInfos, i);
×
1525
      col_id_t    colIdNeed = *(col_id_t*)taosArrayGet(pReader->pColIdList, j);
×
1526
      if (pCol->vColId < colIdNeed) {
×
1527
        i++;
×
1528
      } else if (pCol->vColId > colIdNeed) {
×
1529
        j++;
×
1530
      } else {
1531
        SArray* pRelatedVTs = NULL;
×
1532
        px = taosHashGet(pPhysicalTables, &pCol->pTbUid, sizeof(int64_t));
×
1533
        if (px == NULL) {
×
1534
          pRelatedVTs = taosArrayInit(8, sizeof(int64_t));
×
1535
          TSDB_CHECK_NULL(pRelatedVTs, code, lino, _end, terrno);
×
1536
          code = taosHashPut(pPhysicalTables, &pCol->pTbUid, sizeof(int64_t), &pRelatedVTs, POINTER_BYTES);
×
1537
          if (code != TSDB_CODE_SUCCESS) {
×
1538
            taosArrayDestroy(pRelatedVTs);
×
1539
            TSDB_CHECK_CODE(code, lino, _end);
×
1540
          }
1541
        } else {
1542
          pRelatedVTs = *(SArray**)px;
×
1543
        }
1544
        if (taosArrayGetSize(pRelatedVTs) == 0 || *(int64_t*)taosArrayGetLast(pRelatedVTs) != vTbUid) {
×
1545
          px = taosArrayPush(pRelatedVTs, &vTbUid);
×
1546
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
×
1547
        }
1548
        i++;
×
1549
        j++;
×
1550
      }
1551
    }
1552
    pIter = taosHashIterate(pVirtualTables, pIter);
×
1553
  }
1554

1555
  pScanInfo->pPhysicalTables = pPhysicalTables;
×
1556
  pPhysicalTables = NULL;
×
1557

1558
  if (taosHashGetSize(pScanInfo->pPhysicalTables) > 0) {
×
1559
    pScanInfo->pPhyTblSchemaCache = taosLRUCacheInit(1024 * 128, -1, .5);
×
1560
    TSDB_CHECK_NULL(pScanInfo->pPhyTblSchemaCache, code, lino, _end, terrno);
×
1561
  }
1562

1563
_end:
×
1564
  if (code != TSDB_CODE_SUCCESS) {
1,036!
1565
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1566
  }
1567
  if (pIter != NULL) {
1,034!
1568
    taosHashCancelIterate(pReader->tbIdHash, pIter);
×
1569
  }
1570
  if (pPhysicalTables != NULL) {
1,034!
1571
    taosHashCleanup(pPhysicalTables);
×
1572
  }
1573
  return code;
1,034✔
1574
}
1575

1576
static void freeTableSchemaCache(const void* key, size_t keyLen, void* value, void* ud) {
×
1577
  if (value) {
×
1578
    SSchemaWrapper* pSchemaWrapper = value;
×
1579
    tDeleteSchemaWrapper(pSchemaWrapper);
1580
  }
1581
}
×
1582

1583
bool tqNextVTableSourceBlockImpl(STqReader* pReader, const char* idstr) {
×
1584
  int32_t            code = TSDB_CODE_SUCCESS;
×
1585
  int32_t            lino = 0;
×
1586
  SVTSourceScanInfo* pScanInfo = NULL;
×
1587

1588
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1589

1590
  pScanInfo = &pReader->vtSourceScanInfo;
×
1591
  if (pReader->msg.msgStr == NULL || taosHashGetSize(pScanInfo->pPhysicalTables) == 0) {
×
1592
    return false;
×
1593
  }
1594

1595
  if (pScanInfo->nextVirtualTableIdx >= 0) {
×
1596
    // The data still needs to be converted into the virtual table result block
1597
    return true;
×
1598
  }
1599

1600
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
×
1601
  while (pReader->nextBlk < blockSz) {
×
1602
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
×
1603
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, _end, terrno);
×
1604
    int64_t pTbUid = pSubmitTbData->uid;
×
1605
    void*   px = taosHashGet(pScanInfo->pPhysicalTables, &pTbUid, sizeof(int64_t));
×
1606
    if (px != NULL) {
×
1607
      SArray* pRelatedVTs = *(SArray**)px;
×
1608
      if (taosArrayGetSize(pRelatedVTs) > 0) {
×
1609
        pScanInfo->nextVirtualTableIdx = 0;
×
1610
        return true;
×
1611
      }
1612
    }
1613
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64, pReader->nextBlk, blockSz, pTbUid);
×
1614
    pReader->nextBlk++;
×
1615
  }
1616

1617
  tqReaderClearSubmitMsg(pReader);
×
1618
  tqTrace("iterator data block end, total block num:%d", blockSz);
×
1619

1620
_end:
×
1621
  if (code != TSDB_CODE_SUCCESS) {
×
1622
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1623
  }
1624
  return false;
×
1625
}
1626

1627
bool tqReaderIsQueriedSourceTable(STqReader* pReader, uint64_t uid) {
×
1628
  if (pReader == NULL) {
×
1629
    return false;
×
1630
  }
1631
  return taosHashGet(pReader->vtSourceScanInfo.pPhysicalTables, &uid, sizeof(uint64_t)) != NULL;
×
1632
}
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