From f13c9d2ca4dc9b13e0507e9c8dd339dc40acdb6f Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Fri, 1 Mar 2024 09:49:59 +0800 Subject: [PATCH] fix: merge join timetruncate issue --- source/libs/executor/src/mergejoinoperator.c | 4 +- source/libs/executor/test/joinTests.cpp | 1 + tests/script/tsim/join/join.sim | 20 +-- tests/script/tsim/join/join_scalar.sim | 159 +++++++++++++++++++ 4 files changed, 172 insertions(+), 12 deletions(-) diff --git a/source/libs/executor/src/mergejoinoperator.c b/source/libs/executor/src/mergejoinoperator.c index c7e8b80007..b8c28cfe88 100644 --- a/source/libs/executor/src/mergejoinoperator.c +++ b/source/libs/executor/src/mergejoinoperator.c @@ -798,7 +798,7 @@ static int32_t mJoinInitPrimExprCtx(SNode* pNode, SMJoinPrimExprCtx* pCtx, SMJoi SValueNode* pTimeZone = (5 == pFunc->pParameterList->length) ? (SValueNode*)nodesListGetNode(pFunc->pParameterList, 4) : (SValueNode*)nodesListGetNode(pFunc->pParameterList, 3); pCtx->truncateUnit = pUnit->typeData; - if (NULL != pCurrTz && 0 == pCurrTz->typeData) { + if ((NULL == pCurrTz || 1 == pCurrTz->typeData) && pCtx->truncateUnit >= (86400 * TSDB_TICK_PER_SECOND(pFunc->node.resType.precision))) { pCtx->timezoneUnit = offsetFromTz(pTimeZone->datum.p, TSDB_TICK_PER_SECOND(pFunc->node.resType.precision)); } @@ -905,7 +905,7 @@ int32_t mJoinLaunchPrimExpr(SSDataBlock* pBlock, SMJoinTableCtx* pTable) { SColumnInfoData* pPrimOut = taosArrayGet(pBlock->pDataBlock, pTable->primCtx.targetSlotId); if (0 != pCtx->timezoneUnit) { for (int32_t i = 0; i < pBlock->info.rows; ++i) { - ((int64_t*)pPrimOut->pData)[i] = (((int64_t*)pPrimIn->pData)[i] - pCtx->timezoneUnit) / pCtx->truncateUnit * pCtx->truncateUnit; + ((int64_t*)pPrimOut->pData)[i] = ((int64_t*)pPrimIn->pData)[i] / pCtx->truncateUnit * pCtx->truncateUnit - pCtx->timezoneUnit; } } else { for (int32_t i = 0; i < pBlock->info.rows; ++i) { diff --git a/source/libs/executor/test/joinTests.cpp b/source/libs/executor/test/joinTests.cpp index 7f8b257b9f..bea0a54270 100755 --- a/source/libs/executor/test/joinTests.cpp +++ b/source/libs/executor/test/joinTests.cpp @@ -197,6 +197,7 @@ typedef struct { bool filter; bool asc; bool grpJoin; + bool timetruncate; SExecTaskInfo* pTask; } SJoinTestParam; diff --git a/tests/script/tsim/join/join.sim b/tests/script/tsim/join/join.sim index 289d386979..6bde57072f 100644 --- a/tests/script/tsim/join/join.sim +++ b/tests/script/tsim/join/join.sim @@ -23,17 +23,17 @@ sql drop database if exists testa sql create database testa vgroups 3; sql use testa; -sql create table sta1(ts timestamp, f int, g int) tags (t int); -sql insert into cta11 using sta1 tags(1) values('2023-10-16 09:10:11', 100111, 1001110); -sql insert into cta12 using sta1 tags(2) values('2023-10-16 09:10:12', 100112, 1001120); -sql insert into cta13 using sta1 tags(3) values('2023-10-16 09:10:13', 100113, 1001130); -sql insert into cta14 using sta1 tags(4) values('2023-10-16 09:10:14', 100114, 1001140); +sql create table sta1(ts timestamp, f int, g bigint) tags (t int); +sql insert into cta11 using sta1 tags(1) values('2023-10-16 09:10:11.001', 100111, 1001110); +sql insert into cta12 using sta1 tags(2) values('2023-10-16 09:10:12.001', 100112, 1001120); +sql insert into cta13 using sta1 tags(3) values('2023-10-16 09:10:13.001', 100113, 1001130); +sql insert into cta14 using sta1 tags(4) values('2023-10-16 09:10:14.001', 100114, 1001140); -sql create table st2(ts timestamp, f int, g int) tags (t int); -sql insert into cta21 using st2 tags(1) values('2023-10-16 09:10:11', 100221, 1002210); -sql insert into cta22 using st2 tags(2) values('2023-10-16 09:10:12', 100222, 1002220); -sql insert into cta23 using st2 tags(3) values('2023-10-16 09:10:13', 100223, 1002230); -sql insert into cta24 using st2 tags(4) values('2023-10-16 09:10:14', 100224, 1002240); +sql create table sta2(ts timestamp, f int, g bigint) tags (t int); +sql insert into cta21 using sta2 tags(1) values('2023-10-16 09:10:11.002', 100221, 1002210); +sql insert into cta22 using sta2 tags(2) values('2023-10-16 09:10:12.002', 100222, 1002220); +sql insert into cta23 using sta2 tags(3) values('2023-10-16 09:10:13.002', 100223, 1002230); +sql insert into cta24 using sta2 tags(4) values('2023-10-16 09:10:14.002', 100224, 1002240); sql create table stt(ts timestamp, f int, g int) tags (t int); sql create table tt using stt tags(99); diff --git a/tests/script/tsim/join/join_scalar.sim b/tests/script/tsim/join/join_scalar.sim index 001df82396..d8fb9c663e 100644 --- a/tests/script/tsim/join/join_scalar.sim +++ b/tests/script/tsim/join/join_scalar.sim @@ -77,5 +77,164 @@ sql_error select a.ts, b.col1 from sta a left join sta b on timetruncate(b.ts, 1 sql_error select a.ts, b.col1 from sta a left join sta b on timetruncate(b.ts, 1h) = a.ts + 1; sql_error select a.ts, b.col1 from sta a left join sta b on b.ts + 1 = a.ts + 1; +sql use testa; + +sql select sta1.ts, sta2.ts from sta1 full join sta2 on timetruncate(sta1.ts, 1a) = timetruncate(sta2.ts, 1a); +if $rows != 8 then + return -1 +endi +if $data00 != @23-10-16 09:10:11.001@ then + return -1 +endi +if $data01 != NULL then + return -1 +endi +if $data10 != NULL then + return -1 +endi +if $data11 != @23-10-16 09:10:11.002@ then + return -1 +endi + +sql select sta1.ts, sta2.ts from sta1 full join sta2 on timetruncate(sta1.ts, 1a) = timetruncate(sta2.ts, 1a) and sta1.ts=sta2.ts; +if $rows != 8 then + return -1 +endi + +sql select sta1.ts, sta2.ts from sta1 full join sta2 on timetruncate(sta1.ts, 1s) = timetruncate(sta2.ts, 1s); +if $rows != 4 then + return -1 +endi +sql select sta1.ts, sta2.ts from sta1 full join sta2 on timetruncate(sta1.ts, 1m) = timetruncate(sta2.ts, 1m); +if $rows != 16 then + return -1 +endi +sql select sta1.ts, sta2.ts from sta1 full join sta2 on timetruncate(sta1.ts, 1h) = timetruncate(sta2.ts, 1h) order by timetruncate(sta1.ts, 1s) desc, timetruncate(sta2.ts, 1s); +if $rows != 16 then + return -1 +endi +if $data00 != @23-10-16 09:10:14.001@ then + return -1 +endi +if $data01 != @23-10-16 09:10:11.002@ then + return -1 +endi + +sql select sta1.ts, sta2.ts from sta1 full join sta2 on timetruncate(sta1.ts, 1h) = timetruncate(sta2.ts, 1h, 0) order by timetruncate(sta1.ts, 1s) desc, timetruncate(sta2.ts, 1s); +if $rows != 16 then + return -1 +endi +if $data00 != @23-10-16 09:10:14.001@ then + return -1 +endi +if $data01 != @23-10-16 09:10:11.002@ then + return -1 +endi +sql select sta1.ts, sta2.ts from sta1 full join sta2 on timetruncate(sta1.ts, 1d) = timetruncate(sta2.ts, 1d, 0) order by timetruncate(sta1.ts, 1s) desc, timetruncate(sta2.ts, 1s); +if $rows != 8 then + return -1 +endi +if $data00 != @23-10-16 09:10:14.001@ then + return -1 +endi +if $data01 != NULL then + return -1 +endi +sql select sta1.ts,timetruncate(sta1.ts, 1d), sta2.ts, timetruncate(sta2.ts, 1d, 0) from sta1 left asof join sta2 on timetruncate(sta1.ts, 1d) >= timetruncate(sta2.ts, 1d, 0) order by timetruncate(sta1.ts, 1s) desc, timetruncate(sta2.ts, 1s); +if $rows != 4 then + return -1 +endi +if $data00 != @23-10-16 09:10:14.001@ then + return -1 +endi +if $data02 != NULL then + return -1 +endi +if $data10 != @23-10-16 09:10:13.001@ then + return -1 +endi +if $data12 != NULL then + return -1 +endi +sql select sta1.ts,timetruncate(sta1.ts, 1d, 0), sta2.ts, timetruncate(sta2.ts, 1d, 1) from sta1 left asof join sta2 on timetruncate(sta1.ts, 1d, 0) >= timetruncate(sta2.ts, 1d, 1) jlimit 4 order by timetruncate(sta1.ts, 1s) desc, timetruncate(sta2.ts, 1s); +if $rows != 16 then + return -1 +endi +if $data00 != @23-10-16 09:10:14.001@ then + return -1 +endi +if $data02 != @23-10-16 09:10:11.002@ then + return -1 +endi +if $data10 != @23-10-16 09:10:14.001@ then + return -1 +endi +if $data12 != @23-10-16 09:10:12.002@ then + return -1 +endi +sql select sta1.ts, sta2.ts from sta1 left asof join sta2 on timetruncate(sta1.ts, 1s, 0) > timetruncate(sta2.ts, 1s, 1) jlimit 4 order by timetruncate(sta1.ts, 1s) desc, timetruncate(sta2.ts, 1s); +if $rows != 7 then + return -1 +endi +if $data00 != @23-10-16 09:10:14.001@ then + return -1 +endi +if $data01 != @23-10-16 09:10:11.002@ then + return -1 +endi +if $data10 != @23-10-16 09:10:14.001@ then + return -1 +endi +if $data11 != @23-10-16 09:10:12.002@ then + return -1 +endi +if $data20 != @23-10-16 09:10:14.001@ then + return -1 +endi +if $data21 != @23-10-16 09:10:13.002@ then + return -1 +endi +if $data30 != @23-10-16 09:10:13.001@ then + return -1 +endi +if $data31 != @23-10-16 09:10:11.002@ then + return -1 +endi +if $data40 != @23-10-16 09:10:13.001@ then + return -1 +endi +if $data41 != @23-10-16 09:10:12.002@ then + return -1 +endi +if $data50 != @23-10-16 09:10:12.001@ then + return -1 +endi +if $data51 != @23-10-16 09:10:11.002@ then + return -1 +endi +if $data60 != @23-10-16 09:10:11.001@ then + return -1 +endi +if $data61 != NULL then + return -1 +endi + +sql select sta1.ts,timetruncate(sta1.ts, 1w, 0), sta2.ts,timetruncate(sta2.ts, 1w, 1) from sta1 left asof join sta2 on timetruncate(sta1.ts, 1w, 0) > timetruncate(sta2.ts, 1w, 1) jlimit 4 order by timetruncate(sta1.ts, 1s) desc, timetruncate(sta2.ts, 1s); +if $rows != 16 then + return -1 +endi +if $data00 != @23-10-16 09:10:14.001@ then + return -1 +endi +if $data02 != @23-10-16 09:10:11.002@ then + return -1 +endi +if $data10 != @23-10-16 09:10:14.001@ then + return -1 +endi +if $data12 != @23-10-16 09:10:12.002@ then + return -1 +endi +sql_error select count(*) from sta1 left window join sta2 on timetruncate(sta1.ts, 1h) = timetruncate(sta2.ts, 1h) window_offset(-1s, 1s);