From 0f5e02b0b40aef533fc08fb95d2ee9b1d9e40ca8 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Tue, 18 Oct 2022 17:52:40 +0800 Subject: [PATCH 1/4] fix(query): mode function output one random result instead of NULL if there're multiple highest frequency of occurrence. TD-19619 --- source/libs/function/src/builtinsimpl.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 7077a9b780..6a428af1ed 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -5356,16 +5356,14 @@ int32_t modeFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { int32_t maxCount = 0; for (int32_t i = 0; i < pInfo->numOfPoints; ++i) { SModeItem* pItem = (SModeItem*)(pInfo->pItems + i * (sizeof(SModeItem) + pInfo->colBytes)); - if (pItem->count > maxCount) { + if (pItem->count >= maxCount) { maxCount = pItem->count; resIndex = i; - } else if (pItem->count == maxCount) { - resIndex = -1; } } SModeItem* pResItem = (SModeItem*)(pInfo->pItems + resIndex * (sizeof(SModeItem) + pInfo->colBytes)); - colDataAppend(pCol, currentRow, pResItem->data, (resIndex == -1) ? true : false); + colDataAppend(pCol, currentRow, pResItem->data, false); return pResInfo->numOfRes; } From 25c2cec6028effa60312433b6af1b7da766362af Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Tue, 18 Oct 2022 17:58:17 +0800 Subject: [PATCH 2/4] change mode en docs --- docs/en/12-taos-sql/10-function.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/12-taos-sql/10-function.md b/docs/en/12-taos-sql/10-function.md index 2b39c4b9e5..48bc5e743e 100644 --- a/docs/en/12-taos-sql/10-function.md +++ b/docs/en/12-taos-sql/10-function.md @@ -945,7 +945,7 @@ MIN(expr) MODE(expr) ``` -**Description**:The value which has the highest frequency of occurrence. NULL is returned if there are multiple values which have highest frequency of occurrence. +**Description**:The value which has the highest frequency of occurrence. One random value is returned if there are multiple values which have highest frequency of occurrence. **Return value type**: Same as the input data From 3ec1490fe2050e769f496c05347558d75e1ac3cc Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Tue, 18 Oct 2022 18:00:53 +0800 Subject: [PATCH 3/4] change mode function zh docs --- docs/zh/12-taos-sql/10-function.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zh/12-taos-sql/10-function.md b/docs/zh/12-taos-sql/10-function.md index 9346c3f763..e7ed74631d 100644 --- a/docs/zh/12-taos-sql/10-function.md +++ b/docs/zh/12-taos-sql/10-function.md @@ -946,7 +946,7 @@ MIN(expr) MODE(expr) ``` -**功能说明**:返回出现频率最高的值,若存在多个频率相同的最高值,输出NULL。 +**功能说明**:返回出现频率最高的值,若存在多个频率相同的最高值,则随机输出其中某个值。 **返回数据类型**:与输入数据类型一致。 From 2d1f7354a94b9fd6e447c2bf73f7741d837d31a6 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Tue, 18 Oct 2022 19:11:49 +0800 Subject: [PATCH 4/4] fix null value handling --- source/libs/function/src/builtinsimpl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 6a428af1ed..0ab8fb0550 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -5363,7 +5363,7 @@ int32_t modeFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { } SModeItem* pResItem = (SModeItem*)(pInfo->pItems + resIndex * (sizeof(SModeItem) + pInfo->colBytes)); - colDataAppend(pCol, currentRow, pResItem->data, false); + colDataAppend(pCol, currentRow, pResItem->data, (maxCount == 0) ? true : false); return pResInfo->numOfRes; }