提交 660b610f 编写于 作者: G Ganlin Zhao

fix(query): fix histogram function output

上级 bb3681cd
...@@ -241,7 +241,7 @@ static int32_t translateHistogram(SFunctionNode* pFunc, char* pErrBuf, int32_t l ...@@ -241,7 +241,7 @@ static int32_t translateHistogram(SFunctionNode* pFunc, char* pErrBuf, int32_t l
return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName); return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
} }
pFunc->node.resType = (SDataType) { .bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE }; pFunc->node.resType = (SDataType) { .bytes = 512, .type = TSDB_DATA_TYPE_BINARY };
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
......
...@@ -1826,7 +1826,6 @@ static int8_t getHistogramBinType(char *binTypeStr) { ...@@ -1826,7 +1826,6 @@ static int8_t getHistogramBinType(char *binTypeStr) {
static bool getHistogramBinDesc(SHistoFuncInfo *pInfo, char *binDescStr, int8_t binType, bool normalized) { static bool getHistogramBinDesc(SHistoFuncInfo *pInfo, char *binDescStr, int8_t binType, bool normalized) {
cJSON* binDesc = cJSON_Parse(binDescStr); cJSON* binDesc = cJSON_Parse(binDescStr);
int32_t counter;
int32_t numOfBins; int32_t numOfBins;
double* intervals; double* intervals;
if (cJSON_IsObject(binDesc)) { /* linaer/log bins */ if (cJSON_IsObject(binDesc)) { /* linaer/log bins */
...@@ -1855,7 +1854,7 @@ static bool getHistogramBinDesc(SHistoFuncInfo *pInfo, char *binDescStr, int8_t ...@@ -1855,7 +1854,7 @@ static bool getHistogramBinDesc(SHistoFuncInfo *pInfo, char *binDescStr, int8_t
return false; return false;
} }
counter = (int32_t)count->valueint; int32_t counter = (int32_t)count->valueint;
if (infinity->valueint == false) { if (infinity->valueint == false) {
startIndex = 0; startIndex = 0;
numOfBins = counter + 1; numOfBins = counter + 1;
...@@ -1915,7 +1914,7 @@ static bool getHistogramBinDesc(SHistoFuncInfo *pInfo, char *binDescStr, int8_t ...@@ -1915,7 +1914,7 @@ static bool getHistogramBinDesc(SHistoFuncInfo *pInfo, char *binDescStr, int8_t
if (binType != USER_INPUT_BIN) { if (binType != USER_INPUT_BIN) {
return false; return false;
} }
counter = numOfBins = cJSON_GetArraySize(binDesc); numOfBins = cJSON_GetArraySize(binDesc);
intervals = taosMemoryCalloc(numOfBins, sizeof(double)); intervals = taosMemoryCalloc(numOfBins, sizeof(double));
cJSON* bin = binDesc->child; cJSON* bin = binDesc->child;
if (bin == NULL) { if (bin == NULL) {
...@@ -1940,9 +1939,9 @@ static bool getHistogramBinDesc(SHistoFuncInfo *pInfo, char *binDescStr, int8_t ...@@ -1940,9 +1939,9 @@ static bool getHistogramBinDesc(SHistoFuncInfo *pInfo, char *binDescStr, int8_t
return false; return false;
} }
pInfo->numOfBins = numOfBins; pInfo->numOfBins = numOfBins - 1;
pInfo->normalized = normalized; pInfo->normalized = normalized;
for (int32_t i = 0; i < numOfBins; ++i) { for (int32_t i = 0; i < pInfo->numOfBins; ++i) {
pInfo->bins[i].lower = intervals[i] < intervals[i + 1] ? intervals[i] : intervals[i + 1]; pInfo->bins[i].lower = intervals[i] < intervals[i + 1] ? intervals[i] : intervals[i + 1];
pInfo->bins[i].upper = intervals[i + 1] > intervals[i] ? intervals[i + 1] : intervals[i]; pInfo->bins[i].upper = intervals[i + 1] > intervals[i] ? intervals[i + 1] : intervals[i];
pInfo->bins[i].count = 0; pInfo->bins[i].count = 0;
...@@ -1989,6 +1988,8 @@ int32_t histogramFunction(SqlFunctionCtx *pCtx) { ...@@ -1989,6 +1988,8 @@ int32_t histogramFunction(SqlFunctionCtx *pCtx) {
continue; continue;
} }
numOfElems++;
char* data = colDataGetData(pCol, i); char* data = colDataGetData(pCol, i);
double v; double v;
GET_TYPED_DATA(v, double, type, data); GET_TYPED_DATA(v, double, type, data);
...@@ -1996,25 +1997,26 @@ int32_t histogramFunction(SqlFunctionCtx *pCtx) { ...@@ -1996,25 +1997,26 @@ 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++;
numOfElems++;
break; break;
} }
} }
} }
SET_VAL(GET_RES_INFO(pCtx), numOfElems, pInfo->numOfBins);
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
int32_t histogramFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t histogramFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx);
SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx));
int32_t slotId = pCtx->pExpr->base.resSchema.slotId; int32_t slotId = pCtx->pExpr->base.resSchema.slotId;
SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId);
int32_t currentRow = pBlock->info.rows; int32_t currentRow = pBlock->info.rows;
for (int32_t i = 0; i < pInfo->numOfBins; ++i) { for (int32_t i = 0; i < pResInfo->numOfRes; ++i) {
int32_t len; int32_t len;
char buf[400] = {0}; char buf[512] = {0};
if (!pInfo->normalized) { if (!pInfo->normalized) {
len = sprintf(buf + VARSTR_HEADER_SIZE, "{\"lower_bin\":%g, \"upper_bin\":%g, \"count\":%"PRId64"}", len = sprintf(buf + VARSTR_HEADER_SIZE, "{\"lower_bin\":%g, \"upper_bin\":%g, \"count\":%"PRId64"}",
pInfo->bins[i].lower, pInfo->bins[i].upper, pInfo->bins[i].count); pInfo->bins[i].lower, pInfo->bins[i].upper, pInfo->bins[i].count);
...@@ -2027,5 +2029,5 @@ int32_t histogramFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { ...@@ -2027,5 +2029,5 @@ int32_t histogramFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
currentRow++; currentRow++;
} }
return functionFinalize(pCtx, pBlock); return pResInfo->numOfRes;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册