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

taosdata / TDengine / #4791

13 Oct 2025 06:50AM UTC coverage: 57.628% (-0.8%) from 58.476%
#4791

push

travis-ci

web-flow
Merge pull request #33213 from taosdata/fix/huoh/timemoe_model_directory

fix: fix tdgpt timemoe model directory

136628 of 303332 branches covered (45.04%)

Branch coverage included in aggregate %.

208121 of 294900 relevant lines covered (70.57%)

4250784.02 hits per line

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

67.79
/source/dnode/vnode/src/vnd/vnodeStream.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 <stdbool.h>
17
#include <stdint.h>
18
#include "nodes.h"
19
#include "osMemPool.h"
20
#include "osMemory.h"
21
#include "scalar.h"
22
#include "streamReader.h"
23
#include "taosdef.h"
24
#include "tarray.h"
25
#include "tcommon.h"
26
#include "tdatablock.h"
27
#include "tdb.h"
28
#include "tdef.h"
29
#include "tencode.h"
30
#include "tglobal.h"
31
#include "thash.h"
32
#include "tlist.h"
33
#include "tmsg.h"
34
#include "tsimplehash.h"
35
#include "vnd.h"
36
#include "vnode.h"
37
#include "vnodeInt.h"
38

39
#define BUILD_OPTION(options, sStreamReaderInfo, _ver, _order, startTime, endTime, _schemas, _isSchema, _scanMode, \
40
                     _gid, _initReader, _mapInfo)                                                                        \
41
  SStreamTriggerReaderTaskInnerOptions options = {.suid = (_mapInfo == NULL ? sStreamReaderInfo->suid : 0),              \
42
                                                  .ver = _ver,                                                     \
43
                                                  .order = _order,                                                 \
44
                                                  .twindows = {.skey = startTime, .ekey = endTime},                \
45
                                                  .sStreamReaderInfo = sStreamReaderInfo,                     \
46
                                                  .schemas = _schemas,                                             \
47
                                                  .isSchema = _isSchema,                                           \
48
                                                  .scanMode = _scanMode,                                           \
49
                                                  .gid = _gid,                                                     \
50
                                                  .initReader = _initReader,                                       \
51
                                                  .mapInfo = _mapInfo};
52

53
typedef struct WalMetaResult {
54
  uint64_t    id;
55
  int64_t     skey;
56
  int64_t     ekey;
57
} WalMetaResult;
58

59
static int64_t getSessionKey(int64_t session, int64_t type) { return (session | (type << 32)); }
29,368✔
60

61
static int32_t buildScheamFromMeta(SVnode* pVnode, int64_t uid, SArray** schemas);
62

63
int32_t sortCid(const void *lp, const void *rp) {
215✔
64
  int16_t* c1 = (int16_t*)lp;
215✔
65
  int16_t* c2 = (int16_t*)rp;
215✔
66

67
  if (*c1 < *c2) {
215✔
68
    return -1;
213✔
69
  } else if (*c1 > *c2) {
2!
70
    return 1;
2✔
71
  }
72

73
  return 0;
×
74
}
75

76
int32_t sortSSchema(const void *lp, const void *rp) {
215✔
77
  SSchema* c1 = (SSchema*)lp;
215✔
78
  SSchema* c2 = (SSchema*)rp;
215✔
79

80
  if (c1->colId < c2->colId) {
215✔
81
    return -1;
213✔
82
  } else if (c1->colId > c2->colId) {
2!
83
    return 1;
2✔
84
  }
85

86
  return 0;
×
87
}
88

89
static int32_t addColData(SSDataBlock* pResBlock, int32_t index, void* data) {
25,673✔
90
  SColumnInfoData* pSrc = taosArrayGet(pResBlock->pDataBlock, index);
25,673✔
91
  if (pSrc == NULL) {
25,665!
92
    return terrno;
×
93
  }
94

95
  memcpy(pSrc->pData + pResBlock->info.rows * pSrc->info.bytes, data, pSrc->info.bytes);
25,666✔
96
  return 0;
25,666✔
97
}
98

99
static int32_t getTableDataInfo(SStreamReaderTaskInner* pTask, bool* hasNext) {
33,961✔
100
  int32_t code = pTask->api.tsdReader.tsdNextDataBlock(pTask->pReader, hasNext);
33,961✔
101
  if (code != TSDB_CODE_SUCCESS) {
33,961!
102
    pTask->api.tsdReader.tsdReaderReleaseDataBlock(pTask->pReader);
×
103
  }
104

105
  return code;
33,961✔
106
}
107

108
static int32_t getTableData(SStreamReaderTaskInner* pTask, SSDataBlock** ppRes) {
2,860✔
109
  return pTask->api.tsdReader.tsdReaderRetrieveDataBlock(pTask->pReader, ppRes, NULL);
2,860✔
110
}
111

112
static int32_t buildOTableInfoRsp(const SSTriggerOrigTableInfoRsp* rsp, void** data, size_t* size) {
106✔
113
  int32_t code = 0;
106✔
114
  int32_t lino = 0;
106✔
115
  void*   buf = NULL;
106✔
116
  int32_t len = tSerializeSTriggerOrigTableInfoRsp(NULL, 0, rsp);
106✔
117
  STREAM_CHECK_CONDITION_GOTO(len <= 0, TSDB_CODE_INVALID_PARA);
106!
118
  buf = rpcMallocCont(len);
106✔
119
  STREAM_CHECK_NULL_GOTO(buf, terrno);
106!
120
  int32_t actLen = tSerializeSTriggerOrigTableInfoRsp(buf, len, rsp);
106✔
121
  STREAM_CHECK_CONDITION_GOTO(actLen != len, TSDB_CODE_INVALID_PARA);
106!
122
  *data = buf;
106✔
123
  *size = len;
106✔
124
  buf = NULL;
106✔
125
end:
106✔
126
  rpcFreeCont(buf);
106✔
127
  return code;
106✔
128
}
129

130
static bool needRefreshTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int8_t tableType, int64_t suid, int64_t uid, bool isCalc){
2,592✔
131
  if (sStreamReaderInfo->isVtableStream) {
2,592✔
132
    int64_t id[2] = {suid, uid};
1,183✔
133
    if(tSimpleHashGet(isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, id, sizeof(id)) == NULL) {
1,183!
134
      return true;
1,175✔
135
    }
136
  } else {
137
    if (tableType != TD_CHILD_TABLE) {
1,409✔
138
      return false;
458✔
139
    }
140
    if (sStreamReaderInfo->tableType == TD_SUPER_TABLE && 
951✔
141
        suid == sStreamReaderInfo->suid && 
581✔
142
        qStreamGetGroupId(sStreamReaderInfo->tableList, uid) == -1) {
23✔
143
      return true;
9✔
144
    }
145
  }
146
  return false;
950✔
147
}
148

149
static bool uidInTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id, bool isCalc){
36,600✔
150
  if (sStreamReaderInfo->isVtableStream) {
36,600✔
151
    int64_t tmp[2] = {suid, uid};
14,086✔
152
    if(tSimpleHashGet(isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, tmp, sizeof(tmp)) == NULL) {
14,086✔
153
      return false;
5,462✔
154
    }
155
    *id = uid;
8,725✔
156
  } else {
157
    if (sStreamReaderInfo->tableList == NULL) return false;
22,514!
158

159
    if (sStreamReaderInfo->tableType == TD_SUPER_TABLE) {
22,514✔
160
      if (suid != sStreamReaderInfo->suid) return false;
14,382✔
161
      if (sStreamReaderInfo->pTagCond == NULL) {
10,823✔
162
        if (sStreamReaderInfo->partitionCols == NULL){
9,308✔
163
          *id = 0;
40✔
164
        } else if (sStreamReaderInfo->groupByTbname){
9,268✔
165
          *id= uid;
8,567✔
166
        } else {
167
          *id = qStreamGetGroupId(sStreamReaderInfo->tableList, uid);
701✔
168
          if (*id == -1) return false;
704!
169
        }
170
      } else {
171
        //*id= uid;
172
        *id = qStreamGetGroupId(sStreamReaderInfo->tableList, uid);
1,515✔
173
        if (*id == -1) return false;
1,631✔
174
      }
175
    } else {
176
      *id = qStreamGetGroupId(sStreamReaderInfo->tableList, uid);
8,132✔
177
      if(*id == -1) *id = uid;
8,509✔
178
      return uid == sStreamReaderInfo->uid;
8,509✔
179
    }
180
  }
181
  return true;
18,980✔
182
}
183

184
static int32_t generateTablistForStreamReader(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo, bool isHistory) {
3,330✔
185
  int32_t                   code = 0;
3,330✔
186
  int32_t                   lino = 0;
3,330✔
187
  SNodeList* groupNew = NULL;                                      
3,330✔
188
  STREAM_CHECK_RET_GOTO(nodesCloneList(sStreamReaderInfo->partitionCols, &groupNew));
3,330!
189

190
  SStorageAPI api = {0};
3,330✔
191
  initStorageAPI(&api);
3,330✔
192
  code = qStreamCreateTableListForReader(pVnode, sStreamReaderInfo->suid, sStreamReaderInfo->uid, sStreamReaderInfo->tableType, groupNew,
3,329✔
193
                                         true, sStreamReaderInfo->pTagCond, sStreamReaderInfo->pTagIndexCond, &api, 
194
                                         isHistory ? &sStreamReaderInfo->historyTableList : &sStreamReaderInfo->tableList,
195
                                         isHistory ? NULL : sStreamReaderInfo->groupIdMap);
196
  end:
3,330✔
197
  nodesDestroyList(groupNew);
3,330✔
198
  STREAM_PRINT_LOG_END(code, lino);
3,330!
199
  return code;
3,330✔
200
}
201

202
static int32_t buildVTableInfoRsp(const SStreamMsgVTableInfo* rsp, void** data, size_t* size) {
494✔
203
  int32_t code = 0;
494✔
204
  int32_t lino = 0;
494✔
205
  void*   buf = NULL;
494✔
206
  int32_t len = tSerializeSStreamMsgVTableInfo(NULL, 0, rsp);
494✔
207
  STREAM_CHECK_CONDITION_GOTO(len <= 0, TSDB_CODE_INVALID_PARA);
494!
208
  buf = rpcMallocCont(len);
494✔
209
  STREAM_CHECK_NULL_GOTO(buf, terrno);
494!
210
  int32_t actLen = tSerializeSStreamMsgVTableInfo(buf, len, rsp);
494✔
211
  STREAM_CHECK_CONDITION_GOTO(actLen != len, TSDB_CODE_INVALID_PARA);
494!
212
  *data = buf;
494✔
213
  *size = len;
494✔
214
  buf = NULL;
494✔
215
end:
494✔
216
  rpcFreeCont(buf);
494✔
217
  return code;
494✔
218
}
219

220
static int32_t buildTsRsp(const SStreamTsResponse* tsRsp, void** data, size_t* size) {
824✔
221
  int32_t code = 0;
824✔
222
  int32_t lino = 0;
824✔
223
  void*   buf = NULL;
824✔
224
  int32_t len = tSerializeSStreamTsResponse(NULL, 0, tsRsp);
824✔
225
  STREAM_CHECK_CONDITION_GOTO(len <= 0, TSDB_CODE_INVALID_PARA);
825!
226
  buf = rpcMallocCont(len);
825✔
227
  STREAM_CHECK_NULL_GOTO(buf, terrno);
824!
228
  int32_t actLen = tSerializeSStreamTsResponse(buf, len, tsRsp);
824✔
229
  STREAM_CHECK_CONDITION_GOTO(actLen != len, TSDB_CODE_INVALID_PARA);
825!
230
  *data = buf;
825✔
231
  *size = len;
825✔
232
  buf = NULL;
825✔
233
end:
825✔
234
  rpcFreeCont(buf);
825✔
235
  return code;
823✔
236
}
237

238

239
static int32_t buildRsp(SSDataBlock* pBlock, void** data, size_t* size) {
29,789✔
240
  int32_t code = 0;
29,789✔
241
  int32_t lino = 0;
29,789✔
242
  void*   buf = NULL;
29,789✔
243
  STREAM_CHECK_CONDITION_GOTO(pBlock == NULL || pBlock->info.rows == 0, TSDB_CODE_SUCCESS);
29,789!
244
  size_t dataEncodeSize = blockGetEncodeSize(pBlock);
1,897✔
245
  buf = rpcMallocCont(dataEncodeSize);
1,897✔
246
  STREAM_CHECK_NULL_GOTO(buf, terrno);
1,896!
247
  int32_t actualLen = blockEncode(pBlock, buf, dataEncodeSize, taosArrayGetSize(pBlock->pDataBlock));
1,896✔
248
  STREAM_CHECK_CONDITION_GOTO(actualLen < 0, terrno);
1,897!
249
  *data = buf;
1,897✔
250
  *size = dataEncodeSize;
1,897✔
251
  buf = NULL;
1,897✔
252
end:
29,789✔
253
  rpcFreeCont(buf);
29,789✔
254
  return code;
29,789✔
255
}
256

257
static int32_t resetTsdbReader(SStreamReaderTaskInner* pTask) {
746✔
258
  int32_t        pNum = 1;
746✔
259
  STableKeyInfo* pList = NULL;
746✔
260
  int32_t        code = 0;
746✔
261
  int32_t        lino = 0;
746✔
262
  STREAM_CHECK_RET_GOTO(qStreamGetTableList(pTask->pTableList, pTask->currentGroupIndex, &pList, &pNum));
746!
263
  if (pList == NULL || pNum == 0) {
747!
264
    code = TSDB_CODE_INVALID_PARA;
×
265
    goto end;
×
266
  }
267
  STREAM_CHECK_RET_GOTO(pTask->api.tsdReader.tsdSetQueryTableList(pTask->pReader, pList, pNum));
747!
268

269
  cleanupQueryTableDataCond(&pTask->cond);
747✔
270
  uint64_t suid = pTask->options.sStreamReaderInfo->isVtableStream ? pList->groupId : pTask->options.suid;
747✔
271
  STREAM_CHECK_RET_GOTO(qStreamInitQueryTableDataCond(&pTask->cond, pTask->options.order, pTask->options.schemas, true,
747!
272
                                                      pTask->options.twindows, suid, pTask->options.ver, NULL));
273
  STREAM_CHECK_RET_GOTO(pTask->api.tsdReader.tsdReaderResetStatus(pTask->pReader, &pTask->cond));
747!
274

275
end:
747✔
276
  STREAM_PRINT_LOG_END(code, lino);
747!
277
  return code;
747✔
278
}
279

280
static int32_t buildWalMetaBlock(SSDataBlock* pBlock, int8_t type, int64_t id, bool isVTable, int64_t uid,
×
281
                                 int64_t skey, int64_t ekey, int64_t ver, int64_t rows) {
282
  int32_t code = 0;
×
283
  int32_t lino = 0;
×
284
  int32_t index = 0;
×
285
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &type));
×
286
  if (!isVTable) {
×
287
    STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
×
288
  }
289
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &uid));
×
290
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &skey));
×
291
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ekey));
×
292
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
×
293
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &rows));
×
294

295
end:
×
296
  // STREAM_PRINT_LOG_END(code, lino)
297
  return code;
×
298
}
299

300
static int32_t buildWalMetaBlockNew(SSDataBlock* pBlock, int64_t id, int64_t skey, int64_t ekey, int64_t ver) {
6,245✔
301
  int32_t code = 0;
6,245✔
302
  int32_t lino = 0;
6,245✔
303
  int32_t index = 0;
6,245✔
304
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
6,245!
305
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &skey));
6,242!
306
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ekey));
6,221!
307
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
6,205!
308

309
end:
6,196✔
310
  return code;
6,196✔
311
}
312

313
static int32_t buildDropTableBlock(SSDataBlock* pBlock, int64_t id, int64_t ver) {
1✔
314
  int32_t code = 0;
1✔
315
  int32_t lino = 0;
1✔
316
  int32_t index = 0;
1✔
317
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
1!
318
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
1!
319

320
end:
1✔
321
  return code;
1✔
322
}
323

324
static void buildTSchema(STSchema* pTSchema, int32_t ver, col_id_t colId, int8_t type, int32_t bytes) {
×
325
  pTSchema->numOfCols = 1;
×
326
  pTSchema->version = ver;
×
327
  pTSchema->columns[0].colId = colId;
×
328
  pTSchema->columns[0].type = type;
×
329
  pTSchema->columns[0].bytes = bytes;
×
330
}
×
331

332
static int32_t scanDeleteDataNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len,
111✔
333
                              int64_t ver) {
334
  int32_t    code = 0;
111✔
335
  int32_t    lino = 0;
111✔
336
  SDecoder   decoder = {0};
111✔
337
  SDeleteRes req = {0};
111✔
338
  void* pTask = sStreamReaderInfo->pTask;
111✔
339

340
  req.uidList = taosArrayInit(0, sizeof(tb_uid_t));
111✔
341
  tDecoderInit(&decoder, data, len);
112✔
342
  STREAM_CHECK_RET_GOTO(tDecodeDeleteRes(&decoder, &req));
112!
343
  STREAM_CHECK_CONDITION_GOTO((sStreamReaderInfo->tableType == TSDB_SUPER_TABLE && !sStreamReaderInfo->isVtableStream && req.suid != sStreamReaderInfo->suid), TDB_CODE_SUCCESS);
112✔
344
  
345
  for (int32_t i = 0; i < taosArrayGetSize(req.uidList); i++) {
169✔
346
    uint64_t* uid = taosArrayGet(req.uidList, i);
97✔
347
    STREAM_CHECK_NULL_GOTO(uid, terrno);
122!
348
    uint64_t   id = 0;
97✔
349
    ST_TASK_ILOG("stream reader scan delete start data:uid %" PRIu64 ", skey %" PRIu64 ", ekey %" PRIu64, *uid, req.skey, req.ekey);
97!
350
    STREAM_CHECK_CONDITION_GOTO(!uidInTableList(sStreamReaderInfo, req.suid, *uid, &id, false), TDB_CODE_SUCCESS);
97✔
351
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->deleteBlock, ((SSDataBlock*)rsp->deleteBlock)->info.rows + 1));
72!
352
    STREAM_CHECK_RET_GOTO(buildWalMetaBlockNew(rsp->deleteBlock, id, req.skey, req.ekey, ver));
72!
353
    ((SSDataBlock*)rsp->deleteBlock)->info.rows++;
72✔
354
    rsp->totalRows++;
72✔
355
  }
356

357
end:
72✔
358
  taosArrayDestroy(req.uidList);
112✔
359
  tDecoderClear(&decoder);
112✔
360
  return code;
112✔
361
}
362

363
static int32_t scanDropTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len,
1✔
364
                             int64_t ver) {
365
  int32_t  code = 0;
1✔
366
  int32_t  lino = 0;
1✔
367
  SDecoder decoder = {0};
1✔
368
  void* pTask = sStreamReaderInfo->pTask;
1✔
369

370
  SVDropTbBatchReq req = {0};
1✔
371
  tDecoderInit(&decoder, data, len);
1✔
372
  STREAM_CHECK_RET_GOTO(tDecodeSVDropTbBatchReq(&decoder, &req));
1!
373

374
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
2✔
375
    SVDropTbReq* pDropTbReq = req.pReqs + iReq;
1✔
376
    STREAM_CHECK_NULL_GOTO(pDropTbReq, TSDB_CODE_INVALID_PARA);
1!
377
    uint64_t id = 0;
1✔
378
    if(!uidInTableList(sStreamReaderInfo, pDropTbReq->suid, pDropTbReq->uid, &id, false)) {
1!
379
      continue;
×
380
    }
381

382
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->dropBlock, ((SSDataBlock*)rsp->dropBlock)->info.rows + 1));
1!
383
    STREAM_CHECK_RET_GOTO(buildDropTableBlock(rsp->dropBlock, id, ver));
1!
384
    ((SSDataBlock*)rsp->dropBlock)->info.rows++;
1✔
385
    rsp->totalRows++;
1✔
386
    ST_TASK_ILOG("stream reader scan drop uid %" PRId64 ", id %" PRIu64, pDropTbReq->uid, id);
1!
387
  }
388

389
end:
1✔
390
  tDecoderClear(&decoder);
1✔
391
  return code;
1✔
392
}
393

394
static int32_t reloadTableList(SStreamTriggerReaderInfo* sStreamReaderInfo){
1,205✔
395
  (void)taosThreadMutexLock(&sStreamReaderInfo->mutex);
1,205✔
396
  qStreamDestroyTableList(sStreamReaderInfo->tableList);
1,205✔
397
  sStreamReaderInfo->tableList = NULL;
1,205✔
398
  int32_t code = generateTablistForStreamReader(sStreamReaderInfo->pVnode, sStreamReaderInfo, false);
1,205✔
399
  if (code == 0){
1,205!
400
    qStreamDestroyTableList(sStreamReaderInfo->historyTableList);
1,205✔
401
    sStreamReaderInfo->historyTableList = NULL;
1,205✔
402
    code = generateTablistForStreamReader(sStreamReaderInfo->pVnode, sStreamReaderInfo, true);
1,205✔
403
  }
404
  (void)taosThreadMutexUnlock(&sStreamReaderInfo->mutex);
1,205✔
405
  return code;
1,205✔
406
}
407

408
static int32_t scanCreateTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len) {
104✔
409
  int32_t  code = 0;
104✔
410
  int32_t  lino = 0;
104✔
411
  SDecoder decoder = {0};
104✔
412
  void* pTask = sStreamReaderInfo->pTask;
104✔
413

414
  SVCreateTbBatchReq req = {0};
104✔
415
  tDecoderInit(&decoder, data, len);
104✔
416
  
417
  STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbBatchReq(&decoder, &req));
104!
418

419
  bool found = false;
104✔
420
  SVCreateTbReq* pCreateReq = NULL;
104✔
421
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
157✔
422
    pCreateReq = req.pReqs + iReq;
104✔
423
    if (!needRefreshTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false)) {
104✔
424
      ST_TASK_ILOG("stream reader scan create table jump, %s", pCreateReq->name);
53!
425
      continue;
53✔
426
    }
427
    ST_TASK_ILOG("stream reader scan create table %s", pCreateReq->name);
51!
428

429
    found = true;
51✔
430
    break;
51✔
431
  }
432
  STREAM_CHECK_CONDITION_GOTO(!found, TDB_CODE_SUCCESS);
104✔
433

434
  STREAM_CHECK_RET_GOTO(reloadTableList(sStreamReaderInfo));
51!
435
end:
51✔
436
  tDeleteSVCreateTbBatchReq(&req);
104✔
437
  tDecoderClear(&decoder);
104✔
438
  return code;
104✔
439
}
440

441
static int32_t processAutoCreateTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SVCreateTbReq* pCreateReq) {
2,491✔
442
  int32_t  code = 0;
2,491✔
443
  int32_t  lino = 0;
2,491✔
444
  void*    pTask = sStreamReaderInfo->pTask;
2,491✔
445
  if (!needRefreshTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false)) {
2,491✔
446
    ST_TASK_DLOG("stream reader scan auto create table jump, %s", pCreateReq->name);
1,357✔
447
    goto end;
1,357✔
448
  }
449
  ST_TASK_ILOG("stream reader scan auto create table %s", pCreateReq->name);
1,133!
450

451
  STREAM_CHECK_RET_GOTO(reloadTableList(sStreamReaderInfo));
1,133!
452
end:
1,133✔
453
  return code;
2,490✔
454
}
455

456
static int32_t scanAlterTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len) {
143✔
457
  int32_t  code = 0;
143✔
458
  int32_t  lino = 0;
143✔
459
  SDecoder decoder = {0};
143✔
460
  void* pTask = sStreamReaderInfo->pTask;
143✔
461

462
  SVAlterTbReq req = {0};
143✔
463
  tDecoderInit(&decoder, data, len);
143✔
464
  
465
  STREAM_CHECK_RET_GOTO(tDecodeSVAlterTbReq(&decoder, &req));
143!
466
  STREAM_CHECK_CONDITION_GOTO(req.action != TSDB_ALTER_TABLE_UPDATE_TAG_VAL && req.action != TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL, TDB_CODE_SUCCESS);
144!
467

468
  ETableType tbType = 0;
36✔
469
  uint64_t suid = 0;
36✔
470
  STREAM_CHECK_RET_GOTO(metaGetTableTypeSuidByName(sStreamReaderInfo->pVnode, req.tbName, &tbType, &suid));
36!
471
  STREAM_CHECK_CONDITION_GOTO(tbType != TSDB_CHILD_TABLE, TDB_CODE_SUCCESS);
36!
472
  STREAM_CHECK_CONDITION_GOTO(suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
36✔
473

474
  STREAM_CHECK_RET_GOTO(reloadTableList(sStreamReaderInfo));
21!
475
  ST_TASK_ILOG("stream reader scan alter table %s", req.tbName);
21!
476

477
end:
×
478
  taosArrayDestroy(req.pMultiTag);
144✔
479
  tDecoderClear(&decoder);
144✔
480
  return code;
144✔
481
}
482

483
// static int32_t scanAlterSTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len) {
484
//   int32_t  code = 0;
485
//   int32_t  lino = 0;
486
//   SDecoder decoder = {0};
487
//   SMAlterStbReq reqAlter = {0};
488
//   SVCreateStbReq req = {0};
489
//   tDecoderInit(&decoder, data, len);
490
//   void* pTask = sStreamReaderInfo->pTask;
491
  
492
//   STREAM_CHECK_RET_GOTO(tDecodeSVCreateStbReq(&decoder, &req));
493
//   STREAM_CHECK_CONDITION_GOTO(req.suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
494
//   if (req.alterOriData != 0) {
495
//     STREAM_CHECK_RET_GOTO(tDeserializeSMAlterStbReq(req.alterOriData, req.alterOriDataLen, &reqAlter));
496
//     STREAM_CHECK_CONDITION_GOTO(reqAlter.alterType != TSDB_ALTER_TABLE_DROP_TAG && reqAlter.alterType != TSDB_ALTER_TABLE_UPDATE_TAG_NAME, TDB_CODE_SUCCESS);
497
//   }
498
  
499
//   STREAM_CHECK_RET_GOTO(reloadTableList(sStreamReaderInfo));
500

501
//   ST_TASK_ILOG("stream reader scan alter suid %" PRId64, req.suid);
502
// end:
503
//   tFreeSMAltertbReq(&reqAlter);
504
//   tDecoderClear(&decoder);
505
//   return code;
506
// }
507

508
static int32_t scanDropSTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len) {
×
509
  int32_t  code = 0;
×
510
  int32_t  lino = 0;
×
511
  SDecoder decoder = {0};
×
512
  void* pTask = sStreamReaderInfo->pTask;
×
513

514
  SVDropStbReq req = {0};
×
515
  tDecoderInit(&decoder, data, len);
×
516
  STREAM_CHECK_RET_GOTO(tDecodeSVDropStbReq(&decoder, &req));
×
517
  STREAM_CHECK_CONDITION_GOTO(req.suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
×
518
  STREAM_CHECK_RET_GOTO(reloadTableList(sStreamReaderInfo));
×
519

520
  ST_TASK_ILOG("stream reader scan drop suid %" PRId64, req.suid);
×
521
end:
×
522
  tDecoderClear(&decoder);
×
523
  return code;
×
524
}
525

526
static int32_t scanSubmitTbDataForMeta(SDecoder *pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo, SSHashObj* gidHash) {
9,307✔
527
  int32_t code = 0;
9,307✔
528
  int32_t lino = 0;
9,307✔
529
  WalMetaResult walMeta = {0};
9,307✔
530
  SSubmitTbData submitTbData = {0};
9,307✔
531
  
532
  if (tStartDecode(pCoder) < 0) {
9,307!
533
    code = TSDB_CODE_INVALID_MSG;
×
534
    TSDB_CHECK_CODE(code, lino, end);
×
535
  }
536

537
  uint8_t       version = 0;
9,295✔
538
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
9,247!
539
    code = TSDB_CODE_INVALID_MSG;
×
540
    TSDB_CHECK_CODE(code, lino, end);
×
541
  }
542
  version = (submitTbData.flags >> 8) & 0xff;
9,247✔
543
  submitTbData.flags = submitTbData.flags & 0xff;
9,247✔
544

545
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
546
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
9,247✔
547
    submitTbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
1,320!
548
    STREAM_CHECK_NULL_GOTO(submitTbData.pCreateTbReq, terrno);
1,320!
549
    STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbReq(pCoder, submitTbData.pCreateTbReq));
1,320!
550
    STREAM_CHECK_RET_GOTO(processAutoCreateTableNew(sStreamReaderInfo, submitTbData.pCreateTbReq));
1,320!
551
  }
552

553
  // submit data
554
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
9,236!
555
    code = TSDB_CODE_INVALID_MSG;
×
556
    TSDB_CHECK_CODE(code, lino, end);
×
557
  }
558
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
9,215!
559
    code = TSDB_CODE_INVALID_MSG;
×
560
    TSDB_CHECK_CODE(code, lino, end);
×
561
  }
562

563
  if (!uidInTableList(sStreamReaderInfo, submitTbData.suid, submitTbData.uid, &walMeta.id, false)){
9,215✔
564
    goto end;
6,572✔
565
  }
566
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
2,750!
567
    code = TSDB_CODE_INVALID_MSG;
×
568
    TSDB_CHECK_CODE(code, lino, end);
×
569
  }
570

571
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
2,750!
572
    uint64_t nColData = 0;
×
573
    if (tDecodeU64v(pCoder, &nColData) < 0) {
×
574
      code = TSDB_CODE_INVALID_MSG;
×
575
      TSDB_CHECK_CODE(code, lino, end);
×
576
    }
577

578
    SColData colData = {0};
×
579
    code = tDecodeColData(version, pCoder, &colData, false);
×
580
    if (code) {
×
581
      code = TSDB_CODE_INVALID_MSG;
×
582
      TSDB_CHECK_CODE(code, lino, end);
×
583
    }
584

585
    if (colData.flag != HAS_VALUE) {
×
586
      code = TSDB_CODE_INVALID_MSG;
×
587
      TSDB_CHECK_CODE(code, lino, end);
×
588
    }
589
    walMeta.skey = ((TSKEY *)colData.pData)[0];
×
590
    walMeta.ekey = ((TSKEY *)colData.pData)[colData.nVal - 1];
×
591

592
    for (uint64_t i = 1; i < nColData; i++) {
×
593
      code = tDecodeColData(version, pCoder, &colData, true);
×
594
      if (code) {
×
595
        code = TSDB_CODE_INVALID_MSG;
×
596
        TSDB_CHECK_CODE(code, lino, end);
×
597
      }
598
    }
599
  } else {
600
    uint64_t nRow = 0;
2,750✔
601
    if (tDecodeU64v(pCoder, &nRow) < 0) {
2,747!
602
      code = TSDB_CODE_INVALID_MSG;
×
603
      TSDB_CHECK_CODE(code, lino, end);
×
604
    }
605

606
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
10,648✔
607
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
7,901✔
608
      pCoder->pos += pRow->len;
7,901✔
609
      if (iRow == 0){
7,901✔
610
#ifndef NO_UNALIGNED_ACCESS
611
        walMeta.skey = pRow->ts;
2,747✔
612
#else
613
        walMeta.skey = taosGetInt64Aligned(&pRow->ts);
614
#endif
615
      }
616
      if (iRow == nRow - 1) {
7,901✔
617
#ifndef NO_UNALIGNED_ACCESS
618
        walMeta.ekey = pRow->ts;
2,746✔
619
#else
620
        walMeta.ekey = taosGetInt64Aligned(&pRow->ts);
621
#endif
622
      }
623
    }
624
  }
625

626
  WalMetaResult* data = (WalMetaResult*)tSimpleHashGet(gidHash, &walMeta.id, LONG_BYTES);
2,747✔
627
  if (data != NULL) {
2,744!
628
    if (walMeta.skey < data->skey) data->skey = walMeta.skey;
×
629
    if (walMeta.ekey > data->ekey) data->ekey = walMeta.ekey;
×
630
  } else {
631
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(gidHash, &walMeta.id, LONG_BYTES, &walMeta, sizeof(WalMetaResult)));
2,744!
632
  }
633

634
end:
2,744✔
635
  tDestroySVSubmitCreateTbReq(submitTbData.pCreateTbReq, TSDB_MSG_FLG_DECODE);
9,316✔
636
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
9,294!
637
  tEndDecode(pCoder);
9,294✔
638
  return code;
9,278✔
639
}
640

641
static int32_t scanSubmitDataForMeta(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len, int64_t ver) {
9,317✔
642
  int32_t  code = 0;
9,317✔
643
  int32_t  lino = 0;
9,317✔
644
  SDecoder decoder = {0};
9,317✔
645
  SSHashObj* gidHash = NULL;
9,317✔
646
  void* pTask = sStreamReaderInfo->pTask;
9,317✔
647

648
  tDecoderInit(&decoder, data, len);
9,317✔
649
  if (tStartDecode(&decoder) < 0) {
9,300!
650
    code = TSDB_CODE_INVALID_MSG;
×
651
    TSDB_CHECK_CODE(code, lino, end);
×
652
  }
653

654
  uint64_t nSubmitTbData = 0;
9,320✔
655
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
9,297!
656
    code = TSDB_CODE_INVALID_MSG;
×
657
    TSDB_CHECK_CODE(code, lino, end);
×
658
  }
659

660
  gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
9,297✔
661
  STREAM_CHECK_NULL_GOTO(gidHash, terrno);
9,318!
662

663
  for (int32_t i = 0; i < nSubmitTbData; i++) {
18,585✔
664
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataForMeta(&decoder, sStreamReaderInfo, gidHash));
9,312!
665
  }
666
  tEndDecode(&decoder);
9,273✔
667

668
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
9,243!
669
  int32_t iter = 0;
9,223✔
670
  void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
9,223✔
671
  while (px != NULL) {
12,016✔
672
    WalMetaResult* pMeta = (WalMetaResult*)px;
2,739✔
673
    STREAM_CHECK_RET_GOTO(buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver));
2,739!
674
    ((SSDataBlock*)rsp->metaBlock)->info.rows++;
2,728✔
675
    rsp->totalRows++;
2,728✔
676
    ST_TASK_DLOG("stream reader scan submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
2,728✔
677
          ", ver:%"PRId64, pMeta->skey, pMeta->ekey, pMeta->id, ver);
678
    px = tSimpleHashIterate(gidHash, px, &iter);
2,730✔
679
  }
680
end:
9,277✔
681
  tDecoderClear(&decoder);
9,277✔
682
  tSimpleHashCleanup( gidHash);
9,335✔
683
  return code;
9,324✔
684
}
685

686
static int32_t createBlockForTsdbMeta(SSDataBlock** pBlock, bool isVTable) {
743✔
687
  int32_t code = 0;
743✔
688
  int32_t lino = 0;
743✔
689
  SArray* schemas = taosArrayInit(8, sizeof(SSchema));
743✔
690
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
743!
691

692
  int32_t index = 1;
743✔
693
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_TIMESTAMP, LONG_BYTES, index++))  // skey
743!
694
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_TIMESTAMP, LONG_BYTES, index++))  // ekey
743!
695
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // uid
743!
696
  if (!isVTable) {
743✔
697
    STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_UBIGINT, LONG_BYTES, index++))  // gid
134!
698
  }
699
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))     // nrows
743!
700

701
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
743!
702

703
end:
743✔
704
  taosArrayDestroy(schemas);
743✔
705
  return code;
743✔
706
}
707

708
static int32_t createBlockForWalMetaNew(SSDataBlock** pBlock) {
500✔
709
  int32_t code = 0;
500✔
710
  int32_t lino = 0;
500✔
711
  SArray* schemas = NULL;
500✔
712

713
  schemas = taosArrayInit(8, sizeof(SSchema));
500✔
714
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
501!
715

716
  int32_t index = 0;
501✔
717
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // gid non vtable/uid vtable
501!
718
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // skey
501!
719
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // ekey
501!
720
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // ver
501!
721

722
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
500!
723

724
end:
501✔
725
  taosArrayDestroy(schemas);
501✔
726
  return code;
501✔
727
}
728

729
static int32_t createBlockForDropTable(SSDataBlock** pBlock) {
1✔
730
  int32_t code = 0;
1✔
731
  int32_t lino = 0;
1✔
732
  SArray* schemas = NULL;
1✔
733

734
  schemas = taosArrayInit(8, sizeof(SSchema));
1✔
735
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
1!
736

737
  int32_t index = 0;
1✔
738
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // gid non vtable/uid vtable
1!
739
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // ver
1!
740

741
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
1!
742

743
end:
1✔
744
  taosArrayDestroy(schemas);
1✔
745
  return code;
1✔
746
}
747

748
static int32_t processMeta(int16_t msgType, SStreamTriggerReaderInfo* sStreamReaderInfo, void *data, int32_t len, SSTriggerWalNewRsp* rsp, int32_t ver) {
815✔
749
  int32_t code = 0;
815✔
750
  int32_t lino = 0;
815✔
751
  SDecoder dcoder = {0};
815✔
752
  tDecoderInit(&dcoder, data, len);
815✔
753
  if (msgType == TDMT_VND_DELETE && sStreamReaderInfo->deleteReCalc != 0) {
814✔
754
    if (rsp->deleteBlock == NULL) {
111✔
755
      STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&rsp->deleteBlock));
42!
756
    }
757
      
758
    STREAM_CHECK_RET_GOTO(scanDeleteDataNew(sStreamReaderInfo, rsp, data, len, ver));
111!
759
  } else if (msgType == TDMT_VND_DROP_TABLE && sStreamReaderInfo->deleteOutTbl != 0) {
703✔
760
    if (rsp->dropBlock == NULL) {
1!
761
      STREAM_CHECK_RET_GOTO(createBlockForDropTable((SSDataBlock**)&rsp->dropBlock));
1!
762
    }
763
    STREAM_CHECK_RET_GOTO(scanDropTableNew(sStreamReaderInfo, rsp, data, len, ver));
1!
764
  } else if (msgType == TDMT_VND_DROP_STB) {
702!
765
    STREAM_CHECK_RET_GOTO(scanDropSTableNew(sStreamReaderInfo, data, len));
×
766
  } else if (msgType == TDMT_VND_CREATE_TABLE) {
702✔
767
    STREAM_CHECK_RET_GOTO(scanCreateTableNew(sStreamReaderInfo, data, len));
104!
768
  } else if (msgType == TDMT_VND_ALTER_STB) {
598✔
769
    // STREAM_CHECK_RET_GOTO(scanAlterSTableNew(sStreamReaderInfo, data, len));
770
  } else if (msgType == TDMT_VND_ALTER_TABLE) {
405✔
771
    STREAM_CHECK_RET_GOTO(scanAlterTableNew(sStreamReaderInfo, data, len));
143!
772
  }
773

774
  end:
406✔
775
  tDecoderClear(&dcoder);
816✔
776
  return code;
815✔
777
}
778
static int32_t processWalVerMetaNew(SVnode* pVnode, SSTriggerWalNewRsp* rsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
10,211✔
779
                       int64_t ctime) {
780
  int32_t code = 0;
10,211✔
781
  int32_t lino = 0;
10,211✔
782
  void* pTask = sStreamReaderInfo->pTask;
10,211✔
783

784
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
10,211✔
785
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
10,228!
786
  code = walReaderSeekVer(pWalReader, rsp->ver);
10,228✔
787
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
10,225✔
788
    if (rsp->ver < walGetFirstVer(pWalReader->pWal)) {
9,364!
789
      rsp->ver = walGetFirstVer(pWalReader->pWal);
×
790
    }
791
    ST_TASK_DLOG("vgId:%d %s scan wal error:%s", TD_VID(pVnode), __func__, tstrerror(code));
9,360✔
792
    code = TSDB_CODE_SUCCESS;
9,364✔
793
    goto end;
9,364✔
794
  }
795
  STREAM_CHECK_RET_GOTO(code);
861!
796

797
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, STREAM_RETURN_ROWS_NUM));
861!
798
  while (1) {
9,763✔
799
    code = walNextValidMsg(pWalReader, true);
10,625✔
800
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){\
10,624✔
801
      ST_TASK_DLOG("vgId:%d %s scan wal error:%s", TD_VID(pVnode), __func__, tstrerror(code));
861✔
802
      code = TSDB_CODE_SUCCESS;
862✔
803
      goto end;
862✔
804
    }
805
    STREAM_CHECK_RET_GOTO(code);
9,763!
806
    rsp->ver = pWalReader->curVersion;
9,763✔
807
    SWalCont* wCont = &pWalReader->pHead->head;
9,763✔
808
    rsp->verTime = wCont->ingestTs;
9,763✔
809
    if (wCont->ingestTs / 1000 > ctime) break;
9,763!
810
    void*   data = POINTER_SHIFT(wCont->body, sizeof(SMsgHead));
9,763✔
811
    int32_t len = wCont->bodyLen - sizeof(SMsgHead);
9,763✔
812
    int64_t ver = wCont->version;
9,763✔
813

814
    ST_TASK_DLOG("vgId:%d stream reader scan wal ver:%" PRId64 ", type:%d, deleteData:%d, deleteTb:%d",
9,763✔
815
      TD_VID(pVnode), ver, wCont->msgType, sStreamReaderInfo->deleteReCalc, sStreamReaderInfo->deleteOutTbl);
816
    if (wCont->msgType == TDMT_VND_SUBMIT) {
9,764✔
817
      data = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
9,325✔
818
      len = wCont->bodyLen - sizeof(SSubmitReq2Msg);
9,325✔
819
      STREAM_CHECK_RET_GOTO(scanSubmitDataForMeta(sStreamReaderInfo, rsp, data, len, ver));
9,325!
820
    } else {
821
      STREAM_CHECK_RET_GOTO(processMeta(wCont->msgType, sStreamReaderInfo, data, len, rsp, ver));
439!
822
    }
823

824
    if (rsp->totalRows >= STREAM_RETURN_ROWS_NUM) {
9,763!
825
      break;
×
826
    }
827
  }
828

829
end:
10,226✔
830
  walCloseReader(pWalReader);
10,226✔
831
  return code;
10,229✔
832
}
833

834
static int32_t processTag(SVnode* pVnode, SStreamTriggerReaderInfo* info, bool isCalc, SStorageAPI* api, 
6,707✔
835
  uint64_t uid, SSDataBlock* pBlock, uint32_t currentRow, uint32_t numOfRows, uint32_t numOfBlocks) {
836
  int32_t     code = 0;
6,707✔
837
  int32_t     lino = 0;
6,707✔
838
  SMetaReader mr = {0};
6,707✔
839
  SArray* tagCache = NULL;
6,707✔
840

841
  SHashObj* metaCache = isCalc ? info->pTableMetaCacheCalc : info->pTableMetaCacheTrigger;
6,707!
842
  SExprInfo*   pExprInfo = isCalc ? info->pExprInfoCalcTag : info->pExprInfoTriggerTag; 
6,707!
843
  int32_t      numOfExpr = isCalc ? info->numOfExprCalcTag : info->numOfExprTriggerTag;
6,707!
844
  if (numOfExpr == 0) {
6,707!
845
    return TSDB_CODE_SUCCESS;
×
846
  }
847

848
  void* uidData = taosHashGet(metaCache, &uid, LONG_BYTES);
6,707✔
849
  if (uidData == NULL) {
6,738✔
850
    api->metaReaderFn.initReader(&mr, pVnode, META_READER_LOCK, &api->metaFn);
307✔
851
    code = api->metaReaderFn.getEntryGetUidCache(&mr, uid);
307✔
852
    api->metaReaderFn.readerReleaseLock(&mr);
307✔
853
    STREAM_CHECK_RET_GOTO(code);
307!
854

855
    tagCache = taosArrayInit(numOfExpr, POINTER_BYTES);
307✔
856
    STREAM_CHECK_NULL_GOTO(tagCache, terrno);
307!
857
    if(taosHashPut(metaCache, &uid, LONG_BYTES, &tagCache, POINTER_BYTES) != 0) {
307✔
858
      taosArrayDestroyP(tagCache, taosMemFree);
4✔
859
      code = terrno;
×
860
      goto end;
×
861
    }
862
  } else {
863
    tagCache = *(SArray**)uidData;
6,431✔
864
    STREAM_CHECK_CONDITION_GOTO(taosArrayGetSize(tagCache) != numOfExpr, TSDB_CODE_INVALID_PARA);
6,431!
865
  }
866
  
867
  for (int32_t j = 0; j < numOfExpr; ++j) {
18,065✔
868
    const SExprInfo* pExpr1 = &pExprInfo[j];
11,359✔
869
    int32_t          dstSlotId = pExpr1->base.resSchema.slotId;
11,359✔
870

871
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, dstSlotId);
11,359✔
872
    STREAM_CHECK_NULL_GOTO(pColInfoData, terrno);
11,338!
873
    int32_t functionId = pExpr1->pExpr->_function.functionId;
11,338✔
874

875
    // this is to handle the tbname
876
    if (fmIsScanPseudoColumnFunc(functionId)) {
11,338✔
877
      int32_t fType = pExpr1->pExpr->_function.functionType;
6,674✔
878
      if (fType == FUNCTION_TYPE_TBNAME) {
6,674!
879
        char   buf[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
6,722✔
880
        if (uidData == NULL) {
6,722✔
881
          STR_TO_VARSTR(buf, mr.me.name)
306✔
882
          char* tbname = taosStrdup(mr.me.name);
306!
883
          STREAM_CHECK_NULL_GOTO(tbname, terrno);
307!
884
          STREAM_CHECK_NULL_GOTO(taosArrayPush(tagCache, &tbname), terrno);
614!
885
        } else {
886
          char* tbname = taosArrayGetP(tagCache, j);
6,416✔
887
          STR_TO_VARSTR(buf, tbname)
6,433✔
888
        }
889
        for (uint32_t i = 0; i < numOfRows; i++){
20,220✔
890
          colDataClearNull_f(pColInfoData->nullbitmap, currentRow + i);
13,480✔
891
        }
892
        code = colDataSetNItems(pColInfoData, currentRow, buf, numOfRows, numOfBlocks, false);
6,740✔
893
        pColInfoData->info.colId = -1;
6,724✔
894
      }
895
    } else {  // these are tags
896
      char* data = NULL;
4,653✔
897
      const char* p = NULL;
4,653✔
898
      STagVal tagVal = {0};
4,653✔
899
      if (uidData == NULL) {
4,653✔
900
        tagVal.cid = pExpr1->base.pParam[0].pCol->colId;
234✔
901
        p = api->metaFn.extractTagVal(mr.me.ctbEntry.pTags, pColInfoData->info.type, &tagVal);
234✔
902

903
        if (pColInfoData->info.type != TSDB_DATA_TYPE_JSON && p != NULL) {
234!
904
          data = tTagValToData((const STagVal*)p, false);
234✔
905
        } else {
906
          data = (char*)p;
×
907
        }
908

909
        if (data == NULL) {
234!
910
          STREAM_CHECK_NULL_GOTO(taosArrayPush(tagCache, &data), terrno);
×
911
        } else {
912
          int32_t len = pColInfoData->info.bytes;
234✔
913
          if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
234!
914
            len = calcStrBytesByType(pColInfoData->info.type, (char*)data);
56✔
915
          }
916
          char* pData = taosMemoryCalloc(1, len);
234!
917
          STREAM_CHECK_NULL_GOTO(pData, terrno);
234!
918
          (void)memcpy(pData, data, len);
234✔
919
          STREAM_CHECK_NULL_GOTO(taosArrayPush(tagCache, &pData), terrno);
468!
920
        }
921
      } else {
922
        data = taosArrayGetP(tagCache, j);
4,419✔
923
      }
924

925
      bool isNullVal = (data == NULL) || (pColInfoData->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data));
4,649!
926
      if (isNullVal) {
4,649!
927
        colDataSetNNULL(pColInfoData, currentRow, numOfRows);
×
928
      } else {
929
        for (uint32_t i = 0; i < numOfRows; i++){
35,758✔
930
          colDataClearNull_f(pColInfoData->nullbitmap, currentRow + i);
31,109✔
931
        }
932
        code = colDataSetNItems(pColInfoData, currentRow, data, numOfRows, numOfBlocks, false);
4,649✔
933
        if (uidData == NULL && pColInfoData->info.type != TSDB_DATA_TYPE_JSON && IS_VAR_DATA_TYPE(((const STagVal*)p)->type)) {
4,660!
934
          taosMemoryFree(data);
56!
935
        }
936
        STREAM_CHECK_RET_GOTO(code);
4,658!
937
      }
938
    }
939
  }
940

941
end:
6,706✔
942
  api->metaReaderFn.clearReader(&mr);
6,706✔
943
  return code;
6,706✔
944
}
945

946
int32_t getRowRange(SColData* pCol, STimeWindow* window, int32_t* rowStart, int32_t* rowEnd, int32_t* nRows) {
×
947
  int32_t code = 0;
×
948
  int32_t lino = 0;
×
949
  *nRows = 0;
×
950
  *rowStart = 0;
×
951
  *rowEnd = pCol->nVal;
×
952
  if (window != NULL) {
×
953
    SColVal colVal = {0};
×
954
    *rowStart = -1;
×
955
    *rowEnd = -1;
×
956
    for (int32_t k = 0; k < pCol->nVal; k++) {
×
957
      STREAM_CHECK_RET_GOTO(tColDataGetValue(pCol, k, &colVal));
×
958
      int64_t ts = VALUE_GET_TRIVIAL_DATUM(&colVal.value);
×
959
      if (ts >= window->skey && *rowStart == -1) {
×
960
        *rowStart = k;
×
961
      }
962
      if (ts > window->ekey && *rowEnd == -1) {
×
963
        *rowEnd = k;
×
964
      }
965
    }
966
    STREAM_CHECK_CONDITION_GOTO(*rowStart == -1 || *rowStart == *rowEnd, TDB_CODE_SUCCESS);
×
967

968
    if (*rowStart != -1 && *rowEnd == -1) {
×
969
      *rowEnd = pCol->nVal;
×
970
    }
971
  }
972
  *nRows = *rowEnd - *rowStart;
×
973

974
end:
×
975
  return code;
×
976
}
977

978
static int32_t setColData(int64_t rows, int32_t rowStart, int32_t rowEnd, SColData* colData, SColumnInfoData* pColData) {
×
979
  int32_t code = 0;
×
980
  int32_t lino = 0;
×
981
  for (int32_t k = rowStart; k < rowEnd; k++) {
×
982
    SColVal colVal = {0};
×
983
    STREAM_CHECK_RET_GOTO(tColDataGetValue(colData, k, &colVal));
×
984
    STREAM_CHECK_RET_GOTO(colDataSetVal(pColData, rows + k, VALUE_GET_DATUM(&colVal.value, colVal.value.type),
×
985
                                        !COL_VAL_IS_VALUE(&colVal)));
986
  }
987
  end:
×
988
  return code;
×
989
}
990

991
static int32_t scanSubmitTbData(SVnode* pVnode, SDecoder *pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo, 
12,759✔
992
  STSchema** schemas, SSHashObj* ranges, SSHashObj* gidHash, SSTriggerWalNewRsp* rsp, int64_t ver) {
993
  int32_t code = 0;
12,759✔
994
  int32_t lino = 0;
12,759✔
995
  uint64_t id = 0;
12,759✔
996
  WalMetaResult walMeta = {0};
12,759✔
997
  void* pTask = sStreamReaderInfo->pTask;
12,759✔
998
  SSDataBlock * pBlock = (SSDataBlock*)rsp->dataBlock;
12,759✔
999

1000
  if (tStartDecode(pCoder) < 0) {
12,759!
1001
    code = TSDB_CODE_INVALID_MSG;
×
1002
    TSDB_CHECK_CODE(code, lino, end);
×
1003
  }
1004

1005
  SSubmitTbData submitTbData = {0};
12,787✔
1006
  uint8_t       version = 0;
12,787✔
1007
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
12,749!
1008
    code = TSDB_CODE_INVALID_MSG;
×
1009
    TSDB_CHECK_CODE(code, lino, end);
×
1010
  }
1011
  version = (submitTbData.flags >> 8) & 0xff;
12,749✔
1012
  submitTbData.flags = submitTbData.flags & 0xff;
12,749✔
1013
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
1014
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
12,749✔
1015
    if (tStartDecode(pCoder) < 0) {
109!
1016
      code = TSDB_CODE_INVALID_MSG;
×
1017
      TSDB_CHECK_CODE(code, lino, end);
×
1018
    }
1019
    tEndDecode(pCoder);
109✔
1020
  }
1021

1022
  // submit data
1023
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
12,721!
1024
    code = TSDB_CODE_INVALID_MSG;
×
1025
    TSDB_CHECK_CODE(code, lino, end);
×
1026
  }
1027
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
12,703!
1028
    code = TSDB_CODE_INVALID_MSG;
×
1029
    TSDB_CHECK_CODE(code, lino, end);
×
1030
  }
1031

1032
  STREAM_CHECK_CONDITION_GOTO(!uidInTableList(sStreamReaderInfo, submitTbData.suid, submitTbData.uid, &id, rsp->isCalc), TDB_CODE_SUCCESS);
12,703✔
1033

1034
  walMeta.id = id;
9,794✔
1035
  STimeWindow window = {.skey = INT64_MIN, .ekey = INT64_MAX};
9,794✔
1036

1037
  if (ranges != NULL){
9,794✔
1038
    void* timerange = tSimpleHashGet(ranges, &id, sizeof(id));
6,385✔
1039
    if (timerange == NULL) goto end;;
6,380!
1040
    int64_t* pRange = (int64_t*)timerange;
6,380✔
1041
    window.skey = pRange[0];
6,380✔
1042
    window.ekey = pRange[1];
6,380✔
1043
  }
1044
  
1045
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
9,812!
1046
    code = TSDB_CODE_INVALID_MSG;
×
1047
    TSDB_CHECK_CODE(code, lino, end);
×
1048
  }
1049

1050
  if (*schemas == NULL) {
9,812!
1051
    *schemas = metaGetTbTSchema(pVnode->pMeta, submitTbData.suid != 0 ? submitTbData.suid : submitTbData.uid, submitTbData.sver, 1);
9,819✔
1052
    STREAM_CHECK_NULL_GOTO(*schemas, TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND);
9,843!
1053
  }
1054

1055
  SStreamWalDataSlice* pSlice = (SStreamWalDataSlice*)tSimpleHashGet(sStreamReaderInfo->indexHash, &submitTbData.uid, LONG_BYTES);
9,836✔
1056
  STREAM_CHECK_NULL_GOTO(pSlice, TSDB_CODE_INVALID_PARA);
9,833!
1057
  int32_t blockStart = pSlice->currentRowIdx;
9,833✔
1058

1059
  int32_t numOfRows = 0;
9,833✔
1060
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
9,833!
1061
    uint64_t nColData = 0;
×
1062
    if (tDecodeU64v(pCoder, &nColData) < 0) {
×
1063
      code = TSDB_CODE_INVALID_MSG;
×
1064
      TSDB_CHECK_CODE(code, lino, end);
×
1065
    }
1066

1067
    SColData colData = {0};
×
1068
    code = tDecodeColData(version, pCoder, &colData, false);
×
1069
    if (code) {
×
1070
      code = TSDB_CODE_INVALID_MSG;
×
1071
      TSDB_CHECK_CODE(code, lino, end);
×
1072
    }
1073

1074
    if (colData.flag != HAS_VALUE) {
×
1075
      code = TSDB_CODE_INVALID_MSG;
×
1076
      TSDB_CHECK_CODE(code, lino, end);
×
1077
    }
1078
    
1079
    walMeta.skey = ((TSKEY *)colData.pData)[0];
×
1080
    walMeta.ekey = ((TSKEY *)colData.pData)[colData.nVal - 1];
×
1081

1082
    int32_t rowStart = 0;
×
1083
    int32_t rowEnd = 0;
×
1084
    STREAM_CHECK_RET_GOTO(getRowRange(&colData, &window, &rowStart, &rowEnd, &numOfRows));
×
1085
    STREAM_CHECK_CONDITION_GOTO(numOfRows <= 0, TDB_CODE_SUCCESS);
×
1086

1087
    int32_t pos = pCoder->pos;
×
1088
    for (int16_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {
×
1089
      SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, i);
×
1090
      STREAM_CHECK_NULL_GOTO(pColData, terrno);
×
1091
      if (pColData->info.colId <= -1) {
×
1092
        pColData->hasNull = true;
×
1093
        continue;
×
1094
      }
1095
      if (pColData->info.colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
×
1096
        STREAM_CHECK_RET_GOTO(setColData(blockStart, rowStart, rowEnd, &colData, pColData));
×
1097
        continue;
×
1098
      }
1099

1100
      pCoder->pos = pos;
×
1101

1102
      int16_t colId = 0;
×
1103
      if (sStreamReaderInfo->isVtableStream){
×
1104
        int64_t id[2] = {submitTbData.suid, submitTbData.uid};
×
1105
        void *px = tSimpleHashGet(rsp->isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, id, sizeof(id));
×
1106
        STREAM_CHECK_NULL_GOTO(px, TSDB_CODE_INVALID_PARA);
×
1107
        SSHashObj* uInfo = *(SSHashObj **)px;
×
1108
        STREAM_CHECK_NULL_GOTO(uInfo, TSDB_CODE_INVALID_PARA);
×
1109
        int16_t*  tmp = tSimpleHashGet(uInfo, &i, sizeof(i));
×
1110
        if (tmp != NULL) {
×
1111
          colId = *tmp;
×
1112
        } else {
1113
          colId = -1;
×
1114
        }
1115
      } else {
1116
        colId = pColData->info.colId;
×
1117
      }
1118
      
1119
      uint64_t j = 1;
×
1120
      for (; j < nColData; j++) {
×
1121
        int16_t cid = 0;
×
1122
        int32_t posTmp = pCoder->pos;
×
1123
        pCoder->pos += INT_BYTES;
×
1124
        if ((code = tDecodeI16v(pCoder, &cid))) return code;
×
1125
        pCoder->pos = posTmp;
×
1126
        if (cid == colId) {
×
1127
          SColData colDataTmp = {0};
×
1128
          code = tDecodeColData(version, pCoder, &colDataTmp, false);
×
1129
          if (code) {
×
1130
            code = TSDB_CODE_INVALID_MSG;
×
1131
            TSDB_CHECK_CODE(code, lino, end);
×
1132
          }
1133
          STREAM_CHECK_RET_GOTO(setColData(blockStart, rowStart, rowEnd, &colDataTmp, pColData));
×
1134
          break;
×
1135
        }
1136
        code = tDecodeColData(version, pCoder, &colData, true);
×
1137
        if (code) {
×
1138
          code = TSDB_CODE_INVALID_MSG;
×
1139
          TSDB_CHECK_CODE(code, lino, end);
×
1140
        }
1141
      }
1142
      if (j == nColData) {
×
1143
        colDataSetNNULL(pColData, blockStart, numOfRows);
×
1144
      }
1145
    }
1146
  } else {
1147
    uint64_t nRow = 0;
9,833✔
1148
    if (tDecodeU64v(pCoder, &nRow) < 0) {
9,828!
1149
      code = TSDB_CODE_INVALID_MSG;
×
1150
      TSDB_CHECK_CODE(code, lino, end);
×
1151
    }
1152
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
24,715✔
1153
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
14,920✔
1154
      pCoder->pos += pRow->len;
14,920✔
1155

1156
      if (iRow == 0){
14,920✔
1157
#ifndef NO_UNALIGNED_ACCESS
1158
        walMeta.skey = pRow->ts;
9,824✔
1159
#else
1160
        walMeta.skey = taosGetInt64Aligned(&pRow->ts);
1161
#endif
1162
      }
1163
      if (iRow == nRow - 1) {
14,920✔
1164
#ifndef NO_UNALIGNED_ACCESS
1165
        walMeta.ekey = pRow->ts;
9,828✔
1166
#else
1167
        walMeta.ekey = taosGetInt64Aligned(&pRow->ts);
1168
#endif
1169
      }
1170

1171
      if (pRow->ts < window.skey || pRow->ts > window.ekey) {
14,920✔
1172
        continue;
32✔
1173
      }
1174
     
1175
      for (int16_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {  // reader todo test null
105,623✔
1176
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, i);
90,308✔
1177
        STREAM_CHECK_NULL_GOTO(pColData, terrno);
90,441!
1178
        if (pColData->info.colId <= -1) {
90,723✔
1179
          pColData->hasNull = true;
28,282✔
1180
          continue;
28,282✔
1181
        }
1182
        int16_t colId = 0;
62,441✔
1183
        if (sStreamReaderInfo->isVtableStream){
62,441✔
1184
          int64_t id[2] = {submitTbData.suid, submitTbData.uid};
8,372✔
1185
          void* px = tSimpleHashGet(rsp->isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, id, sizeof(id));
8,372✔
1186
          STREAM_CHECK_NULL_GOTO(px, TSDB_CODE_INVALID_PARA);
8,387!
1187
          SSHashObj* uInfo = *(SSHashObj**)px;
8,387✔
1188
          STREAM_CHECK_NULL_GOTO(uInfo, TSDB_CODE_INVALID_PARA);
8,387!
1189
          int16_t*  tmp = tSimpleHashGet(uInfo, &i, sizeof(i));
8,387✔
1190
          if (tmp != NULL) {
8,338✔
1191
            colId = *tmp;
7,117✔
1192
          } else {
1193
            colId = -1;
1,221✔
1194
          }
1195
          ST_TASK_DLOG("%s vtable colId:%d, i:%d, uid:%" PRId64, __func__, colId, i, submitTbData.uid);
8,338✔
1196
        } else {
1197
          colId = pColData->info.colId;
54,069✔
1198
        }
1199
        
1200
        SColVal colVal = {0};
62,412✔
1201
        int32_t sourceIdx = 0;
62,412✔
1202
        while (1) {
1203
          if (sourceIdx >= (*schemas)->numOfCols) {
166,268✔
1204
            break;
30,772✔
1205
          }
1206
          STREAM_CHECK_RET_GOTO(tRowGet(pRow, *schemas, sourceIdx, &colVal));
135,496!
1207
          if (colVal.cid == colId) {
135,508✔
1208
            break;
31,652✔
1209
          }
1210
          sourceIdx++;
103,856✔
1211
        }
1212
        if (colVal.cid == colId && COL_VAL_IS_VALUE(&colVal)) {
62,424✔
1213
          if (IS_VAR_DATA_TYPE(colVal.value.type) || colVal.value.type == TSDB_DATA_TYPE_DECIMAL){
31,359!
1214
            STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, blockStart+ numOfRows, (const char*)colVal.value.pData, colVal.value.nData, !COL_VAL_IS_VALUE(&colVal)));
51!
1215
          } else {
1216
            STREAM_CHECK_RET_GOTO(colDataSetVal(pColData, blockStart + numOfRows, (const char*)(&(colVal.value.val)), !COL_VAL_IS_VALUE(&colVal)));
31,308!
1217
          }
1218
        } else {
1219
          colDataSetNULL(pColData, blockStart + numOfRows);
31,065✔
1220
        }
1221
      }
1222
      
1223
      numOfRows++;
14,855✔
1224
    }
1225
  }
1226

1227
  if (numOfRows > 0) {
9,795✔
1228
    if (!sStreamReaderInfo->isVtableStream) {
9,791✔
1229
      SStorageAPI  api = {0};
6,511✔
1230
      initStorageAPI(&api);
6,511✔
1231
      STREAM_CHECK_RET_GOTO(processTag(pVnode, sStreamReaderInfo, rsp->isCalc, &api, submitTbData.uid, pBlock, blockStart, numOfRows, 1));
6,516!
1232
    }
1233
    
1234
    SColumnInfoData* pColData = taosArrayGetLast(pBlock->pDataBlock);
9,805✔
1235
    STREAM_CHECK_NULL_GOTO(pColData, terrno);
9,800!
1236
    STREAM_CHECK_RET_GOTO(colDataSetNItems(pColData, blockStart, (const char*)&ver, numOfRows, 1, false));
9,800!
1237
  }
1238

1239
  ST_TASK_DLOG("%s process submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
9,832✔
1240
    ", uid:%" PRId64 ", ver:%d, row index:%d, rows:%d", __func__, window.skey, window.ekey, 
1241
    id, submitTbData.uid, submitTbData.sver, pSlice->currentRowIdx, numOfRows);
1242
  pSlice->currentRowIdx += numOfRows;
9,825✔
1243
  pBlock->info.rows += numOfRows;
9,825✔
1244
  
1245
  if (gidHash == NULL) goto end;
9,825✔
1246

1247
  WalMetaResult* data = (WalMetaResult*)tSimpleHashGet(gidHash, &walMeta.id, LONG_BYTES);
3,436✔
1248
  if (data != NULL) {
3,430!
1249
    if (walMeta.skey < data->skey) data->skey = walMeta.skey;
×
1250
    if (walMeta.ekey > data->ekey) data->ekey = walMeta.ekey;
×
1251
  } else {
1252
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(gidHash, &walMeta.id, LONG_BYTES, &walMeta, sizeof(WalMetaResult)));
3,430!
1253
  }
1254

1255
end:
3,426✔
1256
  if (code != 0) {                                                             \
12,800!
1257
    ST_TASK_ELOG("%s failed at line %d since %s", __func__, lino, tstrerror(code)); \
×
1258
  }
1259
  tEndDecode(pCoder);
12,800✔
1260
  return code;
12,742✔
1261
}
1262
static int32_t scanSubmitData(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo,
12,769✔
1263
  void* data, int32_t len, SSHashObj* ranges, SSTriggerWalNewRsp* rsp, int64_t ver) {
1264
  int32_t  code = 0;
12,769✔
1265
  int32_t  lino = 0;
12,769✔
1266
  STSchema* schemas = NULL;
12,769✔
1267
  SDecoder decoder = {0};
12,769✔
1268
  SSHashObj* gidHash = NULL;
12,769✔
1269
  void* pTask = sStreamReaderInfo->pTask;
12,769✔
1270

1271
  tDecoderInit(&decoder, data, len);
12,769✔
1272
  if (tStartDecode(&decoder) < 0) {
12,757!
1273
    code = TSDB_CODE_INVALID_MSG;
×
1274
    TSDB_CHECK_CODE(code, lino, end);
×
1275
  }
1276

1277
  uint64_t nSubmitTbData = 0;
12,792✔
1278
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
12,783!
1279
    code = TSDB_CODE_INVALID_MSG;
×
1280
    TSDB_CHECK_CODE(code, lino, end);
×
1281
  }
1282

1283
  if (rsp->metaBlock != NULL){
12,783✔
1284
    gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
6,392✔
1285
    STREAM_CHECK_NULL_GOTO(gidHash, terrno);
6,389!
1286
  }
1287

1288
  for (int32_t i = 0; i < nSubmitTbData; i++) {
25,517✔
1289
    STREAM_CHECK_RET_GOTO(scanSubmitTbData(pVnode, &decoder, sStreamReaderInfo, &schemas, ranges, gidHash, rsp, ver));
12,786!
1290
  }
1291

1292
  tEndDecode(&decoder);
12,731✔
1293

1294
  if (rsp->metaBlock != NULL){
12,713✔
1295
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
6,334!
1296
    int32_t iter = 0;
6,323✔
1297
    void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
6,323✔
1298
    while (px != NULL) {
9,813✔
1299
      WalMetaResult* pMeta = (WalMetaResult*)px;
3,436✔
1300
      STREAM_CHECK_RET_GOTO(buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver));
3,436!
1301
      ((SSDataBlock*)rsp->metaBlock)->info.rows++;
3,402✔
1302
      ST_TASK_DLOG("%s process meta data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
3,402✔
1303
            ", ver:%"PRId64, __func__, pMeta->skey, pMeta->ekey, pMeta->id, ver);
1304
      px = tSimpleHashIterate(gidHash, px, &iter);
3,403✔
1305
    }
1306
  }
1307
  
1308

1309
end:
6,379✔
1310
  taosMemoryFree(schemas);
12,756!
1311
  tSimpleHashCleanup(gidHash);
12,789✔
1312
  tDecoderClear(&decoder);
12,779✔
1313
  return code;
12,829✔
1314
}
1315

1316
static int32_t scanSubmitTbDataPre(SDecoder *pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo, SSHashObj* ranges, 
14,801✔
1317
  uint64_t* gid, int64_t* uid, int32_t* numOfRows, bool isCalc) {
1318
  int32_t code = 0;
14,801✔
1319
  int32_t lino = 0;
14,801✔
1320
  void* pTask = sStreamReaderInfo->pTask;
14,801✔
1321

1322
  if (tStartDecode(pCoder) < 0) {
14,801!
1323
    code = TSDB_CODE_INVALID_MSG;
×
1324
    TSDB_CHECK_CODE(code, lino, end);
×
1325
  }
1326

1327
  SSubmitTbData submitTbData = {0};
14,852✔
1328
  uint8_t       version = 0;
14,852✔
1329
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
14,796!
1330
    code = TSDB_CODE_INVALID_MSG;
×
1331
    TSDB_CHECK_CODE(code, lino, end);
×
1332
  }
1333
  version = (submitTbData.flags >> 8) & 0xff;
14,796✔
1334
  submitTbData.flags = submitTbData.flags & 0xff;
14,796✔
1335

1336
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
1337
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
14,796✔
1338
    submitTbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
1,174!
1339
    STREAM_CHECK_NULL_GOTO(submitTbData.pCreateTbReq, terrno);
1,174!
1340
    STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbReq(pCoder, submitTbData.pCreateTbReq));
1,174!
1341
    STREAM_CHECK_RET_GOTO(processAutoCreateTableNew(sStreamReaderInfo, submitTbData.pCreateTbReq));
1,171!
1342
  }
1343

1344
  // submit data
1345
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
14,753!
1346
    code = TSDB_CODE_INVALID_MSG;
×
1347
    TSDB_CHECK_CODE(code, lino, end);
×
1348
  }
1349
  if (tDecodeI64(pCoder, uid) < 0) {
14,700!
1350
    code = TSDB_CODE_INVALID_MSG;
×
1351
    TSDB_CHECK_CODE(code, lino, end);
×
1352
  }
1353

1354
  STREAM_CHECK_CONDITION_GOTO(!uidInTableList(sStreamReaderInfo, submitTbData.suid, *uid, gid, isCalc), TDB_CODE_SUCCESS);
14,700✔
1355

1356
  STimeWindow window = {.skey = INT64_MIN, .ekey = INT64_MAX};
9,693✔
1357

1358
  if (ranges != NULL){
9,693✔
1359
    void* timerange = tSimpleHashGet(ranges, gid, sizeof(*gid));
6,399✔
1360
    if (timerange == NULL) goto end;;
6,386!
1361
    int64_t* pRange = (int64_t*)timerange;
6,386✔
1362
    window.skey = pRange[0];
6,386✔
1363
    window.ekey = pRange[1];
6,386✔
1364
  }
1365
  
1366
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
9,768!
1367
    code = TSDB_CODE_INVALID_MSG;
×
1368
    TSDB_CHECK_CODE(code, lino, end);
×
1369
  }
1370

1371
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
9,768!
1372
    uint64_t nColData = 0;
×
1373
    if (tDecodeU64v(pCoder, &nColData) < 0) {
×
1374
      code = TSDB_CODE_INVALID_MSG;
×
1375
      TSDB_CHECK_CODE(code, lino, end);
×
1376
    }
1377

1378
    SColData colData = {0};
×
1379
    code = tDecodeColData(version, pCoder, &colData, false);
×
1380
    if (code) {
×
1381
      code = TSDB_CODE_INVALID_MSG;
×
1382
      TSDB_CHECK_CODE(code, lino, end);
×
1383
    }
1384

1385
    if (colData.flag != HAS_VALUE) {
×
1386
      code = TSDB_CODE_INVALID_MSG;
×
1387
      TSDB_CHECK_CODE(code, lino, end);
×
1388
    }
1389
    int32_t rowStart = 0;
×
1390
    int32_t rowEnd = 0;
×
1391
    if (window.skey != INT64_MIN || window.ekey != INT64_MAX) {
×
1392
      STREAM_CHECK_RET_GOTO(getRowRange(&colData, &window, &rowStart, &rowEnd, numOfRows));
×
1393
    } else {
1394
      (*numOfRows) = colData.nVal;
×
1395
    } 
1396
  } else {
1397
    uint64_t nRow = 0;
9,768✔
1398
    if (tDecodeU64v(pCoder, &nRow) < 0) {
9,775!
1399
      code = TSDB_CODE_INVALID_MSG;
×
1400
      TSDB_CHECK_CODE(code, lino, end);
×
1401
    }
1402

1403
    if (window.skey != INT64_MIN || window.ekey != INT64_MAX) { 
9,775!
1404
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
17,838✔
1405
        SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
11,441✔
1406
        pCoder->pos += pRow->len;
11,441✔
1407
        if (pRow->ts < window.skey || pRow->ts > window.ekey) {
11,441!
1408
          continue;
32✔
1409
        }
1410
        (*numOfRows)++;
11,409✔
1411
      }
1412
    } else {
1413
      (*numOfRows) = nRow;
3,378✔
1414
    }
1415
  }
1416
  
1417
end:
14,938✔
1418
  tDestroySVSubmitCreateTbReq(submitTbData.pCreateTbReq, TSDB_MSG_FLG_DECODE);
14,938✔
1419
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
14,820!
1420
  tEndDecode(pCoder);
14,821✔
1421
  return code;
14,804✔
1422
}
1423

1424
static int32_t scanSubmitDataPre(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len, SSHashObj* ranges, SSTriggerWalNewRsp* rsp) {
14,857✔
1425
  int32_t  code = 0;
14,857✔
1426
  int32_t  lino = 0;
14,857✔
1427
  SDecoder decoder = {0};
14,857✔
1428
  void* pTask = sStreamReaderInfo->pTask;
14,857✔
1429

1430
  tDecoderInit(&decoder, data, len);
14,857✔
1431
  if (tStartDecode(&decoder) < 0) {
14,829!
1432
    code = TSDB_CODE_INVALID_MSG;
×
1433
    TSDB_CHECK_CODE(code, lino, end);
×
1434
  }
1435

1436
  uint64_t nSubmitTbData = 0;
14,858✔
1437
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
14,824!
1438
    code = TSDB_CODE_INVALID_MSG;
×
1439
    TSDB_CHECK_CODE(code, lino, end);
×
1440
  }
1441

1442
  for (int32_t i = 0; i < nSubmitTbData; i++) {
29,754✔
1443
    uint64_t gid = -1;
14,814✔
1444
    int64_t  uid = 0;
14,814✔
1445
    int32_t numOfRows = 0;
14,814✔
1446
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataPre(&decoder, sStreamReaderInfo, ranges, &gid, &uid, &numOfRows, rsp->isCalc));
14,814!
1447
    if (numOfRows <= 0) {
14,782✔
1448
      continue;
5,143✔
1449
    }
1450
    rsp->totalRows += numOfRows;
9,639✔
1451

1452
    SStreamWalDataSlice* pSlice = (SStreamWalDataSlice*)tSimpleHashGet(sStreamReaderInfo->indexHash, &uid, LONG_BYTES);
9,639✔
1453
    if (pSlice != NULL) {
9,771✔
1454
      pSlice->numRows += numOfRows;
9,354✔
1455
      ST_TASK_DLOG("%s again uid:%" PRId64 ", gid:%" PRIu64 ", total numOfRows:%d", __func__, uid, gid, pSlice->numRows);
9,354✔
1456
      pSlice->gId = gid;
9,353✔
1457
    } else {
1458
      SStreamWalDataSlice tmp = {.gId=gid,.numRows=numOfRows,.currentRowIdx=0,.startRowIdx=0};
417✔
1459
      ST_TASK_DLOG("%s first uid:%" PRId64 ", gid:%" PRIu64 ", numOfRows:%d", __func__, uid, gid, tmp.numRows);
417✔
1460
      STREAM_CHECK_RET_GOTO(tSimpleHashPut(sStreamReaderInfo->indexHash, &uid, LONG_BYTES, &tmp, sizeof(tmp)));
417!
1461
    } 
1462
  }
1463

1464
  tEndDecode(&decoder);
14,940✔
1465

1466
end:
14,729✔
1467
  tDecoderClear(&decoder);
14,729✔
1468
  return code;
14,925✔
1469
}
1470

1471
static void resetIndexHash(SSHashObj* indexHash){
12,840✔
1472
  void*   pe = NULL;
12,840✔
1473
  int32_t iter = 0;
12,840✔
1474
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
31,189✔
1475
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
18,349✔
1476
    pInfo->startRowIdx = 0;
18,349✔
1477
    pInfo->currentRowIdx = 0;
18,349✔
1478
    pInfo->numRows = 0;
18,349✔
1479
    pInfo->gId = -1;
18,349✔
1480
  }
1481
}
12,801✔
1482

1483
static void buildIndexHash(SSHashObj* indexHash, void* pTask){
995✔
1484
  void*   pe = NULL;
995✔
1485
  int32_t iter = 0;
995✔
1486
  int32_t index = 0;
995✔
1487
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
3,574✔
1488
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
2,579✔
1489
    pInfo->startRowIdx = index;
2,579✔
1490
    pInfo->currentRowIdx = index;
2,579✔
1491
    index += pInfo->numRows;
2,579✔
1492
    ST_TASK_DLOG("%s uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", __func__, *(int64_t*)(tSimpleHashGetKey(pe, NULL)),
3,342!
1493
    pInfo->gId, pInfo->startRowIdx, pInfo->numRows);
1494
  }
1495
}
994✔
1496

1497
static void printIndexHash(SSHashObj* indexHash, void* pTask){
993✔
1498
  void*   pe = NULL;
993✔
1499
  int32_t iter = 0;
993✔
1500
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
3,568✔
1501
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
2,574✔
1502
    ST_TASK_DLOG("%s uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", __func__, *(int64_t*)(tSimpleHashGetKey(pe, NULL)),
3,336!
1503
    pInfo->gId, pInfo->startRowIdx, pInfo->numRows);
1504
  }
1505
}
992✔
1506

1507
static void filterIndexHash(SSHashObj* indexHash, SColumnInfoData* pRet){
26✔
1508
  void*   pe = NULL;
26✔
1509
  int32_t iter = 0;
26✔
1510
  int32_t index = 0;
26✔
1511
  int32_t pIndex = 0;
26✔
1512
  int8_t* pIndicator = (int8_t*)pRet->pData;
26✔
1513
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
80✔
1514
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
54✔
1515
    pInfo->startRowIdx = index;
54✔
1516
    int32_t size = pInfo->numRows;
54✔
1517
    for (int32_t i = 0; i < pInfo->numRows; i++) {
342✔
1518
      if (pIndicator && !pIndicator[pIndex++]) {
288!
1519
        size--;
106✔
1520
      }
1521
    }
1522
    pInfo->numRows = size;
54✔
1523
    index += pInfo->numRows;
54✔
1524
    stTrace("stream reader re build index hash uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", *(int64_t*)(tSimpleHashGetKey(pe, NULL)),
54!
1525
    pInfo->gId, pInfo->startRowIdx, pInfo->numRows);
1526
  }
1527
}
26✔
1528

1529
static int32_t prepareIndexMetaData(SWalReader* pWalReader, SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* resultRsp){
7,501✔
1530
  int32_t      code = 0;
7,501✔
1531
  int32_t      lino = 0;
7,501✔
1532
  void* pTask = sStreamReaderInfo->pTask;
7,501✔
1533

1534
  code = walReaderSeekVer(pWalReader, resultRsp->ver);
7,501✔
1535
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
7,520✔
1536
    if (resultRsp->ver < walGetFirstVer(pWalReader->pWal)) {
6,603!
1537
      resultRsp->ver = walGetFirstVer(pWalReader->pWal);
×
1538
    }
1539
    ST_TASK_DLOG("%s scan wal error:%s",  __func__, tstrerror(code));
6,601✔
1540
    code = TSDB_CODE_SUCCESS;
6,600✔
1541
    goto end;
6,600✔
1542
  }
1543
  STREAM_CHECK_RET_GOTO(code);
917!
1544

1545
  while (1) {
8,875✔
1546
    code = walNextValidMsg(pWalReader, true);
9,792✔
1547
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
9,748✔
1548
      ST_TASK_DLOG("%s scan wal error:%s", __func__, tstrerror(code));
894✔
1549
      code = TSDB_CODE_SUCCESS;
915✔
1550
      goto end;
915✔
1551
    }
1552
    STREAM_CHECK_RET_GOTO(code);
8,854!
1553
    resultRsp->ver = pWalReader->curVersion;
8,854✔
1554
    SWalCont* wCont = &pWalReader->pHead->head;
8,854✔
1555
    resultRsp->verTime = wCont->ingestTs;
8,854✔
1556
    void*   data = POINTER_SHIFT(wCont->body, sizeof(SMsgHead));
8,854✔
1557
    int32_t len = wCont->bodyLen - sizeof(SMsgHead);
8,854✔
1558
    int64_t ver = wCont->version;
8,854✔
1559
    ST_TASK_DLOG("%s scan wal ver:%" PRId64 ", type:%d, deleteData:%d, deleteTb:%d", __func__,
8,854✔
1560
      ver, wCont->msgType, sStreamReaderInfo->deleteReCalc, sStreamReaderInfo->deleteOutTbl);
1561
    if (wCont->msgType == TDMT_VND_SUBMIT) {
8,852✔
1562
      data = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
8,471✔
1563
      len = wCont->bodyLen - sizeof(SSubmitReq2Msg);
8,471✔
1564
      STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, data, len, NULL, resultRsp));
8,471!
1565
    } else if (wCont->msgType == TDMT_VND_ALTER_TABLE && resultRsp->totalRows > 0) {
381✔
1566
      resultRsp->ver--;
5✔
1567
      break;
5✔
1568
    } else {
1569
      STREAM_CHECK_RET_GOTO(processMeta(wCont->msgType, sStreamReaderInfo, data, len, resultRsp, ver));
376!
1570
    }
1571

1572
    ST_TASK_DLOG("%s scan wal next ver:%" PRId64 ", totalRows:%d", __func__, resultRsp->ver, resultRsp->totalRows);
8,879✔
1573
    if (resultRsp->totalRows >= STREAM_RETURN_ROWS_NUM) {
8,875!
1574
      break;
×
1575
    }
1576
  }
1577
  
1578
end:
7,520✔
1579
  STREAM_PRINT_LOG_END(code, lino);
7,520!
1580
  return code;
7,517✔
1581
}
1582

1583
static int32_t prepareIndexData(SWalReader* pWalReader, SStreamTriggerReaderInfo* sStreamReaderInfo, 
5,325✔
1584
  SArray* versions, SSHashObj* ranges, SSTriggerWalNewRsp* rsp){
1585
  int32_t      code = 0;
5,325✔
1586
  int32_t      lino = 0;
5,325✔
1587

1588
  for(int32_t i = 0; i < taosArrayGetSize(versions); i++) {
11,716✔
1589
    int64_t *ver = taosArrayGet(versions, i);
6,380✔
1590
    if (ver == NULL) continue;
6,385!
1591

1592
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, *ver));
6,385!
1593
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
6,398!
1594
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
×
1595
      continue;
×
1596
    }
1597
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
6,398!
1598

1599
    SWalCont* wCont = &pWalReader->pHead->head;
6,398✔
1600
    void*   pBody = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
6,398✔
1601
    int32_t bodyLen = wCont->bodyLen - sizeof(SSubmitReq2Msg);
6,398✔
1602

1603
    STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, pBody, bodyLen, ranges, rsp));
6,398!
1604
  }
1605
  
1606
end:
5,327✔
1607
  return code;
5,327✔
1608
}
1609

1610
static int32_t filterData(SSTriggerWalNewRsp* resultRsp, SStreamTriggerReaderInfo* sStreamReaderInfo) {
995✔
1611
  int32_t      code = 0;
995✔
1612
  int32_t       lino = 0;
995✔
1613
  SColumnInfoData* pRet = NULL;
995✔
1614
  int64_t totalRows = ((SSDataBlock*)resultRsp->dataBlock)->info.rows;
995✔
1615
  STREAM_CHECK_RET_GOTO(qStreamFilter(((SSDataBlock*)resultRsp->dataBlock), sStreamReaderInfo->pFilterInfo, &pRet));
995!
1616
  if (((SSDataBlock*)resultRsp->dataBlock)->info.rows < totalRows) {
995✔
1617
    filterIndexHash(sStreamReaderInfo->indexHash, pRet);
26✔
1618
  }
1619

1620
end:
969✔
1621
  colDataDestroy(pRet);
995✔
1622
  taosMemoryFree(pRet);
995!
1623
  return code;
995✔
1624
}
1625

1626
static int32_t processWalVerMetaDataNew(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo, 
7,527✔
1627
                                    SSTriggerWalNewRsp* resultRsp) {
1628
  int32_t      code = 0;
7,527✔
1629
  int32_t      lino = 0;
7,527✔
1630
  void* pTask = sStreamReaderInfo->pTask;
7,527✔
1631
                                        
1632
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
7,527✔
1633
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
7,528!
1634
  resetIndexHash(sStreamReaderInfo->indexHash);
7,528✔
1635
  blockDataEmpty(resultRsp->dataBlock);
7,526✔
1636
  blockDataEmpty(resultRsp->metaBlock);
7,493✔
1637
  int64_t lastVer = resultRsp->ver;                                      
7,501✔
1638
  STREAM_CHECK_RET_GOTO(prepareIndexMetaData(pWalReader, sStreamReaderInfo, resultRsp));
7,501!
1639
  STREAM_CHECK_CONDITION_GOTO(resultRsp->totalRows == 0, TDB_CODE_SUCCESS);
7,517✔
1640

1641
  buildIndexHash(sStreamReaderInfo->indexHash, pTask);
285✔
1642
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(((SSDataBlock*)resultRsp->dataBlock), resultRsp->totalRows));
285!
1643
  while(lastVer < resultRsp->ver) {
6,988✔
1644
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, lastVer++));
6,701!
1645
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
6,716✔
1646
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
289!
1647
      continue;
289✔
1648
    }
1649
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
6,427!
1650
    SWalCont* wCont = &pWalReader->pHead->head;
6,398✔
1651
    void*   pBody = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
6,398✔
1652
    int32_t bodyLen = wCont->bodyLen - sizeof(SSubmitReq2Msg);
6,398✔
1653

1654
    STREAM_CHECK_RET_GOTO(scanSubmitData(pVnode, sStreamReaderInfo, pBody, bodyLen, NULL, resultRsp, wCont->version));
6,398!
1655
  }
1656

1657
  int32_t metaRows = resultRsp->totalRows - ((SSDataBlock*)resultRsp->dataBlock)->info.rows;
287✔
1658
  STREAM_CHECK_RET_GOTO(filterData(resultRsp, sStreamReaderInfo));
287!
1659
  resultRsp->totalRows = ((SSDataBlock*)resultRsp->dataBlock)->info.rows + metaRows;
285✔
1660

1661
end:
7,517✔
1662
  ST_TASK_DLOG("vgId:%d %s end, get result totalRows:%d, process:%"PRId64"/%"PRId64, TD_VID(pVnode), __func__, 
7,517✔
1663
          resultRsp->totalRows, resultRsp->ver, walGetAppliedVer(pWalReader->pWal));
1664
  walCloseReader(pWalReader);
7,517✔
1665
  return code;
7,548✔
1666
}
1667

1668
static int32_t processWalVerDataNew(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo, 
5,328✔
1669
                                    SArray* versions, SSHashObj* ranges, SSTriggerWalNewRsp* rsp) {
1670
  int32_t      code = 0;
5,328✔
1671
  int32_t      lino = 0;
5,328✔
1672

1673
  void* pTask = sStreamReaderInfo->pTask;
5,328✔
1674
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
5,328✔
1675
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
5,327!
1676
  
1677
  if (taosArrayGetSize(versions) > 0) {
5,327✔
1678
    rsp->ver = *(int64_t*)taosArrayGetLast(versions);
710✔
1679
  }
1680
  
1681
  resetIndexHash(sStreamReaderInfo->indexHash);
5,324✔
1682
  STREAM_CHECK_RET_GOTO(prepareIndexData(pWalReader, sStreamReaderInfo, versions, ranges, rsp));
5,326!
1683
  STREAM_CHECK_CONDITION_GOTO(rsp->totalRows == 0, TDB_CODE_SUCCESS);
5,326✔
1684

1685
  buildIndexHash(sStreamReaderInfo->indexHash, pTask);
710✔
1686

1687
  blockDataEmpty(rsp->dataBlock);
709✔
1688
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->dataBlock, rsp->totalRows));
710!
1689

1690
  for(int32_t i = 0; i < taosArrayGetSize(versions); i++) {
7,098✔
1691
    int64_t *ver = taosArrayGet(versions, i);
6,386✔
1692
    if (ver == NULL) continue;
6,387!
1693

1694
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, *ver));
6,387!
1695
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
6,394!
1696
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
×
1697
      continue;
×
1698
    }
1699
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
6,394!
1700
    SWalCont* wCont = &pWalReader->pHead->head;
6,390✔
1701
    void*   pBody = POINTER_SHIFT(wCont->body, sizeof(SSubmitReq2Msg));
6,390✔
1702
    int32_t bodyLen = wCont->bodyLen - sizeof(SSubmitReq2Msg);
6,390✔
1703

1704
    STREAM_CHECK_RET_GOTO(scanSubmitData(pVnode, sStreamReaderInfo, pBody, bodyLen, ranges, rsp, wCont->version));
6,390!
1705
  }
1706
  // printDataBlock(rsp->dataBlock, __func__, "processWalVerDataNew");
1707
  STREAM_CHECK_RET_GOTO(filterData(rsp, sStreamReaderInfo));
707!
1708
  rsp->totalRows = ((SSDataBlock*)rsp->dataBlock)->info.rows;
710✔
1709

1710
end:
5,326✔
1711
  ST_TASK_DLOG("vgId:%d %s end, get result totalRows:%d, process:%"PRId64"/%"PRId64, TD_VID(pVnode), __func__, 
5,326✔
1712
            rsp->totalRows, rsp->ver, walGetAppliedVer(pWalReader->pWal));
1713
  walCloseReader(pWalReader);
5,325✔
1714
  return code;
5,329✔
1715
}
1716

1717
static int32_t buildScheamFromMeta(SVnode* pVnode, int64_t uid, SArray** schemas) {
180✔
1718
  int32_t code = 0;
180✔
1719
  int32_t lino = 0;
180✔
1720
  SMetaReader metaReader = {0};
180✔
1721
  SStorageAPI api = {0};
180✔
1722
  initStorageAPI(&api);
180✔
1723
  *schemas = taosArrayInit(8, sizeof(SSchema));
180✔
1724
  STREAM_CHECK_NULL_GOTO(*schemas, terrno);
180!
1725
  
1726
  api.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &api.metaFn);
180✔
1727
  STREAM_CHECK_RET_GOTO(api.metaReaderFn.getTableEntryByUid(&metaReader, uid));
180!
1728

1729
  SSchemaWrapper* sSchemaWrapper = NULL;
180✔
1730
  if (metaReader.me.type == TD_CHILD_TABLE) {
180!
1731
    int64_t suid = metaReader.me.ctbEntry.suid;
180✔
1732
    tDecoderClear(&metaReader.coder);
180✔
1733
    STREAM_CHECK_RET_GOTO(api.metaReaderFn.getTableEntryByUid(&metaReader, suid));
180!
1734
    sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
180✔
1735
  } else if (metaReader.me.type == TD_NORMAL_TABLE) {
×
1736
    sSchemaWrapper = &metaReader.me.ntbEntry.schemaRow;
×
1737
  } else {
1738
    qError("invalid table type:%d", metaReader.me.type);
×
1739
  }
1740

1741
  for (size_t j = 0; j < sSchemaWrapper->nCols; j++) {
1,004✔
1742
    SSchema* s = sSchemaWrapper->pSchema + j;
824✔
1743
    STREAM_CHECK_NULL_GOTO(taosArrayPush(*schemas, s), terrno);
1,648!
1744
  }
1745

1746
end:
180✔
1747
  api.metaReaderFn.clearReader(&metaReader);
180✔
1748
  STREAM_PRINT_LOG_END(code, lino);
180!
1749
  if (code != 0)  {
180!
1750
    taosArrayDestroy(*schemas);
×
1751
    *schemas = NULL;
×
1752
  }
1753
  return code;
180✔
1754
}
1755

1756
static int32_t shrinkScheams(SArray* cols, SArray* schemas) {
180✔
1757
  int32_t code = 0;
180✔
1758
  int32_t lino = 0;
180✔
1759
  size_t  schemaLen = taosArrayGetSize(schemas);
180✔
1760
  STREAM_CHECK_RET_GOTO(taosArrayEnsureCap(schemas, schemaLen + taosArrayGetSize(cols)));
180!
1761
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
568✔
1762
    col_id_t* id = taosArrayGet(cols, i);
388✔
1763
    STREAM_CHECK_NULL_GOTO(id, terrno);
388!
1764
    for (size_t i = 0; i < schemaLen; i++) {
912!
1765
      SSchema* s = taosArrayGet(schemas, i);
912✔
1766
      STREAM_CHECK_NULL_GOTO(s, terrno);
912!
1767
      if (*id == s->colId) {
912✔
1768
        STREAM_CHECK_NULL_GOTO(taosArrayPush(schemas, s), terrno);
388!
1769
        break;
388✔
1770
      }
1771
    }
1772
  }
1773
  taosArrayPopFrontBatch(schemas, schemaLen);
180✔
1774

1775
end:
180✔
1776
  return code;
180✔
1777
}
1778

1779
static int32_t processWalVerDataVTable(SVnode* pVnode, SArray *cids, int64_t ver,
×
1780
  int64_t uid, STimeWindow* window, SSDataBlock** pBlock) {
1781
  int32_t      code = 0;
×
1782
  int32_t      lino = 0;
×
1783
  SArray*      schemas = NULL;
×
1784

1785
  SSDataBlock* pBlock2 = NULL;
×
1786

1787
  STREAM_CHECK_RET_GOTO(buildScheamFromMeta(pVnode, uid, &schemas));
×
1788
  STREAM_CHECK_RET_GOTO(shrinkScheams(cids, schemas));
×
1789
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, &pBlock2));
×
1790

1791
  pBlock2->info.id.uid = uid;
×
1792

1793
  // STREAM_CHECK_RET_GOTO(scanWalOneVer(pVnode, pBlock2, ver, uid, window));
1794
  //printDataBlock(pBlock2, __func__, "");
1795

1796
  *pBlock = pBlock2;
×
1797
  pBlock2 = NULL;
×
1798

1799
end:
×
1800
  STREAM_PRINT_LOG_END(code, lino);
×
1801
  blockDataDestroy(pBlock2);
×
1802
  taosArrayDestroy(schemas);
×
1803
  return code;
×
1804
}
1805

1806
static int32_t createTSAndCondition(int64_t start, int64_t end, SLogicConditionNode** pCond,
×
1807
                                    STargetNode* pTargetNodeTs) {
1808
  int32_t code = 0;
×
1809
  int32_t lino = 0;
×
1810

1811
  SColumnNode*         pCol = NULL;
×
1812
  SColumnNode*         pCol1 = NULL;
×
1813
  SValueNode*          pVal = NULL;
×
1814
  SValueNode*          pVal1 = NULL;
×
1815
  SOperatorNode*       op = NULL;
×
1816
  SOperatorNode*       op1 = NULL;
×
1817
  SLogicConditionNode* cond = NULL;
×
1818

1819
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pCol));
×
1820
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
×
1821
  pCol->node.resType.type = TSDB_DATA_TYPE_TIMESTAMP;
×
1822
  pCol->node.resType.bytes = LONG_BYTES;
×
1823
  pCol->slotId = pTargetNodeTs->slotId;
×
1824
  pCol->dataBlockId = pTargetNodeTs->dataBlockId;
×
1825

1826
  STREAM_CHECK_RET_GOTO(nodesCloneNode((SNode*)pCol, (SNode**)&pCol1));
×
1827

1828
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&pVal));
×
1829
  pVal->node.resType.type = TSDB_DATA_TYPE_BIGINT;
×
1830
  pVal->node.resType.bytes = LONG_BYTES;
×
1831
  pVal->datum.i = start;
×
1832
  pVal->typeData = start;
×
1833

1834
  STREAM_CHECK_RET_GOTO(nodesCloneNode((SNode*)pVal, (SNode**)&pVal1));
×
1835
  pVal1->datum.i = end;
×
1836
  pVal1->typeData = end;
×
1837

1838
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op));
×
1839
  op->opType = OP_TYPE_GREATER_EQUAL;
×
1840
  op->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
1841
  op->node.resType.bytes = CHAR_BYTES;
×
1842
  op->pLeft = (SNode*)pCol;
×
1843
  op->pRight = (SNode*)pVal;
×
1844
  pCol = NULL;
×
1845
  pVal = NULL;
×
1846

1847
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op1));
×
1848
  op1->opType = OP_TYPE_LOWER_EQUAL;
×
1849
  op1->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
1850
  op1->node.resType.bytes = CHAR_BYTES;
×
1851
  op1->pLeft = (SNode*)pCol1;
×
1852
  op1->pRight = (SNode*)pVal1;
×
1853
  pCol1 = NULL;
×
1854
  pVal1 = NULL;
×
1855

1856
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond));
×
1857
  cond->condType = LOGIC_COND_TYPE_AND;
×
1858
  cond->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
1859
  cond->node.resType.bytes = CHAR_BYTES;
×
1860
  STREAM_CHECK_RET_GOTO(nodesMakeList(&cond->pParameterList));
×
1861
  STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)op));
×
1862
  op = NULL;
×
1863
  STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)op1));
×
1864
  op1 = NULL;
×
1865

1866
  *pCond = cond;
×
1867

1868
end:
×
1869
  if (code != 0) {
×
1870
    nodesDestroyNode((SNode*)pCol);
×
1871
    nodesDestroyNode((SNode*)pCol1);
×
1872
    nodesDestroyNode((SNode*)pVal);
×
1873
    nodesDestroyNode((SNode*)pVal1);
×
1874
    nodesDestroyNode((SNode*)op);
×
1875
    nodesDestroyNode((SNode*)op1);
×
1876
    nodesDestroyNode((SNode*)cond);
×
1877
  }
1878
  STREAM_PRINT_LOG_END(code, lino);
×
1879

1880
  return code;
×
1881
}
1882

1883
/*
1884
static int32_t createExternalConditions(SStreamRuntimeFuncInfo* data, SLogicConditionNode** pCond, STargetNode* pTargetNodeTs, STimeRangeNode* node) {
1885
  int32_t              code = 0;
1886
  int32_t              lino = 0;
1887
  SLogicConditionNode* pAndCondition = NULL;
1888
  SLogicConditionNode* cond = NULL;
1889

1890
  if (pTargetNodeTs == NULL) {
1891
    vError("stream reader %s no ts column", __func__);
1892
    return TSDB_CODE_STREAM_NOT_TABLE_SCAN_PLAN;
1893
  }
1894
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond));
1895
  cond->condType = LOGIC_COND_TYPE_OR;
1896
  cond->node.resType.type = TSDB_DATA_TYPE_BOOL;
1897
  cond->node.resType.bytes = CHAR_BYTES;
1898
  STREAM_CHECK_RET_GOTO(nodesMakeList(&cond->pParameterList));
1899

1900
  for (int i = 0; i < taosArrayGetSize(data->pStreamPesudoFuncVals); ++i) {
1901
    data->curIdx = i;
1902

1903
    SReadHandle handle = {0};
1904
    calcTimeRange(node, data, &handle.winRange, &handle.winRangeValid);
1905
    if (!handle.winRangeValid) {
1906
      stError("stream reader %s invalid time range, skey:%" PRId64 ", ekey:%" PRId64, __func__, handle.winRange.skey,
1907
              handle.winRange.ekey);
1908
      continue;
1909
    }
1910
    STREAM_CHECK_RET_GOTO(createTSAndCondition(handle.winRange.skey, handle.winRange.ekey, &pAndCondition, pTargetNodeTs));
1911
    stDebug("%s create condition skey:%" PRId64 ", eksy:%" PRId64, __func__, handle.winRange.skey, handle.winRange.ekey);
1912
    STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)pAndCondition));
1913
    pAndCondition = NULL;
1914
  }
1915

1916
  *pCond = cond;
1917

1918
end:
1919
  if (code != 0) {
1920
    nodesDestroyNode((SNode*)pAndCondition);
1921
    nodesDestroyNode((SNode*)cond);
1922
  }
1923
  STREAM_PRINT_LOG_END(code, lino);
1924

1925
  return code;
1926
}
1927
*/
1928

1929
static int32_t processCalaTimeRange(SStreamTriggerReaderCalcInfo* sStreamReaderCalcInfo, SResFetchReq* req,
405✔
1930
                                    STimeRangeNode* node, SReadHandle* handle) {
1931
  int32_t code = 0;
405✔
1932
  int32_t lino = 0;
405✔
1933
  SArray* funcVals = NULL;
405✔
1934
  if (req->pStRtFuncInfo->withExternalWindow) {
405✔
1935
/*
1936
    nodesDestroyNode(sStreamReaderCalcInfo->tsConditions);
1937
    filterFreeInfo(sStreamReaderCalcInfo->pFilterInfo);
1938
    sStreamReaderCalcInfo->pFilterInfo = NULL;
1939

1940
    STREAM_CHECK_RET_GOTO(createExternalConditions(req->pStRtFuncInfo,
1941
                                                   (SLogicConditionNode**)&sStreamReaderCalcInfo->tsConditions,
1942
                                                   sStreamReaderCalcInfo->pTargetNodeTs, node));
1943

1944
    STREAM_CHECK_RET_GOTO(filterInitFromNode((SNode*)sStreamReaderCalcInfo->tsConditions,
1945
                                             (SFilterInfo**)&sStreamReaderCalcInfo->pFilterInfo,
1946
                                             FLT_OPTION_NO_REWRITE | FLT_OPTION_SCALAR_MODE, NULL));
1947
*/                                             
1948
    sStreamReaderCalcInfo->tmpRtFuncInfo.curIdx = 0;
205✔
1949
    sStreamReaderCalcInfo->tmpRtFuncInfo.triggerType = req->pStRtFuncInfo->triggerType;
205✔
1950
    
1951
    SSTriggerCalcParam* pFirst = taosArrayGet(req->pStRtFuncInfo->pStreamPesudoFuncVals, 0);
205✔
1952
    SSTriggerCalcParam* pLast = taosArrayGetLast(req->pStRtFuncInfo->pStreamPesudoFuncVals);
205✔
1953
    STREAM_CHECK_NULL_GOTO(pFirst, terrno);
205!
1954
    STREAM_CHECK_NULL_GOTO(pLast, terrno);
205!
1955

1956
    if (!node->needCalc) {
205✔
1957
      handle->winRange.skey = pFirst->wstart;
153✔
1958
      handle->winRange.ekey = pLast->wend;
153✔
1959
      handle->winRangeValid = true;
153✔
1960
      if (req->pStRtFuncInfo->triggerType == STREAM_TRIGGER_SLIDING) {
153✔
1961
        handle->winRange.ekey--;
107✔
1962
      }
1963
    } else {
1964
      SSTriggerCalcParam* pTmp = taosArrayGet(sStreamReaderCalcInfo->tmpRtFuncInfo.pStreamPesudoFuncVals, 0);
52✔
1965
      memcpy(pTmp, pFirst, sizeof(*pTmp));
52✔
1966

1967
      STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, &sStreamReaderCalcInfo->tmpRtFuncInfo, &handle->winRange, &handle->winRangeValid, 1));
52!
1968
      if (handle->winRangeValid) {
52!
1969
        int64_t skey = handle->winRange.skey;
52✔
1970

1971
        memcpy(pTmp, pLast, sizeof(*pTmp));
52✔
1972
        STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, &sStreamReaderCalcInfo->tmpRtFuncInfo, &handle->winRange, &handle->winRangeValid, 2));
52!
1973

1974
        if (handle->winRangeValid) {
52!
1975
          handle->winRange.skey = skey;
52✔
1976
        }
1977
      }
1978
      handle->winRange.ekey--;
52✔
1979
    }
1980
  } else {
1981
    if (!node->needCalc) {
200!
1982
      SSTriggerCalcParam* pCurr = taosArrayGet(req->pStRtFuncInfo->pStreamPesudoFuncVals, req->pStRtFuncInfo->curIdx);
200✔
1983
      handle->winRange.skey = pCurr->wstart;
200✔
1984
      handle->winRange.ekey = pCurr->wend;
200✔
1985
      handle->winRangeValid = true;
200✔
1986
      if (req->pStRtFuncInfo->triggerType == STREAM_TRIGGER_SLIDING) {
200✔
1987
        handle->winRange.ekey--;
188✔
1988
      }
1989
    } else {
1990
      STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, req->pStRtFuncInfo, &handle->winRange, &handle->winRangeValid, 3));
×
1991
      handle->winRange.ekey--;
×
1992
    }
1993
  }
1994

1995
  stDebug("%s withExternalWindow is %d, skey:%" PRId64 ", ekey:%" PRId64 ", validRange:%d", 
405✔
1996
      __func__, req->pStRtFuncInfo->withExternalWindow, handle->winRange.skey, handle->winRange.ekey, handle->winRangeValid);
1997

1998
end:
35✔
1999
  taosArrayDestroy(funcVals);
405✔
2000
  return code;
405✔
2001
}
2002

2003
static int32_t processTs(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
823✔
2004
                                  SStreamReaderTaskInner* pTaskInner) {
2005
  int32_t code = 0;
823✔
2006
  int32_t lino = 0;
823✔
2007

2008
  void* pTask = sStreamReaderInfo->pTask;
823✔
2009
  tsRsp->tsInfo = taosArrayInit(qStreamGetTableListGroupNum(pTaskInner->pTableList), sizeof(STsInfo));
823✔
2010
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
824!
2011
  while (true) {
747✔
2012
    bool hasNext = false;
1,571✔
2013
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
1,571!
2014
    if (hasNext) {
1,570✔
2015
      pTaskInner->api.tsdReader.tsdReaderReleaseDataBlock(pTaskInner->pReader);
588✔
2016
      STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
587✔
2017
      STREAM_CHECK_NULL_GOTO(tsInfo, terrno)
588!
2018
      if (pTaskInner->options.order == TSDB_ORDER_ASC) {
588✔
2019
        tsInfo->ts = pTaskInner->pResBlock->info.window.skey;
390✔
2020
      } else {
2021
        tsInfo->ts = pTaskInner->pResBlock->info.window.ekey;
198✔
2022
      }
2023
      tsInfo->gId = (sStreamReaderInfo->groupByTbname || sStreamReaderInfo->tableType != TSDB_SUPER_TABLE) ? 
118✔
2024
                    pTaskInner->pResBlock->info.id.uid : qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
706✔
2025
      ST_TASK_DLOG("vgId:%d %s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, TD_VID(pVnode), __func__, tsInfo->ts,
588✔
2026
              tsInfo->gId, tsRsp->ver);
2027
    }
2028
    
2029
    pTaskInner->currentGroupIndex++;
1,570✔
2030
    if (pTaskInner->currentGroupIndex >= qStreamGetTableListGroupNum(pTaskInner->pTableList) || pTaskInner->options.gid != 0) {
1,570✔
2031
      break;
2032
    }
2033
    STREAM_CHECK_RET_GOTO(resetTsdbReader(pTaskInner));
747!
2034
  }
2035

2036
end:
824✔
2037
  STREAM_PRINT_LOG_END_WITHID(code, lino);
824!
2038
  return code;
824✔
2039
}
2040

2041
static int32_t vnodeProcessStreamSetTableReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
106✔
2042
  int32_t code = 0;
106✔
2043
  int32_t lino = 0;
106✔
2044
  void*   buf = NULL;
106✔
2045
  size_t  size = 0;
106✔
2046
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
106!
2047
  void* pTask = sStreamReaderInfo->pTask;
106✔
2048

2049
  ST_TASK_DLOG("vgId:%d %s start, trigger hash size:%d, calc hash size:%d", TD_VID(pVnode), __func__,
106✔
2050
                tSimpleHashGetSize(req->setTableReq.uidInfoTrigger), tSimpleHashGetSize(req->setTableReq.uidInfoCalc));
2051

2052
  TSWAP(sStreamReaderInfo->uidHashTrigger, req->setTableReq.uidInfoTrigger);
106✔
2053
  TSWAP(sStreamReaderInfo->uidHashCalc, req->setTableReq.uidInfoCalc);
106✔
2054
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->uidHashTrigger, TSDB_CODE_INVALID_PARA);
106!
2055
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->uidHashCalc, TSDB_CODE_INVALID_PARA);
106!
2056

2057
  sStreamReaderInfo->isVtableStream = true;
106✔
2058
  sStreamReaderInfo->groupByTbname = true;
106✔
2059
end:
106✔
2060
  STREAM_PRINT_LOG_END_WITHID(code, lino);
106!
2061
  SRpcMsg rsp = {
106✔
2062
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2063
  tmsgSendRsp(&rsp);
106✔
2064
  return code;
106✔
2065
}
2066

2067
static int32_t vnodeProcessStreamLastTsReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
442✔
2068
  int32_t                 code = 0;
442✔
2069
  int32_t                 lino = 0;
442✔
2070
  SStreamReaderTaskInner* pTaskInner = NULL;
442✔
2071
  SStreamTsResponse       lastTsRsp = {0};
442✔
2072
  void*                   buf = NULL;
442✔
2073
  size_t                  size = 0;
442✔
2074

2075
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
442!
2076
  void* pTask = sStreamReaderInfo->pTask;
442✔
2077

2078
  ST_TASK_DLOG("vgId:%d %s start", TD_VID(pVnode), __func__);
442✔
2079

2080
  BUILD_OPTION(options, sStreamReaderInfo, -1, TSDB_ORDER_DESC, INT64_MIN, INT64_MAX, sStreamReaderInfo->tsSchemas, true,
443✔
2081
               STREAM_SCAN_GROUP_ONE_BY_ONE, 0, true, sStreamReaderInfo->uidHashTrigger);
2082
  SStorageAPI api = {0};
443✔
2083
  initStorageAPI(&api);
443✔
2084
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, NULL, &api));
443!
2085

2086
  lastTsRsp.ver = pVnode->state.applied + 1;
443✔
2087

2088
  STREAM_CHECK_RET_GOTO(processTs(pVnode, &lastTsRsp, sStreamReaderInfo, pTaskInner));
443!
2089
  ST_TASK_DLOG("vgId:%d %s get result, ver:%" PRId64, TD_VID(pVnode), __func__, lastTsRsp.ver);
443✔
2090
  STREAM_CHECK_RET_GOTO(buildTsRsp(&lastTsRsp, &buf, &size))
443!
2091
  if (stDebugFlag & DEBUG_DEBUG) {
443✔
2092
    int32_t nInfo = taosArrayGetSize(lastTsRsp.tsInfo);
325✔
2093
    for (int32_t i = 0; i < nInfo; i++) {
483✔
2094
      STsInfo* tsInfo = TARRAY_GET_ELEM(lastTsRsp.tsInfo, i);
158✔
2095
      ST_TASK_DLOG("vgId:%d %s get ts:%" PRId64 ", gId:%" PRIu64, TD_VID(pVnode), __func__, tsInfo->ts, tsInfo->gId);
158!
2096
    }
2097
  }
2098

2099
end:
443✔
2100
  STREAM_PRINT_LOG_END_WITHID(code, lino);
443!
2101
  SRpcMsg rsp = {
443✔
2102
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2103
  tmsgSendRsp(&rsp);
443✔
2104
  taosArrayDestroy(lastTsRsp.tsInfo);
443✔
2105
  releaseStreamTask(&pTaskInner);
443✔
2106
  return code;
443✔
2107
}
2108

2109
static int32_t vnodeProcessStreamFirstTsReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
390✔
2110
  int32_t                 code = 0;
390✔
2111
  int32_t                 lino = 0;
390✔
2112
  SStreamReaderTaskInner* pTaskInner = NULL;
390✔
2113
  SStreamTsResponse       firstTsRsp = {0};
390✔
2114
  void*                   buf = NULL;
390✔
2115
  size_t                  size = 0;
390✔
2116

2117
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
390!
2118
  void* pTask = sStreamReaderInfo->pTask;
390✔
2119
  ST_TASK_DLOG("vgId:%d %s start, startTime:%"PRId64" ver:%"PRId64" gid:%"PRId64, TD_VID(pVnode), __func__, req->firstTsReq.startTime, req->firstTsReq.ver, req->firstTsReq.gid);
390✔
2120
  BUILD_OPTION(options, sStreamReaderInfo, req->firstTsReq.ver, TSDB_ORDER_ASC, req->firstTsReq.startTime, INT64_MAX, sStreamReaderInfo->tsSchemas, true,
390✔
2121
               STREAM_SCAN_GROUP_ONE_BY_ONE, req->firstTsReq.gid, true, sStreamReaderInfo->uidHashTrigger);
2122
  SStorageAPI api = {0};
390✔
2123
  initStorageAPI(&api);
390✔
2124
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, NULL, &api));
391✔
2125
  
2126
  firstTsRsp.ver = pVnode->state.applied;
380✔
2127
  STREAM_CHECK_RET_GOTO(processTs(pVnode, &firstTsRsp, sStreamReaderInfo, pTaskInner));
380!
2128

2129
  ST_TASK_DLOG("vgId:%d %s get result size:%"PRIzu", ver:%"PRId64, TD_VID(pVnode), __func__, taosArrayGetSize(firstTsRsp.tsInfo), firstTsRsp.ver);
381✔
2130
  STREAM_CHECK_RET_GOTO(buildTsRsp(&firstTsRsp, &buf, &size));
381!
2131
  if (stDebugFlag & DEBUG_DEBUG) {
380✔
2132
    int32_t nInfo = taosArrayGetSize(firstTsRsp.tsInfo);
255✔
2133
    for (int32_t i = 0; i < nInfo; i++) {
484✔
2134
      STsInfo* tsInfo = TARRAY_GET_ELEM(firstTsRsp.tsInfo, i);
229✔
2135
      ST_TASK_DLOG("vgId:%d %s get ts:%" PRId64 ", gId:%" PRIu64, TD_VID(pVnode), __func__, tsInfo->ts, tsInfo->gId);
229!
2136
    }
2137
  }
2138

2139
end:
380✔
2140
  STREAM_PRINT_LOG_END_WITHID(code, lino);
389!
2141
  SRpcMsg rsp = {
389✔
2142
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2143
  tmsgSendRsp(&rsp);
389✔
2144
  taosArrayDestroy(firstTsRsp.tsInfo);
391✔
2145
  releaseStreamTask(&pTaskInner);
391✔
2146
  return code;
391✔
2147
}
2148

2149
static int32_t vnodeProcessStreamTsdbMetaReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
752✔
2150
  int32_t code = 0;
752✔
2151
  int32_t lino = 0;
752✔
2152
  void*   buf = NULL;
752✔
2153
  size_t  size = 0;
752✔
2154

2155
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
752!
2156
  void* pTask = sStreamReaderInfo->pTask;
752✔
2157
  ST_TASK_DLOG("vgId:%d %s start", TD_VID(pVnode), __func__);
752✔
2158

2159
  SStreamReaderTaskInner* pTaskInner = NULL;
752✔
2160
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_META);
752✔
2161

2162
  if (req->base.type == STRIGGER_PULL_TSDB_META) {
752!
2163
    BUILD_OPTION(options, sStreamReaderInfo, req->tsdbMetaReq.ver, req->tsdbMetaReq.order, req->tsdbMetaReq.startTime, req->tsdbMetaReq.endTime, sStreamReaderInfo->tsSchemas, true, 
752✔
2164
      (req->tsdbMetaReq.gid != 0 ? STREAM_SCAN_GROUP_ONE_BY_ONE : STREAM_SCAN_ALL), req->tsdbMetaReq.gid, true, sStreamReaderInfo->uidHashTrigger);
2165
    SStorageAPI api = {0};
752✔
2166
    initStorageAPI(&api);
752✔
2167
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, NULL, &api));
752✔
2168
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
743!
2169
    
2170
    STREAM_CHECK_RET_GOTO(createBlockForTsdbMeta(&pTaskInner->pResBlockDst, sStreamReaderInfo->isVtableStream));
743!
2171
  } else {
2172
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
2173
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
2174
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
2175
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
2176
  }
2177

2178
  blockDataCleanup(pTaskInner->pResBlockDst);
743✔
2179
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pTaskInner->pResBlockDst, STREAM_RETURN_ROWS_NUM));
743!
2180
  bool hasNext = true;
743✔
2181
  while (true) {
239✔
2182
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
982!
2183
    if (!hasNext) {
982✔
2184
      break;
743✔
2185
    }
2186
    pTaskInner->api.tsdReader.tsdReaderReleaseDataBlock(pTaskInner->pReader);
239✔
2187
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
239✔
2188

2189
    int32_t index = 0;
239✔
2190
    STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.window.skey));
239!
2191
    STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.window.ekey));
239!
2192
    STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.id.uid));
239!
2193
    if (!sStreamReaderInfo->isVtableStream) {
239✔
2194
      STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.id.groupId));
68!
2195
    }
2196
    STREAM_CHECK_RET_GOTO(addColData(pTaskInner->pResBlockDst, index++, &pTaskInner->pResBlock->info.rows));
239!
2197

2198
    stDebug("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
239✔
2199
            TD_VID(pVnode), __func__, pTaskInner->pResBlock->info.window.skey, pTaskInner->pResBlock->info.window.ekey,
2200
            pTaskInner->pResBlock->info.id.uid, pTaskInner->pResBlock->info.id.groupId, pTaskInner->pResBlock->info.rows);
2201
            pTaskInner->pResBlockDst->info.rows++;
239✔
2202
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
239!
2203
      break;
×
2204
    }
2205
  }
2206

2207
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
743✔
2208
  STREAM_CHECK_RET_GOTO(buildRsp(pTaskInner->pResBlockDst, &buf, &size));
743!
2209
  printDataBlock(pTaskInner->pResBlockDst, __func__, "meta", ((SStreamTask *)sStreamReaderInfo->pTask)->streamId);
743✔
2210
  if (!hasNext) {
743!
2211
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
743!
2212
  }
2213

2214
end:
743✔
2215
  STREAM_PRINT_LOG_END_WITHID(code, lino);
752!
2216
  SRpcMsg rsp = {
752✔
2217
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2218
  tmsgSendRsp(&rsp);
752✔
2219
  return code;
752✔
2220
}
2221

2222
static int32_t vnodeProcessStreamTsdbTsDataReqNonVTable(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
35✔
2223
  int32_t                 code = 0;
35✔
2224
  int32_t                 lino = 0;
35✔
2225
  SStreamReaderTaskInner* pTaskInner = NULL;
35✔
2226
  void*                   buf = NULL;
35✔
2227
  size_t                  size = 0;
35✔
2228
  SSDataBlock*            pBlockRes = NULL;
35✔
2229

2230
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
35!
2231
  void* pTask = sStreamReaderInfo->pTask;
35✔
2232
  ST_TASK_DLOG("vgId:%d %s start, ver:%"PRId64",skey:%"PRId64",ekey:%"PRId64",uid:%"PRId64",suid:%"PRId64, TD_VID(pVnode), __func__, req->tsdbTsDataReq.ver, 
35!
2233
                req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey, 
2234
                req->tsdbTsDataReq.uid, req->tsdbTsDataReq.suid);
2235

2236
  BUILD_OPTION(options, sStreamReaderInfo, req->tsdbTsDataReq.ver, TSDB_ORDER_ASC, req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey,
35✔
2237
               sStreamReaderInfo->triggerCols, false, STREAM_SCAN_ALL, 0, true, NULL);
2238
  options.uid = req->tsdbTsDataReq.uid;
35✔
2239
  SStorageAPI api = {0};
35✔
2240
  initStorageAPI(&api);
35✔
2241
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, &api));
35!
2242
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
35!
2243
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->tsBlock, false, &pBlockRes));
35!
2244

2245
  while (1) {
35✔
2246
    bool hasNext = false;
70✔
2247
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
70!
2248
    if (!hasNext) {
70✔
2249
      break;
35✔
2250
    }
2251
    if (!sStreamReaderInfo->isVtableStream){
35!
2252
      pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
35✔
2253
    }
2254

2255
    SSDataBlock* pBlock = NULL;
35✔
2256
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
35!
2257
    if (pBlock != NULL && pBlock->info.rows > 0) {
35!
2258
      STREAM_CHECK_RET_GOTO(processTag(pVnode, sStreamReaderInfo, false, &api, pBlock->info.id.uid, pBlock,
35!
2259
          0, pBlock->info.rows, 1));
2260
    }
2261
    
2262
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, pTaskInner->pFilterInfo, NULL));
35!
2263
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
35!
2264
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
35!
2265
            TD_VID(pVnode), __func__, pTaskInner->pResBlock->info.window.skey, pTaskInner->pResBlock->info.window.ekey,
2266
            pTaskInner->pResBlock->info.id.uid, pTaskInner->pResBlock->info.id.groupId, pTaskInner->pResBlock->info.rows);
2267
  }
2268

2269
  blockDataTransform(pBlockRes, pTaskInner->pResBlockDst);
35✔
2270

2271
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
35!
2272
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
35!
2273

2274
end:
35✔
2275
  STREAM_PRINT_LOG_END_WITHID(code, lino);
35!
2276
  SRpcMsg rsp = {
35✔
2277
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2278
  tmsgSendRsp(&rsp);
35✔
2279
  blockDataDestroy(pBlockRes);
35✔
2280

2281
  releaseStreamTask(&pTaskInner);
35✔
2282
  return code;
35✔
2283
}
2284

2285
static int32_t vnodeProcessStreamTsdbTsDataReqVTable(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
×
2286
  int32_t                 code = 0;
×
2287
  int32_t                 lino = 0;
×
2288
  SStreamReaderTaskInner* pTaskInner = NULL;
×
2289
  void*                   buf = NULL;
×
2290
  size_t                  size = 0;
×
2291
  SSDataBlock*            pBlockRes = NULL;
×
2292

2293
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
×
2294
  void* pTask = sStreamReaderInfo->pTask;
×
2295
  ST_TASK_ELOG("vgId:%d %s start, ver:%"PRId64",skey:%"PRId64",ekey:%"PRId64",uid:%"PRId64",suid:%"PRId64, TD_VID(pVnode), __func__, req->tsdbTsDataReq.ver, 
×
2296
                req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey, 
2297
                req->tsdbTsDataReq.uid, req->tsdbTsDataReq.suid);
2298

2299
  BUILD_OPTION(options, sStreamReaderInfo, req->tsdbTsDataReq.ver, TSDB_ORDER_ASC, req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey,
×
2300
               sStreamReaderInfo->tsSchemas, true, STREAM_SCAN_ALL, 0, true, NULL);
2301
  options.suid = req->tsdbTsDataReq.suid;
×
2302
  options.uid = req->tsdbTsDataReq.uid;
×
2303
  SStorageAPI api = {0};
×
2304
  initStorageAPI(&api);
×
2305
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->tsBlock, &api));
×
2306
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->tsBlock, false, &pBlockRes));
×
2307

2308
  while (1) {
×
2309
    bool hasNext = false;
×
2310
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
×
2311
    if (!hasNext) {
×
2312
      break;
×
2313
    }
2314

2315
    SSDataBlock* pBlock = NULL;
×
2316
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
×
2317
    STREAM_CHECK_RET_GOTO(blockDataMerge(pBlockRes, pBlock));
×
2318
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
×
2319
            TD_VID(pVnode), __func__, pBlockRes->info.window.skey, pBlockRes->info.window.ekey,
2320
            pBlockRes->info.id.uid, pBlockRes->info.id.groupId, pBlockRes->info.rows);
2321
  }
2322

2323
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlockRes->info.rows);
×
2324
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
×
2325

2326
end:
×
2327
  STREAM_PRINT_LOG_END_WITHID(code, lino);
×
2328
  SRpcMsg rsp = {
×
2329
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2330
  tmsgSendRsp(&rsp);
×
2331
  blockDataDestroy(pBlockRes);
×
2332

2333
  releaseStreamTask(&pTaskInner);
×
2334
  return code;
×
2335
}
2336

2337
static int32_t vnodeProcessStreamTsdbTriggerDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
293✔
2338
  int32_t code = 0;
293✔
2339
  int32_t lino = 0;
293✔
2340
  void*   buf = NULL;
293✔
2341
  size_t  size = 0;
293✔
2342

2343
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
293!
2344
  SStreamReaderTaskInner* pTaskInner = NULL;
293✔
2345
  void* pTask = sStreamReaderInfo->pTask;
293✔
2346
  ST_TASK_DLOG("vgId:%d %s start. ver:%"PRId64",order:%d,startTs:%"PRId64",gid:%"PRId64, TD_VID(pVnode), __func__, req->tsdbTriggerDataReq.ver, req->tsdbTriggerDataReq.order, req->tsdbTriggerDataReq.startTime, req->tsdbTriggerDataReq.gid);
293✔
2347
  
2348
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_TRIGGER_DATA);
293✔
2349

2350
  if (req->base.type == STRIGGER_PULL_TSDB_TRIGGER_DATA) {
293✔
2351
    BUILD_OPTION(options, sStreamReaderInfo, req->tsdbTriggerDataReq.ver, req->tsdbTriggerDataReq.order, req->tsdbTriggerDataReq.startTime, INT64_MAX,
141✔
2352
                 sStreamReaderInfo->triggerCols, false, (req->tsdbTriggerDataReq.gid != 0 ? STREAM_SCAN_GROUP_ONE_BY_ONE : STREAM_SCAN_ALL), 
2353
                 req->tsdbTriggerDataReq.gid, true, NULL);
2354
    SStorageAPI api = {0};
141✔
2355
    initStorageAPI(&api);
141✔
2356
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, &api));
141!
2357

2358
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
141!
2359
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
141!
2360
  } else {
2361
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
152✔
2362
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
152!
2363
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
152✔
2364
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
152!
2365
  }
2366

2367
  blockDataCleanup(pTaskInner->pResBlockDst);
293✔
2368
  bool hasNext = true;
293✔
2369
  while (1) {
×
2370
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
293!
2371
    if (!hasNext) {
293✔
2372
      break;
141✔
2373
    }
2374
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
152✔
2375
    pTaskInner->pResBlockDst->info.id.groupId = qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
152✔
2376

2377
    SSDataBlock* pBlock = NULL;
152✔
2378
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
152!
2379
    if (pBlock != NULL && pBlock->info.rows > 0) {
152!
2380
      STREAM_CHECK_RET_GOTO(
152!
2381
        processTag(pVnode, sStreamReaderInfo, false, &pTaskInner->api, pBlock->info.id.uid, pBlock, 0, pBlock->info.rows, 1));
2382
    }
2383
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, pTaskInner->pFilterInfo, NULL));
152!
2384
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
152!
2385
    ST_TASK_DLOG("vgId:%d %s get skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
152✔
2386
            TD_VID(pVnode), __func__, pTaskInner->pResBlock->info.window.skey, pTaskInner->pResBlock->info.window.ekey,
2387
            pTaskInner->pResBlock->info.id.uid, pTaskInner->pResBlock->info.id.groupId, pTaskInner->pResBlock->info.rows);
2388
    if (pTaskInner->pResBlockDst->info.rows >= 0) { //todo
152!
2389
      break;
152✔
2390
    }
2391
  }
2392

2393
  STREAM_CHECK_RET_GOTO(buildRsp(pTaskInner->pResBlockDst, &buf, &size));
293!
2394
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
293✔
2395
  if (!hasNext) {
293✔
2396
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
141!
2397
  }
2398

2399
end:
293✔
2400
  STREAM_PRINT_LOG_END_WITHID(code, lino);
293!
2401
  SRpcMsg rsp = {
293✔
2402
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2403
  tmsgSendRsp(&rsp);
293✔
2404

2405
  return code;
293✔
2406
}
2407

2408
static int32_t vnodeProcessStreamTsdbCalcDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
28,323✔
2409
  int32_t code = 0;
28,323✔
2410
  int32_t lino = 0;
28,323✔
2411
  void*   buf = NULL;
28,323✔
2412
  size_t  size = 0;
28,323✔
2413
  SSDataBlock*            pBlockRes = NULL;
28,323✔
2414

2415
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
28,323!
2416
  void* pTask = sStreamReaderInfo->pTask;
28,323✔
2417
  ST_TASK_DLOG("vgId:%d %s start, skey:%"PRId64",ekey:%"PRId64",gid:%"PRId64, TD_VID(pVnode), __func__, 
28,323✔
2418
    req->tsdbCalcDataReq.skey, req->tsdbCalcDataReq.ekey, req->tsdbCalcDataReq.gid);
2419

2420
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->triggerCols, TSDB_CODE_STREAM_NOT_TABLE_SCAN_PLAN);
28,322!
2421

2422
  SStreamReaderTaskInner* pTaskInner = NULL;
28,322✔
2423
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_CALC_DATA);
28,322✔
2424

2425
  if (req->base.type == STRIGGER_PULL_TSDB_CALC_DATA) {
28,323!
2426
    BUILD_OPTION(options, sStreamReaderInfo, req->tsdbCalcDataReq.ver, TSDB_ORDER_ASC, req->tsdbCalcDataReq.skey, req->tsdbCalcDataReq.ekey,
28,323✔
2427
                 sStreamReaderInfo->triggerCols, false, STREAM_SCAN_GROUP_ONE_BY_ONE, req->tsdbCalcDataReq.gid, true, NULL);
2428
    SStorageAPI api = {0};
28,323✔
2429
    initStorageAPI(&api);
28,323✔
2430
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, &api));
28,322✔
2431

2432
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
28,198!
2433
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
28,197!
2434
  } else {
2435
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
2436
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
2437
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
2438
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
2439
  }
2440

2441
  blockDataCleanup(pTaskInner->pResBlockDst);
28,198✔
2442
  bool hasNext = true;
28,198✔
2443
  while (1) {
2,489✔
2444
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
30,687!
2445
    if (!hasNext) {
30,685✔
2446
      break;
28,191✔
2447
    }
2448
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupId(pTaskInner->pTableList, pTaskInner->pResBlock->info.id.uid);
2,494✔
2449

2450
    SSDataBlock* pBlock = NULL;
2,493✔
2451
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
2,493!
2452
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, pTaskInner->pFilterInfo, NULL));
2,493!
2453
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
2,492!
2454
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
2,489!
2455
      break;
×
2456
    }
2457
  }
2458

2459
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->calcResBlock, false, &pBlockRes));
28,191!
2460
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlockRes, pTaskInner->pResBlockDst->info.capacity));
28,193!
2461
  blockDataTransform(pBlockRes, pTaskInner->pResBlockDst);
28,195✔
2462
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
28,197!
2463
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlockRes->info.rows);
28,196✔
2464
  if (!hasNext) {
28,197!
2465
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
28,197!
2466
  }
2467

2468
end:
28,194✔
2469
  STREAM_PRINT_LOG_END_WITHID(code, lino);
28,319!
2470
  SRpcMsg rsp = {
28,322✔
2471
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2472
  tmsgSendRsp(&rsp);
28,322✔
2473
  blockDataDestroy(pBlockRes);
28,323✔
2474
  return code;
28,320✔
2475
}
2476

2477
static int32_t vnodeProcessStreamTsdbVirtalDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
180✔
2478
  int32_t code = 0;
180✔
2479
  int32_t lino = 0;
180✔
2480
  void*   buf = NULL;
180✔
2481
  size_t  size = 0;
180✔
2482
  int32_t* slotIdList = NULL;
180✔
2483
  SArray* sortedCid = NULL;
180✔
2484
  SArray* schemas = NULL;
180✔
2485
  
2486
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
180!
2487
  void* pTask = sStreamReaderInfo->pTask;
180✔
2488
  ST_TASK_DLOG("vgId:%d %s start", TD_VID(pVnode), __func__);
180✔
2489

2490
  SStreamReaderTaskInner* pTaskInner = NULL;
180✔
2491
  int64_t key = req->tsdbDataReq.uid;
180✔
2492

2493
  if (req->base.type == STRIGGER_PULL_TSDB_DATA) {
180!
2494
    // sort cid and build slotIdList
2495
    slotIdList = taosMemoryMalloc(taosArrayGetSize(req->tsdbDataReq.cids) * sizeof(int32_t));
180!
2496
    STREAM_CHECK_NULL_GOTO(slotIdList, terrno);
180!
2497
    sortedCid = taosArrayDup(req->tsdbDataReq.cids, NULL);
180✔
2498
    STREAM_CHECK_NULL_GOTO(sortedCid, terrno);
180!
2499
    taosArraySort(sortedCid, sortCid);
180✔
2500
    for (int32_t i = 0; i < taosArrayGetSize(req->tsdbDataReq.cids); i++) {
568✔
2501
      int16_t* cid = taosArrayGet(req->tsdbDataReq.cids, i);
388✔
2502
      STREAM_CHECK_NULL_GOTO(cid, terrno);
388!
2503
      for (int32_t j = 0; j < taosArrayGetSize(sortedCid); j++) {
632!
2504
        int16_t* cidSorted = taosArrayGet(sortedCid, j);
632✔
2505
        STREAM_CHECK_NULL_GOTO(cidSorted, terrno);
632!
2506
        if (*cid == *cidSorted) {
632✔
2507
          slotIdList[j] = i;
388✔
2508
          break;
388✔
2509
        }
2510
      }
2511
    }
2512

2513
    STREAM_CHECK_RET_GOTO(buildScheamFromMeta(pVnode, req->tsdbDataReq.uid, &schemas));
180!
2514
    STREAM_CHECK_RET_GOTO(shrinkScheams(req->tsdbDataReq.cids, schemas));
180!
2515
    BUILD_OPTION(options, sStreamReaderInfo, req->tsdbDataReq.ver, req->tsdbDataReq.order, req->tsdbDataReq.skey,
180✔
2516
                    req->tsdbDataReq.ekey, schemas, true, STREAM_SCAN_ALL, 0, false, NULL);
2517

2518
    options.suid = req->tsdbDataReq.suid;
180✔
2519
    options.uid = req->tsdbDataReq.uid;
180✔
2520

2521
    SStorageAPI api = {0};
180✔
2522
    initStorageAPI(&api);
180✔
2523
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, NULL, &api));
180!
2524
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
180!
2525

2526
    STableKeyInfo       keyInfo = {.uid = req->tsdbDataReq.uid};
180✔
2527
    cleanupQueryTableDataCond(&pTaskInner->cond);
180✔
2528
    taosArraySort(pTaskInner->options.schemas, sortSSchema);
180✔
2529

2530
    STREAM_CHECK_RET_GOTO(qStreamInitQueryTableDataCond(&pTaskInner->cond, pTaskInner->options.order, pTaskInner->options.schemas,
180!
2531
                                                        pTaskInner->options.isSchema, pTaskInner->options.twindows,
2532
                                                        pTaskInner->options.suid, pTaskInner->options.ver, &slotIdList));
2533
    STREAM_CHECK_RET_GOTO(pTaskInner->api.tsdReader.tsdReaderOpen(pVnode, &pTaskInner->cond, &keyInfo, 1, pTaskInner->pResBlock,
180!
2534
                                                             (void**)&pTaskInner->pReader, pTaskInner->idStr, NULL));
2535
    STREAM_CHECK_RET_GOTO(createOneDataBlock(pTaskInner->pResBlock, false, &pTaskInner->pResBlockDst));
180!
2536
  } else {
2537
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
2538
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
2539
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
2540
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
2541
  }
2542

2543
  blockDataCleanup(pTaskInner->pResBlockDst);
180✔
2544
  bool hasNext = true;
180✔
2545
  while (1) {
180✔
2546
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
360!
2547
    if (!hasNext) {
360✔
2548
      break;
180✔
2549
    }
2550

2551
    SSDataBlock* pBlock = NULL;
180✔
2552
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
180!
2553
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
180!
2554
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
180!
2555
      break;
×
2556
    }
2557
  }
2558
  STREAM_CHECK_RET_GOTO(buildRsp(pTaskInner->pResBlockDst, &buf, &size));
180!
2559
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
180✔
2560
  printDataBlock(pTaskInner->pResBlockDst, __func__, "tsdb_data", ((SStreamTask*)pTask)->streamId);
180✔
2561
  if (!hasNext) {
180!
2562
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
180!
2563
  }
2564

2565
end:
180✔
2566
  STREAM_PRINT_LOG_END_WITHID(code, lino);
180!
2567
  SRpcMsg rsp = {
180✔
2568
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2569
  tmsgSendRsp(&rsp);
180✔
2570
  taosMemFree(slotIdList);
180✔
2571
  taosArrayDestroy(sortedCid);
180✔
2572
  taosArrayDestroy(schemas);
180✔
2573
  return code;
180✔
2574
}
2575

2576
static int32_t vnodeProcessStreamWalMetaNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
10,272✔
2577
  int32_t      code = 0;
10,272✔
2578
  int32_t      lino = 0;
10,272✔
2579
  void*        buf = NULL;
10,272✔
2580
  size_t       size = 0;
10,272✔
2581
  int64_t      lastVer = 0;
10,272✔
2582
  SSTriggerWalNewRsp resultRsp = {0};
10,272✔
2583

2584
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
10,272✔
2585
  void* pTask = sStreamReaderInfo->pTask;
10,231✔
2586
  ST_TASK_DLOG("vgId:%d %s start, request paras lastVer:%" PRId64, TD_VID(pVnode), __func__, req->walMetaNewReq.lastVer);
10,231✔
2587

2588
  if (sStreamReaderInfo->metaBlock == NULL) {
10,232✔
2589
    STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&sStreamReaderInfo->metaBlock));
285!
2590
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(sStreamReaderInfo->metaBlock, STREAM_RETURN_ROWS_NUM));
285!
2591
  }
2592
  blockDataEmpty(sStreamReaderInfo->metaBlock);
10,232✔
2593
  resultRsp.metaBlock = sStreamReaderInfo->metaBlock;
10,211✔
2594
  resultRsp.ver = req->walMetaNewReq.lastVer;
10,211✔
2595
  STREAM_CHECK_RET_GOTO(processWalVerMetaNew(pVnode, &resultRsp, sStreamReaderInfo, req->walMetaNewReq.ctime));
10,211!
2596

2597
  ST_TASK_DLOG("vgId:%d %s get result last ver:%"PRId64" rows:%d", TD_VID(pVnode), __func__, resultRsp.ver, resultRsp.totalRows);
10,232✔
2598
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
10,232✔
2599
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp, NULL);
233✔
2600
  buf = rpcMallocCont(size);
233✔
2601
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp, NULL);
233✔
2602
  printDataBlock(sStreamReaderInfo->metaBlock, __func__, "meta", ((SStreamTask*)pTask)->streamId);
233✔
2603

2604
end:
10,269✔
2605
  if (resultRsp.totalRows == 0) {
10,269✔
2606
    code = TSDB_CODE_STREAM_NO_DATA;
10,036✔
2607
    buf = rpcMallocCont(sizeof(int64_t));
10,036✔
2608
    *(int64_t *)buf = resultRsp.ver;
10,041✔
2609
    size = sizeof(int64_t);
10,041✔
2610
  }
2611
  SRpcMsg rsp = {
10,274✔
2612
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2613
  tmsgSendRsp(&rsp);
10,274✔
2614
  if (code == TSDB_CODE_STREAM_NO_DATA){
10,271✔
2615
    code = 0;
10,042✔
2616
  }
2617
  STREAM_PRINT_LOG_END_WITHID(code, lino);
10,271!
2618
  blockDataDestroy(resultRsp.deleteBlock);
10,273✔
2619
  blockDataDestroy(resultRsp.dropBlock);
10,272✔
2620

2621
  return code;
10,276✔
2622
}
2623
static int32_t vnodeProcessStreamWalMetaDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
7,539✔
2624
  int32_t      code = 0;
7,539✔
2625
  int32_t      lino = 0;
7,539✔
2626
  void*        buf = NULL;
7,539✔
2627
  size_t       size = 0;
7,539✔
2628
  SSTriggerWalNewRsp resultRsp = {0};
7,539✔
2629
  
2630
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
7,539!
2631
  void* pTask = sStreamReaderInfo->pTask;
7,539✔
2632
  ST_TASK_DLOG("vgId:%d %s start, request paras lastVer:%" PRId64, TD_VID(pVnode), __func__, req->walMetaDataNewReq.lastVer);
7,539✔
2633

2634
  if (sStreamReaderInfo->metaBlock == NULL) {
7,539✔
2635
    STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&sStreamReaderInfo->metaBlock));
174!
2636
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(sStreamReaderInfo->metaBlock, STREAM_RETURN_ROWS_NUM));
174!
2637
  }
2638
  resultRsp.metaBlock = sStreamReaderInfo->metaBlock;
7,539✔
2639
  resultRsp.dataBlock = sStreamReaderInfo->triggerBlock;
7,539✔
2640
  resultRsp.ver = req->walMetaDataNewReq.lastVer;
7,539✔
2641
  STREAM_CHECK_RET_GOTO(processWalVerMetaDataNew(pVnode, sStreamReaderInfo, &resultRsp));
7,539!
2642

2643
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
7,544✔
2644
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp, sStreamReaderInfo->indexHash);
282✔
2645
  buf = rpcMallocCont(size);
283✔
2646
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp, sStreamReaderInfo->indexHash);
283✔
2647
  printDataBlock(sStreamReaderInfo->metaBlock, __func__, "meta", ((SStreamTask*)pTask)->streamId);
283✔
2648
  printDataBlock(sStreamReaderInfo->triggerBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
283✔
2649
  printDataBlock(resultRsp.dropBlock, __func__, "drop", ((SStreamTask*)pTask)->streamId);
283✔
2650
  printDataBlock(resultRsp.deleteBlock, __func__, "delete", ((SStreamTask*)pTask)->streamId);
283✔
2651
  printIndexHash(sStreamReaderInfo->indexHash, pTask);
283✔
2652

2653
end:
7,538✔
2654
  if (resultRsp.totalRows == 0) {
7,538✔
2655
    buf = rpcMallocCont(sizeof(int64_t));
7,255✔
2656
    *(int64_t *)buf = resultRsp.ver;
7,260✔
2657
    size = sizeof(int64_t);
7,260✔
2658
    code = TSDB_CODE_STREAM_NO_DATA;
7,260✔
2659
  }
2660
  SRpcMsg rsp = {
7,543✔
2661
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2662
  tmsgSendRsp(&rsp);
7,543✔
2663
  if (code == TSDB_CODE_STREAM_NO_DATA){
7,545✔
2664
    code = 0;
7,259✔
2665
  }
2666
  blockDataDestroy(resultRsp.deleteBlock);
7,545✔
2667
  blockDataDestroy(resultRsp.dropBlock);
7,541✔
2668

2669
  STREAM_PRINT_LOG_END_WITHID(code, lino);
7,541!
2670

2671
  return code;
7,535✔
2672
}
2673

2674
static int32_t vnodeProcessStreamWalDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
4,585✔
2675
  int32_t      code = 0;
4,585✔
2676
  int32_t      lino = 0;
4,585✔
2677
  void*        buf = NULL;
4,585✔
2678
  size_t       size = 0;
4,585✔
2679
  SSTriggerWalNewRsp resultRsp = {0};
4,585✔
2680

2681
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
4,585!
2682
  void* pTask = sStreamReaderInfo->pTask;
4,585✔
2683
  ST_TASK_DLOG("vgId:%d %s start, request paras size:%zu", TD_VID(pVnode), __func__, taosArrayGetSize(req->walDataNewReq.versions));
4,585✔
2684

2685
  resultRsp.dataBlock = sStreamReaderInfo->triggerBlock;
4,585✔
2686
  STREAM_CHECK_RET_GOTO(processWalVerDataNew(pVnode, sStreamReaderInfo, req->walDataNewReq.versions, req->walDataNewReq.ranges, &resultRsp));
4,585!
2687
  ST_TASK_DLOG("vgId:%d %s get result last ver:%"PRId64" rows:%d", TD_VID(pVnode), __func__, resultRsp.ver, resultRsp.totalRows);
4,582✔
2688

2689
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
4,583✔
2690

2691
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp, sStreamReaderInfo->indexHash);
118✔
2692
  buf = rpcMallocCont(size);
118✔
2693
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp, sStreamReaderInfo->indexHash);
118✔
2694
  printDataBlock(sStreamReaderInfo->triggerBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
118✔
2695
  printIndexHash(sStreamReaderInfo->indexHash, pTask);
118✔
2696

2697
end:
4,580✔
2698
  if (resultRsp.totalRows == 0) {
4,580✔
2699
    buf = rpcMallocCont(sizeof(int64_t));
4,462✔
2700
    *(int64_t *)buf = resultRsp.ver;
4,468✔
2701
    size = sizeof(int64_t);
4,468✔
2702
    code = TSDB_CODE_STREAM_NO_DATA;
4,468✔
2703
  }
2704
  SRpcMsg rsp = {
4,586✔
2705
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2706
  tmsgSendRsp(&rsp);
4,586✔
2707
  if (code == TSDB_CODE_STREAM_NO_DATA){
4,582✔
2708
    code = 0;
4,465✔
2709
  }
2710

2711
  blockDataDestroy(resultRsp.deleteBlock);
4,582✔
2712
  blockDataDestroy(resultRsp.dropBlock);
4,582✔
2713
  STREAM_PRINT_LOG_END_WITHID(code, lino);
4,585!
2714

2715
  return code;
4,585✔
2716
}
2717

2718
static int32_t vnodeProcessStreamWalCalcDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
744✔
2719
  int32_t      code = 0;
744✔
2720
  int32_t      lino = 0;
744✔
2721
  void*        buf = NULL;
744✔
2722
  size_t       size = 0;
744✔
2723
  SSTriggerWalNewRsp resultRsp = {0};
744✔
2724
  SSDataBlock* pBlock1 = NULL;
744✔
2725
  SSDataBlock* pBlock2 = NULL;
744✔
2726
  
2727
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
744!
2728
  void* pTask = sStreamReaderInfo->pTask;
744✔
2729
  ST_TASK_DLOG("vgId:%d %s start, request paras size:%zu", TD_VID(pVnode), __func__, taosArrayGetSize(req->walDataNewReq.versions));
744✔
2730

2731
  resultRsp.dataBlock = sStreamReaderInfo->isVtableStream ? sStreamReaderInfo->calcBlock : sStreamReaderInfo->triggerBlock;
744✔
2732
  resultRsp.isCalc = sStreamReaderInfo->isVtableStream ? true : false;
744✔
2733
  STREAM_CHECK_RET_GOTO(processWalVerDataNew(pVnode, sStreamReaderInfo, req->walDataNewReq.versions, req->walDataNewReq.ranges, &resultRsp));
744!
2734
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
744✔
2735

2736
  if (!sStreamReaderInfo->isVtableStream){
592✔
2737
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerBlock, true, &pBlock1));
470!
2738
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->calcBlock, false, &pBlock2));
470!
2739
  
2740
    blockDataTransform(pBlock2, pBlock1);
470✔
2741
    resultRsp.dataBlock = pBlock2;
470✔
2742
  }
2743

2744
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp, sStreamReaderInfo->indexHash);
592✔
2745
  buf = rpcMallocCont(size);
592✔
2746
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp, sStreamReaderInfo->indexHash);
592✔
2747
  printDataBlock(resultRsp.dataBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
592✔
2748
  printIndexHash(sStreamReaderInfo->indexHash, pTask);
592✔
2749

2750
end:
743✔
2751
  if (resultRsp.totalRows == 0) {
743✔
2752
    buf = rpcMallocCont(sizeof(int64_t));
152✔
2753
    *(int64_t *)buf = resultRsp.ver;
152✔
2754
    size = sizeof(int64_t);
152✔
2755
    code = TSDB_CODE_STREAM_NO_DATA;
152✔
2756
  }
2757
  SRpcMsg rsp = {
743✔
2758
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2759
  tmsgSendRsp(&rsp);
743✔
2760
  if (code == TSDB_CODE_STREAM_NO_DATA){
744✔
2761
    code = 0;
152✔
2762
  }
2763

2764
  blockDataDestroy(pBlock1);
744✔
2765
  blockDataDestroy(pBlock2);
744✔
2766
  blockDataDestroy(resultRsp.deleteBlock);
744✔
2767
  blockDataDestroy(resultRsp.dropBlock);
744✔
2768
  STREAM_PRINT_LOG_END_WITHID(code, lino);
744!
2769

2770
  return code;
744✔
2771
}
2772

2773
static int32_t vnodeProcessStreamGroupColValueReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
670✔
2774
  int32_t code = 0;
670✔
2775
  int32_t lino = 0;
670✔
2776
  void*   buf = NULL;
670✔
2777
  size_t  size = 0;
670✔
2778

2779
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
670!
2780
  void* pTask = sStreamReaderInfo->pTask;
670✔
2781
  ST_TASK_DLOG("vgId:%d %s start, request gid:%" PRId64, TD_VID(pVnode), __func__, req->groupColValueReq.gid);
670✔
2782

2783
  SArray** gInfo = taosHashGet(sStreamReaderInfo->groupIdMap, &req->groupColValueReq.gid, POINTER_BYTES);
670✔
2784
  STREAM_CHECK_NULL_GOTO(gInfo, TSDB_CODE_STREAM_NO_CONTEXT);
670!
2785
  SStreamGroupInfo pGroupInfo = {0};
670✔
2786
  pGroupInfo.gInfo = *gInfo;
670✔
2787

2788
  size = tSerializeSStreamGroupInfo(NULL, 0, &pGroupInfo, TD_VID(pVnode));
670✔
2789
  STREAM_CHECK_CONDITION_GOTO(size < 0, size);
2790
  buf = rpcMallocCont(size);
670✔
2791
  STREAM_CHECK_NULL_GOTO(buf, terrno);
670!
2792
  size = tSerializeSStreamGroupInfo(buf, size, &pGroupInfo, TD_VID(pVnode));
670✔
2793
  STREAM_CHECK_CONDITION_GOTO(size < 0, size);
2794
end:
670✔
2795
  if (code != 0) {
670!
2796
    rpcFreeCont(buf);
×
2797
    buf = NULL;
×
2798
    size = 0;
×
2799
  }
2800
  STREAM_PRINT_LOG_END_WITHID(code, lino);
670!
2801
  SRpcMsg rsp = {
670✔
2802
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2803
  tmsgSendRsp(&rsp);
670✔
2804

2805
  return code;
670✔
2806
}
2807

2808
static int32_t vnodeProcessStreamVTableInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req, SStreamTriggerReaderInfo* sStreamReaderInfo) {
493✔
2809
  int32_t              code = 0;
493✔
2810
  int32_t              lino = 0;
493✔
2811
  void*                buf = NULL;
493✔
2812
  size_t               size = 0;
493✔
2813
  SStreamMsgVTableInfo vTableInfo = {0};
493✔
2814
  SMetaReader          metaReader = {0};
493✔
2815
  SStorageAPI api = {0};
493✔
2816
  initStorageAPI(&api);
493✔
2817

2818
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
494!
2819
  void* pTask = sStreamReaderInfo->pTask;
494✔
2820
  ST_TASK_DLOG("vgId:%d %s start", TD_VID(pVnode), __func__);
494✔
2821

2822
  SArray* cids = req->virTableInfoReq.cids;
494✔
2823
  STREAM_CHECK_NULL_GOTO(cids, terrno);
494!
2824

2825
  SArray* pTableListArray = qStreamGetTableArrayList(sStreamReaderInfo->tableList);
494✔
2826
  STREAM_CHECK_NULL_GOTO(pTableListArray, terrno);
494!
2827

2828
  vTableInfo.infos = taosArrayInit(taosArrayGetSize(pTableListArray), sizeof(VTableInfo));
494✔
2829
  STREAM_CHECK_NULL_GOTO(vTableInfo.infos, terrno);
494!
2830
  api.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &api.metaFn);
494✔
2831

2832
  for (size_t i = 0; i < taosArrayGetSize(pTableListArray); i++) {
1,240✔
2833
    STableKeyInfo* pKeyInfo = taosArrayGet(pTableListArray, i);
744✔
2834
    if (pKeyInfo == NULL) {
744!
2835
      continue;
×
2836
    }
2837
    VTableInfo* vTable = taosArrayReserve(vTableInfo.infos, 1);
744✔
2838
    STREAM_CHECK_NULL_GOTO(vTable, terrno);
744!
2839
    vTable->uid = pKeyInfo->uid;
744✔
2840
    vTable->gId = pKeyInfo->groupId;
744✔
2841

2842
    code = api.metaReaderFn.getTableEntryByUid(&metaReader, pKeyInfo->uid);
744✔
2843
    if (taosArrayGetSize(cids) == 1 && *(col_id_t*)taosArrayGet(cids, 0) == PRIMARYKEY_TIMESTAMP_COL_ID){
745!
2844
      vTable->cols.nCols = metaReader.me.colRef.nCols;
42✔
2845
      vTable->cols.version = metaReader.me.colRef.version;
42✔
2846
      vTable->cols.pColRef = taosMemoryCalloc(metaReader.me.colRef.nCols, sizeof(SColRef));
42!
2847
      for (size_t j = 0; j < metaReader.me.colRef.nCols; j++) {
252✔
2848
        memcpy(vTable->cols.pColRef + j, &metaReader.me.colRef.pColRef[j], sizeof(SColRef));
210✔
2849
      }
2850
    } else {
2851
      vTable->cols.nCols = taosArrayGetSize(cids);
703✔
2852
      vTable->cols.version = metaReader.me.colRef.version;
703✔
2853
      vTable->cols.pColRef = taosMemoryCalloc(taosArrayGetSize(cids), sizeof(SColRef));
703!
2854
      for (size_t i = 0; i < taosArrayGetSize(cids); i++) {
2,276✔
2855
        for (size_t j = 0; j < metaReader.me.colRef.nCols; j++) {
6,828✔
2856
          if (metaReader.me.colRef.pColRef[j].hasRef &&
6,125✔
2857
              metaReader.me.colRef.pColRef[j].id == *(col_id_t*)taosArrayGet(cids, i)) {
4,442✔
2858
            memcpy(vTable->cols.pColRef + i, &metaReader.me.colRef.pColRef[j], sizeof(SColRef));
870✔
2859
            break;
870✔
2860
          }
2861
        }
2862
      }
2863
    }
2864
    tDecoderClear(&metaReader.coder);
746✔
2865
  }
2866
  ST_TASK_DLOG("vgId:%d %s end", TD_VID(pVnode), __func__);
495✔
2867
  STREAM_CHECK_RET_GOTO(buildVTableInfoRsp(&vTableInfo, &buf, &size));
495!
2868

2869
end:
494✔
2870
  tDestroySStreamMsgVTableInfo(&vTableInfo);
494✔
2871
  api.metaReaderFn.clearReader(&metaReader);
494✔
2872
  STREAM_PRINT_LOG_END_WITHID(code, lino);
494!
2873
  SRpcMsg rsp = {
494✔
2874
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2875
  tmsgSendRsp(&rsp);
494✔
2876
  return code;
494✔
2877
}
2878

2879
static int32_t vnodeProcessStreamOTableInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req) {
106✔
2880
  int32_t                   code = 0;
106✔
2881
  int32_t                   lino = 0;
106✔
2882
  void*                     buf = NULL;
106✔
2883
  size_t                    size = 0;
106✔
2884
  SSTriggerOrigTableInfoRsp oTableInfo = {0};
106✔
2885
  SMetaReader               metaReader = {0};
106✔
2886
  int64_t streamId = req->base.streamId;
106✔
2887
  stsDebug("vgId:%d %s start", TD_VID(pVnode), __func__);
106✔
2888

2889
  SStorageAPI api = {0};
106✔
2890
  initStorageAPI(&api);
106✔
2891

2892
  SArray* cols = req->origTableInfoReq.cols;
106✔
2893
  STREAM_CHECK_NULL_GOTO(cols, terrno);
106!
2894

2895
  oTableInfo.cols = taosArrayInit(taosArrayGetSize(cols), sizeof(OTableInfoRsp));
106✔
2896

2897
  STREAM_CHECK_NULL_GOTO(oTableInfo.cols, terrno);
106!
2898

2899
  api.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &api.metaFn);
106✔
2900
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
280✔
2901
    OTableInfo*    oInfo = taosArrayGet(cols, i);
174✔
2902
    OTableInfoRsp* vTableInfo = taosArrayReserve(oTableInfo.cols, 1);
174✔
2903
    STREAM_CHECK_NULL_GOTO(oInfo, terrno);
174!
2904
    STREAM_CHECK_NULL_GOTO(vTableInfo, terrno);
174!
2905
    STREAM_CHECK_RET_GOTO(api.metaReaderFn.getTableEntryByName(&metaReader, oInfo->refTableName));
174!
2906
    vTableInfo->uid = metaReader.me.uid;
174✔
2907
    stsDebug("vgId:%d %s uid:%"PRId64, TD_VID(pVnode), __func__, vTableInfo->uid);
174✔
2908

2909
    SSchemaWrapper* sSchemaWrapper = NULL;
174✔
2910
    if (metaReader.me.type == TD_CHILD_TABLE) {
174✔
2911
      int64_t suid = metaReader.me.ctbEntry.suid;
169✔
2912
      vTableInfo->suid = suid;
169✔
2913
      tDecoderClear(&metaReader.coder);
169✔
2914
      STREAM_CHECK_RET_GOTO(api.metaReaderFn.getTableEntryByUid(&metaReader, suid));
169!
2915
      sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
169✔
2916
    } else if (metaReader.me.type == TD_NORMAL_TABLE) {
5!
2917
      vTableInfo->suid = 0;
5✔
2918
      sSchemaWrapper = &metaReader.me.ntbEntry.schemaRow;
5✔
2919
    } else {
2920
      stError("invalid table type:%d", metaReader.me.type);
×
2921
    }
2922

2923
    for (size_t j = 0; j < sSchemaWrapper->nCols; j++) {
770!
2924
      SSchema* s = sSchemaWrapper->pSchema + j;
770✔
2925
      if (strcmp(s->name, oInfo->refColName) == 0) {
770✔
2926
        vTableInfo->cid = s->colId;
174✔
2927
        break;
174✔
2928
      }
2929
    }
2930
    if (vTableInfo->cid == 0) {
174!
2931
      stError("vgId:%d %s, not found col %s in table %s", TD_VID(pVnode), __func__, oInfo->refColName,
×
2932
              oInfo->refTableName);
2933
    }
2934
    tDecoderClear(&metaReader.coder);
174✔
2935
  }
2936

2937
  STREAM_CHECK_RET_GOTO(buildOTableInfoRsp(&oTableInfo, &buf, &size));
105!
2938

2939
end:
106✔
2940
  tDestroySTriggerOrigTableInfoRsp(&oTableInfo);
106✔
2941
  api.metaReaderFn.clearReader(&metaReader);
106✔
2942
  STREAM_PRINT_LOG_END(code, lino);
106!
2943
  SRpcMsg rsp = {
106✔
2944
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
2945
  tmsgSendRsp(&rsp);
106✔
2946
  return code;
106✔
2947
}
2948

2949
static int32_t vnodeProcessStreamVTableTagInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req) {
342✔
2950
  int32_t                   code = 0;
342✔
2951
  int32_t                   lino = 0;
342✔
2952
  void*                     buf = NULL;
342✔
2953
  size_t                    size = 0;
342✔
2954
  SSDataBlock* pBlock = NULL;
342✔
2955

2956
  SMetaReader               metaReader = {0};
342✔
2957
  SMetaReader               metaReaderStable = {0};
342✔
2958
  int64_t streamId = req->base.streamId;
342✔
2959
  stsDebug("vgId:%d %s start", TD_VID(pVnode), __func__);
342✔
2960

2961
  SStorageAPI api = {0};
342✔
2962
  initStorageAPI(&api);
342✔
2963

2964
  SArray* cols = req->virTablePseudoColReq.cids;
342✔
2965
  STREAM_CHECK_NULL_GOTO(cols, terrno);
342!
2966

2967
  api.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &api.metaFn);
342✔
2968
  STREAM_CHECK_RET_GOTO(api.metaReaderFn.getTableEntryByUid(&metaReader, req->virTablePseudoColReq.uid));
342!
2969

2970
  STREAM_CHECK_CONDITION_GOTO(metaReader.me.type != TD_VIRTUAL_CHILD_TABLE && metaReader.me.type != TD_VIRTUAL_NORMAL_TABLE, TSDB_CODE_INVALID_PARA);
342!
2971

2972
  STREAM_CHECK_RET_GOTO(createDataBlock(&pBlock));
342!
2973
  if (metaReader.me.type == TD_VIRTUAL_NORMAL_TABLE) {
342✔
2974
    STREAM_CHECK_CONDITION_GOTO (taosArrayGetSize(cols) < 1 || *(col_id_t*)taosArrayGet(cols, 0) != -1, TSDB_CODE_INVALID_PARA);
8!
2975
    SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN, -1);
8✔
2976
    STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
8!
2977
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, 1));
8!
2978
    pBlock->info.rows = 1;
8✔
2979
    SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, 0);
8✔
2980
    STREAM_CHECK_NULL_GOTO(pDst, terrno);
8!
2981
    STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, metaReader.me.name, strlen(metaReader.me.name), false));
8!
2982
  } else if (metaReader.me.type == TD_VIRTUAL_CHILD_TABLE){
334!
2983
    int64_t suid = metaReader.me.ctbEntry.suid;
334✔
2984
    api.metaReaderFn.readerReleaseLock(&metaReader);
334✔
2985
    api.metaReaderFn.initReader(&metaReaderStable, pVnode, META_READER_LOCK, &api.metaFn);
334✔
2986

2987
    STREAM_CHECK_RET_GOTO(api.metaReaderFn.getTableEntryByUid(&metaReaderStable, suid));
334!
2988
    SSchemaWrapper*  sSchemaWrapper = &metaReaderStable.me.stbEntry.schemaTag;
334✔
2989
    for (size_t i = 0; i < taosArrayGetSize(cols); i++){
921✔
2990
      col_id_t* id = taosArrayGet(cols, i);
587✔
2991
      STREAM_CHECK_NULL_GOTO(id, terrno);
587!
2992
      if (*id == -1) {
587✔
2993
        SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN, -1);
334✔
2994
        STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
334!
2995
        continue;
334✔
2996
      }
2997
      size_t j = 0;
253✔
2998
      for (; j < sSchemaWrapper->nCols; j++) {
355!
2999
        SSchema* s = sSchemaWrapper->pSchema + j;
355✔
3000
        if (s->colId == *id) {
355✔
3001
          SColumnInfoData idata = createColumnInfoData(s->type, s->bytes, s->colId);
253✔
3002
          STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
253!
3003
          break;
253✔
3004
        }
3005
      }
3006
      if (j == sSchemaWrapper->nCols) {
253!
3007
        SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_NULL, CHAR_BYTES, *id);
×
3008
        STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
×
3009
      }
3010
    }
3011
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, 1));
334!
3012
    pBlock->info.rows = 1;
334✔
3013
    
3014
    for (size_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++){
921✔
3015
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
587✔
3016
      STREAM_CHECK_NULL_GOTO(pDst, terrno);
587!
3017

3018
      if (pDst->info.colId == -1) {
587✔
3019
        STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, metaReader.me.name, strlen(metaReader.me.name), false));
334!
3020
        continue;
334✔
3021
      }
3022
      if (pDst->info.type == TSDB_DATA_TYPE_NULL) {
253!
3023
        STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, NULL, true));
×
3024
        continue;
×
3025
      }
3026

3027
      STagVal val = {0};
253✔
3028
      val.cid = pDst->info.colId;
253✔
3029
      const char* p = api.metaFn.extractTagVal(metaReader.me.ctbEntry.pTags, pDst->info.type, &val);
253✔
3030

3031
      char* data = NULL;
253✔
3032
      if (pDst->info.type != TSDB_DATA_TYPE_JSON && p != NULL) {
253!
3033
        data = tTagValToData((const STagVal*)p, false);
253✔
3034
      } else {
3035
        data = (char*)p;
×
3036
      }
3037

3038
      STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, data,
253!
3039
                            (data == NULL) || (pDst->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data))));
3040

3041
      if ((pDst->info.type != TSDB_DATA_TYPE_JSON) && (p != NULL) && IS_VAR_DATA_TYPE(((const STagVal*)p)->type) &&
253!
3042
          (data != NULL)) {
3043
        taosMemoryFree(data);
68!
3044
      }
3045
    }
3046
  } else {
3047
    stError("vgId:%d %s, invalid table type:%d", TD_VID(pVnode), __func__, metaReader.me.type);
×
3048
    code = TSDB_CODE_INVALID_PARA;
×
3049
    goto end;
×
3050
  }
3051
  
3052
  stsDebug("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlock->info.rows);
342✔
3053
  printDataBlock(pBlock, __func__, "", streamId);
342✔
3054
  STREAM_CHECK_RET_GOTO(buildRsp(pBlock, &buf, &size));
342!
3055

3056
end:
342✔
3057
  if(size == 0){
342!
3058
    code = TSDB_CODE_STREAM_NO_DATA;
×
3059
  }
3060
  api.metaReaderFn.clearReader(&metaReaderStable);
342✔
3061
  api.metaReaderFn.clearReader(&metaReader);
342✔
3062
  STREAM_PRINT_LOG_END(code, lino);
342!
3063
  SRpcMsg rsp = {
342✔
3064
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
3065
  tmsgSendRsp(&rsp);
342✔
3066
  blockDataDestroy(pBlock);
342✔
3067
  return code;
342✔
3068
}
3069

3070
static int32_t vnodeProcessStreamFetchMsg(SVnode* pVnode, SRpcMsg* pMsg) {
12,145✔
3071
  int32_t            code = 0;
12,145✔
3072
  int32_t            lino = 0;
12,145✔
3073
  void*              buf = NULL;
12,145✔
3074
  size_t             size = 0;
12,145✔
3075
  void*              taskAddr = NULL;
12,145✔
3076
  SArray*            pResList = NULL;
12,145✔
3077

3078
  SResFetchReq req = {0};
12,145✔
3079
  STREAM_CHECK_CONDITION_GOTO(tDeserializeSResFetchReq(pMsg->pCont, pMsg->contLen, &req) < 0,
12,145!
3080
                              TSDB_CODE_QRY_INVALID_INPUT);
3081
  SArray* calcInfoList = (SArray*)qStreamGetReaderInfo(req.queryId, req.taskId, &taskAddr);
12,144✔
3082
  STREAM_CHECK_NULL_GOTO(calcInfoList, terrno);
12,145!
3083

3084
  STREAM_CHECK_CONDITION_GOTO(req.execId < 0, TSDB_CODE_INVALID_PARA);
12,145!
3085
  SStreamTriggerReaderCalcInfo* sStreamReaderCalcInfo = taosArrayGetP(calcInfoList, req.execId);
12,145✔
3086
  STREAM_CHECK_NULL_GOTO(sStreamReaderCalcInfo, terrno);
12,145!
3087
  void* pTask = sStreamReaderCalcInfo->pTask;
12,145✔
3088
  ST_TASK_DLOG("vgId:%d %s start, execId:%d, reset:%d, pTaskInfo:%p, scan type:%d", TD_VID(pVnode), __func__, req.execId, req.reset,
12,145✔
3089
               sStreamReaderCalcInfo->pTaskInfo, nodeType(sStreamReaderCalcInfo->calcAst->pNode));
3090

3091
  if (req.reset || sStreamReaderCalcInfo->pTaskInfo == NULL) {
12,145!
3092
  // if (req.reset) {
3093
    qDestroyTask(sStreamReaderCalcInfo->pTaskInfo);
12,038✔
3094
    int64_t uid = 0;
12,038✔
3095
    if (req.dynTbname) {
12,038✔
3096
      SArray* vals = req.pStRtFuncInfo->pStreamPartColVals;
175✔
3097
      for (int32_t i = 0; i < taosArrayGetSize(vals); ++i) {
175!
3098
        SStreamGroupValue* pValue = taosArrayGet(vals, i);
175✔
3099
        if (pValue != NULL && pValue->isTbname) {
175!
3100
          uid = pValue->uid;
175✔
3101
          break;
175✔
3102
        }
3103
      }
3104
    }
3105
    
3106
    SReadHandle handle = {0};
12,038✔
3107
    handle.vnode = pVnode;
12,038✔
3108
    handle.uid = uid;
12,038✔
3109

3110
    initStorageAPI(&handle.api);
12,038✔
3111
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(sStreamReaderCalcInfo->calcAst->pNode) ||
12,037✔
3112
      QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(sStreamReaderCalcInfo->calcAst->pNode)){
11,400✔
3113
      STimeRangeNode* node = (STimeRangeNode*)((STableScanPhysiNode*)(sStreamReaderCalcInfo->calcAst->pNode))->pTimeRange;
11,573✔
3114
      if (node != NULL) {
11,573✔
3115
        STREAM_CHECK_RET_GOTO(processCalaTimeRange(sStreamReaderCalcInfo, &req, node, &handle));
414!
3116
      } else {
3117
        ST_TASK_DLOG("vgId:%d %s no time range node", TD_VID(pVnode), __func__);
11,168✔
3118
      }
3119
    }
3120

3121
    TSWAP(sStreamReaderCalcInfo->rtInfo.funcInfo, *req.pStRtFuncInfo);
12,037✔
3122
    handle.streamRtInfo = &sStreamReaderCalcInfo->rtInfo;
12,037✔
3123

3124
    // if (sStreamReaderCalcInfo->pTaskInfo == NULL) {
3125
    STREAM_CHECK_RET_GOTO(qCreateStreamExecTaskInfo(&sStreamReaderCalcInfo->pTaskInfo,
12,037✔
3126
                                                    sStreamReaderCalcInfo->calcScanPlan, &handle, NULL, TD_VID(pVnode),
3127
                                                    req.taskId));
3128
    // } else {
3129
    // STREAM_CHECK_RET_GOTO(qResetTableScan(sStreamReaderCalcInfo->pTaskInfo, handle.winRange));
3130
    // }
3131

3132
    STREAM_CHECK_RET_GOTO(qSetTaskId(sStreamReaderCalcInfo->pTaskInfo, req.taskId, req.queryId));
12,029!
3133
  }
3134

3135
  if (req.pOpParam != NULL) {
12,136✔
3136
    qUpdateOperatorParam(sStreamReaderCalcInfo->pTaskInfo, req.pOpParam);
200✔
3137
  }
3138
  
3139
  pResList = taosArrayInit(4, POINTER_BYTES);
12,136✔
3140
  STREAM_CHECK_NULL_GOTO(pResList, terrno);
12,136!
3141
  uint64_t ts = 0;
12,136✔
3142
  bool     hasNext = false;
12,136✔
3143
  STREAM_CHECK_RET_GOTO(qExecTaskOpt(sStreamReaderCalcInfo->pTaskInfo, pResList, &ts, &hasNext, NULL, req.pOpParam != NULL));
12,136✔
3144

3145
  for(size_t i = 0; i < taosArrayGetSize(pResList); i++){
28,953✔
3146
    SSDataBlock* pBlock = taosArrayGetP(pResList, i);
16,832✔
3147
    if (pBlock == NULL) continue;
16,832!
3148
    printDataBlock(pBlock, __func__, "fetch", ((SStreamTask*)pTask)->streamId);
16,832✔
3149
/*    
3150
    if (sStreamReaderCalcInfo->rtInfo.funcInfo.withExternalWindow) {
3151
      STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderCalcInfo->pFilterInfo, NULL));
3152
      printDataBlock(pBlock, __func__, "fetch filter");
3153
    }
3154
*/    
3155
  }
3156

3157
  ST_TASK_DLOG("vgId:%d %s start to build rsp", TD_VID(pVnode), __func__);
12,121✔
3158
  STREAM_CHECK_RET_GOTO(streamBuildFetchRsp(pResList, hasNext, &buf, &size, pVnode->config.tsdbCfg.precision));
12,121!
3159
  ST_TASK_DLOG("vgId:%d %s end:", TD_VID(pVnode), __func__);
12,120✔
3160

3161
end:
215✔
3162
  taosArrayDestroy(pResList);
12,144✔
3163
  streamReleaseTask(taskAddr);
12,145✔
3164

3165
  STREAM_PRINT_LOG_END(code, lino);
12,145!
3166
  SRpcMsg rsp = {.msgType = TDMT_STREAM_FETCH_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
12,145✔
3167
  tmsgSendRsp(&rsp);
12,145✔
3168
  tDestroySResFetchReq(&req);
12,145✔
3169
  return code;
12,145✔
3170
}
3171

3172
int32_t vnodeProcessStreamReaderMsg(SVnode* pVnode, SRpcMsg* pMsg) {
67,537✔
3173
  int32_t                   code = 0;
67,537✔
3174
  int32_t                   lino = 0;
67,537✔
3175
  SSTriggerPullRequestUnion req = {0};
67,537✔
3176
  void*                     taskAddr = NULL;
67,537✔
3177

3178
  vDebug("vgId:%d, msg:%p in stream reader queue is processing", pVnode->config.vgId, pMsg);
67,537✔
3179
  if (!syncIsReadyForRead(pVnode->sync)) {
67,537✔
3180
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
81✔
3181
    return 0;
88✔
3182
  }
3183

3184
  if (pMsg->msgType == TDMT_STREAM_FETCH) {
67,454✔
3185
    return vnodeProcessStreamFetchMsg(pVnode, pMsg);
12,145✔
3186
  } else if (pMsg->msgType == TDMT_STREAM_TRIGGER_PULL) {
55,309!
3187
    void*   pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
55,310✔
3188
    int32_t len = pMsg->contLen - sizeof(SMsgHead);
55,310✔
3189
    STREAM_CHECK_RET_GOTO(tDeserializeSTriggerPullRequest(pReq, len, &req));
55,310!
3190
    stDebug("vgId:%d %s start, type:%d, streamId:%" PRIx64 ", readerTaskId:%" PRIx64 ", sessionId:%" PRIx64,
55,277✔
3191
            TD_VID(pVnode), __func__, req.base.type, req.base.streamId, req.base.readerTaskId, req.base.sessionId);
3192
    SStreamTriggerReaderInfo* sStreamReaderInfo = (STRIGGER_PULL_OTABLE_INFO == req.base.type) ? NULL : qStreamGetReaderInfo(req.base.streamId, req.base.readerTaskId, &taskAddr);
55,274✔
3193
    if (sStreamReaderInfo != NULL) {  
55,283✔
3194
      (void)taosThreadMutexLock(&sStreamReaderInfo->mutex);
55,150✔
3195
      if (sStreamReaderInfo->tableList == NULL) {
55,146✔
3196
        STREAM_CHECK_RET_GOTO(generateTablistForStreamReader(pVnode, sStreamReaderInfo, false));  
460!
3197
        STREAM_CHECK_RET_GOTO(generateTablistForStreamReader(pVnode, sStreamReaderInfo, true));
460!
3198
        STREAM_CHECK_RET_GOTO(filterInitFromNode(sStreamReaderInfo->pConditions, &sStreamReaderInfo->pFilterInfo, 0, NULL));
460!
3199
      }
3200
      (void)taosThreadMutexUnlock(&sStreamReaderInfo->mutex);
55,146✔
3201
      sStreamReaderInfo->pVnode = pVnode;
55,152✔
3202
    }
3203
    switch (req.base.type) {
55,285!
3204
      case STRIGGER_PULL_SET_TABLE:
106✔
3205
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamSetTableReq(pVnode, pMsg, &req, sStreamReaderInfo));
106!
3206
        break;
106✔
3207
      case STRIGGER_PULL_LAST_TS:
443✔
3208
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamLastTsReq(pVnode, pMsg, &req, sStreamReaderInfo));
443!
3209
        break;
443✔
3210
      case STRIGGER_PULL_FIRST_TS:
391✔
3211
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamFirstTsReq(pVnode, pMsg, &req, sStreamReaderInfo));
391✔
3212
        break;
381✔
3213
      case STRIGGER_PULL_TSDB_META:
752✔
3214
      case STRIGGER_PULL_TSDB_META_NEXT:
3215
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbMetaReq(pVnode, pMsg, &req, sStreamReaderInfo));
752✔
3216
        break;
743✔
3217
      case STRIGGER_PULL_TSDB_TS_DATA:
35✔
3218
        if (sStreamReaderInfo->isVtableStream) {
35!
3219
          STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTsDataReqVTable(pVnode, pMsg, &req, sStreamReaderInfo));
×
3220
        } else {
3221
          STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTsDataReqNonVTable(pVnode, pMsg, &req, sStreamReaderInfo));
35!
3222
        }
3223
        break;
35✔
3224
      case STRIGGER_PULL_TSDB_TRIGGER_DATA:
293✔
3225
      case STRIGGER_PULL_TSDB_TRIGGER_DATA_NEXT:
3226
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTriggerDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
293!
3227
        break;
293✔
3228
      case STRIGGER_PULL_TSDB_CALC_DATA:
28,323✔
3229
      case STRIGGER_PULL_TSDB_CALC_DATA_NEXT:
3230
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbCalcDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
28,323✔
3231
        break;
28,197✔
3232
      case STRIGGER_PULL_TSDB_DATA:
180✔
3233
      case STRIGGER_PULL_TSDB_DATA_NEXT:
3234
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbVirtalDataReq(pVnode, pMsg, &req, sStreamReaderInfo));
180!
3235
        break;
180✔
3236
      case STRIGGER_PULL_GROUP_COL_VALUE:
670✔
3237
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamGroupColValueReq(pVnode, pMsg, &req, sStreamReaderInfo));
670!
3238
        break;
670✔
3239
      case STRIGGER_PULL_VTABLE_INFO:
494✔
3240
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableInfoReq(pVnode, pMsg, &req, sStreamReaderInfo));
494!
3241
        break;
494✔
3242
      case STRIGGER_PULL_VTABLE_PSEUDO_COL:
342✔
3243
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableTagInfoReq(pVnode, pMsg, &req));
342!
3244
        break;
342✔
3245
      case STRIGGER_PULL_OTABLE_INFO:
106✔
3246
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamOTableInfoReq(pVnode, pMsg, &req));
106!
3247
        break;
106✔
3248
      case STRIGGER_PULL_WAL_META_NEW:
10,276✔
3249
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
10,276!
3250
        break;
10,270✔
3251
      case STRIGGER_PULL_WAL_DATA_NEW:
4,587✔
3252
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
4,587!
3253
        break;
4,581✔
3254
      case STRIGGER_PULL_WAL_META_DATA_NEW:
7,543✔
3255
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
7,543!
3256
        break;
7,533✔
3257
      case STRIGGER_PULL_WAL_CALC_DATA_NEW:
744✔
3258
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalCalcDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo));
744!
3259
        break;
744✔
3260
      default:
×
3261
        vError("unknown inner msg type:%d in stream reader queue", req.base.type);
×
3262
        STREAM_CHECK_RET_GOTO(TSDB_CODE_APP_ERROR);
×
3263
        break;
×
3264
    }
3265
  } else {
3266
    vError("unknown msg type:%d in stream reader queue", pMsg->msgType);
×
3267
    STREAM_CHECK_RET_GOTO(TSDB_CODE_APP_ERROR);
×
3268
  }
3269
end:
×
3270

3271
  streamReleaseTask(taskAddr);
55,261✔
3272

3273
  tDestroySTriggerPullRequest(&req);
55,295✔
3274
  STREAM_PRINT_LOG_END(code, lino);
55,281!
3275
  return code;
55,265✔
3276
}
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