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

taosdata / TDengine / #4693

29 Aug 2025 06:36AM UTC coverage: 58.007% (-1.1%) from 59.151%
#4693

push

travis-ci

web-flow
fix(gpt): fix race-condition in preparing tmp files (#32800)

132676 of 291873 branches covered (45.46%)

Branch coverage included in aggregate %.

5 of 34 new or added lines in 6 files covered. (14.71%)

4288 existing lines in 199 files now uncovered.

201114 of 283561 relevant lines covered (70.92%)

5433357.08 hits per line

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

49.22
/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) {
546✔
22
  if (pHandle == NULL || pHead == NULL) {
546!
23
    return false;
×
24
  }
25
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
546!
26
    return true;
546✔
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) {
144,586✔
208
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
144,586!
209
    return -1;
×
210
  }
211
  int32_t code = -1;
144,663✔
212
  int32_t vgId = TD_VID(pTq->pVnode);
144,663✔
213
  int64_t id = pHandle->pWalReader->readerId;
144,663✔
214

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

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

224
  while (offset <= appliedVer) {
153,733✔
225
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
143,970!
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,959!
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,972✔
236
      code = walFetchBody(pHandle->pWalReader);
134,510✔
237
      goto END;
134,492✔
238
    } else {
239
      if (pHandle->fetchMeta != WITH_DATA) {
9,462✔
240
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
693✔
241
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
693✔
242
          code = walFetchBody(pHandle->pWalReader);
546✔
243
          if (code < 0) {
546!
244
            goto END;
×
245
          }
246

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

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

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

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

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

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

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

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

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

326
  return pReader;
1,318✔
327
}
328

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

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

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

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

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

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

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

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

375
  int64_t st = taosGetTimestampMs();
246,113✔
376
  while (1) {
229,610✔
377
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
475,723✔
378
    while (pReader->nextBlk < numOfBlocks) {
489,429✔
379
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
229,665!
380
              pReader->msg.ver);
381

382
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
229,665✔
383
      if (pSubmitTbData == NULL) {
229,667!
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) {
229,668✔
389
        pReader->nextBlk += 1;
106✔
390
        continue;
106✔
391
      }
392
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
229,558!
393
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
215,960!
394
        SSDataBlock* pRes = NULL;
215,960✔
395
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
215,960✔
396
        if (code == TSDB_CODE_SUCCESS) {
215,953!
397
          return true;
215,957✔
398
        }
399
      } else {
400
        pReader->nextBlk += 1;
13,603✔
401
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
13,603!
402
      }
403
    }
404

405
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
259,764✔
406
    pReader->msg.msgStr = NULL;
259,767✔
407

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

413
    // try next message in wal file
414
    if (walNextValidMsg(pWalReader, false) < 0) {
259,767✔
415
      return false;
30,143✔
416
    }
417

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

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

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

439
  tDecoderInit(&decoder, pReader->msg.msgStr, pReader->msg.msgLen);
364,108✔
440
  int32_t code = tDecodeSubmitReq(&decoder, &pReader->submit, rawList);
364,099✔
441
  tDecoderClear(&decoder);
364,021✔
442

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

447
  return code;
364,099✔
448
}
449

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

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

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

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

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

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

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

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

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

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

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

515
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
195,514✔
516
  while (pReader->nextBlk < blockSz) {
195,497✔
517
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
97,910✔
518
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
97,908!
519
    uid = pSubmitTbData->uid;
97,908✔
520
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
97,908✔
521
    TSDB_CHECK_NULL(ret, code, lino, END, true);
97,909!
UNCOV
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,587✔
526
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
97,794!
527

528
END:
97,794✔
529
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
195,704!
530
  return code;
195,482✔
531
}
532

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

540
  int32_t cnt = 0;
130,839✔
541
  for (int32_t i = 0; i < pSrc->nCols; i++) {
743,064✔
542
    cnt += mask[i];
612,225✔
543
  }
544

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

551
  int32_t j = 0;
130,799✔
552
  for (int32_t i = 0; i < pSrc->nCols; i++) {
742,204✔
553
    if (mask[i]) {
611,363✔
554
      pDst->pSchema[j++] = pSrc->pSchema[i];
611,354✔
555
      SColumnInfoData colInfo =
556
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
611,354✔
557
      if (extSrc != NULL) {
612,193✔
558
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
218✔
559
      }
560
      code = blockDataAppendColInfo(pBlock, &colInfo);
612,193✔
561
      if (code != 0) {
611,396!
562
        return code;
×
563
      }
564
    }
565
  }
566
  return 0;
130,841✔
567
}
568

569
static int32_t buildResSDataBlock(STqReader* pReader, SSchemaWrapper* pSchema, const SArray* pColIdList) {
401✔
570
  if (pReader == NULL || pSchema == NULL || pColIdList == NULL) {
401!
571
    return TSDB_CODE_INVALID_PARA;
×
572
  }
573
  SSDataBlock* pBlock = pReader->pResBlock;
401✔
574
  if (blockDataGetNumOfCols(pBlock) > 0) {
401✔
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);
401✔
587

588
  if (numOfCols == 0) {  // all columns are required
401!
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) {
401✔
604
      numOfCols = pSchema->nCols;
2✔
605
    }
606

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

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

636
  return TSDB_CODE_SUCCESS;
401✔
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) {
31,661,194✔
679
  int32_t code = TSDB_CODE_SUCCESS;
31,661,194✔
680

681
  if (IS_VAR_DATA_TYPE(pColVal->value.type)) {
31,661,194!
682
    if (COL_VAL_IS_VALUE(pColVal)) {
6,655,694!
683
      char val[65535 + 2] = {0};
6,978,017✔
684
      if (pColVal->value.pData != NULL) {
6,978,017!
685
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
7,037,905✔
686
      }
687
      varDataSetLen(val, pColVal->value.nData);
6,978,017✔
688
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
6,978,017✔
689
    } else {
690
      colDataSetNULL(pColumnInfoData, rowIndex);
×
691
    }
692
  } else {
693
    code = colDataSetVal(pColumnInfoData, rowIndex, VALUE_GET_DATUM(&pColVal->value, pColVal->value.type),
25,005,500!
694
                         !COL_VAL_IS_VALUE(pColVal));
25,005,500!
695
  }
696

697
  return code;
31,390,947✔
698
}
699

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

713
  blockDataCleanup(pBlock);
215,965✔
714

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

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

724
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
215,960✔
725
      (pReader->cachedSchemaVer != sversion)) {
215,562✔
726
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
401✔
727
    taosMemoryFree(pReader->extSchema);
401!
728
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema);
401✔
729
    if (pReader->pSchemaWrapper == NULL) {
401!
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;
401✔
738
    pReader->cachedSchemaSuid = suid;
401✔
739
    pReader->cachedSchemaVer = sversion;
401✔
740

741
    if (pReader->cachedSchemaVer != pReader->pSchemaWrapper->version) {
401!
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);
401✔
747
    TSDB_CHECK_CODE(code, line, END);
401!
748
    pBlock = pReader->pResBlock;
401✔
749
    *pRes = pBlock;
401✔
750
  }
751

752
  int32_t numOfRows = 0;
215,960✔
753
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
215,960✔
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);
215,956✔
759
  }
760

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

766
  // convert and scan one block
767
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
215,961✔
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;
215,957✔
812
    SSchemaWrapper* pWrapper = pReader->pSchemaWrapper;
215,957✔
813
    pTSchema = tBuildTSchema(pWrapper->pSchema, pWrapper->nCols, pWrapper->version);
215,957✔
814
    TSDB_CHECK_NULL(pTSchema, code, line, END, terrno);
215,954✔
815

816
    for (int32_t i = 0; i < numOfRows; i++) {
6,733,982✔
817
      SRow* pRow = taosArrayGetP(pRows, i);
6,237,515✔
818
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
6,224,688!
819
      int32_t sourceIdx = 0;
6,225,025✔
820
      for (int32_t j = 0; j < colActual; j++) {
27,083,641✔
821
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, j);
20,565,611✔
822
        TSDB_CHECK_NULL(pColData, code, line, END, terrno);
20,477,158!
823

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

830
          if (colVal.cid < pColData->info.colId) {
20,770,186!
831
            sourceIdx++;
×
832
            continue;
×
833
          } else if (colVal.cid == pColData->info.colId) {
20,770,186!
834
            if (isBlob == 0) {
20,813,308!
835
              code = doSetVal(pColData, i, &colVal);
20,820,510✔
836
            } else {
837
              code = doSetBlobVal(pColData, i, &colVal, pSubmitTbData->pBlobSet);
×
838
            }
839

840
            TSDB_CHECK_CODE(code, line, END);
20,901,738!
841

842
            sourceIdx++;
20,901,738✔
843
            break;
20,858,616✔
844
          } else {
845
            colDataSetNULL(pColData, i);
×
846
            break;
×
847
          }
848
        }
849
      }
850
    }
851
  }
852

853
END:
496,467✔
854
  if (code != 0) {
496,471!
855
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
856
  }
857
  taosMemoryFreeClear(pTSchema);
215,934!
858
  return code;
215,959✔
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,804✔
890
                               char* assigned, int32_t numOfRows, int32_t curRow, int32_t* lastRow) {
891
  int32_t         code = 0;
130,804✔
892
  SSchemaWrapper* pSW = NULL;
130,804✔
893
  SSDataBlock*    block = NULL;
130,804✔
894
  if (taosArrayGetSize(blocks) > 0) {
130,804!
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,806!
902
  TQ_NULL_GO_TO_END(block);
130,852!
903

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

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

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

918
  taosMemoryFreeClear(block);
130,801!
919

920
END:
×
921
  if (code != 0) {
130,869!
922
    tqError("processBuildNew failed, code:%d", code);
×
923
  }
924
  tDeleteSchemaWrapper(pSW);
130,869!
925
  blockDataFreeRes(block);
130,841✔
926
  taosMemoryFree(block);
130,826!
927
  return code;
130,847✔
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++) {
302✔
946
    bool buildNew = false;
196✔
947

948
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
1,009✔
949
      int32_t k = 0;
809✔
950
      for (; k < numOfCols; k++) {
2,161✔
951
        pCol = taosArrayGet(pCols, k);
2,158✔
952
        TQ_NULL_GO_TO_END(pCol);
2,158!
953
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
2,160✔
954
          SColVal colVal = {0};
808✔
955
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
808!
956
          PROCESS_VAL
810!
957
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
810!
958
          break;
810✔
959
        }
960
      }
961
      if (k >= numOfCols) {
813!
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) {
200✔
969
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
106!
970
    }
971

972
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
200✔
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,001!
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);
805!
990
    }
991

992
    curRow++;
196✔
993
  }
994
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
106✔
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,735✔
1007
  int32_t   code = 0;
130,735✔
1008
  STSchema* pTSchema = NULL;
130,735✔
1009

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

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

1022
  for (int32_t i = 0; i < numOfRows; i++) {
2,557,877✔
1023
    bool  buildNew = false;
2,371,128✔
1024
    SRow* pRow = taosArrayGetP(pRows, i);
2,371,128✔
1025
    TQ_NULL_GO_TO_END(pRow);
2,364,884!
1026

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

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

1038
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
2,453,634✔
1039
    TQ_NULL_GO_TO_END(pBlock);
2,361,836!
1040

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

1044
    int32_t targetIdx = 0;
2,361,836✔
1045
    int32_t sourceIdx = 0;
2,361,836✔
1046
    int32_t colActual = blockDataGetNumOfCols(pBlock);
2,361,836✔
1047
    while (targetIdx < colActual && sourceIdx < pTSchema->numOfCols) {
12,866,870!
1048
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
10,439,698✔
1049
      TQ_NULL_GO_TO_END(pColData);
10,389,412!
1050
      SColVal          colVal = {0};
10,389,412✔
1051
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
10,389,412!
1052
      SET_DATA
10,443,875!
1053
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
10,513,749!
1054
    }
1055

1056
    curRow++;
2,427,172✔
1057
  }
1058
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
186,749✔
1059
  pLastBlock->info.rows = curRow - lastRow;
130,628✔
1060

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

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

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

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

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

1102
END:
29✔
1103
  if (code != 0) {
29!
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;
29✔
1108
}
1109

1110
int32_t tqRetrieveTaosxBlock(STqReader* pReader, SMqDataRsp* pRsp, SArray* blocks, SArray* schemas,
131,066✔
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);
131,066!
1113
  SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
131,066✔
1114
  if (pSubmitTbData == NULL) {
131,065!
UNCOV
1115
    return terrno;
×
1116
  }
1117
  pReader->nextBlk++;
131,066✔
1118

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

1123
  if (fetchMeta == ONLY_META) {
131,066✔
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;
131,044✔
1140
  int64_t uid = pSubmitTbData->uid;
131,044✔
1141
  pReader->lastBlkUid = uid;
131,044✔
1142

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

1153
  if (pSubmitTbData->pCreateTbReq != NULL) {
130,881✔
1154
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
29✔
1155
    if (code != 0) {
29!
1156
      return code;
×
1157
    }
1158
  } else if (rawList != NULL) {
130,852!
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,881✔
1167
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
106✔
1168
  } else {
1169
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
130,775✔
1170
  }
1171
}
1172

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

1181
int32_t tqReaderSetTbUidList(STqReader* pReader, const SArray* tbUidList, const char* id) {
1,095✔
1182
  if (pReader == NULL || tbUidList == NULL) {
1,095!
1183
    return TSDB_CODE_SUCCESS;
×
1184
  }
1185
  if (pReader->tbIdHash) {
1,095✔
1186
    taosHashClear(pReader->tbIdHash);
14✔
1187
  } else {
1188
    pReader->tbIdHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
1,081✔
1189
    if (pReader->tbIdHash == NULL) {
1,082!
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++) {
28,749✔
1196
    int64_t* pKey = (int64_t*)taosArrayGet(tbUidList, i);
27,606✔
1197
    if (pKey && taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
27,509!
1198
      tqError("s-task:%s failed to add table uid:%" PRId64 " to hash", id, *pKey);
32!
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,091!
1204
  return TSDB_CODE_SUCCESS;
1,096✔
1205
}
1206

1207
void tqReaderAddTbUidList(STqReader* pReader, const SArray* pTableUidList) {
232✔
1208
  if (pReader == NULL || pTableUidList == NULL) {
232!
1209
    return;
×
1210
  }
1211
  if (pReader->tbIdHash == NULL) {
232!
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);
232✔
1220
  for (int i = 0; i < numOfTables; i++) {
458✔
1221
    int64_t* pKey = (int64_t*)taosArrayGet(pTableUidList, i);
226✔
1222
    if (taosHashPut(pReader->tbIdHash, pKey, sizeof(int64_t), NULL, 0) != 0) {
226!
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) {
154,185✔
1256
  if (pTq == NULL) {
154,185!
1257
    return 0;  // mounted vnode may have no tq
×
1258
  }
1259
  if (tbUidList == NULL) {
154,185!
1260
    return TSDB_CODE_INVALID_PARA;
×
1261
  }
1262
  void*   pIter = NULL;
154,185✔
1263
  int32_t vgId = TD_VID(pTq->pVnode);
154,185✔
1264

1265
  // update the table list for each consumer handle
1266
  taosWLockLatch(&pTq->lock);
154,185✔
1267
  while (1) {
3,406✔
1268
    pIter = taosHashIterate(pTq->pHandle, pIter);
157,592✔
1269
    if (pIter == NULL) {
157,592✔
1270
      break;
154,186✔
1271
    }
1272

1273
    STqHandle* pTqHandle = (STqHandle*)pIter;
3,406✔
1274
    if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__COLUMN) {
3,406✔
1275
      int32_t code = qUpdateTableListForStreamScanner(pTqHandle->execHandle.task, tbUidList, isAdd);
239✔
1276
      if (code != 0) {
239!
1277
        tqError("update qualified table error for %s", pTqHandle->subKey);
×
1278
        continue;
×
1279
      }
1280
    } else if (pTqHandle->execHandle.subType == TOPIC_SUB_TYPE__DB) {
3,167✔
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) {
14!
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);
154,186✔
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;
154,185✔
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,011✔
1486
  int32_t            code = TSDB_CODE_SUCCESS;
1,011✔
1487
  int32_t            lino = 0;
1,011✔
1488
  SVTSourceScanInfo* pScanInfo = NULL;
1,011✔
1489
  SHashObj*          pVirtualTables = NULL;
1,011✔
1490
  SHashObj*          pPhysicalTables = NULL;
1,011✔
1491
  void*              pIter = NULL;
1,011✔
1492
  void*              px = NULL;
1,011✔
1493

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

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

1505
  pVirtualTables = pScanInfo->pVirtualTables;
1,020✔
1506
  if (taosHashGetSize(pVirtualTables) == 0 || taosArrayGetSize(pReader->pColIdList) == 0) {
1,020!
1507
    goto _end;
1,020✔
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,020!
1565
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1566
  }
1567
  if (pIter != NULL) {
1,020!
1568
    taosHashCancelIterate(pReader->tbIdHash, pIter);
×
1569
  }
1570
  if (pPhysicalTables != NULL) {
1,020!
1571
    taosHashCleanup(pPhysicalTables);
×
1572
  }
1573
  return code;
1,020✔
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