提交 7075d621 编写于 作者: G Ganlin Zhao

[TD-11222]<feature>: Histogram function

上级 0f507561
...@@ -214,6 +214,7 @@ typedef struct { ...@@ -214,6 +214,7 @@ typedef struct {
double lower; // >lower double lower; // >lower
double upper; // <=upper double upper; // <=upper
int64_t count; int64_t count;
double count_norm;
} SHistogramFuncBin; } SHistogramFuncBin;
typedef struct{ typedef struct{
...@@ -4989,6 +4990,7 @@ static void histogram_function(SQLFunctionCtx *pCtx) { ...@@ -4989,6 +4990,7 @@ static void histogram_function(SQLFunctionCtx *pCtx) {
} }
int32_t notNullElems = 0; int32_t notNullElems = 0;
int32_t totalElems = 0;
for (int32_t i = 0; i < pCtx->size; ++i) { for (int32_t i = 0; i < pCtx->size; ++i) {
char *data = GET_INPUT_DATA(pCtx, i); char *data = GET_INPUT_DATA(pCtx, i);
if (pCtx->hasNull && isNull(data, pCtx->inputType)) { if (pCtx->hasNull && isNull(data, pCtx->inputType)) {
...@@ -5002,11 +5004,18 @@ static void histogram_function(SQLFunctionCtx *pCtx) { ...@@ -5002,11 +5004,18 @@ static void histogram_function(SQLFunctionCtx *pCtx) {
for (int32_t b = 0; b < pRes->numOfBins; ++b) { for (int32_t b = 0; b < pRes->numOfBins; ++b) {
if (v > pRes->orderedBins[b].lower && v <= pRes->orderedBins[b].upper) { if (v > pRes->orderedBins[b].lower && v <= pRes->orderedBins[b].upper) {
pRes->orderedBins[b].count++; pRes->orderedBins[b].count++;
totalElems++;
break; break;
} }
} }
} }
if (pRes->normalized) {
for (int32_t b = 0; b < pRes->numOfBins; ++b) {
pRes->orderedBins[b].count_norm = pRes->orderedBins[b].count / totalElems;
}
}
// treat the result as only one result // treat the result as only one result
SET_VAL(pCtx, notNullElems, 1); SET_VAL(pCtx, notNullElems, 1);
if (notNullElems > 0) { if (notNullElems > 0) {
...@@ -5036,8 +5045,14 @@ static void histogram_func_finalizer(SQLFunctionCtx *pCtx) { ...@@ -5036,8 +5045,14 @@ static void histogram_func_finalizer(SQLFunctionCtx *pCtx) {
} }
for (int32_t i = 0; i < pRes->numOfBins; ++i) { for (int32_t i = 0; i < pRes->numOfBins; ++i) {
int sz = sprintf(pCtx->pOutput + VARSTR_HEADER_SIZE, "(%g-%g]:%"PRId64, int sz;
pRes->orderedBins[i].lower, pRes->orderedBins[i].upper, pRes->orderedBins[i].count); if (!pRes->normalized) {
sz = sprintf(pCtx->pOutput + VARSTR_HEADER_SIZE, "(%g-%g]:%"PRId64,
pRes->orderedBins[i].lower, pRes->orderedBins[i].upper, pRes->orderedBins[i].count);
} else {
sz = sprintf(pCtx->pOutput + VARSTR_HEADER_SIZE, "(%g-%g]:%lf",
pRes->orderedBins[i].lower, pRes->orderedBins[i].upper, pRes->orderedBins[i].count_norm);
}
varDataSetLen(pCtx->pOutput, sz); varDataSetLen(pCtx->pOutput, sz);
pCtx->pOutput += pCtx->outputBytes; pCtx->pOutput += pCtx->outputBytes;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册