diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 777990db666e40a8aa2ab2a0d54f0d21e35ca662..71bc9422ce771625d99689cf86e232609eba78db 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -5417,7 +5417,7 @@ int32_t validateWhereNode(SQueryInfo* pQueryInfo, tSqlExpr** pExpr, SSqlObj* pSq if (taosArrayGetSize(pQueryInfo->pUpstream) > 0 && condExpr.pTimewindow != NULL) { setNormalExprToCond(&condExpr.pColumnCond, condExpr.pTimewindow, TK_AND); - condExpr.pTimewindow = NULL; + condExpr.pTimewindow = tSqlExprClone(condExpr.pTimewindow); } tSqlExprCompact(pExpr); diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 0cf2fbf276a63164f6383cca3ab97fa46f6a5fe3..de820d220121039c4fe273e6c7528aa0a3680137 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -277,7 +277,7 @@ static int compareRowData(const void *a, const void *b, const void *userData) { return (in1 != NULL && in2 != NULL) ? supporter->comFunc(in1, in2) : 0; } -static void sortGroupResByOrderList(SGroupResInfo *pGroupResInfo, SQueryRuntimeEnv *pRuntimeEnv, SSDataBlock* pDataBlock) { +static void sortGroupResByOrderList(SGroupResInfo *pGroupResInfo, SQueryRuntimeEnv *pRuntimeEnv, SSDataBlock* pDataBlock, SQLFunctionCtx *pCtx) { SArray *columnOrderList = getOrderCheckColumns(pRuntimeEnv->pQueryAttr); size_t size = taosArrayGetSize(columnOrderList); taosArrayDestroy(columnOrderList); @@ -291,12 +291,23 @@ static void sortGroupResByOrderList(SGroupResInfo *pGroupResInfo, SQueryRuntimeE return; } + int32_t orderIndex = -1; + for (int32_t j = 0; j < pDataBlock->info.numOfCols; ++j) { + if (pCtx[j].colId == orderId) { + orderIndex = j; + break; + } + } + if (orderIndex < 0) { + return; + } + bool found = false; int16_t dataOffset = 0; for (int32_t j = 0; j < pDataBlock->info.numOfCols; ++j) { SColumnInfoData* pColInfoData = (SColumnInfoData *)taosArrayGet(pDataBlock->pDataBlock, j); - if (orderId == j) { + if (orderIndex == j) { found = true; break; } @@ -308,8 +319,7 @@ static void sortGroupResByOrderList(SGroupResInfo *pGroupResInfo, SQueryRuntimeE return; } - int16_t type = pRuntimeEnv->pQueryAttr->pExpr1[orderId].base.resType; - + int16_t type = pRuntimeEnv->pQueryAttr->pExpr1[orderIndex].base.resType; SRowCompSupporter support = {.pRuntimeEnv = pRuntimeEnv, .dataOffset = dataOffset, .comFunc = getComparFunc(type, 0)}; taosArraySortPWithExt(pGroupResInfo->pRows, compareRowData, &support); } @@ -6472,7 +6482,7 @@ static SSDataBlock* hashGroupbyAggregate(void* param, bool* newgroup) { initGroupResInfo(&pRuntimeEnv->groupResInfo, &pInfo->binfo.resultRowInfo); if (!pRuntimeEnv->pQueryAttr->stableQuery) { - sortGroupResByOrderList(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pInfo->binfo.pRes); + sortGroupResByOrderList(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pInfo->binfo.pRes, pInfo->binfo.pCtx); } toSSDataBlock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pInfo->binfo.pRes);