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

taosdata / TDengine / #4983

13 Mar 2026 03:38AM UTC coverage: 68.653% (+0.07%) from 68.587%
#4983

push

travis-ci

web-flow
feat/6641435300-save-audit-in-self (#34738)

434 of 584 new or added lines in 10 files covered. (74.32%)

434 existing lines in 121 files now uncovered.

212745 of 309883 relevant lines covered (68.65%)

134272959.11 hits per line

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

76.17
/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) {
1,221✔
91
  SSDataBlock* pRes = NULL;
1,221✔
92
  int32_t code = 0, lino = 0;
1,221✔
93

94
  if (listNEles(pExtW->pFreeBlocks) > 0) {
1,221✔
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));
1,221✔
102
    TAOS_CHECK_EXIT(blockDataEnsureCapacity(pRes, TMAX(rows, 4096)));
1,221✔
103
    SArray* pIdx = taosArrayInit(10, sizeof(int64_t));
1,221✔
104
    TSDB_CHECK_NULL(pIdx, code, lino, _exit, terrno);
1,221✔
105
    void* res[2] = {pRes, pIdx};
1,221✔
106
    TAOS_CHECK_EXIT(tdListAppend(pList, res));
1,221✔
107

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

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

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

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

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

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

143
_exit:
1,398✔
144

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

152
static void extWinDestroyBlockList(void* p) {
40,222,720✔
153
  if (NULL == p) {
40,222,720✔
154
    return;
×
155
  }
156

157
  SListNode* pTmp = NULL;
40,222,720✔
158
  SList** ppList = (SList**)p;
40,222,720✔
159
  if ((*ppList) && TD_DLIST_NELES(*ppList) > 0) {
40,222,720✔
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);
40,222,720✔
172
}
173

174

175
static void extWinRecycleBlkNode(SExternalWindowOperator* pExtW, SListNode** ppNode) {
1,737,067✔
176
  if (NULL == ppNode || NULL == *ppNode) {
1,737,067✔
177
    return;
1,736,828✔
178
  }
179

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

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

203
  SListNode* pTmp = NULL;
1,221✔
204
  SList** ppList = (SList**)p;
1,221✔
205
  if ((*ppList) && TD_DLIST_NELES(*ppList) > 0) {
1,221✔
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);
1,221✔
214
}
215
static void extWinDestroyBlkNode(SExternalWindowOperator* pInfo, SListNode* pNode) {
1,065,751✔
216
  if (NULL == pNode) {
1,065,751✔
217
    return;
1,064,530✔
218
  }
219

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

226
  taosMemoryFree(pNode);
1,221✔
227

228
  pInfo->stat.resBlockDestroyed++;
1,221✔
229
}
230

231

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

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

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

262
  pInfo->binfo.resultRowInfo.openWindow = tdListFree(pInfo->binfo.resultRowInfo.openWindow);
1,065,512✔
263

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

268
  taosMemoryFreeClear(pInfo);
1,065,512✔
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) {
16,457✔
281
  SMergeAlignedExternalWindowOperator* pMlExtInfo = (SMergeAlignedExternalWindowOperator*)pOperator;
16,457✔
282
  destroyExternalWindowOperatorInfo(pMlExtInfo->pExtW);
16,457✔
283
  taosMemoryFreeClear(pMlExtInfo);
16,457✔
284
}
16,457✔
285

286
int64_t* extWinExtractTsCol(SSDataBlock* pBlock, int32_t primaryTsIndex, SExecTaskInfo* pTaskInfo) {
5,382,718✔
287
  TSKEY* tsCols = NULL;
5,382,718✔
288

289
  if (pBlock->pDataBlock != NULL && pBlock->info.dataLoad) {
5,382,718✔
290
    SColumnInfoData* pColDataInfo = taosArrayGet(pBlock->pDataBlock, primaryTsIndex);
5,382,948✔
291
    if (!pColDataInfo) {
5,382,948✔
292
      pTaskInfo->code = terrno;
×
293
      T_LONG_JMP(pTaskInfo->env, terrno);
×
294
    }
295

296
    tsCols = (int64_t*)pColDataInfo->pData;
5,382,948✔
297
    if (pBlock->info.window.skey == 0 && pBlock->info.window.ekey == 0) {
5,382,948✔
298
      int32_t code = blockDataUpdateTsWindow(pBlock, primaryTsIndex);
5,382,948✔
299
      if (code != TSDB_CODE_SUCCESS) {
5,382,948✔
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;
5,382,948✔
308
}
309

310
static int32_t extWinGetCurWinIdx(SExecTaskInfo* pTaskInfo) {
2,147,483,647✔
311
  if (!pTaskInfo->pStreamRuntimeInfo) {
2,147,483,647✔
312
    return 0;
2,147,483,647✔
313
  }
314
  return pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
57,514,058✔
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) {
2,147,483,647✔
323
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
2,147,483,647✔
324
  if (pTaskInfo->pStreamRuntimeInfo) {
2,147,483,647✔
325
    pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = idx;
42,055,784✔
326
  }
327
}
2,147,483,647✔
328

329

330
static void extWinIncCurWinOutIdx(SStreamRuntimeInfo* pStreamRuntimeInfo) {
1,221✔
331
  pStreamRuntimeInfo->funcInfo.curOutIdx++;
1,221✔
332
}
1,221✔
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) {
2,147,483,647✔
343
  int32_t  code = 0, lino = 0;
2,147,483,647✔
344
  int64_t* lastRes = taosArrayGetLast(pIdx);
2,147,483,647✔
345
  int32_t* lastWinIdx = (int32_t*)lastRes;
2,147,483,647✔
346
  int32_t* lastRowIdx = lastWinIdx ? (lastWinIdx + 1) : NULL;
2,147,483,647✔
347
  int64_t  res = 0;
2,147,483,647✔
348
  int32_t* pWinIdx = (int32_t*)&res;
2,147,483,647✔
349
  int32_t* pRowIdx = pWinIdx + 1;
2,147,483,647✔
350

351
  if (lastWinIdx && *lastWinIdx == currWinIdx) {
2,147,483,647✔
352
    return code;
2,147,483,647✔
353
  }
354

355
  *pWinIdx = currWinIdx;
16,721,172✔
356
  *pRowIdx = pBlock->info.rows - rows;
16,720,948✔
357

358
  TSDB_CHECK_NULL(taosArrayPush(pIdx, &res), code, lino, _exit, terrno);
16,718,484✔
359

360
_exit:
16,718,484✔
361

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

366
  return code;
16,718,484✔
367
}
368

369
static int32_t extWinRebuildWinIdxByFilter(SExecTaskInfo* pTaskInfo, SArray* pIdx, int32_t rowsBeforeFilter,
230✔
370
                                           SColumnInfoData* pFilterRes) {
371
  int32_t code = TSDB_CODE_SUCCESS;
230✔
372
  int32_t lino = 0;
230✔
373

374
  if (pIdx == NULL || pFilterRes == NULL || rowsBeforeFilter <= 0 || taosArrayGetSize(pIdx) == 0) {
230✔
375
    return code;
×
376
  }
377

378
  int32_t idxSize = (int32_t)taosArrayGetSize(pIdx);
230✔
379
  SArray* pNewIdx = taosArrayInit(idxSize, sizeof(int64_t));
230✔
380
  TSDB_CHECK_NULL(pNewIdx, code, lino, _exit, terrno);
230✔
381

382
  int8_t* pIndicator = (int8_t*)pFilterRes->pData;
230✔
383
  int32_t newRowStart = 0;
230✔
384
  for (int32_t i = 0; i < idxSize; ++i) {
460✔
385
    int64_t cur = *(int64_t*)taosArrayGet(pIdx, i);
230✔
386
    int32_t* pCurWinIdx = (int32_t*)&cur;
230✔
387
    int32_t* pCurRowIdx = pCurWinIdx + 1;
230✔
388

389
    int32_t startRow = *pCurRowIdx;
230✔
390
    int32_t endRow = rowsBeforeFilter;
230✔
391
    if (i + 1 < idxSize) {
230✔
392
      int64_t next = *(int64_t*)taosArrayGet(pIdx, i + 1);
×
393
      int32_t* pNextWinIdx = (int32_t*)&next;
×
394
      int32_t* pNextRowIdx = pNextWinIdx + 1;
×
395
      endRow = *pNextRowIdx;
×
396
    }
397

398
    startRow = TMIN(TMAX(startRow, 0), rowsBeforeFilter);
230✔
399
    endRow = TMIN(TMAX(endRow, 0), rowsBeforeFilter);
230✔
400
    if (endRow <= startRow) {
230✔
401
      continue;
×
402
    }
403

404
    int32_t survivedRows = 0;
230✔
405
    for (int32_t r = startRow; r < endRow; ++r) {
460✔
406
      if (pIndicator[r]) {
230✔
407
        survivedRows++;
×
408
      }
409
    }
410

411
    if (survivedRows <= 0) {
230✔
412
      continue;
230✔
413
    }
414

415
    int64_t out = 0;
×
416
    int32_t* pOutWinIdx = (int32_t*)&out;
×
417
    int32_t* pOutRowIdx = pOutWinIdx + 1;
×
418
    *pOutWinIdx = *pCurWinIdx;
×
419
    *pOutRowIdx = newRowStart;
×
420
    TSDB_CHECK_NULL(taosArrayPush(pNewIdx, &out), code, lino, _exit, terrno);
×
421

422
    newRowStart += survivedRows;
×
423
  }
424

425
  taosArrayClear(pIdx);
230✔
426
  int32_t newSize = (int32_t)taosArrayGetSize(pNewIdx);
230✔
427
  if (newSize > 0) {
230✔
428
    void* dest = taosArrayReserve(pIdx, newSize);
×
429
    TSDB_CHECK_NULL(dest, code, lino, _exit, terrno);
×
430
    memcpy(dest, pNewIdx->pData, (size_t)newSize * sizeof(int64_t));
×
431
  }
432

433
_exit:
230✔
434
  taosArrayDestroy(pNewIdx);
230✔
435
  if (code != TSDB_CODE_SUCCESS) {
230✔
436
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
437
  }
438
  return code;
230✔
439
}
440

441

442
static int32_t mergeAlignExtWinSetOutputBuf(SOperatorInfo* pOperator, SResultRowInfo* pResultRowInfo, const STimeWindow* pWin, SResultRow** pResult,
2,885,928✔
443
                                       SExprSupp* pExprSup, SAggSupporter* pAggSup) {
444
  if (*pResult == NULL) {
2,885,928✔
445
    *pResult = getNewResultRow(pAggSup->pResultBuf, &pAggSup->currentPageId, pAggSup->resultRowSize);
16,911✔
446
    if (!*pResult) {
16,911✔
447
      qError("get new resultRow failed, err:%s", tstrerror(terrno));
×
448
      return terrno;
×
449
    }
450
    pResultRowInfo->cur = (SResultRowPosition){.pageId = (*pResult)->pageId, .offset = (*pResult)->offset};
16,911✔
451
  }
452
  
453
  (*pResult)->win = *pWin;
2,885,928✔
454
  (*pResult)->winIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
2,885,928✔
455
  
456
  return setResultRowInitCtx((*pResult), pExprSup->pCtx, pExprSup->numOfExprs, pExprSup->rowEntryInfoOffset);
2,885,928✔
457
}
458

459

460
static int32_t mergeAlignExtWinGetWinFromTs(SOperatorInfo* pOperator, SExternalWindowOperator* pExtW, TSKEY ts, STimeWindow** ppWin) {
2,883,688✔
461
  int32_t blkWinIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
2,883,688✔
462
  
463
  // TODO handle desc order
464
  for (int32_t i = blkWinIdx; i < pExtW->pWins->size; ++i) {
5,743,521✔
465
    STimeWindow* pWin = taosArrayGet(pExtW->pWins, i);
5,739,265✔
466
    if (ts == pWin->skey) {
5,736,235✔
467
      extWinSetCurWinIdx(pOperator, i);
2,881,106✔
468
      *ppWin = pWin;
2,881,106✔
469
      return TSDB_CODE_SUCCESS;
2,881,106✔
470
    } else if (ts < pWin->skey) {
2,859,833✔
UNCOV
471
      qError("invalid ts %" PRId64 " for current window idx %d skey %" PRId64, ts, i, pWin->skey);
×
472
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
473
    }
474
  }
475
  
476
  qError("invalid ts %" PRId64 " to find merge aligned ext window, size:%d", ts, (int32_t)pExtW->pWins->size);
×
477
  return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
478
}
479

480
static int32_t mergeAlignExtWinFinalizeResult(SOperatorInfo* pOperator, SResultRowInfo* pResultRowInfo, SSDataBlock* pResultBlock) {
2,880,552✔
481
  int32_t        code = 0, lino = 0;
2,880,552✔
482
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
2,880,552✔
483
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
2,880,552✔
484
  SExprSupp*     pSup = &pOperator->exprSupp;
2,880,552✔
485
  SResultRow*  pResultRow = pMlExtInfo->pResultRow;
2,880,552✔
486
  
487
  finalizeResultRows(pExtW->aggSup.pResultBuf, &pResultRowInfo->cur, pSup, pResultBlock, pOperator->pTaskInfo);
2,880,552✔
488
  
489
  if (pResultRow->numOfRows > 0) {
2,879,208✔
490
    TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pExtW->pWinRowIdx, pResultBlock, pResultRow->winIdx, pResultRow->numOfRows));
2,879,208✔
491
  }
492

493
_exit:
2,877,416✔
494

495
  if (code) {
2,877,416✔
496
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
497
  }
498

499
  return code;
2,876,744✔
500
}
501

502
static int32_t mergeAlignExtWinAggDo(SOperatorInfo* pOperator, SResultRowInfo* pResultRowInfo, SSDataBlock* pBlock, SSDataBlock* pResultBlock) {
24,303✔
503
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
24,303✔
504
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
24,303✔
505

506
  SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
24,303✔
507
  SExprSupp*     pSup = &pOperator->exprSupp;
24,303✔
508
  int32_t        code = 0, lino = 0;
24,303✔
509
  STimeWindow *pWin = NULL;
24,303✔
510

511
  int32_t startPos = 0;
24,303✔
512
  int64_t* tsCols = extWinExtractTsCol(pBlock, pExtW->primaryTsIndex, pTaskInfo);
24,303✔
513
  TSKEY ts = getStartTsKey(&pBlock->info.window, tsCols);
24,303✔
514
  
515
  code = mergeAlignExtWinGetWinFromTs(pOperator, pExtW, ts, &pWin);
24,303✔
516
  if (code) {
24,303✔
517
    qError("failed to get time window for ts:%" PRId64 ", prim ts index:%d, error:%s", ts, pExtW->primaryTsIndex, tstrerror(code));
×
518
    TAOS_CHECK_EXIT(code);
×
519
  }
520

521
  if (pMlExtInfo->curTs != INT64_MIN && pMlExtInfo->curTs != pWin->skey) {
24,303✔
522
    TAOS_CHECK_EXIT(mergeAlignExtWinFinalizeResult(pOperator, pResultRowInfo, pResultBlock));
2,016✔
523
    resetResultRow(pMlExtInfo->pResultRow, pExtW->aggSup.resultRowSize - sizeof(SResultRow));
2,016✔
524
  }
525
  
526
  TAOS_CHECK_EXIT(mergeAlignExtWinSetOutputBuf(pOperator, pResultRowInfo, pWin, &pMlExtInfo->pResultRow, pSup, &pExtW->aggSup));
24,303✔
527

528
  int32_t currPos = startPos;
24,303✔
529
  pMlExtInfo->curTs = pWin->skey;
24,303✔
530
  
531
  while (++currPos < pBlock->info.rows) {
8,647,610✔
532
    if (tsCols[currPos] == pMlExtInfo->curTs) continue;
8,623,307✔
533

534
    qDebug("current ts:%" PRId64 ", startPos:%d, currPos:%d, tsCols[currPos]:%" PRId64,
2,861,849✔
535
      pMlExtInfo->curTs, startPos, currPos, tsCols[currPos]); 
536
    TAOS_CHECK_EXIT(applyAggFunctionOnPartialTuples(pTaskInfo, pSup->pCtx, &pExtW->twAggSup.timeWindowData, startPos,
2,861,849✔
537
                                           currPos - startPos, pBlock->info.rows, pSup->numOfExprs));
538

539
    TAOS_CHECK_EXIT(mergeAlignExtWinFinalizeResult(pOperator, pResultRowInfo, pResultBlock));
2,861,625✔
540
    resetResultRow(pMlExtInfo->pResultRow, pExtW->aggSup.resultRowSize - sizeof(SResultRow));
2,857,593✔
541

542
    TAOS_CHECK_EXIT(mergeAlignExtWinGetWinFromTs(pOperator, pExtW, tsCols[currPos], &pWin));
2,857,817✔
543
    
544
    qDebug("ext window align2 start:%" PRId64 ", end:%" PRId64, pWin->skey, pWin->ekey);
2,856,803✔
545
    startPos = currPos;
2,861,401✔
546
    
547
    TAOS_CHECK_EXIT(mergeAlignExtWinSetOutputBuf(pOperator, pResultRowInfo, pWin, &pMlExtInfo->pResultRow, pSup, &pExtW->aggSup));
2,861,401✔
548

549
    pMlExtInfo->curTs = pWin->skey;
2,861,849✔
550
  }
551

552
  code = applyAggFunctionOnPartialTuples(pTaskInfo, pSup->pCtx, &pExtW->twAggSup.timeWindowData, startPos,
48,606✔
553
                                         currPos - startPos, pBlock->info.rows, pSup->numOfExprs);
24,303✔
554

555
_exit:
24,303✔
556

557
  if (code != 0) {
24,303✔
558
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
559
    T_LONG_JMP(pTaskInfo->env, code);
×
560
  }
561
  
562
  return code;
24,303✔
563
}
564

565
static int32_t mergeAlignExtWinBuildWinRowIdx(SOperatorInfo* pOperator, SSDataBlock* pInput, SSDataBlock* pResult) {
×
566
  SExternalWindowOperator* pExtW = pOperator->info;
×
567
  int64_t* tsCols = extWinExtractTsCol(pInput, pExtW->primaryTsIndex, pOperator->pTaskInfo);
×
568
  STimeWindow* pWin = NULL;
×
569
  int32_t code = 0, lino = 0;
×
570
  int64_t prevTs = INT64_MIN;
×
571
  
572
  for (int32_t i = 0; i < pInput->info.rows; ++i) {
×
573
    if (prevTs == tsCols[i]) {
×
574
      continue;
×
575
    }
576
    
577
    TAOS_CHECK_EXIT(mergeAlignExtWinGetWinFromTs(pOperator, pExtW, tsCols[i], &pWin));
×
578
    TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pExtW->pWinRowIdx, pResult, extWinGetCurWinIdx(pOperator->pTaskInfo), pInput->info.rows - i));
×
579

580
    prevTs = tsCols[i];
×
581
  }
582

583
_exit:
×
584

585
  if (code != 0) {
×
586
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
587
  }
588

589
  return code;  
×
590
}
591

592
static int32_t mergeAlignExtWinProjectDo(SOperatorInfo* pOperator, SResultRowInfo* pResultRowInfo, SSDataBlock* pBlock,
×
593
                                            SSDataBlock* pResultBlock) {
594
  SExternalWindowOperator* pExtW = pOperator->info;
×
595
  SExprSupp*               pExprSup = &pExtW->scalarSupp;
×
596
  int32_t                  code = 0, lino = 0;
×
597
  
598
  TAOS_CHECK_EXIT(projectApplyFunctions(pExprSup->pExprInfo, pResultBlock, pBlock, pExprSup->pCtx, pExprSup->numOfExprs, NULL,
×
599
                        GET_STM_RTINFO(pOperator->pTaskInfo)));
600

601
  TAOS_CHECK_EXIT(mergeAlignExtWinBuildWinRowIdx(pOperator, pBlock, pResultBlock));
×
602

603
_exit:
×
604

605
  if (code != 0) {
×
606
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
607
  }
608

609
  return code;
×
610
}
611

612
void mergeAlignExtWinDo(SOperatorInfo* pOperator) {
18,037✔
613
  SExecTaskInfo*                       pTaskInfo = pOperator->pTaskInfo;
18,037✔
614
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
18,037✔
615
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
18,037✔
616
  SResultRow*                          pResultRow = NULL;
18,037✔
617
  int32_t                              code = 0;
18,037✔
618
  SSDataBlock*                         pRes = pExtW->binfo.pRes;
18,037✔
619
  SExprSupp*                           pSup = &pOperator->exprSupp;
18,037✔
620
  int32_t                              lino = 0;
18,037✔
621

622
  taosArrayClear(pExtW->pWinRowIdx);
18,037✔
623
  blockDataCleanup(pRes);
18,037✔
624

625
  while (1) {
23,631✔
626
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
41,668✔
627

628
    if (pBlock == NULL) {
41,668✔
629
      // close last time window
630
      if (pMlExtInfo->curTs != INT64_MIN && EEXT_MODE_AGG == pExtW->mode) {
17,365✔
631
        TAOS_CHECK_EXIT(mergeAlignExtWinFinalizeResult(pOperator, &pExtW->binfo.resultRowInfo, pRes));
16,911✔
632
      }
633
      setOperatorCompleted(pOperator);
17,365✔
634
      break;
17,365✔
635
    }
636

637
    pRes->info.scanFlag = pBlock->info.scanFlag;
24,303✔
638
    code = setInputDataBlock(pSup, pBlock, pExtW->binfo.inputTsOrder, pBlock->info.scanFlag, true);
24,303✔
639
    QUERY_CHECK_CODE(code, lino, _exit);
24,303✔
640

641
    printDataBlock(pBlock, __func__, "externalwindowAlign", pTaskInfo->id.queryId);
24,303✔
642
    qDebug("ext windowpExtWAlign->scalarMode:%d", pExtW->mode);
24,303✔
643

644
    if (EEXT_MODE_SCALAR == pExtW->mode) {
24,303✔
645
      TAOS_CHECK_EXIT(mergeAlignExtWinProjectDo(pOperator, &pExtW->binfo.resultRowInfo, pBlock, pRes));
×
646
    } else {
647
      TAOS_CHECK_EXIT(mergeAlignExtWinAggDo(pOperator, &pExtW->binfo.resultRowInfo, pBlock, pRes));
24,303✔
648
    }
649

650
    if (pRes->info.rows >= pOperator->resultInfo.threshold) {
24,303✔
651
      break;
672✔
652
    }
653
  }
654

655
  pOperator->pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamBlkWinIdx = pExtW->pWinRowIdx;
18,037✔
656
  
657
_exit:
18,037✔
658

659
  if (code != 0) {
18,037✔
660
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
661
    pTaskInfo->code = code;
×
662
    T_LONG_JMP(pTaskInfo->env, code);
×
663
  }
664
}
18,037✔
665

666
static int32_t mergeAlignExtWinNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
34,948✔
667
  SExecTaskInfo*                       pTaskInfo = pOperator->pTaskInfo;
34,948✔
668
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
34,948✔
669
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
34,948✔
670
  int32_t                              code = 0;
34,948✔
671
  int32_t lino = 0;
34,948✔
672

673
  if (pOperator->status == OP_EXEC_DONE) {
34,948✔
674
    (*ppRes) = NULL;
16,911✔
675
    return TSDB_CODE_SUCCESS;
16,911✔
676
  }
677

678
  SSDataBlock* pRes = pExtW->binfo.pRes;
18,037✔
679
  blockDataCleanup(pRes);
18,037✔
680

681
  if (taosArrayGetSize(pExtW->pWins) <= 0) {
18,037✔
682
    size_t size = taosArrayGetSize(pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamPesudoFuncVals);
17,365✔
683
    STimeWindow* pWin = taosArrayReserve(pExtW->pWins, size);
17,365✔
684
    TSDB_CHECK_NULL(pWin, code, lino, _exit, terrno);
17,365✔
685

686
    for (int32_t i = 0; i < size; ++i) {
2,898,595✔
687
      SSTriggerCalcParam* pParam = taosArrayGet(pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamPesudoFuncVals, i);
2,881,230✔
688
      pWin[i].skey = pParam->wstart;
2,881,230✔
689
      pWin[i].ekey = pParam->wstart + 1;
2,881,230✔
690
    }
691
    
692
    pExtW->outputWinId = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
17,365✔
693
  }
694

695
  mergeAlignExtWinDo(pOperator);
18,037✔
696
  
697
  size_t rows = pRes->info.rows;
18,037✔
698
  pOperator->resultInfo.totalRows += rows;
18,037✔
699
  (*ppRes) = (rows == 0) ? NULL : pRes;
18,037✔
700

701
_exit:
18,037✔
702

703
  if (code != 0) {
18,037✔
704
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
705
    pTaskInfo->code = code;
×
706
    T_LONG_JMP(pTaskInfo->env, code);
×
707
  }
708
  return code;
18,037✔
709
}
710

711
int32_t resetMergeAlignedExtWinOperator(SOperatorInfo* pOperator) {
18,273✔
712
  SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
18,273✔
713
  SExternalWindowOperator*             pExtW = pMlExtInfo->pExtW;
18,273✔
714
  SExecTaskInfo*                       pTaskInfo = pOperator->pTaskInfo;
18,273✔
715
  SMergeAlignedIntervalPhysiNode * pPhynode = (SMergeAlignedIntervalPhysiNode*)pOperator->pPhyNode;
18,273✔
716
  pOperator->status = OP_NOT_OPENED;
18,273✔
717

718
  taosArrayClear(pExtW->pWins);
18,273✔
719

720
  resetBasicOperatorState(&pExtW->binfo);
18,273✔
721
  pMlExtInfo->pResultRow = NULL;
18,273✔
722
  pMlExtInfo->curTs = INT64_MIN;
18,273✔
723

724
  int32_t code = resetAggSup(&pOperator->exprSupp, &pExtW->aggSup, pTaskInfo, pPhynode->window.pFuncs, NULL,
36,546✔
725
                             sizeof(int64_t) * 2 + POINTER_BYTES, pTaskInfo->id.str, NULL,
18,273✔
726
                             &pTaskInfo->storageAPI.functionStore);
727
  if (code == 0) {
18,273✔
728
    colDataDestroy(&pExtW->twAggSup.timeWindowData);
18,273✔
729
    code = initExecTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pTaskInfo->window);
18,273✔
730
  }
731
  return code;
18,273✔
732
}
733

734
int32_t createMergeAlignedExternalWindowOperator(SOperatorInfo* pDownstream, SPhysiNode* pNode,
16,457✔
735
                                                 SExecTaskInfo* pTaskInfo, SOperatorInfo** ppOptrOut) {
736
  SMergeAlignedIntervalPhysiNode* pPhynode = (SMergeAlignedIntervalPhysiNode*)pNode;
16,457✔
737
  int32_t code = 0;
16,457✔
738
  int32_t lino = 0;
16,457✔
739
  SMergeAlignedExternalWindowOperator* pMlExtInfo = taosMemoryCalloc(1, sizeof(SMergeAlignedExternalWindowOperator));
16,457✔
740
  SOperatorInfo*                       pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
16,457✔
741

742
  if (pTaskInfo->pStreamRuntimeInfo != NULL){
16,457✔
743
    pTaskInfo->pStreamRuntimeInfo->funcInfo.withExternalWindow = true;
16,457✔
744
  }
745
  pOperator->pPhyNode = pNode;
16,457✔
746
  if (!pMlExtInfo || !pOperator) {
16,457✔
747
    code = terrno;
×
748
    goto _error;
×
749
  }
750

751
  pMlExtInfo->pExtW = taosMemoryCalloc(1, sizeof(SExternalWindowOperator));
16,457✔
752
  if (!pMlExtInfo->pExtW) {
16,457✔
753
    code = terrno;
×
754
    goto _error;
×
755
  }
756

757
  SExternalWindowOperator* pExtW = pMlExtInfo->pExtW;
16,457✔
758
  SExprSupp* pSup = &pOperator->exprSupp;
16,457✔
759
  pSup->hasWindowOrGroup = true;
16,457✔
760
  pSup->hasWindow = true;
16,457✔
761
  pMlExtInfo->curTs = INT64_MIN;
16,457✔
762

763
  pExtW->primaryTsIndex = ((SColumnNode*)pPhynode->window.pTspk)->slotId;
16,457✔
764
  pExtW->mode = pPhynode->window.pProjs ? EEXT_MODE_SCALAR : EEXT_MODE_AGG;
16,457✔
765
  pExtW->binfo.inputTsOrder = pPhynode->window.node.inputTsOrder = TSDB_ORDER_ASC;
16,457✔
766
  pExtW->binfo.outputTsOrder = pExtW->binfo.inputTsOrder;
16,457✔
767

768
  size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
16,457✔
769
  initResultSizeInfo(&pOperator->resultInfo, 4096);
16,457✔
770

771
  int32_t num = 0;
16,457✔
772
  SExprInfo* pExprInfo = NULL;
16,457✔
773
  code = createExprInfo(pPhynode->window.pFuncs, NULL, &pExprInfo, &num);
16,457✔
774
  QUERY_CHECK_CODE(code, lino, _error);
16,457✔
775

776
  if (pExtW->mode == EEXT_MODE_AGG) {
16,457✔
777
    code = initAggSup(pSup, &pExtW->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str, NULL,
16,457✔
778
                      &pTaskInfo->storageAPI.functionStore);
779
    QUERY_CHECK_CODE(code, lino, _error);
16,457✔
780
  }
781

782
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pPhynode->window.node.pOutputDataBlockDesc);
16,457✔
783
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
16,457✔
784
  initBasicInfo(&pExtW->binfo, pResBlock);
16,457✔
785

786
  pExtW->pWins = taosArrayInit(4096, sizeof(STimeWindow));
16,457✔
787
  if (!pExtW->pWins) QUERY_CHECK_CODE(terrno, lino, _error);
16,457✔
788

789
  pExtW->pWinRowIdx = taosArrayInit(4096, sizeof(int64_t));
16,457✔
790
  TSDB_CHECK_NULL(pExtW->pWinRowIdx, code, lino, _error, terrno);
16,457✔
791

792
  initResultRowInfo(&pExtW->binfo.resultRowInfo);
16,457✔
793
  code = blockDataEnsureCapacity(pExtW->binfo.pRes, pOperator->resultInfo.capacity);
16,457✔
794
  QUERY_CHECK_CODE(code, lino, _error);
16,457✔
795
  setOperatorInfo(pOperator, "MergeAlignedExternalWindowOperator", QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW, false, OP_NOT_OPENED, pMlExtInfo, pTaskInfo);
16,457✔
796
  pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, mergeAlignExtWinNext, NULL,
16,457✔
797
                                         destroyMergeAlignedExternalWindowOperator, optrDefaultBufFn, NULL,
798
                                         optrDefaultGetNextExtFn, NULL);
799
  setOperatorResetStateFn(pOperator, resetMergeAlignedExtWinOperator);
16,457✔
800

801
  code = appendDownstream(pOperator, &pDownstream, 1);
16,457✔
802
  QUERY_CHECK_CODE(code, lino, _error);
16,457✔
803
  *ppOptrOut = pOperator;
16,457✔
804
  return code;
16,457✔
805
  
806
_error:
×
807
  if (pMlExtInfo) destroyMergeAlignedExternalWindowOperator(pMlExtInfo);
×
808
  destroyOperatorAndDownstreams(pOperator, &pDownstream, 1);
×
809
  pTaskInfo->code = code;
×
810
  return code;
×
811
}
812

813
static int32_t resetExternalWindowExprSupp(SExternalWindowOperator* pExtW, SExecTaskInfo* pTaskInfo,
296,789✔
814
                                           SExternalWindowPhysiNode* pPhynode) {
815
  int32_t    code = 0, lino = 0, num = 0;
296,789✔
816
  SExprInfo* pExprInfo = NULL;
296,789✔
817
  cleanupExprSuppWithoutFilter(&pExtW->scalarSupp);
296,789✔
818

819
  SNodeList* pNodeList = NULL;
296,789✔
820
  if (pPhynode->window.pProjs) {
296,789✔
821
    pNodeList = pPhynode->window.pProjs;
×
822
  } else {
823
    pNodeList = pPhynode->window.pExprs;
296,789✔
824
  }
825

826
  code = createExprInfo(pNodeList, NULL, &pExprInfo, &num);
296,613✔
827
  QUERY_CHECK_CODE(code, lino, _error);
296,613✔
828
  code = initExprSupp(&pExtW->scalarSupp, pExprInfo, num, &pTaskInfo->storageAPI.functionStore);
296,613✔
829
  QUERY_CHECK_CODE(code, lino, _error);
296,613✔
830
  return code;
296,613✔
831
_error:
×
832
  if (code != TSDB_CODE_SUCCESS) {
×
833
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
834
    pTaskInfo->code = code;
×
835
  }
836
  return code;
×
837
}
838

839
static int32_t resetExternalWindowOperator(SOperatorInfo* pOperator) {
296,613✔
840
  int32_t code = 0, lino = 0;
296,613✔
841
  SExternalWindowOperator* pExtW = pOperator->info;
296,613✔
842
  SExecTaskInfo*           pTaskInfo = pOperator->pTaskInfo;
296,613✔
843
  SExternalWindowPhysiNode* pPhynode = (SExternalWindowPhysiNode*)pOperator->pPhyNode;
296,789✔
844
  pOperator->status = OP_NOT_OPENED;
296,789✔
845

846
  //resetBasicOperatorState(&pExtW->binfo);
847
  initResultRowInfo(&pExtW->binfo.resultRowInfo);
296,789✔
848

849
  pExtW->outputWinId = 0;
296,437✔
850
  pExtW->lastWinId = -1;
296,437✔
851
  pExtW->outputWinNum = 0;
296,613✔
852
  taosArrayClear(pExtW->pWins);
296,613✔
853
  extWinRecycleBlkNode(pExtW, &pExtW->pLastBlkNode);
296,789✔
854

855
/*
856
  int32_t code = blockDataEnsureCapacity(pExtW->binfo.pRes, pOperator->resultInfo.capacity);
857
  if (code == 0) {
858
    code = resetAggSup(&pOperator->exprSupp, &pExtW->aggSup, pTaskInfo, pPhynode->window.pFuncs, NULL,
859
                       sizeof(int64_t) * 2 + POINTER_BYTES, pTaskInfo->id.str, pTaskInfo->streamInfo.pState,
860
                       &pTaskInfo->storageAPI.functionStore);
861
  }
862
*/
863
  TAOS_CHECK_EXIT(resetExternalWindowExprSupp(pExtW, pTaskInfo, pPhynode));
296,789✔
864
  colDataDestroy(&pExtW->twAggSup.timeWindowData);
296,613✔
865
  TAOS_CHECK_EXIT(initExecTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pTaskInfo->window));
296,789✔
866

867
  pExtW->outWinIdx = 0;
296,613✔
868
  pExtW->lastSKey = INT64_MIN;
296,789✔
869
  pExtW->isDynWindow = false;
296,789✔
870

871
  qDebug("%s ext window stat at reset, created:%" PRId64 ", destroyed:%" PRId64 ", recycled:%" PRId64 ", reused:%" PRId64 ", append:%" PRId64, 
296,390✔
872
      pTaskInfo->id.str, pExtW->stat.resBlockCreated, pExtW->stat.resBlockDestroyed, pExtW->stat.resBlockRecycled, 
873
      pExtW->stat.resBlockReused, pExtW->stat.resBlockAppend);
874

875
_exit:
8,131✔
876

877
  if (code) {
296,789✔
878
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
879
  }
880
  
881
  return code;
296,789✔
882
}
883

884
static EDealRes extWinHasCountLikeFunc(SNode* pNode, void* res) {
5,294,917✔
885
  if (QUERY_NODE_FUNCTION == nodeType(pNode)) {
5,294,917✔
886
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
1,912,208✔
887
    if (fmIsCountLikeFunc(pFunc->funcId) || (pFunc->hasOriginalFunc && fmIsCountLikeFunc(pFunc->originalFuncId))) {
1,912,208✔
888
      *(bool*)res = true;
604,709✔
889
      return DEAL_RES_END;
605,237✔
890
    }
891
  }
892
  return DEAL_RES_CONTINUE;
4,691,073✔
893
}
894

895

896
static int32_t extWinCreateEmptyInputBlock(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
605,237✔
897
  int32_t code = TSDB_CODE_SUCCESS;
605,237✔
898
  int32_t lino = 0;
605,237✔
899
  SSDataBlock* pBlock = NULL;
605,237✔
900
  if (!tsCountAlwaysReturnValue) {
605,237✔
901
    return TSDB_CODE_SUCCESS;
×
902
  }
903

904
  SExternalWindowOperator* pExtW = pOperator->info;
605,237✔
905

906
  if (!pExtW->hasCountFunc) {
605,002✔
907
    return TSDB_CODE_SUCCESS;
×
908
  }
909

910
  code = createDataBlock(&pBlock);
605,002✔
911
  if (code) {
605,237✔
912
    return code;
×
913
  }
914

915
  pBlock->info.rows = 1;
605,237✔
916
  pBlock->info.capacity = 0;
605,237✔
917

918
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
2,754,111✔
919
    SColumnInfoData colInfo = {0};
2,148,874✔
920
    colInfo.hasNull = true;
2,148,874✔
921
    colInfo.info.type = TSDB_DATA_TYPE_NULL;
2,148,874✔
922
    colInfo.info.bytes = 1;
2,148,874✔
923

924
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
2,148,874✔
925
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
4,456,064✔
926
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
2,306,610✔
927
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
2,306,846✔
928
        int32_t slotId = pFuncParam->pCol->slotId;
2,089,710✔
929
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
2,089,710✔
930
        if (slotId >= numOfCols) {
2,089,475✔
931
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
1,461,872✔
932
          QUERY_CHECK_CODE(code, lino, _end);
1,461,754✔
933

934
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
3,424,372✔
935
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
1,962,500✔
936
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,962,618✔
937
          }
938
        }
939
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
217,598✔
940
        // do nothing
941
      }
942
    }
943
  }
944

945
  code = blockDataEnsureCapacity(pBlock, pBlock->info.rows);
605,237✔
946
  QUERY_CHECK_CODE(code, lino, _end);
605,237✔
947

948
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
2,567,401✔
949
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
1,962,391✔
950
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
1,962,618✔
951
    colDataSetNULL(pColInfoData, 0);
952
  }
953
  *ppBlock = pBlock;
605,010✔
954

955
_end:
605,002✔
956
  if (code != TSDB_CODE_SUCCESS) {
605,237✔
957
    blockDataDestroy(pBlock);
×
958
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
959
  }
960
  return code;
605,237✔
961
}
962

963

964

965
static int extWinTsWinCompare(const void* pLeft, const void* pRight) {
16,948,292✔
966
  int64_t ts = *(int64_t*)pLeft;
16,948,292✔
967
  SExtWinTimeWindow* pWin = (SExtWinTimeWindow*)pRight;
16,947,157✔
968
  if (ts < pWin->tw.skey) {
16,947,157✔
969
    return -1;
9,624,495✔
970
  }
971
  if (ts >= pWin->tw.ekey) {
7,323,797✔
972
    return 1;
3,773,766✔
973
  }
974

975
  return 0;
3,550,031✔
976
}
977

978

979
static int32_t extWinGetMultiTbWinFromTs(SOperatorInfo* pOperator, SExternalWindowOperator* pExtW, int64_t* tsCol, int64_t rowNum, int32_t* startPos) {
2,743,755✔
980
  int32_t idx = taosArraySearchIdx(pExtW->pWins, tsCol, extWinTsWinCompare, TD_EQ);
2,743,755✔
981
  if (idx >= 0) {
2,743,755✔
982
    *startPos = 0;
2,586,135✔
983
    return idx;
2,586,135✔
984
  }
985

986
  SExtWinTimeWindow* pWin = NULL;
157,620✔
987
  int32_t w = 0;
157,620✔
988
  for (int64_t i = 1; i < rowNum; ++i) {
59,832,732✔
989
    for (; w < pExtW->pWins->size; ++w) {
296,526,324✔
990
      pWin = TARRAY_GET_ELEM(pExtW->pWins, w);
296,526,324✔
991
      if (tsCol[i] < pWin->tw.skey) {
296,526,324✔
992
        break;
59,675,112✔
993
      }
994
      
995
      if (tsCol[i] < pWin->tw.ekey) {
236,851,212✔
996
        *startPos = i;
135,960✔
997
        return w;
135,960✔
998
      }
999
    }
1000
  }
1001

1002
  return -1;
21,660✔
1003
}
1004

1005
static int32_t extWinGetNoOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
2,147,483,647✔
1006
  SExternalWindowOperator* pExtW = pOperator->info;
2,147,483,647✔
1007
  if ((*startPos) >= pInfo->rows) {
2,147,483,647✔
1008
    qDebug("%s %s blk rowIdx %d reach the end, size: %d, skip block", 
1,336,728✔
1009
        GET_TASKID(pOperator->pTaskInfo), __func__, *startPos, (int32_t)pInfo->rows);
1010
    *ppWin = NULL;
1,336,728✔
1011
    return TSDB_CODE_SUCCESS;
1,336,728✔
1012
  }
1013
  
1014
  if (pExtW->blkWinIdx < 0) {
2,147,483,647✔
1015
    pExtW->blkWinIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
1,570,724✔
1016
  } else {
1017
    pExtW->blkWinIdx++;
2,147,483,647✔
1018
  }
1019

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

1035
  int32_t r = *startPos;
2,147,483,647✔
1036

1037
  qDebug("%s %s start to get novlp win from winIdx %d rowIdx %d", GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, r);
2,147,483,647✔
1038

1039
  // TODO handle desc order
1040
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
2,147,483,647✔
1041
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
2,147,483,647✔
1042
    for (; r < pInfo->rows; ++r) {
2,147,483,647✔
1043
      if (tsCol[r] < pWin->tw.skey) {
2,147,483,647✔
1044
        continue;
326,594,843✔
1045
      }
1046

1047
      if (tsCol[r] < pWin->tw.ekey) {
2,147,483,647✔
1048
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
1,977,088,888✔
1049
        *ppWin = pWin;
1,977,088,888✔
1050
        *startPos = r;
1,977,088,888✔
1051
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
1,977,088,888✔
1052

1053
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
1,977,088,888✔
1054
            GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, r, tsCol[r], tsCol[r + *winRows - 1]);
1055
        
1056
        return TSDB_CODE_SUCCESS;
1,977,088,888✔
1057
      }
1058

1059
      if (!pOperator->pTaskInfo->pStreamRuntimeInfo && tsCol[r] >= pWin->tw.ekey) {
1,880,810,905✔
1060
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
1,880,804,520✔
1061
        *ppWin = pWin;
1,880,804,520✔
1062
        *startPos = r;
1,880,804,520✔
1063
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
1,880,804,520✔
1064

1065
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk",
1,880,804,520✔
1066
               GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, r, tsCol[r], tsCol[r + *winRows - 1]);
1067

1068
        return TSDB_CODE_SUCCESS;
1,880,804,520✔
1069
      }
1070

1071
      break;
6,385✔
1072
    }
1073

1074
    if (r == pInfo->rows) {
7,141✔
1075
      break;
756✔
1076
    }
1077
  }
1078

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

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

1086
static int32_t extWinGetOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
221,964✔
1087
  SExternalWindowOperator* pExtW = pOperator->info;
221,964✔
1088
  if (pExtW->blkWinIdx < 0) {
221,964✔
1089
    pExtW->blkWinIdx = pExtW->blkWinStartIdx;
12,170✔
1090
  } else {
1091
    pExtW->blkWinIdx++;
209,794✔
1092
  }
1093

1094
  if (pExtW->blkWinIdx >= pExtW->pWins->size) {
221,964✔
1095
    qDebug("%s %s ext win blk idx %d reach the end, size: %d, skip block", 
11,414✔
1096
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (int32_t)pExtW->pWins->size);
1097
    *ppWin = NULL;
11,414✔
1098
    return TSDB_CODE_SUCCESS;
11,414✔
1099
  }
1100
  
1101
  SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
210,550✔
1102
  if (tsCol[pInfo->rows - 1] < pWin->tw.skey) {
210,550✔
1103
    qDebug("%s %s block end ts %" PRId64 " is small than curr win %d skey %" PRId64 ", skip block", 
×
1104
        GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[pInfo->rows - 1], pExtW->blkWinIdx, pWin->tw.skey);
1105
    *ppWin = NULL;
×
1106
    return TSDB_CODE_SUCCESS;
×
1107
  }
1108

1109
  int64_t r = 0;
210,550✔
1110

1111
  qDebug("%s %s start to get ovlp win from winIdx %d rowIdx %d", GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pExtW->blkRowStartIdx);
210,550✔
1112
  
1113
  // TODO handle desc order
1114
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
211,306✔
1115
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
211,306✔
1116
    for (r = pExtW->blkRowStartIdx; r < pInfo->rows; ++r) {
432,266✔
1117
      if (tsCol[r] < pWin->tw.skey) {
431,510✔
1118
        pExtW->blkRowStartIdx = r + 1;
220,960✔
1119
        continue;
220,960✔
1120
      }
1121

1122
      if (tsCol[r] < pWin->tw.ekey) {
210,550✔
1123
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
209,794✔
1124
        *ppWin = pWin;
209,794✔
1125
        *startPos = r;
209,794✔
1126
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
209,794✔
1127

1128
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
209,794✔
1129
            GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, (int32_t)r, tsCol[r], tsCol[r + *winRows - 1]);
1130
        
1131
        if ((r + *winRows) < pInfo->rows) {
209,794✔
1132
          pExtW->blkWinStartIdx = pExtW->blkWinIdx + 1;
198,380✔
1133
          pExtW->blkWinStartSet = true;
198,380✔
1134
        }
1135
        
1136
        return TSDB_CODE_SUCCESS;
209,794✔
1137
      }
1138

1139
      break;
756✔
1140
    }
1141

1142
    if (r >= pInfo->rows) {
1,512✔
1143
      if (!pExtW->blkWinStartSet) {
756✔
1144
        pExtW->blkWinStartIdx = pExtW->blkWinIdx;
756✔
1145
      }
1146
      
1147
      break;
756✔
1148
    }
1149
  }
1150

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

1154
  *ppWin = NULL;
756✔
1155
  return TSDB_CODE_SUCCESS;
756✔
1156
}
1157

1158

1159
static int32_t extWinGetMultiTbNoOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
1,386,026,111✔
1160
  SExternalWindowOperator* pExtW = pOperator->info;
1,386,026,111✔
1161
  if ((*startPos) >= pInfo->rows) {
1,386,030,271✔
1162
    qDebug("%s %s blk rowIdx %d reach the end, size: %d, skip block", 
2,517,635✔
1163
        GET_TASKID(pOperator->pTaskInfo), __func__, *startPos, (int32_t)pInfo->rows);
1164
    *ppWin = NULL;
2,517,635✔
1165
    return TSDB_CODE_SUCCESS;
2,518,093✔
1166
  }
1167
  
1168
  if (pExtW->blkWinIdx < 0) {
1,383,512,636✔
1169
    pExtW->blkWinIdx = extWinGetMultiTbWinFromTs(pOperator, pExtW, tsCol, pInfo->rows, startPos);
2,743,755✔
1170
    if (pExtW->blkWinIdx < 0) {
2,743,755✔
1171
      qDebug("%s %s blk TR[%" PRId64 ", %" PRId64 ") not in any win, skip block", 
21,660✔
1172
          GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1173
      *ppWin = NULL;
21,660✔
1174
      return TSDB_CODE_SUCCESS;
21,660✔
1175
    }
1176

1177
    extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
2,722,095✔
1178
    *ppWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
2,722,095✔
1179
    *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, *startPos, (*ppWin)->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
2,722,095✔
1180

1181
    qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
2,722,095✔
1182
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (*ppWin)->tw.skey, (*ppWin)->tw.ekey, *winRows, *startPos, tsCol[*startPos], tsCol[*startPos + *winRows - 1]);
1183
    
1184
    return TSDB_CODE_SUCCESS;
2,722,095✔
1185
  } else {
1186
    pExtW->blkWinIdx++;
1,380,769,573✔
1187
  }
1188

1189
  if (pExtW->blkWinIdx >= pExtW->pWins->size) {
1,380,769,797✔
1190
    qDebug("%s %s ext win blk idx %d reach the end, size: %d, skip block", 
82,180✔
1191
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (int32_t)pExtW->pWins->size);
1192
    *ppWin = NULL;
82,180✔
1193
    return TSDB_CODE_SUCCESS;
82,180✔
1194
  }
1195
  
1196
  SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
1,380,687,612✔
1197
  if (tsCol[pInfo->rows - 1] < pWin->tw.skey) {
1,380,686,243✔
1198
    qDebug("%s %s block end ts %" PRId64 " is small than curr win %d skey %" PRId64 ", skip block", 
121,822✔
1199
        GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[pInfo->rows - 1], pExtW->blkWinIdx, pWin->tw.skey);
1200
    *ppWin = NULL;
121,822✔
1201
    return TSDB_CODE_SUCCESS;
121,822✔
1202
  }
1203

1204
  int32_t r = *startPos;
1,380,565,332✔
1205

1206
  qDebug("%s %s start to get mnovlp win from winIdx %d rowIdx %d", GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, r);
1,380,565,332✔
1207

1208
  // TODO handle desc order
1209
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
1,380,579,139✔
1210
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
1,380,578,681✔
1211
    for (; r < pInfo->rows; ++r) {
1,827,613,036✔
1212
      if (tsCol[r] < pWin->tw.skey) {
1,827,612,807✔
1213
        continue;
447,033,897✔
1214
      }
1215

1216
      if (tsCol[r] < pWin->tw.ekey) {
1,380,579,368✔
1217
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
1,268,986,214✔
1218
        *ppWin = pWin;
1,268,985,985✔
1219
        *startPos = r;
1,268,985,985✔
1220
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
1,268,985,527✔
1221

1222
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
1,268,985,985✔
1223
            GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, r, tsCol[r], tsCol[r + *winRows - 1]);
1224
        
1225
        return TSDB_CODE_SUCCESS;
1,268,985,527✔
1226
      }
1227

1228
      if (!pOperator->pTaskInfo->pStreamRuntimeInfo && tsCol[r] >= pWin->tw.ekey) {
111,593,154✔
1229
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
111,580,482✔
1230
        *ppWin = pWin;
111,580,482✔
1231
        *startPos = r;
111,580,482✔
1232
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
111,580,482✔
1233

1234
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk",
111,580,482✔
1235
               GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, r, tsCol[r], tsCol[r + *winRows - 1]);
1236

1237
        return TSDB_CODE_SUCCESS;
111,580,482✔
1238
      }
1239

1240
      break;
12,672✔
1241
    }
1242

1243
    if (r == pInfo->rows) {
5✔
1244
      break;
×
1245
    }
1246
  }
1247

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

1251
  *ppWin = NULL;
229✔
1252
  return TSDB_CODE_SUCCESS;
×
1253
}
1254

1255
static int32_t extWinGetFirstWinFromTs(SOperatorInfo* pOperator, SExternalWindowOperator* pExtW, int64_t* tsCol,
1,031,996✔
1256
                                       int64_t rowNum, int32_t* startPos) {
1257
  SExtWinTimeWindow* pWin = NULL;
1,031,996✔
1258
  int32_t            idx = taosArraySearchIdx(pExtW->pWins, tsCol, extWinTsWinCompare, TD_EQ);
1,031,996✔
1259
  if (idx >= 0) {
1,031,996✔
1260
    for (int i = idx - 1; i >= 0; --i) {
963,896✔
1261
      pWin = TARRAY_GET_ELEM(pExtW->pWins, i);
×
1262
      if (extWinTsWinCompare(tsCol, pWin) == 0) {
×
1263
        idx = i;
×
1264
      } else {
1265
        break;
×
1266
      }
1267
    }
1268
    *startPos = 0;
963,896✔
1269
    return idx;
963,896✔
1270
  }
1271

1272
  pWin = NULL;
68,100✔
1273
  int32_t w = 0;
68,100✔
1274
  for (int64_t i = 1; i < rowNum; ++i) {
136,654✔
1275
    for (; w < pExtW->pWins->size; ++w) {
159,354✔
1276
      pWin = TARRAY_GET_ELEM(pExtW->pWins, w);
159,354✔
1277
      if (tsCol[i] < pWin->tw.skey) {
159,354✔
1278
        break;
68,554✔
1279
      }
1280

1281
      if (tsCol[i] < pWin->tw.ekey) {
90,800✔
1282
        *startPos = i;
22,700✔
1283
        return w;
22,700✔
1284
      }
1285
    }
1286
  }
1287

1288
  return -1;
45,400✔
1289
}
1290

1291
static int32_t extWinGetMultiTbOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
6,047,481✔
1292
  SExternalWindowOperator* pExtW = pOperator->info;
6,047,481✔
1293
  if (pExtW->blkWinIdx < 0) {
6,047,705✔
1294
    pExtW->blkWinIdx = extWinGetFirstWinFromTs(pOperator, pExtW, tsCol, pInfo->rows, startPos);
1,031,996✔
1295
    if (pExtW->blkWinIdx < 0) {
1,031,996✔
1296
      qDebug("%s %s blk TR[%" PRId64 ", %" PRId64 ") not in any win, skip block", 
45,400✔
1297
          GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1298
      *ppWin = NULL;
45,400✔
1299
      return TSDB_CODE_SUCCESS;
45,400✔
1300
    }
1301

1302
    extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
986,596✔
1303
    *ppWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
986,596✔
1304
    *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, *startPos, (*ppWin)->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
986,596✔
1305
    
1306
    qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
986,596✔
1307
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (*ppWin)->tw.skey, (*ppWin)->tw.ekey, *winRows, *startPos, tsCol[*startPos], tsCol[*startPos + *winRows - 1]);
1308
    
1309
    return TSDB_CODE_SUCCESS;
986,596✔
1310
  } else {
1311
    pExtW->blkWinIdx++;
5,015,709✔
1312
  }
1313

1314
  if (pExtW->blkWinIdx >= pExtW->pWins->size) {
5,015,709✔
1315
    qDebug("%s %s ext win blk idx %d reach the end, size: %d, skip block", 
986,596✔
1316
        GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, (int32_t)pExtW->pWins->size);
1317
    *ppWin = NULL;
986,596✔
1318
    return TSDB_CODE_SUCCESS;
986,596✔
1319
  }
1320
  
1321
  SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
4,029,560✔
1322
  if (tsCol[pInfo->rows - 1] < pWin->tw.skey) {
4,029,560✔
1323
    qDebug("%s %s block end ts %" PRId64 " is small than curr win %d skey %" PRId64 ", skip block", 
×
1324
        GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[pInfo->rows - 1], pExtW->blkWinIdx, pWin->tw.skey);
1325
    *ppWin = NULL;
×
1326
    return TSDB_CODE_SUCCESS;
×
1327
  }
1328

1329
  int64_t r = 0;
4,029,336✔
1330

1331
  qDebug("%s %s start to get movlp win from winIdx %d rowIdx %d", GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pExtW->blkRowStartIdx);
4,029,336✔
1332

1333
  // TODO handle desc order
1334
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
4,096,908✔
1335
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
4,096,684✔
1336
    for (r = pExtW->blkRowStartIdx; r < pInfo->rows; ++r) {
13,860,780✔
1337
      if (tsCol[r] < pWin->tw.skey) {
13,859,436✔
1338
        pExtW->blkRowStartIdx = r + 1;
9,763,872✔
1339
        continue;
9,763,872✔
1340
      }
1341

1342
      if (tsCol[r] < pWin->tw.ekey) {
4,096,460✔
1343
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
4,029,560✔
1344
        *ppWin = pWin;
4,029,560✔
1345
        *startPos = r;
4,029,560✔
1346
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
4,029,560✔
1347

1348
        qDebug("%s %s the %dth ext win TR[%" PRId64 ", %" PRId64 ") got %d rows rowStartidx %d ts[%" PRId64 ", %" PRId64 "] in blk", 
4,028,888✔
1349
            GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pWin->tw.skey, pWin->tw.ekey, *winRows, (int32_t)r, tsCol[r], tsCol[r + *winRows - 1]);
1350
        
1351
        return TSDB_CODE_SUCCESS;
3,997,528✔
1352
      }
1353

1354
      break;
66,900✔
1355
    }
1356

1357
    if (r >= pInfo->rows) {
67,124✔
1358
      break;
×
1359
    }
1360
  }
1361

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

1365
  *ppWin = NULL;
×
1366
  return TSDB_CODE_SUCCESS;
×
1367
}
1368

1369

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

1373
  if (win.ekey <= pBlockInfo->window.skey && ascQuery) {
×
1374
    return -2;
×
1375
  }
1376
//if (win.skey > pBlockInfo->window.ekey && !ascQuery) return -2;
1377

1378
  if (win.skey > pBlockInfo->window.ekey && ascQuery) return -1;
×
1379
//if (win.ekey < pBlockInfo->window.skey && !ascQuery) return -1;
1380

1381
  while (true) {
1382
    if (win.ekey <= tsCol[lastEndPos + 1] && ascQuery) return -2;
×
1383
    if (win.skey <= tsCol[lastEndPos + 1] && ascQuery) break;
×
1384
    lastEndPos++;
×
1385
  }
1386

1387
  *nextPos = lastEndPos + 1;
×
1388
  return 0;
×
1389
}
1390

1391
static int32_t extWinAggSetWinOutputBuf(SOperatorInfo* pOperator, SExtWinTimeWindow* win, SExprSupp* pSupp, 
2,147,483,647✔
1392
                                     SAggSupporter* pAggSup, SExecTaskInfo* pTaskInfo) {
1393
  int32_t code = 0, lino = 0;
2,147,483,647✔
1394
  SResultRow* pResultRow = NULL;
2,147,483,647✔
1395
  SExternalWindowOperator* pExtW = (SExternalWindowOperator*)pOperator->info;
2,147,483,647✔
1396
  
1397
#if 0
1398
  SResultRow* pResultRow = doSetResultOutBufByKey(pAggSup->pResultBuf, pResultRowInfo, (char*)&win->skey, TSDB_KEYSIZE,
1399
                                                  true, tableGroupId, pTaskInfo, true, pAggSup, true);
1400
  if (pResultRow == NULL) {
1401
    qError("failed to set result output buffer, error:%s", tstrerror(pTaskInfo->code));
1402
    return pTaskInfo->code;
1403
  }
1404

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

1407
#else
1408
  if (win->winOutIdx >= 0) {
2,147,483,647✔
1409
    pResultRow = (SResultRow*)((char*)pExtW->pResultRow + win->winOutIdx * pAggSup->resultRowSize);
2,147,483,647✔
1410
  } else {
1411
    win->winOutIdx = pExtW->outWinIdx++;
2,147,483,647✔
1412
    
1413
    qDebug("set window [%" PRId64 ", %" PRId64 "] outIdx:%d", win->tw.skey, win->tw.ekey, win->winOutIdx);
2,147,483,647✔
1414

1415
    pResultRow = (SResultRow*)((char*)pExtW->pResultRow + win->winOutIdx * pAggSup->resultRowSize);
2,147,483,647✔
1416
    
1417
    memset(pResultRow, 0, pAggSup->resultRowSize);
2,147,483,647✔
1418

1419
    pResultRow->winIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
2,147,483,647✔
1420
    TAOS_SET_POBJ_ALIGNED(&pResultRow->win, &win->tw);
2,147,483,647✔
1421
  }
1422
#endif
1423

1424
  // set time window for current result
1425
  TAOS_CHECK_EXIT(setResultRowInitCtx(pResultRow, pSupp->pCtx, pSupp->numOfExprs, pSupp->rowEntryInfoOffset));
2,147,483,647✔
1426

1427
_exit:
2,147,483,647✔
1428
  
1429
  if (code) {
2,147,483,647✔
1430
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1431
  }
1432

1433
  return code;
2,147,483,647✔
1434
}
1435

1436
static int32_t extWinAggDo(SOperatorInfo* pOperator, int32_t startPos, int32_t forwardRows,
2,147,483,647✔
1437
                                  SSDataBlock* pInputBlock) {
1438
  if (pOperator->pTaskInfo->pStreamRuntimeInfo && forwardRows == 0) {
2,147,483,647✔
1439
    return TSDB_CODE_SUCCESS;
×
1440
  }
1441

1442
  SExprSupp*               pSup = &pOperator->exprSupp;
2,147,483,647✔
1443
  SExternalWindowOperator* pExtW = pOperator->info;
2,147,483,647✔
1444
  return applyAggFunctionOnPartialTuples(pOperator->pTaskInfo, pSup->pCtx, &pExtW->twAggSup.timeWindowData, startPos,
2,147,483,647✔
1445
                                         forwardRows, pInputBlock->info.rows, pSup->numOfExprs);
2,147,483,647✔
1446

1447
}
1448

1449
static bool extWinLastWinClosed(SExternalWindowOperator* pExtW) {
1,221✔
1450
  if (pExtW->outWinIdx <= 0 || (pExtW->multiTableMode && !pExtW->inputHasOrder)) {
1,221✔
1451
    return false;
1,221✔
1452
  }
1453

1454
  if (NULL == pExtW->timeRangeExpr || !pExtW->timeRangeExpr->needCalc) {
×
1455
    return true;
×
1456
  }
1457

1458
  SList* pList = taosArrayGetP(pExtW->pOutputBlocks, pExtW->outWinIdx - 1);
×
1459
  if (0 == listNEles(pList)) {
×
1460
    return true;
×
1461
  }
1462

1463
  SListNode* pNode = listTail(pList);
×
1464
  SArray* pBlkWinIdx = *((SArray**)pNode->data + 1);
×
1465
  int64_t* pIdx = taosArrayGetLast(pBlkWinIdx);
×
1466
  if (pIdx && *(int32_t*)pIdx < pExtW->blkWinStartIdx) {
×
1467
    return true;
×
1468
  }
1469

1470
  return false;
×
1471
}
1472

1473
static int32_t extWinGetWinResBlock(SOperatorInfo* pOperator, int32_t rows, SExtWinTimeWindow* pWin, SSDataBlock** ppRes, SArray** ppIdx) {
2,619✔
1474
  SExternalWindowOperator* pExtW = pOperator->info;
2,619✔
1475
  SList*                   pList = NULL;
2,619✔
1476
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
2,619✔
1477
  
1478
  if (pWin->winOutIdx >= 0) {
2,619✔
1479
    pList = taosArrayGetP(pExtW->pOutputBlocks, pWin->winOutIdx);
1,398✔
1480
  } else {
1481
    if (extWinLastWinClosed(pExtW)) {
1,221✔
1482
      pWin->winOutIdx = pExtW->outWinIdx - 1;
×
1483
      pList = taosArrayGetP(pExtW->pOutputBlocks, pWin->winOutIdx);
×
1484
    } else {
1485
      pWin->winOutIdx = pExtW->outWinIdx++;
1,221✔
1486
      pList = tdListNew(POINTER_BYTES * 2);
1,221✔
1487
      TSDB_CHECK_NULL(pList, code, lino, _exit, terrno);
1,221✔
1488
      SList** ppList = taosArrayGet(pExtW->pOutputBlocks, pWin->winOutIdx);
1,221✔
1489
      extWinRecycleBlockList(pExtW, ppList);
1,221✔
1490
      *ppList = pList;
1,221✔
1491
    }
1492
  }
1493
  
1494
  TAOS_CHECK_EXIT(extWinGetLastBlockFromList(pExtW, pList, rows, ppRes, ppIdx));
2,619✔
1495

1496
_exit:
2,619✔
1497

1498
  if (code) {
2,619✔
1499
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1500
  }
1501

1502
  return code;
2,619✔
1503
}
1504

1505
static int32_t extWinProjectDo(SOperatorInfo* pOperator, SSDataBlock* pInputBlock, int32_t startPos, int32_t rows, SExtWinTimeWindow* pWin) {
2,619✔
1506
  SExternalWindowOperator* pExtW = pOperator->info;
2,619✔
1507
  SExprSupp*               pExprSup = &pExtW->scalarSupp;
2,619✔
1508
  SSDataBlock*             pResBlock = NULL;
2,619✔
1509
  SArray*                  pIdx = NULL;
2,619✔
1510
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
2,619✔
1511
  
1512
  TAOS_CHECK_EXIT(extWinGetWinResBlock(pOperator, rows, pWin, &pResBlock, &pIdx));
2,619✔
1513

1514
  qDebug("%s %s win[%" PRId64 ", %" PRId64 "] got res block %p winRowIdx %p, winOutIdx:%d, capacity:%d", 
2,619✔
1515
      pOperator->pTaskInfo->id.str, __func__, pWin->tw.skey, pWin->tw.ekey, pResBlock, pIdx, pWin->winOutIdx, pResBlock->info.capacity);
1516
  
1517
  if (!pExtW->pTmpBlock) {
2,619✔
1518
    TAOS_CHECK_EXIT(createOneDataBlock(pInputBlock, false, &pExtW->pTmpBlock));
982✔
1519
  } else {
1520
    blockDataCleanup(pExtW->pTmpBlock);
1,637✔
1521
  }
1522
  
1523
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pExtW->pTmpBlock, TMAX(1, rows)));
2,619✔
1524

1525
  qDebug("%s %s start to copy %d rows to tmp blk", pOperator->pTaskInfo->id.str, __func__, rows);
2,619✔
1526
  TAOS_CHECK_EXIT(blockDataMergeNRows(pExtW->pTmpBlock, pInputBlock, startPos, rows));
2,619✔
1527

1528
  qDebug("%s %s start to apply project to tmp blk", pOperator->pTaskInfo->id.str, __func__);
2,619✔
1529
  TAOS_CHECK_EXIT(projectApplyFunctionsWithSelect(pExprSup->pExprInfo, pResBlock, pExtW->pTmpBlock, pExprSup->pCtx, pExprSup->numOfExprs,
2,619✔
1530
        NULL, GET_STM_RTINFO(pOperator->pTaskInfo), true, pExprSup->hasIndefRowsFunc));
1531

1532
  TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pIdx, pResBlock, extWinGetCurWinIdx(pOperator->pTaskInfo), rows));
2,619✔
1533

1534
_exit:
2,619✔
1535

1536
  if (code) {
2,619✔
1537
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1538
  } else {
1539
    qDebug("%s %s project succeed", pOperator->pTaskInfo->id.str, __func__);
2,619✔
1540
  }
1541
  
1542
  return code;
2,619✔
1543
}
1544

1545
static int32_t extWinProjectOpen(SOperatorInfo* pOperator, SSDataBlock* pInputBlock) {
2,141✔
1546
  SExternalWindowOperator* pExtW = pOperator->info;
2,141✔
1547
  int64_t*                 tsCol = extWinExtractTsCol(pInputBlock, pExtW->primaryTsIndex, pOperator->pTaskInfo);
2,141✔
1548
  SExtWinTimeWindow*       pWin = NULL;
2,141✔
1549
  bool                     ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
2,141✔
1550
  int32_t                  startPos = 0, winRows = 0;
2,141✔
1551
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
2,141✔
1552
  
1553
  while (true) {
1554
    TAOS_CHECK_EXIT((*pExtW->getWinFp)(pOperator, tsCol, &startPos, &pInputBlock->info, &pWin, &winRows));
4,760✔
1555
    if (pWin == NULL) {
4,760✔
1556
      break;
2,141✔
1557
    }
1558

1559
    qDebug("%s ext window [%" PRId64 ", %" PRId64 ") project start, ascScan:%d, startPos:%d, winRows:%d",
2,619✔
1560
           GET_TASKID(pOperator->pTaskInfo), pWin->tw.skey, pWin->tw.ekey, ascScan, startPos, winRows);        
1561
    
1562
    TAOS_CHECK_EXIT(extWinProjectDo(pOperator, pInputBlock, startPos, winRows, pWin));
2,619✔
1563
    
1564
    startPos += winRows;
2,619✔
1565
  }
1566
  
1567
_exit:
2,141✔
1568

1569
  if (code) {
2,141✔
1570
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
1571
  }
1572

1573
  return code;
2,141✔
1574
}
1575

1576
static int32_t extWinIndefRowsDoImpl(SOperatorInfo* pOperator, SSDataBlock* pRes, SSDataBlock* pBlock) {
×
1577
  SExternalWindowOperator* pExtW = pOperator->info;
×
1578
  SOptrBasicInfo*     pInfo = &pExtW->binfo;
×
1579
  SExprSupp*          pSup = &pOperator->exprSupp;
×
1580
  SExecTaskInfo*      pTaskInfo = pOperator->pTaskInfo;
×
1581
  int32_t order = pInfo->inputTsOrder;
×
1582
  int32_t scanFlag = pBlock->info.scanFlag;
×
1583
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
×
1584

1585
  SExprSupp* pScalarSup = &pExtW->scalarSupp;
×
1586
  if (pScalarSup->pExprInfo != NULL) {
×
1587
    TAOS_CHECK_EXIT(projectApplyFunctions(pScalarSup->pExprInfo, pBlock, pBlock, pScalarSup->pCtx, pScalarSup->numOfExprs,
×
1588
                                 pExtW->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo)));
1589
  }
1590

1591
  TAOS_CHECK_EXIT(setInputDataBlock(pSup, pBlock, order, scanFlag, false));
×
1592

1593
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pRes, pRes->info.rows + pBlock->info.rows));
×
1594

1595
  TAOS_CHECK_EXIT(projectApplyFunctions(pSup->pExprInfo, pRes, pBlock, pSup->pCtx, pSup->numOfExprs,
×
1596
                               pExtW->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo)));
1597

1598
_exit:
×
1599

1600
  if (code) {
×
1601
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __FUNCTION__, lino, tstrerror(code));
×
1602
  }
1603

1604
  return code;
×
1605
}
1606

1607
static int32_t extWinIndefRowsSetWinOutputBuf(SExternalWindowOperator* pExtW, SExtWinTimeWindow* win, SExprSupp* pSupp, 
×
1608
                                     SAggSupporter* pAggSup, SExecTaskInfo* pTaskInfo, bool reset) {
1609
  int32_t code = 0, lino = 0;
×
1610
  SResultRow* pResultRow = NULL;
×
1611

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

1616
  if (reset) {
×
1617
    memset(pResultRow, 0, pAggSup->resultRowSize);
×
1618
    for (int32_t k = 0; k < pSupp->numOfExprs; ++k) {
×
1619
      SqlFunctionCtx* pCtx = &pSupp->pCtx[k];
×
1620
      pCtx->pOutput = NULL;
×
1621
    }
1622
  }
1623

1624
  TAOS_SET_POBJ_ALIGNED(&pResultRow->win, &win->tw);
×
1625

1626
  // set time window for current result
1627
  TAOS_CHECK_EXIT(setResultRowInitCtx(pResultRow, pSupp->pCtx, pSupp->numOfExprs, pSupp->rowEntryInfoOffset));
×
1628

1629
_exit:
×
1630
  
1631
  if (code) {
×
1632
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1633
  }
1634

1635
  return code;
×
1636
}
1637

1638
static int32_t extWinGetSetWinResBlockBuf(SOperatorInfo* pOperator, int32_t rows, SExtWinTimeWindow* pWin, SSDataBlock** ppRes, SArray** ppIdx) {
×
1639
  SExternalWindowOperator* pExtW = pOperator->info;
×
1640
  SList*                   pList = NULL;
×
1641
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
×
1642
  
1643
  if (pWin->winOutIdx >= 0) {
×
1644
    pList = taosArrayGetP(pExtW->pOutputBlocks, pWin->winOutIdx);
×
1645
    TAOS_CHECK_EXIT(extWinIndefRowsSetWinOutputBuf(pExtW, pWin, &pOperator->exprSupp, &pExtW->aggSup, pOperator->pTaskInfo, false));
×
1646
  } else {
1647
    if (extWinLastWinClosed(pExtW)) {
×
1648
      pWin->winOutIdx = pExtW->outWinIdx - 1;
×
1649
      pList = taosArrayGetP(pExtW->pOutputBlocks, pWin->winOutIdx);
×
1650
    } else {
1651
      pWin->winOutIdx = pExtW->outWinIdx++;
×
1652
      pList = tdListNew(POINTER_BYTES * 2);
×
1653
      TSDB_CHECK_NULL(pList, code, lino, _exit, terrno);
×
1654
      SList** ppList = taosArrayGet(pExtW->pOutputBlocks, pWin->winOutIdx);
×
1655
      extWinRecycleBlockList(pExtW, ppList);
×
1656
      *ppList = pList;
×
1657
    }
1658
    TAOS_CHECK_EXIT(extWinIndefRowsSetWinOutputBuf(pExtW, pWin, &pOperator->exprSupp, &pExtW->aggSup, pOperator->pTaskInfo, true));
×
1659
  }
1660
  
1661
  TAOS_CHECK_EXIT(extWinGetLastBlockFromList(pExtW, pList, rows, ppRes, ppIdx));
×
1662

1663
_exit:
×
1664

1665
  if (code) {
×
1666
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1667
  }
1668

1669
  return code;
×
1670
}
1671

1672

1673
static int32_t extWinIndefRowsDo(SOperatorInfo* pOperator, SSDataBlock* pInputBlock, int32_t startPos, int32_t rows, SExtWinTimeWindow* pWin) {
×
1674
  SExternalWindowOperator* pExtW = pOperator->info;
×
1675
  SSDataBlock*             pResBlock = NULL;
×
1676
  SArray*                  pIdx = NULL;
×
1677
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
×
1678
  
1679
  TAOS_CHECK_EXIT(extWinGetSetWinResBlockBuf(pOperator, rows, pWin, &pResBlock, &pIdx));
×
1680
  
1681
  if (!pExtW->pTmpBlock) {
×
1682
    TAOS_CHECK_EXIT(createOneDataBlock(pInputBlock, false, &pExtW->pTmpBlock));
×
1683
  } else {
1684
    blockDataCleanup(pExtW->pTmpBlock);
×
1685
  }
1686
  
1687
  TAOS_CHECK_EXIT(blockDataEnsureCapacity(pExtW->pTmpBlock, TMAX(1, rows)));
×
1688

1689
  TAOS_CHECK_EXIT(blockDataMergeNRows(pExtW->pTmpBlock, pInputBlock, startPos, rows));
×
1690
  TAOS_CHECK_EXIT(extWinIndefRowsDoImpl(pOperator, pResBlock, pExtW->pTmpBlock));
×
1691

1692
  TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pIdx, pResBlock, extWinGetCurWinIdx(pOperator->pTaskInfo), rows));
×
1693

1694
_exit:
×
1695

1696
  if (code) {
×
1697
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1698
  }
1699
  
1700
  return code;
×
1701
}
1702

1703

1704
static int32_t extWinIndefRowsOpen(SOperatorInfo* pOperator, SSDataBlock* pInputBlock) {
×
1705
  SExternalWindowOperator* pExtW = pOperator->info;
×
1706
  int64_t*                 tsCol = extWinExtractTsCol(pInputBlock, pExtW->primaryTsIndex, pOperator->pTaskInfo);
×
1707
  SExtWinTimeWindow*       pWin = NULL;
×
1708
  bool                     ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
×
1709
  int32_t                  startPos = 0, winRows = 0;
×
1710
  int32_t                  code = TSDB_CODE_SUCCESS, lino = 0;
×
1711
  
1712
  while (true) {
1713
    TAOS_CHECK_EXIT((*pExtW->getWinFp)(pOperator, tsCol, &startPos, &pInputBlock->info, &pWin, &winRows));
×
1714
    if (pWin == NULL) {
×
1715
      break;
×
1716
    }
1717

1718
    qDebug("%s ext window [%" PRId64 ", %" PRId64 ") indefRows start, ascScan:%d, startPos:%d, winRows:%d",
×
1719
           GET_TASKID(pOperator->pTaskInfo), pWin->tw.skey, pWin->tw.ekey, ascScan, startPos, winRows);        
1720
    
1721
    TAOS_CHECK_EXIT(extWinIndefRowsDo(pOperator, pInputBlock, startPos, winRows, pWin));
×
1722
    
1723
    startPos += winRows;
×
1724
  }
1725
  
1726
_exit:
×
1727

1728
  if (code) {
×
1729
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
1730
  }
1731

1732
  return code;
×
1733
}
1734

1735
static int32_t extWinNonAggOutputRes(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
1,221✔
1736
  SExternalWindowOperator* pExtW = pOperator->info;
1,221✔
1737
  int32_t                  numOfWin = pExtW->outWinIdx;
1,221✔
1738
  int32_t                  code = TSDB_CODE_SUCCESS;
1,221✔
1739
  int32_t                  lino = 0;
1,221✔
1740
  SSDataBlock*             pRes = NULL;
1,221✔
1741

1742
  for (; pExtW->outputWinId < numOfWin; pExtW->outputWinId++, extWinIncCurWinOutIdx(pOperator->pTaskInfo->pStreamRuntimeInfo)) {
1,221✔
1743
    SList* pList = taosArrayGetP(pExtW->pOutputBlocks, pExtW->outputWinId);
1,221✔
1744
    if (listNEles(pList) <= 0) {
1,221✔
1745
      continue;
×
1746
    }
1747

1748
    SListNode* pNode = tdListPopHead(pList);
1,221✔
1749
    pRes = *(SSDataBlock**)pNode->data;
1,221✔
1750
    pOperator->pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamBlkWinIdx = *(SArray**)((SArray**)pNode->data + 1);
1,221✔
1751
    pExtW->pLastBlkNode = pNode;
1,221✔
1752

1753
    if (listNEles(pList) <= 0) {
1,221✔
1754
      pExtW->outputWinId++;
1,221✔
1755
      extWinIncCurWinOutIdx(pOperator->pTaskInfo->pStreamRuntimeInfo);
1,221✔
1756
    }
1757

1758
    break;
1,221✔
1759
  }
1760

1761
  if (pRes) {
1,221✔
1762
    qDebug("%s result generated, rows:%" PRId64 , GET_TASKID(pOperator->pTaskInfo), pRes->info.rows);
1,221✔
1763
    pRes->info.version = pOperator->pTaskInfo->version;
1,221✔
1764
    pRes->info.dataLoad = 1;
1,221✔
1765
  } else {
1766
    pOperator->pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamBlkWinIdx = NULL;
×
1767
    qDebug("%s ext window done", GET_TASKID(pOperator->pTaskInfo));
×
1768
  }
1769

1770
  *ppRes = (pRes && pRes->info.rows > 0) ? pRes : NULL;
1,221✔
1771

1772
_exit:
1,221✔
1773

1774
  if (code != TSDB_CODE_SUCCESS) {
1,221✔
1775
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1776
  }
1777

1778
  return code;
1,221✔
1779
}
1780

1781
static int32_t extWinAggHandleEmptyWins(SOperatorInfo* pOperator, SSDataBlock* pBlock, bool allRemains, SExtWinTimeWindow* pWin) {
2,147,483,647✔
1782
  int32_t code = 0, lino = 0;
2,147,483,647✔
1783
  SExternalWindowOperator* pExtW = (SExternalWindowOperator*)pOperator->info;
2,147,483,647✔
1784
  SExprSupp* pSup = &pOperator->exprSupp;
2,147,483,647✔
1785
  int32_t currIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
2,147,483,647✔
1786

1787
  if (NULL == pExtW->pEmptyInputBlock || (pWin && pWin->tw.skey == pExtW->lastSKey)) {
2,147,483,647✔
1788
    goto _exit;
2,147,483,647✔
1789
  }
1790

1791
  bool ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
2,147,483,647✔
1792
  int32_t endIdx = allRemains ? (pExtW->pWins->size - 1) : (currIdx - 1);
2,147,483,647✔
1793
  SResultRowInfo* pResultRowInfo = &pExtW->binfo.resultRowInfo;
2,147,483,647✔
1794
  SSDataBlock* pInput = pExtW->pEmptyInputBlock;
2,147,483,647✔
1795

1796
  if ((pExtW->lastWinId + 1) <= endIdx) {
2,147,483,647✔
1797
    TAOS_CHECK_EXIT(setInputDataBlock(pSup, pExtW->pEmptyInputBlock, pExtW->binfo.inputTsOrder, MAIN_SCAN, true));
716,551✔
1798
  }
1799
  
1800
  for (int32_t i = pExtW->lastWinId + 1; i <= endIdx; ++i) {
2,147,483,647✔
1801
    SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, i);
1,560,737,587✔
1802

1803
    extWinSetCurWinIdx(pOperator, i);
1,560,737,587✔
1804
    qDebug("%s %dth ext empty window start:%" PRId64 ", end:%" PRId64 ", ascScan:%d",
1,560,737,587✔
1805
           GET_TASKID(pOperator->pTaskInfo), i, pWin->tw.skey, pWin->tw.ekey, ascScan);
1806

1807
    TAOS_CHECK_EXIT(extWinAggSetWinOutputBuf(pOperator, pWin, pSup, &pExtW->aggSup, pOperator->pTaskInfo));
1,560,737,587✔
1808

1809
    updateTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pWin->tw, 1);
1,560,737,587✔
1810
    code = extWinAggDo(pOperator, 0, 1, pInput);
1,560,737,587✔
1811
    pExtW->lastWinId = i;  
1,560,737,587✔
1812
    TAOS_CHECK_EXIT(code);
1,560,737,587✔
1813
  }
1814

1815
  
1816
_exit:
2,147,483,647✔
1817

1818
  if (code) {
2,147,483,647✔
1819
    qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __FUNCTION__, lino, tstrerror(code));
×
1820
  } else {
1821
    if (pBlock) {
2,147,483,647✔
1822
      TAOS_CHECK_EXIT(setInputDataBlock(pSup, pBlock, pExtW->binfo.inputTsOrder, pBlock->info.scanFlag, true));
2,147,483,647✔
1823
    }
1824

1825
    if (!allRemains) {
2,147,483,647✔
1826
      extWinSetCurWinIdx(pOperator, currIdx);  
2,147,483,647✔
1827
    }
1828
  }
1829

1830
  return code;
2,147,483,647✔
1831
}
1832

1833
static int32_t extWinAggOpen(SOperatorInfo* pOperator, SSDataBlock* pInputBlock) {
5,356,504✔
1834
  SExternalWindowOperator* pExtW = (SExternalWindowOperator*)pOperator->info;
5,356,504✔
1835
  int32_t                  startPos = 0, winRows = 0;
5,356,504✔
1836
  int64_t*                 tsCol = extWinExtractTsCol(pInputBlock, pExtW->primaryTsIndex, pOperator->pTaskInfo);
5,356,504✔
1837
  bool                     ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
5,356,504✔
1838
  int32_t                  code = 0, lino = 0;
5,356,504✔
1839
  SExtWinTimeWindow*       pWin = NULL;
5,356,504✔
1840
  bool                     scalarCalc = false;
5,356,504✔
1841

1842
  while (true) {
1843
    TAOS_CHECK_EXIT((*pExtW->getWinFp)(pOperator, tsCol, &startPos, &pInputBlock->info, &pWin, &winRows));
2,147,483,647✔
1844
    if (pWin == NULL) {
2,147,483,647✔
1845
      break;
5,356,280✔
1846
    }
1847

1848
    TAOS_CHECK_EXIT(extWinAggHandleEmptyWins(pOperator, pInputBlock, false, pWin));
2,147,483,647✔
1849

1850
    qDebug("%s ext window [%" PRId64 ", %" PRId64 ") agg start, ascScan:%d, startPos:%d, winRows:%d",
2,147,483,647✔
1851
           GET_TASKID(pOperator->pTaskInfo), pWin->tw.skey, pWin->tw.ekey, ascScan, startPos, winRows);        
1852

1853
    if (!scalarCalc) {
2,147,483,647✔
1854
      if (pExtW->scalarSupp.pExprInfo) {
5,288,230✔
1855
        SExprSupp* pScalarSup = &pExtW->scalarSupp;
6,048✔
1856
        TAOS_CHECK_EXIT(projectApplyFunctions(pScalarSup->pExprInfo, pInputBlock, pInputBlock, pScalarSup->pCtx, pScalarSup->numOfExprs,
6,048✔
1857
                                     pExtW->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo)));
1858
      }
1859
      
1860
      scalarCalc = true;
5,288,688✔
1861
    }
1862

1863
    if (pWin->tw.skey != pExtW->lastSKey || pWin->tw.skey == INT64_MIN) {
2,147,483,647✔
1864
      TAOS_CHECK_EXIT(extWinAggSetWinOutputBuf(pOperator, pWin, &pOperator->exprSupp, &pExtW->aggSup, pOperator->pTaskInfo));
2,147,483,647✔
1865
    }
1866
    
1867
    updateTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pWin->tw, 1);
2,147,483,647✔
1868
    TAOS_CHECK_EXIT(extWinAggDo(pOperator, startPos, winRows, pInputBlock));
2,147,483,647✔
1869
    
1870
    pExtW->lastSKey = pWin->tw.skey;
2,147,483,647✔
1871
    pExtW->lastWinId = extWinGetCurWinIdx(pOperator->pTaskInfo);
2,147,483,647✔
1872
    startPos += winRows;
2,147,483,647✔
1873
  }
1874

1875
_exit:
5,356,280✔
1876

1877
  if (code) {
5,356,280✔
1878
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
1879
  }
1880

1881
  return code;
5,356,280✔
1882
}
1883

1884
static int32_t extWinAggOutputRes(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
1,439,292✔
1885
  SExternalWindowOperator* pExtW = pOperator->info;
1,439,292✔
1886
  SExecTaskInfo*           pTaskInfo = pOperator->pTaskInfo;
1,439,292✔
1887
  SSDataBlock*             pBlock = pExtW->binfo.pRes;
1,439,292✔
1888
  int32_t                  code = TSDB_CODE_SUCCESS;
1,439,292✔
1889
  int32_t                  lino = 0;
1,439,292✔
1890
  SExprInfo*               pExprInfo = pOperator->exprSupp.pExprInfo;
1,439,292✔
1891
  int32_t                  numOfExprs = pOperator->exprSupp.numOfExprs;
1,439,292✔
1892
  int32_t*                 rowEntryOffset = pOperator->exprSupp.rowEntryInfoOffset;
1,439,292✔
1893
  SqlFunctionCtx*          pCtx = pOperator->exprSupp.pCtx;
1,439,292✔
1894
  int32_t                  numOfWin = pExtW->outWinIdx;
1,439,292✔
1895

1896
  pBlock->info.version = pTaskInfo->version;
1,439,292✔
1897
  blockDataCleanup(pBlock);
1,439,292✔
1898
  taosArrayClear(pExtW->pWinRowIdx);
1,439,292✔
1899

1900
  for (; pExtW->outputWinId < pExtW->pWins->size; ++pExtW->outputWinId) {
2,147,483,647✔
1901
    SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->outputWinId);
2,147,483,647✔
1902
    int32_t            winIdx = pWin->winOutIdx;
2,147,483,647✔
1903
    if (winIdx < 0) {
2,147,483,647✔
1904
      continue;
12,470,298✔
1905
    }
1906

1907
    pExtW->outputWinNum++;
2,147,483,647✔
1908
    SResultRow* pRow = (SResultRow*)((char*)pExtW->pResultRow + winIdx * pExtW->aggSup.resultRowSize);
2,147,483,647✔
1909

1910
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
2,147,483,647✔
1911

1912
    // no results, continue to check the next one
1913
    if (pRow->numOfRows == 0) {
2,147,483,647✔
1914
      continue;
×
1915
    }
1916

1917
    if (pBlock->info.rows + pRow->numOfRows > pBlock->info.capacity) {
2,147,483,647✔
1918
      uint32_t newSize = pBlock->info.rows + pRow->numOfRows + numOfWin - pExtW->outputWinNum;
1,047,914✔
1919
      TAOS_CHECK_EXIT(blockDataEnsureCapacity(pBlock, newSize));
1,047,914✔
1920
      qDebug("datablock capacity not sufficient, expand to required:%d, current capacity:%d, %s", newSize,
1,047,914✔
1921
             pBlock->info.capacity, GET_TASKID(pTaskInfo));
1922
    }
1923

1924
    TAOS_CHECK_EXIT(copyResultrowToDataBlock(pExprInfo, numOfExprs, pRow, pCtx, pBlock, rowEntryOffset, pTaskInfo));
2,147,483,647✔
1925

1926
    pBlock->info.rows += pRow->numOfRows;
2,147,483,647✔
1927

1928
    TAOS_CHECK_EXIT(extWinAppendWinIdx(pOperator->pTaskInfo, pExtW->pWinRowIdx, pBlock, pRow->winIdx, pRow->numOfRows));
2,147,483,647✔
1929

1930
    if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
2,147,483,647✔
1931
      ++pExtW->outputWinId;
903,536✔
1932
      break;
903,536✔
1933
    }
1934
  }
1935

1936
  qDebug("%s result generated, rows:%" PRId64 ", groupId:%" PRIu64, GET_TASKID(pTaskInfo), pBlock->info.rows,
1,439,292✔
1937
         pBlock->info.id.groupId);
1938

1939
  int32_t rowsBeforeFilter = pBlock->info.rows;
1,439,292✔
1940
  SColumnInfoData* pFilterRes = NULL;
1,439,292✔
1941
  TAOS_CHECK_EXIT(doFilter(pBlock, pOperator->exprSupp.pFilterInfo, NULL, &pFilterRes));
1,439,292✔
1942
  if (pBlock->info.rows < rowsBeforeFilter) {
1,439,292✔
1943
    if (pFilterRes != NULL) {
230✔
1944
      TAOS_CHECK_EXIT(extWinRebuildWinIdxByFilter(pTaskInfo, pExtW->pWinRowIdx, rowsBeforeFilter, pFilterRes));
230✔
1945
    } else {
1946
      // no indicator means all rows are filtered out by short-circuit path
1947
      taosArrayClear(pExtW->pWinRowIdx);
×
1948
    }
1949
  }
1950

1951
  pBlock->info.dataLoad = 1;
1,439,292✔
1952

1953
  *ppRes = (pBlock->info.rows > 0) ? pBlock : NULL;
1,439,292✔
1954

1955
  if (*ppRes) {
1,439,292✔
1956
    (*ppRes)->info.window.skey = pExtW->orgTableTimeRange.skey;
1,190,108✔
1957
    (*ppRes)->info.window.ekey = pExtW->orgTableTimeRange.ekey;
1,190,108✔
1958
  }
1959
  if (pOperator->pTaskInfo->pStreamRuntimeInfo) {
1,439,292✔
1960
    pOperator->pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamBlkWinIdx = pExtW->pWinRowIdx;
488,160✔
1961
  }
1962

1963
_exit:
1,436,892✔
1964
  colDataDestroy(pFilterRes);
1,439,292✔
1965
  taosMemoryFree(pFilterRes);
1,439,292✔
1966

1967
  if (code != TSDB_CODE_SUCCESS) {
1,439,292✔
1968
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1969
  }
1970

1971
  return code;
1,439,292✔
1972
}
1973

1974
static int32_t extWinInitResultRow(SExecTaskInfo*        pTaskInfo, SExternalWindowOperator* pExtW, int32_t winNum) {
1,201,063✔
1975
  if (EEXT_MODE_SCALAR == pExtW->mode) {
1,201,063✔
1976
    return TSDB_CODE_SUCCESS;
982✔
1977
  }
1978

1979
  if (winNum <= pExtW->resultRowCapacity) {
1,200,081✔
1980
    return TSDB_CODE_SUCCESS;
140,035✔
1981
  }
1982
  
1983
  taosMemoryFreeClear(pExtW->pResultRow);
1,060,281✔
1984
  pExtW->resultRowCapacity = -1;
1,060,281✔
1985

1986
  int32_t code = 0, lino = 0;
1,060,281✔
1987
  
1988
  pExtW->pResultRow = taosMemoryCalloc(winNum, pExtW->aggSup.resultRowSize);
1,060,281✔
1989
  QUERY_CHECK_NULL(pExtW->pResultRow, code, lino, _exit, terrno);
1,060,281✔
1990

1991
  pExtW->resultRowCapacity = winNum;
1,060,281✔
1992

1993
_exit:
1,060,281✔
1994

1995
  if (code) {
1,060,281✔
1996
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
1997
  }
1998

1999
  return code;
1,060,281✔
2000
}
2001

2002
static void extWinFreeResultRow(SExternalWindowOperator* pExtW) {
249,184✔
2003
  if (pExtW->resultRowCapacity * pExtW->aggSup.resultRowSize >= 1048576) {
249,184✔
2004
    taosMemoryFreeClear(pExtW->pResultRow);
2,016✔
2005
    pExtW->resultRowCapacity = -1;
2,016✔
2006
  }
2007
  if (pExtW->binfo.pRes && pExtW->binfo.pRes->info.rows * pExtW->aggSup.resultRowSize >= 1048576) {
249,184✔
2008
    blockDataFreeCols(pExtW->binfo.pRes);
×
2009
  }
2010
}
249,184✔
2011

2012
static int32_t extWinInitWindowList(SExternalWindowOperator* pExtW, SExecTaskInfo*        pTaskInfo) {
249,931✔
2013
  if (taosArrayGetSize(pExtW->pWins) > 0) {
249,931✔
2014
    return TSDB_CODE_SUCCESS;
×
2015
  }
2016
  
2017
  int32_t code = 0, lino = 0;
250,166✔
2018
  SStreamRuntimeFuncInfo* pInfo = &pTaskInfo->pStreamRuntimeInfo->funcInfo;
250,166✔
2019
  size_t size = taosArrayGetSize(pInfo->pStreamPesudoFuncVals);
250,166✔
2020
  SExtWinTimeWindow* pWin = taosArrayReserve(pExtW->pWins, size);
250,166✔
2021
  TSDB_CHECK_NULL(pWin, code, lino, _exit, terrno);
249,931✔
2022

2023
  TAOS_CHECK_EXIT(extWinInitResultRow(pTaskInfo, pExtW, size));
249,931✔
2024

2025
  if (pExtW->timeRangeExpr && pExtW->timeRangeExpr->needCalc) {
250,166✔
2026
    TAOS_CHECK_EXIT(scalarCalculateExtWinsTimeRange(pExtW->timeRangeExpr, pInfo, pWin));
92,033✔
2027
    if (qDebugFlag & DEBUG_DEBUG) {
92,033✔
2028
      for (int32_t i = 0; i < size; ++i) {
299,822✔
2029
        qDebug("%s the %d/%d ext window calced initialized, TR[%" PRId64 ", %" PRId64 ")", 
207,789✔
2030
            pTaskInfo->id.str, i, (int32_t)size, pWin[i].tw.skey, pWin[i].tw.ekey);
2031
      }
2032
    }
2033
  } else {
2034
    for (int32_t i = 0; i < size; ++i) {
13,032,702✔
2035
      SSTriggerCalcParam* pParam = taosArrayGet(pInfo->pStreamPesudoFuncVals, i);
12,873,449✔
2036

2037
      pWin[i].tw.skey = pParam->wstart;
12,874,569✔
2038
      pWin[i].tw.ekey = pParam->wend + ((pInfo->triggerType != STREAM_TRIGGER_SLIDING) ? 1 : 0);
12,874,345✔
2039
      pWin[i].winOutIdx = -1;
12,874,345✔
2040

2041
      qDebug("%s the %d/%d ext window initialized, TR[%" PRId64 ", %" PRId64 ")", 
12,874,227✔
2042
          pTaskInfo->id.str, i, (int32_t)size, pWin[i].tw.skey, pWin[i].tw.ekey);
2043
    }
2044
  }
2045
  
2046
  pExtW->outputWinId = pInfo->curIdx;
251,286✔
2047
  pExtW->lastWinId = -1;
250,166✔
2048
  pExtW->blkWinStartIdx = pInfo->curIdx;
250,166✔
2049

2050
_exit:
250,166✔
2051

2052
  if (code) {
250,166✔
2053
    qError("%s %s failed at line %d since %s", pTaskInfo->id.str, __func__, lino, tstrerror(code));
×
2054
  }
2055

2056
  return code;
250,166✔
2057
}
2058

2059
static bool extWinNonAggGotResBlock(SExternalWindowOperator* pExtW) {
2,141✔
2060
  if (pExtW->multiTableMode && !pExtW->inputHasOrder) {
2,141✔
2061
    return false;
2,079✔
2062
  }
2063
  int32_t remainWin = pExtW->outWinIdx - pExtW->outputWinId;
62✔
2064
  if (remainWin > 1 && (NULL == pExtW->timeRangeExpr || !pExtW->timeRangeExpr->needCalc)) {
62✔
2065
    return true;
×
2066
  }
2067
  
2068
  SList* pList = taosArrayGetP(pExtW->pOutputBlocks, pExtW->outputWinId);
62✔
2069
  if (!pList || listNEles(pList) <= 0) {
62✔
2070
    return false;
×
2071
  }
2072
  if (listNEles(pList) > 1) {
62✔
2073
    return true;
×
2074
  }
2075

2076
  SListNode* pNode = listHead(pList);
62✔
2077
  SArray* pIdx = *(SArray**)((SArray**)pNode->data + 1);
62✔
2078
  int32_t* winIdx = taosArrayGetLast(pIdx);
62✔
2079
  if (winIdx && *winIdx < pExtW->blkWinStartIdx) {
62✔
2080
    return true;
×
2081
  }
2082

2083
  return false;
62✔
2084
}
2085

2086
static int32_t getTimeWindowOfBlock(SSDataBlock *pBlock, col_id_t tsSlotId, int64_t *startTs, int64_t *endTs) {
2,860,622✔
2087
  int32_t code = TSDB_CODE_SUCCESS;
2,860,622✔
2088
  int32_t lino = 0;
2,860,622✔
2089
  int32_t tsIndex = -1;
2,860,622✔
2090
  for (int32_t i = 0; i < taosArrayGetSize(pBlock->pDataBlock); i++) {
3,685,944✔
2091
    SColumnInfoData *pCol = (SColumnInfoData*)taosArrayGet(pBlock->pDataBlock, i);
3,685,944✔
2092
    QUERY_CHECK_NULL(pCol, code, lino, _return, terrno)
3,685,944✔
2093
    if (pCol->info.colId == tsSlotId) {
3,685,944✔
2094
      tsIndex = i;
2,860,622✔
2095
      break;
2,860,622✔
2096
    }
2097
  }
2098

2099
  if (tsIndex == -1) {
2,860,622✔
2100
    tsIndex = (int32_t)taosArrayGetSize(pBlock->pDataBlock) - 1;
×
2101
  }
2102

2103
  SColumnInfoData *pColData = (SColumnInfoData*)taosArrayGet(pBlock->pDataBlock, tsIndex);
2,860,622✔
2104
  QUERY_CHECK_NULL(pColData, code, lino, _return, terrno)
2,860,622✔
2105

2106
  GET_TYPED_DATA(*startTs, int64_t, TSDB_DATA_TYPE_TIMESTAMP, colDataGetNumData(pColData, 0), 0);
2,860,622✔
2107
  GET_TYPED_DATA(*endTs, int64_t, TSDB_DATA_TYPE_TIMESTAMP, colDataGetNumData(pColData, pBlock->info.rows - 1), 0);
2,860,622✔
2108

2109
  return code;
2,860,622✔
2110
_return:
×
2111
  qError("failed to get time window of block, %s code:%s, line:%d", __func__, tstrerror(code), lino);
×
2112
  return code;
×
2113
}
2114

2115
static int32_t extWinOpen(SOperatorInfo* pOperator) {
1,201,063✔
2116
  if (OPTR_IS_OPENED(pOperator) && !pOperator->pOperatorGetParam) {
1,201,063✔
2117
    return TSDB_CODE_SUCCESS;
×
2118
  }
2119
  
2120
  int32_t                  code = 0;
1,201,298✔
2121
  int32_t                  lino = 0;
1,201,298✔
2122
  SExecTaskInfo*           pTaskInfo = pOperator->pTaskInfo;
1,201,298✔
2123
  SOperatorInfo*           pDownstream = pOperator->pDownstream[0];
1,201,063✔
2124
  SExternalWindowOperator* pExtW = pOperator->info;
1,201,298✔
2125
  SExprSupp*               pSup = &pOperator->exprSupp;
1,201,063✔
2126

2127
  if (pOperator->pOperatorGetParam) {
1,201,063✔
2128
    SOperatorParam*               pParam = (SOperatorParam*)(pOperator->pOperatorGetParam);
951,132✔
2129
    SOperatorParam*               pDownParam = (SOperatorParam*)(pOperator->pDownstreamGetParams[0]);
951,132✔
2130
    SExchangeOperatorParam*       pExecParam = NULL;
951,132✔
2131
    SExternalWindowOperatorParam* pExtPram = (SExternalWindowOperatorParam*)pParam->value;
951,132✔
2132

2133
    if (pExtW->pWins) {
951,132✔
2134
      taosArrayDestroy(pExtW->pWins);
951,132✔
2135
    }
2136

2137
    pExtW->pWins = pExtPram->ExtWins;
951,132✔
2138

2139
    TAOS_CHECK_EXIT(extWinInitResultRow(pTaskInfo, pExtW, taosArrayGetSize(pExtW->pWins)));
951,132✔
2140
    pExtPram->ExtWins = NULL;
951,132✔
2141
    pExtW->outputWinId = 0;
951,132✔
2142
    pExtW->lastWinId = -1;
951,132✔
2143
    pExtW->blkWinStartIdx = 0;
951,132✔
2144
    pExtW->outWinIdx = 0;
951,132✔
2145
    pExtW->lastSKey = INT64_MIN;
951,132✔
2146
    pExtW->isDynWindow = true;
951,132✔
2147
    pExtW->orgTableTimeRange.skey = INT64_MAX;
951,132✔
2148
    pExtW->orgTableTimeRange.ekey = INT64_MIN;
951,132✔
2149

2150
    QUERY_CHECK_CONDITION(pOperator->numOfDownstream == 1, code, lino, _exit, TSDB_CODE_INVALID_PARA)
951,132✔
2151

2152
    switch (pDownParam->opType) {
951,132✔
2153
      case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE: {
951,132✔
2154
        pExecParam = (SExchangeOperatorParam*)((SOperatorParam*)(pOperator->pDownstreamGetParams[0]))->value;
951,132✔
2155
        if (!pExecParam->multiParams) {
951,132✔
2156
          pExecParam->basic.vgId = pExtW->orgTableVgId;
721,140✔
2157
          taosArrayClear(pExecParam->basic.uidList);
721,140✔
2158
          QUERY_CHECK_NULL(taosArrayPush(pExecParam->basic.uidList, &pExtW->orgTableUid), code, lino, _exit, terrno)
1,442,280✔
2159
        }
2160
        break;
951,132✔
2161
      }
2162
      default:
×
2163
        break;
×
2164
    }
2165

2166
    freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
951,132✔
2167
    pOperator->pOperatorGetParam = NULL;
951,132✔
2168
  } else {
2169
    TAOS_CHECK_EXIT(extWinInitWindowList(pExtW, pTaskInfo));
249,931✔
2170
  }
2171

2172
  while (1) {
5,358,421✔
2173
    pExtW->blkWinIdx = -1;
6,559,719✔
2174
    pExtW->blkWinStartSet = false;
6,559,719✔
2175
    pExtW->blkRowStartIdx = 0;
6,559,719✔
2176

2177
    SSDataBlock* pBlock = getNextBlockFromDownstreamRemainDetach(pOperator, 0);
6,559,719✔
2178
    if (pBlock == NULL) {
6,559,943✔
2179
      if (EEXT_MODE_AGG == pExtW->mode) {
1,201,298✔
2180
        TAOS_CHECK_EXIT(extWinAggHandleEmptyWins(pOperator, pBlock, true, NULL));
1,200,316✔
2181
      }
2182
      pExtW->blkWinStartIdx = pExtW->pWins->size;
1,201,298✔
2183
      break;
1,201,298✔
2184
    }
2185

2186
    if (pExtW->isDynWindow) {
5,358,645✔
2187
      TSKEY skey = 0;
2,860,622✔
2188
      TSKEY ekey = 0;
2,860,622✔
2189
      code = getTimeWindowOfBlock(pBlock, pExtW->primaryTsIndex, &skey, &ekey);
2,860,622✔
2190
      QUERY_CHECK_CODE(code, lino, _exit);
2,860,622✔
2191
      pExtW->orgTableTimeRange.skey = TMIN(pExtW->orgTableTimeRange.skey, skey);
2,860,622✔
2192
      pExtW->orgTableTimeRange.ekey = TMAX(pExtW->orgTableTimeRange.ekey, ekey);
2,860,622✔
2193
    }
2194

2195
    printDataBlock(pBlock, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
5,358,645✔
2196

2197
    qDebug("ext window mode:%d got %" PRId64 " rows from downstream", pExtW->mode, pBlock->info.rows);
5,358,645✔
2198
    
2199
    switch (pExtW->mode) {
5,358,645✔
2200
      case EEXT_MODE_SCALAR:
2,141✔
2201
        TAOS_CHECK_EXIT(extWinProjectOpen(pOperator, pBlock));
2,141✔
2202
        if (extWinNonAggGotResBlock(pExtW)) {
2,141✔
2203
          return code;
×
2204
        }
2205
        break;
2,141✔
2206
      case EEXT_MODE_AGG:
5,356,504✔
2207
        TAOS_CHECK_EXIT(extWinAggOpen(pOperator, pBlock));
5,356,504✔
2208
        break;
5,356,280✔
2209
      case EEXT_MODE_INDEFR_FUNC:
×
2210
        TAOS_CHECK_EXIT(extWinIndefRowsOpen(pOperator, pBlock));
×
2211
        if (extWinNonAggGotResBlock(pExtW)) {
×
2212
          return code;
×
2213
        }
2214
        break;
×
2215
      default:
×
2216
        break;
×
2217
    }
2218
  }
2219

2220
  if (pOperator->pOperatorGetParam) {
1,201,298✔
2221
    freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
×
2222
    pOperator->pOperatorGetParam = NULL;
×
2223
  }
2224
  OPTR_SET_OPENED(pOperator);
1,201,298✔
2225

2226
#if 0
2227
  if (pExtW->mode == EEXT_MODE_AGG) {
2228
    qDebug("ext window before dump final rows num:%d", tSimpleHashGetSize(pExtW->aggSup.pResultRowHashTable));
2229

2230
    code = initGroupedResultInfo(&pExtW->groupResInfo, pExtW->aggSup.pResultRowHashTable, pExtW->binfo.inputTsOrder);
2231
    QUERY_CHECK_CODE(code, lino, _exit);
2232

2233
    qDebug("ext window after dump final rows num:%d", tSimpleHashGetSize(pExtW->aggSup.pResultRowHashTable));
2234
  }
2235
#endif
2236

2237
_exit:
1,201,298✔
2238

2239
  if (code != 0) {
1,201,298✔
2240
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
2241
    pTaskInfo->code = code;
×
2242
    T_LONG_JMP(pTaskInfo->env, code);
×
2243
  }
2244
  
2245
  return code;
1,201,298✔
2246
}
2247

2248
static int32_t extWinNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
1,440,278✔
2249
  int32_t                  code = 0;
1,440,278✔
2250
  int32_t                  lino = 0;
1,440,278✔
2251
  SExternalWindowOperator* pExtW = pOperator->info;
1,440,278✔
2252
  SExecTaskInfo*           pTaskInfo = pOperator->pTaskInfo;
1,440,513✔
2253

2254
  if (pOperator->status == OP_EXEC_DONE && !pOperator->pOperatorGetParam) {
1,440,513✔
2255
    *ppRes = NULL;
×
2256
    return code;
×
2257
  }
2258

2259
  if (pOperator->pOperatorGetParam) {
1,440,513✔
2260
    if (pOperator->status == OP_EXEC_DONE) {
951,132✔
2261
      pOperator->status = OP_NOT_OPENED;
41,528✔
2262
    }
2263
  }
2264

2265
  extWinRecycleBlkNode(pExtW, &pExtW->pLastBlkNode);
1,440,278✔
2266

2267
  if (pOperator->status == OP_NOT_OPENED) {
1,440,278✔
2268
    TAOS_CHECK_EXIT(pOperator->fpSet._openFn(pOperator));
1,201,298✔
2269
  }
2270

2271
  if (pExtW->mode == EEXT_MODE_SCALAR || pExtW->mode == EEXT_MODE_INDEFR_FUNC) {
1,440,278✔
2272
    TAOS_CHECK_EXIT(extWinNonAggOutputRes(pOperator, ppRes));
1,221✔
2273
    if (NULL == *ppRes) {
1,221✔
2274
      setOperatorCompleted(pOperator);
×
2275
      extWinFreeResultRow(pExtW);
×
2276
    }
2277
  } else {
2278
#if 0    
2279
    doBuildResultDatablock(pOperator, &pExtW->binfo, &pExtW->groupResInfo, pExtW->aggSup.pResultBuf);
2280
    bool hasRemain = hasRemainResults(&pExtW->groupResInfo);
2281
    if (!hasRemain) {
2282
      setOperatorCompleted(pOperator);
2283
      break;
2284
    }
2285
    if (pExtW->binfo.pRes->info.rows > 0) break;
2286
#else
2287
    while (1) {
2288
      TAOS_CHECK_EXIT(extWinAggOutputRes(pOperator, ppRes));
1,439,292✔
2289
      if (NULL != *ppRes) {
1,439,292✔
2290
        break;
1,190,108✔
2291
      }
2292

2293
      if (pExtW->outputWinId >= pExtW->pWins->size) {
249,184✔
2294
        setOperatorCompleted(pOperator);
249,184✔
2295
        if (pTaskInfo->pStreamRuntimeInfo) {
249,184✔
2296
          extWinFreeResultRow(pExtW);
249,184✔
2297
        }
2298
        break;
249,184✔
2299
      }
2300
    }
2301
#endif      
2302
  }
2303

2304
  if (*ppRes) {
1,440,513✔
2305
    pOperator->resultInfo.totalRows += (*ppRes)->info.rows;
1,191,329✔
2306
    printDataBlock(*ppRes, __func__, GET_TASKID(pTaskInfo), pTaskInfo->id.queryId);
1,191,329✔
2307
  }
2308
  
2309
_exit:
249,184✔
2310

2311
  if (code) {
1,440,513✔
2312
    qError("%s %s failed at line %d since %s", GET_TASKID(pTaskInfo), __func__, lino, tstrerror(code));
×
2313
    pTaskInfo->code = code;
×
2314
    T_LONG_JMP(pTaskInfo->env, code);
×
2315
  }
2316

2317
  if ((*ppRes) && (*ppRes)->info.rows <= 0) {
1,440,513✔
2318
    *ppRes = NULL;
×
2319
  }
2320

2321
  if (pTaskInfo->execModel == OPTR_EXEC_MODEL_STREAM && (*ppRes)) {
1,440,513✔
2322
    printDataBlock(*ppRes, getStreamOpName(pOperator->operatorType), GET_TASKID(pTaskInfo), pTaskInfo->id.queryId);
240,197✔
2323
  }
2324
  
2325
  return code;
1,440,513✔
2326
}
2327

2328

2329
int32_t createExternalWindowOperator(SOperatorInfo* pDownstream, SPhysiNode* pNode, SExecTaskInfo* pTaskInfo,
1,049,055✔
2330
                                     SOperatorInfo** pOptrOut) {
2331
  SExternalWindowPhysiNode* pPhynode = (SExternalWindowPhysiNode*)pNode;
1,049,055✔
2332
  QRY_PARAM_CHECK(pOptrOut);
1,049,055✔
2333
  int32_t                  code = 0;
1,049,055✔
2334
  int32_t                  lino = 0;
1,049,055✔
2335
  SExternalWindowOperator* pExtW = taosMemoryCalloc(1, sizeof(SExternalWindowOperator));
1,049,055✔
2336
  SOperatorInfo*           pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
1,049,055✔
2337
  pOperator->pPhyNode = pNode;
1,049,055✔
2338
  if (!pExtW || !pOperator) {
1,049,055✔
2339
    code = terrno;
×
2340
    lino = __LINE__;
×
2341
    goto _error;
×
2342
  }
2343
  
2344
  setOperatorInfo(pOperator, "ExternalWindowOperator", QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW, true, OP_NOT_OPENED,
1,049,055✔
2345
                  pExtW, pTaskInfo);
2346
                  
2347
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pPhynode->window.node.pOutputDataBlockDesc);
1,049,055✔
2348
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
1,049,055✔
2349
  initBasicInfo(&pExtW->binfo, pResBlock);
1,049,055✔
2350

2351
  pExtW->primaryTsIndex = ((SColumnNode*)pPhynode->window.pTspk)->slotId;
1,049,055✔
2352
  pExtW->mode = pPhynode->window.pProjs ? EEXT_MODE_SCALAR : (pPhynode->window.indefRowsFunc ? EEXT_MODE_INDEFR_FUNC : EEXT_MODE_AGG);
1,049,055✔
2353
  pExtW->binfo.inputTsOrder = pPhynode->window.node.inputTsOrder = TSDB_ORDER_ASC;
1,049,055✔
2354
  pExtW->binfo.outputTsOrder = pExtW->binfo.inputTsOrder;
1,049,055✔
2355
  pExtW->isDynWindow = false;
1,049,055✔
2356

2357
  if (pTaskInfo->pStreamRuntimeInfo != NULL){
1,049,055✔
2358
    pTaskInfo->pStreamRuntimeInfo->funcInfo.withExternalWindow = true;
138,839✔
2359
  }
2360

2361
  // pExtW->limitInfo = (SLimitInfo){0};
2362
  // initLimitInfo(pPhynode->window.node.pLimit, pPhynode->window.node.pSlimit, &pExtW->limitInfo);
2363

2364
  if (pPhynode->window.pProjs) {
1,049,055✔
2365
    int32_t    numOfScalarExpr = 0;
982✔
2366
    SExprInfo* pScalarExprInfo = NULL;
982✔
2367
    code = createExprInfo(pPhynode->window.pProjs, NULL, &pScalarExprInfo, &numOfScalarExpr);
982✔
2368
    QUERY_CHECK_CODE(code, lino, _error);
982✔
2369

2370
    code = initExprSupp(&pExtW->scalarSupp, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
982✔
2371
    QUERY_CHECK_CODE(code, lino, _error);
982✔
2372

2373
  //if (pExtW->multiTableMode) {
2374
    pExtW->pOutputBlocks = taosArrayInit_s(POINTER_BYTES, STREAM_CALC_REQ_MAX_WIN_NUM);
982✔
2375
    if (!pExtW->pOutputBlocks) QUERY_CHECK_CODE(terrno, lino, _error);
982✔
2376
  //}
2377
    pExtW->pFreeBlocks = tdListNew(POINTER_BYTES * 2);
982✔
2378
    QUERY_CHECK_NULL(pExtW->pFreeBlocks, code, lino, _error, terrno);
982✔
2379
  } else if (pExtW->mode == EEXT_MODE_AGG) {
1,048,073✔
2380
    if (pPhynode->window.pExprs != NULL) {
1,048,073✔
2381
      int32_t    num = 0;
2,016✔
2382
      SExprInfo* pSExpr = NULL;
2,016✔
2383
      code = createExprInfo(pPhynode->window.pExprs, NULL, &pSExpr, &num);
2,016✔
2384
      QUERY_CHECK_CODE(code, lino, _error);
2,016✔
2385
    
2386
      code = initExprSupp(&pExtW->scalarSupp, pSExpr, num, &pTaskInfo->storageAPI.functionStore);
2,016✔
2387
      if (code != TSDB_CODE_SUCCESS) {
2,016✔
2388
        goto _error;
×
2389
      }
2390
      checkIndefRowsFuncs(&pExtW->scalarSupp);
2,016✔
2391
    }
2392
    
2393
    size_t keyBufSize = sizeof(int64_t) * 2 + POINTER_BYTES;
1,048,073✔
2394
    initResultSizeInfo(&pOperator->resultInfo, 4096);
1,048,073✔
2395
    //code = blockDataEnsureCapacity(pExtW->binfo.pRes, pOperator->resultInfo.capacity);
2396
    //QUERY_CHECK_CODE(code, lino, _error);
2397

2398
    pExtW->pWinRowIdx = taosArrayInit(4096, sizeof(int64_t));
1,048,073✔
2399
    TSDB_CHECK_NULL(pExtW->pWinRowIdx, code, lino, _error, terrno);
1,048,073✔
2400
    
2401
    int32_t num = 0;
1,048,073✔
2402
    SExprInfo* pExprInfo = NULL;
1,048,073✔
2403
    code = createExprInfo(pPhynode->window.pFuncs, NULL, &pExprInfo, &num);
1,048,073✔
2404
    QUERY_CHECK_CODE(code, lino, _error);
1,048,073✔
2405
    pOperator->exprSupp.hasWindow = true;
1,048,073✔
2406
    pOperator->exprSupp.hasWindowOrGroup = true;
1,048,073✔
2407
    code = initAggSup(&pOperator->exprSupp, &pExtW->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str, 0, 0);
1,048,073✔
2408
    QUERY_CHECK_CODE(code, lino, _error);
1,048,073✔
2409

2410
    code = filterInitFromNode((SNode*)pNode->pConditions, &pOperator->exprSupp.pFilterInfo, 0,
1,048,073✔
2411
                              pTaskInfo->pStreamRuntimeInfo);
1,048,073✔
2412
    QUERY_CHECK_CODE(code, lino, _error);
1,047,955✔
2413

2414
    nodesWalkExprs(pPhynode->window.pFuncs, extWinHasCountLikeFunc, &pExtW->hasCountFunc);
1,047,955✔
2415
    if (pExtW->hasCountFunc) {
1,048,073✔
2416
      code = extWinCreateEmptyInputBlock(pOperator, &pExtW->pEmptyInputBlock);
605,237✔
2417
      QUERY_CHECK_CODE(code, lino, _error);
605,237✔
2418
      qDebug("%s ext window has CountLikeFunc", pOperator->pTaskInfo->id.str);
605,237✔
2419
    } else {
2420
      qDebug("%s ext window doesn't have CountLikeFunc", pOperator->pTaskInfo->id.str);
442,836✔
2421
    }
2422

2423
    code = initExecTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pTaskInfo->window);
1,048,073✔
2424
    QUERY_CHECK_CODE(code, lino, _error);
1,048,073✔
2425

2426
    pExtW->lastSKey = INT64_MIN;
1,048,073✔
2427
  } else {
2428
    size_t  keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
×
2429
    
2430
    if (pPhynode->window.pExprs != NULL) {
×
2431
      int32_t    num = 0;
×
2432
      SExprInfo* pSExpr = NULL;
×
2433
      code = createExprInfo(pPhynode->window.pExprs, NULL, &pSExpr, &num);
×
2434
      QUERY_CHECK_CODE(code, lino, _error);
×
2435
    
2436
      code = initExprSupp(&pExtW->scalarSupp, pSExpr, num, &pTaskInfo->storageAPI.functionStore);
×
2437
      if (code != TSDB_CODE_SUCCESS) {
×
2438
        goto _error;
×
2439
      }
2440
    }
2441
    
2442
    int32_t    numOfExpr = 0;
×
2443
    SExprInfo* pExprInfo = NULL;
×
2444
    code = createExprInfo(pPhynode->window.pFuncs, NULL, &pExprInfo, &numOfExpr);
×
2445
    TSDB_CHECK_CODE(code, lino, _error);
×
2446
    
2447
    code = initAggSup(&pOperator->exprSupp, &pExtW->aggSup, pExprInfo, numOfExpr, keyBufSize, pTaskInfo->id.str,
×
2448
                              NULL, &pTaskInfo->storageAPI.functionStore);
2449
    TSDB_CHECK_CODE(code, lino, _error);
×
2450
    pOperator->exprSupp.hasWindowOrGroup = false;
×
2451
    
2452
    //code = setFunctionResultOutput(pOperator, &pExtW->binfo, &pExtW->aggSup, MAIN_SCAN, numOfExpr);
2453
    //TSDB_CHECK_CODE(code, lino, _error);
2454
    
2455
    code = filterInitFromNode((SNode*)pNode->pConditions, &pOperator->exprSupp.pFilterInfo, 0,
×
2456
                              pTaskInfo->pStreamRuntimeInfo);
×
2457
    TSDB_CHECK_CODE(code, lino, _error);
×
2458
    
2459
    pExtW->binfo.inputTsOrder = pNode->inputTsOrder;
×
2460
    pExtW->binfo.outputTsOrder = pNode->outputTsOrder;
×
2461
    code = setRowTsColumnOutputInfo(pOperator->exprSupp.pCtx, numOfExpr, &pExtW->pPseudoColInfo);
×
2462
    TSDB_CHECK_CODE(code, lino, _error);
×
2463

2464
  //if (pExtW->multiTableMode) {
2465
    pExtW->pOutputBlocks = taosArrayInit_s(POINTER_BYTES, STREAM_CALC_REQ_MAX_WIN_NUM);
×
2466
    if (!pExtW->pOutputBlocks) QUERY_CHECK_CODE(terrno, lino, _error);
×
2467
  //}
2468
    pExtW->pFreeBlocks = tdListNew(POINTER_BYTES * 2);
×
2469
    QUERY_CHECK_NULL(pExtW->pFreeBlocks, code, lino, _error, terrno);  
×
2470
  }
2471

2472
  pExtW->pWins = taosArrayInit(4096, sizeof(SExtWinTimeWindow));
1,048,820✔
2473
  if (!pExtW->pWins) QUERY_CHECK_CODE(terrno, lino, _error);
1,049,055✔
2474
  
2475
  //initResultRowInfo(&pExtW->binfo.resultRowInfo);
2476

2477
  pExtW->timeRangeExpr = (STimeRangeNode*)pPhynode->pTimeRange;
1,049,055✔
2478
  if (pExtW->timeRangeExpr) {
1,049,055✔
2479
    QUERY_CHECK_NULL(pExtW->timeRangeExpr->pStart, code, lino, _error, TSDB_CODE_STREAM_INTERNAL_ERROR);
138,839✔
2480
    QUERY_CHECK_NULL(pExtW->timeRangeExpr->pEnd, code, lino, _error, TSDB_CODE_STREAM_INTERNAL_ERROR);
138,839✔
2481
  }
2482

2483
  if (pPhynode->isSingleTable) {
1,049,055✔
2484
    pExtW->getWinFp = (pExtW->timeRangeExpr && (pExtW->timeRangeExpr->needCalc || (pTaskInfo->pStreamRuntimeInfo->funcInfo.addOptions & CALC_SLIDING_OVERLAP))) ? extWinGetOvlpWin : extWinGetNoOvlpWin;
737,574✔
2485
    pExtW->multiTableMode = false;
737,574✔
2486
  } else {
2487
    pExtW->getWinFp = (pExtW->timeRangeExpr && (pExtW->timeRangeExpr->needCalc || (pTaskInfo->pStreamRuntimeInfo->funcInfo.addOptions & CALC_SLIDING_OVERLAP))) ? extWinGetMultiTbOvlpWin : extWinGetMultiTbNoOvlpWin;
311,481✔
2488
    pExtW->multiTableMode = true;
311,481✔
2489
  }
2490
  pExtW->inputHasOrder = pPhynode->inputHasOrder;
1,049,055✔
2491
  pExtW->orgTableUid = pPhynode->orgTableUid;
1,049,055✔
2492
  pExtW->orgTableVgId = pPhynode->orgTableVgId;
1,049,055✔
2493

2494
  pOperator->fpSet = createOperatorFpSet(extWinOpen, extWinNext, NULL, destroyExternalWindowOperatorInfo,
1,049,055✔
2495
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
2496
  setOperatorResetStateFn(pOperator, resetExternalWindowOperator);
1,049,055✔
2497
  code = appendDownstream(pOperator, &pDownstream, 1);
1,049,055✔
2498
  if (code != 0) {
1,049,055✔
2499
    goto _error;
×
2500
  }
2501

2502
  *pOptrOut = pOperator;
1,049,055✔
2503
  return code;
1,049,055✔
2504

2505
_error:
×
2506

2507
  if (pExtW != NULL) {
×
2508
    destroyExternalWindowOperatorInfo(pExtW);
×
2509
  }
2510

2511
  destroyOperatorAndDownstreams(pOperator, &pDownstream, 1);
×
2512
  pTaskInfo->code = code;
×
2513
  qError("error happens at %s %d, code:%s", __func__, lino, tstrerror(code));
×
2514
  return code;
×
2515
}
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