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

taosdata / TDengine / #5124

16 Sep 2026 01:31AM UTC coverage: 72.781% (-0.5%) from 73.303%
#5124

push

travis-ci

jbjia
test(coverage): sync from gitlab

308307 of 423608 relevant lines covered (72.78%)

65361367.37 hits per line

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

84.98
/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) {
38,393 ✔
55
  SNode* pNode = nodesListGetNode(pInfo->pRollupTagCols, 0);
38,393 ✔
56
  return (pNode != NULL && nodeType(pNode) == QUERY_NODE_COLUMN) ? (SColumnNode*)pNode : NULL;
38,393 ✔
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) {
225 ✔
72
  if (pSkipped != NULL) {
225 ✔
73
    *pSkipped = true;
225 ✔
74
  }
75
  return TSDB_CODE_SUCCESS;
225 ✔
76
}
77

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

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

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

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

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

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

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

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

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

119
  int32_t segStart = 0;
9,493 ✔
120
  for (int32_t i = 0; i <= tagLen; ++i) {
59,314 ✔
121
    if (i < tagLen && tagValue[i] != '.') {
51,117 ✔
122
      if (isIllegalSegmentByte(tagValue[i])) {
26,148 ✔
123
        destroyRollupPath(pOut);
75 ✔
124
        return TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH;
75 ✔
125
      }
126
      continue;
26,073 ✔
127
    }
128

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

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

142
    segStart = i + 1;
23,748 ✔
143
  }
144

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

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

154
  *ppPrefixes = NULL;
8,197 ✔
155

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

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

166
  int32_t totalLen = 0;
8,197 ✔
167
  for (int32_t i = 0; i < numOfSegments; ++i) {
30,799 ✔
168
    SRollupSegmentRef* ref = taosArrayGet(p->segments, i);
22,602 ✔
169
    totalLen += ref->len + (i == 0 ? 0 : 1);
22,602 ✔
170

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

177
    int32_t pos = 0;
22,602 ✔
178
    for (int32_t j = 0; j <= i; ++j) {
66,737 ✔
179
      SRollupSegmentRef* part = taosArrayGet(p->segments, j);
44,135 ✔
180
      if (j > 0) {
44,135 ✔
181
        prefix[pos++] = '.';
21,533 ✔
182
      }
183
      memcpy(prefix + pos, p->input + part->off, part->len);
44,135 ✔
184
      pos += part->len;
44,135 ✔
185
    }
186
    prefix[pos] = '\0';
22,602 ✔
187

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

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

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

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

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

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

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

226
  if (keyBuf != stackBuf) {
22,377 ✔
227
    taosMemoryFree(keyBuf);
×
228
  }
229
  return gid;
22,377 ✔
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,
1,521 ✔
258
                                     int32_t pathLen) {
259
  taosMemoryFreeClear(sStreamReaderInfo->extraErrMsg);
1,521 ✔
260

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

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

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

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

286
  sStreamReaderInfo->extraErrMsg = msg;
1,521 ✔
287
}
288

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

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

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

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

311
  SRpcMsg rsp = {
5,318,853 ✔
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,318,932 ✔
314
    pRspStats->activeScanContexts = taosHashGetSize(sStreamReaderInfo->streamTaskMap);
5,315,880 ✔
315
    pRspStats->tableCount = qStreamGetTableListNum(sStreamReaderInfo);
5,316,148 ✔
316
    pRspStats->activeScanContextsValid = true;
5,316,293 ✔
317
    pRspStats->tableCountValid = true;
5,316,293 ✔
318
    int64_t nowMonoUs = streamTaskGetMonotonicUs();
5,316,293 ✔
319
    stReaderTaskRecordPullResult((SStreamReaderTask*)sStreamReaderInfo->pTask, pRspStats, code, nowMonoUs,
5,315,749 ✔
320
                                 taosGetTimestampMs());
321
  }
322
  tmsgSendRsp(&rsp);
5,319,676 ✔
323
}
5,319,597 ✔
324

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

328
  pRspStats->dataRows += (uint64_t)pBlock->info.rows;
3,735,931 ✔
329
  ++pRspStats->dataBlocks;
3,735,931 ✔
330
}
331

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

335
  for (int32_t i = 0; i < taosArrayGetSize(pBlocks); ++i) {
5,320,770 ✔
336
    addReaderResponseBlock(pRspStats, taosArrayGetP(pBlocks, i));
3,369,784 ✔
337
  }
338
}
339

340
static int8_t getWalDataSliceHashType(SStreamTriggerReaderInfo* sStreamReaderInfo) {
2,034,127 ✔
341
  return isRollupMultiReader(sStreamReaderInfo) ? TSDB_DATA_TYPE_BINARY : TSDB_DATA_TYPE_BIGINT;
2,034,127 ✔
342
}
343

344
static void getWalDataSliceKey(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t uid, uint64_t gid,
1,941,586 ✔
345
                               SWalDataSliceKey* pKeyBuf, const void** ppKey, int32_t* pKeyLen) {
346
  if (isRollupMultiReader(sStreamReaderInfo)) {
1,941,586 ✔
347
    *pKeyBuf = (SWalDataSliceKey){.uid = uid, .gid = gid};
82,882 ✔
348
    *ppKey = pKeyBuf;
82,882 ✔
349
    *pKeyLen = sizeof(*pKeyBuf);
82,882 ✔
350
  } else {
351
    pKeyBuf->uid = uid;
1,858,293 ✔
352
    *ppKey = &pKeyBuf->uid;
1,858,293 ✔
353
    *pKeyLen = LONG_BYTES;
1,858,293 ✔
354
  }
355
}
1,940,911 ✔
356

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

361
  if (sStreamReaderInfo->tableType == TD_SUPER_TABLE && suid != sStreamReaderInfo->suid) {
81,512 ✔
362
    return TSDB_CODE_SUCCESS;
74 ✔
363
  }
364

365
  taosRLockLatch(&sStreamReaderInfo->lock);
81,438 ✔
366

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

372
  int32_t numOfElements = taosArrayGetSize(*pElements);
81,438 ✔
373
  for (int32_t i = 0; i < numOfElements; ++i) {
320,808 ✔
374
    SStreamTableMapElement* element = taosArrayGet(*pElements, i);
239,370 ✔
375
    if (element == NULL || element->table == NULL || element->table->markedDeleted) {
239,370 ✔
376
      continue;
×
377
    }
378

379
    SWalDataTarget target = {.gid = element->table->groupId, .window = {.skey = INT64_MIN, .ekey = INT64_MAX}};
239,370 ✔
380
    if (ranges != NULL) {
239,370 ✔
381
      void* timerange = tSimpleHashGet(ranges, &target.gid, LONG_BYTES);
237,130 ✔
382
      if (timerange == NULL) {
237,130 ✔
383
        continue;
156,488 ✔
384
      }
385
      int64_t* pRange = (int64_t*)timerange;
80,642 ✔
386
      target.window.skey = pRange[0];
80,642 ✔
387
      target.window.ekey = pRange[1];
80,642 ✔
388
    }
389

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

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

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

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

411
  if (rsp->uidHash != NULL) {
970,269 ✔
412
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(rsp->uidHash, &uid, LONG_BYTES, &gid, LONG_BYTES));
548,830 ✔
413
    ST_TASK_DLOG("%s put uid into uidHash, uid:%" PRId64 ", gid:%" PRIu64, __func__, uid, gid);
548,898 ✔
414
  }
415

416
  SWalDataSliceKey keyBuf = {0};
970,337 ✔
417
  const void*      pKey = NULL;
970,335 ✔
418
  int32_t          keyLen = 0;
970,852 ✔
419
  getWalDataSliceKey(sStreamReaderInfo, uid, gid, &keyBuf, &pKey, &keyLen);
970,848 ✔
420

421
  SStreamWalDataSlice* pSlice = (SStreamWalDataSlice*)tSimpleHashGet(rsp->indexHash, pKey, keyLen);
970,641 ✔
422
  if (pSlice != NULL) {
971,206 ✔
423
    pSlice->uid = uid;
796,411 ✔
424
    pSlice->gId = gid;
796,411 ✔
425
    pSlice->numRows += numOfRows;
796,411 ✔
426
    ST_TASK_DLOG("%s again uid:%" PRId64 ", gid:%" PRIu64 ", total numOfRows:%d, hash:%p %d, ver:%" PRId64, __func__,
796,411 ✔
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};
174,795 ✔
430
    ST_TASK_DLOG("%s first uid:%" PRId64 ", gid:%" PRIu64 ", numOfRows:%d, hash:%p %d, ver:%" PRId64, __func__, uid,
174,795 ✔
431
                 gid, tmp.numRows, rsp->indexHash, tSimpleHashGetSize(rsp->indexHash), ver);
432
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(rsp->indexHash, pKey, keyLen, &tmp, sizeof(tmp)));
174,795 ✔
433
  }
434

435
  rsp->totalRows += numOfRows;
970,845 ✔
436
  rsp->totalDataRows += numOfRows;
970,845 ✔
437

438
end:
971,281 ✔
439
  return code;
971,211 ✔
440
}
441

442
static SStreamWalDataSlice* getWalDataSlice(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp,
970,739 ✔
443
                                            int64_t uid, uint64_t gid) {
444
  SWalDataSliceKey keyBuf = {0};
970,739 ✔
445
  const void*      pKey = NULL;
970,956 ✔
446
  int32_t          keyLen = 0;
971,135 ✔
447
  getWalDataSliceKey(sStreamReaderInfo, uid, gid, &keyBuf, &pKey, &keyLen);
971,027 ✔
448
  return (SStreamWalDataSlice*)tSimpleHashGet(rsp->indexHash, pKey, keyLen);
970,889 ✔
449
}
450

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

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

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

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

471
  taosRLockLatch(&sStreamReaderInfo->lock);
6,600 ✔
472

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

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

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

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

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

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

515
end:
2,136,575 ✔
516
  return suid;
2,136,575 ✔
517
}
518

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

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

525
  if (*c1 < *c2) {
34,014 ✔
526
    return -1;
33,742 ✔
527
  } else if (*c1 > *c2) {
272 ✔
528
    return 1;
272 ✔
529
  }
530

531
  return 0;
×
532
}
533

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

538
  if (c1->colId < c2->colId) {
34,014 ✔
539
    return -1;
33,742 ✔
540
  } else if (c1->colId > c2->colId) {
272 ✔
541
    return 1;
272 ✔
542
  }
543

544
  return 0;
×
545
}
546

547
static int32_t addColData(SSDataBlock* pResBlock, int32_t index, void* data) {
3,121,268 ✔
548
  SColumnInfoData* pSrc = taosArrayGet(pResBlock->pDataBlock, index);
3,121,268 ✔
549
  if (pSrc == NULL) {
3,120,917 ✔
550
    return terrno;
×
551
  }
552

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

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

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

572
  if (blockDataGetNumOfCols(pDstBlock) > index) {
120,265 ✔
573
    STREAM_CHECK_RET_GOTO(addColData(pDstBlock, index++, &rollupTbCount));
603 ✔
574
  }
575

576
  stDebug("vgId:%d get tsdb meta skey:%" PRId64 ", ekey:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64
120,265 ✔
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++;
120,265 ✔
581

582
end:
120,265 ✔
583
  return code;
120,265 ✔
584
}
585

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

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

600
  return tSimpleHashPut(pCountMap, &gid, LONG_BYTES, &count, sizeof(count));
603 ✔
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,
226 ✔
624
                                       SSHashObj** ppByUid, SSHashObj** ppCountMap) {
625
  int32_t        code = TSDB_CODE_SUCCESS;
226 ✔
626
  int32_t        lino = 0;
226 ✔
627
  STableKeyInfo* pReadList = NULL;
226 ✔
628
  SSHashObj*     pByUid = NULL;
226 ✔
629
  SSHashObj*     pCountMap = NULL;
226 ✔
630
  SArray*        pNewEntries = NULL;
226 ✔
631

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

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

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

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

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

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

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

689
  return code;
2,746,180 ✔
690
}
691

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

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

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

719
static bool needReLoadTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int8_t tableType, int64_t suid, int64_t uid, bool isCalc){
442,939 ✔
720
  if ((tableType == TD_CHILD_TABLE || tableType == TD_VIRTUAL_CHILD_TABLE) &&
442,939 ✔
721
      sStreamReaderInfo->tableType == TD_SUPER_TABLE && 
227,918 ✔
722
      suid == sStreamReaderInfo->suid) {
161,662 ✔
723
    taosRLockLatch(&sStreamReaderInfo->lock);
3,659 ✔
724
    uint64_t gid = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, uid);
3,659 ✔
725
    taosRUnLockLatch(&sStreamReaderInfo->lock);
3,659 ✔
726
    if (gid == (uint64_t)-1) return true;
3,511 ✔
727
  }
728
  return false;
438,963 ✔
729
}
730

731
static bool matchTriggerTable(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid) {
3,149,057 ✔
732
  switch (sStreamReaderInfo->tableType) {
3,149,057 ✔
733
    case TD_SUPER_TABLE:
1,869,452 ✔
734
      return suid == sStreamReaderInfo->suid;
1,869,452 ✔
735
    case TD_CHILD_TABLE:
659,198 ✔
736
    case TD_VIRTUAL_CHILD_TABLE:
737
      return suid == sStreamReaderInfo->suid && uid == sStreamReaderInfo->uid;
659,198 ✔
738
    case TD_NORMAL_TABLE:
622,615 ✔
739
    case TD_VIRTUAL_NORMAL_TABLE:
740
      return uid == sStreamReaderInfo->uid;
622,615 ✔
741
    default:
×
742
      return true;
×
743
  }
744
}
745

746
static bool uidInTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id) {
3,149,846 ✔
747
  int32_t ret = false;
3,149,846 ✔
748
  if (!matchTriggerTable(sStreamReaderInfo, suid, uid)) {
3,149,846 ✔
749
    goto end;
1,762,496 ✔
750
  }
751
  if (sStreamReaderInfo->tableType == TD_SUPER_TABLE) {
1,387,775 ✔
752
    if (qStreamGetTableListNum(sStreamReaderInfo) == 0) goto end;
668,743 ✔
753
  }
754
  *id = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, uid);
1,386,859 ✔
755
  if (*id == -1) goto end;
1,386,919 ✔
756
  ret = true;
1,372,764 ✔
757

758
end:
3,150,757 ✔
759
  stTrace("%s ret:%d %p %p check suid:%" PRId64 " uid:%" PRId64 " gid:%"PRIu64, __func__, ret, sStreamReaderInfo, sStreamReaderInfo->tableList.gIdMap, suid, uid, *id);
3,150,757 ✔
760
  return ret;
3,149,129 ✔
761
}
762

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

767
static bool uidInTableListSet(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id, bool isCalc) {
3,722,908 ✔
768
  bool ret = false;
3,722,908 ✔
769
  taosRLockLatch(&sStreamReaderInfo->lock);
3,722,908 ✔
770
  if (sStreamReaderInfo->isVtableStream) {
3,725,881 ✔
771
    int64_t tmp[2] = {suid, uid};
578,056 ✔
772
    if(tSimpleHashGet(isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, tmp, sizeof(tmp)) != NULL) {
578,056 ✔
773
      *id = uid;
297,671 ✔
774
      ret = true;
297,671 ✔
775
    }
776
  } else {
777
    ret = uidInTableList(sStreamReaderInfo, suid, uid, id);
3,147,825 ✔
778
  }
779

780
end:
3,723,182 ✔
781
  taosRUnLockLatch(&sStreamReaderInfo->lock);
3,723,182 ✔
782
  return ret;
3,725,460 ✔
783
}
784

785
static int32_t  qTransformStreamTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, void* pTableListInfo, StreamTableListInfo* tableInfo){
84,698 ✔
786
  SArray* pList = qStreamGetTableListArray(pTableListInfo);
84,698 ✔
787
  int32_t totalSize = taosArrayGetSize(pList);
84,821 ✔
788
  int32_t code = 0;
84,825 ✔
789
  void* pTask = sStreamReaderInfo->pTask;
84,825 ✔
790
  for (int32_t i = 0; i < totalSize; ++i) {
226,982 ✔
791
    STableKeyInfo* info = taosArrayGet(pList, i);
142,227 ✔
792
    if (info == NULL) {
142,147 ✔
793
      continue;
×
794
    }
795
    code = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheTrigger, sStreamReaderInfo->pExprInfoTriggerTag, sStreamReaderInfo->numOfExprTriggerTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
142,147 ✔
796
    if (code != 0){
142,227 ✔
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);
142,227 ✔
801
    if (code != 0){
142,227 ✔
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);
142,227 ✔
806
    if (code != 0){
142,157 ✔
807
      return code;
×
808
    }
809
  }
810
  return 0;
84,665 ✔
811
}
812

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

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

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

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

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

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

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

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

882
  *ppPath = path;
9,718 ✔
883
  path = NULL;
9,718 ✔
884

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

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

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

915
  return TSDB_CODE_SUCCESS;
17,211 ✔
916
}
917

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

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

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

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

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

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

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

962
  SArray** ppGroup = taosHashIterate(sStreamReaderInfo->groupIdMap, NULL);
5,248 ✔
963
  while (ppGroup != NULL && sampleNum < 3) {
18,514 ✔
964
    formatRollupGroupPath(*ppGroup, sample[sampleNum], sizeof(sample[sampleNum]));
13,266 ✔
965
    sampleNum++;
13,266 ✔
966
    ppGroup = taosHashIterate(sStreamReaderInfo->groupIdMap, ppGroup);
13,266 ✔
967
  }
968
  if (ppGroup != NULL) {
5,248 ✔
969
    taosHashCancelIterate(sStreamReaderInfo->groupIdMap, ppGroup);
1,305 ✔
970
  }
971

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

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

984
  if (taosHashGet(sStreamReaderInfo->groupIdMap, &gid, sizeof(gid)) != NULL) {
22,377 ✔
985
    return TSDB_CODE_SUCCESS;
5,166 ✔
986
  }
987

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

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

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

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

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

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

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

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

1032
    STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, 0, ucs4, ucs4Len, false));
375 ✔
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:
375 ✔
1042
  taosMemoryFree(ucs4);
375 ✔
1043
  STREAM_PRINT_LOG_END(code, lino);
375 ✔
1044
  return code;
375 ✔
1045
}
1046

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

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

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

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

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

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

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

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

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

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

1100
  STREAM_CHECK_NULL_GOTO(pRollupCol, TSDB_CODE_STREAM_INVALID_ROLLUP);
225 ✔
1101

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

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

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

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

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

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

1140
  *ppBlock = NULL;
150 ✔
1141

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

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

1154
  *ppBlock = pBlock;
150 ✔
1155
  pBlock = NULL;
150 ✔
1156

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

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

1169
  STREAM_CHECK_NULL_GOTO(pRollupCol, TSDB_CODE_STREAM_INVALID_ROLLUP);
600 ✔
1170

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

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

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

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

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

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

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

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

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

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

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

1241
  code = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheTrigger,
9,718 ✔
1242
                  sStreamReaderInfo->pExprInfoTriggerTag, sStreamReaderInfo->numOfExprTriggerTag,
1243
                  &sStreamReaderInfo->storageApi, uid, 0, NULL);
1244
  if (code != TSDB_CODE_SUCCESS) {
9,718 ✔
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,
9,718 ✔
1251
               sStreamReaderInfo->numOfExprCalcTag, &sStreamReaderInfo->storageApi, uid, 0, NULL);
1252
  if (code != TSDB_CODE_SUCCESS) {
9,718 ✔
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;
9,718 ✔
1259
  code = parseRollupPath(path, pathLen, &rollupPath, &skipped);
9,718 ✔
1260
  if (skipped) {
9,718 ✔
1261
    goto end;
×
1262
  }
1263
  if (code != TSDB_CODE_SUCCESS) {
9,718 ✔
1264
    if (code == TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH) {
1,521 ✔
1265
      setRollupIllegalPathInfo(sStreamReaderInfo, uid, path, pathLen);
1,521 ✔
1266
    }
1267
    ST_TASK_ELOG("%s parse rollup tag failed for uid:%" PRId64 ", value:%s, code:%d", __func__, uid, path, code);
1,521 ✔
1268
    goto end;
1,521 ✔
1269
  }
1270

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

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

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

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

1344
  int32_t totalSize = taosArrayGetSize(pList);
6,618 ✔
1345
  for (int32_t i = 0; i < totalSize; ++i) {
14,737 ✔
1346
    STableKeyInfo* info = taosArrayGet(pList, i);
9,489 ✔
1347
    if (info == NULL) {
9,489 ✔
1348
      continue;
×
1349
    }
1350

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

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

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

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

1374
  if (sStreamReaderInfo->pTagCond != NULL) {
454 ✔
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);
454 ✔
1382
  for (int32_t i = 0; i < totalSize; ++i) {
757 ✔
1383
    STableKeyInfo* info = taosArrayGet(pSourceTableList, i);
454 ✔
1384
    if (info == NULL) {
454 ✔
1385
      continue;
×
1386
    }
1387

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

1394
  *ppTableList = pTableList;
303 ✔
1395
  pTableList = NULL;
303 ✔
1396

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

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

1409
  if (!sStreamReaderInfo->isRollupReader) {
91,439 ✔
1410
    STREAM_CHECK_RET_GOTO(nodesCloneList(sStreamReaderInfo->partitionCols, &groupNew));
84,821 ✔
1411
  }
1412

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

1418
  if (sStreamReaderInfo->isRollupReader) {
91,439 ✔
1419
    STREAM_CHECK_RET_GOTO(
6,618 ✔
1420
        qTransformRollupStreamTableList(sStreamReaderInfo, pTableListInfo, &sStreamReaderInfo->tableList));
×
1421
    logRollupGroupSummary(sStreamReaderInfo);
5,248 ✔
1422
  } else {
1423
    STREAM_CHECK_RET_GOTO(qTransformStreamTableList(sStreamReaderInfo, pTableListInfo, &sStreamReaderInfo->tableList));
84,821 ✔
1424
  }
1425

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

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

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

1471

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

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

1494
  void*   buf = NULL;
21,246 ✔
1495

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

1508
  char* dataBuf = (char*)buf;
21,246 ✔
1509
  *((int32_t*)(dataBuf)) = blockNum;
21,246 ✔
1510
  dataBuf += INT_BYTES;
21,246 ✔
1511
  for(size_t i = 0; i < taosArrayGetSize(pBlockList); i++){
46,332 ✔
1512
    SSDataBlock* pBlock = taosArrayGetP(pBlockList, i);
25,086 ✔
1513
    if (pBlock == NULL || pBlock->info.rows == 0) continue;
25,086 ✔
1514
    int32_t actualLen = blockEncode(pBlock, dataBuf, dataEncodeBufSize, taosArrayGetSize(pBlock->pDataBlock));
25,086 ✔
1515
    STREAM_CHECK_CONDITION_GOTO(actualLen < 0, terrno);
25,086 ✔
1516
    dataBuf += actualLen;
25,086 ✔
1517
  }
1518
  *data = buf;
21,246 ✔
1519
  *size = INT_BYTES + dataEncodeBufSize;
21,246 ✔
1520
  buf = NULL;
21,246 ✔
1521
end:
21,246 ✔
1522
  rpcFreeCont(buf);
21,246 ✔
1523
  return code;
21,173 ✔
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,
627,789 ✔
1547
                                    int32_t rollupTbCount) {
1548
  int32_t code = 0;
627,789 ✔
1549
  int32_t lino = 0;
627,789 ✔
1550
  STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_GID_COL, &id));
627,789 ✔
1551
  STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_SKEY_COL, &skey));
628,653 ✔
1552
  STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_EKEY_COL, &ekey));
628,225 ✔
1553
  STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_VER_COL, &ver));
627,728 ✔
1554
  if (blockDataGetNumOfCols(pBlock) > STREAM_WAL_META_ROLLUP_TBCOUNT_COL) {
628,151 ✔
1555
    STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_ROLLUP_TBCOUNT_COL, &rollupTbCount));
19,795 ✔
1556
  }
1557

1558
end:
627,867 ✔
1559
  return code;
627,867 ✔
1560
}
1561

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

1570
end:
729 ✔
1571
  return code;
729 ✔
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,
8,803 ✔
1583
                              int64_t ver) {
1584
  int32_t    code = 0;
8,803 ✔
1585
  int32_t    lino = 0;
8,803 ✔
1586
  SDecoder   decoder = {0};
8,803 ✔
1587
  SDeleteRes req = {0};
8,803 ✔
1588
  void* pTask = sStreamReaderInfo->pTask;
8,803 ✔
1589

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1738
    if (sStreamReaderInfo->deleteOutTbl != 0) {
144 ✔
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) {
144 ✔
1746
      if (uidList == NULL) {
144 ✔
1747
        uidList = taosArrayInit(8, sizeof(tb_uid_t));
144 ✔
1748
        STREAM_CHECK_NULL_GOTO(uidList, terrno);
144 ✔
1749
      }
1750
      STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pDropTbReq->uid), terrno);
288 ✔
1751
    }
1752

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

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

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

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

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

1785
  taosWLockLatch(&sStreamReaderInfo->lock);
13,456 ✔
1786
  int32_t totalSize = tableListDel == NULL ? 0 : taosArrayGetSize(tableListDel);
13,404 ✔
1787
  for (int32_t i = 0; i < totalSize; ++i) {
13,404 ✔
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);
13,404 ✔
1796
  for (int32_t i = 0; i < totalSize; ++i) {
22,505 ✔
1797
    STableKeyInfo* info = taosArrayGet(tableListAdd, i);
9,101 ✔
1798
    if (info == NULL) {
9,101 ✔
1799
      continue;
×
1800
    }
1801
    int ret = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheTrigger,
9,101 ✔
1802
                       sStreamReaderInfo->pExprInfoTriggerTag, sStreamReaderInfo->numOfExprTriggerTag,
1803
                       &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
1804
    if (ret != 0) {
9,101 ✔
1805
      ST_TASK_WLOG("%s cacheTag trigger failed for uid:%" PRId64 ",code:%d", __func__, info->uid, ret);
1,382 ✔
1806
      continue;
1,382 ✔
1807
    }
1808
    ret =
×
1809
        cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheCalc, sStreamReaderInfo->pExprInfoCalcTag,
7,719 ✔
1810
                 sStreamReaderInfo->numOfExprCalcTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
1811
    if (ret != 0) {
7,719 ✔
1812
      ST_TASK_WLOG("%s cacheTag calc failed for uid:%" PRId64 ",code:%d", __func__, info->uid, ret);
×
1813
      continue;
×
1814
    }
1815
    if (removedUidMap == NULL) {
7,719 ✔
1816
      STREAM_CHECK_RET_GOTO(qStreamRemoveTableList(&sStreamReaderInfo->tableList, info->uid));
7,038 ✔
1817
    } else if (tSimpleHashGet(removedUidMap, &info->uid, LONG_BYTES) == NULL) {
681 ✔
1818
      STREAM_CHECK_RET_GOTO(qStreamRemoveTableList(&sStreamReaderInfo->tableList, info->uid));
227 ✔
1819
      STREAM_CHECK_RET_GOTO(tSimpleHashPut(removedUidMap, &info->uid, LONG_BYTES, &info->uid, LONG_BYTES));
227 ✔
1820
    }
1821
    STREAM_CHECK_RET_GOTO(qStreamSetTableList(&sStreamReaderInfo->tableList, info->uid, info->groupId));
7,719 ✔
1822
  }
1823

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

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

1836
  if (taosArrayGetSize(uidList) == 0) {
13,607 ✔
1837
    return 0;
4,961 ✔
1838
  }
1839

1840
  if (!sStreamReaderInfo->isRollupReader) {
8,646 ✔
1841
    STREAM_CHECK_RET_GOTO(nodesCloneList(sStreamReaderInfo->partitionCols, &groupNew));
8,192 ✔
1842
  }
1843
  STREAM_CHECK_RET_GOTO(qStreamFilterTableListForReader(
8,646 ✔
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,646 ✔
1848
    STREAM_CHECK_RET_GOTO(buildRollupTableListFromSource(sStreamReaderInfo, sourceTableList, tableList));
454 ✔
1849
  }
1850

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

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

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

1872
  uidList = taosArrayInit(8, sizeof(tb_uid_t));
7,075 ✔
1873
  STREAM_CHECK_NULL_GOTO(uidList, terrno);
7,075 ✔
1874

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

1880
  SVCreateTbReq* pCreateReq = NULL;
7,075 ✔
1881
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
14,150 ✔
1882
    pCreateReq = req.pReqs + iReq;
7,075 ✔
1883
    if (!needReLoadTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false)) {
7,075 ✔
1884
      ST_TASK_DLOG("stream reader scan create table jump, %s", pCreateReq->name);
4,410 ✔
1885
      continue;
4,410 ✔
1886
    }
1887
    ST_TASK_ILOG("stream reader scan create table %s", pCreateReq->name);
2,665 ✔
1888
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pCreateReq->uid), terrno);
5,330 ✔
1889
  }
1890
  
1891
  STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo, uidList, &tableList));
7,075 ✔
1892
  STREAM_CHECK_RET_GOTO(qStreamGetAddTable(sStreamReaderInfo, tableList, uidListAdd));
6,924 ✔
1893
  if (sStreamReaderInfo->isVtableStream) {
6,924 ✔
1894
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
1,166 ✔
1895
  }
1896

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

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

1914
  ST_TASK_DLOG("%s start, name:%s uid:%"PRId64, __func__, pCreateReq->name, pCreateReq->uid);
435,547 ✔
1915
  if (!needReLoadTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false) ||
436,279 ✔
1916
      ignoreMetaChange(sStreamReaderInfo->tableList.version, ver)) {
605 ✔
1917
    ST_TASK_DLOG("stream reader scan auto create table jump, %s", pCreateReq->name);
435,423 ✔
1918
    goto end;
435,281 ✔
1919
  }
1920
  uidList = taosArrayInit(8, sizeof(tb_uid_t));
441 ✔
1921
  STREAM_CHECK_NULL_GOTO(uidList, terrno);
441 ✔
1922
  STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pCreateReq->uid), terrno);
882 ✔
1923
  ST_TASK_DLOG("stream reader scan auto create table %s", pCreateReq->name);
441 ✔
1924

1925
  STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo, uidList, &tableList));
441 ✔
1926
  STREAM_CHECK_RET_GOTO(qStreamModifyTableList(sStreamReaderInfo, tableList, uidList));
441 ✔
1927
end:
435,164 ✔
1928
  taosArrayDestroy(uidList);
435,935 ✔
1929
  taosArrayDestroy(tableList);
435,793 ✔
1930
  return code;
435,935 ✔
1931
}
1932

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

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

1962
void getAlterColId(void* pVnode, int64_t uid, const char* colName, col_id_t* colId) {
73 ✔
1963
  SSchemaWrapper *pSchema = metaGetTableSchema(((SVnode *)pVnode)->pMeta, uid, -1, 1, NULL, 0, false);
73 ✔
1964
  if (pSchema == NULL) {
73 ✔
1965
    return;
×
1966
  }
1967
  for (int32_t i = 0; i < pSchema->nCols; i++) {
146 ✔
1968
    if (strncmp(pSchema->pSchema[i].name, colName, TSDB_COL_NAME_LEN) == 0) {
146 ✔
1969
      *colId = pSchema->pSchema[i].colId;
73 ✔
1970
      break;
73 ✔
1971
    }
1972
  }
1973
  tDeleteSchemaWrapper(pSchema);
×
1974
  return;
73 ✔
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, 
73 ✔
1979
                                       SVAlterTbReq* pReq, uint64_t uid, int64_t ver) {
1980
  int32_t code = 0;
73 ✔
1981
  int32_t lino = 0;
73 ✔
1982
  void* pTask = sStreamReaderInfo->pTask;
73 ✔
1983
  SArray* uidListAdd = NULL;
73 ✔
1984

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

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

1991
  col_id_t colId = 0;
73 ✔
1992
  getAlterColId(sStreamReaderInfo->pVnode, uid, pReq->colName, &colId);
73 ✔
1993
  if (atomic_load_8(&sStreamReaderInfo->isVtableOnlyTs) == 0 && !isColIdInList(sStreamReaderInfo->triggerCols, colId)) {
73 ✔
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);
73 ✔
1999
  STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
73 ✔
2000

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

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

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

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

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

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

2047
end:
6,091 ✔
2048
  if (code != 0) {
6,091 ✔
2049
    ST_TASK_ELOG("%s failed,code:%d", __func__, code);
×
2050
    taosArrayDestroy(uidList);
×
2051
    uidList = NULL;
×
2052
  }
2053
  return uidList;
6,091 ✔
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,091 ✔
2058
                                    SArray* uidList, int64_t ver) {
2059
  int32_t code = 0;
6,091 ✔
2060
  int32_t lino = 0;
6,091 ✔
2061
  void* pTask = sStreamReaderInfo->pTask;
6,091 ✔
2062
  SArray* uidListAdd = NULL;
6,091 ✔
2063
  SArray* uidListDel = NULL;
6,091 ✔
2064
  SArray* tableList = NULL;
6,091 ✔
2065

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

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

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

2077
  if (rsp->checkAlter && taosArrayGetSize(uidListDel) > 0 && rsp->totalDataRows > 0) {
6,091 ✔
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,091 ✔
2085
  if (sStreamReaderInfo->isVtableStream) {
6,091 ✔
2086
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
483 ✔
2087
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListDel, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_RETIRE));
483 ✔
2088
  }
2089
  STREAM_CHECK_RET_GOTO(qStreamModifyTableList(sStreamReaderInfo, tableList, uidList));
6,091 ✔
2090

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

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

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

2108
  ST_TASK_DLOG("%s start", __func__);
7,634 ✔
2109

2110
  SVAlterTbReq req = {0};
7,634 ✔
2111
  tDecoderInit(&decoder, data, len);
7,634 ✔
2112
  
2113
  STREAM_CHECK_RET_GOTO(tDecodeSVAlterTbReq(&decoder, &req));
7,634 ✔
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 && 
7,634 ✔
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,634 ✔
2119
  if (req.action == TSDB_ALTER_TABLE_ALTER_COLUMN_REF || req.action == TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
7,634 ✔
2120
    STREAM_CHECK_CONDITION_GOTO(!sStreamReaderInfo->isVtableStream, TDB_CODE_SUCCESS);
1,543 ✔
2121
    code = checkAlter(sStreamReaderInfo, req.tbName, req.action, &uid);
73 ✔
2122
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || uid == 0) {
73 ✔
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));
73 ✔
2128
  } else if (req.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL) {
6,091 ✔
2129
    uidList = getTableListForAlterSuperTable(sStreamReaderInfo, &req);
6,091 ✔
2130
    STREAM_CHECK_NULL_GOTO(uidList, terrno);
6,091 ✔
2131
    STREAM_CHECK_RET_GOTO(scanAlterTableTagVal(sStreamReaderInfo, rsp, uidList, ver));
6,091 ✔
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,164 ✔
2146

2147
end:
7,634 ✔
2148
  destroyAlterTbReq(&req);
7,634 ✔
2149

2150
  taosArrayDestroy(uidList);
7,634 ✔
2151
  tDecoderClear(&decoder);
7,634 ✔
2152
  STREAM_PRINT_LOG_END_WITHID(code, lino);
7,634 ✔
2153
  return code;
7,634 ✔
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,249,332 ✔
2199
  int32_t code = 0;
1,249,332 ✔
2200
  int32_t lino = 0;
1,249,332 ✔
2201
  void* pTask = sStreamReaderInfo->pTask;
1,249,332 ✔
2202
  WalMetaResult walMeta = {0};
1,249,332 ✔
2203
  SSubmitTbData submitTbData = {0};
1,249,847 ✔
2204
  bool          expandRollupGids = isRollupMultiReader(sStreamReaderInfo);
1,249,997 ✔
2205

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

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

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

2227
  // submit data
2228
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
1,250,086 ✔
2229
    code = TSDB_CODE_INVALID_MSG;
×
2230
    TSDB_CHECK_CODE(code, lino, end);
×
2231
  }
2232
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
1,250,232 ✔
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,250,232 ✔
2238
    goto end;
1,061,545 ✔
2239
  }
2240
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
188,985 ✔
2241
    code = TSDB_CODE_INVALID_MSG;
×
2242
    TSDB_CHECK_CODE(code, lino, end);
×
2243
  }
2244

2245
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
188,985 ✔
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;
188,904 ✔
2283
    if (tDecodeU64v(pCoder, &nRow) < 0) {
188,904 ✔
2284
      code = TSDB_CODE_INVALID_MSG;
×
2285
      TSDB_CHECK_CODE(code, lino, end);
×
2286
    }
2287
    if (nRow == 0) {
188,904 ✔
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) {
700,850 ✔
2293
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
511,946 ✔
2294
      pCoder->pos += pRow->len;
511,946 ✔
2295
      if (iRow == 0){
511,946 ✔
2296
#ifndef NO_UNALIGNED_ACCESS
2297
        walMeta.skey = pRow->ts;
188,904 ✔
2298
#else
2299
        walMeta.skey = taosGetInt64Aligned(&pRow->ts);
2300
#endif
2301
      }
2302
      if (iRow == nRow - 1) {
511,946 ✔
2303
#ifndef NO_UNALIGNED_ACCESS
2304
        walMeta.ekey = pRow->ts;
188,904 ✔
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) {
188,985 ✔
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) {
188,985 ✔
2319
    STREAM_CHECK_RET_GOTO(putWalMetaResultForRollupUid(sStreamReaderInfo, gidHash, submitTbData.suid, submitTbData.uid,
6,600 ✔
2320
                                                       walMeta.skey, walMeta.ekey));
2321
  } else {
2322
    STREAM_CHECK_RET_GOTO(putWalMetaResult(gidHash, walMeta.id, walMeta.skey, walMeta.ekey));
182,385 ✔
2323
  }
2324

2325
end:
1,249,222 ✔
2326
  tdDestroySVCreateTbReq(submitTbData.pCreateTbReq);
1,250,252 ✔
2327
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
1,250,110 ✔
2328
  tEndDecode(pCoder);
1,250,110 ✔
2329
  return code;
1,250,058 ✔
2330
}
2331

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

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

2345
  uint64_t nSubmitTbData = 0;
1,249,959 ✔
2346
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
1,249,650 ✔
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,249,650 ✔
2352
  STREAM_CHECK_NULL_GOTO(gidHash, terrno);
1,249,644 ✔
2353

2354
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
2,499,634 ✔
2355
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataForMeta(&decoder, sStreamReaderInfo, gidHash, ver, win));
1,249,998 ✔
2356
  }
2357
  tEndDecode(&decoder);
1,249,636 ✔
2358

2359
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
1,249,430 ✔
2360
  int32_t iter = 0;
1,249,301 ✔
2361
  void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
1,249,301 ✔
2362
  while (px != NULL) {
1,450,640 ✔
2363
    WalMetaResult* pMeta = (WalMetaResult*)px;
200,538 ✔
2364
    int32_t        rollupTbCount = qStreamGetGroupTableCount(sStreamReaderInfo, pMeta->id);
200,538 ✔
2365
    STREAM_CHECK_RET_GOTO(
200,768 ✔
2366
        buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver, rollupTbCount));
96 ✔
2367
    ((SSDataBlock*)rsp->metaBlock)->info.rows++;
200,628 ✔
2368
    rsp->totalRows++;
200,628 ✔
2369
    ST_TASK_DLOG("stream reader scan submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
200,628 ✔
2370
          ", ver:%"PRId64, pMeta->skey, pMeta->ekey, pMeta->id, ver);
2371
    px = tSimpleHashIterate(gidHash, px, &iter);
200,628 ✔
2372
  }
2373
end:
1,250,102 ✔
2374
  tDecoderClear(&decoder);
1,250,102 ✔
2375
  tSimpleHashCleanup( gidHash);
1,249,890 ✔
2376
  return code;
1,249,896 ✔
2377
}
2378

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

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

2397
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
49,585 ✔
2398

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

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

2409
  schemas = taosArrayInit(8, sizeof(SSchema));
93,695 ✔
2410
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
93,916 ✔
2411

2412
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES,
93,916 ✔
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))
93,916 ✔
2415
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, STREAM_WAL_META_EKEY_COL))
93,916 ✔
2416
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, STREAM_WAL_META_VER_COL))
93,916 ✔
2417
  if (withRollupTbCount) {
93,916 ✔
2418
    STREAM_CHECK_RET_GOTO(
4,468 ✔
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));
93,916 ✔
2423

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

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

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

2436
  SDecoder dcoder = {0};
124,122 ✔
2437
  tDecoderInit(&dcoder, data, len);
124,122 ✔
2438
  if (msgType == TDMT_VND_DELETE && sStreamReaderInfo->deleteReCalc != 0) {
124,122 ✔
2439
    if (rsp->deleteBlock == NULL) {
8,803 ✔
2440
      STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&rsp->deleteBlock, false));
3,847 ✔
2441
    }
2442
      
2443
    STREAM_CHECK_RET_GOTO(scanDeleteDataNew(sStreamReaderInfo, rsp, data, len, ver));
8,803 ✔
2444
  } else if (msgType == TDMT_VND_DROP_TABLE &&
115,319 ✔
2445
             (sStreamReaderInfo->deleteOutTbl != 0 || sStreamReaderInfo->isVtableStream)) {
7,616 ✔
2446
    STREAM_CHECK_RET_GOTO(scanDropTableNew(sStreamReaderInfo, rsp, data, len, ver));
3,224 ✔
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)) {
112,095 ✔
2450
    STREAM_CHECK_RET_GOTO(scanCreateTableNew(sStreamReaderInfo, rsp, data, len, ver));
7,075 ✔
2451
  } else if (msgType == TDMT_VND_ALTER_STB && !ignoreMetaChange(sStreamReaderInfo->tableList.version, ver)) {
105,020 ✔
2452
    // STREAM_CHECK_RET_GOTO(scanAlterSTableNew(sStreamReaderInfo, data, len));
2453
  } else if (msgType == TDMT_VND_ALTER_TABLE && !ignoreMetaChange(sStreamReaderInfo->tableList.version, ver)) {
97,262 ✔
2454
    STREAM_CHECK_RET_GOTO(scanAlterTableNew(sStreamReaderInfo, rsp, data, len, ver));
7,634 ✔
2455
  }
2456

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

2462
static bool shouldStopBeforeSubmitAfterTableChange(SStreamTriggerReaderInfo* sStreamReaderInfo,
2,461,169 ✔
2463
                                                   SSTriggerWalNewRsp*       rsp) {
2464
  return sStreamReaderInfo->isVtableStream && rsp->tableBlock != NULL &&
2,458,738 ✔
2465
         ((SSDataBlock*)rsp->tableBlock)->info.rows > 0;
431 ✔
2466
}
2467

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

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

2481
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
785,211 ✔
2482
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
784,715 ✔
2483
  code = walReaderSeekVer(pWalReader, rsp->ver);
784,715 ✔
2484
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
785,081 ✔
2485
    if (rsp->ver < walGetFirstVer(pWalReader->pWal)) {
691,024 ✔
2486
      rsp->ver = walGetFirstVer(pWalReader->pWal);
×
2487
      rsp->verTime = 0;
×
2488
    } else {
2489
      rsp->verTime = taosGetTimestampUs();
691,089 ✔
2490
    }
2491
    ST_TASK_DLOG("vgId:%d %s scan wal end:%s", TD_VID(pVnode), __func__, tstrerror(code));
691,089 ✔
2492
    code = TSDB_CODE_SUCCESS;
691,164 ✔
2493
    goto end;
691,164 ✔
2494
  }
2495
  STREAM_CHECK_RET_GOTO(code);
94,057 ✔
2496

2497
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, STREAM_RETURN_ROWS_NUM));
94,057 ✔
2498

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

2517
    ST_TASK_DLOG("vgId:%d stream reader scan wal ver:%" PRId64 "/%" PRId64 ", type:%s, deleteData:%d, deleteTb:%d",
1,289,206 ✔
2518
      TD_VID(pVnode), ver, walGetAppliedVer(pWalReader->pWal), TMSG_INFO(wCont->msgType), sStreamReaderInfo->deleteReCalc, sStreamReaderInfo->deleteOutTbl);
2519
    if (wCont->msgType == TDMT_VND_SUBMIT) {
1,289,811 ✔
2520
      // return when getting data if there are meta data in vtable scan
2521
      if (shouldStopBeforeSubmitAfterTableChange(sStreamReaderInfo, rsp)) {
1,250,239 ✔
2522
        rsp->ver--;
431 ✔
2523
        break;
431 ✔
2524
      }
2525
      data = POINTER_SHIFT(walContBody(wCont), sizeof(SSubmitReq2Msg));
1,249,746 ✔
2526
      len = walContBodyLen(wCont) - sizeof(SSubmitReq2Msg);
1,249,746 ✔
2527
      STREAM_CHECK_RET_GOTO(scanSubmitDataForMeta(sStreamReaderInfo, rsp, data, len, ver, win));
1,249,746 ✔
2528
    } else {
2529
      STREAM_CHECK_RET_GOTO(processMeta(wCont->msgType, sStreamReaderInfo, data, len, rsp, ver));
39,572 ✔
2530
    }
2531

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

2537
end:
784,691 ✔
2538
  walCloseReader(pWalReader);
785,291 ✔
2539
  return code;
785,083 ✔
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) {
6,071,130 ✔
2543
  int32_t     code = 0;
6,071,130 ✔
2544
  int32_t     lino = 0;
6,071,130 ✔
2545
  SMetaReader mr = {0};
6,071,130 ✔
2546
  SArray* tagCache = NULL;
6,071,210 ✔
2547
  char* data = NULL;
6,071,116 ✔
2548

2549
  if (lock != NULL) taosWLockLatch(lock);
6,071,214 ✔
2550
  STREAM_CHECK_CONDITION_GOTO(numOfExpr == 0, code);
6,071,454 ✔
2551
  stDebug("%s start,uid:%"PRIu64, __func__, uid);
425,604 ✔
2552
  void* uidData = taosHashGet(metaCache, &uid, LONG_BYTES);
425,604 ✔
2553
  if (uidData == NULL) {
425,604 ✔
2554
    tagCache = taosArrayInit(numOfExpr, POINTER_BYTES);
419,030 ✔
2555
    STREAM_CHECK_NULL_GOTO(tagCache, terrno);
419,030 ✔
2556
    if(taosHashPut(metaCache, &uid, LONG_BYTES, &tagCache, POINTER_BYTES) != 0) {
419,030 ✔
2557
      taosArrayDestroy(tagCache);
×
2558
      code = terrno;
×
2559
      goto end;
×
2560
    }
2561
  } else {
2562
    tagCache = *(SArray**)uidData;
6,574 ✔
2563
    stDebug("%s found tagCache, size:%zu %d, uid:%"PRIu64, __func__, taosArrayGetSize(tagCache), numOfExpr, uid);
6,574 ✔
2564
    STREAM_CHECK_CONDITION_GOTO(taosArrayGetSize(tagCache) != numOfExpr, TSDB_CODE_INVALID_PARA);
6,574 ✔
2565
  }
2566
  
2567
  api->metaReaderFn.initReader(&mr, pVnode, META_READER_LOCK, &api->metaFn, 0);
425,604 ✔
2568
  code = api->metaReaderFn.getEntryGetUidCache(&mr, uid);
425,534 ✔
2569
  api->metaReaderFn.readerReleaseLock(&mr);
425,380 ✔
2570
  STREAM_CHECK_RET_GOTO(code);
425,604 ✔
2571
  
2572
  for (int32_t j = 0; j < numOfExpr; ++j) {
1,944,358 ✔
2573
    const SExprInfo* pExpr1 = &pExprInfo[j];
1,520,446 ✔
2574
    int32_t functionId = pExpr1->pExpr->_function.functionId;
1,520,446 ✔
2575
    col_id_t cid = 0;
1,520,446 ✔
2576
    // this is to handle the tbname
2577
    if (fmIsScanPseudoColumnFunc(functionId)) {
1,520,446 ✔
2578
      int32_t fType = pExpr1->pExpr->_function.functionType;
190,938 ✔
2579
      if (fType == FUNCTION_TYPE_TBNAME) {
190,938 ✔
2580
        data = taosMemoryCalloc(1, strlen(mr.me.name) + VARSTR_HEADER_SIZE);
190,868 ✔
2581
        STREAM_CHECK_NULL_GOTO(data, terrno);
190,764 ✔
2582
        STR_TO_VARSTR(data, mr.me.name)
190,764 ✔
2583
      }
2584
      cid = -1;
190,870 ✔
2585
    } else {  // these are tags
2586
      const char* p = NULL;
1,329,369 ✔
2587
      char* pData = NULL;
1,329,369 ✔
2588
      int8_t type = pExpr1->base.resSchema.type;
1,329,369 ✔
2589
      int32_t len = pExpr1->base.resSchema.bytes;
1,329,369 ✔
2590
      STagVal tagVal = {0};
1,329,369 ✔
2591
      tagVal.cid = pExpr1->base.pParam[0].pCol->colId;
1,329,439 ✔
2592
      cid = tagVal.cid;
1,329,439 ✔
2593
      if (colId != 0 && cid != colId) {
1,329,439 ✔
2594
        continue;
291 ✔
2595
      }
2596
      p = api->metaFn.extractTagVal(mr.me.ctbEntry.pTags, type, &tagVal);
1,329,148 ✔
2597

2598
      if (type != TSDB_DATA_TYPE_JSON && p != NULL) {
1,329,235 ✔
2599
        pData = tTagValToData((const STagVal*)p, false);
1,326,405 ✔
2600
      } else {
2601
        pData = (char*)p;
2,830 ✔
2602
      }
2603

2604
      if (pData != NULL && (type == TSDB_DATA_TYPE_JSON || !IS_VAR_DATA_TYPE(type))) {
1,329,235 ✔
2605
        if (type == TSDB_DATA_TYPE_JSON) {
787,934 ✔
2606
          len = getJsonValueLen(pData);
×
2607
        }
2608
        data = taosMemoryCalloc(1, len);
787,952 ✔
2609
        STREAM_CHECK_NULL_GOTO(data, terrno);
787,952 ✔
2610
        (void)memcpy(data, pData, len);
787,952 ✔
2611
      } else {
2612
        data = pData;
541,283 ✔
2613
      }
2614
    }
2615
    if (uidData == NULL){
1,519,977 ✔
2616
      STREAM_CHECK_NULL_GOTO(taosArrayPush(tagCache, &data), terrno);
3,014,233 ✔
2617
    } else {
2618
      void* pre = taosArrayGetP(tagCache, j);
12,721 ✔
2619
      taosMemoryFree(pre);
12,721 ✔
2620
      taosArraySet(tagCache, j, &data);
12,721 ✔
2621
    }
2622
    data = NULL;
1,519,845 ✔
2623
  }
2624

2625
end:
6,071,125 ✔
2626
  taosMemoryFree(data);
6,071,237 ✔
2627
  api->metaReaderFn.clearReader(&mr);
6,071,091 ✔
2628
  if (lock != NULL) taosWUnLockLatch(lock);
6,071,266 ✔
2629
  return code;
6,071,461 ✔
2630
}
2631

2632
int32_t fillTag(SHashObj* metaCache, SExprInfo* pExprInfo, int32_t numOfExpr,
1,311,763,008 ✔
2633
                uint64_t uid, SSDataBlock* pBlock, uint32_t currentRow, uint32_t numOfRows, uint32_t numOfBlocks, SRWLatch* lock) {
2634
  int32_t     code = 0;
1,311,763,008 ✔
2635
  int32_t     lino = 0;
1,311,763,008 ✔
2636
  SArray* tagCache = NULL;
1,311,763,008 ✔
2637
  if (numOfExpr == 0) {
1,311,763,008 ✔
2638
    return TSDB_CODE_SUCCESS;
5,485,708 ✔
2639
  }
2640

2641
  taosRLockLatch(lock);
1,306,277,300 ✔
2642
  void* uidData = taosHashGet(metaCache, &uid, LONG_BYTES);
1,311,365,301 ✔
2643
  if (uidData == NULL) {
1,309,927,740 ✔
2644
    stError("%s error uidData is null,uid:%"PRIu64, __func__, uid);
×
2645
  } else {
2646
    tagCache = *(SArray**)uidData;
1,309,927,740 ✔
2647
    if(taosArrayGetSize(tagCache) != numOfExpr) {
1,309,927,740 ✔
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) {
2,147,483,647 ✔
2654
    const SExprInfo* pExpr1 = &pExprInfo[j];
2,147,483,647 ✔
2655
    int32_t          dstSlotId = pExpr1->base.resSchema.slotId;
2,147,483,647 ✔
2656

2657
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, dstSlotId);
2,147,483,647 ✔
2658
    STREAM_CHECK_NULL_GOTO(pColInfoData, terrno);
2,147,483,647 ✔
2659
    int32_t functionId = pExpr1->pExpr->_function.functionId;
2,147,483,647 ✔
2660

2661
    // this is to handle the tbname
2662
    if (fmIsScanPseudoColumnFunc(functionId)) {
2,147,483,647 ✔
2663
      int32_t fType = pExpr1->pExpr->_function.functionType;
954,601 ✔
2664
      if (fType == FUNCTION_TYPE_TBNAME) {
954,601 ✔
2665
        pColInfoData->info.colId = -1;
954,601 ✔
2666
      }
2667
    } 
2668
    char* data = tagCache == NULL ? NULL : taosArrayGetP(tagCache, j);
2,147,483,647 ✔
2669

2670
    bool isNullVal = (data == NULL) || (pColInfoData->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data));
2,147,483,647 ✔
2671
    if (isNullVal) {
2,147,483,647 ✔
2672
      colDataSetNNULL(pColInfoData, currentRow, numOfRows);
×
2673
    } else {
2674
      if (!IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
2,147,483,647 ✔
2675
        for (uint32_t i = 0; i < numOfRows; i++){
2,147,483,647 ✔
2676
          colDataClearNull_f(pColInfoData->nullbitmap, currentRow + i);
2,147,483,647 ✔
2677
        }
2678
      }
2679
      code = colDataSetNItems(pColInfoData, currentRow, (const char*)data, numOfRows, numOfBlocks, false);
2,147,483,647 ✔
2680
      STREAM_CHECK_RET_GOTO(code);
2,147,483,647 ✔
2681
    }
2682
  }
2683
end:
1,305,930,506 ✔
2684
  taosRUnLockLatch(lock);
1,305,930,506 ✔
2685
  return code;
1,311,292,781 ✔
2686
}
2687

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

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

2703
end:
920,914 ✔
2704
  return code;
920,914 ✔
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) {
448,524 ✔
2759
  int32_t code = 0;
448,524 ✔
2760
  int32_t lino = 0;
448,524 ✔
2761
  int64_t id[2] = {suid, uid};
448,524 ✔
2762
  taosRLockLatch(&sStreamReaderInfo->lock);
448,524 ✔
2763
  void *px = tSimpleHashGet(rsp->isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, id, sizeof(id));
448,526 ✔
2764
  STREAM_CHECK_NULL_GOTO(px, TSDB_CODE_INVALID_PARA);
448,526 ✔
2765
  SSHashObj* uInfo = *(SSHashObj **)px;
448,526 ✔
2766
  STREAM_CHECK_NULL_GOTO(uInfo, TSDB_CODE_INVALID_PARA);
448,526 ✔
2767
  int16_t*  tmp = tSimpleHashGet(uInfo, &i, sizeof(i));
448,526 ✔
2768
  if (tmp != NULL) {
448,116 ✔
2769
    *colId = *tmp;
392,596 ✔
2770
  } else {
2771
    *colId = -1;
55,520 ✔
2772
  }
2773

2774
end:
448,116 ✔
2775
  taosRUnLockLatch(&sStreamReaderInfo->lock);
448,250 ✔
2776
  return code;
448,382 ✔
2777
}
2778

2779
static int32_t getSchemas(SVnode* pVnode, int64_t suid, int64_t uid, int32_t sver, SStreamTriggerReaderInfo* sStreamReaderInfo, STSchema** schema) {
970,277 ✔
2780
  int32_t code = 0;
970,277 ✔
2781
  int32_t lino = 0;
970,277 ✔
2782
  int64_t id = suid != 0 ? suid : uid;
970,277 ✔
2783
  if (sStreamReaderInfo->isVtableStream) {
970,348 ✔
2784
    STSchema** schemaTmp = taosHashGet(sStreamReaderInfo->triggerTableSchemaMapVTable, &id, LONG_BYTES);
179,972 ✔
2785
    if (schemaTmp == NULL || *schemaTmp == NULL || (*schemaTmp)->version != sver) {
179,972 ✔
2786
      *schema = metaGetTbTSchema(pVnode->pMeta, id, sver, 1);
7,340 ✔
2787
      STREAM_CHECK_NULL_GOTO(*schema, terrno);
7,340 ✔
2788
      code = taosHashPut(sStreamReaderInfo->triggerTableSchemaMapVTable, &id, LONG_BYTES, schema, POINTER_BYTES);
7,340 ✔
2789
      if (code != 0) {
7,340 ✔
2790
        taosMemoryFree(*schema);
×
2791
        goto end;
×
2792
      }
2793
    } else {
2794
      *schema = *schemaTmp;
172,632 ✔
2795
    }
2796
  } else {
2797
    if (sStreamReaderInfo->triggerTableSchema == NULL || sStreamReaderInfo->triggerTableSchema->version != sver) {
790,376 ✔
2798
      taosMemoryFree(sStreamReaderInfo->triggerTableSchema);
34,144 ✔
2799
      sStreamReaderInfo->triggerTableSchema = metaGetTbTSchema(pVnode->pMeta, id, sver, 1);
34,107 ✔
2800
      STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->triggerTableSchema, terrno);
33,961 ✔
2801
    }
2802
    *schema = sStreamReaderInfo->triggerTableSchema;
790,193 ✔
2803
  }
2804
  
2805
end:
970,165 ✔
2806
  return code;
970,099 ✔
2807
}
2808

2809
static int32_t finishWalDataTarget(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp,
970,737 ✔
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;
970,737 ✔
2814
  int32_t lino = 0;
970,737 ✔
2815
  void*   pTask = sStreamReaderInfo->pTask;
970,737 ✔
2816

2817
  if (numOfRows <= 0) {
970,737 ✔
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) {
970,737 ✔
2824
    STREAM_CHECK_RET_GOTO(processTag(sStreamReaderInfo, rsp->isCalc, uid, pBlock, blockStart, numOfRows, 1));
790,834 ✔
2825
  }
2826

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

2831
  STREAM_CHECK_NULL_GOTO(pSlice, TSDB_CODE_INVALID_PARA);
971,319 ✔
2832
  ST_TASK_DLOG("%s process submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64 ", uid:%" PRId64
971,319 ✔
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;
971,277 ✔
2837
  pBlock->info.rows += numOfRows;
971,277 ✔
2838

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

2843
end:
971,498 ✔
2844
  return code;
971,498 ✔
2845
}
2846

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

2857
  if (tStartDecode(pCoder) < 0) {
1,170,386 ✔
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);
840 ✔
2861
  }
2862

2863
  SSubmitTbData submitTbData = {0};
1,171,030 ✔
2864
  uint8_t       version = 0;
1,170,245 ✔
2865
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
1,169,891 ✔
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);
840 ✔
2869
  }
2870
  version = (submitTbData.flags >> 8) & 0xff;
1,169,891 ✔
2871
  submitTbData.flags = submitTbData.flags & 0xff;
1,169,891 ✔
2872
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
2873
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
1,169,891 ✔
2874
    if (tStartDecode(pCoder) < 0) {
16,172 ✔
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);
108 ✔
2878
    }
2879
    tEndDecode(pCoder);
16,172 ✔
2880
  }
2881

2882
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
1,170,245 ✔
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);
840 ✔
2886
  }
2887
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
1,169,967 ✔
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);
840 ✔
2891
  }
2892

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

2896
  SWalDataTarget singleTarget = {.gid = 0, .window = {.skey = INT64_MIN, .ekey = INT64_MAX}};
1,169,897 ✔
2897
  if (rsp->uidHash != NULL) {
1,169,680 ✔
2898
    uint64_t* gid = tSimpleHashGet(rsp->uidHash, &submitTbData.uid, LONG_BYTES);
549,469 ✔
2899
    STREAM_CHECK_CONDITION_GOTO(gid == NULL, TDB_CODE_SUCCESS);
549,680 ✔
2900
    ST_TASK_DLOG("%s get uid gid from uidHash, uid:%" PRId64 ", suid:%" PRId64 " gid:%" PRIu64, __func__,
548,969 ✔
2901
                  submitTbData.uid, submitTbData.suid, *gid);
2902
    singleTarget.gid = *gid;
548,894 ✔
2903
  }
2904
  if (expandRollupGids) {
1,169,105 ✔
2905
    targets = taosArrayInit(4, sizeof(SWalDataTarget));
40,719 ✔
2906
    STREAM_CHECK_NULL_GOTO(targets, terrno);
40,719 ✔
2907
    STREAM_CHECK_RET_GOTO(
40,719 ✔
2908
        collectWalRollupTargets(sStreamReaderInfo, ranges, submitTbData.suid, submitTbData.uid, targets));
×
2909
    STREAM_CHECK_CONDITION_GOTO(taosArrayGetSize(targets) == 0, TDB_CODE_SUCCESS);
40,719 ✔
2910
  } else {
2911
    if (singleTarget.gid == 0) {
1,128,386 ✔
2912
      STREAM_CHECK_CONDITION_GOTO(
745,014 ✔
2913
          !uidInTableListSet(sStreamReaderInfo, submitTbData.suid, submitTbData.uid, &singleTarget.gid, rsp->isCalc),
2914
          TDB_CODE_SUCCESS);
132 ✔
2915
    }
2916

2917
    if (ranges != NULL) {
929,343 ✔
2918
      void* timerange = tSimpleHashGet(ranges, &singleTarget.gid, sizeof(singleTarget.gid));
508,648 ✔
2919
      if (timerange == NULL) goto end;
508,577 ✔
2920
      int64_t* pRange = (int64_t*)timerange;
508,577 ✔
2921
      singleTarget.window.skey = pRange[0];
508,577 ✔
2922
      singleTarget.window.ekey = pRange[1];
508,577 ✔
2923
      ST_TASK_DLOG("%s get time range from ranges, uid:%" PRId64 ", suid:%" PRId64 ", gid:%" PRIu64 ", skey:%" PRId64
508,577 ✔
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) {
970,568 ✔
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);
132 ✔
2934
  }
2935

2936
  STSchema* schema = NULL;
970,568 ✔
2937
  STREAM_CHECK_RET_GOTO(
969,858 ✔
2938
      getSchemas(pVnode, submitTbData.suid, submitTbData.uid, submitTbData.sver, sStreamReaderInfo, &schema));
132 ✔
2939

2940
  int32_t numOfTargets = expandRollupGids ? taosArrayGetSize(targets) : 1;
970,090 ✔
2941
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
970,090 ✔
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;
970,009 ✔
3056
    if (tDecodeU64v(pCoder, &nRow) < 0) {
970,411 ✔
3057
      code = TSDB_CODE_INVALID_MSG;
×
3058
      TSDB_CHECK_CODE(code, lino, end);
×
3059
    }
3060
    int32_t rowsPos = pCoder->pos;
970,411 ✔
3061

3062
    for (int32_t t = 0; t < numOfTargets; ++t) {
1,941,239 ✔
3063
      SWalDataTarget* target = expandRollupGids ? taosArrayGet(targets, t) : &singleTarget;
969,549 ✔
3064
      STREAM_CHECK_NULL_GOTO(target, terrno);
970,743 ✔
3065
      SStreamWalDataSlice* pSlice = getWalDataSlice(sStreamReaderInfo, rsp, submitTbData.uid, target->gid);
970,875 ✔
3066
      if (pSlice == NULL) {
971,342 ✔
3067
        ST_TASK_WLOG("%s no row data slice, uid:%" PRId64 ", gid:%" PRIu64, __func__, submitTbData.uid, target->gid);
325 ✔
3068
        continue;
×
3069
      }
3070

3071
      int32_t blockStart = pSlice->currentRowIdx;
971,017 ✔
3072
      int32_t sliceEnd = pSlice->startRowIdx + pSlice->numRows;
971,017 ✔
3073
      if (blockStart >= sliceEnd) {
971,017 ✔
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;
971,017 ✔
3080
      int32_t numOfRows = 0;
971,017 ✔
3081
      pCoder->pos = rowsPos;
971,017 ✔
3082
      for (uint64_t iRow = 0; iRow < nRow; ++iRow) {
2,342,792 ✔
3083
        SRow* pRow = (SRow*)(pCoder->data + pCoder->pos);
1,372,226 ✔
3084
        pCoder->pos += pRow->len;
1,372,226 ✔
3085

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

3098
        if (rowTs < target->window.skey || rowTs > target->window.ekey) {
1,372,226 ✔
3099
          continue;
4,542 ✔
3100
        }
3101

3102
        if (numOfRows >= availableRows) {
1,367,684 ✔
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,804,367 ✔
3109
          SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, i);
7,434,400 ✔
3110
          STREAM_CHECK_NULL_GOTO(pColData, terrno);
7,435,532 ✔
3111
          if (pColData->info.colId <= -1) {
7,439,315 ✔
3112
            pColData->hasNull = true;
2,635,877 ✔
3113
            continue;
2,635,877 ✔
3114
          }
3115
          int16_t colId = 0;
4,803,438 ✔
3116
          if (sStreamReaderInfo->isVtableStream) {
4,803,184 ✔
3117
            STREAM_CHECK_RET_GOTO(getColId(submitTbData.suid, submitTbData.uid, i, sStreamReaderInfo, rsp, &colId));
448,728 ✔
3118
            ST_TASK_TLOG("%s vtable colId:%d, i:%d, uid:%" PRId64, __func__, colId, i, submitTbData.uid);
448,382 ✔
3119
          } else {
3120
            colId = pColData->info.colId;
4,354,482 ✔
3121
          }
3122

3123
          SColVal colVal = {0};
4,802,864 ✔
3124
          int32_t sourceIdx = 0;
4,803,010 ✔
3125
          while (1) {
600 ✔
3126
            if (sourceIdx >= schema->numOfCols) {
11,974,851 ✔
3127
              break;
1,950,477 ✔
3128
            }
3129
            STREAM_CHECK_RET_GOTO(tRowGet(pRow, schema, sourceIdx, &colVal));
10,024,374 ✔
3130
            if (colVal.cid == colId) {
10,026,860 ✔
3131
              break;
2,854,779 ✔
3132
            }
3133
            sourceIdx++;
7,171,841 ✔
3134
          }
3135
          if (colVal.cid == colId && COL_VAL_IS_VALUE(&colVal)) {
4,805,496 ✔
3136
            if (IS_VAR_DATA_TYPE(colVal.value.type) || colVal.value.type == TSDB_DATA_TYPE_DECIMAL) {
2,830,104 ✔
3137
              STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, blockStart + numOfRows, (const char*)colVal.value.pData,
20,622 ✔
3138
                                                     colVal.value.nData, !COL_VAL_IS_VALUE(&colVal)));
×
3139
              ST_TASK_TLOG(
20,301 ✔
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,809,482 ✔
3145
                                                  !COL_VAL_IS_VALUE(&colVal)));
3146
            }
3147
          } else {
3148
            colDataSetNULL(pColData, blockStart + numOfRows);
1,975,392 ✔
3149
          }
3150
        }
3151

3152
        numOfRows++;
1,367,233 ✔
3153
      }
3154

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

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

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

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

3188
  if (rsp->metaBlock != NULL){
1,168,070 ✔
3189
    gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
619,592 ✔
3190
    STREAM_CHECK_NULL_GOTO(gidHash, terrno);
619,409 ✔
3191
  }
3192

3193
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
2,337,875 ✔
3194
    STREAM_CHECK_RET_GOTO(scanSubmitTbData(pVnode, &decoder, sStreamReaderInfo, ranges, gidHash, rsp, ver));
1,170,186 ✔
3195
  }
3196

3197
  tEndDecode(&decoder);
1,167,689 ✔
3198

3199
  if (rsp->metaBlock != NULL){
1,168,029 ✔
3200
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
619,201 ✔
3201
    int32_t iter = 0;
619,097 ✔
3202
    void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
618,955 ✔
3203
    while (px != NULL) {
1,042,184 ✔
3204
      WalMetaResult* pMeta = (WalMetaResult*)px;
422,454 ✔
3205
      int32_t        rollupTbCount = qStreamGetGroupTableCount(sStreamReaderInfo, pMeta->id);
422,454 ✔
3206
      STREAM_CHECK_RET_GOTO(
422,529 ✔
3207
          buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver, rollupTbCount));
132 ✔
3208
      ((SSDataBlock*)rsp->metaBlock)->info.rows++;
422,028 ✔
3209
      rsp->totalRows++;
422,028 ✔
3210
      ST_TASK_DLOG("%s process meta data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
422,028 ✔
3211
            ", ver:%"PRId64, __func__, pMeta->skey, pMeta->ekey, pMeta->id, ver);
3212
      px = tSimpleHashIterate(gidHash, px, &iter);
421,954 ✔
3213
    }
3214
  }
3215
  
3216

3217
end:
1,167,718 ✔
3218
  tSimpleHashCleanup(gidHash);
1,168,925 ✔
3219
  tDecoderClear(&decoder);
1,168,144 ✔
3220
  return code;
1,168,142 ✔
3221
}
3222

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

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

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

3245
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
3246
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
1,763,010 ✔
3247
    submitTbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
329,014 ✔
3248
    STREAM_CHECK_NULL_GOTO(submitTbData.pCreateTbReq, terrno);
328,943 ✔
3249
    STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbReq(pCoder, submitTbData.pCreateTbReq));
328,943 ✔
3250
    STREAM_CHECK_RET_GOTO(processAutoCreateTableNew(sStreamReaderInfo, submitTbData.pCreateTbReq, ver));
328,431 ✔
3251
  }
3252

3253
  // submit data
3254
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
1,763,080 ✔
3255
    code = TSDB_CODE_INVALID_MSG;
×
3256
    TSDB_CHECK_CODE(code, lino, end);
×
3257
  }
3258
  if (tDecodeI64(pCoder, uid) < 0) {
1,762,367 ✔
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,762,367 ✔
3263
  STimeWindow window = {.skey = INT64_MIN, .ekey = INT64_MAX};
1,762,442 ✔
3264

3265
  if (expandRollupGids) {
1,762,868 ✔
3266
    targets = taosArrayInit(4, sizeof(SWalDataTarget));
40,793 ✔
3267
    STREAM_CHECK_NULL_GOTO(targets, terrno);
40,793 ✔
3268
    STREAM_CHECK_RET_GOTO(collectWalRollupTargets(sStreamReaderInfo, ranges, submitTbData.suid, *uid, targets));
40,793 ✔
3269
    STREAM_CHECK_CONDITION_GOTO(taosArrayGetSize(targets) == 0, TDB_CODE_SUCCESS);
40,793 ✔
3270
  } else {
3271
    STREAM_CHECK_CONDITION_GOTO(!uidInTableListSet(sStreamReaderInfo, submitTbData.suid, *uid, gid, rsp->isCalc),
1,722,075 ✔
3272
                                TDB_CODE_SUCCESS);
132 ✔
3273

3274
    if (ranges != NULL) {
929,988 ✔
3275
      void* timerange = tSimpleHashGet(ranges, gid, sizeof(*gid));
509,359 ✔
3276
      if (timerange == NULL) goto end;
509,288 ✔
3277
      int64_t* pRange = (int64_t*)timerange;
508,577 ✔
3278
      window.skey = pRange[0];
508,577 ✔
3279
      window.ekey = pRange[1];
508,577 ✔
3280
    }
3281
  }
3282

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

3288
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
970,247 ✔
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;
970,166 ✔
3330
    if (tDecodeU64v(pCoder, &nRow) < 0) {
970,274 ✔
3331
      code = TSDB_CODE_INVALID_MSG;
×
3332
      TSDB_CHECK_CODE(code, lino, end);
×
3333
    }
3334

3335
    if (expandRollupGids) {
970,274 ✔
3336
      int32_t rowsPos = pCoder->pos;
40,719 ✔
3337
      int32_t numOfTargets = taosArrayGetSize(targets);
40,719 ✔
3338
      for (int32_t i = 0; i < numOfTargets; ++i) {
82,160 ✔
3339
        SWalDataTarget* target = taosArrayGet(targets, i);
41,441 ✔
3340
        STREAM_CHECK_NULL_GOTO(target, terrno);
41,441 ✔
3341
        int32_t targetRows = 0;
41,441 ✔
3342
        pCoder->pos = rowsPos;
41,441 ✔
3343
        for (uint64_t iRow = 0; iRow < nRow; ++iRow) {
83,474 ✔
3344
          SRow* pRow = (SRow*)(pCoder->data + pCoder->pos);
42,033 ✔
3345
          pCoder->pos += pRow->len;
42,033 ✔
3346
          if ((target->window.skey != INT64_MIN || target->window.ekey != INT64_MAX) &&
42,033 ✔
3347
              (pRow->ts < target->window.skey || pRow->ts > target->window.ekey)) {
40,617 ✔
3348
            continue;
148 ✔
3349
          }
3350
          targetRows++;
41,885 ✔
3351
        }
3352
        STREAM_CHECK_RET_GOTO(addWalDataSlice(sStreamReaderInfo, rsp, *uid, target->gid, targetRows, ver));
41,441 ✔
3353
        *gid = target->gid;
41,441 ✔
3354
        *numOfRows += targetRows;
41,441 ✔
3355
      }
3356
    } else if (window.skey != INT64_MIN || window.ekey != INT64_MAX) {
929,555 ✔
3357
      for (uint64_t iRow = 0; iRow < nRow; ++iRow) {
1,343,510 ✔
3358
        SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
835,081 ✔
3359
        pCoder->pos += pRow->len;
835,081 ✔
3360
        if (pRow->ts < window.skey || pRow->ts > window.ekey) {
835,081 ✔
3361
          continue;
4,394 ✔
3362
        }
3363
        (*numOfRows)++;
830,687 ✔
3364
      }
3365
    } else {
3366
      (*numOfRows) = nRow;
421,126 ✔
3367
    }
3368
  }
3369
  
3370
end:
1,763,215 ✔
3371
  taosArrayDestroy(targets);
1,762,436 ✔
3372
  tdDestroySVCreateTbReq(submitTbData.pCreateTbReq);
1,762,136 ✔
3373
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
1,761,990 ✔
3374
  tEndDecode(pCoder);
1,761,561 ✔
3375
  return code;
1,761,614 ✔
3376
}
3377

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

3384
  tDecoderInit(&decoder, data, len);
1,759,120 ✔
3385
  if (tStartDecode(&decoder) < 0) {
1,758,552 ✔
3386
    code = TSDB_CODE_INVALID_MSG;
×
3387
    TSDB_CHECK_CODE(code, lino, end);
×
3388
  }
3389

3390
  uint64_t nSubmitTbData = 0;
1,760,982 ✔
3391
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
1,760,630 ✔
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,760,630 ✔
3396

3397
  bool expandRollupGids = isRollupMultiReader(sStreamReaderInfo);
1,760,555 ✔
3398
  for (int32_t i = 0; i < nSubmitTbData; i++) {
3,521,660 ✔
3399
    uint64_t gid = -1;
1,761,791 ✔
3400
    int64_t  uid = 0;
1,761,863 ✔
3401
    int32_t numOfRows = 0;
1,762,005 ✔
3402
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataPre(&decoder, sStreamReaderInfo, ranges, &gid, &uid, &numOfRows, rsp, ver));
1,762,289 ✔
3403
    if (numOfRows <= 0) {
1,762,254 ✔
3404
      ST_TASK_DLOG("%s no valid data uid:%" PRId64 ", gid:%" PRIu64 ", numOfRows:%d, ver:%"PRId64, __func__, uid, gid, numOfRows, ver);
792,586 ✔
3405
      continue;
792,472 ✔
3406
    }
3407
    if (!expandRollupGids) {
969,668 ✔
3408
      STREAM_CHECK_RET_GOTO(addWalDataSlice(sStreamReaderInfo, rsp, uid, gid, numOfRows, ver));
929,095 ✔
3409
    }
3410
  }
3411

3412
  tEndDecode(&decoder);
1,759,869 ✔
3413

3414
end:
1,759,377 ✔
3415
  tDecoderClear(&decoder);
1,760,016 ✔
3416
  return code;
1,760,190 ✔
3417
}
3418

3419
static void buildIndexHash(SSHashObj* indexHash, void* pTask){
151,181 ✔
3420
  void*   pe = NULL;
151,181 ✔
3421
  int32_t iter = 0;
151,181 ✔
3422
  int32_t index = 0;
151,181 ✔
3423
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
326,054 ✔
3424
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
174,795 ✔
3425
    pInfo->startRowIdx = index;
174,795 ✔
3426
    pInfo->currentRowIdx = index;
174,795 ✔
3427
    index += pInfo->numRows;
174,795 ✔
3428
    ST_TASK_DLOG("%s uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", __func__, pInfo->uid, pInfo->gId,
174,963 ✔
3429
                 pInfo->startRowIdx, pInfo->numRows);
3430
  }
3431
}
151,106 ✔
3432

3433
static void printIndexHash(SSHashObj* indexHash, void* pTask){
151,181 ✔
3434
  if (qDebugFlag & DEBUG_TRACE) {
151,181 ✔
3435
    void*   pe = NULL;
147 ✔
3436
    int32_t iter = 0;
147 ✔
3437
    while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
384 ✔
3438
      SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
159 ✔
3439
      ST_TASK_TLOG("%s uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", __func__, pInfo->uid, pInfo->gId,
327 ✔
3440
                   pInfo->startRowIdx, pInfo->numRows);
3441
    }
3442
  }
3443
}
151,181 ✔
3444

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

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

3473
  code = walReaderSeekVer(pWalReader, resultRsp->ver);
1,770,779 ✔
3474
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
1,770,329 ✔
3475
    if (resultRsp->ver < walGetFirstVer(pWalReader->pWal)) {
1,537,308 ✔
3476
      resultRsp->ver = walGetFirstVer(pWalReader->pWal);
×
3477
      resultRsp->verTime = 0;
×
3478
    } else {
3479
      resultRsp->verTime = taosGetTimestampUs();
1,537,030 ✔
3480
    }
3481
    ST_TASK_DLOG("%s scan wal end:%s",  __func__, tstrerror(code));
1,537,030 ✔
3482
    code = TSDB_CODE_SUCCESS;
1,537,483 ✔
3483
    goto end;
1,537,483 ✔
3484
  }
3485
  STREAM_CHECK_RET_GOTO(code);
233,021 ✔
3486

3487
  while (1) {
1,296,101 ✔
3488
    code = walNextValidMsg(pWalReader, true);
1,528,852 ✔
3489
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
1,527,822 ✔
3490
      resultRsp->verTime = taosGetTimestampUs();
231,309 ✔
3491
      ST_TASK_DLOG("%s scan wal end:%s", __func__, tstrerror(code));
231,309 ✔
3492
      code = TSDB_CODE_SUCCESS;
231,309 ✔
3493
      goto end;
231,309 ✔
3494
    }
3495
    STREAM_CHECK_RET_GOTO(code);
1,297,114 ✔
3496
    SWalCont* wCont = &pWalReader->pHead->head;
1,297,114 ✔
3497
    if (endVer > 0 && wCont->version >= endVer) {
1,297,114 ✔
3498
      resultRsp->ver = endVer;
2,048 ✔
3499
      break;
2,048 ✔
3500
    }
3501
    resultRsp->ver = pWalReader->curVersion;
1,295,066 ✔
3502
    resultRsp->verTime = wCont->ingestTs;
1,295,066 ✔
3503
    void*   data = POINTER_SHIFT(walContBody(wCont), sizeof(SMsgHead));
1,295,066 ✔
3504
    int32_t len = walContBodyLen(wCont) - sizeof(SMsgHead);
1,295,066 ✔
3505
    int64_t ver = wCont->version;
1,295,066 ✔
3506
    ST_TASK_DLOG("%s scan wal ver:%" PRId64 ", type:%s, deleteData:%d, deleteTb:%d, msg len:%d", __func__,
1,295,066 ✔
3507
      ver, TMSG_INFO(wCont->msgType), sStreamReaderInfo->deleteReCalc, sStreamReaderInfo->deleteOutTbl, len);
3508
    if (wCont->msgType == TDMT_VND_SUBMIT) {
1,295,406 ✔
3509
      // return when getting data if there are meta data in vtable scan
3510
      if (shouldStopBeforeSubmitAfterTableChange(sStreamReaderInfo, resultRsp)) {
1,210,856 ✔
3511
        resultRsp->ver--;
×
3512
        break;
×
3513
      }
3514
      data = POINTER_SHIFT(walContBody(wCont), sizeof(SSubmitReq2Msg));
1,210,790 ✔
3515
      len = walContBodyLen(wCont) - sizeof(SSubmitReq2Msg);
1,210,790 ✔
3516
      STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, data, len, NULL, resultRsp, ver));
1,210,790 ✔
3517
    } else {
3518
      STREAM_CHECK_RET_GOTO(processMeta(wCont->msgType, sStreamReaderInfo, data, len, resultRsp, ver));
84,550 ✔
3519
    }
3520

3521
    ST_TASK_DLOG("%s scan wal next ver:%" PRId64 ", totalRows:%d", __func__, resultRsp->ver, resultRsp->totalRows);
1,295,334 ✔
3522
    if (resultRsp->totalRows >= STREAM_RETURN_ROWS_NUM || resultRsp->needReturn) {
1,295,192 ✔
3523
      break;
3524
    }
3525
  }
3526
  
3527
end:
×
3528
  STREAM_PRINT_LOG_END(code, lino);
1,770,201 ✔
3529
  return code;
1,770,911 ✔
3530
}
3531

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

3538
  for(int32_t i = 0; i < taosArrayGetSize(versions); i++) {
811,866 ✔
3539
    int64_t *ver = taosArrayGet(versions, i);
548,696 ✔
3540
    if (ver == NULL) continue;
548,696 ✔
3541

3542
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, *ver));
548,696 ✔
3543
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
548,832 ✔
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));
548,832 ✔
3549

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

3554
    STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, pBody, bodyLen, ranges, rsp, *ver));
548,907 ✔
3555
  }
3556
  
3557
end:
263,088 ✔
3558
  return code;
263,088 ✔
3559
}
3560

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

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

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

3573
end:
151,106 ✔
3574
  colDataDestroy(pRet);
151,106 ✔
3575
  taosMemoryFree(pRet);
151,106 ✔
3576
  return code;
151,181 ✔
3577
}
3578

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

3593
  buildIndexHash(resultRsp->indexHash, pTask);
53,729 ✔
3594
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(((SSDataBlock*)resultRsp->dataBlock), resultRsp->totalRows));
53,654 ✔
3595
  while(lastVer < resultRsp->ver) {
686,010 ✔
3596
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, lastVer++));
632,867 ✔
3597
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
632,932 ✔
3598
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
13,699 ✔
3599
      continue;
13,699 ✔
3600
    }
3601
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
619,233 ✔
3602
    SWalCont* wCont = &pWalReader->pHead->head;
618,015 ✔
3603
    void*   pBody = POINTER_SHIFT(walContBody(wCont), sizeof(SSubmitReq2Msg));
618,015 ✔
3604
    int32_t bodyLen = walContBodyLen(wCont) - sizeof(SSubmitReq2Msg);
618,015 ✔
3605
    ST_TASK_DLOG("process wal ver:%" PRId64 ", type:%d, bodyLen:%d", wCont->version, wCont->msgType, bodyLen);
618,015 ✔
3606
    STREAM_CHECK_RET_GOTO(scanSubmitData(pVnode, sStreamReaderInfo, pBody, bodyLen, NULL, resultRsp, wCont->version));
619,051 ✔
3607
  }
3608

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

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

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

3625
  void* pTask = sStreamReaderInfo->pTask;
263,100 ✔
3626
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
263,100 ✔
3627
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
262,974 ✔
3628
  
3629
  if (taosArrayGetSize(versions) > 0) {
262,974 ✔
3630
    rsp->ver = *(int64_t*)taosArrayGetLast(versions);
97,452 ✔
3631
  }
3632
  
3633
  STREAM_CHECK_RET_GOTO(prepareIndexData(pWalReader, sStreamReaderInfo, versions, ranges, rsp));
263,044 ✔
3634
  STREAM_CHECK_CONDITION_GOTO(rsp->totalRows == 0, TDB_CODE_SUCCESS);
263,022 ✔
3635

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

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

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

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

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

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

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

3680
  SSchemaWrapper* sSchemaWrapper = NULL;
14,917 ✔
3681
  if (metaReader.me.type == TD_CHILD_TABLE) {
14,917 ✔
3682
    int64_t suid = metaReader.me.ctbEntry.suid;
14,917 ✔
3683
    tDecoderClear(&metaReader.coder);
14,917 ✔
3684
    STREAM_CHECK_RET_GOTO(api->metaReaderFn.getTableEntryByUid(&metaReader, suid));
14,917 ✔
3685
    sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
14,917 ✔
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++) {
77,078 ✔
3693
    SSchema* s = sSchemaWrapper->pSchema + j;
62,161 ✔
3694
    STREAM_CHECK_NULL_GOTO(taosArrayPush(*schemas, s), terrno);
124,322 ✔
3695
  }
3696

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

3707
static int32_t shrinkScheams(SArray* cols, SArray* schemas) {
14,917 ✔
3708
  int32_t code = 0;
14,917 ✔
3709
  int32_t lino = 0;
14,917 ✔
3710
  size_t  schemaLen = taosArrayGetSize(schemas);
14,917 ✔
3711
  STREAM_CHECK_RET_GOTO(taosArrayEnsureCap(schemas, schemaLen + taosArrayGetSize(cols)));
14,917 ✔
3712
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
46,569 ✔
3713
    col_id_t* id = taosArrayGet(cols, i);
31,652 ✔
3714
    STREAM_CHECK_NULL_GOTO(id, terrno);
31,652 ✔
3715
    for (size_t i = 0; i < schemaLen; i++) {
69,220 ✔
3716
      SSchema* s = taosArrayGet(schemas, i);
69,220 ✔
3717
      STREAM_CHECK_NULL_GOTO(s, terrno);
69,220 ✔
3718
      if (*id == s->colId) {
69,220 ✔
3719
        STREAM_CHECK_NULL_GOTO(taosArrayPush(schemas, s), terrno);
31,652 ✔
3720
        break;
31,652 ✔
3721
      }
3722
    }
3723
  }
3724
  taosArrayPopFrontBatch(schemas, schemaLen);
14,917 ✔
3725

3726
end:
14,917 ✔
3727
  return code;
14,917 ✔
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,
377,570 ✔
3854
                                    STimeRangeNode* node, SReadHandle* handle, bool isExtWin, int32_t actualNodeId) {
3855
  int32_t code = 0;
377,570 ✔
3856
  int32_t lino = 0;
377,570 ✔
3857
  void* pTask = sStreamReaderCalcInfo->pTask;
377,570 ✔
3858
  STimeWindow* pWin = isExtWin ? &handle->extWinRange : &handle->winRange;
377,570 ✔
3859
  bool* pValid = isExtWin ? &handle->extWinRangeValid : &handle->winRangeValid;
377,570 ✔
3860

3861
  if (req->pStRtFuncInfo->withExternalWindow) {
377,570 ✔
3862
    sStreamReaderCalcInfo->tmpRtFuncInfo.triggerType = req->pStRtFuncInfo->triggerType;
188,487 ✔
3863
    sStreamReaderCalcInfo->tmpRtFuncInfo.isWindowTrigger = req->pStRtFuncInfo->isWindowTrigger;
188,487 ✔
3864
    sStreamReaderCalcInfo->tmpRtFuncInfo.precision = req->pStRtFuncInfo->precision;
188,487 ✔
3865

3866
    SSTriggerCalcParam* pFirst = NULL;
188,487 ✔
3867
    SSTriggerCalcParam* pLast = NULL;
188,487 ✔
3868
    int32_t             readInfoIndex = -1;
188,487 ✔
3869
    int32_t             firstParamIndex = 0;
188,487 ✔
3870
    int32_t             lastParamIndex = 0;
188,487 ✔
3871
    if (req->pStRtFuncInfo->isMultiGroupCalc) {
188,487 ✔
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);
188,487 ✔
3881
      pLast = taosArrayGetLast(req->pStRtFuncInfo->pStreamPesudoFuncVals);
188,487 ✔
3882
      lastParamIndex = taosArrayGetSize(req->pStRtFuncInfo->pStreamPesudoFuncVals) - 1;
188,487 ✔
3883
      STREAM_CHECK_NULL_GOTO(pFirst, terrno);
188,487 ✔
3884
      STREAM_CHECK_NULL_GOTO(pLast, terrno);
188,487 ✔
3885
    }
3886

3887
    if (!node->needCalc) {
188,487 ✔
3888
      pWin->skey = pFirst->wstart;
179,852 ✔
3889
      pWin->ekey = pLast->wend;
179,852 ✔
3890
      *pValid = true;
179,852 ✔
3891
      if (req->pStRtFuncInfo->triggerType == STREAM_TRIGGER_SLIDING) {
179,852 ✔
3892
        pWin->ekey--;
173,549 ✔
3893
      }
3894
    } else {
3895
      SSTriggerCalcParam* pTmp = taosArrayGet(sStreamReaderCalcInfo->tmpRtFuncInfo.pStreamPesudoFuncVals, 0);
8,635 ✔
3896
      STREAM_CHECK_NULL_GOTO(pTmp, terrno);
8,635 ✔
3897
      STREAM_CHECK_RET_GOTO(stProjectReaderCalcContext(req->pStRtFuncInfo, actualNodeId, readInfoIndex, firstParamIndex,
8,635 ✔
3898
                                                       &sStreamReaderCalcInfo->tmpRtFuncInfo));
12 ✔
3899
      memcpy(pTmp, pFirst, sizeof(*pTmp));
8,635 ✔
3900

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

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

3910
        if (*pValid) {
8,635 ✔
3911
          pWin->skey = skey;
8,635 ✔
3912
        }
3913
      }
3914
      pWin->ekey--;
8,635 ✔
3915
    }
3916
  } else {
3917
    if (!node->needCalc) {
189,083 ✔
3918
      SSTriggerCalcParam* pCurr = taosArrayGet(req->pStRtFuncInfo->pStreamPesudoFuncVals, req->pStRtFuncInfo->curIdx);
182,676 ✔
3919
      pWin->skey = pCurr->wstart;
182,676 ✔
3920
      pWin->ekey = pCurr->wend;
182,676 ✔
3921
      *pValid = true;
182,676 ✔
3922
      if (req->pStRtFuncInfo->triggerType == STREAM_TRIGGER_SLIDING) {
182,676 ✔
3923
        pWin->ekey--;
23,012 ✔
3924
      }
3925
    } else {
3926
      STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, req->pStRtFuncInfo, pWin, pValid, 3));
6,407 ✔
3927
      pWin->ekey--;
6,407 ✔
3928
    }
3929
  }
3930

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

3934
end:
1,408 ✔
3935

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

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

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

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

3969
  tsRsp->tsInfo = taosArrayInit(pResBlock->info.rows, sizeof(STsInfo));
55,330 ✔
3970
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
55,330 ✔
3971
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
55,330 ✔
3972
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
55,330 ✔
3973
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
156,549 ✔
3974
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
202,438 ✔
3975
      continue;
×
3976
    }
3977
    STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
101,219 ✔
3978
    STREAM_CHECK_NULL_GOTO(tsInfo, terrno)
101,219 ✔
3979
    if (order == TSDB_ORDER_ASC) {
101,219 ✔
3980
      tsInfo->ts = INT64_MAX;
52,084 ✔
3981
    } else {
3982
      tsInfo->ts = INT64_MIN;
49,135 ✔
3983
    }
3984
    int64_t ts = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
101,219 ✔
3985
    if (order == TSDB_ORDER_ASC && ts < tsInfo->ts) {
101,219 ✔
3986
      tsInfo->ts = ts;
52,084 ✔
3987
    } else if (order == TSDB_ORDER_DESC && ts > tsInfo->ts) {
49,135 ✔
3988
      tsInfo->ts = ts;
49,135 ✔
3989
    }
3990
    tsInfo->gId = *(int64_t*)colDataGetNumData(pColInfoDataUid, j);
101,219 ✔
3991
    ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
101,219 ✔
3992
  }
3993

3994
end:
55,330 ✔
3995
  return code;
55,330 ✔
3996
}
3997

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

4004
  tsRsp->tsInfo = taosArrayInit(1, sizeof(STsInfo));
30,556 ✔
4005
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
30,556 ✔
4006
  STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
30,556 ✔
4007
  STREAM_CHECK_NULL_GOTO(tsInfo, terrno)
30,485 ✔
4008
  if (order == TSDB_ORDER_ASC) {
30,485 ✔
4009
    tsInfo->ts = INT64_MAX;
24,142 ✔
4010
  } else {
4011
    tsInfo->ts = INT64_MIN;
6,343 ✔
4012
  }
4013

4014
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
30,485 ✔
4015
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
30,414 ✔
4016
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
67,510 ✔
4017
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
74,192 ✔
4018
      continue;
×
4019
    }
4020
    int64_t ts = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
37,167 ✔
4021
    if (order == TSDB_ORDER_ASC && ts < tsInfo->ts) {
37,167 ✔
4022
      tsInfo->ts = ts;
24,142 ✔
4023
    } else if (order == TSDB_ORDER_DESC && ts > tsInfo->ts) {
13,025 ✔
4024
      tsInfo->ts = ts;
6,343 ✔
4025
    }
4026
  }
4027
  if (gid != 0) {
30,414 ✔
4028
    tsInfo->gId = gid;
13,911 ✔
4029
  } else {
4030
    int64_t uid = *(int64_t*)colDataGetNumData(pColInfoDataUid, 0);
16,503 ✔
4031
    tsInfo->gId = qStreamGetGroupIdFromSet(sStreamReaderInfo, uid);
16,503 ✔
4032
  }
4033
  ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
30,485 ✔
4034

4035
end:
9,187 ✔
4036
  return code;
30,414 ✔
4037
}
4038

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

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

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

4068
    int64_t ts = (order == TSDB_ORDER_ASC) ? INT64_MAX : INT64_MIN;
21,274 ✔
4069
    bool    hasTs = false;
21,274 ✔
4070
    for (int32_t i = 0; i < pNum; i++) {
42,916 ✔
4071
      int64_t  uid = pList[i].uid;
21,642 ✔
4072
      int64_t* rowTs = tSimpleHashGet(uidTsHash, &uid, LONG_BYTES);
21,642 ✔
4073
      if (rowTs == NULL) {
21,642 ✔
4074
        continue;
188 ✔
4075
      }
4076
      if (!hasTs || (order == TSDB_ORDER_ASC && *rowTs < ts) || (order == TSDB_ORDER_DESC && *rowTs > ts)) {
21,454 ✔
4077
        ts = *rowTs;
21,222 ✔
4078
        hasTs = true;
21,222 ✔
4079
      }
4080
    }
4081
    if (!hasTs) {
21,274 ✔
4082
      taosMemoryFreeClear(pList);
52 ✔
4083
      continue;
52 ✔
4084
    }
4085

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

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

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

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

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

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

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

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

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

4166
end:
604 ✔
4167
  qStreamDestroyTableInfo(&tableInfo);
604 ✔
4168
  taosMemoryFreeClear(pList);
604 ✔
4169
  tSimpleHashCleanup(uidTsHash);
604 ✔
4170
  return code;
604 ✔
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) {
126,795 ✔
4176
  int32_t code = 0;
126,795 ✔
4177
  int32_t lino = 0;
126,795 ✔
4178

4179
  stDebug("%s getAllTs enter: pNum:%d suid:%"PRId64" order:%d skey:%"PRId64" ekey:%"PRId64" verRange:[%"PRId64",%"PRId64"]",
126,795 ✔
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++) {
376,391 ✔
4184
    stDebug("%s getAllTs table[%d]: uid:%"PRId64, pTaskInner->idStr, i, pList[i].uid);
249,596 ✔
4185
  }
4186

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

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

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

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

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

4217
  while (true) {
11,696 ✔
4218
    int32_t        pNum = 0;
29,267 ✔
4219
    int64_t        suid = 0;
29,267 ✔
4220
    STREAM_CHECK_RET_GOTO(qStreamIterTableList(&tableInfo, &pList, &pNum, &suid));
29,197 ✔
4221
    if(pNum == 0) break;
29,267 ✔
4222
    pTaskInner->options->suid = suid;
11,696 ✔
4223
    STREAM_CHECK_RET_GOTO(getAllTs(pVnode, pResBlock, pTaskInner, pList, pNum));
11,696 ✔
4224
    taosMemoryFreeClear(pList);
11,696 ✔
4225
  }
4226

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

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

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

4244
  SSDataBlock*  pResBlock = NULL;
112,998 ✔
4245

4246
  int32_t        pNum = 0;
112,998 ✔
4247
  int64_t        suid = 0;
112,998 ✔
4248
  STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, 0, &pList, &pNum));
112,998 ✔
4249
  ST_TASK_DLOG("vgId:%d %s qStreamGetTableList returned pNum:%d", TD_VID(pVnode), __func__, pNum);
113,068 ✔
4250
  STREAM_CHECK_CONDITION_GOTO(pNum == 0, TSDB_CODE_SUCCESS);
113,068 ✔
4251
  STREAM_CHECK_RET_GOTO(createDataBlockTsUid(&pResBlock, pNum));
101,117 ✔
4252

4253
  pTaskInner->options->suid = sStreamReaderInfo->suid;
101,117 ✔
4254
  ST_TASK_DLOG("vgId:%d %s calling getAllTs: suid:%"PRId64" order:%d skey:%"PRId64" ekey:%"PRId64" ver:%"PRId64,
101,117 ✔
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));
101,117 ✔
4258
  ST_TASK_DLOG("vgId:%d %s getAllTs done: pResBlock rows:%"PRId64, TD_VID(pVnode), __func__, pResBlock->info.rows);
101,047 ✔
4259
  int32_t order = pTaskInner->options->order;
101,117 ✔
4260
  if (pResBlock->info.rows == 0 && sStreamReaderInfo->groupByTbname) {
101,117 ✔
4261
    tsRsp->tsInfo = taosArrayInit(pNum, sizeof(STsInfo));
13,645 ✔
4262
    STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
13,575 ✔
4263
    for (int32_t i = 0; i < pNum; i++) {
48,546 ✔
4264
      STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
34,971 ✔
4265
      STREAM_CHECK_NULL_GOTO(tsInfo, terrno);
34,971 ✔
4266
      tsInfo->gId = pList[i].uid;
34,971 ✔
4267
      tsInfo->ts = 0;
34,971 ✔
4268
      ST_TASK_DLOG("%s no data but return gId (uid):%" PRIu64 " for tbname partition", __func__, tsInfo->gId);
34,971 ✔
4269
    }
4270
    goto end;
13,575 ✔
4271
  }
4272

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

4275
  if (sStreamReaderInfo->groupByTbname) {
63,887 ✔
4276
    STREAM_CHECK_RET_GOTO(processTsOutPutAllTables(sStreamReaderInfo, tsRsp, pResBlock, order));
37,759 ✔
4277
  } else if (isRollupMultiReader(sStreamReaderInfo)) {
26,128 ✔
4278
    STREAM_CHECK_RET_GOTO(processTsOutPutRollupGroups(sStreamReaderInfo, tsRsp, pResBlock, order));
604 ✔
4279
  } else if (sStreamReaderInfo->partitionCols == NULL) {
25,524 ✔
4280
    STREAM_CHECK_RET_GOTO(processTsOutPutOneGroup(sStreamReaderInfo, tsRsp, pResBlock, order, 0));
16,574 ✔
4281
  } else {
4282
    STREAM_CHECK_RET_GOTO(processTsOutPutAllGroups(sStreamReaderInfo, tsRsp, pResBlock, order));
8,950 ✔
4283
  }
4284
end:
112,950 ✔
4285
  blockDataDestroy(pResBlock);
113,068 ✔
4286
  taosMemoryFreeClear(pList);
112,998 ✔
4287
  STREAM_PRINT_LOG_END_WITHID(code, lino);
112,928 ✔
4288
  return code;
112,928 ✔
4289
}
4290

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

4300
  int32_t        pNum = 0;
17,639 ✔
4301
  STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, gid, &pList, &pNum));
17,639 ✔
4302
  STREAM_CHECK_CONDITION_GOTO(pNum == 0, TSDB_CODE_SUCCESS);
17,639 ✔
4303
  STREAM_CHECK_RET_GOTO(createDataBlockTsUid(&pResBlock, pNum));
13,982 ✔
4304

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

4310
  STREAM_CHECK_RET_GOTO(processTsOutPutOneGroup(sStreamReaderInfo, tsRsp, pResBlock, order, gid));
13,982 ✔
4311
end:
17,568 ✔
4312
  blockDataDestroy(pResBlock);
17,568 ✔
4313
  taosMemoryFreeClear(pList);
17,426 ✔
4314
  STREAM_PRINT_LOG_END_WITHID(code, lino);
17,497 ✔
4315
  return code;
17,497 ✔
4316
}
4317

4318
static int32_t processTs(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
130,639 ✔
4319
                                  SStreamReaderTaskInner* pTaskInner) {
4320
  if (sStreamReaderInfo->isVtableStream) {
130,639 ✔
4321
    return processTsVTable(pVnode, tsRsp, sStreamReaderInfo, pTaskInner);
17,571 ✔
4322
  }
4323

4324
  return processTsNonVTable(pVnode, tsRsp, sStreamReaderInfo, pTaskInner);
113,068 ✔
4325
}
4326

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

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

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

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

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

4365
  void* pTask = sStreamReaderInfo->pTask;
86,424 ✔
4366

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

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

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

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

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

4396
  void* pTask = sStreamReaderInfo->pTask;
61,784 ✔
4397
  ST_TASK_DLOG("vgId:%d %s start, startTime:%"PRId64" ver:%"PRId64" gid:%"PRId64
61,784 ✔
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;
61,784 ✔
4403

4404
  tsRsp.ver = pVnode->state.applied;
61,784 ✔
4405

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

4409
  if (req->firstTsReq.gid != 0) {
61,784 ✔
4410
    STREAM_CHECK_RET_GOTO(processTsOnce(pVnode, &tsRsp, sStreamReaderInfo, pTaskInner, req->firstTsReq.gid));
17,639 ✔
4411
  } else {
4412
    STREAM_CHECK_RET_GOTO(processTs(pVnode, &tsRsp, sStreamReaderInfo, pTaskInner));
44,145 ✔
4413
  }
4414

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

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

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

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

4449
  if (req->base.type == STRIGGER_PULL_TSDB_META) {
49,585 ✔
4450
    STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, req->tsdbMetaReq.gid, &pList, &pNum));
49,585 ✔
4451
    STableKeyInfo* pCreateList = pList;
49,585 ✔
4452
    int32_t        createNum = pNum;
49,585 ✔
4453
    if (isRollupMultiReader(sStreamReaderInfo)) {
49,585 ✔
4454
      if (req->tsdbMetaReq.gid == 0){
226 ✔
4455
        STREAM_CHECK_RET_GOTO(buildRollupMetaSnapshot(pList, pNum, &pReadList, &readNum, &pRollupMetaByUid, &pRollupMetaCount));
226 ✔
4456
        pCreateList = pReadList;
226 ✔
4457
        createNum = readNum;
226 ✔
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, 
49,585 ✔
4463
                          sStreamReaderInfo->tsSchemas, true, NULL);
×
4464
    STREAM_CHECK_RET_GOTO(
49,585 ✔
4465
        createStreamTask(pVnode, &options, &pTaskInner, NULL, pCreateList, createNum, &sStreamReaderInfo->storageApi));
×
4466
    if (isRollupMultiReader(sStreamReaderInfo)) {
49,585 ✔
4467
      pTaskInner->pRollupMetaByUid = pRollupMetaByUid;
226 ✔
4468
      pRollupMetaByUid = NULL;
226 ✔
4469
      pTaskInner->pRollupMetaCount = pRollupMetaCount;
226 ✔
4470
      pRollupMetaCount = NULL;
226 ✔
4471
    }
4472
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
49,585 ✔
4473

4474
    STREAM_CHECK_RET_GOTO(
49,520 ✔
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);
49,585 ✔
4485
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pTaskInner->pResBlockDst, STREAM_RETURN_ROWS_NUM));
49,585 ✔
4486
  bool hasNext = true;
49,585 ✔
4487
  while (true) {
×
4488
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
169,473 ✔
4489
    if (!hasNext) {
169,473 ✔
4490
      break;
49,585 ✔
4491
    }
4492
    pTaskInner->storageApi->tsdReader.tsdReaderReleaseDataBlock(pTaskInner->pReader);
119,888 ✔
4493
    if (isRollupMultiReader(sStreamReaderInfo)) {
119,888 ✔
4494
      if (req->tsdbMetaReq.gid != 0) {
226 ✔
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);
226 ✔
4502
        STREAM_CHECK_NULL_GOTO(ppEntries, TSDB_CODE_INTERNAL_ERROR);
226 ✔
4503
        int32_t numOfEntries = taosArrayGetSize(*ppEntries);
226 ✔
4504
        for (int32_t i = 0; i < numOfEntries; ++i) {
829 ✔
4505
          STableKeyInfo* pInfo = taosArrayGet(*ppEntries, i);
603 ✔
4506
          STREAM_CHECK_NULL_GOTO(pInfo, TSDB_CODE_INTERNAL_ERROR);
603 ✔
4507
          int32_t rollupTbCount = getRollupMetaCount(pTaskInner->pRollupMetaCount, pInfo->groupId);
603 ✔
4508
          STREAM_CHECK_CONDITION_GOTO(rollupTbCount <= 0, TSDB_CODE_INTERNAL_ERROR);
603 ✔
4509
          STREAM_CHECK_RET_GOTO(appendTsdbMetaRow(sStreamReaderInfo, pTaskInner->pResBlockDst, pTaskInner->pResBlock,
603 ✔
4510
                                                  pInfo->groupId, rollupTbCount, TD_VID(pVnode)));
×
4511
        }
4512
      }
4513
    } else {
4514
      uint64_t gid = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
119,662 ✔
4515
      STREAM_CHECK_RET_GOTO(appendTsdbMetaRow(sStreamReaderInfo, pTaskInner->pResBlockDst, pTaskInner->pResBlock, gid,
119,662 ✔
4516
                                              0, TD_VID(pVnode)));
×
4517
    }
4518

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

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

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

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

4551
  void* pTask = sStreamReaderInfo->pTask;
104,306 ✔
4552
  ST_TASK_DLOG("vgId:%d %s start, ver:%" PRId64 ",skey:%" PRId64 ",ekey:%" PRId64 ",uid:%" PRId64 ",suid:%" PRId64,
104,306 ✔
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;
104,306 ✔
4557
  STableKeyInfo pList = {.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, req->tsdbTsDataReq.uid),
104,306 ✔
4558
                         .uid = req->tsdbTsDataReq.uid};
104,306 ✔
4559
  STREAM_CHECK_CONDITION_GOTO(pList.groupId == -1, TSDB_CODE_INVALID_PARA);
104,306 ✔
4560
  BUILD_OPTION(options, getSuid(sStreamReaderInfo, &pList), req->tsdbTsDataReq.ver, TSDB_ORDER_ASC,
104,306 ✔
4561
               req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey, sStreamReaderInfo->triggerCols, false, NULL);
×
4562
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, &pList,
104,306 ✔
4563
                                         pNum, &sStreamReaderInfo->storageApi));
×
4564
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
104,306 ✔
4565
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->tsBlock, false, &pBlockRes));
104,306 ✔
4566

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

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

4581
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderInfo->pFilterInfo, NULL));
104,306 ✔
4582
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
104,306 ✔
4583
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
104,306 ✔
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);
104,306 ✔
4590
  SSDataBlock* pRspBlock = pTaskInner->pResBlockDst;
104,306 ✔
4591
  bool         requiresAncestorContext =
104,306 ✔
4592
      (sStreamReaderInfo->triggerAst != NULL && sStreamReaderInfo->triggerAst->requiresAncestorContext) ||
206,840 ✔
4593
      (sStreamReaderInfo->calcAst != NULL && sStreamReaderInfo->calcAst->requiresAncestorContext);
102,534 ✔
4594
  if (!requiresAncestorContext) {
104,306 ✔
4595
    blockDataTransform(pBlockRes, pTaskInner->pResBlockDst);
102,534 ✔
4596
    pRspBlock = pBlockRes;
102,534 ✔
4597
  }
4598
  STREAM_CHECK_RET_GOTO(buildRsp(pRspBlock, &buf, &size));
104,306 ✔
4599
  addReaderResponseBlock(pRspStats, pRspBlock);
104,306 ✔
4600

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

4606
  releaseStreamTask(&pTaskInner);
104,306 ✔
4607
  return code;
104,306 ✔
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,
42,492 ✔
4661
                                                    SStreamTriggerReaderInfo*   sStreamReaderInfo,
4662
                                                    SStreamReaderResponseStats* pRspStats) {
4663
  int32_t code = 0;
42,492 ✔
4664
  int32_t lino = 0;
42,492 ✔
4665
  void*   buf = NULL;
42,492 ✔
4666
  size_t  size = 0;
42,492 ✔
4667
  STableKeyInfo* pList = NULL;
42,492 ✔
4668
  SArray*        pResList = NULL;
42,492 ✔
4669
  SSDataBlock*   pBlockTmp = NULL;
42,492 ✔
4670

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

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

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

4705
    SSDataBlock* pBlock = NULL;
25,086 ✔
4706
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
25,086 ✔
4707
    if (pBlock != NULL && pBlock->info.rows > 0) {
25,086 ✔
4708
      STREAM_CHECK_RET_GOTO(
25,086 ✔
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));
25,086 ✔
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);
25,086 ✔
4714
    STREAM_CHECK_RET_GOTO(createOneDataBlock(pBlock, true, &pBlockTmp));
25,086 ✔
4715
    STREAM_CHECK_NULL_GOTO(taosArrayPush(pResList, &pBlockTmp), terrno);
25,086 ✔
4716
    totalRows += blockDataGetNumOfRows(pBlockTmp);
25,086 ✔
4717
    pBlockTmp = NULL;
25,086 ✔
4718

4719
    ST_TASK_DLOG("vgId:%d %s get skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
25,086 ✔
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
25,086 ✔
4723
      break;
×
4724
    }
4725
  }
4726

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

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

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

4752

4753
  void* pTask = sStreamReaderInfo->pTask;
1,961,322 ✔
4754
  ST_TASK_DLOG("vgId:%d %s start, skey:%"PRId64",ekey:%"PRId64",gid:%"PRId64",ver:%"PRId64, TD_VID(pVnode), __func__, 
1,961,322 ✔
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);
1,961,438 ✔
4758

4759
  SStreamReaderTaskInner* pTaskInner = NULL;
1,961,438 ✔
4760
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_CALC_DATA);
1,961,438 ✔
4761

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

4769
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
1,961,438 ✔
4770
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
1,961,438 ✔
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);
1,961,438 ✔
4779
  bool hasNext = true;
1,961,438 ✔
4780
  while (1) {
330,332 ✔
4781
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
2,291,707 ✔
4782
    if (!hasNext) {
2,292,002 ✔
4783
      break;
1,961,438 ✔
4784
    }
4785
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
330,564 ✔
4786

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

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

4805
  if (!hasNext) {
1,961,438 ✔
4806
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
1,961,438 ✔
4807
  }
4808

4809
end:
1,961,554 ✔
4810
  STREAM_PRINT_LOG_END_WITHID(code, lino);
1,961,438 ✔
4811
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
1,961,438 ✔
4812
  blockDataDestroy(pBlockRes);
1,961,312 ✔
4813
  taosMemoryFree(pList);
1,961,438 ✔
4814
  return code;
1,961,438 ✔
4815
}
4816

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

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

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

4860
    taosArraySort(schemas, sortSSchema);
14,917 ✔
4861
    BUILD_OPTION(options, req->tsdbDataReq.suid, req->tsdbDataReq.ver, req->tsdbDataReq.order, req->tsdbDataReq.skey,
14,917 ✔
4862
                    req->tsdbDataReq.ekey, schemas, true, &slotIdList);
×
4863
    STableKeyInfo       keyInfo = {.uid = req->tsdbDataReq.uid, .groupId = 0};
14,917 ✔
4864
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, pBlockRes, &keyInfo, 1, &sStreamReaderInfo->storageApi));
14,917 ✔
4865
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
14,917 ✔
4866
    pTaskInner->pResBlockDst = pBlockRes;
14,917 ✔
4867
    pBlockRes = NULL;
14,917 ✔
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);
14,917 ✔
4876
  bool hasNext = true;
14,917 ✔
4877
  while (1) {
14,917 ✔
4878
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
29,834 ✔
4879
    if (!hasNext) {
29,834 ✔
4880
      break;
14,917 ✔
4881
    }
4882

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

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

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

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

4921
  if (sStreamReaderInfo->metaBlock == NULL) {
785,640 ✔
4922
    STREAM_CHECK_RET_GOTO(
33,906 ✔
4923
        createBlockForWalMetaNew((SSDataBlock**)&sStreamReaderInfo->metaBlock,
4924
                                 isRollupMultiReader(sStreamReaderInfo)));
36 ✔
4925
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(sStreamReaderInfo->metaBlock, STREAM_RETURN_ROWS_NUM));
33,906 ✔
4926
  }
4927
  blockDataEmpty(sStreamReaderInfo->metaBlock);
785,640 ✔
4928
  resultRsp.metaBlock = sStreamReaderInfo->metaBlock;
785,205 ✔
4929
  resultRsp.ver = req->walMetaNewReq.lastVer;
785,205 ✔
4930
  {
4931
    int32_t hookRc = streamMaybeRecheckVTableCache(pVnode, sStreamReaderInfo, resultRsp.ver, &resultRsp);
785,205 ✔
4932
    ST_TASK_DLOG("vgId:%d %s hook rc=0x%x ver=%" PRId64, TD_VID(pVnode), __func__, hookRc, resultRsp.ver);
785,499 ✔
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; }
785,289 ✔
4937
  }
4938
  STREAM_CHECK_RET_GOTO(processWalVerMetaNew(pVnode, &resultRsp, sStreamReaderInfo, req->walMetaNewReq.ctime));
784,941 ✔
4939

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

4949
end:
784,945 ✔
4950
  if (code == 0 && resultRsp.totalRows == 0) {
785,555 ✔
4951
    code = TSDB_CODE_STREAM_NO_DATA;
757,806 ✔
4952
    size = sizeof(int64_t) * 2;
757,806 ✔
4953
    buf = rpcMallocCont(size);
757,806 ✔
4954
    *(int64_t*)buf = resultRsp.ver;
757,820 ✔
4955
    *(((int64_t*)buf) + 1) = resultRsp.verTime;
757,820 ✔
4956
  }
4957
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
785,569 ✔
4958
  if (code == TSDB_CODE_STREAM_NO_DATA){
785,542 ✔
4959
    code = 0;
757,253 ✔
4960
  }
4961
  STREAM_PRINT_LOG_END_WITHID(code, lino);
785,542 ✔
4962
  blockDataDestroy(resultRsp.deleteBlock);
785,664 ✔
4963
  blockDataDestroy(resultRsp.tableBlock);
785,509 ✔
4964

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

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

4985
  if (sStreamReaderInfo->metaBlock == NULL) {
1,770,876 ✔
4986
    STREAM_CHECK_RET_GOTO(
56,163 ✔
4987
        createBlockForWalMetaNew((SSDataBlock**)&sStreamReaderInfo->metaBlock,
4988
                                 isRollupMultiReader(sStreamReaderInfo)));
54 ✔
4989
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(sStreamReaderInfo->metaBlock, STREAM_RETURN_ROWS_NUM));
56,163 ✔
4990
  }
4991

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

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

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

5020
end:
1,769,107 ✔
5021
  if (code == 0 && resultRsp.totalRows == 0) {
1,770,688 ✔
5022
    code = TSDB_CODE_STREAM_NO_DATA;
1,716,953 ✔
5023
    size = sizeof(int64_t) * 2;
1,716,953 ✔
5024
    buf = rpcMallocCont(size);
1,716,953 ✔
5025
    *(int64_t*)buf = resultRsp.ver;
1,716,112 ✔
5026
    *(((int64_t*)buf) + 1) = resultRsp.verTime;
1,716,112 ✔
5027
  }
5028
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
1,769,847 ✔
5029
  if (code == TSDB_CODE_STREAM_NO_DATA){
1,771,127 ✔
5030
    code = 0;
1,717,332 ✔
5031
  }
5032
  blockDataDestroy(resultRsp.dataBlock);
1,771,127 ✔
5033
  blockDataDestroy(resultRsp.deleteBlock);
1,770,144 ✔
5034
  blockDataDestroy(resultRsp.tableBlock);
1,770,358 ✔
5035
  tSimpleHashCleanup(resultRsp.indexHash);
1,770,503 ✔
5036

5037
  STREAM_PRINT_LOG_END_WITHID(code, lino);
1,768,079 ✔
5038

5039
  return code;
1,769,107 ✔
5040
}
5041

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

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

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

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

5063
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
157,077 ✔
5064

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

5074
end:
157,077 ✔
5075
  if (code == 0 && resultRsp.totalRows == 0) {
157,147 ✔
5076
    buf = rpcMallocCont(sizeof(int64_t));
150,521 ✔
5077
    *(int64_t *)buf = resultRsp.ver;
150,368 ✔
5078
    size = sizeof(int64_t);
150,368 ✔
5079
    code = TSDB_CODE_STREAM_NO_DATA;
150,368 ✔
5080
  }
5081
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
156,994 ✔
5082
  if (code == TSDB_CODE_STREAM_NO_DATA){
157,217 ✔
5083
    code = 0;
150,521 ✔
5084
  }
5085

5086
  blockDataDestroy(resultRsp.dataBlock);
157,217 ✔
5087
  blockDataDestroy(resultRsp.deleteBlock);
157,076 ✔
5088
  blockDataDestroy(resultRsp.tableBlock);
157,147 ✔
5089
  tSimpleHashCleanup(resultRsp.indexHash);
157,217 ✔
5090
  tSimpleHashCleanup(resultRsp.uidHash);
157,006 ✔
5091
  STREAM_PRINT_LOG_END_WITHID(code, lino);
157,006 ✔
5092

5093
  return code;
157,006 ✔
5094
}
5095

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

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

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

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

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

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

5151
  blockDataDestroy(pBlock1);
105,953 ✔
5152
  blockDataDestroy(pBlock2);
105,953 ✔
5153
  blockDataDestroy(resultRsp.dataBlock);
105,953 ✔
5154
  blockDataDestroy(resultRsp.deleteBlock);
105,953 ✔
5155
  blockDataDestroy(resultRsp.tableBlock);
105,883 ✔
5156
  tSimpleHashCleanup(resultRsp.indexHash);
105,883 ✔
5157
  tSimpleHashCleanup(resultRsp.uidHash);
105,953 ✔
5158
  STREAM_PRINT_LOG_END_WITHID(code, lino);
105,953 ✔
5159

5160
  return code;
105,953 ✔
5161
}
5162

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

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

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

5196
  return code;
114,969 ✔
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,
932 ✔
5248
                                 int64_t uid, const char* caller, SArray** ppRollupGids) {
5249
  void* pTask = sStreamReaderInfo->pTask;
932 ✔
5250
  SArray* rollupGids = taosArrayInit(4, sizeof(uint64_t));
932 ✔
5251
  if (rollupGids == NULL) return terrno;
932 ✔
5252

5253
  taosRLockLatch(&sStreamReaderInfo->lock);
932 ✔
5254
  SArray** pElements = taosHashGet(sStreamReaderInfo->tableList.uIdMap, &uid, LONG_BYTES);
932 ✔
5255
  if (pElements != NULL) {
932 ✔
5256
    int32_t numOfElements = taosArrayGetSize(*pElements);
932 ✔
5257
    ST_TASK_DLOG("vgId:%d %s uid:%" PRId64 " rollup gid count:%d", TD_VID(pVnode), caller, uid, numOfElements);
932 ✔
5258
    for (int32_t j = 0; j < numOfElements; ++j) {
3,728 ✔
5259
      SStreamTableMapElement* element = taosArrayGet(*pElements, j);
2,796 ✔
5260
      if (element == NULL || element->table == NULL || element->table->markedDeleted) continue;
2,796 ✔
5261
      if (taosArrayPush(rollupGids, &element->table->groupId) == NULL) {
5,592 ✔
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);
932 ✔
5271

5272
  *ppRollupGids = rollupGids;
932 ✔
5273
  return TSDB_CODE_SUCCESS;
932 ✔
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,
8,771 ✔
5280
                           SArray* cids, int64_t ver, SMetaReader* metaReader,
5281
                           SStreamTriggerReaderInfo* sStreamReaderInfo) {
5282
  int32_t code = 0;
8,771 ✔
5283
  void*   pTask = sStreamReaderInfo->pTask;
8,771 ✔
5284

5285
  void*   iter = NULL;
8,771 ✔
5286
  int32_t it   = 0;
8,771 ✔
5287
  while ((iter = tSimpleHashIterate(uid2Result, iter, &it)) != NULL) {
21,896 ✔
5288
    int64_t              uid = *(int64_t*)tSimpleHashGetKey(iter, NULL);
13,125 ✔
5289
    SVTableResolveResult* r  = *(SVTableResolveResult**)iter;
13,125 ✔
5290

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

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

5311
      int32_t rc = streamFillVTableInfoFromResolved(pVnode, sStreamReaderInfo, uid, groupId,
12,193 ✔
5312
                                                    ver, cids, r, metaReader, vTableInfo->infos);
5313
      if (rc != 0) return rc;
12,193 ✔
5314
    }
5315
  }
5316
  return TSDB_CODE_SUCCESS;
8,771 ✔
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,
8,771 ✔
5362
                                               SStreamTriggerReaderInfo*   sStreamReaderInfo,
5363
                                               SStreamReaderResponseStats* pRspStats) {
5364
  int32_t              code = 0;
8,771 ✔
5365
  int32_t              lino = 0;
8,771 ✔
5366
  void*                buf = NULL;
8,771 ✔
5367
  size_t               size = 0;
8,771 ✔
5368
  SStreamMsgVTableInfo vTableInfo = {0};
8,771 ✔
5369
  SMetaReader          metaReader = {0};
8,771 ✔
5370
  SArray              *tagCids   = NULL;
8,771 ✔
5371
  SSHashObj           *uid2Result = NULL;
8,771 ✔
5372

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

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

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

5383
  bool fullScan = req->virTableInfoReq.fetchAllTable
17,542 ✔
5384
                  || req->virTableInfoReq.uids == NULL
×
5385
                  || taosArrayGetSize(req->virTableInfoReq.uids) == 0;
8,771 ✔
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) {
8,771 ✔
5392
    cids = NULL;
2,345 ✔
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));
8,771 ✔
5401

5402
  SArray *uidList = fullScan ? NULL : req->virTableInfoReq.uids;
8,771 ✔
5403
  STREAM_CHECK_RET_GOTO(streamResolveVTableRefChain(pVnode, sStreamReaderInfo->vtbCache, sStreamReaderInfo,
8,771 ✔
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);
8,771 ✔
5408

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

5414
  if (fullScan) {
8,771 ✔
5415
    STREAM_CHECK_RET_GOTO(getAllVinfo(pVnode, &vTableInfo, uid2Result, cids, req->virTableInfoReq.ver, &metaReader, sStreamReaderInfo));
8,771 ✔
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));
8,771 ✔
5421
  STREAM_CHECK_RET_GOTO(buildVTableInfoRsp(&vTableInfo, &buf, &size));
8,771 ✔
5422

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

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

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

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

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

5452
  oTableInfo.cols = taosArrayInit(taosArrayGetSize(cols), sizeof(OTableInfoRsp));
12,624 ✔
5453

5454
  STREAM_CHECK_NULL_GOTO(oTableInfo.cols, terrno);
12,624 ✔
5455

5456
  sStreamReaderInfo->storageApi.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &sStreamReaderInfo->storageApi.metaFn, 0);
12,624 ✔
5457
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
28,338 ✔
5458
    OTableInfo*    oInfo = taosArrayGet(cols, i);
15,714 ✔
5459
    OTableInfoRsp* vTableInfo = taosArrayReserve(oTableInfo.cols, 1);
15,714 ✔
5460
    STREAM_CHECK_NULL_GOTO(oInfo, terrno);
15,714 ✔
5461
    STREAM_CHECK_NULL_GOTO(vTableInfo, terrno);
15,714 ✔
5462
    code = sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionName(&metaReader, req->origTableInfoReq.ver, oInfo->refTableName);
15,714 ✔
5463
    if (code != 0) {
15,714 ✔
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;
15,714 ✔
5469
    ST_TASK_DLOG("vgId:%d %s get original uid:%"PRId64, TD_VID(pVnode), __func__, vTableInfo->uid);
15,714 ✔
5470

5471
    SSchemaWrapper* sSchemaWrapper = NULL;
15,714 ✔
5472
    if (metaReader.me.type == TD_CHILD_TABLE) {
15,714 ✔
5473
      int64_t suid = metaReader.me.ctbEntry.suid;
12,789 ✔
5474
      vTableInfo->suid = suid;
12,789 ✔
5475
      tDecoderClear(&metaReader.coder);
12,789 ✔
5476
      STREAM_CHECK_RET_GOTO(sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(&metaReader, req->origTableInfoReq.ver, suid));
12,649 ✔
5477
      sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
12,723 ✔
5478
    } else if (metaReader.me.type == TD_NORMAL_TABLE) {
2,925 ✔
5479
      vTableInfo->suid = 0;
2,925 ✔
5480
      sSchemaWrapper = &metaReader.me.ntbEntry.schemaRow;
2,925 ✔
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++) {
60,967 ✔
5486
      SSchema* s = sSchemaWrapper->pSchema + j;
61,033 ✔
5487
      if (strcmp(s->name, oInfo->refColName) == 0) {
61,033 ✔
5488
        vTableInfo->cid = s->colId;
15,714 ✔
5489
        break;
15,714 ✔
5490
      }
5491
    }
5492
    if (vTableInfo->cid == 0) {
15,648 ✔
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);
15,648 ✔
5497
  }
5498

5499
  STREAM_CHECK_RET_GOTO(buildOTableInfoRsp(&oTableInfo, &buf, &size));
12,624 ✔
5500

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

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

5531
  SArray* cols = req->virTablePseudoColReq.cids;
25,035 ✔
5532
  STREAM_CHECK_NULL_GOTO(cols, terrno);
25,035 ✔
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);
25,035 ✔
5536
  metaReaderInited = true;
25,035 ✔
5537
  STREAM_CHECK_RET_GOTO(sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(&metaReader, req->virTablePseudoColReq.ver, req->virTablePseudoColReq.uid));
25,035 ✔
5538
  STREAM_CHECK_CONDITION_GOTO(metaReader.me.type != TD_VIRTUAL_CHILD_TABLE && metaReader.me.type != TD_VIRTUAL_NORMAL_TABLE, TSDB_CODE_INVALID_PARA);
25,035 ✔
5539
  vtableType = metaReader.me.type;
25,035 ✔
5540
  tstrncpy(vtableName, metaReader.me.name, sizeof(vtableName));
25,035 ✔
5541
  sStreamReaderInfo->storageApi.metaReaderFn.clearReader(&metaReader);
25,035 ✔
5542
  metaReaderInited = false;
25,035 ✔
5543

5544
  STREAM_CHECK_RET_GOTO(createDataBlock(&pBlock));
25,035 ✔
5545

5546
  if (vtableType == TD_VIRTUAL_NORMAL_TABLE) {
25,035 ✔
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);
146 ✔
5549
    SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN, -1);
146 ✔
5550
    STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
146 ✔
5551
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, 1));
146 ✔
5552
    pBlock->info.rows = 1;
146 ✔
5553
    SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, 0);
146 ✔
5554
    STREAM_CHECK_NULL_GOTO(pDst, terrno);
146 ✔
5555
    STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, vtableName, strlen(vtableName), false));
146 ✔
5556
  } else {
5557
    // Virtual child table: resolve tags via chain resolver (single uid, cache-bypass).
5558
    singleUid = taosArrayInit(1, sizeof(int64_t));
24,889 ✔
5559
    STREAM_CHECK_NULL_GOTO(singleUid, terrno);
24,889 ✔
5560
    int64_t uidVal = req->virTablePseudoColReq.uid;
24,889 ✔
5561
    STREAM_CHECK_NULL_GOTO(taosArrayPush(singleUid, &uidVal), terrno);
24,889 ✔
5562

5563
    emptyCols = taosArrayInit(0, sizeof(col_id_t));
24,889 ✔
5564
    STREAM_CHECK_NULL_GOTO(emptyCols, terrno);
24,889 ✔
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));
24,889 ✔
5569
    STREAM_CHECK_NULL_GOTO(tagOnly, terrno);
24,889 ✔
5570
    for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
66,829 ✔
5571
      col_id_t cid = *(col_id_t *)taosArrayGet(cols, i);
41,940 ✔
5572
      if (cid == -1) continue;
41,940 ✔
5573
      STREAM_CHECK_NULL_GOTO(taosArrayPush(tagOnly, &cid), terrno);
17,051 ✔
5574
    }
5575

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

5583
    SVTableResolveResult **pp = (SVTableResolveResult **)tSimpleHashGet(uid2Result, &uidVal, sizeof(uidVal));
24,889 ✔
5584
    if (pp == NULL || *pp == NULL) {
24,889 ✔
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;
24,889 ✔
5593

5594
    // Append column metadata according to caller-requested cids.
5595
    for (size_t i = 0; i < taosArrayGetSize(cols); i++){
66,829 ✔
5596
      col_id_t cid = *(col_id_t*)taosArrayGet(cols, i);
41,940 ✔
5597
      if (cid == -1) {
41,800 ✔
5598
        SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN, -1);
24,819 ✔
5599
        STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
24,819 ✔
5600
        continue;
24,889 ✔
5601
      }
5602
      STagValue **ppv = (pRes->tagMap == NULL) ? NULL : (STagValue **)tSimpleHashGet(pRes->tagMap, &cid, sizeof(cid));
16,981 ✔
5603
      if (ppv == NULL || *ppv == NULL) {
17,051 ✔
5604
        SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_NULL, CHAR_BYTES, cid);
70 ✔
5605
        STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
×
5606
        continue;
×
5607
      }
5608
      STagValue       *v     = *ppv;
16,981 ✔
5609
      int32_t          bytes = (v->nLen > 0 ? v->nLen : 1);
16,981 ✔
5610
      SColumnInfoData  idata = createColumnInfoData(v->type, bytes, cid);
16,981 ✔
5611
      STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
17,051 ✔
5612
    }
5613

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

5617
    for (size_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++){
66,829 ✔
5618
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
41,870 ✔
5619
      STREAM_CHECK_NULL_GOTO(pDst, terrno);
41,940 ✔
5620

5621
      if (pDst->info.colId == -1) {
41,940 ✔
5622
        STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, vtableName, strlen(vtableName), false));
24,889 ✔
5623
        continue;
24,889 ✔
5624
      }
5625
      if (pDst->info.type == TSDB_DATA_TYPE_NULL) {
17,051 ✔
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));
17,051 ✔
5631
      if (ppv == NULL || *ppv == NULL || (*ppv)->pData == NULL) {
17,051 ✔
5632
        STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, NULL, true));
×
5633
        continue;
×
5634
      }
5635
      if (IS_VAR_DATA_TYPE(pDst->info.type)) {
17,051 ✔
5636
        STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, (*ppv)->pData, (*ppv)->nLen, false));
3,543 ✔
5637
      } else {
5638
        STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, (*ppv)->pData, false));
13,508 ✔
5639
      }
5640
    }
5641
  }
5642

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

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

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

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

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

5692
  pReaderTask = (SStreamReaderTask*)sStreamReaderCalcInfo->pTask;
1,930,154 ✔
5693
  pTask = pReaderTask;
1,930,154 ✔
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,930,154 ✔
5695
               sStreamReaderCalcInfo->pTaskInfo, nodeType(sStreamReaderCalcInfo->calcAst->pNode));
5696

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

5717
    initStorageAPI(&handle.api);
1,794,080 ✔
5718
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == nodeType(sStreamReaderCalcInfo->calcAst->pNode) ||
1,794,496 ✔
5719
      QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == nodeType(sStreamReaderCalcInfo->calcAst->pNode)){
693,537 ✔
5720
      STimeRangeNode* node = (STimeRangeNode*)((STableScanPhysiNode*)(sStreamReaderCalcInfo->calcAst->pNode))->pTimeRange;
1,701,129 ✔
5721
      if (node != NULL) {
1,701,129 ✔
5722
        STREAM_CHECK_RET_GOTO(processCalaTimeRange(sStreamReaderCalcInfo, &req, node, &handle, false, TD_VID(pVnode)));
367,329 ✔
5723
      } else {
5724
        ST_TASK_DLOG("vgId:%d %s no scan time range node", TD_VID(pVnode), __func__);
1,333,800 ✔
5725
      }
5726

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

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

5739
    if (sStreamReaderCalcInfo->pTaskInfo == NULL || !qNeedReset(sStreamReaderCalcInfo->pTaskInfo)) {
1,794,496 ✔
5740
      qDestroyTask(sStreamReaderCalcInfo->pTaskInfo);
235,044 ✔
5741
      STREAM_CHECK_RET_GOTO(qCreateStreamExecTaskInfo(&sStreamReaderCalcInfo->pTaskInfo,
235,120 ✔
5742
                                                    sStreamReaderCalcInfo->calcScanPlan, &handle, NULL, TD_VID(pVnode),
5743
                                                    req.taskId));
78 ✔
5744
      STREAM_CHECK_RET_GOTO(qSetTaskId(sStreamReaderCalcInfo->pTaskInfo, req.taskId, req.queryId));
235,120 ✔
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);
1,559,452 ✔
5752
      setTaskScalarExtraInfo(sStreamReaderCalcInfo->pTaskInfo);
1,559,452 ✔
5753
      STREAM_CHECK_RET_GOTO(qResetTableScan(sStreamReaderCalcInfo->pTaskInfo, &handle));
1,559,452 ✔
5754
    }
5755

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

5759
  if (req.pOpParam != NULL) {
1,930,154 ✔
5760
    qUpdateOperatorParam(sStreamReaderCalcInfo->pTaskInfo, (void*)req.pOpParam);
155,659 ✔
5761
  }
5762

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

5765
  pResList = taosArrayInit(4, POINTER_BYTES);
1,930,098 ✔
5766
  STREAM_CHECK_NULL_GOTO(pResList, terrno);
1,930,023 ✔
5767
  uint64_t ts = 0;
1,930,023 ✔
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,930,022 ✔
5774
  STREAM_CHECK_RET_GOTO(qExecTaskOpt(sStreamReaderCalcInfo->pTaskInfo, pResList, &ts, &hasNext, NULL, req.pOpParam != NULL));
1,929,866 ✔
5775

5776
  for(size_t i = 0; i < taosArrayGetSize(pResList); i++){
5,273,954 ✔
5777
    SSDataBlock* pBlock = taosArrayGetP(pResList, i);
3,343,724 ✔
5778
    if (pBlock == NULL) continue;
3,343,648 ✔
5779
    printDataBlock(pBlock, __func__, "fetch", ((SStreamTask*)pTask)->streamId);
3,343,648 ✔
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,930,161 ✔
5789
  const int32_t rspCode = streamBuildFetchRsp(pResList, hasNext, &buf, &size, pVnode->config.tsdbCfg.precision);
1,930,239 ✔
5790
  if (code == TSDB_CODE_SUCCESS) {
1,930,164 ✔
5791
    code = rspCode;
1,930,079 ✔
5792
    if (code == TSDB_CODE_SUCCESS) {
1,930,079 ✔
5793
      addReaderResponseBlocks(pRspStats, pResList);
1,930,079 ✔
5794
    }
5795
  }
5796
}
5797

5798
  if (sStreamReaderCalcInfo && sStreamReaderCalcInfo->rtInfo.funcInfo.isMultiGroupCalc) {
1,930,239 ✔
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,930,239 ✔
5804
    code = TDB_CODE_SUCCESS;
×
5805
  }
5806
  if (pReaderTask != NULL) {
1,930,239 ✔
5807
    pRspStats->activeScanContexts = taosArrayGetSize(calcInfoList);
1,930,154 ✔
5808
    pRspStats->activeScanContextsValid = true;
1,930,154 ✔
5809
    int64_t nowMonoUs = streamTaskGetMonotonicUs();
1,930,154 ✔
5810
    stReaderTaskRecordPullResult(pReaderTask, pRspStats, code, nowMonoUs, taosGetTimestampMs());
1,930,004 ✔
5811
  }
5812
  taosArrayDestroy(pResList);
1,930,239 ✔
5813
  streamReleaseTask(taskAddr);
1,930,239 ✔
5814

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

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

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

5864
  vDebug("vgId:%d, msg:%p in stream reader queue is processing", pVnode->config.vgId, pMsg);
7,266,327 ✔
5865
  if (!syncIsReadyForRead(pVnode->sync)) {
7,266,856 ✔
5866
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
16,534 ✔
5867
    return 0;
16,335 ✔
5868
  }
5869

5870
  if (pMsg->msgType == TDMT_STREAM_FETCH) {
7,250,558 ✔
5871
    return vnodeProcessStreamFetchMsg(pVnode, pMsg, pInfo, &rspStats);
1,930,239 ✔
5872
  } else if (pMsg->msgType == TDMT_STREAM_TRIGGER_PULL) {
5,320,319 ✔
5873
    void*   pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
5,320,249 ✔
5874
    int32_t len = pMsg->contLen - sizeof(SMsgHead);
5,320,249 ✔
5875
    STREAM_CHECK_RET_GOTO(tDeserializeSTriggerPullRequest(pReq, len, &req));
5,320,249 ✔
5876
    stDebug("vgId:%d %s start, type:%d, streamId:%" PRIx64 ", readerTaskId:%" PRIx64 ", sessionId:%" PRIx64 ", applied:%" PRIx64,
5,319,780 ✔
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,320,021 ✔
5879
    STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
5,320,250 ✔
5880
    STREAM_CHECK_RET_GOTO(initTableList(sStreamReaderInfo, pVnode));
5,316,331 ✔
5881
    sendRsp = true;
5,314,826 ✔
5882
    switch (req.base.type) {
5,314,826 ✔
5883
      case STRIGGER_PULL_SET_TABLE:
12,624 ✔
5884
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamSetTableReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
12,624 ✔
5885
        break;
12,624 ✔
5886
      case STRIGGER_PULL_LAST_TS:
86,494 ✔
5887
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamLastTsReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
86,494 ✔
5888
        break;
86,446 ✔
5889
      case STRIGGER_PULL_FIRST_TS:
61,713 ✔
5890
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamFirstTsReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
61,713 ✔
5891
        break;
61,784 ✔
5892
      case STRIGGER_PULL_TSDB_META:
49,585 ✔
5893
      case STRIGGER_PULL_TSDB_META_NEXT:
5894
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbMetaReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
49,585 ✔
5895
        break;
49,585 ✔
5896
      case STRIGGER_PULL_TSDB_TS_DATA:
104,306 ✔
5897
        if (sStreamReaderInfo->isVtableStream) {
104,306 ✔
5898
          STREAM_CHECK_RET_GOTO(
×
5899
              vnodeProcessStreamTsdbTsDataReqVTable(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
5900
        } else {
5901
          STREAM_CHECK_RET_GOTO(
104,306 ✔
5902
              vnodeProcessStreamTsdbTsDataReqNonVTable(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
5903
        }
5904
        break;
104,306 ✔
5905
      case STRIGGER_PULL_TSDB_TRIGGER_DATA:
42,492 ✔
5906
      case STRIGGER_PULL_TSDB_TRIGGER_DATA_NEXT:
5907
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTriggerDataReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
42,492 ✔
5908
        break;
21,246 ✔
5909
      case STRIGGER_PULL_TSDB_CALC_DATA:
1,961,438 ✔
5910
      case STRIGGER_PULL_TSDB_CALC_DATA_NEXT:
5911
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbCalcDataReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
1,961,438 ✔
5912
        break;
1,961,375 ✔
5913
      case STRIGGER_PULL_TSDB_DATA:
14,917 ✔
5914
      case STRIGGER_PULL_TSDB_DATA_NEXT:
5915
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbVirtalDataReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
14,917 ✔
5916
        break;
14,917 ✔
5917
      case STRIGGER_PULL_GROUP_COL_VALUE:
114,969 ✔
5918
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamGroupColValueReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
114,969 ✔
5919
        break;
114,969 ✔
5920
      case STRIGGER_PULL_VTABLE_INFO:
8,771 ✔
5921
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableInfoReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
8,771 ✔
5922
        break;
8,771 ✔
5923
      case STRIGGER_PULL_VTABLE_PSEUDO_COL:
25,035 ✔
5924
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableTagInfoReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
25,035 ✔
5925
        break;
25,035 ✔
5926
      case STRIGGER_PULL_OTABLE_INFO:
12,624 ✔
5927
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamOTableInfoReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
12,624 ✔
5928
        break;
12,624 ✔
5929
      case STRIGGER_PULL_WAL_META_NEW:
785,539 ✔
5930
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaNewReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
785,539 ✔
5931
        break;
784,452 ✔
5932
      case STRIGGER_PULL_WAL_DATA_NEW:
157,217 ✔
5933
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
157,217 ✔
5934
        break;
157,076 ✔
5935
      case STRIGGER_PULL_WAL_META_DATA_NEW:
1,771,058 ✔
5936
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
1,771,058 ✔
5937
        break;
1,769,897 ✔
5938
      case STRIGGER_PULL_WAL_CALC_DATA_NEW:
105,953 ✔
5939
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalCalcDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
105,953 ✔
5940
        break;
105,953 ✔
5941
      default:
91 ✔
5942
        vError("unknown inner msg type:%d in stream reader queue", req.base.type);
91 ✔
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);
70 ✔
5948
    STREAM_CHECK_RET_GOTO(TSDB_CODE_APP_ERROR);
×
5949
  }
5950
end:
5,319,752 ✔
5951
  tDestroySTriggerPullRequest(&req);
5,319,832 ✔
5952
  STREAM_PRINT_LOG_END(code, lino);
5,318,487 ✔
5953
  if (!sendRsp) {
5,319,818 ✔
5954
    if (pMsg->msgType == TDMT_STREAM_TRIGGER_PULL) {
5,289 ✔
5955
      if (code == TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH) {
5,289 ✔
5956
        sendTriggerPullRsp(pMsg, sStreamReaderInfo, NULL, 0, code, &rspStats);
1,370 ✔
5957
      } else {
5958
        sendTriggerPullRsp(pMsg, sStreamReaderInfo, pMsg->info.rsp, pMsg->info.rspLen, code, &rspStats);
3,919 ✔
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,319,818 ✔
5971
  return code;
5,320,315 ✔
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