2021-11-02 05:37:31 +00:00
/*
* Copyright ( c ) 2019 TAOS Data , Inc . < jhtao @ taosdata . com >
*
* This program is free software : you can use , redistribute , and / or modify
* it under the terms of the GNU Affero General Public License , version 3
* or later ( " AGPL " ) , as published by the Free Software Foundation .
*
* This program is distributed in the hope that it will be useful , but WITHOUT
* ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2022-02-28 08:35:36 +00:00
2022-04-04 06:54:39 +00:00
# include "filter.h"
# include "function.h"
2022-04-20 06:59:06 +00:00
# include "functionMgt.h"
# include "os.h"
2022-04-04 06:54:39 +00:00
# include "querynodes.h"
2022-04-20 06:59:06 +00:00
# include "tfill.h"
2022-04-23 10:29:45 +00:00
# include "tname.h"
2022-06-16 01:31:22 +00:00
# include "tref.h"
2022-02-22 05:12:03 +00:00
2022-03-04 05:25:39 +00:00
# include "tdatablock.h"
2021-11-02 05:37:31 +00:00
# include "tglobal.h"
2022-01-20 09:10:28 +00:00
# include "tmsg.h"
2022-03-04 05:25:39 +00:00
# include "tsort.h"
2021-11-05 02:35:50 +00:00
# include "ttime.h"
2022-01-08 08:28:44 +00:00
2021-11-02 05:37:31 +00:00
# include "executorimpl.h"
2022-05-27 10:13:22 +00:00
# include "index.h"
2022-03-15 06:37:26 +00:00
# include "query.h"
2021-11-02 05:37:31 +00:00
# include "tcompare.h"
# include "tcompression.h"
2022-01-08 14:59:24 +00:00
# include "thash.h"
2021-11-02 05:37:31 +00:00
# include "ttypes.h"
2022-04-23 10:29:45 +00:00
# include "vnode.h"
2021-11-02 05:37:31 +00:00
2022-01-08 14:59:24 +00:00
# define IS_MAIN_SCAN(runtime) ((runtime)->scanFlag == MAIN_SCAN)
2021-11-02 05:37:31 +00:00
# define SET_REVERSE_SCAN_FLAG(runtime) ((runtime)->scanFlag = REVERSE_SCAN)
# define GET_FORWARD_DIRECTION_FACTOR(ord) (((ord) == TSDB_ORDER_ASC) ? QUERY_ASC_FORWARD_STEP : QUERY_DESC_FORWARD_STEP)
#if 0
static UNUSED_FUNC void * u_malloc ( size_t __size ) {
2022-03-09 16:36:30 +00:00
uint32_t v = taosRand ( ) ;
2021-11-02 05:37:31 +00:00
if ( v % 1000 < = 0 ) {
return NULL ;
} else {
2022-03-25 16:29:53 +00:00
return taosMemoryMalloc ( __size ) ;
2021-11-02 05:37:31 +00:00
}
}
static UNUSED_FUNC void * u_calloc ( size_t num , size_t __size ) {
2022-03-09 16:36:30 +00:00
uint32_t v = taosRand ( ) ;
2021-11-02 05:37:31 +00:00
if ( v % 1000 < = 0 ) {
return NULL ;
} else {
2022-03-25 16:29:53 +00:00
return taosMemoryCalloc ( num , __size ) ;
2021-11-02 05:37:31 +00:00
}
}
static UNUSED_FUNC void * u_realloc ( void * p , size_t __size ) {
2022-03-09 16:36:30 +00:00
uint32_t v = taosRand ( ) ;
2021-11-02 05:37:31 +00:00
if ( v % 5 < = 1 ) {
return NULL ;
} else {
2022-03-25 16:29:53 +00:00
return taosMemoryRealloc ( p , __size ) ;
2021-11-02 05:37:31 +00:00
}
}
# define calloc u_calloc
# define malloc u_malloc
# define realloc u_realloc
# endif
2022-06-16 01:31:22 +00:00
# define CLEAR_QUERY_STATUS(q, st) ((q)->status &= (~(st)))
2021-11-02 05:37:31 +00:00
# define QUERY_IS_INTERVAL_QUERY(_q) ((_q)->interval.interval > 0)
2022-03-29 07:24:25 +00:00
int32_t getMaximumIdleDurationSec ( ) { return tsShellActivityTimer * 2 ; }
static int32_t getExprFunctionId ( SExprInfo * pExprInfo ) {
2021-11-02 05:37:31 +00:00
assert ( pExprInfo ! = NULL & & pExprInfo - > pExpr ! = NULL & & pExprInfo - > pExpr - > nodeType = = TEXPR_UNARYEXPR_NODE ) ;
2021-11-08 02:32:06 +00:00
return 0 ;
2021-11-02 05:37:31 +00:00
}
static void doSetTagValueToResultBuf ( char * output , const char * val , int16_t type , int16_t bytes ) ;
2022-04-18 02:46:07 +00:00
static void setBlockStatisInfo ( SqlFunctionCtx * pCtx , SExprInfo * pExpr , SSDataBlock * pSDataBlock ) ;
2021-11-02 05:37:31 +00:00
2022-05-23 11:50:08 +00:00
static void releaseQueryBuf ( size_t numOfTables ) ;
2021-11-02 05:37:31 +00:00
static void destroySFillOperatorInfo ( void * param , int32_t numOfOutput ) ;
static void destroyProjectOperatorInfo ( void * param , int32_t numOfOutput ) ;
static void destroyOrderOperatorInfo ( void * param , int32_t numOfOutput ) ;
static void destroyAggOperatorInfo ( void * param , int32_t numOfOutput ) ;
2022-03-21 06:00:30 +00:00
2022-03-14 06:15:26 +00:00
static void destroyIntervalOperatorInfo ( void * param , int32_t numOfOutput ) ;
2022-03-04 07:53:30 +00:00
static void destroyExchangeOperatorInfo ( void * param , int32_t numOfOutput ) ;
2021-11-02 05:37:31 +00:00
static void destroyOperatorInfo ( SOperatorInfo * pOperator ) ;
2022-04-06 09:59:08 +00:00
void doSetOperatorCompleted ( SOperatorInfo * pOperator ) {
2021-11-02 05:37:31 +00:00
pOperator - > status = OP_EXEC_DONE ;
2022-05-24 03:29:51 +00:00
2022-05-26 08:29:52 +00:00
pOperator - > cost . totalCost = ( taosGetTimestampUs ( ) - pOperator - > pTaskInfo - > cost . start * 1000 ) / 1000.0 ;
2022-01-08 14:59:24 +00:00
if ( pOperator - > pTaskInfo ! = NULL ) {
2022-01-10 11:48:21 +00:00
setTaskStatus ( pOperator - > pTaskInfo , TASK_COMPLETED ) ;
2021-11-02 05:37:31 +00:00
}
}
2022-04-06 09:59:08 +00:00
2022-04-04 06:54:39 +00:00
int32_t operatorDummyOpenFn ( SOperatorInfo * pOperator ) {
2022-03-12 07:28:15 +00:00
OPTR_SET_OPENED ( pOperator ) ;
2022-05-03 15:23:49 +00:00
pOperator - > cost . openCost = 0 ;
2022-03-05 07:58:28 +00:00
return TSDB_CODE_SUCCESS ;
2022-03-04 07:53:30 +00:00
}
2022-04-26 12:26:32 +00:00
SOperatorFpSet createOperatorFpSet ( __optr_open_fn_t openFn , __optr_fn_t nextFn , __optr_fn_t streamFn ,
2022-04-28 08:31:35 +00:00
__optr_fn_t cleanup , __optr_close_fn_t closeFn , __optr_encode_fn_t encode ,
2022-05-24 03:29:51 +00:00
__optr_decode_fn_t decode , __optr_explain_fn_t explain ) {
2022-04-26 12:26:32 +00:00
SOperatorFpSet fpSet = {
. _openFn = openFn ,
. getNextFn = nextFn ,
. getStreamResFn = streamFn ,
. cleanupFn = cleanup ,
. closeFn = closeFn ,
. encodeResultRow = encode ,
. decodeResultRow = decode ,
. getExplainFn = explain ,
} ;
return fpSet ;
}
2022-04-04 06:54:39 +00:00
void operatorDummyCloseFn ( void * param , int32_t numOfCols ) { }
2022-03-05 07:58:28 +00:00
2022-05-23 11:50:08 +00:00
static int32_t doCopyToSDataBlock ( SExecTaskInfo * taskInfo , SSDataBlock * pBlock , SExprInfo * pExprInfo ,
SDiskbasedBuf * pBuf , SGroupResInfo * pGroupResInfo , const int32_t * rowCellOffset ,
SqlFunctionCtx * pCtx , int32_t numOfExprs ) ;
2022-02-24 09:18:56 +00:00
2022-04-22 07:07:20 +00:00
static void initCtxOutputBuffer ( SqlFunctionCtx * pCtx , int32_t size ) ;
2022-06-20 06:40:13 +00:00
static void doSetTableGroupOutputBuf ( SOperatorInfo * pOperator , SAggOperatorInfo * pAggInfo , int32_t numOfOutput ,
uint64_t groupId ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// setup the output buffer for each operator
static bool hasNull ( SColumn * pColumn , SColumnDataAgg * pStatis ) {
2022-04-23 10:29:45 +00:00
if ( TSDB_COL_IS_TAG ( pColumn - > flag ) | | TSDB_COL_IS_UD_COL ( pColumn - > flag ) | |
pColumn - > colId = = PRIMARYKEY_TIMESTAMP_COL_ID ) {
2021-11-02 05:37:31 +00:00
return false ;
}
if ( pStatis ! = NULL & & pStatis - > numOfNull = = 0 ) {
return false ;
}
return true ;
}
2022-06-18 04:00:41 +00:00
#if 0
2022-03-29 07:24:25 +00:00
static bool chkResultRowFromKey ( STaskRuntimeEnv * pRuntimeEnv , SResultRowInfo * pResultRowInfo , char * pData ,
int16_t bytes , bool masterscan , uint64_t uid ) {
2021-11-02 05:37:31 +00:00
bool existed = false ;
SET_RES_WINDOW_KEY ( pRuntimeEnv - > keyBuf , pData , bytes , uid ) ;
2022-03-29 07:24:25 +00:00
SResultRow * * p1 =
( SResultRow * * ) taosHashGet ( pRuntimeEnv - > pResultRowHashTable , pRuntimeEnv - > keyBuf , GET_RES_WINDOW_KEY_LEN ( bytes ) ) ;
2021-11-02 05:37:31 +00:00
// in case of repeat scan/reverse scan, no new time window added.
if ( QUERY_IS_INTERVAL_QUERY ( pRuntimeEnv - > pQueryAttr ) ) {
if ( ! masterscan ) { // the *p1 may be NULL in case of sliding+offset exists.
return p1 ! = NULL ;
}
if ( p1 ! = NULL ) {
if ( pResultRowInfo - > size = = 0 ) {
existed = false ;
} else if ( pResultRowInfo - > size = = 1 ) {
2022-04-23 10:29:45 +00:00
// existed = (pResultRowInfo->pResult[0] == (*p1));
2021-11-02 05:37:31 +00:00
} else { // check if current pResultRowInfo contains the existed pResultRow
SET_RES_EXT_WINDOW_KEY ( pRuntimeEnv - > keyBuf , pData , bytes , uid , pResultRowInfo ) ;
2022-03-29 07:24:25 +00:00
int64_t * index =
taosHashGet ( pRuntimeEnv - > pResultRowListSet , pRuntimeEnv - > keyBuf , GET_RES_EXT_WINDOW_KEY_LEN ( bytes ) ) ;
2021-11-02 05:37:31 +00:00
if ( index ! = NULL ) {
existed = true ;
} else {
existed = false ;
}
}
}
return existed ;
}
return p1 ! = NULL ;
}
2022-06-18 04:00:41 +00:00
# endif
2021-11-02 05:37:31 +00:00
2022-05-29 04:35:11 +00:00
SResultRow * getNewResultRow ( SDiskbasedBuf * pResultBuf , int64_t tableGroupId , int32_t interBufSize ) {
2022-03-29 07:24:25 +00:00
SFilePage * pData = NULL ;
2022-03-15 06:37:26 +00:00
// in the first scan, new space needed for results
int32_t pageId = - 1 ;
SIDList list = getDataBufPagesIdList ( pResultBuf , tableGroupId ) ;
if ( taosArrayGetSize ( list ) = = 0 ) {
pData = getNewBufPage ( pResultBuf , tableGroupId , & pageId ) ;
pData - > num = sizeof ( SFilePage ) ;
} else {
SPageInfo * pi = getLastPageInfo ( list ) ;
pData = getBufPage ( pResultBuf , getPageId ( pi ) ) ;
pageId = getPageId ( pi ) ;
2022-04-11 06:09:47 +00:00
if ( pData - > num + interBufSize > getBufPageSize ( pResultBuf ) ) {
2022-03-15 06:37:26 +00:00
// release current page first, and prepare the next one
releaseBufPageInfo ( pResultBuf , pi ) ;
pData = getNewBufPage ( pResultBuf , tableGroupId , & pageId ) ;
if ( pData ! = NULL ) {
pData - > num = sizeof ( SFilePage ) ;
}
}
}
if ( pData = = NULL ) {
return NULL ;
}
2022-05-20 15:59:17 +00:00
setBufPageDirty ( pData , true ) ;
2022-03-15 06:37:26 +00:00
// set the number of rows in current disk page
SResultRow * pResultRow = ( SResultRow * ) ( ( char * ) pData + pData - > num ) ;
pResultRow - > pageId = pageId ;
pResultRow - > offset = ( int32_t ) pData - > num ;
2022-04-11 06:09:47 +00:00
pData - > num + = interBufSize ;
2022-03-15 06:37:26 +00:00
return pResultRow ;
}
2022-04-24 12:48:42 +00:00
/**
* the struct of key in hash table
* + - - - - - - - - - - + - - - - - - - - - - - - - - - +
* | group id | key data |
* | 8 bytes | actual length |
* + - - - - - - - - - - + - - - - - - - - - - - - - - - +
*/
2022-05-03 06:43:53 +00:00
SResultRow * doSetResultOutBufByKey ( SDiskbasedBuf * pResultBuf , SResultRowInfo * pResultRowInfo , char * pData ,
int16_t bytes , bool masterscan , uint64_t groupId , SExecTaskInfo * pTaskInfo ,
bool isIntervalQuery , SAggSupporter * pSup ) {
2022-04-16 02:00:25 +00:00
SET_RES_WINDOW_KEY ( pSup - > keyBuf , pData , bytes , groupId ) ;
2022-01-20 05:52:46 +00:00
2022-04-23 10:29:45 +00:00
SResultRowPosition * p1 =
( SResultRowPosition * ) taosHashGet ( pSup - > pResultRowHashTable , pSup - > keyBuf , GET_RES_WINDOW_KEY_LEN ( bytes ) ) ;
2022-01-20 05:52:46 +00:00
2022-04-25 08:44:48 +00:00
SResultRow * pResult = NULL ;
2022-01-20 05:52:46 +00:00
// in case of repeat scan/reverse scan, no new time window added.
if ( isIntervalQuery ) {
2022-04-25 08:44:48 +00:00
if ( masterscan & & p1 ! = NULL ) { // the *p1 may be NULL in case of sliding+offset exists.
pResult = getResultRowByPos ( pResultBuf , p1 ) ;
2022-05-20 15:59:17 +00:00
ASSERT ( pResult - > pageId = = p1 - > pageId & & pResult - > offset = = p1 - > offset ) ;
2022-01-20 05:52:46 +00:00
}
} else {
2022-04-23 10:29:45 +00:00
// In case of group by column query, the required SResultRow object must be existInCurrentResusltRowInfo in the
// pResultRowInfo object.
2022-01-20 05:52:46 +00:00
if ( p1 ! = NULL ) {
2022-05-30 12:10:30 +00:00
// todo
2022-04-25 08:44:48 +00:00
pResult = getResultRowByPos ( pResultBuf , p1 ) ;
2022-05-20 15:59:17 +00:00
ASSERT ( pResult - > pageId = = p1 - > pageId & & pResult - > offset = = p1 - > offset ) ;
2022-01-20 05:52:46 +00:00
}
}
2022-04-28 08:31:35 +00:00
// 1. close current opened time window
2022-06-28 03:48:10 +00:00
if ( pResultRowInfo - > cur . pageId ! = - 1 & & ( ( pResult = = NULL ) | | ( pResult - > pageId ! = pResultRowInfo - > cur . pageId ) ) ) {
2022-06-29 10:11:28 +00:00
# ifdef BUF_PAGE_DEBUG
2022-06-29 07:03:17 +00:00
qDebug ( " page_1 " ) ;
2022-06-29 10:11:28 +00:00
# endif
2022-04-25 08:44:48 +00:00
SResultRowPosition pos = pResultRowInfo - > cur ;
2022-06-04 11:54:55 +00:00
SFilePage * pPage = getBufPage ( pResultBuf , pos . pageId ) ;
2022-04-25 08:44:48 +00:00
releaseBufPage ( pResultBuf , pPage ) ;
}
// allocate a new buffer page
if ( pResult = = NULL ) {
2022-06-29 10:11:28 +00:00
# ifdef BUF_PAGE_DEBUG
2022-06-29 07:03:17 +00:00
qDebug ( " page_2 " ) ;
2022-06-29 10:11:28 +00:00
# endif
2022-05-08 08:06:47 +00:00
ASSERT ( pSup - > resultRowSize > 0 ) ;
2022-05-29 04:35:11 +00:00
pResult = getNewResultRow ( pResultBuf , groupId , pSup - > resultRowSize ) ;
2022-04-25 08:44:48 +00:00
initResultRow ( pResult ) ;
2022-01-20 05:52:46 +00:00
2022-04-25 08:44:48 +00:00
// add a new result set for a new group
SResultRowPosition pos = { . pageId = pResult - > pageId , . offset = pResult - > offset } ;
2022-06-04 11:54:55 +00:00
taosHashPut ( pSup - > pResultRowHashTable , pSup - > keyBuf , GET_RES_WINDOW_KEY_LEN ( bytes ) , & pos ,
sizeof ( SResultRowPosition ) ) ;
2022-01-20 05:52:46 +00:00
}
2022-04-25 08:44:48 +00:00
// 2. set the new time window to be the new active time window
pResultRowInfo - > cur = ( SResultRowPosition ) { . pageId = pResult - > pageId , . offset = pResult - > offset } ;
2022-01-20 05:52:46 +00:00
// too many time window in query
2022-05-29 04:35:11 +00:00
if ( taosHashGetSize ( pSup - > pResultRowHashTable ) > MAX_INTERVAL_TIME_WINDOW ) {
2022-01-20 05:52:46 +00:00
longjmp ( pTaskInfo - > env , TSDB_CODE_QRY_TOO_MANY_TIMEWINDOW ) ;
}
2022-03-31 08:10:32 +00:00
return pResult ;
2022-01-20 05:52:46 +00:00
}
2021-11-02 05:37:31 +00:00
// a new buffer page for each table. Needs to opt this design
2022-03-29 07:24:25 +00:00
static int32_t addNewWindowResultBuf ( SResultRow * pWindowRes , SDiskbasedBuf * pResultBuf , int32_t tid , uint32_t size ) {
2021-11-02 05:37:31 +00:00
if ( pWindowRes - > pageId ! = - 1 ) {
return 0 ;
}
2022-03-29 07:24:25 +00:00
SFilePage * pData = NULL ;
2021-11-02 05:37:31 +00:00
// in the first scan, new space needed for results
int32_t pageId = - 1 ;
SIDList list = getDataBufPagesIdList ( pResultBuf , tid ) ;
if ( taosArrayGetSize ( list ) = = 0 ) {
2022-02-23 07:10:15 +00:00
pData = getNewBufPage ( pResultBuf , tid , & pageId ) ;
2022-03-15 06:37:26 +00:00
pData - > num = sizeof ( SFilePage ) ;
2021-11-02 05:37:31 +00:00
} else {
SPageInfo * pi = getLastPageInfo ( list ) ;
2022-02-10 05:49:17 +00:00
pData = getBufPage ( pResultBuf , getPageId ( pi ) ) ;
2022-02-08 02:21:00 +00:00
pageId = getPageId ( pi ) ;
2021-11-02 05:37:31 +00:00
2022-02-08 02:21:00 +00:00
if ( pData - > num + size > getBufPageSize ( pResultBuf ) ) {
2021-11-02 05:37:31 +00:00
// release current page first, and prepare the next one
2022-02-10 05:49:17 +00:00
releaseBufPageInfo ( pResultBuf , pi ) ;
2022-03-15 06:37:26 +00:00
2022-02-23 07:10:15 +00:00
pData = getNewBufPage ( pResultBuf , tid , & pageId ) ;
2021-11-02 05:37:31 +00:00
if ( pData ! = NULL ) {
2022-03-15 06:37:26 +00:00
pData - > num = sizeof ( SFilePage ) ;
2021-11-02 05:37:31 +00:00
}
}
}
if ( pData = = NULL ) {
return - 1 ;
}
// set the number of rows in current disk page
if ( pWindowRes - > pageId = = - 1 ) { // not allocated yet, allocate new buffer
pWindowRes - > pageId = pageId ;
pWindowRes - > offset = ( int32_t ) pData - > num ;
pData - > num + = size ;
assert ( pWindowRes - > pageId > = 0 ) ;
}
return 0 ;
}
2022-03-30 05:41:15 +00:00
// query_range_start, query_range_end, window_duration, window_start, window_end
2022-05-03 06:43:53 +00:00
void initExecTimeWindowInfo ( SColumnInfoData * pColData , STimeWindow * pQueryWindow ) {
2022-03-30 05:41:15 +00:00
pColData - > info . type = TSDB_DATA_TYPE_TIMESTAMP ;
pColData - > info . bytes = sizeof ( int64_t ) ;
2022-06-22 09:21:04 +00:00
colInfoDataEnsureCapacity ( pColData , 5 ) ;
2022-03-30 05:41:15 +00:00
colDataAppendInt64 ( pColData , 0 , & pQueryWindow - > skey ) ;
colDataAppendInt64 ( pColData , 1 , & pQueryWindow - > ekey ) ;
int64_t interval = 0 ;
colDataAppendInt64 ( pColData , 2 , & interval ) ; // this value may be variable in case of 'n' and 'y'.
colDataAppendInt64 ( pColData , 3 , & pQueryWindow - > skey ) ;
colDataAppendInt64 ( pColData , 4 , & pQueryWindow - > ekey ) ;
}
2022-05-23 11:50:08 +00:00
void doApplyFunctions ( SExecTaskInfo * taskInfo , SqlFunctionCtx * pCtx , STimeWindow * pWin ,
SColumnInfoData * pTimeWindowData , int32_t offset , int32_t forwardStep , TSKEY * tsCol ,
int32_t numOfTotal , int32_t numOfOutput , int32_t order ) {
2021-11-02 05:37:31 +00:00
for ( int32_t k = 0 ; k < numOfOutput ; + + k ) {
2022-04-06 11:46:38 +00:00
// keep it temporarily
2022-05-30 12:10:30 +00:00
// todo no need this??
2022-04-23 10:29:45 +00:00
bool hasAgg = pCtx [ k ] . input . colDataAggIsSet ;
int32_t numOfRows = pCtx [ k ] . input . numOfRows ;
2022-03-22 08:03:42 +00:00
int32_t startOffset = pCtx [ k ] . input . startRowIndex ;
2021-11-02 05:37:31 +00:00
2022-05-19 08:49:43 +00:00
pCtx [ k ] . input . startRowIndex = offset ;
2022-03-15 06:37:26 +00:00
pCtx [ k ] . input . numOfRows = forwardStep ;
2021-11-02 05:37:31 +00:00
// not a whole block involved in query processing, statistics data can not be used
// NOTE: the original value of isSet have been changed here
2022-05-17 08:53:55 +00:00
if ( pCtx [ k ] . input . colDataAggIsSet & & forwardStep < numOfTotal ) {
pCtx [ k ] . input . colDataAggIsSet = false ;
2021-11-02 05:37:31 +00:00
}
2022-03-30 05:41:15 +00:00
if ( fmIsWindowPseudoColumnFunc ( pCtx [ k ] . functionId ) ) {
SResultRowEntryInfo * pEntryInfo = GET_RES_INFO ( & pCtx [ k ] ) ;
2022-05-30 12:10:30 +00:00
char * p = GET_ROWCELL_INTERBUF ( pEntryInfo ) ;
2022-03-30 05:41:15 +00:00
2022-04-02 05:32:26 +00:00
SColumnInfoData idata = { 0 } ;
2022-04-23 10:29:45 +00:00
idata . info . type = TSDB_DATA_TYPE_BIGINT ;
2022-04-02 05:32:26 +00:00
idata . info . bytes = tDataTypes [ TSDB_DATA_TYPE_BIGINT ] . bytes ;
2022-04-23 10:29:45 +00:00
idata . pData = p ;
2022-04-02 05:32:26 +00:00
SScalarParam out = { . columnData = & idata } ;
SScalarParam tw = { . numOfRows = 5 , . columnData = pTimeWindowData } ;
pCtx [ k ] . sfp . process ( & tw , 1 , & out ) ;
2022-03-30 05:41:15 +00:00
pEntryInfo - > numOfRes = 1 ;
2022-05-30 12:10:30 +00:00
} else {
int32_t code = TSDB_CODE_SUCCESS ;
if ( functionNeedToExecute ( & pCtx [ k ] ) & & pCtx [ k ] . fpSet . process ! = NULL ) {
code = pCtx [ k ] . fpSet . process ( & pCtx [ k ] ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " %s apply functions error, code: %s " , GET_TASKID ( taskInfo ) , tstrerror ( code ) ) ;
taskInfo - > code = code ;
longjmp ( taskInfo - > env , code ) ;
}
2022-05-12 01:57:43 +00:00
}
2021-11-02 05:37:31 +00:00
2022-05-30 12:10:30 +00:00
// restore it
pCtx [ k ] . input . colDataAggIsSet = hasAgg ;
pCtx [ k ] . input . startRowIndex = startOffset ;
pCtx [ k ] . input . numOfRows = numOfRows ;
}
2021-11-02 05:37:31 +00:00
}
}
2022-04-23 10:29:45 +00:00
static int32_t doSetInputDataBlock ( SOperatorInfo * pOperator , SqlFunctionCtx * pCtx , SSDataBlock * pBlock , int32_t order ,
2022-05-12 09:33:36 +00:00
int32_t scanFlag , bool createDummyCol ) ;
2021-11-04 05:24:19 +00:00
2022-04-23 10:29:45 +00:00
static void doSetInputDataBlockInfo ( SOperatorInfo * pOperator , SqlFunctionCtx * pCtx , SSDataBlock * pBlock ,
int32_t order ) {
2022-06-18 04:00:41 +00:00
for ( int32_t i = 0 ; i < pOperator - > exprSupp . numOfExprs ; + + i ) {
2021-11-02 05:37:31 +00:00
pCtx [ i ] . order = order ;
2022-05-17 08:53:55 +00:00
pCtx [ i ] . input . numOfRows = pBlock - > info . rows ;
2022-06-18 04:00:41 +00:00
setBlockStatisInfo ( & pCtx [ i ] , & pOperator - > exprSupp . pExprInfo [ i ] , pBlock ) ;
2021-11-02 05:37:31 +00:00
}
}
2022-05-23 11:50:08 +00:00
void setInputDataBlock ( SOperatorInfo * pOperator , SqlFunctionCtx * pCtx , SSDataBlock * pBlock , int32_t order ,
int32_t scanFlag , bool createDummyCol ) {
2022-04-16 03:47:50 +00:00
if ( pBlock - > pBlockAgg ! = NULL ) {
2022-03-09 02:22:53 +00:00
doSetInputDataBlockInfo ( pOperator , pCtx , pBlock , order ) ;
2022-04-16 03:47:50 +00:00
} else {
2022-05-12 09:33:36 +00:00
doSetInputDataBlock ( pOperator , pCtx , pBlock , order , scanFlag , createDummyCol ) ;
2022-03-09 02:22:53 +00:00
}
2021-11-02 05:37:31 +00:00
}
2022-04-28 08:31:35 +00:00
static int32_t doCreateConstantValColumnInfo ( SInputColumnInfoData * pInput , SFunctParam * pFuncParam , int32_t paramIndex ,
int32_t numOfRows ) {
2022-04-18 02:46:07 +00:00
SColumnInfoData * pColInfo = NULL ;
if ( pInput - > pData [ paramIndex ] = = NULL ) {
pColInfo = taosMemoryCalloc ( 1 , sizeof ( SColumnInfoData ) ) ;
if ( pColInfo = = NULL ) {
return TSDB_CODE_OUT_OF_MEMORY ;
}
// Set the correct column info (data type and bytes)
2022-04-27 02:11:32 +00:00
pColInfo - > info . type = pFuncParam - > param . nType ;
pColInfo - > info . bytes = pFuncParam - > param . nLen ;
2022-04-18 02:46:07 +00:00
pInput - > pData [ paramIndex ] = pColInfo ;
2022-04-25 09:12:28 +00:00
} else {
pColInfo = pInput - > pData [ paramIndex ] ;
2022-04-18 02:46:07 +00:00
}
2022-06-22 09:21:04 +00:00
colInfoDataEnsureCapacity ( pColInfo , numOfRows ) ;
2022-04-18 02:46:07 +00:00
2022-04-27 02:11:32 +00:00
int8_t type = pFuncParam - > param . nType ;
2022-04-18 02:46:07 +00:00
if ( type = = TSDB_DATA_TYPE_BIGINT | | type = = TSDB_DATA_TYPE_UBIGINT ) {
int64_t v = pFuncParam - > param . i ;
2022-04-23 10:29:45 +00:00
for ( int32_t i = 0 ; i < numOfRows ; + + i ) {
2022-04-18 02:46:07 +00:00
colDataAppendInt64 ( pColInfo , i , & v ) ;
}
} else if ( type = = TSDB_DATA_TYPE_DOUBLE ) {
double v = pFuncParam - > param . d ;
2022-04-23 10:29:45 +00:00
for ( int32_t i = 0 ; i < numOfRows ; + + i ) {
2022-04-18 02:46:07 +00:00
colDataAppendDouble ( pColInfo , i , & v ) ;
}
2022-04-27 02:11:32 +00:00
} else if ( type = = TSDB_DATA_TYPE_VARCHAR ) {
2022-04-28 08:31:35 +00:00
char * tmp = taosMemoryMalloc ( pFuncParam - > param . nLen + VARSTR_HEADER_SIZE ) ;
2022-04-27 02:11:32 +00:00
STR_WITH_SIZE_TO_VARSTR ( tmp , pFuncParam - > param . pz , pFuncParam - > param . nLen ) ;
2022-04-28 08:31:35 +00:00
for ( int32_t i = 0 ; i < numOfRows ; + + i ) {
2022-04-27 02:11:32 +00:00
colDataAppend ( pColInfo , i , tmp , false ) ;
}
2022-04-18 02:46:07 +00:00
}
return TSDB_CODE_SUCCESS ;
}
2022-04-23 10:29:45 +00:00
static int32_t doSetInputDataBlock ( SOperatorInfo * pOperator , SqlFunctionCtx * pCtx , SSDataBlock * pBlock , int32_t order ,
2022-05-23 11:50:08 +00:00
int32_t scanFlag , bool createDummyCol ) {
2022-04-18 02:46:07 +00:00
int32_t code = TSDB_CODE_SUCCESS ;
2022-06-18 04:00:41 +00:00
for ( int32_t i = 0 ; i < pOperator - > exprSupp . numOfExprs ; + + i ) {
2022-04-28 08:31:35 +00:00
pCtx [ i ] . order = order ;
2022-05-17 08:53:55 +00:00
pCtx [ i ] . input . numOfRows = pBlock - > info . rows ;
2022-04-28 08:31:35 +00:00
pCtx [ i ] . pSrcBlock = pBlock ;
2022-05-23 11:50:08 +00:00
pCtx [ i ] . scanFlag = scanFlag ;
2022-03-09 02:22:53 +00:00
2022-04-18 02:46:07 +00:00
SInputColumnInfoData * pInput = & pCtx [ i ] . input ;
2022-04-21 09:44:08 +00:00
pInput - > uid = pBlock - > info . uid ;
2022-04-25 07:58:44 +00:00
pInput - > colDataAggIsSet = false ;
2022-04-18 02:46:07 +00:00
2022-06-18 04:00:41 +00:00
SExprInfo * pOneExpr = & pOperator - > exprSupp . pExprInfo [ i ] ;
2022-04-12 09:55:17 +00:00
for ( int32_t j = 0 ; j < pOneExpr - > base . numOfParams ; + + j ) {
2022-04-23 10:29:45 +00:00
SFunctParam * pFuncParam = & pOneExpr - > base . pParam [ j ] ;
2022-03-29 09:30:44 +00:00
if ( pFuncParam - > type = = FUNC_PARAM_TYPE_COLUMN ) {
int32_t slotId = pFuncParam - > pCol - > slotId ;
2022-04-23 10:29:45 +00:00
pInput - > pData [ j ] = taosArrayGet ( pBlock - > pDataBlock , slotId ) ;
2022-04-18 02:46:07 +00:00
pInput - > totalRows = pBlock - > info . rows ;
pInput - > numOfRows = pBlock - > info . rows ;
pInput - > startRowIndex = 0 ;
2022-04-19 02:12:30 +00:00
2022-05-11 09:19:35 +00:00
// NOTE: the last parameter is the primary timestamp column
2022-04-30 05:48:07 +00:00
if ( fmIsTimelineFunc ( pCtx [ i ] . functionId ) & & ( j = = pOneExpr - > base . numOfParams - 1 ) ) {
pInput - > pPTS = pInput - > pData [ j ] ;
}
2022-04-18 02:46:07 +00:00
ASSERT ( pInput - > pData [ j ] ! = NULL ) ;
} else if ( pFuncParam - > type = = FUNC_PARAM_TYPE_VALUE ) {
2022-04-21 09:44:08 +00:00
// todo avoid case: top(k, 12), 12 is the value parameter.
// sum(11), 11 is also the value parameter.
if ( createDummyCol & & pOneExpr - > base . numOfParams = = 1 ) {
2022-04-24 11:20:05 +00:00
pInput - > totalRows = pBlock - > info . rows ;
pInput - > numOfRows = pBlock - > info . rows ;
pInput - > startRowIndex = 0 ;
2022-04-27 02:11:32 +00:00
code = doCreateConstantValColumnInfo ( pInput , pFuncParam , j , pBlock - > info . rows ) ;
2022-04-18 04:07:04 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
return code ;
}
2022-04-18 02:46:07 +00:00
}
2022-03-29 09:30:44 +00:00
}
}
2022-03-09 02:22:53 +00:00
}
2022-04-18 02:46:07 +00:00
return code ;
2022-03-09 02:22:53 +00:00
}
2022-05-13 04:17:09 +00:00
static int32_t doAggregateImpl ( SOperatorInfo * pOperator , TSKEY startTs , SqlFunctionCtx * pCtx ) {
2022-06-18 04:00:41 +00:00
for ( int32_t k = 0 ; k < pOperator - > exprSupp . numOfExprs ; + + k ) {
2022-02-24 09:18:56 +00:00
if ( functionNeedToExecute ( & pCtx [ k ] ) ) {
2022-05-10 10:21:54 +00:00
// todo add a dummy funtion to avoid process check
2022-05-30 12:10:30 +00:00
if ( pCtx [ k ] . fpSet . process = = NULL ) {
continue ;
}
2022-06-29 10:11:28 +00:00
# ifdef BUF_PAGE_DEBUG
2022-06-29 07:03:17 +00:00
qDebug ( " page_process " ) ;
2022-06-29 10:11:28 +00:00
# endif
2022-05-30 12:10:30 +00:00
int32_t code = pCtx [ k ] . fpSet . process ( & pCtx [ k ] ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " %s aggregate function error happens, code: %s " , GET_TASKID ( pOperator - > pTaskInfo ) , tstrerror ( code ) ) ;
return code ;
2022-05-10 10:21:54 +00:00
}
2021-11-02 05:37:31 +00:00
}
}
2022-05-13 04:17:09 +00:00
return TSDB_CODE_SUCCESS ;
2021-11-02 05:37:31 +00:00
}
2022-04-06 03:01:09 +00:00
static void setPseudoOutputColInfo ( SSDataBlock * pResult , SqlFunctionCtx * pCtx , SArray * pPseudoList ) {
2022-04-23 10:29:45 +00:00
size_t num = ( pPseudoList ! = NULL ) ? taosArrayGetSize ( pPseudoList ) : 0 ;
2022-04-06 03:01:09 +00:00
for ( int32_t i = 0 ; i < num ; + + i ) {
pCtx [ i ] . pOutput = taosArrayGet ( pResult - > pDataBlock , i ) ;
}
}
2022-04-28 10:08:56 +00:00
int32_t projectApplyFunctions ( SExprInfo * pExpr , SSDataBlock * pResult , SSDataBlock * pSrcBlock , SqlFunctionCtx * pCtx ,
2022-04-30 03:40:12 +00:00
int32_t numOfOutput , SArray * pPseudoList ) {
2022-04-06 03:01:09 +00:00
setPseudoOutputColInfo ( pResult , pCtx , pPseudoList ) ;
2022-04-08 09:37:57 +00:00
pResult - > info . groupId = pSrcBlock - > info . groupId ;
2022-04-06 03:01:09 +00:00
2022-04-23 10:29:45 +00:00
// if the source equals to the destination, it is to create a new column as the result of scalar function or some
// operators.
2022-04-19 10:45:28 +00:00
bool createNewColModel = ( pResult = = pSrcBlock ) ;
2022-04-18 11:27:41 +00:00
int32_t numOfRows = 0 ;
2021-11-02 05:37:31 +00:00
for ( int32_t k = 0 ; k < numOfOutput ; + + k ) {
2022-04-23 10:29:45 +00:00
int32_t outputSlotId = pExpr [ k ] . base . resSchema . slotId ;
2022-04-12 09:55:17 +00:00
SqlFunctionCtx * pfCtx = & pCtx [ k ] ;
2022-03-29 07:24:25 +00:00
if ( pExpr [ k ] . pExpr - > nodeType = = QUERY_NODE_COLUMN ) { // it is a project query
2022-04-12 09:55:17 +00:00
SColumnInfoData * pColInfoData = taosArrayGet ( pResult - > pDataBlock , outputSlotId ) ;
2022-04-19 10:45:28 +00:00
if ( pResult - > info . rows > 0 & & ! createNewColModel ) {
2022-05-10 00:55:32 +00:00
colDataMergeCol ( pColInfoData , pResult - > info . rows , & pResult - > info . capacity , pfCtx - > input . pData [ 0 ] ,
pfCtx - > input . numOfRows ) ;
2022-04-18 11:27:41 +00:00
} else {
2022-06-22 09:21:04 +00:00
colDataAssign ( pColInfoData , pfCtx - > input . pData [ 0 ] , pfCtx - > input . numOfRows , & pResult - > info ) ;
2022-04-18 11:27:41 +00:00
}
2022-03-26 14:13:14 +00:00
2022-04-18 11:27:41 +00:00
numOfRows = pfCtx - > input . numOfRows ;
2022-04-07 02:26:18 +00:00
} else if ( pExpr [ k ] . pExpr - > nodeType = = QUERY_NODE_VALUE ) {
2022-04-12 09:55:17 +00:00
SColumnInfoData * pColInfoData = taosArrayGet ( pResult - > pDataBlock , outputSlotId ) ;
2022-04-18 11:27:41 +00:00
2022-04-23 10:29:45 +00:00
int32_t offset = createNewColModel ? 0 : pResult - > info . rows ;
2022-04-07 02:26:18 +00:00
for ( int32_t i = 0 ; i < pSrcBlock - > info . rows ; + + i ) {
2022-04-23 10:29:45 +00:00
colDataAppend ( pColInfoData , i + offset ,
taosVariantGet ( & pExpr [ k ] . base . pParam [ 0 ] . param , pExpr [ k ] . base . pParam [ 0 ] . param . nType ) ,
TSDB_DATA_TYPE_NULL = = pExpr [ k ] . base . pParam [ 0 ] . param . nType ) ;
2022-04-07 02:26:18 +00:00
}
2022-04-18 11:27:41 +00:00
numOfRows = pSrcBlock - > info . rows ;
2022-03-26 14:17:26 +00:00
} else if ( pExpr [ k ] . pExpr - > nodeType = = QUERY_NODE_OPERATOR ) {
2022-03-26 14:13:14 +00:00
SArray * pBlockList = taosArrayInit ( 4 , POINTER_BYTES ) ;
taosArrayPush ( pBlockList , & pSrcBlock ) ;
2022-04-18 11:27:41 +00:00
SColumnInfoData * pResColData = taosArrayGet ( pResult - > pDataBlock , outputSlotId ) ;
2022-04-28 10:08:56 +00:00
SColumnInfoData idata = { . info = pResColData - > info , . hasNull = true } ;
2022-03-27 14:43:07 +00:00
2022-04-18 11:27:41 +00:00
SScalarParam dest = { . columnData = & idata } ;
2022-05-23 11:50:08 +00:00
int32_t code = scalarCalculate ( pExpr [ k ] . pExpr - > _optrRoot . pRootNode , pBlockList , & dest ) ;
2022-05-19 03:20:27 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
taosArrayDestroy ( pBlockList ) ;
return code ;
}
2022-03-27 14:43:07 +00:00
2022-04-23 10:29:45 +00:00
int32_t startOffset = createNewColModel ? 0 : pResult - > info . rows ;
2022-06-22 01:31:45 +00:00
ASSERT ( pResult - > info . capacity > 0 ) ;
2022-04-30 07:13:18 +00:00
colDataMergeCol ( pResColData , startOffset , & pResult - > info . capacity , & idata , dest . numOfRows ) ;
2022-04-18 11:27:41 +00:00
numOfRows = dest . numOfRows ;
2022-03-27 14:43:07 +00:00
taosArrayDestroy ( pBlockList ) ;
} else if ( pExpr [ k ] . pExpr - > nodeType = = QUERY_NODE_FUNCTION ) {
2022-04-12 09:55:17 +00:00
ASSERT ( ! fmIsAggFunc ( pfCtx - > functionId ) ) ;
2022-03-27 14:43:07 +00:00
2022-05-11 09:19:35 +00:00
// _rowts/_c0, not tbname column
if ( fmIsPseudoColumnFunc ( pfCtx - > functionId ) & & ( ! fmIsScanPseudoColumnFunc ( pfCtx - > functionId ) ) ) {
2022-04-06 03:01:09 +00:00
// do nothing
2022-06-16 03:43:11 +00:00
} else if ( fmIsIndefiniteRowsFunc ( pfCtx - > functionId ) ) {
SResultRowEntryInfo * pResInfo = GET_RES_INFO ( & pCtx [ k ] ) ;
pfCtx - > fpSet . init ( & pCtx [ k ] , pResInfo ) ;
pfCtx - > pOutput = taosArrayGet ( pResult - > pDataBlock , outputSlotId ) ;
pfCtx - > offset = createNewColModel ? 0 : pResult - > info . rows ; // set the start offset
// set the timestamp(_rowts) output buffer
if ( taosArrayGetSize ( pPseudoList ) > 0 ) {
int32_t * outputColIndex = taosArrayGet ( pPseudoList , 0 ) ;
pfCtx - > pTsOutput = ( SColumnInfoData * ) pCtx [ * outputColIndex ] . pOutput ;
}
numOfRows = pfCtx - > fpSet . process ( pfCtx ) ;
2022-04-04 06:54:39 +00:00
} else {
SArray * pBlockList = taosArrayInit ( 4 , POINTER_BYTES ) ;
taosArrayPush ( pBlockList , & pSrcBlock ) ;
2022-03-29 09:30:44 +00:00
2022-04-18 11:27:41 +00:00
SColumnInfoData * pResColData = taosArrayGet ( pResult - > pDataBlock , outputSlotId ) ;
2022-04-28 10:08:56 +00:00
SColumnInfoData idata = { . info = pResColData - > info , . hasNull = true } ;
2022-04-04 06:54:39 +00:00
2022-04-18 11:27:41 +00:00
SScalarParam dest = { . columnData = & idata } ;
2022-04-30 03:40:12 +00:00
int32_t code = scalarCalculate ( ( SNode * ) pExpr [ k ] . pExpr - > _function . pFunctNode , pBlockList , & dest ) ;
2022-04-28 10:08:56 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
taosArrayDestroy ( pBlockList ) ;
return code ;
}
2022-04-19 10:45:28 +00:00
2022-04-23 10:29:45 +00:00
int32_t startOffset = createNewColModel ? 0 : pResult - > info . rows ;
2022-06-22 01:31:45 +00:00
ASSERT ( pResult - > info . capacity > 0 ) ;
2022-04-30 07:13:18 +00:00
colDataMergeCol ( pResColData , startOffset , & pResult - > info . capacity , & idata , dest . numOfRows ) ;
2022-04-18 11:27:41 +00:00
numOfRows = dest . numOfRows ;
2022-04-04 06:54:39 +00:00
taosArrayDestroy ( pBlockList ) ;
}
2022-03-26 14:13:14 +00:00
} else {
2022-03-18 16:13:07 +00:00
ASSERT ( 0 ) ;
2021-11-02 05:37:31 +00:00
}
}
2022-04-18 11:27:41 +00:00
2022-04-19 10:45:28 +00:00
if ( ! createNewColModel ) {
pResult - > info . rows + = numOfRows ;
}
2022-04-28 10:08:56 +00:00
return TSDB_CODE_SUCCESS ;
2021-11-02 05:37:31 +00:00
}
2022-05-03 06:43:53 +00:00
static void setResultRowKey ( SResultRow * pResultRow , char * pData , int16_t type ) {
if ( IS_VAR_DATA_TYPE ( type ) ) {
// todo disable this
2022-05-10 09:46:45 +00:00
2022-05-03 06:43:53 +00:00
// if (pResultRow->key == NULL) {
// pResultRow->key = taosMemoryMalloc(varDataTLen(pData));
// varDataCopy(pResultRow->key, pData);
// } else {
// ASSERT(memcmp(pResultRow->key, pData, varDataTLen(pData)) == 0);
// }
} else {
int64_t v = - 1 ;
GET_TYPED_DATA ( v , int64_t , type , pData ) ;
2021-11-02 05:37:31 +00:00
2022-05-03 06:43:53 +00:00
pResultRow - > win . skey = v ;
pResultRow - > win . ekey = v ;
2021-11-02 05:37:31 +00:00
}
}
2022-05-20 11:34:39 +00:00
bool functionNeedToExecute ( SqlFunctionCtx * pCtx ) {
2022-05-03 06:43:53 +00:00
struct SResultRowEntryInfo * pResInfo = GET_RES_INFO ( pCtx ) ;
2021-11-02 05:37:31 +00:00
2022-05-03 06:43:53 +00:00
// in case of timestamp column, always generated results.
int32_t functionId = pCtx - > functionId ;
if ( functionId = = - 1 ) {
return false ;
}
2021-11-02 05:37:31 +00:00
2022-05-13 04:17:09 +00:00
if ( pCtx - > scanFlag = = REPEAT_SCAN ) {
return fmIsRepeatScanFunc ( pCtx - > functionId ) ;
2021-11-02 05:37:31 +00:00
}
2022-05-13 04:17:09 +00:00
if ( isRowEntryCompleted ( pResInfo ) ) {
return false ;
2022-05-03 06:43:53 +00:00
}
2021-11-02 05:37:31 +00:00
return true ;
}
2022-05-03 06:43:53 +00:00
static int32_t doCreateConstantValColumnAggInfo ( SInputColumnInfoData * pInput , SFunctParam * pFuncParam , int32_t type ,
int32_t paramIndex , int32_t numOfRows ) {
if ( pInput - > pData [ paramIndex ] = = NULL ) {
pInput - > pData [ paramIndex ] = taosMemoryCalloc ( 1 , sizeof ( SColumnInfoData ) ) ;
if ( pInput - > pData [ paramIndex ] = = NULL ) {
return TSDB_CODE_OUT_OF_MEMORY ;
}
2021-11-02 05:37:31 +00:00
2022-05-03 06:43:53 +00:00
// Set the correct column info (data type and bytes)
pInput - > pData [ paramIndex ] - > info . type = type ;
pInput - > pData [ paramIndex ] - > info . bytes = tDataTypes [ type ] . bytes ;
2021-11-02 05:37:31 +00:00
}
2022-02-24 09:18:56 +00:00
2022-05-03 06:43:53 +00:00
SColumnDataAgg * da = NULL ;
if ( pInput - > pColumnDataAgg [ paramIndex ] = = NULL ) {
da = taosMemoryCalloc ( 1 , sizeof ( SColumnDataAgg ) ) ;
pInput - > pColumnDataAgg [ paramIndex ] = da ;
if ( da = = NULL ) {
return TSDB_CODE_OUT_OF_MEMORY ;
2021-11-02 05:37:31 +00:00
}
} else {
2022-05-03 06:43:53 +00:00
da = pInput - > pColumnDataAgg [ paramIndex ] ;
2021-11-02 05:37:31 +00:00
}
2022-05-03 06:43:53 +00:00
ASSERT ( ! IS_VAR_DATA_TYPE ( type ) ) ;
2021-11-02 05:37:31 +00:00
2022-05-03 06:43:53 +00:00
if ( type = = TSDB_DATA_TYPE_BIGINT ) {
int64_t v = pFuncParam - > param . i ;
* da = ( SColumnDataAgg ) { . numOfNull = 0 , . min = v , . max = v , . maxIndex = 0 , . minIndex = 0 , . sum = v * numOfRows } ;
} else if ( type = = TSDB_DATA_TYPE_DOUBLE ) {
double v = pFuncParam - > param . d ;
* da = ( SColumnDataAgg ) { . numOfNull = 0 , . maxIndex = 0 , . minIndex = 0 } ;
2021-11-02 05:37:31 +00:00
2022-05-03 06:43:53 +00:00
* ( double * ) & da - > min = v ;
* ( double * ) & da - > max = v ;
* ( double * ) & da - > sum = v * numOfRows ;
} else if ( type = = TSDB_DATA_TYPE_BOOL ) { // todo validate this data type
bool v = pFuncParam - > param . i ;
* da = ( SColumnDataAgg ) { . numOfNull = 0 , . maxIndex = 0 , . minIndex = 0 } ;
* ( bool * ) & da - > min = 0 ;
* ( bool * ) & da - > max = v ;
* ( bool * ) & da - > sum = v * numOfRows ;
} else if ( type = = TSDB_DATA_TYPE_TIMESTAMP ) {
// do nothing
2021-11-02 05:37:31 +00:00
} else {
2022-05-03 06:43:53 +00:00
ASSERT ( 0 ) ;
2021-11-02 05:37:31 +00:00
}
2022-05-03 06:43:53 +00:00
return TSDB_CODE_SUCCESS ;
}
2022-04-18 02:46:07 +00:00
void setBlockStatisInfo ( SqlFunctionCtx * pCtx , SExprInfo * pExprInfo , SSDataBlock * pBlock ) {
int32_t numOfRows = pBlock - > info . rows ;
SInputColumnInfoData * pInput = & pCtx - > input ;
pInput - > numOfRows = numOfRows ;
pInput - > totalRows = numOfRows ;
if ( pBlock - > pBlockAgg ! = NULL ) {
pInput - > colDataAggIsSet = true ;
2022-04-16 03:47:50 +00:00
for ( int32_t j = 0 ; j < pExprInfo - > base . numOfParams ; + + j ) {
SFunctParam * pFuncParam = & pExprInfo - > base . pParam [ j ] ;
2022-04-18 02:46:07 +00:00
2022-04-16 03:47:50 +00:00
if ( pFuncParam - > type = = FUNC_PARAM_TYPE_COLUMN ) {
int32_t slotId = pFuncParam - > pCol - > slotId ;
2022-04-29 12:07:33 +00:00
pInput - > pColumnDataAgg [ j ] = pBlock - > pBlockAgg [ slotId ] ;
if ( pInput - > pColumnDataAgg [ j ] = = NULL ) {
pInput - > colDataAggIsSet = false ;
}
2022-04-16 08:06:48 +00:00
// Here we set the column info data since the data type for each column data is required, but
// the data in the corresponding SColumnInfoData will not be used.
pInput - > pData [ j ] = taosArrayGet ( pBlock - > pDataBlock , slotId ) ;
2022-04-18 02:46:07 +00:00
} else if ( pFuncParam - > type = = FUNC_PARAM_TYPE_VALUE ) {
doCreateConstantValColumnAggInfo ( pInput , pFuncParam , pFuncParam - > param . nType , j , pBlock - > info . rows ) ;
2022-04-16 03:47:50 +00:00
}
}
2021-11-02 05:37:31 +00:00
} else {
2022-04-18 02:46:07 +00:00
pInput - > colDataAggIsSet = false ;
2021-11-02 05:37:31 +00:00
}
// set the statistics data for primary time stamp column
2022-04-16 03:47:50 +00:00
// if (pCtx->functionId == FUNCTION_SPREAD && pColumn->colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
// pCtx->isAggSet = true;
// pCtx->agg.min = pBlock->info.window.skey;
// pCtx->agg.max = pBlock->info.window.ekey;
// }
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
bool isTaskKilled ( SExecTaskInfo * pTaskInfo ) {
2021-11-02 05:37:31 +00:00
// query has been executed more than tsShellActivityTimer, and the retrieve has not arrived
// abort current query execution.
2022-03-29 07:24:25 +00:00
if ( pTaskInfo - > owner ! = 0 & &
( ( taosGetTimestampSec ( ) - pTaskInfo - > cost . start / 1000 ) > 10 * getMaximumIdleDurationSec ( ) )
2022-01-10 11:48:21 +00:00
/*(!needBuildResAfterQueryComplete(pTaskInfo))*/ ) {
assert ( pTaskInfo - > cost . start ! = 0 ) ;
2022-03-29 07:24:25 +00:00
// qDebug("QInfo:%" PRIu64 " retrieve not arrive beyond %d ms, abort current query execution, start:%" PRId64
// ", current:%d", pQInfo->qId, 1, pQInfo->startExecTs, taosGetTimestampSec());
// return true;
2021-11-02 05:37:31 +00:00
}
return false ;
}
2022-03-29 07:24:25 +00:00
void setTaskKilled ( SExecTaskInfo * pTaskInfo ) { pTaskInfo - > code = TSDB_CODE_TSC_QUERY_CANCELLED ; }
2021-11-02 05:37:31 +00:00
/////////////////////////////////////////////////////////////////////////////////////////////
2022-03-29 07:24:25 +00:00
// todo refactor : return window
2022-04-20 06:59:06 +00:00
void getAlignQueryTimeWindow ( SInterval * pInterval , int32_t precision , int64_t key , STimeWindow * win ) {
2022-02-24 09:18:56 +00:00
win - > skey = taosTimeTruncate ( key , pInterval , precision ) ;
2021-11-02 05:37:31 +00:00
/*
2022-02-24 09:18:56 +00:00
* if the realSkey > INT64_MAX - pInterval - > interval , the query duration between
2021-11-02 05:37:31 +00:00
* realSkey and realEkey must be less than one interval . Therefore , no need to adjust the query ranges .
*/
2022-04-20 06:59:06 +00:00
win - > ekey = taosTimeAdd ( win - > skey , pInterval - > interval , pInterval - > intervalUnit , precision ) - 1 ;
if ( win - > ekey < win - > skey ) {
2021-11-02 05:37:31 +00:00
win - > ekey = INT64_MAX ;
}
}
2022-06-18 04:00:41 +00:00
#if 0
2022-03-29 07:24:25 +00:00
static int32_t updateBlockLoadStatus ( STaskAttr * pQuery , int32_t status ) {
2022-06-18 04:00:41 +00:00
2021-11-02 05:37:31 +00:00
bool hasFirstLastFunc = false ;
bool hasOtherFunc = false ;
2022-04-15 06:01:43 +00:00
if ( status = = BLK_DATA_DATA_LOAD | | status = = BLK_DATA_FILTEROUT ) {
2021-11-02 05:37:31 +00:00
return status ;
}
for ( int32_t i = 0 ; i < pQuery - > numOfOutput ; + + i ) {
int32_t functionId = getExprFunctionId ( & pQuery - > pExpr1 [ i ] ) ;
2022-06-18 04:00:41 +00:00
2021-11-02 05:37:31 +00:00
if ( functionId = = FUNCTION_TS | | functionId = = FUNCTION_TS_DUMMY | | functionId = = FUNCTION_TAG | |
functionId = = FUNCTION_TAG_DUMMY ) {
continue ;
}
if ( functionId = = FUNCTION_FIRST_DST | | functionId = = FUNCTION_LAST_DST ) {
hasFirstLastFunc = true ;
} else {
hasOtherFunc = true ;
}
2022-06-18 04:00:41 +00:00
2021-11-02 05:37:31 +00:00
}
2022-04-15 06:01:43 +00:00
if ( hasFirstLastFunc & & status = = BLK_DATA_NOT_LOAD ) {
2022-03-29 07:24:25 +00:00
if ( ! hasOtherFunc ) {
2022-04-15 06:01:43 +00:00
return BLK_DATA_FILTEROUT ;
2021-11-02 05:37:31 +00:00
} else {
2022-04-15 06:01:43 +00:00
return BLK_DATA_DATA_LOAD ;
2021-11-02 05:37:31 +00:00
}
}
return status ;
}
2022-06-18 04:00:41 +00:00
# endif
2022-03-29 07:24:25 +00:00
// static void updateDataCheckOrder(SQInfo *pQInfo, SQueryTableReq* pQueryMsg, bool stableQuery) {
// STaskAttr* pQueryAttr = pQInfo->runtimeEnv.pQueryAttr;
2022-03-09 02:22:53 +00:00
//
2022-03-29 07:24:25 +00:00
// // in case of point-interpolation query, use asc order scan
// char msg[] = "QInfo:0x%"PRIx64" scan order changed for %s query, old:%d, new:%d, qrange exchanged, old qrange:%"
// PRId64
// "-%" PRId64 ", new qrange:%" PRId64 "-%" PRId64;
2022-03-09 02:22:53 +00:00
//
2022-03-29 07:24:25 +00:00
// // todo handle the case the the order irrelevant query type mixed up with order critical query type
// // descending order query for last_row query
// if (isFirstLastRowQuery(pQueryAttr)) {
// //qDebug("QInfo:0x%"PRIx64" scan order changed for last_row query, old:%d, new:%d", pQInfo->qId,
// pQueryAttr->order.order, TSDB_ORDER_ASC);
2022-03-09 02:22:53 +00:00
//
2022-03-29 07:24:25 +00:00
// pQueryAttr->order.order = TSDB_ORDER_ASC;
// if (pQueryAttr->window.skey > pQueryAttr->window.ekey) {
2022-04-27 09:39:54 +00:00
// TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey);
2022-03-29 07:24:25 +00:00
// }
2022-03-09 02:22:53 +00:00
//
2022-03-29 07:24:25 +00:00
// pQueryAttr->needReverseScan = false;
// return;
// }
2022-03-09 02:22:53 +00:00
//
2022-03-29 07:24:25 +00:00
// if (pQueryAttr->groupbyColumn && pQueryAttr->order.order == TSDB_ORDER_DESC) {
// pQueryAttr->order.order = TSDB_ORDER_ASC;
// if (pQueryAttr->window.skey > pQueryAttr->window.ekey) {
2022-04-27 09:39:54 +00:00
// TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey);
2022-03-29 07:24:25 +00:00
// }
2022-03-09 02:22:53 +00:00
//
2022-03-29 07:24:25 +00:00
// pQueryAttr->needReverseScan = false;
// doUpdateLastKey(pQueryAttr);
// return;
// }
2022-03-09 02:22:53 +00:00
//
2022-03-29 07:24:25 +00:00
// if (pQueryAttr->pointInterpQuery && pQueryAttr->interval.interval == 0) {
// if (!QUERY_IS_ASC_QUERY(pQueryAttr)) {
// //qDebug(msg, pQInfo->qId, "interp", pQueryAttr->order.order, TSDB_ORDER_ASC, pQueryAttr->window.skey,
// pQueryAttr->window.ekey, pQueryAttr->window.ekey, pQueryAttr->window.skey); TSWAP(pQueryAttr->window.skey,
// pQueryAttr->window.ekey, TSKEY);
// }
2022-03-09 02:22:53 +00:00
//
2022-03-29 07:24:25 +00:00
// pQueryAttr->order.order = TSDB_ORDER_ASC;
// return;
// }
2022-03-09 02:22:53 +00:00
//
2022-03-29 07:24:25 +00:00
// if (pQueryAttr->interval.interval == 0) {
// if (onlyFirstQuery(pQueryAttr)) {
// if (!QUERY_IS_ASC_QUERY(pQueryAttr)) {
// //qDebug(msg, pQInfo->qId, "only-first", pQueryAttr->order.order, TSDB_ORDER_ASC, pQueryAttr->window.skey,
2022-03-09 02:22:53 +00:00
//// pQueryAttr->window.ekey, pQueryAttr->window.ekey, pQueryAttr->window.skey);
//
2022-04-27 09:39:54 +00:00
// TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey);
2022-03-09 02:22:53 +00:00
// doUpdateLastKey(pQueryAttr);
// }
//
// pQueryAttr->order.order = TSDB_ORDER_ASC;
// pQueryAttr->needReverseScan = false;
// } else if (onlyLastQuery(pQueryAttr) && notContainSessionOrStateWindow(pQueryAttr)) {
// if (QUERY_IS_ASC_QUERY(pQueryAttr)) {
// //qDebug(msg, pQInfo->qId, "only-last", pQueryAttr->order.order, TSDB_ORDER_DESC, pQueryAttr->window.skey,
//// pQueryAttr->window.ekey, pQueryAttr->window.ekey, pQueryAttr->window.skey);
//
2022-04-27 09:39:54 +00:00
// TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey);
2022-03-09 02:22:53 +00:00
// doUpdateLastKey(pQueryAttr);
// }
//
// pQueryAttr->order.order = TSDB_ORDER_DESC;
// pQueryAttr->needReverseScan = false;
// }
//
// } else { // interval query
// if (stableQuery) {
// if (onlyFirstQuery(pQueryAttr)) {
// if (!QUERY_IS_ASC_QUERY(pQueryAttr)) {
// //qDebug(msg, pQInfo->qId, "only-first stable", pQueryAttr->order.order, TSDB_ORDER_ASC,
2022-03-29 07:24:25 +00:00
//// pQueryAttr->window.skey, pQueryAttr->window.ekey, pQueryAttr->window.ekey,
/// pQueryAttr->window.skey);
2022-03-09 02:22:53 +00:00
//
2022-04-27 09:39:54 +00:00
// TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey);
2022-03-09 02:22:53 +00:00
// doUpdateLastKey(pQueryAttr);
// }
//
// pQueryAttr->order.order = TSDB_ORDER_ASC;
// pQueryAttr->needReverseScan = false;
// } else if (onlyLastQuery(pQueryAttr)) {
// if (QUERY_IS_ASC_QUERY(pQueryAttr)) {
// //qDebug(msg, pQInfo->qId, "only-last stable", pQueryAttr->order.order, TSDB_ORDER_DESC,
2022-03-29 07:24:25 +00:00
//// pQueryAttr->window.skey, pQueryAttr->window.ekey, pQueryAttr->window.ekey,
/// pQueryAttr->window.skey);
2022-03-09 02:22:53 +00:00
//
2022-04-27 09:39:54 +00:00
// TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey);
2022-03-09 02:22:53 +00:00
// doUpdateLastKey(pQueryAttr);
// }
//
// pQueryAttr->order.order = TSDB_ORDER_DESC;
// pQueryAttr->needReverseScan = false;
// }
// }
// }
//}
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// static FORCE_INLINE bool doFilterByBlockStatistics(STaskRuntimeEnv* pRuntimeEnv, SDataStatis *pDataStatis,
// SqlFunctionCtx *pCtx, int32_t numOfRows) {
// STaskAttr* pQueryAttr = pRuntimeEnv->pQueryAttr;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// if (pDataStatis == NULL || pQueryAttr->pFilters == NULL) {
// return true;
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// return filterRangeExecute(pQueryAttr->pFilters, pDataStatis, pQueryAttr->numOfCols, numOfRows);
// }
2022-06-18 04:00:41 +00:00
#if 0
2022-01-08 08:28:44 +00:00
static bool overlapWithTimeWindow ( STaskAttr * pQueryAttr , SDataBlockInfo * pBlockInfo ) {
2021-11-02 05:37:31 +00:00
STimeWindow w = { 0 } ;
2022-01-24 04:53:17 +00:00
TSKEY sk = TMIN ( pQueryAttr - > window . skey , pQueryAttr - > window . ekey ) ;
TSKEY ek = TMAX ( pQueryAttr - > window . skey , pQueryAttr - > window . ekey ) ;
2021-11-02 05:37:31 +00:00
2022-04-14 06:39:28 +00:00
if ( true ) {
2022-03-29 07:24:25 +00:00
// getAlignQueryTimeWindow(pQueryAttr, pBlockInfo->window.skey, sk, ek, &w);
2021-11-02 05:37:31 +00:00
assert ( w . ekey > = pBlockInfo - > window . skey ) ;
if ( w . ekey < pBlockInfo - > window . ekey ) {
return true ;
}
2022-03-29 07:24:25 +00:00
while ( 1 ) {
// getNextTimeWindow(pQueryAttr, &w);
2021-11-02 05:37:31 +00:00
if ( w . skey > pBlockInfo - > window . ekey ) {
break ;
}
assert ( w . ekey > pBlockInfo - > window . ekey ) ;
if ( w . skey < = pBlockInfo - > window . ekey & & w . skey > pBlockInfo - > window . skey ) {
return true ;
}
}
} else {
2022-03-29 07:24:25 +00:00
// getAlignQueryTimeWindow(pQueryAttr, pBlockInfo->window.ekey, sk, ek, &w);
2021-11-02 05:37:31 +00:00
assert ( w . skey < = pBlockInfo - > window . ekey ) ;
if ( w . skey > pBlockInfo - > window . skey ) {
return true ;
}
2022-03-29 07:24:25 +00:00
while ( 1 ) {
// getNextTimeWindow(pQueryAttr, &w);
2021-11-02 05:37:31 +00:00
if ( w . ekey < pBlockInfo - > window . skey ) {
break ;
}
assert ( w . skey < pBlockInfo - > window . skey ) ;
if ( w . ekey < pBlockInfo - > window . ekey & & w . ekey > = pBlockInfo - > window . skey ) {
return true ;
}
}
}
return false ;
}
2022-06-18 04:00:41 +00:00
# endif
2021-11-02 05:37:31 +00:00
static uint32_t doFilterByBlockTimeWindow ( STableScanInfo * pTableScanInfo , SSDataBlock * pBlock ) {
2022-01-28 02:44:02 +00:00
SqlFunctionCtx * pCtx = pTableScanInfo - > pCtx ;
2022-04-15 06:01:43 +00:00
uint32_t status = BLK_DATA_NOT_LOAD ;
2021-11-02 05:37:31 +00:00
2022-06-30 06:41:50 +00:00
int32_t numOfOutput = 0 ; // pTableScanInfo->numOfOutput;
2021-11-02 05:37:31 +00:00
for ( int32_t i = 0 ; i < numOfOutput ; + + i ) {
int32_t functionId = pCtx [ i ] . functionId ;
2022-03-09 02:22:53 +00:00
int32_t colId = pTableScanInfo - > pExpr [ i ] . base . pParam [ 0 ] . pCol - > colId ;
2021-11-02 05:37:31 +00:00
// group by + first/last should not apply the first/last block filter
if ( functionId < 0 ) {
2022-04-15 06:01:43 +00:00
status | = BLK_DATA_DATA_LOAD ;
2021-11-02 05:37:31 +00:00
return status ;
} else {
2022-03-29 07:24:25 +00:00
// status |= aAggs[functionId].dataReqFunc(&pTableScanInfo->pCtx[i], &pBlock->info.window, colId);
2022-04-15 06:01:43 +00:00
// if ((status & BLK_DATA_DATA_LOAD) == BLK_DATA_DATA_LOAD) {
2022-03-29 07:24:25 +00:00
// return status;
// }
2021-11-02 05:37:31 +00:00
}
}
return status ;
}
2022-03-29 07:24:25 +00:00
int32_t loadDataBlockOnDemand ( SExecTaskInfo * pTaskInfo , STableScanInfo * pTableScanInfo , SSDataBlock * pBlock ,
uint32_t * status ) {
2022-04-15 06:01:43 +00:00
* status = BLK_DATA_NOT_LOAD ;
2021-11-02 05:37:31 +00:00
2022-01-08 14:59:24 +00:00
pBlock - > pDataBlock = NULL ;
2022-03-29 07:24:25 +00:00
pBlock - > pBlockAgg = NULL ;
2022-01-08 14:59:24 +00:00
2022-03-29 07:24:25 +00:00
// int64_t groupId = pRuntimeEnv->current->groupIndex;
// bool ascQuery = QUERY_IS_ASC_QUERY(pQueryAttr);
2021-11-02 05:37:31 +00:00
2022-01-08 14:59:24 +00:00
STaskCostInfo * pCost = & pTaskInfo - > cost ;
2021-11-02 05:37:31 +00:00
2022-05-03 15:52:17 +00:00
// pCost->totalBlocks += 1;
// pCost->totalRows += pBlock->info.rows;
2022-01-08 14:59:24 +00:00
#if 0
2021-11-02 05:37:31 +00:00
// Calculate all time windows that are overlapping or contain current data block.
// If current data block is contained by all possible time window, do not load current data block.
if ( /*pQueryAttr->pFilters || */ pQueryAttr - > groupbyColumn | | pQueryAttr - > sw . gap > 0 | |
2022-01-08 14:59:24 +00:00
( QUERY_IS_INTERVAL_QUERY ( pQueryAttr ) & & overlapWithTimeWindow ( pTaskInfo , & pBlock - > info ) ) ) {
2022-04-15 06:01:43 +00:00
( * status ) = BLK_DATA_DATA_LOAD ;
2021-11-02 05:37:31 +00:00
}
// check if this data block is required to load
2022-04-15 06:01:43 +00:00
if ( ( * status ) ! = BLK_DATA_DATA_LOAD ) {
2021-11-02 05:37:31 +00:00
bool needFilter = true ;
// the pCtx[i] result is belonged to previous time window since the outputBuf has not been set yet,
// the filter result may be incorrect. So in case of interval query, we need to set the correct time output buffer
if ( QUERY_IS_INTERVAL_QUERY ( pQueryAttr ) ) {
SResultRow * pResult = NULL ;
2022-01-08 14:59:24 +00:00
bool masterScan = IS_MAIN_SCAN ( pRuntimeEnv ) ;
2021-11-02 05:37:31 +00:00
TSKEY k = ascQuery ? pBlock - > info . window . skey : pBlock - > info . window . ekey ;
STimeWindow win = getActiveTimeWindow ( pTableScanInfo - > pResultRowInfo , k , pQueryAttr ) ;
if ( pQueryAttr - > pointInterpQuery ) {
needFilter = chkWindowOutputBufByKey ( pRuntimeEnv , pTableScanInfo - > pResultRowInfo , & win , masterScan , & pResult , groupId ,
pTableScanInfo - > pCtx , pTableScanInfo - > numOfOutput ,
2022-06-17 15:23:37 +00:00
pTableScanInfo - > rowEntryInfoOffset ) ;
2021-11-02 05:37:31 +00:00
} else {
if ( setResultOutputBufByKey ( pRuntimeEnv , pTableScanInfo - > pResultRowInfo , pBlock - > info . uid , & win , masterScan , & pResult , groupId ,
pTableScanInfo - > pCtx , pTableScanInfo - > numOfOutput ,
2022-06-17 15:23:37 +00:00
pTableScanInfo - > rowEntryInfoOffset ) ! = TSDB_CODE_SUCCESS ) {
2021-11-02 05:37:31 +00:00
longjmp ( pRuntimeEnv - > env , TSDB_CODE_QRY_OUT_OF_MEMORY ) ;
}
}
} else if ( pQueryAttr - > stableQuery & & ( ! pQueryAttr - > tsCompQuery ) & & ( ! pQueryAttr - > diffQuery ) ) { // stable aggregate, not interval aggregate or normal column aggregate
doSetTableGroupOutputBuf ( pRuntimeEnv , pTableScanInfo - > pResultRowInfo , pTableScanInfo - > pCtx ,
2022-06-17 15:23:37 +00:00
pTableScanInfo - > rowEntryInfoOffset , pTableScanInfo - > numOfOutput ,
2021-11-02 05:37:31 +00:00
pRuntimeEnv - > current - > groupIndex ) ;
}
if ( needFilter ) {
( * status ) = doFilterByBlockTimeWindow ( pTableScanInfo , pBlock ) ;
} else {
2022-04-15 06:01:43 +00:00
( * status ) = BLK_DATA_DATA_LOAD ;
2021-11-02 05:37:31 +00:00
}
}
SDataBlockInfo * pBlockInfo = & pBlock - > info ;
2022-01-08 14:59:24 +00:00
// *status = updateBlockLoadStatus(pRuntimeEnv->pQueryAttr, *status);
2021-11-02 05:37:31 +00:00
2022-04-15 06:01:43 +00:00
if ( ( * status ) = = BLK_DATA_NOT_LOAD | | ( * status ) = = BLK_DATA_FILTEROUT ) {
2021-11-02 05:37:31 +00:00
//qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId, pBlockInfo->window.skey,
// pBlockInfo->window.ekey, pBlockInfo->rows);
2022-04-15 12:06:27 +00:00
pCost - > skipBlocks + = 1 ;
2022-04-15 06:01:43 +00:00
} else if ( ( * status ) = = BLK_DATA_SMA_LOAD ) {
2021-11-02 05:37:31 +00:00
// this function never returns error?
pCost - > loadBlockStatis + = 1 ;
2022-07-07 07:56:43 +00:00
// tsdbRetrieveDatablockSMA(pTableScanInfo->pTsdbReadHandle, &pBlock->pBlockAgg);
2021-11-02 05:37:31 +00:00
if ( pBlock - > pBlockAgg = = NULL ) { // data block statistics does not exist, load data block
2022-01-10 11:48:21 +00:00
// pBlock->pDataBlock = tsdbRetrieveDataBlock(pTableScanInfo->pTsdbReadHandle, NULL);
2021-11-02 05:37:31 +00:00
pCost - > totalCheckedRows + = pBlock - > info . rows ;
}
} else {
2022-04-15 06:01:43 +00:00
assert ( ( * status ) = = BLK_DATA_DATA_LOAD ) ;
2021-11-02 05:37:31 +00:00
// load the data block statistics to perform further filter
pCost - > loadBlockStatis + = 1 ;
2022-07-07 07:56:43 +00:00
// tsdbRetrieveDatablockSMA(pTableScanInfo->pTsdbReadHandle, &pBlock->pBlockAgg);
2021-11-02 05:37:31 +00:00
if ( pQueryAttr - > topBotQuery & & pBlock - > pBlockAgg ! = NULL ) {
{ // set previous window
if ( QUERY_IS_INTERVAL_QUERY ( pQueryAttr ) ) {
SResultRow * pResult = NULL ;
2022-01-08 14:59:24 +00:00
bool masterScan = IS_MAIN_SCAN ( pRuntimeEnv ) ;
2021-11-02 05:37:31 +00:00
TSKEY k = ascQuery ? pBlock - > info . window . skey : pBlock - > info . window . ekey ;
STimeWindow win = getActiveTimeWindow ( pTableScanInfo - > pResultRowInfo , k , pQueryAttr ) ;
if ( setResultOutputBufByKey ( pRuntimeEnv , pTableScanInfo - > pResultRowInfo , pBlock - > info . uid , & win , masterScan , & pResult , groupId ,
pTableScanInfo - > pCtx , pTableScanInfo - > numOfOutput ,
2022-06-17 15:23:37 +00:00
pTableScanInfo - > rowEntryInfoOffset ) ! = TSDB_CODE_SUCCESS ) {
2021-11-02 05:37:31 +00:00
longjmp ( pRuntimeEnv - > env , TSDB_CODE_QRY_OUT_OF_MEMORY ) ;
}
}
}
bool load = false ;
for ( int32_t i = 0 ; i < pQueryAttr - > numOfOutput ; + + i ) {
int32_t functionId = pTableScanInfo - > pCtx [ i ] . functionId ;
if ( functionId = = FUNCTION_TOP | | functionId = = FUNCTION_BOTTOM ) {
// load = topbot_datablock_filter(&pTableScanInfo->pCtx[i], (char*)&(pBlock->pBlockAgg[i].min),
// (char*)&(pBlock->pBlockAgg[i].max));
if ( ! load ) { // current block has been discard due to filter applied
2022-04-15 12:06:27 +00:00
pCost - > skipBlocks + = 1 ;
2021-11-02 05:37:31 +00:00
//qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId,
// pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows);
2022-04-15 06:01:43 +00:00
( * status ) = BLK_DATA_FILTEROUT ;
2021-11-02 05:37:31 +00:00
return TSDB_CODE_SUCCESS ;
}
}
}
}
// current block has been discard due to filter applied
// if (!doFilterByBlockStatistics(pRuntimeEnv, pBlock->pBlockAgg, pTableScanInfo->pCtx, pBlockInfo->rows)) {
2022-04-15 12:06:27 +00:00
// pCost->skipBlocks += 1;
2021-11-02 05:37:31 +00:00
// qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId, pBlockInfo->window.skey,
// pBlockInfo->window.ekey, pBlockInfo->rows);
2022-04-15 06:01:43 +00:00
// (*status) = BLK_DATA_FILTEROUT;
2021-11-02 05:37:31 +00:00
// return TSDB_CODE_SUCCESS;
// }
pCost - > totalCheckedRows + = pBlockInfo - > rows ;
pCost - > loadBlocks + = 1 ;
2022-01-10 11:48:21 +00:00
// pBlock->pDataBlock = tsdbRetrieveDataBlock(pTableScanInfo->pTsdbReadHandle, NULL);
2021-11-02 05:37:31 +00:00
// if (pBlock->pDataBlock == NULL) {
// return terrno;
// }
// if (pQueryAttr->pFilters != NULL) {
2022-06-22 09:21:04 +00:00
// filterSetColFieldData(pQueryAttr->pFilters, taosArrayGetSize(pBlock->pDataBlock), pBlock->pDataBlock);
2021-11-02 05:37:31 +00:00
// }
2022-05-01 07:22:28 +00:00
2021-11-02 05:37:31 +00:00
// if (pQueryAttr->pFilters != NULL || pRuntimeEnv->pTsBuf != NULL) {
// filterColRowsInDataBlock(pRuntimeEnv, pBlock, ascQuery);
// }
}
2022-01-08 14:59:24 +00:00
# endif
2021-11-02 05:37:31 +00:00
return TSDB_CODE_SUCCESS ;
}
2022-03-29 07:24:25 +00:00
static void updateTableQueryInfoForReverseScan ( STableQueryInfo * pTableQueryInfo ) {
2021-11-02 05:37:31 +00:00
if ( pTableQueryInfo = = NULL ) {
return ;
}
2022-04-27 09:39:54 +00:00
// TSWAP(pTableQueryInfo->win.skey, pTableQueryInfo->win.ekey);
2022-03-29 07:24:25 +00:00
// pTableQueryInfo->lastKey = pTableQueryInfo->win.skey;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// SWITCH_ORDER(pTableQueryInfo->cur.order);
// pTableQueryInfo->cur.vgroupIndex = -1;
2021-11-02 05:37:31 +00:00
// set the index to be the end slot of result rows array
2022-04-23 10:29:45 +00:00
// SResultRowInfo* pResultRowInfo = &pTableQueryInfo->resInfo;
// if (pResultRowInfo->size > 0) {
// pResultRowInfo->curPos = pResultRowInfo->size - 1;
// } else {
// pResultRowInfo->curPos = -1;
// }
2021-11-02 05:37:31 +00:00
}
2022-03-31 08:10:32 +00:00
void initResultRow ( SResultRow * pResultRow ) {
2022-05-10 00:55:32 +00:00
// pResultRow->pEntryInfo = (struct SResultRowEntryInfo*)((char*)pResultRow + sizeof(SResultRow));
2021-11-02 05:37:31 +00:00
}
/*
* The start of each column SResultRowEntryInfo is denote by RowCellInfoOffset .
* Note that in case of top / bottom query , the whole multiple rows of result is treated as only one row of results .
2022-03-09 02:22:53 +00:00
* + - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - result column 1 - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - result column 2 - - - - - - - - - - - +
* | SResultRow | SResultRowEntryInfo | intermediate buffer1 | SResultRowEntryInfo | intermediate buffer 2 |
* + - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
2021-11-02 05:37:31 +00:00
* offset [ 0 ] offset [ 1 ] offset [ 2 ]
*/
2022-02-22 05:12:03 +00:00
// TODO refactor: some function move away
2022-06-20 06:40:13 +00:00
void setFunctionResultOutput ( SOperatorInfo * pOperator , SOptrBasicInfo * pInfo , SAggSupporter * pSup , int32_t stage ,
int32_t numOfExprs ) {
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2022-06-18 04:00:41 +00:00
SqlFunctionCtx * pCtx = pOperator - > exprSupp . pCtx ;
int32_t * rowEntryInfoOffset = pOperator - > exprSupp . rowEntryInfoOffset ;
2022-03-31 09:59:41 +00:00
2022-01-20 05:52:46 +00:00
SResultRowInfo * pResultRowInfo = & pInfo - > resultRowInfo ;
2022-06-17 11:01:45 +00:00
initResultRowInfo ( pResultRowInfo ) ;
2022-01-20 05:52:46 +00:00
2022-03-29 07:24:25 +00:00
int64_t tid = 0 ;
int64_t groupId = 0 ;
2022-05-03 06:43:53 +00:00
SResultRow * pRow = doSetResultOutBufByKey ( pSup - > pResultBuf , pResultRowInfo , ( char * ) & tid , sizeof ( tid ) , true , groupId ,
pTaskInfo , false , pSup ) ;
2022-01-20 05:52:46 +00:00
2022-05-25 09:55:34 +00:00
for ( int32_t i = 0 ; i < numOfExprs ; + + i ) {
2022-06-17 15:23:37 +00:00
struct SResultRowEntryInfo * pEntry = getResultEntryInfo ( pRow , i , rowEntryInfoOffset ) ;
2022-01-20 05:52:46 +00:00
cleanupResultRowEntry ( pEntry ) ;
2022-03-29 07:24:25 +00:00
pCtx [ i ] . resultInfo = pEntry ;
2022-05-13 04:17:09 +00:00
pCtx [ i ] . scanFlag = stage ;
2022-01-20 05:52:46 +00:00
}
2022-05-25 09:55:34 +00:00
initCtxOutputBuffer ( pCtx , numOfExprs ) ;
2022-01-20 05:52:46 +00:00
}
2022-01-28 02:44:02 +00:00
void initCtxOutputBuffer ( SqlFunctionCtx * pCtx , int32_t size ) {
2021-11-02 05:37:31 +00:00
for ( int32_t j = 0 ; j < size ; + + j ) {
struct SResultRowEntryInfo * pResInfo = GET_RES_INFO ( & pCtx [ j ] ) ;
2022-04-23 10:29:45 +00:00
if ( isRowEntryInitialized ( pResInfo ) | | fmIsPseudoColumnFunc ( pCtx [ j ] . functionId ) | | pCtx [ j ] . functionId = = - 1 | |
fmIsScalarFunc ( pCtx [ j ] . functionId ) ) {
2021-11-02 05:37:31 +00:00
continue ;
}
2022-03-09 02:22:53 +00:00
pCtx [ j ] . fpSet . init ( & pCtx [ j ] , pCtx [ j ] . resultInfo ) ;
2021-11-02 05:37:31 +00:00
}
}
2022-03-29 07:24:25 +00:00
void setTaskStatus ( SExecTaskInfo * pTaskInfo , int8_t status ) {
2022-01-10 11:48:21 +00:00
if ( status = = TASK_NOT_COMPLETED ) {
2022-01-08 14:59:24 +00:00
pTaskInfo - > status = status ;
2021-11-02 05:37:31 +00:00
} else {
// QUERY_NOT_COMPLETED is not compatible with any other status, so clear its position first
2022-01-10 11:48:21 +00:00
CLEAR_QUERY_STATUS ( pTaskInfo , TASK_NOT_COMPLETED ) ;
2022-01-08 14:59:24 +00:00
pTaskInfo - > status | = status ;
2021-11-02 05:37:31 +00:00
}
}
2022-03-29 07:24:25 +00:00
void destroyTableQueryInfoImpl ( STableQueryInfo * pTableQueryInfo ) {
2021-11-02 05:37:31 +00:00
if ( pTableQueryInfo = = NULL ) {
return ;
}
2022-03-29 07:24:25 +00:00
// taosVariantDestroy(&pTableQueryInfo->tag);
2022-04-23 10:29:45 +00:00
// cleanupResultRowInfo(&pTableQueryInfo->resInfo);
2021-11-02 05:37:31 +00:00
}
2022-06-17 15:23:37 +00:00
void setResultRowInitCtx ( SResultRow * pResult , SqlFunctionCtx * pCtx , int32_t numOfOutput , int32_t * rowEntryInfoOffset ) {
2022-02-14 02:58:58 +00:00
for ( int32_t i = 0 ; i < numOfOutput ; + + i ) {
2022-06-17 15:23:37 +00:00
pCtx [ i ] . resultInfo = getResultEntryInfo ( pResult , i , rowEntryInfoOffset ) ;
2022-02-14 02:58:58 +00:00
struct SResultRowEntryInfo * pResInfo = pCtx [ i ] . resultInfo ;
if ( isRowEntryCompleted ( pResInfo ) & & isRowEntryInitialized ( pResInfo ) ) {
continue ;
}
2022-03-30 05:41:15 +00:00
if ( fmIsWindowPseudoColumnFunc ( pCtx [ i ] . functionId ) ) {
continue ;
}
2022-04-18 03:01:07 +00:00
if ( ! pResInfo - > initialized ) {
if ( pCtx [ i ] . functionId ! = - 1 ) {
pCtx [ i ] . fpSet . init ( & pCtx [ i ] , pResInfo ) ;
} else {
pResInfo - > initialized = true ;
}
2022-02-14 02:58:58 +00:00
}
}
}
2022-06-10 08:45:26 +00:00
static void extractQualifiedTupleByFilterResult ( SSDataBlock * pBlock , const int8_t * rowRes , bool keep ) ;
2022-06-04 08:30:42 +00:00
2022-06-11 06:51:54 +00:00
void doFilter ( const SNode * pFilterNode , SSDataBlock * pBlock ) {
2022-04-06 09:59:08 +00:00
if ( pFilterNode = = NULL ) {
return ;
}
SFilterInfo * filter = NULL ;
2022-04-06 15:00:32 +00:00
2022-04-07 05:57:47 +00:00
// todo move to the initialization function
2022-04-06 15:00:32 +00:00
int32_t code = filterInitFromNode ( ( SNode * ) pFilterNode , & filter , 0 ) ;
2022-04-06 09:59:08 +00:00
2022-06-24 06:07:52 +00:00
size_t numOfCols = taosArrayGetSize ( pBlock - > pDataBlock ) ;
2022-06-22 09:21:04 +00:00
SFilterColumnParam param1 = { . numOfCols = numOfCols , . pDataBlock = pBlock - > pDataBlock } ;
2022-04-06 09:59:08 +00:00
code = filterSetDataFromSlotId ( filter , & param1 ) ;
int8_t * rowRes = NULL ;
2022-05-14 11:21:12 +00:00
2022-05-14 08:28:34 +00:00
// todo the keep seems never to be True??
2022-05-14 11:21:12 +00:00
bool keep = filterExecute ( filter , pBlock , & rowRes , NULL , param1 . numOfCols ) ;
2022-04-09 05:36:36 +00:00
filterFreeInfo ( filter ) ;
2022-04-06 09:59:08 +00:00
2022-06-10 08:45:26 +00:00
extractQualifiedTupleByFilterResult ( pBlock , rowRes , keep ) ;
2022-05-21 03:02:53 +00:00
blockDataUpdateTsWindow ( pBlock , 0 ) ;
2022-07-06 14:25:59 +00:00
taosMemoryFree ( rowRes ) ;
2022-05-14 08:28:34 +00:00
}
2022-06-10 08:45:26 +00:00
void extractQualifiedTupleByFilterResult ( SSDataBlock * pBlock , const int8_t * rowRes , bool keep ) {
2022-05-14 08:28:34 +00:00
if ( keep ) {
return ;
}
if ( rowRes ! = NULL ) {
2022-06-23 14:05:00 +00:00
int32_t totalRows = pBlock - > info . rows ;
2022-06-10 16:51:59 +00:00
SSDataBlock * px = createOneDataBlock ( pBlock , true ) ;
2022-05-14 08:28:34 +00:00
2022-06-22 09:21:04 +00:00
size_t numOfCols = taosArrayGetSize ( pBlock - > pDataBlock ) ;
for ( int32_t i = 0 ; i < numOfCols ; + + i ) {
2022-06-10 16:51:59 +00:00
SColumnInfoData * pSrc = taosArrayGet ( px - > pDataBlock , i ) ;
SColumnInfoData * pDst = taosArrayGet ( pBlock - > pDataBlock , i ) ;
2022-05-16 10:29:34 +00:00
// it is a reserved column for scalar function, and no data in this column yet.
2022-06-30 09:28:49 +00:00
if ( pDst - > pData = = NULL | | pSrc - > pData = = NULL ) {
2022-05-16 10:29:34 +00:00
continue ;
}
2022-06-11 03:28:59 +00:00
colInfoDataCleanup ( pDst , pBlock - > info . rows ) ;
2022-05-14 08:28:34 +00:00
int32_t numOfRows = 0 ;
2022-05-14 08:40:51 +00:00
for ( int32_t j = 0 ; j < totalRows ; + + j ) {
2022-04-09 05:36:36 +00:00
if ( rowRes [ j ] = = 0 ) {
continue ;
}
2022-04-06 09:59:08 +00:00
2022-04-09 05:36:36 +00:00
if ( colDataIsNull_s ( pSrc , j ) ) {
2022-05-14 08:28:34 +00:00
colDataAppendNULL ( pDst , numOfRows ) ;
2022-04-09 05:36:36 +00:00
} else {
2022-05-14 08:28:34 +00:00
colDataAppend ( pDst , numOfRows , colDataGetData ( pSrc , j ) , false ) ;
2022-04-09 05:36:36 +00:00
}
2022-05-14 08:28:34 +00:00
numOfRows + = 1 ;
2022-04-06 15:00:32 +00:00
}
2022-04-06 09:59:08 +00:00
2022-05-14 08:40:51 +00:00
if ( pBlock - > info . rows = = totalRows ) {
pBlock - > info . rows = numOfRows ;
} else {
ASSERT ( pBlock - > info . rows = = numOfRows ) ;
}
2022-05-14 08:28:34 +00:00
}
2022-06-11 06:10:00 +00:00
2022-06-08 08:13:52 +00:00
blockDataDestroy ( px ) ; // fix memory leak
2022-05-14 08:28:34 +00:00
} else {
// do nothing
pBlock - > info . rows = 0 ;
2022-04-06 09:59:08 +00:00
}
}
2022-06-20 06:40:13 +00:00
void doSetTableGroupOutputBuf ( SOperatorInfo * pOperator , SAggOperatorInfo * pAggInfo , int32_t numOfOutput ,
uint64_t groupId ) {
2021-11-02 05:37:31 +00:00
// for simple group by query without interval, all the tables belong to one group result.
2022-06-20 06:40:13 +00:00
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2022-02-14 02:58:58 +00:00
SResultRowInfo * pResultRowInfo = & pAggInfo - > binfo . resultRowInfo ;
2022-06-18 04:00:41 +00:00
SqlFunctionCtx * pCtx = pOperator - > exprSupp . pCtx ;
int32_t * rowEntryInfoOffset = pOperator - > exprSupp . rowEntryInfoOffset ;
2022-02-14 02:58:58 +00:00
2022-05-03 06:43:53 +00:00
SResultRow * pResultRow = doSetResultOutBufByKey ( pAggInfo - > aggSup . pResultBuf , pResultRowInfo , ( char * ) & groupId ,
2022-04-28 08:31:35 +00:00
sizeof ( groupId ) , true , groupId , pTaskInfo , false , & pAggInfo - > aggSup ) ;
2022-03-29 07:24:25 +00:00
assert ( pResultRow ! = NULL ) ;
2021-11-02 05:37:31 +00:00
/*
* not assign result buffer yet , add new result buffer
* all group belong to one result set , and each group result has different group id so set the id to be one
*/
if ( pResultRow - > pageId = = - 1 ) {
2022-04-23 10:29:45 +00:00
int32_t ret =
addNewWindowResultBuf ( pResultRow , pAggInfo - > aggSup . pResultBuf , groupId , pAggInfo - > binfo . pRes - > info . rowSize ) ;
2021-11-02 05:37:31 +00:00
if ( ret ! = TSDB_CODE_SUCCESS ) {
return ;
}
}
2022-06-17 15:23:37 +00:00
setResultRowInitCtx ( pResultRow , pCtx , numOfOutput , rowEntryInfoOffset ) ;
2021-11-02 05:37:31 +00:00
}
2022-06-18 04:00:41 +00:00
void setExecutionContext ( SOperatorInfo * pOperator , int32_t numOfOutput , uint64_t groupId , SAggOperatorInfo * pAggInfo ) {
2022-04-16 11:10:21 +00:00
if ( pAggInfo - > groupId ! = INT32_MIN & & pAggInfo - > groupId = = groupId ) {
2021-11-02 05:37:31 +00:00
return ;
}
2022-06-29 10:11:28 +00:00
# ifdef BUF_PAGE_DEBUG
2022-07-02 12:18:40 +00:00
qDebug ( " page_setbuf, groupId:% " PRIu64 , groupId ) ;
2022-06-29 10:11:28 +00:00
# endif
2022-06-18 04:00:41 +00:00
doSetTableGroupOutputBuf ( pOperator , pAggInfo , numOfOutput , groupId ) ;
2021-11-02 05:37:31 +00:00
// record the current active group id
2022-04-16 11:10:21 +00:00
pAggInfo - > groupId = groupId ;
2021-11-02 05:37:31 +00:00
}
2022-05-20 15:59:17 +00:00
static void doUpdateNumOfRows ( SResultRow * pRow , int32_t numOfExprs , const int32_t * rowCellOffset ) {
for ( int32_t j = 0 ; j < numOfExprs ; + + j ) {
2022-06-17 11:01:45 +00:00
struct SResultRowEntryInfo * pResInfo = getResultEntryInfo ( pRow , j , rowCellOffset ) ;
2022-05-20 15:59:17 +00:00
if ( ! isRowEntryInitialized ( pResInfo ) ) {
continue ;
}
if ( pRow - > numOfRows < pResInfo - > numOfRes ) {
pRow - > numOfRows = pResInfo - > numOfRes ;
}
}
}
2022-06-10 01:55:14 +00:00
int32_t finalizeResultRowIntoResultDataBlock ( SDiskbasedBuf * pBuf , SResultRowPosition * resultRowPosition ,
2022-06-10 03:09:31 +00:00
SqlFunctionCtx * pCtx , SExprInfo * pExprInfo , int32_t numOfExprs ,
const int32_t * rowCellOffset , SSDataBlock * pBlock ,
SExecTaskInfo * pTaskInfo ) {
2022-06-09 11:21:52 +00:00
SFilePage * page = getBufPage ( pBuf , resultRowPosition - > pageId ) ;
SResultRow * pRow = ( SResultRow * ) ( ( char * ) page + resultRowPosition - > offset ) ;
doUpdateNumOfRows ( pRow , numOfExprs , rowCellOffset ) ;
if ( pRow - > numOfRows = = 0 ) {
releaseBufPage ( pBuf , page ) ;
return 0 ;
}
2022-06-10 01:55:14 +00:00
while ( pBlock - > info . rows + pRow - > numOfRows > pBlock - > info . capacity ) {
int32_t code = blockDataEnsureCapacity ( pBlock , pBlock - > info . capacity * 1.25 ) ;
if ( TAOS_FAILED ( code ) ) {
releaseBufPage ( pBuf , page ) ;
qError ( " %s ensure result data capacity failed, code %s " , GET_TASKID ( pTaskInfo ) , tstrerror ( code ) ) ;
longjmp ( pTaskInfo - > env , code ) ;
}
2022-06-09 11:21:52 +00:00
}
for ( int32_t j = 0 ; j < numOfExprs ; + + j ) {
int32_t slotId = pExprInfo [ j ] . base . resSchema . slotId ;
2022-06-17 11:01:45 +00:00
pCtx [ j ] . resultInfo = getResultEntryInfo ( pRow , j , rowCellOffset ) ;
2022-06-09 11:21:52 +00:00
if ( pCtx [ j ] . fpSet . finalize ) {
int32_t code = pCtx [ j ] . fpSet . finalize ( & pCtx [ j ] , pBlock ) ;
if ( TAOS_FAILED ( code ) ) {
qError ( " %s build result data block error, code %s " , GET_TASKID ( pTaskInfo ) , tstrerror ( code ) ) ;
longjmp ( pTaskInfo - > env , code ) ;
}
} else if ( strcmp ( pCtx [ j ] . pExpr - > pExpr - > _function . functionName , " _select_value " ) = = 0 ) {
// do nothing, todo refactor
} else {
// expand the result into multiple rows. E.g., _wstartts, top(k, 20)
// the _wstartts needs to copy to 20 following rows, since the results of top-k expands to 20 different rows.
SColumnInfoData * pColInfoData = taosArrayGet ( pBlock - > pDataBlock , slotId ) ;
char * in = GET_ROWCELL_INTERBUF ( pCtx [ j ] . resultInfo ) ;
for ( int32_t k = 0 ; k < pRow - > numOfRows ; + + k ) {
colDataAppend ( pColInfoData , pBlock - > info . rows + k , in , pCtx [ j ] . resultInfo - > isNullRes ) ;
}
}
}
releaseBufPage ( pBuf , page ) ;
2022-06-10 10:33:24 +00:00
pBlock - > info . rows + = pRow - > numOfRows ;
2022-06-09 11:21:52 +00:00
return 0 ;
}
2022-05-23 11:50:08 +00:00
int32_t doCopyToSDataBlock ( SExecTaskInfo * pTaskInfo , SSDataBlock * pBlock , SExprInfo * pExprInfo , SDiskbasedBuf * pBuf ,
SGroupResInfo * pGroupResInfo , const int32_t * rowCellOffset , SqlFunctionCtx * pCtx ,
int32_t numOfExprs ) {
2021-11-02 05:37:31 +00:00
int32_t numOfRows = getNumOfTotalRes ( pGroupResInfo ) ;
2022-05-17 04:36:32 +00:00
int32_t start = pGroupResInfo - > index ;
2022-06-29 10:11:28 +00:00
# ifdef BUF_PAGE_DEBUG
2022-06-29 07:03:17 +00:00
qDebug ( " \n page_copytoblock rows:%d " , numOfRows ) ;
2022-06-29 10:11:28 +00:00
# endif
2022-05-17 04:36:32 +00:00
for ( int32_t i = start ; i < numOfRows ; i + = 1 ) {
2022-04-28 08:31:35 +00:00
SResKeyPos * pPos = taosArrayGetP ( pGroupResInfo - > pRows , i ) ;
SFilePage * page = getBufPage ( pBuf , pPos - > pos . pageId ) ;
2022-06-29 10:11:28 +00:00
# ifdef BUF_PAGE_DEBUG
2022-06-29 07:03:17 +00:00
qDebug ( " page_copytoblock pos pageId:%d, offset:%d " , pPos - > pos . pageId , pPos - > pos . offset ) ;
2022-06-29 10:11:28 +00:00
# endif
2022-04-24 12:48:42 +00:00
SResultRow * pRow = ( SResultRow * ) ( ( char * ) page + pPos - > pos . offset ) ;
2022-05-20 15:59:17 +00:00
doUpdateNumOfRows ( pRow , numOfExprs , rowCellOffset ) ;
2021-11-02 05:37:31 +00:00
if ( pRow - > numOfRows = = 0 ) {
pGroupResInfo - > index + = 1 ;
2022-05-20 15:59:17 +00:00
releaseBufPage ( pBuf , page ) ;
2021-11-02 05:37:31 +00:00
continue ;
}
2022-05-20 08:50:39 +00:00
if ( pBlock - > info . groupId = = 0 ) {
pBlock - > info . groupId = pPos - > groupId ;
} else {
// current value belongs to different group, it can't be packed into one datablock
if ( pBlock - > info . groupId ! = pPos - > groupId ) {
2022-05-20 15:59:17 +00:00
releaseBufPage ( pBuf , page ) ;
2022-05-20 08:50:39 +00:00
break ;
}
}
2022-05-17 04:36:32 +00:00
if ( pBlock - > info . rows + pRow - > numOfRows > pBlock - > info . capacity ) {
2022-05-20 15:59:17 +00:00
releaseBufPage ( pBuf , page ) ;
2021-11-02 05:37:31 +00:00
break ;
}
pGroupResInfo - > index + = 1 ;
2022-05-13 10:18:54 +00:00
for ( int32_t j = 0 ; j < numOfExprs ; + + j ) {
2022-04-12 09:55:17 +00:00
int32_t slotId = pExprInfo [ j ] . base . resSchema . slotId ;
2022-06-17 11:01:45 +00:00
pCtx [ j ] . resultInfo = getResultEntryInfo ( pRow , j , rowCellOffset ) ;
2022-05-12 01:57:43 +00:00
if ( pCtx [ j ] . fpSet . finalize ) {
2022-06-29 10:11:28 +00:00
# ifdef BUF_PAGE_DEBUG
2022-06-29 07:03:17 +00:00
qDebug ( " \n page_finalize %d " , numOfExprs ) ;
2022-06-29 10:11:28 +00:00
# endif
2022-05-13 10:18:54 +00:00
int32_t code = pCtx [ j ] . fpSet . finalize ( & pCtx [ j ] , pBlock ) ;
2022-05-12 02:43:54 +00:00
if ( TAOS_FAILED ( code ) ) {
2022-05-20 08:50:39 +00:00
qError ( " %s build result data block error, code %s " , GET_TASKID ( pTaskInfo ) , tstrerror ( code ) ) ;
longjmp ( pTaskInfo - > env , code ) ;
2022-05-12 01:57:43 +00:00
}
2022-05-09 08:06:15 +00:00
} else if ( strcmp ( pCtx [ j ] . pExpr - > pExpr - > _function . functionName , " _select_value " ) = = 0 ) {
// do nothing, todo refactor
2022-04-22 07:07:20 +00:00
} else {
2022-05-10 10:21:54 +00:00
// expand the result into multiple rows. E.g., _wstartts, top(k, 20)
// the _wstartts needs to copy to 20 following rows, since the results of top-k expands to 20 different rows.
2022-05-23 11:50:08 +00:00
SColumnInfoData * pColInfoData = taosArrayGet ( pBlock - > pDataBlock , slotId ) ;
char * in = GET_ROWCELL_INTERBUF ( pCtx [ j ] . resultInfo ) ;
2022-06-09 09:18:17 +00:00
if ( pCtx [ j ] . increase ) {
2022-06-10 07:20:57 +00:00
int64_t ts = * ( int64_t * ) in ;
2022-06-09 09:18:17 +00:00
for ( int32_t k = 0 ; k < pRow - > numOfRows ; + + k ) {
2022-06-10 07:20:57 +00:00
colDataAppend ( pColInfoData , pBlock - > info . rows + k , ( const char * ) & ts , pCtx [ j ] . resultInfo - > isNullRes ) ;
2022-06-09 09:18:17 +00:00
ts + + ;
}
} else {
for ( int32_t k = 0 ; k < pRow - > numOfRows ; + + k ) {
colDataAppend ( pColInfoData , pBlock - > info . rows + k , in , pCtx [ j ] . resultInfo - > isNullRes ) ;
}
2022-05-23 11:50:08 +00:00
}
2022-04-22 07:07:20 +00:00
}
2021-11-02 05:37:31 +00:00
}
2022-03-15 06:37:26 +00:00
releaseBufPage ( pBuf , page ) ;
2022-04-22 07:07:20 +00:00
pBlock - > info . rows + = pRow - > numOfRows ;
2022-07-02 12:18:40 +00:00
// if (pBlock->info.rows >= pBlock->info.capacity) { // output buffer is full
// break;
// }
2021-11-02 05:37:31 +00:00
}
2022-05-23 11:50:08 +00:00
qDebug ( " %s result generated, rows:%d, groupId:% " PRIu64 , GET_TASKID ( pTaskInfo ) , pBlock - > info . rows ,
pBlock - > info . groupId ) ;
2022-05-21 03:02:53 +00:00
blockDataUpdateTsWindow ( pBlock , 0 ) ;
2021-11-02 05:37:31 +00:00
return 0 ;
}
2022-05-23 11:50:08 +00:00
void doBuildResultDatablock ( SOperatorInfo * pOperator , SOptrBasicInfo * pbInfo , SGroupResInfo * pGroupResInfo ,
SDiskbasedBuf * pBuf ) {
2022-06-18 04:00:41 +00:00
SExprInfo * pExprInfo = pOperator - > exprSupp . pExprInfo ;
int32_t numOfExprs = pOperator - > exprSupp . numOfExprs ;
2022-05-13 10:18:54 +00:00
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2022-06-18 04:00:41 +00:00
int32_t * rowCellOffset = pOperator - > exprSupp . rowEntryInfoOffset ;
2022-05-10 00:55:32 +00:00
SSDataBlock * pBlock = pbInfo - > pRes ;
2022-06-18 04:00:41 +00:00
SqlFunctionCtx * pCtx = pOperator - > exprSupp . pCtx ;
2022-05-03 07:04:34 +00:00
2022-03-17 05:11:06 +00:00
blockDataCleanup ( pBlock ) ;
2022-06-17 11:01:45 +00:00
if ( ! hasDataInGroupInfo ( pGroupResInfo ) ) {
2021-11-02 05:37:31 +00:00
return ;
}
2022-05-20 08:50:39 +00:00
// clear the existed group id
pBlock - > info . groupId = 0 ;
2022-05-17 04:36:32 +00:00
doCopyToSDataBlock ( pTaskInfo , pBlock , pExprInfo , pBuf , pGroupResInfo , rowCellOffset , pCtx , numOfExprs ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static void updateNumOfRowsInResultRows ( SqlFunctionCtx * pCtx , int32_t numOfOutput , SResultRowInfo * pResultRowInfo ,
2022-06-17 15:23:37 +00:00
int32_t * rowEntryInfoOffset ) {
2021-11-02 05:37:31 +00:00
// update the number of result for each, only update the number of rows for the corresponding window result.
2022-03-29 07:24:25 +00:00
// if (QUERY_IS_INTERVAL_QUERY(pQueryAttr)) {
// return;
// }
2022-03-31 08:10:32 +00:00
#if 0
2021-11-02 05:37:31 +00:00
for ( int32_t i = 0 ; i < pResultRowInfo - > size ; + + i ) {
2022-03-29 07:24:25 +00:00
SResultRow * pResult = pResultRowInfo - > pResult [ i ] ;
2021-11-02 05:37:31 +00:00
for ( int32_t j = 0 ; j < numOfOutput ; + + j ) {
int32_t functionId = pCtx [ j ] . functionId ;
if ( functionId = = FUNCTION_TS | | functionId = = FUNCTION_TAG | | functionId = = FUNCTION_TAGPRJ ) {
continue ;
}
2022-06-17 15:23:37 +00:00
SResultRowEntryInfo * pCell = getResultEntryInfo ( pResult , j , rowEntryInfoOffset ) ;
2022-02-14 02:58:58 +00:00
pResult - > numOfRows = ( uint16_t ) ( TMAX ( pResult - > numOfRows , pCell - > numOfRes ) ) ;
2021-11-02 05:37:31 +00:00
}
}
2022-03-31 08:10:32 +00:00
# endif
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static int32_t compressQueryColData ( SColumnInfoData * pColRes , int32_t numOfRows , char * data , int8_t compressed ) {
2021-11-02 05:37:31 +00:00
int32_t colSize = pColRes - > info . bytes * numOfRows ;
return ( * ( tDataTypes [ pColRes - > info . type ] . compFunc ) ) ( pColRes - > pData , colSize , numOfRows , data ,
2022-03-29 07:24:25 +00:00
colSize + COMP_OVERFLOW_BYTES , compressed , NULL , 0 ) ;
2021-11-02 05:37:31 +00:00
}
2022-05-02 15:52:32 +00:00
int32_t doFillTimeIntervalGapsInResults ( struct SFillInfo * pFillInfo , SSDataBlock * pBlock , int32_t capacity ) {
int32_t numOfRows = ( int32_t ) taosFillResultDataBlock ( pFillInfo , pBlock , capacity - pBlock - > info . rows ) ;
pBlock - > info . rows + = numOfRows ;
2021-11-02 05:37:31 +00:00
2022-05-02 15:52:32 +00:00
return pBlock - > info . rows ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
void queryCostStatis ( SExecTaskInfo * pTaskInfo ) {
STaskCostInfo * pSummary = & pTaskInfo - > cost ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// uint64_t hashSize = taosHashGetMemSize(pQInfo->runtimeEnv.pResultRowHashTable);
// hashSize += taosHashGetMemSize(pRuntimeEnv->tableqinfoGroupInfo.map);
// pSummary->hashSize = hashSize;
2021-11-02 05:37:31 +00:00
// add the merge time
pSummary - > elapsedTime + = pSummary - > firstStageMergeTime ;
2022-03-29 07:24:25 +00:00
// SResultRowPool* p = pTaskInfo->pool;
// if (p != NULL) {
// pSummary->winInfoSize = getResultRowPoolMemSize(p);
// pSummary->numOfTimeWindows = getNumOfAllocatedResultRows(p);
// } else {
// pSummary->winInfoSize = 0;
// pSummary->numOfTimeWindows = 0;
// }
//
// calculateOperatorProfResults(pQInfo);
2022-05-04 06:34:38 +00:00
SFileBlockLoadRecorder * pRecorder = pSummary - > pRecoder ;
if ( pSummary - > pRecoder ! = NULL ) {
2022-05-10 00:55:32 +00:00
qDebug ( " %s :cost summary: elapsed time:% " PRId64 " us, first merge:% " PRId64
" us, total blocks:%d, "
2022-05-04 06:34:38 +00:00
" load block statis:%d, load data block:%d, total rows:% " PRId64 " , check rows:% " PRId64 ,
GET_TASKID ( pTaskInfo ) , pSummary - > elapsedTime , pSummary - > firstStageMergeTime , pRecorder - > totalBlocks ,
pRecorder - > loadBlockStatis , pRecorder - > loadBlocks , pRecorder - > totalRows , pRecorder - > totalCheckedRows ) ;
}
2022-03-29 07:24:25 +00:00
// qDebug("QInfo:0x%"PRIx64" :cost summary: winResPool size:%.2f Kb, numOfWin:%"PRId64", tableInfoSize:%.2f Kb,
// hashTable:%.2f Kb", pQInfo->qId, pSummary->winInfoSize/1024.0,
// pSummary->numOfTimeWindows, pSummary->tableInfoSize/1024.0, pSummary->hashSize/1024.0);
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
// static void updateOffsetVal(STaskRuntimeEnv *pRuntimeEnv, SDataBlockInfo *pBlockInfo) {
// STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr;
// STableQueryInfo* pTableQueryInfo = pRuntimeEnv->current;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQueryAttr->order.order);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// if (pQueryAttr->limit.offset == pBlockInfo->rows) { // current block will ignore completed
// pTableQueryInfo->lastKey = QUERY_IS_ASC_QUERY(pQueryAttr) ? pBlockInfo->window.ekey + step :
// pBlockInfo->window.skey + step; pQueryAttr->limit.offset = 0; return;
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// if (QUERY_IS_ASC_QUERY(pQueryAttr)) {
// pQueryAttr->pos = (int32_t)pQueryAttr->limit.offset;
// } else {
// pQueryAttr->pos = pBlockInfo->rows - (int32_t)pQueryAttr->limit.offset - 1;
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// assert(pQueryAttr->pos >= 0 && pQueryAttr->pos <= pBlockInfo->rows - 1);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// SArray * pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pTsdbReadHandle, NULL);
// SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// // update the pQueryAttr->limit.offset value, and pQueryAttr->pos value
// TSKEY *keys = (TSKEY *) pColInfoData->pData;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// // update the offset value
// pTableQueryInfo->lastKey = keys[pQueryAttr->pos];
// pQueryAttr->limit.offset = 0;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// int32_t numOfRes = tableApplyFunctionsOnBlock(pRuntimeEnv, pBlockInfo, NULL, binarySearchForKey, pDataBlock);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// //qDebug("QInfo:0x%"PRIx64" check data block, brange:%" PRId64 "-%" PRId64 ", numBlocksOfStep:%d, numOfRes:%d,
// lastKey:%"PRId64, GET_TASKID(pRuntimeEnv),
// pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows, numOfRes, pQuery->current->lastKey);
// }
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// void skipBlocks(STaskRuntimeEnv *pRuntimeEnv) {
// STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// if (pQueryAttr->limit.offset <= 0 || pQueryAttr->numOfFilterCols > 0) {
// return;
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// pQueryAttr->pos = 0;
// int32_t step = GET_FORWARD_DIRECTION_FACTOR(pQueryAttr->order.order);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// STableQueryInfo* pTableQueryInfo = pRuntimeEnv->current;
// TsdbQueryHandleT pTsdbReadHandle = pRuntimeEnv->pTsdbReadHandle;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER;
// while (tsdbNextDataBlock(pTsdbReadHandle)) {
// if (isTaskKilled(pRuntimeEnv->qinfo)) {
// longjmp(pRuntimeEnv->env, TSDB_CODE_TSC_QUERY_CANCELLED);
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// tsdbRetrieveDataBlockInfo(pTsdbReadHandle, &blockInfo);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// if (pQueryAttr->limit.offset > blockInfo.rows) {
// pQueryAttr->limit.offset -= blockInfo.rows;
// pTableQueryInfo->lastKey = (QUERY_IS_ASC_QUERY(pQueryAttr)) ? blockInfo.window.ekey : blockInfo.window.skey;
// pTableQueryInfo->lastKey += step;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// //qDebug("QInfo:0x%"PRIx64" skip rows:%d, offset:%" PRId64, GET_TASKID(pRuntimeEnv), blockInfo.rows,
// pQuery->limit.offset);
// } else { // find the appropriated start position in current block
// updateOffsetVal(pRuntimeEnv, &blockInfo);
// break;
// }
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// if (terrno != TSDB_CODE_SUCCESS) {
// longjmp(pRuntimeEnv->env, terrno);
// }
// }
// static TSKEY doSkipIntervalProcess(STaskRuntimeEnv* pRuntimeEnv, STimeWindow* win, SDataBlockInfo* pBlockInfo,
// STableQueryInfo* pTableQueryInfo) {
// STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr;
// SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// assert(pQueryAttr->limit.offset == 0);
// STimeWindow tw = *win;
// getNextTimeWindow(pQueryAttr, &tw);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// if ((tw.skey <= pBlockInfo->window.ekey && QUERY_IS_ASC_QUERY(pQueryAttr)) ||
// (tw.ekey >= pBlockInfo->window.skey && !QUERY_IS_ASC_QUERY(pQueryAttr))) {
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// // load the data block and check data remaining in current data block
// // TODO optimize performance
// SArray * pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pTsdbReadHandle, NULL);
// SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// tw = *win;
// int32_t startPos =
// getNextQualifiedWindow(pQueryAttr, &tw, pBlockInfo, pColInfoData->pData, binarySearchForKey, -1);
// assert(startPos >= 0);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// // set the abort info
// pQueryAttr->pos = startPos;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// // reset the query start timestamp
// pTableQueryInfo->win.skey = ((TSKEY *)pColInfoData->pData)[startPos];
// pQueryAttr->window.skey = pTableQueryInfo->win.skey;
// TSKEY key = pTableQueryInfo->win.skey;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// pWindowResInfo->prevSKey = tw.skey;
// int32_t index = pRuntimeEnv->resultRowInfo.curIndex;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// int32_t numOfRes = tableApplyFunctionsOnBlock(pRuntimeEnv, pBlockInfo, NULL, binarySearchForKey, pDataBlock);
// pRuntimeEnv->resultRowInfo.curIndex = index; // restore the window index
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// //qDebug("QInfo:0x%"PRIx64" check data block, brange:%" PRId64 "-%" PRId64 ", numOfRows:%d, numOfRes:%d,
// lastKey:%" PRId64,
// GET_TASKID(pRuntimeEnv), pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows, numOfRes,
// pQueryAttr->current->lastKey);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// return key;
// } else { // do nothing
// pQueryAttr->window.skey = tw.skey;
// pWindowResInfo->prevSKey = tw.skey;
// pTableQueryInfo->lastKey = tw.skey;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// return tw.skey;
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// return true;
// }
// static bool skipTimeInterval(STaskRuntimeEnv *pRuntimeEnv, TSKEY* start) {
// STaskAttr *pQueryAttr = pRuntimeEnv->pQueryAttr;
// if (QUERY_IS_ASC_QUERY(pQueryAttr)) {
// assert(*start <= pRuntimeEnv->current->lastKey);
// } else {
// assert(*start >= pRuntimeEnv->current->lastKey);
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// // if queried with value filter, do NOT forward query start position
// if (pQueryAttr->limit.offset <= 0 || pQueryAttr->numOfFilterCols > 0 || pRuntimeEnv->pTsBuf != NULL ||
// pRuntimeEnv->pFillInfo != NULL) {
// return true;
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// /*
// * 1. for interval without interpolation query we forward pQueryAttr->interval.interval at a time for
// * pQueryAttr->limit.offset times. Since hole exists, pQueryAttr->interval.interval*pQueryAttr->limit.offset
// value is
// * not valid. otherwise, we only forward pQueryAttr->limit.offset number of points
// */
// assert(pRuntimeEnv->resultRowInfo.prevSKey == TSKEY_INITIAL_VAL);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// STimeWindow w = TSWINDOW_INITIALIZER;
// bool ascQuery = QUERY_IS_ASC_QUERY(pQueryAttr);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// SResultRowInfo *pWindowResInfo = &pRuntimeEnv->resultRowInfo;
// STableQueryInfo *pTableQueryInfo = pRuntimeEnv->current;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// SDataBlockInfo blockInfo = SDATA_BLOCK_INITIALIZER;
// while (tsdbNextDataBlock(pRuntimeEnv->pTsdbReadHandle)) {
// tsdbRetrieveDataBlockInfo(pRuntimeEnv->pTsdbReadHandle, &blockInfo);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// if (QUERY_IS_ASC_QUERY(pQueryAttr)) {
// if (pWindowResInfo->prevSKey == TSKEY_INITIAL_VAL) {
// getAlignQueryTimeWindow(pQueryAttr, blockInfo.window.skey, blockInfo.window.skey, pQueryAttr->window.ekey,
// &w); pWindowResInfo->prevSKey = w.skey;
// }
// } else {
// getAlignQueryTimeWindow(pQueryAttr, blockInfo.window.ekey, pQueryAttr->window.ekey, blockInfo.window.ekey, &w);
// pWindowResInfo->prevSKey = w.skey;
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// // the first time window
// STimeWindow win = getActiveTimeWindow(pWindowResInfo, pWindowResInfo->prevSKey, pQueryAttr);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// while (pQueryAttr->limit.offset > 0) {
// STimeWindow tw = win;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// if ((win.ekey <= blockInfo.window.ekey && ascQuery) || (win.ekey >= blockInfo.window.skey && !ascQuery)) {
// pQueryAttr->limit.offset -= 1;
// pWindowResInfo->prevSKey = win.skey;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// // current time window is aligned with blockInfo.window.ekey
// // restart it from next data block by set prevSKey to be TSKEY_INITIAL_VAL;
// if ((win.ekey == blockInfo.window.ekey && ascQuery) || (win.ekey == blockInfo.window.skey && !ascQuery)) {
// pWindowResInfo->prevSKey = TSKEY_INITIAL_VAL;
// }
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// if (pQueryAttr->limit.offset == 0) {
// *start = doSkipIntervalProcess(pRuntimeEnv, &win, &blockInfo, pTableQueryInfo);
// return true;
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// // current window does not ended in current data block, try next data block
// getNextTimeWindow(pQueryAttr, &tw);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// /*
// * If the next time window still starts from current data block,
// * load the primary timestamp column first, and then find the start position for the next queried time window.
// * Note that only the primary timestamp column is required.
// * TODO: Optimize for this cases. All data blocks are not needed to be loaded, only if the first actually
// required
// * time window resides in current data block.
// */
// if ((tw.skey <= blockInfo.window.ekey && ascQuery) || (tw.ekey >= blockInfo.window.skey && !ascQuery)) {
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// SArray *pDataBlock = tsdbRetrieveDataBlock(pRuntimeEnv->pTsdbReadHandle, NULL);
// SColumnInfoData *pColInfoData = taosArrayGet(pDataBlock, 0);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// if ((win.ekey > blockInfo.window.ekey && ascQuery) || (win.ekey < blockInfo.window.skey && !ascQuery)) {
// pQueryAttr->limit.offset -= 1;
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// if (pQueryAttr->limit.offset == 0) {
// *start = doSkipIntervalProcess(pRuntimeEnv, &win, &blockInfo, pTableQueryInfo);
// return true;
// } else {
// tw = win;
// int32_t startPos =
// getNextQualifiedWindow(pQueryAttr, &tw, &blockInfo, pColInfoData->pData, binarySearchForKey, -1);
// assert(startPos >= 0);
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// // set the abort info
// pQueryAttr->pos = startPos;
// pTableQueryInfo->lastKey = ((TSKEY *)pColInfoData->pData)[startPos];
// pWindowResInfo->prevSKey = tw.skey;
// win = tw;
// }
// } else {
// break; // offset is not 0, and next time window begins or ends in the next block.
// }
// }
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// // check for error
// if (terrno != TSDB_CODE_SUCCESS) {
// longjmp(pRuntimeEnv->env, terrno);
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// return true;
// }
2021-11-02 05:37:31 +00:00
2022-02-22 05:12:03 +00:00
int32_t appendDownstream ( SOperatorInfo * p , SOperatorInfo * * pDownstream , int32_t num ) {
2022-01-08 08:28:44 +00:00
if ( p - > pDownstream = = NULL ) {
2022-01-08 14:59:24 +00:00
assert ( p - > numOfDownstream = = 0 ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-25 16:29:53 +00:00
p - > pDownstream = taosMemoryCalloc ( 1 , num * POINTER_BYTES ) ;
2022-02-22 05:12:03 +00:00
if ( p - > pDownstream = = NULL ) {
return TSDB_CODE_OUT_OF_MEMORY ;
}
memcpy ( p - > pDownstream , pDownstream , num * POINTER_BYTES ) ;
p - > numOfDownstream = num ;
return TSDB_CODE_SUCCESS ;
2021-11-02 05:37:31 +00:00
}
2022-05-26 08:05:27 +00:00
static void doDestroyTableList ( STableListInfo * pTableqinfoList ) ;
2021-11-02 05:37:31 +00:00
2022-01-10 11:48:21 +00:00
static void doTableQueryInfoTimeWindowCheck ( SExecTaskInfo * pTaskInfo , STableQueryInfo * pTableQueryInfo , int32_t order ) {
2022-02-13 06:34:00 +00:00
#if 0
if ( order = = TSDB_ORDER_ASC ) {
2021-11-02 05:37:31 +00:00
assert (
( pTableQueryInfo - > win . skey < = pTableQueryInfo - > win . ekey ) & &
2022-01-08 14:59:24 +00:00
( pTableQueryInfo - > lastKey > = pTaskInfo - > window . skey ) & &
( pTableQueryInfo - > win . skey > = pTaskInfo - > window . skey & & pTableQueryInfo - > win . ekey < = pTaskInfo - > window . ekey ) ) ;
2021-11-02 05:37:31 +00:00
} else {
assert (
( pTableQueryInfo - > win . skey > = pTableQueryInfo - > win . ekey ) & &
2022-01-08 14:59:24 +00:00
( pTableQueryInfo - > lastKey < = pTaskInfo - > window . skey ) & &
( pTableQueryInfo - > win . skey < = pTaskInfo - > window . skey & & pTableQueryInfo - > win . ekey > = pTaskInfo - > window . ekey ) ) ;
2021-11-02 05:37:31 +00:00
}
2022-02-13 06:34:00 +00:00
# endif
2021-11-02 05:37:31 +00:00
}
2022-06-15 08:26:43 +00:00
typedef struct SFetchRspHandleWrapper {
uint32_t exchangeId ;
int32_t sourceIndex ;
} SFetchRspHandleWrapper ;
2021-11-02 05:37:31 +00:00
2022-06-29 09:15:08 +00:00
int32_t loadRemoteDataCallback ( void * param , SDataBuf * pMsg , int32_t code ) {
2022-06-16 01:31:22 +00:00
SFetchRspHandleWrapper * pWrapper = ( SFetchRspHandleWrapper * ) param ;
2022-06-15 08:26:43 +00:00
SExchangeInfo * pExchangeInfo = taosAcquireRef ( exchangeObjRefPool , pWrapper - > exchangeId ) ;
if ( pExchangeInfo = = NULL ) {
qWarn ( " failed to acquire exchange operator, since it may have been released " ) ;
return TSDB_CODE_SUCCESS ;
}
2022-06-16 01:31:22 +00:00
int32_t index = pWrapper - > sourceIndex ;
2022-06-15 08:26:43 +00:00
SSourceDataInfo * pSourceDataInfo = taosArrayGet ( pExchangeInfo - > pSourceDataInfo , index ) ;
2022-06-14 11:47:25 +00:00
2022-04-01 08:09:00 +00:00
if ( code = = TSDB_CODE_SUCCESS ) {
pSourceDataInfo - > pRsp = pMsg - > pData ;
2022-01-16 07:56:48 +00:00
2022-04-01 08:09:00 +00:00
SRetrieveTableRsp * pRsp = pSourceDataInfo - > pRsp ;
pRsp - > numOfRows = htonl ( pRsp - > numOfRows ) ;
2022-04-23 10:29:45 +00:00
pRsp - > compLen = htonl ( pRsp - > compLen ) ;
2022-04-28 10:08:56 +00:00
pRsp - > numOfCols = htonl ( pRsp - > numOfCols ) ;
2022-04-23 10:29:45 +00:00
pRsp - > useconds = htobe64 ( pRsp - > useconds ) ;
2022-06-04 16:47:03 +00:00
2022-06-14 11:47:25 +00:00
ASSERT ( pRsp ! = NULL ) ;
2022-06-15 08:26:43 +00:00
qDebug ( " %s fetch rsp received, index:%d, rows:%d " , pSourceDataInfo - > taskId , index , pRsp - > numOfRows ) ;
2022-04-01 08:09:00 +00:00
} else {
pSourceDataInfo - > code = code ;
}
2022-01-19 06:47:53 +00:00
2022-03-05 07:58:28 +00:00
pSourceDataInfo - > status = EX_SOURCE_DATA_READY ;
2022-06-15 08:26:43 +00:00
tsem_post ( & pExchangeInfo - > ready ) ;
taosReleaseRef ( exchangeObjRefPool , pWrapper - > exchangeId ) ;
taosMemoryFree ( pWrapper ) ;
2022-04-06 11:50:24 +00:00
return TSDB_CODE_SUCCESS ;
2022-01-19 06:47:53 +00:00
}
static void destroySendMsgInfo ( SMsgSendInfo * pMsgBody ) {
assert ( pMsgBody ! = NULL ) ;
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pMsgBody - > msgInfo . pData ) ;
taosMemoryFreeClear ( pMsgBody ) ;
2022-01-19 06:47:53 +00:00
}
2022-01-26 01:20:48 +00:00
void qProcessFetchRsp ( void * parent , SRpcMsg * pMsg , SEpSet * pEpSet ) {
2022-05-16 07:17:11 +00:00
SMsgSendInfo * pSendInfo = ( SMsgSendInfo * ) pMsg - > info . ahandle ;
assert ( pMsg - > info . ahandle ! = NULL ) ;
2022-01-19 06:47:53 +00:00
SDataBuf buf = { . len = pMsg - > contLen , . pData = NULL } ;
if ( pMsg - > contLen > 0 ) {
2022-03-25 16:29:53 +00:00
buf . pData = taosMemoryCalloc ( 1 , pMsg - > contLen ) ;
2022-01-19 06:47:53 +00:00
if ( buf . pData = = NULL ) {
terrno = TSDB_CODE_OUT_OF_MEMORY ;
pMsg - > code = TSDB_CODE_OUT_OF_MEMORY ;
} else {
memcpy ( buf . pData , pMsg - > pCont , pMsg - > contLen ) ;
}
}
pSendInfo - > fp ( pSendInfo - > param , & buf , pMsg - > code ) ;
rpcFreeCont ( pMsg - > pCont ) ;
destroySendMsgInfo ( pSendInfo ) ;
2022-01-16 07:56:48 +00:00
}
2022-03-29 07:24:25 +00:00
static int32_t doSendFetchDataRequest ( SExchangeInfo * pExchangeInfo , SExecTaskInfo * pTaskInfo , int32_t sourceIndex ) {
2022-02-11 06:33:57 +00:00
size_t totalSources = taosArrayGetSize ( pExchangeInfo - > pSources ) ;
2022-01-16 07:56:48 +00:00
2022-03-25 16:29:53 +00:00
SResFetchReq * pMsg = taosMemoryCalloc ( 1 , sizeof ( SResFetchReq ) ) ;
2022-02-11 06:33:57 +00:00
if ( NULL = = pMsg ) {
pTaskInfo - > code = TSDB_CODE_QRY_OUT_OF_MEMORY ;
return pTaskInfo - > code ;
}
2022-01-16 07:56:48 +00:00
2022-03-29 07:24:25 +00:00
SDownstreamSourceNode * pSource = taosArrayGet ( pExchangeInfo - > pSources , sourceIndex ) ;
SSourceDataInfo * pDataInfo = taosArrayGet ( pExchangeInfo - > pSourceDataInfo , sourceIndex ) ;
2022-01-22 07:42:37 +00:00
2022-06-04 16:47:03 +00:00
ASSERT ( pDataInfo - > status = = EX_SOURCE_DATA_NOT_READY ) ;
2022-07-02 02:55:01 +00:00
qDebug ( " %s build fetch msg and send to vgId:%d, ep:%s, taskId:0x% " PRIx64 " , execId:%d, %d/% " PRIzu ,
GET_TASKID ( pTaskInfo ) , pSource - > addr . nodeId , pSource - > addr . epSet . eps [ 0 ] . fqdn , pSource - > taskId , pSource - > execId ,
sourceIndex , totalSources ) ;
2022-02-11 06:33:57 +00:00
pMsg - > header . vgId = htonl ( pSource - > addr . nodeId ) ;
pMsg - > sId = htobe64 ( pSource - > schedId ) ;
pMsg - > taskId = htobe64 ( pSource - > taskId ) ;
pMsg - > queryId = htobe64 ( pTaskInfo - > id . queryId ) ;
2022-06-30 03:04:49 +00:00
pMsg - > execId = htonl ( pSource - > execId ) ;
2022-02-11 06:33:57 +00:00
// send the fetch remote task result reques
2022-03-25 16:29:53 +00:00
SMsgSendInfo * pMsgSendInfo = taosMemoryCalloc ( 1 , sizeof ( SMsgSendInfo ) ) ;
2022-02-11 06:33:57 +00:00
if ( NULL = = pMsgSendInfo ) {
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pMsg ) ;
2022-02-11 06:33:57 +00:00
qError ( " %s prepare message %d failed " , GET_TASKID ( pTaskInfo ) , ( int32_t ) sizeof ( SMsgSendInfo ) ) ;
pTaskInfo - > code = TSDB_CODE_QRY_OUT_OF_MEMORY ;
return pTaskInfo - > code ;
2022-01-19 06:47:53 +00:00
}
2022-06-15 08:26:43 +00:00
SFetchRspHandleWrapper * pWrapper = taosMemoryCalloc ( 1 , sizeof ( SFetchRspHandleWrapper ) ) ;
2022-06-16 01:31:22 +00:00
pWrapper - > exchangeId = pExchangeInfo - > self ;
2022-06-15 08:26:43 +00:00
pWrapper - > sourceIndex = sourceIndex ;
pMsgSendInfo - > param = pWrapper ;
2022-02-11 06:33:57 +00:00
pMsgSendInfo - > msgInfo . pData = pMsg ;
pMsgSendInfo - > msgInfo . len = sizeof ( SResFetchReq ) ;
2022-07-06 12:33:23 +00:00
pMsgSendInfo - > msgType = pSource - > fetchMsgType ;
2022-02-11 06:33:57 +00:00
pMsgSendInfo - > fp = loadRemoteDataCallback ;
2022-01-16 07:56:48 +00:00
2022-02-11 06:33:57 +00:00
int64_t transporterId = 0 ;
2022-03-08 09:22:21 +00:00
int32_t code = asyncSendMsgToServer ( pExchangeInfo - > pTransporter , & pSource - > addr . epSet , & transporterId , pMsgSendInfo ) ;
2022-02-11 06:33:57 +00:00
return TSDB_CODE_SUCCESS ;
}
2022-06-17 11:01:45 +00:00
int32_t extractDataBlockFromFetchRsp ( SSDataBlock * pRes , SLoadRemoteDataInfo * pLoadInfo , int32_t numOfRows , char * pData ,
2022-06-20 06:40:13 +00:00
int32_t compLen , int32_t numOfOutput , int64_t startTs , uint64_t * total ,
SArray * pColList ) {
2022-03-22 15:29:38 +00:00
if ( pColList = = NULL ) { // data from other sources
2022-06-22 11:40:59 +00:00
blockDataCleanup ( pRes ) ;
2022-06-24 06:07:52 +00:00
// blockDataEnsureCapacity(pRes, numOfRows);
2022-06-28 05:59:49 +00:00
blockDecode ( pRes , numOfOutput , numOfRows , pData ) ;
2022-03-22 15:29:38 +00:00
} else { // extract data according to pColList
2022-03-18 16:13:07 +00:00
ASSERT ( numOfOutput = = taosArrayGetSize ( pColList ) ) ;
2022-04-13 11:27:47 +00:00
char * pStart = pData ;
int32_t numOfCols = htonl ( * ( int32_t * ) pStart ) ;
pStart + = sizeof ( int32_t ) ;
2022-04-27 08:37:19 +00:00
// todo refactor:extract method
2022-04-13 11:27:47 +00:00
SSysTableSchema * pSchema = ( SSysTableSchema * ) pStart ;
2022-04-23 10:29:45 +00:00
for ( int32_t i = 0 ; i < numOfCols ; + + i ) {
2022-04-13 11:27:47 +00:00
SSysTableSchema * p = ( SSysTableSchema * ) pStart ;
p - > colId = htons ( p - > colId ) ;
p - > bytes = htonl ( p - > bytes ) ;
pStart + = sizeof ( SSysTableSchema ) ;
}
2022-06-22 09:21:04 +00:00
SSDataBlock * pBlock = createDataBlock ( ) ;
2022-04-23 10:29:45 +00:00
for ( int32_t i = 0 ; i < numOfCols ; + + i ) {
2022-06-22 09:21:04 +00:00
SColumnInfoData idata = createColumnInfoData ( pSchema [ i ] . type , pSchema [ i ] . bytes , pSchema [ i ] . colId ) ;
blockDataAppendColInfo ( pBlock , & idata ) ;
2022-04-13 11:27:47 +00:00
}
2022-06-28 05:59:49 +00:00
blockDecode ( pBlock , numOfCols , numOfRows , pStart ) ;
2022-06-22 09:21:04 +00:00
blockDataEnsureCapacity ( pRes , numOfRows ) ;
2022-03-22 15:29:38 +00:00
// data from mnode
2022-06-13 02:23:16 +00:00
pRes - > info . rows = numOfRows ;
2022-06-22 11:34:56 +00:00
relocateColumnData ( pRes , pColList , pBlock - > pDataBlock , false ) ;
blockDataDestroy ( pBlock ) ;
2022-02-11 06:33:57 +00:00
}
2022-01-22 07:42:37 +00:00
2022-05-21 03:02:53 +00:00
// todo move this to time window aggregator, since the primary timestamp may not be known by exchange operator.
blockDataUpdateTsWindow ( pRes , 0 ) ;
2022-01-16 07:56:48 +00:00
2022-02-11 06:33:57 +00:00
int64_t el = taosGetTimestampUs ( ) - startTs ;
2022-01-16 07:56:48 +00:00
2022-03-11 11:29:43 +00:00
pLoadInfo - > totalRows + = numOfRows ;
pLoadInfo - > totalSize + = compLen ;
2022-01-16 07:56:48 +00:00
2022-03-11 11:29:43 +00:00
if ( total ! = NULL ) {
* total + = numOfRows ;
}
2021-11-02 05:37:31 +00:00
2022-03-11 11:29:43 +00:00
pLoadInfo - > totalElapsed + = el ;
2022-02-11 06:33:57 +00:00
return TSDB_CODE_SUCCESS ;
}
2022-01-22 07:42:37 +00:00
2022-03-29 07:24:25 +00:00
static void * setAllSourcesCompleted ( SOperatorInfo * pOperator , int64_t startTs ) {
SExchangeInfo * pExchangeInfo = pOperator - > info ;
2022-02-11 06:33:57 +00:00
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2022-01-22 14:51:48 +00:00
2022-05-26 08:29:52 +00:00
int64_t el = taosGetTimestampUs ( ) - startTs ;
2022-03-11 11:29:43 +00:00
SLoadRemoteDataInfo * pLoadInfo = & pExchangeInfo - > loadInfo ;
2022-05-24 07:54:39 +00:00
2022-03-11 11:29:43 +00:00
pLoadInfo - > totalElapsed + = el ;
2022-01-25 06:13:11 +00:00
2022-02-11 06:33:57 +00:00
size_t totalSources = taosArrayGetSize ( pExchangeInfo - > pSources ) ;
2022-03-29 07:24:25 +00:00
qDebug ( " %s all % " PRIzu " sources are exhausted, total rows: % " PRIu64 " bytes:% " PRIu64 " , elapsed:%.2f ms " ,
GET_TASKID ( pTaskInfo ) , totalSources , pLoadInfo - > totalRows , pLoadInfo - > totalSize ,
pLoadInfo - > totalElapsed / 1000.0 ) ;
2022-02-11 06:33:57 +00:00
doSetOperatorCompleted ( pOperator ) ;
return NULL ;
}
2022-03-29 07:24:25 +00:00
static SSDataBlock * concurrentlyLoadRemoteDataImpl ( SOperatorInfo * pOperator , SExchangeInfo * pExchangeInfo ,
SExecTaskInfo * pTaskInfo ) {
2022-02-11 06:33:57 +00:00
int32_t code = 0 ;
int64_t startTs = taosGetTimestampUs ( ) ;
size_t totalSources = taosArrayGetSize ( pExchangeInfo - > pSources ) ;
while ( 1 ) {
int32_t completed = 0 ;
for ( int32_t i = 0 ; i < totalSources ; + + i ) {
SSourceDataInfo * pDataInfo = taosArrayGet ( pExchangeInfo - > pSourceDataInfo , i ) ;
2022-03-29 12:07:38 +00:00
if ( pDataInfo - > status = = EX_SOURCE_DATA_EXHAUSTED ) {
2022-02-11 06:33:57 +00:00
completed + = 1 ;
2022-01-22 14:51:48 +00:00
continue ;
}
2021-11-02 05:37:31 +00:00
2022-03-29 12:07:38 +00:00
if ( pDataInfo - > status ! = EX_SOURCE_DATA_READY ) {
2022-02-11 06:33:57 +00:00
continue ;
}
2022-06-04 16:47:03 +00:00
if ( pDataInfo - > code ! = TSDB_CODE_SUCCESS ) {
code = pDataInfo - > code ;
goto _error ;
}
2022-03-29 07:24:25 +00:00
SRetrieveTableRsp * pRsp = pDataInfo - > pRsp ;
2022-03-09 09:32:12 +00:00
SDownstreamSourceNode * pSource = taosArrayGet ( pExchangeInfo - > pSources , i ) ;
2022-02-11 06:33:57 +00:00
2022-03-29 07:24:25 +00:00
SSDataBlock * pRes = pExchangeInfo - > pResult ;
2022-03-11 11:29:43 +00:00
SLoadRemoteDataInfo * pLoadInfo = & pExchangeInfo - > loadInfo ;
2022-02-11 06:33:57 +00:00
if ( pRsp - > numOfRows = = 0 ) {
2022-07-02 02:55:01 +00:00
qDebug ( " %s vgId:%d, taskId:0x% " PRIx64 " execId:%d index:%d completed, rowsOfSource:% " PRIu64
" , totalRows:% " PRIu64 " , completed:%d try next %d/% " PRIzu ,
2022-06-30 03:04:49 +00:00
GET_TASKID ( pTaskInfo ) , pSource - > addr . nodeId , pSource - > taskId , pSource - > execId , i , pDataInfo - > totalRows ,
2022-06-14 13:01:08 +00:00
pExchangeInfo - > loadInfo . totalRows , completed + 1 , i + 1 , totalSources ) ;
2022-03-29 12:07:38 +00:00
pDataInfo - > status = EX_SOURCE_DATA_EXHAUSTED ;
2022-02-11 06:33:57 +00:00
completed + = 1 ;
2022-05-28 14:13:26 +00:00
taosMemoryFreeClear ( pDataInfo - > pRsp ) ;
2022-02-11 06:33:57 +00:00
continue ;
}
2022-01-19 08:20:13 +00:00
2022-03-11 11:29:43 +00:00
SRetrieveTableRsp * pTableRsp = pDataInfo - > pRsp ;
2022-06-20 06:40:13 +00:00
code =
extractDataBlockFromFetchRsp ( pExchangeInfo - > pResult , pLoadInfo , pTableRsp - > numOfRows , pTableRsp - > data ,
pTableRsp - > compLen , pTableRsp - > numOfCols , startTs , & pDataInfo - > totalRows , NULL ) ;
2022-02-11 06:33:57 +00:00
if ( code ! = 0 ) {
2022-05-30 11:08:14 +00:00
taosMemoryFreeClear ( pDataInfo - > pRsp ) ;
2022-01-22 07:42:37 +00:00
goto _error ;
}
2022-02-11 06:33:57 +00:00
if ( pRsp - > completed = = 1 ) {
2022-07-02 02:55:01 +00:00
qDebug ( " %s fetch msg rsp from vgId:%d, taskId:0x% " PRIx64
" execId:%d "
2022-06-16 01:31:22 +00:00
" index:%d completed, numOfRows:%d, rowsOfSource:% " PRIu64 " , totalRows:% " PRIu64 " , totalBytes:% " PRIu64
" , completed:%d try next %d/% " PRIzu ,
2022-07-02 02:55:01 +00:00
GET_TASKID ( pTaskInfo ) , pSource - > addr . nodeId , pSource - > taskId , pSource - > execId , i , pRes - > info . rows ,
pDataInfo - > totalRows , pLoadInfo - > totalRows , pLoadInfo - > totalSize , completed + 1 , i + 1 , totalSources ) ;
2022-06-14 13:01:08 +00:00
completed + = 1 ;
2022-03-29 12:07:38 +00:00
pDataInfo - > status = EX_SOURCE_DATA_EXHAUSTED ;
2022-02-11 06:33:57 +00:00
} else {
2022-06-30 03:04:49 +00:00
qDebug ( " %s fetch msg rsp from vgId:%d, taskId:0x% " PRIx64 " execId:%d numOfRows:%d, totalRows:% " PRIu64
2022-04-23 10:29:45 +00:00
" , totalBytes:% " PRIu64 ,
2022-07-02 02:55:01 +00:00
GET_TASKID ( pTaskInfo ) , pSource - > addr . nodeId , pSource - > taskId , pSource - > execId , pRes - > info . rows ,
pLoadInfo - > totalRows , pLoadInfo - > totalSize ) ;
2022-02-11 06:33:57 +00:00
}
2022-06-04 16:47:03 +00:00
taosMemoryFreeClear ( pDataInfo - > pRsp ) ;
2022-03-29 12:07:38 +00:00
if ( pDataInfo - > status ! = EX_SOURCE_DATA_EXHAUSTED ) {
pDataInfo - > status = EX_SOURCE_DATA_NOT_READY ;
2022-02-11 06:33:57 +00:00
code = doSendFetchDataRequest ( pExchangeInfo , pTaskInfo , i ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
2022-05-30 11:08:14 +00:00
taosMemoryFreeClear ( pDataInfo - > pRsp ) ;
2022-02-11 06:33:57 +00:00
goto _error ;
}
}
return pExchangeInfo - > pResult ;
}
2022-06-15 08:26:43 +00:00
if ( completed = = totalSources ) {
2022-02-11 06:33:57 +00:00
return setAllSourcesCompleted ( pOperator , startTs ) ;
}
}
_error :
pTaskInfo - > code = code ;
return NULL ;
}
2022-03-29 07:24:25 +00:00
static int32_t prepareConcurrentlyLoad ( SOperatorInfo * pOperator ) {
SExchangeInfo * pExchangeInfo = pOperator - > info ;
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2022-02-11 06:33:57 +00:00
2022-03-29 07:24:25 +00:00
size_t totalSources = taosArrayGetSize ( pExchangeInfo - > pSources ) ;
2022-02-11 06:33:57 +00:00
int64_t startTs = taosGetTimestampUs ( ) ;
// Asynchronously send all fetch requests to all sources.
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < totalSources ; + + i ) {
2022-02-11 06:33:57 +00:00
int32_t code = doSendFetchDataRequest ( pExchangeInfo , pTaskInfo , i ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
2022-03-05 07:58:28 +00:00
pTaskInfo - > code = code ;
return code ;
2022-02-11 06:33:57 +00:00
}
}
int64_t endTs = taosGetTimestampUs ( ) ;
2022-06-15 08:26:43 +00:00
qDebug ( " %s send all fetch requests to % " PRIzu " sources completed, elapsed:%.2fms " , GET_TASKID ( pTaskInfo ) ,
2022-06-16 01:31:22 +00:00
totalSources , ( endTs - startTs ) / 1000.0 ) ;
2022-02-11 06:33:57 +00:00
2022-06-04 16:47:03 +00:00
pOperator - > status = OP_RES_TO_RETURN ;
2022-03-05 07:58:28 +00:00
pOperator - > cost . openCost = taosGetTimestampUs ( ) - startTs ;
2022-02-11 06:33:57 +00:00
2022-06-15 08:26:43 +00:00
tsem_wait ( & pExchangeInfo - > ready ) ;
2022-03-05 07:58:28 +00:00
return TSDB_CODE_SUCCESS ;
2022-02-11 06:33:57 +00:00
}
2022-03-29 07:24:25 +00:00
static SSDataBlock * seqLoadRemoteData ( SOperatorInfo * pOperator ) {
SExchangeInfo * pExchangeInfo = pOperator - > info ;
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2022-02-11 06:33:57 +00:00
2022-03-29 07:24:25 +00:00
size_t totalSources = taosArrayGetSize ( pExchangeInfo - > pSources ) ;
2022-02-11 06:33:57 +00:00
int64_t startTs = taosGetTimestampUs ( ) ;
2022-01-22 07:42:37 +00:00
2022-03-29 07:24:25 +00:00
while ( 1 ) {
2022-02-11 06:33:57 +00:00
if ( pExchangeInfo - > current > = totalSources ) {
return setAllSourcesCompleted ( pOperator , startTs ) ;
2022-01-22 07:42:37 +00:00
}
2022-01-16 07:56:48 +00:00
2022-02-11 06:33:57 +00:00
doSendFetchDataRequest ( pExchangeInfo , pTaskInfo , pExchangeInfo - > current ) ;
tsem_wait ( & pExchangeInfo - > ready ) ;
2022-04-23 10:29:45 +00:00
SSourceDataInfo * pDataInfo = taosArrayGet ( pExchangeInfo - > pSourceDataInfo , pExchangeInfo - > current ) ;
2022-03-09 09:32:12 +00:00
SDownstreamSourceNode * pSource = taosArrayGet ( pExchangeInfo - > pSources , pExchangeInfo - > current ) ;
2022-02-11 06:33:57 +00:00
2022-04-01 08:09:00 +00:00
if ( pDataInfo - > code ! = TSDB_CODE_SUCCESS ) {
2022-07-02 02:55:01 +00:00
qError ( " %s vgId:%d, taskID:0x% " PRIx64 " execId:%d error happens, code:%s " , GET_TASKID ( pTaskInfo ) ,
pSource - > addr . nodeId , pSource - > taskId , pSource - > execId , tstrerror ( pDataInfo - > code ) ) ;
2022-04-01 08:09:00 +00:00
pOperator - > pTaskInfo - > code = pDataInfo - > code ;
return NULL ;
}
2022-03-29 07:24:25 +00:00
SRetrieveTableRsp * pRsp = pDataInfo - > pRsp ;
2022-03-11 11:29:43 +00:00
SLoadRemoteDataInfo * pLoadInfo = & pExchangeInfo - > loadInfo ;
2022-02-11 06:33:57 +00:00
if ( pRsp - > numOfRows = = 0 ) {
2022-07-02 02:55:01 +00:00
qDebug ( " %s vgId:%d, taskID:0x% " PRIx64 " execId:%d %d of total completed, rowsOfSource:% " PRIu64
" , totalRows:% " PRIu64 " try next " ,
2022-06-30 03:04:49 +00:00
GET_TASKID ( pTaskInfo ) , pSource - > addr . nodeId , pSource - > taskId , pSource - > execId , pExchangeInfo - > current + 1 ,
2022-03-11 11:29:43 +00:00
pDataInfo - > totalRows , pLoadInfo - > totalRows ) ;
2022-01-19 08:20:13 +00:00
2022-03-29 12:07:38 +00:00
pDataInfo - > status = EX_SOURCE_DATA_EXHAUSTED ;
2022-02-11 06:33:57 +00:00
pExchangeInfo - > current + = 1 ;
2022-05-28 14:13:26 +00:00
taosMemoryFreeClear ( pDataInfo - > pRsp ) ;
2022-02-11 06:33:57 +00:00
continue ;
}
2022-01-25 06:13:11 +00:00
2022-03-29 07:24:25 +00:00
SSDataBlock * pRes = pExchangeInfo - > pResult ;
2022-03-11 11:29:43 +00:00
SRetrieveTableRsp * pTableRsp = pDataInfo - > pRsp ;
2022-03-29 07:24:25 +00:00
int32_t code =
2022-06-17 11:01:45 +00:00
extractDataBlockFromFetchRsp ( pExchangeInfo - > pResult , pLoadInfo , pTableRsp - > numOfRows , pTableRsp - > data ,
2022-06-20 06:40:13 +00:00
pTableRsp - > compLen , pTableRsp - > numOfCols , startTs , & pDataInfo - > totalRows , NULL ) ;
2022-01-22 07:42:37 +00:00
if ( pRsp - > completed = = 1 ) {
2022-06-30 03:04:49 +00:00
qDebug ( " %s fetch msg rsp from vgId:%d, taskId:0x% " PRIx64 " execId:%d numOfRows:%d, rowsOfSource:% " PRIu64
2022-03-29 07:24:25 +00:00
" , totalRows:% " PRIu64 " , totalBytes:% " PRIu64 " try next %d/% " PRIzu ,
2022-07-02 02:55:01 +00:00
GET_TASKID ( pTaskInfo ) , pSource - > addr . nodeId , pSource - > taskId , pSource - > execId , pRes - > info . rows ,
pDataInfo - > totalRows , pLoadInfo - > totalRows , pLoadInfo - > totalSize , pExchangeInfo - > current + 1 ,
totalSources ) ;
2022-01-22 07:42:37 +00:00
2022-03-29 12:07:38 +00:00
pDataInfo - > status = EX_SOURCE_DATA_EXHAUSTED ;
2022-01-22 07:42:37 +00:00
pExchangeInfo - > current + = 1 ;
} else {
2022-06-30 03:04:49 +00:00
qDebug ( " %s fetch msg rsp from vgId:%d, taskId:0x% " PRIx64 " execId:%d numOfRows:%d, totalRows:% " PRIu64
2022-03-29 07:24:25 +00:00
" , totalBytes:% " PRIu64 ,
2022-07-02 02:55:01 +00:00
GET_TASKID ( pTaskInfo ) , pSource - > addr . nodeId , pSource - > taskId , pSource - > execId , pRes - > info . rows ,
pLoadInfo - > totalRows , pLoadInfo - > totalSize ) ;
2022-01-22 07:42:37 +00:00
}
2022-05-24 07:54:39 +00:00
pOperator - > resultInfo . totalRows + = pRes - > info . rows ;
2022-05-30 11:08:14 +00:00
taosMemoryFreeClear ( pDataInfo - > pRsp ) ;
2022-01-22 07:42:37 +00:00
return pExchangeInfo - > pResult ;
}
2022-02-11 06:33:57 +00:00
}
2022-03-29 07:24:25 +00:00
static int32_t prepareLoadRemoteData ( SOperatorInfo * pOperator ) {
2022-03-12 07:28:15 +00:00
if ( OPTR_IS_OPENED ( pOperator ) ) {
2022-03-05 07:58:28 +00:00
return TSDB_CODE_SUCCESS ;
}
2022-05-24 07:54:39 +00:00
int64_t st = taosGetTimestampUs ( ) ;
2022-03-29 07:24:25 +00:00
SExchangeInfo * pExchangeInfo = pOperator - > info ;
2022-05-24 07:54:39 +00:00
if ( ! pExchangeInfo - > seqLoadData ) {
2022-03-05 07:58:28 +00:00
int32_t code = prepareConcurrentlyLoad ( pOperator ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
return code ;
}
}
2022-03-12 07:28:15 +00:00
OPTR_SET_OPENED ( pOperator ) ;
2022-05-24 07:54:39 +00:00
pOperator - > cost . openCost = ( taosGetTimestampUs ( ) - st ) / 1000.0 ;
2022-03-05 07:58:28 +00:00
return TSDB_CODE_SUCCESS ;
}
2022-05-03 07:27:13 +00:00
static SSDataBlock * doLoadRemoteData ( SOperatorInfo * pOperator ) {
2022-03-29 07:24:25 +00:00
SExchangeInfo * pExchangeInfo = pOperator - > info ;
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2022-02-11 06:33:57 +00:00
2022-04-26 12:26:32 +00:00
pTaskInfo - > code = pOperator - > fpSet . _openFn ( pOperator ) ;
2022-03-15 06:37:26 +00:00
if ( pTaskInfo - > code ! = TSDB_CODE_SUCCESS ) {
2022-03-05 07:58:28 +00:00
return NULL ;
}
2022-02-11 06:33:57 +00:00
2022-03-29 07:24:25 +00:00
size_t totalSources = taosArrayGetSize ( pExchangeInfo - > pSources ) ;
2022-03-11 11:29:43 +00:00
SLoadRemoteDataInfo * pLoadInfo = & pExchangeInfo - > loadInfo ;
2022-03-18 03:12:49 +00:00
2022-02-11 06:33:57 +00:00
if ( pOperator - > status = = OP_EXEC_DONE ) {
2022-03-29 07:24:25 +00:00
qDebug ( " %s all % " PRIzu " source(s) are exhausted, total rows:% " PRIu64 " bytes:% " PRIu64 " , elapsed:%.2f ms " ,
GET_TASKID ( pTaskInfo ) , totalSources , pLoadInfo - > totalRows , pLoadInfo - > totalSize ,
pLoadInfo - > totalElapsed / 1000.0 ) ;
2022-02-11 06:33:57 +00:00
return NULL ;
}
if ( pExchangeInfo - > seqLoadData ) {
return seqLoadRemoteData ( pOperator ) ;
} else {
2022-06-04 16:47:03 +00:00
return concurrentlyLoadRemoteDataImpl ( pOperator , pExchangeInfo , pTaskInfo ) ;
2022-02-11 06:33:57 +00:00
}
2022-01-19 06:47:53 +00:00
}
2022-01-16 07:56:48 +00:00
2022-06-14 13:01:08 +00:00
static int32_t initDataSource ( int32_t numOfSources , SExchangeInfo * pInfo , const char * id ) {
2022-02-11 06:33:57 +00:00
pInfo - > pSourceDataInfo = taosArrayInit ( numOfSources , sizeof ( SSourceDataInfo ) ) ;
2022-03-04 07:53:30 +00:00
if ( pInfo - > pSourceDataInfo = = NULL ) {
return TSDB_CODE_OUT_OF_MEMORY ;
2022-02-11 06:33:57 +00:00
}
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < numOfSources ; + + i ) {
2022-02-11 06:33:57 +00:00
SSourceDataInfo dataInfo = { 0 } ;
2022-03-05 07:58:28 +00:00
dataInfo . status = EX_SOURCE_DATA_NOT_READY ;
2022-06-15 08:26:43 +00:00
dataInfo . taskId = id ;
2022-03-29 07:24:25 +00:00
dataInfo . index = i ;
2022-06-16 01:31:22 +00:00
SSourceDataInfo * pDs = taosArrayPush ( pInfo - > pSourceDataInfo , & dataInfo ) ;
2022-06-15 08:26:43 +00:00
if ( pDs = = NULL ) {
2022-03-04 07:53:30 +00:00
taosArrayDestroy ( pInfo - > pSourceDataInfo ) ;
return TSDB_CODE_OUT_OF_MEMORY ;
}
}
return TSDB_CODE_SUCCESS ;
}
2022-06-11 06:51:54 +00:00
static int32_t initExchangeOperator ( SExchangePhysiNode * pExNode , SExchangeInfo * pInfo , const char * id ) {
2022-06-05 06:48:15 +00:00
size_t numOfSources = LIST_LENGTH ( pExNode - > pSrcEndPoints ) ;
2022-03-04 07:53:30 +00:00
2022-06-11 06:51:54 +00:00
if ( numOfSources = = 0 ) {
2022-06-15 11:08:25 +00:00
qError ( " %s invalid number: %d of sources in exchange operator " , id , ( int32_t ) numOfSources ) ;
2022-06-11 06:51:54 +00:00
return TSDB_CODE_INVALID_PARA ;
}
2022-03-10 10:07:07 +00:00
pInfo - > pSources = taosArrayInit ( numOfSources , sizeof ( SDownstreamSourceNode ) ) ;
2022-06-27 02:14:36 +00:00
if ( pInfo - > pSources = = NULL ) {
2022-06-05 06:48:15 +00:00
return TSDB_CODE_OUT_OF_MEMORY ;
2022-03-10 10:07:07 +00:00
}
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < numOfSources ; + + i ) {
2022-06-30 03:04:49 +00:00
SDownstreamSourceNode * pNode = ( SDownstreamSourceNode * ) nodesListGetNode ( ( SNodeList * ) pExNode - > pSrcEndPoints , i ) ;
2022-03-10 10:07:07 +00:00
taosArrayPush ( pInfo - > pSources , pNode ) ;
}
2022-02-11 06:33:57 +00:00
2022-06-15 08:26:43 +00:00
pInfo - > self = taosAddRef ( exchangeObjRefPool , pInfo ) ;
2022-06-14 13:01:08 +00:00
return initDataSource ( numOfSources , pInfo , id ) ;
2022-06-05 06:48:15 +00:00
}
SOperatorInfo * createExchangeOperatorInfo ( void * pTransporter , SExchangePhysiNode * pExNode , SExecTaskInfo * pTaskInfo ) {
SExchangeInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( SExchangeInfo ) ) ;
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
if ( pInfo = = NULL | | pOperator = = NULL ) {
2022-03-04 07:53:30 +00:00
goto _error ;
2022-02-11 06:33:57 +00:00
}
2022-01-19 06:47:53 +00:00
2022-06-11 06:51:54 +00:00
int32_t code = initExchangeOperator ( pExNode , pInfo , GET_TASKID ( pTaskInfo ) ) ;
2022-06-05 06:48:15 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
}
2022-02-11 06:33:57 +00:00
tsem_init ( & pInfo - > ready , 0 , 0 ) ;
2022-01-16 07:56:48 +00:00
2022-06-24 06:07:52 +00:00
pInfo - > seqLoadData = false ;
2022-06-05 06:48:15 +00:00
pInfo - > pTransporter = pTransporter ;
2022-06-24 06:07:52 +00:00
pInfo - > pResult = createResDataBlock ( pExNode - > node . pOutputDataBlockDesc ) ;
pOperator - > name = " ExchangeOperator " ;
2022-02-28 09:02:43 +00:00
pOperator - > operatorType = QUERY_NODE_PHYSICAL_PLAN_EXCHANGE ;
2022-06-06 10:07:34 +00:00
pOperator - > blocking = false ;
2022-06-24 06:07:52 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > info = pInfo ;
2022-06-22 09:21:04 +00:00
pOperator - > exprSupp . numOfExprs = taosArrayGetSize ( pInfo - > pResult - > pDataBlock ) ;
2022-06-06 10:07:34 +00:00
pOperator - > pTaskInfo = pTaskInfo ;
2022-04-26 12:26:32 +00:00
2022-04-28 08:31:35 +00:00
pOperator - > fpSet = createOperatorFpSet ( prepareLoadRemoteData , doLoadRemoteData , NULL , NULL ,
destroyExchangeOperatorInfo , NULL , NULL , NULL ) ;
2022-01-16 07:56:48 +00:00
return pOperator ;
2022-03-04 07:53:30 +00:00
2022-03-29 07:24:25 +00:00
_error :
2022-03-04 07:53:30 +00:00
if ( pInfo ! = NULL ) {
2022-06-15 08:26:43 +00:00
doDestroyExchangeOperatorInfo ( pInfo ) ;
2022-03-04 07:53:30 +00:00
}
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pInfo ) ;
taosMemoryFreeClear ( pOperator ) ;
2022-06-11 06:51:54 +00:00
pTaskInfo - > code = code ;
2022-03-04 07:53:30 +00:00
return NULL ;
2022-01-16 07:56:48 +00:00
}
2022-04-23 10:29:45 +00:00
static int32_t doInitAggInfoSup ( SAggSupporter * pAggSup , SqlFunctionCtx * pCtx , int32_t numOfOutput , size_t keyBufSize ,
const char * pKey ) ;
2021-11-02 05:37:31 +00:00
2022-02-19 08:15:09 +00:00
static void destroySortedMergeOperatorInfo ( void * param , int32_t numOfOutput ) {
2022-03-29 07:24:25 +00:00
SSortedMergeOperatorInfo * pInfo = ( SSortedMergeOperatorInfo * ) param ;
2022-03-28 11:08:07 +00:00
taosArrayDestroy ( pInfo - > pSortInfo ) ;
2022-02-22 05:12:03 +00:00
taosArrayDestroy ( pInfo - > groupInfo ) ;
if ( pInfo - > pSortHandle ! = NULL ) {
2022-02-22 05:16:46 +00:00
tsortDestroySortHandle ( pInfo - > pSortHandle ) ;
2022-02-22 05:12:03 +00:00
}
2022-03-28 11:08:07 +00:00
blockDataDestroy ( pInfo - > binfo . pRes ) ;
2022-03-14 06:15:26 +00:00
cleanupAggSup ( & pInfo - > aggSup ) ;
2021-11-02 05:37:31 +00:00
}
2022-02-15 06:40:29 +00:00
2022-03-29 07:24:25 +00:00
static bool needToMerge ( SSDataBlock * pBlock , SArray * groupInfo , char * * buf , int32_t rowIndex ) {
2022-02-22 05:12:03 +00:00
size_t size = taosArrayGetSize ( groupInfo ) ;
if ( size = = 0 ) {
return true ;
}
2021-11-02 05:37:31 +00:00
2022-02-22 05:12:03 +00:00
for ( int32_t i = 0 ; i < size ; + + i ) {
int32_t * index = taosArrayGet ( groupInfo , i ) ;
2021-11-02 05:37:31 +00:00
2022-02-22 05:12:03 +00:00
SColumnInfoData * pColInfo = taosArrayGet ( pBlock - > pDataBlock , * index ) ;
2022-03-29 07:24:25 +00:00
bool isNull = colDataIsNull ( pColInfo , rowIndex , pBlock - > info . rows , NULL ) ;
2021-11-02 05:37:31 +00:00
2022-02-22 05:12:03 +00:00
if ( ( isNull & & buf [ i ] ! = NULL ) | | ( ! isNull & & buf [ i ] = = NULL ) ) {
return false ;
}
2021-11-02 05:37:31 +00:00
2022-02-22 05:12:03 +00:00
char * pCell = colDataGetData ( pColInfo , rowIndex ) ;
if ( IS_VAR_DATA_TYPE ( pColInfo - > info . type ) ) {
if ( varDataLen ( pCell ) ! = varDataLen ( buf [ i ] ) ) {
return false ;
} else {
if ( memcmp ( varDataVal ( pCell ) , varDataVal ( buf [ i ] ) , varDataLen ( pCell ) ) ! = 0 ) {
return false ;
}
}
} else {
if ( memcmp ( pCell , buf [ i ] , pColInfo - > info . bytes ) ! = 0 ) {
return false ;
}
2021-11-02 05:37:31 +00:00
}
}
2022-02-22 05:12:03 +00:00
return 0 ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static void doMergeResultImpl ( SSortedMergeOperatorInfo * pInfo , SqlFunctionCtx * pCtx , int32_t numOfExpr ,
int32_t rowIndex ) {
for ( int32_t j = 0 ; j < numOfExpr ; + + j ) { // TODO set row index
2022-05-23 11:50:08 +00:00
// pCtx[j].startRow = rowIndex;
2022-02-08 02:21:00 +00:00
}
2022-02-22 05:12:03 +00:00
for ( int32_t j = 0 ; j < numOfExpr ; + + j ) {
int32_t functionId = pCtx [ j ] . functionId ;
2022-03-29 07:24:25 +00:00
// pCtx[j].fpSet->addInput(&pCtx[j]);
// if (functionId < 0) {
// SUdfInfo* pUdfInfo = taosArrayGet(pInfo->udfInfo, -1 * functionId - 1);
// doInvokeUdf(pUdfInfo, &pCtx[j], 0, TSDB_UDF_FUNC_MERGE);
// } else {
// assert(!TSDB_FUNC_IS_SCALAR(functionId));
// aAggs[functionId].mergeFunc(&pCtx[j]);
// }
2022-02-08 02:21:00 +00:00
}
2022-02-22 05:12:03 +00:00
}
2022-02-08 02:21:00 +00:00
2022-03-29 07:24:25 +00:00
static void doFinalizeResultImpl ( SqlFunctionCtx * pCtx , int32_t numOfExpr ) {
for ( int32_t j = 0 ; j < numOfExpr ; + + j ) {
2022-02-22 05:12:03 +00:00
int32_t functionId = pCtx [ j ] . functionId ;
// if (functionId == FUNC_TAG_DUMMY || functionId == FUNC_TS_DUMMY) {
// continue;
// }
2022-02-08 02:21:00 +00:00
2022-02-22 05:12:03 +00:00
// if (functionId < 0) {
// SUdfInfo* pUdfInfo = taosArrayGet(pInfo->udfInfo, -1 * functionId - 1);
// doInvokeUdf(pUdfInfo, &pCtx[j], 0, TSDB_UDF_FUNC_FINALIZE);
// } else {
2022-04-23 10:29:45 +00:00
// pCtx[j].fpSet.finalize(&pCtx[j]);
2022-02-22 05:12:03 +00:00
}
}
2022-02-08 02:21:00 +00:00
2022-02-22 05:12:03 +00:00
static bool saveCurrentTuple ( char * * rowColData , SArray * pColumnList , SSDataBlock * pBlock , int32_t rowIndex ) {
2022-03-29 07:24:25 +00:00
int32_t size = ( int32_t ) taosArrayGetSize ( pColumnList ) ;
2022-02-08 02:21:00 +00:00
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < size ; + + i ) {
int32_t * index = taosArrayGet ( pColumnList , i ) ;
2022-02-22 05:12:03 +00:00
SColumnInfoData * pColInfo = taosArrayGet ( pBlock - > pDataBlock , * index ) ;
2022-02-09 05:44:16 +00:00
2022-02-22 05:12:03 +00:00
char * data = colDataGetData ( pColInfo , rowIndex ) ;
memcpy ( rowColData [ i ] , data , colDataGetLength ( pColInfo , rowIndex ) ) ;
}
2022-02-08 02:21:00 +00:00
2022-02-22 05:12:03 +00:00
return true ;
}
2022-02-08 02:21:00 +00:00
2022-02-22 05:12:03 +00:00
static void doMergeImpl ( SOperatorInfo * pOperator , int32_t numOfExpr , SSDataBlock * pBlock ) {
SSortedMergeOperatorInfo * pInfo = pOperator - > info ;
2022-02-08 02:21:00 +00:00
2022-06-18 04:00:41 +00:00
SqlFunctionCtx * pCtx = pOperator - > exprSupp . pCtx ;
2022-02-08 02:21:00 +00:00
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < pBlock - > info . rows ; + + i ) {
2022-02-22 05:12:03 +00:00
if ( ! pInfo - > hasGroupVal ) {
ASSERT ( i = = 0 ) ;
doMergeResultImpl ( pInfo , pCtx , numOfExpr , i ) ;
pInfo - > hasGroupVal = saveCurrentTuple ( pInfo - > groupVal , pInfo - > groupInfo , pBlock , i ) ;
} else {
if ( needToMerge ( pBlock , pInfo - > groupInfo , pInfo - > groupVal , i ) ) {
doMergeResultImpl ( pInfo , pCtx , numOfExpr , i ) ;
} else {
doFinalizeResultImpl ( pCtx , numOfExpr ) ;
2022-06-18 04:00:41 +00:00
int32_t numOfRows = getNumOfResult ( pOperator - > exprSupp . pCtx , pOperator - > exprSupp . numOfExprs , NULL ) ;
// setTagValueForMultipleRows(pCtx, pOperator->exprSupp.numOfExprs, numOfRows);
2022-02-08 02:21:00 +00:00
2022-02-22 05:12:03 +00:00
// TODO check for available buffer;
2022-02-09 05:44:16 +00:00
2022-02-22 05:12:03 +00:00
// next group info data
pInfo - > binfo . pRes - > info . rows + = numOfRows ;
for ( int32_t j = 0 ; j < numOfExpr ; + + j ) {
if ( pCtx [ j ] . functionId < 0 ) {
continue ;
2022-02-08 02:21:00 +00:00
}
2022-02-22 05:12:03 +00:00
2022-03-09 02:22:53 +00:00
pCtx [ j ] . fpSet . process ( & pCtx [ j ] ) ;
2022-02-08 02:21:00 +00:00
}
2022-02-22 05:12:03 +00:00
doMergeResultImpl ( pInfo , pCtx , numOfExpr , i ) ;
pInfo - > hasGroupVal = saveCurrentTuple ( pInfo - > groupVal , pInfo - > groupInfo , pBlock , i ) ;
2022-02-09 05:44:16 +00:00
}
2022-02-08 02:21:00 +00:00
}
}
}
2022-02-22 05:12:03 +00:00
static SSDataBlock * doMerge ( SOperatorInfo * pOperator ) {
SSortedMergeOperatorInfo * pInfo = pOperator - > info ;
2022-03-29 07:24:25 +00:00
SSortHandle * pHandle = pInfo - > pSortHandle ;
2022-02-08 07:40:13 +00:00
2022-04-14 07:33:37 +00:00
SSDataBlock * pDataBlock = createOneDataBlock ( pInfo - > binfo . pRes , false ) ;
2022-04-18 10:47:59 +00:00
blockDataEnsureCapacity ( pDataBlock , pOperator - > resultInfo . capacity ) ;
2022-02-22 05:12:03 +00:00
2022-03-29 07:24:25 +00:00
while ( 1 ) {
2022-03-17 05:11:06 +00:00
blockDataCleanup ( pDataBlock ) ;
2022-02-22 05:12:03 +00:00
while ( 1 ) {
2022-02-22 05:16:46 +00:00
STupleHandle * pTupleHandle = tsortNextTuple ( pHandle ) ;
2022-02-22 05:12:03 +00:00
if ( pTupleHandle = = NULL ) {
break ;
2022-02-08 07:40:13 +00:00
}
2022-02-10 05:38:40 +00:00
2022-02-22 05:12:03 +00:00
// build datablock for merge for one group
appendOneRowToDataBlock ( pDataBlock , pTupleHandle ) ;
2022-04-18 10:47:59 +00:00
if ( pDataBlock - > info . rows > = pOperator - > resultInfo . capacity ) {
2022-02-22 05:12:03 +00:00
break ;
}
2022-02-08 07:40:13 +00:00
}
2022-02-08 02:21:00 +00:00
2022-02-22 05:12:03 +00:00
if ( pDataBlock - > info . rows = = 0 ) {
break ;
}
2022-02-08 02:21:00 +00:00
2022-06-18 04:00:41 +00:00
setInputDataBlock ( pOperator , pOperator - > exprSupp . pCtx , pDataBlock , TSDB_ORDER_ASC , MAIN_SCAN , true ) ;
2022-03-29 07:24:25 +00:00
// updateOutputBuf(&pInfo->binfo, &pAggInfo->bufCapacity, pBlock->info.rows * pAggInfo->resultRowFactor,
// pOperator->pRuntimeEnv, true);
2022-06-18 04:00:41 +00:00
doMergeImpl ( pOperator , pOperator - > exprSupp . numOfExprs , pDataBlock ) ;
2022-02-22 05:12:03 +00:00
// flush to tuple store, and after all data have been handled, return to upstream node or sink node
}
2022-02-08 02:21:00 +00:00
2022-06-18 04:00:41 +00:00
doFinalizeResultImpl ( pOperator - > exprSupp . pCtx , pOperator - > exprSupp . numOfExprs ) ;
int32_t numOfRows = getNumOfResult ( pOperator - > exprSupp . pCtx , pOperator - > exprSupp . numOfExprs , NULL ) ;
// setTagValueForMultipleRows(pCtx, pOperator->exprSupp.numOfExprs, numOfRows);
2022-02-08 02:21:00 +00:00
2022-02-22 05:12:03 +00:00
// TODO check for available buffer;
2022-02-08 02:21:00 +00:00
2022-02-22 05:12:03 +00:00
// next group info data
pInfo - > binfo . pRes - > info . rows + = numOfRows ;
2022-03-29 07:24:25 +00:00
return ( pInfo - > binfo . pRes - > info . rows > 0 ) ? pInfo - > binfo . pRes : NULL ;
2022-02-22 05:12:03 +00:00
}
2022-02-08 02:21:00 +00:00
2022-06-10 07:20:57 +00:00
SSDataBlock * getSortedMergeBlockData ( SSortHandle * pHandle , SSDataBlock * pDataBlock , int32_t capacity ,
SArray * pColMatchInfo , SSortedMergeOperatorInfo * pInfo ) {
2022-06-09 02:09:49 +00:00
blockDataCleanup ( pDataBlock ) ;
SSDataBlock * p = tsortGetSortedDataBlock ( pHandle ) ;
if ( p = = NULL ) {
return NULL ;
}
blockDataEnsureCapacity ( p , capacity ) ;
while ( 1 ) {
2022-06-16 09:39:33 +00:00
STupleHandle * pTupleHandle = tsortNextTuple ( pHandle ) ;
2022-06-09 02:09:49 +00:00
if ( pTupleHandle = = NULL ) {
break ;
}
2022-06-16 09:39:33 +00:00
appendOneRowToDataBlock ( p , pTupleHandle ) ;
2022-06-09 02:09:49 +00:00
if ( p - > info . rows > = capacity ) {
break ;
}
}
if ( p - > info . rows > 0 ) {
int32_t numOfCols = taosArrayGetSize ( pColMatchInfo ) ;
for ( int32_t i = 0 ; i < numOfCols ; + + i ) {
SColMatchInfo * pmInfo = taosArrayGet ( pColMatchInfo , i ) ;
ASSERT ( pmInfo - > matchType = = COL_MATCH_FROM_SLOT_ID ) ;
SColumnInfoData * pSrc = taosArrayGet ( p - > pDataBlock , pmInfo - > srcSlotId ) ;
SColumnInfoData * pDst = taosArrayGet ( pDataBlock - > pDataBlock , pmInfo - > targetSlotId ) ;
2022-06-22 09:21:04 +00:00
colDataAssign ( pDst , pSrc , p - > info . rows , & pDataBlock - > info ) ;
2022-06-09 02:09:49 +00:00
}
pDataBlock - > info . rows = p - > info . rows ;
pDataBlock - > info . capacity = p - > info . rows ;
}
blockDataDestroy ( p ) ;
return ( pDataBlock - > info . rows > 0 ) ? pDataBlock : NULL ;
}
2022-05-03 07:27:13 +00:00
static SSDataBlock * doSortedMerge ( SOperatorInfo * pOperator ) {
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
2022-02-08 02:21:00 +00:00
}
2022-03-29 07:24:25 +00:00
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2022-02-19 08:15:09 +00:00
SSortedMergeOperatorInfo * pInfo = pOperator - > info ;
2022-02-08 10:01:21 +00:00
if ( pOperator - > status = = OP_RES_TO_RETURN ) {
2022-06-09 02:09:49 +00:00
return getSortedMergeBlockData ( pInfo - > pSortHandle , pInfo - > binfo . pRes , pOperator - > resultInfo . capacity , NULL , pInfo ) ;
2022-02-08 07:40:13 +00:00
}
2022-02-19 08:15:09 +00:00
int32_t numOfBufPage = pInfo - > sortBufSize / pInfo - > bufPageSize ;
2022-06-20 06:40:13 +00:00
pInfo - > pSortHandle = tsortCreateSortHandle ( pInfo - > pSortInfo , SORT_MULTISOURCE_MERGE , pInfo - > bufPageSize , numOfBufPage ,
pInfo - > binfo . pRes , " GET_TASKID(pTaskInfo) " ) ;
2022-02-13 06:34:00 +00:00
2022-05-05 10:54:19 +00:00
tsortSetFetchRawDataFp ( pInfo - > pSortHandle , loadNextDataBlock , NULL , NULL ) ;
2022-02-08 02:21:00 +00:00
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < pOperator - > numOfDownstream ; + + i ) {
2022-04-06 10:30:25 +00:00
SSortSource * ps = taosMemoryCalloc ( 1 , sizeof ( SSortSource ) ) ;
2022-02-19 15:11:23 +00:00
ps - > param = pOperator - > pDownstream [ i ] ;
2022-02-22 05:16:46 +00:00
tsortAddSource ( pInfo - > pSortHandle , ps ) ;
2022-02-08 02:21:00 +00:00
}
2022-02-22 05:16:46 +00:00
int32_t code = tsortOpen ( pInfo - > pSortHandle ) ;
2022-02-08 07:40:13 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2022-02-19 07:20:20 +00:00
longjmp ( pTaskInfo - > env , terrno ) ;
2022-02-08 02:21:00 +00:00
}
2022-02-08 10:01:21 +00:00
pOperator - > status = OP_RES_TO_RETURN ;
2022-02-22 05:12:03 +00:00
return doMerge ( pOperator ) ;
2022-02-08 07:40:13 +00:00
}
2022-02-08 02:21:00 +00:00
2022-03-29 07:24:25 +00:00
static int32_t initGroupCol ( SExprInfo * pExprInfo , int32_t numOfCols , SArray * pGroupInfo ,
SSortedMergeOperatorInfo * pInfo ) {
2022-02-22 05:12:03 +00:00
if ( pGroupInfo = = NULL | | taosArrayGetSize ( pGroupInfo ) = = 0 ) {
return 0 ;
2022-02-15 06:40:29 +00:00
}
2022-02-22 05:12:03 +00:00
int32_t len = 0 ;
SArray * plist = taosArrayInit ( 3 , sizeof ( SColumn ) ) ;
pInfo - > groupInfo = taosArrayInit ( 3 , sizeof ( int32_t ) ) ;
if ( plist = = NULL | | pInfo - > groupInfo = = NULL ) {
return TSDB_CODE_OUT_OF_MEMORY ;
}
2022-03-29 07:24:25 +00:00
size_t numOfGroupCol = taosArrayGetSize ( pInfo - > groupInfo ) ;
for ( int32_t i = 0 ; i < numOfGroupCol ; + + i ) {
2022-02-22 05:12:03 +00:00
SColumn * pCol = taosArrayGet ( pGroupInfo , i ) ;
2022-03-29 07:24:25 +00:00
for ( int32_t j = 0 ; j < numOfCols ; + + j ) {
2022-03-12 14:59:12 +00:00
SExprInfo * pe = & pExprInfo [ j ] ;
2022-04-12 09:55:17 +00:00
if ( pe - > base . resSchema . slotId = = pCol - > colId ) {
2022-02-22 05:12:03 +00:00
taosArrayPush ( plist , pCol ) ;
taosArrayPush ( pInfo - > groupInfo , & j ) ;
2022-03-09 02:22:53 +00:00
len + = pCol - > bytes ;
2022-02-22 05:12:03 +00:00
break ;
}
2022-02-08 10:01:21 +00:00
}
}
2022-02-22 05:12:03 +00:00
ASSERT ( taosArrayGetSize ( pGroupInfo ) = = taosArrayGetSize ( plist ) ) ;
2022-02-15 06:40:29 +00:00
2022-03-25 16:29:53 +00:00
pInfo - > groupVal = taosMemoryCalloc ( 1 , ( POINTER_BYTES * numOfGroupCol + len ) ) ;
2022-02-22 05:12:03 +00:00
if ( pInfo - > groupVal = = NULL ) {
taosArrayDestroy ( plist ) ;
return TSDB_CODE_OUT_OF_MEMORY ;
}
2022-02-15 06:40:29 +00:00
2022-02-22 05:12:03 +00:00
int32_t offset = 0 ;
2022-03-29 07:24:25 +00:00
char * start = ( char * ) ( pInfo - > groupVal + ( POINTER_BYTES * numOfGroupCol ) ) ;
for ( int32_t i = 0 ; i < numOfGroupCol ; + + i ) {
2022-02-22 05:12:03 +00:00
pInfo - > groupVal [ i ] = start + offset ;
SColumn * pCol = taosArrayGet ( plist , i ) ;
2022-03-09 02:22:53 +00:00
offset + = pCol - > bytes ;
2022-02-22 05:12:03 +00:00
}
2022-02-15 06:40:29 +00:00
2022-02-22 05:12:03 +00:00
taosArrayDestroy ( plist ) ;
2022-02-15 06:40:29 +00:00
2022-02-22 05:12:03 +00:00
return TSDB_CODE_SUCCESS ;
}
2022-02-15 06:40:29 +00:00
2022-03-29 07:24:25 +00:00
SOperatorInfo * createSortedMergeOperatorInfo ( SOperatorInfo * * downstream , int32_t numOfDownstream , SExprInfo * pExprInfo ,
int32_t num , SArray * pSortInfo , SArray * pGroupInfo ,
SExecTaskInfo * pTaskInfo ) {
2022-03-25 16:29:53 +00:00
SSortedMergeOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( SSortedMergeOperatorInfo ) ) ;
2022-03-29 07:24:25 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
2022-02-19 08:15:09 +00:00
if ( pInfo = = NULL | | pOperator = = NULL ) {
2022-02-22 05:12:03 +00:00
goto _error ;
2022-02-19 08:15:09 +00:00
}
2022-02-15 06:40:29 +00:00
2022-06-20 15:22:28 +00:00
int32_t code = initExprSupp ( & pOperator - > exprSupp , pExprInfo , num ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
}
2022-06-17 11:01:45 +00:00
initResultRowInfo ( & pInfo - > binfo . resultRowInfo ) ;
2022-02-15 06:40:29 +00:00
2022-06-18 04:00:41 +00:00
if ( pOperator - > exprSupp . pCtx = = NULL | | pInfo - > binfo . pRes = = NULL ) {
2022-02-22 05:12:03 +00:00
goto _error ;
}
2022-02-15 06:40:29 +00:00
2022-06-22 05:08:20 +00:00
size_t keyBufSize = sizeof ( int64_t ) + sizeof ( int64_t ) + POINTER_BYTES ;
2022-06-20 15:22:28 +00:00
code = doInitAggInfoSup ( & pInfo - > aggSup , pOperator - > exprSupp . pCtx , num , keyBufSize , pTaskInfo - > id . str ) ;
2022-02-22 05:12:03 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
}
2022-02-15 06:40:29 +00:00
2022-06-18 04:00:41 +00:00
setFunctionResultOutput ( pOperator , & pInfo - > binfo , & pInfo - > aggSup , MAIN_SCAN , num ) ;
2022-03-12 14:59:12 +00:00
code = initGroupCol ( pExprInfo , num , pGroupInfo , pInfo ) ;
2022-02-22 05:12:03 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
}
2022-02-15 06:40:29 +00:00
2022-03-29 07:24:25 +00:00
// pInfo->resultRowFactor = (int32_t)(getRowNumForMultioutput(pRuntimeEnv->pQueryAttr,
// pRuntimeEnv->pQueryAttr->topBotQuery, false));
pInfo - > sortBufSize = 1024 * 16 ; // 1MB
pInfo - > bufPageSize = 1024 ;
pInfo - > pSortInfo = pSortInfo ;
2022-02-15 06:40:29 +00:00
2022-04-18 10:47:59 +00:00
pOperator - > resultInfo . capacity = blockDataGetCapacityInRow ( pInfo - > binfo . pRes , pInfo - > bufPageSize ) ;
2022-02-15 06:40:29 +00:00
2022-03-29 07:24:25 +00:00
pOperator - > name = " SortedMerge " ;
2022-03-01 02:40:50 +00:00
// pOperator->operatorType = OP_SortedMerge;
2022-06-22 05:08:20 +00:00
pOperator - > blocking = true ;
pOperator - > status = OP_NOT_OPENED ;
pOperator - > info = pInfo ;
2022-03-29 07:24:25 +00:00
pOperator - > pTaskInfo = pTaskInfo ;
2022-02-15 06:40:29 +00:00
2022-04-26 12:26:32 +00:00
pOperator - > fpSet = createOperatorFpSet ( operatorDummyOpenFn , doSortedMerge , NULL , NULL , destroySortedMergeOperatorInfo ,
NULL , NULL , NULL ) ;
2022-02-22 05:12:03 +00:00
code = appendDownstream ( pOperator , downstream , numOfDownstream ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
2022-02-19 08:15:09 +00:00
}
2022-02-15 06:40:29 +00:00
2022-02-19 08:15:09 +00:00
return pOperator ;
2022-02-15 06:40:29 +00:00
2022-03-29 07:24:25 +00:00
_error :
2022-02-22 05:12:03 +00:00
if ( pInfo ! = NULL ) {
2022-03-12 14:59:12 +00:00
destroySortedMergeOperatorInfo ( pInfo , num ) ;
2022-02-15 06:40:29 +00:00
}
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pInfo ) ;
taosMemoryFreeClear ( pOperator ) ;
2022-02-22 05:12:03 +00:00
terrno = TSDB_CODE_QRY_OUT_OF_MEMORY ;
return NULL ;
2022-02-15 06:40:29 +00:00
}
2022-05-23 11:50:08 +00:00
int32_t getTableScanInfo ( SOperatorInfo * pOperator , int32_t * order , int32_t * scanFlag ) {
2022-05-12 15:10:23 +00:00
// todo add more information about exchange operation
2022-05-19 15:47:27 +00:00
int32_t type = pOperator - > operatorType ;
2022-05-23 11:50:08 +00:00
if ( type = = QUERY_NODE_PHYSICAL_PLAN_EXCHANGE | | type = = QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN | |
2022-06-20 04:54:46 +00:00
type = = QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN | | type = = QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN | |
2022-06-28 15:24:20 +00:00
type = = QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN | | type = = QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN ) {
2022-05-12 15:10:23 +00:00
* order = TSDB_ORDER_ASC ;
* scanFlag = MAIN_SCAN ;
return TSDB_CODE_SUCCESS ;
2022-05-19 15:47:27 +00:00
} else if ( type = = QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN ) {
2022-05-12 15:10:23 +00:00
STableScanInfo * pTableScanInfo = pOperator - > info ;
* order = pTableScanInfo - > cond . order ;
* scanFlag = pTableScanInfo - > scanFlag ;
return TSDB_CODE_SUCCESS ;
} else {
2022-04-26 06:10:25 +00:00
if ( pOperator - > pDownstream = = NULL | | pOperator - > pDownstream [ 0 ] = = NULL ) {
2022-05-12 09:33:36 +00:00
return TSDB_CODE_INVALID_PARA ;
2022-04-26 06:10:25 +00:00
} else {
2022-05-12 09:33:36 +00:00
return getTableScanInfo ( pOperator - > pDownstream [ 0 ] , order , scanFlag ) ;
2022-04-26 05:53:11 +00:00
}
}
}
2021-11-02 05:37:31 +00:00
2022-06-30 06:41:50 +00:00
int32_t doPrepareScan ( SOperatorInfo * pOperator , uint64_t uid , int64_t ts ) {
int32_t type = pOperator - > operatorType ;
2022-07-04 12:47:56 +00:00
pOperator - > status = OP_OPENED ;
2022-06-30 06:41:50 +00:00
if ( type = = QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN ) {
2022-07-06 06:20:07 +00:00
SStreamScanInfo * pScanInfo = pOperator - > info ;
2022-06-30 09:25:19 +00:00
pScanInfo - > blockType = STREAM_INPUT__DATA_SCAN ;
2022-07-06 06:20:07 +00:00
pScanInfo - > pTableScanOp - > status = OP_OPENED ;
2022-07-04 12:47:56 +00:00
2022-07-06 06:20:07 +00:00
STableScanInfo * pInfo = pScanInfo - > pTableScanOp - > info ;
2022-07-04 12:47:56 +00:00
ASSERT ( pInfo - > scanMode = = TABLE_SCAN__TABLE_ORDER ) ;
2022-07-01 06:39:21 +00:00
if ( uid = = 0 ) {
pInfo - > noTable = 1 ;
return TSDB_CODE_SUCCESS ;
}
2022-06-30 09:25:19 +00:00
/*if (pSnapShotScanInfo->dataReader == NULL) {*/
/*pSnapShotScanInfo->dataReader = tsdbReaderOpen(pHandle->vnode, &pSTInfo->cond, tableList, 0, 0);*/
/*pSnapShotScanInfo->scanMode = TABLE_SCAN__TABLE_ORDER;*/
/*}*/
2022-07-01 06:39:21 +00:00
pInfo - > noTable = 0 ;
2022-06-30 09:25:19 +00:00
if ( pInfo - > lastStatus . uid ! = uid | | pInfo - > lastStatus . ts ! = ts ) {
2022-07-02 12:18:40 +00:00
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
int32_t tableSz = taosArrayGetSize ( pTaskInfo - > tableqinfoList . pTableList ) ;
bool found = false ;
for ( int32_t i = 0 ; i < tableSz ; i + + ) {
STableKeyInfo * pTableInfo = taosArrayGet ( pTaskInfo - > tableqinfoList . pTableList , i ) ;
if ( pTableInfo - > uid = = uid ) {
found = true ;
pInfo - > currentTable = i ;
}
}
2022-07-04 12:47:56 +00:00
// TODO after processing drop, found can be false
2022-07-02 12:18:40 +00:00
ASSERT ( found ) ;
2022-07-04 12:47:56 +00:00
tsdbSetTableId ( pInfo - > dataReader , uid ) ;
int64_t oldSkey = pInfo - > cond . twindows [ 0 ] . skey ;
pInfo - > cond . twindows [ 0 ] . skey = ts + 1 ;
2022-07-05 01:47:39 +00:00
tsdbReaderReset ( pInfo - > dataReader , & pInfo - > cond , 0 ) ;
2022-07-04 12:47:56 +00:00
pInfo - > cond . twindows [ 0 ] . skey = oldSkey ;
pInfo - > scanTimes = 0 ;
pInfo - > curTWinIdx = 0 ;
2022-07-02 12:18:40 +00:00
qDebug ( " tsdb reader offset seek to uid %ld ts %ld, table cur set to %d , all table num %d " , uid , ts ,
pInfo - > currentTable , tableSz ) ;
2022-06-30 06:41:50 +00:00
}
2022-07-02 12:18:40 +00:00
2022-07-01 06:39:21 +00:00
return TSDB_CODE_SUCCESS ;
2022-06-30 09:25:19 +00:00
2022-06-30 06:41:50 +00:00
} else {
2022-06-30 09:25:19 +00:00
if ( pOperator - > numOfDownstream = = 1 ) {
return doPrepareScan ( pOperator - > pDownstream [ 0 ] , uid , ts ) ;
} else if ( pOperator - > numOfDownstream = = 0 ) {
qError ( " failed to find stream scan operator to set the input data block " ) ;
return TSDB_CODE_QRY_APP_ERROR ;
2022-06-30 06:41:50 +00:00
} else {
2022-06-30 09:25:19 +00:00
qError ( " join not supported for stream block scan " ) ;
return TSDB_CODE_QRY_APP_ERROR ;
2022-06-30 06:41:50 +00:00
}
}
}
2022-06-29 08:19:19 +00:00
int32_t doGetScanStatus ( SOperatorInfo * pOperator , uint64_t * uid , int64_t * ts ) {
int32_t type = pOperator - > operatorType ;
if ( type = = QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN ) {
2022-07-06 06:20:07 +00:00
SStreamScanInfo * pScanInfo = pOperator - > info ;
STableScanInfo * pSnapShotScanInfo = pScanInfo - > pTableScanOp - > info ;
2022-06-30 06:41:50 +00:00
* uid = pSnapShotScanInfo - > lastStatus . uid ;
* ts = pSnapShotScanInfo - > lastStatus . ts ;
2022-06-29 08:19:19 +00:00
} else {
if ( pOperator - > pDownstream [ 0 ] = = NULL ) {
return TSDB_CODE_INVALID_PARA ;
} else {
doGetScanStatus ( pOperator - > pDownstream [ 0 ] , uid , ts ) ;
}
}
return TSDB_CODE_SUCCESS ;
}
2021-11-02 05:37:31 +00:00
// this is a blocking operator
2022-03-29 07:24:25 +00:00
static int32_t doOpenAggregateOptr ( SOperatorInfo * pOperator ) {
2022-03-12 10:02:56 +00:00
if ( OPTR_IS_OPENED ( pOperator ) ) {
return TSDB_CODE_SUCCESS ;
2021-11-02 05:37:31 +00:00
}
2022-04-16 11:10:21 +00:00
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2021-11-02 05:37:31 +00:00
SAggOperatorInfo * pAggInfo = pOperator - > info ;
2022-04-16 11:10:21 +00:00
2022-06-22 05:08:20 +00:00
SExprSupp * pSup = & pOperator - > exprSupp ;
SOperatorInfo * downstream = pOperator - > pDownstream [ 0 ] ;
2021-11-02 05:37:31 +00:00
2022-05-24 03:29:51 +00:00
int64_t st = taosGetTimestampUs ( ) ;
2022-05-12 09:33:36 +00:00
int32_t order = TSDB_ORDER_ASC ;
int32_t scanFlag = MAIN_SCAN ;
2022-03-12 10:02:56 +00:00
while ( 1 ) {
2022-05-03 07:27:13 +00:00
SSDataBlock * pBlock = downstream - > fpSet . getNextFn ( downstream ) ;
2021-11-02 05:37:31 +00:00
if ( pBlock = = NULL ) {
break ;
}
2022-05-12 09:33:36 +00:00
int32_t code = getTableScanInfo ( pOperator , & order , & scanFlag ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
longjmp ( pTaskInfo - > env , code ) ;
}
2022-05-10 09:46:45 +00:00
2022-04-16 02:00:25 +00:00
// there is an scalar expression that needs to be calculated before apply the group aggregation.
2022-06-18 06:49:27 +00:00
if ( pAggInfo - > scalarExprSup . pExprInfo ! = NULL ) {
SExprSupp * pSup1 = & pAggInfo - > scalarExprSup ;
code = projectApplyFunctions ( pSup1 - > pExprInfo , pBlock , pBlock , pSup1 - > pCtx , pSup1 - > numOfExprs , NULL ) ;
2022-04-28 10:08:56 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2022-05-13 04:17:09 +00:00
longjmp ( pTaskInfo - > env , code ) ;
2022-04-28 10:08:56 +00:00
}
2022-04-16 02:00:25 +00:00
}
2021-11-02 05:37:31 +00:00
// the pDataBlock are always the same one, no need to call this again
2022-06-18 04:00:41 +00:00
setExecutionContext ( pOperator , pOperator - > exprSupp . numOfExprs , pBlock - > info . groupId , pAggInfo ) ;
setInputDataBlock ( pOperator , pSup - > pCtx , pBlock , order , scanFlag , true ) ;
code = doAggregateImpl ( pOperator , 0 , pSup - > pCtx ) ;
2022-05-13 04:17:09 +00:00
if ( code ! = 0 ) {
longjmp ( pTaskInfo - > env , code ) ;
}
2022-03-28 03:13:59 +00:00
2022-04-23 10:29:45 +00:00
#if 0 // test for encode/decode result info
2022-05-30 04:00:51 +00:00
if ( pOperator - > fpSet . encodeResultRow ) {
2022-04-11 09:08:13 +00:00
char * result = NULL ;
int32_t length = 0 ;
2022-05-30 04:00:51 +00:00
pOperator - > fpSet . encodeResultRow ( pOperator , & result , & length ) ;
SAggSupporter * pSup = & pAggInfo - > aggSup ;
2022-04-11 09:08:13 +00:00
taosHashClear ( pSup - > pResultRowHashTable ) ;
pInfo - > resultRowInfo . size = 0 ;
2022-05-30 04:00:51 +00:00
pOperator - > fpSet . decodeResultRow ( pOperator , result ) ;
2022-04-11 09:08:13 +00:00
if ( result ) {
taosMemoryFree ( result ) ;
}
2022-03-28 03:13:59 +00:00
}
2022-04-11 09:30:27 +00:00
# endif
2021-11-02 05:37:31 +00:00
}
2022-04-16 11:10:21 +00:00
closeAllResultRows ( & pAggInfo - > binfo . resultRowInfo ) ;
2022-05-17 04:16:44 +00:00
initGroupedResultInfo ( & pAggInfo - > groupResInfo , pAggInfo - > aggSup . pResultRowHashTable , 0 ) ;
2022-03-12 10:02:56 +00:00
OPTR_SET_OPENED ( pOperator ) ;
2022-05-24 03:29:51 +00:00
2022-05-26 08:29:52 +00:00
pOperator - > cost . openCost = ( taosGetTimestampUs ( ) - st ) / 1000.0 ;
2022-03-12 10:02:56 +00:00
return TSDB_CODE_SUCCESS ;
}
2022-05-03 07:27:13 +00:00
static SSDataBlock * getAggregateResult ( SOperatorInfo * pOperator ) {
2022-03-29 07:24:25 +00:00
SAggOperatorInfo * pAggInfo = pOperator - > info ;
2022-03-12 10:02:56 +00:00
SOptrBasicInfo * pInfo = & pAggInfo - > binfo ;
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
2022-03-29 07:24:25 +00:00
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2022-04-26 12:26:32 +00:00
pTaskInfo - > code = pOperator - > fpSet . _openFn ( pOperator ) ;
2022-03-12 10:02:56 +00:00
if ( pTaskInfo - > code ! = TSDB_CODE_SUCCESS ) {
2022-05-24 03:29:51 +00:00
doSetOperatorCompleted ( pOperator ) ;
2022-03-12 10:02:56 +00:00
return NULL ;
}
2022-04-16 11:10:21 +00:00
blockDataEnsureCapacity ( pInfo - > pRes , pOperator - > resultInfo . capacity ) ;
2022-07-06 05:54:11 +00:00
while ( 1 ) {
doBuildResultDatablock ( pOperator , pInfo , & pAggInfo - > groupResInfo , pAggInfo - > aggSup . pResultBuf ) ;
doFilter ( pAggInfo - > pCondition , pInfo - > pRes ) ;
if ( ! hasDataInGroupInfo ( & pAggInfo - > groupResInfo ) ) {
doSetOperatorCompleted ( pOperator ) ;
break ;
}
2021-11-02 05:37:31 +00:00
2022-07-06 05:54:11 +00:00
if ( pInfo - > pRes - > info . rows > 0 ) {
break ;
}
}
2022-06-16 09:39:33 +00:00
size_t rows = blockDataGetNumOfRows ( pInfo - > pRes ) ;
2022-05-24 03:29:51 +00:00
pOperator - > resultInfo . totalRows + = rows ;
2022-05-26 08:29:52 +00:00
return ( rows = = 0 ) ? NULL : pInfo - > pRes ;
2021-11-02 05:37:31 +00:00
}
2022-05-28 15:26:22 +00:00
int32_t aggEncodeResultRow ( SOperatorInfo * pOperator , char * * result , int32_t * length ) {
2022-05-30 11:08:14 +00:00
if ( result = = NULL | | length = = NULL ) {
2022-05-28 15:26:22 +00:00
return TSDB_CODE_TSC_INVALID_INPUT ;
}
SOptrBasicInfo * pInfo = ( SOptrBasicInfo * ) ( pOperator - > info ) ;
2022-05-30 11:08:14 +00:00
SAggSupporter * pSup = ( SAggSupporter * ) POINTER_SHIFT ( pOperator - > info , sizeof ( SOptrBasicInfo ) ) ;
int32_t size = taosHashGetSize ( pSup - > pResultRowHashTable ) ;
size_t keyLen = sizeof ( uint64_t ) * 2 ; // estimate the key length
int32_t totalSize =
sizeof ( int32_t ) + sizeof ( int32_t ) + size * ( sizeof ( int32_t ) + keyLen + sizeof ( int32_t ) + pSup - > resultRowSize ) ;
2022-05-28 15:26:22 +00:00
2022-06-26 10:44:49 +00:00
// no result
if ( getTotalBufSize ( pSup - > pResultBuf ) = = 0 ) {
* result = NULL ;
* length = 0 ;
return TSDB_CODE_SUCCESS ;
}
2022-06-28 05:59:49 +00:00
2022-05-28 15:48:31 +00:00
* result = ( char * ) taosMemoryCalloc ( 1 , totalSize ) ;
2022-03-29 07:24:25 +00:00
if ( * result = = NULL ) {
2022-05-28 15:26:22 +00:00
return TSDB_CODE_OUT_OF_MEMORY ;
2022-03-24 09:24:01 +00:00
}
2022-05-28 15:26:22 +00:00
2022-03-24 09:24:01 +00:00
int32_t offset = sizeof ( int32_t ) ;
2022-05-28 15:26:22 +00:00
* ( int32_t * ) ( * result + offset ) = size ;
offset + = sizeof ( int32_t ) ;
2022-04-11 09:08:13 +00:00
// prepare memory
2022-04-16 07:15:28 +00:00
SResultRowPosition * pos = & pInfo - > resultRowInfo . cur ;
2022-04-23 10:29:45 +00:00
void * pPage = getBufPage ( pSup - > pResultBuf , pos - > pageId ) ;
SResultRow * pRow = ( SResultRow * ) ( ( char * ) pPage + pos - > offset ) ;
2022-04-11 09:08:13 +00:00
setBufPageDirty ( pPage , true ) ;
releaseBufPage ( pSup - > pResultBuf , pPage ) ;
2022-04-23 10:29:45 +00:00
void * pIter = taosHashIterate ( pSup - > pResultRowHashTable , NULL ) ;
2022-03-24 09:24:01 +00:00
while ( pIter ) {
2022-04-23 10:29:45 +00:00
void * key = taosHashGetKey ( pIter , & keyLen ) ;
2022-04-11 09:30:27 +00:00
SResultRowPosition * p1 = ( SResultRowPosition * ) pIter ;
2022-04-11 09:08:13 +00:00
2022-04-23 10:29:45 +00:00
pPage = ( SFilePage * ) getBufPage ( pSup - > pResultBuf , p1 - > pageId ) ;
2022-04-11 09:30:27 +00:00
pRow = ( SResultRow * ) ( ( char * ) pPage + p1 - > offset ) ;
2022-04-11 09:08:13 +00:00
setBufPageDirty ( pPage , true ) ;
releaseBufPage ( pSup - > pResultBuf , pPage ) ;
2022-03-24 09:24:01 +00:00
// recalculate the result size
int32_t realTotalSize = offset + sizeof ( int32_t ) + keyLen + sizeof ( int32_t ) + pSup - > resultRowSize ;
2022-03-29 07:24:25 +00:00
if ( realTotalSize > totalSize ) {
2022-05-28 15:48:31 +00:00
char * tmp = ( char * ) taosMemoryRealloc ( * result , realTotalSize ) ;
2022-03-29 07:24:25 +00:00
if ( tmp = = NULL ) {
2022-03-25 16:29:53 +00:00
taosMemoryFree ( * result ) ;
2022-03-24 09:24:01 +00:00
* result = NULL ;
2022-05-28 15:26:22 +00:00
return TSDB_CODE_OUT_OF_MEMORY ;
2022-03-29 07:24:25 +00:00
} else {
2022-03-24 09:24:01 +00:00
* result = tmp ;
}
}
// save key
* ( int32_t * ) ( * result + offset ) = keyLen ;
offset + = sizeof ( int32_t ) ;
memcpy ( * result + offset , key , keyLen ) ;
offset + = keyLen ;
// save value
* ( int32_t * ) ( * result + offset ) = pSup - > resultRowSize ;
offset + = sizeof ( int32_t ) ;
2022-04-11 09:08:13 +00:00
memcpy ( * result + offset , pRow , pSup - > resultRowSize ) ;
2022-03-24 09:24:01 +00:00
offset + = pSup - > resultRowSize ;
pIter = taosHashIterate ( pSup - > pResultRowHashTable , pIter ) ;
}
2022-05-28 15:26:22 +00:00
* ( int32_t * ) ( * result ) = offset ;
* length = offset ;
return TDB_CODE_SUCCESS ;
2022-03-24 09:24:01 +00:00
}
2022-05-30 04:00:51 +00:00
int32_t aggDecodeResultRow ( SOperatorInfo * pOperator , char * result ) {
2022-05-30 11:08:14 +00:00
if ( result = = NULL ) {
2022-05-28 15:26:22 +00:00
return TSDB_CODE_TSC_INVALID_INPUT ;
2022-03-24 09:24:01 +00:00
}
2022-05-28 15:26:22 +00:00
SOptrBasicInfo * pInfo = ( SOptrBasicInfo * ) ( pOperator - > info ) ;
2022-05-30 11:08:14 +00:00
SAggSupporter * pSup = ( SAggSupporter * ) POINTER_SHIFT ( pOperator - > info , sizeof ( SOptrBasicInfo ) ) ;
2022-03-24 09:24:01 +00:00
// int32_t size = taosHashGetSize(pSup->pResultRowHashTable);
2022-05-30 04:00:51 +00:00
int32_t length = * ( int32_t * ) ( result ) ;
2022-03-24 09:24:01 +00:00
int32_t offset = sizeof ( int32_t ) ;
2022-05-30 04:00:51 +00:00
int32_t count = * ( int32_t * ) ( result + offset ) ;
offset + = sizeof ( int32_t ) ;
2022-03-29 07:24:25 +00:00
while ( count - - > 0 & & length > offset ) {
2022-03-24 09:24:01 +00:00
int32_t keyLen = * ( int32_t * ) ( result + offset ) ;
offset + = sizeof ( int32_t ) ;
2022-03-29 07:24:25 +00:00
uint64_t tableGroupId = * ( uint64_t * ) ( result + offset ) ;
2022-05-29 04:35:11 +00:00
SResultRow * resultRow = getNewResultRow ( pSup - > pResultBuf , tableGroupId , pSup - > resultRowSize ) ;
2022-03-29 07:24:25 +00:00
if ( ! resultRow ) {
2022-05-28 15:26:22 +00:00
return TSDB_CODE_TSC_INVALID_INPUT ;
2022-03-24 09:24:01 +00:00
}
2022-04-18 09:32:50 +00:00
2022-03-24 09:24:01 +00:00
// add a new result set for a new group
2022-04-11 09:08:13 +00:00
SResultRowPosition pos = { . pageId = resultRow - > pageId , . offset = resultRow - > offset } ;
taosHashPut ( pSup - > pResultRowHashTable , result + offset , keyLen , & pos , sizeof ( SResultRowPosition ) ) ;
2022-03-24 09:24:01 +00:00
offset + = keyLen ;
int32_t valueLen = * ( int32_t * ) ( result + offset ) ;
2022-03-29 07:24:25 +00:00
if ( valueLen ! = pSup - > resultRowSize ) {
2022-05-28 15:26:22 +00:00
return TSDB_CODE_TSC_INVALID_INPUT ;
2022-03-24 09:24:01 +00:00
}
offset + = sizeof ( int32_t ) ;
int32_t pageId = resultRow - > pageId ;
int32_t pOffset = resultRow - > offset ;
memcpy ( resultRow , result + offset , valueLen ) ;
resultRow - > pageId = pageId ;
resultRow - > offset = pOffset ;
offset + = valueLen ;
initResultRow ( resultRow ) ;
2022-04-23 10:29:45 +00:00
pInfo - > resultRowInfo . cur = ( SResultRowPosition ) { . pageId = resultRow - > pageId , . offset = resultRow - > offset } ;
2022-03-24 09:24:01 +00:00
}
2022-03-29 07:24:25 +00:00
if ( offset ! = length ) {
2022-05-28 15:26:22 +00:00
return TSDB_CODE_TSC_INVALID_INPUT ;
2022-03-24 09:24:01 +00:00
}
2022-05-28 15:26:22 +00:00
return TDB_CODE_SUCCESS ;
2022-03-24 09:24:01 +00:00
}
2022-04-26 05:53:11 +00:00
enum {
PROJECT_RETRIEVE_CONTINUE = 0x1 ,
2022-04-28 08:31:35 +00:00
PROJECT_RETRIEVE_DONE = 0x2 ,
2022-04-26 05:53:11 +00:00
} ;
static int32_t handleLimitOffset ( SOperatorInfo * pOperator , SSDataBlock * pBlock ) {
SProjectOperatorInfo * pProjectInfo = pOperator - > info ;
SOptrBasicInfo * pInfo = & pProjectInfo - > binfo ;
2022-04-28 08:31:35 +00:00
SSDataBlock * pRes = pInfo - > pRes ;
2022-04-26 05:53:11 +00:00
if ( pProjectInfo - > curSOffset > 0 ) {
if ( pProjectInfo - > groupId = = 0 ) { // it is the first group
pProjectInfo - > groupId = pBlock - > info . groupId ;
blockDataCleanup ( pInfo - > pRes ) ;
return PROJECT_RETRIEVE_CONTINUE ;
} else if ( pProjectInfo - > groupId ! = pBlock - > info . groupId ) {
pProjectInfo - > curSOffset - = 1 ;
// ignore data block in current group
if ( pProjectInfo - > curSOffset > 0 ) {
blockDataCleanup ( pInfo - > pRes ) ;
return PROJECT_RETRIEVE_CONTINUE ;
}
}
// set current group id of the project operator
pProjectInfo - > groupId = pBlock - > info . groupId ;
}
if ( pProjectInfo - > groupId ! = 0 & & pProjectInfo - > groupId ! = pBlock - > info . groupId ) {
pProjectInfo - > curGroupOutput + = 1 ;
if ( ( pProjectInfo - > slimit . limit > 0 ) & & ( pProjectInfo - > slimit . limit < = pProjectInfo - > curGroupOutput ) ) {
pOperator - > status = OP_EXEC_DONE ;
blockDataCleanup ( pRes ) ;
return PROJECT_RETRIEVE_DONE ;
}
// reset the value for a new group data
pProjectInfo - > curOffset = 0 ;
pProjectInfo - > curOutput = 0 ;
}
// here we reach the start position, according to the limit/offset requirements.
// set current group id
pProjectInfo - > groupId = pBlock - > info . groupId ;
if ( pProjectInfo - > curOffset > = pRes - > info . rows ) {
pProjectInfo - > curOffset - = pRes - > info . rows ;
blockDataCleanup ( pRes ) ;
return PROJECT_RETRIEVE_CONTINUE ;
} else if ( pProjectInfo - > curOffset < pRes - > info . rows & & pProjectInfo - > curOffset > 0 ) {
blockDataTrimFirstNRows ( pRes , pProjectInfo - > curOffset ) ;
pProjectInfo - > curOffset = 0 ;
}
2022-04-27 09:52:39 +00:00
// check for the limitation in each group
2022-06-27 02:51:47 +00:00
if ( pProjectInfo - > limit . limit > = 0 & & pProjectInfo - > curOutput + pRes - > info . rows > = pProjectInfo - > limit . limit ) {
int32_t keepRows = ( int32_t ) ( pProjectInfo - > limit . limit - pProjectInfo - > curOutput ) ;
blockDataKeepFirstNRows ( pRes , keepRows ) ;
2022-06-16 13:13:18 +00:00
if ( pProjectInfo - > slimit . limit > 0 & & pProjectInfo - > slimit . limit < = pProjectInfo - > curGroupOutput ) {
2022-04-27 10:43:46 +00:00
pOperator - > status = OP_EXEC_DONE ;
}
2022-04-27 10:12:26 +00:00
return PROJECT_RETRIEVE_DONE ;
2022-04-27 09:52:39 +00:00
}
2022-04-26 05:53:11 +00:00
2022-04-27 11:59:50 +00:00
// todo optimize performance
2022-04-27 10:43:46 +00:00
// If there are slimit/soffset value exists, multi-round result can not be packed into one group, since the
// they may not belong to the same group the limit/offset value is not valid in this case.
2022-04-28 08:31:35 +00:00
if ( pRes - > info . rows > = pOperator - > resultInfo . threshold | | pProjectInfo - > slimit . offset ! = - 1 | |
pProjectInfo - > slimit . limit ! = - 1 ) {
2022-04-26 05:53:11 +00:00
return PROJECT_RETRIEVE_DONE ;
2022-04-28 08:31:35 +00:00
} else { // not full enough, continue to accumulate the output data in the buffer.
2022-04-26 05:53:11 +00:00
return PROJECT_RETRIEVE_CONTINUE ;
}
}
2022-05-03 07:27:13 +00:00
static SSDataBlock * doProjectOperation ( SOperatorInfo * pOperator ) {
2021-11-02 05:37:31 +00:00
SProjectOperatorInfo * pProjectInfo = pOperator - > info ;
2022-03-29 07:24:25 +00:00
SOptrBasicInfo * pInfo = & pProjectInfo - > binfo ;
2021-11-02 05:37:31 +00:00
2022-06-20 06:40:13 +00:00
SExprSupp * pSup = & pOperator - > exprSupp ;
2021-11-02 05:37:31 +00:00
SSDataBlock * pRes = pInfo - > pRes ;
2022-03-17 05:11:06 +00:00
blockDataCleanup ( pRes ) ;
2022-04-19 08:11:16 +00:00
2022-05-09 11:16:22 +00:00
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2022-04-19 08:11:16 +00:00
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
2022-04-23 10:29:45 +00:00
2022-04-01 09:21:24 +00:00
#if 0
2021-11-02 05:37:31 +00:00
if ( pProjectInfo - > existDataBlock ) { // TODO refactor
SSDataBlock * pBlock = pProjectInfo - > existDataBlock ;
pProjectInfo - > existDataBlock = NULL ;
// the pDataBlock are always the same one, no need to call this again
2022-03-12 14:59:12 +00:00
setInputDataBlock ( pOperator , pInfo - > pCtx , pBlock , TSDB_ORDER_ASC ) ;
2021-11-02 05:37:31 +00:00
2022-03-18 10:02:00 +00:00
blockDataEnsureCapacity ( pInfo - > pRes , pBlock - > info . rows ) ;
2022-06-18 04:00:41 +00:00
projectApplyFunctions ( pOperator - > exprSupp . pExprInfo , pInfo - > pRes , pBlock , pInfo - > pCtx , pOperator - > exprSupp . numOfExprs ) ;
2022-03-29 07:24:25 +00:00
if ( pRes - > info . rows > = pProjectInfo - > binfo . capacity * 0.8 ) {
2022-06-18 04:00:41 +00:00
copyTsColoum ( pRes , pInfo - > pCtx , pOperator - > exprSupp . numOfExprs ) ;
resetResultRowEntryResult ( pInfo - > pCtx , pOperator - > exprSupp . numOfExprs ) ;
2021-11-02 05:37:31 +00:00
return pRes ;
}
}
2022-04-01 09:21:24 +00:00
# endif
2021-11-02 05:37:31 +00:00
2022-05-24 03:29:51 +00:00
int64_t st = 0 ;
2022-05-12 09:33:36 +00:00
int32_t order = 0 ;
int32_t scanFlag = 0 ;
2022-05-24 03:29:51 +00:00
if ( pOperator - > cost . openCost = = 0 ) {
st = taosGetTimestampUs ( ) ;
}
2022-03-12 14:59:12 +00:00
SOperatorInfo * downstream = pOperator - > pDownstream [ 0 ] ;
2022-03-29 07:24:25 +00:00
while ( 1 ) {
2022-01-08 14:59:24 +00:00
// The downstream exec may change the value of the newgroup, so use a local variable instead.
2022-05-03 07:27:13 +00:00
SSDataBlock * pBlock = downstream - > fpSet . getNextFn ( downstream ) ;
2021-11-02 05:37:31 +00:00
if ( pBlock = = NULL ) {
2022-05-24 03:29:51 +00:00
doSetOperatorCompleted ( pOperator ) ;
2021-11-02 05:37:31 +00:00
break ;
}
2022-07-02 07:28:21 +00:00
if ( pBlock - > info . type = = STREAM_RETRIEVE ) {
// for stream interval
return pBlock ;
}
2021-11-02 05:37:31 +00:00
// the pDataBlock are always the same one, no need to call this again
2022-05-12 09:33:36 +00:00
int32_t code = getTableScanInfo ( pOperator - > pDownstream [ 0 ] , & order , & scanFlag ) ;
2022-05-19 15:47:27 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
longjmp ( pTaskInfo - > env , code ) ;
}
2022-04-26 05:53:11 +00:00
2022-06-18 04:00:41 +00:00
setInputDataBlock ( pOperator , pSup - > pCtx , pBlock , order , scanFlag , false ) ;
2022-03-27 14:43:07 +00:00
blockDataEnsureCapacity ( pInfo - > pRes , pInfo - > pRes - > info . rows + pBlock - > info . rows ) ;
2022-06-18 04:00:41 +00:00
code = projectApplyFunctions ( pSup - > pExprInfo , pInfo - > pRes , pBlock , pSup - > pCtx , pSup - > numOfExprs ,
2022-05-23 11:50:08 +00:00
pProjectInfo - > pPseudoColInfo ) ;
2022-05-12 09:33:36 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
longjmp ( pTaskInfo - > env , code ) ;
2022-05-09 11:16:22 +00:00
}
2022-04-26 05:53:11 +00:00
int32_t status = handleLimitOffset ( pOperator , pBlock ) ;
2022-07-01 07:25:02 +00:00
// filter shall be applied after apply functions and limit/offset on the result
doFilter ( pProjectInfo - > pFilterNode , pInfo - > pRes ) ;
2022-04-26 05:53:11 +00:00
if ( status = = PROJECT_RETRIEVE_CONTINUE ) {
2022-04-01 09:21:24 +00:00
continue ;
2022-04-28 08:31:35 +00:00
} else if ( status = = PROJECT_RETRIEVE_DONE ) {
2021-11-02 05:37:31 +00:00
break ;
}
}
2022-04-23 10:29:45 +00:00
2022-04-01 09:21:24 +00:00
pProjectInfo - > curOutput + = pInfo - > pRes - > info . rows ;
2022-03-12 14:59:12 +00:00
2022-05-24 03:29:51 +00:00
size_t rows = pInfo - > pRes - > info . rows ;
pOperator - > resultInfo . totalRows + = rows ;
if ( pOperator - > cost . openCost = = 0 ) {
2022-05-26 08:29:52 +00:00
pOperator - > cost . openCost = ( taosGetTimestampUs ( ) - st ) / 1000.0 ;
2022-05-24 03:29:51 +00:00
}
2022-05-26 08:29:52 +00:00
return ( rows > 0 ) ? pInfo - > pRes : NULL ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static void doHandleRemainBlockForNewGroupImpl ( SFillOperatorInfo * pInfo , SResultInfo * pResultInfo , bool * newgroup ,
SExecTaskInfo * pTaskInfo ) {
2021-11-02 05:37:31 +00:00
pInfo - > totalInputRows = pInfo - > existNewGroupBlock - > info . rows ;
2022-03-24 06:21:14 +00:00
2022-07-06 10:08:23 +00:00
int64_t ekey = Q_STATUS_EQUAL ( pTaskInfo - > status , TASK_COMPLETED ) ? pInfo - > win . ekey
2022-03-29 07:24:25 +00:00
: pInfo - > existNewGroupBlock - > info . window . ekey ;
2021-11-02 05:37:31 +00:00
taosResetFillInfo ( pInfo - > pFillInfo , getFillInfoStart ( pInfo - > pFillInfo ) ) ;
2022-03-24 09:26:04 +00:00
taosFillSetStartInfo ( pInfo - > pFillInfo , pInfo - > existNewGroupBlock - > info . rows , ekey ) ;
2021-11-02 05:37:31 +00:00
taosFillSetInputDataBlock ( pInfo - > pFillInfo , pInfo - > existNewGroupBlock ) ;
2022-05-02 15:52:32 +00:00
doFillTimeIntervalGapsInResults ( pInfo - > pFillInfo , pInfo - > pRes , pResultInfo - > capacity ) ;
2021-11-02 05:37:31 +00:00
pInfo - > existNewGroupBlock = NULL ;
* newgroup = true ;
}
2022-03-29 07:24:25 +00:00
static void doHandleRemainBlockFromNewGroup ( SFillOperatorInfo * pInfo , SResultInfo * pResultInfo , bool * newgroup ,
SExecTaskInfo * pTaskInfo ) {
2021-11-02 05:37:31 +00:00
if ( taosFillHasMoreResults ( pInfo - > pFillInfo ) ) {
* newgroup = false ;
2022-05-02 15:52:32 +00:00
doFillTimeIntervalGapsInResults ( pInfo - > pFillInfo , pInfo - > pRes , ( int32_t ) pResultInfo - > capacity ) ;
2022-03-24 06:21:14 +00:00
if ( pInfo - > pRes - > info . rows > pResultInfo - > threshold | | ( ! pInfo - > multigroupResult ) ) {
2021-11-02 05:37:31 +00:00
return ;
}
}
// handle the cached new group data block
if ( pInfo - > existNewGroupBlock ) {
2022-03-24 09:26:04 +00:00
doHandleRemainBlockForNewGroupImpl ( pInfo , pResultInfo , newgroup , pTaskInfo ) ;
2021-11-02 05:37:31 +00:00
}
}
2022-07-07 06:30:08 +00:00
static SSDataBlock * doFillImpl ( SOperatorInfo * pOperator ) {
2022-03-29 07:24:25 +00:00
SFillOperatorInfo * pInfo = pOperator - > info ;
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2021-11-02 05:37:31 +00:00
2022-03-24 03:15:05 +00:00
SResultInfo * pResultInfo = & pOperator - > resultInfo ;
2022-04-20 06:59:06 +00:00
SSDataBlock * pResBlock = pInfo - > pRes ;
2022-05-03 07:27:13 +00:00
// todo handle different group data interpolation
2022-05-10 00:55:32 +00:00
bool n = false ;
bool * newgroup = & n ;
2022-03-24 09:26:04 +00:00
doHandleRemainBlockFromNewGroup ( pInfo , pResultInfo , newgroup , pTaskInfo ) ;
2022-04-20 06:59:06 +00:00
if ( pResBlock - > info . rows > pResultInfo - > threshold | | ( ! pInfo - > multigroupResult & & pResBlock - > info . rows > 0 ) ) {
return pResBlock ;
2022-03-24 06:21:14 +00:00
}
2021-11-02 05:37:31 +00:00
2022-03-24 03:15:05 +00:00
SOperatorInfo * pDownstream = pOperator - > pDownstream [ 0 ] ;
2022-03-29 07:24:25 +00:00
while ( 1 ) {
2022-05-03 07:27:13 +00:00
SSDataBlock * pBlock = pDownstream - > fpSet . getNextFn ( pDownstream ) ;
2021-11-02 05:37:31 +00:00
if ( * newgroup ) {
assert ( pBlock ! = NULL ) ;
}
if ( * newgroup & & pInfo - > totalInputRows > 0 ) { // there are already processed current group data block
pInfo - > existNewGroupBlock = pBlock ;
* newgroup = false ;
// Fill the previous group data block, before handle the data block of new group.
// Close the fill operation for previous group data block
2022-07-06 10:08:23 +00:00
taosFillSetStartInfo ( pInfo - > pFillInfo , 0 , pInfo - > win . ekey ) ;
2021-11-02 05:37:31 +00:00
} else {
if ( pBlock = = NULL ) {
if ( pInfo - > totalInputRows = = 0 ) {
pOperator - > status = OP_EXEC_DONE ;
return NULL ;
}
2022-07-06 10:08:23 +00:00
taosFillSetStartInfo ( pInfo - > pFillInfo , 0 , pInfo - > win . ekey ) ;
2021-11-02 05:37:31 +00:00
} else {
pInfo - > totalInputRows + = pBlock - > info . rows ;
taosFillSetStartInfo ( pInfo - > pFillInfo , pBlock - > info . rows , pBlock - > info . window . ekey ) ;
taosFillSetInputDataBlock ( pInfo - > pFillInfo , pBlock ) ;
}
}
2022-05-02 15:52:32 +00:00
blockDataEnsureCapacity ( pResBlock , pOperator - > resultInfo . capacity ) ;
doFillTimeIntervalGapsInResults ( pInfo - > pFillInfo , pResBlock , pOperator - > resultInfo . capacity ) ;
2021-11-02 05:37:31 +00:00
// current group has no more result to return
2022-04-20 06:59:06 +00:00
if ( pResBlock - > info . rows > 0 ) {
2021-11-02 05:37:31 +00:00
// 1. The result in current group not reach the threshold of output result, continue
// 2. If multiple group results existing in one SSDataBlock is not allowed, return immediately
2022-04-20 06:59:06 +00:00
if ( pResBlock - > info . rows > pResultInfo - > threshold | | pBlock = = NULL | | ( ! pInfo - > multigroupResult ) ) {
return pResBlock ;
2021-11-02 05:37:31 +00:00
}
2022-03-24 09:26:04 +00:00
doHandleRemainBlockFromNewGroup ( pInfo , pResultInfo , newgroup , pTaskInfo ) ;
2022-04-20 06:59:06 +00:00
if ( pResBlock - > info . rows > pOperator - > resultInfo . threshold | | pBlock = = NULL ) {
return pResBlock ;
2021-11-02 05:37:31 +00:00
}
} else if ( pInfo - > existNewGroupBlock ) { // try next group
assert ( pBlock ! = NULL ) ;
2022-03-24 09:26:04 +00:00
doHandleRemainBlockForNewGroupImpl ( pInfo , pResultInfo , newgroup , pTaskInfo ) ;
2022-04-20 06:59:06 +00:00
if ( pResBlock - > info . rows > pResultInfo - > threshold ) {
return pResBlock ;
2021-11-02 05:37:31 +00:00
}
} else {
return NULL ;
}
}
}
2022-07-07 06:30:08 +00:00
static SSDataBlock * doFill ( SOperatorInfo * pOperator ) {
SFillOperatorInfo * pInfo = pOperator - > info ;
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
SResultInfo * pResultInfo = & pOperator - > resultInfo ;
SSDataBlock * pResBlock = pInfo - > pRes ;
blockDataCleanup ( pResBlock ) ;
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
while ( true ) {
SSDataBlock * fillResult = doFillImpl ( pOperator ) ;
if ( fillResult ! = NULL ) {
doFilter ( pInfo - > pCondition , fillResult ) ;
}
if ( fillResult = = NULL ) {
doSetOperatorCompleted ( pOperator ) ;
break ;
}
if ( fillResult - > info . rows > 0 ) {
break ;
}
}
size_t rows = pResBlock - > info . rows ;
pOperator - > resultInfo . totalRows + = rows ;
return ( rows = = 0 ) ? NULL : pResBlock ;
}
2022-06-04 11:19:49 +00:00
static void destroyExprInfo ( SExprInfo * pExpr , int32_t numOfExprs ) {
for ( int32_t i = 0 ; i < numOfExprs ; + + i ) {
SExprInfo * pExprInfo = & pExpr [ i ] ;
if ( pExprInfo - > pExpr - > nodeType = = QUERY_NODE_COLUMN ) {
taosMemoryFree ( pExprInfo - > base . pParam [ 0 ] . pCol ) ;
}
taosMemoryFree ( pExprInfo - > base . pParam ) ;
taosMemoryFree ( pExprInfo - > pExpr ) ;
}
}
2021-11-02 05:37:31 +00:00
static void destroyOperatorInfo ( SOperatorInfo * pOperator ) {
if ( pOperator = = NULL ) {
return ;
}
2022-04-26 12:26:32 +00:00
if ( pOperator - > fpSet . closeFn ! = NULL ) {
2022-06-18 04:00:41 +00:00
pOperator - > fpSet . closeFn ( pOperator - > info , pOperator - > exprSupp . numOfExprs ) ;
2021-11-02 05:37:31 +00:00
}
2022-01-08 08:28:44 +00:00
if ( pOperator - > pDownstream ! = NULL ) {
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < pOperator - > numOfDownstream ; + + i ) {
2022-01-08 08:28:44 +00:00
destroyOperatorInfo ( pOperator - > pDownstream [ i ] ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pOperator - > pDownstream ) ;
2022-01-08 14:59:24 +00:00
pOperator - > numOfDownstream = 0 ;
2021-11-02 05:37:31 +00:00
}
2022-06-18 04:00:41 +00:00
if ( pOperator - > exprSupp . pExprInfo ! = NULL ) {
destroyExprInfo ( pOperator - > exprSupp . pExprInfo , pOperator - > exprSupp . numOfExprs ) ;
2022-05-07 08:22:52 +00:00
}
2022-06-18 04:00:41 +00:00
taosMemoryFreeClear ( pOperator - > exprSupp . pExprInfo ) ;
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pOperator - > info ) ;
taosMemoryFreeClear ( pOperator ) ;
2021-11-02 05:37:31 +00:00
}
2022-05-26 09:48:07 +00:00
int32_t getBufferPgSize ( int32_t rowSize , uint32_t * defaultPgsz , uint32_t * defaultBufsz ) {
* defaultPgsz = 4096 ;
while ( * defaultPgsz < rowSize * 4 ) {
* defaultPgsz < < = 1u ;
}
// at least four pages need to be in buffer
* defaultBufsz = 4096 * 256 ;
if ( ( * defaultBufsz ) < = ( * defaultPgsz ) ) {
( * defaultBufsz ) = ( * defaultPgsz ) * 4 ;
}
return 0 ;
}
2022-04-23 10:29:45 +00:00
int32_t doInitAggInfoSup ( SAggSupporter * pAggSup , SqlFunctionCtx * pCtx , int32_t numOfOutput , size_t keyBufSize ,
const char * pKey ) {
2022-02-22 05:12:03 +00:00
_hash_fn_t hashFn = taosGetDefaultHashFunction ( TSDB_DATA_TYPE_BINARY ) ;
2022-04-23 10:29:45 +00:00
pAggSup - > resultRowSize = getResultRowSize ( pCtx , numOfOutput ) ;
pAggSup - > keyBuf = taosMemoryCalloc ( 1 , keyBufSize + POINTER_BYTES + sizeof ( int64_t ) ) ;
2022-02-22 05:12:03 +00:00
pAggSup - > pResultRowHashTable = taosHashInit ( 10 , hashFn , true , HASH_NO_LOCK ) ;
2022-05-08 08:06:47 +00:00
if ( pAggSup - > keyBuf = = NULL | | pAggSup - > pResultRowHashTable = = NULL ) {
2022-02-22 05:12:03 +00:00
return TSDB_CODE_OUT_OF_MEMORY ;
}
2022-05-27 11:54:22 +00:00
uint32_t defaultPgsz = 0 ;
2022-05-26 09:48:07 +00:00
uint32_t defaultBufsz = 0 ;
getBufferPgSize ( pAggSup - > resultRowSize , & defaultPgsz , & defaultBufsz ) ;
2022-05-08 08:06:47 +00:00
2022-05-21 15:47:39 +00:00
int32_t code = createDiskbasedBuf ( & pAggSup - > pResultBuf , defaultPgsz , defaultBufsz , pKey , TD_TMP_DIR_PATH ) ;
2022-03-15 08:51:50 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
return code ;
}
2022-02-22 05:12:03 +00:00
return TSDB_CODE_SUCCESS ;
}
2022-05-03 06:43:53 +00:00
void cleanupAggSup ( SAggSupporter * pAggSup ) {
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pAggSup - > keyBuf ) ;
2022-02-22 05:12:03 +00:00
taosHashCleanup ( pAggSup - > pResultRowHashTable ) ;
2022-03-15 08:51:50 +00:00
destroyDiskbasedBuf ( pAggSup - > pResultBuf ) ;
2022-02-22 05:12:03 +00:00
}
2022-06-20 06:40:13 +00:00
int32_t initAggInfo ( SExprSupp * pSup , SAggSupporter * pAggSup , SExprInfo * pExprInfo , int32_t numOfCols , size_t keyBufSize ,
const char * pkey ) {
2022-06-20 15:22:28 +00:00
int32_t code = initExprSupp ( pSup , pExprInfo , numOfCols ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
return code ;
}
2022-06-18 04:00:41 +00:00
doInitAggInfoSup ( pAggSup , pSup - > pCtx , numOfCols , keyBufSize , pkey ) ;
2022-04-28 08:31:35 +00:00
for ( int32_t i = 0 ; i < numOfCols ; + + i ) {
2022-06-18 04:00:41 +00:00
pSup - > pCtx [ i ] . pBuf = pAggSup - > pResultBuf ;
2022-04-22 14:43:07 +00:00
}
2022-04-02 06:29:43 +00:00
return TSDB_CODE_SUCCESS ;
2022-03-14 08:09:26 +00:00
}
2022-04-18 10:47:59 +00:00
void initResultSizeInfo ( SOperatorInfo * pOperator , int32_t numOfRows ) {
2022-06-29 07:03:17 +00:00
ASSERT ( numOfRows ! = 0 ) ;
2022-04-18 10:47:59 +00:00
pOperator - > resultInfo . capacity = numOfRows ;
pOperator - > resultInfo . threshold = numOfRows * 0.75 ;
if ( pOperator - > resultInfo . threshold = = 0 ) {
2022-06-29 07:03:17 +00:00
pOperator - > resultInfo . threshold = numOfRows ;
2022-04-18 10:47:59 +00:00
}
}
2022-06-18 06:49:27 +00:00
void initBasicInfo ( SOptrBasicInfo * pInfo , SSDataBlock * pBlock ) {
pInfo - > pRes = pBlock ;
initResultRowInfo ( & pInfo - > resultRowInfo ) ;
}
2022-06-20 15:22:28 +00:00
int32_t initExprSupp ( SExprSupp * pSup , SExprInfo * pExprInfo , int32_t numOfExpr ) {
2022-06-18 06:49:27 +00:00
pSup - > pExprInfo = pExprInfo ;
pSup - > numOfExprs = numOfExpr ;
if ( pSup - > pExprInfo ! = NULL ) {
pSup - > pCtx = createSqlFunctionCtx ( pExprInfo , numOfExpr , & pSup - > rowEntryInfoOffset ) ;
2022-06-20 15:22:28 +00:00
if ( pSup - > pCtx = = NULL ) {
return TSDB_CODE_OUT_OF_MEMORY ;
}
2022-06-18 06:49:27 +00:00
}
2022-06-20 15:22:28 +00:00
return TSDB_CODE_SUCCESS ;
2022-06-18 06:49:27 +00:00
}
2022-03-29 07:24:25 +00:00
SOperatorInfo * createAggregateOperatorInfo ( SOperatorInfo * downstream , SExprInfo * pExprInfo , int32_t numOfCols ,
2022-07-06 05:54:11 +00:00
SSDataBlock * pResultBlock , SNode * pCondition , SExprInfo * pScalarExprInfo ,
2022-05-26 08:05:27 +00:00
int32_t numOfScalarExpr , SExecTaskInfo * pTaskInfo ) {
2022-03-25 16:29:53 +00:00
SAggOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( SAggOperatorInfo ) ) ;
2022-03-29 07:24:25 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
2022-03-14 06:15:26 +00:00
if ( pInfo = = NULL | | pOperator = = NULL ) {
goto _error ;
}
2022-02-13 06:34:00 +00:00
2022-05-17 14:42:11 +00:00
int32_t numOfRows = 1024 ;
2022-04-23 10:29:45 +00:00
size_t keyBufSize = sizeof ( int64_t ) + sizeof ( int64_t ) + POINTER_BYTES ;
2022-04-18 10:47:59 +00:00
initResultSizeInfo ( pOperator , numOfRows ) ;
2022-06-18 06:49:27 +00:00
int32_t code = initAggInfo ( & pOperator - > exprSupp , & pInfo - > aggSup , pExprInfo , numOfCols , keyBufSize , pTaskInfo - > id . str ) ;
2022-04-28 08:31:35 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2022-03-14 06:15:26 +00:00
goto _error ;
}
2022-02-13 06:34:00 +00:00
2022-06-18 06:49:27 +00:00
initBasicInfo ( & pInfo - > binfo , pResultBlock ) ;
2022-06-20 15:22:28 +00:00
code = initExprSupp ( & pInfo - > scalarExprSup , pScalarExprInfo , numOfScalarExpr ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
}
2021-11-02 05:37:31 +00:00
2022-06-20 06:40:13 +00:00
pInfo - > groupId = INT32_MIN ;
2022-07-06 05:54:11 +00:00
pInfo - > pCondition = pCondition ;
2022-04-23 10:29:45 +00:00
pOperator - > name = " TableAggregate " ;
2022-06-07 12:59:44 +00:00
pOperator - > operatorType = QUERY_NODE_PHYSICAL_PLAN_HASH_AGG ;
2022-05-03 15:23:49 +00:00
pOperator - > blocking = true ;
2022-04-23 10:29:45 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > info = pInfo ;
pOperator - > pTaskInfo = pTaskInfo ;
2022-04-16 11:10:21 +00:00
2022-04-26 12:26:32 +00:00
pOperator - > fpSet = createOperatorFpSet ( doOpenAggregateOptr , getAggregateResult , NULL , NULL , destroyAggOperatorInfo ,
aggEncodeResultRow , aggDecodeResultRow , NULL ) ;
2022-03-14 06:15:26 +00:00
code = appendDownstream ( pOperator , & downstream , 1 ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
}
2021-11-02 05:37:31 +00:00
return pOperator ;
2022-03-29 07:24:25 +00:00
_error :
2022-03-14 06:15:26 +00:00
destroyAggOperatorInfo ( pInfo , numOfCols ) ;
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pInfo ) ;
taosMemoryFreeClear ( pOperator ) ;
2022-03-14 06:15:26 +00:00
pTaskInfo - > code = TSDB_CODE_OUT_OF_MEMORY ;
return NULL ;
2021-11-02 05:37:31 +00:00
}
2022-06-17 11:01:45 +00:00
static void * destroySqlFunctionCtx ( SqlFunctionCtx * pCtx , int32_t numOfOutput ) {
if ( pCtx = = NULL ) {
return NULL ;
}
for ( int32_t i = 0 ; i < numOfOutput ; + + i ) {
for ( int32_t j = 0 ; j < pCtx [ i ] . numOfParams ; + + j ) {
taosVariantDestroy ( & pCtx [ i ] . param [ j ] . param ) ;
}
taosMemoryFreeClear ( pCtx [ i ] . subsidiaries . pCtx ) ;
taosMemoryFree ( pCtx [ i ] . input . pData ) ;
taosMemoryFree ( pCtx [ i ] . input . pColumnDataAgg ) ;
}
taosMemoryFreeClear ( pCtx ) ;
return NULL ;
}
2022-06-18 06:49:27 +00:00
void cleanupBasicInfo ( SOptrBasicInfo * pInfo ) {
2021-11-02 05:37:31 +00:00
assert ( pInfo ! = NULL ) ;
cleanupResultRowInfo ( & pInfo - > resultRowInfo ) ;
2022-02-08 10:01:21 +00:00
pInfo - > pRes = blockDataDestroy ( pInfo - > pRes ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-14 06:15:26 +00:00
void destroyBasicOperatorInfo ( void * param , int32_t numOfOutput ) {
2022-03-29 07:24:25 +00:00
SOptrBasicInfo * pInfo = ( SOptrBasicInfo * ) param ;
2022-06-18 06:49:27 +00:00
cleanupBasicInfo ( pInfo ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-14 06:15:26 +00:00
void destroyAggOperatorInfo ( void * param , int32_t numOfOutput ) {
2022-03-29 07:24:25 +00:00
SAggOperatorInfo * pInfo = ( SAggOperatorInfo * ) param ;
2022-06-18 06:49:27 +00:00
cleanupBasicInfo ( & pInfo - > binfo ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-10 03:12:44 +00:00
2022-03-14 06:15:26 +00:00
void destroySFillOperatorInfo ( void * param , int32_t numOfOutput ) {
2022-03-29 07:24:25 +00:00
SFillOperatorInfo * pInfo = ( SFillOperatorInfo * ) param ;
2021-11-02 05:37:31 +00:00
pInfo - > pFillInfo = taosDestroyFillInfo ( pInfo - > pFillInfo ) ;
2022-02-08 10:01:21 +00:00
pInfo - > pRes = blockDataDestroy ( pInfo - > pRes ) ;
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pInfo - > p ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-18 03:12:49 +00:00
static void destroyProjectOperatorInfo ( void * param , int32_t numOfOutput ) {
2022-05-09 03:40:24 +00:00
if ( NULL = = param ) {
return ;
}
2022-03-29 07:24:25 +00:00
SProjectOperatorInfo * pInfo = ( SProjectOperatorInfo * ) param ;
2022-06-18 06:49:27 +00:00
cleanupBasicInfo ( & pInfo - > binfo ) ;
2022-05-07 07:11:49 +00:00
cleanupAggSup ( & pInfo - > aggSup ) ;
2022-05-07 08:22:52 +00:00
taosArrayDestroy ( pInfo - > pPseudoColInfo ) ;
2021-11-02 05:37:31 +00:00
}
2022-06-29 08:19:19 +00:00
void cleanupExprSupp ( SExprSupp * pSupp ) {
2022-06-17 15:23:37 +00:00
destroySqlFunctionCtx ( pSupp - > pCtx , pSupp - > numOfExprs ) ;
destroyExprInfo ( pSupp - > pExprInfo , pSupp - > numOfExprs ) ;
taosMemoryFree ( pSupp - > rowEntryInfoOffset ) ;
}
2022-06-04 11:19:49 +00:00
static void destroyIndefinitOperatorInfo ( void * param , int32_t numOfOutput ) {
2022-06-04 14:57:01 +00:00
SIndefOperatorInfo * pInfo = ( SIndefOperatorInfo * ) param ;
2022-06-18 06:49:27 +00:00
cleanupBasicInfo ( & pInfo - > binfo ) ;
2022-06-04 11:19:49 +00:00
taosArrayDestroy ( pInfo - > pPseudoColInfo ) ;
cleanupAggSup ( & pInfo - > aggSup ) ;
2022-06-29 08:19:19 +00:00
cleanupExprSupp ( & pInfo - > scalarSup ) ;
2022-06-04 11:19:49 +00:00
}
2022-03-04 07:53:30 +00:00
void destroyExchangeOperatorInfo ( void * param , int32_t numOfOutput ) {
2022-03-29 07:24:25 +00:00
SExchangeInfo * pExInfo = ( SExchangeInfo * ) param ;
2022-06-15 08:26:43 +00:00
taosRemoveRef ( exchangeObjRefPool , pExInfo - > self ) ;
}
void doDestroyExchangeOperatorInfo ( void * param ) {
2022-06-16 01:31:22 +00:00
SExchangeInfo * pExInfo = ( SExchangeInfo * ) param ;
2022-06-15 08:26:43 +00:00
2022-03-04 07:53:30 +00:00
taosArrayDestroy ( pExInfo - > pSources ) ;
taosArrayDestroy ( pExInfo - > pSourceDataInfo ) ;
if ( pExInfo - > pResult ! = NULL ) {
blockDataDestroy ( pExInfo - > pResult ) ;
}
tsem_destroy ( & pExInfo - > ready ) ;
}
2022-04-06 03:01:09 +00:00
static SArray * setRowTsColumnOutputInfo ( SqlFunctionCtx * pCtx , int32_t numOfCols ) {
SArray * pList = taosArrayInit ( 4 , sizeof ( int32_t ) ) ;
2022-04-23 10:29:45 +00:00
for ( int32_t i = 0 ; i < numOfCols ; + + i ) {
2022-04-06 03:01:09 +00:00
if ( fmIsPseudoColumnFunc ( pCtx [ i ] . functionId ) ) {
taosArrayPush ( pList , & i ) ;
}
}
return pList ;
}
2022-06-23 10:03:15 +00:00
static int64_t getLimit ( SNode * pLimit ) { return NULL = = pLimit ? - 1 : ( ( SLimitNode * ) pLimit ) - > limit ; }
static int64_t getOffset ( SNode * pLimit ) { return NULL = = pLimit ? - 1 : ( ( SLimitNode * ) pLimit ) - > offset ; }
2022-06-20 06:40:13 +00:00
SOperatorInfo * createProjectOperatorInfo ( SOperatorInfo * downstream , SProjectPhysiNode * pProjPhyNode ,
2022-04-23 10:29:45 +00:00
SExecTaskInfo * pTaskInfo ) {
2022-03-25 16:29:53 +00:00
SProjectOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( SProjectOperatorInfo ) ) ;
2022-03-29 07:24:25 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
2022-03-14 06:15:26 +00:00
if ( pInfo = = NULL | | pOperator = = NULL ) {
goto _error ;
}
2021-11-02 05:37:31 +00:00
2022-06-20 06:40:13 +00:00
int32_t numOfCols = 0 ;
2022-06-17 11:01:45 +00:00
SExprInfo * pExprInfo = createExprInfo ( pProjPhyNode - > pProjections , NULL , & numOfCols ) ;
SSDataBlock * pResBlock = createResDataBlock ( pProjPhyNode - > node . pOutputDataBlockDesc ) ;
2022-06-23 10:03:15 +00:00
SLimit limit = { . limit = getLimit ( pProjPhyNode - > node . pLimit ) , . offset = getOffset ( pProjPhyNode - > node . pLimit ) } ;
SLimit slimit = { . limit = getLimit ( pProjPhyNode - > node . pSlimit ) , . offset = getOffset ( pProjPhyNode - > node . pSlimit ) } ;
2022-06-17 11:01:45 +00:00
2022-06-20 06:40:13 +00:00
pInfo - > limit = limit ;
pInfo - > slimit = slimit ;
pInfo - > curOffset = limit . offset ;
pInfo - > curSOffset = slimit . offset ;
2022-03-12 14:59:12 +00:00
pInfo - > binfo . pRes = pResBlock ;
2022-06-17 11:01:45 +00:00
pInfo - > pFilterNode = pProjPhyNode - > node . pConditions ;
2022-04-04 06:54:39 +00:00
int32_t numOfRows = 4096 ;
2022-04-23 10:29:45 +00:00
size_t keyBufSize = sizeof ( int64_t ) + sizeof ( int64_t ) + POINTER_BYTES ;
2022-04-18 10:47:59 +00:00
2022-05-19 15:47:27 +00:00
// Make sure the size of SSDataBlock will never exceed the size of 2MB.
int32_t TWOMB = 2 * 1024 * 1024 ;
if ( numOfRows * pResBlock - > info . rowSize > TWOMB ) {
numOfRows = TWOMB / pResBlock - > info . rowSize ;
}
2022-04-18 10:47:59 +00:00
initResultSizeInfo ( pOperator , numOfRows ) ;
2022-05-19 15:47:27 +00:00
2022-06-18 06:49:27 +00:00
initAggInfo ( & pOperator - > exprSupp , & pInfo - > aggSup , pExprInfo , numOfCols , keyBufSize , pTaskInfo - > id . str ) ;
initBasicInfo ( & pInfo - > binfo , pResBlock ) ;
2022-06-18 04:00:41 +00:00
setFunctionResultOutput ( pOperator , & pInfo - > binfo , & pInfo - > aggSup , MAIN_SCAN , numOfCols ) ;
2021-11-02 05:37:31 +00:00
2022-06-18 04:00:41 +00:00
pInfo - > pPseudoColInfo = setRowTsColumnOutputInfo ( pOperator - > exprSupp . pCtx , numOfCols ) ;
2022-05-23 11:50:08 +00:00
pOperator - > name = " ProjectOperator " ;
2022-03-12 10:02:56 +00:00
pOperator - > operatorType = QUERY_NODE_PHYSICAL_PLAN_PROJECT ;
2022-05-23 11:50:08 +00:00
pOperator - > blocking = false ;
pOperator - > status = OP_NOT_OPENED ;
pOperator - > info = pInfo ;
pOperator - > pTaskInfo = pTaskInfo ;
2022-04-26 12:26:32 +00:00
2022-04-28 08:31:35 +00:00
pOperator - > fpSet = createOperatorFpSet ( operatorDummyOpenFn , doProjectOperation , NULL , NULL ,
destroyProjectOperatorInfo , NULL , NULL , NULL ) ;
2022-03-29 07:24:25 +00:00
2022-02-22 05:12:03 +00:00
int32_t code = appendDownstream ( pOperator , & downstream , 1 ) ;
2022-03-18 10:02:00 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2022-03-14 06:15:26 +00:00
goto _error ;
}
2021-11-02 05:37:31 +00:00
return pOperator ;
2022-03-14 06:15:26 +00:00
2022-03-29 07:24:25 +00:00
_error :
2022-03-14 06:15:26 +00:00
pTaskInfo - > code = TSDB_CODE_OUT_OF_MEMORY ;
return NULL ;
2021-11-02 05:37:31 +00:00
}
2022-06-04 11:19:49 +00:00
static SSDataBlock * doApplyIndefinitFunction ( SOperatorInfo * pOperator ) {
SIndefOperatorInfo * pIndefInfo = pOperator - > info ;
2022-06-04 14:57:01 +00:00
SOptrBasicInfo * pInfo = & pIndefInfo - > binfo ;
2022-06-20 06:40:13 +00:00
SExprSupp * pSup = & pOperator - > exprSupp ;
2022-06-04 11:19:49 +00:00
SSDataBlock * pRes = pInfo - > pRes ;
blockDataCleanup ( pRes ) ;
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
int64_t st = 0 ;
int32_t order = 0 ;
int32_t scanFlag = 0 ;
if ( pOperator - > cost . openCost = = 0 ) {
st = taosGetTimestampUs ( ) ;
}
SOperatorInfo * downstream = pOperator - > pDownstream [ 0 ] ;
while ( 1 ) {
// The downstream exec may change the value of the newgroup, so use a local variable instead.
SSDataBlock * pBlock = downstream - > fpSet . getNextFn ( downstream ) ;
if ( pBlock = = NULL ) {
doSetOperatorCompleted ( pOperator ) ;
break ;
}
// the pDataBlock are always the same one, no need to call this again
int32_t code = getTableScanInfo ( pOperator - > pDownstream [ 0 ] , & order , & scanFlag ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
longjmp ( pTaskInfo - > env , code ) ;
}
// there is an scalar expression that needs to be calculated before apply the group aggregation.
2022-06-18 04:00:41 +00:00
SExprSupp * pScalarSup = & pIndefInfo - > scalarSup ;
2022-06-17 15:23:37 +00:00
if ( pScalarSup - > pExprInfo ! = NULL ) {
code = projectApplyFunctions ( pScalarSup - > pExprInfo , pBlock , pBlock , pScalarSup - > pCtx , pScalarSup - > numOfExprs ,
2022-06-20 06:40:13 +00:00
pIndefInfo - > pPseudoColInfo ) ;
2022-06-04 11:19:49 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
longjmp ( pTaskInfo - > env , code ) ;
}
}
2022-06-18 04:00:41 +00:00
setInputDataBlock ( pOperator , pSup - > pCtx , pBlock , order , scanFlag , false ) ;
2022-06-04 11:19:49 +00:00
blockDataEnsureCapacity ( pInfo - > pRes , pInfo - > pRes - > info . rows + pBlock - > info . rows ) ;
2022-06-20 06:40:13 +00:00
code = projectApplyFunctions ( pOperator - > exprSupp . pExprInfo , pInfo - > pRes , pBlock , pSup - > pCtx ,
pOperator - > exprSupp . numOfExprs , pIndefInfo - > pPseudoColInfo ) ;
2022-06-04 11:19:49 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
longjmp ( pTaskInfo - > env , code ) ;
}
}
size_t rows = pInfo - > pRes - > info . rows ;
pOperator - > resultInfo . totalRows + = rows ;
if ( pOperator - > cost . openCost = = 0 ) {
pOperator - > cost . openCost = ( taosGetTimestampUs ( ) - st ) / 1000.0 ;
}
return ( rows > 0 ) ? pInfo - > pRes : NULL ;
}
2022-06-04 14:57:01 +00:00
SOperatorInfo * createIndefinitOutputOperatorInfo ( SOperatorInfo * downstream , SPhysiNode * pNode ,
SExecTaskInfo * pTaskInfo ) {
2022-06-04 11:19:49 +00:00
SIndefOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( SIndefOperatorInfo ) ) ;
2022-06-04 14:57:01 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
2022-06-04 11:19:49 +00:00
if ( pInfo = = NULL | | pOperator = = NULL ) {
goto _error ;
}
2022-06-18 04:00:41 +00:00
SExprSupp * pSup = & pOperator - > exprSupp ;
2022-06-04 11:19:49 +00:00
SIndefRowsFuncPhysiNode * pPhyNode = ( SIndefRowsFuncPhysiNode * ) pNode ;
int32_t numOfExpr = 0 ;
2022-06-19 11:39:12 +00:00
SExprInfo * pExprInfo = createExprInfo ( pPhyNode - > pFuncs , NULL , & numOfExpr ) ;
2022-06-04 11:19:49 +00:00
if ( pPhyNode - > pExprs ! = NULL ) {
2022-06-22 05:08:20 +00:00
int32_t num = 0 ;
2022-06-20 15:22:28 +00:00
SExprInfo * pSExpr = createExprInfo ( pPhyNode - > pExprs , NULL , & num ) ;
2022-06-22 05:08:20 +00:00
int32_t code = initExprSupp ( & pInfo - > scalarSup , pSExpr , num ) ;
2022-06-20 15:22:28 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
}
2022-06-04 11:19:49 +00:00
}
2022-06-04 14:57:01 +00:00
SSDataBlock * pResBlock = createResDataBlock ( pPhyNode - > node . pOutputDataBlockDesc ) ;
2022-06-04 11:19:49 +00:00
int32_t numOfRows = 4096 ;
size_t keyBufSize = sizeof ( int64_t ) + sizeof ( int64_t ) + POINTER_BYTES ;
// Make sure the size of SSDataBlock will never exceed the size of 2MB.
int32_t TWOMB = 2 * 1024 * 1024 ;
if ( numOfRows * pResBlock - > info . rowSize > TWOMB ) {
numOfRows = TWOMB / pResBlock - > info . rowSize ;
}
initResultSizeInfo ( pOperator , numOfRows ) ;
2022-06-18 06:49:27 +00:00
initAggInfo ( & pOperator - > exprSupp , & pInfo - > aggSup , pExprInfo , numOfExpr , keyBufSize , pTaskInfo - > id . str ) ;
initBasicInfo ( & pInfo - > binfo , pResBlock ) ;
2022-06-18 04:00:41 +00:00
setFunctionResultOutput ( pOperator , & pInfo - > binfo , & pInfo - > aggSup , MAIN_SCAN , numOfExpr ) ;
2022-06-04 11:19:49 +00:00
2022-06-22 05:08:20 +00:00
pInfo - > binfo . pRes = pResBlock ;
pInfo - > pPseudoColInfo = setRowTsColumnOutputInfo ( pSup - > pCtx , numOfExpr ) ;
2022-06-04 11:19:49 +00:00
2022-06-04 14:57:01 +00:00
pOperator - > name = " IndefinitOperator " ;
2022-06-04 11:19:49 +00:00
pOperator - > operatorType = QUERY_NODE_PHYSICAL_PLAN_PROJECT ;
2022-06-22 05:08:20 +00:00
pOperator - > blocking = false ;
pOperator - > status = OP_NOT_OPENED ;
pOperator - > info = pInfo ;
2022-06-18 04:00:41 +00:00
pOperator - > exprSupp . pExprInfo = pExprInfo ;
pOperator - > exprSupp . numOfExprs = numOfExpr ;
2022-06-04 14:57:01 +00:00
pOperator - > pTaskInfo = pTaskInfo ;
2022-06-04 11:19:49 +00:00
pOperator - > fpSet = createOperatorFpSet ( operatorDummyOpenFn , doApplyIndefinitFunction , NULL , NULL ,
destroyIndefinitOperatorInfo , NULL , NULL , NULL ) ;
int32_t code = appendDownstream ( pOperator , & downstream , 1 ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
}
return pOperator ;
2022-06-04 14:57:01 +00:00
_error :
2022-06-04 11:19:49 +00:00
taosMemoryFree ( pInfo ) ;
taosMemoryFree ( pOperator ) ;
pTaskInfo - > code = TSDB_CODE_OUT_OF_MEMORY ;
return NULL ;
}
2022-05-02 15:52:32 +00:00
static int32_t initFillInfo ( SFillOperatorInfo * pInfo , SExprInfo * pExpr , int32_t numOfCols , SNodeListNode * pValNode ,
2022-03-29 07:24:25 +00:00
STimeWindow win , int32_t capacity , const char * id , SInterval * pInterval , int32_t fillType ) {
2022-05-02 15:52:32 +00:00
SFillColInfo * pColInfo = createFillColInfo ( pExpr , numOfCols , pValNode ) ;
2022-03-24 06:21:14 +00:00
STimeWindow w = TSWINDOW_INITIALIZER ;
2022-05-02 15:52:32 +00:00
getAlignQueryTimeWindow ( pInterval , pInterval - > precision , win . skey , & w ) ;
2022-03-24 06:21:14 +00:00
int32_t order = TSDB_ORDER_ASC ;
2022-04-20 06:59:06 +00:00
pInfo - > pFillInfo = taosCreateFillInfo ( order , w . skey , 0 , capacity , numOfCols , pInterval , fillType , pColInfo , id ) ;
2022-03-24 06:21:14 +00:00
2022-07-06 10:08:23 +00:00
pInfo - > win = win ;
pInfo - > p = taosMemoryCalloc ( numOfCols , POINTER_BYTES ) ;
2022-03-24 06:21:14 +00:00
if ( pInfo - > pFillInfo = = NULL | | pInfo - > p = = NULL ) {
2022-06-14 06:45:17 +00:00
taosMemoryFree ( pInfo - > pFillInfo ) ;
taosMemoryFree ( pInfo - > p ) ;
2022-03-24 06:21:14 +00:00
return TSDB_CODE_OUT_OF_MEMORY ;
} else {
return TSDB_CODE_SUCCESS ;
}
}
2022-06-17 07:48:32 +00:00
SOperatorInfo * createFillOperatorInfo ( SOperatorInfo * downstream , SFillPhysiNode * pPhyFillNode , bool multigroupResult ,
SExecTaskInfo * pTaskInfo ) {
SFillOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( SFillOperatorInfo ) ) ;
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
if ( pInfo = = NULL | | pOperator = = NULL ) {
goto _error ;
}
2022-06-20 06:40:13 +00:00
int32_t num = 0 ;
SSDataBlock * pResBlock = createResDataBlock ( pPhyFillNode - > node . pOutputDataBlockDesc ) ;
SExprInfo * pExprInfo = createExprInfo ( pPhyFillNode - > pTargets , NULL , & num ) ;
SInterval * pInterval = & ( ( SIntervalAggOperatorInfo * ) downstream - > info ) - > interval ;
int32_t type = convertFillType ( pPhyFillNode - > mode ) ;
2022-06-17 07:48:32 +00:00
2022-03-24 03:15:05 +00:00
SResultInfo * pResultInfo = & pOperator - > resultInfo ;
2022-04-20 06:59:06 +00:00
initResultSizeInfo ( pOperator , 4096 ) ;
2022-06-17 07:48:32 +00:00
int32_t code = initFillInfo ( pInfo , pExprInfo , num , ( SNodeListNode * ) pPhyFillNode - > pValues , pPhyFillNode - > timeRange ,
pResultInfo - > capacity , pTaskInfo - > id . str , pInterval , type ) ;
2022-03-24 09:26:04 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
}
2021-11-02 05:37:31 +00:00
2022-06-20 06:40:13 +00:00
pInfo - > pRes = pResBlock ;
2022-06-17 07:48:32 +00:00
pInfo - > multigroupResult = multigroupResult ;
2022-07-07 06:30:08 +00:00
pInfo - > pCondition = pPhyFillNode - > node . pConditions ;
2022-04-23 10:29:45 +00:00
pOperator - > name = " FillOperator " ;
2022-05-03 15:23:49 +00:00
pOperator - > blocking = false ;
2022-04-23 10:29:45 +00:00
pOperator - > status = OP_NOT_OPENED ;
2022-05-02 15:52:32 +00:00
pOperator - > operatorType = QUERY_NODE_PHYSICAL_PLAN_FILL ;
2022-06-22 05:08:20 +00:00
pOperator - > exprSupp . pExprInfo = pExprInfo ;
pOperator - > exprSupp . numOfExprs = num ;
pOperator - > info = pInfo ;
pOperator - > pTaskInfo = pTaskInfo ;
2022-03-24 03:15:05 +00:00
2022-04-28 08:31:35 +00:00
pOperator - > fpSet =
createOperatorFpSet ( operatorDummyOpenFn , doFill , NULL , NULL , destroySFillOperatorInfo , NULL , NULL , NULL ) ;
2022-06-17 07:48:32 +00:00
2022-03-24 09:26:04 +00:00
code = appendDownstream ( pOperator , & downstream , 1 ) ;
2021-11-02 05:37:31 +00:00
return pOperator ;
2022-03-24 06:21:14 +00:00
2022-03-29 07:24:25 +00:00
_error :
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pOperator ) ;
taosMemoryFreeClear ( pInfo ) ;
2022-03-24 06:21:14 +00:00
return NULL ;
2021-11-02 05:37:31 +00:00
}
2022-05-20 10:00:05 +00:00
static SExecTaskInfo * createExecTaskInfo ( uint64_t queryId , uint64_t taskId , EOPTR_EXEC_MODEL model , char * dbFName ) {
2022-03-25 16:29:53 +00:00
SExecTaskInfo * pTaskInfo = taosMemoryCalloc ( 1 , sizeof ( SExecTaskInfo ) ) ;
2022-01-10 11:48:21 +00:00
setTaskStatus ( pTaskInfo , TASK_NOT_COMPLETED ) ;
2022-01-08 14:59:24 +00:00
2022-05-20 10:00:05 +00:00
pTaskInfo - > schemaVer . dbname = strdup ( dbFName ) ;
2022-01-10 11:48:21 +00:00
pTaskInfo - > cost . created = taosGetTimestampMs ( ) ;
2022-01-11 02:51:23 +00:00
pTaskInfo - > id . queryId = queryId ;
2022-04-23 10:29:45 +00:00
pTaskInfo - > execModel = model ;
2022-01-25 05:42:33 +00:00
2022-03-25 16:29:53 +00:00
char * p = taosMemoryCalloc ( 1 , 128 ) ;
2022-03-29 07:24:25 +00:00
snprintf ( p , 128 , " TID:0x% " PRIx64 " QID:0x% " PRIx64 , taskId , queryId ) ;
2022-05-07 07:11:49 +00:00
pTaskInfo - > id . str = p ;
2022-01-25 05:42:33 +00:00
2022-01-10 11:48:21 +00:00
return pTaskInfo ;
}
2022-01-08 14:59:24 +00:00
2022-06-16 09:53:07 +00:00
static STsdbReader * doCreateDataReader ( STableScanPhysiNode * pTableScanNode , SReadHandle * pHandle ,
2022-06-28 07:22:32 +00:00
STableListInfo * pTableListInfo , const char * idstr ) ;
2022-02-04 14:41:54 +00:00
2022-03-22 08:03:42 +00:00
static SArray * extractColumnInfo ( SNodeList * pNodeList ) ;
2022-04-09 02:18:52 +00:00
2022-06-10 09:07:24 +00:00
int32_t extractTableSchemaVersion ( SReadHandle * pHandle , uint64_t uid , SExecTaskInfo * pTaskInfo ) {
2022-05-18 12:39:00 +00:00
SMetaReader mr = { 0 } ;
metaReaderInit ( & mr , pHandle - > meta , 0 ) ;
2022-06-10 09:07:24 +00:00
int32_t code = metaGetTableEntryByUid ( & mr , uid ) ;
2022-07-06 05:33:21 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2022-06-10 09:07:24 +00:00
metaReaderClear ( & mr ) ;
2022-07-06 05:33:21 +00:00
return terrno ;
2022-06-10 09:07:24 +00:00
}
2022-05-18 12:39:00 +00:00
pTaskInfo - > schemaVer . tablename = strdup ( mr . me . name ) ;
if ( mr . me . type = = TSDB_SUPER_TABLE ) {
2022-06-28 15:24:20 +00:00
pTaskInfo - > schemaVer . sw = tCloneSSchemaWrapper ( & mr . me . stbEntry . schemaRow ) ;
2022-05-26 08:29:52 +00:00
pTaskInfo - > schemaVer . tversion = mr . me . stbEntry . schemaTag . version ;
2022-05-18 12:39:00 +00:00
} else if ( mr . me . type = = TSDB_CHILD_TABLE ) {
tb_uid_t suid = mr . me . ctbEntry . suid ;
metaGetTableEntryByUid ( & mr , suid ) ;
2022-06-28 15:24:20 +00:00
pTaskInfo - > schemaVer . sw = tCloneSSchemaWrapper ( & mr . me . stbEntry . schemaRow ) ;
2022-05-26 08:29:52 +00:00
pTaskInfo - > schemaVer . tversion = mr . me . stbEntry . schemaTag . version ;
2022-05-18 12:39:00 +00:00
} else {
2022-06-28 15:24:20 +00:00
pTaskInfo - > schemaVer . sw = tCloneSSchemaWrapper ( & mr . me . ntbEntry . schemaRow ) ;
2022-05-18 12:39:00 +00:00
}
2022-05-19 08:16:16 +00:00
metaReaderClear ( & mr ) ;
2022-06-10 09:07:24 +00:00
return TSDB_CODE_SUCCESS ;
2022-05-18 12:39:00 +00:00
}
2022-06-28 05:59:49 +00:00
static int32_t sortTableGroup ( STableListInfo * pTableListInfo , int32_t groupNum ) {
2022-06-24 07:57:33 +00:00
taosArrayClear ( pTableListInfo - > pGroupList ) ;
2022-06-28 05:59:49 +00:00
SArray * sortSupport = taosArrayInit ( groupNum , sizeof ( uint64_t ) ) ;
if ( sortSupport = = NULL ) return TSDB_CODE_OUT_OF_MEMORY ;
2022-06-24 07:57:33 +00:00
for ( int32_t i = 0 ; i < taosArrayGetSize ( pTableListInfo - > pTableList ) ; i + + ) {
STableKeyInfo * info = taosArrayGet ( pTableListInfo - > pTableList , i ) ;
2022-06-28 05:59:49 +00:00
uint64_t * groupId = taosHashGet ( pTableListInfo - > map , & info - > uid , sizeof ( uint64_t ) ) ;
2022-06-24 07:57:33 +00:00
int32_t index = taosArraySearchIdx ( sortSupport , groupId , compareUint64Val , TD_EQ ) ;
2022-06-28 05:59:49 +00:00
if ( index = = - 1 ) {
void * p = taosArraySearch ( sortSupport , groupId , compareUint64Val , TD_GT ) ;
SArray * tGroup = taosArrayInit ( 8 , sizeof ( STableKeyInfo ) ) ;
if ( tGroup = = NULL ) {
2022-06-24 07:57:33 +00:00
taosArrayDestroy ( sortSupport ) ;
return TSDB_CODE_OUT_OF_MEMORY ;
}
2022-06-28 05:59:49 +00:00
if ( taosArrayPush ( tGroup , info ) = = NULL ) {
2022-06-24 07:57:33 +00:00
qError ( " taos push info array error " ) ;
taosArrayDestroy ( sortSupport ) ;
return TSDB_CODE_QRY_APP_ERROR ;
}
2022-06-28 05:59:49 +00:00
if ( p = = NULL ) {
2022-07-01 09:35:23 +00:00
if ( taosArrayPush ( sortSupport , groupId ) = = NULL ) {
2022-06-24 07:57:33 +00:00
qError ( " taos push support array error " ) ;
taosArrayDestroy ( sortSupport ) ;
return TSDB_CODE_QRY_APP_ERROR ;
}
2022-07-01 09:35:23 +00:00
if ( taosArrayPush ( pTableListInfo - > pGroupList , & tGroup ) = = NULL ) {
2022-06-24 07:57:33 +00:00
qError ( " taos push group array error " ) ;
taosArrayDestroy ( sortSupport ) ;
return TSDB_CODE_QRY_APP_ERROR ;
}
2022-06-28 05:59:49 +00:00
} else {
2022-06-24 07:57:33 +00:00
int32_t pos = TARRAY_ELEM_IDX ( sortSupport , p ) ;
2022-06-28 05:59:49 +00:00
if ( taosArrayInsert ( sortSupport , pos , groupId ) = = NULL ) {
2022-06-24 07:57:33 +00:00
qError ( " taos insert support array error " ) ;
taosArrayDestroy ( sortSupport ) ;
return TSDB_CODE_QRY_APP_ERROR ;
}
2022-06-28 05:59:49 +00:00
if ( taosArrayInsert ( pTableListInfo - > pGroupList , pos , & tGroup ) = = NULL ) {
2022-06-24 07:57:33 +00:00
qError ( " taos insert group array error " ) ;
taosArrayDestroy ( sortSupport ) ;
return TSDB_CODE_QRY_APP_ERROR ;
}
}
2022-06-28 05:59:49 +00:00
} else {
2022-06-24 07:57:33 +00:00
SArray * tGroup = ( SArray * ) taosArrayGetP ( pTableListInfo - > pGroupList , index ) ;
2022-06-28 05:59:49 +00:00
if ( taosArrayPush ( tGroup , info ) = = NULL ) {
2022-06-24 07:57:33 +00:00
qError ( " taos push uid array error " ) ;
taosArrayDestroy ( sortSupport ) ;
return TSDB_CODE_QRY_APP_ERROR ;
}
}
}
taosArrayDestroy ( sortSupport ) ;
return TDB_CODE_SUCCESS ;
}
2022-06-21 02:52:09 +00:00
int32_t generateGroupIdMap ( STableListInfo * pTableListInfo , SReadHandle * pHandle , SNodeList * group ) {
if ( group = = NULL ) {
2022-06-13 11:59:30 +00:00
return TDB_CODE_SUCCESS ;
}
pTableListInfo - > map = taosHashInit ( 32 , taosGetDefaultHashFunction ( TSDB_DATA_TYPE_BINARY ) , false , HASH_NO_LOCK ) ;
if ( pTableListInfo - > map = = NULL ) {
return TSDB_CODE_OUT_OF_MEMORY ;
}
int32_t keyLen = 0 ;
2022-06-15 11:08:25 +00:00
void * keyBuf = NULL ;
2022-06-21 02:52:09 +00:00
2022-06-28 05:59:49 +00:00
SNode * node ;
2022-06-21 02:52:09 +00:00
FOREACH ( node , group ) {
2022-06-28 05:59:49 +00:00
SExprNode * pExpr = ( SExprNode * ) node ;
2022-06-21 02:52:09 +00:00
keyLen + = pExpr - > resType . bytes ;
2022-06-13 11:59:30 +00:00
}
2022-06-21 02:52:09 +00:00
int32_t nullFlagSize = sizeof ( int8_t ) * LIST_LENGTH ( group ) ;
2022-06-13 11:59:30 +00:00
keyLen + = nullFlagSize ;
keyBuf = taosMemoryCalloc ( 1 , keyLen ) ;
if ( keyBuf = = NULL ) {
return TSDB_CODE_OUT_OF_MEMORY ;
}
2022-06-22 10:14:26 +00:00
int32_t groupNum = 0 ;
2022-06-15 11:08:25 +00:00
for ( int32_t i = 0 ; i < taosArrayGetSize ( pTableListInfo - > pTableList ) ; i + + ) {
STableKeyInfo * info = taosArrayGet ( pTableListInfo - > pTableList , i ) ;
SMetaReader mr = { 0 } ;
2022-06-13 11:59:30 +00:00
metaReaderInit ( & mr , pHandle - > meta , 0 ) ;
metaGetTableEntryByUid ( & mr , info - > uid ) ;
2022-06-28 05:59:49 +00:00
SNodeList * groupNew = nodesCloneList ( group ) ;
2022-06-21 02:52:09 +00:00
2022-06-23 06:26:26 +00:00
nodesRewriteExprsPostOrder ( groupNew , doTranslateTagExpr , & mr ) ;
2022-06-13 11:59:30 +00:00
char * isNull = ( char * ) keyBuf ;
2022-06-21 02:52:09 +00:00
char * pStart = ( char * ) keyBuf + nullFlagSize ;
2022-06-28 05:59:49 +00:00
SNode * pNode ;
2022-06-21 02:52:09 +00:00
int32_t index = 0 ;
2022-06-28 05:59:49 +00:00
FOREACH ( pNode , groupNew ) {
2022-06-21 02:52:09 +00:00
SNode * pNew = NULL ;
int32_t code = scalarCalculateConstants ( pNode , & pNew ) ;
if ( TSDB_CODE_SUCCESS = = code ) {
REPLACE_NODE ( pNew ) ;
2022-06-15 11:08:25 +00:00
} else {
2022-06-22 10:14:26 +00:00
taosMemoryFree ( keyBuf ) ;
2022-06-21 02:52:09 +00:00
nodesClearList ( groupNew ) ;
2022-06-30 09:10:34 +00:00
metaReaderClear ( & mr ) ;
2022-06-21 02:52:09 +00:00
return code ;
2022-06-13 11:59:30 +00:00
}
2022-06-22 10:51:16 +00:00
2022-06-21 02:52:09 +00:00
ASSERT ( nodeType ( pNew ) = = QUERY_NODE_VALUE ) ;
2022-06-28 05:59:49 +00:00
SValueNode * pValue = ( SValueNode * ) pNew ;
2022-06-22 10:51:16 +00:00
2022-07-02 06:47:07 +00:00
if ( pValue - > node . resType . type = = TSDB_DATA_TYPE_NULL | | pValue - > isNull ) {
2022-06-21 02:52:09 +00:00
isNull [ index + + ] = 1 ;
continue ;
} else {
isNull [ index + + ] = 0 ;
2022-06-28 05:59:49 +00:00
char * data = nodesGetValueFromNode ( pValue ) ;
2022-07-02 12:18:40 +00:00
if ( pValue - > node . resType . type = = TSDB_DATA_TYPE_JSON ) {
if ( tTagIsJson ( data ) ) {
2022-06-27 08:17:58 +00:00
terrno = TSDB_CODE_QRY_JSON_IN_GROUP_ERROR ;
taosMemoryFree ( keyBuf ) ;
nodesClearList ( groupNew ) ;
2022-06-30 09:10:34 +00:00
metaReaderClear ( & mr ) ;
2022-06-27 08:17:58 +00:00
return terrno ;
}
2022-06-24 07:57:33 +00:00
int32_t len = getJsonValueLen ( data ) ;
2022-06-21 05:52:07 +00:00
memcpy ( pStart , data , len ) ;
pStart + = len ;
} else if ( IS_VAR_DATA_TYPE ( pValue - > node . resType . type ) ) {
2022-06-21 02:52:09 +00:00
memcpy ( pStart , data , varDataTLen ( data ) ) ;
pStart + = varDataTLen ( data ) ;
2022-06-13 11:59:30 +00:00
} else {
2022-06-21 02:52:09 +00:00
memcpy ( pStart , data , pValue - > node . resType . bytes ) ;
pStart + = pValue - > node . resType . bytes ;
2022-06-13 11:59:30 +00:00
}
}
}
2022-06-28 05:59:49 +00:00
int32_t len = ( int32_t ) ( pStart - ( char * ) keyBuf ) ;
2022-06-22 10:14:26 +00:00
uint64_t groupId = calcGroupId ( keyBuf , len ) ;
taosHashPut ( pTableListInfo - > map , & ( info - > uid ) , sizeof ( uint64_t ) , & groupId , sizeof ( uint64_t ) ) ;
2022-06-25 05:52:52 +00:00
info - > groupId = groupId ;
2022-06-22 10:14:26 +00:00
groupNum + + ;
2022-06-13 11:59:30 +00:00
2022-06-21 02:52:09 +00:00
nodesClearList ( groupNew ) ;
2022-06-13 11:59:30 +00:00
metaReaderClear ( & mr ) ;
}
taosMemoryFree ( keyBuf ) ;
2022-06-22 10:14:26 +00:00
2022-06-28 05:59:49 +00:00
if ( pTableListInfo - > needSortTableByGroupId ) {
2022-06-24 07:57:33 +00:00
return sortTableGroup ( pTableListInfo , groupNum ) ;
2022-06-22 10:14:26 +00:00
}
2022-06-13 11:59:30 +00:00
return TDB_CODE_SUCCESS ;
}
2022-03-29 12:05:04 +00:00
SOperatorInfo * createOperatorTree ( SPhysiNode * pPhyNode , SExecTaskInfo * pTaskInfo , SReadHandle * pHandle ,
2022-06-28 05:59:49 +00:00
uint64_t queryId , uint64_t taskId , STableListInfo * pTableListInfo ,
const char * pUser ) {
2022-04-16 02:00:25 +00:00
int32_t type = nodeType ( pPhyNode ) ;
2022-02-28 09:02:43 +00:00
if ( pPhyNode - > pChildren = = NULL | | LIST_LENGTH ( pPhyNode - > pChildren ) = = 0 ) {
2022-04-08 02:24:35 +00:00
if ( QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN = = type ) {
2022-04-23 10:29:45 +00:00
STableScanPhysiNode * pTableScanNode = ( STableScanPhysiNode * ) pPhyNode ;
2022-01-20 08:02:09 +00:00
2022-06-23 11:58:12 +00:00
int32_t code = createScanTableListInfo ( pTableScanNode , pHandle , pTableListInfo , queryId , taskId ) ;
2022-06-28 05:59:49 +00:00
if ( code ) {
2022-06-24 05:51:33 +00:00
pTaskInfo - > code = code ;
2022-06-10 09:07:24 +00:00
return NULL ;
}
2022-06-13 11:59:30 +00:00
2022-06-23 11:58:12 +00:00
code = extractTableSchemaVersion ( pHandle , pTableScanNode - > scan . uid , pTaskInfo ) ;
2022-06-15 08:40:43 +00:00
if ( code ) {
2022-06-16 08:21:01 +00:00
pTaskInfo - > code = terrno ;
2022-06-13 11:59:30 +00:00
return NULL ;
}
2022-06-28 07:22:32 +00:00
SOperatorInfo * pOperator = createTableScanOperatorInfo ( pTableScanNode , pHandle , pTaskInfo ) ;
2022-05-03 15:52:17 +00:00
STableScanInfo * pScanInfo = pOperator - > info ;
pTaskInfo - > cost . pRecoder = & pScanInfo - > readRecorder ;
2022-06-15 08:40:43 +00:00
return pOperator ;
2022-06-23 14:05:00 +00:00
2022-06-15 08:40:43 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN = = type ) {
STableMergeScanPhysiNode * pTableScanNode = ( STableMergeScanPhysiNode * ) pPhyNode ;
2022-06-23 11:58:12 +00:00
int32_t code = createScanTableListInfo ( pTableScanNode , pHandle , pTableListInfo , queryId , taskId ) ;
2022-07-02 12:18:40 +00:00
if ( code ) {
2022-06-27 08:17:58 +00:00
pTaskInfo - > code = code ;
2022-06-23 11:58:12 +00:00
return NULL ;
}
code = extractTableSchemaVersion ( pHandle , pTableScanNode - > scan . uid , pTaskInfo ) ;
if ( code ) {
pTaskInfo - > code = terrno ;
return NULL ;
}
2022-06-23 06:26:26 +00:00
2022-06-28 05:59:49 +00:00
SOperatorInfo * pOperator =
createTableMergeScanOperatorInfo ( pTableScanNode , pTableListInfo , pHandle , pTaskInfo , queryId , taskId ) ;
2022-06-24 05:51:33 +00:00
2022-05-03 15:52:17 +00:00
STableScanInfo * pScanInfo = pOperator - > info ;
pTaskInfo - > cost . pRecoder = & pScanInfo - > readRecorder ;
return pOperator ;
2022-06-23 14:05:00 +00:00
2022-04-08 02:24:35 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_EXCHANGE = = type ) {
2022-06-05 06:48:15 +00:00
return createExchangeOperatorInfo ( pHandle - > pMsgCb - > clientRpc , ( SExchangePhysiNode * ) pPhyNode , pTaskInfo ) ;
2022-06-23 14:05:00 +00:00
2022-04-08 02:24:35 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN = = type ) {
2022-05-10 07:34:41 +00:00
STableScanPhysiNode * pTableScanNode = ( STableScanPhysiNode * ) pPhyNode ;
2022-05-30 11:08:14 +00:00
STimeWindowAggSupp twSup = {
2022-06-20 11:53:48 +00:00
. waterMark = pTableScanNode - > watermark ,
. calTrigger = pTableScanNode - > triggerType ,
. maxTs = INT64_MIN ,
} ;
2022-06-20 06:29:18 +00:00
if ( pHandle ) {
2022-06-27 08:17:58 +00:00
int32_t code = createScanTableListInfo ( pTableScanNode , pHandle , pTableListInfo , queryId , taskId ) ;
2022-07-02 12:18:40 +00:00
if ( code ) {
2022-06-27 08:17:58 +00:00
pTaskInfo - > code = code ;
return NULL ;
}
2022-05-10 07:34:41 +00:00
}
2022-03-30 11:38:30 +00:00
2022-06-28 05:59:49 +00:00
SOperatorInfo * pOperator =
createStreamScanOperatorInfo ( pHandle , pTableScanNode , pTaskInfo , & twSup , queryId , taskId ) ;
2022-01-27 10:48:12 +00:00
return pOperator ;
2022-06-23 14:05:00 +00:00
2022-04-08 02:24:35 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN = = type ) {
2022-03-29 07:24:25 +00:00
SSystemTableScanPhysiNode * pSysScanPhyNode = ( SSystemTableScanPhysiNode * ) pPhyNode ;
2022-06-25 04:10:34 +00:00
return createSysTableScanOperatorInfo ( pHandle , pSysScanPhyNode , pUser , pTaskInfo ) ;
2022-05-05 15:47:44 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN = = type ) {
2022-05-10 00:55:32 +00:00
STagScanPhysiNode * pScanPhyNode = ( STagScanPhysiNode * ) pPhyNode ;
2022-05-07 03:04:38 +00:00
2022-06-29 09:14:00 +00:00
int32_t code = getTableList ( pHandle - > meta , pHandle - > vnode , pScanPhyNode , pTableListInfo ) ;
2022-05-05 15:47:44 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2022-06-17 07:48:32 +00:00
pTaskInfo - > code = terrno ;
2022-05-05 15:47:44 +00:00
return NULL ;
}
2022-06-05 06:48:15 +00:00
return createTagScanOperatorInfo ( pHandle , pScanPhyNode , pTableListInfo , pTaskInfo ) ;
2022-06-20 04:54:46 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN = = type ) {
2022-06-22 05:08:20 +00:00
SBlockDistScanPhysiNode * pBlockNode = ( SBlockDistScanPhysiNode * ) pPhyNode ;
2022-06-20 04:54:46 +00:00
pTableListInfo - > pTableList = taosArrayInit ( 4 , sizeof ( STableKeyInfo ) ) ;
if ( pBlockNode - > tableType = = TSDB_SUPER_TABLE ) {
2022-06-29 09:14:00 +00:00
int32_t code = vnodeGetAllTableList ( pHandle - > vnode , pBlockNode - > uid , pTableListInfo - > pTableList ) ;
2022-06-20 04:54:46 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
pTaskInfo - > code = terrno ;
return NULL ;
}
} else { // Create one table group.
2022-06-25 05:52:52 +00:00
STableKeyInfo info = { . lastKey = 0 , . uid = pBlockNode - > uid , . groupId = 0 } ;
2022-06-20 04:54:46 +00:00
taosArrayPush ( pTableListInfo - > pTableList , & info ) ;
}
SQueryTableDataCond cond = { 0 } ;
{
cond . order = TSDB_ORDER_ASC ;
cond . numOfCols = 1 ;
cond . colList = taosMemoryCalloc ( 1 , sizeof ( SColumnInfo ) ) ;
if ( cond . colList = = NULL ) {
terrno = TSDB_CODE_QRY_OUT_OF_MEMORY ;
return NULL ;
}
cond . colList - > colId = 1 ;
cond . colList - > type = TSDB_DATA_TYPE_TIMESTAMP ;
cond . colList - > bytes = sizeof ( TSKEY ) ;
cond . numOfTWindows = 1 ;
cond . twindows = taosMemoryCalloc ( 1 , sizeof ( STimeWindow ) ) ;
cond . twindows [ 0 ] = ( STimeWindow ) { . skey = INT64_MIN , . ekey = INT64_MAX } ;
cond . suid = pBlockNode - > suid ;
2022-06-28 07:22:32 +00:00
cond . type = BLOCK_LOAD_OFFSET_ORDER ;
2022-06-20 04:54:46 +00:00
}
2022-06-28 07:22:32 +00:00
STsdbReader * pReader = NULL ;
tsdbReaderOpen ( pHandle - > vnode , & cond , pTableListInfo - > pTableList , & pReader , " " ) ;
2022-06-20 04:54:46 +00:00
cleanupQueryTableDataCond ( & cond ) ;
2022-06-20 05:42:59 +00:00
return createDataBlockInfoScanOperator ( pReader , pHandle , cond . suid , pBlockNode , pTaskInfo ) ;
2022-06-28 07:22:32 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN = = type ) {
SLastRowScanPhysiNode * pScanNode = ( SLastRowScanPhysiNode * ) pPhyNode ;
// int32_t code = createScanTableListInfo(pTableScanNode, pHandle, pTableListInfo, queryId, taskId);
// if (code) {
// pTaskInfo->code = code;
// return NULL;
// }
2022-06-28 15:24:20 +00:00
int32_t code = extractTableSchemaVersion ( pHandle , pScanNode - > uid , pTaskInfo ) ;
2022-07-06 05:33:21 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
pTaskInfo - > code = code ;
return NULL ;
}
2022-06-28 15:24:20 +00:00
2022-06-28 07:38:54 +00:00
pTableListInfo - > pTableList = taosArrayInit ( 4 , sizeof ( STableKeyInfo ) ) ;
2022-06-28 07:35:51 +00:00
if ( pScanNode - > tableType = = TSDB_SUPER_TABLE ) {
2022-06-29 09:14:00 +00:00
code = vnodeGetAllTableList ( pHandle - > vnode , pScanNode - > uid , pTableListInfo - > pTableList ) ;
2022-06-28 07:35:51 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
pTaskInfo - > code = terrno ;
return NULL ;
}
} else { // Create one table group.
STableKeyInfo info = { . lastKey = 0 , . uid = pScanNode - > uid , . groupId = 0 } ;
taosArrayPush ( pTableListInfo - > pTableList , & info ) ;
}
2022-06-28 07:22:32 +00:00
return createLastrowScanOperator ( pScanNode , pHandle , pTableListInfo - > pTableList , pTaskInfo ) ;
2022-03-11 11:29:43 +00:00
} else {
ASSERT ( 0 ) ;
2022-01-20 05:52:46 +00:00
}
}
2022-04-14 14:12:10 +00:00
int32_t num = 0 ;
size_t size = LIST_LENGTH ( pPhyNode - > pChildren ) ;
2022-03-12 14:59:12 +00:00
2022-04-14 14:12:10 +00:00
SOperatorInfo * * ops = taosMemoryCalloc ( size , POINTER_BYTES ) ;
2022-04-23 10:29:45 +00:00
for ( int32_t i = 0 ; i < size ; + + i ) {
2022-04-14 14:12:10 +00:00
SPhysiNode * pChildNode = ( SPhysiNode * ) nodesListGetNode ( pPhyNode - > pChildren , i ) ;
2022-06-25 04:10:34 +00:00
ops [ i ] = createOperatorTree ( pChildNode , pTaskInfo , pHandle , queryId , taskId , pTableListInfo , pUser ) ;
2022-04-25 02:22:48 +00:00
if ( ops [ i ] = = NULL ) {
return NULL ;
}
2022-04-14 14:12:10 +00:00
}
2022-03-12 14:59:12 +00:00
2022-04-15 04:09:27 +00:00
SOperatorInfo * pOptr = NULL ;
2022-04-08 02:24:35 +00:00
if ( QUERY_NODE_PHYSICAL_PLAN_PROJECT = = type ) {
2022-06-17 11:01:45 +00:00
pOptr = createProjectOperatorInfo ( ops [ 0 ] , ( SProjectPhysiNode * ) pPhyNode , pTaskInfo ) ;
2022-06-07 12:59:44 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_HASH_AGG = = type ) {
2022-04-08 02:24:35 +00:00
SAggPhysiNode * pAggNode = ( SAggPhysiNode * ) pPhyNode ;
SExprInfo * pExprInfo = createExprInfo ( pAggNode - > pAggFuncs , pAggNode - > pGroupKeys , & num ) ;
2022-04-15 10:06:49 +00:00
SSDataBlock * pResBlock = createResDataBlock ( pPhyNode - > pOutputDataBlockDesc ) ;
2022-03-09 02:22:53 +00:00
2022-04-23 10:29:45 +00:00
int32_t numOfScalarExpr = 0 ;
2022-04-16 02:00:25 +00:00
SExprInfo * pScalarExprInfo = NULL ;
if ( pAggNode - > pExprs ! = NULL ) {
pScalarExprInfo = createExprInfo ( pAggNode - > pExprs , NULL , & numOfScalarExpr ) ;
}
2022-04-08 02:24:35 +00:00
if ( pAggNode - > pGroupKeys ! = NULL ) {
SArray * pColList = extractColumnInfo ( pAggNode - > pGroupKeys ) ;
2022-04-23 10:29:45 +00:00
pOptr = createGroupOperatorInfo ( ops [ 0 ] , pExprInfo , num , pResBlock , pColList , pAggNode - > node . pConditions ,
2022-05-26 08:05:27 +00:00
pScalarExprInfo , numOfScalarExpr , pTaskInfo ) ;
2022-04-08 02:24:35 +00:00
} else {
2022-05-27 11:54:22 +00:00
pOptr =
2022-07-06 05:54:11 +00:00
createAggregateOperatorInfo ( ops [ 0 ] , pExprInfo , num , pResBlock , pAggNode - > node . pConditions , pScalarExprInfo , numOfScalarExpr , pTaskInfo ) ;
2022-03-14 08:09:26 +00:00
}
2022-06-05 07:48:06 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL = = type | | QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL = = type ) {
2022-04-08 02:24:35 +00:00
SIntervalPhysiNode * pIntervalPhyNode = ( SIntervalPhysiNode * ) pPhyNode ;
2022-03-28 11:08:07 +00:00
2022-04-08 02:24:35 +00:00
SExprInfo * pExprInfo = createExprInfo ( pIntervalPhyNode - > window . pFuncs , NULL , & num ) ;
2022-04-15 10:06:49 +00:00
SSDataBlock * pResBlock = createResDataBlock ( pPhyNode - > pOutputDataBlockDesc ) ;
2022-03-28 11:08:07 +00:00
2022-04-23 10:29:45 +00:00
SInterval interval = { . interval = pIntervalPhyNode - > interval ,
. sliding = pIntervalPhyNode - > sliding ,
. intervalUnit = pIntervalPhyNode - > intervalUnit ,
. slidingUnit = pIntervalPhyNode - > slidingUnit ,
. offset = pIntervalPhyNode - > offset ,
. precision = ( ( SColumnNode * ) pIntervalPhyNode - > window . pTspk ) - > node . resType . precision } ;
2022-04-08 02:24:35 +00:00
2022-06-04 11:54:55 +00:00
STimeWindowAggSupp as = {
. waterMark = pIntervalPhyNode - > window . watermark ,
. calTrigger = pIntervalPhyNode - > window . triggerType ,
. maxTs = INT64_MIN ,
} ;
2022-06-15 08:40:45 +00:00
ASSERT ( as . calTrigger ! = STREAM_TRIGGER_MAX_DELAY ) ;
2022-04-19 02:12:30 +00:00
2022-05-03 07:04:34 +00:00
int32_t tsSlotId = ( ( SColumnNode * ) pIntervalPhyNode - > window . pTspk ) - > slotId ;
2022-06-10 07:20:57 +00:00
bool isStream = ( QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL = = type ) ;
2022-06-27 02:07:48 +00:00
pOptr = createIntervalOperatorInfo ( ops [ 0 ] , pExprInfo , num , pResBlock , & interval , tsSlotId , & as , pIntervalPhyNode ,
pTaskInfo , isStream ) ;
2022-05-03 07:04:34 +00:00
2022-06-22 02:53:22 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL = = type ) {
SMergeAlignedIntervalPhysiNode * pIntervalPhyNode = ( SMergeAlignedIntervalPhysiNode * ) pPhyNode ;
2022-06-10 03:09:31 +00:00
SExprInfo * pExprInfo = createExprInfo ( pIntervalPhyNode - > window . pFuncs , NULL , & num ) ;
SSDataBlock * pResBlock = createResDataBlock ( pPhyNode - > pOutputDataBlockDesc ) ;
SInterval interval = { . interval = pIntervalPhyNode - > interval ,
. sliding = pIntervalPhyNode - > sliding ,
. intervalUnit = pIntervalPhyNode - > intervalUnit ,
. slidingUnit = pIntervalPhyNode - > slidingUnit ,
. offset = pIntervalPhyNode - > offset ,
. precision = ( ( SColumnNode * ) pIntervalPhyNode - > window . pTspk ) - > node . resType . precision } ;
2022-05-03 07:04:34 +00:00
2022-06-10 03:09:31 +00:00
int32_t tsSlotId = ( ( SColumnNode * ) pIntervalPhyNode - > window . pTspk ) - > slotId ;
2022-06-22 02:53:22 +00:00
pOptr = createMergeAlignedIntervalOperatorInfo ( ops [ 0 ] , pExprInfo , num , pResBlock , & interval , tsSlotId , pTaskInfo ) ;
2022-06-10 03:09:31 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_MERGE_INTERVAL = = type ) {
2022-06-15 11:08:25 +00:00
SMergeIntervalPhysiNode * pIntervalPhyNode = ( SMergeIntervalPhysiNode * ) pPhyNode ;
2022-06-10 03:09:31 +00:00
SExprInfo * pExprInfo = createExprInfo ( pIntervalPhyNode - > window . pFuncs , NULL , & num ) ;
SSDataBlock * pResBlock = createResDataBlock ( pPhyNode - > pOutputDataBlockDesc ) ;
SInterval interval = { . interval = pIntervalPhyNode - > interval ,
. sliding = pIntervalPhyNode - > sliding ,
. intervalUnit = pIntervalPhyNode - > intervalUnit ,
. slidingUnit = pIntervalPhyNode - > slidingUnit ,
. offset = pIntervalPhyNode - > offset ,
. precision = ( ( SColumnNode * ) pIntervalPhyNode - > window . pTspk ) - > node . resType . precision } ;
2022-05-03 07:04:34 +00:00
2022-06-10 03:09:31 +00:00
int32_t tsSlotId = ( ( SColumnNode * ) pIntervalPhyNode - > window . pTspk ) - > slotId ;
pOptr = createMergeIntervalOperatorInfo ( ops [ 0 ] , pExprInfo , num , pResBlock , & interval , tsSlotId , pTaskInfo ) ;
2022-06-06 05:23:04 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL = = type ) {
2022-06-10 07:54:20 +00:00
int32_t children = 0 ;
2022-06-06 05:23:04 +00:00
pOptr = createStreamFinalIntervalOperatorInfo ( ops [ 0 ] , pPhyNode , pTaskInfo , children ) ;
} else if ( QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL = = type ) {
2022-06-10 07:54:20 +00:00
int32_t children = 1 ;
2022-06-06 05:23:04 +00:00
pOptr = createStreamFinalIntervalOperatorInfo ( ops [ 0 ] , pPhyNode , pTaskInfo , children ) ;
2022-04-08 02:24:35 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_SORT = = type ) {
2022-06-17 11:01:45 +00:00
pOptr = createSortOperatorInfo ( ops [ 0 ] , ( SSortPhysiNode * ) pPhyNode , pTaskInfo ) ;
2022-06-22 15:01:55 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_GROUP_SORT = = type ) {
pOptr = createGroupSortOperatorInfo ( ops [ 0 ] , ( SGroupSortPhysiNode * ) pPhyNode , pTaskInfo ) ;
2022-06-06 10:07:34 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_MERGE = = type ) {
2022-06-06 06:37:00 +00:00
SMergePhysiNode * pMergePhyNode = ( SMergePhysiNode * ) pPhyNode ;
2022-06-27 09:51:50 +00:00
pOptr = createMultiwayMergeOperatorInfo ( ops , size , pMergePhyNode , pTaskInfo ) ;
2022-06-07 12:59:44 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_MERGE_SESSION = = type ) {
2022-03-28 12:11:02 +00:00
SSessionWinodwPhysiNode * pSessionNode = ( SSessionWinodwPhysiNode * ) pPhyNode ;
2022-05-10 00:55:32 +00:00
STimeWindowAggSupp as = { . waterMark = pSessionNode - > window . watermark ,
. calTrigger = pSessionNode - > window . triggerType } ;
2022-05-03 07:04:34 +00:00
2022-03-28 12:11:02 +00:00
SExprInfo * pExprInfo = createExprInfo ( pSessionNode - > window . pFuncs , NULL , & num ) ;
2022-04-15 10:06:49 +00:00
SSDataBlock * pResBlock = createResDataBlock ( pPhyNode - > pOutputDataBlockDesc ) ;
2022-05-03 07:33:24 +00:00
int32_t tsSlotId = ( ( SColumnNode * ) pSessionNode - > window . pTspk ) - > slotId ;
2022-05-10 00:55:32 +00:00
pOptr =
2022-07-07 01:29:51 +00:00
createSessionAggOperatorInfo ( ops [ 0 ] , pExprInfo , num , pResBlock , pSessionNode - > gap , tsSlotId , & as , pPhyNode - > pConditions , pTaskInfo ) ;
2022-06-07 12:59:44 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION = = type ) {
2022-06-17 06:13:45 +00:00
pOptr = createStreamSessionAggOperatorInfo ( ops [ 0 ] , pPhyNode , pTaskInfo ) ;
} else if ( QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION = = type ) {
int32_t children = 0 ;
pOptr = createStreamFinalSessionAggOperatorInfo ( ops [ 0 ] , pPhyNode , pTaskInfo , children ) ;
} else if ( QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION = = type ) {
int32_t children = 1 ;
pOptr = createStreamFinalSessionAggOperatorInfo ( ops [ 0 ] , pPhyNode , pTaskInfo , children ) ;
2022-04-08 02:24:35 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_PARTITION = = type ) {
2022-06-15 15:00:31 +00:00
pOptr = createPartitionOperatorInfo ( ops [ 0 ] , ( SPartitionPhysiNode * ) pPhyNode , pTaskInfo ) ;
2022-06-07 12:59:44 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_MERGE_STATE = = type ) {
2022-04-23 10:29:45 +00:00
SStateWinodwPhysiNode * pStateNode = ( SStateWinodwPhysiNode * ) pPhyNode ;
2022-04-09 07:01:28 +00:00
2022-04-19 02:12:30 +00:00
STimeWindowAggSupp as = { . waterMark = pStateNode - > window . watermark , . calTrigger = pStateNode - > window . triggerType } ;
2022-04-23 10:29:45 +00:00
SExprInfo * pExprInfo = createExprInfo ( pStateNode - > window . pFuncs , NULL , & num ) ;
2022-04-15 10:06:49 +00:00
SSDataBlock * pResBlock = createResDataBlock ( pPhyNode - > pOutputDataBlockDesc ) ;
2022-05-03 07:33:24 +00:00
int32_t tsSlotId = ( ( SColumnNode * ) pStateNode - > window . pTspk ) - > slotId ;
2022-05-13 10:18:54 +00:00
SColumnNode * pColNode = ( SColumnNode * ) ( ( STargetNode * ) pStateNode - > pStateKey ) - > pExpr ;
2022-05-23 11:50:08 +00:00
SColumn col = extractColumnFromColumnNode ( pColNode ) ;
2022-07-07 01:29:51 +00:00
pOptr = createStatewindowOperatorInfo ( ops [ 0 ] , pExprInfo , num , pResBlock , & as , tsSlotId , & col , pPhyNode - > pConditions , pTaskInfo ) ;
2022-06-07 12:59:44 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE = = type ) {
2022-06-04 12:06:07 +00:00
pOptr = createStreamStateAggOperatorInfo ( ops [ 0 ] , pPhyNode , pTaskInfo ) ;
2022-06-07 12:59:44 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN = = type ) {
2022-06-17 11:01:45 +00:00
pOptr = createMergeJoinOperatorInfo ( ops , size , ( SJoinPhysiNode * ) pPhyNode , pTaskInfo ) ;
2022-05-02 15:52:32 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_FILL = = type ) {
2022-06-17 07:48:32 +00:00
pOptr = createFillOperatorInfo ( ops [ 0 ] , ( SFillPhysiNode * ) pPhyNode , false , pTaskInfo ) ;
2022-06-04 11:19:49 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_INDEF_ROWS_FUNC = = type ) {
pOptr = createIndefinitOutputOperatorInfo ( ops [ 0 ] , pPhyNode , pTaskInfo ) ;
2022-06-20 15:22:28 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC = = type ) {
pOptr = createTimeSliceOperatorInfo ( ops [ 0 ] , pPhyNode , pTaskInfo ) ;
2022-03-28 11:08:07 +00:00
} else {
ASSERT ( 0 ) ;
2022-04-16 11:10:21 +00:00
}
2022-04-15 04:09:27 +00:00
taosMemoryFree ( ops ) ;
return pOptr ;
2022-01-10 11:48:21 +00:00
}
2022-01-08 14:59:24 +00:00
2022-05-27 09:57:27 +00:00
int32_t compareTimeWindow ( const void * p1 , const void * p2 , const void * param ) {
2022-05-28 13:10:09 +00:00
const SQueryTableDataCond * pCond = param ;
const STimeWindow * pWin1 = p1 ;
const STimeWindow * pWin2 = p2 ;
2022-05-27 09:57:27 +00:00
if ( pCond - > order = = TSDB_ORDER_ASC ) {
return pWin1 - > skey - pWin2 - > skey ;
} else if ( pCond - > order = = TSDB_ORDER_DESC ) {
return pWin2 - > skey - pWin1 - > skey ;
}
return 0 ;
}
2022-03-22 08:03:42 +00:00
SArray * extractColumnInfo ( SNodeList * pNodeList ) {
2022-03-29 07:24:25 +00:00
size_t numOfCols = LIST_LENGTH ( pNodeList ) ;
2022-03-22 08:03:42 +00:00
SArray * pList = taosArrayInit ( numOfCols , sizeof ( SColumn ) ) ;
if ( pList = = NULL ) {
terrno = TSDB_CODE_OUT_OF_MEMORY ;
return NULL ;
}
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < numOfCols ; + + i ) {
STargetNode * pNode = ( STargetNode * ) nodesListGetNode ( pNodeList , i ) ;
2022-03-22 08:03:42 +00:00
2022-04-27 02:11:32 +00:00
if ( nodeType ( pNode - > pExpr ) = = QUERY_NODE_COLUMN ) {
SColumnNode * pColNode = ( SColumnNode * ) pNode - > pExpr ;
2022-05-13 10:18:54 +00:00
SColumn c = extractColumnFromColumnNode ( pColNode ) ;
2022-04-27 02:11:32 +00:00
taosArrayPush ( pList , & c ) ;
} else if ( nodeType ( pNode - > pExpr ) = = QUERY_NODE_VALUE ) {
2022-04-28 08:31:35 +00:00
SValueNode * pValNode = ( SValueNode * ) pNode - > pExpr ;
SColumn c = { 0 } ;
2022-04-27 02:11:32 +00:00
c . slotId = pNode - > slotId ;
2022-04-28 08:31:35 +00:00
c . colId = pNode - > slotId ;
c . type = pValNode - > node . type ;
c . bytes = pValNode - > node . resType . bytes ;
c . scale = pValNode - > node . resType . scale ;
2022-04-27 02:11:32 +00:00
c . precision = pValNode - > node . resType . precision ;
taosArrayPush ( pList , & c ) ;
}
2022-03-22 08:03:42 +00:00
}
return pList ;
}
2022-06-28 07:22:32 +00:00
STsdbReader * doCreateDataReader ( STableScanPhysiNode * pTableScanNode , SReadHandle * pHandle , STableListInfo * pTableListInfo , const char * idstr ) {
2022-06-29 09:14:00 +00:00
int32_t code = getTableList ( pHandle - > meta , pHandle - > vnode , & pTableScanNode - > scan , pTableListInfo ) ;
2022-05-26 15:05:06 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
}
if ( taosArrayGetSize ( pTableListInfo - > pTableList ) = = 0 ) {
code = 0 ;
2022-06-28 07:22:32 +00:00
qDebug ( " no table qualified for query, %s " , idstr ) ;
2022-05-26 15:05:06 +00:00
goto _error ;
}
2022-04-26 05:09:29 +00:00
SQueryTableDataCond cond = { 0 } ;
2022-05-26 15:05:06 +00:00
code = initQueryTableDataCond ( & cond , pTableScanNode ) ;
2022-04-26 05:09:29 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2022-05-26 15:05:06 +00:00
goto _error ;
2022-04-30 03:40:12 +00:00
}
2022-05-05 15:47:44 +00:00
2022-06-18 02:09:14 +00:00
STsdbReader * pReader ;
2022-06-28 07:22:32 +00:00
code = tsdbReaderOpen ( pHandle - > vnode , & cond , pTableListInfo - > pTableList , & pReader , idstr ) ;
2022-06-27 02:46:20 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
}
2022-06-17 11:01:45 +00:00
cleanupQueryTableDataCond ( & cond ) ;
2022-06-12 15:37:29 +00:00
return pReader ;
2022-05-26 15:05:06 +00:00
_error :
terrno = code ;
return NULL ;
2022-01-20 08:02:09 +00:00
}
2022-06-24 08:50:11 +00:00
static int32_t extractTbscanInStreamOpTree ( SOperatorInfo * pOperator , STableScanInfo * * ppInfo ) {
if ( pOperator - > operatorType ! = QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN ) {
if ( pOperator - > numOfDownstream = = 0 ) {
qError ( " failed to find stream scan operator " ) ;
return TSDB_CODE_QRY_APP_ERROR ;
}
if ( pOperator - > numOfDownstream > 1 ) {
qError ( " join not supported for stream block scan " ) ;
return TSDB_CODE_QRY_APP_ERROR ;
}
return extractTbscanInStreamOpTree ( pOperator - > pDownstream [ 0 ] , ppInfo ) ;
} else {
2022-07-06 06:20:07 +00:00
SStreamScanInfo * pInfo = pOperator - > info ;
ASSERT ( pInfo - > pTableScanOp - > operatorType = = QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN ) ;
* ppInfo = pInfo - > pTableScanOp - > info ;
2022-06-24 08:50:11 +00:00
return 0 ;
}
}
2022-06-24 06:07:52 +00:00
int32_t extractTableScanNode ( SPhysiNode * pNode , STableScanPhysiNode * * ppNode ) {
if ( pNode - > pChildren = = NULL | | LIST_LENGTH ( pNode - > pChildren ) = = 0 ) {
if ( QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN = = pNode - > type ) {
* ppNode = ( STableScanPhysiNode * ) pNode ;
return 0 ;
} else {
ASSERT ( 0 ) ;
terrno = TSDB_CODE_QRY_APP_ERROR ;
return - 1 ;
}
} else {
if ( LIST_LENGTH ( pNode - > pChildren ) ! = 1 ) {
ASSERT ( 0 ) ;
terrno = TSDB_CODE_QRY_APP_ERROR ;
return - 1 ;
}
SPhysiNode * pChildNode = ( SPhysiNode * ) nodesListGetNode ( pNode - > pChildren , 0 ) ;
return extractTableScanNode ( pChildNode , ppNode ) ;
}
return - 1 ;
}
2022-06-24 08:50:11 +00:00
int32_t rebuildReader ( SOperatorInfo * pOperator , SSubplan * plan , SReadHandle * pHandle , int64_t uid , int64_t ts ) {
STableScanInfo * pTableScanInfo = NULL ;
if ( extractTbscanInStreamOpTree ( pOperator , & pTableScanInfo ) < 0 ) {
return - 1 ;
}
2022-06-24 06:07:52 +00:00
2022-06-24 08:50:11 +00:00
STableScanPhysiNode * pNode = NULL ;
if ( extractTableScanNode ( plan - > pNode , & pNode ) < 0 ) {
ASSERT ( 0 ) ;
}
2022-06-24 06:07:52 +00:00
2022-06-28 07:22:32 +00:00
tsdbReaderClose ( pTableScanInfo - > dataReader ) ;
2022-06-24 06:07:52 +00:00
2022-06-24 08:50:11 +00:00
STableListInfo info = { 0 } ;
2022-06-28 07:22:32 +00:00
pTableScanInfo - > dataReader = doCreateDataReader ( pNode , pHandle , & info , NULL ) ;
2022-06-24 08:50:11 +00:00
if ( pTableScanInfo - > dataReader = = NULL ) {
ASSERT ( 0 ) ;
qError ( " failed to create data reader " ) ;
return TSDB_CODE_QRY_APP_ERROR ;
2022-06-24 06:07:52 +00:00
}
2022-06-24 08:50:11 +00:00
// TODO: set uid and ts to data reader
2022-06-24 06:07:52 +00:00
return 0 ;
}
2022-06-27 06:47:14 +00:00
int32_t encodeOperator ( SOperatorInfo * ops , char * * result , int32_t * length , int32_t * nOptrWithVal ) {
2022-05-28 15:26:22 +00:00
int32_t code = TDB_CODE_SUCCESS ;
2022-05-30 11:08:14 +00:00
char * pCurrent = NULL ;
2022-05-28 15:26:22 +00:00
int32_t currLength = 0 ;
2022-05-30 11:08:14 +00:00
if ( ops - > fpSet . encodeResultRow ) {
2022-06-27 06:47:14 +00:00
if ( result = = NULL | | length = = NULL | | nOptrWithVal = = NULL ) {
2022-05-28 15:26:22 +00:00
return TSDB_CODE_TSC_INVALID_INPUT ;
}
code = ops - > fpSet . encodeResultRow ( ops , & pCurrent , & currLength ) ;
2022-05-27 15:21:41 +00:00
2022-05-30 11:08:14 +00:00
if ( code ! = TDB_CODE_SUCCESS ) {
if ( * result ! = NULL ) {
2022-05-28 15:26:22 +00:00
taosMemoryFree ( * result ) ;
* result = NULL ;
}
return code ;
2022-06-27 06:47:14 +00:00
} else if ( currLength = = 0 ) {
ASSERT ( ! pCurrent ) ;
goto _downstream ;
2022-05-28 15:26:22 +00:00
}
2022-05-27 15:21:41 +00:00
2022-06-27 06:47:14 +00:00
+ + ( * nOptrWithVal ) ;
2022-06-26 10:44:49 +00:00
ASSERT ( currLength > = 0 ) ;
2022-05-27 15:21:41 +00:00
2022-05-30 11:08:14 +00:00
if ( * result = = NULL ) {
2022-05-28 15:48:31 +00:00
* result = ( char * ) taosMemoryCalloc ( 1 , currLength + sizeof ( int32_t ) ) ;
2022-05-28 15:26:22 +00:00
if ( * result = = NULL ) {
taosMemoryFree ( pCurrent ) ;
return TSDB_CODE_OUT_OF_MEMORY ;
}
memcpy ( * result + sizeof ( int32_t ) , pCurrent , currLength ) ;
* ( int32_t * ) ( * result ) = currLength + sizeof ( int32_t ) ;
2022-05-30 11:08:14 +00:00
} else {
2022-05-28 15:26:22 +00:00
int32_t sizePre = * ( int32_t * ) ( * result ) ;
2022-05-30 11:08:14 +00:00
char * tmp = ( char * ) taosMemoryRealloc ( * result , sizePre + currLength ) ;
2022-05-28 15:26:22 +00:00
if ( tmp = = NULL ) {
taosMemoryFree ( pCurrent ) ;
taosMemoryFree ( * result ) ;
* result = NULL ;
return TSDB_CODE_OUT_OF_MEMORY ;
}
* result = tmp ;
memcpy ( * result + sizePre , pCurrent , currLength ) ;
* ( int32_t * ) ( * result ) + = currLength ;
}
taosMemoryFree ( pCurrent ) ;
* length = * ( int32_t * ) ( * result ) ;
2022-05-27 15:21:41 +00:00
}
2022-06-27 06:47:14 +00:00
_downstream :
2022-05-28 15:26:22 +00:00
for ( int32_t i = 0 ; i < ops - > numOfDownstream ; + + i ) {
2022-06-27 06:47:14 +00:00
code = encodeOperator ( ops - > pDownstream [ i ] , result , length , nOptrWithVal ) ;
2022-05-30 11:08:14 +00:00
if ( code ! = TDB_CODE_SUCCESS ) {
2022-05-28 15:26:22 +00:00
return code ;
2022-05-27 15:21:41 +00:00
}
}
2022-05-28 15:26:22 +00:00
return TDB_CODE_SUCCESS ;
2022-05-27 15:21:41 +00:00
}
2022-06-17 12:07:55 +00:00
int32_t decodeOperator ( SOperatorInfo * ops , const char * result , int32_t length ) {
2022-05-28 15:26:22 +00:00
int32_t code = TDB_CODE_SUCCESS ;
2022-05-30 11:08:14 +00:00
if ( ops - > fpSet . decodeResultRow ) {
if ( result = = NULL ) {
2022-05-28 15:26:22 +00:00
return TSDB_CODE_TSC_INVALID_INPUT ;
}
2022-06-17 12:07:55 +00:00
2022-05-30 04:00:51 +00:00
ASSERT ( length = = * ( int32_t * ) result ) ;
2022-06-17 12:07:55 +00:00
const char * data = result + sizeof ( int32_t ) ;
2022-06-20 06:40:13 +00:00
code = ops - > fpSet . decodeResultRow ( ops , ( char * ) data ) ;
2022-05-30 11:08:14 +00:00
if ( code ! = TDB_CODE_SUCCESS ) {
2022-05-28 15:26:22 +00:00
return code ;
}
2022-05-27 15:21:41 +00:00
2022-05-28 15:26:22 +00:00
int32_t totalLength = * ( int32_t * ) result ;
2022-05-30 04:00:51 +00:00
int32_t dataLength = * ( int32_t * ) data ;
2022-05-30 11:08:14 +00:00
if ( totalLength = = dataLength + sizeof ( int32_t ) ) { // the last data
2022-05-28 15:26:22 +00:00
result = NULL ;
length = 0 ;
2022-05-30 11:08:14 +00:00
} else {
2022-05-28 15:26:22 +00:00
result + = dataLength ;
* ( int32_t * ) ( result ) = totalLength - dataLength ;
length = totalLength - dataLength ;
}
2022-05-27 15:21:41 +00:00
}
2022-05-28 15:26:22 +00:00
for ( int32_t i = 0 ; i < ops - > numOfDownstream ; + + i ) {
code = decodeOperator ( ops - > pDownstream [ i ] , result , length ) ;
2022-05-30 11:08:14 +00:00
if ( code ! = TDB_CODE_SUCCESS ) {
2022-05-28 15:26:22 +00:00
return code ;
2022-05-27 15:21:41 +00:00
}
}
2022-05-28 15:26:22 +00:00
return TDB_CODE_SUCCESS ;
2022-05-27 15:21:41 +00:00
}
2022-06-07 12:59:44 +00:00
int32_t createDataSinkParam ( SDataSinkNode * pNode , void * * pParam , qTaskInfo_t * pTaskInfo ) {
2022-06-07 01:11:53 +00:00
SExecTaskInfo * pTask = * ( SExecTaskInfo * * ) pTaskInfo ;
2022-06-07 12:59:44 +00:00
2022-06-06 12:59:36 +00:00
switch ( pNode - > type ) {
case QUERY_NODE_PHYSICAL_PLAN_DELETE : {
2022-06-07 12:59:44 +00:00
SDeleterParam * pDeleterParam = taosMemoryCalloc ( 1 , sizeof ( SDeleterParam ) ) ;
2022-06-06 12:59:36 +00:00
if ( NULL = = pDeleterParam ) {
return TSDB_CODE_OUT_OF_MEMORY ;
}
int32_t tbNum = taosArrayGetSize ( pTask - > tableqinfoList . pTableList ) ;
2022-06-23 06:25:30 +00:00
pDeleterParam - > suid = pTask - > tableqinfoList . suid ;
2022-06-06 12:59:36 +00:00
pDeleterParam - > pUidList = taosArrayInit ( tbNum , sizeof ( uint64_t ) ) ;
if ( NULL = = pDeleterParam - > pUidList ) {
taosMemoryFree ( pDeleterParam ) ;
return TSDB_CODE_OUT_OF_MEMORY ;
}
for ( int32_t i = 0 ; i < tbNum ; + + i ) {
2022-06-07 12:59:44 +00:00
STableKeyInfo * pTable = taosArrayGet ( pTask - > tableqinfoList . pTableList , i ) ;
2022-06-06 12:59:36 +00:00
taosArrayPush ( pDeleterParam - > pUidList , & pTable - > uid ) ;
}
* pParam = pDeleterParam ;
break ;
}
default :
break ;
}
return TSDB_CODE_SUCCESS ;
}
2022-04-23 10:29:45 +00:00
int32_t createExecTaskInfoImpl ( SSubplan * pPlan , SExecTaskInfo * * pTaskInfo , SReadHandle * pHandle , uint64_t taskId ,
2022-06-16 03:43:11 +00:00
const char * sql , EOPTR_EXEC_MODEL model ) {
2022-01-20 08:02:09 +00:00
uint64_t queryId = pPlan - > id . queryId ;
2022-01-21 03:19:24 +00:00
int32_t code = TSDB_CODE_SUCCESS ;
2022-05-20 10:00:05 +00:00
* pTaskInfo = createExecTaskInfo ( queryId , taskId , model , pPlan - > dbFName ) ;
2022-01-21 03:19:24 +00:00
if ( * pTaskInfo = = NULL ) {
code = TSDB_CODE_QRY_OUT_OF_MEMORY ;
goto _complete ;
}
2022-01-08 14:59:24 +00:00
2022-06-16 03:43:11 +00:00
( * pTaskInfo ) - > sql = sql ;
2022-06-21 12:53:19 +00:00
( * pTaskInfo ) - > tableqinfoList . pTagCond = pPlan - > pTagCond ;
( * pTaskInfo ) - > tableqinfoList . pTagIndexCond = pPlan - > pTagIndexCond ;
2022-06-28 05:59:49 +00:00
( * pTaskInfo ) - > pRoot = createOperatorTree ( pPlan - > pNode , * pTaskInfo , pHandle , queryId , taskId ,
& ( * pTaskInfo ) - > tableqinfoList , pPlan - > user ) ;
2022-06-23 14:05:00 +00:00
2022-07-05 01:47:39 +00:00
2022-03-15 07:34:17 +00:00
if ( NULL = = ( * pTaskInfo ) - > pRoot ) {
2022-06-11 06:51:54 +00:00
code = ( * pTaskInfo ) - > code ;
2022-03-10 11:36:30 +00:00
goto _complete ;
2022-01-13 11:01:28 +00:00
}
2022-01-21 03:55:13 +00:00
return code ;
2022-01-21 03:19:24 +00:00
_complete :
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( * pTaskInfo ) ;
2022-01-21 03:19:24 +00:00
terrno = code ;
return code ;
2022-01-08 14:59:24 +00:00
}
2022-05-26 08:05:27 +00:00
static void doDestroyTableList ( STableListInfo * pTableqinfoList ) {
taosArrayDestroy ( pTableqinfoList - > pTableList ) ;
taosHashCleanup ( pTableqinfoList - > map ) ;
2022-06-28 05:59:49 +00:00
if ( pTableqinfoList - > needSortTableByGroupId ) {
for ( int32_t i = 0 ; i < taosArrayGetSize ( pTableqinfoList - > pGroupList ) ; i + + ) {
2022-06-23 11:58:12 +00:00
SArray * tmp = taosArrayGetP ( pTableqinfoList - > pGroupList , i ) ;
taosArrayDestroy ( tmp ) ;
}
}
taosArrayDestroy ( pTableqinfoList - > pGroupList ) ;
2021-11-02 05:37:31 +00:00
2022-05-26 08:05:27 +00:00
pTableqinfoList - > pTableList = NULL ;
pTableqinfoList - > map = NULL ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
void doDestroyTask ( SExecTaskInfo * pTaskInfo ) {
2022-01-26 06:51:57 +00:00
qDebug ( " %s execTask is freed " , GET_TASKID ( pTaskInfo ) ) ;
2022-05-26 08:05:27 +00:00
doDestroyTableList ( & pTaskInfo - > tableqinfoList ) ;
2022-05-07 07:11:49 +00:00
destroyOperatorInfo ( pTaskInfo - > pRoot ) ;
2022-03-29 07:24:25 +00:00
// taosArrayDestroy(pTaskInfo->summary.queryProfEvents);
// taosHashCleanup(pTaskInfo->summary.operatorProfResults);
2021-11-02 05:37:31 +00:00
2022-05-18 12:39:00 +00:00
taosMemoryFree ( pTaskInfo - > schemaVer . dbname ) ;
taosMemoryFree ( pTaskInfo - > schemaVer . tablename ) ;
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pTaskInfo - > sql ) ;
taosMemoryFreeClear ( pTaskInfo - > id . str ) ;
taosMemoryFreeClear ( pTaskInfo ) ;
2021-11-02 05:37:31 +00:00
}
static void doSetTagValueToResultBuf ( char * output , const char * val , int16_t type , int16_t bytes ) {
if ( val = = NULL ) {
setNull ( output , type , bytes ) ;
return ;
}
if ( IS_VAR_DATA_TYPE ( type ) ) {
// Binary data overflows for sort of unknown reasons. Let trim the overflow data
if ( varDataTLen ( val ) > bytes ) {
int32_t maxLen = bytes - VARSTR_HEADER_SIZE ;
2022-03-29 07:24:25 +00:00
int32_t len = ( varDataLen ( val ) > maxLen ) ? maxLen : varDataLen ( val ) ;
2021-11-02 05:37:31 +00:00
memcpy ( varDataVal ( output ) , varDataVal ( val ) , len ) ;
varDataSetLen ( output , len ) ;
} else {
varDataCopy ( output , val ) ;
}
} else {
memcpy ( output , val , bytes ) ;
}
}
static int64_t getQuerySupportBufSize ( size_t numOfTables ) {
size_t s1 = sizeof ( STableQueryInfo ) ;
2022-03-29 07:24:25 +00:00
// size_t s3 = sizeof(STableCheckInfo); buffer consumption in tsdb
return ( int64_t ) ( s1 * 1.5 * numOfTables ) ;
2021-11-02 05:37:31 +00:00
}
int32_t checkForQueryBuf ( size_t numOfTables ) {
int64_t t = getQuerySupportBufSize ( numOfTables ) ;
if ( tsQueryBufferSizeBytes < 0 ) {
return TSDB_CODE_SUCCESS ;
} else if ( tsQueryBufferSizeBytes > 0 ) {
2022-03-29 07:24:25 +00:00
while ( 1 ) {
2021-11-02 05:37:31 +00:00
int64_t s = tsQueryBufferSizeBytes ;
int64_t remain = s - t ;
if ( remain > = 0 ) {
if ( atomic_val_compare_exchange_64 ( & tsQueryBufferSizeBytes , s , remain ) = = s ) {
return TSDB_CODE_SUCCESS ;
}
} else {
return TSDB_CODE_QRY_NOT_ENOUGH_BUFFER ;
}
}
}
// disable query processing if the value of tsQueryBufferSize is zero.
return TSDB_CODE_QRY_NOT_ENOUGH_BUFFER ;
}
void releaseQueryBuf ( size_t numOfTables ) {
if ( tsQueryBufferSizeBytes < 0 ) {
return ;
}
int64_t t = getQuerySupportBufSize ( numOfTables ) ;
// restore value is not enough buffer available
atomic_add_fetch_64 ( & tsQueryBufferSizeBytes , t ) ;
}
2022-04-01 11:45:45 +00:00
2022-04-23 10:29:45 +00:00
int32_t getOperatorExplainExecInfo ( SOperatorInfo * operatorInfo , SExplainExecInfo * * pRes , int32_t * capacity ,
int32_t * resNum ) {
2022-04-01 11:45:45 +00:00
if ( * resNum > = * capacity ) {
* capacity + = 10 ;
2022-04-23 10:29:45 +00:00
2022-04-01 11:45:45 +00:00
* pRes = taosMemoryRealloc ( * pRes , ( * capacity ) * sizeof ( SExplainExecInfo ) ) ;
if ( NULL = = * pRes ) {
2022-04-02 11:31:52 +00:00
qError ( " malloc %d failed " , ( * capacity ) * ( int32_t ) sizeof ( SExplainExecInfo ) ) ;
2022-04-01 11:45:45 +00:00
return TSDB_CODE_QRY_OUT_OF_MEMORY ;
}
}
2022-05-24 03:29:51 +00:00
SExplainExecInfo * pInfo = & ( * pRes ) [ * resNum ] ;
pInfo - > numOfRows = operatorInfo - > resultInfo . totalRows ;
pInfo - > startupCost = operatorInfo - > cost . openCost ;
pInfo - > totalCost = operatorInfo - > cost . totalCost ;
2022-04-06 03:32:21 +00:00
2022-04-26 12:26:32 +00:00
if ( operatorInfo - > fpSet . getExplainFn ) {
2022-05-24 03:29:51 +00:00
int32_t code = operatorInfo - > fpSet . getExplainFn ( operatorInfo , & pInfo - > verboseInfo , & pInfo - > verboseLen ) ;
2022-04-06 03:32:21 +00:00
if ( code ) {
2022-05-24 03:29:51 +00:00
qError ( " %s operator getExplainFn failed, code:%s " , GET_TASKID ( operatorInfo - > pTaskInfo ) , tstrerror ( code ) ) ;
2022-04-06 03:32:21 +00:00
return code ;
}
2022-05-24 03:29:51 +00:00
} else {
pInfo - > verboseLen = 0 ;
pInfo - > verboseInfo = NULL ;
2022-04-06 03:32:21 +00:00
}
2022-04-23 10:29:45 +00:00
2022-04-02 11:31:52 +00:00
+ + ( * resNum ) ;
2022-04-23 10:29:45 +00:00
2022-04-02 11:31:52 +00:00
int32_t code = 0 ;
2022-04-06 03:32:21 +00:00
for ( int32_t i = 0 ; i < operatorInfo - > numOfDownstream ; + + i ) {
code = getOperatorExplainExecInfo ( operatorInfo - > pDownstream [ i ] , pRes , capacity , resNum ) ;
2022-04-02 11:31:52 +00:00
if ( code ) {
taosMemoryFreeClear ( * pRes ) ;
return TSDB_CODE_QRY_OUT_OF_MEMORY ;
}
}
return TSDB_CODE_SUCCESS ;
2022-04-01 11:45:45 +00:00
}
2022-05-17 09:38:21 +00:00
2022-06-10 07:20:57 +00:00
int32_t initStreamAggSupporter ( SStreamAggSupporter * pSup , const char * pKey , SqlFunctionCtx * pCtx , int32_t numOfOutput ,
2022-06-15 08:40:45 +00:00
int32_t size ) {
2022-06-09 09:18:17 +00:00
pSup - > resultRowSize = getResultRowSize ( pCtx , numOfOutput ) ;
2022-05-20 11:34:39 +00:00
pSup - > keySize = sizeof ( int64_t ) + sizeof ( TSKEY ) ;
pSup - > pKeyBuf = taosMemoryCalloc ( 1 , pSup - > keySize ) ;
2022-06-15 08:40:45 +00:00
_hash_fn_t hashFn = taosGetDefaultHashFunction ( TSDB_DATA_TYPE_BINARY ) ;
pSup - > pResultRows = taosHashInit ( 1024 , hashFn , false , HASH_NO_LOCK ) ;
2022-05-20 11:34:39 +00:00
if ( pSup - > pKeyBuf = = NULL | | pSup - > pResultRows = = NULL ) {
return TSDB_CODE_OUT_OF_MEMORY ;
}
2022-06-15 08:40:45 +00:00
pSup - > valueSize = size ;
2022-05-20 11:34:39 +00:00
2022-06-04 12:06:07 +00:00
pSup - > pScanWindow = taosArrayInit ( 4 , sizeof ( STimeWindow ) ) ;
2022-05-20 11:34:39 +00:00
int32_t pageSize = 4096 ;
while ( pageSize < pSup - > resultRowSize * 4 ) {
pageSize < < = 1u ;
}
// at least four pages need to be in buffer
int32_t bufSize = 4096 * 256 ;
if ( bufSize < = pageSize ) {
bufSize = pageSize * 4 ;
}
2022-06-09 09:18:17 +00:00
int32_t code = createDiskbasedBuf ( & pSup - > pResultBuf , pageSize , bufSize , pKey , TD_TMP_DIR_PATH ) ;
2022-06-10 07:20:57 +00:00
for ( int32_t i = 0 ; i < numOfOutput ; + + i ) {
2022-06-09 09:18:17 +00:00
pCtx [ i ] . pBuf = pSup - > pResultBuf ;
}
return code ;
2022-06-04 12:06:07 +00:00
}