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

taosdata / TDengine / #4795

13 Oct 2025 06:50AM UTC coverage: 58.426% (+0.2%) from 58.201%
#4795

push

travis-ci

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

fix: fix tdgpt timemoe model directory

138954 of 303332 branches covered (45.81%)

Branch coverage included in aggregate %.

210571 of 294900 relevant lines covered (71.4%)

16934524.21 hits per line

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

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

16
#include "executorInt.h"
17
#include "filter.h"
18
#include "function.h"
19
#include "operator.h"
20
#include "query.h"
21
#include "querytask.h"
22
#include "tdatablock.h"
23
#include "thash.h"
24
#include "tmsg.h"
25
#include "tref.h"
26
#include "trpc.h"
27

28
typedef struct SFetchRspHandleWrapper {
29
  uint32_t exchangeId;
30
  int32_t  sourceIndex;
31
  int64_t  seqId;
32
} SFetchRspHandleWrapper;
33

34
typedef struct SSourceDataInfo {
35
  int32_t            index;
36
  int64_t            seqId;
37
  SRWLatch           lock;
38
  SRetrieveTableRsp* pRsp;
39
  uint64_t           totalRows;
40
  int64_t            startTime;
41
  int32_t            code;
42
  EX_SOURCE_STATUS   status;
43
  const char*        taskId;
44
  SArray*            pSrcUidList;
45
  int32_t            srcOpType;
46
  bool               tableSeq;
47
  char*              decompBuf;
48
  int32_t            decompBufSize;
49
  SOrgTbInfo*        colMap;
50
  bool               isVtbRefScan;
51
  bool               isVtbTagScan;
52
  STimeWindow        window;
53
  bool               fetchSent; // need reset
54
} SSourceDataInfo;
55

56
static void destroyExchangeOperatorInfo(void* param);
57
static void freeBlock(void* pParam);
58
static void freeSourceDataInfo(void* param);
59
static void setAllSourcesCompleted(SOperatorInfo* pOperator);
60

61
static int32_t loadRemoteDataCallback(void* param, SDataBuf* pMsg, int32_t code);
62
static int32_t doSendFetchDataRequest(SExchangeInfo* pExchangeInfo, SExecTaskInfo* pTaskInfo, int32_t sourceIndex);
63
static int32_t getCompletedSources(const SArray* pArray, int32_t* pRes);
64
static int32_t prepareConcurrentlyLoad(SOperatorInfo* pOperator);
65
static int32_t seqLoadRemoteData(SOperatorInfo* pOperator);
66
static int32_t prepareLoadRemoteData(SOperatorInfo* pOperator);
67
static int32_t handleLimitOffset(SOperatorInfo* pOperator, SLimitInfo* pLimitInfo, SSDataBlock* pBlock,
68
                                 bool holdDataInBuf);
69
static int32_t doExtractResultBlocks(SExchangeInfo* pExchangeInfo, SSourceDataInfo* pDataInfo);
70

71
static int32_t exchangeWait(SOperatorInfo* pOperator, SExchangeInfo* pExchangeInfo);
72

73

74
static void streamConcurrentlyLoadRemoteData(SOperatorInfo* pOperator, SExchangeInfo* pExchangeInfo,
29,488✔
75
                                           SExecTaskInfo* pTaskInfo) {
76
  int32_t code = 0;
29,488✔
77
  int32_t lino = 0;
58,976✔
78
  int64_t startTs = taosGetTimestampUs();  
29,488✔
79
  int32_t  totalSources = (int32_t)taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
29,488✔
80
  int32_t completed = 0;
29,486✔
81
  code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &completed);
29,486✔
82
  if (code != TSDB_CODE_SUCCESS) {
29,485!
83
    pTaskInfo->code = code;
×
84
    T_LONG_JMP(pTaskInfo->env, code);
×
85
  }
86
  if (completed == totalSources) {
29,485✔
87
    qDebug("%s no load since all sources completed, completed:%d, totalSources:%d", pTaskInfo->id.str, completed, totalSources);
7,581✔
88
    setAllSourcesCompleted(pOperator);
7,581✔
89
    return;
29,458✔
90
  }
91

92
  SSourceDataInfo* pDataInfo = NULL;
21,904✔
93

94
  while (1) {
12,172✔
95
    if (pExchangeInfo->current < 0) {
34,076✔
96
      qDebug("current %d and all sources complted, totalSources:%d", pExchangeInfo->current, totalSources);
51✔
97
      setAllSourcesCompleted(pOperator);
51✔
98
      return;
51✔
99
    }
100
    
101
    if (pExchangeInfo->current >= totalSources) {
34,025✔
102
      completed = 0;
14,024✔
103
      code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &completed);
14,024✔
104
      if (code != TSDB_CODE_SUCCESS) {
14,024!
105
        pTaskInfo->code = code;
×
106
        T_LONG_JMP(pTaskInfo->env, code);
×
107
      }
108
      if (completed == totalSources) {
14,024✔
109
        qDebug("stop to load since all sources complted, completed:%d, totalSources:%d", completed, totalSources);
11,868✔
110
        setAllSourcesCompleted(pOperator);
11,868✔
111
        return;
11,868✔
112
      }
113
      
114
      pExchangeInfo->current = 0;
2,156✔
115
    }
116

117
    qDebug("%s start stream exchange %p idx:%d fetch", GET_TASKID(pTaskInfo), pExchangeInfo, pExchangeInfo->current);
22,157✔
118

119
    SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, pExchangeInfo->current);
22,157✔
120
    if (!pDataInfo) {
22,156!
121
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
122
      pTaskInfo->code = terrno;
×
123
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
124
    }
125

126
    if (pDataInfo->status == EX_SOURCE_DATA_EXHAUSTED) {
22,156✔
127
      pExchangeInfo->current++;
23✔
128
      continue;
23✔
129
    }
130

131
    pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
22,133✔
132

133
    code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, pExchangeInfo->current);
22,133✔
134
    if (code != TSDB_CODE_SUCCESS) {
22,137!
135
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
136
      pTaskInfo->code = code;
×
137
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
138
    }
139

140
    while (true) {
10✔
141
      code = exchangeWait(pOperator, pExchangeInfo);
22,147✔
142
      if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
22,147!
143
        T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
10!
144
      }
145

146
      int64_t currSeqId = atomic_load_64(&pExchangeInfo->seqId);
22,137✔
147
      if (pDataInfo->seqId != currSeqId) {
22,136✔
148
        qDebug("%s seq rsp reqId %" PRId64 " mismatch with exchange %p curr seqId %" PRId64 ", ignore it", 
10✔
149
            GET_TASKID(pTaskInfo), pDataInfo->seqId, pExchangeInfo, currSeqId);
150
        taosMemoryFreeClear(pDataInfo->pRsp);
10!
151
        continue;
10✔
152
      }
153

154
      break;
22,126✔
155
    }
156

157
    SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pExchangeInfo->current);
22,126✔
158
    if (!pSource) {
22,126!
159
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
160
      pTaskInfo->code = terrno;
×
161
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
162
    }
163

164
    if (pDataInfo->code != TSDB_CODE_SUCCESS) {
22,126✔
165
      qError("%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64 " execId:%d error happens, code:%s",
17!
166
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
167
             tstrerror(pDataInfo->code));
168
      pTaskInfo->code = pDataInfo->code;
17✔
169
      T_LONG_JMP(pTaskInfo->env, code);
17!
170
    }
171

172
    SRetrieveTableRsp*   pRsp = pDataInfo->pRsp;
22,109✔
173
    SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
22,109✔
174

175
    if (pRsp->numOfRows == 0) {
22,109✔
176
      qDebug("exhausted %p,%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64
12,148✔
177
             " execId:%d idx %d of total completed, rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 " try next", pDataInfo,
178
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
179
             pExchangeInfo->current + 1, pDataInfo->totalRows, pLoadInfo->totalRows);
180

181
      pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
12,149✔
182
      if (pDataInfo->isVtbRefScan || pDataInfo->isVtbTagScan) {
12,149!
183
        pExchangeInfo->current = -1;
51✔
184
      } else {
185
        pExchangeInfo->current += 1;
12,098✔
186
      }
187
      taosMemoryFreeClear(pDataInfo->pRsp);
12,149!
188
      continue;
12,149✔
189
    }
190

191
    code = doExtractResultBlocks(pExchangeInfo, pDataInfo);
9,961✔
192
    TAOS_CHECK_EXIT(code);
9,959!
193

194
    SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
9,959✔
195
    if (pRsp->completed == 1) {
9,959✔
196
      qDebug("exhausted %p,%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d numOfRows:%" PRId64
7,752✔
197
             ", rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 ", totalBytes:%" PRIu64 " try next %d/%d", pDataInfo,
198
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
199
             pRetrieveRsp->numOfRows, pDataInfo->totalRows, pLoadInfo->totalRows, pLoadInfo->totalSize,
200
             pExchangeInfo->current + 1, totalSources);
201

202
      pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
7,752✔
203
      if (pDataInfo->isVtbRefScan) {
7,752!
204
        pExchangeInfo->current = -1;
×
205
        taosMemoryFreeClear(pDataInfo->pRsp);
×
206
        continue;
×
207
      }
208
    } else {
209
      qDebug("%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d idx:%d numOfRows:%" PRId64
2,207✔
210
             ", totalRows:%" PRIu64 ", totalBytes:%" PRIu64,
211
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
212
             pExchangeInfo->current, pRetrieveRsp->numOfRows, pLoadInfo->totalRows, pLoadInfo->totalSize);
213
    }
214

215
    updateLoadRemoteInfo(pLoadInfo, pRetrieveRsp->numOfRows, pRetrieveRsp->compLen, startTs, pOperator);
9,959✔
216
    pDataInfo->totalRows += pRetrieveRsp->numOfRows;
9,959✔
217

218
    pExchangeInfo->current++;
9,959✔
219

220
    taosMemoryFreeClear(pDataInfo->pRsp);
9,959!
221
    return;
9,958✔
222
  }
223

224
_exit:
×
225

226
  if (code) {
×
227
    pTaskInfo->code = code;
×
228
    qError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
229
  }
230
}
231

232

233
static void concurrentlyLoadRemoteDataImpl(SOperatorInfo* pOperator, SExchangeInfo* pExchangeInfo,
6,177,391✔
234
                                           SExecTaskInfo* pTaskInfo) {
235
  int32_t code = 0;
6,177,391✔
236
  int32_t lino = 0;
6,177,391✔
237
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
6,177,391✔
238
  int32_t completed = 0;
6,176,948✔
239
  code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &completed);
6,176,948✔
240
  if (code != TSDB_CODE_SUCCESS) {
6,176,101!
241
    pTaskInfo->code = code;
×
242
    T_LONG_JMP(pTaskInfo->env, code);
×
243
  }
244
  if (completed == totalSources) {
6,176,101✔
245
    setAllSourcesCompleted(pOperator);
1,605,934✔
246
    return;
6,178,133✔
247
  }
248

249
  SSourceDataInfo* pDataInfo = NULL;
4,570,167✔
250

251
  while (1) {
198,230✔
252
    qDebug("prepare wait for ready, %p, %s", pExchangeInfo, GET_TASKID(pTaskInfo));
4,768,397✔
253
    code = exchangeWait(pOperator, pExchangeInfo);
4,768,397✔
254

255
    if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
4,770,887!
256
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
21!
257
    }
258

259
    for (int32_t i = 0; i < totalSources; ++i) {
28,316,255✔
260
      pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, i);
28,293,083✔
261
      QUERY_CHECK_NULL(pDataInfo, code, lino, _exit, terrno);
28,271,065!
262
      if (pDataInfo->status == EX_SOURCE_DATA_EXHAUSTED) {
28,310,495✔
263
        continue;
22,552,903✔
264
      }
265

266
      if (pDataInfo->status != EX_SOURCE_DATA_READY) {
5,757,592✔
267
        continue;
992,529✔
268
      }
269

270
      int64_t currSeqId = atomic_load_64(&pExchangeInfo->seqId);
4,765,063✔
271
      if (pDataInfo->seqId != currSeqId) {
4,767,829!
272
        qDebug("concurrent rsp reqId %" PRId64 " mismatch with exchange %p curr seqId %" PRId64 ", ignore it", pDataInfo->seqId, pExchangeInfo, currSeqId);
×
273
        taosMemoryFreeClear(pDataInfo->pRsp);
×
274
        break;
×
275
      }
276

277
      if (pDataInfo->code != TSDB_CODE_SUCCESS) {
4,767,829✔
278
        code = pDataInfo->code;
15✔
279
        TAOS_CHECK_EXIT(code);
15!
280
      }
281

282
      tmemory_barrier();
4,767,814✔
283
      SRetrieveTableRsp*     pRsp = pDataInfo->pRsp;
4,767,814✔
284
      SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pDataInfo->index);
4,767,814✔
285
      QUERY_CHECK_NULL(pSource, code, lino, _exit, terrno);
4,768,375!
286

287
      // todo
288
      SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
4,769,014✔
289
      if (pRsp->numOfRows == 0) {
4,769,014✔
290
        if (NULL != pDataInfo->pSrcUidList && (!pDataInfo->isVtbRefScan)) {
1,679,980!
291
          pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
×
292
          code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
×
293
          if (code != TSDB_CODE_SUCCESS) {
×
294
            taosMemoryFreeClear(pDataInfo->pRsp);
×
295
            TAOS_CHECK_EXIT(code);
×
296
          }
297
        } else {
298
          pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
1,679,980✔
299
          qDebug("exhausted %p,%s vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
1,679,980✔
300
                 " execId:%d index:%d completed, rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 ", try next %d/%" PRIzu, pDataInfo,
301
                 GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId, i,
302
                 pDataInfo->totalRows, pExchangeInfo->loadInfo.totalRows, i + 1, totalSources);
303
          taosMemoryFreeClear(pDataInfo->pRsp);
1,679,970!
304
        }
305
        break;
1,680,102✔
306
      }
307

308
      TAOS_CHECK_EXIT(doExtractResultBlocks(pExchangeInfo, pDataInfo));
3,089,034!
309

310
      SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
3,089,696✔
311
      updateLoadRemoteInfo(pLoadInfo, pRetrieveRsp->numOfRows, pRetrieveRsp->compLen, pDataInfo->startTime, pOperator);
3,089,696✔
312
      pDataInfo->totalRows += pRetrieveRsp->numOfRows;
3,089,938✔
313

314
      if (pRsp->completed == 1) {
3,089,938✔
315
        pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
2,776,814✔
316
        qDebug("exhausted %p,%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
2,776,814✔
317
               " execId:%d index:%d completed, blocks:%d, numOfRows:%" PRId64 ", rowsOfSource:%" PRIu64
318
               ", totalRows:%" PRIu64 ", total:%.2f Kb, try next %d/%" PRIzu, pDataInfo,
319
               GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId, i,
320
               pRsp->numOfBlocks, pRsp->numOfRows, pDataInfo->totalRows, pLoadInfo->totalRows,
321
               pLoadInfo->totalSize / 1024.0, i + 1, totalSources);
322
      } else {
323
        qDebug("%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
313,124✔
324
               " execId:%d blocks:%d, numOfRows:%" PRId64 ", totalRows:%" PRIu64 ", total:%.2f Kb",
325
               GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
326
               pRsp->numOfBlocks, pRsp->numOfRows, pLoadInfo->totalRows, pLoadInfo->totalSize / 1024.0);
327
      }
328

329
      taosMemoryFreeClear(pDataInfo->pRsp);
3,089,864!
330

331
      if ((pDataInfo->status != EX_SOURCE_DATA_EXHAUSTED || NULL != pDataInfo->pSrcUidList) && !pDataInfo->isVtbRefScan && !pDataInfo->isVtbTagScan) {
3,090,381!
332
        pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
313,128✔
333
        code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
313,128✔
334
        if (code != TSDB_CODE_SUCCESS) {
313,128!
335
          taosMemoryFreeClear(pDataInfo->pRsp);
×
336
          TAOS_CHECK_EXIT(code);
×
337
        }
338
      }
339
      
340
      return;
4,572,117✔
341
    }  // end loop
342

343
    int32_t complete1 = 0;
1,703,274✔
344
    code = getCompletedSources(pExchangeInfo->pSourceDataInfo, &complete1);
1,703,274✔
345
    if (code != TSDB_CODE_SUCCESS) {
1,679,916!
346
      pTaskInfo->code = code;
×
347
      T_LONG_JMP(pTaskInfo->env, code);
×
348
    }
349
    if (complete1 == totalSources) {
1,679,964✔
350
      qDebug("all sources are completed, %s", GET_TASKID(pTaskInfo));
1,481,734✔
351
      return;
1,481,736✔
352
    }
353
  }
354

355
_exit:
15✔
356

357
  if (code) {
15!
358
    pTaskInfo->code = code;
15✔
359
    qError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
15!
360
  }
361
}
362

363
static SSDataBlock* doLoadRemoteDataImpl(SOperatorInfo* pOperator) {
13,656,173✔
364
  int32_t        code = TSDB_CODE_SUCCESS;
13,656,173✔
365
  SExchangeInfo* pExchangeInfo = pOperator->info;
13,656,173✔
366
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
13,656,173✔
367

368
  size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources);
13,656,173✔
369

370
  SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
13,655,651✔
371
  if (pOperator->status == OP_EXEC_DONE) {
13,655,651!
372
    qDebug("%s all %" PRIzu " source(s) are exhausted, total rows:%" PRIu64 " bytes:%" PRIu64 ", elapsed:%.2f ms",
×
373
           GET_TASKID(pTaskInfo), totalSources, pLoadInfo->totalRows, pLoadInfo->totalSize,
374
           pLoadInfo->totalElapsed / 1000.0);
375
    return NULL;
×
376
  }
377

378
  // we have buffered retrieved datablock, return it directly
379
  SSDataBlock* p = NULL;
13,655,651✔
380
  if (taosArrayGetSize(pExchangeInfo->pResultBlockList) > 0) {
13,655,651✔
381
    p = taosArrayGetP(pExchangeInfo->pResultBlockList, 0);
7,399,549✔
382
    taosArrayRemove(pExchangeInfo->pResultBlockList, 0);
7,399,173✔
383
  }
384

385
  if (p != NULL) {
13,655,633✔
386
    void* tmp = taosArrayPush(pExchangeInfo->pRecycledBlocks, &p);
7,399,258✔
387
    if (!tmp) {
7,399,404!
388
      code = terrno;
×
389
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
390
      pTaskInfo->code = code;
×
391
      T_LONG_JMP(pTaskInfo->env, code);
×
392
    }
393
    return p;
7,399,404✔
394
  } else {
395
    if (pExchangeInfo->seqLoadData) {
6,256,375✔
396
      code = seqLoadRemoteData(pOperator);
49,519✔
397
      if (code != TSDB_CODE_SUCCESS) {
49,516!
398
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
399
        pTaskInfo->code = code;
×
400
        T_LONG_JMP(pTaskInfo->env, code);
×
401
      }
402
    } else if (IS_STREAM_MODE(pOperator->pTaskInfo))   {
6,206,856✔
403
      streamConcurrentlyLoadRemoteData(pOperator, pExchangeInfo, pTaskInfo);
29,488✔
404
    } else {
405
      concurrentlyLoadRemoteDataImpl(pOperator, pExchangeInfo, pTaskInfo);
6,177,368✔
406
    }
407
    if (TSDB_CODE_SUCCESS != pOperator->pTaskInfo->code) {
6,256,471✔
408
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(pOperator->pTaskInfo->code));
15!
409
      T_LONG_JMP(pTaskInfo->env, pOperator->pTaskInfo->code);
15!
410
    }
411
    
412
    if (taosArrayGetSize(pExchangeInfo->pResultBlockList) == 0) {
6,256,456✔
413
      qDebug("empty resultBlockList");
3,118,699✔
414
      return NULL;
3,118,701✔
415
    } else {
416
      p = taosArrayGetP(pExchangeInfo->pResultBlockList, 0);
3,137,508✔
417
      taosArrayRemove(pExchangeInfo->pResultBlockList, 0);
3,135,905✔
418
      void* tmp = taosArrayPush(pExchangeInfo->pRecycledBlocks, &p);
3,135,783✔
419
      if (!tmp) {
3,136,973!
420
        code = terrno;
×
421
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
422
        pTaskInfo->code = code;
×
423
        T_LONG_JMP(pTaskInfo->env, code);
×
424
      }
425

426
      qDebug("block with rows:%" PRId64 " loaded", p->info.rows);
3,136,973✔
427
      return p;
3,137,187✔
428
    }
429
}
430
}
431

432
static int32_t loadRemoteDataNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
13,652,370✔
433
  int32_t        code = TSDB_CODE_SUCCESS;
13,652,370✔
434
  int32_t        lino = 0;
13,652,370✔
435
  SExchangeInfo* pExchangeInfo = pOperator->info;
13,652,370✔
436
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
13,652,370✔
437

438
  qDebug("%s start to load from exchange %p", pTaskInfo->id.str, pExchangeInfo);
13,652,370✔
439

440
  code = pOperator->fpSet._openFn(pOperator);
13,652,374✔
441
  QUERY_CHECK_CODE(code, lino, _end);
13,652,417!
442

443
  if (pOperator->status == OP_EXEC_DONE) {
13,652,417✔
444
    (*ppRes) = NULL;
51✔
445
    return code;
51✔
446
  }
447

448
  while (1) {
3,847✔
449
    SSDataBlock* pBlock = doLoadRemoteDataImpl(pOperator);
13,656,213✔
450
    if (pBlock == NULL) {
13,653,834✔
451
      (*ppRes) = NULL;
3,118,783✔
452
      return code;
3,118,783✔
453
    }
454

455
    code = doFilter(pBlock, pOperator->exprSupp.pFilterInfo, NULL, NULL);
10,535,051✔
456
    QUERY_CHECK_CODE(code, lino, _end);
10,536,475!
457

458
    if (blockDataGetNumOfRows(pBlock) == 0) {
10,536,475✔
459
      qDebug("rows 0 block got, continue next load");
2!
460
      continue;
2✔
461
    }
462

463
    SLimitInfo* pLimitInfo = &pExchangeInfo->limitInfo;
10,536,554✔
464
    if (hasLimitOffsetInfo(pLimitInfo)) {
10,536,554✔
465
      int32_t status = handleLimitOffset(pOperator, pLimitInfo, pBlock, false);
8,190✔
466
      if (status == PROJECT_RETRIEVE_CONTINUE) {
8,190✔
467
        qDebug("limit retrieve continue");
3,845✔
468
        continue;
3,845✔
469
      } else if (status == PROJECT_RETRIEVE_DONE) {
4,345!
470
        if (pBlock->info.rows == 0) {
4,345!
471
          setOperatorCompleted(pOperator);
×
472
          (*ppRes) = NULL;
×
473
          return code;
×
474
        } else {
475
          (*ppRes) = pBlock;
4,345✔
476
          return code;
4,345✔
477
        }
478
      }
479
    } else {
480
      (*ppRes) = pBlock;
10,528,503✔
481
      qDebug("block with rows %" PRId64 " returned in exechange", pBlock->info.rows);
10,528,503✔
482
      return code;
10,528,479✔
483
    }
484
  }
485

486
_end:
×
487

488
  if (code != TSDB_CODE_SUCCESS) {
×
489
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
490
    pTaskInfo->code = code;
×
491
    T_LONG_JMP(pTaskInfo->env, code);
×
492
  } else {
493
    qDebug("empty block returned in exchange");
×
494
  }
495
  
496
  (*ppRes) = NULL;
×
497
  return code;
×
498
}
499

500
static int32_t initDataSource(int32_t numOfSources, SExchangeInfo* pInfo, const char* id) {
3,113,706✔
501
  pInfo->pSourceDataInfo = taosArrayInit(numOfSources, sizeof(SSourceDataInfo));
3,113,706✔
502
  if (pInfo->pSourceDataInfo == NULL) {
3,113,535!
503
    return terrno;
×
504
  }
505

506
  if (pInfo->dynamicOp) {
3,113,539✔
507
    return TSDB_CODE_SUCCESS;
7,048✔
508
  }
509

510
  int32_t len = strlen(id) + 1;
3,106,491✔
511
  pInfo->pTaskId = taosMemoryCalloc(1, len);
3,106,491!
512
  if (!pInfo->pTaskId) {
3,106,654!
513
    return terrno;
×
514
  }
515
  tstrncpy(pInfo->pTaskId, id, len);
3,106,654✔
516
  for (int32_t i = 0; i < numOfSources; ++i) {
7,568,384✔
517
    SSourceDataInfo dataInfo = {0};
4,461,796✔
518
    dataInfo.status = EX_SOURCE_DATA_NOT_READY;
4,461,796✔
519
    dataInfo.taskId = pInfo->pTaskId;
4,461,796✔
520
    dataInfo.index = i;
4,461,796✔
521
    SSourceDataInfo* pDs = taosArrayPush(pInfo->pSourceDataInfo, &dataInfo);
4,461,796✔
522
    if (pDs == NULL) {
4,461,730!
523
      taosArrayDestroyEx(pInfo->pSourceDataInfo, freeSourceDataInfo);
×
524
      return terrno;
×
525
    }
526
    qDebug("init source data info %d, pDs:%p, status:%d", i, pDs, pDs->status);
4,461,730✔
527
  }
528

529
  return TSDB_CODE_SUCCESS;
3,106,588✔
530
}
531

532
static int32_t initExchangeOperator(SExchangePhysiNode* pExNode, SExchangeInfo* pInfo, const char* id) {
3,113,622✔
533
  size_t numOfSources = LIST_LENGTH(pExNode->pSrcEndPoints);
3,113,622!
534

535
  if (numOfSources == 0) {
3,113,622!
536
    qError("%s invalid number: %d of sources in exchange operator", id, (int32_t)numOfSources);
×
537
    return TSDB_CODE_INVALID_PARA;
×
538
  }
539
  pInfo->pFetchRpcHandles = taosArrayInit(numOfSources, sizeof(int64_t));
3,113,622✔
540
  if (!pInfo->pFetchRpcHandles) {
3,113,720!
541
    return terrno;
×
542
  }
543
  void* ret = taosArrayReserve(pInfo->pFetchRpcHandles, numOfSources);
3,113,720✔
544
  if (!ret) {
3,113,649!
545
    return terrno;
×
546
  }
547

548
  pInfo->pSources = taosArrayInit(numOfSources, sizeof(SDownstreamSourceNode));
3,113,649✔
549
  if (pInfo->pSources == NULL) {
3,113,480!
550
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
551
    return terrno;
×
552
  }
553

554
  if (pExNode->node.dynamicOp) {
3,113,480✔
555
    pInfo->pHashSources = tSimpleHashInit(numOfSources * 2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
7,048✔
556
    if (NULL == pInfo->pHashSources) {
7,062!
557
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
558
      return terrno;
×
559
    }
560
  }
561

562
  for (int32_t i = 0; i < numOfSources; ++i) {
7,592,674✔
563
    SDownstreamSourceNode* pNode = (SDownstreamSourceNode*)nodesListGetNode((SNodeList*)pExNode->pSrcEndPoints, i);
4,479,019✔
564
    if (!pNode) {
4,479,140!
565
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
566
      return terrno;
×
567
    }
568
    void* tmp = taosArrayPush(pInfo->pSources, pNode);
4,479,140✔
569
    if (!tmp) {
4,479,056!
570
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
571
      return terrno;
×
572
    }
573
    SExchangeSrcIndex idx = {.srcIdx = i, .inUseIdx = -1};
4,479,056✔
574
    int32_t           code =
575
        tSimpleHashPut(pInfo->pHashSources, &pNode->addr.nodeId, sizeof(pNode->addr.nodeId), &idx, sizeof(idx));
4,479,056✔
576
    if (pInfo->pHashSources && code != TSDB_CODE_SUCCESS) {
4,479,180!
577
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
578
      return code;
×
579
    }
580
  }
581

582
  initLimitInfo(pExNode->node.pLimit, pExNode->node.pSlimit, &pInfo->limitInfo);
3,113,655✔
583
  int64_t refId = taosAddRef(exchangeObjRefPool, pInfo);
3,113,663✔
584
  if (refId < 0) {
3,113,727!
585
    int32_t code = terrno;
×
586
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
587
    return code;
×
588
  } else {
589
    pInfo->self = refId;
3,113,727✔
590
  }
591

592
  return initDataSource(numOfSources, pInfo, id);
3,113,727✔
593
}
594

595
int32_t resetExchangeOperState(SOperatorInfo* pOper) {
44,494✔
596
  SExchangeInfo* pInfo = pOper->info;
44,494✔
597
  SExchangePhysiNode* pPhynode = (SExchangePhysiNode*)pOper->pPhyNode;
44,494✔
598

599
  qDebug("%s reset exchange op:%p info:%p", pOper->pTaskInfo->id.str, pOper, pInfo);
44,494✔
600

601
  (void)atomic_add_fetch_64(&pInfo->seqId, 1);
44,494✔
602
  pOper->status = OP_NOT_OPENED;
44,498✔
603
  pInfo->current = 0;
44,498✔
604
  pInfo->loadInfo.totalElapsed = 0;
44,498✔
605
  pInfo->loadInfo.totalRows = 0;
44,498✔
606
  pInfo->loadInfo.totalSize = 0;
44,498✔
607
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pSourceDataInfo); ++i) {
146,137✔
608
    SSourceDataInfo* pDataInfo = taosArrayGet(pInfo->pSourceDataInfo, i);
101,639✔
609
    taosWLockLatch(&pDataInfo->lock);
101,639✔
610
    taosMemoryFreeClear(pDataInfo->decompBuf);
101,639!
611
    taosMemoryFreeClear(pDataInfo->pRsp);
101,639!
612

613
    pDataInfo->totalRows = 0;
101,639✔
614
    pDataInfo->code = 0;
101,639✔
615
    pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
101,639✔
616
    pDataInfo->fetchSent = false;
101,639✔
617
    taosWUnLockLatch(&pDataInfo->lock);
101,639✔
618
  }
619

620
  if (pInfo->dynamicOp) {
44,498✔
621
    taosArrayClearEx(pInfo->pSourceDataInfo, freeSourceDataInfo);
12,218✔
622
  } 
623

624
  taosArrayClearEx(pInfo->pResultBlockList, freeBlock);
44,498✔
625
  taosArrayClearEx(pInfo->pRecycledBlocks, freeBlock);
44,498✔
626

627
  blockDataCleanup(pInfo->pDummyBlock);
44,497✔
628

629
  void   *data = NULL;
44,498✔
630
  int32_t iter = 0;
44,498✔
631
  while ((data = tSimpleHashIterate(pInfo->pHashSources, data, &iter))) {
64,066✔
632
    ((SExchangeSrcIndex *)data)->inUseIdx = -1;
19,568✔
633
  }
634
  
635
  pInfo->limitInfo = (SLimitInfo){0};
44,497✔
636
  initLimitInfo(pPhynode->node.pLimit, pPhynode->node.pSlimit, &pInfo->limitInfo);
44,497✔
637

638
  return 0;
44,498✔
639
}
640

641
int32_t createExchangeOperatorInfo(void* pTransporter, SExchangePhysiNode* pExNode, SExecTaskInfo* pTaskInfo,
3,113,688✔
642
                                   SOperatorInfo** pOptrInfo) {
643
  QRY_PARAM_CHECK(pOptrInfo);
3,113,688!
644

645
  int32_t        code = 0;
3,113,688✔
646
  int32_t        lino = 0;
3,113,688✔
647
  SExchangeInfo* pInfo = taosMemoryCalloc(1, sizeof(SExchangeInfo));
3,113,688!
648
  SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
3,113,627!
649
  if (pInfo == NULL || pOperator == NULL) {
3,113,614!
650
    code = terrno;
×
651
    goto _error;
×
652
  }
653

654
  pOperator->pPhyNode = pExNode;
3,113,615✔
655
  pInfo->dynamicOp = pExNode->node.dynamicOp;
3,113,615✔
656
  code = initExchangeOperator(pExNode, pInfo, GET_TASKID(pTaskInfo));
3,113,615✔
657
  QUERY_CHECK_CODE(code, lino, _error);
3,113,588!
658

659
  code = tsem_init(&pInfo->ready, 0, 0);
3,113,588✔
660
  QUERY_CHECK_CODE(code, lino, _error);
3,113,578!
661

662
  pInfo->pDummyBlock = createDataBlockFromDescNode(pExNode->node.pOutputDataBlockDesc);
3,113,578✔
663
  QUERY_CHECK_NULL(pInfo->pDummyBlock, code, lino, _error, terrno);
3,113,694!
664

665
  pInfo->pResultBlockList = taosArrayInit(64, POINTER_BYTES);
3,113,694✔
666
  QUERY_CHECK_NULL(pInfo->pResultBlockList, code, lino, _error, terrno);
3,113,589!
667
  pInfo->pRecycledBlocks = taosArrayInit(64, POINTER_BYTES);
3,113,589✔
668
  QUERY_CHECK_NULL(pInfo->pRecycledBlocks, code, lino, _error, terrno);
3,113,566!
669

670
  SExchangeOpStopInfo stopInfo = {QUERY_NODE_PHYSICAL_PLAN_EXCHANGE, pInfo->self};
3,113,566✔
671
  code = qAppendTaskStopInfo(pTaskInfo, &stopInfo);
3,113,566✔
672
  QUERY_CHECK_CODE(code, lino, _error);
3,113,738!
673

674
  pInfo->seqLoadData = pExNode->seqRecvData;
3,113,738✔
675
  pInfo->dynTbname = pExNode->dynTbname;
3,113,738✔
676
  if (pInfo->dynTbname) {
3,113,738✔
677
    pInfo->seqLoadData = true;
33✔
678
  }
679
  pInfo->pTransporter = pTransporter;
3,113,738✔
680

681
  setOperatorInfo(pOperator, "ExchangeOperator", QUERY_NODE_PHYSICAL_PLAN_EXCHANGE, false, OP_NOT_OPENED, pInfo,
3,113,738✔
682
                  pTaskInfo);
683
  pOperator->exprSupp.numOfExprs = taosArrayGetSize(pInfo->pDummyBlock->pDataBlock);
3,113,710✔
684

685
  code = filterInitFromNode((SNode*)pExNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
3,113,650✔
686
                            pTaskInfo->pStreamRuntimeInfo);
3,113,650✔
687
  QUERY_CHECK_CODE(code, lino, _error);
3,113,633!
688
  qTrace("%s exchange op:%p", __func__, pOperator);
3,113,633!
689
  pOperator->fpSet = createOperatorFpSet(prepareLoadRemoteData, loadRemoteDataNext, NULL, destroyExchangeOperatorInfo,
3,113,633✔
690
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
691
  setOperatorResetStateFn(pOperator, resetExchangeOperState);
3,113,678✔
692
  *pOptrInfo = pOperator;
3,113,653✔
693
  return TSDB_CODE_SUCCESS;
3,113,653✔
694

695
_error:
×
696
  if (code != TSDB_CODE_SUCCESS) {
×
697
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
698
    pTaskInfo->code = code;
×
699
  }
700
  if (pInfo != NULL) {
×
701
    doDestroyExchangeOperatorInfo(pInfo);
×
702
  }
703

704
  if (pOperator != NULL) {
×
705
    pOperator->info = NULL;
×
706
    destroyOperator(pOperator);
×
707
  }
708
  return code;
×
709
}
710

711
void destroyExchangeOperatorInfo(void* param) {
3,113,712✔
712
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
3,113,712✔
713
  int32_t        code = taosRemoveRef(exchangeObjRefPool, pExInfo->self);
3,113,712✔
714
  if (code != TSDB_CODE_SUCCESS) {
3,113,775!
715
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
716
  }
717
}
3,113,775✔
718

719
void freeBlock(void* pParam) {
4,503,854✔
720
  SSDataBlock* pBlock = *(SSDataBlock**)pParam;
4,503,854✔
721
  blockDataDestroy(pBlock);
4,503,854✔
722
}
4,503,902✔
723

724
void freeSourceDataInfo(void* p) {
4,463,933✔
725
  SSourceDataInfo* pInfo = (SSourceDataInfo*)p;
4,463,933✔
726
  taosMemoryFreeClear(pInfo->decompBuf);
4,463,933!
727
  taosMemoryFreeClear(pInfo->pRsp);
4,463,933!
728

729
  pInfo->decompBufSize = 0;
4,463,933✔
730
}
4,463,933✔
731

732
void doDestroyExchangeOperatorInfo(void* param) {
3,113,752✔
733
  if (param == NULL) {
3,113,752!
734
    return;
×
735
  }
736
  SExchangeInfo* pExInfo = (SExchangeInfo*)param;
3,113,752✔
737
  if (pExInfo->pFetchRpcHandles) {
3,113,752!
738
    for (int32_t i = 0; i < pExInfo->pFetchRpcHandles->size; ++i) {
7,591,003✔
739
      int64_t* pRpcHandle = taosArrayGet(pExInfo->pFetchRpcHandles, i);
4,477,730✔
740
      if (*pRpcHandle > 0) {
4,477,214✔
741
        SDownstreamSourceNode* pSource = taosArrayGet(pExInfo->pSources, i);
8,267✔
742
        (void)asyncFreeConnById(pExInfo->pTransporter, *pRpcHandle);
8,267✔
743
      }
744
    }
745
    taosArrayDestroy(pExInfo->pFetchRpcHandles);
3,113,273✔
746
  }
747

748
  taosArrayDestroy(pExInfo->pSources);
3,113,640✔
749
  taosArrayDestroyEx(pExInfo->pSourceDataInfo, freeSourceDataInfo);
3,113,709✔
750

751
  taosArrayDestroyEx(pExInfo->pResultBlockList, freeBlock);
3,113,667✔
752
  taosArrayDestroyEx(pExInfo->pRecycledBlocks, freeBlock);
3,113,708✔
753

754
  blockDataDestroy(pExInfo->pDummyBlock);
3,113,722✔
755
  tSimpleHashCleanup(pExInfo->pHashSources);
3,113,751✔
756

757
  int32_t code = tsem_destroy(&pExInfo->ready);
3,113,738✔
758
  if (code != TSDB_CODE_SUCCESS) {
3,113,722!
759
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
760
  }
761
  taosMemoryFreeClear(pExInfo->pTaskId);
3,113,722!
762

763
  taosMemoryFreeClear(param);
3,113,720!
764
}
765

766
int32_t loadRemoteDataCallback(void* param, SDataBuf* pMsg, int32_t code) {
4,840,509✔
767
  SFetchRspHandleWrapper* pWrapper = (SFetchRspHandleWrapper*)param;
4,840,509✔
768

769
  taosMemoryFreeClear(pMsg->pEpSet);
4,840,509!
770
  SExchangeInfo* pExchangeInfo = taosAcquireRef(exchangeObjRefPool, pWrapper->exchangeId);
4,840,509✔
771
  if (pExchangeInfo == NULL) {
4,841,316✔
772
    qWarn("failed to acquire exchange operator, since it may have been released, %p", pExchangeInfo);
125!
773
    taosMemoryFree(pMsg->pData);
125!
774
    return TSDB_CODE_SUCCESS;
125✔
775
  }
776

777
  int64_t currSeqId = atomic_load_64(&pExchangeInfo->seqId);
4,841,191✔
778
  if (pWrapper->seqId != currSeqId) {
4,838,545!
779
    qDebug("rsp reqId %" PRId64 " mismatch with exchange %p curr seqId %" PRId64 ", ignore it", pWrapper->seqId, pExchangeInfo, currSeqId);
×
780
    taosMemoryFree(pMsg->pData);
×
781
    code = taosReleaseRef(exchangeObjRefPool, pWrapper->exchangeId);
×
782
    if (code != TSDB_CODE_SUCCESS) {
×
783
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
784
    }
785
    return TSDB_CODE_SUCCESS;
×
786
  }
787

788
  int32_t          index = pWrapper->sourceIndex;
4,838,545✔
789

790
  qDebug("%s exchange %p %dth source got rsp, code:%d, rsp:%p", pExchangeInfo->pTaskId, pExchangeInfo, index, code, pMsg->pData);
4,838,545✔
791

792
  int64_t* pRpcHandle = taosArrayGet(pExchangeInfo->pFetchRpcHandles, index);
4,838,543✔
793
  if (pRpcHandle != NULL) {
4,815,676!
794
    int32_t ret = asyncFreeConnById(pExchangeInfo->pTransporter, *pRpcHandle);
4,815,815✔
795
    if (ret != 0) {
4,842,833✔
796
      qDebug("failed to free rpc handle, code:%s, %p", tstrerror(ret), pExchangeInfo);
6,004!
797
    }
798
    *pRpcHandle = -1;
4,840,748✔
799
  }
800

801
  SSourceDataInfo* pSourceDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, index);
4,840,609✔
802
  if (!pSourceDataInfo) {
4,818,394!
803
    return terrno;
×
804
  }
805

806
  if (0 == code && NULL == pMsg->pData) {
4,818,394!
807
    qError("invalid rsp msg, msgType:%d, len:%d", pMsg->msgType, pMsg->len);
×
808
    code = TSDB_CODE_QRY_INVALID_MSG;
×
809
  }
810

811
  taosWLockLatch(&pSourceDataInfo->lock);
4,818,394✔
812
  if (code == TSDB_CODE_SUCCESS) {
4,839,276!
813
    pSourceDataInfo->seqId = pWrapper->seqId;
4,839,412✔
814
    pSourceDataInfo->pRsp = pMsg->pData;
4,839,412✔
815

816
    SRetrieveTableRsp* pRsp = pSourceDataInfo->pRsp;
4,839,412✔
817
    pRsp->numOfRows = htobe64(pRsp->numOfRows);
4,839,412✔
818
    pRsp->compLen = htonl(pRsp->compLen);
4,836,061✔
819
    pRsp->payloadLen = htonl(pRsp->payloadLen);
4,836,061✔
820
    pRsp->numOfCols = htonl(pRsp->numOfCols);
4,836,061✔
821
    pRsp->useconds = htobe64(pRsp->useconds);
4,836,061✔
822
    pRsp->numOfBlocks = htonl(pRsp->numOfBlocks);
4,839,288✔
823

824
    qDebug("%s fetch rsp received, index:%d, blocks:%d, rows:%" PRId64 ", %p", pSourceDataInfo->taskId, index,
4,839,288✔
825
           pRsp->numOfBlocks, pRsp->numOfRows, pExchangeInfo);
826
  } else {
827
    taosMemoryFree(pMsg->pData);
×
828
    pSourceDataInfo->code = rpcCvtErrCode(code);
54✔
829
    if (pSourceDataInfo->code != code) {
54!
830
      qError("%s fetch rsp received, index:%d, error:%s, cvted error: %s, %p", pSourceDataInfo->taskId, index,
×
831
             tstrerror(code), tstrerror(pSourceDataInfo->code), pExchangeInfo);
832
    } else {
833
      qError("%s fetch rsp received, index:%d, error:%s, %p", pSourceDataInfo->taskId, index, tstrerror(code),
54!
834
             pExchangeInfo);
835
    }
836
  }
837

838
  tmemory_barrier();
4,839,349✔
839
  pSourceDataInfo->status = EX_SOURCE_DATA_READY;
4,839,349✔
840
  taosWUnLockLatch(&pSourceDataInfo->lock);
4,839,349✔
841
  
842
  code = tsem_post(&pExchangeInfo->ready);
4,840,982✔
843
  if (code != TSDB_CODE_SUCCESS) {
4,841,270!
844
    qError("failed to invoke post when fetch rsp is ready, code:%s, %p", tstrerror(code), pExchangeInfo);
×
845
    return code;
×
846
  }
847

848
  code = taosReleaseRef(exchangeObjRefPool, pWrapper->exchangeId);
4,841,270✔
849
  if (code != TSDB_CODE_SUCCESS) {
4,842,799!
850
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
851
  }
852
  return code;
4,842,398✔
853
}
854

855
int32_t buildTableScanOperatorParam(SOperatorParam** ppRes, SArray* pUidList, int32_t srcOpType, bool tableSeq) {
667✔
856
  *ppRes = taosMemoryMalloc(sizeof(SOperatorParam));
667!
857
  if (NULL == *ppRes) {
667!
858
    return terrno;
×
859
  }
860

861
  STableScanOperatorParam* pScan = taosMemoryMalloc(sizeof(STableScanOperatorParam));
667!
862
  if (NULL == pScan) {
667!
863
    taosMemoryFreeClear(*ppRes);
×
864
    return terrno;
×
865
  }
866

867
  pScan->pUidList = taosArrayDup(pUidList, NULL);
667✔
868
  if (NULL == pScan->pUidList) {
667!
869
    taosMemoryFree(pScan);
×
870
    taosMemoryFreeClear(*ppRes);
×
871
    return terrno;
×
872
  }
873
  pScan->tableSeq = tableSeq;
667✔
874
  pScan->pOrgTbInfo = NULL;
667✔
875
  pScan->window.skey = INT64_MAX;
667✔
876
  pScan->window.ekey = INT64_MIN;
667✔
877

878
  (*ppRes)->opType = srcOpType;
667✔
879
  (*ppRes)->downstreamIdx = 0;
667✔
880
  (*ppRes)->value = pScan;
667✔
881
  (*ppRes)->pChildren = NULL;
667✔
882
  (*ppRes)->reUse = false;
667✔
883

884
  return TSDB_CODE_SUCCESS;
667✔
885
}
886

887
int32_t buildTableScanOperatorParamEx(SOperatorParam** ppRes, SArray* pUidList, int32_t srcOpType, SOrgTbInfo *pMap, bool tableSeq, STimeWindow *window) {
45,370✔
888
  int32_t                  code = TSDB_CODE_SUCCESS;
45,370✔
889
  int32_t                  lino = 0;
45,370✔
890
  STableScanOperatorParam* pScan = NULL;
45,370✔
891

892
  *ppRes = taosMemoryMalloc(sizeof(SOperatorParam));
45,370!
893
  QUERY_CHECK_NULL(*ppRes, code, lino, _return, terrno);
45,370!
894

895
  pScan = taosMemoryMalloc(sizeof(STableScanOperatorParam));
45,370!
896
  QUERY_CHECK_NULL(pScan, code, lino, _return, terrno);
45,370!
897

898
  pScan->pUidList = taosArrayDup(pUidList, NULL);
45,370✔
899
  QUERY_CHECK_NULL(pScan->pUidList, code, lino, _return, terrno);
45,370!
900

901
  pScan->pOrgTbInfo = taosMemoryMalloc(sizeof(SOrgTbInfo));
45,370!
902
  QUERY_CHECK_NULL(pScan->pOrgTbInfo, code, lino, _return, terrno);
45,370!
903

904
  pScan->pOrgTbInfo->vgId = pMap->vgId;
45,370✔
905
  tstrncpy(pScan->pOrgTbInfo->tbName, pMap->tbName, TSDB_TABLE_FNAME_LEN);
45,370✔
906

907
  pScan->pOrgTbInfo->colMap = taosArrayDup(pMap->colMap, NULL);
45,370✔
908
  QUERY_CHECK_NULL(pScan->pOrgTbInfo->colMap, code, lino, _return, terrno);
45,370!
909

910
  pScan->tableSeq = tableSeq;
45,370✔
911
  pScan->window.skey = window->skey;
45,370✔
912
  pScan->window.ekey = window->ekey;
45,370✔
913

914
  (*ppRes)->opType = srcOpType;
45,370✔
915
  (*ppRes)->downstreamIdx = 0;
45,370✔
916
  (*ppRes)->value = pScan;
45,370✔
917
  (*ppRes)->pChildren = NULL;
45,370✔
918
  (*ppRes)->reUse = false;
45,370✔
919

920
  return code;
45,370✔
921
_return:
×
922
  qError("%s failed at %d, failed to build scan operator msg:%s", __FUNCTION__, lino, tstrerror(code));
×
923
  taosMemoryFreeClear(*ppRes);
×
924
  if (pScan) {
×
925
    taosArrayDestroy(pScan->pUidList);
×
926
    if (pScan->pOrgTbInfo) {
×
927
      taosArrayDestroy(pScan->pOrgTbInfo->colMap);
×
928
      taosMemoryFreeClear(pScan->pOrgTbInfo);
×
929
    }
930
    taosMemoryFree(pScan);
×
931
  }
932
  return code;
×
933
}
934

935
int32_t buildTagScanOperatorParam(SOperatorParam** ppRes, SArray* pUidList, int32_t srcOpType) {
3,499✔
936
  int32_t                  code = TSDB_CODE_SUCCESS;
3,499✔
937
  int32_t                  lino = 0;
3,499✔
938
  STagScanOperatorParam*   pScan = NULL;
3,499✔
939

940
  *ppRes = taosMemoryMalloc(sizeof(SOperatorParam));
3,499!
941
  QUERY_CHECK_NULL(*ppRes, code, lino, _return, terrno);
3,499!
942

943
  pScan = taosMemoryMalloc(sizeof(STagScanOperatorParam));
3,499!
944
  QUERY_CHECK_NULL(pScan, code, lino, _return, terrno);
3,499!
945
  pScan->vcUid = *(tb_uid_t*)taosArrayGet(pUidList, 0);
3,499✔
946

947
  (*ppRes)->opType = srcOpType;
3,499✔
948
  (*ppRes)->downstreamIdx = 0;
3,499✔
949
  (*ppRes)->value = pScan;
3,499✔
950
  (*ppRes)->pChildren = NULL;
3,499✔
951
  (*ppRes)->reUse = false;
3,499✔
952

953
  return code;
3,499✔
954
_return:
×
955
  qError("%s failed at %d, failed to build scan operator msg:%s", __FUNCTION__, lino, tstrerror(code));
×
956
  taosMemoryFreeClear(*ppRes);
×
957
  if (pScan) {
×
958
    taosMemoryFree(pScan);
×
959
  }
960
  return code;
×
961
}
962

963
static int32_t getCurrentWinCalcTimeRange(SStreamRuntimeFuncInfo* pRuntimeInfo, STimeWindow* pTimeRange) {
8,802✔
964
  if (!pRuntimeInfo || !pTimeRange) {
8,802!
965
    return TSDB_CODE_INTERNAL_ERROR;
×
966
  }
967

968
  SSTriggerCalcParam* pParam = taosArrayGet(pRuntimeInfo->pStreamPesudoFuncVals, pRuntimeInfo->curIdx);
8,802✔
969
  if (!pParam) {
8,802!
970
    return TSDB_CODE_INTERNAL_ERROR;
×
971
  }
972

973
  switch (pRuntimeInfo->triggerType) {
8,802✔
974
    case STREAM_TRIGGER_SLIDING:
7,085✔
975
      // Unable to distinguish whether there is an interval, all use wstart/wend
976
      // and the results are equal to those of prevTs/currentTs, using the same address of union.
977
      pTimeRange->skey = pParam->wstart;  // is equal to wstart
7,085✔
978
      pTimeRange->ekey = pParam->wend;    // is equal to wend
7,085✔
979
      break;
7,085✔
980
    case STREAM_TRIGGER_PERIOD:
8✔
981
      pTimeRange->skey = pParam->prevLocalTime;
8✔
982
      pTimeRange->ekey = pParam->triggerTime;
8✔
983
      break;
8✔
984
    default:
1,709✔
985
      pTimeRange->skey = pParam->wstart;
1,709✔
986
      pTimeRange->ekey = pParam->wend;
1,709✔
987
      break;
1,709✔
988
  }
989

990
  return TSDB_CODE_SUCCESS;
8,802✔
991
}
992

993
int32_t doSendFetchDataRequest(SExchangeInfo* pExchangeInfo, SExecTaskInfo* pTaskInfo, int32_t sourceIndex) {
4,844,122✔
994
  int32_t          code = TSDB_CODE_SUCCESS;
4,844,122✔
995
  int32_t          lino = 0;
4,844,122✔
996
  SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, sourceIndex);
4,844,122✔
997
  if (!pDataInfo) {
4,842,206!
998
    return terrno;
×
999
  }
1000

1001
  if (EX_SOURCE_DATA_NOT_READY != pDataInfo->status) {
4,842,262!
1002
    return TSDB_CODE_SUCCESS;
×
1003
  }
1004

1005
  pDataInfo->status = EX_SOURCE_DATA_STARTED;
4,842,262✔
1006
  SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pDataInfo->index);
4,842,262✔
1007
  if (!pSource) {
4,840,940!
1008
    return terrno;
×
1009
  }
1010

1011
  pDataInfo->startTime = taosGetTimestampUs();
4,843,632✔
1012
  size_t totalSources = taosArrayGetSize(pExchangeInfo->pSources);
4,843,632✔
1013

1014
  SFetchRspHandleWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SFetchRspHandleWrapper));
4,844,704!
1015
  QUERY_CHECK_NULL(pWrapper, code, lino, _end, terrno);
4,846,019!
1016
  pWrapper->exchangeId = pExchangeInfo->self;
4,846,019✔
1017
  pWrapper->sourceIndex = sourceIndex;
4,846,019✔
1018
  pWrapper->seqId = pExchangeInfo->seqId;
4,846,019✔
1019

1020
  if (pSource->localExec) {
4,846,019!
1021
    SDataBuf pBuf = {0};
×
1022
    int32_t  code = (*pTaskInfo->localFetch.fp)(pTaskInfo->localFetch.handle, pSource->sId, pTaskInfo->id.queryId,
×
1023
                                               pSource->clientId, pSource->taskId, 0, pSource->execId, &pBuf.pData,
1024
                                               pTaskInfo->localFetch.explainRes);
1025
    code = loadRemoteDataCallback(pWrapper, &pBuf, code);
×
1026
    taosMemoryFree(pWrapper);
×
1027
    QUERY_CHECK_CODE(code, lino, _end);
×
1028
  } else {
1029
    bool needStreamPesudoFuncVals = true;
4,846,019✔
1030
    SResFetchReq req = {0};
4,846,019✔
1031
    req.header.vgId = pSource->addr.nodeId;
4,846,019✔
1032
    req.sId = pSource->sId;
4,846,019✔
1033
    req.clientId = pSource->clientId;
4,846,019✔
1034
    req.taskId = pSource->taskId;
4,846,019✔
1035
    req.queryId = pTaskInfo->id.queryId;
4,846,019✔
1036
    req.execId = pSource->execId;
4,846,019✔
1037
    if (pTaskInfo->pStreamRuntimeInfo) {
4,846,019✔
1038
      req.dynTbname = pExchangeInfo->dynTbname;
22,413✔
1039
      req.execId = pTaskInfo->pStreamRuntimeInfo->execId;
22,413✔
1040
      req.pStRtFuncInfo = &pTaskInfo->pStreamRuntimeInfo->funcInfo;
22,413✔
1041

1042
      if (pSource->fetchMsgType == TDMT_STREAM_FETCH_FROM_RUNNER) {
22,413✔
1043
        qDebug("%s stream fetch from runner, execId:%d, %p", GET_TASKID(pTaskInfo), req.execId, pTaskInfo->pStreamRuntimeInfo);
80!
1044
      } else if (pSource->fetchMsgType == TDMT_STREAM_FETCH_FROM_CACHE) {
22,333✔
1045
        code = getCurrentWinCalcTimeRange(req.pStRtFuncInfo, &req.pStRtFuncInfo->curWindow);
8,802✔
1046
        QUERY_CHECK_CODE(code, lino, _end);
8,802!
1047
        needStreamPesudoFuncVals = false;
8,802✔
1048
        qDebug("%s stream fetch from cache, execId:%d, curWinIdx:%d, time range:[%" PRId64 ", %" PRId64 "]",
8,802✔
1049
               GET_TASKID(pTaskInfo), req.execId, req.pStRtFuncInfo->curIdx, req.pStRtFuncInfo->curWindow.skey,
1050
               req.pStRtFuncInfo->curWindow.ekey);
1051
      }
1052
      if (!pDataInfo->fetchSent) {
22,507✔
1053
        req.reset = pDataInfo->fetchSent = true;
20,256✔
1054
      }
1055
    }
1056
    if (pDataInfo->isVtbRefScan) {
4,846,113✔
1057
      code = buildTableScanOperatorParamEx(&req.pOpParam, pDataInfo->pSrcUidList, pDataInfo->srcOpType, pDataInfo->colMap, pDataInfo->tableSeq, &pDataInfo->window);
45,370✔
1058
      taosArrayDestroy(pDataInfo->colMap->colMap);
45,370✔
1059
      taosMemoryFreeClear(pDataInfo->colMap);
45,370!
1060
      taosArrayDestroy(pDataInfo->pSrcUidList);
45,370✔
1061
      pDataInfo->pSrcUidList = NULL;
45,369✔
1062
      if (TSDB_CODE_SUCCESS != code) {
45,369!
1063
        pTaskInfo->code = code;
×
1064
        taosMemoryFree(pWrapper);
×
1065
        return pTaskInfo->code;
×
1066
      }
1067
    } else if (pDataInfo->isVtbTagScan) {
4,800,743✔
1068
      code = buildTagScanOperatorParam(&req.pOpParam, pDataInfo->pSrcUidList, pDataInfo->srcOpType);
3,499✔
1069
      taosArrayDestroy(pDataInfo->pSrcUidList);
3,499✔
1070
      pDataInfo->pSrcUidList = NULL;
3,499✔
1071
      if (TSDB_CODE_SUCCESS != code) {
3,499!
1072
        pTaskInfo->code = code;
×
1073
        taosMemoryFree(pWrapper);
×
1074
        return pTaskInfo->code;
×
1075
      }
1076
    } else {
1077
      if (pDataInfo->pSrcUidList) {
4,797,244✔
1078
        code = buildTableScanOperatorParam(&req.pOpParam, pDataInfo->pSrcUidList, pDataInfo->srcOpType, pDataInfo->tableSeq);
636✔
1079
        taosArrayDestroy(pDataInfo->pSrcUidList);
636✔
1080
        pDataInfo->pSrcUidList = NULL;
636✔
1081
        if (TSDB_CODE_SUCCESS != code) {
636!
1082
          pTaskInfo->code = code;
×
1083
          taosMemoryFree(pWrapper);
×
1084
          return pTaskInfo->code;
×
1085
        }
1086
      }
1087
    }
1088

1089
    int32_t msgSize = tSerializeSResFetchReq(NULL, 0, &req, needStreamPesudoFuncVals);
4,846,112✔
1090
    if (msgSize < 0) {
4,845,733!
1091
      pTaskInfo->code = msgSize;
×
1092
      taosMemoryFree(pWrapper);
×
1093
      freeOperatorParam(req.pOpParam, OP_GET_PARAM);
×
1094
      return pTaskInfo->code;
×
1095
    }
1096

1097
    void* msg = taosMemoryCalloc(1, msgSize);
4,845,733!
1098
    if (NULL == msg) {
4,845,901!
1099
      pTaskInfo->code = terrno;
×
1100
      taosMemoryFree(pWrapper);
×
1101
      freeOperatorParam(req.pOpParam, OP_GET_PARAM);
×
1102
      return pTaskInfo->code;
×
1103
    }
1104

1105
    msgSize = tSerializeSResFetchReq(msg, msgSize, &req, needStreamPesudoFuncVals);
4,845,901✔
1106
    if (msgSize < 0) {
4,845,560!
1107
      pTaskInfo->code = msgSize;
×
1108
      taosMemoryFree(pWrapper);
×
1109
      taosMemoryFree(msg);
×
1110
      freeOperatorParam(req.pOpParam, OP_GET_PARAM);
×
1111
      return pTaskInfo->code;
×
1112
    }
1113

1114
    freeOperatorParam(req.pOpParam, OP_GET_PARAM);
4,845,560✔
1115

1116
    qDebug("%s build fetch msg and send to vgId:%d, ep:%s, clientId:0x%" PRIx64 " taskId:0x%" PRIx64
4,845,484✔
1117
           ", seqId:%" PRId64 ", execId:%d, %p, %d/%" PRIzu,
1118
           GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->addr.epSet.eps[0].fqdn, pSource->clientId,
1119
           pSource->taskId, pExchangeInfo->seqId, pSource->execId, pExchangeInfo, sourceIndex, totalSources);
1120

1121
    // send the fetch remote task result reques
1122
    SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo));
4,845,486!
1123
    if (NULL == pMsgSendInfo) {
4,845,576!
1124
      taosMemoryFreeClear(msg);
×
1125
      taosMemoryFree(pWrapper);
×
1126
      qError("%s prepare message %d failed", GET_TASKID(pTaskInfo), (int32_t)sizeof(SMsgSendInfo));
×
1127
      pTaskInfo->code = terrno;
×
1128
      return pTaskInfo->code;
×
1129
    }
1130

1131
    pMsgSendInfo->param = pWrapper;
4,845,576✔
1132
    pMsgSendInfo->paramFreeFp = taosAutoMemoryFree;
4,845,576✔
1133
    pMsgSendInfo->msgInfo.pData = msg;
4,845,576✔
1134
    pMsgSendInfo->msgInfo.len = msgSize;
4,845,576✔
1135
    pMsgSendInfo->msgType = pSource->fetchMsgType;
4,845,576✔
1136
    pMsgSendInfo->fp = loadRemoteDataCallback;
4,845,576✔
1137
    pMsgSendInfo->requestId = pTaskInfo->id.queryId;
4,845,576✔
1138

1139
    int64_t transporterId = 0;
4,845,576✔
1140
    void* poolHandle = NULL;
4,845,576✔
1141
    code = asyncSendMsgToServer(pExchangeInfo->pTransporter, &pSource->addr.epSet, &transporterId, pMsgSendInfo);
4,845,576✔
1142
    QUERY_CHECK_CODE(code, lino, _end);
4,846,110!
1143
    int64_t* pRpcHandle = taosArrayGet(pExchangeInfo->pFetchRpcHandles, sourceIndex);
4,846,110✔
1144
    *pRpcHandle = transporterId;
4,843,632✔
1145
  }
1146

1147
_end:
4,843,632✔
1148
  if (code != TSDB_CODE_SUCCESS) {
4,843,632!
1149
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1150
  }
1151
  return code;
4,843,633✔
1152
}
1153

1154
void updateLoadRemoteInfo(SLoadRemoteDataInfo* pInfo, int64_t numOfRows, int32_t dataLen, int64_t startTs,
3,293,712✔
1155
                          SOperatorInfo* pOperator) {
1156
  pInfo->totalRows += numOfRows;
3,293,712✔
1157
  pInfo->totalSize += dataLen;
3,293,712✔
1158
  pInfo->totalElapsed += (taosGetTimestampUs() - startTs);
3,293,863✔
1159
  pOperator->resultInfo.totalRows += numOfRows;
3,293,863✔
1160
}
3,293,863✔
1161

1162
int32_t extractDataBlockFromFetchRsp(SSDataBlock* pRes, char* pData, SArray* pColList, char** pNextStart) {
10,710,547✔
1163
  int32_t      code = TSDB_CODE_SUCCESS;
10,710,547✔
1164
  int32_t      lino = 0;
10,710,547✔
1165
  SSDataBlock* pBlock = NULL;
10,710,547✔
1166
  if (pColList == NULL) {  // data from other sources
10,710,547✔
1167
    blockDataCleanup(pRes);
10,554,833✔
1168
    code = blockDecode(pRes, pData, (const char**)pNextStart);
10,554,027✔
1169
    if (code) {
10,551,914!
1170
      return code;
×
1171
    }
1172
  } else {  // extract data according to pColList
1173
    char* pStart = pData;
155,714✔
1174

1175
    int32_t numOfCols = htonl(*(int32_t*)pStart);
155,714✔
1176
    pStart += sizeof(int32_t);
155,714✔
1177

1178
    // todo refactor:extract method
1179
    SSysTableSchema* pSchema = (SSysTableSchema*)pStart;
155,714✔
1180
    for (int32_t i = 0; i < numOfCols; ++i) {
2,592,739✔
1181
      SSysTableSchema* p = (SSysTableSchema*)pStart;
2,437,025✔
1182

1183
      p->colId = htons(p->colId);
2,437,025✔
1184
      p->bytes = htonl(p->bytes);
2,437,025✔
1185
      pStart += sizeof(SSysTableSchema);
2,437,025✔
1186
    }
1187

1188
    pBlock = NULL;
155,714✔
1189
    code = createDataBlock(&pBlock);
155,714✔
1190
    QUERY_CHECK_CODE(code, lino, _end);
156,073!
1191

1192
    for (int32_t i = 0; i < numOfCols; ++i) {
2,589,025✔
1193
      SColumnInfoData idata = createColumnInfoData(pSchema[i].type, pSchema[i].bytes, pSchema[i].colId);
2,433,056✔
1194
      code = blockDataAppendColInfo(pBlock, &idata);
2,437,803✔
1195
      QUERY_CHECK_CODE(code, lino, _end);
2,432,952!
1196
    }
1197

1198
    code = blockDecode(pBlock, pStart, NULL);
155,969✔
1199
    QUERY_CHECK_CODE(code, lino, _end);
156,017!
1200

1201
    code = blockDataEnsureCapacity(pRes, pBlock->info.rows);
156,017✔
1202
    QUERY_CHECK_CODE(code, lino, _end);
156,053!
1203

1204
    // data from mnode
1205
    pRes->info.dataLoad = 1;
156,053✔
1206
    pRes->info.rows = pBlock->info.rows;
156,053✔
1207
    pRes->info.scanFlag = MAIN_SCAN;
156,053✔
1208
    code = relocateColumnData(pRes, pColList, pBlock->pDataBlock, false);
156,053✔
1209
    QUERY_CHECK_CODE(code, lino, _end);
156,064!
1210

1211
    blockDataDestroy(pBlock);
156,064✔
1212
    pBlock = NULL;
156,095✔
1213
  }
1214

1215
_end:
10,708,009✔
1216
  if (code != TSDB_CODE_SUCCESS) {
10,708,009!
1217
    blockDataDestroy(pBlock);
×
1218
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1219
  }
1220
  return code;
10,708,556✔
1221
}
1222

1223
void setAllSourcesCompleted(SOperatorInfo* pOperator) {
1,637,036✔
1224
  SExchangeInfo* pExchangeInfo = pOperator->info;
1,637,036✔
1225
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
1,637,036✔
1226

1227
  SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
1,637,036✔
1228
  size_t               totalSources = taosArrayGetSize(pExchangeInfo->pSources);
1,637,036✔
1229
  qDebug("%s all %" PRIzu " sources are exhausted, total rows: %" PRIu64 ", %.2f Kb, elapsed:%.2f ms",
1,637,061✔
1230
         GET_TASKID(pTaskInfo), totalSources, pLoadInfo->totalRows, pLoadInfo->totalSize / 1024.0,
1231
         pLoadInfo->totalElapsed / 1000.0);
1232

1233
  setOperatorCompleted(pOperator);
1,637,061✔
1234
}
1,637,119✔
1235

1236
int32_t getCompletedSources(const SArray* pArray, int32_t* pRes) {
7,900,552✔
1237
  int32_t code = TSDB_CODE_SUCCESS;
7,900,552✔
1238
  int32_t lino = 0;
7,900,552✔
1239
  size_t  total = taosArrayGetSize(pArray);
7,900,552✔
1240

1241
  int32_t completed = 0;
7,900,011✔
1242
  for (int32_t k = 0; k < total; ++k) {
63,527,334✔
1243
    SSourceDataInfo* p = taosArrayGet(pArray, k);
55,646,037✔
1244
    QUERY_CHECK_NULL(p, code, lino, _end, terrno);
55,617,018!
1245
    if (p->status == EX_SOURCE_DATA_EXHAUSTED) {
55,626,842✔
1246
      qDebug("source %d is completed, info:%p %p", k, pArray, p);
27,869,185✔
1247
      completed += 1;
27,869,666✔
1248
    }
1249
  }
1250

1251
  *pRes = completed;
7,881,297✔
1252
_end:
7,881,297✔
1253
  if (code != TSDB_CODE_SUCCESS) {
7,881,297!
1254
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1255
  }
1256
  return code;
7,897,547✔
1257
}
1258

1259
int32_t prepareConcurrentlyLoad(SOperatorInfo* pOperator) {
3,105,516✔
1260
  SExchangeInfo* pExchangeInfo = pOperator->info;
3,105,516✔
1261
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
3,105,516✔
1262

1263
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSourceDataInfo);
3,105,516✔
1264
  int64_t startTs = taosGetTimestampUs();
3,105,471✔
1265

1266
  // Asynchronously send all fetch requests to all sources.
1267
  for (int32_t i = 0; i < totalSources; ++i) {
7,564,409✔
1268
    int32_t code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, i);
4,459,379✔
1269
    if (code != TSDB_CODE_SUCCESS) {
4,458,938!
1270
      pTaskInfo->code = code;
×
1271
      return code;
×
1272
    }
1273
  }
1274

1275
  int64_t endTs = taosGetTimestampUs();
3,105,350✔
1276
  qDebug("%s send all fetch requests to %" PRIzu " sources completed, elapsed:%.2fms", GET_TASKID(pTaskInfo),
3,105,350✔
1277
         totalSources, (endTs - startTs) / 1000.0);
1278

1279
  pOperator->status = OP_RES_TO_RETURN;
3,105,350✔
1280
  pOperator->cost.openCost = taosGetTimestampUs() - startTs;
3,105,356✔
1281
  if (isTaskKilled(pTaskInfo)) {
3,105,356!
1282
    T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
1283
  }
1284

1285
  return TSDB_CODE_SUCCESS;
3,105,484✔
1286
}
1287

1288
int32_t doExtractResultBlocks(SExchangeInfo* pExchangeInfo, SSourceDataInfo* pDataInfo) {
3,136,901✔
1289
  int32_t            code = TSDB_CODE_SUCCESS;
3,136,901✔
1290
  int32_t            lino = 0;
3,136,901✔
1291
  SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
3,136,901✔
1292
  SSDataBlock*       pb = NULL;
3,136,901✔
1293

1294
  char* pNextStart = pRetrieveRsp->data;
3,136,901✔
1295
  char* pStart = pNextStart;
3,136,901✔
1296

1297
  int32_t index = 0;
3,136,901✔
1298

1299
  if (pRetrieveRsp->compressed) {  // decompress the data
3,136,901!
1300
    if (pDataInfo->decompBuf == NULL) {
×
1301
      pDataInfo->decompBuf = taosMemoryMalloc(pRetrieveRsp->payloadLen);
×
1302
      QUERY_CHECK_NULL(pDataInfo->decompBuf, code, lino, _end, terrno);
×
1303
      pDataInfo->decompBufSize = pRetrieveRsp->payloadLen;
×
1304
    } else {
1305
      if (pDataInfo->decompBufSize < pRetrieveRsp->payloadLen) {
×
1306
        char* p = taosMemoryRealloc(pDataInfo->decompBuf, pRetrieveRsp->payloadLen);
×
1307
        QUERY_CHECK_NULL(p, code, lino, _end, terrno);
×
1308
        if (p != NULL) {
×
1309
          pDataInfo->decompBuf = p;
×
1310
          pDataInfo->decompBufSize = pRetrieveRsp->payloadLen;
×
1311
        }
1312
      }
1313
    }
1314
  }
1315

1316
  while (index++ < pRetrieveRsp->numOfBlocks) {
13,691,081✔
1317
    pStart = pNextStart;
10,553,228✔
1318

1319
    if (taosArrayGetSize(pExchangeInfo->pRecycledBlocks) > 0) {
10,553,228✔
1320
      pb = *(SSDataBlock**)taosArrayPop(pExchangeInfo->pRecycledBlocks);
6,050,922✔
1321
      blockDataCleanup(pb);
6,050,857✔
1322
    } else {
1323
      code = createOneDataBlock(pExchangeInfo->pDummyBlock, false, &pb);
4,503,228✔
1324
      QUERY_CHECK_NULL(pb, code, lino, _end, code);
4,503,737!
1325
    }
1326

1327
    int32_t compLen = *(int32_t*)pStart;
10,554,046✔
1328
    pStart += sizeof(int32_t);
10,554,046✔
1329

1330
    int32_t rawLen = *(int32_t*)pStart;
10,554,046✔
1331
    pStart += sizeof(int32_t);
10,554,046✔
1332
    QUERY_CHECK_CONDITION((compLen <= rawLen && compLen != 0), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
10,554,046!
1333

1334
    pNextStart = pStart + compLen;
10,554,046✔
1335
    if (pRetrieveRsp->compressed && (compLen < rawLen)) {
10,554,046!
1336
      int32_t t = tsDecompressString(pStart, compLen, 1, pDataInfo->decompBuf, rawLen, ONE_STAGE_COMP, NULL, 0);
×
1337
      QUERY_CHECK_CONDITION((t == rawLen), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
1338
      pStart = pDataInfo->decompBuf;
×
1339
    }
1340

1341
    code = extractDataBlockFromFetchRsp(pb, pStart, NULL, &pStart);
10,554,046✔
1342
    if (code != 0) {
10,553,015!
1343
      taosMemoryFreeClear(pDataInfo->pRsp);
×
1344
      goto _end;
×
1345
    }
1346

1347
    void* tmp = taosArrayPush(pExchangeInfo->pResultBlockList, &pb);
10,553,015✔
1348
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
10,554,110!
1349
    qDebug("%dth block added to resultBlockList, rows:%" PRId64, index, pb->info.rows);
10,554,110✔
1350
    pb = NULL;
10,554,180✔
1351
  }
1352

1353
_end:
3,137,853✔
1354
  if (code != TSDB_CODE_SUCCESS) {
3,137,853!
1355
    blockDataDestroy(pb);
×
1356
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1357
  }
1358
  return code;
3,137,561✔
1359
}
1360

1361
int32_t seqLoadRemoteData(SOperatorInfo* pOperator) {
49,519✔
1362
  SExchangeInfo* pExchangeInfo = pOperator->info;
49,519✔
1363
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
49,519✔
1364

1365
  int32_t code = 0;
49,519✔
1366
  size_t  totalSources = taosArrayGetSize(pExchangeInfo->pSources);
49,519✔
1367
  int64_t startTs = taosGetTimestampUs();
49,519✔
1368

1369
  int32_t vgId = 0;
49,519✔
1370
  if (pExchangeInfo->dynTbname) {
49,519✔
1371
    SArray* vals = pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamPartColVals;
220✔
1372
    for (int32_t i = 0; i < taosArrayGetSize(vals); ++i) {
220!
1373
      SStreamGroupValue* pValue = taosArrayGet(vals, i);
220✔
1374
      if (pValue != NULL && pValue->isTbname) {
220!
1375
        vgId = pValue->vgId;
220✔
1376
        break;
220✔
1377
      }
1378
    }
1379
  }
1380

1381
  while (1) {
11,716✔
1382
    if (pExchangeInfo->current >= totalSources) {
61,235✔
1383
      setAllSourcesCompleted(pOperator);
11,604✔
1384
      return TSDB_CODE_SUCCESS;
11,604✔
1385
    }
1386

1387
    SDownstreamSourceNode* pSource = taosArrayGet(pExchangeInfo->pSources, pExchangeInfo->current);
49,631✔
1388
    if (!pSource) {
49,631!
1389
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1390
      pTaskInfo->code = terrno;
×
1391
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
1392
    }
1393

1394
    if (vgId != 0 && pSource->addr.nodeId != vgId){
49,631✔
1395
      pExchangeInfo->current += 1;
172✔
1396
      continue;
172✔
1397
    }
1398

1399
    SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, pExchangeInfo->current);
49,459✔
1400
    if (!pDataInfo) {
49,459!
1401
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1402
      pTaskInfo->code = terrno;
×
1403
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
1404
    }
1405
    pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
49,459✔
1406

1407
    code = doSendFetchDataRequest(pExchangeInfo, pTaskInfo, pExchangeInfo->current);
49,459✔
1408
    if (code != TSDB_CODE_SUCCESS) {
49,459!
1409
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1410
      pTaskInfo->code = code;
×
1411
      T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
×
1412
    }
1413

1414
    while (true) {
3✔
1415
      code = exchangeWait(pOperator, pExchangeInfo);
49,462✔
1416
      if (code != TSDB_CODE_SUCCESS || isTaskKilled(pTaskInfo)) {
49,462!
1417
        T_LONG_JMP(pTaskInfo->env, pTaskInfo->code);
3!
1418
      }
1419

1420
      int64_t currSeqId = atomic_load_64(&pExchangeInfo->seqId);
49,459✔
1421
      if (pDataInfo->seqId != currSeqId) {
49,459✔
1422
        qDebug("seq rsp reqId %" PRId64 " mismatch with exchange %p curr seqId %" PRId64 ", ignore it", pDataInfo->seqId, pExchangeInfo, currSeqId);
3!
1423
        taosMemoryFreeClear(pDataInfo->pRsp);
3!
1424
        continue;
3✔
1425
      }
1426

1427
      break;
49,456✔
1428
    }
1429

1430
    if (pDataInfo->code != TSDB_CODE_SUCCESS) {
49,456!
1431
      qError("%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64 " execId:%d error happens, code:%s",
×
1432
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
1433
             tstrerror(pDataInfo->code));
1434
      pOperator->pTaskInfo->code = pDataInfo->code;
×
1435
      return pOperator->pTaskInfo->code;
×
1436
    }
1437

1438
    SRetrieveTableRsp*   pRsp = pDataInfo->pRsp;
49,456✔
1439
    SLoadRemoteDataInfo* pLoadInfo = &pExchangeInfo->loadInfo;
49,456✔
1440

1441
    if (pRsp->numOfRows == 0) {
49,456✔
1442
      qDebug("exhausted %p,%s vgId:%d, clientId:0x%" PRIx64 " taskID:0x%" PRIx64
11,544✔
1443
             " execId:%d %d of total completed, rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 " try next", pDataInfo,
1444
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
1445
             pExchangeInfo->current + 1, pDataInfo->totalRows, pLoadInfo->totalRows);
1446

1447
      pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
11,544✔
1448
      if (pDataInfo->isVtbRefScan || pDataInfo->isVtbTagScan) {
11,544!
1449
        pExchangeInfo->current = totalSources;
11,301✔
1450
      } else {
1451
        pExchangeInfo->current += 1;
243✔
1452
      }
1453
      taosMemoryFreeClear(pDataInfo->pRsp);
11,544!
1454
      continue;
11,544✔
1455
    }
1456

1457
    code = doExtractResultBlocks(pExchangeInfo, pDataInfo);
37,912✔
1458
    if (code != TSDB_CODE_SUCCESS) {
37,912!
1459
      goto _error;
×
1460
    }
1461

1462
    SRetrieveTableRsp* pRetrieveRsp = pDataInfo->pRsp;
37,912✔
1463
    if (pRsp->completed == 1) {
37,912✔
1464
      qDebug("exhausted %p,%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d numOfRows:%" PRId64
241!
1465
             ", rowsOfSource:%" PRIu64 ", totalRows:%" PRIu64 ", totalBytes:%" PRIu64 " try next %d/%" PRIzu, pDataInfo,
1466
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
1467
             pRetrieveRsp->numOfRows, pDataInfo->totalRows, pLoadInfo->totalRows, pLoadInfo->totalSize,
1468
             pExchangeInfo->current + 1, totalSources);
1469

1470
      pDataInfo->status = EX_SOURCE_DATA_EXHAUSTED;
241✔
1471
      if (pDataInfo->isVtbRefScan) {
241!
1472
        pExchangeInfo->current = totalSources;
×
1473
      } else {
1474
        pExchangeInfo->current += 1;
241✔
1475
      }
1476
    } else {
1477
      qDebug("%s fetch msg rsp from vgId:%d, clientId:0x%" PRIx64 " taskId:0x%" PRIx64 " execId:%d numOfRows:%" PRId64
37,671✔
1478
             ", totalRows:%" PRIu64 ", totalBytes:%" PRIu64,
1479
             GET_TASKID(pTaskInfo), pSource->addr.nodeId, pSource->clientId, pSource->taskId, pSource->execId,
1480
             pRetrieveRsp->numOfRows, pLoadInfo->totalRows, pLoadInfo->totalSize);
1481
    }
1482
    if (pExchangeInfo->dynamicOp && pExchangeInfo->seqLoadData) {
37,912!
1483
      taosArrayClear(pExchangeInfo->pSourceDataInfo);
37,466✔
1484
    }
1485
    updateLoadRemoteInfo(pLoadInfo, pRetrieveRsp->numOfRows, pRetrieveRsp->compLen, startTs, pOperator);
37,912✔
1486
    pDataInfo->totalRows += pRetrieveRsp->numOfRows;
37,912✔
1487

1488
    taosMemoryFreeClear(pDataInfo->pRsp);
37,912!
1489
    return TSDB_CODE_SUCCESS;
37,912✔
1490
  }
1491

1492
_error:
×
1493
  pTaskInfo->code = code;
×
1494
  return code;
×
1495
}
1496

1497
void clearVtbScanDataInfo(void* pItem) {
10,253✔
1498
  SSourceDataInfo *pInfo = (SSourceDataInfo *)pItem;
10,253✔
1499
  if (pInfo->colMap) {
10,253!
1500
    taosArrayDestroy(pInfo->colMap->colMap);
×
1501
    taosMemoryFreeClear(pInfo->colMap);
×
1502
  }
1503
  taosArrayDestroy(pInfo->pSrcUidList);
10,253✔
1504
}
10,253✔
1505

1506
int32_t addSingleExchangeSource(SOperatorInfo* pOperator, SExchangeOperatorBasicParam* pBasicParam) {
49,505✔
1507
  SExchangeInfo*     pExchangeInfo = pOperator->info;
49,505✔
1508
  SExchangeSrcIndex* pIdx = tSimpleHashGet(pExchangeInfo->pHashSources, &pBasicParam->vgId, sizeof(pBasicParam->vgId));
49,505✔
1509
  if (NULL == pIdx) {
49,505✔
1510
    if (pBasicParam->isNewDeployed) {
20!
1511
      SDownstreamSourceNode *pNode = NULL;
20✔
1512
      int32_t code = nodesCloneNode((SNode*)&pBasicParam->newDeployedSrc, (SNode**)&pNode);
20✔
1513
      if (code != TSDB_CODE_SUCCESS) {
20!
1514
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1515
        return code;
×
1516
      }
1517

1518
      void* tmp = taosArrayPush(pExchangeInfo->pSources, pNode);
20✔
1519
      if (!tmp) {
20!
1520
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1521
        return terrno;
×
1522
      }
1523
      SExchangeSrcIndex idx = {.srcIdx = taosArrayGetSize(pExchangeInfo->pSources) - 1, .inUseIdx = -1};
20✔
1524
      code =
1525
          tSimpleHashPut(pExchangeInfo->pHashSources, &pNode->addr.nodeId, sizeof(pNode->addr.nodeId), &idx, sizeof(idx));
20✔
1526
      if (pExchangeInfo->pHashSources && code != TSDB_CODE_SUCCESS) {
20!
1527
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1528
        return code;
×
1529
      }
1530
      pIdx = tSimpleHashGet(pExchangeInfo->pHashSources, &pBasicParam->vgId, sizeof(pBasicParam->vgId));
20✔
1531
      if (pIdx == NULL) {
20!
1532
        qError("No exchange source for vgId: %d", pBasicParam->vgId);
×
1533
        return TSDB_CODE_INVALID_PARA;
×
1534
      }
1535
    } else {
1536
      qError("No exchange source for vgId: %d", pBasicParam->vgId);
×
1537
      return TSDB_CODE_INVALID_PARA;
×
1538
    }
1539
  }
1540

1541
  qDebug("start to add single exchange source");
49,505✔
1542

1543
  if (pBasicParam->isVtbRefScan) {
49,505✔
1544
    SSourceDataInfo dataInfo = {0};
45,370✔
1545
    dataInfo.status = EX_SOURCE_DATA_NOT_READY;
45,370✔
1546
    dataInfo.taskId = pExchangeInfo->pTaskId;
45,370✔
1547
    dataInfo.index = pIdx->srcIdx;
45,370✔
1548
    dataInfo.window = pBasicParam->window;
45,370✔
1549
    dataInfo.colMap = taosMemoryMalloc(sizeof(SOrgTbInfo));
45,370!
1550
    dataInfo.colMap->vgId = pBasicParam->colMap->vgId;
45,370✔
1551
    tstrncpy(dataInfo.colMap->tbName, pBasicParam->colMap->tbName, TSDB_TABLE_FNAME_LEN);
45,370✔
1552
    dataInfo.colMap->colMap = taosArrayDup(pBasicParam->colMap->colMap, NULL);
45,370✔
1553

1554
    dataInfo.pSrcUidList = taosArrayDup(pBasicParam->uidList, NULL);
45,370✔
1555
    if (dataInfo.pSrcUidList == NULL) {
45,370!
1556
      return terrno;
×
1557
    }
1558

1559
    dataInfo.isVtbRefScan = pBasicParam->isVtbRefScan;
45,370✔
1560
    dataInfo.isVtbTagScan = pBasicParam->isVtbTagScan;
45,370✔
1561
    dataInfo.srcOpType = pBasicParam->srcOpType;
45,370✔
1562
    dataInfo.tableSeq = pBasicParam->tableSeq;
45,370✔
1563

1564
    taosArrayClearEx(pExchangeInfo->pSourceDataInfo, clearVtbScanDataInfo);
45,370✔
1565
    void* tmp = taosArrayPush(pExchangeInfo->pSourceDataInfo, &dataInfo);
45,370✔
1566
    if (!tmp) {
45,370!
1567
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1568
      return terrno;
×
1569
    }
1570
  } else if (pBasicParam->isVtbTagScan) {
4,135✔
1571
    SSourceDataInfo dataInfo = {0};
3,499✔
1572
    dataInfo.status = EX_SOURCE_DATA_NOT_READY;
3,499✔
1573
    dataInfo.taskId = pExchangeInfo->pTaskId;
3,499✔
1574
    dataInfo.index = pIdx->srcIdx;
3,499✔
1575
    dataInfo.window = pBasicParam->window;
3,499✔
1576
    dataInfo.colMap = NULL;
3,499✔
1577

1578
    dataInfo.pSrcUidList = taosArrayDup(pBasicParam->uidList, NULL);
3,499✔
1579
    if (dataInfo.pSrcUidList == NULL) {
3,499!
1580
      return terrno;
×
1581
    }
1582

1583
    dataInfo.isVtbRefScan = pBasicParam->isVtbRefScan;
3,499✔
1584
    dataInfo.isVtbTagScan = pBasicParam->isVtbTagScan;
3,499✔
1585
    dataInfo.srcOpType = pBasicParam->srcOpType;
3,499✔
1586
    dataInfo.tableSeq = pBasicParam->tableSeq;
3,499✔
1587

1588
    taosArrayClearEx(pExchangeInfo->pSourceDataInfo, clearVtbScanDataInfo);
3,499✔
1589
    void* tmp = taosArrayPush(pExchangeInfo->pSourceDataInfo, &dataInfo);
3,499✔
1590
    if (!tmp) {
3,499!
1591
      qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1592
      return terrno;
×
1593
    }
1594
  } else {
1595
    if (pIdx->inUseIdx < 0) {
636✔
1596
      SSourceDataInfo dataInfo = {0};
630✔
1597
      dataInfo.status = EX_SOURCE_DATA_NOT_READY;
630✔
1598
      dataInfo.taskId = pExchangeInfo->pTaskId;
630✔
1599
      dataInfo.index = pIdx->srcIdx;
630✔
1600
      if (pBasicParam->isVtbRefScan) {
630!
1601
        dataInfo.window = pBasicParam->window;
×
1602
        dataInfo.colMap = taosMemoryMalloc(sizeof(SOrgTbInfo));
×
1603
        dataInfo.colMap->vgId = pBasicParam->colMap->vgId;
×
1604
        tstrncpy(dataInfo.colMap->tbName, pBasicParam->colMap->tbName, TSDB_TABLE_FNAME_LEN);
×
1605
        dataInfo.colMap->colMap = taosArrayDup(pBasicParam->colMap->colMap, NULL);
×
1606
      }
1607

1608
      dataInfo.pSrcUidList = taosArrayDup(pBasicParam->uidList, NULL);
630✔
1609
      if (dataInfo.pSrcUidList == NULL) {
630!
1610
        return terrno;
×
1611
      }
1612

1613
      dataInfo.isVtbRefScan = pBasicParam->isVtbRefScan;
630✔
1614
      dataInfo.isVtbTagScan = pBasicParam->isVtbTagScan;
630✔
1615
      dataInfo.srcOpType = pBasicParam->srcOpType;
630✔
1616
      dataInfo.tableSeq = pBasicParam->tableSeq;
630✔
1617

1618
      void* tmp = taosArrayPush(pExchangeInfo->pSourceDataInfo, &dataInfo);
630✔
1619
      if (!tmp) {
630!
1620
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
1621
        return terrno;
×
1622
      }
1623
      pIdx->inUseIdx = taosArrayGetSize(pExchangeInfo->pSourceDataInfo) - 1;
630✔
1624
    } else {
1625
      SSourceDataInfo* pDataInfo = taosArrayGet(pExchangeInfo->pSourceDataInfo, pIdx->inUseIdx);
6✔
1626
      if (!pDataInfo) {
6!
1627
        return terrno;
×
1628
      }
1629
      if (pDataInfo->status == EX_SOURCE_DATA_EXHAUSTED) {
6!
1630
        pDataInfo->status = EX_SOURCE_DATA_NOT_READY;
6✔
1631
      }
1632

1633
      if (pBasicParam->isVtbRefScan) {
6!
1634
        pDataInfo->window = pBasicParam->window;
×
1635
        if (!pDataInfo->colMap) {
×
1636
          pDataInfo->colMap = taosMemoryMalloc(sizeof(SOrgTbInfo));
×
1637
        }
1638
        pDataInfo->colMap->vgId = pBasicParam->colMap->vgId;
×
1639
        tstrncpy(pDataInfo->colMap->tbName, pBasicParam->colMap->tbName, TSDB_TABLE_FNAME_LEN);
×
1640
        pDataInfo->colMap->colMap = taosArrayDup(pBasicParam->colMap->colMap, NULL);
×
1641
      }
1642

1643
      pDataInfo->pSrcUidList = taosArrayDup(pBasicParam->uidList, NULL);
6✔
1644
      if (pDataInfo->pSrcUidList == NULL) {
6!
1645
        return terrno;
×
1646
      }
1647

1648
      pDataInfo->isVtbRefScan = pBasicParam->isVtbRefScan;
6✔
1649
      pDataInfo->isVtbTagScan = pBasicParam->isVtbTagScan;
6✔
1650
      pDataInfo->srcOpType = pBasicParam->srcOpType;
6✔
1651
      pDataInfo->tableSeq = pBasicParam->tableSeq;
6✔
1652
    }
1653
  }
1654

1655
  return TSDB_CODE_SUCCESS;
49,505✔
1656
}
1657

1658
int32_t addDynamicExchangeSource(SOperatorInfo* pOperator) {
49,240✔
1659
  SExchangeInfo*               pExchangeInfo = pOperator->info;
49,240✔
1660
  int32_t                      code = TSDB_CODE_SUCCESS;
49,240✔
1661
  SExchangeOperatorBasicParam* pBasicParam = NULL;
49,240✔
1662
  SExchangeOperatorParam*      pParam = (SExchangeOperatorParam*)pOperator->pOperatorGetParam->value;
49,240✔
1663
  if (pParam->multiParams) {
49,240✔
1664
    SExchangeOperatorBatchParam* pBatch = (SExchangeOperatorBatchParam*)pOperator->pOperatorGetParam->value;
359✔
1665
    int32_t                      iter = 0;
359✔
1666
    while (NULL != (pBasicParam = tSimpleHashIterate(pBatch->pBatchs, pBasicParam, &iter))) {
983✔
1667
      code = addSingleExchangeSource(pOperator, pBasicParam);
624✔
1668
      if (code) {
624!
1669
        return code;
×
1670
      }
1671
    }
1672
  } else {
1673
    pBasicParam = &pParam->basic;
48,881✔
1674
    code = addSingleExchangeSource(pOperator, pBasicParam);
48,881✔
1675
  }
1676

1677
  freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
49,240✔
1678
  pOperator->pOperatorGetParam = NULL;
49,240✔
1679

1680
  return code;
49,240✔
1681
}
1682

1683
int32_t prepareLoadRemoteData(SOperatorInfo* pOperator) {
16,317,528✔
1684
  SExchangeInfo* pExchangeInfo = pOperator->info;
16,317,528✔
1685
  int32_t        code = TSDB_CODE_SUCCESS;
16,317,528✔
1686
  int32_t        lino = 0;
16,317,528✔
1687
  
1688
  if ((OPTR_IS_OPENED(pOperator) && !pExchangeInfo->dynamicOp) ||
16,317,528✔
1689
      (pExchangeInfo->dynamicOp && NULL == pOperator->pOperatorGetParam)) {
3,174,839✔
1690
    qDebug("skip prepare, opened:%d, dynamicOp:%d, getParam:%p", OPTR_IS_OPENED(pOperator), pExchangeInfo->dynamicOp, pOperator->pOperatorGetParam);
13,143,896✔
1691
    return TSDB_CODE_SUCCESS;
13,143,868✔
1692
  }
1693

1694
  if (pExchangeInfo->dynamicOp) {
3,173,632✔
1695
    code = addDynamicExchangeSource(pOperator);
49,240✔
1696
    QUERY_CHECK_CODE(code, lino, _end);
49,240!
1697
  }
1698

1699
  if (pOperator->status == OP_NOT_OPENED && (pExchangeInfo->dynamicOp && pExchangeInfo->seqLoadData) || IS_STREAM_MODE(pOperator->pTaskInfo)) {
3,173,632✔
1700
    pExchangeInfo->current = 0;
65,923✔
1701
  }
1702

1703
  int64_t st = taosGetTimestampUs();
3,174,198✔
1704

1705
  if (!IS_STREAM_MODE(pOperator->pTaskInfo) && !pExchangeInfo->seqLoadData) {
3,174,198✔
1706
    code = prepareConcurrentlyLoad(pOperator);
3,105,536✔
1707
    QUERY_CHECK_CODE(code, lino, _end);
3,105,474!
1708
    pExchangeInfo->openedTs = taosGetTimestampUs();
3,105,473✔
1709
  }
1710

1711
  OPTR_SET_OPENED(pOperator);
3,174,135✔
1712
  pOperator->cost.openCost = (taosGetTimestampUs() - st) / 1000.0;
3,174,141✔
1713

1714
  qDebug("%s prepare load complete", pOperator->pTaskInfo->id.str);
3,174,141✔
1715

1716
_end:
2,950,410✔
1717
  if (code != TSDB_CODE_SUCCESS) {
3,174,179!
1718
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1719
    pOperator->pTaskInfo->code = code;
×
1720
    T_LONG_JMP(pOperator->pTaskInfo->env, code);
×
1721
  }
1722
  return TSDB_CODE_SUCCESS;
3,174,179✔
1723
}
1724

1725
int32_t handleLimitOffset(SOperatorInfo* pOperator, SLimitInfo* pLimitInfo, SSDataBlock* pBlock, bool holdDataInBuf) {
8,190✔
1726
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
8,190✔
1727

1728
  if (pLimitInfo->remainGroupOffset > 0) {
8,190!
1729
    if (pLimitInfo->currentGroupId == 0) {  // it is the first group
×
1730
      pLimitInfo->currentGroupId = pBlock->info.id.groupId;
×
1731
      blockDataCleanup(pBlock);
×
1732
      return PROJECT_RETRIEVE_CONTINUE;
×
1733
    } else if (pLimitInfo->currentGroupId != pBlock->info.id.groupId) {
×
1734
      // now it is the data from a new group
1735
      pLimitInfo->remainGroupOffset -= 1;
×
1736

1737
      // ignore data block in current group
1738
      if (pLimitInfo->remainGroupOffset > 0) {
×
1739
        blockDataCleanup(pBlock);
×
1740
        return PROJECT_RETRIEVE_CONTINUE;
×
1741
      }
1742
    }
1743

1744
    // set current group id of the project operator
1745
    pLimitInfo->currentGroupId = pBlock->info.id.groupId;
×
1746
  }
1747

1748
  // here check for a new group data, we need to handle the data of the previous group.
1749
  if (pLimitInfo->currentGroupId != 0 && pLimitInfo->currentGroupId != pBlock->info.id.groupId) {
8,190✔
1750
    pLimitInfo->numOfOutputGroups += 1;
240✔
1751
    if ((pLimitInfo->slimit.limit > 0) && (pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
240!
1752
      pOperator->status = OP_EXEC_DONE;
×
1753
      blockDataCleanup(pBlock);
×
1754

1755
      return PROJECT_RETRIEVE_DONE;
×
1756
    }
1757

1758
    // reset the value for a new group data
1759
    resetLimitInfoForNextGroup(pLimitInfo);
240✔
1760
    // existing rows that belongs to previous group.
1761
    if (pBlock->info.rows > 0) {
240!
1762
      return PROJECT_RETRIEVE_DONE;
240✔
1763
    }
1764
  }
1765

1766
  // here we reach the start position, according to the limit/offset requirements.
1767

1768
  // set current group id
1769
  pLimitInfo->currentGroupId = pBlock->info.id.groupId;
7,950✔
1770

1771
  bool limitReached = applyLimitOffset(pLimitInfo, pBlock, pTaskInfo);
7,950✔
1772
  if (pBlock->info.rows == 0) {
7,950✔
1773
    return PROJECT_RETRIEVE_CONTINUE;
3,845✔
1774
  } else {
1775
    if (limitReached && (pLimitInfo->slimit.limit > 0 && pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
4,105!
1776
      setOperatorCompleted(pOperator);
×
1777
      return PROJECT_RETRIEVE_DONE;
×
1778
    }
1779
  }
1780

1781
  // todo optimize performance
1782
  // If there are slimit/soffset value exists, multi-round result can not be packed into one group, since the
1783
  // they may not belong to the same group the limit/offset value is not valid in this case.
1784
  if ((!holdDataInBuf) || (pBlock->info.rows >= pOperator->resultInfo.threshold) || hasSlimitOffsetInfo(pLimitInfo)) {
4,105!
1785
    return PROJECT_RETRIEVE_DONE;
4,105✔
1786
  } else {  // not full enough, continue to accumulate the output data in the buffer.
1787
    return PROJECT_RETRIEVE_CONTINUE;
×
1788
  }
1789
}
1790

1791
static int32_t exchangeWait(SOperatorInfo* pOperator, SExchangeInfo* pExchangeInfo) {
4,840,456✔
1792
  SExecTaskInfo* pTask = pOperator->pTaskInfo;
4,840,456✔
1793
  int32_t        code = TSDB_CODE_SUCCESS;
4,840,456✔
1794
  if (pTask->pWorkerCb) {
4,840,456!
1795
    code = pTask->pWorkerCb->beforeBlocking(pTask->pWorkerCb->pPool);
4,840,556✔
1796
    if (code != TSDB_CODE_SUCCESS) {
4,842,496!
1797
      pTask->code = code;
×
1798
      return pTask->code;
×
1799
    }
1800
  }
1801

1802
  code = tsem_wait(&pExchangeInfo->ready);
4,842,396✔
1803
  if (code != TSDB_CODE_SUCCESS) {
4,842,385!
1804
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
1805
    pTask->code = code;
×
1806
    return pTask->code;
×
1807
  }
1808

1809
  if (pTask->pWorkerCb) {
4,842,404!
1810
    code = pTask->pWorkerCb->afterRecoverFromBlocking(pTask->pWorkerCb->pPool);
4,842,413✔
1811
    if (code != TSDB_CODE_SUCCESS) {
4,842,523!
1812
      pTask->code = code;
×
1813
      return pTask->code;
×
1814
    }
1815
  }
1816
  return TSDB_CODE_SUCCESS;
4,842,514✔
1817
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc