From df58a9bb30341843081ad3cc3a4072423665eaee Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 20 Jul 2022 15:30:52 +0800 Subject: [PATCH] fix(query): handle fraction of ts in add ts offset. --- source/common/src/ttime.c | 6 ++++-- source/libs/executor/inc/executorimpl.h | 2 +- source/libs/executor/src/executil.c | 4 ++-- source/libs/executor/src/executorimpl.c | 17 +++++++++-------- source/libs/executor/src/scanoperator.c | 4 ++-- tests/script/tsim/query/interval-offset.sim | 1 + 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/source/common/src/ttime.c b/source/common/src/ttime.c index 9b044d6e93..8f150441f9 100644 --- a/source/common/src/ttime.c +++ b/source/common/src/ttime.c @@ -700,6 +700,8 @@ int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision) { numOfMonth *= 12; } + int64_t fraction = t % TSDB_TICK_PER_SECOND(precision); + struct tm tm; time_t tt = (time_t)(t / TSDB_TICK_PER_SECOND(precision)); taosLocalTime(&tt, &tm); @@ -707,7 +709,7 @@ int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision) { tm.tm_year = mon / 12; tm.tm_mon = mon % 12; - return (int64_t)(taosMktime(&tm) * TSDB_TICK_PER_SECOND(precision)); + return (int64_t)(taosMktime(&tm) * TSDB_TICK_PER_SECOND(precision) + fraction); } int64_t taosTimeSub(int64_t t, int64_t duration, char unit, int32_t precision) { @@ -851,7 +853,7 @@ int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precisio newEnd = taosTimeAdd(newEnd, -pInterval->sliding, pInterval->slidingUnit, precision); } - start = taosTimeAdd(end, -pInterval->interval + 1, pInterval->intervalUnit, precision); + start = taosTimeAdd(end, -pInterval->interval, pInterval->intervalUnit, precision) + 1; } } diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index 9142af4a6d..2cc4058b3b 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -797,7 +797,7 @@ void doApplyFunctions(SExecTaskInfo* taskInfo, SqlFunctionCtx* pCtx, STimeWin int32_t extractDataBlockFromFetchRsp(SSDataBlock* pRes, SLoadRemoteDataInfo* pLoadInfo, int32_t numOfRows, char* pData, int32_t compLen, int32_t numOfOutput, int64_t startTs, uint64_t* total, SArray* pColList); -void getAlignQueryTimeWindow(SInterval* pInterval, int32_t precision, int64_t key, STimeWindow* win); +STimeWindow getAlignQueryTimeWindow(SInterval* pInterval, int32_t precision, int64_t key); STimeWindow getFirstQualifiedTimeWindow(int64_t ts, STimeWindow* pWindow, SInterval* pInterval, int32_t order); int32_t getTableScanInfo(SOperatorInfo* pOperator, int32_t *order, int32_t* scanFlag); diff --git a/source/libs/executor/src/executil.c b/source/libs/executor/src/executil.c index 978bef1607..60799e0528 100644 --- a/source/libs/executor/src/executil.c +++ b/source/libs/executor/src/executil.c @@ -824,10 +824,10 @@ int32_t convertFillType(int32_t mode) { static void getInitialStartTimeWindow(SInterval* pInterval, TSKEY ts, STimeWindow* w, bool ascQuery) { if (ascQuery) { - getAlignQueryTimeWindow(pInterval, pInterval->precision, ts, w); + *w = getAlignQueryTimeWindow(pInterval, pInterval->precision, ts); } else { // the start position of the first time window in the endpoint that spreads beyond the queried last timestamp - getAlignQueryTimeWindow(pInterval, pInterval->precision, ts, w); + *w = getAlignQueryTimeWindow(pInterval, pInterval->precision, ts); int64_t key = w->skey; while (key < ts) { // moving towards end diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index df039bc0e2..44d1cc0965 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -834,18 +834,20 @@ bool isTaskKilled(SExecTaskInfo* pTaskInfo) { void setTaskKilled(SExecTaskInfo* pTaskInfo) { pTaskInfo->code = TSDB_CODE_TSC_QUERY_CANCELLED; } ///////////////////////////////////////////////////////////////////////////////////////////// -// todo refactor : return window -void getAlignQueryTimeWindow(SInterval* pInterval, int32_t precision, int64_t key, STimeWindow* win) { - win->skey = taosTimeTruncate(key, pInterval, precision); +STimeWindow getAlignQueryTimeWindow(SInterval* pInterval, int32_t precision, int64_t key) { + STimeWindow win = {0}; + win.skey = taosTimeTruncate(key, pInterval, precision); /* * if the realSkey > INT64_MAX - pInterval->interval, the query duration between * realSkey and realEkey must be less than one interval.Therefore, no need to adjust the query ranges. */ - win->ekey = taosTimeAdd(win->skey, pInterval->interval, pInterval->intervalUnit, precision) - 1; - if (win->ekey < win->skey) { - win->ekey = INT64_MAX; + win.ekey = taosTimeAdd(win.skey, pInterval->interval, pInterval->intervalUnit, precision) - 1; + if (win.ekey < win.skey) { + win.ekey = INT64_MAX; } + + return win; } #if 0 @@ -4042,8 +4044,7 @@ static int32_t initFillInfo(SFillOperatorInfo* pInfo, SExprInfo* pExpr, int32_t STimeWindow win, int32_t capacity, const char* id, SInterval* pInterval, int32_t fillType) { SFillColInfo* pColInfo = createFillColInfo(pExpr, numOfCols, pValNode); - STimeWindow w = TSWINDOW_INITIALIZER; - getAlignQueryTimeWindow(pInterval, pInterval->precision, win.skey, &w); + STimeWindow w = getAlignQueryTimeWindow(pInterval, pInterval->precision, win.skey); w = getFirstQualifiedTimeWindow(win.skey, &w, pInterval, TSDB_ORDER_ASC); int32_t order = TSDB_ORDER_ASC; diff --git a/source/libs/executor/src/scanoperator.c b/source/libs/executor/src/scanoperator.c index 1f77dfb093..ae5996c2fa 100644 --- a/source/libs/executor/src/scanoperator.c +++ b/source/libs/executor/src/scanoperator.c @@ -125,7 +125,7 @@ static bool overlapWithTimeWindow(SInterval* pInterval, SDataBlockInfo* pBlockIn } if (order == TSDB_ORDER_ASC) { - getAlignQueryTimeWindow(pInterval, pInterval->precision, pBlockInfo->window.skey, &w); + w = getAlignQueryTimeWindow(pInterval, pInterval->precision, pBlockInfo->window.skey); assert(w.ekey >= pBlockInfo->window.skey); if (w.ekey < pBlockInfo->window.ekey) { @@ -144,7 +144,7 @@ static bool overlapWithTimeWindow(SInterval* pInterval, SDataBlockInfo* pBlockIn } } } else { - getAlignQueryTimeWindow(pInterval, pInterval->precision, pBlockInfo->window.ekey, &w); + w = getAlignQueryTimeWindow(pInterval, pInterval->precision, pBlockInfo->window.ekey); assert(w.skey <= pBlockInfo->window.ekey); if (w.skey > pBlockInfo->window.skey) { diff --git a/tests/script/tsim/query/interval-offset.sim b/tests/script/tsim/query/interval-offset.sim index b6dffb5fe3..1399be7b53 100644 --- a/tests/script/tsim/query/interval-offset.sim +++ b/tests/script/tsim/query/interval-offset.sim @@ -185,6 +185,7 @@ print ===> rows1: $data10 $data11 $data12 $data13 $data14 print ===> rows2: $data20 $data21 $data22 $data23 $data24 print ===> rows3: $data30 $data31 $data32 $data33 $data34 if $rows != 4 then + print expect 4, actual: $rows return -1 endi if $data00 != @21-12-08 00:00:00.000@ then -- GitLab