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

taosdata / TDengine / #4907

30 Dec 2025 10:52AM UTC coverage: 65.541% (+0.03%) from 65.514%
#4907

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

808 existing lines in 106 files now uncovered.

193920 of 295877 relevant lines covered (65.54%)

118520209.34 hits per line

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

69.45
/source/libs/executor/src/externalwindowoperator.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 "operator.h"
18
#include "querytask.h"
19
#include "tdatablock.h"
20
#include "stream.h"
21
#include "filter.h"
22
#include "cmdnodes.h"
23

24
typedef struct SBlockList {
25
  const SSDataBlock* pSrcBlock;
26
  SList*             pBlocks;
27
  int32_t            blockRowNumThreshold;
28
} SBlockList;
29

30

31
typedef int32_t (*extWinGetWinFp)(SOperatorInfo*, int64_t*, int32_t*, SDataBlockInfo*, SExtWinTimeWindow**, int32_t*);
32

33
typedef struct SExtWindowStat {
34
  int64_t resBlockCreated;
35
  int64_t resBlockDestroyed;
36
  int64_t resBlockRecycled;
37
  int64_t resBlockReused;
38
  int64_t resBlockAppend;
39
} SExtWindowStat;
40

41
typedef struct SExternalWindowOperator {
42
  SOptrBasicInfo     binfo;
43
  SExprSupp          scalarSupp;
44
  int32_t            primaryTsIndex;
45
  EExtWinMode        mode;
46
  bool               multiTableMode;
47
  bool               inputHasOrder;
48
  SArray*            pWins;           // SArray<SExtWinTimeWindow>
49
  SArray*            pPseudoColInfo;  
50
  STimeRangeNode*    timeRangeExpr;
51
  
52
  extWinGetWinFp     getWinFp;
53

54
  bool               blkWinStartSet;
55
  int32_t            blkWinStartIdx;
56
  int32_t            blkWinIdx;
57
  int32_t            blkRowStartIdx;
58
  int32_t            outputWinId;
59
  int32_t            outputWinNum;
60
  int32_t            outWinIdx;
61

62
  // for project&indefRows
63
  SList*             pFreeBlocks;    // SList<SSDatablock*+SAarray*>
64
  SArray*            pOutputBlocks;  // SArray<SList*>, for each window, we have a list of blocks
65
  SListNode*         pLastBlkNode; 
66
  SSDataBlock*       pTmpBlock;
67
  
68
  // for agg
69
  SAggSupporter      aggSup;
70
  STimeWindowAggSupp twAggSup;
71

72
  int32_t            resultRowCapacity;
73
  SResultRow*        pResultRow;
74

75
  int64_t            lastSKey;
76
  int32_t            lastWinId;
77
  SSDataBlock*       pEmptyInputBlock;
78
  bool               hasCountFunc;
79
  SExtWindowStat     stat;
80
  SArray*            pWinRowIdx;
81

82
  // for vtable window query
83
  bool               isDynWindow;
84
  int32_t            orgTableVgId;
85
  tb_uid_t           orgTableUid;
86
  STimeWindow        orgTableTimeRange;
87
} SExternalWindowOperator;
88

89

90
static int32_t extWinBlockListAddBlock(SExternalWindowOperator* pExtW, SList* pList, int32_t rows, SSDataBlock** ppBlock, SArray** ppIdx) {
477✔
91
  SSDataBlock* pRes = NULL;
477✔
92
  int32_t code = 0, lino = 0;
477✔
93

94
  if (listNEles(pExtW->pFreeBlocks) > 0) {
477✔
95
    SListNode* pNode = tdListPopHead(pExtW->pFreeBlocks);
×
96
    *ppBlock = *(SSDataBlock**)pNode->data;
×
97
    *ppIdx = *(SArray**)((SArray**)pNode->data + 1);
×
98
    tdListAppendNode(pList, pNode);
×
99
    pExtW->stat.resBlockReused++;
×
100
  } else {
101
    TAOS_CHECK_EXIT(createOneDataBlock(pExtW->binfo.pRes, false, &pRes));
477✔
102
    TAOS_CHECK_EXIT(blockDataEnsureCapacity(pRes, TMAX(rows, 4096)));
477✔
103
    SArray* pIdx = taosArrayInit(10, sizeof(int64_t));
477✔
104
    TSDB_CHECK_NULL(pIdx, code, lino, _exit, terrno);
477✔
105
    void* res[2] = {pRes, pIdx};
477✔
106
    TAOS_CHECK_EXIT(tdListAppend(pList, res));
477✔
107

108
    *ppBlock = pRes;
477✔
109
    *ppIdx = pIdx;
477✔
110
    pExtW->stat.resBlockCreated++;
477✔
111
  }
112
  
113
_exit:
477✔
114

115
  if (code) {
477✔
116
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
117
    blockDataDestroy(pRes);
×
118
  }
119
  
120
  return code;
477✔
121
}
122

123
static int32_t extWinGetLastBlockFromList(SExternalWindowOperator* pExtW, SList* pList, int32_t rows, SSDataBlock** ppBlock, SArray** ppIdx) {
1,098✔
124
  int32_t    code = 0, lino = 0;
1,098✔
125
  SSDataBlock* pRes = NULL;
1,098✔
126

127
  SListNode* pNode = TD_DLIST_TAIL(pList);
1,098✔
128
  if (NULL == pNode) {
1,098✔
129
    TAOS_CHECK_EXIT(extWinBlockListAddBlock(pExtW, pList, rows, ppBlock, ppIdx));
477✔
130
    return code;
477✔
131
  }
132

133
  pRes = *(SSDataBlock**)pNode->data;
621✔
134
  if ((pRes->info.rows + rows) > pRes->info.capacity) {
621✔
135
    TAOS_CHECK_EXIT(extWinBlockListAddBlock(pExtW, pList, rows, ppBlock, ppIdx));
×
136
    return code;
×
137
  }
138

139
  *ppIdx = *(SArray**)((SSDataBlock**)pNode->data + 1);
621✔
140
  *ppBlock = pRes;
621✔
141
  pExtW->stat.resBlockAppend++;
621✔
142

143
_exit:
621✔
144

145
  if (code) {
621✔
146
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
147
  }
148
  
149
  return code;
621✔
150
}
151

152
static void extWinDestroyBlockList(void* p) {
11,059,200✔
153
  if (NULL == p) {
11,059,200✔
154
    return;
×
155
  }
156

157
  SListNode* pTmp = NULL;
11,059,200✔
158
  SList** ppList = (SList**)p;
11,059,200✔
159
  if ((*ppList) && TD_DLIST_NELES(*ppList) > 0) {
11,059,200✔
160
    SListNode* pNode = TD_DLIST_HEAD(*ppList);
×
161
    while (pNode) {
×
162
      SSDataBlock* pBlock = *(SSDataBlock**)pNode->data;
×
163
      blockDataDestroy(pBlock);
×
164
      SArray* pIdx = *(SArray**)((SArray**)pNode->data + 1);
×
165
      taosArrayDestroy(pIdx);
×
166
      pTmp = pNode;
×
167
      pNode = pNode->dl_next_;
×
168
      taosMemoryFree(pTmp);
×
169
    }
170
  }
171
  taosMemoryFree(*ppList);
11,059,200✔
172
}
173

174

175
static void extWinRecycleBlkNode(SExternalWindowOperator* pExtW, SListNode** ppNode) {
655,276✔
176
  if (NULL == ppNode || NULL == *ppNode) {
655,276✔
177
    return;
655,663✔
178
  }
179

UNCOV
180
  SSDataBlock* pBlock = *(SSDataBlock**)(*ppNode)->data;
×
181
  SArray* pIdx = *(SArray**)((SArray**)(*ppNode)->data + 1);
207✔
182
  
183
  if (listNEles(pExtW->pFreeBlocks) >= 10) {
207✔
184
    blockDataDestroy(pBlock);
×
185
    taosArrayDestroy(pIdx);
×
186
    taosMemoryFreeClear(*ppNode);
×
187
    pExtW->stat.resBlockDestroyed++;
×
188
    return;
×
189
  }
190
  
191
  blockDataCleanup(pBlock);
207✔
192
  taosArrayClear(pIdx);
207✔
193
  tdListPrependNode(pExtW->pFreeBlocks, *ppNode);
207✔
194
  *ppNode = NULL;
207✔
195
  pExtW->stat.resBlockRecycled++;
207✔
196
}
197

198
static void extWinRecycleBlockList(SExternalWindowOperator* pExtW, void* p) {
477✔
199
  if (NULL == p) {
477✔
200
    return;
×
201
  }
202

203
  SListNode* pTmp = NULL;
477✔
204
  SList** ppList = (SList**)p;
477✔
205
  if ((*ppList) && TD_DLIST_NELES(*ppList) > 0) {
477✔
206
    SListNode* pNode = TD_DLIST_HEAD(*ppList);
×
207
    while (pNode) {
×
208
      pTmp = pNode;
×
209
      pNode = pNode->dl_next_;
×
210
      extWinRecycleBlkNode(pExtW, &pTmp);
×
211
    }
212
  }
213
  taosMemoryFree(*ppList);
477✔
214
}
215
static void extWinDestroyBlkNode(SExternalWindowOperator* pInfo, SListNode* pNode) {
77,818✔
216
  if (NULL == pNode) {
77,818✔
217
    return;
77,341✔
218
  }
219

220
  SSDataBlock* pBlock = *(SSDataBlock**)pNode->data;
477✔
221
  SArray* pIdx = *(SArray**)((SArray**)pNode->data + 1);
477✔
222
  
223
  blockDataDestroy(pBlock);
477✔
224
  taosArrayDestroy(pIdx);
477✔
225

226
  taosMemoryFree(pNode);
477✔
227

228
  pInfo->stat.resBlockDestroyed++;
477✔
229
}
230

231

232
void destroyExternalWindowOperatorInfo(void* param) {
77,611✔
233
  if (NULL == param) {
77,611✔
234
    return;
×
235
  }
236
  SExternalWindowOperator* pInfo = (SExternalWindowOperator*)param;
77,611✔
237
  cleanupBasicInfo(&pInfo->binfo);
77,611✔
238

239
  taosArrayDestroyEx(pInfo->pOutputBlocks, extWinDestroyBlockList);
77,611✔
240
  taosArrayDestroy(pInfo->pWins);
77,611✔
241
  colDataDestroy(&pInfo->twAggSup.timeWindowData);
77,611✔
242
  taosArrayDestroy(pInfo->pWinRowIdx);
77,611✔
243
  
244
  taosArrayDestroy(pInfo->pPseudoColInfo);
77,611✔
245
  blockDataDestroy(pInfo->pTmpBlock);
77,611✔
246
  blockDataDestroy(pInfo->pEmptyInputBlock);
77,611✔
247

248
  extWinDestroyBlkNode(pInfo, pInfo->pLastBlkNode);
77,611✔
249
  if (pInfo->pFreeBlocks) {
77,611✔
250
    SListNode *node;
251
    while ((node = TD_DLIST_HEAD(pInfo->pFreeBlocks)) != NULL) {
477✔
252
      TD_DLIST_POP(pInfo->pFreeBlocks, node);
207✔
253
      extWinDestroyBlkNode(pInfo, node);
207✔
254
    }
255
    taosMemoryFree(pInfo->pFreeBlocks);
270✔
256
  }
257
  
258
  cleanupAggSup(&pInfo->aggSup);
77,611✔
259
  cleanupExprSupp(&pInfo->scalarSupp);
77,611✔
260
  taosMemoryFreeClear(pInfo->pResultRow);
77,611✔
261

262
  pInfo->binfo.resultRowInfo.openWindow = tdListFree(pInfo->binfo.resultRowInfo.openWindow);
77,611✔
263

264
  qDebug("ext window stat at destroy, created:%" PRId64 ", destroyed:%" PRId64 ", recycled:%" PRId64 ", reused:%" PRId64 ", append:%" PRId64, 
77,611✔
265
      pInfo->stat.resBlockCreated, pInfo->stat.resBlockDestroyed, pInfo->stat.resBlockRecycled, 
266
      pInfo->stat.resBlockReused, pInfo->stat.resBlockAppend);
267

268
  taosMemoryFreeClear(pInfo);
77,611✔
269
}
270

271
static int32_t extWinOpen(SOperatorInfo* pOperator);
272
static int32_t extWinNext(SOperatorInfo* pOperator, SSDataBlock** ppRes);
273

274
typedef struct SMergeAlignedExternalWindowOperator {
275
  SExternalWindowOperator* pExtW;
276
  int64_t curTs;
277
  SResultRow*  pResultRow;
278
} SMergeAlignedExternalWindowOperator;
279

280
void destroyMergeAlignedExternalWindowOperator(void* pOperator) {
1,646✔
281
  SMergeAlignedExternalWindowOperator* pMlExtInfo = (SMergeAlignedExternalWindowOperator*)pOperator;
1,646✔
282
  destroyExternalWindowOperatorInfo(pMlExtInfo->pExtW);
1,646✔
283
  taosMemoryFreeClear(pMlExtInfo);
1,646✔
284
}
1,646✔
285

286
int64_t* extWinExtractTsCol(SSDataBlock* pBlock, int32_t primaryTsIndex, SExecTaskInfo* pTaskInfo) {
2,229,734✔
287
  TSKEY* tsCols = NULL;
2,229,734✔
288

289
  if (pBlock->pDataBlock != NULL && pBlock->info.dataLoad) {
2,229,734✔
290
    SColumnInfoData* pColDataInfo = taosArrayGet(pBlock->pDataBlock, primaryTsIndex);
2,230,144✔
291
    if (!pColDataInfo) {
2,229,939✔
292
      pTaskInfo->code = terrno;
×
293
      T_LONG_JMP(pTaskInfo->env, terrno);
×
294
    }
295

296
    tsCols = (int64_t*)pColDataInfo->pData;
2,229,939✔
297
    if (pBlock->info.window.skey == 0 && pBlock->info.window.ekey == 0) {
2,229,939✔
298
      int32_t code = blockDataUpdateTsWindow(pBlock, primaryTsIndex);
2,230,144✔
299
      if (code != TSDB_CODE_SUCCESS) {
2,229,939✔
300
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
301
        pTaskInfo->code = code;
×
302
        T_LONG_JMP(pTaskInfo->env, code);
×
303
      }
304
    }
305
  }
306

307
  return tsCols;
2,229,939✔
308
}
309

310
static int32_t extWinGetCurWinIdx(SExecTaskInfo* pTaskInfo) {
14,165,893✔
311
  if (!pTaskInfo->pStreamRuntimeInfo) {
14,165,893✔
312
    return 0;
×
313
  }
314
  return pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
14,168,146✔
315
}
316

317
static void extWinIncCurWinIdx(SOperatorInfo* pOperator) {
×
318
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
×
319
  pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx++;
×
320
}
×
321

322
static void extWinSetCurWinIdx(SOperatorInfo* pOperator, int32_t idx) {
13,538,564✔
323
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
13,538,564✔
324
  if (pTaskInfo->pStreamRuntimeInfo) {
13,540,614✔
325
    pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = idx;
13,541,435✔
326
  }
327
}
13,543,692✔
328

329

330
static void extWinIncCurWinOutIdx(SStreamRuntimeInfo* pStreamRuntimeInfo) {
477✔
331
  pStreamRuntimeInfo->funcInfo.curOutIdx++;
477✔
332
}
477✔
333

334

335
static const STimeWindow* extWinGetNextWin(SExternalWindowOperator* pExtW, SExecTaskInfo* pTaskInfo) {
×
336
  int32_t curIdx = extWinGetCurWinIdx(pTaskInfo);
×
337
  if (curIdx + 1 >= pExtW->pWins->size) return NULL;
×
338
  return taosArrayGet(pExtW->pWins, curIdx + 1);
×
339
}
340

341

342
static int32_t extWinAppendWinIdx(SExecTaskInfo*       pTaskInfo, SArray* pIdx, SSDataBlock* pBlock, int32_t currWinIdx, int32_t rows) {
593,266✔
343
  int32_t  code = 0, lino = 0;
593,266✔
344
  int64_t* lastRes = taosArrayGetLast(pIdx);
593,266✔
345
  int32_t* lastWinIdx = (int32_t*)lastRes;
593,266✔
346
  int32_t* lastRowIdx = lastWinIdx ? (lastWinIdx + 1) : NULL;
593,266✔
347
  int64_t  res = 0;
593,266✔
348
  int32_t* pWinIdx = (int32_t*)&res;
593,266✔
349
  int32_t* pRowIdx = pWinIdx + 1;
593,266✔
350

351
  if (lastWinIdx && *lastWinIdx == currWinIdx) {
593,266✔
352
    return code;
621✔
353
  }
354

355
  *pWinIdx = currWinIdx;
592,645✔
356
  *pRowIdx = pBlock->info.rows - rows;
592,645✔
357

358
  TSDB_CHECK_NULL(taosArrayPush(pIdx, &res), code, lino, _exit, terrno);
592,645✔
359

360
_exit:
592,645✔
361

362
  if (code) {
592,645✔
363
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
364
  }
365

366
  return code;
592,645✔
367
}
368

369

370
static int32_t mergeAlignExtWinSetOutputBuf(SOperatorInfo* pOperator, SResultRowInfo* pResultRowInfo, const STimeWindow* pWin, SResultRow** pResult,
2,472✔
371
                                       SExprSupp* pExprSup, SAggSupporter* pAggSup) {
372
  if (*pResult == NULL) {
2,472✔
373
    *pResult = getNewResultRow(pAggSup->pResultBuf, &pAggSup->currentPageId, pAggSup->resultRowSize);
1,851✔
374
    if (!*pResult) {
1,851✔
375
      qError("get new resultRow failed, err:%s", tstrerror(terrno));
×
376
      return terrno;
×
377
    }
378
    pResultRowInfo->cur = (SResultRowPosition){.pageId = (*pResult)->pageId, .offset = (*pResult)->offset};
1,851✔
379
  }
380
  
381
  (*pResult)->win = *pWin;
2,472✔
382
  (*pResult)->winIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
2,472✔
383
  
384
  return setResultRowInitCtx((*pResult), pExprSup->pCtx, pExprSup->numOfExprs, pExprSup->rowEntryInfoOffset);
2,472✔
385
}
386

387

388
static int32_t mergeAlignExtWinGetWinFromTs(SOperatorInfo* pOperator, SExternalWindowOperator* pExtW, TSKEY ts, STimeWindow** ppWin) {
2,472✔
389
  int32_t blkWinIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
2,472✔
390
  
391
  // TODO handle desc order
392
  for (int32_t i = blkWinIdx; i < pExtW->pWins->size; ++i) {
3,093✔
393
    STimeWindow* pWin = taosArrayGet(pExtW->pWins, i);
3,093✔
394
    if (ts == pWin->skey) {
3,093✔
395
      extWinSetCurWinIdx(pOperator, i);
2,472✔
396
      *ppWin = pWin;
2,472✔
397
      return TSDB_CODE_SUCCESS;
2,472✔
398
    } else if (ts < pWin->skey) {
621✔
399
      qError("invalid ts %" PRId64 " for current window idx %d skey %" PRId64, ts, i, pWin->skey);
×
400
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
401
    }
402
  }
403
  
404
  qError("invalid ts %" PRId64 " to find merge aligned ext window, size:%d", ts, (int32_t)pExtW->pWins->size);
×
405
  return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
406
}
407

408
static int32_t mergeAlignExtWinFinalizeResult(SOperatorInfo* pOperator, SResultRowInfo* pResultRowInfo, SSDataBlock* pResultBlock) {
2,472✔
409
  int32_t        code = 0, lino = 0;
2,472✔
410
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
2,472✔
411
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
2,472✔
412
  SExprSupp*     pSup = &pOperator->exprSupp;
2,472✔
413
  SResultRow*  pResultRow = pMlExtInfo->pResultRow;
2,472✔
414
  
415
  finalizeResultRows(pExtW->aggSup.pResultBuf, &pResultRowInfo->cur, pSup, pResultBlock, pOperator->pTaskInfo);
2,472✔
416
  
417
  if (pResultRow->numOfRows > 0) {
2,472✔
418
    TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pExtW->pWinRowIdx, pResultBlock, pResultRow->winIdx, pResultRow->numOfRows));
2,472✔
419
  }
420

421
_exit:
2,472✔
422

423
  if (code) {
2,472✔
424
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
425
  }
426

427
  return code;
2,472✔
428
}
429

430
static int32_t mergeAlignExtWinAggDo(SOperatorInfo* pOperator, SResultRowInfo* pResultRowInfo, SSDataBlock* pBlock, SSDataBlock* pResultBlock) {
1,851✔
431
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
1,851✔
432
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
1,851✔
433

434
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
1,851✔
435
  SExprSupp*     pSup = &pOperator->exprSupp;
1,851✔
436
  int32_t        code = 0, lino = 0;
1,851✔
437
  STimeWindow *pWin = NULL;
1,851✔
438

439
  int32_t startPos = 0;
1,851✔
440
  int64_t* tsCols = extWinExtractTsCol(pBlock, pExtW->primaryTsIndex, pTaskInfo);
1,851✔
441
  TSKEY ts = getStartTsKey(&pBlock->info.window, tsCols);
1,851✔
442
  
443
  code = mergeAlignExtWinGetWinFromTs(pOperator, pExtW, ts, &pWin);
1,851✔
444
  if (code) {
1,851✔
445
    qError("failed to get time window for ts:%" PRId64 ", prim ts index:%d, error:%s", ts, pExtW->primaryTsIndex, tstrerror(code));
×
446
    TAOS_CHECK_EXIT(code);
×
447
  }
448

449
  if (pMlExtInfo->curTs != INT64_MIN && pMlExtInfo->curTs != pWin->skey) {
1,851✔
450
    TAOS_CHECK_EXIT(mergeAlignExtWinFinalizeResult(pOperator, pResultRowInfo, pResultBlock));
×
451
    resetResultRow(pMlExtInfo->pResultRow, pExtW->aggSup.resultRowSize - sizeof(SResultRow));
×
452
  }
453
  
454
  TAOS_CHECK_EXIT(mergeAlignExtWinSetOutputBuf(pOperator, pResultRowInfo, pWin, &pMlExtInfo->pResultRow, pSup, &pExtW->aggSup));
1,851✔
455

456
  int32_t currPos = startPos;
1,851✔
457
  pMlExtInfo->curTs = pWin->skey;
1,851✔
458
  
459
  while (++currPos < pBlock->info.rows) {
3,714✔
460
    if (tsCols[currPos] == pMlExtInfo->curTs) continue;
1,863✔
461

462
    qDebug("current ts:%" PRId64 ", startPos:%d, currPos:%d, tsCols[currPos]:%" PRId64,
621✔
463
      pMlExtInfo->curTs, startPos, currPos, tsCols[currPos]); 
464
    TAOS_CHECK_EXIT(applyAggFunctionOnPartialTuples(pTaskInfo, pSup->pCtx, &pExtW->twAggSup.timeWindowData, startPos,
621✔
465
                                           currPos - startPos, pBlock->info.rows, pSup->numOfExprs));
466

467
    TAOS_CHECK_EXIT(mergeAlignExtWinFinalizeResult(pOperator, pResultRowInfo, pResultBlock));
621✔
468
    resetResultRow(pMlExtInfo->pResultRow, pExtW->aggSup.resultRowSize - sizeof(SResultRow));
621✔
469

470
    TAOS_CHECK_EXIT(mergeAlignExtWinGetWinFromTs(pOperator, pExtW, tsCols[currPos], &pWin));
621✔
471
    
472
    qDebug("ext window align2 start:%" PRId64 ", end:%" PRId64, pWin->skey, pWin->ekey);
621✔
473
    startPos = currPos;
621✔
474
    
475
    TAOS_CHECK_EXIT(mergeAlignExtWinSetOutputBuf(pOperator, pResultRowInfo, pWin, &pMlExtInfo->pResultRow, pSup, &pExtW->aggSup));
621✔
476

477
    pMlExtInfo->curTs = pWin->skey;
621✔
478
  }
479

480
  code = applyAggFunctionOnPartialTuples(pTaskInfo, pSup->pCtx, &pExtW->twAggSup.timeWindowData, startPos,
3,702✔
481
                                         currPos - startPos, pBlock->info.rows, pSup->numOfExprs);
1,851✔
482

483
_exit:
1,851✔
484

485
  if (code != 0) {
1,851✔
486
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
487
    T_LONG_JMP(pTaskInfo->env, code);
×
488
  }
489
  
490
  return code;
1,851✔
491
}
492

493
static int32_t mergeAlignExtWinBuildWinRowIdx(SOperatorInfo* pOperator, SSDataBlock* pInput, SSDataBlock* pResult) {
×
494
  SExternalWindowOperator* pExtW = pOperator->info;
×
495
  int64_t* tsCols = extWinExtractTsCol(pInput, pExtW->primaryTsIndex, pOperator->pTaskInfo);
×
496
  STimeWindow* pWin = NULL;
×
497
  int32_t code = 0, lino = 0;
×
498
  int64_t prevTs = INT64_MIN;
×
499
  
500
  for (int32_t i = 0; i < pInput->info.rows; ++i) {
×
501
    if (prevTs == tsCols[i]) {
×
502
      continue;
×
503
    }
504
    
505
    TAOS_CHECK_EXIT(mergeAlignExtWinGetWinFromTs(pOperator, pExtW, tsCols[i], &pWin));
×
506
    TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pExtW->pWinRowIdx, pResult, extWinGetCurWinIdx(pOperator->pTaskInfo), pInput->info.rows - i));
×
507

508
    prevTs = tsCols[i];
×
509
  }
510

511
_exit:
×
512

513
  if (code != 0) {
×
514
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
515
  }
516

517
  return code;  
×
518
}
519

520
static int32_t mergeAlignExtWinProjectDo(SOperatorInfo* pOperator, SResultRowInfo* pResultRowInfo, SSDataBlock* pBlock,
×
521
                                            SSDataBlock* pResultBlock) {
522
  SExternalWindowOperator* pExtW = pOperator->info;
×
523
  SExprSupp*               pExprSup = &pExtW->scalarSupp;
×
524
  int32_t                  code = 0, lino = 0;
×
525
  
526
  TAOS_CHECK_EXIT(projectApplyFunctions(pExprSup->pExprInfo, pResultBlock, pBlock, pExprSup->pCtx, pExprSup->numOfExprs, NULL,
×
527
                        GET_STM_RTINFO(pOperator->pTaskInfo)));
528

529
  TAOS_CHECK_EXIT(mergeAlignExtWinBuildWinRowIdx(pOperator, pBlock, pResultBlock));
×
530

531
_exit:
×
532

533
  if (code != 0) {
×
534
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
535
  }
536

537
  return code;
×
538
}
539

540
void mergeAlignExtWinDo(SOperatorInfo* pOperator) {
2,261✔
541
  SExecTaskInfo*                       pTaskInfo = pOperator->pTaskInfo;
2,261✔
542
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
2,261✔
543
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
2,261✔
544
  SResultRow*                          pResultRow = NULL;
2,261✔
545
  int32_t                              code = 0;
2,261✔
546
  SSDataBlock*                         pRes = pExtW->binfo.pRes;
2,261✔
547
  SExprSupp*                           pSup = &pOperator->exprSupp;
2,261✔
548
  int32_t                              lino = 0;
2,261✔
549

550
  taosArrayClear(pExtW->pWinRowIdx);
2,261✔
551
  blockDataCleanup(pRes);
2,261✔
552

553
  while (1) {
1,851✔
554
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
4,112✔
555

556
    if (pBlock == NULL) {
4,112✔
557
      // close last time window
558
      if (pMlExtInfo->curTs != INT64_MIN && EEXT_MODE_AGG == pExtW->mode) {
2,261✔
559
        TAOS_CHECK_EXIT(mergeAlignExtWinFinalizeResult(pOperator, &pExtW->binfo.resultRowInfo, pRes));
1,851✔
560
      }
561
      setOperatorCompleted(pOperator);
2,261✔
562
      break;
2,261✔
563
    }
564

565
    pRes->info.scanFlag = pBlock->info.scanFlag;
1,851✔
566
    code = setInputDataBlock(pSup, pBlock, pExtW->binfo.inputTsOrder, pBlock->info.scanFlag, true);
1,851✔
567
    QUERY_CHECK_CODE(code, lino, _exit);
1,851✔
568

569
    printDataBlock(pBlock, __func__, "externalwindowAlign", pTaskInfo->id.queryId);
1,851✔
570
    qDebug("ext windowpExtWAlign->scalarMode:%d", pExtW->mode);
1,851✔
571

572
    if (EEXT_MODE_SCALAR == pExtW->mode) {
1,851✔
573
      TAOS_CHECK_EXIT(mergeAlignExtWinProjectDo(pOperator, &pExtW->binfo.resultRowInfo, pBlock, pRes));
×
574
    } else {
575
      TAOS_CHECK_EXIT(mergeAlignExtWinAggDo(pOperator, &pExtW->binfo.resultRowInfo, pBlock, pRes));
1,851✔
576
    }
577

578
    if (pRes->info.rows >= pOperator->resultInfo.threshold) {
1,851✔
579
      break;
×
580
    }
581
  }
582

583
  pOperator->pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamBlkWinIdx = pExtW->pWinRowIdx;
2,261✔
584
  
585
_exit:
2,261✔
586

587
  if (code != 0) {
2,261✔
588
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
589
    pTaskInfo->code = code;
×
590
    T_LONG_JMP(pTaskInfo->env, code);
×
591
  }
592
}
2,261✔
593

594
static int32_t mergeAlignExtWinNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
4,112✔
595
  SExecTaskInfo*                       pTaskInfo = pOperator->pTaskInfo;
4,112✔
596
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
4,112✔
597
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
4,112✔
598
  int32_t                              code = 0;
4,112✔
599
  int32_t lino = 0;
4,112✔
600

601
  if (pOperator->status == OP_EXEC_DONE) {
4,112✔
602
    (*ppRes) = NULL;
1,851✔
603
    return TSDB_CODE_SUCCESS;
1,851✔
604
  }
605

606
  SSDataBlock* pRes = pExtW->binfo.pRes;
2,261✔
607
  blockDataCleanup(pRes);
2,261✔
608

609
  if (taosArrayGetSize(pExtW->pWins) <= 0) {
2,261✔
610
    size_t size = taosArrayGetSize(pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamPesudoFuncVals);
2,261✔
611
    STimeWindow* pWin = taosArrayReserve(pExtW->pWins, size);
2,261✔
612
    TSDB_CHECK_NULL(pWin, code, lino, _exit, terrno);
2,261✔
613

614
    for (int32_t i = 0; i < size; ++i) {
5,143✔
615
      SSTriggerCalcParam* pParam = taosArrayGet(pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamPesudoFuncVals, i);
2,882✔
616
      pWin[i].skey = pParam->wstart;
2,882✔
617
      pWin[i].ekey = pParam->wstart + 1;
2,882✔
618
    }
619
    
620
    pExtW->outputWinId = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
2,261✔
621
  }
622

623
  mergeAlignExtWinDo(pOperator);
2,261✔
624
  
625
  size_t rows = pRes->info.rows;
2,261✔
626
  pOperator->resultInfo.totalRows += rows;
2,261✔
627
  (*ppRes) = (rows == 0) ? NULL : pRes;
2,261✔
628

629
_exit:
2,261✔
630

631
  if (code != 0) {
2,261✔
632
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
633
    pTaskInfo->code = code;
×
634
    T_LONG_JMP(pTaskInfo->env, code);
×
635
  }
636
  return code;
2,261✔
637
}
638

639
int32_t resetMergeAlignedExtWinOperator(SOperatorInfo* pOperator) {
2,876✔
640
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
2,876✔
641
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
2,876✔
642
  SExecTaskInfo*                       pTaskInfo = pOperator->pTaskInfo;
2,876✔
643
  SMergeAlignedIntervalPhysiNode * pPhynode = (SMergeAlignedIntervalPhysiNode*)pOperator->pPhyNode;
2,876✔
644
  pOperator->status = OP_NOT_OPENED;
2,876✔
645

646
  taosArrayClear(pExtW->pWins);
2,876✔
647

648
  resetBasicOperatorState(&pExtW->binfo);
2,876✔
649
  pMlExtInfo->pResultRow = NULL;
2,876✔
650
  pMlExtInfo->curTs = INT64_MIN;
2,876✔
651

652
  int32_t code = resetAggSup(&pOperator->exprSupp, &pExtW->aggSup, pTaskInfo, pPhynode->window.pFuncs, NULL,
5,752✔
653
                             sizeof(int64_t) * 2 + POINTER_BYTES, pTaskInfo->id.str, pTaskInfo->streamInfo.pState,
2,876✔
654
                             &pTaskInfo->storageAPI.functionStore);
655
  if (code == 0) {
2,876✔
656
    colDataDestroy(&pExtW->twAggSup.timeWindowData);
2,876✔
657
    code = initExecTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pTaskInfo->window);
2,876✔
658
  }
659
  return code;
2,876✔
660
}
661

662
int32_t createMergeAlignedExternalWindowOperator(SOperatorInfo* pDownstream, SPhysiNode* pNode,
1,646✔
663
                                                 SExecTaskInfo* pTaskInfo, SOperatorInfo** ppOptrOut) {
664
  SMergeAlignedIntervalPhysiNode* pPhynode = (SMergeAlignedIntervalPhysiNode*)pNode;
1,646✔
665
  int32_t code = 0;
1,646✔
666
  int32_t lino = 0;
1,646✔
667
  SMergeAlignedExternalWindowOperator* pMlExtInfo = taosMemoryCalloc(1, sizeof(SMergeAlignedExternalWindowOperator));
1,646✔
668
  SOperatorInfo*                       pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
1,646✔
669

670
  if (pTaskInfo->pStreamRuntimeInfo != NULL){
1,646✔
671
    pTaskInfo->pStreamRuntimeInfo->funcInfo.withExternalWindow = true;
1,646✔
672
  }
673
  pOperator->pPhyNode = pNode;
1,646✔
674
  if (!pMlExtInfo || !pOperator) {
1,646✔
675
    code = terrno;
×
676
    goto _error;
×
677
  }
678

679
  pMlExtInfo->pExtW = taosMemoryCalloc(1, sizeof(SExternalWindowOperator));
1,646✔
680
  if (!pMlExtInfo->pExtW) {
1,646✔
681
    code = terrno;
×
682
    goto _error;
×
683
  }
684

685
  SExternalWindowOperator* pExtW = pMlExtInfo->pExtW;
1,646✔
686
  SExprSupp* pSup = &pOperator->exprSupp;
1,646✔
687
  pSup->hasWindowOrGroup = true;
1,646✔
688
  pSup->hasWindow = true;
1,646✔
689
  pMlExtInfo->curTs = INT64_MIN;
1,646✔
690

691
  pExtW->primaryTsIndex = ((SColumnNode*)pPhynode->window.pTspk)->slotId;
1,646✔
692
  pExtW->mode = pPhynode->window.pProjs ? EEXT_MODE_SCALAR : EEXT_MODE_AGG;
1,646✔
693
  pExtW->binfo.inputTsOrder = pPhynode->window.node.inputTsOrder = TSDB_ORDER_ASC;
1,646✔
694
  pExtW->binfo.outputTsOrder = pExtW->binfo.inputTsOrder;
1,646✔
695

696
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
1,646✔
697
  initResultSizeInfo(&pOperator->resultInfo, 4096);
1,646✔
698

699
  int32_t num = 0;
1,646✔
700
  SExprInfo* pExprInfo = NULL;
1,646✔
701
  code = createExprInfo(pPhynode->window.pFuncs, NULL, &pExprInfo, &num);
1,646✔
702
  QUERY_CHECK_CODE(code, lino, _error);
1,646✔
703

704
  if (pExtW->mode == EEXT_MODE_AGG) {
1,646✔
705
    code = initAggSup(pSup, &pExtW->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str, pTaskInfo->streamInfo.pState,
1,646✔
706
                      &pTaskInfo->storageAPI.functionStore);
707
    QUERY_CHECK_CODE(code, lino, _error);
1,646✔
708
  }
709

710
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pPhynode->window.node.pOutputDataBlockDesc);
1,646✔
711
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
1,646✔
712
  initBasicInfo(&pExtW->binfo, pResBlock);
1,646✔
713

714
  pExtW->pWins = taosArrayInit(4096, sizeof(STimeWindow));
1,646✔
715
  if (!pExtW->pWins) QUERY_CHECK_CODE(terrno, lino, _error);
1,646✔
716

717
  pExtW->pWinRowIdx = taosArrayInit(4096, sizeof(int64_t));
1,646✔
718
  TSDB_CHECK_NULL(pExtW->pWinRowIdx, code, lino, _error, terrno);
1,646✔
719

720
  initResultRowInfo(&pExtW->binfo.resultRowInfo);
1,646✔
721
  code = blockDataEnsureCapacity(pExtW->binfo.pRes, pOperator->resultInfo.capacity);
1,646✔
722
  QUERY_CHECK_CODE(code, lino, _error);
1,646✔
723
  setOperatorInfo(pOperator, "MergeAlignedExternalWindowOperator", QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW, false, OP_NOT_OPENED, pMlExtInfo, pTaskInfo);
1,646✔
724
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, mergeAlignExtWinNext, NULL,
1,646✔
725
                                         destroyMergeAlignedExternalWindowOperator, optrDefaultBufFn, NULL,
726
                                         optrDefaultGetNextExtFn, NULL);
727
  setOperatorResetStateFn(pOperator, resetMergeAlignedExtWinOperator);
1,646✔
728

729
  code = appendDownstream(pOperator, &pDownstream, 1);
1,646✔
730
  QUERY_CHECK_CODE(code, lino, _error);
1,646✔
731
  *ppOptrOut = pOperator;
1,646✔
732
  return code;
1,646✔
733
  
734
_error:
×
735
  if (pMlExtInfo) destroyMergeAlignedExternalWindowOperator(pMlExtInfo);
×
736
  destroyOperatorAndDownstreams(pOperator, &pDownstream, 1);
×
737
  pTaskInfo->code = code;
×
738
  return code;
×
739
}
740

741
static int32_t resetExternalWindowExprSupp(SExternalWindowOperator* pExtW, SExecTaskInfo* pTaskInfo,
289,457✔
742
                                           SExternalWindowPhysiNode* pPhynode) {
743
  int32_t    code = 0, lino = 0, num = 0;
289,457✔
744
  SExprInfo* pExprInfo = NULL;
289,457✔
745
  cleanupExprSuppWithoutFilter(&pExtW->scalarSupp);
289,457✔
746

747
  SNodeList* pNodeList = NULL;
289,849✔
748
  if (pPhynode->window.pProjs) {
289,849✔
749
    pNodeList = pPhynode->window.pProjs;
×
750
  } else {
751
    pNodeList = pPhynode->window.pExprs;
289,849✔
752
  }
753

754
  code = createExprInfo(pNodeList, NULL, &pExprInfo, &num);
289,457✔
755
  QUERY_CHECK_CODE(code, lino, _error);
289,457✔
756
  code = initExprSupp(&pExtW->scalarSupp, pExprInfo, num, &pTaskInfo->storageAPI.functionStore);
289,457✔
757
  QUERY_CHECK_CODE(code, lino, _error);
289,457✔
758
  return code;
289,457✔
759
_error:
×
760
  if (code != TSDB_CODE_SUCCESS) {
×
761
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
762
    pTaskInfo->code = code;
×
763
  }
764
  return code;
×
765
}
766

767
static int32_t resetExternalWindowOperator(SOperatorInfo* pOperator) {
289,457✔
768
  int32_t code = 0, lino = 0;
289,457✔
769
  SExternalWindowOperator* pExtW = pOperator->info;
289,457✔
770
  SExecTaskInfo*           pTaskInfo = pOperator->pTaskInfo;
289,457✔
771
  SExternalWindowPhysiNode* pPhynode = (SExternalWindowPhysiNode*)pOperator->pPhyNode;
289,457✔
772
  pOperator->status = OP_NOT_OPENED;
289,457✔
773

774
  //resetBasicOperatorState(&pExtW->binfo);
775
  initResultRowInfo(&pExtW->binfo.resultRowInfo);
289,457✔
776

777
  pExtW->outputWinId = 0;
289,849✔
778
  pExtW->lastWinId = -1;
289,849✔
779
  pExtW->outputWinNum = 0;
289,849✔
780
  taosArrayClear(pExtW->pWins);
289,065✔
781
  extWinRecycleBlkNode(pExtW, &pExtW->pLastBlkNode);
289,849✔
782

783
/*
784
  int32_t code = blockDataEnsureCapacity(pExtW->binfo.pRes, pOperator->resultInfo.capacity);
785
  if (code == 0) {
786
    code = resetAggSup(&pOperator->exprSupp, &pExtW->aggSup, pTaskInfo, pPhynode->window.pFuncs, NULL,
787
                       sizeof(int64_t) * 2 + POINTER_BYTES, pTaskInfo->id.str, pTaskInfo->streamInfo.pState,
788
                       &pTaskInfo->storageAPI.functionStore);
789
  }
790
*/
791
  TAOS_CHECK_EXIT(resetExternalWindowExprSupp(pExtW, pTaskInfo, pPhynode));
289,065✔
792
  colDataDestroy(&pExtW->twAggSup.timeWindowData);
289,457✔
793
  TAOS_CHECK_EXIT(initExecTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pTaskInfo->window));
289,849✔
794

795
  pExtW->outWinIdx = 0;
289,457✔
796
  pExtW->lastSKey = INT64_MIN;
289,849✔
797
  pExtW->isDynWindow = false;
289,457✔
798

799
  qDebug("%s ext window stat at reset, created:%" PRId64 ", destroyed:%" PRId64 ", recycled:%" PRId64 ", reused:%" PRId64 ", append:%" PRId64, 
289,457✔
800
      pTaskInfo->id.str, pExtW->stat.resBlockCreated, pExtW->stat.resBlockDestroyed, pExtW->stat.resBlockRecycled, 
801
      pExtW->stat.resBlockReused, pExtW->stat.resBlockAppend);
802

803
_exit:
6,979✔
804

805
  if (code) {
289,849✔
806
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
807
  }
808
  
809
  return code;
289,849✔
810
}
811

812
static EDealRes extWinHasCountLikeFunc(SNode* pNode, void* res) {
578,220✔
813
  if (QUERY_NODE_FUNCTION == nodeType(pNode)) {
578,220✔
814
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
206,279✔
815
    if (fmIsCountLikeFunc(pFunc->funcId) || (pFunc->hasOriginalFunc && fmIsCountLikeFunc(pFunc->originalFuncId))) {
206,279✔
816
      *(bool*)res = true;
63,304✔
817
      return DEAL_RES_END;
63,505✔
818
    }
819
  }
820
  return DEAL_RES_CONTINUE;
515,937✔
821
}
822

823

824
static int32_t extWinCreateEmptyInputBlock(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
63,505✔
825
  int32_t code = TSDB_CODE_SUCCESS;
63,505✔
826
  int32_t lino = 0;
63,505✔
827
  SSDataBlock* pBlock = NULL;
63,505✔
828
  if (!tsCountAlwaysReturnValue) {
63,505✔
829
    return TSDB_CODE_SUCCESS;
×
830
  }
831

832
  SExternalWindowOperator* pExtW = pOperator->info;
63,505✔
833

834
  if (!pExtW->hasCountFunc) {
63,505✔
835
    return TSDB_CODE_SUCCESS;
×
836
  }
837

838
  code = createDataBlock(&pBlock);
63,505✔
839
  if (code) {
63,507✔
840
    return code;
×
841
  }
842

843
  pBlock->info.rows = 1;
63,507✔
844
  pBlock->info.capacity = 0;
63,710✔
845

846
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
329,843✔
847
    SColumnInfoData colInfo = {0};
265,681✔
848
    colInfo.hasNull = true;
265,729✔
849
    colInfo.info.type = TSDB_DATA_TYPE_NULL;
265,729✔
850
    colInfo.info.bytes = 1;
265,729✔
851

852
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
265,729✔
853
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
545,641✔
854
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
279,552✔
855
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
279,552✔
856
        int32_t slotId = pFuncParam->pCol->slotId;
191,600✔
857
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
191,600✔
858
        if (slotId >= numOfCols) {
191,395✔
859
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
143,988✔
860
          QUERY_CHECK_CODE(code, lino, _end);
143,785✔
861

862
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
295,248✔
863
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
151,055✔
864
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
151,463✔
865
          }
866
        }
867
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
87,952✔
868
        // do nothing
869
      }
870
    }
871
  }
872

873
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows);
63,710✔
874
  QUERY_CHECK_CODE(code, lino, _end);
63,710✔
875

876
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
214,968✔
877
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
151,258✔
878
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
151,258✔
879
    colDataSetNULL(pColInfoData, 0);
880
  }
881
  *ppBlock = pBlock;
63,710✔
882

883
_end:
63,712✔
884
  if (code != TSDB_CODE_SUCCESS) {
63,505✔
885
    blockDataDestroy(pBlock);
×
886
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
887
  }
888
  return code;
63,710✔
889
}
890

891

892

893
static int extWinTsWinCompare(const void* pLeft, const void* pRight) {
3,404,058✔
894
  int64_t ts = *(int64_t*)pLeft;
3,404,058✔
895
  SExtWinTimeWindow* pWin = (SExtWinTimeWindow*)pRight;
3,404,058✔
896
  if (ts < pWin->tw.skey) {
3,404,058✔
897
    return -1;
1,240,121✔
898
  }
899
  if (ts >= pWin->tw.ekey) {
2,164,757✔
900
    return 1;
63,740✔
901
  }
902

903
  return 0;
2,101,017✔
904
}
905

906

907
static int32_t extWinGetMultiTbWinFromTs(SOperatorInfo* pOperator, SExternalWindowOperator* pExtW, int64_t* tsCol, int64_t rowNum, int32_t* startPos) {
1,224,177✔
908
  int32_t idx = taosArraySearchIdx(pExtW->pWins, tsCol, extWinTsWinCompare, TD_EQ);
1,224,177✔
909
  if (idx >= 0) {
1,224,382✔
910
    *startPos = 0;
1,223,912✔
911
    return idx;
1,223,912✔
912
  }
913

914
  SExtWinTimeWindow* pWin = NULL;
470✔
915
  int32_t w = 0;
470✔
916
  for (int64_t i = 1; i < rowNum; ++i) {
940✔
917
    for (; w < pExtW->pWins->size; ++w) {
940✔
918
      pWin = TARRAY_GET_ELEM(pExtW->pWins, w);
940✔
919
      if (tsCol[i] < pWin->tw.skey) {
940✔
920
        break;
470✔
921
      }
922
      
923
      if (tsCol[i] < pWin->tw.ekey) {
470✔
924
        *startPos = i;
×
925
        return w;
×
926
      }
927
    }
928
  }
929

930
  return -1;
470✔
931
}
932

933
static int32_t extWinGetNoOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
193,658✔
934
  SExternalWindowOperator* pExtW = pOperator->info;
193,658✔
935
  if ((*startPos) >= pInfo->rows) {
193,658✔
936
    qDebug("%s %s blk rowIdx %d reach the end, size: %d, skip block", 
51,357✔
937
        GET_TASKID(pOperator->pTaskInfo), __func__, *startPos, (int32_t)pInfo->rows);
938
    *ppWin = NULL;
51,357✔
939
    return TSDB_CODE_SUCCESS;
51,357✔
940
  }
941
  
942
  if (pExtW->blkWinIdx < 0) {
142,301✔
943
    pExtW->blkWinIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
52,084✔
944
  } else {
945
    pExtW->blkWinIdx++;
90,217✔
946
  }
947

948
  if (pExtW->blkWinIdx >= pExtW->pWins->size) {
142,301✔
949
    qDebug("%s %s ext win blk idx %d reach the end, size: %d, skip block", 
×
950
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (int32_t)pExtW->pWins->size);
951
    *ppWin = NULL;
×
952
    return TSDB_CODE_SUCCESS;
×
953
  }
954
  
955
  SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
142,301✔
956
  if (tsCol[pInfo->rows - 1] < pWin->tw.skey) {
142,301✔
957
    qDebug("%s %s block end ts %" PRId64 " is small than curr win %d skey %" PRId64 ", skip block", 
×
958
        GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[pInfo->rows - 1], pExtW->blkWinIdx, pWin->tw.skey);
959
    *ppWin = NULL;
×
960
    return TSDB_CODE_SUCCESS;
×
961
  }
962

963
  int32_t r = *startPos;
142,301✔
964

965
  qDebug("%s %s start to get novlp win from winIdx %d rowIdx %d", GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, r);
142,301✔
966

967
  // TODO handle desc order
968
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
148,063✔
969
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
148,063✔
970
    for (; r < pInfo->rows; ++r) {
192,985✔
971
      if (tsCol[r] < pWin->tw.skey) {
192,258✔
972
        continue;
44,922✔
973
      }
974

975
      if (tsCol[r] < pWin->tw.ekey) {
147,336✔
976
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
141,574✔
977
        *ppWin = pWin;
141,369✔
978
        *startPos = r;
141,369✔
979
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
141,574✔
980

981
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
141,574✔
982
            GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, r, tsCol[r], tsCol[r + *winRows - 1]);
983
        
984
        return TSDB_CODE_SUCCESS;
141,574✔
985
      }
986

987
      if (!pOperator->pTaskInfo->pStreamRuntimeInfo && tsCol[r] >= pWin->tw.ekey) {
5,762✔
988
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
×
989
        *ppWin = pWin;
×
990
        *startPos = r;
×
991
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
×
992

993
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk",
×
994
               GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, r, tsCol[r], tsCol[r + *winRows - 1]);
995

996
        return TSDB_CODE_SUCCESS;
×
997
      }
998

999
      break;
5,762✔
1000
    }
1001

1002
    if (r == pInfo->rows) {
6,489✔
1003
      break;
727✔
1004
    }
1005
  }
1006

1007
  qDebug("%s %s no more ext win in block, TR[%" PRId64 ", %" PRId64 "), skip it", 
727✔
1008
      GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1009

1010
  *ppWin = NULL;
727✔
1011
  return TSDB_CODE_SUCCESS;
727✔
1012
}
1013

1014
static int32_t extWinGetOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
196,562✔
1015
  SExternalWindowOperator* pExtW = pOperator->info;
196,562✔
1016
  if (pExtW->blkWinIdx < 0) {
196,562✔
1017
    pExtW->blkWinIdx = pExtW->blkWinStartIdx;
13,222✔
1018
  } else {
1019
    pExtW->blkWinIdx++;
183,340✔
1020
  }
1021

1022
  if (pExtW->blkWinIdx >= pExtW->pWins->size) {
196,562✔
1023
    qDebug("%s %s ext win blk idx %d reach the end, size: %d, skip block", 
12,495✔
1024
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (int32_t)pExtW->pWins->size);
1025
    *ppWin = NULL;
12,495✔
1026
    return TSDB_CODE_SUCCESS;
12,495✔
1027
  }
1028
  
1029
  SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
184,067✔
1030
  if (tsCol[pInfo->rows - 1] < pWin->tw.skey) {
184,067✔
1031
    qDebug("%s %s block end ts %" PRId64 " is small than curr win %d skey %" PRId64 ", skip block", 
×
1032
        GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[pInfo->rows - 1], pExtW->blkWinIdx, pWin->tw.skey);
1033
    *ppWin = NULL;
×
1034
    return TSDB_CODE_SUCCESS;
×
1035
  }
1036

1037
  int64_t r = 0;
184,067✔
1038

1039
  qDebug("%s %s start to get ovlp win from winIdx %d rowIdx %d", GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pExtW->blkRowStartIdx);
184,067✔
1040
  
1041
  // TODO handle desc order
1042
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
184,794✔
1043
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
184,794✔
1044
    for (r = pExtW->blkRowStartIdx; r < pInfo->rows; ++r) {
320,016✔
1045
      if (tsCol[r] < pWin->tw.skey) {
319,289✔
1046
        pExtW->blkRowStartIdx = r + 1;
135,222✔
1047
        continue;
135,222✔
1048
      }
1049

1050
      if (tsCol[r] < pWin->tw.ekey) {
184,067✔
1051
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
183,340✔
1052
        *ppWin = pWin;
183,340✔
1053
        *startPos = r;
183,340✔
1054
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
183,340✔
1055

1056
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
183,340✔
1057
            GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, (int32_t)r, tsCol[r], tsCol[r + *winRows - 1]);
1058
        
1059
        if ((r + *winRows) < pInfo->rows) {
183,340✔
1060
          pExtW->blkWinStartIdx = pExtW->blkWinIdx + 1;
170,845✔
1061
          pExtW->blkWinStartSet = true;
170,845✔
1062
        }
1063
        
1064
        return TSDB_CODE_SUCCESS;
183,340✔
1065
      }
1066

1067
      break;
727✔
1068
    }
1069

1070
    if (r >= pInfo->rows) {
1,454✔
1071
      if (!pExtW->blkWinStartSet) {
727✔
1072
        pExtW->blkWinStartIdx = pExtW->blkWinIdx;
727✔
1073
      }
1074
      
1075
      break;
727✔
1076
    }
1077
  }
1078

1079
  qDebug("%s %s no more ext win in block, TR[%" PRId64 ", %" PRId64 "), skip it", 
727✔
1080
      GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1081

1082
  *ppWin = NULL;
727✔
1083
  return TSDB_CODE_SUCCESS;
727✔
1084
}
1085

1086

1087
static int32_t extWinGetMultiTbNoOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
6,261,501✔
1088
  SExternalWindowOperator* pExtW = pOperator->info;
6,261,501✔
1089
  if ((*startPos) >= pInfo->rows) {
6,264,576✔
1090
    qDebug("%s %s blk rowIdx %d reach the end, size: %d, skip block", 
1,223,912✔
1091
        GET_TASKID(pOperator->pTaskInfo), __func__, *startPos, (int32_t)pInfo->rows);
1092
    *ppWin = NULL;
1,224,117✔
1093
    return TSDB_CODE_SUCCESS;
1,224,117✔
1094
  }
1095
  
1096
  if (pExtW->blkWinIdx < 0) {
5,042,099✔
1097
    pExtW->blkWinIdx = extWinGetMultiTbWinFromTs(pOperator, pExtW, tsCol, pInfo->rows, startPos);
1,224,587✔
1098
    if (pExtW->blkWinIdx < 0) {
1,224,382✔
1099
      qDebug("%s %s blk TR[%" PRId64 ", %" PRId64 ") not in any win, skip block", 
470✔
1100
          GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1101
      *ppWin = NULL;
470✔
1102
      return TSDB_CODE_SUCCESS;
470✔
1103
    }
1104

1105
    extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
1,223,912✔
1106
    *ppWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
1,224,117✔
1107
    *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, *startPos, (*ppWin)->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
1,224,117✔
1108

1109
    qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
1,223,707✔
1110
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (*ppWin)->tw.skey, (*ppWin)->tw.ekey, *winRows, *startPos, tsCol[*startPos], tsCol[*startPos + *winRows - 1]);
1111
    
1112
    return TSDB_CODE_SUCCESS;
1,224,117✔
1113
  } else {
1114
    pExtW->blkWinIdx++;
3,817,512✔
1115
  }
1116

1117
  if (pExtW->blkWinIdx >= pExtW->pWins->size) {
3,817,512✔
1118
    qDebug("%s %s ext win blk idx %d reach the end, size: %d, skip block", 
×
1119
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (int32_t)pExtW->pWins->size);
1120
    *ppWin = NULL;
×
1121
    return TSDB_CODE_SUCCESS;
×
1122
  }
1123
  
1124
  SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
3,818,742✔
1125
  if (tsCol[pInfo->rows - 1] < pWin->tw.skey) {
3,818,742✔
1126
    qDebug("%s %s block end ts %" PRId64 " is small than curr win %d skey %" PRId64 ", skip block", 
×
1127
        GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[pInfo->rows - 1], pExtW->blkWinIdx, pWin->tw.skey);
1128
    *ppWin = NULL;
×
1129
    return TSDB_CODE_SUCCESS;
×
1130
  }
1131

1132
  int32_t r = *startPos;
3,817,307✔
1133

1134
  qDebug("%s %s start to get mnovlp win from winIdx %d rowIdx %d", GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, r);
3,817,102✔
1135

1136
  // TODO handle desc order
1137
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
3,830,251✔
1138
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
3,830,251✔
1139
    for (; r < pInfo->rows; ++r) {
3,834,528✔
1140
      if (tsCol[r] < pWin->tw.skey) {
3,834,733✔
1141
        continue;
4,687✔
1142
      }
1143

1144
      if (tsCol[r] < pWin->tw.ekey) {
3,830,251✔
1145
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
3,818,947✔
1146
        *ppWin = pWin;
3,818,742✔
1147
        *startPos = r;
3,818,742✔
1148
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
3,818,742✔
1149

1150
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
3,818,947✔
1151
            GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, r, tsCol[r], tsCol[r + *winRows - 1]);
1152
        
1153
        return TSDB_CODE_SUCCESS;
3,817,512✔
1154
      }
1155

1156
      if (!pOperator->pTaskInfo->pStreamRuntimeInfo && tsCol[r] >= pWin->tw.ekey) {
11,304✔
1157
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
×
1158
        *ppWin = pWin;
×
1159
        *startPos = r;
×
1160
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
×
1161

1162
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk",
×
1163
               GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, r, tsCol[r], tsCol[r + *winRows - 1]);
1164

1165
        return TSDB_CODE_SUCCESS;
×
1166
      }
1167

1168
      break;
11,304✔
1169
    }
1170

1171
    if (r == pInfo->rows) {
11,147✔
1172
      break;
×
1173
    }
1174
  }
1175

1176
  qDebug("%s %s no more ext win in block, TR[%" PRId64 ", %" PRId64 "), skip it", 
×
1177
      GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1178

1179
  *ppWin = NULL;
×
1180
  return TSDB_CODE_SUCCESS;
×
1181
}
1182

1183
static int32_t extWinGetFirstWinFromTs(SOperatorInfo* pOperator, SExternalWindowOperator* pExtW, int64_t* tsCol,
938,400✔
1184
                                       int64_t rowNum, int32_t* startPos) {
1185
  SExtWinTimeWindow* pWin = NULL;
938,400✔
1186
  int32_t            idx = taosArraySearchIdx(pExtW->pWins, tsCol, extWinTsWinCompare, TD_EQ);
938,400✔
1187
  if (idx >= 0) {
938,400✔
1188
    for (int i = idx - 1; i >= 0; --i) {
876,900✔
1189
      pWin = TARRAY_GET_ELEM(pExtW->pWins, i);
×
1190
      if (extWinTsWinCompare(tsCol, pWin) == 0) {
×
1191
        idx = i;
×
1192
      } else {
1193
        break;
×
1194
      }
1195
    }
1196
    *startPos = 0;
876,900✔
1197
    return idx;
876,900✔
1198
  }
1199

1200
  pWin = NULL;
61,500✔
1201
  int32_t w = 0;
61,500✔
1202
  for (int64_t i = 1; i < rowNum; ++i) {
123,410✔
1203
    for (; w < pExtW->pWins->size; ++w) {
143,910✔
1204
      pWin = TARRAY_GET_ELEM(pExtW->pWins, w);
143,910✔
1205
      if (tsCol[i] < pWin->tw.skey) {
143,910✔
1206
        break;
61,910✔
1207
      }
1208

1209
      if (tsCol[i] < pWin->tw.ekey) {
82,000✔
1210
        *startPos = i;
20,500✔
1211
        return w;
20,500✔
1212
      }
1213
    }
1214
  }
1215

1216
  return -1;
41,000✔
1217
}
1218

1219
static int32_t extWinGetMultiTbOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
2,244,187✔
1220
  SExternalWindowOperator* pExtW = pOperator->info;
2,244,187✔
1221
  if (pExtW->blkWinIdx < 0) {
2,244,187✔
1222
    pExtW->blkWinIdx = extWinGetFirstWinFromTs(pOperator, pExtW, tsCol, pInfo->rows, startPos);
938,400✔
1223
    if (pExtW->blkWinIdx < 0) {
938,400✔
1224
      qDebug("%s %s blk TR[%" PRId64 ", %" PRId64 ") not in any win, skip block", 
41,000✔
1225
          GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1226
      *ppWin = NULL;
41,000✔
1227
      return TSDB_CODE_SUCCESS;
41,000✔
1228
    }
1229

1230
    extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
897,400✔
1231
    *ppWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
897,400✔
1232
    *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, *startPos, (*ppWin)->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
897,400✔
1233
    
1234
    qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
896,990✔
1235
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (*ppWin)->tw.skey, (*ppWin)->tw.ekey, *winRows, *startPos, tsCol[*startPos], tsCol[*startPos + *winRows - 1]);
1236
    
1237
    return TSDB_CODE_SUCCESS;
897,400✔
1238
  } else {
1239
    pExtW->blkWinIdx++;
1,306,195✔
1240
  }
1241

1242
  if (pExtW->blkWinIdx >= pExtW->pWins->size) {
1,306,195✔
1243
    qDebug("%s %s ext win blk idx %d reach the end, size: %d, skip block", 
897,198✔
1244
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (int32_t)pExtW->pWins->size);
1245
    *ppWin = NULL;
897,198✔
1246
    return TSDB_CODE_SUCCESS;
897,400✔
1247
  }
1248
  
1249
  SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
409,000✔
1250
  if (tsCol[pInfo->rows - 1] < pWin->tw.skey) {
409,000✔
1251
    qDebug("%s %s block end ts %" PRId64 " is small than curr win %d skey %" PRId64 ", skip block", 
×
1252
        GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[pInfo->rows - 1], pExtW->blkWinIdx, pWin->tw.skey);
1253
    *ppWin = NULL;
×
1254
    return TSDB_CODE_SUCCESS;
×
1255
  }
1256

1257
  int64_t r = 0;
409,000✔
1258

1259
  qDebug("%s %s start to get movlp win from winIdx %d rowIdx %d", GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pExtW->blkRowStartIdx);
409,000✔
1260

1261
  // TODO handle desc order
1262
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
470,500✔
1263
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
470,500✔
1264
    for (r = pExtW->blkRowStartIdx; r < pInfo->rows; ++r) {
6,147,412✔
1265
      if (tsCol[r] < pWin->tw.skey) {
6,147,412✔
1266
        pExtW->blkRowStartIdx = r + 1;
5,676,912✔
1267
        continue;
5,676,912✔
1268
      }
1269

1270
      if (tsCol[r] < pWin->tw.ekey) {
470,500✔
1271
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
409,000✔
1272
        *ppWin = pWin;
409,000✔
1273
        *startPos = r;
409,000✔
1274
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
409,000✔
1275

1276
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
409,000✔
1277
            GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, (int32_t)r, tsCol[r], tsCol[r + *winRows - 1]);
1278
        
1279
        return TSDB_CODE_SUCCESS;
409,000✔
1280
      }
1281

1282
      break;
61,500✔
1283
    }
1284

1285
    if (r >= pInfo->rows) {
61,500✔
1286
      break;
×
1287
    }
1288
  }
1289

1290
  qDebug("%s %s no more ext win in block, TR[%" PRId64 ", %" PRId64 "), skip it", 
×
1291
      GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1292

1293
  *ppWin = NULL;
×
1294
  return TSDB_CODE_SUCCESS;
×
1295
}
1296

1297

1298
static int32_t extWinGetWinStartPos(STimeWindow win, const SDataBlockInfo* pBlockInfo, int32_t lastEndPos, int32_t order, int32_t* nextPos, int64_t* tsCol) {
×
1299
  bool ascQuery = order == TSDB_ORDER_ASC;
×
1300

1301
  if (win.ekey <= pBlockInfo->window.skey && ascQuery) {
×
1302
    return -2;
×
1303
  }
1304
//if (win.skey > pBlockInfo->window.ekey && !ascQuery) return -2;
1305

1306
  if (win.skey > pBlockInfo->window.ekey && ascQuery) return -1;
×
1307
//if (win.ekey < pBlockInfo->window.skey && !ascQuery) return -1;
1308

1309
  while (true) {
1310
    if (win.ekey <= tsCol[lastEndPos + 1] && ascQuery) return -2;
×
1311
    if (win.skey <= tsCol[lastEndPos + 1] && ascQuery) break;
×
1312
    lastEndPos++;
×
1313
  }
1314

1315
  *nextPos = lastEndPos + 1;
×
1316
  return 0;
×
1317
}
1318

1319
static int32_t extWinAggSetWinOutputBuf(SOperatorInfo* pOperator, SExtWinTimeWindow* win, SExprSupp* pSupp, 
5,757,548✔
1320
                                     SAggSupporter* pAggSup, SExecTaskInfo* pTaskInfo) {
1321
  int32_t code = 0, lino = 0;
5,757,548✔
1322
  SResultRow* pResultRow = NULL;
5,757,548✔
1323
  SExternalWindowOperator* pExtW = (SExternalWindowOperator*)pOperator->info;
5,757,548✔
1324
  
1325
#if 0
1326
  SResultRow* pResultRow = doSetResultOutBufByKey(pAggSup->pResultBuf, pResultRowInfo, (char*)&win->skey, TSDB_KEYSIZE,
1327
                                                  true, tableGroupId, pTaskInfo, true, pAggSup, true);
1328
  if (pResultRow == NULL) {
1329
    qError("failed to set result output buffer, error:%s", tstrerror(pTaskInfo->code));
1330
    return pTaskInfo->code;
1331
  }
1332

1333
  qDebug("current result rows num:%d", tSimpleHashGetSize(pAggSup->pResultRowHashTable));
1334

1335
#else
1336
  if (win->winOutIdx >= 0) {
5,759,598✔
1337
    pResultRow = (SResultRow*)((char*)pExtW->pResultRow + win->winOutIdx * pAggSup->resultRowSize);
5,170,107✔
1338
  } else {
1339
    win->winOutIdx = pExtW->outWinIdx++;
589,696✔
1340
    
1341
    qDebug("set window [%" PRId64 ", %" PRId64 "] outIdx:%d", win->tw.skey, win->tw.ekey, win->winOutIdx);
589,696✔
1342

1343
    pResultRow = (SResultRow*)((char*)pExtW->pResultRow + win->winOutIdx * pAggSup->resultRowSize);
589,696✔
1344
    
1345
    memset(pResultRow, 0, pAggSup->resultRowSize);
589,696✔
1346

1347
    pResultRow->winIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
589,696✔
1348
    TAOS_SET_POBJ_ALIGNED(&pResultRow->win, &win->tw);
589,696✔
1349
  }
1350
#endif
1351

1352
  // set time window for current result
1353
  TAOS_CHECK_EXIT(setResultRowInitCtx(pResultRow, pSupp->pCtx, pSupp->numOfExprs, pSupp->rowEntryInfoOffset));
5,759,803✔
1354

1355
_exit:
5,758,778✔
1356
  
1357
  if (code) {
5,758,778✔
1358
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1359
  }
1360

1361
  return code;
5,758,778✔
1362
}
1363

1364
static int32_t extWinAggDo(SOperatorInfo* pOperator, int32_t startPos, int32_t forwardRows,
6,872,171✔
1365
                                  SSDataBlock* pInputBlock) {
1366
  if (pOperator->pTaskInfo->pStreamRuntimeInfo && forwardRows == 0) {
6,872,171✔
1367
    return TSDB_CODE_SUCCESS;
×
1368
  }
1369

1370
  SExprSupp*               pSup = &pOperator->exprSupp;
6,872,786✔
1371
  SExternalWindowOperator* pExtW = pOperator->info;
6,873,194✔
1372
  return applyAggFunctionOnPartialTuples(pOperator->pTaskInfo, pSup->pCtx, &pExtW->twAggSup.timeWindowData, startPos,
13,743,613✔
1373
                                         forwardRows, pInputBlock->info.rows, pSup->numOfExprs);
6,873,194✔
1374

1375
}
1376

1377
static bool extWinLastWinClosed(SExternalWindowOperator* pExtW) {
477✔
1378
  if (pExtW->outWinIdx <= 0 || (pExtW->multiTableMode && !pExtW->inputHasOrder)) {
477✔
1379
    return false;
477✔
1380
  }
1381

1382
  if (NULL == pExtW->timeRangeExpr || !pExtW->timeRangeExpr->needCalc) {
×
1383
    return true;
×
1384
  }
1385

1386
  SList* pList = taosArrayGetP(pExtW->pOutputBlocks, pExtW->outWinIdx - 1);
×
1387
  if (0 == listNEles(pList)) {
×
1388
    return true;
×
1389
  }
1390

1391
  SListNode* pNode = listTail(pList);
×
1392
  SArray* pBlkWinIdx = *((SArray**)pNode->data + 1);
×
1393
  int64_t* pIdx = taosArrayGetLast(pBlkWinIdx);
×
1394
  if (pIdx && *(int32_t*)pIdx < pExtW->blkWinStartIdx) {
×
1395
    return true;
×
1396
  }
1397

1398
  return false;
×
1399
}
1400

1401
static int32_t extWinGetWinResBlock(SOperatorInfo* pOperator, int32_t rows, SExtWinTimeWindow* pWin, SSDataBlock** ppRes, SArray** ppIdx) {
1,098✔
1402
  SExternalWindowOperator* pExtW = pOperator->info;
1,098✔
1403
  SList*                   pList = NULL;
1,098✔
1404
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
1,098✔
1405
  
1406
  if (pWin->winOutIdx >= 0) {
1,098✔
1407
    pList = taosArrayGetP(pExtW->pOutputBlocks, pWin->winOutIdx);
621✔
1408
  } else {
1409
    if (extWinLastWinClosed(pExtW)) {
477✔
1410
      pWin->winOutIdx = pExtW->outWinIdx - 1;
×
1411
      pList = taosArrayGetP(pExtW->pOutputBlocks, pWin->winOutIdx);
×
1412
    } else {
1413
      pWin->winOutIdx = pExtW->outWinIdx++;
477✔
1414
      pList = tdListNew(POINTER_BYTES * 2);
477✔
1415
      TSDB_CHECK_NULL(pList, code, lino, _exit, terrno);
477✔
1416
      SList** ppList = taosArrayGet(pExtW->pOutputBlocks, pWin->winOutIdx);
477✔
1417
      extWinRecycleBlockList(pExtW, ppList);
477✔
1418
      *ppList = pList;
477✔
1419
    }
1420
  }
1421
  
1422
  TAOS_CHECK_EXIT(extWinGetLastBlockFromList(pExtW, pList, rows, ppRes, ppIdx));
1,098✔
1423

1424
_exit:
1,098✔
1425

1426
  if (code) {
1,098✔
1427
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1428
  }
1429

1430
  return code;
1,098✔
1431
}
1432

1433
static int32_t extWinProjectDo(SOperatorInfo* pOperator, SSDataBlock* pInputBlock, int32_t startPos, int32_t rows, SExtWinTimeWindow* pWin) {
1,098✔
1434
  SExternalWindowOperator* pExtW = pOperator->info;
1,098✔
1435
  SExprSupp*               pExprSup = &pExtW->scalarSupp;
1,098✔
1436
  SSDataBlock*             pResBlock = NULL;
1,098✔
1437
  SArray*                  pIdx = NULL;
1,098✔
1438
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
1,098✔
1439
  
1440
  TAOS_CHECK_EXIT(extWinGetWinResBlock(pOperator, rows, pWin, &pResBlock, &pIdx));
1,098✔
1441

1442
  qDebug("%s %s win[%" PRId64 ", %" PRId64 "] got res block %p winRowIdx %p, winOutIdx:%d, capacity:%d", 
1,098✔
1443
      pOperator->pTaskInfo->id.str, __func__, pWin->tw.skey, pWin->tw.ekey, pResBlock, pIdx, pWin->winOutIdx, pResBlock->info.capacity);
1444
  
1445
  if (!pExtW->pTmpBlock) {
1,098✔
1446
    TAOS_CHECK_EXIT(createOneDataBlock(pInputBlock, false, &pExtW->pTmpBlock));
270✔
1447
  } else {
1448
    blockDataCleanup(pExtW->pTmpBlock);
828✔
1449
  }
1450
  
1451
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pExtW->pTmpBlock, TMAX(1, rows)));
1,098✔
1452

1453
  qDebug("%s %s start to copy %d rows to tmp blk", pOperator->pTaskInfo->id.str, __func__, rows);
1,098✔
1454
  TAOS_CHECK_EXIT(blockDataMergeNRows(pExtW->pTmpBlock, pInputBlock, startPos, rows));
1,098✔
1455

1456
  qDebug("%s %s start to apply project to tmp blk", pOperator->pTaskInfo->id.str, __func__);
1,098✔
1457
  TAOS_CHECK_EXIT(projectApplyFunctionsWithSelect(pExprSup->pExprInfo, pResBlock, pExtW->pTmpBlock, pExprSup->pCtx, pExprSup->numOfExprs,
1,098✔
1458
        NULL, GET_STM_RTINFO(pOperator->pTaskInfo), true, pExprSup->hasIndefRowsFunc));
1459

1460
  TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pIdx, pResBlock, extWinGetCurWinIdx(pOperator->pTaskInfo), rows));
1,098✔
1461

1462
_exit:
1,098✔
1463

1464
  if (code) {
1,098✔
1465
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1466
  } else {
1467
    qDebug("%s %s project succeed", pOperator->pTaskInfo->id.str, __func__);
1,098✔
1468
  }
1469
  
1470
  return code;
1,098✔
1471
}
1472

1473
static int32_t extWinProjectOpen(SOperatorInfo* pOperator, SSDataBlock* pInputBlock) {
684✔
1474
  SExternalWindowOperator* pExtW = pOperator->info;
684✔
1475
  int64_t*                 tsCol = extWinExtractTsCol(pInputBlock, pExtW->primaryTsIndex, pOperator->pTaskInfo);
684✔
1476
  SExtWinTimeWindow*       pWin = NULL;
684✔
1477
  bool                     ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
684✔
1478
  int32_t                  startPos = 0, winRows = 0;
684✔
1479
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
684✔
1480
  
1481
  while (true) {
1482
    TAOS_CHECK_EXIT((*pExtW->getWinFp)(pOperator, tsCol, &startPos, &pInputBlock->info, &pWin, &winRows));
1,782✔
1483
    if (pWin == NULL) {
1,782✔
1484
      break;
684✔
1485
    }
1486

1487
    qDebug("%s ext window [%" PRId64 ", %" PRId64 ") project start, ascScan:%d, startPos:%d, winRows:%d",
1,098✔
1488
           GET_TASKID(pOperator->pTaskInfo), pWin->tw.skey, pWin->tw.ekey, ascScan, startPos, winRows);        
1489
    
1490
    TAOS_CHECK_EXIT(extWinProjectDo(pOperator, pInputBlock, startPos, winRows, pWin));
1,098✔
1491
    
1492
    startPos += winRows;
1,098✔
1493
  }
1494
  
1495
_exit:
684✔
1496

1497
  if (code) {
684✔
1498
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
1499
  }
1500

1501
  return code;
684✔
1502
}
1503

1504
static int32_t extWinIndefRowsDoImpl(SOperatorInfo* pOperator, SSDataBlock* pRes, SSDataBlock* pBlock) {
×
1505
  SExternalWindowOperator* pExtW = pOperator->info;
×
1506
  SOptrBasicInfo*     pInfo = &pExtW->binfo;
×
1507
  SExprSupp*          pSup = &pOperator->exprSupp;
×
1508
  SExecTaskInfo*      pTaskInfo = pOperator->pTaskInfo;
×
1509
  int32_t order = pInfo->inputTsOrder;
×
1510
  int32_t scanFlag = pBlock->info.scanFlag;
×
1511
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
×
1512

1513
  SExprSupp* pScalarSup = &pExtW->scalarSupp;
×
1514
  if (pScalarSup->pExprInfo != NULL) {
×
1515
    TAOS_CHECK_EXIT(projectApplyFunctions(pScalarSup->pExprInfo, pBlock, pBlock, pScalarSup->pCtx, pScalarSup->numOfExprs,
×
1516
                                 pExtW->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo)));
1517
  }
1518

1519
  TAOS_CHECK_EXIT(setInputDataBlock(pSup, pBlock, order, scanFlag, false));
×
1520

1521
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pRes, pRes->info.rows + pBlock->info.rows));
×
1522

1523
  TAOS_CHECK_EXIT(projectApplyFunctions(pSup->pExprInfo, pRes, pBlock, pSup->pCtx, pSup->numOfExprs,
×
1524
                               pExtW->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo)));
1525

1526
_exit:
×
1527

1528
  if (code) {
×
1529
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __FUNCTION__, lino, tstrerror(code));
×
1530
  }
1531

1532
  return code;
×
1533
}
1534

1535
static int32_t extWinIndefRowsSetWinOutputBuf(SExternalWindowOperator* pExtW, SExtWinTimeWindow* win, SExprSupp* pSupp, 
×
1536
                                     SAggSupporter* pAggSup, SExecTaskInfo* pTaskInfo, bool reset) {
1537
  int32_t code = 0, lino = 0;
×
1538
  SResultRow* pResultRow = NULL;
×
1539

1540
  pResultRow = (SResultRow*)((char*)pExtW->pResultRow + win->winOutIdx * pAggSup->resultRowSize);
×
1541
  
1542
  qDebug("set window [%" PRId64 ", %" PRId64 "] outIdx:%d", win->tw.skey, win->tw.ekey, win->winOutIdx);
×
1543

1544
  if (reset) {
×
1545
    memset(pResultRow, 0, pAggSup->resultRowSize);
×
1546
    for (int32_t k = 0; k < pSupp->numOfExprs; ++k) {
×
1547
      SqlFunctionCtx* pCtx = &pSupp->pCtx[k];
×
1548
      pCtx->pOutput = NULL;
×
1549
    }
1550
  }
1551

1552
  TAOS_SET_POBJ_ALIGNED(&pResultRow->win, &win->tw);
×
1553

1554
  // set time window for current result
1555
  TAOS_CHECK_EXIT(setResultRowInitCtx(pResultRow, pSupp->pCtx, pSupp->numOfExprs, pSupp->rowEntryInfoOffset));
×
1556

1557
_exit:
×
1558
  
1559
  if (code) {
×
1560
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1561
  }
1562

1563
  return code;
×
1564
}
1565

1566
static int32_t extWinGetSetWinResBlockBuf(SOperatorInfo* pOperator, int32_t rows, SExtWinTimeWindow* pWin, SSDataBlock** ppRes, SArray** ppIdx) {
×
1567
  SExternalWindowOperator* pExtW = pOperator->info;
×
1568
  SList*                   pList = NULL;
×
1569
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
×
1570
  
1571
  if (pWin->winOutIdx >= 0) {
×
1572
    pList = taosArrayGetP(pExtW->pOutputBlocks, pWin->winOutIdx);
×
1573
    TAOS_CHECK_EXIT(extWinIndefRowsSetWinOutputBuf(pExtW, pWin, &pOperator->exprSupp, &pExtW->aggSup, pOperator->pTaskInfo, false));
×
1574
  } else {
1575
    if (extWinLastWinClosed(pExtW)) {
×
1576
      pWin->winOutIdx = pExtW->outWinIdx - 1;
×
1577
      pList = taosArrayGetP(pExtW->pOutputBlocks, pWin->winOutIdx);
×
1578
    } else {
1579
      pWin->winOutIdx = pExtW->outWinIdx++;
×
1580
      pList = tdListNew(POINTER_BYTES * 2);
×
1581
      TSDB_CHECK_NULL(pList, code, lino, _exit, terrno);
×
1582
      SList** ppList = taosArrayGet(pExtW->pOutputBlocks, pWin->winOutIdx);
×
1583
      extWinRecycleBlockList(pExtW, ppList);
×
1584
      *ppList = pList;
×
1585
    }
1586
    TAOS_CHECK_EXIT(extWinIndefRowsSetWinOutputBuf(pExtW, pWin, &pOperator->exprSupp, &pExtW->aggSup, pOperator->pTaskInfo, true));
×
1587
  }
1588
  
1589
  TAOS_CHECK_EXIT(extWinGetLastBlockFromList(pExtW, pList, rows, ppRes, ppIdx));
×
1590

1591
_exit:
×
1592

1593
  if (code) {
×
1594
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1595
  }
1596

1597
  return code;
×
1598
}
1599

1600

1601
static int32_t extWinIndefRowsDo(SOperatorInfo* pOperator, SSDataBlock* pInputBlock, int32_t startPos, int32_t rows, SExtWinTimeWindow* pWin) {
×
1602
  SExternalWindowOperator* pExtW = pOperator->info;
×
1603
  SSDataBlock*             pResBlock = NULL;
×
1604
  SArray*                  pIdx = NULL;
×
1605
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
×
1606
  
1607
  TAOS_CHECK_EXIT(extWinGetSetWinResBlockBuf(pOperator, rows, pWin, &pResBlock, &pIdx));
×
1608
  
1609
  if (!pExtW->pTmpBlock) {
×
1610
    TAOS_CHECK_EXIT(createOneDataBlock(pInputBlock, false, &pExtW->pTmpBlock));
×
1611
  } else {
1612
    blockDataCleanup(pExtW->pTmpBlock);
×
1613
  }
1614
  
1615
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pExtW->pTmpBlock, TMAX(1, rows)));
×
1616

1617
  TAOS_CHECK_EXIT(blockDataMergeNRows(pExtW->pTmpBlock, pInputBlock, startPos, rows));
×
1618
  TAOS_CHECK_EXIT(extWinIndefRowsDoImpl(pOperator, pResBlock, pExtW->pTmpBlock));
×
1619

1620
  TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pIdx, pResBlock, extWinGetCurWinIdx(pOperator->pTaskInfo), rows));
×
1621

1622
_exit:
×
1623

1624
  if (code) {
×
1625
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1626
  }
1627
  
1628
  return code;
×
1629
}
1630

1631

1632
static int32_t extWinIndefRowsOpen(SOperatorInfo* pOperator, SSDataBlock* pInputBlock) {
×
1633
  SExternalWindowOperator* pExtW = pOperator->info;
×
1634
  int64_t*                 tsCol = extWinExtractTsCol(pInputBlock, pExtW->primaryTsIndex, pOperator->pTaskInfo);
×
1635
  SExtWinTimeWindow*       pWin = NULL;
×
1636
  bool                     ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
×
1637
  int32_t                  startPos = 0, winRows = 0;
×
1638
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
×
1639
  
1640
  while (true) {
1641
    TAOS_CHECK_EXIT((*pExtW->getWinFp)(pOperator, tsCol, &startPos, &pInputBlock->info, &pWin, &winRows));
×
1642
    if (pWin == NULL) {
×
1643
      break;
×
1644
    }
1645

1646
    qDebug("%s ext window [%" PRId64 ", %" PRId64 ") indefRows start, ascScan:%d, startPos:%d, winRows:%d",
×
1647
           GET_TASKID(pOperator->pTaskInfo), pWin->tw.skey, pWin->tw.ekey, ascScan, startPos, winRows);        
1648
    
1649
    TAOS_CHECK_EXIT(extWinIndefRowsDo(pOperator, pInputBlock, startPos, winRows, pWin));
×
1650
    
1651
    startPos += winRows;
×
1652
  }
1653
  
1654
_exit:
×
1655

1656
  if (code) {
×
1657
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
1658
  }
1659

1660
  return code;
×
1661
}
1662

1663
static int32_t extWinNonAggOutputRes(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
477✔
1664
  SExternalWindowOperator* pExtW = pOperator->info;
477✔
1665
  int32_t                  numOfWin = pExtW->outWinIdx;
477✔
1666
  int32_t                  code = TSDB_CODE_SUCCESS;
477✔
1667
  int32_t                  lino = 0;
477✔
1668
  SSDataBlock*             pRes = NULL;
477✔
1669

1670
  for (; pExtW->outputWinId < numOfWin; pExtW->outputWinId++, extWinIncCurWinOutIdx(pOperator->pTaskInfo->pStreamRuntimeInfo)) {
477✔
1671
    SList* pList = taosArrayGetP(pExtW->pOutputBlocks, pExtW->outputWinId);
477✔
1672
    if (listNEles(pList) <= 0) {
477✔
1673
      continue;
×
1674
    }
1675

1676
    SListNode* pNode = tdListPopHead(pList);
477✔
1677
    pRes = *(SSDataBlock**)pNode->data;
477✔
1678
    pOperator->pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamBlkWinIdx = *(SArray**)((SArray**)pNode->data + 1);
477✔
1679
    pExtW->pLastBlkNode = pNode;
477✔
1680

1681
    if (listNEles(pList) <= 0) {
477✔
1682
      pExtW->outputWinId++;
477✔
1683
      extWinIncCurWinOutIdx(pOperator->pTaskInfo->pStreamRuntimeInfo);
477✔
1684
    }
1685

1686
    break;
477✔
1687
  }
1688

1689
  if (pRes) {
477✔
1690
    qDebug("%s result generated, rows:%" PRId64 , GET_TASKID(pOperator->pTaskInfo), pRes->info.rows);
477✔
1691
    pRes->info.version = pOperator->pTaskInfo->version;
477✔
1692
    pRes->info.dataLoad = 1;
477✔
1693
  } else {
1694
    pOperator->pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamBlkWinIdx = NULL;
×
1695
    qDebug("%s ext window done", GET_TASKID(pOperator->pTaskInfo));
×
1696
  }
1697

1698
  *ppRes = (pRes && pRes->info.rows > 0) ? pRes : NULL;
477✔
1699

1700
_exit:
477✔
1701

1702
  if (code != TSDB_CODE_SUCCESS) {
477✔
1703
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1704
  }
1705

1706
  return code;
477✔
1707
}
1708

1709
static int32_t extWinAggHandleEmptyWins(SOperatorInfo* pOperator, SSDataBlock* pBlock, bool allRemains, SExtWinTimeWindow* pWin) {
6,855,853✔
1710
  int32_t code = 0, lino = 0;
6,855,853✔
1711
  SExternalWindowOperator* pExtW = (SExternalWindowOperator*)pOperator->info;
6,855,853✔
1712
  SExprSupp* pSup = &pOperator->exprSupp;
6,856,878✔
1713
  int32_t currIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
6,857,083✔
1714

1715
  if (NULL == pExtW->pEmptyInputBlock || (pWin && pWin->tw.skey == pExtW->lastSKey)) {
6,857,698✔
1716
    goto _exit;
1,170,411✔
1717
  }
1718

1719
  bool ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
5,686,877✔
1720
  int32_t endIdx = allRemains ? (pExtW->pWins->size - 1) : (currIdx - 1);
5,687,492✔
1721
  SResultRowInfo* pResultRowInfo = &pExtW->binfo.resultRowInfo;
5,687,492✔
1722
  SSDataBlock* pInput = pExtW->pEmptyInputBlock;
5,687,287✔
1723

1724
  if ((pExtW->lastWinId + 1) <= endIdx) {
5,687,287✔
1725
    TAOS_CHECK_EXIT(setInputDataBlock(pSup, pExtW->pEmptyInputBlock, pExtW->binfo.inputTsOrder, MAIN_SCAN, true));
140,270✔
1726
  }
1727
  
1728
  for (int32_t i = pExtW->lastWinId + 1; i <= endIdx; ++i) {
5,888,019✔
1729
    SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, i);
200,322✔
1730

1731
    extWinSetCurWinIdx(pOperator, i);
200,322✔
1732
    qDebug("%s %dth ext empty window start:%" PRId64 ", end:%" PRId64 ", ascScan:%d",
200,322✔
1733
           GET_TASKID(pOperator->pTaskInfo), i, pWin->tw.skey, pWin->tw.ekey, ascScan);
1734

1735
    TAOS_CHECK_EXIT(extWinAggSetWinOutputBuf(pOperator, pWin, pSup, &pExtW->aggSup, pOperator->pTaskInfo));
200,322✔
1736

1737
    updateTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pWin->tw, 1);
200,322✔
1738
    code = extWinAggDo(pOperator, 0, 1, pInput);
200,322✔
1739
    pExtW->lastWinId = i;  
200,322✔
1740
    TAOS_CHECK_EXIT(code);
200,322✔
1741
  }
1742

1743
  
1744
_exit:
5,685,442✔
1745

1746
  if (code) {
6,857,083✔
1747
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __FUNCTION__, lino, tstrerror(code));
×
1748
  } else {
1749
    if (pBlock) {
6,857,083✔
1750
      TAOS_CHECK_EXIT(setInputDataBlock(pSup, pBlock, pExtW->binfo.inputTsOrder, pBlock->info.scanFlag, true));
6,670,820✔
1751
    }
1752

1753
    if (!allRemains) {
6,854,213✔
1754
      extWinSetCurWinIdx(pOperator, currIdx);  
6,669,385✔
1755
    }
1756
  }
1757

1758
  return code;
6,855,445✔
1759
}
1760

1761
static int32_t extWinAggOpen(SOperatorInfo* pOperator, SSDataBlock* pInputBlock) {
2,227,609✔
1762
  SExternalWindowOperator* pExtW = (SExternalWindowOperator*)pOperator->info;
2,227,609✔
1763
  int32_t                  startPos = 0, winRows = 0;
2,227,609✔
1764
  int64_t*                 tsCol = extWinExtractTsCol(pInputBlock, pExtW->primaryTsIndex, pOperator->pTaskInfo);
2,227,609✔
1765
  bool                     ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
2,227,404✔
1766
  int32_t                  code = 0, lino = 0;
2,227,404✔
1767
  SExtWinTimeWindow*       pWin = NULL;
2,227,404✔
1768
  bool                     scalarCalc = false;
2,227,404✔
1769

1770
  while (true) {
1771
    TAOS_CHECK_EXIT((*pExtW->getWinFp)(pOperator, tsCol, &startPos, &pInputBlock->info, &pWin, &winRows));
8,898,224✔
1772
    if (pWin == NULL) {
8,899,249✔
1773
      break;
2,227,404✔
1774
    }
1775

1776
    TAOS_CHECK_EXIT(extWinAggHandleEmptyWins(pOperator, pInputBlock, false, pWin));
6,671,845✔
1777

1778
    qDebug("%s ext window [%" PRId64 ", %" PRId64 ") agg start, ascScan:%d, startPos:%d, winRows:%d",
6,669,797✔
1779
           GET_TASKID(pOperator->pTaskInfo), pWin->tw.skey, pWin->tw.ekey, ascScan, startPos, winRows);        
1780

1781
    if (!scalarCalc) {
6,673,485✔
1782
      if (pExtW->scalarSupp.pExprInfo) {
2,185,412✔
1783
        SExprSupp* pScalarSup = &pExtW->scalarSupp;
×
1784
        TAOS_CHECK_EXIT(projectApplyFunctions(pScalarSup->pExprInfo, pInputBlock, pInputBlock, pScalarSup->pCtx, pScalarSup->numOfExprs,
×
1785
                                     pExtW->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo)));
1786
      }
1787
      
1788
      scalarCalc = true;
2,185,207✔
1789
    }
1790

1791
    if (pWin->tw.skey != pExtW->lastSKey || pWin->tw.skey == INT64_MIN) {
6,673,280✔
1792
      TAOS_CHECK_EXIT(extWinAggSetWinOutputBuf(pOperator, pWin, &pOperator->exprSupp, &pExtW->aggSup, pOperator->pTaskInfo));
5,559,274✔
1793
    }
1794
    
1795
    updateTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pWin->tw, 1);
6,671,847✔
1796
    TAOS_CHECK_EXIT(extWinAggDo(pOperator, startPos, winRows, pInputBlock));
6,672,054✔
1797
    
1798
    pExtW->lastSKey = pWin->tw.skey;
6,670,002✔
1799
    pExtW->lastWinId = extWinGetCurWinIdx(pOperator->pTaskInfo);
6,670,617✔
1800
    startPos += winRows;
6,670,820✔
1801
  }
1802

1803
_exit:
2,227,404✔
1804

1805
  if (code) {
2,227,404✔
1806
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
1807
  }
1808

1809
  return code;
2,227,404✔
1810
}
1811

1812
static int32_t extWinAggOutputRes(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
365,544✔
1813
  SExternalWindowOperator* pExtW = pOperator->info;
365,544✔
1814
  SExecTaskInfo*           pTaskInfo = pOperator->pTaskInfo;
365,544✔
1815
  SSDataBlock*             pBlock = pExtW->binfo.pRes;
365,544✔
1816
  int32_t                  code = TSDB_CODE_SUCCESS;
365,544✔
1817
  int32_t                  lino = 0;
365,544✔
1818
  SExprInfo*               pExprInfo = pOperator->exprSupp.pExprInfo;
365,544✔
1819
  int32_t                  numOfExprs = pOperator->exprSupp.numOfExprs;
365,544✔
1820
  int32_t*                 rowEntryOffset = pOperator->exprSupp.rowEntryInfoOffset;
365,544✔
1821
  SqlFunctionCtx*          pCtx = pOperator->exprSupp.pCtx;
365,544✔
1822
  int32_t                  numOfWin = pExtW->outWinIdx;
365,544✔
1823

1824
  pBlock->info.version = pTaskInfo->version;
365,544✔
1825
  blockDataCleanup(pBlock);
365,544✔
1826
  taosArrayClear(pExtW->pWinRowIdx);
365,544✔
1827

1828
  for (; pExtW->outputWinId < pExtW->pWins->size; pExtW->outputWinId += 1) {
963,256✔
1829
    SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->outputWinId);
597,712✔
1830
    int32_t            winIdx = pWin->winOutIdx;
597,712✔
1831
    if (winIdx < 0) {
597,712✔
1832
      continue;
8,016✔
1833
    }
1834

1835
    pExtW->outputWinNum++;
589,696✔
1836
    SResultRow* pRow = (SResultRow*)((char*)pExtW->pResultRow + winIdx * pExtW->aggSup.resultRowSize);
589,696✔
1837

1838
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
589,491✔
1839

1840
    // no results, continue to check the next one
1841
    if (pRow->numOfRows == 0) {
589,491✔
1842
      continue;
×
1843
    }
1844

1845
    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
589,491✔
1846
      uint32_t newSize = pBlock->info.rows + pRow->numOfRows + numOfWin - pExtW->outputWinNum;
84,550✔
1847
      TAOS_CHECK_EXIT(blockDataEnsureCapacity(pBlock, newSize));
84,550✔
1848
      qDebug("datablock capacity not sufficient, expand to required:%d, current capacity:%d, %s", newSize,
84,550✔
1849
             pBlock->info.capacity, GET_TASKID(pTaskInfo));
1850
    }
1851

1852
    TAOS_CHECK_EXIT(copyResultrowToDataBlock(pExprInfo, numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo));
589,696✔
1853

1854
    pBlock->info.rows += pRow->numOfRows;
589,696✔
1855

1856
    TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pExtW->pWinRowIdx, pBlock, pRow->winIdx, pRow->numOfRows));
589,696✔
1857

1858
    if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
589,696✔
1859
      break;
×
1860
    }
1861
  }
1862

1863
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
365,544✔
1864
         pBlock->info.id.groupId);
1865
         
1866
  pBlock->info.dataLoad = 1;
365,544✔
1867

1868
  *ppRes = (pBlock->info.rows > 0) ? pBlock : NULL;
365,544✔
1869

1870
  if (*ppRes) {
365,544✔
1871
    (*ppRes)->info.window.skey = pExtW->orgTableTimeRange.skey;
182,983✔
1872
    (*ppRes)->info.window.ekey = pExtW->orgTableTimeRange.ekey;
182,983✔
1873
  }
1874
  if (pOperator->pTaskInfo->pStreamRuntimeInfo) {
365,544✔
1875
    pOperator->pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamBlkWinIdx = pExtW->pWinRowIdx;
365,544✔
1876
  }
1877

1878
_exit:
×
1879

1880
  if (code != TSDB_CODE_SUCCESS) {
365,544✔
1881
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1882
  }
1883

1884
  return code;
365,544✔
1885
}
1886

1887
static int32_t extWinInitResultRow(SExecTaskInfo*        pTaskInfo, SExternalWindowOperator* pExtW, int32_t winNum) {
185,303✔
1888
  if (EEXT_MODE_SCALAR == pExtW->mode) {
185,303✔
1889
    return TSDB_CODE_SUCCESS;
270✔
1890
  }
1891

1892
  if (winNum <= pExtW->resultRowCapacity) {
185,033✔
1893
    return TSDB_CODE_SUCCESS;
99,253✔
1894
  }
1895
  
1896
  taosMemoryFreeClear(pExtW->pResultRow);
85,780✔
1897
  pExtW->resultRowCapacity = -1;
85,780✔
1898

1899
  int32_t code = 0, lino = 0;
85,780✔
1900
  
1901
  pExtW->pResultRow = taosMemoryCalloc(winNum, pExtW->aggSup.resultRowSize);
85,780✔
1902
  QUERY_CHECK_NULL(pExtW->pResultRow, code, lino, _exit, terrno);
85,780✔
1903

1904
  pExtW->resultRowCapacity = winNum;
85,780✔
1905

1906
_exit:
85,780✔
1907

1908
  if (code) {
85,780✔
1909
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1910
  }
1911

1912
  return code;
85,780✔
1913
}
1914

1915
static void extWinFreeResultRow(SExternalWindowOperator* pExtW) {
182,561✔
1916
  if (pExtW->resultRowCapacity * pExtW->aggSup.resultRowSize >= 1048576) {
182,561✔
1917
    taosMemoryFreeClear(pExtW->pResultRow);
×
1918
    pExtW->resultRowCapacity = -1;
×
1919
  }
1920
  if (pExtW->binfo.pRes && pExtW->binfo.pRes->info.rows * pExtW->aggSup.resultRowSize >= 1048576) {
182,561✔
1921
    blockDataFreeCols(pExtW->binfo.pRes);
×
1922
  }
1923
}
182,561✔
1924

1925
static int32_t extWinInitWindowList(SExternalWindowOperator* pExtW, SExecTaskInfo*        pTaskInfo) {
184,896✔
1926
  if (taosArrayGetSize(pExtW->pWins) > 0) {
184,896✔
1927
    return TSDB_CODE_SUCCESS;
×
1928
  }
1929
  
1930
  int32_t code = 0, lino = 0;
185,303✔
1931
  SStreamRuntimeFuncInfo* pInfo = &pTaskInfo->pStreamRuntimeInfo->funcInfo;
185,303✔
1932
  size_t size = taosArrayGetSize(pInfo->pStreamPesudoFuncVals);
185,303✔
1933
  SExtWinTimeWindow* pWin = taosArrayReserve(pExtW->pWins, size);
185,303✔
1934
  TSDB_CHECK_NULL(pWin, code, lino, _exit, terrno);
185,303✔
1935

1936
  TAOS_CHECK_EXIT(extWinInitResultRow(pTaskInfo, pExtW, size));
185,303✔
1937

1938
  if (pExtW->timeRangeExpr && pExtW->timeRangeExpr->needCalc) {
185,303✔
1939
    TAOS_CHECK_EXIT(scalarCalculateExtWinsTimeRange(pExtW->timeRangeExpr, pInfo, pWin));
80,766✔
1940
    if (qDebugFlag & DEBUG_DEBUG) {
80,563✔
1941
      for (int32_t i = 0; i < size; ++i) {
256,070✔
1942
        qDebug("%s the %d/%d ext window calced initialized, TR[%" PRId64 ", %" PRId64 ")", 
175,304✔
1943
            pTaskInfo->id.str, i, (int32_t)size, pWin[i].tw.skey, pWin[i].tw.ekey);
1944
      }
1945
    }
1946
  } else {
1947
    for (int32_t i = 0; i < size; ++i) {
527,219✔
1948
      SSTriggerCalcParam* pParam = taosArrayGet(pInfo->pStreamPesudoFuncVals, i);
422,682✔
1949

1950
      pWin[i].tw.skey = pParam->wstart;
422,682✔
1951
      pWin[i].tw.ekey = pParam->wend + ((pInfo->triggerType != STREAM_TRIGGER_SLIDING) ? 1 : 0);
422,682✔
1952
      pWin[i].winOutIdx = -1;
422,682✔
1953

1954
      qDebug("%s the %d/%d ext window initialized, TR[%" PRId64 ", %" PRId64 ")", 
422,682✔
1955
          pTaskInfo->id.str, i, (int32_t)size, pWin[i].tw.skey, pWin[i].tw.ekey);
1956
    }
1957
  }
1958
  
1959
  pExtW->outputWinId = pInfo->curIdx;
185,303✔
1960
  pExtW->lastWinId = -1;
185,303✔
1961
  pExtW->blkWinStartIdx = pInfo->curIdx;
185,303✔
1962

1963
_exit:
185,303✔
1964

1965
  if (code) {
185,303✔
1966
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1967
  }
1968

1969
  return code;
185,303✔
1970
}
1971

1972
static bool extWinNonAggGotResBlock(SExternalWindowOperator* pExtW) {
684✔
1973
  if (pExtW->multiTableMode && !pExtW->inputHasOrder) {
684✔
1974
    return false;
621✔
1975
  }
1976
  int32_t remainWin = pExtW->outWinIdx - pExtW->outputWinId;
63✔
1977
  if (remainWin > 1 && (NULL == pExtW->timeRangeExpr || !pExtW->timeRangeExpr->needCalc)) {
63✔
1978
    return true;
×
1979
  }
1980
  
1981
  SList* pList = taosArrayGetP(pExtW->pOutputBlocks, pExtW->outputWinId);
63✔
1982
  if (!pList || listNEles(pList) <= 0) {
63✔
1983
    return false;
×
1984
  }
1985
  if (listNEles(pList) > 1) {
63✔
1986
    return true;
×
1987
  }
1988

1989
  SListNode* pNode = listHead(pList);
63✔
1990
  SArray* pIdx = *(SArray**)((SArray**)pNode->data + 1);
63✔
1991
  int32_t* winIdx = taosArrayGetLast(pIdx);
63✔
1992
  if (winIdx && *winIdx < pExtW->blkWinStartIdx) {
63✔
1993
    return true;
×
1994
  }
1995

1996
  return false;
63✔
1997
}
1998

1999
static int32_t getTimeWindowOfBlock(SSDataBlock *pBlock, col_id_t tsSlotId, int64_t *startTs, int64_t *endTs) {
×
2000
  int32_t code = TSDB_CODE_SUCCESS;
×
2001
  int32_t lino = 0;
×
2002
  int32_t tsIndex = -1;
×
2003
  for (int32_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {
×
2004
    SColumnInfoData *pCol = (SColumnInfoData*)taosArrayGet(pBlock->pDataBlock, i);
×
2005
    QUERY_CHECK_NULL(pCol, code, lino, _return, terrno)
×
2006
    if (pCol->info.colId == tsSlotId) {
×
2007
      tsIndex = i;
×
2008
      break;
×
2009
    }
2010
  }
2011

2012
  if (tsIndex == -1) {
×
2013
    tsIndex = (int32_t)taosArrayGetSize(pBlock->pDataBlock) - 1;
×
2014
  }
2015

2016
  SColumnInfoData *pColData = (SColumnInfoData*)taosArrayGet(pBlock->pDataBlock, tsIndex);
×
2017
  QUERY_CHECK_NULL(pColData, code, lino, _return, terrno)
×
2018

2019
  GET_TYPED_DATA(*startTs, int64_t, TSDB_DATA_TYPE_TIMESTAMP, colDataGetNumData(pColData, 0), 0);
×
2020
  GET_TYPED_DATA(*endTs, int64_t, TSDB_DATA_TYPE_TIMESTAMP, colDataGetNumData(pColData, pBlock->info.rows - 1), 0);
×
2021

2022
  return code;
×
2023
_return:
×
2024
  qError("failed to get time window of block, %s code:%s, line:%d", __func__, tstrerror(code), lino);
×
2025
  return code;
×
2026
}
2027

2028
static int32_t extWinOpen(SOperatorInfo* pOperator) {
185,303✔
2029
  if (OPTR_IS_OPENED(pOperator) && !pOperator->pOperatorGetParam) {
185,303✔
2030
    return TSDB_CODE_SUCCESS;
×
2031
  }
2032
  
2033
  int32_t                  code = 0;
185,303✔
2034
  int32_t                  lino = 0;
185,303✔
2035
  SExecTaskInfo*           pTaskInfo = pOperator->pTaskInfo;
185,303✔
2036
  SOperatorInfo*           pDownstream = pOperator->pDownstream[0];
185,303✔
2037
  SExternalWindowOperator* pExtW = pOperator->info;
185,303✔
2038
  SExprSupp*               pSup = &pOperator->exprSupp;
185,098✔
2039

2040
  if (pOperator->pOperatorGetParam) {
185,098✔
2041
    SOperatorParam*               pParam = (SOperatorParam*)(pOperator->pOperatorGetParam);
×
2042
    SOperatorParam*               pDownParam = (SOperatorParam*)(pOperator->pDownstreamGetParams[0]);
×
2043
    SExchangeOperatorParam*       pExecParam = NULL;
×
2044
    SExternalWindowOperatorParam* pExtPram = (SExternalWindowOperatorParam*)pParam->value;
×
2045

2046
    if (pExtW->pWins) {
×
2047
      taosArrayDestroy(pExtW->pWins);
×
2048
    }
2049

2050
    pExtW->pWins = pExtPram->ExtWins;
×
2051

2052
    TAOS_CHECK_EXIT(extWinInitResultRow(pTaskInfo, pExtW, taosArrayGetSize(pExtW->pWins)));
×
2053
    pExtPram->ExtWins = NULL;
×
2054
    pExtW->outputWinId = 0;
×
2055
    pExtW->lastWinId = -1;
×
2056
    pExtW->blkWinStartIdx = 0;
×
2057
    pExtW->outWinIdx = 0;
×
2058
    pExtW->lastSKey = INT64_MIN;
×
2059
    pExtW->isDynWindow = true;
×
2060
    pExtW->orgTableTimeRange.skey = INT64_MAX;
×
2061
    pExtW->orgTableTimeRange.ekey = INT64_MIN;
×
2062

2063
    QUERY_CHECK_CONDITION(pOperator->numOfDownstream == 1, code, lino, _exit, TSDB_CODE_INVALID_PARA)
×
2064

2065
    switch (pDownParam->opType) {
×
2066
      case QUERY_NODE_PHYSICAL_PLAN_DYN_QUERY_CTRL: {
×
2067
        break;
×
2068
      }
2069
      case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE: {
×
2070
        pExecParam = (SExchangeOperatorParam*)((SOperatorParam*)(pOperator->pDownstreamGetParams[0]))->value;
×
2071
        pExecParam->basic.vgId = pExtW->orgTableVgId;
×
2072
        taosArrayClear(pExecParam->basic.uidList);
×
2073
        QUERY_CHECK_NULL(taosArrayPush(pExecParam->basic.uidList, &pExtW->orgTableUid), code, lino, _exit, terrno)
×
2074
        break;
×
2075
      }
2076
      default:
×
2077
        QUERY_CHECK_CONDITION(false, code, lino, _exit, TSDB_CODE_INVALID_PARA)
×
2078
    }
2079
  } else {
2080
    TAOS_CHECK_EXIT(extWinInitWindowList(pExtW, pTaskInfo));
185,098✔
2081
  }
2082

2083
  while (1) {
2,228,088✔
2084
    pExtW->blkWinIdx = -1;
2,413,391✔
2085
    pExtW->blkWinStartSet = false;
2,413,596✔
2086
    pExtW->blkRowStartIdx = 0;
2,413,596✔
2087

2088
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
2,413,596✔
2089
    if (pBlock == NULL) {
2,413,596✔
2090
      if (EEXT_MODE_AGG == pExtW->mode) {
185,303✔
2091
        TAOS_CHECK_EXIT(extWinAggHandleEmptyWins(pOperator, pBlock, true, NULL));
185,033✔
2092
      }
2093
      pExtW->blkWinStartIdx = pExtW->pWins->size;
185,303✔
2094
      break;
185,303✔
2095
    }
2096

2097
    if (pExtW->isDynWindow) {
2,228,293✔
2098
      TSKEY skey = 0;
×
2099
      TSKEY ekey = 0;
×
2100
      code = getTimeWindowOfBlock(pBlock, pExtW->primaryTsIndex, &skey, &ekey);
×
2101
      QUERY_CHECK_CODE(code, lino, _exit);
×
2102
      pExtW->orgTableTimeRange.skey = TMIN(pExtW->orgTableTimeRange.skey, skey);
×
2103
      pExtW->orgTableTimeRange.ekey = TMAX(pExtW->orgTableTimeRange.ekey, ekey);
×
2104
    }
2105

2106
    printDataBlock(pBlock, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
2,228,293✔
2107

2108
    qDebug("ext window mode:%d got %" PRId64 " rows from downstream", pExtW->mode, pBlock->info.rows);
2,228,088✔
2109
    
2110
    switch (pExtW->mode) {
2,228,293✔
2111
      case EEXT_MODE_SCALAR:
684✔
2112
        TAOS_CHECK_EXIT(extWinProjectOpen(pOperator, pBlock));
684✔
2113
        if (extWinNonAggGotResBlock(pExtW)) {
684✔
2114
          return code;
×
2115
        }
2116
        break;
684✔
2117
      case EEXT_MODE_AGG:
2,227,199✔
2118
        TAOS_CHECK_EXIT(extWinAggOpen(pOperator, pBlock));
2,227,199✔
2119
        break;
2,227,404✔
2120
      case EEXT_MODE_INDEFR_FUNC:
×
2121
        TAOS_CHECK_EXIT(extWinIndefRowsOpen(pOperator, pBlock));
×
2122
        if (extWinNonAggGotResBlock(pExtW)) {
×
2123
          return code;
×
2124
        }
2125
        break;
×
2126
      default:
×
2127
        break;
×
2128
    }
2129
  }
2130

2131
  if (pOperator->pOperatorGetParam) {
185,303✔
2132
    freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
×
2133
    pOperator->pOperatorGetParam = NULL;
×
2134
  }
2135
  OPTR_SET_OPENED(pOperator);
185,303✔
2136

2137
#if 0
2138
  if (pExtW->mode == EEXT_MODE_AGG) {
2139
    qDebug("ext window before dump final rows num:%d", tSimpleHashGetSize(pExtW->aggSup.pResultRowHashTable));
2140

2141
    code = initGroupedResultInfo(&pExtW->groupResInfo, pExtW->aggSup.pResultRowHashTable, pExtW->binfo.inputTsOrder);
2142
    QUERY_CHECK_CODE(code, lino, _exit);
2143

2144
    qDebug("ext window after dump final rows num:%d", tSimpleHashGetSize(pExtW->aggSup.pResultRowHashTable));
2145
  }
2146
#endif
2147

2148
_exit:
185,303✔
2149

2150
  if (code != 0) {
185,303✔
2151
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
2152
    pTaskInfo->code = code;
×
2153
    T_LONG_JMP(pTaskInfo->env, code);
×
2154
  }
2155
  
2156
  return code;
185,303✔
2157
}
2158

2159
static int32_t extWinNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
366,021✔
2160
  int32_t                  code = 0;
366,021✔
2161
  int32_t                  lino = 0;
366,021✔
2162
  SExternalWindowOperator* pExtW = pOperator->info;
366,021✔
2163
  SExecTaskInfo*           pTaskInfo = pOperator->pTaskInfo;
366,021✔
2164

2165
  if (pOperator->status == OP_EXEC_DONE && !pOperator->pOperatorGetParam) {
366,021✔
2166
    *ppRes = NULL;
×
2167
    return code;
×
2168
  }
2169

2170
  extWinRecycleBlkNode(pExtW, &pExtW->pLastBlkNode);
366,021✔
2171

2172
  if (pOperator->status == OP_NOT_OPENED) {
366,021✔
2173
    TAOS_CHECK_EXIT(pOperator->fpSet._openFn(pOperator));
185,303✔
2174
  }
2175

2176
  if (pExtW->mode == EEXT_MODE_SCALAR || pExtW->mode == EEXT_MODE_INDEFR_FUNC) {
366,021✔
2177
    TAOS_CHECK_EXIT(extWinNonAggOutputRes(pOperator, ppRes));
477✔
2178
    if (NULL == *ppRes) {
477✔
2179
      setOperatorCompleted(pOperator);
×
2180
      extWinFreeResultRow(pExtW);
×
2181
    }
2182
  } else {
2183
#if 0    
2184
    doBuildResultDatablock(pOperator, &pExtW->binfo, &pExtW->groupResInfo, pExtW->aggSup.pResultBuf);
2185
    bool hasRemain = hasRemainResults(&pExtW->groupResInfo);
2186
    if (!hasRemain) {
2187
      setOperatorCompleted(pOperator);
2188
      break;
2189
    }
2190
    if (pExtW->binfo.pRes->info.rows > 0) break;
2191
#else
2192
    TAOS_CHECK_EXIT(extWinAggOutputRes(pOperator, ppRes));
365,544✔
2193
    if (NULL == *ppRes) {
365,544✔
2194
      setOperatorCompleted(pOperator);
182,561✔
2195
      if (pTaskInfo->pStreamRuntimeInfo) {
182,561✔
2196
        extWinFreeResultRow(pExtW);
182,561✔
2197
      }
2198
    }
2199
#endif      
2200
  }
2201

2202
  if (*ppRes) {
366,021✔
2203
    pOperator->resultInfo.totalRows += (*ppRes)->info.rows;
183,460✔
2204
    printDataBlock(*ppRes, __func__, GET_TASKID(pTaskInfo), pTaskInfo->id.queryId);
183,460✔
2205
  }
2206
  
2207
_exit:
182,561✔
2208

2209
  if (code) {
366,021✔
2210
    qError("%s %s failed at line %d since %s", GET_TASKID(pTaskInfo), __func__, lino, tstrerror(code));
×
2211
    pTaskInfo->code = code;
×
2212
    T_LONG_JMP(pTaskInfo->env, code);
×
2213
  }
2214

2215
  if ((*ppRes) && (*ppRes)->info.rows <= 0) {
366,021✔
2216
    *ppRes = NULL;
×
2217
  }
2218

2219
  if (pTaskInfo->execModel == OPTR_EXEC_MODEL_STREAM && (*ppRes)) {
366,021✔
2220
    printDataBlock(*ppRes, getStreamOpName(pOperator->operatorType), GET_TASKID(pTaskInfo), pTaskInfo->id.queryId);
183,460✔
2221
  }
2222
  
2223
  return code;
365,816✔
2224
}
2225

2226

2227
int32_t createExternalWindowOperator(SOperatorInfo* pDownstream, SPhysiNode* pNode, SExecTaskInfo* pTaskInfo,
75,965✔
2228
                                     SOperatorInfo** pOptrOut) {
2229
  SExternalWindowPhysiNode* pPhynode = (SExternalWindowPhysiNode*)pNode;
75,965✔
2230
  QRY_PARAM_CHECK(pOptrOut);
75,965✔
2231
  int32_t                  code = 0;
75,965✔
2232
  int32_t                  lino = 0;
75,965✔
2233
  SExternalWindowOperator* pExtW = taosMemoryCalloc(1, sizeof(SExternalWindowOperator));
75,965✔
2234
  SOperatorInfo*           pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
75,965✔
2235
  pOperator->pPhyNode = pNode;
75,965✔
2236
  if (!pExtW || !pOperator) {
75,965✔
2237
    code = terrno;
×
2238
    lino = __LINE__;
×
2239
    goto _error;
×
2240
  }
2241
  
2242
  setOperatorInfo(pOperator, "ExternalWindowOperator", QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW, true, OP_NOT_OPENED,
75,965✔
2243
                  pExtW, pTaskInfo);
2244
                  
2245
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pPhynode->window.node.pOutputDataBlockDesc);
75,965✔
2246
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
75,965✔
2247
  initBasicInfo(&pExtW->binfo, pResBlock);
75,965✔
2248

2249
  pExtW->primaryTsIndex = ((SColumnNode*)pPhynode->window.pTspk)->slotId;
75,965✔
2250
  pExtW->mode = pPhynode->window.pProjs ? EEXT_MODE_SCALAR : (pPhynode->window.indefRowsFunc ? EEXT_MODE_INDEFR_FUNC : EEXT_MODE_AGG);
75,965✔
2251
  pExtW->binfo.inputTsOrder = pPhynode->window.node.inputTsOrder = TSDB_ORDER_ASC;
75,965✔
2252
  pExtW->binfo.outputTsOrder = pExtW->binfo.inputTsOrder;
75,965✔
2253
  pExtW->isDynWindow = false;
75,965✔
2254

2255
  if (pTaskInfo->pStreamRuntimeInfo != NULL){
75,965✔
2256
    pTaskInfo->pStreamRuntimeInfo->funcInfo.withExternalWindow = true;
75,965✔
2257
  }
2258

2259
  // pExtW->limitInfo = (SLimitInfo){0};
2260
  // initLimitInfo(pPhynode->window.node.pLimit, pPhynode->window.node.pSlimit, &pExtW->limitInfo);
2261

2262
  if (pPhynode->window.pProjs) {
75,965✔
2263
    int32_t    numOfScalarExpr = 0;
270✔
2264
    SExprInfo* pScalarExprInfo = NULL;
270✔
2265
    code = createExprInfo(pPhynode->window.pProjs, NULL, &pScalarExprInfo, &numOfScalarExpr);
270✔
2266
    QUERY_CHECK_CODE(code, lino, _error);
270✔
2267

2268
    code = initExprSupp(&pExtW->scalarSupp, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
270✔
2269
    QUERY_CHECK_CODE(code, lino, _error);
270✔
2270

2271
  //if (pExtW->multiTableMode) {
2272
    pExtW->pOutputBlocks = taosArrayInit_s(POINTER_BYTES, STREAM_CALC_REQ_MAX_WIN_NUM);
270✔
2273
    if (!pExtW->pOutputBlocks) QUERY_CHECK_CODE(terrno, lino, _error);
270✔
2274
  //}
2275
    pExtW->pFreeBlocks = tdListNew(POINTER_BYTES * 2);
270✔
2276
    QUERY_CHECK_NULL(pExtW->pFreeBlocks, code, lino, _error, terrno);
270✔
2277
  } else if (pExtW->mode == EEXT_MODE_AGG) {
75,490✔
2278
    if (pPhynode->window.pExprs != NULL) {
75,695✔
2279
      int32_t    num = 0;
×
2280
      SExprInfo* pSExpr = NULL;
×
2281
      code = createExprInfo(pPhynode->window.pExprs, NULL, &pSExpr, &num);
×
2282
      QUERY_CHECK_CODE(code, lino, _error);
×
2283
    
2284
      code = initExprSupp(&pExtW->scalarSupp, pSExpr, num, &pTaskInfo->storageAPI.functionStore);
×
2285
      if (code != TSDB_CODE_SUCCESS) {
×
2286
        goto _error;
×
2287
      }
2288
      checkIndefRowsFuncs(&pExtW->scalarSupp);
×
2289
    }
2290
    
2291
    size_t keyBufSize = sizeof(int64_t) * 2 + POINTER_BYTES;
75,695✔
2292
    initResultSizeInfo(&pOperator->resultInfo, 4096);
75,695✔
2293
    //code = blockDataEnsureCapacity(pExtW->binfo.pRes, pOperator->resultInfo.capacity);
2294
    //QUERY_CHECK_CODE(code, lino, _error);
2295

2296
    pExtW->pWinRowIdx = taosArrayInit(4096, sizeof(int64_t));
75,695✔
2297
    TSDB_CHECK_NULL(pExtW->pWinRowIdx, code, lino, _error, terrno);
75,695✔
2298
    
2299
    int32_t num = 0;
75,695✔
2300
    SExprInfo* pExprInfo = NULL;
75,695✔
2301
    code = createExprInfo(pPhynode->window.pFuncs, NULL, &pExprInfo, &num);
75,695✔
2302
    QUERY_CHECK_CODE(code, lino, _error);
75,695✔
2303
    pOperator->exprSupp.hasWindow = true;
75,695✔
2304
    pOperator->exprSupp.hasWindowOrGroup = true;
75,695✔
2305
    code = initAggSup(&pOperator->exprSupp, &pExtW->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str, 0, 0);
75,695✔
2306
    QUERY_CHECK_CODE(code, lino, _error);
75,492✔
2307

2308
    nodesWalkExprs(pPhynode->window.pFuncs, extWinHasCountLikeFunc, &pExtW->hasCountFunc);
75,492✔
2309
    if (pExtW->hasCountFunc) {
75,695✔
2310
      code = extWinCreateEmptyInputBlock(pOperator, &pExtW->pEmptyInputBlock);
63,505✔
2311
      QUERY_CHECK_CODE(code, lino, _error);
63,710✔
2312
      qDebug("%s ext window has CountLikeFunc", pOperator->pTaskInfo->id.str);
63,710✔
2313
    } else {
2314
      qDebug("%s ext window doesn't have CountLikeFunc", pOperator->pTaskInfo->id.str);
11,985✔
2315
    }
2316

2317
    code = initExecTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pTaskInfo->window);
75,900✔
2318
    QUERY_CHECK_CODE(code, lino, _error);
75,695✔
2319

2320
    pExtW->lastSKey = INT64_MIN;
75,695✔
2321
  } else {
2322
    size_t  keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
×
2323
    
2324
    if (pPhynode->window.pExprs != NULL) {
×
2325
      int32_t    num = 0;
×
2326
      SExprInfo* pSExpr = NULL;
×
2327
      code = createExprInfo(pPhynode->window.pExprs, NULL, &pSExpr, &num);
×
2328
      QUERY_CHECK_CODE(code, lino, _error);
×
2329
    
2330
      code = initExprSupp(&pExtW->scalarSupp, pSExpr, num, &pTaskInfo->storageAPI.functionStore);
×
2331
      if (code != TSDB_CODE_SUCCESS) {
×
2332
        goto _error;
×
2333
      }
2334
    }
2335
    
2336
    int32_t    numOfExpr = 0;
×
2337
    SExprInfo* pExprInfo = NULL;
×
2338
    code = createExprInfo(pPhynode->window.pFuncs, NULL, &pExprInfo, &numOfExpr);
×
2339
    TSDB_CHECK_CODE(code, lino, _error);
×
2340
    
2341
    code = initAggSup(&pOperator->exprSupp, &pExtW->aggSup, pExprInfo, numOfExpr, keyBufSize, pTaskInfo->id.str,
×
2342
                              pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
×
2343
    TSDB_CHECK_CODE(code, lino, _error);
×
2344
    pOperator->exprSupp.hasWindowOrGroup = false;
×
2345
    
2346
    //code = setFunctionResultOutput(pOperator, &pExtW->binfo, &pExtW->aggSup, MAIN_SCAN, numOfExpr);
2347
    //TSDB_CHECK_CODE(code, lino, _error);
2348
    
2349
    code = filterInitFromNode((SNode*)pNode->pConditions, &pOperator->exprSupp.pFilterInfo, 0,
×
2350
                              pTaskInfo->pStreamRuntimeInfo);
×
2351
    TSDB_CHECK_CODE(code, lino, _error);
×
2352
    
2353
    pExtW->binfo.inputTsOrder = pNode->inputTsOrder;
×
2354
    pExtW->binfo.outputTsOrder = pNode->outputTsOrder;
×
2355
    code = setRowTsColumnOutputInfo(pOperator->exprSupp.pCtx, numOfExpr, &pExtW->pPseudoColInfo);
×
2356
    TSDB_CHECK_CODE(code, lino, _error);
×
2357

2358
  //if (pExtW->multiTableMode) {
2359
    pExtW->pOutputBlocks = taosArrayInit_s(POINTER_BYTES, STREAM_CALC_REQ_MAX_WIN_NUM);
×
2360
    if (!pExtW->pOutputBlocks) QUERY_CHECK_CODE(terrno, lino, _error);
×
2361
  //}
2362
    pExtW->pFreeBlocks = tdListNew(POINTER_BYTES * 2);
×
2363
    QUERY_CHECK_NULL(pExtW->pFreeBlocks, code, lino, _error, terrno);  
×
2364
  }
2365

2366
  pExtW->pWins = taosArrayInit(4096, sizeof(SExtWinTimeWindow));
75,965✔
2367
  if (!pExtW->pWins) QUERY_CHECK_CODE(terrno, lino, _error);
75,965✔
2368
  
2369
  //initResultRowInfo(&pExtW->binfo.resultRowInfo);
2370

2371
  pExtW->timeRangeExpr = (STimeRangeNode*)pPhynode->pTimeRange;
75,965✔
2372
  if (pExtW->timeRangeExpr) {
75,965✔
2373
    QUERY_CHECK_NULL(pExtW->timeRangeExpr->pStart, code, lino, _error, TSDB_CODE_STREAM_INTERNAL_ERROR);
75,965✔
2374
    QUERY_CHECK_NULL(pExtW->timeRangeExpr->pEnd, code, lino, _error, TSDB_CODE_STREAM_INTERNAL_ERROR);
75,965✔
2375
  }
2376

2377
  if (pPhynode->isSingleTable) {
75,965✔
2378
    pExtW->getWinFp = (pExtW->timeRangeExpr && (pExtW->timeRangeExpr->needCalc || (pTaskInfo->pStreamRuntimeInfo->funcInfo.addOptions & CALC_SLIDING_OVERLAP))) ? extWinGetOvlpWin : extWinGetNoOvlpWin;
41,560✔
2379
    pExtW->multiTableMode = false;
41,560✔
2380
  } else {
2381
    pExtW->getWinFp = (pExtW->timeRangeExpr && (pExtW->timeRangeExpr->needCalc || (pTaskInfo->pStreamRuntimeInfo->funcInfo.addOptions & CALC_SLIDING_OVERLAP))) ? extWinGetMultiTbOvlpWin : extWinGetMultiTbNoOvlpWin;
34,405✔
2382
    pExtW->multiTableMode = true;
34,405✔
2383
  }
2384
  pExtW->inputHasOrder = pPhynode->inputHasOrder;
75,965✔
2385
  pExtW->orgTableUid = pPhynode->orgTableUid;
75,965✔
2386
  pExtW->orgTableVgId = pPhynode->orgTableVgId;
75,965✔
2387

2388
  pOperator->fpSet = createOperatorFpSet(extWinOpen, extWinNext, NULL, destroyExternalWindowOperatorInfo,
75,965✔
2389
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
2390
  setOperatorResetStateFn(pOperator, resetExternalWindowOperator);
75,965✔
2391
  code = appendDownstream(pOperator, &pDownstream, 1);
75,965✔
2392
  if (code != 0) {
75,965✔
2393
    goto _error;
×
2394
  }
2395

2396
  *pOptrOut = pOperator;
75,965✔
2397
  return code;
75,965✔
2398

2399
_error:
×
2400

2401
  if (pExtW != NULL) {
×
2402
    destroyExternalWindowOperatorInfo(pExtW);
×
2403
  }
2404

2405
  destroyOperatorAndDownstreams(pOperator, &pDownstream, 1);
×
2406
  pTaskInfo->code = code;
×
2407
  qError("error happens at %s %d, code:%s", __func__, lino, tstrerror(code));
×
2408
  return code;
×
2409
}
2410

2411

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