diff --git a/source/libs/planner/src/planOptimizer.c b/source/libs/planner/src/planOptimizer.c index b00767b6b6..47dbfe95d4 100644 --- a/source/libs/planner/src/planOptimizer.c +++ b/source/libs/planner/src/planOptimizer.c @@ -4056,6 +4056,8 @@ static int32_t lastRowScanOptimize(SOptimizeContext* pCxt, SLogicSubplan* pLogic pScan->igLastNull = pAgg->hasLast ? true : false; SArray* isDuplicateCol = taosArrayInit(pScan->pScanCols->length, sizeof(bool)); SNodeList* pLastRowCols = NULL; + bool adjLastRowTsColName = false; + char tsColName[TSDB_COL_NAME_LEN] = {0}; FOREACH(pNode, pAgg->pAggFuncs) { SFunctionNode* pFunc = (SFunctionNode*)pNode; @@ -4094,6 +4096,11 @@ static int32_t lastRowScanOptimize(SOptimizeContext* pCxt, SLogicSubplan* pLogic sprintf(((SColumnNode*)newColNode)->colName, "#dup_col.%p", newColNode); sprintf(((SColumnNode*)pParamNode)->colName, "#dup_col.%p", newColNode); + if (FUNCTION_TYPE_LAST_ROW == funcType && ((SColumnNode*)pParamNode)->colId == PRIMARYKEY_TIMESTAMP_COL_ID) { + adjLastRowTsColName = true; + strncpy(tsColName, ((SColumnNode*)pParamNode)->colName, TSDB_COL_NAME_LEN); + } + nodesListAppend(pScan->pScanCols, newColNode); isDup = true; taosArrayInsert(isDuplicateCol, pScan->pScanCols->length, &isDup); @@ -4112,7 +4119,7 @@ static int32_t lastRowScanOptimize(SOptimizeContext* pCxt, SLogicSubplan* pLogic lastRowScanBuildFuncTypes(pScan, (SColumnNode*)pColNode, pFunc->funcType); } continue; - }else if (nodeListNodeEqual(pFunc->pParameterList, pColNode)) { + } else if (nodeListNodeEqual(pFunc->pParameterList, pColNode)) { if (funcType != FUNCTION_TYPE_LAST && ((SColumnNode*)pColNode)->colId == PRIMARYKEY_TIMESTAMP_COL_ID && !nodeListNodeEqual(pLastRowCols, pColNode)) { nodesListMakeAppend(&pLastRowCols, nodesCloneNode(pColNode)); @@ -4140,6 +4147,10 @@ static int32_t lastRowScanOptimize(SOptimizeContext* pCxt, SLogicSubplan* pLogic if (pFunc->hasPk) { nodesListMakeAppend(&cxt.pOtherCols, nodesListGetNode(pFunc->pParameterList, LIST_LENGTH(pFunc->pParameterList) - 1)); } + if (FUNCTION_TYPE_LAST_ROW == funcType && adjLastRowTsColName) { + SNode* pParamNodeTs = nodesListGetNode(pFunc->pParameterList, 1); + strncpy(((SColumnNode*)pParamNodeTs)->colName, tsColName, TSDB_COL_NAME_LEN); + } } else { pNode = nodesListGetNode(pFunc->pParameterList, 0); nodesListMakeAppend(&cxt.pOtherCols, pNode); diff --git a/tests/script/tsim/query/cache_last.sim b/tests/script/tsim/query/cache_last.sim index b2d5ad8aa0..a06e6e1e6f 100644 --- a/tests/script/tsim/query/cache_last.sim +++ b/tests/script/tsim/query/cache_last.sim @@ -141,7 +141,7 @@ if $data01 != t2 then return -1 endi -sql select last(ts) ts1,tbname, ts from stb group by tbname; +sql select last(ts) ts1,tbname, ts from stb group by tbname order by 1 desc; if $data00 != $data02 then print $data00 @@ -156,16 +156,16 @@ endi print step 3------------------------------- sql drop database if exists test1; -sql create database test1 cachemodel 'both'; +sql create database test1 cachemodel 'both' vgroups 4; sql use test1; sql create table stb (ts timestamp,a int primary key,b int,c int) tags(ta int,tb int,tc int); -sql create table t1 using stb tags(1,1,1); -sql create table t2 using stb tags(2,2,2); -sql insert into t1 values('2024-06-05 11:00:00',1,2,3); -sql insert into t1 values('2024-06-05 12:00:00',2,2,3); -sql insert into t2 values('2024-06-05 13:00:00',3,2,3); -sql insert into t2 values('2024-06-05 14:00:00',4,2,3); +sql create table aaat1 using stb tags(1,1,1); +sql create table bbbt2 using stb tags(2,2,2); +sql insert into aaat1 values('2024-06-05 11:00:00',1,2,3); +sql insert into aaat1 values('2024-06-05 12:00:00',2,2,3); +sql insert into bbbt2 values('2024-06-05 13:00:00',3,2,3); +sql insert into bbbt2 values('2024-06-05 14:00:00',4,2,3); sql select last(ts) ts1,ts from stb; @@ -179,7 +179,7 @@ if $data00 != @24-06-05 14:00:00.000@ then return -1 endi -sql select last(ts) ts1,ts from stb group by tbname; +sql select last(ts) ts1,ts from stb group by tbname order by 1 desc; if $data00 != $data01 then print $data00 @@ -203,12 +203,12 @@ if $data00 != @24-06-05 14:00:00.000@ then return -1 endi -if $data01 != t2 then +if $data01 != bbbt2 then print $data01 return -1 endi -sql select last(ts) ts1,tbname, ts from stb group by tbname; +sql select last(ts) ts1,tbname, ts from stb group by tbname order by 1 desc; if $data00 != $data02 then print $data00 @@ -220,7 +220,7 @@ if $data00 != @24-06-05 14:00:00.000@ then return -1 endi -if $data01 != t2 then +if $data01 != bbbt2 then print $data01 return -1 endi @@ -239,7 +239,7 @@ if $data01 != @24-06-05 14:00:00.000@ then return -1 endi -sql select last(a) a,ts from stb group by tbname; +sql select last(a) a,ts from stb group by tbname order by 1 desc; if $data00 != 4 then print $data00 @@ -258,7 +258,7 @@ if $data00 != 4 then return -1 endi -if $data01 != t2 then +if $data01 != bbbt2 then print $data01 return -1 endi @@ -268,14 +268,14 @@ if $data02 != @24-06-05 14:00:00.000@ then return -1 endi -sql select last(a) a,tbname, ts from stb group by tbname; +sql select last(a) a,tbname, ts from stb group by tbname order by 1 desc; if $data00 != 4 then print $data00 return -1 endi -if $data01 != t2 then +if $data01 != bbbt2 then print $data01 return -1 endi @@ -299,7 +299,7 @@ if $data01 != 4 then return -1 endi -sql select last(ts) ts1,a from stb group by tbname; +sql select last(ts) ts1,a from stb group by tbname order by 1 desc; if $data00 != @24-06-05 14:00:00.000@ then print $data00 @@ -318,7 +318,7 @@ if $data00 != @24-06-05 14:00:00.000@ then return -1 endi -if $data01 != t2 then +if $data01 != bbbt2 then print $data01 return -1 endi @@ -328,14 +328,14 @@ if $data02 != 4 then return -1 endi -sql select last(ts) ts1,tbname, a from stb group by tbname; +sql select last(ts) ts1,tbname, a from stb group by tbname order by 1 desc; if $data00 != @24-06-05 14:00:00.000@ then print $data00 return -1 endi -if $data01 != t2 then +if $data01 != bbbt2 then print $data01 return -1 endi @@ -345,4 +345,25 @@ if $data02 != 4 then return -1 endi +$loop_count = 0 + +loop0: + +sql select last(ts), last_row(ts) from stb; + +if $data00 != $data01 then + print ====data00=$data00 + print ====data01=$data01 + return -1 +endi + +$loop_count = $loop_count + 1 +if $loop_count < 10 then + sleep 200 + print ====checktimes=$loop_count + goto loop0 +endi + +print ------------------------end + system sh/exec.sh -n dnode1 -s stop -x SIGINT