From 5d5aad9ad756046a8a66a781703249e8ee48d60b Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Thu, 23 Feb 2023 17:03:21 +0800 Subject: [PATCH] fix: percentile finalize wrong error code issue --- source/libs/function/src/builtinsimpl.c | 71 ++++++++++++------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 8e52ae5f30..5f8c35a841 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -1689,53 +1689,50 @@ int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { double v = 0; tMemBucket* pMemBucket = ppInfo->pMemBucket; - if (pMemBucket == NULL || pMemBucket->total == 0) { // check for null - code = TSDB_CODE_FAILED; - goto _fin_error; - } + if (pMemBucket != NULL && pMemBucket->total > 0) { // check for null + if (pCtx->numOfParams > 2) { + char buf[512] = {0}; + size_t len = 1; - if (pCtx->numOfParams > 2) { - char buf[512] = {0}; - size_t len = 1; + varDataVal(buf)[0] = '['; + for (int32_t i = 1; i < pCtx->numOfParams; ++i) { + SVariant* pVal = &pCtx->param[i].param; - varDataVal(buf)[0] = '['; - for (int32_t i = 1; i < pCtx->numOfParams; ++i) { - SVariant* pVal = &pCtx->param[i].param; + GET_TYPED_DATA(v, double, pVal->nType, &pVal->i); + + int32_t code = getPercentile(pMemBucket, v, &ppInfo->result); + if (code != TSDB_CODE_SUCCESS) { + goto _fin_error; + } + + if (i == pCtx->numOfParams - 1) { + len += snprintf(varDataVal(buf) + len, sizeof(buf) - VARSTR_HEADER_SIZE - len, "%.6lf]", ppInfo->result); + } else { + len += snprintf(varDataVal(buf) + len, sizeof(buf) - VARSTR_HEADER_SIZE - len, "%.6lf, ", ppInfo->result); + } + } + + int32_t slotId = pCtx->pExpr->base.resSchema.slotId; + SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + + varDataSetLen(buf, len); + colDataAppend(pCol, pBlock->info.rows, buf, false); + + tMemBucketDestroy(pMemBucket); + return pResInfo->numOfRes; + } else { + SVariant* pVal = &pCtx->param[1].param; GET_TYPED_DATA(v, double, pVal->nType, &pVal->i); - int32_t code = getPercentile(pMemBucket, v, &ppInfo->result); + code = getPercentile(pMemBucket, v, &ppInfo->result); if (code != TSDB_CODE_SUCCESS) { goto _fin_error; } - if (i == pCtx->numOfParams - 1) { - len += snprintf(varDataVal(buf) + len, sizeof(buf) - VARSTR_HEADER_SIZE - len, "%.6lf]", ppInfo->result); - } else { - len += snprintf(varDataVal(buf) + len, sizeof(buf) - VARSTR_HEADER_SIZE - len, "%.6lf, ", ppInfo->result); - } + tMemBucketDestroy(pMemBucket); + return functionFinalize(pCtx, pBlock); } - - int32_t slotId = pCtx->pExpr->base.resSchema.slotId; - SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); - - varDataSetLen(buf, len); - colDataAppend(pCol, pBlock->info.rows, buf, false); - - tMemBucketDestroy(pMemBucket); - return pResInfo->numOfRes; - } else { - SVariant* pVal = &pCtx->param[1].param; - - GET_TYPED_DATA(v, double, pVal->nType, &pVal->i); - - code = getPercentile(pMemBucket, v, &ppInfo->result); - if (code != TSDB_CODE_SUCCESS) { - goto _fin_error; - } - - tMemBucketDestroy(pMemBucket); - return functionFinalize(pCtx, pBlock); } _fin_error: