From 9da0de4d5f5d06f832edad632c06723d0ab8e96f Mon Sep 17 00:00:00 2001 From: Minglei Jin Date: Fri, 13 Dec 2024 11:54:28 +0800 Subject: [PATCH 1/2] fix(tsdb/cache): fix invalid param caused by column mismatching --- source/dnode/vnode/src/tsdb/tsdbCache.c | 32 ++++--- tests/script/tsim/parser/last_both.sim | 1 + tests/script/tsim/parser/last_both_no_ts.tsim | 86 +++++++++++++++++++ 3 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 tests/script/tsim/parser/last_both_no_ts.tsim diff --git a/source/dnode/vnode/src/tsdb/tsdbCache.c b/source/dnode/vnode/src/tsdb/tsdbCache.c index 2cef541cdb..2f61549d18 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCache.c +++ b/source/dnode/vnode/src/tsdb/tsdbCache.c @@ -1540,6 +1540,7 @@ static int32_t tsdbCacheLoadFromRaw(STsdb *pTsdb, tb_uid_t uid, SArray *pLastArr int32_t code = 0, lino = 0; rocksdb_writebatch_t *wb = NULL; SArray *pTmpColArray = NULL; + bool extraTS = false; SIdxKey *idxKey = taosArrayGet(remainCols, 0); if (idxKey->key.cid != PRIMARYKEY_TIMESTAMP_COL_ID) { @@ -1551,23 +1552,19 @@ static int32_t tsdbCacheLoadFromRaw(STsdb *pTsdb, tb_uid_t uid, SArray *pLastArr if (!taosArrayInsert(remainCols, 0, &(SIdxKey){0, *key})) { TAOS_RETURN(terrno); } + + extraTS = true; } int num_keys = TARRAY_SIZE(remainCols); int16_t *slotIds = taosMemoryMalloc(num_keys * sizeof(int16_t)); - int16_t *lastColIds = NULL; - int16_t *lastSlotIds = NULL; - int16_t *lastrowColIds = NULL; - int16_t *lastrowSlotIds = NULL; + int16_t *lastColIds = NULL, *lastSlotIds = NULL, *lastrowColIds = NULL, *lastrowSlotIds = NULL; lastColIds = taosMemoryMalloc(num_keys * sizeof(int16_t)); lastSlotIds = taosMemoryMalloc(num_keys * sizeof(int16_t)); lastrowColIds = taosMemoryMalloc(num_keys * sizeof(int16_t)); lastrowSlotIds = taosMemoryMalloc(num_keys * sizeof(int16_t)); - SArray *lastTmpColArray = NULL; - SArray *lastTmpIndexArray = NULL; - SArray *lastrowTmpColArray = NULL; - SArray *lastrowTmpIndexArray = NULL; + SArray *lastTmpColArray = NULL, *lastTmpIndexArray = NULL, *lastrowTmpColArray = NULL, *lastrowTmpIndexArray = NULL; int lastIndex = 0; int lastrowIndex = 0; @@ -1578,7 +1575,12 @@ static int32_t tsdbCacheLoadFromRaw(STsdb *pTsdb, tb_uid_t uid, SArray *pLastArr for (int i = 0; i < num_keys; ++i) { SIdxKey *idxKey = taosArrayGet(remainCols, i); - slotIds[i] = pr->pSlotIds[idxKey->idx]; + if (extraTS && !i) { + slotIds[i] = 0; + } else { + slotIds[i] = pr->pSlotIds[idxKey->idx]; + } + if (IS_LAST_KEY(idxKey->key)) { if (NULL == lastTmpIndexArray) { lastTmpIndexArray = taosArrayInit(num_keys, sizeof(int32_t)); @@ -1590,7 +1592,11 @@ static int32_t tsdbCacheLoadFromRaw(STsdb *pTsdb, tb_uid_t uid, SArray *pLastArr TAOS_CHECK_EXIT(terrno); } lastColIds[lastIndex] = idxKey->key.cid; - lastSlotIds[lastIndex] = pr->pSlotIds[idxKey->idx]; + if (extraTS && !i) { + lastSlotIds[lastIndex] = 0; + } else { + lastSlotIds[lastIndex] = pr->pSlotIds[idxKey->idx]; + } lastIndex++; } else { if (NULL == lastrowTmpIndexArray) { @@ -1603,7 +1609,11 @@ static int32_t tsdbCacheLoadFromRaw(STsdb *pTsdb, tb_uid_t uid, SArray *pLastArr TAOS_CHECK_EXIT(terrno); } lastrowColIds[lastrowIndex] = idxKey->key.cid; - lastrowSlotIds[lastrowIndex] = pr->pSlotIds[idxKey->idx]; + if (extraTS && !i) { + lastrowSlotIds[lastrowIndex] = 0; + } else { + lastrowSlotIds[lastrowIndex] = pr->pSlotIds[idxKey->idx]; + } lastrowIndex++; } } diff --git a/tests/script/tsim/parser/last_both.sim b/tests/script/tsim/parser/last_both.sim index e01a966744..98548c5257 100644 --- a/tests/script/tsim/parser/last_both.sim +++ b/tests/script/tsim/parser/last_both.sim @@ -145,6 +145,7 @@ sql alter database $db cachemodel 'both' sql alter database $db cachesize 2 sleep 11000 +run tsim/parser/last_both_no_ts.tsim run tsim/parser/last_both_query.sim system sh/exec.sh -n dnode1 -s stop -x SIGINT diff --git a/tests/script/tsim/parser/last_both_no_ts.tsim b/tests/script/tsim/parser/last_both_no_ts.tsim new file mode 100644 index 0000000000..94bf9f6ddf --- /dev/null +++ b/tests/script/tsim/parser/last_both_no_ts.tsim @@ -0,0 +1,86 @@ +$db = testdb +sql use $db +print "test tb1" + +sql select last_row(f1, f2, f3, f4) from st2 +if $rows != 1 then + return -1 +endi +if $data00 != 6 then + return -1 +endi +if $data01 != NULL then + print expect NULL actual: $data02 + return -1 +endi +if $data02 != NULL then + print expect NULL actual: $data02 + return -1 +endi +if $data03 != @70-01-01 07:59:57.000@ then + return -1 +endi + +sql select last_row(*) from st2 +if $rows != 1 then + return -1 +endi +if $data00 != @21-05-12 10:10:12.000@ then + print $data00 + return -1 +endi +if $data01 != 6 then + return -1 +endi +if $data02 != NULL then + print expect NULL actual: $data02 + return -1 +endi +if $data03 != NULL then + print expect NULL actual: $data02 + return -1 +endi +if $data04 != @70-01-01 07:59:57.000@ then + return -1 +endi + +sql select last(f1, f2, f3, f4) from st2 +if $rows != 1 then + return -1 +endi +if $data00 != 6 then + return -1 +endi +if $data01 != 37.000000000 then + print expect 37.000000000 actual: $data02 + return -1 +endi +if $data02 != 27 then + return -1 +endi +if $data03 != @70-01-01 07:59:57.000@ then + return -1 +endi + +sql select last(*) from st2 +if $rows != 1 then + return -1 +endi +if $data00 != @21-05-12 10:10:12.000@ then + print $data00 + return -1 +endi +if $data01 != 6 then + return -1 +endi +if $data02 != 37.000000000 then + print expect 37.000000000 actual: $data02 + return -1 +endi +if $data03 != 27 then + return -1 +endi +if $data04 != @70-01-01 07:59:57.000@ then + return -1 +endi + From 7e7572cd8aeb7db60bfdca81d416a0d6e6e59de3 Mon Sep 17 00:00:00 2001 From: Minglei Jin Date: Fri, 13 Dec 2024 17:44:26 +0800 Subject: [PATCH 2/2] not update auxiliaried ts to none --- source/dnode/vnode/src/tsdb/tsdbCache.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbCache.c b/source/dnode/vnode/src/tsdb/tsdbCache.c index 2f61549d18..72d22b8cf2 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCache.c +++ b/source/dnode/vnode/src/tsdb/tsdbCache.c @@ -1545,8 +1545,8 @@ static int32_t tsdbCacheLoadFromRaw(STsdb *pTsdb, tb_uid_t uid, SArray *pLastArr SIdxKey *idxKey = taosArrayGet(remainCols, 0); if (idxKey->key.cid != PRIMARYKEY_TIMESTAMP_COL_ID) { // ignore 'ts' loaded from cache and load it from tsdb - SLastCol *pLastCol = taosArrayGet(pLastArray, 0); - tsdbCacheUpdateLastColToNone(pLastCol, TSDB_LAST_CACHE_NO_CACHE); + // SLastCol *pLastCol = taosArrayGet(pLastArray, 0); + // tsdbCacheUpdateLastColToNone(pLastCol, TSDB_LAST_CACHE_NO_CACHE); SLastKey *key = &(SLastKey){.lflag = ltype, .uid = uid, .cid = PRIMARYKEY_TIMESTAMP_COL_ID}; if (!taosArrayInsert(remainCols, 0, &(SIdxKey){0, *key})) { @@ -1660,7 +1660,9 @@ static int32_t tsdbCacheLoadFromRaw(STsdb *pTsdb, tb_uid_t uid, SArray *pLastArr pLastCol = &noneCol; } - taosArraySet(pLastArray, idxKey->idx, pLastCol); + if (!extraTS || i > 0) { + taosArraySet(pLastArray, idxKey->idx, pLastCol); + } // taosArrayRemove(remainCols, i); if (/*!pTmpColArray*/ lastTmpIndexArray && !lastTmpColArray) {