2021-09-24 10:05:56 +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-01-21 05:45:24 +00:00
*/
# include "executor.h"
2025-05-08 01:48:53 +00:00
# include <stdint.h>
2025-04-29 07:31:17 +00:00
# include "cmdnodes.h"
2025-06-23 02:36:26 +00:00
# include "dataSinkInt.h"
2025-09-25 07:48:14 +00:00
# include "executil.h"
2023-04-28 03:42:34 +00:00
# include "executorInt.h"
2025-05-08 15:00:37 +00:00
# include "libs/new-stream/stream.h"
2023-04-28 03:42:34 +00:00
# include "operator.h"
2025-05-08 15:00:37 +00:00
# include "osMemPool.h"
# include "osMemory.h"
2022-01-21 06:21:13 +00:00
# include "planner.h"
2025-05-08 01:48:53 +00:00
# include "query.h"
2023-04-28 03:42:34 +00:00
# include "querytask.h"
2025-06-23 02:36:26 +00:00
# include "storageapi.h"
2025-04-29 07:31:17 +00:00
# include "streamexecutorInt.h"
2025-11-11 07:57:54 +00:00
# include "taosdef.h"
# include "tarray.h"
2022-03-29 07:24:25 +00:00
# include "tdatablock.h"
2022-07-27 02:52:25 +00:00
# include "tref.h"
2024-07-22 04:51:25 +00:00
# include "trpc.h"
2026-02-26 09:06:07 +00:00
# include "ttypes.h"
2022-07-22 06:38:28 +00:00
# include "tudf.h"
2024-07-22 04:51:25 +00:00
# include "wal.h"
2022-07-22 06:38:28 +00:00
static TdThreadOnce initPoolOnce = PTHREAD_ONCE_INIT ;
2026-03-06 06:45:30 +00:00
int32_t fetchObjRefPool = - 1 ;
2025-05-08 01:48:53 +00:00
SGlobalExecInfo gExecInfo = { 0 } ;
2025-12-22 03:35:33 +00:00
void setTaskScalarExtraInfo ( qTaskInfo_t tinfo ) {
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
2026-03-06 06:45:30 +00:00
gTaskScalarExtra . pSubJobCtx = pTaskInfo - > pSubJobCtx ;
2026-05-21 11:48:26 +00:00
gTaskScalarExtra . isStream =
( pTaskInfo - > pSubJobCtx ! = NULL ) & & ( ( STaskSubJobCtx * ) pTaskInfo - > pSubJobCtx ) - > isStream ;
2026-01-22 05:39:05 +00:00
gTaskScalarExtra . fp = qFetchRemoteNode ;
2026-05-21 11:48:26 +00:00
if ( pTaskInfo - > pSubJobCtx ) {
gTaskScalarExtra . streamGen = pTaskInfo - > pSubJobCtx - > streamGen ;
}
}
void qSetStreamGen ( qTaskInfo_t tinfo , uint64_t gen ) {
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
if ( pTaskInfo & & pTaskInfo - > pSubJobCtx ) {
pTaskInfo - > pSubJobCtx - > streamGen = gen ;
}
2025-12-22 03:35:33 +00:00
}
2025-05-08 01:48:53 +00:00
void gExecInfoInit ( void * pDnode , getDnodeId_f getDnodeId , getMnodeEpset_f getMnode ) {
gExecInfo . dnode = pDnode ;
gExecInfo . getMnode = getMnode ;
gExecInfo . getDnodeId = getDnodeId ;
return ;
}
2025-06-23 02:36:26 +00:00
int32_t getCurrentMnodeEpset ( SEpSet * pEpSet ) {
if ( gExecInfo . dnode = = NULL | | gExecInfo . getMnode = = NULL ) {
2025-05-08 01:48:53 +00:00
qError ( " gExecInfo is not initialized " ) ;
return TSDB_CODE_APP_ERROR ;
}
gExecInfo . getMnode ( gExecInfo . dnode , pEpSet ) ;
return TSDB_CODE_SUCCESS ;
}
2022-07-22 06:38:28 +00:00
2026-03-06 06:45:30 +00:00
void doDestroyFetchObj ( void * param ) {
if ( param = = NULL ) {
return ;
}
if ( * ( bool * ) param ) {
doDestroyExchangeOperatorInfo ( param ) ;
} else {
destroySubJobCtx ( ( STaskSubJobCtx * ) param ) ;
}
}
2022-07-22 06:38:28 +00:00
static void cleanupRefPool ( ) {
2026-03-06 06:45:30 +00:00
int32_t ref = atomic_val_compare_exchange_32 ( & fetchObjRefPool , fetchObjRefPool , 0 ) ;
2024-08-19 09:30:10 +00:00
taosCloseRef ( ref ) ;
2022-07-22 06:38:28 +00:00
}
2022-01-21 06:21:13 +00:00
2023-04-04 06:50:58 +00:00
static void initRefPool ( ) {
2026-03-06 06:45:30 +00:00
fetchObjRefPool = taosOpenRef ( 1024 , doDestroyFetchObj ) ;
2024-07-22 04:51:25 +00:00
( void ) atexit ( cleanupRefPool ) ;
2023-01-18 09:27:27 +00:00
}
2022-10-27 03:06:16 +00:00
static int32_t doSetSMABlock ( SOperatorInfo * pOperator , void * input , size_t numOfBlocks , int32_t type , char * id ) {
2024-07-22 04:51:25 +00:00
int32_t code = TSDB_CODE_SUCCESS ;
int32_t lino = 0 ;
2022-10-27 03:06:16 +00:00
if ( pOperator - > operatorType ! = QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN ) {
if ( pOperator - > numOfDownstream = = 0 ) {
qError ( " failed to find stream scan operator to set the input data block, %s " PRIx64 , id ) ;
2022-12-03 02:03:18 +00:00
return TSDB_CODE_APP_ERROR ;
2022-10-27 03:06:16 +00:00
}
if ( pOperator - > numOfDownstream > 1 ) { // not handle this in join query
qError ( " join not supported for stream block scan, %s " PRIx64 , id ) ;
2022-12-03 02:03:18 +00:00
return TSDB_CODE_APP_ERROR ;
2022-10-27 03:06:16 +00:00
}
pOperator - > status = OP_NOT_OPENED ;
return doSetSMABlock ( pOperator - > pDownstream [ 0 ] , input , numOfBlocks , type , id ) ;
} else {
pOperator - > status = OP_NOT_OPENED ;
return TSDB_CODE_SUCCESS ;
}
2024-07-22 04:51:25 +00:00
_end :
if ( code ! = TSDB_CODE_SUCCESS ) {
2024-07-23 06:19:04 +00:00
qError ( " %s failed at line %d since %s " , __func__ , lino , tstrerror ( code ) ) ;
2024-07-22 04:51:25 +00:00
}
return code ;
2022-10-27 03:06:16 +00:00
}
2022-11-01 03:56:14 +00:00
static int32_t doSetStreamOpOpen ( SOperatorInfo * pOperator , char * id ) {
2023-02-22 06:32:39 +00:00
if ( pOperator - > operatorType ! = QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN ) {
if ( pOperator - > numOfDownstream = = 0 ) {
qError ( " failed to find stream scan operator to set the input data block, %s " PRIx64 , id ) ;
return TSDB_CODE_APP_ERROR ;
}
2022-11-01 03:56:14 +00:00
2023-02-22 06:32:39 +00:00
if ( pOperator - > numOfDownstream > 1 ) { // not handle this in join query
qError ( " join not supported for stream block scan, %s " PRIx64 , id ) ;
return TSDB_CODE_APP_ERROR ;
2022-11-01 03:56:14 +00:00
}
2023-06-14 06:10:09 +00:00
2023-02-22 06:32:39 +00:00
pOperator - > status = OP_NOT_OPENED ;
return doSetStreamOpOpen ( pOperator - > pDownstream [ 0 ] , id ) ;
2022-11-01 03:56:14 +00:00
}
return 0 ;
}
2024-09-13 15:04:41 +00:00
int32_t doSetTaskId ( SOperatorInfo * pOperator , SStorageAPI * pAPI ) {
2023-03-19 11:22:43 +00:00
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
if ( pOperator - > operatorType = = QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN ) {
2026-03-12 01:11:00 +00:00
STmqQueryScanInfo * pTmqScanInfo = pOperator - > info ;
if ( pTmqScanInfo - > pTableScanOp ! = NULL ) {
STableScanInfo * pScanInfo = pTmqScanInfo - > pTableScanOp - > info ;
2023-05-11 03:36:27 +00:00
if ( pScanInfo - > base . dataReader ! = NULL ) {
2024-09-13 15:04:41 +00:00
int32_t code = pAPI - > tsdReader . tsdSetReaderTaskId ( pScanInfo - > base . dataReader , pTaskInfo - > id . str ) ;
if ( code ) {
qError ( " failed to set reader id for executor, code:%s " , tstrerror ( code ) ) ;
return code ;
}
2023-05-11 03:36:27 +00:00
}
2023-03-19 11:22:43 +00:00
}
} else {
2025-05-07 10:14:17 +00:00
if ( pOperator - > pDownstream ) return doSetTaskId ( pOperator - > pDownstream [ 0 ] , pAPI ) ;
2023-03-19 11:22:43 +00:00
}
2024-09-13 15:04:41 +00:00
return 0 ;
2023-03-19 11:22:43 +00:00
}
2024-09-13 15:04:41 +00:00
int32_t qSetTaskId ( qTaskInfo_t tinfo , uint64_t taskId , uint64_t queryId ) {
2023-03-19 11:06:28 +00:00
SExecTaskInfo * pTaskInfo = tinfo ;
pTaskInfo - > id . queryId = queryId ;
2025-08-29 06:36:36 +00:00
buildTaskId ( taskId , queryId , pTaskInfo - > id . str , 64 ) ;
2023-03-19 11:22:43 +00:00
// set the idstr for tsdbReader
2024-09-13 15:04:41 +00:00
return doSetTaskId ( pTaskInfo - > pRoot , & pTaskInfo - > storageAPI ) ;
2023-03-19 11:06:28 +00:00
}
2025-05-15 06:22:46 +00:00
bool qTaskIsDone ( qTaskInfo_t tinfo ) {
SExecTaskInfo * pTaskInfo = tinfo ;
return pTaskInfo - > status = = OP_EXEC_DONE ;
}
2022-10-27 03:06:16 +00:00
int32_t qSetSMAInput ( qTaskInfo_t tinfo , const void * pBlocks , size_t numOfBlocks , int32_t type ) {
if ( tinfo = = NULL ) {
2022-12-03 02:03:18 +00:00
return TSDB_CODE_APP_ERROR ;
2022-10-27 03:06:16 +00:00
}
if ( pBlocks = = NULL | | numOfBlocks = = 0 ) {
return TSDB_CODE_SUCCESS ;
}
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
int32_t code = doSetSMABlock ( pTaskInfo - > pRoot , ( void * ) pBlocks , numOfBlocks , type , GET_TASKID ( pTaskInfo ) ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " %s failed to set the sma block data " , GET_TASKID ( pTaskInfo ) ) ;
} else {
qDebug ( " %s set the sma block successfully " , GET_TASKID ( pTaskInfo ) ) ;
}
return code ;
}
2025-12-15 03:26:46 +00:00
qTaskInfo_t qCreateQueueExecTaskInfo ( void * msg , SReadHandle * pReaderHandle , int32_t vgId , uint64_t id ) {
2023-04-04 06:50:58 +00:00
if ( msg = = NULL ) { // create raw scan
2024-07-23 09:31:41 +00:00
SExecTaskInfo * pTaskInfo = NULL ;
int32_t code = doCreateTask ( 0 , id , vgId , OPTR_EXEC_MODEL_QUEUE , & pReaderHandle - > api , & pTaskInfo ) ;
if ( NULL = = pTaskInfo | | code ! = 0 ) {
2022-08-04 07:01:59 +00:00
return NULL ;
}
2023-06-27 15:00:14 +00:00
2025-07-10 06:20:35 +00:00
code = createTmqRawScanOperatorInfo ( pReaderHandle , pTaskInfo , & pTaskInfo - > pRoot ) ;
2024-07-24 09:08:08 +00:00
if ( NULL = = pTaskInfo - > pRoot | | code ! = 0 ) {
2022-08-04 07:01:59 +00:00
taosMemoryFree ( pTaskInfo ) ;
return NULL ;
}
2023-03-29 11:35:04 +00:00
2023-05-24 05:22:05 +00:00
pTaskInfo - > storageAPI = pReaderHandle - > api ;
2023-03-29 11:35:04 +00:00
qDebug ( " create raw scan task info completed, vgId:%d, %s " , vgId , GET_TASKID ( pTaskInfo ) ) ;
2022-08-04 07:01:59 +00:00
return pTaskInfo ;
2022-07-12 06:10:22 +00:00
}
2023-07-11 11:29:52 +00:00
SSubplan * pPlan = NULL ;
int32_t code = qStringToSubplan ( msg , & pPlan ) ;
2022-07-12 06:10:22 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2025-12-15 03:26:46 +00:00
qError ( " failed to parse subplan from msg, msg:%s code:%s " , ( char * ) msg , tstrerror ( code ) ) ;
2022-07-12 06:10:22 +00:00
terrno = code ;
return NULL ;
}
qTaskInfo_t pTaskInfo = NULL ;
2026-03-20 02:08:49 +00:00
code = qCreateExecTask ( pReaderHandle , vgId , 0 , pPlan , & pTaskInfo , NULL , 0 ,
NULL , OPTR_EXEC_MODEL_QUEUE , NULL , false ) ;
2022-07-12 06:10:22 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2022-07-19 08:34:26 +00:00
qDestroyTask ( pTaskInfo ) ;
2022-07-12 06:10:22 +00:00
terrno = code ;
return NULL ;
}
return pTaskInfo ;
}
2025-05-08 15:00:37 +00:00
static int32_t checkInsertParam ( SStreamInserterParam * streamInserterParam ) {
if ( streamInserterParam = = NULL ) {
return TSDB_CODE_SUCCESS ;
}
2025-05-29 03:30:28 +00:00
if ( streamInserterParam - > tbType = = TSDB_SUPER_TABLE & & streamInserterParam - > suid < = 0 ) {
2025-05-08 15:00:37 +00:00
stError ( " insertParam: invalid suid:% " PRIx64 " for child table " , streamInserterParam - > suid ) ;
return TSDB_CODE_INVALID_PARA ;
}
2025-06-23 02:36:26 +00:00
if ( streamInserterParam - > dbFName = = NULL | | strlen ( streamInserterParam - > dbFName ) = = 0 ) {
2025-05-08 15:00:37 +00:00
stError ( " insertParam: invalid db/table name " ) ;
return TSDB_CODE_INVALID_PARA ;
2025-05-09 02:03:35 +00:00
}
if ( streamInserterParam - > suid < = 0 & &
( streamInserterParam - > tbname = = NULL | | strlen ( streamInserterParam - > tbname ) = = 0 ) ) {
2026-03-29 01:38:08 +00:00
stError ( " insertParam: invalid table name: %s, suid:% " PRIx64 " " , streamInserterParam - > tbname ? streamInserterParam - > tbname : " (null) " , streamInserterParam - > suid ) ;
2025-05-09 02:03:35 +00:00
return TSDB_CODE_INVALID_PARA ;
2025-05-08 15:00:37 +00:00
}
return TSDB_CODE_SUCCESS ;
}
2025-05-08 01:48:53 +00:00
static int32_t qCreateStreamExecTask ( SReadHandle * readHandle , int32_t vgId , uint64_t taskId , SSubplan * pSubplan ,
qTaskInfo_t * pTaskInfo , DataSinkHandle * handle , int8_t compressResult , char * sql ,
EOPTR_EXEC_MODEL model , SStreamInserterParam * streamInserterParam ) {
2025-05-14 09:55:32 +00:00
if ( pSubplan = = NULL | | pTaskInfo = = NULL ) {
qError ( " invalid parameter, pSubplan:%p, pTaskInfo:%p " , pSubplan , pTaskInfo ) ;
2025-07-03 05:26:22 +00:00
nodesDestroyNode ( ( SNode * ) pSubplan ) ;
2025-05-08 01:48:53 +00:00
return TSDB_CODE_INVALID_PARA ;
}
2025-07-03 05:26:22 +00:00
int32_t lino = 0 ;
2025-05-08 15:00:37 +00:00
int32_t code = checkInsertParam ( streamInserterParam ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " invalid stream inserter param, code:%s " , tstrerror ( code ) ) ;
2025-07-03 05:26:22 +00:00
nodesDestroyNode ( ( SNode * ) pSubplan ) ;
2025-05-08 15:00:37 +00:00
return code ;
}
2025-05-08 01:48:53 +00:00
SInserterParam * pInserterParam = NULL ;
SExecTaskInfo * * pTask = ( SExecTaskInfo * * ) pTaskInfo ;
( void ) taosThreadOnce ( & initPoolOnce , initRefPool ) ;
qDebug ( " start to create task, TID:0x% " PRIx64 " QID:0x% " PRIx64 " , vgId:%d " , taskId , pSubplan - > id . queryId , vgId ) ;
2026-03-16 06:10:31 +00:00
int32_t subTaskNum = ( int32_t ) LIST_LENGTH ( pSubplan - > pSubQ ) ;
SArray * subEndPoints = taosArrayInit ( subTaskNum , POINTER_BYTES ) ;
SDownstreamSourceNode * pSource = NULL ;
for ( int32_t i = 0 ; i < subTaskNum ; + + i ) {
SNode * pVal = nodesListGetNode ( pSubplan - > pSubQ , i ) ;
TSDB_CHECK_CODE ( nodesCloneNode ( pVal , ( SNode * * ) & pSource ) , lino , _error ) ;
if ( NULL = = taosArrayPush ( subEndPoints , & pSource ) ) {
nodesDestroyNode ( ( SNode * ) pSource ) ;
code = terrno ;
goto _error ;
}
}
2026-03-20 02:08:49 +00:00
code = createExecTaskInfo ( pSubplan , pTask , readHandle , taskId , vgId , sql , model , & subEndPoints , false ) ;
2025-05-08 01:48:53 +00:00
if ( code ! = TSDB_CODE_SUCCESS | | NULL = = * pTask ) {
qError ( " failed to createExecTaskInfo, code:%s " , tstrerror ( code ) ) ;
goto _error ;
}
2025-05-08 07:25:20 +00:00
if ( streamInserterParam ) {
2025-07-03 05:26:22 +00:00
SDataSinkMgtCfg cfg = { . maxDataBlockNum = 500 , . maxDataBlockNumPerQuery = 50 , . compress = compressResult } ;
void * pSinkManager = NULL ;
code = dsDataSinkMgtInit ( & cfg , & ( * pTask ) - > storageAPI , & pSinkManager ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " failed to dsDataSinkMgtInit, code:%s, %s " , tstrerror ( code ) , ( * pTask ) - > id . str ) ;
goto _error ;
}
2025-05-08 07:07:04 +00:00
pInserterParam = taosMemoryCalloc ( 1 , sizeof ( SInserterParam ) ) ;
if ( NULL = = pInserterParam ) {
qError ( " failed to taosMemoryCalloc, code:%s, %s " , tstrerror ( terrno ) , ( * pTask ) - > id . str ) ;
code = terrno ;
goto _error ;
}
2025-07-03 05:26:22 +00:00
code = cloneStreamInserterParam ( & pInserterParam - > streamInserterParam , streamInserterParam ) ;
TSDB_CHECK_CODE ( code , lino , _error ) ;
2025-05-08 15:00:37 +00:00
pInserterParam - > readHandle = taosMemCalloc ( 1 , sizeof ( SReadHandle ) ) ;
pInserterParam - > readHandle - > pMsgCb = readHandle - > pMsgCb ;
2025-05-08 01:48:53 +00:00
2025-05-08 07:07:04 +00:00
code = createStreamDataInserter ( pSinkManager , handle , pInserterParam ) ;
if ( code ) {
qError ( " failed to createStreamDataInserter, code:%s, %s " , tstrerror ( code ) , ( * pTask ) - > id . str ) ;
}
2025-05-08 01:48:53 +00:00
}
qDebug ( " subplan task create completed, TID:0x% " PRIx64 " QID:0x% " PRIx64 " code:%s " , taskId , pSubplan - > id . queryId ,
tstrerror ( code ) ) ;
_error :
2026-03-16 06:10:31 +00:00
if ( subEndPoints ) {
taosArrayDestroyP ( subEndPoints , ( FDelete ) nodesDestroyNode ) ;
}
2025-07-03 05:26:22 +00:00
2025-05-08 01:48:53 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2025-07-03 05:26:22 +00:00
qError ( " %s failed at line %d, error:%s " , __FUNCTION__ , lino , tstrerror ( code ) ) ;
2025-05-08 01:48:53 +00:00
if ( pInserterParam ! = NULL ) {
taosMemoryFree ( pInserterParam ) ;
}
}
return code ;
}
2025-10-21 08:17:05 +00:00
bool qNeedReset ( qTaskInfo_t pInfo ) {
if ( pInfo = = NULL ) {
return false ;
}
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) pInfo ;
SOperatorInfo * pOperator = pTaskInfo - > pRoot ;
if ( pOperator = = NULL | | pOperator - > pPhyNode = = NULL ) {
return false ;
}
int32_t node = nodeType ( pOperator - > pPhyNode ) ;
return ( QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN = = node | |
QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN = = node | |
2026-01-05 07:12:54 +00:00
QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN = = node | |
QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN = = node ) ;
2025-10-21 08:17:05 +00:00
}
static void setReadHandle ( SReadHandle * pHandle , STableScanBase * pScanBaseInfo ) {
if ( pHandle = = NULL | | pScanBaseInfo = = NULL ) {
return ;
}
pScanBaseInfo - > readHandle . uid = pHandle - > uid ;
pScanBaseInfo - > readHandle . winRangeValid = pHandle - > winRangeValid ;
pScanBaseInfo - > readHandle . winRange = pHandle - > winRange ;
pScanBaseInfo - > readHandle . extWinRangeValid = pHandle - > extWinRangeValid ;
pScanBaseInfo - > readHandle . extWinRange = pHandle - > extWinRange ;
2025-11-11 03:20:49 +00:00
pScanBaseInfo - > readHandle . cacheSttStatis = pHandle - > cacheSttStatis ;
2025-10-21 08:17:05 +00:00
}
int32_t qResetTableScan ( qTaskInfo_t pInfo , SReadHandle * handle ) {
if ( pInfo = = NULL ) {
return TSDB_CODE_INVALID_PARA ;
}
2025-06-23 02:36:26 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) pInfo ;
SOperatorInfo * pOperator = pTaskInfo - > pRoot ;
2025-05-26 06:52:02 +00:00
2025-10-21 08:17:05 +00:00
void * info = pOperator - > info ;
STableScanBase * pScanBaseInfo = NULL ;
if ( QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN = = nodeType ( pOperator - > pPhyNode ) ) {
pScanBaseInfo = & ( ( STableScanInfo * ) info ) - > base ;
setReadHandle ( handle , pScanBaseInfo ) ;
} else if ( QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN = = nodeType ( pOperator - > pPhyNode ) ) {
pScanBaseInfo = & ( ( STableMergeScanInfo * ) info ) - > base ;
setReadHandle ( handle , pScanBaseInfo ) ;
2025-05-26 06:52:02 +00:00
}
2025-10-21 08:17:05 +00:00
qDebug ( " reset table scan, name:%s, id:%s, time range: [% " PRId64 " , % " PRId64 " ] " , pOperator - > name , GET_TASKID ( pTaskInfo ) , handle - > winRange . skey ,
handle - > winRange . ekey ) ;
return pOperator - > fpSet . resetStateFn ( pOperator ) ;
2025-05-26 06:52:02 +00:00
}
2025-05-08 06:58:41 +00:00
int32_t qCreateStreamExecTaskInfo ( qTaskInfo_t * pTaskInfo , void * msg , SReadHandle * readers ,
SStreamInserterParam * pInserterParams , int32_t vgId , int32_t taskId ) {
2022-06-20 06:29:18 +00:00
if ( msg = = NULL ) {
2024-09-13 15:04:41 +00:00
return TSDB_CODE_INVALID_PARA ;
2022-01-21 05:45:24 +00:00
}
2024-09-13 15:04:41 +00:00
* pTaskInfo = NULL ;
2023-06-27 15:00:14 +00:00
SSubplan * pPlan = NULL ;
int32_t code = qStringToSubplan ( msg , & pPlan ) ;
2022-01-21 05:45:24 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2025-07-03 05:26:22 +00:00
nodesDestroyNode ( ( SNode * ) pPlan ) ;
2024-09-13 15:04:41 +00:00
return code ;
2022-01-21 05:45:24 +00:00
}
2025-05-08 01:48:53 +00:00
// todo: add stream inserter param
2025-06-23 02:36:26 +00:00
code = qCreateStreamExecTask ( readers , vgId , taskId , pPlan , pTaskInfo ,
pInserterParams ? & pInserterParams - > pSinkHandle : NULL , 0 , NULL , OPTR_EXEC_MODEL_STREAM ,
pInserterParams ) ;
2022-01-21 05:45:24 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2024-09-13 15:04:41 +00:00
qDestroyTask ( * pTaskInfo ) ;
return code ;
2022-01-21 05:45:24 +00:00
}
2024-09-13 15:04:41 +00:00
return code ;
2022-01-21 05:45:24 +00:00
}
2022-02-17 10:37:43 +00:00
2025-11-12 03:17:27 +00:00
typedef struct {
tb_uid_t tableUid ;
tb_uid_t childUid ;
2025-11-12 11:52:08 +00:00
int8_t check ;
2025-11-12 03:17:27 +00:00
} STqPair ;
2026-03-12 01:11:00 +00:00
static int32_t filterUnqualifiedTables ( const STmqQueryScanInfo * pScanInfo , const SArray * tableIdList , const char * idstr ,
2024-07-22 04:51:25 +00:00
SStorageAPI * pAPI , SArray * * ppArrayRes ) {
int32_t code = TSDB_CODE_SUCCESS ;
int32_t lino = 0 ;
2025-11-12 03:17:27 +00:00
int8_t locked = 0 ;
2022-05-20 09:07:53 +00:00
SArray * qa = taosArrayInit ( 4 , sizeof ( tb_uid_t ) ) ;
2025-11-12 03:17:27 +00:00
2024-08-27 02:50:28 +00:00
QUERY_CHECK_NULL ( qa , code , lino , _error , terrno ) ;
2025-11-12 03:17:27 +00:00
SArray * tUid = taosArrayInit ( 4 , sizeof ( STqPair ) ) ;
QUERY_CHECK_NULL ( tUid , code , lino , _error , terrno ) ;
2023-02-23 01:21:32 +00:00
int32_t numOfUids = taosArrayGetSize ( tableIdList ) ;
if ( numOfUids = = 0 ) {
2024-07-22 04:51:25 +00:00
( * ppArrayRes ) = qa ;
2024-08-27 02:50:28 +00:00
goto _error ;
2023-02-23 01:21:32 +00:00
}
2022-05-20 09:07:53 +00:00
2023-04-16 14:48:22 +00:00
STableScanInfo * pTableScanInfo = pScanInfo - > pTableScanOp - > info ;
2023-04-17 06:08:54 +00:00
uint64_t suid = 0 ;
2023-04-17 02:59:24 +00:00
uint64_t uid = 0 ;
2023-06-19 12:48:49 +00:00
int32_t type = 0 ;
2023-04-17 06:08:54 +00:00
tableListGetSourceTableInfo ( pTableScanInfo - > base . pTableListInfo , & suid , & uid , & type ) ;
2023-04-16 14:48:22 +00:00
2022-05-20 09:07:53 +00:00
// let's discard the tables those are not created according to the queried super table.
SMetaReader mr = { 0 } ;
2024-03-16 14:03:46 +00:00
pAPI - > metaReaderFn . initReader ( & mr , pScanInfo - > readHandle . vnode , META_READER_LOCK , & pAPI - > metaFn ) ;
2025-11-12 03:17:27 +00:00
locked = 1 ;
2023-02-23 01:21:32 +00:00
for ( int32_t i = 0 ; i < numOfUids ; + + i ) {
2022-07-20 14:38:19 +00:00
uint64_t * id = ( uint64_t * ) taosArrayGet ( tableIdList , i ) ;
2024-08-05 08:09:01 +00:00
QUERY_CHECK_NULL ( id , code , lino , _end , terrno ) ;
2022-05-20 09:07:53 +00:00
2023-05-23 10:29:23 +00:00
int32_t code = pAPI - > metaReaderFn . getTableEntryByUid ( & mr , * id ) ;
2022-05-20 09:07:53 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2022-07-18 07:31:39 +00:00
qError ( " failed to get table meta, uid:% " PRIu64 " code:%s, %s " , * id , tstrerror ( terrno ) , idstr ) ;
2022-05-20 09:07:53 +00:00
continue ;
}
2022-08-05 07:42:45 +00:00
tDecoderClear ( & mr . coder ) ;
2023-04-16 14:48:22 +00:00
if ( mr . me . type = = TSDB_SUPER_TABLE ) {
2022-05-20 09:07:53 +00:00
continue ;
2023-04-16 14:48:22 +00:00
} else {
if ( type = = TSDB_SUPER_TABLE ) {
// this new created child table does not belong to the scanned super table.
2023-04-17 06:08:54 +00:00
if ( mr . me . type ! = TSDB_CHILD_TABLE | | mr . me . ctbEntry . suid ! = suid ) {
2023-04-16 14:48:22 +00:00
continue ;
}
} else { // ordinary table
// In case that the scanned target table is an ordinary table. When replay the WAL during restore the vnode, we
// should check all newly created ordinary table to make sure that this table isn't the destination table.
2023-04-17 02:59:24 +00:00
if ( mr . me . uid ! = uid ) {
2023-04-16 14:48:22 +00:00
continue ;
}
}
2022-05-20 09:07:53 +00:00
}
2022-07-18 07:31:39 +00:00
2025-11-12 11:52:08 +00:00
STqPair item = { . tableUid = * id , . childUid = mr . me . uid , . check = 0 } ;
2022-07-18 07:31:39 +00:00
if ( pScanInfo - > pTagCond ! = NULL ) {
2025-11-12 11:52:08 +00:00
// tb_uid_t id = mr.me.uid;
item . check = 1 ;
}
if ( taosArrayPush ( tUid , & item ) = = NULL ) {
QUERY_CHECK_NULL ( NULL , code , lino , _end , terrno ) ;
2025-11-12 03:17:27 +00:00
}
}
2022-07-18 07:31:39 +00:00
2025-11-12 03:17:27 +00:00
pAPI - > metaReaderFn . clearReader ( & mr ) ;
locked = 0 ;
for ( int32_t j = 0 ; j < taosArrayGetSize ( tUid ) ; + + j ) {
bool qualified = false ;
STqPair * t = ( STqPair * ) taosArrayGet ( tUid , j ) ;
if ( t = = NULL ) {
continue ;
2022-07-18 07:31:39 +00:00
}
2025-11-12 11:52:08 +00:00
if ( t - > check = = 1 ) {
2025-12-22 10:14:39 +00:00
code = isQualifiedTable ( t - > childUid , pScanInfo - > pTagCond , pScanInfo - > readHandle . vnode , & qualified , pAPI ) ;
2022-07-18 07:31:39 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2025-12-22 10:14:39 +00:00
qError ( " failed to filter new table, uid:0x% " PRIx64 " , %s " , t - > childUid , idstr ) ;
2022-07-18 07:31:39 +00:00
continue ;
}
if ( ! qualified ) {
2025-12-23 07:33:55 +00:00
qInfo ( " table uid:0x% " PRIx64 " is unqualified for tag condition, %s " , t - > childUid , idstr ) ;
2022-07-18 07:31:39 +00:00
continue ;
}
}
2025-11-12 03:17:27 +00:00
void * tmp = taosArrayPush ( qa , & t - > tableUid ) ;
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL ( tmp , code , lino , _end , terrno ) ;
2022-05-20 09:07:53 +00:00
}
2025-11-12 03:17:27 +00:00
// handle multiple partition
2024-08-27 02:50:28 +00:00
_end :
2024-11-06 11:26:14 +00:00
2025-11-12 03:17:27 +00:00
if ( locked ) {
pAPI - > metaReaderFn . clearReader ( & mr ) ;
}
2024-07-22 04:51:25 +00:00
( * ppArrayRes ) = qa ;
2024-08-27 02:50:28 +00:00
_error :
2024-07-22 04:51:25 +00:00
2025-11-12 03:17:27 +00:00
taosArrayDestroy ( tUid ) ;
2024-07-22 04:51:25 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " %s failed at line %d since %s " , __func__ , lino , tstrerror ( code ) ) ;
}
return code ;
2022-05-20 09:07:53 +00:00
}
2026-02-26 09:06:07 +00:00
int32_t qDeleteTableListForTmqScanner ( qTaskInfo_t tinfo , const SArray * tableIdList ) {
2022-11-01 11:27:35 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
2023-04-08 17:39:09 +00:00
const char * id = GET_TASKID ( pTaskInfo ) ;
int32_t code = 0 ;
2022-09-16 05:06:57 +00:00
2022-05-17 08:02:03 +00:00
// traverse to the stream scanner node to add this table id
2024-07-24 09:08:08 +00:00
SOperatorInfo * pInfo = NULL ;
code = extractOperatorInTree ( pTaskInfo - > pRoot , QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN , id , & pInfo ) ;
if ( code ! = 0 | | pInfo = = NULL ) {
return code ;
}
2026-03-12 01:11:00 +00:00
STmqQueryScanInfo * pScanInfo = pInfo - > info ;
2026-02-02 11:12:35 +00:00
qDebug ( " %d remove child tables from the stream scanner, %s " , ( int32_t ) taosArrayGetSize ( tableIdList ) , id ) ;
taosWLockLatch ( & pTaskInfo - > lock ) ;
pTaskInfo - > storageAPI . tqReaderFn . tqReaderRemoveTables ( pScanInfo - > tqReader , tableIdList ) ;
taosWUnLockLatch ( & pTaskInfo - > lock ) ;
2023-04-08 17:39:09 +00:00
2026-02-02 11:12:35 +00:00
return code ;
}
2026-03-12 01:11:00 +00:00
static int32_t filterTableForTmqQuery ( STmqQueryScanInfo * pScanInfo , const SArray * tableIdList , const char * id , SStorageAPI * pAPI , SRWLatch * lock ) {
2026-02-02 11:12:35 +00:00
SArray * qa = NULL ;
int32_t code = filterUnqualifiedTables ( pScanInfo , tableIdList , id , pAPI , & qa ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
taosArrayDestroy ( qa ) ;
return code ;
}
int32_t numOfQualifiedTables = taosArrayGetSize ( qa ) ;
qDebug ( " %d qualified child tables added into stream scanner, %s " , numOfQualifiedTables , id ) ;
pAPI - > tqReaderFn . tqReaderAddTables ( pScanInfo - > tqReader , qa ) ;
bool assignUid = false ;
size_t bufLen = ( pScanInfo - > pGroupTags ! = NULL ) ? getTableTagsBufLen ( pScanInfo - > pGroupTags ) : 0 ;
char * keyBuf = NULL ;
if ( bufLen > 0 ) {
assignUid = groupbyTbname ( pScanInfo - > pGroupTags ) ;
keyBuf = taosMemoryMalloc ( bufLen ) ;
if ( keyBuf = = NULL ) {
2024-07-22 04:51:25 +00:00
taosArrayDestroy ( qa ) ;
2026-02-02 11:12:35 +00:00
return terrno ;
}
}
STableListInfo * pTableListInfo = ( ( STableScanInfo * ) pScanInfo - > pTableScanOp - > info ) - > base . pTableListInfo ;
taosWLockLatch ( lock ) ;
for ( int32_t i = 0 ; i < numOfQualifiedTables ; + + i ) {
uint64_t * uid = taosArrayGet ( qa , i ) ;
if ( ! uid ) {
taosMemoryFree ( keyBuf ) ;
taosArrayDestroy ( qa ) ;
taosWUnLockLatch ( lock ) ;
return terrno ;
2024-07-22 04:51:25 +00:00
}
2026-02-02 11:12:35 +00:00
STableKeyInfo keyInfo = { . uid = * uid , . groupId = 0 } ;
2022-07-20 14:38:19 +00:00
2022-07-22 06:38:28 +00:00
if ( bufLen > 0 ) {
2026-02-02 11:12:35 +00:00
if ( assignUid ) {
keyInfo . groupId = keyInfo . uid ;
} else {
code = getGroupIdFromTagsVal ( pScanInfo - > readHandle . vnode , keyInfo . uid , pScanInfo - > pGroupTags , keyBuf ,
& keyInfo . groupId , pAPI ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
taosMemoryFree ( keyBuf ) ;
taosArrayDestroy ( qa ) ;
taosWUnLockLatch ( lock ) ;
return code ;
}
2022-07-22 06:38:28 +00:00
}
}
2022-07-20 14:38:19 +00:00
2026-02-02 11:12:35 +00:00
code = tableListAddTableInfo ( pTableListInfo , keyInfo . uid , keyInfo . groupId ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
taosMemoryFree ( keyBuf ) ;
taosArrayDestroy ( qa ) ;
taosWUnLockLatch ( lock ) ;
return code ;
}
}
2022-10-28 11:56:32 +00:00
2026-02-02 11:12:35 +00:00
taosWUnLockLatch ( lock ) ;
if ( keyBuf ! = NULL ) {
taosMemoryFree ( keyBuf ) ;
}
2022-10-28 11:56:32 +00:00
2026-02-02 11:12:35 +00:00
taosArrayDestroy ( qa ) ;
return 0 ;
}
2022-07-22 06:38:28 +00:00
2026-03-12 01:11:00 +00:00
static void qUpdateTableTagCache ( STmqQueryScanInfo * pScanInfo , const SArray * tableIdList , col_id_t cid , SStorageAPI * api ) {
2026-02-26 09:06:07 +00:00
STqReader * tqReader = pScanInfo - > tqReader ;
for ( int32_t i = 0 ; i < taosArrayGetSize ( tableIdList ) ; + + i ) {
int64_t * uid = ( int64_t * ) taosArrayGet ( tableIdList , i ) ;
api - > tqReaderFn . tqUpdateTableTagCache ( pScanInfo - > tqReader , pScanInfo - > pPseudoExpr , pScanInfo - > numOfPseudoExpr , * uid , cid ) ;
}
}
int32_t qAddTableListForTmqScanner ( qTaskInfo_t tinfo , const SArray * tableIdList ) {
2026-02-02 11:12:35 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
const char * id = GET_TASKID ( pTaskInfo ) ;
int32_t code = 0 ;
2022-07-22 06:38:28 +00:00
2026-02-02 11:12:35 +00:00
qDebug ( " try to add %d tables id into query list, %s " , ( int32_t ) taosArrayGetSize ( tableIdList ) , id ) ;
2022-07-20 14:38:19 +00:00
2026-02-02 11:12:35 +00:00
// traverse to the stream scanner node to add this table id
SOperatorInfo * pInfo = NULL ;
code = extractOperatorInTree ( pTaskInfo - > pRoot , QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN , id , & pInfo ) ;
if ( code ! = 0 | | pInfo = = NULL ) {
return code ;
}
2026-03-12 01:11:00 +00:00
STmqQueryScanInfo * pScanInfo = pInfo - > info ;
2026-02-26 09:06:07 +00:00
qUpdateTableTagCache ( pScanInfo , tableIdList , 0 , & pTaskInfo - > storageAPI ) ;
2022-07-22 06:38:28 +00:00
2026-02-02 11:12:35 +00:00
return filterTableForTmqQuery ( pScanInfo , tableIdList , id , & pTaskInfo - > storageAPI , & pTaskInfo - > lock ) ;
}
2026-03-21 03:38:22 +00:00
// once of cids and cidListArray is NULL
void qUpdateTableTagCacheForTmq ( qTaskInfo_t tinfo , const SArray * tableIdList , SArray * cids , SArray * cidListArray ) {
2026-02-02 11:12:35 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
const char * id = GET_TASKID ( pTaskInfo ) ;
int32_t code = 0 ;
// traverse to the stream scanner node to add this table id
SOperatorInfo * pInfo = NULL ;
code = extractOperatorInTree ( pTaskInfo - > pRoot , QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN , id , & pInfo ) ;
if ( code ! = 0 | | pInfo = = NULL ) {
2026-02-26 09:06:07 +00:00
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( terrno ) ) ;
return ;
2022-02-17 10:37:43 +00:00
}
2026-03-12 01:11:00 +00:00
STmqQueryScanInfo * pScanInfo = pInfo - > info ;
2026-02-26 09:06:07 +00:00
for ( int32_t i = 0 ; i < taosArrayGetSize ( cids ) ; + + i ) {
col_id_t * cid = ( col_id_t * ) taosArrayGet ( cids , i ) ;
qUpdateTableTagCache ( pScanInfo , tableIdList , * cid , & pTaskInfo - > storageAPI ) ;
}
2026-03-21 03:38:22 +00:00
for ( int32_t i = 0 ; i < taosArrayGetSize ( cidListArray ) ; + + i ) {
SArray * cidsTmp = ( SArray * ) taosArrayGetP ( cidListArray , i ) ;
for ( int32_t j = 0 ; j < taosArrayGetSize ( cidsTmp ) ; + + j ) {
col_id_t * cid = ( col_id_t * ) taosArrayGet ( cidsTmp , j ) ;
qUpdateTableTagCache ( pScanInfo , tableIdList , * cid , & pTaskInfo - > storageAPI ) ;
}
}
2026-02-26 09:06:07 +00:00
}
2026-02-02 11:12:35 +00:00
2026-02-26 09:06:07 +00:00
int32_t qUpdateTableListForTmqScanner ( qTaskInfo_t tinfo , const SArray * tableIdList ) {
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
const char * id = GET_TASKID ( pTaskInfo ) ;
int32_t code = 0 ;
// traverse to the stream scanner node to add this table id
SOperatorInfo * pInfo = NULL ;
code = extractOperatorInTree ( pTaskInfo - > pRoot , QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN , id , & pInfo ) ;
if ( code ! = 0 | | pInfo = = NULL ) {
return code ;
2026-02-02 11:12:35 +00:00
}
2026-03-12 01:11:00 +00:00
STmqQueryScanInfo * pScanInfo = pInfo - > info ;
2026-02-26 09:06:07 +00:00
2026-02-02 11:12:35 +00:00
qDebug ( " %s %d remove child tables from the stream scanner, %s " , __func__ , ( int32_t ) taosArrayGetSize ( tableIdList ) , id ) ;
taosWLockLatch ( & pTaskInfo - > lock ) ;
pTaskInfo - > storageAPI . tqReaderFn . tqReaderRemoveTables ( pScanInfo - > tqReader , tableIdList ) ;
taosWUnLockLatch ( & pTaskInfo - > lock ) ;
return filterTableForTmqQuery ( pScanInfo , tableIdList , id , & pTaskInfo - > storageAPI , & pTaskInfo - > lock ) ;
2022-03-02 08:22:43 +00:00
}
2022-05-18 12:39:00 +00:00
2024-10-25 05:47:09 +00:00
int32_t qGetQueryTableSchemaVersion ( qTaskInfo_t tinfo , char * dbName , int32_t dbNameBuffLen , char * tableName ,
2025-05-10 02:34:54 +00:00
int32_t tbaleNameBuffLen , int32_t * sversion , int32_t * tversion , int32_t * rversion ,
int32_t idx , bool * tbGet ) {
2024-07-15 02:05:57 +00:00
* tbGet = false ;
2024-08-20 07:02:09 +00:00
if ( tinfo = = NULL | | dbName = = NULL | | tableName = = NULL ) {
return TSDB_CODE_INVALID_PARA ;
}
2022-06-15 08:11:20 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
2022-05-18 12:39:00 +00:00
2023-08-11 09:39:41 +00:00
if ( taosArrayGetSize ( pTaskInfo - > schemaInfos ) < = idx ) {
2024-07-15 02:05:57 +00:00
return TSDB_CODE_SUCCESS ;
2022-06-29 02:35:07 +00:00
}
2023-08-11 09:39:41 +00:00
SSchemaInfo * pSchemaInfo = taosArrayGet ( pTaskInfo - > schemaInfos , idx ) ;
2024-08-05 08:09:01 +00:00
if ( ! pSchemaInfo ) {
2024-08-05 10:18:13 +00:00
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( terrno ) ) ;
2024-08-05 08:09:01 +00:00
return terrno ;
}
2023-08-11 09:39:41 +00:00
* sversion = pSchemaInfo - > sw - > version ;
* tversion = pSchemaInfo - > tversion ;
2025-05-10 02:34:54 +00:00
* rversion = pSchemaInfo - > rversion ;
2023-08-11 09:39:41 +00:00
if ( pSchemaInfo - > dbname ) {
2024-10-25 05:47:09 +00:00
tstrncpy ( dbName , pSchemaInfo - > dbname , dbNameBuffLen ) ;
2022-05-19 14:08:06 +00:00
} else {
dbName [ 0 ] = 0 ;
}
2023-08-11 09:39:41 +00:00
if ( pSchemaInfo - > tablename ) {
2024-10-25 05:47:09 +00:00
tstrncpy ( tableName , pSchemaInfo - > tablename , tbaleNameBuffLen ) ;
2022-05-19 14:08:06 +00:00
} else {
tableName [ 0 ] = 0 ;
}
2022-05-18 12:39:00 +00:00
2024-07-15 02:05:57 +00:00
* tbGet = true ;
2023-08-11 09:39:41 +00:00
return TSDB_CODE_SUCCESS ;
2022-06-15 08:11:20 +00:00
}
2022-07-22 06:38:28 +00:00
2024-07-22 04:51:25 +00:00
bool qIsDynamicExecTask ( qTaskInfo_t tinfo ) { return ( ( SExecTaskInfo * ) tinfo ) - > dynamicTask ; }
2023-07-05 11:24:06 +00:00
2025-03-19 09:15:17 +00:00
void qDestroyOperatorParam ( SOperatorParam * pParam ) {
if ( NULL = = pParam ) {
return ;
}
freeOperatorParam ( pParam , OP_GET_PARAM ) ;
}
2026-01-16 02:32:01 +00:00
/**
@ brief Update the operator param for the task .
@ note Unlike setOperatorParam , this function will destroy the new param when
operator type mismatch .
*/
2023-07-05 11:24:06 +00:00
void qUpdateOperatorParam ( qTaskInfo_t tinfo , void * pParam ) {
2026-01-16 02:32:01 +00:00
SExecTaskInfo * pTask = ( SExecTaskInfo * ) tinfo ;
SOperatorParam * pNewParam = ( SOperatorParam * ) pParam ;
if ( pTask - > pRoot & & pTask - > pRoot - > operatorType ! = pNewParam - > opType ) {
qError ( " %s, %s operator type mismatch, task operator type:%d, "
" new param operator type:%d " , GET_TASKID ( pTask ) , __func__ ,
pTask - > pRoot - > operatorType ,
pNewParam - > opType ) ;
qDestroyOperatorParam ( ( SOperatorParam * ) pParam ) ;
return ;
}
TSWAP ( pParam , pTask - > pOpParam ) ;
2023-07-05 11:24:06 +00:00
( ( SExecTaskInfo * ) tinfo ) - > paramSet = false ;
2022-06-15 08:11:20 +00:00
}
2022-07-22 06:38:28 +00:00
2024-07-02 11:25:33 +00:00
int32_t qExecutorInit ( void ) {
2025-02-20 11:20:40 +00:00
( void ) taosThreadOnce ( & initPoolOnce , initRefPool ) ;
2024-07-02 11:25:33 +00:00
return TSDB_CODE_SUCCESS ;
}
2026-05-20 03:19:07 +00:00
void qUpdateWorkerCb ( qTaskInfo_t task , void * pWorkerCb ) {
if ( task = = NULL ) return ;
SExecTaskInfo * pTask = ( SExecTaskInfo * ) task ;
pTask - > pWorkerCb = ( SQueryAutoQWorkerPoolCB * ) pWorkerCb ;
}
2025-12-22 03:35:33 +00:00
int32_t qSemWait ( qTaskInfo_t task , tsem_t * pSem ) {
int32_t code = TSDB_CODE_SUCCESS ;
SExecTaskInfo * pTask = ( SExecTaskInfo * ) task ;
if ( pTask - > pWorkerCb ) {
code = pTask - > pWorkerCb - > beforeBlocking ( pTask - > pWorkerCb - > pPool ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
pTask - > code = code ;
return pTask - > code ;
}
}
code = tsem_wait ( pSem ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( code ) ) ;
pTask - > code = code ;
return pTask - > code ;
}
if ( pTask - > pWorkerCb ) {
code = pTask - > pWorkerCb - > afterRecoverFromBlocking ( pTask - > pWorkerCb - > pPool ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
pTask - > code = code ;
return pTask - > code ;
}
}
return TSDB_CODE_SUCCESS ;
}
2022-07-22 06:38:28 +00:00
int32_t qCreateExecTask ( SReadHandle * readHandle , int32_t vgId , uint64_t taskId , SSubplan * pSubplan ,
2024-06-04 07:07:02 +00:00
qTaskInfo_t * pTaskInfo , DataSinkHandle * handle , int8_t compressResult , char * sql ,
2026-03-20 02:08:49 +00:00
EOPTR_EXEC_MODEL model , SArray * * subEndPoints , bool enableExplain ) {
2022-07-22 06:38:28 +00:00
SExecTaskInfo * * pTask = ( SExecTaskInfo * * ) pTaskInfo ;
2024-07-22 04:51:25 +00:00
( void ) taosThreadOnce ( & initPoolOnce , initRefPool ) ;
2022-07-22 06:38:28 +00:00
2026-03-20 02:08:49 +00:00
qDebug ( " start to create task, TID:0x% " PRIx64 " QID:0x% " PRIx64 " , vgId:%d, subEndPoinsNum:%d, enableExplain:%d " ,
taskId , pSubplan - > id . queryId , vgId , ( int32_t ) taosArrayGetSize ( subEndPoints ? * subEndPoints : NULL ) , enableExplain ) ;
2022-08-09 10:35:10 +00:00
2025-06-05 09:26:09 +00:00
readHandle - > uid = 0 ;
2026-03-20 02:08:49 +00:00
int32_t code = createExecTaskInfo ( pSubplan , pTask , readHandle , taskId , vgId , sql , model , subEndPoints , enableExplain ) ;
2023-07-06 03:13:43 +00:00
if ( code ! = TSDB_CODE_SUCCESS | | NULL = = * pTask ) {
2025-02-26 03:34:50 +00:00
qError ( " failed to createExecTaskInfo, code:%s " , tstrerror ( code ) ) ;
2022-07-22 06:38:28 +00:00
goto _error ;
}
2025-12-22 03:35:33 +00:00
2022-07-22 06:38:28 +00:00
if ( handle ) {
2024-06-04 07:07:02 +00:00
SDataSinkMgtCfg cfg = { . maxDataBlockNum = 500 , . maxDataBlockNumPerQuery = 50 , . compress = compressResult } ;
2024-07-22 04:51:25 +00:00
void * pSinkManager = NULL ;
2023-08-22 10:40:42 +00:00
code = dsDataSinkMgtInit ( & cfg , & ( * pTask ) - > storageAPI , & pSinkManager ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " failed to dsDataSinkMgtInit, code:%s, %s " , tstrerror ( code ) , ( * pTask ) - > id . str ) ;
goto _error ;
}
2022-07-22 06:38:28 +00:00
void * pSinkParam = NULL ;
2023-04-06 06:40:01 +00:00
code = createDataSinkParam ( pSubplan - > pDataSink , & pSinkParam , ( * pTask ) , readHandle ) ;
2022-07-22 06:38:28 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2022-10-20 03:27:33 +00:00
qError ( " failed to createDataSinkParam, vgId:%d, code:%s, %s " , vgId , tstrerror ( code ) , ( * pTask ) - > id . str ) ;
2023-08-22 10:40:42 +00:00
taosMemoryFree ( pSinkManager ) ;
2022-07-22 06:38:28 +00:00
goto _error ;
}
2024-11-15 02:16:15 +00:00
SDataSinkNode * pSink = NULL ;
if ( readHandle - > localExec ) {
2025-05-07 11:20:45 +00:00
code = nodesCloneNode ( ( SNode * ) pSubplan - > pDataSink , ( SNode * * ) & pSink ) ;
2024-11-15 02:16:15 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
2025-05-07 11:20:45 +00:00
qError ( " failed to nodesCloneNode, srcType:%d, code:%s, %s " , nodeType ( pSubplan - > pDataSink ) , tstrerror ( code ) ,
( * pTask ) - > id . str ) ;
2024-11-15 02:16:15 +00:00
taosMemoryFree ( pSinkManager ) ;
goto _error ;
}
}
2023-04-18 02:40:53 +00:00
// pSinkParam has been freed during create sinker.
2025-05-07 11:20:45 +00:00
code = dsCreateDataSinker ( pSinkManager , readHandle - > localExec ? & pSink : & pSubplan - > pDataSink , handle , pSinkParam ,
( * pTask ) - > id . str , pSubplan - > processOneBlock ) ;
2024-09-14 05:39:23 +00:00
if ( code ) {
qError ( " s-task:%s failed to create data sinker, code:%s " , ( * pTask ) - > id . str , tstrerror ( code ) ) ;
}
2022-07-22 06:38:28 +00:00
}
2025-12-22 03:35:33 +00:00
qDebug ( " subplan task create completed, TID:0x% " PRIx64 " QID:0x% " PRIx64 " code:%s subEndPoints:%d " ,
2026-03-06 06:45:30 +00:00
taskId , pSubplan - > id . queryId , tstrerror ( code ) , ( * pTask ) - > pSubJobCtx ? ( int32_t ) taosArrayGetSize ( ( * pTask ) - > pSubJobCtx - > subEndPoints ) : 0 ) ;
2022-08-09 10:35:10 +00:00
2022-11-01 11:27:35 +00:00
_error :
2022-07-22 06:38:28 +00:00
// if failed to add ref for all tables in this query, abort current query
return code ;
}
2022-08-09 05:45:44 +00:00
static void freeBlock ( void * param ) {
2022-08-19 03:10:56 +00:00
SSDataBlock * pBlock = * ( SSDataBlock * * ) param ;
2022-08-09 05:45:44 +00:00
blockDataDestroy ( pBlock ) ;
}
2025-05-07 11:20:45 +00:00
int32_t qExecTaskOpt ( qTaskInfo_t tinfo , SArray * pResList , uint64_t * useconds , bool * hasMore , SLocalFetch * pLocal ,
bool processOneBlock ) {
2024-07-22 04:51:25 +00:00
int32_t code = TSDB_CODE_SUCCESS ;
int32_t lino = 0 ;
2022-07-22 06:38:28 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
int64_t threadId = taosGetSelfPthreadId ( ) ;
2022-09-09 11:03:42 +00:00
if ( pLocal ) {
2022-09-15 06:46:15 +00:00
memcpy ( & pTaskInfo - > localFetch , pLocal , sizeof ( * pLocal ) ) ;
2022-09-09 11:03:42 +00:00
}
2022-09-28 02:38:49 +00:00
2022-11-12 08:03:47 +00:00
taosArrayClear ( pResList ) ;
2022-08-09 05:45:44 +00:00
2022-07-22 06:38:28 +00:00
int64_t curOwner = 0 ;
if ( ( curOwner = atomic_val_compare_exchange_64 ( & pTaskInfo - > owner , 0 , threadId ) ) ! = 0 ) {
qError ( " %s-%p execTask is now executed by thread:%p " , GET_TASKID ( pTaskInfo ) , pTaskInfo , ( void * ) curOwner ) ;
pTaskInfo - > code = TSDB_CODE_QRY_IN_EXEC ;
return pTaskInfo - > code ;
}
if ( pTaskInfo - > cost . start = = 0 ) {
2022-12-02 15:10:03 +00:00
pTaskInfo - > cost . start = taosGetTimestampUs ( ) ;
2022-07-22 06:38:28 +00:00
}
if ( isTaskKilled ( pTaskInfo ) ) {
2022-07-25 08:02:49 +00:00
atomic_store_64 ( & pTaskInfo - > owner , 0 ) ;
2022-07-22 06:38:28 +00:00
qDebug ( " %s already killed, abort " , GET_TASKID ( pTaskInfo ) ) ;
2024-08-19 01:50:38 +00:00
return pTaskInfo - > code ;
2022-07-22 06:38:28 +00:00
}
// error occurs, record the error code and return to client
int32_t ret = setjmp ( pTaskInfo - > env ) ;
if ( ret ! = TSDB_CODE_SUCCESS ) {
pTaskInfo - > code = ret ;
2024-07-22 04:51:25 +00:00
( void ) cleanUpUdfs ( ) ;
2022-09-15 07:02:23 +00:00
2022-07-22 06:38:28 +00:00
qDebug ( " %s task abort due to error/cancel occurs, code:%s " , GET_TASKID ( pTaskInfo ) , tstrerror ( pTaskInfo - > code ) ) ;
atomic_store_64 ( & pTaskInfo - > owner , 0 ) ;
return pTaskInfo - > code ;
}
qDebug ( " %s execTask is launched " , GET_TASKID ( pTaskInfo ) ) ;
2022-08-19 03:10:56 +00:00
int32_t current = 0 ;
2022-08-09 05:45:44 +00:00
SSDataBlock * pRes = NULL ;
2024-07-24 09:08:08 +00:00
int64_t st = taosGetTimestampUs ( ) ;
2022-07-22 06:38:28 +00:00
2023-07-05 11:24:06 +00:00
if ( pTaskInfo - > pOpParam & & ! pTaskInfo - > paramSet ) {
pTaskInfo - > paramSet = true ;
2024-07-24 09:08:08 +00:00
code = pTaskInfo - > pRoot - > fpSet . getNextExtFn ( pTaskInfo - > pRoot , pTaskInfo - > pOpParam , & pRes ) ;
2023-07-05 11:24:06 +00:00
} else {
2024-08-27 09:04:44 +00:00
code = pTaskInfo - > pRoot - > fpSet . getNextFn ( pTaskInfo - > pRoot , & pRes ) ;
2023-07-05 11:24:06 +00:00
}
2024-01-14 05:44:40 +00:00
2024-10-23 07:37:49 +00:00
QUERY_CHECK_CODE ( code , lino , _end ) ;
code = blockDataCheck ( pRes ) ;
2024-08-27 09:04:44 +00:00
QUERY_CHECK_CODE ( code , lino , _end ) ;
2024-07-22 04:51:25 +00:00
if ( pRes = = NULL ) {
2024-01-14 05:44:40 +00:00
st = taosGetTimestampUs ( ) ;
}
2024-07-22 04:51:25 +00:00
2023-08-10 03:09:21 +00:00
int32_t rowsThreshold = pTaskInfo - > pSubplan - > rowsThreshold ;
if ( ! pTaskInfo - > pSubplan - > dynamicRowThreshold | | 4096 < = pTaskInfo - > pSubplan - > rowsThreshold ) {
rowsThreshold = 4096 ;
}
2024-07-24 09:08:08 +00:00
2022-11-12 08:03:47 +00:00
int32_t blockIndex = 0 ;
2023-07-05 11:24:06 +00:00
while ( pRes ! = NULL ) {
2022-11-12 08:03:47 +00:00
SSDataBlock * p = NULL ;
if ( blockIndex > = taosArrayGetSize ( pTaskInfo - > pResultBlockList ) ) {
2024-07-27 10:55:34 +00:00
SSDataBlock * p1 = NULL ;
code = createOneDataBlock ( pRes , true , & p1 ) ;
2024-07-28 06:29:56 +00:00
QUERY_CHECK_CODE ( code , lino , _end ) ;
2024-07-27 10:55:34 +00:00
void * tmp = taosArrayPush ( pTaskInfo - > pResultBlockList , & p1 ) ;
2024-12-11 11:18:50 +00:00
QUERY_CHECK_NULL ( tmp , code , lino , _end , terrno ) ;
2022-11-12 08:03:47 +00:00
p = p1 ;
2026-02-02 09:27:02 +00:00
} else if ( processOneBlock ) {
SSDataBlock * * tmp = taosArrayGet ( pTaskInfo - > pResultBlockList , blockIndex ) ;
if ( tmp ) {
blockDataDestroy ( * tmp ) ;
* tmp = NULL ;
}
SSDataBlock * p1 = NULL ;
code = createOneDataBlock ( pRes , true , & p1 ) ;
QUERY_CHECK_CODE ( code , lino , _end ) ;
* tmp = p1 ;
p = p1 ;
2022-11-12 08:03:47 +00:00
} else {
2024-08-05 08:09:01 +00:00
void * tmp = taosArrayGet ( pTaskInfo - > pResultBlockList , blockIndex ) ;
QUERY_CHECK_NULL ( tmp , code , lino , _end , terrno ) ;
2024-08-27 09:04:44 +00:00
2024-08-05 08:09:01 +00:00
p = * ( SSDataBlock * * ) tmp ;
2024-07-22 04:51:25 +00:00
code = copyDataBlock ( p , pRes ) ;
QUERY_CHECK_CODE ( code , lino , _end ) ;
2022-11-12 08:03:47 +00:00
}
blockIndex + = 1 ;
2022-08-09 05:45:44 +00:00
current + = p - > info . rows ;
2026-03-12 01:11:00 +00:00
QUERY_CHECK_CONDITION ( ( p - > info . rows > 0 ) , code , lino , _end ,
2024-08-20 07:02:09 +00:00
TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR ) ;
2024-07-22 04:51:25 +00:00
void * tmp = taosArrayPush ( pResList , & p ) ;
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL ( tmp , code , lino , _end , terrno ) ;
2022-08-09 05:45:44 +00:00
2025-03-15 06:10:46 +00:00
if ( current > = rowsThreshold | | processOneBlock ) {
2022-08-09 05:45:44 +00:00
break ;
}
2023-07-05 11:24:06 +00:00
2024-08-27 09:04:44 +00:00
code = pTaskInfo - > pRoot - > fpSet . getNextFn ( pTaskInfo - > pRoot , & pRes ) ;
2024-10-23 07:37:49 +00:00
QUERY_CHECK_CODE ( code , lino , _end ) ;
code = blockDataCheck ( pRes ) ;
2024-08-27 09:04:44 +00:00
QUERY_CHECK_CODE ( code , lino , _end ) ;
2022-08-09 05:45:44 +00:00
}
2024-08-27 09:04:44 +00:00
2023-08-10 03:09:21 +00:00
if ( pTaskInfo - > pSubplan - > dynamicRowThreshold ) {
pTaskInfo - > pSubplan - > rowsThreshold - = current ;
}
2022-08-09 05:45:44 +00:00
2022-09-27 02:52:55 +00:00
* hasMore = ( pRes ! = NULL ) ;
2022-07-22 06:38:28 +00:00
uint64_t el = ( taosGetTimestampUs ( ) - st ) ;
pTaskInfo - > cost . elapsedTime + = el ;
2022-08-09 05:45:44 +00:00
if ( NULL = = pRes ) {
2022-07-22 06:38:28 +00:00
* useconds = pTaskInfo - > cost . elapsedTime ;
}
2024-07-22 04:51:25 +00:00
_end :
2024-07-22 05:15:24 +00:00
( void ) cleanUpUdfs ( ) ;
2022-07-22 06:38:28 +00:00
2022-09-15 07:02:23 +00:00
uint64_t total = pTaskInfo - > pRoot - > resultInfo . totalRows ;
2022-08-09 05:45:44 +00:00
qDebug ( " %s task suspended, %d rows in %d blocks returned, total:% " PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms " ,
2022-08-19 03:10:56 +00:00
GET_TASKID ( pTaskInfo ) , current , ( int32_t ) taosArrayGetSize ( pResList ) , total , 0 , el / 1000.0 ) ;
2022-07-22 06:38:28 +00:00
atomic_store_64 ( & pTaskInfo - > owner , 0 ) ;
2024-08-27 09:04:44 +00:00
if ( code ) {
pTaskInfo - > code = code ;
qError ( " %s failed at line %d since %s " , __func__ , lino , tstrerror ( code ) ) ;
}
2022-07-22 06:38:28 +00:00
return pTaskInfo - > code ;
}
2022-11-27 06:57:44 +00:00
void qCleanExecTaskBlockBuf ( qTaskInfo_t tinfo ) {
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
2022-12-20 02:15:14 +00:00
SArray * pList = pTaskInfo - > pResultBlockList ;
size_t num = taosArrayGetSize ( pList ) ;
for ( int32_t i = 0 ; i < num ; + + i ) {
2022-11-27 06:57:44 +00:00
SSDataBlock * * p = taosArrayGet ( pTaskInfo - > pResultBlockList , i ) ;
2024-08-05 08:09:01 +00:00
if ( p ) {
blockDataDestroy ( * p ) ;
}
2022-11-27 06:57:44 +00:00
}
taosArrayClear ( pTaskInfo - > pResultBlockList ) ;
}
2022-07-22 06:38:28 +00:00
int32_t qExecTask ( qTaskInfo_t tinfo , SSDataBlock * * pRes , uint64_t * useconds ) {
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
int64_t threadId = taosGetSelfPthreadId ( ) ;
2024-08-27 09:04:44 +00:00
int64_t curOwner = 0 ;
2022-07-22 06:38:28 +00:00
* pRes = NULL ;
2023-08-01 02:16:14 +00:00
// todo extract method
taosRLockLatch ( & pTaskInfo - > lock ) ;
bool isKilled = isTaskKilled ( pTaskInfo ) ;
if ( isKilled ) {
qDebug ( " %s already killed, abort " , GET_TASKID ( pTaskInfo ) ) ;
taosRUnLockLatch ( & pTaskInfo - > lock ) ;
2024-08-19 01:50:38 +00:00
return pTaskInfo - > code ;
2023-08-01 02:16:14 +00:00
}
if ( pTaskInfo - > owner ! = 0 ) {
2022-07-22 06:38:28 +00:00
qError ( " %s-%p execTask is now executed by thread:%p " , GET_TASKID ( pTaskInfo ) , pTaskInfo , ( void * ) curOwner ) ;
pTaskInfo - > code = TSDB_CODE_QRY_IN_EXEC ;
2023-08-01 02:16:14 +00:00
taosRUnLockLatch ( & pTaskInfo - > lock ) ;
2022-07-22 06:38:28 +00:00
return pTaskInfo - > code ;
}
2023-08-01 02:16:14 +00:00
pTaskInfo - > owner = threadId ;
taosRUnLockLatch ( & pTaskInfo - > lock ) ;
2022-07-22 06:38:28 +00:00
if ( pTaskInfo - > cost . start = = 0 ) {
2022-12-02 15:10:03 +00:00
pTaskInfo - > cost . start = taosGetTimestampUs ( ) ;
2022-07-22 06:38:28 +00:00
}
// error occurs, record the error code and return to client
int32_t ret = setjmp ( pTaskInfo - > env ) ;
if ( ret ! = TSDB_CODE_SUCCESS ) {
pTaskInfo - > code = ret ;
2024-07-22 04:51:25 +00:00
( void ) cleanUpUdfs ( ) ;
2022-07-22 06:38:28 +00:00
qDebug ( " %s task abort due to error/cancel occurs, code:%s " , GET_TASKID ( pTaskInfo ) , tstrerror ( pTaskInfo - > code ) ) ;
atomic_store_64 ( & pTaskInfo - > owner , 0 ) ;
return pTaskInfo - > code ;
}
qDebug ( " %s execTask is launched " , GET_TASKID ( pTaskInfo ) ) ;
int64_t st = taosGetTimestampUs ( ) ;
2025-07-08 06:30:18 +00:00
int32_t code = TSDB_CODE_SUCCESS ;
if ( pTaskInfo - > pOpParam & & ! pTaskInfo - > paramSet ) {
pTaskInfo - > paramSet = true ;
code = pTaskInfo - > pRoot - > fpSet . getNextExtFn ( pTaskInfo - > pRoot , pTaskInfo - > pOpParam , pRes ) ;
} else {
code = pTaskInfo - > pRoot - > fpSet . getNextFn ( pTaskInfo - > pRoot , pRes ) ;
}
2024-08-27 09:04:44 +00:00
if ( code ) {
pTaskInfo - > code = code ;
2024-09-24 10:19:47 +00:00
qError ( " %s failed at line %d, code:%s %s " , __func__ , __LINE__ , tstrerror ( code ) , GET_TASKID ( pTaskInfo ) ) ;
2024-08-27 09:04:44 +00:00
}
2024-10-23 07:37:49 +00:00
code = blockDataCheck ( * pRes ) ;
if ( code ) {
pTaskInfo - > code = code ;
qError ( " %s failed at line %d, code:%s %s " , __func__ , __LINE__ , tstrerror ( code ) , GET_TASKID ( pTaskInfo ) ) ;
}
2024-09-10 08:56:36 +00:00
2022-07-22 06:38:28 +00:00
uint64_t el = ( taosGetTimestampUs ( ) - st ) ;
pTaskInfo - > cost . elapsedTime + = el ;
if ( NULL = = * pRes ) {
* useconds = pTaskInfo - > cost . elapsedTime ;
}
2025-05-07 11:20:45 +00:00
( void ) cleanUpUdfs ( ) ;
2022-07-22 06:38:28 +00:00
int32_t current = ( * pRes ! = NULL ) ? ( * pRes ) - > info . rows : 0 ;
uint64_t total = pTaskInfo - > pRoot - > resultInfo . totalRows ;
qDebug ( " %s task suspended, %d rows returned, total:% " PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms " ,
GET_TASKID ( pTaskInfo ) , current , total , 0 , el / 1000.0 ) ;
atomic_store_64 ( & pTaskInfo - > owner , 0 ) ;
return pTaskInfo - > code ;
}
2022-11-15 03:59:29 +00:00
int32_t qAppendTaskStopInfo ( SExecTaskInfo * pTaskInfo , SExchangeOpStopInfo * pInfo ) {
2022-11-11 09:13:55 +00:00
taosWLockLatch ( & pTaskInfo - > stopInfo . lock ) ;
2024-07-22 04:51:25 +00:00
void * tmp = taosArrayPush ( pTaskInfo - > stopInfo . pStopInfo , pInfo ) ;
2022-11-11 09:13:55 +00:00
taosWUnLockLatch ( & pTaskInfo - > stopInfo . lock ) ;
2024-07-22 05:55:00 +00:00
if ( ! tmp ) {
2024-12-11 11:18:50 +00:00
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( terrno ) ) ;
2024-09-20 05:23:44 +00:00
return terrno ;
2024-07-22 04:51:25 +00:00
}
2022-11-11 09:13:55 +00:00
return TSDB_CODE_SUCCESS ;
}
int32_t stopInfoComp ( void const * lp , void const * rp ) {
SExchangeOpStopInfo * key = ( SExchangeOpStopInfo * ) lp ;
SExchangeOpStopInfo * pInfo = ( SExchangeOpStopInfo * ) rp ;
if ( key - > refId < pInfo - > refId ) {
return - 1 ;
} else if ( key - > refId > pInfo - > refId ) {
return 1 ;
}
return 0 ;
}
2022-11-15 03:59:29 +00:00
void qRemoveTaskStopInfo ( SExecTaskInfo * pTaskInfo , SExchangeOpStopInfo * pInfo ) {
2022-11-11 09:13:55 +00:00
taosWLockLatch ( & pTaskInfo - > stopInfo . lock ) ;
int32_t idx = taosArraySearchIdx ( pTaskInfo - > stopInfo . pStopInfo , pInfo , stopInfoComp , TD_EQ ) ;
if ( idx > = 0 ) {
taosArrayRemove ( pTaskInfo - > stopInfo . pStopInfo , idx ) ;
}
taosWUnLockLatch ( & pTaskInfo - > stopInfo . lock ) ;
}
void qStopTaskOperators ( SExecTaskInfo * pTaskInfo ) {
2026-03-06 06:45:30 +00:00
if ( pTaskInfo - > pSubJobCtx ) {
pTaskInfo - > pSubJobCtx - > code = pTaskInfo - > code ;
int32_t code = tsem_post ( & pTaskInfo - > pSubJobCtx - > ready ) ;
2025-12-22 03:35:33 +00:00
}
2022-11-11 09:13:55 +00:00
taosWLockLatch ( & pTaskInfo - > stopInfo . lock ) ;
int32_t num = taosArrayGetSize ( pTaskInfo - > stopInfo . pStopInfo ) ;
for ( int32_t i = 0 ; i < num ; + + i ) {
2022-11-15 03:59:29 +00:00
SExchangeOpStopInfo * pStop = taosArrayGet ( pTaskInfo - > stopInfo . pStopInfo , i ) ;
2024-08-05 08:09:01 +00:00
if ( ! pStop ) {
2024-08-05 10:18:13 +00:00
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( terrno ) ) ;
2024-08-05 08:09:01 +00:00
continue ;
}
2026-03-06 06:45:30 +00:00
SExchangeInfo * pExchangeInfo = taosAcquireRef ( fetchObjRefPool , pStop - > refId ) ;
2022-11-11 09:13:55 +00:00
if ( pExchangeInfo ) {
2024-09-04 10:37:41 +00:00
int32_t code = tsem_post ( & pExchangeInfo - > ready ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( code ) ) ;
2025-07-18 00:06:26 +00:00
} else {
qDebug ( " post to exchange % " PRId64 " to stop " , pStop - > refId ) ;
2024-09-04 10:37:41 +00:00
}
2026-03-06 06:45:30 +00:00
code = taosReleaseRef ( fetchObjRefPool , pStop - > refId ) ;
2024-09-04 10:37:41 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( code ) ) ;
}
2022-11-11 09:13:55 +00:00
}
}
taosWUnLockLatch ( & pTaskInfo - > stopInfo . lock ) ;
}
2022-11-29 08:17:05 +00:00
int32_t qAsyncKillTask ( qTaskInfo_t qinfo , int32_t rspCode ) {
2022-07-22 06:38:28 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) qinfo ;
if ( pTaskInfo = = NULL ) {
return TSDB_CODE_QRY_INVALID_QHANDLE ;
}
qDebug ( " %s execTask async killed " , GET_TASKID ( pTaskInfo ) ) ;
2022-11-15 03:59:29 +00:00
2022-11-29 08:17:05 +00:00
setTaskKilled ( pTaskInfo , rspCode ) ;
2022-11-11 09:13:55 +00:00
qStopTaskOperators ( pTaskInfo ) ;
2022-11-15 03:59:29 +00:00
2022-07-22 06:38:28 +00:00
return TSDB_CODE_SUCCESS ;
}
2025-02-17 02:06:09 +00:00
int32_t qKillTask ( qTaskInfo_t tinfo , int32_t rspCode , int64_t waitDuration ) {
2025-02-13 15:41:06 +00:00
int64_t st = taosGetTimestampMs ( ) ;
2023-03-28 01:53:49 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
if ( pTaskInfo = = NULL ) {
return TSDB_CODE_QRY_INVALID_QHANDLE ;
}
2025-02-17 02:06:09 +00:00
if ( waitDuration > 0 ) {
2025-05-07 11:20:45 +00:00
qDebug ( " %s sync killed execTask, and waiting for at most %.2fs " , GET_TASKID ( pTaskInfo ) , waitDuration / 1000.0 ) ;
2025-02-17 02:06:09 +00:00
} else {
qDebug ( " %s async killed execTask " , GET_TASKID ( pTaskInfo ) ) ;
}
2023-03-31 02:35:13 +00:00
setTaskKilled ( pTaskInfo , TSDB_CODE_TSC_QUERY_KILLED ) ;
2023-03-28 01:53:49 +00:00
2025-02-17 02:06:09 +00:00
if ( waitDuration > 0 ) {
while ( 1 ) {
taosWLockLatch ( & pTaskInfo - > lock ) ;
if ( qTaskIsExecuting ( pTaskInfo ) ) { // let's wait for 100 ms and try again
taosWUnLockLatch ( & pTaskInfo - > lock ) ;
taosMsleep ( 200 ) ;
int64_t d = taosGetTimestampMs ( ) - st ;
if ( d > = waitDuration & & waitDuration > = 0 ) {
qWarn ( " %s waiting more than %.2fs, not wait anymore " , GET_TASKID ( pTaskInfo ) , waitDuration / 1000.0 ) ;
return TSDB_CODE_SUCCESS ;
}
} else { // not running now
pTaskInfo - > code = rspCode ;
taosWUnLockLatch ( & pTaskInfo - > lock ) ;
return TSDB_CODE_SUCCESS ;
}
2024-05-14 10:05:13 +00:00
}
2023-03-28 01:53:49 +00:00
}
2025-02-17 02:06:09 +00:00
2025-03-17 02:20:17 +00:00
int64_t et = taosGetTimestampMs ( ) - st ;
if ( et < waitDuration ) {
qInfo ( " %s waiting %.2fs for executor stopping " , GET_TASKID ( pTaskInfo ) , et / 1000.0 ) ;
return TSDB_CODE_SUCCESS ;
}
2025-02-17 02:06:09 +00:00
return TSDB_CODE_SUCCESS ;
2023-03-28 01:53:49 +00:00
}
2023-01-03 10:07:18 +00:00
bool qTaskIsExecuting ( qTaskInfo_t qinfo ) {
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) qinfo ;
if ( NULL = = pTaskInfo ) {
return false ;
}
return 0 ! = atomic_load_64 ( & pTaskInfo - > owner ) ;
}
2022-11-30 13:04:58 +00:00
static void printTaskExecCostInLog ( SExecTaskInfo * pTaskInfo ) {
STaskCostInfo * pSummary = & pTaskInfo - > cost ;
2022-12-02 15:10:03 +00:00
int64_t idleTime = pSummary - > start - pSummary - > created ;
2022-11-30 13:04:58 +00:00
SFileBlockLoadRecorder * pRecorder = pSummary - > pRecoder ;
if ( pSummary - > pRecoder ! = NULL ) {
qDebug (
2022-12-02 15:38:05 +00:00
" %s :cost summary: idle:%.2f ms, elapsed time:%.2f ms, extract tableList:%.2f ms, "
2026-03-20 02:08:49 +00:00
" createGroupIdMap:%.2f ms, total blocks:% " PRId64
" ,load block SMA:% " PRId64 " , load data block:% " PRId64 " , check rows:% " PRId64 ,
GET_TASKID ( pTaskInfo ) , idleTime / 1000.0 , pSummary - > elapsedTime / 1000.0 ,
pSummary - > extractListTime , pSummary - > groupIdMapTime , pRecorder - > totalBlocks ,
pRecorder - > smaLoadBlocks , pRecorder - > fileLoadBlocks , pRecorder - > checkRows ) ;
2022-12-02 15:10:03 +00:00
} else {
qDebug ( " %s :cost summary: idle in queue:%.2f ms, elapsed time:%.2f ms " , GET_TASKID ( pTaskInfo ) , idleTime / 1000.0 ,
pSummary - > elapsedTime / 1000.0 ) ;
2022-11-30 13:04:58 +00:00
}
}
2025-12-22 03:35:33 +00:00
2022-07-22 06:38:28 +00:00
void qDestroyTask ( qTaskInfo_t qTaskHandle ) {
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) qTaskHandle ;
if ( pTaskInfo = = NULL ) {
return ;
}
2023-04-28 08:29:09 +00:00
if ( pTaskInfo - > pRoot ! = NULL ) {
qDebug ( " %s execTask completed, numOfRows:% " PRId64 , GET_TASKID ( pTaskInfo ) , pTaskInfo - > pRoot - > resultInfo . totalRows ) ;
} else {
qDebug ( " %s execTask completed " , GET_TASKID ( pTaskInfo ) ) ;
}
2022-07-22 06:38:28 +00:00
2022-11-27 16:51:18 +00:00
printTaskExecCostInLog ( pTaskInfo ) ; // print the query cost summary
2022-07-22 06:38:28 +00:00
doDestroyTask ( pTaskInfo ) ;
}
2022-07-30 03:30:31 +00:00
int32_t qGetExplainExecInfo ( qTaskInfo_t tinfo , SArray * pExecInfoList ) {
2022-07-22 06:38:28 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
2022-07-30 03:30:31 +00:00
return getOperatorExplainExecInfo ( pTaskInfo - > pRoot , pExecInfoList ) ;
2022-07-22 06:38:28 +00:00
}
2025-07-10 06:20:35 +00:00
void qExtractTmqScanner ( qTaskInfo_t tinfo , void * * scanner ) {
2022-07-22 06:38:28 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
SOperatorInfo * pOperator = pTaskInfo - > pRoot ;
while ( 1 ) {
2022-08-19 03:10:56 +00:00
uint16_t type = pOperator - > operatorType ;
2022-07-22 06:38:28 +00:00
if ( type = = QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN ) {
* scanner = pOperator - > info ;
2024-07-24 09:56:29 +00:00
break ;
2022-07-22 06:38:28 +00:00
} else {
pOperator = pOperator - > pDownstream [ 0 ] ;
}
}
}
2025-07-10 06:20:35 +00:00
void * qExtractReaderFromTmqScanner ( void * scanner ) {
2026-03-12 01:11:00 +00:00
STmqQueryScanInfo * pInfo = scanner ;
2022-07-22 06:38:28 +00:00
return ( void * ) pInfo - > tqReader ;
}
2022-08-09 11:06:24 +00:00
const SSchemaWrapper * qExtractSchemaFromTask ( qTaskInfo_t tinfo ) {
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
2026-03-12 01:11:00 +00:00
return pTaskInfo - > tmqInfo . schema ;
2022-08-09 11:06:24 +00:00
}
const char * qExtractTbnameFromTask ( qTaskInfo_t tinfo ) {
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
2026-03-12 01:11:00 +00:00
return pTaskInfo - > tmqInfo . tbName ;
2022-07-22 06:38:28 +00:00
}
2024-05-23 09:35:54 +00:00
SMqBatchMetaRsp * qStreamExtractMetaMsg ( qTaskInfo_t tinfo ) {
2022-07-22 06:38:28 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
2026-03-12 01:11:00 +00:00
return & pTaskInfo - > tmqInfo . btMetaRsp ;
2022-07-22 06:38:28 +00:00
}
2024-07-29 02:29:40 +00:00
int32_t qStreamExtractOffset ( qTaskInfo_t tinfo , STqOffsetVal * pOffset ) {
2022-08-09 11:06:24 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
2026-03-12 01:11:00 +00:00
tOffsetCopy ( pOffset , & pTaskInfo - > tmqInfo . currentOffset ) ;
2024-07-29 10:16:04 +00:00
return 0 ;
2022-07-22 06:38:28 +00:00
}
2022-10-17 06:05:40 +00:00
int32_t initQueryTableDataCondForTmq ( SQueryTableDataCond * pCond , SSnapContext * sContext , SMetaTableInfo * pMtInfo ) {
2022-08-04 07:01:59 +00:00
memset ( pCond , 0 , sizeof ( SQueryTableDataCond ) ) ;
pCond - > order = TSDB_ORDER_ASC ;
2022-10-17 06:05:40 +00:00
pCond - > numOfCols = pMtInfo - > schema - > nCols ;
2022-08-04 07:01:59 +00:00
pCond - > colList = taosMemoryCalloc ( pCond - > numOfCols , sizeof ( SColumnInfo ) ) ;
2022-12-02 07:52:32 +00:00
pCond - > pSlotList = taosMemoryMalloc ( sizeof ( int32_t ) * pCond - > numOfCols ) ;
if ( pCond - > colList = = NULL | | pCond - > pSlotList = = NULL ) {
taosMemoryFreeClear ( pCond - > colList ) ;
taosMemoryFreeClear ( pCond - > pSlotList ) ;
2022-08-04 07:01:59 +00:00
return terrno ;
}
2025-03-14 05:32:13 +00:00
TAOS_SET_OBJ_ALIGNED ( & pCond - > twindows , TSWINDOW_INITIALIZER ) ;
2022-10-17 06:05:40 +00:00
pCond - > suid = pMtInfo - > suid ;
2022-08-04 07:01:59 +00:00
pCond - > type = TIMEWINDOW_RANGE_CONTAINED ;
pCond - > startVersion = - 1 ;
pCond - > endVersion = sContext - > snapVersion ;
for ( int32_t i = 0 ; i < pCond - > numOfCols ; + + i ) {
2022-12-02 07:52:32 +00:00
SColumnInfo * pColInfo = & pCond - > colList [ i ] ;
pColInfo - > type = pMtInfo - > schema - > pSchema [ i ] . type ;
pColInfo - > bytes = pMtInfo - > schema - > pSchema [ i ] . bytes ;
2025-10-10 01:37:05 +00:00
if ( pMtInfo - > pExtSchemas ! = NULL ) {
decimalFromTypeMod ( pMtInfo - > pExtSchemas [ i ] . typeMod , & pColInfo - > precision , & pColInfo - > scale ) ;
}
2022-12-02 07:52:32 +00:00
pColInfo - > colId = pMtInfo - > schema - > pSchema [ i ] . colId ;
2024-04-01 08:15:34 +00:00
pColInfo - > pk = pMtInfo - > schema - > pSchema [ i ] . flags & COL_IS_KEY ;
2022-12-02 07:52:32 +00:00
pCond - > pSlotList [ i ] = i ;
2022-08-04 07:01:59 +00:00
}
return TSDB_CODE_SUCCESS ;
}
2023-04-03 11:54:52 +00:00
void qStreamSetOpen ( qTaskInfo_t tinfo ) {
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
2023-04-11 07:56:28 +00:00
SOperatorInfo * pOperator = pTaskInfo - > pRoot ;
2023-04-03 11:54:52 +00:00
pOperator - > status = OP_NOT_OPENED ;
}
2026-02-26 09:06:07 +00:00
void qStreamSetParams ( qTaskInfo_t tinfo , int8_t sourceExcluded , int32_t minPollRows , int64_t timeout , int8_t enableReplay ) {
2024-02-05 09:07:50 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
2026-03-12 01:11:00 +00:00
pTaskInfo - > tmqInfo . sourceExcluded = sourceExcluded ;
pTaskInfo - > tmqInfo . minPollRows = minPollRows ;
pTaskInfo - > tmqInfo . timeout = timeout ;
pTaskInfo - > tmqInfo . enableReplay = enableReplay ;
2024-02-05 09:07:50 +00:00
}
2022-08-04 07:01:59 +00:00
int32_t qStreamPrepareScan ( qTaskInfo_t tinfo , STqOffsetVal * pOffset , int8_t subType ) {
2024-07-22 04:51:25 +00:00
int32_t code = TSDB_CODE_SUCCESS ;
2022-07-22 06:38:28 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
2023-06-19 12:48:49 +00:00
SStorageAPI * pAPI = & pTaskInfo - > storageAPI ;
2023-05-23 10:29:23 +00:00
2022-07-22 06:38:28 +00:00
SOperatorInfo * pOperator = pTaskInfo - > pRoot ;
2023-04-04 06:50:58 +00:00
const char * id = GET_TASKID ( pTaskInfo ) ;
2023-03-29 08:36:35 +00:00
2024-07-22 04:51:25 +00:00
if ( subType = = TOPIC_SUB_TYPE__COLUMN & & pOffset - > type = = TMQ_OFFSET__LOG ) {
2024-07-24 09:08:08 +00:00
code = extractOperatorInTree ( pOperator , QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN , id , & pOperator ) ;
if ( pOperator = = NULL | | code ! = 0 ) {
return code ;
2023-06-28 03:54:48 +00:00
}
2024-07-24 09:08:08 +00:00
2026-03-12 01:11:00 +00:00
STmqQueryScanInfo * pInfo = pOperator - > info ;
2024-07-22 04:51:25 +00:00
SStoreTqReader * pReaderAPI = & pTaskInfo - > storageAPI . tqReaderFn ;
SWalReader * pWalReader = pReaderAPI - > tqReaderGetWalReader ( pInfo - > tqReader ) ;
2023-06-28 03:54:48 +00:00
walReaderVerifyOffset ( pWalReader , pOffset ) ;
}
2023-03-29 09:27:04 +00:00
// if pOffset equal to current offset, means continue consume
2026-03-12 01:11:00 +00:00
if ( tOffsetEqual ( pOffset , & pTaskInfo - > tmqInfo . currentOffset ) ) {
2022-08-04 07:01:59 +00:00
return 0 ;
}
2023-02-26 04:16:45 +00:00
2022-08-04 07:01:59 +00:00
if ( subType = = TOPIC_SUB_TYPE__COLUMN ) {
2024-07-29 02:29:40 +00:00
code = extractOperatorInTree ( pOperator , QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN , id , & pOperator ) ;
2024-07-24 09:08:08 +00:00
if ( pOperator = = NULL | | code ! = 0 ) {
return code ;
2022-08-04 07:01:59 +00:00
}
2023-05-04 08:15:14 +00:00
2026-03-12 01:11:00 +00:00
STmqQueryScanInfo * pInfo = pOperator - > info ;
2023-03-29 08:36:35 +00:00
STableScanInfo * pScanInfo = pInfo - > pTableScanOp - > info ;
STableScanBase * pScanBaseInfo = & pScanInfo - > base ;
2023-04-06 06:40:01 +00:00
STableListInfo * pTableListInfo = pScanBaseInfo - > pTableListInfo ;
2023-03-29 08:36:35 +00:00
2022-08-04 07:01:59 +00:00
if ( pOffset - > type = = TMQ_OFFSET__LOG ) {
2023-05-23 10:58:54 +00:00
pTaskInfo - > storageAPI . tsdReader . tsdReaderClose ( pScanBaseInfo - > dataReader ) ;
2023-03-29 08:36:35 +00:00
pScanBaseInfo - > dataReader = NULL ;
2023-05-25 09:51:03 +00:00
SStoreTqReader * pReaderAPI = & pTaskInfo - > storageAPI . tqReaderFn ;
2023-06-19 12:48:49 +00:00
SWalReader * pWalReader = pReaderAPI - > tqReaderGetWalReader ( pInfo - > tqReader ) ;
2023-05-25 09:51:03 +00:00
walReaderVerifyOffset ( pWalReader , pOffset ) ;
2024-07-24 09:56:29 +00:00
code = pReaderAPI - > tqReaderSeek ( pInfo - > tqReader , pOffset - > version , id ) ;
if ( code < 0 ) {
2023-07-14 08:43:28 +00:00
qError ( " tqReaderSeek failed ver:% " PRId64 " , %s " , pOffset - > version , id ) ;
2024-07-24 09:56:29 +00:00
return code ;
2023-05-25 09:51:03 +00:00
}
2022-08-04 07:01:59 +00:00
} else if ( pOffset - > type = = TMQ_OFFSET__SNAPSHOT_DATA ) {
2023-02-26 04:35:50 +00:00
// iterate all tables from tableInfoList, and retrieve rows from each table one-by-one
// those data are from the snapshot in tsdb, besides the data in the wal file.
2022-08-04 07:01:59 +00:00
int64_t uid = pOffset - > uid ;
int64_t ts = pOffset - > ts ;
2023-03-29 11:40:34 +00:00
int32_t index = 0 ;
2022-08-04 07:01:59 +00:00
2023-03-29 08:36:35 +00:00
// this value may be changed if new tables are created
taosRLockLatch ( & pTaskInfo - > lock ) ;
2024-08-23 03:01:45 +00:00
int32_t numOfTables = 0 ;
code = tableListGetSize ( pTableListInfo , & numOfTables ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( code ) ) ;
taosRUnLockLatch ( & pTaskInfo - > lock ) ;
return code ;
}
2023-03-29 08:36:35 +00:00
2022-08-04 07:01:59 +00:00
if ( uid = = 0 ) {
2023-03-29 08:36:35 +00:00
if ( numOfTables ! = 0 ) {
2024-07-24 09:56:29 +00:00
STableKeyInfo * tmp = tableListGetInfo ( pTableListInfo , 0 ) ;
2024-08-06 01:24:31 +00:00
if ( ! tmp ) {
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( terrno ) ) ;
taosRUnLockLatch ( & pTaskInfo - > lock ) ;
return terrno ;
}
2024-07-24 09:56:29 +00:00
if ( tmp ) uid = tmp - > uid ;
2022-08-04 07:01:59 +00:00
ts = INT64_MIN ;
2024-01-16 01:28:45 +00:00
pScanInfo - > currentTable = 0 ;
2022-08-04 07:01:59 +00:00
} else {
2023-03-29 08:36:35 +00:00
taosRUnLockLatch ( & pTaskInfo - > lock ) ;
qError ( " no table in table list, %s " , id ) ;
2024-07-24 09:56:29 +00:00
return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED ;
2022-07-27 02:52:25 +00:00
}
2022-08-04 07:01:59 +00:00
}
2024-03-22 09:48:59 +00:00
pTaskInfo - > storageAPI . tqReaderFn . tqSetTablePrimaryKey ( pInfo - > tqReader , uid ) ;
2022-07-22 13:16:52 +00:00
2023-04-11 07:56:28 +00:00
qDebug ( " switch to table uid:% " PRId64 " ts:% " PRId64 " % " PRId64 " rows returned " , uid , ts ,
pInfo - > pTableScanOp - > resultInfo . totalRows ) ;
2022-08-04 07:01:59 +00:00
pInfo - > pTableScanOp - > resultInfo . totalRows = 0 ;
2022-07-22 13:16:52 +00:00
2023-03-29 08:36:35 +00:00
// start from current accessed position
2024-01-16 01:28:45 +00:00
// we cannot start from the pScanInfo->currentTable, since the commit offset may cause the rollback of the start
2023-03-30 04:04:47 +00:00
// position, let's find it from the beginning.
index = tableListFind ( pTableListInfo , uid , 0 ) ;
2023-03-29 08:36:35 +00:00
taosRUnLockLatch ( & pTaskInfo - > lock ) ;
2022-07-22 06:38:28 +00:00
2023-03-29 08:36:35 +00:00
if ( index > = 0 ) {
2024-01-16 01:28:45 +00:00
pScanInfo - > currentTable = index ;
2023-03-29 08:36:35 +00:00
} else {
2023-03-30 04:04:47 +00:00
qError ( " vgId:%d uid:% " PRIu64 " not found in table list, total:%d, index:%d %s " , pTaskInfo - > id . vgId , uid ,
2024-01-16 01:28:45 +00:00
numOfTables , pScanInfo - > currentTable , id ) ;
2024-07-24 09:56:29 +00:00
return TSDB_CODE_TMQ_NO_TABLE_QUALIFIED ;
2023-03-11 08:12:11 +00:00
}
2022-07-22 06:38:28 +00:00
2023-03-29 08:36:35 +00:00
STableKeyInfo keyInfo = { . uid = uid } ;
2023-04-04 06:50:58 +00:00
int64_t oldSkey = pScanBaseInfo - > cond . twindows . skey ;
2023-03-29 15:45:41 +00:00
// let's start from the next ts that returned to consumer.
2024-07-22 04:51:25 +00:00
if ( pTaskInfo - > storageAPI . tqReaderFn . tqGetTablePrimaryKey ( pInfo - > tqReader ) ) {
2024-03-22 09:48:59 +00:00
pScanBaseInfo - > cond . twindows . skey = ts ;
2024-07-22 04:51:25 +00:00
} else {
2024-03-22 09:48:59 +00:00
pScanBaseInfo - > cond . twindows . skey = ts + 1 ;
}
2023-03-30 04:04:47 +00:00
pScanInfo - > scanTimes = 0 ;
2023-03-29 15:45:41 +00:00
2023-03-29 08:36:35 +00:00
if ( pScanBaseInfo - > dataReader = = NULL ) {
2024-07-23 16:08:39 +00:00
code = pTaskInfo - > storageAPI . tsdReader . tsdReaderOpen ( pScanBaseInfo - > readHandle . vnode , & pScanBaseInfo - > cond ,
& keyInfo , 1 , pScanInfo - > pResBlock ,
( void * * ) & pScanBaseInfo - > dataReader , id , NULL ) ;
2023-03-29 08:36:35 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " prepare read tsdb snapshot failed, uid:% " PRId64 " , code:%s %s " , pOffset - > uid , tstrerror ( code ) , id ) ;
2024-07-24 09:56:29 +00:00
return code ;
2022-07-27 02:52:25 +00:00
}
2023-03-29 15:45:41 +00:00
2023-03-30 04:04:47 +00:00
qDebug ( " tsdb reader created with offset(snapshot) uid:% " PRId64 " ts:% " PRId64 " table index:%d, total:%d, %s " ,
2024-01-16 01:28:45 +00:00
uid , pScanBaseInfo - > cond . twindows . skey , pScanInfo - > currentTable , numOfTables , id ) ;
2023-03-29 08:36:35 +00:00
} else {
2024-07-22 04:51:25 +00:00
code = pTaskInfo - > storageAPI . tsdReader . tsdSetQueryTableList ( pScanBaseInfo - > dataReader , & keyInfo , 1 ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( code ) ) ;
return code ;
}
code = pTaskInfo - > storageAPI . tsdReader . tsdReaderResetStatus ( pScanBaseInfo - > dataReader , & pScanBaseInfo - > cond ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( code ) ) ;
return code ;
}
2023-03-29 08:36:35 +00:00
qDebug ( " tsdb reader offset seek snapshot to uid:% " PRId64 " ts % " PRId64 " table index:%d numOfTable:%d, %s " ,
2024-01-16 01:28:45 +00:00
uid , pScanBaseInfo - > cond . twindows . skey , pScanInfo - > currentTable , numOfTables , id ) ;
2023-03-29 08:36:35 +00:00
}
2023-03-29 15:45:41 +00:00
// restore the key value
pScanBaseInfo - > cond . twindows . skey = oldSkey ;
2022-08-04 07:01:59 +00:00
} else {
2023-03-29 08:36:35 +00:00
qError ( " invalid pOffset->type:%d, %s " , pOffset - > type , id ) ;
2026-01-22 00:28:17 +00:00
return TSDB_CODE_STREAM_INTERNAL_ERROR ;
2022-08-04 07:01:59 +00:00
}
2023-03-29 11:40:34 +00:00
} else { // subType == TOPIC_SUB_TYPE__TABLE/TOPIC_SUB_TYPE__DB
2023-03-29 08:36:35 +00:00
if ( pOffset - > type = = TMQ_OFFSET__SNAPSHOT_DATA ) {
2026-03-12 01:11:00 +00:00
STmqRawScanInfo * pInfo = pOperator - > info ;
2023-03-29 08:36:35 +00:00
SSnapContext * sContext = pInfo - > sContext ;
2024-08-24 10:19:25 +00:00
SOperatorInfo * p = NULL ;
2024-07-24 09:08:08 +00:00
code = extractOperatorInTree ( pOperator , QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN , id , & p ) ;
if ( code ! = 0 ) {
return code ;
}
2023-04-03 03:57:17 +00:00
2026-03-12 01:11:00 +00:00
STableListInfo * pTableListInfo = ( ( STmqRawScanInfo * ) ( p - > info ) ) - > pTableListInfo ;
2023-04-03 06:49:14 +00:00
2023-07-14 08:43:28 +00:00
if ( pAPI - > snapshotFn . setForSnapShot ( sContext , pOffset - > uid ) ! = 0 ) {
2023-04-04 06:50:58 +00:00
qError ( " setDataForSnapShot error. uid:% " PRId64 " , %s " , pOffset - > uid , id ) ;
2026-01-22 00:28:17 +00:00
return TSDB_CODE_STREAM_INTERNAL_ERROR ;
2023-03-29 08:36:35 +00:00
}
2022-10-30 14:13:49 +00:00
2024-07-24 09:56:29 +00:00
SMetaTableInfo mtInfo = { 0 } ;
code = pTaskInfo - > storageAPI . snapshotFn . getMetaTableInfoFromSnapshot ( sContext , & mtInfo ) ;
2024-08-24 10:19:25 +00:00
if ( code ! = 0 ) {
2025-03-14 10:08:07 +00:00
destroyMetaTableInfo ( & mtInfo ) ;
2024-07-24 09:56:29 +00:00
return code ;
}
2023-05-23 10:58:54 +00:00
pTaskInfo - > storageAPI . tsdReader . tsdReaderClose ( pInfo - > dataReader ) ;
2023-03-29 08:36:35 +00:00
pInfo - > dataReader = NULL ;
2022-10-30 14:13:49 +00:00
2026-03-12 01:11:00 +00:00
cleanupQueryTableDataCond ( & pTaskInfo - > tmqInfo . tableCond ) ;
2023-03-29 08:36:35 +00:00
tableListClear ( pTableListInfo ) ;
2022-08-25 08:55:12 +00:00
2023-03-29 08:36:35 +00:00
if ( mtInfo . uid = = 0 ) {
2025-03-14 10:08:07 +00:00
destroyMetaTableInfo ( & mtInfo ) ;
2023-04-07 06:24:29 +00:00
goto end ; // no data
2023-03-29 08:36:35 +00:00
}
2022-10-28 11:56:32 +00:00
2024-03-22 09:48:59 +00:00
pAPI - > snapshotFn . taosXSetTablePrimaryKey ( sContext , mtInfo . uid ) ;
2026-03-12 01:11:00 +00:00
code = initQueryTableDataCondForTmq ( & pTaskInfo - > tmqInfo . tableCond , sContext , & mtInfo ) ;
2024-07-22 04:51:25 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( code ) ) ;
2025-03-14 10:08:07 +00:00
destroyMetaTableInfo ( & mtInfo ) ;
2024-07-22 04:51:25 +00:00
return code ;
}
if ( pAPI - > snapshotFn . taosXGetTablePrimaryKey ( sContext ) ) {
2026-03-12 01:11:00 +00:00
pTaskInfo - > tmqInfo . tableCond . twindows . skey = pOffset - > ts ;
2024-07-22 04:51:25 +00:00
} else {
2026-03-12 01:11:00 +00:00
pTaskInfo - > tmqInfo . tableCond . twindows . skey = pOffset - > ts + 1 ;
2024-03-22 09:48:59 +00:00
}
2022-10-31 03:53:35 +00:00
2024-07-22 04:51:25 +00:00
code = tableListAddTableInfo ( pTableListInfo , mtInfo . uid , 0 ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
2025-03-14 10:08:07 +00:00
destroyMetaTableInfo ( & mtInfo ) ;
2024-07-22 04:51:25 +00:00
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( code ) ) ;
return code ;
}
2022-10-28 11:56:32 +00:00
2023-03-29 08:36:35 +00:00
STableKeyInfo * pList = tableListGetInfo ( pTableListInfo , 0 ) ;
2024-08-06 01:24:31 +00:00
if ( ! pList ) {
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( code ) ) ;
2025-03-14 10:08:07 +00:00
destroyMetaTableInfo ( & mtInfo ) ;
2024-08-06 01:24:31 +00:00
return code ;
}
2024-08-23 03:01:45 +00:00
int32_t size = 0 ;
code = tableListGetSize ( pTableListInfo , & size ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( code ) ) ;
2025-03-14 10:08:07 +00:00
destroyMetaTableInfo ( & mtInfo ) ;
2024-08-23 03:01:45 +00:00
return code ;
2024-08-26 06:49:02 +00:00
}
2022-08-04 07:01:59 +00:00
2026-03-12 01:11:00 +00:00
code = pTaskInfo - > storageAPI . tsdReader . tsdReaderOpen ( pInfo - > vnode , & pTaskInfo - > tmqInfo . tableCond , pList , size ,
2024-07-22 04:51:25 +00:00
NULL , ( void * * ) & pInfo - > dataReader , NULL , NULL ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( code ) ) ;
2025-03-14 10:08:07 +00:00
destroyMetaTableInfo ( & mtInfo ) ;
2024-07-22 04:51:25 +00:00
return code ;
}
2022-08-31 07:27:41 +00:00
2026-03-12 01:11:00 +00:00
cleanupQueryTableDataCond ( & pTaskInfo - > tmqInfo . tableCond ) ;
tstrncpy ( pTaskInfo - > tmqInfo . tbName , mtInfo . tbName , TSDB_TABLE_NAME_LEN ) ;
// pTaskInfo->tmqInfo.suid = mtInfo.suid == 0 ? mtInfo.uid : mtInfo.suid;
tDeleteSchemaWrapper ( pTaskInfo - > tmqInfo . schema ) ;
pTaskInfo - > tmqInfo . schema = mtInfo . schema ;
2025-10-10 01:37:05 +00:00
taosMemoryFreeClear ( mtInfo . pExtSchemas ) ;
2023-03-29 08:36:35 +00:00
2023-04-04 06:50:58 +00:00
qDebug ( " tmqsnap qStreamPrepareScan snapshot data uid:% " PRId64 " ts % " PRId64 " %s " , mtInfo . uid , pOffset - > ts , id ) ;
2023-03-29 08:36:35 +00:00
} else if ( pOffset - > type = = TMQ_OFFSET__SNAPSHOT_META ) {
2026-03-12 01:11:00 +00:00
STmqRawScanInfo * pInfo = pOperator - > info ;
2023-03-29 08:36:35 +00:00
SSnapContext * sContext = pInfo - > sContext ;
2024-07-24 09:56:29 +00:00
code = pTaskInfo - > storageAPI . snapshotFn . setForSnapShot ( sContext , pOffset - > uid ) ;
if ( code ! = 0 ) {
2023-03-29 08:36:35 +00:00
qError ( " setForSnapShot error. uid:% " PRIu64 " ,version:% " PRId64 , pOffset - > uid , pOffset - > version ) ;
2024-07-24 09:56:29 +00:00
return code ;
2023-03-29 08:36:35 +00:00
}
2023-04-04 06:50:58 +00:00
qDebug ( " tmqsnap qStreamPrepareScan snapshot meta uid:% " PRId64 " ts % " PRId64 " %s " , pOffset - > uid , pOffset - > ts ,
id ) ;
2023-03-29 08:36:35 +00:00
} else if ( pOffset - > type = = TMQ_OFFSET__LOG ) {
2026-03-12 01:11:00 +00:00
STmqRawScanInfo * pInfo = pOperator - > info ;
2023-05-23 10:58:54 +00:00
pTaskInfo - > storageAPI . tsdReader . tsdReaderClose ( pInfo - > dataReader ) ;
2023-03-29 08:36:35 +00:00
pInfo - > dataReader = NULL ;
2023-03-29 11:40:34 +00:00
qDebug ( " tmqsnap qStreamPrepareScan snapshot log, %s " , id ) ;
2022-08-05 13:12:18 +00:00
}
2022-07-22 06:38:28 +00:00
}
2023-04-07 06:24:29 +00:00
end :
2026-03-12 01:11:00 +00:00
tOffsetCopy ( & pTaskInfo - > tmqInfo . currentOffset , pOffset ) ;
2022-07-22 06:38:28 +00:00
return 0 ;
}
2022-11-09 05:45:46 +00:00
void qProcessRspMsg ( void * parent , SRpcMsg * pMsg , SEpSet * pEpSet ) {
SMsgSendInfo * pSendInfo = ( SMsgSendInfo * ) pMsg - > info . ahandle ;
2023-04-04 06:50:58 +00:00
if ( pMsg - > info . ahandle = = NULL ) {
2025-12-12 01:29:33 +00:00
rpcFreeCont ( pMsg - > pCont ) ;
2026-01-22 05:39:05 +00:00
qError ( " rsp msg got while pMsg->info.ahandle is NULL, 0x% " PRIx64 " :0x% " PRIx64 , TRACE_GET_ROOTID ( & pMsg - > info . traceId ) , TRACE_GET_MSGID ( & pMsg - > info . traceId ) ) ;
2023-03-11 08:12:11 +00:00
return ;
}
2022-11-09 05:45:46 +00:00
2025-07-17 05:26:02 +00:00
qDebug ( " rsp msg got, code:%x, len:%d, 0x% " PRIx64 " :0x% " PRIx64 ,
pMsg - > code , pMsg - > contLen , TRACE_GET_ROOTID ( & pMsg - > info . traceId ) , TRACE_GET_MSGID ( & pMsg - > info . traceId ) ) ;
2022-11-09 05:45:46 +00:00
SDataBuf buf = { . len = pMsg - > contLen , . pData = NULL } ;
if ( pMsg - > contLen > 0 ) {
buf . pData = taosMemoryCalloc ( 1 , pMsg - > contLen ) ;
if ( buf . pData = = NULL ) {
2024-12-11 11:18:50 +00:00
pMsg - > code = terrno ;
2022-11-09 05:45:46 +00:00
} else {
memcpy ( buf . pData , pMsg - > pCont , pMsg - > contLen ) ;
}
}
2024-07-22 04:51:25 +00:00
( void ) pSendInfo - > fp ( pSendInfo - > param , & buf , pMsg - > code ) ;
2022-11-09 05:45:46 +00:00
rpcFreeCont ( pMsg - > pCont ) ;
destroySendMsgInfo ( pSendInfo ) ;
2022-11-15 03:59:29 +00:00
}
2023-01-04 07:14:00 +00:00
2023-04-08 17:39:09 +00:00
SArray * qGetQueriedTableListInfo ( qTaskInfo_t tinfo ) {
2024-07-22 04:51:25 +00:00
int32_t code = TSDB_CODE_SUCCESS ;
int32_t lino = 0 ;
2023-04-08 17:39:09 +00:00
SExecTaskInfo * pTaskInfo = tinfo ;
2024-07-23 09:31:41 +00:00
SArray * plist = NULL ;
code = getTableListInfo ( pTaskInfo , & plist ) ;
if ( code | | plist = = NULL ) {
return NULL ;
}
2023-04-08 17:39:09 +00:00
// only extract table in the first elements
STableListInfo * pTableListInfo = taosArrayGetP ( plist , 0 ) ;
SArray * pUidList = taosArrayInit ( 10 , sizeof ( uint64_t ) ) ;
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL ( pUidList , code , lino , _end , terrno ) ;
2023-04-08 17:39:09 +00:00
2024-08-23 03:01:45 +00:00
int32_t numOfTables = 0 ;
code = tableListGetSize ( pTableListInfo , & numOfTables ) ;
QUERY_CHECK_CODE ( code , lino , _end ) ;
2023-06-19 12:48:49 +00:00
for ( int32_t i = 0 ; i < numOfTables ; + + i ) {
2023-04-08 17:39:09 +00:00
STableKeyInfo * pKeyInfo = tableListGetInfo ( pTableListInfo , i ) ;
2024-08-06 01:24:31 +00:00
QUERY_CHECK_NULL ( pKeyInfo , code , lino , _end , terrno ) ;
2024-08-24 10:19:25 +00:00
void * tmp = taosArrayPush ( pUidList , & pKeyInfo - > uid ) ;
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL ( tmp , code , lino , _end , terrno ) ;
2023-04-08 17:39:09 +00:00
}
taosArrayDestroy ( plist ) ;
2024-07-22 04:51:25 +00:00
_end :
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " %s failed at line %d since %s " , __func__ , lino , tstrerror ( code ) ) ;
T_LONG_JMP ( pTaskInfo - > env , code ) ;
}
2023-04-08 17:39:09 +00:00
return pUidList ;
}
2023-04-28 03:42:34 +00:00
2024-08-06 05:22:14 +00:00
static int32_t extractTableList ( SArray * pList , const SOperatorInfo * pOperator ) {
2024-07-22 04:51:25 +00:00
int32_t code = TSDB_CODE_SUCCESS ;
int32_t lino = 0 ;
SExecTaskInfo * pTaskInfo = pOperator - > pTaskInfo ;
2023-04-28 03:42:34 +00:00
if ( pOperator - > operatorType = = QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN ) {
2026-03-12 01:11:00 +00:00
STmqQueryScanInfo * pScanInfo = pOperator - > info ;
2023-04-28 03:42:34 +00:00
STableScanInfo * pTableScanInfo = pScanInfo - > pTableScanOp - > info ;
2024-08-06 05:22:14 +00:00
void * tmp = taosArrayPush ( pList , & pTableScanInfo - > base . pTableListInfo ) ;
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL ( tmp , code , lino , _end , terrno ) ;
2023-04-28 03:42:34 +00:00
} else if ( pOperator - > operatorType = = QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN ) {
STableScanInfo * pScanInfo = pOperator - > info ;
2024-08-06 05:22:14 +00:00
void * tmp = taosArrayPush ( pList , & pScanInfo - > base . pTableListInfo ) ;
2024-07-25 11:11:32 +00:00
QUERY_CHECK_NULL ( tmp , code , lino , _end , terrno ) ;
2023-04-28 03:42:34 +00:00
} else {
if ( pOperator - > pDownstream ! = NULL & & pOperator - > pDownstream [ 0 ] ! = NULL ) {
2024-08-06 05:22:14 +00:00
code = extractTableList ( pList , pOperator - > pDownstream [ 0 ] ) ;
2023-04-28 03:42:34 +00:00
}
}
2024-07-22 04:51:25 +00:00
_end :
if ( code ! = TSDB_CODE_SUCCESS ) {
2024-08-06 05:22:14 +00:00
qError ( " %s %s failed at line %d since %s " , pTaskInfo - > id . str , __func__ , lino , tstrerror ( code ) ) ;
2024-07-22 04:51:25 +00:00
}
2024-08-06 05:22:14 +00:00
return code ;
2023-04-28 03:42:34 +00:00
}
2024-07-23 09:31:41 +00:00
int32_t getTableListInfo ( const SExecTaskInfo * pTaskInfo , SArray * * pList ) {
if ( pList = = NULL ) {
return TSDB_CODE_INVALID_PARA ;
}
2024-08-06 06:05:46 +00:00
* pList = NULL ;
2024-07-23 09:31:41 +00:00
SArray * pArray = taosArrayInit ( 0 , POINTER_BYTES ) ;
if ( pArray = = NULL ) {
2024-08-06 06:05:46 +00:00
return terrno ;
2024-07-23 09:31:41 +00:00
}
2024-08-06 06:05:46 +00:00
int32_t code = extractTableList ( pArray , pTaskInfo - > pRoot ) ;
if ( code = = 0 ) {
* pList = pArray ;
2024-09-02 05:46:21 +00:00
} else {
taosArrayDestroy ( pArray ) ;
2024-08-06 06:05:46 +00:00
}
return code ;
2023-06-15 06:13:17 +00:00
}
int32_t qStreamOperatorReleaseState ( qTaskInfo_t tInfo ) {
2024-07-22 04:51:25 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tInfo ;
2024-10-10 09:17:21 +00:00
if ( pTaskInfo - > pRoot - > fpSet . releaseStreamStateFn ! = NULL ) {
pTaskInfo - > pRoot - > fpSet . releaseStreamStateFn ( pTaskInfo - > pRoot ) ;
}
2023-06-15 06:13:17 +00:00
return 0 ;
}
int32_t qStreamOperatorReloadState ( qTaskInfo_t tInfo ) {
2024-07-22 04:51:25 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tInfo ;
2024-10-11 07:27:29 +00:00
if ( pTaskInfo - > pRoot - > fpSet . reloadStreamStateFn ! = NULL ) {
pTaskInfo - > pRoot - > fpSet . reloadStreamStateFn ( pTaskInfo - > pRoot ) ;
}
2023-06-15 06:13:17 +00:00
return 0 ;
}
2024-09-08 06:54:08 +00:00
void qResetTaskCode ( qTaskInfo_t tinfo ) {
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tinfo ;
int32_t code = pTaskInfo - > code ;
pTaskInfo - > code = 0 ;
2024-09-08 07:17:08 +00:00
qDebug ( " 0x% " PRIx64 " reset task code to be success, prev:%s " , pTaskInfo - > id . taskId , tstrerror ( code ) ) ;
2024-09-08 06:54:08 +00:00
}
2025-04-16 08:36:09 +00:00
int32_t collectExprsToReplaceForStream ( SOperatorInfo * pOper , SArray * pExprs ) {
int32_t code = 0 ;
return code ;
}
int32_t streamCollectExprsForReplace ( qTaskInfo_t tInfo , SArray * pExprs ) {
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tInfo ;
2025-05-07 11:20:45 +00:00
int32_t code = collectExprsToReplaceForStream ( pTaskInfo - > pRoot , pExprs ) ;
2025-04-16 08:36:09 +00:00
return code ;
}
int32_t clearStatesForOperator ( SOperatorInfo * pOper ) {
int32_t code = 0 ;
2025-07-01 00:38:36 +00:00
freeResetOperatorParams ( pOper , OP_GET_PARAM , true ) ;
freeResetOperatorParams ( pOper , OP_NOTIFY_PARAM , true ) ;
2025-04-29 07:31:17 +00:00
if ( pOper - > fpSet . resetStateFn ) {
code = pOper - > fpSet . resetStateFn ( pOper ) ;
}
2025-05-08 09:36:10 +00:00
pOper - > status = OP_NOT_OPENED ;
2025-04-16 08:36:09 +00:00
for ( int32_t i = 0 ; i < pOper - > numOfDownstream & & code = = 0 ; + + i ) {
code = clearStatesForOperator ( pOper - > pDownstream [ i ] ) ;
}
return code ;
}
int32_t streamClearStatesForOperators ( qTaskInfo_t tInfo ) {
2025-05-07 11:20:45 +00:00
int32_t code = 0 ;
2025-04-16 08:36:09 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tInfo ;
SOperatorInfo * pOper = pTaskInfo - > pRoot ;
2025-09-10 08:12:11 +00:00
pTaskInfo - > code = TSDB_CODE_SUCCESS ;
2026-05-21 11:48:26 +00:00
// Increment stream generation so scalar remote value caches are invalidated
if ( pTaskInfo - > pSubJobCtx ) {
pTaskInfo - > pSubJobCtx - > streamGen + + ;
// Propagate gen via dedicated funcInfo.streamGen field so vnode readers
// (reached via Exchange) can detect new trigger events and invalidate
// their scalar subquery caches.
if ( pTaskInfo - > pStreamRuntimeInfo ) {
pTaskInfo - > pStreamRuntimeInfo - > funcInfo . streamGen = pTaskInfo - > pSubJobCtx - > streamGen ;
}
}
2025-04-16 08:36:09 +00:00
code = clearStatesForOperator ( pOper ) ;
return code ;
}
2026-03-29 01:38:08 +00:00
int32_t streamExecuteTask ( qTaskInfo_t tInfo , SSDataBlock * * ppRes , bool * finished ) {
2025-04-16 08:36:09 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tInfo ;
int64_t threadId = taosGetSelfPthreadId ( ) ;
int64_t curOwner = 0 ;
2025-05-13 05:46:12 +00:00
* ppRes = NULL ;
2025-04-16 08:36:09 +00:00
// todo extract method
taosRLockLatch ( & pTaskInfo - > lock ) ;
bool isKilled = isTaskKilled ( pTaskInfo ) ;
if ( isKilled ) {
2025-05-07 11:20:45 +00:00
// clearStreamBlock(pTaskInfo->pRoot);
2025-04-16 08:36:09 +00:00
qDebug ( " %s already killed, abort " , GET_TASKID ( pTaskInfo ) ) ;
taosRUnLockLatch ( & pTaskInfo - > lock ) ;
return pTaskInfo - > code ;
}
if ( pTaskInfo - > owner ! = 0 ) {
qError ( " %s-%p execTask is now executed by thread:%p " , GET_TASKID ( pTaskInfo ) , pTaskInfo , ( void * ) curOwner ) ;
pTaskInfo - > code = TSDB_CODE_QRY_IN_EXEC ;
taosRUnLockLatch ( & pTaskInfo - > lock ) ;
return pTaskInfo - > code ;
}
pTaskInfo - > owner = threadId ;
taosRUnLockLatch ( & pTaskInfo - > lock ) ;
2026-05-21 11:48:26 +00:00
// Stream compute tasks in the community build run via streamRunner ->
// streamExecuteTask, bypassing qworker (which also calls this). Set the
// thread-local scalar context here so scalar subqueries embedded in the
// stream operator (e.g. WHERE ts >= (SELECT ...)) can find their subJobCtx.
setTaskScalarExtraInfo ( tInfo ) ;
2025-04-16 08:36:09 +00:00
if ( pTaskInfo - > cost . start = = 0 ) {
pTaskInfo - > cost . start = taosGetTimestampUs ( ) ;
}
// error occurs, record the error code and return to client
int32_t ret = setjmp ( pTaskInfo - > env ) ;
if ( ret ! = TSDB_CODE_SUCCESS ) {
pTaskInfo - > code = ret ;
( void ) cleanUpUdfs ( ) ;
qDebug ( " %s task abort due to error/cancel occurs, code:%s " , GET_TASKID ( pTaskInfo ) , tstrerror ( pTaskInfo - > code ) ) ;
atomic_store_64 ( & pTaskInfo - > owner , 0 ) ;
return pTaskInfo - > code ;
}
qDebug ( " %s execTask is launched " , GET_TASKID ( pTaskInfo ) ) ;
int64_t st = taosGetTimestampUs ( ) ;
2025-05-13 05:46:12 +00:00
int32_t code = pTaskInfo - > pRoot - > fpSet . getNextFn ( pTaskInfo - > pRoot , ppRes ) ;
2025-04-16 08:36:09 +00:00
if ( code ) {
pTaskInfo - > code = code ;
qError ( " %s failed at line %d, code:%s %s " , __func__ , __LINE__ , tstrerror ( code ) , GET_TASKID ( pTaskInfo ) ) ;
2025-04-29 07:31:17 +00:00
} else {
2025-05-16 08:29:34 +00:00
* finished = * ppRes = = NULL ;
2025-05-13 05:46:12 +00:00
code = blockDataCheck ( * ppRes ) ;
2025-04-16 08:36:09 +00:00
}
if ( code ) {
pTaskInfo - > code = code ;
qError ( " %s failed at line %d, code:%s %s " , __func__ , __LINE__ , tstrerror ( code ) , GET_TASKID ( pTaskInfo ) ) ;
}
uint64_t el = ( taosGetTimestampUs ( ) - st ) ;
pTaskInfo - > cost . elapsedTime + = el ;
2025-05-07 11:20:45 +00:00
( void ) cleanUpUdfs ( ) ;
2025-04-16 08:36:09 +00:00
2025-05-13 05:46:12 +00:00
int32_t current = ( * ppRes ! = NULL ) ? ( * ppRes ) - > info . rows : 0 ;
2025-04-16 08:36:09 +00:00
uint64_t total = pTaskInfo - > pRoot - > resultInfo . totalRows ;
qDebug ( " %s task suspended, %d rows returned, total:% " PRId64 " rows, in sinkNode:%d, elapsed:%.2f ms " ,
GET_TASKID ( pTaskInfo ) , current , total , 0 , el / 1000.0 ) ;
atomic_store_64 ( & pTaskInfo - > owner , 0 ) ;
return pTaskInfo - > code ;
}
2025-06-28 03:47:31 +00:00
// void streamSetTaskRuntimeInfo(qTaskInfo_t tinfo, SStreamRuntimeInfo* pStreamRuntimeInfo) {
// SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)tinfo;
// pTaskInfo->pStreamRuntimeInfo = pStreamRuntimeInfo;
// }
2025-04-24 10:02:12 +00:00
2025-04-28 11:21:47 +00:00
int32_t qStreamCreateTableListForReader ( void * pVnode , uint64_t suid , uint64_t uid , int8_t tableType ,
2025-05-07 11:20:45 +00:00
SNodeList * pGroupTags , bool groupSort , SNode * pTagCond , SNode * pTagIndexCond ,
2025-05-15 02:03:57 +00:00
SStorageAPI * storageAPI , void * * pTableListInfo , SHashObj * groupIdMap ) {
2025-11-11 07:57:54 +00:00
int32_t code = 0 ;
if ( * pTableListInfo ! = NULL ) {
qDebug ( " table list already exists, no need to create again " ) ;
goto end ;
}
2025-05-07 11:20:45 +00:00
STableListInfo * pList = tableListCreate ( ) ;
if ( pList = = NULL ) {
qError ( " %s failed at line %d since %s " , __func__ , __LINE__ , tstrerror ( terrno ) ) ;
2025-11-11 07:57:54 +00:00
code = terrno ;
goto end ;
2025-05-07 11:20:45 +00:00
}
SScanPhysiNode pScanNode = { . suid = suid , . uid = uid , . tableType = tableType } ;
SReadHandle pHandle = { . vnode = pVnode } ;
SExecTaskInfo pTaskInfo = { . id . str = " " , . storageAPI = * storageAPI } ;
2025-11-11 07:57:54 +00:00
code = createScanTableListInfo ( & pScanNode , pGroupTags , groupSort , & pHandle , pList , pTagCond , pTagIndexCond , & pTaskInfo , groupIdMap ) ;
2025-05-07 11:20:45 +00:00
if ( code ! = 0 ) {
tableListDestroy ( pList ) ;
qError ( " failed to createScanTableListInfo, code:%s " , tstrerror ( code ) ) ;
2025-11-11 07:57:54 +00:00
goto end ;
2025-05-07 11:20:45 +00:00
}
* pTableListInfo = pList ;
2025-11-11 07:57:54 +00:00
end :
2025-05-07 11:20:45 +00:00
return 0 ;
2025-04-24 10:02:12 +00:00
}
2026-02-02 11:12:35 +00:00
static int32_t doFilterTableByTagCond ( void * pVnode , STableListInfo * pListInfo , SArray * pUidList , SNode * pTagCond , SStorageAPI * pStorageAPI ) {
int32_t code = doFilterByTagCond ( pListInfo - > idInfo . suid , pUidList , pTagCond , pVnode , SFLT_NOT_INDEX , pStorageAPI , NULL ) ;
if ( code ! = 0 ) {
return code ;
}
int32_t numOfTables = taosArrayGetSize ( pUidList ) ;
for ( int i = 0 ; i < numOfTables ; i + + ) {
void * tmp = taosArrayGet ( pUidList , i ) ;
if ( tmp = = NULL ) {
return terrno ;
}
STableKeyInfo info = { . uid = * ( uint64_t * ) tmp , . groupId = 0 } ;
2025-04-24 10:02:12 +00:00
2026-02-02 11:12:35 +00:00
void * p = taosArrayPush ( ( ( STableListInfo * ) pListInfo ) - > pTableList , & info ) ;
if ( p = = NULL ) {
return terrno ;
2025-07-03 06:59:30 +00:00
}
}
2025-11-11 07:57:54 +00:00
return code ;
2025-07-03 06:59:30 +00:00
}
2025-11-11 07:57:54 +00:00
int32_t qStreamFilterTableListForReader ( void * pVnode , SArray * uidList ,
SNodeList * pGroupTags , SNode * pTagCond , SNode * pTagIndexCond ,
SStorageAPI * storageAPI , SHashObj * groupIdMap , uint64_t suid , SArray * * tableList ) {
int32_t code = TSDB_CODE_SUCCESS ;
2026-03-03 09:58:05 +00:00
SArray * uidListCopy = NULL ;
2025-11-11 07:57:54 +00:00
STableListInfo * pList = tableListCreate ( ) ;
if ( pList = = NULL ) {
code = terrno ;
goto end ;
}
2026-03-03 09:58:05 +00:00
uidListCopy = taosArrayDup ( uidList , NULL ) ;
2025-11-11 07:57:54 +00:00
if ( uidListCopy = = NULL ) {
code = terrno ;
goto end ;
2025-07-23 09:34:59 +00:00
}
2025-11-11 07:57:54 +00:00
SScanPhysiNode pScanNode = { . suid = suid , . tableType = TD_SUPER_TABLE } ;
SReadHandle pHandle = { . vnode = pVnode } ;
2025-04-24 10:02:12 +00:00
2025-11-11 07:57:54 +00:00
pList - > idInfo . suid = suid ;
pList - > idInfo . tableType = TD_SUPER_TABLE ;
code = doFilterTableByTagCond ( pVnode , pList , uidList , pTagCond , storageAPI ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
goto end ;
}
2026-03-29 01:38:08 +00:00
code = buildGroupIdMapForAllTables ( pList , & pHandle , & pScanNode , pGroupTags , false , NULL , storageAPI , groupIdMap ,
false ) ;
2025-11-11 07:57:54 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
goto end ;
}
* tableList = pList - > pTableList ;
pList - > pTableList = NULL ;
taosArrayClear ( uidList ) ;
for ( int32_t i = 0 ; i < taosArrayGetSize ( uidListCopy ) ; i + + ) {
void * tmp = taosArrayGet ( uidListCopy , i ) ;
if ( tmp = = NULL ) {
continue ;
}
int32_t * slot = taosHashGet ( pList - > map , tmp , LONG_BYTES ) ;
if ( slot = = NULL ) {
if ( taosArrayPush ( uidList , tmp ) = = NULL ) {
code = terrno ;
goto end ;
}
2025-05-07 11:20:45 +00:00
}
2025-04-24 10:02:12 +00:00
}
2025-11-11 07:57:54 +00:00
end :
taosArrayDestroy ( uidListCopy ) ;
tableListDestroy ( pList ) ;
return code ;
2025-04-24 10:02:12 +00:00
}
2025-11-11 07:57:54 +00:00
// int32_t qStreamGetGroupIndex(void* pTableListInfo, int64_t gid, TdThreadRwlock* lock) {
// int32_t index = -1;
// (void)taosThreadRwlockRdlock(lock);
// if (((STableListInfo*)pTableListInfo)->groupOffset == NULL){
// index = 0;
// goto end;
// }
// for (int32_t i = 0; i < ((STableListInfo*)pTableListInfo)->numOfOuputGroups; ++i) {
// int32_t offset = ((STableListInfo*)pTableListInfo)->groupOffset[i];
// STableKeyInfo* pKeyInfo = taosArrayGet(((STableListInfo*)pTableListInfo)->pTableList, offset);
// if (pKeyInfo != NULL && pKeyInfo->groupId == gid) {
// index = i;
// goto end;
// }
// }
// end:
// (void)taosThreadRwlockUnlock(lock);
// return index;
// }
2025-04-28 11:21:47 +00:00
void qStreamDestroyTableList ( void * pTableListInfo ) { tableListDestroy ( pTableListInfo ) ; }
2025-11-11 07:57:54 +00:00
SArray * qStreamGetTableListArray ( void * pTableListInfo ) {
STableListInfo * pList = pTableListInfo ;
return pList - > pTableList ;
}
2025-04-28 11:21:47 +00:00
2025-09-25 07:48:14 +00:00
int32_t qStreamFilter ( SSDataBlock * pBlock , void * pFilterInfo , SColumnInfoData * * pRet ) { return doFilter ( pBlock , pFilterInfo , NULL , pRet ) ; }
2025-04-28 11:21:47 +00:00
2025-07-03 05:26:22 +00:00
void streamDestroyExecTask ( qTaskInfo_t tInfo ) {
2025-09-25 07:48:14 +00:00
qDebug ( " streamDestroyExecTask called, task:%p " , tInfo ) ;
2025-07-03 05:26:22 +00:00
qDestroyTask ( tInfo ) ;
}
2025-04-29 07:31:17 +00:00
2025-05-20 03:12:55 +00:00
int32_t streamCalcOneScalarExpr ( SNode * pExpr , SScalarParam * pDst , const SStreamRuntimeFuncInfo * pExtraParams ) {
2025-07-07 11:07:09 +00:00
return streamCalcOneScalarExprInRange ( pExpr , pDst , - 1 , - 1 , pExtraParams ) ;
}
2025-08-13 08:23:35 +00:00
int32_t streamCalcOneScalarExprInRange ( SNode * pExpr , SScalarParam * pDst , int32_t rowStartIdx , int32_t rowEndIdx ,
const SStreamRuntimeFuncInfo * pExtraParams ) {
2025-06-23 02:36:26 +00:00
int32_t code = 0 ;
2026-02-28 03:21:02 +00:00
SNode * pNode = NULL ;
2025-06-23 02:36:26 +00:00
SNodeList * pList = NULL ;
SExprInfo * pExprInfo = NULL ;
int32_t numOfExprs = 1 ;
2026-02-28 03:21:02 +00:00
int32_t * offset = NULL ;
2025-05-07 10:14:17 +00:00
STargetNode * pTargetNode = NULL ;
code = nodesMakeNode ( QUERY_NODE_TARGET , ( SNode * * ) & pTargetNode ) ;
2026-02-28 03:21:02 +00:00
if ( code = = 0 ) {
code = nodesCloneNode ( pExpr , & pNode ) ;
}
2025-04-29 07:31:17 +00:00
2025-05-07 10:14:17 +00:00
if ( code = = 0 ) {
pTargetNode - > dataBlockId = 0 ;
pTargetNode - > pExpr = pNode ;
pTargetNode - > slotId = 0 ;
}
2025-04-29 07:31:17 +00:00
if ( code = = 0 ) {
code = nodesMakeList ( & pList ) ;
}
if ( code = = 0 ) {
2025-05-07 10:14:17 +00:00
code = nodesListAppend ( pList , ( SNode * ) pTargetNode ) ;
2025-04-29 07:31:17 +00:00
}
if ( code = = 0 ) {
pNode = NULL ;
code = createExprInfo ( pList , NULL , & pExprInfo , & numOfExprs ) ;
}
if ( code = = 0 ) {
const char * pVal = NULL ;
2025-05-07 11:20:45 +00:00
int32_t len = 0 ;
SNode * pSclNode = NULL ;
2025-04-29 07:31:17 +00:00
switch ( pExprInfo - > pExpr - > nodeType ) {
case QUERY_NODE_FUNCTION :
pSclNode = ( SNode * ) pExprInfo - > pExpr - > _function . pFunctNode ;
break ;
case QUERY_NODE_OPERATOR :
pSclNode = pExprInfo - > pExpr - > _optrRoot . pRootNode ;
break ;
default :
code = TSDB_CODE_OPS_NOT_SUPPORT ;
break ;
}
2025-06-23 02:36:26 +00:00
SArray * pBlockList = taosArrayInit ( 2 , POINTER_BYTES ) ;
2025-05-07 10:14:17 +00:00
SSDataBlock block = { 0 } ;
2025-06-18 09:56:29 +00:00
block . info . rows = 1 ;
2025-05-07 10:14:17 +00:00
SSDataBlock * pBlock = & block ;
2025-08-13 08:23:35 +00:00
void * tmp = taosArrayPush ( pBlockList , & pBlock ) ;
if ( tmp = = NULL ) {
code = terrno ;
}
if ( code = = 0 ) {
2025-12-22 03:35:33 +00:00
gTaskScalarExtra . pStreamInfo = ( void * ) pExtraParams ;
gTaskScalarExtra . pStreamRange = NULL ;
code = scalarCalculateInRange ( pSclNode , pBlockList , pDst , rowStartIdx , rowEndIdx , & gTaskScalarExtra ) ;
2025-08-13 08:23:35 +00:00
}
2025-05-10 07:41:44 +00:00
taosArrayDestroy ( pBlockList ) ;
2025-04-29 07:31:17 +00:00
}
nodesDestroyList ( pList ) ;
destroyExprInfo ( pExprInfo , numOfExprs ) ;
2025-07-03 05:26:22 +00:00
taosMemoryFreeClear ( pExprInfo ) ;
2025-04-29 07:31:17 +00:00
return code ;
}
2025-05-16 08:29:34 +00:00
int32_t streamForceOutput ( qTaskInfo_t tInfo , SSDataBlock * * pRes , int32_t winIdx ) {
2025-04-29 07:31:17 +00:00
SExecTaskInfo * pTaskInfo = ( SExecTaskInfo * ) tInfo ;
const SArray * pForceOutputCols = pTaskInfo - > pStreamRuntimeInfo - > pForceOutputCols ;
int32_t code = 0 ;
SNode * pNode = NULL ;
if ( ! pForceOutputCols ) return 0 ;
2025-05-16 08:29:34 +00:00
if ( ! * pRes ) {
2025-04-29 07:31:17 +00:00
code = createDataBlock ( pRes ) ;
}
if ( code = = 0 & & ( ! ( * pRes ) - > pDataBlock | | ( * pRes ) - > pDataBlock - > size = = 0 ) ) {
int32_t idx = 0 ;
2025-05-07 11:20:45 +00:00
for ( int32_t i = 0 ; i < pForceOutputCols - > size ; + + i ) {
SStreamOutCol * pCol = ( SStreamOutCol * ) taosArrayGet ( pForceOutputCols , i ) ;
2025-04-29 07:31:17 +00:00
SColumnInfoData colInfo = createColumnInfoData ( pCol - > type . type , pCol - > type . bytes , idx + + ) ;
colInfo . info . precision = pCol - > type . precision ;
colInfo . info . scale = pCol - > type . scale ;
code = blockDataAppendColInfo ( * pRes , & colInfo ) ;
if ( code ! = 0 ) break ;
}
}
2025-08-13 08:23:35 +00:00
code = blockDataEnsureCapacity ( * pRes , ( * pRes ) - > info . rows + 1 ) ;
if ( code ! = TSDB_CODE_SUCCESS ) {
qError ( " failed to ensure capacity for force output, code:%s " , tstrerror ( code ) ) ;
return code ;
}
2025-04-29 07:31:17 +00:00
// loop all exprs for force output, execute all exprs
int32_t idx = 0 ;
2025-05-16 01:41:31 +00:00
int32_t rowIdx = ( * pRes ) - > info . rows ;
2025-05-16 08:29:34 +00:00
int32_t tmpWinIdx = pTaskInfo - > pStreamRuntimeInfo - > funcInfo . curIdx ;
pTaskInfo - > pStreamRuntimeInfo - > funcInfo . curIdx = winIdx ;
2025-04-29 07:31:17 +00:00
for ( int32_t i = 0 ; i < pForceOutputCols - > size ; + + i ) {
2025-05-20 03:12:55 +00:00
SScalarParam dst = { 0 } ;
2025-04-29 07:31:17 +00:00
SStreamOutCol * pCol = ( SStreamOutCol * ) taosArrayGet ( pForceOutputCols , i ) ;
2025-05-16 08:29:34 +00:00
code = nodesStringToNode ( pCol - > expr , & pNode ) ;
if ( code ! = 0 ) break ;
2025-04-29 07:31:17 +00:00
SColumnInfoData * pInfo = taosArrayGet ( ( * pRes ) - > pDataBlock , idx ) ;
if ( nodeType ( pNode ) = = QUERY_NODE_VALUE ) {
void * p = nodesGetValueFromNode ( ( SValueNode * ) pNode ) ;
2025-05-16 01:41:31 +00:00
code = colDataSetVal ( pInfo , rowIdx , p , ( ( SValueNode * ) pNode ) - > isNull ) ;
2025-04-29 07:31:17 +00:00
} else {
dst . columnData = pInfo ;
2025-07-07 11:07:09 +00:00
dst . numOfRows = rowIdx ;
2025-05-20 03:12:55 +00:00
dst . colAlloced = false ;
2025-09-25 07:48:14 +00:00
code = streamCalcOneScalarExprInRange ( pNode , & dst , rowIdx , rowIdx , & pTaskInfo - > pStreamRuntimeInfo - > funcInfo ) ;
2025-04-29 07:31:17 +00:00
}
+ + idx ;
2025-05-20 03:12:55 +00:00
// TODO sclFreeParam(&dst);
2025-05-16 08:29:34 +00:00
nodesDestroyNode ( pNode ) ;
2025-04-29 07:31:17 +00:00
if ( code ! = 0 ) break ;
}
2025-09-25 07:48:14 +00:00
if ( code = = TSDB_CODE_SUCCESS ) {
( * pRes ) - > info . rows + + ;
}
2025-05-16 08:29:34 +00:00
pTaskInfo - > pStreamRuntimeInfo - > funcInfo . curIdx = tmpWinIdx ;
2025-04-29 07:31:17 +00:00
return code ;
}
2025-09-25 07:48:14 +00:00
int32_t streamCalcOutputTbName ( SNode * pExpr , char * tbname , SStreamRuntimeFuncInfo * pStreamRuntimeInfo ) {
2025-04-29 07:31:17 +00:00
int32_t code = 0 ;
const char * pVal = NULL ;
SScalarParam dst = { 0 } ;
2025-05-07 11:20:45 +00:00
int32_t len = 0 ;
2025-09-25 07:48:14 +00:00
int32_t nextIdx = pStreamRuntimeInfo - > curIdx ;
pStreamRuntimeInfo - > curIdx = 0 ; // always use the first window to calc tbname
2026-03-29 01:38:08 +00:00
if ( pStreamRuntimeInfo - > outNormalTable ! = NULL ) {
tstrncpy ( tbname , pStreamRuntimeInfo - > outNormalTable , TSDB_DB_NAME_LEN + TSDB_TABLE_NAME_LEN + 1 ) ;
return code ;
}
2025-04-29 07:31:17 +00:00
// execute the expr
switch ( pExpr - > type ) {
case QUERY_NODE_VALUE : {
2025-06-23 02:36:26 +00:00
SValueNode * pValue = ( SValueNode * ) pExpr ;
int32_t type = pValue - > node . resType . type ;
2025-04-29 07:31:17 +00:00
if ( ! IS_STR_DATA_TYPE ( type ) ) {
qError ( " invalid sub tb expr with non-str type " ) ;
code = TSDB_CODE_INVALID_PARA ;
break ;
}
2025-06-23 02:36:26 +00:00
void * pTmp = nodesGetValueFromNode ( ( SValueNode * ) pExpr ) ;
if ( pTmp = = NULL ) {
qError ( " invalid sub tb expr with null value " ) ;
code = TSDB_CODE_INVALID_PARA ;
break ;
}
pVal = varDataVal ( pTmp ) ;
len = varDataLen ( pTmp ) ;
2025-04-29 07:31:17 +00:00
} break ;
case QUERY_NODE_FUNCTION : {
SFunctionNode * pFunc = ( SFunctionNode * ) pExpr ;
if ( ! IS_STR_DATA_TYPE ( pFunc - > node . resType . type ) ) {
qError ( " invalid sub tb expr with non-str type func " ) ;
code = TSDB_CODE_INVALID_PARA ;
break ;
}
2025-06-23 02:36:26 +00:00
SColumnInfoData * pCol = taosMemoryCalloc ( 1 , sizeof ( SColumnInfoData ) ) ;
2025-05-20 03:12:55 +00:00
if ( ! pCol ) {
code = terrno ;
qError ( " failed to allocate col info data at: %s, %d " , __func__ , __LINE__ ) ;
break ;
}
2025-06-18 09:56:29 +00:00
2025-05-20 03:12:55 +00:00
pCol - > hasNull = true ;
pCol - > info . type = ( ( SExprNode * ) pExpr ) - > resType . type ;
pCol - > info . colId = 0 ;
pCol - > info . bytes = ( ( SExprNode * ) pExpr ) - > resType . bytes ;
pCol - > info . precision = ( ( SExprNode * ) pExpr ) - > resType . precision ;
pCol - > info . scale = ( ( SExprNode * ) pExpr ) - > resType . scale ;
2025-08-13 08:23:35 +00:00
code = colInfoDataEnsureCapacity ( pCol , 1 , true ) ;
if ( code ! = 0 ) {
qError ( " failed to ensure capacity for col info data at: %s, %d " , __func__ , __LINE__ ) ;
taosMemoryFree ( pCol ) ;
break ;
}
2025-05-20 03:12:55 +00:00
dst . columnData = pCol ;
dst . numOfRows = 1 ;
dst . colAlloced = true ;
2025-04-29 07:31:17 +00:00
code = streamCalcOneScalarExpr ( pExpr , & dst , pStreamRuntimeInfo ) ;
2025-08-13 08:23:35 +00:00
if ( colDataIsNull_var ( dst . columnData , 0 ) ) {
qInfo ( " invalid sub tb expr with null value " ) ;
code = TSDB_CODE_MND_STREAM_TBNAME_CALC_FAILED ;
}
2025-04-29 07:31:17 +00:00
if ( code = = 0 ) {
pVal = varDataVal ( colDataGetVarData ( dst . columnData , 0 ) ) ;
2025-05-07 10:14:17 +00:00
len = varDataLen ( colDataGetVarData ( dst . columnData , 0 ) ) ;
2025-04-29 07:31:17 +00:00
}
} break ;
default :
qError ( " wrong subtable expr with type: %d " , pExpr - > type ) ;
code = TSDB_CODE_OPS_NOT_SUPPORT ;
break ;
}
if ( code = = 0 ) {
if ( ! pVal | | len = = 0 ) {
qError ( " tbname generated with no characters which is not allowed " ) ;
code = TSDB_CODE_INVALID_PARA ;
}
2025-07-09 08:19:39 +00:00
if ( len > TSDB_TABLE_NAME_LEN - 1 ) {
2025-07-16 10:43:02 +00:00
qError ( " tbname generated with too long characters, max allowed is %d, got %d, truncated. " , TSDB_TABLE_NAME_LEN - 1 , len ) ;
2025-07-17 05:37:03 +00:00
len = TSDB_TABLE_NAME_LEN - 1 ;
2025-07-16 07:12:26 +00:00
}
2025-07-22 08:04:24 +00:00
2025-07-17 05:37:03 +00:00
memcpy ( tbname , pVal , len ) ;
tbname [ len ] = ' \0 ' ; // ensure null terminated
2025-07-22 08:04:24 +00:00
if ( NULL ! = strchr ( tbname , ' . ' ) ) {
code = TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME ;
qError ( " tbname generated with invalid characters, '.' is not allowed " ) ;
}
2025-04-29 07:31:17 +00:00
}
2025-05-20 03:12:55 +00:00
// TODO free dst
2025-06-18 09:56:29 +00:00
sclFreeParam ( & dst ) ;
2025-09-25 07:48:14 +00:00
pStreamRuntimeInfo - > curIdx = nextIdx ; // restore
2025-04-29 07:31:17 +00:00
return code ;
}
2025-05-08 15:00:37 +00:00
2025-06-07 09:57:07 +00:00
void destroyStreamInserterParam ( SStreamInserterParam * pParam ) {
2025-05-08 15:00:37 +00:00
if ( pParam ) {
if ( pParam - > tbname ) {
taosMemFree ( pParam - > tbname ) ;
pParam - > tbname = NULL ;
}
2025-05-14 09:41:51 +00:00
if ( pParam - > stbname ) {
taosMemFree ( pParam - > stbname ) ;
pParam - > stbname = NULL ;
}
2025-05-08 15:00:37 +00:00
if ( pParam - > dbFName ) {
taosMemFree ( pParam - > dbFName ) ;
pParam - > dbFName = NULL ;
}
if ( pParam - > pFields ) {
taosArrayDestroy ( pParam - > pFields ) ;
pParam - > pFields = NULL ;
}
if ( pParam - > pTagFields ) {
taosArrayDestroy ( pParam - > pTagFields ) ;
pParam - > pTagFields = NULL ;
}
2025-12-30 02:55:17 +00:00
if ( pParam - > colCids ) {
taosArrayDestroy ( pParam - > colCids ) ;
pParam - > colCids = NULL ;
}
if ( pParam - > tagCids ) {
taosArrayDestroy ( pParam - > tagCids ) ;
pParam - > tagCids = NULL ;
}
2025-05-08 15:00:37 +00:00
taosMemFree ( pParam ) ;
}
}
int32_t cloneStreamInserterParam ( SStreamInserterParam * * ppDst , SStreamInserterParam * pSrc ) {
2025-07-01 00:38:36 +00:00
int32_t code = 0 , lino = 0 ;
2025-05-08 15:00:37 +00:00
if ( ppDst = = NULL | | pSrc = = NULL ) {
2025-07-01 00:38:36 +00:00
TAOS_CHECK_EXIT ( TSDB_CODE_INVALID_PARA ) ;
2025-05-08 15:00:37 +00:00
}
* ppDst = ( SStreamInserterParam * ) taosMemoryCalloc ( 1 , sizeof ( SStreamInserterParam ) ) ;
2025-07-01 00:38:36 +00:00
TSDB_CHECK_NULL ( * ppDst , code , lino , _exit , terrno ) ;
2025-05-08 15:00:37 +00:00
( * ppDst ) - > suid = pSrc - > suid ;
( * ppDst ) - > sver = pSrc - > sver ;
( * ppDst ) - > tbType = pSrc - > tbType ;
( * ppDst ) - > tbname = taosStrdup ( pSrc - > tbname ) ;
2025-07-01 00:38:36 +00:00
TSDB_CHECK_NULL ( ( * ppDst ) - > tbname , code , lino , _exit , terrno ) ;
2025-05-14 09:41:51 +00:00
if ( pSrc - > stbname ) {
( * ppDst ) - > stbname = taosStrdup ( pSrc - > stbname ) ;
2025-07-01 00:38:36 +00:00
TSDB_CHECK_NULL ( ( * ppDst ) - > stbname , code , lino , _exit , terrno ) ;
2025-05-14 09:41:51 +00:00
}
2025-05-08 15:00:37 +00:00
( * ppDst ) - > dbFName = taosStrdup ( pSrc - > dbFName ) ;
2025-07-01 00:38:36 +00:00
TSDB_CHECK_NULL ( ( * ppDst ) - > dbFName , code , lino , _exit , terrno ) ;
2025-06-23 02:36:26 +00:00
( * ppDst ) - > pSinkHandle = pSrc - > pSinkHandle ; // don't need clone and free
2025-05-08 15:00:37 +00:00
if ( pSrc - > pFields & & pSrc - > pFields - > size > 0 ) {
( * ppDst ) - > pFields = taosArrayDup ( pSrc - > pFields , NULL ) ;
2025-07-01 00:38:36 +00:00
TSDB_CHECK_NULL ( ( * ppDst ) - > pFields , code , lino , _exit , terrno ) ;
2025-05-08 15:00:37 +00:00
} else {
( * ppDst ) - > pFields = NULL ;
}
2025-07-01 00:38:36 +00:00
2025-05-08 15:00:37 +00:00
if ( pSrc - > pTagFields & & pSrc - > pTagFields - > size > 0 ) {
( * ppDst ) - > pTagFields = taosArrayDup ( pSrc - > pTagFields , NULL ) ;
2025-07-01 00:38:36 +00:00
TSDB_CHECK_NULL ( ( * ppDst ) - > pTagFields , code , lino , _exit , terrno ) ;
2025-05-08 15:00:37 +00:00
} else {
( * ppDst ) - > pTagFields = NULL ;
}
2025-12-30 02:55:17 +00:00
if ( pSrc - > colCids & & pSrc - > colCids - > size > 0 ) {
( * ppDst ) - > colCids = taosArrayDup ( pSrc - > colCids , NULL ) ;
TSDB_CHECK_NULL ( ( * ppDst ) - > colCids , code , lino , _exit , terrno ) ;
} else {
( * ppDst ) - > colCids = NULL ;
}
if ( pSrc - > tagCids & & pSrc - > tagCids - > size > 0 ) {
( * ppDst ) - > tagCids = taosArrayDup ( pSrc - > tagCids , NULL ) ;
TSDB_CHECK_NULL ( ( * ppDst ) - > tagCids , code , lino , _exit , terrno ) ;
} else {
( * ppDst ) - > tagCids = NULL ;
}
2025-07-01 00:38:36 +00:00
_exit :
if ( code ! = 0 ) {
if ( * ppDst ) {
destroyStreamInserterParam ( * ppDst ) ;
* ppDst = NULL ;
}
stError ( " %s failed at line %d, error:%s " , __FUNCTION__ , lino , tstrerror ( code ) ) ;
2025-05-08 15:00:37 +00:00
}
return code ;
}
2025-09-25 07:48:14 +00:00
int32_t dropStreamTable ( SMsgCb * pMsgCb , void * pOutput , SSTriggerDropRequest * pReq ) {
return doDropStreamTable ( pMsgCb , pOutput , pReq ) ;
}
int32_t dropStreamTableByTbName ( SMsgCb * pMsgCb , void * pOutput , SSTriggerDropRequest * pReq , char * tbName ) {
return doDropStreamTableByTbName ( pMsgCb , pOutput , pReq , tbName ) ;
}
2025-12-22 10:47:59 +00:00
2026-02-02 11:12:35 +00:00
int32_t qFilterTableList ( void * pVnode , SArray * uidList , SNode * node , void * pTaskInfo , uint64_t suid ) {
2026-01-09 09:40:51 +00:00
int32_t code = TSDB_CODE_SUCCESS ;
2025-12-22 10:14:39 +00:00
SNode * pTagCond = node = = NULL ? NULL : ( ( SSubplan * ) node ) - > pTagCond ;
2026-02-02 11:12:35 +00:00
code = doFilterByTagCond ( suid , uidList , pTagCond , pVnode , SFLT_NOT_INDEX , & ( ( SExecTaskInfo * ) pTaskInfo ) - > storageAPI , NULL ) ;
2025-12-22 10:14:39 +00:00
if ( code ! = TSDB_CODE_SUCCESS ) {
goto end ;
2026-01-09 09:40:51 +00:00
}
2025-12-22 10:14:39 +00:00
end :
return code ;
}
2026-01-29 06:54:28 +00:00
bool isTrueForSatisfied ( STrueForInfo * pTrueForInfo , int64_t skey , int64_t ekey , int64_t count ) {
if ( pTrueForInfo = = NULL ) {
return true ;
}
bool durationSatisfied = ( pTrueForInfo - > duration < = 0 ) | | ( llabs ( ekey - skey ) > = pTrueForInfo - > duration ) ;
bool countSatisfied = ( pTrueForInfo - > count < = 0 ) | | ( count > = pTrueForInfo - > count ) ;
switch ( pTrueForInfo - > trueForType ) {
case TRUE_FOR_DURATION_ONLY :
return durationSatisfied ;
case TRUE_FOR_COUNT_ONLY :
return countSatisfied ;
case TRUE_FOR_AND :
return durationSatisfied & & countSatisfied ;
case TRUE_FOR_OR :
return durationSatisfied | | countSatisfied ;
default :
return true ;
}
}