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

taosdata / TDengine / #4938

23 Jan 2026 09:40AM UTC coverage: 66.8% (+0.006%) from 66.794%
#4938

push

travis-ci

web-flow
fix: case failuer caused by the modification of the error description (#34391)

204187 of 305671 relevant lines covered (66.8%)

124015580.65 hits per line

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

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

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

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

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

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

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

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

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

143
_exit:
660✔
144

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

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

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

174

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

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

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

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

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

226
  taosMemoryFree(pNode);
497✔
227

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

231

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

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

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

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

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

268
  taosMemoryFreeClear(pInfo);
840,327✔
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) {
2,123✔
281
  SMergeAlignedExternalWindowOperator* pMlExtInfo = (SMergeAlignedExternalWindowOperator*)pOperator;
2,123✔
282
  destroyExternalWindowOperatorInfo(pMlExtInfo->pExtW);
2,123✔
283
  taosMemoryFreeClear(pMlExtInfo);
2,123✔
284
}
2,123✔
285

286
int64_t* extWinExtractTsCol(SSDataBlock* pBlock, int32_t primaryTsIndex, SExecTaskInfo* pTaskInfo) {
4,281,875✔
287
  TSKEY* tsCols = NULL;
4,281,875✔
288

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

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

307
  return tsCols;
4,282,088✔
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;
52,174,391✔
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;
38,162,003✔
326
  }
327
}
2,147,483,647✔
328

329

330
static void extWinIncCurWinOutIdx(SStreamRuntimeInfo* pStreamRuntimeInfo) {
497✔
331
  pStreamRuntimeInfo->funcInfo.curOutIdx++;
497✔
332
}
497✔
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;
14,737,047✔
356
  *pRowIdx = pBlock->info.rows - rows;
14,736,420✔
357

358
  TSDB_CHECK_NULL(taosArrayPush(pIdx, &res), code, lino, _exit, terrno);
14,733,076✔
359

360
_exit:
14,733,076✔
361

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

366
  return code;
14,732,449✔
367
}
368

369

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

387

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

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

421
_exit:
2,509,529✔
422

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

427
  return code;
2,509,529✔
428
}
429

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

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

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

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

456
  int32_t currPos = startPos;
9,438✔
457
  pMlExtInfo->curTs = pWin->skey;
9,438✔
458
  
459
  while (++currPos < pBlock->info.rows) {
7,527,685✔
460
    if (tsCols[currPos] == pMlExtInfo->curTs) continue;
7,518,038✔
461

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

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

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

477
    pMlExtInfo->curTs = pWin->skey;
2,506,152✔
478
  }
479

480
  code = applyAggFunctionOnPartialTuples(pTaskInfo, pSup->pCtx, &pExtW->twAggSup.timeWindowData, startPos,
18,876✔
481
                                         currPos - startPos, pBlock->info.rows, pSup->numOfExprs);
9,438✔
482

483
_exit:
9,438✔
484

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

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

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

511
_exit:
×
512

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

517
  return code;  
×
518
}
519

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

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

531
_exit:
×
532

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

537
  return code;
×
538
}
539

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

550
  taosArrayClear(pExtW->pWinRowIdx);
3,586✔
551
  blockDataCleanup(pRes);
3,586✔
552

553
  while (1) {
8,811✔
554
    SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
12,397✔
555

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

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

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

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

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

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

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

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

601
  if (pOperator->status == OP_EXEC_DONE) {
6,127✔
602
    (*ppRes) = NULL;
2,541✔
603
    return TSDB_CODE_SUCCESS;
2,541✔
604
  }
605

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

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

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

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

629
_exit:
3,586✔
630

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

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

646
  taosArrayClear(pExtW->pWins);
3,795✔
647

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

795
  pExtW->outWinIdx = 0;
275,512✔
796
  pExtW->lastSKey = INT64_MIN;
275,512✔
797
  pExtW->isDynWindow = false;
275,512✔
798

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

803
_exit:
7,310✔
804

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

812
static EDealRes extWinHasCountLikeFunc(SNode* pNode, void* res) {
3,799,372✔
813
  if (QUERY_NODE_FUNCTION == nodeType(pNode)) {
3,799,372✔
814
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
1,401,144✔
815
    if (fmIsCountLikeFunc(pFunc->funcId) || (pFunc->hasOriginalFunc && fmIsCountLikeFunc(pFunc->originalFuncId))) {
1,401,144✔
816
      *(bool*)res = true;
435,653✔
817
      return DEAL_RES_END;
436,099✔
818
    }
819
  }
820
  return DEAL_RES_CONTINUE;
3,363,719✔
821
}
822

823

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

832
  SExternalWindowOperator* pExtW = pOperator->info;
436,099✔
833

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

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

843
  pBlock->info.rows = 1;
436,099✔
844
  pBlock->info.capacity = 0;
436,099✔
845

846
  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
1,856,657✔
847
    SColumnInfoData colInfo = {0};
1,420,558✔
848
    colInfo.hasNull = true;
1,420,558✔
849
    colInfo.info.type = TSDB_DATA_TYPE_NULL;
1,420,558✔
850
    colInfo.info.bytes = 1;
1,420,558✔
851

852
    SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
1,420,558✔
853
    for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
2,854,794✔
854
      SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
1,434,022✔
855
      if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
1,434,022✔
856
        int32_t slotId = pFuncParam->pCol->slotId;
1,339,911✔
857
        int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
1,339,911✔
858
        if (slotId >= numOfCols) {
1,339,911✔
859
          code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
1,043,062✔
860
          QUERY_CHECK_CODE(code, lino, _end);
1,043,062✔
861

862
          for (int32_t k = numOfCols; k < slotId + 1; ++k) {
2,459,334✔
863
            void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
1,416,272✔
864
            QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1,416,272✔
865
          }
866
        }
867
      } else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
94,325✔
868
        // do nothing
869
      }
870
    }
871
  }
872

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

876
  for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
1,852,371✔
877
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
1,416,242✔
878
    QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
1,416,272✔
879
    colDataSetNULL(pColInfoData, 0);
880
  }
881
  *ppBlock = pBlock;
436,099✔
882

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

891

892

893
static int extWinTsWinCompare(const void* pLeft, const void* pRight) {
8,668,460✔
894
  int64_t ts = *(int64_t*)pLeft;
8,668,460✔
895
  SExtWinTimeWindow* pWin = (SExtWinTimeWindow*)pRight;
8,668,876✔
896
  if (ts < pWin->tw.skey) {
8,668,876✔
897
    return -1;
4,679,619✔
898
  }
899
  if (ts >= pWin->tw.ekey) {
3,989,262✔
900
    return 1;
1,344,164✔
901
  }
902

903
  return 0;
2,645,307✔
904
}
905

906

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

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

930
  return -1;
486✔
931
}
932

933
static int32_t extWinGetNoOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
2,147,483,647✔
934
  SExternalWindowOperator* pExtW = pOperator->info;
2,147,483,647✔
935
  if ((*startPos) >= pInfo->rows) {
2,147,483,647✔
936
    qDebug("%s %s blk rowIdx %d reach the end, size: %d, skip block", 
1,285,199✔
937
        GET_TASKID(pOperator->pTaskInfo), __func__, *startPos, (int32_t)pInfo->rows);
938
    *ppWin = NULL;
1,285,199✔
939
    return TSDB_CODE_SUCCESS;
1,285,199✔
940
  }
941
  
942
  if (pExtW->blkWinIdx < 0) {
2,147,483,647✔
943
    pExtW->blkWinIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
1,507,196✔
944
  } else {
945
    pExtW->blkWinIdx++;
2,147,483,647✔
946
  }
947

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

963
  int32_t r = *startPos;
2,147,483,647✔
964

965
  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✔
966

967
  // TODO handle desc order
968
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
2,147,483,647✔
969
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
2,147,483,647✔
970
    for (; r < pInfo->rows; ++r) {
2,147,483,647✔
971
      if (tsCol[r] < pWin->tw.skey) {
2,147,483,647✔
972
        continue;
309,867,576✔
973
      }
974

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

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

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

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

996
        return TSDB_CODE_SUCCESS;
1,784,393,532✔
997
      }
998

999
      break;
5,918✔
1000
    }
1001

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

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

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

1014
static int32_t extWinGetOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
191,380✔
1015
  SExternalWindowOperator* pExtW = pOperator->info;
191,380✔
1016
  if (pExtW->blkWinIdx < 0) {
191,380✔
1017
    pExtW->blkWinIdx = pExtW->blkWinStartIdx;
12,269✔
1018
  } else {
1019
    pExtW->blkWinIdx++;
179,111✔
1020
  }
1021

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

1037
  int64_t r = 0;
179,824✔
1038

1039
  qDebug("%s %s start to get ovlp win from winIdx %d rowIdx %d", GET_TASKID(pOperator->pTaskInfo), __func__, pExtW->blkWinIdx, pExtW->blkRowStartIdx);
179,824✔
1040
  
1041
  // TODO handle desc order
1042
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
180,537✔
1043
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
180,537✔
1044
    for (r = pExtW->blkRowStartIdx; r < pInfo->rows; ++r) {
313,155✔
1045
      if (tsCol[r] < pWin->tw.skey) {
312,442✔
1046
        pExtW->blkRowStartIdx = r + 1;
132,618✔
1047
        continue;
132,618✔
1048
      }
1049

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

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

1067
      break;
713✔
1068
    }
1069

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

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

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

1086

1087
static int32_t extWinGetMultiTbNoOvlpWin(SOperatorInfo* pOperator, int64_t* tsCol, int32_t* startPos, SDataBlockInfo* pInfo, SExtWinTimeWindow** ppWin, int32_t* winRows) {
733,640,835✔
1088
  SExternalWindowOperator* pExtW = pOperator->info;
733,640,835✔
1089
  if ((*startPos) >= pInfo->rows) {
733,643,387✔
1090
    qDebug("%s %s blk rowIdx %d reach the end, size: %d, skip block", 
1,754,171✔
1091
        GET_TASKID(pOperator->pTaskInfo), __func__, *startPos, (int32_t)pInfo->rows);
1092
    *ppWin = NULL;
1,754,171✔
1093
    return TSDB_CODE_SUCCESS;
1,754,171✔
1094
  }
1095
  
1096
  if (pExtW->blkWinIdx < 0) {
731,892,401✔
1097
    pExtW->blkWinIdx = extWinGetMultiTbWinFromTs(pOperator, pExtW, tsCol, pInfo->rows, startPos);
1,798,649✔
1098
    if (pExtW->blkWinIdx < 0) {
1,798,649✔
1099
      qDebug("%s %s blk TR[%" PRId64 ", %" PRId64 ") not in any win, skip block", 
486✔
1100
          GET_TASKID(pOperator->pTaskInfo), __func__, tsCol[0], tsCol[pInfo->rows - 1]);
1101
      *ppWin = NULL;
486✔
1102
      return TSDB_CODE_SUCCESS;
486✔
1103
    }
1104

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

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

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

1132
  int32_t r = *startPos;
730,049,546✔
1133

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

1136
  // TODO handle desc order
1137
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
730,063,474✔
1138
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
730,063,479✔
1139
    for (; r < pInfo->rows; ++r) {
890,718,501✔
1140
      if (tsCol[r] < pWin->tw.skey) {
890,718,287✔
1141
        continue;
160,655,654✔
1142
      }
1143

1144
      if (tsCol[r] < pWin->tw.ekey) {
730,062,429✔
1145
        extWinSetCurWinIdx(pOperator, pExtW->blkWinIdx);
644,305,910✔
1146
        *ppWin = pWin;
644,305,696✔
1147
        *startPos = r;
644,305,487✔
1148
        *winRows = getNumOfRowsInTimeWindow(pInfo, tsCol, r, pWin->tw.ekey - 1, binarySearchForKey, NULL, pExtW->binfo.inputTsOrder);
644,305,278✔
1149

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

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

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

1165
        return TSDB_CODE_SUCCESS;
85,744,920✔
1166
      }
1167

1168
      break;
11,808✔
1169
    }
1170

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

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

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

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

1200
  pWin = NULL;
63,400✔
1201
  int32_t w = 0;
63,400✔
1202
  for (int64_t i = 1; i < rowNum; ++i) {
127,226✔
1203
    for (; w < pExtW->pWins->size; ++w) {
148,526✔
1204
      pWin = TARRAY_GET_ELEM(pExtW->pWins, w);
148,526✔
1205
      if (tsCol[i] < pWin->tw.skey) {
148,526✔
1206
        break;
63,826✔
1207
      }
1208

1209
      if (tsCol[i] < pWin->tw.ekey) {
84,700✔
1210
        *startPos = i;
21,300✔
1211
        return w;
21,300✔
1212
      }
1213
    }
1214
  }
1215

1216
  return -1;
42,100✔
1217
}
1218

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

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

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

1257
  int64_t r = 0;
3,758,401✔
1258

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

1261
  // TODO handle desc order
1262
  for (; pExtW->blkWinIdx < pExtW->pWins->size; ++pExtW->blkWinIdx) {
3,821,728✔
1263
    pWin = taosArrayGet(pExtW->pWins, pExtW->blkWinIdx);
3,821,728✔
1264
    for (r = pExtW->blkRowStartIdx; r < pInfo->rows; ++r) {
12,950,554✔
1265
      if (tsCol[r] < pWin->tw.skey) {
12,949,300✔
1266
        pExtW->blkRowStartIdx = r + 1;
9,129,244✔
1267
        continue;
9,128,826✔
1268
      }
1269

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

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

1282
      break;
62,700✔
1283
    }
1284

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

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

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

1297

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

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

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

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

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

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

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

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

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

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

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

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

1361
  return code;
2,147,483,647✔
1362
}
1363

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

1370
  SExprSupp*               pSup = &pOperator->exprSupp;
2,147,483,647✔
1371
  SExternalWindowOperator* pExtW = pOperator->info;
2,147,483,647✔
1372
  return applyAggFunctionOnPartialTuples(pOperator->pTaskInfo, pSup->pCtx, &pExtW->twAggSup.timeWindowData, startPos,
2,147,483,647✔
1373
                                         forwardRows, pInputBlock->info.rows, pSup->numOfExprs);
2,147,483,647✔
1374

1375
}
1376

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

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

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

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

1398
  return false;
×
1399
}
1400

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

1424
_exit:
1,157✔
1425

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

1430
  return code;
1,157✔
1431
}
1432

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

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

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

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

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

1462
_exit:
1,157✔
1463

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

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

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

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

1501
  return code;
717✔
1502
}
1503

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

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

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

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

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

1526
_exit:
×
1527

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

1532
  return code;
×
1533
}
1534

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

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

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

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

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

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

1563
  return code;
×
1564
}
1565

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

1591
_exit:
×
1592

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

1597
  return code;
×
1598
}
1599

1600

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

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

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

1622
_exit:
×
1623

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

1631

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

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

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

1660
  return code;
×
1661
}
1662

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

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

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

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

1686
    break;
497✔
1687
  }
1688

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

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

1700
_exit:
497✔
1701

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

1706
  return code;
497✔
1707
}
1708

1709
static int32_t extWinAggHandleEmptyWins(SOperatorInfo* pOperator, SSDataBlock* pBlock, bool allRemains, SExtWinTimeWindow* pWin) {
2,147,483,647✔
1710
  int32_t code = 0, lino = 0;
2,147,483,647✔
1711
  SExternalWindowOperator* pExtW = (SExternalWindowOperator*)pOperator->info;
2,147,483,647✔
1712
  SExprSupp* pSup = &pOperator->exprSupp;
2,147,483,647✔
1713
  int32_t currIdx = extWinGetCurWinIdx(pOperator->pTaskInfo);
2,147,483,647✔
1714

1715
  if (NULL == pExtW->pEmptyInputBlock || (pWin && pWin->tw.skey == pExtW->lastSKey)) {
2,147,483,647✔
1716
    goto _exit;
2,147,483,647✔
1717
  }
1718

1719
  bool ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
2,147,483,647✔
1720
  int32_t endIdx = allRemains ? (pExtW->pWins->size - 1) : (currIdx - 1);
2,147,483,647✔
1721
  SResultRowInfo* pResultRowInfo = &pExtW->binfo.resultRowInfo;
2,147,483,647✔
1722
  SSDataBlock* pInput = pExtW->pEmptyInputBlock;
2,147,483,647✔
1723

1724
  if ((pExtW->lastWinId + 1) <= endIdx) {
2,147,483,647✔
1725
    TAOS_CHECK_EXIT(setInputDataBlock(pSup, pExtW->pEmptyInputBlock, pExtW->binfo.inputTsOrder, MAIN_SCAN, true));
515,675✔
1726
  }
1727
  
1728
  for (int32_t i = pExtW->lastWinId + 1; i <= endIdx; ++i) {
2,147,483,647✔
1729
    SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, i);
1,166,631,988✔
1730

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

1735
    TAOS_CHECK_EXIT(extWinAggSetWinOutputBuf(pOperator, pWin, pSup, &pExtW->aggSup, pOperator->pTaskInfo));
1,166,631,988✔
1736

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

1743
  
1744
_exit:
2,147,483,647✔
1745

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

1753
    if (!allRemains) {
2,147,483,647✔
1754
      extWinSetCurWinIdx(pOperator, currIdx);  
2,147,483,647✔
1755
    }
1756
  }
1757

1758
  return code;
2,147,483,647✔
1759
}
1760

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

1770
  while (true) {
1771
    TAOS_CHECK_EXIT((*pExtW->getWinFp)(pOperator, tsCol, &startPos, &pInputBlock->info, &pWin, &winRows));
2,147,483,647✔
1772
    if (pWin == NULL) {
2,147,483,647✔
1773
      break;
4,271,933✔
1774
    }
1775

1776
    TAOS_CHECK_EXIT(extWinAggHandleEmptyWins(pOperator, pInputBlock, false, pWin));
2,147,483,647✔
1777

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

1781
    if (!scalarCalc) {
2,147,483,647✔
1782
      if (pExtW->scalarSupp.pExprInfo) {
4,228,634✔
1783
        SExprSupp* pScalarSup = &pExtW->scalarSupp;
5,643✔
1784
        TAOS_CHECK_EXIT(projectApplyFunctions(pScalarSup->pExprInfo, pInputBlock, pInputBlock, pScalarSup->pCtx, pScalarSup->numOfExprs,
5,643✔
1785
                                     pExtW->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo)));
1786
      }
1787
      
1788
      scalarCalc = true;
4,228,634✔
1789
    }
1790

1791
    if (pWin->tw.skey != pExtW->lastSKey || pWin->tw.skey == INT64_MIN) {
2,147,483,647✔
1792
      TAOS_CHECK_EXIT(extWinAggSetWinOutputBuf(pOperator, pWin, &pOperator->exprSupp, &pExtW->aggSup, pOperator->pTaskInfo));
2,147,483,647✔
1793
    }
1794
    
1795
    updateTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pWin->tw, 1);
2,147,483,647✔
1796
    TAOS_CHECK_EXIT(extWinAggDo(pOperator, startPos, winRows, pInputBlock));
2,147,483,647✔
1797
    
1798
    pExtW->lastSKey = pWin->tw.skey;
2,147,483,647✔
1799
    pExtW->lastWinId = extWinGetCurWinIdx(pOperator->pTaskInfo);
2,147,483,647✔
1800
    startPos += winRows;
2,147,483,647✔
1801
  }
1802

1803
_exit:
4,271,933✔
1804

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

1809
  return code;
4,271,724✔
1810
}
1811

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

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

1828
  for (; pExtW->outputWinId < pExtW->pWins->size; ++pExtW->outputWinId) {
2,147,483,647✔
1829
    SExtWinTimeWindow* pWin = taosArrayGet(pExtW->pWins, pExtW->outputWinId);
2,147,483,647✔
1830
    int32_t            winIdx = pWin->winOutIdx;
2,147,483,647✔
1831
    if (winIdx < 0) {
2,147,483,647✔
1832
      continue;
11,659,602✔
1833
    }
1834

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

1838
    doUpdateNumOfRows(pCtx, pRow, numOfExprs, rowEntryOffset);
2,147,483,647✔
1839

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

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

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

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

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

1858
    if (pBlock->info.rows >= pOperator->resultInfo.threshold) {
2,147,483,647✔
1859
      ++pExtW->outputWinId;
763,577✔
1860
      break;
763,577✔
1861
    }
1862
  }
1863

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

1869
  *ppRes = (pBlock->info.rows > 0) ? pBlock : NULL;
1,147,701✔
1870

1871
  if (*ppRes) {
1,147,701✔
1872
    (*ppRes)->info.window.skey = pExtW->orgTableTimeRange.skey;
965,883✔
1873
    (*ppRes)->info.window.ekey = pExtW->orgTableTimeRange.ekey;
965,883✔
1874
  }
1875
  if (pOperator->pTaskInfo->pStreamRuntimeInfo) {
1,147,701✔
1876
    pOperator->pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamBlkWinIdx = pExtW->pWinRowIdx;
364,049✔
1877
  }
1878

1879
_exit:
783,438✔
1880

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

1885
  return code;
1,147,701✔
1886
}
1887

1888
static int32_t extWinInitResultRow(SExecTaskInfo*        pTaskInfo, SExternalWindowOperator* pExtW, int32_t winNum) {
965,533✔
1889
  if (EEXT_MODE_SCALAR == pExtW->mode) {
965,533✔
1890
    return TSDB_CODE_SUCCESS;
277✔
1891
  }
1892

1893
  if (winNum <= pExtW->resultRowCapacity) {
965,256✔
1894
    return TSDB_CODE_SUCCESS;
115,989✔
1895
  }
1896
  
1897
  taosMemoryFreeClear(pExtW->pResultRow);
849,047✔
1898
  pExtW->resultRowCapacity = -1;
849,267✔
1899

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

1905
  pExtW->resultRowCapacity = winNum;
849,267✔
1906

1907
_exit:
849,267✔
1908

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

1913
  return code;
849,267✔
1914
}
1915

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

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

1937
  TAOS_CHECK_EXIT(extWinInitResultRow(pTaskInfo, pExtW, size));
182,095✔
1938

1939
  if (pExtW->timeRangeExpr && pExtW->timeRangeExpr->needCalc) {
182,095✔
1940
    TAOS_CHECK_EXIT(scalarCalculateExtWinsTimeRange(pExtW->timeRangeExpr, pInfo, pWin));
71,003✔
1941
    if (qDebugFlag & DEBUG_DEBUG) {
71,003✔
1942
      for (int32_t i = 0; i < size; ++i) {
235,133✔
1943
        qDebug("%s the %d/%d ext window calced initialized, TR[%" PRId64 ", %" PRId64 ")", 
164,130✔
1944
            pTaskInfo->id.str, i, (int32_t)size, pWin[i].tw.skey, pWin[i].tw.ekey);
1945
      }
1946
    }
1947
  } else {
1948
    for (int32_t i = 0; i < size; ++i) {
11,403,544✔
1949
      SSTriggerCalcParam* pParam = taosArrayGet(pInfo->pStreamPesudoFuncVals, i);
11,292,452✔
1950

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

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

1964
_exit:
182,095✔
1965

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

1970
  return code;
182,095✔
1971
}
1972

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

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

1997
  return false;
57✔
1998
}
1999

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

2013
  if (tsIndex == -1) {
1,979,798✔
2014
    tsIndex = (int32_t)taosArrayGetSize(pBlock->pDataBlock) - 1;
×
2015
  }
2016

2017
  SColumnInfoData *pColData = (SColumnInfoData*)taosArrayGet(pBlock->pDataBlock, tsIndex);
1,979,798✔
2018
  QUERY_CHECK_NULL(pColData, code, lino, _return, terrno)
1,979,798✔
2019

2020
  GET_TYPED_DATA(*startTs, int64_t, TSDB_DATA_TYPE_TIMESTAMP, colDataGetNumData(pColData, 0), 0);
1,979,798✔
2021
  GET_TYPED_DATA(*endTs, int64_t, TSDB_DATA_TYPE_TIMESTAMP, colDataGetNumData(pColData, pBlock->info.rows - 1), 0);
1,979,798✔
2022

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

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

2041
  if (pOperator->pOperatorGetParam) {
965,533✔
2042
    SOperatorParam*               pParam = (SOperatorParam*)(pOperator->pOperatorGetParam);
783,438✔
2043
    SOperatorParam*               pDownParam = (SOperatorParam*)(pOperator->pDownstreamGetParams[0]);
783,438✔
2044
    SExchangeOperatorParam*       pExecParam = NULL;
783,438✔
2045
    SExternalWindowOperatorParam* pExtPram = (SExternalWindowOperatorParam*)pParam->value;
783,438✔
2046

2047
    if (pExtW->pWins) {
783,438✔
2048
      taosArrayDestroy(pExtW->pWins);
783,438✔
2049
    }
2050

2051
    pExtW->pWins = pExtPram->ExtWins;
783,438✔
2052

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

2064
    QUERY_CHECK_CONDITION(pOperator->numOfDownstream == 1, code, lino, _exit, TSDB_CODE_INVALID_PARA)
783,438✔
2065

2066
    switch (pDownParam->opType) {
783,438✔
2067
      case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE: {
783,438✔
2068
        pExecParam = (SExchangeOperatorParam*)((SOperatorParam*)(pOperator->pDownstreamGetParams[0]))->value;
783,438✔
2069
        if (!pExecParam->multiParams) {
783,438✔
2070
          pExecParam->basic.vgId = pExtW->orgTableVgId;
684,174✔
2071
          taosArrayClear(pExecParam->basic.uidList);
684,174✔
2072
          QUERY_CHECK_NULL(taosArrayPush(pExecParam->basic.uidList, &pExtW->orgTableUid), code, lino, _exit, terrno)
1,368,348✔
2073
        }
2074
        break;
783,438✔
2075
      }
2076
      default:
×
2077
        break;
×
2078
    }
2079

2080
    freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
783,438✔
2081
    pOperator->pOperatorGetParam = NULL;
783,438✔
2082
  } else {
2083
    TAOS_CHECK_EXIT(extWinInitWindowList(pExtW, pTaskInfo));
182,095✔
2084
  }
2085

2086
  while (1) {
4,272,232✔
2087
    pExtW->blkWinIdx = -1;
5,237,765✔
2088
    pExtW->blkWinStartSet = false;
5,237,765✔
2089
    pExtW->blkRowStartIdx = 0;
5,237,974✔
2090

2091
    SSDataBlock* pBlock = getNextBlockFromDownstreamRemain(pOperator, 0);
5,237,974✔
2092
    if (pOperator->pDownstreamGetParams) {
5,238,183✔
2093
      pOperator->pDownstreamGetParams[0] = NULL;
2,763,236✔
2094
    }
2095
    if (pBlock == NULL) {
5,238,183✔
2096
      if (EEXT_MODE_AGG == pExtW->mode) {
965,533✔
2097
        TAOS_CHECK_EXIT(extWinAggHandleEmptyWins(pOperator, pBlock, true, NULL));
965,256✔
2098
      }
2099
      pExtW->blkWinStartIdx = pExtW->pWins->size;
965,533✔
2100
      break;
965,533✔
2101
    }
2102

2103
    if (pExtW->isDynWindow) {
4,272,650✔
2104
      TSKEY skey = 0;
1,979,798✔
2105
      TSKEY ekey = 0;
1,979,798✔
2106
      code = getTimeWindowOfBlock(pBlock, pExtW->primaryTsIndex, &skey, &ekey);
1,979,798✔
2107
      QUERY_CHECK_CODE(code, lino, _exit);
1,979,798✔
2108
      pExtW->orgTableTimeRange.skey = TMIN(pExtW->orgTableTimeRange.skey, skey);
1,979,798✔
2109
      pExtW->orgTableTimeRange.ekey = TMAX(pExtW->orgTableTimeRange.ekey, ekey);
1,979,798✔
2110
    }
2111

2112
    printDataBlock(pBlock, __func__, pTaskInfo->id.str, pTaskInfo->id.queryId);
4,272,650✔
2113

2114
    qDebug("ext window mode:%d got %" PRId64 " rows from downstream", pExtW->mode, pBlock->info.rows);
4,272,650✔
2115
    
2116
    switch (pExtW->mode) {
4,269,868✔
2117
      case EEXT_MODE_SCALAR:
717✔
2118
        TAOS_CHECK_EXIT(extWinProjectOpen(pOperator, pBlock));
717✔
2119
        if (extWinNonAggGotResBlock(pExtW)) {
717✔
2120
          return code;
×
2121
        }
2122
        break;
717✔
2123
      case EEXT_MODE_AGG:
4,271,933✔
2124
        TAOS_CHECK_EXIT(extWinAggOpen(pOperator, pBlock));
4,271,933✔
2125
        break;
4,271,515✔
2126
      case EEXT_MODE_INDEFR_FUNC:
×
2127
        TAOS_CHECK_EXIT(extWinIndefRowsOpen(pOperator, pBlock));
×
2128
        if (extWinNonAggGotResBlock(pExtW)) {
×
2129
          return code;
×
2130
        }
2131
        break;
×
2132
      default:
×
2133
        break;
×
2134
    }
2135
  }
2136

2137
  if (pOperator->pOperatorGetParam) {
965,533✔
2138
    freeOperatorParam(pOperator->pOperatorGetParam, OP_GET_PARAM);
×
2139
    pOperator->pOperatorGetParam = NULL;
×
2140
  }
2141
  OPTR_SET_OPENED(pOperator);
965,533✔
2142

2143
#if 0
2144
  if (pExtW->mode == EEXT_MODE_AGG) {
2145
    qDebug("ext window before dump final rows num:%d", tSimpleHashGetSize(pExtW->aggSup.pResultRowHashTable));
2146

2147
    code = initGroupedResultInfo(&pExtW->groupResInfo, pExtW->aggSup.pResultRowHashTable, pExtW->binfo.inputTsOrder);
2148
    QUERY_CHECK_CODE(code, lino, _exit);
2149

2150
    qDebug("ext window after dump final rows num:%d", tSimpleHashGetSize(pExtW->aggSup.pResultRowHashTable));
2151
  }
2152
#endif
2153

2154
_exit:
965,533✔
2155

2156
  if (code != 0) {
965,533✔
2157
    qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
×
2158
    pTaskInfo->code = code;
×
2159
    T_LONG_JMP(pTaskInfo->env, code);
×
2160
  }
2161
  
2162
  return code;
965,533✔
2163
}
2164

2165
static int32_t extWinNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
1,148,198✔
2166
  int32_t                  code = 0;
1,148,198✔
2167
  int32_t                  lino = 0;
1,148,198✔
2168
  SExternalWindowOperator* pExtW = pOperator->info;
1,148,198✔
2169
  SExecTaskInfo*           pTaskInfo = pOperator->pTaskInfo;
1,148,198✔
2170

2171
  if (pOperator->status == OP_EXEC_DONE && !pOperator->pOperatorGetParam) {
1,148,198✔
2172
    *ppRes = NULL;
×
2173
    return code;
×
2174
  }
2175

2176
  if (pOperator->pOperatorGetParam) {
1,148,198✔
2177
    if (pOperator->status == OP_EXEC_DONE) {
783,438✔
2178
      pOperator->status = OP_NOT_OPENED;
27,090✔
2179
    }
2180
  }
2181

2182
  extWinRecycleBlkNode(pExtW, &pExtW->pLastBlkNode);
1,148,198✔
2183

2184
  if (pOperator->status == OP_NOT_OPENED) {
1,148,198✔
2185
    TAOS_CHECK_EXIT(pOperator->fpSet._openFn(pOperator));
965,533✔
2186
  }
2187

2188
  if (pExtW->mode == EEXT_MODE_SCALAR || pExtW->mode == EEXT_MODE_INDEFR_FUNC) {
1,148,198✔
2189
    TAOS_CHECK_EXIT(extWinNonAggOutputRes(pOperator, ppRes));
497✔
2190
    if (NULL == *ppRes) {
497✔
2191
      setOperatorCompleted(pOperator);
×
2192
      extWinFreeResultRow(pExtW);
×
2193
    }
2194
  } else {
2195
#if 0    
2196
    doBuildResultDatablock(pOperator, &pExtW->binfo, &pExtW->groupResInfo, pExtW->aggSup.pResultBuf);
2197
    bool hasRemain = hasRemainResults(&pExtW->groupResInfo);
2198
    if (!hasRemain) {
2199
      setOperatorCompleted(pOperator);
2200
      break;
2201
    }
2202
    if (pExtW->binfo.pRes->info.rows > 0) break;
2203
#else
2204
    TAOS_CHECK_EXIT(extWinAggOutputRes(pOperator, ppRes));
1,147,701✔
2205
    if (NULL == *ppRes) {
1,147,701✔
2206
      setOperatorCompleted(pOperator);
181,818✔
2207
      if (pTaskInfo->pStreamRuntimeInfo) {
181,818✔
2208
        extWinFreeResultRow(pExtW);
181,818✔
2209
      }
2210
    }
2211
#endif      
2212
  }
2213

2214
  if (*ppRes) {
1,148,198✔
2215
    pOperator->resultInfo.totalRows += (*ppRes)->info.rows;
966,380✔
2216
    printDataBlock(*ppRes, __func__, GET_TASKID(pTaskInfo), pTaskInfo->id.queryId);
966,380✔
2217
  }
2218
  
2219
_exit:
181,818✔
2220

2221
  if (code) {
1,148,198✔
2222
    qError("%s %s failed at line %d since %s", GET_TASKID(pTaskInfo), __func__, lino, tstrerror(code));
×
2223
    pTaskInfo->code = code;
×
2224
    T_LONG_JMP(pTaskInfo->env, code);
×
2225
  }
2226

2227
  if ((*ppRes) && (*ppRes)->info.rows <= 0) {
1,148,198✔
2228
    *ppRes = NULL;
×
2229
  }
2230

2231
  if (pTaskInfo->execModel == OPTR_EXEC_MODEL_STREAM && (*ppRes)) {
1,148,198✔
2232
    printDataBlock(*ppRes, getStreamOpName(pOperator->operatorType), GET_TASKID(pTaskInfo), pTaskInfo->id.queryId);
182,942✔
2233
  }
2234
  
2235
  return code;
1,148,198✔
2236
}
2237

2238

2239
int32_t createExternalWindowOperator(SOperatorInfo* pDownstream, SPhysiNode* pNode, SExecTaskInfo* pTaskInfo,
838,204✔
2240
                                     SOperatorInfo** pOptrOut) {
2241
  SExternalWindowPhysiNode* pPhynode = (SExternalWindowPhysiNode*)pNode;
838,204✔
2242
  QRY_PARAM_CHECK(pOptrOut);
838,204✔
2243
  int32_t                  code = 0;
838,204✔
2244
  int32_t                  lino = 0;
838,204✔
2245
  SExternalWindowOperator* pExtW = taosMemoryCalloc(1, sizeof(SExternalWindowOperator));
838,204✔
2246
  SOperatorInfo*           pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
838,204✔
2247
  pOperator->pPhyNode = pNode;
838,204✔
2248
  if (!pExtW || !pOperator) {
838,204✔
2249
    code = terrno;
×
2250
    lino = __LINE__;
×
2251
    goto _error;
×
2252
  }
2253
  
2254
  setOperatorInfo(pOperator, "ExternalWindowOperator", QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW, true, OP_NOT_OPENED,
838,204✔
2255
                  pExtW, pTaskInfo);
2256
                  
2257
  SSDataBlock* pResBlock = createDataBlockFromDescNode(pPhynode->window.node.pOutputDataBlockDesc);
838,204✔
2258
  QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
838,204✔
2259
  initBasicInfo(&pExtW->binfo, pResBlock);
838,204✔
2260

2261
  pExtW->primaryTsIndex = ((SColumnNode*)pPhynode->window.pTspk)->slotId;
838,204✔
2262
  pExtW->mode = pPhynode->window.pProjs ? EEXT_MODE_SCALAR : (pPhynode->window.indefRowsFunc ? EEXT_MODE_INDEFR_FUNC : EEXT_MODE_AGG);
838,204✔
2263
  pExtW->binfo.inputTsOrder = pPhynode->window.node.inputTsOrder = TSDB_ORDER_ASC;
838,204✔
2264
  pExtW->binfo.outputTsOrder = pExtW->binfo.inputTsOrder;
838,204✔
2265
  pExtW->isDynWindow = false;
837,984✔
2266

2267
  if (pTaskInfo->pStreamRuntimeInfo != NULL){
838,204✔
2268
    pTaskInfo->pStreamRuntimeInfo->funcInfo.withExternalWindow = true;
81,856✔
2269
  }
2270

2271
  // pExtW->limitInfo = (SLimitInfo){0};
2272
  // initLimitInfo(pPhynode->window.node.pLimit, pPhynode->window.node.pSlimit, &pExtW->limitInfo);
2273

2274
  if (pPhynode->window.pProjs) {
837,984✔
2275
    int32_t    numOfScalarExpr = 0;
277✔
2276
    SExprInfo* pScalarExprInfo = NULL;
277✔
2277
    code = createExprInfo(pPhynode->window.pProjs, NULL, &pScalarExprInfo, &numOfScalarExpr);
277✔
2278
    QUERY_CHECK_CODE(code, lino, _error);
277✔
2279

2280
    code = initExprSupp(&pExtW->scalarSupp, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
277✔
2281
    QUERY_CHECK_CODE(code, lino, _error);
277✔
2282

2283
  //if (pExtW->multiTableMode) {
2284
    pExtW->pOutputBlocks = taosArrayInit_s(POINTER_BYTES, STREAM_CALC_REQ_MAX_WIN_NUM);
277✔
2285
    if (!pExtW->pOutputBlocks) QUERY_CHECK_CODE(terrno, lino, _error);
277✔
2286
  //}
2287
    pExtW->pFreeBlocks = tdListNew(POINTER_BYTES * 2);
277✔
2288
    QUERY_CHECK_NULL(pExtW->pFreeBlocks, code, lino, _error, terrno);
277✔
2289
  } else if (pExtW->mode == EEXT_MODE_AGG) {
837,707✔
2290
    if (pPhynode->window.pExprs != NULL) {
837,927✔
2291
      int32_t    num = 0;
1,881✔
2292
      SExprInfo* pSExpr = NULL;
1,881✔
2293
      code = createExprInfo(pPhynode->window.pExprs, NULL, &pSExpr, &num);
1,881✔
2294
      QUERY_CHECK_CODE(code, lino, _error);
1,881✔
2295
    
2296
      code = initExprSupp(&pExtW->scalarSupp, pSExpr, num, &pTaskInfo->storageAPI.functionStore);
1,881✔
2297
      if (code != TSDB_CODE_SUCCESS) {
1,881✔
2298
        goto _error;
×
2299
      }
2300
      checkIndefRowsFuncs(&pExtW->scalarSupp);
1,881✔
2301
    }
2302
    
2303
    size_t keyBufSize = sizeof(int64_t) * 2 + POINTER_BYTES;
837,707✔
2304
    initResultSizeInfo(&pOperator->resultInfo, 4096);
837,707✔
2305
    //code = blockDataEnsureCapacity(pExtW->binfo.pRes, pOperator->resultInfo.capacity);
2306
    //QUERY_CHECK_CODE(code, lino, _error);
2307

2308
    pExtW->pWinRowIdx = taosArrayInit(4096, sizeof(int64_t));
837,927✔
2309
    TSDB_CHECK_NULL(pExtW->pWinRowIdx, code, lino, _error, terrno);
837,927✔
2310
    
2311
    int32_t num = 0;
837,927✔
2312
    SExprInfo* pExprInfo = NULL;
837,927✔
2313
    code = createExprInfo(pPhynode->window.pFuncs, NULL, &pExprInfo, &num);
837,927✔
2314
    QUERY_CHECK_CODE(code, lino, _error);
837,927✔
2315
    pOperator->exprSupp.hasWindow = true;
837,927✔
2316
    pOperator->exprSupp.hasWindowOrGroup = true;
837,927✔
2317
    code = initAggSup(&pOperator->exprSupp, &pExtW->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str, 0, 0);
837,927✔
2318
    QUERY_CHECK_CODE(code, lino, _error);
837,927✔
2319

2320
    nodesWalkExprs(pPhynode->window.pFuncs, extWinHasCountLikeFunc, &pExtW->hasCountFunc);
837,927✔
2321
    if (pExtW->hasCountFunc) {
837,927✔
2322
      code = extWinCreateEmptyInputBlock(pOperator, &pExtW->pEmptyInputBlock);
436,099✔
2323
      QUERY_CHECK_CODE(code, lino, _error);
436,099✔
2324
      qDebug("%s ext window has CountLikeFunc", pOperator->pTaskInfo->id.str);
436,099✔
2325
    } else {
2326
      qDebug("%s ext window doesn't have CountLikeFunc", pOperator->pTaskInfo->id.str);
401,828✔
2327
    }
2328

2329
    code = initExecTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pTaskInfo->window);
837,927✔
2330
    QUERY_CHECK_CODE(code, lino, _error);
837,927✔
2331

2332
    pExtW->lastSKey = INT64_MIN;
837,927✔
2333
  } else {
2334
    size_t  keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
×
2335
    
2336
    if (pPhynode->window.pExprs != NULL) {
×
2337
      int32_t    num = 0;
×
2338
      SExprInfo* pSExpr = NULL;
×
2339
      code = createExprInfo(pPhynode->window.pExprs, NULL, &pSExpr, &num);
×
2340
      QUERY_CHECK_CODE(code, lino, _error);
×
2341
    
2342
      code = initExprSupp(&pExtW->scalarSupp, pSExpr, num, &pTaskInfo->storageAPI.functionStore);
×
2343
      if (code != TSDB_CODE_SUCCESS) {
×
2344
        goto _error;
×
2345
      }
2346
    }
2347
    
2348
    int32_t    numOfExpr = 0;
×
2349
    SExprInfo* pExprInfo = NULL;
×
2350
    code = createExprInfo(pPhynode->window.pFuncs, NULL, &pExprInfo, &numOfExpr);
×
2351
    TSDB_CHECK_CODE(code, lino, _error);
×
2352
    
2353
    code = initAggSup(&pOperator->exprSupp, &pExtW->aggSup, pExprInfo, numOfExpr, keyBufSize, pTaskInfo->id.str,
×
2354
                              pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
×
2355
    TSDB_CHECK_CODE(code, lino, _error);
×
2356
    pOperator->exprSupp.hasWindowOrGroup = false;
×
2357
    
2358
    //code = setFunctionResultOutput(pOperator, &pExtW->binfo, &pExtW->aggSup, MAIN_SCAN, numOfExpr);
2359
    //TSDB_CHECK_CODE(code, lino, _error);
2360
    
2361
    code = filterInitFromNode((SNode*)pNode->pConditions, &pOperator->exprSupp.pFilterInfo, 0,
×
2362
                              pTaskInfo->pStreamRuntimeInfo);
×
2363
    TSDB_CHECK_CODE(code, lino, _error);
×
2364
    
2365
    pExtW->binfo.inputTsOrder = pNode->inputTsOrder;
×
2366
    pExtW->binfo.outputTsOrder = pNode->outputTsOrder;
×
2367
    code = setRowTsColumnOutputInfo(pOperator->exprSupp.pCtx, numOfExpr, &pExtW->pPseudoColInfo);
×
2368
    TSDB_CHECK_CODE(code, lino, _error);
×
2369

2370
  //if (pExtW->multiTableMode) {
2371
    pExtW->pOutputBlocks = taosArrayInit_s(POINTER_BYTES, STREAM_CALC_REQ_MAX_WIN_NUM);
×
2372
    if (!pExtW->pOutputBlocks) QUERY_CHECK_CODE(terrno, lino, _error);
×
2373
  //}
2374
    pExtW->pFreeBlocks = tdListNew(POINTER_BYTES * 2);
×
2375
    QUERY_CHECK_NULL(pExtW->pFreeBlocks, code, lino, _error, terrno);  
×
2376
  }
2377

2378
  pExtW->pWins = taosArrayInit(4096, sizeof(SExtWinTimeWindow));
838,204✔
2379
  if (!pExtW->pWins) QUERY_CHECK_CODE(terrno, lino, _error);
838,204✔
2380
  
2381
  //initResultRowInfo(&pExtW->binfo.resultRowInfo);
2382

2383
  pExtW->timeRangeExpr = (STimeRangeNode*)pPhynode->pTimeRange;
838,204✔
2384
  if (pExtW->timeRangeExpr) {
838,204✔
2385
    QUERY_CHECK_NULL(pExtW->timeRangeExpr->pStart, code, lino, _error, TSDB_CODE_STREAM_INTERNAL_ERROR);
81,856✔
2386
    QUERY_CHECK_NULL(pExtW->timeRangeExpr->pEnd, code, lino, _error, TSDB_CODE_STREAM_INTERNAL_ERROR);
81,856✔
2387
  }
2388

2389
  if (pPhynode->isSingleTable) {
838,204✔
2390
    pExtW->getWinFp = (pExtW->timeRangeExpr && (pExtW->timeRangeExpr->needCalc || (pTaskInfo->pStreamRuntimeInfo->funcInfo.addOptions & CALC_SLIDING_OVERLAP))) ? extWinGetOvlpWin : extWinGetNoOvlpWin;
709,562✔
2391
    pExtW->multiTableMode = false;
709,562✔
2392
  } else {
2393
    pExtW->getWinFp = (pExtW->timeRangeExpr && (pExtW->timeRangeExpr->needCalc || (pTaskInfo->pStreamRuntimeInfo->funcInfo.addOptions & CALC_SLIDING_OVERLAP))) ? extWinGetMultiTbOvlpWin : extWinGetMultiTbNoOvlpWin;
128,642✔
2394
    pExtW->multiTableMode = true;
128,642✔
2395
  }
2396
  pExtW->inputHasOrder = pPhynode->inputHasOrder;
838,204✔
2397
  pExtW->orgTableUid = pPhynode->orgTableUid;
838,204✔
2398
  pExtW->orgTableVgId = pPhynode->orgTableVgId;
838,204✔
2399

2400
  pOperator->fpSet = createOperatorFpSet(extWinOpen, extWinNext, NULL, destroyExternalWindowOperatorInfo,
838,204✔
2401
                                         optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
2402
  setOperatorResetStateFn(pOperator, resetExternalWindowOperator);
838,204✔
2403
  code = appendDownstream(pOperator, &pDownstream, 1);
838,204✔
2404
  if (code != 0) {
838,204✔
2405
    goto _error;
×
2406
  }
2407

2408
  *pOptrOut = pOperator;
838,204✔
2409
  return code;
838,204✔
2410

2411
_error:
×
2412

2413
  if (pExtW != NULL) {
×
2414
    destroyExternalWindowOperatorInfo(pExtW);
×
2415
  }
2416

2417
  destroyOperatorAndDownstreams(pOperator, &pDownstream, 1);
×
2418
  pTaskInfo->code = code;
×
2419
  qError("error happens at %s %d, code:%s", __func__, lino, tstrerror(code));
×
2420
  return code;
×
2421
}
2422

2423

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