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 "functionMgt.h"
# include "function.h"
# include "querynodes.h"
# include "tname.h"
2021-11-02 05:37:31 +00:00
# include "os.h"
2022-02-22 05:12:03 +00:00
2022-02-04 14:41:54 +00:00
# include "parser.h"
2022-03-04 05:25:39 +00:00
# include "tdatablock.h"
# include "texception.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"
# include "function.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"
2022-03-15 06:37:26 +00:00
# include "tsdb.h"
2021-11-02 05:37:31 +00:00
# include "ttypes.h"
2022-01-21 08:12:02 +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 IS_REVERSE_SCAN(runtime) ((runtime)->scanFlag == REVERSE_SCAN)
# define IS_REPEAT_SCAN(runtime) ((runtime)->scanFlag == REPEAT_SCAN)
2022-03-29 07:24:25 +00:00
# define SET_MAIN_SCAN_FLAG(runtime) ((runtime)->scanFlag = MAIN_SCAN)
2021-11-02 05:37:31 +00:00
# define SET_REVERSE_SCAN_FLAG(runtime) ((runtime)->scanFlag = REVERSE_SCAN)
# define TSWINDOW_IS_EQUAL(t1, t2) (((t1).skey == (t2).skey) && ((t1).ekey == (t2).ekey))
2022-03-29 07:24:25 +00:00
# define SDATA_BLOCK_INITIALIZER \
( SDataBlockInfo ) { { 0 } , 0 }
2021-11-02 05:37:31 +00:00
# define GET_FORWARD_DIRECTION_FACTOR(ord) (((ord) == TSDB_ORDER_ASC) ? QUERY_ASC_FORWARD_STEP : QUERY_DESC_FORWARD_STEP)
enum {
2022-03-29 07:24:25 +00:00
TS_JOIN_TS_EQUAL = 0 ,
TS_JOIN_TS_NOT_EQUALS = 1 ,
2021-11-02 05:37:31 +00:00
TS_JOIN_TAG_NOT_EQUALS = 2 ,
} ;
typedef enum SResultTsInterpType {
RESULT_ROW_START_INTERP = 1 ,
2022-03-29 07:24:25 +00:00
RESULT_ROW_END_INTERP = 2 ,
2021-11-02 05:37:31 +00:00
} SResultTsInterpType ;
2022-03-26 07:02:29 +00:00
2021-11-02 05:37:31 +00:00
#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
# define CLEAR_QUERY_STATUS(q, st) ((q)->status &= (~(st)))
# define GET_NUM_OF_TABLEGROUP(q) taosArrayGetSize((q)->tableqinfoGroupInfo.pGroupList)
# define QUERY_IS_INTERVAL_QUERY(_q) ((_q)->interval.interval > 0)
2022-03-29 07:24:25 +00:00
# define TSKEY_MAX_ADD(a, b) \
do { \
if ( a < 0 ) { \
a = a + b ; \
break ; \
} \
if ( sizeof ( a ) = = sizeof ( int32_t ) ) { \
if ( ( b ) > 0 & & ( ( b ) > = INT32_MAX - ( a ) ) ) { \
a = INT32_MAX ; \
} else { \
a = a + b ; \
} \
} else { \
if ( ( b ) > 0 & & ( ( b ) > = INT64_MAX - ( a ) ) ) { \
a = INT64_MAX ; \
} else { \
a = a + b ; \
} \
} \
} while ( 0 )
# define TSKEY_MIN_SUB(a, b) \
do { \
if ( a > = 0 ) { \
a = a + b ; \
break ; \
} \
if ( sizeof ( a ) = = sizeof ( int32_t ) ) { \
if ( ( b ) < 0 & & ( ( b ) < = INT32_MIN - ( a ) ) ) { \
a = INT32_MIN ; \
} else { \
a = a + b ; \
} \
} else { \
if ( ( b ) < 0 & & ( ( b ) < = INT64_MIN - ( a ) ) ) { \
a = INT64_MIN ; \
} else { \
a = a + b ; \
} \
} \
} while ( 0 )
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
}
2022-02-24 09:18:56 +00:00
static void getNextTimeWindow ( SInterval * pInterval , int32_t precision , int32_t order , STimeWindow * tw ) {
int32_t factor = GET_FORWARD_DIRECTION_FACTOR ( order ) ;
if ( pInterval - > intervalUnit ! = ' n ' & & pInterval - > intervalUnit ! = ' y ' ) {
tw - > skey + = pInterval - > sliding * factor ;
tw - > ekey = tw - > skey + pInterval - > interval - 1 ;
2021-11-02 05:37:31 +00:00
return ;
}
2022-02-24 09:18:56 +00:00
int64_t key = tw - > skey , interval = pInterval - > interval ;
2022-03-29 07:24:25 +00:00
// convert key to second
2022-02-24 09:18:56 +00:00
key = convertTimePrecision ( key , precision , TSDB_TIME_PRECISION_MILLI ) / 1000 ;
2021-11-02 05:37:31 +00:00
2022-02-24 09:18:56 +00:00
if ( pInterval - > intervalUnit = = ' y ' ) {
2021-11-02 05:37:31 +00:00
interval * = 12 ;
}
struct tm tm ;
2022-03-29 07:24:25 +00:00
time_t t = ( time_t ) key ;
2022-03-11 08:49:07 +00:00
taosLocalTime ( & t , & tm ) ;
2021-11-02 05:37:31 +00:00
int mon = ( int ) ( tm . tm_year * 12 + tm . tm_mon + interval * factor ) ;
tm . tm_year = mon / 12 ;
tm . tm_mon = mon % 12 ;
2022-02-24 09:18:56 +00:00
tw - > skey = convertTimePrecision ( ( int64_t ) mktime ( & tm ) * 1000L , TSDB_TIME_PRECISION_MILLI , precision ) ;
2021-11-02 05:37:31 +00:00
mon = ( int ) ( mon + interval ) ;
tm . tm_year = mon / 12 ;
tm . tm_mon = mon % 12 ;
2022-02-24 09:18:56 +00:00
tw - > ekey = convertTimePrecision ( ( int64_t ) mktime ( & tm ) * 1000L , TSDB_TIME_PRECISION_MILLI , precision ) ;
2021-11-02 05:37:31 +00:00
tw - > ekey - = 1 ;
}
static void doSetTagValueToResultBuf ( char * output , const char * val , int16_t type , int16_t bytes ) ;
2022-03-29 07:24:25 +00:00
void setResultRowOutputBufInitCtx ( STaskRuntimeEnv * pRuntimeEnv , SResultRow * pResult , SqlFunctionCtx * pCtx ,
int32_t numOfOutput , int32_t * rowCellInfoOffset ) ;
static bool functionNeedToExecute ( SqlFunctionCtx * pCtx ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
static void setBlockStatisInfo ( SqlFunctionCtx * pCtx , SSDataBlock * pSDataBlock , SColumn * pColumn ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
static void destroyTableQueryInfoImpl ( STableQueryInfo * pTableQueryInfo ) ;
static bool hasMainOutput ( STaskAttr * pQueryAttr ) ;
2021-11-02 05:37:31 +00:00
static SColumnInfo * extractColumnFilterInfo ( SExprInfo * pExpr , int32_t numOfOutput , int32_t * numOfFilterCols ) ;
2022-03-29 07:24:25 +00:00
static int32_t setTimestampListJoinInfo ( STaskRuntimeEnv * pRuntimeEnv , SVariant * pTag , STableQueryInfo * pTableQueryInfo ) ;
static void releaseQueryBuf ( size_t numOfTables ) ;
static int32_t binarySearchForKey ( char * pValue , int num , TSKEY key , int order ) ;
// static STsdbQueryCond createTsdbQueryCond(STaskAttr* pQueryAttr, STimeWindow* win);
2021-11-02 05:37:31 +00:00
static STableIdInfo createTableIdInfo ( STableQueryInfo * pTableQueryInfo ) ;
2022-01-08 08:28:44 +00:00
static int32_t getNumOfScanTimes ( STaskAttr * pQueryAttr ) ;
2021-11-02 05:37:31 +00:00
static void destroyBasicOperatorInfo ( void * param , int32_t numOfOutput ) ;
static void destroySFillOperatorInfo ( void * param , int32_t numOfOutput ) ;
static void destroyProjectOperatorInfo ( void * param , int32_t numOfOutput ) ;
static void destroyTagScanOperatorInfo ( void * param , int32_t numOfOutput ) ;
static void destroyOrderOperatorInfo ( void * param , int32_t numOfOutput ) ;
static void destroySWindowOperatorInfo ( void * param , int32_t numOfOutput ) ;
static void destroyStateWindowOperatorInfo ( 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 ) ;
static void destroyConditionOperatorInfo ( void * param , int32_t numOfOutput ) ;
2021-11-02 05:37:31 +00:00
static void destroyOperatorInfo ( SOperatorInfo * pOperator ) ;
2022-03-10 03:12:44 +00:00
static void destroySysTableScannerOperatorInfo ( void * param , int32_t numOfOutput ) ;
2021-11-02 05:37:31 +00:00
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-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-03-12 07:28:15 +00:00
# define OPTR_IS_OPENED(_optr) (((_optr)->status & OP_OPENED) == OP_OPENED)
# define OPTR_SET_OPENED(_optr) ((_optr)->status |= OP_OPENED)
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-03-05 07:58:28 +00:00
return TSDB_CODE_SUCCESS ;
2022-03-04 07:53:30 +00:00
}
2022-04-04 06:54:39 +00:00
void operatorDummyCloseFn ( void * param , int32_t numOfCols ) { }
2022-03-05 07:58:28 +00:00
2022-03-29 07:24:25 +00:00
static int32_t doCopyToSDataBlock ( SDiskbasedBuf * pBuf , SGroupResInfo * pGroupResInfo , int32_t orderType ,
SSDataBlock * pBlock , int32_t rowCapacity , int32_t * rowCellOffset ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
static void initCtxOutputBuffer ( SqlFunctionCtx * pCtx , int32_t size ) ;
static void getAlignQueryTimeWindow ( SInterval * pInterval , int32_t precision , int64_t key , int64_t keyFirst ,
int64_t keyLast , STimeWindow * win ) ;
2022-02-24 09:18:56 +00:00
2022-03-24 03:15:05 +00:00
static void setResultBufSize ( STaskAttr * pQueryAttr , SResultInfo * pResultInfo ) ;
2022-01-28 02:44:02 +00:00
static void setCtxTagForJoin ( STaskRuntimeEnv * pRuntimeEnv , SqlFunctionCtx * pCtx , SExprInfo * pExprInfo , void * pTable ) ;
2022-03-29 07:24:25 +00:00
static void doSetTableGroupOutputBuf ( SAggOperatorInfo * pAggInfo , int32_t numOfOutput , int32_t tableGroupId ,
SExecTaskInfo * pTaskInfo ) ;
2021-11-02 05:37:31 +00:00
2022-02-14 05:20:46 +00:00
SArray * getOrderCheckColumns ( STaskAttr * pQuery ) ;
2021-11-02 05:37:31 +00:00
typedef struct SRowCompSupporter {
2022-03-29 07:24:25 +00:00
STaskRuntimeEnv * pRuntimeEnv ;
int16_t dataOffset ;
__compar_fn_t comFunc ;
2021-11-02 05:37:31 +00:00
} SRowCompSupporter ;
2022-03-29 07:24:25 +00:00
static int compareRowData ( const void * a , const void * b , const void * userData ) {
const SResultRow * pRow1 = ( const SResultRow * ) a ;
const SResultRow * pRow2 = ( const SResultRow * ) b ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
SRowCompSupporter * supporter = ( SRowCompSupporter * ) userData ;
STaskRuntimeEnv * pRuntimeEnv = supporter - > pRuntimeEnv ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
SFilePage * page1 = getBufPage ( pRuntimeEnv - > pResultBuf , pRow1 - > pageId ) ;
SFilePage * page2 = getBufPage ( pRuntimeEnv - > pResultBuf , pRow2 - > pageId ) ;
2021-11-02 05:37:31 +00:00
int16_t offset = supporter - > dataOffset ;
2022-03-29 07:24:25 +00:00
char * in1 = getPosInResultPage ( pRuntimeEnv - > pQueryAttr , page1 , pRow1 - > offset , offset ) ;
char * in2 = getPosInResultPage ( pRuntimeEnv - > pQueryAttr , page2 , pRow2 - > offset , offset ) ;
2021-11-02 05:37:31 +00:00
return ( in1 ! = NULL & & in2 ! = NULL ) ? supporter - > comFunc ( in1 , in2 ) : 0 ;
}
2022-03-29 07:24:25 +00:00
// setup the output buffer for each operator
2022-03-04 11:21:30 +00:00
SSDataBlock * createOutputBuf_rv1 ( SDataBlockDescNode * pNode ) {
int32_t numOfCols = LIST_LENGTH ( pNode - > pSlots ) ;
2022-03-09 02:22:53 +00:00
2022-03-25 16:29:53 +00:00
SSDataBlock * pBlock = taosMemoryCalloc ( 1 , sizeof ( SSDataBlock ) ) ;
2022-03-04 11:21:30 +00:00
pBlock - > info . numOfCols = numOfCols ;
pBlock - > pDataBlock = taosArrayInit ( numOfCols , sizeof ( SColumnInfoData ) ) ;
2022-03-09 02:22:53 +00:00
pBlock - > info . blockId = pNode - > dataBlockId ;
2022-03-29 06:56:24 +00:00
pBlock - > info . rowSize = pNode - > totalRowSize ; // todo ??
2022-03-09 02:22:53 +00:00
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < numOfCols ; + + i ) {
2022-03-04 11:21:30 +00:00
SColumnInfoData idata = { { 0 } } ;
2022-03-29 07:24:25 +00:00
SSlotDescNode * pDescNode = nodesListGetNode ( pNode - > pSlots , i ) ;
2022-03-29 02:08:21 +00:00
if ( ! pDescNode - > output ) {
continue ;
2022-03-28 12:11:02 +00:00
}
2022-03-29 02:08:21 +00:00
2022-03-30 11:38:30 +00:00
idata . info . type = pDescNode - > dataType . type ;
2022-03-29 07:24:25 +00:00
idata . info . bytes = pDescNode - > dataType . bytes ;
idata . info . scale = pDescNode - > dataType . scale ;
2022-03-04 11:21:30 +00:00
idata . info . slotId = pDescNode - > slotId ;
2022-03-09 02:22:53 +00:00
idata . info . precision = pDescNode - > dataType . precision ;
taosArrayPush ( pBlock - > pDataBlock , & idata ) ;
2022-03-04 11:21:30 +00:00
}
2022-03-09 02:22:53 +00:00
return pBlock ;
2022-03-04 11:21:30 +00:00
}
2022-03-29 07:24:25 +00:00
static bool isSelectivityWithTagsQuery ( SqlFunctionCtx * pCtx , int32_t numOfOutput ) {
2021-11-02 05:37:31 +00:00
return true ;
2022-03-29 07:24:25 +00:00
// bool hasTags = false;
// int32_t numOfSelectivity = 0;
//
// for (int32_t i = 0; i < numOfOutput; ++i) {
// int32_t functId = pCtx[i].functionId;
// if (functId == FUNCTION_TAG_DUMMY || functId == FUNCTION_TS_DUMMY) {
// hasTags = true;
// continue;
// }
//
// if ((aAggs[functId].status & FUNCSTATE_SELECTIVITY) != 0) {
// numOfSelectivity++;
// }
// }
//
// return (numOfSelectivity > 0 && hasTags);
}
static bool hasNull ( SColumn * pColumn , SColumnDataAgg * pStatis ) {
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-01-20 05:52:46 +00:00
static void prepareResultListBuffer ( SResultRowInfo * pResultRowInfo , jmp_buf env ) {
2022-02-22 05:12:03 +00:00
int64_t newCapacity = 0 ;
2021-11-02 05:37:31 +00:00
// more than the capacity, reallocate the resources
if ( pResultRowInfo - > size < pResultRowInfo - > capacity ) {
return ;
}
if ( pResultRowInfo - > capacity > 10000 ) {
newCapacity = ( int64_t ) ( pResultRowInfo - > capacity * 1.25 ) ;
} else {
newCapacity = ( int64_t ) ( pResultRowInfo - > capacity * 1.5 ) ;
}
2022-02-24 09:18:56 +00:00
if ( newCapacity = = pResultRowInfo - > capacity ) {
newCapacity + = 4 ;
}
2022-03-25 16:29:53 +00:00
pResultRowInfo - > pPosition = taosMemoryRealloc ( pResultRowInfo - > pPosition , newCapacity * sizeof ( SResultRowPosition ) ) ;
2021-11-02 05:37:31 +00:00
int32_t inc = ( int32_t ) newCapacity - pResultRowInfo - > capacity ;
2022-03-15 06:37:26 +00:00
memset ( & pResultRowInfo - > pPosition [ pResultRowInfo - > capacity ] , 0 , sizeof ( SResultRowPosition ) ) ;
2021-11-02 05:37:31 +00:00
pResultRowInfo - > capacity = ( int32_t ) newCapacity ;
}
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-03-31 08:10:32 +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-03-31 08:10:32 +00:00
#if 0
2022-01-08 08:28:44 +00:00
static SResultRow * doSetResultOutBufByKey ( STaskRuntimeEnv * pRuntimeEnv , SResultRowInfo * pResultRowInfo , int64_t tid ,
2021-11-02 05:37:31 +00:00
char * pData , int16_t bytes , bool masterscan , uint64_t tableGroupId ) {
bool existed = false ;
SET_RES_WINDOW_KEY ( pRuntimeEnv - > keyBuf , pData , bytes , tableGroupId ) ;
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.
2022-03-29 07:24:25 +00:00
return ( p1 ! = NULL ) ? * p1 : NULL ;
2021-11-02 05:37:31 +00:00
}
if ( p1 ! = NULL ) {
if ( pResultRowInfo - > size = = 0 ) {
existed = false ;
2022-03-31 08:10:32 +00:00
// assert(pResultRowInfo->curPos == -1);
2021-11-02 05:37:31 +00:00
} else if ( pResultRowInfo - > size = = 1 ) {
2022-03-31 08:10:32 +00:00
// existed = (pResultRowInfo->pResult[0] == (*p1));
// pResultRowInfo->curPos = 0;
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 , tid , 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 ) {
2022-03-31 08:10:32 +00:00
// pResultRowInfo->curPos = (int32_t)*index;
2021-11-02 05:37:31 +00:00
existed = true ;
} else {
existed = false ;
}
}
}
} else {
// In case of group by column query, the required SResultRow object must be existed in the pResultRowInfo object.
if ( p1 ! = NULL ) {
return * p1 ;
}
}
if ( ! existed ) {
2022-03-29 07:24:25 +00:00
// prepareResultListBuffer(pResultRowInfo, pRuntimeEnv);
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
SResultRow * pResult = NULL ;
2021-11-02 05:37:31 +00:00
if ( p1 = = NULL ) {
pResult = getNewResultRow ( pRuntimeEnv - > pool ) ;
int32_t ret = initResultRow ( pResult ) ;
if ( ret ! = TSDB_CODE_SUCCESS ) {
longjmp ( pRuntimeEnv - > env , TSDB_CODE_QRY_OUT_OF_MEMORY ) ;
}
// add a new result set for a new group
2022-03-29 07:24:25 +00:00
taosHashPut ( pRuntimeEnv - > pResultRowHashTable , pRuntimeEnv - > keyBuf , GET_RES_WINDOW_KEY_LEN ( bytes ) , & pResult ,
POINTER_BYTES ) ;
2021-11-02 05:37:31 +00:00
SResultRowCell cell = { . groupId = tableGroupId , . pRow = pResult } ;
taosArrayPush ( pRuntimeEnv - > pResultRowArrayList , & cell ) ;
} else {
pResult = * p1 ;
}
pResultRowInfo - > curPos = pResultRowInfo - > size ;
pResultRowInfo - > pResult [ pResultRowInfo - > size + + ] = pResult ;
int64_t index = pResultRowInfo - > curPos ;
SET_RES_EXT_WINDOW_KEY ( pRuntimeEnv - > keyBuf , pData , bytes , tid , pResultRowInfo ) ;
2022-03-29 07:24:25 +00:00
taosHashPut ( pRuntimeEnv - > pResultRowListSet , pRuntimeEnv - > keyBuf , GET_RES_EXT_WINDOW_KEY_LEN ( bytes ) , & index ,
POINTER_BYTES ) ;
2021-11-02 05:37:31 +00:00
}
// too many time window in query
if ( pResultRowInfo - > size > MAX_INTERVAL_TIME_WINDOW ) {
longjmp ( pRuntimeEnv - > env , TSDB_CODE_QRY_TOO_MANY_TIMEWINDOW ) ;
}
return pResultRowInfo - > pResult [ pResultRowInfo - > curPos ] ;
}
2022-03-31 08:10:32 +00:00
# endif
2021-11-02 05:37:31 +00:00
2022-03-15 06:37:26 +00:00
SResultRow * getNewResultRow_rv ( 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 ) ;
if ( pData - > num + interBufSize + sizeof ( SResultRow ) > getBufPageSize ( pResultBuf ) ) {
// 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 ;
}
// 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 ;
pData - > num + = interBufSize + sizeof ( SResultRow ) ;
return pResultRow ;
}
2022-03-29 07:24:25 +00:00
static SResultRow * doSetResultOutBufByKey_rv ( SDiskbasedBuf * pResultBuf , SResultRowInfo * pResultRowInfo , int64_t tid ,
char * pData , int16_t bytes , bool masterscan , uint64_t tableGroupId ,
SExecTaskInfo * pTaskInfo , bool isIntervalQuery , SAggSupporter * pSup ) {
2022-03-31 08:10:32 +00:00
bool existInCurrentResusltRowInfo = false ;
2022-02-22 05:12:03 +00:00
SET_RES_WINDOW_KEY ( pSup - > keyBuf , pData , bytes , tableGroupId ) ;
2022-01-20 05:52:46 +00:00
2022-03-31 08:10:32 +00:00
SResultRowPosition * p1 = ( SResultRowPosition * ) taosHashGet ( pSup - > pResultRowHashTable , pSup - > keyBuf , GET_RES_WINDOW_KEY_LEN ( bytes ) ) ;
2022-01-20 05:52:46 +00:00
// in case of repeat scan/reverse scan, no new time window added.
if ( isIntervalQuery ) {
if ( ! masterscan ) { // the *p1 may be NULL in case of sliding+offset exists.
2022-03-31 08:10:32 +00:00
if ( p1 ! = NULL ) {
return getResultRowByPos ( pResultBuf , p1 ) ;
} else {
return NULL ;
}
2022-01-20 05:52:46 +00:00
}
if ( p1 ! = NULL ) {
if ( pResultRowInfo - > size = = 0 ) {
2022-03-31 08:10:32 +00:00
existInCurrentResusltRowInfo = false ; // this time window created by other timestamp that does not belongs to current table.
2022-01-20 05:52:46 +00:00
assert ( pResultRowInfo - > curPos = = - 1 ) ;
} else if ( pResultRowInfo - > size = = 1 ) {
2022-03-31 08:10:32 +00:00
ASSERT ( 0 ) ;
// existInCurrentResusltRowInfo = (pResultRowInfo->pResult[0] == (*p1));
} else { // check if current pResultRowInfo contains the existInCurrentResusltRowInfo pResultRow
2022-02-22 05:12:03 +00:00
SET_RES_EXT_WINDOW_KEY ( pSup - > keyBuf , pData , bytes , tid , pResultRowInfo ) ;
int64_t * index = taosHashGet ( pSup - > pResultRowListSet , pSup - > keyBuf , GET_RES_EXT_WINDOW_KEY_LEN ( bytes ) ) ;
2022-01-20 05:52:46 +00:00
if ( index ! = NULL ) {
2022-03-31 08:10:32 +00:00
// TODO check the scan order for current opened time window
// pResultRowInfo->curPos = (int32_t)*index;
existInCurrentResusltRowInfo = true ;
2022-01-20 05:52:46 +00:00
} else {
2022-03-31 08:10:32 +00:00
existInCurrentResusltRowInfo = false ;
2022-01-20 05:52:46 +00:00
}
}
}
} else {
2022-03-31 08:10:32 +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-03-31 08:10:32 +00:00
return getResultRowByPos ( pResultBuf , p1 ) ;
2022-01-20 05:52:46 +00:00
}
}
2022-03-31 08:10:32 +00:00
SResultRow * pResult = NULL ;
if ( ! existInCurrentResusltRowInfo ) {
// 1. close current opened time window
if ( pResultRowInfo - > curPos ! = - 1 ) { // todo extract function
SResultRowPosition * pos = & pResultRowInfo - > pPosition [ pResultRowInfo - > curPos ] ;
SFilePage * pPage = getBufPage ( pResultBuf , pos - > pageId ) ;
SResultRow * pRow = ( SResultRow * ) ( ( char * ) pPage + pos - > offset ) ;
closeResultRow ( pRow ) ;
releaseBufPage ( pResultBuf , pPage ) ;
}
2022-01-20 05:52:46 +00:00
2022-03-31 08:10:32 +00:00
prepareResultListBuffer ( pResultRowInfo , pTaskInfo - > env ) ;
2022-01-20 05:52:46 +00:00
if ( p1 = = NULL ) {
2022-03-15 06:37:26 +00:00
pResult = getNewResultRow_rv ( pResultBuf , tableGroupId , pSup - > resultRowSize ) ;
2022-03-31 08:10:32 +00:00
initResultRow ( pResult ) ;
2022-01-20 05:52:46 +00:00
// add a new result set for a new group
2022-03-31 08:10:32 +00:00
SResultRowPosition pos = { . pageId = pResult - > pageId , . offset = pResult - > offset } ;
taosHashPut ( pSup - > pResultRowHashTable , pSup - > keyBuf , GET_RES_WINDOW_KEY_LEN ( bytes ) , & pos , POINTER_BYTES ) ;
SResultRowCell cell = { . groupId = tableGroupId , . pos = pos } ;
2022-02-22 05:12:03 +00:00
taosArrayPush ( pSup - > pResultRowArrayList , & cell ) ;
2022-01-20 05:52:46 +00:00
} else {
2022-03-31 08:10:32 +00:00
pResult = getResultRowByPos ( pResultBuf , p1 ) ;
2022-01-20 05:52:46 +00:00
}
2022-03-31 08:10:32 +00:00
// 2. set the new time window to be the new active time window
2022-01-20 05:52:46 +00:00
pResultRowInfo - > curPos = pResultRowInfo - > size ;
2022-03-31 08:10:32 +00:00
pResultRowInfo - > pPosition [ pResultRowInfo - > size + + ] = ( SResultRowPosition ) { . pageId = pResult - > pageId , . offset = pResult - > offset } ;
2022-01-20 05:52:46 +00:00
int64_t index = pResultRowInfo - > curPos ;
2022-02-22 05:12:03 +00:00
SET_RES_EXT_WINDOW_KEY ( pSup - > keyBuf , pData , bytes , tid , pResultRowInfo ) ;
taosHashPut ( pSup - > pResultRowListSet , pSup - > keyBuf , GET_RES_EXT_WINDOW_KEY_LEN ( bytes ) , & index , POINTER_BYTES ) ;
2022-01-20 05:52:46 +00:00
}
// too many time window in query
if ( pResultRowInfo - > size > MAX_INTERVAL_TIME_WINDOW ) {
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
}
2022-03-29 07:24:25 +00:00
static void getInitialStartTimeWindow ( SInterval * pInterval , int32_t precision , TSKEY ts , STimeWindow * w , TSKEY ekey ,
bool ascQuery ) {
2022-02-24 09:18:56 +00:00
if ( ascQuery ) {
getAlignQueryTimeWindow ( pInterval , precision , ts , ts , ekey , w ) ;
2021-11-02 05:37:31 +00:00
} else {
// the start position of the first time window in the endpoint that spreads beyond the queried last timestamp
2022-02-24 09:18:56 +00:00
getAlignQueryTimeWindow ( pInterval , precision , ts , ekey , ts , w ) ;
2021-11-02 05:37:31 +00:00
int64_t key = w - > skey ;
2022-03-29 07:24:25 +00:00
while ( key < ts ) { // moving towards end
2022-03-25 06:28:01 +00:00
key = taosTimeAdd ( key , pInterval - > sliding , pInterval - > slidingUnit , precision ) ;
2021-11-02 05:37:31 +00:00
if ( key > = ts ) {
break ;
}
w - > skey = key ;
}
}
}
// get the correct time window according to the handled timestamp
2022-03-31 08:10:32 +00:00
static STimeWindow getActiveTimeWindow ( SDiskbasedBuf * pBuf , SResultRowInfo * pResultRowInfo , int64_t ts , SInterval * pInterval ,
2022-03-29 07:24:25 +00:00
int32_t precision , STimeWindow * win ) {
2021-11-02 05:37:31 +00:00
STimeWindow w = { 0 } ;
2022-03-29 07:24:25 +00:00
if ( pResultRowInfo - > curPos = = - 1 ) { // the first window, from the previous stored value
2022-02-24 09:18:56 +00:00
getInitialStartTimeWindow ( pInterval , precision , ts , & w , win - > ekey , true ) ;
2022-03-25 06:28:01 +00:00
w . ekey = taosTimeAdd ( w . skey , pInterval - > interval , pInterval - > intervalUnit , precision ) - 1 ;
2021-11-02 05:37:31 +00:00
} else {
2022-03-31 08:10:32 +00:00
w = getResultRow ( pBuf , pResultRowInfo , pResultRowInfo - > curPos ) - > win ;
2021-11-02 05:37:31 +00:00
}
if ( w . skey > ts | | w . ekey < ts ) {
2022-02-24 09:18:56 +00:00
if ( pInterval - > intervalUnit = = ' n ' | | pInterval - > intervalUnit = = ' y ' ) {
w . skey = taosTimeTruncate ( ts , pInterval , precision ) ;
w . ekey = taosTimeAdd ( w . skey , pInterval - > interval , pInterval - > intervalUnit , precision ) - 1 ;
2021-11-02 05:37:31 +00:00
} else {
int64_t st = w . skey ;
if ( st > ts ) {
2022-02-24 09:18:56 +00:00
st - = ( ( st - ts + pInterval - > sliding - 1 ) / pInterval - > sliding ) * pInterval - > sliding ;
2021-11-02 05:37:31 +00:00
}
2022-02-24 09:18:56 +00:00
int64_t et = st + pInterval - > interval - 1 ;
2021-11-02 05:37:31 +00:00
if ( et < ts ) {
2022-02-24 09:18:56 +00:00
st + = ( ( ts - et + pInterval - > sliding - 1 ) / pInterval - > sliding ) * pInterval - > sliding ;
2021-11-02 05:37:31 +00:00
}
w . skey = st ;
2022-03-25 06:28:01 +00:00
w . ekey = taosTimeAdd ( w . skey , pInterval - > interval , pInterval - > intervalUnit , precision ) - 1 ;
2021-11-02 05:37:31 +00:00
}
}
return w ;
}
// get the correct time window according to the handled timestamp
2022-03-29 07:24:25 +00:00
static STimeWindow getCurrentActiveTimeWindow ( SResultRowInfo * pResultRowInfo , int64_t ts , STaskAttr * pQueryAttr ) {
2021-11-02 05:37:31 +00:00
STimeWindow w = { 0 } ;
2022-03-31 08:10:32 +00:00
#if 0
2022-03-29 07:24:25 +00:00
if ( pResultRowInfo - > curPos = = - 1 ) { // the first window, from the previous stored value
// getInitialStartTimeWindow(pQueryAttr, ts, &w);
2021-11-02 05:37:31 +00:00
if ( pQueryAttr - > interval . intervalUnit = = ' n ' | | pQueryAttr - > interval . intervalUnit = = ' y ' ) {
2022-03-29 07:24:25 +00:00
w . ekey =
taosTimeAdd ( w . skey , pQueryAttr - > interval . interval , pQueryAttr - > interval . intervalUnit , pQueryAttr - > precision ) -
1 ;
2021-11-02 05:37:31 +00:00
} else {
w . ekey = w . skey + pQueryAttr - > interval . interval - 1 ;
}
} else {
2022-03-31 08:10:32 +00:00
w = pRow - > win ;
2021-11-02 05:37:31 +00:00
}
/*
* query border check , skey should not be bounded by the query time range , since the value skey will
* be used as the time window index value . So we only change ekey of time window accordingly .
*/
if ( w . ekey > pQueryAttr - > window . ekey & & QUERY_IS_ASC_QUERY ( pQueryAttr ) ) {
w . ekey = pQueryAttr - > window . ekey ;
}
2022-03-31 08:10:32 +00:00
# endif
2021-11-02 05:37:31 +00:00
return w ;
}
// 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-29 07:24:25 +00:00
static bool chkWindowOutputBufByKey ( STaskRuntimeEnv * pRuntimeEnv , SResultRowInfo * pResultRowInfo , STimeWindow * win ,
bool masterscan , SResultRow * * pResult , int64_t groupId , SqlFunctionCtx * pCtx ,
int32_t numOfOutput , int32_t * rowCellInfoOffset ) {
2021-11-02 05:37:31 +00:00
assert ( win - > skey < = win - > ekey ) ;
2022-03-29 07:24:25 +00:00
return chkResultRowFromKey ( pRuntimeEnv , pResultRowInfo , ( char * ) & win - > skey , TSDB_KEYSIZE , masterscan , groupId ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static int32_t setResultOutputBufByKey ( STaskRuntimeEnv * pRuntimeEnv , SResultRowInfo * pResultRowInfo , int64_t tid ,
STimeWindow * win , bool masterscan , SResultRow * * pResult , int64_t tableGroupId ,
SqlFunctionCtx * pCtx , int32_t numOfOutput , int32_t * rowCellInfoOffset ) {
2021-11-02 05:37:31 +00:00
assert ( win - > skey < = win - > ekey ) ;
2022-03-29 07:24:25 +00:00
SDiskbasedBuf * pResultBuf = pRuntimeEnv - > pResultBuf ;
2021-11-02 05:37:31 +00:00
2022-03-31 08:10:32 +00:00
SResultRow * pResultRow = NULL ; //doSetResultOutBufByKey(pRuntimeEnv, pResultRowInfo, tid, (char*)&win->skey, TSDB_KEYSIZE,
// masterscan, tableGroupId);
2021-11-02 05:37:31 +00:00
if ( pResultRow = = NULL ) {
* pResult = NULL ;
return TSDB_CODE_SUCCESS ;
}
// not assign result buffer yet, add new result buffer
if ( pResultRow - > pageId = = - 1 ) {
2022-03-29 07:24:25 +00:00
int32_t ret = addNewWindowResultBuf ( pResultRow , pResultBuf , ( int32_t ) tableGroupId ,
pRuntimeEnv - > pQueryAttr - > intermediateResultRowSize ) ;
2021-11-02 05:37:31 +00:00
if ( ret ! = TSDB_CODE_SUCCESS ) {
return - 1 ;
}
}
// set time window for current result
pResultRow - > win = ( * win ) ;
* pResult = pResultRow ;
setResultRowOutputBufInitCtx ( pRuntimeEnv , pResultRow , pCtx , numOfOutput , rowCellInfoOffset ) ;
return TSDB_CODE_SUCCESS ;
}
2022-04-01 02:44:34 +00:00
static void setResultRowOutputBufInitCtx_rv ( SResultRow * pResult , SqlFunctionCtx * pCtx , int32_t numOfOutput , int32_t * rowCellInfoOffset ) ;
2022-02-24 09:18:56 +00:00
2022-03-29 07:24:25 +00:00
static int32_t setResultOutputBufByKey_rv ( SResultRowInfo * pResultRowInfo , int64_t id , STimeWindow * win , bool masterscan ,
SResultRow * * pResult , int64_t tableGroupId , SqlFunctionCtx * pCtx ,
int32_t numOfOutput , int32_t * rowCellInfoOffset , SAggSupporter * pAggSup ,
SExecTaskInfo * pTaskInfo ) {
2022-02-24 09:18:56 +00:00
assert ( win - > skey < = win - > ekey ) ;
2022-03-29 07:24:25 +00:00
SResultRow * pResultRow = doSetResultOutBufByKey_rv ( pAggSup - > pResultBuf , pResultRowInfo , id , ( char * ) & win - > skey ,
TSDB_KEYSIZE , masterscan , tableGroupId , pTaskInfo , true , pAggSup ) ;
2022-02-24 09:18:56 +00:00
if ( pResultRow = = NULL ) {
* pResult = NULL ;
return TSDB_CODE_SUCCESS ;
}
// set time window for current result
pResultRow - > win = ( * win ) ;
* pResult = pResultRow ;
2022-04-01 02:44:34 +00:00
setResultRowOutputBufInitCtx_rv ( pResultRow , pCtx , numOfOutput , rowCellInfoOffset ) ;
2022-02-24 09:18:56 +00:00
return TSDB_CODE_SUCCESS ;
}
2021-11-02 05:37:31 +00:00
static void setResultRowInterpo ( SResultRow * pResult , SResultTsInterpType type ) {
assert ( pResult ! = NULL & & ( type = = RESULT_ROW_START_INTERP | | type = = RESULT_ROW_END_INTERP ) ) ;
if ( type = = RESULT_ROW_START_INTERP ) {
pResult - > startInterp = true ;
} else {
2022-03-29 07:24:25 +00:00
pResult - > endInterp = true ;
2021-11-02 05:37:31 +00:00
}
}
static bool resultRowInterpolated ( SResultRow * pResult , SResultTsInterpType type ) {
assert ( pResult ! = NULL & & ( type = = RESULT_ROW_START_INTERP | | type = = RESULT_ROW_END_INTERP ) ) ;
if ( type = = RESULT_ROW_START_INTERP ) {
return pResult - > startInterp = = true ;
} else {
2022-03-29 07:24:25 +00:00
return pResult - > endInterp = = true ;
2021-11-02 05:37:31 +00:00
}
}
2022-03-29 07:24:25 +00:00
static FORCE_INLINE int32_t getForwardStepsInBlock ( int32_t numOfRows , __block_search_fn_t searchFn , TSKEY ekey ,
int16_t pos , int16_t order , int64_t * pData ) {
2021-11-02 05:37:31 +00:00
int32_t forwardStep = 0 ;
if ( order = = TSDB_ORDER_ASC ) {
2022-03-29 07:24:25 +00:00
int32_t end = searchFn ( ( char * ) & pData [ pos ] , numOfRows - pos , ekey , order ) ;
2021-11-02 05:37:31 +00:00
if ( end > = 0 ) {
forwardStep = end ;
if ( pData [ end + pos ] = = ekey ) {
forwardStep + = 1 ;
}
}
} else {
2022-03-29 07:24:25 +00:00
int32_t end = searchFn ( ( char * ) pData , pos + 1 , ekey , order ) ;
2021-11-02 05:37:31 +00:00
if ( end > = 0 ) {
forwardStep = pos - end ;
if ( pData [ end ] = = ekey ) {
forwardStep + = 1 ;
}
}
}
assert ( forwardStep > = 0 ) ;
return forwardStep ;
}
2022-03-31 08:10:32 +00:00
static void doUpdateResultRowIndex ( SResultRowInfo * pResultRowInfo , TSKEY lastKey , bool ascQuery , bool timeWindowInterpo ) {
2021-11-02 05:37:31 +00:00
int64_t skey = TSKEY_INITIAL_VAL ;
2022-03-31 08:10:32 +00:00
#if 0
2021-11-02 05:37:31 +00:00
int32_t i = 0 ;
for ( i = pResultRowInfo - > size - 1 ; i > = 0 ; - - i ) {
2022-03-29 07:24:25 +00:00
SResultRow * pResult = pResultRowInfo - > pResult [ i ] ;
2021-11-02 05:37:31 +00:00
if ( pResult - > closed ) {
break ;
}
// new closed result rows
if ( timeWindowInterpo ) {
2022-03-29 07:24:25 +00:00
if ( pResult - > endInterp & &
( ( pResult - > win . skey < = lastKey & & ascQuery ) | | ( pResult - > win . skey > = lastKey & & ! ascQuery ) ) ) {
if ( i > 0 ) { // the first time window, the startInterp is false.
2021-11-02 05:37:31 +00:00
assert ( pResult - > startInterp ) ;
}
closeResultRow ( pResultRowInfo , i ) ;
} else {
skey = pResult - > win . skey ;
}
} else {
if ( ( pResult - > win . ekey < = lastKey & & ascQuery ) | | ( pResult - > win . skey > = lastKey & & ! ascQuery ) ) {
closeResultRow ( pResultRowInfo , i ) ;
} else {
skey = pResult - > win . skey ;
}
}
}
// all result rows are closed, set the last one to be the skey
if ( skey = = TSKEY_INITIAL_VAL ) {
if ( pResultRowInfo - > size = = 0 ) {
2022-03-29 07:24:25 +00:00
// assert(pResultRowInfo->current == NULL);
2021-11-02 05:37:31 +00:00
assert ( pResultRowInfo - > curPos = = - 1 ) ;
pResultRowInfo - > curPos = - 1 ;
} else {
pResultRowInfo - > curPos = pResultRowInfo - > size - 1 ;
}
} else {
for ( i = pResultRowInfo - > size - 1 ; i > = 0 ; - - i ) {
2022-03-29 07:24:25 +00:00
SResultRow * pResult = pResultRowInfo - > pResult [ i ] ;
2021-11-02 05:37:31 +00:00
if ( pResult - > closed ) {
break ;
}
}
if ( i = = pResultRowInfo - > size - 1 ) {
pResultRowInfo - > curPos = i ;
} else {
pResultRowInfo - > curPos = i + 1 ; // current not closed result object
}
}
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 void updateResultRowInfoActiveIndex ( SResultRowInfo * pResultRowInfo , const STimeWindow * pWin , TSKEY lastKey ,
bool ascQuery , bool interp ) {
2022-02-24 09:18:56 +00:00
if ( ( lastKey > pWin - > ekey & & ascQuery ) | | ( lastKey < pWin - > ekey & & ( ! ascQuery ) ) ) {
2021-11-02 05:37:31 +00:00
closeAllResultRows ( pResultRowInfo ) ;
pResultRowInfo - > curPos = pResultRowInfo - > size - 1 ;
} else {
int32_t step = ascQuery ? 1 : - 1 ;
2022-02-24 09:18:56 +00:00
doUpdateResultRowIndex ( pResultRowInfo , lastKey - step , ascQuery , interp ) ;
2021-11-02 05:37:31 +00:00
}
}
2022-03-29 07:24:25 +00:00
static int32_t getNumOfRowsInTimeWindow ( SDataBlockInfo * pDataBlockInfo , TSKEY * pPrimaryColumn , int32_t startPos ,
TSKEY ekey , __block_search_fn_t searchFn , STableQueryInfo * item ,
int32_t order ) {
2021-11-02 05:37:31 +00:00
assert ( startPos > = 0 & & startPos < pDataBlockInfo - > rows ) ;
2022-03-29 07:24:25 +00:00
int32_t num = - 1 ;
int32_t step = GET_FORWARD_DIRECTION_FACTOR ( order ) ;
2021-11-02 05:37:31 +00:00
2022-02-24 09:18:56 +00:00
if ( order = = TSDB_ORDER_ASC ) {
2021-11-02 05:37:31 +00:00
if ( ekey < pDataBlockInfo - > window . ekey & & pPrimaryColumn ) {
num = getForwardStepsInBlock ( pDataBlockInfo - > rows , searchFn , ekey , startPos , order , pPrimaryColumn ) ;
2022-02-24 09:18:56 +00:00
if ( item ! = NULL ) {
2021-11-02 05:37:31 +00:00
item - > lastKey = pPrimaryColumn [ startPos + ( num - 1 ) ] + step ;
}
} else {
num = pDataBlockInfo - > rows - startPos ;
2022-02-24 09:18:56 +00:00
if ( item ! = NULL ) {
2021-11-02 05:37:31 +00:00
item - > lastKey = pDataBlockInfo - > window . ekey + step ;
}
}
} else { // desc
if ( ekey > pDataBlockInfo - > window . skey & & pPrimaryColumn ) {
num = getForwardStepsInBlock ( pDataBlockInfo - > rows , searchFn , ekey , startPos , order , pPrimaryColumn ) ;
2022-02-24 09:18:56 +00:00
if ( item ! = NULL ) {
2021-11-02 05:37:31 +00:00
item - > lastKey = pPrimaryColumn [ startPos - ( num - 1 ) ] + step ;
}
} else {
num = startPos + 1 ;
2022-02-24 09:18:56 +00:00
if ( item ! = NULL ) {
2021-11-02 05:37:31 +00:00
item - > lastKey = pDataBlockInfo - > window . skey + step ;
}
}
}
assert ( num > = 0 ) ;
return num ;
}
2022-03-30 05:41:15 +00:00
// query_range_start, query_range_end, window_duration, window_start, window_end
static void initExecTimeWindowInfo ( SColumnInfoData * pColData , STimeWindow * pQueryWindow ) {
pColData - > info . type = TSDB_DATA_TYPE_TIMESTAMP ;
pColData - > info . bytes = sizeof ( int64_t ) ;
2022-04-01 09:21:24 +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-04-02 05:32:26 +00:00
static void updateTimeWindowInfo ( SColumnInfoData * pColData , STimeWindow * pWin , bool includeEndpoint ) {
2022-03-30 05:41:15 +00:00
int64_t * ts = ( int64_t * ) pColData - > pData ;
2022-04-02 05:32:26 +00:00
int32_t delta = includeEndpoint ? 1 : 0 ;
2022-03-30 05:41:15 +00:00
2022-04-02 05:32:26 +00:00
int64_t duration = pWin - > ekey - pWin - > skey + delta ;
2022-03-30 05:41:15 +00:00
ts [ 2 ] = duration ; // set the duration
ts [ 3 ] = pWin - > skey ; // window start key
2022-04-02 05:32:26 +00:00
ts [ 4 ] = pWin - > ekey + delta ; // window end key
2022-03-30 05:41:15 +00:00
}
2022-04-04 06:54:39 +00:00
void doApplyFunctions ( SqlFunctionCtx * pCtx , STimeWindow * pWin , SColumnInfoData * pTimeWindowData , int32_t offset , int32_t forwardStep , TSKEY * tsCol ,
2022-03-15 06:37:26 +00:00
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 ) {
pCtx [ k ] . startTs = pWin - > skey ;
2022-04-06 11:46:38 +00:00
// keep it temporarily
2022-04-02 05:32:26 +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-02-24 09:18:56 +00:00
int32_t pos = ( order = = TSDB_ORDER_ASC ) ? offset : offset - ( forwardStep - 1 ) ;
2022-03-15 06:37:26 +00:00
pCtx [ k ] . input . startRowIndex = pos ;
pCtx [ k ] . input . numOfRows = forwardStep ;
2021-11-02 05:37:31 +00:00
if ( tsCol ! = NULL ) {
2022-03-15 08:51:50 +00:00
pCtx [ k ] . ptsList = tsCol ;
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
if ( pCtx [ k ] . isAggSet & & forwardStep < numOfTotal ) {
pCtx [ k ] . isAggSet = false ;
}
2022-03-30 05:41:15 +00:00
if ( fmIsWindowPseudoColumnFunc ( pCtx [ k ] . functionId ) ) {
SResultRowEntryInfo * pEntryInfo = GET_RES_INFO ( & pCtx [ k ] ) ;
char * p = GET_ROWCELL_INTERBUF ( pEntryInfo ) ;
2022-04-02 05:32:26 +00:00
SColumnInfoData idata = { 0 } ;
idata . info . type = TSDB_DATA_TYPE_BIGINT ;
idata . info . bytes = tDataTypes [ TSDB_DATA_TYPE_BIGINT ] . bytes ;
idata . pData = p ;
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 ;
continue ;
}
2022-02-24 09:18:56 +00:00
if ( functionNeedToExecute ( & pCtx [ k ] ) ) {
2022-03-15 06:37:26 +00:00
pCtx [ k ] . fpSet . process ( & pCtx [ k ] ) ;
2021-11-02 05:37:31 +00:00
}
// restore it
2022-03-15 06:37:26 +00:00
pCtx [ k ] . input . colDataAggIsSet = hasAgg ;
pCtx [ k ] . input . startRowIndex = startOffset ;
pCtx [ k ] . input . numOfRows = numOfRows ;
2021-11-02 05:37:31 +00:00
}
}
2022-02-24 09:18:56 +00:00
static int32_t getNextQualifiedWindow ( SInterval * pInterval , STimeWindow * pNext , SDataBlockInfo * pDataBlockInfo ,
TSKEY * primaryKeys , int32_t prevPosition , STableIntervalOperatorInfo * pInfo ) {
int32_t order = pInfo - > order ;
2022-03-29 07:24:25 +00:00
bool ascQuery = ( order = = TSDB_ORDER_ASC ) ;
2022-02-24 09:18:56 +00:00
2022-03-29 12:07:38 +00:00
int32_t precision = pInterval - > precision ;
2022-02-24 09:18:56 +00:00
getNextTimeWindow ( pInterval , precision , order , pNext ) ;
2021-11-02 05:37:31 +00:00
// next time window is not in current block
2022-02-24 09:18:56 +00:00
if ( ( pNext - > skey > pDataBlockInfo - > window . ekey & & order = = TSDB_ORDER_ASC ) | |
( pNext - > ekey < pDataBlockInfo - > window . skey & & order = = TSDB_ORDER_DESC ) ) {
2021-11-02 05:37:31 +00:00
return - 1 ;
}
2022-03-29 07:24:25 +00:00
TSKEY startKey = ascQuery ? pNext - > skey : pNext - > ekey ;
2021-11-02 05:37:31 +00:00
int32_t startPos = 0 ;
// tumbling time window query, a special case of sliding time window query
2022-02-24 09:18:56 +00:00
if ( pInterval - > sliding = = pInterval - > interval & & prevPosition ! = - 1 ) {
int32_t factor = GET_FORWARD_DIRECTION_FACTOR ( order ) ;
2021-11-02 05:37:31 +00:00
startPos = prevPosition + factor ;
} else {
2022-02-24 09:18:56 +00:00
if ( startKey < = pDataBlockInfo - > window . skey & & ascQuery ) {
2021-11-02 05:37:31 +00:00
startPos = 0 ;
2022-02-24 09:18:56 +00:00
} else if ( startKey > = pDataBlockInfo - > window . ekey & & ! ascQuery ) {
2021-11-02 05:37:31 +00:00
startPos = pDataBlockInfo - > rows - 1 ;
} else {
2022-03-29 07:24:25 +00:00
startPos = binarySearchForKey ( ( char * ) primaryKeys , pDataBlockInfo - > rows , startKey , order ) ;
2021-11-02 05:37:31 +00:00
}
}
/* interp query with fill should not skip time window */
2022-03-29 07:24:25 +00:00
// if (pQueryAttr->pointInterpQuery && pQueryAttr->fillType != TSDB_FILL_NONE) {
// return startPos;
// }
2021-11-02 05:37:31 +00:00
/*
* This time window does not cover any data , try next time window ,
* this case may happen when the time window is too small
*/
if ( primaryKeys = = NULL ) {
2022-02-24 09:18:56 +00:00
if ( ascQuery ) {
2021-11-02 05:37:31 +00:00
assert ( pDataBlockInfo - > window . skey < = pNext - > ekey ) ;
} else {
assert ( pDataBlockInfo - > window . ekey > = pNext - > skey ) ;
}
} else {
2022-02-24 09:18:56 +00:00
if ( ascQuery & & primaryKeys [ startPos ] > pNext - > ekey ) {
2021-11-02 05:37:31 +00:00
TSKEY next = primaryKeys [ startPos ] ;
2022-02-24 09:18:56 +00:00
if ( pInterval - > intervalUnit = = ' n ' | | pInterval - > intervalUnit = = ' y ' ) {
pNext - > skey = taosTimeTruncate ( next , pInterval , precision ) ;
pNext - > ekey = taosTimeAdd ( pNext - > skey , pInterval - > interval , pInterval - > intervalUnit , precision ) - 1 ;
2021-11-02 05:37:31 +00:00
} else {
2022-03-29 07:24:25 +00:00
pNext - > ekey + = ( ( next - pNext - > ekey + pInterval - > sliding - 1 ) / pInterval - > sliding ) * pInterval - > sliding ;
2022-02-24 09:18:56 +00:00
pNext - > skey = pNext - > ekey - pInterval - > interval + 1 ;
2021-11-02 05:37:31 +00:00
}
2022-02-24 09:18:56 +00:00
} else if ( ( ! ascQuery ) & & primaryKeys [ startPos ] < pNext - > skey ) {
2021-11-02 05:37:31 +00:00
TSKEY next = primaryKeys [ startPos ] ;
2022-02-24 09:18:56 +00:00
if ( pInterval - > intervalUnit = = ' n ' | | pInterval - > intervalUnit = = ' y ' ) {
pNext - > skey = taosTimeTruncate ( next , pInterval , precision ) ;
pNext - > ekey = taosTimeAdd ( pNext - > skey , pInterval - > interval , pInterval - > intervalUnit , precision ) - 1 ;
2021-11-02 05:37:31 +00:00
} else {
2022-02-24 09:18:56 +00:00
pNext - > skey - = ( ( pNext - > skey - next + pInterval - > sliding - 1 ) / pInterval - > sliding ) * pInterval - > sliding ;
pNext - > ekey = pNext - > skey + pInterval - > interval - 1 ;
2021-11-02 05:37:31 +00:00
}
}
}
return startPos ;
}
2022-03-29 07:24:25 +00:00
static FORCE_INLINE TSKEY reviseWindowEkey ( STaskAttr * pQueryAttr , STimeWindow * pWindow ) {
2021-11-02 05:37:31 +00:00
TSKEY ekey = - 1 ;
if ( QUERY_IS_ASC_QUERY ( pQueryAttr ) ) {
ekey = pWindow - > ekey ;
if ( ekey > pQueryAttr - > window . ekey ) {
ekey = pQueryAttr - > window . ekey ;
}
} else {
ekey = pWindow - > skey ;
if ( ekey < pQueryAttr - > window . ekey ) {
ekey = pQueryAttr - > window . ekey ;
}
}
return ekey ;
}
2022-01-28 02:44:02 +00:00
static void setNotInterpoWindowKey ( SqlFunctionCtx * pCtx , int32_t numOfOutput , int32_t type ) {
2021-11-02 05:37:31 +00:00
if ( type = = RESULT_ROW_START_INTERP ) {
for ( int32_t k = 0 ; k < numOfOutput ; + + k ) {
pCtx [ k ] . start . key = INT64_MIN ;
}
} else {
for ( int32_t k = 0 ; k < numOfOutput ; + + k ) {
pCtx [ k ] . end . key = INT64_MIN ;
}
}
}
2022-02-24 09:18:56 +00:00
static void saveDataBlockLastRow ( char * * pRow , SArray * pDataBlock , int32_t rowIndex , int32_t numOfCols ) {
2021-11-02 05:37:31 +00:00
if ( pDataBlock = = NULL ) {
return ;
}
2022-02-24 09:18:56 +00:00
for ( int32_t k = 0 ; k < numOfCols ; + + k ) {
2022-03-29 07:24:25 +00:00
SColumnInfoData * pColInfo = taosArrayGet ( pDataBlock , k ) ;
2022-02-24 09:18:56 +00:00
memcpy ( pRow [ k ] , ( ( char * ) pColInfo - > pData ) + ( pColInfo - > info . bytes * rowIndex ) , pColInfo - > info . bytes ) ;
2021-11-02 05:37:31 +00:00
}
}
2022-02-24 09:18:56 +00:00
static TSKEY getStartTsKey ( STimeWindow * win , const TSKEY * tsCols , int32_t rows , bool ascQuery ) {
2021-11-02 05:37:31 +00:00
TSKEY ts = TSKEY_INITIAL_VAL ;
if ( tsCols = = NULL ) {
2022-03-29 07:24:25 +00:00
ts = ascQuery ? win - > skey : win - > ekey ;
2021-11-02 05:37:31 +00:00
} else {
2022-03-29 07:24:25 +00:00
int32_t offset = ascQuery ? 0 : rows - 1 ;
2021-11-02 05:37:31 +00:00
ts = tsCols [ offset ] ;
}
return ts ;
}
2022-01-28 02:44:02 +00:00
static void doSetInputDataBlock ( SOperatorInfo * pOperator , SqlFunctionCtx * pCtx , SSDataBlock * pBlock , int32_t order ) ;
2021-11-04 05:24:19 +00:00
2022-03-29 07:24:25 +00:00
static void doSetInputDataBlockInfo ( SOperatorInfo * pOperator , SqlFunctionCtx * pCtx , SSDataBlock * pBlock ,
int32_t order ) {
2021-11-02 05:37:31 +00:00
for ( int32_t i = 0 ; i < pOperator - > numOfOutput ; + + i ) {
pCtx [ i ] . order = order ;
2022-03-29 07:24:25 +00:00
pCtx [ i ] . size = pBlock - > info . rows ;
2021-11-02 05:37:31 +00:00
pCtx [ i ] . currentStage = ( uint8_t ) pOperator - > pRuntimeEnv - > scanFlag ;
2022-03-29 07:24:25 +00:00
setBlockStatisInfo ( & pCtx [ i ] , pBlock , NULL /*&pOperator->pExpr[i].base.colInfo*/ ) ;
2021-11-02 05:37:31 +00:00
}
}
2022-01-28 02:44:02 +00:00
void setInputDataBlock ( SOperatorInfo * pOperator , SqlFunctionCtx * pCtx , SSDataBlock * pBlock , int32_t order ) {
2022-03-09 02:22:53 +00:00
if ( pBlock - > pDataBlock ! = NULL ) {
doSetInputDataBlock ( pOperator , pCtx , pBlock , order ) ;
} else {
doSetInputDataBlockInfo ( pOperator , pCtx , pBlock , order ) ;
}
2021-11-02 05:37:31 +00:00
}
2022-01-28 02:44:02 +00:00
static void doSetInputDataBlock ( SOperatorInfo * pOperator , SqlFunctionCtx * pCtx , SSDataBlock * pBlock , int32_t order ) {
2021-11-02 05:37:31 +00:00
for ( int32_t i = 0 ; i < pOperator - > numOfOutput ; + + i ) {
pCtx [ i ] . order = order ;
2022-03-29 07:24:25 +00:00
pCtx [ i ] . size = pBlock - > info . rows ;
2022-03-09 02:22:53 +00:00
pCtx [ i ] . currentStage = MAIN_SCAN ;
2022-03-29 09:30:44 +00:00
SExprInfo expr = pOperator - > pExpr [ i ] ;
for ( int32_t j = 0 ; j < expr . base . numOfParams ; + + j ) {
SFunctParam * pFuncParam = & expr . base . pParam [ j ] ;
if ( pFuncParam - > type = = FUNC_PARAM_TYPE_COLUMN ) {
int32_t slotId = pFuncParam - > pCol - > slotId ;
pCtx [ i ] . input . pData [ j ] = taosArrayGet ( pBlock - > pDataBlock , slotId ) ;
pCtx [ i ] . input . totalRows = pBlock - > info . rows ;
pCtx [ i ] . input . numOfRows = pBlock - > info . rows ;
pCtx [ i ] . input . startRowIndex = 0 ;
ASSERT ( pCtx [ i ] . input . pData [ j ] ! = NULL ) ;
}
}
2022-03-09 02:22:53 +00:00
2022-03-31 09:36:51 +00:00
// setBlockStatisInfo(&pCtx[i], pBlock, pOperator->pExpr[i].base.pColumns);
2022-03-09 02:22:53 +00:00
// uint32_t flag = pOperator->pExpr[i].base.pParam[0].pCol->flag;
// if (TSDB_COL_IS_NORMAL_COL(flag) /*|| (pCtx[i].functionId == FUNCTION_BLKINFO) ||
// (TSDB_COL_IS_TAG(flag) && pOperator->pRuntimeEnv->scanFlag == MERGE_STAGE)*/) {
// SColumn* pCol = pOperator->pExpr[i].base.pParam[0].pCol;
// if (pCtx[i].columnIndex == -1) {
// for(int32_t j = 0; j < pBlock->info.numOfCols; ++j) {
// SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, j);
// if (pColData->info.colId == pCol->colId) {
// pCtx[i].columnIndex = j;
// break;
// }
// }
// }
// in case of the block distribution query, the inputBytes is not a constant value.
2022-03-29 09:30:44 +00:00
//pCtx[i].input.pData[0] = taosArrayGet(pBlock->pDataBlock, slotId);
//pCtx[i].input.totalRows = pBlock->info.rows;
//pCtx[i].input.numOfRows = pBlock->info.rows;
//pCtx[i].input.startRowIndex = 0;
2022-03-09 02:22:53 +00:00
// uint32_t status = aAggs[pCtx[i].functionId].status;
// if ((status & (FUNCSTATE_SELECTIVITY | FUNCSTATE_NEED_TS)) != 0) {
// SColumnInfoData* tsInfo = taosArrayGet(pBlock->pDataBlock, 0);
// In case of the top/bottom query again the nest query result, which has no timestamp column
// don't set the ptsList attribute.
// if (tsInfo->info.type == TSDB_DATA_TYPE_TIMESTAMP) {
// pCtx[i].ptsList = (int64_t*) tsInfo->pData;
// } else {
// pCtx[i].ptsList = NULL;
// }
// }
// } else if (TSDB_COL_IS_UD_COL(pCol->flag) && (pOperator->pRuntimeEnv->scanFlag == MERGE_STAGE)) {
// SColIndex* pColIndex = &pOperator->pExpr[i].base.colInfo;
// SColumnInfoData* p = taosArrayGet(pBlock->pDataBlock, pColIndex->colIndex);
//
// pCtx[i].pInput = p->pData;
// assert(p->info.colId == pColIndex->info.colId && pCtx[i].inputType == p->info.type);
// for(int32_t j = 0; j < pBlock->info.rows; ++j) {
// char* dst = p->pData + j * p->info.bytes;
// taosVariantDump(&pOperator->pExpr[i].base.param[1], dst, p->info.type, true);
// }
// }
}
}
static void doAggregateImpl ( SOperatorInfo * pOperator , TSKEY startTs , SqlFunctionCtx * pCtx ) {
2021-11-02 05:37:31 +00:00
for ( int32_t k = 0 ; k < pOperator - > numOfOutput ; + + k ) {
2022-02-24 09:18:56 +00:00
if ( functionNeedToExecute ( & pCtx [ k ] ) ) {
2022-03-29 07:24:25 +00:00
pCtx [ k ] . startTs = startTs ; // this can be set during create the struct
2022-03-09 02:22:53 +00:00
pCtx [ k ] . fpSet . process ( & pCtx [ k ] ) ;
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-06 09:59:08 +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-03-29 07:24:25 +00:00
static void projectApplyFunctions ( SExprInfo * pExpr , SSDataBlock * pResult , SSDataBlock * pSrcBlock , SqlFunctionCtx * pCtx ,
2022-04-06 03:01:09 +00:00
int32_t numOfOutput , SArray * pPseudoList ) {
setPseudoOutputColInfo ( pResult , pCtx , pPseudoList ) ;
2021-11-02 05:37:31 +00:00
for ( int32_t k = 0 ; k < numOfOutput ; + + k ) {
2022-03-29 07:24:25 +00:00
if ( pExpr [ k ] . pExpr - > nodeType = = QUERY_NODE_COLUMN ) { // it is a project query
2022-03-18 10:02:00 +00:00
SColumnInfoData * pColInfoData = taosArrayGet ( pResult - > pDataBlock , k ) ;
2022-03-18 16:13:07 +00:00
colDataAssign ( pColInfoData , pCtx [ k ] . input . pData [ 0 ] , pCtx [ k ] . input . numOfRows ) ;
2022-03-26 14:13:14 +00:00
pResult - > info . rows = pCtx [ 0 ] . input . numOfRows ;
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-03-27 14:43:07 +00:00
SScalarParam dest = { 0 } ;
dest . columnData = taosArrayGet ( pResult - > pDataBlock , k ) ;
scalarCalculate ( pExpr [ k ] . pExpr - > _optrRoot . pRootNode , pBlockList , & dest ) ;
pResult - > info . rows = dest . numOfRows ;
taosArrayDestroy ( pBlockList ) ;
} else if ( pExpr [ k ] . pExpr - > nodeType = = QUERY_NODE_FUNCTION ) {
2022-04-06 02:09:14 +00:00
ASSERT ( ! fmIsAggFunc ( pCtx [ k ] . functionId ) ) ;
2022-03-27 14:43:07 +00:00
2022-04-06 02:09:14 +00:00
if ( fmIsPseudoColumnFunc ( pCtx [ k ] . functionId ) ) {
2022-04-06 03:01:09 +00:00
// do nothing
2022-04-06 02:09:14 +00:00
} else if ( fmIsNonstandardSQLFunc ( pCtx [ k ] . functionId ) ) {
2022-04-06 03:01:09 +00:00
// todo set the correct timestamp column
pCtx [ k ] . input . pPTS = taosArrayGet ( pSrcBlock - > pDataBlock , 1 ) ;
2022-03-27 14:43:07 +00:00
2022-04-04 06:54:39 +00:00
SResultRowEntryInfo * pResInfo = GET_RES_INFO ( & pCtx [ k ] ) ;
pCtx [ k ] . fpSet . init ( & pCtx [ k ] , pResInfo ) ;
2022-03-27 14:43:07 +00:00
2022-04-06 03:01:09 +00:00
pCtx [ k ] . pOutput = taosArrayGet ( pResult - > pDataBlock , k ) ;
pCtx [ k ] . offset = pResult - > info . rows ; // set the start offset
2022-04-06 09:59:08 +00:00
if ( taosArrayGetSize ( pPseudoList ) > 0 ) {
int32_t * outputColIndex = taosArrayGet ( pPseudoList , 0 ) ;
pCtx [ k ] . pTsOutput = ( SColumnInfoData * ) pCtx [ * outputColIndex ] . pOutput ;
}
2022-04-06 03:01:09 +00:00
2022-04-04 06:54:39 +00:00
int32_t numOfRows = pCtx [ k ] . fpSet . process ( & pCtx [ k ] ) ;
2022-04-06 02:09:14 +00:00
pResult - > info . rows + = numOfRows ;
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-04 06:54:39 +00:00
SScalarParam dest = { 0 } ;
dest . columnData = taosArrayGet ( pResult - > pDataBlock , k ) ;
scalarCalculate ( ( SNode * ) pExpr [ k ] . pExpr - > _function . pFunctNode , pBlockList , & dest ) ;
pResult - > info . rows = dest . numOfRows ;
taosArrayDestroy ( pBlockList ) ;
}
2022-04-06 07:20:38 +00:00
} else if ( pExpr [ k ] . pExpr - > nodeType = = QUERY_NODE_VALUE ) {
SColumnInfoData * pColInfoData = taosArrayGet ( pResult - > pDataBlock , k ) ;
for ( int32_t i = 0 ; i < pSrcBlock - > info . rows ; + + i ) {
colDataAppend ( pColInfoData , i , taosVariantGet ( & pExpr [ k ] . base . pParam [ 0 ] . param , pExpr [ k ] . base . pParam [ 0 ] . type ) , TSDB_DATA_TYPE_NULL = = pExpr [ k ] . base . pParam [ 0 ] . param . nType ) ;
}
pResult - > info . rows = pSrcBlock - > info . rows ;
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
}
}
}
void doTimeWindowInterpolation ( SOperatorInfo * pOperator , SOptrBasicInfo * pInfo , SArray * pDataBlock , TSKEY prevTs ,
int32_t prevRowIndex , TSKEY curTs , int32_t curRowIndex , TSKEY windowKey , int32_t type ) {
2022-03-29 07:24:25 +00:00
STaskRuntimeEnv * pRuntimeEnv = pOperator - > pRuntimeEnv ;
SExprInfo * pExpr = pOperator - > pExpr ;
2021-11-02 05:37:31 +00:00
2022-01-28 02:44:02 +00:00
SqlFunctionCtx * pCtx = pInfo - > pCtx ;
2021-11-02 05:37:31 +00:00
for ( int32_t k = 0 ; k < pOperator - > numOfOutput ; + + k ) {
int32_t functionId = pCtx [ k ] . functionId ;
if ( functionId ! = FUNCTION_TWA & & functionId ! = FUNCTION_INTERP ) {
pCtx [ k ] . start . key = INT64_MIN ;
continue ;
}
2022-03-29 07:24:25 +00:00
SColIndex * pColIndex = NULL /*&pExpr[k].base.colInfo*/ ;
2021-11-02 05:37:31 +00:00
int16_t index = pColIndex - > colIndex ;
2022-03-29 07:24:25 +00:00
SColumnInfoData * pColInfo = taosArrayGet ( pDataBlock , index ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// assert(pColInfo->info.colId == pColIndex->info.colId && curTs != windowKey);
2021-11-02 05:37:31 +00:00
double v1 = 0 , v2 = 0 , v = 0 ;
if ( prevRowIndex = = - 1 ) {
2022-03-29 07:24:25 +00:00
GET_TYPED_DATA ( v1 , double , pColInfo - > info . type , ( char * ) pRuntimeEnv - > prevRow [ index ] ) ;
2021-11-02 05:37:31 +00:00
} else {
2022-03-29 07:24:25 +00:00
GET_TYPED_DATA ( v1 , double , pColInfo - > info . type , ( char * ) pColInfo - > pData + prevRowIndex * pColInfo - > info . bytes ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
GET_TYPED_DATA ( v2 , double , pColInfo - > info . type , ( char * ) pColInfo - > pData + curRowIndex * pColInfo - > info . bytes ) ;
2021-11-02 05:37:31 +00:00
if ( functionId = = FUNCTION_INTERP ) {
if ( type = = RESULT_ROW_START_INTERP ) {
pCtx [ k ] . start . key = prevTs ;
pCtx [ k ] . start . val = v1 ;
pCtx [ k ] . end . key = curTs ;
pCtx [ k ] . end . val = v2 ;
if ( pColInfo - > info . type = = TSDB_DATA_TYPE_BINARY | | pColInfo - > info . type = = TSDB_DATA_TYPE_NCHAR ) {
if ( prevRowIndex = = - 1 ) {
2022-03-29 07:24:25 +00:00
pCtx [ k ] . start . ptr = ( char * ) pRuntimeEnv - > prevRow [ index ] ;
2021-11-02 05:37:31 +00:00
} else {
2022-03-29 07:24:25 +00:00
pCtx [ k ] . start . ptr = ( char * ) pColInfo - > pData + prevRowIndex * pColInfo - > info . bytes ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
pCtx [ k ] . end . ptr = ( char * ) pColInfo - > pData + curRowIndex * pColInfo - > info . bytes ;
2021-11-02 05:37:31 +00:00
}
}
} else if ( functionId = = FUNCTION_TWA ) {
2022-03-29 07:24:25 +00:00
SPoint point1 = ( SPoint ) { . key = prevTs , . val = & v1 } ;
SPoint point2 = ( SPoint ) { . key = curTs , . val = & v2 } ;
SPoint point = ( SPoint ) { . key = windowKey , . val = & v } ;
2021-11-02 05:37:31 +00:00
taosGetLinearInterpolationVal ( & point , TSDB_DATA_TYPE_DOUBLE , & point1 , & point2 , TSDB_DATA_TYPE_DOUBLE ) ;
if ( type = = RESULT_ROW_START_INTERP ) {
pCtx [ k ] . start . key = point . key ;
pCtx [ k ] . start . val = v ;
} else {
pCtx [ k ] . end . key = point . key ;
pCtx [ k ] . end . val = v ;
}
}
}
}
2022-01-28 02:44:02 +00:00
static bool setTimeWindowInterpolationStartTs ( SOperatorInfo * pOperatorInfo , SqlFunctionCtx * pCtx , int32_t pos ,
2022-04-02 07:08:48 +00:00
int32_t numOfRows , SArray * pDataBlock , const TSKEY * tsCols , STimeWindow * win ) {
bool ascQuery = true ;
2022-03-29 07:24:25 +00:00
TSKEY curTs = tsCols [ pos ] ;
2022-04-02 07:08:48 +00:00
TSKEY lastTs = 0 ; //*(TSKEY*)pRuntimeEnv->prevRow[0];
2021-11-02 05:37:31 +00:00
// lastTs == INT64_MIN and pos == 0 means this is the first time window, interpolation is not needed.
// start exactly from this point, no need to do interpolation
2022-03-29 07:24:25 +00:00
TSKEY key = ascQuery ? win - > skey : win - > ekey ;
2021-11-02 05:37:31 +00:00
if ( key = = curTs ) {
setNotInterpoWindowKey ( pCtx , pOperatorInfo - > numOfOutput , RESULT_ROW_START_INTERP ) ;
return true ;
}
if ( lastTs = = INT64_MIN & & ( ( pos = = 0 & & ascQuery ) | | ( pos = = ( numOfRows - 1 ) & & ! ascQuery ) ) ) {
setNotInterpoWindowKey ( pCtx , pOperatorInfo - > numOfOutput , RESULT_ROW_START_INTERP ) ;
return true ;
}
2022-04-02 07:08:48 +00:00
int32_t step = 1 ; //GET_FORWARD_DIRECTION_FACTOR(pQueryAttr->order.order);
2022-03-29 07:24:25 +00:00
TSKEY prevTs = ( ( pos = = 0 & & ascQuery ) | | ( pos = = ( numOfRows - 1 ) & & ! ascQuery ) ) ? lastTs : tsCols [ pos - step ] ;
2021-11-02 05:37:31 +00:00
2022-04-02 07:08:48 +00:00
doTimeWindowInterpolation ( pOperatorInfo , pOperatorInfo - > info , pDataBlock , prevTs , pos - step , curTs , pos , key , RESULT_ROW_START_INTERP ) ;
2021-11-02 05:37:31 +00:00
return true ;
}
2022-03-29 07:24:25 +00:00
static bool setTimeWindowInterpolationEndTs ( SOperatorInfo * pOperatorInfo , SqlFunctionCtx * pCtx , int32_t endRowIndex ,
SArray * pDataBlock , const TSKEY * tsCols , TSKEY blockEkey ,
STimeWindow * win ) {
2022-04-02 07:08:48 +00:00
int32_t order = TSDB_ORDER_ASC ;
int32_t numOfOutput = pOperatorInfo - > numOfOutput ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
TSKEY actualEndKey = tsCols [ endRowIndex ] ;
2022-04-02 07:08:48 +00:00
TSKEY key = order ? win - > ekey : win - > skey ;
2021-11-02 05:37:31 +00:00
// not ended in current data block, do not invoke interpolation
2022-04-02 07:08:48 +00:00
if ( ( key > blockEkey /*&& QUERY_IS_ASC_QUERY(pQueryAttr)*/ ) | | ( key < blockEkey /*&& !QUERY_IS_ASC_QUERY(pQueryAttr)*/ ) ) {
2021-11-02 05:37:31 +00:00
setNotInterpoWindowKey ( pCtx , numOfOutput , RESULT_ROW_END_INTERP ) ;
return false ;
}
// there is actual end point of current time window, no interpolation need
if ( key = = actualEndKey ) {
setNotInterpoWindowKey ( pCtx , numOfOutput , RESULT_ROW_END_INTERP ) ;
return true ;
}
2022-04-02 07:08:48 +00:00
int32_t step = GET_FORWARD_DIRECTION_FACTOR ( order ) ;
2021-11-02 05:37:31 +00:00
int32_t nextRowIndex = endRowIndex + step ;
assert ( nextRowIndex > = 0 ) ;
TSKEY nextKey = tsCols [ nextRowIndex ] ;
doTimeWindowInterpolation ( pOperatorInfo , pOperatorInfo - > info , pDataBlock , actualEndKey , endRowIndex , nextKey ,
2022-03-29 07:24:25 +00:00
nextRowIndex , key , RESULT_ROW_END_INTERP ) ;
2021-11-02 05:37:31 +00:00
return true ;
}
2022-01-28 02:44:02 +00:00
static void doWindowBorderInterpolation ( SOperatorInfo * pOperatorInfo , SSDataBlock * pBlock , SqlFunctionCtx * pCtx ,
2022-03-29 07:24:25 +00:00
SResultRow * pResult , STimeWindow * win , int32_t startPos , int32_t forwardStep ,
int32_t order , bool timeWindowInterpo ) {
2022-02-24 09:18:56 +00:00
if ( ! timeWindowInterpo ) {
2021-11-02 05:37:31 +00:00
return ;
}
assert ( pBlock ! = NULL ) ;
2022-02-24 09:18:56 +00:00
int32_t step = GET_FORWARD_DIRECTION_FACTOR ( order ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
if ( pBlock - > pDataBlock = = NULL ) {
// tscError("pBlock->pDataBlock == NULL");
2021-11-02 05:37:31 +00:00
return ;
}
2022-02-24 09:18:56 +00:00
2022-03-29 07:24:25 +00:00
SColumnInfoData * pColInfo = taosArrayGet ( pBlock - > pDataBlock , 0 ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
TSKEY * tsCols = ( TSKEY * ) ( pColInfo - > pData ) ;
bool done = resultRowInterpolated ( pResult , RESULT_ROW_START_INTERP ) ;
if ( ! done ) { // it is not interpolated, now start to generated the interpolated value
2021-11-02 05:37:31 +00:00
int32_t startRowIndex = startPos ;
2022-03-29 07:24:25 +00:00
bool interp = setTimeWindowInterpolationStartTs ( pOperatorInfo , pCtx , startRowIndex , pBlock - > info . rows ,
pBlock - > pDataBlock , tsCols , win ) ;
2021-11-02 05:37:31 +00:00
if ( interp ) {
setResultRowInterpo ( pResult , RESULT_ROW_START_INTERP ) ;
}
} else {
2022-02-24 09:18:56 +00:00
setNotInterpoWindowKey ( pCtx , pOperatorInfo - > numOfOutput , RESULT_ROW_START_INTERP ) ;
2021-11-02 05:37:31 +00:00
}
// point interpolation does not require the end key time window interpolation.
2022-03-29 07:24:25 +00:00
// if (pointInterpQuery) {
// return;
// }
2021-11-02 05:37:31 +00:00
// interpolation query does not generate the time window end interpolation
done = resultRowInterpolated ( pResult , RESULT_ROW_END_INTERP ) ;
if ( ! done ) {
int32_t endRowIndex = startPos + ( forwardStep - 1 ) * step ;
2022-03-29 07:24:25 +00:00
TSKEY endKey = ( order = = TSDB_ORDER_ASC ) ? pBlock - > info . window . ekey : pBlock - > info . window . skey ;
bool interp =
setTimeWindowInterpolationEndTs ( pOperatorInfo , pCtx , endRowIndex , pBlock - > pDataBlock , tsCols , endKey , win ) ;
2021-11-02 05:37:31 +00:00
if ( interp ) {
setResultRowInterpo ( pResult , RESULT_ROW_END_INTERP ) ;
}
} else {
2022-02-24 09:18:56 +00:00
setNotInterpoWindowKey ( pCtx , pOperatorInfo - > numOfOutput , RESULT_ROW_END_INTERP ) ;
2021-11-02 05:37:31 +00:00
}
}
2022-03-30 05:41:15 +00:00
static SArray * hashIntervalAgg ( SOperatorInfo * pOperatorInfo , SResultRowInfo * pResultRowInfo , SSDataBlock * pSDataBlock , int32_t tableGroupId ) {
2022-03-29 07:24:25 +00:00
STableIntervalOperatorInfo * pInfo = ( STableIntervalOperatorInfo * ) pOperatorInfo - > info ;
2021-11-02 05:37:31 +00:00
2022-02-24 09:18:56 +00:00
SExecTaskInfo * pTaskInfo = pOperatorInfo - > pTaskInfo ;
2022-03-29 07:24:25 +00:00
int32_t numOfOutput = pOperatorInfo - > numOfOutput ;
2021-11-02 05:37:31 +00:00
2022-03-29 12:07:38 +00:00
SArray * pUpdated = NULL ;
if ( pInfo - > execModel = = OPTR_EXEC_MODEL_STREAM ) {
pUpdated = taosArrayInit ( 4 , sizeof ( SResultRowPosition ) ) ;
}
2022-02-24 09:18:56 +00:00
int32_t step = 1 ;
2022-03-29 12:07:38 +00:00
bool ascScan = true ;
2021-11-02 05:37:31 +00:00
int32_t prevIndex = pResultRowInfo - > curPos ;
TSKEY * tsCols = NULL ;
if ( pSDataBlock - > pDataBlock ! = NULL ) {
2022-04-06 10:26:35 +00:00
SColumnInfoData * pColDataInfo = taosArrayGet ( pSDataBlock - > pDataBlock , pInfo - > primaryTsIndex ) ;
2022-03-29 07:24:25 +00:00
tsCols = ( int64_t * ) pColDataInfo - > pData ;
2022-03-31 08:10:32 +00:00
// assert(tsCols[0] == pSDataBlock->info.window.skey && tsCols[pSDataBlock->info.rows - 1] ==
// pSDataBlock->info.window.ekey);
2021-11-02 05:37:31 +00:00
}
2022-03-29 12:07:38 +00:00
int32_t startPos = ascScan ? 0 : ( pSDataBlock - > info . rows - 1 ) ;
TSKEY ts = getStartTsKey ( & pSDataBlock - > info . window , tsCols , pSDataBlock - > info . rows , ascScan ) ;
2021-11-02 05:37:31 +00:00
2022-03-31 08:10:32 +00:00
STimeWindow win = getActiveTimeWindow ( pInfo - > aggSup . pResultBuf , pResultRowInfo , ts , & pInfo - > interval , pInfo - > interval . precision , & pInfo - > win ) ;
2022-03-29 07:24:25 +00:00
bool masterScan = true ;
2021-11-02 05:37:31 +00:00
SResultRow * pResult = NULL ;
2022-03-29 07:24:25 +00:00
int32_t ret = setResultOutputBufByKey_rv ( pResultRowInfo , pSDataBlock - > info . uid , & win , masterScan , & pResult ,
tableGroupId , pInfo - > binfo . pCtx , numOfOutput , pInfo - > binfo . rowCellInfoOffset ,
& pInfo - > aggSup , pTaskInfo ) ;
2021-11-02 05:37:31 +00:00
if ( ret ! = TSDB_CODE_SUCCESS | | pResult = = NULL ) {
2022-02-24 09:18:56 +00:00
longjmp ( pTaskInfo - > env , TSDB_CODE_QRY_OUT_OF_MEMORY ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 12:07:38 +00:00
if ( pInfo - > execModel = = OPTR_EXEC_MODEL_STREAM ) {
SResultRowPosition pos = { . pageId = pResult - > pageId , . offset = pResult - > offset } ;
taosArrayPush ( pUpdated , & pos ) ;
}
2021-11-02 05:37:31 +00:00
int32_t forwardStep = 0 ;
2022-02-24 09:18:56 +00:00
TSKEY ekey = win . ekey ;
2021-11-02 05:37:31 +00:00
forwardStep =
2022-02-24 09:18:56 +00:00
getNumOfRowsInTimeWindow ( & pSDataBlock - > info , tsCols , startPos , ekey , binarySearchForKey , NULL , TSDB_ORDER_ASC ) ;
2021-11-02 05:37:31 +00:00
// prev time window not interpolation yet.
int32_t curIndex = pResultRowInfo - > curPos ;
2022-03-31 08:10:32 +00:00
#if 0
2022-02-24 09:18:56 +00:00
if ( prevIndex ! = - 1 & & prevIndex < curIndex & & pInfo - > timeWindowInterpo ) {
2021-11-02 05:37:31 +00:00
for ( int32_t j = prevIndex ; j < curIndex ; + + j ) { // previous time window may be all closed already.
SResultRow * pRes = getResultRow ( pResultRowInfo , j ) ;
if ( pRes - > closed ) {
2022-03-29 12:07:38 +00:00
assert ( resultRowInterpolated ( pRes , RESULT_ROW_START_INTERP ) & & resultRowInterpolated ( pRes , RESULT_ROW_END_INTERP ) ) ;
2021-11-02 05:37:31 +00:00
continue ;
}
2022-03-29 07:24:25 +00:00
STimeWindow w = pRes - > win ;
ret = setResultOutputBufByKey_rv ( pResultRowInfo , pSDataBlock - > info . uid , & w , masterScan , & pResult , tableGroupId ,
pInfo - > binfo . pCtx , numOfOutput , pInfo - > binfo . rowCellInfoOffset , & pInfo - > aggSup ,
pTaskInfo ) ;
if ( ret ! = TSDB_CODE_SUCCESS ) {
longjmp ( pTaskInfo - > env , TSDB_CODE_QRY_OUT_OF_MEMORY ) ;
}
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
assert ( ! resultRowInterpolated ( pResult , RESULT_ROW_END_INTERP ) ) ;
doTimeWindowInterpolation ( pOperatorInfo , & pInfo - > binfo , pSDataBlock - > pDataBlock , * ( TSKEY * ) pInfo - > pRow [ 0 ] , - 1 ,
tsCols [ startPos ] , startPos , w . ekey , RESULT_ROW_END_INTERP ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
setResultRowInterpo ( pResult , RESULT_ROW_END_INTERP ) ;
setNotInterpoWindowKey ( pInfo - > binfo . pCtx , pOperatorInfo - > numOfOutput , RESULT_ROW_START_INTERP ) ;
2021-11-02 05:37:31 +00:00
2022-03-30 05:41:15 +00:00
doApplyFunctions ( pInfo - > binfo . pCtx , & w , & pInfo - > timeWindowData , startPos , 0 , tsCols , pSDataBlock - > info . rows , numOfOutput , TSDB_ORDER_ASC ) ;
2022-03-29 07:24:25 +00:00
}
2021-11-02 05:37:31 +00:00
// restore current time window
2022-03-29 07:24:25 +00:00
ret = setResultOutputBufByKey_rv ( pResultRowInfo , pSDataBlock - > info . uid , & win , masterScan , & pResult , tableGroupId ,
pInfo - > binfo . pCtx , numOfOutput , pInfo - > binfo . rowCellInfoOffset , & pInfo - > aggSup ,
pTaskInfo ) ;
2021-11-02 05:37:31 +00:00
if ( ret ! = TSDB_CODE_SUCCESS ) {
2022-02-24 09:18:56 +00:00
longjmp ( pTaskInfo - > env , TSDB_CODE_QRY_OUT_OF_MEMORY ) ;
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
// window start key interpolation
2022-04-02 05:32:26 +00:00
doWindowBorderInterpolation ( pOperatorInfo , pSDataBlock , pInfo - > binfo . pCtx , pResult , & win , startPos , forwardStep , pInfo - > order , false ) ;
updateTimeWindowInfo ( & pInfo - > timeWindowData , & win , true ) ;
2022-03-30 05:41:15 +00:00
doApplyFunctions ( pInfo - > binfo . pCtx , & win , & pInfo - > timeWindowData , startPos , forwardStep , tsCols , pSDataBlock - > info . rows , numOfOutput , TSDB_ORDER_ASC ) ;
2021-11-02 05:37:31 +00:00
STimeWindow nextWin = win ;
while ( 1 ) {
int32_t prevEndPos = ( forwardStep - 1 ) * step + startPos ;
2022-02-24 09:18:56 +00:00
startPos = getNextQualifiedWindow ( & pInfo - > interval , & nextWin , & pSDataBlock - > info , tsCols , prevEndPos , pInfo ) ;
2021-11-02 05:37:31 +00:00
if ( startPos < 0 ) {
break ;
}
// null data, failed to allocate more memory buffer
2022-03-29 07:24:25 +00:00
int32_t code = setResultOutputBufByKey_rv ( pResultRowInfo , pSDataBlock - > info . uid , & nextWin , masterScan , & pResult ,
tableGroupId , pInfo - > binfo . pCtx , numOfOutput ,
pInfo - > binfo . rowCellInfoOffset , & pInfo - > aggSup , pTaskInfo ) ;
2021-11-02 05:37:31 +00:00
if ( code ! = TSDB_CODE_SUCCESS | | pResult = = NULL ) {
2022-02-24 09:18:56 +00:00
longjmp ( pTaskInfo - > env , TSDB_CODE_QRY_OUT_OF_MEMORY ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 12:07:38 +00:00
if ( pInfo - > execModel = = OPTR_EXEC_MODEL_STREAM ) {
SResultRowPosition pos = { . pageId = pResult - > pageId , . offset = pResult - > offset } ;
taosArrayPush ( pUpdated , & pos ) ;
}
2022-03-29 07:24:25 +00:00
ekey = nextWin . ekey ; // reviseWindowEkey(pQueryAttr, &nextWin);
forwardStep =
getNumOfRowsInTimeWindow ( & pSDataBlock - > info , tsCols , startPos , ekey , binarySearchForKey , NULL , TSDB_ORDER_ASC ) ;
2021-11-02 05:37:31 +00:00
// window start(end) key interpolation
2022-04-02 05:32:26 +00:00
doWindowBorderInterpolation ( pOperatorInfo , pSDataBlock , pInfo - > binfo . pCtx , pResult , & nextWin , startPos , forwardStep , pInfo - > order , false ) ;
2022-04-07 05:57:47 +00:00
updateTimeWindowInfo ( & pInfo - > timeWindowData , & nextWin , true ) ;
2022-03-30 05:41:15 +00:00
doApplyFunctions ( pInfo - > binfo . pCtx , & nextWin , & pInfo - > timeWindowData , startPos , forwardStep , tsCols , pSDataBlock - > info . rows , numOfOutput , TSDB_ORDER_ASC ) ;
2021-11-02 05:37:31 +00:00
}
2022-02-24 09:18:56 +00:00
if ( pInfo - > timeWindowInterpo ) {
2022-03-29 12:07:38 +00:00
int32_t rowIndex = ascScan ? ( pSDataBlock - > info . rows - 1 ) : 0 ;
2022-02-24 09:18:56 +00:00
saveDataBlockLastRow ( pInfo - > pRow , pSDataBlock - > pDataBlock , rowIndex , pSDataBlock - > info . numOfCols ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 12:07:38 +00:00
return pUpdated ;
2022-03-29 07:24:25 +00:00
// updateResultRowInfoActiveIndex(pResultRowInfo, &pInfo->win, pRuntimeEnv->current->lastKey, true, false);
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static void hashAllIntervalAgg ( SOperatorInfo * pOperatorInfo , SResultRowInfo * pResultRowInfo , SSDataBlock * pSDataBlock ,
int32_t tableGroupId ) {
STableIntervalOperatorInfo * pInfo = ( STableIntervalOperatorInfo * ) pOperatorInfo - > info ;
2021-11-02 05:37:31 +00:00
2022-01-08 08:28:44 +00:00
STaskRuntimeEnv * pRuntimeEnv = pOperatorInfo - > pRuntimeEnv ;
2022-03-29 07:24:25 +00:00
int32_t numOfOutput = pOperatorInfo - > numOfOutput ;
2021-11-02 05:37:31 +00:00
2022-04-02 07:08:48 +00:00
int32_t step = 1 ; //GET_FORWARD_DIRECTION_FACTOR(pQueryAttr->order.order);
bool ascQuery = true ;
2021-11-02 05:37:31 +00:00
TSKEY * tsCols = NULL ;
if ( pSDataBlock - > pDataBlock ! = NULL ) {
SColumnInfoData * pColDataInfo = taosArrayGet ( pSDataBlock - > pDataBlock , 0 ) ;
2022-03-29 07:24:25 +00:00
tsCols = ( int64_t * ) pColDataInfo - > pData ;
2021-11-02 05:37:31 +00:00
assert ( tsCols [ 0 ] = = pSDataBlock - > info . window . skey & &
tsCols [ pSDataBlock - > info . rows - 1 ] = = pSDataBlock - > info . window . ekey ) ;
}
2022-03-29 07:24:25 +00:00
int32_t startPos = ascQuery ? 0 : ( pSDataBlock - > info . rows - 1 ) ;
TSKEY ts = getStartTsKey ( & pSDataBlock - > info . window , tsCols , pSDataBlock - > info . rows , ascQuery ) ;
2021-11-02 05:37:31 +00:00
2022-04-02 07:08:48 +00:00
STimeWindow win = { 0 } ; //getCurrentActiveTimeWindow(pResultRowInfo, ts, pQueryAttr);
2022-03-29 07:24:25 +00:00
bool masterScan = IS_MAIN_SCAN ( pRuntimeEnv ) ;
2021-11-02 05:37:31 +00:00
SResultRow * pResult = NULL ;
2022-03-29 07:24:25 +00:00
int32_t forwardStep = 0 ;
int32_t ret = 0 ;
2021-11-02 05:37:31 +00:00
STimeWindow preWin = win ;
while ( 1 ) {
// null data, failed to allocate more memory buffer
ret = setResultOutputBufByKey ( pRuntimeEnv , pResultRowInfo , pSDataBlock - > info . uid , & win , masterScan , & pResult ,
2022-02-24 09:18:56 +00:00
tableGroupId , pInfo - > binfo . pCtx , numOfOutput , pInfo - > binfo . rowCellInfoOffset ) ;
2021-11-02 05:37:31 +00:00
if ( ret ! = TSDB_CODE_SUCCESS ) {
longjmp ( pRuntimeEnv - > env , TSDB_CODE_QRY_OUT_OF_MEMORY ) ;
}
2022-04-02 07:08:48 +00:00
TSKEY ekey = 0 ; //reviseWindowEkey(pQueryAttr, &win);
2022-03-29 07:24:25 +00:00
// forwardStep = getNumOfRowsInTimeWindow(pRuntimeEnv, &pSDataBlock->info, tsCols, startPos, ekey,
// binarySearchForKey, true);
2021-11-02 05:37:31 +00:00
// window start(end) key interpolation
2022-03-29 07:24:25 +00:00
// doWindowBorderInterpolation(pOperatorInfo, pSDataBlock, pInfo->binfo.pCtx, pResult, &win, startPos,
// forwardStep); doApplyFunctions(pRuntimeEnv, pInfo->binfo.pCtx, ascQuery ? &win : &preWin, startPos,
// forwardStep, tsCols, pSDataBlock->info.rows, numOfOutput);
2021-11-02 05:37:31 +00:00
preWin = win ;
int32_t prevEndPos = ( forwardStep - 1 ) * step + startPos ;
2022-03-29 07:24:25 +00:00
// startPos = getNextQualifiedWindow(pQueryAttr, &win, &pSDataBlock->info, tsCols, binarySearchForKey,
// prevEndPos);
2021-11-02 05:37:31 +00:00
if ( startPos < 0 ) {
2022-04-02 07:08:48 +00:00
// if ((ascQuery && win.skey <= pQueryAttr->window.ekey) || ((!ascQuery) && win.ekey >= pQueryAttr->window.ekey)) {
// int32_t code =
// setResultOutputBufByKey(pRuntimeEnv, pResultRowInfo, pSDataBlock->info.uid, &win, masterScan, &pResult,
// tableGroupId, pInfo->binfo.pCtx, numOfOutput, pInfo->binfo.rowCellInfoOffset);
// if (code != TSDB_CODE_SUCCESS || pResult == NULL) {
// longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY);
// }
//
// startPos = pSDataBlock->info.rows - 1;
2021-11-02 05:37:31 +00:00
// window start(end) key interpolation
2022-03-29 07:24:25 +00:00
// doWindowBorderInterpolation(pOperatorInfo, pSDataBlock, pInfo->binfo.pCtx, pResult, &win, startPos,
// forwardStep); doApplyFunctions(pRuntimeEnv, pInfo->binfo.pCtx, ascQuery ? &win : &preWin, startPos,
// forwardStep, tsCols, pSDataBlock->info.rows, numOfOutput);
2022-04-02 07:08:48 +00:00
// }
2021-11-02 05:37:31 +00:00
break ;
}
setResultRowInterpo ( pResult , RESULT_ROW_END_INTERP ) ;
}
2022-04-02 07:08:48 +00:00
// if (pQueryAttr->timeWindowInterpo) {
// int32_t rowIndex = ascQuery ? (pSDataBlock->info.rows - 1) : 0;
2022-03-29 07:24:25 +00:00
// saveDataBlockLastRow(pRuntimeEnv, &pSDataBlock->info, pSDataBlock->pDataBlock, rowIndex);
2022-04-02 07:08:48 +00:00
// }
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// updateResultRowInfoActiveIndex(pResultRowInfo, pQueryAttr, pRuntimeEnv->current->lastKey);
2021-11-02 05:37:31 +00:00
}
2022-04-02 05:32:26 +00:00
static void doKeepTuple ( SSessionAggOperatorInfo * pInfo , int64_t ts ) {
pInfo - > curWindow . ekey = ts ;
pInfo - > prevTs = ts ;
pInfo - > numOfRows + = 1 ;
}
static void doKeepSessionStartInfo ( SSessionAggOperatorInfo * pInfo , const int64_t * tsList , int32_t rowIndex ) {
pInfo - > start = rowIndex ;
pInfo - > numOfRows = 0 ;
pInfo - > curWindow . skey = tsList [ rowIndex ] ;
}
2022-03-29 06:36:08 +00:00
// todo handle multiple tables cases.
2022-03-29 07:24:25 +00:00
static void doSessionWindowAggImpl ( SOperatorInfo * pOperator , SSessionAggOperatorInfo * pInfo , SSDataBlock * pBlock ) {
2022-03-28 12:11:02 +00:00
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2021-11-02 05:37:31 +00:00
2022-03-29 06:36:08 +00:00
SColumnInfoData * pColInfoData = taosArrayGet ( pBlock - > pDataBlock , 0 ) ;
2021-11-02 05:37:31 +00:00
2022-04-02 05:32:26 +00:00
bool masterScan = true ;
int32_t numOfOutput = pOperator - > numOfOutput ;
int64_t gid = pBlock - > info . groupId ;
2021-11-02 05:37:31 +00:00
2022-03-28 12:11:02 +00:00
int64_t gap = pInfo - > gap ;
2021-11-02 05:37:31 +00:00
pInfo - > numOfRows = 0 ;
2022-04-02 05:32:26 +00:00
if ( ! pInfo - > reptScan ) {
2021-11-02 05:37:31 +00:00
pInfo - > reptScan = true ;
2022-04-02 05:32:26 +00:00
pInfo - > prevTs = INT64_MIN ;
2021-11-02 05:37:31 +00:00
}
2022-04-02 05:32:26 +00:00
// In case of ascending or descending order scan data, only one time window needs to be kepted for each table.
2021-11-02 05:37:31 +00:00
TSKEY * tsList = ( TSKEY * ) pColInfoData - > pData ;
2022-03-29 06:36:08 +00:00
for ( int32_t j = 0 ; j < pBlock - > info . rows ; + + j ) {
2021-11-02 05:37:31 +00:00
if ( pInfo - > prevTs = = INT64_MIN ) {
2022-04-02 05:32:26 +00:00
doKeepSessionStartInfo ( pInfo , tsList , j ) ;
doKeepTuple ( pInfo , tsList [ j ] ) ;
2021-11-02 05:37:31 +00:00
} else if ( tsList [ j ] - pInfo - > prevTs < = gap & & ( tsList [ j ] - pInfo - > prevTs ) > = 0 ) {
2022-04-02 05:32:26 +00:00
// The gap is less than the threshold, so it belongs to current session window that has been opened already.
doKeepTuple ( pInfo , tsList [ j ] ) ;
2021-11-02 05:37:31 +00:00
if ( j = = 0 & & pInfo - > start ! = 0 ) {
pInfo - > start = 0 ;
}
} else { // start a new session window
SResultRow * pResult = NULL ;
2022-04-02 05:32:26 +00:00
// keep the time window for the closed time window.
STimeWindow window = pInfo - > curWindow ;
2021-11-02 05:37:31 +00:00
pInfo - > curWindow . ekey = pInfo - > curWindow . skey ;
2022-03-29 07:24:25 +00:00
int32_t ret = setResultOutputBufByKey_rv ( & pInfo - > binfo . resultRowInfo , pBlock - > info . uid , & window , masterScan ,
2022-04-02 05:32:26 +00:00
& pResult , gid , pInfo - > binfo . pCtx , numOfOutput , pInfo - > binfo . rowCellInfoOffset , & pInfo - > aggSup , pTaskInfo ) ;
2021-11-02 05:37:31 +00:00
if ( ret ! = TSDB_CODE_SUCCESS ) { // null data, too many state code
2022-03-29 06:36:08 +00:00
longjmp ( pTaskInfo - > env , TSDB_CODE_QRY_APP_ERROR ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 06:36:08 +00:00
// pInfo->numOfRows data belong to the current session window
2022-04-02 05:32:26 +00:00
updateTimeWindowInfo ( & pInfo - > timeWindowData , & window , false ) ;
doApplyFunctions ( pInfo - > binfo . pCtx , & window , & pInfo - > timeWindowData , pInfo - > start , pInfo - > numOfRows , NULL , pBlock - > info . rows , numOfOutput , TSDB_ORDER_ASC ) ;
2021-11-02 05:37:31 +00:00
2022-04-02 05:32:26 +00:00
// here we start a new session window
doKeepSessionStartInfo ( pInfo , tsList , j ) ;
doKeepTuple ( pInfo , tsList [ j ] ) ;
2021-11-02 05:37:31 +00:00
}
}
SResultRow * pResult = NULL ;
2022-04-02 05:32:26 +00:00
pInfo - > curWindow . ekey = tsList [ pBlock - > info . rows - 1 ] ;
int32_t ret = setResultOutputBufByKey_rv ( & pInfo - > binfo . resultRowInfo , pBlock - > info . uid , & pInfo - > curWindow , masterScan , & pResult ,
gid , pInfo - > binfo . pCtx , numOfOutput , pInfo - > binfo . rowCellInfoOffset , & pInfo - > aggSup , pTaskInfo ) ;
2021-11-02 05:37:31 +00:00
if ( ret ! = TSDB_CODE_SUCCESS ) { // null data, too many state code
2022-03-29 06:36:08 +00:00
longjmp ( pTaskInfo - > env , TSDB_CODE_QRY_APP_ERROR ) ;
2021-11-02 05:37:31 +00:00
}
2022-04-02 05:32:26 +00:00
updateTimeWindowInfo ( & pInfo - > timeWindowData , & pInfo - > curWindow , false ) ;
doApplyFunctions ( pInfo - > binfo . pCtx , & pInfo - > curWindow , & pInfo - > timeWindowData , pInfo - > start , pInfo - > numOfRows , NULL , pBlock - > info . rows , numOfOutput , TSDB_ORDER_ASC ) ;
2021-11-02 05:37:31 +00:00
}
static void setResultRowKey ( SResultRow * pResultRow , char * pData , int16_t type ) {
if ( IS_VAR_DATA_TYPE ( type ) ) {
if ( pResultRow - > key = = NULL ) {
2022-03-25 16:29:53 +00:00
pResultRow - > key = taosMemoryMalloc ( varDataTLen ( pData ) ) ;
2021-11-02 05:37:31 +00:00
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 ) ;
pResultRow - > win . skey = v ;
pResultRow - > win . ekey = v ;
}
}
2022-04-04 06:54:39 +00:00
int32_t setGroupResultOutputBuf_rv ( SOptrBasicInfo * binfo , int32_t numOfCols , char * pData , int16_t type ,
2022-03-29 07:24:25 +00:00
int16_t bytes , int32_t groupId , SDiskbasedBuf * pBuf , SExecTaskInfo * pTaskInfo ,
SAggSupporter * pAggSup ) {
SResultRowInfo * pResultRowInfo = & binfo - > resultRowInfo ;
SqlFunctionCtx * pCtx = binfo - > pCtx ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
SResultRow * pResultRow = doSetResultOutBufByKey_rv ( pBuf , pResultRowInfo , groupId , ( char * ) pData , bytes , true , groupId ,
2022-04-01 02:44:34 +00:00
pTaskInfo , false , pAggSup ) ;
2022-03-29 07:24:25 +00:00
assert ( pResultRow ! = NULL ) ;
2021-11-02 05:37:31 +00:00
setResultRowKey ( pResultRow , pData , type ) ;
2022-04-01 02:44:34 +00:00
setResultRowOutputBufInitCtx_rv ( pResultRow , pCtx , numOfCols , binfo - > rowCellInfoOffset ) ;
2021-11-02 05:37:31 +00:00
return TSDB_CODE_SUCCESS ;
}
2022-03-29 07:24:25 +00:00
static bool functionNeedToExecute ( SqlFunctionCtx * pCtx ) {
struct SResultRowEntryInfo * pResInfo = GET_RES_INFO ( pCtx ) ;
2021-11-02 05:37:31 +00:00
// in case of timestamp column, always generated results.
int32_t functionId = pCtx - > functionId ;
2022-03-22 08:03:42 +00:00
if ( functionId = = - 1 ) {
return false ;
}
2022-03-30 05:41:15 +00:00
if ( isRowEntryCompleted ( pResInfo ) ) {
2021-11-02 05:37:31 +00:00
return false ;
}
if ( functionId = = FUNCTION_FIRST_DST | | functionId = = FUNCTION_FIRST ) {
2022-03-29 07:24:25 +00:00
// return QUERY_IS_ASC_QUERY(pQueryAttr);
2021-11-02 05:37:31 +00:00
}
// denote the order type
if ( ( functionId = = FUNCTION_LAST_DST | | functionId = = FUNCTION_LAST ) ) {
2022-03-29 07:24:25 +00:00
// return pCtx->param[0].i == pQueryAttr->order.order;
2021-11-02 05:37:31 +00:00
}
// in the reverse table scan, only the following functions need to be executed
2022-03-29 07:24:25 +00:00
// if (IS_REVERSE_SCAN(pRuntimeEnv) ||
// (pRuntimeEnv->scanFlag == REPEAT_SCAN && functionId != FUNCTION_STDDEV && functionId != FUNCTION_PERCT)) {
// return false;
// }
2021-11-02 05:37:31 +00:00
return true ;
}
2022-03-29 07:24:25 +00:00
void setBlockStatisInfo ( SqlFunctionCtx * pCtx , SSDataBlock * pSDataBlock , SColumn * pColumn ) {
SColumnDataAgg * pAgg = NULL ;
2021-11-02 05:37:31 +00:00
2022-01-20 09:10:28 +00:00
if ( pSDataBlock - > pBlockAgg ! = NULL & & TSDB_COL_IS_NORMAL_COL ( pColumn - > flag ) ) {
pAgg = & pSDataBlock - > pBlockAgg [ pCtx - > columnIndex ] ;
2021-11-02 05:37:31 +00:00
pCtx - > agg = * pAgg ;
2022-03-29 07:24:25 +00:00
pCtx - > isAggSet = true ;
2021-11-02 05:37:31 +00:00
assert ( pCtx - > agg . numOfNull < = pSDataBlock - > info . rows ) ;
} else {
pCtx - > isAggSet = false ;
}
2022-01-20 09:10:28 +00:00
pCtx - > hasNull = hasNull ( pColumn , pAgg ) ;
2021-11-02 05:37:31 +00:00
// set the statistics data for primary time stamp column
2022-03-09 02:22:53 +00:00
if ( pCtx - > functionId = = FUNCTION_SPREAD & & pColumn - > colId = = PRIMARYKEY_TIMESTAMP_COL_ID ) {
2022-03-29 07:24:25 +00:00
pCtx - > isAggSet = true ;
2021-11-02 05:37:31 +00:00
pCtx - > agg . min = pSDataBlock - > info . window . skey ;
pCtx - > agg . max = pSDataBlock - > info . window . ekey ;
}
}
// set the output buffer for the selectivity + tag query
2022-03-29 07:24:25 +00:00
static int32_t setCtxTagColumnInfo ( SqlFunctionCtx * pCtx , int32_t numOfOutput ) {
2021-11-02 05:37:31 +00:00
if ( ! isSelectivityWithTagsQuery ( pCtx , numOfOutput ) ) {
return TSDB_CODE_SUCCESS ;
}
int32_t num = 0 ;
int16_t tagLen = 0 ;
2022-01-28 02:44:02 +00:00
SqlFunctionCtx * p = NULL ;
2022-03-25 16:29:53 +00:00
SqlFunctionCtx * * pTagCtx = taosMemoryCalloc ( numOfOutput , POINTER_BYTES ) ;
2021-11-02 05:37:31 +00:00
if ( pTagCtx = = NULL ) {
return TSDB_CODE_QRY_OUT_OF_MEMORY ;
}
for ( int32_t i = 0 ; i < numOfOutput ; + + i ) {
int32_t functionId = pCtx [ i ] . functionId ;
if ( functionId = = FUNCTION_TAG_DUMMY | | functionId = = FUNCTION_TS_DUMMY ) {
2021-11-02 07:08:00 +00:00
tagLen + = pCtx [ i ] . resDataInfo . bytes ;
2021-11-02 05:37:31 +00:00
pTagCtx [ num + + ] = & pCtx [ i ] ;
2022-03-29 07:24:25 +00:00
} else if ( 1 /*(aAggs[functionId].status & FUNCSTATE_SELECTIVITY) != 0*/ ) {
2021-11-02 05:37:31 +00:00
p = & pCtx [ i ] ;
} else if ( functionId = = FUNCTION_TS | | functionId = = FUNCTION_TAG ) {
// tag function may be the group by tag column
// ts may be the required primary timestamp column
continue ;
} else {
// the column may be the normal column, group by normal_column, the functionId is FUNCTION_PRJ
}
}
if ( p ! = NULL ) {
2022-03-10 03:12:44 +00:00
p - > subsidiaryRes . pCtx = pTagCtx ;
p - > subsidiaryRes . numOfCols = num ;
p - > subsidiaryRes . bufLen = tagLen ;
2021-11-02 05:37:31 +00:00
} else {
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pTagCtx ) ;
2021-11-02 05:37:31 +00:00
}
return TSDB_CODE_SUCCESS ;
}
2022-03-12 14:59:12 +00:00
static SqlFunctionCtx * createSqlFunctionCtx_rv ( SExprInfo * pExprInfo , int32_t numOfOutput , int32_t * * rowCellInfoOffset ) {
2022-03-29 07:24:25 +00:00
SqlFunctionCtx * pFuncCtx = ( SqlFunctionCtx * ) taosMemoryCalloc ( numOfOutput , sizeof ( SqlFunctionCtx ) ) ;
2022-01-14 02:20:55 +00:00
if ( pFuncCtx = = NULL ) {
return NULL ;
}
2022-03-25 16:29:53 +00:00
* rowCellInfoOffset = taosMemoryCalloc ( numOfOutput , sizeof ( int32_t ) ) ;
2022-01-14 02:20:55 +00:00
if ( * rowCellInfoOffset = = 0 ) {
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pFuncCtx ) ;
2022-01-14 02:20:55 +00:00
return NULL ;
}
for ( int32_t i = 0 ; i < numOfOutput ; + + i ) {
2022-03-12 14:59:12 +00:00
SExprInfo * pExpr = & pExprInfo [ i ] ;
2022-01-20 05:52:46 +00:00
2022-03-29 07:24:25 +00:00
SExprBasicInfo * pFunct = & pExpr - > base ;
2022-01-28 02:44:02 +00:00
SqlFunctionCtx * pCtx = & pFuncCtx [ i ] ;
2022-01-20 09:10:28 +00:00
2022-03-26 14:13:14 +00:00
pCtx - > functionId = - 1 ;
2022-03-26 14:17:26 +00:00
if ( pExpr - > pExpr - > nodeType = = QUERY_NODE_FUNCTION ) {
2022-03-18 10:02:00 +00:00
SFuncExecEnv env = { 0 } ;
2022-03-22 08:03:42 +00:00
pCtx - > functionId = pExpr - > pExpr - > _function . pFunctNode - > funcId ;
2022-04-04 06:54:39 +00:00
if ( fmIsAggFunc ( pCtx - > functionId ) | | fmIsNonstandardSQLFunc ( pCtx - > functionId ) ) {
2022-03-27 14:43:07 +00:00
fmGetFuncExecFuncs ( pCtx - > functionId , & pCtx - > fpSet ) ;
pCtx - > fpSet . getEnv ( pExpr - > pExpr - > _function . pFunctNode , & env ) ;
} else {
fmGetScalarFuncExecFuncs ( pCtx - > functionId , & pCtx - > sfp ) ;
2022-03-30 05:41:15 +00:00
if ( pCtx - > sfp . getEnv ! = NULL ) {
pCtx - > sfp . getEnv ( pExpr - > pExpr - > _function . pFunctNode , & env ) ;
}
2022-03-27 14:43:07 +00:00
}
2022-03-18 10:02:00 +00:00
pCtx - > resDataInfo . interBufSize = env . calcMemSize ;
2022-04-06 07:20:38 +00:00
} else if ( pExpr - > pExpr - > nodeType = = QUERY_NODE_COLUMN | | pExpr - > pExpr - > nodeType = = QUERY_NODE_OPERATOR | | pExpr - > pExpr - > nodeType = = QUERY_NODE_VALUE ) {
2022-04-01 02:44:34 +00:00
pCtx - > resDataInfo . interBufSize = pFunct - > resSchema . bytes ; // for simple column, the intermediate buffer needs to hold one element.
2022-03-18 10:02:00 +00:00
}
2022-01-14 02:20:55 +00:00
2022-03-18 10:02:00 +00:00
pCtx - > input . numOfInputCols = pFunct - > numOfParams ;
2022-03-25 16:29:53 +00:00
pCtx - > input . pData = taosMemoryCalloc ( pFunct - > numOfParams , POINTER_BYTES ) ;
pCtx - > input . pColumnDataAgg = taosMemoryCalloc ( pFunct - > numOfParams , POINTER_BYTES ) ;
2022-01-14 02:20:55 +00:00
2022-04-06 03:01:09 +00:00
pCtx - > pTsOutput = NULL ; //taosArrayInit(4, POINTER_BYTES);
2022-03-29 07:24:25 +00:00
pCtx - > resDataInfo . bytes = pFunct - > resSchema . bytes ;
2022-04-06 02:09:14 +00:00
pCtx - > resDataInfo . type = pFunct - > resSchema . type ;
pCtx - > order = TSDB_ORDER_ASC ;
2022-03-29 07:24:25 +00:00
pCtx - > start . key = INT64_MIN ;
2022-04-06 02:09:14 +00:00
pCtx - > end . key = INT64_MIN ;
2022-03-09 02:22:53 +00:00
#if 0
2022-01-14 02:20:55 +00:00
for ( int32_t j = 0 ; j < pCtx - > numOfParams ; + + j ) {
2022-03-09 02:22:53 +00:00
// int16_t type = pFunct->param[j].nType;
// int16_t bytes = pFunct->param[j].nLen;
2022-01-14 02:20:55 +00:00
2022-03-09 02:22:53 +00:00
// if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) {
// taosVariantCreateFromBinary(&pCtx->param[j], pFunct->param[j].pz, bytes, type);
// } else {
// taosVariantCreateFromBinary(&pCtx->param[j], (char *)&pFunct->param[j].i, bytes, type);
// }
2022-01-14 02:20:55 +00:00
}
// set the order information for top/bottom query
int32_t functionId = pCtx - > functionId ;
if ( functionId = = FUNCTION_TOP | | functionId = = FUNCTION_BOTTOM | | functionId = = FUNCTION_DIFF ) {
int32_t f = getExprFunctionId ( & pExpr [ 0 ] ) ;
assert ( f = = FUNCTION_TS | | f = = FUNCTION_TS_DUMMY ) ;
// pCtx->param[2].i = pQueryAttr->order.order;
pCtx - > param [ 2 ] . nType = TSDB_DATA_TYPE_BIGINT ;
pCtx - > param [ 3 ] . i = functionId ;
pCtx - > param [ 3 ] . nType = TSDB_DATA_TYPE_BIGINT ;
// pCtx->param[1].i = pQueryAttr->order.col.info.colId;
} else if ( functionId = = FUNCTION_INTERP ) {
// pCtx->param[2].i = (int8_t)pQueryAttr->fillType;
// if (pQueryAttr->fillVal != NULL) {
// if (isNull((const char *)&pQueryAttr->fillVal[i], pCtx->inputType)) {
// pCtx->param[1].nType = TSDB_DATA_TYPE_NULL;
// } else { // todo refactor, taosVariantCreateFromBinary should handle the NULL value
// if (pCtx->inputType != TSDB_DATA_TYPE_BINARY && pCtx->inputType != TSDB_DATA_TYPE_NCHAR) {
// taosVariantCreateFromBinary(&pCtx->param[1], (char *)&pQueryAttr->fillVal[i], pCtx->inputBytes, pCtx->inputType);
// }
// }
// }
} else if ( functionId = = FUNCTION_TS_COMP ) {
// pCtx->param[0].i = pQueryAttr->vgId; //TODO this should be the parameter from client
pCtx - > param [ 0 ] . nType = TSDB_DATA_TYPE_BIGINT ;
} else if ( functionId = = FUNCTION_TWA ) {
// pCtx->param[1].i = pQueryAttr->window.skey;
pCtx - > param [ 1 ] . nType = TSDB_DATA_TYPE_BIGINT ;
// pCtx->param[2].i = pQueryAttr->window.ekey;
pCtx - > param [ 2 ] . nType = TSDB_DATA_TYPE_BIGINT ;
} else if ( functionId = = FUNCTION_ARITHM ) {
// pCtx->param[1].pz = (char*) getScalarFuncSupport(pRuntimeEnv->scalarSup, i);
}
2022-03-09 02:22:53 +00:00
# endif
2022-01-14 02:20:55 +00:00
}
2022-03-29 07:24:25 +00:00
for ( int32_t i = 1 ; i < numOfOutput ; + + i ) {
( * rowCellInfoOffset ) [ i ] =
( int32_t ) ( ( * rowCellInfoOffset ) [ i - 1 ] + sizeof ( SResultRowEntryInfo ) + pFuncCtx [ i - 1 ] . resDataInfo . interBufSize ) ;
2022-01-20 09:10:28 +00:00
}
2022-01-14 02:20:55 +00:00
setCtxTagColumnInfo ( pFuncCtx , numOfOutput ) ;
return pFuncCtx ;
}
2022-03-10 03:12:44 +00:00
static void * destroySqlFunctionCtx ( SqlFunctionCtx * pCtx , int32_t numOfOutput ) {
2021-11-02 05:37:31 +00:00
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 ] ) ;
}
taosVariantDestroy ( & pCtx [ i ] . tag ) ;
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pCtx [ i ] . subsidiaryRes . pCtx ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pCtx ) ;
2021-11-02 05:37:31 +00:00
return NULL ;
}
2022-01-08 08:28:44 +00:00
static void doFreeQueryHandle ( STaskRuntimeEnv * pRuntimeEnv ) {
STaskAttr * pQueryAttr = pRuntimeEnv - > pQueryAttr ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// tsdbCleanupReadHandle(pRuntimeEnv->pTsdbReadHandle);
2022-01-10 11:48:21 +00:00
pRuntimeEnv - > pTsdbReadHandle = NULL ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// SMemRef* pMemRef = &pQueryAttr->memRef;
// assert(pMemRef->ref == 0 && pMemRef->snapshot.imem == NULL && pMemRef->snapshot.mem == NULL);
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static void destroyTsComp ( STaskRuntimeEnv * pRuntimeEnv , STaskAttr * pQueryAttr ) {
if ( pQueryAttr - > tsCompQuery & & pRuntimeEnv - > outputBuf & & pRuntimeEnv - > outputBuf - > pDataBlock & &
taosArrayGetSize ( pRuntimeEnv - > outputBuf - > pDataBlock ) > 0 ) {
2021-11-02 05:37:31 +00:00
SColumnInfoData * pColInfoData = taosArrayGet ( pRuntimeEnv - > outputBuf - > pDataBlock , 0 ) ;
if ( pColInfoData ) {
2022-03-29 07:24:25 +00:00
TdFilePtr pFile = * ( TdFilePtr * ) pColInfoData - > pData ; // TODO refactor
2022-03-03 16:04:57 +00:00
if ( pFile ! = NULL ) {
taosCloseFile ( & pFile ) ;
2022-03-29 07:24:25 +00:00
* ( TdFilePtr * ) pColInfoData - > pData = NULL ;
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
static bool isCachedLastQuery ( STaskAttr * pQueryAttr ) {
2021-11-02 05:37:31 +00:00
for ( int32_t i = 0 ; i < pQueryAttr - > numOfOutput ; + + i ) {
int32_t functionId = getExprFunctionId ( & pQueryAttr - > pExpr1 [ i ] ) ;
if ( functionId = = FUNCTION_LAST | | functionId = = FUNCTION_LAST_DST ) {
continue ;
}
return false ;
}
if ( pQueryAttr - > order . order ! = TSDB_ORDER_DESC | | ! TSWINDOW_IS_EQUAL ( pQueryAttr - > window , TSWINDOW_DESC_INITIALIZER ) ) {
return false ;
}
if ( pQueryAttr - > groupbyColumn ) {
return false ;
}
if ( pQueryAttr - > interval . interval > 0 ) {
return false ;
}
if ( pQueryAttr - > numOfFilterCols > 0 | | pQueryAttr - > havingNum > 0 ) {
return false ;
}
return true ;
}
/////////////////////////////////////////////////////////////////////////////////////////////
2022-03-29 07:24:25 +00:00
// todo refactor : return window
void getAlignQueryTimeWindow ( SInterval * pInterval , int32_t precision , int64_t key , int64_t keyFirst , int64_t keyLast ,
STimeWindow * win ) {
2022-03-25 06:28:01 +00:00
ASSERT ( key > = keyFirst & & key < = keyLast ) ;
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-02-24 09:18:56 +00:00
if ( keyFirst > ( INT64_MAX - pInterval - > interval ) ) {
assert ( keyLast - keyFirst < pInterval - > interval ) ;
2021-11-02 05:37:31 +00:00
win - > ekey = INT64_MAX ;
} else {
2022-03-25 06:28:01 +00:00
win - > ekey = taosTimeAdd ( win - > skey , pInterval - > interval , pInterval - > intervalUnit , precision ) - 1 ;
2021-11-02 05:37:31 +00:00
}
}
2022-03-29 07:24:25 +00:00
static int32_t updateBlockLoadStatus ( STaskAttr * pQuery , int32_t status ) {
2021-11-02 05:37:31 +00:00
bool hasFirstLastFunc = false ;
bool hasOtherFunc = false ;
if ( status = = BLK_DATA_ALL_NEEDED | | status = = BLK_DATA_DISCARD ) {
return status ;
}
for ( int32_t i = 0 ; i < pQuery - > numOfOutput ; + + i ) {
int32_t functionId = getExprFunctionId ( & pQuery - > pExpr1 [ i ] ) ;
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 ;
}
}
if ( hasFirstLastFunc & & status = = BLK_DATA_NO_NEEDED ) {
2022-03-29 07:24:25 +00:00
if ( ! hasOtherFunc ) {
2021-11-02 05:37:31 +00:00
return BLK_DATA_DISCARD ;
} else {
return BLK_DATA_ALL_NEEDED ;
}
}
return status ;
}
2022-01-08 08:28:44 +00:00
static void doUpdateLastKey ( STaskAttr * pQueryAttr ) {
2021-11-02 05:37:31 +00:00
STimeWindow * win = & pQueryAttr - > window ;
size_t num = taosArrayGetSize ( pQueryAttr - > tableGroupInfo . pGroupList ) ;
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < num ; + + i ) {
2021-11-02 05:37:31 +00:00
SArray * p1 = taosArrayGetP ( pQueryAttr - > tableGroupInfo . pGroupList , i ) ;
size_t len = taosArrayGetSize ( p1 ) ;
2022-03-29 07:24:25 +00:00
for ( int32_t j = 0 ; j < len ; + + j ) {
// STableKeyInfo* pInfo = taosArrayGet(p1, j);
//
// // update the new lastkey if it is equalled to the value of the old skey
// if (pInfo->lastKey == win->ekey) {
// pInfo->lastKey = win->skey;
// }
2021-11-02 05:37:31 +00:00
}
}
}
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) {
// 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->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) {
// 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->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);
//
// TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey, TSKEY);
// 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);
//
// TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey, TSKEY);
// 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
//
// TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey, TSKEY);
// 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
//
// TSWAP(pQueryAttr->window.skey, pQueryAttr->window.ekey, TSKEY);
// doUpdateLastKey(pQueryAttr);
// }
//
// pQueryAttr->order.order = TSDB_ORDER_DESC;
// pQueryAttr->needReverseScan = false;
// }
// }
// }
//}
2021-11-02 05:37:31 +00:00
2022-01-08 08:28:44 +00:00
static void getIntermediateBufInfo ( STaskRuntimeEnv * pRuntimeEnv , int32_t * ps , int32_t * rowsize ) {
STaskAttr * pQueryAttr = pRuntimeEnv - > pQueryAttr ;
2022-03-29 07:24:25 +00:00
int32_t MIN_ROWS_PER_PAGE = 4 ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
* rowsize = ( int32_t ) ( pQueryAttr - > resultRowSize *
getRowNumForMultioutput ( pQueryAttr , pQueryAttr - > topBotQuery , pQueryAttr - > stableQuery ) ) ;
2021-11-02 05:37:31 +00:00
int32_t overhead = sizeof ( SFilePage ) ;
// one page contains at least two rows
* ps = DEFAULT_INTERN_BUF_PAGE_SIZE ;
2022-03-29 07:24:25 +00:00
while ( ( ( * rowsize ) * MIN_ROWS_PER_PAGE ) > ( * ps ) - overhead ) {
2021-11-02 05:37:31 +00:00
* ps = ( ( * ps ) < < 1u ) ;
}
}
# define IS_PREFILTER_TYPE(_t) ((_t) != TSDB_DATA_TYPE_BINARY && (_t) != TSDB_DATA_TYPE_NCHAR)
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);
// }
2021-11-02 05:37:31 +00:00
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
if ( QUERY_IS_ASC_QUERY ( pQueryAttr ) ) {
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-03-29 07:24:25 +00:00
static int32_t doTSJoinFilter ( STaskRuntimeEnv * pRuntimeEnv , TSKEY key , bool ascQuery ) {
2021-11-02 05:37:31 +00:00
STSElem elem = tsBufGetElem ( pRuntimeEnv - > pTsBuf ) ;
# if defined(_DEBUG_VIEW)
2022-03-29 07:24:25 +00:00
printf ( " elem in comp ts file:% " PRId64 " , key:% " PRId64 " , tag:% " PRIu64
" , query order:%d, ts order:%d, traverse:%d, index:%d \n " ,
2021-11-02 05:37:31 +00:00
elem . ts , key , elem . tag . i , pQueryAttr - > order . order , pRuntimeEnv - > pTsBuf - > tsOrder ,
pRuntimeEnv - > pTsBuf - > cur . order , pRuntimeEnv - > pTsBuf - > cur . tsIndex ) ;
# endif
if ( ascQuery ) {
if ( key < elem . ts ) {
return TS_JOIN_TS_NOT_EQUALS ;
} else if ( key > elem . ts ) {
longjmp ( pRuntimeEnv - > env , TSDB_CODE_QRY_INCONSISTAN ) ;
}
} else {
if ( key > elem . ts ) {
return TS_JOIN_TS_NOT_EQUALS ;
} else if ( key < elem . ts ) {
longjmp ( pRuntimeEnv - > env , TSDB_CODE_QRY_INCONSISTAN ) ;
}
}
return TS_JOIN_TS_EQUAL ;
}
bool doFilterDataBlock ( SSingleColumnFilterInfo * pFilterInfo , int32_t numOfFilterCols , int32_t numOfRows , int8_t * p ) {
bool all = true ;
for ( int32_t i = 0 ; i < numOfRows ; + + i ) {
bool qualified = false ;
for ( int32_t k = 0 ; k < numOfFilterCols ; + + k ) {
char * pElem = ( char * ) pFilterInfo [ k ] . pData + pFilterInfo [ k ] . info . bytes * i ;
qualified = false ;
for ( int32_t j = 0 ; j < pFilterInfo [ k ] . numOfFilters ; + + j ) {
SColumnFilterElem * pFilterElem = NULL ;
2022-03-29 07:24:25 +00:00
// SColumnFilterElem* pFilterElem = &pFilterInfo[k].pFilters[j];
2021-11-02 05:37:31 +00:00
bool isnull = isNull ( pElem , pFilterInfo [ k ] . info . type ) ;
if ( isnull ) {
2022-03-29 07:24:25 +00:00
// if (pFilterElem->fp == isNullOperator) {
// qualified = true;
// break;
// } else {
// continue;
// }
2021-11-02 05:37:31 +00:00
} else {
2022-03-29 07:24:25 +00:00
// if (pFilterElem->fp == notNullOperator) {
// qualified = true;
// break;
// } else if (pFilterElem->fp == isNullOperator) {
// continue;
// }
2021-11-02 05:37:31 +00:00
}
if ( pFilterElem - > fp ( pFilterElem , pElem , pElem , pFilterInfo [ k ] . info . type ) ) {
qualified = true ;
break ;
}
}
if ( ! qualified ) {
break ;
}
}
p [ i ] = qualified ? 1 : 0 ;
if ( ! qualified ) {
all = false ;
}
}
return all ;
}
void doCompactSDataBlock ( SSDataBlock * pBlock , int32_t numOfRows , int8_t * p ) {
int32_t len = 0 ;
int32_t start = 0 ;
for ( int32_t j = 0 ; j < numOfRows ; + + j ) {
if ( p [ j ] = = 1 ) {
len + + ;
} else {
if ( len > 0 ) {
int32_t cstart = j - len ;
for ( int32_t i = 0 ; i < pBlock - > info . numOfCols ; + + i ) {
SColumnInfoData * pColumnInfoData = taosArrayGet ( pBlock - > pDataBlock , i ) ;
int16_t bytes = pColumnInfoData - > info . bytes ;
memmove ( ( ( char * ) pColumnInfoData - > pData ) + start * bytes , pColumnInfoData - > pData + cstart * bytes ,
len * bytes ) ;
}
start + = len ;
len = 0 ;
}
}
}
if ( len > 0 ) {
int32_t cstart = numOfRows - len ;
for ( int32_t i = 0 ; i < pBlock - > info . numOfCols ; + + i ) {
SColumnInfoData * pColumnInfoData = taosArrayGet ( pBlock - > pDataBlock , i ) ;
int16_t bytes = pColumnInfoData - > info . bytes ;
memmove ( pColumnInfoData - > pData + start * bytes , pColumnInfoData - > pData + cstart * bytes , len * bytes ) ;
}
start + = len ;
len = 0 ;
}
pBlock - > info . rows = start ;
pBlock - > pBlockAgg = NULL ; // clean the block statistics info
if ( start > 0 ) {
SColumnInfoData * pColumnInfoData = taosArrayGet ( pBlock - > pDataBlock , 0 ) ;
if ( pColumnInfoData - > info . type = = TSDB_DATA_TYPE_TIMESTAMP & &
pColumnInfoData - > info . colId = = PRIMARYKEY_TIMESTAMP_COL_ID ) {
pBlock - > info . window . skey = * ( int64_t * ) pColumnInfoData - > pData ;
pBlock - > info . window . ekey = * ( int64_t * ) ( pColumnInfoData - > pData + TSDB_KEYSIZE * ( start - 1 ) ) ;
}
}
}
2022-01-08 08:28:44 +00:00
void filterRowsInDataBlock ( STaskRuntimeEnv * pRuntimeEnv , SSingleColumnFilterInfo * pFilterInfo , int32_t numOfFilterCols ,
2021-11-02 05:37:31 +00:00
SSDataBlock * pBlock , bool ascQuery ) {
int32_t numOfRows = pBlock - > info . rows ;
2022-03-29 07:24:25 +00:00
int8_t * p = taosMemoryCalloc ( numOfRows , sizeof ( int8_t ) ) ;
2021-11-02 05:37:31 +00:00
bool all = true ;
2022-02-13 06:34:00 +00:00
#if 0
2021-11-02 05:37:31 +00:00
if ( pRuntimeEnv - > pTsBuf ! = NULL ) {
SColumnInfoData * pColInfoData = taosArrayGet ( pBlock - > pDataBlock , 0 ) ;
TSKEY * k = ( TSKEY * ) pColInfoData - > pData ;
for ( int32_t i = 0 ; i < numOfRows ; + + i ) {
int32_t offset = ascQuery ? i : ( numOfRows - i - 1 ) ;
int32_t ret = doTSJoinFilter ( pRuntimeEnv , k [ offset ] , ascQuery ) ;
if ( ret = = TS_JOIN_TAG_NOT_EQUALS ) {
break ;
} else if ( ret = = TS_JOIN_TS_NOT_EQUALS ) {
all = false ;
continue ;
} else {
assert ( ret = = TS_JOIN_TS_EQUAL ) ;
p [ offset ] = true ;
}
if ( ! tsBufNextPos ( pRuntimeEnv - > pTsBuf ) ) {
break ;
}
}
// save the cursor status
pRuntimeEnv - > current - > cur = tsBufGetCursor ( pRuntimeEnv - > pTsBuf ) ;
} else {
all = doFilterDataBlock ( pFilterInfo , numOfFilterCols , numOfRows , p ) ;
}
2022-02-13 06:34:00 +00:00
# endif
2021-11-02 05:37:31 +00:00
if ( ! all ) {
doCompactSDataBlock ( pBlock , numOfRows , p ) ;
}
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( p ) ;
2021-11-02 05:37:31 +00:00
}
2022-01-08 08:28:44 +00:00
void filterColRowsInDataBlock ( STaskRuntimeEnv * pRuntimeEnv , SSDataBlock * pBlock , bool ascQuery ) {
2022-03-29 07:24:25 +00:00
int32_t numOfRows = pBlock - > info . rows ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
int8_t * p = NULL ;
bool all = true ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
if ( pRuntimeEnv - > pTsBuf ! = NULL ) {
SColumnInfoData * pColInfoData = taosArrayGet ( pBlock - > pDataBlock , 0 ) ;
p = taosMemoryCalloc ( numOfRows , sizeof ( int8_t ) ) ;
TSKEY * k = ( TSKEY * ) pColInfoData - > pData ;
for ( int32_t i = 0 ; i < numOfRows ; + + i ) {
int32_t offset = ascQuery ? i : ( numOfRows - i - 1 ) ;
int32_t ret = doTSJoinFilter ( pRuntimeEnv , k [ offset ] , ascQuery ) ;
if ( ret = = TS_JOIN_TAG_NOT_EQUALS ) {
break ;
} else if ( ret = = TS_JOIN_TS_NOT_EQUALS ) {
all = false ;
continue ;
} else {
assert ( ret = = TS_JOIN_TS_EQUAL ) ;
p [ offset ] = true ;
}
if ( ! tsBufNextPos ( pRuntimeEnv - > pTsBuf ) ) {
break ;
}
}
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// save the cursor status
// pRuntimeEnv->current->cur = tsBufGetCursor(pRuntimeEnv->pTsBuf);
} else {
// all = filterExecute(pRuntimeEnv->pQueryAttr->pFilters, numOfRows, &p, pBlock->pBlockAgg,
// pRuntimeEnv->pQueryAttr->numOfCols);
}
if ( ! all ) {
if ( p ) {
doCompactSDataBlock ( pBlock , numOfRows , p ) ;
} else {
pBlock - > info . rows = 0 ;
pBlock - > pBlockAgg = NULL ; // clean the block statistics info
}
}
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
taosMemoryFreeClear ( p ) ;
2021-11-02 05:37:31 +00:00
}
static SColumnInfo * doGetTagColumnInfoById ( SColumnInfo * pTagColList , int32_t numOfTags , int16_t colId ) ;
2022-03-29 07:24:25 +00:00
static void doSetTagValueInParam ( void * pTable , int32_t tagColId , SVariant * tag , int16_t type , int16_t bytes ) ;
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-03-29 07:24:25 +00:00
uint32_t status = BLK_DATA_NO_NEEDED ;
2021-11-02 05:37:31 +00:00
int32_t numOfOutput = pTableScanInfo - > numOfOutput ;
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 ) {
status | = BLK_DATA_ALL_NEEDED ;
return status ;
} else {
2022-03-29 07:24:25 +00:00
// status |= aAggs[functionId].dataReqFunc(&pTableScanInfo->pCtx[i], &pBlock->info.window, colId);
// if ((status & BLK_DATA_ALL_NEEDED) == BLK_DATA_ALL_NEEDED) {
// 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 ) {
2021-11-02 05:37:31 +00:00
* status = BLK_DATA_NO_NEEDED ;
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
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
if ( pRuntimeEnv - > pTsBuf ! = NULL ) {
( * status ) = BLK_DATA_ALL_NEEDED ;
if ( pQueryAttr - > stableQuery ) { // todo refactor
SExprInfo * pExprInfo = & pTableScanInfo - > pExpr [ 0 ] ;
int16_t tagId = ( int16_t ) pExprInfo - > base . param [ 0 ] . i ;
SColumnInfo * pColInfo = doGetTagColumnInfoById ( pQueryAttr - > tagColList , pQueryAttr - > numOfTags , tagId ) ;
// compare tag first
SVariant t = { 0 } ;
doSetTagValueInParam ( pRuntimeEnv - > current - > pTable , tagId , & t , pColInfo - > type , pColInfo - > bytes ) ;
setTimestampListJoinInfo ( pRuntimeEnv , & t , pRuntimeEnv - > current ) ;
STSElem elem = tsBufGetElem ( pRuntimeEnv - > pTsBuf ) ;
if ( ! tsBufIsValidElem ( & elem ) | | ( tsBufIsValidElem ( & elem ) & & ( taosVariantCompare ( & t , elem . tag ) ! = 0 ) ) ) {
( * status ) = BLK_DATA_DISCARD ;
return TSDB_CODE_SUCCESS ;
}
}
}
// 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 ) ) ) {
2021-11-02 05:37:31 +00:00
( * status ) = BLK_DATA_ALL_NEEDED ;
}
// check if this data block is required to load
if ( ( * status ) ! = BLK_DATA_ALL_NEEDED ) {
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 ,
pTableScanInfo - > rowCellInfoOffset ) ;
} else {
if ( setResultOutputBufByKey ( pRuntimeEnv , pTableScanInfo - > pResultRowInfo , pBlock - > info . uid , & win , masterScan , & pResult , groupId ,
pTableScanInfo - > pCtx , pTableScanInfo - > numOfOutput ,
pTableScanInfo - > rowCellInfoOffset ) ! = TSDB_CODE_SUCCESS ) {
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 ,
pTableScanInfo - > rowCellInfoOffset , pTableScanInfo - > numOfOutput ,
pRuntimeEnv - > current - > groupIndex ) ;
}
if ( needFilter ) {
( * status ) = doFilterByBlockTimeWindow ( pTableScanInfo , pBlock ) ;
} else {
( * status ) = BLK_DATA_ALL_NEEDED ;
}
}
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
if ( ( * status ) = = BLK_DATA_NO_NEEDED | | ( * status ) = = BLK_DATA_DISCARD ) {
//qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId, pBlockInfo->window.skey,
// pBlockInfo->window.ekey, pBlockInfo->rows);
pCost - > discardBlocks + = 1 ;
} else if ( ( * status ) = = BLK_DATA_STATIS_NEEDED ) {
// this function never returns error?
pCost - > loadBlockStatis + = 1 ;
2022-01-10 11:48:21 +00:00
// tsdbRetrieveDataBlockStatisInfo(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 {
assert ( ( * status ) = = BLK_DATA_ALL_NEEDED ) ;
// load the data block statistics to perform further filter
pCost - > loadBlockStatis + = 1 ;
2022-01-10 11:48:21 +00:00
// tsdbRetrieveDataBlockStatisInfo(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 ,
pTableScanInfo - > rowCellInfoOffset ) ! = TSDB_CODE_SUCCESS ) {
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
pCost - > discardBlocks + = 1 ;
//qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId,
// pBlockInfo->window.skey, pBlockInfo->window.ekey, pBlockInfo->rows);
( * status ) = BLK_DATA_DISCARD ;
return TSDB_CODE_SUCCESS ;
}
}
}
}
// current block has been discard due to filter applied
// if (!doFilterByBlockStatistics(pRuntimeEnv, pBlock->pBlockAgg, pTableScanInfo->pCtx, pBlockInfo->rows)) {
// pCost->discardBlocks += 1;
// qDebug("QInfo:0x%"PRIx64" data block discard, brange:%" PRId64 "-%" PRId64 ", rows:%d", pQInfo->qId, pBlockInfo->window.skey,
// pBlockInfo->window.ekey, pBlockInfo->rows);
// (*status) = BLK_DATA_DISCARD;
// 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) {
// filterSetColFieldData(pQueryAttr->pFilters, pBlock->info.numOfCols, pBlock->pDataBlock);
// }
// 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
int32_t binarySearchForKey ( char * pValue , int num , TSKEY key , int order ) {
2021-11-02 05:37:31 +00:00
int32_t midPos = - 1 ;
int32_t numOfRows ;
if ( num < = 0 ) {
return - 1 ;
}
assert ( order = = TSDB_ORDER_ASC | | order = = TSDB_ORDER_DESC ) ;
2022-03-29 07:24:25 +00:00
TSKEY * keyList = ( TSKEY * ) pValue ;
2021-11-02 05:37:31 +00:00
int32_t firstPos = 0 ;
int32_t lastPos = num - 1 ;
if ( order = = TSDB_ORDER_DESC ) {
// find the first position which is smaller than the key
while ( 1 ) {
if ( key > = keyList [ lastPos ] ) return lastPos ;
if ( key = = keyList [ firstPos ] ) return firstPos ;
if ( key < keyList [ firstPos ] ) return firstPos - 1 ;
numOfRows = lastPos - firstPos + 1 ;
midPos = ( numOfRows > > 1 ) + firstPos ;
if ( key < keyList [ midPos ] ) {
lastPos = midPos - 1 ;
} else if ( key > keyList [ midPos ] ) {
firstPos = midPos + 1 ;
} else {
break ;
}
}
} else {
// find the first position which is bigger than the key
while ( 1 ) {
if ( key < = keyList [ firstPos ] ) return firstPos ;
if ( key = = keyList [ lastPos ] ) return lastPos ;
if ( key > keyList [ lastPos ] ) {
lastPos = lastPos + 1 ;
if ( lastPos > = num )
return - 1 ;
else
return lastPos ;
}
numOfRows = lastPos - firstPos + 1 ;
midPos = ( numOfRows > > 1u ) + firstPos ;
if ( key < keyList [ midPos ] ) {
lastPos = midPos - 1 ;
} else if ( key > keyList [ midPos ] ) {
firstPos = midPos + 1 ;
} else {
break ;
}
}
}
return midPos ;
}
/*
2022-01-28 02:44:02 +00:00
* set tag value in SqlFunctionCtx
2021-11-02 05:37:31 +00:00
* e . g . , tag information into input buffer
*/
2022-03-29 07:24:25 +00:00
static void doSetTagValueInParam ( void * pTable , int32_t tagColId , SVariant * tag , int16_t type , int16_t bytes ) {
2021-11-02 05:37:31 +00:00
taosVariantDestroy ( tag ) ;
char * val = NULL ;
2022-03-29 07:24:25 +00:00
// if (tagColId == TSDB_TBNAME_COLUMN_INDEX) {
// val = tsdbGetTableName(pTable);
// assert(val != NULL);
// } else {
// val = tsdbGetTableTagVal(pTable, tagColId, type, bytes);
// }
2021-11-02 05:37:31 +00:00
if ( val = = NULL | | isNull ( val , type ) ) {
tag - > nType = TSDB_DATA_TYPE_NULL ;
return ;
}
if ( type = = TSDB_DATA_TYPE_BINARY | | type = = TSDB_DATA_TYPE_NCHAR ) {
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
taosVariantCreateFromBinary ( tag , varDataVal ( val ) , len , type ) ;
2022-03-29 07:24:25 +00:00
// taosVariantCreateFromBinary(tag, varDataVal(val), varDataLen(val), type);
2021-11-02 05:37:31 +00:00
} else {
taosVariantCreateFromBinary ( tag , val , bytes , type ) ;
}
}
static SColumnInfo * doGetTagColumnInfoById ( SColumnInfo * pTagColList , int32_t numOfTags , int16_t colId ) {
assert ( pTagColList ! = NULL & & numOfTags > 0 ) ;
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < numOfTags ; + + i ) {
2021-11-02 05:37:31 +00:00
if ( pTagColList [ i ] . colId = = colId ) {
return & pTagColList [ i ] ;
}
}
return NULL ;
}
2022-03-29 07:24:25 +00:00
void setTagValue ( SOperatorInfo * pOperatorInfo , void * pTable , SqlFunctionCtx * pCtx , int32_t numOfOutput ) {
2022-01-08 08:28:44 +00:00
STaskRuntimeEnv * pRuntimeEnv = pOperatorInfo - > pRuntimeEnv ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
SExprInfo * pExpr = pOperatorInfo - > pExpr ;
STaskAttr * pQueryAttr = pRuntimeEnv - > pQueryAttr ;
2021-11-02 05:37:31 +00:00
SExprInfo * pExprInfo = & pExpr [ 0 ] ;
2022-03-29 07:24:25 +00:00
int32_t functionId = getExprFunctionId ( pExprInfo ) ;
2021-11-02 05:37:31 +00:00
if ( pQueryAttr - > numOfOutput = = 1 & & functionId = = FUNCTION_TS_COMP & & pQueryAttr - > stableQuery ) {
assert ( pExprInfo - > base . numOfParams = = 1 ) ;
2022-03-29 07:24:25 +00:00
// int16_t tagColId = (int16_t)pExprInfo->base.param[0].i;
2022-03-09 02:22:53 +00:00
int16_t tagColId = - 1 ;
2021-11-02 05:37:31 +00:00
SColumnInfo * pColInfo = doGetTagColumnInfoById ( pQueryAttr - > tagColList , pQueryAttr - > numOfTags , tagColId ) ;
doSetTagValueInParam ( pTable , tagColId , & pCtx [ 0 ] . tag , pColInfo - > type , pColInfo - > bytes ) ;
2022-03-09 02:22:53 +00:00
2021-11-02 05:37:31 +00:00
} else {
// set tag value, by which the results are aggregated.
int32_t offset = 0 ;
memset ( pRuntimeEnv - > tagVal , 0 , pQueryAttr - > tagLen ) ;
for ( int32_t idx = 0 ; idx < numOfOutput ; + + idx ) {
SExprInfo * pLocalExprInfo = & pExpr [ idx ] ;
// ts_comp column required the tag value for join filter
2022-03-09 02:22:53 +00:00
if ( ! TSDB_COL_IS_TAG ( pLocalExprInfo - > base . pParam [ 0 ] . pCol - > flag ) ) {
2021-11-02 05:37:31 +00:00
continue ;
}
// todo use tag column index to optimize performance
2022-03-29 07:24:25 +00:00
doSetTagValueInParam ( pTable , pLocalExprInfo - > base . pParam [ 0 ] . pCol - > colId , & pCtx [ idx ] . tag ,
pLocalExprInfo - > base . resSchema . type , pLocalExprInfo - > base . resSchema . bytes ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
if ( IS_NUMERIC_TYPE ( pLocalExprInfo - > base . resSchema . type ) | |
pLocalExprInfo - > base . resSchema . type = = TSDB_DATA_TYPE_BOOL | |
pLocalExprInfo - > base . resSchema . type = = TSDB_DATA_TYPE_TIMESTAMP ) {
2021-11-02 05:37:31 +00:00
memcpy ( pRuntimeEnv - > tagVal + offset , & pCtx [ idx ] . tag . i , pLocalExprInfo - > base . resSchema . bytes ) ;
} else {
if ( pCtx [ idx ] . tag . pz ! = NULL ) {
memcpy ( pRuntimeEnv - > tagVal + offset , pCtx [ idx ] . tag . pz , pCtx [ idx ] . tag . nLen ) ;
2022-03-29 07:24:25 +00:00
}
2021-11-02 05:37:31 +00:00
}
offset + = pLocalExprInfo - > base . resSchema . bytes ;
}
}
// set the tsBuf start position before check each data block
if ( pRuntimeEnv - > pTsBuf ! = NULL ) {
setCtxTagForJoin ( pRuntimeEnv , & pCtx [ 0 ] , pExprInfo , pTable ) ;
}
}
2022-02-14 05:20:46 +00:00
void copyToSDataBlock ( SSDataBlock * pBlock , int32_t * offset , SGroupResInfo * pGroupResInfo , SDiskbasedBuf * pResBuf ) {
2021-11-02 05:37:31 +00:00
pBlock - > info . rows = 0 ;
int32_t code = TSDB_CODE_SUCCESS ;
while ( pGroupResInfo - > currentGroup < pGroupResInfo - > totalGroup ) {
// all results in current group have been returned to client, try next group
if ( ( pGroupResInfo - > pRows = = NULL ) | | taosArrayGetSize ( pGroupResInfo - > pRows ) = = 0 ) {
assert ( pGroupResInfo - > index = = 0 ) ;
2022-03-29 07:24:25 +00:00
// if ((code = mergeIntoGroupResult(&pGroupResInfo, pRuntimeEnv, offset)) != TSDB_CODE_SUCCESS) {
return ;
// }
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
// doCopyToSDataBlock(pResBuf, pGroupResInfo, TSDB_ORDER_ASC, pBlock, );
2021-11-02 05:37:31 +00:00
// current data are all dumped to result buffer, clear it
if ( ! hasRemainDataInCurrentGroup ( pGroupResInfo ) ) {
cleanupGroupResInfo ( pGroupResInfo ) ;
if ( ! incNextGroup ( pGroupResInfo ) ) {
break ;
}
}
// enough results in data buffer, return
2022-03-29 07:24:25 +00:00
// if (pBlock->info.rows >= threshold) {
// break;
// }
2021-11-02 05:37:31 +00:00
}
}
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-03-29 07:24:25 +00:00
// TSWAP(pTableQueryInfo->win.skey, pTableQueryInfo->win.ekey, TSKEY);
// 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
SResultRowInfo * pResultRowInfo = & pTableQueryInfo - > resInfo ;
if ( pResultRowInfo - > size > 0 ) {
pResultRowInfo - > curPos = pResultRowInfo - > size - 1 ;
} else {
pResultRowInfo - > curPos = - 1 ;
}
}
2022-03-31 08:10:32 +00:00
void initResultRow ( SResultRow * pResultRow ) {
2021-11-02 05:37:31 +00:00
pResultRow - > pEntryInfo = ( struct SResultRowEntryInfo * ) ( ( char * ) pResultRow + sizeof ( SResultRow ) ) ;
}
/*
* 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-03-09 02:22:53 +00:00
void setFunctionResultOutput ( SOptrBasicInfo * pInfo , SAggSupporter * pSup , int32_t stage , SExecTaskInfo * pTaskInfo ) {
2022-03-29 07:24:25 +00:00
SqlFunctionCtx * pCtx = pInfo - > pCtx ;
SSDataBlock * pDataBlock = pInfo - > pRes ;
int32_t * rowCellInfoOffset = pInfo - > rowCellInfoOffset ;
2022-03-31 09:59:41 +00:00
2022-01-20 05:52:46 +00:00
SResultRowInfo * pResultRowInfo = & pInfo - > resultRowInfo ;
2022-03-31 09:59:41 +00:00
initResultRowInfo ( pResultRowInfo , 16 ) ;
2022-01-20 05:52:46 +00:00
2022-03-29 07:24:25 +00:00
int64_t tid = 0 ;
int64_t groupId = 0 ;
SResultRow * pRow = doSetResultOutBufByKey_rv ( pSup - > pResultBuf , pResultRowInfo , tid , ( char * ) & tid , sizeof ( tid ) , true ,
groupId , pTaskInfo , false , pSup ) ;
2022-01-20 05:52:46 +00:00
for ( int32_t i = 0 ; i < pDataBlock - > info . numOfCols ; + + i ) {
struct SResultRowEntryInfo * pEntry = getResultCell ( pRow , i , rowCellInfoOffset ) ;
cleanupResultRowEntry ( pEntry ) ;
2022-03-29 07:24:25 +00:00
pCtx [ i ] . resultInfo = pEntry ;
2022-01-20 05:52:46 +00:00
pCtx [ i ] . currentStage = stage ;
// set the timestamp output buffer for top/bottom/diff query
2022-03-29 07:24:25 +00:00
// int32_t fid = pCtx[i].functionId;
// if (fid == FUNCTION_TOP || fid == FUNCTION_BOTTOM || fid == FUNCTION_DIFF || fid == FUNCTION_DERIVATIVE) {
2022-04-06 02:09:14 +00:00
// if (i > 0) pCtx[i].pTsOutput = pCtx[i-1].pOutput;
2022-03-29 07:24:25 +00:00
// }
2022-01-20 05:52:46 +00:00
}
initCtxOutputBuffer ( pCtx , pDataBlock - > info . numOfCols ) ;
}
2022-03-29 07:24:25 +00:00
void updateOutputBuf ( SOptrBasicInfo * pBInfo , int32_t * bufCapacity , int32_t numOfInputRows ) {
2021-11-02 05:37:31 +00:00
SSDataBlock * pDataBlock = pBInfo - > pRes ;
2022-03-29 07:24:25 +00:00
int32_t newSize = pDataBlock - > info . rows + numOfInputRows + 5 ; // extra output buffer
2021-11-02 05:37:31 +00:00
if ( ( * bufCapacity ) < newSize ) {
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < pDataBlock - > info . numOfCols ; + + i ) {
SColumnInfoData * pColInfo = taosArrayGet ( pDataBlock - > pDataBlock , i ) ;
2021-11-02 05:37:31 +00:00
2022-03-25 16:29:53 +00:00
char * p = taosMemoryRealloc ( pColInfo - > pData , newSize * pColInfo - > info . bytes ) ;
2021-11-02 05:37:31 +00:00
if ( p ! = NULL ) {
pColInfo - > pData = p ;
// it starts from the tail of the previously generated results.
pBInfo - > pCtx [ i ] . pOutput = pColInfo - > pData ;
( * bufCapacity ) = newSize ;
} else {
// longjmp
}
}
}
for ( int32_t i = 0 ; i < pDataBlock - > info . numOfCols ; + + i ) {
2022-03-29 07:24:25 +00:00
SColumnInfoData * pColInfo = taosArrayGet ( pDataBlock - > pDataBlock , i ) ;
2021-11-02 05:37:31 +00:00
pBInfo - > pCtx [ i ] . pOutput = pColInfo - > pData + pColInfo - > info . bytes * pDataBlock - > info . rows ;
// set the correct pointer after the memory buffer reallocated.
int32_t functionId = pBInfo - > pCtx [ i ] . functionId ;
2022-03-29 07:24:25 +00:00
if ( functionId = = FUNCTION_TOP | | functionId = = FUNCTION_BOTTOM | | functionId = = FUNCTION_DIFF | |
functionId = = FUNCTION_DERIVATIVE ) {
2022-04-06 02:09:14 +00:00
// if (i > 0) pBInfo->pCtx[i].pTsOutput = pBInfo->pCtx[i - 1].pOutput;
2021-11-02 05:37:31 +00:00
}
}
}
2022-01-28 02:44:02 +00:00
void copyTsColoum ( SSDataBlock * pRes , SqlFunctionCtx * pCtx , int32_t numOfOutput ) {
2021-11-02 05:37:31 +00:00
bool needCopyTs = false ;
int32_t tsNum = 0 ;
2022-03-29 07:24:25 +00:00
char * src = NULL ;
2021-11-02 05:37:31 +00:00
for ( int32_t i = 0 ; i < numOfOutput ; i + + ) {
int32_t functionId = pCtx [ i ] . functionId ;
if ( functionId = = FUNCTION_DIFF | | functionId = = FUNCTION_DERIVATIVE ) {
needCopyTs = true ;
2022-03-29 07:24:25 +00:00
if ( i > 0 & & pCtx [ i - 1 ] . functionId = = FUNCTION_TS_DUMMY ) {
SColumnInfoData * pColRes = taosArrayGet ( pRes - > pDataBlock , i - 1 ) ; // find ts data
2021-11-02 05:37:31 +00:00
src = pColRes - > pData ;
}
2022-03-29 07:24:25 +00:00
} else if ( functionId = = FUNCTION_TS_DUMMY ) {
2021-11-02 05:37:31 +00:00
tsNum + + ;
}
}
if ( ! needCopyTs ) return ;
if ( tsNum < 2 ) return ;
if ( src = = NULL ) return ;
for ( int32_t i = 0 ; i < numOfOutput ; i + + ) {
int32_t functionId = pCtx [ i ] . functionId ;
2022-03-29 07:24:25 +00:00
if ( functionId = = FUNCTION_TS_DUMMY ) {
2021-11-02 05:37:31 +00:00
SColumnInfoData * pColRes = taosArrayGet ( pRes - > pDataBlock , i ) ;
memcpy ( pColRes - > pData , src , pColRes - > info . bytes * pRes - > info . rows ) ;
}
}
}
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-06 02:09:14 +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-15 06:37:26 +00:00
void finalizeQueryResult ( SqlFunctionCtx * pCtx , int32_t numOfOutput ) {
for ( int32_t j = 0 ; j < numOfOutput ; + + j ) {
2022-03-22 08:03:42 +00:00
if ( pCtx [ j ] . functionId = = - 1 ) {
continue ;
}
2022-03-15 06:37:26 +00:00
pCtx [ j ] . fpSet . finalize ( & pCtx [ j ] ) ;
}
}
2022-03-29 07:24:25 +00:00
void finalizeMultiTupleQueryResult ( SqlFunctionCtx * pCtx , int32_t numOfOutput , SDiskbasedBuf * pBuf ,
SResultRowInfo * pResultRowInfo , int32_t * rowCellInfoOffset ) {
2022-03-15 06:37:26 +00:00
for ( int32_t i = 0 ; i < pResultRowInfo - > size ; + + i ) {
SResultRowPosition * pPos = & pResultRowInfo - > pPosition [ i ] ;
2022-03-29 07:24:25 +00:00
SFilePage * bufPage = getBufPage ( pBuf , pPos - > pageId ) ;
2022-03-15 06:37:26 +00:00
SResultRow * pRow = ( SResultRow * ) ( ( char * ) bufPage + pPos - > offset ) ;
2022-03-31 08:10:32 +00:00
// TODO ignore the close status anyway.
// if (!isResultRowClosed(pRow)) {
// continue;
// }
2022-03-15 06:37:26 +00:00
2021-11-02 05:37:31 +00:00
for ( int32_t j = 0 ; j < numOfOutput ; + + j ) {
2022-03-15 06:37:26 +00:00
pCtx [ j ] . resultInfo = getResultCell ( pRow , j , rowCellInfoOffset ) ;
2022-03-15 08:51:50 +00:00
struct SResultRowEntryInfo * pResInfo = pCtx [ j ] . resultInfo ;
2022-03-15 06:37:26 +00:00
if ( isRowEntryCompleted ( pResInfo ) & & isRowEntryInitialized ( pResInfo ) ) {
continue ;
}
2022-03-31 08:10:32 +00:00
if ( pCtx [ j ] . fpSet . process ) { // TODO set the dummy function, to avoid the check for null ptr.
2022-03-22 08:03:42 +00:00
pCtx [ j ] . fpSet . finalize ( & pCtx [ j ] ) ;
}
2022-03-15 06:37:26 +00:00
if ( pRow - > numOfRows < pResInfo - > numOfRes ) {
pRow - > numOfRows = pResInfo - > numOfRes ;
}
2021-11-02 05:37:31 +00:00
}
2022-03-15 06:37:26 +00:00
releaseBufPage ( pBuf , bufPage ) ;
}
2021-11-02 05:37:31 +00:00
}
2022-03-29 12:07:38 +00:00
void finalizeUpdatedResult ( SqlFunctionCtx * pCtx , int32_t numOfOutput , SDiskbasedBuf * pBuf , SArray * pUpdateList ,
int32_t * rowCellInfoOffset ) {
size_t num = taosArrayGetSize ( pUpdateList ) ;
for ( int32_t i = 0 ; i < num ; + + i ) {
SResultRowPosition * pPos = taosArrayGet ( pUpdateList , i ) ;
SFilePage * bufPage = getBufPage ( pBuf , pPos - > pageId ) ;
SResultRow * pRow = ( SResultRow * ) ( ( char * ) bufPage + pPos - > offset ) ;
for ( int32_t j = 0 ; j < numOfOutput ; + + j ) {
pCtx [ j ] . resultInfo = getResultCell ( pRow , j , rowCellInfoOffset ) ;
struct SResultRowEntryInfo * pResInfo = pCtx [ j ] . resultInfo ;
if ( isRowEntryCompleted ( pResInfo ) & & isRowEntryInitialized ( pResInfo ) ) {
continue ;
}
if ( pCtx [ j ] . fpSet . process ) { // TODO set the dummy function.
pCtx [ j ] . fpSet . finalize ( & pCtx [ j ] ) ;
2022-03-30 11:38:30 +00:00
pResInfo - > initialized = true ;
2022-03-29 12:07:38 +00:00
}
if ( pRow - > numOfRows < pResInfo - > numOfRes ) {
pRow - > numOfRows = pResInfo - > numOfRes ;
}
}
releaseBufPage ( pBuf , bufPage ) ;
/*
* set the number of output results for group by normal columns , the number of output rows usually is 1 except
* the top and bottom query
*/
// buf->numOfRows = (uint16_t)getNumOfResult(pCtx, numOfOutput);
}
}
2022-03-29 07:24:25 +00:00
static bool hasMainOutput ( STaskAttr * pQueryAttr ) {
2021-11-02 05:37:31 +00:00
for ( int32_t i = 0 ; i < pQueryAttr - > numOfOutput ; + + i ) {
int32_t functionId = getExprFunctionId ( & pQueryAttr - > pExpr1 [ i ] ) ;
if ( functionId ! = FUNCTION_TS & & functionId ! = FUNCTION_TAG & & functionId ! = FUNCTION_TAGPRJ ) {
return true ;
}
}
return false ;
}
2022-03-29 07:24:25 +00:00
STableQueryInfo * createTableQueryInfo ( void * buf , bool groupbyColumn , STimeWindow win ) {
STableQueryInfo * pTableQueryInfo = buf ;
2021-11-02 05:37:31 +00:00
pTableQueryInfo - > lastKey = win . skey ;
// set more initial size of interval/groupby query
2022-03-29 07:24:25 +00:00
// if (/*QUERY_IS_INTERVAL_QUERY(pQueryAttr) || */groupbyColumn) {
int32_t initialSize = 128 ;
int32_t code = initResultRowInfo ( & pTableQueryInfo - > resInfo , initialSize ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
return NULL ;
}
// } else { // in other aggregate query, do not initialize the windowResInfo
// }
2021-11-02 05:37:31 +00:00
return pTableQueryInfo ;
}
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);
2021-11-02 05:37:31 +00:00
cleanupResultRowInfo ( & pTableQueryInfo - > resInfo ) ;
}
2022-03-29 07:24:25 +00:00
void setResultRowOutputBufInitCtx ( STaskRuntimeEnv * pRuntimeEnv , SResultRow * pResult , SqlFunctionCtx * pCtx ,
int32_t numOfOutput , int32_t * rowCellInfoOffset ) {
2021-11-02 05:37:31 +00:00
// Note: pResult->pos[i]->num == 0, there is only fixed number of results for each group
2022-02-10 05:49:17 +00:00
SFilePage * bufPage = getBufPage ( pRuntimeEnv - > pResultBuf , pResult - > pageId ) ;
2021-11-02 05:37:31 +00:00
int32_t offset = 0 ;
for ( int32_t i = 0 ; i < numOfOutput ; + + i ) {
pCtx [ i ] . resultInfo = getResultCell ( pResult , i , rowCellInfoOffset ) ;
struct SResultRowEntryInfo * pResInfo = pCtx [ i ] . resultInfo ;
if ( isRowEntryCompleted ( pResInfo ) & & isRowEntryInitialized ( pResInfo ) ) {
2021-11-02 07:08:00 +00:00
offset + = pCtx [ i ] . resDataInfo . bytes ;
2021-11-02 05:37:31 +00:00
continue ;
}
pCtx [ i ] . pOutput = getPosInResultPage ( pRuntimeEnv - > pQueryAttr , bufPage , pResult - > offset , offset ) ;
2021-11-02 07:08:00 +00:00
offset + = pCtx [ i ] . resDataInfo . bytes ;
2021-11-02 05:37:31 +00:00
int32_t functionId = pCtx [ i ] . functionId ;
if ( functionId < 0 ) {
continue ;
}
if ( functionId = = FUNCTION_TOP | | functionId = = FUNCTION_BOTTOM | | functionId = = FUNCTION_DIFF ) {
2022-04-06 02:09:14 +00:00
// if (i > 0) pCtx[i].pTsOutput = pCtx[i - 1].pOutput;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
// if (!pResInfo->initialized) {
// aAggs[functionId].init(&pCtx[i], pResInfo);
// }
2021-11-02 05:37:31 +00:00
}
}
2022-04-01 02:44:34 +00:00
void setResultRowOutputBufInitCtx_rv ( SResultRow * pResult , SqlFunctionCtx * pCtx , int32_t numOfOutput , int32_t * rowCellInfoOffset ) {
2022-02-14 02:58:58 +00:00
for ( int32_t i = 0 ; i < numOfOutput ; + + i ) {
pCtx [ i ] . resultInfo = getResultCell ( pResult , i , rowCellInfoOffset ) ;
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-03-22 08:03:42 +00:00
if ( ! pResInfo - > initialized & & pCtx [ i ] . functionId ! = - 1 ) {
2022-03-15 06:37:26 +00:00
pCtx [ i ] . fpSet . init ( & pCtx [ i ] , pResInfo ) ;
2022-02-14 02:58:58 +00:00
}
}
}
2022-04-06 09:59:08 +00:00
void doFilter ( const SNode * pFilterNode , SSDataBlock * pBlock ) {
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
SFilterColumnParam param1 = { . numOfCols = pBlock - > info . numOfCols , . pDataBlock = pBlock - > pDataBlock } ;
code = filterSetDataFromSlotId ( filter , & param1 ) ;
int8_t * rowRes = NULL ;
bool keep = filterExecute ( filter , pBlock , & rowRes , NULL , param1 . numOfCols ) ;
SSDataBlock * px = createOneDataBlock ( pBlock ) ;
blockDataEnsureCapacity ( px , pBlock - > info . rows ) ;
2022-04-06 15:00:32 +00:00
// todo extract method
2022-04-06 09:59:08 +00:00
int32_t numOfRow = 0 ;
for ( int32_t i = 0 ; i < pBlock - > info . numOfCols ; + + i ) {
SColumnInfoData * pDst = taosArrayGet ( px - > pDataBlock , i ) ;
SColumnInfoData * pSrc = taosArrayGet ( pBlock - > pDataBlock , i ) ;
numOfRow = 0 ;
for ( int32_t j = 0 ; j < pBlock - > info . rows ; + + j ) {
if ( rowRes [ j ] = = 0 ) {
continue ;
}
2022-04-06 15:00:32 +00:00
if ( colDataIsNull_s ( pSrc , j ) ) {
colDataAppendNULL ( pDst , numOfRow ) ;
} else {
colDataAppend ( pDst , numOfRow , colDataGetData ( pSrc , j ) , false ) ;
}
2022-04-06 09:59:08 +00:00
numOfRow + = 1 ;
}
* pSrc = * pDst ;
}
pBlock - > info . rows = numOfRow ;
}
2022-04-01 02:44:34 +00:00
void doSetTableGroupOutputBuf ( SAggOperatorInfo * pAggInfo , int32_t numOfOutput , int32_t tableGroupId , SExecTaskInfo * pTaskInfo ) {
2021-11-02 05:37:31 +00:00
// for simple group by query without interval, all the tables belong to one group result.
int64_t uid = 0 ;
int64_t tid = 0 ;
2022-02-14 02:58:58 +00:00
SResultRowInfo * pResultRowInfo = & pAggInfo - > binfo . resultRowInfo ;
SqlFunctionCtx * pCtx = pAggInfo - > binfo . pCtx ;
2022-03-29 07:24:25 +00:00
int32_t * rowCellInfoOffset = pAggInfo - > binfo . rowCellInfoOffset ;
2022-02-14 02:58:58 +00:00
2021-11-02 05:37:31 +00:00
SResultRow * pResultRow =
2022-03-29 07:24:25 +00:00
doSetResultOutBufByKey_rv ( pAggInfo - > pResultBuf , pResultRowInfo , tid , ( char * ) & tableGroupId , sizeof ( tableGroupId ) ,
true , uid , pTaskInfo , false , & pAggInfo - > aggSup ) ;
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-01 02:44:34 +00:00
int32_t ret = addNewWindowResultBuf ( pResultRow , pAggInfo - > pResultBuf , tableGroupId , pAggInfo - > binfo . pRes - > info . rowSize ) ;
2021-11-02 05:37:31 +00:00
if ( ret ! = TSDB_CODE_SUCCESS ) {
return ;
}
}
2022-04-01 02:44:34 +00:00
setResultRowOutputBufInitCtx_rv ( pResultRow , pCtx , numOfOutput , rowCellInfoOffset ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
void setExecutionContext ( int32_t numOfOutput , int32_t tableGroupId , TSKEY nextKey , SExecTaskInfo * pTaskInfo ,
STableQueryInfo * pTableQueryInfo , SAggOperatorInfo * pAggInfo ) {
2021-11-02 05:37:31 +00:00
// lastKey needs to be updated
pTableQueryInfo - > lastKey = nextKey ;
2022-02-14 02:58:58 +00:00
if ( pAggInfo - > groupId ! = INT32_MIN & & pAggInfo - > groupId = = tableGroupId ) {
2021-11-02 05:37:31 +00:00
return ;
}
2022-02-14 02:58:58 +00:00
doSetTableGroupOutputBuf ( pAggInfo , numOfOutput , tableGroupId , pTaskInfo ) ;
2021-11-02 05:37:31 +00:00
// record the current active group id
2022-02-14 02:58:58 +00:00
pAggInfo - > groupId = tableGroupId ;
2021-11-02 05:37:31 +00:00
}
2022-01-28 02:44:02 +00:00
void setCtxTagForJoin ( STaskRuntimeEnv * pRuntimeEnv , SqlFunctionCtx * pCtx , SExprInfo * pExprInfo , void * pTable ) {
2022-01-08 08:28:44 +00:00
STaskAttr * pQueryAttr = pRuntimeEnv - > pQueryAttr ;
2021-11-02 05:37:31 +00:00
2022-03-09 02:22:53 +00:00
SExprBasicInfo * pExpr = & pExprInfo - > base ;
2022-03-29 07:24:25 +00:00
// if (pQueryAttr->stableQuery && (pRuntimeEnv->pTsBuf != NULL) &&
// (pExpr->functionId == FUNCTION_TS || pExpr->functionId == FUNCTION_PRJ) &&
// (pExpr->colInfo.colIndex == PRIMARYKEY_TIMESTAMP_COL_ID)) {
// assert(pExpr->numOfParams == 1);
//
// int16_t tagColId = (int16_t)pExprInfo->base.param[0].i;
// SColumnInfo* pColInfo = doGetTagColumnInfoById(pQueryAttr->tagColList, pQueryAttr->numOfTags, tagColId);
//
// doSetTagValueInParam(pTable, tagColId, &pCtx->tag, pColInfo->type, pColInfo->bytes);
//
// int16_t tagType = pCtx[0].tag.nType;
// if (tagType == TSDB_DATA_TYPE_BINARY || tagType == TSDB_DATA_TYPE_NCHAR) {
// //qDebug("QInfo:0x%"PRIx64" set tag value for join comparison, colId:%" PRId64 ", val:%s",
// GET_TASKID(pRuntimeEnv),
//// pExprInfo->base.param[0].i, pCtx[0].tag.pz);
// } else {
// //qDebug("QInfo:0x%"PRIx64" set tag value for join comparison, colId:%" PRId64 ", val:%" PRId64,
// GET_TASKID(pRuntimeEnv),
//// pExprInfo->base.param[0].i, pCtx[0].tag.i);
// }
// }
}
int32_t setTimestampListJoinInfo ( STaskRuntimeEnv * pRuntimeEnv , SVariant * pTag , STableQueryInfo * pTableQueryInfo ) {
2022-01-08 08:28:44 +00:00
STaskAttr * pQueryAttr = pRuntimeEnv - > pQueryAttr ;
2021-11-02 05:37:31 +00:00
assert ( pRuntimeEnv - > pTsBuf ! = NULL ) ;
2022-02-13 06:34:00 +00:00
#if 0
2021-11-02 05:37:31 +00:00
// both the master and supplement scan needs to set the correct ts comp start position
if ( pTableQueryInfo - > cur . vgroupIndex = = - 1 ) {
taosVariantAssign ( & pTableQueryInfo - > tag , pTag ) ;
STSElem elem = tsBufGetElemStartPos ( pRuntimeEnv - > pTsBuf , pQueryAttr - > vgId , & pTableQueryInfo - > tag ) ;
// failed to find data with the specified tag value and vnodeId
if ( ! tsBufIsValidElem ( & elem ) ) {
if ( pTag - > nType = = TSDB_DATA_TYPE_BINARY | | pTag - > nType = = TSDB_DATA_TYPE_NCHAR ) {
2022-01-10 11:48:21 +00:00
//qError("QInfo:0x%"PRIx64" failed to find tag:%s in ts_comp", GET_TASKID(pRuntimeEnv), pTag->pz);
2021-11-02 05:37:31 +00:00
} else {
2022-01-10 11:48:21 +00:00
//qError("QInfo:0x%"PRIx64" failed to find tag:%" PRId64 " in ts_comp", GET_TASKID(pRuntimeEnv), pTag->i);
2021-11-02 05:37:31 +00:00
}
return - 1 ;
}
// Keep the cursor info of current table
pTableQueryInfo - > cur = tsBufGetCursor ( pRuntimeEnv - > pTsBuf ) ;
if ( pTag - > nType = = TSDB_DATA_TYPE_BINARY | | pTag - > nType = = TSDB_DATA_TYPE_NCHAR ) {
2022-01-10 11:48:21 +00:00
//qDebug("QInfo:0x%"PRIx64" find tag:%s start pos in ts_comp, blockIndex:%d, tsIndex:%d", GET_TASKID(pRuntimeEnv), pTag->pz, pTableQueryInfo->cur.blockIndex, pTableQueryInfo->cur.tsIndex);
2021-11-02 05:37:31 +00:00
} else {
2022-01-10 11:48:21 +00:00
//qDebug("QInfo:0x%"PRIx64" find tag:%"PRId64" start pos in ts_comp, blockIndex:%d, tsIndex:%d", GET_TASKID(pRuntimeEnv), pTag->i, pTableQueryInfo->cur.blockIndex, pTableQueryInfo->cur.tsIndex);
2021-11-02 05:37:31 +00:00
}
} else {
tsBufSetCursor ( pRuntimeEnv - > pTsBuf , & pTableQueryInfo - > cur ) ;
if ( pTag - > nType = = TSDB_DATA_TYPE_BINARY | | pTag - > nType = = TSDB_DATA_TYPE_NCHAR ) {
2022-01-10 11:48:21 +00:00
//qDebug("QInfo:0x%"PRIx64" find tag:%s start pos in ts_comp, blockIndex:%d, tsIndex:%d", GET_TASKID(pRuntimeEnv), pTag->pz, pTableQueryInfo->cur.blockIndex, pTableQueryInfo->cur.tsIndex);
2021-11-02 05:37:31 +00:00
} else {
2022-01-10 11:48:21 +00:00
//qDebug("QInfo:0x%"PRIx64" find tag:%"PRId64" start pos in ts_comp, blockIndex:%d, tsIndex:%d", GET_TASKID(pRuntimeEnv), pTag->i, pTableQueryInfo->cur.blockIndex, pTableQueryInfo->cur.tsIndex);
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
return 0 ;
}
/*
* There are two cases to handle :
*
2022-03-29 07:24:25 +00:00
* 1. Query range is not set yet ( queryRangeSet = 0 ) . we need to set the query range info , including
* pQueryAttr - > lastKey , pQueryAttr - > window . skey , and pQueryAttr - > eKey .
2021-11-02 05:37:31 +00:00
* 2. Query range is set and query is in progress . There may be another result with the same query ranges to be
* merged during merge stage . In this case , we need the pTableQueryInfo - > lastResRows to decide if there
* is a previous result generated or not .
*/
2022-03-29 07:24:25 +00:00
void setIntervalQueryRange ( STaskRuntimeEnv * pRuntimeEnv , TSKEY key ) {
STaskAttr * pQueryAttr = pRuntimeEnv - > pQueryAttr ;
STableQueryInfo * pTableQueryInfo = pRuntimeEnv - > current ;
SResultRowInfo * pResultRowInfo = & pTableQueryInfo - > resInfo ;
2021-11-02 05:37:31 +00:00
if ( pResultRowInfo - > curPos ! = - 1 ) {
return ;
}
2022-03-29 07:24:25 +00:00
// pTableQueryInfo->win.skey = key;
2021-11-02 05:37:31 +00:00
STimeWindow win = { . skey = key , . ekey = pQueryAttr - > window . ekey } ;
/**
* In handling the both ascending and descending order super table query , we need to find the first qualified
* timestamp of this table , and then set the first qualified start timestamp .
* In ascending query , the key is the first qualified timestamp . However , in the descending order query , additional
* operations involve .
*/
STimeWindow w = TSWINDOW_INITIALIZER ;
2022-01-24 04:53:17 +00:00
TSKEY sk = TMIN ( win . skey , win . ekey ) ;
TSKEY ek = TMAX ( win . skey , win . ekey ) ;
2022-03-29 07:24:25 +00:00
// getAlignQueryTimeWindow(pQueryAttr, win.skey, sk, ek, &w);
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// if (pResultRowInfo->prevSKey == TSKEY_INITIAL_VAL) {
// if (!QUERY_IS_ASC_QUERY(pQueryAttr)) {
// assert(win.ekey == pQueryAttr->window.ekey);
// }
//
// pResultRowInfo->prevSKey = w.skey;
// }
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// pTableQueryInfo->lastKey = pTableQueryInfo->win.skey;
2021-11-02 05:37:31 +00:00
}
/**
* copyToOutputBuf support copy data in ascending / descending order
* For interval query of both super table and table , copy the data in ascending order , since the output results are
* ordered in SWindowResutl already . While handling the group by query for both table and super table ,
* all group result are completed already .
*
* @ param pQInfo
* @ param result
*/
2022-03-29 07:24:25 +00:00
static int32_t doCopyToSDataBlock ( SDiskbasedBuf * pBuf , SGroupResInfo * pGroupResInfo , int32_t orderType ,
SSDataBlock * pBlock , int32_t rowCapacity , int32_t * rowCellOffset ) {
2021-11-02 05:37:31 +00:00
int32_t numOfRows = getNumOfTotalRes ( pGroupResInfo ) ;
2022-03-29 07:24:25 +00:00
int32_t numOfResult = pBlock - > info . rows ; // there are already exists result rows
2021-11-02 05:37:31 +00:00
int32_t start = 0 ;
int32_t step = - 1 ;
2022-03-29 07:24:25 +00:00
// qDebug("QInfo:0x%"PRIx64" start to copy data from windowResInfo to output buf", GET_TASKID(pRuntimeEnv));
2021-11-02 05:37:31 +00:00
assert ( orderType = = TSDB_ORDER_ASC | | orderType = = TSDB_ORDER_DESC ) ;
if ( orderType = = TSDB_ORDER_ASC ) {
start = pGroupResInfo - > index ;
step = 1 ;
} else { // desc order copy all data
start = numOfRows - pGroupResInfo - > index - 1 ;
step = - 1 ;
}
2022-03-15 06:37:26 +00:00
int32_t nrows = pBlock - > info . rows ;
2021-11-02 05:37:31 +00:00
for ( int32_t i = start ; ( i < numOfRows ) & & ( i > = 0 ) ; i + = step ) {
2022-03-15 06:37:26 +00:00
SResultRowPosition * pPos = taosArrayGet ( pGroupResInfo - > pRows , i ) ;
2022-03-29 07:24:25 +00:00
SFilePage * page = getBufPage ( pBuf , pPos - > pageId ) ;
2022-03-15 06:37:26 +00:00
SResultRow * pRow = ( SResultRow * ) ( ( char * ) page + pPos - > offset ) ;
2021-11-02 05:37:31 +00:00
if ( pRow - > numOfRows = = 0 ) {
pGroupResInfo - > index + = 1 ;
continue ;
}
2022-03-15 06:37:26 +00:00
// TODO copy multiple rows?
2021-11-02 05:37:31 +00:00
int32_t numOfRowsToCopy = pRow - > numOfRows ;
2022-03-29 07:24:25 +00:00
if ( numOfResult + numOfRowsToCopy > = rowCapacity ) {
2021-11-02 05:37:31 +00:00
break ;
}
pGroupResInfo - > index + = 1 ;
for ( int32_t j = 0 ; j < pBlock - > info . numOfCols ; + + j ) {
2022-03-29 07:24:25 +00:00
SColumnInfoData * pColInfoData = taosArrayGet ( pBlock - > pDataBlock , j ) ;
2022-03-15 06:37:26 +00:00
SResultRowEntryInfo * pEntryInfo = getResultCell ( pRow , j , rowCellOffset ) ;
2021-11-02 05:37:31 +00:00
2022-03-15 06:37:26 +00:00
char * in = GET_ROWCELL_INTERBUF ( pEntryInfo ) ;
2022-04-06 15:00:32 +00:00
colDataAppend ( pColInfoData , nrows , in , pEntryInfo - > isNullRes ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-15 06:37:26 +00:00
releaseBufPage ( pBuf , page ) ;
nrows + = 1 ;
2021-11-02 05:37:31 +00:00
numOfResult + = numOfRowsToCopy ;
2022-02-14 05:20:46 +00:00
if ( numOfResult = = rowCapacity ) { // output buffer is full
2021-11-02 05:37:31 +00:00
break ;
}
}
2022-03-29 07:24:25 +00:00
// qDebug("QInfo:0x%"PRIx64" copy data to query buf completed", GET_TASKID(pRuntimeEnv));
2021-11-02 05:37:31 +00:00
pBlock - > info . rows = numOfResult ;
return 0 ;
}
2022-04-04 06:54:39 +00:00
void toSDatablock ( SGroupResInfo * pGroupResInfo , SDiskbasedBuf * pBuf , SSDataBlock * pBlock , int32_t rowCapacity ,
2022-03-29 07:24:25 +00:00
int32_t * rowCellOffset ) {
2021-11-02 05:37:31 +00:00
assert ( pGroupResInfo - > currentGroup < = pGroupResInfo - > totalGroup ) ;
2022-03-17 05:11:06 +00:00
blockDataCleanup ( pBlock ) ;
2021-11-02 05:37:31 +00:00
if ( ! hasRemainDataInCurrentGroup ( pGroupResInfo ) ) {
return ;
}
2022-03-30 05:41:15 +00:00
int32_t orderType = TSDB_ORDER_ASC ;
2022-03-15 06:37:26 +00:00
doCopyToSDataBlock ( pBuf , pGroupResInfo , orderType , pBlock , rowCapacity , rowCellOffset ) ;
2021-11-02 05:37:31 +00:00
2022-02-14 06:39:42 +00:00
// add condition (pBlock->info.rows >= 1) just to runtime happy
blockDataUpdateTsWindow ( pBlock ) ;
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 ,
int32_t * rowCellInfoOffset ) {
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-02-14 02:58:58 +00:00
SResultRowEntryInfo * pCell = getResultCell ( pResult , j , rowCellInfoOffset ) ;
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-03-29 07:24:25 +00:00
int32_t doFillTimeIntervalGapsInResults ( struct SFillInfo * pFillInfo , SSDataBlock * pOutput , int32_t capacity , void * * p ) {
// for(int32_t i = 0; i < pFillInfo->numOfCols; ++i) {
// SColumnInfoData* pColInfoData = taosArrayGet(pOutput->pDataBlock, i);
// p[i] = pColInfoData->pData + (pColInfoData->info.bytes * pOutput->info.rows);
// }
2021-11-02 05:37:31 +00:00
int32_t numOfRows = ( int32_t ) taosFillResultDataBlock ( pFillInfo , p , capacity - pOutput - > info . rows ) ;
pOutput - > info . rows + = numOfRows ;
return pOutput - > info . rows ;
}
void publishOperatorProfEvent ( SOperatorInfo * operatorInfo , EQueryProfEventType eventType ) {
SQueryProfEvent event = { 0 } ;
2022-03-29 07:24:25 +00:00
event . eventType = eventType ;
event . eventTime = taosGetTimestampUs ( ) ;
2021-11-02 05:37:31 +00:00
event . operatorType = operatorInfo - > operatorType ;
if ( operatorInfo - > pRuntimeEnv ) {
2022-03-29 07:24:25 +00:00
// SQInfo* pQInfo = operatorInfo->pRuntimeEnv->qinfo;
// if (pQInfo->summary.queryProfEvents) {
// taosArrayPush(pQInfo->summary.queryProfEvents, &event);
// }
2021-11-02 05:37:31 +00:00
}
}
2022-03-29 07:24:25 +00:00
void publishQueryAbortEvent ( SExecTaskInfo * pTaskInfo , int32_t code ) {
2021-11-02 05:37:31 +00:00
SQueryProfEvent event ;
event . eventType = QUERY_PROF_QUERY_ABORT ;
event . eventTime = taosGetTimestampUs ( ) ;
event . abortCode = code ;
2022-01-18 03:21:13 +00:00
if ( pTaskInfo - > cost . queryProfEvents ) {
taosArrayPush ( pTaskInfo - > cost . queryProfEvents , & event ) ;
2021-11-02 05:37:31 +00:00
}
}
2022-03-29 07:24:25 +00:00
typedef struct {
2021-11-02 05:37:31 +00:00
uint8_t operatorType ;
int64_t beginTime ;
int64_t endTime ;
int64_t selfTime ;
int64_t descendantsTime ;
} SOperatorStackItem ;
2022-03-29 07:24:25 +00:00
static void doOperatorExecProfOnce ( SOperatorStackItem * item , SQueryProfEvent * event , SArray * opStack ,
SHashObj * profResults ) {
2021-11-02 05:37:31 +00:00
item - > endTime = event - > eventTime ;
item - > selfTime = ( item - > endTime - item - > beginTime ) - ( item - > descendantsTime ) ;
for ( int32_t j = 0 ; j < taosArrayGetSize ( opStack ) ; + + j ) {
SOperatorStackItem * ancestor = taosArrayGet ( opStack , j ) ;
ancestor - > descendantsTime + = item - > selfTime ;
}
2022-03-29 07:24:25 +00:00
uint8_t operatorType = item - > operatorType ;
2021-11-02 05:37:31 +00:00
SOperatorProfResult * result = taosHashGet ( profResults , & operatorType , sizeof ( operatorType ) ) ;
if ( result ! = NULL ) {
result - > sumRunTimes + + ;
result - > sumSelfTime + = item - > selfTime ;
} else {
SOperatorProfResult opResult ;
opResult . operatorType = operatorType ;
opResult . sumSelfTime = item - > selfTime ;
opResult . sumRunTimes = 1 ;
2022-03-29 07:24:25 +00:00
taosHashPut ( profResults , & ( operatorType ) , sizeof ( operatorType ) , & opResult , sizeof ( opResult ) ) ;
2021-11-02 05:37:31 +00:00
}
}
void calculateOperatorProfResults ( SQInfo * pQInfo ) {
if ( pQInfo - > summary . queryProfEvents = = NULL ) {
2022-03-29 07:24:25 +00:00
// qDebug("QInfo:0x%"PRIx64" query prof events array is null", pQInfo->qId);
2021-11-02 05:37:31 +00:00
return ;
}
if ( pQInfo - > summary . operatorProfResults = = NULL ) {
2022-03-29 07:24:25 +00:00
// qDebug("QInfo:0x%"PRIx64" operator prof results hash is null", pQInfo->qId);
2021-11-02 05:37:31 +00:00
return ;
}
SArray * opStack = taosArrayInit ( 32 , sizeof ( SOperatorStackItem ) ) ;
if ( opStack = = NULL ) {
return ;
}
2022-03-29 07:24:25 +00:00
size_t size = taosArrayGetSize ( pQInfo - > summary . queryProfEvents ) ;
2021-11-02 05:37:31 +00:00
SHashObj * profResults = pQInfo - > summary . operatorProfResults ;
for ( int i = 0 ; i < size ; + + i ) {
SQueryProfEvent * event = taosArrayGet ( pQInfo - > summary . queryProfEvents , i ) ;
if ( event - > eventType = = QUERY_PROF_BEFORE_OPERATOR_EXEC ) {
SOperatorStackItem opItem ;
opItem . operatorType = event - > operatorType ;
opItem . beginTime = event - > eventTime ;
opItem . descendantsTime = 0 ;
taosArrayPush ( opStack , & opItem ) ;
} else if ( event - > eventType = = QUERY_PROF_AFTER_OPERATOR_EXEC ) {
SOperatorStackItem * item = taosArrayPop ( opStack ) ;
assert ( item - > operatorType = = event - > operatorType ) ;
doOperatorExecProfOnce ( item , event , opStack , profResults ) ;
} else if ( event - > eventType = = QUERY_PROF_QUERY_ABORT ) {
SOperatorStackItem * item ;
while ( ( item = taosArrayPop ( opStack ) ) ! = NULL ) {
doOperatorExecProfOnce ( item , event , opStack , profResults ) ;
}
}
}
taosArrayDestroy ( opStack ) ;
}
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);
qDebug ( " %s :cost summary: elapsed time:% " PRId64 " us, first merge:% " PRId64
" us, total blocks:%d, "
" load block statis:%d, load data block:%d, total rows:% " PRId64 " , check rows:% " PRId64 ,
GET_TASKID ( pTaskInfo ) , pSummary - > elapsedTime , pSummary - > firstStageMergeTime , pSummary - > totalBlocks ,
pSummary - > loadBlockStatis , pSummary - > loadBlocks , pSummary - > totalRows , pSummary - > totalCheckedRows ) ;
//
// 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
if ( pSummary - > operatorProfResults ) {
SOperatorProfResult * opRes = taosHashIterate ( pSummary - > operatorProfResults , NULL ) ;
while ( opRes ! = NULL ) {
2022-03-29 07:24:25 +00:00
// qDebug("QInfo:0x%" PRIx64 " :cost summary: operator : %d, exec times: %" PRId64 ", self time: %" PRId64,
// pQInfo->qId, opRes->operatorType, opRes->sumRunTimes, opRes->sumSelfTime);
2021-11-02 05:37:31 +00:00
opRes = taosHashIterate ( pSummary - > operatorProfResults , opRes ) ;
}
}
}
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
}
static void doDestroyTableQueryInfo ( STableGroupInfo * pTableqinfoGroupInfo ) ;
2022-01-08 08:28:44 +00:00
static int32_t setupQueryHandle ( void * tsdb , STaskRuntimeEnv * pRuntimeEnv , int64_t qId , bool isSTableQuery ) {
2022-03-29 07:24:25 +00:00
STaskAttr * pQueryAttr = pRuntimeEnv - > pQueryAttr ;
2021-11-02 05:37:31 +00:00
#if 0
// TODO set the tags scan handle
if ( onlyQueryTags ( pQueryAttr ) ) {
return TSDB_CODE_SUCCESS ;
}
STsdbQueryCond cond = createTsdbQueryCond ( pQueryAttr , & pQueryAttr - > window ) ;
if ( pQueryAttr - > tsCompQuery | | pQueryAttr - > pointInterpQuery ) {
cond . type = BLOCK_LOAD_TABLE_SEQ_ORDER ;
}
if ( ! isSTableQuery
& & ( pRuntimeEnv - > tableqinfoGroupInfo . numOfTables = = 1 )
& & ( cond . order = = TSDB_ORDER_ASC )
& & ( ! QUERY_IS_INTERVAL_QUERY ( pQueryAttr ) )
& & ( ! pQueryAttr - > groupbyColumn )
& & ( ! pQueryAttr - > simpleAgg )
) {
SArray * pa = GET_TABLEGROUP ( pRuntimeEnv , 0 ) ;
STableQueryInfo * pCheckInfo = taosArrayGetP ( pa , 0 ) ;
cond . twindow = pCheckInfo - > win ;
}
terrno = TSDB_CODE_SUCCESS ;
if ( isFirstLastRowQuery ( pQueryAttr ) ) {
2022-01-10 11:48:21 +00:00
pRuntimeEnv - > pTsdbReadHandle = tsdbQueryLastRow ( tsdb , & cond , & pQueryAttr - > tableGroupInfo , qId , & pQueryAttr - > memRef ) ;
2021-11-02 05:37:31 +00:00
// update the query time window
pQueryAttr - > window = cond . twindow ;
if ( pQueryAttr - > tableGroupInfo . numOfTables = = 0 ) {
pRuntimeEnv - > tableqinfoGroupInfo . numOfTables = 0 ;
} else {
size_t numOfGroups = GET_NUM_OF_TABLEGROUP ( pRuntimeEnv ) ;
for ( int32_t i = 0 ; i < numOfGroups ; + + i ) {
SArray * group = GET_TABLEGROUP ( pRuntimeEnv , i ) ;
size_t t = taosArrayGetSize ( group ) ;
for ( int32_t j = 0 ; j < t ; + + j ) {
STableQueryInfo * pCheckInfo = taosArrayGetP ( group , j ) ;
pCheckInfo - > win = pQueryAttr - > window ;
pCheckInfo - > lastKey = pCheckInfo - > win . skey ;
}
}
}
} else if ( isCachedLastQuery ( pQueryAttr ) ) {
2022-01-10 11:48:21 +00:00
pRuntimeEnv - > pTsdbReadHandle = tsdbQueryCacheLast ( tsdb , & cond , & pQueryAttr - > tableGroupInfo , qId , & pQueryAttr - > memRef ) ;
2021-11-02 05:37:31 +00:00
} else if ( pQueryAttr - > pointInterpQuery ) {
2022-01-10 11:48:21 +00:00
pRuntimeEnv - > pTsdbReadHandle = tsdbQueryRowsInExternalWindow ( tsdb , & cond , & pQueryAttr - > tableGroupInfo , qId , & pQueryAttr - > memRef ) ;
2021-11-02 05:37:31 +00:00
} else {
2022-01-10 11:48:21 +00:00
pRuntimeEnv - > pTsdbReadHandle = tsdbQueryTables ( tsdb , & cond , & pQueryAttr - > tableGroupInfo , qId , & pQueryAttr - > memRef ) ;
2021-11-02 05:37:31 +00:00
}
# endif
return terrno ;
}
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-03-29 07:24:25 +00:00
// STsdbQueryCond createTsdbQueryCond(STaskAttr* pQueryAttr, STimeWindow* win) {
// STsdbQueryCond cond = {
// .colList = pQueryAttr->tableCols,
// .order = pQueryAttr->order.order,
// .numOfCols = pQueryAttr->numOfCols,
// .type = BLOCK_LOAD_OFFSET_SEQ_ORDER,
// .loadExternalRows = false,
// };
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// TIME_WINDOW_COPY(cond.twindow, *win);
// return cond;
// }
2021-11-02 05:37:31 +00:00
static STableIdInfo createTableIdInfo ( STableQueryInfo * pTableQueryInfo ) {
STableIdInfo tidInfo ;
2022-03-29 07:24:25 +00:00
// STableId* id = TSDB_TABLEID(pTableQueryInfo->pTable);
//
// tidInfo.uid = id->uid;
// tidInfo.tid = id->tid;
// tidInfo.key = pTableQueryInfo->lastKey;
2021-11-02 05:37:31 +00:00
return tidInfo ;
}
2022-03-29 07:24:25 +00:00
// static void updateTableIdInfo(STableQueryInfo* pTableQueryInfo, SSDataBlock* pBlock, SHashObj* pTableIdInfo, int32_t
// order) {
// int32_t step = GET_FORWARD_DIRECTION_FACTOR(order);
// pTableQueryInfo->lastKey = ((order == TSDB_ORDER_ASC)? pBlock->info.window.ekey:pBlock->info.window.skey) + step;
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// if (pTableQueryInfo->pTable == NULL) {
// return;
// }
2021-11-02 05:37:31 +00:00
//
2022-03-29 07:24:25 +00:00
// STableIdInfo tidInfo = createTableIdInfo(pTableQueryInfo);
// STableIdInfo *idinfo = taosHashGet(pTableIdInfo, &tidInfo.tid, sizeof(tidInfo.tid));
// if (idinfo != NULL) {
// assert(idinfo->tid == tidInfo.tid && idinfo->uid == tidInfo.uid);
// idinfo->key = tidInfo.key;
// } else {
// taosHashPut(pTableIdInfo, &tidInfo.tid, sizeof(tidInfo.tid), &tidInfo, sizeof(STableIdInfo));
// }
// }
2021-11-02 05:37:31 +00:00
2022-01-08 08:28:44 +00:00
static void doCloseAllTimeWindow ( STaskRuntimeEnv * pRuntimeEnv ) {
2021-11-02 05:37:31 +00:00
size_t numOfGroup = GET_NUM_OF_TABLEGROUP ( pRuntimeEnv ) ;
for ( int32_t i = 0 ; i < numOfGroup ; + + i ) {
SArray * group = GET_TABLEGROUP ( pRuntimeEnv , i ) ;
size_t num = taosArrayGetSize ( group ) ;
for ( int32_t j = 0 ; j < num ; + + j ) {
STableQueryInfo * item = taosArrayGetP ( group , j ) ;
closeAllResultRows ( & item - > resInfo ) ;
}
}
}
2022-01-16 07:56:48 +00:00
int32_t loadRemoteDataCallback ( void * param , const SDataBuf * pMsg , int32_t code ) {
2022-03-29 07:24:25 +00:00
SSourceDataInfo * pSourceDataInfo = ( SSourceDataInfo * ) param ;
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 ) ;
pRsp - > compLen = htonl ( pRsp - > compLen ) ;
pRsp - > useconds = htobe64 ( pRsp - > useconds ) ;
} 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-02-11 06:33:57 +00:00
tsem_post ( & pSourceDataInfo - > pEx - > ready ) ;
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-03-29 07:24:25 +00:00
SMsgSendInfo * pSendInfo = ( SMsgSendInfo * ) pMsg - > ahandle ;
2022-01-19 06:47:53 +00:00
assert ( pMsg - > ahandle ! = NULL ) ;
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-03-29 07:24:25 +00:00
qDebug ( " %s build fetch msg and send to vgId:%d, ep:%s, taskId:0x% " PRIx64 " , %d/% " PRIzu , GET_TASKID ( pTaskInfo ) ,
pSource - > addr . nodeId , pSource - > addr . epSet . eps [ 0 ] . fqdn , pSource - > taskId , 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 ) ;
// 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-02-11 06:33:57 +00:00
pMsgSendInfo - > param = pDataInfo ;
pMsgSendInfo - > msgInfo . pData = pMsg ;
pMsgSendInfo - > msgInfo . len = sizeof ( SResFetchReq ) ;
pMsgSendInfo - > msgType = TDMT_VND_FETCH ;
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-03-18 16:13:07 +00:00
// TODO if only one or two columnss required, how to extract data?
2022-04-04 06:54:39 +00:00
int32_t setSDataBlockFromFetchRsp ( SSDataBlock * pRes , SLoadRemoteDataInfo * pLoadInfo , int32_t numOfRows ,
2022-03-29 07:24:25 +00:00
char * pData , int32_t compLen , int32_t numOfOutput , int64_t startTs ,
uint64_t * total , SArray * pColList ) {
2022-03-18 03:12:49 +00:00
blockDataEnsureCapacity ( pRes , numOfRows ) ;
2022-02-11 06:33:57 +00:00
2022-03-22 15:29:38 +00:00
if ( pColList = = NULL ) { // data from other sources
int32_t * colLen = ( int32_t * ) pData ;
2022-03-29 07:24:25 +00:00
char * pStart = pData + sizeof ( int32_t ) * numOfOutput ;
2022-03-22 15:29:38 +00:00
2022-03-18 16:13:07 +00:00
for ( int32_t i = 0 ; i < numOfOutput ; + + i ) {
2022-03-22 15:29:38 +00:00
colLen [ i ] = htonl ( colLen [ i ] ) ;
ASSERT ( colLen [ i ] > 0 ) ;
2022-03-18 16:13:07 +00:00
SColumnInfoData * pColInfoData = taosArrayGet ( pRes - > pDataBlock , i ) ;
2022-03-22 15:29:38 +00:00
if ( IS_VAR_DATA_TYPE ( pColInfoData - > info . type ) ) {
pColInfoData - > varmeta . length = colLen [ i ] ;
pColInfoData - > varmeta . allocLen = colLen [ i ] ;
2022-02-11 06:33:57 +00:00
2022-03-29 07:24:25 +00:00
memcpy ( pColInfoData - > varmeta . offset , pStart , sizeof ( int32_t ) * numOfRows ) ;
pStart + = sizeof ( int32_t ) * numOfRows ;
2022-03-22 15:29:38 +00:00
2022-03-25 16:29:53 +00:00
pColInfoData - > pData = taosMemoryMalloc ( colLen [ i ] ) ;
2022-03-22 15:29:38 +00:00
} else {
memcpy ( pColInfoData - > nullbitmap , pStart , BitmapLen ( numOfRows ) ) ;
pStart + = BitmapLen ( numOfRows ) ;
2022-03-18 16:13:07 +00:00
}
2022-03-22 15:29:38 +00:00
memcpy ( pColInfoData - > pData , pStart , colLen [ i ] ) ;
pStart + = colLen [ i ] ;
2022-01-22 07:42:37 +00:00
}
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-03-22 15:29:38 +00:00
// data from mnode
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < numOfOutput ; + + i ) {
for ( int32_t j = 0 ; j < numOfOutput ; + + j ) {
int16_t colIndex = * ( int16_t * ) taosArrayGet ( pColList , j ) ;
2022-03-18 16:13:07 +00:00
if ( colIndex - 1 = = i ) {
SColumnInfoData * pColInfoData = taosArrayGet ( pRes - > pDataBlock , j ) ;
2022-01-16 07:56:48 +00:00
2022-03-18 16:13:07 +00:00
for ( int32_t k = 0 ; k < numOfRows ; + + k ) {
colDataAppend ( pColInfoData , k , pData , false ) ;
pData + = pColInfoData - > info . bytes ;
}
break ;
}
}
2022-01-22 07:42:37 +00:00
}
2022-02-11 06:33:57 +00:00
}
2022-01-22 07:42:37 +00:00
2022-03-11 11:29:43 +00:00
pRes - > info . rows = numOfRows ;
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-03-29 07:24:25 +00:00
int64_t el = taosGetTimestampUs ( ) - startTs ;
2022-03-11 11:29:43 +00:00
SLoadRemoteDataInfo * pLoadInfo = & pExchangeInfo - > loadInfo ;
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-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-03-29 07:24:25 +00:00
qDebug ( " %s vgId:%d, taskID:0x% " PRIx64 " index:%d completed, rowsOfSource:% " PRIu64 " , totalRows:% " PRIu64
" try next " ,
2022-02-11 06:33:57 +00:00
GET_TASKID ( pTaskInfo ) , pSource - > addr . nodeId , pSource - > taskId , i + 1 , pDataInfo - > totalRows ,
2022-03-11 11:29:43 +00:00
pExchangeInfo - > loadInfo . totalRows ) ;
2022-03-29 12:07:38 +00:00
pDataInfo - > status = EX_SOURCE_DATA_EXHAUSTED ;
2022-02-11 06:33:57 +00:00
completed + = 1 ;
continue ;
}
2022-01-19 08:20:13 +00:00
2022-03-11 11:29:43 +00:00
SRetrieveTableRsp * pTableRsp = pDataInfo - > pRsp ;
2022-03-29 07:24:25 +00:00
code =
setSDataBlockFromFetchRsp ( pExchangeInfo - > pResult , pLoadInfo , pTableRsp - > numOfRows , pTableRsp - > data ,
pTableRsp - > compLen , pOperator - > numOfOutput , startTs , & pDataInfo - > totalRows , NULL ) ;
2022-02-11 06:33:57 +00:00
if ( code ! = 0 ) {
2022-01-22 07:42:37 +00:00
goto _error ;
}
2022-02-11 06:33:57 +00:00
if ( pRsp - > completed = = 1 ) {
qDebug ( " %s fetch msg rsp from vgId:%d, taskId:0x% " PRIx64 " numOfRows:%d, rowsOfSource:% " PRIu64
" , totalRows:% " PRIu64 " , totalBytes:% " PRIu64 " try next %d/% " PRIzu ,
2022-03-29 07:24:25 +00:00
GET_TASKID ( pTaskInfo ) , pSource - > addr . nodeId , pSource - > taskId , pRes - > info . rows , pDataInfo - > totalRows ,
pLoadInfo - > totalRows , pLoadInfo - > totalSize , 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
} else {
2022-03-29 12:07:38 +00:00
qDebug ( " %s fetch msg rsp from vgId:%d, taskId:0x% " PRIx64 " numOfRows:%d, totalRows:% " PRIu64 " , totalBytes:% " PRIu64 ,
2022-03-11 11:29:43 +00:00
GET_TASKID ( pTaskInfo ) , pSource - > addr . nodeId , pSource - > taskId , pRes - > info . rows , pLoadInfo - > totalRows ,
pLoadInfo - > totalSize ) ;
2022-02-11 06:33:57 +00:00
}
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 ) {
goto _error ;
}
}
return pExchangeInfo - > pResult ;
}
if ( completed = = totalSources ) {
return setAllSourcesCompleted ( pOperator , startTs ) ;
}
}
_error :
pTaskInfo - > code = code ;
return NULL ;
}
2022-03-29 07:24:25 +00:00
static SSDataBlock * concurrentlyLoadRemoteData ( SOperatorInfo * pOperator ) {
SExchangeInfo * pExchangeInfo = pOperator - > info ;
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2022-03-21 10:34:42 +00:00
2022-03-18 03:12:49 +00:00
if ( pOperator - > status = = OP_RES_TO_RETURN ) {
return concurrentlyLoadRemoteDataImpl ( pOperator , pExchangeInfo , 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-18 03:12:49 +00:00
return NULL ;
2022-02-11 06:33:57 +00:00
}
}
int64_t endTs = taosGetTimestampUs ( ) ;
2022-03-29 07:24:25 +00:00
qDebug ( " %s send all fetch request to % " PRIzu " sources completed, elapsed:% " PRId64 , GET_TASKID ( pTaskInfo ) ,
totalSources , endTs - startTs ) ;
2022-02-11 06:33:57 +00:00
tsem_wait ( & pExchangeInfo - > ready ) ;
2022-03-18 03:12:49 +00:00
pOperator - > status = OP_RES_TO_RETURN ;
return concurrentlyLoadRemoteDataImpl ( pOperator , pExchangeInfo , pTaskInfo ) ;
2022-02-11 06:33:57 +00:00
}
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-03-29 07:24:25 +00:00
qDebug ( " %s send all fetch request to % " PRIzu " sources completed, elapsed:% " PRId64 , GET_TASKID ( pTaskInfo ) ,
totalSources , endTs - startTs ) ;
2022-02-11 06:33:57 +00:00
tsem_wait ( & pExchangeInfo - > ready ) ;
2022-03-05 07:58:28 +00:00
pOperator - > cost . openCost = taosGetTimestampUs ( ) - startTs ;
2022-02-11 06:33:57 +00:00
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-01 08:09:00 +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 ) {
qError ( " %s vgId:%d, taskID:0x% " PRIx64 " error happens, code:%s " ,
GET_TASKID ( pTaskInfo ) , pSource - > addr . nodeId , pSource - > taskId , tstrerror ( pDataInfo - > code ) ) ;
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-04-01 08:09:00 +00:00
qDebug ( " %s vgId:%d, taskID:0x% " PRIx64 " %d of total completed, rowsOfSource:% " PRIu64 " , totalRows:% " PRIu64 " try next " ,
2022-02-11 06:33:57 +00:00
GET_TASKID ( pTaskInfo ) , pSource - > addr . nodeId , pSource - > taskId , 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 ;
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 =
setSDataBlockFromFetchRsp ( pExchangeInfo - > pResult , pLoadInfo , pTableRsp - > numOfRows , pTableRsp - > data ,
pTableRsp - > compLen , pOperator - > numOfOutput , startTs , & pDataInfo - > totalRows , NULL ) ;
2022-01-22 07:42:37 +00:00
if ( pRsp - > completed = = 1 ) {
2022-01-25 05:42:33 +00:00
qDebug ( " %s fetch msg rsp from vgId:%d, taskId:0x% " PRIx64 " numOfRows:%d, rowsOfSource:% " PRIu64
2022-03-29 07:24:25 +00:00
" , totalRows:% " PRIu64 " , totalBytes:% " PRIu64 " try next %d/% " PRIzu ,
GET_TASKID ( pTaskInfo ) , pSource - > addr . nodeId , pSource - > taskId , 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-03-29 07:24:25 +00:00
qDebug ( " %s fetch msg rsp from vgId:%d, taskId:0x% " PRIx64 " numOfRows:%d, totalRows:% " PRIu64
" , totalBytes:% " PRIu64 ,
GET_TASKID ( pTaskInfo ) , pSource - > addr . nodeId , pSource - > taskId , pRes - > info . rows , pLoadInfo - > totalRows ,
pLoadInfo - > totalSize ) ;
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-03-29 07:24:25 +00:00
SExchangeInfo * pExchangeInfo = pOperator - > info ;
2022-03-05 07:58:28 +00:00
if ( pExchangeInfo - > seqLoadData ) {
// do nothing for sequentially load data
} else {
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-03-05 07:58:28 +00:00
return TSDB_CODE_SUCCESS ;
}
2022-03-29 07:24:25 +00:00
static SSDataBlock * doLoadRemoteData ( SOperatorInfo * pOperator , bool * newgroup ) {
SExchangeInfo * pExchangeInfo = pOperator - > info ;
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2022-02-11 06:33:57 +00:00
2022-03-15 06:37:26 +00:00
pTaskInfo - > code = pOperator - > _openFn ( pOperator ) ;
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 ;
}
* newgroup = false ;
2022-03-18 03:12:49 +00:00
2022-02-11 06:33:57 +00:00
if ( pExchangeInfo - > seqLoadData ) {
return seqLoadRemoteData ( pOperator ) ;
} else {
2022-03-18 03:12:49 +00:00
return concurrentlyLoadRemoteData ( pOperator ) ;
2022-02-11 06:33:57 +00:00
}
2022-01-19 09:03:29 +00:00
2022-02-11 06:33:57 +00:00
#if 0
2022-01-19 09:03:29 +00:00
_error :
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pMsg ) ;
taosMemoryFreeClear ( pMsgSendInfo ) ;
2022-01-19 09:03:29 +00:00
terrno = pTaskInfo - > code ;
return NULL ;
2022-02-11 06:33:57 +00:00
# endif
2022-01-19 06:47:53 +00:00
}
2022-01-16 07:56:48 +00:00
2022-03-04 07:53:30 +00:00
static int32_t initDataSource ( int32_t numOfSources , SExchangeInfo * pInfo ) {
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-03-29 07:24:25 +00:00
dataInfo . pEx = pInfo ;
dataInfo . index = i ;
2022-02-11 06:33:57 +00:00
2022-03-04 07:53:30 +00:00
void * ret = taosArrayPush ( pInfo - > pSourceDataInfo , & dataInfo ) ;
if ( ret = = NULL ) {
taosArrayDestroy ( pInfo - > pSourceDataInfo ) ;
return TSDB_CODE_OUT_OF_MEMORY ;
}
}
return TSDB_CODE_SUCCESS ;
}
2022-03-10 10:07:07 +00:00
SOperatorInfo * createExchangeOperatorInfo ( const SNodeList * pSources , SSDataBlock * pBlock , SExecTaskInfo * pTaskInfo ) {
2022-03-29 07:24:25 +00:00
SExchangeInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( SExchangeInfo ) ) ;
2022-03-25 16:29:53 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
2022-03-04 07:53:30 +00:00
if ( pInfo = = NULL | | pOperator = = NULL ) {
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pInfo ) ;
taosMemoryFreeClear ( pOperator ) ;
2022-03-18 03:12:49 +00:00
terrno = TSDB_CODE_QRY_OUT_OF_MEMORY ;
return NULL ;
2022-03-04 07:53:30 +00:00
}
2022-03-10 08:18:35 +00:00
size_t numOfSources = LIST_LENGTH ( pSources ) ;
2022-03-10 10:07:07 +00:00
pInfo - > pSources = taosArrayInit ( numOfSources , sizeof ( SDownstreamSourceNode ) ) ;
2022-03-14 06:15:26 +00:00
pInfo - > pSourceDataInfo = taosArrayInit ( numOfSources , sizeof ( SSourceDataInfo ) ) ;
if ( pInfo - > pSourceDataInfo = = NULL | | pInfo - > pSources = = NULL ) {
goto _error ;
2022-03-10 10:07:07 +00:00
}
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < numOfSources ; + + i ) {
SNodeListNode * pNode = nodesListGetNode ( ( SNodeList * ) pSources , i ) ;
2022-03-10 10:07:07 +00:00
taosArrayPush ( pInfo - > pSources , pNode ) ;
}
2022-02-11 06:33:57 +00:00
2022-03-04 07:53:30 +00:00
int32_t code = initDataSource ( numOfSources , pInfo ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
2022-02-11 06:33:57 +00:00
}
2022-01-19 06:47:53 +00:00
2022-03-29 07:24:25 +00:00
size_t size = pBlock - > info . numOfCols ;
pInfo - > pResult = pBlock ;
2022-02-11 06:33:57 +00:00
pInfo - > seqLoadData = true ;
tsem_init ( & pInfo - > ready , 0 , 0 ) ;
2022-01-16 07:56:48 +00:00
2022-03-29 07:24:25 +00:00
pOperator - > name = " ExchangeOperator " ;
2022-02-28 09:02:43 +00:00
pOperator - > operatorType = QUERY_NODE_PHYSICAL_PLAN_EXCHANGE ;
2022-01-16 07:56:48 +00:00
pOperator - > blockingOptr = false ;
2022-03-29 07:24:25 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > info = pInfo ;
pOperator - > numOfOutput = size ;
pOperator - > pTaskInfo = pTaskInfo ;
pOperator - > _openFn = prepareLoadRemoteData ; // assign a dummy function.
pOperator - > getNextFn = doLoadRemoteData ;
pOperator - > closeFn = destroyExchangeOperatorInfo ;
2022-01-16 07:56:48 +00:00
2022-01-26 01:20:48 +00:00
# if 1
2022-03-29 07:24:25 +00:00
{ // todo refactor
2022-01-19 06:47:53 +00:00
SRpcInit rpcInit ;
memset ( & rpcInit , 0 , sizeof ( rpcInit ) ) ;
rpcInit . localPort = 0 ;
2022-01-25 06:13:11 +00:00
rpcInit . label = " EX " ;
2022-01-19 06:47:53 +00:00
rpcInit . numOfThreads = 1 ;
2022-01-26 01:20:48 +00:00
rpcInit . cfp = qProcessFetchRsp ;
2022-02-24 10:35:25 +00:00
rpcInit . sessions = tsMaxConnections ;
2022-01-19 06:47:53 +00:00
rpcInit . connType = TAOS_CONN_CLIENT ;
2022-03-29 07:24:25 +00:00
rpcInit . user = ( char * ) " root " ;
2022-02-24 10:35:25 +00:00
rpcInit . idleTime = tsShellActivityTimer * 1000 ;
2022-01-19 06:47:53 +00:00
rpcInit . ckey = " key " ;
2022-01-23 11:50:18 +00:00
rpcInit . spi = 1 ;
2022-03-29 07:24:25 +00:00
rpcInit . secret = ( char * ) " dcc5bed04851fec854c035b2e40263b6 " ;
2022-01-19 06:47:53 +00:00
pInfo - > pTransporter = rpcOpen ( & rpcInit ) ;
if ( pInfo - > pTransporter = = NULL ) {
2022-03-29 07:24:25 +00:00
return NULL ; // todo
2022-01-19 06:47:53 +00:00
}
}
2022-01-25 07:18:56 +00:00
# endif
2022-02-11 06:33:57 +00:00
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-03-14 06:15:26 +00:00
destroyExchangeOperatorInfo ( pInfo , numOfSources ) ;
2022-03-04 07:53:30 +00:00
}
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 ;
2022-03-04 07:53:30 +00:00
return NULL ;
2022-01-16 07:56:48 +00:00
}
2022-01-19 09:03:29 +00:00
SSDataBlock * createResultDataBlock ( const SArray * pExprInfo ) {
2022-03-25 16:29:53 +00:00
SSDataBlock * pResBlock = taosMemoryCalloc ( 1 , sizeof ( SSDataBlock ) ) ;
2022-01-19 09:03:29 +00:00
if ( pResBlock = = NULL ) {
return NULL ;
}
size_t numOfCols = taosArrayGetSize ( pExprInfo ) ;
pResBlock - > pDataBlock = taosArrayInit ( numOfCols , sizeof ( SColumnInfoData ) ) ;
SArray * pResult = pResBlock - > pDataBlock ;
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < numOfCols ; + + i ) {
2022-01-19 09:03:29 +00:00
SColumnInfoData colInfoData = { 0 } ;
2022-03-29 07:24:25 +00:00
SExprInfo * p = taosArrayGetP ( pExprInfo , i ) ;
2022-01-19 09:03:29 +00:00
2022-03-10 08:18:35 +00:00
SResSchema * pSchema = & p - > base . resSchema ;
2022-03-29 07:24:25 +00:00
colInfoData . info . type = pSchema - > type ;
2022-01-19 09:03:29 +00:00
colInfoData . info . colId = pSchema - > colId ;
colInfoData . info . bytes = pSchema - > bytes ;
2022-03-10 08:18:35 +00:00
colInfoData . info . scale = pSchema - > scale ;
colInfoData . info . precision = pSchema - > precision ;
2022-01-19 09:03:29 +00:00
taosArrayPush ( pResult , & colInfoData ) ;
}
return pResBlock ;
}
2022-04-04 06:54:39 +00:00
static int32_t doInitAggInfoSup ( SAggSupporter * pAggSup , SqlFunctionCtx * pCtx , int32_t numOfOutput , const char * pKey ) ;
static void cleanupAggSup ( SAggSupporter * pAggSup ) ;
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
2021-11-02 05:37:31 +00:00
static void destroySlimitOperatorInfo ( void * param , int32_t numOfOutput ) {
2022-03-29 07:24:25 +00:00
SSLimitOperatorInfo * pInfo = ( SSLimitOperatorInfo * ) param ;
2021-11-02 05:37:31 +00:00
taosArrayDestroy ( pInfo - > orderColumnList ) ;
2022-02-08 10:01:21 +00:00
pInfo - > pRes = blockDataDestroy ( pInfo - > pRes ) ;
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pInfo - > prevRow ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-01 02:40:50 +00:00
static void assignExprInfo ( SExprInfo * dst , const SExprInfo * src ) {
assert ( dst ! = NULL & & src ! = NULL ) ;
2021-11-02 05:37:31 +00:00
2022-03-01 02:40:50 +00:00
* dst = * src ;
2021-11-02 05:37:31 +00:00
2022-03-01 02:40:50 +00:00
dst - > pExpr = exprdup ( src - > pExpr ) ;
2022-03-25 16:29:53 +00:00
dst - > base . pParam = taosMemoryCalloc ( src - > base . numOfParams , sizeof ( SColumn ) ) ;
2022-03-09 02:22:53 +00:00
memcpy ( dst - > base . pParam , src - > base . pParam , sizeof ( SColumn ) * src - > base . numOfParams ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// memset(dst->base.param, 0, sizeof(SVariant) * tListLen(dst->base.param));
// for (int32_t j = 0; j < src->base.numOfParams; ++j) {
// taosVariantAssign(&dst->base.param[j], &src->base.param[j]);
// }
2022-03-01 02:40:50 +00:00
}
2021-11-02 05:37:31 +00:00
2022-02-22 05:12:03 +00:00
static SExprInfo * exprArrayDup ( SArray * pExprList ) {
size_t numOfOutput = taosArrayGetSize ( pExprList ) ;
2021-11-02 05:37:31 +00:00
2022-03-25 16:29:53 +00:00
SExprInfo * p = taosMemoryCalloc ( numOfOutput , sizeof ( SExprInfo ) ) ;
2022-02-22 05:12:03 +00:00
for ( int32_t i = 0 ; i < numOfOutput ; + + i ) {
SExprInfo * pExpr = taosArrayGetP ( pExprList , i ) ;
2022-02-17 10:16:50 +00:00
assignExprInfo ( & p [ i ] , pExpr ) ;
2021-11-02 05:37:31 +00:00
}
2022-02-17 10:16:50 +00:00
return p ;
}
2021-11-02 05:37:31 +00:00
2022-02-21 05:28:20 +00:00
// TODO merge aggregate super table
2022-03-29 07:24:25 +00:00
static void appendOneRowToDataBlock ( SSDataBlock * pBlock , STupleHandle * pTupleHandle ) {
2022-02-08 02:21:00 +00:00
for ( int32_t i = 0 ; i < pBlock - > info . numOfCols ; + + i ) {
SColumnInfoData * pColInfo = taosArrayGet ( pBlock - > pDataBlock , i ) ;
2021-11-02 05:37:31 +00:00
2022-02-22 05:16:46 +00:00
bool isNull = tsortIsNullVal ( pTupleHandle , i ) ;
2022-02-15 06:40:29 +00:00
if ( isNull ) {
colDataAppend ( pColInfo , pBlock - > info . rows , NULL , true ) ;
} else {
2022-02-22 05:16:46 +00:00
char * pData = tsortGetValue ( pTupleHandle , i ) ;
2022-02-15 06:40:29 +00:00
colDataAppend ( pColInfo , pBlock - > info . rows , pData , false ) ;
}
2021-11-02 05:37:31 +00:00
}
2022-02-08 02:21:00 +00:00
pBlock - > info . rows + = 1 ;
}
2021-11-02 05:37:31 +00:00
2022-04-07 06:51:52 +00:00
SSDataBlock * getSortedBlockData ( SSortHandle * pHandle , SSDataBlock * pDataBlock , int32_t capacity ) {
2022-03-17 05:11:06 +00:00
blockDataCleanup ( pDataBlock ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
while ( 1 ) {
2022-02-22 05:16:46 +00:00
STupleHandle * pTupleHandle = tsortNextTuple ( pHandle ) ;
2022-02-19 08:15:09 +00:00
if ( pTupleHandle = = NULL ) {
break ;
}
2021-11-02 05:37:31 +00:00
2022-02-19 08:15:09 +00:00
appendOneRowToDataBlock ( pDataBlock , pTupleHandle ) ;
if ( pDataBlock - > info . rows > = capacity ) {
return pDataBlock ;
2022-02-08 10:01:21 +00:00
}
}
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
return ( pDataBlock - > info . rows > 0 ) ? pDataBlock : NULL ;
2022-02-08 02:21:00 +00:00
}
2021-11-02 05:37:31 +00:00
2022-02-19 08:15:09 +00:00
SSDataBlock * loadNextDataBlock ( void * param ) {
2022-03-29 07:24:25 +00:00
SOperatorInfo * pOperator = ( SOperatorInfo * ) param ;
bool newgroup = false ;
2022-03-05 07:58:28 +00:00
return pOperator - > getNextFn ( pOperator , & newgroup ) ;
2021-11-02 05:37:31 +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-02-22 05:12:03 +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-03-09 02:22:53 +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-02-22 05:12:03 +00:00
SqlFunctionCtx * pCtx = pInfo - > binfo . pCtx ;
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < pBlock - > info . numOfCols ; + + i ) {
2022-02-22 05:12:03 +00:00
pCtx [ i ] . size = 1 ;
}
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-03-09 02:22:53 +00:00
int32_t numOfRows = getNumOfResult ( pInfo - > binfo . pCtx , pOperator - > numOfOutput , NULL ) ;
2022-02-22 05:12:03 +00:00
// setTagValueForMultipleRows(pCtx, pOperator->numOfOutput, 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-02-22 05:12:03 +00:00
SSDataBlock * pDataBlock = createOneDataBlock ( pInfo - > binfo . pRes ) ;
blockDataEnsureCapacity ( pDataBlock , pInfo - > binfo . capacity ) ;
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 ) ;
if ( pDataBlock - > info . rows > = pInfo - > binfo . capacity ) {
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-02-22 05:12:03 +00:00
setInputDataBlock ( pOperator , pInfo - > binfo . pCtx , pDataBlock , TSDB_ORDER_ASC ) ;
2022-03-29 07:24:25 +00:00
// updateOutputBuf(&pInfo->binfo, &pAggInfo->bufCapacity, pBlock->info.rows * pAggInfo->resultRowFactor,
// pOperator->pRuntimeEnv, true);
2022-02-22 05:12:03 +00:00
doMergeImpl ( pOperator , pOperator - > numOfOutput , pDataBlock ) ;
// 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-02-22 05:12:03 +00:00
doFinalizeResultImpl ( pInfo - > binfo . pCtx , pOperator - > numOfOutput ) ;
2022-03-09 02:22:53 +00:00
int32_t numOfRows = getNumOfResult ( pInfo - > binfo . pCtx , pOperator - > numOfOutput , NULL ) ;
2022-02-22 05:12:03 +00:00
// setTagValueForMultipleRows(pCtx, pOperator->numOfOutput, 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-03-29 07:24:25 +00:00
static SSDataBlock * doSortedMerge ( SOperatorInfo * pOperator , bool * newgroup ) {
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-04-07 06:51:52 +00:00
return getSortedBlockData ( pInfo - > pSortHandle , pInfo - > binfo . pRes , pInfo - > binfo . capacity ) ;
2022-02-08 07:40:13 +00:00
}
2022-02-19 08:15:09 +00:00
int32_t numOfBufPage = pInfo - > sortBufSize / pInfo - > bufPageSize ;
2022-03-29 07:24:25 +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-02-22 05:16:46 +00:00
tsortSetFetchRawDataFp ( pInfo - > pSortHandle , loadNextDataBlock ) ;
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-03-25 16:29:53 +00:00
SGenericSource * ps = taosMemoryCalloc ( 1 , sizeof ( SGenericSource ) ) ;
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-03-09 02:22:53 +00:00
if ( pe - > base . resSchema . colId = = 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-03-29 07:24:25 +00:00
pInfo - > binfo . pCtx = createSqlFunctionCtx_rv ( pExprInfo , num , & pInfo - > binfo . rowCellInfoOffset ) ;
2022-02-22 05:12:03 +00:00
initResultRowInfo ( & pInfo - > binfo . resultRowInfo , ( int32_t ) 1 ) ;
2022-02-15 06:40:29 +00:00
2022-02-22 05:12:03 +00:00
if ( pInfo - > binfo . pCtx = = NULL | | pInfo - > binfo . pRes = = NULL ) {
goto _error ;
}
2022-02-15 06:40:29 +00:00
2022-03-15 08:51:50 +00:00
int32_t code = doInitAggInfoSup ( & pInfo - > aggSup , pInfo - > binfo . pCtx , num , 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-03-09 02:22:53 +00:00
setFunctionResultOutput ( & pInfo - > binfo , & pInfo - > aggSup , MAIN_SCAN , pTaskInfo ) ;
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-02-22 05:12:03 +00:00
pInfo - > binfo . 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-02-19 08:15:09 +00:00
pOperator - > blockingOptr = true ;
2022-03-29 07:24:25 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > info = pInfo ;
pOperator - > numOfOutput = num ;
pOperator - > pExpr = pExprInfo ;
2022-02-15 06:40:29 +00:00
2022-03-29 07:24:25 +00:00
pOperator - > pTaskInfo = pTaskInfo ;
pOperator - > getNextFn = doSortedMerge ;
pOperator - > closeFn = destroySortedMergeOperatorInfo ;
2022-02-15 06:40:29 +00:00
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-03-29 07:24:25 +00:00
static SSDataBlock * doSort ( SOperatorInfo * pOperator , bool * newgroup ) {
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
2022-03-29 07:24:25 +00:00
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2022-03-28 11:08:07 +00:00
SSortOperatorInfo * pInfo = pOperator - > info ;
2022-02-08 10:01:21 +00:00
if ( pOperator - > status = = OP_RES_TO_RETURN ) {
2022-04-07 06:51:52 +00:00
return getSortedBlockData ( pInfo - > pSortHandle , pInfo - > pDataBlock , pInfo - > numOfRowsInRes ) ;
2022-02-08 10:01:21 +00:00
}
2022-02-19 08:15:09 +00:00
int32_t numOfBufPage = pInfo - > sortBufSize / pInfo - > bufPageSize ;
2022-03-29 07:24:25 +00:00
pInfo - > pSortHandle = tsortCreateSortHandle ( pInfo - > pSortInfo , SORT_SINGLESOURCE_SORT , pInfo - > bufPageSize , numOfBufPage ,
2022-04-07 06:51:52 +00:00
pInfo - > pDataBlock , pTaskInfo - > id . str ) ;
2021-11-02 05:37:31 +00:00
2022-02-22 05:16:46 +00:00
tsortSetFetchRawDataFp ( pInfo - > pSortHandle , loadNextDataBlock ) ;
2022-02-09 10:23:51 +00:00
2022-03-25 16:29:53 +00:00
SGenericSource * ps = taosMemoryCalloc ( 1 , sizeof ( SGenericSource ) ) ;
2022-03-28 11:08:07 +00:00
ps - > param = pOperator - > pDownstream [ 0 ] ;
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 08:15:09 +00:00
longjmp ( pTaskInfo - > env , terrno ) ;
2022-02-08 07:40:13 +00:00
}
2022-02-08 02:21:00 +00:00
2022-02-08 10:01:21 +00:00
pOperator - > status = OP_RES_TO_RETURN ;
2022-04-07 06:51:52 +00:00
return getSortedBlockData ( pInfo - > pSortHandle , pInfo - > pDataBlock , pInfo - > numOfRowsInRes ) ;
2021-11-02 05:37:31 +00:00
}
2022-04-07 06:51:52 +00:00
SOperatorInfo * createSortOperatorInfo ( SOperatorInfo * downstream , SSDataBlock * pResBlock , SArray * pSortInfo , SExecTaskInfo * pTaskInfo ) {
2022-03-28 11:08:07 +00:00
SSortOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( SSortOperatorInfo ) ) ;
2022-03-29 07:24:25 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
2022-02-08 10:07:03 +00:00
if ( pInfo = = NULL | | pOperator = = NULL ) {
2022-04-07 06:51:52 +00:00
goto _error ;
2022-02-08 10:07:03 +00:00
}
2021-11-02 05:37:31 +00:00
2022-04-07 06:51:52 +00:00
pInfo - > sortBufSize = 1024 * 16 ; // TODO dynamic set the available sort buffer
pInfo - > bufPageSize = 1024 ;
pInfo - > numOfRowsInRes = 1024 ;
pInfo - > pDataBlock = pResBlock ;
pInfo - > pSortInfo = pSortInfo ;
2022-02-08 10:07:03 +00:00
2022-04-07 06:51:52 +00:00
pOperator - > name = " SortOperator " ;
2022-03-29 07:24:25 +00:00
pOperator - > operatorType = QUERY_NODE_PHYSICAL_PLAN_SORT ;
pOperator - > blockingOptr = true ;
2022-04-07 06:51:52 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > info = pInfo ;
2022-02-19 08:15:09 +00:00
2022-04-07 06:51:52 +00:00
pOperator - > pTaskInfo = pTaskInfo ;
pOperator - > getNextFn = doSort ;
pOperator - > closeFn = destroyOrderOperatorInfo ;
2021-11-02 05:37:31 +00:00
2022-02-22 05:12:03 +00:00
int32_t code = appendDownstream ( pOperator , & downstream , 1 ) ;
2021-11-02 05:37:31 +00:00
return pOperator ;
2022-04-07 06:51:52 +00:00
_error :
pTaskInfo - > code = TSDB_CODE_OUT_OF_MEMORY ;
taosMemoryFree ( pInfo ) ;
taosMemoryFree ( pOperator ) ;
return NULL ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static int32_t getTableScanOrder ( STableScanInfo * pTableScanInfo ) { return pTableScanInfo - > order ; }
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
}
SAggOperatorInfo * pAggInfo = pOperator - > info ;
2022-03-12 10:02:56 +00:00
SOptrBasicInfo * pInfo = & pAggInfo - > binfo ;
2021-11-02 05:37:31 +00:00
2022-03-12 10:02:56 +00:00
int32_t order = TSDB_ORDER_ASC ;
2022-01-08 14:59:24 +00:00
SOperatorInfo * downstream = pOperator - > pDownstream [ 0 ] ;
2021-11-02 05:37:31 +00:00
2022-03-12 10:02:56 +00:00
bool newgroup = true ;
while ( 1 ) {
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_BEFORE_OPERATOR_EXEC ) ;
2022-03-12 10:02:56 +00:00
SSDataBlock * pBlock = downstream - > getNextFn ( downstream , & newgroup ) ;
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_AFTER_OPERATOR_EXEC ) ;
2021-11-02 05:37:31 +00:00
if ( pBlock = = NULL ) {
break ;
}
2022-03-12 10:02:56 +00:00
// if (pAggInfo->current != NULL) {
// setTagValue(pOperator, pAggInfo->current->pTable, pInfo->pCtx, pOperator->numOfOutput);
// }
2021-11-02 05:37:31 +00:00
// the pDataBlock are always the same one, no need to call this again
setInputDataBlock ( pOperator , pInfo - > pCtx , pBlock , order ) ;
2022-03-09 02:22:53 +00:00
doAggregateImpl ( pOperator , 0 , pInfo - > pCtx ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-15 06:37:26 +00:00
finalizeQueryResult ( pInfo - > pCtx , pOperator - > numOfOutput ) ;
2022-03-12 10:02:56 +00:00
OPTR_SET_OPENED ( pOperator ) ;
return TSDB_CODE_SUCCESS ;
}
2022-03-29 07:24:25 +00:00
static SSDataBlock * getAggregateResult ( SOperatorInfo * pOperator , bool * newgroup ) {
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-03-12 10:02:56 +00:00
pTaskInfo - > code = pOperator - > _openFn ( pOperator ) ;
if ( pTaskInfo - > code ! = TSDB_CODE_SUCCESS ) {
return NULL ;
}
2022-03-09 02:22:53 +00:00
getNumOfResult ( pInfo - > pCtx , pOperator - > numOfOutput , pInfo - > pRes ) ;
2022-03-12 10:02:56 +00:00
doSetOperatorCompleted ( pOperator ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
return ( blockDataGetNumOfRows ( pInfo - > pRes ) ! = 0 ) ? pInfo - > pRes : NULL ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static void aggEncodeResultRow ( SOperatorInfo * pOperator , char * * result , int32_t * length ) {
SAggOperatorInfo * pAggInfo = pOperator - > info ;
SAggSupporter * pSup = & pAggInfo - > aggSup ;
2022-03-24 09:24:01 +00:00
int32_t size = taosHashGetSize ( pSup - > pResultRowHashTable ) ;
2022-03-29 07:24:25 +00:00
size_t keyLen = POINTER_BYTES ; // estimate the key length
2022-03-24 09:24:01 +00:00
int32_t totalSize = sizeof ( int32_t ) + size * ( sizeof ( int32_t ) + keyLen + sizeof ( int32_t ) + pSup - > resultRowSize ) ;
2022-03-25 16:29:53 +00:00
* result = taosMemoryCalloc ( 1 , totalSize ) ;
2022-03-29 07:24:25 +00:00
if ( * result = = NULL ) {
2022-03-24 09:24:01 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY ;
return ;
}
* ( int32_t * ) ( * result ) = size ;
int32_t offset = sizeof ( int32_t ) ;
2022-03-29 07:24:25 +00:00
void * pIter = taosHashIterate ( pSup - > pResultRowHashTable , NULL ) ;
2022-03-24 09:24:01 +00:00
while ( pIter ) {
2022-03-29 07:24:25 +00:00
void * key = taosHashGetKey ( pIter , & keyLen ) ;
SResultRow * * p1 = ( SResultRow * * ) pIter ;
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 ) {
char * tmp = taosMemoryRealloc ( * result , realTotalSize ) ;
if ( tmp = = NULL ) {
2022-03-24 09:24:01 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY ;
2022-03-25 16:29:53 +00:00
taosMemoryFree ( * result ) ;
2022-03-24 09:24:01 +00:00
* result = NULL ;
return ;
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 ) ;
memcpy ( * result + offset , * p1 , pSup - > resultRowSize ) ;
offset + = pSup - > resultRowSize ;
pIter = taosHashIterate ( pSup - > pResultRowHashTable , pIter ) ;
}
2022-03-29 07:24:25 +00:00
if ( length ) {
2022-03-24 09:24:01 +00:00
* length = offset ;
}
return ;
}
2022-03-29 07:24:25 +00:00
static bool aggDecodeResultRow ( SOperatorInfo * pOperator , char * result , int32_t length ) {
if ( ! result | | length < = 0 ) {
2022-03-24 09:24:01 +00:00
return false ;
}
2022-03-29 07:24:25 +00:00
SAggOperatorInfo * pAggInfo = pOperator - > info ;
SAggSupporter * pSup = & pAggInfo - > aggSup ;
SOptrBasicInfo * pInfo = & pAggInfo - > binfo ;
2022-03-24 09:24:01 +00:00
// int32_t size = taosHashGetSize(pSup->pResultRowHashTable);
int32_t count = * ( int32_t * ) ( result ) ;
int32_t 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 ) ;
SResultRow * resultRow = getNewResultRow_rv ( pSup - > pResultBuf , tableGroupId , pSup - > resultRowSize ) ;
if ( ! resultRow ) {
2022-03-24 09:24:01 +00:00
terrno = TSDB_CODE_TSC_INVALID_INPUT ;
return false ;
}
// add a new result set for a new group
taosHashPut ( pSup - > pResultRowHashTable , result + offset , keyLen , & resultRow , POINTER_BYTES ) ;
offset + = keyLen ;
int32_t valueLen = * ( int32_t * ) ( result + offset ) ;
2022-03-29 07:24:25 +00:00
if ( valueLen ! = pSup - > resultRowSize ) {
2022-03-24 09:24:01 +00:00
terrno = TSDB_CODE_TSC_INVALID_INPUT ;
return false ;
}
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-03-29 07:24:25 +00:00
pInfo - > resultRowInfo . pPosition [ pInfo - > resultRowInfo . size + + ] =
( 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-03-24 09:24:01 +00:00
terrno = TSDB_CODE_TSC_INVALID_INPUT ;
return false ;
}
return true ;
}
2022-03-29 07:24:25 +00:00
static SSDataBlock * doMultiTableAggregate ( SOperatorInfo * pOperator , bool * newgroup ) {
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
SAggOperatorInfo * pAggInfo = pOperator - > info ;
2022-03-29 07:24:25 +00:00
SOptrBasicInfo * pInfo = & pAggInfo - > binfo ;
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_RES_TO_RETURN ) {
2022-03-29 07:24:25 +00:00
toSDatablock ( & pAggInfo - > groupResInfo , pAggInfo - > pResultBuf , pInfo - > pRes , pAggInfo - > binfo . capacity ,
pAggInfo - > binfo . rowCellInfoOffset ) ;
2021-11-02 05:37:31 +00:00
2022-02-14 07:09:32 +00:00
if ( pInfo - > pRes - > info . rows = = 0 | | ! hasRemainDataInCurrentGroup ( & pAggInfo - > groupResInfo ) ) {
2021-11-02 05:37:31 +00:00
pOperator - > status = OP_EXEC_DONE ;
}
return pInfo - > pRes ;
}
2022-02-14 02:58:58 +00:00
// table scan order
2022-03-29 07:24:25 +00:00
int32_t order = TSDB_ORDER_ASC ;
2022-01-08 14:59:24 +00:00
SOperatorInfo * downstream = pOperator - > pDownstream [ 0 ] ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
while ( 1 ) {
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_BEFORE_OPERATOR_EXEC ) ;
2022-03-05 07:58:28 +00:00
SSDataBlock * pBlock = downstream - > getNextFn ( downstream , newgroup ) ;
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_AFTER_OPERATOR_EXEC ) ;
2021-11-02 05:37:31 +00:00
if ( pBlock = = NULL ) {
break ;
}
2022-03-29 07:24:25 +00:00
// setTagValue(pOperator, pRuntimeEnv->current->pTable, pInfo->pCtx, pOperator->numOfOutput);
// if (downstream->operatorType == OP_TableScan) {
// STableScanInfo* pScanInfo = downstream->info;
// order = getTableScanOrder(pScanInfo);
// }
2021-11-02 05:37:31 +00:00
// the pDataBlock are always the same one, no need to call this again
setInputDataBlock ( pOperator , pInfo - > pCtx , pBlock , order ) ;
TSKEY key = 0 ;
2022-02-14 02:58:58 +00:00
if ( order = = TSDB_ORDER_ASC ) {
2021-11-02 05:37:31 +00:00
key = pBlock - > info . window . ekey ;
TSKEY_MAX_ADD ( key , 1 ) ;
} else {
key = pBlock - > info . window . skey ;
TSKEY_MIN_SUB ( key , - 1 ) ;
}
2022-03-29 07:24:25 +00:00
setExecutionContext ( pOperator - > numOfOutput , pAggInfo - > current - > groupIndex , key , pTaskInfo , pAggInfo - > current ,
pAggInfo ) ;
2022-03-09 02:22:53 +00:00
doAggregateImpl ( pOperator , 0 , pInfo - > pCtx ) ;
2021-11-02 05:37:31 +00:00
}
pOperator - > status = OP_RES_TO_RETURN ;
closeAllResultRows ( & pInfo - > resultRowInfo ) ;
2022-02-14 02:58:58 +00:00
updateNumOfRowsInResultRows ( pInfo - > pCtx , pOperator - > numOfOutput , & pInfo - > resultRowInfo , pInfo - > rowCellInfoOffset ) ;
2021-11-02 05:37:31 +00:00
2022-02-15 06:40:29 +00:00
initGroupResInfo ( & pAggInfo - > groupResInfo , & pInfo - > resultRowInfo ) ;
2022-03-29 07:24:25 +00:00
toSDatablock ( & pAggInfo - > groupResInfo , pAggInfo - > pResultBuf , pInfo - > pRes , pAggInfo - > binfo . capacity ,
pAggInfo - > binfo . rowCellInfoOffset ) ;
2021-11-02 05:37:31 +00:00
2022-02-15 06:40:29 +00:00
if ( pInfo - > pRes - > info . rows = = 0 | | ! hasRemainDataInCurrentGroup ( & pAggInfo - > groupResInfo ) ) {
2021-11-02 05:37:31 +00:00
doSetOperatorCompleted ( pOperator ) ;
}
return pInfo - > pRes ;
}
2022-03-29 07:24:25 +00:00
static SSDataBlock * doProjectOperation ( SOperatorInfo * pOperator , bool * newgroup ) {
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
SSDataBlock * pRes = pInfo - > pRes ;
2022-03-17 05:11:06 +00:00
blockDataCleanup ( pRes ) ;
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 ;
* newgroup = true ;
// todo dynamic set tags
2022-03-29 07:24:25 +00:00
// if (pTableQueryInfo != NULL) {
// setTagValue(pOperator, pTableQueryInfo->pTable, pInfo->pCtx, pOperator->numOfOutput);
// }
2021-11-02 05:37:31 +00:00
// 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-03-26 14:13:14 +00:00
projectApplyFunctions ( pOperator - > pExpr , pInfo - > pRes , pBlock , pInfo - > pCtx , pOperator - > numOfOutput ) ;
2022-03-29 07:24:25 +00:00
if ( pRes - > info . rows > = pProjectInfo - > binfo . capacity * 0.8 ) {
2021-11-02 05:37:31 +00:00
copyTsColoum ( pRes , pInfo - > pCtx , pOperator - > numOfOutput ) ;
resetResultRowEntryResult ( pInfo - > pCtx , pOperator - > numOfOutput ) ;
return pRes ;
}
}
2022-04-01 09:21:24 +00:00
# endif
2021-11-02 05:37:31 +00:00
2022-03-12 14:59:12 +00:00
SOperatorInfo * downstream = pOperator - > pDownstream [ 0 ] ;
2022-03-29 07:24:25 +00:00
while ( 1 ) {
2021-11-02 05:37:31 +00:00
bool prevVal = * newgroup ;
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-03-12 14:59:12 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_BEFORE_OPERATOR_EXEC ) ;
SSDataBlock * pBlock = downstream - > getNextFn ( downstream , newgroup ) ;
publishOperatorProfEvent ( downstream , QUERY_PROF_AFTER_OPERATOR_EXEC ) ;
2021-11-02 05:37:31 +00:00
if ( pBlock = = NULL ) {
assert ( * newgroup = = false ) ;
* newgroup = prevVal ;
2022-01-10 11:48:21 +00:00
setTaskStatus ( pOperator - > pTaskInfo , TASK_COMPLETED ) ;
2021-11-02 05:37:31 +00:00
break ;
}
// Return result of the previous group in the firstly.
if ( * newgroup ) {
if ( pRes - > info . rows > 0 ) {
pProjectInfo - > existDataBlock = pBlock ;
break ;
2022-03-29 07:24:25 +00:00
} else { // init output buffer for a new group data
2021-11-02 05:37:31 +00:00
initCtxOutputBuffer ( pInfo - > pCtx , pOperator - > numOfOutput ) ;
}
}
// todo dynamic set tags
2022-03-12 14:59:12 +00:00
// STableQueryInfo* pTableQueryInfo = pRuntimeEnv->current;
// if (pTableQueryInfo != NULL) {
// setTagValue(pOperator, pTableQueryInfo->pTable, pInfo->pCtx, pOperator->numOfOutput);
// }
2021-11-02 05:37:31 +00:00
// 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 ) ;
2022-03-27 14:43:07 +00:00
blockDataEnsureCapacity ( pInfo - > pRes , pInfo - > pRes - > info . rows + pBlock - > info . rows ) ;
2022-04-06 03:01:09 +00:00
projectApplyFunctions ( pOperator - > pExpr , pInfo - > pRes , pBlock , pInfo - > pCtx , pOperator - > numOfOutput , pProjectInfo - > pPseudoColInfo ) ;
2022-04-01 09:21:24 +00:00
2022-04-06 03:01:09 +00:00
// todo extract method
2022-04-01 09:21:24 +00:00
if ( pProjectInfo - > curOffset < pInfo - > pRes - > info . rows & & pProjectInfo - > curOffset > 0 ) {
blockDataTrimFirstNRows ( pInfo - > pRes , pProjectInfo - > curOffset ) ;
pProjectInfo - > curOffset = 0 ;
} else if ( pProjectInfo - > curOffset > = pInfo - > pRes - > info . rows ) {
pProjectInfo - > curOffset - = pInfo - > pRes - > info . rows ;
blockDataCleanup ( pInfo - > pRes ) ;
continue ;
}
2022-03-26 14:13:14 +00:00
if ( pRes - > info . rows > = pOperator - > resultInfo . threshold ) {
2021-11-02 05:37:31 +00:00
break ;
}
}
2022-04-01 09:21:24 +00:00
2022-04-01 10:08:36 +00:00
if ( pProjectInfo - > limit . limit > 0 & & pProjectInfo - > curOutput + pInfo - > pRes - > info . rows > = pProjectInfo - > limit . limit ) {
2022-04-01 09:21:24 +00:00
pInfo - > pRes - > info . rows = ( int32_t ) ( pProjectInfo - > limit . limit - pProjectInfo - > curOutput ) ;
}
pProjectInfo - > curOutput + = pInfo - > pRes - > info . rows ;
2022-03-12 14:59:12 +00:00
2022-03-29 07:24:25 +00:00
// copyTsColoum(pRes, pInfo->pCtx, pOperator->numOfOutput);
return ( pInfo - > pRes - > info . rows > 0 ) ? pInfo - > pRes : NULL ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static int32_t doOpenIntervalAgg ( SOperatorInfo * pOperator ) {
2022-03-15 06:37:26 +00:00
if ( OPTR_IS_OPENED ( pOperator ) ) {
return TSDB_CODE_SUCCESS ;
2021-11-02 05:37:31 +00:00
}
2022-02-24 09:18:56 +00:00
STableIntervalOperatorInfo * pInfo = pOperator - > info ;
2021-11-02 05:37:31 +00:00
2022-03-29 06:36:08 +00:00
int32_t order = TSDB_ORDER_ASC ;
2022-03-29 12:07:38 +00:00
// STimeWindow win = {0};
bool newgroup = false ;
2022-01-08 14:59:24 +00:00
SOperatorInfo * downstream = pOperator - > pDownstream [ 0 ] ;
2021-11-02 05:37:31 +00:00
2022-03-15 06:37:26 +00:00
while ( 1 ) {
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_BEFORE_OPERATOR_EXEC ) ;
2022-03-15 06:37:26 +00:00
SSDataBlock * pBlock = downstream - > getNextFn ( downstream , & newgroup ) ;
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_AFTER_OPERATOR_EXEC ) ;
2021-11-02 05:37:31 +00:00
if ( pBlock = = NULL ) {
break ;
}
2022-03-15 06:37:26 +00:00
// setTagValue(pOperator, pRuntimeEnv->current->pTable, pInfo->pCtx, pOperator->numOfOutput);
2021-11-02 05:37:31 +00:00
// the pDataBlock are always the same one, no need to call this again
2022-03-29 06:36:08 +00:00
setInputDataBlock ( pOperator , pInfo - > binfo . pCtx , pBlock , order ) ;
2022-02-24 09:18:56 +00:00
hashIntervalAgg ( pOperator , & pInfo - > binfo . resultRowInfo , pBlock , 0 ) ;
2021-11-02 05:37:31 +00:00
}
2022-02-24 09:18:56 +00:00
closeAllResultRows ( & pInfo - > binfo . resultRowInfo ) ;
2022-03-29 07:24:25 +00:00
finalizeMultiTupleQueryResult ( pInfo - > binfo . pCtx , pOperator - > numOfOutput , pInfo - > aggSup . pResultBuf ,
& pInfo - > binfo . resultRowInfo , pInfo - > binfo . rowCellInfoOffset ) ;
2021-11-02 05:37:31 +00:00
2022-02-24 09:18:56 +00:00
initGroupResInfo ( & pInfo - > groupResInfo , & pInfo - > binfo . resultRowInfo ) ;
2022-03-15 06:37:26 +00:00
OPTR_SET_OPENED ( pOperator ) ;
return TSDB_CODE_SUCCESS ;
}
2022-03-29 07:24:25 +00:00
static SSDataBlock * doBuildIntervalResult ( SOperatorInfo * pOperator , bool * newgroup ) {
2022-03-15 06:37:26 +00:00
STableIntervalOperatorInfo * pInfo = pOperator - > info ;
2022-03-29 07:24:25 +00:00
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2022-03-15 06:37:26 +00:00
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
2022-03-30 06:54:00 +00:00
if ( pInfo - > execModel = = OPTR_EXEC_MODEL_STREAM ) {
return pOperator - > getStreamResFn ( pOperator , newgroup ) ;
}
2022-03-15 06:37:26 +00:00
pTaskInfo - > code = pOperator - > _openFn ( pOperator ) ;
if ( pTaskInfo - > code ! = TSDB_CODE_SUCCESS ) {
return NULL ;
}
blockDataEnsureCapacity ( pInfo - > binfo . pRes , pInfo - > binfo . capacity ) ;
2022-03-29 07:24:25 +00:00
toSDatablock ( & pInfo - > groupResInfo , pInfo - > aggSup . pResultBuf , pInfo - > binfo . pRes , pInfo - > binfo . capacity ,
pInfo - > binfo . rowCellInfoOffset ) ;
2021-11-02 05:37:31 +00:00
2022-02-24 09:18:56 +00:00
if ( pInfo - > binfo . pRes - > info . rows = = 0 | | ! hasRemainDataInCurrentGroup ( & pInfo - > groupResInfo ) ) {
2021-11-02 05:37:31 +00:00
doSetOperatorCompleted ( pOperator ) ;
}
2022-03-29 07:24:25 +00:00
return pInfo - > binfo . pRes - > info . rows = = 0 ? NULL : pInfo - > binfo . pRes ;
2021-11-02 05:37:31 +00:00
}
2022-03-30 06:54:00 +00:00
static SSDataBlock * doStreamIntervalAgg ( SOperatorInfo * pOperator , bool * newgroup ) {
2022-03-29 12:07:38 +00:00
STableIntervalOperatorInfo * pInfo = pOperator - > info ;
int32_t order = TSDB_ORDER_ASC ;
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
if ( pOperator - > status = = OP_RES_TO_RETURN ) {
toSDatablock ( & pInfo - > groupResInfo , pInfo - > aggSup . pResultBuf , pInfo - > binfo . pRes , pInfo - > binfo . capacity ,
pInfo - > binfo . rowCellInfoOffset ) ;
if ( pInfo - > binfo . pRes - > info . rows = = 0 | | ! hasRemainDataInCurrentGroup ( & pInfo - > groupResInfo ) ) {
pOperator - > status = OP_EXEC_DONE ;
}
2022-03-30 11:38:30 +00:00
return pInfo - > binfo . pRes - > info . rows = = 0 ? NULL : pInfo - > binfo . pRes ;
2022-03-29 12:07:38 +00:00
}
// STimeWindow win = {0};
2022-03-30 06:54:00 +00:00
* newgroup = false ;
2022-03-29 12:07:38 +00:00
SOperatorInfo * downstream = pOperator - > pDownstream [ 0 ] ;
SArray * pUpdated = NULL ;
while ( 1 ) {
publishOperatorProfEvent ( downstream , QUERY_PROF_BEFORE_OPERATOR_EXEC ) ;
2022-03-30 06:54:00 +00:00
SSDataBlock * pBlock = downstream - > getNextFn ( downstream , newgroup ) ;
2022-03-29 12:07:38 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_AFTER_OPERATOR_EXEC ) ;
if ( pBlock = = NULL ) {
break ;
}
// The timewindows that overlaps the timestamps of the input pBlock need to be recalculated and return to the caller.
// Note that all the time window are not close till now.
// setTagValue(pOperator, pRuntimeEnv->current->pTable, pInfo->pCtx, pOperator->numOfOutput);
// the pDataBlock are always the same one, no need to call this again
setInputDataBlock ( pOperator , pInfo - > binfo . pCtx , pBlock , order ) ;
pUpdated = hashIntervalAgg ( pOperator , & pInfo - > binfo . resultRowInfo , pBlock , 0 ) ;
}
finalizeUpdatedResult ( pInfo - > binfo . pCtx , pOperator - > numOfOutput , pInfo - > aggSup . pResultBuf , pUpdated , pInfo - > binfo . rowCellInfoOffset ) ;
2022-03-30 09:09:06 +00:00
initMultiResInfoFromArrayList ( & pInfo - > groupResInfo , pUpdated ) ;
2022-03-29 12:07:38 +00:00
blockDataEnsureCapacity ( pInfo - > binfo . pRes , pInfo - > binfo . capacity ) ;
toSDatablock ( & pInfo - > groupResInfo , pInfo - > aggSup . pResultBuf , pInfo - > binfo . pRes , pInfo - > binfo . capacity ,
pInfo - > binfo . rowCellInfoOffset ) ;
ASSERT ( pInfo - > binfo . pRes - > info . rows > 0 ) ;
pOperator - > status = OP_RES_TO_RETURN ;
return pInfo - > binfo . pRes - > info . rows = = 0 ? NULL : pInfo - > binfo . pRes ;
}
static SSDataBlock * doAllIntervalAgg ( SOperatorInfo * pOperator , bool * newgroup ) {
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
STableIntervalOperatorInfo * pIntervalInfo = pOperator - > info ;
2022-01-08 08:28:44 +00:00
STaskRuntimeEnv * pRuntimeEnv = pOperator - > pRuntimeEnv ;
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_RES_TO_RETURN ) {
2022-03-29 07:24:25 +00:00
// toSDatablock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pIntervalInfo->pRes);
2021-11-02 05:37:31 +00:00
2022-02-24 09:18:56 +00:00
if ( pIntervalInfo - > binfo . pRes - > info . rows = = 0 | | ! hasRemainDataInCurrentGroup ( & pRuntimeEnv - > groupResInfo ) ) {
2021-11-02 05:37:31 +00:00
doSetOperatorCompleted ( pOperator ) ;
}
2022-02-24 09:18:56 +00:00
return pIntervalInfo - > binfo . pRes ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
STaskAttr * pQueryAttr = pRuntimeEnv - > pQueryAttr ;
int32_t order = pQueryAttr - > order . order ;
2021-11-02 05:37:31 +00:00
STimeWindow win = pQueryAttr - > window ;
2022-01-08 14:59:24 +00:00
SOperatorInfo * downstream = pOperator - > pDownstream [ 0 ] ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
while ( 1 ) {
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_BEFORE_OPERATOR_EXEC ) ;
2022-03-05 07:58:28 +00:00
SSDataBlock * pBlock = downstream - > getNextFn ( downstream , newgroup ) ;
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_AFTER_OPERATOR_EXEC ) ;
2021-11-02 05:37:31 +00:00
if ( pBlock = = NULL ) {
break ;
}
2022-03-29 07:24:25 +00:00
// setTagValue(pOperator, pRuntimeEnv->current->pTable, pIntervalInfo->pCtx, pOperator->numOfOutput);
2021-11-02 05:37:31 +00:00
// the pDataBlock are always the same one, no need to call this again
2022-02-24 09:18:56 +00:00
setInputDataBlock ( pOperator , pIntervalInfo - > binfo . pCtx , pBlock , pQueryAttr - > order . order ) ;
hashAllIntervalAgg ( pOperator , & pIntervalInfo - > binfo . resultRowInfo , pBlock , 0 ) ;
2021-11-02 05:37:31 +00:00
}
// restore the value
pQueryAttr - > order . order = order ;
pQueryAttr - > window = win ;
pOperator - > status = OP_RES_TO_RETURN ;
2022-02-24 09:18:56 +00:00
closeAllResultRows ( & pIntervalInfo - > binfo . resultRowInfo ) ;
2022-01-10 11:48:21 +00:00
setTaskStatus ( pOperator - > pTaskInfo , TASK_COMPLETED ) ;
2022-03-15 06:37:26 +00:00
finalizeQueryResult ( pIntervalInfo - > binfo . pCtx , pOperator - > numOfOutput ) ;
2021-11-02 05:37:31 +00:00
2022-02-24 09:18:56 +00:00
initGroupResInfo ( & pRuntimeEnv - > groupResInfo , & pIntervalInfo - > binfo . resultRowInfo ) ;
2022-03-29 07:24:25 +00:00
// toSDatablock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pIntervalInfo->pRes);
2021-11-02 05:37:31 +00:00
2022-02-24 09:18:56 +00:00
if ( pIntervalInfo - > binfo . pRes - > info . rows = = 0 | | ! hasRemainDataInCurrentGroup ( & pRuntimeEnv - > groupResInfo ) ) {
2021-11-02 05:37:31 +00:00
pOperator - > status = OP_EXEC_DONE ;
}
2022-03-29 07:24:25 +00:00
return pIntervalInfo - > binfo . pRes - > info . rows = = 0 ? NULL : pIntervalInfo - > binfo . pRes ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static SSDataBlock * doSTableIntervalAgg ( SOperatorInfo * pOperator , bool * newgroup ) {
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
STableIntervalOperatorInfo * pIntervalInfo = pOperator - > info ;
2022-03-29 07:24:25 +00:00
STaskRuntimeEnv * pRuntimeEnv = pOperator - > pRuntimeEnv ;
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_RES_TO_RETURN ) {
int64_t st = taosGetTimestampUs ( ) ;
2022-03-29 07:24:25 +00:00
// copyToSDataBlock(NULL, 3000, pIntervalInfo->pRes, pIntervalInfo->rowCellInfoOffset);
2022-02-24 09:18:56 +00:00
if ( pIntervalInfo - > binfo . pRes - > info . rows = = 0 | | ! hasRemainData ( & pRuntimeEnv - > groupResInfo ) ) {
2021-11-02 05:37:31 +00:00
doSetOperatorCompleted ( pOperator ) ;
}
2022-02-24 09:18:56 +00:00
return pIntervalInfo - > binfo . pRes ;
2021-11-02 05:37:31 +00:00
}
2022-01-08 08:28:44 +00:00
STaskAttr * pQueryAttr = pRuntimeEnv - > pQueryAttr ;
2022-03-29 07:24:25 +00:00
int32_t order = pQueryAttr - > order . order ;
2021-11-02 05:37:31 +00:00
2022-01-08 14:59:24 +00:00
SOperatorInfo * downstream = pOperator - > pDownstream [ 0 ] ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
while ( 1 ) {
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_BEFORE_OPERATOR_EXEC ) ;
2022-03-05 07:58:28 +00:00
SSDataBlock * pBlock = downstream - > getNextFn ( downstream , newgroup ) ;
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_AFTER_OPERATOR_EXEC ) ;
2021-11-02 05:37:31 +00:00
if ( pBlock = = NULL ) {
break ;
}
// the pDataBlock are always the same one, no need to call this again
STableQueryInfo * pTableQueryInfo = pRuntimeEnv - > current ;
2022-03-29 07:24:25 +00:00
// setTagValue(pOperator, pTableQueryInfo->pTable, pIntervalInfo->pCtx, pOperator->numOfOutput);
2022-02-24 09:18:56 +00:00
setInputDataBlock ( pOperator , pIntervalInfo - > binfo . pCtx , pBlock , pQueryAttr - > order . order ) ;
2021-11-02 05:37:31 +00:00
setIntervalQueryRange ( pRuntimeEnv , pBlock - > info . window . skey ) ;
hashIntervalAgg ( pOperator , & pTableQueryInfo - > resInfo , pBlock , pTableQueryInfo - > groupIndex ) ;
}
pOperator - > status = OP_RES_TO_RETURN ;
2022-03-29 07:24:25 +00:00
pQueryAttr - > order . order = order ; // TODO : restore the order
2021-11-02 05:37:31 +00:00
doCloseAllTimeWindow ( pRuntimeEnv ) ;
2022-01-10 11:48:21 +00:00
setTaskStatus ( pOperator - > pTaskInfo , TASK_COMPLETED ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// copyToSDataBlock(pRuntimeEnv, 3000, pIntervalInfo->pRes, pIntervalInfo->rowCellInfoOffset);
2022-02-24 09:18:56 +00:00
if ( pIntervalInfo - > binfo . pRes - > info . rows = = 0 | | ! hasRemainData ( & pRuntimeEnv - > groupResInfo ) ) {
2021-11-02 05:37:31 +00:00
pOperator - > status = OP_EXEC_DONE ;
}
2022-02-24 09:18:56 +00:00
return pIntervalInfo - > binfo . pRes ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static SSDataBlock * doAllSTableIntervalAgg ( SOperatorInfo * pOperator , bool * newgroup ) {
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
STableIntervalOperatorInfo * pIntervalInfo = pOperator - > info ;
2022-03-29 07:24:25 +00:00
STaskRuntimeEnv * pRuntimeEnv = pOperator - > pRuntimeEnv ;
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_RES_TO_RETURN ) {
2022-03-29 07:24:25 +00:00
// copyToSDataBlock(pRuntimeEnv, 3000, pIntervalInfo->pRes, pIntervalInfo->rowCellInfoOffset);
2022-02-24 09:18:56 +00:00
if ( pIntervalInfo - > binfo . pRes - > info . rows = = 0 | | ! hasRemainData ( & pRuntimeEnv - > groupResInfo ) ) {
2021-11-02 05:37:31 +00:00
pOperator - > status = OP_EXEC_DONE ;
}
2022-02-24 09:18:56 +00:00
return pIntervalInfo - > binfo . pRes ;
2021-11-02 05:37:31 +00:00
}
2022-01-08 14:59:24 +00:00
SOperatorInfo * downstream = pOperator - > pDownstream [ 0 ] ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
while ( 1 ) {
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_BEFORE_OPERATOR_EXEC ) ;
2022-03-05 07:58:28 +00:00
SSDataBlock * pBlock = downstream - > getNextFn ( downstream , newgroup ) ;
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_AFTER_OPERATOR_EXEC ) ;
2021-11-02 05:37:31 +00:00
if ( pBlock = = NULL ) {
break ;
}
// the pDataBlock are always the same one, no need to call this again
STableQueryInfo * pTableQueryInfo = pRuntimeEnv - > current ;
2022-03-29 07:24:25 +00:00
// setTagValue(pOperator, pTableQueryInfo->pTable, pIntervalInfo->pCtx, pOperator->numOfOutput);
2022-04-02 07:08:48 +00:00
// setInputDataBlock(pOperator, pIntervalInfo->binfo.pCtx, pBlock, pQueryAttr->order.order);
2021-11-02 05:37:31 +00:00
setIntervalQueryRange ( pRuntimeEnv , pBlock - > info . window . skey ) ;
hashAllIntervalAgg ( pOperator , & pTableQueryInfo - > resInfo , pBlock , pTableQueryInfo - > groupIndex ) ;
}
pOperator - > status = OP_RES_TO_RETURN ;
2022-04-02 07:08:48 +00:00
// pQueryAttr->order.order = order; // TODO : restore the order
2021-11-02 05:37:31 +00:00
doCloseAllTimeWindow ( pRuntimeEnv ) ;
2022-01-10 11:48:21 +00:00
setTaskStatus ( pOperator - > pTaskInfo , TASK_COMPLETED ) ;
2021-11-02 05:37:31 +00:00
int64_t st = taosGetTimestampUs ( ) ;
2022-03-29 07:24:25 +00:00
// copyToSDataBlock(pRuntimeEnv, 3000, pIntervalInfo->pRes, pIntervalInfo->rowCellInfoOffset);
2022-02-24 09:18:56 +00:00
if ( pIntervalInfo - > binfo . pRes - > info . rows = = 0 | | ! hasRemainData ( & pRuntimeEnv - > groupResInfo ) ) {
2021-11-02 05:37:31 +00:00
pOperator - > status = OP_EXEC_DONE ;
}
2022-03-29 07:24:25 +00:00
// SQInfo* pQInfo = pRuntimeEnv->qinfo;
// pQInfo->summary.firstStageMergeTime += (taosGetTimestampUs() - st);
2021-11-02 05:37:31 +00:00
2022-02-24 09:18:56 +00:00
return pIntervalInfo - > binfo . pRes ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static void doStateWindowAggImpl ( SOperatorInfo * pOperator , SStateWindowOperatorInfo * pInfo , SSDataBlock * pSDataBlock ) {
2022-01-08 08:28:44 +00:00
STaskRuntimeEnv * pRuntimeEnv = pOperator - > pRuntimeEnv ;
2022-03-29 07:24:25 +00:00
STableQueryInfo * item = pRuntimeEnv - > current ;
2021-11-02 05:37:31 +00:00
SColumnInfoData * pColInfoData = taosArrayGet ( pSDataBlock - > pDataBlock , pInfo - > colIndex ) ;
SOptrBasicInfo * pBInfo = & pInfo - > binfo ;
2022-01-08 14:59:24 +00:00
bool masterScan = IS_MAIN_SCAN ( pRuntimeEnv ) ;
2022-03-29 07:24:25 +00:00
int16_t bytes = pColInfoData - > info . bytes ;
int16_t type = pColInfoData - > info . type ;
2021-11-02 05:37:31 +00:00
SColumnInfoData * pTsColInfoData = taosArrayGet ( pSDataBlock - > pDataBlock , 0 ) ;
2022-03-29 07:24:25 +00:00
TSKEY * tsList = ( TSKEY * ) pTsColInfoData - > pData ;
2021-11-02 05:37:31 +00:00
if ( IS_REPEAT_SCAN ( pRuntimeEnv ) & & ! pInfo - > reptScan ) {
pInfo - > reptScan = true ;
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pInfo - > prevData ) ;
2021-11-02 05:37:31 +00:00
}
pInfo - > numOfRows = 0 ;
for ( int32_t j = 0 ; j < pSDataBlock - > info . rows ; + + j ) {
char * val = ( ( char * ) pColInfoData - > pData ) + bytes * j ;
if ( isNull ( val , type ) ) {
continue ;
}
if ( pInfo - > prevData = = NULL ) {
2022-03-25 16:29:53 +00:00
pInfo - > prevData = taosMemoryMalloc ( bytes ) ;
2021-11-02 05:37:31 +00:00
memcpy ( pInfo - > prevData , val , bytes ) ;
pInfo - > numOfRows = 1 ;
pInfo - > curWindow . skey = tsList [ j ] ;
pInfo - > curWindow . ekey = tsList [ j ] ;
pInfo - > start = j ;
} else if ( memcmp ( pInfo - > prevData , val , bytes ) = = 0 ) {
pInfo - > curWindow . ekey = tsList [ j ] ;
pInfo - > numOfRows + = 1 ;
2022-03-29 07:24:25 +00:00
// pInfo->start = j;
2021-11-02 05:37:31 +00:00
if ( j = = 0 & & pInfo - > start ! = 0 ) {
pInfo - > numOfRows = 1 ;
pInfo - > start = 0 ;
}
} else {
SResultRow * pResult = NULL ;
pInfo - > curWindow . ekey = pInfo - > curWindow . skey ;
2022-03-29 07:24:25 +00:00
int32_t ret = setResultOutputBufByKey ( pRuntimeEnv , & pBInfo - > resultRowInfo , pSDataBlock - > info . uid ,
& pInfo - > curWindow , masterScan , & pResult , item - > groupIndex , pBInfo - > pCtx ,
pOperator - > numOfOutput , pBInfo - > rowCellInfoOffset ) ;
2021-11-02 05:37:31 +00:00
if ( ret ! = TSDB_CODE_SUCCESS ) { // null data, too many state code
longjmp ( pRuntimeEnv - > env , TSDB_CODE_QRY_APP_ERROR ) ;
}
2022-03-29 07:24:25 +00:00
// doApplyFunctions(pRuntimeEnv, pBInfo->pCtx, &pInfo->curWindow, pInfo->start, pInfo->numOfRows, tsList,
// pSDataBlock->info.rows, pOperator->numOfOutput);
2021-11-02 05:37:31 +00:00
pInfo - > curWindow . skey = tsList [ j ] ;
pInfo - > curWindow . ekey = tsList [ j ] ;
memcpy ( pInfo - > prevData , val , bytes ) ;
pInfo - > numOfRows = 1 ;
pInfo - > start = j ;
}
}
SResultRow * pResult = NULL ;
pInfo - > curWindow . ekey = pInfo - > curWindow . skey ;
2022-03-29 07:24:25 +00:00
int32_t ret = setResultOutputBufByKey ( pRuntimeEnv , & pBInfo - > resultRowInfo , pSDataBlock - > info . uid , & pInfo - > curWindow ,
masterScan , & pResult , item - > groupIndex , pBInfo - > pCtx , pOperator - > numOfOutput ,
2021-11-02 05:37:31 +00:00
pBInfo - > rowCellInfoOffset ) ;
if ( ret ! = TSDB_CODE_SUCCESS ) { // null data, too many state code
longjmp ( pRuntimeEnv - > env , TSDB_CODE_QRY_APP_ERROR ) ;
}
2022-03-29 07:24:25 +00:00
// doApplyFunctions(pRuntimeEnv, pBInfo->pCtx, &pInfo->curWindow, pInfo->start, pInfo->numOfRows, tsList,
// pSDataBlock->info.rows, pOperator->numOfOutput);
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static SSDataBlock * doStateWindowAgg ( SOperatorInfo * pOperator , bool * newgroup ) {
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
SStateWindowOperatorInfo * pWindowInfo = pOperator - > info ;
2022-04-01 10:08:36 +00:00
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
SOptrBasicInfo * pBInfo = & pWindowInfo - > binfo ;
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_RES_TO_RETURN ) {
2022-03-29 07:24:25 +00:00
// toSDatablock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pBInfo->pRes);
2021-11-02 05:37:31 +00:00
2022-04-01 10:08:36 +00:00
// if (pBInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pRuntimeEnv->groupResInfo)) {
// pOperator->status = OP_EXEC_DONE;
// }
2021-11-02 05:37:31 +00:00
return pBInfo - > pRes ;
}
2022-04-01 10:08:36 +00:00
int32_t order = TSDB_ORDER_ASC ;
STimeWindow win = pTaskInfo - > window ;
2022-01-08 14:59:24 +00:00
SOperatorInfo * downstream = pOperator - > pDownstream [ 0 ] ;
2021-11-02 05:37:31 +00:00
while ( 1 ) {
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_BEFORE_OPERATOR_EXEC ) ;
2022-03-05 07:58:28 +00:00
SSDataBlock * pBlock = downstream - > getNextFn ( downstream , newgroup ) ;
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_AFTER_OPERATOR_EXEC ) ;
2021-11-02 05:37:31 +00:00
if ( pBlock = = NULL ) {
break ;
}
2022-04-01 10:08:36 +00:00
// setInputDataBlock(pOperator, pBInfo->pCtx, pDataBlock, TSDB_ORDER_ASC);
// if (pWindowInfo->colIndex == -1) {
// pWindowInfo->colIndex = getGroupbyColumnIndex(pRuntimeEnv->pQueryAttr->pGroupbyExpr, pBlock);
// }
2022-03-29 07:24:25 +00:00
doStateWindowAggImpl ( pOperator , pWindowInfo , pBlock ) ;
2021-11-02 05:37:31 +00:00
}
// restore the value
2022-04-01 10:08:36 +00:00
// pQueryAttr->order.order = order;
// pQueryAttr->window = win;
2021-11-02 05:37:31 +00:00
pOperator - > status = OP_RES_TO_RETURN ;
closeAllResultRows ( & pBInfo - > resultRowInfo ) ;
2022-01-10 11:48:21 +00:00
setTaskStatus ( pOperator - > pTaskInfo , TASK_COMPLETED ) ;
2022-03-15 06:37:26 +00:00
finalizeQueryResult ( pBInfo - > pCtx , pOperator - > numOfOutput ) ;
2021-11-02 05:37:31 +00:00
2022-04-01 10:08:36 +00:00
// initGroupResInfo(&pRuntimeEnv->groupResInfo, &pBInfo->resultRowInfo);
2022-03-29 07:24:25 +00:00
// toSDatablock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pBInfo->pRes);
2021-11-02 05:37:31 +00:00
2022-04-01 10:08:36 +00:00
// if (pBInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pRuntimeEnv->groupResInfo)) {
// pOperator->status = OP_EXEC_DONE;
// }
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
return pBInfo - > pRes - > info . rows = = 0 ? NULL : pBInfo - > pRes ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
static SSDataBlock * doSessionWindowAgg ( SOperatorInfo * pOperator , bool * newgroup ) {
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
2022-03-29 06:36:08 +00:00
SSessionAggOperatorInfo * pInfo = pOperator - > info ;
2022-03-29 07:24:25 +00:00
SOptrBasicInfo * pBInfo = & pInfo - > binfo ;
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_RES_TO_RETURN ) {
2022-04-02 05:32:26 +00:00
toSDatablock ( & pInfo - > groupResInfo , pInfo - > aggSup . pResultBuf , pBInfo - > pRes , pBInfo - > capacity , pBInfo - > rowCellInfoOffset ) ;
2022-03-29 06:36:08 +00:00
if ( pBInfo - > pRes - > info . rows = = 0 | | ! hasRemainDataInCurrentGroup ( & pInfo - > groupResInfo ) ) {
doSetOperatorCompleted ( pOperator ) ;
return NULL ;
2021-11-02 05:37:31 +00:00
}
return pBInfo - > pRes ;
}
2022-03-29 07:24:25 +00:00
int32_t order = TSDB_ORDER_ASC ;
2022-01-08 14:59:24 +00:00
SOperatorInfo * downstream = pOperator - > pDownstream [ 0 ] ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
while ( 1 ) {
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_BEFORE_OPERATOR_EXEC ) ;
2022-03-05 07:58:28 +00:00
SSDataBlock * pBlock = downstream - > getNextFn ( downstream , newgroup ) ;
2022-01-08 14:59:24 +00:00
publishOperatorProfEvent ( downstream , QUERY_PROF_AFTER_OPERATOR_EXEC ) ;
2021-11-02 05:37:31 +00:00
if ( pBlock = = NULL ) {
break ;
}
// the pDataBlock are always the same one, no need to call this again
2022-03-12 15:40:04 +00:00
setInputDataBlock ( pOperator , pBInfo - > pCtx , pBlock , order ) ;
2022-03-29 06:36:08 +00:00
doSessionWindowAggImpl ( pOperator , pInfo , pBlock ) ;
2021-11-02 05:37:31 +00:00
}
// restore the value
pOperator - > status = OP_RES_TO_RETURN ;
closeAllResultRows ( & pBInfo - > resultRowInfo ) ;
2022-04-02 05:32:26 +00:00
finalizeMultiTupleQueryResult ( pBInfo - > pCtx , pOperator - > numOfOutput , pInfo - > aggSup . pResultBuf , & pBInfo - > resultRowInfo , pBInfo - > rowCellInfoOffset ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 06:36:08 +00:00
initGroupResInfo ( & pInfo - > groupResInfo , & pBInfo - > resultRowInfo ) ;
blockDataEnsureCapacity ( pBInfo - > pRes , pBInfo - > capacity ) ;
2022-04-02 05:32:26 +00:00
toSDatablock ( & pInfo - > groupResInfo , pInfo - > aggSup . pResultBuf , pBInfo - > pRes , pBInfo - > capacity , pBInfo - > rowCellInfoOffset ) ;
2022-03-29 06:36:08 +00:00
if ( pBInfo - > pRes - > info . rows = = 0 | | ! hasRemainDataInCurrentGroup ( & pInfo - > groupResInfo ) ) {
doSetOperatorCompleted ( pOperator ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
return pBInfo - > pRes - > info . rows = = 0 ? NULL : pBInfo - > pRes ;
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-03-29 07:24:25 +00:00
int64_t ekey = Q_STATUS_EQUAL ( pTaskInfo - > status , TASK_COMPLETED ) ? pTaskInfo - > window . ekey
: 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-03-24 06:21:14 +00:00
doFillTimeIntervalGapsInResults ( pInfo - > pFillInfo , pInfo - > pRes , pResultInfo - > capacity , pInfo - > p ) ;
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-03-24 06:21:14 +00:00
doFillTimeIntervalGapsInResults ( pInfo - > pFillInfo , pInfo - > pRes , ( int32_t ) pResultInfo - > capacity , pInfo - > p ) ;
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-03-29 07:24:25 +00:00
static SSDataBlock * doFill ( SOperatorInfo * pOperator , bool * newgroup ) {
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 ;
blockDataCleanup ( pInfo - > pRes ) ;
2021-11-02 05:37:31 +00:00
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
2022-03-24 09:26:04 +00:00
doHandleRemainBlockFromNewGroup ( pInfo , pResultInfo , newgroup , pTaskInfo ) ;
2022-03-24 06:21:14 +00:00
if ( pInfo - > pRes - > info . rows > pResultInfo - > threshold | | ( ! pInfo - > multigroupResult & & pInfo - > pRes - > info . rows > 0 ) ) {
return pInfo - > pRes ;
}
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-03-24 03:15:05 +00:00
publishOperatorProfEvent ( pDownstream , QUERY_PROF_BEFORE_OPERATOR_EXEC ) ;
SSDataBlock * pBlock = pDownstream - > getNextFn ( pDownstream , newgroup ) ;
publishOperatorProfEvent ( pDownstream , QUERY_PROF_AFTER_OPERATOR_EXEC ) ;
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-03-24 09:26:04 +00:00
taosFillSetStartInfo ( pInfo - > pFillInfo , 0 , pTaskInfo - > window . 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-03-24 09:26:04 +00:00
taosFillSetStartInfo ( pInfo - > pFillInfo , 0 , pTaskInfo - > window . 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-03-24 03:15:05 +00:00
doFillTimeIntervalGapsInResults ( pInfo - > pFillInfo , pInfo - > pRes , pInfo - > capacity , pInfo - > p ) ;
2021-11-02 05:37:31 +00:00
// current group has no more result to return
if ( pInfo - > pRes - > info . rows > 0 ) {
// 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-03-24 03:15:05 +00:00
if ( pInfo - > pRes - > info . rows > pResultInfo - > threshold | | pBlock = = NULL | | ( ! pInfo - > multigroupResult ) ) {
2021-11-02 05:37:31 +00:00
return pInfo - > pRes ;
}
2022-03-24 09:26:04 +00:00
doHandleRemainBlockFromNewGroup ( pInfo , pResultInfo , newgroup , pTaskInfo ) ;
2022-03-24 03:15:05 +00:00
if ( pInfo - > pRes - > info . rows > pOperator - > resultInfo . threshold | | pBlock = = NULL ) {
2021-11-02 05:37:31 +00:00
return pInfo - > pRes ;
}
} else if ( pInfo - > existNewGroupBlock ) { // try next group
assert ( pBlock ! = NULL ) ;
2022-03-24 09:26:04 +00:00
doHandleRemainBlockForNewGroupImpl ( pInfo , pResultInfo , newgroup , pTaskInfo ) ;
2022-03-24 03:15:05 +00:00
if ( pInfo - > pRes - > info . rows > pResultInfo - > threshold ) {
2021-11-02 05:37:31 +00:00
return pInfo - > pRes ;
}
} else {
return NULL ;
}
}
}
// todo set the attribute of query scan count
2022-01-08 08:28:44 +00:00
static int32_t getNumOfScanTimes ( STaskAttr * pQueryAttr ) {
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < pQueryAttr - > numOfOutput ; + + i ) {
2021-11-02 05:37:31 +00:00
int32_t functionId = getExprFunctionId ( & pQueryAttr - > pExpr1 [ i ] ) ;
if ( functionId = = FUNCTION_STDDEV | | functionId = = FUNCTION_PERCT ) {
return 2 ;
}
}
return 1 ;
}
static void destroyOperatorInfo ( SOperatorInfo * pOperator ) {
if ( pOperator = = NULL ) {
return ;
}
2022-03-02 02:34:29 +00:00
if ( pOperator - > closeFn ! = NULL ) {
pOperator - > closeFn ( pOperator - > info , pOperator - > numOfOutput ) ;
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-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pOperator - > info ) ;
taosMemoryFreeClear ( pOperator ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
int32_t doInitAggInfoSup ( SAggSupporter * pAggSup , SqlFunctionCtx * pCtx , int32_t numOfOutput , const char * pKey ) {
2022-02-22 05:12:03 +00:00
_hash_fn_t hashFn = taosGetDefaultHashFunction ( TSDB_DATA_TYPE_BINARY ) ;
2022-03-31 09:59:41 +00:00
pAggSup - > resultRowSize = getResultRowSize ( pCtx , numOfOutput ) ;
pAggSup - > keyBuf = taosMemoryCalloc ( 1 , sizeof ( int64_t ) + sizeof ( int64_t ) + POINTER_BYTES ) ;
2022-02-22 05:12:03 +00:00
pAggSup - > pResultRowHashTable = taosHashInit ( 10 , hashFn , true , HASH_NO_LOCK ) ;
2022-03-31 09:59:41 +00:00
pAggSup - > pResultRowListSet = taosHashInit ( 100 , hashFn , false , HASH_NO_LOCK ) ;
2022-02-22 05:12:03 +00:00
pAggSup - > pResultRowArrayList = taosArrayInit ( 10 , sizeof ( SResultRowCell ) ) ;
if ( pAggSup - > keyBuf = = NULL | | pAggSup - > pResultRowArrayList = = NULL | | pAggSup - > pResultRowListSet = = NULL | |
2022-03-15 06:37:26 +00:00
pAggSup - > pResultRowHashTable = = NULL ) {
2022-02-22 05:12:03 +00:00
return TSDB_CODE_OUT_OF_MEMORY ;
}
2022-03-15 08:51:50 +00:00
int32_t code = createDiskbasedBuf ( & pAggSup - > pResultBuf , 4096 , 4096 * 256 , pKey , " /tmp/ " ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
return code ;
}
2022-02-22 05:12:03 +00:00
return TSDB_CODE_SUCCESS ;
}
2022-03-14 06:15:26 +00:00
static 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 ) ;
taosHashCleanup ( pAggSup - > pResultRowListSet ) ;
taosArrayDestroy ( pAggSup - > pResultRowArrayList ) ;
2022-03-15 08:51:50 +00:00
destroyDiskbasedBuf ( pAggSup - > pResultBuf ) ;
2022-02-22 05:12:03 +00:00
}
2022-04-04 06:54:39 +00:00
int32_t initAggInfo ( SOptrBasicInfo * pBasicInfo , SAggSupporter * pAggSup , SExprInfo * pExprInfo , int32_t numOfCols ,
2022-03-15 08:51:50 +00:00
int32_t numOfRows , SSDataBlock * pResultBlock , const char * pkey ) {
2022-03-14 08:09:26 +00:00
pBasicInfo - > pCtx = createSqlFunctionCtx_rv ( pExprInfo , numOfCols , & pBasicInfo - > rowCellInfoOffset ) ;
pBasicInfo - > pRes = pResultBlock ;
pBasicInfo - > capacity = numOfRows ;
2022-03-15 08:51:50 +00:00
doInitAggInfoSup ( pAggSup , pBasicInfo - > pCtx , numOfCols , pkey ) ;
2022-03-14 08:09:26 +00:00
}
static STableQueryInfo * initTableQueryInfo ( const STableGroupInfo * pTableGroupInfo ) {
2022-03-25 16:29:53 +00:00
STableQueryInfo * pTableQueryInfo = taosMemoryCalloc ( pTableGroupInfo - > numOfTables , sizeof ( STableQueryInfo ) ) ;
2022-03-14 08:09:26 +00:00
if ( pTableQueryInfo = = NULL ) {
return NULL ;
2022-03-14 06:15:26 +00:00
}
2022-02-14 06:39:42 +00:00
int32_t index = 0 ;
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < taosArrayGetSize ( pTableGroupInfo - > pGroupList ) ; + + i ) {
2022-02-14 06:39:42 +00:00
SArray * pa = taosArrayGetP ( pTableGroupInfo - > pGroupList , i ) ;
2022-03-29 07:24:25 +00:00
for ( int32_t j = 0 ; j < taosArrayGetSize ( pa ) ; + + j ) {
2022-02-14 06:39:42 +00:00
STableKeyInfo * pk = taosArrayGet ( pa , j ) ;
2022-03-14 08:09:26 +00:00
STableQueryInfo * pTQueryInfo = & pTableQueryInfo [ index + + ] ;
2022-03-29 07:24:25 +00:00
pTQueryInfo - > uid = pk - > uid ;
pTQueryInfo - > lastKey = pk - > lastKey ;
2022-02-14 06:39:42 +00:00
pTQueryInfo - > groupIndex = i ;
}
}
2022-02-14 05:20:46 +00:00
STimeWindow win = { 0 , INT64_MAX } ;
2022-03-14 08:09:26 +00:00
createTableQueryInfo ( pTableQueryInfo , false , win ) ;
return pTableQueryInfo ;
2022-02-13 06:34:00 +00:00
}
2022-03-29 07:24:25 +00:00
SOperatorInfo * createAggregateOperatorInfo ( SOperatorInfo * downstream , SExprInfo * pExprInfo , int32_t numOfCols ,
SSDataBlock * pResultBlock , SExecTaskInfo * pTaskInfo ,
const STableGroupInfo * pTableGroupInfo ) {
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
//(int32_t)(getRowNumForMultioutput(pQueryAttr, pQueryAttr->topBotQuery, pQueryAttr->stableQuery));
2022-03-14 06:15:26 +00:00
int32_t numOfRows = 1 ;
2022-03-29 12:05:04 +00:00
int32_t code = initAggInfo ( & pInfo - > binfo , & pInfo - > aggSup , pExprInfo , numOfCols , numOfRows , pResultBlock , pTaskInfo - > id . str ) ;
2022-03-14 08:09:26 +00:00
pInfo - > pTableQueryInfo = initTableQueryInfo ( pTableGroupInfo ) ;
if ( code ! = TSDB_CODE_SUCCESS | | pInfo - > pTableQueryInfo = = NULL ) {
2022-03-14 06:15:26 +00:00
goto _error ;
}
2022-02-13 06:34:00 +00:00
2022-03-09 02:22:53 +00:00
setFunctionResultOutput ( & pInfo - > binfo , & pInfo - > aggSup , MAIN_SCAN , pTaskInfo ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 12:05:04 +00:00
pOperator - > name = " TableAggregate " ;
2022-02-28 09:02:43 +00:00
pOperator - > operatorType = QUERY_NODE_PHYSICAL_PLAN_AGG ;
2021-11-02 05:37:31 +00:00
pOperator - > blockingOptr = true ;
2022-03-29 12:05:04 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > info = pInfo ;
pOperator - > pExpr = pExprInfo ;
pOperator - > numOfOutput = numOfCols ;
pOperator - > pTaskInfo = pTaskInfo ;
pOperator - > _openFn = doOpenAggregateOptr ;
pOperator - > getNextFn = getAggregateResult ;
pOperator - > closeFn = destroyAggOperatorInfo ;
2022-03-24 09:24:01 +00:00
pOperator - > encodeResultRow = aggEncodeResultRow ;
pOperator - > decodeResultRow = aggDecodeResultRow ;
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-04-04 06:54:39 +00:00
void doDestroyBasicInfo ( SOptrBasicInfo * pInfo , int32_t numOfOutput ) {
2021-11-02 05:37:31 +00:00
assert ( pInfo ! = NULL ) ;
2022-03-10 03:12:44 +00:00
destroySqlFunctionCtx ( pInfo - > pCtx , numOfOutput ) ;
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pInfo - > rowCellInfoOffset ) ;
2021-11-02 05:37:31 +00:00
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 ;
2021-11-02 05:37:31 +00:00
doDestroyBasicInfo ( pInfo , numOfOutput ) ;
}
2022-03-14 06:15:26 +00:00
void destroyStateWindowOperatorInfo ( void * param , int32_t numOfOutput ) {
2022-03-29 07:24:25 +00:00
SStateWindowOperatorInfo * pInfo = ( SStateWindowOperatorInfo * ) param ;
2021-11-02 05:37:31 +00:00
doDestroyBasicInfo ( & pInfo - > binfo , numOfOutput ) ;
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pInfo - > prevData ) ;
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 ;
2021-11-02 05:37:31 +00:00
doDestroyBasicInfo ( & pInfo - > binfo , numOfOutput ) ;
}
2022-03-10 03:12:44 +00:00
2022-03-14 06:15:26 +00:00
void destroyIntervalOperatorInfo ( void * param , int32_t numOfOutput ) {
2022-03-29 07:24:25 +00:00
STableIntervalOperatorInfo * pInfo = ( STableIntervalOperatorInfo * ) param ;
2022-03-14 06:15:26 +00:00
doDestroyBasicInfo ( & pInfo - > binfo , numOfOutput ) ;
cleanupAggSup ( & pInfo - > aggSup ) ;
}
void destroySWindowOperatorInfo ( void * param , int32_t numOfOutput ) {
2022-03-29 07:24:25 +00:00
SSessionAggOperatorInfo * pInfo = ( SSessionAggOperatorInfo * ) param ;
2021-11-02 05:37:31 +00:00
doDestroyBasicInfo ( & pInfo - > binfo , numOfOutput ) ;
}
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-03-29 07:24:25 +00:00
SProjectOperatorInfo * pInfo = ( SProjectOperatorInfo * ) param ;
2021-11-02 05:37:31 +00:00
doDestroyBasicInfo ( & pInfo - > binfo , numOfOutput ) ;
}
2022-03-18 03:12:49 +00:00
static void destroyTagScanOperatorInfo ( void * param , int32_t numOfOutput ) {
2022-03-29 07:24:25 +00:00
STagScanInfo * pInfo = ( STagScanInfo * ) param ;
2022-02-08 10:01:21 +00:00
pInfo - > pRes = blockDataDestroy ( pInfo - > pRes ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-18 03:12:49 +00:00
static void destroyOrderOperatorInfo ( void * param , int32_t numOfOutput ) {
2022-03-29 07:24:25 +00:00
SSortOperatorInfo * pInfo = ( SSortOperatorInfo * ) param ;
2022-02-08 10:01:21 +00:00
pInfo - > pDataBlock = blockDataDestroy ( pInfo - > pDataBlock ) ;
2022-03-28 11:08:07 +00:00
taosArrayDestroy ( pInfo - > pSortInfo ) ;
2021-11-02 05:37:31 +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-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-03-29 07:24:25 +00:00
SOperatorInfo * createMultiTableAggOperatorInfo ( SOperatorInfo * downstream , SExprInfo * pExprInfo , int32_t numOfCols ,
SSDataBlock * pResBlock , SExecTaskInfo * pTaskInfo ,
const STableGroupInfo * pTableGroupInfo ) {
2022-03-25 16:29:53 +00:00
SAggOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( SAggOperatorInfo ) ) ;
2021-11-02 05:37:31 +00:00
2022-02-13 06:34:00 +00:00
int32_t numOfRows = 1 ;
2022-03-29 07:24:25 +00:00
int32_t code =
initAggInfo ( & pInfo - > binfo , & pInfo - > aggSup , pExprInfo , numOfCols , numOfRows , pResBlock , pTaskInfo - > id . str ) ;
2022-03-14 08:09:26 +00:00
pInfo - > pTableQueryInfo = initTableQueryInfo ( pTableGroupInfo ) ;
if ( code ! = TSDB_CODE_SUCCESS | | pInfo - > pTableQueryInfo = = NULL ) {
goto _error ;
}
2022-02-14 06:39:42 +00:00
2022-03-29 07:24:25 +00:00
size_t tableGroup = taosArrayGetSize ( pTableGroupInfo - > pGroupList ) ;
2022-02-22 05:12:03 +00:00
initResultRowInfo ( & pInfo - > binfo . resultRowInfo , ( int32_t ) tableGroup ) ;
2021-11-02 05:37:31 +00:00
2022-03-25 16:29:53 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
2022-03-29 07:24:25 +00:00
pOperator - > name = " MultiTableAggregate " ;
2022-02-28 09:02:43 +00:00
// pOperator->operatorType = OP_MultiTableAggregate;
2021-11-02 05:37:31 +00:00
pOperator - > blockingOptr = true ;
2022-03-29 07:24:25 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > info = pInfo ;
pOperator - > pExpr = pExprInfo ;
pOperator - > numOfOutput = numOfCols ;
pOperator - > pTaskInfo = pTaskInfo ;
2021-11-02 05:37:31 +00:00
2022-03-05 07:58:28 +00:00
pOperator - > getNextFn = doMultiTableAggregate ;
2022-03-02 02:34:29 +00:00
pOperator - > closeFn = destroyAggOperatorInfo ;
2022-03-14 08:09: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-14 08:09:26 +00:00
_error :
return NULL ;
2021-11-02 05:37:31 +00:00
}
2022-04-06 03:01:09 +00:00
static SArray * setRowTsColumnOutputInfo ( SqlFunctionCtx * pCtx , int32_t numOfCols ) {
SArray * pList = taosArrayInit ( 4 , sizeof ( int32_t ) ) ;
for ( int32_t i = 0 ; i < numOfCols ; + + i ) {
if ( fmIsPseudoColumnFunc ( pCtx [ i ] . functionId ) ) {
taosArrayPush ( pList , & i ) ;
}
}
return pList ;
}
2022-03-29 07:24:25 +00:00
SOperatorInfo * createProjectOperatorInfo ( SOperatorInfo * downstream , SExprInfo * pExprInfo , int32_t num ,
2022-04-01 09:21:24 +00:00
SSDataBlock * pResBlock , SLimit * pLimit , 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-04-01 09:21:24 +00:00
pInfo - > limit = * pLimit ;
pInfo - > curOffset = pLimit - > offset ;
2022-03-12 14:59:12 +00:00
pInfo - > binfo . pRes = pResBlock ;
2022-04-04 06:54:39 +00:00
int32_t numOfCols = num ;
int32_t numOfRows = 4096 ;
initAggInfo ( & pInfo - > binfo , & pInfo - > aggSup , pExprInfo , numOfCols , numOfRows , pResBlock , pTaskInfo - > id . str ) ;
setFunctionResultOutput ( & pInfo - > binfo , & pInfo - > aggSup , MAIN_SCAN , pTaskInfo ) ;
2022-04-06 03:01:09 +00:00
pInfo - > pPseudoColInfo = setRowTsColumnOutputInfo ( pInfo - > binfo . pCtx , numOfCols ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 12:05:04 +00:00
pOperator - > name = " ProjectOperator " ;
2022-03-12 10:02:56 +00:00
pOperator - > operatorType = QUERY_NODE_PHYSICAL_PLAN_PROJECT ;
2021-11-02 05:37:31 +00:00
pOperator - > blockingOptr = false ;
2022-03-29 12:05:04 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > info = pInfo ;
pOperator - > pExpr = pExprInfo ;
pOperator - > numOfOutput = num ;
pOperator - > _openFn = operatorDummyOpenFn ;
pOperator - > getNextFn = doProjectOperation ;
pOperator - > closeFn = destroyProjectOperatorInfo ;
2022-03-29 07:24:25 +00:00
pOperator - > pTaskInfo = pTaskInfo ;
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-03-29 07:24:25 +00:00
SOperatorInfo * createIntervalOperatorInfo ( SOperatorInfo * downstream , SExprInfo * pExprInfo , int32_t numOfCols ,
2022-04-06 10:26:35 +00:00
SSDataBlock * pResBlock , SInterval * pInterval , int32_t primaryTsSlot ,
2022-03-15 06:37:26 +00:00
const STableGroupInfo * pTableGroupInfo , SExecTaskInfo * pTaskInfo ) {
2022-03-25 16:29:53 +00:00
STableIntervalOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( STableIntervalOperatorInfo ) ) ;
2022-03-29 07:24:25 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
2022-03-14 08:09:26 +00:00
if ( pInfo = = NULL | | pOperator = = NULL ) {
goto _error ;
}
2022-02-24 09:18:56 +00:00
2022-03-29 12:05:04 +00:00
pInfo - > order = TSDB_ORDER_ASC ;
pInfo - > interval = * pInterval ;
2022-03-30 06:54:00 +00:00
pInfo - > execModel = pTaskInfo - > execModel ;
pInfo - > win = pTaskInfo - > window ;
2022-03-30 05:41:15 +00:00
pInfo - > win . skey = 0 ;
pInfo - > win . ekey = INT64_MAX ;
2022-03-15 06:37:26 +00:00
2022-04-06 10:26:35 +00:00
pInfo - > primaryTsIndex = primaryTsSlot ;
2022-03-15 06:37:26 +00:00
int32_t numOfRows = 4096 ;
2022-03-29 12:05:04 +00:00
int32_t code = initAggInfo ( & pInfo - > binfo , & pInfo - > aggSup , pExprInfo , numOfCols , numOfRows , pResBlock , pTaskInfo - > id . str ) ;
2022-03-30 05:41:15 +00:00
initExecTimeWindowInfo ( & pInfo - > timeWindowData , & pInfo - > win ) ;
2022-03-29 07:24:25 +00:00
// pInfo->pTableQueryInfo = initTableQueryInfo(pTableGroupInfo);
if ( code ! = TSDB_CODE_SUCCESS /* || pInfo->pTableQueryInfo == NULL*/ ) {
2022-03-14 08:09:26 +00:00
goto _error ;
}
2022-02-24 09:18:56 +00:00
initResultRowInfo ( & pInfo - > binfo . resultRowInfo , ( int32_t ) 1 ) ;
2021-11-02 05:37:31 +00:00
2022-03-30 05:41:15 +00:00
pOperator - > name = " TimeIntervalAggOperator " ;
2022-03-14 08:09:26 +00:00
pOperator - > operatorType = QUERY_NODE_PHYSICAL_PLAN_INTERVAL ;
2021-11-02 05:37:31 +00:00
pOperator - > blockingOptr = true ;
2022-03-29 12:05:04 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > pExpr = pExprInfo ;
pOperator - > pTaskInfo = pTaskInfo ;
pOperator - > numOfOutput = numOfCols ;
pOperator - > info = pInfo ;
pOperator - > _openFn = doOpenIntervalAgg ;
pOperator - > getNextFn = doBuildIntervalResult ;
2022-03-30 06:54:00 +00:00
pOperator - > getStreamResFn = doStreamIntervalAgg ;
2022-03-29 12:05:04 +00:00
pOperator - > closeFn = destroyIntervalOperatorInfo ;
2021-11-02 05:37:31 +00:00
2022-02-24 09:18:56 +00:00
code = appendDownstream ( pOperator , & downstream , 1 ) ;
2022-03-14 08:09:26 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
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
destroyIntervalOperatorInfo ( pInfo , numOfCols ) ;
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pInfo ) ;
taosMemoryFreeClear ( pOperator ) ;
2022-03-14 06:15:26 +00:00
pTaskInfo - > code = code ;
return NULL ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
SOperatorInfo * createAllTimeIntervalOperatorInfo ( STaskRuntimeEnv * pRuntimeEnv , SOperatorInfo * downstream ,
SExprInfo * pExpr , int32_t numOfOutput ) {
2022-03-25 16:29:53 +00:00
STableIntervalOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( STableIntervalOperatorInfo ) ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// pInfo->binfo.pCtx = createSqlFunctionCtx(pRuntimeEnv, pExpr, numOfOutput, &pInfo->binfo.rowCellInfoOffset);
// pInfo->binfo.pRes = createOutputBuf(pExpr, numOfOutput, pResultInfo->capacity);
2022-02-24 09:18:56 +00:00
initResultRowInfo ( & pInfo - > binfo . resultRowInfo , 8 ) ;
2021-11-02 05:37:31 +00:00
2022-03-25 16:29:53 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
pOperator - > name = " AllTimeIntervalAggOperator " ;
// pOperator->operatorType = OP_AllTimeWindow;
2021-11-02 05:37:31 +00:00
pOperator - > blockingOptr = true ;
2022-03-29 07:24:25 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > pExpr = pExpr ;
pOperator - > numOfOutput = numOfOutput ;
pOperator - > info = pInfo ;
pOperator - > pRuntimeEnv = pRuntimeEnv ;
2022-03-05 07:58:28 +00:00
pOperator - > getNextFn = doAllIntervalAgg ;
2022-03-02 02:34:29 +00:00
pOperator - > closeFn = destroyBasicOperatorInfo ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
int32_t code = appendDownstream ( pOperator , & downstream , 1 ) ;
2021-11-02 05:37:31 +00:00
return pOperator ;
}
2022-04-01 10:08:36 +00:00
SOperatorInfo * createStatewindowOperatorInfo ( SOperatorInfo * downstream , SExprInfo * pExpr , int32_t numOfCols , SSDataBlock * pResBlock , SExecTaskInfo * pTaskInfo ) {
2022-03-25 16:29:53 +00:00
SStateWindowOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( SStateWindowOperatorInfo ) ) ;
2022-04-01 10:08:36 +00:00
2022-03-29 07:24:25 +00:00
pInfo - > colIndex = - 1 ;
pInfo - > reptScan = false ;
// pInfo->binfo.pCtx = createSqlFunctionCtx(pRuntimeEnv, pExpr, numOfOutput, &pInfo->binfo.rowCellInfoOffset);
// pInfo->binfo.pRes = createOutputBuf(pExpr, numOfOutput, pResultInfo->capacity);
2022-02-22 05:12:03 +00:00
initResultRowInfo ( & pInfo - > binfo . resultRowInfo , 8 ) ;
2021-11-02 05:37:31 +00:00
2022-03-25 16:29:53 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
2022-03-29 07:24:25 +00:00
pOperator - > name = " StateWindowOperator " ;
// pOperator->operatorType = OP_StateWindow;
2021-11-02 05:37:31 +00:00
pOperator - > blockingOptr = true ;
2022-04-01 10:08:36 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > pExpr = pExpr ;
pOperator - > numOfOutput = numOfCols ;
pOperator - > pTaskInfo = pTaskInfo ;
pOperator - > info = pInfo ;
pOperator - > getNextFn = doStateWindowAgg ;
pOperator - > closeFn = destroyStateWindowOperatorInfo ;
2021-11-02 05:37:31 +00:00
2022-03-12 15:40:04 +00:00
int32_t code = appendDownstream ( pOperator , & downstream , 1 ) ;
2021-11-02 05:37:31 +00:00
return pOperator ;
}
2022-03-29 07:24:25 +00:00
SOperatorInfo * createSessionAggOperatorInfo ( SOperatorInfo * downstream , SExprInfo * pExprInfo , int32_t numOfCols ,
SSDataBlock * pResBlock , int64_t gap , SExecTaskInfo * pTaskInfo ) {
2022-03-25 16:29:53 +00:00
SSessionAggOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( SSessionAggOperatorInfo ) ) ;
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-03-12 15:40:04 +00:00
2022-03-28 12:11:02 +00:00
int32_t numOfRows = 4096 ;
2022-04-02 05:32:26 +00:00
int32_t code = initAggInfo ( & pInfo - > binfo , & pInfo - > aggSup , pExprInfo , numOfCols , numOfRows , pResBlock , pTaskInfo - > id . str ) ;
2022-03-14 06:15:26 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
}
2021-11-02 05:37:31 +00:00
2022-03-14 06:15:26 +00:00
initResultRowInfo ( & pInfo - > binfo . resultRowInfo , 8 ) ;
2022-04-02 05:32:26 +00:00
initExecTimeWindowInfo ( & pInfo - > timeWindowData , & pTaskInfo - > window ) ;
2021-11-02 05:37:31 +00:00
2022-04-02 05:32:26 +00:00
pInfo - > gap = gap ;
pInfo - > binfo . pRes = pResBlock ;
pInfo - > prevTs = INT64_MIN ;
pInfo - > reptScan = false ;
pOperator - > name = " SessionWindowAggOperator " ;
2022-03-28 12:11:02 +00:00
pOperator - > operatorType = QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW ;
2021-11-02 05:37:31 +00:00
pOperator - > blockingOptr = true ;
2022-04-02 05:32:26 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > pExpr = pExprInfo ;
pOperator - > numOfOutput = numOfCols ;
pOperator - > info = pInfo ;
pOperator - > getNextFn = doSessionWindowAgg ;
pOperator - > closeFn = destroySWindowOperatorInfo ;
pOperator - > pTaskInfo = pTaskInfo ;
2021-11-02 05:37:31 +00:00
2022-03-14 06:15:26 +00:00
code = appendDownstream ( pOperator , & downstream , 1 ) ;
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
if ( pInfo ! = NULL ) {
destroySWindowOperatorInfo ( pInfo , numOfCols ) ;
}
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear ( pInfo ) ;
taosMemoryFreeClear ( pOperator ) ;
2022-03-14 06:15:26 +00:00
pTaskInfo - > code = code ;
return NULL ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
SOperatorInfo * createMultiTableTimeIntervalOperatorInfo ( STaskRuntimeEnv * pRuntimeEnv , SOperatorInfo * downstream ,
SExprInfo * pExpr , int32_t numOfOutput ) {
2022-03-25 16:29:53 +00:00
STableIntervalOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( STableIntervalOperatorInfo ) ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// pInfo->binfo.pCtx = createSqlFunctionCtx(pRuntimeEnv, pExpr, numOfOutput, &pInfo->binfo.rowCellInfoOffset);
// pInfo->binfo.pRes = createOutputBuf(pExpr, numOfOutput, pResultInfo->capacity);
2022-02-24 09:18:56 +00:00
initResultRowInfo ( & pInfo - > binfo . resultRowInfo , 8 ) ;
2021-11-02 05:37:31 +00:00
2022-03-25 16:29:53 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
2022-03-29 07:24:25 +00:00
pOperator - > name = " MultiTableTimeIntervalOperator " ;
// pOperator->operatorType = OP_MultiTableTimeInterval;
2021-11-02 05:37:31 +00:00
pOperator - > blockingOptr = true ;
2022-03-29 07:24:25 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > pExpr = pExpr ;
pOperator - > numOfOutput = numOfOutput ;
pOperator - > info = pInfo ;
pOperator - > pRuntimeEnv = pRuntimeEnv ;
2021-11-02 05:37:31 +00:00
2022-03-05 07:58:28 +00:00
pOperator - > getNextFn = doSTableIntervalAgg ;
2022-03-02 02:34:29 +00:00
pOperator - > closeFn = destroyBasicOperatorInfo ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
int32_t code = appendDownstream ( pOperator , & downstream , 1 ) ;
2021-11-02 05:37:31 +00:00
return pOperator ;
}
2022-03-29 07:24:25 +00:00
SOperatorInfo * createAllMultiTableTimeIntervalOperatorInfo ( STaskRuntimeEnv * pRuntimeEnv , SOperatorInfo * downstream ,
SExprInfo * pExpr , int32_t numOfOutput ) {
2022-03-25 16:29:53 +00:00
STableIntervalOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( STableIntervalOperatorInfo ) ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
// pInfo->binfo.pCtx = createSqlFunctionCtx(pRuntimeEnv, pExpr, numOfOutput, &pInfo->binfo.rowCellInfoOffset);
// pInfo->binfo.pRes = createOutputBuf(pExpr, numOfOutput, pResultInfo->capacity);
2022-02-24 09:18:56 +00:00
initResultRowInfo ( & pInfo - > binfo . resultRowInfo , 8 ) ;
2021-11-02 05:37:31 +00:00
2022-03-25 16:29:53 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
2022-03-29 07:24:25 +00:00
pOperator - > name = " AllMultiTableTimeIntervalOperator " ;
// pOperator->operatorType = OP_AllMultiTableTimeInterval;
2021-11-02 05:37:31 +00:00
pOperator - > blockingOptr = true ;
2022-03-29 07:24:25 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > pExpr = pExpr ;
pOperator - > numOfOutput = numOfOutput ;
pOperator - > info = pInfo ;
pOperator - > pRuntimeEnv = pRuntimeEnv ;
2021-11-02 05:37:31 +00:00
2022-03-05 07:58:28 +00:00
pOperator - > getNextFn = doAllSTableIntervalAgg ;
2022-03-02 02:34:29 +00:00
pOperator - > closeFn = destroyBasicOperatorInfo ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
int32_t code = appendDownstream ( pOperator , & downstream , 1 ) ;
2021-11-02 05:37:31 +00:00
return pOperator ;
}
2022-03-24 06:21:14 +00:00
static int32_t initFillInfo ( SFillOperatorInfo * pInfo , SExprInfo * pExpr , int32_t numOfCols , int64_t * fillVal ,
2022-03-29 07:24:25 +00:00
STimeWindow win , int32_t capacity , const char * id , SInterval * pInterval , int32_t fillType ) {
2022-03-24 06:21:14 +00:00
struct SFillColInfo * pColInfo = createFillColInfo ( pExpr , numOfCols , ( int64_t * ) fillVal ) ;
TSKEY sk = TMIN ( win . skey , win . ekey ) ;
TSKEY ek = TMAX ( win . skey , win . ekey ) ;
// TODO set correct time precision
STimeWindow w = TSWINDOW_INITIALIZER ;
getAlignQueryTimeWindow ( pInterval , TSDB_TIME_PRECISION_MILLI , win . skey , sk , ek , & w ) ;
int32_t order = TSDB_ORDER_ASC ;
2022-03-29 07:24:25 +00:00
pInfo - > pFillInfo = taosCreateFillInfo ( order , w . skey , 0 , capacity , numOfCols , pInterval - > sliding ,
pInterval - > slidingUnit , ( int8_t ) pInterval - > precision , fillType , pColInfo , id ) ;
2022-03-24 06:21:14 +00:00
2022-03-25 16:29:53 +00:00
pInfo - > p = taosMemoryCalloc ( numOfCols , POINTER_BYTES ) ;
2022-03-24 06:21:14 +00:00
if ( pInfo - > pFillInfo = = NULL | | pInfo - > p = = NULL ) {
return TSDB_CODE_OUT_OF_MEMORY ;
} else {
return TSDB_CODE_SUCCESS ;
}
}
2022-03-29 07:24:25 +00:00
SOperatorInfo * createFillOperatorInfo ( SOperatorInfo * downstream , SExprInfo * pExpr , int32_t numOfCols ,
SInterval * pInterval , SSDataBlock * pResBlock , int32_t fillType , char * fillVal ,
bool multigroupResult , SExecTaskInfo * pTaskInfo ) {
2022-03-25 16:29:53 +00:00
SFillOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( SFillOperatorInfo ) ) ;
2022-03-29 07:24:25 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
2022-03-24 03:15:05 +00:00
2022-03-29 07:24:25 +00:00
pInfo - > pRes = pResBlock ;
2021-11-02 05:37:31 +00:00
pInfo - > multigroupResult = multigroupResult ;
2022-03-29 07:24:25 +00:00
pInfo - > intervalInfo = * pInterval ;
2021-11-02 05:37:31 +00:00
2022-03-24 03:15:05 +00:00
SResultInfo * pResultInfo = & pOperator - > resultInfo ;
2022-03-29 07:24:25 +00:00
int32_t code = initFillInfo ( pInfo , pExpr , numOfCols , ( int64_t * ) fillVal , pTaskInfo - > window , pResultInfo - > capacity ,
pTaskInfo - > id . str , pInterval , fillType ) ;
2022-03-24 09:26:04 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
}
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
pOperator - > name = " FillOperator " ;
2021-11-02 05:37:31 +00:00
pOperator - > blockingOptr = false ;
2022-03-29 07:24:25 +00:00
pOperator - > status = OP_NOT_OPENED ;
// pOperator->operatorType = OP_Fill;
pOperator - > pExpr = pExpr ;
pOperator - > numOfOutput = numOfCols ;
pOperator - > info = pInfo ;
pOperator - > _openFn = operatorDummyOpenFn ;
pOperator - > getNextFn = doFill ;
pOperator - > pTaskInfo = pTaskInfo ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
pOperator - > closeFn = destroySFillOperatorInfo ;
2022-03-24 03:15:05 +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-03-29 07:24:25 +00:00
SOperatorInfo * createSLimitOperatorInfo ( STaskRuntimeEnv * pRuntimeEnv , SOperatorInfo * downstream , SExprInfo * pExpr ,
int32_t numOfOutput , void * pMerger , bool multigroupResult ) {
2022-03-25 16:29:53 +00:00
SSLimitOperatorInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( SSLimitOperatorInfo ) ) ;
2022-03-29 07:24:25 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
// pInfo->orderColumnList = getResultGroupCheckColumns(pQueryAttr);
// pInfo->slimit = pQueryAttr->slimit;
// pInfo->limit = pQueryAttr->limit;
// pInfo->capacity = pResultInfo->capacity;
// pInfo->threshold = (int64_t)(pInfo->capacity * 0.8);
// pInfo->currentOffset = pQueryAttr->limit.offset;
// pInfo->currentGroupOffset = pQueryAttr->slimit.offset;
pInfo - > multigroupResult = multigroupResult ;
2021-11-02 05:37:31 +00:00
// TODO refactor
int32_t len = 0 ;
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < numOfOutput ; + + i ) {
2021-11-02 05:37:31 +00:00
len + = pExpr [ i ] . base . resSchema . bytes ;
}
2022-03-29 07:24:25 +00:00
int32_t numOfCols = ( pInfo - > orderColumnList ! = NULL ) ? ( int32_t ) taosArrayGetSize ( pInfo - > orderColumnList ) : 0 ;
2022-03-25 16:29:53 +00:00
pInfo - > prevRow = taosMemoryCalloc ( 1 , ( POINTER_BYTES * numOfCols + len ) ) ;
2021-11-02 05:37:31 +00:00
int32_t offset = POINTER_BYTES * numOfCols ;
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < numOfCols ; + + i ) {
2021-11-02 05:37:31 +00:00
pInfo - > prevRow [ i ] = ( char * ) pInfo - > prevRow + offset ;
SColIndex * index = taosArrayGet ( pInfo - > orderColumnList , i ) ;
offset + = pExpr [ index - > colIndex ] . base . resSchema . bytes ;
}
2022-03-29 07:24:25 +00:00
// pInfo->pRes = createOutputBuf(pExpr, numOfOutput, pOperator->resultInfo.capacity);
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
pOperator - > name = " SLimitOperator " ;
2022-02-28 09:02:43 +00:00
// pOperator->operatorType = OP_SLimit;
2021-11-02 05:37:31 +00:00
pOperator - > blockingOptr = false ;
2022-03-29 07:24:25 +00:00
pOperator - > status = OP_NOT_OPENED ;
// pOperator->exec = doSLimit;
pOperator - > info = pInfo ;
pOperator - > pRuntimeEnv = pRuntimeEnv ;
2022-03-02 02:34:29 +00:00
pOperator - > closeFn = destroySlimitOperatorInfo ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
int32_t code = appendDownstream ( pOperator , & downstream , 1 ) ;
2021-11-02 05:37:31 +00:00
return pOperator ;
}
2022-03-29 07:24:25 +00:00
static SSDataBlock * doTagScan ( SOperatorInfo * pOperator , bool * newgroup ) {
2021-11-02 05:37:31 +00:00
#if 0
SOperatorInfo * pOperator = ( SOperatorInfo * ) param ;
if ( pOperator - > status = = OP_EXEC_DONE ) {
return NULL ;
}
2022-01-08 08:28:44 +00:00
STaskRuntimeEnv * pRuntimeEnv = pOperator - > pRuntimeEnv ;
2022-03-24 03:15:05 +00:00
int32_t maxNumOfTables = ( int32_t ) pResultInfo - > capacity ;
2021-11-02 05:37:31 +00:00
STagScanInfo * pInfo = pOperator - > info ;
SSDataBlock * pRes = pInfo - > pRes ;
* newgroup = false ;
int32_t count = 0 ;
SArray * pa = GET_TABLEGROUP ( pRuntimeEnv , 0 ) ;
int32_t functionId = getExprFunctionId ( & pOperator - > pExpr [ 0 ] ) ;
if ( functionId = = FUNCTION_TID_TAG ) { // return the tags & table Id
assert ( pQueryAttr - > numOfOutput = = 1 ) ;
SExprInfo * pExprInfo = & pOperator - > pExpr [ 0 ] ;
int32_t rsize = pExprInfo - > base . resSchema . bytes ;
count = 0 ;
int16_t bytes = pExprInfo - > base . resSchema . bytes ;
int16_t type = pExprInfo - > base . resSchema . type ;
for ( int32_t i = 0 ; i < pQueryAttr - > numOfTags ; + + i ) {
2021-11-05 02:35:50 +00:00
if ( pQueryAttr - > tagColList [ i ] . colId = = pExprInfo - > base . pColumns - > info . colId ) {
2021-11-02 05:37:31 +00:00
bytes = pQueryAttr - > tagColList [ i ] . bytes ;
type = pQueryAttr - > tagColList [ i ] . type ;
break ;
}
}
SColumnInfoData * pColInfo = taosArrayGet ( pRes - > pDataBlock , 0 ) ;
while ( pInfo - > curPos < pInfo - > totalTables & & count < maxNumOfTables ) {
int32_t i = pInfo - > curPos + + ;
STableQueryInfo * item = taosArrayGetP ( pa , i ) ;
char * output = pColInfo - > pData + count * rsize ;
varDataSetLen ( output , rsize - VARSTR_HEADER_SIZE ) ;
output = varDataVal ( output ) ;
STableId * id = TSDB_TABLEID ( item - > pTable ) ;
* ( int16_t * ) output = 0 ;
output + = sizeof ( int16_t ) ;
* ( int64_t * ) output = id - > uid ; // memory align problem, todo serialize
output + = sizeof ( id - > uid ) ;
* ( int32_t * ) output = id - > tid ;
output + = sizeof ( id - > tid ) ;
* ( int32_t * ) output = pQueryAttr - > vgId ;
output + = sizeof ( pQueryAttr - > vgId ) ;
char * data = NULL ;
2021-11-05 02:35:50 +00:00
if ( pExprInfo - > base . pColumns - > info . colId = = TSDB_TBNAME_COLUMN_INDEX ) {
2021-11-02 05:37:31 +00:00
data = tsdbGetTableName ( item - > pTable ) ;
} else {
2021-11-05 02:35:50 +00:00
data = tsdbGetTableTagVal ( item - > pTable , pExprInfo - > base . pColumns - > info . colId , type , bytes ) ;
2021-11-02 05:37:31 +00:00
}
doSetTagValueToResultBuf ( output , data , type , bytes ) ;
count + = 1 ;
}
2022-01-10 11:48:21 +00:00
//qDebug("QInfo:0x%"PRIx64" create (tableId, tag) info completed, rows:%d", GET_TASKID(pRuntimeEnv), count);
2021-11-02 05:37:31 +00:00
} else if ( functionId = = FUNCTION_COUNT ) { // handle the "count(tbname)" query
SColumnInfoData * pColInfo = taosArrayGet ( pRes - > pDataBlock , 0 ) ;
* ( int64_t * ) pColInfo - > pData = pInfo - > totalTables ;
count = 1 ;
pOperator - > status = OP_EXEC_DONE ;
2022-01-10 11:48:21 +00:00
//qDebug("QInfo:0x%"PRIx64" create count(tbname) query, res:%d rows:1", GET_TASKID(pRuntimeEnv), count);
2021-11-02 05:37:31 +00:00
} else { // return only the tags|table name etc.
SExprInfo * pExprInfo = & pOperator - > pExpr [ 0 ] ; // todo use the column list instead of exprinfo
count = 0 ;
while ( pInfo - > curPos < pInfo - > totalTables & & count < maxNumOfTables ) {
int32_t i = pInfo - > curPos + + ;
STableQueryInfo * item = taosArrayGetP ( pa , i ) ;
char * data = NULL , * dst = NULL ;
int16_t type = 0 , bytes = 0 ;
for ( int32_t j = 0 ; j < pOperator - > numOfOutput ; + + j ) {
// not assign value in case of user defined constant output column
2021-11-04 05:24:19 +00:00
if ( TSDB_COL_IS_UD_COL ( pExprInfo [ j ] . base . pColumns - > flag ) ) {
2021-11-02 05:37:31 +00:00
continue ;
}
SColumnInfoData * pColInfo = taosArrayGet ( pRes - > pDataBlock , j ) ;
type = pExprInfo [ j ] . base . resSchema . type ;
bytes = pExprInfo [ j ] . base . resSchema . bytes ;
2021-11-05 02:35:50 +00:00
if ( pExprInfo [ j ] . base . pColumns - > info . colId = = TSDB_TBNAME_COLUMN_INDEX ) {
2021-11-02 05:37:31 +00:00
data = tsdbGetTableName ( item - > pTable ) ;
} else {
2021-11-05 02:35:50 +00:00
data = tsdbGetTableTagVal ( item - > pTable , pExprInfo [ j ] . base . pColumns - > info . colId , type , bytes ) ;
2021-11-02 05:37:31 +00:00
}
dst = pColInfo - > pData + count * pExprInfo [ j ] . base . resSchema . bytes ;
doSetTagValueToResultBuf ( dst , data , type , bytes ) ;
}
count + = 1 ;
}
if ( pInfo - > curPos > = pInfo - > totalTables ) {
pOperator - > status = OP_EXEC_DONE ;
}
2022-01-10 11:48:21 +00:00
//qDebug("QInfo:0x%"PRIx64" create tag values results completed, rows:%d", GET_TASKID(pRuntimeEnv), count);
2021-11-02 05:37:31 +00:00
}
if ( pOperator - > status = = OP_EXEC_DONE ) {
2022-01-10 11:48:21 +00:00
setTaskStatus ( pOperator - > pRuntimeEnv , TASK_COMPLETED ) ;
2021-11-02 05:37:31 +00:00
}
pRes - > info . rows = count ;
return ( pRes - > info . rows = = 0 ) ? NULL : pInfo - > pRes ;
# endif
}
2022-01-08 08:28:44 +00:00
SOperatorInfo * createTagScanOperatorInfo ( STaskRuntimeEnv * pRuntimeEnv , SExprInfo * pExpr , int32_t numOfOutput ) {
2022-03-25 16:29:53 +00:00
STagScanInfo * pInfo = taosMemoryCalloc ( 1 , sizeof ( STagScanInfo ) ) ;
2022-03-29 07:24:25 +00:00
// pInfo->pRes = createOutputBuf(pExpr, numOfOutput, pResultInfo->capacity);
2021-11-02 05:37:31 +00:00
size_t numOfGroup = GET_NUM_OF_TABLEGROUP ( pRuntimeEnv ) ;
assert ( numOfGroup = = 0 | | numOfGroup = = 1 ) ;
pInfo - > totalTables = pRuntimeEnv - > tableqinfoGroupInfo . numOfTables ;
pInfo - > curPos = 0 ;
2022-03-25 16:29:53 +00:00
SOperatorInfo * pOperator = taosMemoryCalloc ( 1 , sizeof ( SOperatorInfo ) ) ;
2022-03-29 07:24:25 +00:00
pOperator - > name = " SeqTableTagScan " ;
2022-02-28 09:02:43 +00:00
pOperator - > operatorType = QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN ;
2021-11-02 05:37:31 +00:00
pOperator - > blockingOptr = false ;
2022-03-29 07:24:25 +00:00
pOperator - > status = OP_NOT_OPENED ;
pOperator - > info = pInfo ;
2022-03-05 07:58:28 +00:00
pOperator - > getNextFn = doTagScan ;
2022-03-29 07:24:25 +00:00
pOperator - > pExpr = pExpr ;
pOperator - > numOfOutput = numOfOutput ;
pOperator - > pRuntimeEnv = pRuntimeEnv ;
2022-03-02 02:34:29 +00:00
pOperator - > closeFn = destroyTagScanOperatorInfo ;
2021-11-02 05:37:31 +00:00
return pOperator ;
}
2022-04-04 06:54:39 +00:00
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
static int32_t getColumnIndexInSource ( SQueriedTableInfo * pTableInfo , SExprBasicInfo * pExpr , SColumnInfo * pTagCols ) {
2021-11-02 05:37:31 +00:00
int32_t j = 0 ;
2022-03-09 02:22:53 +00:00
if ( TSDB_COL_IS_TAG ( pExpr - > pParam [ 0 ] . pCol - > type ) ) {
if ( pExpr - > pParam [ 0 ] . pCol - > colId = = TSDB_TBNAME_COLUMN_INDEX ) {
2021-11-02 05:37:31 +00:00
return TSDB_TBNAME_COLUMN_INDEX ;
}
2022-03-29 07:24:25 +00:00
while ( j < pTableInfo - > numOfTags ) {
2022-03-09 02:22:53 +00:00
if ( pExpr - > pParam [ 0 ] . pCol - > colId = = pTagCols [ j ] . colId ) {
2021-11-02 05:37:31 +00:00
return j ;
}
j + = 1 ;
}
2021-11-05 02:35:50 +00:00
} /*else if (TSDB_COL_IS_UD_COL(pExpr->colInfo.flag)) { // user specified column data
2021-11-02 05:37:31 +00:00
return TSDB_UD_COLUMN_INDEX ;
} else {
while ( j < pTableInfo - > numOfCols ) {
if ( pExpr - > colInfo . colId = = pTableInfo - > colList [ j ] . colId ) {
return j ;
}
j + = 1 ;
}
2021-11-05 02:35:50 +00:00
} */
2021-11-02 05:37:31 +00:00
return INT32_MIN ; // return a less than TSDB_TBNAME_COLUMN_INDEX value
}
2022-03-29 07:24:25 +00:00
bool validateExprColumnInfo ( SQueriedTableInfo * pTableInfo , SExprBasicInfo * pExpr , SColumnInfo * pTagCols ) {
2021-11-02 05:37:31 +00:00
int32_t j = getColumnIndexInSource ( pTableInfo , pExpr , pTagCols ) ;
return j ! = INT32_MIN ;
}
static int32_t deserializeColFilterInfo ( SColumnFilterInfo * pColFilters , int16_t numOfFilters , char * * pMsg ) {
for ( int32_t f = 0 ; f < numOfFilters ; + + f ) {
2022-03-29 07:24:25 +00:00
SColumnFilterInfo * pFilterMsg = ( SColumnFilterInfo * ) ( * pMsg ) ;
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
SColumnFilterInfo * pColFilter = & pColFilters [ f ] ;
2021-11-02 05:37:31 +00:00
pColFilter - > filterstr = htons ( pFilterMsg - > filterstr ) ;
( * pMsg ) + = sizeof ( SColumnFilterInfo ) ;
if ( pColFilter - > filterstr ) {
pColFilter - > len = htobe64 ( pFilterMsg - > len ) ;
2022-03-29 07:24:25 +00:00
pColFilter - > pz =
( int64_t ) taosMemoryCalloc ( 1 , ( size_t ) ( pColFilter - > len + 1 * TSDB_NCHAR_SIZE ) ) ; // note: null-terminator
2021-11-02 05:37:31 +00:00
if ( pColFilter - > pz = = 0 ) {
return TSDB_CODE_QRY_OUT_OF_MEMORY ;
}
2022-03-29 07:24:25 +00:00
memcpy ( ( void * ) pColFilter - > pz , ( * pMsg ) , ( size_t ) pColFilter - > len ) ;
2021-11-02 05:37:31 +00:00
( * pMsg ) + = ( pColFilter - > len + 1 ) ;
} else {
pColFilter - > lowerBndi = htobe64 ( pFilterMsg - > lowerBndi ) ;
pColFilter - > upperBndi = htobe64 ( pFilterMsg - > upperBndi ) ;
}
pColFilter - > lowerRelOptr = htons ( pFilterMsg - > lowerRelOptr ) ;
pColFilter - > upperRelOptr = htons ( pFilterMsg - > upperRelOptr ) ;
}
return TSDB_CODE_SUCCESS ;
}
2022-03-29 07:24:25 +00:00
static SResSchema createResSchema ( int32_t type , int32_t bytes , int32_t slotId , int32_t scale , int32_t precision ,
const char * name ) {
2022-03-10 08:18:35 +00:00
SResSchema s = { 0 } ;
s . scale = scale ;
2022-03-29 07:24:25 +00:00
s . type = type ;
2022-03-10 08:18:35 +00:00
s . bytes = bytes ;
s . colId = slotId ;
2022-03-11 15:28:43 +00:00
s . precision = precision ;
2022-03-10 08:18:35 +00:00
strncpy ( s . name , name , tListLen ( s . name ) ) ;
return s ;
}
2022-03-09 02:22:53 +00:00
2022-03-29 12:05:04 +00:00
static SColumn * createColumn ( int32_t blockId , int32_t slotId , SDataType * pType ) {
SColumn * pCol = taosMemoryCalloc ( 1 , sizeof ( SColumn ) ) ;
if ( pCol = = NULL ) {
terrno = TSDB_CODE_OUT_OF_MEMORY ;
return NULL ;
}
pCol - > slotId = slotId ;
pCol - > bytes = pType - > bytes ;
pCol - > type = pType - > type ;
pCol - > scale = pType - > scale ;
pCol - > precision = pType - > precision ;
pCol - > dataBlockId = blockId ;
return pCol ;
}
2022-03-22 08:03:42 +00:00
SExprInfo * createExprInfo ( SNodeList * pNodeList , SNodeList * pGroupKeys , int32_t * numOfExprs ) {
2022-03-12 14:59:12 +00:00
int32_t numOfFuncs = LIST_LENGTH ( pNodeList ) ;
2022-03-22 08:03:42 +00:00
int32_t numOfGroupKeys = 0 ;
if ( pGroupKeys ! = NULL ) {
numOfGroupKeys = LIST_LENGTH ( pGroupKeys ) ;
}
2022-03-09 02:22:53 +00:00
2022-03-22 08:03:42 +00:00
* numOfExprs = numOfFuncs + numOfGroupKeys ;
2022-03-25 16:29:53 +00:00
SExprInfo * pExprs = taosMemoryCalloc ( * numOfExprs , sizeof ( SExprInfo ) ) ;
2022-03-12 14:59:12 +00:00
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < ( * numOfExprs ) ; + + i ) {
2022-03-22 08:03:42 +00:00
STargetNode * pTargetNode = NULL ;
if ( i < numOfFuncs ) {
pTargetNode = ( STargetNode * ) nodesListGetNode ( pNodeList , i ) ;
} else {
pTargetNode = ( STargetNode * ) nodesListGetNode ( pGroupKeys , i - numOfFuncs ) ;
}
2022-03-09 02:22:53 +00:00
2022-03-22 08:03:42 +00:00
SExprInfo * pExp = & pExprs [ pTargetNode - > slotId ] ;
2022-03-09 02:22:53 +00:00
2022-03-25 16:29:53 +00:00
pExp - > pExpr = taosMemoryCalloc ( 1 , sizeof ( tExprNode ) ) ;
2022-03-09 02:22:53 +00:00
pExp - > pExpr - > _function . num = 1 ;
2022-03-22 08:03:42 +00:00
pExp - > pExpr - > _function . functionId = - 1 ;
2022-03-09 02:22:53 +00:00
2022-03-22 08:03:42 +00:00
// it is a project query, or group by column
2022-03-18 10:02:00 +00:00
if ( nodeType ( pTargetNode - > pExpr ) = = QUERY_NODE_COLUMN ) {
2022-03-26 14:17:26 +00:00
pExp - > pExpr - > nodeType = QUERY_NODE_COLUMN ;
2022-03-29 07:24:25 +00:00
SColumnNode * pColNode = ( SColumnNode * ) pTargetNode - > pExpr ;
2022-03-18 10:02:00 +00:00
2022-03-29 09:30:44 +00:00
pExp - > base . pParam = taosMemoryCalloc ( 1 , sizeof ( SFunctParam ) ) ;
pExp - > base . numOfParams = 1 ;
2022-03-18 10:02:00 +00:00
SDataType * pType = & pColNode - > node . resType ;
pExp - > base . resSchema = createResSchema ( pType - > type , pType - > bytes , pTargetNode - > slotId , pType - > scale , pType - > precision , pColNode - > colName ) ;
2022-03-29 12:05:04 +00:00
pExp - > base . pParam [ 0 ] . pCol = createColumn ( pColNode - > dataBlockId , pColNode - > slotId , pType ) ;
pExp - > base . pParam [ 0 ] . type = FUNC_PARAM_TYPE_COLUMN ;
2022-03-26 14:13:14 +00:00
} else if ( nodeType ( pTargetNode - > pExpr ) = = QUERY_NODE_FUNCTION ) {
2022-03-26 14:17:26 +00:00
pExp - > pExpr - > nodeType = QUERY_NODE_FUNCTION ;
2022-03-18 10:02:00 +00:00
SFunctionNode * pFuncNode = ( SFunctionNode * ) pTargetNode - > pExpr ;
SDataType * pType = & pFuncNode - > node . resType ;
2022-03-29 07:24:25 +00:00
pExp - > base . resSchema = createResSchema ( pType - > type , pType - > bytes , pTargetNode - > slotId , pType - > scale ,
pType - > precision , pFuncNode - > node . aliasName ) ;
2022-03-18 10:02:00 +00:00
2022-03-22 08:03:42 +00:00
pExp - > pExpr - > _function . functionId = pFuncNode - > funcId ;
2022-03-18 10:02:00 +00:00
pExp - > pExpr - > _function . pFunctNode = pFuncNode ;
2022-03-29 12:05:04 +00:00
strncpy ( pExp - > pExpr - > _function . functionName , pFuncNode - > functionName , tListLen ( pExp - > pExpr - > _function . functionName ) ) ;
2022-03-18 10:02:00 +00:00
// TODO: value parameter needs to be handled
int32_t numOfParam = LIST_LENGTH ( pFuncNode - > pParameterList ) ;
2022-03-29 09:30:44 +00:00
pExp - > base . pParam = taosMemoryCalloc ( numOfParam , sizeof ( SFunctParam ) ) ;
pExp - > base . numOfParams = numOfParam ;
2022-03-18 10:02:00 +00:00
for ( int32_t j = 0 ; j < numOfParam ; + + j ) {
SNode * p1 = nodesListGetNode ( pFuncNode - > pParameterList , j ) ;
2022-03-29 09:30:44 +00:00
if ( p1 - > type = = QUERY_NODE_COLUMN ) {
2022-03-29 12:05:04 +00:00
SColumnNode * pcn = ( SColumnNode * ) p1 ;
2022-03-29 09:30:44 +00:00
pExp - > base . pParam [ j ] . type = FUNC_PARAM_TYPE_COLUMN ;
2022-03-29 12:05:04 +00:00
pExp - > base . pParam [ j ] . pCol = createColumn ( pcn - > dataBlockId , pcn - > slotId , & pcn - > node . resType ) ;
2022-03-29 09:30:44 +00:00
} else if ( p1 - > type = = QUERY_NODE_VALUE ) {
SValueNode * pvn = ( SValueNode * ) p1 ;
pExp - > base . pParam [ j ] . type = FUNC_PARAM_TYPE_VALUE ;
}
2022-03-18 10:02:00 +00:00
}
2022-03-26 14:13:14 +00:00
} else if ( nodeType ( pTargetNode - > pExpr ) = = QUERY_NODE_OPERATOR ) {
2022-03-26 14:17:26 +00:00
pExp - > pExpr - > nodeType = QUERY_NODE_OPERATOR ;
2022-03-29 07:24:25 +00:00
SOperatorNode * pNode = ( SOperatorNode * ) pTargetNode - > pExpr ;
2022-03-26 14:13:14 +00:00
2022-03-29 09:30:44 +00:00
pExp - > base . pParam = taosMemoryCalloc ( 1 , sizeof ( SFunctParam ) ) ;
pExp - > base . numOfParams = 1 ;
2022-03-26 14:13:14 +00:00
SDataType * pType = & pNode - > node . resType ;
2022-03-29 12:05:04 +00:00
pExp - > base . resSchema = createResSchema ( pType - > type , pType - > bytes , pTargetNode - > slotId , pType - > scale , pType - > precision , pNode - > node . aliasName ) ;
2022-03-26 14:13:14 +00:00
pExp - > pExpr - > _optrRoot . pRootNode = pTargetNode - > pExpr ;
2022-03-31 09:36:51 +00:00
// pExp->base.pParam[0].type = FUNC_PARAM_TYPE_COLUMN;
// pExp->base.pParam[0].pCol = createColumn(pTargetNode->dataBlockId, pTargetNode->slotId, pType);
2022-04-06 07:20:38 +00:00
} else if ( nodeType ( pTargetNode - > pExpr ) = = QUERY_NODE_VALUE ) {
pExp - > pExpr - > nodeType = QUERY_NODE_VALUE ;
SValueNode * pValNode = ( SValueNode * ) pTargetNode - > pExpr ;
pExp - > base . pParam = taosMemoryCalloc ( 1 , sizeof ( SFunctParam ) ) ;
pExp - > base . numOfParams = 1 ;
SDataType * pType = & pValNode - > node . resType ;
pExp - > base . resSchema = createResSchema ( pType - > type , pType - > bytes , pTargetNode - > slotId , pType - > scale , pType - > precision , pValNode - > node . aliasName ) ;
pExp - > base . pParam [ 0 ] . type = FUNC_PARAM_TYPE_VALUE ;
valueNodeToVariant ( pValNode , & pExp - > base . pParam [ 0 ] . param ) ;
2022-03-26 14:13:14 +00:00
} else {
ASSERT ( 0 ) ;
2022-03-09 02:22:53 +00:00
}
}
2022-03-12 14:59:12 +00:00
return pExprs ;
2022-03-09 02:22:53 +00:00
}
2022-03-30 06:54:00 +00:00
static SExecTaskInfo * createExecTaskInfo ( uint64_t queryId , uint64_t taskId , EOPTR_EXEC_MODEL model ) {
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-01-10 11:48:21 +00:00
pTaskInfo - > cost . created = taosGetTimestampMs ( ) ;
2022-01-11 02:51:23 +00:00
pTaskInfo - > id . queryId = queryId ;
2022-03-30 06:54:00 +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-01-25 15:08:22 +00:00
pTaskInfo - > id . str = strdup ( 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-03-29 07:24:25 +00:00
static tsdbReaderT doCreateDataReader ( STableScanPhysiNode * pTableScanNode , SReadHandle * pHandle ,
STableGroupInfo * pTableGroupInfo , uint64_t queryId , uint64_t taskId ) ;
2022-02-04 14:41:54 +00:00
2022-03-29 07:24:25 +00:00
static int32_t doCreateTableGroup ( void * metaHandle , int32_t tableType , uint64_t tableUid , STableGroupInfo * pGroupInfo ,
uint64_t queryId , uint64_t taskId ) ;
2022-03-10 15:12:34 +00:00
static SArray * extractTableIdList ( const STableGroupInfo * pTableGroupInfo ) ;
static SArray * extractScanColumnId ( SNodeList * pNodeList ) ;
2022-03-22 08:03:42 +00:00
static SArray * extractColumnInfo ( SNodeList * pNodeList ) ;
2022-03-29 02:08:21 +00:00
static SArray * extractColMatchInfo ( SNodeList * pNodeList , SDataBlockDescNode * pOutputNodeList , int32_t * numOfOutputCols ) ;
2022-03-28 11:08:07 +00:00
static SArray * createSortInfo ( SNodeList * pNodeList ) ;
2022-01-20 08:02:09 +00:00
2022-03-29 12:05:04 +00:00
SOperatorInfo * createOperatorTree ( SPhysiNode * pPhyNode , SExecTaskInfo * pTaskInfo , SReadHandle * pHandle ,
2022-03-29 07:24:25 +00:00
uint64_t queryId , uint64_t taskId , STableGroupInfo * pTableGroupInfo ) {
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
int32_t type = nodeType ( pPhyNode ) ;
if ( QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN = = type ) {
2022-02-28 09:02:43 +00:00
SScanPhysiNode * pScanPhyNode = ( SScanPhysiNode * ) pPhyNode ;
2022-01-20 08:02:09 +00:00
2022-03-29 02:08:21 +00:00
int32_t numOfCols = 0 ;
2022-03-29 12:05:04 +00:00
tsdbReaderT pDataReader = doCreateDataReader ( ( STableScanPhysiNode * ) pPhyNode , pHandle , pTableGroupInfo , ( uint64_t ) queryId , taskId ) ;
SArray * pColList = extractColMatchInfo ( pScanPhyNode - > pScanCols , pScanPhyNode - > node . pOutputDataBlockDesc , & numOfCols ) ;
2022-03-29 07:24:25 +00:00
return createTableScanOperatorInfo ( pDataReader , pScanPhyNode - > order , numOfCols , pScanPhyNode - > count ,
2022-04-01 12:19:40 +00:00
pScanPhyNode - > reverse , pColList , pScanPhyNode - > node . pConditions , pTaskInfo ) ;
2022-04-08 02:24:35 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_EXCHANGE = = type ) {
2022-03-10 10:07:07 +00:00
SExchangePhysiNode * pExchange = ( SExchangePhysiNode * ) pPhyNode ;
2022-03-29 07:24:25 +00:00
SSDataBlock * pResBlock = createOutputBuf_rv1 ( pExchange - > node . pOutputDataBlockDesc ) ;
2022-03-10 10:07:07 +00:00
return createExchangeOperatorInfo ( pExchange - > pSrcEndPoints , pResBlock , pTaskInfo ) ;
2022-04-08 02:24:35 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN = = type ) {
2022-03-10 08:18:35 +00:00
SScanPhysiNode * pScanPhyNode = ( SScanPhysiNode * ) pPhyNode ; // simple child table.
2022-03-10 15:12:34 +00:00
2022-04-06 09:59:08 +00:00
int32_t code = doCreateTableGroup ( pHandle - > meta , pScanPhyNode - > tableType , pScanPhyNode - > uid , pTableGroupInfo , queryId , taskId ) ;
2022-03-24 06:21:14 +00:00
SArray * tableIdList = extractTableIdList ( pTableGroupInfo ) ;
2022-02-17 10:16:50 +00:00
2022-03-10 15:12:34 +00:00
SSDataBlock * pResBlock = createOutputBuf_rv1 ( pScanPhyNode - > node . pOutputDataBlockDesc ) ;
2022-03-30 11:38:30 +00:00
int32_t numOfCols = 0 ;
SArray * pColList = extractColMatchInfo ( pScanPhyNode - > pScanCols , pScanPhyNode - > node . pOutputDataBlockDesc , & numOfCols ) ;
SOperatorInfo * pOperator = createStreamScanOperatorInfo ( pHandle - > reader , pResBlock , pColList , tableIdList , pTaskInfo ) ;
2022-03-10 15:12:34 +00:00
taosArrayDestroy ( tableIdList ) ;
2022-01-27 10:48:12 +00:00
return pOperator ;
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 ;
SSDataBlock * pResBlock = createOutputBuf_rv1 ( pSysScanPhyNode - > scan . node . pOutputDataBlockDesc ) ;
2022-03-11 11:29:43 +00:00
2022-03-18 16:13:07 +00:00
struct SScanPhysiNode * pScanNode = & pSysScanPhyNode - > scan ;
2022-03-29 07:24:25 +00:00
SArray * colList = extractScanColumnId ( pScanNode - > pScanCols ) ;
2022-03-18 16:13:07 +00:00
2022-03-29 07:24:25 +00:00
SOperatorInfo * pOperator = createSysTableScanOperatorInfo (
pHandle - > meta , pResBlock , & pScanNode - > tableName , pScanNode - > node . pConditions , pSysScanPhyNode - > mgmtEpSet ,
colList , pTaskInfo , pSysScanPhyNode - > showRewrite , pSysScanPhyNode - > accountId ) ;
2022-03-11 11:29:43 +00:00
return pOperator ;
} else {
ASSERT ( 0 ) ;
2022-01-20 05:52:46 +00:00
}
}
2022-04-08 02:24:35 +00:00
int32_t type = nodeType ( pPhyNode ) ;
size_t size = LIST_LENGTH ( pPhyNode - > pChildren ) ;
ASSERT ( size = = 1 ) ;
2022-03-12 14:59:12 +00:00
2022-04-08 02:24:35 +00:00
SPhysiNode * pChildNode = ( SPhysiNode * ) nodesListGetNode ( pPhyNode - > pChildren , 0 ) ;
SOperatorInfo * op = createOperatorTree ( pChildNode , pTaskInfo , pHandle , queryId , taskId , pTableGroupInfo ) ;
2022-03-12 14:59:12 +00:00
2022-04-08 02:24:35 +00:00
if ( QUERY_NODE_PHYSICAL_PLAN_PROJECT = = type ) {
2022-03-29 07:24:25 +00:00
int32_t num = 0 ;
2022-04-01 09:21:24 +00:00
SProjectPhysiNode * pProjPhyNode = ( SProjectPhysiNode * ) pPhyNode ;
SExprInfo * pExprInfo = createExprInfo ( pProjPhyNode - > pProjections , NULL , & num ) ;
2022-03-29 02:08:21 +00:00
SSDataBlock * pResBlock = createOutputBuf_rv1 ( pPhyNode - > pOutputDataBlockDesc ) ;
2022-04-01 09:21:24 +00:00
SLimit limit = { . limit = pProjPhyNode - > limit , . offset = pProjPhyNode - > offset } ;
return createProjectOperatorInfo ( op , pExprInfo , num , pResBlock , & limit , pTaskInfo ) ;
2022-04-08 02:24:35 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_AGG = = type ) {
int32_t num = 0 ;
2022-01-20 05:52:46 +00:00
2022-04-08 02:24:35 +00:00
SAggPhysiNode * pAggNode = ( SAggPhysiNode * ) pPhyNode ;
SExprInfo * pExprInfo = createExprInfo ( pAggNode - > pAggFuncs , pAggNode - > pGroupKeys , & num ) ;
SSDataBlock * pResBlock = createOutputBuf_rv1 ( pPhyNode - > pOutputDataBlockDesc ) ;
2022-03-09 02:22:53 +00:00
2022-04-08 02:24:35 +00:00
if ( pAggNode - > pGroupKeys ! = NULL ) {
SArray * pColList = extractColumnInfo ( pAggNode - > pGroupKeys ) ;
return createGroupOperatorInfo ( op , pExprInfo , num , pResBlock , pColList , pAggNode - > node . pConditions , pTaskInfo , NULL ) ;
} else {
return createAggregateOperatorInfo ( op , pExprInfo , num , pResBlock , pTaskInfo , pTableGroupInfo ) ;
2022-03-14 08:09:26 +00:00
}
2022-04-08 02:24:35 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_INTERVAL = = type ) {
SIntervalPhysiNode * pIntervalPhyNode = ( SIntervalPhysiNode * ) pPhyNode ;
2022-03-28 11:08:07 +00:00
2022-04-08 02:24:35 +00:00
int32_t num = 0 ;
SExprInfo * pExprInfo = createExprInfo ( pIntervalPhyNode - > window . pFuncs , NULL , & num ) ;
SSDataBlock * pResBlock = createOutputBuf_rv1 ( pPhyNode - > pOutputDataBlockDesc ) ;
2022-03-28 11:08:07 +00:00
2022-04-08 02:24:35 +00:00
SInterval interval = {
. interval = pIntervalPhyNode - > interval ,
. sliding = pIntervalPhyNode - > sliding ,
. intervalUnit = pIntervalPhyNode - > intervalUnit ,
. slidingUnit = pIntervalPhyNode - > slidingUnit ,
. offset = pIntervalPhyNode - > offset ,
. precision = pIntervalPhyNode - > precision
} ;
int32_t primaryTsSlotId = ( ( SColumnNode * ) pIntervalPhyNode - > pTspk ) - > slotId ;
return createIntervalOperatorInfo ( op , pExprInfo , num , pResBlock , & interval , primaryTsSlotId , pTableGroupInfo , pTaskInfo ) ;
} else if ( QUERY_NODE_PHYSICAL_PLAN_SORT = = type ) {
2022-03-28 12:11:02 +00:00
SSortPhysiNode * pSortPhyNode = ( SSortPhysiNode * ) pPhyNode ;
2022-03-28 11:08:07 +00:00
2022-03-28 12:11:02 +00:00
SSDataBlock * pResBlock = createOutputBuf_rv1 ( pPhyNode - > pOutputDataBlockDesc ) ;
2022-04-08 02:24:35 +00:00
SArray * info = createSortInfo ( pSortPhyNode - > pSortKeys ) ;
return createSortOperatorInfo ( op , pResBlock , info , pTaskInfo ) ;
} else if ( QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW = = type ) {
2022-03-28 12:11:02 +00:00
SSessionWinodwPhysiNode * pSessionNode = ( SSessionWinodwPhysiNode * ) pPhyNode ;
int32_t num = 0 ;
SExprInfo * pExprInfo = createExprInfo ( pSessionNode - > window . pFuncs , NULL , & num ) ;
SSDataBlock * pResBlock = createOutputBuf_rv1 ( pPhyNode - > pOutputDataBlockDesc ) ;
return createSessionAggOperatorInfo ( op , pExprInfo , num , pResBlock , pSessionNode - > gap , pTaskInfo ) ;
2022-04-08 02:24:35 +00:00
} else if ( QUERY_NODE_PHYSICAL_PLAN_PARTITION = = type ) {
SPartitionPhysiNode * pPartNode = ( SPartitionPhysiNode * ) pPhyNode ;
SArray * pColList = extractColumnInfo ( pPartNode - > pPartitionKeys ) ;
SSDataBlock * pResBlock = createOutputBuf_rv1 ( pPhyNode - > pOutputDataBlockDesc ) ;
return createPartitionOperatorInfo ( op , pResBlock , pColList , pTaskInfo , NULL ) ;
2022-03-28 11:08:07 +00:00
} else {
ASSERT ( 0 ) ;
2022-03-29 07:24:25 +00:00
} /*else if (pPhyNode->info.type == OP_MultiTableAggregate) {
size_t size = taosArrayGetSize ( pPhyNode - > pChildren ) ;
assert ( size = = 1 ) ;
for ( int32_t i = 0 ; i < size ; + + i ) {
SPhysiNode * pChildNode = taosArrayGetP ( pPhyNode - > pChildren , i ) ;
2022-03-29 12:05:04 +00:00
SOperatorInfo * op = createOperatorTree ( pChildNode , pTaskInfo , pHandle , queryId , taskId , pTableGroupInfo ) ;
2022-03-29 07:24:25 +00:00
return createMultiTableAggOperatorInfo ( op , pPhyNode - > pTargets , pTaskInfo , pTableGroupInfo ) ;
}
} */
2022-01-10 11:48:21 +00:00
}
2022-01-08 14:59:24 +00:00
2022-03-29 07:24:25 +00:00
static tsdbReaderT createDataReaderImpl ( STableScanPhysiNode * pTableScanNode , STableGroupInfo * pGroupInfo ,
void * readHandle , uint64_t queryId , uint64_t taskId ) {
2022-01-11 15:16:45 +00:00
STsdbQueryCond cond = { . loadExternalRows = false } ;
2022-01-08 14:59:24 +00:00
2022-03-29 07:24:25 +00:00
cond . order = pTableScanNode - > scan . order ;
2022-02-28 09:02:43 +00:00
cond . numOfCols = LIST_LENGTH ( pTableScanNode - > scan . pScanCols ) ;
2022-03-29 07:24:25 +00:00
cond . colList = taosMemoryCalloc ( cond . numOfCols , sizeof ( SColumnInfo ) ) ;
2022-01-28 02:44:02 +00:00
if ( cond . colList = = NULL ) {
terrno = TSDB_CODE_QRY_OUT_OF_MEMORY ;
return NULL ;
}
2022-02-28 09:02:43 +00:00
cond . twindow = pTableScanNode - > scanRange ;
2022-01-20 08:02:09 +00:00
cond . type = BLOCK_LOAD_OFFSET_SEQ_ORDER ;
2022-03-29 07:24:25 +00:00
// cond.type = pTableScanNode->scanFlag;
2022-01-19 06:47:53 +00:00
2022-03-10 11:14:36 +00:00
int32_t j = 0 ;
2022-01-20 08:02:09 +00:00
for ( int32_t i = 0 ; i < cond . numOfCols ; + + i ) {
2022-03-04 11:21:30 +00:00
STargetNode * pNode = ( STargetNode * ) nodesListGetNode ( pTableScanNode - > scan . pScanCols , i ) ;
SColumnNode * pColNode = ( SColumnNode * ) pNode - > pExpr ;
2022-03-10 11:14:36 +00:00
if ( pColNode - > colType = = COLUMN_TYPE_TAG ) {
continue ;
}
2022-01-20 08:02:09 +00:00
2022-04-06 09:59:08 +00:00
cond . colList [ j ] . type = pColNode - > node . resType . type ;
2022-03-10 11:14:36 +00:00
cond . colList [ j ] . bytes = pColNode - > node . resType . bytes ;
cond . colList [ j ] . colId = pColNode - > colId ;
j + = 1 ;
2022-01-20 08:02:09 +00:00
}
2022-03-10 11:14:36 +00:00
cond . numOfCols = j ;
2022-01-28 02:44:02 +00:00
return tsdbQueryTables ( readHandle , & cond , pGroupInfo , queryId , taskId ) ;
2022-01-20 08:02:09 +00:00
}
2022-01-11 15:16:45 +00:00
2022-03-10 15:12:34 +00:00
SArray * extractScanColumnId ( SNodeList * pNodeList ) {
2022-03-29 07:24:25 +00:00
size_t numOfCols = LIST_LENGTH ( pNodeList ) ;
2022-03-10 15:12:34 +00:00
SArray * pList = taosArrayInit ( numOfCols , sizeof ( int16_t ) ) ;
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 ) {
2022-03-26 05:39:10 +00:00
for ( int32_t j = 0 ; j < numOfCols ; + + j ) {
2022-03-29 07:24:25 +00:00
STargetNode * pNode = ( STargetNode * ) nodesListGetNode ( pNodeList , j ) ;
2022-03-26 05:39:10 +00:00
if ( pNode - > slotId = = i ) {
2022-03-29 07:24:25 +00:00
SColumnNode * pColNode = ( SColumnNode * ) pNode - > pExpr ;
2022-03-26 05:39:10 +00:00
taosArrayPush ( pList , & pColNode - > colId ) ;
break ;
}
}
2022-03-10 15:12:34 +00:00
}
return pList ;
}
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 ) ;
SColumnNode * pColNode = ( SColumnNode * ) pNode - > pExpr ;
2022-03-22 08:03:42 +00:00
2022-04-08 02:24:35 +00:00
// todo extract method
2022-03-22 08:03:42 +00:00
SColumn c = { 0 } ;
c . slotId = pColNode - > slotId ;
2022-04-08 02:24:35 +00:00
c . colId = pColNode - > colId ;
c . type = pColNode - > node . resType . type ;
c . bytes = pColNode - > node . resType . bytes ;
2022-03-29 07:24:25 +00:00
c . precision = pColNode - > node . resType . precision ;
c . scale = pColNode - > node . resType . scale ;
2022-03-22 08:03:42 +00:00
taosArrayPush ( pList , & c ) ;
}
return pList ;
}
2022-03-28 11:08:07 +00:00
SArray * createSortInfo ( SNodeList * pNodeList ) {
2022-03-29 07:24:25 +00:00
size_t numOfCols = LIST_LENGTH ( pNodeList ) ;
2022-03-28 11:08:07 +00:00
SArray * pList = taosArrayInit ( numOfCols , sizeof ( SBlockOrderInfo ) ) ;
if ( pList = = NULL ) {
terrno = TSDB_CODE_OUT_OF_MEMORY ;
return pList ;
}
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < numOfCols ; + + i ) {
STargetNode * pNode = ( STargetNode * ) nodesListGetNode ( pNodeList , i ) ;
SOrderByExprNode * pSortKey = ( SOrderByExprNode * ) pNode - > pExpr ;
SBlockOrderInfo bi = { 0 } ;
bi . order = ( pSortKey - > order = = ORDER_ASC ) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC ;
2022-03-28 11:08:07 +00:00
bi . nullFirst = ( pSortKey - > nullOrder = = NULL_ORDER_FIRST ) ;
SColumnNode * pColNode = ( SColumnNode * ) pSortKey - > pExpr ;
bi . slotId = pColNode - > slotId ;
taosArrayPush ( pList , & bi ) ;
}
return pList ;
}
2022-03-29 02:08:21 +00:00
SArray * extractColMatchInfo ( SNodeList * pNodeList , SDataBlockDescNode * pOutputNodeList , int32_t * numOfOutputCols ) {
2022-03-29 07:24:25 +00:00
size_t numOfCols = LIST_LENGTH ( pNodeList ) ;
2022-03-26 07:02:29 +00:00
SArray * pList = taosArrayInit ( numOfCols , sizeof ( SColMatchInfo ) ) ;
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 ) ;
SColumnNode * pColNode = ( SColumnNode * ) pNode - > pExpr ;
2022-03-26 07:02:29 +00:00
SColMatchInfo c = { 0 } ;
2022-03-29 07:24:25 +00:00
c . colId = pColNode - > colId ;
2022-03-26 07:02:29 +00:00
c . targetSlotId = pNode - > slotId ;
2022-03-29 07:24:25 +00:00
c . output = true ;
2022-03-26 07:02:29 +00:00
taosArrayPush ( pList , & c ) ;
}
2022-03-29 02:08:21 +00:00
* numOfOutputCols = 0 ;
int32_t num = LIST_LENGTH ( pOutputNodeList - > pSlots ) ;
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < num ; + + i ) {
SSlotDescNode * pNode = ( SSlotDescNode * ) nodesListGetNode ( pOutputNodeList - > pSlots , i ) ;
2022-03-29 02:08:21 +00:00
SColMatchInfo * info = taosArrayGet ( pList , pNode - > slotId ) ;
2022-03-29 12:05:04 +00:00
if ( pNode - > output ) {
( * numOfOutputCols ) + = 1 ;
} else {
info - > output = false ;
}
2022-03-29 02:08:21 +00:00
}
2022-03-26 07:02:29 +00:00
return pList ;
}
2022-04-02 07:08:48 +00:00
int32_t doCreateTableGroup ( void * metaHandle , int32_t tableType , uint64_t tableUid , STableGroupInfo * pGroupInfo , uint64_t queryId , uint64_t taskId ) {
2022-01-27 10:27:26 +00:00
int32_t code = 0 ;
2022-01-20 08:02:09 +00:00
if ( tableType = = TSDB_SUPER_TABLE ) {
2022-01-27 11:03:58 +00:00
code = tsdbQuerySTableByTagCond ( metaHandle , tableUid , 0 , NULL , 0 , 0 , NULL , pGroupInfo , NULL , 0 , queryId , taskId ) ;
2022-01-20 08:02:09 +00:00
} else { // Create one table group.
2022-01-27 11:03:58 +00:00
code = tsdbGetOneTableGroup ( metaHandle , tableUid , 0 , pGroupInfo ) ;
2022-01-27 10:27:26 +00:00
}
return code ;
}
2022-01-20 08:02:09 +00:00
2022-03-10 15:12:34 +00:00
SArray * extractTableIdList ( const STableGroupInfo * pTableGroupInfo ) {
SArray * tableIdList = taosArrayInit ( 4 , sizeof ( uint64_t ) ) ;
if ( pTableGroupInfo - > numOfTables > 0 ) {
SArray * pa = taosArrayGetP ( pTableGroupInfo - > pGroupList , 0 ) ;
ASSERT ( taosArrayGetSize ( pTableGroupInfo - > pGroupList ) = = 1 ) ;
// Transfer the Array of STableKeyInfo into uid list.
size_t numOfTables = taosArrayGetSize ( pa ) ;
for ( int32_t i = 0 ; i < numOfTables ; + + i ) {
STableKeyInfo * pkeyInfo = taosArrayGet ( pa , i ) ;
taosArrayPush ( tableIdList , & pkeyInfo - > uid ) ;
}
}
return tableIdList ;
}
2022-03-29 07:24:25 +00:00
tsdbReaderT doCreateDataReader ( STableScanPhysiNode * pTableScanNode , SReadHandle * pHandle ,
STableGroupInfo * pTableGroupInfo , uint64_t queryId , uint64_t taskId ) {
2022-01-27 10:27:26 +00:00
uint64_t uid = pTableScanNode - > scan . uid ;
2022-03-29 07:24:25 +00:00
int32_t code =
doCreateTableGroup ( pHandle - > meta , pTableScanNode - > scan . tableType , uid , pTableGroupInfo , queryId , taskId ) ;
2022-01-27 10:27:26 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
goto _error ;
2022-01-11 15:16:45 +00:00
}
2022-01-08 14:59:24 +00:00
2022-03-24 06:21:14 +00:00
if ( pTableGroupInfo - > numOfTables = = 0 ) {
2022-01-20 08:02:09 +00:00
code = 0 ;
2022-03-29 07:24:25 +00:00
qDebug ( " no table qualified for query, TID:0x% " PRIx64 " , QID:0x% " PRIx64 , taskId , queryId ) ;
2022-01-20 08:02:09 +00:00
goto _error ;
}
2022-01-08 14:59:24 +00:00
2022-03-24 06:21:14 +00:00
return createDataReaderImpl ( pTableScanNode , pTableGroupInfo , pHandle - > reader , queryId , taskId ) ;
2022-01-22 14:51:48 +00:00
2022-03-29 07:24:25 +00:00
_error :
2022-01-20 08:02:09 +00:00
terrno = code ;
return NULL ;
}
2022-03-30 06:54:00 +00:00
int32_t createExecTaskInfoImpl ( SSubplan * pPlan , SExecTaskInfo * * pTaskInfo , SReadHandle * pHandle , uint64_t taskId , 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-03-30 06:54:00 +00:00
* pTaskInfo = createExecTaskInfo ( queryId , taskId , model ) ;
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-02-14 06:39:42 +00:00
STableGroupInfo group = { 0 } ;
2022-03-29 12:05:04 +00:00
( * pTaskInfo ) - > pRoot = createOperatorTree ( pPlan - > pNode , * pTaskInfo , pHandle , queryId , taskId , & group ) ;
2022-03-15 07:34:17 +00:00
if ( NULL = = ( * pTaskInfo ) - > pRoot ) {
2022-03-29 07:24:25 +00:00
code = terrno ;
2022-03-10 11:36:30 +00:00
goto _complete ;
}
2022-03-21 10:34:42 +00:00
2022-01-13 11:01:28 +00:00
if ( ( * pTaskInfo ) - > pRoot = = NULL ) {
2022-01-21 03:19:24 +00:00
code = TSDB_CODE_QRY_OUT_OF_MEMORY ;
2022-01-21 03:55:13 +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-03-29 07:24:25 +00:00
static int32_t updateOutputBufForTopBotQuery ( SQueriedTableInfo * pTableInfo , SColumnInfo * pTagCols , SExprInfo * pExprs ,
int32_t numOfOutput , int32_t tagLen , bool superTable ) {
2021-11-02 05:37:31 +00:00
for ( int32_t i = 0 ; i < numOfOutput ; + + i ) {
int16_t functId = getExprFunctionId ( & pExprs [ i ] ) ;
if ( functId = = FUNCTION_TOP | | functId = = FUNCTION_BOTTOM ) {
int32_t j = getColumnIndexInSource ( pTableInfo , & pExprs [ i ] . base , pTagCols ) ;
if ( j < 0 | | j > = pTableInfo - > numOfCols ) {
return TSDB_CODE_QRY_INVALID_MSG ;
} else {
SColumnInfo * pCol = & pTableInfo - > colList [ j ] ;
2022-03-29 07:24:25 +00:00
// int32_t ret = getResultDataInfo(pCol->type, pCol->bytes, functId, (int32_t)pExprs[i].base.param[0].i,
// &pExprs[i].base.resSchema.type, &pExprs[i].base.resSchema.bytes,
// &pExprs[i].base.interBytes, tagLen, superTable, NULL);
// assert(ret == TSDB_CODE_SUCCESS);
2021-11-02 05:37:31 +00:00
}
}
}
return TSDB_CODE_SUCCESS ;
}
2022-03-24 03:15:05 +00:00
void setResultBufSize ( STaskAttr * pQueryAttr , SResultInfo * pResultInfo ) {
2021-11-02 05:37:31 +00:00
const int32_t DEFAULT_RESULT_MSG_SIZE = 1024 * ( 1024 + 512 ) ;
// the minimum number of rows for projection query
const int32_t MIN_ROWS_FOR_PRJ_QUERY = 8192 ;
const int32_t DEFAULT_MIN_ROWS = 4096 ;
const float THRESHOLD_RATIO = 0.85f ;
2022-04-02 07:08:48 +00:00
// if (isProjQuery(pQueryAttr)) {
// int32_t numOfRes = DEFAULT_RESULT_MSG_SIZE / pQueryAttr->resultRowSize;
// if (numOfRes < MIN_ROWS_FOR_PRJ_QUERY) {
// numOfRes = MIN_ROWS_FOR_PRJ_QUERY;
// }
//
// pResultInfo->capacity = numOfRes;
// } else { // in case of non-prj query, a smaller output buffer will be used.
// pResultInfo->capacity = DEFAULT_MIN_ROWS;
// }
2021-11-02 05:37:31 +00:00
pResultInfo - > threshold = ( int32_t ) ( pResultInfo - > capacity * THRESHOLD_RATIO ) ;
2022-03-24 03:15:05 +00:00
pResultInfo - > totalRows = 0 ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
// TODO refactor
2021-11-02 05:37:31 +00:00
void freeColumnFilterInfo ( SColumnFilterInfo * pFilter , int32_t numOfFilters ) {
2022-03-29 07:24:25 +00:00
if ( pFilter = = NULL | | numOfFilters = = 0 ) {
return ;
}
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
for ( int32_t i = 0 ; i < numOfFilters ; i + + ) {
if ( pFilter [ i ] . filterstr & & pFilter [ i ] . pz ) {
taosMemoryFree ( ( void * ) ( pFilter [ i ] . pz ) ) ;
2021-11-02 05:37:31 +00:00
}
2022-03-29 07:24:25 +00:00
}
2021-11-02 05:37:31 +00:00
2022-03-29 07:24:25 +00:00
taosMemoryFree ( pFilter ) ;
2021-11-02 05:37:31 +00:00
}
static void doDestroyTableQueryInfo ( STableGroupInfo * pTableqinfoGroupInfo ) {
if ( pTableqinfoGroupInfo - > pGroupList ! = NULL ) {
2022-03-29 07:24:25 +00:00
int32_t numOfGroups = ( int32_t ) taosArrayGetSize ( pTableqinfoGroupInfo - > pGroupList ) ;
2021-11-02 05:37:31 +00:00
for ( int32_t i = 0 ; i < numOfGroups ; + + i ) {
2022-03-29 07:24:25 +00:00
SArray * p = taosArrayGetP ( pTableqinfoGroupInfo - > pGroupList , i ) ;
2021-11-02 05:37:31 +00:00
size_t num = taosArrayGetSize ( p ) ;
2022-03-29 07:24:25 +00:00
for ( int32_t j = 0 ; j < num ; + + j ) {
2021-11-02 05:37:31 +00:00
STableQueryInfo * item = taosArrayGetP ( p , j ) ;
destroyTableQueryInfoImpl ( item ) ;
}
taosArrayDestroy ( p ) ;
}
}
taosArrayDestroy ( pTableqinfoGroupInfo - > pGroupList ) ;
taosHashCleanup ( pTableqinfoGroupInfo - > map ) ;
pTableqinfoGroupInfo - > pGroupList = NULL ;
pTableqinfoGroupInfo - > map = NULL ;
pTableqinfoGroupInfo - > numOfTables = 0 ;
}
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-01-25 02:41:35 +00:00
doDestroyTableQueryInfo ( & pTaskInfo - > tableqinfoGroupInfo ) ;
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-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-06 03:32:21 +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 ;
* 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-04-06 03:32:21 +00:00
( * pRes ) [ * resNum ] . numOfRows = operatorInfo - > resultInfo . totalRows ;
( * pRes ) [ * resNum ] . startupCost = operatorInfo - > cost . openCost ;
( * pRes ) [ * resNum ] . totalCost = operatorInfo - > cost . totalCost ;
if ( operatorInfo - > getExplainFn ) {
int32_t code = ( * operatorInfo - > getExplainFn ) ( operatorInfo , & ( * pRes ) - > verboseInfo ) ;
if ( code ) {
qError ( " operator getExplainFn failed, error:%s " , tstrerror ( code ) ) ;
return code ;
}
}
2022-04-02 11:31:52 +00:00
+ + ( * resNum ) ;
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
}