2025-04-29 07:31:17 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can use, redistribute, and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3
|
|
|
|
|
* or later ("AGPL"), as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "executorInt.h"
|
|
|
|
|
#include "operator.h"
|
|
|
|
|
#include "querytask.h"
|
|
|
|
|
#include "tdatablock.h"
|
2025-07-09 10:57:01 +00:00
|
|
|
#include "stream.h"
|
2025-07-19 09:49:48 +00:00
|
|
|
#include "filter.h"
|
2025-04-29 07:31:17 +00:00
|
|
|
|
|
|
|
|
typedef struct SBlockList {
|
|
|
|
|
const SSDataBlock* pSrcBlock;
|
|
|
|
|
SList* pBlocks;
|
|
|
|
|
int32_t blockRowNumThreshold;
|
|
|
|
|
} SBlockList;
|
|
|
|
|
|
2025-07-04 03:17:39 +00:00
|
|
|
typedef struct SExternalWindowOperator {
|
|
|
|
|
SOptrBasicInfo binfo;
|
|
|
|
|
SAggSupporter aggSup;
|
|
|
|
|
SExprSupp scalarSupp;
|
|
|
|
|
STimeWindowAggSupp twAggSup;
|
|
|
|
|
SGroupResInfo groupResInfo;
|
|
|
|
|
int32_t primaryTsIndex;
|
2025-07-19 09:49:48 +00:00
|
|
|
EExtWinMode mode;
|
2025-07-04 03:17:39 +00:00
|
|
|
SArray* pWins;
|
|
|
|
|
SArray* pOutputBlocks; // for each window, we have a list of blocks
|
2025-07-04 08:00:38 +00:00
|
|
|
// SArray* pOffsetList; // for each window
|
2025-07-22 08:38:37 +00:00
|
|
|
int32_t lastWinId;
|
2025-07-04 03:17:39 +00:00
|
|
|
int32_t outputWinId;
|
|
|
|
|
SListNode* pOutputBlockListNode; // block index in block array used for output
|
|
|
|
|
SSDataBlock* pTmpBlock;
|
2025-07-22 08:38:37 +00:00
|
|
|
SSDataBlock* pEmptyInputBlock;
|
2025-07-19 09:49:48 +00:00
|
|
|
SArray* pPseudoColInfo;
|
2025-07-22 08:38:37 +00:00
|
|
|
bool hasCountFunc;
|
2025-07-04 08:00:38 +00:00
|
|
|
// SLimitInfo limitInfo; // limit info for each window
|
2025-07-04 03:17:39 +00:00
|
|
|
} SExternalWindowOperator;
|
|
|
|
|
|
2025-05-10 07:41:44 +00:00
|
|
|
static int32_t blockListInit(SBlockList* pBlockList, int32_t threshold) {
|
2025-04-29 07:31:17 +00:00
|
|
|
pBlockList->pBlocks = tdListNew(sizeof(SSDataBlock*));
|
|
|
|
|
if (!pBlockList->pBlocks) {
|
|
|
|
|
return terrno;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t blockListAddBlock(SBlockList* pBlockList) {
|
|
|
|
|
SSDataBlock* pRes = NULL;
|
|
|
|
|
int32_t code = createOneDataBlock(pBlockList->pSrcBlock, false, &pRes);
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
code = blockDataEnsureCapacity(pRes, pBlockList->blockRowNumThreshold);
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
blockDataDestroy(pRes);
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
code = tdListAppend(pBlockList->pBlocks, &pRes);
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
blockDataDestroy(pRes);
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t blockListGetLastBlock(SBlockList* pBlockList, SSDataBlock** ppBlock) {
|
|
|
|
|
SListNode* pNode = TD_DLIST_TAIL(pBlockList->pBlocks);
|
|
|
|
|
int32_t code = 0;
|
|
|
|
|
*ppBlock = NULL;
|
2025-05-21 00:50:15 +00:00
|
|
|
code = blockListAddBlock(pBlockList);
|
|
|
|
|
if (0 == code) {
|
2025-05-10 08:23:34 +00:00
|
|
|
pNode = TD_DLIST_TAIL(pBlockList->pBlocks);
|
|
|
|
|
*ppBlock = *(SSDataBlock**)pNode->data;
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void blockListDestroy(void* p) {
|
|
|
|
|
SBlockList* pBlockList = (SBlockList*)p;
|
2025-05-10 08:23:34 +00:00
|
|
|
if (TD_DLIST_NELES(pBlockList->pBlocks) > 0) {
|
|
|
|
|
SListNode* pNode = TD_DLIST_HEAD(pBlockList->pBlocks);
|
|
|
|
|
while (pNode) {
|
|
|
|
|
SSDataBlock* pBlock = *(SSDataBlock**)pNode->data;
|
|
|
|
|
blockDataDestroy(pBlock);
|
|
|
|
|
pNode = pNode->dl_next_;
|
|
|
|
|
}
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
2025-07-05 02:17:22 +00:00
|
|
|
taosMemoryFree(pBlockList->pBlocks);
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-04 03:17:39 +00:00
|
|
|
void destroyExternalWindowOperatorInfo(void* param) {
|
|
|
|
|
if (NULL == param) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
SExternalWindowOperator* pInfo = (SExternalWindowOperator*)param;
|
|
|
|
|
cleanupBasicInfo(&pInfo->binfo);
|
2025-04-29 07:31:17 +00:00
|
|
|
|
2025-07-04 03:17:39 +00:00
|
|
|
taosArrayDestroyEx(pInfo->pOutputBlocks, blockListDestroy);
|
|
|
|
|
taosArrayDestroy(pInfo->pWins);
|
|
|
|
|
colDataDestroy(&pInfo->twAggSup.timeWindowData);
|
|
|
|
|
cleanupGroupResInfo(&pInfo->groupResInfo);
|
|
|
|
|
|
2025-07-19 09:49:48 +00:00
|
|
|
taosArrayDestroy(pInfo->pPseudoColInfo);
|
2025-07-04 03:17:39 +00:00
|
|
|
blockDataDestroy(pInfo->pTmpBlock);
|
2025-07-22 08:38:37 +00:00
|
|
|
blockDataDestroy(pInfo->pEmptyInputBlock);
|
2025-07-04 03:17:39 +00:00
|
|
|
|
|
|
|
|
cleanupAggSup(&pInfo->aggSup);
|
|
|
|
|
cleanupExprSupp(&pInfo->scalarSupp);
|
|
|
|
|
|
|
|
|
|
pInfo->binfo.resultRowInfo.openWindow = tdListFree(pInfo->binfo.resultRowInfo.openWindow);
|
|
|
|
|
|
|
|
|
|
taosMemoryFreeClear(pInfo);
|
|
|
|
|
}
|
2025-04-29 07:31:17 +00:00
|
|
|
|
|
|
|
|
static int32_t doOpenExternalWindow(SOperatorInfo* pOperator);
|
|
|
|
|
static int32_t externalWindowNext(SOperatorInfo* pOperator, SSDataBlock** ppRes);
|
|
|
|
|
|
|
|
|
|
typedef struct SMergeAlignedExternalWindowOperator {
|
|
|
|
|
SExternalWindowOperator* pExtW;
|
|
|
|
|
int64_t curTs;
|
|
|
|
|
SSDataBlock* pPrefetchedBlock;
|
|
|
|
|
SResultRow* pResultRow;
|
|
|
|
|
} SMergeAlignedExternalWindowOperator;
|
|
|
|
|
|
|
|
|
|
void destroyMergeAlignedExternalWindowOperator(void* pOperator) {
|
|
|
|
|
SMergeAlignedExternalWindowOperator* pMlExtInfo = (SMergeAlignedExternalWindowOperator*)pOperator;
|
2025-07-04 03:17:39 +00:00
|
|
|
destroyExternalWindowOperatorInfo(pMlExtInfo->pExtW);
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void doMergeAlignExternalWindow(SOperatorInfo* pOperator);
|
|
|
|
|
|
|
|
|
|
static int32_t mergeAlignedExternalWindowNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
|
|
|
|
|
SExternalWindowOperator* pExtW = pMlExtInfo->pExtW;
|
|
|
|
|
int32_t code = 0;
|
2025-05-20 03:12:55 +00:00
|
|
|
int32_t lino = 0;
|
2025-04-29 07:31:17 +00:00
|
|
|
|
|
|
|
|
if (pOperator->status == OP_EXEC_DONE) {
|
|
|
|
|
(*ppRes) = NULL;
|
|
|
|
|
return TSDB_CODE_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SSDataBlock* pRes = pExtW->binfo.pRes;
|
|
|
|
|
blockDataCleanup(pRes);
|
|
|
|
|
|
2025-05-20 03:12:55 +00:00
|
|
|
if (!pExtW->pWins) {
|
|
|
|
|
size_t size = taosArrayGetSize(pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamPesudoFuncVals);
|
|
|
|
|
pExtW->pWins = taosArrayInit(size, sizeof(STimeWindow));
|
|
|
|
|
if (!pExtW->pWins) QUERY_CHECK_CODE(terrno, lino, _end);
|
2025-07-04 07:38:01 +00:00
|
|
|
// pExtW->pOffsetList = taosArrayInit(size, sizeof(SLimitInfo));
|
|
|
|
|
// if (!pExtW->pOffsetList) QUERY_CHECK_CODE(terrno, lino, _end);
|
|
|
|
|
|
2025-05-20 03:12:55 +00:00
|
|
|
for (int32_t i = 0; i < size; ++i) {
|
|
|
|
|
SSTriggerCalcParam* pParam = taosArrayGet(pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamPesudoFuncVals, i);
|
|
|
|
|
STimeWindow win = {.skey = pParam->wstart, .ekey = pParam->wend};
|
2025-06-19 12:29:54 +00:00
|
|
|
if (pTaskInfo->pStreamRuntimeInfo->funcInfo.triggerType != 1){ // 1 meams STREAM_TRIGGER_SLIDING
|
|
|
|
|
win.ekey++;
|
|
|
|
|
}
|
2025-07-08 02:24:05 +00:00
|
|
|
TSDB_CHECK_NULL(taosArrayPush(pExtW->pWins, &win), code, lino, _end, terrno);
|
2025-05-20 03:12:55 +00:00
|
|
|
}
|
|
|
|
|
pExtW->outputWinId = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
doMergeAlignExternalWindow(pOperator);
|
|
|
|
|
size_t rows = pRes->info.rows;
|
|
|
|
|
pOperator->resultInfo.totalRows += rows;
|
|
|
|
|
(*ppRes) = (rows == 0) ? NULL : pRes;
|
2025-05-20 03:12:55 +00:00
|
|
|
|
|
|
|
|
_end:
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
|
|
|
|
|
pTaskInfo->code = code;
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
|
|
|
|
}
|
2025-04-29 07:31:17 +00:00
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t resetMergeAlignedExternalWindowOperator(SOperatorInfo* pOperator) {
|
|
|
|
|
SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
|
|
|
|
|
SExternalWindowOperator* pExtW = pMlExtInfo->pExtW;
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
SMergeAlignedIntervalPhysiNode * pPhynode = (SMergeAlignedIntervalPhysiNode*)pOperator->pPhyNode;
|
2025-06-18 09:39:59 +00:00
|
|
|
pOperator->status = OP_NOT_OPENED;
|
|
|
|
|
|
2025-07-11 05:55:15 +00:00
|
|
|
taosArrayDestroy(pExtW->pWins);
|
|
|
|
|
pExtW->pWins = NULL;
|
|
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
resetBasicOperatorState(&pExtW->binfo);
|
2025-07-12 03:08:37 +00:00
|
|
|
pMlExtInfo->pResultRow = NULL;
|
2025-05-20 03:12:55 +00:00
|
|
|
pMlExtInfo->curTs = INT64_MIN;
|
2025-04-29 07:31:17 +00:00
|
|
|
if (pMlExtInfo->pPrefetchedBlock) blockDataCleanup(pMlExtInfo->pPrefetchedBlock);
|
|
|
|
|
|
2025-05-26 06:52:02 +00:00
|
|
|
int32_t code = resetAggSup(&pOperator->exprSupp, &pExtW->aggSup, pTaskInfo, pPhynode->window.pFuncs, NULL,
|
2025-04-29 07:31:17 +00:00
|
|
|
sizeof(int64_t) * 2 + POINTER_BYTES, pTaskInfo->id.str, pTaskInfo->streamInfo.pState,
|
|
|
|
|
&pTaskInfo->storageAPI.functionStore);
|
|
|
|
|
if (code == 0) {
|
|
|
|
|
colDataDestroy(&pExtW->twAggSup.timeWindowData);
|
|
|
|
|
code = initExecTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pTaskInfo->window);
|
|
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-07 10:14:17 +00:00
|
|
|
int32_t createMergeAlignedExternalWindowOperator(SOperatorInfo* pDownstream, SPhysiNode* pNode,
|
2025-04-29 07:31:17 +00:00
|
|
|
SExecTaskInfo* pTaskInfo, SOperatorInfo** ppOptrOut) {
|
2025-05-07 10:14:17 +00:00
|
|
|
SMergeAlignedIntervalPhysiNode* pPhynode = (SMergeAlignedIntervalPhysiNode*)pNode;
|
2025-04-29 07:31:17 +00:00
|
|
|
int32_t code = 0;
|
|
|
|
|
int32_t lino = 0;
|
|
|
|
|
SMergeAlignedExternalWindowOperator* pMlExtInfo = taosMemoryCalloc(1, sizeof(SMergeAlignedExternalWindowOperator));
|
|
|
|
|
SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
|
|
|
|
|
|
2025-06-28 03:47:31 +00:00
|
|
|
if (pTaskInfo->pStreamRuntimeInfo != NULL){
|
|
|
|
|
pTaskInfo->pStreamRuntimeInfo->funcInfo.withExternalWindow = true;
|
|
|
|
|
}
|
2025-05-14 09:55:32 +00:00
|
|
|
pOperator->pPhyNode = pNode;
|
2025-04-29 07:31:17 +00:00
|
|
|
if (!pMlExtInfo || !pOperator) {
|
|
|
|
|
code = terrno;
|
|
|
|
|
goto _error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pMlExtInfo->pExtW = taosMemoryCalloc(1, sizeof(SExternalWindowOperator));
|
|
|
|
|
if (!pMlExtInfo->pExtW) {
|
|
|
|
|
code = terrno;
|
|
|
|
|
goto _error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SExternalWindowOperator* pExtW = pMlExtInfo->pExtW;
|
|
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
|
|
|
|
pSup->hasWindowOrGroup = true;
|
2025-05-20 03:12:55 +00:00
|
|
|
pMlExtInfo->curTs = INT64_MIN;
|
2025-04-29 07:31:17 +00:00
|
|
|
|
2025-07-02 06:03:00 +00:00
|
|
|
pExtW->primaryTsIndex = ((SColumnNode*)pPhynode->window.pTspk)->slotId;
|
2025-07-19 09:49:48 +00:00
|
|
|
pExtW->mode = pPhynode->window.pProjs ? EEXT_MODE_SCALAR : EEXT_MODE_AGG;
|
2025-07-02 06:03:00 +00:00
|
|
|
pExtW->binfo.inputTsOrder = pPhynode->window.node.inputTsOrder = TSDB_ORDER_ASC;
|
|
|
|
|
pExtW->binfo.outputTsOrder = pExtW->binfo.inputTsOrder;
|
2025-04-29 07:31:17 +00:00
|
|
|
|
2025-07-04 07:38:01 +00:00
|
|
|
// pExtW->limitInfo = (SLimitInfo){0};
|
|
|
|
|
// initLimitInfo(pPhynode->window.node.pLimit, pPhynode->window.node.pSlimit, &pExtW->limitInfo);
|
|
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
|
|
|
|
|
initResultSizeInfo(&pOperator->resultInfo, 512);
|
|
|
|
|
|
|
|
|
|
int32_t num = 0;
|
|
|
|
|
SExprInfo* pExprInfo = NULL;
|
|
|
|
|
code = createExprInfo(pPhynode->window.pFuncs, NULL, &pExprInfo, &num);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _error);
|
|
|
|
|
|
2025-07-19 09:49:48 +00:00
|
|
|
if (pExtW->mode == EEXT_MODE_AGG) {
|
2025-04-29 07:31:17 +00:00
|
|
|
code = initAggSup(pSup, &pExtW->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str, pTaskInfo->streamInfo.pState,
|
|
|
|
|
&pTaskInfo->storageAPI.functionStore);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SSDataBlock* pResBlock = createDataBlockFromDescNode(pPhynode->window.node.pOutputDataBlockDesc);
|
|
|
|
|
QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
|
|
|
|
|
initBasicInfo(&pExtW->binfo, pResBlock);
|
|
|
|
|
|
|
|
|
|
initResultRowInfo(&pExtW->binfo.resultRowInfo);
|
|
|
|
|
code = blockDataEnsureCapacity(pExtW->binfo.pRes, pOperator->resultInfo.capacity);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _error);
|
|
|
|
|
setOperatorInfo(pOperator, "MergeAlignedExternalWindowOperator", QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW, false, OP_NOT_OPENED, pMlExtInfo, pTaskInfo);
|
|
|
|
|
pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, mergeAlignedExternalWindowNext, NULL,
|
|
|
|
|
destroyMergeAlignedExternalWindowOperator, optrDefaultBufFn, NULL,
|
|
|
|
|
optrDefaultGetNextExtFn, NULL);
|
|
|
|
|
setOperatorResetStateFn(pOperator, resetMergeAlignedExternalWindowOperator);
|
|
|
|
|
|
|
|
|
|
code = appendDownstream(pOperator, &pDownstream, 1);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _error);
|
|
|
|
|
*ppOptrOut = pOperator;
|
|
|
|
|
return code;
|
|
|
|
|
_error:
|
|
|
|
|
if (pMlExtInfo) destroyMergeAlignedExternalWindowOperator(pMlExtInfo);
|
|
|
|
|
destroyOperatorAndDownstreams(pOperator, &pDownstream, 1);
|
|
|
|
|
pTaskInfo->code = code;
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t resetExternalWindowOperator(SOperatorInfo* pOperator) {
|
|
|
|
|
SExternalWindowOperator* pExtW = pOperator->info;
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
SExternalWindowPhysiNode* pPhynode = (SExternalWindowPhysiNode*)pOperator->pPhyNode;
|
2025-06-18 09:39:59 +00:00
|
|
|
pOperator->status = OP_NOT_OPENED;
|
|
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
resetBasicOperatorState(&pExtW->binfo);
|
|
|
|
|
pExtW->outputWinId = 0;
|
2025-07-22 08:38:37 +00:00
|
|
|
pExtW->lastWinId = -1;
|
2025-04-29 07:31:17 +00:00
|
|
|
taosArrayDestroyEx(pExtW->pOutputBlocks, blockListDestroy);
|
2025-07-04 07:38:01 +00:00
|
|
|
// taosArrayDestroy(pExtW->pOffsetList);
|
2025-05-09 10:05:02 +00:00
|
|
|
taosArrayDestroy(pExtW->pWins);
|
|
|
|
|
pExtW->pWins = NULL;
|
2025-04-29 07:31:17 +00:00
|
|
|
pExtW->pOutputBlockListNode = NULL;
|
2025-05-10 08:23:34 +00:00
|
|
|
pExtW->pOutputBlocks = NULL;
|
2025-07-04 07:38:01 +00:00
|
|
|
// pExtW->pOffsetList = NULL;
|
2025-04-29 07:31:17 +00:00
|
|
|
initResultSizeInfo(&pOperator->resultInfo, 512);
|
|
|
|
|
int32_t code = blockDataEnsureCapacity(pExtW->binfo.pRes, pOperator->resultInfo.capacity);
|
|
|
|
|
if (code == 0) {
|
2025-05-26 06:52:02 +00:00
|
|
|
code = resetAggSup(&pOperator->exprSupp, &pExtW->aggSup, pTaskInfo, pPhynode->window.pFuncs, NULL,
|
2025-04-29 07:31:17 +00:00
|
|
|
sizeof(int64_t) * 2 + POINTER_BYTES, pTaskInfo->id.str, pTaskInfo->streamInfo.pState,
|
|
|
|
|
&pTaskInfo->storageAPI.functionStore);
|
|
|
|
|
}
|
|
|
|
|
if (code == 0) {
|
2025-05-26 06:52:02 +00:00
|
|
|
code = resetExprSupp(&pExtW->scalarSupp, pTaskInfo, pPhynode->window.pProjs, NULL,
|
2025-05-12 09:02:38 +00:00
|
|
|
&pTaskInfo->storageAPI.functionStore);
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
|
|
|
|
if (code == 0) {
|
|
|
|
|
colDataDestroy(&pExtW->twAggSup.timeWindowData);
|
|
|
|
|
code = initExecTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pTaskInfo->window);
|
|
|
|
|
}
|
|
|
|
|
if (code == 0) {
|
|
|
|
|
cleanupGroupResInfo(&pExtW->groupResInfo);
|
|
|
|
|
}
|
2025-05-10 08:23:34 +00:00
|
|
|
blockDataDestroy(pExtW->pTmpBlock);
|
|
|
|
|
pExtW->pTmpBlock = NULL;
|
2025-04-29 07:31:17 +00:00
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 08:38:37 +00:00
|
|
|
static EDealRes extWindowHasCountLikeFunc(SNode* pNode, void* res) {
|
|
|
|
|
if (QUERY_NODE_FUNCTION == nodeType(pNode)) {
|
|
|
|
|
SFunctionNode* pFunc = (SFunctionNode*)pNode;
|
|
|
|
|
if (fmIsCountLikeFunc(pFunc->funcId) || (pFunc->hasOriginalFunc && fmIsCountLikeFunc(pFunc->originalFuncId))) {
|
|
|
|
|
*(bool*)res = true;
|
|
|
|
|
return DEAL_RES_END;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return DEAL_RES_CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int32_t extWindowCreateEmptyInputBlock(SOperatorInfo* pOperator, SSDataBlock** ppBlock) {
|
|
|
|
|
int32_t code = TSDB_CODE_SUCCESS;
|
|
|
|
|
int32_t lino = 0;
|
|
|
|
|
SSDataBlock* pBlock = NULL;
|
|
|
|
|
if (!tsCountAlwaysReturnValue) {
|
|
|
|
|
return TSDB_CODE_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SExternalWindowOperator* pExtW = pOperator->info;
|
|
|
|
|
|
|
|
|
|
if (!pExtW->hasCountFunc) {
|
|
|
|
|
return TSDB_CODE_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
code = createDataBlock(&pBlock);
|
|
|
|
|
if (code) {
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pBlock->info.rows = 1;
|
|
|
|
|
pBlock->info.capacity = 0;
|
|
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
|
|
|
|
|
SColumnInfoData colInfo = {0};
|
|
|
|
|
colInfo.hasNull = true;
|
|
|
|
|
colInfo.info.type = TSDB_DATA_TYPE_NULL;
|
|
|
|
|
colInfo.info.bytes = 1;
|
|
|
|
|
|
|
|
|
|
SExprInfo* pOneExpr = &pOperator->exprSupp.pExprInfo[i];
|
|
|
|
|
for (int32_t j = 0; j < pOneExpr->base.numOfParams; ++j) {
|
|
|
|
|
SFunctParam* pFuncParam = &pOneExpr->base.pParam[j];
|
|
|
|
|
if (pFuncParam->type == FUNC_PARAM_TYPE_COLUMN) {
|
|
|
|
|
int32_t slotId = pFuncParam->pCol->slotId;
|
|
|
|
|
int32_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
|
|
|
|
|
if (slotId >= numOfCols) {
|
|
|
|
|
code = taosArrayEnsureCap(pBlock->pDataBlock, slotId + 1);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _end);
|
|
|
|
|
|
|
|
|
|
for (int32_t k = numOfCols; k < slotId + 1; ++k) {
|
|
|
|
|
void* tmp = taosArrayPush(pBlock->pDataBlock, &colInfo);
|
|
|
|
|
QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (pFuncParam->type == FUNC_PARAM_TYPE_VALUE) {
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
code = blockDataEnsureCapacity(pBlock, pBlock->info.rows);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _end);
|
|
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < blockDataGetNumOfCols(pBlock); ++i) {
|
|
|
|
|
SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
|
|
|
|
|
QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno);
|
|
|
|
|
colDataSetNULL(pColInfoData, 0);
|
|
|
|
|
}
|
|
|
|
|
*ppBlock = pBlock;
|
|
|
|
|
|
|
|
|
|
_end:
|
|
|
|
|
if (code != TSDB_CODE_SUCCESS) {
|
|
|
|
|
blockDataDestroy(pBlock);
|
|
|
|
|
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
|
|
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-07 10:14:17 +00:00
|
|
|
int32_t createExternalWindowOperator(SOperatorInfo* pDownstream, SPhysiNode* pNode, SExecTaskInfo* pTaskInfo,
|
2025-04-29 07:31:17 +00:00
|
|
|
SOperatorInfo** pOptrOut) {
|
2025-05-07 10:14:17 +00:00
|
|
|
SExternalWindowPhysiNode* pPhynode = (SExternalWindowPhysiNode*)pNode;
|
2025-04-29 07:31:17 +00:00
|
|
|
QRY_PARAM_CHECK(pOptrOut);
|
|
|
|
|
int32_t code = 0;
|
|
|
|
|
int32_t lino = 0;
|
|
|
|
|
SExternalWindowOperator* pExtW = taosMemoryCalloc(1, sizeof(SExternalWindowOperator));
|
|
|
|
|
SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
|
2025-05-09 10:05:02 +00:00
|
|
|
pOperator->pPhyNode = pNode;
|
2025-04-29 07:31:17 +00:00
|
|
|
if (!pExtW || !pOperator) {
|
|
|
|
|
code = terrno;
|
|
|
|
|
lino = __LINE__;
|
|
|
|
|
goto _error;
|
|
|
|
|
}
|
2025-07-19 09:49:48 +00:00
|
|
|
|
|
|
|
|
setOperatorInfo(pOperator, "ExternalWindowOperator", QUERY_NODE_PHYSICAL_PLAN_EXTERNAL_WINDOW, true, OP_NOT_OPENED,
|
|
|
|
|
pExtW, pTaskInfo);
|
|
|
|
|
|
2025-06-28 03:47:31 +00:00
|
|
|
if (pTaskInfo->pStreamRuntimeInfo != NULL){
|
|
|
|
|
pTaskInfo->pStreamRuntimeInfo->funcInfo.withExternalWindow = true;
|
|
|
|
|
}
|
2025-04-29 07:31:17 +00:00
|
|
|
SSDataBlock* pResBlock = createDataBlockFromDescNode(pPhynode->window.node.pOutputDataBlockDesc);
|
|
|
|
|
QUERY_CHECK_NULL(pResBlock, code, lino, _error, terrno);
|
|
|
|
|
initBasicInfo(&pExtW->binfo, pResBlock);
|
|
|
|
|
|
|
|
|
|
pExtW->primaryTsIndex = ((SColumnNode*)pPhynode->window.pTspk)->slotId;
|
2025-07-19 09:49:48 +00:00
|
|
|
pExtW->mode = pPhynode->window.pProjs ? EEXT_MODE_SCALAR : (pPhynode->window.indefRowsFunc ? EEXT_MODE_INDEFR_FUNC : EEXT_MODE_AGG);
|
2025-05-21 00:50:15 +00:00
|
|
|
pExtW->binfo.inputTsOrder = pPhynode->window.node.inputTsOrder = TSDB_ORDER_ASC;
|
2025-05-10 07:41:44 +00:00
|
|
|
pExtW->binfo.outputTsOrder = pExtW->binfo.inputTsOrder;
|
2025-04-29 07:31:17 +00:00
|
|
|
|
2025-07-04 07:38:01 +00:00
|
|
|
// pExtW->limitInfo = (SLimitInfo){0};
|
|
|
|
|
// initLimitInfo(pPhynode->window.node.pLimit, pPhynode->window.node.pSlimit, &pExtW->limitInfo);
|
|
|
|
|
|
2025-07-19 09:49:48 +00:00
|
|
|
if (pPhynode->window.pProjs) {
|
|
|
|
|
int32_t numOfScalarExpr = 0;
|
|
|
|
|
SExprInfo* pScalarExprInfo = NULL;
|
|
|
|
|
code = createExprInfo(pPhynode->window.pProjs, NULL, &pScalarExprInfo, &numOfScalarExpr);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _error);
|
|
|
|
|
|
|
|
|
|
code = initExprSupp(&pExtW->scalarSupp, pScalarExprInfo, numOfScalarExpr, &pTaskInfo->storageAPI.functionStore);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _error);
|
|
|
|
|
} else if (pExtW->mode == EEXT_MODE_AGG) {
|
2025-04-29 07:31:17 +00:00
|
|
|
size_t keyBufSize = sizeof(int64_t) * 2 + POINTER_BYTES;
|
|
|
|
|
initResultSizeInfo(&pOperator->resultInfo, 512);
|
|
|
|
|
code = blockDataEnsureCapacity(pExtW->binfo.pRes, pOperator->resultInfo.capacity);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _error);
|
|
|
|
|
|
|
|
|
|
int32_t num = 0;
|
|
|
|
|
SExprInfo* pExprInfo = NULL;
|
|
|
|
|
code = createExprInfo(pPhynode->window.pFuncs, NULL, &pExprInfo, &num);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _error);
|
|
|
|
|
code = initAggSup(&pOperator->exprSupp, &pExtW->aggSup, pExprInfo, num, keyBufSize, pTaskInfo->id.str, 0, 0);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _error);
|
|
|
|
|
|
2025-07-22 08:38:37 +00:00
|
|
|
nodesWalkExprs(pPhynode->window.pFuncs, extWindowHasCountLikeFunc, &pExtW->hasCountFunc);
|
|
|
|
|
if (pExtW->hasCountFunc) {
|
|
|
|
|
code = extWindowCreateEmptyInputBlock(pOperator, &pExtW->pEmptyInputBlock);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _error);
|
|
|
|
|
qDebug("%s ext window has CountLikeFunc", pOperator->pTaskInfo->id.str);
|
|
|
|
|
} else {
|
|
|
|
|
qDebug("%s ext window doesn't have CountLikeFunc", pOperator->pTaskInfo->id.str);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 09:49:48 +00:00
|
|
|
code = initExecTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &pTaskInfo->window);
|
2025-04-29 07:31:17 +00:00
|
|
|
QUERY_CHECK_CODE(code, lino, _error);
|
2025-07-19 09:49:48 +00:00
|
|
|
} else {
|
|
|
|
|
size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
|
|
|
|
|
|
|
|
|
|
if (pPhynode->window.pExprs != NULL) {
|
|
|
|
|
int32_t num = 0;
|
|
|
|
|
SExprInfo* pSExpr = NULL;
|
|
|
|
|
code = createExprInfo(pPhynode->window.pExprs, NULL, &pSExpr, &num);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _error);
|
|
|
|
|
|
|
|
|
|
code = initExprSupp(&pExtW->scalarSupp, pSExpr, num, &pTaskInfo->storageAPI.functionStore);
|
|
|
|
|
if (code != TSDB_CODE_SUCCESS) {
|
|
|
|
|
goto _error;
|
|
|
|
|
}
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
2025-07-19 09:49:48 +00:00
|
|
|
|
|
|
|
|
initResultSizeInfo(&pOperator->resultInfo, 512);
|
|
|
|
|
code = blockDataEnsureCapacity(pResBlock, 512);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
|
|
|
|
|
|
|
|
|
int32_t numOfExpr = 0;
|
|
|
|
|
SExprInfo* pExprInfo = NULL;
|
|
|
|
|
code = createExprInfo(pPhynode->window.pFuncs, NULL, &pExprInfo, &numOfExpr);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
|
|
|
|
|
|
|
|
|
code = initAggSup(&pOperator->exprSupp, &pExtW->aggSup, pExprInfo, numOfExpr, keyBufSize, pTaskInfo->id.str,
|
|
|
|
|
pTaskInfo->streamInfo.pState, &pTaskInfo->storageAPI.functionStore);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
|
|
|
|
pOperator->exprSupp.hasWindowOrGroup = false;
|
|
|
|
|
|
|
|
|
|
code = setFunctionResultOutput(pOperator, &pExtW->binfo, &pExtW->aggSup, MAIN_SCAN, numOfExpr);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
|
|
|
|
|
|
|
|
|
code = filterInitFromNode((SNode*)pNode->pConditions, &pOperator->exprSupp.pFilterInfo, 0,
|
|
|
|
|
pTaskInfo->pStreamRuntimeInfo);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
|
|
|
|
|
|
|
|
|
pExtW->binfo.pRes = pResBlock;
|
|
|
|
|
pExtW->binfo.inputTsOrder = pNode->inputTsOrder;
|
|
|
|
|
pExtW->binfo.outputTsOrder = pNode->outputTsOrder;
|
|
|
|
|
code = setRowTsColumnOutputInfo(pOperator->exprSupp.pCtx, numOfExpr, &pExtW->pPseudoColInfo);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initResultRowInfo(&pExtW->binfo.resultRowInfo);
|
2025-07-19 09:49:48 +00:00
|
|
|
|
2025-07-04 03:17:39 +00:00
|
|
|
pOperator->fpSet = createOperatorFpSet(doOpenExternalWindow, externalWindowNext, NULL, destroyExternalWindowOperatorInfo,
|
2025-04-29 07:31:17 +00:00
|
|
|
optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
|
|
|
|
|
setOperatorResetStateFn(pOperator, resetExternalWindowOperator);
|
|
|
|
|
code = appendDownstream(pOperator, &pDownstream, 1);
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
goto _error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*pOptrOut = pOperator;
|
|
|
|
|
return code;
|
2025-07-04 03:17:39 +00:00
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
_error:
|
2025-07-04 03:17:39 +00:00
|
|
|
|
|
|
|
|
if (pExtW != NULL) {
|
|
|
|
|
destroyExternalWindowOperatorInfo(pExtW);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
destroyOperatorAndDownstreams(pOperator, &pDownstream, 1);
|
|
|
|
|
pTaskInfo->code = code;
|
|
|
|
|
qError("error happens at %s %d, code:%s", __func__, lino, tstrerror(code));
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int64_t* extractTsCol(SSDataBlock* pBlock, int32_t primaryTsIndex, SExecTaskInfo* pTaskInfo) {
|
|
|
|
|
TSKEY* tsCols = NULL;
|
|
|
|
|
|
|
|
|
|
if (pBlock->pDataBlock != NULL && pBlock->info.dataLoad) {
|
|
|
|
|
SColumnInfoData* pColDataInfo = taosArrayGet(pBlock->pDataBlock, primaryTsIndex);
|
|
|
|
|
if (!pColDataInfo) {
|
|
|
|
|
pTaskInfo->code = terrno;
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, terrno);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tsCols = (int64_t*)pColDataInfo->pData;
|
|
|
|
|
if (tsCols[0] == 0) {
|
|
|
|
|
qWarn("%s at line %d.block start ts:%" PRId64 ",end ts:%" PRId64, __func__, __LINE__, tsCols[0],
|
|
|
|
|
tsCols[pBlock->info.rows - 1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tsCols[0] != 0 && (pBlock->info.window.skey == 0 && pBlock->info.window.ekey == 0)) {
|
|
|
|
|
int32_t code = blockDataUpdateTsWindow(pBlock, primaryTsIndex);
|
|
|
|
|
if (code != TSDB_CODE_SUCCESS) {
|
|
|
|
|
qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
|
|
|
|
|
pTaskInfo->code = code;
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tsCols;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-20 03:12:55 +00:00
|
|
|
static int32_t getExtWinCurIdx(SExecTaskInfo* pTaskInfo) {
|
2025-05-09 04:56:08 +00:00
|
|
|
return pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void incExtWinCurIdx(SOperatorInfo* pOperator) {
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx++;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-14 11:15:21 +00:00
|
|
|
static void setExtWinCurIdx(SOperatorInfo* pOperator, int32_t idx) {
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx = idx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-05-20 03:12:55 +00:00
|
|
|
static void incExtWinOutIdx(SOperatorInfo* pOperator) {
|
2025-05-16 01:41:31 +00:00
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
pTaskInfo->pStreamRuntimeInfo->funcInfo.curOutIdx++;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-14 11:15:21 +00:00
|
|
|
static const STimeWindow* getExtWindow(SOperatorInfo* pOperator, SExternalWindowOperator* pExtW, TSKEY ts) {
|
2025-05-21 00:50:15 +00:00
|
|
|
// TODO handle desc order
|
2025-04-29 07:31:17 +00:00
|
|
|
for (int32_t i = 0; i < pExtW->pWins->size; ++i) {
|
|
|
|
|
const STimeWindow* pWin = taosArrayGet(pExtW->pWins, i);
|
|
|
|
|
if (ts >= pWin->skey && ts < pWin->ekey) {
|
2025-07-14 11:15:21 +00:00
|
|
|
setExtWinCurIdx(pOperator, i);
|
2025-04-29 07:31:17 +00:00
|
|
|
return pWin;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-20 03:12:55 +00:00
|
|
|
static const STimeWindow* getExtNextWindow(SExternalWindowOperator* pExtW, SExecTaskInfo* pTaskInfo) {
|
|
|
|
|
int32_t curIdx = getExtWinCurIdx(pTaskInfo);
|
2025-05-10 07:41:44 +00:00
|
|
|
if (curIdx + 1 >= pExtW->pWins->size) return NULL;
|
|
|
|
|
return taosArrayGet(pExtW->pWins, curIdx + 1);
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-21 00:10:55 +00:00
|
|
|
static int32_t getNextStartPos(STimeWindow win, const SDataBlockInfo* pBlockInfo, int32_t lastEndPos, int32_t order, int32_t* nextPos, int64_t* tsCol) {
|
2025-04-29 07:31:17 +00:00
|
|
|
bool ascQuery = order == TSDB_ORDER_ASC;
|
|
|
|
|
|
2025-07-21 00:10:55 +00:00
|
|
|
if (win.ekey <= pBlockInfo->window.skey && ascQuery) {
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
//if (win.skey > pBlockInfo->window.ekey && !ascQuery) return -2;
|
2025-07-05 10:15:00 +00:00
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
if (win.skey > pBlockInfo->window.ekey && ascQuery) return -1;
|
2025-07-21 00:10:55 +00:00
|
|
|
//if (win.ekey < pBlockInfo->window.skey && !ascQuery) return -1;
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
if (win.ekey <= tsCol[lastEndPos + 1] && ascQuery) return -2;
|
|
|
|
|
if (win.skey <= tsCol[lastEndPos + 1] && ascQuery) break;
|
|
|
|
|
lastEndPos++;
|
|
|
|
|
}
|
2025-07-05 10:15:00 +00:00
|
|
|
|
|
|
|
|
*nextPos = lastEndPos + 1;
|
|
|
|
|
return 0;
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-22 08:38:37 +00:00
|
|
|
static int32_t setExtWindowOutputBuf(SResultRowInfo* pResultRowInfo, const STimeWindow* win, SResultRow** pResult, int64_t tableGroupId, SqlFunctionCtx* pCtx,
|
2025-04-29 07:31:17 +00:00
|
|
|
int32_t numOfOutput, int32_t* rowEntryInfoOffset, SAggSupporter* pAggSup,
|
|
|
|
|
SExecTaskInfo* pTaskInfo) {
|
|
|
|
|
SResultRow* pResultRow = doSetResultOutBufByKey(pAggSup->pResultBuf, pResultRowInfo, (char*)&win->skey, TSDB_KEYSIZE,
|
|
|
|
|
true, tableGroupId, pTaskInfo, true, pAggSup, true);
|
|
|
|
|
|
|
|
|
|
if (pResultRow == NULL || pTaskInfo->code != 0) {
|
|
|
|
|
*pResult = NULL;
|
|
|
|
|
qError("failed to set result output buffer, error:%s", tstrerror(pTaskInfo->code));
|
|
|
|
|
return pTaskInfo->code;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-17 05:26:02 +00:00
|
|
|
qDebug("current result rows num:%d", tSimpleHashGetSize(pAggSup->pResultRowHashTable));
|
|
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
// set time window for current result
|
|
|
|
|
TAOS_SET_POBJ_ALIGNED(&pResultRow->win, win);
|
|
|
|
|
*pResult = pResultRow;
|
2025-07-22 08:38:37 +00:00
|
|
|
pTaskInfo->code = setResultRowInitCtx(pResultRow, pCtx, numOfOutput, rowEntryInfoOffset);
|
|
|
|
|
if (pTaskInfo->code) {
|
|
|
|
|
*pResult = NULL;
|
|
|
|
|
qError("failed to set result row ctx, error:%s", tstrerror(pTaskInfo->code));
|
|
|
|
|
return pTaskInfo->code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TSDB_CODE_SUCCESS;
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t extWindowDoHashAgg(SOperatorInfo* pOperator, int32_t startPos, int32_t forwardRows,
|
|
|
|
|
SSDataBlock* pInputBlock) {
|
2025-07-07 11:07:09 +00:00
|
|
|
if (forwardRows == 0) return 0;
|
2025-04-29 07:31:17 +00:00
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
|
|
|
|
SExternalWindowOperator* pExtW = pOperator->info;
|
|
|
|
|
return applyAggFunctionOnPartialTuples(pOperator->pTaskInfo, pSup->pCtx, &pExtW->twAggSup.timeWindowData, startPos,
|
|
|
|
|
forwardRows, pInputBlock->info.rows, pSup->numOfExprs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static SSDataBlock* extWindowGetOutputBlock(SOperatorInfo* pOperator, int32_t winIdx) {
|
|
|
|
|
SExternalWindowOperator* pExtW = pOperator->info;
|
|
|
|
|
SSDataBlock* pBlock = NULL;
|
|
|
|
|
SBlockList* pBlockList = taosArrayGet(pExtW->pOutputBlocks, winIdx);
|
|
|
|
|
int32_t code = blockListGetLastBlock(pBlockList, &pBlock);
|
|
|
|
|
if (code != 0) terrno = code;
|
|
|
|
|
return pBlock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t extWindowCopyRows(SOperatorInfo* pOperator, SSDataBlock* pInputBlock, int32_t startPos,
|
|
|
|
|
int32_t forwardRows) {
|
2025-05-16 01:41:31 +00:00
|
|
|
if (forwardRows == 0) return 0;
|
2025-04-29 07:31:17 +00:00
|
|
|
SExternalWindowOperator* pExtW = pOperator->info;
|
2025-05-20 03:12:55 +00:00
|
|
|
SSDataBlock* pResBlock = extWindowGetOutputBlock(pOperator, getExtWinCurIdx(pOperator->pTaskInfo));
|
2025-05-10 08:23:34 +00:00
|
|
|
SExprSupp* pExprSup = &pExtW->scalarSupp;
|
|
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
if (!pResBlock) {
|
2025-07-14 11:15:21 +00:00
|
|
|
qError("%s failed to get output block for ext window:%s", GET_TASKID(pOperator->pTaskInfo), tstrerror(terrno));
|
2025-04-29 07:31:17 +00:00
|
|
|
return terrno;
|
|
|
|
|
}
|
2025-05-10 08:23:34 +00:00
|
|
|
int32_t rowsToCopy = forwardRows;
|
2025-04-29 07:31:17 +00:00
|
|
|
int32_t code = 0;
|
2025-05-10 08:23:34 +00:00
|
|
|
if (!pExtW->pTmpBlock)
|
|
|
|
|
code = createOneDataBlock(pInputBlock, false, &pExtW->pTmpBlock);
|
|
|
|
|
else
|
|
|
|
|
blockDataCleanup(pExtW->pTmpBlock);
|
|
|
|
|
if (code) {
|
2025-07-14 11:15:21 +00:00
|
|
|
qError("%s failed to create datablock:%s", GET_TASKID(pOperator->pTaskInfo), tstrerror(terrno));
|
2025-05-10 08:23:34 +00:00
|
|
|
return code;
|
|
|
|
|
}
|
2025-05-16 01:41:31 +00:00
|
|
|
code = blockDataEnsureCapacity(pExtW->pTmpBlock, TMAX(1, rowsToCopy));
|
2025-05-10 08:23:34 +00:00
|
|
|
if (code) {
|
2025-07-14 11:15:21 +00:00
|
|
|
qError("%s failed to ensure capacity:%s", GET_TASKID(pOperator->pTaskInfo), tstrerror(terrno));
|
2025-05-10 08:23:34 +00:00
|
|
|
return code;
|
|
|
|
|
}
|
2025-04-29 07:31:17 +00:00
|
|
|
if (rowsToCopy > 0) {
|
2025-05-10 08:23:34 +00:00
|
|
|
code = blockDataMergeNRows(pExtW->pTmpBlock, pInputBlock, startPos, rowsToCopy);
|
2025-05-16 01:41:31 +00:00
|
|
|
if (code == 0) {
|
2025-07-08 08:32:49 +00:00
|
|
|
code = projectApplyFunctionsWithSelect(pExprSup->pExprInfo, pResBlock, pExtW->pTmpBlock, pExprSup->pCtx, pExprSup->numOfExprs,
|
|
|
|
|
NULL, GET_STM_RTINFO(pOperator->pTaskInfo), true);
|
2025-05-16 01:41:31 +00:00
|
|
|
}
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-10 08:23:34 +00:00
|
|
|
if (code != 0) return code;
|
2025-04-29 07:31:17 +00:00
|
|
|
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t hashExternalWindowProject(SOperatorInfo* pOperator, SSDataBlock* pInputBlock) {
|
|
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
SExternalWindowOperator* pExtW = pOperator->info;
|
|
|
|
|
SqlFunctionCtx* pCtx = NULL;
|
|
|
|
|
int64_t* tsCol = extractTsCol(pInputBlock, pExtW->primaryTsIndex, pTaskInfo);
|
|
|
|
|
TSKEY ts = getStartTsKey(&pInputBlock->info.window, tsCol);
|
2025-07-14 11:15:21 +00:00
|
|
|
const STimeWindow* pWin = getExtWindow(pOperator, pExtW, ts);
|
2025-04-29 07:31:17 +00:00
|
|
|
bool ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
|
|
|
|
|
int32_t tsOrder = pExtW->binfo.inputTsOrder;
|
2025-05-21 00:50:15 +00:00
|
|
|
int32_t startPos = 0;
|
2025-04-29 07:31:17 +00:00
|
|
|
|
2025-05-16 01:41:31 +00:00
|
|
|
if (!pWin) return 0;
|
2025-04-29 07:31:17 +00:00
|
|
|
TSKEY ekey = ascScan ? pWin->ekey : pWin->skey;
|
|
|
|
|
int32_t forwardRows =
|
2025-05-16 01:41:31 +00:00
|
|
|
getNumOfRowsInTimeWindow(&pInputBlock->info, tsCol, startPos, ekey-1, binarySearchForKey, NULL, tsOrder);
|
2025-04-29 07:31:17 +00:00
|
|
|
|
|
|
|
|
int32_t code = extWindowCopyRows(pOperator, pInputBlock, startPos, forwardRows);
|
|
|
|
|
|
|
|
|
|
while (code == 0 && pInputBlock->info.rows > startPos + forwardRows) {
|
2025-05-20 03:12:55 +00:00
|
|
|
pWin = getExtNextWindow(pExtW, pTaskInfo);
|
2025-04-29 07:31:17 +00:00
|
|
|
if (!pWin) break;
|
2025-05-14 09:55:32 +00:00
|
|
|
incExtWinCurIdx(pOperator);
|
2025-04-29 07:31:17 +00:00
|
|
|
|
2025-05-16 01:41:31 +00:00
|
|
|
startPos = startPos + forwardRows;
|
2025-04-29 07:31:17 +00:00
|
|
|
ekey = ascScan ? pWin->ekey : pWin->skey;
|
|
|
|
|
forwardRows =
|
2025-05-16 01:41:31 +00:00
|
|
|
getNumOfRowsInTimeWindow(&pInputBlock->info, tsCol, startPos, ekey-1, binarySearchForKey, NULL, tsOrder);
|
2025-04-29 07:31:17 +00:00
|
|
|
code = extWindowCopyRows(pOperator, pInputBlock, startPos, forwardRows);
|
|
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 09:49:48 +00:00
|
|
|
static int32_t extWindowDoIndefRows(SOperatorInfo* pOperator, SSDataBlock* pRes, SSDataBlock* pBlock) {
|
|
|
|
|
SExternalWindowOperator* pExtW = pOperator->info;
|
|
|
|
|
SOptrBasicInfo* pInfo = &pExtW->binfo;
|
|
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
int32_t order = pInfo->inputTsOrder;
|
|
|
|
|
int32_t scanFlag = pBlock->info.scanFlag;
|
|
|
|
|
int32_t code = TSDB_CODE_SUCCESS, lino = 0;
|
|
|
|
|
|
|
|
|
|
// there is an scalar expression that needs to be calculated before apply the group aggregation.
|
|
|
|
|
SExprSupp* pScalarSup = &pExtW->scalarSupp;
|
|
|
|
|
if (pScalarSup->pExprInfo != NULL) {
|
|
|
|
|
TAOS_CHECK_EXIT(projectApplyFunctions(pScalarSup->pExprInfo, pBlock, pBlock, pScalarSup->pCtx, pScalarSup->numOfExprs,
|
|
|
|
|
pExtW->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TAOS_CHECK_EXIT(setInputDataBlock(pSup, pBlock, order, scanFlag, false));
|
|
|
|
|
|
|
|
|
|
TAOS_CHECK_EXIT(blockDataEnsureCapacity(pRes, pRes->info.rows + pBlock->info.rows));
|
|
|
|
|
|
|
|
|
|
TAOS_CHECK_EXIT(projectApplyFunctions(pSup->pExprInfo, pRes, pBlock, pSup->pCtx, pSup->numOfExprs,
|
|
|
|
|
pExtW->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo)));
|
|
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
|
|
|
|
|
|
if (code) {
|
|
|
|
|
qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __FUNCTION__, lino, tstrerror(code));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int32_t extWindowHandleIndefRows(SOperatorInfo* pOperator, SSDataBlock* pInputBlock, int32_t startPos,
|
|
|
|
|
int32_t forwardRows) {
|
|
|
|
|
if (forwardRows == 0) return 0;
|
|
|
|
|
SExternalWindowOperator* pExtW = pOperator->info;
|
|
|
|
|
SSDataBlock* pResBlock = extWindowGetOutputBlock(pOperator, getExtWinCurIdx(pOperator->pTaskInfo));
|
|
|
|
|
|
|
|
|
|
if (!pResBlock) {
|
|
|
|
|
qError("%s failed to get output block for ext window:%s", GET_TASKID(pOperator->pTaskInfo), tstrerror(terrno));
|
|
|
|
|
return terrno;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t rowsToCopy = forwardRows;
|
|
|
|
|
int32_t code = 0;
|
|
|
|
|
if (!pExtW->pTmpBlock)
|
|
|
|
|
code = createOneDataBlock(pInputBlock, false, &pExtW->pTmpBlock);
|
|
|
|
|
else
|
|
|
|
|
blockDataCleanup(pExtW->pTmpBlock);
|
|
|
|
|
|
|
|
|
|
if (code) {
|
|
|
|
|
qError("%s failed to create datablock:%s", GET_TASKID(pOperator->pTaskInfo), tstrerror(terrno));
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
code = blockDataEnsureCapacity(pExtW->pTmpBlock, TMAX(1, rowsToCopy));
|
|
|
|
|
if (code) {
|
|
|
|
|
qError("%s failed to ensure capacity:%s", GET_TASKID(pOperator->pTaskInfo), tstrerror(terrno));
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rowsToCopy > 0) {
|
|
|
|
|
code = blockDataMergeNRows(pExtW->pTmpBlock, pInputBlock, startPos, rowsToCopy);
|
|
|
|
|
if (code == 0) {
|
|
|
|
|
code = extWindowDoIndefRows(pOperator, pResBlock, pExtW->pTmpBlock);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int32_t hashExternalWindowIndefRows(SOperatorInfo* pOperator, SSDataBlock* pInputBlock) {
|
|
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
SExternalWindowOperator* pExtW = pOperator->info;
|
|
|
|
|
SqlFunctionCtx* pCtx = NULL;
|
|
|
|
|
int64_t* tsCol = extractTsCol(pInputBlock, pExtW->primaryTsIndex, pTaskInfo);
|
|
|
|
|
TSKEY ts = getStartTsKey(&pInputBlock->info.window, tsCol);
|
|
|
|
|
const STimeWindow* pWin = getExtWindow(pOperator, pExtW, ts);
|
|
|
|
|
bool ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
|
|
|
|
|
int32_t tsOrder = pExtW->binfo.inputTsOrder;
|
|
|
|
|
int32_t startPos = 0;
|
|
|
|
|
|
|
|
|
|
if (!pWin) return 0;
|
|
|
|
|
TSKEY ekey = ascScan ? pWin->ekey : pWin->skey;
|
|
|
|
|
int32_t forwardRows =
|
|
|
|
|
getNumOfRowsInTimeWindow(&pInputBlock->info, tsCol, startPos, ekey-1, binarySearchForKey, NULL, tsOrder);
|
|
|
|
|
|
|
|
|
|
int32_t code = extWindowHandleIndefRows(pOperator, pInputBlock, startPos, forwardRows);
|
|
|
|
|
|
|
|
|
|
while (code == 0 && pInputBlock->info.rows > startPos + forwardRows) {
|
|
|
|
|
pWin = getExtNextWindow(pExtW, pTaskInfo);
|
|
|
|
|
if (!pWin) break;
|
|
|
|
|
incExtWinCurIdx(pOperator);
|
|
|
|
|
|
|
|
|
|
startPos = startPos + forwardRows;
|
|
|
|
|
ekey = ascScan ? pWin->ekey : pWin->skey;
|
|
|
|
|
forwardRows =
|
|
|
|
|
getNumOfRowsInTimeWindow(&pInputBlock->info, tsCol, startPos, ekey-1, binarySearchForKey, NULL, tsOrder);
|
|
|
|
|
code = extWindowHandleIndefRows(pOperator, pInputBlock, startPos, forwardRows);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-07-22 08:38:37 +00:00
|
|
|
static int32_t extWindowProcessEmptyWins(SOperatorInfo* pOperator, SSDataBlock* pBlock, bool allRemains) {
|
|
|
|
|
int32_t code = 0, lino = 0;
|
|
|
|
|
SExternalWindowOperator* pExtW = (SExternalWindowOperator*)pOperator->info;
|
|
|
|
|
bool ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
|
|
|
|
|
int32_t currIdx = getExtWinCurIdx(pOperator->pTaskInfo);
|
|
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
|
|
|
|
|
|
|
|
|
if (NULL == pExtW->pEmptyInputBlock) {
|
|
|
|
|
goto _exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t endIdx = allRemains ? (pExtW->pWins->size - 1) : (currIdx - 1);
|
|
|
|
|
SResultRowInfo* pResultRowInfo = &pExtW->binfo.resultRowInfo;
|
|
|
|
|
SResultRow* pResult = NULL;
|
|
|
|
|
SSDataBlock* pInput = pExtW->pEmptyInputBlock;
|
|
|
|
|
|
|
|
|
|
if ((pExtW->lastWinId + 1) <= endIdx) {
|
|
|
|
|
TAOS_CHECK_EXIT(setInputDataBlock(pSup, pExtW->pEmptyInputBlock, pExtW->binfo.inputTsOrder, MAIN_SCAN, true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int32_t i = pExtW->lastWinId + 1; i <= endIdx; ++i) {
|
|
|
|
|
const STimeWindow* pWin = taosArrayGet(pExtW->pWins, i);
|
|
|
|
|
|
|
|
|
|
setExtWinCurIdx(pOperator, i);
|
|
|
|
|
qDebug("%s %dth ext empty window start:%" PRId64 ", end:%" PRId64 ", ascScan:%d",
|
|
|
|
|
GET_TASKID(pOperator->pTaskInfo), i, pWin->skey, pWin->ekey, ascScan);
|
|
|
|
|
|
|
|
|
|
TAOS_CHECK_EXIT(setExtWindowOutputBuf(pResultRowInfo, pWin, &pResult, pInput->info.id.groupId, pSup->pCtx, pSup->numOfExprs,
|
|
|
|
|
pSup->rowEntryInfoOffset, &pExtW->aggSup, pOperator->pTaskInfo));
|
|
|
|
|
|
|
|
|
|
updateTimeWindowInfo(&pExtW->twAggSup.timeWindowData, pWin, 1);
|
|
|
|
|
code = extWindowDoHashAgg(pOperator, 0, 1, pInput);
|
|
|
|
|
pExtW->lastWinId = i;
|
|
|
|
|
TAOS_CHECK_EXIT(code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
|
|
|
|
|
|
if (code) {
|
|
|
|
|
qError("%s %s failed at line %d since %s", pOperator->pTaskInfo->id.str, __FUNCTION__, lino, tstrerror(code));
|
|
|
|
|
} else {
|
|
|
|
|
if (pBlock) {
|
|
|
|
|
TAOS_CHECK_EXIT(setInputDataBlock(pSup, pBlock, pExtW->binfo.inputTsOrder, MAIN_SCAN, true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!allRemains) {
|
|
|
|
|
setExtWinCurIdx(pOperator, currIdx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-17 05:26:02 +00:00
|
|
|
static void hashExternalWindowAgg(SOperatorInfo* pOperator, SSDataBlock* pInputBlock) {
|
2025-04-29 07:31:17 +00:00
|
|
|
SExternalWindowOperator* pExtW = (SExternalWindowOperator*)pOperator->info;
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
|
|
|
|
SResultRowInfo* pResultRowInfo = &pExtW->binfo.resultRowInfo;
|
|
|
|
|
int32_t startPos = 0;
|
|
|
|
|
int32_t numOfOutput = pSup->numOfExprs;
|
|
|
|
|
int64_t* tsCols = extractTsCol(pInputBlock, pExtW->primaryTsIndex, pTaskInfo);
|
|
|
|
|
bool ascScan = pExtW->binfo.inputTsOrder == TSDB_ORDER_ASC;
|
|
|
|
|
TSKEY ts = getStartTsKey(&pInputBlock->info.window, tsCols);
|
|
|
|
|
SResultRow* pResult = NULL;
|
|
|
|
|
int32_t ret = 0;
|
|
|
|
|
|
2025-07-14 11:15:21 +00:00
|
|
|
const STimeWindow* pWin = getExtWindow(pOperator, pExtW, ts);
|
2025-06-18 12:11:55 +00:00
|
|
|
if (pWin == NULL) {
|
2025-07-14 11:15:21 +00:00
|
|
|
qError("%s failed to get time window for ts:%" PRId64 ", error:%s", GET_TASKID(pOperator->pTaskInfo), ts, tstrerror(terrno));
|
2025-06-18 12:11:55 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2025-07-22 08:38:37 +00:00
|
|
|
|
|
|
|
|
ret = extWindowProcessEmptyWins(pOperator, pInputBlock, false);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, ret);
|
|
|
|
|
}
|
2025-07-09 10:57:01 +00:00
|
|
|
|
2025-07-14 11:15:21 +00:00
|
|
|
qDebug("%s ext window1 start:%" PRId64 ", end:%" PRId64 ", ts:%" PRId64 ", ascScan:%d",
|
2025-07-22 08:38:37 +00:00
|
|
|
GET_TASKID(pOperator->pTaskInfo), pWin->skey, pWin->ekey, ts, ascScan);
|
2025-07-14 12:41:03 +00:00
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
STimeWindow win = *pWin;
|
2025-07-17 05:26:02 +00:00
|
|
|
ret = setExtWindowOutputBuf(pResultRowInfo, &win, &pResult, pInputBlock->info.id.groupId, pSup->pCtx, numOfOutput,
|
|
|
|
|
pSup->rowEntryInfoOffset, &pExtW->aggSup, pTaskInfo);
|
|
|
|
|
if (ret != 0 || !pResult) {
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, ret);
|
2025-07-09 10:57:01 +00:00
|
|
|
}
|
2025-07-14 12:41:03 +00:00
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
TSKEY ekey = ascScan ? win.ekey : win.skey;
|
2025-05-16 10:18:36 +00:00
|
|
|
int32_t forwardRows = getNumOfRowsInTimeWindow(&pInputBlock->info, tsCols, startPos, ekey - 1, binarySearchForKey, NULL,
|
|
|
|
|
pExtW->binfo.inputTsOrder);
|
2025-04-29 07:31:17 +00:00
|
|
|
|
|
|
|
|
updateTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &win, 1);
|
|
|
|
|
ret = extWindowDoHashAgg(pOperator, startPos, forwardRows, pInputBlock);
|
2025-07-22 08:38:37 +00:00
|
|
|
pExtW->lastWinId = getExtWinCurIdx(pTaskInfo);
|
2025-04-29 07:31:17 +00:00
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, ret);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-05 10:15:00 +00:00
|
|
|
int32_t nextPosGot = 0;
|
2025-04-29 07:31:17 +00:00
|
|
|
while (1) {
|
2025-05-16 10:18:36 +00:00
|
|
|
int32_t prevEndPos = forwardRows + startPos - 1;
|
2025-07-21 00:10:55 +00:00
|
|
|
if (prevEndPos >= pInputBlock->info.rows) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-20 03:12:55 +00:00
|
|
|
pWin = getExtNextWindow(pExtW, pTaskInfo);
|
2025-04-29 07:31:17 +00:00
|
|
|
if (!pWin)
|
|
|
|
|
break;
|
|
|
|
|
else
|
|
|
|
|
win = *pWin;
|
2025-07-05 10:15:00 +00:00
|
|
|
|
2025-07-14 11:15:21 +00:00
|
|
|
qDebug("%s ext window2 start:%" PRId64 ", end:%" PRId64 ", ts:%" PRId64 ", ascScan:%d",
|
|
|
|
|
GET_TASKID(pOperator->pTaskInfo), win.skey, win.ekey, ts, ascScan);
|
2025-07-05 10:15:00 +00:00
|
|
|
|
2025-07-21 00:10:55 +00:00
|
|
|
nextPosGot = getNextStartPos(win, &pInputBlock->info, prevEndPos, pExtW->binfo.inputTsOrder, &startPos, tsCols);
|
2025-07-05 10:15:00 +00:00
|
|
|
if (-1 == nextPosGot) {
|
2025-07-14 11:15:21 +00:00
|
|
|
qDebug("%s ignore current block", GET_TASKID(pOperator->pTaskInfo));
|
2025-07-05 10:15:00 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (-2 == nextPosGot) {
|
2025-07-14 11:15:21 +00:00
|
|
|
qDebug("%s skip current window", GET_TASKID(pOperator->pTaskInfo));
|
2025-07-21 00:10:55 +00:00
|
|
|
incExtWinCurIdx(pOperator);
|
2025-07-05 10:15:00 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-10 07:41:44 +00:00
|
|
|
incExtWinCurIdx(pOperator);
|
2025-04-29 07:31:17 +00:00
|
|
|
|
2025-07-22 08:38:37 +00:00
|
|
|
ret = extWindowProcessEmptyWins(pOperator, pInputBlock, false);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, ret);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
ekey = ascScan ? win.ekey : win.skey;
|
2025-05-16 10:18:36 +00:00
|
|
|
forwardRows = getNumOfRowsInTimeWindow(&pInputBlock->info, tsCols, startPos, ekey - 1, binarySearchForKey, NULL,
|
|
|
|
|
pExtW->binfo.inputTsOrder);
|
2025-04-29 07:31:17 +00:00
|
|
|
|
|
|
|
|
ret = setExtWindowOutputBuf(pResultRowInfo, &win, &pResult, pInputBlock->info.id.groupId, pSup->pCtx,
|
|
|
|
|
numOfOutput, pSup->rowEntryInfoOffset, &pExtW->aggSup, pTaskInfo);
|
|
|
|
|
if (ret != 0 || !pResult) T_LONG_JMP(pTaskInfo->env, ret);
|
|
|
|
|
|
|
|
|
|
updateTimeWindowInfo(&pExtW->twAggSup.timeWindowData, &win, 1);
|
|
|
|
|
ret = extWindowDoHashAgg(pOperator, startPos, forwardRows, pInputBlock);
|
2025-07-22 08:38:37 +00:00
|
|
|
pExtW->lastWinId = getExtWinCurIdx(pTaskInfo);
|
2025-04-29 07:31:17 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, ret);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 08:38:37 +00:00
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
static int32_t doOpenExternalWindow(SOperatorInfo* pOperator) {
|
|
|
|
|
if (OPTR_IS_OPENED(pOperator)) return TSDB_CODE_SUCCESS;
|
|
|
|
|
int32_t code = 0;
|
|
|
|
|
int32_t lino = 0;
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
SOperatorInfo* pDownstream = pOperator->pDownstream[0];
|
|
|
|
|
SExternalWindowOperator* pExtW = pOperator->info;
|
|
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
2025-07-19 09:49:48 +00:00
|
|
|
pTaskInfo->pStreamRuntimeInfo->funcInfo.extWinProjMode = (pExtW->mode != EEXT_MODE_AGG);
|
2025-04-29 07:31:17 +00:00
|
|
|
|
|
|
|
|
int32_t scanFlag = MAIN_SCAN;
|
|
|
|
|
int64_t st = taosGetTimestampUs();
|
|
|
|
|
|
2025-05-09 10:05:02 +00:00
|
|
|
if (!pExtW->pWins) {
|
|
|
|
|
size_t size = taosArrayGetSize(pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamPesudoFuncVals);
|
|
|
|
|
pExtW->pWins = taosArrayInit(size, sizeof(STimeWindow));
|
2025-07-19 09:49:48 +00:00
|
|
|
if (!pExtW->pWins) QUERY_CHECK_CODE(terrno, lino, _exit);
|
2025-05-10 07:41:44 +00:00
|
|
|
pExtW->pOutputBlocks = taosArrayInit(size, sizeof(SBlockList));
|
2025-07-19 09:49:48 +00:00
|
|
|
if (!pExtW->pOutputBlocks) QUERY_CHECK_CODE(terrno, lino, _exit);
|
2025-07-04 07:38:01 +00:00
|
|
|
// pExtW->pOffsetList = taosArrayInit(size, sizeof(SLimitInfo));
|
|
|
|
|
// if (!pExtW->pOffsetList) QUERY_CHECK_CODE(terrno, lino, _end);
|
2025-05-09 10:05:02 +00:00
|
|
|
for (int32_t i = 0; i < size; ++i) {
|
|
|
|
|
SSTriggerCalcParam* pParam = taosArrayGet(pTaskInfo->pStreamRuntimeInfo->funcInfo.pStreamPesudoFuncVals, i);
|
|
|
|
|
STimeWindow win = {.skey = pParam->wstart, .ekey = pParam->wend};
|
2025-07-09 10:57:01 +00:00
|
|
|
if (pTaskInfo->pStreamRuntimeInfo->funcInfo.triggerType != STREAM_TRIGGER_SLIDING) {
|
2025-06-18 12:11:55 +00:00
|
|
|
win.ekey++;
|
|
|
|
|
}
|
2025-07-19 09:49:48 +00:00
|
|
|
TSDB_CHECK_NULL(taosArrayPush(pExtW->pWins, &win), code, lino, _exit, terrno);
|
2025-05-10 07:41:44 +00:00
|
|
|
|
|
|
|
|
SBlockList bl = {.pSrcBlock = pExtW->binfo.pRes, .pBlocks = 0, .blockRowNumThreshold = 4096};
|
|
|
|
|
code = blockListInit(&bl, 4096);
|
2025-07-19 09:49:48 +00:00
|
|
|
if (code != 0) QUERY_CHECK_CODE(code, lino, _exit);
|
|
|
|
|
TSDB_CHECK_NULL(taosArrayPush(pExtW->pOutputBlocks, &bl), code, lino, _exit, terrno);
|
2025-05-09 10:05:02 +00:00
|
|
|
}
|
2025-05-12 09:02:38 +00:00
|
|
|
pExtW->outputWinId = pTaskInfo->pStreamRuntimeInfo->funcInfo.curIdx;
|
2025-07-22 08:38:37 +00:00
|
|
|
pExtW->lastWinId = -1;
|
2025-05-09 10:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
while (1) {
|
|
|
|
|
SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
|
2025-07-22 08:38:37 +00:00
|
|
|
if (pBlock == NULL) {
|
|
|
|
|
if (EEXT_MODE_AGG == pExtW->mode) {
|
|
|
|
|
TAOS_CHECK_EXIT(extWindowProcessEmptyWins(pOperator, pBlock, true));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-04-29 07:31:17 +00:00
|
|
|
|
2025-06-19 12:29:54 +00:00
|
|
|
printDataBlock(pBlock, __func__, "externalwindow");
|
|
|
|
|
|
2025-07-19 09:49:48 +00:00
|
|
|
qDebug("ext windowpExtW->mode:%d", pExtW->mode);
|
|
|
|
|
switch (pExtW->mode) {
|
|
|
|
|
case EEXT_MODE_SCALAR:
|
|
|
|
|
TAOS_CHECK_EXIT(hashExternalWindowProject(pOperator, pBlock));
|
|
|
|
|
break;
|
|
|
|
|
case EEXT_MODE_AGG:
|
|
|
|
|
hashExternalWindowAgg(pOperator, pBlock);
|
|
|
|
|
break;
|
|
|
|
|
case EEXT_MODE_INDEFR_FUNC:
|
|
|
|
|
TAOS_CHECK_EXIT(hashExternalWindowIndefRows(pOperator, pBlock));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-07-21 00:10:55 +00:00
|
|
|
|
2025-07-22 08:38:37 +00:00
|
|
|
OPTR_SET_OPENED(pOperator);
|
|
|
|
|
|
2025-07-21 00:10:55 +00:00
|
|
|
if (pExtW->mode == EEXT_MODE_AGG) {
|
|
|
|
|
qDebug("ext window before dump final rows num:%d", tSimpleHashGetSize(pExtW->aggSup.pResultRowHashTable));
|
|
|
|
|
|
|
|
|
|
code = initGroupedResultInfo(&pExtW->groupResInfo, pExtW->aggSup.pResultRowHashTable, pExtW->binfo.inputTsOrder);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _exit);
|
|
|
|
|
|
|
|
|
|
qDebug("ext window after dump final rows num:%d", tSimpleHashGetSize(pExtW->aggSup.pResultRowHashTable));
|
|
|
|
|
}
|
2025-04-29 07:31:17 +00:00
|
|
|
|
2025-07-19 09:49:48 +00:00
|
|
|
_exit:
|
2025-07-17 05:26:02 +00:00
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
if (code != 0) {
|
|
|
|
|
qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
|
|
|
|
|
pTaskInfo->code = code;
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
|
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t externalWindowNext(SOperatorInfo* pOperator, SSDataBlock** ppRes) {
|
|
|
|
|
int32_t code = 0;
|
|
|
|
|
int32_t lino = 0;
|
|
|
|
|
SExternalWindowOperator* pExtW = pOperator->info;
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
|
|
|
|
|
|
|
|
|
if (pOperator->status == OP_EXEC_DONE) {
|
|
|
|
|
*ppRes = NULL;
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SSDataBlock* pBlock = pExtW->binfo.pRes;
|
2025-05-14 09:55:32 +00:00
|
|
|
blockDataCleanup(pBlock);
|
2025-04-29 07:31:17 +00:00
|
|
|
code = pOperator->fpSet._openFn(pOperator);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _end);
|
|
|
|
|
|
|
|
|
|
while (1) {
|
2025-07-19 09:49:48 +00:00
|
|
|
if (pExtW->mode == EEXT_MODE_SCALAR || pExtW->mode == EEXT_MODE_INDEFR_FUNC) {
|
2025-04-29 07:31:17 +00:00
|
|
|
if (pExtW->outputWinId >= taosArrayGetSize(pExtW->pOutputBlocks)) {
|
|
|
|
|
pBlock->info.rows = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
SBlockList* pList = taosArrayGet(pExtW->pOutputBlocks, pExtW->outputWinId);
|
2025-07-04 07:38:01 +00:00
|
|
|
// SLimitInfo* pLimitInfo = taosArrayGet(pExtW->pOffsetList, pExtW->outputWinId);
|
2025-04-29 07:31:17 +00:00
|
|
|
if (pExtW->pOutputBlockListNode == NULL) {
|
|
|
|
|
pExtW->pOutputBlockListNode = tdListGetHead(pList->pBlocks);
|
|
|
|
|
} else {
|
|
|
|
|
if (!pExtW->pOutputBlockListNode->dl_next_) {
|
|
|
|
|
pExtW->pOutputBlockListNode = NULL;
|
|
|
|
|
pExtW->outputWinId++;
|
2025-05-20 03:12:55 +00:00
|
|
|
incExtWinOutIdx(pOperator);
|
2025-04-29 07:31:17 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
pExtW->pOutputBlockListNode = pExtW->pOutputBlockListNode->dl_next_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pExtW->pOutputBlockListNode) {
|
|
|
|
|
code = blockDataMerge(pBlock, *(SSDataBlock**)pExtW->pOutputBlockListNode->data);
|
|
|
|
|
if (code != 0) goto _end;
|
2025-07-04 07:38:01 +00:00
|
|
|
|
|
|
|
|
// int32_t status = handleLimitOffset(pOperator, pLimitInfo, pBlock);
|
|
|
|
|
// if (status == EXTERNAL_RETRIEVE_CONTINUE) {
|
|
|
|
|
// continue;
|
|
|
|
|
// } else if (status == EXTERNAL_RETRIEVE_NEXT_WINDOW) {
|
|
|
|
|
// pExtW->pOutputBlockListNode = NULL;
|
|
|
|
|
// pExtW->outputWinId++;
|
|
|
|
|
// incExtWinOutIdx(pOperator);
|
|
|
|
|
// continue;
|
|
|
|
|
// } else if (status == EXTERNAL_RETRIEVE_DONE) {
|
|
|
|
|
// // do nothing, just break
|
|
|
|
|
// }
|
2025-04-29 07:31:17 +00:00
|
|
|
} else {
|
2025-05-16 01:41:31 +00:00
|
|
|
pExtW->outputWinId++;
|
2025-05-20 03:12:55 +00:00
|
|
|
incExtWinOutIdx(pOperator);
|
2025-05-16 01:41:31 +00:00
|
|
|
continue;
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
doBuildResultDatablock(pOperator, &pExtW->binfo, &pExtW->groupResInfo, pExtW->aggSup.pResultBuf);
|
|
|
|
|
bool hasRemain = hasRemainResults(&pExtW->groupResInfo);
|
|
|
|
|
if (!hasRemain) {
|
|
|
|
|
setOperatorCompleted(pOperator);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (pExtW->binfo.pRes->info.rows > 0) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pOperator->resultInfo.totalRows += pExtW->binfo.pRes->info.rows;
|
2025-07-09 10:57:01 +00:00
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
_end:
|
2025-07-09 10:57:01 +00:00
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
if (code != 0) {
|
|
|
|
|
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
|
|
|
|
|
pTaskInfo->code = code;
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
|
|
|
|
}
|
|
|
|
|
(*ppRes) = (pExtW->binfo.pRes->info.rows == 0) ? NULL : pExtW->binfo.pRes;
|
2025-07-11 05:55:15 +00:00
|
|
|
|
|
|
|
|
if (pTaskInfo->execModel == OPTR_EXEC_MODEL_STREAM && (*ppRes)) {
|
|
|
|
|
printDataBlock(*ppRes, getStreamOpName(pOperator->operatorType), GET_TASKID(pTaskInfo));
|
|
|
|
|
}
|
2025-07-09 10:57:01 +00:00
|
|
|
|
2025-04-29 07:31:17 +00:00
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t setSingleOutputTupleBuf(SResultRowInfo* pResultRowInfo, const STimeWindow* pWin, SResultRow** pResult,
|
|
|
|
|
SExprSupp* pExprSup, SAggSupporter* pAggSup) {
|
|
|
|
|
if (*pResult == NULL) {
|
|
|
|
|
*pResult = getNewResultRow(pAggSup->pResultBuf, &pAggSup->currentPageId, pAggSup->resultRowSize);
|
|
|
|
|
if (!*pResult) {
|
2025-07-12 03:08:37 +00:00
|
|
|
qError("get new resultRow failed, err:%s", tstrerror(terrno));
|
2025-04-29 07:31:17 +00:00
|
|
|
return terrno;
|
|
|
|
|
}
|
|
|
|
|
pResultRowInfo->cur = (SResultRowPosition){.pageId = (*pResult)->pageId, .offset = (*pResult)->offset};
|
|
|
|
|
}
|
|
|
|
|
(*pResult)->win = *pWin;
|
|
|
|
|
return setResultRowInitCtx((*pResult), pExprSup->pCtx, pExprSup->numOfExprs, pExprSup->rowEntryInfoOffset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t doMergeAlignExtWindowAgg(SOperatorInfo* pOperator, SResultRowInfo* pResultRowInfo, SSDataBlock* pBlock, SSDataBlock* pResultBlock) {
|
|
|
|
|
SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
|
|
|
|
|
SExternalWindowOperator* pExtW = pMlExtInfo->pExtW;
|
|
|
|
|
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
|
|
|
|
int32_t code = 0;
|
|
|
|
|
|
|
|
|
|
int32_t startPos = 0;
|
|
|
|
|
int64_t* tsCols = extractTsCol(pBlock, pExtW->primaryTsIndex, pTaskInfo);
|
|
|
|
|
TSKEY ts = getStartTsKey(&pBlock->info.window, tsCols);
|
|
|
|
|
|
2025-07-14 11:15:21 +00:00
|
|
|
const STimeWindow *pWin = getExtWindow(pOperator, pExtW, ts);
|
2025-06-19 12:29:54 +00:00
|
|
|
if (pWin == NULL) {
|
2025-07-02 06:03:00 +00:00
|
|
|
qError("failed to get time window for ts:%" PRId64 ", index:%d, error:%s", ts, pExtW->primaryTsIndex, tstrerror(terrno));
|
2025-06-19 12:29:54 +00:00
|
|
|
T_LONG_JMP(pTaskInfo->env, TSDB_CODE_INVALID_PARA);
|
|
|
|
|
}
|
2025-04-29 07:31:17 +00:00
|
|
|
code = setSingleOutputTupleBuf(pResultRowInfo, pWin, &pMlExtInfo->pResultRow, pSup, &pExtW->aggSup);
|
|
|
|
|
if (code != 0 || pMlExtInfo->pResultRow == NULL) {
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t currPos = startPos;
|
2025-06-19 12:29:54 +00:00
|
|
|
pMlExtInfo->curTs = pWin->skey;
|
2025-04-29 07:31:17 +00:00
|
|
|
while (++currPos < pBlock->info.rows) {
|
|
|
|
|
if (tsCols[currPos] == pMlExtInfo->curTs) continue;
|
|
|
|
|
|
2025-06-19 12:29:54 +00:00
|
|
|
qDebug("current ts:%" PRId64 ", startPos:%d, currPos:%d, tsCols[currPos]:%" PRId64,
|
|
|
|
|
pMlExtInfo->curTs, startPos, currPos, tsCols[currPos]);
|
2025-05-20 03:12:55 +00:00
|
|
|
code = applyAggFunctionOnPartialTuples(pTaskInfo, pSup->pCtx, &pExtW->twAggSup.timeWindowData, startPos,
|
|
|
|
|
currPos - startPos, pBlock->info.rows, pSup->numOfExprs);
|
2025-04-29 07:31:17 +00:00
|
|
|
if (code != 0) {
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finalizeResultRows(pExtW->aggSup.pResultBuf, &pResultRowInfo->cur, pSup, pResultBlock, pTaskInfo);
|
|
|
|
|
resetResultRow(pMlExtInfo->pResultRow, pExtW->aggSup.resultRowSize - sizeof(SResultRow));
|
|
|
|
|
|
2025-05-20 03:12:55 +00:00
|
|
|
pWin = getExtNextWindow(pExtW, pTaskInfo);
|
2025-04-29 07:31:17 +00:00
|
|
|
if (!pWin) break;
|
2025-06-19 12:29:54 +00:00
|
|
|
qDebug("ext window align2 start:%" PRId64 ", end:%" PRId64, pWin->skey, pWin->ekey);
|
2025-05-20 03:12:55 +00:00
|
|
|
incExtWinCurIdx(pOperator);
|
2025-04-29 07:31:17 +00:00
|
|
|
startPos = currPos;
|
|
|
|
|
code = setSingleOutputTupleBuf(pResultRowInfo, pWin, &pMlExtInfo->pResultRow, pSup, &pExtW->aggSup);
|
|
|
|
|
if (code != 0 || pMlExtInfo->pResultRow == NULL) {
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
|
|
|
|
}
|
|
|
|
|
pMlExtInfo->curTs = pWin->skey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
code = applyAggFunctionOnPartialTuples(pTaskInfo, pSup->pCtx, &pExtW->twAggSup.timeWindowData, startPos,
|
|
|
|
|
currPos - startPos, pBlock->info.rows, pSup->numOfExprs);
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
|
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t doMergeAlignExtWindowProject(SOperatorInfo* pOperator, SResultRowInfo* pResultRowInfo, SSDataBlock* pBlock,
|
|
|
|
|
SSDataBlock* pResultBlock) {
|
2025-05-20 03:12:55 +00:00
|
|
|
SExternalWindowOperator* pExtW = pOperator->info;
|
|
|
|
|
SExprSupp* pExprSup = &pExtW->scalarSupp;
|
|
|
|
|
int32_t code = projectApplyFunctions(pExprSup->pExprInfo, pResultBlock, pBlock, pExprSup->pCtx, pExprSup->numOfExprs, NULL,
|
2025-06-27 05:44:23 +00:00
|
|
|
GET_STM_RTINFO(pOperator->pTaskInfo));
|
2025-05-20 03:12:55 +00:00
|
|
|
return code;
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void doMergeAlignExternalWindow(SOperatorInfo* pOperator) {
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
SMergeAlignedExternalWindowOperator* pMlExtInfo = pOperator->info;
|
|
|
|
|
SExternalWindowOperator* pExtW = pMlExtInfo->pExtW;
|
|
|
|
|
SResultRow* pResultRow = NULL;
|
|
|
|
|
int32_t code = 0;
|
|
|
|
|
SSDataBlock* pRes = pExtW->binfo.pRes;
|
|
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
|
|
|
|
int32_t lino = 0;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
|
|
|
|
|
|
|
|
|
|
if (pBlock == NULL) {
|
2025-05-20 03:12:55 +00:00
|
|
|
// close last time window
|
2025-07-19 09:49:48 +00:00
|
|
|
if (pMlExtInfo->curTs != INT64_MIN && EEXT_MODE_AGG == pExtW->mode) {
|
2025-05-20 03:12:55 +00:00
|
|
|
finalizeResultRows(pMlExtInfo->pExtW->aggSup.pResultBuf, &pExtW->binfo.resultRowInfo.cur, pSup, pRes, pTaskInfo);
|
|
|
|
|
resetResultRow(pMlExtInfo->pResultRow,pExtW->aggSup.resultRowSize - sizeof(SResultRow));
|
|
|
|
|
}
|
|
|
|
|
setOperatorCompleted(pOperator);
|
2025-04-29 07:31:17 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pRes->info.scanFlag = pBlock->info.scanFlag;
|
|
|
|
|
code = setInputDataBlock(pSup, pBlock, pExtW->binfo.inputTsOrder, pBlock->info.scanFlag, true);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _end);
|
|
|
|
|
|
2025-06-19 12:29:54 +00:00
|
|
|
printDataBlock(pBlock, __func__, "externalwindowAlign");
|
2025-07-19 09:49:48 +00:00
|
|
|
qDebug("ext windowpExtWAlign->scalarMode:%d", pExtW->mode);
|
2025-06-19 12:29:54 +00:00
|
|
|
|
|
|
|
|
|
2025-07-19 09:49:48 +00:00
|
|
|
if (EEXT_MODE_SCALAR == pExtW->mode) {
|
2025-04-29 07:31:17 +00:00
|
|
|
code = doMergeAlignExtWindowProject(pOperator, &pExtW->binfo.resultRowInfo, pBlock, pRes);
|
|
|
|
|
} else {
|
|
|
|
|
code = doMergeAlignExtWindowAgg(pOperator, &pExtW->binfo.resultRowInfo, pBlock, pRes);
|
|
|
|
|
}
|
2025-07-04 07:38:01 +00:00
|
|
|
|
|
|
|
|
// int32_t status = handleLimitOffset(pOperator, pLimitInfo, pBlock);
|
|
|
|
|
// if (status == EXTERNAL_RETRIEVE_CONTINUE) {
|
|
|
|
|
// continue;
|
|
|
|
|
// } else if (status == EXTERNAL_RETRIEVE_NEXT_WINDOW) {
|
|
|
|
|
// pExtW->pOutputBlockListNode = NULL;
|
|
|
|
|
// pExtW->outputWinId++;
|
|
|
|
|
// incExtWinOutIdx(pOperator);
|
|
|
|
|
// continue;
|
|
|
|
|
// } else if (status == EXTERNAL_RETRIEVE_DONE) {
|
|
|
|
|
// // do nothing, just break
|
|
|
|
|
// }
|
2025-04-29 07:31:17 +00:00
|
|
|
}
|
|
|
|
|
_end:
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
qError("%s failed at line %d since:%s", __func__, lino, tstrerror(code));
|
|
|
|
|
pTaskInfo->code = code;
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
|
|
|
|
}
|
|
|
|
|
}
|