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

taosdata / TDengine / #5127

16 Sep 2026 01:31AM UTC coverage: 73.395% (-0.3%) from 73.686%
#5127

push

travis-ci

jbjia
test(coverage): sync from gitlab

310906 of 423608 relevant lines covered (73.39%)

71046059.39 hits per line

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

81.04
/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 <taos.h>
19
#include <tdef.h>
20
#include "executor.h"
21
#include "nodes.h"
22
#include "osMemPool.h"
23
#include "osMemory.h"
24
#include "osSemaphore.h"
25
#include "query.h"
26
#include "scalar.h"
27
#include "stream.h"
28
#include "streamReader.h"
29
#include "taosdef.h"
30
#include "taoserror.h"
31
#include "tarray.h"
32
#include "tcommon.h"
33
#include "tdatablock.h"
34
#include "tdb.h"
35
#include "tdef.h"
36
#include "tencode.h"
37
#include "tglobal.h"
38
#include "thash.h"
39
#include "tlist.h"
40
#include "tlockfree.h"
41
#include "tmsg.h"
42
#include "tsimplehash.h"
43
#include "ttypes.h"
44
#include "vnd.h"
45
#include "vnode.h"
46
#include "vnodeInt.h"
47
#include "vnodeStreamVTable.h"
48
#include "executor.h"
49

50
int32_t cacheTag(SVnode* pVnode, SHashObj* metaCache, SExprInfo* pExprInfo, int32_t numOfExpr, SStorageAPI* api, uint64_t uid, col_id_t colId, SRWLatch* lock);
51

52
extern int64_t tsMaxKeyByPrecision[];
53

54
static SColumnNode* getRollupTagCol(const SStreamTriggerReaderInfo* pInfo) {
14,083 ✔
55
  SNode* pNode = nodesListGetNode(pInfo->pRollupTagCols, 0);
14,083 ✔
56
  return (pNode != NULL && nodeType(pNode) == QUERY_NODE_COLUMN) ? (SColumnNode*)pNode : NULL;
14,083 ✔
57
}
58

59
typedef struct {
60
  int32_t off;
61
  int32_t len;
62
} SRollupSegmentRef;
63

64
typedef struct {
65
  const char* input;
66
  SArray*     segments;
67
} SRollupPath;
68

69
static int32_t streamRollupErrno(void) { return terrno == 0 ? TSDB_CODE_OUT_OF_MEMORY : terrno; }
×
70

71
static int32_t streamRollupSkipped(bool* pSkipped) {
×
72
  if (pSkipped != NULL) {
×
73
    *pSkipped = true;
×
74
  }
75
  return TSDB_CODE_SUCCESS;
×
76
}
77

78
static bool isIllegalSegmentByte(char c) {
8,363 ✔
79
  unsigned char uc = (unsigned char)c;
8,363 ✔
80
  return uc < 0x20;
8,363 ✔
81
}
82

83
static bool isSegmentEdgeBlank(char c) { return c == ' ' || c == '\t'; }
16,726 ✔
84

85
static void destroyRollupPath(SRollupPath* p) {
3,575 ✔
86
  if (p == NULL) {
3,575 ✔
87
    return;
×
88
  }
89

90
  taosArrayDestroy(p->segments);
3,575 ✔
91
  p->segments = NULL;
3,575 ✔
92
  p->input = NULL;
3,575 ✔
93
}
94

95
static int32_t parseRollupPath(const char* tagValue, int32_t tagLen, SRollupPath* pOut, bool* pSkipped) {
3,259 ✔
96
  if (pOut == NULL) {
3,259 ✔
97
    return TSDB_CODE_INVALID_PARA;
×
98
  }
99

100
  pOut->input = NULL;
3,259 ✔
101
  pOut->segments = NULL;
3,259 ✔
102
  if (pSkipped != NULL) {
3,259 ✔
103
    *pSkipped = false;
3,259 ✔
104
  }
105

106
  if (tagValue == NULL || tagLen == 0) {
3,259 ✔
107
    return streamRollupSkipped(pSkipped);
×
108
  }
109

110
  if (tagLen < 0 || tagValue[0] == '.' || tagValue[tagLen - 1] == '.') {
3,259 ✔
111
    return TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH;
×
112
  }
113

114
  pOut->segments = taosArrayInit(4, sizeof(SRollupSegmentRef));
3,259 ✔
115
  if (pOut->segments == NULL) {
3,259 ✔
116
    return streamRollupErrno();
×
117
  }
118

119
  int32_t segStart = 0;
3,259 ✔
120
  for (int32_t i = 0; i <= tagLen; ++i) {
19,985 ✔
121
    if (i < tagLen && tagValue[i] != '.') {
17,042 ✔
122
      if (isIllegalSegmentByte(tagValue[i])) {
8,363 ✔
123
        destroyRollupPath(pOut);
×
124
        return TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH;
×
125
      }
126
      continue;
8,363 ✔
127
    }
128

129
    int32_t segLen = i - segStart;
8,679 ✔
130
    if (segLen <= 0 || isSegmentEdgeBlank(tagValue[segStart]) || isSegmentEdgeBlank(tagValue[i - 1])) {
8,679 ✔
131
      destroyRollupPath(pOut);
316 ✔
132
      return TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH;
316 ✔
133
    }
134

135
    SRollupSegmentRef ref = {.off = segStart, .len = segLen};
8,363 ✔
136
    if (taosArrayPush(pOut->segments, &ref) == NULL) {
16,726 ✔
137
      int32_t code = streamRollupErrno();
×
138
      destroyRollupPath(pOut);
×
139
      return code;
×
140
    }
141

142
    segStart = i + 1;
8,363 ✔
143
  }
144

145
  pOut->input = tagValue;
2,943 ✔
146
  return TSDB_CODE_SUCCESS;
2,943 ✔
147
}
148

149
static int32_t buildRollupPrefixes(const SRollupPath* p, SArray** ppPrefixes) {
2,943 ✔
150
  if (ppPrefixes == NULL) {
2,943 ✔
151
    return TSDB_CODE_INVALID_PARA;
×
152
  }
153

154
  *ppPrefixes = NULL;
2,943 ✔
155

156
  if (p == NULL || p->input == NULL || p->segments == NULL) {
2,943 ✔
157
    return TSDB_CODE_INVALID_PARA;
×
158
  }
159

160
  int32_t numOfSegments = (int32_t)taosArrayGetSize(p->segments);
2,943 ✔
161
  SArray* prefixes = taosArrayInit(numOfSegments, sizeof(char*));
2,943 ✔
162
  if (prefixes == NULL) {
2,943 ✔
163
    return streamRollupErrno();
×
164
  }
165

166
  int32_t totalLen = 0;
2,943 ✔
167
  for (int32_t i = 0; i < numOfSegments; ++i) {
10,990 ✔
168
    SRollupSegmentRef* ref = taosArrayGet(p->segments, i);
8,047 ✔
169
    totalLen += ref->len + (i == 0 ? 0 : 1);
8,047 ✔
170

171
    char* prefix = taosMemoryMalloc(totalLen + 1);
8,047 ✔
172
    if (prefix == NULL) {
8,047 ✔
173
      taosArrayDestroyP(prefixes, NULL);
×
174
      return streamRollupErrno();
×
175
    }
176

177
    int32_t pos = 0;
8,047 ✔
178
    for (int32_t j = 0; j <= i; ++j) {
23,683 ✔
179
      SRollupSegmentRef* part = taosArrayGet(p->segments, j);
15,636 ✔
180
      if (j > 0) {
15,636 ✔
181
        prefix[pos++] = '.';
7,589 ✔
182
      }
183
      memcpy(prefix + pos, p->input + part->off, part->len);
15,636 ✔
184
      pos += part->len;
15,636 ✔
185
    }
186
    prefix[pos] = '\0';
8,047 ✔
187

188
    if (taosArrayPush(prefixes, &prefix) == NULL) {
8,047 ✔
189
      int32_t code = streamRollupErrno();
×
190
      taosMemoryFree(prefix);
×
191
      taosArrayDestroyP(prefixes, NULL);
×
192
      return code;
×
193
    }
194
  }
195

196
  *ppPrefixes = prefixes;
2,943 ✔
197
  return TSDB_CODE_SUCCESS;
2,943 ✔
198
}
199

200
static uint64_t computeRollupGid(uint64_t suid, const char* path) {
8,047 ✔
201
  if (path == NULL) {
8,047 ✔
202
    return 0;
×
203
  }
204

205
  int32_t pathLen = (int32_t)strlen(path);
8,047 ✔
206
  int32_t keyLen = (int32_t)sizeof(suid) + pathLen;
8,047 ✔
207
  char    stackBuf[256] = {0};
8,047 ✔
208
  char*   keyBuf = stackBuf;
8,047 ✔
209

210
  if (keyLen > (int32_t)sizeof(stackBuf)) {
8,047 ✔
211
    keyBuf = taosMemoryMalloc(keyLen);
×
212
    if (keyBuf == NULL) {
×
213
      return 0;
×
214
    }
215
  }
216

217
  memcpy(keyBuf, &suid, sizeof(suid));
8,047 ✔
218
  memcpy(keyBuf + sizeof(suid), path, pathLen);
8,047 ✔
219

220
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
8,047 ✔
221
  uint64_t   gid = hashFn(keyBuf, keyLen);
8,047 ✔
222
  if (gid == 0) {
8,047 ✔
223
    gid = 1;
×
224
  }
225

226
  if (keyBuf != stackBuf) {
8,047 ✔
227
    taosMemoryFree(keyBuf);
×
228
  }
229
  return gid;
8,047 ✔
230
}
231

232
#define BUILD_OPTION(options, _suid, _ver, _order, startTime, endTime, _schemas, _isSchema, _pSlotList)      \
233
  SStreamOptions                       options = {.suid = _suid,                                                   \
234
                                                  .ver = _ver,                                                     \
235
                                                  .order = _order,                                                 \
236
                                                  .twindows = {.skey = startTime, .ekey = endTime},                \
237
                                                  .schemas = _schemas,                                             \
238
                                                  .isSchema = _isSchema,                                           \
239
                                                  .pSlotList = _pSlotList};
240

241
typedef struct WalMetaResult {
242
  uint64_t    id;
243
  int64_t     skey;
244
  int64_t     ekey;
245
} WalMetaResult;
246

247
typedef struct SWalDataTarget {
248
  uint64_t    gid;
249
  STimeWindow window;
250
} SWalDataTarget;
251

252
typedef struct SWalDataSliceKey {
253
  int64_t  uid;
254
  uint64_t gid;
255
} SWalDataSliceKey;
256

257
static void setRollupIllegalPathInfo(SStreamTriggerReaderInfo* sStreamReaderInfo, uint64_t uid, const char* path,
316 ✔
258
                                     int32_t pathLen) {
259
  taosMemoryFreeClear(sStreamReaderInfo->extraErrMsg);
316 ✔
260

261
  char* sanitized = taosMemoryMalloc(pathLen + 1);
316 ✔
262
  if (sanitized == NULL) {
316 ✔
263
    return;
×
264
  }
265

266
  for (int32_t i = 0; i < pathLen; ++i) {
1,580 ✔
267
    unsigned char c = (unsigned char)path[i];
1,264 ✔
268
    sanitized[i] = (c < 0x20) ? '?' : path[i];
1,264 ✔
269
  }
270
  sanitized[pathLen] = '\0';
316 ✔
271

272
  int32_t len = snprintf(NULL, 0, "invalid tag value: uid=%" PRIu64 " tag='%s'", uid, sanitized);
316 ✔
273
  if (len < 0) {
316 ✔
274
    taosMemoryFree(sanitized);
×
275
    return;
×
276
  }
277

278
  char* msg = taosMemoryMalloc(len + 1);
316 ✔
279
  if (msg == NULL) {
316 ✔
280
    taosMemoryFree(sanitized);
×
281
    return;
×
282
  }
283
  (void)snprintf(msg, len + 1, "invalid tag value: uid=%" PRIu64 " tag='%s'", uid, sanitized);
316 ✔
284
  taosMemoryFree(sanitized);
316 ✔
285

286
  sStreamReaderInfo->extraErrMsg = msg;
316 ✔
287
}
288

289
static void buildExtraErrMsgRsp(SStreamTriggerReaderInfo* sStreamReaderInfo, void** pBuf, size_t* pSize) {
316 ✔
290
  if (*pBuf != NULL || sStreamReaderInfo == NULL || sStreamReaderInfo->extraErrMsg == NULL) {
316 ✔
291
    return;
×
292
  }
293

294
  int32_t len = strlen(sStreamReaderInfo->extraErrMsg) + 1;
316 ✔
295
  void*   buf = rpcMallocCont(len);
316 ✔
296
  if (buf == NULL) {
316 ✔
297
    return;
×
298
  }
299

300
  memcpy(buf, sStreamReaderInfo->extraErrMsg, len);
316 ✔
301
  *pBuf = buf;
316 ✔
302
  *pSize = len;
316 ✔
303
}
304

305
static void sendTriggerPullRsp(SRpcMsg* pMsg, SStreamTriggerReaderInfo* sStreamReaderInfo, void* buf, size_t size,
5,094,285 ✔
306
                               int32_t code, SStreamReaderResponseStats* pRspStats) {
307
  if (code == TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH) {
5,094,285 ✔
308
    buildExtraErrMsgRsp(sStreamReaderInfo, &buf, &size);
316 ✔
309
  }
310

311
  SRpcMsg rsp = {
5,094,285 ✔
312
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
313
  if (sStreamReaderInfo != NULL && sStreamReaderInfo->pTask != NULL && pRspStats != NULL) {
5,094,614 ✔
314
    pRspStats->activeScanContexts = taosHashGetSize(sStreamReaderInfo->streamTaskMap);
5,092,933 ✔
315
    pRspStats->tableCount = qStreamGetTableListNum(sStreamReaderInfo);
5,092,315 ✔
316
    pRspStats->activeScanContextsValid = true;
5,093,040 ✔
317
    pRspStats->tableCountValid = true;
5,093,040 ✔
318
    int64_t nowMonoUs = streamTaskGetMonotonicUs();
5,093,040 ✔
319
    stReaderTaskRecordPullResult((SStreamReaderTask*)sStreamReaderInfo->pTask, pRspStats, code, nowMonoUs,
5,092,254 ✔
320
                                 taosGetTimestampMs());
321
  }
322
  tmsgSendRsp(&rsp);
5,094,219 ✔
323
}
5,095,452 ✔
324

325
static void addReaderResponseBlock(SStreamReaderResponseStats* pRspStats, const SSDataBlock* pBlock) {
3,964,069 ✔
326
  if (pRspStats == NULL || pBlock == NULL || pBlock->info.rows <= 0) return;
3,964,069 ✔
327

328
  pRspStats->dataRows += (uint64_t)pBlock->info.rows;
2,170,951 ✔
329
  ++pRspStats->dataBlocks;
2,170,951 ✔
330
}
331

332
static void addReaderResponseBlocks(SStreamReaderResponseStats* pRspStats, const SArray* pBlocks) {
1,139,257 ✔
333
  if (pRspStats == NULL || pBlocks == NULL) return;
1,139,257 ✔
334

335
  for (int32_t i = 0; i < taosArrayGetSize(pBlocks); ++i) {
3,061,246 ✔
336
    addReaderResponseBlock(pRspStats, taosArrayGetP(pBlocks, i));
1,922,253 ✔
337
  }
338
}
339

340
static int8_t getWalDataSliceHashType(SStreamTriggerReaderInfo* sStreamReaderInfo) {
1,657,457 ✔
341
  return isRollupMultiReader(sStreamReaderInfo) ? TSDB_DATA_TYPE_BINARY : TSDB_DATA_TYPE_BIGINT;
1,657,457 ✔
342
}
343

344
static void getWalDataSliceKey(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t uid, uint64_t gid,
1,740,711 ✔
345
                               SWalDataSliceKey* pKeyBuf, const void** ppKey, int32_t* pKeyLen) {
346
  if (isRollupMultiReader(sStreamReaderInfo)) {
1,740,711 ✔
347
    *pKeyBuf = (SWalDataSliceKey){.uid = uid, .gid = gid};
60,110 ✔
348
    *ppKey = pKeyBuf;
60,110 ✔
349
    *pKeyLen = sizeof(*pKeyBuf);
60,110 ✔
350
  } else {
351
    pKeyBuf->uid = uid;
1,680,270 ✔
352
    *ppKey = &pKeyBuf->uid;
1,680,270 ✔
353
    *pKeyLen = LONG_BYTES;
1,680,270 ✔
354
  }
355
}
1,740,160 ✔
356

357
static int32_t collectWalRollupTargets(SStreamTriggerReaderInfo* sStreamReaderInfo, SSHashObj* ranges, int64_t suid,
58,747 ✔
358
                                       int64_t uid, SArray* targets) {
359
  int32_t code = TSDB_CODE_SUCCESS;
58,747 ✔
360

361
  if (sStreamReaderInfo->tableType == TD_SUPER_TABLE && suid != sStreamReaderInfo->suid) {
58,747 ✔
362
    return TSDB_CODE_SUCCESS;
67 ✔
363
  }
364

365
  taosRLockLatch(&sStreamReaderInfo->lock);
58,680 ✔
366

367
  SArray** pElements = taosHashGet(sStreamReaderInfo->tableList.uIdMap, &uid, LONG_BYTES);
58,680 ✔
368
  if (pElements == NULL) {
58,599 ✔
369
    goto end;
×
370
  }
371

372
  int32_t numOfElements = taosArrayGetSize(*pElements);
58,599 ✔
373
  for (int32_t i = 0; i < numOfElements; ++i) {
234,156 ✔
374
    SStreamTableMapElement* element = taosArrayGet(*pElements, i);
175,476 ✔
375
    if (element == NULL || element->table == NULL || element->table->markedDeleted) {
175,476 ✔
376
      continue;
×
377
    }
378

379
    SWalDataTarget target = {.gid = element->table->groupId, .window = {.skey = INT64_MIN, .ekey = INT64_MAX}};
175,476 ✔
380
    if (ranges != NULL) {
175,476 ✔
381
      void* timerange = tSimpleHashGet(ranges, &target.gid, LONG_BYTES);
173,264 ✔
382
      if (timerange == NULL) {
173,266 ✔
383
        continue;
115,447 ✔
384
      }
385
      int64_t* pRange = (int64_t*)timerange;
57,819 ✔
386
      target.window.skey = pRange[0];
57,819 ✔
387
      target.window.ekey = pRange[1];
57,819 ✔
388
    }
389

390
    if (taosArrayPush(targets, &target) == NULL) {
60,110 ✔
391
      code = terrno;
×
392
      break;
×
393
    }
394
  }
395

396
end:
58,680 ✔
397
  taosRUnLockLatch(&sStreamReaderInfo->lock);
58,680 ✔
398
  return code;
58,680 ✔
399
}
400

401
static int32_t addWalDataSlice(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, int64_t uid,
869,925 ✔
402
                               uint64_t gid, int32_t numOfRows, int64_t ver) {
403
  int32_t code = TSDB_CODE_SUCCESS;
869,925 ✔
404
  int32_t lino = 0;
869,925 ✔
405
  void*   pTask = sStreamReaderInfo->pTask;
869,925 ✔
406

407
  if (numOfRows <= 0) {
869,925 ✔
408
    goto end;
×
409
  }
410

411
  if (rsp->uidHash != NULL) {
869,925 ✔
412
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(rsp->uidHash, &uid, LONG_BYTES, &gid, LONG_BYTES));
473,761 ✔
413
    ST_TASK_DLOG("%s put uid into uidHash, uid:%" PRId64 ", gid:%" PRIu64, __func__, uid, gid);
473,497 ✔
414
  }
415

416
  SWalDataSliceKey keyBuf = {0};
869,661 ✔
417
  const void*      pKey = NULL;
869,989 ✔
418
  int32_t          keyLen = 0;
870,453 ✔
419
  getWalDataSliceKey(sStreamReaderInfo, uid, gid, &keyBuf, &pKey, &keyLen);
870,387 ✔
420

421
  SStreamWalDataSlice* pSlice = (SStreamWalDataSlice*)tSimpleHashGet(rsp->indexHash, pKey, keyLen);
869,522 ✔
422
  if (pSlice != NULL) {
871,517 ✔
423
    pSlice->uid = uid;
710,921 ✔
424
    pSlice->gId = gid;
710,921 ✔
425
    pSlice->numRows += numOfRows;
710,921 ✔
426
    ST_TASK_DLOG("%s again uid:%" PRId64 ", gid:%" PRIu64 ", total numOfRows:%d, hash:%p %d, ver:%" PRId64, __func__,
710,921 ✔
427
                 uid, gid, pSlice->numRows, rsp->indexHash, tSimpleHashGetSize(rsp->indexHash), ver);
428
  } else {
429
    SStreamWalDataSlice tmp = {.uid = uid, .gId = gid, .numRows = numOfRows, .currentRowIdx = 0, .startRowIdx = 0};
160,596 ✔
430
    ST_TASK_DLOG("%s first uid:%" PRId64 ", gid:%" PRIu64 ", numOfRows:%d, hash:%p %d, ver:%" PRId64, __func__, uid,
160,596 ✔
431
                 gid, tmp.numRows, rsp->indexHash, tSimpleHashGetSize(rsp->indexHash), ver);
432
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(rsp->indexHash, pKey, keyLen, &tmp, sizeof(tmp)));
160,596 ✔
433
  }
434

435
  rsp->totalRows += numOfRows;
871,054 ✔
436
  rsp->totalDataRows += numOfRows;
871,054 ✔
437

438
end:
871,583 ✔
439
  return code;
871,318 ✔
440
}
441

442
static SStreamWalDataSlice* getWalDataSlice(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp,
870,983 ✔
443
                                            int64_t uid, uint64_t gid) {
444
  SWalDataSliceKey keyBuf = {0};
870,983 ✔
445
  const void*      pKey = NULL;
871,184 ✔
446
  int32_t          keyLen = 0;
871,251 ✔
447
  getWalDataSliceKey(sStreamReaderInfo, uid, gid, &keyBuf, &pKey, &keyLen);
871,385 ✔
448
  return (SStreamWalDataSlice*)tSimpleHashGet(rsp->indexHash, pKey, keyLen);
870,922 ✔
449
}
450

451
static int32_t putWalMetaResult(SSHashObj* gidHash, uint64_t id, int64_t skey, int64_t ekey) {
559,888 ✔
452
  WalMetaResult  walMeta = {.id = id, .skey = skey, .ekey = ekey};
559,888 ✔
453
  WalMetaResult* data = (WalMetaResult*)tSimpleHashGet(gidHash, &walMeta.id, LONG_BYTES);
559,888 ✔
454
  if (data != NULL) {
559,423 ✔
455
    if (walMeta.skey < data->skey) data->skey = walMeta.skey;
268 ✔
456
    if (walMeta.ekey > data->ekey) data->ekey = walMeta.ekey;
268 ✔
457
    return TSDB_CODE_SUCCESS;
268 ✔
458
  }
459

460
  return tSimpleHashPut(gidHash, &walMeta.id, LONG_BYTES, &walMeta, sizeof(WalMetaResult));
559,155 ✔
461
}
462

463
static int32_t putWalMetaResultForRollupUid(SStreamTriggerReaderInfo* sStreamReaderInfo, SSHashObj* gidHash,
2,645 ✔
464
                                            int64_t suid, int64_t uid, int64_t skey, int64_t ekey) {
465
  int32_t code = TSDB_CODE_SUCCESS;
2,645 ✔
466

467
  if (sStreamReaderInfo->tableType == TD_SUPER_TABLE && suid != sStreamReaderInfo->suid) {
2,645 ✔
468
    return TSDB_CODE_SUCCESS;
×
469
  }
470

471
  taosRLockLatch(&sStreamReaderInfo->lock);
2,645 ✔
472

473
  SArray** pElements = taosHashGet(sStreamReaderInfo->tableList.uIdMap, &uid, LONG_BYTES);
2,645 ✔
474
  if (pElements == NULL) {
2,645 ✔
475
    goto end;
×
476
  }
477

478
  int32_t numOfElements = taosArrayGetSize(*pElements);
2,645 ✔
479
  for (int32_t i = 0; i < numOfElements; ++i) {
10,580 ✔
480
    SStreamTableMapElement* element = taosArrayGet(*pElements, i);
7,935 ✔
481
    if (element == NULL || element->table == NULL || element->table->markedDeleted) {
7,935 ✔
482
      continue;
×
483
    }
484
    code = putWalMetaResult(gidHash, element->table->groupId, skey, ekey);
7,935 ✔
485
    if (code != TSDB_CODE_SUCCESS) {
7,935 ✔
486
      break;
×
487
    }
488
  }
489

490
end:
2,645 ✔
491
  taosRUnLockLatch(&sStreamReaderInfo->lock);
2,645 ✔
492
  return code;
2,645 ✔
493
}
494

495
static int64_t getSuid(SStreamTriggerReaderInfo* sStreamReaderInfo, STableKeyInfo* pList) {
2,080,720 ✔
496
  int64_t suid = 0;
2,080,720 ✔
497
  if (!sStreamReaderInfo->isVtableStream) {
2,080,720 ✔
498
    suid = sStreamReaderInfo->suid;
2,062,086 ✔
499
    goto end;
2,062,086 ✔
500
  }
501

502
  if (pList == NULL) {
18,634 ✔
503
    goto end;
×
504
  }
505

506
  taosRLockLatch(&sStreamReaderInfo->lock);
18,634 ✔
507
  SStreamTableMapElement* element = taosHashGet(sStreamReaderInfo->vSetTableList.uIdMap, &pList->uid, LONG_BYTES);  
18,634 ✔
508
  if (element != 0) {
18,634 ✔
509
    suid = element->table->groupId;
17,580 ✔
510
    taosRUnLockLatch(&sStreamReaderInfo->lock);
17,580 ✔
511
    goto end;
17,580 ✔
512
  }
513
  taosRUnLockLatch(&sStreamReaderInfo->lock);
1,054 ✔
514

515
end:
2,080,720 ✔
516
  return suid;
2,080,720 ✔
517
}
518

519
static int64_t getSessionKey(int64_t session, int64_t type) { return (session | (type << 32)); }
2,096,939 ✔
520

521
int32_t sortCid(const void *lp, const void *rp) {
27,172 ✔
522
  int16_t* c1 = (int16_t*)lp;
27,172 ✔
523
  int16_t* c2 = (int16_t*)rp;
27,172 ✔
524

525
  if (*c1 < *c2) {
27,172 ✔
526
    return -1;
26,908 ✔
527
  } else if (*c1 > *c2) {
264 ✔
528
    return 1;
264 ✔
529
  }
530

531
  return 0;
×
532
}
533

534
int32_t sortSSchema(const void *lp, const void *rp) {
27,172 ✔
535
  SSchema* c1 = (SSchema*)lp;
27,172 ✔
536
  SSchema* c2 = (SSchema*)rp;
27,172 ✔
537

538
  if (c1->colId < c2->colId) {
27,172 ✔
539
    return -1;
26,908 ✔
540
  } else if (c1->colId > c2->colId) {
264 ✔
541
    return 1;
264 ✔
542
  }
543

544
  return 0;
×
545
}
546

547
static int32_t addColData(SSDataBlock* pResBlock, int32_t index, void* data) {
2,336,034 ✔
548
  SColumnInfoData* pSrc = taosArrayGet(pResBlock->pDataBlock, index);
2,336,034 ✔
549
  if (pSrc == NULL) {
2,334,923 ✔
550
    return terrno;
×
551
  }
552

553
  memcpy(pSrc->pData + pResBlock->info.rows * pSrc->info.bytes, data, pSrc->info.bytes);
2,336,733 ✔
554
  return 0;
2,336,733 ✔
555
}
556

557
static int32_t appendTsdbMetaRow(SStreamTriggerReaderInfo* sStreamReaderInfo, SSDataBlock* pDstBlock,
17,683 ✔
558
                                 const SSDataBlock* pSrcBlock, uint64_t gid, int32_t rollupTbCount, int32_t vgId) {
559
  int32_t code = TSDB_CODE_SUCCESS;
17,683 ✔
560
  int32_t lino = 0;
17,683 ✔
561
  int32_t index = 0;
17,683 ✔
562

563
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pDstBlock, pDstBlock->info.rows + 1));
17,683 ✔
564
  STREAM_CHECK_RET_GOTO(addColData(pDstBlock, index++, (void*)&pSrcBlock->info.window.skey));
17,683 ✔
565
  STREAM_CHECK_RET_GOTO(addColData(pDstBlock, index++, (void*)&pSrcBlock->info.window.ekey));
17,683 ✔
566
  STREAM_CHECK_RET_GOTO(addColData(pDstBlock, index++, (void*)&pSrcBlock->info.id.uid));
17,683 ✔
567
  if (!sStreamReaderInfo->isVtableStream) {
17,683 ✔
568
    STREAM_CHECK_RET_GOTO(addColData(pDstBlock, index++, &gid));
9,658 ✔
569
  }
570
  STREAM_CHECK_RET_GOTO(addColData(pDstBlock, index++, (void*)&pSrcBlock->info.rows));
17,683 ✔
571

572
  if (blockDataGetNumOfCols(pDstBlock) > index) {
17,683 ✔
573
    STREAM_CHECK_RET_GOTO(addColData(pDstBlock, index++, &rollupTbCount));
237 ✔
574
  }
575

576
  stDebug("vgId:%d get tsdb meta skey:%" PRId64 ", ekey:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64
17,683 ✔
577
          ", rollupTbCount:%d",
578
          vgId, pSrcBlock->info.window.skey, pSrcBlock->info.window.ekey, pSrcBlock->info.id.uid, gid,
579
          pSrcBlock->info.rows, rollupTbCount);
580
  pDstBlock->info.rows++;
17,683 ✔
581

582
end:
17,683 ✔
583
  return code;
17,683 ✔
584
}
585

586
static void destroyRollupMetaEntries(void* pData) {
79 ✔
587
  SArray** ppEntries = pData;
79 ✔
588
  if (ppEntries != NULL) {
79 ✔
589
    taosArrayDestroy(*ppEntries);
79 ✔
590
  }
591
}
79 ✔
592

593
static int32_t putRollupMetaCount(SSHashObj* pCountMap, uint64_t gid) {
237 ✔
594
  int32_t  count = 1;
237 ✔
595
  int32_t* pCount = tSimpleHashGet(pCountMap, &gid, LONG_BYTES);
237 ✔
596
  if (pCount != NULL) {
237 ✔
597
    count = *pCount + 1;
×
598
  }
599

600
  return tSimpleHashPut(pCountMap, &gid, LONG_BYTES, &count, sizeof(count));
237 ✔
601
}
602

603
static int32_t buildRollupMetaCount(STableKeyInfo* pList, int32_t num, SSHashObj** ppCountMap) {
×
604
  int32_t    code = TSDB_CODE_SUCCESS;
×
605
  int32_t    lino = 0;
×
606
  SSHashObj* pCountMap = NULL;
×
607

608
  *ppCountMap = NULL;
×
609
  pCountMap = tSimpleHashInit(TMAX(num, 1), taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT));
×
610
  STREAM_CHECK_NULL_GOTO(pCountMap, terrno);
×
611
  for (int32_t i = 0; i < num; ++i) {
×
612
    STREAM_CHECK_RET_GOTO(putRollupMetaCount(pCountMap, pList[i].groupId));
×
613
  }
614

615
  *ppCountMap = pCountMap;
×
616
  pCountMap = NULL;
×
617

618
end:
×
619
  tSimpleHashCleanup(pCountMap);
×
620
  return code;
×
621
}
622

623
static int32_t buildRollupMetaSnapshot(STableKeyInfo* pList, int32_t num, STableKeyInfo** ppReadList, int32_t* pReadNum,
79 ✔
624
                                       SSHashObj** ppByUid, SSHashObj** ppCountMap) {
625
  int32_t        code = TSDB_CODE_SUCCESS;
79 ✔
626
  int32_t        lino = 0;
79 ✔
627
  STableKeyInfo* pReadList = NULL;
79 ✔
628
  SSHashObj*     pByUid = NULL;
79 ✔
629
  SSHashObj*     pCountMap = NULL;
79 ✔
630
  SArray*        pNewEntries = NULL;
79 ✔
631

632
  *ppReadList = NULL;
79 ✔
633
  *pReadNum = 0;
79 ✔
634
  *ppByUid = NULL;
79 ✔
635
  *ppCountMap = NULL;
79 ✔
636
  if (num > 0) {
79 ✔
637
    pReadList = taosMemoryCalloc(num, sizeof(STableKeyInfo));
79 ✔
638
    STREAM_CHECK_NULL_GOTO(pReadList, terrno);
79 ✔
639
  }
640
  pByUid = tSimpleHashInit(TMAX(num, 1), taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT));
79 ✔
641
  STREAM_CHECK_NULL_GOTO(pByUid, terrno);
79 ✔
642
  tSimpleHashSetFreeFp(pByUid, destroyRollupMetaEntries);
79 ✔
643
  pCountMap = tSimpleHashInit(TMAX(num, 1), taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT));
79 ✔
644
  STREAM_CHECK_NULL_GOTO(pCountMap, terrno);
79 ✔
645

646
  for (int32_t i = 0; i < num; ++i) {
316 ✔
647
    SArray** ppEntries = tSimpleHashGet(pByUid, &pList[i].uid, LONG_BYTES);
237 ✔
648
    if (ppEntries == NULL) {
237 ✔
649
      pNewEntries = taosArrayInit(1, sizeof(STableKeyInfo));
79 ✔
650
      STREAM_CHECK_NULL_GOTO(pNewEntries, terrno);
79 ✔
651
      STREAM_CHECK_RET_GOTO(tSimpleHashPut(pByUid, &pList[i].uid, LONG_BYTES, &pNewEntries, POINTER_BYTES));
79 ✔
652
      pNewEntries = NULL;
79 ✔
653
      ppEntries = tSimpleHashGet(pByUid, &pList[i].uid, LONG_BYTES);
79 ✔
654
      STREAM_CHECK_NULL_GOTO(ppEntries, TSDB_CODE_INTERNAL_ERROR);
79 ✔
655
      pReadList[*pReadNum] = pList[i];
79 ✔
656
      (*pReadNum)++;
79 ✔
657
    }
658

659
    STREAM_CHECK_NULL_GOTO(taosArrayPush(*ppEntries, &pList[i]), terrno);
474 ✔
660
    STREAM_CHECK_RET_GOTO(putRollupMetaCount(pCountMap, pList[i].groupId));
237 ✔
661
  }
662

663
  *ppReadList = pReadList;
79 ✔
664
  pReadList = NULL;
79 ✔
665
  *ppByUid = pByUid;
79 ✔
666
  pByUid = NULL;
79 ✔
667
  *ppCountMap = pCountMap;
79 ✔
668
  pCountMap = NULL;
79 ✔
669

670
end:
79 ✔
671
  taosArrayDestroy(pNewEntries);
79 ✔
672
  taosMemoryFree(pReadList);
79 ✔
673
  tSimpleHashCleanup(pByUid);
79 ✔
674
  tSimpleHashCleanup(pCountMap);
79 ✔
675
  return code;
79 ✔
676
}
677

678
static int32_t getRollupMetaCount(SSHashObj* pCountMap, uint64_t gid) {
237 ✔
679
  int32_t* pCount = tSimpleHashGet(pCountMap, &gid, LONG_BYTES);
237 ✔
680
  return pCount == NULL ? 0 : *pCount;
237 ✔
681
}
682

683
static int32_t getTableDataInfo(SStreamReaderTaskInner* pTask, bool* hasNext) {
2,468,899 ✔
684
  int32_t code = pTask->storageApi->tsdReader.tsdNextDataBlock(pTask->pReader, hasNext);
2,468,899 ✔
685
  if (code != TSDB_CODE_SUCCESS) {
2,468,845 ✔
686
    pTask->storageApi->tsdReader.tsdReaderReleaseDataBlock(pTask->pReader);
×
687
  }
688

689
  return code;
2,468,977 ✔
690
}
691

692
static int32_t getTableData(SStreamReaderTaskInner* pTask, SSDataBlock** ppRes) {
358,652 ✔
693
  return pTask->storageApi->tsdReader.tsdReaderRetrieveDataBlock(pTask->pReader, ppRes);
358,652 ✔
694
}
695

696
static int32_t buildOTableInfoRsp(const SSTriggerOrigTableInfoRsp* rsp, void** data, size_t* size) {
9,604 ✔
697
  int32_t code = 0;
9,604 ✔
698
  int32_t lino = 0;
9,604 ✔
699
  void*   buf = NULL;
9,604 ✔
700
  int32_t len = tSerializeSTriggerOrigTableInfoRsp(NULL, 0, rsp);
9,604 ✔
701
  STREAM_CHECK_CONDITION_GOTO(len <= 0, TSDB_CODE_INVALID_PARA);
9,604 ✔
702
  buf = rpcMallocCont(len);
9,604 ✔
703
  STREAM_CHECK_NULL_GOTO(buf, terrno);
9,604 ✔
704
  int32_t actLen = tSerializeSTriggerOrigTableInfoRsp(buf, len, rsp);
9,604 ✔
705
  STREAM_CHECK_CONDITION_GOTO(actLen != len, TSDB_CODE_INVALID_PARA);
9,604 ✔
706
  *data = buf;
9,604 ✔
707
  *size = len;
9,604 ✔
708
  buf = NULL;
9,604 ✔
709
end:
9,604 ✔
710
  rpcFreeCont(buf);
9,604 ✔
711
  return code;
9,604 ✔
712
}
713

714
static bool ignoreMetaChange(int64_t tableListVer, int64_t ver) {
46,524 ✔
715
  stDebug("%s tableListVer:%" PRId64 " ver:%" PRId64, __func__, tableListVer, ver);
46,524 ✔
716
  return tableListVer >= ver;
46,524 ✔
717
}
718

719
static bool needReLoadTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int8_t tableType, int64_t suid, int64_t uid, bool isCalc){
332,788 ✔
720
  if ((tableType == TD_CHILD_TABLE || tableType == TD_VIRTUAL_CHILD_TABLE) &&
332,788 ✔
721
      sStreamReaderInfo->tableType == TD_SUPER_TABLE && 
138,021 ✔
722
      suid == sStreamReaderInfo->suid) {
81,646 ✔
723
    taosRLockLatch(&sStreamReaderInfo->lock);
3,242 ✔
724
    uint64_t gid = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, uid);
3,242 ✔
725
    taosRUnLockLatch(&sStreamReaderInfo->lock);
3,242 ✔
726
    if (gid == (uint64_t)-1) return true;
3,242 ✔
727
  }
728
  return false;
329,447 ✔
729
}
730

731
static bool matchTriggerTable(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid) {
2,959,108 ✔
732
  switch (sStreamReaderInfo->tableType) {
2,959,108 ✔
733
    case TD_SUPER_TABLE:
1,677,411 ✔
734
      return suid == sStreamReaderInfo->suid;
1,677,411 ✔
735
    case TD_CHILD_TABLE:
628,809 ✔
736
    case TD_VIRTUAL_CHILD_TABLE:
737
      return suid == sStreamReaderInfo->suid && uid == sStreamReaderInfo->uid;
628,809 ✔
738
    case TD_NORMAL_TABLE:
655,789 ✔
739
    case TD_VIRTUAL_NORMAL_TABLE:
740
      return uid == sStreamReaderInfo->uid;
655,789 ✔
741
    default:
3 ✔
742
      return true;
3 ✔
743
  }
744
}
745

746
static bool uidInTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id) {
2,960,164 ✔
747
  int32_t ret = false;
2,960,164 ✔
748
  if (!matchTriggerTable(sStreamReaderInfo, suid, uid)) {
2,960,164 ✔
749
    goto end;
1,649,097 ✔
750
  }
751
  if (sStreamReaderInfo->tableType == TD_SUPER_TABLE) {
1,310,204 ✔
752
    if (qStreamGetTableListNum(sStreamReaderInfo) == 0) goto end;
617,581 ✔
753
  }
754
  *id = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, uid);
1,308,520 ✔
755
  if (*id == -1) goto end;
1,311,234 ✔
756
  ret = true;
1,297,959 ✔
757

758
end:
2,961,619 ✔
759
  stTrace("%s ret:%d %p %p check suid:%" PRId64 " uid:%" PRId64 " gid:%"PRIu64, __func__, ret, sStreamReaderInfo, sStreamReaderInfo->tableList.gIdMap, suid, uid, *id);
2,961,619 ✔
760
  return ret;
2,958,123 ✔
761
}
762

763
static bool uidInTableListOrigin(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id) {
3,696 ✔
764
  return uidInTableList(sStreamReaderInfo, suid, uid, id);
3,696 ✔
765
}
766

767
static bool uidInTableListSet(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id, bool isCalc) {
3,339,521 ✔
768
  bool ret = false;
3,339,521 ✔
769
  taosRLockLatch(&sStreamReaderInfo->lock);
3,339,521 ✔
770
  if (sStreamReaderInfo->isVtableStream) {
3,343,532 ✔
771
    int64_t tmp[2] = {suid, uid};
385,216 ✔
772
    if(tSimpleHashGet(isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, tmp, sizeof(tmp)) != NULL) {
385,216 ✔
773
      *id = uid;
223,002 ✔
774
      ret = true;
223,002 ✔
775
    }
776
  } else {
777
    ret = uidInTableList(sStreamReaderInfo, suid, uid, id);
2,958,316 ✔
778
  }
779

780
end:
3,338,980 ✔
781
  taosRUnLockLatch(&sStreamReaderInfo->lock);
3,338,980 ✔
782
  return ret;
3,342,741 ✔
783
}
784

785
static int32_t  qTransformStreamTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, void* pTableListInfo, StreamTableListInfo* tableInfo){
66,703 ✔
786
  SArray* pList = qStreamGetTableListArray(pTableListInfo);
66,703 ✔
787
  int32_t totalSize = taosArrayGetSize(pList);
66,759 ✔
788
  int32_t code = 0;
66,759 ✔
789
  void* pTask = sStreamReaderInfo->pTask;
66,759 ✔
790
  for (int32_t i = 0; i < totalSize; ++i) {
167,343 ✔
791
    STableKeyInfo* info = taosArrayGet(pList, i);
100,584 ✔
792
    if (info == NULL) {
100,584 ✔
793
      continue;
×
794
    }
795
    code = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheTrigger, sStreamReaderInfo->pExprInfoTriggerTag, sStreamReaderInfo->numOfExprTriggerTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
100,584 ✔
796
    if (code != 0){
100,517 ✔
797
      ST_TASK_WLOG("%s cacheTag trigger failed for uid:%" PRId64",code:%d", __func__, info->uid, code);
×
798
      continue;
×
799
    }
800
    code = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheCalc, sStreamReaderInfo->pExprInfoCalcTag, sStreamReaderInfo->numOfExprCalcTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
100,517 ✔
801
    if (code != 0){
100,584 ✔
802
      ST_TASK_WLOG("%s cacheTag calc failed for uid:%" PRId64",code:%d", __func__, info->uid, code);
×
803
      continue;
×
804
    }
805
    code = qStreamSetTableList(tableInfo, info->uid, info->groupId);
100,584 ✔
806
    if (code != 0){
100,584 ✔
807
      return code;
×
808
    }
809
  }
810
  return 0;
66,685 ✔
811
}
812

813
static int32_t readRollupTagPath(SStreamTriggerReaderInfo* sStreamReaderInfo, SMetaReader* pReader, char** ppPath,
3,259 ✔
814
                                 int32_t* pPathLen, bool* pSkipped) {
815
  int32_t      code = TSDB_CODE_SUCCESS;
3,259 ✔
816
  char*        pData = NULL;
3,259 ✔
817
  char*        path = NULL;
3,259 ✔
818
  SColumnNode* pRollupCol = getRollupTagCol(sStreamReaderInfo);
3,259 ✔
819

820
  *ppPath = NULL;
3,259 ✔
821
  *pPathLen = 0;
3,259 ✔
822
  if (pSkipped != NULL) {
3,259 ✔
823
    *pSkipped = false;
3,259 ✔
824
  }
825

826
  if (pRollupCol == NULL || nodeType((SNode*)pRollupCol) != QUERY_NODE_COLUMN) {
3,259 ✔
827
    return TSDB_CODE_STREAM_INVALID_ROLLUP;
×
828
  }
829

830
  int8_t type = pRollupCol->node.resType.type;
3,259 ✔
831
  if (type != TSDB_DATA_TYPE_VARCHAR && type != TSDB_DATA_TYPE_NCHAR) {
3,259 ✔
832
    return TSDB_CODE_STREAM_INVALID_ROLLUP;
×
833
  }
834

835
  STagVal     tagVal = {.cid = pRollupCol->colId};
3,259 ✔
836
  const char* pTagVal = sStreamReaderInfo->storageApi.metaFn.extractTagVal(pReader->me.ctbEntry.pTags, type, &tagVal);
3,259 ✔
837
  if (pTagVal == NULL) {
3,259 ✔
838
    code = streamRollupSkipped(pSkipped);
×
839
    goto end;
×
840
  }
841

842
  pData = tTagValToData((const STagVal*)pTagVal, false);
3,259 ✔
843
  if (pData == NULL) {
3,259 ✔
844
    code = terrno == 0 ? streamRollupSkipped(pSkipped) : terrno;
×
845
    goto end;
×
846
  }
847

848
  int32_t tagLen = varDataLen(pData);
3,259 ✔
849
  if (tagLen == 0) {
3,259 ✔
850
    code = streamRollupSkipped(pSkipped);
×
851
    goto end;
×
852
  }
853

854
  if (type == TSDB_DATA_TYPE_NCHAR) {
3,259 ✔
855
    path = taosMemoryCalloc(1, tagLen + 1);
2,327 ✔
856
    if (path == NULL) {
2,327 ✔
857
      code = terrno;
×
858
      goto end;
×
859
    }
860
    int32_t len = taosUcs4ToMbs((TdUcs4*)varDataVal(pData), tagLen, path, NULL);
2,327 ✔
861
    if (len < 0) {
2,327 ✔
862
      code = len;
×
863
      goto end;
×
864
    }
865
    if (len == 0) {
2,327 ✔
866
      code = streamRollupSkipped(pSkipped);
×
867
      goto end;
×
868
    }
869
    path[len] = '\0';
2,327 ✔
870
    *pPathLen = len;
2,327 ✔
871
  } else {
872
    path = taosMemoryMalloc(tagLen + 1);
932 ✔
873
    if (path == NULL) {
932 ✔
874
      code = terrno;
×
875
      goto end;
×
876
    }
877
    memcpy(path, varDataVal(pData), tagLen);
932 ✔
878
    path[tagLen] = '\0';
932 ✔
879
    *pPathLen = tagLen;
932 ✔
880
  }
881

882
  *ppPath = path;
3,259 ✔
883
  path = NULL;
3,259 ✔
884

885
end:
3,259 ✔
886
  taosMemoryFree(path);
3,259 ✔
887
  taosMemoryFree(pData);
3,259 ✔
888
  return code;
3,259 ✔
889
}
890

891
static int32_t setRollupGroupValue(SStreamGroupValue* pValue, const char* path, int32_t pathLen, int8_t type) {
7,237 ✔
892
  pValue->data.type = type;
7,237 ✔
893
  pValue->isNull = false;
7,237 ✔
894

895
  if (type == TSDB_DATA_TYPE_NCHAR) {
7,237 ✔
896
    int32_t ucs4Cap = (pathLen + 1) * TSDB_NCHAR_SIZE;
6,171 ✔
897
    pValue->data.pData = taosMemoryCalloc(1, ucs4Cap);
6,171 ✔
898
    if (pValue->data.pData == NULL) {
6,171 ✔
899
      return terrno;
×
900
    }
901
    if (!taosMbsToUcs4(path, pathLen, (TdUcs4*)pValue->data.pData, ucs4Cap, &pValue->data.nData, NULL)) {
6,171 ✔
902
      taosMemoryFreeClear(pValue->data.pData);
×
903
      return terrno;
×
904
    }
905
  } else {
906
    pValue->data.pData = taosMemoryMalloc(pathLen + 1);
1,066 ✔
907
    if (pValue->data.pData == NULL) {
1,066 ✔
908
      return terrno;
×
909
    }
910
    memcpy(pValue->data.pData, path, pathLen);
1,066 ✔
911
    ((char*)pValue->data.pData)[pathLen] = '\0';
1,066 ✔
912
    pValue->data.nData = pathLen;
1,066 ✔
913
  }
914

915
  return TSDB_CODE_SUCCESS;
7,237 ✔
916
}
917

918
static void formatRollupGroupPath(SArray* gInfo, char* buf, int32_t bufLen) {
6,832 ✔
919
  if (buf == NULL || bufLen <= 0) {
6,832 ✔
920
    return;
×
921
  }
922

923
  buf[0] = '\0';
6,832 ✔
924
  if (gInfo == NULL || taosArrayGetSize(gInfo) <= 0) {
6,832 ✔
925
    return;
×
926
  }
927

928
  SStreamGroupValue* pValue = taosArrayGet(gInfo, 0);
6,832 ✔
929
  if (pValue == NULL || pValue->isNull || pValue->data.pData == NULL) {
6,832 ✔
930
    return;
×
931
  }
932

933
  if (pValue->data.type == TSDB_DATA_TYPE_NCHAR) {
6,832 ✔
934
    if (pValue->data.nData >= bufLen) {
5,766 ✔
935
      buf[bufLen - 1] = '\0';
×
936
      return;
×
937
    }
938

939
    int32_t len = taosUcs4ToMbs((TdUcs4*)pValue->data.pData, pValue->data.nData, buf, NULL);
5,766 ✔
940
    if (len < 0 || len >= bufLen) {
5,766 ✔
941
      buf[0] = '\0';
×
942
    } else {
943
      buf[len] = '\0';
5,766 ✔
944
    }
945
  } else {
946
    int32_t len = TMIN(pValue->data.nData, bufLen - 1);
1,066 ✔
947
    memcpy(buf, pValue->data.pData, len);
1,066 ✔
948
    buf[len] = '\0';
1,066 ✔
949
  }
950
}
951

952
static void logRollupGroupSummary(SStreamTriggerReaderInfo* sStreamReaderInfo) {
2,777 ✔
953
  if (!sStreamReaderInfo->isRollupReader || sStreamReaderInfo->groupIdMap == NULL) {
2,777 ✔
954
    return;
×
955
  }
956

957
  void*   pTask = sStreamReaderInfo->pTask;
2,777 ✔
958
  int32_t total = taosHashGetSize(sStreamReaderInfo->groupIdMap);
2,777 ✔
959
  char    sample[3][TSDB_TABLE_NAME_LEN] = {{0}};
2,777 ✔
960
  int32_t sampleNum = 0;
2,777 ✔
961

962
  SArray** ppGroup = taosHashIterate(sStreamReaderInfo->groupIdMap, NULL);
2,777 ✔
963
  while (ppGroup != NULL && sampleNum < 3) {
9,609 ✔
964
    formatRollupGroupPath(*ppGroup, sample[sampleNum], sizeof(sample[sampleNum]));
6,832 ✔
965
    sampleNum++;
6,832 ✔
966
    ppGroup = taosHashIterate(sStreamReaderInfo->groupIdMap, ppGroup);
6,832 ✔
967
  }
968
  if (ppGroup != NULL) {
2,777 ✔
969
    taosHashCancelIterate(sStreamReaderInfo->groupIdMap, ppGroup);
405 ✔
970
  }
971

972
  SColumnNode* pRollupCol = getRollupTagCol(sStreamReaderInfo);
2,777 ✔
973
  ST_TASK_ILOG("rollup by %s, total_groups=%d, sample_paths=[%s,%s,%s]", pRollupCol == NULL ? "" : pRollupCol->colName,
2,777 ✔
974
               total, sample[0], sample[1], sample[2]);
975
}
976

977
static int32_t putRollupGroupValue(SStreamTriggerReaderInfo* sStreamReaderInfo, uint64_t gid, const char* path,
8,047 ✔
978
                                   int32_t pathLen) {
979
  int32_t      code = TSDB_CODE_SUCCESS;
8,047 ✔
980
  SArray*      gInfo = NULL;
8,047 ✔
981
  SColumnNode* pRollupCol = getRollupTagCol(sStreamReaderInfo);
8,047 ✔
982
  void*        pTask = sStreamReaderInfo->pTask;
8,047 ✔
983

984
  if (taosHashGet(sStreamReaderInfo->groupIdMap, &gid, sizeof(gid)) != NULL) {
8,047 ✔
985
    return TSDB_CODE_SUCCESS;
810 ✔
986
  }
987

988
  gInfo = taosArrayInit(1, sizeof(SStreamGroupValue));
7,237 ✔
989
  if (gInfo == NULL) {
7,237 ✔
990
    return terrno;
×
991
  }
992

993
  SStreamGroupValue* pValue = taosArrayReserve(gInfo, 1);
7,237 ✔
994
  if (pValue == NULL) {
7,237 ✔
995
    code = terrno;
×
996
    goto end;
×
997
  }
998

999
  code = setRollupGroupValue(pValue, path, pathLen, pRollupCol->node.resType.type);
7,237 ✔
1000
  if (code != TSDB_CODE_SUCCESS) {
7,237 ✔
1001
    goto end;
×
1002
  }
1003

1004
  code = taosHashPut(sStreamReaderInfo->groupIdMap, &gid, sizeof(gid), &gInfo, POINTER_BYTES);
7,237 ✔
1005
  if (code != TSDB_CODE_SUCCESS) {
7,237 ✔
1006
    goto end;
×
1007
  }
1008
  ST_TASK_DLOG("rollup new group gid=%" PRIu64 " path=%.*s", gid, pathLen, path);
7,237 ✔
1009
  gInfo = NULL;
7,237 ✔
1010

1011
end:
7,237 ✔
1012
  taosArrayDestroyEx(gInfo, tDestroySStreamGroupValue);
7,237 ✔
1013
  return code;
7,237 ✔
1014
}
1015

1016
static int32_t setRollupPrefixColData(SColumnInfoData* pColData, const char* prefix, int32_t prefixLen) {
×
1017
  int32_t code = TSDB_CODE_SUCCESS;
×
1018
  int32_t lino = 0;
×
1019
  char*   ucs4 = NULL;
×
1020

1021
  if (pColData->info.type == TSDB_DATA_TYPE_NCHAR) {
×
1022
    int32_t ucs4Cap = (prefixLen + 1) * TSDB_NCHAR_SIZE;
×
1023
    int32_t ucs4Len = 0;
×
1024

1025
    ucs4 = taosMemoryCalloc(1, ucs4Cap);
×
1026
    STREAM_CHECK_NULL_GOTO(ucs4, terrno);
×
1027
    if (!taosMbsToUcs4(prefix, prefixLen, (TdUcs4*)ucs4, ucs4Cap, &ucs4Len, NULL)) {
×
1028
      code = streamRollupErrno();
×
1029
      goto end;
×
1030
    }
1031

1032
    STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, 0, ucs4, ucs4Len, false));
×
1033
  } else if (pColData->info.type == TSDB_DATA_TYPE_VARCHAR || pColData->info.type == TSDB_DATA_TYPE_BINARY) {
×
1034
    STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, 0, prefix, prefixLen, false));
×
1035
  } else {
1036
    code = TSDB_CODE_STREAM_INVALID_ROLLUP;
×
1037
    lino = __LINE__;
×
1038
    goto end;
×
1039
  }
1040

1041
end:
×
1042
  taosMemoryFree(ucs4);
×
1043
  STREAM_PRINT_LOG_END(code, lino);
×
1044
  return code;
×
1045
}
1046

1047
static int32_t createRollupFilterResult(SScalarParam* pParam) {
×
1048
  SColumnInfoData* pColumnData = taosMemoryCalloc(1, sizeof(SColumnInfoData));
×
1049
  if (pColumnData == NULL) {
×
1050
    return terrno;
×
1051
  }
1052

1053
  pColumnData->info.type = TSDB_DATA_TYPE_BOOL;
×
1054
  pColumnData->info.bytes = sizeof(bool);
×
1055

1056
  int32_t code = colInfoDataEnsureCapacity(pColumnData, 1, true);
×
1057
  if (code != TSDB_CODE_SUCCESS) {
×
1058
    colDataDestroy(pColumnData);
×
1059
    taosMemoryFree(pColumnData);
×
1060
    return code;
×
1061
  }
1062

1063
  pParam->columnData = pColumnData;
×
1064
  pParam->colAlloced = true;
×
1065
  return TSDB_CODE_SUCCESS;
×
1066
}
1067

1068
static int32_t appendRollupFilterColInfo(SSDataBlock* pBlock, SArray* pColList) {
×
1069
  int32_t code = TSDB_CODE_SUCCESS;
×
1070
  int32_t lino = 0;
×
1071

1072
  int32_t numOfCols = taosArrayGetSize(pColList);
×
1073
  for (int32_t i = 0; i < numOfCols; ++i) {
×
1074
    SColumnInfo* pInfo = taosArrayGet(pColList, i);
×
1075
    STREAM_CHECK_NULL_GOTO(pInfo, terrno);
×
1076

1077
    SColumnInfoData colInfo = {0};
×
1078
    colInfo.info = *pInfo;
×
1079
    STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &colInfo));
×
1080
  }
1081

1082
end:
×
1083
  STREAM_PRINT_LOG_END(code, lino);
×
1084
  return code;
×
1085
}
1086

1087
static bool isRollupFilterTagCol(const SColumnNode* pRollupCol, const SColumnInfoData* pDst) {
×
1088
  return pRollupCol != NULL && pDst->info.colId == pRollupCol->colId &&
×
1089
         pDst->info.type == pRollupCol->node.resType.type;
×
1090
}
1091

1092
static int32_t setRollupFilterTagVal(SStreamTriggerReaderInfo* sStreamReaderInfo, SMetaReader* pReader,
×
1093
                                     SColumnInfoData* pDst) {
1094
  int32_t      code = TSDB_CODE_SUCCESS;
×
1095
  int32_t      lino = 0;
×
1096
  char*        data = NULL;
×
1097
  const char*  pTagVal = NULL;
×
1098
  SColumnNode* pRollupCol = getRollupTagCol(sStreamReaderInfo);
×
1099

1100
  STREAM_CHECK_NULL_GOTO(pRollupCol, TSDB_CODE_STREAM_INVALID_ROLLUP);
×
1101

1102
  if (pDst->info.colId == -1) {
×
1103
    char tbname[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1104
    STREAM_CHECK_RET_GOTO(
×
1105
        sStreamReaderInfo->storageApi.metaFn.getTableNameByUid(sStreamReaderInfo->pVnode, pReader->me.uid, tbname, 0));
×
1106
    STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, tbname, false));
×
1107
    goto end;
×
1108
  }
1109

1110
  if (isRollupFilterTagCol(pRollupCol, pDst)) {
×
1111
    goto end;
×
1112
  }
1113

1114
  STagVal tagVal = {.cid = pDst->info.colId};
×
1115
  pTagVal = sStreamReaderInfo->storageApi.metaFn.extractTagVal(pReader->me.ctbEntry.pTags, pDst->info.type, &tagVal);
×
1116
  if (pTagVal != NULL && pDst->info.type != TSDB_DATA_TYPE_JSON) {
×
1117
    data = tTagValToData((const STagVal*)pTagVal, false);
×
1118
    STREAM_CHECK_CONDITION_GOTO(data == NULL && IS_VAR_DATA_TYPE(((const STagVal*)pTagVal)->type), terrno);
×
1119
  } else {
1120
    data = (char*)pTagVal;
×
1121
  }
1122

1123
  bool isNull = data == NULL || (pDst->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data));
×
1124
  STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, data, isNull));
×
1125

1126
end:
×
1127
  if (pTagVal != NULL && pDst->info.type != TSDB_DATA_TYPE_JSON && IS_VAR_DATA_TYPE(((const STagVal*)pTagVal)->type)) {
×
1128
    taosMemoryFree(data);
×
1129
  }
1130
  STREAM_PRINT_LOG_END(code, lino);
×
1131
  return code;
×
1132
}
1133

1134
static int32_t createRollupPrefixFilterBlock(SStreamTriggerReaderInfo* sStreamReaderInfo, SMetaReader* pReader,
×
1135
                                             SArray* pColList, SSDataBlock** ppBlock) {
1136
  int32_t      code = TSDB_CODE_SUCCESS;
×
1137
  int32_t      lino = 0;
×
1138
  SSDataBlock* pBlock = NULL;
×
1139

1140
  *ppBlock = NULL;
×
1141

1142
  STREAM_CHECK_RET_GOTO(createDataBlock(&pBlock));
×
1143
  STREAM_CHECK_RET_GOTO(appendRollupFilterColInfo(pBlock, pColList));
×
1144
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, 1));
×
1145
  pBlock->info.rows = 1;
×
1146

1147
  int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
×
1148
  for (int32_t i = 0; i < numOfCols; ++i) {
×
1149
    SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
×
1150
    STREAM_CHECK_NULL_GOTO(pDst, terrno);
×
1151
    STREAM_CHECK_RET_GOTO(setRollupFilterTagVal(sStreamReaderInfo, pReader, pDst));
×
1152
  }
1153

1154
  *ppBlock = pBlock;
×
1155
  pBlock = NULL;
×
1156

1157
end:
×
1158
  blockDataDestroy(pBlock);
×
1159
  STREAM_PRINT_LOG_END(code, lino);
×
1160
  return code;
×
1161
}
1162

1163
static int32_t setRollupPrefixFilterTagVals(SStreamTriggerReaderInfo* sStreamReaderInfo, SSDataBlock* pBlock,
×
1164
                                            const char* prefix, int32_t prefixLen) {
1165
  int32_t      code = TSDB_CODE_SUCCESS;
×
1166
  int32_t      lino = 0;
×
1167
  SColumnNode* pRollupCol = getRollupTagCol(sStreamReaderInfo);
×
1168

1169
  STREAM_CHECK_NULL_GOTO(pRollupCol, TSDB_CODE_STREAM_INVALID_ROLLUP);
×
1170

1171
  int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
×
1172
  for (int32_t i = 0; i < numOfCols; ++i) {
×
1173
    SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
×
1174
    STREAM_CHECK_NULL_GOTO(pDst, terrno);
×
1175
    if (isRollupFilterTagCol(pRollupCol, pDst)) {
×
1176
      STREAM_CHECK_RET_GOTO(setRollupPrefixColData(pDst, prefix, prefixLen));
×
1177
    }
1178
  }
1179

1180
end:
×
1181
  STREAM_PRINT_LOG_END(code, lino);
×
1182
  return code;
×
1183
}
1184

1185
static int32_t rollupPrefixPassesTagFilter(SStreamTriggerReaderInfo* sStreamReaderInfo, SSDataBlock* pBlock,
8,047 ✔
1186
                                           SArray* pBlockList, const char* prefix, int32_t prefixLen, bool* pPassed) {
1187
  int32_t      code = TSDB_CODE_SUCCESS;
8,047 ✔
1188
  int32_t      lino = 0;
8,047 ✔
1189
  SScalarParam output = {0};
8,047 ✔
1190

1191
  *pPassed = true;
8,047 ✔
1192
  if (sStreamReaderInfo->pTagCond == NULL) {
8,047 ✔
1193
    return TSDB_CODE_SUCCESS;
8,047 ✔
1194
  }
1195

1196
  STREAM_CHECK_RET_GOTO(setRollupPrefixFilterTagVals(sStreamReaderInfo, pBlock, prefix, prefixLen));
×
1197
  STREAM_CHECK_RET_GOTO(createRollupFilterResult(&output));
×
1198

1199
  gTaskScalarExtra.pStreamInfo = NULL;
×
1200
  gTaskScalarExtra.pStreamRange = NULL;
×
1201
  STREAM_CHECK_RET_GOTO(scalarCalculate(sStreamReaderInfo->pTagCond, pBlockList, &output, &gTaskScalarExtra));
×
1202

1203
  *pPassed = output.columnData != NULL && !colDataIsNull_s(output.columnData, 0) && *(bool*)output.columnData->pData;
×
1204

1205
end:
×
1206
  colDataDestroy(output.columnData);
×
1207
  taosMemoryFree(output.columnData);
×
1208
  STREAM_PRINT_LOG_END(code, lino);
×
1209
  return code;
×
1210
}
1211

1212
static int32_t appendRollupGroupsForUid(SStreamTriggerReaderInfo* sStreamReaderInfo, uint64_t uid, SArray* pTableList,
3,259 ✔
1213
                                        StreamTableListInfo* tableInfo, SArray* pTagFilterCols) {
1214
  int32_t      code = TSDB_CODE_SUCCESS;
3,259 ✔
1215
  char*        path = NULL;
3,259 ✔
1216
  int32_t      pathLen = 0;
3,259 ✔
1217
  bool         skipped = false;
3,259 ✔
1218
  void*        pTask = sStreamReaderInfo->pTask;
3,259 ✔
1219
  SRollupPath  rollupPath = {0};
3,259 ✔
1220
  SArray*      prefixes = NULL;
3,259 ✔
1221
  SSDataBlock* pFilterBlock = NULL;
3,259 ✔
1222
  SArray*      pFilterBlockList = NULL;
3,259 ✔
1223
  SMetaReader  mr = {0};
3,259 ✔
1224
  SStorageAPI* api = &sStreamReaderInfo->storageApi;
3,259 ✔
1225

1226
  api->metaReaderFn.initReader(&mr, sStreamReaderInfo->pVnode, META_READER_LOCK, &api->metaFn, 0);
3,259 ✔
1227
  code = api->metaReaderFn.getEntryGetUidCache(&mr, uid);
3,259 ✔
1228
  api->metaReaderFn.readerReleaseLock(&mr);
3,259 ✔
1229
  if (code != TSDB_CODE_SUCCESS) {
3,259 ✔
1230
    goto end;
×
1231
  }
1232

1233
  code = readRollupTagPath(sStreamReaderInfo, &mr, &path, &pathLen, &skipped);
3,259 ✔
1234
  if (skipped) {
3,259 ✔
1235
    goto end;
×
1236
  }
1237
  if (code != TSDB_CODE_SUCCESS) {
3,259 ✔
1238
    goto end;
×
1239
  }
1240

1241
  code = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheTrigger,
3,259 ✔
1242
                  sStreamReaderInfo->pExprInfoTriggerTag, sStreamReaderInfo->numOfExprTriggerTag,
1243
                  &sStreamReaderInfo->storageApi, uid, 0, NULL);
1244
  if (code != TSDB_CODE_SUCCESS) {
3,259 ✔
1245
    ST_TASK_WLOG("%s cacheTag trigger failed for uid:%" PRId64 ",code:%d", __func__, uid, code);
×
1246
    code = TSDB_CODE_SUCCESS;
×
1247
    goto end;
×
1248
  }
1249
  code =
×
1250
      cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheCalc, sStreamReaderInfo->pExprInfoCalcTag,
3,259 ✔
1251
               sStreamReaderInfo->numOfExprCalcTag, &sStreamReaderInfo->storageApi, uid, 0, NULL);
1252
  if (code != TSDB_CODE_SUCCESS) {
3,259 ✔
1253
    ST_TASK_WLOG("%s cacheTag calc failed for uid:%" PRId64 ",code:%d", __func__, uid, code);
×
1254
    code = TSDB_CODE_SUCCESS;
×
1255
    goto end;
×
1256
  }
1257

1258
  skipped = false;
3,259 ✔
1259
  code = parseRollupPath(path, pathLen, &rollupPath, &skipped);
3,259 ✔
1260
  if (skipped) {
3,259 ✔
1261
    goto end;
×
1262
  }
1263
  if (code != TSDB_CODE_SUCCESS) {
3,259 ✔
1264
    if (code == TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH) {
316 ✔
1265
      setRollupIllegalPathInfo(sStreamReaderInfo, uid, path, pathLen);
316 ✔
1266
    }
1267
    ST_TASK_ELOG("%s parse rollup tag failed for uid:%" PRId64 ", value:%s, code:%d", __func__, uid, path, code);
316 ✔
1268
    goto end;
316 ✔
1269
  }
1270

1271
  code = buildRollupPrefixes(&rollupPath, &prefixes);
2,943 ✔
1272
  if (code == TSDB_CODE_SUCCESS && sStreamReaderInfo->pTagCond != NULL) {
2,943 ✔
1273
    code = createRollupPrefixFilterBlock(sStreamReaderInfo, &mr, pTagFilterCols, &pFilterBlock);
×
1274
  }
1275
  if (code == TSDB_CODE_SUCCESS && sStreamReaderInfo->pTagCond != NULL) {
2,943 ✔
1276
    pFilterBlockList = taosArrayInit(1, POINTER_BYTES);
×
1277
    if (pFilterBlockList == NULL) {
×
1278
      code = terrno;
×
1279
    } else if (taosArrayPush(pFilterBlockList, &pFilterBlock) == NULL) {
×
1280
      code = terrno;
×
1281
    }
1282
  }
1283
  if (code == TSDB_CODE_SUCCESS) {
2,943 ✔
1284
    int32_t numOfPrefixes = taosArrayGetSize(prefixes);
2,943 ✔
1285
    for (int32_t j = 0; j < numOfPrefixes; ++j) {
10,990 ✔
1286
      char* prefix = *(char**)taosArrayGet(prefixes, j);
8,047 ✔
1287
      bool  passFilter = true;
8,047 ✔
1288
      code = rollupPrefixPassesTagFilter(sStreamReaderInfo, pFilterBlock, pFilterBlockList, prefix,
8,047 ✔
1289
                                         (int32_t)strlen(prefix), &passFilter);
8,047 ✔
1290
      if (code != TSDB_CODE_SUCCESS) {
8,047 ✔
1291
        break;
×
1292
      }
1293
      if (!passFilter) {
8,047 ✔
1294
        continue;
×
1295
      }
1296
      uint64_t gid = computeRollupGid(sStreamReaderInfo->suid, prefix);
8,047 ✔
1297
      if (gid == 0) {
8,047 ✔
1298
        code = TSDB_CODE_OUT_OF_MEMORY;
×
1299
        break;
×
1300
      }
1301
      if (tableInfo != NULL) {
8,047 ✔
1302
        code = qStreamSetTableList(tableInfo, uid, gid);
8,047 ✔
1303
        if (code != TSDB_CODE_SUCCESS) {
8,047 ✔
1304
          break;
×
1305
        }
1306
      }
1307
      if (pTableList != NULL) {
8,047 ✔
1308
        STableKeyInfo keyInfo = {.uid = uid, .groupId = gid};
×
1309
        if (taosArrayPush(pTableList, &keyInfo) == NULL) {
×
1310
          code = terrno;
×
1311
          break;
×
1312
        }
1313
      }
1314
      code = putRollupGroupValue(sStreamReaderInfo, gid, prefix, (int32_t)strlen(prefix));
8,047 ✔
1315
      if (code != TSDB_CODE_SUCCESS) {
8,047 ✔
1316
        break;
×
1317
      }
1318
    }
1319
  }
1320

1321
end:
3,259 ✔
1322
  blockDataDestroy(pFilterBlock);
3,259 ✔
1323
  taosArrayDestroy(pFilterBlockList);
3,259 ✔
1324
  taosArrayDestroyP(prefixes, NULL);
3,259 ✔
1325
  destroyRollupPath(&rollupPath);
3,259 ✔
1326
  taosMemoryFree(path);
3,259 ✔
1327
  api->metaReaderFn.clearReader(&mr);
3,259 ✔
1328
  return code;
3,259 ✔
1329
}
1330

1331
static int32_t qTransformRollupStreamTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, void* pTableListInfo,
3,093 ✔
1332
                                               StreamTableListInfo* tableInfo) {
1333
  int32_t code = TSDB_CODE_SUCCESS;
3,093 ✔
1334
  SArray* pList = qStreamGetTableListArray(pTableListInfo);
3,093 ✔
1335
  SArray* pTagFilterCols = NULL;
3,093 ✔
1336

1337
  if (sStreamReaderInfo->pTagCond != NULL) {
3,093 ✔
1338
    code = qGetColumnsFromNodeList(sStreamReaderInfo->pTagCond, false, &pTagFilterCols);
×
1339
    if (code != TSDB_CODE_SUCCESS) {
×
1340
      goto end;
×
1341
    }
1342
  }
1343

1344
  int32_t totalSize = taosArrayGetSize(pList);
3,093 ✔
1345
  for (int32_t i = 0; i < totalSize; ++i) {
6,036 ✔
1346
    STableKeyInfo* info = taosArrayGet(pList, i);
3,259 ✔
1347
    if (info == NULL) {
3,259 ✔
1348
      continue;
×
1349
    }
1350

1351
    code = appendRollupGroupsForUid(sStreamReaderInfo, info->uid, NULL, tableInfo, pTagFilterCols);
3,259 ✔
1352
    if (code != TSDB_CODE_SUCCESS) {
3,259 ✔
1353
      goto end;
316 ✔
1354
    }
1355
  }
1356

1357
end:
3,093 ✔
1358
  taosArrayDestroy(pTagFilterCols);
3,093 ✔
1359
  return code;
3,093 ✔
1360
}
1361

1362
static int32_t buildRollupTableListFromSource(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* pSourceTableList,
×
1363
                                              SArray** ppTableList) {
1364
  int32_t code = TSDB_CODE_SUCCESS;
×
1365
  SArray* pTableList = NULL;
×
1366
  SArray* pTagFilterCols = NULL;
×
1367

1368
  *ppTableList = NULL;
×
1369
  pTableList = taosArrayInit(TMAX(taosArrayGetSize(pSourceTableList), 1), sizeof(STableKeyInfo));
×
1370
  if (pTableList == NULL) {
×
1371
    return terrno;
×
1372
  }
1373

1374
  if (sStreamReaderInfo->pTagCond != NULL) {
×
1375
    code = qGetColumnsFromNodeList(sStreamReaderInfo->pTagCond, false, &pTagFilterCols);
×
1376
    if (code != TSDB_CODE_SUCCESS) {
×
1377
      goto end;
×
1378
    }
1379
  }
1380

1381
  int32_t totalSize = taosArrayGetSize(pSourceTableList);
×
1382
  for (int32_t i = 0; i < totalSize; ++i) {
×
1383
    STableKeyInfo* info = taosArrayGet(pSourceTableList, i);
×
1384
    if (info == NULL) {
×
1385
      continue;
×
1386
    }
1387

1388
    code = appendRollupGroupsForUid(sStreamReaderInfo, info->uid, pTableList, NULL, pTagFilterCols);
×
1389
    if (code != TSDB_CODE_SUCCESS) {
×
1390
      goto end;
×
1391
    }
1392
  }
1393

1394
  *ppTableList = pTableList;
×
1395
  pTableList = NULL;
×
1396

1397
end:
×
1398
  taosArrayDestroy(pTagFilterCols);
×
1399
  taosArrayDestroy(pTableList);
×
1400
  return code;
×
1401
}
1402

1403
static int32_t generateTablistForStreamReader(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo) {
69,787 ✔
1404
  int32_t                   code = 0;
69,787 ✔
1405
  int32_t                   lino = 0;
69,787 ✔
1406
  SNodeList* groupNew = NULL;   
69,787 ✔
1407
  void* pTableListInfo = NULL;
69,852 ✔
1408

1409
  if (!sStreamReaderInfo->isRollupReader) {
69,852 ✔
1410
    STREAM_CHECK_RET_GOTO(nodesCloneList(sStreamReaderInfo->partitionCols, &groupNew));
66,759 ✔
1411
  }
1412

1413
  STREAM_CHECK_RET_GOTO(qStreamCreateTableListForReader(
69,852 ✔
1414
      pVnode, sStreamReaderInfo->suid, sStreamReaderInfo->uid, sStreamReaderInfo->tableType, groupNew, true,
1415
      sStreamReaderInfo->pTagCond, sStreamReaderInfo->pTagIndexCond, &sStreamReaderInfo->storageApi, &pTableListInfo,
1416
      sStreamReaderInfo->isRollupReader ? NULL : sStreamReaderInfo->groupIdMap));
74 ✔
1417

1418
  if (sStreamReaderInfo->isRollupReader) {
69,852 ✔
1419
    STREAM_CHECK_RET_GOTO(
3,093 ✔
1420
        qTransformRollupStreamTableList(sStreamReaderInfo, pTableListInfo, &sStreamReaderInfo->tableList));
×
1421
    logRollupGroupSummary(sStreamReaderInfo);
2,777 ✔
1422
  } else {
1423
    STREAM_CHECK_RET_GOTO(qTransformStreamTableList(sStreamReaderInfo, pTableListInfo, &sStreamReaderInfo->tableList));
66,759 ✔
1424
  }
1425

1426
  void* pTask = sStreamReaderInfo->pTask;
69,536 ✔
1427
  ST_TASK_DLOG("vgId:%d %s tablelist size:%" PRIzu, TD_VID(pVnode), __func__, taosArrayGetSize(sStreamReaderInfo->tableList.pTableList));
69,536 ✔
1428
end:
69,778 ✔
1429
  nodesDestroyList(groupNew);
69,852 ✔
1430
  qStreamDestroyTableList(pTableListInfo);
69,852 ✔
1431
  STREAM_PRINT_LOG_END(code, lino);
69,852 ✔
1432
  return code;
69,852 ✔
1433
}
1434

1435
static int32_t buildVTableInfoRsp(const SStreamMsgVTableInfo* rsp, void** data, size_t* size) {
7,892 ✔
1436
  int32_t code = 0;
7,892 ✔
1437
  int32_t lino = 0;
7,892 ✔
1438
  void*   buf = NULL;
7,892 ✔
1439
  int32_t len = tSerializeSStreamMsgVTableInfo(NULL, 0, rsp);
7,892 ✔
1440
  STREAM_CHECK_CONDITION_GOTO(len <= 0, TSDB_CODE_INVALID_PARA);
7,892 ✔
1441
  buf = rpcMallocCont(len);
7,892 ✔
1442
  STREAM_CHECK_NULL_GOTO(buf, terrno);
7,892 ✔
1443
  int32_t actLen = tSerializeSStreamMsgVTableInfo(buf, len, rsp);
7,892 ✔
1444
  STREAM_CHECK_CONDITION_GOTO(actLen != len, TSDB_CODE_INVALID_PARA);
7,892 ✔
1445
  *data = buf;
7,892 ✔
1446
  *size = len;
7,892 ✔
1447
  buf = NULL;
7,892 ✔
1448
end:
7,892 ✔
1449
  rpcFreeCont(buf);
7,892 ✔
1450
  return code;
7,892 ✔
1451
}
1452

1453
static int32_t buildTsRsp(const SStreamTsResponse* tsRsp, void** data, size_t* size) {
110,354 ✔
1454
  int32_t code = 0;
110,354 ✔
1455
  int32_t lino = 0;
110,354 ✔
1456
  void*   buf = NULL;
110,354 ✔
1457
  int32_t len = tSerializeSStreamTsResponse(NULL, 0, tsRsp);
110,354 ✔
1458
  STREAM_CHECK_CONDITION_GOTO(len <= 0, TSDB_CODE_INVALID_PARA);
110,288 ✔
1459
  buf = rpcMallocCont(len);
110,288 ✔
1460
  STREAM_CHECK_NULL_GOTO(buf, terrno);
110,420 ✔
1461
  int32_t actLen = tSerializeSStreamTsResponse(buf, len, tsRsp);
110,420 ✔
1462
  STREAM_CHECK_CONDITION_GOTO(actLen != len, TSDB_CODE_INVALID_PARA);
110,354 ✔
1463
  *data = buf;
110,354 ✔
1464
  *size = len;
110,354 ✔
1465
  buf = NULL;
110,354 ✔
1466
end:
110,354 ✔
1467
  rpcFreeCont(buf);
110,354 ✔
1468
  return code;
110,420 ✔
1469
}
1470

1471

1472
static int32_t buildRsp(SSDataBlock* pBlock, void** data, size_t* size) {
2,094,842 ✔
1473
  int32_t code = 0;
2,094,842 ✔
1474
  int32_t lino = 0;
2,094,842 ✔
1475
  void*   buf = NULL;
2,094,842 ✔
1476
  STREAM_CHECK_CONDITION_GOTO(pBlock == NULL || pBlock->info.rows == 0, TSDB_CODE_SUCCESS);
2,094,842 ✔
1477
  size_t dataEncodeSize = blockGetEncodeSize(pBlock);
280,247 ✔
1478
  buf = rpcMallocCont(dataEncodeSize);
280,247 ✔
1479
  STREAM_CHECK_NULL_GOTO(buf, terrno);
280,313 ✔
1480
  int32_t actualLen = blockEncode(pBlock, buf, dataEncodeSize, taosArrayGetSize(pBlock->pDataBlock));
280,313 ✔
1481
  STREAM_CHECK_CONDITION_GOTO(actualLen < 0, terrno);
280,313 ✔
1482
  *data = buf;
280,313 ✔
1483
  *size = dataEncodeSize;
280,313 ✔
1484
  buf = NULL;
280,313 ✔
1485
end:
2,094,908 ✔
1486
  rpcFreeCont(buf);
2,094,908 ✔
1487
  return code;
2,094,974 ✔
1488
}
1489

1490
static int32_t buildArrayRsp(SArray* pBlockList, void** data, size_t* size) {
20,532 ✔
1491
  int32_t code = 0;
20,532 ✔
1492
  int32_t lino = 0;
20,532 ✔
1493

1494
  void*   buf = NULL;
20,532 ✔
1495

1496
  int32_t blockNum = 0;
20,532 ✔
1497
  size_t  dataEncodeBufSize = 0;
20,532 ✔
1498
  for(size_t i = 0; i < taosArrayGetSize(pBlockList); i++){
44,472 ✔
1499
    SSDataBlock* pBlock = taosArrayGetP(pBlockList, i);
23,940 ✔
1500
    if (pBlock == NULL || pBlock->info.rows == 0) continue;
23,940 ✔
1501
    int32_t blockSize = blockGetEncodeSize(pBlock);
23,940 ✔
1502
    dataEncodeBufSize += blockSize;
23,940 ✔
1503
    blockNum++;
23,940 ✔
1504
  }
1505
  buf = rpcMallocCont(INT_BYTES + dataEncodeBufSize);
20,532 ✔
1506
  STREAM_CHECK_NULL_GOTO(buf, terrno);
20,532 ✔
1507

1508
  char* dataBuf = (char*)buf;
20,532 ✔
1509
  *((int32_t*)(dataBuf)) = blockNum;
20,532 ✔
1510
  dataBuf += INT_BYTES;
20,532 ✔
1511
  for(size_t i = 0; i < taosArrayGetSize(pBlockList); i++){
44,472 ✔
1512
    SSDataBlock* pBlock = taosArrayGetP(pBlockList, i);
23,940 ✔
1513
    if (pBlock == NULL || pBlock->info.rows == 0) continue;
23,940 ✔
1514
    int32_t actualLen = blockEncode(pBlock, dataBuf, dataEncodeBufSize, taosArrayGetSize(pBlock->pDataBlock));
23,940 ✔
1515
    STREAM_CHECK_CONDITION_GOTO(actualLen < 0, terrno);
23,940 ✔
1516
    dataBuf += actualLen;
23,940 ✔
1517
  }
1518
  *data = buf;
20,532 ✔
1519
  *size = INT_BYTES + dataEncodeBufSize;
20,532 ✔
1520
  buf = NULL;
20,532 ✔
1521
end:
20,532 ✔
1522
  rpcFreeCont(buf);
20,532 ✔
1523
  return code;
20,532 ✔
1524
}
1525

1526
static int32_t buildWalMetaBlock(SSDataBlock* pBlock, int8_t type, int64_t id, bool isVTable, int64_t uid,
×
1527
                                 int64_t skey, int64_t ekey, int64_t ver, int64_t rows) {
1528
  int32_t code = 0;
×
1529
  int32_t lino = 0;
×
1530
  int32_t index = 0;
×
1531
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &type));
×
1532
  if (!isVTable) {
×
1533
    STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
×
1534
  }
1535
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &uid));
×
1536
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &skey));
×
1537
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ekey));
×
1538
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
×
1539
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &rows));
×
1540

1541
end:
×
1542
  // STREAM_PRINT_LOG_END(code, lino)
1543
  return code;
×
1544
}
1545

1546
static int32_t buildWalMetaBlockNew(SSDataBlock* pBlock, int64_t id, int64_t skey, int64_t ekey, int64_t ver,
564,269 ✔
1547
                                    int32_t rollupTbCount) {
1548
  int32_t code = 0;
564,269 ✔
1549
  int32_t lino = 0;
564,269 ✔
1550
  STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_GID_COL, &id));
564,269 ✔
1551
  STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_SKEY_COL, &skey));
564,086 ✔
1552
  STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_EKEY_COL, &ekey));
563,690 ✔
1553
  STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_VER_COL, &ver));
563,894 ✔
1554
  if (blockDataGetNumOfCols(pBlock) > STREAM_WAL_META_ROLLUP_TBCOUNT_COL) {
563,478 ✔
1555
    STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_ROLLUP_TBCOUNT_COL, &rollupTbCount));
9,107 ✔
1556
  }
1557

1558
end:
563,609 ✔
1559
  return code;
563,609 ✔
1560
}
1561

1562
static int32_t buildTableBlock(SSDataBlock* pBlock, int64_t id, int64_t ver, ETableBlockType type) {
660 ✔
1563
  int32_t code = 0;
660 ✔
1564
  int32_t lino = 0;
660 ✔
1565
  int32_t index = 0;
660 ✔
1566
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
660 ✔
1567
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
660 ✔
1568
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &type));
660 ✔
1569

1570
end:
660 ✔
1571
  return code;
660 ✔
1572
}
1573

1574
static void buildTSchema(STSchema* pTSchema, int32_t ver, col_id_t colId, int8_t type, int32_t bytes) {
×
1575
  pTSchema->numOfCols = 1;
×
1576
  pTSchema->version = ver;
×
1577
  pTSchema->columns[0].colId = colId;
×
1578
  pTSchema->columns[0].type = type;
×
1579
  pTSchema->columns[0].bytes = bytes;
×
1580
}
×
1581

1582
static int32_t scanDeleteDataNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len,
7,894 ✔
1583
                              int64_t ver) {
1584
  int32_t    code = 0;
7,894 ✔
1585
  int32_t    lino = 0;
7,894 ✔
1586
  SDecoder   decoder = {0};
7,894 ✔
1587
  SDeleteRes req = {0};
7,894 ✔
1588
  void* pTask = sStreamReaderInfo->pTask;
7,894 ✔
1589

1590
  req.uidList = taosArrayInit(0, sizeof(tb_uid_t));
7,894 ✔
1591
  tDecoderInit(&decoder, data, len);
7,960 ✔
1592
  STREAM_CHECK_RET_GOTO(tDecodeDeleteRes(&decoder, &req));
7,894 ✔
1593
  STREAM_CHECK_CONDITION_GOTO((sStreamReaderInfo->tableType == TSDB_SUPER_TABLE && !sStreamReaderInfo->isVtableStream && req.suid != sStreamReaderInfo->suid), TDB_CODE_SUCCESS);
8,026 ✔
1594
  
1595
  for (int32_t i = 0; i < taosArrayGetSize(req.uidList); i++) {
11,426 ✔
1596
    uint64_t* uid = taosArrayGet(req.uidList, i);
6,376 ✔
1597
    STREAM_CHECK_NULL_GOTO(uid, terrno);
6,442 ✔
1598
    uint64_t   id = 0;
6,442 ✔
1599
    ST_TASK_DLOG("stream reader scan delete start data:uid %" PRIu64 ", skey %" PRIu64 ", ekey %" PRIu64, *uid, req.skey, req.ekey);
6,442 ✔
1600
    STREAM_CHECK_CONDITION_GOTO(!uidInTableListSet(sStreamReaderInfo, req.suid, *uid, &id, false), TDB_CODE_SUCCESS);
6,442 ✔
1601
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->deleteBlock, ((SSDataBlock*)rsp->deleteBlock)->info.rows + 1));
4,918 ✔
1602
    STREAM_CHECK_RET_GOTO(buildWalMetaBlockNew(rsp->deleteBlock, id, req.skey, req.ekey, ver, 0));
4,918 ✔
1603
    ((SSDataBlock*)rsp->deleteBlock)->info.rows++;
4,918 ✔
1604
    rsp->totalRows++;
4,918 ✔
1605
  }
1606

1607
end:
8,026 ✔
1608
  taosArrayDestroy(req.uidList);
8,026 ✔
1609
  tDecoderClear(&decoder);
8,026 ✔
1610
  return code;
8,026 ✔
1611
}
1612

1613
static int32_t createBlockForProcessMeta(SSDataBlock** pBlock) {
462 ✔
1614
  int32_t code = 0;
462 ✔
1615
  int32_t lino = 0;
462 ✔
1616
  SArray* schemas = NULL;
462 ✔
1617

1618
  schemas = taosArrayInit(8, sizeof(SSchema));
462 ✔
1619
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
462 ✔
1620

1621
  int32_t index = 0;
462 ✔
1622
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // gid non vtable/uid vtable
462 ✔
1623
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // ver
462 ✔
1624
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_TINYINT, CHAR_BYTES, index++))  // type
462 ✔
1625

1626
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
462 ✔
1627

1628
end:
462 ✔
1629
  taosArrayDestroy(schemas);
462 ✔
1630
  return code;
462 ✔
1631
}
1632

1633
static int32_t addOneRow(void** tmp, int64_t id, int64_t ver, ETableBlockType type) {
660 ✔
1634
  int32_t  code = 0;
660 ✔
1635
  int32_t  lino = 0;
660 ✔
1636
  if (*tmp == NULL) {
660 ✔
1637
    STREAM_CHECK_RET_GOTO(createBlockForProcessMeta((SSDataBlock**)tmp));
462 ✔
1638
  }
1639
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(*tmp, ((SSDataBlock*)(*tmp))->info.rows + 1));
660 ✔
1640
  STREAM_CHECK_RET_GOTO(buildTableBlock(*tmp, id, ver, type));
660 ✔
1641
  ((SSDataBlock*)(*tmp))->info.rows++;
660 ✔
1642
  
1643
end:
660 ✔
1644
  return code;
660 ✔
1645
}
1646

1647
int32_t addUidListToBlock(SArray* uidListAdd, void** block, int64_t ver, int32_t* totalRows, ETableBlockType type) {
10,302 ✔
1648
  for (int32_t i = 0; i < taosArrayGetSize(uidListAdd); ++i) {
10,962 ✔
1649
    uint64_t* uid = taosArrayGet(uidListAdd, i);
660 ✔
1650
    if (uid == NULL) {
660 ✔
1651
      continue;
×
1652
    }
1653
    int32_t code = addOneRow(block, *uid, ver, type);
660 ✔
1654
    if (code != 0) {
660 ✔
1655
      return code;
×
1656
    }
1657
    (*totalRows)++;
660 ✔
1658
  }
1659
  return 0;
10,302 ✔
1660
}
1661

1662
static int32_t qStreamGetAddTable(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* tableListAdd, SArray* uidListAdd) {
25,845 ✔
1663
  int32_t      code = 0;
25,845 ✔
1664
  int32_t      lino = 0;
25,845 ✔
1665
  if (uidListAdd == NULL) {
25,845 ✔
1666
    return 0;
23,598 ✔
1667
  }
1668
  void* pTask = sStreamReaderInfo->pTask;
2,247 ✔
1669
  
1670
  taosRLockLatch(&sStreamReaderInfo->lock);
2,247 ✔
1671
  int32_t totalSize = taosArrayGetSize(tableListAdd);
2,247 ✔
1672
  for (int32_t i = 0; i < totalSize; ++i) {
2,445 ✔
1673
    STableKeyInfo* info = taosArrayGet(tableListAdd, i);
198 ✔
1674
    if (info == NULL) {
198 ✔
1675
      continue;
×
1676
    }
1677
    if (taosHashGet(sStreamReaderInfo->tableList.uIdMap, &info->uid, LONG_BYTES) != NULL) {
198 ✔
1678
      continue;
×
1679
    }
1680
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidListAdd, &info->uid), terrno);
396 ✔
1681
    ST_TASK_WLOG("%s real add table to list for uid:%" PRId64, __func__, info->uid);
198 ✔
1682
  }
1683

1684
end:
2,247 ✔
1685
  taosRUnLockLatch(&sStreamReaderInfo->lock);
2,247 ✔
1686
  return code;
2,247 ✔
1687
}
1688

1689
static int32_t qStreamGetDelTable(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* tableListDel, SArray* uidListDel) {
9,657 ✔
1690
  int32_t      code = 0;
9,657 ✔
1691
  int32_t      lino = 0;
9,657 ✔
1692
  if (uidListDel == NULL) {
9,657 ✔
1693
    return 0;
×
1694
  }
1695
  void* pTask = sStreamReaderInfo->pTask;
9,657 ✔
1696
  
1697
  taosRLockLatch(&sStreamReaderInfo->lock);
9,657 ✔
1698
  int32_t totalSize = taosArrayGetSize(tableListDel);
9,657 ✔
1699
  for (int32_t i = 0; i < totalSize; ++i) {
9,855 ✔
1700
    int64_t* uid = taosArrayGet(tableListDel, i);
198 ✔
1701
    if (uid == NULL) {
198 ✔
1702
      continue;
×
1703
    }
1704
    
1705
    if (taosHashGet(sStreamReaderInfo->tableList.uIdMap, uid, LONG_BYTES) == NULL) {
198 ✔
1706
      continue;
×
1707
    }
1708
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidListDel, uid), terrno);
198 ✔
1709
    ST_TASK_WLOG("%s real del table from list for uid:%" PRId64, __func__, *uid);
198 ✔
1710
  }
1711

1712
end:
9,657 ✔
1713
  taosRUnLockLatch(&sStreamReaderInfo->lock);
9,657 ✔
1714
  return code;
9,657 ✔
1715
}
1716

1717
static int32_t scanDropTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len,
3,630 ✔
1718
                             int64_t ver) {
1719
  int32_t  code = 0;
3,630 ✔
1720
  int32_t  lino = 0;
3,630 ✔
1721
  SDecoder decoder = {0};
3,630 ✔
1722
  void* pTask = sStreamReaderInfo->pTask;
3,630 ✔
1723
  SArray* uidList = NULL;
3,630 ✔
1724
  SArray* uidListDel = NULL;
3,630 ✔
1725
  SArray* uidListDelOutTbl = NULL;
3,630 ✔
1726
  SVDropTbBatchReq req = {0};
3,630 ✔
1727
  tDecoderInit(&decoder, data, len);
3,630 ✔
1728
  STREAM_CHECK_RET_GOTO(tDecodeSVDropTbBatchReq(&decoder, &req));
3,630 ✔
1729

1730
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
7,260 ✔
1731
    SVDropTbReq* pDropTbReq = req.pReqs + iReq;
3,630 ✔
1732
    STREAM_CHECK_NULL_GOTO(pDropTbReq, TSDB_CODE_INVALID_PARA);
3,630 ✔
1733
    uint64_t id = 0;
3,630 ✔
1734
    if(!uidInTableListOrigin(sStreamReaderInfo, pDropTbReq->suid, pDropTbReq->uid, &id)) {
3,630 ✔
1735
      continue;
3,432 ✔
1736
    }
1737

1738
    if (sStreamReaderInfo->deleteOutTbl != 0) {
198 ✔
1739
      if (uidListDelOutTbl == NULL) {
×
1740
        uidListDelOutTbl = taosArrayInit(8, sizeof(tb_uid_t));
×
1741
        STREAM_CHECK_NULL_GOTO(uidListDelOutTbl, terrno);
×
1742
      }
1743
      STREAM_CHECK_NULL_GOTO(taosArrayPush(uidListDelOutTbl, &pDropTbReq->uid), terrno);
×
1744
    }
1745
    if (sStreamReaderInfo->isVtableStream) {
198 ✔
1746
      if (uidList == NULL) {
198 ✔
1747
        uidList = taosArrayInit(8, sizeof(tb_uid_t));
198 ✔
1748
        STREAM_CHECK_NULL_GOTO(uidList, terrno);
198 ✔
1749
      }
1750
      STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pDropTbReq->uid), terrno);
396 ✔
1751
    }
1752

1753
    ST_TASK_DLOG("stream reader scan drop uid %" PRId64 ", id %" PRIu64, pDropTbReq->uid, id);
198 ✔
1754
  }
1755
  STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListDelOutTbl, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_DROP));
3,630 ✔
1756

1757
  if (sStreamReaderInfo->isVtableStream) {
3,630 ✔
1758
    uidListDel = taosArrayInit(8, sizeof(tb_uid_t));
3,630 ✔
1759
    STREAM_CHECK_NULL_GOTO(uidListDel, terrno);
3,630 ✔
1760
    STREAM_CHECK_RET_GOTO(qStreamGetDelTable(sStreamReaderInfo, uidList, uidListDel));
3,630 ✔
1761
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListDel, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_RETIRE));
3,630 ✔
1762
  }
1763

1764
end:
3,630 ✔
1765
  taosArrayDestroy(uidList);
3,630 ✔
1766
  taosArrayDestroy(uidListDel);
3,630 ✔
1767
  taosArrayDestroy(uidListDelOutTbl);
3,630 ✔
1768
  tDecoderClear(&decoder);
3,630 ✔
1769
  return code;
3,630 ✔
1770
}
1771

1772
static int32_t qStreamModifyTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* tableListAdd, SArray* tableListDel) {
26,271 ✔
1773
  int32_t    code = 0;
26,271 ✔
1774
  int32_t    lino = 0;
26,271 ✔
1775
  void*      pTask = sStreamReaderInfo->pTask;
26,271 ✔
1776
  SSHashObj* removedUidMap = NULL;
26,271 ✔
1777

1778
  if (isRollupMultiReader(sStreamReaderInfo)) {
26,271 ✔
1779
    removedUidMap = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
×
1780
    if (removedUidMap == NULL) {
×
1781
      return terrno;
×
1782
    }
1783
  }
1784

1785
  taosWLockLatch(&sStreamReaderInfo->lock);
26,271 ✔
1786
  int32_t totalSize = tableListDel == NULL ? 0 : taosArrayGetSize(tableListDel);
26,271 ✔
1787
  for (int32_t i = 0; i < totalSize; ++i) {
26,271 ✔
1788
    int64_t* uid = taosArrayGet(tableListDel, i);
×
1789
    if (uid == NULL) {
×
1790
      continue;
×
1791
    }
1792
    STREAM_CHECK_RET_GOTO(qStreamRemoveTableList(&sStreamReaderInfo->tableList, *uid));
×
1793
  }
1794

1795
  totalSize = tableListAdd == NULL ? 0 : taosArrayGetSize(tableListAdd);
26,271 ✔
1796
  for (int32_t i = 0; i < totalSize; ++i) {
34,415 ✔
1797
    STableKeyInfo* info = taosArrayGet(tableListAdd, i);
8,144 ✔
1798
    if (info == NULL) {
8,144 ✔
1799
      continue;
×
1800
    }
1801
    int ret = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheTrigger,
8,144 ✔
1802
                       sStreamReaderInfo->pExprInfoTriggerTag, sStreamReaderInfo->numOfExprTriggerTag,
1803
                       &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
1804
    if (ret != 0) {
8,144 ✔
1805
      ST_TASK_WLOG("%s cacheTag trigger failed for uid:%" PRId64 ",code:%d", __func__, info->uid, ret);
1,356 ✔
1806
      continue;
1,356 ✔
1807
    }
1808
    ret =
×
1809
        cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheCalc, sStreamReaderInfo->pExprInfoCalcTag,
6,788 ✔
1810
                 sStreamReaderInfo->numOfExprCalcTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
1811
    if (ret != 0) {
6,788 ✔
1812
      ST_TASK_WLOG("%s cacheTag calc failed for uid:%" PRId64 ",code:%d", __func__, info->uid, ret);
×
1813
      continue;
×
1814
    }
1815
    if (removedUidMap == NULL) {
6,788 ✔
1816
      STREAM_CHECK_RET_GOTO(qStreamRemoveTableList(&sStreamReaderInfo->tableList, info->uid));
6,788 ✔
1817
    } else if (tSimpleHashGet(removedUidMap, &info->uid, LONG_BYTES) == NULL) {
×
1818
      STREAM_CHECK_RET_GOTO(qStreamRemoveTableList(&sStreamReaderInfo->tableList, info->uid));
×
1819
      STREAM_CHECK_RET_GOTO(tSimpleHashPut(removedUidMap, &info->uid, LONG_BYTES, &info->uid, LONG_BYTES));
×
1820
    }
1821
    STREAM_CHECK_RET_GOTO(qStreamSetTableList(&sStreamReaderInfo->tableList, info->uid, info->groupId));
6,788 ✔
1822
  }
1823

1824
end:
26,271 ✔
1825
  taosWUnLockLatch(&sStreamReaderInfo->lock);
26,271 ✔
1826
  tSimpleHashCleanup(removedUidMap);
26,271 ✔
1827
  return code;
26,271 ✔
1828
}
1829

1830
static int32_t processTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* uidList, SArray** tableList) {
26,271 ✔
1831
  int32_t code = 0;
26,271 ✔
1832
  int32_t lino = 0;
26,271 ✔
1833
  SNodeList* groupNew = NULL;
26,271 ✔
1834
  SArray*    sourceTableList = NULL;
26,271 ✔
1835

1836
  if (taosArrayGetSize(uidList) == 0) {
26,271 ✔
1837
    return 0;
18,127 ✔
1838
  }
1839

1840
  if (!sStreamReaderInfo->isRollupReader) {
8,144 ✔
1841
    STREAM_CHECK_RET_GOTO(nodesCloneList(sStreamReaderInfo->partitionCols, &groupNew));
8,144 ✔
1842
  }
1843
  STREAM_CHECK_RET_GOTO(qStreamFilterTableListForReader(
8,144 ✔
1844
      sStreamReaderInfo->pVnode, uidList, groupNew, sStreamReaderInfo->pTagCond, sStreamReaderInfo->pTagIndexCond,
1845
      &sStreamReaderInfo->storageApi, sStreamReaderInfo->isRollupReader ? NULL : sStreamReaderInfo->groupIdMap,
1846
      sStreamReaderInfo->suid, sStreamReaderInfo->isRollupReader ? &sourceTableList : tableList));
×
1847
  if (sStreamReaderInfo->isRollupReader) {
8,144 ✔
1848
    STREAM_CHECK_RET_GOTO(buildRollupTableListFromSource(sStreamReaderInfo, sourceTableList, tableList));
×
1849
  }
1850

1851
end:
8,144 ✔
1852
  nodesDestroyList(groupNew);
8,144 ✔
1853
  taosArrayDestroy(sourceTableList);
8,144 ✔
1854
  return code;
8,144 ✔
1855
}
1856

1857
static int32_t scanCreateTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len,
19,818 ✔
1858
                             int64_t ver) {
1859
  int32_t  code = 0;
19,818 ✔
1860
  int32_t  lino = 0;
19,818 ✔
1861
  SDecoder decoder = {0};
19,818 ✔
1862
  SArray*  uidList = NULL;
19,818 ✔
1863
  SArray*  tableList = NULL;
19,818 ✔
1864
  SArray*  uidListAdd = NULL;
19,818 ✔
1865
  void* pTask = sStreamReaderInfo->pTask;
19,818 ✔
1866

1867
  SVCreateTbBatchReq req = {0};
19,818 ✔
1868
  tDecoderInit(&decoder, data, len);
19,818 ✔
1869
  
1870
  STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbBatchReq(&decoder, &req));
19,818 ✔
1871

1872
  uidList = taosArrayInit(8, sizeof(tb_uid_t));
19,762 ✔
1873
  STREAM_CHECK_NULL_GOTO(uidList, terrno);
19,818 ✔
1874

1875
  if (sStreamReaderInfo->isVtableStream) {
19,818 ✔
1876
    uidListAdd = taosArrayInit(8, sizeof(tb_uid_t));
1,716 ✔
1877
    STREAM_CHECK_NULL_GOTO(uidListAdd, terrno);
1,716 ✔
1878
  }
1879

1880
  SVCreateTbReq* pCreateReq = NULL;
19,818 ✔
1881
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
39,636 ✔
1882
    pCreateReq = req.pReqs + iReq;
19,762 ✔
1883
    if (!needReLoadTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false)) {
19,762 ✔
1884
      ST_TASK_DLOG("stream reader scan create table jump, %s", pCreateReq->name);
17,474 ✔
1885
      continue;
17,530 ✔
1886
    }
1887
    ST_TASK_ILOG("stream reader scan create table %s", pCreateReq->name);
2,288 ✔
1888
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pCreateReq->uid), terrno);
4,576 ✔
1889
  }
1890
  
1891
  STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo, uidList, &tableList));
19,874 ✔
1892
  STREAM_CHECK_RET_GOTO(qStreamGetAddTable(sStreamReaderInfo, tableList, uidListAdd));
19,818 ✔
1893
  if (sStreamReaderInfo->isVtableStream) {
19,818 ✔
1894
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
1,716 ✔
1895
  }
1896

1897
  STREAM_CHECK_RET_GOTO(qStreamModifyTableList(sStreamReaderInfo, tableList, uidList));
19,818 ✔
1898
end:
19,818 ✔
1899
  taosArrayDestroy(uidList);
19,818 ✔
1900
  taosArrayDestroy(uidListAdd);
19,818 ✔
1901
  taosArrayDestroy(tableList);
19,818 ✔
1902
  tDeleteSVCreateTbBatchReq(&req);
19,818 ✔
1903
  tDecoderClear(&decoder);
19,818 ✔
1904
  return code;
19,818 ✔
1905
}
1906

1907
static int32_t processAutoCreateTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SVCreateTbReq* pCreateReq, int64_t ver) {
312,711 ✔
1908
  int32_t  code = 0;
312,711 ✔
1909
  int32_t  lino = 0;
312,711 ✔
1910
  void*    pTask = sStreamReaderInfo->pTask;
312,711 ✔
1911
  SArray*  uidList = NULL;
312,711 ✔
1912
  SArray*  tableList = NULL;
312,711 ✔
1913

1914
  ST_TASK_DLOG("%s start, name:%s uid:%"PRId64, __func__, pCreateReq->name, pCreateReq->uid);
312,761 ✔
1915
  if (!needReLoadTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false) ||
313,479 ✔
1916
      ignoreMetaChange(sStreamReaderInfo->tableList.version, ver)) {
588 ✔
1917
    ST_TASK_DLOG("stream reader scan auto create table jump, %s", pCreateReq->name);
312,600 ✔
1918
    goto end;
312,534 ✔
1919
  }
1920
  uidList = taosArrayInit(8, sizeof(tb_uid_t));
426 ✔
1921
  STREAM_CHECK_NULL_GOTO(uidList, terrno);
426 ✔
1922
  STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pCreateReq->uid), terrno);
852 ✔
1923
  ST_TASK_DLOG("stream reader scan auto create table %s", pCreateReq->name);
426 ✔
1924

1925
  STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo, uidList, &tableList));
426 ✔
1926
  STREAM_CHECK_RET_GOTO(qStreamModifyTableList(sStreamReaderInfo, tableList, uidList));
426 ✔
1927
end:
312,495 ✔
1928
  taosArrayDestroy(uidList);
313,026 ✔
1929
  taosArrayDestroy(tableList);
312,891 ✔
1930
  return code;
312,960 ✔
1931
}
1932

1933
static bool isColIdInList(SNodeList* colList, col_id_t cid){
66 ✔
1934
  int32_t  code = 0;
66 ✔
1935
  int32_t  lino = 0;
66 ✔
1936
  SNode*  nodeItem = NULL;
66 ✔
1937
  FOREACH(nodeItem, colList) {
132 ✔
1938
    SNode*           pNode = ((STargetNode*)nodeItem)->pExpr;
132 ✔
1939
    if (nodeType(pNode) == QUERY_NODE_COLUMN) {
132 ✔
1940
      SColumnNode*     valueNode = (SColumnNode*)(pNode);
132 ✔
1941
      if (cid == valueNode->colId) {
132 ✔
1942
        return true;
66 ✔
1943
      }
1944
    }
1945
  }
1946
end:
×
1947
  return false;
×
1948
}
1949

1950
static bool isAlteredTable(int8_t action, ETableType tbType) {
6,093 ✔
1951
  if (action == TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL && tbType == TSDB_CHILD_TABLE) {
6,093 ✔
1952
    return true;
6,027 ✔
1953
  } else if (action == TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL && tbType == TSDB_SUPER_TABLE) {
66 ✔
1954
    return true;
×
1955
  } else if ((action == TSDB_ALTER_TABLE_ALTER_COLUMN_REF || action == TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) && 
66 ✔
1956
     (tbType == TSDB_VIRTUAL_CHILD_TABLE || tbType == TSDB_VIRTUAL_NORMAL_TABLE)) {
66 ✔
1957
    return true;
66 ✔
1958
  }
1959
  return false;
×
1960
}
1961

1962
void getAlterColId(void* pVnode, int64_t uid, const char* colName, col_id_t* colId) {
66 ✔
1963
  SSchemaWrapper *pSchema = metaGetTableSchema(((SVnode *)pVnode)->pMeta, uid, -1, 1, NULL, 0, false);
66 ✔
1964
  if (pSchema == NULL) {
66 ✔
1965
    return;
×
1966
  }
1967
  for (int32_t i = 0; i < pSchema->nCols; i++) {
132 ✔
1968
    if (strncmp(pSchema->pSchema[i].name, colName, TSDB_COL_NAME_LEN) == 0) {
132 ✔
1969
      *colId = pSchema->pSchema[i].colId;
66 ✔
1970
      break;
66 ✔
1971
    }
1972
  }
1973
  tDeleteSchemaWrapper(pSchema);
×
1974
  return;
66 ✔
1975
}
1976

1977
// Handle TSDB_ALTER_TABLE_ALTER_COLUMN_REF and TSDB_ALTER_TABLE_REMOVE_COLUMN_REF
1978
static int32_t scanAlterTableColumnRef(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, 
66 ✔
1979
                                       SVAlterTbReq* pReq, uint64_t uid, int64_t ver) {
1980
  int32_t code = 0;
66 ✔
1981
  int32_t lino = 0;
66 ✔
1982
  void* pTask = sStreamReaderInfo->pTask;
66 ✔
1983
  SArray* uidListAdd = NULL;
66 ✔
1984

1985
  uidListAdd = taosArrayInit(8, sizeof(tb_uid_t));
66 ✔
1986
  STREAM_CHECK_NULL_GOTO(uidListAdd, terrno);
66 ✔
1987

1988
  uint64_t id = 0;
66 ✔
1989
  STREAM_CHECK_CONDITION_GOTO(!uidInTableListOrigin(sStreamReaderInfo, sStreamReaderInfo->suid, uid, &id), TDB_CODE_SUCCESS);
66 ✔
1990

1991
  col_id_t colId = 0;
66 ✔
1992
  getAlterColId(sStreamReaderInfo->pVnode, uid, pReq->colName, &colId);
66 ✔
1993
  if (atomic_load_8(&sStreamReaderInfo->isVtableOnlyTs) == 0 && !isColIdInList(sStreamReaderInfo->triggerCols, colId)) {
66 ✔
1994
    ST_TASK_ILOG("stream reader scan alter table %s, colId %d not in trigger cols", pReq->tbName, colId);
×
1995
    goto end;
×
1996
  }
1997

1998
  STREAM_CHECK_NULL_GOTO(taosArrayPush(uidListAdd, &uid), terrno);
66 ✔
1999
  STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
66 ✔
2000

2001
  ST_TASK_DLOG("stream reader scan alter table column ref %s", pReq->tbName);
66 ✔
2002

2003
end:
66 ✔
2004
  taosArrayDestroy(uidListAdd);
66 ✔
2005
  STREAM_PRINT_LOG_END_WITHID(code, lino);
66 ✔
2006
  return code;
66 ✔
2007
}
2008

2009
static int32_t checkAlter(SStreamTriggerReaderInfo* sStreamReaderInfo, char* tbName, int8_t action, uint64_t *uid) {
6,093 ✔
2010
  int32_t  code = 0;
6,093 ✔
2011
  int32_t  lino = 0;
6,093 ✔
2012
  ETableType tbType = 0;
6,093 ✔
2013
  uint64_t suid = 0;
6,093 ✔
2014

2015
  STREAM_CHECK_RET_GOTO(metaGetTableTypeSuidByName(sStreamReaderInfo->pVnode, tbName, &tbType, &suid, 0));
6,093 ✔
2016
  STREAM_CHECK_CONDITION_GOTO(!isAlteredTable(action, tbType), TDB_CODE_SUCCESS);
6,093 ✔
2017
  STREAM_CHECK_CONDITION_GOTO(suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
6,093 ✔
2018
  if (action == TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL) {
5,496 ✔
2019
    *uid = suid;
×
2020
    goto end;
×
2021
  }
2022
  STREAM_CHECK_RET_GOTO(metaGetTableUidByName(sStreamReaderInfo->pVnode, tbName, uid, 0));
5,496 ✔
2023

2024
end:
6,093 ✔
2025
  return code;
6,093 ✔
2026
}
2027

2028
static SArray* getTableListForAlterSuperTable(SStreamTriggerReaderInfo* sStreamReaderInfo, SVAlterTbReq* pReq){
6,027 ✔
2029
  int32_t code = 0;
6,027 ✔
2030
  int32_t lino = 0;
6,027 ✔
2031
  void* pTask = sStreamReaderInfo->pTask;
6,027 ✔
2032
  SArray* uidList = taosArrayInit(8, sizeof(tb_uid_t));
6,027 ✔
2033
  STREAM_CHECK_NULL_GOTO(uidList, terrno);
6,027 ✔
2034
  for (int32_t i = 0; i < taosArrayGetSize(pReq->tables); i++) {
12,054 ✔
2035
    SUpdateTableTagVal *pTable = taosArrayGet(pReq->tables, i);
6,027 ✔
2036
    uint64_t uid = 0;
6,027 ✔
2037
    code = checkAlter(sStreamReaderInfo, pTable->tbName, pReq->action, &uid);
6,027 ✔
2038
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || uid == 0) {
6,027 ✔
2039
      code = 0;
597 ✔
2040
      ST_TASK_WLOG("stream reader scan alter ctable table %s not exist, %s %"PRIu64, pTable->tbName, __func__, uid);
597 ✔
2041
      continue;
597 ✔
2042
    }
2043
    STREAM_CHECK_RET_GOTO(code);
5,430 ✔
2044
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, (const void *)&uid), terrno);
5,430 ✔
2045
  }
2046

2047
end:
6,027 ✔
2048
  if (code != 0) {
6,027 ✔
2049
    ST_TASK_ELOG("%s failed,code:%d", __func__, code);
×
2050
    taosArrayDestroy(uidList);
×
2051
    uidList = NULL;
×
2052
  }
2053
  return uidList;
6,027 ✔
2054
}
2055

2056
// Handle TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL and TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL
2057
static int32_t scanAlterTableTagVal(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, 
6,027 ✔
2058
                                    SArray* uidList, int64_t ver) {
2059
  int32_t code = 0;
6,027 ✔
2060
  int32_t lino = 0;
6,027 ✔
2061
  void* pTask = sStreamReaderInfo->pTask;
6,027 ✔
2062
  SArray* uidListAdd = NULL;
6,027 ✔
2063
  SArray* uidListDel = NULL;
6,027 ✔
2064
  SArray* tableList = NULL;
6,027 ✔
2065

2066
  if (sStreamReaderInfo->isVtableStream) {
6,027 ✔
2067
    uidListAdd = taosArrayInit(8, sizeof(tb_uid_t));
531 ✔
2068
    STREAM_CHECK_NULL_GOTO(uidListAdd, terrno);
531 ✔
2069
  }
2070

2071
  uidListDel = taosArrayInit(8, sizeof(tb_uid_t));
6,027 ✔
2072
  STREAM_CHECK_NULL_GOTO(uidListDel, terrno);
5,976 ✔
2073

2074
  STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo, uidList, &tableList));
5,976 ✔
2075
  STREAM_CHECK_RET_GOTO(qStreamGetDelTable(sStreamReaderInfo, uidList, uidListDel));
6,027 ✔
2076

2077
  if (rsp->checkAlter && taosArrayGetSize(uidListDel) > 0 && rsp->totalDataRows > 0) {
6,027 ✔
2078
    rsp->needReturn = true;
×
2079
    rsp->ver--;
×
2080
    ST_TASK_DLOG("%s stream reader scan alter table need return data", __func__);
×
2081
    goto end;
×
2082
  }
2083

2084
  STREAM_CHECK_RET_GOTO(qStreamGetAddTable(sStreamReaderInfo, tableList, uidListAdd));
6,027 ✔
2085
  if (sStreamReaderInfo->isVtableStream) {
6,027 ✔
2086
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
531 ✔
2087
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListDel, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_RETIRE));
531 ✔
2088
  }
2089
  STREAM_CHECK_RET_GOTO(qStreamModifyTableList(sStreamReaderInfo, tableList, uidList));
6,027 ✔
2090

2091
  ST_TASK_DLOG("%s stream reader scan alter table tag val", __func__);
6,027 ✔
2092

2093
end:
6,027 ✔
2094
  taosArrayDestroy(uidListAdd);
6,027 ✔
2095
  taosArrayDestroy(uidListDel);
6,027 ✔
2096
  taosArrayDestroy(tableList);
6,027 ✔
2097
  STREAM_PRINT_LOG_END_WITHID(code, lino);
6,027 ✔
2098
  return code;
6,027 ✔
2099
}
2100

2101
static int32_t scanAlterTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len, int64_t ver) {
10,200 ✔
2102
  int32_t  code = 0;
10,200 ✔
2103
  int32_t  lino = 0;
10,200 ✔
2104
  SDecoder decoder = {0};
10,200 ✔
2105
  void* pTask = sStreamReaderInfo->pTask;
10,200 ✔
2106
  SArray* uidList = NULL;
10,200 ✔
2107

2108
  ST_TASK_DLOG("%s start", __func__);
10,200 ✔
2109

2110
  SVAlterTbReq req = {0};
10,200 ✔
2111
  tDecoderInit(&decoder, data, len);
10,251 ✔
2112
  
2113
  STREAM_CHECK_RET_GOTO(tDecodeSVAlterTbReq(&decoder, &req));
10,251 ✔
2114

2115
  STREAM_CHECK_CONDITION_GOTO(req.action != TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL && req.action != TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL && 
10,251 ✔
2116
    req.action != TSDB_ALTER_TABLE_ALTER_COLUMN_REF && req.action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF, TDB_CODE_SUCCESS);
×
2117

2118
  uint64_t uid = 0;
7,479 ✔
2119
  if (req.action == TSDB_ALTER_TABLE_ALTER_COLUMN_REF || req.action == TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
7,479 ✔
2120
    STREAM_CHECK_CONDITION_GOTO(!sStreamReaderInfo->isVtableStream, TDB_CODE_SUCCESS);
1,452 ✔
2121
    code = checkAlter(sStreamReaderInfo, req.tbName, req.action, &uid);
66 ✔
2122
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || uid == 0) {
66 ✔
2123
      ST_TASK_WLOG("stream reader scan alter ref table %s not exist, %s uid:%" PRIu64, req.tbName, __func__, uid);
×
2124
      code = 0;
×
2125
      goto end;
×
2126
    }
2127
    STREAM_CHECK_RET_GOTO(scanAlterTableColumnRef(sStreamReaderInfo, rsp, &req, uid, ver));
66 ✔
2128
  } else if (req.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL) {
6,027 ✔
2129
    uidList = getTableListForAlterSuperTable(sStreamReaderInfo, &req);
6,027 ✔
2130
    STREAM_CHECK_NULL_GOTO(uidList, terrno);
6,027 ✔
2131
    STREAM_CHECK_RET_GOTO(scanAlterTableTagVal(sStreamReaderInfo, rsp, uidList, ver));
6,027 ✔
2132
  } else if (req.action == TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL) {
×
2133
    code = checkAlter(sStreamReaderInfo, req.tbName, req.action, &uid);
×
2134
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || uid == 0) {
×
2135
      ST_TASK_WLOG("stream reader scan alter suid table %s not exist, %s uid:%" PRIu64, req.tbName, __func__, uid);
×
2136
      code = 0;
×
2137
      goto end;
×
2138
    }
2139
    uidList = taosArrayInit(8, sizeof(uint64_t));
×
2140
    STREAM_CHECK_NULL_GOTO(uidList, terrno);
×
2141
    STREAM_CHECK_RET_GOTO(vnodeGetCtbIdList(sStreamReaderInfo->pVnode, uid, uidList));
×
2142
    STREAM_CHECK_RET_GOTO(scanAlterTableTagVal(sStreamReaderInfo, rsp, uidList, ver));
×
2143
  }
2144

2145
  ST_TASK_DLOG("%s stream reader scan alter table", __func__);
6,093 ✔
2146

2147
end:
10,251 ✔
2148
  destroyAlterTbReq(&req);
10,251 ✔
2149

2150
  taosArrayDestroy(uidList);
10,251 ✔
2151
  tDecoderClear(&decoder);
10,251 ✔
2152
  STREAM_PRINT_LOG_END_WITHID(code, lino);
10,251 ✔
2153
  return code;
10,251 ✔
2154
}
2155

2156
// static int32_t scanAlterSTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len) {
2157
//   int32_t  code = 0;
2158
//   int32_t  lino = 0;
2159
//   SDecoder decoder = {0};
2160
//   SMAlterStbReq reqAlter = {0};
2161
//   SVCreateStbReq req = {0};
2162
//   tDecoderInit(&decoder, data, len);
2163
//   void* pTask = sStreamReaderInfo->pTask;
2164
  
2165
//   STREAM_CHECK_RET_GOTO(tDecodeSVCreateStbReq(&decoder, &req));
2166
//   STREAM_CHECK_CONDITION_GOTO(req.suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
2167
//   if (req.alterOriData != 0) {
2168
//     STREAM_CHECK_RET_GOTO(tDeserializeSMAlterStbReq(req.alterOriData, req.alterOriDataLen, &reqAlter));
2169
//     STREAM_CHECK_CONDITION_GOTO(reqAlter.alterType != TSDB_ALTER_TABLE_DROP_TAG && reqAlter.alterType != TSDB_ALTER_TABLE_UPDATE_TAG_NAME, TDB_CODE_SUCCESS);
2170
//   }
2171
  
2172
//   STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo));
2173

2174
//   ST_TASK_DLOG("stream reader scan alter suid %" PRId64, req.suid);
2175
// end:
2176
//   tFreeSMAltertbReq(&reqAlter);
2177
//   tDecoderClear(&decoder);
2178
//   return code;
2179
// }
2180

2181
// static int32_t scanDropSTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len) {
2182
//   int32_t  code = 0;
2183
//   int32_t  lino = 0;
2184
//   SDecoder decoder = {0};
2185
//   void* pTask = sStreamReaderInfo->pTask;
2186

2187
//   SVDropStbReq req = {0};
2188
//   tDecoderInit(&decoder, data, len);
2189
//   STREAM_CHECK_RET_GOTO(tDecodeSVDropStbReq(&decoder, &req));
2190
//   STREAM_CHECK_CONDITION_GOTO(req.suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
2191

2192
//   ST_TASK_DLOG("stream reader scan drop suid %" PRId64, req.suid);
2193
// end:
2194
//   tDecoderClear(&decoder);
2195
//   return code;
2196
// }
2197

2198
static int32_t scanSubmitTbDataForMeta(SDecoder *pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo, SSHashObj* gidHash, int64_t ver, STimeWindow win) {
1,120,023 ✔
2199
  int32_t code = 0;
1,120,023 ✔
2200
  int32_t lino = 0;
1,120,023 ✔
2201
  void* pTask = sStreamReaderInfo->pTask;
1,120,023 ✔
2202
  WalMetaResult walMeta = {0};
1,120,023 ✔
2203
  SSubmitTbData submitTbData = {0};
1,120,395 ✔
2204
  bool          expandRollupGids = isRollupMultiReader(sStreamReaderInfo);
1,120,329 ✔
2205

2206
  if (tStartDecode(pCoder) < 0) {
1,119,825 ✔
2207
    code = TSDB_CODE_INVALID_MSG;
×
2208
    TSDB_CHECK_CODE(code, lino, end);
×
2209
  }
2210

2211
  uint8_t       version = 0;
1,121,563 ✔
2212
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
1,120,023 ✔
2213
    code = TSDB_CODE_INVALID_MSG;
×
2214
    TSDB_CHECK_CODE(code, lino, end);
×
2215
  }
2216
  version = (submitTbData.flags >> 8) & 0xff;
1,120,023 ✔
2217
  submitTbData.flags = submitTbData.flags & 0xff;
1,120,023 ✔
2218

2219
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
2220
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
1,120,023 ✔
2221
    submitTbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
75,472 ✔
2222
    STREAM_CHECK_NULL_GOTO(submitTbData.pCreateTbReq, terrno);
75,406 ✔
2223
    STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbReq(pCoder, submitTbData.pCreateTbReq));
75,406 ✔
2224
    STREAM_CHECK_RET_GOTO(processAutoCreateTableNew(sStreamReaderInfo, submitTbData.pCreateTbReq, ver));
75,353 ✔
2225
  }
2226

2227
  // submit data
2228
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
1,119,904 ✔
2229
    code = TSDB_CODE_INVALID_MSG;
×
2230
    TSDB_CHECK_CODE(code, lino, end);
×
2231
  }
2232
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
1,119,854 ✔
2233
    code = TSDB_CODE_INVALID_MSG;
×
2234
    TSDB_CHECK_CODE(code, lino, end);
×
2235
  }
2236

2237
  if (!uidInTableListSet(sStreamReaderInfo, submitTbData.suid, submitTbData.uid, &walMeta.id, false)) {
1,119,854 ✔
2238
    goto end;
963,483 ✔
2239
  }
2240
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
157,373 ✔
2241
    code = TSDB_CODE_INVALID_MSG;
×
2242
    TSDB_CHECK_CODE(code, lino, end);
×
2243
  }
2244

2245
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
157,373 ✔
2246
    uint64_t nColData = 0;
81 ✔
2247
    if (tDecodeU64v(pCoder, &nColData) < 0) {
81 ✔
2248
      code = TSDB_CODE_INVALID_MSG;
×
2249
      TSDB_CHECK_CODE(code, lino, end);
×
2250
    }
2251
    if (nColData == 0) {
81 ✔
2252
      ST_TASK_WLOG("%s stream reader scan submit data without column data", __func__);
×
2253
      goto end;
×
2254
    }
2255

2256
    SColData colData = {0};
81 ✔
2257
    code = tDecodeColData(version, pCoder, &colData, false);
81 ✔
2258
    if (code) {
81 ✔
2259
      code = TSDB_CODE_INVALID_MSG;
×
2260
      TSDB_CHECK_CODE(code, lino, end);
×
2261
    }
2262

2263
    if (colData.flag != HAS_VALUE) {
81 ✔
2264
      code = TSDB_CODE_INVALID_MSG;
×
2265
      TSDB_CHECK_CODE(code, lino, end);
×
2266
    }
2267
    if (colData.nVal == 0) {
81 ✔
2268
      ST_TASK_WLOG("%s stream reader scan submit data without rows", __func__);
×
2269
      goto end;
×
2270
    }
2271
    walMeta.skey = ((TSKEY *)colData.pData)[0];
81 ✔
2272
    walMeta.ekey = ((TSKEY *)colData.pData)[colData.nVal - 1];
81 ✔
2273

2274
    for (uint64_t i = 1; i < nColData; i++) {
1,215 ✔
2275
      code = tDecodeColData(version, pCoder, &colData, true);
1,134 ✔
2276
      if (code) {
1,134 ✔
2277
        code = TSDB_CODE_INVALID_MSG;
×
2278
        TSDB_CHECK_CODE(code, lino, end);
×
2279
      }
2280
    }
2281
  } else {
2282
    uint64_t nRow = 0;
157,292 ✔
2283
    if (tDecodeU64v(pCoder, &nRow) < 0) {
156,977 ✔
2284
      code = TSDB_CODE_INVALID_MSG;
×
2285
      TSDB_CHECK_CODE(code, lino, end);
×
2286
    }
2287
    if (nRow == 0) {
156,977 ✔
2288
      ST_TASK_WLOG("%s stream reader scan submit data without rows", __func__);
×
2289
      goto end;
×
2290
    }
2291

2292
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
659,384 ✔
2293
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
502,407 ✔
2294
      pCoder->pos += pRow->len;
502,407 ✔
2295
      if (iRow == 0){
502,407 ✔
2296
#ifndef NO_UNALIGNED_ACCESS
2297
        walMeta.skey = pRow->ts;
157,226 ✔
2298
#else
2299
        walMeta.skey = taosGetInt64Aligned(&pRow->ts);
2300
#endif
2301
      }
2302
      if (iRow == nRow - 1) {
502,407 ✔
2303
#ifndef NO_UNALIGNED_ACCESS
2304
        walMeta.ekey = pRow->ts;
157,292 ✔
2305
#else
2306
        walMeta.ekey = taosGetInt64Aligned(&pRow->ts);
2307
#endif
2308
      }
2309
    }
2310
  }
2311

2312
  if (walMeta.skey > walMeta.ekey || walMeta.skey < win.skey || walMeta.ekey > win.ekey) {
157,256 ✔
2313
    ST_TASK_WLOG("%s stream reader scan data invalid, walMeta time:%" PRId64 ",%" PRId64 ". win time:%" PRId64 ",%" PRId64, __func__,
×
2314
      walMeta.skey, walMeta.ekey, win.skey, win.ekey);
2315
    goto end;
×
2316
  }
2317

2318
  if (expandRollupGids) {
157,373 ✔
2319
    STREAM_CHECK_RET_GOTO(putWalMetaResultForRollupUid(sStreamReaderInfo, gidHash, submitTbData.suid, submitTbData.uid,
2,645 ✔
2320
                                                       walMeta.skey, walMeta.ekey));
2321
  } else {
2322
    STREAM_CHECK_RET_GOTO(putWalMetaResult(gidHash, walMeta.id, walMeta.skey, walMeta.ekey));
154,728 ✔
2323
  }
2324

2325
end:
1,119,700 ✔
2326
  tdDestroySVCreateTbReq(submitTbData.pCreateTbReq);
1,120,145 ✔
2327
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
1,120,013 ✔
2328
  tEndDecode(pCoder);
1,120,079 ✔
2329
  return code;
1,120,211 ✔
2330
}
2331

2332
static int32_t scanSubmitDataForMeta(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len, int64_t ver, STimeWindow win) {
1,120,145 ✔
2333
  int32_t  code = 0;
1,120,145 ✔
2334
  int32_t  lino = 0;
1,120,145 ✔
2335
  SDecoder decoder = {0};
1,120,145 ✔
2336
  SSHashObj* gidHash = NULL;
1,120,211 ✔
2337
  void* pTask = sStreamReaderInfo->pTask;
1,120,211 ✔
2338

2339
  tDecoderInit(&decoder, data, len);
1,120,211 ✔
2340
  if (tStartDecode(&decoder) < 0) {
1,119,749 ✔
2341
    code = TSDB_CODE_INVALID_MSG;
×
2342
    TSDB_CHECK_CODE(code, lino, end);
×
2343
  }
2344

2345
  uint64_t nSubmitTbData = 0;
1,120,343 ✔
2346
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
1,120,343 ✔
2347
    code = TSDB_CODE_INVALID_MSG;
×
2348
    TSDB_CHECK_CODE(code, lino, end);
×
2349
  }
2350

2351
  gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
1,120,343 ✔
2352
  STREAM_CHECK_NULL_GOTO(gidHash, terrno);
1,119,921 ✔
2353

2354
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
2,239,920 ✔
2355
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataForMeta(&decoder, sStreamReaderInfo, gidHash, ver, win));
1,120,052 ✔
2356
  }
2357
  tEndDecode(&decoder);
1,119,868 ✔
2358

2359
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
1,119,538 ✔
2360
  int32_t iter = 0;
1,119,458 ✔
2361
  void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
1,119,458 ✔
2362
  while (px != NULL) {
1,282,027 ✔
2363
    WalMetaResult* pMeta = (WalMetaResult*)px;
162,197 ✔
2364
    int32_t        rollupTbCount = qStreamGetGroupTableCount(sStreamReaderInfo, pMeta->id);
162,197 ✔
2365
    STREAM_CHECK_RET_GOTO(
162,263 ✔
2366
        buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver, rollupTbCount));
80 ✔
2367
    ((SSDataBlock*)rsp->metaBlock)->info.rows++;
162,065 ✔
2368
    rsp->totalRows++;
162,065 ✔
2369
    ST_TASK_DLOG("stream reader scan submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
162,065 ✔
2370
          ", ver:%"PRId64, pMeta->skey, pMeta->ekey, pMeta->id, ver);
2371
    px = tSimpleHashIterate(gidHash, px, &iter);
162,065 ✔
2372
  }
2373
end:
1,119,830 ✔
2374
  tDecoderClear(&decoder);
1,119,830 ✔
2375
  tSimpleHashCleanup( gidHash);
1,119,977 ✔
2376
  return code;
1,120,277 ✔
2377
}
2378

2379
static int32_t createBlockForTsdbMeta(SSDataBlock** pBlock, bool isVTable, bool withRollupTbCount) {
29,978 ✔
2380
  int32_t code = 0;
29,978 ✔
2381
  int32_t lino = 0;
29,978 ✔
2382
  SArray* schemas = taosArrayInit(8, sizeof(SSchema));
29,978 ✔
2383
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
29,978 ✔
2384

2385
  int32_t index = 1;
29,978 ✔
2386
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_TIMESTAMP, LONG_BYTES, index++))  // skey
29,978 ✔
2387
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_TIMESTAMP, LONG_BYTES, index++))  // ekey
29,978 ✔
2388
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // uid
29,978 ✔
2389
  if (!isVTable) {
29,978 ✔
2390
    STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_UBIGINT, LONG_BYTES, index++))  // gid
11,344 ✔
2391
  }
2392
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // nrows
29,978 ✔
2393
  if (withRollupTbCount) {
29,978 ✔
2394
    STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_INT, INT_BYTES, index++))  // rollupTbCount
79 ✔
2395
  }
2396

2397
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
29,978 ✔
2398

2399
end:
29,978 ✔
2400
  taosArrayDestroy(schemas);
29,978 ✔
2401
  return code;
29,978 ✔
2402
}
2403

2404
static int32_t createBlockForWalMetaNew(SSDataBlock** pBlock, bool withRollupTbCount) {
72,824 ✔
2405
  int32_t code = 0;
72,824 ✔
2406
  int32_t lino = 0;
72,824 ✔
2407
  SArray* schemas = NULL;
72,824 ✔
2408

2409
  schemas = taosArrayInit(8, sizeof(SSchema));
72,824 ✔
2410
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
73,062 ✔
2411

2412
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES,
73,062 ✔
2413
                                           STREAM_WAL_META_GID_COL))  // gid non vtable/uid vtable
2414
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, STREAM_WAL_META_SKEY_COL))
73,062 ✔
2415
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, STREAM_WAL_META_EKEY_COL))
73,062 ✔
2416
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, STREAM_WAL_META_VER_COL))
73,062 ✔
2417
  if (withRollupTbCount) {
73,062 ✔
2418
    STREAM_CHECK_RET_GOTO(
2,453 ✔
2419
        qStreamBuildSchema(schemas, TSDB_DATA_TYPE_INT, INT_BYTES, STREAM_WAL_META_ROLLUP_TBCOUNT_COL))
2420
  }
2421

2422
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
73,062 ✔
2423

2424
end:
73,062 ✔
2425
  taosArrayDestroy(schemas);
73,062 ✔
2426
  return code;
73,062 ✔
2427
}
2428

2429
static int32_t processMeta(int16_t msgType, SStreamTriggerReaderInfo* sStreamReaderInfo, void *data, int32_t len, SSTriggerWalNewRsp* rsp, int64_t ver) {
77,724 ✔
2430
  int32_t code = 0;
77,724 ✔
2431
  int32_t lino = 0;
77,724 ✔
2432
  void* pTask = sStreamReaderInfo->pTask;
77,724 ✔
2433

2434
  ST_TASK_DLOG("%s check meta msg, stream ver:%" PRId64 ", wal ver:%" PRId64, __func__, sStreamReaderInfo->tableList.version, ver);
77,724 ✔
2435

2436
  SDecoder dcoder = {0};
77,724 ✔
2437
  tDecoderInit(&dcoder, data, len);
77,790 ✔
2438
  if (msgType == TDMT_VND_DELETE && sStreamReaderInfo->deleteReCalc != 0) {
77,856 ✔
2439
    if (rsp->deleteBlock == NULL) {
8,026 ✔
2440
      STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&rsp->deleteBlock, false));
3,526 ✔
2441
    }
2442
      
2443
    STREAM_CHECK_RET_GOTO(scanDeleteDataNew(sStreamReaderInfo, rsp, data, len, ver));
8,026 ✔
2444
  } else if (msgType == TDMT_VND_DROP_TABLE &&
69,830 ✔
2445
             (sStreamReaderInfo->deleteOutTbl != 0 || sStreamReaderInfo->isVtableStream)) {
7,896 ✔
2446
    STREAM_CHECK_RET_GOTO(scanDropTableNew(sStreamReaderInfo, rsp, data, len, ver));
3,630 ✔
2447
  // } else if (msgType == TDMT_VND_DROP_STB) {
2448
  //   STREAM_CHECK_RET_GOTO(scanDropSTableNew(sStreamReaderInfo, data, len));
2449
  } else if (msgType == TDMT_VND_CREATE_TABLE && !ignoreMetaChange(sStreamReaderInfo->tableList.version, ver)) {
66,200 ✔
2450
    STREAM_CHECK_RET_GOTO(scanCreateTableNew(sStreamReaderInfo, rsp, data, len, ver));
19,818 ✔
2451
  } else if (msgType == TDMT_VND_ALTER_STB && !ignoreMetaChange(sStreamReaderInfo->tableList.version, ver)) {
46,382 ✔
2452
    // STREAM_CHECK_RET_GOTO(scanAlterSTableNew(sStreamReaderInfo, data, len));
2453
  } else if (msgType == TDMT_VND_ALTER_TABLE && !ignoreMetaChange(sStreamReaderInfo->tableList.version, ver)) {
38,330 ✔
2454
    STREAM_CHECK_RET_GOTO(scanAlterTableNew(sStreamReaderInfo, rsp, data, len, ver));
10,251 ✔
2455
  }
2456

2457
end:
77,790 ✔
2458
  tDecoderClear(&dcoder);
77,790 ✔
2459
  return code;
77,790 ✔
2460
}
2461

2462
static bool shouldStopBeforeSubmitAfterTableChange(SStreamTriggerReaderInfo* sStreamReaderInfo,
2,131,923 ✔
2463
                                                   SSTriggerWalNewRsp*       rsp) {
2464
  return sStreamReaderInfo->isVtableStream && rsp->tableBlock != NULL &&
2,129,964 ✔
2465
         ((SSDataBlock*)rsp->tableBlock)->info.rows > 0;
396 ✔
2466
}
2467

2468
static STimeWindow getDbTimeRange(SVnode* pVnode) {
73,217 ✔
2469
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
73,217 ✔
2470
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
73,336 ✔
2471
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
73,336 ✔
2472
  return (STimeWindow){minKey, maxKey};
73,336 ✔
2473
}
2474

2475
static int32_t processWalVerMetaNew(SVnode* pVnode, SSTriggerWalNewRsp* rsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
1,095,544 ✔
2476
                       int64_t ctime) {
2477
  int32_t code = 0;
1,095,544 ✔
2478
  int32_t lino = 0;
1,095,544 ✔
2479
  void* pTask = sStreamReaderInfo->pTask;
1,095,544 ✔
2480

2481
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
1,095,544 ✔
2482
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
1,095,361 ✔
2483
  code = walReaderSeekVer(pWalReader, rsp->ver);
1,095,361 ✔
2484
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
1,095,295 ✔
2485
    if (rsp->ver < walGetFirstVer(pWalReader->pWal)) {
1,022,078 ✔
2486
      rsp->ver = walGetFirstVer(pWalReader->pWal);
×
2487
      rsp->verTime = 0;
×
2488
    } else {
2489
      rsp->verTime = taosGetTimestampUs();
1,021,786 ✔
2490
    }
2491
    ST_TASK_DLOG("vgId:%d %s scan wal end:%s", TD_VID(pVnode), __func__, tstrerror(code));
1,021,786 ✔
2492
    code = TSDB_CODE_SUCCESS;
1,021,946 ✔
2493
    goto end;
1,021,946 ✔
2494
  }
2495
  STREAM_CHECK_RET_GOTO(code);
73,217 ✔
2496

2497
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, STREAM_RETURN_ROWS_NUM));
73,217 ✔
2498

2499
  STimeWindow win = getDbTimeRange(pVnode);
73,415 ✔
2500
  while (1) {
1,160,729 ✔
2501
    code = walNextValidMsg(pWalReader, true);
1,233,640 ✔
2502
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
1,233,870 ✔
2503
      rsp->verTime = taosGetTimestampUs();
73,070 ✔
2504
      ST_TASK_DLOG("vgId:%d %s scan wal end:%s", TD_VID(pVnode), __func__, tstrerror(code));
73,070 ✔
2505
      code = TSDB_CODE_SUCCESS;
73,004 ✔
2506
      goto end;
73,004 ✔
2507
    }
2508
    STREAM_CHECK_RET_GOTO(code);
1,160,800 ✔
2509
    rsp->ver = pWalReader->curVersion;
1,160,800 ✔
2510
    SWalCont* wCont = &pWalReader->pHead->head;
1,160,800 ✔
2511
    rsp->verTime = wCont->ingestTs;
1,160,800 ✔
2512
    if (wCont->ingestTs / 1000 > ctime) break;
1,160,800 ✔
2513
    void*   data = POINTER_SHIFT(walContBody(wCont), sizeof(SMsgHead));
1,160,800 ✔
2514
    int32_t len = walContBodyLen(wCont) - sizeof(SMsgHead);
1,160,800 ✔
2515
    int64_t ver = wCont->version;
1,160,800 ✔
2516

2517
    ST_TASK_DLOG("vgId:%d stream reader scan wal ver:%" PRId64 "/%" PRId64 ", type:%s, deleteData:%d, deleteTb:%d",
1,160,800 ✔
2518
      TD_VID(pVnode), ver, walGetAppliedVer(pWalReader->pWal), TMSG_INFO(wCont->msgType), sStreamReaderInfo->deleteReCalc, sStreamReaderInfo->deleteOutTbl);
2519
    if (wCont->msgType == TDMT_VND_SUBMIT) {
1,161,346 ✔
2520
      // return when getting data if there are meta data in vtable scan
2521
      if (shouldStopBeforeSubmitAfterTableChange(sStreamReaderInfo, rsp)) {
1,120,871 ✔
2522
        rsp->ver--;
396 ✔
2523
        break;
396 ✔
2524
      }
2525
      data = POINTER_SHIFT(walContBody(wCont), sizeof(SSubmitReq2Msg));
1,120,343 ✔
2526
      len = walContBodyLen(wCont) - sizeof(SSubmitReq2Msg);
1,120,343 ✔
2527
      STREAM_CHECK_RET_GOTO(scanSubmitDataForMeta(sStreamReaderInfo, rsp, data, len, ver, win));
1,120,343 ✔
2528
    } else {
2529
      STREAM_CHECK_RET_GOTO(processMeta(wCont->msgType, sStreamReaderInfo, data, len, rsp, ver));
40,475 ✔
2530
    }
2531

2532
    if (rsp->totalRows >= STREAM_RETURN_ROWS_NUM) {
1,160,489 ✔
2533
      break;
×
2534
    }
2535
  }
2536

2537
end:
1,094,796 ✔
2538
  walCloseReader(pWalReader);
1,095,346 ✔
2539
  return code;
1,095,277 ✔
2540
}
2541

2542
int32_t cacheTag(SVnode* pVnode, SHashObj* metaCache, SExprInfo* pExprInfo, int32_t numOfExpr, SStorageAPI* api, uint64_t uid, col_id_t colId, SRWLatch* lock) {
10,780,772 ✔
2543
  int32_t     code = 0;
10,780,772 ✔
2544
  int32_t     lino = 0;
10,780,772 ✔
2545
  SMetaReader mr = {0};
10,780,772 ✔
2546
  SArray* tagCache = NULL;
10,781,164 ✔
2547
  char* data = NULL;
10,779,880 ✔
2548

2549
  if (lock != NULL) taosWLockLatch(lock);
10,779,983 ✔
2550
  STREAM_CHECK_CONDITION_GOTO(numOfExpr == 0, code);
10,782,595 ✔
2551
  stDebug("%s start,uid:%"PRIu64, __func__, uid);
528,719 ✔
2552
  void* uidData = taosHashGet(metaCache, &uid, LONG_BYTES);
528,801 ✔
2553
  if (uidData == NULL) {
528,801 ✔
2554
    tagCache = taosArrayInit(numOfExpr, POINTER_BYTES);
523,245 ✔
2555
    STREAM_CHECK_NULL_GOTO(tagCache, terrno);
523,245 ✔
2556
    if(taosHashPut(metaCache, &uid, LONG_BYTES, &tagCache, POINTER_BYTES) != 0) {
523,245 ✔
2557
      taosArrayDestroy(tagCache);
×
2558
      code = terrno;
×
2559
      goto end;
×
2560
    }
2561
  } else {
2562
    tagCache = *(SArray**)uidData;
5,556 ✔
2563
    stDebug("%s found tagCache, size:%zu %d, uid:%"PRIu64, __func__, taosArrayGetSize(tagCache), numOfExpr, uid);
5,556 ✔
2564
    STREAM_CHECK_CONDITION_GOTO(taosArrayGetSize(tagCache) != numOfExpr, TSDB_CODE_INVALID_PARA);
5,556 ✔
2565
  }
2566
  
2567
  api->metaReaderFn.initReader(&mr, pVnode, META_READER_LOCK, &api->metaFn, 0);
528,801 ✔
2568
  code = api->metaReaderFn.getEntryGetUidCache(&mr, uid);
528,801 ✔
2569
  api->metaReaderFn.readerReleaseLock(&mr);
528,801 ✔
2570
  STREAM_CHECK_RET_GOTO(code);
528,706 ✔
2571
  
2572
  for (int32_t j = 0; j < numOfExpr; ++j) {
2,236,528 ✔
2573
    const SExprInfo* pExpr1 = &pExprInfo[j];
1,709,127 ✔
2574
    int32_t functionId = pExpr1->pExpr->_function.functionId;
1,709,127 ✔
2575
    col_id_t cid = 0;
1,709,127 ✔
2576
    // this is to handle the tbname
2577
    if (fmIsScanPseudoColumnFunc(functionId)) {
1,709,127 ✔
2578
      int32_t fType = pExpr1->pExpr->_function.functionType;
140,435 ✔
2579
      if (fType == FUNCTION_TYPE_TBNAME) {
140,435 ✔
2580
        data = taosMemoryCalloc(1, strlen(mr.me.name) + VARSTR_HEADER_SIZE);
140,357 ✔
2581
        STREAM_CHECK_NULL_GOTO(data, terrno);
140,279 ✔
2582
        STR_TO_VARSTR(data, mr.me.name)
140,279 ✔
2583
      }
2584
      cid = -1;
140,393 ✔
2585
    } else {  // these are tags
2586
      const char* p = NULL;
1,568,517 ✔
2587
      char* pData = NULL;
1,568,517 ✔
2588
      int8_t type = pExpr1->base.resSchema.type;
1,568,517 ✔
2589
      int32_t len = pExpr1->base.resSchema.bytes;
1,568,517 ✔
2590
      STagVal tagVal = {0};
1,568,517 ✔
2591
      tagVal.cid = pExpr1->base.pParam[0].pCol->colId;
1,568,768 ✔
2592
      cid = tagVal.cid;
1,568,768 ✔
2593
      if (colId != 0 && cid != colId) {
1,568,768 ✔
2594
        continue;
292 ✔
2595
      }
2596
      p = api->metaFn.extractTagVal(mr.me.ctbEntry.pTags, type, &tagVal);
1,568,476 ✔
2597

2598
      if (type != TSDB_DATA_TYPE_JSON && p != NULL) {
1,568,512 ✔
2599
        pData = tTagValToData((const STagVal*)p, false);
1,565,824 ✔
2600
      } else {
2601
        pData = (char*)p;
2,688 ✔
2602
      }
2603

2604
      if (pData != NULL && (type == TSDB_DATA_TYPE_JSON || !IS_VAR_DATA_TYPE(type))) {
1,568,398 ✔
2605
        if (type == TSDB_DATA_TYPE_JSON) {
837,297 ✔
2606
          len = getJsonValueLen(pData);
×
2607
        }
2608
        data = taosMemoryCalloc(1, len);
837,315 ✔
2609
        STREAM_CHECK_NULL_GOTO(data, terrno);
837,326 ✔
2610
        (void)memcpy(data, pData, len);
837,326 ✔
2611
      } else {
2612
        data = pData;
731,083 ✔
2613
      }
2614
    }
2615
    if (uidData == NULL){
1,708,540 ✔
2616
      STREAM_CHECK_NULL_GOTO(taosArrayPush(tagCache, &data), terrno);
3,396,017 ✔
2617
    } else {
2618
      void* pre = taosArrayGetP(tagCache, j);
10,688 ✔
2619
      taosMemoryFree(pre);
10,688 ✔
2620
      taosArraySet(tagCache, j, &data);
10,688 ✔
2621
    }
2622
    data = NULL;
1,708,819 ✔
2623
  }
2624

2625
end:
10,782,541 ✔
2626
  taosMemoryFree(data);
10,782,064 ✔
2627
  api->metaReaderFn.clearReader(&mr);
10,781,184 ✔
2628
  if (lock != NULL) taosWUnLockLatch(lock);
10,779,519 ✔
2629
  return code;
10,781,724 ✔
2630
}
2631

2632
int32_t fillTag(SHashObj* metaCache, SExprInfo* pExprInfo, int32_t numOfExpr,
66,551,659 ✔
2633
                uint64_t uid, SSDataBlock* pBlock, uint32_t currentRow, uint32_t numOfRows, uint32_t numOfBlocks, SRWLatch* lock) {
2634
  int32_t     code = 0;
66,551,659 ✔
2635
  int32_t     lino = 0;
66,551,659 ✔
2636
  SArray* tagCache = NULL;
66,551,659 ✔
2637
  if (numOfExpr == 0) {
66,551,659 ✔
2638
    return TSDB_CODE_SUCCESS;
10,139,559 ✔
2639
  }
2640

2641
  taosRLockLatch(lock);
56,412,100 ✔
2642
  void* uidData = taosHashGet(metaCache, &uid, LONG_BYTES);
56,447,552 ✔
2643
  if (uidData == NULL) {
56,441,070 ✔
2644
    stError("%s error uidData is null,uid:%"PRIu64, __func__, uid);
×
2645
  } else {
2646
    tagCache = *(SArray**)uidData;
56,441,070 ✔
2647
    if(taosArrayGetSize(tagCache) != numOfExpr) {
56,441,070 ✔
2648
      stError("%s numOfExpr:%d,tagCache size:%zu", __func__, numOfExpr, taosArrayGetSize(tagCache));
×
2649
      tagCache = NULL;
×
2650
    }
2651
  }
2652
  
2653
  for (int32_t j = 0; j < numOfExpr; ++j) {
219,084,166 ✔
2654
    const SExprInfo* pExpr1 = &pExprInfo[j];
162,661,945 ✔
2655
    int32_t          dstSlotId = pExpr1->base.resSchema.slotId;
162,661,945 ✔
2656

2657
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, dstSlotId);
162,661,945 ✔
2658
    STREAM_CHECK_NULL_GOTO(pColInfoData, terrno);
162,612,731 ✔
2659
    int32_t functionId = pExpr1->pExpr->_function.functionId;
162,612,731 ✔
2660

2661
    // this is to handle the tbname
2662
    if (fmIsScanPseudoColumnFunc(functionId)) {
162,612,731 ✔
2663
      int32_t fType = pExpr1->pExpr->_function.functionType;
799,003 ✔
2664
      if (fType == FUNCTION_TYPE_TBNAME) {
799,003 ✔
2665
        pColInfoData->info.colId = -1;
798,935 ✔
2666
      }
2667
    } 
2668
    char* data = tagCache == NULL ? NULL : taosArrayGetP(tagCache, j);
162,609,090 ✔
2669

2670
    bool isNullVal = (data == NULL) || (pColInfoData->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data));
162,531,187 ✔
2671
    if (isNullVal) {
162,531,187 ✔
2672
      colDataSetNNULL(pColInfoData, currentRow, numOfRows);
×
2673
    } else {
2674
      if (!IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
162,531,187 ✔
2675
        for (uint32_t i = 0; i < numOfRows; i++){
775,304,334 ✔
2676
          colDataClearNull_f(pColInfoData->nullbitmap, currentRow + i);
689,246,652 ✔
2677
        }
2678
      }
2679
      code = colDataSetNItems(pColInfoData, currentRow, (const char*)data, numOfRows, numOfBlocks, false);
162,531,187 ✔
2680
      STREAM_CHECK_RET_GOTO(code);
162,641,885 ✔
2681
    }
2682
  }
2683
end:
56,422,221 ✔
2684
  taosRUnLockLatch(lock);
56,422,313 ✔
2685
  return code;
56,445,284 ✔
2686
}
2687

2688
static int32_t processTag(SStreamTriggerReaderInfo* info, bool isCalc, 
767,405 ✔
2689
  uint64_t uid, SSDataBlock* pBlock, uint32_t currentRow, uint32_t numOfRows, uint32_t numOfBlocks) {
2690
  int32_t     code = 0;
767,405 ✔
2691
  int32_t     lino = 0;
767,405 ✔
2692

2693
  void* pTask = info->pTask;
767,405 ✔
2694
  ST_TASK_DLOG("%s start. rows:%" PRIu32 ",uid:%"PRIu64, __func__,  numOfRows, uid);
767,405 ✔
2695
  
2696
  SHashObj* metaCache = isCalc ? info->pTableMetaCacheCalc : info->pTableMetaCacheTrigger;
767,750 ✔
2697
  SExprInfo*   pExprInfo = isCalc ? info->pExprInfoCalcTag : info->pExprInfoTriggerTag; 
767,750 ✔
2698
  int32_t      numOfExpr = isCalc ? info->numOfExprCalcTag : info->numOfExprTriggerTag;
767,750 ✔
2699
  
2700
  code = fillTag(metaCache, pExprInfo, numOfExpr, uid, pBlock, currentRow, numOfRows, numOfBlocks, &info->lock);
767,750 ✔
2701
  STREAM_CHECK_RET_GOTO(code);
767,885 ✔
2702

2703
end:
767,885 ✔
2704
  return code;
767,885 ✔
2705
}
2706

2707
int32_t getRowRange(SColData* pCol, STimeWindow* window, int32_t* rowStart, int32_t* rowEnd, int32_t* nRows) {
162 ✔
2708
  int32_t code = 0;
162 ✔
2709
  int32_t lino = 0;
162 ✔
2710
  *nRows = 0;
162 ✔
2711
  *rowStart = 0;
162 ✔
2712
  *rowEnd = pCol->nVal;
162 ✔
2713
  if (window != NULL) {
162 ✔
2714
    SColVal colVal = {0};
162 ✔
2715
    *rowStart = -1;
162 ✔
2716
    *rowEnd = -1;
162 ✔
2717
    for (int32_t k = 0; k < pCol->nVal; k++) {
162,162 ✔
2718
      STREAM_CHECK_RET_GOTO(tColDataGetValue(pCol, k, &colVal));
162,000 ✔
2719
      int64_t ts = VALUE_GET_TRIVIAL_DATUM(&colVal.value);
162,000 ✔
2720
      if (ts >= window->skey && *rowStart == -1) {
162,000 ✔
2721
        *rowStart = k;
162 ✔
2722
      }
2723
      if (ts > window->ekey && *rowEnd == -1) {
162,000 ✔
2724
        *rowEnd = k;
×
2725
      }
2726
    }
2727
    STREAM_CHECK_CONDITION_GOTO(*rowStart == -1 || *rowStart == *rowEnd, TDB_CODE_SUCCESS);
162 ✔
2728

2729
    if (*rowStart != -1 && *rowEnd == -1) {
162 ✔
2730
      *rowEnd = pCol->nVal;
162 ✔
2731
    }
2732
  }
2733
  *nRows = *rowEnd - *rowStart;
162 ✔
2734

2735
end:
162 ✔
2736
  return code;
162 ✔
2737
}
2738

2739
static int32_t setColData(int64_t rows, int32_t rowStart, int32_t rowEnd, SColData* colData, SColumnInfoData* pColData) {
1,215 ✔
2740
  int32_t code = 0;
1,215 ✔
2741
  int32_t lino = 0;
1,215 ✔
2742
  for (int32_t k = rowStart; k < rowEnd; k++) {
1,216,215 ✔
2743
    SColVal colVal = {0};
1,215,000 ✔
2744
    STREAM_CHECK_RET_GOTO(tColDataGetValue(colData, k, &colVal));
1,215,000 ✔
2745
    if (IS_VAR_DATA_TYPE(colVal.value.type)) {
1,215,000 ✔
2746
      STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, rows + k - rowStart,
243,000 ✔
2747
                                             (const char*)colVal.value.pData, colVal.value.nData,
2748
                                             !COL_VAL_IS_VALUE(&colVal)));
2749
    } else {
2750
      STREAM_CHECK_RET_GOTO(colDataSetVal(pColData, rows + k - rowStart, VALUE_GET_DATUM(&colVal.value, colVal.value.type),
972,000 ✔
2751
                                          !COL_VAL_IS_VALUE(&colVal)));
×
2752
    }    
2753
  }
2754
  end:
1,215 ✔
2755
  return code;
1,215 ✔
2756
}
2757

2758
static int32_t getColId(int64_t suid, int64_t uid, int16_t i, SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, int16_t* colId) {
321,166 ✔
2759
  int32_t code = 0;
321,166 ✔
2760
  int32_t lino = 0;
321,166 ✔
2761
  int64_t id[2] = {suid, uid};
321,166 ✔
2762
  taosRLockLatch(&sStreamReaderInfo->lock);
321,166 ✔
2763
  void *px = tSimpleHashGet(rsp->isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, id, sizeof(id));
321,166 ✔
2764
  STREAM_CHECK_NULL_GOTO(px, TSDB_CODE_INVALID_PARA);
320,902 ✔
2765
  SSHashObj* uInfo = *(SSHashObj **)px;
320,902 ✔
2766
  STREAM_CHECK_NULL_GOTO(uInfo, TSDB_CODE_INVALID_PARA);
320,902 ✔
2767
  int16_t*  tmp = tSimpleHashGet(uInfo, &i, sizeof(i));
320,902 ✔
2768
  if (tmp != NULL) {
320,770 ✔
2769
    *colId = *tmp;
276,124 ✔
2770
  } else {
2771
    *colId = -1;
44,646 ✔
2772
  }
2773

2774
end:
320,770 ✔
2775
  taosRUnLockLatch(&sStreamReaderInfo->lock);
320,770 ✔
2776
  return code;
321,166 ✔
2777
}
2778

2779
static int32_t getSchemas(SVnode* pVnode, int64_t suid, int64_t uid, int32_t sver, SStreamTriggerReaderInfo* sStreamReaderInfo, STSchema** schema) {
870,535 ✔
2780
  int32_t code = 0;
870,535 ✔
2781
  int32_t lino = 0;
870,535 ✔
2782
  int64_t id = suid != 0 ? suid : uid;
870,535 ✔
2783
  if (sStreamReaderInfo->isVtableStream) {
870,601 ✔
2784
    STSchema** schemaTmp = taosHashGet(sStreamReaderInfo->triggerTableSchemaMapVTable, &id, LONG_BYTES);
131,822 ✔
2785
    if (schemaTmp == NULL || *schemaTmp == NULL || (*schemaTmp)->version != sver) {
131,888 ✔
2786
      *schema = metaGetTbTSchema(pVnode->pMeta, id, sver, 1);
6,048 ✔
2787
      STREAM_CHECK_NULL_GOTO(*schema, terrno);
6,048 ✔
2788
      code = taosHashPut(sStreamReaderInfo->triggerTableSchemaMapVTable, &id, LONG_BYTES, schema, POINTER_BYTES);
6,048 ✔
2789
      if (code != 0) {
6,048 ✔
2790
        taosMemoryFree(*schema);
×
2791
        goto end;
×
2792
      }
2793
    } else {
2794
      *schema = *schemaTmp;
125,840 ✔
2795
    }
2796
  } else {
2797
    if (sStreamReaderInfo->triggerTableSchema == NULL || sStreamReaderInfo->triggerTableSchema->version != sver) {
738,779 ✔
2798
      taosMemoryFree(sStreamReaderInfo->triggerTableSchema);
34,205 ✔
2799
      sStreamReaderInfo->triggerTableSchema = metaGetTbTSchema(pVnode->pMeta, id, sver, 1);
33,676 ✔
2800
      STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->triggerTableSchema, terrno);
33,676 ✔
2801
    }
2802
    *schema = sStreamReaderInfo->triggerTableSchema;
738,250 ✔
2803
  }
2804
  
2805
end:
870,138 ✔
2806
  return code;
870,204 ✔
2807
}
2808

2809
static int32_t finishWalDataTarget(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp,
871,502 ✔
2810
                                   SSHashObj* gidHash, SSDataBlock* pBlock, int64_t uid, int64_t ver,
2811
                                   SWalDataTarget* target, WalMetaResult* walMeta, SStreamWalDataSlice* pSlice,
2812
                                   int32_t blockStart, int32_t numOfRows) {
2813
  int32_t code = 0;
871,502 ✔
2814
  int32_t lino = 0;
871,502 ✔
2815
  void*   pTask = sStreamReaderInfo->pTask;
871,502 ✔
2816

2817
  if (numOfRows <= 0) {
871,502 ✔
2818
    ST_TASK_DLOG("%s no valid data in time range:skey %" PRId64 ", ekey %" PRId64 ", uid:%" PRId64 ", gid:%" PRIu64,
×
2819
                 __func__, target->window.skey, target->window.ekey, uid, target->gid);
2820
    goto end;
×
2821
  }
2822

2823
  if (!sStreamReaderInfo->isVtableStream) {
871,502 ✔
2824
    STREAM_CHECK_RET_GOTO(processTag(sStreamReaderInfo, rsp->isCalc, uid, pBlock, blockStart, numOfRows, 1));
739,416 ✔
2825
  }
2826

2827
  SColumnInfoData* pColData = taosArrayGetLast(pBlock->pDataBlock);
871,519 ✔
2828
  STREAM_CHECK_NULL_GOTO(pColData, terrno);
871,051 ✔
2829
  STREAM_CHECK_RET_GOTO(colDataSetNItems(pColData, blockStart, (const char*)&ver, numOfRows, 1, false));
871,051 ✔
2830

2831
  STREAM_CHECK_NULL_GOTO(pSlice, TSDB_CODE_INVALID_PARA);
871,647 ✔
2832
  ST_TASK_DLOG("%s process submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64 ", uid:%" PRId64
871,647 ✔
2833
               ", ver:%" PRId64 ", row index:%d, rows:%d",
2834
               __func__, target->window.skey, target->window.ekey, target->gid, uid, ver, pSlice->currentRowIdx,
2835
               numOfRows);
2836
  pSlice->currentRowIdx += numOfRows;
871,381 ✔
2837
  pBlock->info.rows += numOfRows;
871,381 ✔
2838

2839
  if (gidHash != NULL) {
871,381 ✔
2840
    STREAM_CHECK_RET_GOTO(putWalMetaResult(gidHash, target->gid, walMeta->skey, walMeta->ekey));
397,422 ✔
2841
  }
2842

2843
end:
871,586 ✔
2844
  return code;
871,586 ✔
2845
}
2846

2847
static int32_t scanSubmitTbData(SVnode* pVnode, SDecoder* pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo,
1,108,154 ✔
2848
                                SSHashObj* ranges, SSHashObj* gidHash, SSTriggerWalNewRsp* rsp, int64_t ver) {
2849
  int32_t       code = 0;
1,108,154 ✔
2850
  int32_t       lino = 0;
1,108,154 ✔
2851
  WalMetaResult walMeta = {0};
1,108,154 ✔
2852
  void*         pTask = sStreamReaderInfo->pTask;
1,108,022 ✔
2853
  SSDataBlock*  pBlock = (SSDataBlock*)rsp->dataBlock;
1,108,022 ✔
2854
  SArray*       targets = NULL;
1,108,022 ✔
2855
  bool          expandRollupGids = isRollupMultiReader(sStreamReaderInfo);
1,108,022 ✔
2856

2857
  if (tStartDecode(pCoder) < 0) {
1,106,771 ✔
2858
    ST_TASK_ELOG("vgId:%d %s invalid submit data", TD_VID(pVnode), __func__);
×
2859
    code = TSDB_CODE_INVALID_MSG;
×
2860
    TSDB_CHECK_CODE(code, lino, end);
680 ✔
2861
  }
2862

2863
  SSubmitTbData submitTbData = {0};
1,108,688 ✔
2864
  uint8_t       version = 0;
1,108,088 ✔
2865
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
1,106,887 ✔
2866
    ST_TASK_ELOG("vgId:%d %s invalid submit data flags", TD_VID(pVnode), __func__);
×
2867
    code = TSDB_CODE_INVALID_MSG;
×
2868
    TSDB_CHECK_CODE(code, lino, end);
680 ✔
2869
  }
2870
  version = (submitTbData.flags >> 8) & 0xff;
1,106,887 ✔
2871
  submitTbData.flags = submitTbData.flags & 0xff;
1,106,887 ✔
2872
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
2873
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
1,106,887 ✔
2874
    if (tStartDecode(pCoder) < 0) {
60,990 ✔
2875
      ST_TASK_ELOG("vgId:%d %s invalid auto create table data", TD_VID(pVnode), __func__);
×
2876
      code = TSDB_CODE_INVALID_MSG;
×
2877
      TSDB_CHECK_CODE(code, lino, end);
100 ✔
2878
    }
2879
    tEndDecode(pCoder);
61,124 ✔
2880
  }
2881

2882
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
1,106,911 ✔
2883
    ST_TASK_ELOG("vgId:%d %s invalid submit data suid", TD_VID(pVnode), __func__);
×
2884
    code = TSDB_CODE_INVALID_MSG;
×
2885
    TSDB_CHECK_CODE(code, lino, end);
680 ✔
2886
  }
2887
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
1,106,758 ✔
2888
    ST_TASK_ELOG("vgId:%d %s invalid submit data uid", TD_VID(pVnode), __func__);
×
2889
    code = TSDB_CODE_INVALID_MSG;
×
2890
    TSDB_CHECK_CODE(code, lino, end);
680 ✔
2891
  }
2892

2893
  ST_TASK_DLOG("%s uid:%" PRId64 ", suid:%" PRId64 ", ver:%" PRId64, __func__, submitTbData.uid, submitTbData.suid,
1,106,758 ✔
2894
               ver);
2895

2896
  SWalDataTarget singleTarget = {.gid = 0, .window = {.skey = INT64_MIN, .ekey = INT64_MAX}};
1,107,064 ✔
2897
  if (rsp->uidHash != NULL) {
1,106,772 ✔
2898
    uint64_t* gid = tSimpleHashGet(rsp->uidHash, &submitTbData.uid, LONG_BYTES);
473,998 ✔
2899
    STREAM_CHECK_CONDITION_GOTO(gid == NULL, TDB_CODE_SUCCESS);
474,455 ✔
2900
    ST_TASK_DLOG("%s get uid gid from uidHash, uid:%" PRId64 ", suid:%" PRId64 " gid:%" PRIu64, __func__,
473,761 ✔
2901
                  submitTbData.uid, submitTbData.suid, *gid);
2902
    singleTarget.gid = *gid;
473,695 ✔
2903
  }
2904
  if (expandRollupGids) {
1,106,469 ✔
2905
    targets = taosArrayInit(4, sizeof(SWalDataTarget));
29,340 ✔
2906
    STREAM_CHECK_NULL_GOTO(targets, terrno);
29,340 ✔
2907
    STREAM_CHECK_RET_GOTO(
29,340 ✔
2908
        collectWalRollupTargets(sStreamReaderInfo, ranges, submitTbData.suid, submitTbData.uid, targets));
×
2909
    STREAM_CHECK_CONDITION_GOTO(taosArrayGetSize(targets) == 0, TDB_CODE_SUCCESS);
29,340 ✔
2910
  } else {
2911
    if (singleTarget.gid == 0) {
1,077,129 ✔
2912
      STREAM_CHECK_CONDITION_GOTO(
753,732 ✔
2913
          !uidInTableListSet(sStreamReaderInfo, submitTbData.suid, submitTbData.uid, &singleTarget.gid, rsp->isCalc),
2914
          TDB_CODE_SUCCESS);
110 ✔
2915
    }
2916

2917
    if (ranges != NULL) {
840,734 ✔
2918
      void* timerange = tSimpleHashGet(ranges, &singleTarget.gid, sizeof(singleTarget.gid));
444,878 ✔
2919
      if (timerange == NULL) goto end;
444,812 ✔
2920
      int64_t* pRange = (int64_t*)timerange;
444,812 ✔
2921
      singleTarget.window.skey = pRange[0];
444,812 ✔
2922
      singleTarget.window.ekey = pRange[1];
444,812 ✔
2923
      ST_TASK_DLOG("%s get time range from ranges, uid:%" PRId64 ", suid:%" PRId64 ", gid:%" PRIu64 ", skey:%" PRId64
444,812 ✔
2924
                   ", ekey:%" PRId64,
2925
                   __func__, submitTbData.uid, submitTbData.suid, singleTarget.gid, singleTarget.window.skey,
2926
                   singleTarget.window.ekey);
2927
    }
2928
  }
2929

2930
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
870,802 ✔
2931
    ST_TASK_ELOG("vgId:%d %s invalid submit data sver", TD_VID(pVnode), __func__);
×
2932
    code = TSDB_CODE_INVALID_MSG;
×
2933
    TSDB_CHECK_CODE(code, lino, end);
110 ✔
2934
  }
2935

2936
  STSchema* schema = NULL;
870,802 ✔
2937
  STREAM_CHECK_RET_GOTO(
870,738 ✔
2938
      getSchemas(pVnode, submitTbData.suid, submitTbData.uid, submitTbData.sver, sStreamReaderInfo, &schema));
110 ✔
2939

2940
  int32_t numOfTargets = expandRollupGids ? taosArrayGetSize(targets) : 1;
869,739 ✔
2941
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
869,739 ✔
2942
    uint64_t nColData = 0;
81 ✔
2943
    if (tDecodeU64v(pCoder, &nColData) < 0) {
81 ✔
2944
      ST_TASK_ELOG("vgId:%d %s invalid submit data nColData", TD_VID(pVnode), __func__);
×
2945
      code = TSDB_CODE_INVALID_MSG;
×
2946
      TSDB_CHECK_CODE(code, lino, end);
×
2947
    }
2948

2949
    SColData colData = {0};
81 ✔
2950
    code = tDecodeColData(version, pCoder, &colData, false);
81 ✔
2951
    if (code) {
81 ✔
2952
      ST_TASK_ELOG("vgId:%d %s invalid submit data colData", TD_VID(pVnode), __func__);
×
2953
      code = TSDB_CODE_INVALID_MSG;
×
2954
      TSDB_CHECK_CODE(code, lino, end);
×
2955
    }
2956

2957
    if (colData.flag != HAS_VALUE) {
81 ✔
2958
      ST_TASK_ELOG("vgId:%d %s invalid submit data colData flag", TD_VID(pVnode), __func__);
×
2959
      code = TSDB_CODE_INVALID_MSG;
×
2960
      TSDB_CHECK_CODE(code, lino, end);
×
2961
    }
2962

2963
    walMeta.skey = ((TSKEY*)colData.pData)[0];
81 ✔
2964
    walMeta.ekey = ((TSKEY*)colData.pData)[colData.nVal - 1];
81 ✔
2965
    int32_t colDataPos = pCoder->pos;
81 ✔
2966

2967
    for (int32_t t = 0; t < numOfTargets; ++t) {
162 ✔
2968
      SWalDataTarget* target = expandRollupGids ? taosArrayGet(targets, t) : &singleTarget;
81 ✔
2969
      STREAM_CHECK_NULL_GOTO(target, terrno);
81 ✔
2970

2971
      int32_t rowStart = 0;
81 ✔
2972
      int32_t rowEnd = 0;
81 ✔
2973
      int32_t numOfRows = 0;
81 ✔
2974
      STREAM_CHECK_RET_GOTO(getRowRange(&colData, &target->window, &rowStart, &rowEnd, &numOfRows));
81 ✔
2975
      if (numOfRows <= 0) {
81 ✔
2976
        ST_TASK_WLOG("%s no valid column data, uid:%" PRId64 ", gid:%" PRIu64, __func__, submitTbData.uid, target->gid);
×
2977
        continue;
×
2978
      }
2979

2980
      SStreamWalDataSlice* pSlice = getWalDataSlice(sStreamReaderInfo, rsp, submitTbData.uid, target->gid);
81 ✔
2981
      STREAM_CHECK_NULL_GOTO(pSlice, TSDB_CODE_INVALID_PARA);
81 ✔
2982

2983
      int32_t blockStart = pSlice->currentRowIdx;
81 ✔
2984
      int32_t sliceEnd = pSlice->startRowIdx + pSlice->numRows;
81 ✔
2985
      if (blockStart >= sliceEnd) {
81 ✔
2986
        ST_TASK_WLOG("%s skip column data beyond slice uid:%" PRId64 ", gid:%" PRIu64 ", ver:%" PRId64
×
2987
                     ", currentRowIdx:%d, sliceEnd:%d",
2988
                     __func__, submitTbData.uid, target->gid, ver, blockStart, sliceEnd);
2989
        continue;
×
2990
      }
2991
      int32_t availableRows = sliceEnd - blockStart;
81 ✔
2992
      if (numOfRows > availableRows) {
81 ✔
2993
        ST_TASK_WLOG("%s truncate column data to slice uid:%" PRId64 ", gid:%" PRIu64 ", ver:%" PRId64
×
2994
                     ", sliceRows:%d, inputRows:%d, currentRowIdx:%d",
2995
                     __func__, submitTbData.uid, target->gid, ver, availableRows, numOfRows, blockStart);
2996
        rowEnd = rowStart + availableRows;
×
2997
        numOfRows = availableRows;
×
2998
      }
2999
      for (int16_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {
1,458 ✔
3000
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, i);
1,377 ✔
3001
        STREAM_CHECK_NULL_GOTO(pColData, terrno);
1,377 ✔
3002
        if (pColData->info.colId <= -1) {
1,377 ✔
3003
          pColData->hasNull = true;
162 ✔
3004
          continue;
162 ✔
3005
        }
3006
        if (pColData->info.colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
1,215 ✔
3007
          STREAM_CHECK_RET_GOTO(setColData(blockStart, rowStart, rowEnd, &colData, pColData));
81 ✔
3008
          continue;
81 ✔
3009
        }
3010

3011
        pCoder->pos = colDataPos;
1,134 ✔
3012

3013
        int16_t colId = 0;
1,134 ✔
3014
        if (sStreamReaderInfo->isVtableStream) {
1,134 ✔
3015
          STREAM_CHECK_RET_GOTO(getColId(submitTbData.suid, submitTbData.uid, i, sStreamReaderInfo, rsp, &colId));
×
3016
          ST_TASK_TLOG("%s vtable colId:%d, i:%d, uid:%" PRId64, __func__, colId, i, submitTbData.uid);
×
3017
        } else {
3018
          colId = pColData->info.colId;
1,134 ✔
3019
        }
3020

3021
        uint64_t j = 1;
1,134 ✔
3022
        for (; j < nColData; j++) {
8,505 ✔
3023
          int16_t cid = 0;
8,505 ✔
3024
          int32_t posTmp = pCoder->pos;
8,505 ✔
3025
          pCoder->pos += INT_BYTES;
8,505 ✔
3026
          code = tDecodeI16v(pCoder, &cid);
8,505 ✔
3027
          TSDB_CHECK_CODE(code, lino, end);
8,505 ✔
3028
          pCoder->pos = posTmp;
8,505 ✔
3029
          if (cid == colId) {
8,505 ✔
3030
            SColData colDataTmp = {0};
1,134 ✔
3031
            code = tDecodeColData(version, pCoder, &colDataTmp, false);
1,134 ✔
3032
            if (code) {
1,134 ✔
3033
              code = TSDB_CODE_INVALID_MSG;
×
3034
              TSDB_CHECK_CODE(code, lino, end);
×
3035
            }
3036
            STREAM_CHECK_RET_GOTO(setColData(blockStart, rowStart, rowEnd, &colDataTmp, pColData));
1,134 ✔
3037
            break;
1,134 ✔
3038
          }
3039
          SColData skipColData = {0};
7,371 ✔
3040
          code = tDecodeColData(version, pCoder, &skipColData, true);
7,371 ✔
3041
          if (code) {
7,371 ✔
3042
            code = TSDB_CODE_INVALID_MSG;
×
3043
            TSDB_CHECK_CODE(code, lino, end);
×
3044
          }
3045
        }
3046
        if (j == nColData) {
1,134 ✔
3047
          colDataSetNNULL(pColData, blockStart, numOfRows);
×
3048
        }
3049
      }
3050

3051
      STREAM_CHECK_RET_GOTO(finishWalDataTarget(sStreamReaderInfo, rsp, gidHash, pBlock, submitTbData.uid, ver, target,
81 ✔
3052
                                                &walMeta, pSlice, blockStart, numOfRows));
×
3053
    }
3054
  } else {
3055
    uint64_t nRow = 0;
869,658 ✔
3056
    if (tDecodeU64v(pCoder, &nRow) < 0) {
870,323 ✔
3057
      code = TSDB_CODE_INVALID_MSG;
×
3058
      TSDB_CHECK_CODE(code, lino, end);
×
3059
    }
3060
    int32_t rowsPos = pCoder->pos;
870,323 ✔
3061

3062
    for (int32_t t = 0; t < numOfTargets; ++t) {
1,741,762 ✔
3063
      SWalDataTarget* target = expandRollupGids ? taosArrayGet(targets, t) : &singleTarget;
870,839 ✔
3064
      STREAM_CHECK_NULL_GOTO(target, terrno);
870,597 ✔
3065
      SStreamWalDataSlice* pSlice = getWalDataSlice(sStreamReaderInfo, rsp, submitTbData.uid, target->gid);
870,707 ✔
3066
      if (pSlice == NULL) {
871,175 ✔
3067
        ST_TASK_WLOG("%s no row data slice, uid:%" PRId64 ", gid:%" PRIu64, __func__, submitTbData.uid, target->gid);
333 ✔
3068
        continue;
×
3069
      }
3070

3071
      int32_t blockStart = pSlice->currentRowIdx;
870,842 ✔
3072
      int32_t sliceEnd = pSlice->startRowIdx + pSlice->numRows;
870,842 ✔
3073
      if (blockStart >= sliceEnd) {
870,842 ✔
3074
        ST_TASK_WLOG("%s skip row data beyond slice uid:%" PRId64 ", gid:%" PRIu64 ", ver:%" PRId64
×
3075
                     ", currentRowIdx:%d, sliceEnd:%d",
3076
                     __func__, submitTbData.uid, target->gid, ver, blockStart, sliceEnd);
3077
        continue;
×
3078
      }
3079
      int32_t availableRows = sliceEnd - blockStart;
870,842 ✔
3080
      int32_t numOfRows = 0;
870,842 ✔
3081
      pCoder->pos = rowsPos;
870,842 ✔
3082
      for (uint64_t iRow = 0; iRow < nRow; ++iRow) {
2,179,979 ✔
3083
        SRow* pRow = (SRow*)(pCoder->data + pCoder->pos);
1,310,244 ✔
3084
        pCoder->pos += pRow->len;
1,310,244 ✔
3085

3086
#ifndef NO_UNALIGNED_ACCESS
3087
        TSKEY rowTs = pRow->ts;
1,310,244 ✔
3088
#else
3089
        TSKEY rowTs = taosGetInt64Aligned(&pRow->ts);
3090
#endif
3091
        if (iRow == 0) {
1,310,244 ✔
3092
          walMeta.skey = rowTs;
870,891 ✔
3093
        }
3094
        if (iRow == nRow - 1) {
1,310,244 ✔
3095
          walMeta.ekey = rowTs;
871,108 ✔
3096
        }
3097

3098
        if (rowTs < target->window.skey || rowTs > target->window.ekey) {
1,310,244 ✔
3099
          continue;
5,334 ✔
3100
        }
3101

3102
        if (numOfRows >= availableRows) {
1,304,910 ✔
3103
          ST_TASK_WLOG("%s truncate row data to slice uid:%" PRId64 ", gid:%" PRIu64 ", ver:%" PRId64
×
3104
                       ", sliceRows:%d, walRows:%" PRIu64 ", currentRowIdx:%d",
3105
                       __func__, submitTbData.uid, target->gid, ver, availableRows, nRow, blockStart);
3106
          continue;
×
3107
        }
3108
        for (int16_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {
8,640,852 ✔
3109
          SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, i);
7,329,225 ✔
3110
          STREAM_CHECK_NULL_GOTO(pColData, terrno);
7,332,696 ✔
3111
          if (pColData->info.colId <= -1) {
7,342,511 ✔
3112
            pColData->hasNull = true;
2,548,473 ✔
3113
            continue;
2,548,473 ✔
3114
          }
3115
          int16_t colId = 0;
4,794,038 ✔
3116
          if (sStreamReaderInfo->isVtableStream) {
4,793,789 ✔
3117
            STREAM_CHECK_RET_GOTO(getColId(submitTbData.suid, submitTbData.uid, i, sStreamReaderInfo, rsp, &colId));
320,903 ✔
3118
            ST_TASK_TLOG("%s vtable colId:%d, i:%d, uid:%" PRId64, __func__, colId, i, submitTbData.uid);
321,100 ✔
3119
          } else {
3120
            colId = pColData->info.colId;
4,472,886 ✔
3121
          }
3122

3123
          SColVal colVal = {0};
4,793,986 ✔
3124
          int32_t sourceIdx = 0;
4,794,052 ✔
3125
          while (1) {
500 ✔
3126
            if (sourceIdx >= schema->numOfCols) {
11,636,121 ✔
3127
              break;
2,032,464 ✔
3128
            }
3129
            STREAM_CHECK_RET_GOTO(tRowGet(pRow, schema, sourceIdx, &colVal));
9,603,657 ✔
3130
            if (colVal.cid == colId) {
9,612,239 ✔
3131
              break;
2,769,970 ✔
3132
            }
3133
            sourceIdx++;
6,842,069 ✔
3134
          }
3135
          if (colVal.cid == colId && COL_VAL_IS_VALUE(&colVal)) {
4,802,634 ✔
3136
            if (IS_VAR_DATA_TYPE(colVal.value.type) || colVal.value.type == TSDB_DATA_TYPE_DECIMAL) {
2,758,857 ✔
3137
              STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, blockStart + numOfRows, (const char*)colVal.value.pData,
19,113 ✔
3138
                                                     colVal.value.nData, !COL_VAL_IS_VALUE(&colVal)));
×
3139
              ST_TASK_TLOG(
19,395 ✔
3140
                  "%s vtable colId:%d, i:%d, colData:%p, data:%s, len:%d, rowIndex:%d, offset:%d, uid:%" PRId64,
3141
                  __func__, colId, i, pColData, (const char*)colVal.value.pData, colVal.value.nData,
3142
                  blockStart + numOfRows, pColData->varmeta.offset[blockStart + numOfRows], submitTbData.uid);
3143
            } else {
3144
              STREAM_CHECK_RET_GOTO(colDataSetVal(pColData, blockStart + numOfRows, (const char*)(&(colVal.value.val)),
2,739,744 ✔
3145
                                                  !COL_VAL_IS_VALUE(&colVal)));
3146
            }
3147
          } else {
3148
            colDataSetNULL(pColData, blockStart + numOfRows);
2,043,777 ✔
3149
          }
3150
        }
3151

3152
        numOfRows++;
1,303,803 ✔
3153
      }
3154

3155
      STREAM_CHECK_RET_GOTO(finishWalDataTarget(sStreamReaderInfo, rsp, gidHash, pBlock, submitTbData.uid, ver, target,
869,735 ✔
3156
                                                &walMeta, pSlice, blockStart, numOfRows));
110 ✔
3157
    }
3158
  }
3159

3160
end:
1,108,579 ✔
3161
  if (code != 0) {
1,108,426 ✔
3162
    ST_TASK_ELOG("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3163
  }
3164
  taosArrayDestroy(targets);
1,108,426 ✔
3165
  tEndDecode(pCoder);
1,108,558 ✔
3166
  return code;
1,107,634 ✔
3167
}
3168
static int32_t scanSubmitData(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo,
1,106,022 ✔
3169
  void* data, int32_t len, SSHashObj* ranges, SSTriggerWalNewRsp* rsp, int64_t ver) {
3170
  int32_t  code = 0;
1,106,022 ✔
3171
  int32_t  lino = 0;
1,106,022 ✔
3172
  SDecoder decoder = {0};
1,106,022 ✔
3173
  SSHashObj* gidHash = NULL;
1,106,088 ✔
3174
  void* pTask = sStreamReaderInfo->pTask;
1,106,088 ✔
3175

3176
  tDecoderInit(&decoder, data, len);
1,106,088 ✔
3177
  if (tStartDecode(&decoder) < 0) {
1,105,561 ✔
3178
    code = TSDB_CODE_INVALID_MSG;
×
3179
    TSDB_CHECK_CODE(code, lino, end);
×
3180
  }
3181

3182
  uint64_t nSubmitTbData = 0;
1,106,355 ✔
3183
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
1,105,559 ✔
3184
    code = TSDB_CODE_INVALID_MSG;
×
3185
    TSDB_CHECK_CODE(code, lino, end);
×
3186
  }
3187

3188
  if (rsp->metaBlock != NULL){
1,105,559 ✔
3189
    gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
632,446 ✔
3190
    STREAM_CHECK_NULL_GOTO(gidHash, terrno);
631,566 ✔
3191
  }
3192

3193
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
2,212,773 ✔
3194
    STREAM_CHECK_RET_GOTO(scanSubmitTbData(pVnode, &decoder, sStreamReaderInfo, ranges, gidHash, rsp, ver));
1,106,879 ✔
3195
  }
3196

3197
  tEndDecode(&decoder);
1,105,894 ✔
3198

3199
  if (rsp->metaBlock != NULL){
1,104,575 ✔
3200
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
631,535 ✔
3201
    int32_t iter = 0;
631,059 ✔
3202
    void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
630,993 ✔
3203
    while (px != NULL) {
1,030,543 ✔
3204
      WalMetaResult* pMeta = (WalMetaResult*)px;
397,426 ✔
3205
      int32_t        rollupTbCount = qStreamGetGroupTableCount(sStreamReaderInfo, pMeta->id);
397,426 ✔
3206
      STREAM_CHECK_RET_GOTO(
397,822 ✔
3207
          buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver, rollupTbCount));
110 ✔
3208
      ((SSDataBlock*)rsp->metaBlock)->info.rows++;
396,956 ✔
3209
      rsp->totalRows++;
396,956 ✔
3210
      ST_TASK_DLOG("%s process meta data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
396,956 ✔
3211
            ", ver:%"PRId64, __func__, pMeta->skey, pMeta->ekey, pMeta->id, ver);
3212
      px = tSimpleHashIterate(gidHash, px, &iter);
397,024 ✔
3213
    }
3214
  }
3215
  
3216

3217
end:
1,105,543 ✔
3218
  tSimpleHashCleanup(gidHash);
1,106,883 ✔
3219
  tDecoderClear(&decoder);
1,106,156 ✔
3220
  return code;
1,106,750 ✔
3221
}
3222

3223
static int32_t scanSubmitTbDataPre(SDecoder *pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo, SSHashObj* ranges, 
1,487,809 ✔
3224
  uint64_t* gid, int64_t* uid, int32_t* numOfRows, SSTriggerWalNewRsp* rsp, int64_t ver) {
3225
  int32_t code = 0;
1,487,809 ✔
3226
  int32_t lino = 0;
1,487,809 ✔
3227
  void* pTask = sStreamReaderInfo->pTask;
1,487,809 ✔
3228
  SArray* targets = NULL;
1,487,809 ✔
3229
  bool    expandRollupGids = isRollupMultiReader(sStreamReaderInfo);
1,487,809 ✔
3230

3231
  if (tStartDecode(pCoder) < 0) {
1,487,281 ✔
3232
    code = TSDB_CODE_INVALID_MSG;
×
3233
    TSDB_CHECK_CODE(code, lino, end);
×
3234
  }
3235

3236
  SSubmitTbData submitTbData = {0};
1,489,598 ✔
3237
  uint8_t       version = 0;
1,489,796 ✔
3238
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
1,487,142 ✔
3239
    code = TSDB_CODE_INVALID_MSG;
×
3240
    TSDB_CHECK_CODE(code, lino, end);
×
3241
  }
3242
  version = (submitTbData.flags >> 8) & 0xff;
1,487,142 ✔
3243
  submitTbData.flags = submitTbData.flags & 0xff;
1,487,142 ✔
3244

3245
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
3246
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
1,487,142 ✔
3247
    submitTbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
237,285 ✔
3248
    STREAM_CHECK_NULL_GOTO(submitTbData.pCreateTbReq, terrno);
237,554 ✔
3249
    STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbReq(pCoder, submitTbData.pCreateTbReq));
237,554 ✔
3250
    STREAM_CHECK_RET_GOTO(processAutoCreateTableNew(sStreamReaderInfo, submitTbData.pCreateTbReq, ver));
237,424 ✔
3251
  }
3252

3253
  // submit data
3254
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
1,487,476 ✔
3255
    code = TSDB_CODE_INVALID_MSG;
×
3256
    TSDB_CHECK_CODE(code, lino, end);
×
3257
  }
3258
  if (tDecodeI64(pCoder, uid) < 0) {
1,487,543 ✔
3259
    code = TSDB_CODE_INVALID_MSG;
×
3260
    TSDB_CHECK_CODE(code, lino, end);
×
3261
  }
3262
  ST_TASK_DLOG("%s uid:%" PRId64 ", suid:%" PRId64, __func__, *uid, submitTbData.suid);
1,487,543 ✔
3263
  STimeWindow window = {.skey = INT64_MIN, .ekey = INT64_MAX};
1,487,409 ✔
3264

3265
  if (expandRollupGids) {
1,487,674 ✔
3266
    targets = taosArrayInit(4, sizeof(SWalDataTarget));
29,407 ✔
3267
    STREAM_CHECK_NULL_GOTO(targets, terrno);
29,407 ✔
3268
    STREAM_CHECK_RET_GOTO(collectWalRollupTargets(sStreamReaderInfo, ranges, submitTbData.suid, *uid, targets));
29,407 ✔
3269
    STREAM_CHECK_CONDITION_GOTO(taosArrayGetSize(targets) == 0, TDB_CODE_SUCCESS);
29,407 ✔
3270
  } else {
3271
    STREAM_CHECK_CONDITION_GOTO(!uidInTableListSet(sStreamReaderInfo, submitTbData.suid, *uid, gid, rsp->isCalc),
1,458,267 ✔
3272
                                TDB_CODE_SUCCESS);
110 ✔
3273

3274
    if (ranges != NULL) {
840,899 ✔
3275
      void* timerange = tSimpleHashGet(ranges, gid, sizeof(*gid));
445,638 ✔
3276
      if (timerange == NULL) goto end;
445,440 ✔
3277
      int64_t* pRange = (int64_t*)timerange;
444,746 ✔
3278
      window.skey = pRange[0];
444,746 ✔
3279
      window.ekey = pRange[1];
444,746 ✔
3280
    }
3281
  }
3282

3283
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
870,144 ✔
3284
    code = TSDB_CODE_INVALID_MSG;
×
3285
    TSDB_CHECK_CODE(code, lino, end);
×
3286
  }
3287

3288
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
870,144 ✔
3289
    uint64_t nColData = 0;
81 ✔
3290
    if (tDecodeU64v(pCoder, &nColData) < 0) {
81 ✔
3291
      code = TSDB_CODE_INVALID_MSG;
×
3292
      TSDB_CHECK_CODE(code, lino, end);
×
3293
    }
3294

3295
    SColData colData = {0};
81 ✔
3296
    code = tDecodeColData(version, pCoder, &colData, false);
81 ✔
3297
    if (code) {
81 ✔
3298
      code = TSDB_CODE_INVALID_MSG;
×
3299
      TSDB_CHECK_CODE(code, lino, end);
×
3300
    }
3301

3302
    if (colData.flag != HAS_VALUE) {
81 ✔
3303
      code = TSDB_CODE_INVALID_MSG;
×
3304
      TSDB_CHECK_CODE(code, lino, end);
×
3305
    }
3306
    int32_t rowStart = 0;
81 ✔
3307
    int32_t rowEnd = 0;
81 ✔
3308
    if (expandRollupGids) {
81 ✔
3309
      int32_t numOfTargets = taosArrayGetSize(targets);
×
3310
      for (int32_t i = 0; i < numOfTargets; ++i) {
×
3311
        SWalDataTarget* target = taosArrayGet(targets, i);
×
3312
        STREAM_CHECK_NULL_GOTO(target, terrno);
×
3313
        int32_t targetRows = 0;
×
3314
        if (target->window.skey != INT64_MIN || target->window.ekey != INT64_MAX) {
×
3315
          STREAM_CHECK_RET_GOTO(getRowRange(&colData, &target->window, &rowStart, &rowEnd, &targetRows));
×
3316
        } else {
3317
          targetRows = colData.nVal;
×
3318
        }
3319
        STREAM_CHECK_RET_GOTO(addWalDataSlice(sStreamReaderInfo, rsp, *uid, target->gid, targetRows, ver));
×
3320
        *gid = target->gid;
×
3321
        *numOfRows += targetRows;
×
3322
      }
3323
    } else if (window.skey != INT64_MIN || window.ekey != INT64_MAX) {
81 ✔
3324
      STREAM_CHECK_RET_GOTO(getRowRange(&colData, &window, &rowStart, &rowEnd, numOfRows));
81 ✔
3325
    } else {
3326
      (*numOfRows) = colData.nVal;
×
3327
    }
3328
  } else {
3329
    uint64_t nRow = 0;
870,063 ✔
3330
    if (tDecodeU64v(pCoder, &nRow) < 0) {
869,402 ✔
3331
      code = TSDB_CODE_INVALID_MSG;
×
3332
      TSDB_CHECK_CODE(code, lino, end);
×
3333
    }
3334

3335
    if (expandRollupGids) {
869,402 ✔
3336
      int32_t rowsPos = pCoder->pos;
29,340 ✔
3337
      int32_t numOfTargets = taosArrayGetSize(targets);
29,340 ✔
3338
      for (int32_t i = 0; i < numOfTargets; ++i) {
59,395 ✔
3339
        SWalDataTarget* target = taosArrayGet(targets, i);
30,055 ✔
3340
        STREAM_CHECK_NULL_GOTO(target, terrno);
30,055 ✔
3341
        int32_t targetRows = 0;
30,055 ✔
3342
        pCoder->pos = rowsPos;
30,055 ✔
3343
        for (uint64_t iRow = 0; iRow < nRow; ++iRow) {
60,646 ✔
3344
          SRow* pRow = (SRow*)(pCoder->data + pCoder->pos);
30,591 ✔
3345
          pCoder->pos += pRow->len;
30,591 ✔
3346
          if ((target->window.skey != INT64_MIN || target->window.ekey != INT64_MAX) &&
30,591 ✔
3347
              (pRow->ts < target->window.skey || pRow->ts > target->window.ekey)) {
29,217 ✔
3348
            continue;
134 ✔
3349
          }
3350
          targetRows++;
30,457 ✔
3351
        }
3352
        STREAM_CHECK_RET_GOTO(addWalDataSlice(sStreamReaderInfo, rsp, *uid, target->gid, targetRows, ver));
30,055 ✔
3353
        *gid = target->gid;
30,055 ✔
3354
        *numOfRows += targetRows;
30,055 ✔
3355
      }
3356
    } else if (window.skey != INT64_MIN || window.ekey != INT64_MAX) {
840,062 ✔
3357
      for (uint64_t iRow = 0; iRow < nRow; ++iRow) {
1,248,271 ✔
3358
        SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
803,805 ✔
3359
        pCoder->pos += pRow->len;
803,805 ✔
3360
        if (pRow->ts < window.skey || pRow->ts > window.ekey) {
803,805 ✔
3361
          continue;
5,200 ✔
3362
        }
3363
        (*numOfRows)++;
798,605 ✔
3364
      }
3365
    } else {
3366
      (*numOfRows) = nRow;
395,596 ✔
3367
    }
3368
  }
3369
  
3370
end:
1,488,868 ✔
3371
  taosArrayDestroy(targets);
1,486,822 ✔
3372
  tdDestroySVCreateTbReq(submitTbData.pCreateTbReq);
1,486,026 ✔
3373
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
1,486,278 ✔
3374
  tEndDecode(pCoder);
1,486,077 ✔
3375
  return code;
1,486,226 ✔
3376
}
3377

3378
static int32_t scanSubmitDataPre(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len, SSHashObj* ranges, SSTriggerWalNewRsp* rsp, int64_t ver) {
1,486,046 ✔
3379
  int32_t  code = 0;
1,486,046 ✔
3380
  int32_t  lino = 0;
1,486,046 ✔
3381
  SDecoder decoder = {0};
1,486,046 ✔
3382
  void* pTask = sStreamReaderInfo->pTask;
1,486,046 ✔
3383

3384
  tDecoderInit(&decoder, data, len);
1,486,046 ✔
3385
  if (tStartDecode(&decoder) < 0) {
1,484,485 ✔
3386
    code = TSDB_CODE_INVALID_MSG;
×
3387
    TSDB_CHECK_CODE(code, lino, end);
×
3388
  }
3389

3390
  uint64_t nSubmitTbData = 0;
1,487,851 ✔
3391
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
1,486,929 ✔
3392
    code = TSDB_CODE_INVALID_MSG;
×
3393
    TSDB_CHECK_CODE(code, lino, end);
×
3394
  }
3395
  ST_TASK_DLOG("%s nSubmitTbData:%" PRIu64 ", ver:%"PRId64 " bodyLen:%d", __func__, nSubmitTbData, ver, len);
1,486,929 ✔
3396

3397
  bool expandRollupGids = isRollupMultiReader(sStreamReaderInfo);
1,487,288 ✔
3398
  for (int32_t i = 0; i < nSubmitTbData; i++) {
2,975,336 ✔
3399
    uint64_t gid = -1;
1,488,734 ✔
3400
    int64_t  uid = 0;
1,488,602 ✔
3401
    int32_t numOfRows = 0;
1,488,602 ✔
3402
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataPre(&decoder, sStreamReaderInfo, ranges, &gid, &uid, &numOfRows, rsp, ver));
1,488,734 ✔
3403
    if (numOfRows <= 0) {
1,488,469 ✔
3404
      ST_TASK_DLOG("%s no valid data uid:%" PRId64 ", gid:%" PRIu64 ", numOfRows:%d, ver:%"PRId64, __func__, uid, gid, numOfRows, ver);
619,451 ✔
3405
      continue;
619,648 ✔
3406
    }
3407
    if (!expandRollupGids) {
869,018 ✔
3408
      STREAM_CHECK_RET_GOTO(addWalDataSlice(sStreamReaderInfo, rsp, uid, gid, numOfRows, ver));
839,745 ✔
3409
    }
3410
  }
3411

3412
  tEndDecode(&decoder);
1,486,602 ✔
3413

3414
end:
1,484,687 ✔
3415
  tDecoderClear(&decoder);
1,484,885 ✔
3416
  return code;
1,486,802 ✔
3417
}
3418

3419
static void buildIndexHash(SSHashObj* indexHash, void* pTask){
141,444 ✔
3420
  void*   pe = NULL;
141,444 ✔
3421
  int32_t iter = 0;
141,444 ✔
3422
  int32_t index = 0;
141,444 ✔
3423
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
302,171 ✔
3424
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
160,662 ✔
3425
    pInfo->startRowIdx = index;
160,662 ✔
3426
    pInfo->currentRowIdx = index;
160,662 ✔
3427
    index += pInfo->numRows;
160,662 ✔
3428
    ST_TASK_DLOG("%s uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", __func__, pInfo->uid, pInfo->gId,
160,802 ✔
3429
                 pInfo->startRowIdx, pInfo->numRows);
3430
  }
3431
}
141,444 ✔
3432

3433
static void printIndexHash(SSHashObj* indexHash, void* pTask){
141,365 ✔
3434
  if (qDebugFlag & DEBUG_TRACE) {
141,365 ✔
3435
    void*   pe = NULL;
1,913 ✔
3436
    int32_t iter = 0;
1,913 ✔
3437
    while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
4,759 ✔
3438
      SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
2,781 ✔
3439
      ST_TASK_TLOG("%s uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", __func__, pInfo->uid, pInfo->gId,
2,921 ✔
3440
                   pInfo->startRowIdx, pInfo->numRows);
3441
    }
3442
  }
3443
}
141,365 ✔
3444

3445
static void filterIndexHash(SSHashObj* indexHash, SColumnInfoData* pRet){
1,764 ✔
3446
  void*   pe = NULL;
1,764 ✔
3447
  int32_t iter = 0;
1,764 ✔
3448
  int32_t index = 0;
1,764 ✔
3449
  int32_t pIndex = 0;
1,764 ✔
3450
  int8_t* pIndicator = (int8_t*)pRet->pData;
1,764 ✔
3451
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
4,053 ✔
3452
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
2,289 ✔
3453
    pInfo->startRowIdx = index;
2,289 ✔
3454
    int32_t size = pInfo->numRows;
2,289 ✔
3455
    for (int32_t i = 0; i < pInfo->numRows; i++) {
22,579 ✔
3456
      if (pIndicator && !pIndicator[pIndex++]) {
20,290 ✔
3457
        size--;
6,655 ✔
3458
      }
3459
    }
3460
    pInfo->numRows = size;
2,289 ✔
3461
    index += pInfo->numRows;
2,289 ✔
3462
    stTrace("stream reader re build index hash uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d",
2,289 ✔
3463
            pInfo->uid, pInfo->gId, pInfo->startRowIdx, pInfo->numRows);
3464
  }
3465
}
1,831 ✔
3466

3467
static int32_t prepareIndexMetaData(SWalReader* pWalReader, SStreamTriggerReaderInfo* sStreamReaderInfo,
1,416,680 ✔
3468
                                    SSTriggerWalNewRsp* resultRsp, int64_t endVer) {
3469
  int32_t      code = 0;
1,416,680 ✔
3470
  int32_t      lino = 0;
1,416,680 ✔
3471
  void* pTask = sStreamReaderInfo->pTask;
1,416,680 ✔
3472

3473
  code = walReaderSeekVer(pWalReader, resultRsp->ver);
1,416,680 ✔
3474
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
1,416,893 ✔
3475
    if (resultRsp->ver < walGetFirstVer(pWalReader->pWal)) {
1,259,270 ✔
3476
      resultRsp->ver = walGetFirstVer(pWalReader->pWal);
×
3477
      resultRsp->verTime = 0;
×
3478
    } else {
3479
      resultRsp->verTime = taosGetTimestampUs();
1,258,241 ✔
3480
    }
3481
    ST_TASK_DLOG("%s scan wal end:%s",  __func__, tstrerror(code));
1,258,241 ✔
3482
    code = TSDB_CODE_SUCCESS;
1,258,739 ✔
3483
    goto end;
1,258,739 ✔
3484
  }
3485
  STREAM_CHECK_RET_GOTO(code);
157,623 ✔
3486

3487
  while (1) {
1,051,043 ✔
3488
    code = walNextValidMsg(pWalReader, true);
1,208,396 ✔
3489
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
1,205,933 ✔
3490
      resultRsp->verTime = taosGetTimestampUs();
155,431 ✔
3491
      ST_TASK_DLOG("%s scan wal end:%s", __func__, tstrerror(code));
155,431 ✔
3492
      code = TSDB_CODE_SUCCESS;
155,538 ✔
3493
      goto end;
155,538 ✔
3494
    }
3495
    STREAM_CHECK_RET_GOTO(code);
1,051,098 ✔
3496
    SWalCont* wCont = &pWalReader->pHead->head;
1,051,098 ✔
3497
    if (endVer > 0 && wCont->version >= endVer) {
1,051,098 ✔
3498
      resultRsp->ver = endVer;
2,016 ✔
3499
      break;
2,016 ✔
3500
    }
3501
    resultRsp->ver = pWalReader->curVersion;
1,049,082 ✔
3502
    resultRsp->verTime = wCont->ingestTs;
1,049,082 ✔
3503
    void*   data = POINTER_SHIFT(walContBody(wCont), sizeof(SMsgHead));
1,049,082 ✔
3504
    int32_t len = walContBodyLen(wCont) - sizeof(SMsgHead);
1,049,082 ✔
3505
    int64_t ver = wCont->version;
1,049,082 ✔
3506
    ST_TASK_DLOG("%s scan wal ver:%" PRId64 ", type:%s, deleteData:%d, deleteTb:%d, msg len:%d", __func__,
1,049,082 ✔
3507
      ver, TMSG_INFO(wCont->msgType), sStreamReaderInfo->deleteReCalc, sStreamReaderInfo->deleteOutTbl, len);
3508
    if (wCont->msgType == TDMT_VND_SUBMIT) {
1,049,821 ✔
3509
      // return when getting data if there are meta data in vtable scan
3510
      if (shouldStopBeforeSubmitAfterTableChange(sStreamReaderInfo, resultRsp)) {
1,012,506 ✔
3511
        resultRsp->ver--;
×
3512
        break;
×
3513
      }
3514
      data = POINTER_SHIFT(walContBody(wCont), sizeof(SSubmitReq2Msg));
1,012,308 ✔
3515
      len = walContBodyLen(wCont) - sizeof(SSubmitReq2Msg);
1,012,308 ✔
3516
      STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, data, len, NULL, resultRsp, ver));
1,012,308 ✔
3517
    } else {
3518
      STREAM_CHECK_RET_GOTO(processMeta(wCont->msgType, sStreamReaderInfo, data, len, resultRsp, ver));
37,315 ✔
3519
    }
3520

3521
    ST_TASK_DLOG("%s scan wal next ver:%" PRId64 ", totalRows:%d", __func__, resultRsp->ver, resultRsp->totalRows);
1,050,021 ✔
3522
    if (resultRsp->totalRows >= STREAM_RETURN_ROWS_NUM || resultRsp->needReturn) {
1,050,110 ✔
3523
      break;
3524
    }
3525
  }
3526
  
3527
end:
×
3528
  STREAM_PRINT_LOG_END(code, lino);
1,415,630 ✔
3529
  return code;
1,415,768 ✔
3530
}
3531

3532
static int32_t prepareIndexData(SWalReader* pWalReader, SStreamTriggerReaderInfo* sStreamReaderInfo, 
242,010 ✔
3533
  SArray* versions, SSHashObj* ranges, SSTriggerWalNewRsp* rsp){
3534
  int32_t      code = 0;
242,010 ✔
3535
  int32_t      lino = 0;
242,010 ✔
3536
  void* pTask = sStreamReaderInfo->pTask;
242,010 ✔
3537

3538
  for(int32_t i = 0; i < taosArrayGetSize(versions); i++) {
715,842 ✔
3539
    int64_t *ver = taosArrayGet(versions, i);
473,692 ✔
3540
    if (ver == NULL) continue;
473,753 ✔
3541

3542
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, *ver));
473,753 ✔
3543
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
473,885 ✔
3544
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
×
3545
      ST_TASK_TLOG("%s not data, skip, ver:%"PRId64, __func__, *ver);
×
3546
      continue;
×
3547
    }
3548
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
473,885 ✔
3549

3550
    SWalCont* wCont = &pWalReader->pHead->head;
473,542 ✔
3551
    void*   pBody = POINTER_SHIFT(walContBody(wCont), sizeof(SSubmitReq2Msg));
473,542 ✔
3552
    int32_t bodyLen = walContBodyLen(wCont) - sizeof(SSubmitReq2Msg);
473,542 ✔
3553

3554
    STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, pBody, bodyLen, ranges, rsp, *ver));
473,542 ✔
3555
  }
3556
  
3557
end:
242,071 ✔
3558
  return code;
242,071 ✔
3559
}
3560

3561
static int32_t filterData(SSTriggerWalNewRsp* resultRsp, SStreamTriggerReaderInfo* sStreamReaderInfo) {
141,444 ✔
3562
  int32_t      code = 0;
141,444 ✔
3563
  int32_t       lino = 0;
141,444 ✔
3564
  SColumnInfoData* pRet = NULL;
141,444 ✔
3565

3566
  int64_t totalRows = ((SSDataBlock*)resultRsp->dataBlock)->info.rows;
141,444 ✔
3567
  STREAM_CHECK_RET_GOTO(qStreamFilter(((SSDataBlock*)resultRsp->dataBlock), sStreamReaderInfo->pFilterInfo, &pRet));
141,444 ✔
3568

3569
  if (((SSDataBlock*)resultRsp->dataBlock)->info.rows < totalRows) {
141,444 ✔
3570
    filterIndexHash(resultRsp->indexHash, pRet);
1,831 ✔
3571
  }
3572

3573
end:
141,444 ✔
3574
  colDataDestroy(pRet);
141,444 ✔
3575
  taosMemoryFree(pRet);
141,444 ✔
3576
  return code;
141,444 ✔
3577
}
3578

3579
static int32_t processWalVerMetaDataNew(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo,
1,416,299 ✔
3580
                                        SSTriggerWalNewRsp* resultRsp, int64_t endVer) {
3581
  int32_t      code = 0;
1,416,299 ✔
3582
  int32_t      lino = 0;
1,416,299 ✔
3583
  void* pTask = sStreamReaderInfo->pTask;
1,416,299 ✔
3584
                                        
3585
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
1,416,299 ✔
3586
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
1,415,841 ✔
3587
  blockDataEmpty(resultRsp->dataBlock);
1,415,841 ✔
3588
  blockDataEmpty(resultRsp->metaBlock);
1,416,359 ✔
3589
  int64_t lastVer = resultRsp->ver;
1,414,199 ✔
3590
  STREAM_CHECK_RET_GOTO(prepareIndexMetaData(pWalReader, sStreamReaderInfo, resultRsp, endVer));
1,414,199 ✔
3591
  STREAM_CHECK_CONDITION_GOTO(resultRsp->totalRows == 0, TDB_CODE_SUCCESS);
1,415,771 ✔
3592

3593
  buildIndexHash(resultRsp->indexHash, pTask);
54,869 ✔
3594
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(((SSDataBlock*)resultRsp->dataBlock), resultRsp->totalRows));
54,869 ✔
3595
  while(lastVer < resultRsp->ver) {
700,067 ✔
3596
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, lastVer++));
645,411 ✔
3597
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
645,613 ✔
3598
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
12,627 ✔
3599
      continue;
12,627 ✔
3600
    }
3601
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
632,986 ✔
3602
    SWalCont* wCont = &pWalReader->pHead->head;
632,231 ✔
3603
    void*   pBody = POINTER_SHIFT(walContBody(wCont), sizeof(SSubmitReq2Msg));
632,231 ✔
3604
    int32_t bodyLen = walContBodyLen(wCont) - sizeof(SSubmitReq2Msg);
632,231 ✔
3605
    ST_TASK_DLOG("process wal ver:%" PRId64 ", type:%d, bodyLen:%d", wCont->version, wCont->msgType, bodyLen);
632,231 ✔
3606
    STREAM_CHECK_RET_GOTO(scanSubmitData(pVnode, sStreamReaderInfo, pBody, bodyLen, NULL, resultRsp, wCont->version));
633,388 ✔
3607
  }
3608

3609
  int32_t metaRows = resultRsp->totalRows - ((SSDataBlock*)resultRsp->dataBlock)->info.rows;
54,656 ✔
3610
  STREAM_CHECK_RET_GOTO(filterData(resultRsp, sStreamReaderInfo));
54,656 ✔
3611
  resultRsp->totalRows = ((SSDataBlock*)resultRsp->dataBlock)->info.rows + metaRows;
54,869 ✔
3612

3613
end:
1,415,771 ✔
3614
  ST_TASK_DLOG("vgId:%d %s end, get result totalRows:%d, process:%"PRId64"/%"PRId64, TD_VID(pVnode), __func__, 
1,415,771 ✔
3615
          resultRsp->totalRows, resultRsp->ver, walGetAppliedVer(pWalReader->pWal));
3616
  walCloseReader(pWalReader);
1,415,705 ✔
3617
  return code;
1,415,950 ✔
3618
}
3619

3620
static int32_t processWalVerDataNew(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo, 
242,010 ✔
3621
                                    SArray* versions, SSHashObj* ranges, SSTriggerWalNewRsp* rsp) {
3622
  int32_t      code = 0;
242,010 ✔
3623
  int32_t      lino = 0;
242,010 ✔
3624

3625
  void* pTask = sStreamReaderInfo->pTask;
242,010 ✔
3626
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
242,010 ✔
3627
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
241,944 ✔
3628
  
3629
  if (taosArrayGetSize(versions) > 0) {
241,944 ✔
3630
    rsp->ver = *(int64_t*)taosArrayGetLast(versions);
86,575 ✔
3631
  }
3632
  
3633
  STREAM_CHECK_RET_GOTO(prepareIndexData(pWalReader, sStreamReaderInfo, versions, ranges, rsp));
242,010 ✔
3634
  STREAM_CHECK_CONDITION_GOTO(rsp->totalRows == 0, TDB_CODE_SUCCESS);
242,071 ✔
3635

3636
  ST_TASK_TLOG("%s index hash:%p %d", __func__, rsp->indexHash, tSimpleHashGetSize(rsp->indexHash));
86,575 ✔
3637
  buildIndexHash(rsp->indexHash, pTask);
86,575 ✔
3638

3639
  blockDataEmpty(rsp->dataBlock);
86,575 ✔
3640
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->dataBlock, rsp->totalRows));
86,575 ✔
3641

3642
  for(int32_t i = 0; i < taosArrayGetSize(versions); i++) {
560,341 ✔
3643
    int64_t *ver = taosArrayGet(versions, i);
473,634 ✔
3644
    if (ver == NULL) continue;
473,766 ✔
3645
    ST_TASK_TLOG("vgId:%d %s scan wal process:%"PRId64"/%"PRId64, TD_VID(pVnode), __func__, *ver, walGetAppliedVer(pWalReader->pWal));
473,766 ✔
3646

3647
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, *ver));
473,766 ✔
3648
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
473,832 ✔
3649
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
×
3650
      continue;
×
3651
    }
3652
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
473,832 ✔
3653
    SWalCont* wCont = &pWalReader->pHead->head;
473,634 ✔
3654
    void*   pBody = POINTER_SHIFT(walContBody(wCont), sizeof(SSubmitReq2Msg));
473,634 ✔
3655
    int32_t bodyLen = walContBodyLen(wCont) - sizeof(SSubmitReq2Msg);
473,634 ✔
3656

3657
    STREAM_CHECK_RET_GOTO(scanSubmitData(pVnode, sStreamReaderInfo, pBody, bodyLen, ranges, rsp, wCont->version));
473,634 ✔
3658
  }
3659
  // printDataBlock(rsp->dataBlock, __func__, "processWalVerDataNew");
3660
  STREAM_CHECK_RET_GOTO(filterData(rsp, sStreamReaderInfo));
86,575 ✔
3661
  rsp->totalRows = ((SSDataBlock*)rsp->dataBlock)->info.rows;
86,575 ✔
3662

3663
end:
242,071 ✔
3664
  ST_TASK_DLOG("vgId:%d %s end, get result totalRows:%d, process:%"PRId64"/%"PRId64, TD_VID(pVnode), __func__, 
242,071 ✔
3665
            rsp->totalRows, rsp->ver, walGetAppliedVer(pWalReader->pWal));
3666
  walCloseReader(pWalReader);
242,132 ✔
3667
  return code;
242,005 ✔
3668
}
3669

3670
static int32_t buildScheamFromMeta(SVnode* pVnode, int64_t uid, SArray** schemas, SStorageAPI* api) {
12,398 ✔
3671
  int32_t code = 0;
12,398 ✔
3672
  int32_t lino = 0;
12,398 ✔
3673
  SMetaReader metaReader = {0};
12,398 ✔
3674
  *schemas = taosArrayInit(8, sizeof(SSchema));
12,398 ✔
3675
  STREAM_CHECK_NULL_GOTO(*schemas, terrno);
12,398 ✔
3676
  
3677
  api->metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &api->metaFn, 0);
12,398 ✔
3678
  STREAM_CHECK_RET_GOTO(api->metaReaderFn.getTableEntryByUid(&metaReader, uid));
12,398 ✔
3679

3680
  SSchemaWrapper* sSchemaWrapper = NULL;
12,398 ✔
3681
  if (metaReader.me.type == TD_CHILD_TABLE) {
12,398 ✔
3682
    int64_t suid = metaReader.me.ctbEntry.suid;
12,398 ✔
3683
    tDecoderClear(&metaReader.coder);
12,398 ✔
3684
    STREAM_CHECK_RET_GOTO(api->metaReaderFn.getTableEntryByUid(&metaReader, suid));
12,398 ✔
3685
    sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
12,398 ✔
3686
  } else if (metaReader.me.type == TD_NORMAL_TABLE) {
×
3687
    sSchemaWrapper = &metaReader.me.ntbEntry.schemaRow;
×
3688
  } else {
3689
    qError("invalid table type:%d", metaReader.me.type);
×
3690
  }
3691

3692
  for (size_t j = 0; j < sSchemaWrapper->nCols; j++) {
57,786 ✔
3693
    SSchema* s = sSchemaWrapper->pSchema + j;
45,388 ✔
3694
    STREAM_CHECK_NULL_GOTO(taosArrayPush(*schemas, s), terrno);
90,776 ✔
3695
  }
3696

3697
end:
12,398 ✔
3698
  api->metaReaderFn.clearReader(&metaReader);
12,398 ✔
3699
  STREAM_PRINT_LOG_END(code, lino);
12,398 ✔
3700
  if (code != 0)  {
12,398 ✔
3701
    taosArrayDestroy(*schemas);
×
3702
    *schemas = NULL;
×
3703
  }
3704
  return code;
12,398 ✔
3705
}
3706

3707
static int32_t shrinkScheams(SArray* cols, SArray* schemas) {
12,398 ✔
3708
  int32_t code = 0;
12,398 ✔
3709
  int32_t lino = 0;
12,398 ✔
3710
  size_t  schemaLen = taosArrayGetSize(schemas);
12,398 ✔
3711
  STREAM_CHECK_RET_GOTO(taosArrayEnsureCap(schemas, schemaLen + taosArrayGetSize(cols)));
12,398 ✔
3712
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
38,118 ✔
3713
    col_id_t* id = taosArrayGet(cols, i);
25,720 ✔
3714
    STREAM_CHECK_NULL_GOTO(id, terrno);
25,720 ✔
3715
    for (size_t i = 0; i < schemaLen; i++) {
48,876 ✔
3716
      SSchema* s = taosArrayGet(schemas, i);
48,876 ✔
3717
      STREAM_CHECK_NULL_GOTO(s, terrno);
48,876 ✔
3718
      if (*id == s->colId) {
48,876 ✔
3719
        STREAM_CHECK_NULL_GOTO(taosArrayPush(schemas, s), terrno);
25,720 ✔
3720
        break;
25,720 ✔
3721
      }
3722
    }
3723
  }
3724
  taosArrayPopFrontBatch(schemas, schemaLen);
12,398 ✔
3725

3726
end:
12,398 ✔
3727
  return code;
12,398 ✔
3728
}
3729

3730
static int32_t createTSAndCondition(int64_t start, int64_t end, SLogicConditionNode** pCond,
×
3731
                                    STargetNode* pTargetNodeTs) {
3732
  int32_t code = 0;
×
3733
  int32_t lino = 0;
×
3734

3735
  SColumnNode*         pCol = NULL;
×
3736
  SColumnNode*         pCol1 = NULL;
×
3737
  SValueNode*          pVal = NULL;
×
3738
  SValueNode*          pVal1 = NULL;
×
3739
  SOperatorNode*       op = NULL;
×
3740
  SOperatorNode*       op1 = NULL;
×
3741
  SLogicConditionNode* cond = NULL;
×
3742

3743
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pCol));
×
3744
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
×
3745
  pCol->node.resType.type = TSDB_DATA_TYPE_TIMESTAMP;
×
3746
  pCol->node.resType.bytes = LONG_BYTES;
×
3747
  pCol->slotId = pTargetNodeTs->slotId;
×
3748
  pCol->dataBlockId = pTargetNodeTs->dataBlockId;
×
3749

3750
  STREAM_CHECK_RET_GOTO(nodesCloneNode((SNode*)pCol, (SNode**)&pCol1));
×
3751

3752
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&pVal));
×
3753
  pVal->node.resType.type = TSDB_DATA_TYPE_BIGINT;
×
3754
  pVal->node.resType.bytes = LONG_BYTES;
×
3755
  pVal->datum.i = start;
×
3756
  pVal->typeData = start;
×
3757

3758
  STREAM_CHECK_RET_GOTO(nodesCloneNode((SNode*)pVal, (SNode**)&pVal1));
×
3759
  pVal1->datum.i = end;
×
3760
  pVal1->typeData = end;
×
3761

3762
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op));
×
3763
  op->opType = OP_TYPE_GREATER_EQUAL;
×
3764
  op->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
3765
  op->node.resType.bytes = CHAR_BYTES;
×
3766
  op->pLeft = (SNode*)pCol;
×
3767
  op->pRight = (SNode*)pVal;
×
3768
  pCol = NULL;
×
3769
  pVal = NULL;
×
3770

3771
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op1));
×
3772
  op1->opType = OP_TYPE_LOWER_EQUAL;
×
3773
  op1->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
3774
  op1->node.resType.bytes = CHAR_BYTES;
×
3775
  op1->pLeft = (SNode*)pCol1;
×
3776
  op1->pRight = (SNode*)pVal1;
×
3777
  pCol1 = NULL;
×
3778
  pVal1 = NULL;
×
3779

3780
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond));
×
3781
  cond->condType = LOGIC_COND_TYPE_AND;
×
3782
  cond->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
3783
  cond->node.resType.bytes = CHAR_BYTES;
×
3784
  STREAM_CHECK_RET_GOTO(nodesMakeList(&cond->pParameterList));
×
3785
  STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)op));
×
3786
  op = NULL;
×
3787
  STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)op1));
×
3788
  op1 = NULL;
×
3789

3790
  *pCond = cond;
×
3791

3792
end:
×
3793
  if (code != 0) {
×
3794
    nodesDestroyNode((SNode*)pCol);
×
3795
    nodesDestroyNode((SNode*)pCol1);
×
3796
    nodesDestroyNode((SNode*)pVal);
×
3797
    nodesDestroyNode((SNode*)pVal1);
×
3798
    nodesDestroyNode((SNode*)op);
×
3799
    nodesDestroyNode((SNode*)op1);
×
3800
    nodesDestroyNode((SNode*)cond);
×
3801
  }
3802
  STREAM_PRINT_LOG_END(code, lino);
×
3803

3804
  return code;
×
3805
}
3806

3807
/*
3808
static int32_t createExternalConditions(SStreamRuntimeFuncInfo* data, SLogicConditionNode** pCond, STargetNode* pTargetNodeTs, STimeRangeNode* node) {
3809
  int32_t              code = 0;
3810
  int32_t              lino = 0;
3811
  SLogicConditionNode* pAndCondition = NULL;
3812
  SLogicConditionNode* cond = NULL;
3813

3814
  if (pTargetNodeTs == NULL) {
3815
    vError("stream reader %s no ts column", __func__);
3816
    return TSDB_CODE_STREAM_NOT_TABLE_SCAN_PLAN;
3817
  }
3818
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond));
3819
  cond->condType = LOGIC_COND_TYPE_OR;
3820
  cond->node.resType.type = TSDB_DATA_TYPE_BOOL;
3821
  cond->node.resType.bytes = CHAR_BYTES;
3822
  STREAM_CHECK_RET_GOTO(nodesMakeList(&cond->pParameterList));
3823

3824
  for (int i = 0; i < taosArrayGetSize(data->pStreamPesudoFuncVals); ++i) {
3825
    data->curIdx = i;
3826

3827
    SReadHandle handle = {0};
3828
    calcTimeRange(node, data, &handle.winRange, &handle.winRangeValid);
3829
    if (!handle.winRangeValid) {
3830
      stError("stream reader %s invalid time range, skey:%" PRId64 ", ekey:%" PRId64, __func__, handle.winRange.skey,
3831
              handle.winRange.ekey);
3832
      continue;
3833
    }
3834
    STREAM_CHECK_RET_GOTO(createTSAndCondition(handle.winRange.skey, handle.winRange.ekey, &pAndCondition, pTargetNodeTs));
3835
    stDebug("%s create condition skey:%" PRId64 ", eksy:%" PRId64, __func__, handle.winRange.skey, handle.winRange.ekey);
3836
    STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)pAndCondition));
3837
    pAndCondition = NULL;
3838
  }
3839

3840
  *pCond = cond;
3841

3842
end:
3843
  if (code != 0) {
3844
    nodesDestroyNode((SNode*)pAndCondition);
3845
    nodesDestroyNode((SNode*)cond);
3846
  }
3847
  STREAM_PRINT_LOG_END(code, lino);
3848

3849
  return code;
3850
}
3851
*/
3852

3853
static int32_t processCalaTimeRange(SStreamTriggerReaderCalcInfo* sStreamReaderCalcInfo, SResFetchReq* req,
124,223 ✔
3854
                                    STimeRangeNode* node, SReadHandle* handle, bool isExtWin, int32_t actualNodeId) {
3855
  int32_t code = 0;
124,223 ✔
3856
  int32_t lino = 0;
124,223 ✔
3857
  void* pTask = sStreamReaderCalcInfo->pTask;
124,223 ✔
3858
  STimeWindow* pWin = isExtWin ? &handle->extWinRange : &handle->winRange;
124,223 ✔
3859
  bool* pValid = isExtWin ? &handle->extWinRangeValid : &handle->winRangeValid;
124,223 ✔
3860

3861
  if (req->pStRtFuncInfo->withExternalWindow) {
124,223 ✔
3862
    sStreamReaderCalcInfo->tmpRtFuncInfo.triggerType = req->pStRtFuncInfo->triggerType;
28,630 ✔
3863
    sStreamReaderCalcInfo->tmpRtFuncInfo.isWindowTrigger = req->pStRtFuncInfo->isWindowTrigger;
28,630 ✔
3864
    sStreamReaderCalcInfo->tmpRtFuncInfo.precision = req->pStRtFuncInfo->precision;
28,630 ✔
3865

3866
    SSTriggerCalcParam* pFirst = NULL;
28,630 ✔
3867
    SSTriggerCalcParam* pLast = NULL;
28,630 ✔
3868
    int32_t             readInfoIndex = -1;
28,630 ✔
3869
    int32_t             firstParamIndex = 0;
28,630 ✔
3870
    int32_t             lastParamIndex = 0;
28,630 ✔
3871
    if (req->pStRtFuncInfo->isMultiGroupCalc) {
28,630 ✔
3872
      readInfoIndex = 0;
×
3873
      firstParamIndex = -1;
×
3874
      lastParamIndex = -1;
×
3875
      SSTriggerGroupReadInfo* pGrp = taosArrayGet(req->pStRtFuncInfo->curGrpRead, readInfoIndex);
×
3876
      STREAM_CHECK_NULL_GOTO(pGrp, TSDB_CODE_INVALID_PARA);
×
3877
      pFirst = &pGrp->firstParam;
×
3878
      pLast = &pGrp->lastParam;
×
3879
    } else {
3880
      pFirst = taosArrayGet(req->pStRtFuncInfo->pStreamPesudoFuncVals, 0);
28,630 ✔
3881
      pLast = taosArrayGetLast(req->pStRtFuncInfo->pStreamPesudoFuncVals);
28,630 ✔
3882
      lastParamIndex = taosArrayGetSize(req->pStRtFuncInfo->pStreamPesudoFuncVals) - 1;
28,630 ✔
3883
      STREAM_CHECK_NULL_GOTO(pFirst, terrno);
28,630 ✔
3884
      STREAM_CHECK_NULL_GOTO(pLast, terrno);
28,630 ✔
3885
    }
3886

3887
    if (!node->needCalc) {
28,630 ✔
3888
      pWin->skey = pFirst->wstart;
21,016 ✔
3889
      pWin->ekey = pLast->wend;
21,016 ✔
3890
      *pValid = true;
21,016 ✔
3891
      if (req->pStRtFuncInfo->triggerType == STREAM_TRIGGER_SLIDING) {
21,016 ✔
3892
        pWin->ekey--;
14,788 ✔
3893
      }
3894
    } else {
3895
      SSTriggerCalcParam* pTmp = taosArrayGet(sStreamReaderCalcInfo->tmpRtFuncInfo.pStreamPesudoFuncVals, 0);
7,614 ✔
3896
      STREAM_CHECK_NULL_GOTO(pTmp, terrno);
7,614 ✔
3897
      STREAM_CHECK_RET_GOTO(stProjectReaderCalcContext(req->pStRtFuncInfo, actualNodeId, readInfoIndex, firstParamIndex,
7,614 ✔
3898
                                                       &sStreamReaderCalcInfo->tmpRtFuncInfo));
10 ✔
3899
      memcpy(pTmp, pFirst, sizeof(*pTmp));
7,614 ✔
3900

3901
      STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, &sStreamReaderCalcInfo->tmpRtFuncInfo, pWin, pValid, 1));
7,614 ✔
3902
      if (*pValid) {
7,614 ✔
3903
        int64_t skey = pWin->skey;
7,614 ✔
3904

3905
        STREAM_CHECK_RET_GOTO(stProjectReaderCalcContext(req->pStRtFuncInfo, actualNodeId, readInfoIndex,
7,614 ✔
3906
                                                         lastParamIndex, &sStreamReaderCalcInfo->tmpRtFuncInfo));
10 ✔
3907
        memcpy(pTmp, pLast, sizeof(*pTmp));
7,614 ✔
3908
        STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, &sStreamReaderCalcInfo->tmpRtFuncInfo, pWin, pValid, 2));
7,614 ✔
3909

3910
        if (*pValid) {
7,614 ✔
3911
          pWin->skey = skey;
7,614 ✔
3912
        }
3913
      }
3914
      pWin->ekey--;
7,614 ✔
3915
    }
3916
  } else {
3917
    if (!node->needCalc) {
95,593 ✔
3918
      SSTriggerCalcParam* pCurr = taosArrayGet(req->pStRtFuncInfo->pStreamPesudoFuncVals, req->pStRtFuncInfo->curIdx);
89,178 ✔
3919
      pWin->skey = pCurr->wstart;
89,178 ✔
3920
      pWin->ekey = pCurr->wend;
89,178 ✔
3921
      *pValid = true;
89,178 ✔
3922
      if (req->pStRtFuncInfo->triggerType == STREAM_TRIGGER_SLIDING) {
89,178 ✔
3923
        pWin->ekey--;
7,905 ✔
3924
      }
3925
    } else {
3926
      STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, req->pStRtFuncInfo, pWin, pValid, 3));
6,415 ✔
3927
      pWin->ekey--;
6,415 ✔
3928
    }
3929
  }
3930

3931
  ST_TASK_DLOG("%s type:%s, withExternalWindow:%d, skey:%" PRId64 ", ekey:%" PRId64 ", validRange:%d", 
124,423 ✔
3932
      __func__, isExtWin ? "interp range" : "scan time range", req->pStRtFuncInfo->withExternalWindow, pWin->skey, pWin->ekey, *pValid);
3933

3934
end:
1,320 ✔
3935

3936
  if (code) {
124,223 ✔
3937
    ST_TASK_ELOG("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3938
  }
3939
  
3940
  return code;
124,223 ✔
3941
}
3942

3943
static int32_t createDataBlockTsUid(SSDataBlock** pBlockRet, uint32_t numOfRows) {
97,804 ✔
3944
  int32_t      code = 0;
97,804 ✔
3945
  int32_t      lino = 0;
97,804 ✔
3946
  SSDataBlock* pBlock = NULL;
97,804 ✔
3947
  STREAM_CHECK_RET_GOTO(createDataBlock(&pBlock));
97,870 ✔
3948
  SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_TIMESTAMP, LONG_BYTES, PRIMARYKEY_TIMESTAMP_COL_ID);
97,870 ✔
3949
  STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
97,870 ✔
3950
  idata = createColumnInfoData(TSDB_DATA_TYPE_BIGINT, LONG_BYTES, PRIMARYKEY_TIMESTAMP_COL_ID + 1);
97,805 ✔
3951
  STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
97,870 ✔
3952
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, numOfRows));
97,870 ✔
3953

3954
end:
97,870 ✔
3955
  STREAM_PRINT_LOG_END(code, lino)
97,870 ✔
3956
  if (code != TSDB_CODE_SUCCESS) {
97,870 ✔
3957
    blockDataDestroy(pBlock);
×
3958
    pBlock = NULL;
×
3959
  }
3960
  *pBlockRet = pBlock;
97,870 ✔
3961
  return code;
97,870 ✔
3962
}
3963

3964
static int32_t processTsOutPutAllTables(SStreamTriggerReaderInfo* sStreamReaderInfo, SStreamTsResponse* tsRsp, SSDataBlock* pResBlock, int32_t order) {
24,513 ✔
3965
  int32_t code = 0;
24,513 ✔
3966
  int32_t lino = 0;
24,513 ✔
3967
  void* pTask = sStreamReaderInfo->pTask;
24,513 ✔
3968

3969
  tsRsp->tsInfo = taosArrayInit(pResBlock->info.rows, sizeof(STsInfo));
24,513 ✔
3970
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
24,513 ✔
3971
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
24,513 ✔
3972
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
24,513 ✔
3973
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
56,650 ✔
3974
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
64,274 ✔
3975
      continue;
66 ✔
3976
    }
3977
    STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
32,071 ✔
3978
    STREAM_CHECK_NULL_GOTO(tsInfo, terrno)
32,071 ✔
3979
    if (order == TSDB_ORDER_ASC) {
32,071 ✔
3980
      tsInfo->ts = INT64_MAX;
17,166 ✔
3981
    } else {
3982
      tsInfo->ts = INT64_MIN;
14,905 ✔
3983
    }
3984
    int64_t ts = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
32,071 ✔
3985
    if (order == TSDB_ORDER_ASC && ts < tsInfo->ts) {
32,071 ✔
3986
      tsInfo->ts = ts;
17,166 ✔
3987
    } else if (order == TSDB_ORDER_DESC && ts > tsInfo->ts) {
14,905 ✔
3988
      tsInfo->ts = ts;
14,905 ✔
3989
    }
3990
    tsInfo->gId = *(int64_t*)colDataGetNumData(pColInfoDataUid, j);
32,071 ✔
3991
    ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
32,071 ✔
3992
  }
3993

3994
end:
24,513 ✔
3995
  return code;
24,513 ✔
3996
}
3997

3998
static int32_t processTsOutPutOneGroup(SStreamTriggerReaderInfo* sStreamReaderInfo, SStreamTsResponse* tsRsp,
29,442 ✔
3999
                                       SSDataBlock* pResBlock, int32_t order, uint64_t gid) {
4000
  int32_t code = 0;
29,442 ✔
4001
  int32_t lino = 0;
29,442 ✔
4002
  void* pTask = sStreamReaderInfo->pTask;
29,442 ✔
4003

4004
  tsRsp->tsInfo = taosArrayInit(1, sizeof(STsInfo));
29,442 ✔
4005
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
29,376 ✔
4006
  STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
29,376 ✔
4007
  STREAM_CHECK_NULL_GOTO(tsInfo, terrno)
29,442 ✔
4008
  if (order == TSDB_ORDER_ASC) {
29,442 ✔
4009
    tsInfo->ts = INT64_MAX;
23,942 ✔
4010
  } else {
4011
    tsInfo->ts = INT64_MIN;
5,500 ✔
4012
  }
4013

4014
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
29,442 ✔
4015
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
29,442 ✔
4016
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
63,602 ✔
4017
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
68,320 ✔
4018
      continue;
×
4019
    }
4020
    int64_t ts = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
34,160 ✔
4021
    if (order == TSDB_ORDER_ASC && ts < tsInfo->ts) {
34,160 ✔
4022
      tsInfo->ts = ts;
23,942 ✔
4023
    } else if (order == TSDB_ORDER_DESC && ts > tsInfo->ts) {
10,218 ✔
4024
      tsInfo->ts = ts;
5,500 ✔
4025
    }
4026
  }
4027
  if (gid != 0) {
29,442 ✔
4028
    tsInfo->gId = gid;
12,619 ✔
4029
  } else {
4030
    int64_t uid = *(int64_t*)colDataGetNumData(pColInfoDataUid, 0);
16,823 ✔
4031
    tsInfo->gId = qStreamGetGroupIdFromSet(sStreamReaderInfo, uid);
16,823 ✔
4032
  }
4033
  ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
29,442 ✔
4034

4035
end:
8,646 ✔
4036
  return code;
29,442 ✔
4037
}
4038

4039
static int32_t processTsOutPutAllGroups(SStreamTriggerReaderInfo* sStreamReaderInfo, SStreamTsResponse* tsRsp, SSDataBlock* pResBlock, int32_t order) {
1,254 ✔
4040
  int32_t code = 0;
1,254 ✔
4041
  int32_t lino = 0;
1,254 ✔
4042
  STableKeyInfo* pList = NULL;
1,254 ✔
4043
  StreamTableListInfo     tableInfo = {0};
1,254 ✔
4044

4045
  void* pTask = sStreamReaderInfo->pTask;
1,254 ✔
4046
  STREAM_CHECK_RET_GOTO(qStreamCopyTableInfo(sStreamReaderInfo, &tableInfo));
1,254 ✔
4047

4048
  SSHashObj*   uidTsHash = tSimpleHashInit(pResBlock->info.rows, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
1,254 ✔
4049
  STREAM_CHECK_NULL_GOTO(uidTsHash, terrno);
1,254 ✔
4050
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
1,254 ✔
4051
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
1,254 ✔
4052
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
3,362 ✔
4053
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
4,216 ✔
4054
      continue;
×
4055
    }
4056
    int64_t ts = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
2,108 ✔
4057
    int64_t uid = *(int64_t*)colDataGetNumData(pColInfoDataUid, j);
2,108 ✔
4058
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(uidTsHash, &uid, LONG_BYTES, &ts, LONG_BYTES));
2,108 ✔
4059
  }
4060
  tsRsp->tsInfo = taosArrayInit(qStreamGetTableListGroupNum(sStreamReaderInfo), sizeof(STsInfo));
1,254 ✔
4061
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
1,254 ✔
4062
  while (true) {
2,039 ✔
4063
    int32_t pNum = 0;
3,293 ✔
4064
    int64_t gId = 0;
3,293 ✔
4065
    STREAM_CHECK_RET_GOTO(qStreamIterTableList(&tableInfo, &pList, &pNum, &gId));
3,293 ✔
4066
    if (pNum == 0) break;
3,293 ✔
4067

4068
    int64_t ts = (order == TSDB_ORDER_ASC) ? INT64_MAX : INT64_MIN;
2,039 ✔
4069
    bool    hasTs = false;
2,039 ✔
4070
    for (int32_t i = 0; i < pNum; i++) {
4,464 ✔
4071
      int64_t  uid = pList[i].uid;
2,425 ✔
4072
      int64_t* rowTs = tSimpleHashGet(uidTsHash, &uid, LONG_BYTES);
2,425 ✔
4073
      if (rowTs == NULL) {
2,425 ✔
4074
        continue;
317 ✔
4075
      }
4076
      if (!hasTs || (order == TSDB_ORDER_ASC && *rowTs < ts) || (order == TSDB_ORDER_DESC && *rowTs > ts)) {
2,108 ✔
4077
        ts = *rowTs;
1,988 ✔
4078
        hasTs = true;
1,988 ✔
4079
      }
4080
    }
4081
    if (!hasTs) {
2,039 ✔
4082
      taosMemoryFreeClear(pList);
118 ✔
4083
      continue;
118 ✔
4084
    }
4085

4086
    STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
1,921 ✔
4087
    STREAM_CHECK_NULL_GOTO(tsInfo, terrno);
1,921 ✔
4088
    tsInfo->ts = ts;
1,921 ✔
4089
    tsInfo->gId = gId;
1,921 ✔
4090
    ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
1,921 ✔
4091
    taosMemoryFreeClear(pList);
1,921 ✔
4092
  }
4093

4094
end:
1,254 ✔
4095
  qStreamDestroyTableInfo(&tableInfo);
1,254 ✔
4096
  taosMemoryFreeClear(pList);
1,254 ✔
4097
  tSimpleHashCleanup(uidTsHash);
1,254 ✔
4098
  return code;
1,254 ✔
4099
}
4100

4101
static int32_t processTsOutPutRollupGroups(SStreamTriggerReaderInfo* sStreamReaderInfo, SStreamTsResponse* tsRsp,
316 ✔
4102
                                           SSDataBlock* pResBlock, int32_t order) {
4103
  int32_t             code = 0;
316 ✔
4104
  int32_t             lino = 0;
316 ✔
4105
  STableKeyInfo*      pList = NULL;
316 ✔
4106
  StreamTableListInfo tableInfo = {0};
316 ✔
4107
  SSHashObj*          uidTsHash = NULL;
316 ✔
4108

4109
  void* pTask = sStreamReaderInfo->pTask;
316 ✔
4110
  STREAM_CHECK_RET_GOTO(qStreamCopyTableInfo(sStreamReaderInfo, &tableInfo));
316 ✔
4111

4112
  uidTsHash = tSimpleHashInit(pResBlock->info.rows, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
316 ✔
4113
  STREAM_CHECK_NULL_GOTO(uidTsHash, terrno);
316 ✔
4114
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
316 ✔
4115
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
316 ✔
4116
  STREAM_CHECK_NULL_GOTO(pColInfoDataTs, terrno);
316 ✔
4117
  STREAM_CHECK_NULL_GOTO(pColInfoDataUid, terrno);
316 ✔
4118
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
632 ✔
4119
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
632 ✔
4120
      continue;
×
4121
    }
4122
    int64_t  rowTs = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
316 ✔
4123
    int64_t  rowUid = *(int64_t*)colDataGetNumData(pColInfoDataUid, j);
316 ✔
4124
    int64_t* savedTs = tSimpleHashGet(uidTsHash, &rowUid, LONG_BYTES);
316 ✔
4125
    if (savedTs == NULL || (order == TSDB_ORDER_ASC && rowTs < *savedTs) ||
316 ✔
4126
        (order == TSDB_ORDER_DESC && rowTs > *savedTs)) {
×
4127
      STREAM_CHECK_RET_GOTO(tSimpleHashPut(uidTsHash, &rowUid, LONG_BYTES, &rowTs, LONG_BYTES));
316 ✔
4128
    }
4129
  }
4130

4131
  tsRsp->tsInfo = taosArrayInit(qStreamGetTableListGroupNum(sStreamReaderInfo), sizeof(STsInfo));
316 ✔
4132
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
316 ✔
4133

4134
  while (true) {
948 ✔
4135
    int32_t pNum = 0;
1,264 ✔
4136
    int64_t gId = 0;
1,264 ✔
4137
    STREAM_CHECK_RET_GOTO(qStreamIterTableList(&tableInfo, &pList, &pNum, &gId));
1,264 ✔
4138
    if (pNum == 0) break;
1,264 ✔
4139

4140
    int64_t          ts = (order == TSDB_ORDER_ASC) ? INT64_MAX : INT64_MIN;
948 ✔
4141
    bool             hasTs = false;
948 ✔
4142
    for (int32_t i = 0; i < pNum; i++) {
1,896 ✔
4143
      int64_t uid = pList[i].uid;
948 ✔
4144
      int64_t* rowTs = tSimpleHashGet(uidTsHash, &uid, LONG_BYTES);
948 ✔
4145
      if (rowTs == NULL) {
948 ✔
4146
        continue;
×
4147
      }
4148
      if (!hasTs || (order == TSDB_ORDER_ASC && *rowTs < ts) || (order == TSDB_ORDER_DESC && *rowTs > ts)) {
948 ✔
4149
        ts = *rowTs;
948 ✔
4150
        hasTs = true;
948 ✔
4151
      }
4152
    }
4153
    if (!hasTs) {
948 ✔
4154
      taosMemoryFreeClear(pList);
×
4155
      continue;
×
4156
    }
4157

4158
    STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
948 ✔
4159
    STREAM_CHECK_NULL_GOTO(tsInfo, terrno);
948 ✔
4160
    tsInfo->ts = ts;
948 ✔
4161
    tsInfo->gId = gId;
948 ✔
4162
    ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
948 ✔
4163
    taosMemoryFreeClear(pList);
948 ✔
4164
  }
4165

4166
end:
316 ✔
4167
  qStreamDestroyTableInfo(&tableInfo);
316 ✔
4168
  taosMemoryFreeClear(pList);
316 ✔
4169
  tSimpleHashCleanup(uidTsHash);
316 ✔
4170
  return code;
316 ✔
4171
}
4172

4173
// static bool stReaderTaskWaitQuit(SStreamTask* pTask) { return taosHasRWWFlag(&pTask->entryLock); }
4174

4175
static int32_t getAllTs(SVnode* pVnode, SSDataBlock*  pResBlock, SStreamReaderTaskInner* pTaskInner, STableKeyInfo* pList, int32_t pNum) {
94,306 ✔
4176
  int32_t code = 0;
94,306 ✔
4177
  int32_t lino = 0;
94,306 ✔
4178

4179
  stDebug("%s getAllTs enter: pNum:%d suid:%"PRId64" order:%d skey:%"PRId64" ekey:%"PRId64" verRange:[%"PRId64",%"PRId64"]",
94,306 ✔
4180
          pTaskInner->idStr, pNum, pTaskInner->options->suid, pTaskInner->options->order,
4181
          pTaskInner->options->twindows.skey, pTaskInner->options->twindows.ekey,
4182
          (int64_t)-1, pTaskInner->options->ver);
4183
  for (int32_t i = 0; i < pNum; i++) {
246,830 ✔
4184
    stDebug("%s getAllTs table[%d]: uid:%"PRId64, pTaskInner->idStr, i, pList[i].uid);
152,656 ✔
4185
  }
4186

4187
  STREAM_CHECK_RET_GOTO(pTaskInner->storageApi->tsdReader.tsdCreateFirstLastTsIter(pVnode, &pTaskInner->options->twindows, &(SVersionRange){.minVer = -1, .maxVer = pTaskInner->options->ver},
94,174 ✔
4188
                                                pTaskInner->options->suid, pList, pNum, pTaskInner->options->order, &pTaskInner->pReader, pTaskInner->idStr));
37 ✔
4189
  bool hasNext = true;
94,240 ✔
4190
  int32_t iterCount = 0;
94,240 ✔
4191
  while(1){
37 ✔
4192
    STREAM_CHECK_RET_GOTO(pTaskInner->storageApi->tsdReader.tsdNextFirstLastTsBlock(pTaskInner->pReader, pResBlock, &hasNext));
94,240 ✔
4193
    stDebug("%s getAllTs iter[%d]: hasNext:%d pResBlock->info.rows:%"PRId64, pTaskInner->idStr, iterCount++, hasNext, pResBlock->info.rows);
94,240 ✔
4194
    STREAM_CHECK_CONDITION_GOTO(!hasNext, TDB_CODE_SUCCESS);
94,306 ✔
4195
  }
4196

4197
end:
94,306 ✔
4198
  stDebug("%s getAllTs done: code:%d pResBlock->info.rows:%"PRId64, pTaskInner->idStr, code, pResBlock ? pResBlock->info.rows : -1);
94,306 ✔
4199
  pTaskInner->storageApi->tsdReader.tsdDestroyFirstLastTsIter(pTaskInner->pReader);
94,306 ✔
4200
  pTaskInner->pReader = NULL;
94,173 ✔
4201
  return code;
94,173 ✔
4202
}
4203

4204
static int32_t processTsVTable(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
12,994 ✔
4205
                                  SStreamReaderTaskInner* pTaskInner) {
4206
  int32_t code = 0;
12,994 ✔
4207
  int32_t lino = 0;
12,994 ✔
4208
  STableKeyInfo* pList = NULL;
12,994 ✔
4209
  StreamTableListInfo     tableInfo = {0};
12,994 ✔
4210

4211
  void* pTask = sStreamReaderInfo->pTask;
12,994 ✔
4212
  STREAM_CHECK_RET_GOTO(qStreamCopyTableInfo(sStreamReaderInfo, &tableInfo));
12,994 ✔
4213

4214
  SSDataBlock*  pResBlock = NULL;
12,994 ✔
4215
  STREAM_CHECK_RET_GOTO(createDataBlockTsUid(&pResBlock, qStreamGetTableListNum(sStreamReaderInfo)));
12,994 ✔
4216

4217
  while (true) {
9,364 ✔
4218
    int32_t        pNum = 0;
22,358 ✔
4219
    int64_t        suid = 0;
22,358 ✔
4220
    STREAM_CHECK_RET_GOTO(qStreamIterTableList(&tableInfo, &pList, &pNum, &suid));
22,358 ✔
4221
    if(pNum == 0) break;
22,424 ✔
4222
    pTaskInner->options->suid = suid;
9,430 ✔
4223
    STREAM_CHECK_RET_GOTO(getAllTs(pVnode, pResBlock, pTaskInner, pList, pNum));
9,430 ✔
4224
    taosMemoryFreeClear(pList);
9,364 ✔
4225
  }
4226

4227
  STREAM_CHECK_RET_GOTO(processTsOutPutAllTables(sStreamReaderInfo, tsRsp, pResBlock, pTaskInner->options->order));
12,994 ✔
4228

4229
end:
12,994 ✔
4230
  qStreamDestroyTableInfo(&tableInfo);
12,994 ✔
4231
  taosMemoryFreeClear(pList);
12,994 ✔
4232
  blockDataDestroy(pResBlock);
12,994 ✔
4233
  STREAM_PRINT_LOG_END_WITHID(code, lino);
12,994 ✔
4234
  return code;
12,994 ✔
4235
}
4236

4237
static int32_t processTsNonVTable(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
81,488 ✔
4238
                                  SStreamReaderTaskInner* pTaskInner) {
4239
  int32_t code = 0;
81,488 ✔
4240
  int32_t lino = 0;
81,488 ✔
4241
  STableKeyInfo* pList = NULL;
81,488 ✔
4242
  void* pTask = sStreamReaderInfo->pTask;
81,488 ✔
4243

4244
  SSDataBlock*  pResBlock = NULL;
81,488 ✔
4245

4246
  int32_t        pNum = 0;
81,488 ✔
4247
  int64_t        suid = 0;
81,488 ✔
4248
  STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, 0, &pList, &pNum));
81,488 ✔
4249
  ST_TASK_DLOG("vgId:%d %s qStreamGetTableList returned pNum:%d", TD_VID(pVnode), __func__, pNum);
81,488 ✔
4250
  STREAM_CHECK_CONDITION_GOTO(pNum == 0, TSDB_CODE_SUCCESS);
81,488 ✔
4251
  STREAM_CHECK_RET_GOTO(createDataBlockTsUid(&pResBlock, pNum));
72,257 ✔
4252

4253
  pTaskInner->options->suid = sStreamReaderInfo->suid;
72,257 ✔
4254
  ST_TASK_DLOG("vgId:%d %s calling getAllTs: suid:%"PRId64" order:%d skey:%"PRId64" ekey:%"PRId64" ver:%"PRId64,
72,257 ✔
4255
               TD_VID(pVnode), __func__, pTaskInner->options->suid, pTaskInner->options->order,
4256
               pTaskInner->options->twindows.skey, pTaskInner->options->twindows.ekey, pTaskInner->options->ver);
4257
  STREAM_CHECK_RET_GOTO(getAllTs(pVnode, pResBlock, pTaskInner, pList, pNum));
72,257 ✔
4258
  ST_TASK_DLOG("vgId:%d %s getAllTs done: pResBlock rows:%"PRId64, TD_VID(pVnode), __func__, pResBlock->info.rows);
72,190 ✔
4259
  int32_t order = pTaskInner->options->order;
72,257 ✔
4260
  if (pResBlock->info.rows == 0 && sStreamReaderInfo->groupByTbname) {
72,257 ✔
4261
    tsRsp->tsInfo = taosArrayInit(pNum, sizeof(STsInfo));
14,744 ✔
4262
    STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
14,744 ✔
4263
    for (int32_t i = 0; i < pNum; i++) {
50,352 ✔
4264
      STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
35,608 ✔
4265
      STREAM_CHECK_NULL_GOTO(tsInfo, terrno);
35,608 ✔
4266
      tsInfo->gId = pList[i].uid;
35,608 ✔
4267
      tsInfo->ts = 0;
35,608 ✔
4268
      ST_TASK_DLOG("%s no data but return gId (uid):%" PRIu64 " for tbname partition", __func__, tsInfo->gId);
35,608 ✔
4269
    }
4270
    goto end;
14,744 ✔
4271
  }
4272

4273
  STREAM_CHECK_CONDITION_GOTO(pResBlock->info.rows == 0, TDB_CODE_SUCCESS);
57,513 ✔
4274

4275
  if (sStreamReaderInfo->groupByTbname) {
29,912 ✔
4276
    STREAM_CHECK_RET_GOTO(processTsOutPutAllTables(sStreamReaderInfo, tsRsp, pResBlock, order));
11,519 ✔
4277
  } else if (isRollupMultiReader(sStreamReaderInfo)) {
18,393 ✔
4278
    STREAM_CHECK_RET_GOTO(processTsOutPutRollupGroups(sStreamReaderInfo, tsRsp, pResBlock, order));
316 ✔
4279
  } else if (sStreamReaderInfo->partitionCols == NULL) {
18,077 ✔
4280
    STREAM_CHECK_RET_GOTO(processTsOutPutOneGroup(sStreamReaderInfo, tsRsp, pResBlock, order, 0));
16,823 ✔
4281
  } else {
4282
    STREAM_CHECK_RET_GOTO(processTsOutPutAllGroups(sStreamReaderInfo, tsRsp, pResBlock, order));
1,254 ✔
4283
  }
4284
end:
81,449 ✔
4285
  blockDataDestroy(pResBlock);
81,488 ✔
4286
  taosMemoryFreeClear(pList);
81,488 ✔
4287
  STREAM_PRINT_LOG_END_WITHID(code, lino);
81,488 ✔
4288
  return code;
81,488 ✔
4289
}
4290

4291
static int32_t processTsOnce(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
15,938 ✔
4292
                                  SStreamReaderTaskInner* pTaskInner, uint64_t gid) {
4293
  int32_t code = 0;
15,938 ✔
4294
  int32_t lino = 0;
15,938 ✔
4295
  STableKeyInfo* pList = NULL;
15,938 ✔
4296
  void* pTask = sStreamReaderInfo->pTask;
15,938 ✔
4297
  
4298
  SSDataBlock*  pResBlock = NULL;
15,938 ✔
4299

4300
  int32_t        pNum = 0;
15,938 ✔
4301
  STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, gid, &pList, &pNum));
15,938 ✔
4302
  STREAM_CHECK_CONDITION_GOTO(pNum == 0, TSDB_CODE_SUCCESS);
15,938 ✔
4303
  STREAM_CHECK_RET_GOTO(createDataBlockTsUid(&pResBlock, pNum));
12,619 ✔
4304

4305
  pTaskInner->options->suid = sStreamReaderInfo->suid;
12,553 ✔
4306
  STREAM_CHECK_RET_GOTO(getAllTs(pVnode, pResBlock, pTaskInner, pList, pNum));
12,553 ✔
4307
  STREAM_CHECK_CONDITION_GOTO(pResBlock->info.rows == 0, TDB_CODE_SUCCESS);
12,619 ✔
4308
  int32_t order = pTaskInner->options->order;
12,619 ✔
4309

4310
  STREAM_CHECK_RET_GOTO(processTsOutPutOneGroup(sStreamReaderInfo, tsRsp, pResBlock, order, gid));
12,619 ✔
4311
end:
15,938 ✔
4312
  blockDataDestroy(pResBlock);
15,938 ✔
4313
  taosMemoryFreeClear(pList);
15,872 ✔
4314
  STREAM_PRINT_LOG_END_WITHID(code, lino);
15,872 ✔
4315
  return code;
15,872 ✔
4316
}
4317

4318
static int32_t processTs(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
94,482 ✔
4319
                                  SStreamReaderTaskInner* pTaskInner) {
4320
  if (sStreamReaderInfo->isVtableStream) {
94,482 ✔
4321
    return processTsVTable(pVnode, tsRsp, sStreamReaderInfo, pTaskInner);
12,994 ✔
4322
  }
4323

4324
  return processTsNonVTable(pVnode, tsRsp, sStreamReaderInfo, pTaskInner);
81,488 ✔
4325
}
4326

4327
static int32_t vnodeProcessStreamSetTableReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
9,604 ✔
4328
                                             SStreamTriggerReaderInfo*   sStreamReaderInfo,
4329
                                             SStreamReaderResponseStats* pRspStats) {
4330
  int32_t code = 0;
9,604 ✔
4331
  int32_t lino = 0;
9,604 ✔
4332
  void*   buf = NULL;
9,604 ✔
4333
  size_t  size = 0;
9,604 ✔
4334
  void* pTask = sStreamReaderInfo->pTask;
9,604 ✔
4335

4336
  ST_TASK_DLOG("vgId:%d %s start, trigger hash size:%d, calc hash size:%d, appver:%"PRId64, TD_VID(pVnode), __func__,
9,604 ✔
4337
                tSimpleHashGetSize(req->setTableReq.uidInfoTrigger), tSimpleHashGetSize(req->setTableReq.uidInfoCalc), pVnode->state.applied);
4338

4339
  taosWLockLatch(&sStreamReaderInfo->lock);
9,604 ✔
4340
  TSWAP(sStreamReaderInfo->uidHashTrigger, req->setTableReq.uidInfoTrigger);
9,604 ✔
4341
  TSWAP(sStreamReaderInfo->uidHashCalc, req->setTableReq.uidInfoCalc);
9,604 ✔
4342
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->uidHashTrigger, TSDB_CODE_INVALID_PARA);
9,604 ✔
4343
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->uidHashCalc, TSDB_CODE_INVALID_PARA);
9,604 ✔
4344

4345
  qStreamClearTableInfo(&sStreamReaderInfo->vSetTableList);
9,604 ✔
4346
  STREAM_CHECK_RET_GOTO(initStreamTableListInfo(&sStreamReaderInfo->vSetTableList, UIDMAP_SINGLE));
9,604 ✔
4347
  STREAM_CHECK_RET_GOTO(qBuildVTableList(sStreamReaderInfo));
9,604 ✔
4348
end:
9,604 ✔
4349
  taosWUnLockLatch(&sStreamReaderInfo->lock);
9,604 ✔
4350
  STREAM_PRINT_LOG_END_WITHID(code, lino);
9,604 ✔
4351
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
9,604 ✔
4352
  return code;
9,604 ✔
4353
}
4354

4355
static int32_t vnodeProcessStreamLastTsReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
65,778 ✔
4356
                                           SStreamTriggerReaderInfo*   sStreamReaderInfo,
4357
                                           SStreamReaderResponseStats* pRspStats) {
4358
  int32_t                 code = 0;
65,778 ✔
4359
  int32_t                 lino = 0;
65,778 ✔
4360
  SStreamReaderTaskInner* pTaskInner = NULL;
65,778 ✔
4361
  SStreamTsResponse       tsRsp = {0};
65,974 ✔
4362
  void*                   buf = NULL;
65,974 ✔
4363
  size_t                  size = 0;
65,974 ✔
4364

4365
  void* pTask = sStreamReaderInfo->pTask;
65,974 ✔
4366

4367
  ST_TASK_DLOG("vgId:%d %s start", TD_VID(pVnode), __func__);
65,974 ✔
4368

4369
  BUILD_OPTION(options, 0, sStreamReaderInfo->tableList.version, TSDB_ORDER_DESC, INT64_MIN, INT64_MAX, NULL, false, NULL);
65,974 ✔
4370
  STREAM_CHECK_RET_GOTO(createStreamTaskForTs(&options, &pTaskInner, &sStreamReaderInfo->storageApi));
65,974 ✔
4371

4372
  tsRsp.ver = sStreamReaderInfo->tableList.version + 1;
65,974 ✔
4373

4374
  STREAM_CHECK_RET_GOTO(processTs(pVnode, &tsRsp, sStreamReaderInfo, pTaskInner));
65,974 ✔
4375
  
4376
end:
65,974 ✔
4377
  ST_TASK_DLOG("vgId:%d %s get result size:%"PRIzu", ver:%"PRId64, TD_VID(pVnode), __func__, taosArrayGetSize(tsRsp.tsInfo), tsRsp.ver);
65,974 ✔
4378
  code = buildTsRsp(&tsRsp, &buf, &size);
65,974 ✔
4379
  STREAM_PRINT_LOG_END_WITHID(code, lino);
65,974 ✔
4380
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
65,974 ✔
4381
  taosArrayDestroy(tsRsp.tsInfo);
65,974 ✔
4382
  taosMemoryFree(pTaskInner);
65,974 ✔
4383
  return code;
65,974 ✔
4384
}
4385

4386
static int32_t vnodeProcessStreamFirstTsReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
44,446 ✔
4387
                                            SStreamTriggerReaderInfo*   sStreamReaderInfo,
4388
                                            SStreamReaderResponseStats* pRspStats) {
4389
  int32_t                 code = 0;
44,446 ✔
4390
  int32_t                 lino = 0;
44,446 ✔
4391
  SStreamReaderTaskInner* pTaskInner = NULL;
44,446 ✔
4392
  SStreamTsResponse       tsRsp = {0};
44,446 ✔
4393
  void*                   buf = NULL;
44,446 ✔
4394
  size_t                  size = 0;
44,446 ✔
4395

4396
  void* pTask = sStreamReaderInfo->pTask;
44,446 ✔
4397
  ST_TASK_DLOG("vgId:%d %s start, startTime:%"PRId64" ver:%"PRId64" gid:%"PRId64
44,446 ✔
4398
               " applied:%"PRId64" tableListNum:%d isVtable:%d groupByTbname:%d partitionCols:%p",
4399
               TD_VID(pVnode), __func__, req->firstTsReq.startTime, req->firstTsReq.ver, req->firstTsReq.gid,
4400
               pVnode->state.applied, qStreamGetTableListNum(sStreamReaderInfo),
4401
               sStreamReaderInfo->isVtableStream, sStreamReaderInfo->groupByTbname, sStreamReaderInfo->partitionCols);
4402
  int32_t        pNum = 0;
44,446 ✔
4403

4404
  tsRsp.ver = pVnode->state.applied;
44,446 ✔
4405

4406
  BUILD_OPTION(options, 0, req->firstTsReq.ver, TSDB_ORDER_ASC, req->firstTsReq.startTime, INT64_MAX, NULL, false, NULL);
44,446 ✔
4407
  STREAM_CHECK_RET_GOTO(createStreamTaskForTs(&options, &pTaskInner, &sStreamReaderInfo->storageApi));
44,446 ✔
4408

4409
  if (req->firstTsReq.gid != 0) {
44,446 ✔
4410
    STREAM_CHECK_RET_GOTO(processTsOnce(pVnode, &tsRsp, sStreamReaderInfo, pTaskInner, req->firstTsReq.gid));
15,938 ✔
4411
  } else {
4412
    STREAM_CHECK_RET_GOTO(processTs(pVnode, &tsRsp, sStreamReaderInfo, pTaskInner));
28,508 ✔
4413
  }
4414

4415
end:
44,380 ✔
4416
  ST_TASK_DLOG("vgId:%d %s get result size:%"PRIzu", ver:%"PRId64, TD_VID(pVnode), __func__, taosArrayGetSize(tsRsp.tsInfo), tsRsp.ver);
44,380 ✔
4417
  code = buildTsRsp(&tsRsp, &buf, &size);
44,380 ✔
4418
  STREAM_PRINT_LOG_END_WITHID(code, lino);
44,446 ✔
4419
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
44,446 ✔
4420
  taosArrayDestroy(tsRsp.tsInfo);
44,446 ✔
4421
  taosMemoryFree(pTaskInner);
44,446 ✔
4422
  return code;
44,446 ✔
4423
}
4424

4425
static int32_t vnodeProcessStreamTsdbMetaReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
29,978 ✔
4426
                                             SStreamTriggerReaderInfo*   sStreamReaderInfo,
4427
                                             SStreamReaderResponseStats* pRspStats) {
4428
  int32_t code = 0;
29,978 ✔
4429
  int32_t lino = 0;
29,978 ✔
4430
  void*   buf = NULL;
29,978 ✔
4431
  size_t  size = 0;
29,978 ✔
4432
  STableKeyInfo* pList = NULL;
29,978 ✔
4433
  STableKeyInfo* pReadList = NULL;
29,978 ✔
4434
  SSHashObj*     pRollupMetaByUid = NULL;
29,978 ✔
4435
  SSHashObj*     pRollupMetaCount = NULL;
29,978 ✔
4436
  int32_t        pNum = 0;
29,978 ✔
4437
  int32_t        readNum = 0;
29,978 ✔
4438

4439
  void* pTask = sStreamReaderInfo->pTask;
29,978 ✔
4440
  ST_TASK_DLOG("vgId:%d %s start, ver:%" PRId64 ",skey:%" PRId64 ",ekey:%" PRId64 ",gid:%" PRId64, TD_VID(pVnode),
29,978 ✔
4441
               __func__, req->tsdbMetaReq.ver, req->tsdbMetaReq.startTime, req->tsdbMetaReq.endTime,
4442
               req->tsdbMetaReq.gid);
4443

4444
  SStreamReaderTaskInner* pTaskInner = NULL;
29,978 ✔
4445
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_META);
29,978 ✔
4446
  bool                    snapshotRollupMeta = isRollupMultiReader(sStreamReaderInfo);
29,978 ✔
4447
  bool                    expandRollupMeta = snapshotRollupMeta && req->tsdbMetaReq.gid == 0;
29,978 ✔
4448

4449
  if (req->base.type == STRIGGER_PULL_TSDB_META) {
29,978 ✔
4450
    STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, req->tsdbMetaReq.gid, &pList, &pNum));
29,978 ✔
4451
    STableKeyInfo* pCreateList = pList;
29,978 ✔
4452
    int32_t        createNum = pNum;
29,978 ✔
4453
    if (isRollupMultiReader(sStreamReaderInfo)) {
29,978 ✔
4454
      if (req->tsdbMetaReq.gid == 0){
79 ✔
4455
        STREAM_CHECK_RET_GOTO(buildRollupMetaSnapshot(pList, pNum, &pReadList, &readNum, &pRollupMetaByUid, &pRollupMetaCount));
79 ✔
4456
        pCreateList = pReadList;
79 ✔
4457
        createNum = readNum;
79 ✔
4458
      } else {
4459
        STREAM_CHECK_RET_GOTO(buildRollupMetaCount(pList, pNum, &pRollupMetaCount));
×
4460
      }
4461
    }
4462
    BUILD_OPTION(options, getSuid(sStreamReaderInfo, pList), req->tsdbMetaReq.ver, req->tsdbMetaReq.order, req->tsdbMetaReq.startTime, req->tsdbMetaReq.endTime, 
29,978 ✔
4463
                          sStreamReaderInfo->tsSchemas, true, NULL);
×
4464
    STREAM_CHECK_RET_GOTO(
29,978 ✔
4465
        createStreamTask(pVnode, &options, &pTaskInner, NULL, pCreateList, createNum, &sStreamReaderInfo->storageApi));
×
4466
    if (isRollupMultiReader(sStreamReaderInfo)) {
29,978 ✔
4467
      pTaskInner->pRollupMetaByUid = pRollupMetaByUid;
79 ✔
4468
      pRollupMetaByUid = NULL;
79 ✔
4469
      pTaskInner->pRollupMetaCount = pRollupMetaCount;
79 ✔
4470
      pRollupMetaCount = NULL;
79 ✔
4471
    }
4472
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
29,978 ✔
4473

4474
    STREAM_CHECK_RET_GOTO(
29,978 ✔
4475
        createBlockForTsdbMeta(&pTaskInner->pResBlockDst, sStreamReaderInfo->isVtableStream,
4476
                               isRollupMultiReader(sStreamReaderInfo)));
×
4477
  } else {
4478
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
4479
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
4480
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
4481
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
4482
  }
4483

4484
  blockDataCleanup(pTaskInner->pResBlockDst);
29,978 ✔
4485
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pTaskInner->pResBlockDst, STREAM_RETURN_ROWS_NUM));
29,978 ✔
4486
  bool hasNext = true;
29,978 ✔
4487
  while (true) {
×
4488
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
47,503 ✔
4489
    if (!hasNext) {
47,503 ✔
4490
      break;
29,978 ✔
4491
    }
4492
    pTaskInner->storageApi->tsdReader.tsdReaderReleaseDataBlock(pTaskInner->pReader);
17,525 ✔
4493
    if (isRollupMultiReader(sStreamReaderInfo)) {
17,525 ✔
4494
      if (req->tsdbMetaReq.gid != 0) {
79 ✔
4495
        int32_t rollupTbCount = getRollupMetaCount(pTaskInner->pRollupMetaCount, req->tsdbMetaReq.gid);
×
4496
        STREAM_CHECK_CONDITION_GOTO(rollupTbCount <= 0, TSDB_CODE_INTERNAL_ERROR);
×
4497
        STREAM_CHECK_RET_GOTO(appendTsdbMetaRow(sStreamReaderInfo, pTaskInner->pResBlockDst, pTaskInner->pResBlock,
×
4498
                                                req->tsdbMetaReq.gid, rollupTbCount, TD_VID(pVnode)));
4499
      } else {
4500
        SArray** ppEntries =
×
4501
            tSimpleHashGet(pTaskInner->pRollupMetaByUid, &pTaskInner->pResBlock->info.id.uid, LONG_BYTES);
79 ✔
4502
        STREAM_CHECK_NULL_GOTO(ppEntries, TSDB_CODE_INTERNAL_ERROR);
79 ✔
4503
        int32_t numOfEntries = taosArrayGetSize(*ppEntries);
79 ✔
4504
        for (int32_t i = 0; i < numOfEntries; ++i) {
316 ✔
4505
          STableKeyInfo* pInfo = taosArrayGet(*ppEntries, i);
237 ✔
4506
          STREAM_CHECK_NULL_GOTO(pInfo, TSDB_CODE_INTERNAL_ERROR);
237 ✔
4507
          int32_t rollupTbCount = getRollupMetaCount(pTaskInner->pRollupMetaCount, pInfo->groupId);
237 ✔
4508
          STREAM_CHECK_CONDITION_GOTO(rollupTbCount <= 0, TSDB_CODE_INTERNAL_ERROR);
237 ✔
4509
          STREAM_CHECK_RET_GOTO(appendTsdbMetaRow(sStreamReaderInfo, pTaskInner->pResBlockDst, pTaskInner->pResBlock,
237 ✔
4510
                                                  pInfo->groupId, rollupTbCount, TD_VID(pVnode)));
×
4511
        }
4512
      }
4513
    } else {
4514
      uint64_t gid = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
17,446 ✔
4515
      STREAM_CHECK_RET_GOTO(appendTsdbMetaRow(sStreamReaderInfo, pTaskInner->pResBlockDst, pTaskInner->pResBlock, gid,
17,446 ✔
4516
                                              0, TD_VID(pVnode)));
×
4517
    }
4518

4519
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
17,525 ✔
4520
      break;
×
4521
    }
4522
  }
4523

4524
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
29,978 ✔
4525
  STREAM_CHECK_RET_GOTO(buildRsp(pTaskInner->pResBlockDst, &buf, &size));
29,978 ✔
4526
  printDataBlock(pTaskInner->pResBlockDst, __func__, "meta", ((SStreamTask *)sStreamReaderInfo->pTask)->streamId);
29,978 ✔
4527
  if (!hasNext) {
29,978 ✔
4528
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
29,978 ✔
4529
  }
4530

4531
end:
29,978 ✔
4532
  STREAM_PRINT_LOG_END_WITHID(code, lino);
29,913 ✔
4533
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
29,913 ✔
4534
  tSimpleHashCleanup(pRollupMetaByUid);
29,978 ✔
4535
  tSimpleHashCleanup(pRollupMetaCount);
29,978 ✔
4536
  taosMemoryFree(pReadList);
29,978 ✔
4537
  taosMemoryFree(pList);
29,978 ✔
4538
  return code;
29,978 ✔
4539
}
4540

4541
static int32_t vnodeProcessStreamTsdbTsDataReqNonVTable(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
4,313 ✔
4542
                                                        SStreamTriggerReaderInfo*   sStreamReaderInfo,
4543
                                                        SStreamReaderResponseStats* pRspStats) {
4544
  int32_t                 code = 0;
4,313 ✔
4545
  int32_t                 lino = 0;
4,313 ✔
4546
  SStreamReaderTaskInner* pTaskInner = NULL;
4,313 ✔
4547
  void*                   buf = NULL;
4,313 ✔
4548
  size_t                  size = 0;
4,313 ✔
4549
  SSDataBlock*            pBlockRes = NULL;
4,313 ✔
4550

4551
  void* pTask = sStreamReaderInfo->pTask;
4,313 ✔
4552
  ST_TASK_DLOG("vgId:%d %s start, ver:%" PRId64 ",skey:%" PRId64 ",ekey:%" PRId64 ",uid:%" PRId64 ",suid:%" PRId64,
4,313 ✔
4553
               TD_VID(pVnode), __func__, req->tsdbTsDataReq.ver, req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey,
4554
               req->tsdbTsDataReq.uid, req->tsdbTsDataReq.suid);
4555

4556
  int32_t       pNum = 1;
4,313 ✔
4557
  STableKeyInfo pList = {.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, req->tsdbTsDataReq.uid),
4,313 ✔
4558
                         .uid = req->tsdbTsDataReq.uid};
4,313 ✔
4559
  STREAM_CHECK_CONDITION_GOTO(pList.groupId == -1, TSDB_CODE_INVALID_PARA);
4,313 ✔
4560
  BUILD_OPTION(options, getSuid(sStreamReaderInfo, &pList), req->tsdbTsDataReq.ver, TSDB_ORDER_ASC,
4,313 ✔
4561
               req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey, sStreamReaderInfo->triggerCols, false, NULL);
×
4562
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, &pList,
4,313 ✔
4563
                                         pNum, &sStreamReaderInfo->storageApi));
×
4564
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
4,313 ✔
4565
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->tsBlock, false, &pBlockRes));
4,313 ✔
4566

4567
  while (1) {
4,313 ✔
4568
    bool hasNext = false;
8,626 ✔
4569
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
8,626 ✔
4570
    if (!hasNext) {
8,626 ✔
4571
      break;
4,313 ✔
4572
    }
4573
    pTaskInner->pResBlock->info.id.groupId = pList.groupId;
4,313 ✔
4574

4575
    SSDataBlock* pBlock = NULL;
4,313 ✔
4576
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
4,313 ✔
4577
    if (pBlock != NULL && pBlock->info.rows > 0) {
4,313 ✔
4578
      STREAM_CHECK_RET_GOTO(processTag(sStreamReaderInfo, false, pBlock->info.id.uid, pBlock, 0, pBlock->info.rows, 1));
4,313 ✔
4579
    }
4580

4581
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderInfo->pFilterInfo, NULL));
4,313 ✔
4582
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
4,313 ✔
4583
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
4,313 ✔
4584
                 TD_VID(pVnode), __func__, pTaskInner->pResBlock->info.window.skey,
4585
                 pTaskInner->pResBlock->info.window.ekey, pTaskInner->pResBlock->info.id.uid,
4586
                 pTaskInner->pResBlock->info.id.groupId, pTaskInner->pResBlock->info.rows);
4587
  }
4588

4589
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
4,313 ✔
4590
  SSDataBlock* pRspBlock = pTaskInner->pResBlockDst;
4,313 ✔
4591
  bool         requiresAncestorContext =
4,313 ✔
4592
      (sStreamReaderInfo->triggerAst != NULL && sStreamReaderInfo->triggerAst->requiresAncestorContext) ||
6,920 ✔
4593
      (sStreamReaderInfo->calcAst != NULL && sStreamReaderInfo->calcAst->requiresAncestorContext);
2,607 ✔
4594
  if (!requiresAncestorContext) {
4,313 ✔
4595
    blockDataTransform(pBlockRes, pTaskInner->pResBlockDst);
2,607 ✔
4596
    pRspBlock = pBlockRes;
2,607 ✔
4597
  }
4598
  STREAM_CHECK_RET_GOTO(buildRsp(pRspBlock, &buf, &size));
4,313 ✔
4599
  addReaderResponseBlock(pRspStats, pRspBlock);
4,313 ✔
4600

4601
end:
4,313 ✔
4602
  STREAM_PRINT_LOG_END_WITHID(code, lino);
4,313 ✔
4603
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
4,313 ✔
4604
  blockDataDestroy(pBlockRes);
4,313 ✔
4605

4606
  releaseStreamTask(&pTaskInner);
4,313 ✔
4607
  return code;
4,313 ✔
4608
}
4609

4610
static int32_t vnodeProcessStreamTsdbTsDataReqVTable(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
×
4611
                                                     SStreamTriggerReaderInfo*   sStreamReaderInfo,
4612
                                                     SStreamReaderResponseStats* pRspStats) {
4613
  int32_t                 code = 0;
×
4614
  int32_t                 lino = 0;
×
4615
  SStreamReaderTaskInner* pTaskInner = NULL;
×
4616
  void*                   buf = NULL;
×
4617
  size_t                  size = 0;
×
4618
  SSDataBlock*            pBlockRes = NULL;
×
4619

4620
  void* pTask = sStreamReaderInfo->pTask;
×
4621
  ST_TASK_DLOG("vgId:%d %s start, ver:%"PRId64",skey:%"PRId64",ekey:%"PRId64",uid:%"PRId64",suid:%"PRId64, TD_VID(pVnode), __func__, req->tsdbTsDataReq.ver, 
×
4622
                req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey, 
4623
                req->tsdbTsDataReq.uid, req->tsdbTsDataReq.suid);
4624

4625
  int32_t        pNum = 1;
×
4626
  STableKeyInfo  pList = {.groupId = req->tsdbTsDataReq.uid, .uid = req->tsdbTsDataReq.uid};
×
4627
  BUILD_OPTION(options, getSuid(sStreamReaderInfo, &pList), req->tsdbTsDataReq.ver, TSDB_ORDER_ASC, req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey,
×
4628
               sStreamReaderInfo->tsSchemas, true, NULL);
×
4629
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->tsBlock, &pList, pNum, &sStreamReaderInfo->storageApi));
×
4630
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->tsBlock, false, &pBlockRes));
×
4631

4632
  while (1) {
×
4633
    bool hasNext = false;
×
4634
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
×
4635
    if (!hasNext) {
×
4636
      break;
×
4637
    }
4638

4639
    SSDataBlock* pBlock = NULL;
×
4640
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
×
4641
    STREAM_CHECK_RET_GOTO(blockDataMerge(pBlockRes, pBlock));
×
4642
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
×
4643
            TD_VID(pVnode), __func__, pBlockRes->info.window.skey, pBlockRes->info.window.ekey,
4644
            pBlockRes->info.id.uid, pBlockRes->info.id.groupId, pBlockRes->info.rows);
4645
  }
4646

4647
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlockRes->info.rows);
×
4648
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
×
4649
  addReaderResponseBlock(pRspStats, pBlockRes);
×
4650

4651
end:
×
4652
  STREAM_PRINT_LOG_END_WITHID(code, lino);
×
4653
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
×
4654
  blockDataDestroy(pBlockRes);
×
4655

4656
  releaseStreamTask(&pTaskInner);
×
4657
  return code;
×
4658
}
4659

4660
static int32_t vnodeProcessStreamTsdbTriggerDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
41,064 ✔
4661
                                                    SStreamTriggerReaderInfo*   sStreamReaderInfo,
4662
                                                    SStreamReaderResponseStats* pRspStats) {
4663
  int32_t code = 0;
41,064 ✔
4664
  int32_t lino = 0;
41,064 ✔
4665
  void*   buf = NULL;
41,064 ✔
4666
  size_t  size = 0;
41,064 ✔
4667
  STableKeyInfo* pList = NULL;
41,064 ✔
4668
  SArray*        pResList = NULL;
41,064 ✔
4669
  SSDataBlock*   pBlockTmp = NULL;
41,064 ✔
4670

4671
  SStreamReaderTaskInner* pTaskInner = NULL;
41,064 ✔
4672
  void* pTask = sStreamReaderInfo->pTask;
41,064 ✔
4673
  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);
41,064 ✔
4674
  
4675
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_TRIGGER_DATA);
41,064 ✔
4676

4677
  if (req->base.type == STRIGGER_PULL_TSDB_TRIGGER_DATA) {
41,064 ✔
4678
    int32_t        pNum = 0;
20,532 ✔
4679
    STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, req->tsdbTriggerDataReq.gid, &pList, &pNum));
20,532 ✔
4680
    BUILD_OPTION(options, getSuid(sStreamReaderInfo, pList), req->tsdbTriggerDataReq.ver, req->tsdbTriggerDataReq.order, req->tsdbTriggerDataReq.startTime, INT64_MAX,
20,532 ✔
4681
                 sStreamReaderInfo->triggerCols, false, NULL);
×
4682
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, pList, pNum, &sStreamReaderInfo->storageApi));
20,532 ✔
4683
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
20,532 ✔
4684
  } else {
4685
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
20,532 ✔
4686
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
20,532 ✔
4687
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
4688
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
4689
  }
4690

4691
  blockDataCleanup(pTaskInner->pResBlockDst);
20,532 ✔
4692
  bool hasNext = true;
20,532 ✔
4693
  int32_t totalRows = 0;
20,532 ✔
4694
    
4695
  pResList = taosArrayInit(4, POINTER_BYTES);
20,532 ✔
4696
  STREAM_CHECK_NULL_GOTO(pResList, terrno);
20,532 ✔
4697
  while (1) {
23,940 ✔
4698
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
44,472 ✔
4699
    if (!hasNext) {
44,472 ✔
4700
      break;
20,532 ✔
4701
    }
4702
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
23,940 ✔
4703
    // pTaskInner->pResBlockDst->info.id.groupId = pTaskInner->pResBlock->info.id.groupId;
4704

4705
    SSDataBlock* pBlock = NULL;
23,940 ✔
4706
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
23,940 ✔
4707
    if (pBlock != NULL && pBlock->info.rows > 0) {
23,940 ✔
4708
      STREAM_CHECK_RET_GOTO(
23,940 ✔
4709
        processTag(sStreamReaderInfo, false, pBlock->info.id.uid, pBlock, 0, pBlock->info.rows, 1));
×
4710
    }
4711
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderInfo->pFilterInfo, NULL));
23,940 ✔
4712
    // STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
4713
    ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlock->info.rows);
23,940 ✔
4714
    STREAM_CHECK_RET_GOTO(createOneDataBlock(pBlock, true, &pBlockTmp));
24,006 ✔
4715
    STREAM_CHECK_NULL_GOTO(taosArrayPush(pResList, &pBlockTmp), terrno);
23,940 ✔
4716
    totalRows += blockDataGetNumOfRows(pBlockTmp);
23,940 ✔
4717
    pBlockTmp = NULL;
23,872 ✔
4718

4719
    ST_TASK_DLOG("vgId:%d %s get skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
23,872 ✔
4720
            TD_VID(pVnode), __func__, pTaskInner->pResBlock->info.window.skey, pTaskInner->pResBlock->info.window.ekey,
4721
            pTaskInner->pResBlock->info.id.uid, pTaskInner->pResBlock->info.id.groupId, pTaskInner->pResBlock->info.rows);
4722
    if (totalRows >= STREAM_RETURN_ROWS_NUM) {  //todo optimize send multi blocks in one group
23,940 ✔
4723
      break;
×
4724
    }
4725
  }
4726

4727
  STREAM_CHECK_RET_GOTO(buildArrayRsp(pResList, &buf, &size));
20,532 ✔
4728
  addReaderResponseBlocks(pRspStats, pResList);
20,532 ✔
4729
  if (!hasNext) {
20,532 ✔
4730
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
20,532 ✔
4731
  }
4732

4733
end:
40,998 ✔
4734
  STREAM_PRINT_LOG_END_WITHID(code, lino);
41,064 ✔
4735
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
41,064 ✔
4736
  taosMemoryFree(pList);
41,064 ✔
4737
  blockDataDestroy(pBlockTmp);
41,064 ✔
4738
  taosArrayDestroyP(pResList, (FDelete)blockDataDestroy);
41,064 ✔
4739
  return code;
41,064 ✔
4740
}
4741

4742
static int32_t vnodeProcessStreamTsdbCalcDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
2,025,897 ✔
4743
                                                 SStreamTriggerReaderInfo*   sStreamReaderInfo,
4744
                                                 SStreamReaderResponseStats* pRspStats) {
4745
  int32_t code = 0;
2,025,897 ✔
4746
  int32_t lino = 0;
2,025,897 ✔
4747
  void*   buf = NULL;
2,025,897 ✔
4748
  size_t  size = 0;
2,025,897 ✔
4749
  SSDataBlock*   pBlockRes = NULL;
2,025,897 ✔
4750
  STableKeyInfo* pList = NULL;
2,025,897 ✔
4751

4752

4753
  void* pTask = sStreamReaderInfo->pTask;
2,025,897 ✔
4754
  ST_TASK_DLOG("vgId:%d %s start, skey:%"PRId64",ekey:%"PRId64",gid:%"PRId64",ver:%"PRId64, TD_VID(pVnode), __func__, 
2,025,897 ✔
4755
    req->tsdbCalcDataReq.skey, req->tsdbCalcDataReq.ekey, req->tsdbCalcDataReq.gid, req->tsdbCalcDataReq.ver);
4756

4757
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->triggerCols, TSDB_CODE_STREAM_NOT_TABLE_SCAN_PLAN);
2,025,830 ✔
4758

4759
  SStreamReaderTaskInner* pTaskInner = NULL;
2,025,830 ✔
4760
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_CALC_DATA);
2,025,830 ✔
4761

4762
  if (req->base.type == STRIGGER_PULL_TSDB_CALC_DATA) {
2,025,897 ✔
4763
    int32_t        pNum = 0;
2,025,897 ✔
4764
    STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, req->tsdbCalcDataReq.gid, &pList, &pNum));
2,025,897 ✔
4765
    BUILD_OPTION(options, getSuid(sStreamReaderInfo, pList), req->tsdbCalcDataReq.ver, TSDB_ORDER_ASC, req->tsdbCalcDataReq.skey, req->tsdbCalcDataReq.ekey,
2,025,897 ✔
4766
                 sStreamReaderInfo->triggerCols, false, NULL);
×
4767
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, pList, pNum, &sStreamReaderInfo->storageApi));
2,025,897 ✔
4768

4769
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
2,025,633 ✔
4770
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
2,025,765 ✔
4771
  } else {
4772
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
4773
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
4774
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
4775
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
4776
  }
4777

4778
  blockDataCleanup(pTaskInner->pResBlockDst);
2,025,633 ✔
4779
  bool hasNext = true;
2,025,567 ✔
4780
  while (1) {
318,001 ✔
4781
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
2,343,634 ✔
4782
    if (!hasNext) {
2,343,448 ✔
4783
      break;
2,025,699 ✔
4784
    }
4785
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
317,749 ✔
4786

4787
    SSDataBlock* pBlock = NULL;
317,941 ✔
4788
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
318,001 ✔
4789
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderInfo->pFilterInfo, NULL));
317,941 ✔
4790
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
318,001 ✔
4791
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
318,001 ✔
4792
      break;
×
4793
    }
4794
  }
4795

4796
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->calcResBlock, false, &pBlockRes));
2,025,501 ✔
4797
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlockRes, pTaskInner->pResBlockDst->info.capacity));
2,025,897 ✔
4798
  blockDataTransform(pBlockRes, pTaskInner->pResBlockDst);
2,025,699 ✔
4799
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
2,025,831 ✔
4800
  addReaderResponseBlock(pRspStats, pBlockRes);
2,025,567 ✔
4801
  printDataBlock(pBlockRes, __func__, "tsdb_calc_data", ((SStreamTask*)pTask)->streamId);
2,025,567 ✔
4802
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlockRes->info.rows);
2,025,501 ✔
4803
  printDataBlock(pBlockRes, __func__, "tsdb_data", ((SStreamTask*)pTask)->streamId);
2,025,765 ✔
4804

4805
  if (!hasNext) {
2,025,897 ✔
4806
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
2,025,897 ✔
4807
  }
4808

4809
end:
2,025,765 ✔
4810
  STREAM_PRINT_LOG_END_WITHID(code, lino);
2,025,831 ✔
4811
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
2,025,897 ✔
4812
  blockDataDestroy(pBlockRes);
2,025,897 ✔
4813
  taosMemoryFree(pList);
2,025,836 ✔
4814
  return code;
2,025,699 ✔
4815
}
4816

4817
static int32_t vnodeProcessStreamTsdbVirtalDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
12,398 ✔
4818
                                                   SStreamTriggerReaderInfo*   sStreamReaderInfo,
4819
                                                   SStreamReaderResponseStats* pRspStats) {
4820
  int32_t code = 0;
12,398 ✔
4821
  int32_t lino = 0;
12,398 ✔
4822
  void*   buf = NULL;
12,398 ✔
4823
  size_t  size = 0;
12,398 ✔
4824
  int32_t* slotIdList = NULL;
12,398 ✔
4825
  SArray* sortedCid = NULL;
12,398 ✔
4826
  SArray* schemas = NULL;
12,398 ✔
4827
  SSDataBlock*   pBlockRes = NULL;
12,398 ✔
4828
  
4829
  void* pTask = sStreamReaderInfo->pTask;
12,398 ✔
4830
  ST_TASK_DLOG("vgId:%d %s start, skey:%"PRId64",ekey:%"PRId64",uid:%"PRId64",ver:%"PRId64, TD_VID(pVnode), __func__, 
12,398 ✔
4831
    req->tsdbDataReq.skey, req->tsdbDataReq.ekey, req->tsdbDataReq.uid, req->tsdbDataReq.ver);
4832
    
4833
  SStreamReaderTaskInner* pTaskInner = NULL;
12,398 ✔
4834
  int64_t key = req->tsdbDataReq.uid;
12,398 ✔
4835

4836
  if (req->base.type == STRIGGER_PULL_TSDB_DATA) {
12,398 ✔
4837
    // sort cid and build slotIdList
4838
    slotIdList = taosMemoryMalloc(taosArrayGetSize(req->tsdbDataReq.cids) * sizeof(int32_t));
12,398 ✔
4839
    STREAM_CHECK_NULL_GOTO(slotIdList, terrno);
12,398 ✔
4840
    sortedCid = taosArrayDup(req->tsdbDataReq.cids, NULL);
12,398 ✔
4841
    STREAM_CHECK_NULL_GOTO(sortedCid, terrno);
12,398 ✔
4842
    taosArraySort(sortedCid, sortCid);
12,398 ✔
4843
    for (int32_t i = 0; i < taosArrayGetSize(req->tsdbDataReq.cids); i++) {
38,118 ✔
4844
      int16_t* cid = taosArrayGet(req->tsdbDataReq.cids, i);
25,720 ✔
4845
      STREAM_CHECK_NULL_GOTO(cid, terrno);
25,720 ✔
4846
      for (int32_t j = 0; j < taosArrayGetSize(sortedCid); j++) {
40,494 ✔
4847
        int16_t* cidSorted = taosArrayGet(sortedCid, j);
40,494 ✔
4848
        STREAM_CHECK_NULL_GOTO(cidSorted, terrno);
40,494 ✔
4849
        if (*cid == *cidSorted) {
40,494 ✔
4850
          slotIdList[j] = i;
25,720 ✔
4851
          break;
25,720 ✔
4852
        }
4853
      }
4854
    }
4855

4856
    STREAM_CHECK_RET_GOTO(buildScheamFromMeta(pVnode, req->tsdbDataReq.uid, &schemas, &sStreamReaderInfo->storageApi));
12,398 ✔
4857
    STREAM_CHECK_RET_GOTO(shrinkScheams(req->tsdbDataReq.cids, schemas));
12,398 ✔
4858
    STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, &pBlockRes));
12,398 ✔
4859

4860
    taosArraySort(schemas, sortSSchema);
12,398 ✔
4861
    BUILD_OPTION(options, req->tsdbDataReq.suid, req->tsdbDataReq.ver, req->tsdbDataReq.order, req->tsdbDataReq.skey,
12,398 ✔
4862
                    req->tsdbDataReq.ekey, schemas, true, &slotIdList);
×
4863
    STableKeyInfo       keyInfo = {.uid = req->tsdbDataReq.uid, .groupId = 0};
12,398 ✔
4864
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, pBlockRes, &keyInfo, 1, &sStreamReaderInfo->storageApi));
12,398 ✔
4865
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
12,398 ✔
4866
    pTaskInner->pResBlockDst = pBlockRes;
12,398 ✔
4867
    pBlockRes = NULL;
12,398 ✔
4868
  } else {
4869
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
4870
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
4871
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
4872
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
4873
  }
4874

4875
  blockDataCleanup(pTaskInner->pResBlockDst);
12,398 ✔
4876
  bool hasNext = true;
12,398 ✔
4877
  while (1) {
12,398 ✔
4878
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
24,796 ✔
4879
    if (!hasNext) {
24,796 ✔
4880
      break;
12,398 ✔
4881
    }
4882

4883
    SSDataBlock* pBlock = NULL;
12,398 ✔
4884
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
12,398 ✔
4885
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
12,398 ✔
4886
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
12,398 ✔
4887
      break;
×
4888
    }
4889
  }
4890
  STREAM_CHECK_RET_GOTO(buildRsp(pTaskInner->pResBlockDst, &buf, &size));
12,398 ✔
4891
  addReaderResponseBlock(pRspStats, pTaskInner->pResBlockDst);
12,398 ✔
4892
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
12,398 ✔
4893
  printDataBlock(pTaskInner->pResBlockDst, __func__, "tsdb_data", ((SStreamTask*)pTask)->streamId);
12,398 ✔
4894
  if (!hasNext) {
12,398 ✔
4895
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
12,398 ✔
4896
  }
4897

4898
end:
12,398 ✔
4899
  STREAM_PRINT_LOG_END_WITHID(code, lino);
12,398 ✔
4900
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
12,398 ✔
4901
  taosMemFree(slotIdList);
12,398 ✔
4902
  taosArrayDestroy(sortedCid);
12,398 ✔
4903
  taosArrayDestroy(schemas);
12,398 ✔
4904
  blockDataDestroy(pBlockRes);
12,398 ✔
4905
  return code;
12,398 ✔
4906
}
4907

4908
static int32_t vnodeProcessStreamWalMetaNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
1,095,610 ✔
4909
                                               SStreamTriggerReaderInfo*   sStreamReaderInfo,
4910
                                               SStreamReaderResponseStats* pRspStats) {
4911
  int32_t      code = 0;
1,095,610 ✔
4912
  int32_t      lino = 0;
1,095,610 ✔
4913
  void*        buf = NULL;
1,095,610 ✔
4914
  size_t       size = 0;
1,095,610 ✔
4915
  int64_t      lastVer = 0;
1,095,610 ✔
4916
  SSTriggerWalNewRsp resultRsp = {0};
1,095,610 ✔
4917

4918
  void* pTask = sStreamReaderInfo->pTask;
1,095,661 ✔
4919
  ST_TASK_DLOG("vgId:%d %s start, request paras lastVer:%" PRId64, TD_VID(pVnode), __func__, req->walMetaNewReq.lastVer);
1,095,661 ✔
4920

4921
  if (sStreamReaderInfo->metaBlock == NULL) {
1,095,886 ✔
4922
    STREAM_CHECK_RET_GOTO(
30,976 ✔
4923
        createBlockForWalMetaNew((SSDataBlock**)&sStreamReaderInfo->metaBlock,
4924
                                 isRollupMultiReader(sStreamReaderInfo)));
30 ✔
4925
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(sStreamReaderInfo->metaBlock, STREAM_RETURN_ROWS_NUM));
30,976 ✔
4926
  }
4927
  blockDataEmpty(sStreamReaderInfo->metaBlock);
1,095,886 ✔
4928
  resultRsp.metaBlock = sStreamReaderInfo->metaBlock;
1,094,968 ✔
4929
  resultRsp.ver = req->walMetaNewReq.lastVer;
1,094,968 ✔
4930
  {
4931
    int32_t hookRc = streamMaybeRecheckVTableCache(pVnode, sStreamReaderInfo, resultRsp.ver, &resultRsp);
1,094,968 ✔
4932
    ST_TASK_DLOG("vgId:%d %s hook rc=0x%x ver=%" PRId64, TD_VID(pVnode), __func__, hookRc, resultRsp.ver);
1,095,452 ✔
4933
    // H2 v0.5: any non-zero hook rc (TAG_CHANGED, REF_TABLE_NOT_EXIST,
4934
    // REF_COL_NOT_EXIST, REF_TOO_DEEP, RPC failure, etc.) is propagated to
4935
    // the trigger via rsp.code so it can fail-fast and request a redeploy.
4936
    if (hookRc != 0) { code = hookRc; goto end; }
1,095,743 ✔
4937
  }
4938
  STREAM_CHECK_RET_GOTO(processWalVerMetaNew(pVnode, &resultRsp, sStreamReaderInfo, req->walMetaNewReq.ctime));
1,095,479 ✔
4939

4940
  ST_TASK_DLOG("vgId:%d %s get result last ver:%"PRId64" rows:%d", TD_VID(pVnode), __func__, resultRsp.ver, resultRsp.totalRows);
1,095,277 ✔
4941
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
1,095,331 ✔
4942
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
26,942 ✔
4943
  buf = rpcMallocCont(size);
27,008 ✔
4944
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
27,008 ✔
4945
  printDataBlock(sStreamReaderInfo->metaBlock, __func__, "meta", ((SStreamTask*)pTask)->streamId);
27,008 ✔
4946
  printDataBlock(resultRsp.deleteBlock, __func__, "delete", ((SStreamTask*)pTask)->streamId);
26,823 ✔
4947
  printDataBlock(resultRsp.tableBlock, __func__, "table", ((SStreamTask*)pTask)->streamId);
26,823 ✔
4948

4949
end:
1,094,976 ✔
4950
  if (code == 0 && resultRsp.totalRows == 0) {
1,095,417 ✔
4951
    code = TSDB_CODE_STREAM_NO_DATA;
1,068,330 ✔
4952
    size = sizeof(int64_t) * 2;
1,068,330 ✔
4953
    buf = rpcMallocCont(size);
1,068,330 ✔
4954
    *(int64_t*)buf = resultRsp.ver;
1,068,302 ✔
4955
    *(((int64_t*)buf) + 1) = resultRsp.verTime;
1,068,302 ✔
4956
  }
4957
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
1,095,389 ✔
4958
  if (code == TSDB_CODE_STREAM_NO_DATA){
1,095,151 ✔
4959
    code = 0;
1,067,629 ✔
4960
  }
4961
  STREAM_PRINT_LOG_END_WITHID(code, lino);
1,095,151 ✔
4962
  blockDataDestroy(resultRsp.deleteBlock);
1,095,677 ✔
4963
  blockDataDestroy(resultRsp.tableBlock);
1,095,808 ✔
4964

4965
  return code;
1,095,808 ✔
4966
}
4967
static int32_t vnodeProcessStreamWalMetaDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
1,416,083 ✔
4968
                                                   SStreamTriggerReaderInfo*   sStreamReaderInfo,
4969
                                                   SStreamReaderResponseStats* pRspStats) {
4970
  int32_t      code = 0;
1,416,083 ✔
4971
  int32_t      lino = 0;
1,416,083 ✔
4972
  void*        buf = NULL;
1,416,083 ✔
4973
  size_t       size = 0;
1,416,083 ✔
4974
  SSTriggerWalNewRsp resultRsp = {0};
1,416,083 ✔
4975
  
4976
  void* pTask = sStreamReaderInfo->pTask;
1,416,230 ✔
4977
  ST_TASK_DLOG("vgId:%d %s start, request paras lastVer:%" PRId64 ", endVer:%" PRId64, TD_VID(pVnode), __func__,
1,416,230 ✔
4978
               req->walMetaDataNewReq.lastVer, req->walMetaDataNewReq.endVer);
4979

4980
  if (req->walMetaDataNewReq.endVer > 0 && req->walMetaDataNewReq.endVer <= req->walMetaDataNewReq.lastVer) {
1,416,189 ✔
4981
    code = TSDB_CODE_INVALID_PARA;
×
4982
    goto end;
×
4983
  }
4984

4985
  if (sStreamReaderInfo->metaBlock == NULL) {
1,416,189 ✔
4986
    STREAM_CHECK_RET_GOTO(
38,560 ✔
4987
        createBlockForWalMetaNew((SSDataBlock**)&sStreamReaderInfo->metaBlock,
4988
                                 isRollupMultiReader(sStreamReaderInfo)));
44 ✔
4989
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(sStreamReaderInfo->metaBlock, STREAM_RETURN_ROWS_NUM));
38,560 ✔
4990
  }
4991

4992
  resultRsp.metaBlock = sStreamReaderInfo->metaBlock;
1,416,189 ✔
4993
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerBlock, false, (SSDataBlock**)&resultRsp.dataBlock));
1,416,189 ✔
4994
  resultRsp.ver = req->walMetaDataNewReq.lastVer;
1,415,475 ✔
4995
  resultRsp.checkAlter = true;
1,415,475 ✔
4996
  resultRsp.indexHash = tSimpleHashInit(8, taosGetDefaultHashFunction(getWalDataSliceHashType(sStreamReaderInfo)));
1,416,224 ✔
4997
  STREAM_CHECK_NULL_GOTO(resultRsp.indexHash, terrno);
1,415,782 ✔
4998
  {
4999
    int32_t hookRc = streamMaybeRecheckVTableCache(pVnode, sStreamReaderInfo, resultRsp.ver, &resultRsp);
1,415,782 ✔
5000
    ST_TASK_DLOG("vgId:%d %s hook rc=0x%x ver=%" PRId64, TD_VID(pVnode), __func__, hookRc, resultRsp.ver);
1,416,457 ✔
5001
    // H2 v0.5: propagate any hook error so trigger fail-fasts.
5002
    if (hookRc != 0) { code = hookRc; goto end; }
1,416,695 ✔
5003
  }
5004

5005
  STREAM_CHECK_RET_GOTO(processWalVerMetaDataNew(pVnode, sStreamReaderInfo, &resultRsp, req->walMetaDataNewReq.endVer));
1,416,695 ✔
5006

5007
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
1,415,991 ✔
5008
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
54,580 ✔
5009
  buf = rpcMallocCont(size);
54,869 ✔
5010
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
54,869 ✔
5011
  printDataBlock(sStreamReaderInfo->metaBlock, __func__, "meta", ((SStreamTask*)pTask)->streamId);
54,869 ✔
5012
  printDataBlock(resultRsp.dataBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
54,869 ✔
5013
  printDataBlock(resultRsp.deleteBlock, __func__, "delete", ((SStreamTask*)pTask)->streamId);
54,803 ✔
5014
  printDataBlock(resultRsp.tableBlock, __func__, "table", ((SStreamTask*)pTask)->streamId);
54,869 ✔
5015
  printIndexHash(resultRsp.indexHash, pTask);
54,869 ✔
5016
  stReaderResponseStatsSetWalData(pRspStats, &resultRsp);
54,869 ✔
5017
  pRspStats->lastReturnedWalVer = resultRsp.ver;
54,869 ✔
5018
  pRspStats->lastReturnedWalVerValid = true;
54,869 ✔
5019

5020
end:
1,415,596 ✔
5021
  if (code == 0 && resultRsp.totalRows == 0) {
1,415,859 ✔
5022
    code = TSDB_CODE_STREAM_NO_DATA;
1,361,826 ✔
5023
    size = sizeof(int64_t) * 2;
1,361,826 ✔
5024
    buf = rpcMallocCont(size);
1,361,826 ✔
5025
    *(int64_t*)buf = resultRsp.ver;
1,360,719 ✔
5026
    *(((int64_t*)buf) + 1) = resultRsp.verTime;
1,360,719 ✔
5027
  }
5028
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
1,414,752 ✔
5029
  if (code == TSDB_CODE_STREAM_NO_DATA){
1,416,148 ✔
5030
    code = 0;
1,361,719 ✔
5031
  }
5032
  blockDataDestroy(resultRsp.dataBlock);
1,416,148 ✔
5033
  blockDataDestroy(resultRsp.deleteBlock);
1,415,587 ✔
5034
  blockDataDestroy(resultRsp.tableBlock);
1,415,825 ✔
5035
  tSimpleHashCleanup(resultRsp.indexHash);
1,416,198 ✔
5036

5037
  STREAM_PRINT_LOG_END_WITHID(code, lino);
1,414,391 ✔
5038

5039
  return code;
1,415,236 ✔
5040
}
5041

5042
static int32_t vnodeProcessStreamWalDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
150,254 ✔
5043
                                               SStreamTriggerReaderInfo*   sStreamReaderInfo,
5044
                                               SStreamReaderResponseStats* pRspStats) {
5045
  int32_t      code = 0;
150,254 ✔
5046
  int32_t      lino = 0;
150,254 ✔
5047
  void*        buf = NULL;
150,254 ✔
5048
  size_t       size = 0;
150,254 ✔
5049
  SSTriggerWalNewRsp resultRsp = {0};
150,254 ✔
5050

5051
  void* pTask = sStreamReaderInfo->pTask;
150,254 ✔
5052
  ST_TASK_DLOG("vgId:%d %s start, request paras size:%zu", TD_VID(pVnode), __func__, taosArrayGetSize(req->walDataNewReq.versions));
150,254 ✔
5053

5054
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerBlock, false, (SSDataBlock**)&resultRsp.dataBlock));
150,254 ✔
5055
  resultRsp.indexHash = tSimpleHashInit(8, taosGetDefaultHashFunction(getWalDataSliceHashType(sStreamReaderInfo)));
150,340 ✔
5056
  STREAM_CHECK_NULL_GOTO(resultRsp.indexHash, terrno);
150,208 ✔
5057
  resultRsp.uidHash = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
150,208 ✔
5058
  STREAM_CHECK_NULL_GOTO(resultRsp.uidHash, terrno);
150,340 ✔
5059

5060
  STREAM_CHECK_RET_GOTO(processWalVerDataNew(pVnode, sStreamReaderInfo, req->walDataNewReq.versions, req->walDataNewReq.ranges, &resultRsp));
150,340 ✔
5061
  ST_TASK_DLOG("vgId:%d %s get result last ver:%"PRId64" rows:%d", TD_VID(pVnode), __func__, resultRsp.ver, resultRsp.totalRows);
150,335 ✔
5062

5063
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
150,335 ✔
5064

5065
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
7,021 ✔
5066
  buf = rpcMallocCont(size);
7,021 ✔
5067
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
7,021 ✔
5068
  printDataBlock(resultRsp.dataBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
7,021 ✔
5069
  printIndexHash(resultRsp.indexHash, pTask);
7,021 ✔
5070
  stReaderResponseStatsSetWalData(pRspStats, &resultRsp);
7,021 ✔
5071
  pRspStats->lastReturnedWalVer = resultRsp.ver;
7,021 ✔
5072
  pRspStats->lastReturnedWalVerValid = true;
7,021 ✔
5073

5074
end:
150,335 ✔
5075
  if (code == 0 && resultRsp.totalRows == 0) {
150,335 ✔
5076
    buf = rpcMallocCont(sizeof(int64_t));
143,314 ✔
5077
    *(int64_t *)buf = resultRsp.ver;
143,248 ✔
5078
    size = sizeof(int64_t);
143,248 ✔
5079
    code = TSDB_CODE_STREAM_NO_DATA;
143,248 ✔
5080
  }
5081
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
150,269 ✔
5082
  if (code == TSDB_CODE_STREAM_NO_DATA){
150,269 ✔
5083
    code = 0;
143,248 ✔
5084
  }
5085

5086
  blockDataDestroy(resultRsp.dataBlock);
150,269 ✔
5087
  blockDataDestroy(resultRsp.deleteBlock);
150,401 ✔
5088
  blockDataDestroy(resultRsp.tableBlock);
150,401 ✔
5089
  tSimpleHashCleanup(resultRsp.indexHash);
150,401 ✔
5090
  tSimpleHashCleanup(resultRsp.uidHash);
150,401 ✔
5091
  STREAM_PRINT_LOG_END_WITHID(code, lino);
150,320 ✔
5092

5093
  return code;
150,320 ✔
5094
}
5095

5096
static int32_t vnodeProcessStreamWalCalcDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
91,670 ✔
5097
                                                   SStreamTriggerReaderInfo*   sStreamReaderInfo,
5098
                                                   SStreamReaderResponseStats* pRspStats) {
5099
  int32_t      code = 0;
91,670 ✔
5100
  int32_t      lino = 0;
91,670 ✔
5101
  void*        buf = NULL;
91,670 ✔
5102
  size_t       size = 0;
91,670 ✔
5103
  SSTriggerWalNewRsp resultRsp = {0};
91,670 ✔
5104
  SSDataBlock* pBlock1 = NULL;
91,670 ✔
5105
  SSDataBlock* pBlock2 = NULL;
91,670 ✔
5106
  
5107
  void* pTask = sStreamReaderInfo->pTask;
91,670 ✔
5108
  ST_TASK_DLOG("vgId:%d %s start, request paras size:%zu", TD_VID(pVnode), __func__, taosArrayGetSize(req->walDataNewReq.versions));
91,670 ✔
5109

5110
  SSDataBlock* dataBlock = sStreamReaderInfo->isVtableStream ? sStreamReaderInfo->calcBlock : sStreamReaderInfo->triggerBlock;
91,670 ✔
5111
  STREAM_CHECK_RET_GOTO(createOneDataBlock(dataBlock, false, (SSDataBlock**)&resultRsp.dataBlock));
91,670 ✔
5112
  resultRsp.isCalc = sStreamReaderInfo->isVtableStream ? true : false;
91,670 ✔
5113
  resultRsp.indexHash = tSimpleHashInit(8, taosGetDefaultHashFunction(getWalDataSliceHashType(sStreamReaderInfo)));
91,670 ✔
5114
  STREAM_CHECK_NULL_GOTO(resultRsp.indexHash, terrno);
91,670 ✔
5115
  resultRsp.uidHash = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
91,670 ✔
5116
  STREAM_CHECK_NULL_GOTO(resultRsp.uidHash, terrno);
91,670 ✔
5117

5118
  STREAM_CHECK_RET_GOTO(processWalVerDataNew(pVnode, sStreamReaderInfo, req->walDataNewReq.versions, req->walDataNewReq.ranges, &resultRsp));
91,670 ✔
5119
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
91,670 ✔
5120

5121
  if (!sStreamReaderInfo->isVtableStream){
79,554 ✔
5122
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->calcBlock, false, &pBlock2));
72,109 ✔
5123
  
5124
    blockDataTransform(pBlock2, resultRsp.dataBlock);
72,109 ✔
5125
    blockDataDestroy(resultRsp.dataBlock);
72,109 ✔
5126
    resultRsp.dataBlock = pBlock2;
72,109 ✔
5127
    pBlock2 = NULL;
72,109 ✔
5128
  }
5129

5130
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
79,554 ✔
5131
  buf = rpcMallocCont(size);
79,475 ✔
5132
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
79,396 ✔
5133
  printDataBlock(resultRsp.dataBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
79,475 ✔
5134
  printIndexHash(resultRsp.indexHash, pTask);
79,475 ✔
5135
  stReaderResponseStatsSetWalData(pRspStats, &resultRsp);
79,317 ✔
5136
  pRspStats->lastReturnedWalVer = resultRsp.ver;
79,317 ✔
5137
  pRspStats->lastReturnedWalVerValid = true;
79,317 ✔
5138

5139
end:
91,433 ✔
5140
  if (code == 0 && resultRsp.totalRows == 0) {
91,433 ✔
5141
    buf = rpcMallocCont(sizeof(int64_t));
12,116 ✔
5142
    *(int64_t *)buf = resultRsp.ver;
12,116 ✔
5143
    size = sizeof(int64_t);
12,116 ✔
5144
    code = TSDB_CODE_STREAM_NO_DATA;
12,116 ✔
5145
  }
5146
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
91,433 ✔
5147
  if (code == TSDB_CODE_STREAM_NO_DATA){
91,591 ✔
5148
    code = 0;
12,116 ✔
5149
  }
5150

5151
  blockDataDestroy(pBlock1);
91,591 ✔
5152
  blockDataDestroy(pBlock2);
91,591 ✔
5153
  blockDataDestroy(resultRsp.dataBlock);
91,670 ✔
5154
  blockDataDestroy(resultRsp.deleteBlock);
91,591 ✔
5155
  blockDataDestroy(resultRsp.tableBlock);
91,591 ✔
5156
  tSimpleHashCleanup(resultRsp.indexHash);
91,526 ✔
5157
  tSimpleHashCleanup(resultRsp.uidHash);
91,512 ✔
5158
  STREAM_PRINT_LOG_END_WITHID(code, lino);
91,433 ✔
5159

5160
  return code;
91,591 ✔
5161
}
5162

5163
static int32_t vnodeProcessStreamGroupColValueReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
64,237 ✔
5164
                                                  SStreamTriggerReaderInfo*   sStreamReaderInfo,
5165
                                                  SStreamReaderResponseStats* pRspStats) {
5166
  int32_t code = 0;
64,237 ✔
5167
  int32_t lino = 0;
64,237 ✔
5168
  void*   buf = NULL;
64,237 ✔
5169
  size_t  size = 0;
64,237 ✔
5170
  SArray** gInfo = NULL;
64,237 ✔
5171
  
5172
  void* pTask = sStreamReaderInfo->pTask;
64,237 ✔
5173
  ST_TASK_DLOG("vgId:%d %s start, request gid:%" PRId64, TD_VID(pVnode), __func__, req->groupColValueReq.gid);
64,237 ✔
5174

5175
  gInfo = taosHashAcquire(sStreamReaderInfo->groupIdMap, &req->groupColValueReq.gid, POINTER_BYTES);
64,237 ✔
5176
  STREAM_CHECK_NULL_GOTO(gInfo, TSDB_CODE_STREAM_NO_CONTEXT);
64,237 ✔
5177
  SStreamGroupInfo pGroupInfo = {0};
64,237 ✔
5178
  pGroupInfo.gInfo = *gInfo;
64,237 ✔
5179

5180
  size = tSerializeSStreamGroupInfo(NULL, 0, &pGroupInfo, TD_VID(pVnode));
64,237 ✔
5181
  STREAM_CHECK_CONDITION_GOTO(size < 0, size);
×
5182
  buf = rpcMallocCont(size);
64,237 ✔
5183
  STREAM_CHECK_NULL_GOTO(buf, terrno);
64,237 ✔
5184
  size = tSerializeSStreamGroupInfo(buf, size, &pGroupInfo, TD_VID(pVnode));
64,237 ✔
5185
  STREAM_CHECK_CONDITION_GOTO(size < 0, size);
×
5186
end:
64,237 ✔
5187
  taosHashRelease(sStreamReaderInfo->groupIdMap, gInfo);
64,237 ✔
5188
  if (code != 0) {
64,237 ✔
5189
    rpcFreeCont(buf);
×
5190
    buf = NULL;
×
5191
    size = 0;
×
5192
  }
5193
  STREAM_PRINT_LOG_END_WITHID(code, lino);
64,237 ✔
5194
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
64,237 ✔
5195

5196
  return code;
64,237 ✔
5197
}
5198

5199
static int32_t setVtableInfo(SVnode* pVnode, SArray* infos, SArray* cids, int64_t uid, uint64_t gid, int64_t ver, SMetaReader* metaReader, SStreamTriggerReaderInfo* sStreamReaderInfo) {
×
5200
  int32_t              code = 0;
×
5201
  int32_t              lino = 0;
×
5202
  void* pTask = sStreamReaderInfo->pTask;
×
5203

5204
  VTableInfo* vTable = taosArrayReserve(infos, 1);
×
5205
  STREAM_CHECK_NULL_GOTO(vTable, terrno);
×
5206
  vTable->uid = uid;
×
5207
  vTable->gId = gid;
×
5208

5209
  ST_TASK_DLOG("vgId:%d %s put vtable uid:%"PRId64, TD_VID(pVnode), __func__, uid);
×
5210

5211
  code = sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(metaReader, ver, uid);
×
5212
  if (code != 0) {
×
5213
    ST_TASK_ELOG("vgId:%d %s get table entry by uid:%"PRId64" failed, msg:%s", TD_VID(pVnode), __func__, uid, tstrerror(code));
×
5214
    goto end;
×
5215
  }
5216
  if (atomic_load_8(&sStreamReaderInfo->isVtableOnlyTs) == 1) {
×
5217
    vTable->cols.nCols = metaReader->me.colRef.nCols;
×
5218
    vTable->cols.version = metaReader->me.colRef.version;
×
5219
    vTable->cols.pColRef = taosMemoryCalloc(metaReader->me.colRef.nCols, sizeof(SColRef));
×
5220
    STREAM_CHECK_NULL_GOTO(vTable->cols.pColRef, terrno);
×
5221
    for (size_t j = 0; j < metaReader->me.colRef.nCols; j++) {
×
5222
      memcpy(vTable->cols.pColRef + j, &metaReader->me.colRef.pColRef[j], sizeof(SColRef));
×
5223
    }
5224
  } else {
5225
    vTable->cols.nCols = taosArrayGetSize(cids);
×
5226
    vTable->cols.version = metaReader->me.colRef.version;
×
5227
    vTable->cols.pColRef = taosMemoryCalloc(taosArrayGetSize(cids), sizeof(SColRef));
×
5228
    STREAM_CHECK_NULL_GOTO(vTable->cols.pColRef, terrno);
×
5229
    for (size_t i = 0; i < taosArrayGetSize(cids); i++) {
×
5230
      for (size_t j = 0; j < metaReader->me.colRef.nCols; j++) {
×
5231
        if (metaReader->me.colRef.pColRef[j].hasRef &&
×
5232
            metaReader->me.colRef.pColRef[j].id == *(col_id_t*)taosArrayGet(cids, i)) {
×
5233
          memcpy(vTable->cols.pColRef + i, &metaReader->me.colRef.pColRef[j], sizeof(SColRef));
×
5234
          break;
×
5235
        }
5236
      }
5237
    }
5238
  }
5239
  tDecoderClear(&metaReader->coder);
×
5240

5241
end:
×
5242
  return code;
×
5243
}
5244

5245
// Collect all rollup gids for a given uid from uIdMap (under read-lock) into rollupGids.
5246
// Returns TSDB_CODE_SUCCESS on success; caller owns rollupGids and must destroy it.
5247
static int32_t collectRollupGids(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo,
324 ✔
5248
                                 int64_t uid, const char* caller, SArray** ppRollupGids) {
5249
  void* pTask = sStreamReaderInfo->pTask;
324 ✔
5250
  SArray* rollupGids = taosArrayInit(4, sizeof(uint64_t));
324 ✔
5251
  if (rollupGids == NULL) return terrno;
324 ✔
5252

5253
  taosRLockLatch(&sStreamReaderInfo->lock);
324 ✔
5254
  SArray** pElements = taosHashGet(sStreamReaderInfo->tableList.uIdMap, &uid, LONG_BYTES);
324 ✔
5255
  if (pElements != NULL) {
324 ✔
5256
    int32_t numOfElements = taosArrayGetSize(*pElements);
324 ✔
5257
    ST_TASK_DLOG("vgId:%d %s uid:%" PRId64 " rollup gid count:%d", TD_VID(pVnode), caller, uid, numOfElements);
324 ✔
5258
    for (int32_t j = 0; j < numOfElements; ++j) {
1,296 ✔
5259
      SStreamTableMapElement* element = taosArrayGet(*pElements, j);
972 ✔
5260
      if (element == NULL || element->table == NULL || element->table->markedDeleted) continue;
972 ✔
5261
      if (taosArrayPush(rollupGids, &element->table->groupId) == NULL) {
1,944 ✔
5262
        taosRUnLockLatch(&sStreamReaderInfo->lock);
×
5263
        taosArrayDestroy(rollupGids);
×
5264
        return terrno;
×
5265
      }
5266
    }
5267
  } else {
5268
    ST_TASK_WLOG("vgId:%d %s uid:%" PRId64 " not found in uIdMap", TD_VID(pVnode), caller, uid);
×
5269
  }
5270
  taosRUnLockLatch(&sStreamReaderInfo->lock);
324 ✔
5271

5272
  *ppRollupGids = rollupGids;
324 ✔
5273
  return TSDB_CODE_SUCCESS;
324 ✔
5274
}
5275

5276
// Fill VTableInfo entries for all uids in uid2Result (fullScan path).
5277
// For rollup vstb streams every uid maps to multiple rollup gids; for regular vtable
5278
// streams each uid maps to a single groupId looked up via qStreamGetGroupIdFromOrigin.
5279
static int32_t getAllVinfo(SVnode* pVnode, SStreamMsgVTableInfo* vTableInfo, SSHashObj* uid2Result,
7,892 ✔
5280
                           SArray* cids, int64_t ver, SMetaReader* metaReader,
5281
                           SStreamTriggerReaderInfo* sStreamReaderInfo) {
5282
  int32_t code = 0;
7,892 ✔
5283
  void*   pTask = sStreamReaderInfo->pTask;
7,892 ✔
5284

5285
  void*   iter = NULL;
7,892 ✔
5286
  int32_t it   = 0;
7,892 ✔
5287
  while ((iter = tSimpleHashIterate(uid2Result, iter, &it)) != NULL) {
19,006 ✔
5288
    int64_t              uid = *(int64_t*)tSimpleHashGetKey(iter, NULL);
11,114 ✔
5289
    SVTableResolveResult* r  = *(SVTableResolveResult**)iter;
11,114 ✔
5290

5291
    if (sStreamReaderInfo->isRollupReader) {
11,114 ✔
5292
      // vstb rollup: one uid → multiple rollup gids
5293
      SArray* rollupGids = NULL;
324 ✔
5294
      code = collectRollupGids(pVnode, sStreamReaderInfo, uid, __func__, &rollupGids);
324 ✔
5295
      if (code != 0) return code;
324 ✔
5296

5297
      for (int32_t j = 0; j < taosArrayGetSize(rollupGids); ++j) {
1,296 ✔
5298
        uint64_t rollupGid = *(uint64_t*)taosArrayGet(rollupGids, j);
972 ✔
5299
        int32_t  rc = streamFillVTableInfoFromResolved(pVnode, sStreamReaderInfo, uid, rollupGid,
972 ✔
5300
                                                       ver, cids, r, metaReader, vTableInfo->infos);
5301
        if (rc != 0) { taosArrayDestroy(rollupGids); return rc; }
972 ✔
5302
      }
5303
      taosArrayDestroy(rollupGids);
324 ✔
5304
    } else {
5305
      // regular vtable stream: one uid → one groupId
5306
      taosRLockLatch(&sStreamReaderInfo->lock);
10,790 ✔
5307
      uint64_t groupId = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, uid);
10,790 ✔
5308
      taosRUnLockLatch(&sStreamReaderInfo->lock);
10,790 ✔
5309
      if (groupId == (uint64_t)-1) continue;
10,790 ✔
5310

5311
      int32_t rc = streamFillVTableInfoFromResolved(pVnode, sStreamReaderInfo, uid, groupId,
10,790 ✔
5312
                                                    ver, cids, r, metaReader, vTableInfo->infos);
5313
      if (rc != 0) return rc;
10,790 ✔
5314
    }
5315
  }
5316
  return TSDB_CODE_SUCCESS;
7,892 ✔
5317
}
5318

5319
// Fill VTableInfo entries for the specific uids listed in req->virTableInfoReq.uids
5320
// (non-fullScan path).  uid2Result provides the resolved vtable metadata for each uid.
5321
static int32_t getSpicificVinfo(SVnode* pVnode, SStreamMsgVTableInfo* vTableInfo, SSHashObj* uid2Result,
×
5322
                                SArray* uids, SArray* cids, int64_t ver, SMetaReader* metaReader,
5323
                                SStreamTriggerReaderInfo* sStreamReaderInfo) {
5324
  int32_t code  = 0;
×
5325
  void*   pTask = sStreamReaderInfo->pTask;
×
5326
  int32_t nReq  = (int32_t)taosArrayGetSize(uids);
×
5327

5328
  for (int32_t i = 0; i < nReq; ++i) {
×
5329
    int64_t              uid = *(int64_t*)taosArrayGet(uids, i);
×
5330
    SVTableResolveResult** pp = (SVTableResolveResult**)tSimpleHashGet(uid2Result, &uid, sizeof(uid));
×
5331
    if (pp == NULL || *pp == NULL) continue;
×
5332

5333
    if (sStreamReaderInfo->isRollupReader) {
×
5334
      // vstb rollup: one uid → multiple rollup gids
5335
      SArray* rollupGids = NULL;
×
5336
      code = collectRollupGids(pVnode, sStreamReaderInfo, uid, __func__, &rollupGids);
×
5337
      if (code != 0) return code;
×
5338

5339
      for (int32_t j = 0; j < taosArrayGetSize(rollupGids); ++j) {
×
5340
        uint64_t rollupGid = *(uint64_t*)taosArrayGet(rollupGids, j);
×
5341
        int32_t  rc = streamFillVTableInfoFromResolved(pVnode, sStreamReaderInfo, uid, rollupGid,
×
5342
                                                       ver, cids, *pp, metaReader, vTableInfo->infos);
5343
        if (rc != 0) { taosArrayDestroy(rollupGids); return rc; }
×
5344
      }
5345
      taosArrayDestroy(rollupGids);
×
5346
    } else {
5347
      // regular vtable stream: one uid → one groupId
5348
      taosRLockLatch(&sStreamReaderInfo->lock);
×
5349
      uint64_t groupId = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, uid);
×
5350
      taosRUnLockLatch(&sStreamReaderInfo->lock);
×
5351
      if (groupId == (uint64_t)-1) continue;
×
5352

5353
      int32_t rc = streamFillVTableInfoFromResolved(pVnode, sStreamReaderInfo, uid, groupId,
×
5354
                                                    ver, cids, *pp, metaReader, vTableInfo->infos);
5355
      if (rc != 0) return rc;
×
5356
    }
5357
  }
5358
  return TSDB_CODE_SUCCESS;
×
5359
}
5360

5361
static int32_t vnodeProcessStreamVTableInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
7,892 ✔
5362
                                               SStreamTriggerReaderInfo*   sStreamReaderInfo,
5363
                                               SStreamReaderResponseStats* pRspStats) {
5364
  int32_t              code = 0;
7,892 ✔
5365
  int32_t              lino = 0;
7,892 ✔
5366
  void*                buf = NULL;
7,892 ✔
5367
  size_t               size = 0;
7,892 ✔
5368
  SStreamMsgVTableInfo vTableInfo = {0};
7,892 ✔
5369
  SMetaReader          metaReader = {0};
7,892 ✔
5370
  SArray              *tagCids   = NULL;
7,892 ✔
5371
  SSHashObj           *uid2Result = NULL;
7,892 ✔
5372

5373
  void* pTask = sStreamReaderInfo->pTask;
7,892 ✔
5374
  ST_TASK_DLOG("vgId:%d %s start, version:%"PRId64, TD_VID(pVnode), __func__, req->virTableInfoReq.ver);
7,892 ✔
5375

5376
  SArray* cids = req->virTableInfoReq.cids;
7,892 ✔
5377
  STREAM_CHECK_NULL_GOTO(cids, terrno);
7,892 ✔
5378

5379
  if (taosArrayGetSize(cids) == 1 && *(col_id_t*)taosArrayGet(cids, 0) == PRIMARYKEY_TIMESTAMP_COL_ID){
7,892 ✔
5380
    (void)atomic_val_compare_exchange_8(&sStreamReaderInfo->isVtableOnlyTs, 0, 1);
1,841 ✔
5381
  }
5382

5383
  bool fullScan = req->virTableInfoReq.fetchAllTable
15,784 ✔
5384
                  || req->virTableInfoReq.uids == NULL
×
5385
                  || taosArrayGetSize(req->virTableInfoReq.uids) == 0;
7,892 ✔
5386

5387
  // When the trigger only references the TS column, req->cids carries just the
5388
  // primary-key timestamp, but the response must still describe every column
5389
  // ref of each vtable. Pass cids=NULL into the chain-resolver / formatter to
5390
  // request "all columns of the vtable, per-uid".
5391
  if (atomic_load_8(&sStreamReaderInfo->isVtableOnlyTs) == 1) {
7,892 ✔
5392
    cids = NULL;
1,841 ✔
5393
  }
5394

5395
  // Chain-resolver path.
5396
  // NOTE: streamResolveVTableRefChain fires cross-vnode RPCs (TDMT_VND_VTABLE_REF_RESOLVE).
5397
  // Holding META_READER_LOCK across those RPCs risks deadlock if the remote vnode
5398
  // needs to acquire the same lock on this vnode during its own meta read. Delay
5399
  // initReader until after the resolve round-trip completes.
5400
  STREAM_CHECK_RET_GOTO(streamCollectTagCidsFromPartitionCols(sStreamReaderInfo->partitionCols, &tagCids));
7,892 ✔
5401

5402
  SArray *uidList = fullScan ? NULL : req->virTableInfoReq.uids;
7,892 ✔
5403
  STREAM_CHECK_RET_GOTO(streamResolveVTableRefChain(pVnode, sStreamReaderInfo->vtbCache, sStreamReaderInfo,
7,892 ✔
5404
                                                    req->virTableInfoReq.ver, uidList, cids, tagCids, &uid2Result));
×
5405

5406
  // Acquire meta reader lock only after the cross-vnode RPC round-trip is done.
5407
  sStreamReaderInfo->storageApi.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &sStreamReaderInfo->storageApi.metaFn, 0);
7,892 ✔
5408

5409
  // Encode response: iterate the resolved set.
5410
  int32_t expected = (int32_t)tSimpleHashGetSize(uid2Result);
7,892 ✔
5411
  vTableInfo.infos = taosArrayInit(expected, sizeof(VTableInfo));
7,892 ✔
5412
  STREAM_CHECK_NULL_GOTO(vTableInfo.infos, terrno);
7,892 ✔
5413

5414
  if (fullScan) {
7,892 ✔
5415
    STREAM_CHECK_RET_GOTO(getAllVinfo(pVnode, &vTableInfo, uid2Result, cids, req->virTableInfoReq.ver, &metaReader, sStreamReaderInfo));
7,892 ✔
5416
  } else {
5417
    STREAM_CHECK_RET_GOTO(getSpicificVinfo(pVnode, &vTableInfo, uid2Result, req->virTableInfoReq.uids, cids, req->virTableInfoReq.ver, &metaReader, sStreamReaderInfo));
×
5418
  }
5419

5420
  ST_TASK_DLOG("vgId:%d %s end, size:%"PRIzu, TD_VID(pVnode), __func__, taosArrayGetSize(vTableInfo.infos));
7,892 ✔
5421
  STREAM_CHECK_RET_GOTO(buildVTableInfoRsp(&vTableInfo, &buf, &size));
7,892 ✔
5422

5423
  int32_t rc = streamCacheCommitResolved(sStreamReaderInfo->vtbCache, fullScan, cids, tagCids, &uid2Result);
7,892 ✔
5424
  if (rc != 0) { code = rc; goto end; }
7,892 ✔
5425

5426
end:
7,892 ✔
5427
  if (tagCids != NULL) taosArrayDestroy(tagCids);
7,892 ✔
5428
  tSimpleHashCleanup(uid2Result);
7,892 ✔
5429
  tDestroySStreamMsgVTableInfo(&vTableInfo);
7,892 ✔
5430
  sStreamReaderInfo->storageApi.metaReaderFn.clearReader(&metaReader);
7,892 ✔
5431
  STREAM_PRINT_LOG_END_WITHID(code, lino);
7,892 ✔
5432
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
7,892 ✔
5433
  return code;
7,892 ✔
5434
}
5435

5436
static int32_t vnodeProcessStreamOTableInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
9,604 ✔
5437
                                               SStreamTriggerReaderInfo*   sStreamReaderInfo,
5438
                                               SStreamReaderResponseStats* pRspStats) {
5439
  int32_t                   code = 0;
9,604 ✔
5440
  int32_t                   lino = 0;
9,604 ✔
5441
  void*                     buf = NULL;
9,604 ✔
5442
  size_t                    size = 0;
9,604 ✔
5443
  SSTriggerOrigTableInfoRsp oTableInfo = {0};
9,604 ✔
5444
  SMetaReader               metaReader = {0};
9,604 ✔
5445
  void*                     pTask = sStreamReaderInfo->pTask;
9,604 ✔
5446

5447
  ST_TASK_DLOG("vgId:%d %s start, ver:%" PRId64, TD_VID(pVnode), __func__, req->origTableInfoReq.ver);
9,604 ✔
5448

5449
  SArray* cols = req->origTableInfoReq.cols;
9,604 ✔
5450
  STREAM_CHECK_NULL_GOTO(cols, terrno);
9,604 ✔
5451

5452
  oTableInfo.cols = taosArrayInit(taosArrayGetSize(cols), sizeof(OTableInfoRsp));
9,604 ✔
5453

5454
  STREAM_CHECK_NULL_GOTO(oTableInfo.cols, terrno);
9,604 ✔
5455

5456
  sStreamReaderInfo->storageApi.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &sStreamReaderInfo->storageApi.metaFn, 0);
9,604 ✔
5457
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
21,974 ✔
5458
    OTableInfo*    oInfo = taosArrayGet(cols, i);
12,304 ✔
5459
    OTableInfoRsp* vTableInfo = taosArrayReserve(oTableInfo.cols, 1);
12,304 ✔
5460
    STREAM_CHECK_NULL_GOTO(oInfo, terrno);
12,370 ✔
5461
    STREAM_CHECK_NULL_GOTO(vTableInfo, terrno);
12,370 ✔
5462
    code = sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionName(&metaReader, req->origTableInfoReq.ver, oInfo->refTableName);
12,370 ✔
5463
    if (code != 0) {
12,370 ✔
5464
      code = 0;
×
5465
      ST_TASK_ELOG("vgId:%d %s get table entry by name:%s failed, msg:%s", TD_VID(pVnode), __func__, oInfo->refTableName, tstrerror(code));
×
5466
      continue;
×
5467
    }
5468
    vTableInfo->uid = metaReader.me.uid;
12,370 ✔
5469
    ST_TASK_DLOG("vgId:%d %s get original uid:%"PRId64, TD_VID(pVnode), __func__, vTableInfo->uid);
12,370 ✔
5470

5471
    SSchemaWrapper* sSchemaWrapper = NULL;
12,370 ✔
5472
    if (metaReader.me.type == TD_CHILD_TABLE) {
12,370 ✔
5473
      int64_t suid = metaReader.me.ctbEntry.suid;
9,875 ✔
5474
      vTableInfo->suid = suid;
9,875 ✔
5475
      tDecoderClear(&metaReader.coder);
9,875 ✔
5476
      STREAM_CHECK_RET_GOTO(sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(&metaReader, req->origTableInfoReq.ver, suid));
9,875 ✔
5477
      sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
9,809 ✔
5478
    } else if (metaReader.me.type == TD_NORMAL_TABLE) {
2,495 ✔
5479
      vTableInfo->suid = 0;
2,495 ✔
5480
      sSchemaWrapper = &metaReader.me.ntbEntry.schemaRow;
2,495 ✔
5481
    } else {
5482
      ST_TASK_ELOG("invalid table type:%d", metaReader.me.type);
×
5483
    }
5484

5485
    for (size_t j = 0; j < sSchemaWrapper->nCols; j++) {
39,263 ✔
5486
      SSchema* s = sSchemaWrapper->pSchema + j;
39,329 ✔
5487
      if (strcmp(s->name, oInfo->refColName) == 0) {
39,329 ✔
5488
        vTableInfo->cid = s->colId;
12,370 ✔
5489
        break;
12,370 ✔
5490
      }
5491
    }
5492
    if (vTableInfo->cid == 0) {
12,304 ✔
5493
      stError("vgId:%d %s, not found col %s in table %s", TD_VID(pVnode), __func__, oInfo->refColName,
×
5494
              oInfo->refTableName);
5495
    }
5496
    tDecoderClear(&metaReader.coder);
12,304 ✔
5497
  }
5498

5499
  STREAM_CHECK_RET_GOTO(buildOTableInfoRsp(&oTableInfo, &buf, &size));
9,604 ✔
5500

5501
end:
9,604 ✔
5502
  tDestroySTriggerOrigTableInfoRsp(&oTableInfo);
9,604 ✔
5503
  sStreamReaderInfo->storageApi.metaReaderFn.clearReader(&metaReader);
9,604 ✔
5504
  STREAM_PRINT_LOG_END_WITHID(code, lino);
9,604 ✔
5505
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
9,665 ✔
5506
  return code;
9,604 ✔
5507
}
5508

5509
static int32_t vnodeProcessStreamVTableTagInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
22,718 ✔
5510
                                                  SStreamTriggerReaderInfo*   sStreamReaderInfo,
5511
                                                  SStreamReaderResponseStats* pRspStats) {
5512
  int32_t                   code = 0;
22,718 ✔
5513
  int32_t                   lino = 0;
22,718 ✔
5514
  void*                     buf = NULL;
22,718 ✔
5515
  size_t                    size = 0;
22,718 ✔
5516
  SSDataBlock              *pBlock      = NULL;
22,718 ✔
5517
  SArray                   *singleUid   = NULL;
22,718 ✔
5518
  SArray                   *emptyCols   = NULL;
22,718 ✔
5519
  SArray                   *tagOnly     = NULL;
22,718 ✔
5520
  SSHashObj                *uid2Result  = NULL;
22,718 ✔
5521
  SVTableResolveResult     *pRes        = NULL;
22,718 ✔
5522
  SMetaReader               metaReader  = {0};
22,718 ✔
5523
  bool                      metaReaderInited = false;
22,718 ✔
5524
  int8_t                    vtableType = 0;
22,718 ✔
5525
  char                      vtableName[TSDB_TABLE_NAME_LEN] = {0};
22,718 ✔
5526
  int64_t streamId = req->base.streamId;
22,718 ✔
5527
  stsDebug("vgId:%d %s start, ver:%"PRId64" uid:%"PRId64" cols_size:%d", TD_VID(pVnode), __func__,
22,718 ✔
5528
           req->virTablePseudoColReq.ver, req->virTablePseudoColReq.uid,
5529
           req->virTablePseudoColReq.cids ? (int)taosArrayGetSize(req->virTablePseudoColReq.cids) : -1);
5530

5531
  SArray* cols = req->virTablePseudoColReq.cids;
22,718 ✔
5532
  STREAM_CHECK_NULL_GOTO(cols, terrno);
22,718 ✔
5533

5534
  // We still need metaReader for vtable name (cid == -1) and to assert table type.
5535
  sStreamReaderInfo->storageApi.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &sStreamReaderInfo->storageApi.metaFn, 0);
22,718 ✔
5536
  metaReaderInited = true;
22,718 ✔
5537
  STREAM_CHECK_RET_GOTO(sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(&metaReader, req->virTablePseudoColReq.ver, req->virTablePseudoColReq.uid));
22,718 ✔
5538
  STREAM_CHECK_CONDITION_GOTO(metaReader.me.type != TD_VIRTUAL_CHILD_TABLE && metaReader.me.type != TD_VIRTUAL_NORMAL_TABLE, TSDB_CODE_INVALID_PARA);
22,718 ✔
5539
  vtableType = metaReader.me.type;
22,718 ✔
5540
  tstrncpy(vtableName, metaReader.me.name, sizeof(vtableName));
22,718 ✔
5541
  sStreamReaderInfo->storageApi.metaReaderFn.clearReader(&metaReader);
22,718 ✔
5542
  metaReaderInited = false;
22,718 ✔
5543

5544
  STREAM_CHECK_RET_GOTO(createDataBlock(&pBlock));
22,718 ✔
5545

5546
  if (vtableType == TD_VIRTUAL_NORMAL_TABLE) {
22,718 ✔
5547
    // Normal vtable: caller only requests the table-name pseudo column.
5548
    STREAM_CHECK_CONDITION_GOTO (taosArrayGetSize(cols) < 1 || *(col_id_t*)taosArrayGet(cols, 0) != -1, TSDB_CODE_INVALID_PARA);
660 ✔
5549
    SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN, -1);
660 ✔
5550
    STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
660 ✔
5551
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, 1));
660 ✔
5552
    pBlock->info.rows = 1;
660 ✔
5553
    SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, 0);
660 ✔
5554
    STREAM_CHECK_NULL_GOTO(pDst, terrno);
660 ✔
5555
    STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, vtableName, strlen(vtableName), false));
660 ✔
5556
  } else {
5557
    // Virtual child table: resolve tags via chain resolver (single uid, cache-bypass).
5558
    singleUid = taosArrayInit(1, sizeof(int64_t));
22,058 ✔
5559
    STREAM_CHECK_NULL_GOTO(singleUid, terrno);
22,058 ✔
5560
    int64_t uidVal = req->virTablePseudoColReq.uid;
22,058 ✔
5561
    STREAM_CHECK_NULL_GOTO(taosArrayPush(singleUid, &uidVal), terrno);
21,992 ✔
5562

5563
    emptyCols = taosArrayInit(0, sizeof(col_id_t));
21,992 ✔
5564
    STREAM_CHECK_NULL_GOTO(emptyCols, terrno);
22,058 ✔
5565

5566
    // The pseudo-column request can carry cid==-1 for the vtable name; the
5567
    // chain resolver only handles real tag cids, so pass a filtered copy.
5568
    tagOnly = taosArrayInit(taosArrayGetSize(cols), sizeof(col_id_t));
22,058 ✔
5569
    STREAM_CHECK_NULL_GOTO(tagOnly, terrno);
22,058 ✔
5570
    for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
58,663 ✔
5571
      col_id_t cid = *(col_id_t *)taosArrayGet(cols, i);
36,671 ✔
5572
      if (cid == -1) continue;
36,605 ✔
5573
      STREAM_CHECK_NULL_GOTO(taosArrayPush(tagOnly, &cid), terrno);
14,613 ✔
5574
    }
5575

5576
    // PSEUDO_COL bypasses cache — pass NULL so resolver does not read/write it.
5577
    code = streamResolveVTableRefChain(pVnode, NULL, sStreamReaderInfo,
21,992 ✔
5578
                                       req->virTablePseudoColReq.ver,
5579
                                       singleUid, emptyCols, tagOnly, &uid2Result);
5580
    taosArrayDestroy(tagOnly); tagOnly = NULL;
22,058 ✔
5581
    STREAM_CHECK_RET_GOTO(code);
22,058 ✔
5582

5583
    SVTableResolveResult **pp = (SVTableResolveResult **)tSimpleHashGet(uid2Result, &uidVal, sizeof(uidVal));
22,058 ✔
5584
    if (pp == NULL || *pp == NULL) {
22,058 ✔
5585
      // H2 v0.5: A returned 0 but the uid has no entry in uid2Result. This
5586
      // happens only when the top-level uid was missing from local meta
5587
      // (vtable dropped concurrently). PSEUDO_COL is single-uid and has no
5588
      // partial-success semantic, so propagate as REF_TABLE_NOT_EXIST.
5589
      code = TSDB_CODE_STREAM_VTB_REF_TABLE_NOT_EXIST;
×
5590
      goto end;
×
5591
    }
5592
    pRes = *pp;
22,058 ✔
5593

5594
    // Append column metadata according to caller-requested cids.
5595
    for (size_t i = 0; i < taosArrayGetSize(cols); i++){
58,729 ✔
5596
      col_id_t cid = *(col_id_t*)taosArrayGet(cols, i);
36,671 ✔
5597
      if (cid == -1) {
36,671 ✔
5598
        SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN, -1);
22,058 ✔
5599
        STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
22,058 ✔
5600
        continue;
22,058 ✔
5601
      }
5602
      STagValue **ppv = (pRes->tagMap == NULL) ? NULL : (STagValue **)tSimpleHashGet(pRes->tagMap, &cid, sizeof(cid));
14,613 ✔
5603
      if (ppv == NULL || *ppv == NULL) {
14,613 ✔
5604
        SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_NULL, CHAR_BYTES, cid);
×
5605
        STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
×
5606
        continue;
×
5607
      }
5608
      STagValue       *v     = *ppv;
14,613 ✔
5609
      int32_t          bytes = (v->nLen > 0 ? v->nLen : 1);
14,613 ✔
5610
      SColumnInfoData  idata = createColumnInfoData(v->type, bytes, cid);
14,613 ✔
5611
      STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
14,613 ✔
5612
    }
5613

5614
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, 1));
22,058 ✔
5615
    pBlock->info.rows = 1;
22,058 ✔
5616

5617
    for (size_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++){
58,729 ✔
5618
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
36,671 ✔
5619
      STREAM_CHECK_NULL_GOTO(pDst, terrno);
36,671 ✔
5620

5621
      if (pDst->info.colId == -1) {
36,671 ✔
5622
        STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, vtableName, strlen(vtableName), false));
22,058 ✔
5623
        continue;
22,058 ✔
5624
      }
5625
      if (pDst->info.type == TSDB_DATA_TYPE_NULL) {
14,613 ✔
5626
        STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, NULL, true));
×
5627
        continue;
×
5628
      }
5629

5630
      STagValue **ppv = (STagValue **)tSimpleHashGet(pRes->tagMap, &pDst->info.colId, sizeof(pDst->info.colId));
14,613 ✔
5631
      if (ppv == NULL || *ppv == NULL || (*ppv)->pData == NULL) {
14,613 ✔
5632
        STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, NULL, true));
×
5633
        continue;
×
5634
      }
5635
      if (IS_VAR_DATA_TYPE(pDst->info.type)) {
14,613 ✔
5636
        STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, (*ppv)->pData, (*ppv)->nLen, false));
243 ✔
5637
      } else {
5638
        STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, (*ppv)->pData, false));
14,370 ✔
5639
      }
5640
    }
5641
  }
5642

5643
  stsDebug("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlock->info.rows);
22,652 ✔
5644
  printDataBlock(pBlock, __func__, "", streamId);
22,652 ✔
5645
  STREAM_CHECK_RET_GOTO(buildRsp(pBlock, &buf, &size));
22,652 ✔
5646

5647
end:
22,718 ✔
5648
  if(size == 0){
22,718 ✔
5649
    code = TSDB_CODE_STREAM_NO_DATA;
×
5650
  }
5651
  if (singleUid != NULL) taosArrayDestroy(singleUid);
22,718 ✔
5652
  if (emptyCols != NULL) taosArrayDestroy(emptyCols);
22,718 ✔
5653
  if (tagOnly   != NULL) taosArrayDestroy(tagOnly);
22,718 ✔
5654
  tSimpleHashCleanup(uid2Result);
22,718 ✔
5655
  if (metaReaderInited) {
22,718 ✔
5656
    sStreamReaderInfo->storageApi.metaReaderFn.clearReader(&metaReader);
×
5657
  }
5658
  STREAM_PRINT_LOG_END(code, lino);
22,718 ✔
5659
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
22,718 ✔
5660
  blockDataDestroy(pBlock);
22,718 ✔
5661
  return code;
22,718 ✔
5662
}
5663

5664
static int32_t vnodeProcessStreamFetchMsg(SVnode* pVnode, SRpcMsg* pMsg, SQueueInfo* pInfo,
1,119,123 ✔
5665
                                          SStreamReaderResponseStats* pRspStats) {
5666
  int32_t            code = 0;
1,119,123 ✔
5667
  int32_t            lino = 0;
1,119,123 ✔
5668
  void*              buf = NULL;
1,119,123 ✔
5669
  size_t             size = 0;
1,119,123 ✔
5670
  void*              taskAddr = NULL;
1,119,123 ✔
5671
  void*              pTask = NULL;
1,119,123 ✔
5672
  SArray*            pResList = NULL;
1,119,123 ✔
5673
  bool               hasNext = false;
1,119,123 ✔
5674
  SStreamTriggerReaderCalcInfo* sStreamReaderCalcInfo = NULL;
1,119,123 ✔
5675
  SStreamReaderTask*            pReaderTask = NULL;
1,119,123 ✔
5676
  SArray*                       calcInfoList = NULL;
1,119,123 ✔
5677

5678
  SResFetchReq req = {0};
1,119,123 ✔
5679
  STREAM_CHECK_CONDITION_GOTO(tDeserializeSResFetchReq(pMsg->pCont, pMsg->contLen, &req) < 0,
1,119,123 ✔
5680
                              TSDB_CODE_QRY_INVALID_INPUT);
202 ✔
5681
  calcInfoList = (SArray*)qStreamGetReaderInfo(req.queryId, req.taskId, &taskAddr);
1,119,123 ✔
5682
  STREAM_CHECK_NULL_GOTO(calcInfoList, terrno);
1,119,123 ✔
5683

5684
  STREAM_CHECK_CONDITION_GOTO(req.execId < 0, TSDB_CODE_INVALID_PARA);
1,119,123 ✔
5685
  sStreamReaderCalcInfo = taosArrayGetP(calcInfoList, req.execId);
1,119,123 ✔
5686
  STREAM_CHECK_NULL_GOTO(sStreamReaderCalcInfo, terrno);
1,119,123 ✔
5687
  STREAM_CHECK_RET_GOTO(tAdmitStreamContext(req.pStRtFuncInfo == NULL ? NULL : req.pStRtFuncInfo->pContextPolicy,
1,119,123 ✔
5688
                                            req.pStRtFuncInfo == NULL ? NULL : req.pStRtFuncInfo->pAncestorContext,
5689
                                            sStreamReaderCalcInfo->requiresContextPolicy));
200 ✔
5690
  sStreamReaderCalcInfo->rtInfo.execId = req.execId;
1,119,121 ✔
5691

5692
  pReaderTask = (SStreamReaderTask*)sStreamReaderCalcInfo->pTask;
1,119,121 ✔
5693
  pTask = pReaderTask;
1,119,121 ✔
5694
  ST_TASK_DLOG("vgId:%d %s start, execId:%d, reset:%d, pTaskInfo:%p, scan type:%d", TD_VID(pVnode), __func__, req.execId, req.reset,
1,119,121 ✔
5695
               sStreamReaderCalcInfo->pTaskInfo, nodeType(sStreamReaderCalcInfo->calcAst->pNode));
5696

5697
  if (req.reset) {
1,119,121 ✔
5698
    int64_t uid = 0;
1,038,046 ✔
5699
    if (req.dynTbname && !req.pStRtFuncInfo->isMultiGroupCalc) {
1,038,046 ✔
5700
      SArray* vals = req.pStRtFuncInfo->pStreamPartColVals;
154,945 ✔
5701
      for (int32_t i = 0; i < taosArrayGetSize(vals); ++i) {
154,945 ✔
5702
        SStreamGroupValue* pValue = taosArrayGet(vals, i);
154,945 ✔
5703
        if (pValue != NULL && pValue->isTbname) {
154,945 ✔
5704
          uid = pValue->uid;
154,945 ✔
5705
          break;
154,945 ✔
5706
        }
5707
      }
5708
    }
5709
    
5710
    SReadHandle handle = {0};
1,038,046 ✔
5711
    handle.vnode = pVnode;
1,038,046 ✔
5712
    handle.pMsgCb = &pVnode->msgCb;
1,038,046 ✔
5713
    handle.pWorkerCb = pInfo->workerCb;
1,038,046 ✔
5714
    handle.uid = uid;
1,038,046 ✔
5715
    handle.cacheSttStatis = true;
1,038,046 ✔
5716

5717
    initStorageAPI(&handle.api);
1,038,046 ✔
5718
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(sStreamReaderCalcInfo->calcAst->pNode) ||
1,038,046 ✔
5719
      QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(sStreamReaderCalcInfo->calcAst->pNode)){
629,153 ✔
5720
      STimeRangeNode* node = (STimeRangeNode*)((STableScanPhysiNode*)(sStreamReaderCalcInfo->calcAst->pNode))->pTimeRange;
935,166 ✔
5721
      if (node != NULL) {
935,166 ✔
5722
        STREAM_CHECK_RET_GOTO(processCalaTimeRange(sStreamReaderCalcInfo, &req, node, &handle, false, TD_VID(pVnode)));
113,751 ✔
5723
      } else {
5724
        ST_TASK_DLOG("vgId:%d %s no scan time range node", TD_VID(pVnode), __func__);
821,415 ✔
5725
      }
5726

5727
      node = (STimeRangeNode*)((STableScanPhysiNode*)(sStreamReaderCalcInfo->calcAst->pNode))->pExtTimeRange;
935,085 ✔
5728
      if (node != NULL) {
935,085 ✔
5729
        STREAM_CHECK_RET_GOTO(processCalaTimeRange(sStreamReaderCalcInfo, &req, node, &handle, true, TD_VID(pVnode)));
10,472 ✔
5730
      } else {
5731
        ST_TASK_DLOG("vgId:%d %s no interp time range node", TD_VID(pVnode), __func__);
924,613 ✔
5732
      }      
5733
    }
5734

5735
    TSWAP(sStreamReaderCalcInfo->rtInfo.funcInfo, *req.pStRtFuncInfo);
1,038,046 ✔
5736
    sStreamReaderCalcInfo->rtInfo.funcInfo.hasPlaceHolder = sStreamReaderCalcInfo->hasPlaceHolder;
1,038,046 ✔
5737
    handle.streamRtInfo = &sStreamReaderCalcInfo->rtInfo;
1,038,046 ✔
5738

5739
    if (sStreamReaderCalcInfo->pTaskInfo == NULL || !qNeedReset(sStreamReaderCalcInfo->pTaskInfo)) {
1,038,046 ✔
5740
      qDestroyTask(sStreamReaderCalcInfo->pTaskInfo);
72,829 ✔
5741
      STREAM_CHECK_RET_GOTO(qCreateStreamExecTaskInfo(&sStreamReaderCalcInfo->pTaskInfo,
72,829 ✔
5742
                                                    sStreamReaderCalcInfo->calcScanPlan, &handle, NULL, TD_VID(pVnode),
5743
                                                    req.taskId));
65 ✔
5744
      STREAM_CHECK_RET_GOTO(qSetTaskId(sStreamReaderCalcInfo->pTaskInfo, req.taskId, req.queryId));
72,829 ✔
5745
    } else {
5746
      // Must set scalar extra BEFORE reset: qResetTableScan rebuilds the table
5747
      // list which evaluates TagCond (RemoteValueList scalar subquery).  The
5748
      // thread-local gTaskScalarExtra.pSubJobCtx must point to THIS task's
5749
      // context so that sendFetchRemoteNodeReq uses the correct execId for
5750
      // routing to the right non-topTask exec slot.
5751
      qSetStreamGen(sStreamReaderCalcInfo->pTaskInfo, sStreamReaderCalcInfo->rtInfo.funcInfo.streamGen);
965,217 ✔
5752
      setTaskScalarExtraInfo(sStreamReaderCalcInfo->pTaskInfo);
965,217 ✔
5753
      STREAM_CHECK_RET_GOTO(qResetTableScan(sStreamReaderCalcInfo->pTaskInfo, &handle));
965,140 ✔
5754
    }
5755

5756
    STREAM_CHECK_RET_GOTO(qSetTaskId(sStreamReaderCalcInfo->pTaskInfo, req.taskId, req.queryId));
1,037,965 ✔
5757
  }
5758

5759
  if (req.pOpParam != NULL) {
1,119,121 ✔
5760
    qUpdateOperatorParam(sStreamReaderCalcInfo->pTaskInfo, (void*)req.pOpParam);
169,086 ✔
5761
  }
5762

5763
  qSetStreamGen(sStreamReaderCalcInfo->pTaskInfo, sStreamReaderCalcInfo->rtInfo.funcInfo.streamGen);
1,119,121 ✔
5764

5765
  pResList = taosArrayInit(4, POINTER_BYTES);
1,119,002 ✔
5766
  STREAM_CHECK_NULL_GOTO(pResList, terrno);
1,118,968 ✔
5767
  uint64_t ts = 0;
1,118,968 ✔
5768
  // Stream fetches enter via vnode worker threads that may not have
5769
  // initialized the thread_local gTaskScalarExtra for this task.  Without
5770
  // this, scalar subqueries embedded in the operator (e.g. WHERE ts >=
5771
  // (SELECT last_row(ts) FROM ref)) fail with "no subJob ctx" on whichever
5772
  // event happens to land on a fresh worker thread.
5773
  setTaskScalarExtraInfo(sStreamReaderCalcInfo->pTaskInfo);
1,118,968 ✔
5774
  STREAM_CHECK_RET_GOTO(qExecTaskOpt(sStreamReaderCalcInfo->pTaskInfo, pResList, &ts, &hasNext, NULL, req.pOpParam != NULL));
1,119,121 ✔
5775

5776
  for(size_t i = 0; i < taosArrayGetSize(pResList); i++){
3,016,774 ✔
5777
    SSDataBlock* pBlock = taosArrayGetP(pResList, i);
1,897,983 ✔
5778
    if (pBlock == NULL) continue;
1,897,983 ✔
5779
    printDataBlock(pBlock, __func__, "fetch", ((SStreamTask*)pTask)->streamId);
1,897,983 ✔
5780
/*    
5781
    if (sStreamReaderCalcInfo->rtInfo.funcInfo.withExternalWindow) {
5782
      STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderCalcInfo->pFilterInfo, NULL));
5783
      printDataBlock(pBlock, __func__, "fetch filter");
5784
    }
5785
*/    
5786
  }
5787

5788
end: {
1,119,040 ✔
5789
  const int32_t rspCode = streamBuildFetchRsp(pResList, hasNext, &buf, &size, pVnode->config.tsdbCfg.precision);
1,119,123 ✔
5790
  if (code == TSDB_CODE_SUCCESS) {
1,119,123 ✔
5791
    code = rspCode;
1,118,791 ✔
5792
    if (code == TSDB_CODE_SUCCESS) {
1,118,791 ✔
5793
      addReaderResponseBlocks(pRspStats, pResList);
1,118,791 ✔
5794
    }
5795
  }
5796
}
5797

5798
  if (sStreamReaderCalcInfo && sStreamReaderCalcInfo->rtInfo.funcInfo.isMultiGroupCalc) {
1,119,123 ✔
5799
    sStreamReaderCalcInfo->rtInfo.funcInfo.pStreamPesudoFuncVals = NULL;
×
5800
    sStreamReaderCalcInfo->rtInfo.funcInfo.pStreamPartColVals = NULL;
×
5801
  }
5802

5803
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_TDB_TABLE_NOT_EXIST){
1,119,123 ✔
5804
    code = TDB_CODE_SUCCESS;
×
5805
  }
5806
  if (pReaderTask != NULL) {
1,119,123 ✔
5807
    pRspStats->activeScanContexts = taosArrayGetSize(calcInfoList);
1,119,121 ✔
5808
    pRspStats->activeScanContextsValid = true;
1,119,121 ✔
5809
    int64_t nowMonoUs = streamTaskGetMonotonicUs();
1,119,121 ✔
5810
    stReaderTaskRecordPullResult(pReaderTask, pRspStats, code, nowMonoUs, taosGetTimestampMs());
1,119,121 ✔
5811
  }
5812
  taosArrayDestroy(pResList);
1,119,123 ✔
5813
  streamReleaseTask(taskAddr);
1,119,123 ✔
5814

5815
  STREAM_PRINT_LOG_END(code, lino);
1,119,054 ✔
5816
  SRpcMsg rsp = {.msgType = TDMT_STREAM_FETCH_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
1,119,110 ✔
5817
  tmsgSendRsp(&rsp);
1,119,123 ✔
5818
  tDestroySResFetchReq(&req);
1,119,042 ✔
5819
  if (TDB_CODE_SUCCESS != code) {
1,119,123 ✔
5820
    if (pTask != NULL) {
332 ✔
5821
      ST_TASK_ELOG("vgId:%d %s failed, code:%d - %s", TD_VID(pVnode), __func__, code, tstrerror(code));
330 ✔
5822
    } else {
5823
      stError("vgId:%d %s failed, code:%d - %s", TD_VID(pVnode), __func__, code, tstrerror(code));
2 ✔
5824
    }
5825
  }
5826
  return code;
1,119,123 ✔
5827
}
5828

5829
static int32_t initTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, SVnode* pVnode) {
5,092,403 ✔
5830
  int32_t code = 0;
5,092,403 ✔
5831
  if (sStreamReaderInfo->tableList.pTableList != NULL) {  
5,092,403 ✔
5832
    return code;
5,021,789 ✔
5833
  }
5834
  taosWLockLatch(&sStreamReaderInfo->lock);
69,350 ✔
5835
  sStreamReaderInfo->pVnode = pVnode;
69,852 ✔
5836
  initStorageAPI(&sStreamReaderInfo->storageApi);
69,852 ✔
5837
  if (sStreamReaderInfo->tableList.pTableList == NULL) {
69,787 ✔
5838
    code = initStreamTableListInfo(&sStreamReaderInfo->tableList,
69,861 ✔
5839
                                   sStreamReaderInfo->isRollupReader ? UIDMAP_MULTI : UIDMAP_SINGLE);
69,787 ✔
5840
    if (code == 0) {
69,852 ✔
5841
      code = generateTablistForStreamReader(pVnode, sStreamReaderInfo);
69,852 ✔
5842
      if (code != 0) {
69,852 ✔
5843
        qStreamDestroyTableInfo(&sStreamReaderInfo->tableList);
316 ✔
5844
      } else {
5845
        sStreamReaderInfo->tableList.version = pVnode->state.applied;
69,536 ✔
5846
        stDebug("vgId:%d %s init table list for stream reader, table num:%zu, version:%" PRId64,
69,536 ✔
5847
                TD_VID(pVnode), __func__, taosArrayGetSize(sStreamReaderInfo->tableList.pTableList), sStreamReaderInfo->tableList.version);
5848
      }
5849
    }
5850
  }
5851
  taosWUnLockLatch(&sStreamReaderInfo->lock);
69,852 ✔
5852
  return code;
69,852 ✔
5853
}
5854

5855
int32_t vnodeProcessStreamReaderMsg(SVnode* pVnode, SRpcMsg* pMsg, SQueueInfo *pInfo) {
6,250,657 ✔
5856
  int32_t                   code = 0;
6,250,657 ✔
5857
  int32_t                   lino = 0;
6,250,657 ✔
5858
  SSTriggerPullRequestUnion req = {0};
6,250,657 ✔
5859
  void*                     taskAddr = NULL;
6,250,944 ✔
5860
  SStreamTriggerReaderInfo* sStreamReaderInfo = NULL;
6,250,013 ✔
5861
  bool                      sendRsp = false;
6,250,013 ✔
5862
  SStreamReaderResponseStats rspStats = {.requestStartMonoUs = streamTaskGetMonotonicUs()};
6,250,013 ✔
5863

5864
  vDebug("vgId:%d, msg:%p in stream reader queue is processing", pVnode->config.vgId, pMsg);
6,252,815 ✔
5865
  if (!syncIsReadyForRead(pVnode->sync)) {
6,253,998 ✔
5866
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
37,599 ✔
5867
    return 0;
37,584 ✔
5868
  }
5869

5870
  if (pMsg->msgType == TDMT_STREAM_FETCH) {
6,215,206 ✔
5871
    return vnodeProcessStreamFetchMsg(pVnode, pMsg, pInfo, &rspStats);
1,119,123 ✔
5872
  } else if (pMsg->msgType == TDMT_STREAM_TRIGGER_PULL) {
5,096,083 ✔
5873
    void*   pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
5,096,350 ✔
5874
    int32_t len = pMsg->contLen - sizeof(SMsgHead);
5,096,350 ✔
5875
    STREAM_CHECK_RET_GOTO(tDeserializeSTriggerPullRequest(pReq, len, &req));
5,096,350 ✔
5876
    stDebug("vgId:%d %s start, type:%d, streamId:%" PRIx64 ", readerTaskId:%" PRIx64 ", sessionId:%" PRIx64 ", applied:%" PRIx64,
5,095,606 ✔
5877
            TD_VID(pVnode), __func__, req.base.type, req.base.streamId, req.base.readerTaskId, req.base.sessionId, pVnode->state.applied);
5878
    sStreamReaderInfo = qStreamGetReaderInfo(req.base.streamId, req.base.readerTaskId, &taskAddr);
5,095,798 ✔
5879
    STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
5,095,909 ✔
5880
    STREAM_CHECK_RET_GOTO(initTableList(sStreamReaderInfo, pVnode));
5,092,706 ✔
5881
    sendRsp = true;
5,092,589 ✔
5882
    switch (req.base.type) {
5,092,589 ✔
5883
      case STRIGGER_PULL_SET_TABLE:
9,604 ✔
5884
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamSetTableReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
9,604 ✔
5885
        break;
9,604 ✔
5886
      case STRIGGER_PULL_LAST_TS:
65,974 ✔
5887
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamLastTsReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
65,974 ✔
5888
        break;
65,935 ✔
5889
      case STRIGGER_PULL_FIRST_TS:
44,380 ✔
5890
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamFirstTsReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
44,380 ✔
5891
        break;
44,446 ✔
5892
      case STRIGGER_PULL_TSDB_META:
29,978 ✔
5893
      case STRIGGER_PULL_TSDB_META_NEXT:
5894
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbMetaReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
29,978 ✔
5895
        break;
29,978 ✔
5896
      case STRIGGER_PULL_TSDB_TS_DATA:
4,313 ✔
5897
        if (sStreamReaderInfo->isVtableStream) {
4,313 ✔
5898
          STREAM_CHECK_RET_GOTO(
×
5899
              vnodeProcessStreamTsdbTsDataReqVTable(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
5900
        } else {
5901
          STREAM_CHECK_RET_GOTO(
4,313 ✔
5902
              vnodeProcessStreamTsdbTsDataReqNonVTable(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
5903
        }
5904
        break;
4,313 ✔
5905
      case STRIGGER_PULL_TSDB_TRIGGER_DATA:
41,064 ✔
5906
      case STRIGGER_PULL_TSDB_TRIGGER_DATA_NEXT:
5907
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTriggerDataReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
41,064 ✔
5908
        break;
20,466 ✔
5909
      case STRIGGER_PULL_TSDB_CALC_DATA:
2,025,897 ✔
5910
      case STRIGGER_PULL_TSDB_CALC_DATA_NEXT:
5911
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbCalcDataReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
2,025,897 ✔
5912
        break;
2,025,699 ✔
5913
      case STRIGGER_PULL_TSDB_DATA:
12,398 ✔
5914
      case STRIGGER_PULL_TSDB_DATA_NEXT:
5915
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbVirtalDataReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
12,398 ✔
5916
        break;
12,398 ✔
5917
      case STRIGGER_PULL_GROUP_COL_VALUE:
64,237 ✔
5918
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamGroupColValueReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
64,237 ✔
5919
        break;
64,237 ✔
5920
      case STRIGGER_PULL_VTABLE_INFO:
7,892 ✔
5921
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableInfoReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
7,892 ✔
5922
        break;
7,892 ✔
5923
      case STRIGGER_PULL_VTABLE_PSEUDO_COL:
22,718 ✔
5924
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableTagInfoReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
22,718 ✔
5925
        break;
22,718 ✔
5926
      case STRIGGER_PULL_OTABLE_INFO:
9,604 ✔
5927
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamOTableInfoReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
9,604 ✔
5928
        break;
9,604 ✔
5929
      case STRIGGER_PULL_WAL_META_NEW:
1,095,596 ✔
5930
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaNewReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
1,095,596 ✔
5931
        break;
1,094,994 ✔
5932
      case STRIGGER_PULL_WAL_DATA_NEW:
150,401 ✔
5933
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
150,401 ✔
5934
        break;
150,401 ✔
5935
      case STRIGGER_PULL_WAL_META_DATA_NEW:
1,416,451 ✔
5936
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
1,416,451 ✔
5937
        break;
1,415,274 ✔
5938
      case STRIGGER_PULL_WAL_CALC_DATA_NEW:
91,670 ✔
5939
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalCalcDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
91,670 ✔
5940
        break;
91,670 ✔
5941
      default:
412 ✔
5942
        vError("unknown inner msg type:%d in stream reader queue", req.base.type);
412 ✔
5943
        sendRsp = false;
×
5944
        STREAM_CHECK_RET_GOTO(TSDB_CODE_APP_ERROR);
×
5945
    }
5946
  } else {
5947
    vError("unknown msg type:%d in stream reader queue", pMsg->msgType);
×
5948
    STREAM_CHECK_RET_GOTO(TSDB_CODE_APP_ERROR);
×
5949
  }
5950
end:
5,095,216 ✔
5951
  tDestroySTriggerPullRequest(&req);
5,095,681 ✔
5952
  STREAM_PRINT_LOG_END(code, lino);
5,095,110 ✔
5953
  if (!sendRsp) {
5,094,880 ✔
5954
    if (pMsg->msgType == TDMT_STREAM_TRIGGER_PULL) {
3,519 ✔
5955
      if (code == TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH) {
3,519 ✔
5956
        sendTriggerPullRsp(pMsg, sStreamReaderInfo, NULL, 0, code, &rspStats);
316 ✔
5957
      } else {
5958
        sendTriggerPullRsp(pMsg, sStreamReaderInfo, pMsg->info.rsp, pMsg->info.rspLen, code, &rspStats);
3,203 ✔
5959
      }
5960
    } else {
5961
      SRpcMsg rsp = {
×
5962
          .code = code,
5963
          .pCont = pMsg->info.rsp,
×
5964
          .contLen = pMsg->info.rspLen,
×
5965
          .info = pMsg->info,
5966
      };
5967
      tmsgSendRsp(&rsp);
×
5968
    }
5969
  }
5970
  streamReleaseTask(taskAddr);
5,094,880 ✔
5971
  return code;
5,095,945 ✔
5972
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc