feat(query): add histogram normalized processing

This commit is contained in:
Ganlin Zhao 2022-05-07 14:23:07 +08:00
parent 660b610f04
commit dd7cec4132
1 changed files with 14 additions and 1 deletions

View File

@ -1972,7 +1972,6 @@ bool histogramFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo *pResultIn
} }
int32_t histogramFunction(SqlFunctionCtx *pCtx) { int32_t histogramFunction(SqlFunctionCtx *pCtx) {
int32_t numOfElems = 0;
SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
SInputColumnInfoData* pInput = &pCtx->input; SInputColumnInfoData* pInput = &pCtx->input;
@ -1983,6 +1982,8 @@ int32_t histogramFunction(SqlFunctionCtx *pCtx) {
int32_t start = pInput->startRowIndex; int32_t start = pInput->startRowIndex;
int32_t numOfRows = pInput->numOfRows; int32_t numOfRows = pInput->numOfRows;
int32_t numOfElems = 0;
int32_t totalElems = 0;
for (int32_t i = start; i < numOfRows + start; ++i) { for (int32_t i = start; i < numOfRows + start; ++i) {
if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) { if (pCol->hasNull && colDataIsNull_f(pCol->nullbitmap, i)) {
continue; continue;
@ -1997,9 +1998,21 @@ int32_t histogramFunction(SqlFunctionCtx *pCtx) {
for (int32_t k = 0; k < pInfo->numOfBins; ++k) { for (int32_t k = 0; k < pInfo->numOfBins; ++k) {
if (v > pInfo->bins[k].lower && v <= pInfo->bins[k].upper) { if (v > pInfo->bins[k].lower && v <= pInfo->bins[k].upper) {
pInfo->bins[k].count++; pInfo->bins[k].count++;
totalElems++;
break; 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); SET_VAL(GET_RES_INFO(pCtx), numOfElems, pInfo->numOfBins);