diff --git a/include/libs/nodes/nodes.h b/include/libs/nodes/nodes.h index 1ab8f2b48c6fd98a1f41ae0e5f6ae846ddb380d9..4c83a30bb9c5f55ffb3bf2ebfba2442019a19fd3 100644 --- a/include/libs/nodes/nodes.h +++ b/include/libs/nodes/nodes.h @@ -135,6 +135,7 @@ typedef enum ENodeType { QUERY_NODE_PHYSICAL_PLAN_EXCHANGE, QUERY_NODE_PHYSICAL_PLAN_SORT, QUERY_NODE_PHYSICAL_PLAN_INTERVAL, + QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW, QUERY_NODE_PHYSICAL_PLAN_DISPATCH, QUERY_NODE_PHYSICAL_PLAN_INSERT, QUERY_NODE_PHYSICAL_SUBPLAN, @@ -169,6 +170,7 @@ void nodesDestroyNode(SNodeptr pNode); SNodeList* nodesMakeList(); int32_t nodesListAppend(SNodeList* pList, SNodeptr pNode); int32_t nodesListStrictAppend(SNodeList* pList, SNodeptr pNode); +int32_t nodesListMakeAppend(SNodeList** pList, SNodeptr pNode); int32_t nodesListAppendList(SNodeList* pTarget, SNodeList* pSrc); int32_t nodesListStrictAppendList(SNodeList* pTarget, SNodeList* pSrc); SListCell* nodesListErase(SNodeList* pList, SListCell* pCell); diff --git a/include/libs/nodes/plannodes.h b/include/libs/nodes/plannodes.h index 77c59a38c4444408096d33aa68c49bcd611cbd66..794e0ca85a3e2a8ebdce741504f8d844bbecd2a2 100644 --- a/include/libs/nodes/plannodes.h +++ b/include/libs/nodes/plannodes.h @@ -95,6 +95,7 @@ typedef struct SWindowLogicNode { int8_t intervalUnit; int8_t slidingUnit; SFillNode* pFill; + int64_t sessionGap; } SWindowLogicNode; typedef enum ESubplanType { @@ -110,7 +111,7 @@ typedef struct SSubplanId { int32_t subplanId; } SSubplanId; -typedef struct SSubLogicPlan { +typedef struct SLogicSubplan { ENodeType type; SSubplanId id; SNodeList* pChildren; @@ -120,7 +121,7 @@ typedef struct SSubLogicPlan { SVgroupsInfo* pVgroupList; int32_t level; int32_t splitFlag; -} SSubLogicPlan; +} SLogicSubplan; typedef struct SQueryLogicPlan { ENodeType type; @@ -213,10 +214,14 @@ typedef struct SExchangePhysiNode { SNodeList* pSrcEndPoints; // element is SDownstreamSource, scheduler fill by calling qSetSuplanExecutionNode } SExchangePhysiNode; -typedef struct SIntervalPhysiNode { +typedef struct SWinodwPhysiNode { SPhysiNode node; SNodeList* pExprs; // these are expression list of parameter expression of function SNodeList* pFuncs; +} SWinodwPhysiNode; + +typedef struct SIntervalPhysiNode { + SWinodwPhysiNode window; int64_t interval; int64_t offset; int64_t sliding; @@ -225,6 +230,11 @@ typedef struct SIntervalPhysiNode { SFillNode* pFill; } SIntervalPhysiNode; +typedef struct SSessionWinodwPhysiNode { + SWinodwPhysiNode window; + int64_t gap; +} SSessionWinodwPhysiNode; + typedef struct SDataSinkNode { ENodeType type; SDataBlockDescNode* pInputDataBlockDesc; diff --git a/include/libs/nodes/querynodes.h b/include/libs/nodes/querynodes.h index b6a7892f18b2e131173d00c0f11b3e168327ebdb..616e24d67d0b915c40532c4722f88283ae169990 100644 --- a/include/libs/nodes/querynodes.h +++ b/include/libs/nodes/querynodes.h @@ -191,8 +191,8 @@ typedef struct SStateWindowNode { typedef struct SSessionWindowNode { ENodeType type; // QUERY_NODE_SESSION_WINDOW - int64_t gap; // gap between two session window(in microseconds) SNode* pCol; + SNode* pGap; // gap between two session window(in microseconds) } SSessionWindowNode; typedef struct SIntervalWindowNode { diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index e927b577deb3d37cedd8e7e101e92bda5ba6ea6f..b760f0e5029557f26a5bc8fd147ed808c12370c8 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -8342,7 +8342,7 @@ SOperatorInfo* doCreateOperatorTreeNode(SPhysiNode* pPhyNode, SExecTaskInfo* pTa SIntervalPhysiNode* pIntervalPhyNode = (SIntervalPhysiNode*)pPhyNode; int32_t num = 0; - SExprInfo* pExprInfo = createExprInfo(pIntervalPhyNode->pFuncs, NULL, &num); + SExprInfo* pExprInfo = createExprInfo(pIntervalPhyNode->window.pFuncs, NULL, &num); SSDataBlock* pResBlock = createOutputBuf_rv1(pPhyNode->pOutputDataBlockDesc); SInterval interval = {.interval = pIntervalPhyNode->interval, .sliding = pIntervalPhyNode->sliding, .intervalUnit = 'a', .slidingUnit = 'a'}; diff --git a/source/libs/nodes/src/nodesCloneFuncs.c b/source/libs/nodes/src/nodesCloneFuncs.c index 2fa7a8d663756f10b67e319ecb6640d65e3050e6..1b1fb01ffab64509bd339d30bc159fb9a0dffc9d 100644 --- a/source/libs/nodes/src/nodesCloneFuncs.c +++ b/source/libs/nodes/src/nodesCloneFuncs.c @@ -276,7 +276,7 @@ static SNode* logicWindowCopy(const SWindowLogicNode* pSrc, SWindowLogicNode* pD return (SNode*)pDst; } -static SNode* logicSubplanCopy(const SSubLogicPlan* pSrc, SSubLogicPlan* pDst) { +static SNode* logicSubplanCopy(const SLogicSubplan* pSrc, SLogicSubplan* pDst) { CLONE_NODE_FIELD(pNode); COPY_SCALAR_FIELD(subplanType); return (SNode*)pDst; @@ -358,7 +358,7 @@ SNodeptr nodesCloneNode(const SNodeptr pNode) { case QUERY_NODE_LOGIC_PLAN_WINDOW: return logicWindowCopy((const SWindowLogicNode*)pNode, (SWindowLogicNode*)pDst); case QUERY_NODE_LOGIC_SUBPLAN: - return logicSubplanCopy((const SSubLogicPlan*)pNode, (SSubLogicPlan*)pDst); + return logicSubplanCopy((const SLogicSubplan*)pNode, (SLogicSubplan*)pDst); default: break; } diff --git a/source/libs/nodes/src/nodesCodeFuncs.c b/source/libs/nodes/src/nodesCodeFuncs.c index f43557c33527d4d03ea45387c5d65962eee36b62..f4b6a42b758e80df0c56fd9beca7b2c69ba861f3 100644 --- a/source/libs/nodes/src/nodesCodeFuncs.c +++ b/source/libs/nodes/src/nodesCodeFuncs.c @@ -70,6 +70,14 @@ const char* nodesNodeName(ENodeType type) { return "SlotDesc"; case QUERY_NODE_COLUMN_DEF: return "ColumnDef"; + case QUERY_NODE_DOWNSTREAM_SOURCE: + return "DownstreamSource"; + case QUERY_NODE_DATABASE_OPTIONS: + return "DatabaseOptions"; + case QUERY_NODE_TABLE_OPTIONS: + return "TableOptions"; + case QUERY_NODE_INDEX_OPTIONS: + return "IndexOptions"; case QUERY_NODE_SET_OPERATOR: return "SetOperator"; case QUERY_NODE_SELECT_STMT: @@ -78,16 +86,76 @@ const char* nodesNodeName(ENodeType type) { return "VnodeModifStmt"; case QUERY_NODE_CREATE_DATABASE_STMT: return "CreateDatabaseStmt"; + case QUERY_NODE_DROP_DATABASE_STMT: + return "DropDatabaseStmt"; + case QUERY_NODE_ALTER_DATABASE_STMT: + return "AlterDatabaseStmt"; case QUERY_NODE_CREATE_TABLE_STMT: return "CreateTableStmt"; + case QUERY_NODE_CREATE_SUBTABLE_CLAUSE: + return "CreateSubtableClause"; + case QUERY_NODE_CREATE_MULTI_TABLE_STMT: + return "CreateMultiTableStmt"; + case QUERY_NODE_DROP_TABLE_CLAUSE: + return "DropTableClause"; + case QUERY_NODE_DROP_TABLE_STMT: + return "DropTableStmt"; + case QUERY_NODE_DROP_SUPER_TABLE_STMT: + return "DropSuperTableStmt"; + case QUERY_NODE_ALTER_TABLE_STMT: + return "AlterTableStmt"; + case QUERY_NODE_CREATE_USER_STMT: + return "CreateUserStmt"; + case QUERY_NODE_ALTER_USER_STMT: + return "AlterUserStmt"; + case QUERY_NODE_DROP_USER_STMT: + return "DropUserStmt"; case QUERY_NODE_USE_DATABASE_STMT: return "UseDatabaseStmt"; + case QUERY_NODE_CREATE_DNODE_STMT: + return "CreateDnodeStmt"; + case QUERY_NODE_DROP_DNODE_STMT: + return "DropDnodeStmt"; + case QUERY_NODE_ALTER_DNODE_STMT: + return "AlterDnodeStmt"; + case QUERY_NODE_CREATE_INDEX_STMT: + return "CreateIndexStmt"; + case QUERY_NODE_DROP_INDEX_STMT: + return "DropIndexStmt"; + case QUERY_NODE_CREATE_QNODE_STMT: + return "CreateQnodeStmt"; + case QUERY_NODE_DROP_QNODE_STMT: + return "DropQnodeStmt"; + case QUERY_NODE_CREATE_TOPIC_STMT: + return "CreateTopicStmt"; + case QUERY_NODE_DROP_TOPIC_STMT: + return "DropTopicStmt"; + case QUERY_NODE_ALTER_LOCAL_STMT: + return "AlterLocalStmt"; case QUERY_NODE_SHOW_DATABASES_STMT: return "ShowDatabaseStmt"; case QUERY_NODE_SHOW_TABLES_STMT: return "ShowTablesStmt"; - case QUERY_NODE_CREATE_TOPIC_STMT: - return "CreateTopicStmt"; + case QUERY_NODE_SHOW_STABLES_STMT: + return "ShowStablesStmt"; + case QUERY_NODE_SHOW_USERS_STMT: + return "ShowUsersStmt"; + case QUERY_NODE_SHOW_DNODES_STMT: + return "ShowDnodesStmt"; + case QUERY_NODE_SHOW_VGROUPS_STMT: + return "ShowVgroupsStmt"; + case QUERY_NODE_SHOW_MNODES_STMT: + return "ShowMnodesStmt"; + case QUERY_NODE_SHOW_MODULES_STMT: + return "ShowModulesStmt"; + case QUERY_NODE_SHOW_QNODES_STMT: + return "ShowQnodesStmt"; + case QUERY_NODE_SHOW_FUNCTIONS_STMT: + return "ShowFunctionsStmt"; + case QUERY_NODE_SHOW_INDEXES_STMT: + return "ShowIndexesStmt"; + case QUERY_NODE_SHOW_STREAMS_STMT: + return "ShowStreamsStmt"; case QUERY_NODE_LOGIC_PLAN_SCAN: return "LogicScan"; case QUERY_NODE_LOGIC_PLAN_JOIN: @@ -98,6 +166,10 @@ const char* nodesNodeName(ENodeType type) { return "LogicProject"; case QUERY_NODE_LOGIC_PLAN_VNODE_MODIF: return "LogicVnodeModif"; + case QUERY_NODE_LOGIC_PLAN_EXCHANGE: + return "LogicExchange"; + case QUERY_NODE_LOGIC_PLAN_WINDOW: + return "LogicWindow"; case QUERY_NODE_LOGIC_SUBPLAN: return "LogicSubplan"; case QUERY_NODE_LOGIC_PLAN: @@ -846,8 +918,37 @@ static int32_t jsonToPhysiExchangeNode(const SJson* pJson, void* pObj) { return code; } -static const char* jkIntervalPhysiPlanExprs = "Exprs"; -static const char* jkIntervalPhysiPlanFuncs = "Funcs"; +static const char* jkWindowPhysiPlanExprs = "Exprs"; +static const char* jkWindowPhysiPlanFuncs = "Funcs"; + +static int32_t physiWindowNodeToJson(const void* pObj, SJson* pJson) { + const SWinodwPhysiNode* pNode = (const SWinodwPhysiNode*)pObj; + + int32_t code = physicPlanNodeToJson(pObj, pJson); + if (TSDB_CODE_SUCCESS == code) { + code = nodeListToJson(pJson, jkWindowPhysiPlanExprs, pNode->pExprs); + } + if (TSDB_CODE_SUCCESS == code) { + code = nodeListToJson(pJson, jkWindowPhysiPlanFuncs, pNode->pFuncs); + } + + return code; +} + +static int32_t jsonToPhysiWindowNode(const SJson* pJson, void* pObj) { + SWinodwPhysiNode* pNode = (SWinodwPhysiNode*)pObj; + + int32_t code = jsonToPhysicPlanNode(pJson, pObj); + if (TSDB_CODE_SUCCESS == code) { + code = jsonToNodeList(pJson, jkWindowPhysiPlanExprs, &pNode->pExprs); + } + if (TSDB_CODE_SUCCESS == code) { + code = jsonToNodeList(pJson, jkWindowPhysiPlanFuncs, &pNode->pFuncs); + } + + return code; +} + static const char* jkIntervalPhysiPlanInterval = "Interval"; static const char* jkIntervalPhysiPlanOffset = "Offset"; static const char* jkIntervalPhysiPlanSliding = "Sliding"; @@ -858,13 +959,7 @@ static const char* jkIntervalPhysiPlanFill = "Fill"; static int32_t physiIntervalNodeToJson(const void* pObj, SJson* pJson) { const SIntervalPhysiNode* pNode = (const SIntervalPhysiNode*)pObj; - int32_t code = physicPlanNodeToJson(pObj, pJson); - if (TSDB_CODE_SUCCESS == code) { - code = nodeListToJson(pJson, jkIntervalPhysiPlanExprs, pNode->pExprs); - } - if (TSDB_CODE_SUCCESS == code) { - code = nodeListToJson(pJson, jkIntervalPhysiPlanFuncs, pNode->pFuncs); - } + int32_t code = physiWindowNodeToJson(pObj, pJson); if (TSDB_CODE_SUCCESS == code) { code = tjsonAddIntegerToObject(pJson, jkIntervalPhysiPlanInterval, pNode->interval); } @@ -890,13 +985,7 @@ static int32_t physiIntervalNodeToJson(const void* pObj, SJson* pJson) { static int32_t jsonToPhysiIntervalNode(const SJson* pJson, void* pObj) { SIntervalPhysiNode* pNode = (SIntervalPhysiNode*)pObj; - int32_t code = jsonToPhysicPlanNode(pJson, pObj); - if (TSDB_CODE_SUCCESS == code) { - code = jsonToNodeList(pJson, jkIntervalPhysiPlanExprs, &pNode->pExprs); - } - if (TSDB_CODE_SUCCESS == code) { - code = jsonToNodeList(pJson, jkIntervalPhysiPlanFuncs, &pNode->pFuncs); - } + int32_t code = jsonToPhysiWindowNode(pJson, pObj); if (TSDB_CODE_SUCCESS == code) { code = tjsonGetBigIntValue(pJson, jkIntervalPhysiPlanInterval, &pNode->interval); } @@ -919,6 +1008,30 @@ static int32_t jsonToPhysiIntervalNode(const SJson* pJson, void* pObj) { return code; } +static const char* jkSessionWindowPhysiPlanGap = "Gap"; + +static int32_t physiSessionWindowNodeToJson(const void* pObj, SJson* pJson) { + const SSessionWinodwPhysiNode* pNode = (const SSessionWinodwPhysiNode*)pObj; + + int32_t code = physiWindowNodeToJson(pObj, pJson); + if (TSDB_CODE_SUCCESS == code) { + code = tjsonAddIntegerToObject(pJson, jkSessionWindowPhysiPlanGap, pNode->gap); + } + + return code; +} + +static int32_t jsonToPhysiSessionWindowNode(const SJson* pJson, void* pObj) { + SSessionWinodwPhysiNode* pNode = (SSessionWinodwPhysiNode*)pObj; + + int32_t code = jsonToPhysiWindowNode(pJson, pObj); + if (TSDB_CODE_SUCCESS == code) { + code = tjsonGetNumberValue(pJson, jkSessionWindowPhysiPlanGap, pNode->gap); + } + + return code; +} + static const char* jkDataSinkInputDataBlockDesc = "InputDataBlockDesc"; static int32_t physicDataSinkNodeToJson(const void* pObj, SJson* pJson) { @@ -2066,6 +2179,8 @@ static int32_t specificNodeToJson(const void* pObj, SJson* pJson) { break; case QUERY_NODE_PHYSICAL_PLAN_INTERVAL: return physiIntervalNodeToJson(pObj, pJson); + case QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW: + return physiSessionWindowNodeToJson(pObj, pJson); case QUERY_NODE_PHYSICAL_PLAN_DISPATCH: return physiDispatchNodeToJson(pObj, pJson); case QUERY_NODE_PHYSICAL_PLAN_INSERT: @@ -2075,7 +2190,7 @@ static int32_t specificNodeToJson(const void* pObj, SJson* pJson) { case QUERY_NODE_PHYSICAL_PLAN: return planToJson(pObj, pJson); default: - assert(0); + // assert(0); break; } nodesWarn("specificNodeToJson unknown node = %s", nodesNodeName(nodeType(pObj))); @@ -2149,14 +2264,16 @@ static int32_t jsonToSpecificNode(const SJson* pJson, void* pObj) { return jsonToPhysiAggNode(pJson, pObj); case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE: return jsonToPhysiExchangeNode(pJson, pObj); + case QUERY_NODE_PHYSICAL_PLAN_INTERVAL: + return jsonToPhysiIntervalNode(pJson, pObj); + case QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW: + return jsonToPhysiSessionWindowNode(pJson, pObj); case QUERY_NODE_PHYSICAL_PLAN_DISPATCH: return jsonToPhysiDispatchNode(pJson, pObj); case QUERY_NODE_PHYSICAL_SUBPLAN: return jsonToSubplan(pJson, pObj); case QUERY_NODE_PHYSICAL_PLAN: return jsonToPlan(pJson, pObj); - case QUERY_NODE_PHYSICAL_PLAN_INTERVAL: - return jsonToPhysiIntervalNode(pJson, pObj); default: assert(0); break; diff --git a/source/libs/nodes/src/nodesTraverseFuncs.c b/source/libs/nodes/src/nodesTraverseFuncs.c index e3c2dff1208b427db0df79dfe1eafd2fb7682c2b..ff71c3bd585450fe2c544290cc35a625c42efc50 100644 --- a/source/libs/nodes/src/nodesTraverseFuncs.c +++ b/source/libs/nodes/src/nodesTraverseFuncs.c @@ -79,9 +79,14 @@ static EDealRes walkNode(SNode* pNode, ETraversalOrder order, FNodeWalker walker case QUERY_NODE_STATE_WINDOW: res = walkNode(((SStateWindowNode*)pNode)->pCol, order, walker, pContext); break; - case QUERY_NODE_SESSION_WINDOW: - res = walkNode(((SSessionWindowNode*)pNode)->pCol, order, walker, pContext); + case QUERY_NODE_SESSION_WINDOW: { + SSessionWindowNode* pSession = (SSessionWindowNode*)pNode; + res = walkNode(pSession->pCol, order, walker, pContext); + if (DEAL_RES_ERROR != res) { + res = walkNode(pSession->pGap, order, walker, pContext); + } break; + } case QUERY_NODE_INTERVAL_WINDOW: { SIntervalWindowNode* pInterval = (SIntervalWindowNode*)pNode; res = walkNode(pInterval->pInterval, order, walker, pContext); diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index 14bae19cf1bc1c0b2f9738326e2dbf2a39e12e45..b2d6ce981658575ef06098d91e39c3dd3ae31cf2 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -160,7 +160,7 @@ SNodeptr nodesMakeNode(ENodeType type) { case QUERY_NODE_LOGIC_PLAN_WINDOW: return makeNode(type, sizeof(SWindowLogicNode)); case QUERY_NODE_LOGIC_SUBPLAN: - return makeNode(type, sizeof(SSubLogicPlan)); + return makeNode(type, sizeof(SLogicSubplan)); case QUERY_NODE_LOGIC_PLAN: return makeNode(type, sizeof(SQueryLogicPlan)); case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN: @@ -332,6 +332,7 @@ int32_t nodesListAppend(SNodeList* pList, SNodeptr pNode) { int32_t nodesListStrictAppend(SNodeList* pList, SNodeptr pNode) { if (NULL == pNode) { + terrno = TSDB_CODE_OUT_OF_MEMORY; return TSDB_CODE_OUT_OF_MEMORY; } int32_t code = nodesListAppend(pList, pNode); @@ -341,6 +342,17 @@ int32_t nodesListStrictAppend(SNodeList* pList, SNodeptr pNode) { return code; } +int32_t nodesListMakeAppend(SNodeList** pList, SNodeptr pNode) { + if (NULL == *pList) { + *pList = nodesMakeList(); + if (NULL == *pList) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return TSDB_CODE_OUT_OF_MEMORY; + } + } + return nodesListAppend(*pList, pNode); +} + int32_t nodesListAppendList(SNodeList* pTarget, SNodeList* pSrc) { if (NULL == pTarget || NULL == pSrc) { return TSDB_CODE_SUCCESS; diff --git a/source/libs/parser/inc/parAst.h b/source/libs/parser/inc/parAst.h index eb33dec7210677c2071fb1130f12e1718ba43b0d..f417356209ce489469c0c6ea94bea4b58a50c38e 100644 --- a/source/libs/parser/inc/parAst.h +++ b/source/libs/parser/inc/parAst.h @@ -100,7 +100,7 @@ SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const STok SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond); SNode* createLimitNode(SAstCreateContext* pCxt, const SToken* pLimit, const SToken* pOffset); SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder); -SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, const SToken* pVal); +SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap); SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pCol); SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding, SNode* pFill); SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues); diff --git a/source/libs/parser/inc/sql.y b/source/libs/parser/inc/sql.y index 4bb555681418552cba3a05658ef007e9917eda9b..1bc2fd9fb9b6b11cf808a526b023a218ca92672c 100644 --- a/source/libs/parser/inc/sql.y +++ b/source/libs/parser/inc/sql.y @@ -624,7 +624,7 @@ partition_by_clause_opt(A) ::= PARTITION BY expression_list(B). twindow_clause_opt(A) ::= . { A = NULL; } twindow_clause_opt(A) ::= - SESSION NK_LP column_reference(B) NK_COMMA NK_INTEGER(C) NK_RP. { A = createSessionWindowNode(pCxt, releaseRawExprNode(pCxt, B), &C); } + SESSION NK_LP column_reference(B) NK_COMMA duration_literal(C) NK_RP. { A = createSessionWindowNode(pCxt, releaseRawExprNode(pCxt, B), C); } twindow_clause_opt(A) ::= STATE_WINDOW NK_LP column_reference(B) NK_RP. { A = createStateWindowNode(pCxt, releaseRawExprNode(pCxt, B)); } twindow_clause_opt(A) ::= INTERVAL NK_LP duration_literal(B) NK_RP sliding_opt(C) fill_opt(D). { A = createIntervalWindowNode(pCxt, releaseRawExprNode(pCxt, B), NULL, C, D); } diff --git a/source/libs/parser/src/parAstCreater.c b/source/libs/parser/src/parAstCreater.c index fbf74b96a3b6fe3f1485fb3ce2887db69a8856df..a6d6e06113a83265d9cc27e7721121e0ab3395e9 100644 --- a/source/libs/parser/src/parAstCreater.c +++ b/source/libs/parser/src/parAstCreater.c @@ -679,11 +679,11 @@ SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order return (SNode*)orderByExpr; } -SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, const SToken* pVal) { +SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) { SSessionWindowNode* session = (SSessionWindowNode*)nodesMakeNode(QUERY_NODE_SESSION_WINDOW); CHECK_OUT_OF_MEM(session); session->pCol = pCol; - // session->gap = getInteger(pVal); + session->pGap = pGap; return (SNode*)session; } diff --git a/source/libs/parser/src/parUtil.c b/source/libs/parser/src/parUtil.c index 69c2380b4538765a68881722f5926d4500f4a28b..64c6b761d9d3354d28ec9d424d64cce850442544 100644 --- a/source/libs/parser/src/parUtil.c +++ b/source/libs/parser/src/parUtil.c @@ -71,6 +71,7 @@ int32_t generateSyntaxErrMsg(SMsgBuf* pBuf, int32_t errCode, ...) { va_start(vArgList, errCode); vsnprintf(pBuf->buf, pBuf->len, getSyntaxErrFormat(errCode), vArgList); va_end(vArgList); + terrno = errCode; return errCode; } diff --git a/source/libs/parser/src/sql.c b/source/libs/parser/src/sql.c index 50c29e095fcc722a2edbc58a34e4a6fee76fb7d2..eede8b1938e628101fc43d644d2017710adc0f47 100644 --- a/source/libs/parser/src/sql.c +++ b/source/libs/parser/src/sql.c @@ -208,135 +208,135 @@ typedef union { ** yy_default[] Default action for each state. ** *********** Begin parsing tables **********************************************/ -#define YY_ACTTAB_COUNT (1269) +#define YY_ACTTAB_COUNT (1266) static const YYACTIONTYPE yy_action[] = { - /* 0 */ 1042, 1201, 221, 43, 24, 167, 359, 1197, 1203, 1092, - /* 10 */ 246, 265, 89, 31, 29, 27, 26, 25, 20, 1201, + /* 0 */ 1042, 1201, 222, 43, 24, 168, 359, 1197, 1203, 1092, + /* 10 */ 247, 266, 89, 31, 29, 27, 26, 25, 20, 1201, /* 20 */ 1098, 27, 26, 25, 1081, 1197, 1202, 1103, 31, 29, - /* 30 */ 27, 26, 25, 358, 997, 78, 205, 866, 77, 76, - /* 40 */ 75, 74, 73, 72, 71, 70, 69, 1088, 207, 411, + /* 30 */ 27, 26, 25, 358, 997, 78, 206, 866, 77, 76, + /* 40 */ 75, 74, 73, 72, 71, 70, 69, 1088, 208, 411, /* 50 */ 410, 409, 408, 407, 406, 405, 404, 403, 402, 401, /* 60 */ 400, 399, 398, 397, 396, 395, 394, 393, 1000, 105, - /* 70 */ 266, 1011, 878, 31, 29, 27, 26, 25, 1228, 131, - /* 80 */ 901, 346, 111, 245, 233, 343, 1213, 1177, 271, 78, + /* 70 */ 267, 1011, 878, 31, 29, 27, 26, 25, 1228, 131, + /* 80 */ 901, 346, 111, 246, 234, 343, 1213, 1177, 272, 78, /* 90 */ 130, 22, 77, 76, 75, 74, 73, 72, 71, 70, - /* 100 */ 69, 31, 29, 27, 26, 25, 1228, 1314, 207, 287, - /* 110 */ 316, 282, 266, 343, 286, 44, 902, 285, 128, 283, - /* 120 */ 117, 342, 284, 345, 1312, 23, 228, 1189, 896, 897, - /* 130 */ 898, 899, 900, 904, 905, 906, 1079, 200, 1214, 1217, + /* 100 */ 69, 31, 29, 27, 26, 25, 1228, 1314, 208, 288, + /* 110 */ 317, 283, 267, 343, 287, 44, 902, 286, 128, 284, + /* 120 */ 117, 342, 285, 345, 1312, 23, 229, 1189, 896, 897, + /* 130 */ 898, 899, 900, 904, 905, 906, 1079, 201, 1214, 1217, /* 140 */ 901, 769, 382, 381, 380, 773, 379, 775, 776, 378, /* 150 */ 778, 375, 109, 784, 372, 786, 787, 369, 366, 1148, - /* 160 */ 1213, 846, 127, 1141, 301, 220, 125, 43, 323, 185, - /* 170 */ 1146, 358, 1133, 30, 28, 938, 902, 844, 254, 238, - /* 180 */ 1228, 230, 392, 846, 1099, 23, 228, 343, 896, 897, + /* 160 */ 1213, 846, 127, 1141, 302, 221, 125, 43, 323, 186, + /* 170 */ 1146, 358, 1133, 30, 28, 938, 902, 844, 255, 239, + /* 180 */ 1228, 231, 392, 846, 1099, 23, 229, 343, 896, 897, /* 190 */ 898, 899, 900, 904, 905, 906, 1201, 345, 358, 844, - /* 200 */ 893, 1189, 1197, 1202, 302, 359, 331, 845, 12, 118, - /* 210 */ 66, 61, 1214, 1217, 1253, 1213, 962, 275, 206, 1249, + /* 200 */ 893, 1189, 1197, 1202, 303, 359, 331, 845, 12, 118, + /* 210 */ 66, 61, 1214, 1217, 1253, 1213, 962, 276, 207, 1249, /* 220 */ 1165, 10, 122, 121, 30, 28, 1103, 120, 1314, 845, - /* 230 */ 1314, 1, 230, 423, 846, 1228, 312, 960, 961, 963, - /* 240 */ 964, 117, 330, 117, 1213, 1312, 241, 1312, 10, 321, + /* 230 */ 1314, 1, 231, 423, 846, 1228, 313, 960, 961, 963, + /* 240 */ 964, 117, 330, 117, 1213, 1312, 242, 1312, 10, 321, /* 250 */ 844, 334, 345, 1168, 1170, 423, 1189, 346, 696, 12, /* 260 */ 695, 847, 850, 1178, 1228, 359, 62, 1214, 1217, 1253, - /* 270 */ 66, 330, 279, 223, 1249, 112, 278, 281, 697, 327, - /* 280 */ 845, 345, 1, 847, 850, 1189, 1103, 163, 118, 9, - /* 290 */ 8, 59, 1213, 308, 1280, 62, 1214, 1217, 1253, 280, - /* 300 */ 92, 93, 223, 1249, 112, 1314, 423, 327, 1095, 106, + /* 270 */ 66, 330, 280, 224, 1249, 112, 279, 282, 697, 327, + /* 280 */ 845, 345, 1, 847, 850, 1189, 1103, 164, 118, 9, + /* 290 */ 8, 59, 1213, 309, 1280, 62, 1214, 1217, 1253, 281, + /* 300 */ 92, 93, 224, 1249, 112, 1314, 423, 327, 1095, 106, /* 310 */ 1070, 903, 1228, 31, 29, 27, 26, 25, 1313, 343, /* 320 */ 21, 1213, 1312, 1281, 392, 1267, 90, 868, 92, 345, /* 330 */ 907, 419, 418, 1189, 847, 850, 114, 1260, 1261, 867, /* 340 */ 1265, 1228, 1264, 62, 1214, 1217, 1253, 331, 343, 118, - /* 350 */ 223, 1249, 1326, 695, 90, 1094, 1213, 864, 345, 914, - /* 360 */ 1228, 1287, 1189, 865, 160, 1260, 326, 343, 325, 273, - /* 370 */ 234, 1314, 62, 1214, 1217, 1253, 1228, 359, 104, 223, + /* 350 */ 224, 1249, 1326, 695, 90, 1094, 1213, 864, 345, 914, + /* 360 */ 1228, 1287, 1189, 865, 161, 1260, 326, 343, 325, 274, + /* 370 */ 235, 1314, 62, 1214, 1217, 1253, 1228, 359, 104, 224, /* 380 */ 1249, 1326, 1100, 343, 117, 1213, 1105, 385, 1312, 1022, /* 390 */ 1310, 1090, 320, 345, 30, 28, 1189, 1189, 1103, 1021, - /* 400 */ 1267, 333, 230, 104, 846, 1228, 1080, 62, 1214, 1217, - /* 410 */ 1253, 1106, 343, 1148, 223, 1249, 1326, 1263, 359, 235, - /* 420 */ 844, 1148, 345, 356, 1146, 1271, 1189, 242, 162, 12, - /* 430 */ 1189, 331, 1146, 30, 28, 1012, 195, 1214, 1217, 1103, - /* 440 */ 1189, 230, 1213, 846, 190, 1020, 1019, 1018, 136, 192, - /* 450 */ 845, 134, 1, 1038, 315, 1314, 30, 28, 344, 844, - /* 460 */ 6, 191, 1228, 389, 230, 1213, 846, 388, 117, 343, - /* 470 */ 1017, 123, 1312, 1148, 118, 288, 423, 322, 317, 345, + /* 400 */ 1267, 333, 231, 104, 846, 1228, 1080, 62, 1214, 1217, + /* 410 */ 1253, 1106, 343, 1148, 224, 1249, 1326, 1263, 359, 236, + /* 420 */ 844, 1148, 345, 356, 1146, 1271, 1189, 243, 163, 12, + /* 430 */ 1189, 331, 1146, 30, 28, 1012, 196, 1214, 1217, 1103, + /* 440 */ 1189, 231, 1213, 846, 191, 1020, 1019, 1018, 136, 193, + /* 450 */ 845, 134, 1, 1038, 316, 1314, 30, 28, 344, 844, + /* 460 */ 6, 192, 1228, 389, 231, 1213, 846, 388, 117, 343, + /* 470 */ 1017, 123, 1312, 1148, 118, 289, 423, 322, 318, 345, /* 480 */ 1016, 1015, 844, 1189, 1169, 1228, 1189, 1189, 1189, 845, /* 490 */ 390, 7, 343, 63, 1214, 1217, 1253, 1148, 869, 1086, /* 500 */ 1252, 1249, 345, 1045, 847, 850, 1189, 945, 1147, 387, /* 510 */ 386, 1189, 845, 866, 7, 423, 63, 1214, 1217, 1253, - /* 520 */ 240, 1189, 1189, 341, 1249, 30, 28, 299, 104, 30, - /* 530 */ 28, 64, 384, 230, 338, 846, 1105, 230, 423, 846, - /* 540 */ 297, 1213, 1267, 847, 850, 999, 31, 29, 27, 26, - /* 550 */ 25, 844, 287, 933, 282, 844, 1014, 286, 118, 1262, - /* 560 */ 285, 1228, 283, 118, 1213, 284, 847, 850, 343, 87, + /* 520 */ 241, 1189, 1189, 341, 1249, 30, 28, 300, 104, 30, + /* 530 */ 28, 64, 384, 231, 338, 846, 1105, 231, 423, 846, + /* 540 */ 298, 1213, 1267, 847, 850, 999, 31, 29, 27, 26, + /* 550 */ 25, 844, 288, 933, 283, 844, 1014, 287, 118, 1262, + /* 560 */ 286, 1228, 284, 118, 1213, 285, 847, 850, 343, 87, /* 570 */ 86, 85, 84, 83, 82, 81, 80, 79, 345, 864, - /* 580 */ 1033, 845, 1189, 7, 1228, 845, 247, 1, 1071, 259, - /* 590 */ 1013, 343, 107, 1214, 1217, 937, 426, 1189, 260, 149, - /* 600 */ 1213, 345, 290, 164, 359, 1189, 243, 423, 339, 357, - /* 610 */ 183, 423, 304, 88, 104, 63, 1214, 1217, 1253, 415, - /* 620 */ 1228, 182, 1105, 1250, 98, 1103, 1010, 343, 313, 332, + /* 580 */ 1033, 845, 1189, 7, 1228, 845, 248, 1, 1071, 260, + /* 590 */ 1013, 343, 107, 1214, 1217, 937, 426, 1189, 261, 149, + /* 600 */ 1213, 345, 291, 165, 359, 1189, 244, 423, 339, 357, + /* 610 */ 184, 423, 305, 88, 104, 63, 1214, 1217, 1253, 415, + /* 620 */ 1228, 183, 1105, 1250, 98, 1103, 1010, 343, 314, 332, /* 630 */ 1327, 1189, 1031, 1009, 1008, 847, 850, 345, 359, 847, - /* 640 */ 850, 1189, 1142, 180, 229, 359, 60, 1007, 1006, 178, - /* 650 */ 244, 201, 1214, 1217, 293, 1005, 157, 1004, 1213, 1103, - /* 660 */ 1272, 933, 1229, 1213, 272, 258, 1103, 1189, 253, 252, - /* 670 */ 251, 250, 249, 335, 1189, 1189, 1003, 853, 1228, 138, + /* 640 */ 850, 1189, 1142, 181, 230, 359, 60, 1007, 1006, 179, + /* 650 */ 245, 202, 1214, 1217, 294, 1005, 158, 1004, 1213, 1103, + /* 660 */ 1272, 933, 1229, 1213, 273, 259, 1103, 1189, 254, 253, + /* 670 */ 252, 251, 250, 335, 1189, 1189, 1003, 853, 1228, 138, /* 680 */ 355, 1213, 137, 1228, 140, 343, 1213, 139, 1189, 1189, - /* 690 */ 343, 852, 991, 992, 307, 345, 1189, 146, 1189, 1189, - /* 700 */ 345, 1228, 309, 327, 1189, 328, 1228, 856, 343, 201, + /* 690 */ 343, 852, 991, 992, 308, 345, 1189, 146, 1189, 1189, + /* 700 */ 345, 1228, 310, 327, 1189, 328, 1228, 856, 343, 202, /* 710 */ 1214, 1217, 1002, 343, 107, 1214, 1217, 1189, 345, 9, - /* 720 */ 8, 855, 1189, 345, 92, 227, 864, 1189, 1213, 936, - /* 730 */ 231, 1213, 201, 1214, 1217, 1283, 1213, 201, 1214, 1217, + /* 720 */ 8, 855, 1189, 345, 92, 228, 864, 1189, 1213, 936, + /* 730 */ 232, 1213, 202, 1214, 1217, 1283, 1213, 202, 1214, 1217, /* 740 */ 31, 29, 27, 26, 25, 336, 327, 1167, 1228, 52, - /* 750 */ 90, 1228, 1328, 1189, 166, 343, 1228, 1078, 343, 329, + /* 750 */ 90, 1228, 1328, 1189, 167, 343, 1228, 1078, 343, 329, /* 760 */ 113, 1260, 1261, 343, 1265, 345, 1096, 92, 345, 1189, - /* 770 */ 2, 119, 1189, 345, 1213, 142, 256, 1189, 141, 199, - /* 780 */ 1214, 1217, 202, 1214, 1217, 959, 154, 193, 1214, 1217, - /* 790 */ 248, 255, 257, 90, 1228, 261, 1213, 41, 152, 878, + /* 770 */ 2, 119, 1189, 345, 1213, 142, 257, 1189, 141, 200, + /* 780 */ 1214, 1217, 203, 1214, 1217, 959, 154, 194, 1214, 1217, + /* 790 */ 249, 256, 258, 90, 1228, 262, 1213, 41, 152, 878, /* 800 */ 908, 343, 1213, 115, 1260, 1261, 872, 1265, 994, 995, - /* 810 */ 262, 345, 32, 263, 389, 1189, 1228, 124, 388, 871, - /* 820 */ 875, 58, 1228, 343, 42, 203, 1214, 1217, 1207, 343, - /* 830 */ 1213, 54, 32, 345, 839, 264, 1213, 1189, 267, 345, - /* 840 */ 1205, 390, 129, 1189, 274, 870, 32, 194, 1214, 1217, - /* 850 */ 1228, 276, 68, 204, 1214, 1217, 1228, 343, 172, 1093, - /* 860 */ 387, 386, 219, 343, 1213, 133, 351, 345, 177, 1089, - /* 870 */ 170, 1189, 135, 345, 762, 100, 101, 1189, 95, 1091, + /* 810 */ 263, 345, 32, 264, 389, 1189, 1228, 124, 388, 871, + /* 820 */ 875, 58, 1228, 343, 42, 204, 1214, 1217, 1207, 343, + /* 830 */ 1213, 54, 32, 345, 839, 265, 1213, 1189, 268, 345, + /* 840 */ 1205, 390, 129, 1189, 275, 870, 32, 195, 1214, 1217, + /* 850 */ 1228, 277, 68, 205, 1214, 1217, 1228, 343, 173, 1093, + /* 860 */ 387, 386, 220, 343, 1213, 133, 351, 345, 178, 1089, + /* 870 */ 171, 1189, 135, 345, 762, 100, 101, 1189, 95, 1091, /* 880 */ 96, 1225, 1214, 1217, 1228, 1087, 98, 1224, 1214, 1217, /* 890 */ 757, 343, 1213, 102, 790, 794, 103, 800, 1213, 799, - /* 900 */ 306, 345, 41, 303, 145, 1189, 364, 96, 99, 97, - /* 910 */ 305, 98, 1228, 869, 314, 1223, 1214, 1217, 1228, 343, - /* 920 */ 96, 1284, 349, 1294, 150, 343, 311, 850, 1293, 345, - /* 930 */ 153, 222, 1213, 1189, 5, 345, 324, 110, 1274, 1189, - /* 940 */ 1213, 310, 156, 210, 1214, 1217, 237, 236, 4, 209, - /* 950 */ 1214, 1217, 1228, 933, 91, 868, 858, 1268, 33, 343, - /* 960 */ 1228, 159, 158, 340, 1311, 224, 1329, 343, 1213, 345, - /* 970 */ 337, 165, 851, 1189, 292, 214, 17, 345, 1235, 1176, - /* 980 */ 347, 1189, 348, 211, 1214, 1217, 352, 1175, 1228, 300, - /* 990 */ 232, 208, 1214, 1217, 353, 343, 354, 279, 51, 174, - /* 1000 */ 184, 278, 854, 144, 1104, 345, 295, 53, 186, 1189, - /* 1010 */ 362, 289, 181, 215, 143, 213, 212, 422, 277, 198, - /* 1020 */ 1214, 1217, 196, 197, 280, 188, 189, 1183, 360, 822, - /* 1030 */ 1160, 1159, 94, 1158, 1157, 1156, 1155, 1154, 1153, 40, - /* 1040 */ 824, 1152, 39, 1151, 1150, 1149, 1044, 1182, 1173, 126, - /* 1050 */ 1082, 708, 1043, 1041, 268, 269, 859, 850, 270, 1030, - /* 1060 */ 1029, 1026, 1084, 67, 132, 1083, 805, 804, 1039, 1034, - /* 1070 */ 737, 803, 1032, 1025, 1024, 1181, 65, 736, 735, 216, - /* 1080 */ 734, 1180, 36, 1172, 45, 217, 218, 148, 296, 733, - /* 1090 */ 3, 32, 732, 298, 291, 14, 294, 151, 15, 34, - /* 1100 */ 37, 11, 48, 319, 1205, 8, 19, 161, 894, 350, - /* 1110 */ 147, 176, 998, 998, 998, 998, 998, 318, 1171, 998, - /* 1120 */ 783, 998, 998, 998, 998, 998, 998, 998, 998, 980, - /* 1130 */ 979, 225, 984, 983, 226, 998, 998, 998, 998, 998, - /* 1140 */ 998, 958, 998, 175, 998, 998, 108, 155, 998, 952, - /* 1150 */ 46, 998, 860, 951, 47, 768, 930, 929, 998, 985, - /* 1160 */ 998, 998, 998, 998, 998, 998, 998, 998, 998, 13, - /* 1170 */ 35, 116, 876, 16, 18, 998, 169, 956, 171, 168, - /* 1180 */ 49, 173, 50, 998, 1204, 363, 239, 54, 367, 38, - /* 1190 */ 1040, 179, 370, 373, 998, 998, 998, 998, 376, 998, - /* 1200 */ 998, 796, 998, 791, 365, 998, 788, 728, 720, 998, - /* 1210 */ 420, 848, 361, 785, 368, 798, 797, 782, 998, 998, - /* 1220 */ 727, 371, 998, 998, 779, 726, 55, 374, 706, 777, - /* 1230 */ 391, 725, 724, 1028, 414, 1027, 1023, 723, 413, 998, - /* 1240 */ 377, 998, 722, 721, 56, 57, 719, 718, 187, 729, - /* 1250 */ 421, 717, 716, 715, 714, 713, 712, 383, 424, 711, - /* 1260 */ 412, 425, 998, 416, 417, 998, 998, 781, 780, + /* 900 */ 307, 345, 41, 304, 145, 1189, 364, 96, 99, 97, + /* 910 */ 306, 98, 1228, 869, 315, 1223, 1214, 1217, 1228, 343, + /* 920 */ 96, 1284, 349, 1294, 150, 343, 312, 850, 1293, 345, + /* 930 */ 153, 223, 1213, 1189, 319, 345, 5, 110, 1274, 1189, + /* 940 */ 1213, 324, 157, 211, 1214, 1217, 238, 237, 311, 210, + /* 950 */ 1214, 1217, 1228, 4, 933, 91, 858, 868, 1268, 343, + /* 960 */ 1228, 33, 159, 160, 1311, 225, 1329, 343, 1213, 345, + /* 970 */ 337, 340, 851, 1189, 293, 215, 166, 345, 17, 1235, + /* 980 */ 1176, 1189, 347, 212, 1214, 1217, 348, 233, 1228, 301, + /* 990 */ 1175, 209, 1214, 1217, 352, 343, 353, 280, 354, 175, + /* 1000 */ 51, 279, 854, 144, 185, 345, 296, 187, 53, 1189, + /* 1010 */ 1104, 290, 182, 216, 143, 214, 213, 422, 278, 199, + /* 1020 */ 1214, 1217, 362, 197, 281, 198, 189, 190, 360, 1183, + /* 1030 */ 822, 1160, 1159, 94, 1158, 1157, 1156, 1155, 1154, 40, + /* 1040 */ 1153, 824, 39, 1152, 1151, 1150, 1149, 1044, 1182, 1173, + /* 1050 */ 126, 1082, 708, 1043, 1041, 269, 859, 850, 270, 271, + /* 1060 */ 1030, 1029, 1026, 1084, 67, 132, 805, 1083, 804, 1039, + /* 1070 */ 1034, 737, 803, 1032, 1025, 1024, 1181, 736, 217, 735, + /* 1080 */ 734, 218, 1180, 36, 1172, 45, 219, 148, 297, 3, + /* 1090 */ 733, 65, 32, 732, 299, 14, 292, 151, 958, 15, + /* 1100 */ 1205, 147, 162, 295, 37, 11, 156, 19, 34, 48, + /* 1110 */ 8, 350, 894, 1171, 177, 998, 998, 998, 998, 998, + /* 1120 */ 998, 783, 998, 998, 998, 998, 998, 998, 980, 979, + /* 1130 */ 226, 984, 983, 227, 860, 998, 998, 998, 108, 998, + /* 1140 */ 998, 176, 998, 998, 998, 998, 998, 155, 998, 952, + /* 1150 */ 46, 998, 951, 47, 998, 768, 930, 929, 998, 985, + /* 1160 */ 998, 998, 35, 998, 998, 998, 998, 998, 876, 116, + /* 1170 */ 13, 18, 16, 170, 956, 998, 172, 174, 169, 49, + /* 1180 */ 50, 1204, 998, 998, 54, 38, 363, 1040, 240, 367, + /* 1190 */ 1028, 370, 373, 180, 998, 361, 998, 796, 376, 998, + /* 1200 */ 791, 365, 728, 788, 368, 1027, 785, 998, 798, 371, + /* 1210 */ 420, 720, 797, 779, 998, 998, 998, 727, 782, 998, + /* 1220 */ 726, 998, 998, 725, 724, 706, 781, 374, 391, 777, + /* 1230 */ 723, 722, 721, 377, 719, 1023, 718, 717, 413, 716, + /* 1240 */ 383, 55, 56, 57, 425, 715, 729, 714, 713, 421, + /* 1250 */ 712, 711, 412, 848, 188, 424, 998, 416, 417, 998, + /* 1260 */ 998, 414, 998, 998, 998, 780, }; static const YYCODETYPE yy_lookahead[] = { /* 0 */ 0, 207, 190, 174, 221, 222, 172, 213, 214, 187, @@ -432,40 +432,40 @@ static const YYCODETYPE yy_lookahead[] = { /* 900 */ 204, 203, 83, 211, 171, 207, 83, 83, 71, 83, /* 910 */ 193, 83, 186, 20, 145, 217, 218, 219, 186, 193, /* 920 */ 83, 216, 144, 246, 208, 193, 207, 123, 246, 203, - /* 930 */ 208, 207, 166, 207, 152, 203, 151, 240, 243, 207, - /* 940 */ 166, 140, 242, 217, 218, 219, 12, 13, 139, 217, - /* 950 */ 218, 219, 186, 136, 193, 20, 22, 215, 114, 193, - /* 960 */ 186, 227, 239, 156, 252, 160, 257, 193, 166, 203, - /* 970 */ 154, 251, 38, 207, 4, 35, 70, 203, 223, 208, - /* 980 */ 207, 207, 207, 217, 218, 219, 118, 208, 186, 19, - /* 990 */ 207, 217, 218, 219, 205, 193, 204, 57, 171, 193, - /* 1000 */ 182, 61, 68, 33, 193, 203, 36, 70, 172, 207, - /* 1010 */ 189, 41, 171, 73, 44, 75, 76, 168, 78, 217, - /* 1020 */ 218, 219, 180, 180, 84, 173, 164, 0, 94, 82, - /* 1030 */ 0, 0, 114, 0, 0, 0, 0, 0, 0, 69, - /* 1040 */ 22, 0, 72, 0, 0, 0, 0, 0, 0, 43, - /* 1050 */ 0, 48, 0, 0, 38, 36, 122, 123, 43, 0, - /* 1060 */ 0, 0, 0, 79, 77, 0, 38, 38, 0, 0, - /* 1070 */ 38, 22, 0, 0, 0, 0, 20, 38, 38, 22, - /* 1080 */ 38, 0, 121, 0, 70, 22, 22, 116, 22, 38, - /* 1090 */ 83, 83, 38, 22, 39, 141, 38, 71, 141, 135, - /* 1100 */ 83, 141, 4, 83, 82, 2, 83, 82, 126, 119, - /* 1110 */ 43, 116, 258, 258, 258, 258, 258, 38, 0, 258, - /* 1120 */ 96, 258, 258, 258, 258, 258, 258, 258, 258, 38, - /* 1130 */ 38, 38, 38, 38, 38, 258, 258, 258, 258, 258, - /* 1140 */ 258, 71, 258, 43, 258, 258, 70, 70, 258, 71, - /* 1150 */ 70, 258, 22, 71, 70, 22, 71, 71, 258, 71, - /* 1160 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 70, - /* 1170 */ 83, 82, 71, 83, 70, 258, 71, 71, 70, 82, - /* 1180 */ 70, 70, 70, 258, 82, 38, 38, 80, 38, 70, - /* 1190 */ 0, 82, 38, 38, 258, 258, 258, 258, 38, 258, - /* 1200 */ 258, 22, 258, 71, 70, 258, 71, 22, 22, 258, - /* 1210 */ 22, 22, 81, 71, 70, 38, 38, 96, 258, 258, - /* 1220 */ 38, 70, 258, 258, 71, 38, 70, 70, 48, 71, - /* 1230 */ 47, 38, 38, 0, 43, 0, 0, 38, 36, 258, - /* 1240 */ 70, 258, 38, 38, 70, 70, 38, 38, 22, 68, - /* 1250 */ 21, 38, 38, 38, 38, 38, 38, 84, 21, 38, - /* 1260 */ 38, 20, 258, 38, 37, 258, 258, 96, 96, 258, + /* 930 */ 208, 207, 166, 207, 207, 203, 152, 240, 243, 207, + /* 940 */ 166, 151, 242, 217, 218, 219, 12, 13, 140, 217, + /* 950 */ 218, 219, 186, 139, 136, 193, 22, 20, 215, 193, + /* 960 */ 186, 114, 239, 227, 252, 160, 257, 193, 166, 203, + /* 970 */ 154, 156, 38, 207, 4, 35, 251, 203, 70, 223, + /* 980 */ 208, 207, 207, 217, 218, 219, 207, 207, 186, 19, + /* 990 */ 208, 217, 218, 219, 118, 193, 205, 57, 204, 193, + /* 1000 */ 171, 61, 68, 33, 182, 203, 36, 172, 70, 207, + /* 1010 */ 193, 41, 171, 73, 44, 75, 76, 168, 78, 217, + /* 1020 */ 218, 219, 189, 180, 84, 180, 173, 164, 94, 0, + /* 1030 */ 82, 0, 0, 114, 0, 0, 0, 0, 0, 69, + /* 1040 */ 0, 22, 72, 0, 0, 0, 0, 0, 0, 0, + /* 1050 */ 43, 0, 48, 0, 0, 38, 122, 123, 36, 43, + /* 1060 */ 0, 0, 0, 0, 79, 77, 38, 0, 38, 0, + /* 1070 */ 0, 38, 22, 0, 0, 0, 0, 38, 22, 38, + /* 1080 */ 38, 22, 0, 121, 0, 70, 22, 116, 22, 83, + /* 1090 */ 38, 20, 83, 38, 22, 141, 39, 71, 71, 141, + /* 1100 */ 82, 43, 82, 38, 83, 141, 83, 83, 135, 4, + /* 1110 */ 2, 119, 126, 0, 116, 258, 258, 258, 258, 258, + /* 1120 */ 258, 96, 258, 258, 258, 258, 258, 258, 38, 38, + /* 1130 */ 38, 38, 38, 38, 22, 258, 258, 258, 70, 258, + /* 1140 */ 258, 43, 258, 258, 258, 258, 258, 70, 258, 71, + /* 1150 */ 70, 258, 71, 70, 258, 22, 71, 71, 258, 71, + /* 1160 */ 258, 258, 83, 258, 258, 258, 258, 258, 71, 82, + /* 1170 */ 70, 70, 83, 71, 71, 258, 70, 70, 82, 70, + /* 1180 */ 70, 82, 258, 258, 80, 70, 38, 0, 38, 38, + /* 1190 */ 0, 38, 38, 82, 258, 81, 258, 22, 38, 258, + /* 1200 */ 71, 70, 22, 71, 70, 0, 71, 258, 38, 70, + /* 1210 */ 22, 22, 38, 71, 258, 258, 258, 38, 96, 258, + /* 1220 */ 38, 258, 258, 38, 38, 48, 96, 70, 47, 71, + /* 1230 */ 38, 38, 38, 70, 38, 0, 38, 38, 36, 38, + /* 1240 */ 84, 70, 70, 70, 20, 38, 68, 38, 38, 21, + /* 1250 */ 38, 38, 38, 22, 22, 21, 258, 38, 37, 258, + /* 1260 */ 258, 43, 258, 258, 258, 96, 258, 258, 258, 258, /* 1270 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 258, /* 1280 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 258, /* 1290 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 258, @@ -481,12 +481,11 @@ static const YYCODETYPE yy_lookahead[] = { /* 1390 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 258, /* 1400 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 258, /* 1410 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 258, - /* 1420 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, 258, - /* 1430 */ 258, 258, + /* 1420 */ 258, 258, 258, 258, 258, 258, 258, 258, 258, }; #define YY_SHIFT_COUNT (426) #define YY_SHIFT_MIN (0) -#define YY_SHIFT_MAX (1241) +#define YY_SHIFT_MAX (1235) static const unsigned short int yy_shift_ofst[] = { /* 0 */ 426, 212, 161, 382, 382, 382, 382, 421, 382, 382, /* 10 */ 151, 513, 517, 444, 513, 513, 513, 513, 513, 513, @@ -503,38 +502,38 @@ static const unsigned short int yy_shift_ofst[] = { /* 120 */ 657, 661, 661, 706, 786, 66, 343, 799, 66, 706, /* 130 */ 66, 825, 17, 17, 17, 17, 17, 17, 17, 17, /* 140 */ 17, 17, 17, 706, 825, 786, 278, 343, 799, 893, - /* 150 */ 769, 778, 804, 769, 778, 804, 782, 785, 801, 809, - /* 160 */ 817, 343, 935, 844, 805, 807, 816, 906, 17, 778, - /* 170 */ 804, 804, 778, 804, 868, 343, 799, 306, 278, 343, - /* 180 */ 937, 706, 278, 825, 1269, 1269, 1269, 1269, 0, 545, - /* 190 */ 577, 46, 970, 16, 89, 728, 406, 757, 301, 301, - /* 200 */ 301, 301, 301, 301, 301, 115, 288, 196, 7, 7, - /* 210 */ 7, 7, 374, 605, 610, 701, 453, 580, 632, 506, - /* 220 */ 541, 714, 715, 718, 647, 590, 451, 729, 74, 749, - /* 230 */ 758, 763, 787, 795, 797, 803, 639, 653, 819, 823, - /* 240 */ 824, 826, 828, 837, 751, 1027, 947, 1030, 1031, 918, - /* 250 */ 1033, 1034, 1035, 1036, 1037, 1038, 1018, 1041, 1043, 1044, - /* 260 */ 1045, 1046, 1047, 1048, 1006, 1050, 1003, 1052, 1053, 1016, - /* 270 */ 1019, 1015, 1059, 1060, 1061, 1062, 984, 987, 1028, 1029, - /* 280 */ 1049, 1065, 1032, 1039, 1040, 1042, 1051, 1054, 1068, 1057, - /* 290 */ 1069, 1063, 1055, 1072, 1064, 1058, 1073, 1066, 1074, 1071, - /* 300 */ 1056, 1075, 1081, 961, 1083, 1014, 1067, 971, 1007, 1008, - /* 310 */ 954, 1026, 1017, 1070, 1076, 1077, 1078, 1080, 1082, 1079, - /* 320 */ 1020, 1022, 1084, 1023, 957, 1085, 1086, 1025, 964, 1087, - /* 330 */ 1089, 1088, 1090, 960, 1098, 1091, 1092, 1093, 1094, 1095, - /* 340 */ 1096, 1103, 982, 1097, 1101, 1099, 1104, 1105, 1106, 1108, - /* 350 */ 1111, 990, 1110, 1118, 1100, 995, 1112, 1107, 1102, 1109, - /* 360 */ 1130, 1119, 1131, 1132, 1147, 1148, 1134, 1135, 1150, 1144, - /* 370 */ 1142, 1154, 1151, 1153, 1155, 1157, 1158, 1160, 1170, 1024, - /* 380 */ 1121, 1171, 1172, 1133, 1173, 1156, 1174, 1175, 1177, 1178, - /* 390 */ 1179, 1180, 1183, 1181, 1185, 1182, 1187, 1193, 1194, 1199, - /* 400 */ 1204, 1205, 1186, 1208, 1209, 1213, 1214, 1215, 1216, 1217, - /* 410 */ 1218, 1221, 1190, 1222, 1202, 1191, 1233, 1225, 1227, 1235, - /* 420 */ 1236, 1188, 1229, 1189, 1226, 1237, 1241, + /* 150 */ 769, 778, 804, 769, 778, 804, 804, 784, 790, 808, + /* 160 */ 814, 818, 343, 937, 847, 805, 815, 816, 908, 17, + /* 170 */ 778, 804, 804, 778, 804, 876, 343, 799, 306, 278, + /* 180 */ 343, 938, 706, 278, 825, 1266, 1266, 1266, 1266, 0, + /* 190 */ 545, 577, 46, 970, 16, 89, 728, 406, 757, 301, + /* 200 */ 301, 301, 301, 301, 301, 301, 115, 288, 196, 7, + /* 210 */ 7, 7, 7, 374, 605, 610, 701, 453, 580, 632, + /* 220 */ 506, 541, 714, 715, 718, 647, 590, 451, 729, 74, + /* 230 */ 749, 758, 763, 787, 795, 797, 803, 639, 653, 819, + /* 240 */ 823, 824, 826, 828, 837, 751, 1029, 948, 1031, 1032, + /* 250 */ 919, 1034, 1035, 1036, 1037, 1038, 1040, 1019, 1043, 1044, + /* 260 */ 1045, 1046, 1047, 1048, 1049, 1007, 1051, 1004, 1053, 1054, + /* 270 */ 1017, 1022, 1016, 1060, 1061, 1062, 1063, 985, 988, 1028, + /* 280 */ 1030, 1050, 1067, 1033, 1039, 1041, 1042, 1052, 1055, 1069, + /* 290 */ 1056, 1070, 1059, 1057, 1073, 1064, 1065, 1074, 1066, 1075, + /* 300 */ 1072, 1071, 1076, 1082, 962, 1084, 1015, 1058, 971, 1006, + /* 310 */ 1009, 954, 1026, 1021, 1027, 1068, 1077, 1078, 1080, 1081, + /* 320 */ 1023, 1018, 1083, 1024, 958, 1085, 1086, 1020, 973, 1079, + /* 330 */ 1087, 1088, 1089, 964, 1105, 1090, 1091, 1092, 1093, 1094, + /* 340 */ 1095, 1108, 986, 1096, 1097, 1100, 1101, 1102, 1103, 1106, + /* 350 */ 1107, 992, 1109, 1113, 1098, 998, 1110, 1104, 1099, 1111, + /* 360 */ 1112, 1115, 1114, 1129, 1148, 1150, 1131, 1132, 1151, 1134, + /* 370 */ 1135, 1153, 1139, 1142, 1154, 1157, 1158, 1160, 1163, 1025, + /* 380 */ 1122, 1130, 1169, 1133, 1156, 1171, 1172, 1173, 1170, 1174, + /* 390 */ 1175, 1177, 1181, 1178, 1180, 1179, 1182, 1185, 1186, 1192, + /* 400 */ 1193, 1194, 1189, 1196, 1198, 1199, 1201, 1207, 1209, 1210, + /* 410 */ 1212, 1213, 1187, 1214, 1202, 1218, 1190, 1219, 1221, 1205, + /* 420 */ 1235, 1188, 1228, 1231, 1232, 1234, 1224, }; -#define YY_REDUCE_COUNT (187) +#define YY_REDUCE_COUNT (188) #define YY_REDUCE_MIN (-217) -#define YY_REDUCE_MAX (862) +#define YY_REDUCE_MAX (863) static const short yy_reduce_ofst[] = { /* 0 */ -129, -6, 49, 78, 126, 155, 190, 219, 276, 299, /* 10 */ 135, 375, 398, 434, 492, -80, 497, 515, 520, 562, @@ -551,10 +550,10 @@ static const short yy_reduce_ofst[] = { /* 120 */ 589, 592, 593, 623, 599, 643, 620, 631, 650, 666, /* 130 */ 668, 676, 665, 673, 679, 683, 686, 689, 690, 693, /* 140 */ 699, 707, 710, 680, 694, 692, 733, 717, 696, 705, - /* 150 */ 677, 716, 719, 682, 722, 724, 695, 700, 697, 723, - /* 160 */ 471, 761, 742, 734, 709, 712, 720, 755, 476, 771, - /* 170 */ 773, 775, 779, 783, 789, 806, 792, 818, 827, 811, - /* 180 */ 821, 836, 841, 849, 842, 843, 852, 862, + /* 150 */ 677, 716, 719, 682, 722, 724, 727, 695, 700, 697, + /* 160 */ 723, 471, 762, 743, 736, 709, 712, 725, 756, 476, + /* 170 */ 772, 775, 779, 782, 780, 791, 806, 794, 822, 829, + /* 180 */ 817, 833, 835, 841, 849, 843, 845, 853, 863, }; static const YYACTIONTYPE yy_default[] = { /* 0 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, @@ -572,23 +571,23 @@ static const YYACTIONTYPE yy_default[] = { /* 120 */ 1166, 1163, 1163, 996, 996, 1049, 996, 996, 1049, 996, /* 130 */ 1049, 996, 996, 996, 996, 996, 996, 996, 996, 996, /* 140 */ 996, 996, 996, 996, 996, 996, 1047, 996, 996, 996, - /* 150 */ 1297, 1295, 996, 1297, 1295, 996, 1309, 1305, 1288, 1286, - /* 160 */ 1272, 996, 996, 996, 1333, 1321, 1317, 996, 996, 1295, - /* 170 */ 996, 996, 1295, 996, 1174, 996, 996, 996, 1047, 996, - /* 180 */ 1101, 996, 1047, 996, 1135, 1135, 1050, 1001, 996, 996, - /* 190 */ 996, 996, 996, 996, 996, 996, 996, 996, 1227, 1308, - /* 200 */ 1307, 1226, 1232, 1231, 1230, 996, 996, 996, 1221, 1222, - /* 210 */ 1220, 1219, 996, 996, 996, 996, 996, 996, 996, 996, - /* 220 */ 996, 996, 996, 1256, 996, 1318, 1322, 996, 996, 996, - /* 230 */ 1206, 996, 996, 996, 996, 996, 996, 996, 996, 996, + /* 150 */ 1297, 1295, 996, 1297, 1295, 996, 996, 1309, 1305, 1288, + /* 160 */ 1286, 1272, 996, 996, 996, 1333, 1321, 1317, 996, 996, + /* 170 */ 1295, 996, 996, 1295, 996, 1174, 996, 996, 996, 1047, + /* 180 */ 996, 1101, 996, 1047, 996, 1135, 1135, 1050, 1001, 996, + /* 190 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 1227, + /* 200 */ 1308, 1307, 1226, 1232, 1231, 1230, 996, 996, 996, 1221, + /* 210 */ 1222, 1220, 1219, 996, 996, 996, 996, 996, 996, 996, + /* 220 */ 996, 996, 996, 996, 1256, 996, 1318, 1322, 996, 996, + /* 230 */ 996, 1206, 996, 996, 996, 996, 996, 996, 996, 996, /* 240 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, /* 250 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, /* 260 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, /* 270 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, /* 280 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, /* 290 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, - /* 300 */ 996, 996, 996, 996, 996, 996, 996, 996, 1279, 1289, - /* 310 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 996, + /* 300 */ 996, 996, 996, 996, 996, 996, 996, 996, 996, 1279, + /* 310 */ 1289, 996, 996, 996, 996, 996, 996, 996, 996, 996, /* 320 */ 996, 1206, 996, 1306, 996, 1265, 1261, 996, 996, 1257, /* 330 */ 996, 996, 1316, 996, 996, 996, 996, 996, 996, 996, /* 340 */ 996, 1251, 996, 996, 996, 996, 996, 996, 996, 996, @@ -1263,7 +1262,7 @@ static const char *const yyRuleName[] = { /* 289 */ "partition_by_clause_opt ::=", /* 290 */ "partition_by_clause_opt ::= PARTITION BY expression_list", /* 291 */ "twindow_clause_opt ::=", - /* 292 */ "twindow_clause_opt ::= SESSION NK_LP column_reference NK_COMMA NK_INTEGER NK_RP", + /* 292 */ "twindow_clause_opt ::= SESSION NK_LP column_reference NK_COMMA duration_literal NK_RP", /* 293 */ "twindow_clause_opt ::= STATE_WINDOW NK_LP column_reference NK_RP", /* 294 */ "twindow_clause_opt ::= INTERVAL NK_LP duration_literal NK_RP sliding_opt fill_opt", /* 295 */ "twindow_clause_opt ::= INTERVAL NK_LP duration_literal NK_COMMA duration_literal NK_RP sliding_opt fill_opt", @@ -2163,7 +2162,7 @@ static const struct { { 240, 0 }, /* (289) partition_by_clause_opt ::= */ { 240, -3 }, /* (290) partition_by_clause_opt ::= PARTITION BY expression_list */ { 241, 0 }, /* (291) twindow_clause_opt ::= */ - { 241, -6 }, /* (292) twindow_clause_opt ::= SESSION NK_LP column_reference NK_COMMA NK_INTEGER NK_RP */ + { 241, -6 }, /* (292) twindow_clause_opt ::= SESSION NK_LP column_reference NK_COMMA duration_literal NK_RP */ { 241, -4 }, /* (293) twindow_clause_opt ::= STATE_WINDOW NK_LP column_reference NK_RP */ { 241, -6 }, /* (294) twindow_clause_opt ::= INTERVAL NK_LP duration_literal NK_RP sliding_opt fill_opt */ { 241, -8 }, /* (295) twindow_clause_opt ::= INTERVAL NK_LP duration_literal NK_COMMA duration_literal NK_RP sliding_opt fill_opt */ @@ -3230,8 +3229,8 @@ static YYACTIONTYPE yy_reduce( case 317: /* order_by_clause_opt ::= ORDER BY sort_specification_list */ yytestcase(yyruleno==317); { yymsp[-2].minor.yy136 = yymsp[0].minor.yy136; } break; - case 292: /* twindow_clause_opt ::= SESSION NK_LP column_reference NK_COMMA NK_INTEGER NK_RP */ -{ yymsp[-5].minor.yy140 = createSessionWindowNode(pCxt, releaseRawExprNode(pCxt, yymsp[-3].minor.yy140), &yymsp[-1].minor.yy0); } + case 292: /* twindow_clause_opt ::= SESSION NK_LP column_reference NK_COMMA duration_literal NK_RP */ +{ yymsp[-5].minor.yy140 = createSessionWindowNode(pCxt, releaseRawExprNode(pCxt, yymsp[-3].minor.yy140), yymsp[-1].minor.yy140); } break; case 293: /* twindow_clause_opt ::= STATE_WINDOW NK_LP column_reference NK_RP */ { yymsp[-3].minor.yy140 = createStateWindowNode(pCxt, releaseRawExprNode(pCxt, yymsp[-1].minor.yy140)); } diff --git a/source/libs/parser/test/parserAstTest.cpp b/source/libs/parser/test/parserAstTest.cpp index 8079e4f3bdd0a95767b05ea6e6b03e379956b825..3ffdf4d9b9e98b6923657bbe9c0276d11a738f5b 100644 --- a/source/libs/parser/test/parserAstTest.cpp +++ b/source/libs/parser/test/parserAstTest.cpp @@ -44,6 +44,9 @@ protected: query_ = nullptr; bool res = runImpl(parseCode, translateCode); qDestroyQuery(query_); + if (!res) { + dump(); + } return res; } @@ -53,26 +56,40 @@ private: bool runImpl(int32_t parseCode, int32_t translateCode) { int32_t code = doParse(&cxt_, &query_); if (code != TSDB_CODE_SUCCESS) { - cout << "sql:[" << cxt_.pSql << "] parser code:" << tstrerror(code) << ", msg:" << errMagBuf_ << endl; - return (TSDB_CODE_SUCCESS != parseCode); + parseErrStr_ = string("code:") + tstrerror(code) + string(", msg:") + errMagBuf_; + return (terrno == parseCode); } if (TSDB_CODE_SUCCESS != parseCode) { return false; } - string parserStr = toString(query_->pRoot); + parsedAstStr_ = toString(query_->pRoot); code = doTranslate(&cxt_, query_); if (code != TSDB_CODE_SUCCESS) { - cout << "sql:[" << cxt_.pSql << "] translate code:" << code << ", " << translateCode << ", msg:" << errMagBuf_ << endl; - return (code == translateCode); + translateErrStr_ = string("code:") + tstrerror(code) + string(", msg:") + errMagBuf_; + return (terrno == translateCode); } - cout << "input sql : [" << cxt_.pSql << "]" << endl; - cout << "parser output: " << endl; - cout << parserStr << endl; - cout << "translate output: " << endl; - cout << toString(query_->pRoot) << endl; + translatedAstStr_ = toString(query_->pRoot); return (TSDB_CODE_SUCCESS == translateCode); } + void dump() { + cout << "input sql : [" << cxt_.pSql << "]" << endl; + if (!parseErrStr_.empty()) { + cout << "parse error: " << parseErrStr_ << endl; + } + if (!parsedAstStr_.empty()) { + cout << "parse output: " << endl; + cout << parsedAstStr_ << endl; + } + if (!translateErrStr_.empty()) { + cout << "translate error: " << translateErrStr_ << endl; + } + if (!translatedAstStr_.empty()) { + cout << "translate output: " << endl; + cout << translatedAstStr_ << endl; + } + } + string toString(const SNode* pRoot, bool format = false) { char* pStr = NULL; int32_t len = 0; @@ -91,6 +108,10 @@ private: memset(errMagBuf_, 0, max_err_len); cxt_.pMsg = errMagBuf_; cxt_.msgLen = max_err_len; + parseErrStr_.clear(); + parsedAstStr_.clear(); + translateErrStr_.clear(); + translatedAstStr_.clear(); } string acctId_; @@ -99,8 +120,50 @@ private: string sqlBuf_; SParseContext cxt_; SQuery* query_; + string parseErrStr_; + string parsedAstStr_; + string translateErrStr_; + string translatedAstStr_; }; +TEST_F(ParserTest, createAccount) { + setDatabase("root", "test"); + + bind("create account ac_wxy pass '123456'"); + ASSERT_TRUE(run(TSDB_CODE_PAR_EXPRIE_STATEMENT)); +} + +TEST_F(ParserTest, alterAccount) { + setDatabase("root", "test"); + + bind("alter account ac_wxy pass '123456'"); + ASSERT_TRUE(run(TSDB_CODE_PAR_EXPRIE_STATEMENT)); +} + +TEST_F(ParserTest, createUser) { + setDatabase("root", "test"); + + bind("create user wxy pass '123456'"); + ASSERT_TRUE(run()); +} + +TEST_F(ParserTest, alterUser) { + setDatabase("root", "test"); + + bind("alter user wxy pass '123456'"); + ASSERT_TRUE(run()); + + bind("alter user wxy privilege 'write'"); + ASSERT_TRUE(run()); +} + +TEST_F(ParserTest, dropUser) { + setDatabase("root", "test"); + + bind("drop user wxy"); + ASSERT_TRUE(run()); +} + TEST_F(ParserTest, selectSimple) { setDatabase("root", "test"); @@ -295,12 +358,6 @@ TEST_F(ParserTest, selectSemanticError) { ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION)); } -TEST_F(ParserTest, createUser) { - setDatabase("root", "test"); - - bind("create user wxy pass '123456'"); - ASSERT_TRUE(run()); -} TEST_F(ParserTest, showUsers) { setDatabase("root", "test"); @@ -309,12 +366,7 @@ TEST_F(ParserTest, showUsers) { ASSERT_TRUE(run()); } -TEST_F(ParserTest, alterAccount) { - setDatabase("root", "test"); - bind("alter account ac_wxy pass '123456'"); - ASSERT_TRUE(run(TSDB_CODE_PAR_EXPRIE_STATEMENT)); -} TEST_F(ParserTest, createDnode) { setDatabase("root", "test"); diff --git a/source/libs/planner/inc/planInt.h b/source/libs/planner/inc/planInt.h index be271cf05775ab7bdac5fd393ada78151621c5f6..42449d63d6c2d79ffe586f2d5ee1d96567d3bc85 100644 --- a/source/libs/planner/inc/planInt.h +++ b/source/libs/planner/inc/planInt.h @@ -56,9 +56,10 @@ extern "C" { #define planTrace(param, ...) qTrace("PLAN: " param, __VA_ARGS__) int32_t createLogicPlan(SPlanContext* pCxt, SLogicNode** pLogicNode); -int32_t optimize(SPlanContext* pCxt, SLogicNode* pLogicNode); -int32_t applySplitRule(SSubLogicPlan* pSubplan); -int32_t createPhysiPlan(SPlanContext* pCxt, SLogicNode* pLogicNode, SQueryPlan** pPlan, SArray* pExecNodeList); +int32_t optimizeLogicPlan(SPlanContext* pCxt, SLogicNode* pLogicNode); +int32_t splitLogicPlan(SPlanContext* pCxt, SLogicNode* pLogicNode, SLogicSubplan** pLogicSubplan); +int32_t scaleOutLogicPlan(SPlanContext* pCxt, SLogicSubplan* pLogicSubplan, SQueryLogicPlan** pLogicPlan); +int32_t createPhysiPlan(SPlanContext* pCxt, SQueryLogicPlan* pLogicPlan, SQueryPlan** pPlan, SArray* pExecNodeList); #ifdef __cplusplus } diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index 881db88503f26c7902d665d7d12d0d435a3cc085..84fa52a070e01e7cf203555d5a718b3bf20a9bd8 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -411,6 +411,38 @@ static int32_t createAggLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, return code; } +static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SWindowLogicNode* pWindow, SLogicNode** pLogicNode) { + int32_t code = nodesCollectFuncs(pSelect, fmIsAggFunc, &pWindow->pFuncs); + + if (TSDB_CODE_SUCCESS == code) { + code = rewriteExpr(pWindow->pFuncs, pSelect, SQL_CLAUSE_WINDOW); + } + + if (TSDB_CODE_SUCCESS == code) { + code = createColumnByRewriteExps(pCxt, pWindow->pFuncs, &pWindow->node.pTargets); + } + + if (TSDB_CODE_SUCCESS == code) { + *pLogicNode = (SLogicNode*)pWindow; + } else { + nodesDestroyNode(pWindow); + } + + return code; +} + +static int32_t createWindowLogicNodeBySession(SLogicPlanContext* pCxt, SSessionWindowNode* pSession, SSelectStmt* pSelect, SLogicNode** pLogicNode) { + SWindowLogicNode* pWindow = nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW); + if (NULL == pWindow) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + pWindow->winType = WINDOW_TYPE_SESSION; + pWindow->sessionGap = ((SValueNode*)pSession->pGap)->datum.i; + + return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode); +} + static int32_t createWindowLogicNodeByInterval(SLogicPlanContext* pCxt, SIntervalWindowNode* pInterval, SSelectStmt* pSelect, SLogicNode** pLogicNode) { SWindowLogicNode* pWindow = nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW); if (NULL == pWindow) { @@ -424,34 +456,15 @@ static int32_t createWindowLogicNodeByInterval(SLogicPlanContext* pCxt, SInterva pWindow->sliding = (NULL != pInterval->pSliding ? ((SValueNode*)pInterval->pSliding)->datum.i : pWindow->interval); pWindow->slidingUnit = (NULL != pInterval->pSliding ? ((SValueNode*)pInterval->pSliding)->unit : pWindow->intervalUnit); - int32_t code = TSDB_CODE_SUCCESS; - if (NULL != pInterval->pFill) { pWindow->pFill = nodesCloneNode(pInterval->pFill); if (NULL == pWindow->pFill) { - code = TSDB_CODE_OUT_OF_MEMORY; + nodesDestroyNode(pWindow); + return TSDB_CODE_OUT_OF_MEMORY; } } - if (TSDB_CODE_SUCCESS == code) { - code = nodesCollectFuncs(pSelect, fmIsAggFunc, &pWindow->pFuncs); - } - - if (TSDB_CODE_SUCCESS == code) { - code = rewriteExpr(pWindow->pFuncs, pSelect, SQL_CLAUSE_WINDOW); - } - - if (TSDB_CODE_SUCCESS == code) { - code = createColumnByRewriteExps(pCxt, pWindow->pFuncs, &pWindow->node.pTargets); - } - - if (TSDB_CODE_SUCCESS == code) { - *pLogicNode = (SLogicNode*)pWindow; - } else { - nodesDestroyNode(pWindow); - } - - return code; + return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode); } static int32_t createWindowLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) { @@ -460,8 +473,10 @@ static int32_t createWindowLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSele } switch (nodeType(pSelect->pWindow)) { + case QUERY_NODE_SESSION_WINDOW: + return createWindowLogicNodeBySession(pCxt, (SSessionWindowNode*)pSelect->pWindow, pSelect, pLogicNode); case QUERY_NODE_INTERVAL_WINDOW: - return createWindowLogicNodeByInterval(pCxt, (SIntervalWindowNode*)pSelect->pWindow, pSelect, pLogicNode); + return createWindowLogicNodeByInterval(pCxt, (SIntervalWindowNode*)pSelect->pWindow, pSelect, pLogicNode); default: break; } diff --git a/source/libs/planner/src/planOptimizer.c b/source/libs/planner/src/planOptimizer.c new file mode 100644 index 0000000000000000000000000000000000000000..2a36e38ce1f7f84de807a4f2eeee1229fcdc41ec --- /dev/null +++ b/source/libs/planner/src/planOptimizer.c @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#include "planInt.h" + +int32_t optimizeLogicPlan(SPlanContext* pCxt, SLogicNode* pLogicNode) { + return TSDB_CODE_SUCCESS; +} diff --git a/source/libs/planner/src/planPhysiCreater.c b/source/libs/planner/src/planPhysiCreater.c index a33cb8725356b50924092f2a165a310c58f921ca..60742d3c2e72e1ed5e9c85d065523197b407ab1a 100644 --- a/source/libs/planner/src/planPhysiCreater.c +++ b/source/libs/planner/src/planPhysiCreater.c @@ -28,7 +28,6 @@ typedef struct SPhysiPlanContext { int16_t nextDataBlockId; SArray* pLocationHelper; SArray* pExecNodeList; - int32_t subplanId; } SPhysiPlanContext; static int32_t getSlotKey(SNode* pNode, char* pKey) { @@ -124,35 +123,52 @@ static EDealRes doSetSlotId(SNode* pNode, void* pContext) { return DEAL_RES_CONTINUE; } -static SNode* setNodeSlotId(SPhysiPlanContext* pCxt, int16_t leftDataBlockId, int16_t rightDataBlockId, SNode* pNode) { +static int32_t setNodeSlotId(SPhysiPlanContext* pCxt, int16_t leftDataBlockId, int16_t rightDataBlockId, SNode* pNode, SNode** pOutput) { SNode* pRes = nodesCloneNode(pNode); - CHECK_ALLOC(pRes, NULL); - SSetSlotIdCxt cxt = { .errCode = TSDB_CODE_SUCCESS, .pLeftHash = taosArrayGetP(pCxt->pLocationHelper, leftDataBlockId), - .pRightHash = (rightDataBlockId < 0 ? NULL : taosArrayGetP(pCxt->pLocationHelper, rightDataBlockId)) }; + if (NULL == pRes) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + SSetSlotIdCxt cxt = { + .errCode = TSDB_CODE_SUCCESS, + .pLeftHash = taosArrayGetP(pCxt->pLocationHelper, leftDataBlockId), + .pRightHash = (rightDataBlockId < 0 ? NULL : taosArrayGetP(pCxt->pLocationHelper, rightDataBlockId)) + }; nodesWalkNode(pRes, doSetSlotId, &cxt); if (TSDB_CODE_SUCCESS != cxt.errCode) { nodesDestroyNode(pRes); - return NULL; + return cxt.errCode; } - return pRes; + + *pOutput = pRes; + return TSDB_CODE_SUCCESS; } -static SNodeList* setListSlotId(SPhysiPlanContext* pCxt, int16_t leftDataBlockId, int16_t rightDataBlockId, SNodeList* pList) { +static int32_t setListSlotId(SPhysiPlanContext* pCxt, int16_t leftDataBlockId, int16_t rightDataBlockId, SNodeList* pList, SNodeList** pOutput) { SNodeList* pRes = nodesCloneList(pList); - CHECK_ALLOC(pRes, NULL); - SSetSlotIdCxt cxt = { .errCode = TSDB_CODE_SUCCESS, .pLeftHash = taosArrayGetP(pCxt->pLocationHelper, leftDataBlockId), - .pRightHash = (rightDataBlockId < 0 ? NULL : taosArrayGetP(pCxt->pLocationHelper, rightDataBlockId)) }; + if (NULL == pRes) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + SSetSlotIdCxt cxt = { + .errCode = TSDB_CODE_SUCCESS, + .pLeftHash = taosArrayGetP(pCxt->pLocationHelper, leftDataBlockId), + .pRightHash = (rightDataBlockId < 0 ? NULL : taosArrayGetP(pCxt->pLocationHelper, rightDataBlockId)) + }; nodesWalkList(pRes, doSetSlotId, &cxt); if (TSDB_CODE_SUCCESS != cxt.errCode) { nodesDestroyList(pRes); - return NULL; + return cxt.errCode; } - return pRes; + *pOutput = pRes; + return TSDB_CODE_SUCCESS; } static SPhysiNode* makePhysiNode(SPhysiPlanContext* pCxt, ENodeType type) { SPhysiNode* pPhysiNode = (SPhysiNode*)nodesMakeNode(type); - CHECK_ALLOC(pPhysiNode, NULL); + if (NULL == pPhysiNode) { + return NULL; + } pPhysiNode->pOutputDataBlockDesc = nodesMakeNode(QUERY_NODE_DATABLOCK_DESC); if (NULL == pPhysiNode->pOutputDataBlockDesc) { nodesDestroyNode(pPhysiNode); @@ -165,8 +181,7 @@ static SPhysiNode* makePhysiNode(SPhysiPlanContext* pCxt, ENodeType type) { static int32_t setConditionsSlotId(SPhysiPlanContext* pCxt, const SLogicNode* pLogicNode, SPhysiNode* pPhysiNode) { if (NULL != pLogicNode->pConditions) { - pPhysiNode->pConditions = setNodeSlotId(pCxt, pPhysiNode->pOutputDataBlockDesc->dataBlockId, -1, pLogicNode->pConditions); - CHECK_ALLOC(pPhysiNode->pConditions, TSDB_CODE_OUT_OF_MEMORY); + return setNodeSlotId(pCxt, pPhysiNode->pOutputDataBlockDesc->dataBlockId, -1, pLogicNode->pConditions, &pPhysiNode->pConditions); } return TSDB_CODE_SUCCESS; } @@ -223,24 +238,34 @@ static int32_t createScanCols(SPhysiPlanContext* pCxt, SScanPhysiNode* pScanPhys return TSDB_CODE_SUCCESS; } -static int32_t initScanPhysiNode(SPhysiPlanContext* pCxt, SScanLogicNode* pScanLogicNode, SScanPhysiNode* pScanPhysiNode) { - CHECK_CODE(createScanCols(pCxt, pScanPhysiNode, pScanLogicNode->pScanCols), TSDB_CODE_OUT_OF_MEMORY); - - // Data block describe also needs to be set without scanning column, such as SELECT COUNT(*) FROM t - CHECK_CODE(addDataBlockDesc(pCxt, pScanPhysiNode->pScanCols, pScanPhysiNode->node.pOutputDataBlockDesc), TSDB_CODE_OUT_OF_MEMORY); - - CHECK_CODE(setConditionsSlotId(pCxt, (const SLogicNode*)pScanLogicNode, (SPhysiNode*)pScanPhysiNode), TSDB_CODE_OUT_OF_MEMORY); - - CHECK_CODE(setSlotOutput(pCxt, pScanLogicNode->node.pTargets, pScanPhysiNode->node.pOutputDataBlockDesc), TSDB_CODE_OUT_OF_MEMORY); +static int32_t createScanPhysiNodeFinalize(SPhysiPlanContext* pCxt, SScanLogicNode* pScanLogicNode, SScanPhysiNode* pScanPhysiNode, SPhysiNode** pPhyNode) { + int32_t code = createScanCols(pCxt, pScanPhysiNode, pScanLogicNode->pScanCols); + if (TSDB_CODE_SUCCESS == code) { + // Data block describe also needs to be set without scanning column, such as SELECT COUNT(*) FROM t + code = addDataBlockDesc(pCxt, pScanPhysiNode->pScanCols, pScanPhysiNode->node.pOutputDataBlockDesc); + } + if (TSDB_CODE_SUCCESS == code) { + code = setConditionsSlotId(pCxt, (const SLogicNode*)pScanLogicNode, (SPhysiNode*)pScanPhysiNode); + } + if (TSDB_CODE_SUCCESS == code) { + code = setSlotOutput(pCxt, pScanLogicNode->node.pTargets, pScanPhysiNode->node.pOutputDataBlockDesc); + } + if (TSDB_CODE_SUCCESS == code) { + pScanPhysiNode->uid = pScanLogicNode->pMeta->uid; + pScanPhysiNode->tableType = pScanLogicNode->pMeta->tableType; + pScanPhysiNode->order = TSDB_ORDER_ASC; + pScanPhysiNode->count = 1; + pScanPhysiNode->reverse = 0; + memcpy(&pScanPhysiNode->tableName, &pScanLogicNode->tableName, sizeof(SName)); + } - pScanPhysiNode->uid = pScanLogicNode->pMeta->uid; - pScanPhysiNode->tableType = pScanLogicNode->pMeta->tableType; - pScanPhysiNode->order = TSDB_ORDER_ASC; - pScanPhysiNode->count = 1; - pScanPhysiNode->reverse = 0; - memcpy(&pScanPhysiNode->tableName, &pScanLogicNode->tableName, sizeof(SName)); + if (TSDB_CODE_SUCCESS == code) { + *pPhyNode = (SPhysiNode*)pScanPhysiNode; + } else { + nodesDestroyNode(pScanPhysiNode); + } - return TSDB_CODE_SUCCESS; + return code; } static void vgroupInfoToNodeAddr(const SVgroupInfo* vg, SQueryNodeAddr* pNodeAddr) { @@ -248,30 +273,36 @@ static void vgroupInfoToNodeAddr(const SVgroupInfo* vg, SQueryNodeAddr* pNodeAdd pNodeAddr->epSet = vg->epSet; } -static SPhysiNode* createTagScanPhysiNode(SPhysiPlanContext* pCxt, SScanLogicNode* pScanLogicNode) { +static int32_t createTagScanPhysiNode(SPhysiPlanContext* pCxt, SScanLogicNode* pScanLogicNode, SPhysiNode** pPhyNode) { STagScanPhysiNode* pTagScan = (STagScanPhysiNode*)makePhysiNode(pCxt, QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN); - CHECK_ALLOC(pTagScan, NULL); - CHECK_CODE(initScanPhysiNode(pCxt, pScanLogicNode, (SScanPhysiNode*)pTagScan), (SPhysiNode*)pTagScan); - return (SPhysiNode*)pTagScan; + if (NULL == pTagScan) { + return TSDB_CODE_OUT_OF_MEMORY; + } + return createScanPhysiNodeFinalize(pCxt, pScanLogicNode, (SScanPhysiNode*)pTagScan, pPhyNode); } -static SPhysiNode* createTableScanPhysiNode(SPhysiPlanContext* pCxt, SSubplan* pSubplan, SScanLogicNode* pScanLogicNode) { +static int32_t createTableScanPhysiNode(SPhysiPlanContext* pCxt, SSubplan* pSubplan, SScanLogicNode* pScanLogicNode, SPhysiNode** pPhyNode) { STableScanPhysiNode* pTableScan = (STableScanPhysiNode*)makePhysiNode(pCxt, QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN); - CHECK_ALLOC(pTableScan, NULL); - CHECK_CODE(initScanPhysiNode(pCxt, pScanLogicNode, (SScanPhysiNode*)pTableScan), (SPhysiNode*)pTableScan); + if (NULL == pTableScan) { + return TSDB_CODE_OUT_OF_MEMORY; + } + pTableScan->scanFlag = pScanLogicNode->scanFlag; pTableScan->scanRange = pScanLogicNode->scanRange; vgroupInfoToNodeAddr(pScanLogicNode->pVgroupList->vgroups, &pSubplan->execNode); taosArrayPush(pCxt->pExecNodeList, &pSubplan->execNode); pSubplan->execNodeStat.tableNum = pScanLogicNode->pVgroupList->vgroups[0].numOfTable; tNameGetFullDbName(&pScanLogicNode->tableName, pSubplan->dbFName); - return (SPhysiNode*)pTableScan; + + return createScanPhysiNodeFinalize(pCxt, pScanLogicNode, (SScanPhysiNode*)pTableScan, pPhyNode); } -static SPhysiNode* createSystemTableScanPhysiNode(SPhysiPlanContext* pCxt, SSubplan* pSubplan, SScanLogicNode* pScanLogicNode) { +static int32_t createSystemTableScanPhysiNode(SPhysiPlanContext* pCxt, SSubplan* pSubplan, SScanLogicNode* pScanLogicNode, SPhysiNode** pPhyNode) { SSystemTableScanPhysiNode* pScan = (SSystemTableScanPhysiNode*)makePhysiNode(pCxt, QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN); - CHECK_ALLOC(pScan, NULL); - CHECK_CODE(initScanPhysiNode(pCxt, pScanLogicNode, (SScanPhysiNode*)pScan), (SPhysiNode*)pScan); + if (NULL == pScan) { + return TSDB_CODE_OUT_OF_MEMORY; + } + if (0 == strcmp(pScanLogicNode->tableName.tname, TSDB_INS_TABLE_USER_TABLES)) { vgroupInfoToNodeAddr(pScanLogicNode->pVgroupList->vgroups, &pSubplan->execNode); taosArrayPush(pCxt->pExecNodeList, &pSubplan->execNode); @@ -284,88 +315,104 @@ static SPhysiNode* createSystemTableScanPhysiNode(SPhysiPlanContext* pCxt, SSubp } pScan->mgmtEpSet = pCxt->pPlanCxt->mgmtEpSet; tNameGetFullDbName(&pScanLogicNode->tableName, pSubplan->dbFName); - return (SPhysiNode*)pScan; + + return createScanPhysiNodeFinalize(pCxt, pScanLogicNode, (SScanPhysiNode*)pScan, pPhyNode); } -static SPhysiNode* createStreamScanPhysiNode(SPhysiPlanContext* pCxt, SSubplan* pSubplan, SScanLogicNode* pScanLogicNode) { - SStreamScanPhysiNode* pTableScan = (SStreamScanPhysiNode*)makePhysiNode(pCxt, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN); - CHECK_ALLOC(pTableScan, NULL); - CHECK_CODE(initScanPhysiNode(pCxt, pScanLogicNode, (SScanPhysiNode*)pTableScan), (SPhysiNode*)pTableScan); - return (SPhysiNode*)pTableScan; +static int32_t createStreamScanPhysiNode(SPhysiPlanContext* pCxt, SSubplan* pSubplan, SScanLogicNode* pScanLogicNode, SPhysiNode** pPhyNode) { + SStreamScanPhysiNode* pScan = (SStreamScanPhysiNode*)makePhysiNode(pCxt, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN); + if (NULL == pScan) { + return TSDB_CODE_OUT_OF_MEMORY; + } + return createScanPhysiNodeFinalize(pCxt, pScanLogicNode, (SScanPhysiNode*)pScan, pPhyNode); } -static SPhysiNode* createScanPhysiNode(SPhysiPlanContext* pCxt, SSubplan* pSubplan, SScanLogicNode* pScanLogicNode) { +static int32_t createScanPhysiNode(SPhysiPlanContext* pCxt, SSubplan* pSubplan, SScanLogicNode* pScanLogicNode, SPhysiNode** pPhyNode) { switch (pScanLogicNode->scanType) { case SCAN_TYPE_TAG: - return createTagScanPhysiNode(pCxt, pScanLogicNode); + return createTagScanPhysiNode(pCxt, pScanLogicNode, pPhyNode); case SCAN_TYPE_TABLE: - return createTableScanPhysiNode(pCxt, pSubplan, pScanLogicNode); + return createTableScanPhysiNode(pCxt, pSubplan, pScanLogicNode, pPhyNode); case SCAN_TYPE_SYSTEM_TABLE: - return createSystemTableScanPhysiNode(pCxt, pSubplan, pScanLogicNode); + return createSystemTableScanPhysiNode(pCxt, pSubplan, pScanLogicNode, pPhyNode); case SCAN_TYPE_STREAM: - return createStreamScanPhysiNode(pCxt, pSubplan, pScanLogicNode); + return createStreamScanPhysiNode(pCxt, pSubplan, pScanLogicNode, pPhyNode); default: break; } - return NULL; + return TSDB_CODE_FAILED; } -static SNodeList* createJoinOutputCols(SPhysiPlanContext* pCxt, SDataBlockDescNode* pLeftDesc, SDataBlockDescNode* pRightDesc) { - SNodeList* pCols = nodesMakeList(); - CHECK_ALLOC(pCols, NULL); +static int32_t createColFromDataBlockDesc(SDataBlockDescNode* pDesc, SNodeList* pCols) { SNode* pNode; - FOREACH(pNode, pLeftDesc->pSlots) { + FOREACH(pNode, pDesc->pSlots) { SSlotDescNode* pSlot = (SSlotDescNode*)pNode; SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN); if (NULL == pCol) { - goto error; + return TSDB_CODE_OUT_OF_MEMORY; } pCol->node.resType = pSlot->dataType; - pCol->dataBlockId = pLeftDesc->dataBlockId; + pCol->dataBlockId = pDesc->dataBlockId; pCol->slotId = pSlot->slotId; pCol->colId = -1; - if (TSDB_CODE_SUCCESS != nodesListAppend(pCols, (SNode*)pCol)) { - goto error; + int32_t code = nodesListStrictAppend(pCols, pCol); + if (TSDB_CODE_SUCCESS != code) { + return code; } } - FOREACH(pNode, pRightDesc->pSlots) { - SSlotDescNode* pSlot = (SSlotDescNode*)pNode; - SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN); - if (NULL == pCol) { - goto error; - } - pCol->node.resType = pSlot->dataType; - pCol->dataBlockId = pRightDesc->dataBlockId; - pCol->slotId = pSlot->slotId; - pCol->colId = -1; - if (TSDB_CODE_SUCCESS != nodesListAppend(pCols, (SNode*)pCol)) { - goto error; - } + return TSDB_CODE_SUCCESS; +} + +static int32_t createJoinOutputCols(SPhysiPlanContext* pCxt, SDataBlockDescNode* pLeftDesc, SDataBlockDescNode* pRightDesc, SNodeList** pList) { + SNodeList* pCols = nodesMakeList(); + if (NULL == pCols) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + int32_t code = createColFromDataBlockDesc(pLeftDesc, pCols); + if (TSDB_CODE_SUCCESS == code) { + code = createColFromDataBlockDesc(pRightDesc, pCols); + } + + if (TSDB_CODE_SUCCESS == code) { + *pList = pCols; + } else { + nodesDestroyList(pCols); } - return pCols; -error: - nodesDestroyList(pCols); - return NULL; + + return code; } -static SPhysiNode* createJoinPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SJoinLogicNode* pJoinLogicNode) { +static int32_t createJoinPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SJoinLogicNode* pJoinLogicNode, SPhysiNode** pPhyNode) { SJoinPhysiNode* pJoin = (SJoinPhysiNode*)makePhysiNode(pCxt, QUERY_NODE_PHYSICAL_PLAN_JOIN); - CHECK_ALLOC(pJoin, NULL); + if (NULL == pJoin) { + return TSDB_CODE_OUT_OF_MEMORY; + } SDataBlockDescNode* pLeftDesc = ((SPhysiNode*)nodesListGetNode(pChildren, 0))->pOutputDataBlockDesc; SDataBlockDescNode* pRightDesc = ((SPhysiNode*)nodesListGetNode(pChildren, 1))->pOutputDataBlockDesc; - pJoin->pOnConditions = setNodeSlotId(pCxt, pLeftDesc->dataBlockId, pRightDesc->dataBlockId, pJoinLogicNode->pOnConditions); - CHECK_ALLOC(pJoin->pOnConditions, (SPhysiNode*)pJoin); - - pJoin->pTargets = createJoinOutputCols(pCxt, pLeftDesc, pRightDesc); - CHECK_ALLOC(pJoin->pTargets, (SPhysiNode*)pJoin); - CHECK_CODE(addDataBlockDesc(pCxt, pJoin->pTargets, pJoin->node.pOutputDataBlockDesc), (SPhysiNode*)pJoin); - CHECK_CODE(setConditionsSlotId(pCxt, (const SLogicNode*)pJoinLogicNode, (SPhysiNode*)pJoin), (SPhysiNode*)pJoin); + int32_t code = setNodeSlotId(pCxt, pLeftDesc->dataBlockId, pRightDesc->dataBlockId, pJoinLogicNode->pOnConditions, &pJoin->pOnConditions); + if (TSDB_CODE_SUCCESS == code) { + code = createJoinOutputCols(pCxt, pLeftDesc, pRightDesc, &pJoin->pTargets); + } + if (TSDB_CODE_SUCCESS == code) { + code = addDataBlockDesc(pCxt, pJoin->pTargets, pJoin->node.pOutputDataBlockDesc); + } + if (TSDB_CODE_SUCCESS == code) { + code = setConditionsSlotId(pCxt, (const SLogicNode*)pJoinLogicNode, (SPhysiNode*)pJoin); + } + if (TSDB_CODE_SUCCESS == code) { + code = setSlotOutput(pCxt, pJoinLogicNode->node.pTargets, pJoin->node.pOutputDataBlockDesc); + } - CHECK_CODE(setSlotOutput(pCxt, pJoinLogicNode->node.pTargets, pJoin->node.pOutputDataBlockDesc), (SPhysiNode*)pJoin); + if (TSDB_CODE_SUCCESS == code) { + *pPhyNode = (SPhysiNode*)pJoin; + } else { + nodesDestroyNode(pJoin); + } - return (SPhysiNode*)pJoin; + return code; } typedef struct SRewritePrecalcExprsCxt { @@ -451,76 +498,169 @@ static int32_t rewritePrecalcExprs(SPhysiPlanContext* pCxt, SNodeList* pList, SN return cxt.errCode; } -static SPhysiNode* createAggPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SAggLogicNode* pAggLogicNode) { +static int32_t createAggPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SAggLogicNode* pAggLogicNode, SPhysiNode** pPhyNode) { SAggPhysiNode* pAgg = (SAggPhysiNode*)makePhysiNode(pCxt, QUERY_NODE_PHYSICAL_PLAN_AGG); - CHECK_ALLOC(pAgg, NULL); + if (NULL == pAgg) { + return TSDB_CODE_OUT_OF_MEMORY; + } SNodeList* pPrecalcExprs = NULL; SNodeList* pGroupKeys = NULL; SNodeList* pAggFuncs = NULL; - CHECK_CODE(rewritePrecalcExprs(pCxt, pAggLogicNode->pGroupKeys, &pPrecalcExprs, &pGroupKeys), (SPhysiNode*)pAgg); - CHECK_CODE(rewritePrecalcExprs(pCxt, pAggLogicNode->pAggFuncs, &pPrecalcExprs, &pAggFuncs), (SPhysiNode*)pAgg); + int32_t code = rewritePrecalcExprs(pCxt, pAggLogicNode->pGroupKeys, &pPrecalcExprs, &pGroupKeys); + if (TSDB_CODE_SUCCESS == code) { + code = rewritePrecalcExprs(pCxt, pAggLogicNode->pAggFuncs, &pPrecalcExprs, &pAggFuncs); + } SDataBlockDescNode* pChildTupe = (((SPhysiNode*)nodesListGetNode(pChildren, 0))->pOutputDataBlockDesc); // push down expression to pOutputDataBlockDesc of child node - if (NULL != pPrecalcExprs) { - pAgg->pExprs = setListSlotId(pCxt, pChildTupe->dataBlockId, -1, pPrecalcExprs); - CHECK_ALLOC(pAgg->pExprs, (SPhysiNode*)pAgg); - CHECK_CODE(addDataBlockDesc(pCxt, pAgg->pExprs, pChildTupe), (SPhysiNode*)pAgg); + if (TSDB_CODE_SUCCESS == code && NULL != pPrecalcExprs) { + code = setListSlotId(pCxt, pChildTupe->dataBlockId, -1, pPrecalcExprs, &pAgg->pExprs); + if (TSDB_CODE_SUCCESS == code) { + code = addDataBlockDesc(pCxt, pAgg->pExprs, pChildTupe); + } } - if (NULL != pGroupKeys) { - pAgg->pGroupKeys = setListSlotId(pCxt, pChildTupe->dataBlockId, -1, pGroupKeys); - CHECK_ALLOC(pAgg->pGroupKeys, (SPhysiNode*)pAgg); - CHECK_CODE(addDataBlockDesc(pCxt, pAgg->pGroupKeys, pAgg->node.pOutputDataBlockDesc), (SPhysiNode*)pAgg); + if (TSDB_CODE_SUCCESS == code && NULL != pGroupKeys) { + code = setListSlotId(pCxt, pChildTupe->dataBlockId, -1, pGroupKeys, &pAgg->pGroupKeys); + if (TSDB_CODE_SUCCESS == code) { + code = addDataBlockDesc(pCxt, pAgg->pGroupKeys, pAgg->node.pOutputDataBlockDesc); + } } - if (NULL != pAggFuncs) { - pAgg->pAggFuncs = setListSlotId(pCxt, pChildTupe->dataBlockId, -1, pAggFuncs); - CHECK_ALLOC(pAgg->pAggFuncs, (SPhysiNode*)pAgg); - CHECK_CODE(addDataBlockDesc(pCxt, pAgg->pAggFuncs, pAgg->node.pOutputDataBlockDesc), (SPhysiNode*)pAgg); + if (TSDB_CODE_SUCCESS == code && NULL != pAggFuncs) { + code = setListSlotId(pCxt, pChildTupe->dataBlockId, -1, pAggFuncs, &pAgg->pAggFuncs); + if (TSDB_CODE_SUCCESS == code) { + code = addDataBlockDesc(pCxt, pAgg->pAggFuncs, pAgg->node.pOutputDataBlockDesc); + } } - CHECK_CODE(setConditionsSlotId(pCxt, (const SLogicNode*)pAggLogicNode, (SPhysiNode*)pAgg), (SPhysiNode*)pAgg); + if (TSDB_CODE_SUCCESS == code) { + code = setConditionsSlotId(pCxt, (const SLogicNode*)pAggLogicNode, (SPhysiNode*)pAgg); + } + if (TSDB_CODE_SUCCESS == code) { + code = setSlotOutput(pCxt, pAggLogicNode->node.pTargets, pAgg->node.pOutputDataBlockDesc); + } - CHECK_CODE(setSlotOutput(pCxt, pAggLogicNode->node.pTargets, pAgg->node.pOutputDataBlockDesc), (SPhysiNode*)pAgg); + if (TSDB_CODE_SUCCESS == code) { + *pPhyNode = (SPhysiNode*)pAgg; + } else { + nodesDestroyNode(pAgg); + } - return (SPhysiNode*)pAgg; + return code; } -static SPhysiNode* createProjectPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SProjectLogicNode* pProjectLogicNode) { +static int32_t createProjectPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SProjectLogicNode* pProjectLogicNode, SPhysiNode** pPhyNode) { SProjectPhysiNode* pProject = (SProjectPhysiNode*)makePhysiNode(pCxt, QUERY_NODE_PHYSICAL_PLAN_PROJECT); - CHECK_ALLOC(pProject, NULL); + if (NULL == pProject) { + return TSDB_CODE_OUT_OF_MEMORY; + } - pProject->pProjections = setListSlotId(pCxt, ((SPhysiNode*)nodesListGetNode(pChildren, 0))->pOutputDataBlockDesc->dataBlockId, -1, pProjectLogicNode->pProjections); - CHECK_ALLOC(pProject->pProjections, (SPhysiNode*)pProject); - CHECK_CODE(addDataBlockDesc(pCxt, pProject->pProjections, pProject->node.pOutputDataBlockDesc), (SPhysiNode*)pProject); + int32_t code = setListSlotId(pCxt, ((SPhysiNode*)nodesListGetNode(pChildren, 0))->pOutputDataBlockDesc->dataBlockId, -1, pProjectLogicNode->pProjections, &pProject->pProjections); + if (TSDB_CODE_SUCCESS == code) { + code = addDataBlockDesc(pCxt, pProject->pProjections, pProject->node.pOutputDataBlockDesc); + } + if (TSDB_CODE_SUCCESS == code) { + code = setConditionsSlotId(pCxt, (const SLogicNode*)pProjectLogicNode, (SPhysiNode*)pProject); + } - CHECK_CODE(setConditionsSlotId(pCxt, (const SLogicNode*)pProjectLogicNode, (SPhysiNode*)pProject), (SPhysiNode*)pProject); + if (TSDB_CODE_SUCCESS == code) { + *pPhyNode = (SPhysiNode*)pProject; + } else { + nodesDestroyNode(pProject); + } - return (SPhysiNode*)pProject; + return code; } -static SPhysiNode* createExchangePhysiNode(SPhysiPlanContext* pCxt, SExchangeLogicNode* pExchangeLogicNode) { - if (pCxt->pPlanCxt->streamQuery) { - SStreamScanPhysiNode* pScan = (SStreamScanPhysiNode*)makePhysiNode(pCxt, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN); - CHECK_ALLOC(pScan, NULL); +static int32_t doCreateExchangePhysiNode(SPhysiPlanContext* pCxt, SExchangeLogicNode* pExchangeLogicNode, SPhysiNode** pPhyNode) { + SExchangePhysiNode* pExchange = (SExchangePhysiNode*)makePhysiNode(pCxt, QUERY_NODE_PHYSICAL_PLAN_EXCHANGE); + if (NULL == pExchange) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + pExchange->srcGroupId = pExchangeLogicNode->srcGroupId; + int32_t code = addDataBlockDesc(pCxt, pExchangeLogicNode->node.pTargets, pExchange->node.pOutputDataBlockDesc); + + if (TSDB_CODE_SUCCESS == code) { + *pPhyNode = (SPhysiNode*)pExchange; + } else { + nodesDestroyNode(pExchange); + } + + return code; +} +static int32_t createStreamScanPhysiNodeByExchange(SPhysiPlanContext* pCxt, SExchangeLogicNode* pExchangeLogicNode, SPhysiNode** pPhyNode) { + SStreamScanPhysiNode* pScan = (SStreamScanPhysiNode*)makePhysiNode(pCxt, QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN); + if (NULL == pScan) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + int32_t code = addDataBlockDesc(pCxt, pExchangeLogicNode->node.pTargets, pScan->node.pOutputDataBlockDesc); + if (TSDB_CODE_SUCCESS == code) { pScan->pScanCols = nodesCloneList(pExchangeLogicNode->node.pTargets); - CHECK_ALLOC(pScan->pScanCols, (SPhysiNode*)pScan); - CHECK_CODE(addDataBlockDesc(pCxt, pExchangeLogicNode->node.pTargets, pScan->node.pOutputDataBlockDesc), (SPhysiNode*)pScan); - return (SPhysiNode*)pScan; + if (NULL == pScan->pScanCols) { + code = TSDB_CODE_OUT_OF_MEMORY; + } + } + + if (TSDB_CODE_SUCCESS == code) { + *pPhyNode = (SPhysiNode*)pScan; + } else { + nodesDestroyNode(pScan); + } + + return code; +} + +static int32_t createExchangePhysiNode(SPhysiPlanContext* pCxt, SExchangeLogicNode* pExchangeLogicNode, SPhysiNode** pPhyNode) { + if (pCxt->pPlanCxt->streamQuery) { + return createStreamScanPhysiNodeByExchange(pCxt, pExchangeLogicNode, pPhyNode); } else { - SExchangePhysiNode* pExchange = (SExchangePhysiNode*)makePhysiNode(pCxt, QUERY_NODE_PHYSICAL_PLAN_EXCHANGE); - CHECK_ALLOC(pExchange, NULL); - CHECK_CODE(addDataBlockDesc(pCxt, pExchangeLogicNode->node.pTargets, pExchange->node.pOutputDataBlockDesc), (SPhysiNode*)pExchange); - pExchange->srcGroupId = pExchangeLogicNode->srcGroupId; - return (SPhysiNode*)pExchange; + return doCreateExchangePhysiNode(pCxt, pExchangeLogicNode, pPhyNode); } } -static SPhysiNode* createIntervalPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SWindowLogicNode* pWindowLogicNode) { +static int32_t createWindowPhysiNodeFinalize(SPhysiPlanContext* pCxt, SNodeList* pChildren, SWinodwPhysiNode* pWindow, SWindowLogicNode* pWindowLogicNode, SPhysiNode** pPhyNode) { + SNodeList* pPrecalcExprs = NULL; + SNodeList* pFuncs = NULL; + int32_t code = rewritePrecalcExprs(pCxt, pWindowLogicNode->pFuncs, &pPrecalcExprs, &pFuncs); + + SDataBlockDescNode* pChildTupe = (((SPhysiNode*)nodesListGetNode(pChildren, 0))->pOutputDataBlockDesc); + // push down expression to pOutputDataBlockDesc of child node + if (TSDB_CODE_SUCCESS == code && NULL != pPrecalcExprs) { + code = setListSlotId(pCxt, pChildTupe->dataBlockId, -1, pPrecalcExprs, &pWindow->pExprs); + if (TSDB_CODE_SUCCESS == code) { + code = addDataBlockDesc(pCxt, pWindow->pExprs, pChildTupe); + } + } + + if (TSDB_CODE_SUCCESS == code && NULL != pFuncs) { + code = setListSlotId(pCxt, pChildTupe->dataBlockId, -1, pFuncs, &pWindow->pFuncs); + if (TSDB_CODE_SUCCESS == code) { + code = addDataBlockDesc(pCxt, pWindow->pFuncs, pWindow->node.pOutputDataBlockDesc); + } + } + + if (TSDB_CODE_SUCCESS == code) { + code = setSlotOutput(pCxt, pWindowLogicNode->node.pTargets, pWindow->node.pOutputDataBlockDesc); + } + + if (TSDB_CODE_SUCCESS == code) { + *pPhyNode = (SPhysiNode*)pWindow; + } else { + nodesDestroyNode(pWindow); + } + + return code; +} + +static int32_t createIntervalPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SWindowLogicNode* pWindowLogicNode, SPhysiNode** pPhyNode) { SIntervalPhysiNode* pInterval = (SIntervalPhysiNode*)makePhysiNode(pCxt, QUERY_NODE_PHYSICAL_PLAN_INTERVAL); - CHECK_ALLOC(pInterval, NULL); + if (NULL == pInterval) { + return TSDB_CODE_OUT_OF_MEMORY; + } pInterval->interval = pWindowLogicNode->interval; pInterval->offset = pWindowLogicNode->offset; @@ -529,163 +669,178 @@ static SPhysiNode* createIntervalPhysiNode(SPhysiPlanContext* pCxt, SNodeList* p pInterval->slidingUnit = pWindowLogicNode->slidingUnit; pInterval->pFill = nodesCloneNode(pWindowLogicNode->pFill); - - SNodeList* pPrecalcExprs = NULL; - SNodeList* pFuncs = NULL; - CHECK_CODE(rewritePrecalcExprs(pCxt, pWindowLogicNode->pFuncs, &pPrecalcExprs, &pFuncs), (SPhysiNode*)pInterval); - - SDataBlockDescNode* pChildTupe = (((SPhysiNode*)nodesListGetNode(pChildren, 0))->pOutputDataBlockDesc); - // push down expression to pOutputDataBlockDesc of child node - if (NULL != pPrecalcExprs) { - pInterval->pExprs = setListSlotId(pCxt, pChildTupe->dataBlockId, -1, pPrecalcExprs); - CHECK_ALLOC(pInterval->pExprs, (SPhysiNode*)pInterval); - CHECK_CODE(addDataBlockDesc(pCxt, pInterval->pExprs, pChildTupe), (SPhysiNode*)pInterval); + if (NULL == pInterval->pFill) { + nodesDestroyNode(pInterval); + return TSDB_CODE_OUT_OF_MEMORY; } - if (NULL != pFuncs) { - pInterval->pFuncs = setListSlotId(pCxt, pChildTupe->dataBlockId, -1, pFuncs); - CHECK_ALLOC(pInterval->pFuncs, (SPhysiNode*)pInterval); - CHECK_CODE(addDataBlockDesc(pCxt, pInterval->pFuncs, pInterval->node.pOutputDataBlockDesc), (SPhysiNode*)pInterval); + return createWindowPhysiNodeFinalize(pCxt, pChildren, &pInterval->window, pWindowLogicNode, pPhyNode); +} + +static int32_t createSessionWindowPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SWindowLogicNode* pWindowLogicNode, SPhysiNode** pPhyNode) { + SSessionWinodwPhysiNode* pSession = (SSessionWinodwPhysiNode*)makePhysiNode(pCxt, QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW); + if (NULL == pSession) { + return TSDB_CODE_OUT_OF_MEMORY; } - CHECK_CODE(setSlotOutput(pCxt, pWindowLogicNode->node.pTargets, pInterval->node.pOutputDataBlockDesc), (SPhysiNode*)pInterval); + pSession->gap = pWindowLogicNode->sessionGap; - return (SPhysiNode*)pInterval; + return createWindowPhysiNodeFinalize(pCxt, pChildren, &pSession->window, pWindowLogicNode, pPhyNode); } -static SPhysiNode* createWindowPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SWindowLogicNode* pWindowLogicNode) { +static int32_t createWindowPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SWindowLogicNode* pWindowLogicNode, SPhysiNode** pPhyNode) { switch (pWindowLogicNode->winType) { case WINDOW_TYPE_INTERVAL: - return createIntervalPhysiNode(pCxt, pChildren, pWindowLogicNode); + return createIntervalPhysiNode(pCxt, pChildren, pWindowLogicNode, pPhyNode); case WINDOW_TYPE_SESSION: + return createSessionWindowPhysiNode(pCxt, pChildren, pWindowLogicNode, pPhyNode); case WINDOW_TYPE_STATE: break; default: break; } - return NULL; + return TSDB_CODE_FAILED; } -static SPhysiNode* createPhysiNode(SPhysiPlanContext* pCxt, SSubplan* pSubplan, SLogicNode* pLogicPlan) { - SNodeList* pChildren = nodesMakeList(); - CHECK_ALLOC(pChildren, NULL); - - SNode* pLogicChild; - FOREACH(pLogicChild, pLogicPlan->pChildren) { - if (TSDB_CODE_SUCCESS != nodesListStrictAppend(pChildren, createPhysiNode(pCxt, pSubplan, (SLogicNode*)pLogicChild))) { - pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY; - nodesDestroyList(pChildren); - return NULL; - } - } - - SPhysiNode* pPhyNode = NULL; - switch (nodeType(pLogicPlan)) { +static int32_t doCreatePhysiNode(SPhysiPlanContext* pCxt, SLogicNode* pLogicNode, SSubplan* pSubplan, SNodeList* pChildren, SPhysiNode** pPhyNode) { + switch (nodeType(pLogicNode)) { case QUERY_NODE_LOGIC_PLAN_SCAN: - pPhyNode = createScanPhysiNode(pCxt, pSubplan, (SScanLogicNode*)pLogicPlan); - break; + return createScanPhysiNode(pCxt, pSubplan, (SScanLogicNode*)pLogicNode, pPhyNode); case QUERY_NODE_LOGIC_PLAN_JOIN: - pPhyNode = createJoinPhysiNode(pCxt, pChildren, (SJoinLogicNode*)pLogicPlan); - break; + return createJoinPhysiNode(pCxt, pChildren, (SJoinLogicNode*)pLogicNode, pPhyNode); case QUERY_NODE_LOGIC_PLAN_AGG: - pPhyNode = createAggPhysiNode(pCxt, pChildren, (SAggLogicNode*)pLogicPlan); - break; + return createAggPhysiNode(pCxt, pChildren, (SAggLogicNode*)pLogicNode, pPhyNode); case QUERY_NODE_LOGIC_PLAN_PROJECT: - pPhyNode = createProjectPhysiNode(pCxt, pChildren, (SProjectLogicNode*)pLogicPlan); - break; + return createProjectPhysiNode(pCxt, pChildren, (SProjectLogicNode*)pLogicNode, pPhyNode); case QUERY_NODE_LOGIC_PLAN_EXCHANGE: - pPhyNode = createExchangePhysiNode(pCxt, (SExchangeLogicNode*)pLogicPlan); - break; + return createExchangePhysiNode(pCxt, (SExchangeLogicNode*)pLogicNode, pPhyNode); case QUERY_NODE_LOGIC_PLAN_WINDOW: - pPhyNode = createWindowPhysiNode(pCxt, pChildren, (SWindowLogicNode*)pLogicPlan); - break; + return createWindowPhysiNode(pCxt, pChildren, (SWindowLogicNode*)pLogicNode, pPhyNode); default: break; } - if (TSDB_CODE_SUCCESS != pCxt->errCode) { - nodesDestroyNode(pPhyNode); - return NULL; + + return TSDB_CODE_FAILED; +} + +static int32_t createPhysiNode(SPhysiPlanContext* pCxt, SLogicNode* pLogicNode, SSubplan* pSubplan, SPhysiNode** pPhyNode) { + SNodeList* pChildren = nodesMakeList(); + if (NULL == pChildren) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + int32_t code = TSDB_CODE_SUCCESS; + + SNode* pLogicChild; + FOREACH(pLogicChild, pLogicNode->pChildren) { + SPhysiNode* pChild = NULL; + code = createPhysiNode(pCxt, (SLogicNode*)pLogicChild, pSubplan, &pChild); + if (TSDB_CODE_SUCCESS == code) { + code = nodesListStrictAppend(pChildren, pChild); + } + } + + if (TSDB_CODE_SUCCESS == code) { + code = doCreatePhysiNode(pCxt, pLogicNode, pSubplan, pChildren, pPhyNode); } - pPhyNode->pChildren = pChildren; - SNode* pChild; - FOREACH(pChild, pPhyNode->pChildren) { - ((SPhysiNode*)pChild)->pParent = pPhyNode; + if (TSDB_CODE_SUCCESS == code) { + (*pPhyNode)->pChildren = pChildren; + SNode* pChild; + FOREACH(pChild, (*pPhyNode)->pChildren) { + ((SPhysiNode*)pChild)->pParent = (*pPhyNode); + } + } else { + nodesDestroyList(pChildren); } - return pPhyNode; + return code; } -static SDataSinkNode* createDataInserter(SPhysiPlanContext* pCxt, SVgDataBlocks* pBlocks) { +static int32_t createDataInserter(SPhysiPlanContext* pCxt, SVgDataBlocks* pBlocks, SDataSinkNode** pSink) { SDataInserterNode* pInserter = nodesMakeNode(QUERY_NODE_PHYSICAL_PLAN_INSERT); - CHECK_ALLOC(pInserter, NULL); + if (NULL == pInserter) { + return TSDB_CODE_OUT_OF_MEMORY; + } + pInserter->numOfTables = pBlocks->numOfTables; pInserter->size = pBlocks->size; TSWAP(pInserter->pData, pBlocks->pData, char*); - return (SDataSinkNode*)pInserter; + + *pSink = (SDataSinkNode*)pInserter; + return TSDB_CODE_SUCCESS; } -static SDataSinkNode* createDataDispatcher(SPhysiPlanContext* pCxt, const SPhysiNode* pRoot) { +static int32_t createDataDispatcher(SPhysiPlanContext* pCxt, const SPhysiNode* pRoot, SDataSinkNode** pSink) { SDataDispatcherNode* pDispatcher = nodesMakeNode(QUERY_NODE_PHYSICAL_PLAN_DISPATCH); - CHECK_ALLOC(pDispatcher, NULL); + if (NULL == pDispatcher) { + return TSDB_CODE_OUT_OF_MEMORY; + } + pDispatcher->sink.pInputDataBlockDesc = nodesCloneNode(pRoot->pOutputDataBlockDesc); - CHECK_ALLOC(pDispatcher->sink.pInputDataBlockDesc, (SDataSinkNode*)pDispatcher); - return (SDataSinkNode*)pDispatcher; + if (NULL == pDispatcher->sink.pInputDataBlockDesc) { + nodesDestroyNode(pDispatcher); + return TSDB_CODE_OUT_OF_MEMORY; + } + + *pSink = (SDataSinkNode*)pDispatcher; + return TSDB_CODE_SUCCESS; } -static SSubplan* makeSubplan(SPhysiPlanContext* pCxt, SSubLogicPlan* pLogicSubplan) { +static SSubplan* makeSubplan(SPhysiPlanContext* pCxt, SLogicSubplan* pLogicSubplan) { SSubplan* pSubplan = nodesMakeNode(QUERY_NODE_PHYSICAL_SUBPLAN); - CHECK_ALLOC(pSubplan, NULL); + if (NULL == pSubplan) { + return NULL; + } pSubplan->id = pLogicSubplan->id; pSubplan->subplanType = pLogicSubplan->subplanType; pSubplan->level = pLogicSubplan->level; return pSubplan; } -static SSubplan* createPhysiSubplan(SPhysiPlanContext* pCxt, SSubLogicPlan* pLogicSubplan) { +static int32_t createPhysiSubplan(SPhysiPlanContext* pCxt, SLogicSubplan* pLogicSubplan, SSubplan** pPhysiSubplan) { SSubplan* pSubplan = makeSubplan(pCxt, pLogicSubplan); - CHECK_ALLOC(pSubplan, NULL); + if (NULL == pSubplan) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + int32_t code = TSDB_CODE_SUCCESS; + if (SUBPLAN_TYPE_MODIFY == pLogicSubplan->subplanType) { SVnodeModifLogicNode* pModif = (SVnodeModifLogicNode*)pLogicSubplan->pNode; - pSubplan->pDataSink = createDataInserter(pCxt, pModif->pVgDataBlocks); pSubplan->msgType = pModif->msgType; pSubplan->execNode.epSet = pModif->pVgDataBlocks->vg.epSet; taosArrayPush(pCxt->pExecNodeList, &pSubplan->execNode); + code = createDataInserter(pCxt, pModif->pVgDataBlocks, &pSubplan->pDataSink); } else { - pSubplan->pNode = createPhysiNode(pCxt, pSubplan, pLogicSubplan->pNode); - if (!pCxt->pPlanCxt->streamQuery && !pCxt->pPlanCxt->topicQuery) { - pSubplan->pDataSink = createDataDispatcher(pCxt, pSubplan->pNode); - } pSubplan->msgType = TDMT_VND_QUERY; + code = createPhysiNode(pCxt, pLogicSubplan->pNode, pSubplan, &pSubplan->pNode); + if (TSDB_CODE_SUCCESS == code && !pCxt->pPlanCxt->streamQuery && !pCxt->pPlanCxt->topicQuery) { + code = createDataDispatcher(pCxt, pSubplan->pNode, &pSubplan->pDataSink); + } } - return pSubplan; -} -static void doSetLogicNodeParent(SLogicNode* pNode, SLogicNode* pParent) { - pNode->pParent = pParent; - SNode* pChild; - FOREACH(pChild, pNode->pChildren) { - doSetLogicNodeParent((SLogicNode*)pChild, pNode); + if (TSDB_CODE_SUCCESS == code) { + *pPhysiSubplan = pSubplan; + } else { + nodesDestroyNode(pSubplan); } + + return code; } -static void setLogicNodeParent(SLogicNode* pNode) { - doSetLogicNodeParent(pNode, NULL); -} - -static int32_t splitLogicPlan(SPhysiPlanContext* pCxt, SLogicNode* pLogicNode, SSubLogicPlan** pSubLogicPlan) { - *pSubLogicPlan = (SSubLogicPlan*)nodesMakeNode(QUERY_NODE_LOGIC_SUBPLAN); - CHECK_ALLOC(*pSubLogicPlan, TSDB_CODE_OUT_OF_MEMORY); - (*pSubLogicPlan)->pNode = nodesCloneNode(pLogicNode); - if (QUERY_NODE_LOGIC_PLAN_VNODE_MODIF == nodeType(pLogicNode)) { - (*pSubLogicPlan)->subplanType = SUBPLAN_TYPE_MODIFY; - TSWAP(((SVnodeModifLogicNode*)pLogicNode)->pDataBlocks, ((SVnodeModifLogicNode*)(*pSubLogicPlan)->pNode)->pDataBlocks, SArray*); - } else { - (*pSubLogicPlan)->subplanType = SUBPLAN_TYPE_SCAN; +static SQueryPlan* makeQueryPhysiPlan(SPhysiPlanContext* pCxt) { + SQueryPlan* pPlan = nodesMakeNode(QUERY_NODE_PHYSICAL_PLAN); + if (NULL == pPlan) { + return NULL; + } + pPlan->pSubplans = nodesMakeList(); + if (NULL == pPlan->pSubplans) { + nodesDestroyNode(pPlan); + return NULL; } - (*pSubLogicPlan)->id.queryId = pCxt->pPlanCxt->queryId; - setLogicNodeParent((*pSubLogicPlan)->pNode); - return applySplitRule(*pSubLogicPlan); + pPlan->queryId = pCxt->pPlanCxt->queryId; + return pPlan; } static int32_t pushSubplan(SPhysiPlanContext* pCxt, SNodeptr pSubplan, int32_t level, SNodeList* pSubplans) { @@ -705,193 +860,65 @@ static int32_t pushSubplan(SPhysiPlanContext* pCxt, SNodeptr pSubplan, int32_t l return TSDB_CODE_SUCCESS; } -static SSubLogicPlan* singleCloneSubLogicPlan(SPhysiPlanContext* pCxt, SSubLogicPlan* pSrc, int32_t level) { - SSubLogicPlan* pDst = nodesMakeNode(QUERY_NODE_LOGIC_SUBPLAN); - CHECK_ALLOC(pDst, NULL); - pDst->pNode = nodesCloneNode(pSrc->pNode); - if (NULL == pDst->pNode) { - nodesDestroyNode(pDst); - return NULL; - } - pDst->subplanType = pSrc->subplanType; - pDst->level = level; - pDst->id.queryId = pSrc->id.queryId; - pDst->id.groupId = pSrc->id.groupId; - pDst->id.subplanId = pCxt->subplanId++; - return pDst; -} - -static int32_t scaleOutForModify(SPhysiPlanContext* pCxt, SSubLogicPlan* pSubplan, int32_t level, SNodeList* pGroup) { - SVnodeModifLogicNode* pNode = (SVnodeModifLogicNode*)pSubplan->pNode; - size_t numOfVgroups = taosArrayGetSize(pNode->pDataBlocks); - for (int32_t i = 0; i < numOfVgroups; ++i) { - SSubLogicPlan* pNewSubplan = singleCloneSubLogicPlan(pCxt, pSubplan, level); - CHECK_ALLOC(pNewSubplan, TSDB_CODE_OUT_OF_MEMORY); - SVgDataBlocks* blocks = (SVgDataBlocks*)taosArrayGetP(pNode->pDataBlocks, i); - ((SVnodeModifLogicNode*)pNewSubplan->pNode)->pVgDataBlocks = blocks; - CHECK_CODE_EXT(nodesListAppend(pGroup, pNewSubplan)); - } - return TSDB_CODE_SUCCESS; -} - -static int32_t scaleOutForMerge(SPhysiPlanContext* pCxt, SSubLogicPlan* pSubplan, int32_t level, SNodeList* pGroup) { - return nodesListStrictAppend(pGroup, singleCloneSubLogicPlan(pCxt, pSubplan, level)); -} - -static int32_t doSetScanVgroup(SPhysiPlanContext* pCxt, SLogicNode* pNode, const SVgroupInfo* pVgroup, bool* pFound) { - if (QUERY_NODE_LOGIC_PLAN_SCAN == nodeType(pNode)) { - SScanLogicNode* pScan = (SScanLogicNode*)pNode; - pScan->pVgroupList = calloc(1, sizeof(SVgroupsInfo) + sizeof(SVgroupInfo)); - CHECK_ALLOC(pScan->pVgroupList, TSDB_CODE_OUT_OF_MEMORY); - memcpy(pScan->pVgroupList->vgroups, pVgroup, sizeof(SVgroupInfo)); - *pFound = true; - return TSDB_CODE_SUCCESS; - } - SNode* pChild = NULL; - FOREACH(pChild, pNode->pChildren) { - int32_t code = doSetScanVgroup(pCxt, (SLogicNode*)pChild, pVgroup, pFound); - if (TSDB_CODE_SUCCESS != code || *pFound) { - return code; - } - } - return TSDB_CODE_SUCCESS; -} - -static int32_t setScanVgroup(SPhysiPlanContext* pCxt, SLogicNode* pNode, const SVgroupInfo* pVgroup) { - bool found = false; - return doSetScanVgroup(pCxt, pNode, pVgroup, &found); -} +static int32_t buildPhysiPlan(SPhysiPlanContext* pCxt, SLogicSubplan* pLogicSubplan, SSubplan* pParent, SQueryPlan* pQueryPlan) { + SSubplan* pSubplan = NULL; + int32_t code = createPhysiSubplan(pCxt, pLogicSubplan, &pSubplan); -static int32_t scaleOutForScan(SPhysiPlanContext* pCxt, SSubLogicPlan* pSubplan, int32_t level, SNodeList* pGroup) { - if (pSubplan->pVgroupList) { - for (int32_t i = 0; i < pSubplan->pVgroupList->numOfVgroups; ++i) { - SSubLogicPlan* pNewSubplan = singleCloneSubLogicPlan(pCxt, pSubplan, level); - CHECK_ALLOC(pNewSubplan, TSDB_CODE_OUT_OF_MEMORY); - CHECK_CODE_EXT(setScanVgroup(pCxt, pNewSubplan->pNode, pSubplan->pVgroupList->vgroups + i)); - CHECK_CODE_EXT(nodesListAppend(pGroup, pNewSubplan)); - } - return TSDB_CODE_SUCCESS; - } else { - return scaleOutForMerge(pCxt, pSubplan, level, pGroup); + if (TSDB_CODE_SUCCESS == code) { + code = pushSubplan(pCxt, pSubplan, pLogicSubplan->level, pQueryPlan->pSubplans); + ++(pQueryPlan->numOfSubplans); } -} -static int32_t appendWithMakeList(SNodeList** pList, SNodeptr pNode) { - if (NULL == *pList) { - *pList = nodesMakeList(); - if (NULL == *pList) { - return TSDB_CODE_OUT_OF_MEMORY; + if (TSDB_CODE_SUCCESS == code && NULL != pParent) { + code = nodesListMakeAppend(&pParent->pChildren, pSubplan); + if (TSDB_CODE_SUCCESS == code) { + code = nodesListMakeAppend(&pSubplan->pParents, pParent); } } - return nodesListAppend(*pList, pNode); -} -static int32_t pushHierarchicalPlan(SPhysiPlanContext* pCxt, SNodeList* pParentsGroup, SNodeList* pCurrentGroup) { - bool topLevel = (0 == LIST_LENGTH(pParentsGroup)); - SNode* pChild = NULL; - FOREACH(pChild, pCurrentGroup) { - if (topLevel) { - CHECK_CODE_EXT(nodesListAppend(pParentsGroup, pChild)); - } else { - SNode* pParent = NULL; - FOREACH(pParent, pParentsGroup) { - CHECK_CODE_EXT(appendWithMakeList(&(((SSubLogicPlan*)pParent)->pChildren), pChild)); - CHECK_CODE_EXT(appendWithMakeList(&(((SSubLogicPlan*)pChild)->pParents), pParent)); + if (TSDB_CODE_SUCCESS == code) { + SNode* pChild = NULL; + FOREACH(pChild, pLogicSubplan->pChildren) { + code = buildPhysiPlan(pCxt, (SLogicSubplan*)pChild, pSubplan, pQueryPlan); + if (TSDB_CODE_SUCCESS != code) { + break; } } } - return TSDB_CODE_SUCCESS; -} -static int32_t doScaleOut(SPhysiPlanContext* pCxt, SSubLogicPlan* pSubplan, int32_t* pLevel, SNodeList* pParentsGroup) { - SNodeList* pCurrentGroup = nodesMakeList(); - CHECK_ALLOC(pCurrentGroup, TSDB_CODE_OUT_OF_MEMORY); - int32_t code = TSDB_CODE_SUCCESS; - switch (pSubplan->subplanType) { - case SUBPLAN_TYPE_MERGE: - code = scaleOutForMerge(pCxt, pSubplan, *pLevel, pCurrentGroup); - break; - case SUBPLAN_TYPE_SCAN: - code = scaleOutForScan(pCxt, pSubplan, *pLevel, pCurrentGroup); - break; - case SUBPLAN_TYPE_MODIFY: - code = scaleOutForModify(pCxt, pSubplan, *pLevel, pCurrentGroup); - break; - default: - break; - } if (TSDB_CODE_SUCCESS != code) { - return code; + nodesDestroyNode(pSubplan); } - CHECK_CODE_EXT(pushHierarchicalPlan(pCxt, pParentsGroup, pCurrentGroup)); - ++(*pLevel); - SNode* pChild; - FOREACH(pChild, pSubplan->pChildren) { - CHECK_CODE_EXT(doScaleOut(pCxt, (SSubLogicPlan*)pChild, pLevel, pCurrentGroup)); - } - - return TSDB_CODE_SUCCESS; + return code; } -static SQueryLogicPlan* makeQueryLogicPlan(SPhysiPlanContext* pCxt) { - SQueryLogicPlan* pLogicPlan = (SQueryLogicPlan*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN); - CHECK_ALLOC(pLogicPlan, NULL); - pLogicPlan->pTopSubplans = nodesMakeList(); - if (NULL == pLogicPlan->pTopSubplans) { - nodesDestroyNode(pLogicPlan); - return NULL; +static int32_t doCreatePhysiPlan(SPhysiPlanContext* pCxt, SQueryLogicPlan* pLogicPlan, SQueryPlan** pPhysiPlan) { + SQueryPlan* pPlan = makeQueryPhysiPlan(pCxt); + if (NULL == pPlan) { + return TSDB_CODE_OUT_OF_MEMORY; } - return pLogicPlan; -} - -static int32_t scaleOutLogicPlan(SPhysiPlanContext* pCxt, SSubLogicPlan* pRootSubLogicPlan, SQueryLogicPlan** pLogicPlan) { - *pLogicPlan = makeQueryLogicPlan(pCxt); - CHECK_ALLOC(*pLogicPlan, TSDB_CODE_OUT_OF_MEMORY); - return doScaleOut(pCxt, pRootSubLogicPlan, &((*pLogicPlan)->totalLevel), (*pLogicPlan)->pTopSubplans); -} -static SQueryPlan* makeQueryPhysiPlan(SPhysiPlanContext* pCxt) { - SQueryPlan* pPlan = nodesMakeNode(QUERY_NODE_PHYSICAL_PLAN); - CHECK_ALLOC(pPlan, NULL); - pPlan->pSubplans = nodesMakeList(); - if (NULL == pPlan->pSubplans) { - nodesDestroyNode(pPlan); - return NULL; - } - pPlan->queryId = pCxt->pPlanCxt->queryId; - return pPlan; -} + int32_t code = TSDB_CODE_SUCCESS; -static int32_t doBuildPhysiPlan(SPhysiPlanContext* pCxt, SSubLogicPlan* pLogicSubplan, SSubplan* pParent, SQueryPlan* pQueryPlan) { - SSubplan* pSubplan = createPhysiSubplan(pCxt, pLogicSubplan); - CHECK_ALLOC(pSubplan, TSDB_CODE_OUT_OF_MEMORY); - CHECK_CODE_EXT(pushSubplan(pCxt, pSubplan, pLogicSubplan->level, pQueryPlan->pSubplans)); - ++(pQueryPlan->numOfSubplans); - if (NULL != pParent) { - CHECK_CODE_EXT(appendWithMakeList(&pParent->pChildren, pSubplan)); - CHECK_CODE_EXT(appendWithMakeList(&pSubplan->pParents, pParent)); + SNode* pSubplan = NULL; + FOREACH(pSubplan, pLogicPlan->pTopSubplans) { + code = buildPhysiPlan(pCxt, (SLogicSubplan*)pSubplan, NULL, pPlan); + if (TSDB_CODE_SUCCESS != code) { + break; + } } - SNode* pChild = NULL; - FOREACH(pChild, pLogicSubplan->pChildren) { - CHECK_CODE_EXT(doBuildPhysiPlan(pCxt, (SSubLogicPlan*)pChild, pSubplan, pQueryPlan)); + if (TSDB_CODE_SUCCESS == code) { + *pPhysiPlan = pPlan; + } else { + nodesDestroyNode(pPlan); } - return TSDB_CODE_SUCCESS; -} - -static int32_t buildPhysiPlan(SPhysiPlanContext* pCxt, SQueryLogicPlan* pLogicPlan, SQueryPlan** pPlan) { - *pPlan = makeQueryPhysiPlan(pCxt); - CHECK_ALLOC(*pPlan, TSDB_CODE_OUT_OF_MEMORY); - SNode* pSubplan = NULL; - FOREACH(pSubplan, pLogicPlan->pTopSubplans) { - CHECK_CODE_EXT(doBuildPhysiPlan(pCxt, (SSubLogicPlan*)pSubplan, NULL, *pPlan)); - } - return TSDB_CODE_SUCCESS; + return code; } -int32_t createPhysiPlan(SPlanContext* pCxt, SLogicNode* pLogicNode, SQueryPlan** pPlan, SArray* pExecNodeList) { +int32_t createPhysiPlan(SPlanContext* pCxt, SQueryLogicPlan* pLogicPlan, SQueryPlan** pPlan, SArray* pExecNodeList) { SPhysiPlanContext cxt = { .pPlanCxt = pCxt, .errCode = TSDB_CODE_SUCCESS, @@ -902,16 +929,5 @@ int32_t createPhysiPlan(SPlanContext* pCxt, SLogicNode* pLogicNode, SQueryPlan** if (NULL == cxt.pLocationHelper) { return TSDB_CODE_OUT_OF_MEMORY; } - SQueryLogicPlan* pLogicPlan = NULL; - SSubLogicPlan* pSubLogicPlan = NULL; - int32_t code = splitLogicPlan(&cxt, pLogicNode, &pSubLogicPlan); - if (TSDB_CODE_SUCCESS == code) { - code = scaleOutLogicPlan(&cxt, pSubLogicPlan, &pLogicPlan); - } - if (TSDB_CODE_SUCCESS == code) { - code = buildPhysiPlan(&cxt, pLogicPlan, pPlan); - } - nodesDestroyNode(pSubLogicPlan); - nodesDestroyNode(pLogicPlan); - return code; + return doCreatePhysiPlan(&cxt, pLogicPlan, pPlan); } diff --git a/source/libs/planner/src/planScaleOut.c b/source/libs/planner/src/planScaleOut.c new file mode 100644 index 0000000000000000000000000000000000000000..345b64f56d894b51f3cbc154dc2384f3dc1c084d --- /dev/null +++ b/source/libs/planner/src/planScaleOut.c @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#include "planInt.h" + +typedef struct SScaleOutContext { + SPlanContext* pPlanCxt; + int32_t subplanId; +} SScaleOutContext; + +static SLogicSubplan* singleCloneSubLogicPlan(SScaleOutContext* pCxt, SLogicSubplan* pSrc, int32_t level) { + SLogicSubplan* pDst = nodesMakeNode(QUERY_NODE_LOGIC_SUBPLAN); + if (NULL == pDst->pNode) { + return NULL; + } + pDst->pNode = nodesCloneNode(pSrc->pNode); + if (NULL == pDst->pNode) { + nodesDestroyNode(pDst); + return NULL; + } + pDst->subplanType = pSrc->subplanType; + pDst->level = level; + pDst->id.queryId = pSrc->id.queryId; + pDst->id.groupId = pSrc->id.groupId; + pDst->id.subplanId = pCxt->subplanId++; + return pDst; +} + +static int32_t scaleOutForModify(SScaleOutContext* pCxt, SLogicSubplan* pSubplan, int32_t level, SNodeList* pGroup) { + SVnodeModifLogicNode* pNode = (SVnodeModifLogicNode*)pSubplan->pNode; + size_t numOfVgroups = taosArrayGetSize(pNode->pDataBlocks); + for (int32_t i = 0; i < numOfVgroups; ++i) { + SLogicSubplan* pNewSubplan = singleCloneSubLogicPlan(pCxt, pSubplan, level); + if (NULL == pNewSubplan) { + return TSDB_CODE_OUT_OF_MEMORY; + } + ((SVnodeModifLogicNode*)pNewSubplan->pNode)->pVgDataBlocks = (SVgDataBlocks*)taosArrayGetP(pNode->pDataBlocks, i); + if (TSDB_CODE_SUCCESS != nodesListStrictAppend(pGroup, pNewSubplan)) { + return TSDB_CODE_OUT_OF_MEMORY; + } + } + return TSDB_CODE_SUCCESS; +} + +static int32_t scaleOutForMerge(SScaleOutContext* pCxt, SLogicSubplan* pSubplan, int32_t level, SNodeList* pGroup) { + return nodesListStrictAppend(pGroup, singleCloneSubLogicPlan(pCxt, pSubplan, level)); +} + +static int32_t doSetScanVgroup(SLogicNode* pNode, const SVgroupInfo* pVgroup, bool* pFound) { + if (QUERY_NODE_LOGIC_PLAN_SCAN == nodeType(pNode)) { + SScanLogicNode* pScan = (SScanLogicNode*)pNode; + pScan->pVgroupList = calloc(1, sizeof(SVgroupsInfo) + sizeof(SVgroupInfo)); + if (NULL == pScan->pVgroupList) { + return TSDB_CODE_OUT_OF_MEMORY; + } + memcpy(pScan->pVgroupList->vgroups, pVgroup, sizeof(SVgroupInfo)); + *pFound = true; + return TSDB_CODE_SUCCESS; + } + SNode* pChild = NULL; + FOREACH(pChild, pNode->pChildren) { + int32_t code = doSetScanVgroup((SLogicNode*)pChild, pVgroup, pFound); + if (TSDB_CODE_SUCCESS != code || *pFound) { + return code; + } + } + return TSDB_CODE_SUCCESS; +} + +static int32_t setScanVgroup(SLogicNode* pNode, const SVgroupInfo* pVgroup) { + bool found = false; + return doSetScanVgroup(pNode, pVgroup, &found); +} + +static int32_t scaleOutForScan(SScaleOutContext* pCxt, SLogicSubplan* pSubplan, int32_t level, SNodeList* pGroup) { + if (pSubplan->pVgroupList) { + int32_t code = TSDB_CODE_SUCCESS; + for (int32_t i = 0; i < pSubplan->pVgroupList->numOfVgroups; ++i) { + SLogicSubplan* pNewSubplan = singleCloneSubLogicPlan(pCxt, pSubplan, level); + if (NULL == pNewSubplan) { + return TSDB_CODE_OUT_OF_MEMORY; + } + code = setScanVgroup(pNewSubplan->pNode, pSubplan->pVgroupList->vgroups + i); + if (TSDB_CODE_SUCCESS == code) { + code = nodesListStrictAppend(pGroup, pNewSubplan); + } + if (TSDB_CODE_SUCCESS != code) { + break; + } + } + return code; + } else { + return scaleOutForMerge(pCxt, pSubplan, level, pGroup); + } +} + +static int32_t pushHierarchicalPlan(SNodeList* pParentsGroup, SNodeList* pCurrentGroup) { + int32_t code = TSDB_CODE_SUCCESS; + bool topLevel = (0 == LIST_LENGTH(pParentsGroup)); + SNode* pChild = NULL; + FOREACH(pChild, pCurrentGroup) { + if (topLevel) { + code = nodesListAppend(pParentsGroup, pChild); + } else { + SNode* pParent = NULL; + FOREACH(pParent, pParentsGroup) { + code = nodesListMakeAppend(&(((SLogicSubplan*)pParent)->pChildren), pChild); + if (TSDB_CODE_SUCCESS == code) { + code = nodesListMakeAppend(&(((SLogicSubplan*)pChild)->pParents), pParent); + } + } + } + if (TSDB_CODE_SUCCESS != code) { + break; + } + } + return code; +} + +static int32_t doScaleOut(SScaleOutContext* pCxt, SLogicSubplan* pSubplan, int32_t* pLevel, SNodeList* pParentsGroup) { + SNodeList* pCurrentGroup = nodesMakeList(); + if (NULL == pCurrentGroup) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + int32_t code = TSDB_CODE_SUCCESS; + switch (pSubplan->subplanType) { + case SUBPLAN_TYPE_MERGE: + code = scaleOutForMerge(pCxt, pSubplan, *pLevel, pCurrentGroup); + break; + case SUBPLAN_TYPE_SCAN: + code = scaleOutForScan(pCxt, pSubplan, *pLevel, pCurrentGroup); + break; + case SUBPLAN_TYPE_MODIFY: + code = scaleOutForModify(pCxt, pSubplan, *pLevel, pCurrentGroup); + break; + default: + break; + } + + if (TSDB_CODE_SUCCESS == code) { + code = pushHierarchicalPlan(pParentsGroup, pCurrentGroup); + ++(*pLevel); + } + + if (TSDB_CODE_SUCCESS == code) { + SNode* pChild; + FOREACH(pChild, pSubplan->pChildren) { + code = doScaleOut(pCxt, (SLogicSubplan*)pChild, pLevel, pCurrentGroup); + if (TSDB_CODE_SUCCESS != code) { + break; + } + } + } + + if (TSDB_CODE_SUCCESS != code) { + nodesDestroyList(pCurrentGroup); + } + + return code; +} + +static SQueryLogicPlan* makeQueryLogicPlan() { + SQueryLogicPlan* pLogicPlan = (SQueryLogicPlan*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN); + if (NULL == pLogicPlan) { + return NULL; + } + pLogicPlan->pTopSubplans = nodesMakeList(); + if (NULL == pLogicPlan->pTopSubplans) { + nodesDestroyNode(pLogicPlan); + return NULL; + } + return pLogicPlan; +} + +int32_t scaleOutLogicPlan(SPlanContext* pCxt, SLogicSubplan* pLogicSubplan, SQueryLogicPlan** pLogicPlan) { + SQueryLogicPlan* pPlan = makeQueryLogicPlan(); + if (NULL == pPlan) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + SScaleOutContext cxt = { .pPlanCxt = pCxt, .subplanId = 1 }; + int32_t code = doScaleOut(&cxt, pLogicSubplan, &(pPlan->totalLevel), pPlan->pTopSubplans); + if (TSDB_CODE_SUCCESS == code) { + *pLogicPlan = pPlan; + } else { + nodesDestroyNode(pPlan); + } + + return code; +} diff --git a/source/libs/planner/src/planSpliter.c b/source/libs/planner/src/planSpliter.c index 4d7d31879bec6c3d37bb9ee6f49386ec87ced48c..2bba9aefb170e5274aac351f53b6864d6592b0a6 100644 --- a/source/libs/planner/src/planSpliter.c +++ b/source/libs/planner/src/planSpliter.c @@ -29,7 +29,7 @@ typedef struct SSplitContext { void* pInfo; } SSplitContext; -typedef int32_t (*FMatch)(SSplitContext* pCxt, SSubLogicPlan* pSubplan); +typedef int32_t (*FMatch)(SSplitContext* pCxt, SLogicSubplan* pSubplan); typedef int32_t (*FSplit)(SSplitContext* pCxt); typedef struct SSplitRule { @@ -40,7 +40,7 @@ typedef struct SSplitRule { typedef struct SStsInfo { SScanLogicNode* pScan; - SSubLogicPlan* pSubplan; + SLogicSubplan* pSubplan; } SStsInfo; static SLogicNode* stsMatchByNode(SLogicNode* pNode) { @@ -58,7 +58,7 @@ static SLogicNode* stsMatchByNode(SLogicNode* pNode) { return NULL; } -static int32_t stsMatch(SSplitContext* pCxt, SSubLogicPlan* pSubplan) { +static int32_t stsMatch(SSplitContext* pCxt, SLogicSubplan* pSubplan) { if (SPLIT_FLAG_TEST_MASK(pSubplan->splitFlag, SPLIT_FLAG_STS)) { return TSDB_CODE_SUCCESS; } @@ -74,7 +74,7 @@ static int32_t stsMatch(SSplitContext* pCxt, SSubLogicPlan* pSubplan) { } SNode* pChild; FOREACH(pChild, pSubplan->pChildren) { - int32_t code = stsMatch(pCxt, (SSubLogicPlan*)pChild); + int32_t code = stsMatch(pCxt, (SLogicSubplan*)pChild); if (TSDB_CODE_SUCCESS != code || pCxt->match) { return code; } @@ -82,8 +82,8 @@ static int32_t stsMatch(SSplitContext* pCxt, SSubLogicPlan* pSubplan) { return TSDB_CODE_SUCCESS; } -static SSubLogicPlan* stsCreateScanSubplan(SSplitContext* pCxt, SScanLogicNode* pScan) { - SSubLogicPlan* pSubplan = nodesMakeNode(QUERY_NODE_LOGIC_SUBPLAN); +static SLogicSubplan* stsCreateScanSubplan(SSplitContext* pCxt, SScanLogicNode* pScan) { + SLogicSubplan* pSubplan = nodesMakeNode(QUERY_NODE_LOGIC_SUBPLAN); if (NULL == pSubplan) { return NULL; } @@ -95,7 +95,7 @@ static SSubLogicPlan* stsCreateScanSubplan(SSplitContext* pCxt, SScanLogicNode* return pSubplan; } -static int32_t stsCreateExchangeNode(SSplitContext* pCxt, SSubLogicPlan* pSubplan, SScanLogicNode* pScan) { +static int32_t stsCreateExchangeNode(SSplitContext* pCxt, SLogicSubplan* pSubplan, SScanLogicNode* pScan) { SExchangeLogicNode* pExchange = nodesMakeNode(QUERY_NODE_LOGIC_PLAN_EXCHANGE); if (NULL == pExchange) { return TSDB_CODE_OUT_OF_MEMORY; @@ -145,7 +145,7 @@ static const SSplitRule splitRuleSet[] = { static const int32_t splitRuleNum = (sizeof(splitRuleSet) / sizeof(SSplitRule)); -int32_t applySplitRule(SSubLogicPlan* pSubplan) { +static int32_t applySplitRule(SLogicSubplan* pSubplan) { SSplitContext cxt = { .errCode = TSDB_CODE_SUCCESS, .groupId = pSubplan->id.groupId + 1, .match = false, .pInfo = NULL }; bool split = false; do { @@ -164,3 +164,45 @@ int32_t applySplitRule(SSubLogicPlan* pSubplan) { } while (split); return TSDB_CODE_SUCCESS; } + +static void doSetLogicNodeParent(SLogicNode* pNode, SLogicNode* pParent) { + pNode->pParent = pParent; + SNode* pChild; + FOREACH(pChild, pNode->pChildren) { + doSetLogicNodeParent((SLogicNode*)pChild, pNode); + } +} + +static void setLogicNodeParent(SLogicNode* pNode) { + doSetLogicNodeParent(pNode, NULL); +} + +int32_t splitLogicPlan(SPlanContext* pCxt, SLogicNode* pLogicNode, SLogicSubplan** pLogicSubplan) { + SLogicSubplan* pSubplan = (SLogicSubplan*)nodesMakeNode(QUERY_NODE_LOGIC_SUBPLAN); + if (NULL == pSubplan) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + pSubplan->pNode = nodesCloneNode(pLogicNode); + if (NULL == pSubplan->pNode) { + nodesDestroyNode(pSubplan); + return TSDB_CODE_OUT_OF_MEMORY; + } + if (QUERY_NODE_LOGIC_PLAN_VNODE_MODIF == nodeType(pLogicNode)) { + pSubplan->subplanType = SUBPLAN_TYPE_MODIFY; + TSWAP(((SVnodeModifLogicNode*)pLogicNode)->pDataBlocks, ((SVnodeModifLogicNode*)pSubplan->pNode)->pDataBlocks, SArray*); + } else { + pSubplan->subplanType = SUBPLAN_TYPE_SCAN; + } + pSubplan->id.queryId = pCxt->queryId; + setLogicNodeParent(pSubplan->pNode); + + int32_t code = applySplitRule(pSubplan); + if (TSDB_CODE_SUCCESS == code) { + *pLogicSubplan = pSubplan; + } else { + nodesDestroyNode(pSubplan); + } + + return code; +} \ No newline at end of file diff --git a/source/libs/planner/src/planner.c b/source/libs/planner/src/planner.c index fa0dc549c8a0d3ea1dcaa4120a9c9e0d020cb3b5..f2bc81957a60944c302c0cb849ea27f867392b74 100644 --- a/source/libs/planner/src/planner.c +++ b/source/libs/planner/src/planner.c @@ -17,20 +17,28 @@ #include "planInt.h" -int32_t optimize(SPlanContext* pCxt, SLogicNode* pLogicNode) { - return TSDB_CODE_SUCCESS; -} - int32_t qCreateQueryPlan(SPlanContext* pCxt, SQueryPlan** pPlan, SArray* pExecNodeList) { SLogicNode* pLogicNode = NULL; + SLogicSubplan* pLogicSubplan = NULL; + SQueryLogicPlan* pLogicPlan = NULL; + int32_t code = createLogicPlan(pCxt, &pLogicNode); if (TSDB_CODE_SUCCESS == code) { - code = optimize(pCxt, pLogicNode); + code = optimizeLogicPlan(pCxt, pLogicNode); + } + if (TSDB_CODE_SUCCESS == code) { + code = splitLogicPlan(pCxt, pLogicNode, &pLogicSubplan); + } + if (TSDB_CODE_SUCCESS == code) { + code = scaleOutLogicPlan(pCxt, pLogicSubplan, &pLogicPlan); } if (TSDB_CODE_SUCCESS == code) { - code = createPhysiPlan(pCxt, pLogicNode, pPlan, pExecNodeList); + code = createPhysiPlan(pCxt, pLogicPlan, pPlan, pExecNodeList); } + nodesDestroyNode(pLogicNode); + nodesDestroyNode(pLogicSubplan); + nodesDestroyNode(pLogicPlan); return code; } diff --git a/source/libs/planner/test/plannerTest.cpp b/source/libs/planner/test/plannerTest.cpp index 90d8f05efc153c0332082d507a88087e8f6d5443..67a637a956aa788c0817618ae881adcddb5e4804 100644 --- a/source/libs/planner/test/plannerTest.cpp +++ b/source/libs/planner/test/plannerTest.cpp @@ -26,11 +26,6 @@ using namespace testing; class PlannerTest : public Test { protected: - enum TestTarget { - TEST_LOGIC_PLAN, - TEST_PHYSICAL_PLAN - }; - void setDatabase(const string& acctId, const string& db) { acctId_ = acctId; db_ = db; @@ -46,7 +41,7 @@ protected: cxt_.pSql = sqlBuf_.c_str(); } - bool run(TestTarget target = TEST_PHYSICAL_PLAN) { + bool run() { int32_t code = qParseQuerySql(&cxt_, &query_); if (code != TSDB_CODE_SUCCESS) { @@ -56,12 +51,12 @@ protected: const string syntaxTreeStr = toString(query_->pRoot, false); - SLogicNode* pLogicPlan = nullptr; + SLogicNode* pLogicNode = nullptr; SPlanContext cxt = { .queryId = 1, .acctId = 0 }; setPlanContext(query_, &cxt); - code = createLogicPlan(&cxt, &pLogicPlan); + code = createLogicPlan(&cxt, &pLogicNode); if (code != TSDB_CODE_SUCCESS) { - cout << "sql:[" << cxt_.pSql << "] logic plan code:" << code << ", strerror:" << tstrerror(code) << endl; + cout << "sql:[" << cxt_.pSql << "] createLogicPlan code:" << code << ", strerror:" << tstrerror(code) << endl; return false; } @@ -69,24 +64,37 @@ protected: cout << "syntax test : " << endl; cout << syntaxTreeStr << endl; cout << "unformatted logic plan : " << endl; - cout << toString((const SNode*)pLogicPlan, false) << endl; - - if (TEST_PHYSICAL_PLAN == target) { - SQueryPlan* pPlan = nullptr; - code = createPhysiPlan(&cxt, pLogicPlan, &pPlan, NULL); - if (code != TSDB_CODE_SUCCESS) { - cout << "sql:[" << cxt_.pSql << "] physical plan code:" << code << ", strerror:" << tstrerror(code) << endl; - return false; - } - cout << "unformatted physical plan : " << endl; - cout << toString((const SNode*)pPlan, false) << endl; - SNode* pNode; - FOREACH(pNode, pPlan->pSubplans) { - SNode* pSubplan; - FOREACH(pSubplan, ((SNodeListNode*)pNode)->pNodeList) { - cout << "unformatted physical subplan : " << endl; - cout << toString(pSubplan, false) << endl; - } + cout << toString((const SNode*)pLogicNode, false) << endl; + + SLogicSubplan* pLogicSubplan = nullptr; + code = splitLogicPlan(&cxt, pLogicNode, &pLogicSubplan); + if (code != TSDB_CODE_SUCCESS) { + cout << "sql:[" << cxt_.pSql << "] splitLogicPlan code:" << code << ", strerror:" << tstrerror(code) << endl; + return false; + } + + SQueryLogicPlan* pLogicPlan = NULL; + code = scaleOutLogicPlan(&cxt, pLogicSubplan, &pLogicPlan); + if (code != TSDB_CODE_SUCCESS) { + cout << "sql:[" << cxt_.pSql << "] createPhysiPlan code:" << code << ", strerror:" << tstrerror(code) << endl; + return false; + } + + SQueryPlan* pPlan = nullptr; + code = createPhysiPlan(&cxt, pLogicPlan, &pPlan, NULL); + if (code != TSDB_CODE_SUCCESS) { + cout << "sql:[" << cxt_.pSql << "] createPhysiPlan code:" << code << ", strerror:" << tstrerror(code) << endl; + return false; + } + + cout << "unformatted physical plan : " << endl; + cout << toString((const SNode*)pPlan, false) << endl; + SNode* pNode; + FOREACH(pNode, pPlan->pSubplans) { + SNode* pSubplan; + FOREACH(pSubplan, ((SNodeListNode*)pNode)->pNodeList) { + cout << "unformatted physical subplan : " << endl; + cout << toString(pSubplan, false) << endl; } } @@ -120,14 +128,6 @@ private: cout << "sql:[" << cxt_.pSql << "] toString code:" << code << ", strerror:" << tstrerror(code) << endl; return string(); } - SNode* pNode; - code = nodesStringToNode(pStr, &pNode); - if (code != TSDB_CODE_SUCCESS) { - tfree(pStr); - cout << "sql:[" << cxt_.pSql << "] toObject code:" << code << ", strerror:" << tstrerror(code) << endl; - return string(); - } - nodesDestroyNode(pNode); string str(pStr); tfree(pStr); return str; @@ -185,6 +185,13 @@ TEST_F(PlannerTest, interval) { ASSERT_TRUE(run()); } +TEST_F(PlannerTest, sessionWindow) { + setDatabase("root", "test"); + + bind("SELECT count(*) FROM t1 session(ts, 10s)"); + ASSERT_TRUE(run()); +} + TEST_F(PlannerTest, showTables) { setDatabase("root", "test");