From 93e92832e27bc65e07a447c5bf017f07347a0880 Mon Sep 17 00:00:00 2001 From: dapan1121 <89396746@qq.com> Date: Mon, 8 Mar 2021 10:13:07 +0800 Subject: [PATCH 1/3] fix bug --- src/client/src/tscSQLParser.c | 42 +++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index d5f8f420bf..125700d662 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -1872,6 +1872,24 @@ void setResultColName(char* name, tSqlExprItem* pItem, int32_t functionId, SStrT } } + +void setLastOrderForGoupBy(SQueryInfo* pQueryInfo, STableMetaInfo* pTableMetaInfo) { // todo refactor + SSqlGroupbyExpr* pGroupBy = &pQueryInfo->groupbyExpr; + if (pGroupBy->numOfGroupCols > 0) { + size_t idx = taosArrayGetSize(pQueryInfo->exprList); + for(int32_t k = 0; k < pGroupBy->numOfGroupCols; ++k) { + SColIndex* pIndex = taosArrayGet(pGroupBy->columnInfo, k); + if (!TSDB_COL_IS_TAG(pIndex->flag) && pIndex->colIndex < tscGetNumOfColumns(pTableMetaInfo->pTableMeta)) { // group by normal columns + SSqlExpr* pExpr = taosArrayGetP(pQueryInfo->exprList, idx - 1); + pExpr->numOfParams = 1; + pExpr->param->i64 = TSDB_ORDER_ASC; + + break; + } + } + } +} + int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t colIndex, tSqlExprItem* pItem, bool finalResult) { STableMetaInfo* pTableMetaInfo = NULL; int32_t optr = pItem->pNode->nSQLOptr; @@ -2152,6 +2170,10 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col if (setExprInfoForFunctions(pCmd, pQueryInfo, &pSchema[j], cvtFunc, name, colIndex++, &index, finalResult) != 0) { return TSDB_CODE_TSC_INVALID_SQL; } + + if (optr == TK_LAST) { + setLastOrderForGoupBy(pQueryInfo, pTableMetaInfo); + } } } else { @@ -2177,20 +2199,8 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col return TSDB_CODE_TSC_INVALID_SQL; } - if (optr == TK_LAST) { // todo refactor - SSqlGroupbyExpr* pGroupBy = &pQueryInfo->groupbyExpr; - if (pGroupBy->numOfGroupCols > 0) { - for(int32_t k = 0; k < pGroupBy->numOfGroupCols; ++k) { - SColIndex* pIndex = taosArrayGet(pGroupBy->columnInfo, k); - if (!TSDB_COL_IS_TAG(pIndex->flag) && pIndex->colIndex < tscGetNumOfColumns(pTableMetaInfo->pTableMeta)) { // group by normal columns - SSqlExpr* pExpr = taosArrayGetP(pQueryInfo->exprList, colIndex + i); - pExpr->numOfParams = 1; - pExpr->param->i64 = TSDB_ORDER_ASC; - - break; - } - } - } + if (optr == TK_LAST) { + setLastOrderForGoupBy(pQueryInfo, pTableMetaInfo); } } } @@ -2220,6 +2230,10 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col } colIndex++; + + if (optr == TK_LAST) { + setLastOrderForGoupBy(pQueryInfo, pTableMetaInfo); + } } numOfFields += tscGetNumOfColumns(pTableMetaInfo->pTableMeta); From 572df7364615cdcffb26ed93b472feaa562eb5af Mon Sep 17 00:00:00 2001 From: dapan1121 <89396746@qq.com> Date: Mon, 8 Mar 2021 11:39:08 +0800 Subject: [PATCH 2/3] fix bug --- src/client/src/tscSQLParser.c | 2 +- tests/script/general/parser/last_groupby.sim | 67 ++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/script/general/parser/last_groupby.sim diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 125700d662..24397b8dba 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -2195,7 +2195,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col bool multiColOutput = pItem->pNode->pParam->nExpr > 1; setResultColName(name, pItem, cvtFunc.originFuncId, &pParamElem->pNode->colInfo, multiColOutput); - if (setExprInfoForFunctions(pCmd, pQueryInfo, pSchema, cvtFunc, name, colIndex + i, &index, finalResult) != 0) { + if (setExprInfoForFunctions(pCmd, pQueryInfo, pSchema, cvtFunc, name, colIndex++, &index, finalResult) != 0) { return TSDB_CODE_TSC_INVALID_SQL; } diff --git a/tests/script/general/parser/last_groupby.sim b/tests/script/general/parser/last_groupby.sim new file mode 100644 index 0000000000..ed86e1ee40 --- /dev/null +++ b/tests/script/general/parser/last_groupby.sim @@ -0,0 +1,67 @@ +system sh/stop_dnodes.sh +system sh/deploy.sh -n dnode1 -i 1 +system sh/cfg.sh -n dnode1 -c walLevel -v 0 +system sh/cfg.sh -n dnode1 -c maxtablesPerVnode -v 2 +system sh/exec.sh -n dnode1 -s start + +sleep 100 +sql connect +print ======================== dnode1 start + +$db = testdb + +sql create database $db +sql use $db + +sql create stable st2 (ts timestamp, f1 int, f2 float, f3 double, f4 bigint, f5 smallint, f6 tinyint, f7 bool, f8 binary(10), f9 nchar(10)) tags (id1 int, id2 float, id3 nchar(10), id4 double, id5 smallint, id6 bigint, id7 binary(10)) + +sql create table tb1 using st2 tags (1,1.0,"1",1.0,1,1,"1"); + +sql insert into tb1 values (now-200s,1,1.0,1.0,1,1,1,true,"1","1") +sql insert into tb1 values (now-100s,2,2.0,2.0,2,2,2,true,"2","2") +sql insert into tb1 values (now,3,3.0,3.0,3,3,3,true,"3","3") +sql insert into tb1 values (now+100s,4,4.0,4.0,4,4,4,true,"4","4") +sql insert into tb1 values (now+200s,4,4.0,4.0,4,4,4,true,"4","4") +sql insert into tb1 values (now+300s,4,4.0,4.0,4,4,4,true,"4","4") +sql insert into tb1 values (now+400s,4,4.0,4.0,4,4,4,true,"4","4") +sql insert into tb1 values (now+500s,4,4.0,4.0,4,4,4,true,"4","4") + +sql select f1,last(*) from st2 group by f1; + +print $data00 $data01 $data02 $data03 $data04 $data05 $data06 $data07 $data08 $data09 $data10 + +if $rows != 4 then + return -1 +endi + +if $data00 != 1 then + return -1 +endi + +if $data02 != 1 then + print $data02 + return -1 +endi +if $data03 != 1.00000 then + return -1 +endi +if $data04 != 1.000000000 then + return -1 +endi +if $data05 != 1 then + return -1 +endi +if $data06 != 1 then + return -1 +endi +if $data07 != 1 then + return -1 +endi +if $data08 != 1 then + return -1 +endi +if $data09 != 1 then + return -1 +endi + +system sh/exec.sh -n dnode1 -s stop -x SIGINT From 57545ec692afc53b34632eb1d340856eaac120f4 Mon Sep 17 00:00:00 2001 From: dapan1121 <89396746@qq.com> Date: Mon, 8 Mar 2021 11:43:29 +0800 Subject: [PATCH 3/3] add case --- tests/script/general/parser/last_groupby.sim | 36 ++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/tests/script/general/parser/last_groupby.sim b/tests/script/general/parser/last_groupby.sim index ed86e1ee40..f993324cd1 100644 --- a/tests/script/general/parser/last_groupby.sim +++ b/tests/script/general/parser/last_groupby.sim @@ -28,8 +28,6 @@ sql insert into tb1 values (now+500s,4,4.0,4.0,4,4,4,true,"4","4") sql select f1,last(*) from st2 group by f1; -print $data00 $data01 $data02 $data03 $data04 $data05 $data06 $data07 $data08 $data09 $data10 - if $rows != 4 then return -1 endi @@ -64,4 +62,38 @@ if $data09 != 1 then return -1 endi +sql select f1,last(f1,st2.*) from st2 group by f1; +if $rows != 4 then + return -1 +endi + +if $data00 != 1 then + return -1 +endi + +if $data01 != 1 then + return -1 +endi +if $data03 != 1 then + return -1 +endi +if $data04 != 1.00000 then + return -1 +endi +if $data05 != 1.000000000 then + return -1 +endi +if $data06 != 1 then + return -1 +endi +if $data07 != 1 then + return -1 +endi +if $data08 != 1 then + return -1 +endi +if $data09 != 1 then + return -1 +endi + system sh/exec.sh -n dnode1 -s stop -x SIGINT