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

taosdata / TDengine / #5138

05 Nov 2026 03:32AM UTC coverage: 73.942% (-0.3%) from 74.268%
#5138

push

travis-ci

jbjia
fix: capture coverage once from per-thread gcda

Per-case lcov walked the shared build tree and kept each case busy for minutes. Pass GCOV_PREFIX into the test container so each thread writes its own counters, then merge and capture after the suite.

373256 of 504798 relevant lines covered (73.94%)

4305711.12 hits per line

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

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

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

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

54
extern int64_t tsMaxKeyByPrecision[];
55
extern int     metaGetTableSzNameByUid(void *meta, uint64_t uid, char *tbName);
56

57
static SColumnNode* getRollupTagCol(const SStreamTriggerReaderInfo* pInfo) {
368 ✔
58
  SNode* pNode = nodesListGetNode(pInfo->pRollupTagCols, 0);
368 ✔
59
  return (pNode != NULL && nodeType(pNode) == QUERY_NODE_COLUMN) ? (SColumnNode*)pNode : NULL;
368 ✔
60
}
61

62
typedef struct {
63
  int32_t off;
64
  int32_t len;
65
} SRollupSegmentRef;
66

67
typedef struct {
68
  const char* input;
69
  SArray*     segments;
70
} SRollupPath;
71

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

74
static int32_t streamRollupSkipped(bool* pSkipped) {
3 ✔
75
  if (pSkipped != NULL) {
3 ✔
76
    *pSkipped = true;
3 ✔
77
  }
78
  return TSDB_CODE_SUCCESS;
3 ✔
79
}
80

81
static bool isIllegalSegmentByte(char c) {
257 ✔
82
  unsigned char uc = (unsigned char)c;
257 ✔
83
  return uc < 0x20;
257 ✔
84
}
85

86
static bool isSegmentEdgeBlank(char c) { return c == ' ' || c == '\t'; }
453 ✔
87

88
static void destroyRollupPath(SRollupPath* p) {
111 ✔
89
  if (p == NULL) {
111 ✔
90
    return;
×
91
  }
92

93
  taosArrayDestroy(p->segments);
111 ✔
94
  p->segments = NULL;
111 ✔
95
  p->input = NULL;
111 ✔
96
}
97

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

103
  pOut->input = NULL;
96 ✔
104
  pOut->segments = NULL;
96 ✔
105
  if (pSkipped != NULL) {
96 ✔
106
    *pSkipped = false;
96 ✔
107
  }
108

109
  if (tagValue == NULL || tagLen == 0) {
96 ✔
110
    return streamRollupSkipped(pSkipped);
×
111
  }
112

113
  if (tagLen < 0 || tagValue[0] == '.' || tagValue[tagLen - 1] == '.') {
96 ✔
114
    return TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH;
3 ✔
115
  }
116

117
  pOut->segments = taosArrayInit(4, sizeof(SRollupSegmentRef));
93 ✔
118
  if (pOut->segments == NULL) {
93 ✔
119
    return streamRollupErrno();
×
120
  }
121

122
  int32_t segStart = 0;
93 ✔
123
  for (int32_t i = 0; i <= tagLen; ++i) {
574 ✔
124
    if (i < tagLen && tagValue[i] != '.') {
493 ✔
125
      if (isIllegalSegmentByte(tagValue[i])) {
257 ✔
126
        destroyRollupPath(pOut);
1 ✔
127
        return TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH;
1 ✔
128
      }
129
      continue;
256 ✔
130
    }
131

132
    int32_t segLen = i - segStart;
236 ✔
133
    if (segLen <= 0 || isSegmentEdgeBlank(tagValue[segStart]) || isSegmentEdgeBlank(tagValue[i - 1])) {
236 ✔
134
      destroyRollupPath(pOut);
11 ✔
135
      return TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH;
11 ✔
136
    }
137

138
    SRollupSegmentRef ref = {.off = segStart, .len = segLen};
225 ✔
139
    if (taosArrayPush(pOut->segments, &ref) == NULL) {
450 ✔
140
      int32_t code = streamRollupErrno();
×
141
      destroyRollupPath(pOut);
×
142
      return code;
×
143
    }
144

145
    segStart = i + 1;
225 ✔
146
  }
147

148
  pOut->input = tagValue;
81 ✔
149
  return TSDB_CODE_SUCCESS;
81 ✔
150
}
151

152
static int32_t buildRollupPrefixes(const SRollupPath* p, SArray** ppPrefixes) {
81 ✔
153
  if (ppPrefixes == NULL) {
81 ✔
154
    return TSDB_CODE_INVALID_PARA;
×
155
  }
156

157
  *ppPrefixes = NULL;
81 ✔
158

159
  if (p == NULL || p->input == NULL || p->segments == NULL) {
81 ✔
160
    return TSDB_CODE_INVALID_PARA;
×
161
  }
162

163
  int32_t numOfSegments = (int32_t)taosArrayGetSize(p->segments);
81 ✔
164
  SArray* prefixes = taosArrayInit(numOfSegments, sizeof(char*));
81 ✔
165
  if (prefixes == NULL) {
81 ✔
166
    return streamRollupErrno();
×
167
  }
168

169
  int32_t totalLen = 0;
81 ✔
170
  for (int32_t i = 0; i < numOfSegments; ++i) {
296 ✔
171
    SRollupSegmentRef* ref = taosArrayGet(p->segments, i);
215 ✔
172
    totalLen += ref->len + (i == 0 ? 0 : 1);
215 ✔
173

174
    char* prefix = taosMemoryMalloc(totalLen + 1);
215 ✔
175
    if (prefix == NULL) {
215 ✔
176
      taosArrayDestroyP(prefixes, NULL);
×
177
      return streamRollupErrno();
×
178
    }
179

180
    int32_t pos = 0;
215 ✔
181
    for (int32_t j = 0; j <= i; ++j) {
626 ✔
182
      SRollupSegmentRef* part = taosArrayGet(p->segments, j);
411 ✔
183
      if (j > 0) {
411 ✔
184
        prefix[pos++] = '.';
196 ✔
185
      }
186
      memcpy(prefix + pos, p->input + part->off, part->len);
411 ✔
187
      pos += part->len;
411 ✔
188
    }
189
    prefix[pos] = '\0';
215 ✔
190

191
    if (taosArrayPush(prefixes, &prefix) == NULL) {
215 ✔
192
      int32_t code = streamRollupErrno();
×
193
      taosMemoryFree(prefix);
×
194
      taosArrayDestroyP(prefixes, NULL);
×
195
      return code;
×
196
    }
197
  }
198

199
  *ppPrefixes = prefixes;
81 ✔
200
  return TSDB_CODE_SUCCESS;
81 ✔
201
}
202

203
static uint64_t computeRollupGid(uint64_t suid, const char* path) {
215 ✔
204
  if (path == NULL) {
215 ✔
205
    return 0;
×
206
  }
207

208
  int32_t pathLen = (int32_t)strlen(path);
215 ✔
209
  int32_t keyLen = (int32_t)sizeof(suid) + pathLen;
215 ✔
210
  char    stackBuf[256] = {0};
215 ✔
211
  char*   keyBuf = stackBuf;
215 ✔
212

213
  if (keyLen > (int32_t)sizeof(stackBuf)) {
215 ✔
214
    keyBuf = taosMemoryMalloc(keyLen);
×
215
    if (keyBuf == NULL) {
×
216
      return 0;
×
217
    }
218
  }
219

220
  memcpy(keyBuf, &suid, sizeof(suid));
215 ✔
221
  memcpy(keyBuf + sizeof(suid), path, pathLen);
215 ✔
222

223
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
215 ✔
224
  uint64_t   gid = hashFn(keyBuf, keyLen);
215 ✔
225
  if (gid == 0) {
215 ✔
226
    gid = 1;
×
227
  }
228

229
  if (keyBuf != stackBuf) {
215 ✔
230
    taosMemoryFree(keyBuf);
×
231
  }
232
  return gid;
215 ✔
233
}
234

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

244
typedef struct WalMetaResult {
245
  uint64_t    id;
246
  int64_t     skey;
247
  int64_t     ekey;
248
} WalMetaResult;
249

250
typedef struct SWalDataTarget {
251
  uint64_t    gid;
252
  STimeWindow window;
253
} SWalDataTarget;
254

255
typedef struct SWalDataSliceKey {
256
  int64_t  uid;
257
  uint64_t gid;
258
} SWalDataSliceKey;
259

260
static void setRollupIllegalPathInfo(SStreamTriggerReaderInfo* sStreamReaderInfo, uint64_t uid, const char* path,
15 ✔
261
                                     int32_t pathLen) {
262
  taosMemoryFreeClear(sStreamReaderInfo->extraErrMsg);
15 ✔
263

264
  char* sanitized = taosMemoryMalloc(pathLen + 1);
15 ✔
265
  if (sanitized == NULL) {
15 ✔
266
    return;
×
267
  }
268

269
  for (int32_t i = 0; i < pathLen; ++i) {
73 ✔
270
    unsigned char c = (unsigned char)path[i];
58 ✔
271
    sanitized[i] = (c < 0x20) ? '?' : path[i];
58 ✔
272
  }
273
  sanitized[pathLen] = '\0';
15 ✔
274

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

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

289
  sStreamReaderInfo->extraErrMsg = msg;
15 ✔
290
}
291

292
static void buildExtraErrMsgRsp(SStreamTriggerReaderInfo* sStreamReaderInfo, void** pBuf, size_t* pSize) {
15 ✔
293
  if (*pBuf != NULL || sStreamReaderInfo == NULL || sStreamReaderInfo->extraErrMsg == NULL) {
15 ✔
294
    return;
×
295
  }
296

297
  int32_t len = strlen(sStreamReaderInfo->extraErrMsg) + 1;
15 ✔
298
  void*   buf = rpcMallocCont(len);
15 ✔
299
  if (buf == NULL) {
15 ✔
300
    return;
×
301
  }
302

303
  memcpy(buf, sStreamReaderInfo->extraErrMsg, len);
15 ✔
304
  *pBuf = buf;
15 ✔
305
  *pSize = len;
15 ✔
306
}
307

308
static void sendTriggerPullRsp(SRpcMsg* pMsg, SStreamTriggerReaderInfo* sStreamReaderInfo, void* buf, size_t size,
102,199 ✔
309
                               int32_t code, SStreamReaderResponseStats* pRspStats) {
310
  if (code == TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH) {
102,199 ✔
311
    buildExtraErrMsgRsp(sStreamReaderInfo, &buf, &size);
15 ✔
312
  }
313

314
  SRpcMsg rsp = {
102,199 ✔
315
      .msgType = TDMT_STREAM_TRIGGER_PULL_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
316
  if (sStreamReaderInfo != NULL && sStreamReaderInfo->pTask != NULL && pRspStats != NULL) {
102,202 ✔
317
    pRspStats->activeScanContexts = taosHashGetSize(sStreamReaderInfo->streamTaskMap);
102,200 ✔
318
    pRspStats->tableCount = qStreamGetTableListNum(sStreamReaderInfo);
102,200 ✔
319
    pRspStats->activeScanContextsValid = true;
102,204 ✔
320
    pRspStats->tableCountValid = true;
102,204 ✔
321
    int64_t nowMonoUs = streamTaskGetMonotonicUs();
102,204 ✔
322
    stReaderTaskRecordPullResult((SStreamReaderTask*)sStreamReaderInfo->pTask, pRspStats, code, nowMonoUs,
102,206 ✔
323
                                 taosGetTimestampMs());
324
  }
325
  tmsgSendRsp(&rsp);
102,201 ✔
326
}
102,208 ✔
327

328
static void addReaderResponseBlock(SStreamReaderResponseStats* pRspStats, const SSDataBlock* pBlock) {
96,776 ✔
329
  if (pRspStats == NULL || pBlock == NULL || pBlock->info.rows <= 0) return;
96,776 ✔
330

331
  pRspStats->dataRows += (uint64_t)pBlock->info.rows;
64,110 ✔
332
  ++pRspStats->dataBlocks;
64,110 ✔
333
}
334

335
static void addReaderResponseBlocks(SStreamReaderResponseStats* pRspStats, const SArray* pBlocks) {
35,951 ✔
336
  if (pRspStats == NULL || pBlocks == NULL) return;
35,951 ✔
337

338
  for (int32_t i = 0; i < taosArrayGetSize(pBlocks); ++i) {
99,829 ✔
339
    addReaderResponseBlock(pRspStats, taosArrayGetP(pBlocks, i));
69,177 ✔
340
  }
341
}
342

343
static int8_t getWalDataSliceHashType(SStreamTriggerReaderInfo* sStreamReaderInfo) {
38,742 ✔
344
  return isRollupMultiReader(sStreamReaderInfo) ? TSDB_DATA_TYPE_BINARY : TSDB_DATA_TYPE_BIGINT;
38,742 ✔
345
}
346

347
static void getWalDataSliceKey(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t uid, uint64_t gid,
53,548 ✔
348
                               SWalDataSliceKey* pKeyBuf, const void** ppKey, int32_t* pKeyLen) {
349
  if (isRollupMultiReader(sStreamReaderInfo)) {
53,548 ✔
350
    *pKeyBuf = (SWalDataSliceKey){.uid = uid, .gid = gid};
3,694 ✔
351
    *ppKey = pKeyBuf;
3,694 ✔
352
    *pKeyLen = sizeof(*pKeyBuf);
3,694 ✔
353
  } else {
354
    pKeyBuf->uid = uid;
49,847 ✔
355
    *ppKey = &pKeyBuf->uid;
49,847 ✔
356
    *pKeyLen = LONG_BYTES;
49,847 ✔
357
  }
358
}
53,467 ✔
359

360
static int32_t collectWalRollupTargets(SStreamTriggerReaderInfo* sStreamReaderInfo, SSHashObj* ranges, int64_t suid,
3,676 ✔
361
                                       int64_t uid, SArray* targets) {
362
  int32_t code = TSDB_CODE_SUCCESS;
3,676 ✔
363

364
  if (sStreamReaderInfo->tableType == TD_SUPER_TABLE && suid != sStreamReaderInfo->suid) {
3,676 ✔
365
    return TSDB_CODE_SUCCESS;
×
366
  }
367

368
  taosRLockLatch(&sStreamReaderInfo->lock);
3,676 ✔
369

370
  SArray** pElements = taosHashGet(sStreamReaderInfo->tableList.uIdMap, &uid, LONG_BYTES);
3,676 ✔
371
  if (pElements == NULL) {
3,676 ✔
372
    goto end;
×
373
  }
374

375
  int32_t numOfElements = taosArrayGetSize(*pElements);
3,676 ✔
376
  for (int32_t i = 0; i < numOfElements; ++i) {
14,638 ✔
377
    SStreamTableMapElement* element = taosArrayGet(*pElements, i);
10,962 ✔
378
    if (element == NULL || element->table == NULL || element->table->markedDeleted) {
10,962 ✔
379
      continue;
×
380
    }
381

382
    SWalDataTarget target = {.gid = element->table->groupId, .window = {.skey = INT64_MIN, .ekey = INT64_MAX}};
10,962 ✔
383
    if (ranges != NULL) {
10,962 ✔
384
      void* timerange = tSimpleHashGet(ranges, &target.gid, LONG_BYTES);
10,934 ✔
385
      if (timerange == NULL) {
10,934 ✔
386
        continue;
7,268 ✔
387
      }
388
      int64_t* pRange = (int64_t*)timerange;
3,666 ✔
389
      target.window.skey = pRange[0];
3,666 ✔
390
      target.window.ekey = pRange[1];
3,666 ✔
391
    }
392

393
    if (taosArrayPush(targets, &target) == NULL) {
3,694 ✔
394
      code = terrno;
×
395
      break;
×
396
    }
397
  }
398

399
end:
3,676 ✔
400
  taosRUnLockLatch(&sStreamReaderInfo->lock);
3,676 ✔
401
  return code;
3,676 ✔
402
}
403

404
static int32_t addWalDataSlice(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, int64_t uid,
26,765 ✔
405
                               uint64_t gid, int32_t numOfRows, int64_t ver) {
406
  int32_t code = TSDB_CODE_SUCCESS;
26,765 ✔
407
  int32_t lino = 0;
26,765 ✔
408
  void*   pTask = sStreamReaderInfo->pTask;
26,765 ✔
409

410
  if (numOfRows <= 0) {
26,765 ✔
411
    goto end;
×
412
  }
413

414
  if (rsp->uidHash != NULL) {
26,765 ✔
415
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(rsp->uidHash, &uid, LONG_BYTES, &gid, LONG_BYTES));
18,859 ✔
416
    ST_TASK_DLOG("%s put uid into uidHash, uid:%" PRId64 ", gid:%" PRIu64, __func__, uid, gid);
18,864 ✔
417
  }
418

419
  SWalDataSliceKey keyBuf = {0};
26,771 ✔
420
  const void*      pKey = NULL;
26,772 ✔
421
  int32_t          keyLen = 0;
26,779 ✔
422
  getWalDataSliceKey(sStreamReaderInfo, uid, gid, &keyBuf, &pKey, &keyLen);
26,779 ✔
423

424
  SStreamWalDataSlice* pSlice = (SStreamWalDataSlice*)tSimpleHashGet(rsp->indexHash, pKey, keyLen);
26,777 ✔
425
  if (pSlice != NULL) {
26,775 ✔
426
    pSlice->uid = uid;
22,895 ✔
427
    pSlice->gId = gid;
22,895 ✔
428
    pSlice->numRows += numOfRows;
22,895 ✔
429
    ST_TASK_DLOG("%s again uid:%" PRId64 ", gid:%" PRIu64 ", total numOfRows:%d, hash:%p %d, ver:%" PRId64, __func__,
22,895 ✔
430
                 uid, gid, pSlice->numRows, rsp->indexHash, tSimpleHashGetSize(rsp->indexHash), ver);
431
  } else {
432
    SStreamWalDataSlice tmp = {.uid = uid, .gId = gid, .numRows = numOfRows, .currentRowIdx = 0, .startRowIdx = 0};
3,880 ✔
433
    ST_TASK_DLOG("%s first uid:%" PRId64 ", gid:%" PRIu64 ", numOfRows:%d, hash:%p %d, ver:%" PRId64, __func__, uid,
3,880 ✔
434
                 gid, tmp.numRows, rsp->indexHash, tSimpleHashGetSize(rsp->indexHash), ver);
435
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(rsp->indexHash, pKey, keyLen, &tmp, sizeof(tmp)));
3,880 ✔
436
  }
437

438
  rsp->totalRows += numOfRows;
26,778 ✔
439
  rsp->totalDataRows += numOfRows;
26,778 ✔
440

441
end:
26,780 ✔
442
  return code;
26,779 ✔
443
}
444

445
static SStreamWalDataSlice* getWalDataSlice(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp,
26,778 ✔
446
                                            int64_t uid, uint64_t gid) {
447
  SWalDataSliceKey keyBuf = {0};
26,778 ✔
448
  const void*      pKey = NULL;
26,778 ✔
449
  int32_t          keyLen = 0;
26,778 ✔
450
  getWalDataSliceKey(sStreamReaderInfo, uid, gid, &keyBuf, &pKey, &keyLen);
26,777 ✔
451
  return (SStreamWalDataSlice*)tSimpleHashGet(rsp->indexHash, pKey, keyLen);
26,777 ✔
452
}
453

454
static int32_t putWalMetaResult(SSHashObj* gidHash, uint64_t id, int64_t skey, int64_t ekey) {
20,667 ✔
455
  WalMetaResult  walMeta = {.id = id, .skey = skey, .ekey = ekey};
20,667 ✔
456
  WalMetaResult* data = (WalMetaResult*)tSimpleHashGet(gidHash, &walMeta.id, LONG_BYTES);
20,669 ✔
457
  if (data != NULL) {
20,666 ✔
458
    if (walMeta.skey < data->skey) data->skey = walMeta.skey;
4 ✔
459
    if (walMeta.ekey > data->ekey) data->ekey = walMeta.ekey;
4 ✔
460
    return TSDB_CODE_SUCCESS;
4 ✔
461
  }
462

463
  return tSimpleHashPut(gidHash, &walMeta.id, LONG_BYTES, &walMeta, sizeof(WalMetaResult));
20,662 ✔
464
}
465

466
static int32_t putWalMetaResultForRollupUid(SStreamTriggerReaderInfo* sStreamReaderInfo, SSHashObj* gidHash,
75 ✔
467
                                            int64_t suid, int64_t uid, int64_t skey, int64_t ekey) {
468
  int32_t code = TSDB_CODE_SUCCESS;
75 ✔
469

470
  if (sStreamReaderInfo->tableType == TD_SUPER_TABLE && suid != sStreamReaderInfo->suid) {
75 ✔
471
    return TSDB_CODE_SUCCESS;
×
472
  }
473

474
  taosRLockLatch(&sStreamReaderInfo->lock);
75 ✔
475

476
  SArray** pElements = taosHashGet(sStreamReaderInfo->tableList.uIdMap, &uid, LONG_BYTES);
75 ✔
477
  if (pElements == NULL) {
75 ✔
478
    goto end;
×
479
  }
480

481
  int32_t numOfElements = taosArrayGetSize(*pElements);
75 ✔
482
  for (int32_t i = 0; i < numOfElements; ++i) {
285 ✔
483
    SStreamTableMapElement* element = taosArrayGet(*pElements, i);
210 ✔
484
    if (element == NULL || element->table == NULL || element->table->markedDeleted) {
210 ✔
485
      continue;
×
486
    }
487
    code = putWalMetaResult(gidHash, element->table->groupId, skey, ekey);
210 ✔
488
    if (code != TSDB_CODE_SUCCESS) {
210 ✔
489
      break;
×
490
    }
491
  }
492

493
end:
75 ✔
494
  taosRUnLockLatch(&sStreamReaderInfo->lock);
75 ✔
495
  return code;
75 ✔
496
}
497

498
static int64_t getSuid(SStreamTriggerReaderInfo* sStreamReaderInfo, STableKeyInfo* pList) {
34,183 ✔
499
  int64_t suid = 0;
34,183 ✔
500
  if (!sStreamReaderInfo->isVtableStream) {
34,183 ✔
501
    suid = sStreamReaderInfo->suid;
32,536 ✔
502
    goto end;
32,536 ✔
503
  }
504

505
  if (pList == NULL) {
1,647 ✔
506
    goto end;
×
507
  }
508

509
  taosRLockLatch(&sStreamReaderInfo->lock);
1,647 ✔
510
  SStreamTableMapElement* element = taosHashGet(sStreamReaderInfo->vSetTableList.uIdMap, &pList->uid, LONG_BYTES);  
1,647 ✔
511
  if (element != 0) {
1,647 ✔
512
    suid = element->table->groupId;
950 ✔
513
    taosRUnLockLatch(&sStreamReaderInfo->lock);
950 ✔
514
    goto end;
950 ✔
515
  }
516
  taosRUnLockLatch(&sStreamReaderInfo->lock);
697 ✔
517

518
end:
34,183 ✔
519
  return suid;
34,183 ✔
520
}
521

522
static int64_t getSessionKey(int64_t session, int64_t type) { return (session | (type << 32)); }
34,032 ✔
523

524
int32_t sortCid(const void *lp, const void *rp) {
3,520 ✔
525
  int16_t* c1 = (int16_t*)lp;
3,520 ✔
526
  int16_t* c2 = (int16_t*)rp;
3,520 ✔
527

528
  if (*c1 < *c2) {
3,520 ✔
529
    return -1;
3,498 ✔
530
  } else if (*c1 > *c2) {
22 ✔
531
    return 1;
22 ✔
532
  }
533

534
  return 0;
×
535
}
536

537
int32_t sortSSchema(const void *lp, const void *rp) {
3,512 ✔
538
  SSchema* c1 = (SSchema*)lp;
3,512 ✔
539
  SSchema* c2 = (SSchema*)rp;
3,512 ✔
540

541
  if (c1->colId < c2->colId) {
3,512 ✔
542
    return -1;
3,490 ✔
543
  } else if (c1->colId > c2->colId) {
22 ✔
544
    return 1;
22 ✔
545
  }
546

547
  return 0;
×
548
}
549

550
static int32_t addColData(SSDataBlock* pResBlock, int32_t index, void* data) {
87,792 ✔
551
  SColumnInfoData* pSrc = taosArrayGet(pResBlock->pDataBlock, index);
87,792 ✔
552
  if (pSrc == NULL) {
87,796 ✔
553
    return terrno;
17 ✔
554
  }
555

556
  memcpy(pSrc->pData + pResBlock->info.rows * pSrc->info.bytes, data, pSrc->info.bytes);
87,779 ✔
557
  return 0;
87,779 ✔
558
}
559

560
static int32_t appendTsdbMetaRow(SStreamTriggerReaderInfo* sStreamReaderInfo, SSDataBlock* pDstBlock,
1,017 ✔
561
                                 const SSDataBlock* pSrcBlock, uint64_t gid, int32_t rollupTbCount, int32_t vgId) {
562
  int32_t code = TSDB_CODE_SUCCESS;
1,017 ✔
563
  int32_t lino = 0;
1,017 ✔
564
  int32_t index = 0;
1,017 ✔
565
  void*   pTask = sStreamReaderInfo->pTask;
1,017 ✔
566

567
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pDstBlock, pDstBlock->info.rows + 1));
1,017 ✔
568
  STREAM_CHECK_RET_GOTO(addColData(pDstBlock, index++, (void*)&pSrcBlock->info.window.skey));
1,017 ✔
569
  STREAM_CHECK_RET_GOTO(addColData(pDstBlock, index++, (void*)&pSrcBlock->info.window.ekey));
1,017 ✔
570
  STREAM_CHECK_RET_GOTO(addColData(pDstBlock, index++, (void*)&pSrcBlock->info.id.uid));
1,017 ✔
571
  if (!sStreamReaderInfo->isVtableStream) {
1,017 ✔
572
    STREAM_CHECK_RET_GOTO(addColData(pDstBlock, index++, &gid));
589 ✔
573
  }
574
  STREAM_CHECK_RET_GOTO(addColData(pDstBlock, index++, (void*)&pSrcBlock->info.rows));
1,017 ✔
575

576
  if (blockDataGetNumOfCols(pDstBlock) > index) {
1,017 ✔
577
    STREAM_CHECK_RET_GOTO(addColData(pDstBlock, index++, &rollupTbCount));
3 ✔
578
  }
579

580
  ST_TASK_DLOG("vgId:%d get tsdb meta skey:%" PRId64 ", ekey:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64
1,017 ✔
581
               ", rows:%" PRId64 ", rollupTbCount:%d",
582
               vgId, pSrcBlock->info.window.skey, pSrcBlock->info.window.ekey, pSrcBlock->info.id.uid, gid,
583
               pSrcBlock->info.rows, rollupTbCount);
584
  pDstBlock->info.rows++;
1,017 ✔
585

586
end:
1,017 ✔
587
  return code;
1,017 ✔
588
}
589

590
static void destroyRollupMetaEntries(void* pData) {
1 ✔
591
  SArray** ppEntries = pData;
1 ✔
592
  if (ppEntries != NULL) {
1 ✔
593
    taosArrayDestroy(*ppEntries);
1 ✔
594
  }
595
}
1 ✔
596

597
static int32_t putRollupMetaCount(SSHashObj* pCountMap, uint64_t gid) {
3 ✔
598
  int32_t  count = 1;
3 ✔
599
  int32_t* pCount = tSimpleHashGet(pCountMap, &gid, LONG_BYTES);
3 ✔
600
  if (pCount != NULL) {
3 ✔
601
    count = *pCount + 1;
×
602
  }
603

604
  return tSimpleHashPut(pCountMap, &gid, LONG_BYTES, &count, sizeof(count));
3 ✔
605
}
606

607
static int32_t buildRollupMetaCount(STableKeyInfo* pList, int32_t num, SSHashObj** ppCountMap) {
×
608
  int32_t    code = TSDB_CODE_SUCCESS;
×
609
  int32_t    lino = 0;
×
610
  SSHashObj* pCountMap = NULL;
×
611

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

619
  *ppCountMap = pCountMap;
×
620
  pCountMap = NULL;
×
621

622
end:
×
623
  tSimpleHashCleanup(pCountMap);
×
624
  return code;
×
625
}
626

627
static int32_t buildRollupMetaSnapshot(STableKeyInfo* pList, int32_t num, STableKeyInfo** ppReadList, int32_t* pReadNum,
1 ✔
628
                                       SSHashObj** ppByUid, SSHashObj** ppCountMap) {
629
  int32_t        code = TSDB_CODE_SUCCESS;
1 ✔
630
  int32_t        lino = 0;
1 ✔
631
  STableKeyInfo* pReadList = NULL;
1 ✔
632
  SSHashObj*     pByUid = NULL;
1 ✔
633
  SSHashObj*     pCountMap = NULL;
1 ✔
634
  SArray*        pNewEntries = NULL;
1 ✔
635

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

650
  for (int32_t i = 0; i < num; ++i) {
4 ✔
651
    SArray** ppEntries = tSimpleHashGet(pByUid, &pList[i].uid, LONG_BYTES);
3 ✔
652
    if (ppEntries == NULL) {
3 ✔
653
      pNewEntries = taosArrayInit(1, sizeof(STableKeyInfo));
1 ✔
654
      STREAM_CHECK_NULL_GOTO(pNewEntries, terrno);
1 ✔
655
      STREAM_CHECK_RET_GOTO(tSimpleHashPut(pByUid, &pList[i].uid, LONG_BYTES, &pNewEntries, POINTER_BYTES));
1 ✔
656
      pNewEntries = NULL;
1 ✔
657
      ppEntries = tSimpleHashGet(pByUid, &pList[i].uid, LONG_BYTES);
1 ✔
658
      STREAM_CHECK_NULL_GOTO(ppEntries, TSDB_CODE_INTERNAL_ERROR);
1 ✔
659
      pReadList[*pReadNum] = pList[i];
1 ✔
660
      (*pReadNum)++;
1 ✔
661
    }
662

663
    STREAM_CHECK_NULL_GOTO(taosArrayPush(*ppEntries, &pList[i]), terrno);
6 ✔
664
    STREAM_CHECK_RET_GOTO(putRollupMetaCount(pCountMap, pList[i].groupId));
3 ✔
665
  }
666

667
  *ppReadList = pReadList;
1 ✔
668
  pReadList = NULL;
1 ✔
669
  *ppByUid = pByUid;
1 ✔
670
  pByUid = NULL;
1 ✔
671
  *ppCountMap = pCountMap;
1 ✔
672
  pCountMap = NULL;
1 ✔
673

674
end:
1 ✔
675
  taosArrayDestroy(pNewEntries);
1 ✔
676
  taosMemoryFree(pReadList);
1 ✔
677
  tSimpleHashCleanup(pByUid);
1 ✔
678
  tSimpleHashCleanup(pCountMap);
1 ✔
679
  return code;
1 ✔
680
}
681

682
static int32_t getRollupMetaCount(SSHashObj* pCountMap, uint64_t gid) {
3 ✔
683
  int32_t* pCount = tSimpleHashGet(pCountMap, &gid, LONG_BYTES);
3 ✔
684
  return pCount == NULL ? 0 : *pCount;
3 ✔
685
}
686

687
static int32_t getTableDataInfo(SStreamReaderTaskInner* pTask, bool* hasNext) {
43,474 ✔
688
  int32_t code = pTask->storageApi->tsdReader.tsdNextDataBlock(pTask->pReader, hasNext);
43,474 ✔
689
  if (code != TSDB_CODE_SUCCESS) {
43,475 ✔
690
    pTask->storageApi->tsdReader.tsdReaderReleaseDataBlock(pTask->pReader);
×
691
  }
692

693
  return code;
43,475 ✔
694
}
695

696
static int32_t getTableData(SStreamReaderTaskInner* pTask, SSDataBlock** ppRes) {
7,308 ✔
697
  return pTask->storageApi->tsdReader.tsdReaderRetrieveDataBlock(pTask->pReader, ppRes);
7,308 ✔
698
}
699

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

718
static bool ignoreMetaChange(void* pTask, int64_t tableListVer, int64_t ver) {
754 ✔
719
  ST_TASK_DLOG("%s tableListVer:%" PRId64 " ver:%" PRId64, __func__, tableListVer, ver);
754 ✔
720
  return tableListVer >= ver;
754 ✔
721
}
722

723
static bool needReLoadTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int8_t tableType, int64_t suid, int64_t uid, bool isCalc){
11,654 ✔
724
  if ((tableType == TD_CHILD_TABLE || tableType == TD_VIRTUAL_CHILD_TABLE) &&
11,654 ✔
725
      sStreamReaderInfo->tableType == TD_SUPER_TABLE && 
5,552 ✔
726
      suid == sStreamReaderInfo->suid) {
2,757 ✔
727
    taosRLockLatch(&sStreamReaderInfo->lock);
102 ✔
728
    uint64_t gid = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, uid);
102 ✔
729
    taosRUnLockLatch(&sStreamReaderInfo->lock);
102 ✔
730
    if (gid == (uint64_t)-1) return true;
102 ✔
731
  }
732
  return false;
11,484 ✔
733
}
734

735
static bool matchTriggerTable(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid) {
56,475 ✔
736
  switch (sStreamReaderInfo->tableType) {
56,475 ✔
737
    case TD_SUPER_TABLE:
33,087 ✔
738
      return suid == sStreamReaderInfo->suid;
33,087 ✔
739
    case TD_CHILD_TABLE:
12,383 ✔
740
    case TD_VIRTUAL_CHILD_TABLE:
741
      return suid == sStreamReaderInfo->suid && uid == sStreamReaderInfo->uid;
12,383 ✔
742
    case TD_NORMAL_TABLE:
11,061 ✔
743
    case TD_VIRTUAL_NORMAL_TABLE:
744
      return uid == sStreamReaderInfo->uid;
11,061 ✔
745
    default:
×
746
      return true;
×
747
  }
748
}
749

750
static bool uidInTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id) {
56,485 ✔
751
  int32_t ret = false;
56,485 ✔
752
  if (!matchTriggerTable(sStreamReaderInfo, suid, uid)) {
56,485 ✔
753
    goto end;
30,392 ✔
754
  }
755
  if (sStreamReaderInfo->tableType == TD_SUPER_TABLE) {
26,092 ✔
756
    if (qStreamGetTableListNum(sStreamReaderInfo) == 0) goto end;
14,282 ✔
757
  }
758
  *id = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, uid);
26,075 ✔
759
  if (*id == -1) goto end;
26,125 ✔
760
  ret = true;
25,891 ✔
761

762
end:
56,540 ✔
763
  stTrace("%s ret:%d %p %p check suid:%" PRId64 " uid:%" PRId64 " gid:%"PRIu64, __func__, ret, sStreamReaderInfo, sStreamReaderInfo->tableList.gIdMap, suid, uid, *id);
56,540 ✔
764
  return ret;
56,511 ✔
765
}
766

767
static bool uidInTableListOrigin(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id) {
83 ✔
768
  return uidInTableList(sStreamReaderInfo, suid, uid, id);
83 ✔
769
}
770

771
static bool uidInTableListSet(SStreamTriggerReaderInfo* sStreamReaderInfo, int64_t suid, int64_t uid, uint64_t* id, bool isCalc) {
107,503 ✔
772
  bool ret = false;
107,503 ✔
773
  taosRLockLatch(&sStreamReaderInfo->lock);
107,503 ✔
774
  if (sStreamReaderInfo->isVtableStream) {
107,511 ✔
775
    int64_t tmp[2] = {suid, uid};
51,094 ✔
776
    if(tSimpleHashGet(isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, tmp, sizeof(tmp)) != NULL) {
51,095 ✔
777
      *id = uid;
22,051 ✔
778
      ret = true;
22,051 ✔
779
    }
780
  } else {
781
    ret = uidInTableList(sStreamReaderInfo, suid, uid, id);
56,417 ✔
782
  }
783

784
end:
107,527 ✔
785
  taosRUnLockLatch(&sStreamReaderInfo->lock);
107,527 ✔
786
  return ret;
107,552 ✔
787
}
788

789
static int32_t  qTransformStreamTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, void* pTableListInfo, StreamTableListInfo* tableInfo){
1,621 ✔
790
  SArray* pList = qStreamGetTableListArray(pTableListInfo);
1,621 ✔
791
  int32_t totalSize = taosArrayGetSize(pList);
1,622 ✔
792
  int32_t code = 0;
1,622 ✔
793
  void* pTask = sStreamReaderInfo->pTask;
1,622 ✔
794
  for (int32_t i = 0; i < totalSize; ++i) {
24,181 ✔
795
    STableKeyInfo* info = taosArrayGet(pList, i);
22,559 ✔
796
    if (info == NULL) {
22,559 ✔
797
      continue;
×
798
    }
799
    code = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheTrigger, sStreamReaderInfo->pExprInfoTriggerTag, sStreamReaderInfo->numOfExprTriggerTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
22,559 ✔
800
    if (code != 0){
22,558 ✔
801
      ST_TASK_WLOG("%s cacheTag trigger failed for uid:%" PRId64",code:%d", __func__, info->uid, code);
×
802
      continue;
×
803
    }
804
    code = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheCalc, sStreamReaderInfo->pExprInfoCalcTag, sStreamReaderInfo->numOfExprCalcTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
22,558 ✔
805
    if (code != 0){
22,559 ✔
806
      ST_TASK_WLOG("%s cacheTag calc failed for uid:%" PRId64",code:%d", __func__, info->uid, code);
×
807
      continue;
×
808
    }
809
    code = qStreamSetTableList(tableInfo, info->uid, info->groupId);
22,559 ✔
810
    if (code != 0){
22,559 ✔
811
      return code;
×
812
    }
813
  }
814
  return 0;
1,608 ✔
815
}
816

817
static int32_t readRollupTagPath(SStreamTriggerReaderInfo* sStreamReaderInfo, SMetaReader* pReader, char** ppPath,
99 ✔
818
                                 int32_t* pPathLen, bool* pSkipped) {
819
  int32_t      code = TSDB_CODE_SUCCESS;
99 ✔
820
  char*        pData = NULL;
99 ✔
821
  char*        path = NULL;
99 ✔
822
  SColumnNode* pRollupCol = getRollupTagCol(sStreamReaderInfo);
99 ✔
823

824
  *ppPath = NULL;
99 ✔
825
  *pPathLen = 0;
99 ✔
826
  if (pSkipped != NULL) {
99 ✔
827
    *pSkipped = false;
99 ✔
828
  }
829

830
  if (pRollupCol == NULL || nodeType((SNode*)pRollupCol) != QUERY_NODE_COLUMN) {
99 ✔
831
    return TSDB_CODE_STREAM_INVALID_ROLLUP;
×
832
  }
833

834
  int8_t type = pRollupCol->node.resType.type;
99 ✔
835
  if (type != TSDB_DATA_TYPE_VARCHAR && type != TSDB_DATA_TYPE_NCHAR) {
99 ✔
836
    return TSDB_CODE_STREAM_INVALID_ROLLUP;
×
837
  }
838

839
  STagVal     tagVal = {.cid = pRollupCol->colId};
99 ✔
840
  const char* pTagVal = sStreamReaderInfo->storageApi.metaFn.extractTagVal(pReader->me.ctbEntry.pTags, type, &tagVal);
99 ✔
841
  if (pTagVal == NULL) {
99 ✔
842
    code = streamRollupSkipped(pSkipped);
1 ✔
843
    goto end;
1 ✔
844
  }
845

846
  pData = tTagValToData((const STagVal*)pTagVal, false);
98 ✔
847
  if (pData == NULL) {
98 ✔
848
    code = terrno == 0 ? streamRollupSkipped(pSkipped) : terrno;
×
849
    goto end;
×
850
  }
851

852
  int32_t tagLen = varDataLen(pData);
98 ✔
853
  if (tagLen == 0) {
98 ✔
854
    code = streamRollupSkipped(pSkipped);
2 ✔
855
    goto end;
2 ✔
856
  }
857

858
  if (type == TSDB_DATA_TYPE_NCHAR) {
96 ✔
859
    path = taosMemoryCalloc(1, tagLen + 1);
67 ✔
860
    if (path == NULL) {
67 ✔
861
      code = terrno;
×
862
      goto end;
×
863
    }
864
    // Rollup paths are server-internal and must use the same server charset in both directions.
865
    int32_t len = taosUcs4ToMbsCharsetInsensitive((TdUcs4*)varDataVal(pData),
67 ✔
866
                                                  tagLen, path);
867
    if (len < 0) {
67 ✔
868
      code = len;
×
869
      goto end;
×
870
    }
871
    if (len == 0) {
67 ✔
872
      code = streamRollupSkipped(pSkipped);
×
873
      goto end;
×
874
    }
875
    path[len] = '\0';
67 ✔
876
    *pPathLen = len;
67 ✔
877
  } else {
878
    path = taosMemoryMalloc(tagLen + 1);
29 ✔
879
    if (path == NULL) {
29 ✔
880
      code = terrno;
×
881
      goto end;
×
882
    }
883
    memcpy(path, varDataVal(pData), tagLen);
29 ✔
884
    path[tagLen] = '\0';
29 ✔
885
    *pPathLen = tagLen;
29 ✔
886
  }
887

888
  *ppPath = path;
96 ✔
889
  path = NULL;
96 ✔
890

891
end:
99 ✔
892
  taosMemoryFree(path);
99 ✔
893
  taosMemoryFree(pData);
99 ✔
894
  return code;
99 ✔
895
}
896

897
static int32_t setRollupGroupValue(SStreamGroupValue* pValue, const char* path, int32_t pathLen, int8_t type) {
163 ✔
898
  pValue->data.type = type;
163 ✔
899
  pValue->isNull = false;
163 ✔
900

901
  if (type == TSDB_DATA_TYPE_NCHAR) {
163 ✔
902
    int32_t ucs4Cap = (pathLen + 1) * TSDB_NCHAR_SIZE;
136 ✔
903
    pValue->data.pData = taosMemoryCalloc(1, ucs4Cap);
136 ✔
904
    if (pValue->data.pData == NULL) {
136 ✔
905
      return terrno;
×
906
    }
907
    if (!taosMbsToUcs4CharsetInsensitive(path, pathLen,
136 ✔
908
                                         (TdUcs4*)pValue->data.pData, ucs4Cap,
136 ✔
909
                                         &pValue->data.nData)) {
136 ✔
910
      taosMemoryFreeClear(pValue->data.pData);
×
911
      return terrno;
×
912
    }
913
  } else {
914
    pValue->data.pData = taosMemoryMalloc(pathLen + 1);
27 ✔
915
    if (pValue->data.pData == NULL) {
27 ✔
916
      return terrno;
×
917
    }
918
    memcpy(pValue->data.pData, path, pathLen);
27 ✔
919
    ((char*)pValue->data.pData)[pathLen] = '\0';
27 ✔
920
    pValue->data.nData = pathLen;
27 ✔
921
  }
922

923
  return TSDB_CODE_SUCCESS;
163 ✔
924
}
925

926
static void formatRollupGroupPath(SArray* gInfo, char* buf, int32_t bufLen) {
131 ✔
927
  if (buf == NULL || bufLen <= 0) {
131 ✔
928
    return;
×
929
  }
930

931
  buf[0] = '\0';
131 ✔
932
  if (gInfo == NULL || taosArrayGetSize(gInfo) <= 0) {
131 ✔
933
    return;
×
934
  }
935

936
  SStreamGroupValue* pValue = taosArrayGet(gInfo, 0);
131 ✔
937
  if (pValue == NULL || pValue->isNull || pValue->data.pData == NULL) {
131 ✔
938
    return;
×
939
  }
940

941
  if (pValue->data.type == TSDB_DATA_TYPE_NCHAR) {
131 ✔
942
    if (pValue->data.nData >= bufLen) {
110 ✔
943
      buf[bufLen - 1] = '\0';
×
944
      return;
×
945
    }
946

947
    int32_t len = taosUcs4ToMbsCharsetInsensitive((TdUcs4*)pValue->data.pData,
110 ✔
948
                                                  pValue->data.nData, buf);
110 ✔
949
    if (len < 0 || len >= bufLen) {
110 ✔
950
      buf[0] = '\0';
×
951
    } else {
952
      buf[len] = '\0';
110 ✔
953
    }
954
  } else {
955
    int32_t len = TMIN(pValue->data.nData, bufLen - 1);
21 ✔
956
    memcpy(buf, pValue->data.pData, len);
21 ✔
957
    buf[len] = '\0';
21 ✔
958
  }
959
}
960

961
static void logRollupGroupSummary(SStreamTriggerReaderInfo* sStreamReaderInfo) {
54 ✔
962
  if (!sStreamReaderInfo->isRollupReader || sStreamReaderInfo->groupIdMap == NULL) {
54 ✔
963
    return;
×
964
  }
965

966
  void*   pTask = sStreamReaderInfo->pTask;
54 ✔
967
  int32_t total = taosHashGetSize(sStreamReaderInfo->groupIdMap);
54 ✔
968
  char    sample[3][TSDB_TABLE_NAME_LEN] = {{0}};
54 ✔
969
  int32_t sampleNum = 0;
54 ✔
970

971
  SArray** ppGroup = taosHashIterate(sStreamReaderInfo->groupIdMap, NULL);
54 ✔
972
  while (ppGroup != NULL && sampleNum < 3) {
185 ✔
973
    formatRollupGroupPath(*ppGroup, sample[sampleNum], sizeof(sample[sampleNum]));
131 ✔
974
    sampleNum++;
131 ✔
975
    ppGroup = taosHashIterate(sStreamReaderInfo->groupIdMap, ppGroup);
131 ✔
976
  }
977
  if (ppGroup != NULL) {
54 ✔
978
    taosHashCancelIterate(sStreamReaderInfo->groupIdMap, ppGroup);
12 ✔
979
  }
980

981
  SColumnNode* pRollupCol = getRollupTagCol(sStreamReaderInfo);
54 ✔
982
  ST_TASK_ILOG("rollup by %s, total_groups=%d, sample_paths=[%s,%s,%s]", pRollupCol == NULL ? "" : pRollupCol->colName,
54 ✔
983
               total, sample[0], sample[1], sample[2]);
984
}
985

986
static int32_t putRollupGroupValue(SStreamTriggerReaderInfo* sStreamReaderInfo, uint64_t gid, const char* path,
215 ✔
987
                                   int32_t pathLen) {
988
  int32_t      code = TSDB_CODE_SUCCESS;
215 ✔
989
  SArray*      gInfo = NULL;
215 ✔
990
  SColumnNode* pRollupCol = getRollupTagCol(sStreamReaderInfo);
215 ✔
991
  void*        pTask = sStreamReaderInfo->pTask;
215 ✔
992

993
  if (taosHashGet(sStreamReaderInfo->groupIdMap, &gid, sizeof(gid)) != NULL) {
215 ✔
994
    return TSDB_CODE_SUCCESS;
52 ✔
995
  }
996

997
  gInfo = taosArrayInit(1, sizeof(SStreamGroupValue));
163 ✔
998
  if (gInfo == NULL) {
163 ✔
999
    return terrno;
×
1000
  }
1001

1002
  SStreamGroupValue* pValue = taosArrayReserve(gInfo, 1);
163 ✔
1003
  if (pValue == NULL) {
163 ✔
1004
    code = terrno;
×
1005
    goto end;
×
1006
  }
1007

1008
  code = setRollupGroupValue(pValue, path, pathLen, pRollupCol->node.resType.type);
163 ✔
1009
  if (code != TSDB_CODE_SUCCESS) {
163 ✔
1010
    goto end;
×
1011
  }
1012

1013
  code = taosHashPut(sStreamReaderInfo->groupIdMap, &gid, sizeof(gid), &gInfo, POINTER_BYTES);
163 ✔
1014
  if (code != TSDB_CODE_SUCCESS) {
163 ✔
1015
    goto end;
×
1016
  }
1017
  ST_TASK_DLOG("rollup new group gid=%" PRIu64 " path=%.*s", gid, pathLen, path);
163 ✔
1018
  gInfo = NULL;
163 ✔
1019

1020
end:
163 ✔
1021
  taosArrayDestroyEx(gInfo, tDestroySStreamGroupValue);
163 ✔
1022
  return code;
163 ✔
1023
}
1024

1025
static int32_t setRollupPrefixColData(SColumnInfoData* pColData, const char* prefix, int32_t prefixLen) {
×
1026
  int32_t code = TSDB_CODE_SUCCESS;
×
1027
  int32_t lino = 0;
×
1028
  char*   ucs4 = NULL;
×
1029

1030
  if (pColData->info.type == TSDB_DATA_TYPE_NCHAR) {
×
1031
    int32_t ucs4Cap = (prefixLen + 1) * TSDB_NCHAR_SIZE;
×
1032
    int32_t ucs4Len = 0;
×
1033

1034
    ucs4 = taosMemoryCalloc(1, ucs4Cap);
×
1035
    STREAM_CHECK_NULL_GOTO(ucs4, terrno);
×
1036
    if (!taosMbsToUcs4CharsetInsensitive(prefix, prefixLen, (TdUcs4*)ucs4,
×
1037
                                         ucs4Cap, &ucs4Len)) {
1038
      code = streamRollupErrno();
×
1039
      goto end;
×
1040
    }
1041

1042
    STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, 0, ucs4, ucs4Len, false));
×
1043
  } else if (pColData->info.type == TSDB_DATA_TYPE_VARCHAR || pColData->info.type == TSDB_DATA_TYPE_BINARY) {
×
1044
    STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, 0, prefix, prefixLen, false));
×
1045
  } else {
1046
    code = TSDB_CODE_STREAM_INVALID_ROLLUP;
×
1047
    lino = __LINE__;
×
1048
    goto end;
×
1049
  }
1050

1051
end:
×
1052
  taosMemoryFree(ucs4);
×
1053
  STREAM_PRINT_LOG_END(code, lino);
×
1054
  return code;
×
1055
}
1056

1057
static int32_t createRollupFilterResult(SScalarParam* pParam) {
×
1058
  SColumnInfoData* pColumnData = taosMemoryCalloc(1, sizeof(SColumnInfoData));
×
1059
  if (pColumnData == NULL) {
×
1060
    return terrno;
×
1061
  }
1062

1063
  pColumnData->info.type = TSDB_DATA_TYPE_BOOL;
×
1064
  pColumnData->info.bytes = sizeof(bool);
×
1065

1066
  int32_t code = colInfoDataEnsureCapacity(pColumnData, 1, true);
×
1067
  if (code != TSDB_CODE_SUCCESS) {
×
1068
    colDataDestroy(pColumnData);
×
1069
    taosMemoryFree(pColumnData);
×
1070
    return code;
×
1071
  }
1072

1073
  pParam->columnData = pColumnData;
×
1074
  pParam->colAlloced = true;
×
1075
  return TSDB_CODE_SUCCESS;
×
1076
}
1077

1078
static int32_t appendRollupFilterColInfo(SSDataBlock* pBlock, SArray* pColList) {
×
1079
  int32_t code = TSDB_CODE_SUCCESS;
×
1080
  int32_t lino = 0;
×
1081

1082
  int32_t numOfCols = taosArrayGetSize(pColList);
×
1083
  for (int32_t i = 0; i < numOfCols; ++i) {
×
1084
    SColumnInfo* pInfo = taosArrayGet(pColList, i);
×
1085
    STREAM_CHECK_NULL_GOTO(pInfo, terrno);
×
1086

1087
    SColumnInfoData colInfo = {0};
×
1088
    colInfo.info = *pInfo;
×
1089
    STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &colInfo));
×
1090
  }
1091

1092
end:
×
1093
  STREAM_PRINT_LOG_END(code, lino);
×
1094
  return code;
×
1095
}
1096

1097
static bool isRollupFilterTagCol(const SColumnNode* pRollupCol, const SColumnInfoData* pDst) {
×
1098
  return pRollupCol != NULL && pDst->info.colId == pRollupCol->colId &&
×
1099
         pDst->info.type == pRollupCol->node.resType.type;
×
1100
}
1101

1102
static int32_t setRollupFilterTagVal(SStreamTriggerReaderInfo* sStreamReaderInfo, SMetaReader* pReader,
×
1103
                                     SColumnInfoData* pDst) {
1104
  int32_t      code = TSDB_CODE_SUCCESS;
×
1105
  int32_t      lino = 0;
×
1106
  char*        data = NULL;
×
1107
  const char*  pTagVal = NULL;
×
1108
  SColumnNode* pRollupCol = getRollupTagCol(sStreamReaderInfo);
×
1109

1110
  STREAM_CHECK_NULL_GOTO(pRollupCol, TSDB_CODE_STREAM_INVALID_ROLLUP);
×
1111

1112
  if (pDst->info.colId == -1) {
×
1113
    char tbname[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1114
    STREAM_CHECK_RET_GOTO(
×
1115
        sStreamReaderInfo->storageApi.metaFn.getTableNameByUid(sStreamReaderInfo->pVnode, pReader->me.uid, tbname, 0));
×
1116
    STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, tbname, false));
×
1117
    goto end;
×
1118
  }
1119

1120
  if (isRollupFilterTagCol(pRollupCol, pDst)) {
×
1121
    goto end;
×
1122
  }
1123

1124
  STagVal tagVal = {.cid = pDst->info.colId};
×
1125
  pTagVal = sStreamReaderInfo->storageApi.metaFn.extractTagVal(pReader->me.ctbEntry.pTags, pDst->info.type, &tagVal);
×
1126
  if (pTagVal != NULL && pDst->info.type != TSDB_DATA_TYPE_JSON) {
×
1127
    data = tTagValToData((const STagVal*)pTagVal, false);
×
1128
    STREAM_CHECK_CONDITION_GOTO(data == NULL && IS_VAR_DATA_TYPE(((const STagVal*)pTagVal)->type), terrno);
×
1129
  } else {
1130
    data = (char*)pTagVal;
×
1131
  }
1132

1133
  bool isNull = data == NULL || (pDst->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data));
×
1134
  STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, data, isNull));
×
1135

1136
end:
×
1137
  if (pTagVal != NULL && pDst->info.type != TSDB_DATA_TYPE_JSON && IS_VAR_DATA_TYPE(((const STagVal*)pTagVal)->type)) {
×
1138
    taosMemoryFree(data);
×
1139
  }
1140
  STREAM_PRINT_LOG_END(code, lino);
×
1141
  return code;
×
1142
}
1143

1144
static int32_t createRollupPrefixFilterBlock(SStreamTriggerReaderInfo* sStreamReaderInfo, SMetaReader* pReader,
×
1145
                                             SArray* pColList, SSDataBlock** ppBlock) {
1146
  int32_t      code = TSDB_CODE_SUCCESS;
×
1147
  int32_t      lino = 0;
×
1148
  SSDataBlock* pBlock = NULL;
×
1149

1150
  *ppBlock = NULL;
×
1151

1152
  STREAM_CHECK_RET_GOTO(createDataBlock(&pBlock));
×
1153
  STREAM_CHECK_RET_GOTO(appendRollupFilterColInfo(pBlock, pColList));
×
1154
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, 1));
×
1155
  pBlock->info.rows = 1;
×
1156

1157
  int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
×
1158
  for (int32_t i = 0; i < numOfCols; ++i) {
×
1159
    SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
×
1160
    STREAM_CHECK_NULL_GOTO(pDst, terrno);
×
1161
    STREAM_CHECK_RET_GOTO(setRollupFilterTagVal(sStreamReaderInfo, pReader, pDst));
×
1162
  }
1163

1164
  *ppBlock = pBlock;
×
1165
  pBlock = NULL;
×
1166

1167
end:
×
1168
  blockDataDestroy(pBlock);
×
1169
  STREAM_PRINT_LOG_END(code, lino);
×
1170
  return code;
×
1171
}
1172

1173
static int32_t setRollupPrefixFilterTagVals(SStreamTriggerReaderInfo* sStreamReaderInfo, SSDataBlock* pBlock,
×
1174
                                            const char* prefix, int32_t prefixLen) {
1175
  int32_t      code = TSDB_CODE_SUCCESS;
×
1176
  int32_t      lino = 0;
×
1177
  SColumnNode* pRollupCol = getRollupTagCol(sStreamReaderInfo);
×
1178

1179
  STREAM_CHECK_NULL_GOTO(pRollupCol, TSDB_CODE_STREAM_INVALID_ROLLUP);
×
1180

1181
  int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
×
1182
  for (int32_t i = 0; i < numOfCols; ++i) {
×
1183
    SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
×
1184
    STREAM_CHECK_NULL_GOTO(pDst, terrno);
×
1185
    if (isRollupFilterTagCol(pRollupCol, pDst)) {
×
1186
      STREAM_CHECK_RET_GOTO(setRollupPrefixColData(pDst, prefix, prefixLen));
×
1187
    }
1188
  }
1189

1190
end:
×
1191
  STREAM_PRINT_LOG_END(code, lino);
×
1192
  return code;
×
1193
}
1194

1195
static int32_t rollupPrefixPassesTagFilter(SStreamTriggerReaderInfo* sStreamReaderInfo, SSDataBlock* pBlock,
215 ✔
1196
                                           SArray* pBlockList, const char* prefix, int32_t prefixLen, bool* pPassed) {
1197
  int32_t      code = TSDB_CODE_SUCCESS;
215 ✔
1198
  int32_t      lino = 0;
215 ✔
1199
  SScalarParam output = {0};
215 ✔
1200

1201
  *pPassed = true;
215 ✔
1202
  if (sStreamReaderInfo->pTagCond == NULL) {
215 ✔
1203
    return TSDB_CODE_SUCCESS;
215 ✔
1204
  }
1205

1206
  STREAM_CHECK_RET_GOTO(setRollupPrefixFilterTagVals(sStreamReaderInfo, pBlock, prefix, prefixLen));
×
1207
  STREAM_CHECK_RET_GOTO(createRollupFilterResult(&output));
×
1208

1209
  gTaskScalarExtra.pStreamInfo = NULL;
×
1210
  gTaskScalarExtra.pStreamRange = NULL;
×
1211
  STREAM_CHECK_RET_GOTO(scalarCalculate(sStreamReaderInfo->pTagCond, pBlockList, &output, &gTaskScalarExtra));
×
1212

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

1215
end:
×
1216
  colDataDestroy(output.columnData);
×
1217
  taosMemoryFree(output.columnData);
×
1218
  STREAM_PRINT_LOG_END(code, lino);
×
1219
  return code;
×
1220
}
1221

1222
static int32_t appendRollupGroupsForUid(SStreamTriggerReaderInfo* sStreamReaderInfo, uint64_t uid, SArray* pTableList,
99 ✔
1223
                                        StreamTableListInfo* tableInfo, SArray* pTagFilterCols) {
1224
  int32_t      code = TSDB_CODE_SUCCESS;
99 ✔
1225
  char*        path = NULL;
99 ✔
1226
  int32_t      pathLen = 0;
99 ✔
1227
  bool         skipped = false;
99 ✔
1228
  void*        pTask = sStreamReaderInfo->pTask;
99 ✔
1229
  SRollupPath  rollupPath = {0};
99 ✔
1230
  SArray*      prefixes = NULL;
99 ✔
1231
  SSDataBlock* pFilterBlock = NULL;
99 ✔
1232
  SArray*      pFilterBlockList = NULL;
99 ✔
1233
  SMetaReader  mr = {0};
99 ✔
1234
  SStorageAPI* api = &sStreamReaderInfo->storageApi;
99 ✔
1235

1236
  api->metaReaderFn.initReader(&mr, sStreamReaderInfo->pVnode, META_READER_LOCK, &api->metaFn, 0);
99 ✔
1237
  code = api->metaReaderFn.getEntryGetUidCache(&mr, uid);
99 ✔
1238
  api->metaReaderFn.readerReleaseLock(&mr);
99 ✔
1239
  if (code != TSDB_CODE_SUCCESS) {
99 ✔
1240
    goto end;
×
1241
  }
1242

1243
  code = readRollupTagPath(sStreamReaderInfo, &mr, &path, &pathLen, &skipped);
99 ✔
1244
  if (skipped) {
99 ✔
1245
    goto end;
3 ✔
1246
  }
1247
  if (code != TSDB_CODE_SUCCESS) {
96 ✔
1248
    goto end;
×
1249
  }
1250

1251
  code = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheTrigger,
96 ✔
1252
                  sStreamReaderInfo->pExprInfoTriggerTag, sStreamReaderInfo->numOfExprTriggerTag,
1253
                  &sStreamReaderInfo->storageApi, uid, 0, NULL);
1254
  if (code != TSDB_CODE_SUCCESS) {
96 ✔
1255
    ST_TASK_WLOG("%s cacheTag trigger failed for uid:%" PRId64 ",code:%d", __func__, uid, code);
×
1256
    code = TSDB_CODE_SUCCESS;
×
1257
    goto end;
×
1258
  }
1259
  code =
×
1260
      cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheCalc, sStreamReaderInfo->pExprInfoCalcTag,
96 ✔
1261
               sStreamReaderInfo->numOfExprCalcTag, &sStreamReaderInfo->storageApi, uid, 0, NULL);
1262
  if (code != TSDB_CODE_SUCCESS) {
96 ✔
1263
    ST_TASK_WLOG("%s cacheTag calc failed for uid:%" PRId64 ",code:%d", __func__, uid, code);
×
1264
    code = TSDB_CODE_SUCCESS;
×
1265
    goto end;
×
1266
  }
1267

1268
  skipped = false;
96 ✔
1269
  code = parseRollupPath(path, pathLen, &rollupPath, &skipped);
96 ✔
1270
  if (skipped) {
96 ✔
1271
    goto end;
×
1272
  }
1273
  if (code != TSDB_CODE_SUCCESS) {
96 ✔
1274
    if (code == TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH) {
15 ✔
1275
      setRollupIllegalPathInfo(sStreamReaderInfo, uid, path, pathLen);
15 ✔
1276
    }
1277
    ST_TASK_ELOG("%s parse rollup tag failed for uid:%" PRId64 ", value:%s, code:%d", __func__, uid, path, code);
15 ✔
1278
    goto end;
15 ✔
1279
  }
1280

1281
  code = buildRollupPrefixes(&rollupPath, &prefixes);
81 ✔
1282
  if (code == TSDB_CODE_SUCCESS && sStreamReaderInfo->pTagCond != NULL) {
81 ✔
1283
    code = createRollupPrefixFilterBlock(sStreamReaderInfo, &mr, pTagFilterCols, &pFilterBlock);
×
1284
  }
1285
  if (code == TSDB_CODE_SUCCESS && sStreamReaderInfo->pTagCond != NULL) {
81 ✔
1286
    pFilterBlockList = taosArrayInit(1, POINTER_BYTES);
×
1287
    if (pFilterBlockList == NULL) {
×
1288
      code = terrno;
×
1289
    } else if (taosArrayPush(pFilterBlockList, &pFilterBlock) == NULL) {
×
1290
      code = terrno;
×
1291
    }
1292
  }
1293
  if (code == TSDB_CODE_SUCCESS) {
81 ✔
1294
    int32_t numOfPrefixes = taosArrayGetSize(prefixes);
81 ✔
1295
    for (int32_t j = 0; j < numOfPrefixes; ++j) {
296 ✔
1296
      char* prefix = *(char**)taosArrayGet(prefixes, j);
215 ✔
1297
      bool  passFilter = true;
215 ✔
1298
      code = rollupPrefixPassesTagFilter(sStreamReaderInfo, pFilterBlock, pFilterBlockList, prefix,
215 ✔
1299
                                         (int32_t)strlen(prefix), &passFilter);
215 ✔
1300
      if (code != TSDB_CODE_SUCCESS) {
215 ✔
1301
        break;
×
1302
      }
1303
      if (!passFilter) {
215 ✔
1304
        continue;
×
1305
      }
1306
      uint64_t gid = computeRollupGid(sStreamReaderInfo->suid, prefix);
215 ✔
1307
      if (gid == 0) {
215 ✔
1308
        code = TSDB_CODE_OUT_OF_MEMORY;
×
1309
        break;
×
1310
      }
1311
      if (tableInfo != NULL) {
215 ✔
1312
        code = qStreamSetTableList(tableInfo, uid, gid);
212 ✔
1313
        if (code != TSDB_CODE_SUCCESS) {
212 ✔
1314
          break;
×
1315
        }
1316
      }
1317
      if (pTableList != NULL) {
215 ✔
1318
        STableKeyInfo keyInfo = {.uid = uid, .groupId = gid};
3 ✔
1319
        if (taosArrayPush(pTableList, &keyInfo) == NULL) {
3 ✔
1320
          code = terrno;
×
1321
          break;
×
1322
        }
1323
      }
1324
      code = putRollupGroupValue(sStreamReaderInfo, gid, prefix, (int32_t)strlen(prefix));
215 ✔
1325
      if (code != TSDB_CODE_SUCCESS) {
215 ✔
1326
        break;
×
1327
      }
1328
    }
1329
  }
1330

1331
end:
99 ✔
1332
  blockDataDestroy(pFilterBlock);
99 ✔
1333
  taosArrayDestroy(pFilterBlockList);
99 ✔
1334
  taosArrayDestroyP(prefixes, NULL);
99 ✔
1335
  destroyRollupPath(&rollupPath);
99 ✔
1336
  taosMemoryFree(path);
99 ✔
1337
  api->metaReaderFn.clearReader(&mr);
99 ✔
1338
  return code;
99 ✔
1339
}
1340

1341
static int32_t qTransformRollupStreamTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, void* pTableListInfo,
68 ✔
1342
                                               StreamTableListInfo* tableInfo) {
1343
  int32_t code = TSDB_CODE_SUCCESS;
68 ✔
1344
  SArray* pList = qStreamGetTableListArray(pTableListInfo);
68 ✔
1345
  SArray* pTagFilterCols = NULL;
68 ✔
1346

1347
  if (sStreamReaderInfo->pTagCond != NULL) {
68 ✔
1348
    code = qGetColumnsFromNodeList(sStreamReaderInfo->pTagCond, false, &pTagFilterCols);
×
1349
    if (code != TSDB_CODE_SUCCESS) {
×
1350
      goto end;
×
1351
    }
1352
  }
1353

1354
  int32_t totalSize = taosArrayGetSize(pList);
68 ✔
1355
  for (int32_t i = 0; i < totalSize; ++i) {
151 ✔
1356
    STableKeyInfo* info = taosArrayGet(pList, i);
97 ✔
1357
    if (info == NULL) {
97 ✔
1358
      continue;
×
1359
    }
1360

1361
    code = appendRollupGroupsForUid(sStreamReaderInfo, info->uid, NULL, tableInfo, pTagFilterCols);
97 ✔
1362
    if (code != TSDB_CODE_SUCCESS) {
97 ✔
1363
      goto end;
14 ✔
1364
    }
1365
  }
1366

1367
end:
68 ✔
1368
  taosArrayDestroy(pTagFilterCols);
68 ✔
1369
  return code;
68 ✔
1370
}
1371

1372
static int32_t buildRollupTableListFromSource(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* pSourceTableList,
2 ✔
1373
                                              SArray** ppTableList) {
1374
  int32_t code = TSDB_CODE_SUCCESS;
2 ✔
1375
  SArray* pTableList = NULL;
2 ✔
1376
  SArray* pTagFilterCols = NULL;
2 ✔
1377

1378
  *ppTableList = NULL;
2 ✔
1379
  pTableList = taosArrayInit(TMAX(taosArrayGetSize(pSourceTableList), 1), sizeof(STableKeyInfo));
2 ✔
1380
  if (pTableList == NULL) {
2 ✔
1381
    return terrno;
×
1382
  }
1383

1384
  if (sStreamReaderInfo->pTagCond != NULL) {
2 ✔
1385
    code = qGetColumnsFromNodeList(sStreamReaderInfo->pTagCond, false, &pTagFilterCols);
×
1386
    if (code != TSDB_CODE_SUCCESS) {
×
1387
      goto end;
×
1388
    }
1389
  }
1390
  int32_t totalSize = taosArrayGetSize(pSourceTableList);
2 ✔
1391
  for (int32_t i = 0; i < totalSize; ++i) {
3 ✔
1392
    STableKeyInfo* info = taosArrayGet(pSourceTableList, i);
2 ✔
1393
    if (info == NULL) {
2 ✔
1394
      continue;
×
1395
    }
1396

1397
    code = appendRollupGroupsForUid(sStreamReaderInfo, info->uid, pTableList, NULL, pTagFilterCols);
2 ✔
1398
    if (code != TSDB_CODE_SUCCESS) {
2 ✔
1399
      goto end;
1 ✔
1400
    }
1401
  }
1402

1403
  *ppTableList = pTableList;
1 ✔
1404
  pTableList = NULL;
1 ✔
1405

1406
end:
2 ✔
1407
  taosArrayDestroy(pTagFilterCols);
2 ✔
1408
  taosArrayDestroy(pTableList);
2 ✔
1409
  return code;
2 ✔
1410
}
1411

1412
static int32_t generateTablistForStreamReader(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo) {
1,688 ✔
1413
  int32_t                   code = 0;
1,688 ✔
1414
  int32_t                   lino = 0;
1,688 ✔
1415
  SNodeList* groupNew = NULL;   
1,688 ✔
1416
  void* pTableListInfo = NULL;
1,690 ✔
1417

1418
  if (!sStreamReaderInfo->isRollupReader) {
1,690 ✔
1419
    STREAM_CHECK_RET_GOTO(nodesCloneList(sStreamReaderInfo->partitionCols, &groupNew));
1,622 ✔
1420
  }
1421

1422
  STREAM_CHECK_RET_GOTO(qStreamCreateTableListForReader(
1,690 ✔
1423
      pVnode, sStreamReaderInfo->suid, sStreamReaderInfo->uid, sStreamReaderInfo->tableType, groupNew, true,
1424
      sStreamReaderInfo->pTagCond, sStreamReaderInfo->pTagIndexCond, &sStreamReaderInfo->storageApi, &pTableListInfo,
1425
      sStreamReaderInfo->isRollupReader ? NULL : sStreamReaderInfo->groupIdMap));
14 ✔
1426

1427
  if (sStreamReaderInfo->isRollupReader) {
1,690 ✔
1428
    STREAM_CHECK_RET_GOTO(
68 ✔
1429
        qTransformRollupStreamTableList(sStreamReaderInfo, pTableListInfo, &sStreamReaderInfo->tableList));
×
1430
    logRollupGroupSummary(sStreamReaderInfo);
54 ✔
1431
  } else {
1432
    STREAM_CHECK_RET_GOTO(qTransformStreamTableList(sStreamReaderInfo, pTableListInfo, &sStreamReaderInfo->tableList));
1,622 ✔
1433
  }
1434

1435
  void* pTask = sStreamReaderInfo->pTask;
1,676 ✔
1436
  ST_TASK_DLOG("vgId:%d %s tablelist size:%" PRIzu, TD_VID(pVnode), __func__, taosArrayGetSize(sStreamReaderInfo->tableList.pTableList));
1,676 ✔
1437
end:
1,676 ✔
1438
  nodesDestroyList(groupNew);
1,690 ✔
1439
  qStreamDestroyTableList(pTableListInfo);
1,690 ✔
1440
  STREAM_PRINT_LOG_END(code, lino);
1,689 ✔
1441
  return code;
1,690 ✔
1442
}
1443

1444
static int32_t buildVTableInfoRsp(const SStreamMsgVTableInfo* rsp, void** data, size_t* size) {
311 ✔
1445
  int32_t code = 0;
311 ✔
1446
  int32_t lino = 0;
311 ✔
1447
  void*   buf = NULL;
311 ✔
1448
  int32_t len = tSerializeSStreamMsgVTableInfo(NULL, 0, rsp);
311 ✔
1449
  STREAM_CHECK_CONDITION_GOTO(len <= 0, TSDB_CODE_INVALID_PARA);
311 ✔
1450
  buf = rpcMallocCont(len);
311 ✔
1451
  STREAM_CHECK_NULL_GOTO(buf, terrno);
311 ✔
1452
  int32_t actLen = tSerializeSStreamMsgVTableInfo(buf, len, rsp);
311 ✔
1453
  STREAM_CHECK_CONDITION_GOTO(actLen != len, TSDB_CODE_INVALID_PARA);
311 ✔
1454
  *data = buf;
311 ✔
1455
  *size = len;
311 ✔
1456
  buf = NULL;
311 ✔
1457
end:
311 ✔
1458
  rpcFreeCont(buf);
311 ✔
1459
  return code;
311 ✔
1460
}
1461

1462
static int32_t buildTsRsp(const SStreamTsResponse* tsRsp, void** data, size_t* size) {
2,544 ✔
1463
  int32_t code = 0;
2,544 ✔
1464
  int32_t lino = 0;
2,544 ✔
1465
  void*   buf = NULL;
2,544 ✔
1466
  int32_t len = tSerializeSStreamTsResponse(NULL, 0, tsRsp);
2,544 ✔
1467
  STREAM_CHECK_CONDITION_GOTO(len <= 0, TSDB_CODE_INVALID_PARA);
2,547 ✔
1468
  buf = rpcMallocCont(len);
2,547 ✔
1469
  STREAM_CHECK_NULL_GOTO(buf, terrno);
2,544 ✔
1470
  int32_t actLen = tSerializeSStreamTsResponse(buf, len, tsRsp);
2,544 ✔
1471
  STREAM_CHECK_CONDITION_GOTO(actLen != len, TSDB_CODE_INVALID_PARA);
2,545 ✔
1472
  *data = buf;
2,545 ✔
1473
  *size = len;
2,545 ✔
1474
  buf = NULL;
2,545 ✔
1475
end:
2,545 ✔
1476
  rpcFreeCont(buf);
2,545 ✔
1477
  return code;
2,547 ✔
1478
}
1479

1480

1481
static int32_t buildRsp(SSDataBlock* pBlock, void** data, size_t* size) {
36,503 ✔
1482
  int32_t code = 0;
36,503 ✔
1483
  int32_t lino = 0;
36,503 ✔
1484
  void*   buf = NULL;
36,503 ✔
1485
  STREAM_CHECK_CONDITION_GOTO(pBlock == NULL || pBlock->info.rows == 0, TSDB_CODE_SUCCESS);
36,503 ✔
1486
  size_t dataEncodeSize = blockGetEncodeSize(pBlock);
7,466 ✔
1487
  buf = rpcMallocCont(dataEncodeSize);
7,466 ✔
1488
  STREAM_CHECK_NULL_GOTO(buf, terrno);
7,466 ✔
1489
  int32_t actualLen = blockEncode(pBlock, buf, dataEncodeSize, taosArrayGetSize(pBlock->pDataBlock));
7,466 ✔
1490
  STREAM_CHECK_CONDITION_GOTO(actualLen < 0, terrno);
7,466 ✔
1491
  *data = buf;
7,466 ✔
1492
  *size = dataEncodeSize;
7,466 ✔
1493
  buf = NULL;
7,466 ✔
1494
end:
36,503 ✔
1495
  rpcFreeCont(buf);
36,503 ✔
1496
  return code;
36,503 ✔
1497
}
1498

1499
static int32_t buildArrayRsp(SArray* pBlockList, void** data, size_t* size) {
355 ✔
1500
  int32_t code = 0;
355 ✔
1501
  int32_t lino = 0;
355 ✔
1502

1503
  void*   buf = NULL;
355 ✔
1504

1505
  int32_t blockNum = 0;
355 ✔
1506
  size_t  dataEncodeBufSize = 0;
355 ✔
1507
  for(size_t i = 0; i < taosArrayGetSize(pBlockList); i++){
768 ✔
1508
    SSDataBlock* pBlock = taosArrayGetP(pBlockList, i);
413 ✔
1509
    if (pBlock == NULL || pBlock->info.rows == 0) continue;
413 ✔
1510
    int32_t blockSize = blockGetEncodeSize(pBlock);
413 ✔
1511
    dataEncodeBufSize += blockSize;
413 ✔
1512
    blockNum++;
413 ✔
1513
  }
1514
  buf = rpcMallocCont(INT_BYTES + dataEncodeBufSize);
355 ✔
1515
  STREAM_CHECK_NULL_GOTO(buf, terrno);
355 ✔
1516

1517
  char* dataBuf = (char*)buf;
355 ✔
1518
  *((int32_t*)(dataBuf)) = blockNum;
355 ✔
1519
  dataBuf += INT_BYTES;
355 ✔
1520
  for(size_t i = 0; i < taosArrayGetSize(pBlockList); i++){
768 ✔
1521
    SSDataBlock* pBlock = taosArrayGetP(pBlockList, i);
413 ✔
1522
    if (pBlock == NULL || pBlock->info.rows == 0) continue;
413 ✔
1523
    int32_t actualLen = blockEncode(pBlock, dataBuf, dataEncodeBufSize, taosArrayGetSize(pBlock->pDataBlock));
413 ✔
1524
    STREAM_CHECK_CONDITION_GOTO(actualLen < 0, terrno);
413 ✔
1525
    dataBuf += actualLen;
413 ✔
1526
  }
1527
  *data = buf;
355 ✔
1528
  *size = INT_BYTES + dataEncodeBufSize;
355 ✔
1529
  buf = NULL;
355 ✔
1530
end:
355 ✔
1531
  rpcFreeCont(buf);
355 ✔
1532
  return code;
355 ✔
1533
}
1534

1535
static int32_t buildWalMetaBlock(SSDataBlock* pBlock, int8_t type, int64_t id, bool isVTable, int64_t uid,
×
1536
                                 int64_t skey, int64_t ekey, int64_t ver, int64_t rows) {
1537
  int32_t code = 0;
×
1538
  int32_t lino = 0;
×
1539
  int32_t index = 0;
×
1540
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &type));
×
1541
  if (!isVTable) {
×
1542
    STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
×
1543
  }
1544
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &uid));
×
1545
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &skey));
×
1546
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ekey));
×
1547
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
×
1548
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &rows));
×
1549

1550
end:
×
1551
  // STREAM_PRINT_LOG_END(code, lino)
1552
  return code;
×
1553
}
1554

1555
static int32_t buildWalMetaBlockNew(SSDataBlock* pBlock, int64_t id, int64_t skey, int64_t ekey, int64_t ver,
20,733 ✔
1556
                                    int32_t rollupTbCount) {
1557
  int32_t code = 0;
20,733 ✔
1558
  int32_t lino = 0;
20,733 ✔
1559
  STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_GID_COL, &id));
20,733 ✔
1560
  STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_SKEY_COL, &skey));
20,735 ✔
1561
  STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_EKEY_COL, &ekey));
20,726 ✔
1562
  STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_VER_COL, &ver));
20,721 ✔
1563
  if (blockDataGetNumOfCols(pBlock) > STREAM_WAL_META_ROLLUP_TBCOUNT_COL) {
20,718 ✔
1564
    STREAM_CHECK_RET_GOTO(addColData(pBlock, STREAM_WAL_META_ROLLUP_TBCOUNT_COL, &rollupTbCount));
225 ✔
1565
  }
1566

1567
end:
20,722 ✔
1568
  return code;
20,722 ✔
1569
}
1570

1571
static int32_t buildTableBlock(SSDataBlock* pBlock, int64_t id, int64_t ver, ETableBlockType type) {
34 ✔
1572
  int32_t code = 0;
34 ✔
1573
  int32_t lino = 0;
34 ✔
1574
  int32_t index = 0;
34 ✔
1575
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &id));
34 ✔
1576
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &ver));
34 ✔
1577
  STREAM_CHECK_RET_GOTO(addColData(pBlock, index++, &type));
34 ✔
1578

1579
end:
34 ✔
1580
  return code;
34 ✔
1581
}
1582

1583
static void buildTSchema(STSchema* pTSchema, int32_t ver, col_id_t colId, int8_t type, int32_t bytes) {
×
1584
  pTSchema->numOfCols = 1;
×
1585
  pTSchema->version = ver;
×
1586
  pTSchema->columns[0].colId = colId;
×
1587
  pTSchema->columns[0].type = type;
×
1588
  pTSchema->columns[0].bytes = bytes;
×
1589
}
×
1590

1591
static int32_t scanDeleteDataNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len,
131 ✔
1592
                              int64_t ver) {
1593
  int32_t    code = 0;
131 ✔
1594
  int32_t    lino = 0;
131 ✔
1595
  SDecoder   decoder = {0};
131 ✔
1596
  SDeleteRes req = {0};
131 ✔
1597
  void* pTask = sStreamReaderInfo->pTask;
131 ✔
1598

1599
  req.uidList = taosArrayInit(0, sizeof(tb_uid_t));
131 ✔
1600
  tDecoderInit(&decoder, data, len);
131 ✔
1601
  STREAM_CHECK_RET_GOTO(tDecodeDeleteRes(&decoder, &req));
131 ✔
1602
  STREAM_CHECK_CONDITION_GOTO((sStreamReaderInfo->tableType == TSDB_SUPER_TABLE && !sStreamReaderInfo->isVtableStream && req.suid != sStreamReaderInfo->suid), TDB_CODE_SUCCESS);
131 ✔
1603

1604
  for (int32_t i = 0; i < taosArrayGetSize(req.uidList); i++) {
189 ✔
1605
    uint64_t* uid = taosArrayGet(req.uidList, i);
107 ✔
1606
    STREAM_CHECK_NULL_GOTO(uid, terrno);
107 ✔
1607
    uint64_t   id = 0;
107 ✔
1608
    ST_TASK_DLOG("stream reader scan delete start data:uid %" PRIu64 ", skey %" PRIu64 ", ekey %" PRIu64, *uid, req.skey, req.ekey);
107 ✔
1609
    STREAM_CHECK_CONDITION_GOTO(!uidInTableListSet(sStreamReaderInfo, req.suid, *uid, &id, false), TDB_CODE_SUCCESS);
107 ✔
1610
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->deleteBlock, ((SSDataBlock*)rsp->deleteBlock)->info.rows + 1));
81 ✔
1611
    STREAM_CHECK_RET_GOTO(buildWalMetaBlockNew(rsp->deleteBlock, id, req.skey, req.ekey, ver, 0));
81 ✔
1612
    ((SSDataBlock*)rsp->deleteBlock)->info.rows++;
81 ✔
1613
    rsp->totalRows++;
81 ✔
1614
  }
1615

1616
end:
131 ✔
1617
  taosArrayDestroy(req.uidList);
131 ✔
1618
  tDecoderClear(&decoder);
131 ✔
1619
  return code;
131 ✔
1620
}
1621

1622
static int32_t createBlockForProcessMeta(SSDataBlock** pBlock) {
30 ✔
1623
  int32_t code = 0;
30 ✔
1624
  int32_t lino = 0;
30 ✔
1625
  SArray* schemas = NULL;
30 ✔
1626

1627
  schemas = taosArrayInit(8, sizeof(SSchema));
30 ✔
1628
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
30 ✔
1629

1630
  int32_t index = 0;
30 ✔
1631
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // gid non vtable/uid vtable
30 ✔
1632
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // ver
30 ✔
1633
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_TINYINT, CHAR_BYTES, index++))  // type
30 ✔
1634

1635
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
30 ✔
1636

1637
end:
30 ✔
1638
  taosArrayDestroy(schemas);
30 ✔
1639
  return code;
30 ✔
1640
}
1641

1642
static int32_t addOneRow(void** tmp, int64_t id, int64_t ver, ETableBlockType type) {
34 ✔
1643
  int32_t  code = 0;
34 ✔
1644
  int32_t  lino = 0;
34 ✔
1645
  if (*tmp == NULL) {
34 ✔
1646
    STREAM_CHECK_RET_GOTO(createBlockForProcessMeta((SSDataBlock**)tmp));
30 ✔
1647
  }
1648
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(*tmp, ((SSDataBlock*)(*tmp))->info.rows + 1));
34 ✔
1649
  STREAM_CHECK_RET_GOTO(buildTableBlock(*tmp, id, ver, type));
34 ✔
1650
  ((SSDataBlock*)(*tmp))->info.rows++;
34 ✔
1651
  
1652
end:
34 ✔
1653
  return code;
34 ✔
1654
}
1655

1656
int32_t addUidListToBlock(SArray* uidListAdd, void** block, int64_t ver, int32_t* totalRows, ETableBlockType type) {
232 ✔
1657
  for (int32_t i = 0; i < taosArrayGetSize(uidListAdd); ++i) {
266 ✔
1658
    uint64_t* uid = taosArrayGet(uidListAdd, i);
34 ✔
1659
    if (uid == NULL) {
34 ✔
1660
      continue;
×
1661
    }
1662
    int32_t code = addOneRow(block, *uid, ver, type);
34 ✔
1663
    if (code != 0) {
34 ✔
1664
      return code;
×
1665
    }
1666
    (*totalRows)++;
34 ✔
1667
  }
1668
  return 0;
232 ✔
1669
}
1670

1671
static int32_t qStreamGetAddTable(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* tableListAdd, SArray* uidListAdd) {
449 ✔
1672
  int32_t      code = 0;
449 ✔
1673
  int32_t      lino = 0;
449 ✔
1674
  if (uidListAdd == NULL) {
449 ✔
1675
    return 0;
411 ✔
1676
  }
1677
  void* pTask = sStreamReaderInfo->pTask;
38 ✔
1678
  
1679
  taosRLockLatch(&sStreamReaderInfo->lock);
38 ✔
1680
  int32_t totalSize = taosArrayGetSize(tableListAdd);
38 ✔
1681
  for (int32_t i = 0; i < totalSize; ++i) {
41 ✔
1682
    STableKeyInfo* info = taosArrayGet(tableListAdd, i);
3 ✔
1683
    if (info == NULL) {
3 ✔
1684
      continue;
×
1685
    }
1686
    if (taosHashGet(sStreamReaderInfo->tableList.uIdMap, &info->uid, LONG_BYTES) != NULL) {
3 ✔
1687
      continue;
×
1688
    }
1689
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidListAdd, &info->uid), terrno);
6 ✔
1690
    ST_TASK_WLOG("%s real add table to list for uid:%" PRId64, __func__, info->uid);
3 ✔
1691
  }
1692

1693
end:
38 ✔
1694
  taosRUnLockLatch(&sStreamReaderInfo->lock);
38 ✔
1695
  return code;
38 ✔
1696
}
1697

1698
static int32_t qStreamGetDelTable(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* tableListDel, SArray* uidListDel) {
221 ✔
1699
  int32_t      code = 0;
221 ✔
1700
  int32_t      lino = 0;
221 ✔
1701
  if (uidListDel == NULL) {
221 ✔
1702
    return 0;
×
1703
  }
1704
  void* pTask = sStreamReaderInfo->pTask;
221 ✔
1705
  
1706
  taosRLockLatch(&sStreamReaderInfo->lock);
221 ✔
1707
  int32_t totalSize = taosArrayGetSize(tableListDel);
221 ✔
1708
  for (int32_t i = 0; i < totalSize; ++i) {
225 ✔
1709
    int64_t* uid = taosArrayGet(tableListDel, i);
4 ✔
1710
    if (uid == NULL) {
4 ✔
1711
      continue;
×
1712
    }
1713
    
1714
    if (taosHashGet(sStreamReaderInfo->tableList.uIdMap, uid, LONG_BYTES) == NULL) {
4 ✔
1715
      continue;
×
1716
    }
1717
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidListDel, uid), terrno);
4 ✔
1718
    ST_TASK_WLOG("%s real del table from list for uid:%" PRId64, __func__, *uid);
4 ✔
1719
  }
1720

1721
end:
221 ✔
1722
  taosRUnLockLatch(&sStreamReaderInfo->lock);
221 ✔
1723
  return code;
221 ✔
1724
}
1725

1726
static int32_t scanDropTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len,
78 ✔
1727
                             int64_t ver) {
1728
  int32_t  code = 0;
78 ✔
1729
  int32_t  lino = 0;
78 ✔
1730
  SDecoder decoder = {0};
78 ✔
1731
  void* pTask = sStreamReaderInfo->pTask;
78 ✔
1732
  SArray* uidList = NULL;
78 ✔
1733
  SArray* uidListDel = NULL;
78 ✔
1734
  SArray* uidListDelOutTbl = NULL;
78 ✔
1735
  SVDropTbBatchReq req = {0};
78 ✔
1736
  tDecoderInit(&decoder, data, len);
78 ✔
1737
  STREAM_CHECK_RET_GOTO(tDecodeSVDropTbBatchReq(&decoder, &req));
78 ✔
1738

1739
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
156 ✔
1740
    SVDropTbReq* pDropTbReq = req.pReqs + iReq;
78 ✔
1741
    STREAM_CHECK_NULL_GOTO(pDropTbReq, TSDB_CODE_INVALID_PARA);
78 ✔
1742
    uint64_t id = 0;
78 ✔
1743
    if(!uidInTableListOrigin(sStreamReaderInfo, pDropTbReq->suid, pDropTbReq->uid, &id)) {
78 ✔
1744
      continue;
74 ✔
1745
    }
1746

1747
    if (sStreamReaderInfo->deleteOutTbl != 0) {
4 ✔
1748
      if (uidListDelOutTbl == NULL) {
×
1749
        uidListDelOutTbl = taosArrayInit(8, sizeof(tb_uid_t));
×
1750
        STREAM_CHECK_NULL_GOTO(uidListDelOutTbl, terrno);
×
1751
      }
1752
      STREAM_CHECK_NULL_GOTO(taosArrayPush(uidListDelOutTbl, &pDropTbReq->uid), terrno);
×
1753
    }
1754
    if (sStreamReaderInfo->isVtableStream) {
4 ✔
1755
      if (uidList == NULL) {
4 ✔
1756
        uidList = taosArrayInit(8, sizeof(tb_uid_t));
4 ✔
1757
        STREAM_CHECK_NULL_GOTO(uidList, terrno);
4 ✔
1758
      }
1759
      STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pDropTbReq->uid), terrno);
8 ✔
1760
    }
1761

1762
    ST_TASK_DLOG("stream reader scan drop uid %" PRId64 ", id %" PRIu64, pDropTbReq->uid, id);
4 ✔
1763
  }
1764
  STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListDelOutTbl, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_DROP));
78 ✔
1765

1766
  if (sStreamReaderInfo->isVtableStream) {
78 ✔
1767
    uidListDel = taosArrayInit(8, sizeof(tb_uid_t));
78 ✔
1768
    STREAM_CHECK_NULL_GOTO(uidListDel, terrno);
78 ✔
1769
    STREAM_CHECK_RET_GOTO(qStreamGetDelTable(sStreamReaderInfo, uidList, uidListDel));
78 ✔
1770
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListDel, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_RETIRE));
78 ✔
1771
  }
1772

1773
end:
78 ✔
1774
  taosArrayDestroy(uidList);
78 ✔
1775
  taosArrayDestroy(uidListDel);
78 ✔
1776
  taosArrayDestroy(uidListDelOutTbl);
78 ✔
1777
  tDecoderClear(&decoder);
78 ✔
1778
  return code;
78 ✔
1779
}
1780

1781
static int32_t qStreamModifyTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* tableListAdd, SArray* tableListDel) {
478 ✔
1782
  int32_t    code = 0;
478 ✔
1783
  int32_t    lino = 0;
478 ✔
1784
  void*      pTask = sStreamReaderInfo->pTask;
478 ✔
1785
  SSHashObj* removedUidMap = NULL;
478 ✔
1786

1787
  if (isRollupMultiReader(sStreamReaderInfo)) {
478 ✔
1788
    removedUidMap = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
1 ✔
1789
    if (removedUidMap == NULL) {
1 ✔
1790
      return terrno;
×
1791
    }
1792
  }
1793

1794
  taosWLockLatch(&sStreamReaderInfo->lock);
478 ✔
1795
  int32_t totalSize = tableListDel == NULL ? 0 : taosArrayGetSize(tableListDel);
478 ✔
1796
  for (int32_t i = 0; i < totalSize; ++i) {
478 ✔
1797
    int64_t* uid = taosArrayGet(tableListDel, i);
×
1798
    if (uid == NULL) {
×
1799
      continue;
×
1800
    }
1801
    STREAM_CHECK_RET_GOTO(qStreamRemoveTableList(&sStreamReaderInfo->tableList, *uid));
×
1802
  }
1803

1804
  totalSize = tableListAdd == NULL ? 0 : taosArrayGetSize(tableListAdd);
478 ✔
1805
  for (int32_t i = 0; i < totalSize; ++i) {
658 ✔
1806
    STableKeyInfo* info = taosArrayGet(tableListAdd, i);
180 ✔
1807
    if (info == NULL) {
180 ✔
1808
      continue;
×
1809
    }
1810
    int ret = cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheTrigger,
180 ✔
1811
                       sStreamReaderInfo->pExprInfoTriggerTag, sStreamReaderInfo->numOfExprTriggerTag,
1812
                       &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
1813
    if (ret != 0) {
180 ✔
1814
      ST_TASK_WLOG("%s cacheTag trigger failed for uid:%" PRId64 ",code:%d", __func__, info->uid, ret);
×
1815
      continue;
×
1816
    }
1817
    ret =
×
1818
        cacheTag(sStreamReaderInfo->pVnode, sStreamReaderInfo->pTableMetaCacheCalc, sStreamReaderInfo->pExprInfoCalcTag,
180 ✔
1819
                 sStreamReaderInfo->numOfExprCalcTag, &sStreamReaderInfo->storageApi, info->uid, 0, NULL);
1820
    if (ret != 0) {
180 ✔
1821
      ST_TASK_WLOG("%s cacheTag calc failed for uid:%" PRId64 ",code:%d", __func__, info->uid, ret);
×
1822
      continue;
×
1823
    }
1824
    if (removedUidMap == NULL) {
180 ✔
1825
      STREAM_CHECK_RET_GOTO(qStreamRemoveTableList(&sStreamReaderInfo->tableList, info->uid));
177 ✔
1826
    } else if (tSimpleHashGet(removedUidMap, &info->uid, LONG_BYTES) == NULL) {
3 ✔
1827
      STREAM_CHECK_RET_GOTO(qStreamRemoveTableList(&sStreamReaderInfo->tableList, info->uid));
1 ✔
1828
      STREAM_CHECK_RET_GOTO(tSimpleHashPut(removedUidMap, &info->uid, LONG_BYTES, &info->uid, LONG_BYTES));
1 ✔
1829
    }
1830
    STREAM_CHECK_RET_GOTO(qStreamSetTableList(&sStreamReaderInfo->tableList, info->uid, info->groupId));
180 ✔
1831
  }
1832

1833
end:
478 ✔
1834
  taosWUnLockLatch(&sStreamReaderInfo->lock);
478 ✔
1835
  tSimpleHashCleanup(removedUidMap);
478 ✔
1836
  return code;
478 ✔
1837
}
1838

1839
static int32_t processTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* uidList, SArray** tableList) {
480 ✔
1840
  int32_t code = 0;
480 ✔
1841
  int32_t lino = 0;
480 ✔
1842
  SNodeList* groupNew = NULL;
480 ✔
1843
  SArray*    sourceTableList = NULL;
480 ✔
1844

1845
  if (taosArrayGetSize(uidList) == 0) {
480 ✔
1846
    return 0;
300 ✔
1847
  }
1848

1849
  if (!sStreamReaderInfo->isRollupReader) {
180 ✔
1850
    STREAM_CHECK_RET_GOTO(nodesCloneList(sStreamReaderInfo->partitionCols, &groupNew));
178 ✔
1851
  }
1852
  STREAM_CHECK_RET_GOTO(qStreamFilterTableListForReader(
180 ✔
1853
      sStreamReaderInfo->pVnode, uidList, groupNew, sStreamReaderInfo->pTagCond, sStreamReaderInfo->pTagIndexCond,
1854
      &sStreamReaderInfo->storageApi, sStreamReaderInfo->isRollupReader ? NULL : sStreamReaderInfo->groupIdMap,
1855
      sStreamReaderInfo->suid, sStreamReaderInfo->isRollupReader ? &sourceTableList : tableList));
×
1856
  if (sStreamReaderInfo->isRollupReader) {
179 ✔
1857
    STREAM_CHECK_RET_GOTO(buildRollupTableListFromSource(sStreamReaderInfo, sourceTableList, tableList));
2 ✔
1858
  }
1859

1860
end:
180 ✔
1861
  nodesDestroyList(groupNew);
180 ✔
1862
  taosArrayDestroy(sourceTableList);
180 ✔
1863
  return code;
180 ✔
1864
}
1865

1866
static int32_t scanCreateTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len,
307 ✔
1867
                             int64_t ver) {
1868
  int32_t  code = 0;
307 ✔
1869
  int32_t  lino = 0;
307 ✔
1870
  SDecoder decoder = {0};
307 ✔
1871
  SArray*  uidList = NULL;
307 ✔
1872
  SArray*  tableList = NULL;
307 ✔
1873
  SArray*  uidListAdd = NULL;
307 ✔
1874
  void* pTask = sStreamReaderInfo->pTask;
307 ✔
1875

1876
  SVCreateTbBatchReq req = {0};
307 ✔
1877
  tDecoderInit(&decoder, data, len);
307 ✔
1878
  
1879
  STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbBatchReq(&decoder, &req));
307 ✔
1880

1881
  uidList = taosArrayInit(8, sizeof(tb_uid_t));
307 ✔
1882
  STREAM_CHECK_NULL_GOTO(uidList, terrno);
307 ✔
1883

1884
  if (sStreamReaderInfo->isVtableStream) {
307 ✔
1885
    uidListAdd = taosArrayInit(8, sizeof(tb_uid_t));
27 ✔
1886
    STREAM_CHECK_NULL_GOTO(uidListAdd, terrno);
27 ✔
1887
  }
1888

1889
  SVCreateTbReq* pCreateReq = NULL;
307 ✔
1890
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
614 ✔
1891
    pCreateReq = req.pReqs + iReq;
307 ✔
1892
    if (!needReLoadTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false)) {
307 ✔
1893
      ST_TASK_DLOG("stream reader scan create table jump, %s", pCreateReq->name);
262 ✔
1894
      continue;
262 ✔
1895
    }
1896
    ST_TASK_ILOG("stream reader scan create table %s", pCreateReq->name);
45 ✔
1897
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pCreateReq->uid), terrno);
90 ✔
1898
  }
1899
  
1900
  STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo, uidList, &tableList));
307 ✔
1901
  STREAM_CHECK_RET_GOTO(qStreamGetAddTable(sStreamReaderInfo, tableList, uidListAdd));
306 ✔
1902
  if (sStreamReaderInfo->isVtableStream) {
306 ✔
1903
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
27 ✔
1904
  }
1905

1906
  STREAM_CHECK_RET_GOTO(qStreamModifyTableList(sStreamReaderInfo, tableList, uidList));
306 ✔
1907
end:
307 ✔
1908
  taosArrayDestroy(uidList);
307 ✔
1909
  taosArrayDestroy(uidListAdd);
307 ✔
1910
  taosArrayDestroy(tableList);
307 ✔
1911
  tDeleteSVCreateTbBatchReq(&req);
307 ✔
1912
  tDecoderClear(&decoder);
307 ✔
1913
  return code;
307 ✔
1914
}
1915

1916
static int32_t processAutoCreateTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SVCreateTbReq* pCreateReq, int64_t ver) {
11,344 ✔
1917
  int32_t  code = 0;
11,344 ✔
1918
  int32_t  lino = 0;
11,344 ✔
1919
  void*    pTask = sStreamReaderInfo->pTask;
11,344 ✔
1920
  SArray*  uidList = NULL;
11,344 ✔
1921
  SArray*  tableList = NULL;
11,344 ✔
1922

1923
  ST_TASK_DLOG("%s start, name:%s uid:%"PRId64, __func__, pCreateReq->name, pCreateReq->uid);
11,345 ✔
1924
  if (!needReLoadTableList(sStreamReaderInfo, pCreateReq->type, pCreateReq->ctb.suid, pCreateReq->uid, false) ||
11,377 ✔
1925
      ignoreMetaChange(pTask, sStreamReaderInfo->tableList.version, ver)) {
32 ✔
1926
    ST_TASK_DLOG("stream reader scan auto create table jump, %s", pCreateReq->name);
11,317 ✔
1927
    goto end;
11,317 ✔
1928
  }
1929
  uidList = taosArrayInit(8, sizeof(tb_uid_t));
30 ✔
1930
  STREAM_CHECK_NULL_GOTO(uidList, terrno);
30 ✔
1931
  STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, &pCreateReq->uid), terrno);
60 ✔
1932
  ST_TASK_DLOG("stream reader scan auto create table %s", pCreateReq->name);
30 ✔
1933

1934
  STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo, uidList, &tableList));
30 ✔
1935
  STREAM_CHECK_RET_GOTO(qStreamModifyTableList(sStreamReaderInfo, tableList, uidList));
29 ✔
1936
end:
11,254 ✔
1937
  taosArrayDestroy(uidList);
11,347 ✔
1938
  taosArrayDestroy(tableList);
11,346 ✔
1939
  return code;
11,346 ✔
1940
}
1941

1942
static bool isColIdInList(SNodeList* colList, col_id_t cid){
4 ✔
1943
  int32_t  code = 0;
4 ✔
1944
  int32_t  lino = 0;
4 ✔
1945
  SNode*  nodeItem = NULL;
4 ✔
1946
  FOREACH(nodeItem, colList) {
13 ✔
1947
    SNode*           pNode = ((STargetNode*)nodeItem)->pExpr;
13 ✔
1948
    if (nodeType(pNode) == QUERY_NODE_COLUMN) {
13 ✔
1949
      SColumnNode*     valueNode = (SColumnNode*)(pNode);
13 ✔
1950
      if (cid == valueNode->colId) {
13 ✔
1951
        return true;
4 ✔
1952
      }
1953
    }
1954
  }
1955
end:
×
1956
  return false;
×
1957
}
1958

1959
static bool isAlteredTable(int8_t action, ETableType tbType) {
149 ✔
1960
  if (action == TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL && tbType == TSDB_CHILD_TABLE) {
149 ✔
1961
    return true;
143 ✔
1962
  } else if (action == TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL && tbType == TSDB_SUPER_TABLE) {
6 ✔
1963
    return true;
×
1964
  } else if ((action == TSDB_ALTER_TABLE_ALTER_COLUMN_REF || action == TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) && 
6 ✔
1965
     (tbType == TSDB_VIRTUAL_CHILD_TABLE || tbType == TSDB_VIRTUAL_NORMAL_TABLE)) {
3 ✔
1966
    return true;
6 ✔
1967
  }
1968
  return false;
×
1969
}
1970

1971
void getAlterColId(void* pVnode, int64_t uid, const char* colName, col_id_t* colId) {
4 ✔
1972
  SSchemaWrapper *pSchema = metaGetTableSchema(((SVnode *)pVnode)->pMeta, uid, -1, 1, NULL, 0, false);
4 ✔
1973
  if (pSchema == NULL) {
4 ✔
1974
    return;
×
1975
  }
1976
  for (int32_t i = 0; i < pSchema->nCols; i++) {
13 ✔
1977
    if (strncmp(pSchema->pSchema[i].name, colName, TSDB_COL_NAME_LEN) == 0) {
13 ✔
1978
      *colId = pSchema->pSchema[i].colId;
4 ✔
1979
      break;
4 ✔
1980
    }
1981
  }
1982
  tDeleteSchemaWrapper(pSchema);
×
1983
  return;
4 ✔
1984
}
1985

1986
// Handle TSDB_ALTER_TABLE_ALTER_COLUMN_REF and TSDB_ALTER_TABLE_REMOVE_COLUMN_REF
1987
static int32_t scanAlterTableColumnRef(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, 
5 ✔
1988
                                       SVAlterTbReq* pReq, uint64_t uid, int64_t ver) {
1989
  int32_t code = 0;
5 ✔
1990
  int32_t lino = 0;
5 ✔
1991
  void* pTask = sStreamReaderInfo->pTask;
5 ✔
1992
  SArray* uidListAdd = NULL;
5 ✔
1993

1994
  uidListAdd = taosArrayInit(8, sizeof(tb_uid_t));
5 ✔
1995
  STREAM_CHECK_NULL_GOTO(uidListAdd, terrno);
5 ✔
1996

1997
  uint64_t id = 0;
5 ✔
1998
  STREAM_CHECK_CONDITION_GOTO(!uidInTableListOrigin(sStreamReaderInfo, sStreamReaderInfo->suid, uid, &id), TDB_CODE_SUCCESS);
5 ✔
1999

2000
  col_id_t colId = 0;
4 ✔
2001
  getAlterColId(sStreamReaderInfo->pVnode, uid, pReq->colName, &colId);
4 ✔
2002
  if (atomic_load_8(&sStreamReaderInfo->isVtableOnlyTs) == 0 && !isColIdInList(sStreamReaderInfo->triggerCols, colId)) {
4 ✔
2003
    ST_TASK_ILOG("stream reader scan alter table %s, colId %d not in trigger cols", pReq->tbName, colId);
×
2004
    goto end;
×
2005
  }
2006

2007
  STREAM_CHECK_NULL_GOTO(taosArrayPush(uidListAdd, &uid), terrno);
4 ✔
2008
  STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
4 ✔
2009

2010
  ST_TASK_DLOG("stream reader scan alter table column ref %s", pReq->tbName);
4 ✔
2011

2012
end:
5 ✔
2013
  taosArrayDestroy(uidListAdd);
5 ✔
2014
  STREAM_PRINT_LOG_END_WITHID(code, lino);
5 ✔
2015
  return code;
5 ✔
2016
}
2017

2018
static int32_t checkAlter(SStreamTriggerReaderInfo* sStreamReaderInfo, char* tbName, int8_t action, uint64_t *uid) {
149 ✔
2019
  int32_t  code = 0;
149 ✔
2020
  int32_t  lino = 0;
149 ✔
2021
  ETableType tbType = 0;
149 ✔
2022
  uint64_t suid = 0;
149 ✔
2023

2024
  STREAM_CHECK_RET_GOTO(metaGetTableTypeSuidByName(sStreamReaderInfo->pVnode, tbName, &tbType, &suid, 0));
149 ✔
2025
  STREAM_CHECK_CONDITION_GOTO(!isAlteredTable(action, tbType), TDB_CODE_SUCCESS);
149 ✔
2026
  STREAM_CHECK_CONDITION_GOTO(suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
149 ✔
2027
  if (action == TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL) {
110 ✔
2028
    *uid = suid;
×
2029
    goto end;
×
2030
  }
2031
  STREAM_CHECK_RET_GOTO(metaGetTableUidByName(sStreamReaderInfo->pVnode, tbName, uid, 0));
110 ✔
2032

2033
end:
149 ✔
2034
  return code;
149 ✔
2035
}
2036

2037
static SArray* getTableListForAlterSuperTable(SStreamTriggerReaderInfo* sStreamReaderInfo, SVAlterTbReq* pReq){
143 ✔
2038
  int32_t code = 0;
143 ✔
2039
  int32_t lino = 0;
143 ✔
2040
  void* pTask = sStreamReaderInfo->pTask;
143 ✔
2041
  SArray* uidList = taosArrayInit(8, sizeof(tb_uid_t));
143 ✔
2042
  STREAM_CHECK_NULL_GOTO(uidList, terrno);
143 ✔
2043
  for (int32_t i = 0; i < taosArrayGetSize(pReq->tables); i++) {
286 ✔
2044
    SUpdateTableTagVal *pTable = taosArrayGet(pReq->tables, i);
143 ✔
2045
    uint64_t uid = 0;
143 ✔
2046
    code = checkAlter(sStreamReaderInfo, pTable->tbName, pReq->action, &uid);
143 ✔
2047
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || uid == 0) {
143 ✔
2048
      code = 0;
38 ✔
2049
      ST_TASK_WLOG("stream reader scan alter ctable table %s does not exist, %s %"PRIu64, pTable->tbName, __func__, uid);
38 ✔
2050
      continue;
38 ✔
2051
    }
2052
    STREAM_CHECK_RET_GOTO(code);
105 ✔
2053
    STREAM_CHECK_NULL_GOTO(taosArrayPush(uidList, (const void *)&uid), terrno);
105 ✔
2054
  }
2055

2056
end:
143 ✔
2057
  if (code != 0) {
143 ✔
2058
    ST_TASK_ELOG("%s failed,code:%d", __func__, code);
×
2059
    taosArrayDestroy(uidList);
×
2060
    uidList = NULL;
×
2061
  }
2062
  return uidList;
143 ✔
2063
}
2064

2065
// Handle TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL and TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL
2066
static int32_t scanAlterTableTagVal(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, 
143 ✔
2067
                                    SArray* uidList, int64_t ver) {
2068
  int32_t code = 0;
143 ✔
2069
  int32_t lino = 0;
143 ✔
2070
  void* pTask = sStreamReaderInfo->pTask;
143 ✔
2071
  SArray* uidListAdd = NULL;
143 ✔
2072
  SArray* uidListDel = NULL;
143 ✔
2073
  SArray* tableList = NULL;
143 ✔
2074

2075
  if (sStreamReaderInfo->isVtableStream) {
143 ✔
2076
    uidListAdd = taosArrayInit(8, sizeof(tb_uid_t));
11 ✔
2077
    STREAM_CHECK_NULL_GOTO(uidListAdd, terrno);
11 ✔
2078
  }
2079

2080
  uidListDel = taosArrayInit(8, sizeof(tb_uid_t));
143 ✔
2081
  STREAM_CHECK_NULL_GOTO(uidListDel, terrno);
143 ✔
2082

2083
  STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo, uidList, &tableList));
143 ✔
2084
  STREAM_CHECK_RET_GOTO(qStreamGetDelTable(sStreamReaderInfo, uidList, uidListDel));
143 ✔
2085

2086
  if (rsp->checkAlter && taosArrayGetSize(uidListDel) > 0 && rsp->totalDataRows > 0) {
143 ✔
2087
    rsp->needReturn = true;
×
2088
    rsp->ver--;
×
2089
    ST_TASK_DLOG("%s stream reader scan alter table need return data", __func__);
×
2090
    goto end;
×
2091
  }
2092

2093
  STREAM_CHECK_RET_GOTO(qStreamGetAddTable(sStreamReaderInfo, tableList, uidListAdd));
143 ✔
2094
  if (sStreamReaderInfo->isVtableStream) {
143 ✔
2095
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListAdd, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_ADD));
11 ✔
2096
    STREAM_CHECK_RET_GOTO(addUidListToBlock(uidListDel, &rsp->tableBlock, ver, &rsp->totalRows, TABLE_BLOCK_RETIRE));
11 ✔
2097
  }
2098
  STREAM_CHECK_RET_GOTO(qStreamModifyTableList(sStreamReaderInfo, tableList, uidList));
143 ✔
2099

2100
  ST_TASK_DLOG("%s stream reader scan alter table tag val", __func__);
143 ✔
2101

2102
end:
143 ✔
2103
  taosArrayDestroy(uidListAdd);
143 ✔
2104
  taosArrayDestroy(uidListDel);
143 ✔
2105
  taosArrayDestroy(tableList);
143 ✔
2106
  STREAM_PRINT_LOG_END_WITHID(code, lino);
143 ✔
2107
  return code;
143 ✔
2108
}
2109

2110
static int32_t scanAlterTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len, int64_t ver) {
249 ✔
2111
  int32_t  code = 0;
249 ✔
2112
  int32_t  lino = 0;
249 ✔
2113
  SDecoder decoder = {0};
249 ✔
2114
  void* pTask = sStreamReaderInfo->pTask;
249 ✔
2115
  SArray* uidList = NULL;
249 ✔
2116

2117
  ST_TASK_DLOG("%s start", __func__);
249 ✔
2118

2119
  SVAlterTbReq req = {0};
249 ✔
2120
  tDecoderInit(&decoder, data, len);
249 ✔
2121
  
2122
  STREAM_CHECK_RET_GOTO(tDecodeSVAlterTbReq(&decoder, &req));
249 ✔
2123

2124
  STREAM_CHECK_CONDITION_GOTO(req.action != TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL && req.action != TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL && 
249 ✔
2125
    req.action != TSDB_ALTER_TABLE_ALTER_COLUMN_REF && req.action != TSDB_ALTER_TABLE_REMOVE_COLUMN_REF, TDB_CODE_SUCCESS);
×
2126

2127
  uint64_t uid = 0;
206 ✔
2128
  if (req.action == TSDB_ALTER_TABLE_ALTER_COLUMN_REF || req.action == TSDB_ALTER_TABLE_REMOVE_COLUMN_REF) {
206 ✔
2129
    STREAM_CHECK_CONDITION_GOTO(!sStreamReaderInfo->isVtableStream, TDB_CODE_SUCCESS);
63 ✔
2130
    code = checkAlter(sStreamReaderInfo, req.tbName, req.action, &uid);
6 ✔
2131
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || uid == 0) {
6 ✔
2132
      ST_TASK_WLOG("stream reader scan alter ref table %s does not exist, %s uid:%" PRIu64, req.tbName, __func__, uid);
1 ✔
2133
      code = 0;
1 ✔
2134
      goto end;
1 ✔
2135
    }
2136
    STREAM_CHECK_RET_GOTO(scanAlterTableColumnRef(sStreamReaderInfo, rsp, &req, uid, ver));
5 ✔
2137
  } else if (req.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL) {
143 ✔
2138
    uidList = getTableListForAlterSuperTable(sStreamReaderInfo, &req);
143 ✔
2139
    STREAM_CHECK_NULL_GOTO(uidList, terrno);
143 ✔
2140
    STREAM_CHECK_RET_GOTO(scanAlterTableTagVal(sStreamReaderInfo, rsp, uidList, ver));
143 ✔
2141
  } else if (req.action == TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL) {
×
2142
    code = checkAlter(sStreamReaderInfo, req.tbName, req.action, &uid);
×
2143
    if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || uid == 0) {
×
2144
      ST_TASK_WLOG("stream reader scan alter suid table %s does not exist, %s uid:%" PRIu64, req.tbName, __func__, uid);
×
2145
      code = 0;
×
2146
      goto end;
×
2147
    }
2148
    uidList = taosArrayInit(8, sizeof(uint64_t));
×
2149
    STREAM_CHECK_NULL_GOTO(uidList, terrno);
×
2150
    STREAM_CHECK_RET_GOTO(vnodeGetCtbIdList(sStreamReaderInfo->pVnode, uid, uidList));
×
2151
    STREAM_CHECK_RET_GOTO(scanAlterTableTagVal(sStreamReaderInfo, rsp, uidList, ver));
×
2152
  }
2153

2154
  ST_TASK_DLOG("%s stream reader scan alter table", __func__);
148 ✔
2155

2156
end:
249 ✔
2157
  destroyAlterTbReq(&req);
249 ✔
2158

2159
  taosArrayDestroy(uidList);
249 ✔
2160
  tDecoderClear(&decoder);
249 ✔
2161
  STREAM_PRINT_LOG_END_WITHID(code, lino);
249 ✔
2162
  return code;
249 ✔
2163
}
2164

2165
// static int32_t scanAlterSTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len) {
2166
//   int32_t  code = 0;
2167
//   int32_t  lino = 0;
2168
//   SDecoder decoder = {0};
2169
//   SMAlterStbReq reqAlter = {0};
2170
//   SVCreateStbReq req = {0};
2171
//   tDecoderInit(&decoder, data, len);
2172
//   void* pTask = sStreamReaderInfo->pTask;
2173
  
2174
//   STREAM_CHECK_RET_GOTO(tDecodeSVCreateStbReq(&decoder, &req));
2175
//   STREAM_CHECK_CONDITION_GOTO(req.suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
2176
//   if (req.alterOriData != 0) {
2177
//     STREAM_CHECK_RET_GOTO(tDeserializeSMAlterStbReq(req.alterOriData, req.alterOriDataLen, &reqAlter));
2178
//     STREAM_CHECK_CONDITION_GOTO(reqAlter.alterType != TSDB_ALTER_TABLE_DROP_TAG && reqAlter.alterType != TSDB_ALTER_TABLE_UPDATE_TAG_NAME, TDB_CODE_SUCCESS);
2179
//   }
2180
  
2181
//   STREAM_CHECK_RET_GOTO(processTableList(sStreamReaderInfo));
2182

2183
//   ST_TASK_DLOG("stream reader scan alter suid %" PRId64, req.suid);
2184
// end:
2185
//   tFreeSMAltertbReq(&reqAlter);
2186
//   tDecoderClear(&decoder);
2187
//   return code;
2188
// }
2189

2190
// static int32_t scanDropSTableNew(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len) {
2191
//   int32_t  code = 0;
2192
//   int32_t  lino = 0;
2193
//   SDecoder decoder = {0};
2194
//   void* pTask = sStreamReaderInfo->pTask;
2195

2196
//   SVDropStbReq req = {0};
2197
//   tDecoderInit(&decoder, data, len);
2198
//   STREAM_CHECK_RET_GOTO(tDecodeSVDropStbReq(&decoder, &req));
2199
//   STREAM_CHECK_CONDITION_GOTO(req.suid != sStreamReaderInfo->suid, TDB_CODE_SUCCESS);
2200

2201
//   ST_TASK_DLOG("stream reader scan drop suid %" PRId64, req.suid);
2202
// end:
2203
//   tDecoderClear(&decoder);
2204
//   return code;
2205
// }
2206

2207
static int32_t scanSubmitTbDataForMeta(SDecoder *pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo, SSHashObj* gidHash, int64_t ver, STimeWindow win) {
55,147 ✔
2208
  int32_t code = 0;
55,147 ✔
2209
  int32_t lino = 0;
55,147 ✔
2210
  void* pTask = sStreamReaderInfo->pTask;
55,147 ✔
2211
  WalMetaResult walMeta = {0};
55,147 ✔
2212
  SSubmitTbData submitTbData = {0};
55,150 ✔
2213
  bool          expandRollupGids = isRollupMultiReader(sStreamReaderInfo);
55,151 ✔
2214

2215
  if (tStartDecode(pCoder) < 0) {
55,139 ✔
2216
    code = TSDB_CODE_INVALID_MSG;
×
2217
    TSDB_CHECK_CODE(code, lino, end);
×
2218
  }
2219

2220
  uint8_t       version = 0;
55,392 ✔
2221
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
55,135 ✔
2222
    code = TSDB_CODE_INVALID_MSG;
×
2223
    TSDB_CHECK_CODE(code, lino, end);
×
2224
  }
2225
  version = (submitTbData.flags >> 8) & 0xff;
55,135 ✔
2226
  submitTbData.flags = submitTbData.flags & 0xff;
55,135 ✔
2227

2228
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
2229
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
55,135 ✔
2230
    submitTbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
6,584 ✔
2231
    STREAM_CHECK_NULL_GOTO(submitTbData.pCreateTbReq, terrno);
6,585 ✔
2232
    STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbReq(pCoder, submitTbData.pCreateTbReq));
6,585 ✔
2233
    STREAM_CHECK_RET_GOTO(processAutoCreateTableNew(sStreamReaderInfo, submitTbData.pCreateTbReq, ver));
6,584 ✔
2234
  }
2235

2236
  // submit data
2237
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
55,134 ✔
2238
    code = TSDB_CODE_INVALID_MSG;
×
2239
    TSDB_CHECK_CODE(code, lino, end);
×
2240
  }
2241
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
55,139 ✔
2242
    code = TSDB_CODE_INVALID_MSG;
×
2243
    TSDB_CHECK_CODE(code, lino, end);
×
2244
  }
2245

2246
  if (!uidInTableListSet(sStreamReaderInfo, submitTbData.suid, submitTbData.uid, &walMeta.id, false)) {
55,139 ✔
2247
    goto end;
42,548 ✔
2248
  }
2249
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
12,614 ✔
2250
    code = TSDB_CODE_INVALID_MSG;
×
2251
    TSDB_CHECK_CODE(code, lino, end);
×
2252
  }
2253

2254
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
12,614 ✔
2255
    uint64_t nColData = 0;
1 ✔
2256
    if (tDecodeU64v(pCoder, &nColData) < 0) {
1 ✔
2257
      code = TSDB_CODE_INVALID_MSG;
×
2258
      TSDB_CHECK_CODE(code, lino, end);
×
2259
    }
2260
    if (nColData == 0) {
1 ✔
2261
      ST_TASK_WLOG("%s stream reader scan submit data without column data", __func__);
×
2262
      goto end;
×
2263
    }
2264

2265
    SColData colData = {0};
1 ✔
2266
    code = tDecodeColData(version, pCoder, &colData, false);
1 ✔
2267
    if (code) {
1 ✔
2268
      code = TSDB_CODE_INVALID_MSG;
×
2269
      TSDB_CHECK_CODE(code, lino, end);
×
2270
    }
2271

2272
    if (colData.flag != HAS_VALUE) {
1 ✔
2273
      code = TSDB_CODE_INVALID_MSG;
×
2274
      TSDB_CHECK_CODE(code, lino, end);
×
2275
    }
2276
    if (colData.nVal == 0) {
1 ✔
2277
      ST_TASK_WLOG("%s stream reader scan submit data without rows", __func__);
×
2278
      goto end;
×
2279
    }
2280
    walMeta.skey = ((TSKEY *)colData.pData)[0];
1 ✔
2281
    walMeta.ekey = ((TSKEY *)colData.pData)[colData.nVal - 1];
1 ✔
2282

2283
    for (uint64_t i = 1; i < nColData; i++) {
15 ✔
2284
      code = tDecodeColData(version, pCoder, &colData, true);
14 ✔
2285
      if (code) {
14 ✔
2286
        code = TSDB_CODE_INVALID_MSG;
×
2287
        TSDB_CHECK_CODE(code, lino, end);
×
2288
      }
2289
    }
2290
  } else {
2291
    uint64_t nRow = 0;
12,613 ✔
2292
    if (tDecodeU64v(pCoder, &nRow) < 0) {
12,613 ✔
2293
      code = TSDB_CODE_INVALID_MSG;
×
2294
      TSDB_CHECK_CODE(code, lino, end);
×
2295
    }
2296
    if (nRow == 0) {
12,613 ✔
2297
      ST_TASK_WLOG("%s stream reader scan submit data without rows", __func__);
×
2298
      goto end;
×
2299
    }
2300

2301
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
36,109 ✔
2302
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
23,496 ✔
2303
      pCoder->pos += pRow->len;
23,496 ✔
2304
      if (iRow == 0){
23,496 ✔
2305
#ifndef NO_UNALIGNED_ACCESS
2306
        walMeta.skey = pRow->ts;
12,612 ✔
2307
#else
2308
        walMeta.skey = taosGetInt64Aligned(&pRow->ts);
2309
#endif
2310
      }
2311
      if (iRow == nRow - 1) {
23,496 ✔
2312
#ifndef NO_UNALIGNED_ACCESS
2313
        walMeta.ekey = pRow->ts;
12,613 ✔
2314
#else
2315
        walMeta.ekey = taosGetInt64Aligned(&pRow->ts);
2316
#endif
2317
      }
2318
    }
2319
  }
2320

2321
  if (walMeta.skey > walMeta.ekey || walMeta.skey < win.skey || walMeta.ekey > win.ekey) {
12,614 ✔
2322
    ST_TASK_WLOG("%s stream reader scan data invalid, walMeta time:%" PRId64 ",%" PRId64 ". win time:%" PRId64 ",%" PRId64, __func__,
2 ✔
2323
      walMeta.skey, walMeta.ekey, win.skey, win.ekey);
2324
    goto end;
×
2325
  }
2326

2327
  if (expandRollupGids) {
12,612 ✔
2328
    STREAM_CHECK_RET_GOTO(putWalMetaResultForRollupUid(sStreamReaderInfo, gidHash, submitTbData.suid, submitTbData.uid,
75 ✔
2329
                                                       walMeta.skey, walMeta.ekey));
2330
  } else {
2331
    STREAM_CHECK_RET_GOTO(putWalMetaResult(gidHash, walMeta.id, walMeta.skey, walMeta.ekey));
12,537 ✔
2332
  }
2333

2334
end:
54,940 ✔
2335
  tdDestroySVCreateTbReq(submitTbData.pCreateTbReq);
55,148 ✔
2336
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
55,146 ✔
2337
  tEndDecode(pCoder);
55,147 ✔
2338
  return code;
55,143 ✔
2339
}
2340

2341
static int32_t scanSubmitDataForMeta(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, void* data, int32_t len, int64_t ver, STimeWindow win) {
55,138 ✔
2342
  int32_t  code = 0;
55,138 ✔
2343
  int32_t  lino = 0;
55,138 ✔
2344
  SDecoder decoder = {0};
55,138 ✔
2345
  SSHashObj* gidHash = NULL;
55,134 ✔
2346
  void* pTask = sStreamReaderInfo->pTask;
55,134 ✔
2347

2348
  tDecoderInit(&decoder, data, len);
55,134 ✔
2349
  if (tStartDecode(&decoder) < 0) {
55,125 ✔
2350
    code = TSDB_CODE_INVALID_MSG;
×
2351
    TSDB_CHECK_CODE(code, lino, end);
×
2352
  }
2353

2354
  uint64_t nSubmitTbData = 0;
55,150 ✔
2355
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
55,147 ✔
2356
    code = TSDB_CODE_INVALID_MSG;
×
2357
    TSDB_CHECK_CODE(code, lino, end);
×
2358
  }
2359

2360
  gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
55,147 ✔
2361
  STREAM_CHECK_NULL_GOTO(gidHash, terrno);
55,126 ✔
2362

2363
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
110,265 ✔
2364
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataForMeta(&decoder, sStreamReaderInfo, gidHash, ver, win));
55,135 ✔
2365
  }
2366
  tEndDecode(&decoder);
55,130 ✔
2367

2368
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
55,129 ✔
2369
  int32_t iter = 0;
55,129 ✔
2370
  void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
55,129 ✔
2371
  while (px != NULL) {
67,885 ✔
2372
    WalMetaResult* pMeta = (WalMetaResult*)px;
12,738 ✔
2373
    int32_t        rollupTbCount = qStreamGetGroupTableCount(sStreamReaderInfo, pMeta->id);
12,738 ✔
2374
    STREAM_CHECK_RET_GOTO(
12,745 ✔
2375
        buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver, rollupTbCount));
16 ✔
2376
    ((SSDataBlock*)rsp->metaBlock)->info.rows++;
12,739 ✔
2377
    rsp->totalRows++;
12,739 ✔
2378
    ST_TASK_DLOG("stream reader scan submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
12,739 ✔
2379
          ", ver:%"PRId64, pMeta->skey, pMeta->ekey, pMeta->id, ver);
2380
    px = tSimpleHashIterate(gidHash, px, &iter);
12,745 ✔
2381
  }
2382
end:
55,147 ✔
2383
  tDecoderClear(&decoder);
55,147 ✔
2384
  tSimpleHashCleanup( gidHash);
55,148 ✔
2385
  return code;
55,140 ✔
2386
}
2387

2388
static int32_t createBlockForTsdbMeta(SSDataBlock** pBlock, bool isVTable, bool withRollupTbCount) {
1,903 ✔
2389
  int32_t code = 0;
1,903 ✔
2390
  int32_t lino = 0;
1,903 ✔
2391
  SArray* schemas = taosArrayInit(8, sizeof(SSchema));
1,903 ✔
2392
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
1,903 ✔
2393

2394
  int32_t index = 1;
1,903 ✔
2395
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_TIMESTAMP, LONG_BYTES, index++))  // skey
1,903 ✔
2396
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_TIMESTAMP, LONG_BYTES, index++))  // ekey
1,903 ✔
2397
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // uid
1,903 ✔
2398
  if (!isVTable) {
1,903 ✔
2399
    STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_UBIGINT, LONG_BYTES, index++))  // gid
262 ✔
2400
  }
2401
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, index++))  // nrows
1,903 ✔
2402
  if (withRollupTbCount) {
1,903 ✔
2403
    STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_INT, INT_BYTES, index++))  // rollupTbCount
1 ✔
2404
  }
2405

2406
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
1,903 ✔
2407

2408
end:
1,903 ✔
2409
  taosArrayDestroy(schemas);
1,903 ✔
2410
  return code;
1,903 ✔
2411
}
2412

2413
static int32_t createBlockForWalMetaNew(SSDataBlock** pBlock, bool withRollupTbCount) {
1,728 ✔
2414
  int32_t code = 0;
1,728 ✔
2415
  int32_t lino = 0;
1,728 ✔
2416
  SArray* schemas = NULL;
1,728 ✔
2417

2418
  schemas = taosArrayInit(8, sizeof(SSchema));
1,728 ✔
2419
  STREAM_CHECK_NULL_GOTO(schemas, terrno);
1,728 ✔
2420

2421
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES,
1,728 ✔
2422
                                           STREAM_WAL_META_GID_COL))  // gid non vtable/uid vtable
2423
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, STREAM_WAL_META_SKEY_COL))
1,728 ✔
2424
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, STREAM_WAL_META_EKEY_COL))
1,728 ✔
2425
  STREAM_CHECK_RET_GOTO(qStreamBuildSchema(schemas, TSDB_DATA_TYPE_BIGINT, LONG_BYTES, STREAM_WAL_META_VER_COL))
1,728 ✔
2426
  if (withRollupTbCount) {
1,728 ✔
2427
    STREAM_CHECK_RET_GOTO(
50 ✔
2428
        qStreamBuildSchema(schemas, TSDB_DATA_TYPE_INT, INT_BYTES, STREAM_WAL_META_ROLLUP_TBCOUNT_COL))
2429
  }
2430

2431
  STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, pBlock));
1,728 ✔
2432

2433
end:
1,728 ✔
2434
  taosArrayDestroy(schemas);
1,728 ✔
2435
  return code;
1,728 ✔
2436
}
2437

2438
static int32_t processMeta(int16_t msgType, SStreamTriggerReaderInfo* sStreamReaderInfo, void *data, int32_t len, SSTriggerWalNewRsp* rsp, int64_t ver) {
2,324 ✔
2439
  int32_t code = 0;
2,324 ✔
2440
  int32_t lino = 0;
2,324 ✔
2441
  void* pTask = sStreamReaderInfo->pTask;
2,324 ✔
2442

2443
  ST_TASK_DLOG("%s check meta msg, stream ver:%" PRId64 ", wal ver:%" PRId64, __func__, sStreamReaderInfo->tableList.version, ver);
2,324 ✔
2444

2445
  SDecoder dcoder = {0};
2,324 ✔
2446
  tDecoderInit(&dcoder, data, len);
2,324 ✔
2447
  if (msgType == TDMT_VND_DELETE && sStreamReaderInfo->deleteReCalc != 0) {
2,325 ✔
2448
    if (rsp->deleteBlock == NULL) {
131 ✔
2449
      STREAM_CHECK_RET_GOTO(createBlockForWalMetaNew((SSDataBlock**)&rsp->deleteBlock, false));
61 ✔
2450
    }
2451
      
2452
    STREAM_CHECK_RET_GOTO(scanDeleteDataNew(sStreamReaderInfo, rsp, data, len, ver));
131 ✔
2453
  } else if (msgType == TDMT_VND_DROP_TABLE &&
2,194 ✔
2454
             (sStreamReaderInfo->deleteOutTbl != 0 || sStreamReaderInfo->isVtableStream)) {
165 ✔
2455
    STREAM_CHECK_RET_GOTO(scanDropTableNew(sStreamReaderInfo, rsp, data, len, ver));
78 ✔
2456
  // } else if (msgType == TDMT_VND_DROP_STB) {
2457
  //   STREAM_CHECK_RET_GOTO(scanDropSTableNew(sStreamReaderInfo, data, len));
2458
  } else if (msgType == TDMT_VND_CREATE_TABLE && !ignoreMetaChange(pTask, sStreamReaderInfo->tableList.version, ver)) {
2,116 ✔
2459
    STREAM_CHECK_RET_GOTO(scanCreateTableNew(sStreamReaderInfo, rsp, data, len, ver));
307 ✔
2460
  } else if (msgType == TDMT_VND_ALTER_STB && !ignoreMetaChange(pTask, sStreamReaderInfo->tableList.version, ver)) {
1,809 ✔
2461
    // STREAM_CHECK_RET_GOTO(scanAlterSTableNew(sStreamReaderInfo, data, len));
2462
  } else if (msgType == TDMT_VND_ALTER_TABLE && !ignoreMetaChange(pTask, sStreamReaderInfo->tableList.version, ver)) {
1,678 ✔
2463
    STREAM_CHECK_RET_GOTO(scanAlterTableNew(sStreamReaderInfo, rsp, data, len, ver));
249 ✔
2464
  }
2465

2466
end:
2,325 ✔
2467
  tDecoderClear(&dcoder);
2,325 ✔
2468
  return code;
2,325 ✔
2469
}
2470

2471
static bool shouldStopBeforeSubmitAfterTableChange(SStreamTriggerReaderInfo* sStreamReaderInfo,
75,918 ✔
2472
                                                   SSTriggerWalNewRsp*       rsp) {
2473
  return sStreamReaderInfo->isVtableStream && rsp->tableBlock != NULL &&
75,455 ✔
2474
         ((SSDataBlock*)rsp->tableBlock)->info.rows > 0;
26 ✔
2475
}
2476

2477
static STimeWindow getDbTimeRange(SVnode* pVnode) {
5,134 ✔
2478
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
5,134 ✔
2479
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
5,134 ✔
2480
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
5,134 ✔
2481
  return (STimeWindow){minKey, maxKey};
5,134 ✔
2482
}
2483

2484
static int32_t processWalVerMetaNew(SVnode* pVnode, SSTriggerWalNewRsp* rsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
20,870 ✔
2485
                       int64_t ctime) {
2486
  int32_t code = 0;
20,870 ✔
2487
  int32_t lino = 0;
20,870 ✔
2488
  void* pTask = sStreamReaderInfo->pTask;
20,870 ✔
2489

2490
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
20,870 ✔
2491
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
20,868 ✔
2492
  code = walReaderSeekVer(pWalReader, rsp->ver);
20,868 ✔
2493
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
20,870 ✔
2494
    if (rsp->ver < walGetFirstVer(pWalReader->pWal)) {
15,738 ✔
2495
      rsp->ver = walGetFirstVer(pWalReader->pWal);
×
2496
      rsp->verTime = 0;
×
2497
    } else {
2498
      rsp->verTime = taosGetTimestampUs();
15,740 ✔
2499
    }
2500
    ST_TASK_DLOG("vgId:%d %s scan wal end:%s", TD_VID(pVnode), __func__, tstrerror(code));
15,740 ✔
2501
    code = TSDB_CODE_SUCCESS;
15,734 ✔
2502
    goto end;
15,734 ✔
2503
  }
2504
  STREAM_CHECK_RET_GOTO(code);
5,132 ✔
2505

2506
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, STREAM_RETURN_ROWS_NUM));
5,132 ✔
2507

2508
  STimeWindow win = getDbTimeRange(pVnode);
5,134 ✔
2509
  while (1) {
56,549 ✔
2510
    code = walNextValidMsg(pWalReader, true);
61,620 ✔
2511
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
61,622 ✔
2512
      rsp->verTime = taosGetTimestampUs();
5,107 ✔
2513
      ST_TASK_DLOG("vgId:%d %s scan wal end:%s", TD_VID(pVnode), __func__, tstrerror(code));
5,107 ✔
2514
      code = TSDB_CODE_SUCCESS;
5,107 ✔
2515
      goto end;
5,107 ✔
2516
    }
2517
    STREAM_CHECK_RET_GOTO(code);
56,516 ✔
2518
    rsp->ver = pWalReader->curVersion;
56,516 ✔
2519
    SWalCont* wCont = &pWalReader->pHead->head;
56,516 ✔
2520
    rsp->verTime = wCont->ingestTs;
56,516 ✔
2521
    if (wCont->ingestTs / 1000 > ctime) break;
56,516 ✔
2522
    void*   data = POINTER_SHIFT(walContBody(wCont), sizeof(SMsgHead));
56,516 ✔
2523
    int32_t len = walContBodyLen(wCont) - sizeof(SMsgHead);
56,516 ✔
2524
    int64_t ver = wCont->version;
56,516 ✔
2525

2526
    ST_TASK_DLOG("vgId:%d stream reader scan wal ver:%" PRId64 "/%" PRId64 ", type:%s, deleteData:%d, deleteTb:%d",
56,516 ✔
2527
      TD_VID(pVnode), ver, walGetAppliedVer(pWalReader->pWal), TMSG_INFO(wCont->msgType), sStreamReaderInfo->deleteReCalc, sStreamReaderInfo->deleteOutTbl);
2528
    if (wCont->msgType == TDMT_VND_SUBMIT) {
56,520 ✔
2529
      // return when getting data if there are meta data in vtable scan
2530
      if (shouldStopBeforeSubmitAfterTableChange(sStreamReaderInfo, rsp)) {
55,172 ✔
2531
        rsp->ver--;
26 ✔
2532
        break;
26 ✔
2533
      }
2534
      data = POINTER_SHIFT(walContBody(wCont), sizeof(SSubmitReq2Msg));
55,152 ✔
2535
      len = walContBodyLen(wCont) - sizeof(SSubmitReq2Msg);
55,152 ✔
2536
      STREAM_CHECK_RET_GOTO(scanSubmitDataForMeta(sStreamReaderInfo, rsp, data, len, ver, win));
55,152 ✔
2537
    } else {
2538
      STREAM_CHECK_RET_GOTO(processMeta(wCont->msgType, sStreamReaderInfo, data, len, rsp, ver));
1,348 ✔
2539
    }
2540

2541
    if (rsp->totalRows >= STREAM_RETURN_ROWS_NUM) {
56,488 ✔
2542
      break;
×
2543
    }
2544
  }
2545

2546
end:
20,767 ✔
2547
  walCloseReader(pWalReader);
20,868 ✔
2548
  return code;
20,868 ✔
2549
}
2550

2551
int32_t cacheTag(SVnode* pVnode, SHashObj* metaCache, SExprInfo* pExprInfo, int32_t numOfExpr, SStorageAPI* api, uint64_t uid, col_id_t colId, SRWLatch* lock) {
158,118 ✔
2552
  int32_t     code = 0;
158,118 ✔
2553
  int32_t     lino = 0;
158,118 ✔
2554
  SMetaReader mr = {0};
158,118 ✔
2555
  SArray* tagCache = NULL;
158,123 ✔
2556
  char* data = NULL;
158,117 ✔
2557

2558
  if (lock != NULL) taosWLockLatch(lock);
158,116 ✔
2559
  STREAM_CHECK_CONDITION_GOTO(numOfExpr == 0, code);
158,180 ✔
2560
  stDebug("%s start,uid:%"PRIu64, __func__, uid);
27,906 ✔
2561
  void* uidData = taosHashGet(metaCache, &uid, LONG_BYTES);
27,906 ✔
2562
  if (uidData == NULL) {
27,907 ✔
2563
    tagCache = taosArrayInit(numOfExpr, POINTER_BYTES);
27,797 ✔
2564
    STREAM_CHECK_NULL_GOTO(tagCache, terrno);
27,797 ✔
2565
    if(taosHashPut(metaCache, &uid, LONG_BYTES, &tagCache, POINTER_BYTES) != 0) {
27,797 ✔
2566
      taosArrayDestroy(tagCache);
×
2567
      code = terrno;
×
2568
      goto end;
×
2569
    }
2570
  } else {
2571
    tagCache = *(SArray**)uidData;
110 ✔
2572
    stDebug("%s found tagCache, size:%zu %d, uid:%"PRIu64, __func__, taosArrayGetSize(tagCache), numOfExpr, uid);
110 ✔
2573
    STREAM_CHECK_CONDITION_GOTO(taosArrayGetSize(tagCache) != numOfExpr, TSDB_CODE_INVALID_PARA);
110 ✔
2574
  }
2575
  
2576
  api->metaReaderFn.initReader(&mr, pVnode, META_READER_LOCK, &api->metaFn, 0);
27,907 ✔
2577
  code = api->metaReaderFn.getEntryGetUidCache(&mr, uid);
27,907 ✔
2578
  api->metaReaderFn.readerReleaseLock(&mr);
27,900 ✔
2579
  STREAM_CHECK_RET_GOTO(code);
27,902 ✔
2580
  
2581
  for (int32_t j = 0; j < numOfExpr; ++j) {
66,807 ✔
2582
    const SExprInfo* pExpr1 = &pExprInfo[j];
38,907 ✔
2583
    int32_t functionId = pExpr1->pExpr->_function.functionId;
38,907 ✔
2584
    col_id_t cid = 0;
38,907 ✔
2585
    // this is to handle the tbname
2586
    if (fmIsScanPseudoColumnFunc(functionId)) {
38,907 ✔
2587
      int32_t fType = pExpr1->pExpr->_function.functionType;
23,200 ✔
2588
      if (fType == FUNCTION_TYPE_TBNAME) {
23,200 ✔
2589
        data = taosMemoryCalloc(1, strlen(mr.me.name) + VARSTR_HEADER_SIZE);
23,200 ✔
2590
        STREAM_CHECK_NULL_GOTO(data, terrno);
23,200 ✔
2591
        STR_TO_VARSTR(data, mr.me.name)
23,200 ✔
2592
      }
2593
      cid = -1;
23,204 ✔
2594
    } else {  // these are tags
2595
      const char* p = NULL;
15,712 ✔
2596
      char* pData = NULL;
15,712 ✔
2597
      int8_t type = pExpr1->base.resSchema.type;
15,712 ✔
2598
      int32_t len = pExpr1->base.resSchema.bytes;
15,712 ✔
2599
      STagVal tagVal = {0};
15,712 ✔
2600
      tagVal.cid = pExpr1->base.pParam[0].pCol->colId;
15,718 ✔
2601
      cid = tagVal.cid;
15,718 ✔
2602
      if (colId != 0 && cid != colId) {
15,718 ✔
2603
        continue;
4 ✔
2604
      }
2605
      p = api->metaFn.extractTagVal(mr.me.ctbEntry.pTags, type, &tagVal);
15,714 ✔
2606

2607
      if (type != TSDB_DATA_TYPE_JSON && p != NULL) {
15,711 ✔
2608
        pData = tTagValToData((const STagVal*)p, false);
15,661 ✔
2609
      } else {
2610
        pData = (char*)p;
50 ✔
2611
      }
2612

2613
      if (pData != NULL && (type == TSDB_DATA_TYPE_JSON || !IS_VAR_DATA_TYPE(type))) {
15,706 ✔
2614
        if (type == TSDB_DATA_TYPE_JSON) {
7,746 ✔
2615
          len = getJsonValueLen(pData);
×
2616
        }
2617
        data = taosMemoryCalloc(1, len);
7,748 ✔
2618
        STREAM_CHECK_NULL_GOTO(data, terrno);
7,749 ✔
2619
        (void)memcpy(data, pData, len);
7,748 ✔
2620
      } else {
2621
        data = pData;
7,958 ✔
2622
      }
2623
    }
2624
    if (uidData == NULL){
38,905 ✔
2625
      STREAM_CHECK_NULL_GOTO(taosArrayPush(tagCache, &data), terrno);
77,359 ✔
2626
    } else {
2627
      void* pre = taosArrayGetP(tagCache, j);
214 ✔
2628
      taosMemoryFree(pre);
214 ✔
2629
      taosArraySet(tagCache, j, &data);
214 ✔
2630
    }
2631
    data = NULL;
38,901 ✔
2632
  }
2633

2634
end:
158,157 ✔
2635
  taosMemoryFree(data);
158,154 ✔
2636
  api->metaReaderFn.clearReader(&mr);
158,157 ✔
2637
  if (lock != NULL) taosWUnLockLatch(lock);
158,130 ✔
2638
  return code;
158,163 ✔
2639
}
2640

2641
int32_t fillTag(SHashObj* metaCache, SExprInfo* pExprInfo, int32_t numOfExpr,
323,516 ✔
2642
                uint64_t uid, SSDataBlock* pBlock, uint32_t currentRow, uint32_t numOfRows, uint32_t numOfBlocks, SRWLatch* lock) {
2643
  int32_t     code = 0;
323,516 ✔
2644
  int32_t     lino = 0;
323,516 ✔
2645
  SArray* tagCache = NULL;
323,516 ✔
2646
  if (numOfExpr == 0) {
323,516 ✔
2647
    return TSDB_CODE_SUCCESS;
107,398 ✔
2648
  }
2649

2650
  taosRLockLatch(lock);
216,118 ✔
2651
  void* uidData = taosHashGet(metaCache, &uid, LONG_BYTES);
216,197 ✔
2652
  if (uidData == NULL) {
216,193 ✔
2653
    stError("%s error uidData is null,uid:%"PRIu64, __func__, uid);
×
2654
  } else {
2655
    tagCache = *(SArray**)uidData;
216,193 ✔
2656
    if(taosArrayGetSize(tagCache) != numOfExpr) {
216,193 ✔
2657
      stError("%s numOfExpr:%d,tagCache size:%zu", __func__, numOfExpr, taosArrayGetSize(tagCache));
×
2658
      tagCache = NULL;
×
2659
    }
2660
  }
2661
  
2662
  for (int32_t j = 0; j < numOfExpr; ++j) {
1,065,638 ✔
2663
    const SExprInfo* pExpr1 = &pExprInfo[j];
849,647 ✔
2664
    int32_t          dstSlotId = pExpr1->base.resSchema.slotId;
849,647 ✔
2665

2666
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, dstSlotId);
849,647 ✔
2667
    STREAM_CHECK_NULL_GOTO(pColInfoData, terrno);
849,571 ✔
2668
    int32_t functionId = pExpr1->pExpr->_function.functionId;
849,571 ✔
2669

2670
    // this is to handle the tbname
2671
    if (fmIsScanPseudoColumnFunc(functionId)) {
849,571 ✔
2672
      int32_t fType = pExpr1->pExpr->_function.functionType;
17,043 ✔
2673
      if (fType == FUNCTION_TYPE_TBNAME) {
17,043 ✔
2674
        pColInfoData->info.colId = -1;
17,043 ✔
2675
      }
2676
    } 
2677
    char* data = tagCache == NULL ? NULL : taosArrayGetP(tagCache, j);
849,520 ✔
2678

2679
    bool isNullVal = (data == NULL) || (pColInfoData->info.type == TSDB_DATA_TYPE_JSON && tTagIsJsonNull(data));
849,345 ✔
2680
    if (isNullVal) {
849,345 ✔
2681
      colDataSetNNULL(pColInfoData, currentRow, numOfRows);
10 ✔
2682
    } else {
2683
      if (!IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
849,335 ✔
2684
        for (uint32_t i = 0; i < numOfRows; i++){
12,308,068 ✔
2685
          colDataClearNull_f(pColInfoData->nullbitmap, currentRow + i);
11,827,475 ✔
2686
        }
2687
      }
2688
      code = colDataSetNItems(pColInfoData, currentRow, (const char*)data, numOfRows, numOfBlocks, false);
849,335 ✔
2689
      STREAM_CHECK_RET_GOTO(code);
849,442 ✔
2690
    }
2691
  }
2692
end:
215,991 ✔
2693
  taosRUnLockLatch(lock);
215,992 ✔
2694
  return code;
216,199 ✔
2695
}
2696

2697
static int32_t processTag(SStreamTriggerReaderInfo* info, bool isCalc, 
16,647 ✔
2698
  uint64_t uid, SSDataBlock* pBlock, uint32_t currentRow, uint32_t numOfRows, uint32_t numOfBlocks) {
2699
  int32_t     code = 0;
16,647 ✔
2700
  int32_t     lino = 0;
16,647 ✔
2701

2702
  void* pTask = info->pTask;
16,647 ✔
2703
  ST_TASK_DLOG("%s start. rows:%" PRIu32 ",uid:%"PRIu64, __func__,  numOfRows, uid);
16,647 ✔
2704
  
2705
  SHashObj* metaCache = isCalc ? info->pTableMetaCacheCalc : info->pTableMetaCacheTrigger;
16,653 ✔
2706
  SExprInfo*   pExprInfo = isCalc ? info->pExprInfoCalcTag : info->pExprInfoTriggerTag; 
16,653 ✔
2707
  int32_t      numOfExpr = isCalc ? info->numOfExprCalcTag : info->numOfExprTriggerTag;
16,653 ✔
2708
  
2709
  code = fillTag(metaCache, pExprInfo, numOfExpr, uid, pBlock, currentRow, numOfRows, numOfBlocks, &info->lock);
16,653 ✔
2710
  STREAM_CHECK_RET_GOTO(code);
16,656 ✔
2711

2712
end:
16,656 ✔
2713
  return code;
16,656 ✔
2714
}
2715

2716
int32_t getRowRange(SColData* pCol, STimeWindow* window, int32_t* rowStart, int32_t* rowEnd, int32_t* nRows) {
14 ✔
2717
  int32_t code = 0;
14 ✔
2718
  int32_t lino = 0;
14 ✔
2719
  *nRows = 0;
14 ✔
2720
  *rowStart = 0;
14 ✔
2721
  *rowEnd = pCol->nVal;
14 ✔
2722
  if (window != NULL) {
14 ✔
2723
    SColVal colVal = {0};
14 ✔
2724
    *rowStart = -1;
14 ✔
2725
    *rowEnd = -1;
14 ✔
2726
    for (int32_t k = 0; k < pCol->nVal; k++) {
2,026 ✔
2727
      STREAM_CHECK_RET_GOTO(tColDataGetValue(pCol, k, &colVal));
2,012 ✔
2728
      int64_t ts = VALUE_GET_TRIVIAL_DATUM(&colVal.value);
2,012 ✔
2729
      if (ts >= window->skey && *rowStart == -1) {
2,012 ✔
2730
        *rowStart = k;
14 ✔
2731
      }
2732
      if (ts > window->ekey && *rowEnd == -1) {
2,012 ✔
2733
        *rowEnd = k;
×
2734
      }
2735
    }
2736
    STREAM_CHECK_CONDITION_GOTO(*rowStart == -1 || *rowStart == *rowEnd, TDB_CODE_SUCCESS);
14 ✔
2737

2738
    if (*rowStart != -1 && *rowEnd == -1) {
14 ✔
2739
      *rowEnd = pCol->nVal;
14 ✔
2740
    }
2741
  }
2742
  *nRows = *rowEnd - *rowStart;
14 ✔
2743

2744
end:
14 ✔
2745
  return code;
14 ✔
2746
}
2747

2748
static int32_t setColData(int64_t rows, int32_t rowStart, int32_t rowEnd, SColData* colData, SColumnInfoData* pColData) {
39 ✔
2749
  int32_t code = 0;
39 ✔
2750
  int32_t lino = 0;
39 ✔
2751
  for (int32_t k = rowStart; k < rowEnd; k++) {
15,063 ✔
2752
    SColVal colVal = {0};
15,024 ✔
2753
    STREAM_CHECK_RET_GOTO(tColDataGetValue(colData, k, &colVal));
15,024 ✔
2754
    if (IS_VAR_DATA_TYPE(colVal.value.type)) {
15,024 ✔
2755
      STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, rows + k - rowStart,
3,000 ✔
2756
                                             (const char*)colVal.value.pData, colVal.value.nData,
2757
                                             !COL_VAL_IS_VALUE(&colVal)));
2758
    } else {
2759
      STREAM_CHECK_RET_GOTO(colDataSetVal(pColData, rows + k - rowStart, VALUE_GET_DATUM(&colVal.value, colVal.value.type),
12,024 ✔
2760
                                          !COL_VAL_IS_VALUE(&colVal)));
24 ✔
2761
    }    
2762
  }
2763
  end:
39 ✔
2764
  return code;
39 ✔
2765
}
2766

2767
static int32_t getColId(int64_t suid, int64_t uid, int16_t i, SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp, int16_t* colId) {
63,827 ✔
2768
  int32_t code = 0;
63,827 ✔
2769
  int32_t lino = 0;
63,827 ✔
2770
  int64_t id[2] = {suid, uid};
63,827 ✔
2771
  taosRLockLatch(&sStreamReaderInfo->lock);
63,828 ✔
2772
  void *px = tSimpleHashGet(rsp->isCalc ? sStreamReaderInfo->uidHashCalc : sStreamReaderInfo->uidHashTrigger, id, sizeof(id));
63,846 ✔
2773
  STREAM_CHECK_NULL_GOTO(px, TSDB_CODE_INVALID_PARA);
63,841 ✔
2774
  SSHashObj* uInfo = *(SSHashObj **)px;
63,841 ✔
2775
  STREAM_CHECK_NULL_GOTO(uInfo, TSDB_CODE_INVALID_PARA);
63,841 ✔
2776
  int16_t*  tmp = tSimpleHashGet(uInfo, &i, sizeof(i));
63,841 ✔
2777
  if (tmp != NULL) {
63,842 ✔
2778
    *colId = *tmp;
50,622 ✔
2779
  } else {
2780
    *colId = -1;
13,220 ✔
2781
  }
2782

2783
end:
63,842 ✔
2784
  taosRUnLockLatch(&sStreamReaderInfo->lock);
63,842 ✔
2785
  return code;
63,849 ✔
2786
}
2787

2788
static int32_t getSchemas(SVnode* pVnode, int64_t suid, int64_t uid, int32_t sver, SStreamTriggerReaderInfo* sStreamReaderInfo, STSchema** schema) {
26,761 ✔
2789
  int32_t code = 0;
26,761 ✔
2790
  int32_t lino = 0;
26,761 ✔
2791
  int64_t id = suid != 0 ? suid : uid;
26,761 ✔
2792
  if (sStreamReaderInfo->isVtableStream) {
26,761 ✔
2793
    STSchema** schemaTmp = taosHashGet(sStreamReaderInfo->triggerTableSchemaMapVTable, &id, LONG_BYTES);
11,040 ✔
2794
    if (schemaTmp == NULL || *schemaTmp == NULL || (*schemaTmp)->version != sver) {
11,042 ✔
2795
      *schema = metaGetTbTSchema(pVnode->pMeta, id, sver, 1);
187 ✔
2796
      STREAM_CHECK_NULL_GOTO(*schema, terrno);
188 ✔
2797
      code = taosHashPut(sStreamReaderInfo->triggerTableSchemaMapVTable, &id, LONG_BYTES, schema, POINTER_BYTES);
188 ✔
2798
      if (code != 0) {
188 ✔
2799
        taosMemoryFree(*schema);
×
2800
        goto end;
×
2801
      }
2802
    } else {
2803
      *schema = *schemaTmp;
10,855 ✔
2804
    }
2805
  } else {
2806
    if (sStreamReaderInfo->triggerTableSchema == NULL || sStreamReaderInfo->triggerTableSchema->version != sver) {
15,721 ✔
2807
      taosMemoryFree(sStreamReaderInfo->triggerTableSchema);
660 ✔
2808
      sStreamReaderInfo->triggerTableSchema = metaGetTbTSchema(pVnode->pMeta, id, sver, 1);
669 ✔
2809
      STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->triggerTableSchema, terrno);
669 ✔
2810
    }
2811
    *schema = sStreamReaderInfo->triggerTableSchema;
15,730 ✔
2812
  }
2813
  
2814
end:
26,773 ✔
2815
  return code;
26,768 ✔
2816
}
2817

2818
static int32_t finishWalDataTarget(SStreamTriggerReaderInfo* sStreamReaderInfo, SSTriggerWalNewRsp* rsp,
26,774 ✔
2819
                                   SSHashObj* gidHash, SSDataBlock* pBlock, int64_t uid, int64_t ver,
2820
                                   SWalDataTarget* target, WalMetaResult* walMeta, SStreamWalDataSlice* pSlice,
2821
                                   int32_t blockStart, int32_t numOfRows) {
2822
  int32_t code = 0;
26,774 ✔
2823
  int32_t lino = 0;
26,774 ✔
2824
  void*   pTask = sStreamReaderInfo->pTask;
26,774 ✔
2825

2826
  if (numOfRows <= 0) {
26,774 ✔
2827
    ST_TASK_DLOG("%s no valid data in time range:skey %" PRId64 ", ekey %" PRId64 ", uid:%" PRId64 ", gid:%" PRIu64,
×
2828
                 __func__, target->window.skey, target->window.ekey, uid, target->gid);
2829
    goto end;
×
2830
  }
2831

2832
  if (!sStreamReaderInfo->isVtableStream) {
26,774 ✔
2833
    STREAM_CHECK_RET_GOTO(processTag(sStreamReaderInfo, rsp->isCalc, uid, pBlock, blockStart, numOfRows, 1));
15,734 ✔
2834
  }
2835

2836
  SColumnInfoData* pColData = taosArrayGetLast(pBlock->pDataBlock);
26,782 ✔
2837
  STREAM_CHECK_NULL_GOTO(pColData, terrno);
26,778 ✔
2838
  STREAM_CHECK_RET_GOTO(colDataSetNItems(pColData, blockStart, (const char*)&ver, numOfRows, 1, false));
26,778 ✔
2839

2840
  STREAM_CHECK_NULL_GOTO(pSlice, TSDB_CODE_INVALID_PARA);
26,775 ✔
2841
  ST_TASK_DLOG("%s process submit data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64 ", uid:%" PRId64
26,775 ✔
2842
               ", ver:%" PRId64 ", row index:%d, rows:%d",
2843
               __func__, target->window.skey, target->window.ekey, target->gid, uid, ver, pSlice->currentRowIdx,
2844
               numOfRows);
2845
  pSlice->currentRowIdx += numOfRows;
26,774 ✔
2846
  pBlock->info.rows += numOfRows;
26,774 ✔
2847

2848
  if (gidHash != NULL) {
26,774 ✔
2849
    STREAM_CHECK_RET_GOTO(putWalMetaResult(gidHash, target->gid, walMeta->skey, walMeta->ekey));
7,918 ✔
2850
  }
2851

2852
end:
26,782 ✔
2853
  return code;
26,782 ✔
2854
}
2855

2856
static int32_t scanSubmitTbData(SVnode* pVnode, SDecoder* pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo,
30,399 ✔
2857
                                SSHashObj* ranges, SSHashObj* gidHash, SSTriggerWalNewRsp* rsp, int64_t ver) {
2858
  int32_t       code = 0;
30,399 ✔
2859
  int32_t       lino = 0;
30,399 ✔
2860
  WalMetaResult walMeta = {0};
30,399 ✔
2861
  void*         pTask = sStreamReaderInfo->pTask;
30,397 ✔
2862
  SSDataBlock*  pBlock = (SSDataBlock*)rsp->dataBlock;
30,397 ✔
2863
  SArray*       targets = NULL;
30,397 ✔
2864
  bool          expandRollupGids = isRollupMultiReader(sStreamReaderInfo);
30,397 ✔
2865

2866
  if (tStartDecode(pCoder) < 0) {
30,387 ✔
2867
    ST_TASK_ELOG("vgId:%d %s invalid submit data", TD_VID(pVnode), __func__);
×
2868
    code = TSDB_CODE_INVALID_MSG;
×
2869
    TSDB_CHECK_CODE(code, lino, end);
136 ✔
2870
  }
2871

2872
  SSubmitTbData submitTbData = {0};
30,403 ✔
2873
  uint8_t       version = 0;
30,400 ✔
2874
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
30,390 ✔
2875
    ST_TASK_ELOG("vgId:%d %s invalid submit data flags", TD_VID(pVnode), __func__);
×
2876
    code = TSDB_CODE_INVALID_MSG;
×
2877
    TSDB_CHECK_CODE(code, lino, end);
136 ✔
2878
  }
2879
  version = (submitTbData.flags >> 8) & 0xff;
30,390 ✔
2880
  submitTbData.flags = submitTbData.flags & 0xff;
30,390 ✔
2881
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
2882
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
30,390 ✔
2883
    if (tStartDecode(pCoder) < 0) {
335 ✔
2884
      ST_TASK_ELOG("vgId:%d %s invalid auto create table data", TD_VID(pVnode), __func__);
×
2885
      code = TSDB_CODE_INVALID_MSG;
×
2886
      TSDB_CHECK_CODE(code, lino, end);
20 ✔
2887
    }
2888
    tEndDecode(pCoder);
335 ✔
2889
  }
2890

2891
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
30,374 ✔
2892
    ST_TASK_ELOG("vgId:%d %s invalid submit data suid", TD_VID(pVnode), __func__);
×
2893
    code = TSDB_CODE_INVALID_MSG;
×
2894
    TSDB_CHECK_CODE(code, lino, end);
136 ✔
2895
  }
2896
  if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
30,382 ✔
2897
    ST_TASK_ELOG("vgId:%d %s invalid submit data uid", TD_VID(pVnode), __func__);
×
2898
    code = TSDB_CODE_INVALID_MSG;
×
2899
    TSDB_CHECK_CODE(code, lino, end);
136 ✔
2900
  }
2901

2902
  ST_TASK_DLOG("%s uid:%" PRId64 ", suid:%" PRId64 ", ver:%" PRId64, __func__, submitTbData.uid, submitTbData.suid,
30,382 ✔
2903
               ver);
2904

2905
  SWalDataTarget singleTarget = {.gid = 0, .window = {.skey = INT64_MIN, .ekey = INT64_MAX}};
30,389 ✔
2906
  if (rsp->uidHash != NULL) {
30,383 ✔
2907
    uint64_t* gid = tSimpleHashGet(rsp->uidHash, &submitTbData.uid, LONG_BYTES);
18,866 ✔
2908
    STREAM_CHECK_CONDITION_GOTO(gid == NULL, TDB_CODE_SUCCESS);
18,869 ✔
2909
    ST_TASK_DLOG("%s get uid gid from uidHash, uid:%" PRId64 ", suid:%" PRId64 " gid:%" PRIu64, __func__,
18,856 ✔
2910
                  submitTbData.uid, submitTbData.suid, *gid);
2911
    singleTarget.gid = *gid;
18,855 ✔
2912
  }
2913
  if (expandRollupGids) {
30,372 ✔
2914
    targets = taosArrayInit(4, sizeof(SWalDataTarget));
1,838 ✔
2915
    STREAM_CHECK_NULL_GOTO(targets, terrno);
1,838 ✔
2916
    STREAM_CHECK_RET_GOTO(
1,838 ✔
2917
        collectWalRollupTargets(sStreamReaderInfo, ranges, submitTbData.suid, submitTbData.uid, targets));
×
2918
    STREAM_CHECK_CONDITION_GOTO(taosArrayGetSize(targets) == 0, TDB_CODE_SUCCESS);
1,838 ✔
2919
  } else {
2920
    if (singleTarget.gid == 0) {
28,534 ✔
2921
      STREAM_CHECK_CONDITION_GOTO(
13,961 ✔
2922
          !uidInTableListSet(sStreamReaderInfo, submitTbData.suid, submitTbData.uid, &singleTarget.gid, rsp->isCalc),
2923
          TDB_CODE_SUCCESS);
34 ✔
2924
    }
2925

2926
    if (ranges != NULL) {
24,930 ✔
2927
      void* timerange = tSimpleHashGet(ranges, &singleTarget.gid, sizeof(singleTarget.gid));
17,024 ✔
2928
      if (timerange == NULL) goto end;
17,022 ✔
2929
      int64_t* pRange = (int64_t*)timerange;
17,022 ✔
2930
      singleTarget.window.skey = pRange[0];
17,022 ✔
2931
      singleTarget.window.ekey = pRange[1];
17,022 ✔
2932
      ST_TASK_DLOG("%s get time range from ranges, uid:%" PRId64 ", suid:%" PRId64 ", gid:%" PRIu64 ", skey:%" PRId64
17,022 ✔
2933
                   ", ekey:%" PRId64,
2934
                   __func__, submitTbData.uid, submitTbData.suid, singleTarget.gid, singleTarget.window.skey,
2935
                   singleTarget.window.ekey);
2936
    }
2937
  }
2938

2939
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
26,766 ✔
2940
    ST_TASK_ELOG("vgId:%d %s invalid submit data sver", TD_VID(pVnode), __func__);
×
2941
    code = TSDB_CODE_INVALID_MSG;
×
2942
    TSDB_CHECK_CODE(code, lino, end);
34 ✔
2943
  }
2944

2945
  STSchema* schema = NULL;
26,766 ✔
2946
  STREAM_CHECK_RET_GOTO(
26,767 ✔
2947
      getSchemas(pVnode, submitTbData.suid, submitTbData.uid, submitTbData.sver, sStreamReaderInfo, &schema));
34 ✔
2948

2949
  int32_t numOfTargets = expandRollupGids ? taosArrayGetSize(targets) : 1;
26,767 ✔
2950
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
26,767 ✔
2951
    uint64_t nColData = 0;
13 ✔
2952
    if (tDecodeU64v(pCoder, &nColData) < 0) {
13 ✔
2953
      ST_TASK_ELOG("vgId:%d %s invalid submit data nColData", TD_VID(pVnode), __func__);
×
2954
      code = TSDB_CODE_INVALID_MSG;
×
2955
      TSDB_CHECK_CODE(code, lino, end);
×
2956
    }
2957

2958
    SColData colData = {0};
13 ✔
2959
    code = tDecodeColData(version, pCoder, &colData, false);
13 ✔
2960
    if (code) {
13 ✔
2961
      ST_TASK_ELOG("vgId:%d %s invalid submit data colData", TD_VID(pVnode), __func__);
×
2962
      code = TSDB_CODE_INVALID_MSG;
×
2963
      TSDB_CHECK_CODE(code, lino, end);
×
2964
    }
2965

2966
    if (colData.flag != HAS_VALUE) {
13 ✔
2967
      ST_TASK_ELOG("vgId:%d %s invalid submit data colData flag", TD_VID(pVnode), __func__);
×
2968
      code = TSDB_CODE_INVALID_MSG;
×
2969
      TSDB_CHECK_CODE(code, lino, end);
×
2970
    }
2971

2972
    walMeta.skey = ((TSKEY*)colData.pData)[0];
13 ✔
2973
    walMeta.ekey = ((TSKEY*)colData.pData)[colData.nVal - 1];
13 ✔
2974
    int32_t colDataPos = pCoder->pos;
13 ✔
2975

2976
    for (int32_t t = 0; t < numOfTargets; ++t) {
26 ✔
2977
      SWalDataTarget* target = expandRollupGids ? taosArrayGet(targets, t) : &singleTarget;
13 ✔
2978
      STREAM_CHECK_NULL_GOTO(target, terrno);
1 ✔
2979

2980
      int32_t rowStart = 0;
13 ✔
2981
      int32_t rowEnd = 0;
13 ✔
2982
      int32_t numOfRows = 0;
13 ✔
2983
      STREAM_CHECK_RET_GOTO(getRowRange(&colData, &target->window, &rowStart, &rowEnd, &numOfRows));
13 ✔
2984
      if (numOfRows <= 0) {
13 ✔
2985
        ST_TASK_WLOG("%s no valid column data, uid:%" PRId64 ", gid:%" PRIu64, __func__, submitTbData.uid, target->gid);
×
2986
        continue;
×
2987
      }
2988

2989
      SStreamWalDataSlice* pSlice = getWalDataSlice(sStreamReaderInfo, rsp, submitTbData.uid, target->gid);
13 ✔
2990
      STREAM_CHECK_NULL_GOTO(pSlice, TSDB_CODE_INVALID_PARA);
13 ✔
2991

2992
      int32_t blockStart = pSlice->currentRowIdx;
13 ✔
2993
      int32_t sliceEnd = pSlice->startRowIdx + pSlice->numRows;
13 ✔
2994
      if (blockStart >= sliceEnd) {
13 ✔
2995
        ST_TASK_WLOG("%s skip column data beyond slice uid:%" PRId64 ", gid:%" PRIu64 ", ver:%" PRId64
×
2996
                     ", currentRowIdx:%d, sliceEnd:%d",
2997
                     __func__, submitTbData.uid, target->gid, ver, blockStart, sliceEnd);
2998
        continue;
×
2999
      }
3000
      int32_t availableRows = sliceEnd - blockStart;
13 ✔
3001
      if (numOfRows > availableRows) {
13 ✔
3002
        ST_TASK_WLOG("%s truncate column data to slice uid:%" PRId64 ", gid:%" PRIu64 ", ver:%" PRId64
×
3003
                     ", sliceRows:%d, inputRows:%d, currentRowIdx:%d",
3004
                     __func__, submitTbData.uid, target->gid, ver, availableRows, numOfRows, blockStart);
3005
        rowEnd = rowStart + availableRows;
×
3006
        numOfRows = availableRows;
×
3007
      }
3008
      for (int16_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {
66 ✔
3009
        SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, i);
53 ✔
3010
        STREAM_CHECK_NULL_GOTO(pColData, terrno);
53 ✔
3011
        if (pColData->info.colId <= -1) {
53 ✔
3012
          pColData->hasNull = true;
14 ✔
3013
          continue;
26 ✔
3014
        }
3015
        if (pColData->info.colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
39 ✔
3016
          STREAM_CHECK_RET_GOTO(setColData(blockStart, rowStart, rowEnd, &colData, pColData));
13 ✔
3017
          continue;
13 ✔
3018
        }
3019

3020
        pCoder->pos = colDataPos;
26 ✔
3021

3022
        int16_t colId = 0;
26 ✔
3023
        if (sStreamReaderInfo->isVtableStream) {
26 ✔
3024
          STREAM_CHECK_RET_GOTO(getColId(submitTbData.suid, submitTbData.uid, i, sStreamReaderInfo, rsp, &colId));
12 ✔
3025
          ST_TASK_TLOG("%s vtable colId:%d, i:%d, uid:%" PRId64, __func__, colId, i, submitTbData.uid);
12 ✔
3026
        } else {
3027
          colId = pColData->info.colId;
14 ✔
3028
        }
3029

3030
        uint64_t j = 1;
14 ✔
3031
        for (; j < nColData; j++) {
117 ✔
3032
          int16_t cid = 0;
117 ✔
3033
          int32_t posTmp = pCoder->pos;
117 ✔
3034
          pCoder->pos += INT_BYTES;
117 ✔
3035
          code = tDecodeI16v(pCoder, &cid);
117 ✔
3036
          TSDB_CHECK_CODE(code, lino, end);
105 ✔
3037
          pCoder->pos = posTmp;
117 ✔
3038
          if (cid == colId) {
117 ✔
3039
            SColData colDataTmp = {0};
26 ✔
3040
            code = tDecodeColData(version, pCoder, &colDataTmp, false);
26 ✔
3041
            if (code) {
26 ✔
3042
              code = TSDB_CODE_INVALID_MSG;
×
3043
              TSDB_CHECK_CODE(code, lino, end);
×
3044
            }
3045
            STREAM_CHECK_RET_GOTO(setColData(blockStart, rowStart, rowEnd, &colDataTmp, pColData));
26 ✔
3046
            break;
26 ✔
3047
          }
3048
          SColData skipColData = {0};
91 ✔
3049
          code = tDecodeColData(version, pCoder, &skipColData, true);
91 ✔
3050
          if (code) {
91 ✔
3051
            code = TSDB_CODE_INVALID_MSG;
×
3052
            TSDB_CHECK_CODE(code, lino, end);
×
3053
          }
3054
        }
3055
        if (j == nColData) {
26 ✔
3056
          colDataSetNNULL(pColData, blockStart, numOfRows);
×
3057
        }
3058
      }
3059

3060
      STREAM_CHECK_RET_GOTO(finishWalDataTarget(sStreamReaderInfo, rsp, gidHash, pBlock, submitTbData.uid, ver, target,
13 ✔
3061
                                                &walMeta, pSlice, blockStart, numOfRows));
12 ✔
3062
    }
3063
  } else {
3064
    uint64_t nRow = 0;
26,754 ✔
3065
    if (tDecodeU64v(pCoder, &nRow) < 0) {
26,756 ✔
3066
      code = TSDB_CODE_INVALID_MSG;
×
3067
      TSDB_CHECK_CODE(code, lino, end);
×
3068
    }
3069
    int32_t rowsPos = pCoder->pos;
26,756 ✔
3070

3071
    for (int32_t t = 0; t < numOfTargets; ++t) {
53,521 ✔
3072
      SWalDataTarget* target = expandRollupGids ? taosArrayGet(targets, t) : &singleTarget;
26,762 ✔
3073
      STREAM_CHECK_NULL_GOTO(target, terrno);
26,740 ✔
3074
      SStreamWalDataSlice* pSlice = getWalDataSlice(sStreamReaderInfo, rsp, submitTbData.uid, target->gid);
26,762 ✔
3075
      if (pSlice == NULL) {
26,766 ✔
3076
        ST_TASK_WLOG("%s no row data slice, uid:%" PRId64 ", gid:%" PRIu64, __func__, submitTbData.uid, target->gid);
1 ✔
3077
        continue;
×
3078
      }
3079

3080
      int32_t blockStart = pSlice->currentRowIdx;
26,765 ✔
3081
      int32_t sliceEnd = pSlice->startRowIdx + pSlice->numRows;
26,765 ✔
3082
      if (blockStart >= sliceEnd) {
26,765 ✔
3083
        ST_TASK_WLOG("%s skip row data beyond slice uid:%" PRId64 ", gid:%" PRIu64 ", ver:%" PRId64
×
3084
                     ", currentRowIdx:%d, sliceEnd:%d",
3085
                     __func__, submitTbData.uid, target->gid, ver, blockStart, sliceEnd);
3086
        continue;
×
3087
      }
3088
      int32_t availableRows = sliceEnd - blockStart;
26,765 ✔
3089
      int32_t numOfRows = 0;
26,765 ✔
3090
      pCoder->pos = rowsPos;
26,765 ✔
3091
      for (uint64_t iRow = 0; iRow < nRow; ++iRow) {
72,329 ✔
3092
        SRow* pRow = (SRow*)(pCoder->data + pCoder->pos);
45,580 ✔
3093
        pCoder->pos += pRow->len;
45,580 ✔
3094

3095
#ifndef NO_UNALIGNED_ACCESS
3096
        TSKEY rowTs = pRow->ts;
45,580 ✔
3097
#else
3098
        TSKEY rowTs = taosGetInt64Aligned(&pRow->ts);
3099
#endif
3100
        if (iRow == 0) {
45,580 ✔
3101
          walMeta.skey = rowTs;
26,766 ✔
3102
        }
3103
        if (iRow == nRow - 1) {
45,580 ✔
3104
          walMeta.ekey = rowTs;
26,763 ✔
3105
        }
3106

3107
        if (rowTs < target->window.skey || rowTs > target->window.ekey) {
45,580 ✔
3108
          continue;
92 ✔
3109
        }
3110

3111
        if (numOfRows >= availableRows) {
45,488 ✔
3112
          ST_TASK_WLOG("%s truncate row data to slice uid:%" PRId64 ", gid:%" PRIu64 ", ver:%" PRId64
×
3113
                       ", sliceRows:%d, walRows:%" PRIu64 ", currentRowIdx:%d",
3114
                       __func__, submitTbData.uid, target->gid, ver, availableRows, nRow, blockStart);
3115
          continue;
×
3116
        }
3117
        for (int16_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {
281,845 ✔
3118
          SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, i);
236,267 ✔
3119
          STREAM_CHECK_NULL_GOTO(pColData, terrno);
236,302 ✔
3120
          if (pColData->info.colId <= -1) {
236,465 ✔
3121
            pColData->hasNull = true;
81,382 ✔
3122
            continue;
81,382 ✔
3123
          }
3124
          int16_t colId = 0;
155,083 ✔
3125
          if (sStreamReaderInfo->isVtableStream) {
155,064 ✔
3126
            STREAM_CHECK_RET_GOTO(getColId(submitTbData.suid, submitTbData.uid, i, sStreamReaderInfo, rsp, &colId));
63,821 ✔
3127
            ST_TASK_TLOG("%s vtable colId:%d, i:%d, uid:%" PRId64, __func__, colId, i, submitTbData.uid);
63,838 ✔
3128
          } else {
3129
            colId = pColData->info.colId;
91,243 ✔
3130
          }
3131

3132
          SColVal colVal = {0};
155,081 ✔
3133
          int32_t sourceIdx = 0;
155,077 ✔
3134
          while (1) {
100 ✔
3135
            if (sourceIdx >= schema->numOfCols) {
392,031 ✔
3136
              break;
51,052 ✔
3137
            }
3138
            STREAM_CHECK_RET_GOTO(tRowGet(pRow, schema, sourceIdx, &colVal));
340,979 ✔
3139
            if (colVal.cid == colId) {
341,219 ✔
3140
              break;
104,225 ✔
3141
            }
3142
            sourceIdx++;
236,954 ✔
3143
          }
3144
          if (colVal.cid == colId && COL_VAL_IS_VALUE(&colVal)) {
155,317 ✔
3145
            if (IS_VAR_DATA_TYPE(colVal.value.type) || colVal.value.type == TSDB_DATA_TYPE_DECIMAL) {
100,789 ✔
3146
              STREAM_CHECK_RET_GOTO(varColSetVarData(pColData, blockStart + numOfRows, (const char*)colVal.value.pData,
3,050 ✔
3147
                                                     colVal.value.nData, !COL_VAL_IS_VALUE(&colVal)));
×
3148
              ST_TASK_TLOG(
3,056 ✔
3149
                  "%s vtable colId:%d, i:%d, colData:%p, data:%s, len:%d, rowIndex:%d, offset:%d, uid:%" PRId64,
3150
                  __func__, colId, i, pColData, (const char*)colVal.value.pData, colVal.value.nData,
3151
                  blockStart + numOfRows, pColData->varmeta.offset[blockStart + numOfRows], submitTbData.uid);
3152
            } else {
3153
              STREAM_CHECK_RET_GOTO(colDataSetVal(pColData, blockStart + numOfRows, (const char*)(&(colVal.value.val)),
97,739 ✔
3154
                                                  !COL_VAL_IS_VALUE(&colVal)));
3155
            }
3156
          } else {
3157
            colDataSetNULL(pColData, blockStart + numOfRows);
54,528 ✔
3158
          }
3159
        }
3160

3161
        numOfRows++;
45,472 ✔
3162
      }
3163

3164
      STREAM_CHECK_RET_GOTO(finishWalDataTarget(sStreamReaderInfo, rsp, gidHash, pBlock, submitTbData.uid, ver, target,
26,749 ✔
3165
                                                &walMeta, pSlice, blockStart, numOfRows));
22 ✔
3166
    }
3167
  }
3168

3169
end:
30,303 ✔
3170
  if (code != 0) {
30,393 ✔
3171
    ST_TASK_ELOG("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
3172
  }
3173
  taosArrayDestroy(targets);
30,393 ✔
3174
  tEndDecode(pCoder);
30,386 ✔
3175
  return code;
30,387 ✔
3176
}
3177
static int32_t scanSubmitData(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo,
29,895 ✔
3178
  void* data, int32_t len, SSHashObj* ranges, SSTriggerWalNewRsp* rsp, int64_t ver) {
3179
  int32_t  code = 0;
29,895 ✔
3180
  int32_t  lino = 0;
29,895 ✔
3181
  SDecoder decoder = {0};
29,895 ✔
3182
  SSHashObj* gidHash = NULL;
29,896 ✔
3183
  void* pTask = sStreamReaderInfo->pTask;
29,896 ✔
3184

3185
  tDecoderInit(&decoder, data, len);
29,896 ✔
3186
  if (tStartDecode(&decoder) < 0) {
29,888 ✔
3187
    code = TSDB_CODE_INVALID_MSG;
×
3188
    TSDB_CHECK_CODE(code, lino, end);
×
3189
  }
3190

3191
  uint64_t nSubmitTbData = 0;
29,891 ✔
3192
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
29,889 ✔
3193
    code = TSDB_CODE_INVALID_MSG;
×
3194
    TSDB_CHECK_CODE(code, lino, end);
×
3195
  }
3196

3197
  if (rsp->metaBlock != NULL){
29,889 ✔
3198
    gidHash = tSimpleHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
11,037 ✔
3199
    STREAM_CHECK_NULL_GOTO(gidHash, terrno);
11,031 ✔
3200
  }
3201

3202
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
60,267 ✔
3203
    STREAM_CHECK_RET_GOTO(scanSubmitTbData(pVnode, &decoder, sStreamReaderInfo, ranges, gidHash, rsp, ver));
30,393 ✔
3204
  }
3205

3206
  tEndDecode(&decoder);
29,874 ✔
3207

3208
  if (rsp->metaBlock != NULL){
29,877 ✔
3209
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->metaBlock, ((SSDataBlock*)rsp->metaBlock)->info.rows + tSimpleHashGetSize(gidHash)));
11,030 ✔
3210
    int32_t iter = 0;
11,021 ✔
3211
    void*   px = tSimpleHashIterate(gidHash, NULL, &iter);
11,021 ✔
3212
    while (px != NULL) {
18,945 ✔
3213
      WalMetaResult* pMeta = (WalMetaResult*)px;
7,913 ✔
3214
      int32_t        rollupTbCount = qStreamGetGroupTableCount(sStreamReaderInfo, pMeta->id);
7,913 ✔
3215
      STREAM_CHECK_RET_GOTO(
7,922 ✔
3216
          buildWalMetaBlockNew(rsp->metaBlock, pMeta->id, pMeta->skey, pMeta->ekey, ver, rollupTbCount));
22 ✔
3217
      ((SSDataBlock*)rsp->metaBlock)->info.rows++;
7,905 ✔
3218
      rsp->totalRows++;
7,905 ✔
3219
      ST_TASK_DLOG("%s process meta data:skey %" PRId64 ", ekey %" PRId64 ", id %" PRIu64
7,905 ✔
3220
            ", ver:%"PRId64, __func__, pMeta->skey, pMeta->ekey, pMeta->id, ver);
3221
      px = tSimpleHashIterate(gidHash, px, &iter);
7,909 ✔
3222
    }
3223
  }
3224
  
3225

3226
end:
29,755 ✔
3227
  tSimpleHashCleanup(gidHash);
29,881 ✔
3228
  tDecoderClear(&decoder);
29,894 ✔
3229
  return code;
29,900 ✔
3230
}
3231

3232
static int32_t scanSubmitTbDataPre(SDecoder *pCoder, SStreamTriggerReaderInfo* sStreamReaderInfo, SSHashObj* ranges, 
40,119 ✔
3233
  uint64_t* gid, int64_t* uid, int32_t* numOfRows, SSTriggerWalNewRsp* rsp, int64_t ver) {
3234
  int32_t code = 0;
40,119 ✔
3235
  int32_t lino = 0;
40,119 ✔
3236
  void* pTask = sStreamReaderInfo->pTask;
40,119 ✔
3237
  SArray* targets = NULL;
40,119 ✔
3238
  bool    expandRollupGids = isRollupMultiReader(sStreamReaderInfo);
40,119 ✔
3239

3240
  if (tStartDecode(pCoder) < 0) {
40,120 ✔
3241
    code = TSDB_CODE_INVALID_MSG;
×
3242
    TSDB_CHECK_CODE(code, lino, end);
×
3243
  }
3244

3245
  SSubmitTbData submitTbData = {0};
40,134 ✔
3246
  uint8_t       version = 0;
40,137 ✔
3247
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
40,115 ✔
3248
    code = TSDB_CODE_INVALID_MSG;
×
3249
    TSDB_CHECK_CODE(code, lino, end);
×
3250
  }
3251
  version = (submitTbData.flags >> 8) & 0xff;
40,115 ✔
3252
  submitTbData.flags = submitTbData.flags & 0xff;
40,115 ✔
3253

3254
  // STREAM_CHECK_CONDITION_GOTO(version < 2, TDB_CODE_SUCCESS);
3255
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
40,115 ✔
3256
    submitTbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
4,763 ✔
3257
    STREAM_CHECK_NULL_GOTO(submitTbData.pCreateTbReq, terrno);
4,764 ✔
3258
    STREAM_CHECK_RET_GOTO(tDecodeSVCreateTbReq(pCoder, submitTbData.pCreateTbReq));
4,764 ✔
3259
    STREAM_CHECK_RET_GOTO(processAutoCreateTableNew(sStreamReaderInfo, submitTbData.pCreateTbReq, ver));
4,760 ✔
3260
  }
3261

3262
  // submit data
3263
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
40,100 ✔
3264
    code = TSDB_CODE_INVALID_MSG;
×
3265
    TSDB_CHECK_CODE(code, lino, end);
×
3266
  }
3267
  if (tDecodeI64(pCoder, uid) < 0) {
40,095 ✔
3268
    code = TSDB_CODE_INVALID_MSG;
×
3269
    TSDB_CHECK_CODE(code, lino, end);
×
3270
  }
3271
  ST_TASK_DLOG("%s uid:%" PRId64 ", suid:%" PRId64, __func__, *uid, submitTbData.suid);
40,095 ✔
3272
  STimeWindow window = {.skey = INT64_MIN, .ekey = INT64_MAX};
40,099 ✔
3273

3274
  if (expandRollupGids) {
40,099 ✔
3275
    targets = taosArrayInit(4, sizeof(SWalDataTarget));
1,838 ✔
3276
    STREAM_CHECK_NULL_GOTO(targets, terrno);
1,838 ✔
3277
    STREAM_CHECK_RET_GOTO(collectWalRollupTargets(sStreamReaderInfo, ranges, submitTbData.suid, *uid, targets));
1,838 ✔
3278
    STREAM_CHECK_CONDITION_GOTO(taosArrayGetSize(targets) == 0, TDB_CODE_SUCCESS);
1,838 ✔
3279
  } else {
3280
    STREAM_CHECK_CONDITION_GOTO(!uidInTableListSet(sStreamReaderInfo, submitTbData.suid, *uid, gid, rsp->isCalc),
38,261 ✔
3281
                                TDB_CODE_SUCCESS);
40 ✔
3282

3283
    if (ranges != NULL) {
24,940 ✔
3284
      void* timerange = tSimpleHashGet(ranges, gid, sizeof(*gid));
17,045 ✔
3285
      if (timerange == NULL) goto end;
17,040 ✔
3286
      int64_t* pRange = (int64_t*)timerange;
17,029 ✔
3287
      window.skey = pRange[0];
17,029 ✔
3288
      window.ekey = pRange[1];
17,029 ✔
3289
    }
3290
  }
3291

3292
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
26,767 ✔
3293
    code = TSDB_CODE_INVALID_MSG;
×
3294
    TSDB_CHECK_CODE(code, lino, end);
×
3295
  }
3296

3297
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
26,767 ✔
3298
    uint64_t nColData = 0;
1 ✔
3299
    if (tDecodeU64v(pCoder, &nColData) < 0) {
19 ✔
3300
      code = TSDB_CODE_INVALID_MSG;
×
3301
      TSDB_CHECK_CODE(code, lino, end);
×
3302
    }
3303

3304
    SColData colData = {0};
19 ✔
3305
    code = tDecodeColData(version, pCoder, &colData, false);
19 ✔
3306
    if (code) {
19 ✔
3307
      code = TSDB_CODE_INVALID_MSG;
×
3308
      TSDB_CHECK_CODE(code, lino, end);
×
3309
    }
3310

3311
    if (colData.flag != HAS_VALUE) {
19 ✔
3312
      code = TSDB_CODE_INVALID_MSG;
×
3313
      TSDB_CHECK_CODE(code, lino, end);
×
3314
    }
3315
    int32_t rowStart = 0;
19 ✔
3316
    int32_t rowEnd = 0;
19 ✔
3317
    if (expandRollupGids) {
19 ✔
3318
      int32_t numOfTargets = taosArrayGetSize(targets);
×
3319
      for (int32_t i = 0; i < numOfTargets; ++i) {
×
3320
        SWalDataTarget* target = taosArrayGet(targets, i);
×
3321
        STREAM_CHECK_NULL_GOTO(target, terrno);
×
3322
        int32_t targetRows = 0;
×
3323
        if (target->window.skey != INT64_MIN || target->window.ekey != INT64_MAX) {
×
3324
          STREAM_CHECK_RET_GOTO(getRowRange(&colData, &target->window, &rowStart, &rowEnd, &targetRows));
×
3325
        } else {
3326
          targetRows = colData.nVal;
×
3327
        }
3328
        STREAM_CHECK_RET_GOTO(addWalDataSlice(sStreamReaderInfo, rsp, *uid, target->gid, targetRows, ver));
×
3329
        *gid = target->gid;
×
3330
        *numOfRows += targetRows;
×
3331
      }
3332
    } else if (window.skey != INT64_MIN || window.ekey != INT64_MAX) {
19 ✔
3333
      STREAM_CHECK_RET_GOTO(getRowRange(&colData, &window, &rowStart, &rowEnd, numOfRows));
1 ✔
3334
    } else {
3335
      (*numOfRows) = colData.nVal;
18 ✔
3336
    }
3337
  } else {
3338
    uint64_t nRow = 0;
26,748 ✔
3339
    if (tDecodeU64v(pCoder, &nRow) < 0) {
26,741 ✔
3340
      code = TSDB_CODE_INVALID_MSG;
×
3341
      TSDB_CHECK_CODE(code, lino, end);
×
3342
    }
3343

3344
    if (expandRollupGids) {
26,741 ✔
3345
      int32_t rowsPos = pCoder->pos;
1,838 ✔
3346
      int32_t numOfTargets = taosArrayGetSize(targets);
1,838 ✔
3347
      for (int32_t i = 0; i < numOfTargets; ++i) {
3,685 ✔
3348
        SWalDataTarget* target = taosArrayGet(targets, i);
1,847 ✔
3349
        STREAM_CHECK_NULL_GOTO(target, terrno);
1,847 ✔
3350
        int32_t targetRows = 0;
1,847 ✔
3351
        pCoder->pos = rowsPos;
1,847 ✔
3352
        for (uint64_t iRow = 0; iRow < nRow; ++iRow) {
3,702 ✔
3353
          SRow* pRow = (SRow*)(pCoder->data + pCoder->pos);
1,855 ✔
3354
          pCoder->pos += pRow->len;
1,855 ✔
3355
          if ((target->window.skey != INT64_MIN || target->window.ekey != INT64_MAX) &&
1,855 ✔
3356
              (pRow->ts < target->window.skey || pRow->ts > target->window.ekey)) {
1,837 ✔
3357
            continue;
2 ✔
3358
          }
3359
          targetRows++;
1,853 ✔
3360
        }
3361
        STREAM_CHECK_RET_GOTO(addWalDataSlice(sStreamReaderInfo, rsp, *uid, target->gid, targetRows, ver));
1,847 ✔
3362
        *gid = target->gid;
1,847 ✔
3363
        *numOfRows += targetRows;
1,847 ✔
3364
      }
3365
    } else if (window.skey != INT64_MIN || window.ekey != INT64_MAX) {
24,903 ✔
3366
      for (uint64_t iRow = 0; iRow < nRow; ++iRow) {
49,594 ✔
3367
        SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
32,587 ✔
3368
        pCoder->pos += pRow->len;
32,587 ✔
3369
        if (pRow->ts < window.skey || pRow->ts > window.ekey) {
32,587 ✔
3370
          continue;
90 ✔
3371
        }
3372
        (*numOfRows)++;
32,497 ✔
3373
      }
3374
    } else {
3375
      (*numOfRows) = nRow;
7,896 ✔
3376
    }
3377
  }
3378
  
3379
end:
40,135 ✔
3380
  taosArrayDestroy(targets);
40,120 ✔
3381
  tdDestroySVCreateTbReq(submitTbData.pCreateTbReq);
40,108 ✔
3382
  taosMemoryFreeClear(submitTbData.pCreateTbReq);
40,099 ✔
3383
  tEndDecode(pCoder);
40,100 ✔
3384
  return code;
40,095 ✔
3385
}
3386

3387
static int32_t scanSubmitDataPre(SStreamTriggerReaderInfo* sStreamReaderInfo, void* data, int32_t len, SSHashObj* ranges, SSTriggerWalNewRsp* rsp, int64_t ver) {
39,618 ✔
3388
  int32_t  code = 0;
39,618 ✔
3389
  int32_t  lino = 0;
39,618 ✔
3390
  SDecoder decoder = {0};
39,618 ✔
3391
  void* pTask = sStreamReaderInfo->pTask;
39,617 ✔
3392

3393
  tDecoderInit(&decoder, data, len);
39,617 ✔
3394
  if (tStartDecode(&decoder) < 0) {
39,599 ✔
3395
    code = TSDB_CODE_INVALID_MSG;
×
3396
    TSDB_CHECK_CODE(code, lino, end);
×
3397
  }
3398

3399
  uint64_t nSubmitTbData = 0;
39,621 ✔
3400
  if (tDecodeU64v(&decoder, &nSubmitTbData) < 0) {
39,625 ✔
3401
    code = TSDB_CODE_INVALID_MSG;
×
3402
    TSDB_CHECK_CODE(code, lino, end);
×
3403
  }
3404
  ST_TASK_DLOG("%s nSubmitTbData:%" PRIu64 ", ver:%"PRId64 " bodyLen:%d", __func__, nSubmitTbData, ver, len);
39,625 ✔
3405

3406
  bool expandRollupGids = isRollupMultiReader(sStreamReaderInfo);
39,628 ✔
3407
  for (int32_t i = 0; i < nSubmitTbData; i++) {
79,725 ✔
3408
    uint64_t gid = -1;
40,120 ✔
3409
    int64_t  uid = 0;
40,118 ✔
3410
    int32_t numOfRows = 0;
40,121 ✔
3411
    STREAM_CHECK_RET_GOTO(scanSubmitTbDataPre(&decoder, sStreamReaderInfo, ranges, &gid, &uid, &numOfRows, rsp, ver));
40,121 ✔
3412
    if (numOfRows <= 0) {
40,108 ✔
3413
      ST_TASK_DLOG("%s no valid data uid:%" PRId64 ", gid:%" PRIu64 ", numOfRows:%d, ver:%"PRId64, __func__, uid, gid, numOfRows, ver);
13,373 ✔
3414
      continue;
13,373 ✔
3415
    }
3416
    if (!expandRollupGids) {
26,735 ✔
3417
      STREAM_CHECK_RET_GOTO(addWalDataSlice(sStreamReaderInfo, rsp, uid, gid, numOfRows, ver));
24,906 ✔
3418
    }
3419
  }
3420

3421
  tEndDecode(&decoder);
39,605 ✔
3422

3423
end:
39,583 ✔
3424
  tDecoderClear(&decoder);
39,587 ✔
3425
  return code;
39,634 ✔
3426
}
3427

3428
static void buildIndexHash(SSHashObj* indexHash, void* pTask){
3,097 ✔
3429
  void*   pe = NULL;
3,097 ✔
3430
  int32_t iter = 0;
3,097 ✔
3431
  int32_t index = 0;
3,097 ✔
3432
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
7,000 ✔
3433
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
3,880 ✔
3434
    pInfo->startRowIdx = index;
3,880 ✔
3435
    pInfo->currentRowIdx = index;
3,880 ✔
3436
    index += pInfo->numRows;
3,880 ✔
3437
    ST_TASK_DLOG("%s uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", __func__, pInfo->uid, pInfo->gId,
3,932 ✔
3438
                 pInfo->startRowIdx, pInfo->numRows);
3439
  }
3440
}
3,099 ✔
3441

3442
static void printIndexHash(SSHashObj* indexHash, void* pTask){
3,094 ✔
3443
  if (qDebugFlag & DEBUG_TRACE) {
3,094 ✔
3444
    void*   pe = NULL;
72 ✔
3445
    int32_t iter = 0;
72 ✔
3446
    while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
172 ✔
3447
      SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
88 ✔
3448
      ST_TASK_TLOG("%s uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d", __func__, pInfo->uid, pInfo->gId,
114 ✔
3449
                   pInfo->startRowIdx, pInfo->numRows);
3450
    }
3451
  }
3452
}
3,094 ✔
3453

3454
static void filterIndexHash(SSHashObj* indexHash, SColumnInfoData* pRet){
56 ✔
3455
  void*   pe = NULL;
56 ✔
3456
  int32_t iter = 0;
56 ✔
3457
  int32_t index = 0;
56 ✔
3458
  int32_t pIndex = 0;
56 ✔
3459
  int8_t* pIndicator = (int8_t*)pRet->pData;
56 ✔
3460
  while ((pe = tSimpleHashIterate(indexHash, pe, &iter)) != NULL) {
187 ✔
3461
    SStreamWalDataSlice* pInfo = (SStreamWalDataSlice*)pe;
131 ✔
3462
    pInfo->startRowIdx = index;
131 ✔
3463
    int32_t size = pInfo->numRows;
131 ✔
3464
    for (int32_t i = 0; i < pInfo->numRows; i++) {
1,620 ✔
3465
      if (pIndicator && !pIndicator[pIndex++]) {
1,489 ✔
3466
        size--;
302 ✔
3467
      }
3468
    }
3469
    pInfo->numRows = size;
131 ✔
3470
    index += pInfo->numRows;
131 ✔
3471
    stTrace("stream reader re build index hash uid:%" PRId64 ", gid:%" PRIu64 ", startRowIdx:%d, numRows:%d",
131 ✔
3472
            pInfo->uid, pInfo->gId, pInfo->startRowIdx, pInfo->numRows);
3473
  }
3474
}
55 ✔
3475

3476
static int32_t prepareIndexMetaData(SWalReader* pWalReader, SStreamTriggerReaderInfo* sStreamReaderInfo,
28,563 ✔
3477
                                    SSTriggerWalNewRsp* resultRsp, int64_t endVer) {
3478
  int32_t      code = 0;
28,563 ✔
3479
  int32_t      lino = 0;
28,563 ✔
3480
  void* pTask = sStreamReaderInfo->pTask;
28,563 ✔
3481

3482
  code = walReaderSeekVer(pWalReader, resultRsp->ver);
28,563 ✔
3483
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
28,579 ✔
3484
    if (resultRsp->ver < walGetFirstVer(pWalReader->pWal)) {
25,300 ✔
3485
      resultRsp->ver = walGetFirstVer(pWalReader->pWal);
4 ✔
3486
      resultRsp->verTime = 0;
4 ✔
3487
    } else {
3488
      resultRsp->verTime = taosGetTimestampUs();
25,280 ✔
3489
    }
3490
    ST_TASK_DLOG("%s scan wal end:%s",  __func__, tstrerror(code));
25,284 ✔
3491
    code = TSDB_CODE_SUCCESS;
25,288 ✔
3492
    goto end;
25,288 ✔
3493
  }
3494
  STREAM_CHECK_RET_GOTO(code);
3,279 ✔
3495

3496
  while (1) {
21,812 ✔
3497
    code = walNextValidMsg(pWalReader, true);
25,025 ✔
3498
    if (code == TSDB_CODE_WAL_LOG_NOT_EXIST){
24,997 ✔
3499
      resultRsp->verTime = taosGetTimestampUs();
3,229 ✔
3500
      ST_TASK_DLOG("%s scan wal end:%s", __func__, tstrerror(code));
3,229 ✔
3501
      code = TSDB_CODE_SUCCESS;
3,229 ✔
3502
      goto end;
3,229 ✔
3503
    }
3504
    STREAM_CHECK_RET_GOTO(code);
21,773 ✔
3505
    SWalCont* wCont = &pWalReader->pHead->head;
21,773 ✔
3506
    if (endVer > 0 && wCont->version >= endVer) {
21,773 ✔
3507
      resultRsp->ver = endVer;
52 ✔
3508
      break;
52 ✔
3509
    }
3510
    resultRsp->ver = pWalReader->curVersion;
21,721 ✔
3511
    resultRsp->verTime = wCont->ingestTs;
21,721 ✔
3512
    void*   data = POINTER_SHIFT(walContBody(wCont), sizeof(SMsgHead));
21,721 ✔
3513
    int32_t len = walContBodyLen(wCont) - sizeof(SMsgHead);
21,721 ✔
3514
    int64_t ver = wCont->version;
21,721 ✔
3515
    ST_TASK_DLOG("%s scan wal ver:%" PRId64 ", type:%s, deleteData:%d, deleteTb:%d, msg len:%d", __func__,
21,721 ✔
3516
      ver, TMSG_INFO(wCont->msgType), sStreamReaderInfo->deleteReCalc, sStreamReaderInfo->deleteOutTbl, len);
3517
    if (wCont->msgType == TDMT_VND_SUBMIT) {
21,729 ✔
3518
      // return when getting data if there are meta data in vtable scan
3519
      if (shouldStopBeforeSubmitAfterTableChange(sStreamReaderInfo, resultRsp)) {
20,752 ✔
3520
        resultRsp->ver--;
×
3521
        break;
×
3522
      }
3523
      data = POINTER_SHIFT(walContBody(wCont), sizeof(SSubmitReq2Msg));
20,748 ✔
3524
      len = walContBodyLen(wCont) - sizeof(SSubmitReq2Msg);
20,748 ✔
3525
      STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, data, len, NULL, resultRsp, ver));
20,748 ✔
3526
    } else {
3527
      STREAM_CHECK_RET_GOTO(processMeta(wCont->msgType, sStreamReaderInfo, data, len, resultRsp, ver));
977 ✔
3528
    }
3529

3530
    ST_TASK_DLOG("%s scan wal next ver:%" PRId64 ", totalRows:%d", __func__, resultRsp->ver, resultRsp->totalRows);
21,741 ✔
3531
    if (resultRsp->totalRows >= STREAM_RETURN_ROWS_NUM || resultRsp->needReturn) {
21,739 ✔
3532
      break;
3533
    }
3534
  }
3535
  
3536
end:
×
3537
  STREAM_PRINT_LOG_END(code, lino);
28,563 ✔
3538
  return code;
28,566 ✔
3539
}
3540

3541
static int32_t prepareIndexData(SWalReader* pWalReader, SStreamTriggerReaderInfo* sStreamReaderInfo, SArray* versions,
10,176 ✔
3542
                                SSHashObj* ranges, SSTriggerWalNewRsp* rsp, int64_t firstVer, int64_t* pReadVer) {
3543
  int32_t      code = 0;
10,176 ✔
3544
  int32_t      lino = 0;
10,176 ✔
3545
  void* pTask = sStreamReaderInfo->pTask;
10,176 ✔
3546

3547
  for(int32_t i = 0; i < taosArrayGetSize(versions); i++) {
29,053 ✔
3548
    int64_t *ver = taosArrayGet(versions, i);
18,880 ✔
3549
    if (ver == NULL || *ver < firstVer) continue;
18,878 ✔
3550

3551
    *pReadVer = *ver;
18,864 ✔
3552
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, *ver));
18,864 ✔
3553
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
18,862 ✔
3554
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
×
3555
      ST_TASK_TLOG("%s not data, skip, ver:%"PRId64, __func__, *ver);
×
3556
      continue;
×
3557
    }
3558
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
18,862 ✔
3559

3560
    SWalCont* wCont = &pWalReader->pHead->head;
18,860 ✔
3561
    void*   pBody = POINTER_SHIFT(walContBody(wCont), sizeof(SSubmitReq2Msg));
18,860 ✔
3562
    int32_t bodyLen = walContBodyLen(wCont) - sizeof(SSubmitReq2Msg);
18,860 ✔
3563

3564
    STREAM_CHECK_RET_GOTO(scanSubmitDataPre(sStreamReaderInfo, pBody, bodyLen, ranges, rsp, *ver));
18,892 ✔
3565
  }
3566
  
3567
end:
10,169 ✔
3568
  return code;
10,157 ✔
3569
}
3570

3571
static int32_t filterData(SSTriggerWalNewRsp* resultRsp, SStreamTriggerReaderInfo* sStreamReaderInfo) {
3,097 ✔
3572
  int32_t      code = 0;
3,097 ✔
3573
  int32_t       lino = 0;
3,097 ✔
3574
  SColumnInfoData* pRet = NULL;
3,097 ✔
3575

3576
  int64_t totalRows = ((SSDataBlock*)resultRsp->dataBlock)->info.rows;
3,097 ✔
3577
  STREAM_CHECK_RET_GOTO(qStreamFilter(((SSDataBlock*)resultRsp->dataBlock), sStreamReaderInfo->pFilterInfo, &pRet));
3,097 ✔
3578

3579
  if (((SSDataBlock*)resultRsp->dataBlock)->info.rows < totalRows) {
3,097 ✔
3580
    filterIndexHash(resultRsp->indexHash, pRet);
56 ✔
3581
  }
3582

3583
end:
3,096 ✔
3584
  colDataDestroy(pRet);
3,096 ✔
3585
  taosMemoryFree(pRet);
3,097 ✔
3586
  return code;
3,096 ✔
3587
}
3588

3589
static int32_t processWalVerMetaDataNew(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo,
28,576 ✔
3590
                                        SSTriggerWalNewRsp* resultRsp, int64_t endVer) {
3591
  int32_t      code = 0;
28,576 ✔
3592
  int32_t      lino = 0;
28,576 ✔
3593
  void* pTask = sStreamReaderInfo->pTask;
28,576 ✔
3594
                                        
3595
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
28,576 ✔
3596
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
28,563 ✔
3597
  blockDataEmpty(resultRsp->dataBlock);
28,563 ✔
3598
  blockDataEmpty(resultRsp->metaBlock);
28,561 ✔
3599
  int64_t lastVer = resultRsp->ver;
28,568 ✔
3600
  STREAM_CHECK_RET_GOTO(prepareIndexMetaData(pWalReader, sStreamReaderInfo, resultRsp, endVer));
28,568 ✔
3601
  STREAM_CHECK_CONDITION_GOTO(resultRsp->totalRows == 0, TDB_CODE_SUCCESS);
28,564 ✔
3602

3603
  buildIndexHash(resultRsp->indexHash, pTask);
1,019 ✔
3604
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(((SSDataBlock*)resultRsp->dataBlock), resultRsp->totalRows));
1,019 ✔
3605
  while(lastVer < resultRsp->ver) {
12,303 ✔
3606
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, lastVer++));
11,283 ✔
3607
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
11,275 ✔
3608
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
240 ✔
3609
      continue;
240 ✔
3610
    }
3611
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
11,035 ✔
3612
    SWalCont* wCont = &pWalReader->pHead->head;
11,037 ✔
3613
    void*   pBody = POINTER_SHIFT(walContBody(wCont), sizeof(SSubmitReq2Msg));
11,037 ✔
3614
    int32_t bodyLen = walContBodyLen(wCont) - sizeof(SSubmitReq2Msg);
11,037 ✔
3615
    ST_TASK_DLOG("process wal ver:%" PRId64 ", type:%d, bodyLen:%d", wCont->version, wCont->msgType, bodyLen);
11,037 ✔
3616
    STREAM_CHECK_RET_GOTO(scanSubmitData(pVnode, sStreamReaderInfo, pBody, bodyLen, NULL, resultRsp, wCont->version));
11,181 ✔
3617
  }
3618

3619
  int32_t metaRows = resultRsp->totalRows - ((SSDataBlock*)resultRsp->dataBlock)->info.rows;
1,020 ✔
3620
  STREAM_CHECK_RET_GOTO(filterData(resultRsp, sStreamReaderInfo));
1,020 ✔
3621
  resultRsp->totalRows = ((SSDataBlock*)resultRsp->dataBlock)->info.rows + metaRows;
1,019 ✔
3622

3623
end:
28,565 ✔
3624
  ST_TASK_DLOG("vgId:%d %s end, get result totalRows:%d, process:%"PRId64"/%"PRId64, TD_VID(pVnode), __func__, 
28,565 ✔
3625
          resultRsp->totalRows, resultRsp->ver, walGetAppliedVer(pWalReader->pWal));
3626
  walCloseReader(pWalReader);
28,566 ✔
3627
  return code;
28,564 ✔
3628
}
3629

3630
static int32_t processWalVerDataNew(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo, 
10,174 ✔
3631
                                    SArray* versions, SSHashObj* ranges, SSTriggerWalNewRsp* rsp) {
3632
  int32_t      code = 0;
10,174 ✔
3633
  int32_t      lino = 0;
10,174 ✔
3634
  int64_t      firstVer = walGetFirstVer(pVnode->pWal);
10,174 ✔
3635
  int64_t      readVer = firstVer;
10,174 ✔
3636

3637
  void* pTask = sStreamReaderInfo->pTask;
10,174 ✔
3638
  SWalReader* pWalReader = walOpenReader(pVnode->pWal, 0);
10,174 ✔
3639
  STREAM_CHECK_NULL_GOTO(pWalReader, terrno);
10,173 ✔
3640
  
3641
  if (taosArrayGetSize(versions) > 0) {
10,173 ✔
3642
    rsp->ver = *(int64_t*)taosArrayGetLast(versions);
2,085 ✔
3643
  }
3644

3645
retry:
10,157 ✔
3646
  // Use one retention boundary for both passes so the slice sizes match the rows.
3647
  STREAM_CHECK_RET_GOTO(prepareIndexData(pWalReader, sStreamReaderInfo, versions, ranges, rsp, firstVer, &readVer));
10,177 ✔
3648
  STREAM_CHECK_CONDITION_GOTO(rsp->totalRows == 0, TDB_CODE_SUCCESS);
10,168 ✔
3649

3650
  ST_TASK_TLOG("%s index hash:%p %d", __func__, rsp->indexHash, tSimpleHashGetSize(rsp->indexHash));
2,079 ✔
3651
  buildIndexHash(rsp->indexHash, pTask);
2,079 ✔
3652

3653
  blockDataEmpty(rsp->dataBlock);
2,079 ✔
3654
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(rsp->dataBlock, rsp->totalRows));
2,080 ✔
3655

3656
  for(int32_t i = 0; i < taosArrayGetSize(versions); i++) {
20,947 ✔
3657
    int64_t *ver = taosArrayGet(versions, i);
18,866 ✔
3658
    if (ver == NULL || *ver < firstVer) continue;
18,868 ✔
3659
    ST_TASK_TLOG("vgId:%d %s scan wal process:%"PRId64"/%"PRId64, TD_VID(pVnode), __func__, *ver, walGetAppliedVer(pWalReader->pWal));
18,858 ✔
3660

3661
    readVer = *ver;
18,858 ✔
3662
    STREAM_CHECK_RET_GOTO(walFetchHead(pWalReader, *ver));
18,858 ✔
3663
    if(pWalReader->pHead->head.msgType != TDMT_VND_SUBMIT) {
18,854 ✔
3664
      TAOS_CHECK_RETURN(walSkipFetchBody(pWalReader));
×
3665
      continue;
×
3666
    }
3667
    STREAM_CHECK_RET_GOTO(walFetchBody(pWalReader));
18,854 ✔
3668
    SWalCont* wCont = &pWalReader->pHead->head;
18,851 ✔
3669
    void*   pBody = POINTER_SHIFT(walContBody(wCont), sizeof(SSubmitReq2Msg));
18,851 ✔
3670
    int32_t bodyLen = walContBodyLen(wCont) - sizeof(SSubmitReq2Msg);
18,851 ✔
3671

3672
    STREAM_CHECK_RET_GOTO(scanSubmitData(pVnode, sStreamReaderInfo, pBody, bodyLen, ranges, rsp, wCont->version));
18,861 ✔
3673
  }
3674
  // printDataBlock(rsp->dataBlock, __func__, "processWalVerDataNew");
3675
  STREAM_CHECK_RET_GOTO(filterData(rsp, sStreamReaderInfo));
2,078 ✔
3676
  rsp->totalRows = ((SSDataBlock*)rsp->dataBlock)->info.rows;
2,078 ✔
3677

3678
end:
10,176 ✔
3679
  if (code == TSDB_CODE_WAL_LOG_NOT_EXIST || code == TSDB_CODE_WAL_INVALID_VER || code == TAOS_SYSTEM_ERROR(ENOENT)) {
10,177 ✔
3680
    int64_t retainedVer = walGetFirstVer(pVnode->pWal);
7 ✔
3681
    if (pWalReader != NULL && retainedVer > firstVer && readVer < retainedVer) {
7 ✔
3682
      // Retention can remove a segment during either pass. Discard the partial
3683
      // result and rebuild it from the remaining requested versions.
3684
      ST_TASK_DLOG("vgId:%d WAL retention advanced from:%" PRId64 " to:%" PRId64 ", restart data read at:%" PRId64,
3 ✔
3685
                   TD_VID(pVnode), firstVer, retainedVer, readVer);
3686
      firstVer = retainedVer;
3 ✔
3687
      walReadReset(pWalReader);
3 ✔
3688
      tSimpleHashClear(rsp->indexHash);
3 ✔
3689
      tSimpleHashClear(rsp->uidHash);
3 ✔
3690
      blockDataEmpty(rsp->dataBlock);
3 ✔
3691
      rsp->totalRows = 0;
3 ✔
3692
      rsp->totalDataRows = 0;
3 ✔
3693
      code = TSDB_CODE_SUCCESS;
3 ✔
3694
      goto retry;
3 ✔
3695
    }
3696
  }
3697
  ST_TASK_DLOG("vgId:%d %s end, get result totalRows:%d, process:%" PRId64 "/%" PRId64, TD_VID(pVnode), __func__,
10,174 ✔
3698
               rsp->totalRows, rsp->ver, walGetAppliedVer(pVnode->pWal));
3699
  walCloseReader(pWalReader);
10,174 ✔
3700
  return code;
10,173 ✔
3701
}
3702

3703
static int32_t buildScheamFromMeta(void* pTask, SVnode* pVnode, int64_t uid, SArray** schemas, SStorageAPI* api) {
973 ✔
3704
  int32_t code = 0;
973 ✔
3705
  int32_t lino = 0;
973 ✔
3706
  SMetaReader metaReader = {0};
973 ✔
3707
  *schemas = taosArrayInit(8, sizeof(SSchema));
973 ✔
3708
  STREAM_CHECK_NULL_GOTO(*schemas, terrno);
973 ✔
3709
  
3710
  api->metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &api->metaFn, 0);
973 ✔
3711
  STREAM_CHECK_RET_GOTO(api->metaReaderFn.getTableEntryByUid(&metaReader, uid));
973 ✔
3712

3713
  SSchemaWrapper* sSchemaWrapper = NULL;
971 ✔
3714
  if (metaReader.me.type == TD_CHILD_TABLE) {
971 ✔
3715
    int64_t suid = metaReader.me.ctbEntry.suid;
962 ✔
3716
    tDecoderClear(&metaReader.coder);
962 ✔
3717
    STREAM_CHECK_RET_GOTO(api->metaReaderFn.getTableEntryByUid(&metaReader, suid));
962 ✔
3718
    sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
962 ✔
3719
  } else if (metaReader.me.type == TD_NORMAL_TABLE) {
9 ✔
3720
    sSchemaWrapper = &metaReader.me.ntbEntry.schemaRow;
9 ✔
3721
  } else {
3722
    ST_TASK_ELOG("invalid table type:%d for uid:%" PRId64, metaReader.me.type, uid);
×
3723
  }
3724

3725
  for (size_t j = 0; j < sSchemaWrapper->nCols; j++) {
5,544 ✔
3726
    SSchema* s = sSchemaWrapper->pSchema + j;
4,573 ✔
3727
    STREAM_CHECK_NULL_GOTO(taosArrayPush(*schemas, s), terrno);
9,146 ✔
3728
  }
3729

3730
end:
973 ✔
3731
  api->metaReaderFn.clearReader(&metaReader);
973 ✔
3732
  STREAM_PRINT_LOG_END(code, lino);
973 ✔
3733
  if (code != 0)  {
973 ✔
3734
    taosArrayDestroy(*schemas);
2 ✔
3735
    *schemas = NULL;
2 ✔
3736
  }
3737
  return code;
973 ✔
3738
}
3739

3740
static int32_t shrinkScheams(SArray* cols, SArray* schemas) {
971 ✔
3741
  int32_t code = 0;
971 ✔
3742
  int32_t lino = 0;
971 ✔
3743
  size_t  schemaLen = taosArrayGetSize(schemas);
971 ✔
3744
  STREAM_CHECK_RET_GOTO(taosArrayEnsureCap(schemas, schemaLen + taosArrayGetSize(cols)));
971 ✔
3745
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
3,690 ✔
3746
    col_id_t* id = taosArrayGet(cols, i);
2,719 ✔
3747
    STREAM_CHECK_NULL_GOTO(id, terrno);
2,719 ✔
3748
    for (size_t i = 0; i < schemaLen; i++) {
6,896 ✔
3749
      SSchema* s = taosArrayGet(schemas, i);
6,896 ✔
3750
      STREAM_CHECK_NULL_GOTO(s, terrno);
6,896 ✔
3751
      if (*id == s->colId) {
6,896 ✔
3752
        STREAM_CHECK_NULL_GOTO(taosArrayPush(schemas, s), terrno);
2,719 ✔
3753
        break;
2,719 ✔
3754
      }
3755
    }
3756
  }
3757
  taosArrayPopFrontBatch(schemas, schemaLen);
971 ✔
3758

3759
end:
971 ✔
3760
  return code;
971 ✔
3761
}
3762

3763
static int32_t createTSAndCondition(int64_t start, int64_t end, SLogicConditionNode** pCond,
×
3764
                                    STargetNode* pTargetNodeTs) {
3765
  int32_t code = 0;
×
3766
  int32_t lino = 0;
×
3767

3768
  SColumnNode*         pCol = NULL;
×
3769
  SColumnNode*         pCol1 = NULL;
×
3770
  SValueNode*          pVal = NULL;
×
3771
  SValueNode*          pVal1 = NULL;
×
3772
  SOperatorNode*       op = NULL;
×
3773
  SOperatorNode*       op1 = NULL;
×
3774
  SLogicConditionNode* cond = NULL;
×
3775

3776
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pCol));
×
3777
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
×
3778
  pCol->node.resType.type = TSDB_DATA_TYPE_TIMESTAMP;
×
3779
  pCol->node.resType.bytes = LONG_BYTES;
×
3780
  pCol->slotId = pTargetNodeTs->slotId;
×
3781
  pCol->dataBlockId = pTargetNodeTs->dataBlockId;
×
3782

3783
  STREAM_CHECK_RET_GOTO(nodesCloneNode((SNode*)pCol, (SNode**)&pCol1));
×
3784

3785
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&pVal));
×
3786
  pVal->node.resType.type = TSDB_DATA_TYPE_BIGINT;
×
3787
  pVal->node.resType.bytes = LONG_BYTES;
×
3788
  pVal->datum.i = start;
×
3789
  pVal->typeData = start;
×
3790

3791
  STREAM_CHECK_RET_GOTO(nodesCloneNode((SNode*)pVal, (SNode**)&pVal1));
×
3792
  pVal1->datum.i = end;
×
3793
  pVal1->typeData = end;
×
3794

3795
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op));
×
3796
  op->opType = OP_TYPE_GREATER_EQUAL;
×
3797
  op->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
3798
  op->node.resType.bytes = CHAR_BYTES;
×
3799
  op->pLeft = (SNode*)pCol;
×
3800
  op->pRight = (SNode*)pVal;
×
3801
  pCol = NULL;
×
3802
  pVal = NULL;
×
3803

3804
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op1));
×
3805
  op1->opType = OP_TYPE_LOWER_EQUAL;
×
3806
  op1->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
3807
  op1->node.resType.bytes = CHAR_BYTES;
×
3808
  op1->pLeft = (SNode*)pCol1;
×
3809
  op1->pRight = (SNode*)pVal1;
×
3810
  pCol1 = NULL;
×
3811
  pVal1 = NULL;
×
3812

3813
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond));
×
3814
  cond->condType = LOGIC_COND_TYPE_AND;
×
3815
  cond->node.resType.type = TSDB_DATA_TYPE_BOOL;
×
3816
  cond->node.resType.bytes = CHAR_BYTES;
×
3817
  STREAM_CHECK_RET_GOTO(nodesMakeList(&cond->pParameterList));
×
3818
  STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)op));
×
3819
  op = NULL;
×
3820
  STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)op1));
×
3821
  op1 = NULL;
×
3822

3823
  *pCond = cond;
×
3824

3825
end:
×
3826
  if (code != 0) {
×
3827
    nodesDestroyNode((SNode*)pCol);
×
3828
    nodesDestroyNode((SNode*)pCol1);
×
3829
    nodesDestroyNode((SNode*)pVal);
×
3830
    nodesDestroyNode((SNode*)pVal1);
×
3831
    nodesDestroyNode((SNode*)op);
×
3832
    nodesDestroyNode((SNode*)op1);
×
3833
    nodesDestroyNode((SNode*)cond);
×
3834
  }
3835
  STREAM_PRINT_LOG_END(code, lino);
×
3836

3837
  return code;
×
3838
}
3839

3840
/*
3841
static int32_t createExternalConditions(SStreamRuntimeFuncInfo* data, SLogicConditionNode** pCond, STargetNode* pTargetNodeTs, STimeRangeNode* node) {
3842
  int32_t              code = 0;
3843
  int32_t              lino = 0;
3844
  SLogicConditionNode* pAndCondition = NULL;
3845
  SLogicConditionNode* cond = NULL;
3846

3847
  if (pTargetNodeTs == NULL) {
3848
    vError("stream reader %s no ts column", __func__);
3849
    return TSDB_CODE_STREAM_NOT_TABLE_SCAN_PLAN;
3850
  }
3851
  STREAM_CHECK_RET_GOTO(nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond));
3852
  cond->condType = LOGIC_COND_TYPE_OR;
3853
  cond->node.resType.type = TSDB_DATA_TYPE_BOOL;
3854
  cond->node.resType.bytes = CHAR_BYTES;
3855
  STREAM_CHECK_RET_GOTO(nodesMakeList(&cond->pParameterList));
3856

3857
  for (int i = 0; i < taosArrayGetSize(data->pStreamPesudoFuncVals); ++i) {
3858
    data->curIdx = i;
3859

3860
    SReadHandle handle = {0};
3861
    calcTimeRange(node, data, &handle.winRange, &handle.winRangeValid);
3862
    if (!handle.winRangeValid) {
3863
      stError("stream reader %s invalid time range, skey:%" PRId64 ", ekey:%" PRId64, __func__, handle.winRange.skey,
3864
              handle.winRange.ekey);
3865
      continue;
3866
    }
3867
    STREAM_CHECK_RET_GOTO(createTSAndCondition(handle.winRange.skey, handle.winRange.ekey, &pAndCondition, pTargetNodeTs));
3868
    stDebug("%s create condition skey:%" PRId64 ", eksy:%" PRId64, __func__, handle.winRange.skey, handle.winRange.ekey);
3869
    STREAM_CHECK_RET_GOTO(nodesListAppend(cond->pParameterList, (SNode*)pAndCondition));
3870
    pAndCondition = NULL;
3871
  }
3872

3873
  *pCond = cond;
3874

3875
end:
3876
  if (code != 0) {
3877
    nodesDestroyNode((SNode*)pAndCondition);
3878
    nodesDestroyNode((SNode*)cond);
3879
  }
3880
  STREAM_PRINT_LOG_END(code, lino);
3881

3882
  return code;
3883
}
3884
*/
3885

3886
int32_t vnodeCollectNodeGroupReadRange(const SStreamRuntimeFuncInfo* pRuntime, STimeWindow* pRange) {
1 ✔
3887
  if (pRuntime == NULL || pRange == NULL || taosArrayGetSize(pRuntime->curGrpRead) <= 0) {
1 ✔
3888
    return TSDB_CODE_INVALID_PARA;
×
3889
  }
3890

3891
  pRange->skey = INT64_MAX;
1 ✔
3892
  pRange->ekey = INT64_MIN;
1 ✔
3893
  for (int32_t i = 0; i < taosArrayGetSize(pRuntime->curGrpRead); ++i) {
3 ✔
3894
    const SSTriggerGroupReadInfo* pRead = taosArrayGet(pRuntime->curGrpRead, i);
2 ✔
3895
    if (pRead == NULL) return TSDB_CODE_INVALID_PARA;
2 ✔
3896
    pRange->skey = TMIN(pRange->skey, pRead->firstParam.wstart);
2 ✔
3897
    pRange->ekey = TMAX(pRange->ekey, pRead->lastParam.wend);
2 ✔
3898
  }
3899
  return TSDB_CODE_SUCCESS;
×
3900
}
3901

3902
int32_t vnodeCalculateNodeGroupReadRange(STimeRangeNode* node, SStreamRuntimeFuncInfo* pRuntime,
14 ✔
3903
                                         SStreamRuntimeFuncInfo* pTmpRuntime, int32_t actualNodeId, STimeWindow* pRange,
3904
                                         bool* pRangeValid) {
3905
  return vnodeCalculateNodeGroupReadRanges(node, pRuntime, pTmpRuntime, actualNodeId, pRange, pRangeValid, NULL);
14 ✔
3906
}
3907

3908
int32_t vnodeCalculateNodeGroupReadRanges(STimeRangeNode* node, SStreamRuntimeFuncInfo* pRuntime,
358 ✔
3909
                                          SStreamRuntimeFuncInfo* pTmpRuntime, int32_t actualNodeId,
3910
                                          STimeWindow* pRange, bool* pRangeValid, SArray** readRanges) {
3911
  if (readRanges != NULL) *readRanges = NULL;
358 ✔
3912
  if (node == NULL || pRuntime == NULL || pTmpRuntime == NULL || pRange == NULL || pRangeValid == NULL) {
358 ✔
3913
    return TSDB_CODE_INVALID_PARA;
×
3914
  }
3915
  *pRangeValid = false;
358 ✔
3916
  const int32_t readCount = taosArrayGetSize(pRuntime->curGrpRead);
358 ✔
3917
  if (readCount <= 0) return TSDB_CODE_INVALID_PARA;
358 ✔
3918

3919
  int32_t                      code = TSDB_CODE_SUCCESS;
358 ✔
3920
  SArray*                      pRanges = NULL;
358 ✔
3921
  STimeWindow                  combined = {.skey = INT64_MAX, .ekey = INT64_MIN};
358 ✔
3922
  bool                         combinedValid = false;
358 ✔
3923
  SStreamReaderCalcProjection* pProjection = NULL;
358 ✔
3924
  if (readRanges != NULL) {
358 ✔
3925
    pRanges = taosArrayInit(readCount, sizeof(SReaderReadRange));
130 ✔
3926
    if (pRanges == NULL) return terrno;
130 ✔
3927
  }
3928
  if (node->needCalc) {
358 ✔
3929
    code = stPrepareReaderCalcProjection(pRuntime, actualNodeId, &pProjection);
99 ✔
3930
    if (code != TSDB_CODE_SUCCESS) goto _exit;
99 ✔
3931
  }
3932
  for (int32_t readInfoIndex = 0; readInfoIndex < readCount; ++readInfoIndex) {
2,008 ✔
3933
    const SSTriggerGroupReadInfo* pGrp = taosArrayGet(pRuntime->curGrpRead, readInfoIndex);
1,673 ✔
3934
    if (pGrp == NULL) {
1,673 ✔
3935
      code = TSDB_CODE_INVALID_PARA;
×
3936
      goto _exit;
23 ✔
3937
    }
3938
    SReaderReadRange current = {.readInfoIndex = readInfoIndex};
1,673 ✔
3939
    if (!node->needCalc) {
1,673 ✔
3940
      current.window.skey = pGrp->firstParam.wstart;
688 ✔
3941
      current.window.ekey = pGrp->lastParam.wend;
688 ✔
3942
      if (current.window.skey > current.window.ekey ||
688 ✔
3943
          (pRuntime->triggerType == STREAM_TRIGGER_SLIDING &&
681 ✔
3944
           (current.window.ekey == INT64_MIN || current.window.skey >= current.window.ekey))) {
375 ✔
3945
        code = TSDB_CODE_INVALID_PARA;
17 ✔
3946
        goto _exit;
17 ✔
3947
      }
3948
      current.valid = true;
671 ✔
3949
      if (pRuntime->triggerType == STREAM_TRIGGER_SLIDING) --current.window.ekey;
671 ✔
3950
    } else {
3951
      code = stProjectPreparedReaderCalcContext(pProjection, readInfoIndex, pTmpRuntime);
985 ✔
3952
      if (code != TSDB_CODE_SUCCESS) goto _exit;
985 ✔
3953
      SSTriggerCalcParam* pTmp = taosArrayGet(pTmpRuntime->pStreamPesudoFuncVals, 0);
985 ✔
3954
      if (pTmp == NULL) {
985 ✔
3955
        code = TSDB_CODE_INVALID_PARA;
×
3956
        goto _exit;
×
3957
      }
3958
      memcpy(pTmp, &pGrp->firstParam, sizeof(*pTmp));
985 ✔
3959
      code = streamCalcCurrWinTimeRange(node, pTmpRuntime, &current.window, &current.valid, 1);
985 ✔
3960
      if (code != TSDB_CODE_SUCCESS) goto _exit;
985 ✔
3961
      if (current.valid) {
984 ✔
3962
        const int64_t skey = current.window.skey;
984 ✔
3963
        memcpy(pTmp, &pGrp->lastParam, sizeof(*pTmp));
984 ✔
3964
        code = streamCalcCurrWinTimeRange(node, pTmpRuntime, &current.window, &current.valid, 2);
984 ✔
3965
        if (code != TSDB_CODE_SUCCESS) goto _exit;
984 ✔
3966
        if (current.valid) current.window.skey = skey;
983 ✔
3967
      }
3968
      if (!current.valid || current.window.ekey == INT64_MIN) {
983 ✔
3969
        code = TSDB_CODE_INVALID_PARA;
4 ✔
3970
        goto _exit;
4 ✔
3971
      }
3972
      current.valid = current.window.skey < current.window.ekey;
979 ✔
3973
      if (current.valid) --current.window.ekey;
979 ✔
3974
    }
3975
    if (current.valid) {
1,650 ✔
3976
      // combined remains the effective MAIN range. Per-group reader ranges widen
3977
      // only for predecessor lookup and therefore intentionally keep a different skey.
3978
      combined.skey = TMIN(combined.skey, current.window.skey);
1,631 ✔
3979
      combined.ekey = TMAX(combined.ekey, current.window.ekey);
1,631 ✔
3980
      combinedValid = true;
1,631 ✔
3981
      if (pRanges != NULL && pRuntime->withExternalWindow && pRuntime->externalWindowNeedsPrevRow) {
1,631 ✔
3982
        current.window.skey = INT64_MIN;
6 ✔
3983
      }
3984
    }
3985
    // Preserve one entry per curGrpRead index. qInstallReaderTableRanges uses
3986
    // valid=false entries as empty bindings and requires positional parity.
3987
    if (pRanges != NULL && taosArrayPush(pRanges, &current) == NULL) {
2,026 ✔
3988
      code = terrno;
×
3989
      goto _exit;
×
3990
    }
3991
  }
3992
  if (combinedValid) *pRange = combined;
335 ✔
3993
  *pRangeValid = combinedValid;
335 ✔
3994
  if (readRanges != NULL) {
335 ✔
3995
    *readRanges = pRanges;
118 ✔
3996
    pRanges = NULL;
118 ✔
3997
  }
3998

3999
_exit:
319 ✔
4000
  taosArrayDestroy(pRanges);
358 ✔
4001
  stDestroyReaderCalcProjection(&pProjection);
358 ✔
4002
  return code;
358 ✔
4003
}
4004

4005
static int32_t processCalaTimeRange(SStreamTriggerReaderCalcInfo* sStreamReaderCalcInfo, SResFetchReq* req,
11,107 ✔
4006
                                    STimeRangeNode* node, SReadHandle* handle, bool isExtWin, int32_t actualNodeId,
4007
                                    SArray** readRanges) {
4008
  int32_t      code = 0;
11,107 ✔
4009
  int32_t      lino = 0;
11,107 ✔
4010
  void*        pTask = sStreamReaderCalcInfo->pTask;
11,107 ✔
4011
  STimeWindow* pWin = isExtWin ? &handle->extWinRange : &handle->winRange;
11,107 ✔
4012
  bool*        pValid = isExtWin ? &handle->extWinRangeValid : &handle->winRangeValid;
11,107 ✔
4013

4014
  if (req->pStRtFuncInfo->withExternalWindow) {
11,107 ✔
4015
    sStreamReaderCalcInfo->tmpRtFuncInfo.triggerType = req->pStRtFuncInfo->triggerType;
936 ✔
4016
    sStreamReaderCalcInfo->tmpRtFuncInfo.isWindowTrigger = req->pStRtFuncInfo->isWindowTrigger;
936 ✔
4017
    sStreamReaderCalcInfo->tmpRtFuncInfo.precision = req->pStRtFuncInfo->precision;
936 ✔
4018

4019
    if (req->pStRtFuncInfo->isMultiGroupCalc) {
936 ✔
4020
      STimeWindow combined = {.skey = INT64_MAX, .ekey = INT64_MIN};
319 ✔
4021
      bool        combinedValid = false;
319 ✔
4022
      STREAM_CHECK_RET_GOTO(vnodeCalculateNodeGroupReadRanges(node, req->pStRtFuncInfo,
319 ✔
4023
                                                              &sStreamReaderCalcInfo->tmpRtFuncInfo, actualNodeId,
4024
                                                              &combined, &combinedValid, readRanges));
4 ✔
4025
      if (combinedValid) *pWin = combined;
319 ✔
4026
      *pValid = combinedValid;
319 ✔
4027
    } else {
4028
      SSTriggerCalcParam* pFirst = taosArrayGet(req->pStRtFuncInfo->pStreamPesudoFuncVals, 0);
617 ✔
4029
      SSTriggerCalcParam* pLast = taosArrayGetLast(req->pStRtFuncInfo->pStreamPesudoFuncVals);
617 ✔
4030
      const int32_t       lastParamIndex = taosArrayGetSize(req->pStRtFuncInfo->pStreamPesudoFuncVals) - 1;
617 ✔
4031
      STREAM_CHECK_NULL_GOTO(pFirst, terrno);
617 ✔
4032
      STREAM_CHECK_NULL_GOTO(pLast, terrno);
617 ✔
4033
      if (!node->needCalc) {
617 ✔
4034
        pWin->skey = pFirst->wstart;
428 ✔
4035
        pWin->ekey = pLast->wend;
428 ✔
4036
        *pValid = true;
428 ✔
4037
        if (req->pStRtFuncInfo->triggerType == STREAM_TRIGGER_SLIDING) pWin->ekey--;
428 ✔
4038
      } else {
4039
        SSTriggerCalcParam* pTmp = taosArrayGet(sStreamReaderCalcInfo->tmpRtFuncInfo.pStreamPesudoFuncVals, 0);
189 ✔
4040
        STREAM_CHECK_NULL_GOTO(pTmp, terrno);
189 ✔
4041
        STREAM_CHECK_RET_GOTO(
189 ✔
4042
            stProjectReaderCalcContext(req->pStRtFuncInfo, actualNodeId, -1, 0, &sStreamReaderCalcInfo->tmpRtFuncInfo));
2 ✔
4043
        memcpy(pTmp, pFirst, sizeof(*pTmp));
189 ✔
4044
        STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, &sStreamReaderCalcInfo->tmpRtFuncInfo, pWin, pValid, 1));
189 ✔
4045
        if (*pValid) {
189 ✔
4046
          const int64_t skey = pWin->skey;
189 ✔
4047
          STREAM_CHECK_RET_GOTO(stProjectReaderCalcContext(req->pStRtFuncInfo, actualNodeId, -1, lastParamIndex,
189 ✔
4048
                                                           &sStreamReaderCalcInfo->tmpRtFuncInfo));
2 ✔
4049
          memcpy(pTmp, pLast, sizeof(*pTmp));
189 ✔
4050
          STREAM_CHECK_RET_GOTO(
189 ✔
4051
              streamCalcCurrWinTimeRange(node, &sStreamReaderCalcInfo->tmpRtFuncInfo, pWin, pValid, 2));
2 ✔
4052
          if (*pValid) pWin->skey = skey;
189 ✔
4053
        }
4054
        pWin->ekey--;
189 ✔
4055
      }
4056
    }
4057
  } else {
4058
    if (!node->needCalc) {
10,171 ✔
4059
      SSTriggerCalcParam* pCurr = taosArrayGet(req->pStRtFuncInfo->pStreamPesudoFuncVals, req->pStRtFuncInfo->curIdx);
9,970 ✔
4060
      pWin->skey = pCurr->wstart;
9,970 ✔
4061
      pWin->ekey = pCurr->wend;
9,970 ✔
4062
      *pValid = true;
9,970 ✔
4063
      if (req->pStRtFuncInfo->triggerType == STREAM_TRIGGER_SLIDING) {
9,970 ✔
4064
        pWin->ekey--;
7,789 ✔
4065
      }
4066
    } else {
4067
      STREAM_CHECK_RET_GOTO(streamCalcCurrWinTimeRange(node, req->pStRtFuncInfo, pWin, pValid, 3));
201 ✔
4068
      pWin->ekey--;
201 ✔
4069
    }
4070
  }
4071

4072
  ST_TASK_DLOG("%s type:%s, withExternalWindow:%d, skey:%" PRId64 ", ekey:%" PRId64 ", validRange:%d", __func__,
11,152 ✔
4073
               isExtWin ? "interp range" : "scan time range", req->pStRtFuncInfo->withExternalWindow, pWin->skey,
4074
               pWin->ekey, *pValid);
4075

4076
end:
46 ✔
4077

4078
  if (code) {
11,107 ✔
4079
    ST_TASK_ELOG("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4080
  }
4081

4082
  return code;
11,107 ✔
4083
}
4084

4085
static int32_t createDataBlockTsUid(SSDataBlock** pBlockRet, uint32_t numOfRows) {
2,318 ✔
4086
  int32_t      code = 0;
2,318 ✔
4087
  int32_t      lino = 0;
2,318 ✔
4088
  SSDataBlock* pBlock = NULL;
2,318 ✔
4089
  STREAM_CHECK_RET_GOTO(createDataBlock(&pBlock));
2,318 ✔
4090
  SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_TIMESTAMP, LONG_BYTES, PRIMARYKEY_TIMESTAMP_COL_ID);
2,317 ✔
4091
  STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
2,318 ✔
4092
  idata = createColumnInfoData(TSDB_DATA_TYPE_BIGINT, LONG_BYTES, PRIMARYKEY_TIMESTAMP_COL_ID + 1);
2,317 ✔
4093
  STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
2,316 ✔
4094
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, numOfRows));
2,318 ✔
4095

4096
end:
2,318 ✔
4097
  STREAM_PRINT_LOG_END(code, lino)
2,318 ✔
4098
  if (code != TSDB_CODE_SUCCESS) {
2,318 ✔
4099
    blockDataDestroy(pBlock);
×
4100
    pBlock = NULL;
×
4101
  }
4102
  *pBlockRet = pBlock;
2,318 ✔
4103
  return code;
2,318 ✔
4104
}
4105

4106
static int32_t processTsOutPutAllTables(SStreamTriggerReaderInfo* sStreamReaderInfo, SStreamTsResponse* tsRsp, SSDataBlock* pResBlock, int32_t order) {
887 ✔
4107
  int32_t code = 0;
887 ✔
4108
  int32_t lino = 0;
887 ✔
4109
  void* pTask = sStreamReaderInfo->pTask;
887 ✔
4110

4111
  tsRsp->tsInfo = taosArrayInit(pResBlock->info.rows, sizeof(STsInfo));
887 ✔
4112
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
887 ✔
4113
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
887 ✔
4114
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
887 ✔
4115
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
22,231 ✔
4116
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
42,688 ✔
4117
      continue;
×
4118
    }
4119
    STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
21,344 ✔
4120
    STREAM_CHECK_NULL_GOTO(tsInfo, terrno)
21,344 ✔
4121
    if (order == TSDB_ORDER_ASC) {
21,344 ✔
4122
      tsInfo->ts = INT64_MAX;
715 ✔
4123
    } else {
4124
      tsInfo->ts = INT64_MIN;
20,629 ✔
4125
    }
4126
    int64_t ts = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
21,344 ✔
4127
    if (order == TSDB_ORDER_ASC && ts < tsInfo->ts) {
21,344 ✔
4128
      tsInfo->ts = ts;
715 ✔
4129
    } else if (order == TSDB_ORDER_DESC && ts > tsInfo->ts) {
20,629 ✔
4130
      tsInfo->ts = ts;
20,629 ✔
4131
    }
4132
    tsInfo->gId = *(int64_t*)colDataGetNumData(pColInfoDataUid, j);
21,344 ✔
4133
    ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
21,344 ✔
4134
  }
4135

4136
end:
887 ✔
4137
  return code;
887 ✔
4138
}
4139

4140
static int32_t processTsOutPutOneGroup(SStreamTriggerReaderInfo* sStreamReaderInfo, SStreamTsResponse* tsRsp,
559 ✔
4141
                                       SSDataBlock* pResBlock, int32_t order, uint64_t gid) {
4142
  int32_t code = 0;
559 ✔
4143
  int32_t lino = 0;
559 ✔
4144
  void* pTask = sStreamReaderInfo->pTask;
559 ✔
4145

4146
  tsRsp->tsInfo = taosArrayInit(1, sizeof(STsInfo));
559 ✔
4147
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
559 ✔
4148
  STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
559 ✔
4149
  STREAM_CHECK_NULL_GOTO(tsInfo, terrno)
559 ✔
4150
  if (order == TSDB_ORDER_ASC) {
559 ✔
4151
    tsInfo->ts = INT64_MAX;
415 ✔
4152
  } else {
4153
    tsInfo->ts = INT64_MIN;
144 ✔
4154
  }
4155

4156
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
559 ✔
4157
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
559 ✔
4158
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
1,196 ✔
4159
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
1,274 ✔
4160
      continue;
×
4161
    }
4162
    int64_t ts = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
637 ✔
4163
    if (order == TSDB_ORDER_ASC && ts < tsInfo->ts) {
637 ✔
4164
      tsInfo->ts = ts;
415 ✔
4165
    } else if (order == TSDB_ORDER_DESC && ts > tsInfo->ts) {
222 ✔
4166
      tsInfo->ts = ts;
144 ✔
4167
    }
4168
  }
4169
  if (gid != 0) {
559 ✔
4170
    tsInfo->gId = gid;
192 ✔
4171
  } else {
4172
    int64_t uid = *(int64_t*)colDataGetNumData(pColInfoDataUid, 0);
367 ✔
4173
    tsInfo->gId = qStreamGetGroupIdFromSet(sStreamReaderInfo, uid);
367 ✔
4174
  }
4175
  ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
559 ✔
4176

4177
end:
136 ✔
4178
  return code;
559 ✔
4179
}
4180

4181
static int32_t processTsOutPutAllGroups(SStreamTriggerReaderInfo* sStreamReaderInfo, SStreamTsResponse* tsRsp, SSDataBlock* pResBlock, int32_t order) {
60 ✔
4182
  int32_t code = 0;
60 ✔
4183
  int32_t lino = 0;
60 ✔
4184
  STableKeyInfo* pList = NULL;
60 ✔
4185
  StreamTableListInfo     tableInfo = {0};
60 ✔
4186

4187
  void* pTask = sStreamReaderInfo->pTask;
60 ✔
4188
  STREAM_CHECK_RET_GOTO(qStreamCopyTableInfo(sStreamReaderInfo, &tableInfo));
60 ✔
4189

4190
  SSHashObj*   uidTsHash = tSimpleHashInit(pResBlock->info.rows, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
60 ✔
4191
  STREAM_CHECK_NULL_GOTO(uidTsHash, terrno);
60 ✔
4192
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
60 ✔
4193
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
60 ✔
4194
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
346 ✔
4195
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
572 ✔
4196
      continue;
×
4197
    }
4198
    int64_t ts = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
286 ✔
4199
    int64_t uid = *(int64_t*)colDataGetNumData(pColInfoDataUid, j);
286 ✔
4200
    STREAM_CHECK_RET_GOTO(tSimpleHashPut(uidTsHash, &uid, LONG_BYTES, &ts, LONG_BYTES));
286 ✔
4201
  }
4202
  tsRsp->tsInfo = taosArrayInit(qStreamGetTableListGroupNum(sStreamReaderInfo), sizeof(STsInfo));
60 ✔
4203
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
60 ✔
4204
  while (true) {
283 ✔
4205
    int32_t pNum = 0;
343 ✔
4206
    int64_t gId = 0;
343 ✔
4207
    STREAM_CHECK_RET_GOTO(qStreamIterTableList(&tableInfo, &pList, &pNum, &gId));
343 ✔
4208
    if (pNum == 0) break;
343 ✔
4209

4210
    int64_t ts = (order == TSDB_ORDER_ASC) ? INT64_MAX : INT64_MIN;
283 ✔
4211
    bool    hasTs = false;
283 ✔
4212
    for (int32_t i = 0; i < pNum; i++) {
574 ✔
4213
      int64_t  uid = pList[i].uid;
291 ✔
4214
      int64_t* rowTs = tSimpleHashGet(uidTsHash, &uid, LONG_BYTES);
291 ✔
4215
      if (rowTs == NULL) {
291 ✔
4216
        continue;
5 ✔
4217
      }
4218
      if (!hasTs || (order == TSDB_ORDER_ASC && *rowTs < ts) || (order == TSDB_ORDER_DESC && *rowTs > ts)) {
286 ✔
4219
        ts = *rowTs;
282 ✔
4220
        hasTs = true;
282 ✔
4221
      }
4222
    }
4223
    if (!hasTs) {
283 ✔
4224
      taosMemoryFreeClear(pList);
2 ✔
4225
      continue;
2 ✔
4226
    }
4227

4228
    STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
281 ✔
4229
    STREAM_CHECK_NULL_GOTO(tsInfo, terrno);
281 ✔
4230
    tsInfo->ts = ts;
281 ✔
4231
    tsInfo->gId = gId;
281 ✔
4232
    ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
281 ✔
4233
    taosMemoryFreeClear(pList);
281 ✔
4234
  }
4235

4236
end:
60 ✔
4237
  qStreamDestroyTableInfo(&tableInfo);
60 ✔
4238
  taosMemoryFreeClear(pList);
60 ✔
4239
  tSimpleHashCleanup(uidTsHash);
60 ✔
4240
  return code;
60 ✔
4241
}
4242

4243
static int32_t processTsOutPutRollupGroups(SStreamTriggerReaderInfo* sStreamReaderInfo, SStreamTsResponse* tsRsp,
4 ✔
4244
                                           SSDataBlock* pResBlock, int32_t order) {
4245
  int32_t             code = 0;
4 ✔
4246
  int32_t             lino = 0;
4 ✔
4247
  STableKeyInfo*      pList = NULL;
4 ✔
4248
  StreamTableListInfo tableInfo = {0};
4 ✔
4249
  SSHashObj*          uidTsHash = NULL;
4 ✔
4250

4251
  void* pTask = sStreamReaderInfo->pTask;
4 ✔
4252
  STREAM_CHECK_RET_GOTO(qStreamCopyTableInfo(sStreamReaderInfo, &tableInfo));
4 ✔
4253

4254
  uidTsHash = tSimpleHashInit(pResBlock->info.rows, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
4 ✔
4255
  STREAM_CHECK_NULL_GOTO(uidTsHash, terrno);
4 ✔
4256
  SColumnInfoData* pColInfoDataTs = taosArrayGet(pResBlock->pDataBlock, 0);
4 ✔
4257
  SColumnInfoData* pColInfoDataUid = taosArrayGet(pResBlock->pDataBlock, 1);
4 ✔
4258
  STREAM_CHECK_NULL_GOTO(pColInfoDataTs, terrno);
4 ✔
4259
  STREAM_CHECK_NULL_GOTO(pColInfoDataUid, terrno);
4 ✔
4260
  for (int32_t j = 0; j < pResBlock->info.rows; j++) {
8 ✔
4261
    if (colDataIsNull_s(pColInfoDataTs, j) || pColInfoDataTs->pData == NULL) {
8 ✔
4262
      continue;
×
4263
    }
4264
    int64_t  rowTs = *(int64_t*)colDataGetNumData(pColInfoDataTs, j);
4 ✔
4265
    int64_t  rowUid = *(int64_t*)colDataGetNumData(pColInfoDataUid, j);
4 ✔
4266
    int64_t* savedTs = tSimpleHashGet(uidTsHash, &rowUid, LONG_BYTES);
4 ✔
4267
    if (savedTs == NULL || (order == TSDB_ORDER_ASC && rowTs < *savedTs) ||
4 ✔
4268
        (order == TSDB_ORDER_DESC && rowTs > *savedTs)) {
×
4269
      STREAM_CHECK_RET_GOTO(tSimpleHashPut(uidTsHash, &rowUid, LONG_BYTES, &rowTs, LONG_BYTES));
4 ✔
4270
    }
4271
  }
4272

4273
  tsRsp->tsInfo = taosArrayInit(qStreamGetTableListGroupNum(sStreamReaderInfo), sizeof(STsInfo));
4 ✔
4274
  STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
4 ✔
4275

4276
  while (true) {
12 ✔
4277
    int32_t pNum = 0;
16 ✔
4278
    int64_t gId = 0;
16 ✔
4279
    STREAM_CHECK_RET_GOTO(qStreamIterTableList(&tableInfo, &pList, &pNum, &gId));
16 ✔
4280
    if (pNum == 0) break;
16 ✔
4281

4282
    int64_t          ts = (order == TSDB_ORDER_ASC) ? INT64_MAX : INT64_MIN;
12 ✔
4283
    bool             hasTs = false;
12 ✔
4284
    for (int32_t i = 0; i < pNum; i++) {
24 ✔
4285
      int64_t uid = pList[i].uid;
12 ✔
4286
      int64_t* rowTs = tSimpleHashGet(uidTsHash, &uid, LONG_BYTES);
12 ✔
4287
      if (rowTs == NULL) {
12 ✔
4288
        continue;
×
4289
      }
4290
      if (!hasTs || (order == TSDB_ORDER_ASC && *rowTs < ts) || (order == TSDB_ORDER_DESC && *rowTs > ts)) {
12 ✔
4291
        ts = *rowTs;
12 ✔
4292
        hasTs = true;
12 ✔
4293
      }
4294
    }
4295
    if (!hasTs) {
12 ✔
4296
      taosMemoryFreeClear(pList);
×
4297
      continue;
×
4298
    }
4299

4300
    STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
12 ✔
4301
    STREAM_CHECK_NULL_GOTO(tsInfo, terrno);
12 ✔
4302
    tsInfo->ts = ts;
12 ✔
4303
    tsInfo->gId = gId;
12 ✔
4304
    ST_TASK_DLOG("%s get ts:%" PRId64 ", gId:%" PRIu64 ", ver:%" PRId64, __func__, tsInfo->ts, tsInfo->gId, tsRsp->ver);
12 ✔
4305
    taosMemoryFreeClear(pList);
12 ✔
4306
  }
4307

4308
end:
4 ✔
4309
  qStreamDestroyTableInfo(&tableInfo);
4 ✔
4310
  taosMemoryFreeClear(pList);
4 ✔
4311
  tSimpleHashCleanup(uidTsHash);
4 ✔
4312
  return code;
4 ✔
4313
}
4314

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

4317
static int32_t getAllTs(void* pTask, SVnode* pVnode, SSDataBlock* pResBlock, SStreamReaderTaskInner* pTaskInner,
2,071 ✔
4318
                        STableKeyInfo* pList, int32_t pNum) {
4319
  int32_t code = 0;
2,071 ✔
4320
  int32_t lino = 0;
2,071 ✔
4321

4322
  ST_TASK_DLOG("%s getAllTs enter: tables:%d suid:%" PRId64 " order:%d skey:%" PRId64 " ekey:%" PRId64
2,071 ✔
4323
               " verRange:[%" PRId64 ",%" PRId64 "]",
4324
               pTaskInner->idStr, pNum, pTaskInner->options->suid, pTaskInner->options->order,
4325
               pTaskInner->options->twindows.skey, pTaskInner->options->twindows.ekey, (int64_t)-1,
4326
               pTaskInner->options->ver);
4327
  for (int32_t i = 0; i < pNum; i++) {
26,277 ✔
4328
    ST_TASK_DLOG("%s getAllTs table[%d]: uid:%" PRId64, pTaskInner->idStr, i, pList[i].uid);
24,206 ✔
4329
  }
4330

4331
  STREAM_CHECK_RET_GOTO(pTaskInner->storageApi->tsdReader.tsdCreateFirstLastTsIter(pVnode, &pTaskInner->options->twindows, &(SVersionRange){.minVer = -1, .maxVer = pTaskInner->options->ver},
2,071 ✔
4332
                                                pTaskInner->options->suid, pList, pNum, pTaskInner->options->order, &pTaskInner->pReader, pTaskInner->idStr));
7 ✔
4333
  bool hasNext = true;
2,071 ✔
4334
  int32_t iterCount = 0;
2,071 ✔
4335
  while(1){
7 ✔
4336
    STREAM_CHECK_RET_GOTO(pTaskInner->storageApi->tsdReader.tsdNextFirstLastTsBlock(pTaskInner->pReader, pResBlock, &hasNext));
2,075 ✔
4337
    ST_TASK_DLOG("%s getAllTs iter[%d]: hasNext:%d rows:%" PRId64, pTaskInner->idStr, iterCount++, hasNext,
2,073 ✔
4338
                 pResBlock->info.rows);
4339
    STREAM_CHECK_CONDITION_GOTO(!hasNext, TDB_CODE_SUCCESS);
2,075 ✔
4340
  }
4341

4342
end:
2,071 ✔
4343
  ST_TASK_DLOG("%s getAllTs done: code:%d rows:%" PRId64, pTaskInner->idStr, code,
2,071 ✔
4344
               pResBlock == NULL ? -1 : pResBlock->info.rows);
4345
  pTaskInner->storageApi->tsdReader.tsdDestroyFirstLastTsIter(pTaskInner->pReader);
2,071 ✔
4346
  pTaskInner->pReader = NULL;
2,070 ✔
4347
  return code;
2,070 ✔
4348
}
4349

4350
static int32_t processTsVTable(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
600 ✔
4351
                                  SStreamReaderTaskInner* pTaskInner) {
4352
  int32_t code = 0;
600 ✔
4353
  int32_t lino = 0;
600 ✔
4354
  STableKeyInfo* pList = NULL;
600 ✔
4355
  StreamTableListInfo     tableInfo = {0};
600 ✔
4356

4357
  void* pTask = sStreamReaderInfo->pTask;
600 ✔
4358
  STREAM_CHECK_RET_GOTO(qStreamCopyTableInfo(sStreamReaderInfo, &tableInfo));
600 ✔
4359

4360
  SSDataBlock*  pResBlock = NULL;
600 ✔
4361
  STREAM_CHECK_RET_GOTO(createDataBlockTsUid(&pResBlock, qStreamGetTableListNum(sStreamReaderInfo)));
600 ✔
4362

4363
  while (true) {
354 ✔
4364
    int32_t        pNum = 0;
952 ✔
4365
    int64_t        suid = 0;
952 ✔
4366
    STREAM_CHECK_RET_GOTO(qStreamIterTableList(&tableInfo, &pList, &pNum, &suid));
953 ✔
4367
    if(pNum == 0) break;
953 ✔
4368
    pTaskInner->options->suid = suid;
353 ✔
4369
    STREAM_CHECK_RET_GOTO(getAllTs(pTask, pVnode, pResBlock, pTaskInner, pList, pNum));
353 ✔
4370
    taosMemoryFreeClear(pList);
352 ✔
4371
  }
4372

4373
  STREAM_CHECK_RET_GOTO(processTsOutPutAllTables(sStreamReaderInfo, tsRsp, pResBlock, pTaskInner->options->order));
600 ✔
4374

4375
end:
600 ✔
4376
  qStreamDestroyTableInfo(&tableInfo);
600 ✔
4377
  taosMemoryFreeClear(pList);
600 ✔
4378
  blockDataDestroy(pResBlock);
600 ✔
4379
  STREAM_PRINT_LOG_END_WITHID(code, lino);
600 ✔
4380
  return code;
600 ✔
4381
}
4382

4383
static int32_t processTsNonVTable(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
1,708 ✔
4384
                                  SStreamReaderTaskInner* pTaskInner) {
4385
  int32_t code = 0;
1,708 ✔
4386
  int32_t lino = 0;
1,708 ✔
4387
  STableKeyInfo* pList = NULL;
1,708 ✔
4388
  void* pTask = sStreamReaderInfo->pTask;
1,708 ✔
4389

4390
  SSDataBlock*  pResBlock = NULL;
1,708 ✔
4391

4392
  int32_t        pNum = 0;
1,708 ✔
4393
  int64_t        suid = 0;
1,708 ✔
4394
  STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, 0, &pList, &pNum));
1,708 ✔
4395
  ST_TASK_DLOG("vgId:%d %s qStreamGetTableList returned pNum:%d", TD_VID(pVnode), __func__, pNum);
1,708 ✔
4396
  STREAM_CHECK_CONDITION_GOTO(pNum == 0, TSDB_CODE_SUCCESS);
1,708 ✔
4397
  STREAM_CHECK_RET_GOTO(createDataBlockTsUid(&pResBlock, pNum));
1,526 ✔
4398

4399
  pTaskInner->options->suid = sStreamReaderInfo->suid;
1,526 ✔
4400
  ST_TASK_DLOG("vgId:%d %s calling getAllTs: suid:%"PRId64" order:%d skey:%"PRId64" ekey:%"PRId64" ver:%"PRId64,
1,526 ✔
4401
               TD_VID(pVnode), __func__, pTaskInner->options->suid, pTaskInner->options->order,
4402
               pTaskInner->options->twindows.skey, pTaskInner->options->twindows.ekey, pTaskInner->options->ver);
4403
  STREAM_CHECK_RET_GOTO(getAllTs(pTask, pVnode, pResBlock, pTaskInner, pList, pNum));
1,526 ✔
4404
  ST_TASK_DLOG("vgId:%d %s getAllTs done: pResBlock rows:%"PRId64, TD_VID(pVnode), __func__, pResBlock->info.rows);
1,525 ✔
4405
  int32_t order = pTaskInner->options->order;
1,526 ✔
4406
  if (pResBlock->info.rows == 0 && sStreamReaderInfo->groupByTbname) {
1,526 ✔
4407
    tsRsp->tsInfo = taosArrayInit(pNum, sizeof(STsInfo));
275 ✔
4408
    STREAM_CHECK_NULL_GOTO(tsRsp->tsInfo, terrno);
275 ✔
4409
    for (int32_t i = 0; i < pNum; i++) {
1,001 ✔
4410
      STsInfo* tsInfo = taosArrayReserve(tsRsp->tsInfo, 1);
726 ✔
4411
      STREAM_CHECK_NULL_GOTO(tsInfo, terrno);
726 ✔
4412
      tsInfo->gId = pList[i].uid;
726 ✔
4413
      tsInfo->ts = 0;
726 ✔
4414
      ST_TASK_DLOG("%s no data but return gId (uid):%" PRIu64 " for tbname partition", __func__, tsInfo->gId);
726 ✔
4415
    }
4416
    goto end;
275 ✔
4417
  }
4418

4419
  STREAM_CHECK_CONDITION_GOTO(pResBlock->info.rows == 0, TDB_CODE_SUCCESS);
1,251 ✔
4420

4421
  if (sStreamReaderInfo->groupByTbname) {
718 ✔
4422
    STREAM_CHECK_RET_GOTO(processTsOutPutAllTables(sStreamReaderInfo, tsRsp, pResBlock, order));
287 ✔
4423
  } else if (isRollupMultiReader(sStreamReaderInfo)) {
431 ✔
4424
    STREAM_CHECK_RET_GOTO(processTsOutPutRollupGroups(sStreamReaderInfo, tsRsp, pResBlock, order));
4 ✔
4425
  } else if (sStreamReaderInfo->partitionCols == NULL) {
427 ✔
4426
    STREAM_CHECK_RET_GOTO(processTsOutPutOneGroup(sStreamReaderInfo, tsRsp, pResBlock, order, 0));
367 ✔
4427
  } else {
4428
    STREAM_CHECK_RET_GOTO(processTsOutPutAllGroups(sStreamReaderInfo, tsRsp, pResBlock, order));
60 ✔
4429
  }
4430
end:
1,700 ✔
4431
  blockDataDestroy(pResBlock);
1,708 ✔
4432
  taosMemoryFreeClear(pList);
1,708 ✔
4433
  STREAM_PRINT_LOG_END_WITHID(code, lino);
1,708 ✔
4434
  return code;
1,708 ✔
4435
}
4436

4437
static int32_t processTsOnce(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
239 ✔
4438
                                  SStreamReaderTaskInner* pTaskInner, uint64_t gid) {
4439
  int32_t code = 0;
239 ✔
4440
  int32_t lino = 0;
239 ✔
4441
  STableKeyInfo* pList = NULL;
239 ✔
4442
  void* pTask = sStreamReaderInfo->pTask;
239 ✔
4443
  
4444
  SSDataBlock*  pResBlock = NULL;
239 ✔
4445

4446
  int32_t        pNum = 0;
239 ✔
4447
  STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, gid, &pList, &pNum));
239 ✔
4448
  STREAM_CHECK_CONDITION_GOTO(pNum == 0, TSDB_CODE_SUCCESS);
239 ✔
4449
  STREAM_CHECK_RET_GOTO(createDataBlockTsUid(&pResBlock, pNum));
192 ✔
4450

4451
  pTaskInner->options->suid = sStreamReaderInfo->suid;
192 ✔
4452
  STREAM_CHECK_RET_GOTO(getAllTs(pTask, pVnode, pResBlock, pTaskInner, pList, pNum));
192 ✔
4453
  STREAM_CHECK_CONDITION_GOTO(pResBlock->info.rows == 0, TDB_CODE_SUCCESS);
192 ✔
4454
  int32_t order = pTaskInner->options->order;
192 ✔
4455

4456
  STREAM_CHECK_RET_GOTO(processTsOutPutOneGroup(sStreamReaderInfo, tsRsp, pResBlock, order, gid));
192 ✔
4457
end:
239 ✔
4458
  blockDataDestroy(pResBlock);
239 ✔
4459
  taosMemoryFreeClear(pList);
239 ✔
4460
  STREAM_PRINT_LOG_END_WITHID(code, lino);
239 ✔
4461
  return code;
239 ✔
4462
}
4463

4464
static int32_t processTs(SVnode* pVnode, SStreamTsResponse* tsRsp, SStreamTriggerReaderInfo* sStreamReaderInfo,
2,308 ✔
4465
                                  SStreamReaderTaskInner* pTaskInner) {
4466
  if (sStreamReaderInfo->isVtableStream) {
2,308 ✔
4467
    return processTsVTable(pVnode, tsRsp, sStreamReaderInfo, pTaskInner);
600 ✔
4468
  }
4469

4470
  return processTsNonVTable(pVnode, tsRsp, sStreamReaderInfo, pTaskInner);
1,708 ✔
4471
}
4472

4473
static int32_t vnodeProcessStreamSetTableReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
418 ✔
4474
                                             SStreamTriggerReaderInfo*   sStreamReaderInfo,
4475
                                             SStreamReaderResponseStats* pRspStats) {
4476
  int32_t code = 0;
418 ✔
4477
  int32_t lino = 0;
418 ✔
4478
  void*   buf = NULL;
418 ✔
4479
  size_t  size = 0;
418 ✔
4480
  void* pTask = sStreamReaderInfo->pTask;
418 ✔
4481

4482
  ST_TASK_DLOG("vgId:%d %s start, trigger hash size:%d, calc hash size:%d, appver:%"PRId64, TD_VID(pVnode), __func__,
418 ✔
4483
                tSimpleHashGetSize(req->setTableReq.uidInfoTrigger), tSimpleHashGetSize(req->setTableReq.uidInfoCalc), pVnode->state.applied);
4484

4485
  taosWLockLatch(&sStreamReaderInfo->lock);
418 ✔
4486
  atomic_store_8(&sStreamReaderInfo->vTableReady, 0);
418 ✔
4487
  TSWAP(sStreamReaderInfo->uidHashTrigger, req->setTableReq.uidInfoTrigger);
418 ✔
4488
  TSWAP(sStreamReaderInfo->uidHashCalc, req->setTableReq.uidInfoCalc);
418 ✔
4489
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->uidHashTrigger, TSDB_CODE_INVALID_PARA);
418 ✔
4490
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->uidHashCalc, TSDB_CODE_INVALID_PARA);
418 ✔
4491

4492
  qStreamClearTableInfo(&sStreamReaderInfo->vSetTableList);
418 ✔
4493
  STREAM_CHECK_RET_GOTO(initStreamTableListInfo(&sStreamReaderInfo->vSetTableList, UIDMAP_SINGLE));
418 ✔
4494
  STREAM_CHECK_RET_GOTO(qBuildVTableList(sStreamReaderInfo));
418 ✔
4495
  atomic_store_8(&sStreamReaderInfo->vTableReady, 1);
417 ✔
4496
  ST_TASK_DLOG("vgId:%d virtual table reader is ready after set table", TD_VID(pVnode));
417 ✔
4497
end:
35 ✔
4498
  taosWUnLockLatch(&sStreamReaderInfo->lock);
419 ✔
4499
  STREAM_PRINT_LOG_END_WITHID(code, lino);
418 ✔
4500
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
418 ✔
4501
  return code;
418 ✔
4502
}
4503

4504
static int32_t vnodeProcessStreamLastTsReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
1,581 ✔
4505
                                           SStreamTriggerReaderInfo*   sStreamReaderInfo,
4506
                                           SStreamReaderResponseStats* pRspStats) {
4507
  int32_t                 code = 0;
1,581 ✔
4508
  int32_t                 lino = 0;
1,581 ✔
4509
  SStreamReaderTaskInner* pTaskInner = NULL;
1,581 ✔
4510
  SStreamTsResponse       tsRsp = {0};
1,585 ✔
4511
  void*                   buf = NULL;
1,585 ✔
4512
  size_t                  size = 0;
1,585 ✔
4513

4514
  void* pTask = sStreamReaderInfo->pTask;
1,585 ✔
4515

4516
  ST_TASK_DLOG("vgId:%d %s start", TD_VID(pVnode), __func__);
1,585 ✔
4517

4518
  BUILD_OPTION(options, 0, sStreamReaderInfo->tableList.version, TSDB_ORDER_DESC, INT64_MIN, INT64_MAX, NULL, false, NULL);
1,585 ✔
4519
  STREAM_CHECK_RET_GOTO(createStreamTaskForTs(&options, &pTaskInner, &sStreamReaderInfo->storageApi));
1,585 ✔
4520

4521
  tsRsp.ver = sStreamReaderInfo->tableList.version + 1;
1,585 ✔
4522

4523
  STREAM_CHECK_RET_GOTO(processTs(pVnode, &tsRsp, sStreamReaderInfo, pTaskInner));
1,585 ✔
4524
  
4525
end:
1,585 ✔
4526
  ST_TASK_DLOG("vgId:%d %s get result size:%"PRIzu", ver:%"PRId64, TD_VID(pVnode), __func__, taosArrayGetSize(tsRsp.tsInfo), tsRsp.ver);
1,585 ✔
4527
  code = buildTsRsp(&tsRsp, &buf, &size);
1,585 ✔
4528
  STREAM_PRINT_LOG_END_WITHID(code, lino);
1,584 ✔
4529
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
1,587 ✔
4530
  taosArrayDestroy(tsRsp.tsInfo);
1,585 ✔
4531
  taosMemoryFree(pTaskInner);
1,585 ✔
4532
  return code;
1,585 ✔
4533
}
4534

4535
static int32_t vnodeProcessStreamFirstTsReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
962 ✔
4536
                                            SStreamTriggerReaderInfo*   sStreamReaderInfo,
4537
                                            SStreamReaderResponseStats* pRspStats) {
4538
  int32_t                 code = 0;
962 ✔
4539
  int32_t                 lino = 0;
962 ✔
4540
  SStreamReaderTaskInner* pTaskInner = NULL;
962 ✔
4541
  SStreamTsResponse       tsRsp = {0};
962 ✔
4542
  void*                   buf = NULL;
962 ✔
4543
  size_t                  size = 0;
962 ✔
4544

4545
  void* pTask = sStreamReaderInfo->pTask;
962 ✔
4546
  ST_TASK_DLOG("vgId:%d %s start, startTime:%"PRId64" ver:%"PRId64" gid:%"PRId64
962 ✔
4547
               " applied:%"PRId64" tableListNum:%d isVtable:%d groupByTbname:%d partitionCols:%p",
4548
               TD_VID(pVnode), __func__, req->firstTsReq.startTime, req->firstTsReq.ver, req->firstTsReq.gid,
4549
               pVnode->state.applied, qStreamGetTableListNum(sStreamReaderInfo),
4550
               sStreamReaderInfo->isVtableStream, sStreamReaderInfo->groupByTbname, sStreamReaderInfo->partitionCols);
4551
  int32_t        pNum = 0;
962 ✔
4552

4553
  tsRsp.ver = pVnode->state.applied;
962 ✔
4554

4555
  BUILD_OPTION(options, 0, req->firstTsReq.ver, TSDB_ORDER_ASC, req->firstTsReq.startTime, INT64_MAX, NULL, false, NULL);
962 ✔
4556
  STREAM_CHECK_RET_GOTO(createStreamTaskForTs(&options, &pTaskInner, &sStreamReaderInfo->storageApi));
962 ✔
4557

4558
  if (req->firstTsReq.gid != 0) {
962 ✔
4559
    STREAM_CHECK_RET_GOTO(processTsOnce(pVnode, &tsRsp, sStreamReaderInfo, pTaskInner, req->firstTsReq.gid));
239 ✔
4560
  } else {
4561
    STREAM_CHECK_RET_GOTO(processTs(pVnode, &tsRsp, sStreamReaderInfo, pTaskInner));
723 ✔
4562
  }
4563

4564
end:
962 ✔
4565
  ST_TASK_DLOG("vgId:%d %s get result size:%"PRIzu", ver:%"PRId64, TD_VID(pVnode), __func__, taosArrayGetSize(tsRsp.tsInfo), tsRsp.ver);
962 ✔
4566
  code = buildTsRsp(&tsRsp, &buf, &size);
962 ✔
4567
  STREAM_PRINT_LOG_END_WITHID(code, lino);
962 ✔
4568
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
962 ✔
4569
  taosArrayDestroy(tsRsp.tsInfo);
962 ✔
4570
  taosMemoryFree(pTaskInner);
962 ✔
4571
  return code;
962 ✔
4572
}
4573

4574
static int32_t vnodeProcessStreamTsdbMetaReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
1,903 ✔
4575
                                             SStreamTriggerReaderInfo*   sStreamReaderInfo,
4576
                                             SStreamReaderResponseStats* pRspStats) {
4577
  int32_t code = 0;
1,903 ✔
4578
  int32_t lino = 0;
1,903 ✔
4579
  void*   buf = NULL;
1,903 ✔
4580
  size_t  size = 0;
1,903 ✔
4581
  STableKeyInfo* pList = NULL;
1,903 ✔
4582
  STableKeyInfo* pReadList = NULL;
1,903 ✔
4583
  SSHashObj*     pRollupMetaByUid = NULL;
1,903 ✔
4584
  SSHashObj*     pRollupMetaCount = NULL;
1,903 ✔
4585
  int32_t        pNum = 0;
1,903 ✔
4586
  int32_t        readNum = 0;
1,903 ✔
4587

4588
  void* pTask = sStreamReaderInfo->pTask;
1,903 ✔
4589
  ST_TASK_DLOG("vgId:%d %s start, ver:%" PRId64 ",skey:%" PRId64 ",ekey:%" PRId64 ",gid:%" PRId64, TD_VID(pVnode),
1,903 ✔
4590
               __func__, req->tsdbMetaReq.ver, req->tsdbMetaReq.startTime, req->tsdbMetaReq.endTime,
4591
               req->tsdbMetaReq.gid);
4592

4593
  SStreamReaderTaskInner* pTaskInner = NULL;
1,903 ✔
4594
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_META);
1,903 ✔
4595
  bool                    snapshotRollupMeta = isRollupMultiReader(sStreamReaderInfo);
1,903 ✔
4596
  bool                    expandRollupMeta = snapshotRollupMeta && req->tsdbMetaReq.gid == 0;
1,903 ✔
4597

4598
  if (req->base.type == STRIGGER_PULL_TSDB_META) {
1,903 ✔
4599
    STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, req->tsdbMetaReq.gid, &pList, &pNum));
1,903 ✔
4600
    STableKeyInfo* pCreateList = pList;
1,903 ✔
4601
    int32_t        createNum = pNum;
1,903 ✔
4602
    if (isRollupMultiReader(sStreamReaderInfo)) {
1,903 ✔
4603
      if (req->tsdbMetaReq.gid == 0){
1 ✔
4604
        STREAM_CHECK_RET_GOTO(buildRollupMetaSnapshot(pList, pNum, &pReadList, &readNum, &pRollupMetaByUid, &pRollupMetaCount));
1 ✔
4605
        pCreateList = pReadList;
1 ✔
4606
        createNum = readNum;
1 ✔
4607
      } else {
4608
        STREAM_CHECK_RET_GOTO(buildRollupMetaCount(pList, pNum, &pRollupMetaCount));
×
4609
      }
4610
    }
4611
    BUILD_OPTION(options, getSuid(sStreamReaderInfo, pList), req->tsdbMetaReq.ver, req->tsdbMetaReq.order, req->tsdbMetaReq.startTime, req->tsdbMetaReq.endTime, 
1,903 ✔
4612
                          sStreamReaderInfo->tsSchemas, true, NULL);
×
4613
    STREAM_CHECK_RET_GOTO(
1,903 ✔
4614
        createStreamTask(pVnode, &options, &pTaskInner, NULL, pCreateList, createNum, &sStreamReaderInfo->storageApi));
×
4615
    if (isRollupMultiReader(sStreamReaderInfo)) {
1,903 ✔
4616
      pTaskInner->pRollupMetaByUid = pRollupMetaByUid;
1 ✔
4617
      pRollupMetaByUid = NULL;
1 ✔
4618
      pTaskInner->pRollupMetaCount = pRollupMetaCount;
1 ✔
4619
      pRollupMetaCount = NULL;
1 ✔
4620
    }
4621
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
1,903 ✔
4622

4623
    STREAM_CHECK_RET_GOTO(
1,903 ✔
4624
        createBlockForTsdbMeta(&pTaskInner->pResBlockDst, sStreamReaderInfo->isVtableStream,
4625
                               isRollupMultiReader(sStreamReaderInfo)));
×
4626
  } else {
4627
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
4628
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
4629
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
4630
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
4631
  }
4632

4633
  blockDataCleanup(pTaskInner->pResBlockDst);
1,903 ✔
4634
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pTaskInner->pResBlockDst, STREAM_RETURN_ROWS_NUM));
1,903 ✔
4635
  bool hasNext = true;
1,903 ✔
4636
  while (true) {
×
4637
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
2,918 ✔
4638
    if (!hasNext) {
2,918 ✔
4639
      break;
1,903 ✔
4640
    }
4641
    pTaskInner->storageApi->tsdReader.tsdReaderReleaseDataBlock(pTaskInner->pReader);
1,015 ✔
4642
    if (isRollupMultiReader(sStreamReaderInfo)) {
1,015 ✔
4643
      if (req->tsdbMetaReq.gid != 0) {
1 ✔
4644
        int32_t rollupTbCount = getRollupMetaCount(pTaskInner->pRollupMetaCount, req->tsdbMetaReq.gid);
×
4645
        STREAM_CHECK_CONDITION_GOTO(rollupTbCount <= 0, TSDB_CODE_INTERNAL_ERROR);
×
4646
        STREAM_CHECK_RET_GOTO(appendTsdbMetaRow(sStreamReaderInfo, pTaskInner->pResBlockDst, pTaskInner->pResBlock,
×
4647
                                                req->tsdbMetaReq.gid, rollupTbCount, TD_VID(pVnode)));
4648
      } else {
4649
        SArray** ppEntries =
×
4650
            tSimpleHashGet(pTaskInner->pRollupMetaByUid, &pTaskInner->pResBlock->info.id.uid, LONG_BYTES);
1 ✔
4651
        STREAM_CHECK_NULL_GOTO(ppEntries, TSDB_CODE_INTERNAL_ERROR);
1 ✔
4652
        int32_t numOfEntries = taosArrayGetSize(*ppEntries);
1 ✔
4653
        for (int32_t i = 0; i < numOfEntries; ++i) {
4 ✔
4654
          STableKeyInfo* pInfo = taosArrayGet(*ppEntries, i);
3 ✔
4655
          STREAM_CHECK_NULL_GOTO(pInfo, TSDB_CODE_INTERNAL_ERROR);
3 ✔
4656
          int32_t rollupTbCount = getRollupMetaCount(pTaskInner->pRollupMetaCount, pInfo->groupId);
3 ✔
4657
          STREAM_CHECK_CONDITION_GOTO(rollupTbCount <= 0, TSDB_CODE_INTERNAL_ERROR);
3 ✔
4658
          STREAM_CHECK_RET_GOTO(appendTsdbMetaRow(sStreamReaderInfo, pTaskInner->pResBlockDst, pTaskInner->pResBlock,
3 ✔
4659
                                                  pInfo->groupId, rollupTbCount, TD_VID(pVnode)));
×
4660
        }
4661
      }
4662
    } else {
4663
      uint64_t gid = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
1,014 ✔
4664
      STREAM_CHECK_RET_GOTO(appendTsdbMetaRow(sStreamReaderInfo, pTaskInner->pResBlockDst, pTaskInner->pResBlock, gid,
1,014 ✔
4665
                                              0, TD_VID(pVnode)));
×
4666
    }
4667

4668
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
1,015 ✔
4669
      break;
×
4670
    }
4671
  }
4672

4673
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
1,903 ✔
4674
  STREAM_CHECK_RET_GOTO(buildRsp(pTaskInner->pResBlockDst, &buf, &size));
1,903 ✔
4675
  printDataBlock(pTaskInner->pResBlockDst, __func__, "meta", ((SStreamTask *)sStreamReaderInfo->pTask)->streamId);
1,903 ✔
4676
  if (!hasNext) {
1,903 ✔
4677
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
1,903 ✔
4678
  }
4679

4680
end:
1,903 ✔
4681
  STREAM_PRINT_LOG_END_WITHID(code, lino);
1,903 ✔
4682
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
1,903 ✔
4683
  tSimpleHashCleanup(pRollupMetaByUid);
1,902 ✔
4684
  tSimpleHashCleanup(pRollupMetaCount);
1,903 ✔
4685
  taosMemoryFree(pReadList);
1,902 ✔
4686
  taosMemoryFree(pList);
1,902 ✔
4687
  return code;
1,903 ✔
4688
}
4689

4690
static int32_t vnodeProcessStreamTsdbTsDataReqNonVTable(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
500 ✔
4691
                                                        SStreamTriggerReaderInfo*   sStreamReaderInfo,
4692
                                                        SStreamReaderResponseStats* pRspStats) {
4693
  int32_t                 code = 0;
500 ✔
4694
  int32_t                 lino = 0;
500 ✔
4695
  SStreamReaderTaskInner* pTaskInner = NULL;
500 ✔
4696
  void*                   buf = NULL;
500 ✔
4697
  size_t                  size = 0;
500 ✔
4698
  SSDataBlock*            pBlockRes = NULL;
500 ✔
4699

4700
  void* pTask = sStreamReaderInfo->pTask;
500 ✔
4701
  ST_TASK_DLOG("vgId:%d %s start, ver:%" PRId64 ",skey:%" PRId64 ",ekey:%" PRId64 ",uid:%" PRId64 ",suid:%" PRId64,
500 ✔
4702
               TD_VID(pVnode), __func__, req->tsdbTsDataReq.ver, req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey,
4703
               req->tsdbTsDataReq.uid, req->tsdbTsDataReq.suid);
4704

4705
  int32_t       pNum = 1;
500 ✔
4706
  STableKeyInfo pList = {.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, req->tsdbTsDataReq.uid),
500 ✔
4707
                         .uid = req->tsdbTsDataReq.uid};
500 ✔
4708
  STREAM_CHECK_CONDITION_GOTO(pList.groupId == -1, TSDB_CODE_INVALID_PARA);
500 ✔
4709
  BUILD_OPTION(options, getSuid(sStreamReaderInfo, &pList), req->tsdbTsDataReq.ver, TSDB_ORDER_ASC,
500 ✔
4710
               req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey, sStreamReaderInfo->triggerCols, false, NULL);
×
4711
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, &pList,
500 ✔
4712
                                         pNum, &sStreamReaderInfo->storageApi));
×
4713
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
500 ✔
4714
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->tsBlock, false, &pBlockRes));
500 ✔
4715

4716
  while (1) {
500 ✔
4717
    bool hasNext = false;
1,000 ✔
4718
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
1,000 ✔
4719
    if (!hasNext) {
1,000 ✔
4720
      break;
500 ✔
4721
    }
4722
    pTaskInner->pResBlock->info.id.groupId = pList.groupId;
500 ✔
4723

4724
    SSDataBlock* pBlock = NULL;
500 ✔
4725
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
500 ✔
4726
    if (pBlock != NULL && pBlock->info.rows > 0) {
500 ✔
4727
      STREAM_CHECK_RET_GOTO(processTag(sStreamReaderInfo, false, pBlock->info.id.uid, pBlock, 0, pBlock->info.rows, 1));
500 ✔
4728
    }
4729

4730
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderInfo->pFilterInfo, NULL));
500 ✔
4731
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
500 ✔
4732
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
500 ✔
4733
                 TD_VID(pVnode), __func__, pTaskInner->pResBlock->info.window.skey,
4734
                 pTaskInner->pResBlock->info.window.ekey, pTaskInner->pResBlock->info.id.uid,
4735
                 pTaskInner->pResBlock->info.id.groupId, pTaskInner->pResBlock->info.rows);
4736
  }
4737

4738
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
500 ✔
4739
  SSDataBlock* pRspBlock = pTaskInner->pResBlockDst;
500 ✔
4740
  bool         requiresAncestorContext =
500 ✔
4741
      (sStreamReaderInfo->triggerAst != NULL && sStreamReaderInfo->triggerAst->requiresAncestorContext) ||
962 ✔
4742
      (sStreamReaderInfo->calcAst != NULL && sStreamReaderInfo->calcAst->requiresAncestorContext);
462 ✔
4743
  if (!requiresAncestorContext) {
500 ✔
4744
    blockDataTransform(pBlockRes, pTaskInner->pResBlockDst);
462 ✔
4745
    pRspBlock = pBlockRes;
462 ✔
4746
  }
4747
  STREAM_CHECK_RET_GOTO(buildRsp(pRspBlock, &buf, &size));
500 ✔
4748
  addReaderResponseBlock(pRspStats, pRspBlock);
500 ✔
4749

4750
end:
500 ✔
4751
  STREAM_PRINT_LOG_END_WITHID(code, lino);
500 ✔
4752
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
500 ✔
4753
  blockDataDestroy(pBlockRes);
500 ✔
4754

4755
  releaseStreamTask(&pTaskInner);
500 ✔
4756
  return code;
500 ✔
4757
}
4758

4759
static int32_t vnodeProcessStreamTsdbTsDataReqVTable(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
6 ✔
4760
                                                     SStreamTriggerReaderInfo*   sStreamReaderInfo,
4761
                                                     SStreamReaderResponseStats* pRspStats) {
4762
  int32_t                 code = 0;
6 ✔
4763
  int32_t                 lino = 0;
6 ✔
4764
  SStreamReaderTaskInner* pTaskInner = NULL;
6 ✔
4765
  void*                   buf = NULL;
6 ✔
4766
  size_t                  size = 0;
6 ✔
4767
  SSDataBlock*            pBlockRes = NULL;
6 ✔
4768

4769
  void* pTask = sStreamReaderInfo->pTask;
6 ✔
4770
  ST_TASK_DLOG("vgId:%d %s start, ver:%"PRId64",skey:%"PRId64",ekey:%"PRId64",uid:%"PRId64",suid:%"PRId64, TD_VID(pVnode), __func__, req->tsdbTsDataReq.ver, 
6 ✔
4771
                req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey, 
4772
                req->tsdbTsDataReq.uid, req->tsdbTsDataReq.suid);
4773

4774
  int32_t        pNum = 1;
6 ✔
4775
  STableKeyInfo  pList = {.groupId = req->tsdbTsDataReq.uid, .uid = req->tsdbTsDataReq.uid};
6 ✔
4776
  BUILD_OPTION(options, getSuid(sStreamReaderInfo, &pList), req->tsdbTsDataReq.ver, TSDB_ORDER_ASC, req->tsdbTsDataReq.skey, req->tsdbTsDataReq.ekey,
6 ✔
4777
               sStreamReaderInfo->tsSchemas, true, NULL);
×
4778
  STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->tsBlock, &pList, pNum, &sStreamReaderInfo->storageApi));
6 ✔
4779
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->tsBlock, false, &pBlockRes));
6 ✔
4780

4781
  while (1) {
6 ✔
4782
    bool hasNext = false;
12 ✔
4783
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
12 ✔
4784
    if (!hasNext) {
12 ✔
4785
      break;
6 ✔
4786
    }
4787

4788
    SSDataBlock* pBlock = NULL;
6 ✔
4789
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
6 ✔
4790
    STREAM_CHECK_RET_GOTO(blockDataMerge(pBlockRes, pBlock));
6 ✔
4791
    ST_TASK_DLOG("vgId:%d %s get  skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
6 ✔
4792
            TD_VID(pVnode), __func__, pBlockRes->info.window.skey, pBlockRes->info.window.ekey,
4793
            pBlockRes->info.id.uid, pBlockRes->info.id.groupId, pBlockRes->info.rows);
4794
  }
4795

4796
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlockRes->info.rows);
6 ✔
4797
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
6 ✔
4798
  addReaderResponseBlock(pRspStats, pBlockRes);
6 ✔
4799

4800
end:
6 ✔
4801
  STREAM_PRINT_LOG_END_WITHID(code, lino);
6 ✔
4802
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
6 ✔
4803
  blockDataDestroy(pBlockRes);
6 ✔
4804

4805
  releaseStreamTask(&pTaskInner);
6 ✔
4806
  return code;
6 ✔
4807
}
4808

4809
static int32_t vnodeProcessStreamTsdbTriggerDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
706 ✔
4810
                                                    SStreamTriggerReaderInfo*   sStreamReaderInfo,
4811
                                                    SStreamReaderResponseStats* pRspStats) {
4812
  int32_t code = 0;
706 ✔
4813
  int32_t lino = 0;
706 ✔
4814
  void*   buf = NULL;
706 ✔
4815
  size_t  size = 0;
706 ✔
4816
  STableKeyInfo* pList = NULL;
706 ✔
4817
  SArray*        pResList = NULL;
706 ✔
4818
  SSDataBlock*   pBlockTmp = NULL;
706 ✔
4819

4820
  SStreamReaderTaskInner* pTaskInner = NULL;
706 ✔
4821
  void* pTask = sStreamReaderInfo->pTask;
706 ✔
4822
  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);
706 ✔
4823
  
4824
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_TRIGGER_DATA);
706 ✔
4825

4826
  if (req->base.type == STRIGGER_PULL_TSDB_TRIGGER_DATA) {
705 ✔
4827
    int32_t        pNum = 0;
350 ✔
4828
    STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, req->tsdbTriggerDataReq.gid, &pList, &pNum));
350 ✔
4829
    BUILD_OPTION(options, getSuid(sStreamReaderInfo, pList), req->tsdbTriggerDataReq.ver, req->tsdbTriggerDataReq.order, req->tsdbTriggerDataReq.startTime, INT64_MAX,
351 ✔
4830
                 sStreamReaderInfo->triggerCols, false, NULL);
×
4831
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, pList, pNum, &sStreamReaderInfo->storageApi));
351 ✔
4832
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
351 ✔
4833
  } else {
4834
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
355 ✔
4835
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
355 ✔
4836
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
4 ✔
4837
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
4 ✔
4838
  }
4839

4840
  blockDataCleanup(pTaskInner->pResBlockDst);
355 ✔
4841
  bool hasNext = true;
355 ✔
4842
  int32_t totalRows = 0;
355 ✔
4843
    
4844
  pResList = taosArrayInit(4, POINTER_BYTES);
355 ✔
4845
  STREAM_CHECK_NULL_GOTO(pResList, terrno);
355 ✔
4846
  while (1) {
409 ✔
4847
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
764 ✔
4848
    if (!hasNext) {
764 ✔
4849
      break;
351 ✔
4850
    }
4851
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
413 ✔
4852
    // pTaskInner->pResBlockDst->info.id.groupId = pTaskInner->pResBlock->info.id.groupId;
4853

4854
    SSDataBlock* pBlock = NULL;
413 ✔
4855
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
413 ✔
4856
    if (pBlock != NULL && pBlock->info.rows > 0) {
413 ✔
4857
      STREAM_CHECK_RET_GOTO(
413 ✔
4858
        processTag(sStreamReaderInfo, false, pBlock->info.id.uid, pBlock, 0, pBlock->info.rows, 1));
×
4859
    }
4860
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderInfo->pFilterInfo, NULL));
413 ✔
4861
    // STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
4862
    ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlock->info.rows);
413 ✔
4863
    STREAM_CHECK_RET_GOTO(createOneDataBlock(pBlock, true, &pBlockTmp));
413 ✔
4864
    STREAM_CHECK_NULL_GOTO(taosArrayPush(pResList, &pBlockTmp), terrno);
412 ✔
4865
    totalRows += blockDataGetNumOfRows(pBlockTmp);
412 ✔
4866
    pBlockTmp = NULL;
412 ✔
4867

4868
    ST_TASK_DLOG("vgId:%d %s get skey:%" PRId64 ", eksy:%" PRId64 ", uid:%" PRId64 ", gId:%" PRIu64 ", rows:%" PRId64,
412 ✔
4869
            TD_VID(pVnode), __func__, pTaskInner->pResBlock->info.window.skey, pTaskInner->pResBlock->info.window.ekey,
4870
            pTaskInner->pResBlock->info.id.uid, pTaskInner->pResBlock->info.id.groupId, pTaskInner->pResBlock->info.rows);
4871
    if (totalRows >= STREAM_RETURN_ROWS_NUM) {  //todo optimize send multi blocks in one group
413 ✔
4872
      break;
4 ✔
4873
    }
4874
  }
4875

4876
  STREAM_CHECK_RET_GOTO(buildArrayRsp(pResList, &buf, &size));
355 ✔
4877
  addReaderResponseBlocks(pRspStats, pResList);
355 ✔
4878
  if (!hasNext) {
355 ✔
4879
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
351 ✔
4880
  }
4881

4882
end:
705 ✔
4883
  STREAM_PRINT_LOG_END_WITHID(code, lino);
703 ✔
4884
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
706 ✔
4885
  taosMemoryFree(pList);
706 ✔
4886
  blockDataDestroy(pBlockTmp);
706 ✔
4887
  taosArrayDestroyP(pResList, (FDelete)blockDataDestroy);
706 ✔
4888
  return code;
706 ✔
4889
}
4890

4891
static int32_t vnodeProcessStreamTsdbCalcDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
31,423 ✔
4892
                                                 SStreamTriggerReaderInfo*   sStreamReaderInfo,
4893
                                                 SStreamReaderResponseStats* pRspStats) {
4894
  int32_t code = 0;
31,423 ✔
4895
  int32_t lino = 0;
31,423 ✔
4896
  void*   buf = NULL;
31,423 ✔
4897
  size_t  size = 0;
31,423 ✔
4898
  SSDataBlock*   pBlockRes = NULL;
31,423 ✔
4899
  STableKeyInfo* pList = NULL;
31,423 ✔
4900

4901

4902
  void* pTask = sStreamReaderInfo->pTask;
31,423 ✔
4903
  ST_TASK_DLOG("vgId:%d %s start, skey:%"PRId64",ekey:%"PRId64",gid:%"PRId64",ver:%"PRId64, TD_VID(pVnode), __func__, 
31,423 ✔
4904
    req->tsdbCalcDataReq.skey, req->tsdbCalcDataReq.ekey, req->tsdbCalcDataReq.gid, req->tsdbCalcDataReq.ver);
4905

4906
  STREAM_CHECK_NULL_GOTO(sStreamReaderInfo->triggerCols, TSDB_CODE_STREAM_NOT_TABLE_SCAN_PLAN);
31,423 ✔
4907

4908
  SStreamReaderTaskInner* pTaskInner = NULL;
31,423 ✔
4909
  int64_t                 key = getSessionKey(req->base.sessionId, STRIGGER_PULL_TSDB_CALC_DATA);
31,423 ✔
4910

4911
  if (req->base.type == STRIGGER_PULL_TSDB_CALC_DATA) {
31,423 ✔
4912
    int32_t        pNum = 0;
31,423 ✔
4913
    STREAM_CHECK_RET_GOTO(qStreamGetTableList(sStreamReaderInfo, req->tsdbCalcDataReq.gid, &pList, &pNum));
31,423 ✔
4914
    BUILD_OPTION(options, getSuid(sStreamReaderInfo, pList), req->tsdbCalcDataReq.ver, TSDB_ORDER_ASC, req->tsdbCalcDataReq.skey, req->tsdbCalcDataReq.ekey,
31,423 ✔
4915
                 sStreamReaderInfo->triggerCols, false, NULL);
×
4916
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, sStreamReaderInfo->triggerResBlock, pList, pNum, &sStreamReaderInfo->storageApi));
31,423 ✔
4917

4918
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
31,420 ✔
4919
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerResBlock, false, &pTaskInner->pResBlockDst));
31,422 ✔
4920
  } else {
4921
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
4922
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
4923
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
4924
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
4925
  }
4926

4927
  blockDataCleanup(pTaskInner->pResBlockDst);
31,421 ✔
4928
  bool hasNext = true;
31,422 ✔
4929
  while (1) {
5,419 ✔
4930
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
36,841 ✔
4931
    if (!hasNext) {
36,840 ✔
4932
      break;
31,423 ✔
4933
    }
4934
    pTaskInner->pResBlock->info.id.groupId = qStreamGetGroupIdFromSet(sStreamReaderInfo, pTaskInner->pResBlock->info.id.uid);
5,417 ✔
4935

4936
    SSDataBlock* pBlock = NULL;
5,419 ✔
4937
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
5,419 ✔
4938
    STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderInfo->pFilterInfo, NULL));
5,418 ✔
4939
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
5,419 ✔
4940
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
5,418 ✔
4941
      break;
×
4942
    }
4943
  }
4944

4945
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->calcResBlock, false, &pBlockRes));
31,422 ✔
4946
  STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlockRes, pTaskInner->pResBlockDst->info.capacity));
31,419 ✔
4947
  blockDataTransform(pBlockRes, pTaskInner->pResBlockDst);
31,419 ✔
4948
  STREAM_CHECK_RET_GOTO(buildRsp(pBlockRes, &buf, &size));
31,422 ✔
4949
  addReaderResponseBlock(pRspStats, pBlockRes);
31,422 ✔
4950
  printDataBlock(pBlockRes, __func__, "tsdb_calc_data", ((SStreamTask*)pTask)->streamId);
31,423 ✔
4951
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlockRes->info.rows);
31,422 ✔
4952
  printDataBlock(pBlockRes, __func__, "tsdb_data", ((SStreamTask*)pTask)->streamId);
31,422 ✔
4953

4954
  if (!hasNext) {
31,423 ✔
4955
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
31,423 ✔
4956
  }
4957

4958
end:
31,422 ✔
4959
  STREAM_PRINT_LOG_END_WITHID(code, lino);
31,423 ✔
4960
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
31,423 ✔
4961
  blockDataDestroy(pBlockRes);
31,422 ✔
4962
  taosMemoryFree(pList);
31,420 ✔
4963
  return code;
31,419 ✔
4964
}
4965

4966
static int32_t vnodeProcessStreamTsdbVirtalDataReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
973 ✔
4967
                                                   SStreamTriggerReaderInfo*   sStreamReaderInfo,
4968
                                                   SStreamReaderResponseStats* pRspStats) {
4969
  int32_t code = 0;
973 ✔
4970
  int32_t lino = 0;
973 ✔
4971
  void*   buf = NULL;
973 ✔
4972
  size_t  size = 0;
973 ✔
4973
  int32_t* slotIdList = NULL;
973 ✔
4974
  SArray* sortedCid = NULL;
973 ✔
4975
  SArray* schemas = NULL;
973 ✔
4976
  SSDataBlock*   pBlockRes = NULL;
973 ✔
4977
  
4978
  void* pTask = sStreamReaderInfo->pTask;
973 ✔
4979
  ST_TASK_DLOG("vgId:%d %s start, skey:%"PRId64",ekey:%"PRId64",uid:%"PRId64",ver:%"PRId64, TD_VID(pVnode), __func__, 
973 ✔
4980
    req->tsdbDataReq.skey, req->tsdbDataReq.ekey, req->tsdbDataReq.uid, req->tsdbDataReq.ver);
4981
    
4982
  SStreamReaderTaskInner* pTaskInner = NULL;
973 ✔
4983
  int64_t key = req->tsdbDataReq.uid;
973 ✔
4984

4985
  if (req->base.type == STRIGGER_PULL_TSDB_DATA) {
973 ✔
4986
    // sort cid and build slotIdList
4987
    slotIdList = taosMemoryMalloc(taosArrayGetSize(req->tsdbDataReq.cids) * sizeof(int32_t));
973 ✔
4988
    STREAM_CHECK_NULL_GOTO(slotIdList, terrno);
973 ✔
4989
    sortedCid = taosArrayDup(req->tsdbDataReq.cids, NULL);
973 ✔
4990
    STREAM_CHECK_NULL_GOTO(sortedCid, terrno);
973 ✔
4991
    taosArraySort(sortedCid, sortCid);
973 ✔
4992
    for (int32_t i = 0; i < taosArrayGetSize(req->tsdbDataReq.cids); i++) {
3,698 ✔
4993
      int16_t* cid = taosArrayGet(req->tsdbDataReq.cids, i);
2,725 ✔
4994
      STREAM_CHECK_NULL_GOTO(cid, terrno);
2,725 ✔
4995
      for (int32_t j = 0; j < taosArrayGetSize(sortedCid); j++) {
5,270 ✔
4996
        int16_t* cidSorted = taosArrayGet(sortedCid, j);
5,270 ✔
4997
        STREAM_CHECK_NULL_GOTO(cidSorted, terrno);
5,270 ✔
4998
        if (*cid == *cidSorted) {
5,270 ✔
4999
          slotIdList[j] = i;
2,725 ✔
5000
          break;
2,725 ✔
5001
        }
5002
      }
5003
    }
5004

5005
    STREAM_CHECK_RET_GOTO(buildScheamFromMeta(pTask, pVnode, req->tsdbDataReq.uid, &schemas,
973 ✔
5006
                                              &sStreamReaderInfo->storageApi));
×
5007
    STREAM_CHECK_RET_GOTO(shrinkScheams(req->tsdbDataReq.cids, schemas));
971 ✔
5008
    STREAM_CHECK_RET_GOTO(createDataBlockForStream(schemas, &pBlockRes));
971 ✔
5009

5010
    taosArraySort(schemas, sortSSchema);
971 ✔
5011
    BUILD_OPTION(options, req->tsdbDataReq.suid, req->tsdbDataReq.ver, req->tsdbDataReq.order, req->tsdbDataReq.skey,
971 ✔
5012
                    req->tsdbDataReq.ekey, schemas, true, &slotIdList);
×
5013
    STableKeyInfo       keyInfo = {.uid = req->tsdbDataReq.uid, .groupId = 0};
971 ✔
5014
    STREAM_CHECK_RET_GOTO(createStreamTask(pVnode, &options, &pTaskInner, pBlockRes, &keyInfo, 1, &sStreamReaderInfo->storageApi));
971 ✔
5015
    STREAM_CHECK_RET_GOTO(taosHashPut(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES, &pTaskInner, sizeof(pTaskInner)));
971 ✔
5016
    pTaskInner->pResBlockDst = pBlockRes;
971 ✔
5017
    pBlockRes = NULL;
971 ✔
5018
  } else {
5019
    void** tmp = taosHashGet(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES);
×
5020
    STREAM_CHECK_NULL_GOTO(tmp, TSDB_CODE_STREAM_NO_CONTEXT);
×
5021
    pTaskInner = *(SStreamReaderTaskInner**)tmp;
×
5022
    STREAM_CHECK_NULL_GOTO(pTaskInner, TSDB_CODE_INTERNAL_ERROR);
×
5023
  }
5024

5025
  blockDataCleanup(pTaskInner->pResBlockDst);
971 ✔
5026
  bool hasNext = true;
971 ✔
5027
  while (1) {
971 ✔
5028
    STREAM_CHECK_RET_GOTO(getTableDataInfo(pTaskInner, &hasNext));
1,942 ✔
5029
    if (!hasNext) {
1,942 ✔
5030
      break;
971 ✔
5031
    }
5032

5033
    SSDataBlock* pBlock = NULL;
971 ✔
5034
    STREAM_CHECK_RET_GOTO(getTableData(pTaskInner, &pBlock));
971 ✔
5035
    STREAM_CHECK_RET_GOTO(blockDataMerge(pTaskInner->pResBlockDst, pBlock));
971 ✔
5036
    if (pTaskInner->pResBlockDst->info.rows >= STREAM_RETURN_ROWS_NUM) {
971 ✔
5037
      break;
×
5038
    }
5039
  }
5040
  STREAM_CHECK_RET_GOTO(buildRsp(pTaskInner->pResBlockDst, &buf, &size));
971 ✔
5041
  addReaderResponseBlock(pRspStats, pTaskInner->pResBlockDst);
971 ✔
5042
  ST_TASK_DLOG("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pTaskInner->pResBlockDst->info.rows);
971 ✔
5043
  printDataBlock(pTaskInner->pResBlockDst, __func__, "tsdb_data", ((SStreamTask*)pTask)->streamId);
971 ✔
5044
  if (!hasNext) {
971 ✔
5045
    STREAM_CHECK_RET_GOTO(taosHashRemove(sStreamReaderInfo->streamTaskMap, &key, LONG_BYTES));
971 ✔
5046
  }
5047

5048
end:
973 ✔
5049
  STREAM_PRINT_LOG_END_WITHID(code, lino);
973 ✔
5050
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
973 ✔
5051
  taosMemFree(slotIdList);
973 ✔
5052
  taosArrayDestroy(sortedCid);
973 ✔
5053
  taosArrayDestroy(schemas);
973 ✔
5054
  blockDataDestroy(pBlockRes);
973 ✔
5055
  return code;
973 ✔
5056
}
5057

5058
static int32_t vnodeProcessStreamWalMetaNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
20,872 ✔
5059
                                               SStreamTriggerReaderInfo*   sStreamReaderInfo,
5060
                                               SStreamReaderResponseStats* pRspStats) {
5061
  int32_t      code = 0;
20,872 ✔
5062
  int32_t      lino = 0;
20,872 ✔
5063
  void*        buf = NULL;
20,872 ✔
5064
  size_t       size = 0;
20,872 ✔
5065
  int64_t      lastVer = 0;
20,872 ✔
5066
  SSTriggerWalNewRsp resultRsp = {0};
20,872 ✔
5067

5068
  void* pTask = sStreamReaderInfo->pTask;
20,871 ✔
5069
  ST_TASK_DLOG("vgId:%d %s start, request paras lastVer:%" PRId64, TD_VID(pVnode), __func__, req->walMetaNewReq.lastVer);
20,871 ✔
5070

5071
  if (sStreamReaderInfo->metaBlock == NULL) {
20,874 ✔
5072
    STREAM_CHECK_RET_GOTO(
798 ✔
5073
        createBlockForWalMetaNew((SSDataBlock**)&sStreamReaderInfo->metaBlock,
5074
                                 isRollupMultiReader(sStreamReaderInfo)));
6 ✔
5075
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(sStreamReaderInfo->metaBlock, STREAM_RETURN_ROWS_NUM));
798 ✔
5076
  }
5077
  blockDataEmpty(sStreamReaderInfo->metaBlock);
20,874 ✔
5078
  resultRsp.metaBlock = sStreamReaderInfo->metaBlock;
20,873 ✔
5079
  resultRsp.ver = req->walMetaNewReq.lastVer;
20,873 ✔
5080
  {
5081
    int32_t hookRc = streamMaybeRecheckVTableCache(pVnode, sStreamReaderInfo, resultRsp.ver, &resultRsp);
20,873 ✔
5082
    ST_TASK_DLOG("vgId:%d %s hook rc=0x%x ver=%" PRId64, TD_VID(pVnode), __func__, hookRc, resultRsp.ver);
20,870 ✔
5083
    // H2 v0.5: any non-zero hook rc (TAG_CHANGED, REF_TABLE_NOT_EXIST,
5084
    // REF_COL_NOT_EXIST, REF_TOO_DEEP, RPC failure, etc.) is propagated to
5085
    // the trigger via rsp.code so it can fail-fast and request a redeploy.
5086
    if (hookRc != 0) { code = hookRc; goto end; }
20,869 ✔
5087
  }
5088
  STREAM_CHECK_RET_GOTO(processWalVerMetaNew(pVnode, &resultRsp, sStreamReaderInfo, req->walMetaNewReq.ctime));
20,868 ✔
5089

5090
  ST_TASK_DLOG("vgId:%d %s get result last ver:%"PRId64" rows:%d", TD_VID(pVnode), __func__, resultRsp.ver, resultRsp.totalRows);
20,866 ✔
5091
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
20,867 ✔
5092
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
810 ✔
5093
  buf = rpcMallocCont(size);
811 ✔
5094
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
811 ✔
5095
  printDataBlock(sStreamReaderInfo->metaBlock, __func__, "meta", ((SStreamTask*)pTask)->streamId);
811 ✔
5096
  printDataBlock(resultRsp.deleteBlock, __func__, "delete", ((SStreamTask*)pTask)->streamId);
811 ✔
5097
  printDataBlock(resultRsp.tableBlock, __func__, "table", ((SStreamTask*)pTask)->streamId);
811 ✔
5098

5099
end:
20,779 ✔
5100
  if (code == 0 && resultRsp.totalRows == 0) {
20,872 ✔
5101
    code = TSDB_CODE_STREAM_NO_DATA;
20,060 ✔
5102
    size = sizeof(int64_t) * 2;
20,060 ✔
5103
    buf = rpcMallocCont(size);
20,060 ✔
5104
    *(int64_t*)buf = resultRsp.ver;
20,059 ✔
5105
    *(((int64_t*)buf) + 1) = resultRsp.verTime;
20,059 ✔
5106
  }
5107
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
20,871 ✔
5108
  if (code == TSDB_CODE_STREAM_NO_DATA){
20,873 ✔
5109
    code = 0;
19,971 ✔
5110
  }
5111
  STREAM_PRINT_LOG_END_WITHID(code, lino);
20,873 ✔
5112
  blockDataDestroy(resultRsp.deleteBlock);
20,877 ✔
5113
  blockDataDestroy(resultRsp.tableBlock);
20,873 ✔
5114

5115
  return code;
20,874 ✔
5116
}
5117
static int32_t vnodeProcessStreamWalMetaDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
28,566 ✔
5118
                                                   SStreamTriggerReaderInfo*   sStreamReaderInfo,
5119
                                                   SStreamReaderResponseStats* pRspStats) {
5120
  int32_t      code = 0;
28,566 ✔
5121
  int32_t      lino = 0;
28,566 ✔
5122
  void*        buf = NULL;
28,566 ✔
5123
  size_t       size = 0;
28,566 ✔
5124
  SSTriggerWalNewRsp resultRsp = {0};
28,566 ✔
5125
  
5126
  void* pTask = sStreamReaderInfo->pTask;
28,566 ✔
5127
  ST_TASK_DLOG("vgId:%d %s start, request paras lastVer:%" PRId64 ", endVer:%" PRId64, TD_VID(pVnode), __func__,
28,566 ✔
5128
               req->walMetaDataNewReq.lastVer, req->walMetaDataNewReq.endVer);
5129

5130
  if (req->walMetaDataNewReq.endVer > 0 && req->walMetaDataNewReq.endVer <= req->walMetaDataNewReq.lastVer) {
28,566 ✔
5131
    code = TSDB_CODE_INVALID_PARA;
×
5132
    goto end;
×
5133
  }
5134

5135
  if (sStreamReaderInfo->metaBlock == NULL) {
28,566 ✔
5136
    STREAM_CHECK_RET_GOTO(
869 ✔
5137
        createBlockForWalMetaNew((SSDataBlock**)&sStreamReaderInfo->metaBlock,
5138
                                 isRollupMultiReader(sStreamReaderInfo)));
8 ✔
5139
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(sStreamReaderInfo->metaBlock, STREAM_RETURN_ROWS_NUM));
869 ✔
5140
  }
5141

5142
  resultRsp.metaBlock = sStreamReaderInfo->metaBlock;
28,566 ✔
5143
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerBlock, false, (SSDataBlock**)&resultRsp.dataBlock));
28,566 ✔
5144
  resultRsp.ver = req->walMetaDataNewReq.lastVer;
28,568 ✔
5145
  resultRsp.checkAlter = true;
28,568 ✔
5146
  resultRsp.indexHash = tSimpleHashInit(8, taosGetDefaultHashFunction(getWalDataSliceHashType(sStreamReaderInfo)));
28,693 ✔
5147
  STREAM_CHECK_NULL_GOTO(resultRsp.indexHash, terrno);
28,573 ✔
5148
  {
5149
    int32_t hookRc = streamMaybeRecheckVTableCache(pVnode, sStreamReaderInfo, resultRsp.ver, &resultRsp);
28,573 ✔
5150
    ST_TASK_DLOG("vgId:%d %s hook rc=0x%x ver=%" PRId64, TD_VID(pVnode), __func__, hookRc, resultRsp.ver);
28,581 ✔
5151
    // H2 v0.5: propagate any hook error so trigger fail-fasts.
5152
    if (hookRc != 0) { code = hookRc; goto end; }
28,586 ✔
5153
  }
5154

5155
  STREAM_CHECK_RET_GOTO(processWalVerMetaDataNew(pVnode, sStreamReaderInfo, &resultRsp, req->walMetaDataNewReq.endVer));
28,586 ✔
5156

5157
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
28,562 ✔
5158
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
1,019 ✔
5159
  buf = rpcMallocCont(size);
1,019 ✔
5160
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
1,019 ✔
5161
  printDataBlock(sStreamReaderInfo->metaBlock, __func__, "meta", ((SStreamTask*)pTask)->streamId);
1,018 ✔
5162
  printDataBlock(resultRsp.dataBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
1,017 ✔
5163
  printDataBlock(resultRsp.deleteBlock, __func__, "delete", ((SStreamTask*)pTask)->streamId);
1,018 ✔
5164
  printDataBlock(resultRsp.tableBlock, __func__, "table", ((SStreamTask*)pTask)->streamId);
1,017 ✔
5165
  printIndexHash(resultRsp.indexHash, pTask);
1,017 ✔
5166
  stReaderResponseStatsSetWalData(pRspStats, &resultRsp);
1,016 ✔
5167
  pRspStats->lastReturnedWalVer = resultRsp.ver;
1,017 ✔
5168
  pRspStats->lastReturnedWalVerValid = true;
1,017 ✔
5169

5170
end:
28,449 ✔
5171
  if (code == 0 && resultRsp.totalRows == 0) {
28,561 ✔
5172
    code = TSDB_CODE_STREAM_NO_DATA;
27,543 ✔
5173
    size = sizeof(int64_t) * 2;
27,543 ✔
5174
    buf = rpcMallocCont(size);
27,543 ✔
5175
    *(int64_t*)buf = resultRsp.ver;
27,542 ✔
5176
    *(((int64_t*)buf) + 1) = resultRsp.verTime;
27,542 ✔
5177
  }
5178
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
28,560 ✔
5179
  if (code == TSDB_CODE_STREAM_NO_DATA){
28,568 ✔
5180
    code = 0;
27,551 ✔
5181
  }
5182
  blockDataDestroy(resultRsp.dataBlock);
28,568 ✔
5183
  blockDataDestroy(resultRsp.deleteBlock);
28,557 ✔
5184
  blockDataDestroy(resultRsp.tableBlock);
28,562 ✔
5185
  tSimpleHashCleanup(resultRsp.indexHash);
28,573 ✔
5186

5187
  STREAM_PRINT_LOG_END_WITHID(code, lino);
28,540 ✔
5188

5189
  return code;
28,547 ✔
5190
}
5191

5192
static int32_t vnodeProcessStreamWalDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
7,637 ✔
5193
                                               SStreamTriggerReaderInfo*   sStreamReaderInfo,
5194
                                               SStreamReaderResponseStats* pRspStats) {
5195
  int32_t      code = 0;
7,637 ✔
5196
  int32_t      lino = 0;
7,637 ✔
5197
  void*        buf = NULL;
7,637 ✔
5198
  size_t       size = 0;
7,637 ✔
5199
  SSTriggerWalNewRsp resultRsp = {0};
7,637 ✔
5200

5201
  void* pTask = sStreamReaderInfo->pTask;
7,637 ✔
5202
  ST_TASK_DLOG("vgId:%d %s start, request paras size:%zu", TD_VID(pVnode), __func__, taosArrayGetSize(req->walDataNewReq.versions));
7,637 ✔
5203

5204
  STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->triggerBlock, false, (SSDataBlock**)&resultRsp.dataBlock));
7,637 ✔
5205
  resultRsp.indexHash = tSimpleHashInit(8, taosGetDefaultHashFunction(getWalDataSliceHashType(sStreamReaderInfo)));
7,648 ✔
5206
  STREAM_CHECK_NULL_GOTO(resultRsp.indexHash, terrno);
7,637 ✔
5207
  resultRsp.uidHash = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
7,637 ✔
5208
  STREAM_CHECK_NULL_GOTO(resultRsp.uidHash, terrno);
7,637 ✔
5209

5210
  STREAM_CHECK_RET_GOTO(processWalVerDataNew(pVnode, sStreamReaderInfo, req->walDataNewReq.versions, req->walDataNewReq.ranges, &resultRsp));
7,637 ✔
5211
  ST_TASK_DLOG("vgId:%d %s get result last ver:%"PRId64" rows:%d", TD_VID(pVnode), __func__, resultRsp.ver, resultRsp.totalRows);
7,632 ✔
5212

5213
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
7,632 ✔
5214

5215
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
263 ✔
5216
  buf = rpcMallocCont(size);
263 ✔
5217
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
263 ✔
5218
  printDataBlock(resultRsp.dataBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
263 ✔
5219
  printIndexHash(resultRsp.indexHash, pTask);
263 ✔
5220
  stReaderResponseStatsSetWalData(pRspStats, &resultRsp);
263 ✔
5221
  pRspStats->lastReturnedWalVer = resultRsp.ver;
263 ✔
5222
  pRspStats->lastReturnedWalVerValid = true;
263 ✔
5223

5224
end:
7,631 ✔
5225
  if (code == 0 && resultRsp.totalRows == 0) {
7,638 ✔
5226
    buf = rpcMallocCont(sizeof(int64_t));
7,369 ✔
5227
    *(int64_t *)buf = resultRsp.ver;
7,367 ✔
5228
    size = sizeof(int64_t);
7,367 ✔
5229
    code = TSDB_CODE_STREAM_NO_DATA;
7,367 ✔
5230
  }
5231
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
7,636 ✔
5232
  if (code == TSDB_CODE_STREAM_NO_DATA){
7,636 ✔
5233
    code = 0;
7,367 ✔
5234
  }
5235

5236
  blockDataDestroy(resultRsp.dataBlock);
7,636 ✔
5237
  blockDataDestroy(resultRsp.deleteBlock);
7,634 ✔
5238
  blockDataDestroy(resultRsp.tableBlock);
7,637 ✔
5239
  tSimpleHashCleanup(resultRsp.indexHash);
7,637 ✔
5240
  tSimpleHashCleanup(resultRsp.uidHash);
7,636 ✔
5241
  STREAM_PRINT_LOG_END_WITHID(code, lino);
7,633 ✔
5242

5243
  return code;
7,634 ✔
5244
}
5245

5246
static int32_t vnodeProcessStreamWalCalcDataNewReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
2,536 ✔
5247
                                                   SStreamTriggerReaderInfo*   sStreamReaderInfo,
5248
                                                   SStreamReaderResponseStats* pRspStats) {
5249
  int32_t      code = 0;
2,536 ✔
5250
  int32_t      lino = 0;
2,536 ✔
5251
  void*        buf = NULL;
2,536 ✔
5252
  size_t       size = 0;
2,536 ✔
5253
  SSTriggerWalNewRsp resultRsp = {0};
2,536 ✔
5254
  SSDataBlock* pBlock1 = NULL;
2,536 ✔
5255
  SSDataBlock* pBlock2 = NULL;
2,536 ✔
5256
  
5257
  void* pTask = sStreamReaderInfo->pTask;
2,536 ✔
5258
  ST_TASK_DLOG("vgId:%d %s start, request paras size:%zu", TD_VID(pVnode), __func__, taosArrayGetSize(req->walDataNewReq.versions));
2,536 ✔
5259

5260
  SSDataBlock* dataBlock = sStreamReaderInfo->isVtableStream ? sStreamReaderInfo->calcBlock : sStreamReaderInfo->triggerBlock;
2,536 ✔
5261
  STREAM_CHECK_RET_GOTO(createOneDataBlock(dataBlock, false, (SSDataBlock**)&resultRsp.dataBlock));
2,536 ✔
5262
  resultRsp.isCalc = sStreamReaderInfo->isVtableStream ? true : false;
2,536 ✔
5263
  resultRsp.indexHash = tSimpleHashInit(8, taosGetDefaultHashFunction(getWalDataSliceHashType(sStreamReaderInfo)));
2,540 ✔
5264
  STREAM_CHECK_NULL_GOTO(resultRsp.indexHash, terrno);
2,535 ✔
5265
  resultRsp.uidHash = tSimpleHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
2,535 ✔
5266
  STREAM_CHECK_NULL_GOTO(resultRsp.uidHash, terrno);
2,534 ✔
5267

5268
  STREAM_CHECK_RET_GOTO(processWalVerDataNew(pVnode, sStreamReaderInfo, req->walDataNewReq.versions, req->walDataNewReq.ranges, &resultRsp));
2,534 ✔
5269
  STREAM_CHECK_CONDITION_GOTO(resultRsp.totalRows == 0, TDB_CODE_SUCCESS);
2,535 ✔
5270

5271
  if (!sStreamReaderInfo->isVtableStream){
1,814 ✔
5272
    STREAM_CHECK_RET_GOTO(createOneDataBlock(sStreamReaderInfo->calcBlock, false, &pBlock2));
1,571 ✔
5273
  
5274
    blockDataTransform(pBlock2, resultRsp.dataBlock);
1,570 ✔
5275
    blockDataDestroy(resultRsp.dataBlock);
1,570 ✔
5276
    resultRsp.dataBlock = pBlock2;
1,570 ✔
5277
    pBlock2 = NULL;
1,570 ✔
5278
  }
5279

5280
  size = tSerializeSStreamWalDataResponse(NULL, 0, &resultRsp);
1,813 ✔
5281
  buf = rpcMallocCont(size);
1,813 ✔
5282
  size = tSerializeSStreamWalDataResponse(buf, size, &resultRsp);
1,814 ✔
5283
  printDataBlock(resultRsp.dataBlock, __func__, "data", ((SStreamTask*)pTask)->streamId);
1,814 ✔
5284
  printIndexHash(resultRsp.indexHash, pTask);
1,814 ✔
5285
  stReaderResponseStatsSetWalData(pRspStats, &resultRsp);
1,814 ✔
5286
  pRspStats->lastReturnedWalVer = resultRsp.ver;
1,814 ✔
5287
  pRspStats->lastReturnedWalVerValid = true;
1,814 ✔
5288

5289
end:
2,534 ✔
5290
  if (code == 0 && resultRsp.totalRows == 0) {
2,535 ✔
5291
    buf = rpcMallocCont(sizeof(int64_t));
721 ✔
5292
    *(int64_t *)buf = resultRsp.ver;
721 ✔
5293
    size = sizeof(int64_t);
721 ✔
5294
    code = TSDB_CODE_STREAM_NO_DATA;
721 ✔
5295
  }
5296
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
2,535 ✔
5297
  if (code == TSDB_CODE_STREAM_NO_DATA){
2,535 ✔
5298
    code = 0;
721 ✔
5299
  }
5300

5301
  blockDataDestroy(pBlock1);
2,535 ✔
5302
  blockDataDestroy(pBlock2);
2,534 ✔
5303
  blockDataDestroy(resultRsp.dataBlock);
2,535 ✔
5304
  blockDataDestroy(resultRsp.deleteBlock);
2,536 ✔
5305
  blockDataDestroy(resultRsp.tableBlock);
2,536 ✔
5306
  tSimpleHashCleanup(resultRsp.indexHash);
2,536 ✔
5307
  tSimpleHashCleanup(resultRsp.uidHash);
2,536 ✔
5308
  STREAM_PRINT_LOG_END_WITHID(code, lino);
2,535 ✔
5309

5310
  return code;
2,535 ✔
5311
}
5312

5313
static int32_t vnodeProcessStreamGroupColValueReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
1,642 ✔
5314
                                                  SStreamTriggerReaderInfo*   sStreamReaderInfo,
5315
                                                  SStreamReaderResponseStats* pRspStats) {
5316
  int32_t code = 0;
1,642 ✔
5317
  int32_t lino = 0;
1,642 ✔
5318
  void*   buf = NULL;
1,642 ✔
5319
  size_t  size = 0;
1,642 ✔
5320
  SArray** gInfo = NULL;
1,642 ✔
5321
  
5322
  void* pTask = sStreamReaderInfo->pTask;
1,642 ✔
5323
  ST_TASK_DLOG("vgId:%d %s start, request gid:%" PRId64, TD_VID(pVnode), __func__, req->groupColValueReq.gid);
1,642 ✔
5324

5325
  gInfo = taosHashAcquire(sStreamReaderInfo->groupIdMap, &req->groupColValueReq.gid, POINTER_BYTES);
1,642 ✔
5326
  STREAM_CHECK_NULL_GOTO(gInfo, TSDB_CODE_STREAM_NO_CONTEXT);
1,644 ✔
5327
  SStreamGroupInfo pGroupInfo = {0};
1,631 ✔
5328
  pGroupInfo.gInfo = *gInfo;
1,631 ✔
5329

5330
  size = tSerializeSStreamGroupInfo(NULL, 0, &pGroupInfo, TD_VID(pVnode));
1,631 ✔
5331
  STREAM_CHECK_CONDITION_GOTO(size < 0, size);
×
5332
  buf = rpcMallocCont(size);
1,631 ✔
5333
  STREAM_CHECK_NULL_GOTO(buf, terrno);
1,630 ✔
5334
  size = tSerializeSStreamGroupInfo(buf, size, &pGroupInfo, TD_VID(pVnode));
1,630 ✔
5335
  STREAM_CHECK_CONDITION_GOTO(size < 0, size);
1 ✔
5336
end:
1,643 ✔
5337
  taosHashRelease(sStreamReaderInfo->groupIdMap, gInfo);
1,644 ✔
5338
  if (code != 0) {
1,644 ✔
5339
    rpcFreeCont(buf);
13 ✔
5340
    buf = NULL;
13 ✔
5341
    size = 0;
13 ✔
5342
  }
5343
  STREAM_PRINT_LOG_END_WITHID(code, lino);
1,644 ✔
5344
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
1,644 ✔
5345

5346
  return code;
1,644 ✔
5347
}
5348

5349
static int32_t streamCopyColRef(SColRef* pDst, const SColRef* pSrc) {
×
5350
  // Reject half-initialized sources so a colRef with a dangling length but no payload can never
5351
  // escape the copy.
5352
  if ((pSrc->tagCondLen > 0 && pSrc->tagCondJson == NULL) ||
×
5353
      (pSrc->filterCondLen > 0 && pSrc->filterCondJson == NULL)) {
×
5354
    return TSDB_CODE_INVALID_MSG;
×
5355
  }
5356
  *pDst = *pSrc;
×
5357
  pDst->tagCondJson = NULL;
×
5358
  pDst->filterCondJson = NULL;
×
5359
  if (pSrc->tagCondLen > 0 && pSrc->tagCondJson != NULL) {
×
5360
    // Same NUL-free length contract as the direct-reference path: drop a trailing NUL that the
5361
    // resolver may have included in the length.
5362
    int32_t tagCondLen = (int32_t)strnlen(pSrc->tagCondJson, (size_t)pSrc->tagCondLen);
×
5363
    if (tagCondLen <= 0) return TSDB_CODE_INVALID_MSG;
×
5364
    pDst->tagCondJson = taosMemoryMalloc((size_t)tagCondLen + 1);
×
5365
    if (pDst->tagCondJson == NULL) {
×
5366
      return terrno;
×
5367
    }
5368
    memcpy(pDst->tagCondJson, pSrc->tagCondJson, tagCondLen);
×
5369
    pDst->tagCondJson[tagCondLen] = '\0';
×
5370
    pDst->tagCondLen = tagCondLen;
×
5371
  }
5372
  if (pSrc->filterCondLen > 0 && pSrc->filterCondJson != NULL) {
×
5373
    int32_t filterCondLen = (int32_t)strnlen(pSrc->filterCondJson, (size_t)pSrc->filterCondLen);
×
5374
    if (filterCondLen <= 0) {
×
5375
      // Roll back the already-copied condition so a partially initialized col ref never
5376
      // escapes to a caller that has no failure cleanup for its reserved slot.
5377
      taosMemoryFreeClear(pDst->tagCondJson);
×
5378
      pDst->tagCondLen = 0;
×
5379
      return TSDB_CODE_INVALID_MSG;
×
5380
    }
5381
    pDst->filterCondJson = taosMemoryMalloc((size_t)filterCondLen + 1);
×
5382
    if (pDst->filterCondJson == NULL) {
×
5383
      // Roll back the already-copied condition so a partially initialized col ref never
5384
      // escapes to a caller that has no failure cleanup for its reserved slot.
5385
      taosMemoryFreeClear(pDst->tagCondJson);
×
5386
      pDst->tagCondLen = 0;
×
5387
      return terrno;
×
5388
    }
5389
    memcpy(pDst->filterCondJson, pSrc->filterCondJson, filterCondLen);
×
5390
    pDst->filterCondJson[filterCondLen] = '\0';
×
5391
    pDst->filterCondLen = filterCondLen;
×
5392
  }
5393
  return TSDB_CODE_SUCCESS;
×
5394
}
5395

5396
static int32_t setVtableInfo(SVnode* pVnode, SArray* infos, SArray* cids, int64_t uid, uint64_t gid, int64_t ver, SMetaReader* metaReader, SStreamTriggerReaderInfo* sStreamReaderInfo) {
×
5397
  int32_t              code = 0;
×
5398
  int32_t              lino = 0;
×
5399
  void* pTask = sStreamReaderInfo->pTask;
×
5400

5401
  VTableInfo* vTable = taosArrayReserve(infos, 1);
×
5402
  STREAM_CHECK_NULL_GOTO(vTable, terrno);
×
5403
  memset(vTable, 0, sizeof(*vTable));
×
5404
  vTable->uid = uid;
×
5405
  vTable->gId = gid;
×
5406

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

5409
  code = sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(metaReader, ver, uid);
×
5410
  if (code != 0) {
×
5411
    ST_TASK_ELOG("vgId:%d %s get table entry by uid:%"PRId64" failed, msg:%s", TD_VID(pVnode), __func__, uid, tstrerror(code));
×
5412
    goto end;
×
5413
  }
5414
  tstrncpy(vTable->tbName, metaReader->me.name, sizeof(vTable->tbName));
×
5415
  if (vTable->tbName[0] == '\0') {
×
5416
    code = metaGetTableSzNameByUid(pVnode->pMeta, uid, vTable->tbName);
×
5417
    if (code != 0) {
×
5418
      ST_TASK_ELOG("vgId:%d %s get table name by uid:%" PRId64 " failed, msg:%s", TD_VID(pVnode), __func__, uid,
×
5419
                   tstrerror(code));
5420
      goto end;
×
5421
    }
5422
  }
5423
  ST_TASK_DLOG("vgId:%d %s put vtable uid:%" PRId64 ", gid:%" PRIu64 ", tbname:%s", TD_VID(pVnode), __func__, uid,
×
5424
               gid, vTable->tbName);
5425
  if (atomic_load_8(&sStreamReaderInfo->isVtableOnlyTs) == 1) {
×
5426
    vTable->cols.nCols = metaReader->me.colRef.nCols;
×
5427
    vTable->cols.version = metaReader->me.colRef.version;
×
5428
    vTable->cols.pColRef = taosMemoryCalloc(metaReader->me.colRef.nCols, sizeof(SColRef));
×
5429
    STREAM_CHECK_NULL_GOTO(vTable->cols.pColRef, terrno);
×
5430
    for (size_t j = 0; j < metaReader->me.colRef.nCols; j++) {
×
5431
      code = streamCopyColRef(&vTable->cols.pColRef[j], &metaReader->me.colRef.pColRef[j]);
×
5432
      if (code != 0) {
×
5433
        goto end;
×
5434
      }
5435
    }
5436
  } else {
5437
    vTable->cols.nCols = taosArrayGetSize(cids);
×
5438
    vTable->cols.version = metaReader->me.colRef.version;
×
5439
    vTable->cols.pColRef = taosMemoryCalloc(taosArrayGetSize(cids), sizeof(SColRef));
×
5440
    STREAM_CHECK_NULL_GOTO(vTable->cols.pColRef, terrno);
×
5441
    for (size_t i = 0; i < taosArrayGetSize(cids); i++) {
×
5442
      for (size_t j = 0; j < metaReader->me.colRef.nCols; j++) {
×
5443
        if (metaReader->me.colRef.pColRef[j].hasRef &&
×
5444
            metaReader->me.colRef.pColRef[j].id == *(col_id_t*)taosArrayGet(cids, i)) {
×
5445
          code = streamCopyColRef(&vTable->cols.pColRef[i], &metaReader->me.colRef.pColRef[j]);
×
5446
          if (code != 0) {
×
5447
            goto end;
×
5448
          }
5449
          break;
×
5450
        }
5451
      }
5452
    }
5453
  }
5454
  tDecoderClear(&metaReader->coder);
×
5455

5456
end:
×
5457
  return code;
×
5458
}
5459

5460
// Collect all rollup gids for a given uid from uIdMap (under read-lock) into rollupGids.
5461
// Returns TSDB_CODE_SUCCESS on success; caller owns rollupGids and must destroy it.
5462
static int32_t collectRollupGids(SVnode* pVnode, SStreamTriggerReaderInfo* sStreamReaderInfo,
4 ✔
5463
                                 int64_t uid, const char* caller, SArray** ppRollupGids) {
5464
  void* pTask = sStreamReaderInfo->pTask;
4 ✔
5465
  SArray* rollupGids = taosArrayInit(4, sizeof(uint64_t));
4 ✔
5466
  if (rollupGids == NULL) return terrno;
4 ✔
5467

5468
  taosRLockLatch(&sStreamReaderInfo->lock);
4 ✔
5469
  SArray** pElements = taosHashGet(sStreamReaderInfo->tableList.uIdMap, &uid, LONG_BYTES);
4 ✔
5470
  if (pElements != NULL) {
4 ✔
5471
    int32_t numOfElements = taosArrayGetSize(*pElements);
4 ✔
5472
    ST_TASK_DLOG("vgId:%d %s uid:%" PRId64 " rollup gid count:%d", TD_VID(pVnode), caller, uid, numOfElements);
4 ✔
5473
    for (int32_t j = 0; j < numOfElements; ++j) {
16 ✔
5474
      SStreamTableMapElement* element = taosArrayGet(*pElements, j);
12 ✔
5475
      if (element == NULL || element->table == NULL || element->table->markedDeleted) continue;
12 ✔
5476
      if (taosArrayPush(rollupGids, &element->table->groupId) == NULL) {
24 ✔
5477
        taosRUnLockLatch(&sStreamReaderInfo->lock);
×
5478
        taosArrayDestroy(rollupGids);
×
5479
        return terrno;
×
5480
      }
5481
    }
5482
  } else {
5483
    ST_TASK_WLOG("vgId:%d %s uid:%" PRId64 " not found in uIdMap", TD_VID(pVnode), caller, uid);
×
5484
  }
5485
  taosRUnLockLatch(&sStreamReaderInfo->lock);
4 ✔
5486

5487
  *ppRollupGids = rollupGids;
4 ✔
5488
  return TSDB_CODE_SUCCESS;
4 ✔
5489
}
5490

5491
// Fill VTableInfo entries for all uids in uid2Result (fullScan path).
5492
// For rollup vstb streams every uid maps to multiple rollup gids; for regular vtable
5493
// streams each uid maps to a single groupId looked up via qStreamGetGroupIdFromOrigin.
5494
static int32_t getAllVinfo(SVnode* pVnode, SStreamMsgVTableInfo* vTableInfo, SSHashObj* uid2Result,
284 ✔
5495
                           SArray* cids, int64_t ver, SMetaReader* metaReader,
5496
                           SStreamTriggerReaderInfo* sStreamReaderInfo) {
5497
  int32_t code = 0;
284 ✔
5498
  void*   pTask = sStreamReaderInfo->pTask;
284 ✔
5499

5500
  void*   iter = NULL;
284 ✔
5501
  int32_t it   = 0;
284 ✔
5502
  while ((iter = tSimpleHashIterate(uid2Result, iter, &it)) != NULL) {
785 ✔
5503
    int64_t              uid = *(int64_t*)tSimpleHashGetKey(iter, NULL);
501 ✔
5504
    SVTableResolveResult* r  = *(SVTableResolveResult**)iter;
501 ✔
5505

5506
    if (sStreamReaderInfo->isRollupReader) {
501 ✔
5507
      // vstb rollup: one uid  ->  multiple rollup gids
5508
      SArray* rollupGids = NULL;
4 ✔
5509
      code = collectRollupGids(pVnode, sStreamReaderInfo, uid, __func__, &rollupGids);
4 ✔
5510
      if (code != 0) return code;
4 ✔
5511

5512
      for (int32_t j = 0; j < taosArrayGetSize(rollupGids); ++j) {
16 ✔
5513
        uint64_t rollupGid = *(uint64_t*)taosArrayGet(rollupGids, j);
12 ✔
5514
        int32_t  rc = streamFillVTableInfoFromResolved(pVnode, sStreamReaderInfo, uid, rollupGid,
12 ✔
5515
                                                       ver, cids, r, metaReader, vTableInfo->infos);
5516
        if (rc != 0) { taosArrayDestroy(rollupGids); return rc; }
12 ✔
5517
      }
5518
      taosArrayDestroy(rollupGids);
4 ✔
5519
    } else {
5520
      // regular vtable stream: one uid  ->  one groupId
5521
      taosRLockLatch(&sStreamReaderInfo->lock);
497 ✔
5522
      uint64_t groupId = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, uid);
497 ✔
5523
      taosRUnLockLatch(&sStreamReaderInfo->lock);
497 ✔
5524
      if (groupId == (uint64_t)-1) continue;
497 ✔
5525

5526
      int32_t rc = streamFillVTableInfoFromResolved(pVnode, sStreamReaderInfo, uid, groupId,
497 ✔
5527
                                                    ver, cids, r, metaReader, vTableInfo->infos);
5528
      if (rc != 0) return rc;
497 ✔
5529
    }
5530
  }
5531
  return TSDB_CODE_SUCCESS;
284 ✔
5532
}
5533

5534
// Fill VTableInfo entries for the specific uids listed in req->virTableInfoReq.uids
5535
// (non-fullScan path).  uid2Result provides the resolved vtable metadata for each uid.
5536
static int32_t getSpicificVinfo(SVnode* pVnode, SStreamMsgVTableInfo* vTableInfo, SSHashObj* uid2Result,
27 ✔
5537
                                SArray* uids, SArray* cids, int64_t ver, SMetaReader* metaReader,
5538
                                SStreamTriggerReaderInfo* sStreamReaderInfo) {
5539
  int32_t code  = 0;
27 ✔
5540
  void*   pTask = sStreamReaderInfo->pTask;
27 ✔
5541
  int32_t nReq  = (int32_t)taosArrayGetSize(uids);
27 ✔
5542

5543
  for (int32_t i = 0; i < nReq; ++i) {
55 ✔
5544
    int64_t              uid = *(int64_t*)taosArrayGet(uids, i);
28 ✔
5545
    SVTableResolveResult** pp = (SVTableResolveResult**)tSimpleHashGet(uid2Result, &uid, sizeof(uid));
28 ✔
5546
    if (pp == NULL || *pp == NULL) continue;
28 ✔
5547

5548
    if (sStreamReaderInfo->isRollupReader) {
28 ✔
5549
      // vstb rollup: one uid  ->  multiple rollup gids
5550
      SArray* rollupGids = NULL;
×
5551
      code = collectRollupGids(pVnode, sStreamReaderInfo, uid, __func__, &rollupGids);
×
5552
      if (code != 0) return code;
×
5553

5554
      for (int32_t j = 0; j < taosArrayGetSize(rollupGids); ++j) {
×
5555
        uint64_t rollupGid = *(uint64_t*)taosArrayGet(rollupGids, j);
×
5556
        int32_t  rc = streamFillVTableInfoFromResolved(pVnode, sStreamReaderInfo, uid, rollupGid,
×
5557
                                                       ver, cids, *pp, metaReader, vTableInfo->infos);
5558
        if (rc != 0) { taosArrayDestroy(rollupGids); return rc; }
×
5559
      }
5560
      taosArrayDestroy(rollupGids);
×
5561
    } else {
5562
      // regular vtable stream: one uid  ->  one groupId
5563
      taosRLockLatch(&sStreamReaderInfo->lock);
28 ✔
5564
      uint64_t groupId = qStreamGetGroupIdFromOrigin(sStreamReaderInfo, uid);
28 ✔
5565
      taosRUnLockLatch(&sStreamReaderInfo->lock);
28 ✔
5566
      if (groupId == (uint64_t)-1) continue;
28 ✔
5567

5568
      int32_t rc = streamFillVTableInfoFromResolved(pVnode, sStreamReaderInfo, uid, groupId,
28 ✔
5569
                                                    ver, cids, *pp, metaReader, vTableInfo->infos);
5570
      if (rc != 0) return rc;
28 ✔
5571
    }
5572
  }
5573
  return TSDB_CODE_SUCCESS;
27 ✔
5574
}
5575

5576
static int32_t vnodeProcessStreamVTableInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
310 ✔
5577
                                               SStreamTriggerReaderInfo*   sStreamReaderInfo,
5578
                                               SStreamReaderResponseStats* pRspStats) {
5579
  int32_t              code = 0;
310 ✔
5580
  int32_t              lino = 0;
310 ✔
5581
  void*                buf = NULL;
310 ✔
5582
  size_t               size = 0;
311 ✔
5583
  SStreamMsgVTableInfo vTableInfo = {0};
311 ✔
5584
  SMetaReader          metaReader = {0};
311 ✔
5585
  SArray              *tagCids   = NULL;
311 ✔
5586
  SSHashObj           *uid2Result = NULL;
311 ✔
5587

5588
  void* pTask = sStreamReaderInfo->pTask;
311 ✔
5589
  ST_TASK_DLOG("vgId:%d %s start, version:%"PRId64, TD_VID(pVnode), __func__, req->virTableInfoReq.ver);
311 ✔
5590

5591
  SArray* cids = req->virTableInfoReq.cids;
311 ✔
5592
  STREAM_CHECK_NULL_GOTO(cids, terrno);
311 ✔
5593

5594
  if (taosArrayGetSize(cids) == 1 && *(col_id_t*)taosArrayGet(cids, 0) == PRIMARYKEY_TIMESTAMP_COL_ID){
311 ✔
5595
    (void)atomic_val_compare_exchange_8(&sStreamReaderInfo->isVtableOnlyTs, 0, 1);
47 ✔
5596
  }
5597

5598
  bool fullScan = req->virTableInfoReq.fetchAllTable
622 ✔
5599
                  || req->virTableInfoReq.uids == NULL
67 ✔
5600
                  || taosArrayGetSize(req->virTableInfoReq.uids) == 0;
378 ✔
5601

5602
  // When the trigger only references the TS column, req->cids carries just the
5603
  // primary-key timestamp, but the response must still describe every column
5604
  // ref of each vtable. Pass cids=NULL into the chain-resolver / formatter to
5605
  // request "all columns of the vtable, per-uid".
5606
  if (atomic_load_8(&sStreamReaderInfo->isVtableOnlyTs) == 1) {
311 ✔
5607
    cids = NULL;
47 ✔
5608
  }
5609

5610
  // Chain-resolver path.
5611
  // NOTE: streamResolveVTableRefChain fires cross-vnode RPCs (TDMT_VND_VTABLE_REF_RESOLVE).
5612
  // Holding META_READER_LOCK across those RPCs risks deadlock if the remote vnode
5613
  // needs to acquire the same lock on this vnode during its own meta read. Delay
5614
  // initReader until after the resolve round-trip completes.
5615
  STREAM_CHECK_RET_GOTO(streamCollectTagCidsFromPartitionCols(sStreamReaderInfo->partitionCols, &tagCids));
311 ✔
5616

5617
  SArray *uidList = fullScan ? NULL : req->virTableInfoReq.uids;
311 ✔
5618
  STREAM_CHECK_RET_GOTO(streamResolveVTableRefChain(pVnode, sStreamReaderInfo->vtbCache, sStreamReaderInfo,
311 ✔
5619
                                                    req->virTableInfoReq.ver, uidList, cids, tagCids, &uid2Result, 0));
×
5620

5621
  // Acquire meta reader lock only after the cross-vnode RPC round-trip is done.
5622
  sStreamReaderInfo->storageApi.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &sStreamReaderInfo->storageApi.metaFn, 0);
311 ✔
5623

5624
  // Encode response: iterate the resolved set.
5625
  int32_t expected = (int32_t)tSimpleHashGetSize(uid2Result);
311 ✔
5626
  vTableInfo.infos = taosArrayInit(expected, sizeof(VTableInfo));
311 ✔
5627
  STREAM_CHECK_NULL_GOTO(vTableInfo.infos, terrno);
311 ✔
5628

5629
  if (fullScan) {
311 ✔
5630
    STREAM_CHECK_RET_GOTO(getAllVinfo(pVnode, &vTableInfo, uid2Result, cids, req->virTableInfoReq.ver, &metaReader, sStreamReaderInfo));
284 ✔
5631
  } else {
5632
    STREAM_CHECK_RET_GOTO(getSpicificVinfo(pVnode, &vTableInfo, uid2Result, req->virTableInfoReq.uids, cids, req->virTableInfoReq.ver, &metaReader, sStreamReaderInfo));
27 ✔
5633
  }
5634

5635
  ST_TASK_DLOG("vgId:%d %s end, size:%"PRIzu, TD_VID(pVnode), __func__, taosArrayGetSize(vTableInfo.infos));
311 ✔
5636
  STREAM_CHECK_RET_GOTO(buildVTableInfoRsp(&vTableInfo, &buf, &size));
311 ✔
5637

5638
  // A virtual table that maps no source column at all has nothing to be configured by the set table request.
5639
  // Its reader is ready to serve the following pull requests as soon as the (empty) table list is resolved,
5640
  // otherwise every pull request would be rejected and, since the trigger skips the set table request when
5641
  // no orig table is referenced, the stream would never be able to run.
5642
  if (fullScan && !streamVTableInfoHasColumnRef(&vTableInfo)) {
311 ✔
5643
    atomic_store_8(&sStreamReaderInfo->vTableReady, 1);
48 ✔
5644
    ST_TASK_DLOG("vgId:%d virtual table reader is ready, no source table is referenced", TD_VID(pVnode));
48 ✔
5645
  }
5646

5647
  int32_t rc = streamCacheCommitResolved(sStreamReaderInfo->vtbCache, fullScan, cids, tagCids, &uid2Result);
311 ✔
5648
  if (rc != 0) {
311 ✔
5649
    code = rc;
×
5650
    lino = __LINE__;
×
5651
    goto end;
×
5652
  }
5653

5654
end:
311 ✔
5655
  if (tagCids != NULL) taosArrayDestroy(tagCids);
311 ✔
5656
  tSimpleHashCleanup(uid2Result);
311 ✔
5657
  tDestroySStreamMsgVTableInfo(&vTableInfo);
311 ✔
5658
  sStreamReaderInfo->storageApi.metaReaderFn.clearReader(&metaReader);
311 ✔
5659
  STREAM_PRINT_LOG_END_WITHID(code, lino);
311 ✔
5660
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
311 ✔
5661
  return code;
311 ✔
5662
}
5663

5664
static int32_t vnodeProcessStreamOTableInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
414 ✔
5665
                                               SStreamTriggerReaderInfo*   sStreamReaderInfo,
5666
                                               SStreamReaderResponseStats* pRspStats) {
5667
  int32_t                   code = 0;
414 ✔
5668
  int32_t                   lino = 0;
414 ✔
5669
  void*                     buf = NULL;
414 ✔
5670
  size_t                    size = 0;
414 ✔
5671
  SSTriggerOrigTableInfoRsp oTableInfo = {0};
414 ✔
5672
  SMetaReader               metaReader = {0};
414 ✔
5673
  void*                     pTask = sStreamReaderInfo->pTask;
414 ✔
5674

5675
  ST_TASK_DLOG("vgId:%d %s start, ver:%" PRId64, TD_VID(pVnode), __func__, req->origTableInfoReq.ver);
414 ✔
5676

5677
  SArray* cols = req->origTableInfoReq.cols;
414 ✔
5678
  STREAM_CHECK_NULL_GOTO(cols, terrno);
414 ✔
5679

5680
  oTableInfo.cols = taosArrayInit(taosArrayGetSize(cols), sizeof(OTableInfoRsp));
414 ✔
5681

5682
  STREAM_CHECK_NULL_GOTO(oTableInfo.cols, terrno);
414 ✔
5683

5684
  sStreamReaderInfo->storageApi.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &sStreamReaderInfo->storageApi.metaFn, 0);
414 ✔
5685
  for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
1,197 ✔
5686
    OTableInfo*    oInfo = taosArrayGet(cols, i);
783 ✔
5687
    OTableInfoRsp* vTableInfo = taosArrayReserve(oTableInfo.cols, 1);
783 ✔
5688
    STREAM_CHECK_NULL_GOTO(oInfo, terrno);
783 ✔
5689
    STREAM_CHECK_NULL_GOTO(vTableInfo, terrno);
783 ✔
5690
    code = sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionName(&metaReader, req->origTableInfoReq.ver, oInfo->refTableName);
783 ✔
5691
    if (code != 0) {
783 ✔
5692
      code = 0;
×
5693
      ST_TASK_ELOG("vgId:%d %s get table entry by name:%s failed, msg:%s", TD_VID(pVnode), __func__, oInfo->refTableName, tstrerror(code));
×
5694
      continue;
×
5695
    }
5696
    vTableInfo->uid = metaReader.me.uid;
783 ✔
5697
    ST_TASK_DLOG("vgId:%d %s get original uid:%"PRId64, TD_VID(pVnode), __func__, vTableInfo->uid);
783 ✔
5698

5699
    SSchemaWrapper* sSchemaWrapper = NULL;
783 ✔
5700
    if (metaReader.me.type == TD_CHILD_TABLE) {
783 ✔
5701
      int64_t suid = metaReader.me.ctbEntry.suid;
707 ✔
5702
      vTableInfo->suid = suid;
707 ✔
5703
      tDecoderClear(&metaReader.coder);
707 ✔
5704
      STREAM_CHECK_RET_GOTO(sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(&metaReader, req->origTableInfoReq.ver, suid));
707 ✔
5705
      sSchemaWrapper = &metaReader.me.stbEntry.schemaRow;
707 ✔
5706
    } else if (metaReader.me.type == TD_NORMAL_TABLE) {
76 ✔
5707
      vTableInfo->suid = 0;
76 ✔
5708
      sSchemaWrapper = &metaReader.me.ntbEntry.schemaRow;
76 ✔
5709
    } else {
5710
      ST_TASK_ELOG("invalid table type:%d", metaReader.me.type);
×
5711
    }
5712

5713
    for (size_t j = 0; j < sSchemaWrapper->nCols; j++) {
2,484 ✔
5714
      SSchema* s = sSchemaWrapper->pSchema + j;
2,484 ✔
5715
      if (strcmp(s->name, oInfo->refColName) == 0) {
2,484 ✔
5716
        vTableInfo->cid = s->colId;
783 ✔
5717
        break;
783 ✔
5718
      }
5719
    }
5720
    if (vTableInfo->cid == 0) {
783 ✔
5721
      ST_TASK_ELOG("vgId:%d %s, column:%s not found in table:%s", TD_VID(pVnode), __func__, oInfo->refColName,
×
5722
                   oInfo->refTableName);
5723
    }
5724
    tDecoderClear(&metaReader.coder);
783 ✔
5725
  }
5726

5727
  STREAM_CHECK_RET_GOTO(buildOTableInfoRsp(&oTableInfo, &buf, &size));
414 ✔
5728

5729
end:
414 ✔
5730
  tDestroySTriggerOrigTableInfoRsp(&oTableInfo);
414 ✔
5731
  sStreamReaderInfo->storageApi.metaReaderFn.clearReader(&metaReader);
414 ✔
5732
  STREAM_PRINT_LOG_END_WITHID(code, lino);
414 ✔
5733
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
414 ✔
5734
  return code;
414 ✔
5735
}
5736

5737
static int32_t vnodeProcessStreamVTableTagInfoReq(SVnode* pVnode, SRpcMsg* pMsg, SSTriggerPullRequestUnion* req,
1,701 ✔
5738
                                                  SStreamTriggerReaderInfo*   sStreamReaderInfo,
5739
                                                  SStreamReaderResponseStats* pRspStats) {
5740
  int32_t                   code = 0;
1,701 ✔
5741
  int32_t                   lino = 0;
1,701 ✔
5742
  void*                     buf = NULL;
1,701 ✔
5743
  size_t                    size = 0;
1,701 ✔
5744
  SSDataBlock              *pBlock      = NULL;
1,701 ✔
5745
  SArray                   *singleUid   = NULL;
1,701 ✔
5746
  SArray                   *emptyCols   = NULL;
1,701 ✔
5747
  SArray                   *tagOnly     = NULL;
1,701 ✔
5748
  SSHashObj                *uid2Result  = NULL;
1,701 ✔
5749
  SVTableResolveResult     *pRes        = NULL;
1,701 ✔
5750
  SMetaReader               metaReader  = {0};
1,701 ✔
5751
  bool                      metaReaderInited = false;
1,701 ✔
5752
  int8_t                    vtableType = 0;
1,701 ✔
5753
  char                      vtableName[TSDB_TABLE_NAME_LEN] = {0};
1,701 ✔
5754
  int64_t streamId = req->base.streamId;
1,701 ✔
5755
  stsDebug("vgId:%d %s start, ver:%"PRId64" uid:%"PRId64" cols_size:%d", TD_VID(pVnode), __func__,
1,701 ✔
5756
           req->virTablePseudoColReq.ver, req->virTablePseudoColReq.uid,
5757
           req->virTablePseudoColReq.cids ? (int)taosArrayGetSize(req->virTablePseudoColReq.cids) : -1);
5758

5759
  SArray* cols = req->virTablePseudoColReq.cids;
1,701 ✔
5760
  STREAM_CHECK_NULL_GOTO(cols, terrno);
1,701 ✔
5761

5762
  // We still need metaReader for vtable name (cid == -1) and to assert table type.
5763
  sStreamReaderInfo->storageApi.metaReaderFn.initReader(&metaReader, pVnode, META_READER_LOCK, &sStreamReaderInfo->storageApi.metaFn, 0);
1,701 ✔
5764
  metaReaderInited = true;
1,701 ✔
5765
  STREAM_CHECK_RET_GOTO(sStreamReaderInfo->storageApi.metaReaderFn.getTableEntryByVersionUid(&metaReader, req->virTablePseudoColReq.ver, req->virTablePseudoColReq.uid));
1,701 ✔
5766
  STREAM_CHECK_CONDITION_GOTO(metaReader.me.type != TD_VIRTUAL_CHILD_TABLE && metaReader.me.type != TD_VIRTUAL_NORMAL_TABLE, TSDB_CODE_INVALID_PARA);
1,700 ✔
5767
  vtableType = metaReader.me.type;
1,700 ✔
5768
  tstrncpy(vtableName, metaReader.me.name, sizeof(vtableName));
1,700 ✔
5769
  sStreamReaderInfo->storageApi.metaReaderFn.clearReader(&metaReader);
1,700 ✔
5770
  metaReaderInited = false;
1,701 ✔
5771

5772
  STREAM_CHECK_RET_GOTO(createDataBlock(&pBlock));
1,701 ✔
5773

5774
  if (vtableType == TD_VIRTUAL_NORMAL_TABLE) {
1,701 ✔
5775
    // Normal vtable: caller only requests the table-name pseudo column. The binary column
5776
    // must reserve the string header in addition to the NUL-terminated name buffer size.
5777
    STREAM_CHECK_CONDITION_GOTO (taosArrayGetSize(cols) < 1 || *(col_id_t*)taosArrayGet(cols, 0) != -1, TSDB_CODE_INVALID_PARA);
13 ✔
5778
    SColumnInfoData idata =
13 ✔
5779
        createColumnInfoData(TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE, -1);
13 ✔
5780
    STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
13 ✔
5781
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, 1));
13 ✔
5782
    pBlock->info.rows = 1;
13 ✔
5783
    SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, 0);
13 ✔
5784
    STREAM_CHECK_NULL_GOTO(pDst, terrno);
13 ✔
5785
    STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, vtableName, strlen(vtableName), false));
13 ✔
5786
  } else {
5787
    // Virtual child table: resolve tags with shared routing and private ref results.
5788
    singleUid = taosArrayInit(1, sizeof(int64_t));
1,688 ✔
5789
    STREAM_CHECK_NULL_GOTO(singleUid, terrno);
1,688 ✔
5790
    int64_t uidVal = req->virTablePseudoColReq.uid;
1,688 ✔
5791
    STREAM_CHECK_NULL_GOTO(taosArrayPush(singleUid, &uidVal), terrno);
1,688 ✔
5792

5793
    emptyCols = taosArrayInit(0, sizeof(col_id_t));
1,688 ✔
5794
    STREAM_CHECK_NULL_GOTO(emptyCols, terrno);
1,688 ✔
5795

5796
    // The pseudo-column request can carry cid==-1 for the vtable name; the
5797
    // chain resolver only handles real tag cids, so pass a filtered copy.
5798
    tagOnly = taosArrayInit(taosArrayGetSize(cols), sizeof(col_id_t));
1,688 ✔
5799
    STREAM_CHECK_NULL_GOTO(tagOnly, terrno);
1,688 ✔
5800
    for (size_t i = 0; i < taosArrayGetSize(cols); i++) {
4,473 ✔
5801
      col_id_t cid = *(col_id_t *)taosArrayGet(cols, i);
2,785 ✔
5802
      if (cid == -1) continue;
2,785 ✔
5803
      STREAM_CHECK_NULL_GOTO(taosArrayPush(tagOnly, &cid), terrno);
1,111 ✔
5804
    }
5805

5806
    // Share only the reader's db -> vgroup routing map. The resolver uses a
5807
    // private tblRefCache and returns a caller-owned result hash, leaving
5808
    // the reader's tblRefCache and uid2Result untouched. Passing NULL here used
5809
    // to disable that map as well and made every pseudo-column request re-issue
5810
    // TDMT_MND_GET_DB_INFO to mnode for the same db.
5811
    code = streamResolveVTableRefChain(pVnode, sStreamReaderInfo->vtbCache, sStreamReaderInfo,
1,688 ✔
5812
                                       req->virTablePseudoColReq.ver,
5813
                                       singleUid, emptyCols, tagOnly, &uid2Result, 0);
5814
    taosArrayDestroy(tagOnly); tagOnly = NULL;
1,688 ✔
5815
    STREAM_CHECK_RET_GOTO(code);
1,688 ✔
5816

5817
    SVTableResolveResult **pp = (SVTableResolveResult **)tSimpleHashGet(uid2Result, &uidVal, sizeof(uidVal));
1,688 ✔
5818
    if (pp == NULL || *pp == NULL) {
1,688 ✔
5819
      // H2 v0.5: A returned 0 but the uid has no entry in uid2Result. This
5820
      // happens only when the top-level uid was missing from local meta
5821
      // (vtable dropped concurrently). PSEUDO_COL is single-uid and has no
5822
      // partial-success semantic, so propagate as REF_TABLE_NOT_EXIST.
5823
      code = TSDB_CODE_STREAM_VTB_REF_TABLE_NOT_EXIST;
×
5824
      goto end;
×
5825
    }
5826
    pRes = *pp;
1,688 ✔
5827

5828
    // Append column metadata according to caller-requested cids.
5829
    for (size_t i = 0; i < taosArrayGetSize(cols); i++){
4,473 ✔
5830
      col_id_t cid = *(col_id_t*)taosArrayGet(cols, i);
2,785 ✔
5831
      if (cid == -1) {
2,785 ✔
5832
        SColumnInfoData idata =
1,674 ✔
5833
            createColumnInfoData(TSDB_DATA_TYPE_BINARY, TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE, -1);
1,674 ✔
5834
        STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
1,674 ✔
5835
        continue;
1,674 ✔
5836
      }
5837
      STagValue **ppv = (pRes->tagMap == NULL) ? NULL : (STagValue **)tSimpleHashGet(pRes->tagMap, &cid, sizeof(cid));
1,111 ✔
5838
      if (ppv == NULL || *ppv == NULL) {
1,111 ✔
5839
        SColumnInfoData idata = createColumnInfoData(TSDB_DATA_TYPE_NULL, CHAR_BYTES, cid);
×
5840
        STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
×
5841
        continue;
×
5842
      }
5843
      STagValue *v = *ppv;
1,111 ✔
5844
      // tDataTypes is indexed by type, so a type carried by a resolve response must be
5845
      // range-checked before it is used as a width source. Report it with its own context:
5846
      // the shared tail log only knows the line, not which cid broke the block.
5847
      if (IS_INVALID_TYPE(v->type)) {
1,111 ✔
5848
        stsError("vgId:%d pseudo col cid:%d has invalid type:%d nLen:%d", TD_VID(pVnode), (int32_t)cid,
×
5849
                 (int32_t)v->type, v->nLen);
5850
        code = TSDB_CODE_INVALID_MSG;
×
5851
        lino = __LINE__;
×
5852
        goto end;
×
5853
      }
5854
      // A variable-length column must be able to hold the string header plus the payload:
5855
      // deriving the width from the raw value length alone produced columns narrower than
5856
      // VARSTR_HEADER_SIZE (a 1-byte ref TAG became bytes=1), which the block decoder on the
5857
      // reader side rejects as TSDB_CODE_INVALID_MSG. Reserving exactly header + payload is
5858
      // enough because the decoder never compares the width of a variable-length column
5859
      // against its schema, it only requires bytes >= header.
5860
      int32_t          header = IS_STR_DATA_BLOB(v->type) ? BLOBSTR_HEADER_SIZE : VARSTR_HEADER_SIZE;
1,111 ✔
5861
      int32_t          bytes  = IS_VAR_DATA_TYPE(v->type) ? (v->nLen > 0 ? v->nLen : 0) + header
221 ✔
5862
                                                          : (v->nLen > 0 ? v->nLen : tDataTypes[v->type].bytes);
1,332 ✔
5863
      SColumnInfoData  idata  = createColumnInfoData(v->type, bytes, cid);
1,111 ✔
5864
      stsDebug("vgId:%d pseudo col cid:%d type:%d nLen:%d bytes:%d", TD_VID(pVnode), (int32_t)cid, (int32_t)v->type,
1,111 ✔
5865
               v->nLen, bytes);
5866
      STREAM_CHECK_RET_GOTO(blockDataAppendColInfo(pBlock, &idata));
1,111 ✔
5867
    }
5868

5869
    STREAM_CHECK_RET_GOTO(blockDataEnsureCapacity(pBlock, 1));
1,688 ✔
5870
    pBlock->info.rows = 1;
1,688 ✔
5871

5872
    for (size_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++){
4,473 ✔
5873
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
2,785 ✔
5874
      STREAM_CHECK_NULL_GOTO(pDst, terrno);
2,785 ✔
5875

5876
      if (pDst->info.colId == -1) {
2,785 ✔
5877
        STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, vtableName, strlen(vtableName), false));
1,674 ✔
5878
        continue;
1,674 ✔
5879
      }
5880
      if (pDst->info.type == TSDB_DATA_TYPE_NULL) {
1,111 ✔
5881
        STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, NULL, true));
×
5882
        continue;
×
5883
      }
5884

5885
      STagValue **ppv = (STagValue **)tSimpleHashGet(pRes->tagMap, &pDst->info.colId, sizeof(pDst->info.colId));
1,111 ✔
5886
      if (ppv == NULL || *ppv == NULL || (*ppv)->pData == NULL) {
1,111 ✔
5887
        STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, NULL, true));
1 ✔
5888
        continue;
1 ✔
5889
      }
5890
      if (IS_VAR_DATA_TYPE(pDst->info.type)) {
1,110 ✔
5891
        STREAM_CHECK_RET_GOTO(varColSetVarData(pDst, 0, (*ppv)->pData, (*ppv)->nLen, false));
893 ✔
5892
      } else {
5893
        STREAM_CHECK_RET_GOTO(colDataSetVal(pDst, 0, (*ppv)->pData, false));
217 ✔
5894
      }
5895
    }
5896
  }
5897

5898
  stsDebug("vgId:%d %s get result rows:%" PRId64, TD_VID(pVnode), __func__, pBlock->info.rows);
1,701 ✔
5899
  printDataBlock(pBlock, __func__, "", streamId);
1,701 ✔
5900
  STREAM_CHECK_RET_GOTO(buildRsp(pBlock, &buf, &size));
1,701 ✔
5901

5902
end:
1,701 ✔
5903
  if(size == 0){
1,701 ✔
5904
    code = TSDB_CODE_STREAM_NO_DATA;
×
5905
  }
5906
  if (singleUid != NULL) taosArrayDestroy(singleUid);
1,701 ✔
5907
  if (emptyCols != NULL) taosArrayDestroy(emptyCols);
1,701 ✔
5908
  if (tagOnly   != NULL) taosArrayDestroy(tagOnly);
1,701 ✔
5909
  tSimpleHashCleanup(uid2Result);
1,701 ✔
5910
  if (metaReaderInited) {
1,701 ✔
5911
    sStreamReaderInfo->storageApi.metaReaderFn.clearReader(&metaReader);
×
5912
  }
5913
  STREAM_PRINT_LOG_END(code, lino);
1,701 ✔
5914
  sendTriggerPullRsp(pMsg, sStreamReaderInfo, buf, size, code, pRspStats);
1,701 ✔
5915
  blockDataDestroy(pBlock);
1,701 ✔
5916
  return code;
1,701 ✔
5917
}
5918

5919
static bool vnodeDimensionReaderPlanSupported(const SSubplan* pPlan) {
507 ✔
5920
  if (pPlan == NULL || pPlan->pNode == NULL || pPlan->subplanType != SUBPLAN_TYPE_SCAN || pPlan->dynTbname ||
507 ✔
5921
      pPlan->pVTables != NULL || LIST_LENGTH(pPlan->pSubQ) || LIST_LENGTH(pPlan->pChildren) ||
507 ✔
5922
      nodeType(pPlan->pNode) != QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN)
507 ✔
5923
    return false;
×
5924
  const STableScanPhysiNode* pTable = (const STableScanPhysiNode*)pPlan->pNode;
507 ✔
5925
  const SScanPhysiNode*      pScan = &pTable->scan;
507 ✔
5926
  return pScan->uid != 0 &&
1,014 ✔
5927
         (pScan->tableType == TSDB_SUPER_TABLE || pScan->tableType == TSDB_CHILD_TABLE ||
507 ✔
5928
          pScan->tableType == TSDB_NORMAL_TABLE) &&
476 ✔
5929
         !pScan->virtualStableScan && !pScan->useTriggerTableList && !pScan->node.dynamicOp &&
507 ✔
5930
         !LIST_LENGTH(pScan->node.pChildren) && pScan->node.pLimit == NULL && pScan->node.pSlimit == NULL &&
505 ✔
5931
         LIST_LENGTH(pScan->pScanCols) > 0 && pTable->scanSeq[0] == 1 && pTable->scanSeq[1] == 0 &&
504 ✔
5932
         pTable->ratio == 1 && pTable->scanRange.skey <= pTable->scanRange.ekey && pTable->pTimeRange == NULL &&
504 ✔
5933
         pTable->pExtTimeRange == NULL && pTable->pExtScanRange == NULL && pTable->interval == 0 &&
503 ✔
5934
         pTable->sliding == 0 && pTable->pDynamicScanFuncs == NULL;
1,014 ✔
5935
}
5936

5937
static int32_t vnodeCompareDimensionGid(const void* lhs, const void* rhs) {
27,264 ✔
5938
  uint64_t left = *(const uint64_t*)lhs, right = *(const uint64_t*)rhs;
27,264 ✔
5939
  return left < right ? -1 : left > right;
27,264 ✔
5940
}
5941

5942
static int32_t vnodeInstallDimensionBatch(SStreamTriggerReaderCalcInfo* pCalc, SResFetchReq* pReq) {
500 ✔
5943
  SStreamReaderDimensionBatch* pBatch = &pCalc->dimensionBatch;
500 ✔
5944
  pBatch->installed = true;
500 ✔
5945
  pBatch->reuseReader = pReq->reuseReader;
500 ✔
5946
  TSWAP(pBatch->runtime, *pReq->pStRtFuncInfo);
500 ✔
5947
  pBatch->pGroupOrder = taosArrayInit(tSimpleHashGetSize(pBatch->runtime.pGroupCalcInfos), sizeof(uint64_t));
500 ✔
5948
  if (pBatch->pGroupOrder == NULL) return terrno;
500 ✔
5949
  int32_t iter = 0;
500 ✔
5950
  void*   pGroup = NULL;
500 ✔
5951
  while ((pGroup = tSimpleHashIterate(pBatch->runtime.pGroupCalcInfos, pGroup, &iter)) != NULL) {
5,846 ✔
5952
    size_t          keyLen = 0;
5,346 ✔
5953
    const uint64_t* pGid = tSimpleHashGetKey(pGroup, &keyLen);
5,346 ✔
5954
    if (pGid == NULL || keyLen != sizeof(*pGid) || *pGid == 0) return TSDB_CODE_INVALID_PARA;
5,346 ✔
5955
    if (taosArrayPush(pBatch->pGroupOrder, pGid) == NULL) return terrno;
5,685 ✔
5956
  }
5957
  taosArraySort(pBatch->pGroupOrder, vnodeCompareDimensionGid);
500 ✔
5958
  int32_t code = tProjectStreamCalcContextForAllGroups(pBatch->runtime.pContextPolicy, pBatch->runtime.pAncestorContext,
500 ✔
5959
                                                       &pBatch->pContextViews);
5960
  if (code != TSDB_CODE_SUCCESS) return code;
500 ✔
5961
  // gid is the first member of a group view; use the same unsigned order as Runner.
5962
  taosArraySort(pBatch->pContextViews, vnodeCompareDimensionGid);
499 ✔
5963
  return TSDB_CODE_SUCCESS;
499 ✔
5964
}
5965

5966
static bool vnodeDimensionGroupMissing(int32_t code) {
5,797 ✔
5967
  return code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_TDB_TABLE_NOT_EXIST;
5,797 ✔
5968
}
5969

5970
static int32_t vnodeExecDimensionReaderBatch(SStreamTriggerReaderCalcInfo* pCalc, SReadHandle* handle,
523 ✔
5971
                                             const SResFetchReq* pReq, SArray* pResults, bool* pHasNext) {
5972
  SStreamReaderDimensionBatch* pBatch = &pCalc->dimensionBatch;
523 ✔
5973
  int64_t                      rows = 0, bytes = 0;
523 ✔
5974
  *pHasNext = true;
523 ✔
5975
  while (pBatch->groupIndex < taosArrayGetSize(pBatch->pGroupOrder)) {
6,067 ✔
5976
    uint64_t gid = *(uint64_t*)taosArrayGet(pBatch->pGroupOrder, pBatch->groupIndex);
5,787 ✔
5977
    int32_t  code = TSDB_CODE_SUCCESS;
5,787 ✔
5978
    if (!pBatch->groupStarted) {
5,787 ✔
5979
      code = stReaderBindDimensionGroup(pCalc, gid);
5,134 ✔
5980
      if (code != TSDB_CODE_SUCCESS) return code;
5,147 ✔
5981
      handle->streamRtInfo = &pCalc->rtInfo;
5,134 ✔
5982
      if (pCalc->pTaskInfo == NULL || !qNeedReset(pCalc->pTaskInfo)) {
5,134 ✔
5983
        qDestroyTask(pCalc->pTaskInfo);
71 ✔
5984
        pCalc->pTaskInfo = NULL;
71 ✔
5985
        code = qCreateStreamExecTaskInfo(&pCalc->pTaskInfo, pCalc->calcScanPlan, handle, NULL, pCalc->fetchState.nodeId,
71 ✔
5986
                                         pReq->taskId);
71 ✔
5987
      } else {
5988
        qSetStreamGen(pCalc->pTaskInfo, pCalc->rtInfo.funcInfo.streamGen);
5,063 ✔
5989
        setTaskScalarExtraInfo(pCalc->pTaskInfo);
5,063 ✔
5990
        code = qResetStreamDimensionScan(
5,063 ✔
5991
            pCalc->pTaskInfo, handle,
5992
            pBatch->reuseReader ? QUERY_TABLE_SCAN_RESET_REUSE_READER : QUERY_TABLE_SCAN_RESET_FRESH);
5,063 ✔
5993
      }
5994
      if (code != TSDB_CODE_SUCCESS && !vnodeDimensionGroupMissing(code)) return code;
5,134 ✔
5995
      if (code == TSDB_CODE_SUCCESS) {
5,120 ✔
5996
        code = qSetTaskId(pCalc->pTaskInfo, pReq->taskId, pReq->queryId);
5,120 ✔
5997
        if (code != TSDB_CODE_SUCCESS) return code;
5,120 ✔
5998
      }
5999
      pBatch->groupStarted = true;
5,124 ✔
6000
    }
6001
    SSDataBlock* borrowed = NULL;
5,777 ✔
6002
    if (code == TSDB_CODE_SUCCESS) {
5,777 ✔
6003
      uint64_t elapsed = 0;
5,773 ✔
6004
      qSetStreamGen(pCalc->pTaskInfo, pCalc->rtInfo.funcInfo.streamGen);
5,773 ✔
6005
      setTaskScalarExtraInfo(pCalc->pTaskInfo);
5,773 ✔
6006
      code = qExecTask(pCalc->pTaskInfo, &borrowed, &elapsed);
5,773 ✔
6007
    }
6008
    if (code != TSDB_CODE_SUCCESS && !vnodeDimensionGroupMissing(code)) return code;
5,777 ✔
6009
    bool         eog = vnodeDimensionGroupMissing(code) || borrowed == NULL;
5,776 ✔
6010
    SSDataBlock* owned = NULL;
5,776 ✔
6011
    if (eog) {
5,099 ✔
6012
      code = createDataBlock(&owned);
4,915 ✔
6013
      if (code == TSDB_CODE_SUCCESS) code = qSetMultiGroupEogBlock(owned, gid);
4,915 ✔
6014
    } else {
6015
      if (borrowed->info.rows <= 0) return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
861 ✔
6016
      code = createOneDataBlock(borrowed, true, &owned);
860 ✔
6017
      if (code == TSDB_CODE_SUCCESS) owned->info.id.baseGId = gid;
860 ✔
6018
    }
6019
    if (code == TSDB_CODE_SUCCESS && taosArrayPush(pResults, &owned) == NULL) code = terrno;
11,548 ✔
6020
    if (code != TSDB_CODE_SUCCESS) {
5,774 ✔
6021
      blockDataDestroy(owned);
1 ✔
6022
      return code;
1 ✔
6023
    }
6024
    rows += owned->info.rows;
5,774 ✔
6025
    bytes += blockDataGetSize(owned);
5,774 ✔
6026
    if (eog) {
5,774 ✔
6027
      ++pBatch->groupIndex;
4,915 ✔
6028
      pBatch->groupStarted = false;
4,915 ✔
6029
      // Broadcast's existing group transition preserves a static Reader's version range.
6030
      pBatch->reuseReader = true;
4,915 ✔
6031
    }
6032
    if (rows >= QUERY_EXEC_FETCH_MAX_ROWS || bytes >= QUERY_EXEC_FETCH_MAX_BYTES ||
11,319 ✔
6033
        taosArrayGetSize(pResults) >= QUERY_EXEC_FETCH_MAX_EVENTS)
5,545 ✔
6034
      break;
6035
  }
6036
  *pHasNext = pBatch->groupIndex < taosArrayGetSize(pBatch->pGroupOrder);
510 ✔
6037
  return TSDB_CODE_SUCCESS;
510 ✔
6038
}
6039

6040
static int32_t vnodeProcessStreamFetchMsg(SVnode* pVnode, SRpcMsg* pMsg, SQueueInfo* pInfo,
35,660 ✔
6041
                                          SStreamReaderResponseStats* pRspStats) {
6042
  int32_t                       code = 0;
35,660 ✔
6043
  int32_t                       lino = 0;
35,660 ✔
6044
  void*                         buf = NULL;
35,660 ✔
6045
  size_t                        size = 0;
35,660 ✔
6046
  void*                         taskAddr = NULL;
35,660 ✔
6047
  void*                         pTask = NULL;
35,659 ✔
6048
  SArray*                       pResList = NULL;
35,659 ✔
6049
  bool                          hasNext = false;
35,659 ✔
6050
  bool                          fetchStateTouched = false;
35,659 ✔
6051
  bool                          dimensionResults = false;
35,659 ✔
6052
  bool                          preparationClearOwnerRejected = false;
35,659 ✔
6053
  SArray*                       readRanges = NULL;
35,659 ✔
6054
  SStreamTriggerReaderCalcInfo  retired = {0};
35,659 ✔
6055
  SStreamReaderFetchState       candidate = {0};
35,659 ✔
6056
  SStreamTriggerReaderCalcInfo* sStreamReaderCalcInfo = NULL;
35,659 ✔
6057
  SStreamReaderTask*            pReaderTask = NULL;
35,659 ✔
6058
  SArray*                       calcInfoList = NULL;
35,659 ✔
6059

6060
  SResFetchReq req = {0};
35,659 ✔
6061
  STREAM_CHECK_CONDITION_GOTO(tDeserializeSResFetchReq(pMsg->pCont, pMsg->contLen, &req) < 0,
35,658 ✔
6062
                              TSDB_CODE_QRY_INVALID_INPUT);
820 ✔
6063
  STREAM_CHECK_NULL_GOTO(req.pStRtFuncInfo, TSDB_CODE_INVALID_PARA);
35,657 ✔
6064
  req.pStRtFuncInfo->curNodeId = TD_VID(pVnode);
35,657 ✔
6065
  calcInfoList = (SArray*)qStreamGetReaderInfo(req.queryId, req.taskId, &taskAddr);
35,657 ✔
6066
  STREAM_CHECK_NULL_GOTO(calcInfoList, terrno);
35,658 ✔
6067

6068
  STREAM_CHECK_CONDITION_GOTO(req.execId < 0, TSDB_CODE_INVALID_PARA);
35,658 ✔
6069
  sStreamReaderCalcInfo = taosArrayGetP(calcInfoList, req.execId);
35,658 ✔
6070
  STREAM_CHECK_NULL_GOTO(sStreamReaderCalcInfo, terrno);
35,658 ✔
6071
  bool readerFetch = req.pStRtFuncInfo->isMultiGroupCalc && req.pOpParam == NULL &&
36,040 ✔
6072
                     (req.readerDimensionBatch || req.pStRtFuncInfo->curGrpRead != NULL);
1,150 ✔
6073
  bool continuation =
36,478 ✔
6074
      !req.reset &&
38,095 ✔
6075
      (req.readerFetchId != 0 || sStreamReaderCalcInfo->fetchState.enabled ||
2,645 ✔
6076
       (req.pStRtFuncInfo->isMultiGroupCalc && req.pOpParam == NULL && req.pStRtFuncInfo->pGroupCalcInfos == NULL));
2,414 ✔
6077
  if (continuation) {
35,452 ✔
6078
    STREAM_CHECK_RET_GOTO(stReaderCheckFetchContinuation(sStreamReaderCalcInfo, &req, TD_VID(pVnode)));
230 ✔
6079
  } else {
6080
    STREAM_CHECK_CONDITION_GOTO(req.readerFetchId != 0 && (!req.reset || !readerFetch), TSDB_CODE_INVALID_MSG);
35,428 ✔
6081
    STREAM_CHECK_CONDITION_GOTO(req.reset && readerFetch && req.readerFetchId == 0, TSDB_CODE_INVALID_MSG);
35,428 ✔
6082
    STREAM_CHECK_CONDITION_GOTO(
35,427 ✔
6083
        req.pStRtFuncInfo->isMultiGroupCalc && req.pOpParam == NULL && req.pStRtFuncInfo->pGroupCalcInfos == NULL,
6084
        TSDB_CODE_INVALID_MSG);
613 ✔
6085
    if (req.readerDimensionBatch) {
35,427 ✔
6086
      STREAM_CHECK_CONDITION_GOTO(!req.readerGroupMode || req.dynTbname || req.pOpParam != NULL ||
507 ✔
6087
                                      !vnodeDimensionReaderPlanSupported(sStreamReaderCalcInfo->calcAst),
6088
                                  TSDB_CODE_INVALID_MSG);
459 ✔
6089
      STREAM_CHECK_RET_GOTO(
502 ✔
6090
          tValidateStreamDimensionReaderContext(req.pStRtFuncInfo, sStreamReaderCalcInfo->requiresContextPolicy));
607 ✔
6091
    }
6092
    if (req.pStRtFuncInfo->isMultiGroupCalc && req.pStRtFuncInfo->curGrpRead != NULL) {
35,421 ✔
6093
      STREAM_CHECK_RET_GOTO(stValidateNodeGroupReadBindings(req.pStRtFuncInfo, TD_VID(pVnode)));
413 ✔
6094
    }
6095
    STREAM_CHECK_RET_GOTO(tAdmitStreamContext(req.pStRtFuncInfo->pContextPolicy, req.pStRtFuncInfo->pAncestorContext,
35,421 ✔
6096
                                              sStreamReaderCalcInfo->requiresContextPolicy));
791 ✔
6097
  }
6098

6099
  pReaderTask = (SStreamReaderTask*)sStreamReaderCalcInfo->pTask;
35,629 ✔
6100
  pTask = pReaderTask;
35,629 ✔
6101
  ENodeType scanType = nodeType(sStreamReaderCalcInfo->calcAst->pNode);
35,629 ✔
6102
  bool      reuseReader = req.reuseReader && (scanType == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN ||
35,629 ✔
6103
                                         scanType == QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN);
4 ✔
6104
  ST_TASK_DLOG("vgId:%d %s start, execId:%d, reset:%d, pTaskInfo:%p, scan type:%d", TD_VID(pVnode), __func__,
35,629 ✔
6105
               req.execId, req.reset, sStreamReaderCalcInfo->pTaskInfo, scanType);
6106

6107
  if (reuseReader && (sStreamReaderCalcInfo->pTaskInfo == NULL || !qNeedReset(sStreamReaderCalcInfo->pTaskInfo))) {
35,629 ✔
6108
    code = TSDB_CODE_INVALID_PARA;
1 ✔
6109
    goto end;
1 ✔
6110
  }
6111

6112
  if (req.reset && req.readerDimensionBatch) {
35,628 ✔
6113
    // Owner rejection must leave the previous batch and its runtime intact.
6114
    code = qClearStreamDimensionPreparation(sStreamReaderCalcInfo->pTaskInfo);
501 ✔
6115
    preparationClearOwnerRejected = code == TSDB_CODE_QRY_IN_EXEC;
501 ✔
6116
    STREAM_CHECK_RET_GOTO(code);
501 ✔
6117
  }
6118
  if (req.reset && !req.readerDimensionBatch) qFinishReaderGroupRead(sStreamReaderCalcInfo->pTaskInfo);
35,627 ✔
6119

6120
  if (req.readerDimensionBatch) {
35,627 ✔
6121
    dimensionResults = true;
524 ✔
6122
    fetchStateTouched = true;
524 ✔
6123
    if (req.reset) {
524 ✔
6124
      stReaderCaptureFetchState(&candidate, &req, TD_VID(pVnode));
500 ✔
6125
      retired.dimensionBatch = sStreamReaderCalcInfo->dimensionBatch;
500 ✔
6126
      retired.rtInfo.funcInfo = sStreamReaderCalcInfo->rtInfo.funcInfo;
500 ✔
6127
      memset(&sStreamReaderCalcInfo->dimensionBatch, 0, sizeof(sStreamReaderCalcInfo->dimensionBatch));
500 ✔
6128
      memset(&sStreamReaderCalcInfo->rtInfo.funcInfo, 0, sizeof(sStreamReaderCalcInfo->rtInfo.funcInfo));
500 ✔
6129
      sStreamReaderCalcInfo->fetchState = candidate;
500 ✔
6130
      sStreamReaderCalcInfo->readerGroupModeSet = true;
500 ✔
6131
      sStreamReaderCalcInfo->readerGroupRequested = true;
500 ✔
6132
      sStreamReaderCalcInfo->readerGroupAccepted = true;
500 ✔
6133
      STREAM_CHECK_RET_GOTO(vnodeInstallDimensionBatch(sStreamReaderCalcInfo, &req));
500 ✔
6134
    }
6135
    sStreamReaderCalcInfo->rtInfo.execId = req.execId;
523 ✔
6136
    SReadHandle handle = {
523 ✔
6137
        .vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb, .cacheSttStatis = true};
523 ✔
6138
    initStorageAPI(&handle.api);
523 ✔
6139
    pResList = taosArrayInit(4, POINTER_BYTES);
523 ✔
6140
    STREAM_CHECK_NULL_GOTO(pResList, terrno);
523 ✔
6141
    code = vnodeExecDimensionReaderBatch(sStreamReaderCalcInfo, &handle, &req, pResList, &hasNext);
523 ✔
6142
    if (code == TSDB_CODE_SUCCESS && !hasNext) {
523 ✔
6143
      code = qClearStreamDimensionPreparation(sStreamReaderCalcInfo->pTaskInfo);
280 ✔
6144
      preparationClearOwnerRejected = code == TSDB_CODE_QRY_IN_EXEC;
280 ✔
6145
    }
6146
    goto end;
523 ✔
6147
  }
6148

6149
  if (!req.reset) {
35,103 ✔
6150
    STREAM_CHECK_RET_GOTO(stReaderSetGroupOutputMode(sStreamReaderCalcInfo, &req));
2,600 ✔
6151
  }
6152

6153
  if (req.reset) {
35,102 ✔
6154
    stReaderClearDimensionBatch(sStreamReaderCalcInfo);
32,503 ✔
6155
    if (readerFetch) stReaderCaptureFetchState(&candidate, &req, TD_VID(pVnode));
32,503 ✔
6156
    stReaderInvalidateFetchState(sStreamReaderCalcInfo);
32,503 ✔
6157
    sStreamReaderCalcInfo->fetchState.enabled = candidate.enabled;
32,503 ✔
6158
    fetchStateTouched = true;
32,503 ✔
6159
    sStreamReaderCalcInfo->rtInfo.execId = req.execId;
32,503 ✔
6160
    sStreamReaderCalcInfo->readerGroupModeSet = false;
32,503 ✔
6161
    sStreamReaderCalcInfo->readerGroupAccepted = false;
32,503 ✔
6162
    int64_t uid = 0;
32,503 ✔
6163
    if (req.dynTbname && !req.pStRtFuncInfo->isMultiGroupCalc) {
32,503 ✔
6164
      SArray* vals = req.pStRtFuncInfo->pStreamPartColVals;
3,260 ✔
6165
      for (int32_t i = 0; i < taosArrayGetSize(vals); ++i) {
3,260 ✔
6166
        SStreamGroupValue* pValue = taosArrayGet(vals, i);
3,260 ✔
6167
        if (pValue != NULL && pValue->isTbname) {
3,260 ✔
6168
          uid = pValue->uid;
3,260 ✔
6169
          break;
3,260 ✔
6170
        }
6171
      }
6172
    }
6173
    
6174
    SReadHandle handle = {0};
32,503 ✔
6175
    handle.vnode = pVnode;
32,503 ✔
6176
    handle.pMsgCb = &pVnode->msgCb;
32,503 ✔
6177
    handle.pWorkerCb = pInfo->workerCb;
32,503 ✔
6178
    handle.uid = uid;
32,503 ✔
6179
    handle.cacheSttStatis = true;
32,503 ✔
6180

6181
    initStorageAPI(&handle.api);
32,503 ✔
6182
    if (QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN == scanType || QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN == scanType) {
32,503 ✔
6183
      STimeRangeNode* node = (STimeRangeNode*)((STableScanPhysiNode*)(sStreamReaderCalcInfo->calcAst->pNode))->pTimeRange;
29,852 ✔
6184
      if (node != NULL) {
29,852 ✔
6185
        STREAM_CHECK_RET_GOTO(processCalaTimeRange(sStreamReaderCalcInfo, &req, node, &handle, false, TD_VID(pVnode),
11,001 ✔
6186
                                                   req.readerGroupMode ? &readRanges : NULL));
6187
      } else {
6188
        ST_TASK_DLOG("vgId:%d %s no scan time range node", TD_VID(pVnode), __func__);
18,899 ✔
6189
      }
6190

6191
      node = (STimeRangeNode*)((STableScanPhysiNode*)(sStreamReaderCalcInfo->calcAst->pNode))->pExtTimeRange;
29,851 ✔
6192
      if (node != NULL) {
29,851 ✔
6193
        STREAM_CHECK_RET_GOTO(
154 ✔
6194
            processCalaTimeRange(sStreamReaderCalcInfo, &req, node, &handle, true, TD_VID(pVnode), NULL));
6195
      } else {
6196
        ST_TASK_DLOG("vgId:%d %s no interp time range node", TD_VID(pVnode), __func__);
29,697 ✔
6197
      }
6198

6199
    }
6200

6201
    TSWAP(sStreamReaderCalcInfo->rtInfo.funcInfo, *req.pStRtFuncInfo);
32,502 ✔
6202
    sStreamReaderCalcInfo->rtInfo.funcInfo.hasPlaceHolder = sStreamReaderCalcInfo->hasPlaceHolder;
32,502 ✔
6203
    handle.streamRtInfo = &sStreamReaderCalcInfo->rtInfo;
32,502 ✔
6204

6205
    if (sStreamReaderCalcInfo->pTaskInfo == NULL || !qNeedReset(sStreamReaderCalcInfo->pTaskInfo)) {
32,502 ✔
6206
      qDestroyTask(sStreamReaderCalcInfo->pTaskInfo);
2,621 ✔
6207
      STREAM_CHECK_RET_GOTO(qCreateStreamExecTaskInfo(&sStreamReaderCalcInfo->pTaskInfo,
2,621 ✔
6208
                                                    sStreamReaderCalcInfo->calcScanPlan, &handle, NULL, TD_VID(pVnode),
6209
                                                    req.taskId));
6210
    } else {
6211
      // Must set scalar extra BEFORE reset: qResetTableScan rebuilds the table
6212
      // list which evaluates TagCond (RemoteValueList scalar subquery).  The
6213
      // thread-local gTaskScalarExtra.pSubJobCtx must point to THIS task's
6214
      // context so that sendFetchRemoteNodeReq uses the correct execId for
6215
      // routing to the right non-topTask exec slot.
6216
      qSetStreamGen(sStreamReaderCalcInfo->pTaskInfo, sStreamReaderCalcInfo->rtInfo.funcInfo.streamGen);
29,882 ✔
6217
      setTaskScalarExtraInfo(sStreamReaderCalcInfo->pTaskInfo);
29,882 ✔
6218
      EQueryTableScanResetMode resetMode =
29,882 ✔
6219
          reuseReader ? QUERY_TABLE_SCAN_RESET_REUSE_READER : QUERY_TABLE_SCAN_RESET_FRESH;
6220
      code = qResetTableScan(sStreamReaderCalcInfo->pTaskInfo, &handle, resetMode);
29,882 ✔
6221
      STREAM_CHECK_RET_GOTO(code);
29,881 ✔
6222
    }
6223

6224
    STREAM_CHECK_RET_GOTO(qSetTaskId(sStreamReaderCalcInfo->pTaskInfo, req.taskId, req.queryId));
32,500 ✔
6225
    STREAM_CHECK_RET_GOTO(stReaderSetGroupOutputMode(sStreamReaderCalcInfo, &req));
32,500 ✔
6226
    if (sStreamReaderCalcInfo->readerGroupAccepted) {
32,498 ✔
6227
      STREAM_CHECK_RET_GOTO(qInstallReaderTableRanges(sStreamReaderCalcInfo->pTaskInfo, readRanges));
195 ✔
6228
    }
6229
    sStreamReaderCalcInfo->fetchState = candidate;
32,498 ✔
6230
    ST_TASK_DLOG("vgId:%d Reader group output requested:%d accepted:%d, scan type:%d execId:%d", TD_VID(pVnode),
32,498 ✔
6231
                 req.readerGroupMode, sStreamReaderCalcInfo->readerGroupAccepted, scanType, req.execId);
6232
  }
6233

6234
  fetchStateTouched = true;
35,099 ✔
6235
  sStreamReaderCalcInfo->rtInfo.execId = req.execId;
35,099 ✔
6236
  if (req.pOpParam != NULL) {
35,099 ✔
6237
    qUpdateOperatorParam(sStreamReaderCalcInfo->pTaskInfo, (void*)req.pOpParam);
4,887 ✔
6238
  }
6239

6240
  qSetStreamGen(sStreamReaderCalcInfo->pTaskInfo, sStreamReaderCalcInfo->rtInfo.funcInfo.streamGen);
35,099 ✔
6241

6242
  pResList = taosArrayInit(4, POINTER_BYTES);
35,099 ✔
6243
  STREAM_CHECK_NULL_GOTO(pResList, terrno);
35,099 ✔
6244
  uint64_t ts = 0;
35,099 ✔
6245
  // Stream fetches enter via vnode worker threads that may not have
6246
  // initialized the thread_local gTaskScalarExtra for this task.  Without
6247
  // this, scalar subqueries embedded in the operator (e.g. WHERE ts >=
6248
  // (SELECT last_row(ts) FROM ref)) fail with "no subJob ctx" on whichever
6249
  // event happens to land on a fresh worker thread.
6250
  setTaskScalarExtraInfo(sStreamReaderCalcInfo->pTaskInfo);
35,099 ✔
6251
  code = qExecTaskOpt(sStreamReaderCalcInfo->pTaskInfo, pResList, &ts, &hasNext, NULL, req.pOpParam != NULL);
35,099 ✔
6252
  STREAM_CHECK_RET_GOTO(code);
35,099 ✔
6253

6254
  for(size_t i = 0; i < taosArrayGetSize(pResList); i++){
92,805 ✔
6255
    SSDataBlock* pBlock = taosArrayGetP(pResList, i);
57,714 ✔
6256
    if (pBlock == NULL) continue;
57,714 ✔
6257
    printDataBlock(pBlock, __func__, "fetch", ((SStreamTask*)pTask)->streamId);
57,714 ✔
6258
/*    
6259
    if (sStreamReaderCalcInfo->rtInfo.funcInfo.withExternalWindow) {
6260
      STREAM_CHECK_RET_GOTO(qStreamFilter(pBlock, sStreamReaderCalcInfo->pFilterInfo, NULL));
6261
      printDataBlock(pBlock, __func__, "fetch filter");
6262
    }
6263
*/    
6264
  }
6265

6266
end: {
35,140 ✔
6267
  bool          grouped = sStreamReaderCalcInfo != NULL && sStreamReaderCalcInfo->readerGroupAccepted;
35,661 ✔
6268
  const int32_t rspCode =
37,261 ✔
6269
      grouped ? (code == TSDB_CODE_SUCCESS
6270
                     ? streamBuildGroupFetchRsp(pResList, hasNext, &buf, &size, pVnode->config.tsdbCfg.precision)
862 ✔
6271
                     : code)
904 ✔
6272
              : streamBuildFetchRsp(pResList, hasNext, &buf, &size, pVnode->config.tsdbCfg.precision);
35,046 ✔
6273
  if (code == TSDB_CODE_SUCCESS) {
35,617 ✔
6274
    code = rspCode;
35,600 ✔
6275
    if (code == TSDB_CODE_SUCCESS) {
35,600 ✔
6276
      addReaderResponseBlocks(pRspStats, pResList);
35,597 ✔
6277
    }
6278
  }
6279
}
6280

6281
  if (fetchStateTouched && !preparationClearOwnerRejected && (code != TSDB_CODE_SUCCESS || !hasNext)) {
35,659 ✔
6282
    if (!dimensionResults) qFinishReaderGroupRead(sStreamReaderCalcInfo->pTaskInfo);
32,910 ✔
6283
    stReaderInvalidateFetchState(sStreamReaderCalcInfo);
32,909 ✔
6284
  }
6285
  taosArrayDestroy(readRanges);
35,658 ✔
6286
  if (dimensionResults && code != TSDB_CODE_SUCCESS && !preparationClearOwnerRejected) {
35,658 ✔
6287
    stReaderClearDimensionBatch(sStreamReaderCalcInfo);
15 ✔
6288
  }
6289
  // Release the previous owner only after the executor has rebound or been destroyed.
6290
  stReaderClearDimensionBatch(&retired);
35,658 ✔
6291
  tDestroyStRtFuncInfo(&retired.rtInfo.funcInfo);
35,658 ✔
6292
  if (fetchStateTouched && sStreamReaderCalcInfo->rtInfo.funcInfo.isMultiGroupCalc) {
35,659 ✔
6293
    sStreamReaderCalcInfo->rtInfo.funcInfo.pStreamPesudoFuncVals = NULL;
597 ✔
6294
    sStreamReaderCalcInfo->rtInfo.funcInfo.pStreamPartColVals = NULL;
597 ✔
6295
  }
6296

6297
  if ((sStreamReaderCalcInfo == NULL || !sStreamReaderCalcInfo->readerGroupAccepted) &&
35,659 ✔
6298
      (code == TSDB_CODE_PAR_TABLE_NOT_EXIST || code == TSDB_CODE_TDB_TABLE_NOT_EXIST)) {
34,755 ✔
6299
    code = TDB_CODE_SUCCESS;
822 ✔
6300
  }
6301
  if (pReaderTask != NULL) {
35,659 ✔
6302
    pRspStats->activeScanContexts = taosArrayGetSize(calcInfoList);
35,625 ✔
6303
    pRspStats->activeScanContextsValid = true;
35,625 ✔
6304
    int64_t nowMonoUs = streamTaskGetMonotonicUs();
35,625 ✔
6305
    stReaderTaskRecordPullResult(pReaderTask, pRspStats, code, nowMonoUs, taosGetTimestampMs());
35,625 ✔
6306
  }
6307
  if (dimensionResults)
35,660 ✔
6308
    taosArrayDestroyP(pResList, (FDelete)blockDataDestroy);
524 ✔
6309
  else
6310
    taosArrayDestroy(pResList);
35,136 ✔
6311

6312
  STREAM_PRINT_LOG_END(code, lino);
35,660 ✔
6313
  SRpcMsg rsp = {.msgType = TDMT_STREAM_FETCH_RSP, .info = pMsg->info, .pCont = buf, .contLen = size, .code = code};
35,659 ✔
6314
  tDestroySResFetchReq(&req);
35,660 ✔
6315
  if (TDB_CODE_SUCCESS != code) {
35,659 ✔
6316
    if (pTask != NULL) {
63 ✔
6317
      ST_TASK_ELOG("vgId:%d %s failed, code:%d - %s", TD_VID(pVnode), __func__, code, tstrerror(code));
31 ✔
6318
    } else {
6319
      stError("vgId:%d %s failed, code:%d - %s", TD_VID(pVnode), __func__, code, tstrerror(code));
32 ✔
6320
    }
6321
  }
6322
  streamReleaseTask(taskAddr);
35,659 ✔
6323
  tmsgSendRsp(&rsp);
35,660 ✔
6324
  return code;
35,660 ✔
6325
}
6326

6327
#if defined(BUILD_TEST)
6328
int32_t vnodeTestProcessStreamFetchMsg(int32_t vgId, SRpcMsg* pMsg) {
773 ✔
6329
  SVnode                     vnode = {0};
773 ✔
6330
  SQueueInfo                 queueInfo = {0};
773 ✔
6331
  SStreamReaderResponseStats stats = {0};
773 ✔
6332
  vnode.config.vgId = vgId;
773 ✔
6333
  vnode.config.tsdbCfg.precision = TSDB_TIME_PRECISION_MILLI;
773 ✔
6334
  return vnodeProcessStreamFetchMsg(&vnode, pMsg, &queueInfo, &stats);
773 ✔
6335
}
6336
#endif
6337

6338
static int32_t initTableList(SStreamTriggerReaderInfo* sStreamReaderInfo, SVnode* pVnode) {
102,165 ✔
6339
  int32_t code = 0;
102,165 ✔
6340
  void*   pTask = sStreamReaderInfo->pTask;
102,165 ✔
6341
  if (sStreamReaderInfo->tableList.pTableList != NULL) {  
102,165 ✔
6342
    return code;
100,251 ✔
6343
  }
6344
  taosWLockLatch(&sStreamReaderInfo->lock);
1,671 ✔
6345
  sStreamReaderInfo->pVnode = pVnode;
1,690 ✔
6346
  initStorageAPI(&sStreamReaderInfo->storageApi);
1,690 ✔
6347
  if (sStreamReaderInfo->tableList.pTableList == NULL) {
1,690 ✔
6348
    code = initStreamTableListInfo(&sStreamReaderInfo->tableList,
1,704 ✔
6349
                                   sStreamReaderInfo->isRollupReader ? UIDMAP_MULTI : UIDMAP_SINGLE);
1,690 ✔
6350
    if (code == 0) {
1,690 ✔
6351
      code = generateTablistForStreamReader(pVnode, sStreamReaderInfo);
1,690 ✔
6352
      if (code != 0) {
1,690 ✔
6353
        qStreamDestroyTableInfo(&sStreamReaderInfo->tableList);
14 ✔
6354
      } else {
6355
        sStreamReaderInfo->tableList.version = pVnode->state.applied;
1,676 ✔
6356
        ST_TASK_DLOG("vgId:%d %s initialized table list, tables:%zu, version:%" PRId64, TD_VID(pVnode), __func__,
1,676 ✔
6357
                     taosArrayGetSize(sStreamReaderInfo->tableList.pTableList), sStreamReaderInfo->tableList.version);
6358
      }
6359
    }
6360
  }
6361
  taosWUnLockLatch(&sStreamReaderInfo->lock);
1,690 ✔
6362
  return code;
1,690 ✔
6363
}
6364

6365
int32_t vnodeProcessStreamReaderMsg(SVnode* pVnode, SRpcMsg* pMsg, SQueueInfo *pInfo) {
137,270 ✔
6366
  int32_t                   code = 0;
137,270 ✔
6367
  int32_t                   lino = 0;
137,270 ✔
6368
  SSTriggerPullRequestUnion req = {0};
137,270 ✔
6369
  void*                     taskAddr = NULL;
137,254 ✔
6370
  SStreamTriggerReaderInfo* sStreamReaderInfo = NULL;
137,245 ✔
6371
  bool                      sendRsp = false;
137,245 ✔
6372
  SStreamReaderResponseStats rspStats = {.requestStartMonoUs = streamTaskGetMonotonicUs()};
137,245 ✔
6373

6374
  vDebug("vgId:%d, msg:%p in stream reader queue is processing", pVnode->config.vgId, pMsg);
137,280 ✔
6375
  if (!syncIsReadyForRead(pVnode->sync)) {
137,278 ✔
6376
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
169 ✔
6377
    return 0;
169 ✔
6378
  }
6379

6380
  if (pMsg->msgType == TDMT_STREAM_FETCH) {
137,111 ✔
6381
    return vnodeProcessStreamFetchMsg(pVnode, pMsg, pInfo, &rspStats);
34,886 ✔
6382
  } else if (pMsg->msgType == TDMT_STREAM_TRIGGER_PULL) {
102,225 ✔
6383
    void*   pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
102,228 ✔
6384
    int32_t len = pMsg->contLen - sizeof(SMsgHead);
102,228 ✔
6385
    STREAM_CHECK_RET_GOTO(tDeserializeSTriggerPullRequest(pReq, len, &req));
102,228 ✔
6386
    sStreamReaderInfo = qStreamGetReaderInfo(req.base.streamId, req.base.readerTaskId, &taskAddr);
102,212 ✔
6387
    STREAM_CHECK_NULL_GOTO(sStreamReaderInfo, terrno);
102,220 ✔
6388
    void* pTask = sStreamReaderInfo->pTask;
102,199 ✔
6389
    ST_TASK_DLOG("vgId:%d %s start, type:%d, requestedStream:%" PRIx64 ", requestedTask:%" PRIx64
102,199 ✔
6390
                 ", requestedSession:%" PRIx64 ", applied:%" PRIx64,
6391
                 TD_VID(pVnode), __func__, req.base.type, req.base.streamId, req.base.readerTaskId,
6392
                 req.base.sessionId, pVnode->state.applied);
6393
    if (sStreamReaderInfo->isVtableStream && !atomic_load_8(&sStreamReaderInfo->vTableReady)) {
102,199 ✔
6394
      switch (req.base.type) {
945 ✔
6395
        case STRIGGER_PULL_SET_TABLE:
925 ✔
6396
        case STRIGGER_PULL_GROUP_COL_VALUE:
6397
        case STRIGGER_PULL_VTABLE_INFO:
6398
        case STRIGGER_PULL_VTABLE_PSEUDO_COL:
6399
        case STRIGGER_PULL_OTABLE_INFO:
6400
          break;
925 ✔
6401
        default:
16 ✔
6402
          stWarn("vgId:%d virtual table reader is not ready, type:%d, streamId:%" PRIx64
16 ✔
6403
                 ", readerTaskId:%" PRIx64 ", sessionId:%" PRIx64 ", set table is required",
6404
                 TD_VID(pVnode), req.base.type, req.base.streamId, req.base.readerTaskId, req.base.sessionId);
6405
          STREAM_CHECK_RET_GOTO(TSDB_CODE_STREAM_TASK_NOT_EXIST);
16 ✔
6406
      }
6407
    }
6408
    STREAM_CHECK_RET_GOTO(initTableList(sStreamReaderInfo, pVnode));
102,183 ✔
6409
    sendRsp = true;
102,169 ✔
6410
    switch (req.base.type) {
102,169 ✔
6411
      case STRIGGER_PULL_SET_TABLE:
418 ✔
6412
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamSetTableReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
418 ✔
6413
        break;
414 ✔
6414
      case STRIGGER_PULL_LAST_TS:
1,585 ✔
6415
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamLastTsReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
1,585 ✔
6416
        break;
1,575 ✔
6417
      case STRIGGER_PULL_FIRST_TS:
962 ✔
6418
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamFirstTsReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
962 ✔
6419
        break;
962 ✔
6420
      case STRIGGER_PULL_TSDB_META:
1,903 ✔
6421
      case STRIGGER_PULL_TSDB_META_NEXT:
6422
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbMetaReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
1,903 ✔
6423
        break;
1,903 ✔
6424
      case STRIGGER_PULL_TSDB_TS_DATA:
506 ✔
6425
        if (sStreamReaderInfo->isVtableStream) {
506 ✔
6426
          STREAM_CHECK_RET_GOTO(
6 ✔
6427
              vnodeProcessStreamTsdbTsDataReqVTable(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
6428
        } else {
6429
          STREAM_CHECK_RET_GOTO(
500 ✔
6430
              vnodeProcessStreamTsdbTsDataReqNonVTable(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
6431
        }
6432
        break;
506 ✔
6433
      case STRIGGER_PULL_TSDB_TRIGGER_DATA:
706 ✔
6434
      case STRIGGER_PULL_TSDB_TRIGGER_DATA_NEXT:
6435
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbTriggerDataReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
706 ✔
6436
        break;
355 ✔
6437
      case STRIGGER_PULL_TSDB_CALC_DATA:
31,422 ✔
6438
      case STRIGGER_PULL_TSDB_CALC_DATA_NEXT:
6439
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbCalcDataReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
31,422 ✔
6440
        break;
31,419 ✔
6441
      case STRIGGER_PULL_TSDB_DATA:
973 ✔
6442
      case STRIGGER_PULL_TSDB_DATA_NEXT:
6443
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamTsdbVirtalDataReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
973 ✔
6444
        break;
971 ✔
6445
      case STRIGGER_PULL_GROUP_COL_VALUE:
1,643 ✔
6446
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamGroupColValueReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
1,643 ✔
6447
        break;
1,631 ✔
6448
      case STRIGGER_PULL_VTABLE_INFO:
311 ✔
6449
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableInfoReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
311 ✔
6450
        break;
311 ✔
6451
      case STRIGGER_PULL_VTABLE_PSEUDO_COL:
1,701 ✔
6452
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamVTableTagInfoReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
1,701 ✔
6453
        break;
1,701 ✔
6454
      case STRIGGER_PULL_OTABLE_INFO:
414 ✔
6455
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamOTableInfoReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
414 ✔
6456
        break;
414 ✔
6457
      case STRIGGER_PULL_WAL_META_NEW:
20,874 ✔
6458
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaNewReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
20,874 ✔
6459
        break;
20,769 ✔
6460
      case STRIGGER_PULL_WAL_DATA_NEW:
7,636 ✔
6461
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
7,636 ✔
6462
        break;
7,622 ✔
6463
      case STRIGGER_PULL_WAL_META_DATA_NEW:
28,562 ✔
6464
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalMetaDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
28,562 ✔
6465
        break;
28,430 ✔
6466
      case STRIGGER_PULL_WAL_CALC_DATA_NEW:
2,536 ✔
6467
        STREAM_CHECK_RET_GOTO(vnodeProcessStreamWalCalcDataNewReq(pVnode, pMsg, &req, sStreamReaderInfo, &rspStats));
2,536 ✔
6468
        break;
2,531 ✔
6469
      default:
17 ✔
6470
        ST_TASK_ELOG("unknown inner message type:%d in stream reader queue", req.base.type);
17 ✔
6471
        sendRsp = false;
×
6472
        STREAM_CHECK_RET_GOTO(TSDB_CODE_APP_ERROR);
×
6473
    }
6474
  } else {
6475
    vError("unknown msg type:%d in stream reader queue", pMsg->msgType);
×
6476
    STREAM_CHECK_RET_GOTO(TSDB_CODE_APP_ERROR);
×
6477
  }
6478
end:
102,186 ✔
6479
  tDestroySTriggerPullRequest(&req);
102,197 ✔
6480
  STREAM_PRINT_LOG_END(code, lino);
102,182 ✔
6481
  if (!sendRsp) {
102,204 ✔
6482
    if (pMsg->msgType == TDMT_STREAM_TRIGGER_PULL) {
51 ✔
6483
      if (code == TSDB_CODE_STREAM_ROLLUP_ILLEGAL_PATH) {
51 ✔
6484
        sendTriggerPullRsp(pMsg, sStreamReaderInfo, NULL, 0, code, &rspStats);
14 ✔
6485
      } else {
6486
        sendTriggerPullRsp(pMsg, sStreamReaderInfo, pMsg->info.rsp, pMsg->info.rspLen, code, &rspStats);
37 ✔
6487
      }
6488
    } else {
6489
      SRpcMsg rsp = {
×
6490
          .code = code,
6491
          .pCont = pMsg->info.rsp,
×
6492
          .contLen = pMsg->info.rspLen,
×
6493
          .info = pMsg->info,
6494
      };
6495
      tmsgSendRsp(&rsp);
×
6496
    }
6497
  }
6498
  streamReleaseTask(taskAddr);
102,204 ✔
6499
  return code;
102,216 ✔
6500
}
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