未验证 提交 ee98673e 编写于 作者: S Shengliang Guan 提交者: GitHub

Merge pull request #17452 from taosdata/fix/TS-1913

fix(query): fix mode function behavior if multiple highest occurrence exist
......@@ -191,7 +191,7 @@ Query OK, 1 row(s) in set (0.000921s)
SELECT MODE(field_name) FROM tb_name [WHERE clause];
```
**Description**:The value which has the highest frequency of occurrence. NULL is returned if there are multiple values which have highest frequency of occurrence. It can't be used on timestamp column or tags.
**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. It can't be used on timestamp column or tags.
**Return value type**:Same as the data type of the column being operated upon
......
......@@ -193,7 +193,7 @@ Query OK, 1 row(s) in set (0.000921s)
SELECT MODE(field_name) FROM tb_name [WHERE clause];
```
**功能说明**:返回出现频率最高的值,若存在多个频率相同的最高值,输出空。不能匹配标签、时间戳输出。
**功能说明**:返回出现频率最高的值,若存在多个频率相同的最高值,则随机输出其中某个值。不能匹配标签、时间戳输出。
**返回数据类型**:同应用的字段。
......
......@@ -5912,6 +5912,11 @@ static void mode_func_finalizer(SQLFunctionCtx *pCtx) {
SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx);
SModeFuncInfo *pRes = GET_ROWCELL_INTERBUF(pResInfo);
if (pRes->num == 0) {
setNull(pCtx->pOutput, type, 0);
goto _mode_over;
}
size_t size = sizeof(ModeUnit) + bytes;
char *tvp = pRes->res;
......@@ -5919,20 +5924,18 @@ static void mode_func_finalizer(SQLFunctionCtx *pCtx) {
int64_t maxCount = 0;
for (int32_t i = 0; i < pRes->num; ++i) {
int64_t count = ((ModeUnit*)tvp)->count;
if (count > maxCount){
if (count >= maxCount){
maxCount = count;
result = tvp;
}else if(count == maxCount){
result = NULL;
}
tvp += size;
}
if (result){
memcpy(pCtx->pOutput, result + sizeof(ModeUnit), bytes);
}else{
setNull(pCtx->pOutput, type, 0);
}
_mode_over:
pResInfo->numOfRes = 1;
doFinalizer(pCtx);
}
......
......@@ -57,10 +57,10 @@ class TDTestCase:
tdSql.checkData(0, 1, 2)
tdSql.query('select mode(num) from d001')
tdSql.checkRows(1)
tdSql.checkData(0, 0, None)
tdSql.checkData(0, 0, -32767)
tdSql.query('select mode(dbool) from d001')
tdSql.checkRows(1)
tdSql.checkData(0, 0, None)
tdSql.checkData(0, 0, False)
tdSql.query('select mode(dtiny) from d001')
tdSql.checkRows(1)
tdSql.checkData(0, 0, None)
......@@ -87,7 +87,7 @@ class TDTestCase:
tdSql.checkData(0, 0, 1)
tdSql.query('select mode(num),mode(voltage) from smode')
tdSql.checkRows(1)
tdSql.checkData(0, 0, None)
tdSql.checkData(0, 0, 3276)
tdSql.checkData(0, 1, 1)
tdSql.query('select mode(dbool) from smode')
tdSql.checkRows(1)
......@@ -97,7 +97,7 @@ class TDTestCase:
tdSql.checkData(0, 0, None)
tdSql.query('select mode(dfloat) from smode')
tdSql.checkRows(1)
tdSql.checkData(0, 0, None)
tdSql.checkData(0, 0, 3.3232219219207764)
tdSql.query('select mode(ddouble) from smode')
tdSql.checkRows(1)
tdSql.checkData(0, 0, 4.982392323)
......@@ -112,16 +112,16 @@ class TDTestCase:
#group by column
tdSql.query('select mode(num) from d002 group by dbinary')
tdSql.checkRows(2)
tdSql.checkData(0, 0, None)
tdSql.checkData(0, 0, 3276)
tdSql.checkData(1, 0, None)
tdSql.execute('insert into D002 values("2021-11-17 20:31:31", 1, 3276, true, NULL, 3.32322, 4.982392323, "你好吗", "sdf", 333)')
tdSql.query('select mode(num) from d002 group by dbinary')
tdSql.checkRows(2)
tdSql.checkData(1, 0, 3276)
tdSql.checkData(0, 0, None)
tdSql.checkData(0, 0, 3276)
tdSql.query('select mode(dfloat) from d002 group by dbinary')
tdSql.checkRows(2)
tdSql.checkData(1, 0, None)
tdSql.checkData(1, 0, 3.3232200145721436)
tdSql.query('select mode(dchar) from d002 group by dbinary')
tdSql.checkRows(2)
tdSql.checkData(1, 0, "你好吗")
......@@ -138,7 +138,7 @@ class TDTestCase:
#group by tbname
tdSql.query('select mode(dchar) from smode group by tbname')
tdSql.checkRows(2)
tdSql.checkData(0, 0, None)
tdSql.checkData(0, 0, "你好吗")
tdSql.checkData(0, 1, "d001")
tdSql.checkData(1, 0, "你好吗")
tdSql.checkData(1, 1, "d002")
......@@ -148,7 +148,7 @@ class TDTestCase:
tdSql.checkRows(2)
tdSql.checkData(0, 0, 4.982392323)
tdSql.checkData(0, 1, "Beijing.haidian")
tdSql.checkData(1, 0, None)
tdSql.checkData(1, 0, 4.982392323)
tdSql.checkData(1, 1, "Beijing.Chaoyang")
#group by tag,column
......@@ -169,7 +169,7 @@ class TDTestCase:
tdSql.checkRows(2)
tdSql.checkData(0, 0, "你好吗")
tdSql.checkData(0, 1, "d002")
tdSql.checkData(1, 0, None)
tdSql.checkData(1, 0, "你好吗")
tdSql.checkData(1, 1, "d001")
#where
......@@ -178,7 +178,7 @@ class TDTestCase:
tdSql.checkData(0, 0, 19)
tdSql.query('select mode(voltage) from smode where num > 9')
tdSql.checkRows(1)
tdSql.checkData(0, 0, None)
tdSql.checkData(0, 0, 19)
#interval
tdSql.query('select mode(voltage) from smode interval(1s)')
......@@ -188,7 +188,7 @@ class TDTestCase:
tdSql.checkData(0, 0, "2021-01-01 00:00:00")
tdSql.checkData(0, 1, 1)
tdSql.checkData(1, 0, "2022-01-01 00:00:00")
tdSql.checkData(1, 1, None)
tdSql.checkData(1, 1, 19)
tdSql.query('select mode(voltage) from smode interval(1n)')
tdSql.checkRows(4)
tdSql.checkData(0, 0, "2021-10-01 00:00:00")
......@@ -198,7 +198,7 @@ class TDTestCase:
tdSql.checkData(2, 0, "2021-12-01 00:00:00")
tdSql.checkData(2, 1, 2)
tdSql.checkData(3, 0, "2022-01-01 00:00:00")
tdSql.checkData(3, 1, None)
tdSql.checkData(3, 1, 19)
tdSql.query('select mode(voltage) from smode where ts > "2021-09-01 00:00:00" and ts <"2022-02-02 00:00:00" interval(1n) fill(prev)')
tdSql.checkRows(6)
......@@ -207,7 +207,7 @@ class TDTestCase:
tdSql.checkData(3, 0, "2021-12-01 00:00:00")
tdSql.checkData(3, 1, 2)
tdSql.checkData(5, 0, "2022-02-01 00:00:00")
tdSql.checkData(5, 1, 2)
tdSql.checkData(5, 1, 19)
#session
tdSql.query('select mode(voltage) from d002 session(ts,1w)')
......@@ -222,7 +222,7 @@ class TDTestCase:
#state_window
tdSql.query('select mode(dfloat) from d002 state_window(voltage)')
tdSql.checkRows(3)
tdSql.checkData(0, 0, None)
tdSql.checkData(0, 0, 3.3232200145721436)
tdSql.checkData(1, 0, None)
#slimit/soffset
......@@ -237,9 +237,9 @@ class TDTestCase:
#having
tdSql.query('select mode(ddouble) from smode group by location having mode(ddouble)>3')
tdSql.checkRows(1)
tdSql.checkRows(2)
tdSql.checkData(0, 0, 4.982392323)
tdSql.checkData(0, 1, "Beijing.haidian")
tdSql.checkData(0, 1, "Beijing.Chaoyang")
#subquery
tdSql.query('select mode(ddouble) from (select * from smode where voltage = 1)')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册