diff --git a/include/common/tdatablock.h b/include/common/tdatablock.h index 89f6beaab4..c09e96d16d 100644 --- a/include/common/tdatablock.h +++ b/include/common/tdatablock.h @@ -49,7 +49,7 @@ typedef struct SBlockOrderInfo { #define colDataSetNull_f_s(c_, r_) \ do { \ colDataSetNull_f((c_)->nullbitmap, r_); \ - memset(((char*)(c_)->pData) + (c_)->info.bytes * (r_), 0, (c_)->info.bytes); \ + (void)memset(((char*)(c_)->pData) + (c_)->info.bytes * (r_), 0, (c_)->info.bytes); \ } while (0) #define colDataClearNull_f(bm_, r_) \ diff --git a/include/dnode/qnode/qnode.h b/include/dnode/qnode/qnode.h index e7f9d00ff3..d6efa52ea7 100644 --- a/include/dnode/qnode/qnode.h +++ b/include/dnode/qnode/qnode.h @@ -34,9 +34,10 @@ typedef struct { * @brief Start one Qnode in Dnode. * * @param pOption Option of the qnode. - * @return SQnode* The qnode object. + * @param pQnode The qnode object. + * @return int32_t The error code. */ -SQnode *qndOpen(const SQnodeOpt *pOption); +int32_t qndOpen(const SQnodeOpt *pOption, SQnode **pQnode); /** * @brief Stop Qnode in Dnode. diff --git a/include/libs/function/function.h b/include/libs/function/function.h index 700cc5ba7f..24fa2898ea 100644 --- a/include/libs/function/function.h +++ b/include/libs/function/function.h @@ -36,7 +36,7 @@ typedef struct SFuncExecEnv { } SFuncExecEnv; typedef bool (*FExecGetEnv)(struct SFunctionNode *pFunc, SFuncExecEnv *pEnv); -typedef bool (*FExecInit)(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo *pResultCellInfo); +typedef int32_t (*FExecInit)(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo *pResultCellInfo); typedef int32_t (*FExecProcess)(struct SqlFunctionCtx *pCtx); typedef int32_t (*FExecFinalize)(struct SqlFunctionCtx *pCtx, SSDataBlock *pBlock); typedef int32_t (*FScalarExecProcess)(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput); diff --git a/include/libs/function/functionMgt.h b/include/libs/function/functionMgt.h index d3fb953dc3..1da1cbf716 100644 --- a/include/libs/function/functionMgt.h +++ b/include/libs/function/functionMgt.h @@ -260,7 +260,7 @@ bool fmIsProcessByRowFunc(int32_t funcId); bool fmisSelectGroupConstValueFunc(int32_t funcId); void getLastCacheDataType(SDataType* pType, int32_t pkBytes); -SFunctionNode* createFunction(const char* pName, SNodeList* pParameterList); +int32_t createFunction(const char* pName, SNodeList* pParameterList, SFunctionNode** pFunc); int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc, SFunctionNode** pMidFunc, SFunctionNode** pMergeFunc); @@ -273,7 +273,7 @@ typedef enum EFuncDataRequired { } EFuncDataRequired; EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow); -EFuncDataRequired fmFuncDynDataRequired(int32_t funcId, void* pRes, SDataBlockInfo* pBlockInfo); +int32_t fmFuncDynDataRequired(int32_t funcId, void* pRes, SDataBlockInfo* pBlockInfo, int32_t *reqStatus); int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet); int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet); diff --git a/include/libs/function/tudf.h b/include/libs/function/tudf.h index 6b15833917..7a8927ca90 100644 --- a/include/libs/function/tudf.h +++ b/include/libs/function/tudf.h @@ -77,7 +77,7 @@ void freeUdfInterBuf(SUdfInterBuf *buf); // high level APIs bool udfAggGetEnv(struct SFunctionNode *pFunc, SFuncExecEnv *pEnv); -bool udfAggInit(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo *pResultCellInfo); +int32_t udfAggInit(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo *pResultCellInfo); int32_t udfAggProcess(struct SqlFunctionCtx *pCtx); int32_t udfAggFinalize(struct SqlFunctionCtx *pCtx, SSDataBlock *pBlock); diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 22da6d5390..9b49c1908d 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -860,6 +860,11 @@ int32_t taosGetErrSize(); #define TSDB_CODE_FUNC_TO_TIMESTAMP_FAILED_TS_ERR TAOS_DEF_ERROR_CODE(0, 0x2807) #define TSDB_CODE_FUNC_TO_TIMESTAMP_FAILED_NOT_SUPPORTED TAOS_DEF_ERROR_CODE(0, 0x2808) #define TSDB_CODE_FUNC_TO_CHAR_NOT_SUPPORTED TAOS_DEF_ERROR_CODE(0, 0x2809) +#define TSDB_CODE_FUNC_TIME_UNIT_INVALID TAOS_DEF_ERROR_CODE(0, 0x280A) +#define TSDB_CODE_FUNC_TIME_UNIT_TOO_SMALL TAOS_DEF_ERROR_CODE(0, 0x280B) +#define TSDB_CODE_FUNC_INVALID_VALUE_RANGE TAOS_DEF_ERROR_CODE(0, 0x280C) +#define TSDB_CODE_FUNC_SETUP_ERROR TAOS_DEF_ERROR_CODE(0, 0x280D) + //udf #define TSDB_CODE_UDF_STOPPING TAOS_DEF_ERROR_CODE(0, 0x2901) diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 724229af16..f640618897 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -897,7 +897,11 @@ void taos_init_imp(void) { tscError("failed to init task queue"); return; } - fmFuncMgtInit(); + if (fmFuncMgtInit() != TSDB_CODE_SUCCESS) { + tscInitRes = -1; + tscError("failed to init function manager"); + return; + } nodesInitAllocatorSet(); clientConnRefPool = taosOpenRef(200, destroyTscObj); diff --git a/source/dnode/mgmt/mgmt_qnode/src/qmInt.c b/source/dnode/mgmt/mgmt_qnode/src/qmInt.c index 553f39e5da..3138614189 100644 --- a/source/dnode/mgmt/mgmt_qnode/src/qmInt.c +++ b/source/dnode/mgmt/mgmt_qnode/src/qmInt.c @@ -34,11 +34,8 @@ static void qmClose(SQnodeMgmt *pMgmt) { } static int32_t qndOpenWrapper(SQnodeOpt *pOption, SQnode **pQnode) { - *pQnode = qndOpen(pOption); - if (*pQnode == NULL) { - return terrno; - } - return 0; + int32_t code = qndOpen(pOption, pQnode); + return code; } static int32_t qmOpen(SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) { int32_t code = 0; @@ -62,7 +59,7 @@ static int32_t qmOpen(SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) { if (code != 0) { dError("failed to open qnode since %s", tstrerror(code)); qmClose(pMgmt); - return -1; + return code; } tmsgReportStartup("qnode-impl", "initialized"); diff --git a/source/dnode/mgmt/mgmt_qnode/src/qmWorker.c b/source/dnode/mgmt/mgmt_qnode/src/qmWorker.c index 23a2aa36e8..5b380c2a0b 100644 --- a/source/dnode/mgmt/mgmt_qnode/src/qmWorker.c +++ b/source/dnode/mgmt/mgmt_qnode/src/qmWorker.c @@ -48,8 +48,8 @@ static int32_t qmPutNodeMsgToWorker(SSingleWorker *pWorker, SRpcMsg *pMsg) { } int32_t qmPutNodeMsgToQueryQueue(SQnodeMgmt *pMgmt, SRpcMsg *pMsg) { - qndPreprocessQueryMsg(pMgmt->pQnode, pMsg); - + int32_t code = qndPreprocessQueryMsg(pMgmt->pQnode, pMsg); + if (code) return code; return qmPutNodeMsgToWorker(&pMgmt->queryWorker, pMsg); } diff --git a/source/dnode/qnode/src/qnode.c b/source/dnode/qnode/src/qnode.c index 8cd967a8a8..b0fcd3400d 100644 --- a/source/dnode/qnode/src/qnode.c +++ b/source/dnode/qnode/src/qnode.c @@ -19,21 +19,22 @@ #include "query.h" #include "qworker.h" -SQnode *qndOpen(const SQnodeOpt *pOption) { - SQnode *pQnode = taosMemoryCalloc(1, sizeof(SQnode)); - if (NULL == pQnode) { +int32_t qndOpen(const SQnodeOpt *pOption, SQnode **pQnode) { + *pQnode = taosMemoryCalloc(1, sizeof(SQnode)); + if (NULL == *pQnode) { qError("calloc SQnode failed"); - return NULL; + return TSDB_CODE_OUT_OF_MEMORY; } - pQnode->qndId = QNODE_HANDLE; + (*pQnode)->qndId = QNODE_HANDLE; - if (qWorkerInit(NODE_TYPE_QNODE, pQnode->qndId, (void **)&pQnode->pQuery, &pOption->msgCb)) { + int32_t code = qWorkerInit(NODE_TYPE_QNODE, (*pQnode)->qndId, (void **)&(*pQnode)->pQuery, &pOption->msgCb); + if (TSDB_CODE_SUCCESS != code) { taosMemoryFreeClear(pQnode); - return NULL; + return code; } - pQnode->msgCb = pOption->msgCb; - return pQnode; + (*pQnode)->msgCb = pOption->msgCb; + return TSDB_CODE_SUCCESS; } void qndClose(SQnode *pQnode) { diff --git a/source/dnode/vnode/src/meta/metaQuery.c b/source/dnode/vnode/src/meta/metaQuery.c index 88a551e47c..062c019d48 100644 --- a/source/dnode/vnode/src/meta/metaQuery.c +++ b/source/dnode/vnode/src/meta/metaQuery.c @@ -1542,9 +1542,14 @@ int32_t metaGetStbStats(void *pVnode, int64_t uid, int64_t *numOfTables, int32_t // slow path: search TDB int64_t ctbNum = 0; int32_t colNum = 0; - vnodeGetCtbNum(pVnode, uid, &ctbNum); - vnodeGetStbColumnNum(pVnode, uid, &colNum); + code = vnodeGetCtbNum(pVnode, uid, &ctbNum); + if (TSDB_CODE_SUCCESS == code) { + code = vnodeGetStbColumnNum(pVnode, uid, &colNum); + } metaULock(pVnodeObj->pMeta); + if (TSDB_CODE_SUCCESS != code) { + goto _exit; + } if (numOfTables) *numOfTables = ctbNum; if (numOfCols) *numOfCols = colNum; diff --git a/source/dnode/vnode/src/vnd/vnodeQuery.c b/source/dnode/vnode/src/vnd/vnodeQuery.c index 5b17e0f1da..f7b618d18b 100644 --- a/source/dnode/vnode/src/vnd/vnodeQuery.c +++ b/source/dnode/vnode/src/vnd/vnodeQuery.c @@ -47,7 +47,7 @@ int32_t fillTableColCmpr(SMetaReader *reader, SSchemaExt *pExt, int32_t numOfCol return 0; } -int vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { +int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { STableInfoReq infoReq = {0}; STableMetaRsp metaRsp = {0}; SMetaReader mer1 = {0}; @@ -62,15 +62,15 @@ int vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { // decode req if (tDeserializeSTableInfoReq(pMsg->pCont, pMsg->contLen, &infoReq) != 0) { - code = TSDB_CODE_INVALID_MSG; + code = terrno; goto _exit4; } metaRsp.dbId = pVnode->config.dbId; - strcpy(metaRsp.tbName, infoReq.tbName); - memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName)); + (void)strcpy(metaRsp.tbName, infoReq.tbName); + (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName)); - sprintf(tableFName, "%s.%s", infoReq.dbFName, infoReq.tbName); + (void)sprintf(tableFName, "%s.%s", infoReq.dbFName, infoReq.tbName); code = vnodeValidateTableHash(pVnode, tableFName); if (code) { goto _exit4; @@ -89,7 +89,7 @@ int vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { metaRsp.tuid = mer1.me.uid; if (mer1.me.type == TSDB_SUPER_TABLE) { - strcpy(metaRsp.stbName, mer1.me.name); + (void)strcpy(metaRsp.stbName, mer1.me.name); schema = mer1.me.stbEntry.schemaRow; schemaTag = mer1.me.stbEntry.schemaTag; metaRsp.suid = mer1.me.uid; @@ -97,7 +97,7 @@ int vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_NOLOCK); if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit2; - strcpy(metaRsp.stbName, mer2.me.name); + (void)strcpy(metaRsp.stbName, mer2.me.name); metaRsp.suid = mer2.me.uid; schema = mer2.me.stbEntry.schemaRow; schemaTag = mer2.me.stbEntry.schemaTag; @@ -114,10 +114,13 @@ int vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { metaRsp.tversion = schemaTag.version; metaRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (metaRsp.numOfColumns + metaRsp.numOfTags)); metaRsp.pSchemaExt = (SSchemaExt *)taosMemoryCalloc(metaRsp.numOfColumns, sizeof(SSchemaExt)); - - memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols); + if (NULL == metaRsp.pSchemas || NULL == metaRsp.pSchemaExt) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _exit; + } + (void)memcpy(metaRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols); if (schemaTag.nCols) { - memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols); + (void)memcpy(metaRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols); } if (metaRsp.pSchemaExt) { SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1; @@ -134,7 +137,7 @@ int vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { // encode and send response rspLen = tSerializeSTableMetaRsp(NULL, 0, &metaRsp); if (rspLen < 0) { - code = TSDB_CODE_INVALID_MSG; + code = terrno; goto _exit; } @@ -148,7 +151,12 @@ int vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { code = TSDB_CODE_OUT_OF_MEMORY; goto _exit; } - tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp); + + rspLen = tSerializeSTableMetaRsp(pRsp, rspLen, &metaRsp); + if (rspLen < 0) { + code = terrno; + goto _exit; + } _exit: taosMemoryFree(metaRsp.pSchemas); @@ -174,10 +182,10 @@ _exit4: *pMsg = rpcMsg; } - return TSDB_CODE_SUCCESS; + return code; } -int vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { +int32_t vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { STableCfgReq cfgReq = {0}; STableCfgRsp cfgRsp = {0}; SMetaReader mer1 = {0}; @@ -192,14 +200,14 @@ int vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { // decode req if (tDeserializeSTableCfgReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) { - code = TSDB_CODE_INVALID_MSG; + code = terrno; goto _exit; } - strcpy(cfgRsp.tbName, cfgReq.tbName); - memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName)); + (void)strcpy(cfgRsp.tbName, cfgReq.tbName); + (void)memcpy(cfgRsp.dbFName, cfgReq.dbFName, sizeof(cfgRsp.dbFName)); - sprintf(tableFName, "%s.%s", cfgReq.dbFName, cfgReq.tbName); + (void)sprintf(tableFName, "%s.%s", cfgReq.dbFName, cfgReq.tbName); code = vnodeValidateTableHash(pVnode, tableFName); if (code) { goto _exit; @@ -222,24 +230,36 @@ int vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { metaReaderDoInit(&mer2, pVnode->pMeta, META_READER_LOCK); if (metaReaderGetTableEntryByUid(&mer2, mer1.me.ctbEntry.suid) < 0) goto _exit; - strcpy(cfgRsp.stbName, mer2.me.name); + (void)strcpy(cfgRsp.stbName, mer2.me.name); schema = mer2.me.stbEntry.schemaRow; schemaTag = mer2.me.stbEntry.schemaTag; cfgRsp.ttl = mer1.me.ctbEntry.ttlDays; cfgRsp.commentLen = mer1.me.ctbEntry.commentLen; if (mer1.me.ctbEntry.commentLen > 0) { cfgRsp.pComment = taosStrdup(mer1.me.ctbEntry.comment); + if (NULL == cfgRsp.pComment) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _exit; + } } STag *pTag = (STag *)mer1.me.ctbEntry.pTags; cfgRsp.tagsLen = pTag->len; cfgRsp.pTags = taosMemoryMalloc(cfgRsp.tagsLen); - memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen); + if (NULL == cfgRsp.pTags) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _exit; + } + (void)memcpy(cfgRsp.pTags, pTag, cfgRsp.tagsLen); } else if (mer1.me.type == TSDB_NORMAL_TABLE) { schema = mer1.me.ntbEntry.schemaRow; cfgRsp.ttl = mer1.me.ntbEntry.ttlDays; cfgRsp.commentLen = mer1.me.ntbEntry.commentLen; if (mer1.me.ntbEntry.commentLen > 0) { cfgRsp.pComment = taosStrdup(mer1.me.ntbEntry.comment); + if (NULL == cfgRsp.pComment) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _exit; + } } } else { ASSERT(0); @@ -250,9 +270,13 @@ int vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { cfgRsp.pSchemas = (SSchema *)taosMemoryMalloc(sizeof(SSchema) * (cfgRsp.numOfColumns + cfgRsp.numOfTags)); cfgRsp.pSchemaExt = (SSchemaExt *)taosMemoryMalloc(cfgRsp.numOfColumns * sizeof(SSchemaExt)); - memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols); + if (NULL == cfgRsp.pSchemas || NULL == cfgRsp.pSchemaExt) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _exit; + } + (void)memcpy(cfgRsp.pSchemas, schema.pSchema, sizeof(SSchema) * schema.nCols); if (schemaTag.nCols) { - memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols); + (void)memcpy(cfgRsp.pSchemas + schema.nCols, schemaTag.pSchema, sizeof(SSchema) * schemaTag.nCols); } // if (useCompress(cfgRsp.tableType)) { @@ -271,7 +295,7 @@ int vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { // encode and send response rspLen = tSerializeSTableCfgRsp(NULL, 0, &cfgRsp); if (rspLen < 0) { - code = TSDB_CODE_INVALID_MSG; + code = terrno; goto _exit; } @@ -285,7 +309,12 @@ int vnodeGetTableCfg(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { code = TSDB_CODE_OUT_OF_MEMORY; goto _exit; } - tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp); + + rspLen = tSerializeSTableCfgRsp(pRsp, rspLen, &cfgRsp); + if (rspLen < 0) { + code = terrno; + goto _exit; + } _exit: rpcMsg.info = pMsg->info; @@ -307,7 +336,7 @@ _exit: tFreeSTableCfgRsp(&cfgRsp); metaReaderClear(&mer2); metaReaderClear(&mer1); - return TSDB_CODE_SUCCESS; + return code; } static FORCE_INLINE void vnodeFreeSBatchRspMsg(void *p) { @@ -331,7 +360,7 @@ int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) { void *pRsp = NULL; if (tDeserializeSBatchReq(pMsg->pCont, pMsg->contLen, &batchReq)) { - code = TSDB_CODE_OUT_OF_MEMORY; + code = terrno; qError("tDeserializeSBatchReq failed"); goto _exit; } @@ -352,6 +381,10 @@ int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) { for (int32_t i = 0; i < msgNum; ++i) { req = taosArrayGet(batchReq.pMsgs, i); + if (req == NULL) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _exit; + } reqMsg.msgType = req->msgType; reqMsg.pCont = req->msg; @@ -359,13 +392,16 @@ int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) { switch (req->msgType) { case TDMT_VND_TABLE_META: - vnodeGetTableMeta(pVnode, &reqMsg, false); + // error code has been set into reqMsg, no need to handle it here. + (void)vnodeGetTableMeta(pVnode, &reqMsg, false); break; case TDMT_VND_TABLE_CFG: - vnodeGetTableCfg(pVnode, &reqMsg, false); + // error code has been set into reqMsg, no need to handle it here. + (void)vnodeGetTableCfg(pVnode, &reqMsg, false); break; case TDMT_VND_GET_STREAM_PROGRESS: - vnodeGetStreamProgress(pVnode, &reqMsg, false); + // error code has been set into reqMsg, no need to handle it here. + (void)vnodeGetStreamProgress(pVnode, &reqMsg, false); break; default: qError("invalid req msgType %d", req->msgType); @@ -381,24 +417,28 @@ int32_t vnodeGetBatchMeta(SVnode *pVnode, SRpcMsg *pMsg) { rsp.rspCode = reqMsg.code; rsp.msg = reqMsg.pCont; - taosArrayPush(batchRsp.pRsps, &rsp); + if (NULL == taosArrayPush(batchRsp.pRsps, &rsp)) { + qError("taosArrayPush failed"); + code = TSDB_CODE_OUT_OF_MEMORY; + goto _exit; + } } rspSize = tSerializeSBatchRsp(NULL, 0, &batchRsp); if (rspSize < 0) { qError("tSerializeSBatchRsp failed"); - code = TSDB_CODE_OUT_OF_MEMORY; + code = terrno; goto _exit; } pRsp = rpcMallocCont(rspSize); if (pRsp == NULL) { qError("rpcMallocCont %d failed", rspSize); - code = TSDB_CODE_OUT_OF_MEMORY; + code = terrno; goto _exit; } if (tSerializeSBatchRsp(pRsp, rspSize, &batchRsp) < 0) { qError("tSerializeSBatchRsp %d failed", rspSize); - code = TSDB_CODE_OUT_OF_MEMORY; + code = terrno; goto _exit; } @@ -501,7 +541,12 @@ int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) { } int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) { + int32_t code = TSDB_CODE_SUCCESS; SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1); + if (NULL == pCur) { + qError("vnode get all table list failed"); + return TSDB_CODE_FAILED; + } while (1) { tb_uid_t id = metaCtbCursorNext(pCur); @@ -510,11 +555,15 @@ int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) { } STableKeyInfo info = {uid = id}; - taosArrayPush(list, &info); + if (NULL == taosArrayPush(list, &info)) { + qError("taosArrayPush failed"); + code = TSDB_CODE_OUT_OF_MEMORY; + goto _exit; + } } - +_exit: metaCloseCtbCursor(pCur); - return TSDB_CODE_SUCCESS; + return code; } int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) { @@ -522,8 +571,13 @@ int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bo } int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) { + int32_t code = TSDB_CODE_SUCCESS; SVnode *pVnodeObj = pVnode; SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1); + if (NULL == pCur) { + qError("vnode get all table list failed"); + return TSDB_CODE_FAILED; + } while (1) { tb_uid_t id = metaCtbCursorNext(pCur); @@ -531,14 +585,20 @@ int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) { break; } - taosArrayPush(list, &id); + if (NULL == taosArrayPush(list, &id)) { + qError("taosArrayPush failed"); + code = TSDB_CODE_OUT_OF_MEMORY; + goto _exit; + } } +_exit: metaCloseCtbCursor(pCur); - return TSDB_CODE_SUCCESS; + return code; } int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) { + int32_t code = TSDB_CODE_SUCCESS; SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid); if (!pCur) { return TSDB_CODE_FAILED; @@ -550,15 +610,21 @@ int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) { break; } - taosArrayPush(list, &id); + if (NULL == taosArrayPush(list, &id)) { + qError("taosArrayPush failed"); + code = TSDB_CODE_OUT_OF_MEMORY; + goto _exit; + } } +_exit: metaCloseStbCursor(pCur); - return TSDB_CODE_SUCCESS; + return code; } int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1), void *arg) { + int32_t code = TSDB_CODE_SUCCESS; SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid); if (!pCur) { return TSDB_CODE_FAILED; @@ -574,11 +640,16 @@ int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bo continue; } - taosArrayPush(list, &id); + if (NULL == taosArrayPush(list, &id)) { + qError("taosArrayPush failed"); + code = TSDB_CODE_OUT_OF_MEMORY; + goto _exit; + } } +_exit: metaCloseStbCursor(pCur); - return TSDB_CODE_SUCCESS; + return code; } int32_t vnodeGetCtbNum(SVnode *pVnode, int64_t suid, int64_t *num) { @@ -700,16 +771,20 @@ int32_t vnodeGetTimeSeriesNum(SVnode *pVnode, int64_t *num) { *num = 0; int64_t arrSize = taosArrayGetSize(suidList); + int32_t code = TSDB_CODE_SUCCESS; for (int64_t i = 0; i < arrSize; ++i) { tb_uid_t suid = *(tb_uid_t *)taosArrayGet(suidList, i); int64_t ctbNum = 0; int32_t numOfCols = 0; - metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols); - + code = metaGetStbStats(pVnode, suid, &ctbNum, &numOfCols); + if (TSDB_CODE_SUCCESS != code) { + goto _exit; + } *num += ctbNum * (numOfCols - 1); } +_exit: taosArrayDestroy(suidList); return TSDB_CODE_SUCCESS; } @@ -728,7 +803,11 @@ int32_t vnodeGetAllCtbNum(SVnode *pVnode, int64_t *num) { } int64_t ctbNum = 0; - vnodeGetCtbNum(pVnode, id, &ctbNum); + int32_t code = vnodeGetCtbNum(pVnode, id, &ctbNum); + if (TSDB_CODE_SUCCESS != code) { + metaCloseStbCursor(pCur); + return code; + } *num += ctbNum; } @@ -771,14 +850,17 @@ int32_t vnodeGetStreamProgress(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { rsp.vgId = req.vgId; rsp.streamId = req.streamId; rspLen = tSerializeStreamProgressRsp(0, 0, &rsp); + if (rspLen < 0) { + code = terrno; + goto _OVER; + } if (direct) { buf = rpcMallocCont(rspLen); } else { buf = taosMemoryCalloc(1, rspLen); } if (!buf) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - code = -1; + code = TSDB_CODE_OUT_OF_MEMORY; goto _OVER; } } @@ -787,7 +869,11 @@ int32_t vnodeGetStreamProgress(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { code = tqGetStreamExecInfo(pVnode, req.streamId, &rsp.progressDelay, &rsp.fillHisFinished); } if (code == TSDB_CODE_SUCCESS) { - tSerializeStreamProgressRsp(buf, rspLen, &rsp); + rspLen = tSerializeStreamProgressRsp(buf, rspLen, &rsp); + if (rspLen < 0) { + code = terrno; + goto _OVER; + } rpcMsg.pCont = buf; buf = NULL; rpcMsg.contLen = rspLen; diff --git a/source/libs/executor/src/executil.c b/source/libs/executor/src/executil.c index 698ace3e4e..f4489a4f2a 100644 --- a/source/libs/executor/src/executil.c +++ b/source/libs/executor/src/executil.c @@ -1684,14 +1684,17 @@ SqlFunctionCtx* createSqlFunctionCtx(SExprInfo* pExprInfo, int32_t numOfOutput, if (fmIsAggFunc(pCtx->functionId) || fmIsIndefiniteRowsFunc(pCtx->functionId)) { bool isUdaf = fmIsUserDefinedFunc(pCtx->functionId); if (!isUdaf) { + // TODO(xxx) : need handle return value of fmGetFuncExecFuncs. fmGetFuncExecFuncs(pCtx->functionId, &pCtx->fpSet); } else { char* udfName = pExpr->pExpr->_function.pFunctNode->functionName; pCtx->udfName = taosStrdup(udfName); + // TODO(xxx) : need handle return value of fmGetUdafExecFuncs. fmGetUdafExecFuncs(pCtx->functionId, &pCtx->fpSet); } pCtx->fpSet.getEnv(pExpr->pExpr->_function.pFunctNode, &env); } else { + // TODO(xxx) : need handle return value of fmGetScalarFuncExecFuncs. fmGetScalarFuncExecFuncs(pCtx->functionId, &pCtx->sfp); if (pCtx->sfp.getEnv != NULL) { pCtx->sfp.getEnv(pExpr->pExpr->_function.pFunctNode, &env); diff --git a/source/libs/executor/src/executorInt.c b/source/libs/executor/src/executorInt.c index a3e3501114..4ee7030d9a 100644 --- a/source/libs/executor/src/executorInt.c +++ b/source/libs/executor/src/executorInt.c @@ -521,8 +521,8 @@ int32_t setResultRowInitCtx(SResultRow* pResult, SqlFunctionCtx* pCtx, int32_t n if (!pResInfo->initialized) { if (pCtx[i].functionId != -1) { - bool ini = pCtx[i].fpSet.init(&pCtx[i], pResInfo); - if (!ini && fmIsUserDefinedFunc(pCtx[i].functionId)) { + int32_t code = pCtx[i].fpSet.init(&pCtx[i], pResInfo); + if (code != TSDB_CODE_SUCCESS && fmIsUserDefinedFunc(pCtx[i].functionId)){ pResInfo->initialized = false; return TSDB_CODE_UDF_FUNC_EXEC_FAILURE; } diff --git a/source/libs/executor/src/projectoperator.c b/source/libs/executor/src/projectoperator.c index 6167497c21..93901b6b33 100644 --- a/source/libs/executor/src/projectoperator.c +++ b/source/libs/executor/src/projectoperator.c @@ -44,8 +44,8 @@ static int32_t doGenerateSourceData(SOperatorInfo* pOperator); static SSDataBlock* doProjectOperation(SOperatorInfo* pOperator); static SSDataBlock* doApplyIndefinitFunction(SOperatorInfo* pOperator); static SArray* setRowTsColumnOutputInfo(SqlFunctionCtx* pCtx, int32_t numOfCols); -static void setFunctionResultOutput(SOperatorInfo* pOperator, SOptrBasicInfo* pInfo, SAggSupporter* pSup, int32_t stage, - int32_t numOfExprs); +static int32_t setFunctionResultOutput(SOperatorInfo* pOperator, SOptrBasicInfo* pInfo, SAggSupporter* pSup, + int32_t stage, int32_t numOfExprs); static void destroyProjectOperatorInfo(void* param) { if (NULL == param) { @@ -142,7 +142,10 @@ SOperatorInfo* createProjectOperatorInfo(SOperatorInfo* downstream, SProjectPhys } initBasicInfo(&pInfo->binfo, pResBlock); - setFunctionResultOutput(pOperator, &pInfo->binfo, &pInfo->aggSup, MAIN_SCAN, numOfCols); + code = setFunctionResultOutput(pOperator, &pInfo->binfo, &pInfo->aggSup, MAIN_SCAN, numOfCols); + if (code != TSDB_CODE_SUCCESS) { + goto _error; + } code = filterInitFromNode((SNode*)pProjPhyNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0); if (code != TSDB_CODE_SUCCESS) { @@ -447,7 +450,11 @@ SOperatorInfo* createIndefinitOutputOperatorInfo(SOperatorInfo* downstream, SPhy goto _error; } - setFunctionResultOutput(pOperator, &pInfo->binfo, &pInfo->aggSup, MAIN_SCAN, numOfExpr); + code = setFunctionResultOutput(pOperator, &pInfo->binfo, &pInfo->aggSup, MAIN_SCAN, numOfExpr); + if (code != TSDB_CODE_SUCCESS) { + goto _error; + } + code = filterInitFromNode((SNode*)pPhyNode->node.pConditions, &pOperator->exprSupp.pFilterInfo, 0); if (code != TSDB_CODE_SUCCESS) { goto _error; @@ -589,7 +596,8 @@ SSDataBlock* doApplyIndefinitFunction(SOperatorInfo* pOperator) { return (rows > 0) ? pInfo->pRes : NULL; } -void initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size) { +int32_t initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size) { + int32_t code = TSDB_CODE_SUCCESS; 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 || @@ -597,8 +605,12 @@ void initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size) { continue; } - pCtx[j].fpSet.init(&pCtx[j], pCtx[j].resultInfo); + code = pCtx[j].fpSet.init(&pCtx[j], pCtx[j].resultInfo); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } + return code; } /* @@ -610,7 +622,7 @@ void initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size) { * offset[0] offset[1] offset[2] */ // TODO refactor: some function move away -void setFunctionResultOutput(SOperatorInfo* pOperator, SOptrBasicInfo* pInfo, SAggSupporter* pSup, int32_t stage, +int32_t setFunctionResultOutput(SOperatorInfo* pOperator, SOptrBasicInfo* pInfo, SAggSupporter* pSup, int32_t stage, int32_t numOfExprs) { SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo; SqlFunctionCtx* pCtx = pOperator->exprSupp.pCtx; @@ -632,7 +644,7 @@ void setFunctionResultOutput(SOperatorInfo* pOperator, SOptrBasicInfo* pInfo, SA pCtx[i].scanFlag = stage; } - initCtxOutputBuffer(pCtx, numOfExprs); + return initCtxOutputBuffer(pCtx, numOfExprs); } SArray* setRowTsColumnOutputInfo(SqlFunctionCtx* pCtx, int32_t numOfCols) { @@ -841,7 +853,10 @@ int32_t projectApplyFunctions(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBloc // do nothing } else if (fmIsIndefiniteRowsFunc(pfCtx->functionId)) { SResultRowEntryInfo* pResInfo = GET_RES_INFO(pfCtx); - pfCtx->fpSet.init(pfCtx, pResInfo); + code = pfCtx->fpSet.init(pfCtx, pResInfo); + if (TSDB_CODE_SUCCESS != code) { + goto _exit; + } pfCtx->pOutput = taosArrayGet(pResult->pDataBlock, outputSlotId); pfCtx->offset = createNewColModel ? 0 : pResult->info.rows; // set the start offset diff --git a/source/libs/executor/src/scanoperator.c b/source/libs/executor/src/scanoperator.c index 520cba92a1..39883566d1 100644 --- a/source/libs/executor/src/scanoperator.c +++ b/source/libs/executor/src/scanoperator.c @@ -202,8 +202,9 @@ static int32_t doDynamicPruneDataBlock(SOperatorInfo* pOperator, SDataBlockInfo* SResultRowEntryInfo* pEntry = getResultEntryInfo(pRow, i, pTableScanInfo->base.pdInfo.pExprSup->rowEntryInfoOffset); - int32_t reqStatus = fmFuncDynDataRequired(functionId, pEntry, pBlockInfo); - if (reqStatus != FUNC_DATA_REQUIRED_NOT_LOAD) { + int32_t reqStatus; + code = fmFuncDynDataRequired(functionId, pEntry, pBlockInfo, &reqStatus); + if (code != TSDB_CODE_SUCCESS || reqStatus != FUNC_DATA_REQUIRED_NOT_LOAD) { notLoadBlock = false; break; } diff --git a/source/libs/executor/src/timewindowoperator.c b/source/libs/executor/src/timewindowoperator.c index fb3176a2a8..5678400410 100644 --- a/source/libs/executor/src/timewindowoperator.c +++ b/source/libs/executor/src/timewindowoperator.c @@ -1086,12 +1086,13 @@ static SSDataBlock* doBuildIntervalResult(SOperatorInfo* pOperator) { return (rows == 0) ? NULL : pBlock; } -static void doClearWindowImpl(SResultRowPosition* p1, SDiskbasedBuf* pResultBuf, SExprSupp* pSup, int32_t numOfOutput) { +static int32_t doClearWindowImpl(SResultRowPosition* p1, SDiskbasedBuf* pResultBuf, SExprSupp* pSup, int32_t numOfOutput) { SResultRow* pResult = getResultRowByPos(pResultBuf, p1, false); if (NULL == pResult) { - return; + return TSDB_CODE_SUCCESS; } + int32_t code = TSDB_CODE_SUCCESS; SqlFunctionCtx* pCtx = pSup->pCtx; for (int32_t i = 0; i < numOfOutput; ++i) { pCtx[i].resultInfo = getResultEntryInfo(pResult, i, pSup->rowEntryInfoOffset); @@ -1101,15 +1102,19 @@ static void doClearWindowImpl(SResultRowPosition* p1, SDiskbasedBuf* pResultBuf, } pResInfo->initialized = false; if (pCtx[i].functionId != -1) { - pCtx[i].fpSet.init(&pCtx[i], pResInfo); + code = pCtx[i].fpSet.init(&pCtx[i], pResInfo); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } } SFilePage* bufPage = getBufPage(pResultBuf, p1->pageId); if (NULL == bufPage) { - return; + return TSDB_CODE_SUCCESS; } setBufPageDirty(bufPage, true); releaseBufPage(pResultBuf, bufPage); + return TSDB_CODE_SUCCESS; } static void destroyStateWindowOperatorInfo(void* param) { diff --git a/source/libs/function/inc/builtinsimpl.h b/source/libs/function/inc/builtinsimpl.h index b48a617b9c..274ef61feb 100644 --- a/source/libs/function/inc/builtinsimpl.h +++ b/source/libs/function/inc/builtinsimpl.h @@ -49,9 +49,9 @@ int32_t doMinMaxHelper(SqlFunctionCtx* pCtx, int32_t isMinFunc, int32_t* nElems) int32_t saveTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STuplePos* pPos); int32_t updateTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STuplePos* pPos); -const char* loadTupleData(SqlFunctionCtx* pCtx, const STuplePos* pPos); +int32_t loadTupleData(SqlFunctionCtx* pCtx, const STuplePos* pPos, char** value); -bool functionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t functionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t functionFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); int32_t functionFinalizeWithResultBuf(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, char* finalResult); int32_t combineFunction(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx); @@ -74,7 +74,7 @@ int32_t sumInvertFunction(SqlFunctionCtx* pCtx); int32_t sumCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx); -bool minmaxFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t minmaxFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); bool getMinmaxFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); int32_t minFunction(SqlFunctionCtx* pCtx); int32_t maxFunction(SqlFunctionCtx* pCtx); @@ -83,7 +83,7 @@ int32_t minCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx); int32_t maxCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx); bool getAvgFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool avgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t avgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t avgFunction(SqlFunctionCtx* pCtx); int32_t avgFunctionMerge(SqlFunctionCtx* pCtx); int32_t avgFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); @@ -97,7 +97,7 @@ int32_t avgCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx); int32_t getAvgInfoSize(); bool getStddevFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool stddevFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t stddevFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t stddevFunction(SqlFunctionCtx* pCtx); int32_t stddevFunctionMerge(SqlFunctionCtx* pCtx); int32_t stddevFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); @@ -111,18 +111,18 @@ int32_t stddevCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx); int32_t getStddevInfoSize(); bool getLeastSQRFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool leastSQRFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t leastSQRFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t leastSQRFunction(SqlFunctionCtx* pCtx); int32_t leastSQRFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); int32_t leastSQRCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx); bool getPercentileFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool percentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t percentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t percentileFunction(SqlFunctionCtx* pCtx); int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); bool getApercentileFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool apercentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t apercentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t apercentileFunction(SqlFunctionCtx* pCtx); int32_t apercentileFunctionMerge(SqlFunctionCtx* pCtx); int32_t apercentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); @@ -131,16 +131,16 @@ int32_t apercentileCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) int32_t getApercentileMaxSize(); bool getDiffFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool diffFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo); +int32_t diffFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo); int32_t diffFunction(SqlFunctionCtx* pCtx); int32_t diffFunctionByRow(SArray* pCtx); bool getDerivativeFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool derivativeFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo); +int32_t derivativeFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo); int32_t derivativeFunction(SqlFunctionCtx* pCtx); bool getIrateFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool irateFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo); +int32_t irateFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo); int32_t irateFunction(SqlFunctionCtx* pCtx); int32_t irateFunctionMerge(SqlFunctionCtx* pCtx); int32_t irateFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); @@ -165,7 +165,7 @@ EFuncDataRequired lastDynDataReq(void* pRes, SDataBlockInfo* pBlockInfo); int32_t lastRowFunction(SqlFunctionCtx* pCtx); bool getTopBotFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv); -bool topBotFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t topBotFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t topFunction(SqlFunctionCtx* pCtx); int32_t bottomFunction(SqlFunctionCtx* pCtx); int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); @@ -174,7 +174,7 @@ int32_t bottomCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx); int32_t getTopBotInfoSize(int64_t numOfItems); bool getSpreadFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool spreadFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t spreadFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t spreadFunction(SqlFunctionCtx* pCtx); int32_t spreadFunctionMerge(SqlFunctionCtx* pCtx); int32_t spreadFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); @@ -183,7 +183,7 @@ int32_t getSpreadInfoSize(); int32_t spreadCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx); bool getElapsedFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool elapsedFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t elapsedFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t elapsedFunction(SqlFunctionCtx* pCtx); int32_t elapsedFunctionMerge(SqlFunctionCtx* pCtx); int32_t elapsedFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); @@ -192,7 +192,7 @@ int32_t getElapsedInfoSize(); int32_t elapsedCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx); bool getHistogramFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool histogramFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t histogramFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t histogramFunction(SqlFunctionCtx* pCtx); int32_t histogramFunctionPartial(SqlFunctionCtx* pCtx); int32_t histogramFunctionMerge(SqlFunctionCtx* pCtx); @@ -210,7 +210,7 @@ int32_t getHLLInfoSize(); int32_t hllCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx); bool getStateFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool stateFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t stateFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t stateCountFunction(SqlFunctionCtx* pCtx); int32_t stateDurationFunction(SqlFunctionCtx* pCtx); @@ -218,35 +218,35 @@ bool getCsumFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); int32_t csumFunction(SqlFunctionCtx* pCtx); bool getMavgFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool mavgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t mavgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t mavgFunction(SqlFunctionCtx* pCtx); bool getSampleFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool sampleFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t sampleFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t sampleFunction(SqlFunctionCtx* pCtx); int32_t sampleFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); bool getTailFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool tailFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t tailFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t tailFunction(SqlFunctionCtx* pCtx); bool getUniqueFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool uniqueFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t uniqueFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t uniqueFunction(SqlFunctionCtx* pCtx); bool getModeFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool modeFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t modeFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t modeFunction(SqlFunctionCtx* pCtx); int32_t modeFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); bool getTwaFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool twaFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t twaFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t twaFunction(SqlFunctionCtx* pCtx); int32_t twaFinalize(struct SqlFunctionCtx* pCtx, SSDataBlock* pBlock); bool getSelectivityFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv); -bool blockDistSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); +int32_t blockDistSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo); int32_t blockDistFunction(SqlFunctionCtx* pCtx); int32_t blockDistFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); diff --git a/source/libs/function/inc/tfunctionInt.h b/source/libs/function/inc/tfunctionInt.h index b4c48abf37..8d9e42576d 100644 --- a/source/libs/function/inc/tfunctionInt.h +++ b/source/libs/function/inc/tfunctionInt.h @@ -37,7 +37,7 @@ static FORCE_INLINE void initResultRowEntry(SResultRowEntryInfo *pResInfo, int32 pResInfo->complete = false; pResInfo->numOfRes = 0; - memset(GET_ROWCELL_INTERBUF(pResInfo), 0, bufLen); + (void)memset(GET_ROWCELL_INTERBUF(pResInfo), 0, bufLen); } #ifdef __cplusplus diff --git a/source/libs/function/inc/thistogram.h b/source/libs/function/inc/thistogram.h index 20111086cd..5bc6a87c70 100644 --- a/source/libs/function/inc/thistogram.h +++ b/source/libs/function/inc/thistogram.h @@ -55,14 +55,15 @@ typedef struct SHistogramInfo { #endif } SHistogramInfo; -SHistogramInfo* tHistogramCreate(int32_t numOfBins); +int32_t tHistogramCreate(int32_t numOfEntries, SHistogramInfo** pHisto); SHistogramInfo* tHistogramCreateFrom(void* pBuf, int32_t numOfBins); int32_t tHistogramAdd(SHistogramInfo** pHisto, double val); int64_t tHistogramSum(SHistogramInfo* pHisto, double v); -double* tHistogramUniform(SHistogramInfo* pHisto, double* ratio, int32_t num); -SHistogramInfo* tHistogramMerge(SHistogramInfo* pHisto1, SHistogramInfo* pHisto2, int32_t numOfEntries); +int32_t tHistogramUniform(SHistogramInfo* pHisto, double* ratio, int32_t num, double** pVal); +int32_t tHistogramMerge(SHistogramInfo* pHisto1, SHistogramInfo* pHisto2, int32_t numOfEntries, + SHistogramInfo** pResHistogram); void tHistogramDestroy(SHistogramInfo** pHisto); void tHistogramPrint(SHistogramInfo* pHisto); diff --git a/source/libs/function/inc/tpercentile.h b/source/libs/function/inc/tpercentile.h index 34815a34ad..90fb279259 100644 --- a/source/libs/function/inc/tpercentile.h +++ b/source/libs/function/inc/tpercentile.h @@ -67,7 +67,8 @@ typedef struct tMemBucket { SHashObj *groupPagesMap; // disk page map for different groups; } tMemBucket; -tMemBucket *tMemBucketCreate(int32_t nElemSize, int16_t dataType, double minval, double maxval, bool hasWindowOrGroup); +int32_t tMemBucketCreate(int32_t nElemSize, int16_t dataType, double minval, double maxval, bool hasWindowOrGroup, + tMemBucket **pBucket); void tMemBucketDestroy(tMemBucket *pBucket); diff --git a/source/libs/function/src/builtins.c b/source/libs/function/src/builtins.c index f5c98933fd..9c57c682cf 100644 --- a/source/libs/function/src/builtins.c +++ b/source/libs/function/src/builtins.c @@ -25,7 +25,7 @@ static int32_t buildFuncErrMsg(char* pErrBuf, int32_t len, int32_t errCode, const char* pFormat, ...) { va_list vArgList; va_start(vArgList, pFormat); - vsnprintf(pErrBuf, len, pFormat, vArgList); + (void)vsnprintf(pErrBuf, len, pFormat, vArgList); va_end(vArgList); return errCode; } @@ -42,27 +42,24 @@ static int32_t invaildFuncParaValueErrMsg(char* pErrBuf, int32_t len, const char return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_PARA_VALUE, "Invalid parameter value : %s", pFuncName); } -#define TIME_UNIT_INVALID 1 -#define TIME_UNIT_TOO_SMALL 2 - static int32_t validateTimeUnitParam(uint8_t dbPrec, const SValueNode* pVal) { if (!IS_DURATION_VAL(pVal->flag)) { - return TIME_UNIT_INVALID; + return TSDB_CODE_FUNC_TIME_UNIT_INVALID; } if (TSDB_TIME_PRECISION_MILLI == dbPrec && (0 == strcasecmp(pVal->literal, "1u") || 0 == strcasecmp(pVal->literal, "1b"))) { - return TIME_UNIT_TOO_SMALL; + return TSDB_CODE_FUNC_TIME_UNIT_TOO_SMALL; } if (TSDB_TIME_PRECISION_MICRO == dbPrec && 0 == strcasecmp(pVal->literal, "1b")) { - return TIME_UNIT_TOO_SMALL; + return TSDB_CODE_FUNC_TIME_UNIT_TOO_SMALL; } if (pVal->literal[0] != '1' || (pVal->literal[1] != 'u' && pVal->literal[1] != 'a' && pVal->literal[1] != 's' && pVal->literal[1] != 'm' && pVal->literal[1] != 'h' && pVal->literal[1] != 'd' && pVal->literal[1] != 'w' && pVal->literal[1] != 'b')) { - return TIME_UNIT_INVALID; + return TSDB_CODE_FUNC_TIME_UNIT_INVALID; } return TSDB_CODE_SUCCESS; @@ -138,13 +135,13 @@ static bool validateTimezoneFormat(const SValueNode* pVal) { } if (i == 2) { - memcpy(buf, &tz[i - 1], 2); + (void)memcpy(buf, &tz[i - 1], 2); hour = taosStr2Int8(buf, NULL, 10); if (!validateHourRange(hour)) { return false; } } else if (i == 4) { - memcpy(buf, &tz[i - 1], 2); + (void)memcpy(buf, &tz[i - 1], 2); minute = taosStr2Int8(buf, NULL, 10); if (!validateMinuteRange(hour, minute, tz[0])) { return false; @@ -167,13 +164,13 @@ static bool validateTimezoneFormat(const SValueNode* pVal) { } if (i == 2) { - memcpy(buf, &tz[i - 1], 2); + (void)memcpy(buf, &tz[i - 1], 2); hour = taosStr2Int8(buf, NULL, 10); if (!validateHourRange(hour)) { return false; } } else if (i == 5) { - memcpy(buf, &tz[i - 1], 2); + (void)memcpy(buf, &tz[i - 1], 2); minute = taosStr2Int8(buf, NULL, 10); if (!validateMinuteRange(hour, minute, tz[0])) { return false; @@ -215,7 +212,7 @@ static int32_t addTimezoneParam(SNodeList* pList) { time_t t = taosTime(NULL); struct tm tmInfo; if (taosLocalTime(&t, &tmInfo, buf) != NULL) { - strftime(buf, sizeof(buf), "%z", &tmInfo); + (void)strftime(buf, sizeof(buf), "%z", &tmInfo); } int32_t len = (int32_t)strlen(buf); @@ -225,15 +222,27 @@ static int32_t addTimezoneParam(SNodeList* pList) { } pVal->literal = strndup(buf, len); + if (pVal->literal == NULL) { + nodesDestroyNode((SNode*)pVal); + return TSDB_CODE_OUT_OF_MEMORY; + } pVal->translate = true; pVal->node.resType.type = TSDB_DATA_TYPE_BINARY; pVal->node.resType.bytes = len + VARSTR_HEADER_SIZE; pVal->node.resType.precision = TSDB_TIME_PRECISION_MILLI; pVal->datum.p = taosMemoryCalloc(1, len + VARSTR_HEADER_SIZE + 1); + if (pVal->datum.p == NULL) { + nodesDestroyNode((SNode*)pVal); + return TSDB_CODE_OUT_OF_MEMORY; + } varDataSetLen(pVal->datum.p, len); - strncpy(varDataVal(pVal->datum.p), pVal->literal, len); + (void)strncpy(varDataVal(pVal->datum.p), pVal->literal, len); - nodesListAppend(pList, (SNode*)pVal); + int32_t code = nodesListAppend(pList, (SNode*)pVal); + if (TSDB_CODE_SUCCESS != code) { + nodesDestroyNode((SNode*)pVal); + return code; + } return TSDB_CODE_SUCCESS; } @@ -252,7 +261,11 @@ static int32_t addDbPrecisonParam(SNodeList** pList, uint8_t precision) { pVal->datum.i = (int64_t)precision; pVal->typeData = (int64_t)precision; - nodesListMakeAppend(pList, (SNode*)pVal); + int32_t code = nodesListMakeAppend(pList, (SNode*)pVal); + if (TSDB_CODE_SUCCESS != code) { + nodesDestroyNode((SNode*)pVal); + return code; + } return TSDB_CODE_SUCCESS; } @@ -936,13 +949,13 @@ static int32_t translateElapsed(SFunctionNode* pFunc, char* pErrBuf, int32_t len uint8_t dbPrec = pFunc->node.resType.precision; - int32_t ret = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1)); - if (ret == TIME_UNIT_TOO_SMALL) { - return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, + int32_t code = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1)); + if (code == TSDB_CODE_FUNC_TIME_UNIT_TOO_SMALL) { + return buildFuncErrMsg(pErrBuf, len, code, "ELAPSED function time unit parameter should be greater than db precision"); - } else if (ret == TIME_UNIT_INVALID) { + } else if (code == TSDB_CODE_FUNC_TIME_UNIT_INVALID) { return buildFuncErrMsg( - pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, + pErrBuf, len, code, "ELAPSED function time unit parameter should be one of the following: [1b, 1u, 1a, 1s, 1m, 1h, 1d, 1w]"); } } @@ -1062,7 +1075,7 @@ static int8_t validateHistogramBinType(char* binTypeStr) { return binType; } -static bool validateHistogramBinDesc(char* binDescStr, int8_t binType, char* errMsg, int32_t msgLen) { +static int32_t validateHistogramBinDesc(char* binDescStr, int8_t binType, char* errMsg, int32_t msgLen) { const char* msg1 = "HISTOGRAM function requires four parameters"; const char* msg3 = "HISTOGRAM function invalid format for binDesc parameter"; const char* msg4 = "HISTOGRAM function binDesc parameter \"count\" should be in range [1, 1000]"; @@ -1070,6 +1083,7 @@ static bool validateHistogramBinDesc(char* binDescStr, int8_t binType, char* err const char* msg6 = "HISTOGRAM function binDesc parameter \"width\" cannot be 0"; const char* msg7 = "HISTOGRAM function binDesc parameter \"start\" cannot be 0 with \"log_bin\" type"; const char* msg8 = "HISTOGRAM function binDesc parameter \"factor\" cannot be negative or equal to 0/1"; + const char* msg9 = "HISTOGRAM function out of memory"; cJSON* binDesc = cJSON_Parse(binDescStr); int32_t numOfBins; @@ -1078,9 +1092,9 @@ static bool validateHistogramBinDesc(char* binDescStr, int8_t binType, char* err int32_t numOfParams = cJSON_GetArraySize(binDesc); int32_t startIndex; if (numOfParams != 4) { - snprintf(errMsg, msgLen, "%s", msg1); + (void)snprintf(errMsg, msgLen, "%s", msg1); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } cJSON* start = cJSON_GetObjectItem(binDesc, "start"); @@ -1090,22 +1104,22 @@ static bool validateHistogramBinDesc(char* binDescStr, int8_t binType, char* err cJSON* infinity = cJSON_GetObjectItem(binDesc, "infinity"); if (!cJSON_IsNumber(start) || !cJSON_IsNumber(count) || !cJSON_IsBool(infinity)) { - snprintf(errMsg, msgLen, "%s", msg3); + (void)snprintf(errMsg, msgLen, "%s", msg3); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } if (count->valueint <= 0 || count->valueint > 1000) { // limit count to 1000 - snprintf(errMsg, msgLen, "%s", msg4); + (void)snprintf(errMsg, msgLen, "%s", msg4); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } if (isinf(start->valuedouble) || (width != NULL && isinf(width->valuedouble)) || (factor != NULL && isinf(factor->valuedouble)) || (count != NULL && isinf(count->valuedouble))) { - snprintf(errMsg, msgLen, "%s", msg5); + (void)snprintf(errMsg, msgLen, "%s", msg5); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } int32_t counter = (int32_t)count->valueint; @@ -1118,53 +1132,58 @@ static bool validateHistogramBinDesc(char* binDescStr, int8_t binType, char* err } intervals = taosMemoryCalloc(numOfBins, sizeof(double)); + if (intervals == NULL) { + (void)snprintf(errMsg, msgLen, "%s", msg9); + cJSON_Delete(binDesc); + return TSDB_CODE_FAILED; + } if (cJSON_IsNumber(width) && factor == NULL && binType == LINEAR_BIN) { // linear bin process if (width->valuedouble == 0) { - snprintf(errMsg, msgLen, "%s", msg6); + (void)snprintf(errMsg, msgLen, "%s", msg6); taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } for (int i = 0; i < counter + 1; ++i) { intervals[startIndex] = start->valuedouble + i * width->valuedouble; if (isinf(intervals[startIndex])) { - snprintf(errMsg, msgLen, "%s", msg5); + (void)snprintf(errMsg, msgLen, "%s", msg5); taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } startIndex++; } } else if (cJSON_IsNumber(factor) && width == NULL && binType == LOG_BIN) { // log bin process if (start->valuedouble == 0) { - snprintf(errMsg, msgLen, "%s", msg7); + (void)snprintf(errMsg, msgLen, "%s", msg7); taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } if (factor->valuedouble < 0 || factor->valuedouble == 0 || factor->valuedouble == 1) { - snprintf(errMsg, msgLen, "%s", msg8); + (void)snprintf(errMsg, msgLen, "%s", msg8); taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } for (int i = 0; i < counter + 1; ++i) { intervals[startIndex] = start->valuedouble * pow(factor->valuedouble, i * 1.0); if (isinf(intervals[startIndex])) { - snprintf(errMsg, msgLen, "%s", msg5); + (void)snprintf(errMsg, msgLen, "%s", msg5); taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } startIndex++; } } else { - snprintf(errMsg, msgLen, "%s", msg3); + (void)snprintf(errMsg, msgLen, "%s", msg3); taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } if (infinity->valueint == true) { @@ -1172,7 +1191,7 @@ static bool validateHistogramBinDesc(char* binDescStr, int8_t binType, char* err intervals[numOfBins - 1] = INFINITY; // in case of desc bin orders, -inf/inf should be swapped if (numOfBins < 4) { - return false; + return TSDB_CODE_FAILED; } if (intervals[1] > intervals[numOfBins - 2]) { @@ -1181,46 +1200,51 @@ static bool validateHistogramBinDesc(char* binDescStr, int8_t binType, char* err } } else if (cJSON_IsArray(binDesc)) { /* user input bins */ if (binType != USER_INPUT_BIN) { - snprintf(errMsg, msgLen, "%s", msg3); + (void)snprintf(errMsg, msgLen, "%s", msg3); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } numOfBins = cJSON_GetArraySize(binDesc); intervals = taosMemoryCalloc(numOfBins, sizeof(double)); + if (intervals == NULL) { + (void)snprintf(errMsg, msgLen, "%s", msg9); + cJSON_Delete(binDesc); + return TSDB_CODE_OUT_OF_MEMORY; + } cJSON* bin = binDesc->child; if (bin == NULL) { - snprintf(errMsg, msgLen, "%s", msg3); + (void)snprintf(errMsg, msgLen, "%s", msg3); taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } int i = 0; while (bin) { intervals[i] = bin->valuedouble; if (!cJSON_IsNumber(bin)) { - snprintf(errMsg, msgLen, "%s", msg3); + (void)snprintf(errMsg, msgLen, "%s", msg3); taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } if (i != 0 && intervals[i] <= intervals[i - 1]) { - snprintf(errMsg, msgLen, "%s", msg3); + (void)snprintf(errMsg, msgLen, "%s", msg3); taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } bin = bin->next; i++; } } else { - snprintf(errMsg, msgLen, "%s", msg3); + (void)snprintf(errMsg, msgLen, "%s", msg3); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } cJSON_Delete(binDesc); taosMemoryFree(intervals); - return true; + return TSDB_CODE_SUCCESS; } static int32_t translateHistogram(SFunctionNode* pFunc, char* pErrBuf, int32_t len) { @@ -1265,7 +1289,7 @@ static int32_t translateHistogram(SFunctionNode* pFunc, char* pErrBuf, int32_t l if (i == 2) { char errMsg[128] = {0}; binDesc = varDataVal(pValue->datum.p); - if (!validateHistogramBinDesc(binDesc, binType, errMsg, (int32_t)sizeof(errMsg))) { + if (TSDB_CODE_SUCCESS != validateHistogramBinDesc(binDesc, binType, errMsg, (int32_t)sizeof(errMsg))) { return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, errMsg); } } @@ -1323,7 +1347,7 @@ static int32_t translateHistogramImpl(SFunctionNode* pFunc, char* pErrBuf, int32 if (i == 2) { char errMsg[128] = {0}; binDesc = varDataVal(pValue->datum.p); - if (!validateHistogramBinDesc(binDesc, binType, errMsg, (int32_t)sizeof(errMsg))) { + if (TSDB_CODE_SUCCESS != validateHistogramBinDesc(binDesc, binType, errMsg, (int32_t)sizeof(errMsg))) { return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, errMsg); } } @@ -1507,12 +1531,12 @@ static int32_t translateStateDuration(SFunctionNode* pFunc, char* pErrBuf, int32 if (numOfParams == 4) { uint8_t dbPrec = pFunc->node.resType.precision; - int32_t ret = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 3)); - if (ret == TIME_UNIT_TOO_SMALL) { - return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, + int32_t code = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 3)); + if (code == TSDB_CODE_FUNC_TIME_UNIT_TOO_SMALL) { + return buildFuncErrMsg(pErrBuf, len, code, "STATEDURATION function time unit parameter should be greater than db precision"); - } else if (ret == TIME_UNIT_INVALID) { - return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, + } else if (code == TSDB_CODE_FUNC_TIME_UNIT_INVALID) { + return buildFuncErrMsg(pErrBuf, len, code, "STATEDURATION function time unit parameter should be one of the following: [1b, 1u, 1a, " "1s, 1m, 1h, 1d, 1w]"); } @@ -2268,13 +2292,13 @@ static int32_t translateTimeTruncate(SFunctionNode* pFunc, char* pErrBuf, int32_ } uint8_t dbPrec = pFunc->node.resType.precision; - int32_t ret = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1)); - if (ret == TIME_UNIT_TOO_SMALL) { - return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, + int32_t code = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1)); + if (code == TSDB_CODE_FUNC_TIME_UNIT_TOO_SMALL) { + return buildFuncErrMsg(pErrBuf, len, code, "TIMETRUNCATE function time unit parameter should be greater than db precision"); - } else if (ret == TIME_UNIT_INVALID) { + } else if (code == TSDB_CODE_FUNC_TIME_UNIT_INVALID) { return buildFuncErrMsg( - pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, + pErrBuf, len, code, "TIMETRUNCATE function time unit parameter should be one of the following: [1b, 1u, 1a, 1s, 1m, 1h, 1d, 1w]"); } @@ -2291,7 +2315,7 @@ static int32_t translateTimeTruncate(SFunctionNode* pFunc, char* pErrBuf, int32_ // add database precision as param - int32_t code = addDbPrecisonParam(&pFunc->pParameterList, dbPrec); + code = addDbPrecisonParam(&pFunc->pParameterList, dbPrec); if (code != TSDB_CODE_SUCCESS) { return code; } @@ -2330,13 +2354,13 @@ static int32_t translateTimeDiff(SFunctionNode* pFunc, char* pErrBuf, int32_t le uint8_t dbPrec = pFunc->node.resType.precision; if (3 == numOfParams) { - int32_t ret = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 2)); - if (ret == TIME_UNIT_TOO_SMALL) { - return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, + int32_t code = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 2)); + if (code == TSDB_CODE_FUNC_TIME_UNIT_TOO_SMALL) { + return buildFuncErrMsg(pErrBuf, len, code, "TIMEDIFF function time unit parameter should be greater than db precision"); - } else if (ret == TIME_UNIT_INVALID) { + } else if (code == TSDB_CODE_FUNC_TIME_UNIT_INVALID) { return buildFuncErrMsg( - pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, + pErrBuf, len, code, "TIMEDIFF function time unit parameter should be one of the following: [1b, 1u, 1a, 1s, 1m, 1h, 1d, 1w]"); } } diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 5f6565be1f..1197891cab 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -429,7 +429,7 @@ typedef struct SGroupKeyInfo { (_p).val = (_v); \ } while (0) -int32_t funcInputUpdate(SqlFunctionCtx* pCtx) { +void funcInputUpdate(SqlFunctionCtx* pCtx) { SFuncInputRowIter* pIter = &pCtx->rowIter; if (!pCtx->bInputFinished) { @@ -448,11 +448,9 @@ int32_t funcInputUpdate(SqlFunctionCtx* pCtx) { } else { pIter->finalRow = true; } - - return TSDB_CODE_SUCCESS; } -bool funcInputGetNextRowDescPk(SFuncInputRowIter* pIter, SFuncInputRow* pRow) { +int32_t funcInputGetNextRowDescPk(SFuncInputRowIter* pIter, SFuncInputRow* pRow, bool *res) { if (pIter->finalRow) { if (pIter->hasPrev) { pRow->ts = pIter->prevBlockTsEnd; @@ -462,29 +460,41 @@ bool funcInputGetNextRowDescPk(SFuncInputRowIter* pIter, SFuncInputRow* pRow) { pRow->rowIndex = 0; pIter->hasPrev = false; - return true; + *res = true; + return TSDB_CODE_SUCCESS; } else { - return false; + *res = false; + return TSDB_CODE_SUCCESS; } } if (pIter->hasPrev) { if (pIter->prevBlockTsEnd == pIter->tsList[pIter->inputEndIndex]) { - blockDataDestroy(pIter->pPrevRowBlock); + (void)blockDataDestroy(pIter->pPrevRowBlock); pIter->pPrevRowBlock = blockDataExtractBlock(pIter->pSrcBlock, pIter->inputEndIndex, 1); pIter->prevIsDataNull = colDataIsNull_f(pIter->pDataCol->nullbitmap, pIter->inputEndIndex); pIter->pPrevData = taosMemoryMalloc(pIter->pDataCol->info.bytes); + if (NULL == pIter->pPrevData) { + qError("out of memory when function get input row."); + return TSDB_CODE_OUT_OF_MEMORY; + } char* srcData = colDataGetData(pIter->pDataCol, pIter->inputEndIndex); - memcpy(pIter->pPrevData, srcData, pIter->pDataCol->info.bytes); + (void)memcpy(pIter->pPrevData, srcData, pIter->pDataCol->info.bytes); pIter->pPrevPk = taosMemoryMalloc(pIter->pPkCol->info.bytes); + if (NULL == pIter->pPrevPk) { + qError("out of memory when function get input row."); + taosMemoryFree(pIter->pPrevData); + return TSDB_CODE_OUT_OF_MEMORY; + } char* pkData = colDataGetData(pIter->pPkCol, pIter->inputEndIndex); - memcpy(pIter->pPrevPk, pkData, pIter->pPkCol->info.bytes); + (void)memcpy(pIter->pPrevPk, pkData, pIter->pPkCol->info.bytes); pIter->pPrevRowBlock = blockDataExtractBlock(pIter->pSrcBlock, pIter->inputEndIndex, 1); pIter->hasPrev = true; - return false; + *res = false; + return TSDB_CODE_SUCCESS; } else { int32_t idx = pIter->rowIndex; while (pIter->tsList[idx] == pIter->prevBlockTsEnd) { @@ -506,7 +516,8 @@ bool funcInputGetNextRowDescPk(SFuncInputRowIter* pIter, SFuncInputRow* pRow) { } pIter->hasPrev = false; pIter->rowIndex = idx; - return true; + *res = true; + return TSDB_CODE_SUCCESS; } } else { TSKEY tsEnd = pIter->tsList[pIter->inputEndIndex]; @@ -522,18 +533,29 @@ bool funcInputGetNextRowDescPk(SFuncInputRowIter* pIter, SFuncInputRow* pRow) { pRow->block = pIter->pSrcBlock; pIter->rowIndex = idx + 1; - return true; + *res = true; + return TSDB_CODE_SUCCESS; } else { pIter->hasPrev = true; pIter->prevBlockTsEnd = tsEnd; pIter->prevIsDataNull = colDataIsNull_f(pIter->pDataCol->nullbitmap, pIter->inputEndIndex); pIter->pPrevData = taosMemoryMalloc(pIter->pDataCol->info.bytes); - memcpy(pIter->pPrevData, colDataGetData(pIter->pDataCol, pIter->inputEndIndex), pIter->pDataCol->info.bytes); + if (NULL == pIter->pPrevData) { + qError("out of memory when function get input row."); + return TSDB_CODE_OUT_OF_MEMORY; + } + (void)memcpy(pIter->pPrevData, colDataGetData(pIter->pDataCol, pIter->inputEndIndex), pIter->pDataCol->info.bytes); pIter->pPrevPk = taosMemoryMalloc(pIter->pPkCol->info.bytes); - memcpy(pIter->pPrevPk, colDataGetData(pIter->pPkCol, pIter->inputEndIndex), pIter->pPkCol->info.bytes); + if (NULL == pIter->pPrevPk) { + qError("out of memory when function get input row."); + taosMemoryFree(pIter->pPrevData); + return TSDB_CODE_OUT_OF_MEMORY; + } + (void)memcpy(pIter->pPrevPk, colDataGetData(pIter->pPkCol, pIter->inputEndIndex), pIter->pPkCol->info.bytes); pIter->pPrevRowBlock = blockDataExtractBlock(pIter->pSrcBlock, pIter->inputEndIndex, 1); - return false; + *res = false; + return TSDB_CODE_SUCCESS; } } } @@ -602,24 +624,27 @@ bool funcInputGetNextRowNoPk(SFuncInputRowIter *pIter, SFuncInputRow* pRow) { } } -bool funcInputGetNextRow(SqlFunctionCtx* pCtx, SFuncInputRow* pRow) { +int32_t funcInputGetNextRow(SqlFunctionCtx* pCtx, SFuncInputRow* pRow, bool *res) { SFuncInputRowIter* pIter = &pCtx->rowIter; if (pCtx->hasPrimaryKey) { if (pCtx->order == TSDB_ORDER_ASC) { - return funcInputGetNextRowAscPk(pIter, pRow); + *res = funcInputGetNextRowAscPk(pIter, pRow); + return TSDB_CODE_SUCCESS; } else { - return funcInputGetNextRowDescPk(pIter, pRow); + return funcInputGetNextRowDescPk(pIter, pRow, res); } } else { - return funcInputGetNextRowNoPk(pIter, pRow); + *res = funcInputGetNextRowNoPk(pIter, pRow); + return TSDB_CODE_SUCCESS; } + return TSDB_CODE_SUCCESS; } // This function append the selectivity to subsidiaries function context directly, without fetching data // from intermediate disk based buf page -void appendSelectivityCols(SqlFunctionCtx* pCtx, SSDataBlock* pSrcBlock, int32_t rowIndex, int32_t pos) { +int32_t appendSelectivityCols(SqlFunctionCtx* pCtx, SSDataBlock* pSrcBlock, int32_t rowIndex, int32_t pos) { if (pCtx->subsidiaries.num <= 0) { - return; + return TSDB_CODE_SUCCESS; } for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) { @@ -630,6 +655,9 @@ void appendSelectivityCols(SqlFunctionCtx* pCtx, SSDataBlock* pSrcBlock, int32_t int32_t srcSlotId = pFuncParam->pCol->slotId; SColumnInfoData* pSrcCol = taosArrayGet(pSrcBlock->pDataBlock, srcSlotId); + if (NULL == pSrcCol) { + return TSDB_CODE_OUT_OF_RANGE; + } char* pData = colDataGetData(pSrcCol, rowIndex); @@ -637,43 +665,52 @@ void appendSelectivityCols(SqlFunctionCtx* pCtx, SSDataBlock* pSrcBlock, int32_t int32_t dstSlotId = pc->pExpr->base.resSchema.slotId; SColumnInfoData* pDstCol = taosArrayGet(pCtx->pDstBlock->pDataBlock, dstSlotId); - + if (NULL == pDstCol) { + return TSDB_CODE_OUT_OF_RANGE; + } if (colDataIsNull_s(pSrcCol, rowIndex) == true) { colDataSetNULL(pDstCol, pos); } else { - colDataSetVal(pDstCol, pos, pData, false); + int32_t code = colDataSetVal(pDstCol, pos, pData, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } } + return TSDB_CODE_SUCCESS; } bool funcInputGetNextRowIndex(SInputColumnInfoData* pInput, int32_t from, bool firstOccur, int32_t* pRowIndex, int32_t* nextFrom); -static int32_t firstLastTransferInfoImpl(SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst); +static bool firstLastTransferInfoImpl(SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst); -bool functionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { +int32_t functionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { if (pResultInfo->initialized) { - return false; + return TSDB_CODE_SUCCESS; // already initialized } if (pCtx->pOutput != NULL) { - memset(pCtx->pOutput, 0, (size_t)pCtx->resDataInfo.bytes); + (void)memset(pCtx->pOutput, 0, (size_t)pCtx->resDataInfo.bytes); } initResultRowEntry(pResultInfo, pCtx->resDataInfo.interBufSize); - return true; + return TSDB_CODE_SUCCESS; } int32_t functionFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { + int32_t code = TSDB_CODE_SUCCESS; int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); - + if (NULL == pCol) { + return TSDB_CODE_OUT_OF_RANGE; + } SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0; char* in = GET_ROWCELL_INTERBUF(pResInfo); - colDataSetVal(pCol, pBlock->info.rows, in, pResInfo->isNullRes); + code = colDataSetVal(pCol, pBlock->info.rows, in, pResInfo->isNullRes); - return pResInfo->numOfRes; + return code; } int32_t firstCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { @@ -684,9 +721,7 @@ int32_t firstCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx); SFirstLastRes* pSBuf = GET_ROWCELL_INTERBUF(pSResInfo); - if (TSDB_CODE_SUCCESS == firstLastTransferInfoImpl(pSBuf, pDBuf, true)) { - pDBuf->hasResult = true; - } + pDBuf->hasResult = firstLastTransferInfoImpl(pSBuf, pDBuf, true); pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes); pDResInfo->isNullRes &= pSResInfo->isNullRes; @@ -696,14 +731,16 @@ int32_t firstCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { int32_t functionFinalizeWithResultBuf(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, char* finalResult) { int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); - + if (NULL == pCol) { + return TSDB_CODE_OUT_OF_RANGE; + } SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0; char* in = finalResult; - colDataSetVal(pCol, pBlock->info.rows, in, pResInfo->isNullRes); + int32_t code = colDataSetVal(pCol, pBlock->info.rows, in, pResInfo->isNullRes); - return pResInfo->numOfRes; + return code; } EFuncDataRequired countDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) { @@ -972,9 +1009,12 @@ EFuncDataRequired statisDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWin return FUNC_DATA_REQUIRED_SMA_LOAD; } -bool minmaxFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { - if (!functionSetup(pCtx, pResultInfo)) { - return false; // not initialized since it has been initialized +int32_t minmaxFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { + if (pResultInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; // not initialized since it has been initialized } SMinmaxResInfo* buf = GET_ROWCELL_INTERBUF(pResultInfo); @@ -983,7 +1023,7 @@ bool minmaxFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) buf->nullTupleSaved = false; buf->nullTuplePos.pageId = -1; - return true; + return TSDB_CODE_SUCCESS; } bool getMinmaxFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) { @@ -1025,6 +1065,9 @@ int32_t minmaxFunctionFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t currentRow = pBlock->info.rows; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + return TSDB_CODE_OUT_OF_RANGE; + } pEntryInfo->isNullRes = (pEntryInfo->numOfRes == 0) ? 1 : 0; // NOTE: do nothing change it, for performance issue @@ -1097,12 +1140,12 @@ int32_t setSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, const STu if ((pCtx->saveHandle.pBuf != NULL && pTuplePos->pageId != -1) || (pCtx->saveHandle.pState && pTuplePos->streamTupleKey.ts > 0)) { int32_t numOfCols = pCtx->subsidiaries.num; - const char* p = loadTupleData(pCtx, pTuplePos); - if (p == NULL) { - terrno = TSDB_CODE_NOT_FOUND; + char* p = NULL; + int32_t code = loadTupleData(pCtx, pTuplePos, &p); + if (p == NULL || TSDB_CODE_SUCCESS != code) { qError("Load tuple data failed since %s, groupId:%" PRIu64 ", ts:%" PRId64, terrstr(), pTuplePos->streamTupleKey.groupId, pTuplePos->streamTupleKey.ts); - return terrno; + return TSDB_CODE_NOT_FOUND; } bool* nullList = (bool*)p; @@ -1120,10 +1163,16 @@ int32_t setSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, const STu } SColumnInfoData* pDstCol = taosArrayGet(pBlock->pDataBlock, dstSlotId); + if (NULL == pDstCol) { + return TSDB_CODE_OUT_OF_RANGE; + } if (nullList[j]) { colDataSetNULL(pDstCol, rowIndex); } else { - colDataSetVal(pDstCol, rowIndex, pStart, false); + code = colDataSetVal(pDstCol, rowIndex, pStart, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } pStart += pDstCol->info.bytes; } @@ -1134,11 +1183,12 @@ int32_t setSelectivityValue(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, const STu // This function append the selectivity to subsidiaries function context directly, without fetching data // from intermediate disk based buf page -void appendSelectivityValue(SqlFunctionCtx* pCtx, int32_t rowIndex, int32_t pos) { +int32_t appendSelectivityValue(SqlFunctionCtx* pCtx, int32_t rowIndex, int32_t pos) { if (pCtx->subsidiaries.num <= 0) { - return; + return TSDB_CODE_SUCCESS; } + int32_t code = TSDB_CODE_SUCCESS; for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) { SqlFunctionCtx* pc = pCtx->subsidiaries.pCtx[j]; @@ -1147,6 +1197,9 @@ void appendSelectivityValue(SqlFunctionCtx* pCtx, int32_t rowIndex, int32_t pos) int32_t srcSlotId = pFuncParam->pCol->slotId; SColumnInfoData* pSrcCol = taosArrayGet(pCtx->pSrcBlock->pDataBlock, srcSlotId); + if (NULL == pSrcCol) { + return TSDB_CODE_OUT_OF_RANGE; + } char* pData = colDataGetData(pSrcCol, rowIndex); @@ -1154,13 +1207,20 @@ void appendSelectivityValue(SqlFunctionCtx* pCtx, int32_t rowIndex, int32_t pos) int32_t dstSlotId = pc->pExpr->base.resSchema.slotId; SColumnInfoData* pDstCol = taosArrayGet(pCtx->pDstBlock->pDataBlock, dstSlotId); + if (NULL == pDstCol) { + return TSDB_CODE_OUT_OF_RANGE; + } if (colDataIsNull_s(pSrcCol, rowIndex) == true) { colDataSetNULL(pDstCol, pos); } else { - colDataSetVal(pDstCol, pos, pData, false); + code = colDataSetVal(pDstCol, pos, pData, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } } + return code; } void replaceTupleData(STuplePos* pDestPos, STuplePos* pSourcePos) { *pDestPos = *pSourcePos; } @@ -1244,14 +1304,17 @@ bool getStddevFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) { return true; } -bool stddevFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { - if (!functionSetup(pCtx, pResultInfo)) { - return false; +int32_t stddevFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { + if (pResultInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } SStddevRes* pRes = GET_ROWCELL_INTERBUF(pResultInfo); - memset(pRes, 0, sizeof(SStddevRes)); - return true; + (void)memset(pRes, 0, sizeof(SStddevRes)); + return TSDB_CODE_SUCCESS; } int32_t stddevFunction(SqlFunctionCtx* pCtx) { @@ -1588,16 +1651,23 @@ int32_t stddevPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t resultBytes = getStddevInfoSize(); char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char)); - memcpy(varDataVal(res), pInfo, resultBytes); + if (NULL == res) { + return TSDB_CODE_OUT_OF_MEMORY; + } + (void)memcpy(varDataVal(res), pInfo, resultBytes); varDataSetLen(res, resultBytes); int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + taosMemoryFree(res); + return TSDB_CODE_OUT_OF_RANGE; + } - colDataSetVal(pCol, pBlock->info.rows, res, false); + int32_t code = colDataSetVal(pCol, pBlock->info.rows, res, false); taosMemoryFree(res); - return pResInfo->numOfRes; + return code; } int32_t stddevCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { @@ -1620,16 +1690,19 @@ bool getLeastSQRFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) { return true; } -bool leastSQRFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { - if (!functionSetup(pCtx, pResultInfo)) { - return false; +int32_t leastSQRFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { + if (pResultInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } SLeastSQRInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo); GET_TYPED_DATA(pInfo->startVal, double, pCtx->param[1].param.nType, &pCtx->param[1].param.i); GET_TYPED_DATA(pInfo->stepVal, double, pCtx->param[2].param.nType, &pCtx->param[2].param.i); - return true; + return TSDB_CODE_SUCCESS; } int32_t leastSQRFunction(SqlFunctionCtx* pCtx) { @@ -1798,11 +1871,14 @@ int32_t leastSQRFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + return TSDB_CODE_OUT_OF_RANGE; + } int32_t currentRow = pBlock->info.rows; if (0 == pInfo->num) { colDataSetNULL(pCol, currentRow); - return 0; + return TSDB_CODE_SUCCESS; } double(*param)[3] = pInfo->matrix; @@ -1815,7 +1891,7 @@ int32_t leastSQRFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { if (0 == param00) { colDataSetNULL(pCol, currentRow); - return 0; + return TSDB_CODE_SUCCESS; } // param[0][1] = 0; @@ -1830,19 +1906,19 @@ int32_t leastSQRFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { char interceptBuf[64] = {0}; int n = snprintf(slopBuf, 64, "%.6lf", param02); if (n > LEASTSQUARES_DOUBLE_ITEM_LENGTH) { - snprintf(slopBuf, 64, "%." DOUBLE_PRECISION_DIGITS, param02); + (void)snprintf(slopBuf, 64, "%." DOUBLE_PRECISION_DIGITS, param02); } n = snprintf(interceptBuf, 64, "%.6lf", param12); if (n > LEASTSQUARES_DOUBLE_ITEM_LENGTH) { - snprintf(interceptBuf, 64, "%." DOUBLE_PRECISION_DIGITS, param12); + (void)snprintf(interceptBuf, 64, "%." DOUBLE_PRECISION_DIGITS, param12); } size_t len = snprintf(varDataVal(buf), sizeof(buf) - VARSTR_HEADER_SIZE, "{slop:%s, intercept:%s}", slopBuf, interceptBuf); varDataSetLen(buf, len); - colDataSetVal(pCol, currentRow, buf, pResInfo->isNullRes); + int32_t code = colDataSetVal(pCol, currentRow, buf, pResInfo->isNullRes); - return pResInfo->numOfRes; + return code; } int32_t leastSQRCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { @@ -1872,9 +1948,12 @@ bool getPercentileFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) { return true; } -bool percentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { - if (!functionSetup(pCtx, pResultInfo)) { - return false; +int32_t percentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { + if (pResultInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } // in the first round, get the min-max value of all involved data @@ -1883,10 +1962,11 @@ bool percentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultI SET_DOUBLE_VAL(&pInfo->maxval, -DBL_MAX); pInfo->numOfElems = 0; - return true; + return TSDB_CODE_SUCCESS; } int32_t percentileFunction(SqlFunctionCtx* pCtx) { + int32_t code = TSDB_CODE_SUCCESS; int32_t numOfElems = 0; SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); @@ -1905,7 +1985,10 @@ int32_t percentileFunction(SqlFunctionCtx* pCtx) { pResInfo->complete = true; return TSDB_CODE_SUCCESS; } else { - pInfo->pMemBucket = tMemBucketCreate(pCol->info.bytes, type, pInfo->minval, pInfo->maxval, pCtx->hasWindowOrGroup); + code = tMemBucketCreate(pCol->info.bytes, type, pInfo->minval, pInfo->maxval, pCtx->hasWindowOrGroup, &pInfo->pMemBucket); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } } @@ -1966,7 +2049,7 @@ int32_t percentileFunction(SqlFunctionCtx* pCtx) { char* data = colDataGetData(pCol, i); numOfElems += 1; - int32_t code = tMemBucketPut(pInfo->pMemBucket, data, 1); + code = tMemBucketPut(pInfo->pMemBucket, data, 1); if (code != TSDB_CODE_SUCCESS) { tMemBucketDestroy(pInfo->pMemBucket); return code; @@ -2012,12 +2095,19 @@ int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _fin_error; + } varDataSetLen(buf, len); - colDataSetVal(pCol, pBlock->info.rows, buf, false); + code = colDataSetVal(pCol, pBlock->info.rows, buf, false); + if (code != TSDB_CODE_SUCCESS) { + goto _fin_error; + } tMemBucketDestroy(pMemBucket); - return pResInfo->numOfRes; + return TSDB_CODE_SUCCESS; } else { SVariant* pVal = &pCtx->param[1].param; @@ -2076,9 +2166,12 @@ static void buildTDigestInfo(SAPercentileInfo* pInfo) { pInfo->pTDigest = (TDigest*)((char*)pInfo + sizeof(SAPercentileInfo)); } -bool apercentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { - if (!functionSetup(pCtx, pResultInfo)) { - return false; +int32_t apercentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { + if (pResultInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } SAPercentileInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo); @@ -2092,7 +2185,7 @@ bool apercentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResult } else if (pCtx->numOfParams == 3) { pInfo->algo = getApercentileAlgo(varDataVal(pCtx->param[2].param.pz)); if (pInfo->algo == APERCT_ALGO_UNKNOWN) { - return false; + return TSDB_CODE_FUNC_FUNTION_PARA_VALUE; } } @@ -2106,7 +2199,7 @@ bool apercentileFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResult pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries, pInfo->pHisto, pInfo->pHisto->elems); } - return true; + return TSDB_CODE_SUCCESS; } int32_t apercentileFunction(SqlFunctionCtx* pCtx) { @@ -2152,8 +2245,8 @@ int32_t apercentileFunction(SqlFunctionCtx* pCtx) { double v = 0; GET_TYPED_DATA(v, double, type, data); int32_t code = tHistogramAdd(&pInfo->pHisto, v); - if (code != 0) { - return TSDB_CODE_FAILED; + if (code != TSDB_CODE_SUCCESS) { + return code; } } @@ -2166,7 +2259,7 @@ int32_t apercentileFunction(SqlFunctionCtx* pCtx) { return TSDB_CODE_SUCCESS; } -static void apercentileTransferInfo(SAPercentileInfo* pInput, SAPercentileInfo* pOutput, bool* hasRes) { +static int32_t apercentileTransferInfo(SAPercentileInfo* pInput, SAPercentileInfo* pOutput, bool* hasRes) { pOutput->percent = pInput->percent; pOutput->algo = pInput->algo; if (pOutput->algo == APERCT_ALGO_TDIGEST) { @@ -2174,7 +2267,7 @@ static void apercentileTransferInfo(SAPercentileInfo* pInput, SAPercentileInfo* tdigestAutoFill(pInput->pTDigest, COMPRESSION); if (pInput->pTDigest->num_centroids == 0 && pInput->pTDigest->num_buffered_pts == 0) { - return; + return TSDB_CODE_SUCCESS; } if (hasRes) { @@ -2186,7 +2279,7 @@ static void apercentileTransferInfo(SAPercentileInfo* pInput, SAPercentileInfo* tdigestAutoFill(pTDigest, COMPRESSION); if (pTDigest->num_centroids <= 0 && pTDigest->num_buffered_pts == 0) { - memcpy(pTDigest, pInput->pTDigest, (size_t)TDIGEST_SIZE(COMPRESSION)); + (void)memcpy(pTDigest, pInput->pTDigest, (size_t)TDIGEST_SIZE(COMPRESSION)); tdigestAutoFill(pTDigest, COMPRESSION); } else { tdigestMerge(pTDigest, pInput->pTDigest); @@ -2194,7 +2287,7 @@ static void apercentileTransferInfo(SAPercentileInfo* pInput, SAPercentileInfo* } else { buildHistogramInfo(pInput); if (pInput->pHisto->numOfElems <= 0) { - return; + return TSDB_CODE_SUCCESS; } if (hasRes) { @@ -2205,7 +2298,7 @@ static void apercentileTransferInfo(SAPercentileInfo* pInput, SAPercentileInfo* SHistogramInfo* pHisto = pOutput->pHisto; if (pHisto->numOfElems <= 0) { - memcpy(pHisto, pInput->pHisto, sizeof(SHistogramInfo) + sizeof(SHistBin) * (MAX_HISTOGRAM_BIN + 1)); + (void)memcpy(pHisto, pInput->pHisto, sizeof(SHistogramInfo) + sizeof(SHistBin) * (MAX_HISTOGRAM_BIN + 1)); pHisto->elems = (SHistBin*)((char*)pHisto + sizeof(SHistogramInfo)); qDebug("%s merge histo, total:%" PRId64 ", entry:%d, %p", __FUNCTION__, pHisto->numOfElems, pHisto->numOfEntries, @@ -2215,8 +2308,13 @@ static void apercentileTransferInfo(SAPercentileInfo* pInput, SAPercentileInfo* qDebug("%s input histogram, elem:%" PRId64 ", entry:%d, %p", __FUNCTION__, pHisto->numOfElems, pHisto->numOfEntries, pInput->pHisto); - SHistogramInfo* pRes = tHistogramMerge(pHisto, pInput->pHisto, MAX_HISTOGRAM_BIN); - memcpy(pHisto, pRes, sizeof(SHistogramInfo) + sizeof(SHistBin) * MAX_HISTOGRAM_BIN); + SHistogramInfo* pRes = NULL; + int32_t code = tHistogramMerge(pHisto, pInput->pHisto, MAX_HISTOGRAM_BIN, &pRes); + if (TSDB_CODE_SUCCESS != code) { + tHistogramDestroy(&pRes); + return code; + } + (void)memcpy(pHisto, pRes, sizeof(SHistogramInfo) + sizeof(SHistBin) * MAX_HISTOGRAM_BIN); pHisto->elems = (SHistBin*)((char*)pHisto + sizeof(SHistogramInfo)); qDebug("%s merge histo, total:%" PRId64 ", entry:%d, %p", __FUNCTION__, pHisto->numOfElems, pHisto->numOfEntries, @@ -2224,6 +2322,7 @@ static void apercentileTransferInfo(SAPercentileInfo* pInput, SAPercentileInfo* tHistogramDestroy(&pRes); } } + return TSDB_CODE_SUCCESS; } int32_t apercentileFunctionMerge(SqlFunctionCtx* pCtx) { @@ -2246,7 +2345,10 @@ int32_t apercentileFunctionMerge(SqlFunctionCtx* pCtx) { char* data = colDataGetData(pCol, i); SAPercentileInfo* pInputInfo = (SAPercentileInfo*)varDataVal(data); - apercentileTransferInfo(pInputInfo, pInfo, &hasRes); + int32_t code = apercentileTransferInfo(pInputInfo, pInfo, &hasRes); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } if (pInfo->algo != APERCT_ALGO_TDIGEST) { @@ -2279,7 +2381,12 @@ int32_t apercentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { pInfo->pHisto->numOfElems, pInfo->pHisto->numOfEntries, pInfo->pHisto, pInfo->pHisto->elems); double ratio[] = {pInfo->percent}; - double* res = tHistogramUniform(pInfo->pHisto, ratio, 1); + double* res = NULL; + int32_t code = tHistogramUniform(pInfo->pHisto, ratio, 1, &res); + if (TSDB_CODE_SUCCESS != code) { + taosMemoryFree(res); + return code; + } pInfo->result = *res; // memcpy(pCtx->pOutput, res, sizeof(double)); taosMemoryFree(res); @@ -2300,22 +2407,29 @@ int32_t apercentilePartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t resultBytes = getApercentileMaxSize(); char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char)); + if (NULL == res) { + return TSDB_CODE_OUT_OF_MEMORY; + } if (pInfo->algo == APERCT_ALGO_TDIGEST) { - memcpy(varDataVal(res), pInfo, resultBytes); + (void)memcpy(varDataVal(res), pInfo, resultBytes); varDataSetLen(res, resultBytes); } else { - memcpy(varDataVal(res), pInfo, resultBytes); + (void)memcpy(varDataVal(res), pInfo, resultBytes); varDataSetLen(res, resultBytes); } int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + taosMemoryFree(res); + return TSDB_CODE_OUT_OF_RANGE; + } - colDataSetVal(pCol, pBlock->info.rows, res, false); + int32_t code = colDataSetVal(pCol, pBlock->info.rows, res, false); taosMemoryFree(res); - return pResInfo->numOfRes; + return code; } int32_t apercentileCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { @@ -2327,7 +2441,10 @@ int32_t apercentileCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) qDebug("%s start to combine apercentile, %p", __FUNCTION__, pDBuf->pHisto); - apercentileTransferInfo(pSBuf, pDBuf, NULL); + int32_t code = apercentileTransferInfo(pSBuf, pDBuf, NULL); + if (TSDB_CODE_SUCCESS != code) { + return code; + } pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes); pDResInfo->isNullRes &= pSResInfo->isNullRes; return TSDB_CODE_SUCCESS; @@ -2447,7 +2564,7 @@ static FORCE_INLINE TSKEY getRowPTs(SColumnInfoData* pTsColInfo, int32_t rowInde return *(TSKEY*)colDataGetData(pTsColInfo, rowIndex); } -static void prepareBuf(SqlFunctionCtx* pCtx) { +static int32_t prepareBuf(SqlFunctionCtx* pCtx) { if (pCtx->subsidiaries.rowLen == 0) { int32_t rowLen = 0; for (int32_t j = 0; j < pCtx->subsidiaries.num; ++j) { @@ -2457,7 +2574,11 @@ static void prepareBuf(SqlFunctionCtx* pCtx) { pCtx->subsidiaries.rowLen = rowLen + pCtx->subsidiaries.num * sizeof(bool); pCtx->subsidiaries.buf = taosMemoryMalloc(pCtx->subsidiaries.rowLen); + if (NULL == pCtx->subsidiaries.buf) { + return TSDB_CODE_OUT_OF_MEMORY; + } } + return TSDB_CODE_SUCCESS; } static int32_t firstlastSaveTupleData(const SSDataBlock* pSrcBlock, int32_t rowIndex, SqlFunctionCtx* pCtx, @@ -2485,12 +2606,12 @@ static int32_t doSaveCurrentVal(SqlFunctionCtx* pCtx, int32_t rowIndex, int64_t pInfo->bytes = varDataTLen(pData); } - memcpy(pInfo->buf, pData, pInfo->bytes); + (void)memcpy(pInfo->buf, pData, pInfo->bytes); if (pkData != NULL) { if (IS_VAR_DATA_TYPE(pInfo->pkType)) { pInfo->pkBytes = varDataTLen(pkData); } - memcpy(pInfo->buf + pInfo->bytes, pkData, pInfo->pkBytes); + (void)memcpy(pInfo->buf + pInfo->bytes, pkData, pInfo->pkBytes); pInfo->pkData = pInfo->buf + pInfo->bytes; } @@ -2818,9 +2939,9 @@ int32_t lastFunction(SqlFunctionCtx* pCtx) { return TSDB_CODE_SUCCESS; } -static int32_t firstLastTransferInfoImpl(SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst) { +static bool firstLastTransferInfoImpl(SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst) { if (!pInput->hasResult) { - return TSDB_CODE_FAILED; + return false; } __compar_fn_t pkCompareFn = NULL; if (pInput->pkData) { @@ -2830,12 +2951,12 @@ static int32_t firstLastTransferInfoImpl(SFirstLastRes* pInput, SFirstLastRes* p if (isFirst) { if (pInput->ts > pOutput->ts || (pInput->ts == pOutput->ts && pkCompareFn && pkCompareFn(pInput->pkData, pOutput->pkData) > 0)) { - return TSDB_CODE_FAILED; + return false; } } else { if (pInput->ts < pOutput->ts || (pInput->ts == pOutput->ts && pkCompareFn && pkCompareFn(pInput->pkData, pOutput->pkData) > 0)) { - return TSDB_CODE_FAILED; + return false; } } } @@ -2845,25 +2966,24 @@ static int32_t firstLastTransferInfoImpl(SFirstLastRes* pInput, SFirstLastRes* p pOutput->bytes = pInput->bytes; pOutput->pkType = pInput->pkType; - memcpy(pOutput->buf, pInput->buf, pOutput->bytes); + (void)memcpy(pOutput->buf, pInput->buf, pOutput->bytes); if (pInput->pkData) { pOutput->pkBytes = pInput->pkBytes; - memcpy(pOutput->buf + pOutput->bytes, pInput->pkData, pOutput->pkBytes); + (void)memcpy(pOutput->buf + pOutput->bytes, pInput->pkData, pOutput->pkBytes); pOutput->pkData = pOutput->buf + pOutput->bytes; } - return TSDB_CODE_SUCCESS; + return true; } static int32_t firstLastTransferInfo(SqlFunctionCtx* pCtx, SFirstLastRes* pInput, SFirstLastRes* pOutput, bool isFirst, int32_t rowIndex) { - if (TSDB_CODE_SUCCESS == firstLastTransferInfoImpl(pInput, pOutput, isFirst)) { + if (firstLastTransferInfoImpl(pInput, pOutput, isFirst)) { int32_t code = firstlastSaveTupleData(pCtx->pSrcBlock, rowIndex, pCtx, pOutput); - if (code != TSDB_CODE_SUCCESS) { + if (TSDB_CODE_SUCCESS != code) { return code; } pOutput->hasResult = true; } - return TSDB_CODE_SUCCESS; } @@ -2918,12 +3038,18 @@ int32_t firstLastFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t code = TSDB_CODE_SUCCESS; int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + return TSDB_CODE_OUT_OF_RANGE; + } SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0; SFirstLastRes* pRes = GET_ROWCELL_INTERBUF(pResInfo); - colDataSetVal(pCol, pBlock->info.rows, pRes->buf, pRes->isNull || pResInfo->isNullRes); + code = colDataSetVal(pCol, pBlock->info.rows, pRes->buf, pRes->isNull || pResInfo->isNullRes); + if (TSDB_CODE_SUCCESS != code) { + return code; + } // handle selectivity code = setSelectivityValue(pCtx, pBlock, &pRes->pos, pBlock->info.rows); @@ -2941,14 +3067,24 @@ int32_t firstLastPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { // todo check for failure char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char)); - memcpy(varDataVal(res), pRes, resultBytes); + if (NULL == res) { + return TSDB_CODE_OUT_OF_MEMORY; + } + (void)memcpy(varDataVal(res), pRes, resultBytes); varDataSetLen(res, resultBytes); int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + taosMemoryFree(res); + return TSDB_CODE_OUT_OF_RANGE; + } - colDataSetVal(pCol, pBlock->info.rows, res, false); + code = colDataSetVal(pCol, pBlock->info.rows, res, false); + if (TSDB_CODE_SUCCESS != code) { + return TSDB_CODE_OUT_OF_MEMORY; + } code = setSelectivityValue(pCtx, pBlock, &pRes->pos, pBlock->info.rows); taosMemoryFree(res); @@ -2963,10 +3099,7 @@ int32_t lastCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx); SFirstLastRes* pSBuf = GET_ROWCELL_INTERBUF(pSResInfo); - if (TSDB_CODE_SUCCESS == firstLastTransferInfoImpl(pSBuf, pDBuf, false)) { - pDBuf->hasResult = true; - } - + pDBuf->hasResult = firstLastTransferInfoImpl(pSBuf, pDBuf, false); pDResInfo->numOfRes = TMAX(pDResInfo->numOfRes, pSResInfo->numOfRes); pDResInfo->isNullRes &= pSResInfo->isNullRes; return TSDB_CODE_SUCCESS; @@ -2987,7 +3120,7 @@ static int32_t doSaveLastrow(SqlFunctionCtx* pCtx, char* pData, int32_t rowIndex pInfo->bytes = varDataTLen(pData); } - memcpy(pInfo->buf, pData, pInfo->bytes); + (void)memcpy(pInfo->buf, pData, pInfo->bytes); } if (pCtx->hasPrimaryKey) { @@ -2995,7 +3128,7 @@ static int32_t doSaveLastrow(SqlFunctionCtx* pCtx, char* pData, int32_t rowIndex if (IS_VAR_DATA_TYPE(pInfo->pkType)) { pInfo->pkBytes = varDataTLen(pkData); } - memcpy(pInfo->buf + pInfo->bytes, pkData, pInfo->pkBytes); + (void)memcpy(pInfo->buf + pInfo->bytes, pkData, pInfo->pkBytes); pInfo->pkData = pInfo->buf + pInfo->bytes; } pInfo->ts = cts; @@ -3101,11 +3234,13 @@ bool getDiffFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) { return true; } -bool diffFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { - if (!functionSetup(pCtx, pResInfo)) { - return false; +int32_t diffFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { + if (pResInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } - SDiffInfo* pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo); pDiffInfo->hasPrev = false; pDiffInfo->isFirstRow = true; @@ -3116,7 +3251,7 @@ bool diffFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { } else { pDiffInfo->ignoreOption = 0; } - return true; + return TSDB_CODE_SUCCESS; } static int32_t doSetPrevVal(SDiffInfo* pDiffInfo, int32_t type, const char* pv, int64_t ts) { @@ -3155,7 +3290,7 @@ static int32_t doSetPrevVal(SDiffInfo* pDiffInfo, int32_t type, const char* pv, return TSDB_CODE_SUCCESS; } -static int32_t diffIsNegtive(SDiffInfo* pDiffInfo, int32_t type, const char* pv) { +static bool diffIsNegtive(SDiffInfo* pDiffInfo, int32_t type, const char* pv) { switch (type) { case TSDB_DATA_TYPE_UINT: { int64_t v = *(uint32_t*)pv; @@ -3382,14 +3517,17 @@ int32_t setDoDiffResult(SqlFunctionCtx* pCtx, SFuncInputRow* pRow, int32_t pos) SColumnInfoData* pInputCol = pInput->pData[0]; int8_t inputType = pInputCol->info.type; SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput; - + int32_t code = TSDB_CODE_SUCCESS; if (pRow->isDataNull) { colDataSetNull_f_s(pOutput, pos); pOutput->hasNull = true; // handle selectivity if (pCtx->subsidiaries.num > 0) { - appendSelectivityCols(pCtx, pRow->block, pRow->rowIndex, pos); + code = appendSelectivityCols(pCtx, pRow->block, pRow->rowIndex, pos); + if (code != TSDB_CODE_SUCCESS) { + return code; + } } return TSDB_CODE_SUCCESS; } @@ -3399,13 +3537,16 @@ int32_t setDoDiffResult(SqlFunctionCtx* pCtx, SFuncInputRow* pRow, int32_t pos) if (pRow->ts == pDiffInfo->prevTs) { return TSDB_CODE_FUNC_DUP_TIMESTAMP; } - int32_t code = doHandleDiff(pDiffInfo, inputType, pv, pOutput, pos, pRow->ts); + code = doHandleDiff(pDiffInfo, inputType, pv, pOutput, pos, pRow->ts); if (code != TSDB_CODE_SUCCESS) { return code; } // handle selectivity if (pCtx->subsidiaries.num > 0) { - appendSelectivityCols(pCtx, pRow->block, pRow->rowIndex, pos); + code = appendSelectivityCols(pCtx, pRow->block, pRow->rowIndex, pos); + if (code != TSDB_CODE_SUCCESS) { + return code; + } } return TSDB_CODE_SUCCESS; @@ -3424,10 +3565,17 @@ int32_t diffFunctionByRow(SArray* pCtxArray) { int32_t numOfElems = 0; SArray* pRows = taosArrayInit_s(sizeof(SFuncInputRow), diffColNum); + if (NULL == pRows) { + return terrno; + } bool keepNull = false; for (int i = 0; i < diffColNum; ++i) { SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i); + if (NULL == pCtx) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _exit; + } funcInputUpdate(pCtx); SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); SDiffInfo* pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo); @@ -3438,13 +3586,33 @@ int32_t diffFunctionByRow(SArray* pCtxArray) { SqlFunctionCtx* pCtx0 = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, 0); SFuncInputRow* pRow0 = (SFuncInputRow*)taosArrayGet(pRows, 0); + if (NULL == pCtx0 || NULL == pRow0) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _exit; + } int32_t startOffset = pCtx0->offset; - while (funcInputGetNextRow(pCtx0, pRow0)) { + bool result = false; + while (1) { + code = funcInputGetNextRow(pCtx0, pRow0, &result); + if (TSDB_CODE_SUCCESS != code) { + goto _exit; + } + if (!result) { + break; + } bool hasNotNullValue = !diffResultIsNull(pCtx0, pRow0); for (int i = 1; i < diffColNum; ++i) { SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i); SFuncInputRow* pRow = (SFuncInputRow*)taosArrayGet(pRows, i); - if(!funcInputGetNextRow(pCtx, pRow)) { + if (NULL == pCtx || NULL == pRow) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _exit; + } + code = funcInputGetNextRow(pCtx, pRow, &result); + if (TSDB_CODE_SUCCESS != code) { + goto _exit; + } + if (!result) { // rows are not equal code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; goto _exit; @@ -3459,6 +3627,10 @@ int32_t diffFunctionByRow(SArray* pCtxArray) { for (int i = 0; i < diffColNum; ++i) { SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i); SFuncInputRow* pRow = (SFuncInputRow*)taosArrayGet(pRows, i); + if (NULL == pCtx || NULL == pRow) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _exit; + } if ((keepNull || hasNotNullValue) && !isFirstRow(pCtx, pRow)){ code = setDoDiffResult(pCtx, pRow, pos); if (code != TSDB_CODE_SUCCESS) { @@ -3477,6 +3649,10 @@ int32_t diffFunctionByRow(SArray* pCtxArray) { for (int i = 0; i < diffColNum; ++i) { SqlFunctionCtx* pCtx = *(SqlFunctionCtx**)taosArrayGet(pCtxArray, i); + if (NULL == pCtx) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _exit; + } SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); pResInfo->numOfRes = numOfElems; } @@ -3497,9 +3673,12 @@ bool getTopBotFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) { return true; } -bool topBotFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { - if (!functionSetup(pCtx, pResInfo)) { - return false; +int32_t topBotFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { + if (pResInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } STopBotRes* pRes = GET_ROWCELL_INTERBUF(pResInfo); @@ -3509,7 +3688,7 @@ bool topBotFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { pRes->nullTupleSaved = false; pRes->nullTuplePos.pageId = -1; - return true; + return TSDB_CODE_SUCCESS; } static STopBotRes* getTopBotOutputInfo(SqlFunctionCtx* pCtx) { @@ -3523,7 +3702,7 @@ static STopBotRes* getTopBotOutputInfo(SqlFunctionCtx* pCtx) { static int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSDataBlock* pSrcBlock, uint16_t type, uint64_t uid, SResultRowEntryInfo* pEntryInfo, bool isTopQuery); -static void addResult(SqlFunctionCtx* pCtx, STopBotResItem* pSourceItem, int16_t type, bool isTopQuery); +static int32_t addResult(SqlFunctionCtx* pCtx, STopBotResItem* pSourceItem, int16_t type, bool isTopQuery); int32_t topFunction(SqlFunctionCtx* pCtx) { int32_t numOfElems = 0; @@ -3630,8 +3809,10 @@ static int32_t topBotResComparFn(const void* p1, const void* p2, const void* par int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSDataBlock* pSrcBlock, uint16_t type, uint64_t uid, SResultRowEntryInfo* pEntryInfo, bool isTopQuery) { STopBotRes* pRes = getTopBotOutputInfo(pCtx); + int32_t code = TSDB_CODE_SUCCESS; SVariant val = {0}; + // TODO(smj) : this func need err code taosVariantCreateFromBinary(&val, pData, tDataTypes[type].bytes, type); STopBotResItem* pItems = pRes->pItems; @@ -3644,7 +3825,7 @@ int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSD // save the data of this tuple if (pCtx->subsidiaries.num > 0) { - int32_t code = saveTupleData(pCtx, rowIndex, pSrcBlock, &pItem->tuplePos); + code = saveTupleData(pCtx, rowIndex, pSrcBlock, &pItem->tuplePos); if (code != TSDB_CODE_SUCCESS) { return code; } @@ -3655,8 +3836,11 @@ int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSD #endif // allocate the buffer and keep the data of this row into the new allocated buffer pEntryInfo->numOfRes++; - taosheapsort((void*)pItems, sizeof(STopBotResItem), pEntryInfo->numOfRes, (const void*)&type, topBotResComparFn, - !isTopQuery); + code = taosheapsort((void*)pItems, sizeof(STopBotResItem), pEntryInfo->numOfRes, (const void*)&type, + topBotResComparFn, !isTopQuery); + if (code != TSDB_CODE_SUCCESS) { + return code; + } } else { // replace the minimum value in the result if ((isTopQuery && ((IS_SIGNED_NUMERIC_TYPE(type) && val.i > pItems[0].v.i) || (IS_UNSIGNED_NUMERIC_TYPE(type) && val.u > pItems[0].v.u) || @@ -3673,7 +3857,7 @@ int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSD // save the data of this tuple by over writing the old data if (pCtx->subsidiaries.num > 0) { - int32_t code = updateTupleData(pCtx, rowIndex, pSrcBlock, &pItem->tuplePos); + code = updateTupleData(pCtx, rowIndex, pSrcBlock, &pItem->tuplePos); if (code != TSDB_CODE_SUCCESS) { return code; } @@ -3681,8 +3865,11 @@ int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSD #ifdef BUF_PAGE_DEBUG qDebug("page_copyTuple pageId:%d, offset:%d", pItem->tuplePos.pageId, pItem->tuplePos.offset); #endif - taosheapadjust((void*)pItems, sizeof(STopBotResItem), 0, pEntryInfo->numOfRes - 1, (const void*)&type, + code = taosheapadjust((void*)pItems, sizeof(STopBotResItem), 0, pEntryInfo->numOfRes - 1, (const void*)&type, topBotResComparFn, NULL, !isTopQuery); + if (code != TSDB_CODE_SUCCESS) { + return code; + } } } @@ -3695,8 +3882,8 @@ int32_t doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSD * |(n columns, one bit for each column)| src column #1| src column #2| * +------------------------------------+--------------+--------------+ */ -void* serializeTupleData(const SSDataBlock* pSrcBlock, int32_t rowIndex, SSubsidiaryResInfo* pSubsidiaryies, - char* buf) { +int32_t serializeTupleData(const SSDataBlock* pSrcBlock, int32_t rowIndex, SSubsidiaryResInfo* pSubsidiaryies, + char* buf, char** res) { char* nullList = buf; char* pStart = (char*)(nullList + sizeof(bool) * pSubsidiaryies->num); @@ -3714,6 +3901,9 @@ void* serializeTupleData(const SSDataBlock* pSrcBlock, int32_t rowIndex, SSubsid int32_t srcSlotId = pFuncParam->pCol->slotId; SColumnInfoData* pCol = taosArrayGet(pSrcBlock->pDataBlock, srcSlotId); + if (NULL == pCol) { + return TSDB_CODE_OUT_OF_RANGE; + } if ((nullList[i] = colDataIsNull_s(pCol, rowIndex)) == true) { offset += pCol->info.bytes; continue; @@ -3721,15 +3911,16 @@ void* serializeTupleData(const SSDataBlock* pSrcBlock, int32_t rowIndex, SSubsid char* p = colDataGetData(pCol, rowIndex); if (IS_VAR_DATA_TYPE(pCol->info.type)) { - memcpy(pStart + offset, p, (pCol->info.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(p) : varDataTLen(p)); + (void)memcpy(pStart + offset, p, (pCol->info.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(p) : varDataTLen(p)); } else { - memcpy(pStart + offset, p, pCol->info.bytes); + (void)memcpy(pStart + offset, p, pCol->info.bytes); } offset += pCol->info.bytes; } - return buf; + *res = buf; + return TSDB_CODE_SUCCESS; } static int32_t doSaveTupleData(SSerializeDataHandle* pHandle, const void* pBuf, size_t length, SWinKey* key, @@ -3761,7 +3952,7 @@ static int32_t doSaveTupleData(SSerializeDataHandle* pHandle, const void* pBuf, } p = (STuplePos){.pageId = pHandle->currentPage, .offset = pPage->num}; - memcpy(pPage->data + pPage->num, pBuf, length); + (void)memcpy(pPage->data + pPage->num, pBuf, length); pPage->num += length; setBufPageDirty(pPage, true); @@ -3777,17 +3968,27 @@ static int32_t doSaveTupleData(SSerializeDataHandle* pHandle, const void* pBuf, } int32_t saveTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STuplePos* pPos) { - prepareBuf(pCtx); + int32_t code = prepareBuf(pCtx); + if (TSDB_CODE_SUCCESS != code) { + return code; + } SWinKey key = {0}; if (pCtx->saveHandle.pBuf == NULL) { SColumnInfoData* pColInfo = taosArrayGet(pSrcBlock->pDataBlock, pCtx->saveHandle.pState->tsIndex); + if (NULL == pColInfo) { + return TSDB_CODE_OUT_OF_RANGE; + } ASSERT(pColInfo->info.type == TSDB_DATA_TYPE_TIMESTAMP); key.groupId = pSrcBlock->info.id.groupId; key.ts = *(int64_t*)colDataGetData(pColInfo, rowIndex); } - char* buf = serializeTupleData(pSrcBlock, rowIndex, &pCtx->subsidiaries, pCtx->subsidiaries.buf); + char* buf = NULL; + code = serializeTupleData(pSrcBlock, rowIndex, &pCtx->subsidiaries, pCtx->subsidiaries.buf, &buf); + if (TSDB_CODE_SUCCESS != code) { + return code; + } return doSaveTupleData(&pCtx->saveHandle, buf, pCtx->subsidiaries.rowLen, &key, pPos, pCtx->pStore); } @@ -3797,42 +3998,56 @@ static int32_t doUpdateTupleData(SSerializeDataHandle* pHandle, const void* pBuf if (pPage == NULL) { return terrno; } - memcpy(pPage->data + pPos->offset, pBuf, length); + (void)memcpy(pPage->data + pPos->offset, pBuf, length); setBufPageDirty(pPage, true); releaseBufPage(pHandle->pBuf, pPage); } else { - pStore->streamStateFuncPut(pHandle->pState, &pPos->streamTupleKey, pBuf, length); + int32_t code = pStore->streamStateFuncPut(pHandle->pState, &pPos->streamTupleKey, pBuf, length); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } return TSDB_CODE_SUCCESS; } int32_t updateTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pSrcBlock, STuplePos* pPos) { - prepareBuf(pCtx); + int32_t code = prepareBuf(pCtx); + if (TSDB_CODE_SUCCESS != code) { + return code; + } - char* buf = serializeTupleData(pSrcBlock, rowIndex, &pCtx->subsidiaries, pCtx->subsidiaries.buf); + char* buf = NULL; + code = serializeTupleData(pSrcBlock, rowIndex, &pCtx->subsidiaries, pCtx->subsidiaries.buf, &buf); + if (TSDB_CODE_SUCCESS != code) { + return code; + } return doUpdateTupleData(&pCtx->saveHandle, buf, pCtx->subsidiaries.rowLen, pPos, pCtx->pStore); } -static char* doLoadTupleData(SSerializeDataHandle* pHandle, const STuplePos* pPos, SFunctionStateStore* pStore) { +static int32_t doLoadTupleData(SSerializeDataHandle* pHandle, const STuplePos* pPos, SFunctionStateStore* pStore, char** value) { if (pHandle->pBuf != NULL) { SFilePage* pPage = getBufPage(pHandle->pBuf, pPos->pageId); if (pPage == NULL) { - return NULL; + *value = NULL; + return terrno; } - char* p = pPage->data + pPos->offset; + *value = pPage->data + pPos->offset; releaseBufPage(pHandle->pBuf, pPage); - return p; + return TSDB_CODE_SUCCESS; } else { - void* value = NULL; + *value = NULL; int32_t vLen; - pStore->streamStateFuncGet(pHandle->pState, &pPos->streamTupleKey, &value, &vLen); - return (char*)value; + int32_t code = pStore->streamStateFuncGet(pHandle->pState, &pPos->streamTupleKey, (void **)(value), &vLen); + if (TSDB_CODE_SUCCESS != code) { + return code; + } + return TSDB_CODE_SUCCESS; } } -const char* loadTupleData(SqlFunctionCtx* pCtx, const STuplePos* pPos) { - return doLoadTupleData(&pCtx->saveHandle, pPos, pCtx->pStore); +int32_t loadTupleData(SqlFunctionCtx* pCtx, const STuplePos* pPos, char** value) { + return doLoadTupleData(&pCtx->saveHandle, pPos, pCtx->pStore, value); } int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { @@ -3845,6 +4060,9 @@ int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + return TSDB_CODE_OUT_OF_RANGE; + } // todo assign the tag value and the corresponding row data int32_t currentRow = pBlock->info.rows; @@ -3855,22 +4073,29 @@ int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { } for (int32_t i = 0; i < pEntryInfo->numOfRes; ++i) { STopBotResItem* pItem = &pRes->pItems[i]; - colDataSetVal(pCol, currentRow, (const char*)&pItem->v.i, false); + code = colDataSetVal(pCol, currentRow, (const char*)&pItem->v.i, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } #ifdef BUF_PAGE_DEBUG qDebug("page_finalize i:%d,item:%p,pageId:%d, offset:%d\n", i, pItem, pItem->tuplePos.pageId, pItem->tuplePos.offset); #endif code = setSelectivityValue(pCtx, pBlock, &pRes->pItems[i].tuplePos, currentRow); + if (TSDB_CODE_SUCCESS != code) { + return code; + } currentRow += 1; } return code; } -void addResult(SqlFunctionCtx* pCtx, STopBotResItem* pSourceItem, int16_t type, bool isTopQuery) { +int32_t addResult(SqlFunctionCtx* pCtx, STopBotResItem* pSourceItem, int16_t type, bool isTopQuery) { SResultRowEntryInfo* pEntryInfo = GET_RES_INFO(pCtx); STopBotRes* pRes = getTopBotOutputInfo(pCtx); STopBotResItem* pItems = pRes->pItems; + int32_t code = TSDB_CODE_SUCCESS; // not full yet if (pEntryInfo->numOfRes < pRes->maxSize) { @@ -3880,8 +4105,11 @@ void addResult(SqlFunctionCtx* pCtx, STopBotResItem* pSourceItem, int16_t type, pItem->tuplePos.pageId = -1; replaceTupleData(&pItem->tuplePos, &pSourceItem->tuplePos); pEntryInfo->numOfRes++; - taosheapsort((void*)pItems, sizeof(STopBotResItem), pEntryInfo->numOfRes, (const void*)&type, topBotResComparFn, - !isTopQuery); + code = taosheapsort((void*)pItems, sizeof(STopBotResItem), pEntryInfo->numOfRes, (const void*)&type, + topBotResComparFn, !isTopQuery); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } else { // replace the minimum value in the result if ((isTopQuery && ((IS_SIGNED_NUMERIC_TYPE(type) && pSourceItem->v.i > pItems[0].v.i) || (IS_UNSIGNED_NUMERIC_TYPE(type) && pSourceItem->v.u > pItems[0].v.u) || @@ -3898,18 +4126,26 @@ void addResult(SqlFunctionCtx* pCtx, STopBotResItem* pSourceItem, int16_t type, // save the data of this tuple by over writing the old data replaceTupleData(&pItem->tuplePos, &pSourceItem->tuplePos); - taosheapadjust((void*)pItems, sizeof(STopBotResItem), 0, pEntryInfo->numOfRes - 1, (const void*)&type, - topBotResComparFn, NULL, !isTopQuery); + code = taosheapadjust((void*)pItems, sizeof(STopBotResItem), 0, pEntryInfo->numOfRes - 1, (const void*)&type, + topBotResComparFn, NULL, !isTopQuery); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } } + return code; } int32_t topCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx); STopBotRes* pSBuf = getTopBotOutputInfo(pSourceCtx); int16_t type = pSBuf->type; + int32_t code = TSDB_CODE_SUCCESS; for (int32_t i = 0; i < pSResInfo->numOfRes; i++) { - addResult(pDestCtx, pSBuf->pItems + i, type, true); + code = addResult(pDestCtx, pSBuf->pItems + i, type, true); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } return TSDB_CODE_SUCCESS; } @@ -3918,8 +4154,12 @@ int32_t bottomCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { SResultRowEntryInfo* pSResInfo = GET_RES_INFO(pSourceCtx); STopBotRes* pSBuf = getTopBotOutputInfo(pSourceCtx); int16_t type = pSBuf->type; + int32_t code = TSDB_CODE_SUCCESS; for (int32_t i = 0; i < pSResInfo->numOfRes; i++) { - addResult(pDestCtx, pSBuf->pItems + i, type, false); + code = addResult(pDestCtx, pSBuf->pItems + i, type, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } return TSDB_CODE_SUCCESS; } @@ -3931,16 +4171,19 @@ bool getSpreadFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) { return true; } -bool spreadFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { - if (!functionSetup(pCtx, pResultInfo)) { - return false; +int32_t spreadFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { + if (pResultInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } SSpreadInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo); SET_DOUBLE_VAL(&pInfo->min, DBL_MAX); SET_DOUBLE_VAL(&pInfo->max, -DBL_MAX); pInfo->hasResult = false; - return true; + return TSDB_CODE_SUCCESS; } int32_t spreadFunction(SqlFunctionCtx* pCtx) { @@ -4073,16 +4316,28 @@ int32_t spreadPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t resultBytes = getSpreadInfoSize(); char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char)); - memcpy(varDataVal(res), pInfo, resultBytes); + if (NULL == res) { + return TSDB_CODE_OUT_OF_MEMORY; + } + (void)memcpy(varDataVal(res), pInfo, resultBytes); varDataSetLen(res, resultBytes); int32_t slotId = pCtx->pExpr->base.resSchema.slotId; + int32_t code = TSDB_CODE_SUCCESS; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _exit; + } - colDataSetVal(pCol, pBlock->info.rows, res, false); + code = colDataSetVal(pCol, pBlock->info.rows, res, false); + if (TSDB_CODE_SUCCESS != code) { + goto _exit; + } +_exit: taosMemoryFree(res); - return pResInfo->numOfRes; + return TSDB_CODE_SUCCESS; } int32_t spreadCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { @@ -4104,9 +4359,12 @@ bool getElapsedFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) { return true; } -bool elapsedFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { - if (!functionSetup(pCtx, pResultInfo)) { - return false; +int32_t elapsedFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { + if (pResultInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } SElapsedInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo); @@ -4120,7 +4378,7 @@ bool elapsedFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo pInfo->timeUnit = 1; } - return true; + return TSDB_CODE_SUCCESS; } int32_t elapsedFunction(SqlFunctionCtx* pCtx) { @@ -4248,16 +4506,27 @@ int32_t elapsedPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t resultBytes = getElapsedInfoSize(); char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char)); - memcpy(varDataVal(res), pInfo, resultBytes); + if (NULL == res) { + return TSDB_CODE_OUT_OF_MEMORY; + } + (void)memcpy(varDataVal(res), pInfo, resultBytes); varDataSetLen(res, resultBytes); int32_t slotId = pCtx->pExpr->base.resSchema.slotId; + int32_t code = TSDB_CODE_SUCCESS; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _exit; + } - colDataSetVal(pCol, pBlock->info.rows, res, false); - + code = colDataSetVal(pCol, pBlock->info.rows, res, false); + if (TSDB_CODE_SUCCESS != code) { + goto _exit; + } +_exit: taosMemoryFree(res); - return pResInfo->numOfRes; + return code; } int32_t elapsedCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { @@ -4297,7 +4566,7 @@ static int8_t getHistogramBinType(char* binTypeStr) { return binType; } -static bool getHistogramBinDesc(SHistoFuncInfo* pInfo, char* binDescStr, int8_t binType, bool normalized) { +static int32_t getHistogramBinDesc(SHistoFuncInfo* pInfo, char* binDescStr, int8_t binType, bool normalized) { cJSON* binDesc = cJSON_Parse(binDescStr); int32_t numOfBins; double* intervals; @@ -4306,7 +4575,7 @@ static bool getHistogramBinDesc(SHistoFuncInfo* pInfo, char* binDescStr, int8_t int32_t startIndex; if (numOfParams != 4) { cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } cJSON* start = cJSON_GetObjectItem(binDesc, "start"); @@ -4317,18 +4586,18 @@ static bool getHistogramBinDesc(SHistoFuncInfo* pInfo, char* binDescStr, int8_t if (!cJSON_IsNumber(start) || !cJSON_IsNumber(count) || !cJSON_IsBool(infinity)) { cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } if (count->valueint <= 0 || count->valueint > 1000) { // limit count to 1000 cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } if (isinf(start->valuedouble) || (width != NULL && isinf(width->valuedouble)) || (factor != NULL && isinf(factor->valuedouble)) || (count != NULL && isinf(count->valuedouble))) { cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } int32_t counter = (int32_t)count->valueint; @@ -4341,19 +4610,24 @@ static bool getHistogramBinDesc(SHistoFuncInfo* pInfo, char* binDescStr, int8_t } intervals = taosMemoryCalloc(numOfBins, sizeof(double)); + if (NULL == intervals) { + cJSON_Delete(binDesc); + qError("histogram function out of memory"); + return TSDB_CODE_OUT_OF_MEMORY; + } if (cJSON_IsNumber(width) && factor == NULL && binType == LINEAR_BIN) { // linear bin process if (width->valuedouble == 0) { taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } for (int i = 0; i < counter + 1; ++i) { intervals[startIndex] = start->valuedouble + i * width->valuedouble; if (isinf(intervals[startIndex])) { taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } startIndex++; } @@ -4362,26 +4636,26 @@ static bool getHistogramBinDesc(SHistoFuncInfo* pInfo, char* binDescStr, int8_t if (start->valuedouble == 0) { taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } if (factor->valuedouble < 0 || factor->valuedouble == 0 || factor->valuedouble == 1) { taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } for (int i = 0; i < counter + 1; ++i) { intervals[startIndex] = start->valuedouble * pow(factor->valuedouble, i * 1.0); if (isinf(intervals[startIndex])) { taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } startIndex++; } } else { taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } if (infinity->valueint == true) { @@ -4389,7 +4663,7 @@ static bool getHistogramBinDesc(SHistoFuncInfo* pInfo, char* binDescStr, int8_t intervals[numOfBins - 1] = INFINITY; // in case of desc bin orders, -inf/inf should be swapped if (numOfBins < 4) { - return false; + return TSDB_CODE_FAILED; } if (intervals[1] > intervals[numOfBins - 2]) { TSWAP(intervals[0], intervals[numOfBins - 1]); @@ -4398,15 +4672,20 @@ static bool getHistogramBinDesc(SHistoFuncInfo* pInfo, char* binDescStr, int8_t } else if (cJSON_IsArray(binDesc)) { /* user input bins */ if (binType != USER_INPUT_BIN) { cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } numOfBins = cJSON_GetArraySize(binDesc); intervals = taosMemoryCalloc(numOfBins, sizeof(double)); + if (NULL == intervals) { + cJSON_Delete(binDesc); + qError("histogram function out of memory"); + return TSDB_CODE_OUT_OF_MEMORY; + } cJSON* bin = binDesc->child; if (bin == NULL) { taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } int i = 0; while (bin) { @@ -4414,19 +4693,19 @@ static bool getHistogramBinDesc(SHistoFuncInfo* pInfo, char* binDescStr, int8_t if (!cJSON_IsNumber(bin)) { taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } if (i != 0 && intervals[i] <= intervals[i - 1]) { taosMemoryFree(intervals); cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } bin = bin->next; i++; } } else { cJSON_Delete(binDesc); - return false; + return TSDB_CODE_FAILED; } pInfo->numOfBins = numOfBins - 1; @@ -4440,12 +4719,15 @@ static bool getHistogramBinDesc(SHistoFuncInfo* pInfo, char* binDescStr, int8_t taosMemoryFree(intervals); cJSON_Delete(binDesc); - return true; + return TSDB_CODE_SUCCESS; } -bool histogramFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { - if (!functionSetup(pCtx, pResultInfo)) { - return false; +int32_t histogramFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { + if (pResultInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo); @@ -4458,21 +4740,22 @@ bool histogramFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultIn taosMemoryFree(binTypeStr); if (binType == UNKNOWN_BIN) { - return false; + return TSDB_CODE_FUNC_FUNTION_PARA_VALUE; } char* binDesc = strndup(varDataVal(pCtx->param[2].param.pz), varDataLen(pCtx->param[2].param.pz)); int64_t normalized = pCtx->param[3].param.i; if (normalized != 0 && normalized != 1) { taosMemoryFree(binDesc); - return false; + return TSDB_CODE_FUNC_FUNTION_PARA_VALUE; } - if (!getHistogramBinDesc(pInfo, binDesc, binType, (bool)normalized)) { + int32_t code = getHistogramBinDesc(pInfo, binDesc, binType, (bool)normalized); + if (TSDB_CODE_SUCCESS != code) { taosMemoryFree(binDesc); - return false; + return code; } taosMemoryFree(binDesc); - return true; + return TSDB_CODE_SUCCESS; } static int32_t histogramFunctionImpl(SqlFunctionCtx* pCtx, bool isPartial) { @@ -4556,8 +4839,12 @@ int32_t histogramFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + int32_t code = TSDB_CODE_SUCCESS; int32_t currentRow = pBlock->info.rows; + if (NULL == pCol) { + return TSDB_CODE_OUT_OF_RANGE; + } if (pInfo->normalized) { for (int32_t k = 0; k < pResInfo->numOfRes; ++k) { @@ -4580,11 +4867,14 @@ int32_t histogramFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { pInfo->bins[i].upper, pInfo->bins[i].percentage); } varDataSetLen(buf, len); - colDataSetVal(pCol, currentRow, buf, false); + code = colDataSetVal(pCol, currentRow, buf, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } currentRow++; } - return pResInfo->numOfRes; + return code; } int32_t histogramPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { @@ -4593,16 +4883,24 @@ int32_t histogramPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t resultBytes = getHistogramInfoSize(); char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char)); - memcpy(varDataVal(res), pInfo, resultBytes); + if (NULL == res) { + return TSDB_CODE_OUT_OF_MEMORY; + } + (void)memcpy(varDataVal(res), pInfo, resultBytes); varDataSetLen(res, resultBytes); int32_t slotId = pCtx->pExpr->base.resSchema.slotId; + int32_t code = TSDB_CODE_SUCCESS; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _exit; + } + code = colDataSetVal(pCol, pBlock->info.rows, res, false); - colDataSetVal(pCol, pBlock->info.rows, res, false); - +_exit: taosMemoryFree(res); - return pResInfo->numOfRes; + return code; } int32_t histogramCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { @@ -4818,16 +5116,25 @@ int32_t hllPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t resultBytes = getHLLInfoSize(); char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char)); - memcpy(varDataVal(res), pInfo, resultBytes); + if (NULL == res) { + return TSDB_CODE_OUT_OF_MEMORY; + } + (void)memcpy(varDataVal(res), pInfo, resultBytes); varDataSetLen(res, resultBytes); int32_t slotId = pCtx->pExpr->base.resSchema.slotId; + int32_t code = TSDB_CODE_SUCCESS; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _exit; + } - colDataSetVal(pCol, pBlock->info.rows, res, false); + code = colDataSetVal(pCol, pBlock->info.rows, res, false); +_exit: taosMemoryFree(res); - return pResInfo->numOfRes; + return code; } int32_t hllCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { @@ -4930,6 +5237,7 @@ static bool checkStateOp(int8_t op, SColumnInfoData* pCol, int32_t index, SVaria } int32_t stateCountFunction(SqlFunctionCtx* pCtx) { + int32_t code = TSDB_CODE_SUCCESS; SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); SStateInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo); @@ -4960,7 +5268,10 @@ int32_t stateCountFunction(SqlFunctionCtx* pCtx) { colDataSetNULL(pOutput, i); // handle selectivity if (pCtx->subsidiaries.num > 0) { - appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1); + code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } continue; } @@ -4973,11 +5284,17 @@ int32_t stateCountFunction(SqlFunctionCtx* pCtx) { } else { pInfo->count = 0; } - colDataSetVal(pOutput, pCtx->offset + numOfElems - 1, (char*)&output, false); + code = colDataSetVal(pOutput, pCtx->offset + numOfElems - 1, (char*)&output, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } // handle selectivity if (pCtx->subsidiaries.num > 0) { - appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1); + code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } } @@ -4986,6 +5303,7 @@ int32_t stateCountFunction(SqlFunctionCtx* pCtx) { } int32_t stateDurationFunction(SqlFunctionCtx* pCtx) { + int32_t code = TSDB_CODE_SUCCESS; SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); SStateInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo); @@ -5022,7 +5340,10 @@ int32_t stateDurationFunction(SqlFunctionCtx* pCtx) { colDataSetNULL(pOutput, i); // handle selectivity if (pCtx->subsidiaries.num > 0) { - appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1); + code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } continue; } @@ -5039,11 +5360,17 @@ int32_t stateDurationFunction(SqlFunctionCtx* pCtx) { } else { pInfo->durationStart = 0; } - colDataSetVal(pOutput, pCtx->offset + numOfElems - 1, (char*)&output, false); + code = colDataSetVal(pOutput, pCtx->offset + numOfElems - 1, (char*)&output, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } // handle selectivity if (pCtx->subsidiaries.num > 0) { - appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1); + code = appendSelectivityValue(pCtx, i, pCtx->offset + numOfElems - 1); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } } @@ -5057,6 +5384,7 @@ bool getCsumFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) { } int32_t csumFunction(SqlFunctionCtx* pCtx) { + int32_t code = TSDB_CODE_SUCCESS; SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); SSumRes* pSumRes = GET_ROWCELL_INTERBUF(pResInfo); @@ -5088,12 +5416,18 @@ int32_t csumFunction(SqlFunctionCtx* pCtx) { int64_t v; GET_TYPED_DATA(v, int64_t, type, data); pSumRes->isum += v; - colDataSetVal(pOutput, pos, (char*)&pSumRes->isum, false); + code = colDataSetVal(pOutput, pos, (char*)&pSumRes->isum, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) { uint64_t v; GET_TYPED_DATA(v, uint64_t, type, data); pSumRes->usum += v; - colDataSetVal(pOutput, pos, (char*)&pSumRes->usum, false); + code = colDataSetVal(pOutput, pos, (char*)&pSumRes->usum, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } else if (IS_FLOAT_TYPE(type)) { double v; GET_TYPED_DATA(v, double, type, data); @@ -5102,13 +5436,19 @@ int32_t csumFunction(SqlFunctionCtx* pCtx) { if (isinf(pSumRes->dsum) || isnan(pSumRes->dsum)) { colDataSetNULL(pOutput, pos); } else { - colDataSetVal(pOutput, pos, (char*)&pSumRes->dsum, false); + code = colDataSetVal(pOutput, pos, (char*)&pSumRes->dsum, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } } // handle selectivity if (pCtx->subsidiaries.num > 0) { - appendSelectivityValue(pCtx, i, pos); + code = appendSelectivityValue(pCtx, i, pos); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } numOfElems++; @@ -5123,9 +5463,12 @@ bool getMavgFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) { return true; } -bool mavgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { - if (!functionSetup(pCtx, pResultInfo)) { - return false; +int32_t mavgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { + if (pResultInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } SMavgInfo* pInfo = GET_ROWCELL_INTERBUF(pResultInfo); @@ -5135,14 +5478,15 @@ bool mavgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { pInfo->isPrevTsSet = false; pInfo->numOfPoints = pCtx->param[1].param.i; if (pInfo->numOfPoints < 1 || pInfo->numOfPoints > MAVG_MAX_POINTS_NUM) { - return false; + return TSDB_CODE_FUNC_FUNTION_PARA_VALUE; } pInfo->pointsMeet = false; - return true; + return TSDB_CODE_SUCCESS; } int32_t mavgFunction(SqlFunctionCtx* pCtx) { + int32_t code = TSDB_CODE_SUCCESS; SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); SMavgInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo); @@ -5191,12 +5535,18 @@ int32_t mavgFunction(SqlFunctionCtx* pCtx) { if (isinf(result) || isnan(result)) { colDataSetNULL(pOutput, pos); } else { - colDataSetVal(pOutput, pos, (char*)&result, false); + code = colDataSetVal(pOutput, pos, (char*)&result, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } // handle selectivity if (pCtx->subsidiaries.num > 0) { - appendSelectivityValue(pCtx, i, pos); + code = appendSelectivityValue(pCtx, i, pos); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } numOfElems++; @@ -5230,9 +5580,12 @@ bool getSampleFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) { return true; } -bool sampleFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { - if (!functionSetup(pCtx, pResultInfo)) { - return false; +int32_t sampleFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { + if (pResultInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } taosSeedRand(taosSafeRand()); @@ -5248,7 +5601,7 @@ bool sampleFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) pInfo->data = (char*)pInfo + sizeof(SSampleInfo); pInfo->tuplePos = (STuplePos*)((char*)pInfo + sizeof(SSampleInfo) + pInfo->samples * pInfo->colBytes); - return true; + return TSDB_CODE_SUCCESS; } static void sampleAssignResult(SSampleInfo* pInfo, char* data, int32_t index) { @@ -5322,6 +5675,9 @@ int32_t sampleFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + return TSDB_CODE_OUT_OF_RANGE; + } int32_t currentRow = pBlock->info.rows; if (pInfo->numSampled == 0) { @@ -5330,8 +5686,14 @@ int32_t sampleFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { return code; } for (int32_t i = 0; i < pInfo->numSampled; ++i) { - colDataSetVal(pCol, currentRow + i, pInfo->data + i * pInfo->colBytes, false); + code = colDataSetVal(pCol, currentRow + i, pInfo->data + i * pInfo->colBytes, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } code = setSelectivityValue(pCtx, pBlock, &pInfo->tuplePos[i], currentRow + i); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } return code; @@ -5347,7 +5709,7 @@ bool getTailFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) { return true; } -bool tailFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { +int32_t tailFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { #if 0 if (!functionSetup(pCtx, pResultInfo)) { return false; @@ -5378,7 +5740,7 @@ bool tailFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { } #endif - return true; + return TSDB_CODE_SUCCESS; } static void tailAssignResult(STailItem* pItem, char* data, int32_t colBytes, TSKEY ts, bool isNull) { @@ -5483,7 +5845,7 @@ bool getUniqueFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) { return true; } -bool uniqueFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { +int32_t uniqueFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { #if 0 if (!functionSetup(pCtx, pResInfo)) { return false; @@ -5499,7 +5861,7 @@ bool uniqueFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { pInfo->pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); } #endif - return true; + return TSDB_CODE_SUCCESS; } #if 0 @@ -5580,9 +5942,12 @@ bool getModeFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) { return true; } -bool modeFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { - if (!functionSetup(pCtx, pResInfo)) { - return false; +int32_t modeFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { + if (pResInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } SModeInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo); @@ -5592,13 +5957,19 @@ bool modeFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { taosHashClear(pInfo->pHash); } else { pInfo->pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); + if (NULL == pInfo->pHash) { + return terrno; + } } pInfo->nullTupleSaved = false; pInfo->nullTuplePos.pageId = -1; pInfo->buf = taosMemoryMalloc(pInfo->colBytes); + if (NULL == pInfo->buf) { + return TSDB_CODE_OUT_OF_MEMORY; + } - return true; + return TSDB_CODE_SUCCESS; } static void modeFunctionCleanup(SModeInfo * pInfo) { @@ -5608,9 +5979,9 @@ static void modeFunctionCleanup(SModeInfo * pInfo) { static int32_t saveModeTupleData(SqlFunctionCtx* pCtx, char* data, SModeInfo *pInfo, STuplePos* pPos) { if (IS_VAR_DATA_TYPE(pInfo->colType)) { - memcpy(pInfo->buf, data, varDataTLen(data)); + (void)memcpy(pInfo->buf, data, varDataTLen(data)); } else { - memcpy(pInfo->buf, data, pInfo->colBytes); + (void)memcpy(pInfo->buf, data, pInfo->colBytes); } return doSaveTupleData(&pCtx->saveHandle, pInfo->buf, pInfo->colBytes, NULL, pPos, pCtx->pStore); @@ -5638,7 +6009,10 @@ static int32_t doModeAdd(SModeInfo* pInfo, int32_t rowIndex, SqlFunctionCtx* pCt } } - taosHashPut(pInfo->pHash, data, hashKeyBytes, &item, sizeof(SModeItem)); + code = taosHashPut(pInfo->pHash, data, hashKeyBytes, &item, sizeof(SModeItem)); + if (code != TSDB_CODE_SUCCESS) { + return code; + } } else { pHashItem->count += 1; if (pCtx->subsidiaries.num > 0) { @@ -5698,6 +6072,9 @@ int32_t modeFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); int32_t currentRow = pBlock->info.rows; + if (NULL == pCol) { + return TSDB_CODE_OUT_OF_RANGE; + } STuplePos resDataPos, resTuplePos; int32_t maxCount = 0; @@ -5715,8 +6092,9 @@ int32_t modeFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { } if (maxCount != 0) { - const char* pData = loadTupleData(pCtx, &resDataPos); - if (pData == NULL) { + char* pData = NULL; + code = loadTupleData(pCtx, &resDataPos, &pData); + if (pData == NULL || TSDB_CODE_SUCCESS != code) { code = terrno = TSDB_CODE_NOT_FOUND; qError("Load tuple data failed since %s, groupId:%" PRIu64 ", ts:%" PRId64, terrstr(), resDataPos.streamTupleKey.groupId, resDataPos.streamTupleKey.ts); @@ -5724,7 +6102,10 @@ int32_t modeFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { return code; } - colDataSetVal(pCol, currentRow, pData, false); + code = colDataSetVal(pCol, currentRow, pData, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } code = setSelectivityValue(pCtx, pBlock, &resTuplePos, currentRow); } else { colDataSetNULL(pCol, currentRow); @@ -5741,16 +6122,19 @@ bool getTwaFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) { return true; } -bool twaFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { - if (!functionSetup(pCtx, pResultInfo)) { - return false; +int32_t twaFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { + if (pResultInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } STwaInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); pInfo->numOfElems = 0; pInfo->p.key = INT64_MIN; pInfo->win = TSWINDOW_INITIALIZER; - return true; + return TSDB_CODE_SUCCESS; } static double twa_get_area(SPoint1 s, SPoint1 e) { @@ -5768,6 +6152,7 @@ static double twa_get_area(SPoint1 s, SPoint1 e) { } int32_t twaFunction(SqlFunctionCtx* pCtx) { + int32_t code = TSDB_CODE_SUCCESS; SInputColumnInfoData* pInput = &pCtx->input; SColumnInfoData* pInputCol = pInput->pData[0]; @@ -5782,8 +6167,16 @@ int32_t twaFunction(SqlFunctionCtx* pCtx) { funcInputUpdate(pCtx); SFuncInputRow row = {0}; + bool result = false; if (pCtx->start.key != INT64_MIN && last->key == INT64_MIN) { - while (funcInputGetNextRow(pCtx, &row)) { + while (1) { + code = funcInputGetNextRow(pCtx, &row, &result); + if (TSDB_CODE_SUCCESS != code) { + return code; + } + if (!result) { + break; + } if (row.isDataNull) { continue; } @@ -5798,7 +6191,14 @@ int32_t twaFunction(SqlFunctionCtx* pCtx) { break; } } else if (pInfo->p.key == INT64_MIN) { - while (funcInputGetNextRow(pCtx, &row)) { + while (1) { + code = funcInputGetNextRow(pCtx, &row, &result); + if (TSDB_CODE_SUCCESS != code) { + return code; + } + if (!result) { + break; + } if (row.isDataNull) { continue; } @@ -5816,181 +6216,69 @@ int32_t twaFunction(SqlFunctionCtx* pCtx) { SPoint1 st = {0}; // calculate the value of - switch (pInputCol->info.type) { - case TSDB_DATA_TYPE_TINYINT: { - while (funcInputGetNextRow(pCtx, &row)) { - if (row.isDataNull) { - continue; - } - pInfo->numOfElems++; - + while (1) { + code = funcInputGetNextRow(pCtx, &row, &result); + if (TSDB_CODE_SUCCESS != code) { + return code; + } + if (!result) { + break; + } + if (row.isDataNull) { + continue; + } + pInfo->numOfElems++; + switch (pInputCol->info.type) { + case TSDB_DATA_TYPE_TINYINT: { INIT_INTP_POINT(st, row.ts, *(int8_t*)row.pData); - if (pInfo->p.key == st.key) { - return TSDB_CODE_FUNC_DUP_TIMESTAMP; - } - - pInfo->dOutput += twa_get_area(pInfo->p, st); - pInfo->p = st; + break; } - break; - } - - case TSDB_DATA_TYPE_SMALLINT: { - while (funcInputGetNextRow(pCtx, &row)) { - if (row.isDataNull) { - continue; - } - pInfo->numOfElems++; - + case TSDB_DATA_TYPE_SMALLINT: { INIT_INTP_POINT(st, row.ts, *(int16_t*)row.pData); - if (pInfo->p.key == st.key) { - return TSDB_CODE_FUNC_DUP_TIMESTAMP; - } - - pInfo->dOutput += twa_get_area(pInfo->p, st); - pInfo->p = st; + break; } - break; - } - case TSDB_DATA_TYPE_INT: { - while (funcInputGetNextRow(pCtx, &row)) { - if (row.isDataNull) { - continue; - } - pInfo->numOfElems++; - + case TSDB_DATA_TYPE_INT: { INIT_INTP_POINT(st, row.ts, *(int32_t*)row.pData); - if (pInfo->p.key == st.key) { - return TSDB_CODE_FUNC_DUP_TIMESTAMP; - } - - pInfo->dOutput += twa_get_area(pInfo->p, st); - pInfo->p = st; + break; } - break; - } - case TSDB_DATA_TYPE_BIGINT: { - while (funcInputGetNextRow(pCtx, &row)) { - if (row.isDataNull) { - continue; - } - pInfo->numOfElems++; - + case TSDB_DATA_TYPE_BIGINT: { INIT_INTP_POINT(st, row.ts, *(int64_t*)row.pData); - if (pInfo->p.key == st.key) { - return TSDB_CODE_FUNC_DUP_TIMESTAMP; - } - - pInfo->dOutput += twa_get_area(pInfo->p, st); - pInfo->p = st; + break; } - break; - } - case TSDB_DATA_TYPE_FLOAT: { - while (funcInputGetNextRow(pCtx, &row)) { - if (row.isDataNull) { - continue; - } - pInfo->numOfElems++; - + case TSDB_DATA_TYPE_FLOAT: { INIT_INTP_POINT(st, row.ts, *(float_t*)row.pData); - if (pInfo->p.key == st.key) { - return TSDB_CODE_FUNC_DUP_TIMESTAMP; - } - - pInfo->dOutput += twa_get_area(pInfo->p, st); - pInfo->p = st; + break; } - break; - } - case TSDB_DATA_TYPE_DOUBLE: { - while (funcInputGetNextRow(pCtx, &row)) { - if (row.isDataNull) { - continue; - } - pInfo->numOfElems++; - + case TSDB_DATA_TYPE_DOUBLE: { INIT_INTP_POINT(st, row.ts, *(double*)row.pData); - if (pInfo->p.key == st.key) { - return TSDB_CODE_FUNC_DUP_TIMESTAMP; - } - - pInfo->dOutput += twa_get_area(pInfo->p, st); - pInfo->p = st; + break; } - break; - } - case TSDB_DATA_TYPE_UTINYINT: { - while (funcInputGetNextRow(pCtx, &row)) { - if (row.isDataNull) { - continue; - } - pInfo->numOfElems++; - + case TSDB_DATA_TYPE_UTINYINT: { INIT_INTP_POINT(st, row.ts, *(uint8_t*)row.pData); - if (pInfo->p.key == st.key) { - return TSDB_CODE_FUNC_DUP_TIMESTAMP; - } - - pInfo->dOutput += twa_get_area(pInfo->p, st); - pInfo->p = st; + break; } - break; - } - case TSDB_DATA_TYPE_USMALLINT: { - while (funcInputGetNextRow(pCtx, &row)) { - if (row.isDataNull) { - continue; - } - pInfo->numOfElems++; - + case TSDB_DATA_TYPE_USMALLINT: { INIT_INTP_POINT(st, row.ts, *(uint16_t*)row.pData); - if (pInfo->p.key == st.key) { - return TSDB_CODE_FUNC_DUP_TIMESTAMP; - } - - pInfo->dOutput += twa_get_area(pInfo->p, st); - pInfo->p = st; + break; } - break; - } - case TSDB_DATA_TYPE_UINT: { - while (funcInputGetNextRow(pCtx, &row)) { - if (row.isDataNull) { - continue; - } - pInfo->numOfElems++; - + case TSDB_DATA_TYPE_UINT: { INIT_INTP_POINT(st, row.ts, *(uint32_t*)row.pData); - if (pInfo->p.key == st.key) { - return TSDB_CODE_FUNC_DUP_TIMESTAMP; - } - - pInfo->dOutput += twa_get_area(pInfo->p, st); - pInfo->p = st; + break; } - break; - } - case TSDB_DATA_TYPE_UBIGINT: { - while (funcInputGetNextRow(pCtx, &row)) { - if (row.isDataNull) { - continue; - } - pInfo->numOfElems++; - + case TSDB_DATA_TYPE_UBIGINT: { INIT_INTP_POINT(st, row.ts, *(uint64_t*)row.pData); - if (pInfo->p.key == st.key) { - return TSDB_CODE_FUNC_DUP_TIMESTAMP; - } - - pInfo->dOutput += twa_get_area(pInfo->p, st); - pInfo->p = st; + break; } - break; + default: { + return TSDB_CODE_FUNC_FUNTION_PARA_TYPE; + } + } + if (pInfo->p.key == st.key) { + return TSDB_CODE_FUNC_DUP_TIMESTAMP; } - default: - return TSDB_CODE_FUNC_FUNTION_PARA_TYPE; + pInfo->dOutput += twa_get_area(pInfo->p, st); + pInfo->p = st; } // the last interpolated time window value @@ -6040,14 +6328,17 @@ int32_t twaFinalize(struct SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { return functionFinalize(pCtx, pBlock); } -bool blockDistSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { - if (!functionSetup(pCtx, pResultInfo)) { - return false; +int32_t blockDistSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { + if (pResultInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } STableBlockDistInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); pInfo->minRows = INT32_MAX; - return true; + return TSDB_CODE_SUCCESS; } int32_t blockDistFunction(SqlFunctionCtx* pCtx) { @@ -6059,7 +6350,10 @@ int32_t blockDistFunction(SqlFunctionCtx* pCtx) { STableBlockDistInfo* pDistInfo = GET_ROWCELL_INTERBUF(pResInfo); STableBlockDistInfo p1 = {0}; - tDeserializeBlockDistInfo(varDataVal(pInputCol->pData), varDataLen(pInputCol->pData), &p1); + if (tDeserializeBlockDistInfo(varDataVal(pInputCol->pData), varDataLen(pInputCol->pData), &p1) < 0) { + qError("failed to deserialize block dist info"); + return TSDB_CODE_FAILED; + } pDistInfo->numOfBlocks += p1.numOfBlocks; pDistInfo->numOfTables += p1.numOfTables; @@ -6154,6 +6448,9 @@ int32_t blockDistFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { STableBlockDistInfo* pData = GET_ROWCELL_INTERBUF(pResInfo); SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, 0); + if (NULL == pColInfo) { + return TSDB_CODE_OUT_OF_RANGE; + } if (pData->totalRows == 0) { pData->minRows = 0; @@ -6176,7 +6473,10 @@ int32_t blockDistFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { pData->numOfBlocks, pData->totalSize / 1024.0, averageSize / 1024.0, compRatio, '%'); varDataSetLen(st, len); - colDataSetVal(pColInfo, row++, st, false); + int32_t code = colDataSetVal(pColInfo, row++, st, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } int64_t avgRows = 0; if (pData->numOfBlocks > 0) { @@ -6186,22 +6486,34 @@ int32_t blockDistFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { len = sprintf(st + VARSTR_HEADER_SIZE, "Block_Rows=[%" PRId64 "] MinRows=[%d] MaxRows=[%d] AvgRows=[%" PRId64 "]", pData->totalRows, pData->minRows, pData->maxRows, avgRows); varDataSetLen(st, len); - colDataSetVal(pColInfo, row++, st, false); + code = colDataSetVal(pColInfo, row++, st, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } len = sprintf(st + VARSTR_HEADER_SIZE, "Inmem_Rows=[%d] Stt_Rows=[%d] ", pData->numOfInmemRows, pData->numOfSttRows); varDataSetLen(st, len); - colDataSetVal(pColInfo, row++, st, false); + code = colDataSetVal(pColInfo, row++, st, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } len = sprintf(st + VARSTR_HEADER_SIZE, "Total_Tables=[%d] Total_Filesets=[%d] Total_Vgroups=[%d]", pData->numOfTables, pData->numOfFiles, pData->numOfVgroups); varDataSetLen(st, len); - colDataSetVal(pColInfo, row++, st, false); + code = colDataSetVal(pColInfo, row++, st, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } len = sprintf(st + VARSTR_HEADER_SIZE, "--------------------------------------------------------------------------------"); varDataSetLen(st, len); - colDataSetVal(pColInfo, row++, st, false); + code = colDataSetVal(pColInfo, row++, st, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } int32_t maxVal = 0; int32_t minVal = INT32_MAX; @@ -6240,7 +6552,10 @@ int32_t blockDistFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { } varDataSetLen(st, len); - colDataSetVal(pColInfo, row++, st, false); + code = colDataSetVal(pColInfo, row++, st, false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } return TSDB_CODE_SUCCESS; @@ -6251,9 +6566,12 @@ bool getDerivativeFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) { return true; } -bool derivativeFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { - if (!functionSetup(pCtx, pResInfo)) { - return false; // not initialized since it has been initialized +int32_t derivativeFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { + if (pResInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } SDerivInfo* pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo); @@ -6262,7 +6580,7 @@ bool derivativeFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { pDerivInfo->prevTs = -1; pDerivInfo->tsWindow = pCtx->param[1].param.i; pDerivInfo->valueSet = false; - return true; + return TSDB_CODE_SUCCESS; } int32_t derivativeFunction(SqlFunctionCtx* pCtx) { @@ -6275,13 +6593,22 @@ int32_t derivativeFunction(SqlFunctionCtx* pCtx) { int32_t numOfElems = 0; SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput; SColumnInfoData* pTsOutput = pCtx->pTsOutput; + int32_t code = TSDB_CODE_SUCCESS; funcInputUpdate(pCtx); double v = 0; if (pCtx->order == TSDB_ORDER_ASC) { SFuncInputRow row = {0}; - while (funcInputGetNextRow(pCtx, &row)) { + bool result = false; + while (1) { + code = funcInputGetNextRow(pCtx, &row, &result); + if (TSDB_CODE_SUCCESS != code) { + return code; + } + if (!result) { + break; + } if (row.isDataNull) { continue; } @@ -6302,7 +6629,10 @@ int32_t derivativeFunction(SqlFunctionCtx* pCtx) { if (isinf(r) || isnan(r)) { colDataSetNULL(pOutput, pos); } else { - colDataSetVal(pOutput, pos, (const char*)&r, false); + code = colDataSetVal(pOutput, pos, (const char*)&r, false); + if (code != TSDB_CODE_SUCCESS) { + return code; + } } if (pTsOutput != NULL) { @@ -6311,7 +6641,10 @@ int32_t derivativeFunction(SqlFunctionCtx* pCtx) { // handle selectivity if (pCtx->subsidiaries.num > 0) { - appendSelectivityCols(pCtx, row.block, row.rowIndex, pos); + code = appendSelectivityCols(pCtx, row.block, row.rowIndex, pos); + if (code != TSDB_CODE_SUCCESS) { + return code; + } } numOfElems++; @@ -6323,7 +6656,15 @@ int32_t derivativeFunction(SqlFunctionCtx* pCtx) { } } else { SFuncInputRow row = {0}; - while (funcInputGetNextRow(pCtx, &row)) { + bool result = false; + while (1) { + code = funcInputGetNextRow(pCtx, &row, &result); + if (TSDB_CODE_SUCCESS != code) { + return code; + } + if (!result) { + break; + } if (row.isDataNull) { continue; } @@ -6344,7 +6685,10 @@ int32_t derivativeFunction(SqlFunctionCtx* pCtx) { if (isinf(r) || isnan(r)) { colDataSetNULL(pOutput, pos); } else { - colDataSetVal(pOutput, pos, (const char*)&r, false); + code = colDataSetVal(pOutput, pos, (const char*)&r, false); + if (code != TSDB_CODE_SUCCESS) { + return code; + } } if (pTsOutput != NULL) { @@ -6353,7 +6697,10 @@ int32_t derivativeFunction(SqlFunctionCtx* pCtx) { // handle selectivity if (pCtx->subsidiaries.num > 0) { - appendSelectivityCols(pCtx, row.block, row.rowIndex, pos); + code = appendSelectivityCols(pCtx, row.block, row.rowIndex, pos); + if (code != TSDB_CODE_SUCCESS) { + return code; + } } numOfElems++; } @@ -6377,9 +6724,12 @@ bool getIrateFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv) { return true; } -bool irateFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { - if (!functionSetup(pCtx, pResInfo)) { - return false; // not initialized since it has been initialized +int32_t irateFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { + if (pResInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } SRateInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo); @@ -6390,7 +6740,7 @@ bool irateFuncSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { pInfo->lastValue = (double)INT64_MIN; pInfo->hasResult = 0; - return true; + return TSDB_CODE_SUCCESS; } static void doSaveRateInfo(SRateInfo* pRateInfo, bool isFirst, int64_t ts, char* pk, double v) { @@ -6399,14 +6749,14 @@ static void doSaveRateInfo(SRateInfo* pRateInfo, bool isFirst, int64_t ts, char* pRateInfo->firstKey = ts; if (pRateInfo->firstPk) { int32_t pkBytes = IS_VAR_DATA_TYPE(pRateInfo->pkType) ? varDataTLen(pk) : pRateInfo->pkBytes; - memcpy(pRateInfo->firstPk, pk, pkBytes); + (void)memcpy(pRateInfo->firstPk, pk, pkBytes); } } else { pRateInfo->lastValue = v; pRateInfo->lastKey = ts; if (pRateInfo->lastPk) { int32_t pkBytes = IS_VAR_DATA_TYPE(pRateInfo->pkType) ? varDataTLen(pk) : pRateInfo->pkBytes; - memcpy(pRateInfo->lastPk, pk, pkBytes); + (void)memcpy(pRateInfo->lastPk, pk, pkBytes); } } } @@ -6429,6 +6779,7 @@ static void initializeRateInfo(SqlFunctionCtx* pCtx, SRateInfo* pRateInfo, bool } int32_t irateFunction(SqlFunctionCtx* pCtx) { + int32_t code = TSDB_CODE_SUCCESS; SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); SRateInfo* pRateInfo = GET_ROWCELL_INTERBUF(pResInfo); @@ -6444,7 +6795,15 @@ int32_t irateFunction(SqlFunctionCtx* pCtx) { int32_t numOfElems = 0; int32_t type = pInputCol->info.type; SFuncInputRow row = {0}; - while (funcInputGetNextRow(pCtx, &row)) { + bool result = false; + while (1) { + code = funcInputGetNextRow(pCtx, &row, &result); + if (TSDB_CODE_SUCCESS != code) { + return code; + } + if (!result) { + break; + } if (row.isDataNull) { continue; } @@ -6589,30 +6948,40 @@ int32_t iratePartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t resultBytes = getIrateInfoSize(pInfo->pkBytes); char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char)); - memcpy(varDataVal(res), pInfo, resultBytes); + if (NULL == res) { + return TSDB_CODE_OUT_OF_MEMORY; + } + (void)memcpy(varDataVal(res), pInfo, resultBytes); varDataSetLen(res, resultBytes); int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + taosMemoryFree(res); + return TSDB_CODE_OUT_OF_RANGE; + } - colDataSetVal(pCol, pBlock->info.rows, res, false); + int32_t code = colDataSetVal(pCol, pBlock->info.rows, res, false); taosMemoryFree(res); - return pResInfo->numOfRes; + return code; } int32_t irateFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + return TSDB_CODE_OUT_OF_RANGE; + } SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); pResInfo->isNullRes = (pResInfo->numOfRes == 0) ? 1 : 0; SRateInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo); double result = doCalcRate(pInfo, (double)TSDB_TICK_PER_SECOND(pCtx->param[1].param.i)); - colDataSetVal(pCol, pBlock->info.rows, (const char*)&result, pResInfo->isNullRes); + int32_t code = colDataSetVal(pCol, pBlock->info.rows, (const char*)&result, pResInfo->isNullRes); - return pResInfo->numOfRes; + return code; } int32_t groupConstValueFunction(SqlFunctionCtx* pCtx) { @@ -6637,10 +7006,10 @@ int32_t groupConstValueFunction(SqlFunctionCtx* pCtx) { char* data = colDataGetData(pInputCol, startIndex); if (IS_VAR_DATA_TYPE(pInputCol->info.type)) { - memcpy(pInfo->data, data, - (pInputCol->info.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(data) : varDataTLen(data)); + (void)memcpy(pInfo->data, data, + (pInputCol->info.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(data) : varDataTLen(data)); } else { - memcpy(pInfo->data, data, pInputCol->info.bytes); + (void)memcpy(pInfo->data, data, pInputCol->info.bytes); } pInfo->hasResult = true; @@ -6656,7 +7025,11 @@ int32_t groupKeyFunction(SqlFunctionCtx* pCtx) { int32_t groupConstValueFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t slotId = pCtx->pExpr->base.resSchema.slotId; + int32_t code = TSDB_CODE_SUCCESS; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if (NULL == pCol) { + return TSDB_CODE_OUT_OF_RANGE; + } SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); @@ -6665,13 +7038,16 @@ int32_t groupConstValueFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { if (pInfo->hasResult) { int32_t currentRow = pBlock->info.rows; for (; currentRow < pBlock->info.rows + pResInfo->numOfRes; ++currentRow) { - colDataSetVal(pCol, currentRow, pInfo->data, pInfo->isNull ? true : false); + code = colDataSetVal(pCol, currentRow, pInfo->data, pInfo->isNull ? true : false); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } } else { pResInfo->numOfRes = 0; } - return pResInfo->numOfRes; + return code; } int32_t groupKeyFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock){ @@ -6697,10 +7073,10 @@ int32_t groupKeyCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { } if (IS_VAR_DATA_TYPE(pSourceCtx->resDataInfo.type)) { - memcpy(pDBuf->data, pSBuf->data, + (void)memcpy(pDBuf->data, pSBuf->data, (pSourceCtx->resDataInfo.type == TSDB_DATA_TYPE_JSON) ? getJsonValueLen(pSBuf->data) : varDataTLen(pSBuf->data)); } else { - memcpy(pDBuf->data, pSBuf->data, pSourceCtx->resDataInfo.bytes); + (void)memcpy(pDBuf->data, pSBuf->data, pSourceCtx->resDataInfo.bytes); } pDBuf->hasResult = true; diff --git a/source/libs/function/src/detail/tavgfunction.c b/source/libs/function/src/detail/tavgfunction.c index 3d51f0cd16..df9edb9948 100644 --- a/source/libs/function/src/detail/tavgfunction.c +++ b/source/libs/function/src/detail/tavgfunction.c @@ -354,14 +354,17 @@ bool getAvgFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) { return true; } -bool avgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { - if (!functionSetup(pCtx, pResultInfo)) { - return false; +int32_t avgFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultInfo) { + if (pResultInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (TSDB_CODE_SUCCESS != functionSetup(pCtx, pResultInfo)) { + return TSDB_CODE_FUNC_SETUP_ERROR; } SAvgRes* pRes = GET_ROWCELL_INTERBUF(pResultInfo); - memset(pRes, 0, sizeof(SAvgRes)); - return true; + (void)memset(pRes, 0, sizeof(SAvgRes)); + return TSDB_CODE_SUCCESS; } static int32_t calculateAvgBySMAInfo(SAvgRes* pRes, int32_t numOfRows, int32_t type, const SColumnDataAgg* pAgg) { @@ -849,15 +852,23 @@ int32_t avgPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { SAvgRes* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); int32_t resultBytes = getAvgInfoSize(); char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char)); - - memcpy(varDataVal(res), pInfo, resultBytes); + int32_t code = TSDB_CODE_SUCCESS; + if (NULL == res) { + return TSDB_CODE_OUT_OF_MEMORY; + } + (void)memcpy(varDataVal(res), pInfo, resultBytes); varDataSetLen(res, resultBytes); int32_t slotId = pCtx->pExpr->base.resSchema.slotId; SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + if(NULL == pCol) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _exit; + } - colDataSetVal(pCol, pBlock->info.rows, res, false); + code = colDataSetVal(pCol, pBlock->info.rows, res, false); +_exit: taosMemoryFree(res); - return pResInfo->numOfRes; + return code; } diff --git a/source/libs/function/src/detail/tminmax.c b/source/libs/function/src/detail/tminmax.c index 590a15c753..b316b7a512 100644 --- a/source/libs/function/src/detail/tminmax.c +++ b/source/libs/function/src/detail/tminmax.c @@ -20,6 +20,7 @@ #include "tglobal.h" #define __COMPARE_ACQUIRED_MAX(i, end, bm, _data, ctx, val, pos) \ + int32_t code = TSDB_CODE_SUCCESS; \ for (; i < (end); ++i) { \ if (colDataIsNull_f(bm, i)) { \ continue; \ @@ -28,12 +29,16 @@ if ((val) < (_data)[i]) { \ (val) = (_data)[i]; \ if ((ctx)->subsidiaries.num > 0) { \ - updateTupleData((ctx), i, (ctx)->pSrcBlock, pos); \ + code = updateTupleData((ctx), i, (ctx)->pSrcBlock, pos); \ + if (TSDB_CODE_SUCCESS != code) { \ + return code; \ + } \ } \ } \ } #define __COMPARE_ACQUIRED_MIN(i, end, bm, _data, ctx, val, pos) \ + int32_t code = TSDB_CODE_SUCCESS; \ for (; i < (end); ++i) { \ if (colDataIsNull_f(bm, i)) { \ continue; \ @@ -42,7 +47,10 @@ if ((val) > (_data)[i]) { \ (val) = (_data)[i]; \ if ((ctx)->subsidiaries.num > 0) { \ - updateTupleData((ctx), i, (ctx)->pSrcBlock, pos); \ + code = updateTupleData((ctx), i, (ctx)->pSrcBlock, pos); \ + if (TSDB_CODE_SUCCESS != code) { \ + return code; \ + } \ } \ } \ } @@ -571,7 +579,7 @@ static int32_t findRowIndex(int32_t start, int32_t num, SColumnInfoData* pCol, c return -1; } -static void doExtractVal(SColumnInfoData* pCol, int32_t i, int32_t end, SqlFunctionCtx* pCtx, SMinmaxResInfo* pBuf, +static int32_t doExtractVal(SColumnInfoData* pCol, int32_t i, int32_t end, SqlFunctionCtx* pCtx, SMinmaxResInfo* pBuf, bool isMinFunc) { if (isMinFunc) { switch (pCol->info.type) { @@ -700,6 +708,7 @@ static void doExtractVal(SColumnInfoData* pCol, int32_t i, int32_t end, SqlFunct } } } + return TSDB_CODE_SUCCESS; } static int32_t saveRelatedTupleTag(SqlFunctionCtx* pCtx, SInputColumnInfoData* pInput, void* tval) { @@ -840,7 +849,7 @@ int32_t doMinMaxHelper(SqlFunctionCtx* pCtx, int32_t isMinFunc, int32_t* nElems) break; } default: - memcpy(&pBuf->v, p, pCol->info.bytes); + (void)memcpy(&pBuf->v, p, pCol->info.bytes); break; } @@ -858,7 +867,7 @@ int32_t doMinMaxHelper(SqlFunctionCtx* pCtx, int32_t isMinFunc, int32_t* nElems) goto _over; } - doExtractVal(pCol, i, end, pCtx, pBuf, isMinFunc); + code = doExtractVal(pCol, i, end, pCtx, pBuf, isMinFunc); } else { numOfElems = numOfRows; diff --git a/source/libs/function/src/functionMgt.c b/source/libs/function/src/functionMgt.c index 8255997836..0424b2f179 100644 --- a/source/libs/function/src/functionMgt.c +++ b/source/libs/function/src/functionMgt.c @@ -35,14 +35,14 @@ static void doInitFunctionTable() { gFunMgtService.pFuncNameHashTable = taosHashInit(funcMgtBuiltinsNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); if (NULL == gFunMgtService.pFuncNameHashTable) { - initFunctionCode = TSDB_CODE_FAILED; + initFunctionCode = terrno; return; } for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) { if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name, strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) { - initFunctionCode = TSDB_CODE_FAILED; + initFunctionCode = terrno; return; } } @@ -61,7 +61,7 @@ static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) { } int32_t fmFuncMgtInit() { - taosThreadOnce(&functionHashTableInit, doInitFunctionTable); + (void)taosThreadOnce(&functionHashTableInit, doInitFunctionTable); return initFunctionCode; } @@ -115,20 +115,24 @@ EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWin return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow); } -EFuncDataRequired fmFuncDynDataRequired(int32_t funcId, void* pRes, SDataBlockInfo* pBlockInfo) { +int32_t fmFuncDynDataRequired(int32_t funcId, void* pRes, SDataBlockInfo* pBlockInfo, int32_t *reqStatus) { if (fmIsUserDefinedFunc(funcId) || funcId < 0 || funcId >= funcMgtBuiltinsNum) { + *reqStatus = -1; return TSDB_CODE_FAILED; } const char* name = funcMgtBuiltins[funcId].name; if ((strcmp(name, "_group_key") == 0) || (strcmp(name, "_select_value") == 0)) { - return FUNC_DATA_REQUIRED_NOT_LOAD; + *reqStatus = FUNC_DATA_REQUIRED_NOT_LOAD; + return TSDB_CODE_SUCCESS;; } if (funcMgtBuiltins[funcId].dynDataRequiredFunc == NULL) { - return FUNC_DATA_REQUIRED_DATA_LOAD; + *reqStatus = FUNC_DATA_REQUIRED_DATA_LOAD; + return TSDB_CODE_SUCCESS; } else { - return funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo); + *reqStatus = funcMgtBuiltins[funcId].dynDataRequiredFunc(pRes, pBlockInfo); + return TSDB_CODE_SUCCESS; } } @@ -378,29 +382,30 @@ static int32_t getFuncInfo(SFunctionNode* pFunc) { return fmGetFuncInfo(pFunc, msg, sizeof(msg)); } -SFunctionNode* createFunction(const char* pName, SNodeList* pParameterList) { - SFunctionNode* pFunc = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION); - if (NULL == pFunc) { - return NULL; +int32_t createFunction(const char* pName, SNodeList* pParameterList, SFunctionNode** pFunc) { + *pFunc = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION); + if (NULL == *pFunc) { + return TSDB_CODE_OUT_OF_MEMORY; } - snprintf(pFunc->functionName, sizeof(pFunc->functionName), "%s", pName); - pFunc->pParameterList = pParameterList; - if (TSDB_CODE_SUCCESS != getFuncInfo(pFunc)) { - pFunc->pParameterList = NULL; - nodesDestroyNode((SNode*)pFunc); - return NULL; + (void)snprintf((*pFunc)->functionName, sizeof((*pFunc)->functionName), "%s", pName); + (*pFunc)->pParameterList = pParameterList; + int32_t code = getFuncInfo((*pFunc)); + if (TSDB_CODE_SUCCESS != code) { + (*pFunc)->pParameterList = NULL; + nodesDestroyNode((SNode*)*pFunc); + return code; } - return pFunc; + return code; } -static SNode* createColumnByFunc(const SFunctionNode* pFunc) { - SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN); - if (NULL == pCol) { - return NULL; +static int32_t createColumnByFunc(const SFunctionNode* pFunc, SColumnNode** pCol) { + *pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN); + if (NULL == *pCol) { + return TSDB_CODE_OUT_OF_MEMORY; } - strcpy(pCol->colName, pFunc->node.aliasName); - pCol->node.resType = pFunc->node.resType; - return (SNode*)pCol; + (void)strcpy((*pCol)->colName, pFunc->node.aliasName); + (*pCol)->node.resType = pFunc->node.resType; + return TSDB_CODE_SUCCESS; } bool fmIsDistExecFunc(int32_t funcId) { @@ -418,17 +423,17 @@ static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNod if (NULL == pParameterList) { return TSDB_CODE_OUT_OF_MEMORY; } - *pPartialFunc = createFunction(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pParameterList); - if (NULL == *pPartialFunc) { + int32_t code = createFunction(funcMgtBuiltins[pSrcFunc->funcId].pPartialFunc, pParameterList,pPartialFunc ); + if (TSDB_CODE_SUCCESS != code) { nodesDestroyList(pParameterList); - return TSDB_CODE_OUT_OF_MEMORY; + return code; } (*pPartialFunc)->hasOriginalFunc = true; (*pPartialFunc)->originalFuncId = pSrcFunc->hasOriginalFunc ? pSrcFunc->originalFuncId : pSrcFunc->funcId; char name[TSDB_FUNC_NAME_LEN + TSDB_NAME_DELIMITER_LEN + TSDB_POINTER_PRINT_BYTES + 1] = {0}; int32_t len = snprintf(name, sizeof(name) - 1, "%s.%p", (*pPartialFunc)->functionName, pSrcFunc); - taosCreateMD5Hash(name, len); - strncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN - 1); + (void)taosCreateMD5Hash(name, len); + (void)strncpy((*pPartialFunc)->node.aliasName, name, TSDB_COL_NAME_LEN - 1); (*pPartialFunc)->hasPk = pSrcFunc->hasPk; (*pPartialFunc)->pkBytes = pSrcFunc->pkBytes; return TSDB_CODE_SUCCESS; @@ -436,7 +441,11 @@ static int32_t createPartialFunction(const SFunctionNode* pSrcFunc, SFunctionNod static int32_t createMergeFuncPara(const SFunctionNode* pSrcFunc, const SFunctionNode* pPartialFunc, SNodeList** pParameterList) { - SNode* pRes = createColumnByFunc(pPartialFunc); + SNode *pRes = NULL; + int32_t code = createColumnByFunc(pPartialFunc, (SColumnNode**)&pRes); + if (TSDB_CODE_SUCCESS != code) { + return code; + } if (NULL != funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc) { return funcMgtBuiltins[pSrcFunc->funcId].createMergeParaFuc(pSrcFunc->pParameterList, pRes, pParameterList); } else { @@ -452,16 +461,13 @@ static int32_t createMidFunction(const SFunctionNode* pSrcFunc, const SFunctionN int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList); if (TSDB_CODE_SUCCESS == code) { if(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc != NULL){ - pFunc = createFunction(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pParameterList); + code = createFunction(funcMgtBuiltins[pSrcFunc->funcId].pMiddleFunc, pParameterList, &pFunc); }else{ - pFunc = createFunction(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pParameterList); - } - if (NULL == pFunc) { - code = TSDB_CODE_OUT_OF_MEMORY; + code = createFunction(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pParameterList, &pFunc); } } if (TSDB_CODE_SUCCESS == code) { - strcpy(pFunc->node.aliasName, pPartialFunc->node.aliasName); + (void)strcpy(pFunc->node.aliasName, pPartialFunc->node.aliasName); } if (TSDB_CODE_SUCCESS == code) { @@ -481,10 +487,7 @@ static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctio int32_t code = createMergeFuncPara(pSrcFunc, pPartialFunc, &pParameterList); if (TSDB_CODE_SUCCESS == code) { - pFunc = createFunction(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pParameterList); - if (NULL == pFunc) { - code = TSDB_CODE_OUT_OF_MEMORY; - } + code = createFunction(funcMgtBuiltins[pSrcFunc->funcId].pMergeFunc, pParameterList, &pFunc); } if (TSDB_CODE_SUCCESS == code) { pFunc->hasOriginalFunc = true; @@ -493,7 +496,7 @@ static int32_t createMergeFunction(const SFunctionNode* pSrcFunc, const SFunctio if (fmIsSameInOutType(pSrcFunc->funcId)) { pFunc->node.resType = pSrcFunc->node.resType; } - strcpy(pFunc->node.aliasName, pSrcFunc->node.aliasName); + (void)strcpy(pFunc->node.aliasName, pSrcFunc->node.aliasName); } if (TSDB_CODE_SUCCESS == code) { @@ -541,13 +544,13 @@ static int32_t fmCreateStateFunc(const SFunctionNode* pFunc, SFunctionNode** pSt if (funcMgtBuiltins[pFunc->funcId].pStateFunc) { SNodeList* pParams = nodesCloneList(pFunc->pParameterList); if (!pParams) return TSDB_CODE_OUT_OF_MEMORY; - *pStateFunc = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams); - if (!*pStateFunc) { + int32_t code = createFunction(funcMgtBuiltins[pFunc->funcId].pStateFunc, pParams, pStateFunc); + if (TSDB_CODE_SUCCESS != code) { nodesDestroyList(pParams); return TSDB_CODE_FUNC_FUNTION_ERROR; } - strcpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName); - strcpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias); + (void)strcpy((*pStateFunc)->node.aliasName, pFunc->node.aliasName); + (void)strcpy((*pStateFunc)->node.userAlias, pFunc->node.userAlias); } return TSDB_CODE_SUCCESS; } @@ -587,13 +590,13 @@ static int32_t fmCreateStateMergeFunc(SFunctionNode* pFunc, SFunctionNode** pSta if (funcMgtBuiltins[pFunc->funcId].pMergeFunc) { SNodeList* pParams = nodesCloneList(pFunc->pParameterList); if (!pParams) return TSDB_CODE_OUT_OF_MEMORY; - *pStateMergeFunc = createFunction(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pParams); - if (!*pStateMergeFunc) { + int32_t code = createFunction(funcMgtBuiltins[pFunc->funcId].pMergeFunc, pParams, pStateMergeFunc); + if (TSDB_CODE_SUCCESS != code) { nodesDestroyList(pParams); - return TSDB_CODE_FUNC_FUNTION_ERROR; + return code; } - strcpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName); - strcpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias); + (void)strcpy((*pStateMergeFunc)->node.aliasName, pFunc->node.aliasName); + (void)strcpy((*pStateMergeFunc)->node.userAlias, pFunc->node.userAlias); } return TSDB_CODE_SUCCESS; } @@ -646,6 +649,9 @@ bool fmIsMyStateFunc(int32_t funcId, int32_t stateFuncId) { } if (strcmp(pFunc->pStateFunc, pStateFunc->name) == 0) return true; int32_t stateMergeFuncId = fmGetFuncId(pFunc->pStateFunc); + if (stateMergeFuncId == -1) { + return false; + } const SBuiltinFuncDefinition* pStateMergeFunc = &funcMgtBuiltins[stateMergeFuncId]; return strcmp(pStateFunc->name, pStateMergeFunc->pMergeFunc) == 0; } diff --git a/source/libs/function/src/thistogram.c b/source/libs/function/src/thistogram.c index b56691f35d..f57f6aa118 100644 --- a/source/libs/function/src/thistogram.c +++ b/source/libs/function/src/thistogram.c @@ -32,9 +32,12 @@ */ static int32_t histogramCreateBin(SHistogramInfo* pHisto, int32_t index, double val); -SHistogramInfo* tHistogramCreate(int32_t numOfEntries) { +int32_t tHistogramCreate(int32_t numOfEntries, SHistogramInfo** pHisto) { /* need one redundant slot */ - SHistogramInfo* pHisto = taosMemoryMalloc(sizeof(SHistogramInfo) + sizeof(SHistBin) * (numOfEntries + 1)); + *pHisto = taosMemoryMalloc(sizeof(SHistogramInfo) + sizeof(SHistBin) * (numOfEntries + 1)); + if (NULL == *pHisto) { + return TSDB_CODE_OUT_OF_MEMORY; + } #if !defined(USE_ARRAYLIST) pHisto->pList = SSkipListCreate(MAX_SKIP_LIST_LEVEL, TSDB_DATA_TYPE_DOUBLE, sizeof(double)); @@ -46,11 +49,12 @@ SHistogramInfo* tHistogramCreate(int32_t numOfEntries) { pss->pTree = pHisto->pLoserTree; #endif - return tHistogramCreateFrom(pHisto, numOfEntries); + *pHisto = tHistogramCreateFrom(*pHisto, numOfEntries); + return TSDB_CODE_SUCCESS; } SHistogramInfo* tHistogramCreateFrom(void* pBuf, int32_t numOfBins) { - memset(pBuf, 0, sizeof(SHistogramInfo) + sizeof(SHistBin) * (numOfBins + 1)); + (void)memset(pBuf, 0, sizeof(SHistogramInfo) + sizeof(SHistBin) * (numOfBins + 1)); SHistogramInfo* pHisto = (SHistogramInfo*)pBuf; pHisto->elems = (SHistBin*)((char*)pBuf + sizeof(SHistogramInfo)); @@ -67,15 +71,19 @@ SHistogramInfo* tHistogramCreateFrom(void* pBuf, int32_t numOfBins) { } int32_t tHistogramAdd(SHistogramInfo** pHisto, double val) { + int32_t code = TSDB_CODE_SUCCESS; if (*pHisto == NULL) { - *pHisto = tHistogramCreate(MAX_HISTOGRAM_BIN); + code = tHistogramCreate(MAX_HISTOGRAM_BIN, pHisto); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } #if defined(USE_ARRAYLIST) int32_t idx = histoBinarySearch((*pHisto)->elems, (*pHisto)->numOfEntries, val); if (ASSERTS(idx >= 0 && idx <= (*pHisto)->maxEntries && (*pHisto)->elems != NULL, "tHistogramAdd Error, idx:%d, maxEntries:%d, elems:%p", idx, (*pHisto)->maxEntries, (*pHisto)->elems)) { - return -1; + return TSDB_CODE_FAILED; } if ((*pHisto)->elems[idx].val == val && idx >= 0) { @@ -89,23 +97,23 @@ int32_t tHistogramAdd(SHistogramInfo** pHisto, double val) { if (idx > 0) { if (ASSERTS((*pHisto)->elems[idx - 1].val <= val, "tHistogramAdd Error, elems[%d].val:%lf, val:%lf", idx - 1, (*pHisto)->elems[idx - 1].val, val)) { - return -1; + return TSDB_CODE_FAILED; } } else { if (ASSERTS((*pHisto)->elems[idx].val > val, "tHistogramAdd Error, elems[%d].val:%lf, val:%lf", idx, (*pHisto)->elems[idx].val, val)) { - return -1; + return TSDB_CODE_FAILED; } } } else if ((*pHisto)->numOfElems > 0) { if (ASSERTS((*pHisto)->elems[(*pHisto)->numOfEntries].val <= val, "tHistogramAdd Error, elems[%d].val:%lf, val:%lf", (*pHisto)->numOfEntries, (*pHisto)->elems[idx].val, val)) { - return -1; + return TSDB_CODE_FAILED; } } - int32_t code = histogramCreateBin(*pHisto, idx, val); - if (code != 0) { + code = histogramCreateBin(*pHisto, idx, val); + if (code != TSDB_CODE_SUCCESS) { return code; } } @@ -286,7 +294,7 @@ int32_t tHistogramAdd(SHistogramInfo** pHisto, double val) { } (*pHisto)->numOfElems += 1; - return 0; + return code; } int32_t histoBinarySearch(SHistBin* pEntry, int32_t len, double val) { @@ -335,7 +343,7 @@ static void histogramMergeImpl(SHistBin* pHistBin, int32_t* size) { s1->val = newVal; s1->num = s1->num + s2->num; - memmove(&pHistBin[index + 1], &pHistBin[index + 2], (oldSize - index - 2) * sizeof(SHistBin)); + (void)memmove(&pHistBin[index + 1], &pHistBin[index + 2], (oldSize - index - 2) * sizeof(SHistBin)); (*size) -= 1; #endif } @@ -345,12 +353,12 @@ int32_t histogramCreateBin(SHistogramInfo* pHisto, int32_t index, double val) { #if defined(USE_ARRAYLIST) int32_t remain = pHisto->numOfEntries - index; if (remain > 0) { - memmove(&pHisto->elems[index + 1], &pHisto->elems[index], sizeof(SHistBin) * remain); + (void)memmove(&pHisto->elems[index + 1], &pHisto->elems[index], sizeof(SHistBin) * remain); } if (ASSERTS(index >= 0 && index <= pHisto->maxEntries, "histogramCreateBin Error, index:%d, maxEntries:%d", index, pHisto->maxEntries)) { - return -1; + return TSDB_CODE_FAILED; } pHisto->elems[index].num = 1; @@ -367,10 +375,10 @@ int32_t histogramCreateBin(SHistogramInfo* pHisto, int32_t index, double val) { #endif if (ASSERTS(pHisto->numOfEntries <= pHisto->maxEntries, "histogramCreateBin Error, numOfEntries:%d, maxEntries:%d", pHisto->numOfEntries, pHisto->maxEntries)) { - return -1; + return TSDB_CODE_FAILED; } - return 0; + return TSDB_CODE_SUCCESS; } void tHistogramDestroy(SHistogramInfo** pHisto) { @@ -383,17 +391,17 @@ void tHistogramDestroy(SHistogramInfo** pHisto) { } void tHistogramPrint(SHistogramInfo* pHisto) { - printf("total entries: %d, elements: %" PRId64 "\n", pHisto->numOfEntries, pHisto->numOfElems); + (void)printf("total entries: %d, elements: %" PRId64 "\n", pHisto->numOfEntries, pHisto->numOfElems); #if defined(USE_ARRAYLIST) for (int32_t i = 0; i < pHisto->numOfEntries; ++i) { - printf("%d: (%f, %" PRId64 ")\n", i + 1, pHisto->elems[i].val, pHisto->elems[i].num); + (void)printf("%d: (%f, %" PRId64 ")\n", i + 1, pHisto->elems[i].val, pHisto->elems[i].num); } #else tSkipListNode* pNode = pHisto->pList->pHead.pForward[0]; for (int32_t i = 0; i < pHisto->numOfEntries; ++i) { SHistBin* pEntry = (SHistBin*)pNode->pData; - printf("%d: (%f, %" PRId64 ")\n", i + 1, pEntry->val, pEntry->num); + (void)printf("%d: (%f, %" PRId64 ")\n", i + 1, pEntry->val, pEntry->num); pNode = pNode->pForward[0]; } #endif @@ -443,21 +451,24 @@ int64_t tHistogramSum(SHistogramInfo* pHisto, double v) { #endif } -double* tHistogramUniform(SHistogramInfo* pHisto, double* ratio, int32_t num) { +int32_t tHistogramUniform(SHistogramInfo* pHisto, double* ratio, int32_t num, double** pVal) { #if defined(USE_ARRAYLIST) - double* pVal = taosMemoryMalloc(num * sizeof(double)); + *pVal = taosMemoryMalloc(num * sizeof(double)); + if (NULL == *pVal) { + return TSDB_CODE_OUT_OF_MEMORY; + } for (int32_t i = 0; i < num; ++i) { double numOfElem = (ratio[i] / 100) * pHisto->numOfElems; if (numOfElem == 0) { - pVal[i] = pHisto->min; + (*pVal)[i] = pHisto->min; continue; } else if (numOfElem <= pHisto->elems[0].num) { - pVal[i] = pHisto->elems[0].val; + (*pVal)[i] = pHisto->elems[0].val; continue; } else if (numOfElem == pHisto->numOfElems) { - pVal[i] = pHisto->max; + (*pVal)[i] = pHisto->max; continue; } @@ -479,37 +490,39 @@ double* tHistogramUniform(SHistogramInfo* pHisto, double* ratio, int32_t num) { double delta = numOfElem - total; if (fabs(delta) < FLT_EPSILON) { - pVal[i] = pHisto->elems[j].val; + (*pVal)[i] = pHisto->elems[j].val; } double start = (double)pHisto->elems[j].num; double range = pHisto->elems[j + 1].num - start; if (range == 0) { - pVal[i] = (pHisto->elems[j + 1].val - pHisto->elems[j].val) * delta / start + pHisto->elems[j].val; + (*pVal)[i] = (pHisto->elems[j + 1].val - pHisto->elems[j].val) * delta / start + pHisto->elems[j].val; } else { double factor = (-2 * start + sqrt(4 * start * start - 4 * range * (-2 * delta))) / (2 * range); - pVal[i] = pHisto->elems[j].val + (pHisto->elems[j + 1].val - pHisto->elems[j].val) * factor; + (*pVal)[i] = pHisto->elems[j].val + (pHisto->elems[j + 1].val - pHisto->elems[j].val) * factor; } } #else double* pVal = taosMemoryMalloc(num * sizeof(double)); - + if (NULL == *pVal) { + return TSDB_CODE_OUT_OF_MEMORY; + } for (int32_t i = 0; i < num; ++i) { double numOfElem = ratio[i] * pHisto->numOfElems; tSkipListNode* pFirst = pHisto->pList->pHead.pForward[0]; SHistBin* pEntry = (SHistBin*)pFirst->pData; if (numOfElem == 0) { - pVal[i] = pHisto->min; + (*pVal)[i] = pHisto->min; printf("i/numofSlot: %f, v:%f, %f\n", ratio[i], numOfElem, pVal[i]); continue; } else if (numOfElem <= pEntry->num) { - pVal[i] = pEntry->val; + (*pVal)[i] = pEntry->val; printf("i/numofSlot: %f, v:%f, %f\n", ratio[i], numOfElem, pVal[i]); continue; } else if (numOfElem == pHisto->numOfElems) { - pVal[i] = pHisto->max; + (*pVal)[i] = pHisto->max; printf("i/numofSlot: %f, v:%f, %f\n", ratio[i], numOfElem, pVal[i]); continue; } @@ -540,34 +553,40 @@ double* tHistogramUniform(SHistogramInfo* pHisto, double* ratio, int32_t num) { if (fabs(delta) < FLT_EPSILON) { // printf("i/numofSlot: %f, v:%f, %f\n", // (double)i/numOfSlots, numOfElem, pHisto->elems[j].val); - pVal[i] = pPrev->val; + (*pVal)[i] = pPrev->val; } double start = pPrev->num; double range = pEntry->num - start; if (range == 0) { - pVal[i] = (pEntry->val - pPrev->val) * delta / start + pPrev->val; + (*pVal)[i] = (pEntry->val - pPrev->val) * delta / start + pPrev->val; } else { double factor = (-2 * start + sqrt(4 * start * start - 4 * range * (-2 * delta))) / (2 * range); - pVal[i] = pPrev->val + (pEntry->val - pPrev->val) * factor; + (*pVal)[i] = pPrev->val + (pEntry->val - pPrev->val) * factor; } // printf("i/numofSlot: %f, v:%f, %f\n", (double)i/numOfSlots, // numOfElem, val); } #endif - return pVal; + return TSDB_CODE_SUCCESS; } -SHistogramInfo* tHistogramMerge(SHistogramInfo* pHisto1, SHistogramInfo* pHisto2, int32_t numOfEntries) { - SHistogramInfo* pResHistogram = tHistogramCreate(numOfEntries); +int32_t tHistogramMerge(SHistogramInfo* pHisto1, SHistogramInfo* pHisto2, int32_t numOfEntries, SHistogramInfo** pResHistogram) { + int32_t code = tHistogramCreate(numOfEntries, pResHistogram); + if (TSDB_CODE_SUCCESS != code) { + return code; + } // error in histogram info if (pHisto1->numOfEntries > MAX_HISTOGRAM_BIN || pHisto2->numOfEntries > MAX_HISTOGRAM_BIN) { - return pResHistogram; + return code; } SHistBin* pHistoBins = taosMemoryCalloc(1, sizeof(SHistBin) * (pHisto1->numOfEntries + pHisto2->numOfEntries)); + if (NULL == pHistoBins) { + return TSDB_CODE_OUT_OF_MEMORY; + } int32_t i = 0, j = 0, k = 0; while (i < pHisto1->numOfEntries && j < pHisto2->numOfEntries) { @@ -583,28 +602,28 @@ SHistogramInfo* tHistogramMerge(SHistogramInfo* pHisto1, SHistogramInfo* pHisto2 if (i < pHisto1->numOfEntries) { int32_t remain = pHisto1->numOfEntries - i; - memcpy(&pHistoBins[k], &pHisto1->elems[i], sizeof(SHistBin) * remain); + (void)memcpy(&pHistoBins[k], &pHisto1->elems[i], sizeof(SHistBin) * remain); k += remain; } if (j < pHisto2->numOfEntries) { int32_t remain = pHisto2->numOfEntries - j; - memcpy(&pHistoBins[k], &pHisto2->elems[j], sizeof(SHistBin) * remain); + (void)memcpy(&pHistoBins[k], &pHisto2->elems[j], sizeof(SHistBin) * remain); k += remain; } /* update other information */ - pResHistogram->numOfElems = pHisto1->numOfElems + pHisto2->numOfElems; - pResHistogram->min = (pHisto1->min < pHisto2->min) ? pHisto1->min : pHisto2->min; - pResHistogram->max = (pHisto1->max > pHisto2->max) ? pHisto1->max : pHisto2->max; + (*pResHistogram)->numOfElems = pHisto1->numOfElems + pHisto2->numOfElems; + (*pResHistogram)->min = (pHisto1->min < pHisto2->min) ? pHisto1->min : pHisto2->min; + (*pResHistogram)->max = (pHisto1->max > pHisto2->max) ? pHisto1->max : pHisto2->max; while (k > numOfEntries) { histogramMergeImpl(pHistoBins, &k); } - pResHistogram->numOfEntries = k; - memcpy(pResHistogram->elems, pHistoBins, sizeof(SHistBin) * k); + (*pResHistogram)->numOfEntries = k; + (void)memcpy((*pResHistogram)->elems, pHistoBins, sizeof(SHistBin) * k); taosMemoryFree(pHistoBins); - return pResHistogram; + return TSDB_CODE_SUCCESS; } diff --git a/source/libs/function/src/tpercentile.c b/source/libs/function/src/tpercentile.c index a068186992..40e0407a54 100644 --- a/source/libs/function/src/tpercentile.c +++ b/source/libs/function/src/tpercentile.c @@ -28,9 +28,12 @@ int32_t getGroupId(int32_t numOfSlots, int32_t slotIndex, int32_t times) { return (times * numOfSlots) + slotIndex; } -static SFilePage *loadDataFromFilePage(tMemBucket *pMemBucket, int32_t slotIdx) { - SFilePage *buffer = +static int32_t loadDataFromFilePage(tMemBucket *pMemBucket, int32_t slotIdx, SFilePage ** buffer) { + *buffer = (SFilePage *)taosMemoryCalloc(1, pMemBucket->bytes * pMemBucket->pSlots[slotIdx].info.size + sizeof(SFilePage)); + if (NULL == *buffer) { + return TSDB_CODE_OUT_OF_MEMORY; + } int32_t groupId = getGroupId(pMemBucket->numOfSlots, slotIdx, pMemBucket->times); @@ -39,26 +42,30 @@ static SFilePage *loadDataFromFilePage(tMemBucket *pMemBucket, int32_t slotIdx) if (p != NULL) { pIdList = *(SArray **)p; } else { - taosMemoryFree(buffer); - return NULL; + taosMemoryFree(*buffer); + return TSDB_CODE_OUT_OF_MEMORY; } int32_t offset = 0; for (int32_t i = 0; i < taosArrayGetSize(pIdList); ++i) { int32_t *pageId = taosArrayGet(pIdList, i); + if (pageId == NULL) { + taosMemoryFree(*buffer); + return TSDB_CODE_OUT_OF_RANGE; + } SFilePage *pg = getBufPage(pMemBucket->pBuffer, *pageId); if (pg == NULL) { - taosMemoryFree(buffer); - return NULL; + taosMemoryFree(*buffer); + return terrno; } - memcpy(buffer->data + offset, pg->data, (size_t)(pg->num * pMemBucket->bytes)); + (void)memcpy((*buffer)->data + offset, pg->data, (size_t)(pg->num * pMemBucket->bytes)); offset += (int32_t)(pg->num * pMemBucket->bytes); } - taosSort(buffer->data, pMemBucket->pSlots[slotIdx].info.size, pMemBucket->bytes, pMemBucket->comparFn); - return buffer; + taosSort((*buffer)->data, pMemBucket->pSlots[slotIdx].info.size, pMemBucket->bytes, pMemBucket->comparFn); + return TSDB_CODE_SUCCESS; } static void resetBoundingBox(MinMaxEntry *range, int32_t type) { @@ -116,6 +123,9 @@ int32_t findOnlyResult(tMemBucket *pMemBucket, double *result) { ASSERT(list->size == 1); int32_t *pageId = taosArrayGet(list, 0); + if (NULL == pageId) { + return TSDB_CODE_OUT_OF_RANGE; + } SFilePage *pPage = getBufPage(pMemBucket->pBuffer, *pageId); if (pPage == NULL) { return terrno; @@ -238,67 +248,71 @@ static void resetSlotInfo(tMemBucket *pBucket) { } } -tMemBucket *tMemBucketCreate(int32_t nElemSize, int16_t dataType, double minval, double maxval, bool hasWindowOrGroup) { - tMemBucket *pBucket = (tMemBucket *)taosMemoryCalloc(1, sizeof(tMemBucket)); - if (pBucket == NULL) { - return NULL; +int32_t tMemBucketCreate(int32_t nElemSize, int16_t dataType, double minval, double maxval, bool hasWindowOrGroup, + tMemBucket **pBucket) { + *pBucket = (tMemBucket *)taosMemoryCalloc(1, sizeof(tMemBucket)); + if (*pBucket == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; } if (hasWindowOrGroup) { // With window or group by, we need to shrink page size and reduce page num to save memory. - pBucket->numOfSlots = DEFAULT_NUM_OF_SLOT / 8 ; // 128 bucket - pBucket->bufPageSize = 4096; // 4k per page + (*pBucket)->numOfSlots = DEFAULT_NUM_OF_SLOT / 8 ; // 128 bucket + (*pBucket)->bufPageSize = 4096; // 4k per page } else { - pBucket->numOfSlots = DEFAULT_NUM_OF_SLOT; - pBucket->bufPageSize = 16384 * 4; // 16k per page + (*pBucket)->numOfSlots = DEFAULT_NUM_OF_SLOT; + (*pBucket)->bufPageSize = 16384 * 4; // 16k per page } - pBucket->type = dataType; - pBucket->bytes = nElemSize; - pBucket->total = 0; - pBucket->times = 1; + (*pBucket)->type = dataType; + (*pBucket)->bytes = nElemSize; + (*pBucket)->total = 0; + (*pBucket)->times = 1; - pBucket->maxCapacity = 200000; - pBucket->groupPagesMap = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK); - if (setBoundingBox(&pBucket->range, pBucket->type, minval, maxval) != 0) { + (*pBucket)->maxCapacity = 200000; + (*pBucket)->groupPagesMap = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK); + if ((*pBucket)->groupPagesMap == NULL) { + tMemBucketDestroy(*pBucket); + return terrno; + } + if (setBoundingBox(&(*pBucket)->range, (*pBucket)->type, minval, maxval) != 0) { // qError("MemBucket:%p, invalid value range: %f-%f", pBucket, minval, maxval); - taosMemoryFree(pBucket); - return NULL; + tMemBucketDestroy(*pBucket); + return TSDB_CODE_FUNC_INVALID_VALUE_RANGE; } - pBucket->elemPerPage = (pBucket->bufPageSize - sizeof(SFilePage)) / pBucket->bytes; - pBucket->comparFn = getKeyComparFunc(pBucket->type, TSDB_ORDER_ASC); + (*pBucket)->elemPerPage = ((*pBucket)->bufPageSize - sizeof(SFilePage)) / (*pBucket)->bytes; + (*pBucket)->comparFn = getKeyComparFunc((*pBucket)->type, TSDB_ORDER_ASC); - pBucket->hashFunc = getHashFunc(pBucket->type); - if (pBucket->hashFunc == NULL) { + (*pBucket)->hashFunc = getHashFunc((*pBucket)->type); + if ((*pBucket)->hashFunc == NULL) { // qError("MemBucket:%p, not support data type %d, failed", pBucket, pBucket->type); - taosMemoryFree(pBucket); - return NULL; + tMemBucketDestroy(*pBucket); + return TSDB_CODE_FUNC_FUNTION_PARA_TYPE; } - pBucket->pSlots = (tMemBucketSlot *)taosMemoryCalloc(pBucket->numOfSlots, sizeof(tMemBucketSlot)); - if (pBucket->pSlots == NULL) { - taosMemoryFree(pBucket); - return NULL; + (*pBucket)->pSlots = (tMemBucketSlot *)taosMemoryCalloc((*pBucket)->numOfSlots, sizeof(tMemBucketSlot)); + if ((*pBucket)->pSlots == NULL) { + tMemBucketDestroy(*pBucket); + return TSDB_CODE_OUT_OF_MEMORY; } - resetSlotInfo(pBucket); + resetSlotInfo((*pBucket)); if (!osTempSpaceAvailable()) { - terrno = TSDB_CODE_NO_DISKSPACE; // qError("MemBucket create disk based Buf failed since %s", terrstr(terrno)); - tMemBucketDestroy(pBucket); - return NULL; + tMemBucketDestroy(*pBucket); + return TSDB_CODE_NO_DISKSPACE; } - int32_t ret = createDiskbasedBuf(&pBucket->pBuffer, pBucket->bufPageSize, pBucket->bufPageSize * 1024, "1", tsTempDir); + int32_t ret = createDiskbasedBuf(&(*pBucket)->pBuffer, (*pBucket)->bufPageSize, (*pBucket)->bufPageSize * 1024, "1", tsTempDir); if (ret != 0) { - tMemBucketDestroy(pBucket); - return NULL; + tMemBucketDestroy(*pBucket); + return ret; } // qDebug("MemBucket:%p, elem size:%d", pBucket, pBucket->bytes); - return pBucket; + return TSDB_CODE_SUCCESS; } void tMemBucketDestroy(tMemBucket *pBucket) { @@ -394,7 +408,14 @@ int32_t tMemBucketPut(tMemBucket *pBucket, const void *data, size_t size) { void *p = taosHashGet(pBucket->groupPagesMap, &groupId, sizeof(groupId)); if (p == NULL) { pPageIdList = taosArrayInit(4, sizeof(int32_t)); - taosHashPut(pBucket->groupPagesMap, &groupId, sizeof(groupId), &pPageIdList, POINTER_BYTES); + if (NULL == pPageIdList) { + return terrno; + } + int32_t code = taosHashPut(pBucket->groupPagesMap, &groupId, sizeof(groupId), &pPageIdList, POINTER_BYTES); + if (TSDB_CODE_SUCCESS != code) { + taosArrayDestroy(pPageIdList); + return code; + } } else { pPageIdList = *(SArray **)p; } @@ -404,10 +425,13 @@ int32_t tMemBucketPut(tMemBucket *pBucket, const void *data, size_t size) { return terrno; } pSlot->info.pageId = pageId; - taosArrayPush(pPageIdList, &pageId); + if (taosArrayPush(pPageIdList, &pageId) == NULL) { + taosArrayDestroy(pPageIdList); + return TSDB_CODE_OUT_OF_MEMORY; + } } - memcpy(pSlot->info.data->data + pSlot->info.data->num * pBucket->bytes, d, pBucket->bytes); + (void)memcpy(pSlot->info.data->data + pSlot->info.data->num * pBucket->bytes, d, pBucket->bytes); pSlot->info.data->num += 1; pSlot->info.size += 1; @@ -493,9 +517,10 @@ int32_t getPercentileImpl(tMemBucket *pMemBucket, int32_t count, double fraction if (pSlot->info.size <= pMemBucket->maxCapacity) { // data in buffer and file are merged together to be processed. - SFilePage *buffer = loadDataFromFilePage(pMemBucket, i); - if (buffer == NULL) { - return terrno; + SFilePage *buffer = NULL; + int32_t code = loadDataFromFilePage(pMemBucket, i, &buffer); + if (TSDB_CODE_SUCCESS != code) { + return code; } int32_t currentIdx = count - num; @@ -541,6 +566,9 @@ int32_t getPercentileImpl(tMemBucket *pMemBucket, int32_t count, double fraction for (int32_t f = 0; f < list->size; ++f) { int32_t *pageId = taosArrayGet(list, f); + if (NULL == pageId) { + return TSDB_CODE_OUT_OF_RANGE; + } SFilePage *pg = getBufPage(pMemBucket->pBuffer, *pageId); if (pg == NULL) { return terrno; diff --git a/source/libs/function/src/tudf.c b/source/libs/function/src/tudf.c index 3c5e4014b3..c9b8a1e08b 100644 --- a/source/libs/function/src/tudf.c +++ b/source/libs/function/src/tudf.c @@ -1011,7 +1011,7 @@ void releaseUdfFuncHandle(char *udfName, UdfcFuncHandle handle); int32_t cleanUpUdfs(); bool udfAggGetEnv(struct SFunctionNode *pFunc, SFuncExecEnv *pEnv); -bool udfAggInit(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo *pResultCellInfo); +int32_t udfAggInit(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo *pResultCellInfo); int32_t udfAggProcess(struct SqlFunctionCtx *pCtx); int32_t udfAggFinalize(struct SqlFunctionCtx *pCtx, SSDataBlock *pBlock); @@ -1196,15 +1196,18 @@ bool udfAggGetEnv(struct SFunctionNode *pFunc, SFuncExecEnv *pEnv) { return true; } -bool udfAggInit(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo *pResultCellInfo) { - if (functionSetup(pCtx, pResultCellInfo) != true) { - return false; +int32_t udfAggInit(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo *pResultCellInfo) { + if (pResultCellInfo->initialized) { + return TSDB_CODE_SUCCESS; + } + if (functionSetup(pCtx, pResultCellInfo) != TSDB_CODE_SUCCESS) { + return TSDB_CODE_FUNC_SETUP_ERROR; } UdfcFuncHandle handle; int32_t udfCode = 0; if ((udfCode = acquireUdfFuncHandle((char *)pCtx->udfName, &handle)) != 0) { fnError("udfAggInit error. step doSetupUdf. udf code: %d", udfCode); - return false; + return TSDB_CODE_FUNC_SETUP_ERROR; } SUdfcUvSession *session = (SUdfcUvSession *)handle; SUdfAggRes *udfRes = (SUdfAggRes *)GET_ROWCELL_INTERBUF(pResultCellInfo); @@ -1218,7 +1221,7 @@ bool udfAggInit(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo *pResult if ((udfCode = doCallUdfAggInit(handle, &buf)) != 0) { fnError("udfAggInit error. step doCallUdfAggInit. udf code: %d", udfCode); releaseUdfFuncHandle(pCtx->udfName, handle); - return false; + return TSDB_CODE_FUNC_SETUP_ERROR; } if (buf.bufLen <= session->bufSize) { memcpy(udfRes->interResBuf, buf.buf, buf.bufLen); @@ -1227,11 +1230,11 @@ bool udfAggInit(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo *pResult } else { fnError("udfc inter buf size %d is greater than function bufSize %d", buf.bufLen, session->bufSize); releaseUdfFuncHandle(pCtx->udfName, handle); - return false; + return TSDB_CODE_FUNC_SETUP_ERROR; } releaseUdfFuncHandle(pCtx->udfName, handle); freeUdfInterBuf(&buf); - return true; + return TSDB_CODE_SUCCESS; } int32_t udfAggProcess(struct SqlFunctionCtx *pCtx) { diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 844a608456..e67001889e 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -10420,8 +10420,9 @@ static int32_t createLastTsSelectStmt(char* pDb, const char* pTable, const char* return code; } - SNode* pFunc = (SNode*)createFunction("last", pParameterList); - if (NULL == pFunc) { + SNode* pFunc = NULL; + code = createFunction("last", pParameterList, (SFunctionNode**)&pFunc); + if (code) { nodesDestroyList(pParameterList); return TSDB_CODE_OUT_OF_MEMORY; } @@ -10438,8 +10439,9 @@ static int32_t createLastTsSelectStmt(char* pDb, const char* pTable, const char* return code; } - SFunctionNode* pFunc1 = createFunction("_vgid", NULL); - if (NULL == pFunc1) { + SFunctionNode* pFunc1 = NULL; + code = createFunction("_vgid", NULL, &pFunc1); + if (code) { nodesDestroyList(pProjectionList); return TSDB_CODE_OUT_OF_MEMORY; } @@ -10451,8 +10453,9 @@ static int32_t createLastTsSelectStmt(char* pDb, const char* pTable, const char* return code; } - SFunctionNode* pFunc2 = createFunction("_vgver", NULL); - if (NULL == pFunc2) { + SFunctionNode* pFunc2 = NULL; + code = createFunction("_vgver", NULL, &pFunc2); + if (code) { nodesDestroyList(pProjectionList); return TSDB_CODE_OUT_OF_MEMORY; } diff --git a/source/libs/parser/test/parTestMain.cpp b/source/libs/parser/test/parTestMain.cpp index 8d13d7cf0e..c8925b3df7 100644 --- a/source/libs/parser/test/parTestMain.cpp +++ b/source/libs/parser/test/parTestMain.cpp @@ -35,7 +35,8 @@ namespace ParserTest { class ParserEnv : public testing::Environment { public: virtual void SetUp() { - fmFuncMgtInit(); + // TODO(smj) : How to handle return value of fmFuncMgtInit + (void)fmFuncMgtInit(); initMetaDataEnv(); generateMetaData(); initLog(TD_TMP_DIR_PATH "td"); diff --git a/source/libs/planner/src/planOptimizer.c b/source/libs/planner/src/planOptimizer.c index b1db171875..b72cad012d 100644 --- a/source/libs/planner/src/planOptimizer.c +++ b/source/libs/planner/src/planOptimizer.c @@ -5336,9 +5336,13 @@ static bool stbJoinOptShouldBeOptimized(SLogicNode* pNode) { } int32_t stbJoinOptAddFuncToScanNode(char* funcName, SScanLogicNode* pScan) { - SFunctionNode* pUidFunc = createFunction(funcName, NULL); + SFunctionNode* pUidFunc = NULL; + int32_t code = createFunction(funcName, NULL, &pUidFunc); + if (TSDB_CODE_SUCCESS != code) { + return code; + } snprintf(pUidFunc->node.aliasName, sizeof(pUidFunc->node.aliasName), "%s.%p", pUidFunc->functionName, pUidFunc); - int32_t code = nodesListStrictAppend(pScan->pScanPseudoCols, (SNode*)pUidFunc); + code = nodesListStrictAppend(pScan->pScanPseudoCols, (SNode*)pUidFunc); if (TSDB_CODE_SUCCESS == code) { code = createColumnByRewriteExpr((SNode*)pUidFunc, &pScan->node.pTargets); } diff --git a/source/libs/planner/test/planTestMain.cpp b/source/libs/planner/test/planTestMain.cpp index 4e013c44b8..e05fa27c1e 100644 --- a/source/libs/planner/test/planTestMain.cpp +++ b/source/libs/planner/test/planTestMain.cpp @@ -27,7 +27,8 @@ class PlannerEnv : public testing::Environment { public: virtual void SetUp() { - fmFuncMgtInit(); + // TODO(smj) : How to handle return value of fmFuncMgtInit + (void)fmFuncMgtInit(); initMetaDataEnv(); generateMetaData(); initLog(TD_TMP_DIR_PATH "td"); diff --git a/source/libs/scalar/src/filter.c b/source/libs/scalar/src/filter.c index 105243608e..19b4c521a4 100644 --- a/source/libs/scalar/src/filter.c +++ b/source/libs/scalar/src/filter.c @@ -997,9 +997,15 @@ int32_t filterDetachCnfGroups(SArray *group, SArray *left, SArray *right) { for (int32_t l = 0; l < leftSize; ++l) { SFilterGroup *gp1 = taosArrayGet(left, l); + if (NULL == gp1) { + FLT_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } for (int32_t r = 0; r < rightSize; ++r) { SFilterGroup *gp2 = taosArrayGet(right, r); + if (NULL == gp2) { + FLT_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } FLT_ERR_RET(filterDetachCnfGroup(gp1, gp2, group)); } @@ -1212,7 +1218,7 @@ int32_t filterAddUnitImpl(SFilterInfo *info, uint8_t optr, SFilterFieldId *left, FLT_PACKAGE_UNIT_HASH_KEY(&v, optr, optr2, left->idx, (right ? right->idx : -1), (right2 ? right2->idx : -1)); if (taosHashPut(info->pctx.unitHash, v, sizeof(v), uidx, sizeof(*uidx))) { fltError("taosHashPut to set failed"); - FLT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); + FLT_ERR_RET(terrno); } } @@ -2157,7 +2163,7 @@ int32_t fltInitValFieldData(SFilterInfo *info) { int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(fi->data), varDataLen(fi->data), varDataVal(newValData)); if (len < 0) { qError("filterInitValFieldData taosUcs4ToMbs error 1"); - return TSDB_CODE_APP_ERROR; + return TSDB_CODE_SCALAR_CONVERT_ERROR; } varDataSetLen(newValData, len); (void)varDataCopy(fi->data, newValData); @@ -2307,6 +2313,9 @@ int32_t filterMergeUnits(SFilterInfo *info, SFilterGroupCtx *gRes, uint32_t colI for (uint32_t i = 0; i < size; ++i) { SFilterUnit *u = taosArrayGetP(colArray, i); + if (NULL == u) { + FLT_ERR_JRET(TSDB_CODE_OUT_OF_RANGE); + } uint8_t optr = FILTER_UNIT_OPTR(u); FLT_ERR_RET(filterAddRangeOptr(ctx, optr, LOGIC_COND_TYPE_AND, empty, NULL)); @@ -2637,6 +2646,9 @@ int32_t filterMergeTwoGroups(SFilterInfo *info, SFilterGroupCtx **gRes1, SFilter for (int32_t i = 0; i < ctxSize; ++i) { pctx = taosArrayGet(colCtxs, i); + if (NULL == pctx) { + FLT_ERR_JRET(TSDB_CODE_OUT_OF_RANGE); + } colInfo = &(*gRes1)->colInfo[pctx->colIdx]; filterFreeColInfo(colInfo); @@ -2763,6 +2775,9 @@ int32_t filterConvertGroupFromArray(SFilterInfo *info, SArray *group) { for (size_t i = 0; i < groupSize; ++i) { SFilterGroup *pg = taosArrayGet(group, i); + if (NULL == pg) { + FLT_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } pg->unitFlags = taosMemoryCalloc(pg->unitNum, sizeof(*pg->unitFlags)); if (pg->unitFlags == NULL) { FLT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); @@ -2819,7 +2834,9 @@ int32_t filterRewrite(SFilterInfo *info, SFilterGroupCtx **gRes, int32_t gResNum for (int32_t n = 0; n < usize; ++n) { SFilterUnit *u = (SFilterUnit *)taosArrayGetP((SArray *)colInfo->info, n); - + if (NULL == u) { + FLT_ERR_JRET(TSDB_CODE_OUT_OF_RANGE); + } FLT_ERR_JRET(filterAddUnitFromUnit(info, &oinfo, u, &uidx)); FLT_ERR_JRET(filterAddUnitToGroup(&ng, uidx)); } @@ -3425,7 +3442,7 @@ int32_t filterExecuteImplMisc(void *pinfo, int32_t numOfRows, SColumnInfoData *p if (len < 0) { qError("castConvert1 taosUcs4ToMbs error"); taosMemoryFreeClear(newColData); - FLT_ERR_RET(TSDB_CODE_APP_ERROR); + FLT_ERR_RET(TSDB_CODE_SCALAR_CONVERT_ERROR); } else { varDataSetLen(newColData, len); p[i] = filterDoCompare(gDataCompare[info->cunits[uidx].func], info->cunits[uidx].optr, newColData, @@ -3500,7 +3517,7 @@ int32_t filterExecuteImpl(void *pinfo, int32_t numOfRows, SColumnInfoData *pRes, if (len < 0) { qError("castConvert1 taosUcs4ToMbs error"); taosMemoryFreeClear(newColData); - FLT_ERR_RET(TSDB_CODE_APP_ERROR); + FLT_ERR_RET(TSDB_CODE_SCALAR_CONVERT_ERROR); } else { varDataSetLen(newColData, len); p[i] = filterDoCompare(gDataCompare[cunit->func], cunit->optr, newColData, cunit->valData); @@ -3814,6 +3831,9 @@ int32_t fltSclMergeSort(SArray *pts1, SArray *pts2, SArray *result) { while (i < len1 && j < len2) { SFltSclPoint *pt1 = taosArrayGet(pts1, i); SFltSclPoint *pt2 = taosArrayGet(pts2, j); + if (NULL == pt1 || NULL == pt2) { + FLT_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } bool less = fltSclLessPoint(pt1, pt2); if (less) { if (NULL == taosArrayPush(result, pt1)) { @@ -3830,6 +3850,9 @@ int32_t fltSclMergeSort(SArray *pts1, SArray *pts2, SArray *result) { if (i < len1) { for (; i < len1; ++i) { SFltSclPoint *pt1 = taosArrayGet(pts1, i); + if (NULL == pt1) { + FLT_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } if (NULL == taosArrayPush(result, pt1)) { FLT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); } @@ -3838,6 +3861,9 @@ int32_t fltSclMergeSort(SArray *pts1, SArray *pts2, SArray *result) { if (j < len2) { for (; j < len2; ++j) { SFltSclPoint *pt2 = taosArrayGet(pts2, j); + if (NULL == pt2) { + FLT_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } if (NULL == taosArrayPush(result, pt2)) { FLT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); } @@ -3859,6 +3885,9 @@ int32_t fltSclMerge(SArray *pts1, SArray *pts2, bool isUnion, SArray *merged) { int32_t count = 0; for (int32_t i = 0; i < taosArrayGetSize(all); ++i) { SFltSclPoint *pt = taosArrayGet(all, i); + if (NULL == pt) { + FLT_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } if (pt->start) { ++count; if (count == countRequired) { @@ -3893,6 +3922,9 @@ typedef struct { int32_t fltSclGetOrCreateColumnRange(SColumnNode *colNode, SArray *colRangeList, SFltSclColumnRange **colRange) { for (int32_t i = 0; i < taosArrayGetSize(colRangeList); ++i) { *colRange = taosArrayGet(colRangeList, i); + if (NULL == colRange) { + return TSDB_CODE_OUT_OF_RANGE; + } if (nodesEqualNode((SNode *)(*colRange)->colNode, (SNode *)colNode)) { return TSDB_CODE_SUCCESS; } @@ -4044,6 +4076,9 @@ int32_t filterRangeExecute(SFilterInfo *info, SColumnDataAgg *pDataStatis, int32 SArray *colRanges = info->sclCtx.fltSclRange; for (int32_t i = 0; i < taosArrayGetSize(colRanges); ++i) { SFltSclColumnRange *colRange = taosArrayGet(colRanges, i); + if (NULL == colRange) { + FLT_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } bool foundCol = false; int32_t j = 0; for (; j < numOfCols; ++j) { @@ -4332,11 +4367,17 @@ int32_t filterGetTimeRange(SNode *pNode, STimeWindow *win, bool *isStrict) { SArray *colRanges = info->sclCtx.fltSclRange; if (taosArrayGetSize(colRanges) == 1) { SFltSclColumnRange *colRange = taosArrayGet(colRanges, 0); + if (NULL == colRange) { + FLT_ERR_JRET(TSDB_CODE_OUT_OF_RANGE); + } SArray *points = colRange->points; if (taosArrayGetSize(points) == 2) { *win = TSWINDOW_DESC_INITIALIZER; SFltSclPoint *startPt = taosArrayGet(points, 0); SFltSclPoint *endPt = taosArrayGet(points, 1); + if (NULL == startPt || NULL == endPt) { + FLT_ERR_JRET(TSDB_CODE_OUT_OF_RANGE); + } SFltSclDatum start; SFltSclDatum end; FLT_ERR_JRET(fltSclGetTimeStampDatum(startPt, &start)); @@ -4398,7 +4439,7 @@ int32_t filterConverNcharColumns(SFilterInfo *info, int32_t rows, bool *gotNchar bool ret = taosMbsToUcs4(varDataVal(src), varDataLen(src), (TdUcs4 *)varDataVal(dst), bufSize, &len); if (!ret) { qError("filterConverNcharColumns taosMbsToUcs4 error"); - return TSDB_CODE_FAILED; + return TSDB_CODE_SCALAR_CONVERT_ERROR; } varDataLen(dst) = len; } @@ -4432,7 +4473,7 @@ int32_t fltAddValueNodeToConverList(SFltTreeStat *stat, SValueNode *pNode) { if (NULL == stat->nodeList) { stat->nodeList = taosArrayInit(10, POINTER_BYTES); if (NULL == stat->nodeList) { - FLT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); + FLT_ERR_RET(terrno); } } @@ -4816,6 +4857,9 @@ int32_t fltSclProcessCNF(SArray *sclOpListCNF, SArray *colRangeList) { size_t sz = taosArrayGetSize(sclOpListCNF); for (int32_t i = 0; i < sz; ++i) { SFltSclOperator *sclOper = taosArrayGet(sclOpListCNF, i); + if (NULL == sclOper) { + FLT_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } SFltSclColumnRange *colRange = NULL; FLT_ERR_RET(fltSclGetOrCreateColumnRange(sclOper->colNode, colRangeList, &colRange)); SArray *points = taosArrayInit(4, sizeof(SFltSclPoint)); @@ -4920,22 +4964,27 @@ static int32_t fltSclCollectOperators(SNode *pNode, SArray *sclOpList) { int32_t fltOptimizeNodes(SFilterInfo *pInfo, SNode **pNode, SFltTreeStat *pStat) { SArray *sclOpList = taosArrayInit(16, sizeof(SFltSclOperator)); + int32_t code = TSDB_CODE_SUCCESS; if (NULL == sclOpList) { FLT_ERR_RET(terrno); } - FLT_ERR_RET(fltSclCollectOperators(*pNode, sclOpList)); + FLT_ERR_JRET(fltSclCollectOperators(*pNode, sclOpList)); SArray *colRangeList = taosArrayInit(16, sizeof(SFltSclColumnRange)); if (NULL == colRangeList) { FLT_ERR_RET(terrno); } - FLT_ERR_RET(fltSclProcessCNF(sclOpList, colRangeList)); + FLT_ERR_JRET(fltSclProcessCNF(sclOpList, colRangeList)); pInfo->sclCtx.fltSclRange = colRangeList; for (int32_t i = 0; i < taosArrayGetSize(sclOpList); ++i) { SFltSclOperator *sclOp = taosArrayGet(sclOpList, i); + if (NULL == sclOp) { + FLT_ERR_JRET(TSDB_CODE_OUT_OF_RANGE); + } nodesDestroyNode((SNode *)sclOp->colNode); nodesDestroyNode((SNode *)sclOp->valNode); } +_return: taosArrayDestroy(sclOpList); return TSDB_CODE_SUCCESS; } @@ -4946,6 +4995,9 @@ int32_t fltGetDataFromColId(void *param, int32_t id, void **data) { for (int32_t j = 0; j < numOfCols; ++j) { SColumnInfoData *pColInfo = taosArrayGet(pDataBlock, j); + if (NULL == pColInfo) { + FLT_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } if (id == pColInfo->info.colId) { *data = pColInfo; break; @@ -4965,6 +5017,9 @@ int32_t fltGetDataFromSlotId(void *param, int32_t id, void **data) { } SColumnInfoData *pColInfo = taosArrayGet(pDataBlock, id); + if (NULL == pColInfo) { + return TSDB_CODE_OUT_OF_RANGE; + } *data = pColInfo; return TSDB_CODE_SUCCESS; diff --git a/source/libs/scalar/src/scalar.c b/source/libs/scalar/src/scalar.c index 346bbb2811..040e090661 100644 --- a/source/libs/scalar/src/scalar.c +++ b/source/libs/scalar/src/scalar.c @@ -90,6 +90,9 @@ int32_t sclConvertValueToSclParam(SValueNode *pValueNode, SScalarParam *out, int int32_t sclExtendResRows(SScalarParam *pDst, SScalarParam *pSrc, SArray *pBlockList) { SSDataBlock *pb = taosArrayGetP(pBlockList, 0); + if (NULL == pb) { + SCL_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } SScalarParam *pLeft = taosMemoryCalloc(1, sizeof(SScalarParam)); int32_t code = TSDB_CODE_SUCCESS; if (NULL == pLeft) { @@ -116,7 +119,7 @@ int32_t scalarGenerateSetFromList(void **data, void *pNode, uint32_t type) { SHashObj *pObj = taosHashInit(256, taosGetDefaultHashFunction(type), true, false); if (NULL == pObj) { sclError("taosHashInit failed, size:%d", 256); - SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); + SCL_ERR_RET(terrno); } taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(type)); @@ -176,7 +179,7 @@ int32_t scalarGenerateSetFromList(void **data, void *pNode, uint32_t type) { if (taosHashPut(pObj, buf, (size_t)len, NULL, 0)) { sclError("taosHashPut to set failed"); - SCL_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY); + SCL_ERR_JRET(terrno); } colInfoDataCleanup(out.columnData, out.numOfRows); @@ -336,6 +339,9 @@ int32_t sclInitParam(SNode *node, SScalarParam *param, SScalarCtx *ctx, int32_t switch (nodeType(node)) { case QUERY_NODE_LEFT_VALUE: { SSDataBlock *pb = taosArrayGetP(ctx->pBlockList, 0); + if (NULL == pb) { + SCL_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } param->numOfRows = pb->info.rows; break; } @@ -347,10 +353,7 @@ int32_t sclInitParam(SNode *node, SScalarParam *param, SScalarCtx *ctx, int32_t SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); } param->numOfRows = 1; - int32_t code = sclCreateColumnInfoData(&valueNode->node.resType, 1, param); - if (code != TSDB_CODE_SUCCESS) { - SCL_RET(TSDB_CODE_OUT_OF_MEMORY); - } + SCL_ERR_RET(sclCreateColumnInfoData(&valueNode->node.resType, 1, param)); if (TSDB_DATA_TYPE_NULL == valueNode->node.resType.type || valueNode->isNull) { colDataSetNULL(param->columnData, 0); } else { @@ -377,7 +380,7 @@ int32_t sclInitParam(SNode *node, SScalarParam *param, SScalarCtx *ctx, int32_t taosHashCleanup(param->pHashFilter); param->pHashFilter = NULL; sclError("taosHashPut nodeList failed, size:%d", (int32_t)sizeof(*param)); - return TSDB_CODE_OUT_OF_MEMORY; + return terrno; } param->colAlloced = false; break; @@ -393,6 +396,9 @@ int32_t sclInitParam(SNode *node, SScalarParam *param, SScalarCtx *ctx, int32_t int32_t index = -1; for (int32_t i = 0; i < taosArrayGetSize(ctx->pBlockList); ++i) { SSDataBlock *pb = taosArrayGetP(ctx->pBlockList, i); + if (NULL == pb) { + SCL_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } if (pb->info.id.blockId == ref->dataBlockId) { index = i; break; @@ -461,6 +467,9 @@ int32_t sclInitParamList(SScalarParam **pParams, SNodeList *pParamList, SScalarC if (NULL == pParamList) { if (ctx->pBlockList) { SSDataBlock *pBlock = taosArrayGetP(ctx->pBlockList, 0); + if (NULL == pBlock) { + SCL_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } *rowNum = pBlock->info.rows; } else { *rowNum = 1; @@ -919,6 +928,9 @@ int32_t sclExecCaseWhen(SCaseWhenNode *node, SScalarCtx *ctx, SScalarParam *outp if (ctx->pBlockList) { SSDataBlock *pb = taosArrayGetP(ctx->pBlockList, 0); + if (NULL == pb) { + SCL_ERR_RET(TSDB_CODE_OUT_OF_RANGE); + } rowNum = pb->info.rows; output->numOfRows = pb->info.rows; } @@ -1532,6 +1544,10 @@ EDealRes sclWalkTarget(SNode *pNode, SScalarCtx *ctx) { int32_t index = -1; for (int32_t i = 0; i < taosArrayGetSize(ctx->pBlockList); ++i) { SSDataBlock *pb = taosArrayGetP(ctx->pBlockList, i); + if (NULL == pb) { + ctx->code = TSDB_CODE_OUT_OF_RANGE; + return DEAL_RES_ERROR; + } if (pb->info.id.blockId == target->dataBlockId) { index = i; break; @@ -1636,7 +1652,7 @@ int32_t sclCalcConstants(SNode *pNode, bool dual, SNode **pRes) { ctx.pRes = taosHashInit(SCL_DEFAULT_OP_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK); if (NULL == ctx.pRes) { sclError("taosHashInit failed, num:%d", SCL_DEFAULT_OP_NUM); - SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); + SCL_ERR_RET(terrno); } nodesRewriteExprPostOrder(&pNode, sclConstantsRewriter, (void *)&ctx); @@ -1770,7 +1786,7 @@ int32_t scalarCalculate(SNode *pNode, SArray *pBlockList, SScalarParam *pDst) { ctx.pRes = taosHashInit(SCL_DEFAULT_OP_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK); if (NULL == ctx.pRes) { sclError("taosHashInit failed, num:%d", SCL_DEFAULT_OP_NUM); - SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); + SCL_ERR_RET(terrno); } nodesWalkExprPostOrder(pNode, sclCalcWalker, (void *)&ctx); @@ -1784,6 +1800,9 @@ int32_t scalarCalculate(SNode *pNode, SArray *pBlockList, SScalarParam *pDst) { } SSDataBlock *pb = taosArrayGetP(pBlockList, 0); + if (NULL == pb) { + SCL_ERR_JRET(TSDB_CODE_OUT_OF_RANGE); + } if (1 == res->numOfRows && pb->info.rows > 0) { SCL_ERR_JRET(sclExtendResRows(pDst, res, pBlockList)); } else { diff --git a/source/libs/scalar/src/sclfunc.c b/source/libs/scalar/src/sclfunc.c index 5baaa57b1d..fd747ce78a 100644 --- a/source/libs/scalar/src/sclfunc.c +++ b/source/libs/scalar/src/sclfunc.c @@ -399,7 +399,7 @@ static int32_t concatCopyHelper(const char *input, char *output, bool hasNchar, bool ret = taosMbsToUcs4(varDataVal(input), len, newBuf, (varDataLen(input) + 1) * TSDB_NCHAR_SIZE, &len); if (!ret) { taosMemoryFree(newBuf); - return TSDB_CODE_FAILED; + return TSDB_CODE_SCALAR_CONVERT_ERROR; } memcpy(varDataVal(output) + *dataLen, newBuf, varDataLen(input) * TSDB_NCHAR_SIZE); *dataLen += varDataLen(input) * TSDB_NCHAR_SIZE; @@ -818,7 +818,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp } else if (inputType == TSDB_DATA_TYPE_NCHAR) { int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf); if (len < 0) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } @@ -837,7 +837,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp } else if (inputType == TSDB_DATA_TYPE_NCHAR) { int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf); if (len < 0) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } convBuf[len] = 0; @@ -855,7 +855,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp } else if (inputType == TSDB_DATA_TYPE_NCHAR) { int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf); if (len < 0) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } @@ -874,7 +874,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp } else if (inputType == TSDB_DATA_TYPE_NCHAR) { int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf); if (len < 0) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } convBuf[len] = 0; @@ -892,7 +892,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp } else if (inputType == TSDB_DATA_TYPE_NCHAR) { int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf); if (len < 0) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } convBuf[len] = 0; @@ -910,7 +910,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp } else if (inputType == TSDB_DATA_TYPE_NCHAR) { int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf); if (len < 0) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } convBuf[len] = 0; @@ -928,7 +928,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp } else if (inputType == TSDB_DATA_TYPE_NCHAR) { int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf); if (len < 0) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } convBuf[len] = 0; @@ -946,7 +946,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp } else if (inputType == TSDB_DATA_TYPE_NCHAR) { int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf); if (len < 0) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } @@ -965,7 +965,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp } else if (inputType == TSDB_DATA_TYPE_NCHAR) { int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf); if (len < 0) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } convBuf[len] = 0; @@ -983,7 +983,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp } else if (inputType == TSDB_DATA_TYPE_NCHAR) { int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf); if (len < 0) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } convBuf[len] = 0; @@ -1001,7 +1001,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp } else if (inputType == TSDB_DATA_TYPE_NCHAR) { int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf); if (len < 0) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } convBuf[len] = 0; @@ -1041,7 +1041,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp } else if (inputType == TSDB_DATA_TYPE_NCHAR) { int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf); if (len < 0) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } len = TMIN(len, outputLen - VARSTR_HEADER_SIZE); @@ -1075,7 +1075,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp len = sprintf(tmp, "%.*s", outputCharLen, *(int8_t *)input ? "true" : "false"); bool ret = taosMbsToUcs4(tmp, len, (TdUcs4 *)varDataVal(output), outputLen - VARSTR_HEADER_SIZE, &len); if (!ret) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } @@ -1085,7 +1085,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp bool ret = taosMbsToUcs4(input + VARSTR_HEADER_SIZE, len, (TdUcs4 *)varDataVal(output), outputLen - VARSTR_HEADER_SIZE, &len); if (!ret) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } varDataSetLen(output, len); @@ -1099,7 +1099,7 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp len = outputCharLen > len ? len : outputCharLen; bool ret = taosMbsToUcs4(buf, len, (TdUcs4 *)varDataVal(output), outputLen - VARSTR_HEADER_SIZE, &len); if (!ret) { - code = TSDB_CODE_FAILED; + code = TSDB_CODE_SCALAR_CONVERT_ERROR; goto _end; } varDataSetLen(output, len); diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index f841ec6618..cbc671a73a 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -105,7 +105,7 @@ int32_t convertNcharToDouble(const void *inData, void *outData) { int len = taosUcs4ToMbs((TdUcs4 *)varDataVal(inData), varDataLen(inData), tmp); if (len < 0) { sclError("castConvert taosUcs4ToMbs error 1"); - SCL_ERR_JRET(TSDB_CODE_FAILED); + SCL_ERR_JRET(TSDB_CODE_SCALAR_CONVERT_ERROR); } tmp[len] = 0; @@ -596,7 +596,7 @@ int32_t ncharTobinary(void *buf, void **out) { // todo need to remove , if tobi sclError("charset:%s to %s. val:%s convert ncharTobinary failed.", DEFAULT_UNICODE_ENCODEC, tsCharset, (char *)varDataVal(buf)); taosMemoryFree(*out); - SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); + SCL_ERR_RET(TSDB_CODE_SCALAR_CONVERT_ERROR); } varDataSetLen(*out, len); SCL_RET(TSDB_CODE_SUCCESS); diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 9650793959..3801ae9ffd 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -707,6 +707,10 @@ TAOS_DEFINE_ERROR(TSDB_CODE_FUNC_TO_TIMESTAMP_FAILED_FORMAT_ERR, "Func to_timest TAOS_DEFINE_ERROR(TSDB_CODE_FUNC_TO_TIMESTAMP_FAILED_TS_ERR, "Func to_timestamp failed for wrong timestamp") TAOS_DEFINE_ERROR(TSDB_CODE_FUNC_TO_TIMESTAMP_FAILED_NOT_SUPPORTED, "Func to_timestamp failed for unsupported timestamp format") TAOS_DEFINE_ERROR(TSDB_CODE_FUNC_TO_CHAR_NOT_SUPPORTED, "Func to_char failed for unsupported format") +TAOS_DEFINE_ERROR(TSDB_CODE_FUNC_TIME_UNIT_INVALID, "Invalid function time unit") +TAOS_DEFINE_ERROR(TSDB_CODE_FUNC_TIME_UNIT_TOO_SMALL, "Function time unit cannot be smaller than db precision") +TAOS_DEFINE_ERROR(TSDB_CODE_FUNC_INVALID_VALUE_RANGE, "Function got invalid value range") +TAOS_DEFINE_ERROR(TSDB_CODE_FUNC_SETUP_ERROR, "Function set up failed") //udf TAOS_DEFINE_ERROR(TSDB_CODE_UDF_STOPPING, "udf is stopping") diff --git a/tests/system-test/8-stream/stream_basic.py b/tests/system-test/8-stream/stream_basic.py index 2067c2395e..95f4f1addc 100644 --- a/tests/system-test/8-stream/stream_basic.py +++ b/tests/system-test/8-stream/stream_basic.py @@ -93,9 +93,8 @@ class TDTestCase: tdSql.execute("create stream stream2 fill_history 1 into stb subtable(concat('new-', tname)) AS SELECT " "_wstart, count(*), avg(i) FROM st PARTITION BY tbname tname INTERVAL(1m)", show=True) - time.sleep(2) - tdSql.query("select * from sta") - tdSql.checkRows(3) + sql= "select * from sta" + tdSql.check_rows_loop(3, sql, loopCount=100, waitTime=0.5) tdSql.query("select tbname from sta order by tbname") if not tdSql.getData(0, 0).startswith('nee_w-t1_sta_'): tdLog.exit("error1") @@ -106,8 +105,8 @@ class TDTestCase: if not tdSql.getData(2, 0).startswith('nee_w-t3_sta_'): tdLog.exit("error3") - tdSql.query("select * from stb") - tdSql.checkRows(3) + sql= "select * from stb" + tdSql.check_rows_loop(3, sql, loopCount=100, waitTime=0.5) tdSql.query("select tbname from stb order by tbname") if not tdSql.getData(0, 0).startswith('new-t1_stb_'): tdLog.exit("error4")