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] 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); -- GitLab