2022-07-25 06:15:49 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-04-28 03:42:34 +00:00
|
|
|
#include "executorInt.h"
|
2022-12-07 09:54:09 +00:00
|
|
|
#include "filter.h"
|
2022-07-25 06:15:49 +00:00
|
|
|
#include "functionMgt.h"
|
2023-04-27 16:23:38 +00:00
|
|
|
#include "operator.h"
|
|
|
|
|
#include "querytask.h"
|
2025-01-08 03:24:24 +00:00
|
|
|
#include "taoserror.h"
|
2023-05-23 10:29:23 +00:00
|
|
|
#include "tdatablock.h"
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2022-11-22 14:42:44 +00:00
|
|
|
typedef struct SProjectOperatorInfo {
|
|
|
|
|
SOptrBasicInfo binfo;
|
|
|
|
|
SAggSupporter aggSup;
|
|
|
|
|
SArray* pPseudoColInfo;
|
|
|
|
|
SLimitInfo limitInfo;
|
|
|
|
|
bool mergeDataBlocks;
|
|
|
|
|
SSDataBlock* pFinalRes;
|
2024-01-16 00:13:47 +00:00
|
|
|
bool inputIgnoreGroup;
|
2024-03-12 03:45:54 +00:00
|
|
|
bool outputIgnoreGroup;
|
2022-11-22 14:42:44 +00:00
|
|
|
} SProjectOperatorInfo;
|
|
|
|
|
|
|
|
|
|
typedef struct SIndefOperatorInfo {
|
|
|
|
|
SOptrBasicInfo binfo;
|
|
|
|
|
SAggSupporter aggSup;
|
|
|
|
|
SArray* pPseudoColInfo;
|
|
|
|
|
SExprSupp scalarSup;
|
|
|
|
|
uint64_t groupId;
|
|
|
|
|
SSDataBlock* pNextGroupRes;
|
|
|
|
|
} SIndefOperatorInfo;
|
|
|
|
|
|
2023-07-06 07:05:49 +00:00
|
|
|
static int32_t doGenerateSourceData(SOperatorInfo* pOperator);
|
2024-07-24 09:08:08 +00:00
|
|
|
static int32_t doProjectOperation(SOperatorInfo* pOperator, SSDataBlock** pResBlock);
|
|
|
|
|
static int32_t doApplyIndefinitFunction(SOperatorInfo* pOperator, SSDataBlock** pResBlock);
|
2026-03-06 06:45:30 +00:00
|
|
|
int32_t projectApplyOperator(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBlock* pSrcBlock, int32_t outputSlotId, int32_t* numOfRows, bool createNewColModel, const void* pExtraParams);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2026-03-29 01:38:08 +00:00
|
|
|
static bool hasLagLeadFunc(const SExprSupp* pSup) {
|
|
|
|
|
if (pSup == NULL || pSup->pCtx == NULL) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < pSup->numOfExprs; ++i) {
|
|
|
|
|
EFunctionType type = fmGetFuncTypeFromId(pSup->pCtx[i].functionId);
|
|
|
|
|
if (type == FUNCTION_TYPE_LAG || type == FUNCTION_TYPE_LEAD) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-25 02:55:28 +00:00
|
|
|
static void destroyProjectOperatorInfo(void* param) {
|
2022-07-25 06:15:49 +00:00
|
|
|
if (NULL == param) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SProjectOperatorInfo* pInfo = (SProjectOperatorInfo*)param;
|
|
|
|
|
cleanupBasicInfo(&pInfo->binfo);
|
|
|
|
|
cleanupAggSup(&pInfo->aggSup);
|
|
|
|
|
taosArrayDestroy(pInfo->pPseudoColInfo);
|
|
|
|
|
|
|
|
|
|
blockDataDestroy(pInfo->pFinalRes);
|
|
|
|
|
taosMemoryFreeClear(param);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-25 02:55:28 +00:00
|
|
|
static void destroyIndefinitOperatorInfo(void* param) {
|
2022-07-25 06:15:49 +00:00
|
|
|
SIndefOperatorInfo* pInfo = (SIndefOperatorInfo*)param;
|
2022-08-25 02:55:28 +00:00
|
|
|
if (pInfo == NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2022-08-25 02:55:28 +00:00
|
|
|
cleanupBasicInfo(&pInfo->binfo);
|
2022-07-25 06:15:49 +00:00
|
|
|
taosArrayDestroy(pInfo->pPseudoColInfo);
|
|
|
|
|
cleanupAggSup(&pInfo->aggSup);
|
|
|
|
|
cleanupExprSupp(&pInfo->scalarSup);
|
|
|
|
|
|
|
|
|
|
taosMemoryFreeClear(param);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 01:52:04 +00:00
|
|
|
static void cleanupProcessByRowIter(SqlFunctionCtx* pCtx) {
|
|
|
|
|
SFuncInputRowIter* pIter = &pCtx->rowIter;
|
|
|
|
|
|
|
|
|
|
if (pIter->pPrevRowBlock != NULL) {
|
|
|
|
|
blockDataDestroy(pIter->pPrevRowBlock);
|
|
|
|
|
}
|
|
|
|
|
taosMemoryFreeClear(pIter->pPrevData);
|
|
|
|
|
taosMemoryFreeClear(pIter->pPrevPk);
|
|
|
|
|
memset(pIter, 0, sizeof(*pIter));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t resetProcessByRowCtx(SqlFunctionCtx* pCtx) {
|
|
|
|
|
SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
|
|
|
|
|
char* pOutput = pCtx->pOutput;
|
|
|
|
|
|
|
|
|
|
if (pResInfo->initialized && pCtx->fpSet.cleanup != NULL) {
|
|
|
|
|
pCtx->fpSet.cleanup(pCtx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cleanupProcessByRowIter(pCtx);
|
|
|
|
|
pResInfo->initialized = false;
|
|
|
|
|
pResInfo->numOfRes = 0;
|
|
|
|
|
pCtx->bInputFinished = false;
|
|
|
|
|
|
|
|
|
|
pCtx->pOutput = NULL;
|
|
|
|
|
int32_t code = pCtx->fpSet.init(pCtx, pResInfo);
|
|
|
|
|
pCtx->pOutput = pOutput;
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool allProcessByRowCtxSameFuncId(SArray* pProcessByRowFunctionCtx) {
|
|
|
|
|
if (pProcessByRowFunctionCtx == NULL || taosArrayGetSize(pProcessByRowFunctionCtx) <= 1) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SqlFunctionCtx** ppFirstCtx = taosArrayGet(pProcessByRowFunctionCtx, 0);
|
|
|
|
|
if (ppFirstCtx == NULL || *ppFirstCtx == NULL) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t funcId = (*ppFirstCtx)->functionId;
|
|
|
|
|
for (int32_t i = 1; i < taosArrayGetSize(pProcessByRowFunctionCtx); ++i) {
|
|
|
|
|
SqlFunctionCtx** ppCtx = taosArrayGet(pProcessByRowFunctionCtx, i);
|
|
|
|
|
if (ppCtx == NULL || *ppCtx == NULL || (*ppCtx)->functionId != funcId) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t processByRowInExternalWindows(SArray* pGroupedCtxArray, SSDataBlock* pSrcBlock,
|
|
|
|
|
SStreamRuntimeFuncInfo* pStreamInfo) {
|
|
|
|
|
int32_t code = TSDB_CODE_SUCCESS;
|
|
|
|
|
int32_t lino = 0;
|
|
|
|
|
|
|
|
|
|
int32_t ctxNum = taosArrayGetSize(pGroupedCtxArray);
|
|
|
|
|
int32_t idxNum = taosArrayGetSize(pStreamInfo->pStreamBlkWinIdx);
|
|
|
|
|
int32_t totalRows = 0;
|
|
|
|
|
SArray* pInputWinIdx = NULL;
|
|
|
|
|
int32_t* pStartRows = NULL;
|
|
|
|
|
int64_t* pNumRows = NULL;
|
|
|
|
|
int32_t* pOffsets = NULL;
|
|
|
|
|
|
|
|
|
|
pInputWinIdx = taosArrayInit(idxNum, sizeof(int64_t));
|
|
|
|
|
TSDB_CHECK_NULL(pInputWinIdx, code, lino, _exit, terrno);
|
|
|
|
|
TSDB_CHECK_NULL(taosArrayAddBatch(pInputWinIdx, TARRAY_DATA(pStreamInfo->pStreamBlkWinIdx), idxNum), code, lino,
|
|
|
|
|
_exit, terrno);
|
|
|
|
|
|
|
|
|
|
pStartRows = taosMemoryCalloc(ctxNum, sizeof(int32_t));
|
|
|
|
|
TSDB_CHECK_NULL(pStartRows, code, lino, _exit, terrno);
|
|
|
|
|
pNumRows = taosMemoryCalloc(ctxNum, sizeof(int64_t));
|
|
|
|
|
TSDB_CHECK_NULL(pNumRows, code, lino, _exit, terrno);
|
|
|
|
|
pOffsets = taosMemoryCalloc(ctxNum, sizeof(int32_t));
|
|
|
|
|
TSDB_CHECK_NULL(pOffsets, code, lino, _exit, terrno);
|
|
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < ctxNum; ++i) {
|
|
|
|
|
SqlFunctionCtx** ppCtx = taosArrayGet(pGroupedCtxArray, i);
|
|
|
|
|
TSDB_CHECK_NULL(ppCtx, code, lino, _exit, terrno);
|
|
|
|
|
TSDB_CHECK_NULL(*ppCtx, code, lino, _exit, terrno);
|
|
|
|
|
pStartRows[i] = (*ppCtx)->input.startRowIndex;
|
|
|
|
|
pNumRows[i] = (*ppCtx)->input.numOfRows;
|
|
|
|
|
pOffsets[i] = (*ppCtx)->offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
taosArrayClear(pStreamInfo->pStreamBlkWinIdx);
|
|
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < idxNum; ++i) {
|
|
|
|
|
int64_t* pCurr = taosArrayGet(pInputWinIdx, i);
|
|
|
|
|
int32_t* pCurrPair = (int32_t*)pCurr;
|
|
|
|
|
int32_t winIdx = pCurrPair[0];
|
|
|
|
|
int32_t rowStart = pCurrPair[1];
|
|
|
|
|
int32_t rowEnd = pSrcBlock->info.rows;
|
|
|
|
|
|
|
|
|
|
if (i + 1 < idxNum) {
|
|
|
|
|
int64_t* pNext = taosArrayGet(pInputWinIdx, i + 1);
|
|
|
|
|
rowEnd = ((int32_t*)pNext)[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rowEnd <= rowStart) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int32_t j = 0; j < ctxNum; ++j) {
|
|
|
|
|
SqlFunctionCtx** ppCtx = taosArrayGet(pGroupedCtxArray, j);
|
|
|
|
|
SqlFunctionCtx* pCtx = *ppCtx;
|
|
|
|
|
|
|
|
|
|
pCtx->input.startRowIndex = rowStart;
|
|
|
|
|
pCtx->input.numOfRows = rowEnd - rowStart;
|
|
|
|
|
pCtx->offset = pOffsets[j] + totalRows;
|
|
|
|
|
TAOS_CHECK_EXIT(resetProcessByRowCtx(pCtx));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SqlFunctionCtx** ppFirstCtx = taosArrayGet(pGroupedCtxArray, 0);
|
|
|
|
|
TAOS_CHECK_EXIT((*ppFirstCtx)->fpSet.processFuncByRow(pGroupedCtxArray));
|
|
|
|
|
|
|
|
|
|
int32_t winRows = (*ppFirstCtx)->resultInfo->numOfRes;
|
|
|
|
|
if (winRows > 0) {
|
|
|
|
|
int64_t val = 0;
|
|
|
|
|
int32_t* pOutPair = (int32_t*)&val;
|
|
|
|
|
pOutPair[0] = winIdx;
|
|
|
|
|
pOutPair[1] = totalRows;
|
|
|
|
|
TSDB_CHECK_NULL(taosArrayPush(pStreamInfo->pStreamBlkWinIdx, &val), code, lino, _exit, terrno);
|
|
|
|
|
totalRows += winRows;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < ctxNum; ++i) {
|
|
|
|
|
SqlFunctionCtx** ppCtx = taosArrayGet(pGroupedCtxArray, i);
|
|
|
|
|
SqlFunctionCtx* pCtx = *ppCtx;
|
|
|
|
|
pCtx->input.startRowIndex = pStartRows[i];
|
|
|
|
|
pCtx->input.numOfRows = pNumRows[i];
|
|
|
|
|
pCtx->offset = pOffsets[i];
|
|
|
|
|
pCtx->resultInfo->numOfRes = totalRows;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
|
if (pInputWinIdx != NULL) {
|
|
|
|
|
taosArrayDestroy(pInputWinIdx);
|
|
|
|
|
}
|
|
|
|
|
taosMemoryFreeClear(pStartRows);
|
|
|
|
|
taosMemoryFreeClear(pNumRows);
|
|
|
|
|
taosMemoryFreeClear(pOffsets);
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t assignPlaceHolderInExternalWindows(SColumnInfoData* pResColData, int64_t offset, int64_t rows,
|
|
|
|
|
int16_t funcId, SStreamRuntimeFuncInfo* pInfo, SNode* pParamNode) {
|
|
|
|
|
int32_t code = TSDB_CODE_SUCCESS;
|
|
|
|
|
int32_t lino = 0;
|
|
|
|
|
|
|
|
|
|
int32_t originIdx = pInfo->curIdx;
|
|
|
|
|
int32_t idxNum = taosArrayGetSize(pInfo->pStreamBlkWinIdx);
|
|
|
|
|
SArray* pInputWinIdx = taosArrayInit(idxNum, sizeof(int64_t));
|
|
|
|
|
TSDB_CHECK_NULL(pInputWinIdx, code, lino, _exit, terrno);
|
|
|
|
|
TSDB_CHECK_NULL(taosArrayAddBatch(pInputWinIdx, TARRAY_DATA(pInfo->pStreamBlkWinIdx), idxNum), code, lino, _exit,
|
|
|
|
|
terrno);
|
|
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < idxNum; ++i) {
|
|
|
|
|
int64_t* pCurr = taosArrayGet(pInputWinIdx, i);
|
|
|
|
|
int32_t* pCurrPair = (int32_t*)pCurr;
|
|
|
|
|
int32_t winIdx = pCurrPair[0];
|
|
|
|
|
int32_t rowStart = pCurrPair[1];
|
|
|
|
|
int32_t rowEnd = rows;
|
|
|
|
|
|
|
|
|
|
if (i + 1 < idxNum) {
|
|
|
|
|
int64_t* pNext = taosArrayGet(pInputWinIdx, i + 1);
|
|
|
|
|
rowEnd = ((int32_t*)pNext)[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rowEnd <= rowStart) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pInfo->curIdx = winIdx;
|
|
|
|
|
TAOS_CHECK_EXIT(scalarAssignPlaceHolderRes(pResColData, offset + rowStart, rowEnd - rowStart, funcId, pInfo,
|
|
|
|
|
pParamNode));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
|
pInfo->curIdx = originIdx;
|
|
|
|
|
taosArrayDestroy(pInputWinIdx);
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-15 06:13:17 +00:00
|
|
|
void streamOperatorReleaseState(SOperatorInfo* pOperator) {
|
|
|
|
|
SOperatorInfo* downstream = pOperator->pDownstream[0];
|
|
|
|
|
if (downstream->fpSet.releaseStreamStateFn) {
|
|
|
|
|
downstream->fpSet.releaseStreamStateFn(downstream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void streamOperatorReloadState(SOperatorInfo* pOperator) {
|
|
|
|
|
SOperatorInfo* downstream = pOperator->pDownstream[0];
|
|
|
|
|
if (downstream->fpSet.reloadStreamStateFn) {
|
|
|
|
|
downstream->fpSet.reloadStreamStateFn(downstream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 06:42:16 +00:00
|
|
|
static int32_t resetProjectOperState(SOperatorInfo* pOper) {
|
|
|
|
|
SProjectOperatorInfo* pProject = pOper->info;
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOper->pTaskInfo;
|
|
|
|
|
pOper->status = OP_NOT_OPENED;
|
|
|
|
|
|
|
|
|
|
resetBasicOperatorState(&pProject->binfo);
|
|
|
|
|
SProjectPhysiNode* pPhynode = (SProjectPhysiNode*)pOper->pPhyNode;
|
|
|
|
|
|
|
|
|
|
pProject->limitInfo = (SLimitInfo){0};
|
|
|
|
|
initLimitInfo(pPhynode->node.pLimit, pPhynode->node.pSlimit, &pProject->limitInfo);
|
|
|
|
|
|
|
|
|
|
blockDataCleanup(pProject->pFinalRes);
|
|
|
|
|
|
|
|
|
|
int32_t code = resetAggSup(&pOper->exprSupp, &pProject->aggSup, pTaskInfo, pPhynode->pProjections, NULL,
|
2026-03-12 01:11:00 +00:00
|
|
|
sizeof(int64_t) * 2 + POINTER_BYTES, pTaskInfo->id.str, NULL,
|
2025-07-16 06:42:16 +00:00
|
|
|
&pTaskInfo->storageAPI.functionStore);
|
|
|
|
|
if (code == 0){
|
|
|
|
|
code = setFunctionResultOutput(pOper, &pProject->binfo, &pProject->aggSup, MAIN_SCAN, pOper->exprSupp.numOfExprs);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
int32_t createProjectOperatorInfo(SOperatorInfo* downstream, SProjectPhysiNode* pProjPhyNode, SExecTaskInfo* pTaskInfo,
|
|
|
|
|
SOperatorInfo** pOptrInfo) {
|
2024-09-06 07:44:25 +00:00
|
|
|
QRY_PARAM_CHECK(pOptrInfo);
|
2024-07-24 09:08:08 +00:00
|
|
|
|
|
|
|
|
int32_t code = TSDB_CODE_SUCCESS;
|
2022-07-25 06:15:49 +00:00
|
|
|
SProjectOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SProjectOperatorInfo));
|
|
|
|
|
SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
|
|
|
|
|
if (pInfo == NULL || pOperator == NULL) {
|
2024-09-03 03:01:02 +00:00
|
|
|
code = terrno;
|
2022-07-25 06:15:49 +00:00
|
|
|
goto _error;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 06:42:16 +00:00
|
|
|
pOperator->pPhyNode = pProjPhyNode;
|
2024-07-08 08:02:43 +00:00
|
|
|
pOperator->exprSupp.hasWindowOrGroup = false;
|
2022-08-03 12:45:21 +00:00
|
|
|
pOperator->pTaskInfo = pTaskInfo;
|
2026-03-20 02:08:49 +00:00
|
|
|
initOperatorCostInfo(pOperator);
|
2022-08-03 12:45:21 +00:00
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
int32_t lino = 0;
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2022-11-27 16:27:49 +00:00
|
|
|
SSDataBlock* pResBlock = createDataBlockFromDescNode(pProjPhyNode->node.pOutputDataBlockDesc);
|
2024-08-05 03:57:18 +00:00
|
|
|
TSDB_CHECK_NULL(pResBlock, code, lino, _error, terrno);
|
|
|
|
|
|
2022-07-25 06:15:49 +00:00
|
|
|
initLimitInfo(pProjPhyNode->node.pLimit, pProjPhyNode->node.pSlimit, &pInfo->limitInfo);
|
|
|
|
|
|
|
|
|
|
pInfo->binfo.pRes = pResBlock;
|
2024-07-27 10:55:34 +00:00
|
|
|
pInfo->pFinalRes = NULL;
|
|
|
|
|
|
|
|
|
|
code = createOneDataBlock(pResBlock, false, &pInfo->pFinalRes);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
|
|
|
|
|
2023-06-16 02:26:09 +00:00
|
|
|
pInfo->binfo.inputTsOrder = pProjPhyNode->node.inputTsOrder;
|
|
|
|
|
pInfo->binfo.outputTsOrder = pProjPhyNode->node.outputTsOrder;
|
2024-01-16 00:13:47 +00:00
|
|
|
pInfo->inputIgnoreGroup = pProjPhyNode->inputIgnoreGroup;
|
2024-03-12 03:45:54 +00:00
|
|
|
pInfo->outputIgnoreGroup = pProjPhyNode->ignoreGroupId;
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2025-07-16 06:42:16 +00:00
|
|
|
if (pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {
|
2023-01-20 14:50:35 +00:00
|
|
|
pInfo->mergeDataBlocks = false;
|
|
|
|
|
} else {
|
|
|
|
|
if (!pProjPhyNode->ignoreGroupId) {
|
|
|
|
|
pInfo->mergeDataBlocks = false;
|
|
|
|
|
} else {
|
|
|
|
|
pInfo->mergeDataBlocks = pProjPhyNode->mergeDataBlock;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-25 07:23:13 +00:00
|
|
|
|
2022-07-25 06:15:49 +00:00
|
|
|
int32_t numOfRows = 4096;
|
|
|
|
|
size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
|
|
|
|
|
|
|
|
|
|
// Make sure the size of SSDataBlock will never exceed the size of 2MB.
|
|
|
|
|
int32_t TWOMB = 2 * 1024 * 1024;
|
|
|
|
|
if (numOfRows * pResBlock->info.rowSize > TWOMB) {
|
|
|
|
|
numOfRows = TWOMB / pResBlock->info.rowSize;
|
|
|
|
|
}
|
2022-08-23 09:28:08 +00:00
|
|
|
|
2022-07-25 06:15:49 +00:00
|
|
|
initResultSizeInfo(&pOperator->resultInfo, numOfRows);
|
2024-08-15 09:03:32 +00:00
|
|
|
|
|
|
|
|
int32_t numOfCols = 0;
|
|
|
|
|
SExprInfo* pExprInfo = NULL;
|
|
|
|
|
code = createExprInfo(pProjPhyNode->pProjections, NULL, &pExprInfo, &numOfCols);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
2022-12-29 03:35:46 +00:00
|
|
|
code = initAggSup(&pOperator->exprSupp, &pInfo->aggSup, pExprInfo, numOfCols, keyBufSize, pTaskInfo->id.str,
|
2026-03-12 01:11:00 +00:00
|
|
|
NULL, &pTaskInfo->storageAPI.functionStore);
|
2024-07-24 09:08:08 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
initBasicInfo(&pInfo->binfo, pResBlock);
|
2024-07-24 09:08:08 +00:00
|
|
|
code = setFunctionResultOutput(pOperator, &pInfo->binfo, &pInfo->aggSup, MAIN_SCAN, numOfCols);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2025-07-16 06:42:16 +00:00
|
|
|
code = filterInitFromNode((SNode*)pProjPhyNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
|
|
|
|
|
pTaskInfo->pStreamRuntimeInfo);
|
2024-07-24 09:08:08 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
2022-11-04 14:13:40 +00:00
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
code = setRowTsColumnOutputInfo(pOperator->exprSupp.pCtx, numOfCols, &pInfo->pPseudoColInfo);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2022-12-07 09:54:09 +00:00
|
|
|
setOperatorInfo(pOperator, "ProjectOperator", QUERY_NODE_PHYSICAL_PLAN_PROJECT, false, OP_NOT_OPENED, pInfo,
|
|
|
|
|
pTaskInfo);
|
2024-08-27 09:04:44 +00:00
|
|
|
pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, doProjectOperation, NULL, destroyProjectOperatorInfo,
|
2023-07-05 03:07:08 +00:00
|
|
|
optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
|
2024-07-24 09:08:08 +00:00
|
|
|
setOperatorStreamStateFn(pOperator, streamOperatorReleaseState, streamOperatorReloadState);
|
2025-07-16 06:42:16 +00:00
|
|
|
setOperatorResetStateFn(pOperator, resetProjectOperState);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2023-10-25 02:47:01 +00:00
|
|
|
if (NULL != downstream) {
|
|
|
|
|
code = appendDownstream(pOperator, &downstream, 1);
|
|
|
|
|
if (code != TSDB_CODE_SUCCESS) {
|
|
|
|
|
goto _error;
|
|
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
*pOptrInfo = pOperator;
|
2024-08-29 11:43:59 +00:00
|
|
|
return TSDB_CODE_SUCCESS;
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2022-08-03 12:45:21 +00:00
|
|
|
_error:
|
2024-08-08 08:24:18 +00:00
|
|
|
if (pInfo != NULL) destroyProjectOperatorInfo(pInfo);
|
2024-08-29 11:43:59 +00:00
|
|
|
destroyOperatorAndDownstreams(pOperator, &downstream, 1);
|
2022-08-23 09:28:08 +00:00
|
|
|
pTaskInfo->code = code;
|
2024-07-24 09:08:08 +00:00
|
|
|
return code;
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t discardGroupDataBlock(SSDataBlock* pBlock, SLimitInfo* pLimitInfo) {
|
|
|
|
|
if (pLimitInfo->remainGroupOffset > 0) {
|
|
|
|
|
// it is the first group
|
2022-11-28 04:32:40 +00:00
|
|
|
if (pLimitInfo->currentGroupId == 0 || pLimitInfo->currentGroupId == pBlock->info.id.groupId) {
|
|
|
|
|
pLimitInfo->currentGroupId = pBlock->info.id.groupId;
|
2022-07-25 06:15:49 +00:00
|
|
|
return PROJECT_RETRIEVE_CONTINUE;
|
2022-11-28 04:32:40 +00:00
|
|
|
} else if (pLimitInfo->currentGroupId != pBlock->info.id.groupId) {
|
2022-07-25 06:15:49 +00:00
|
|
|
// now it is the data from a new group
|
|
|
|
|
pLimitInfo->remainGroupOffset -= 1;
|
2022-11-28 04:32:40 +00:00
|
|
|
pLimitInfo->currentGroupId = pBlock->info.id.groupId;
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
// ignore data block in current group
|
|
|
|
|
if (pLimitInfo->remainGroupOffset > 0) {
|
|
|
|
|
return PROJECT_RETRIEVE_CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 07:18:54 +00:00
|
|
|
pLimitInfo->currentGroupId = 0;
|
|
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PROJECT_RETRIEVE_DONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t setInfoForNewGroup(SSDataBlock* pBlock, SLimitInfo* pLimitInfo, SOperatorInfo* pOperator) {
|
|
|
|
|
// remainGroupOffset == 0
|
|
|
|
|
// here check for a new group data, we need to handle the data of the previous group.
|
2024-08-20 06:33:38 +00:00
|
|
|
if (!(pLimitInfo->remainGroupOffset == 0 || pLimitInfo->remainGroupOffset == -1)) {
|
|
|
|
|
qError("project failed at: %s:%d", __func__, __LINE__);
|
|
|
|
|
return TSDB_CODE_INVALID_PARA;
|
|
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2023-03-22 09:14:45 +00:00
|
|
|
bool newGroup = false;
|
|
|
|
|
if (0 == pBlock->info.id.groupId) {
|
|
|
|
|
pLimitInfo->numOfOutputGroups = 1;
|
|
|
|
|
} else if (pLimitInfo->currentGroupId != pBlock->info.id.groupId) {
|
2023-03-22 07:18:54 +00:00
|
|
|
pLimitInfo->currentGroupId = pBlock->info.id.groupId;
|
2022-07-25 06:15:49 +00:00
|
|
|
pLimitInfo->numOfOutputGroups += 1;
|
2023-03-22 09:14:45 +00:00
|
|
|
newGroup = true;
|
2023-03-22 07:18:54 +00:00
|
|
|
} else {
|
|
|
|
|
return PROJECT_RETRIEVE_CONTINUE;
|
|
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2023-03-22 07:18:54 +00:00
|
|
|
if ((pLimitInfo->slimit.limit >= 0) && (pLimitInfo->slimit.limit < pLimitInfo->numOfOutputGroups)) {
|
|
|
|
|
setOperatorCompleted(pOperator);
|
|
|
|
|
return PROJECT_RETRIEVE_DONE;
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-22 07:18:54 +00:00
|
|
|
// reset the value for a new group data
|
|
|
|
|
// existing rows that belongs to previous group.
|
2023-03-22 09:14:45 +00:00
|
|
|
if (newGroup) {
|
|
|
|
|
resetLimitInfoForNextGroup(pLimitInfo);
|
|
|
|
|
}
|
2023-07-06 07:05:49 +00:00
|
|
|
|
2023-03-22 07:18:54 +00:00
|
|
|
return PROJECT_RETRIEVE_CONTINUE;
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 05:40:51 +00:00
|
|
|
// todo refactor
|
2022-08-03 12:45:21 +00:00
|
|
|
static int32_t doIngroupLimitOffset(SLimitInfo* pLimitInfo, uint64_t groupId, SSDataBlock* pBlock,
|
|
|
|
|
SOperatorInfo* pOperator) {
|
2022-07-25 06:15:49 +00:00
|
|
|
// set current group id
|
|
|
|
|
pLimitInfo->currentGroupId = groupId;
|
2023-01-20 10:08:34 +00:00
|
|
|
bool limitReached = applyLimitOffset(pLimitInfo, pBlock, pOperator->pTaskInfo);
|
2024-04-16 10:47:03 +00:00
|
|
|
if (pBlock->info.rows == 0 && 0 != pLimitInfo->limit.limit) {
|
2022-07-25 06:15:49 +00:00
|
|
|
return PROJECT_RETRIEVE_CONTINUE;
|
2023-01-20 10:08:34 +00:00
|
|
|
} else {
|
2023-03-22 07:18:54 +00:00
|
|
|
if (limitReached && (pLimitInfo->slimit.limit >= 0 && pLimitInfo->slimit.limit <= pLimitInfo->numOfOutputGroups)) {
|
2022-11-09 11:14:27 +00:00
|
|
|
setOperatorCompleted(pOperator);
|
2023-06-06 11:28:55 +00:00
|
|
|
} else if (limitReached && groupId == 0) {
|
|
|
|
|
setOperatorCompleted(pOperator);
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PROJECT_RETRIEVE_DONE;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
int32_t doProjectOperation(SOperatorInfo* pOperator, SSDataBlock** pResBlock) {
|
2024-09-06 07:44:25 +00:00
|
|
|
QRY_PARAM_CHECK(pResBlock);
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2022-07-25 06:15:49 +00:00
|
|
|
SProjectOperatorInfo* pProjectInfo = pOperator->info;
|
|
|
|
|
SOptrBasicInfo* pInfo = &pProjectInfo->binfo;
|
2024-08-05 03:57:18 +00:00
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
|
|
|
|
SSDataBlock* pRes = pInfo->pRes;
|
|
|
|
|
SSDataBlock* pFinalRes = pProjectInfo->pFinalRes;
|
|
|
|
|
int32_t code = 0;
|
2024-09-23 11:54:33 +00:00
|
|
|
int32_t lino = 0;
|
2024-08-05 03:57:18 +00:00
|
|
|
int32_t order = pInfo->inputTsOrder;
|
|
|
|
|
int32_t scanFlag = 0;
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
blockDataCleanup(pFinalRes);
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
2026-04-21 01:52:04 +00:00
|
|
|
bool withExternalWindow = pTaskInfo->pStreamRuntimeInfo != NULL &&
|
|
|
|
|
pTaskInfo->pStreamRuntimeInfo->funcInfo.withExternalWindow;
|
2022-09-28 06:17:26 +00:00
|
|
|
|
2022-07-25 06:15:49 +00:00
|
|
|
if (pOperator->status == OP_EXEC_DONE) {
|
2024-07-24 09:08:08 +00:00
|
|
|
return code;
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-10-25 02:47:01 +00:00
|
|
|
SOperatorInfo* downstream = pOperator->numOfDownstream > 0 ? pOperator->pDownstream[0] : NULL;
|
2022-08-03 12:45:21 +00:00
|
|
|
SLimitInfo* pLimitInfo = &pProjectInfo->limitInfo;
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
if (downstream == NULL) {
|
2023-07-06 07:05:49 +00:00
|
|
|
code = doGenerateSourceData(pOperator);
|
2024-09-23 11:54:33 +00:00
|
|
|
QUERY_CHECK_CODE(code, lino, _end);
|
2023-07-06 07:05:49 +00:00
|
|
|
|
2024-03-12 03:45:54 +00:00
|
|
|
if (pProjectInfo->outputIgnoreGroup) {
|
|
|
|
|
pRes->info.id.groupId = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
*pResBlock = (pRes->info.rows > 0)? pRes:NULL;
|
|
|
|
|
return code;
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
while (1) {
|
|
|
|
|
blockDataCleanup(pRes);
|
|
|
|
|
|
|
|
|
|
// The downstream exec may change the value of the newgroup, so use a local variable instead.
|
2023-07-05 03:07:08 +00:00
|
|
|
SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
|
2023-04-03 11:54:52 +00:00
|
|
|
if (pBlock == NULL) {
|
2023-03-31 08:20:21 +00:00
|
|
|
qDebug("set op close, exec %d, status %d rows %" PRId64 , pTaskInfo->execModel, pOperator->status, pFinalRes->info.rows);
|
2022-11-09 11:14:27 +00:00
|
|
|
setOperatorCompleted(pOperator);
|
2022-07-25 06:15:49 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2023-04-03 09:06:51 +00:00
|
|
|
// if (pTaskInfo->execModel == OPTR_EXEC_MODEL_QUEUE) {
|
|
|
|
|
// qDebug("set status recv");
|
|
|
|
|
// pOperator->status = OP_EXEC_RECV;
|
|
|
|
|
// }
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2024-01-16 00:13:47 +00:00
|
|
|
if (pProjectInfo->inputIgnoreGroup) {
|
2024-01-15 03:58:29 +00:00
|
|
|
pBlock->info.id.groupId = 0;
|
|
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
int32_t status = discardGroupDataBlock(pBlock, pLimitInfo);
|
|
|
|
|
if (status == PROJECT_RETRIEVE_CONTINUE) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 10:43:35 +00:00
|
|
|
(void) setInfoForNewGroup(pBlock, pLimitInfo, pOperator);
|
2022-07-25 06:15:49 +00:00
|
|
|
if (pOperator->status == OP_EXEC_DONE) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 02:26:09 +00:00
|
|
|
if (pProjectInfo->mergeDataBlocks) {
|
|
|
|
|
pFinalRes->info.scanFlag = scanFlag = pBlock->info.scanFlag;
|
|
|
|
|
} else {
|
|
|
|
|
pRes->info.scanFlag = scanFlag = pBlock->info.scanFlag;
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
code = setInputDataBlock(pSup, pBlock, order, scanFlag, false);
|
2024-09-23 11:54:33 +00:00
|
|
|
QUERY_CHECK_CODE(code, lino, _end);
|
2024-07-24 09:08:08 +00:00
|
|
|
|
|
|
|
|
code = blockDataEnsureCapacity(pInfo->pRes, pInfo->pRes->info.rows + pBlock->info.rows);
|
2024-09-23 11:54:33 +00:00
|
|
|
QUERY_CHECK_CODE(code, lino, _end);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
code = projectApplyFunctions(pSup->pExprInfo, pInfo->pRes, pBlock, pSup->pCtx, pSup->numOfExprs,
|
2025-07-16 06:42:16 +00:00
|
|
|
pProjectInfo->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo));
|
2024-09-23 11:54:33 +00:00
|
|
|
QUERY_CHECK_CODE(code, lino, _end);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2022-11-28 04:32:40 +00:00
|
|
|
status = doIngroupLimitOffset(pLimitInfo, pBlock->info.id.groupId, pInfo->pRes, pOperator);
|
2022-07-25 06:15:49 +00:00
|
|
|
if (status == PROJECT_RETRIEVE_CONTINUE) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 01:52:04 +00:00
|
|
|
if (pProjectInfo->mergeDataBlocks) {
|
|
|
|
|
if (pRes->info.rows > 0) {
|
|
|
|
|
pFinalRes->info.id.groupId = 0; // clear groupId
|
|
|
|
|
pFinalRes->info.version = pRes->info.version;
|
|
|
|
|
// keep baseGId from current upstream block; already set above for this merge round
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2026-04-21 01:52:04 +00:00
|
|
|
// continue merge data, ignore the group id
|
|
|
|
|
code = blockDataMerge(pFinalRes, pRes);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _end);
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2026-04-21 01:52:04 +00:00
|
|
|
if (!withExternalWindow && pFinalRes->info.rows + pRes->info.rows <= pOperator->resultInfo.threshold &&
|
|
|
|
|
(pOperator->status != OP_EXEC_DONE)) {
|
2022-07-25 06:15:49 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// do apply filter
|
2025-09-25 07:48:14 +00:00
|
|
|
code = doFilter(pFinalRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
|
2024-09-23 11:54:33 +00:00
|
|
|
QUERY_CHECK_CODE(code, lino, _end);
|
2022-07-26 11:07:43 +00:00
|
|
|
|
|
|
|
|
// when apply the limit/offset for each group, pRes->info.rows may be 0, due to limit constraint.
|
|
|
|
|
if (pFinalRes->info.rows > 0 || (pOperator->status == OP_EXEC_DONE)) {
|
2023-03-29 02:46:56 +00:00
|
|
|
qDebug("project return %" PRId64 " rows, status %d", pFinalRes->info.rows, pOperator->status);
|
2022-07-25 06:15:49 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// do apply filter
|
|
|
|
|
if (pRes->info.rows > 0) {
|
2025-09-25 07:48:14 +00:00
|
|
|
code = doFilter(pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
|
2024-09-23 11:54:33 +00:00
|
|
|
QUERY_CHECK_CODE(code, lino, _end);
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2022-07-25 06:15:49 +00:00
|
|
|
if (pRes->info.rows == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// no results generated
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SSDataBlock* p = pProjectInfo->mergeDataBlocks ? pFinalRes : pRes;
|
2022-12-22 12:02:43 +00:00
|
|
|
p->info.dataLoad = 1;
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2024-03-12 03:45:54 +00:00
|
|
|
if (pProjectInfo->outputIgnoreGroup) {
|
|
|
|
|
p->info.id.groupId = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 08:43:36 +00:00
|
|
|
if (pTaskInfo->execModel == OPTR_EXEC_MODEL_STREAM) {
|
2025-09-25 07:48:14 +00:00
|
|
|
printDataBlock(p, getStreamOpName(pOperator->operatorType), GET_TASKID(pTaskInfo), pTaskInfo->id.queryId);
|
2024-04-01 08:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
*pResBlock = (p->info.rows > 0)? p:NULL;
|
2024-09-23 11:54:33 +00:00
|
|
|
|
|
|
|
|
_end:
|
|
|
|
|
if (code != TSDB_CODE_SUCCESS) {
|
|
|
|
|
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
|
|
|
|
|
pTaskInfo->code = code;
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
|
|
|
|
}
|
2024-07-24 09:08:08 +00:00
|
|
|
return code;
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-16 06:42:16 +00:00
|
|
|
static int32_t resetIndefinitOutputOperState(SOperatorInfo* pOper) {
|
|
|
|
|
SIndefOperatorInfo* pInfo = pOper->info;
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOper->pTaskInfo;
|
|
|
|
|
SIndefRowsFuncPhysiNode* pPhynode = (SIndefRowsFuncPhysiNode*)pOper->pPhyNode;
|
|
|
|
|
pOper->status = OP_NOT_OPENED;
|
|
|
|
|
|
|
|
|
|
resetBasicOperatorState(&pInfo->binfo);
|
|
|
|
|
|
|
|
|
|
pInfo->groupId = 0;
|
|
|
|
|
pInfo->pNextGroupRes = NULL;
|
|
|
|
|
int32_t code = resetAggSup(&pOper->exprSupp, &pInfo->aggSup, pTaskInfo, pPhynode->pFuncs, NULL,
|
2026-03-12 01:11:00 +00:00
|
|
|
sizeof(int64_t) * 2 + POINTER_BYTES, pTaskInfo->id.str, NULL,
|
2025-07-16 06:42:16 +00:00
|
|
|
&pTaskInfo->storageAPI.functionStore);
|
|
|
|
|
if (code == 0){
|
|
|
|
|
code = setFunctionResultOutput(pOper, &pInfo->binfo, &pInfo->aggSup, MAIN_SCAN, pOper->exprSupp.numOfExprs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (code == 0) {
|
|
|
|
|
code = resetExprSupp(&pInfo->scalarSup, pTaskInfo, pPhynode->pExprs, NULL,
|
|
|
|
|
&pTaskInfo->storageAPI.functionStore);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
int32_t createIndefinitOutputOperatorInfo(SOperatorInfo* downstream, SPhysiNode* pNode,
|
|
|
|
|
SExecTaskInfo* pTaskInfo, SOperatorInfo** pOptrInfo) {
|
2024-09-06 07:44:25 +00:00
|
|
|
QRY_PARAM_CHECK(pOptrInfo);
|
2024-07-24 09:08:08 +00:00
|
|
|
int32_t code = 0;
|
|
|
|
|
int32_t lino = 0;
|
|
|
|
|
int32_t numOfRows = 4096;
|
|
|
|
|
size_t keyBufSize = sizeof(int64_t) + sizeof(int64_t) + POINTER_BYTES;
|
|
|
|
|
|
2022-07-25 06:15:49 +00:00
|
|
|
SIndefOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SIndefOperatorInfo));
|
|
|
|
|
SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
|
|
|
|
|
if (pInfo == NULL || pOperator == NULL) {
|
2024-09-03 03:01:02 +00:00
|
|
|
code = terrno;
|
2022-07-25 06:15:49 +00:00
|
|
|
goto _error;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 06:42:16 +00:00
|
|
|
pOperator->pPhyNode = pNode;
|
2022-08-03 12:45:21 +00:00
|
|
|
pOperator->pTaskInfo = pTaskInfo;
|
2026-03-20 02:08:49 +00:00
|
|
|
initOperatorCostInfo(pOperator);
|
2022-08-03 12:45:21 +00:00
|
|
|
|
2022-07-25 06:15:49 +00:00
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
2024-07-08 08:02:43 +00:00
|
|
|
pSup->hasWindowOrGroup = false;
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
SIndefRowsFuncPhysiNode* pPhyNode = (SIndefRowsFuncPhysiNode*)pNode;
|
|
|
|
|
|
|
|
|
|
if (pPhyNode->pExprs != NULL) {
|
|
|
|
|
int32_t num = 0;
|
2024-08-05 03:57:18 +00:00
|
|
|
SExprInfo* pSExpr = NULL;
|
|
|
|
|
code = createExprInfo(pPhyNode->pExprs, NULL, &pSExpr, &num);
|
|
|
|
|
QUERY_CHECK_CODE(code, lino, _error);
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
code = initExprSupp(&pInfo->scalarSup, pSExpr, num, &pTaskInfo->storageAPI.functionStore);
|
2022-07-25 06:15:49 +00:00
|
|
|
if (code != TSDB_CODE_SUCCESS) {
|
|
|
|
|
goto _error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-27 16:27:49 +00:00
|
|
|
SSDataBlock* pResBlock = createDataBlockFromDescNode(pPhyNode->node.pOutputDataBlockDesc);
|
2024-08-05 09:08:21 +00:00
|
|
|
TSDB_CHECK_NULL(pResBlock, code, lino, _error, terrno);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
// Make sure the size of SSDataBlock will never exceed the size of 2MB.
|
|
|
|
|
int32_t TWOMB = 2 * 1024 * 1024;
|
|
|
|
|
if (numOfRows * pResBlock->info.rowSize > TWOMB) {
|
|
|
|
|
numOfRows = TWOMB / pResBlock->info.rowSize;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 03:43:58 +00:00
|
|
|
initBasicInfo(&pInfo->binfo, pResBlock);
|
2022-07-25 06:15:49 +00:00
|
|
|
initResultSizeInfo(&pOperator->resultInfo, numOfRows);
|
2024-07-24 09:08:08 +00:00
|
|
|
code = blockDataEnsureCapacity(pResBlock, numOfRows);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2024-08-15 09:38:29 +00:00
|
|
|
int32_t numOfExpr = 0;
|
|
|
|
|
SExprInfo* pExprInfo = NULL;
|
|
|
|
|
code = createExprInfo(pPhyNode->pFuncs, NULL, &pExprInfo, &numOfExpr);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
code = initAggSup(pSup, &pInfo->aggSup, pExprInfo, numOfExpr, keyBufSize, pTaskInfo->id.str,
|
2026-03-12 01:11:00 +00:00
|
|
|
NULL, &pTaskInfo->storageAPI.functionStore);
|
2024-07-24 09:08:08 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
|
|
|
|
|
|
|
|
|
code = setFunctionResultOutput(pOperator, &pInfo->binfo, &pInfo->aggSup, MAIN_SCAN, numOfExpr);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2025-07-16 06:42:16 +00:00
|
|
|
code = filterInitFromNode((SNode*)pPhyNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0,
|
|
|
|
|
pTaskInfo->pStreamRuntimeInfo);
|
2024-07-24 09:08:08 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
pInfo->binfo.pRes = pResBlock;
|
2023-06-16 02:26:09 +00:00
|
|
|
pInfo->binfo.inputTsOrder = pNode->inputTsOrder;
|
|
|
|
|
pInfo->binfo.outputTsOrder = pNode->outputTsOrder;
|
2024-07-24 09:08:08 +00:00
|
|
|
code = setRowTsColumnOutputInfo(pSup->pCtx, numOfExpr, &pInfo->pPseudoColInfo);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _error);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2022-12-07 09:54:09 +00:00
|
|
|
setOperatorInfo(pOperator, "IndefinitOperator", QUERY_NODE_PHYSICAL_PLAN_INDEF_ROWS_FUNC, false, OP_NOT_OPENED, pInfo,
|
|
|
|
|
pTaskInfo);
|
2024-08-27 09:04:44 +00:00
|
|
|
pOperator->fpSet = createOperatorFpSet(optrDummyOpenFn, doApplyIndefinitFunction, NULL, destroyIndefinitOperatorInfo,
|
2023-07-05 03:07:08 +00:00
|
|
|
optrDefaultBufFn, NULL, optrDefaultGetNextExtFn, NULL);
|
2025-07-16 06:42:16 +00:00
|
|
|
|
|
|
|
|
setOperatorResetStateFn(pOperator, resetIndefinitOutputOperState);
|
2022-08-25 02:55:28 +00:00
|
|
|
code = appendDownstream(pOperator, &downstream, 1);
|
2022-07-25 06:15:49 +00:00
|
|
|
if (code != TSDB_CODE_SUCCESS) {
|
|
|
|
|
goto _error;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
*pOptrInfo = pOperator;
|
2024-08-29 11:43:59 +00:00
|
|
|
return TSDB_CODE_SUCCESS;
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2022-08-03 12:45:21 +00:00
|
|
|
_error:
|
2024-08-08 08:24:18 +00:00
|
|
|
if (pInfo != NULL) destroyIndefinitOperatorInfo(pInfo);
|
2024-08-29 11:43:59 +00:00
|
|
|
destroyOperatorAndDownstreams(pOperator, &downstream, 1);
|
2024-02-26 02:22:48 +00:00
|
|
|
pTaskInfo->code = code;
|
2024-07-24 09:08:08 +00:00
|
|
|
return code;
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void doHandleDataBlock(SOperatorInfo* pOperator, SSDataBlock* pBlock, SOperatorInfo* downstream,
|
|
|
|
|
SExecTaskInfo* pTaskInfo) {
|
|
|
|
|
SIndefOperatorInfo* pIndefInfo = pOperator->info;
|
|
|
|
|
SOptrBasicInfo* pInfo = &pIndefInfo->binfo;
|
|
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
|
|
|
|
|
2023-06-16 02:26:09 +00:00
|
|
|
int32_t order = pInfo->inputTsOrder;
|
|
|
|
|
int32_t scanFlag = pBlock->info.scanFlag;
|
|
|
|
|
int32_t code = TSDB_CODE_SUCCESS;
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
// there is an scalar expression that needs to be calculated before apply the group aggregation.
|
|
|
|
|
SExprSupp* pScalarSup = &pIndefInfo->scalarSup;
|
|
|
|
|
if (pScalarSup->pExprInfo != NULL) {
|
|
|
|
|
code = projectApplyFunctions(pScalarSup->pExprInfo, pBlock, pBlock, pScalarSup->pCtx, pScalarSup->numOfExprs,
|
2025-07-16 06:42:16 +00:00
|
|
|
pIndefInfo->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo));
|
2022-07-25 06:15:49 +00:00
|
|
|
if (code != TSDB_CODE_SUCCESS) {
|
2022-08-24 09:09:33 +00:00
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
code = setInputDataBlock(pSup, pBlock, order, scanFlag, false);
|
|
|
|
|
if (code) {
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
code = blockDataEnsureCapacity(pInfo->pRes, pInfo->pRes->info.rows + pBlock->info.rows);
|
|
|
|
|
if (code != TSDB_CODE_SUCCESS) {
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
|
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
code = projectApplyFunctions(pSup->pExprInfo, pInfo->pRes, pBlock, pSup->pCtx, pSup->numOfExprs,
|
2025-07-16 06:42:16 +00:00
|
|
|
pIndefInfo->pPseudoColInfo, GET_STM_RTINFO(pOperator->pTaskInfo));
|
2022-07-25 06:15:49 +00:00
|
|
|
if (code != TSDB_CODE_SUCCESS) {
|
2022-08-24 09:09:33 +00:00
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
int32_t doApplyIndefinitFunction(SOperatorInfo* pOperator, SSDataBlock** pResBlock) {
|
2024-09-06 07:44:25 +00:00
|
|
|
QRY_PARAM_CHECK(pResBlock);
|
2022-07-25 06:15:49 +00:00
|
|
|
SIndefOperatorInfo* pIndefInfo = pOperator->info;
|
|
|
|
|
SOptrBasicInfo* pInfo = &pIndefInfo->binfo;
|
|
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
2024-09-23 11:54:33 +00:00
|
|
|
int32_t code = TSDB_CODE_SUCCESS;
|
|
|
|
|
int32_t lino = 0;
|
2024-07-24 09:08:08 +00:00
|
|
|
SSDataBlock* pRes = pInfo->pRes;
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
blockDataCleanup(pRes);
|
|
|
|
|
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
if (pOperator->status == OP_EXEC_DONE) {
|
2024-09-23 11:54:33 +00:00
|
|
|
return code;
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SOperatorInfo* downstream = pOperator->pDownstream[0];
|
2026-03-29 01:38:08 +00:00
|
|
|
bool noSplitOutput = hasLagLeadFunc(pSup);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
// here we need to handle the existsed group results
|
|
|
|
|
if (pIndefInfo->pNextGroupRes != NULL) { // todo extract method
|
|
|
|
|
for (int32_t k = 0; k < pSup->numOfExprs; ++k) {
|
|
|
|
|
SqlFunctionCtx* pCtx = &pSup->pCtx[k];
|
|
|
|
|
|
|
|
|
|
SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
|
2026-03-29 01:38:08 +00:00
|
|
|
if (pResInfo->initialized && pCtx->fpSet.cleanup != NULL) {
|
|
|
|
|
pCtx->fpSet.cleanup(pCtx);
|
|
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
pResInfo->initialized = false;
|
|
|
|
|
pCtx->pOutput = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
doHandleDataBlock(pOperator, pIndefInfo->pNextGroupRes, downstream, pTaskInfo);
|
|
|
|
|
pIndefInfo->pNextGroupRes = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 01:38:08 +00:00
|
|
|
if (noSplitOutput || pInfo->pRes->info.rows < pOperator->resultInfo.threshold) {
|
2022-07-25 06:15:49 +00:00
|
|
|
while (1) {
|
|
|
|
|
// The downstream exec may change the value of the newgroup, so use a local variable instead.
|
2023-07-05 03:07:08 +00:00
|
|
|
SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0);
|
2022-07-25 06:15:49 +00:00
|
|
|
if (pBlock == NULL) {
|
2022-11-09 11:14:27 +00:00
|
|
|
setOperatorCompleted(pOperator);
|
2022-07-25 06:15:49 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2023-06-16 02:26:09 +00:00
|
|
|
pInfo->pRes->info.scanFlag = pBlock->info.scanFlag;
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2022-11-28 04:32:40 +00:00
|
|
|
if (pIndefInfo->groupId == 0 && pBlock->info.id.groupId != 0) {
|
|
|
|
|
pIndefInfo->groupId = pBlock->info.id.groupId; // this is the initial group result
|
2022-07-25 06:15:49 +00:00
|
|
|
} else {
|
2022-11-28 04:32:40 +00:00
|
|
|
if (pIndefInfo->groupId != pBlock->info.id.groupId) { // reset output buffer and computing status
|
|
|
|
|
pIndefInfo->groupId = pBlock->info.id.groupId;
|
2022-07-25 06:15:49 +00:00
|
|
|
pIndefInfo->pNextGroupRes = pBlock;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
doHandleDataBlock(pOperator, pBlock, downstream, pTaskInfo);
|
2026-04-21 01:52:04 +00:00
|
|
|
// External-window outputs carry per-window row ranges in stream runtime state.
|
|
|
|
|
// Return as soon as this operator has a result block so the downstream state
|
|
|
|
|
// still matches the block we are about to hand back to the runner.
|
|
|
|
|
if (pTaskInfo->pStreamRuntimeInfo != NULL && pTaskInfo->pStreamRuntimeInfo->funcInfo.withExternalWindow &&
|
|
|
|
|
pInfo->pRes->info.rows > 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-03-29 01:38:08 +00:00
|
|
|
if (!noSplitOutput && pInfo->pRes->info.rows >= pOperator->resultInfo.threshold) {
|
2022-07-25 06:15:49 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
code = doFilter(pInfo->pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
|
2024-09-23 11:54:33 +00:00
|
|
|
QUERY_CHECK_CODE(code, lino, _end);
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2022-07-25 06:15:49 +00:00
|
|
|
size_t rows = pInfo->pRes->info.rows;
|
|
|
|
|
if (rows > 0 || pOperator->status == OP_EXEC_DONE) {
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
blockDataCleanup(pInfo->pRes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 02:08:49 +00:00
|
|
|
*pResBlock = (pInfo->pRes->info.rows> 0) ? pInfo->pRes : NULL;
|
2024-09-23 11:54:33 +00:00
|
|
|
|
|
|
|
|
_end:
|
|
|
|
|
if (code != TSDB_CODE_SUCCESS) {
|
|
|
|
|
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
|
|
|
|
|
pTaskInfo->code = code;
|
|
|
|
|
T_LONG_JMP(pTaskInfo->env, code);
|
|
|
|
|
}
|
2024-07-24 09:08:08 +00:00
|
|
|
return code;
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
int32_t initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size) {
|
2024-08-21 08:28:44 +00:00
|
|
|
int32_t code = TSDB_CODE_SUCCESS;
|
2022-07-25 06:15:49 +00:00
|
|
|
for (int32_t j = 0; j < size; ++j) {
|
|
|
|
|
struct SResultRowEntryInfo* pResInfo = GET_RES_INFO(&pCtx[j]);
|
|
|
|
|
if (isRowEntryInitialized(pResInfo) || fmIsPseudoColumnFunc(pCtx[j].functionId) || pCtx[j].functionId == -1 ||
|
|
|
|
|
fmIsScalarFunc(pCtx[j].functionId)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-21 08:28:44 +00:00
|
|
|
code = pCtx[j].fpSet.init(&pCtx[j], pCtx[j].resultInfo);
|
|
|
|
|
if (code) {
|
|
|
|
|
return code;
|
|
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
2024-07-24 09:08:08 +00:00
|
|
|
|
|
|
|
|
return 0;
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The start of each column SResultRowEntryInfo is denote by RowCellInfoOffset.
|
|
|
|
|
* Note that in case of top/bottom query, the whole multiple rows of result is treated as only one row of results.
|
|
|
|
|
* +------------+-----------------result column 1------------+------------------result column 2-----------+
|
|
|
|
|
* | SResultRow | SResultRowEntryInfo | intermediate buffer1 | SResultRowEntryInfo | intermediate buffer 2|
|
|
|
|
|
* +------------+--------------------------------------------+--------------------------------------------+
|
|
|
|
|
* offset[0] offset[1] offset[2]
|
|
|
|
|
*/
|
|
|
|
|
// TODO refactor: some function move away
|
2025-07-23 09:11:16 +00:00
|
|
|
int32_t setFunctionResultOutput(struct SOperatorInfo* pOperator, SOptrBasicInfo* pInfo, SAggSupporter* pSup, int32_t stage,
|
2022-07-25 06:15:49 +00:00
|
|
|
int32_t numOfExprs) {
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
|
|
|
|
SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx;
|
|
|
|
|
int32_t* rowEntryInfoOffset = pOperator->exprSupp.rowEntryInfoOffset;
|
|
|
|
|
|
|
|
|
|
SResultRowInfo* pResultRowInfo = &pInfo->resultRowInfo;
|
|
|
|
|
initResultRowInfo(pResultRowInfo);
|
|
|
|
|
|
|
|
|
|
int64_t tid = 0;
|
|
|
|
|
int64_t groupId = 0;
|
|
|
|
|
SResultRow* pRow = doSetResultOutBufByKey(pSup->pResultBuf, pResultRowInfo, (char*)&tid, sizeof(tid), true, groupId,
|
2023-03-12 03:03:41 +00:00
|
|
|
pTaskInfo, false, pSup, true);
|
2024-08-06 09:32:20 +00:00
|
|
|
if (pRow == NULL || pTaskInfo->code != 0) {
|
|
|
|
|
return pTaskInfo->code;
|
|
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < numOfExprs; ++i) {
|
|
|
|
|
struct SResultRowEntryInfo* pEntry = getResultEntryInfo(pRow, i, rowEntryInfoOffset);
|
|
|
|
|
cleanupResultRowEntry(pEntry);
|
|
|
|
|
|
|
|
|
|
pCtx[i].resultInfo = pEntry;
|
|
|
|
|
pCtx[i].scanFlag = stage;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
return initCtxOutputBuffer(pCtx, numOfExprs);
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
int32_t setRowTsColumnOutputInfo(SqlFunctionCtx* pCtx, int32_t numOfCols, SArray** pResList) {
|
2024-09-06 07:44:25 +00:00
|
|
|
QRY_PARAM_CHECK(pResList);
|
2022-07-25 06:15:49 +00:00
|
|
|
SArray* pList = taosArrayInit(4, sizeof(int32_t));
|
2024-07-24 09:08:08 +00:00
|
|
|
if (pList == NULL) {
|
|
|
|
|
return terrno;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-25 06:15:49 +00:00
|
|
|
for (int32_t i = 0; i < numOfCols; ++i) {
|
2025-07-23 09:11:16 +00:00
|
|
|
if (fmIsPseudoColumnFunc(pCtx[i].functionId) && !fmIsPlaceHolderFunc(pCtx[i].functionId)) {
|
2024-07-24 09:08:08 +00:00
|
|
|
void* px = taosArrayPush(pList, &i);
|
|
|
|
|
if (px == NULL) {
|
|
|
|
|
return terrno;
|
|
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
*pResList = pList;
|
|
|
|
|
return 0;
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-06 07:05:49 +00:00
|
|
|
int32_t doGenerateSourceData(SOperatorInfo* pOperator) {
|
2022-07-25 06:15:49 +00:00
|
|
|
SProjectOperatorInfo* pProjectInfo = pOperator->info;
|
|
|
|
|
|
|
|
|
|
SExprSupp* pSup = &pOperator->exprSupp;
|
|
|
|
|
SSDataBlock* pRes = pProjectInfo->binfo.pRes;
|
2024-07-24 09:08:08 +00:00
|
|
|
SExprInfo* pExpr = pSup->pExprInfo;
|
|
|
|
|
SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo;
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
int32_t code = blockDataEnsureCapacity(pRes, pOperator->resultInfo.capacity);
|
|
|
|
|
if (code) {
|
|
|
|
|
return code;
|
|
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
|
|
|
|
|
for (int32_t k = 0; k < pSup->numOfExprs; ++k) {
|
|
|
|
|
int32_t outputSlotId = pExpr[k].base.resSchema.slotId;
|
|
|
|
|
|
2023-07-06 06:54:01 +00:00
|
|
|
if (pExpr[k].pExpr->nodeType == QUERY_NODE_VALUE) {
|
|
|
|
|
SColumnInfoData* pColInfoData = taosArrayGet(pRes->pDataBlock, outputSlotId);
|
2024-07-24 09:08:08 +00:00
|
|
|
if (pColInfoData == NULL) {
|
|
|
|
|
return terrno;
|
|
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2023-07-06 06:54:01 +00:00
|
|
|
int32_t type = pExpr[k].base.pParam[0].param.nType;
|
|
|
|
|
if (TSDB_DATA_TYPE_NULL == type) {
|
|
|
|
|
colDataSetNNULL(pColInfoData, 0, 1);
|
|
|
|
|
} else {
|
2024-07-24 09:08:08 +00:00
|
|
|
code = colDataSetVal(pColInfoData, 0, taosVariantGet(&pExpr[k].base.pParam[0].param, type), false);
|
|
|
|
|
if (code) {
|
|
|
|
|
return code;
|
|
|
|
|
}
|
2023-07-06 06:54:01 +00:00
|
|
|
}
|
|
|
|
|
} else if (pExpr[k].pExpr->nodeType == QUERY_NODE_FUNCTION) {
|
|
|
|
|
SqlFunctionCtx* pfCtx = &pSup->pCtx[k];
|
|
|
|
|
|
2023-07-06 08:11:41 +00:00
|
|
|
// UDF scalar functions will be calculated here, for example, select foo(n) from (select 1 n).
|
|
|
|
|
// UDF aggregate functions will be handled in agg operator.
|
|
|
|
|
if (fmIsScalarFunc(pfCtx->functionId)) {
|
2023-07-06 06:54:01 +00:00
|
|
|
SArray* pBlockList = taosArrayInit(4, POINTER_BYTES);
|
2024-07-24 09:08:08 +00:00
|
|
|
if (pBlockList == NULL) {
|
|
|
|
|
return terrno;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* px = taosArrayPush(pBlockList, &pRes);
|
|
|
|
|
if (px == NULL) {
|
|
|
|
|
return terrno;
|
|
|
|
|
}
|
2023-07-06 06:54:01 +00:00
|
|
|
|
|
|
|
|
SColumnInfoData* pResColData = taosArrayGet(pRes->pDataBlock, outputSlotId);
|
2024-07-24 09:08:08 +00:00
|
|
|
if (pResColData == NULL) {
|
|
|
|
|
return terrno;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-06 06:54:01 +00:00
|
|
|
SColumnInfoData idata = {.info = pResColData->info, .hasNull = true};
|
|
|
|
|
|
|
|
|
|
SScalarParam dest = {.columnData = &idata};
|
2025-12-22 03:35:33 +00:00
|
|
|
gTaskScalarExtra.pStreamInfo = GET_STM_RTINFO(pOperator->pTaskInfo);
|
|
|
|
|
gTaskScalarExtra.pStreamRange = NULL;
|
|
|
|
|
code = scalarCalculate((SNode*)pExpr[k].pExpr->_function.pFunctNode, pBlockList, &dest, &gTaskScalarExtra);
|
2023-07-06 06:54:01 +00:00
|
|
|
if (code != TSDB_CODE_SUCCESS) {
|
|
|
|
|
taosArrayDestroy(pBlockList);
|
2023-07-06 07:05:49 +00:00
|
|
|
return code;
|
2023-07-06 06:54:01 +00:00
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2023-07-06 06:54:01 +00:00
|
|
|
int32_t startOffset = pRes->info.rows;
|
2024-08-20 06:33:38 +00:00
|
|
|
if (pRes->info.capacity <= 0) {
|
|
|
|
|
qError("project failed at: %s:%d", __func__, __LINE__);
|
2024-08-22 10:34:15 +00:00
|
|
|
return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
|
2024-08-20 06:33:38 +00:00
|
|
|
}
|
2024-07-24 09:08:08 +00:00
|
|
|
code = colDataAssign(pResColData, &idata, dest.numOfRows, &pRes->info);
|
|
|
|
|
if (code) {
|
|
|
|
|
return code;
|
|
|
|
|
}
|
2023-07-06 06:54:01 +00:00
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
colDataDestroy(&idata);
|
2023-07-06 06:54:01 +00:00
|
|
|
taosArrayDestroy(pBlockList);
|
2023-07-06 08:11:41 +00:00
|
|
|
} else {
|
|
|
|
|
return TSDB_CODE_OPS_NOT_SUPPORT;
|
2023-07-06 06:54:01 +00:00
|
|
|
}
|
2026-03-06 06:45:30 +00:00
|
|
|
} else if (pExpr[k].pExpr->nodeType == QUERY_NODE_OPERATOR) {
|
|
|
|
|
TAOS_CHECK_RETURN(projectApplyOperator(&pExpr[k], pRes, NULL, outputSlotId, NULL, false, &gTaskScalarExtra));
|
2022-07-25 06:15:49 +00:00
|
|
|
} else {
|
2023-07-06 07:05:49 +00:00
|
|
|
return TSDB_CODE_OPS_NOT_SUPPORT;
|
2022-07-25 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pRes->info.rows = 1;
|
2025-09-25 07:48:14 +00:00
|
|
|
code = doFilter(pRes, pOperator->exprSupp.pFilterInfo, NULL, NULL);
|
2024-07-24 09:08:08 +00:00
|
|
|
if (code) {
|
|
|
|
|
pTaskInfo->code = code;
|
|
|
|
|
return code;
|
|
|
|
|
}
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
(void) doIngroupLimitOffset(&pProjectInfo->limitInfo, 0, pRes, pOperator);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2022-11-09 11:14:27 +00:00
|
|
|
setOperatorCompleted(pOperator);
|
2022-07-25 06:15:49 +00:00
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
return code;
|
2022-07-25 13:50:51 +00:00
|
|
|
}
|
2022-11-28 04:32:40 +00:00
|
|
|
|
|
|
|
|
static void setPseudoOutputColInfo(SSDataBlock* pResult, SqlFunctionCtx* pCtx, SArray* pPseudoList) {
|
|
|
|
|
size_t num = (pPseudoList != NULL) ? taosArrayGetSize(pPseudoList) : 0;
|
|
|
|
|
for (int32_t i = 0; i < num; ++i) {
|
|
|
|
|
pCtx[i].pOutput = taosArrayGet(pResult->pDataBlock, i);
|
2024-07-24 09:08:08 +00:00
|
|
|
if (pCtx[i].pOutput == NULL) {
|
|
|
|
|
qError("failed to get the output buf, ptr is null");
|
|
|
|
|
}
|
2022-11-28 04:32:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
int32_t projectApplyColumn(SSDataBlock* pResult, SSDataBlock* pSrcBlock, int32_t outputSlotId, SqlFunctionCtx* pfCtx, int32_t* numOfRows, bool createNewColModel) {
|
|
|
|
|
int32_t code = 0, lino = 0;
|
|
|
|
|
SInputColumnInfoData* pInputData = &pfCtx->input;
|
|
|
|
|
SColumnInfoData* pColInfoData = taosArrayGet(pResult->pDataBlock, outputSlotId);
|
|
|
|
|
TSDB_CHECK_NULL(pColInfoData, code, lino, _exit, terrno);
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
if (pResult->info.rows > 0 && !createNewColModel) {
|
|
|
|
|
if (pInputData->pData[0] == NULL) {
|
|
|
|
|
int32_t slotId = pfCtx->param[0].pCol->slotId;
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
SColumnInfoData* pInput = taosArrayGet(pSrcBlock->pDataBlock, slotId);
|
|
|
|
|
TSDB_CHECK_NULL(pInput, code, lino, _exit, terrno);
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
TAOS_CHECK_EXIT(colDataMergeCol(pColInfoData, pResult->info.rows, (int32_t*)&pResult->info.capacity, pInput,
|
|
|
|
|
pSrcBlock->info.rows));
|
|
|
|
|
*numOfRows = pSrcBlock->info.rows;
|
|
|
|
|
return code;
|
2022-11-28 04:32:40 +00:00
|
|
|
}
|
2025-09-25 07:48:14 +00:00
|
|
|
|
|
|
|
|
TAOS_CHECK_EXIT(colDataMergeCol(pColInfoData, pResult->info.rows, (int32_t*)&pResult->info.capacity,
|
|
|
|
|
pInputData->pData[0], pInputData->numOfRows));
|
|
|
|
|
*numOfRows = pInputData->numOfRows;
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pInputData->pData[0] == NULL) {
|
|
|
|
|
int32_t slotId = pfCtx->param[0].pCol->slotId;
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
SColumnInfoData* pInput = taosArrayGet(pSrcBlock->pDataBlock, slotId);
|
|
|
|
|
TSDB_CHECK_NULL(pInput, code, lino, _exit, terrno);
|
|
|
|
|
|
|
|
|
|
TAOS_CHECK_EXIT(colDataAssign(pColInfoData, pInput, pSrcBlock->info.rows, &pResult->info));
|
|
|
|
|
*numOfRows = pSrcBlock->info.rows;
|
|
|
|
|
|
|
|
|
|
return code;
|
2022-11-28 04:32:40 +00:00
|
|
|
}
|
2025-09-25 07:48:14 +00:00
|
|
|
|
|
|
|
|
TAOS_CHECK_EXIT(colDataAssign(pColInfoData, pInputData->pData[0], pInputData->numOfRows, &pResult->info));
|
|
|
|
|
*numOfRows = pInputData->numOfRows;
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
_exit:
|
|
|
|
|
|
|
|
|
|
if (code) {
|
|
|
|
|
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
|
2022-11-28 04:32:40 +00:00
|
|
|
}
|
2025-09-25 07:48:14 +00:00
|
|
|
|
|
|
|
|
return code;
|
|
|
|
|
}
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
|
|
|
|
|
int32_t projectApplyValue(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBlock* pSrcBlock, int32_t outputSlotId, int32_t* numOfRows, bool createNewColModel) {
|
|
|
|
|
int32_t code = 0, lino = 0;
|
|
|
|
|
SColumnInfoData* pColInfoData = taosArrayGet(pResult->pDataBlock, outputSlotId);
|
|
|
|
|
TSDB_CHECK_NULL(pColInfoData, code, lino, _exit, terrno);
|
|
|
|
|
|
|
|
|
|
int32_t offset = createNewColModel ? 0 : pResult->info.rows;
|
|
|
|
|
int32_t type = pExpr->base.pParam[0].param.nType;
|
|
|
|
|
if (TSDB_DATA_TYPE_NULL == type) {
|
|
|
|
|
colDataSetNNULL(pColInfoData, offset, pSrcBlock->info.rows);
|
|
|
|
|
} else {
|
|
|
|
|
char* p = taosVariantGet(&pExpr->base.pParam[0].param, type);
|
|
|
|
|
for (int32_t i = 0; i < pSrcBlock->info.rows; ++i) {
|
|
|
|
|
TAOS_CHECK_EXIT(colDataSetVal(pColInfoData, i + offset, p, false));
|
2024-07-24 09:08:08 +00:00
|
|
|
}
|
2022-11-28 04:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
*numOfRows = pSrcBlock->info.rows;
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
_exit:
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
if (code) {
|
|
|
|
|
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return code;
|
|
|
|
|
}
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2024-07-25 01:55:27 +00:00
|
|
|
|
2022-12-20 02:15:14 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
int32_t projectApplyOperator(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBlock* pSrcBlock, int32_t outputSlotId, int32_t* numOfRows, bool createNewColModel, const void* pExtraParams) {
|
|
|
|
|
int32_t code = 0, lino = 0;
|
2026-03-06 06:45:30 +00:00
|
|
|
SArray* pBlockList = NULL;
|
|
|
|
|
if (NULL != pSrcBlock) {
|
|
|
|
|
pBlockList = taosArrayInit(4, POINTER_BYTES);
|
|
|
|
|
TSDB_CHECK_NULL(pBlockList, code, lino, _exit, terrno);
|
2024-07-25 01:55:27 +00:00
|
|
|
|
2026-03-06 06:45:30 +00:00
|
|
|
void* px = taosArrayPush(pBlockList, &pSrcBlock);
|
|
|
|
|
TSDB_CHECK_NULL(px, code, lino, _exit, terrno);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
SColumnInfoData* pResColData = taosArrayGet(pResult->pDataBlock, outputSlotId);
|
|
|
|
|
TSDB_CHECK_NULL(pResColData, code, lino, _exit, terrno);
|
2024-07-25 01:55:27 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
SColumnInfoData idata = {.info = pResColData->info, .hasNull = true};
|
|
|
|
|
SScalarParam dest = {.columnData = &idata};
|
2025-12-22 03:35:33 +00:00
|
|
|
gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
|
|
|
|
|
gTaskScalarExtra.pStreamRange = NULL;
|
|
|
|
|
TAOS_CHECK_EXIT(scalarCalculate(pExpr->pExpr->_optrRoot.pRootNode, pBlockList, &dest, &gTaskScalarExtra));
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
if (pResult->info.rows > 0 && !createNewColModel) {
|
|
|
|
|
code = colDataMergeCol(pResColData, pResult->info.rows, (int32_t*)&pResult->info.capacity, &idata, dest.numOfRows);
|
|
|
|
|
} else {
|
|
|
|
|
code = colDataAssign(pResColData, &idata, dest.numOfRows, &pResult->info);
|
|
|
|
|
}
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
colDataDestroy(&idata);
|
|
|
|
|
TAOS_CHECK_EXIT(code);
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2026-03-06 06:45:30 +00:00
|
|
|
if (numOfRows) {
|
|
|
|
|
*numOfRows = dest.numOfRows;
|
|
|
|
|
}
|
2025-09-25 07:48:14 +00:00
|
|
|
|
|
|
|
|
_exit:
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
if (code < 0) {
|
|
|
|
|
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
|
|
|
|
|
}
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
taosArrayDestroy(pBlockList);
|
|
|
|
|
|
|
|
|
|
return code;
|
|
|
|
|
}
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
int32_t projectApplyFunction(SqlFunctionCtx* pCtx, SqlFunctionCtx* pfCtx, SExprInfo* pExpr, SSDataBlock* pResult, SSDataBlock* pSrcBlock,
|
|
|
|
|
int32_t outputSlotId, int32_t* numOfRows, bool createNewColModel, const void* pExtraParams,
|
|
|
|
|
SArray* pPseudoList, SArray** processByRowFunctionCtx, bool doSelectFunc) {
|
|
|
|
|
int32_t code = 0, lino = 0;
|
|
|
|
|
SArray* pBlockList = NULL;
|
|
|
|
|
SColumnInfoData* pResColData = taosArrayGet(pResult->pDataBlock, outputSlotId);
|
|
|
|
|
TSDB_CHECK_NULL(pResColData, code, lino, _exit, terrno);
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
if (fmIsPlaceHolderFunc(pfCtx->functionId) && pExtraParams && pfCtx->pExpr->base.pParamList && 1 == pfCtx->pExpr->base.pParamList->length) {
|
2026-03-29 01:38:08 +00:00
|
|
|
SNode* pParamNode = nodesListGetNode(pfCtx->pExpr->base.pParamList, 0);
|
2026-04-21 01:52:04 +00:00
|
|
|
SStreamRuntimeFuncInfo* pStreamInfo = (SStreamRuntimeFuncInfo*)pExtraParams;
|
|
|
|
|
if (pStreamInfo != NULL && pStreamInfo->withExternalWindow && pStreamInfo->pStreamBlkWinIdx != NULL &&
|
|
|
|
|
taosArrayGetSize(pStreamInfo->pStreamBlkWinIdx) > 1) {
|
|
|
|
|
TAOS_CHECK_EXIT(assignPlaceHolderInExternalWindows(pResColData, pResult->info.rows, pSrcBlock->info.rows,
|
|
|
|
|
pfCtx->functionId, pStreamInfo, pParamNode));
|
|
|
|
|
} else {
|
|
|
|
|
TAOS_CHECK_EXIT(scalarAssignPlaceHolderRes(pResColData, pResult->info.rows, pSrcBlock->info.rows,
|
|
|
|
|
pfCtx->functionId, pExtraParams, pParamNode));
|
|
|
|
|
}
|
2025-09-25 07:48:14 +00:00
|
|
|
*numOfRows = pSrcBlock->info.rows;
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
return code;
|
|
|
|
|
}
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
if (fmIsScalarFunc(pfCtx->functionId) || fmIsPlaceHolderFunc(pfCtx->functionId)) {
|
|
|
|
|
pBlockList = taosArrayInit(4, POINTER_BYTES);
|
|
|
|
|
TSDB_CHECK_NULL(pBlockList, code, lino, _exit, terrno);
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
void* px = taosArrayPush(pBlockList, &pSrcBlock);
|
|
|
|
|
TSDB_CHECK_NULL(px, code, lino, _exit, terrno);
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
SColumnInfoData idata = {.info = pResColData->info, .hasNull = true};
|
|
|
|
|
SScalarParam dest = {.columnData = &idata};
|
2025-12-22 03:35:33 +00:00
|
|
|
gTaskScalarExtra.pStreamInfo = (void*)pExtraParams;
|
|
|
|
|
gTaskScalarExtra.pStreamRange = NULL;
|
|
|
|
|
TAOS_CHECK_EXIT(scalarCalculate((SNode*)pExpr->pExpr->_function.pFunctNode, pBlockList, &dest, &gTaskScalarExtra));
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
if (pResult->info.rows > 0 && !createNewColModel) {
|
|
|
|
|
code = colDataMergeCol(pResColData, pResult->info.rows, (int32_t*)&pResult->info.capacity, &idata, dest.numOfRows);
|
|
|
|
|
} else {
|
|
|
|
|
SColumnInfo oriInfo = pResColData->info;
|
|
|
|
|
code = colDataAssign(pResColData, &idata, dest.numOfRows, &pResult->info);
|
|
|
|
|
// restore the original column info to satisfy the output column schema
|
|
|
|
|
pResColData->info = oriInfo;
|
|
|
|
|
}
|
2024-07-25 01:55:27 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
colDataDestroy(&idata);
|
|
|
|
|
taosArrayDestroy(pBlockList);
|
|
|
|
|
TAOS_CHECK_EXIT(code);
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
*numOfRows = dest.numOfRows;
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
return code;
|
|
|
|
|
}
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
if (fmIsIndefiniteRowsFunc(pfCtx->functionId)) {
|
|
|
|
|
SResultRowEntryInfo* pResInfo = GET_RES_INFO(pfCtx);
|
|
|
|
|
TAOS_CHECK_EXIT(pfCtx->fpSet.init(pfCtx, pResInfo));
|
2022-11-28 04:32:40 +00:00
|
|
|
|
|
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
pfCtx->pOutput = (char*)pResColData;
|
|
|
|
|
TSDB_CHECK_NULL(pfCtx->pOutput, code, lino, _exit, terrno);
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
pfCtx->offset = createNewColModel ? 0 : pResult->info.rows; // set the start offset
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
// set the timestamp(_rowts) output buffer
|
|
|
|
|
if (taosArrayGetSize(pPseudoList) > 0) {
|
|
|
|
|
int32_t* outputColIndex = taosArrayGet(pPseudoList, 0);
|
|
|
|
|
TSDB_CHECK_NULL(outputColIndex, code, lino, _exit, terrno);
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
pfCtx->pTsOutput = (SColumnInfoData*)pCtx[*outputColIndex].pOutput;
|
|
|
|
|
}
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
// link pDstBlock to set selectivity value
|
|
|
|
|
if (pfCtx->subsidiaries.num > 0) {
|
|
|
|
|
pfCtx->pDstBlock = pResult;
|
|
|
|
|
}
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
code = pfCtx->fpSet.process(pfCtx);
|
|
|
|
|
if (code != TSDB_CODE_SUCCESS) {
|
|
|
|
|
if (pfCtx->fpSet.cleanup != NULL) {
|
|
|
|
|
pfCtx->fpSet.cleanup(pfCtx);
|
|
|
|
|
}
|
|
|
|
|
TAOS_CHECK_EXIT(code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*numOfRows = pResInfo->numOfRes;
|
|
|
|
|
|
|
|
|
|
if (fmIsProcessByRowFunc(pfCtx->functionId)) {
|
|
|
|
|
if (NULL == *processByRowFunctionCtx) {
|
|
|
|
|
*processByRowFunctionCtx = taosArrayInit(1, sizeof(SqlFunctionCtx*));
|
|
|
|
|
TSDB_CHECK_NULL(*processByRowFunctionCtx, code, lino, _exit, terrno);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* px = taosArrayPush(*processByRowFunctionCtx, &pfCtx);
|
|
|
|
|
TSDB_CHECK_NULL(px, code, lino, _exit, terrno);
|
|
|
|
|
}
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fmIsAggFunc(pfCtx->functionId)) {
|
|
|
|
|
// selective value output should be set during corresponding function execution
|
|
|
|
|
if (!doSelectFunc && fmIsSelectValueFunc(pfCtx->functionId)) {
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// _group_key function for "partition by tbname" + csum(col_name) query
|
|
|
|
|
int32_t slotId = pfCtx->param[0].pCol->slotId;
|
|
|
|
|
|
|
|
|
|
// todo handle the json tag
|
|
|
|
|
SColumnInfoData* pInput = taosArrayGet(pSrcBlock->pDataBlock, slotId);
|
|
|
|
|
TSDB_CHECK_NULL(pInput, code, lino, _exit, terrno);
|
|
|
|
|
|
|
|
|
|
for (int32_t f = 0; f < pSrcBlock->info.rows; ++f) {
|
|
|
|
|
bool isNull = colDataIsNull_s(pInput, f);
|
|
|
|
|
if (isNull) {
|
|
|
|
|
colDataSetNULL(pResColData, pResult->info.rows + f);
|
2022-11-28 04:32:40 +00:00
|
|
|
} else {
|
2025-09-25 07:48:14 +00:00
|
|
|
char* data = colDataGetData(pInput, f);
|
|
|
|
|
TAOS_CHECK_EXIT(colDataSetVal(pResColData, pResult->info.rows + f, data, isNull));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
*numOfRows = pSrcBlock->info.rows;
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fmIsGroupIdFunc(pfCtx->functionId)) {
|
|
|
|
|
for (int32_t f = 0; f < pSrcBlock->info.rows; ++f) {
|
|
|
|
|
TAOS_CHECK_EXIT(colDataSetVal(pResColData, pResult->info.rows + f, (const char*)&pSrcBlock->info.id.groupId, false));
|
|
|
|
|
}
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
*numOfRows = pSrcBlock->info.rows;
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_exit:
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
if (code) {
|
|
|
|
|
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
|
|
|
|
|
}
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
taosArrayDestroy(pBlockList);
|
|
|
|
|
|
|
|
|
|
return code;
|
|
|
|
|
}
|
2024-07-25 01:55:27 +00:00
|
|
|
|
2022-11-28 04:32:40 +00:00
|
|
|
|
2025-09-25 07:48:14 +00:00
|
|
|
int32_t projectApplyFunctionsWithSelect(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBlock* pSrcBlock,
|
|
|
|
|
SqlFunctionCtx* pCtx, int32_t numOfOutput, SArray* pPseudoList,
|
|
|
|
|
const void* pExtraParams, bool doSelectFunc, bool hasIndefRowsFunc) {
|
|
|
|
|
int32_t lino = 0;
|
|
|
|
|
int32_t code = TSDB_CODE_SUCCESS;
|
|
|
|
|
if (hasIndefRowsFunc) {
|
|
|
|
|
setPseudoOutputColInfo(pResult, pCtx, pPseudoList);
|
|
|
|
|
}
|
|
|
|
|
pResult->info.dataLoad = 1;
|
|
|
|
|
|
|
|
|
|
SArray* processByRowFunctionCtx = NULL;
|
2026-03-29 01:38:08 +00:00
|
|
|
SArray* pProcessedFuncIds = NULL;
|
|
|
|
|
SArray* pGroupedCtxArray = NULL;
|
2025-09-25 07:48:14 +00:00
|
|
|
if (pSrcBlock == NULL) {
|
|
|
|
|
for (int32_t k = 0; k < numOfOutput; ++k) {
|
|
|
|
|
int32_t outputSlotId = pExpr[k].base.resSchema.slotId;
|
|
|
|
|
|
|
|
|
|
if (pExpr[k].pExpr->nodeType != QUERY_NODE_VALUE) {
|
|
|
|
|
qError("project failed at: %s:%d", __func__, __LINE__);
|
|
|
|
|
TAOS_CHECK_EXIT(TSDB_CODE_INVALID_PARA);
|
|
|
|
|
}
|
|
|
|
|
SColumnInfoData* pColInfoData = taosArrayGet(pResult->pDataBlock, outputSlotId);
|
|
|
|
|
TSDB_CHECK_NULL(pColInfoData, code, lino, _exit, terrno);
|
|
|
|
|
|
|
|
|
|
int32_t type = pExpr[k].base.pParam[0].param.nType;
|
|
|
|
|
if (TSDB_DATA_TYPE_NULL == type) {
|
|
|
|
|
colDataSetNNULL(pColInfoData, 0, 1);
|
|
|
|
|
} else {
|
|
|
|
|
TAOS_CHECK_EXIT(colDataSetVal(pColInfoData, 0, taosVariantGet(&pExpr[k].base.pParam[0].param, type), false));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pResult->info.rows = 1;
|
|
|
|
|
goto _exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pResult != pSrcBlock) {
|
|
|
|
|
pResult->info.id.groupId = pSrcBlock->info.id.groupId;
|
|
|
|
|
if (pSrcBlock->info.parTbName[0]) {
|
|
|
|
|
tstrncpy(pResult->info.parTbName, pSrcBlock->info.parTbName, TSDB_TABLE_NAME_LEN);
|
|
|
|
|
}
|
|
|
|
|
qTrace("%s, parName:%s,groupId:%" PRIu64, __FUNCTION__, pSrcBlock->info.parTbName, pResult->info.id.groupId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if the source equals to the destination, it is to create a new column as the result of scalar
|
|
|
|
|
// function or some operators.
|
|
|
|
|
bool createNewColModel = (pResult == pSrcBlock);
|
|
|
|
|
if (createNewColModel) {
|
|
|
|
|
TAOS_CHECK_EXIT(blockDataEnsureCapacity(pResult, pResult->info.rows));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t numOfRows = 0;
|
|
|
|
|
|
|
|
|
|
for (int32_t k = 0; k < numOfOutput; ++k) {
|
|
|
|
|
int32_t outputSlotId = pExpr[k].base.resSchema.slotId;
|
|
|
|
|
SqlFunctionCtx* pfCtx = &pCtx[k];
|
|
|
|
|
switch (pExpr[k].pExpr->nodeType) {
|
|
|
|
|
case QUERY_NODE_COLUMN: {
|
|
|
|
|
TAOS_CHECK_EXIT(projectApplyColumn(pResult, pSrcBlock, outputSlotId, pfCtx, &numOfRows, createNewColModel));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case QUERY_NODE_VALUE: {
|
|
|
|
|
TAOS_CHECK_EXIT(projectApplyValue(&pExpr[k], pResult, pSrcBlock, outputSlotId, &numOfRows, createNewColModel));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case QUERY_NODE_OPERATOR: {
|
|
|
|
|
TAOS_CHECK_EXIT(projectApplyOperator(&pExpr[k], pResult, pSrcBlock, outputSlotId, &numOfRows, createNewColModel, pExtraParams));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case QUERY_NODE_FUNCTION: {
|
|
|
|
|
TAOS_CHECK_EXIT(projectApplyFunction(pCtx, pfCtx, &pExpr[k], pResult, pSrcBlock, outputSlotId, &numOfRows, createNewColModel, pExtraParams, pPseudoList, &processByRowFunctionCtx, doSelectFunc));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
qError("invalid project expr nodeType:%d", pExpr[k].pExpr->nodeType);
|
|
|
|
|
TAOS_CHECK_EXIT(TSDB_CODE_OPS_NOT_SUPPORT);
|
2022-11-28 04:32:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-16 06:42:16 +00:00
|
|
|
if (processByRowFunctionCtx && taosArrayGetSize(processByRowFunctionCtx) > 0) {
|
2026-03-29 01:38:08 +00:00
|
|
|
int32_t processByRowSize = taosArrayGetSize(processByRowFunctionCtx);
|
2026-04-21 01:52:04 +00:00
|
|
|
SStreamRuntimeFuncInfo* pStreamInfo = (SStreamRuntimeFuncInfo*)pExtraParams;
|
|
|
|
|
bool splitByExternalWindow = pSrcBlock != NULL && pStreamInfo != NULL && pStreamInfo->withExternalWindow &&
|
|
|
|
|
pStreamInfo->pStreamBlkWinIdx != NULL &&
|
|
|
|
|
taosArrayGetSize(pStreamInfo->pStreamBlkWinIdx) > 1 &&
|
|
|
|
|
allProcessByRowCtxSameFuncId(processByRowFunctionCtx);
|
2026-03-29 01:38:08 +00:00
|
|
|
pProcessedFuncIds = taosArrayInit(4, sizeof(int32_t));
|
|
|
|
|
TSDB_CHECK_NULL(pProcessedFuncIds, code, lino, _exit, terrno);
|
|
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < processByRowSize; ++i) {
|
|
|
|
|
SqlFunctionCtx** ppCurrCtx = taosArrayGet(processByRowFunctionCtx, i);
|
|
|
|
|
TSDB_CHECK_NULL(ppCurrCtx, code, lino, _exit, terrno);
|
|
|
|
|
TSDB_CHECK_NULL(*ppCurrCtx, code, lino, _exit, terrno);
|
|
|
|
|
|
|
|
|
|
bool processed = false;
|
|
|
|
|
int32_t processedNum = taosArrayGetSize(pProcessedFuncIds);
|
|
|
|
|
for (int32_t j = 0; j < processedNum; ++j) {
|
|
|
|
|
int32_t* pFuncId = taosArrayGet(pProcessedFuncIds, j);
|
|
|
|
|
TSDB_CHECK_NULL(pFuncId, code, lino, _exit, terrno);
|
|
|
|
|
if (*pFuncId == (*ppCurrCtx)->functionId) {
|
|
|
|
|
processed = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2026-03-29 01:38:08 +00:00
|
|
|
if (processed) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pGroupedCtxArray = taosArrayInit(2, sizeof(SqlFunctionCtx*));
|
|
|
|
|
TSDB_CHECK_NULL(pGroupedCtxArray, code, lino, _exit, terrno);
|
|
|
|
|
|
|
|
|
|
for (int32_t j = i; j < processByRowSize; ++j) {
|
|
|
|
|
SqlFunctionCtx** ppTmpCtx = taosArrayGet(processByRowFunctionCtx, j);
|
|
|
|
|
TSDB_CHECK_NULL(ppTmpCtx, code, lino, _exit, terrno);
|
|
|
|
|
TSDB_CHECK_NULL(*ppTmpCtx, code, lino, _exit, terrno);
|
|
|
|
|
|
|
|
|
|
if ((*ppTmpCtx)->functionId == (*ppCurrCtx)->functionId) {
|
|
|
|
|
void* px = taosArrayPush(pGroupedCtxArray, ppTmpCtx);
|
|
|
|
|
TSDB_CHECK_NULL(px, code, lino, _exit, terrno);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 01:52:04 +00:00
|
|
|
if (splitByExternalWindow) {
|
|
|
|
|
TAOS_CHECK_EXIT(processByRowInExternalWindows(pGroupedCtxArray, pSrcBlock, pStreamInfo));
|
|
|
|
|
} else {
|
|
|
|
|
TAOS_CHECK_EXIT((*ppCurrCtx)->fpSet.processFuncByRow(pGroupedCtxArray));
|
|
|
|
|
}
|
2026-03-29 01:38:08 +00:00
|
|
|
taosArrayDestroy(pGroupedCtxArray);
|
|
|
|
|
pGroupedCtxArray = NULL;
|
|
|
|
|
|
|
|
|
|
void* px = taosArrayPush(pProcessedFuncIds, &(*ppCurrCtx)->functionId);
|
|
|
|
|
TSDB_CHECK_NULL(px, code, lino, _exit, terrno);
|
|
|
|
|
|
|
|
|
|
numOfRows = (*ppCurrCtx)->resultInfo->numOfRes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
taosArrayDestroy(pProcessedFuncIds);
|
|
|
|
|
pProcessedFuncIds = NULL;
|
2024-07-04 03:04:59 +00:00
|
|
|
}
|
2024-07-25 01:55:27 +00:00
|
|
|
|
2022-11-28 04:32:40 +00:00
|
|
|
if (!createNewColModel) {
|
|
|
|
|
pResult->info.rows += numOfRows;
|
|
|
|
|
}
|
2024-07-24 09:08:08 +00:00
|
|
|
|
2024-07-04 03:04:59 +00:00
|
|
|
_exit:
|
2026-03-29 01:38:08 +00:00
|
|
|
if (pGroupedCtxArray) {
|
|
|
|
|
taosArrayDestroy(pGroupedCtxArray);
|
|
|
|
|
}
|
|
|
|
|
if (pProcessedFuncIds) {
|
|
|
|
|
taosArrayDestroy(pProcessedFuncIds);
|
|
|
|
|
}
|
2025-07-16 06:42:16 +00:00
|
|
|
if (processByRowFunctionCtx) {
|
2024-07-08 12:38:23 +00:00
|
|
|
taosArrayDestroy(processByRowFunctionCtx);
|
2024-07-04 03:04:59 +00:00
|
|
|
}
|
2025-07-16 06:42:16 +00:00
|
|
|
if (code) {
|
2025-09-25 07:48:14 +00:00
|
|
|
qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
|
2024-11-25 03:33:02 +00:00
|
|
|
}
|
2024-07-04 03:04:59 +00:00
|
|
|
return code;
|
2022-11-28 04:32:40 +00:00
|
|
|
}
|
2025-07-16 06:42:16 +00:00
|
|
|
|
|
|
|
|
int32_t projectApplyFunctions(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBlock* pSrcBlock, SqlFunctionCtx* pCtx,
|
|
|
|
|
int32_t numOfOutput, SArray* pPseudoList, const void* pExtraParams) {
|
2025-09-25 07:48:14 +00:00
|
|
|
return projectApplyFunctionsWithSelect(pExpr, pResult, pSrcBlock, pCtx, numOfOutput, pPseudoList, pExtraParams, false, true);
|
2025-07-16 06:42:16 +00:00
|
|
|
}
|