提交 08854f86 编写于 作者: H hjxilinx

refactor codes of super table query in handling the sliding query.

上级 d6529943
...@@ -2162,7 +2162,7 @@ static STopBotInfo *getTopBotOutputInfo(SQLFunctionCtx *pCtx) { ...@@ -2162,7 +2162,7 @@ static STopBotInfo *getTopBotOutputInfo(SQLFunctionCtx *pCtx) {
// only the first_stage_merge is directly written data into final output buffer // only the first_stage_merge is directly written data into final output buffer
if (pResInfo->superTableQ && pCtx->currentStage != SECONDARY_STAGE_MERGE) { if (pResInfo->superTableQ && pCtx->currentStage != SECONDARY_STAGE_MERGE) {
return (STopBotInfo*) pCtx->aOutputBuf; return (STopBotInfo*) pCtx->aOutputBuf;
} else { // for normal table query and super table at the secondary_stage, result is written to intermediate buffer } else { // during normal table query and super table at the secondary_stage, result is written to intermediate buffer
return pResInfo->interResultBuf; return pResInfo->interResultBuf;
} }
} }
......
...@@ -175,8 +175,7 @@ void copyFromGroupBuf(SQInfo* pQInfo, SWindowResult* result); ...@@ -175,8 +175,7 @@ void copyFromGroupBuf(SQInfo* pQInfo, SWindowResult* result);
SBlockInfo getBlockBasicInfo(SQueryRuntimeEnv* pRuntimeEnv, void* pBlock, int32_t blockType); SBlockInfo getBlockBasicInfo(SQueryRuntimeEnv* pRuntimeEnv, void* pBlock, int32_t blockType);
SCacheBlock* getCacheDataBlock(SMeterObj* pMeterObj, SQueryRuntimeEnv* pRuntimeEnv, int32_t slot); SCacheBlock* getCacheDataBlock(SMeterObj* pMeterObj, SQueryRuntimeEnv* pRuntimeEnv, int32_t slot);
void queryOnBlock(SMeterQuerySupportObj* pSupporter, int64_t* primaryKeys, int32_t blockStatus, void queryOnBlock(SMeterQuerySupportObj* pSupporter, int32_t blockStatus, SBlockInfo* pBlockBasicInfo, SMeterDataInfo* pDataHeadInfoEx, SField* pFields,
SBlockInfo* pBlockBasicInfo, SMeterDataInfo* pDataHeadInfoEx, SField* pFields,
__block_search_fn_t searchFn); __block_search_fn_t searchFn);
int32_t vnodeFilterQualifiedMeters(SQInfo *pQInfo, int32_t vid, tSidSet *pSidSet, SMeterDataInfo *pMeterDataInfo, int32_t vnodeFilterQualifiedMeters(SQInfo *pQInfo, int32_t vid, tSidSet *pSidSet, SMeterDataInfo *pMeterDataInfo,
...@@ -278,14 +277,17 @@ void displayInterResult(SData** pdata, SQuery* pQuery, int32_t numOfRows); ...@@ -278,14 +277,17 @@ void displayInterResult(SData** pdata, SQuery* pQuery, int32_t numOfRows);
void vnodePrintQueryStatistics(SMeterQuerySupportObj* pSupporter); void vnodePrintQueryStatistics(SMeterQuerySupportObj* pSupporter);
void clearGroupResultBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *pOneOutputRes); void clearTimeWindowResBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *pOneOutputRes);
void copyGroupResultBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult* dst, const SWindowResult* src); void copyTimeWindowResBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult* dst, const SWindowResult* src);
void resetSlidingWindowInfo(SQueryRuntimeEnv *pRuntimeEnv, SWindowResInfo* pWindowResInfo); int32_t initWindowResInfo(SWindowResInfo *pWindowResInfo, SQueryRuntimeEnv *pRuntimeEnv, int32_t size, int32_t threshold, int16_t type);
void clearClosedSlidingWindows(SQueryRuntimeEnv* pRuntimeEnv);
int32_t numOfClosedSlidingWindow(SWindowResInfo* pWindowResInfo); void cleanupTimeWindowInfo(SWindowResInfo *pWindowResInfo, SQueryRuntimeEnv *pRuntimeEnv);
void closeSlidingWindow(SWindowResInfo* pWindowResInfo, int32_t slot); void resetTimeWindowInfo(SQueryRuntimeEnv *pRuntimeEnv, SWindowResInfo* pWindowResInfo);
void closeAllSlidingWindow(SWindowResInfo* pWindowResInfo); void clearClosedTimeWindow(SQueryRuntimeEnv* pRuntimeEnv);
int32_t numOfClosedTimeWindow(SWindowResInfo* pWindowResInfo);
void closeTimeWindow(SWindowResInfo* pWindowResInfo, int32_t slot);
void closeAllTimeWindow(SWindowResInfo* pWindowResInfo);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -197,7 +197,6 @@ typedef struct SMeterQueryInfo { ...@@ -197,7 +197,6 @@ typedef struct SMeterQueryInfo {
int16_t lastResRows; int16_t lastResRows;
int64_t tag; int64_t tag;
STSCursor cur; STSCursor cur;
SWindowResult* pWindowRes;
int32_t sid; // for retrieve the page id list int32_t sid; // for retrieve the page id list
SWindowResInfo windowResInfo; SWindowResInfo windowResInfo;
...@@ -279,7 +278,7 @@ typedef struct _qinfo { ...@@ -279,7 +278,7 @@ typedef struct _qinfo {
int (*fp)(SMeterObj*, SQuery*); int (*fp)(SMeterObj*, SQuery*);
} SQInfo; } SQInfo;
int32_t vnodeQuerySingleMeterPrepare(SQInfo* pQInfo, SMeterObj* pMeterObj, SMeterQuerySupportObj* pSMultiMeterObj, int32_t vnodeQuerySingleTablePrepare(SQInfo* pQInfo, SMeterObj* pMeterObj, SMeterQuerySupportObj* pSMultiMeterObj,
void* param); void* param);
void vnodeQueryFreeQInfoEx(SQInfo* pQInfo); void vnodeQueryFreeQInfoEx(SQInfo* pQInfo);
......
...@@ -65,7 +65,7 @@ static TSKEY getTimestampInDiskBlock(SQueryRuntimeEnv *pRuntimeEnv, int32_t inde ...@@ -65,7 +65,7 @@ static TSKEY getTimestampInDiskBlock(SQueryRuntimeEnv *pRuntimeEnv, int32_t inde
static void savePointPosition(SPositionInfo *position, int32_t fileId, int32_t slot, int32_t pos); static void savePointPosition(SPositionInfo *position, int32_t fileId, int32_t slot, int32_t pos);
static int32_t getNextDataFileCompInfo(SQueryRuntimeEnv *pRuntimeEnv, SMeterObj *pMeterObj, int32_t step); static int32_t getNextDataFileCompInfo(SQueryRuntimeEnv *pRuntimeEnv, SMeterObj *pMeterObj, int32_t step);
static void setGroupOutputBuffer(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *pResult); static void setWindowResOutputBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *pResult);
static void getAlignedIntervalQueryRange(SQueryRuntimeEnv *pRuntimeEnv, TSKEY keyInData, TSKEY skey, TSKEY ekey); static void getAlignedIntervalQueryRange(SQueryRuntimeEnv *pRuntimeEnv, TSKEY keyInData, TSKEY skey, TSKEY ekey);
...@@ -83,7 +83,7 @@ static TSKEY getQueryPositionForCacheInvalid(SQueryRuntimeEnv *pRuntimeEnv, __ ...@@ -83,7 +83,7 @@ static TSKEY getQueryPositionForCacheInvalid(SQueryRuntimeEnv *pRuntimeEnv, __
static bool functionNeedToExecute(SQueryRuntimeEnv *pRuntimeEnv, SQLFunctionCtx *pCtx, int32_t functionId); static bool functionNeedToExecute(SQueryRuntimeEnv *pRuntimeEnv, SQLFunctionCtx *pCtx, int32_t functionId);
void doGetAlignedIntervalQueryRangeImpl(SQuery *pQuery, int64_t pKey, int64_t keyFirst, int64_t keyLast, void doGetAlignedIntervalQueryRangeImpl(SQuery *pQuery, int64_t pKey, int64_t keyFirst, int64_t keyLast,
int64_t *actualSkey, int64_t *actualEkey, int64_t *skey, int64_t *ekey); int64_t *actualSkey, int64_t *actualEkey, int64_t *skey, int64_t *ekey);
static void getNextLogicalQueryRange(SQueryRuntimeEnv *pRuntimeEnv, STimeWindow *pTimeWindow); static void getNextTimeWindow(SQueryRuntimeEnv *pRuntimeEnv, STimeWindow *pTimeWindow);
// check the offset value integrity // check the offset value integrity
static FORCE_INLINE int32_t validateHeaderOffsetSegment(SQInfo *pQInfo, char *filePath, int32_t vid, char *data, static FORCE_INLINE int32_t validateHeaderOffsetSegment(SQInfo *pQInfo, char *filePath, int32_t vid, char *data,
...@@ -588,7 +588,7 @@ static void setExecParams(SQuery *pQuery, SQLFunctionCtx *pCtx, int64_t StartQue ...@@ -588,7 +588,7 @@ static void setExecParams(SQuery *pQuery, SQLFunctionCtx *pCtx, int64_t StartQue
void createQueryResultInfo(SQuery *pQuery, SWindowResult *pResultRow, bool isSTableQuery, SPosInfo *posInfo); void createQueryResultInfo(SQuery *pQuery, SWindowResult *pResultRow, bool isSTableQuery, SPosInfo *posInfo);
static void destroyGroupResultBuf(SWindowResult *pOneOutputRes, int32_t nOutputCols); static void destroyTimeWindowRes(SWindowResult *pOneOutputRes, int32_t nOutputCols);
static int32_t binarySearchForBlockImpl(SCompBlock *pBlock, int32_t numOfBlocks, TSKEY skey, int32_t order) { static int32_t binarySearchForBlockImpl(SCompBlock *pBlock, int32_t numOfBlocks, TSKEY skey, int32_t order) {
int32_t firstSlot = 0; int32_t firstSlot = 0;
...@@ -1432,17 +1432,17 @@ static char *getDataBlocks(SQueryRuntimeEnv *pRuntimeEnv, SArithmeticSupport *sa ...@@ -1432,17 +1432,17 @@ static char *getDataBlocks(SQueryRuntimeEnv *pRuntimeEnv, SArithmeticSupport *sa
return dataBlock; return dataBlock;
} }
static bool slidingWindowClosed(SWindowResInfo *pWindowResInfo, int32_t slot) { static bool isWindowResClosed(SWindowResInfo *pWindowResInfo, int32_t slot) {
return (pWindowResInfo->pResult[slot].status.closed == true); return (pWindowResInfo->pResult[slot].status.closed == true);
} }
static int32_t curSlidingWindow(SWindowResInfo *pWindowResInfo) { static int32_t curTimeWindow(SWindowResInfo *pWindowResInfo) {
assert(pWindowResInfo->curIndex >= 0 && pWindowResInfo->curIndex < pWindowResInfo->size); assert(pWindowResInfo->curIndex >= 0 && pWindowResInfo->curIndex < pWindowResInfo->size);
return pWindowResInfo->curIndex; return pWindowResInfo->curIndex;
} }
static SWindowResult *doSetSlidingWindowFromKey(SQueryRuntimeEnv *pRuntimeEnv, SWindowResInfo *pWindowResInfo, static SWindowResult *doSetTimeWindowFromKey(SQueryRuntimeEnv *pRuntimeEnv, SWindowResInfo *pWindowResInfo, char *pData,
char *pData, int16_t bytes) { int16_t bytes) {
SQuery *pQuery = pRuntimeEnv->pQuery; SQuery *pQuery = pRuntimeEnv->pQuery;
int32_t *p1 = (int32_t *)taosGetDataFromHashTable(pWindowResInfo->hashList, pData, bytes); int32_t *p1 = (int32_t *)taosGetDataFromHashTable(pWindowResInfo->hashList, pData, bytes);
...@@ -1476,31 +1476,28 @@ static SWindowResult *doSetSlidingWindowFromKey(SQueryRuntimeEnv *pRuntimeEnv, S ...@@ -1476,31 +1476,28 @@ static SWindowResult *doSetSlidingWindowFromKey(SQueryRuntimeEnv *pRuntimeEnv, S
return &pWindowResInfo->pResult[pWindowResInfo->curIndex]; return &pWindowResInfo->pResult[pWindowResInfo->curIndex];
} }
// get the correct sliding window according to the handled timestamp // get the correct time window according to the handled timestamp
static STimeWindow getActiveSlidingWindow(SWindowResInfo *pWindowResInfo, int64_t ts, SQuery *pQuery) { static STimeWindow getActiveTimeWindow(SWindowResInfo *pWindowResInfo, int64_t ts, SQuery *pQuery) {
STimeWindow w = {0}; STimeWindow w = {0};
if (pWindowResInfo->curIndex == -1) { // the first window, from the prevous stored value if (pWindowResInfo->curIndex == -1) { // the first window, from the previous stored value
w.skey = pWindowResInfo->prevSKey; w.skey = pWindowResInfo->prevSKey;
w.ekey = w.skey + pQuery->intervalTime - 1; w.ekey = w.skey + pQuery->intervalTime - 1;
} else { } else {
int32_t slot = curSlidingWindow(pWindowResInfo); int32_t slot = curTimeWindow(pWindowResInfo);
w = pWindowResInfo->pResult[slot].window; w = pWindowResInfo->pResult[slot].window;
} }
// STimeWindow *window = &pWindowResInfo->pResult[slot].window;
if (w.skey > ts || w.ekey < ts) { if (w.skey > ts || w.ekey < ts) {
// if (w.skey <= ts && w.ekey >= ts) {
// w = *window; // belongs to current active window
// } else {
int64_t st = w.skey; int64_t st = w.skey;
while (st > ts) { if (st > ts) {
st -= pQuery->slidingTime; st -= ((st - ts + pQuery->slidingTime - 1)/pQuery->slidingTime) * pQuery->slidingTime;
} }
while ((st + pQuery->intervalTime - 1) < ts) { int64_t et = st + pQuery->intervalTime - 1;
st += pQuery->slidingTime; if (et < ts) {
st += ((ts - et + pQuery->slidingTime - 1)/pQuery->slidingTime) * pQuery->slidingTime;
} }
w.skey = st; w.skey = st;
...@@ -1555,7 +1552,7 @@ static int32_t setWindowOutputBufByKey(SQueryRuntimeEnv *pRuntimeEnv, SWindowRes ...@@ -1555,7 +1552,7 @@ static int32_t setWindowOutputBufByKey(SQueryRuntimeEnv *pRuntimeEnv, SWindowRes
assert(win->skey < win->ekey); assert(win->skey < win->ekey);
SQueryDiskbasedResultBuf *pResultBuf = pRuntimeEnv->pResultBuf; SQueryDiskbasedResultBuf *pResultBuf = pRuntimeEnv->pResultBuf;
SWindowResult *pWindowRes = doSetSlidingWindowFromKey(pRuntimeEnv, pWindowResInfo, (char *)&win->skey, TSDB_KEYSIZE); SWindowResult *pWindowRes = doSetTimeWindowFromKey(pRuntimeEnv, pWindowResInfo, (char *)&win->skey, TSDB_KEYSIZE);
if (pWindowRes == NULL) { if (pWindowRes == NULL) {
return -1; return -1;
} }
...@@ -1571,13 +1568,13 @@ static int32_t setWindowOutputBufByKey(SQueryRuntimeEnv *pRuntimeEnv, SWindowRes ...@@ -1571,13 +1568,13 @@ static int32_t setWindowOutputBufByKey(SQueryRuntimeEnv *pRuntimeEnv, SWindowRes
// set time window for current result // set time window for current result
pWindowRes->window = *win; pWindowRes->window = *win;
setGroupOutputBuffer(pRuntimeEnv, pWindowRes); setWindowResOutputBuf(pRuntimeEnv, pWindowRes);
initCtxOutputBuf(pRuntimeEnv); initCtxOutputBuf(pRuntimeEnv);
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
static SWindowStatus *getSlidingWindowStatus(SWindowResInfo *pWindowResInfo, int32_t slot) { static SWindowStatus *getTimeWindowResStatus(SWindowResInfo *pWindowResInfo, int32_t slot) {
assert(slot >= 0 && slot < pWindowResInfo->size); assert(slot >= 0 && slot < pWindowResInfo->size);
return &pWindowResInfo->pResult[slot].status; return &pWindowResInfo->pResult[slot].status;
} }
...@@ -1606,11 +1603,11 @@ static void doCheckQueryCompleted(SQueryRuntimeEnv *pRuntimeEnv, TSKEY lastKey, ...@@ -1606,11 +1603,11 @@ static void doCheckQueryCompleted(SQueryRuntimeEnv *pRuntimeEnv, TSKEY lastKey,
if (pQuery->slidingTime > 0 && pQuery->intervalTime > 0 && IS_MASTER_SCAN(pRuntimeEnv)) { // query completed if (pQuery->slidingTime > 0 && pQuery->intervalTime > 0 && IS_MASTER_SCAN(pRuntimeEnv)) { // query completed
if ((lastKey >= pQuery->ekey && QUERY_IS_ASC_QUERY(pQuery)) || if ((lastKey >= pQuery->ekey && QUERY_IS_ASC_QUERY(pQuery)) ||
(lastKey <= pQuery->ekey && !QUERY_IS_ASC_QUERY(pQuery))) { (lastKey <= pQuery->ekey && !QUERY_IS_ASC_QUERY(pQuery))) {
closeAllSlidingWindow(pWindowResInfo); closeAllTimeWindow(pWindowResInfo);
pWindowResInfo->curIndex = pWindowResInfo->size - 1; pWindowResInfo->curIndex = pWindowResInfo->size - 1;
setQueryStatus(pQuery, QUERY_COMPLETED | QUERY_RESBUF_FULL); setQueryStatus(pQuery, QUERY_COMPLETED | QUERY_RESBUF_FULL);
} else { } else { // set the current index to be the last unclosed window
int32_t i = 0; int32_t i = 0;
int64_t skey = 0; int64_t skey = 0;
...@@ -1622,7 +1619,7 @@ static void doCheckQueryCompleted(SQueryRuntimeEnv *pRuntimeEnv, TSKEY lastKey, ...@@ -1622,7 +1619,7 @@ static void doCheckQueryCompleted(SQueryRuntimeEnv *pRuntimeEnv, TSKEY lastKey,
if ((pResult->window.ekey <= lastKey && QUERY_IS_ASC_QUERY(pQuery)) || if ((pResult->window.ekey <= lastKey && QUERY_IS_ASC_QUERY(pQuery)) ||
(pResult->window.skey >= lastKey && !QUERY_IS_ASC_QUERY(pQuery))) { (pResult->window.skey >= lastKey && !QUERY_IS_ASC_QUERY(pQuery))) {
closeSlidingWindow(pWindowResInfo, i); closeTimeWindow(pWindowResInfo, i);
} else { } else {
skey = pResult->window.skey; skey = pResult->window.skey;
break; break;
...@@ -1631,24 +1628,109 @@ static void doCheckQueryCompleted(SQueryRuntimeEnv *pRuntimeEnv, TSKEY lastKey, ...@@ -1631,24 +1628,109 @@ static void doCheckQueryCompleted(SQueryRuntimeEnv *pRuntimeEnv, TSKEY lastKey,
// all windows are closed, set the last one to be the skey // all windows are closed, set the last one to be the skey
if (skey == 0) { if (skey == 0) {
skey = pWindowResInfo->pResult[pWindowResInfo->size-1].window.skey; assert(i == pWindowResInfo->size);
pWindowResInfo->curIndex = pWindowResInfo->size - 1;
} else {
pWindowResInfo->curIndex = i;
} }
pWindowResInfo->prevSKey = skey; pWindowResInfo->prevSKey = pWindowResInfo->pResult[pWindowResInfo->curIndex].window.skey;
// the number of completed slots are larger than the threshold, dump to client immediately. // the number of completed slots are larger than the threshold, dump to client immediately.
int32_t v = numOfClosedSlidingWindow(pWindowResInfo); int32_t n = numOfClosedTimeWindow(pWindowResInfo);
if (v > pWindowResInfo->threshold) { if (n > pWindowResInfo->threshold) {
setQueryStatus(pQuery, QUERY_RESBUF_FULL); setQueryStatus(pQuery, QUERY_RESBUF_FULL);
} }
dTrace("QInfo:%p total window:%d, closed:%d", GET_QINFO_ADDR(pQuery), pWindowResInfo->size, v); dTrace("QInfo:%p total window:%d, closed:%d", GET_QINFO_ADDR(pQuery), pWindowResInfo->size, n);
} }
assert(pWindowResInfo->prevSKey != 0); assert(pWindowResInfo->prevSKey != 0);
} }
} }
static int32_t getNumOfRowsInTimeWindow(SQuery *pQuery, SBlockInfo *pBlockInfo, TSKEY *pPrimaryColumn, int32_t startPos,
TSKEY ekey, __block_search_fn_t searchFn, bool updateLastKey) {
assert(startPos >= 0 && startPos < pBlockInfo->size);
int32_t forwardStep = -1;
int32_t order = pQuery->order.order;
int32_t step = GET_FORWARD_DIRECTION_FACTOR(order);
if (QUERY_IS_ASC_QUERY(pQuery)) {
if (ekey < pBlockInfo->keyLast) {
forwardStep = getForwardStepsInBlock(pBlockInfo->size, searchFn, ekey, startPos, order, pPrimaryColumn);
if (forwardStep == 0) { // no qualified data in current block, do not update the lastKey value
assert(ekey < pPrimaryColumn[startPos]);
} else {
if (updateLastKey) {
pQuery->lastKey = MAX(ekey, pPrimaryColumn[startPos + (forwardStep - 1)]) + step;
}
}
} else {
forwardStep = pBlockInfo->size - startPos;
if (updateLastKey) {
pQuery->lastKey = pBlockInfo->keyLast + step;
}
}
} else { // desc
if (ekey > pBlockInfo->keyFirst) {
forwardStep = getForwardStepsInBlock(pBlockInfo->size, searchFn, ekey, startPos, order, pPrimaryColumn);
if (forwardStep == 0) { // no qualified data in current block, do not update the lastKey value
assert(ekey > pPrimaryColumn[startPos]);
} else {
if (updateLastKey) {
pQuery->lastKey = MIN(ekey, pPrimaryColumn[startPos - (forwardStep - 1)]) + step;
}
}
} else {
forwardStep = startPos + 1;
if (updateLastKey) {
pQuery->lastKey = pBlockInfo->keyFirst + step;
}
}
}
assert(forwardStep >= 0);
return forwardStep;
}
static void doBlockwiseApplyFunctions(SQueryRuntimeEnv *pRuntimeEnv, SWindowStatus *pStatus, STimeWindow *pWin,
int32_t startPos, int32_t forwardStep) {
SQuery * pQuery = pRuntimeEnv->pQuery;
SQLFunctionCtx *pCtx = pRuntimeEnv->pCtx;
if (IS_MASTER_SCAN(pRuntimeEnv) || pStatus->closed) {
for (int32_t k = 0; k < pQuery->numOfOutputCols; ++k) {
pCtx[k].nStartQueryTimestamp = pWin->skey;
pCtx[k].size = forwardStep;
pCtx[k].startOffset = (QUERY_IS_ASC_QUERY(pQuery)) ? startPos : startPos - (forwardStep - 1);
int32_t functionId = pQuery->pSelectExpr[k].pBase.functionId;
if (functionNeedToExecute(pRuntimeEnv, &pCtx[k], functionId)) {
aAggs[functionId].xFunction(&pCtx[k]);
}
}
}
}
static void doRowwiseApplyFunctions(SQueryRuntimeEnv *pRuntimeEnv, SWindowStatus *pStatus, STimeWindow *pWin) {
SQuery * pQuery = pRuntimeEnv->pQuery;
SQLFunctionCtx *pCtx = pRuntimeEnv->pCtx;
if (IS_MASTER_SCAN(pRuntimeEnv) || pStatus->closed) {
for (int32_t k = 0; k < pQuery->numOfOutputCols; ++k) {
pCtx[k].nStartQueryTimestamp = pWin->skey;
int32_t functionId = pQuery->pSelectExpr[k].pBase.functionId;
if (functionNeedToExecute(pRuntimeEnv, &pCtx[k], functionId)) {
aAggs[functionId].xFunction(&pCtx[k]);
}
}
}
}
/** /**
* *
* @param pRuntimeEnv * @param pRuntimeEnv
...@@ -1659,11 +1741,12 @@ static void doCheckQueryCompleted(SQueryRuntimeEnv *pRuntimeEnv, TSKEY lastKey, ...@@ -1659,11 +1741,12 @@ static void doCheckQueryCompleted(SQueryRuntimeEnv *pRuntimeEnv, TSKEY lastKey,
* @return the incremental number of output value, so it maybe 0 for fixed number of query, * @return the incremental number of output value, so it maybe 0 for fixed number of query,
* such as count/min/max etc. * such as count/min/max etc.
*/ */
static int32_t blockwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t forwardStep, TSKEY *primaryKeyCol, static int32_t blockwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t forwardStep, SField *pFields,
SField *pFields, SBlockInfo *pBlockInfo, SWindowResInfo *pWindowResInfo, SBlockInfo *pBlockInfo, SWindowResInfo *pWindowResInfo,
__block_search_fn_t searchFn) { __block_search_fn_t searchFn) {
SQLFunctionCtx *pCtx = pRuntimeEnv->pCtx; SQLFunctionCtx *pCtx = pRuntimeEnv->pCtx;
SQuery * pQuery = pRuntimeEnv->pQuery; SQuery * pQuery = pRuntimeEnv->pQuery;
TSKEY * primaryKeyCol = (TSKEY *)pRuntimeEnv->primaryColBuffer->data;
bool isDiskFileBlock = IS_FILE_BLOCK(pRuntimeEnv->blockStatus); bool isDiskFileBlock = IS_FILE_BLOCK(pRuntimeEnv->blockStatus);
int64_t prevNumOfRes = getNumOfResult(pRuntimeEnv); int64_t prevNumOfRes = getNumOfResult(pRuntimeEnv);
...@@ -1702,50 +1785,25 @@ static int32_t blockwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t ...@@ -1702,50 +1785,25 @@ static int32_t blockwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t
int32_t offset = GET_COL_DATA_POS(pQuery, 0, step); int32_t offset = GET_COL_DATA_POS(pQuery, 0, step);
TSKEY ts = primaryKeyCol[offset]; TSKEY ts = primaryKeyCol[offset];
STimeWindow win = getActiveSlidingWindow(pWindowResInfo, ts, pQuery); STimeWindow win = getActiveTimeWindow(pWindowResInfo, ts, pQuery);
if (setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, pRuntimeEnv->pMeterObj->sid, &win) != TSDB_CODE_SUCCESS) { if (setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, pRuntimeEnv->pMeterObj->sid, &win) != TSDB_CODE_SUCCESS) {
return 0; return 0;
} }
if (QUERY_IS_ASC_QUERY(pQuery)) { //todo refactor TSKEY ekey = QUERY_IS_ASC_QUERY(pQuery) ? win.ekey : win.skey;
if (win.ekey < pBlockInfo->keyLast) { forwardStep = getNumOfRowsInTimeWindow(pQuery, pBlockInfo, primaryKeyCol, pQuery->pos, ekey, searchFn, false);
forwardStep =
getForwardStepsInBlock(pBlockInfo->size, searchFn, win.ekey, pQuery->pos, pQuery->order.order, primaryKeyCol);
} else {
forwardStep = pBlockInfo->size - pQuery->pos;
}
} else {
if (win.skey > pBlockInfo->keyFirst) {
forwardStep = getForwardStepsInBlock(pBlockInfo->size, searchFn, win.skey, pQuery->pos, pQuery->order.order, primaryKeyCol);
} else {
forwardStep = pQuery->pos + 1;
}
}
SWindowStatus* pStatus = getSlidingWindowStatus(pWindowResInfo, curSlidingWindow(pWindowResInfo)); SWindowStatus *pStatus = getTimeWindowResStatus(pWindowResInfo, curTimeWindow(pWindowResInfo));
doBlockwiseApplyFunctions(pRuntimeEnv, pStatus, &win, pQuery->pos, forwardStep);
if (IS_MASTER_SCAN(pRuntimeEnv) || pStatus->closed) {
for (int32_t k = 0; k < pQuery->numOfOutputCols; ++k) {
pCtx[k].nStartQueryTimestamp = win.skey;
pCtx[k].size = forwardStep;
pCtx[k].startOffset = (QUERY_IS_ASC_QUERY(pQuery)) ? pQuery->pos : pQuery->pos - (forwardStep - 1);
int32_t functionId = pQuery->pSelectExpr[k].pBase.functionId;
if (functionNeedToExecute(pRuntimeEnv, &pCtx[k], functionId)) {
aAggs[functionId].xFunction(&pCtx[k]);
}
}
}
int32_t index = pWindowResInfo->curIndex; int32_t index = pWindowResInfo->curIndex;
STimeWindow nextWin = win; STimeWindow nextWin = win;
while (1) { while (1) {
getNextLogicalQueryRange(pRuntimeEnv, &nextWin); getNextTimeWindow(pRuntimeEnv, &nextWin);
if (pWindowResInfo->startTime > nextWin.skey || (nextWin.skey > pQuery->ekey && QUERY_IS_ASC_QUERY(pQuery)) || if (pWindowResInfo->startTime > nextWin.skey || (nextWin.skey > pQuery->ekey && QUERY_IS_ASC_QUERY(pQuery)) ||
(nextWin.ekey < pQuery->ekey && !QUERY_IS_ASC_QUERY(pQuery))) { (nextWin.ekey < pQuery->ekey && !QUERY_IS_ASC_QUERY(pQuery))) {
pWindowResInfo->curIndex = index;
break; break;
} }
...@@ -1755,13 +1813,8 @@ static int32_t blockwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t ...@@ -1755,13 +1813,8 @@ static int32_t blockwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t
break; break;
} }
// if (pBlockInfo->keyLast >= nextWin.skey && pBlockInfo->keyFirst <= nextWin.ekey) { TSKEY startKey = QUERY_IS_ASC_QUERY(pQuery) ? nextWin.skey : nextWin.ekey;
int32_t startPos = -1; int32_t startPos = searchFn((char *)primaryKeyCol, pBlockInfo->size, startKey, pQuery->order.order);
if (QUERY_IS_ASC_QUERY(pQuery)) {
startPos = searchFn((char *)primaryKeyCol, pBlockInfo->size, nextWin.skey, TSQL_SO_ASC);
} else {
startPos = searchFn((char *)primaryKeyCol, pBlockInfo->size, nextWin.ekey, TSQL_SO_DESC);
}
/* /*
* This time window does not cover any data, try next time window * This time window does not cover any data, try next time window
...@@ -1773,44 +1826,16 @@ static int32_t blockwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t ...@@ -1773,44 +1826,16 @@ static int32_t blockwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t
} }
// null data, failed to allocate more memory buffer // null data, failed to allocate more memory buffer
if (setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, pRuntimeEnv->pMeterObj->sid, &nextWin) != int32_t sid = pRuntimeEnv->pMeterObj->sid;
TSDB_CODE_SUCCESS) { if (setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, sid, &nextWin) != TSDB_CODE_SUCCESS) {
pRuntimeEnv->windowResInfo.curIndex = index;
break; break;
} }
if (QUERY_IS_ASC_QUERY(pQuery)) { //todo refactor ekey = QUERY_IS_ASC_QUERY(pQuery) ? nextWin.ekey : nextWin.skey;
if (nextWin.ekey < pBlockInfo->keyLast) { forwardStep = getNumOfRowsInTimeWindow(pQuery, pBlockInfo, primaryKeyCol, startPos, ekey, searchFn, false);
forwardStep = getForwardStepsInBlock(pBlockInfo->size, searchFn, nextWin.ekey, startPos, pQuery->order.order, primaryKeyCol);
} else {
forwardStep = pBlockInfo->size - startPos;
}
} else {
if (nextWin.skey > pBlockInfo->keyFirst) {
forwardStep = getForwardStepsInBlock(pBlockInfo->size, searchFn, nextWin.skey, startPos, pQuery->order.order, primaryKeyCol);
} else {
forwardStep = startPos + 1;
}
}
pStatus = getSlidingWindowStatus(pWindowResInfo, curSlidingWindow(pWindowResInfo));
if (IS_MASTER_SCAN(pRuntimeEnv) || pStatus->closed) {
for (int32_t k = 0; k < pQuery->numOfOutputCols; ++k) {
pCtx[k].nStartQueryTimestamp = nextWin.skey;
pCtx[k].size = forwardStep;
pCtx[k].startOffset = (QUERY_IS_ASC_QUERY(pQuery))? startPos : startPos - (forwardStep - 1);
int32_t functionId = pQuery->pSelectExpr[k].pBase.functionId; pStatus = getTimeWindowResStatus(pWindowResInfo, curTimeWindow(pWindowResInfo));
if (functionNeedToExecute(pRuntimeEnv, &pCtx[k], functionId)) { doBlockwiseApplyFunctions(pRuntimeEnv, pStatus, &nextWin, startPos, forwardStep);
aAggs[functionId].xFunction(&pCtx[k]);
}
}
}
// } else {
// pWindowResInfo->curIndex = index;
// break;
// }
} }
pWindowResInfo->curIndex = index; pWindowResInfo->curIndex = index;
...@@ -1828,9 +1853,6 @@ static int32_t blockwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t ...@@ -1828,9 +1853,6 @@ static int32_t blockwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t
} }
} }
TSKEY lastKey = (QUERY_IS_ASC_QUERY(pQuery)) ? pBlockInfo->keyLast : pBlockInfo->keyFirst;
doCheckQueryCompleted(pRuntimeEnv, lastKey, pWindowResInfo);
/* /*
* No need to calculate the number of output results for group-by normal columns, interval query * No need to calculate the number of output results for group-by normal columns, interval query
* because the results of group by normal column is put into intermediate buffer. * because the results of group by normal column is put into intermediate buffer.
...@@ -1901,19 +1923,24 @@ static bool needToLoadDataBlock(SQuery *pQuery, SField *pField, SQLFunctionCtx * ...@@ -1901,19 +1923,24 @@ static bool needToLoadDataBlock(SQuery *pQuery, SField *pField, SQLFunctionCtx *
} }
} }
for (int32_t i = 0; i < pQuery->numOfOutputCols; ++i) { // todo disable this opt code block temporarily
int32_t functId = pQuery->pSelectExpr[i].pBase.functionId; // for (int32_t i = 0; i < pQuery->numOfOutputCols; ++i) {
if (functId == TSDB_FUNC_TOP || functId == TSDB_FUNC_BOTTOM) { // int32_t functId = pQuery->pSelectExpr[i].pBase.functionId;
return top_bot_datablock_filter(&pCtx[i], functId, (char *)&pField[i].min, (char *)&pField[i].max); // if (functId == TSDB_FUNC_TOP || functId == TSDB_FUNC_BOTTOM) {
} // return top_bot_datablock_filter(&pCtx[i], functId, (char *)&pField[i].min, (char *)&pField[i].max);
} // }
// }
return true; return true;
} }
static int32_t initSlidingWindowInfo(SQueryRuntimeEnv *pRuntimeEnv, SWindowResInfo *pWindowResInfo, int32_t threshold, int32_t initWindowResInfo(SWindowResInfo *pWindowResInfo, SQueryRuntimeEnv *pRuntimeEnv, int32_t size,
int16_t type) { int32_t threshold, int16_t type) {
pWindowResInfo->capacity = threshold; if (size < threshold) {
size = threshold;
}
pWindowResInfo->capacity = size;
pWindowResInfo->threshold = threshold; pWindowResInfo->threshold = threshold;
pWindowResInfo->type = type; pWindowResInfo->type = type;
...@@ -1934,23 +1961,29 @@ static int32_t initSlidingWindowInfo(SQueryRuntimeEnv *pRuntimeEnv, SWindowResIn ...@@ -1934,23 +1961,29 @@ static int32_t initSlidingWindowInfo(SQueryRuntimeEnv *pRuntimeEnv, SWindowResIn
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
static void destroySlidingWindowInfo(SWindowResInfo *pWindowResInfo) { void cleanupTimeWindowInfo(SWindowResInfo *pWindowResInfo, SQueryRuntimeEnv *pRuntimeEnv) {
if (pWindowResInfo == NULL || pWindowResInfo->capacity == 0) { if (pWindowResInfo == NULL || pWindowResInfo->capacity == 0) {
assert(pWindowResInfo->hashList == NULL && pWindowResInfo->pResult == NULL); assert(pWindowResInfo->hashList == NULL && pWindowResInfo->pResult == NULL);
return; return;
} }
for (int32_t i = 0; i < pWindowResInfo->size; ++i) {
SWindowResult *pResult = &pWindowResInfo->pResult[i];
destroyTimeWindowRes(pResult, pRuntimeEnv->pQuery->numOfOutputCols);
}
taosCleanUpHashTable(pWindowResInfo->hashList); taosCleanUpHashTable(pWindowResInfo->hashList);
tfree(pWindowResInfo->pResult);
} }
void resetSlidingWindowInfo(SQueryRuntimeEnv *pRuntimeEnv, SWindowResInfo *pWindowResInfo) { void resetTimeWindowInfo(SQueryRuntimeEnv *pRuntimeEnv, SWindowResInfo *pWindowResInfo) {
if (pWindowResInfo == NULL || pWindowResInfo->capacity == 0) { if (pWindowResInfo == NULL || pWindowResInfo->capacity == 0) {
return; return;
} }
for (int32_t i = 0; i < pWindowResInfo->size; ++i) { for (int32_t i = 0; i < pWindowResInfo->size; ++i) {
SWindowResult *pWindowRes = &pWindowResInfo->pResult[i]; SWindowResult *pWindowRes = &pWindowResInfo->pResult[i];
clearGroupResultBuf(pRuntimeEnv, pWindowRes); clearTimeWindowResBuf(pRuntimeEnv, pWindowRes);
} }
pWindowResInfo->curIndex = -1; pWindowResInfo->curIndex = -1;
...@@ -1964,7 +1997,7 @@ void resetSlidingWindowInfo(SQueryRuntimeEnv *pRuntimeEnv, SWindowResInfo *pWind ...@@ -1964,7 +1997,7 @@ void resetSlidingWindowInfo(SQueryRuntimeEnv *pRuntimeEnv, SWindowResInfo *pWind
pWindowResInfo->prevSKey = 0; pWindowResInfo->prevSKey = 0;
} }
void clearClosedSlidingWindows(SQueryRuntimeEnv *pRuntimeEnv) { void clearClosedTimeWindow(SQueryRuntimeEnv *pRuntimeEnv) {
SWindowResInfo *pWindowResInfo = &pRuntimeEnv->windowResInfo; SWindowResInfo *pWindowResInfo = &pRuntimeEnv->windowResInfo;
if (pWindowResInfo == NULL || pWindowResInfo->capacity == 0 || pWindowResInfo->size == 0) { if (pWindowResInfo == NULL || pWindowResInfo->capacity == 0 || pWindowResInfo->size == 0) {
return; return;
...@@ -1989,13 +2022,13 @@ void clearClosedSlidingWindows(SQueryRuntimeEnv *pRuntimeEnv) { ...@@ -1989,13 +2022,13 @@ void clearClosedSlidingWindows(SQueryRuntimeEnv *pRuntimeEnv) {
// clear all the closed windows from the window list // clear all the closed windows from the window list
for (int32_t k = 0; k < unclosed; ++k) { for (int32_t k = 0; k < unclosed; ++k) {
copyGroupResultBuf(pRuntimeEnv, &pWindowResInfo->pResult[k], &pWindowResInfo->pResult[i + k]); copyTimeWindowResBuf(pRuntimeEnv, &pWindowResInfo->pResult[k], &pWindowResInfo->pResult[i + k]);
} }
// move the unclosed window in the front of the window list // move the unclosed window in the front of the window list
for (int32_t k = unclosed; k < pWindowResInfo->size; ++k) { for (int32_t k = unclosed; k < pWindowResInfo->size; ++k) {
SWindowResult *pWindowRes = &pWindowResInfo->pResult[k]; SWindowResult *pWindowRes = &pWindowResInfo->pResult[k];
clearGroupResultBuf(pRuntimeEnv, pWindowRes); clearTimeWindowResBuf(pRuntimeEnv, pWindowRes);
} }
pWindowResInfo->size = unclosed; pWindowResInfo->size = unclosed;
...@@ -2006,7 +2039,7 @@ void clearClosedSlidingWindows(SQueryRuntimeEnv *pRuntimeEnv) { ...@@ -2006,7 +2039,7 @@ void clearClosedSlidingWindows(SQueryRuntimeEnv *pRuntimeEnv) {
TSDB_KEYSIZE); TSDB_KEYSIZE);
int32_t v = (*p - i); int32_t v = (*p - i);
//todo add the update function for hash table // todo add the update function for hash table
taosDeleteFromHashTable(pWindowResInfo->hashList, (const char *)&pResult->window.skey, TSDB_KEYSIZE); taosDeleteFromHashTable(pWindowResInfo->hashList, (const char *)&pResult->window.skey, TSDB_KEYSIZE);
taosAddToHashTable(pWindowResInfo->hashList, (const char *)&pResult->window.skey, TSDB_KEYSIZE, (char *)&v, taosAddToHashTable(pWindowResInfo->hashList, (const char *)&pResult->window.skey, TSDB_KEYSIZE, (char *)&v,
sizeof(int32_t)); sizeof(int32_t));
...@@ -2015,7 +2048,7 @@ void clearClosedSlidingWindows(SQueryRuntimeEnv *pRuntimeEnv) { ...@@ -2015,7 +2048,7 @@ void clearClosedSlidingWindows(SQueryRuntimeEnv *pRuntimeEnv) {
pWindowResInfo->curIndex = -1; pWindowResInfo->curIndex = -1;
} }
int32_t numOfClosedSlidingWindow(SWindowResInfo *pWindowResInfo) { int32_t numOfClosedTimeWindow(SWindowResInfo *pWindowResInfo) {
int32_t i = 0; int32_t i = 0;
while (i < pWindowResInfo->size && pWindowResInfo->pResult[i].status.closed) { while (i < pWindowResInfo->size && pWindowResInfo->pResult[i].status.closed) {
++i; ++i;
...@@ -2024,12 +2057,12 @@ int32_t numOfClosedSlidingWindow(SWindowResInfo *pWindowResInfo) { ...@@ -2024,12 +2057,12 @@ int32_t numOfClosedSlidingWindow(SWindowResInfo *pWindowResInfo) {
return i; return i;
} }
void closeSlidingWindow(SWindowResInfo *pWindowResInfo, int32_t slot) { void closeTimeWindow(SWindowResInfo *pWindowResInfo, int32_t slot) {
assert(slot >= 0 && slot < pWindowResInfo->size); assert(slot >= 0 && slot < pWindowResInfo->size);
pWindowResInfo->pResult[slot].status.closed = true; pWindowResInfo->pResult[slot].status.closed = true;
} }
void closeAllSlidingWindow(SWindowResInfo *pWindowResInfo) { void closeAllTimeWindow(SWindowResInfo *pWindowResInfo) {
assert(pWindowResInfo->size >= 0 && pWindowResInfo->capacity >= pWindowResInfo->size); assert(pWindowResInfo->size >= 0 && pWindowResInfo->capacity >= pWindowResInfo->size);
for (int32_t i = 0; i < pWindowResInfo->size; ++i) { for (int32_t i = 0; i < pWindowResInfo->size; ++i) {
...@@ -2042,12 +2075,12 @@ static int32_t setGroupResultFromKey(SQueryRuntimeEnv *pRuntimeEnv, char *pData, ...@@ -2042,12 +2075,12 @@ static int32_t setGroupResultFromKey(SQueryRuntimeEnv *pRuntimeEnv, char *pData,
return -1; return -1;
} }
SWindowResult *pWindowRes = doSetSlidingWindowFromKey(pRuntimeEnv, &pRuntimeEnv->windowResInfo, pData, bytes); SWindowResult *pWindowRes = doSetTimeWindowFromKey(pRuntimeEnv, &pRuntimeEnv->windowResInfo, pData, bytes);
if (pWindowRes == NULL) { if (pWindowRes == NULL) {
return -1; return -1;
} }
setGroupOutputBuffer(pRuntimeEnv, pWindowRes); setWindowResOutputBuf(pRuntimeEnv, pWindowRes);
initCtxOutputBuf(pRuntimeEnv); initCtxOutputBuf(pRuntimeEnv);
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
...@@ -2139,10 +2172,11 @@ static bool functionNeedToExecute(SQueryRuntimeEnv *pRuntimeEnv, SQLFunctionCtx ...@@ -2139,10 +2172,11 @@ static bool functionNeedToExecute(SQueryRuntimeEnv *pRuntimeEnv, SQLFunctionCtx
return true; return true;
} }
static int32_t rowwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t *forwardStep, TSKEY *primaryKeyCol, static int32_t rowwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t *forwardStep, SField *pFields,
SField *pFields, SBlockInfo *pBlockInfo, SWindowResInfo *pWindowResInfo) { SBlockInfo *pBlockInfo, SWindowResInfo *pWindowResInfo) {
SQLFunctionCtx *pCtx = pRuntimeEnv->pCtx; SQLFunctionCtx *pCtx = pRuntimeEnv->pCtx;
SQuery * pQuery = pRuntimeEnv->pQuery; SQuery * pQuery = pRuntimeEnv->pQuery;
TSKEY * primaryKeyCol = (TSKEY *)pRuntimeEnv->primaryColBuffer->data;
bool isDiskFileBlock = IS_FILE_BLOCK(pRuntimeEnv->blockStatus); bool isDiskFileBlock = IS_FILE_BLOCK(pRuntimeEnv->blockStatus);
SData **data = pRuntimeEnv->colDataBuffer; SData **data = pRuntimeEnv->colDataBuffer;
...@@ -2221,7 +2255,7 @@ static int32_t rowwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t * ...@@ -2221,7 +2255,7 @@ static int32_t rowwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t *
if (pQuery->slidingTime > 0 && pQuery->intervalTime > 0) { if (pQuery->slidingTime > 0 && pQuery->intervalTime > 0) {
// decide the time window according to the primary timestamp // decide the time window according to the primary timestamp
int64_t ts = primaryKeyCol[offset]; int64_t ts = primaryKeyCol[offset];
STimeWindow win = getActiveSlidingWindow(pWindowResInfo, ts, pQuery); STimeWindow win = getActiveTimeWindow(pWindowResInfo, ts, pQuery);
int32_t ret = setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, pRuntimeEnv->pMeterObj->sid, &win); int32_t ret = setWindowOutputBufByKey(pRuntimeEnv, pWindowResInfo, pRuntimeEnv->pMeterObj->sid, &win);
if (ret != TSDB_CODE_SUCCESS) { // null data, too many state code if (ret != TSDB_CODE_SUCCESS) { // null data, too many state code
...@@ -2231,25 +2265,15 @@ static int32_t rowwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t * ...@@ -2231,25 +2265,15 @@ static int32_t rowwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t *
// all startOffset are identical // all startOffset are identical
offset -= pCtx[0].startOffset; offset -= pCtx[0].startOffset;
SWindowStatus *pStatus = getSlidingWindowStatus(pWindowResInfo, curSlidingWindow(pWindowResInfo)); SWindowStatus *pStatus = getTimeWindowResStatus(pWindowResInfo, curTimeWindow(pWindowResInfo));
if (IS_MASTER_SCAN(pRuntimeEnv) || pStatus->closed) { doRowwiseApplyFunctions(pRuntimeEnv, pStatus, &win);
for (int32_t k = 0; k < pQuery->numOfOutputCols; ++k) {
int32_t functionId = pQuery->pSelectExpr[k].pBase.functionId;
pCtx[k].nStartQueryTimestamp = win.skey;
if (functionNeedToExecute(pRuntimeEnv, &pCtx[k], functionId)) {
aAggs[functionId].xFunctionF(&pCtx[k], offset);
}
}
}
lastKey = ts; lastKey = ts;
int32_t prev = pWindowResInfo->curIndex; int32_t prev = pWindowResInfo->curIndex;
STimeWindow nextWin = win; STimeWindow nextWin = win;
while (1) { while (1) {
getNextLogicalQueryRange(pRuntimeEnv, &nextWin); getNextTimeWindow(pRuntimeEnv, &nextWin);
if (pWindowResInfo->startTime > nextWin.skey || (nextWin.skey > pQuery->ekey && QUERY_IS_ASC_QUERY(pQuery)) || if (pWindowResInfo->startTime > nextWin.skey || (nextWin.skey > pQuery->ekey && QUERY_IS_ASC_QUERY(pQuery)) ||
(nextWin.skey > pQuery->skey && !QUERY_IS_ASC_QUERY(pQuery))) { (nextWin.skey > pQuery->skey && !QUERY_IS_ASC_QUERY(pQuery))) {
pWindowResInfo->curIndex = prev; pWindowResInfo->curIndex = prev;
...@@ -2264,17 +2288,8 @@ static int32_t rowwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t * ...@@ -2264,17 +2288,8 @@ static int32_t rowwiseApplyAllFunctions(SQueryRuntimeEnv *pRuntimeEnv, int32_t *
break; break;
} }
pStatus = getSlidingWindowStatus(pWindowResInfo, curSlidingWindow(pWindowResInfo)); pStatus = getTimeWindowResStatus(pWindowResInfo, curTimeWindow(pWindowResInfo));
if (IS_MASTER_SCAN(pRuntimeEnv) || pStatus->closed) { doRowwiseApplyFunctions(pRuntimeEnv, pStatus, &nextWin);
for (int32_t k = 0; k < pQuery->numOfOutputCols; ++k) {
int32_t functionId = pQuery->pSelectExpr[k].pBase.functionId;
pCtx[k].nStartQueryTimestamp = nextWin.skey;
if (functionNeedToExecute(pRuntimeEnv, &pCtx[k], functionId)) {
aAggs[functionId].xFunctionF(&pCtx[k], offset);
}
}
}
} else { } else {
pWindowResInfo->curIndex = prev; pWindowResInfo->curIndex = prev;
break; break;
...@@ -2383,39 +2398,12 @@ static void validateQueryRangeAndData(SQueryRuntimeEnv *pRuntimeEnv, const TSKEY ...@@ -2383,39 +2398,12 @@ static void validateQueryRangeAndData(SQueryRuntimeEnv *pRuntimeEnv, const TSKEY
static int32_t applyFunctionsOnBlock(SQueryRuntimeEnv *pRuntimeEnv, SBlockInfo *pBlockInfo, int64_t *pPrimaryColumn, static int32_t applyFunctionsOnBlock(SQueryRuntimeEnv *pRuntimeEnv, SBlockInfo *pBlockInfo, int64_t *pPrimaryColumn,
SField *pFields, __block_search_fn_t searchFn, int32_t *numOfRes, SField *pFields, __block_search_fn_t searchFn, int32_t *numOfRes,
SWindowResInfo *pWindowResInfo) { SWindowResInfo *pWindowResInfo) {
int32_t forwardStep = 0;
SQuery *pQuery = pRuntimeEnv->pQuery; SQuery *pQuery = pRuntimeEnv->pQuery;
int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order);
validateQueryRangeAndData(pRuntimeEnv, pPrimaryColumn, pBlockInfo); validateQueryRangeAndData(pRuntimeEnv, pPrimaryColumn, pBlockInfo);
if (QUERY_IS_ASC_QUERY(pQuery)) { int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order);
if (pQuery->ekey < pBlockInfo->keyLast) { int32_t forwardStep =
forwardStep = getForwardStepsInBlock(pBlockInfo->size, searchFn, pQuery->ekey, pQuery->pos, pQuery->order.order, getNumOfRowsInTimeWindow(pQuery, pBlockInfo, pPrimaryColumn, pQuery->pos, pQuery->ekey, searchFn, true);
pPrimaryColumn);
if (forwardStep == 0) { // no qualified data in current block, do not update the lastKey value
assert(pQuery->ekey < pPrimaryColumn[pQuery->pos]);
} else {
pQuery->lastKey = MAX(pQuery->ekey, pPrimaryColumn[pQuery->pos + (forwardStep - 1)]) + step;
}
} else {
forwardStep = pBlockInfo->size - pQuery->pos;
pQuery->lastKey = pBlockInfo->keyLast + step;
}
} else { // desc
if (pQuery->ekey > pBlockInfo->keyFirst) {
forwardStep = getForwardStepsInBlock(pBlockInfo->size, searchFn, pQuery->ekey, pQuery->pos, pQuery->order.order,
pPrimaryColumn);
if (forwardStep == 0) { // no qualified data in current block, do not update the lastKey value
assert(pQuery->ekey > pPrimaryColumn[pQuery->pos]);
} else {
pQuery->lastKey = MIN(pQuery->ekey, pPrimaryColumn[pQuery->pos - (forwardStep - 1)]) + step;
}
} else {
forwardStep = pQuery->pos + 1;
pQuery->lastKey = pBlockInfo->keyFirst + step;
}
}
assert(forwardStep >= 0); assert(forwardStep >= 0);
...@@ -2427,13 +2415,10 @@ static int32_t applyFunctionsOnBlock(SQueryRuntimeEnv *pRuntimeEnv, SBlockInfo * ...@@ -2427,13 +2415,10 @@ static int32_t applyFunctionsOnBlock(SQueryRuntimeEnv *pRuntimeEnv, SBlockInfo *
pQuery->lastKey = pPrimaryColumn[pQuery->pos + (newForwardStep - 1) * step] + step; pQuery->lastKey = pPrimaryColumn[pQuery->pos + (newForwardStep - 1) * step] + step;
} }
if (pQuery->numOfFilterCols > 0 || pRuntimeEnv->pTSBuf != NULL || isGroupbyNormalCol(pQuery->pGroupbyExpr) /*|| if (pQuery->numOfFilterCols > 0 || pRuntimeEnv->pTSBuf != NULL || isGroupbyNormalCol(pQuery->pGroupbyExpr)) {
(pQuery->slidingTime != -1 && pQuery->intervalTime > 0)*/) { *numOfRes = rowwiseApplyAllFunctions(pRuntimeEnv, &newForwardStep, pFields, pBlockInfo, pWindowResInfo);
*numOfRes =
rowwiseApplyAllFunctions(pRuntimeEnv, &newForwardStep, pPrimaryColumn, pFields, pBlockInfo, pWindowResInfo);
} else { } else {
*numOfRes = blockwiseApplyAllFunctions(pRuntimeEnv, newForwardStep, pPrimaryColumn, pFields, pBlockInfo, *numOfRes = blockwiseApplyAllFunctions(pRuntimeEnv, newForwardStep, pFields, pBlockInfo, pWindowResInfo, searchFn);
pWindowResInfo, searchFn);
} }
TSKEY lastKey = (QUERY_IS_ASC_QUERY(pQuery)) ? pBlockInfo->keyLast : pBlockInfo->keyFirst; TSKEY lastKey = (QUERY_IS_ASC_QUERY(pQuery)) ? pBlockInfo->keyLast : pBlockInfo->keyFirst;
...@@ -2761,7 +2746,7 @@ static void teardownQueryRuntimeEnv(SQueryRuntimeEnv *pRuntimeEnv) { ...@@ -2761,7 +2746,7 @@ static void teardownQueryRuntimeEnv(SQueryRuntimeEnv *pRuntimeEnv) {
} }
tfree(pRuntimeEnv->secondaryUnzipBuffer); tfree(pRuntimeEnv->secondaryUnzipBuffer);
destroySlidingWindowInfo(&pRuntimeEnv->windowResInfo); cleanupTimeWindowInfo(&pRuntimeEnv->windowResInfo, pRuntimeEnv);
if (pRuntimeEnv->pCtx != NULL) { if (pRuntimeEnv->pCtx != NULL) {
for (int32_t i = 0; i < pRuntimeEnv->pQuery->numOfOutputCols; ++i) { for (int32_t i = 0; i < pRuntimeEnv->pQuery->numOfOutputCols; ++i) {
...@@ -4360,44 +4345,6 @@ static int32_t initialNumOfRows(SMeterQuerySupportObj *pSupporter) { ...@@ -4360,44 +4345,6 @@ static int32_t initialNumOfRows(SMeterQuerySupportObj *pSupporter) {
return numOfRows; return numOfRows;
} }
static int32_t createQueryResultBuffer(SQueryRuntimeEnv *pRuntimeEnv, int32_t numOfRows, bool isSTableQuery) {
SQuery *pQuery = pRuntimeEnv->pQuery;
createDiskbasedResultBuffer(&pRuntimeEnv->pResultBuf, numOfRows, pQuery->rowSize);
// int32_t pageId = -1;
// tFilePage* page = NULL;
//
// pRuntimeEnv->windowResInfo.pResult = calloc(numOfRows, sizeof(SWindowResult));
//
// for (int32_t k = 0; k < numOfRows; ++k) {
// SWindowResult *pWindowRes = &pRuntimeEnv->windowResInfo.pResult[k];
// pWindowRes->nAlloc = 1;
//
// /*
// * for single table top/bottom query, the output for group by normal column, the output rows is
// * equals to the maximum rows, instead of 1.
// */
// if (!isSTableQuery && isTopBottomQuery(pQuery)) {
// assert(pQuery->numOfOutputCols > 1);
//
// SSqlFunctionExpr *pExpr = &pQuery->pSelectExpr[1];
// pWindowRes->nAlloc = pExpr->pBase.arg[0].argValue.i64;
// }
//
// if (page == NULL || page->numOfElems >= pRuntimeEnv->numOfRowsPerPage) {
// page = getNewDataBuf(pRuntimeEnv->pResultBuf, 0, &pageId);
// }
//
// assert(pageId >= 0);
//
// SPosInfo posInfo = {.pageId = pageId, .rowId = page->numOfElems};
// createQueryResultInfo(pQuery, pWindowRes, isSTableQuery, &posInfo);
// page->numOfElems += 1; // next row is available
// }
return TSDB_CODE_SUCCESS;
}
static int32_t allocateRuntimeEnvBuf(SQueryRuntimeEnv *pRuntimeEnv, SMeterObj *pMeterObj) { static int32_t allocateRuntimeEnvBuf(SQueryRuntimeEnv *pRuntimeEnv, SMeterObj *pMeterObj) {
SQuery *pQuery = pRuntimeEnv->pQuery; SQuery *pQuery = pRuntimeEnv->pQuery;
...@@ -4481,7 +4428,7 @@ static char *getPosInResultPage(SQueryRuntimeEnv *pRuntimeEnv, int32_t columnInd ...@@ -4481,7 +4428,7 @@ static char *getPosInResultPage(SQueryRuntimeEnv *pRuntimeEnv, int32_t columnInd
pQuery->pSelectExpr[columnIndex].resBytes * realRowId; pQuery->pSelectExpr[columnIndex].resBytes * realRowId;
} }
int32_t vnodeQuerySingleMeterPrepare(SQInfo *pQInfo, SMeterObj *pMeterObj, SMeterQuerySupportObj *pSupporter, int32_t vnodeQuerySingleTablePrepare(SQInfo *pQInfo, SMeterObj *pMeterObj, SMeterQuerySupportObj *pSupporter,
void *param) { void *param) {
SQuery *pQuery = &pQInfo->query; SQuery *pQuery = &pQInfo->query;
int32_t code = TSDB_CODE_SUCCESS; int32_t code = TSDB_CODE_SUCCESS;
...@@ -4567,7 +4514,7 @@ int32_t vnodeQuerySingleMeterPrepare(SQInfo *pQInfo, SMeterObj *pMeterObj, SMete ...@@ -4567,7 +4514,7 @@ int32_t vnodeQuerySingleMeterPrepare(SQInfo *pQInfo, SMeterObj *pMeterObj, SMete
type = TSDB_DATA_TYPE_TIMESTAMP; type = TSDB_DATA_TYPE_TIMESTAMP;
} }
initSlidingWindowInfo(pRuntimeEnv, &pRuntimeEnv->windowResInfo, rows, type); initWindowResInfo(&pRuntimeEnv->windowResInfo, pRuntimeEnv, rows, 4096, type);
} }
pSupporter->rawSKey = pQuery->skey; pSupporter->rawSKey = pQuery->skey;
...@@ -4598,6 +4545,13 @@ int32_t vnodeQuerySingleMeterPrepare(SQInfo *pQInfo, SMeterObj *pMeterObj, SMete ...@@ -4598,6 +4545,13 @@ int32_t vnodeQuerySingleMeterPrepare(SQInfo *pQInfo, SMeterObj *pMeterObj, SMete
STimeWindow win = {0}; STimeWindow win = {0};
getActualRange(pSupporter, &win); getActualRange(pSupporter, &win);
// there is no qualified data with respect to the primary timestamp
if (win.skey > win.ekey) {
sem_post(&pQInfo->dataReady);
pQInfo->over = 1;
return TSDB_CODE_SUCCESS;
}
TSKEY skey1, ekey1; TSKEY skey1, ekey1;
TSKEY windowSKey = 0, windowEKey = 0; TSKEY windowSKey = 0, windowEKey = 0;
...@@ -4680,7 +4634,7 @@ void vnodeQueryFreeQInfoEx(SQInfo *pQInfo) { ...@@ -4680,7 +4634,7 @@ void vnodeQueryFreeQInfoEx(SQInfo *pQInfo) {
} }
for (int32_t i = 0; i < size; ++i) { for (int32_t i = 0; i < size; ++i) {
// destroyGroupResultBuf(&pSupporter->pResult[i], pQInfo->query.numOfOutputCols); // destroyTimeWindowRes(&pSupporter->pResult[i], pQInfo->query.numOfOutputCols);
} }
} }
...@@ -4770,7 +4724,7 @@ int32_t vnodeMultiMeterQueryPrepare(SQInfo *pQInfo, SQuery *pQuery, void *param) ...@@ -4770,7 +4724,7 @@ int32_t vnodeMultiMeterQueryPrepare(SQInfo *pQInfo, SQuery *pQuery, void *param)
if (isGroupbyNormalCol(pQuery->pGroupbyExpr)) { // group by columns not tags; if (isGroupbyNormalCol(pQuery->pGroupbyExpr)) { // group by columns not tags;
int16_t type = getGroupbyColumnType(pQuery, pQuery->pGroupbyExpr); int16_t type = getGroupbyColumnType(pQuery, pQuery->pGroupbyExpr);
initSlidingWindowInfo(pRuntimeEnv, &pRuntimeEnv->windowResInfo, 128, type); initWindowResInfo(&pRuntimeEnv->windowResInfo, pRuntimeEnv, 128, 4096, type);
} }
} else { } else {
// one page for each table at least // one page for each table at least
...@@ -5133,7 +5087,7 @@ static void doHandleDataBlockImpl(SQueryRuntimeEnv *pRuntimeEnv, SBlockInfo *pbl ...@@ -5133,7 +5087,7 @@ static void doHandleDataBlockImpl(SQueryRuntimeEnv *pRuntimeEnv, SBlockInfo *pbl
} }
} }
static void getNextLogicalQueryRange(SQueryRuntimeEnv *pRuntimeEnv, STimeWindow *pTimeWindow) { static void getNextTimeWindow(SQueryRuntimeEnv *pRuntimeEnv, STimeWindow *pTimeWindow) {
SQuery *pQuery = pRuntimeEnv->pQuery; SQuery *pQuery = pRuntimeEnv->pQuery;
int32_t factor = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order); int32_t factor = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order);
...@@ -5194,33 +5148,15 @@ static int64_t doScanAllDataBlocks(SQueryRuntimeEnv *pRuntimeEnv) { ...@@ -5194,33 +5148,15 @@ static int64_t doScanAllDataBlocks(SQueryRuntimeEnv *pRuntimeEnv) {
* 1. interval query. * 1. interval query.
* 2. multi-output query that may cause buffer overflow. * 2. multi-output query that may cause buffer overflow.
*/ */
// if (pQuery->intervalTime > 0 || Q_STATUS_EQUAL(pQuery->over, QUERY_RESBUF_FULL)) { // if (pQuery->intervalTime > 0 || Q_STATUS_EQUAL(pQuery->over, QUERY_RESBUF_FULL)) {
if (nextPos >= blockInfo.size || nextPos < 0) { if (nextPos >= blockInfo.size || nextPos < 0) {
moveToNextBlock(pRuntimeEnv, step, searchFn, !LOAD_DATA); moveToNextBlock(pRuntimeEnv, step, searchFn, !LOAD_DATA);
// if (!Q_STATUS_EQUAL(pQuery->over, QUERY_NO_DATA_TO_CHECK|QUERY_COMPLETED)) {
// slot/pos/fileId is updated in moveToNextBlock function // slot/pos/fileId is updated in moveToNextBlock function
savePointPosition(&pRuntimeEnv->nextPos, pQuery->fileId, pQuery->slot, pQuery->pos); savePointPosition(&pRuntimeEnv->nextPos, pQuery->fileId, pQuery->slot, pQuery->pos);
// // check next block
// void *pNextBlock = getGenericDataBlock(pMeterObj, pRuntimeEnv, pQuery->slot);
//
// int32_t blockType = (IS_DISK_DATA_BLOCK(pQuery)) ? BLK_FILE_BLOCK : BLK_CACHE_BLOCK;
// blockInfo = getBlockBasicInfo(pRuntimeEnv, pNextBlock, blockType);
//
// // check if need to close window result or not
// if (pQuery->intervalTime > 0 && pQuery->slidingTime > 0) {
// TSKEY t = (QUERY_IS_ASC_QUERY(pQuery))? blockInfo.keyFirst:blockInfo.keyLast;
// doCheckQueryCompleted(pRuntimeEnv, t, &pRuntimeEnv->windowResInfo);
// }
// }
} else { } else {
savePointPosition(&pRuntimeEnv->nextPos, pQuery->fileId, pQuery->slot, accessPos + step); savePointPosition(&pRuntimeEnv->nextPos, pQuery->fileId, pQuery->slot, accessPos + step);
} }
// } else {
// assert(0);
// }
break; break;
} else { // query not completed, move to next block } else { // query not completed, move to next block
blockLoadStatus = moveToNextBlock(pRuntimeEnv, step, searchFn, LOAD_DATA); blockLoadStatus = moveToNextBlock(pRuntimeEnv, step, searchFn, LOAD_DATA);
...@@ -5242,21 +5178,15 @@ static int64_t doScanAllDataBlocks(SQueryRuntimeEnv *pRuntimeEnv) { ...@@ -5242,21 +5178,15 @@ static int64_t doScanAllDataBlocks(SQueryRuntimeEnv *pRuntimeEnv) {
break; break;
} }
// // check if need to close window result or not if (Q_STATUS_EQUAL(pQuery->over, QUERY_RESBUF_FULL)) {
// if (pQuery->intervalTime > 0 && pQuery->slidingTime > 0) {
// TSKEY t = (QUERY_IS_ASC_QUERY(pQuery))? blockInfo.keyFirst:blockInfo.keyLast;
// doCheckQueryCompleted(pRuntimeEnv, t, &pRuntimeEnv->windowResInfo);
// }
if(Q_STATUS_EQUAL(pQuery->over, QUERY_RESBUF_FULL)) {
break; break;
} }
} // while(1) } // while(1)
if (pQuery->intervalTime > 0) { if (pQuery->intervalTime > 0) {
if (Q_STATUS_EQUAL(pQuery->over, QUERY_COMPLETED|QUERY_NO_DATA_TO_CHECK)) { if (Q_STATUS_EQUAL(pQuery->over, QUERY_COMPLETED | QUERY_NO_DATA_TO_CHECK)) {
closeAllSlidingWindow(&pRuntimeEnv->windowResInfo); closeAllTimeWindow(&pRuntimeEnv->windowResInfo);
} else if (Q_STATUS_EQUAL(pQuery->over, QUERY_RESBUF_FULL)) { // check if window needs to be closed } else if (Q_STATUS_EQUAL(pQuery->over, QUERY_RESBUF_FULL)) { // check if window needs to be closed
void *pNextBlock = getGenericDataBlock(pMeterObj, pRuntimeEnv, pQuery->slot); void *pNextBlock = getGenericDataBlock(pMeterObj, pRuntimeEnv, pQuery->slot);
...@@ -5264,7 +5194,7 @@ static int64_t doScanAllDataBlocks(SQueryRuntimeEnv *pRuntimeEnv) { ...@@ -5264,7 +5194,7 @@ static int64_t doScanAllDataBlocks(SQueryRuntimeEnv *pRuntimeEnv) {
SBlockInfo blockInfo = getBlockBasicInfo(pRuntimeEnv, pNextBlock, blockType); SBlockInfo blockInfo = getBlockBasicInfo(pRuntimeEnv, pNextBlock, blockType);
// check if need to close window result or not // check if need to close window result or not
TSKEY t = (QUERY_IS_ASC_QUERY(pQuery))? blockInfo.keyFirst:blockInfo.keyLast; TSKEY t = (QUERY_IS_ASC_QUERY(pQuery)) ? blockInfo.keyFirst : blockInfo.keyLast;
doCheckQueryCompleted(pRuntimeEnv, t, &pRuntimeEnv->windowResInfo); doCheckQueryCompleted(pRuntimeEnv, t, &pRuntimeEnv->windowResInfo);
} }
} }
...@@ -5274,9 +5204,8 @@ static int64_t doScanAllDataBlocks(SQueryRuntimeEnv *pRuntimeEnv) { ...@@ -5274,9 +5204,8 @@ static int64_t doScanAllDataBlocks(SQueryRuntimeEnv *pRuntimeEnv) {
static void updatelastkey(SQuery *pQuery, SMeterQueryInfo *pMeterQInfo) { pMeterQInfo->lastKey = pQuery->lastKey; } static void updatelastkey(SQuery *pQuery, SMeterQueryInfo *pMeterQInfo) { pMeterQInfo->lastKey = pQuery->lastKey; }
void queryOnBlock(SMeterQuerySupportObj *pSupporter, int64_t *primaryKeys, int32_t blockStatus, void queryOnBlock(SMeterQuerySupportObj *pSupporter, int32_t blockStatus, SBlockInfo *pBlockBasicInfo,
SBlockInfo *pBlockBasicInfo, SMeterDataInfo *pMeterDataInfo, SField *pFields, SMeterDataInfo *pMeterDataInfo, SField *pFields, __block_search_fn_t searchFn) {
__block_search_fn_t searchFn) {
/* cache blocks may be assign to other meter, abort */ /* cache blocks may be assign to other meter, abort */
if (pBlockBasicInfo->size <= 0) { if (pBlockBasicInfo->size <= 0) {
return; return;
...@@ -5285,14 +5214,18 @@ void queryOnBlock(SMeterQuerySupportObj *pSupporter, int64_t *primaryKeys, int32 ...@@ -5285,14 +5214,18 @@ void queryOnBlock(SMeterQuerySupportObj *pSupporter, int64_t *primaryKeys, int32
SQueryRuntimeEnv *pRuntimeEnv = &pSupporter->runtimeEnv; SQueryRuntimeEnv *pRuntimeEnv = &pSupporter->runtimeEnv;
SQuery * pQuery = pRuntimeEnv->pQuery; SQuery * pQuery = pRuntimeEnv->pQuery;
TSKEY* primaryKeys = (TSKEY*) pRuntimeEnv->primaryColBuffer->data;
if (pQuery->intervalTime == 0) { // not interval query if (pQuery->intervalTime == 0) { // not interval query
assert(0);
int32_t numOfRes = 0; int32_t numOfRes = 0;
applyFunctionsOnBlock(pRuntimeEnv, pBlockBasicInfo, primaryKeys, pFields, searchFn, &numOfRes, applyFunctionsOnBlock(pRuntimeEnv, pBlockBasicInfo, primaryKeys, pFields, searchFn, &numOfRes,
&pMeterDataInfo->pMeterQInfo->windowResInfo); &pMeterDataInfo->pMeterQInfo->windowResInfo);///????bug
// note: only fixed number of output for each group by operation // note: only fixed number of output for each group by operation
if (numOfRes > 0) { if (numOfRes > 0) {//???
pRuntimeEnv->windowResInfo.pResult[pMeterDataInfo->groupIdx].numOfRows = numOfRes; pRuntimeEnv->windowResInfo.pResult[pMeterDataInfo->groupIdx].numOfRows = numOfRes;////????bug
} }
// used to decide the correct start position in cache after check all data in files // used to decide the correct start position in cache after check all data in files
...@@ -5879,7 +5812,7 @@ void disableFunctForSuppleScan(SQueryRuntimeEnv *pRuntimeEnv, int32_t order) { ...@@ -5879,7 +5812,7 @@ void disableFunctForSuppleScan(SQueryRuntimeEnv *pRuntimeEnv, int32_t order) {
SWindowResInfo *pWindowResInfo = &pRuntimeEnv->windowResInfo; SWindowResInfo *pWindowResInfo = &pRuntimeEnv->windowResInfo;
for (int32_t i = 0; i < pWindowResInfo->size; ++i) { for (int32_t i = 0; i < pWindowResInfo->size; ++i) {
SWindowStatus *pStatus = getSlidingWindowStatus(pWindowResInfo, i); SWindowStatus *pStatus = getTimeWindowResStatus(pWindowResInfo, i);
if (!pStatus->closed) { if (!pStatus->closed) {
continue; continue;
} }
...@@ -5941,7 +5874,7 @@ void createQueryResultInfo(SQuery *pQuery, SWindowResult *pResultRow, bool isSTa ...@@ -5941,7 +5874,7 @@ void createQueryResultInfo(SQuery *pQuery, SWindowResult *pResultRow, bool isSTa
} }
} }
void clearGroupResultBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *pWindowRes) { void clearTimeWindowResBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *pWindowRes) {
if (pWindowRes == NULL) { if (pWindowRes == NULL) {
return; return;
} }
...@@ -5960,7 +5893,7 @@ void clearGroupResultBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *pWindowRe ...@@ -5960,7 +5893,7 @@ void clearGroupResultBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *pWindowRe
pWindowRes->nAlloc = 0; pWindowRes->nAlloc = 0;
pWindowRes->pos = (SPosInfo){-1, -1}; pWindowRes->pos = (SPosInfo){-1, -1};
pWindowRes->status.closed = false; pWindowRes->status.closed = false;
pWindowRes->window = (STimeWindow) {0, 0}; pWindowRes->window = (STimeWindow){0, 0};
} }
/** /**
...@@ -5968,7 +5901,7 @@ void clearGroupResultBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *pWindowRe ...@@ -5968,7 +5901,7 @@ void clearGroupResultBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *pWindowRe
* since the attribute of "Pos" is bound to each window result when the window result is created in the * since the attribute of "Pos" is bound to each window result when the window result is created in the
* disk-based result buffer. * disk-based result buffer.
*/ */
void copyGroupResultBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *dst, const SWindowResult *src) { void copyTimeWindowResBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *dst, const SWindowResult *src) {
dst->numOfRows = src->numOfRows; dst->numOfRows = src->numOfRows;
dst->nAlloc = src->nAlloc; dst->nAlloc = src->nAlloc;
dst->window = src->window; dst->window = src->window;
...@@ -5996,7 +5929,7 @@ void copyGroupResultBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *dst, const ...@@ -5996,7 +5929,7 @@ void copyGroupResultBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *dst, const
} }
} }
void destroyGroupResultBuf(SWindowResult *pWindowRes, int32_t nOutputCols) { void destroyTimeWindowRes(SWindowResult *pWindowRes, int32_t nOutputCols) {
if (pWindowRes == NULL) { if (pWindowRes == NULL) {
return; return;
} }
...@@ -6237,7 +6170,7 @@ void vnodeScanAllData(SQueryRuntimeEnv *pRuntimeEnv) { ...@@ -6237,7 +6170,7 @@ void vnodeScanAllData(SQueryRuntimeEnv *pRuntimeEnv) {
continue; continue;
} }
setGroupOutputBuffer(pRuntimeEnv, pResult); setWindowResOutputBuf(pRuntimeEnv, pResult);
for (int32_t j = 0; j < pQuery->numOfOutputCols; ++j) { for (int32_t j = 0; j < pQuery->numOfOutputCols; ++j) {
aAggs[pQuery->pSelectExpr[j].pBase.functionId].xNextStep(&pRuntimeEnv->pCtx[j]); aAggs[pQuery->pSelectExpr[j].pBase.functionId].xNextStep(&pRuntimeEnv->pCtx[j]);
...@@ -6279,7 +6212,7 @@ void vnodeScanAllData(SQueryRuntimeEnv *pRuntimeEnv) { ...@@ -6279,7 +6212,7 @@ void vnodeScanAllData(SQueryRuntimeEnv *pRuntimeEnv) {
doSingleMeterSupplementScan(pRuntimeEnv); doSingleMeterSupplementScan(pRuntimeEnv);
// update the pQuery->skey/pQuery->ekey to limit the scan scope of sliding query during supplementary scan // update the pQuery->skey/pQuery->ekey to limit the scan scope of sliding query during supplementary scan
pQuery->skey = newSkey; pQuery->skey = newSkey;
} }
...@@ -6290,16 +6223,16 @@ void doFinalizeResult(SQueryRuntimeEnv *pRuntimeEnv) { ...@@ -6290,16 +6223,16 @@ void doFinalizeResult(SQueryRuntimeEnv *pRuntimeEnv) {
// for each group result, call the finalize function for each column // for each group result, call the finalize function for each column
SWindowResInfo *pWindowResInfo = &pRuntimeEnv->windowResInfo; SWindowResInfo *pWindowResInfo = &pRuntimeEnv->windowResInfo;
if (isGroupbyNormalCol(pQuery->pGroupbyExpr)) { if (isGroupbyNormalCol(pQuery->pGroupbyExpr)) {
closeAllSlidingWindow(pWindowResInfo); closeAllTimeWindow(pWindowResInfo);
} }
for (int32_t i = 0; i < pWindowResInfo->size; ++i) { for (int32_t i = 0; i < pWindowResInfo->size; ++i) {
SWindowResult *buf = &pWindowResInfo->pResult[i]; SWindowResult *buf = &pWindowResInfo->pResult[i];
if (!slidingWindowClosed(pWindowResInfo, i)) { if (!isWindowResClosed(pWindowResInfo, i)) {
continue; continue;
} }
setGroupOutputBuffer(pRuntimeEnv, buf); setWindowResOutputBuf(pRuntimeEnv, buf);
for (int32_t j = 0; j < pQuery->numOfOutputCols; ++j) { for (int32_t j = 0; j < pQuery->numOfOutputCols; ++j) {
aAggs[pQuery->pSelectExpr[j].pBase.functionId].xFinalize(&pRuntimeEnv->pCtx[j]); aAggs[pQuery->pSelectExpr[j].pBase.functionId].xFinalize(&pRuntimeEnv->pCtx[j]);
...@@ -6414,30 +6347,30 @@ void forwardIntervalQueryRange(SMeterQuerySupportObj *pSupporter, SQueryRuntimeE ...@@ -6414,30 +6347,30 @@ void forwardIntervalQueryRange(SMeterQuerySupportObj *pSupporter, SQueryRuntimeE
return; return;
} }
// int32_t r = getNextIntervalQueryRange(pSupporter, pRuntimeEnv, &pQuery->skey, &pQuery->ekey); // int32_t r = getNextIntervalQueryRange(pSupporter, pRuntimeEnv, &pQuery->skey, &pQuery->ekey);
// if (r == QUERY_COMPLETED) { // if (r == QUERY_COMPLETED) {
// setQueryStatus(pQuery, QUERY_COMPLETED); // setQueryStatus(pQuery, QUERY_COMPLETED);
// return; // return;
// } // }
// //
// getNextLogicalQueryRange(pRuntimeEnv, &pRuntimeEnv->intervalWindow); // getNextTimeWindow(pRuntimeEnv, &pRuntimeEnv->intervalWindow);
// //
// /* ensure the search in cache will return right position */ // /* ensure the search in cache will return right position */
// pQuery->lastKey = pQuery->skey; // pQuery->lastKey = pQuery->skey;
// //
// TSKEY nextTimestamp = loadRequiredBlockIntoMem(pRuntimeEnv, &pRuntimeEnv->nextPos); // TSKEY nextTimestamp = loadRequiredBlockIntoMem(pRuntimeEnv, &pRuntimeEnv->nextPos);
// if ((nextTimestamp > pSupporter->rawEKey && QUERY_IS_ASC_QUERY(pQuery)) || // if ((nextTimestamp > pSupporter->rawEKey && QUERY_IS_ASC_QUERY(pQuery)) ||
// (nextTimestamp < pSupporter->rawEKey && !QUERY_IS_ASC_QUERY(pQuery)) || // (nextTimestamp < pSupporter->rawEKey && !QUERY_IS_ASC_QUERY(pQuery)) ||
// Q_STATUS_EQUAL(pQuery->over, QUERY_NO_DATA_TO_CHECK)) { // Q_STATUS_EQUAL(pQuery->over, QUERY_NO_DATA_TO_CHECK)) {
// setQueryStatus(pQuery, QUERY_COMPLETED); // setQueryStatus(pQuery, QUERY_COMPLETED);
// return; // return;
// } // }
// //
// // bridge the gap in group by time function // // bridge the gap in group by time function
// if ((nextTimestamp > pQuery->ekey && QUERY_IS_ASC_QUERY(pQuery)) || // if ((nextTimestamp > pQuery->ekey && QUERY_IS_ASC_QUERY(pQuery)) ||
// (nextTimestamp < pQuery->ekey && !QUERY_IS_ASC_QUERY(pQuery))) { // (nextTimestamp < pQuery->ekey && !QUERY_IS_ASC_QUERY(pQuery))) {
// getAlignedIntervalQueryRange(pRuntimeEnv, nextTimestamp, pSupporter->rawSKey, pSupporter->rawEKey); // getAlignedIntervalQueryRange(pRuntimeEnv, nextTimestamp, pSupporter->rawSKey, pSupporter->rawEKey);
// } // }
} }
static int32_t offsetComparator(const void *pLeft, const void *pRight) { static int32_t offsetComparator(const void *pLeft, const void *pRight) {
...@@ -6594,7 +6527,7 @@ SMeterQueryInfo *createMeterQueryInfo(SMeterQuerySupportObj *pSupporter, int32_t ...@@ -6594,7 +6527,7 @@ SMeterQueryInfo *createMeterQueryInfo(SMeterQuerySupportObj *pSupporter, int32_t
pMeterQueryInfo->sid = sid; pMeterQueryInfo->sid = sid;
pMeterQueryInfo->cur.vnodeIndex = -1; pMeterQueryInfo->cur.vnodeIndex = -1;
initSlidingWindowInfo(pRuntimeEnv, &pMeterQueryInfo->windowResInfo, 100, TSDB_DATA_TYPE_INT); initWindowResInfo(&pMeterQueryInfo->windowResInfo, pRuntimeEnv, 100, 100, TSDB_DATA_TYPE_INT);
return pMeterQueryInfo; return pMeterQueryInfo;
} }
...@@ -6618,11 +6551,13 @@ void changeMeterQueryInfoForSuppleQuery(SQueryDiskbasedResultBuf *pResultBuf, SM ...@@ -6618,11 +6551,13 @@ void changeMeterQueryInfoForSuppleQuery(SQueryDiskbasedResultBuf *pResultBuf, SM
return; return;
} }
pMeterQueryInfo->skey = skey; // pMeterQueryInfo->skey = skey;
pMeterQueryInfo->ekey = ekey; // pMeterQueryInfo->ekey = ekey;
SWAP(pMeterQueryInfo->skey, pMeterQueryInfo->ekey, TSKEY);
pMeterQueryInfo->lastKey = pMeterQueryInfo->skey; pMeterQueryInfo->lastKey = pMeterQueryInfo->skey;
pMeterQueryInfo->queryRangeSet = 0; // pMeterQueryInfo->queryRangeSet = 0;
pMeterQueryInfo->cur.order = pMeterQueryInfo->cur.order ^ 1; pMeterQueryInfo->cur.order = pMeterQueryInfo->cur.order ^ 1;
pMeterQueryInfo->cur.vnodeIndex = -1; pMeterQueryInfo->cur.vnodeIndex = -1;
...@@ -7029,7 +6964,7 @@ void setExecutionContext(SMeterQuerySupportObj *pSupporter, SWindowResult *outpu ...@@ -7029,7 +6964,7 @@ void setExecutionContext(SMeterQuerySupportObj *pSupporter, SWindowResult *outpu
int32_t groupIdx, SMeterQueryInfo *pMeterQueryInfo) { int32_t groupIdx, SMeterQueryInfo *pMeterQueryInfo) {
SQueryRuntimeEnv *pRuntimeEnv = &pSupporter->runtimeEnv; SQueryRuntimeEnv *pRuntimeEnv = &pSupporter->runtimeEnv;
setGroupOutputBuffer(pRuntimeEnv, &outputRes[groupIdx]); setWindowResOutputBuf(pRuntimeEnv, &outputRes[groupIdx]);
initCtxOutputBuf(pRuntimeEnv); initCtxOutputBuf(pRuntimeEnv);
vnodeSetTagValueInParam(pSupporter->pSidSet, pRuntimeEnv, pSupporter->pMeterSidExtInfo[meterIdx]); vnodeSetTagValueInParam(pSupporter->pSidSet, pRuntimeEnv, pSupporter->pMeterSidExtInfo[meterIdx]);
...@@ -7046,7 +6981,7 @@ void setExecutionContext(SMeterQuerySupportObj *pSupporter, SWindowResult *outpu ...@@ -7046,7 +6981,7 @@ void setExecutionContext(SMeterQuerySupportObj *pSupporter, SWindowResult *outpu
} }
} }
static void setGroupOutputBuffer(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *pResult) { static void setWindowResOutputBuf(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *pResult) {
SQuery *pQuery = pRuntimeEnv->pQuery; SQuery *pQuery = pRuntimeEnv->pQuery;
// Note: pResult->pos[i]->numOfElems == 0, there is only fixed number of results for each group // Note: pResult->pos[i]->numOfElems == 0, there is only fixed number of results for each group
...@@ -7067,9 +7002,9 @@ static void setGroupOutputBuffer(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *p ...@@ -7067,9 +7002,9 @@ static void setGroupOutputBuffer(SQueryRuntimeEnv *pRuntimeEnv, SWindowResult *p
// set super table query flag // set super table query flag
SResultInfo *pResInfo = GET_RES_INFO(pCtx); SResultInfo *pResInfo = GET_RES_INFO(pCtx);
if (!isGroupbyNormalCol(pQuery->pGroupbyExpr)) { // if (!isGroupbyNormalCol(pQuery->pGroupbyExpr)) {
pResInfo->superTableQ = true; pResInfo->superTableQ = pRuntimeEnv->stableQuery;
} // }
} }
} }
...@@ -7160,9 +7095,9 @@ int32_t setOutputBufferForIntervalQuery(SQueryRuntimeEnv *pRuntimeEnv, SMeterQue ...@@ -7160,9 +7095,9 @@ int32_t setOutputBufferForIntervalQuery(SQueryRuntimeEnv *pRuntimeEnv, SMeterQue
SQueryDiskbasedResultBuf *pResultBuf = pRuntimeEnv->pResultBuf; SQueryDiskbasedResultBuf *pResultBuf = pRuntimeEnv->pResultBuf;
SWindowResInfo * pWindowResInfo = &pMeterQueryInfo->windowResInfo; SWindowResInfo * pWindowResInfo = &pMeterQueryInfo->windowResInfo;
STimeWindow win = getActiveSlidingWindow(pWindowResInfo, pMeterQueryInfo->lastKey, pRuntimeEnv->pQuery); STimeWindow win = getActiveTimeWindow(pWindowResInfo, pMeterQueryInfo->lastKey, pRuntimeEnv->pQuery);
SWindowResult *pWindowRes = doSetSlidingWindowFromKey(pRuntimeEnv, pWindowResInfo, (char *)&win.skey, TSDB_KEYSIZE); SWindowResult *pWindowRes = doSetTimeWindowFromKey(pRuntimeEnv, pWindowResInfo, (char *)&win.skey, TSDB_KEYSIZE);
if (pWindowRes == NULL) { if (pWindowRes == NULL) {
return -1; return -1;
} }
...@@ -7190,8 +7125,8 @@ int32_t setIntervalQueryExecutionContext(SMeterQuerySupportObj *pSupporter, int3 ...@@ -7190,8 +7125,8 @@ int32_t setIntervalQueryExecutionContext(SMeterQuerySupportObj *pSupporter, int3
SQueryRuntimeEnv *pRuntimeEnv = &pSupporter->runtimeEnv; SQueryRuntimeEnv *pRuntimeEnv = &pSupporter->runtimeEnv;
if (IS_MASTER_SCAN(pRuntimeEnv)) { if (IS_MASTER_SCAN(pRuntimeEnv)) {
if (setOutputBufferForIntervalQuery(pRuntimeEnv, pMeterQueryInfo) != TSDB_CODE_SUCCESS) {
// not enough disk space or memory buffer for intermediate results // not enough disk space or memory buffer for intermediate results
if (setOutputBufferForIntervalQuery(pRuntimeEnv, pMeterQueryInfo) != TSDB_CODE_SUCCESS) {
return -1; return -1;
} }
...@@ -7234,172 +7169,6 @@ int32_t setIntervalQueryExecutionContext(SMeterQuerySupportObj *pSupporter, int3 ...@@ -7234,172 +7169,6 @@ int32_t setIntervalQueryExecutionContext(SMeterQuerySupportObj *pSupporter, int3
return 0; return 0;
} }
// static void doApplyIntervalQueryOnBlock(SMeterQuerySupportObj *pSupporter, SMeterQueryInfo *pMeterQueryInfo,
// SBlockInfo *pBlockInfo, int64_t *pPrimaryCol, SField *pFields,
// __block_search_fn_t searchFn) {
// SQueryRuntimeEnv *pRuntimeEnv = &pSupporter->runtimeEnv;
// SQuery * pQuery = pRuntimeEnv->pQuery;
// int32_t factor = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order);
//
// int64_t nextKey = -1;
// bool queryCompleted = false;
//
// while (1) {
// int32_t numOfRes = 0;
// int32_t steps = applyFunctionsOnBlock(pRuntimeEnv, pBlockInfo, pPrimaryCol, pFields, searchFn, &numOfRes);
// assert(steps > 0);
//
// // NOTE: in case of stable query, only ONE(or ZERO) row of pos generated for each query range
// if (pMeterQueryInfo->lastResRows == 0) {
// pMeterQueryInfo->lastResRows = numOfRes;
// } else {
// assert(pMeterQueryInfo->lastResRows == 1);
// }
//
// int32_t pos = pQuery->pos + steps * factor;
//
// // query does not reach the end of current block
// if ((pos < pBlockInfo->size && QUERY_IS_ASC_QUERY(pQuery)) || (pos >= 0 && !QUERY_IS_ASC_QUERY(pQuery))) {
// nextKey = pPrimaryCol[pos];
// } else {
// assert((pQuery->lastKey > pBlockInfo->keyLast && QUERY_IS_ASC_QUERY(pQuery)) ||
// (pQuery->lastKey < pBlockInfo->keyFirst && !QUERY_IS_ASC_QUERY(pQuery)));
// }
//
// // all data satisfy current query are checked, query completed
// if (QUERY_IS_ASC_QUERY(pQuery)) {
// queryCompleted = (nextKey > pQuery->ekey || pQuery->ekey <= pBlockInfo->keyLast);
// } else {
// queryCompleted = (nextKey < pQuery->ekey || pQuery->ekey >= pBlockInfo->keyFirst);
// }
//
// /*
// * 1. there may be more date that satisfy current query interval, other than
// * current block, we need to try next data blocks
// * 2. query completed, since reaches the upper bound of the main query range
// */
// if (QUERY_IS_ASC_QUERY(pQuery)) {
// if (pQuery->lastKey > pBlockInfo->keyLast || pQuery->lastKey > pSupporter->rawEKey ||
// nextKey > pSupporter->rawEKey) {
// /*
// * current interval query is completed, set query pos flag closed and
// * try next data block if pQuery->ekey == pSupporter->rawEKey, whole query is completed
// */
// if (pQuery->lastKey > pBlockInfo->keyLast) {
// assert(pQuery->ekey >= pBlockInfo->keyLast);
// }
//
// if (pQuery->lastKey > pSupporter->rawEKey || nextKey > pSupporter->rawEKey) {
// /* whole query completed, save pos and abort */
// assert(queryCompleted);
// saveResult(pSupporter, pMeterQueryInfo, pMeterQueryInfo->lastResRows);
//
// // save the pQuery->lastKey for retrieve data in cache, actually, there will be no qualified data in cache.
// saveIntervalQueryRange(pRuntimeEnv, pMeterQueryInfo);
// } else if (pQuery->ekey == pBlockInfo->keyLast) {
// /* current interval query is completed, set the next query range on other data blocks if exist */
// int64_t prevEKey = pQuery->ekey;
//
// getAlignedIntervalQueryRange(pRuntimeEnv, pQuery->lastKey, pSupporter->rawSKey, pSupporter->rawEKey);
// saveIntervalQueryRange(pRuntimeEnv, pMeterQueryInfo);
//
// assert(queryCompleted && prevEKey < pQuery->skey);
// if (pMeterQueryInfo->lastResRows > 0) {
// saveResult(pSupporter, pMeterQueryInfo, pMeterQueryInfo->lastResRows);
// }
// } else {
// /*
// * Data that satisfy current query range may locate in current block and blocks that are directly right
// * next to current block. Therefore, we need to keep the query range(interval) unchanged until reaching
// * the direct next data block, while only forwards the pQuery->lastKey.
// *
// * With the information of the directly next data block, whether locates in cache or disk,
// * current interval query being completed or not can be decided.
// */
// saveIntervalQueryRange(pRuntimeEnv, pMeterQueryInfo);
// assert(pQuery->lastKey > pBlockInfo->keyLast && pQuery->lastKey <= pQuery->ekey);
//
// /*
// * if current block is the last block of current file, we still close the pos flag, and
// * merge with other meters in the same group
// */
// if (queryCompleted) {
// saveResult(pSupporter, pMeterQueryInfo, pMeterQueryInfo->lastResRows);
// }
// }
//
// break;
// }
// } else {
// if (pQuery->lastKey < pBlockInfo->keyFirst || pQuery->lastKey < pSupporter->rawEKey ||
// nextKey < pSupporter->rawEKey) {
// if (pQuery->lastKey < pBlockInfo->keyFirst) {
// assert(pQuery->ekey <= pBlockInfo->keyFirst);
// }
//
// if (pQuery->lastKey < pSupporter->rawEKey || (nextKey < pSupporter->rawEKey && nextKey != -1)) {
// /* whole query completed, save pos and abort */
// assert(queryCompleted);
// saveResult(pSupporter, pMeterQueryInfo, pMeterQueryInfo->lastResRows);
//
// /*
// * save the pQuery->lastKey for retrieve data in cache, actually,
// * there will be no qualified data in cache.
// */
// saveIntervalQueryRange(pRuntimeEnv, pMeterQueryInfo);
// } else if (pQuery->ekey == pBlockInfo->keyFirst) {
// // current interval query is completed, set the next query range on other data blocks if exist
// int64_t prevEKey = pQuery->ekey;
//
// getAlignedIntervalQueryRange(pRuntimeEnv, pQuery->lastKey, pSupporter->rawSKey, pSupporter->rawEKey);
// saveIntervalQueryRange(pRuntimeEnv, pMeterQueryInfo);
//
// assert(queryCompleted && prevEKey > pQuery->skey);
// if (pMeterQueryInfo->lastResRows > 0) {
// saveResult(pSupporter, pMeterQueryInfo, pMeterQueryInfo->lastResRows);
// }
// } else {
// /*
// * Data that satisfy current query range may locate in current block and blocks that are
// * directly right next to current block. Therefore, we need to keep the query range(interval)
// * unchanged until reaching the direct next data block, while only forwards the pQuery->lastKey.
// *
// * With the information of the directly next data block, whether locates in cache or disk,
// * current interval query being completed or not can be decided.
// */
// saveIntervalQueryRange(pRuntimeEnv, pMeterQueryInfo);
// assert(pQuery->lastKey < pBlockInfo->keyFirst && pQuery->lastKey >= pQuery->ekey);
//
// /*
// * if current block is the last block of current file, we still close the pos
// * flag, and merge with other meters in the same group
// */
// if (queryCompleted) {
// saveResult(pSupporter, pMeterQueryInfo, pMeterQueryInfo->lastResRows);
// }
// }
//
// break;
// }
// }
//
// assert(queryCompleted);
// saveResult(pSupporter, pMeterQueryInfo, pMeterQueryInfo->lastResRows);
//
// assert((nextKey >= pQuery->lastKey && QUERY_IS_ASC_QUERY(pQuery)) ||
// (nextKey <= pQuery->lastKey && !QUERY_IS_ASC_QUERY(pQuery)));
//
// /* still in the same block to query */
// getAlignedIntervalQueryRange(pRuntimeEnv, nextKey, pSupporter->rawSKey, pSupporter->rawEKey);
// saveIntervalQueryRange(pRuntimeEnv, pMeterQueryInfo);
//
// int32_t newPos = searchFn((char *)pPrimaryCol, pBlockInfo->size, pQuery->skey, pQuery->order.order);
// assert(newPos == pQuery->pos + steps * factor);
//
// pQuery->pos = newPos;
// }
//}
static void doApplyIntervalQueryOnBlock_rv(SMeterQuerySupportObj *pSupporter, SMeterQueryInfo *pMeterQueryInfo, static void doApplyIntervalQueryOnBlock_rv(SMeterQuerySupportObj *pSupporter, SMeterQueryInfo *pMeterQueryInfo,
SBlockInfo *pBlockInfo, int64_t *pPrimaryCol, SField *pFields, SBlockInfo *pBlockInfo, int64_t *pPrimaryCol, SField *pFields,
__block_search_fn_t searchFn) { __block_search_fn_t searchFn) {
...@@ -7443,7 +7212,7 @@ static void doApplyIntervalQueryOnBlock_rv(SMeterQuerySupportObj *pSupporter, SM ...@@ -7443,7 +7212,7 @@ static void doApplyIntervalQueryOnBlock_rv(SMeterQuerySupportObj *pSupporter, SM
/* /*
* 1. there may be more date that satisfy current query interval, other than * 1. there may be more date that satisfy current query interval, other than
* current block, we need to try next data blocks * current block, we need to try next data blocks
* 2. query completed, since reaches the upper bound of the main query range * 2. query completed, since it reaches the upper bound of the main query range
*/ */
if (!completed) { if (!completed) {
/* /*
...@@ -7501,13 +7270,14 @@ static void doApplyIntervalQueryOnBlock_rv(SMeterQuerySupportObj *pSupporter, SM ...@@ -7501,13 +7270,14 @@ static void doApplyIntervalQueryOnBlock_rv(SMeterQuerySupportObj *pSupporter, SM
pQuery->pos = newPos; pQuery->pos = newPos;
} }
} }
int64_t getNextAccessedKeyInData(SQuery *pQuery, int64_t *pPrimaryCol, SBlockInfo *pBlockInfo, int32_t blockStatus) { int64_t getNextAccessedKeyInData(SQuery *pQuery, int64_t *pPrimaryCol, SBlockInfo *pBlockInfo, int32_t blockStatus) {
assert(pQuery->pos >= 0 && pQuery->pos <= pBlockInfo->size - 1); assert(pQuery->pos >= 0 && pQuery->pos <= pBlockInfo->size - 1);
TSKEY key = -1; TSKEY key = -1;
if (IS_DATA_BLOCK_LOADED(blockStatus)) { if (IS_DATA_BLOCK_LOADED(blockStatus)) {
key = pPrimaryCol[pQuery->pos]; key = pPrimaryCol[pQuery->pos];
} else { } else {// while the data block is not loaded, the position must be the first or last position
assert(pQuery->pos == pBlockInfo->size - 1 || pQuery->pos == 0); assert(pQuery->pos == pBlockInfo->size - 1 || pQuery->pos == 0);
key = QUERY_IS_ASC_QUERY(pQuery) ? pBlockInfo->keyFirst : pBlockInfo->keyLast; key = QUERY_IS_ASC_QUERY(pQuery) ? pBlockInfo->keyFirst : pBlockInfo->keyLast;
} }
...@@ -7530,55 +7300,66 @@ void setIntervalQueryRange(SMeterQueryInfo *pMeterQueryInfo, SMeterQuerySupportO ...@@ -7530,55 +7300,66 @@ void setIntervalQueryRange(SMeterQueryInfo *pMeterQueryInfo, SMeterQuerySupportO
SQuery * pQuery = pRuntimeEnv->pQuery; SQuery * pQuery = pRuntimeEnv->pQuery;
if (pMeterQueryInfo->queryRangeSet) { if (pMeterQueryInfo->queryRangeSet) {
assert((QUERY_IS_ASC_QUERY(pQuery) && pQuery->lastKey >= pQuery->skey) || // assert((QUERY_IS_ASC_QUERY(pQuery) && pQuery->lastKey >= pQuery->skey) ||
(!QUERY_IS_ASC_QUERY(pQuery) && pQuery->lastKey <= pQuery->skey)); // (!QUERY_IS_ASC_QUERY(pQuery) && pQuery->lastKey <= pQuery->skey));
//
if ((pQuery->ekey < key && QUERY_IS_ASC_QUERY(pQuery)) || (pQuery->ekey > key && !QUERY_IS_ASC_QUERY(pQuery))) { // if ((pQuery->ekey < key && QUERY_IS_ASC_QUERY(pQuery)) || (pQuery->ekey > key && !QUERY_IS_ASC_QUERY(pQuery))) {
/* // /*
* last query on this block of the meter is done, start next interval on this block // * last query on this block of the meter is done, start next interval on this block
* otherwise, keep the previous query range and proceed // * otherwise, keep the previous query range and proceed
*/ // */
getAlignedIntervalQueryRange(pRuntimeEnv, key, pSupporter->rawSKey, pSupporter->rawEKey); // getAlignedIntervalQueryRange(pRuntimeEnv, key, pSupporter->rawSKey, pSupporter->rawEKey);
saveIntervalQueryRange(pRuntimeEnv, pMeterQueryInfo); // saveIntervalQueryRange(pRuntimeEnv, pMeterQueryInfo);
//
// previous query does not be closed, save the results and close it // // previous query does not be closed, save the results and close it
if (pMeterQueryInfo->lastResRows > 0) { // if (pMeterQueryInfo->lastResRows > 0) {
saveResult(pSupporter, pMeterQueryInfo, pMeterQueryInfo->lastResRows); // saveResult(pSupporter, pMeterQueryInfo, pMeterQueryInfo->lastResRows);
} // }
} else { // } else {
/* current query not completed, continue. do nothing with respect to query range, */ // /* current query not completed, continue. do nothing with respect to query range, */
} // }
} else { } else {
pQuery->skey = key; pQuery->skey = key;
assert(pMeterQueryInfo->lastResRows == 0); assert(pMeterQueryInfo->lastResRows == 0);
// for too small query range, no data in this interval.
if ((QUERY_IS_ASC_QUERY(pQuery) && (pQuery->ekey < pQuery->skey)) || if ((QUERY_IS_ASC_QUERY(pQuery) && (pQuery->ekey < pQuery->skey)) ||
(!QUERY_IS_ASC_QUERY(pQuery) && (pQuery->skey < pQuery->ekey))) { (!QUERY_IS_ASC_QUERY(pQuery) && (pQuery->skey < pQuery->ekey))) {
// for too small query range, no data in this interval.
return; return;
} }
// todo /**
if (pMeterQueryInfo->windowResInfo.prevSKey == 0) { * In handling the both ascending and descending order super table query, we need to find the first qualified
pMeterQueryInfo->windowResInfo.prevSKey = key; * timestamp of this table, and then set the first qualified start timestamp.
* In ascending query, key is the first qualified timestamp. However, in the descending order query, additional
* operations involve.
*/
if (!QUERY_IS_ASC_QUERY(pQuery)) {
} }
STimeWindow win = getActiveSlidingWindow(&pMeterQueryInfo->windowResInfo, key, pQuery); TSKEY skey1, ekey1;
int64_t st = win.skey; TSKEY windowSKey = 0, windowEKey = 0;
STimeWindow win = {.skey = key, pSupporter->rawEKey};
SWindowResInfo* pWindowResInfo = &pMeterQueryInfo->windowResInfo;
doGetAlignedIntervalQueryRangeImpl(pQuery, win.skey, win.skey, win.ekey, &skey1, &ekey1, &windowSKey, &windowEKey);
pWindowResInfo->startTime = windowSKey;
if (pWindowResInfo->prevSKey == 0) {
pWindowResInfo->prevSKey = windowSKey;
}
SWindowResult *pWindowRes = win = getActiveTimeWindow(pWindowResInfo, key, pQuery);
doSetSlidingWindowFromKey(pRuntimeEnv, &pMeterQueryInfo->windowResInfo, (char *)&st, TSDB_KEYSIZE); SWindowResult *pWindowRes = doSetTimeWindowFromKey(pRuntimeEnv, pWindowResInfo, (char *)&win.skey, TSDB_KEYSIZE);
if (pWindowRes == NULL) { if (pWindowRes == NULL) {
return; return;
} }
pWindowRes->window = win; pWindowRes->window = win;
// setGroupOutputBuffer(pRuntimeEnv, pWindowRes);
// initCtxOutputBuf(pRuntimeEnv);
// getAlignedIntervalQueryRange(pRuntimeEnv, pQuery->skey, pSupporter->rawSKey, pSupporter->rawEKey);
// saveIntervalQueryRange(pRuntimeEnv, pMeterQueryInfo);
pMeterQueryInfo->queryRangeSet = 1; pMeterQueryInfo->queryRangeSet = 1;
pMeterQueryInfo->lastKey = win.skey;
pMeterQueryInfo->skey = win.skey;
} }
} }
...@@ -7844,7 +7625,7 @@ static int32_t getNumOfSubset(SMeterQuerySupportObj *pSupporter) { ...@@ -7844,7 +7625,7 @@ static int32_t getNumOfSubset(SMeterQuerySupportObj *pSupporter) {
int32_t totalSubset = 0; int32_t totalSubset = 0;
if (isGroupbyNormalCol(pQuery->pGroupbyExpr) || (pQuery->intervalTime > 0 && pQuery->slidingTime > 0)) { if (isGroupbyNormalCol(pQuery->pGroupbyExpr) || (pQuery->intervalTime > 0 && pQuery->slidingTime > 0)) {
totalSubset = numOfClosedSlidingWindow(&pSupporter->runtimeEnv.windowResInfo); totalSubset = numOfClosedTimeWindow(&pSupporter->runtimeEnv.windowResInfo);
} else { } else {
totalSubset = pSupporter->pSidSet->numOfSubSet; totalSubset = pSupporter->pSidSet->numOfSubSet;
} }
...@@ -7935,20 +7716,45 @@ static void applyIntervalQueryOnBlock(SMeterQuerySupportObj *pSupporter, SMeterD ...@@ -7935,20 +7716,45 @@ static void applyIntervalQueryOnBlock(SMeterQuerySupportObj *pSupporter, SMeterD
SQueryRuntimeEnv *pRuntimeEnv = &pSupporter->runtimeEnv; SQueryRuntimeEnv *pRuntimeEnv = &pSupporter->runtimeEnv;
SQuery * pQuery = pRuntimeEnv->pQuery; SQuery * pQuery = pRuntimeEnv->pQuery;
SMeterQueryInfo * pMeterQueryInfo = pMeterDataInfo->pMeterQInfo; SMeterQueryInfo * pMeterQueryInfo = pMeterDataInfo->pMeterQInfo;
SWindowResInfo* pWindowResInfo = &pMeterQueryInfo->windowResInfo;
int64_t *pPrimaryKey = (int64_t *)pRuntimeEnv->primaryColBuffer->data; int64_t *pPrimaryKey = (int64_t *)pRuntimeEnv->primaryColBuffer->data;
/* /*
* for each block, we need to handle the previous query, since the determination of previous query being completed * for each block, we need to handle the previous query, since the determination of previous query being completed
* or not is based on the start key of current block. * or not is based on the start key of current block.
*/ */
TSKEY key = getNextAccessedKeyInData(pQuery, pPrimaryKey, pBlockInfo, blockStatus); TSKEY key = getNextAccessedKeyInData(pQuery, pPrimaryKey, pBlockInfo, blockStatus);
setIntervalQueryRange(pMeterDataInfo->pMeterQInfo, pSupporter, key); setIntervalQueryRange(pMeterQueryInfo, pSupporter, key);
if (((pQuery->skey > pQuery->ekey) && QUERY_IS_ASC_QUERY(pQuery)) || int32_t forwardStep =
((pQuery->skey < pQuery->ekey) && !QUERY_IS_ASC_QUERY(pQuery))) { getNumOfRowsInTimeWindow(pQuery, pBlockInfo, pPrimaryKey, pQuery->pos, pQuery->ekey, searchFn, true);
return;
int32_t numOfRes = 0;
int64_t st = taosGetTimestampUs();
if (pQuery->numOfFilterCols > 0 || pRuntimeEnv->pTSBuf != NULL) {
numOfRes = rowwiseApplyAllFunctions(pRuntimeEnv, &forwardStep, pFields, pBlockInfo, pWindowResInfo);
} else {
numOfRes = blockwiseApplyAllFunctions(pRuntimeEnv, forwardStep, pFields, pBlockInfo, pWindowResInfo, searchFn);
}
int64_t e = taosGetTimestampUs() - st;
printf("-------------------------------%lld\n", e);
int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQuery->order.order);
if ((pQuery->lastKey > pSupporter->rawEKey && QUERY_IS_ASC_QUERY(pQuery)) ||
(pQuery->lastKey < pSupporter->rawEKey && !QUERY_IS_ASC_QUERY(pQuery))) {
pMeterQueryInfo->ekey = pQuery->lastKey - step;
} }
// doCheckQueryCompleted(pRuntimeEnv, pQuery->lastKey, pWindowResInfo);
//
// if (((pQuery->skey > pQuery->ekey) && QUERY_IS_ASC_QUERY(pQuery)) ||
// ((pQuery->skey < pQuery->ekey) && !QUERY_IS_ASC_QUERY(pQuery))) {
// return;
// }
// if (((pBlockInfo->keyLast < pQuery->ekey) && QUERY_IS_ASC_QUERY(pQuery)) || // if (((pBlockInfo->keyLast < pQuery->ekey) && QUERY_IS_ASC_QUERY(pQuery)) ||
// ((pBlockInfo->keyFirst > pQuery->ekey) && !QUERY_IS_ASC_QUERY(pQuery))) { // ((pBlockInfo->keyFirst > pQuery->ekey) && !QUERY_IS_ASC_QUERY(pQuery))) {
// int32_t numOfRes = 0; // int32_t numOfRes = 0;
...@@ -7963,7 +7769,7 @@ static void applyIntervalQueryOnBlock(SMeterQuerySupportObj *pSupporter, SMeterD ...@@ -7963,7 +7769,7 @@ static void applyIntervalQueryOnBlock(SMeterQuerySupportObj *pSupporter, SMeterD
// assert(pMeterQueryInfo->lastResRows == 1 || pMeterQueryInfo->lastResRows == 0); // assert(pMeterQueryInfo->lastResRows == 1 || pMeterQueryInfo->lastResRows == 0);
// saveIntervalQueryRange(pRuntimeEnv, pMeterQueryInfo); // saveIntervalQueryRange(pRuntimeEnv, pMeterQueryInfo);
// } else { // } else {
doApplyIntervalQueryOnBlock_rv(pSupporter, pMeterQueryInfo, pBlockInfo, pPrimaryKey, pFields, searchFn); // doApplyIntervalQueryOnBlock_rv(pSupporter, pMeterQueryInfo, pBlockInfo, pPrimaryKey, pFields, searchFn);
// } // }
} }
......
...@@ -241,7 +241,7 @@ static void queryOnMultiDataCache(SQInfo *pQInfo, SMeterDataInfo *pMeterInfo) { ...@@ -241,7 +241,7 @@ static void queryOnMultiDataCache(SQInfo *pQInfo, SMeterDataInfo *pMeterInfo) {
pRuntimeEnv->blockStatus); pRuntimeEnv->blockStatus);
totalBlocks++; totalBlocks++;
queryOnBlock(pSupporter, primaryKeys, pRuntimeEnv->blockStatus, &binfo, &pMeterInfo[k], NULL, searchFn); queryOnBlock(pSupporter, pRuntimeEnv->blockStatus, &binfo, &pMeterInfo[k], NULL, searchFn);
if (ALL_CACHE_BLOCKS_CHECKED(pQuery)) { if (ALL_CACHE_BLOCKS_CHECKED(pQuery)) {
break; break;
...@@ -447,24 +447,8 @@ static void queryOnMultiDataFiles(SQInfo *pQInfo, SMeterDataInfo *pMeterDataInfo ...@@ -447,24 +447,8 @@ static void queryOnMultiDataFiles(SQInfo *pQInfo, SMeterDataInfo *pMeterDataInfo
(pBlock->keyFirst >= pQuery->ekey && pBlock->keyLast <= pQuery->lastKey && !QUERY_IS_ASC_QUERY(pQuery))); (pBlock->keyFirst >= pQuery->ekey && pBlock->keyLast <= pQuery->lastKey && !QUERY_IS_ASC_QUERY(pQuery)));
} }
if (pQuery->intervalTime > 0 && pQuery->slidingTime > 0) { if (pQuery->intervalTime > 0 && pQuery->slidingTime > 0) {
assert(pMeterQueryInfo->lastKey <= nextKey && QUERY_IS_ASC_QUERY(pQuery)); setIntervalQueryRange(pMeterQueryInfo, pSupporter, nextKey);
pMeterQueryInfo->lastKey = nextKey;
pQuery->lastKey = nextKey;
if (pMeterQueryInfo->windowResInfo.prevSKey == 0) {
// normalize the window prev time window
TSKEY skey1, ekey1;
TSKEY windowSKey = 0, windowEKey = 0;
TSKEY skey2 = MIN(pSupporter->rawSKey, pSupporter->rawEKey);
TSKEY ekey2 = MAX(pSupporter->rawSKey, pSupporter->rawEKey);
doGetAlignedIntervalQueryRangeImpl(pQuery, nextKey, skey2, ekey2, &skey1, &ekey1, &windowSKey, &windowEKey);
pMeterQueryInfo->windowResInfo.prevSKey = windowSKey;
}
ret = setIntervalQueryExecutionContext(pSupporter, pOneMeterDataInfo->meterOrderIdx, pMeterQueryInfo); ret = setIntervalQueryExecutionContext(pSupporter, pOneMeterDataInfo->meterOrderIdx, pMeterQueryInfo);
if (ret != TSDB_CODE_SUCCESS) { if (ret != TSDB_CODE_SUCCESS) {
...@@ -474,8 +458,7 @@ static void queryOnMultiDataFiles(SQInfo *pQInfo, SMeterDataInfo *pMeterDataInfo ...@@ -474,8 +458,7 @@ static void queryOnMultiDataFiles(SQInfo *pQInfo, SMeterDataInfo *pMeterDataInfo
} }
} }
queryOnBlock(pSupporter, primaryKeys, pRuntimeEnv->blockStatus, &binfo, pOneMeterDataInfo, pInfoEx->pBlock.fields, queryOnBlock(pSupporter, pRuntimeEnv->blockStatus, &binfo, pOneMeterDataInfo, pInfoEx->pBlock.fields, searchFn);
searchFn);
} }
tfree(pReqMeterDataInfo); tfree(pReqMeterDataInfo);
...@@ -711,7 +694,7 @@ static void vnodeSTableSeqProcessor(SQInfo *pQInfo) { ...@@ -711,7 +694,7 @@ static void vnodeSTableSeqProcessor(SQInfo *pQInfo) {
} }
resetCtxOutputBuf(pRuntimeEnv); resetCtxOutputBuf(pRuntimeEnv);
resetSlidingWindowInfo(pRuntimeEnv, &pRuntimeEnv->windowResInfo); resetTimeWindowInfo(pRuntimeEnv, &pRuntimeEnv->windowResInfo);
while (pSupporter->meterIdx < pSupporter->numOfMeters) { while (pSupporter->meterIdx < pSupporter->numOfMeters) {
int32_t k = pSupporter->meterIdx; int32_t k = pSupporter->meterIdx;
...@@ -1115,7 +1098,7 @@ static void vnodeSingleMeterIntervalMainLooper(SMeterQuerySupportObj *pSupporter ...@@ -1115,7 +1098,7 @@ static void vnodeSingleMeterIntervalMainLooper(SMeterQuerySupportObj *pSupporter
while (1) { while (1) {
initCtxOutputBuf(pRuntimeEnv); initCtxOutputBuf(pRuntimeEnv);
clearClosedSlidingWindows(pRuntimeEnv); clearClosedTimeWindow(pRuntimeEnv);
vnodeScanAllData(pRuntimeEnv); vnodeScanAllData(pRuntimeEnv);
if (isQueryKilled(pQuery)) { if (isQueryKilled(pQuery)) {
...@@ -1124,8 +1107,6 @@ static void vnodeSingleMeterIntervalMainLooper(SMeterQuerySupportObj *pSupporter ...@@ -1124,8 +1107,6 @@ static void vnodeSingleMeterIntervalMainLooper(SMeterQuerySupportObj *pSupporter
assert(!Q_STATUS_EQUAL(pQuery->over, QUERY_NOT_COMPLETED)); assert(!Q_STATUS_EQUAL(pQuery->over, QUERY_NOT_COMPLETED));
// clear tag, used to decide if the whole interval query is completed or not
pQuery->over &= (~QUERY_COMPLETED);
doFinalizeResult(pRuntimeEnv); doFinalizeResult(pRuntimeEnv);
int64_t maxOutput = getNumOfResult(pRuntimeEnv); int64_t maxOutput = getNumOfResult(pRuntimeEnv);
...@@ -1143,7 +1124,7 @@ static void vnodeSingleMeterIntervalMainLooper(SMeterQuerySupportObj *pSupporter ...@@ -1143,7 +1124,7 @@ static void vnodeSingleMeterIntervalMainLooper(SMeterQuerySupportObj *pSupporter
// forwardCtxOutputBuf(pRuntimeEnv, maxOutput); // forwardCtxOutputBuf(pRuntimeEnv, maxOutput);
} }
if (Q_STATUS_EQUAL(pQuery->over, QUERY_NO_DATA_TO_CHECK)) { if (Q_STATUS_EQUAL(pQuery->over, QUERY_NO_DATA_TO_CHECK|QUERY_COMPLETED)) {
break; break;
} }
......
...@@ -670,7 +670,7 @@ void *vnodeQueryOnSingleTable(SMeterObj **pMetersObj, SSqlGroupbyExpr *pGroupbyE ...@@ -670,7 +670,7 @@ void *vnodeQueryOnSingleTable(SMeterObj **pMetersObj, SSqlGroupbyExpr *pGroupbyE
tsBufNextPos(pTSBuf); tsBufNextPos(pTSBuf);
} }
if (((*code) = vnodeQuerySingleMeterPrepare(pQInfo, pQInfo->pObj, pSupporter, pTSBuf)) != TSDB_CODE_SUCCESS) { if (((*code) = vnodeQuerySingleTablePrepare(pQInfo, pQInfo->pObj, pSupporter, pTSBuf)) != TSDB_CODE_SUCCESS) {
goto _error; goto _error;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册