From dd7cec41324e5cfd0037302c30290f90d053804d Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Sat, 7 May 2022 14:23:07 +0800 Subject: [PATCH 1/3] feat(query): add histogram normalized processing --- source/libs/function/src/builtinsimpl.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 909659701c..7229cf98b0 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -1972,7 +1972,6 @@ bool histogramFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo *pResultIn } int32_t histogramFunction(SqlFunctionCtx *pCtx) { - int32_t numOfElems = 0; SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); SInputColumnInfoData* pInput = &pCtx->input; @@ -1983,6 +1982,8 @@ int32_t histogramFunction(SqlFunctionCtx *pCtx) { int32_t start = pInput->startRowIndex; int32_t numOfRows = pInput->numOfRows; + int32_t numOfElems = 0; + int32_t totalElems = 0; for (int32_t i = start; i < numOfRows + start; ++i) { if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) { continue; @@ -1997,9 +1998,21 @@ int32_t histogramFunction(SqlFunctionCtx *pCtx) { for (int32_t k = 0; k < pInfo->numOfBins; ++k) { if (v > pInfo->bins[k].lower && v <= pInfo->bins[k].upper) { pInfo->bins[k].count++; + totalElems++; break; } } + + } + + if (pInfo->normalized) { + for (int32_t k = 0; k < pInfo->numOfBins; ++k) { + if(totalElems != 0) { + pInfo->bins[k].percentage = pInfo->bins[k].count / (double)totalElems; + } else { + pInfo->bins[k].percentage = 0; + } + } } SET_VAL(GET_RES_INFO(pCtx), numOfElems, pInfo->numOfBins); From e2413a062c477784aaf40062f9db7330c303177d Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Sat, 7 May 2022 14:29:50 +0800 Subject: [PATCH 2/3] fix: histogram normalized can only be 0/1 --- source/libs/function/src/builtinsimpl.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 7229cf98b0..0808ec58b0 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -1964,6 +1964,9 @@ bool histogramFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo *pResultIn } char* binDesc = varDataVal(pCtx->param[2].param.pz); int64_t normalized = pCtx->param[3].param.i; + if (normalized != 0 && normalized != 1) { + return false; + } if (!getHistogramBinDesc(pInfo, binDesc, binType, (bool)normalized)) { return false; } From 5763b24843059d8bef618f1df32c3f43509d0416 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Sat, 7 May 2022 15:51:02 +0800 Subject: [PATCH 3/3] fix(query): fix timeunit not processed in time functions when calculate constant values. --- source/libs/parser/src/parTranslater.c | 1 + 1 file changed, 1 insertion(+) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index bd614d0165..ea5d373a73 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -481,6 +481,7 @@ static EDealRes translateValue(STranslateContext* pCxt, SValueNode* pVal) { TSDB_CODE_SUCCESS) { return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, pVal->literal); } + *(int64_t*)&pVal->typeData = pVal->datum.i; } else { switch (pVal->node.resType.type) { case TSDB_DATA_TYPE_NULL: