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

taosdata / TDengine / #4871

04 Dec 2025 01:55AM UTC coverage: 64.654% (+0.1%) from 64.545%
#4871

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

869 of 2219 new or added lines in 36 files covered. (39.16%)

441 existing lines in 120 files now uncovered.

159620 of 246882 relevant lines covered (64.65%)

110922946.31 hits per line

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

63.54
/source/dnode/vnode/src/tq/tqRead.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tmsg.h"
17
#include "tq.h"
18

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

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

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

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

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

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

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

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

116
end:
111✔
117
  taosArrayDestroy(req.pMultiTag);
111✔
118
  metaReaderClear(&mr);  
111✔
119
  if (code < 0) {
111✔
120
    tqError("processAlterTbMsg failed, code:%d, line:%d", code, lino);
×
121
  }
122
} 
111✔
123

124
static void processDropTbMsg(SDecoder* dcoder, SWalCont* pHead, STqReader* pReader, int64_t* realTbSuid, int64_t tbSuid) {
×
125
  SVDropTbBatchReq req = {0};
×
126
  SVDropTbBatchReq reqNew = {0};
×
127
  void* buf = NULL;
×
128
  int32_t lino = 0;
×
129
  int32_t code = tDecodeSVDropTbBatchReq(dcoder, &req);
×
130
  if (code < 0) {
×
131
    lino = __LINE__;
×
132
    goto end;
×
133
  }
134

135
  int32_t      needRebuild = 0;
×
136
  SVDropTbReq* pDropReq = NULL;
×
137
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
138
    pDropReq = req.pReqs + iReq;
×
139

140
    if (pDropReq->suid == tbSuid &&
×
141
        taosHashGet(pReader->tbIdHash, &pDropReq->uid, sizeof(int64_t)) != NULL) {
×
142
      needRebuild++;
×
143
    }
144
  }
145
  if (needRebuild == 0) {
×
146
    // do nothing
147
  } else if (needRebuild == req.nReqs) {
×
148
    *realTbSuid = tbSuid;
×
149
  } else {
150
    *realTbSuid = tbSuid;
×
151
    reqNew.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbReq));
×
152
    if (reqNew.pArray == NULL) {
×
153
      code = terrno;
×
154
      lino = __LINE__;
×
155
      goto end;
×
156
    }
157
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
158
      pDropReq = req.pReqs + iReq;
×
159
      if (pDropReq->suid == tbSuid &&
×
160
          taosHashGet(pReader->tbIdHash, &pDropReq->uid, sizeof(int64_t)) != NULL) {
×
161
        reqNew.nReqs++;
×
162
        if (taosArrayPush(reqNew.pArray, pDropReq) == NULL) {
×
163
          code = terrno;
×
164
          lino = __LINE__;
×
165
          goto end;
×
166
        }
167
      }
168
    }
169

170
    int     tlen = 0;
×
171
    tEncodeSize(tEncodeSVDropTbBatchReq, &reqNew, tlen, code);
×
172
    buf = taosMemoryMalloc(tlen);
×
173
    if (NULL == buf || code < 0) {
×
174
      lino = __LINE__;
×
175
      goto end;
×
176
    }
177
    SEncoder coderNew = {0};
×
178
    tEncoderInit(&coderNew, buf, tlen);
×
179
    code = tEncodeSVDropTbBatchReq(&coderNew, &reqNew);
×
180
    tEncoderClear(&coderNew);
×
181
    if (code != 0) {
×
182
      lino = __LINE__;
×
183
      goto end;
×
184
    }
185
    (void)memcpy(pHead->body + sizeof(SMsgHead), buf, tlen);
×
186
    pHead->bodyLen = tlen + sizeof(SMsgHead);
×
187
  }
188

189
end:
×
190
  taosMemoryFree(buf);
×
191
  taosArrayDestroy(reqNew.pArray);
×
192
  if (code < 0) {
×
193
    tqError("processDropTbMsg failed, code:%d, line:%d", code, lino);
×
194
  }
195
}
×
196

197
bool isValValidForTable(STqHandle* pHandle, SWalCont* pHead) {
13,805✔
198
  int32_t code = 0;
13,805✔
199
  int32_t lino = 0;
13,805✔
200
  if (pHandle == NULL || pHead == NULL) {
13,805✔
201
    return false;
×
202
  }
203
  if (pHandle->execHandle.subType != TOPIC_SUB_TYPE__TABLE) {
13,805✔
204
    return true;
13,476✔
205
  }
206

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

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

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

221
  if (msgType == TDMT_VND_CREATE_STB || msgType == TDMT_VND_ALTER_STB) {
401✔
222
    SVCreateStbReq req = {0};
72✔
223
    if (tDecodeSVCreateStbReq(&dcoder, &req) < 0) {
72✔
224
      goto end;
×
225
    }
226
    realTbSuid = req.suid;
72✔
227
  } else if (msgType == TDMT_VND_DROP_STB) {
257✔
228
    SVDropStbReq req = {0};
×
229
    if (tDecodeSVDropStbReq(&dcoder, &req) < 0) {
×
230
      goto end;
×
231
    }
232
    realTbSuid = req.suid;
×
233
  } else if (msgType == TDMT_VND_CREATE_TABLE) {
257✔
234
    processCreateTbMsg(&dcoder, pHead, pReader, &realTbSuid, tbSuid);
146✔
235
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
111✔
236
    processAlterTbMsg(&dcoder, pReader, &realTbSuid);
111✔
237
  } else if (msgType == TDMT_VND_DROP_TABLE) {
×
238
    processDropTbMsg(&dcoder, pHead, pReader, &realTbSuid, tbSuid);
×
239
  } else if (msgType == TDMT_VND_DELETE) {
×
240
    SDeleteRes req = {0};
×
241
    if (tDecodeDeleteRes(&dcoder, &req) < 0) {
×
242
      goto end;
×
243
    }
244
    realTbSuid = req.suid;
×
245
  }
246

247
end:
329✔
248
  tDecoderClear(&dcoder);
329✔
249
  bool tmp = tbSuid == realTbSuid;
329✔
250
  tqDebug("%s suid:%" PRId64 " realSuid:%" PRId64 " return:%d", __FUNCTION__, tbSuid, realTbSuid, tmp);
329✔
251
  return tmp;
329✔
252
}
253

254
int32_t tqFetchLog(STQ* pTq, STqHandle* pHandle, int64_t* fetchOffset, uint64_t reqId) {
15,119,486✔
255
  if (pTq == NULL || pHandle == NULL || fetchOffset == NULL) {
15,119,486✔
256
    return -1;
×
257
  }
258
  int32_t code = -1;
15,154,033✔
259
  int32_t vgId = TD_VID(pTq->pVnode);
15,154,033✔
260
  int64_t id = pHandle->pWalReader->readerId;
15,155,601✔
261

262
  int64_t offset = *fetchOffset;
15,153,131✔
263
  int64_t lastVer = walGetLastVer(pHandle->pWalReader->pWal);
15,157,031✔
264
  int64_t committedVer = walGetCommittedVer(pHandle->pWalReader->pWal);
15,155,573✔
265
  int64_t appliedVer = walGetAppliedVer(pHandle->pWalReader->pWal);
15,159,058✔
266

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

271
  while (offset <= appliedVer) {
15,574,547✔
272
    if (walFetchHead(pHandle->pWalReader, offset) < 0) {
14,566,791✔
273
      tqDebug("tmq poll: consumer:0x%" PRIx64 ", (epoch %d) vgId:%d offset %" PRId64
×
274
              ", no more log to return, QID:0x%" PRIx64 " 0x%" PRIx64,
275
              pHandle->consumerId, pHandle->epoch, vgId, offset, reqId, id);
276
      goto END;
×
277
    }
278

279
    tqDebug("vgId:%d, consumer:0x%" PRIx64 " taosx get msg ver %" PRId64 ", type:%s, QID:0x%" PRIx64 " 0x%" PRIx64,
14,566,586✔
280
            vgId, pHandle->consumerId, offset, TMSG_INFO(pHandle->pWalReader->pHead->head.msgType), reqId, id);
281

282
    if (pHandle->pWalReader->pHead->head.msgType == TDMT_VND_SUBMIT) {
14,566,710✔
283
      code = walFetchBody(pHandle->pWalReader);
14,139,815✔
284
      goto END;
14,139,610✔
285
    } else {
286
      if (pHandle->fetchMeta != WITH_DATA) {
426,977✔
287
        SWalCont* pHead = &(pHandle->pWalReader->pHead->head);
18,058✔
288
        if (IS_META_MSG(pHead->msgType) && !(pHead->msgType == TDMT_VND_DELETE && pHandle->fetchMeta == ONLY_META)) {
18,058✔
289
          code = walFetchBody(pHandle->pWalReader);
13,805✔
290
          if (code < 0) {
13,805✔
291
            goto END;
×
292
          }
293

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

314
END:
1,007,756✔
315
  *fetchOffset = offset;
15,161,060✔
316
  tqDebug("vgId:%d, end to fetch wal, code:%d , index:%" PRId64 ", last:%" PRId64 " commit:%" PRId64
15,160,900✔
317
          ", applied:%" PRId64 ", 0x%" PRIx64,
318
          vgId, code, offset, lastVer, committedVer, appliedVer, id);
319
  return code;
15,161,097✔
320
}
321

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

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

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

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

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

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

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

373
  return pReader;
129,269✔
374
}
375

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

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

385
  if (pReader->pSchemaWrapper) {
129,269✔
386
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
75,057✔
387
  }
388

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

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

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

405
int32_t tqReaderSeek(STqReader* pReader, int64_t ver, const char* id) {
218,923✔
406
  if (pReader == NULL) {
218,923✔
407
    return TSDB_CODE_INVALID_PARA;
×
408
  }
409
  if (walReaderSeekVer(pReader->pWalReader, ver) < 0) {
218,923✔
410
    return terrno;
12,787✔
411
  }
412
  tqDebug("wal reader seek to ver:%" PRId64 " %s", ver, id);
206,243✔
413
  return 0;
206,340✔
414
}
415

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

422
  int64_t st = taosGetTimestampMs();
34,902,154✔
423
  while (1) {
36,396,099✔
424
    int32_t numOfBlocks = taosArrayGetSize(pReader->submit.aSubmitTbData);
71,298,253✔
425
    while (pReader->nextBlk < numOfBlocks) {
76,113,653✔
426
      tqTrace("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
36,396,205✔
427
              pReader->msg.ver);
428

429
      SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
36,396,205✔
430
      if (pSubmitTbData == NULL) {
36,389,172✔
431
        tqError("tq reader next data block %d/%d, len:%d %" PRId64, pReader->nextBlk, numOfBlocks, pReader->msg.msgLen,
×
432
                pReader->msg.ver);
433
        return false;
537✔
434
      }
435
      if ((pSubmitTbData->flags & sourceExcluded) != 0) {
36,389,172✔
436
        pReader->nextBlk += 1;
2,544✔
437
        continue;
2,544✔
438
      }
439
      if (pReader->tbIdHash == NULL || taosHashGet(pReader->tbIdHash, &pSubmitTbData->uid, sizeof(int64_t)) != NULL) {
36,386,528✔
440
        tqTrace("tq reader return submit block, uid:%" PRId64, pSubmitTbData->uid);
31,578,081✔
441
        SSDataBlock* pRes = NULL;
31,578,081✔
442
        int32_t      code = tqRetrieveDataBlock(pReader, &pRes, NULL);
31,570,063✔
443
        if (code == TSDB_CODE_SUCCESS) {
31,577,103✔
444
          return true;
31,577,103✔
445
        }
446
      } else {
447
        pReader->nextBlk += 1;
4,817,070✔
448
        tqTrace("tq reader discard submit block, uid:%" PRId64 ", continue", pSubmitTbData->uid);
4,817,070✔
449
      }
450
    }
451

452
    tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
39,720,702✔
453
    pReader->msg.msgStr = NULL;
39,720,905✔
454

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

460
    // try next message in wal file
461
    if (walNextValidMsg(pWalReader, false) < 0) {
39,721,005✔
462
      return false;
3,323,859✔
463
    }
464

465
    void*   pBody = POINTER_SHIFT(pWalReader->pHead->head.body, sizeof(SSubmitReq2Msg));
36,395,167✔
466
    int32_t bodyLen = pWalReader->pHead->head.bodyLen - sizeof(SSubmitReq2Msg);
36,396,463✔
467
    int64_t ver = pWalReader->pHead->head.version;
36,396,283✔
468
    SDecoder decoder = {0};
36,396,123✔
469
    if (tqReaderSetSubmitMsg(pReader, pBody, bodyLen, ver, NULL, &decoder) != 0) {
36,396,015✔
470
      tDecoderClear(&decoder);
×
471
      return false;
×
472
    }
473
    tDecoderClear(&decoder);
36,395,072✔
474
    pReader->nextBlk = 0;
36,395,631✔
475
  }
476
}
477

478
int32_t tqReaderSetSubmitMsg(STqReader* pReader, void* msgStr, int32_t msgLen, int64_t ver, SArray* rawList, SDecoder* decoder) {
50,534,888✔
479
  if (pReader == NULL) {
50,534,888✔
480
    return TSDB_CODE_INVALID_PARA;
×
481
  }
482
  pReader->msg.msgStr = msgStr;
50,534,888✔
483
  pReader->msg.msgLen = msgLen;
50,535,052✔
484
  pReader->msg.ver = ver;
50,535,807✔
485

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

488
  tDecoderInit(decoder, pReader->msg.msgStr, pReader->msg.msgLen);
50,536,183✔
489
  int32_t code = tDecodeSubmitReq(decoder, &pReader->submit, rawList);
50,535,460✔
490

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

495
  return code;
50,530,480✔
496
}
497

498
void tqReaderClearSubmitMsg(STqReader* pReader) {
28,242,490✔
499
  tDestroySubmitReq(&pReader->submit, TSDB_MSG_FLG_DECODE);
28,242,490✔
500
  pReader->nextBlk = 0;
28,246,708✔
501
  pReader->msg.msgStr = NULL;
28,247,288✔
502
}
28,263,288✔
503

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

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

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

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

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

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

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

549
END:
2,888,259✔
550
  tqTrace("%s:%d return:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
5,489,341✔
551
  return code;
5,489,149✔
552
}
553

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

559
  TSDB_CHECK_NULL(pReader, code, lino, END, false);
22,481,513✔
560
  TSDB_CHECK_NULL(pReader->msg.msgStr, code, lino, END, false);
22,481,513✔
561
  TSDB_CHECK_NULL(filterOutUids, code, lino, END, true);
22,496,910✔
562

563
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
22,496,910✔
564
  while (pReader->nextBlk < blockSz) {
22,497,260✔
565
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
11,252,897✔
566
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, END, false);
11,253,225✔
567
    uid = pSubmitTbData->uid;
11,253,225✔
568
    void* ret = taosHashGet(filterOutUids, &pSubmitTbData->uid, sizeof(int64_t));
11,253,225✔
569
    TSDB_CHECK_NULL(ret, code, lino, END, true);
11,252,706✔
570
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64, pReader->nextBlk, blockSz, uid);
×
571
    pReader->nextBlk++;
×
572
  }
573
  tqReaderClearSubmitMsg(pReader);
11,248,582✔
574
  tqTrace("iterator data block end, total block num:%d, uid:%" PRId64, blockSz, uid);
11,246,577✔
575

576
END:
11,246,577✔
577
  tqTrace("%s:%d get data:%s, uid:%" PRId64, __FUNCTION__, lino, code ? "true" : "false", uid);
22,499,283✔
578
  return code;
22,463,618✔
579
}
580

581
int32_t tqMaskBlock(SSchemaWrapper* pDst, SSDataBlock* pBlock, const SSchemaWrapper* pSrc, char* mask,
13,785,780✔
582
                    SExtSchema* extSrc) {
583
  if (pDst == NULL || pBlock == NULL || pSrc == NULL || mask == NULL) {
13,785,780✔
584
    return TSDB_CODE_INVALID_PARA;
×
585
  }
586
  int32_t code = 0;
13,788,941✔
587

588
  int32_t cnt = 0;
13,788,941✔
589
  for (int32_t i = 0; i < pSrc->nCols; i++) {
63,049,432✔
590
    cnt += mask[i];
49,254,559✔
591
  }
592

593
  pDst->nCols = cnt;
13,795,722✔
594
  pDst->pSchema = taosMemoryCalloc(cnt, sizeof(SSchema));
13,808,661✔
595
  if (pDst->pSchema == NULL) {
13,799,016✔
596
    return TAOS_GET_TERRNO(terrno);
×
597
  }
598

599
  int32_t j = 0;
13,790,819✔
600
  for (int32_t i = 0; i < pSrc->nCols; i++) {
63,082,433✔
601
    if (mask[i]) {
49,260,411✔
602
      pDst->pSchema[j++] = pSrc->pSchema[i];
49,288,887✔
603
      SColumnInfoData colInfo =
49,275,173✔
604
          createColumnInfoData(pSrc->pSchema[i].type, pSrc->pSchema[i].bytes, pSrc->pSchema[i].colId);
49,278,627✔
605
      if (extSrc != NULL) {
49,269,084✔
606
        decimalFromTypeMod(extSrc[i].typeMod, &colInfo.info.precision, &colInfo.info.scale);
5,208✔
607
      }
608
      code = blockDataAppendColInfo(pBlock, &colInfo);
49,269,084✔
609
      if (code != 0) {
49,285,639✔
610
        return code;
×
611
      }
612
    }
613
  }
614
  return 0;
13,812,810✔
615
}
616

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

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

634
  int32_t numOfCols = taosArrayGetSize(pColIdList);
64,129✔
635

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

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

655
    int32_t i = 0;
64,105✔
656
    int32_t j = 0;
64,105✔
657
    while (i < pSchema->nCols && j < numOfCols) {
823,367✔
658
      SSchema* pColSchema = &pSchema->pSchema[i];
759,262✔
659
      col_id_t colIdSchema = pColSchema->colId;
759,238✔
660

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

684
  return TSDB_CODE_SUCCESS;
64,129✔
685
}
686

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

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

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

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

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

731
  if (IS_VAR_DATA_TYPE(pColVal->value.type)) {
2,147,483,647✔
732
    if (COL_VAL_IS_VALUE(pColVal)) {
697,692,640✔
733
      char val[65535 + 2] = {0};
723,701,753✔
734
      if (pColVal->value.pData != NULL) {
722,766,937✔
735
        (void)memcpy(varDataVal(val), pColVal->value.pData, pColVal->value.nData);
723,329,612✔
736
      }
737
      varDataSetLen(val, pColVal->value.nData);
723,031,294✔
738
      code = colDataSetVal(pColumnInfoData, rowIndex, val, false);
723,867,328✔
739
    } else {
740
      colDataSetNULL(pColumnInfoData, rowIndex);
×
741
    }
742
  } else {
743
    code = colDataSetVal(pColumnInfoData, rowIndex, VALUE_GET_DATUM(&pColVal->value, pColVal->value.type),
2,147,483,647✔
744
                         !COL_VAL_IS_VALUE(pColVal));
2,147,483,647✔
745
  }
746

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

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

763
  blockDataCleanup(pBlock);
31,579,095✔
764

765
  int32_t vgId = pReader->pWalReader->pWal->cfg.vgId;
31,578,595✔
766
  int32_t sversion = pSubmitTbData->sver;
31,578,795✔
767
  int64_t suid = pSubmitTbData->suid;
31,579,095✔
768
  int64_t uid = pSubmitTbData->uid;
31,578,895✔
769
  pReader->lastTs = pSubmitTbData->ctimeMs;
31,578,895✔
770

771
  pBlock->info.id.uid = uid;
31,578,995✔
772
  pBlock->info.version = pReader->msg.ver;
31,578,695✔
773

774
  if ((suid != 0 && pReader->cachedSchemaSuid != suid) || (suid == 0 && pReader->cachedSchemaUid != uid) ||
31,578,795✔
775
      (pReader->cachedSchemaVer != sversion)) {
31,514,366✔
776
    tDeleteSchemaWrapper(pReader->pSchemaWrapper);
62,719✔
777
    taosMemoryFree(pReader->extSchema);
64,129✔
778
    pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
64,129✔
779
    if (pReader->pSchemaWrapper == NULL) {
64,105✔
780
      tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", uid:%" PRId64
×
781
             "version %d, possibly dropped table",
782
             vgId, suid, uid, pReader->cachedSchemaVer);
783
      pReader->cachedSchemaSuid = 0;
×
784
      return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
×
785
    }
786

787
    pReader->cachedSchemaUid = uid;
64,105✔
788
    pReader->cachedSchemaSuid = suid;
64,129✔
789
    pReader->cachedSchemaVer = sversion;
64,129✔
790

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

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

811
  code = blockDataEnsureCapacity(pBlock, numOfRows);
31,578,408✔
812
  TSDB_CHECK_CODE(code, line, END);
31,578,595✔
813
  pBlock->info.rows = numOfRows;
31,578,595✔
814
  int32_t colActual = blockDataGetNumOfCols(pBlock);
31,578,532✔
815

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

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

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

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

866
    for (int32_t i = 0; i < numOfRows; i++) {
1,662,297,395✔
867
      SRow* pRow = taosArrayGetP(pRows, i);
1,516,850,352✔
868
      TSDB_CHECK_NULL(pRow, code, line, END, terrno);
1,508,198,968✔
869
      int32_t sourceIdx = 0;
1,517,004,436✔
870
      for (int32_t j = 0; j < colActual; j++) {
2,147,483,647✔
871
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, j);
2,147,483,647✔
872
        TSDB_CHECK_NULL(pColData, code, line, END, terrno);
2,147,483,647✔
873

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

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

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

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

903
END:
145,447,043✔
904
  if (code != 0) {
133,844,808✔
905
    tqError("tqRetrieveDataBlock failed, line:%d, msg:%s", line, tstrerror(code));
×
906
  }
907
  taosMemoryFreeClear(pTSchema);
31,577,612✔
908
  return code;
31,577,433✔
909
}
910

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

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

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

951
  block = taosMemoryCalloc(1, sizeof(SSDataBlock));
13,804,764✔
952
  TQ_NULL_GO_TO_END(block);
13,796,505✔
953

954
  pSW = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
13,796,505✔
955
  TQ_NULL_GO_TO_END(pSW);
13,785,084✔
956

957
  TQ_ERR_GO_TO_END(tqMaskBlock(pSW, block, pReader->pSchemaWrapper, assigned, pReader->extSchema));
13,785,084✔
958
  tqTrace("vgId:%d, build new block, col %d", pReader->pWalReader->pWal->cfg.vgId,
13,812,689✔
959
          (int32_t)taosArrayGetSize(block->pDataBlock));
960

961
  block->info.id.uid = pSubmitTbData->uid;
13,812,689✔
962
  block->info.version = pReader->msg.ver;
13,808,998✔
963
  TQ_ERR_GO_TO_END(blockDataEnsureCapacity(block, numOfRows - curRow));
13,811,352✔
964
  TQ_NULL_GO_TO_END(taosArrayPush(blocks, block));
13,803,138✔
965
  TQ_NULL_GO_TO_END(taosArrayPush(schemas, &pSW));
13,802,285✔
966
  pSW = NULL;
13,802,285✔
967

968
  taosMemoryFreeClear(block);
13,802,285✔
969

970
END:
13,808,116✔
971
  if (code != 0) {
13,806,937✔
972
    tqError("processBuildNew failed, code:%d", code);
×
973
  }
974
  tDeleteSchemaWrapper(pSW);
13,806,937✔
975
  blockDataFreeRes(block);
13,788,231✔
976
  taosMemoryFree(block);
13,791,670✔
977
  return code;
13,803,657✔
978
}
979
static int32_t tqProcessColData(STqReader* pReader, SSubmitTbData* pSubmitTbData, SArray* blocks, SArray* schemas) {
5,144✔
980
  int32_t code = 0;
5,144✔
981
  int32_t curRow = 0;
5,144✔
982
  int32_t lastRow = 0;
5,144✔
983

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

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

998
    for (int32_t j = 0; j < pSchemaWrapper->nCols; j++) {
9,553,458✔
999
      int32_t k = 0;
6,815,556✔
1000
      for (; k < numOfCols; k++) {
12,880,760✔
1001
        pCol = taosArrayGet(pCols, k);
11,270,190✔
1002
        TQ_NULL_GO_TO_END(pCol);
10,667,146✔
1003
        if (pSchemaWrapper->pSchema[j].colId == pCol->cid) {
10,667,146✔
1004
          SColVal colVal = {0};
6,178,790✔
1005
          TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
6,388,038✔
1006
          PROCESS_VAL
7,266,110✔
1007
          tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], numOfCols);
7,430,846✔
1008
          break;
7,070,772✔
1009
        }
1010
      }
1011
      if (k >= numOfCols) {
7,200,642✔
1012
        // this column is not in the current row, so we set it to NULL
1013
        assigned[j] = 0;
×
1014
        buildNew = true;
×
1015
      }
1016
    }
1017

1018
    if (buildNew) {
2,431,830✔
1019
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
5,144✔
1020
    }
1021

1022
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
2,431,830✔
1023
    TQ_NULL_GO_TO_END(pBlock);
2,504,032✔
1024

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

1028
    int32_t targetIdx = 0;
2,504,032✔
1029
    int32_t sourceIdx = 0;
2,504,032✔
1030
    int32_t colActual = blockDataGetNumOfCols(pBlock);
2,504,032✔
1031
    while (targetIdx < colActual && sourceIdx < numOfCols) {
9,402,954✔
1032
      pCol = taosArrayGet(pCols, sourceIdx);
6,965,586✔
1033
      TQ_NULL_GO_TO_END(pCol);
6,506,712✔
1034
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
6,506,712✔
1035
      TQ_NULL_GO_TO_END(pColData);
6,142,270✔
1036
      SColVal colVal = {0};
6,142,270✔
1037
      TQ_ERR_GO_TO_END(tColDataGetValue(pCol, i, &colVal));
6,179,112✔
1038
      SET_DATA
7,332,550✔
1039
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
7,011,840✔
1040
    }
1041

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

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

1060
  SSchemaWrapper* pSchemaWrapper = pReader->pSchemaWrapper;
13,770,518✔
1061
  char*           assigned = taosMemoryCalloc(1, pSchemaWrapper->nCols);
13,782,995✔
1062
  TQ_NULL_GO_TO_END(assigned);
13,778,511✔
1063

1064
  int32_t curRow = 0;
13,778,511✔
1065
  int32_t lastRow = 0;
13,778,511✔
1066
  SArray* pRows = pSubmitTbData->aRowP;
13,768,204✔
1067
  int32_t numOfRows = taosArrayGetSize(pRows);
13,803,080✔
1068
  pTSchema = tBuildTSchema(pSchemaWrapper->pSchema, pSchemaWrapper->nCols, pSchemaWrapper->version);
13,798,067✔
1069
  TQ_NULL_GO_TO_END(pTSchema);
13,777,928✔
1070
  tqTrace("vgId:%d, tqProcessRowData start, rows:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows);
13,777,928✔
1071

1072
  for (int32_t i = 0; i < numOfRows; i++) {
293,992,915✔
1073
    bool  buildNew = false;
280,194,451✔
1074
    SRow* pRow = taosArrayGetP(pRows, i);
280,194,451✔
1075
    TQ_NULL_GO_TO_END(pRow);
280,050,540✔
1076

1077
    for (int32_t j = 0; j < pTSchema->numOfCols; j++) {
1,552,704,479✔
1078
      SColVal colVal = {0};
1,271,767,088✔
1079
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, j, &colVal));
1,271,318,943✔
1080
      PROCESS_VAL
1,271,501,186✔
1081
      tqTrace("assign[%d] = %d, nCols:%d", j, assigned[j], pTSchema->numOfCols);
1,271,213,798✔
1082
    }
1083

1084
    if (buildNew) {
279,000,223✔
1085
      TQ_ERR_GO_TO_END(processBuildNew(pReader, pSubmitTbData, blocks, schemas, assigned, numOfRows, curRow, &lastRow));
13,806,058✔
1086
    }
1087

1088
    SSDataBlock* pBlock = taosArrayGetLast(blocks);
278,988,613✔
1089
    TQ_NULL_GO_TO_END(pBlock);
280,125,417✔
1090

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

1094
    int32_t targetIdx = 0;
280,125,417✔
1095
    int32_t sourceIdx = 0;
280,125,417✔
1096
    int32_t colActual = blockDataGetNumOfCols(pBlock);
280,125,417✔
1097
    while (targetIdx < colActual && sourceIdx < pTSchema->numOfCols) {
1,551,153,302✔
1098
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, targetIdx);
1,271,333,649✔
1099
      TQ_NULL_GO_TO_END(pColData);
1,270,520,628✔
1100
      SColVal          colVal = {0};
1,270,520,628✔
1101
      TQ_ERR_GO_TO_END(tRowGet(pRow, pTSchema, sourceIdx, &colVal));
1,269,419,383✔
1102
      SET_DATA
1,270,099,118✔
1103
      tqTrace("targetIdx:%d sourceIdx:%d colActual:%d", targetIdx, sourceIdx, colActual);
1,271,393,792✔
1104
    }
1105

1106
    curRow++;
280,226,469✔
1107
  }
1108
  SSDataBlock* pLastBlock = taosArrayGetLast(blocks);
13,798,464✔
1109
  if (pLastBlock != NULL) {
13,805,649✔
1110
    pLastBlock->info.rows = curRow - lastRow;
13,806,570✔
1111
  }
1112

1113
  tqTrace("vgId:%d, tqProcessRowData end, rows:%d, block num:%d", pReader->pWalReader->pWal->cfg.vgId, numOfRows,
13,807,337✔
1114
          (int)taosArrayGetSize(blocks));
1115
END:
14,635,557✔
1116
  if (code != TSDB_CODE_SUCCESS) {
13,799,177✔
1117
    tqError("vgId:%d, process row data failed, code:%d", pReader->pWalReader->pWal->cfg.vgId, code);
×
1118
  }
1119
  taosMemoryFreeClear(pTSchema);
13,785,711✔
1120
  taosMemoryFree(assigned);
13,783,885✔
1121
  return code;
13,798,451✔
1122
}
1123

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

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

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

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

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

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

1171
  if (pSubmitTbDataRet) {
13,844,841✔
1172
    *pSubmitTbDataRet = pSubmitTbData;
13,851,763✔
1173
  }
1174

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

1191
  int32_t sversion = pSubmitTbData->sver;
13,846,533✔
1192
  int64_t uid = pSubmitTbData->uid;
13,848,294✔
1193
  pReader->lastBlkUid = uid;
13,848,318✔
1194

1195
  tDeleteSchemaWrapper(pReader->pSchemaWrapper);
13,851,063✔
1196
  taosMemoryFreeClear(pReader->extSchema);
13,846,151✔
1197
  pReader->pSchemaWrapper = metaGetTableSchema(pReader->pVnodeMeta, uid, sversion, 1, &pReader->extSchema, 0);
13,850,625✔
1198
  if (pReader->pSchemaWrapper == NULL) {
13,847,256✔
1199
    tqWarn("vgId:%d, cannot found schema wrapper for table: suid:%" PRId64 ", version %d, possibly dropped table",
41,208✔
1200
           pReader->pWalReader->pWal->cfg.vgId, uid, pReader->cachedSchemaVer);
1201
    pReader->cachedSchemaSuid = 0;
41,208✔
1202
    return TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND;
41,208✔
1203
  }
1204

1205
  if (pSubmitTbData->pCreateTbReq != NULL) {
13,784,112✔
1206
    int32_t code = buildCreateTbInfo(pRsp, pSubmitTbData->pCreateTbReq);
764✔
1207
    if (code != 0) {
764✔
1208
      return code;
×
1209
    }
1210
  } else if (rawList != NULL) {
13,804,599✔
1211
    if (taosArrayPush(schemas, &pReader->pSchemaWrapper) == NULL) {
×
1212
      return terrno;
×
1213
    }
1214
    pReader->pSchemaWrapper = NULL;
×
1215
    return 0;
×
1216
  }
1217

1218
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
13,805,363✔
1219
    return tqProcessColData(pReader, pSubmitTbData, blocks, schemas);
5,144✔
1220
  } else {
1221
    return tqProcessRowData(pReader, pSubmitTbData, blocks, schemas);
13,784,424✔
1222
  }
1223
}
1224

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

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

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

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

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

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

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

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

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

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

1317
  // update the table list for each consumer handle
1318
  taosWLockLatch(&pTq->lock);
63,056,573✔
1319
  while (1) {
183,466✔
1320
    pIter = taosHashIterate(pTq->pHandle, pIter);
63,239,384✔
1321
    if (pIter == NULL) {
63,239,576✔
1322
      break;
63,056,110✔
1323
    }
1324

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

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

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

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

1391
int32_t tqReaderSetVtableInfo(STqReader* pReader, void* vnode, void* ptr, SSHashObj* pVtableInfos,
×
1392
                              SSDataBlock** ppResBlock, const char* idstr) {
1393
  int32_t            code = TSDB_CODE_SUCCESS;
×
1394
  int32_t            lino = 0;
×
1395
  SStorageAPI*       pAPI = ptr;
×
1396
  SVTSourceScanInfo* pScanInfo = NULL;
×
1397
  SHashObj*          pVirtualTables = NULL;
×
1398
  SMetaReader        metaReader = {0};
×
1399
  SVTColInfo         colInfo = {0};
×
1400
  SSchemaWrapper*    schema = NULL;
×
1401

1402
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1403
  TSDB_CHECK_NULL(vnode, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1404
  TSDB_CHECK_NULL(pAPI, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1405

1406
  pScanInfo = &pReader->vtSourceScanInfo;
×
1407
  taosHashCleanup(pScanInfo->pVirtualTables);
×
1408
  pScanInfo->pVirtualTables = NULL;
×
1409

1410
  if (tSimpleHashGetSize(pVtableInfos) == 0) {
×
1411
    goto _end;
×
1412
  }
1413

1414
  pVirtualTables = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
1415
  TSDB_CHECK_NULL(pVirtualTables, code, lino, _end, terrno);
×
1416
  taosHashSetFreeFp(pVirtualTables, destroySourceScanTables);
×
1417

1418
  int32_t iter = 0;
×
1419
  void*   px = tSimpleHashIterate(pVtableInfos, NULL, &iter);
×
1420
  while (px != NULL) {
×
1421
    int64_t vTbUid = *(int64_t*)tSimpleHashGetKey(px, NULL);
×
1422
    SArray* pColInfos = taosArrayInit(8, sizeof(SVTColInfo));
×
1423
    TSDB_CHECK_NULL(pColInfos, code, lino, _end, terrno);
×
1424
    code = taosHashPut(pVirtualTables, &vTbUid, sizeof(int64_t), &pColInfos, POINTER_BYTES);
×
1425
    TSDB_CHECK_CODE(code, lino, _end);
×
1426

1427
    SSHashObj* pPhysicalTables = *(SSHashObj**)px;
×
1428
    int32_t    iterIn = 0;
×
1429
    void*      pxIn = tSimpleHashIterate(pPhysicalTables, NULL, &iterIn);
×
1430
    while (pxIn != NULL) {
×
1431
      char* physicalTableName = tSimpleHashGetKey(pxIn, NULL);
×
1432
      pAPI->metaReaderFn.clearReader(&metaReader);
×
1433
      pAPI->metaReaderFn.initReader(&metaReader, vnode, META_READER_LOCK, &pAPI->metaFn);
×
1434
      code = pAPI->metaReaderFn.getTableEntryByName(&metaReader, physicalTableName);
×
1435
      TSDB_CHECK_CODE(code, lino, _end);
×
1436
      pAPI->metaReaderFn.readerReleaseLock(&metaReader);
×
1437
      colInfo.pTbUid = metaReader.me.uid;
×
1438

1439
      switch (metaReader.me.type) {
×
1440
        case TSDB_CHILD_TABLE: {
×
1441
          int64_t suid = metaReader.me.ctbEntry.suid;
×
1442
          pAPI->metaReaderFn.clearReader(&metaReader);
×
1443
          pAPI->metaReaderFn.initReader(&metaReader, vnode, META_READER_LOCK, &pAPI->metaFn);
×
1444
          code = pAPI->metaReaderFn.getTableEntryByUid(&metaReader, suid);
×
1445
          TSDB_CHECK_CODE(code, lino, _end);
×
1446
          pAPI->metaReaderFn.readerReleaseLock(&metaReader);
×
1447
          schema = &metaReader.me.stbEntry.schemaRow;
×
1448
          break;
×
1449
        }
1450
        case TSDB_NORMAL_TABLE: {
×
1451
          schema = &metaReader.me.ntbEntry.schemaRow;
×
1452
          break;
×
1453
        }
1454
        default: {
×
1455
          tqError("invalid table type: %d", metaReader.me.type);
×
1456
          code = TSDB_CODE_INVALID_PARA;
×
1457
          TSDB_CHECK_CODE(code, lino, _end);
×
1458
        }
1459
      }
1460

1461
      SArray* pCols = *(SArray**)pxIn;
×
1462
      int32_t ncols = taosArrayGetSize(pCols);
×
1463
      for (int32_t i = 0; i < ncols; ++i) {
×
1464
        SColIdName* pCol = taosArrayGet(pCols, i);
×
1465
        colInfo.vColId = pCol->colId;
×
1466

1467
        for (int32_t j = 0; j < schema->nCols; ++j) {
×
1468
          if (strncmp(pCol->colName, schema->pSchema[j].name, strlen(schema->pSchema[j].name)) == 0) {
×
1469
            colInfo.pColId = schema->pSchema[j].colId;
×
1470
            void* px = taosArrayPush(pColInfos, &colInfo);
×
1471
            TSDB_CHECK_NULL(px, code, lino, _end, terrno);
×
1472
            break;
×
1473
          }
1474
        }
1475
      }
1476

1477
      taosArraySort(pColInfos, compareSVTColInfo);
×
1478
      pxIn = tSimpleHashIterate(pPhysicalTables, pxIn, &iterIn);
×
1479
    }
1480

1481
    px = tSimpleHashIterate(pVtableInfos, px, &iter);
×
1482
  }
1483

1484
  pScanInfo->pVirtualTables = pVirtualTables;
×
1485
  pVirtualTables = NULL;
×
1486

1487
  // set the result data block
1488
  if (pReader->pResBlock) {
×
1489
    blockDataDestroy(pReader->pResBlock);
×
1490
  }
1491
  pReader->pResBlock = *ppResBlock;
×
1492
  *ppResBlock = NULL;
×
1493

1494
  // update reader callback for vtable source scan
1495
  pAPI->tqReaderFn.tqNextBlockImpl = tqNextVTableSourceBlockImpl;
×
1496
  pAPI->tqReaderFn.tqReaderIsQueriedTable = tqReaderIsQueriedSourceTable;
×
1497

1498
_end:
×
1499
  if (code != TSDB_CODE_SUCCESS) {
×
1500
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1501
  }
1502
  pAPI->metaReaderFn.clearReader(&metaReader);
×
1503
  if (pVirtualTables != NULL) {
×
1504
    taosHashCleanup(pVirtualTables);
×
1505
  }
1506
  return code;
×
1507
}
1508

1509
static int32_t tqCollectPhysicalTables(STqReader* pReader, const char* idstr) {
109,259✔
1510
  int32_t            code = TSDB_CODE_SUCCESS;
109,259✔
1511
  int32_t            lino = 0;
109,259✔
1512
  SVTSourceScanInfo* pScanInfo = NULL;
109,259✔
1513
  SHashObj*          pVirtualTables = NULL;
109,259✔
1514
  SHashObj*          pPhysicalTables = NULL;
109,259✔
1515
  void*              pIter = NULL;
109,259✔
1516
  void*              px = NULL;
109,259✔
1517

1518
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
109,259✔
1519

1520
  pScanInfo = &pReader->vtSourceScanInfo;
109,259✔
1521
  taosHashCleanup(pScanInfo->pPhysicalTables);
109,259✔
1522
  pScanInfo->pPhysicalTables = NULL;
109,699✔
1523
  taosLRUCacheCleanup(pScanInfo->pPhyTblSchemaCache);
109,699✔
1524
  pScanInfo->pPhyTblSchemaCache = NULL;
109,259✔
1525
  pScanInfo->nextVirtualTableIdx = -1;
109,699✔
1526
  pScanInfo->metaFetch = 0;
109,201✔
1527
  pScanInfo->cacheHit = 0;
109,201✔
1528

1529
  pVirtualTables = pScanInfo->pVirtualTables;
109,259✔
1530
  if (taosHashGetSize(pVirtualTables) == 0 || taosArrayGetSize(pReader->pColIdList) == 0) {
109,259✔
1531
    goto _end;
109,259✔
1532
  }
1533

1534
  pPhysicalTables = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
1535
  TSDB_CHECK_NULL(pPhysicalTables, code, lino, _end, terrno);
×
1536
  taosHashSetFreeFp(pPhysicalTables, destroySourceScanTables);
×
1537

1538
  pIter = taosHashIterate(pVirtualTables, NULL);
×
1539
  while (pIter != NULL) {
×
1540
    int64_t vTbUid = *(int64_t*)taosHashGetKey(pIter, NULL);
×
1541
    SArray* pColInfos = *(SArray**)pIter;
×
1542
    TSDB_CHECK_NULL(pColInfos, code, lino, _end, TSDB_CODE_INTERNAL_ERROR);
×
1543

1544
    // Traverse all required columns and collect corresponding physical tables
1545
    int32_t nColInfos = taosArrayGetSize(pColInfos);
×
1546
    int32_t nOutputCols = taosArrayGetSize(pReader->pColIdList);
×
1547
    for (int32_t i = 0, j = 0; i < nColInfos && j < nOutputCols;) {
×
1548
      SVTColInfo* pCol = taosArrayGet(pColInfos, i);
×
1549
      col_id_t    colIdNeed = *(col_id_t*)taosArrayGet(pReader->pColIdList, j);
×
1550
      if (pCol->vColId < colIdNeed) {
×
1551
        i++;
×
1552
      } else if (pCol->vColId > colIdNeed) {
×
1553
        j++;
×
1554
      } else {
1555
        SArray* pRelatedVTs = NULL;
×
1556
        px = taosHashGet(pPhysicalTables, &pCol->pTbUid, sizeof(int64_t));
×
1557
        if (px == NULL) {
×
1558
          pRelatedVTs = taosArrayInit(8, sizeof(int64_t));
×
1559
          TSDB_CHECK_NULL(pRelatedVTs, code, lino, _end, terrno);
×
1560
          code = taosHashPut(pPhysicalTables, &pCol->pTbUid, sizeof(int64_t), &pRelatedVTs, POINTER_BYTES);
×
1561
          if (code != TSDB_CODE_SUCCESS) {
×
1562
            taosArrayDestroy(pRelatedVTs);
×
1563
            TSDB_CHECK_CODE(code, lino, _end);
×
1564
          }
1565
        } else {
1566
          pRelatedVTs = *(SArray**)px;
×
1567
        }
1568
        if (taosArrayGetSize(pRelatedVTs) == 0 || *(int64_t*)taosArrayGetLast(pRelatedVTs) != vTbUid) {
×
1569
          px = taosArrayPush(pRelatedVTs, &vTbUid);
×
1570
          TSDB_CHECK_NULL(px, code, lino, _end, terrno);
×
1571
        }
1572
        i++;
×
1573
        j++;
×
1574
      }
1575
    }
1576
    pIter = taosHashIterate(pVirtualTables, pIter);
×
1577
  }
1578

1579
  pScanInfo->pPhysicalTables = pPhysicalTables;
×
1580
  pPhysicalTables = NULL;
×
1581

1582
  if (taosHashGetSize(pScanInfo->pPhysicalTables) > 0) {
×
1583
    pScanInfo->pPhyTblSchemaCache = taosLRUCacheInit(1024 * 128, -1, .5);
×
1584
    TSDB_CHECK_NULL(pScanInfo->pPhyTblSchemaCache, code, lino, _end, terrno);
×
1585
  }
1586

1587
_end:
×
1588
  if (code != TSDB_CODE_SUCCESS) {
109,259✔
1589
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1590
  }
1591
  if (pIter != NULL) {
109,259✔
1592
    taosHashCancelIterate(pReader->tbIdHash, pIter);
×
1593
  }
1594
  if (pPhysicalTables != NULL) {
109,259✔
1595
    taosHashCleanup(pPhysicalTables);
×
1596
  }
1597
  return code;
109,196✔
1598
}
1599

1600
static void freeTableSchemaCache(const void* key, size_t keyLen, void* value, void* ud) {
×
1601
  if (value) {
×
1602
    SSchemaWrapper* pSchemaWrapper = value;
×
1603
    tDeleteSchemaWrapper(pSchemaWrapper);
1604
  }
1605
}
×
1606

1607
bool tqNextVTableSourceBlockImpl(STqReader* pReader, const char* idstr) {
×
1608
  int32_t            code = TSDB_CODE_SUCCESS;
×
1609
  int32_t            lino = 0;
×
1610
  SVTSourceScanInfo* pScanInfo = NULL;
×
1611

1612
  TSDB_CHECK_NULL(pReader, code, lino, _end, TSDB_CODE_INVALID_PARA);
×
1613

1614
  pScanInfo = &pReader->vtSourceScanInfo;
×
1615
  if (pReader->msg.msgStr == NULL || taosHashGetSize(pScanInfo->pPhysicalTables) == 0) {
×
1616
    return false;
×
1617
  }
1618

1619
  if (pScanInfo->nextVirtualTableIdx >= 0) {
×
1620
    // The data still needs to be converted into the virtual table result block
1621
    return true;
×
1622
  }
1623

1624
  int32_t blockSz = taosArrayGetSize(pReader->submit.aSubmitTbData);
×
1625
  while (pReader->nextBlk < blockSz) {
×
1626
    SSubmitTbData* pSubmitTbData = taosArrayGet(pReader->submit.aSubmitTbData, pReader->nextBlk);
×
1627
    TSDB_CHECK_NULL(pSubmitTbData, code, lino, _end, terrno);
×
1628
    int64_t pTbUid = pSubmitTbData->uid;
×
1629
    void*   px = taosHashGet(pScanInfo->pPhysicalTables, &pTbUid, sizeof(int64_t));
×
1630
    if (px != NULL) {
×
1631
      SArray* pRelatedVTs = *(SArray**)px;
×
1632
      if (taosArrayGetSize(pRelatedVTs) > 0) {
×
1633
        pScanInfo->nextVirtualTableIdx = 0;
×
1634
        return true;
×
1635
      }
1636
    }
1637
    tqTrace("iterator data block in hash jump block, progress:%d/%d, uid:%" PRId64, pReader->nextBlk, blockSz, pTbUid);
×
1638
    pReader->nextBlk++;
×
1639
  }
1640

1641
  tqReaderClearSubmitMsg(pReader);
×
1642
  tqTrace("iterator data block end, total block num:%d", blockSz);
×
1643

1644
_end:
×
1645
  if (code != TSDB_CODE_SUCCESS) {
×
1646
    tqError("%s failed at line %d since %s, id: %s", __func__, lino, tstrerror(code), idstr);
×
1647
  }
1648
  return false;
×
1649
}
1650

1651
bool tqReaderIsQueriedSourceTable(STqReader* pReader, uint64_t uid) {
×
1652
  if (pReader == NULL) {
×
1653
    return false;
×
1654
  }
1655
  return taosHashGet(pReader->vtSourceScanInfo.pPhysicalTables, &uid, sizeof(uint64_t)) != NULL;
×
1656
}
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