From 7602f9161218afa4e24fe6eee711e92060ae2cdd Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Fri, 1 Apr 2022 19:45:45 +0800 Subject: [PATCH 01/25] feature/qnode --- include/common/tmsg.h | 7 + include/common/tmsgdef.h | 1 + include/libs/command/command.h | 12 ++ include/libs/executor/executor.h | 2 + include/libs/nodes/nodes.h | 1 + include/libs/nodes/plannodes.h | 1 + include/libs/nodes/querynodes.h | 1 + include/libs/scheduler/scheduler.h | 2 +- source/client/src/clientImpl.c | 2 +- source/libs/command/inc/commandInt.h | 13 +- source/libs/command/src/explain.c | 154 +++++++------- source/libs/executor/src/executorMain.c | 9 + source/libs/executor/src/executorimpl.c | 15 ++ source/libs/nodes/inc/nodesUtil.h | 1 + source/libs/nodes/src/nodesUtilFuncs.c | 30 +++ source/libs/qworker/inc/qworkerInt.h | 6 +- source/libs/qworker/src/qworker.c | 100 +++++---- source/libs/qworker/src/qworkerMsg.c | 2 +- source/libs/scheduler/inc/schedulerInt.h | 5 + source/libs/scheduler/src/scheduler.c | 194 ++++++++++++------ source/libs/scheduler/test/schedulerTests.cpp | 2 +- 21 files changed, 376 insertions(+), 184 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index cd16bbf862..4cfcf0e888 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -915,6 +915,12 @@ typedef struct { char data[]; } SRetrieveMetaTableRsp; +typedef struct { + int32_t numOfItems; + char data[]; +} SExplainRsp; + + typedef struct { char fqdn[TSDB_FQDN_LEN]; // end point, hostname:port int32_t port; @@ -1052,6 +1058,7 @@ typedef struct SSubQueryMsg { uint64_t taskId; int64_t refId; int8_t taskType; + int8_t explain; uint32_t sqlLen; // the query sql, uint32_t phyLen; char msg[]; diff --git a/include/common/tmsgdef.h b/include/common/tmsgdef.h index 36a489eb59..1d4667cda0 100644 --- a/include/common/tmsgdef.h +++ b/include/common/tmsgdef.h @@ -188,6 +188,7 @@ enum { TD_DEF_MSG_TYPE(TDMT_VND_SHOW_TABLES_FETCH, "vnode-show-tables-fetch", SVShowTablesFetchReq, SVShowTablesFetchRsp) TD_DEF_MSG_TYPE(TDMT_VND_QUERY_CONTINUE, "vnode-query-continue", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_VND_QUERY_HEARTBEAT, "vnode-query-heartbeat", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_VND_EXPLAIN, "vnode-explain", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_VND_SUBSCRIBE, "vnode-subscribe", SMVSubscribeReq, SMVSubscribeRsp) TD_DEF_MSG_TYPE(TDMT_VND_CONSUME, "vnode-consume", SMqCVConsumeReq, SMqCVConsumeRsp) diff --git a/include/libs/command/command.h b/include/libs/command/command.h index 7e58d39692..31e370ac5c 100644 --- a/include/libs/command/command.h +++ b/include/libs/command/command.h @@ -17,8 +17,20 @@ #include "tmsg.h" #include "plannodes.h" +typedef struct SExplainCtx SExplainCtx; + +typedef struct SExplainExecInfo { + uint64_t startupCost; + uint64_t totalCost; + uint64_t numOfRows; +} SExplainExecInfo; + int32_t qExecCommand(SNode* pStmt, SRetrieveTableRsp** pRsp); int32_t qExecStaticExplain(SQueryPlan *pDag, SRetrieveTableRsp **pRsp); +int32_t qExecExplainBegin(SQueryPlan *pDag, SExplainCtx **pCtx, int32_t startTs); +int32_t qExecExplainEnd(SExplainCtx *pCtx); +int32_t qExplainUpdateExecInfo(SExplainCtx *pCtx, SExplainRsp *pRspMsg, int32_t groupId, SRetrieveTableRsp **pRsp); +void qExplainFreeCtx(SExplainCtx *pCtx); diff --git a/include/libs/executor/executor.h b/include/libs/executor/executor.h index d3cd828cf5..4d289147d0 100644 --- a/include/libs/executor/executor.h +++ b/include/libs/executor/executor.h @@ -174,6 +174,8 @@ void** qReleaseTask(void* pMgmt, void* pQInfo, bool freeHandle); void qProcessFetchRsp(void* parent, struct SRpcMsg* pMsg, struct SEpSet* pEpSet); +int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, int32_t *resNum, SExplainExecInfo **pRes); + #ifdef __cplusplus } #endif diff --git a/include/libs/nodes/nodes.h b/include/libs/nodes/nodes.h index 9849dfca39..2979040c07 100644 --- a/include/libs/nodes/nodes.h +++ b/include/libs/nodes/nodes.h @@ -216,6 +216,7 @@ int32_t nodesListToString(const SNodeList* pList, bool format, char** pStr, int3 int32_t nodesStringToList(const char* pStr, SNodeList** pList); int32_t nodesNodeToSQL(SNode *pNode, char *buf, int32_t bufSize, int32_t *len); +char *nodesGetNameFromColumnNode(SNode *pNode); #ifdef __cplusplus } diff --git a/include/libs/nodes/plannodes.h b/include/libs/nodes/plannodes.h index 37163f60dd..eda7882e2c 100644 --- a/include/libs/nodes/plannodes.h +++ b/include/libs/nodes/plannodes.h @@ -308,6 +308,7 @@ typedef enum EExplainMode { typedef struct SExplainInfo { EExplainMode mode; bool verbose; + double ratio; } SExplainInfo; typedef struct SQueryPlan { diff --git a/include/libs/nodes/querynodes.h b/include/libs/nodes/querynodes.h index ddcbaa0bee..04fa8f21df 100644 --- a/include/libs/nodes/querynodes.h +++ b/include/libs/nodes/querynodes.h @@ -315,6 +315,7 @@ bool nodesIsTimelineQuery(const SNode* pQuery); void* nodesGetValueFromNode(SValueNode *pNode); char* nodesGetStrValueFromNode(SValueNode *pNode); +char *getFillModeString(EFillMode mode); #ifdef __cplusplus } diff --git a/include/libs/scheduler/scheduler.h b/include/libs/scheduler/scheduler.h index 16a6ae32cf..5ab4ead89c 100644 --- a/include/libs/scheduler/scheduler.h +++ b/include/libs/scheduler/scheduler.h @@ -71,7 +71,7 @@ int32_t schedulerInit(SSchedulerCfg *cfg); * @param nodeList Qnode/Vnode address list, element is SQueryNodeAddr * @return */ -int32_t schedulerExecJob(void *transport, SArray *nodeList, SQueryPlan* pDag, int64_t* pJob, const char* sql, SQueryResult *pRes); +int32_t schedulerExecJob(void *transport, SArray *nodeList, SQueryPlan *pDag, int64_t *pJob, const char *sql, int64_t startTs, SQueryResult *pRes); /** * Process the query job, generated according to the query physical plan. diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index 8cd53f18b0..1512322f1a 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -241,7 +241,7 @@ int32_t scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList void* pTransporter = pRequest->pTscObj->pAppInfo->pTransporter; SQueryResult res = {.code = 0, .numOfRows = 0, .msgSize = ERROR_MSG_BUF_DEFAULT_SIZE, .msg = pRequest->msgBuf}; - int32_t code = schedulerExecJob(pTransporter, pNodeList, pDag, &pRequest->body.queryJob, pRequest->sqlstr, &res); + int32_t code = schedulerExecJob(pTransporter, pNodeList, pDag, &pRequest->body.queryJob, pRequest->sqlstr, pRequest->metric.start, &res); if (code != TSDB_CODE_SUCCESS) { if (pRequest->body.queryJob != 0) { schedulerFreeJob(pRequest->body.queryJob); diff --git a/source/libs/command/inc/commandInt.h b/source/libs/command/inc/commandInt.h index 771baca2ab..de0a7c433a 100644 --- a/source/libs/command/inc/commandInt.h +++ b/source/libs/command/inc/commandInt.h @@ -50,8 +50,9 @@ extern "C" { typedef struct SExplainGroup { int32_t nodeNum; + SRWLatch lock; SSubplan *plan; - void *execInfo; //TODO + SArray *execInfo; } SExplainGroup; typedef struct SExplainResNode { @@ -67,10 +68,18 @@ typedef struct SQueryExplainRowInfo { } SQueryExplainRowInfo; typedef struct SExplainCtx { - int32_t totalSize; + double ratio; bool verbose; + + int32_t rootGroupId; + int32_t dataSize; + bool execDone; + int64_t reqStartTs; + int64_t jobStartTs; + int64_t jobDoneTs; char *tbuf; SArray *rows; + int32_t groupDoneNum; SHashObj *groupHash; } SExplainCtx; diff --git a/source/libs/command/src/explain.c b/source/libs/command/src/explain.c index 847a863e76..4bf354dee5 100644 --- a/source/libs/command/src/explain.c +++ b/source/libs/command/src/explain.c @@ -17,32 +17,31 @@ #include "plannodes.h" #include "commandInt.h" -int32_t qGenerateExplainResNode(SPhysiNode *pNode, void *pExecInfo, SExplainResNode **pRes); -int32_t qAppendTaskExplainResRows(void *pCtx, int32_t groupId, int32_t level); +int32_t qExplainGenerateResNode(SPhysiNode *pNode, void *pExecInfo, SExplainResNode **pRes); +int32_t qExplainAppendGroupResRows(void *pCtx, int32_t groupId, int32_t level); -void qFreeExplainResTree(SExplainResNode *res) { - if (NULL == res) { +void qExplainFreeResNode(SExplainResNode *resNode) { + if (NULL == resNode) { return; } - taosMemoryFreeClear(res->pExecInfo); + taosMemoryFreeClear(resNode->pExecInfo); SNode* node = NULL; - FOREACH(node, res->pChildren) { - qFreeExplainResTree((SExplainResNode *)node); + FOREACH(node, resNode->pChildren) { + qExplainFreeResNode((SExplainResNode *)node); } - nodesClearList(res->pChildren); + nodesClearList(resNode->pChildren); - taosMemoryFreeClear(res); + taosMemoryFreeClear(resNode); } -void qFreeExplainCtx(void *ctx) { - if (NULL == ctx) { +void qExplainFreeCtx(SExplainCtx *pCtx) { + if (NULL == pCtx) { return; } - SExplainCtx *pCtx = (SExplainCtx *)ctx; int32_t rowSize = taosArrayGetSize(pCtx->rows); for (int32_t i = 0; i < rowSize; ++i) { SQueryExplainRowInfo *row = taosArrayGet(pCtx->rows, i); @@ -54,7 +53,7 @@ void qFreeExplainCtx(void *ctx) { taosMemoryFree(pCtx); } -int32_t qInitExplainCtx(void **pCtx, SHashObj *groupHash, bool verbose) { +int32_t qExplainInitCtx(SExplainCtx **pCtx, SHashObj *groupHash, bool verbose, double ratio) { int32_t code = 0; SExplainCtx *ctx = taosMemoryCalloc(1, sizeof(SExplainCtx)); if (NULL == ctx) { @@ -75,6 +74,7 @@ int32_t qInitExplainCtx(void **pCtx, SHashObj *groupHash, bool verbose) { } ctx->verbose = verbose; + ctx->ratio = ratio; ctx->tbuf = tbuf; ctx->rows = rows; ctx->groupHash = groupHash; @@ -92,35 +92,7 @@ _return: QRY_RET(code); } - -char *qFillModeString(EFillMode mode) { - switch (mode) { - case FILL_MODE_NONE: - return "none"; - case FILL_MODE_VALUE: - return "value"; - case FILL_MODE_PREV: - return "prev"; - case FILL_MODE_NULL: - return "null"; - case FILL_MODE_LINEAR: - return "linear"; - case FILL_MODE_NEXT: - return "next"; - default: - return "unknown"; - } -} - -char *qGetNameFromColumnNode(SNode *pNode) { - if (NULL == pNode || QUERY_NODE_COLUMN != pNode->type) { - return "NULL"; - } - - return ((SColumnNode *)pNode)->colName; -} - -int32_t qGenerateExplainResChildren(SPhysiNode *pNode, void *pExecInfo, SNodeList **pChildren) { +int32_t qExplainGenerateResChildren(SPhysiNode *pNode, void *pExecInfo, SNodeList **pChildren) { int32_t tlen = 0; SNodeList *pPhysiChildren = NULL; @@ -192,38 +164,38 @@ int32_t qGenerateExplainResChildren(SPhysiNode *pNode, void *pExecInfo, SNodeLis SNode* node = NULL; SExplainResNode *pResNode = NULL; FOREACH(node, pPhysiChildren) { - QRY_ERR_RET(qGenerateExplainResNode((SPhysiNode *)node, pExecInfo, &pResNode)); + QRY_ERR_RET(qExplainGenerateResNode((SPhysiNode *)node, pExecInfo, &pResNode)); QRY_ERR_RET(nodesListAppend(*pChildren, pResNode)); } return TSDB_CODE_SUCCESS; } -int32_t qGenerateExplainResNode(SPhysiNode *pNode, void *pExecInfo, SExplainResNode **pRes) { +int32_t qExplainGenerateResNode(SPhysiNode *pNode, void *pExecInfo, SExplainResNode **pResNode) { if (NULL == pNode) { - *pRes = NULL; + *pResNode = NULL; qError("physical node is NULL"); return TSDB_CODE_QRY_APP_ERROR; } - SExplainResNode *res = taosMemoryCalloc(1, sizeof(SExplainResNode)); - if (NULL == res) { + SExplainResNode *resNode = taosMemoryCalloc(1, sizeof(SExplainResNode)); + if (NULL == resNode) { qError("calloc SPhysiNodeExplainRes failed"); return TSDB_CODE_QRY_OUT_OF_MEMORY; } int32_t code = 0; - res->pNode = pNode; - res->pExecInfo = pExecInfo; - QRY_ERR_JRET(qGenerateExplainResChildren(pNode, pExecInfo, &res->pChildren)); + resNode->pNode = pNode; + resNode->pExecInfo = pExecInfo; + QRY_ERR_JRET(qExplainGenerateResChildren(pNode, pExecInfo, &resNode->pChildren)); - *pRes = res; + *pResNode = resNode; return TSDB_CODE_SUCCESS; _return: - qFreeExplainResTree(res); + qExplainFreeResNode(resNode); QRY_RET(code); } @@ -249,7 +221,7 @@ int32_t qExplainResAppendRow(SExplainCtx *ctx, char *tbuf, int32_t len, int32_t memcpy(row.buf, tbuf, len); row.level = level; row.len = len; - ctx->totalSize += len; + ctx->dataSize += len; if (NULL == taosArrayPush(ctx->rows, &row)) { qError("taosArrayPush row to explain res rows failed"); @@ -444,7 +416,7 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i } } - QRY_ERR_RET(qAppendTaskExplainResRows(ctx, pExchNode->srcGroupId, level + 1)); + QRY_ERR_RET(qExplainAppendGroupResRows(ctx, pExchNode->srcGroupId, level + 1)); break; } case QUERY_NODE_PHYSICAL_PLAN_SORT:{ @@ -468,7 +440,7 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i } case QUERY_NODE_PHYSICAL_PLAN_INTERVAL:{ SIntervalPhysiNode *pIntNode = (SIntervalPhysiNode *)pNode; - EXPLAIN_ROW_NEW(level, EXPLAIN_INTERVAL_FORMAT, qGetNameFromColumnNode(pIntNode->pTspk), pIntNode->window.pFuncs->length, + EXPLAIN_ROW_NEW(level, EXPLAIN_INTERVAL_FORMAT, nodesGetNameFromColumnNode(pIntNode->pTspk), pIntNode->window.pFuncs->length, INVERAL_TIME_FROM_PRECISION_TO_UNIT(pIntNode->interval, pIntNode->intervalUnit, pIntNode->precision), pIntNode->intervalUnit, pIntNode->offset, getPrecisionUnit(pIntNode->precision), INVERAL_TIME_FROM_PRECISION_TO_UNIT(pIntNode->sliding, pIntNode->slidingUnit, pIntNode->precision), pIntNode->slidingUnit, @@ -481,7 +453,7 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i if (verbose) { if (pIntNode->pFill) { - EXPLAIN_ROW_NEW(level + 1, EXPLAIN_FILL_FORMAT, qFillModeString(pIntNode->pFill->mode)); + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_FILL_FORMAT, getFillModeString(pIntNode->pFill->mode)); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); } @@ -540,7 +512,7 @@ int32_t qExplainResNodeToRows(SExplainResNode *pResNode, SExplainCtx *ctx, int32 return TSDB_CODE_SUCCESS; } -int32_t qAppendTaskExplainResRows(void *pCtx, int32_t groupId, int32_t level) { +int32_t qExplainAppendGroupResRows(void *pCtx, int32_t groupId, int32_t level) { SExplainResNode *node = NULL; int32_t code = 0; SExplainCtx *ctx = (SExplainCtx *)pCtx; @@ -551,19 +523,19 @@ int32_t qAppendTaskExplainResRows(void *pCtx, int32_t groupId, int32_t level) { QRY_ERR_RET(TSDB_CODE_QRY_APP_ERROR); } - QRY_ERR_RET(qGenerateExplainResNode(group->plan->pNode, group->execInfo, &node)); + QRY_ERR_RET(qExplainGenerateResNode(group->plan->pNode, group->execInfo, &node)); QRY_ERR_JRET(qExplainResNodeToRows(node, ctx, level)); _return: - qFreeExplainResTree(node); + qExplainFreeResNode(node); QRY_RET(code); } -int32_t qGetExplainRspFromCtx(void *ctx, SRetrieveTableRsp **pRsp) { +int32_t qExplainGetRspFromCtx(void *ctx, SRetrieveTableRsp **pRsp) { SExplainCtx *pCtx = (SExplainCtx *)ctx; int32_t rowNum = taosArrayGetSize(pCtx->rows); if (rowNum <= 0) { @@ -572,7 +544,7 @@ int32_t qGetExplainRspFromCtx(void *ctx, SRetrieveTableRsp **pRsp) { } int32_t colNum = 1; - int32_t rspSize = sizeof(SRetrieveTableRsp) + sizeof(int32_t) * colNum + sizeof(int32_t) * rowNum + pCtx->totalSize; + int32_t rspSize = sizeof(SRetrieveTableRsp) + sizeof(int32_t) * colNum + sizeof(int32_t) * rowNum + pCtx->dataSize; SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)taosMemoryCalloc(1, rspSize); if (NULL == rsp) { qError("malloc SRetrieveTableRsp failed, size:%d", rspSize); @@ -582,7 +554,7 @@ int32_t qGetExplainRspFromCtx(void *ctx, SRetrieveTableRsp **pRsp) { rsp->completed = 1; rsp->numOfRows = htonl(rowNum); - *(int32_t *)rsp->data = htonl(pCtx->totalSize); + *(int32_t *)rsp->data = htonl(pCtx->dataSize); int32_t *offset = (int32_t *)((char *)rsp->data + sizeof(int32_t)); char *data = (char *)(offset + rowNum); @@ -604,13 +576,13 @@ int32_t qGetExplainRspFromCtx(void *ctx, SRetrieveTableRsp **pRsp) { return TSDB_CODE_SUCCESS; } -int32_t qExecStaticExplain(SQueryPlan *pDag, SRetrieveTableRsp **pRsp) { + +int32_t qExplainPrepareCtx(SQueryPlan *pDag, SExplainCtx **pCtx) { int32_t code = 0; SNodeListNode *plans = NULL; int32_t taskNum = 0; SExplainGroup *pGroup = NULL; - void *pCtx = NULL; - int32_t rootGroupId = 0; + SExplainCtx *ctx = NULL; if (pDag->numOfSubplans <= 0) { qError("invalid subplan num:%d", pDag->numOfSubplans); @@ -629,7 +601,7 @@ int32_t qExecStaticExplain(SQueryPlan *pDag, SRetrieveTableRsp **pRsp) { QRY_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); } - QRY_ERR_JRET(qInitExplainCtx(&pCtx, groupHash, pDag->explainInfo.verbose)); + QRY_ERR_JRET(qExplainInitCtx(&ctx, groupHash, pDag->explainInfo.verbose, pDag->explainInfo.ratio)); for (int32_t i = 0; i < levelNum; ++i) { plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i); @@ -666,22 +638,62 @@ int32_t qExecStaticExplain(SQueryPlan *pDag, SRetrieveTableRsp **pRsp) { QRY_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT); } - rootGroupId = plan->id.groupId; + ctx->rootGroupId = plan->id.groupId; } qDebug("level %d group handled, taskNum:%d", i, taskNum); } - QRY_ERR_JRET(qAppendTaskExplainResRows(pCtx, rootGroupId, 0)); - - QRY_ERR_JRET(qGetExplainRspFromCtx(pCtx, pRsp)); + *pCtx = ctx; + + return TSDB_CODE_SUCCESS; _return: - qFreeExplainCtx(pCtx); + qExplainFreeCtx(ctx); QRY_RET(code); } +int32_t qExplainUpdateExecInfo(SExplainCtx *pCtx, SExplainRsp *pRspMsg, int32_t groupId, SRetrieveTableRsp **pRsp) { + +} + + +int32_t qExecStaticExplain(SQueryPlan *pDag, SRetrieveTableRsp **pRsp) { + int32_t code = 0; + SExplainCtx *pCtx = NULL; + + QRY_ERR_RET(qExplainPrepareCtx(pDag, &pCtx)); + + QRY_ERR_JRET(qExplainAppendGroupResRows(pCtx, pCtx->rootGroupId, 0)); + + QRY_ERR_JRET(qExplainGetRspFromCtx(pCtx, pRsp)); + +_return: + + qExplainFreeCtx(pCtx); + + QRY_RET(code); +} + +int32_t qExecExplainBegin(SQueryPlan *pDag, SExplainCtx **pCtx, int32_t startTs) { + QRY_ERR_RET(qExplainPrepareCtx(pDag, pCtx)); + + (*pCtx)->reqStartTs = startTs; + (*pCtx)->jobStartTs = taosGetTimestampMs(); + + return TSDB_CODE_SUCCESS; +} + +int32_t qExecExplainEnd(SExplainCtx *pCtx) { + pCtx->jobDoneTs = taosGetTimestampMs(); + + atomic_store_8((int8_t *)&pCtx->execDone, true); + + return TSDB_CODE_SUCCESS; +} + + diff --git a/source/libs/executor/src/executorMain.c b/source/libs/executor/src/executorMain.c index 6d78fa835a..d7c99d54df 100644 --- a/source/libs/executor/src/executorMain.c +++ b/source/libs/executor/src/executorMain.c @@ -229,3 +229,12 @@ void qDestroyTask(qTaskInfo_t qTaskHandle) { queryCostStatis(pTaskInfo); // print the query cost summary doDestroyTask(pTaskInfo); } + +int32_t qGetExplainExecInfo(qTaskInfo_t tinfo, int32_t *resNum, SExplainExecInfo **pRes) { + SExecTaskInfo *pTaskInfo = (SExecTaskInfo *)tinfo; + int32_t capacity = 0; + + return getOperatorExplainExecInfo(pTaskInfo->pRoot, pRes, &capacity, resNum); +} + + diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 169e47eb33..e423690740 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -9516,3 +9516,18 @@ void releaseQueryBuf(size_t numOfTables) { // restore value is not enough buffer available atomic_add_fetch_64(&tsQueryBufferSizeBytes, t); } + +int32_t getOperatorExplainExecInfo(SOperatorInfo *operator, SExplainExecInfo **pRes, int32_t *capacity, int32_t *resNum) { + if (*resNum >= *capacity) { + *capacity += 10; + + *pRes = taosMemoryRealloc(*pRes, (*capacity) * sizeof(SExplainExecInfo)); + if (NULL == *pRes) { + qError("malloc %d failed", capacity * sizeof(SExplainExecInfo)); + return TSDB_CODE_QRY_OUT_OF_MEMORY; + } + } + +} + + diff --git a/source/libs/nodes/inc/nodesUtil.h b/source/libs/nodes/inc/nodesUtil.h index 976044c16f..c6233ba980 100644 --- a/source/libs/nodes/inc/nodesUtil.h +++ b/source/libs/nodes/inc/nodesUtil.h @@ -31,6 +31,7 @@ extern "C" { #define NODES_RET(c) do { int32_t _code = c; if (_code != TSDB_CODE_SUCCESS) { terrno = _code; } return _code; } while (0) #define NODES_ERR_JRET(c) do { code = c; if (code != TSDB_CODE_SUCCESS) { terrno = code; goto _return; } } while (0) + #ifdef __cplusplus } #endif diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index 732d9acfbe..812fa333b6 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -1023,3 +1023,33 @@ int32_t nodesCollectFuncs(SSelectStmt* pSelect, FFuncClassifier classifier, SNod return TSDB_CODE_SUCCESS; } + + +char *getFillModeString(EFillMode mode) { + switch (mode) { + case FILL_MODE_NONE: + return "none"; + case FILL_MODE_VALUE: + return "value"; + case FILL_MODE_PREV: + return "prev"; + case FILL_MODE_NULL: + return "null"; + case FILL_MODE_LINEAR: + return "linear"; + case FILL_MODE_NEXT: + return "next"; + default: + return "unknown"; + } +} + +char *nodesGetNameFromColumnNode(SNode *pNode) { + if (NULL == pNode || QUERY_NODE_COLUMN != pNode->type) { + return "NULL"; + } + + return ((SColumnNode *)pNode)->colName; +} + + diff --git a/source/libs/qworker/inc/qworkerInt.h b/source/libs/qworker/inc/qworkerInt.h index 573eaed2e6..cfd4a3ec7b 100644 --- a/source/libs/qworker/inc/qworkerInt.h +++ b/source/libs/qworker/inc/qworkerInt.h @@ -107,15 +107,17 @@ typedef struct SQWTaskCtx { SRWLatch lock; int8_t phase; int8_t taskType; + int8_t explain; - bool emptyRes; bool queryFetched; bool queryEnd; bool queryContinue; bool queryInQueue; int32_t rspCode; - SQWConnInfo connInfo; + SQWConnInfo ctrlConnInfo; + SQWConnInfo dataConnInfo; + int8_t events[QW_EVENT_MAX]; qTaskInfo_t taskHandle; diff --git a/source/libs/qworker/src/qworker.c b/source/libs/qworker/src/qworker.c index 42d10a2361..726d0ebf79 100644 --- a/source/libs/qworker/src/qworker.c +++ b/source/libs/qworker/src/qworker.c @@ -432,8 +432,10 @@ int32_t qwKillTaskHandle(QW_FPARAMS_DEF, SQWTaskCtx *ctx) { void qwFreeTask(QW_FPARAMS_DEF, SQWTaskCtx *ctx) { - tmsgReleaseHandle(ctx->connInfo.handle, TAOS_CONN_SERVER); - ctx->connInfo.handle = NULL; + tmsgReleaseHandle(ctx->ctrlConnInfo.handle, TAOS_CONN_SERVER); + ctx->ctrlConnInfo.handle = NULL; + + // NO need to release dataConnInfo qwFreeTaskHandle(QW_FPARAMS(), &ctx->taskHandle); @@ -537,6 +539,26 @@ int32_t qwDropTask(QW_FPARAMS_DEF) { return TSDB_CODE_SUCCESS; } + +int32_t qwHandleTaskComplete(QW_FPARAMS_DEF, SQWTaskCtx *ctx) { + qTaskInfo_t *taskHandle = &ctx->taskHandle; + + if (TASK_TYPE_TEMP == ctx->taskType) { + if (ctx->explain) { + SExplainExecInfo *execInfo = NULL; + int32_t resNum = 0; + QW_ERR_RET(qGetExplainExecInfo(ctx->taskHandle, &resNum, &execInfo)); + + QW_ERR_RET(qwBuildAndSendExplainRsp(&ctx->ctrlConnInfo, &rsp)); + } + + qwFreeTaskHandle(QW_FPARAMS(), taskHandle); + } + + return TSDB_CODE_SUCCESS; +} + + int32_t qwExecTask(QW_FPARAMS_DEF, SQWTaskCtx *ctx, bool *queryEnd) { int32_t code = 0; bool qcontinue = true; @@ -562,10 +584,8 @@ int32_t qwExecTask(QW_FPARAMS_DEF, SQWTaskCtx *ctx, bool *queryEnd) { QW_TASK_DLOG("qExecTask end with empty res, useconds:%"PRIu64, useconds); dsEndPut(sinkHandle, useconds); - - if (TASK_TYPE_TEMP == ctx->taskType) { - qwFreeTaskHandle(QW_FPARAMS(), taskHandle); - } + + QW_ERR_RET(qwHandleTaskComplete(QW_FPARAMS(), ctx)); if (queryEnd) { *queryEnd = true; @@ -658,19 +678,6 @@ int32_t qwGetResFromSink(QW_FPARAMS_DEF, SQWTaskCtx *ctx, int32_t *dataLen, void bool queryEnd = false; int32_t code = 0; - if (ctx->emptyRes) { - QW_TASK_DLOG_E("query end with empty result"); - - qwUpdateTaskStatus(QW_FPARAMS(), JOB_TASK_STATUS_SUCCEED); - QW_ERR_RET(qwMallocFetchRsp(len, &rsp)); - - *rspMsg = rsp; - *dataLen = 0; - pOutput->queryEnd = true; - - return TSDB_CODE_SUCCESS; - } - dsGetDataLength(ctx->sinkHandle, &len, &queryEnd); if (len < 0) { @@ -760,12 +767,12 @@ int32_t qwHandlePrePhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *inpu } if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) { - dropConnection = &ctx->connInfo; + dropConnection = &ctx->ctrlConnInfo; QW_ERR_JRET(qwDropTask(QW_FPARAMS())); dropConnection = NULL; - qwBuildAndSendDropRsp(&ctx->connInfo, code); - QW_TASK_DLOG("drop rsp send, handle:%p, code:%x - %s", ctx->connInfo.handle, code, tstrerror(code)); + qwBuildAndSendDropRsp(&ctx->ctrlConnInfo, code); + QW_TASK_DLOG("drop rsp send, handle:%p, code:%x - %s", ctx->ctrlConnInfo.handle, code, tstrerror(code)); QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED); break; @@ -798,12 +805,12 @@ int32_t qwHandlePrePhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *inpu } if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_DROP)) { - dropConnection = &ctx->connInfo; + dropConnection = &ctx->ctrlConnInfo; QW_ERR_JRET(qwDropTask(QW_FPARAMS())); dropConnection = NULL; - qwBuildAndSendDropRsp(&ctx->connInfo, code); - QW_TASK_DLOG("drop rsp send, handle:%p, code:%x - %s", ctx->connInfo.handle, code, tstrerror(code)); + qwBuildAndSendDropRsp(&ctx->ctrlConnInfo, code); + QW_TASK_DLOG("drop rsp send, handle:%p, code:%x - %s", ctx->ctrlConnInfo.handle, code, tstrerror(code)); QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED); } @@ -863,17 +870,13 @@ int32_t qwHandlePostPhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *inp } if (QW_PHASE_POST_QUERY == phase) { - if (NULL == ctx->taskHandle && NULL == ctx->sinkHandle) { - ctx->emptyRes = true; - } - #if 0 if (QW_IS_EVENT_RECEIVED(ctx, QW_EVENT_READY)) { readyConnection = &ctx->connInfo; QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_READY); } #else - connInfo.handle = ctx->connInfo.handle; + connInfo.handle = ctx->ctrlConnInfo.handle; readyConnection = &connInfo; QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_READY); @@ -886,8 +889,8 @@ int32_t qwHandlePostPhaseEvents(QW_FPARAMS_DEF, int8_t phase, SQWPhaseInput *inp QW_ERR_JRET(TSDB_CODE_QRY_APP_ERROR); } - qwBuildAndSendDropRsp(&ctx->connInfo, code); - QW_TASK_DLOG("drop rsp send, handle:%p, code:%x - %s", ctx->connInfo.handle, code, tstrerror(code)); + qwBuildAndSendDropRsp(&ctx->ctrlConnInfo, code); + QW_TASK_DLOG("drop rsp send, handle:%p, code:%x - %s", ctx->ctrlConnInfo.handle, code, tstrerror(code)); QW_ERR_JRET(qwDropTask(QW_FPARAMS())); QW_ERR_JRET(TSDB_CODE_QRY_TASK_DROPPED); @@ -931,7 +934,7 @@ _return: QW_RET(code); } -int32_t qwProcessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg, int8_t taskType) { +int32_t qwProcessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg, int8_t taskType, int8_t explain) { int32_t code = 0; bool queryRsped = false; struct SSubplan *plan = NULL; @@ -947,9 +950,10 @@ int32_t qwProcessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg, int8_t taskType) { QW_ERR_JRET(qwGetTaskCtx(QW_FPARAMS(), &ctx)); atomic_store_8(&ctx->taskType, taskType); + atomic_store_8(&ctx->explain, explain); - atomic_store_ptr(&ctx->connInfo.handle, qwMsg->connInfo.handle); - atomic_store_ptr(&ctx->connInfo.ahandle, qwMsg->connInfo.ahandle); + atomic_store_ptr(&ctx->ctrlConnInfo.handle, qwMsg->connInfo.handle); + atomic_store_ptr(&ctx->ctrlConnInfo.ahandle, qwMsg->connInfo.ahandle); QW_TASK_DLOGL("subplan json string, len:%d, %s", qwMsg->msgLen, qwMsg->msg); @@ -1011,8 +1015,8 @@ int32_t qwProcessReady(QW_FPARAMS_DEF, SQWMsg *qwMsg) { } if (ctx->phase == QW_PHASE_PRE_QUERY) { - ctx->connInfo.handle == qwMsg->connInfo.handle; - ctx->connInfo.ahandle = qwMsg->connInfo.ahandle; + ctx->ctrlConnInfo.handle == qwMsg->connInfo.handle; + ctx->ctrlConnInfo.ahandle = qwMsg->connInfo.ahandle; QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_READY); needRsp = false; QW_TASK_DLOG_E("ready msg will not rsp now"); @@ -1089,10 +1093,13 @@ int32_t qwProcessCQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg) { if (rsp) { bool qComplete = (DS_BUF_EMPTY == sOutput.bufStatus && sOutput.queryEnd); + qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete); - atomic_store_8((int8_t*)&ctx->queryEnd, qComplete); + if (qComplete) { + atomic_store_8((int8_t*)&ctx->queryEnd, true); + } - qwMsg->connInfo = ctx->connInfo; + qwMsg->connInfo = ctx->dataConnInfo; QW_SET_EVENT_PROCESSED(ctx, QW_EVENT_FETCH); qwBuildAndSendFetchRsp(&qwMsg->connInfo, rsp, dataLen, code); @@ -1113,7 +1120,7 @@ _return: qwFreeFetchRsp(rsp); rsp = NULL; - qwMsg->connInfo = ctx->connInfo; + qwMsg->connInfo = ctx->dataConnInfo; qwBuildAndSendFetchRsp(&qwMsg->connInfo, rsp, 0, code); QW_TASK_DLOG("fetch rsp send, handle:%p, code:%x - %s, dataLen:%d", qwMsg->connInfo.handle, code, tstrerror(code), 0); } @@ -1151,14 +1158,17 @@ int32_t qwProcessFetch(QW_FPARAMS_DEF, SQWMsg *qwMsg) { QW_ERR_JRET(qwGetResFromSink(QW_FPARAMS(), ctx, &dataLen, &rsp, &sOutput)); if (NULL == rsp) { - atomic_store_ptr(&ctx->connInfo.handle, qwMsg->connInfo.handle); - atomic_store_ptr(&ctx->connInfo.ahandle, qwMsg->connInfo.ahandle); + atomic_store_ptr(&ctx->dataConnInfo.handle, qwMsg->connInfo.handle); + atomic_store_ptr(&ctx->dataConnInfo.ahandle, qwMsg->connInfo.ahandle); QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_FETCH); } else { bool qComplete = (DS_BUF_EMPTY == sOutput.bufStatus && sOutput.queryEnd); + qwBuildFetchRsp(rsp, &sOutput, dataLen, qComplete); - atomic_store_8((int8_t*)&ctx->queryEnd, qComplete); + if (qComplete) { + atomic_store_8((int8_t*)&ctx->queryEnd, true); + } } if ((!sOutput.queryEnd) && (DS_BUF_LOW == sOutput.bufStatus || DS_BUF_EMPTY == sOutput.bufStatus)) { @@ -1236,8 +1246,8 @@ int32_t qwProcessDrop(QW_FPARAMS_DEF, SQWMsg *qwMsg) { } if (!rsped) { - ctx->connInfo.handle = qwMsg->connInfo.handle; - ctx->connInfo.ahandle = qwMsg->connInfo.ahandle; + ctx->ctrlConnInfo.handle = qwMsg->connInfo.handle; + ctx->ctrlConnInfo.ahandle = qwMsg->connInfo.ahandle; QW_SET_EVENT_RECEIVED(ctx, QW_EVENT_DROP); } diff --git a/source/libs/qworker/src/qworkerMsg.c b/source/libs/qworker/src/qworkerMsg.c index 80ae013653..c9779f9252 100644 --- a/source/libs/qworker/src/qworkerMsg.c +++ b/source/libs/qworker/src/qworkerMsg.c @@ -327,7 +327,7 @@ int32_t qWorkerProcessQueryMsg(void *node, void *qWorkerMgmt, SRpcMsg *pMsg) { QW_SCH_TASK_DLOG("processQuery start, node:%p, handle:%p, sql:%s", node, pMsg->handle, sql); taosMemoryFreeClear(sql); - QW_ERR_RET(qwProcessQuery(QW_FPARAMS(), &qwMsg, msg->taskType)); + QW_ERR_RET(qwProcessQuery(QW_FPARAMS(), &qwMsg, msg->taskType, msg->explain)); QW_SCH_TASK_DLOG("processQuery end, node:%p", node); diff --git a/source/libs/scheduler/inc/schedulerInt.h b/source/libs/scheduler/inc/schedulerInt.h index 22bd039219..62a96b6438 100644 --- a/source/libs/scheduler/inc/schedulerInt.h +++ b/source/libs/scheduler/inc/schedulerInt.h @@ -26,6 +26,7 @@ extern "C" { #include "scheduler.h" #include "thash.h" #include "trpc.h" +#include "command.h" #define SCHEDULE_DEFAULT_MAX_JOB_NUM 1000 #define SCHEDULE_DEFAULT_MAX_TASK_NUM 1000 @@ -165,6 +166,7 @@ typedef struct SSchJob { SHashObj *succTasks; // succeed tasks, key:taskid, value:SQueryTask* SHashObj *failTasks; // failed tasks, key:taskid, value:SQueryTask* + SExplainCtx *explainCtx; int8_t status; SQueryNodeAddr resNode; tsem_t rspSem; @@ -211,6 +213,7 @@ extern SSchedulerMgmt schMgmt; #define SCH_JOB_NEED_FETCH(_job) SCH_IS_QUERY_JOB(_job) #define SCH_IS_WAIT_ALL_JOB(_job) (!SCH_IS_QUERY_JOB(_job)) #define SCH_IS_NEED_DROP_JOB(_job) (SCH_IS_QUERY_JOB(_job)) +#define SCH_IS_EXPLAIN_JOB(_job) (EXPLAIN_MODE_ANALYZE == (_job)->attr.explainMode) #define SCH_IS_LEVEL_UNFINISHED(_level) ((_level)->taskLaunchedNum < (_level)->taskNum) #define SCH_GET_CUR_EP(_addr) (&(_addr)->epSet.eps[(_addr)->epSet.inUse]) @@ -251,6 +254,8 @@ int32_t schFetchFromRemote(SSchJob *pJob); int32_t schProcessOnTaskFailure(SSchJob *pJob, SSchTask *pTask, int32_t errCode); int32_t schBuildAndSendHbMsg(SQueryNodeEpId *nodeEpId); int32_t schCloneSMsgSendInfo(void *src, void **dst); +int32_t schValidateAndBuildJob(SQueryPlan *pDag, SSchJob *pJob); +void schFreeJobImpl(void *job); #ifdef __cplusplus diff --git a/source/libs/scheduler/src/scheduler.c b/source/libs/scheduler/src/scheduler.c index 9c5019aedc..658d924f38 100644 --- a/source/libs/scheduler/src/scheduler.c +++ b/source/libs/scheduler/src/scheduler.c @@ -67,6 +67,81 @@ int32_t schInitTask(SSchJob *pJob, SSchTask *pTask, SSubplan *pPlan, SSchLevel * return TSDB_CODE_SUCCESS; } +int32_t schInitJob(SSchJob **pSchJob, SQueryPlan *pDag, void *transport, SArray *pNodeList, const char *sql, + int64_t startTs, bool syncSchedule) { + int32_t code = 0; + SSchJob *pJob = taosMemoryCalloc(1, sizeof(SSchJob)); + if (NULL == pJob) { + qError("QID:%" PRIx64 " calloc %d failed", pDag->queryId, (int32_t)sizeof(SSchJob)); + SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); + } + + pJob->attr.explainMode = pDag->explainInfo.mode; + pJob->attr.syncSchedule = syncSchedule; + pJob->transport = transport; + pJob->sql = sql; + + if (pNodeList != NULL) { + pJob->nodeList = taosArrayDup(pNodeList); + } + + SCH_ERR_JRET(schValidateAndBuildJob(pDag, pJob)); + + if (SCH_IS_EXPLAIN_JOB(pJob)) { + SCH_ERR_JRET(qExecExplainBegin(pDag, &pJob->explainCtx, startTs)); + } + + pJob->execTasks = + taosHashInit(pDag->numOfSubplans, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_ENTRY_LOCK); + if (NULL == pJob->execTasks) { + SCH_JOB_ELOG("taosHashInit %d execTasks failed", pDag->numOfSubplans); + SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); + } + + pJob->succTasks = + taosHashInit(pDag->numOfSubplans, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_ENTRY_LOCK); + if (NULL == pJob->succTasks) { + SCH_JOB_ELOG("taosHashInit %d succTasks failed", pDag->numOfSubplans); + SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); + } + + pJob->failTasks = + taosHashInit(pDag->numOfSubplans, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_ENTRY_LOCK); + if (NULL == pJob->failTasks) { + SCH_JOB_ELOG("taosHashInit %d failTasks failed", pDag->numOfSubplans); + SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); + } + + tsem_init(&pJob->rspSem, 0, 0); + + int64_t refId = taosAddRef(schMgmt.jobRef, pJob); + if (refId < 0) { + SCH_JOB_ELOG("taosAddRef job failed, error:%s", tstrerror(terrno)); + SCH_ERR_JRET(terrno); + } + + if (NULL == schAcquireJob(refId)) { + SCH_JOB_ELOG("schAcquireJob job failed, refId:%" PRIx64, refId); + SCH_RET(TSDB_CODE_SCH_STATUS_ERROR); + } + + pJob->refId = refId; + + SCH_JOB_DLOG("job refId:%" PRIx64, pJob->refId); + + pJob->status = JOB_TASK_STATUS_NOT_START; + + *pSchJob = pJob; + + return TSDB_CODE_SUCCESS; + +_return: + + schFreeJobImpl(pJob); + SCH_RET(code); +} + + void schFreeRpcCtx(SRpcCtx *pCtx) { if (NULL == pCtx) { return; @@ -1050,6 +1125,40 @@ int32_t schHandleResponseMsg(SSchJob *pJob, SSchTask *pTask, int32_t msgType, ch break; } + case TDMT_VND_EXPLAIN_RSP: { + SExplainRsp *taskRsp = (SExplainRsp *)msg; + + SCH_ERR_JRET(rspCode); + if (NULL == msg) { + SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT); + } + + if (!SCH_IS_EXPLAIN_JOB(pJob)) { + SCH_TASK_ELOG("invalid msg received for none explain query, msg type:%s", TMSG_INFO(msgType)); + SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT); + } + + if (pJob->resData) { + SCH_TASK_ELOG("explain result is already generated, res:%p", pJob->resData); + taosMemoryFreeClear(taskRsp); + SCH_ERR_JRET(TSDB_CODE_SCH_STATUS_ERROR); + } + + SRetrieveTableRsp *pRsp = NULL; + SCH_ERR_JRET(qExplainUpdateExecInfo(pJob->explainCtx, taskRsp, pTask->plan->id.groupId, &pRsp)); + + if (pRsp) { + atomic_store_ptr(&pJob->resData, pRsp); + atomic_store_32(&pJob->resNumOfRows, htonl(pRsp->numOfRows)); + + SCH_SET_TASK_STATUS(pTask, JOB_TASK_STATUS_SUCCEED); + + SCH_TASK_DLOG("got explain rsp, rows:%d, complete:%d", htonl(pRsp->numOfRows), pRsp->completed); + + schProcessOnDataFetched(pJob); + } + break; + } case TDMT_VND_FETCH_RSP: { SRetrieveTableRsp *rsp = (SRetrieveTableRsp *)msg; @@ -1058,6 +1167,17 @@ int32_t schHandleResponseMsg(SSchJob *pJob, SSchTask *pTask, int32_t msgType, ch SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT); } + if (SCH_IS_EXPLAIN_JOB(pJob)) { + if (rsp->completed) { + SCH_ERR_JRET(qExecExplainEnd(pJob->explainCtx)); + return TSDB_CODE_SUCCESS; + } + + SCH_ERR_JRET(schFetchFromRemote(pJob)); + + return TSDB_CODE_SUCCESS; + } + if (pJob->resData) { SCH_TASK_ELOG("got fetch rsp while res already exists, res:%p", pJob->resData); taosMemoryFreeClear(rsp); @@ -1767,6 +1887,7 @@ int32_t schBuildAndSendMsg(SSchJob *pJob, SSchTask *pTask, SQueryNodeAddr *addr, pMsg->taskId = htobe64(pTask->taskId); pMsg->refId = htobe64(pJob->refId); pMsg->taskType = TASK_TYPE_TEMP; + pMsg->explain = SCH_IS_EXPLAIN_JOB(pJob); pMsg->phyLen = htonl(pTask->msgLen); pMsg->sqlLen = htonl(len); @@ -2083,6 +2204,8 @@ void schFreeJobImpl(void *job) { taosArrayDestroy(pJob->levels); taosArrayDestroy(pJob->nodeList); + qExplainFreeCtx(pJob->explainCtx); + taosMemoryFreeClear(pJob->resData); taosMemoryFreeClear(pJob); @@ -2090,70 +2213,17 @@ void schFreeJobImpl(void *job) { } static int32_t schExecJobImpl(void *transport, SArray *pNodeList, SQueryPlan *pDag, int64_t *job, const char *sql, - bool syncSchedule) { + int64_t startTs, bool syncSchedule) { qDebug("QID:0x%" PRIx64 " job started", pDag->queryId); - if (pNodeList == NULL || (pNodeList && taosArrayGetSize(pNodeList) <= 0)) { + if (pNodeList == NULL || taosArrayGetSize(pNodeList) <= 0) { qDebug("QID:0x%" PRIx64 " input exec nodeList is empty", pDag->queryId); } int32_t code = 0; - SSchJob *pJob = taosMemoryCalloc(1, sizeof(SSchJob)); - if (NULL == pJob) { - qError("QID:%" PRIx64 " calloc %d failed", pDag->queryId, (int32_t)sizeof(SSchJob)); - SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); - } + SSchJob *pJob = NULL; + SCH_ERR_JRET(schInitJob(&pJob, pDag, transport, pNodeList, sql, startTs, syncSchedule)); - pJob->attr.explainMode = pDag->explainInfo.mode; - pJob->attr.syncSchedule = syncSchedule; - pJob->transport = transport; - pJob->sql = sql; - - if (pNodeList != NULL) { - pJob->nodeList = taosArrayDup(pNodeList); - } - - SCH_ERR_JRET(schValidateAndBuildJob(pDag, pJob)); - - pJob->execTasks = - taosHashInit(pDag->numOfSubplans, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_ENTRY_LOCK); - if (NULL == pJob->execTasks) { - SCH_JOB_ELOG("taosHashInit %d execTasks failed", pDag->numOfSubplans); - SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); - } - - pJob->succTasks = - taosHashInit(pDag->numOfSubplans, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_ENTRY_LOCK); - if (NULL == pJob->succTasks) { - SCH_JOB_ELOG("taosHashInit %d succTasks failed", pDag->numOfSubplans); - SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); - } - - pJob->failTasks = - taosHashInit(pDag->numOfSubplans, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_ENTRY_LOCK); - if (NULL == pJob->failTasks) { - SCH_JOB_ELOG("taosHashInit %d failTasks failed", pDag->numOfSubplans); - SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); - } - - tsem_init(&pJob->rspSem, 0, 0); - - int64_t refId = taosAddRef(schMgmt.jobRef, pJob); - if (refId < 0) { - SCH_JOB_ELOG("taosAddRef job failed, error:%s", tstrerror(terrno)); - SCH_ERR_JRET(terrno); - } - - if (NULL == schAcquireJob(refId)) { - SCH_JOB_ELOG("schAcquireJob job failed, refId:%" PRIx64, refId); - SCH_RET(TSDB_CODE_SCH_STATUS_ERROR); - } - - pJob->refId = refId; - - SCH_JOB_DLOG("job refId:%" PRIx64, pJob->refId); - - pJob->status = JOB_TASK_STATUS_NOT_START; SCH_ERR_JRET(schLaunchJob(pJob)); *job = pJob->refId; @@ -2266,7 +2336,7 @@ int32_t schedulerInit(SSchedulerCfg *cfg) { } int32_t schedulerExecJob(void *transport, SArray *nodeList, SQueryPlan *pDag, int64_t *pJob, const char *sql, - SQueryResult *pRes) { + int64_t startTs, SQueryResult *pRes) { if (NULL == transport || NULL == pDag || NULL == pDag->pSubplans || NULL == pJob || NULL == pRes) { SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); } @@ -2274,7 +2344,7 @@ int32_t schedulerExecJob(void *transport, SArray *nodeList, SQueryPlan *pDag, in if (EXPLAIN_MODE_STATIC == pDag->explainInfo.mode) { SCH_ERR_RET(schExecStaticExplain(transport, nodeList, pDag, pJob, sql, true)); } else { - SCH_ERR_RET(schExecJobImpl(transport, nodeList, pDag, pJob, sql, true)); + SCH_ERR_RET(schExecJobImpl(transport, nodeList, pDag, pJob, sql, startTs, true)); } SSchJob *job = schAcquireJob(*pJob); @@ -2292,7 +2362,11 @@ int32_t schedulerAsyncExecJob(void *transport, SArray *pNodeList, SQueryPlan *pD SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); } - SCH_ERR_RET(schExecJobImpl(transport, pNodeList, pDag, pJob, sql, false)); + if (EXPLAIN_MODE_STATIC == pDag->explainInfo.mode) { + SCH_ERR_RET(schExecStaticExplain(transport, pNodeList, pDag, pJob, sql, false)); + } else { + SCH_ERR_RET(schExecJobImpl(transport, pNodeList, pDag, pJob, sql, 0, false)); + } return TSDB_CODE_SUCCESS; } diff --git a/source/libs/scheduler/test/schedulerTests.cpp b/source/libs/scheduler/test/schedulerTests.cpp index cf04b06579..5d9322a575 100644 --- a/source/libs/scheduler/test/schedulerTests.cpp +++ b/source/libs/scheduler/test/schedulerTests.cpp @@ -951,7 +951,7 @@ TEST(insertTest, normalCase) { taosThreadCreate(&(thread1), &thattr, schtSendRsp, &insertJobRefId); SQueryResult res = {0}; - code = schedulerExecJob(mockPointer, qnodeList, &dag, &insertJobRefId, "insert into tb values(now,1)", &res); + code = schedulerExecJob(mockPointer, qnodeList, &dag, &insertJobRefId, "insert into tb values(now,1)", 0, &res); ASSERT_EQ(code, 0); ASSERT_EQ(res.numOfRows, 20); From 08ed99b0692cef6d791887dd2351f6bb8f1d1523 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 2 Apr 2022 02:56:09 +0000 Subject: [PATCH 02/25] more TDB --- source/libs/tdb/src/btree/tdbBtreeBalance.c | 14 ------ source/libs/tdb/src/btree/tdbBtreeCommon.c | 14 ------ source/libs/tdb/src/btree/tdbBtreeDelete.c | 14 ------ source/libs/tdb/src/btree/tdbBtreeInsert.c | 14 ------ source/libs/tdb/src/btree/tdbBtreeInt.h | 27 ------------ source/libs/tdb/src/btree/tdbBtreeOpen.c | 14 ------ source/libs/tdb/src/db/tdbPCache.c | 47 ++++++++++++--------- source/libs/tdb/src/inc/tdbPCache.h | 1 + source/libs/tdb/src/inc/tdbTxn.h | 1 + 9 files changed, 29 insertions(+), 117 deletions(-) delete mode 100644 source/libs/tdb/src/btree/tdbBtreeBalance.c delete mode 100644 source/libs/tdb/src/btree/tdbBtreeCommon.c delete mode 100644 source/libs/tdb/src/btree/tdbBtreeDelete.c delete mode 100644 source/libs/tdb/src/btree/tdbBtreeInsert.c delete mode 100644 source/libs/tdb/src/btree/tdbBtreeInt.h delete mode 100644 source/libs/tdb/src/btree/tdbBtreeOpen.c diff --git a/source/libs/tdb/src/btree/tdbBtreeBalance.c b/source/libs/tdb/src/btree/tdbBtreeBalance.c deleted file mode 100644 index 6dea4a4e57..0000000000 --- a/source/libs/tdb/src/btree/tdbBtreeBalance.c +++ /dev/null @@ -1,14 +0,0 @@ -/* - * 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 . - */ \ No newline at end of file diff --git a/source/libs/tdb/src/btree/tdbBtreeCommon.c b/source/libs/tdb/src/btree/tdbBtreeCommon.c deleted file mode 100644 index 6dea4a4e57..0000000000 --- a/source/libs/tdb/src/btree/tdbBtreeCommon.c +++ /dev/null @@ -1,14 +0,0 @@ -/* - * 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 . - */ \ No newline at end of file diff --git a/source/libs/tdb/src/btree/tdbBtreeDelete.c b/source/libs/tdb/src/btree/tdbBtreeDelete.c deleted file mode 100644 index 6dea4a4e57..0000000000 --- a/source/libs/tdb/src/btree/tdbBtreeDelete.c +++ /dev/null @@ -1,14 +0,0 @@ -/* - * 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 . - */ \ No newline at end of file diff --git a/source/libs/tdb/src/btree/tdbBtreeInsert.c b/source/libs/tdb/src/btree/tdbBtreeInsert.c deleted file mode 100644 index 6dea4a4e57..0000000000 --- a/source/libs/tdb/src/btree/tdbBtreeInsert.c +++ /dev/null @@ -1,14 +0,0 @@ -/* - * 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 . - */ \ No newline at end of file diff --git a/source/libs/tdb/src/btree/tdbBtreeInt.h b/source/libs/tdb/src/btree/tdbBtreeInt.h deleted file mode 100644 index b8a935a614..0000000000 --- a/source/libs/tdb/src/btree/tdbBtreeInt.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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 . - */ - -#ifndef _TDB_BTREE_INT_H_ -#define _TDB_BTREE_INT_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef __cplusplus -} -#endif - -#endif /*_TDB_BTREE_INT_H_*/ \ No newline at end of file diff --git a/source/libs/tdb/src/btree/tdbBtreeOpen.c b/source/libs/tdb/src/btree/tdbBtreeOpen.c deleted file mode 100644 index 6dea4a4e57..0000000000 --- a/source/libs/tdb/src/btree/tdbBtreeOpen.c +++ /dev/null @@ -1,14 +0,0 @@ -/* - * 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 . - */ \ No newline at end of file diff --git a/source/libs/tdb/src/db/tdbPCache.c b/source/libs/tdb/src/db/tdbPCache.c index d886cfd889..024143c8b2 100644 --- a/source/libs/tdb/src/db/tdbPCache.c +++ b/source/libs/tdb/src/db/tdbPCache.c @@ -18,6 +18,7 @@ struct SPCache { int pageSize; int cacheSize; tdb_mutex_t mutex; + SPage *pList; int nFree; SPage *pFree; int nPage; @@ -35,16 +36,17 @@ struct SPCache { #define PAGE_IS_PINNED(pPage) ((pPage)->pLruNext == NULL) static int tdbPCacheOpenImpl(SPCache *pCache); -static void tdbPCacheInitLock(SPCache *pCache); -static void tdbPCacheClearLock(SPCache *pCache); -static void tdbPCacheLock(SPCache *pCache); -static void tdbPCacheUnlock(SPCache *pCache); -static bool tdbPCacheLocked(SPCache *pCache); static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage); static void tdbPCachePinPage(SPCache *pCache, SPage *pPage); static void tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage); static void tdbPCacheAddPageToHash(SPCache *pCache, SPage *pPage); static void tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage); +static int tdbPCacheCloseImpl(SPCache *pCache); + +static void tdbPCacheInitLock(SPCache *pCache) { tdbMutexInit(&(pCache->mutex), NULL); } +static void tdbPCacheDestroyLock(SPCache *pCache) { tdbMutexDestroy(&(pCache->mutex)); } +static void tdbPCacheLock(SPCache *pCache) { tdbMutexLock(&(pCache->mutex)); } +static void tdbPCacheUnlock(SPCache *pCache) { tdbMutexUnlock(&(pCache->mutex)); } int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) { SPCache *pCache; @@ -69,7 +71,10 @@ int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache) { } int tdbPCacheClose(SPCache *pCache) { - /* TODO */ + if (pCache) { + tdbPCacheCloseImpl(pCache); + tdbOsFree(pCache); + } return 0; } @@ -99,19 +104,7 @@ void tdbPCacheRelease(SPCache *pCache, SPage *pPage) { } } -static void tdbPCacheInitLock(SPCache *pCache) { tdbMutexInit(&(pCache->mutex), NULL); } - -static void tdbPCacheClearLock(SPCache *pCache) { tdbMutexDestroy(&(pCache->mutex)); } - -static void tdbPCacheLock(SPCache *pCache) { tdbMutexLock(&(pCache->mutex)); } - -static void tdbPCacheUnlock(SPCache *pCache) { tdbMutexUnlock(&(pCache->mutex)); } - -static bool tdbPCacheLocked(SPCache *pCache) { - assert(0); - // TODO - return true; -} +int tdbPCacheGetPageSize(SPCache *pCache) { return pCache->pageSize; } static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) { SPage *pPage; @@ -268,4 +261,18 @@ static int tdbPCacheOpenImpl(SPCache *pCache) { return 0; } -int tdbPCacheGetPageSize(SPCache *pCache) { return pCache->pageSize; } +static int tdbPCacheDestroyPage(SPage *pPage) { + // TODO + return 0; +} + +static int tdbPCacheCloseImpl(SPCache *pCache) { + SPage *pPage; + + for (pPage = pCache->pList; pPage; pPage = pCache->pList) { + pCache->pList = pPage->pCacheNext; + tdbPCacheDestroyPage(pPage); + } + + tdbPCacheDestroyLock(pCache); +} diff --git a/source/libs/tdb/src/inc/tdbPCache.h b/source/libs/tdb/src/inc/tdbPCache.h index f71d34ab53..eab5d7582a 100644 --- a/source/libs/tdb/src/inc/tdbPCache.h +++ b/source/libs/tdb/src/inc/tdbPCache.h @@ -25,6 +25,7 @@ extern "C" { u8 isLocalPage; \ u8 isDirty; \ i32 nRef; \ + SPage *pCacheNext; \ SPage *pFreeNext; \ SPage *pHashNext; \ SPage *pLruNext; \ diff --git a/source/libs/tdb/src/inc/tdbTxn.h b/source/libs/tdb/src/inc/tdbTxn.h index 0be2dad3c2..d979249f03 100644 --- a/source/libs/tdb/src/inc/tdbTxn.h +++ b/source/libs/tdb/src/inc/tdbTxn.h @@ -25,6 +25,7 @@ typedef struct STxn TXN; struct STxn { u64 txnId; void *(*xMalloc)(void *, int); + void (*xFree)(void *, void *); void *xArg; }; From 9dea86249cb1fce10aa80ec1cc9fa44707d4b205 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 2 Apr 2022 05:16:32 +0000 Subject: [PATCH 03/25] refact --- source/libs/tdb/src/db/tdbPCache.c | 23 ++++++++--------------- source/libs/tdb/src/db/tdbPager.c | 4 ++-- source/libs/tdb/src/inc/tdbPCache.h | 2 +- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/source/libs/tdb/src/db/tdbPCache.c b/source/libs/tdb/src/db/tdbPCache.c index 024143c8b2..5d16aeaa29 100644 --- a/source/libs/tdb/src/db/tdbPCache.c +++ b/source/libs/tdb/src/db/tdbPCache.c @@ -36,7 +36,7 @@ struct SPCache { #define PAGE_IS_PINNED(pPage) ((pPage)->pLruNext == NULL) static int tdbPCacheOpenImpl(SPCache *pCache); -static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage); +static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid); static void tdbPCachePinPage(SPCache *pCache, SPage *pPage); static void tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage); static void tdbPCacheAddPageToHash(SPCache *pCache, SPage *pPage); @@ -78,12 +78,12 @@ int tdbPCacheClose(SPCache *pCache) { return 0; } -SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) { +SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid) { SPage *pPage; tdbPCacheLock(pCache); - pPage = tdbPCacheFetchImpl(pCache, pPgid, alcNewPage); + pPage = tdbPCacheFetchImpl(pCache, pPgid); if (pPage) { TDB_REF_PAGE(pPage); } @@ -106,7 +106,7 @@ void tdbPCacheRelease(SPCache *pCache, SPage *pPage) { int tdbPCacheGetPageSize(SPCache *pCache) { return pCache->pageSize; } -static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNewPage) { +static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid) { SPage *pPage; // 1. Search the hash table @@ -116,12 +116,10 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, bool alcNe pPage = pPage->pHashNext; } - if (pPage || !alcNewPage) { - if (pPage) { - tdbPCachePinPage(pCache, pPage); - } - return pPage; + if (pPage) { + tdbPCachePinPage(pCache, pPage); } + return pPage; // 2. Try to allocate a new page from the free list if (pCache->pFree) { @@ -261,17 +259,12 @@ static int tdbPCacheOpenImpl(SPCache *pCache) { return 0; } -static int tdbPCacheDestroyPage(SPage *pPage) { - // TODO - return 0; -} - static int tdbPCacheCloseImpl(SPCache *pCache) { SPage *pPage; for (pPage = pCache->pList; pPage; pPage = pCache->pList) { pCache->pList = pPage->pCacheNext; - tdbPCacheDestroyPage(pPage); + tdbPageDestroy(pPage, NULL, NULL); } tdbPCacheDestroyLock(pCache); diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index 2bc40a6aad..1953419bf4 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -227,7 +227,7 @@ int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage // Fetch a page container from the page cache memcpy(&pgid, pPager->fid, TDB_FILE_ID_LEN); pgid.pgno = pgno; - pPage = tdbPCacheFetch(pPager->pCache, &pgid, 1); + pPage = tdbPCacheFetch(pPager->pCache, &pgid); if (pPage == NULL) { return -1; } @@ -263,7 +263,7 @@ int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage // Fetch a page container from the page cache memcpy(&pgid, pPager->fid, TDB_FILE_ID_LEN); pgid.pgno = *ppgno; - pPage = tdbPCacheFetch(pPager->pCache, &pgid, 1); + pPage = tdbPCacheFetch(pPager->pCache, &pgid); if (pPage == NULL) { return -1; } diff --git a/source/libs/tdb/src/inc/tdbPCache.h b/source/libs/tdb/src/inc/tdbPCache.h index eab5d7582a..c5eba88545 100644 --- a/source/libs/tdb/src/inc/tdbPCache.h +++ b/source/libs/tdb/src/inc/tdbPCache.h @@ -48,7 +48,7 @@ extern "C" { int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache); int tdbPCacheClose(SPCache *pCache); -SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, bool alcNewPage); +SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid); void tdbPCacheRelease(SPCache *pCache, SPage *pPage); int tdbPCacheGetPageSize(SPCache *pCache); From f9e699bea5a3fdf42d57a000e9d0a0afea845496 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 2 Apr 2022 06:23:04 +0000 Subject: [PATCH 04/25] more TDB --- source/libs/tdb/src/db/tdbEnv.c | 10 +++--- source/libs/tdb/src/db/tdbPCache.c | 53 +++++++++++++++++++++-------- source/libs/tdb/src/db/tdbPager.c | 8 ++--- source/libs/tdb/src/inc/tdbEnv.h | 6 ++-- source/libs/tdb/src/inc/tdbInt.h | 7 ++++ source/libs/tdb/src/inc/tdbPCache.h | 4 +-- source/libs/tdb/src/inc/tdbPager.h | 4 +-- source/libs/tdb/src/inc/tdbTxn.h | 9 ----- source/libs/tdb/test/tdbTest.cpp | 6 ++-- 9 files changed, 63 insertions(+), 44 deletions(-) diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index 06d37df653..779435861e 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -73,12 +73,12 @@ int tdbEnvClose(TENV *pEnv) { return 0; } -int tdbBegin(TENV *pEnv) { +int tdbBegin(TENV *pEnv, TXN *pTxn) { SPager *pPager; int ret; for (pPager = pEnv->pgrList; pPager; pPager = pPager->pNext) { - ret = tdbPagerBegin(pPager); + ret = tdbPagerBegin(pPager, pTxn); if (ret < 0) { ASSERT(0); return -1; @@ -88,12 +88,12 @@ int tdbBegin(TENV *pEnv) { return 0; } -int tdbCommit(TENV *pEnv) { +int tdbCommit(TENV *pEnv, TXN *pTxn) { SPager *pPager; int ret; for (pPager = pEnv->pgrList; pPager; pPager = pPager->pNext) { - ret = tdbPagerCommit(pPager); + ret = tdbPagerCommit(pPager, pTxn); if (ret < 0) { ASSERT(0); return -1; @@ -103,7 +103,7 @@ int tdbCommit(TENV *pEnv) { return 0; } -int tdbRollback(TENV *pEnv) { +int tdbRollback(TENV *pEnv, TXN *pTxn) { ASSERT(0); return 0; } diff --git a/source/libs/tdb/src/db/tdbPCache.c b/source/libs/tdb/src/db/tdbPCache.c index 5d16aeaa29..b2d370a668 100644 --- a/source/libs/tdb/src/db/tdbPCache.c +++ b/source/libs/tdb/src/db/tdbPCache.c @@ -36,7 +36,7 @@ struct SPCache { #define PAGE_IS_PINNED(pPage) ((pPage)->pLruNext == NULL) static int tdbPCacheOpenImpl(SPCache *pCache); -static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid); +static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, TXN *pTxn); static void tdbPCachePinPage(SPCache *pCache, SPage *pPage); static void tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage); static void tdbPCacheAddPageToHash(SPCache *pCache, SPage *pPage); @@ -78,12 +78,12 @@ int tdbPCacheClose(SPCache *pCache) { return 0; } -SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid) { +SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, TXN *pTxn) { SPage *pPage; tdbPCacheLock(pCache); - pPage = tdbPCacheFetchImpl(pCache, pPgid); + pPage = tdbPCacheFetchImpl(pCache, pPgid, pTxn); if (pPage) { TDB_REF_PAGE(pPage); } @@ -106,7 +106,8 @@ void tdbPCacheRelease(SPCache *pCache, SPage *pPage) { int tdbPCacheGetPageSize(SPCache *pCache) { return pCache->pageSize; } -static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid) { +static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, TXN *pTxn) { + int ret; SPage *pPage; // 1. Search the hash table @@ -117,9 +118,11 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid) { } if (pPage) { + // TODO: the page need to be copied and + // replaced the page in hash table tdbPCachePinPage(pCache, pPage); + return pPage; } - return pPage; // 2. Try to allocate a new page from the free list if (pCache->pFree) { @@ -136,7 +139,20 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid) { tdbPCachePinPage(pCache, pPage); } - // 4. Try a stress allocation (TODO) + // 4. Try a create new page + if (pTxn && pTxn->xMalloc) { + ret = tdbPageCreate(pCache->pageSize, &pPage, pTxn->xMalloc, pTxn->xArg); + if (ret < 0) { + // TODO + ASSERT(0); + return NULL; + } + + // init the page fields + pPage->isAnchor = 0; + pPage->isLocal = 0; + TDB_INIT_PAGE_REF(pPage); + } // 5. Page here are just created from a free list // or by recycling or allocated streesly, @@ -145,6 +161,8 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid) { memcpy(&(pPage->pgid), pPgid, sizeof(*pPgid)); pPage->pLruNext = NULL; pPage->pPager = NULL; + + // TODO: allocated page may not add to hash tdbPCacheAddPageToHash(pCache, pPage); } @@ -171,17 +189,21 @@ static void tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage) { nRef = TDB_GET_PAGE_REF(pPage); ASSERT(nRef >= 0); if (nRef == 0) { - // Add the page to LRU list - ASSERT(pPage->pLruNext == NULL); + if (1) { + // Add the page to LRU list + ASSERT(pPage->pLruNext == NULL); - pPage->pLruPrev = &(pCache->lru); - pPage->pLruNext = pCache->lru.pLruNext; - pCache->lru.pLruNext->pLruPrev = pPage; - pCache->lru.pLruNext = pPage; + pPage->pLruPrev = &(pCache->lru); + pPage->pLruNext = pCache->lru.pLruNext; + pCache->lru.pLruNext->pLruPrev = pPage; + pCache->lru.pLruNext = pPage; + + pCache->nRecyclable++; + } else { + // TODO: may need to free the page + } } - pCache->nRecyclable++; - tdbPCacheUnlock(pCache); } @@ -229,13 +251,14 @@ static int tdbPCacheOpenImpl(SPCache *pCache) { // pPage->pgid = 0; pPage->isAnchor = 0; - pPage->isLocalPage = 1; + pPage->isLocal = 1; TDB_INIT_PAGE_REF(pPage); pPage->pHashNext = NULL; pPage->pLruNext = NULL; pPage->pLruPrev = NULL; pPage->pDirtyNext = NULL; + // add page to free list pPage->pFreeNext = pCache->pFree; pCache->pFree = pPage; pCache->nFree++; diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index 1953419bf4..13db278c6e 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -157,7 +157,7 @@ int tdbPagerWrite(SPager *pPager, SPage *pPage) { return 0; } -int tdbPagerBegin(SPager *pPager) { +int tdbPagerBegin(SPager *pPager, TXN *pTxn) { if (pPager->inTran) { return 0; } @@ -175,7 +175,7 @@ int tdbPagerBegin(SPager *pPager) { return 0; } -int tdbPagerCommit(SPager *pPager) { +int tdbPagerCommit(SPager *pPager, TXN *pTxn) { SPage *pPage; int ret; @@ -227,7 +227,7 @@ int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage // Fetch a page container from the page cache memcpy(&pgid, pPager->fid, TDB_FILE_ID_LEN); pgid.pgno = pgno; - pPage = tdbPCacheFetch(pPager->pCache, &pgid); + pPage = tdbPCacheFetch(pPager->pCache, &pgid, NULL); if (pPage == NULL) { return -1; } @@ -263,7 +263,7 @@ int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage // Fetch a page container from the page cache memcpy(&pgid, pPager->fid, TDB_FILE_ID_LEN); pgid.pgno = *ppgno; - pPage = tdbPCacheFetch(pPager->pCache, &pgid); + pPage = tdbPCacheFetch(pPager->pCache, &pgid, NULL); if (pPage == NULL) { return -1; } diff --git a/source/libs/tdb/src/inc/tdbEnv.h b/source/libs/tdb/src/inc/tdbEnv.h index e10c5d54e0..54f5818be1 100644 --- a/source/libs/tdb/src/inc/tdbEnv.h +++ b/source/libs/tdb/src/inc/tdbEnv.h @@ -33,9 +33,9 @@ typedef struct STEnv { int tdbEnvOpen(const char *rootDir, int pageSize, int cacheSize, TENV **ppEnv); int tdbEnvClose(TENV *pEnv); -int tdbBegin(TENV *pEnv); -int tdbCommit(TENV *pEnv); -int tdbRollback(TENV *pEnv); +int tdbBegin(TENV *pEnv, TXN *pTxn); +int tdbCommit(TENV *pEnv, TXN *pTxn); +int tdbRollback(TENV *pEnv, TXN *pTxn); void tdbEnvAddPager(TENV *pEnv, SPager *pPager); void tdbEnvRemovePager(TENV *pEnv, SPager *pPager); diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 57e01f904c..2a196b2cc2 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -111,6 +111,13 @@ typedef struct SPager SPager; typedef struct SPCache SPCache; typedef struct SPage SPage; +typedef struct STxn { + u64 txnId; + void *(*xMalloc)(void *, size_t); + void (*xFree)(void *, void *); + void *xArg; +} TXN; + #include "tdbOs.h" #include "tdbUtil.h" diff --git a/source/libs/tdb/src/inc/tdbPCache.h b/source/libs/tdb/src/inc/tdbPCache.h index c5eba88545..5b24a53591 100644 --- a/source/libs/tdb/src/inc/tdbPCache.h +++ b/source/libs/tdb/src/inc/tdbPCache.h @@ -22,7 +22,7 @@ extern "C" { #define TDB_PCACHE_PAGE \ u8 isAnchor; \ - u8 isLocalPage; \ + u8 isLocal; \ u8 isDirty; \ i32 nRef; \ SPage *pCacheNext; \ @@ -48,7 +48,7 @@ extern "C" { int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache); int tdbPCacheClose(SPCache *pCache); -SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid); +SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, TXN *pTxn); void tdbPCacheRelease(SPCache *pCache, SPage *pPage); int tdbPCacheGetPageSize(SPCache *pCache); diff --git a/source/libs/tdb/src/inc/tdbPager.h b/source/libs/tdb/src/inc/tdbPager.h index 81b6074431..cc8860ae7a 100644 --- a/source/libs/tdb/src/inc/tdbPager.h +++ b/source/libs/tdb/src/inc/tdbPager.h @@ -40,8 +40,8 @@ int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager); int tdbPagerClose(SPager *pPager); int tdbPagerOpenDB(SPager *pPager, SPgno *ppgno, bool toCreate); int tdbPagerWrite(SPager *pPager, SPage *pPage); -int tdbPagerBegin(SPager *pPager); -int tdbPagerCommit(SPager *pPager); +int tdbPagerBegin(SPager *pPager, TXN *pTxn); +int tdbPagerCommit(SPager *pPager, TXN *pTxn); int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg); int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg); void tdbPagerReturnPage(SPager *pPager, SPage *pPage); diff --git a/source/libs/tdb/src/inc/tdbTxn.h b/source/libs/tdb/src/inc/tdbTxn.h index d979249f03..13303bffb6 100644 --- a/source/libs/tdb/src/inc/tdbTxn.h +++ b/source/libs/tdb/src/inc/tdbTxn.h @@ -20,15 +20,6 @@ extern "C" { #endif -typedef struct STxn TXN; - -struct STxn { - u64 txnId; - void *(*xMalloc)(void *, int); - void (*xFree)(void *, void *); - void *xArg; -}; - #ifdef __cplusplus } #endif diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index 9e1277a53d..7e5cfeff42 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -135,7 +135,7 @@ TEST(tdb_test, simple_test) { { // Insert some data for (int i = 1; i <= nData;) { - tdbBegin(pEnv); + tdbBegin(pEnv, NULL); for (int k = 0; k < 2000; k++) { sprintf(key, "key%d", i); @@ -145,12 +145,10 @@ TEST(tdb_test, simple_test) { i++; } - tdbCommit(pEnv); + tdbCommit(pEnv, NULL); } } - tdbCommit(pEnv); - { // Query the data void *pVal = NULL; int vLen; From 6b47fd8984f22266b852156d5c4c6c7ea96e707d Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 2 Apr 2022 11:20:56 +0000 Subject: [PATCH 05/25] TDB with txn --- source/libs/tdb/src/db/tdbBtree.c | 45 ++++++++++++----------- source/libs/tdb/src/db/tdbDb.c | 6 +-- source/libs/tdb/src/db/tdbPCache.c | 51 +++++++++++++++----------- source/libs/tdb/src/db/tdbPager.c | 20 ++++++---- source/libs/tdb/src/db/tdbTxn.c | 36 ++++++------------ source/libs/tdb/src/inc/tdbBtree.h | 5 ++- source/libs/tdb/src/inc/tdbDb.h | 2 +- source/libs/tdb/src/inc/tdbInt.h | 10 ++++- source/libs/tdb/src/inc/tdbPCache.h | 2 +- source/libs/tdb/src/inc/tdbPager.h | 9 +++-- source/libs/tdb/src/inc/tdbTxn.h | 4 ++ source/libs/tdb/test/tdbTest.cpp | 57 ++++++++++++++++++++--------- 12 files changed, 142 insertions(+), 105 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index f4e1621742..285434b402 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -127,7 +127,7 @@ int tdbBtreeClose(SBTree *pBt) { return 0; } -int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, int vLen) { +int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, int vLen, TXN *pTxn) { SBTC btc; SCell *pCell; void *pBuf; @@ -137,7 +137,7 @@ int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, in int idx; int c; - tdbBtcOpen(&btc, pBt); + tdbBtcOpen(&btc, pBt, pTxn); // move to the position to insert ret = tdbBtcMoveTo(&btc, pKey, kLen, &c); @@ -225,7 +225,7 @@ int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkL void *pTVal = NULL; SCellDecoder cd; - tdbBtcOpen(&btc, pBt); + tdbBtcOpen(&btc, pBt, NULL); ret = tdbBtcMoveTo(&btc, pKey, kLen, &cret); if (ret < 0) { @@ -307,13 +307,13 @@ static int tdbBtreeOpenImpl(SBTree *pBt) { // Try to create a new database SBtreeInitPageArg zArg = {.flags = TDB_BTREE_ROOT | TDB_BTREE_LEAF, .pBt = pBt}; - ret = tdbPagerNewPage(pBt->pPager, &pgno, &pPage, tdbBtreeZeroPage, &zArg); + ret = tdbPagerNewPage(pBt->pPager, &pgno, &pPage, tdbBtreeZeroPage, &zArg, NULL); if (ret < 0) { return -1; } // TODO: here still has problem - tdbPagerReturnPage(pBt->pPager, pPage); + tdbPagerReturnPage(pBt->pPager, pPage, NULL); ASSERT(pgno != 0); pBt->root = pgno; @@ -385,7 +385,7 @@ static int tdbBtreeZeroPage(SPage *pPage, void *arg) { } // TDB_BTREE_BALANCE ===================== -static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild) { +static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild, TXN *pTxn) { SPager *pPager; SPage *pChild; SPgno pgnoChild; @@ -402,7 +402,7 @@ static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild) { // Allocate a new child page zArg.flags = TDB_FLAG_REMOVE(flags, TDB_BTREE_ROOT); zArg.pBt = pBt; - ret = tdbPagerNewPage(pPager, &pgnoChild, &pChild, tdbBtreeZeroPage, &zArg); + ret = tdbPagerNewPage(pPager, &pgnoChild, &pChild, tdbBtreeZeroPage, &zArg, pTxn); if (ret < 0) { return -1; } @@ -436,7 +436,7 @@ static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild) { return 0; } -static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { +static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx, TXN *pTxn) { int ret; int nOlds; @@ -477,7 +477,7 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { pgno = *(SPgno *)pCell; } - ret = tdbPagerFetchPage(pBt->pPager, pgno, pOlds + i, tdbBtreeInitPage, pBt); + ret = tdbPagerFetchPage(pBt->pPager, pgno, pOlds + i, tdbBtreeInitPage, pBt, pTxn); if (ret < 0) { ASSERT(0); return -1; @@ -640,7 +640,7 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { } else { iarg.pBt = pBt; iarg.flags = flags; - ret = tdbPagerNewPage(pBt->pPager, &pgno, pNews + iNew, tdbBtreeZeroPage, &iarg); + ret = tdbPagerNewPage(pBt->pPager, &pgno, pNews + iNew, tdbBtreeZeroPage, &iarg, pTxn); if (ret < 0) { ASSERT(0); } @@ -767,9 +767,9 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx) { // TODO: here is not corrent for drop case for (int i = 0; i < nNews; i++) { if (i < nOlds) { - tdbPagerReturnPage(pBt->pPager, pOlds[i]); + tdbPagerReturnPage(pBt->pPager, pOlds[i], pTxn); } else { - tdbPagerReturnPage(pBt->pPager, pNews[i]); + tdbPagerReturnPage(pBt->pPager, pNews[i], pTxn); } } @@ -805,7 +805,7 @@ static int tdbBtreeBalance(SBTC *pBtc) { // ignore the case of empty if (pPage->nOverflow == 0) break; - ret = tdbBtreeBalanceDeeper(pBtc->pBt, pPage, &(pBtc->pgStack[1])); + ret = tdbBtreeBalanceDeeper(pBtc->pBt, pPage, &(pBtc->pgStack[1]), pBtc->pTxn); if (ret < 0) { return -1; } @@ -819,12 +819,12 @@ static int tdbBtreeBalance(SBTC *pBtc) { // Generalized balance step pParent = pBtc->pgStack[iPage - 1]; - ret = tdbBtreeBalanceNonRoot(pBtc->pBt, pParent, pBtc->idxStack[pBtc->iPage - 1]); + ret = tdbBtreeBalanceNonRoot(pBtc->pBt, pParent, pBtc->idxStack[pBtc->iPage - 1], pBtc->pTxn); if (ret < 0) { return -1; } - tdbPagerReturnPage(pBtc->pBt->pPager, pBtc->pPage); + tdbPagerReturnPage(pBtc->pBt->pPager, pBtc->pPage, pBtc->pTxn); pBtc->iPage--; pBtc->pPage = pBtc->pgStack[pBtc->iPage]; @@ -1024,11 +1024,12 @@ static int tdbBtreeCellSize(const SPage *pPage, SCell *pCell) { // TDB_BTREE_CELL // TDB_BTREE_CURSOR ===================== -int tdbBtcOpen(SBTC *pBtc, SBTree *pBt) { +int tdbBtcOpen(SBTC *pBtc, SBTree *pBt, TXN *pTxn) { pBtc->pBt = pBt; pBtc->iPage = -1; pBtc->pPage = NULL; pBtc->idx = -1; + pBtc->pTxn = pTxn; return 0; } @@ -1045,7 +1046,7 @@ int tdbBtcMoveToFirst(SBTC *pBtc) { if (pBtc->iPage < 0) { // move a clean cursor - ret = tdbPagerFetchPage(pPager, pBt->root, &(pBtc->pPage), tdbBtreeInitPage, pBt); + ret = tdbPagerFetchPage(pPager, pBt->root, &(pBtc->pPage), tdbBtreeInitPage, pBt, pBtc->pTxn); if (ret < 0) { ASSERT(0); return -1; @@ -1110,7 +1111,7 @@ int tdbBtcMoveToLast(SBTC *pBtc) { if (pBtc->iPage < 0) { // move a clean cursor - ret = tdbPagerFetchPage(pPager, pBt->root, &(pBtc->pPage), tdbBtreeInitPage, pBt); + ret = tdbPagerFetchPage(pPager, pBt->root, &(pBtc->pPage), tdbBtreeInitPage, pBt, pBtc->pTxn); if (ret < 0) { ASSERT(0); return -1; @@ -1284,7 +1285,7 @@ static int tdbBtcMoveDownward(SBTC *pBtc) { pBtc->pPage = NULL; pBtc->idx = -1; - ret = tdbPagerFetchPage(pBtc->pBt->pPager, pgno, &pBtc->pPage, tdbBtreeInitPage, pBtc->pBt); + ret = tdbPagerFetchPage(pBtc->pBt->pPager, pgno, &pBtc->pPage, tdbBtreeInitPage, pBtc->pBt, pBtc->pTxn); if (ret < 0) { ASSERT(0); return -1; @@ -1296,7 +1297,7 @@ static int tdbBtcMoveDownward(SBTC *pBtc) { static int tdbBtcMoveUpward(SBTC *pBtc) { if (pBtc->iPage == 0) return -1; - tdbPagerReturnPage(pBtc->pBt->pPager, pBtc->pPage); + tdbPagerReturnPage(pBtc->pBt->pPager, pBtc->pPage, pBtc->pTxn); pBtc->iPage--; pBtc->pPage = pBtc->pgStack[pBtc->iPage]; @@ -1319,7 +1320,7 @@ static int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) { if (pBtc->iPage < 0) { // move from a clear cursor - ret = tdbPagerFetchPage(pPager, pBt->root, &(pBtc->pPage), tdbBtreeInitPage, pBt); + ret = tdbPagerFetchPage(pPager, pBt->root, &(pBtc->pPage), tdbBtreeInitPage, pBt, pBtc->pTxn); if (ret < 0) { // TODO ASSERT(0); @@ -1456,7 +1457,7 @@ int tdbBtcClose(SBTC *pBtc) { for (;;) { ASSERT(pBtc->pPage); - tdbPagerReturnPage(pBtc->pBt->pPager, pBtc->pPage); + tdbPagerReturnPage(pBtc->pBt->pPager, pBtc->pPage, pBtc->pTxn); pBtc->iPage--; if (pBtc->iPage < 0) break; diff --git a/source/libs/tdb/src/db/tdbDb.c b/source/libs/tdb/src/db/tdbDb.c index fe7b8c6d48..f4468e7e82 100644 --- a/source/libs/tdb/src/db/tdbDb.c +++ b/source/libs/tdb/src/db/tdbDb.c @@ -75,8 +75,8 @@ int tdbDbDrop(TDB *pDb) { return 0; } -int tdbDbInsert(TDB *pDb, const void *pKey, int keyLen, const void *pVal, int valLen) { - return tdbBtreeInsert(pDb->pBt, pKey, keyLen, pVal, valLen); +int tdbDbInsert(TDB *pDb, const void *pKey, int keyLen, const void *pVal, int valLen, TXN *pTxn) { + return tdbBtreeInsert(pDb->pBt, pKey, keyLen, pVal, valLen, pTxn); } int tdbDbGet(TDB *pDb, const void *pKey, int kLen, void **ppVal, int *vLen) { @@ -97,7 +97,7 @@ int tdbDbcOpen(TDB *pDb, TDBC **ppDbc) { return -1; } - tdbBtcOpen(&pDbc->btc, pDb->pBt); + tdbBtcOpen(&pDbc->btc, pDb->pBt, NULL); // TODO: move to first now, we can move to any key-value // and in any direction, design new APIs. diff --git a/source/libs/tdb/src/db/tdbPCache.c b/source/libs/tdb/src/db/tdbPCache.c index b2d370a668..b4e291f5e4 100644 --- a/source/libs/tdb/src/db/tdbPCache.c +++ b/source/libs/tdb/src/db/tdbPCache.c @@ -93,14 +93,33 @@ SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, TXN *pTxn) { return pPage; } -void tdbPCacheRelease(SPCache *pCache, SPage *pPage) { +void tdbPCacheRelease(SPCache *pCache, SPage *pPage, TXN *pTxn) { i32 nRef; nRef = TDB_UNREF_PAGE(pPage); ASSERT(nRef >= 0); if (nRef == 0) { - tdbPCacheUnpinPage(pCache, pPage); + tdbPCacheLock(pCache); + + // test the nRef again to make sure + // it is safe th handle the page + nRef = TDB_GET_PAGE_REF(pPage); + if (nRef == 0) { + if (pPage->isLocal) { + tdbPCacheUnpinPage(pCache, pPage); + } else { + // remove from hash + tdbPCacheRemovePageFromHash(pCache, pPage); + + // free the page + if (pTxn && pTxn->xFree) { + tdbPageDestroy(pPage, pTxn->xFree, pTxn->xArg); + } + } + } + + tdbPCacheUnlock(pCache); } } @@ -140,7 +159,7 @@ static SPage *tdbPCacheFetchImpl(SPCache *pCache, const SPgid *pPgid, TXN *pTxn) } // 4. Try a create new page - if (pTxn && pTxn->xMalloc) { + if (!pPage && pTxn && pTxn->xMalloc) { ret = tdbPageCreate(pCache->pageSize, &pPage, pTxn->xMalloc, pTxn->xArg); if (ret < 0) { // TODO @@ -182,29 +201,17 @@ static void tdbPCachePinPage(SPCache *pCache, SPage *pPage) { static void tdbPCacheUnpinPage(SPCache *pCache, SPage *pPage) { i32 nRef; - tdbPCacheLock(pCache); - ASSERT(!pPage->isDirty); + ASSERT(TDB_GET_PAGE_REF(pPage) == 0); - nRef = TDB_GET_PAGE_REF(pPage); - ASSERT(nRef >= 0); - if (nRef == 0) { - if (1) { - // Add the page to LRU list - ASSERT(pPage->pLruNext == NULL); + ASSERT(pPage->pLruNext == NULL); - pPage->pLruPrev = &(pCache->lru); - pPage->pLruNext = pCache->lru.pLruNext; - pCache->lru.pLruNext->pLruPrev = pPage; - pCache->lru.pLruNext = pPage; + pPage->pLruPrev = &(pCache->lru); + pPage->pLruNext = pCache->lru.pLruNext; + pCache->lru.pLruNext->pLruPrev = pPage; + pCache->lru.pLruNext = pPage; - pCache->nRecyclable++; - } else { - // TODO: may need to free the page - } - } - - tdbPCacheUnlock(pCache); + pCache->nRecyclable++; } static void tdbPCacheRemovePageFromHash(SPCache *pCache, SPage *pPage) { diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index 13db278c6e..f78568b51c 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -27,7 +27,6 @@ TDB_STATIC_ASSERT(sizeof(SFileHdr) == 128, "Size of file header is not correct") #define TDB_PAGE_INITIALIZED(pPage) ((pPage)->pPager != NULL) -static int tdbPagerAllocPage(SPager *pPager, SPgno *ppgno); static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage *, void *), void *arg, u8 loadPage); static int tdbPagerWritePageToJournal(SPager *pPager, SPage *pPage); static int tdbPagerWritePageToDB(SPager *pPager, SPage *pPage); @@ -204,7 +203,7 @@ int tdbPagerCommit(SPager *pPager, TXN *pTxn) { pPage->isDirty = 0; - tdbPCacheRelease(pPager->pCache, pPage); + tdbPCacheRelease(pPager->pCache, pPage, pTxn); } // sync the db file @@ -219,7 +218,8 @@ int tdbPagerCommit(SPager *pPager, TXN *pTxn) { return 0; } -int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg) { +int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg, + TXN *pTxn) { SPage *pPage; SPgid pgid; int ret; @@ -227,7 +227,7 @@ int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage // Fetch a page container from the page cache memcpy(&pgid, pPager->fid, TDB_FILE_ID_LEN); pgid.pgno = pgno; - pPage = tdbPCacheFetch(pPager->pCache, &pgid, NULL); + pPage = tdbPCacheFetch(pPager->pCache, &pgid, pTxn); if (pPage == NULL) { return -1; } @@ -247,7 +247,8 @@ int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage return 0; } -int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg) { +int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg, + TXN *pTxn) { int ret; SPage *pPage; SPgid pgid; @@ -255,6 +256,7 @@ int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage // Allocate a page number ret = tdbPagerAllocPage(pPager, ppgno); if (ret < 0) { + ASSERT(0); return -1; } @@ -263,8 +265,9 @@ int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage // Fetch a page container from the page cache memcpy(&pgid, pPager->fid, TDB_FILE_ID_LEN); pgid.pgno = *ppgno; - pPage = tdbPCacheFetch(pPager->pCache, &pgid, NULL); + pPage = tdbPCacheFetch(pPager->pCache, &pgid, pTxn); if (pPage == NULL) { + ASSERT(0); return -1; } @@ -273,6 +276,7 @@ int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage // Initialize the page if need ret = tdbPagerInitPage(pPager, pPage, initPage, arg, 0); if (ret < 0) { + ASSERT(0); return -1; } @@ -283,7 +287,7 @@ int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage return 0; } -void tdbPagerReturnPage(SPager *pPager, SPage *pPage) { tdbPCacheRelease(pPager->pCache, pPage); } +void tdbPagerReturnPage(SPager *pPager, SPage *pPage, TXN *pTxn) { tdbPCacheRelease(pPager->pCache, pPage, pTxn); } static int tdbPagerAllocFreePage(SPager *pPager, SPgno *ppgno) { // TODO: Allocate a page from the free list @@ -295,7 +299,7 @@ static int tdbPagerAllocNewPage(SPager *pPager, SPgno *ppgno) { return 0; } -static int tdbPagerAllocPage(SPager *pPager, SPgno *ppgno) { +int tdbPagerAllocPage(SPager *pPager, SPgno *ppgno) { int ret; *ppgno = 0; diff --git a/source/libs/tdb/src/db/tdbTxn.c b/source/libs/tdb/src/db/tdbTxn.c index 03bcbb44a7..b06fe05acd 100644 --- a/source/libs/tdb/src/db/tdbTxn.c +++ b/source/libs/tdb/src/db/tdbTxn.c @@ -15,29 +15,17 @@ #include "tdbInt.h" -// int tdbTxnBegin(TENV *pEnv) { -// // TODO -// return 0; -// } +int tdbTxnOpen(TXN *pTxn, int64_t txnid, void *(*xMalloc)(void *, size_t), void (*xFree)(void *, void *), void *xArg, + int flags) { + // not support read-committed version at the moment + ASSERT(flags == 0 || flags == TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED); -// int tdbTxnCommit(TENV *pEnv) { -// SPager *pPager = NULL; -// int ret; + pTxn->flags = flags; + pTxn->txnId = txnid; + pTxn->xMalloc = xMalloc; + pTxn->xFree = xFree; + pTxn->xArg = xArg; + return 0; +} -// for (;;) { -// break; -// ret = tdbPagerCommit(pPager); -// if (ret < 0) { -// ASSERT(0); -// return -1; -// } -// } - -// // TODO -// return 0; -// } - -// int tdbTxnRollback(TENV *pEnv) { -// // TODO -// return 0; -// } \ No newline at end of file +int tdbTxnClose(TXN *pTxn) { return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbBtree.h b/source/libs/tdb/src/inc/tdbBtree.h index 2eba5f4f1a..3cdd30c7b5 100644 --- a/source/libs/tdb/src/inc/tdbBtree.h +++ b/source/libs/tdb/src/inc/tdbBtree.h @@ -35,17 +35,18 @@ struct SBTC { int idx; int idxStack[BTREE_MAX_DEPTH + 1]; SPage *pgStack[BTREE_MAX_DEPTH + 1]; + TXN *pTxn; }; // SBTree int tdbBtreeOpen(int keyLen, int valLen, SPager *pFile, FKeyComparator kcmpr, SBTree **ppBt); int tdbBtreeClose(SBTree *pBt); -int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, int vLen); +int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, int vLen, TXN *pTxn); int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen); int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkLen, void **ppVal, int *vLen); // SBTC -int tdbBtcOpen(SBTC *pCur, SBTree *pBt); +int tdbBtcOpen(SBTC *pBtc, SBTree *pBt, TXN *pTxn); int tdbBtcMoveToFirst(SBTC *pBtc); int tdbBtcMoveToLast(SBTC *pBtc); int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen); diff --git a/source/libs/tdb/src/inc/tdbDb.h b/source/libs/tdb/src/inc/tdbDb.h index e60371c734..f9ff378633 100644 --- a/source/libs/tdb/src/inc/tdbDb.h +++ b/source/libs/tdb/src/inc/tdbDb.h @@ -27,7 +27,7 @@ typedef struct STDBC TDBC; int tdbDbOpen(const char *fname, int keyLen, int valLen, FKeyComparator keyCmprFn, TENV *pEnv, TDB **ppDb); int tdbDbClose(TDB *pDb); int tdbDbDrop(TDB *pDb); -int tdbDbInsert(TDB *pDb, const void *pKey, int keyLen, const void *pVal, int valLen); +int tdbDbInsert(TDB *pDb, const void *pKey, int keyLen, const void *pVal, int valLen, TXN *pTxn); int tdbDbGet(TDB *pDb, const void *pKey, int kLen, void **ppVal, int *vLen); int tdbDbPGet(TDB *pDb, const void *pKey, int kLen, void **ppKey, int *pkLen, void **ppVal, int *vLen); diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 2a196b2cc2..b85b7174bc 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -111,13 +111,21 @@ typedef struct SPager SPager; typedef struct SPCache SPCache; typedef struct SPage SPage; +// transaction +#define TDB_TXN_WRITE 0x1 +#define TDB_TXN_READ_UNCOMMITTED 0x2 typedef struct STxn { - u64 txnId; + int flags; + i64 txnId; void *(*xMalloc)(void *, size_t); void (*xFree)(void *, void *); void *xArg; } TXN; +#define TDB_TXN_IS_WRITE(PTXN) ((PTXN)->flags & TDB_TXN_WRITE) +#define TDB_TXN_IS_READ(PTXN) (!TDB_TXN_IS_WRITE(PTXN)) +#define TDB_TXN_IS_READ_UNCOMMITTED(PTXN) ((PTXN)->flags & TDB_TXN_READ_UNCOMMITTED) + #include "tdbOs.h" #include "tdbUtil.h" diff --git a/source/libs/tdb/src/inc/tdbPCache.h b/source/libs/tdb/src/inc/tdbPCache.h index 5b24a53591..a77cf6248c 100644 --- a/source/libs/tdb/src/inc/tdbPCache.h +++ b/source/libs/tdb/src/inc/tdbPCache.h @@ -49,7 +49,7 @@ extern "C" { int tdbPCacheOpen(int pageSize, int cacheSize, SPCache **ppCache); int tdbPCacheClose(SPCache *pCache); SPage *tdbPCacheFetch(SPCache *pCache, const SPgid *pPgid, TXN *pTxn); -void tdbPCacheRelease(SPCache *pCache, SPage *pPage); +void tdbPCacheRelease(SPCache *pCache, SPage *pPage, TXN *pTxn); int tdbPCacheGetPageSize(SPCache *pCache); #ifdef __cplusplus diff --git a/source/libs/tdb/src/inc/tdbPager.h b/source/libs/tdb/src/inc/tdbPager.h index cc8860ae7a..ca196785d0 100644 --- a/source/libs/tdb/src/inc/tdbPager.h +++ b/source/libs/tdb/src/inc/tdbPager.h @@ -42,9 +42,12 @@ int tdbPagerOpenDB(SPager *pPager, SPgno *ppgno, bool toCreate); int tdbPagerWrite(SPager *pPager, SPage *pPage); int tdbPagerBegin(SPager *pPager, TXN *pTxn); int tdbPagerCommit(SPager *pPager, TXN *pTxn); -int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg); -int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg); -void tdbPagerReturnPage(SPager *pPager, SPage *pPage); +int tdbPagerFetchPage(SPager *pPager, SPgno pgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg, + TXN *pTxn); +int tdbPagerNewPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initPage)(SPage *, void *), void *arg, + TXN *pTxn); +void tdbPagerReturnPage(SPager *pPager, SPage *pPage, TXN *pTxn); +int tdbPagerAllocPage(SPager *pPager, SPgno *ppgno); #ifdef __cplusplus } diff --git a/source/libs/tdb/src/inc/tdbTxn.h b/source/libs/tdb/src/inc/tdbTxn.h index 13303bffb6..ed9784614c 100644 --- a/source/libs/tdb/src/inc/tdbTxn.h +++ b/source/libs/tdb/src/inc/tdbTxn.h @@ -20,6 +20,10 @@ extern "C" { #endif +int tdbTxnOpen(TXN *pTxn, int64_t txnid, void *(*xMalloc)(void *, size_t), void (*xFree)(void *, void *), void *xArg, + int flags); +int tdbTxnClose(TXN *pTxn); + #ifdef __cplusplus } #endif diff --git a/source/libs/tdb/test/tdbTest.cpp b/source/libs/tdb/test/tdbTest.cpp index 7e5cfeff42..904ad64fe7 100644 --- a/source/libs/tdb/test/tdbTest.cpp +++ b/source/libs/tdb/test/tdbTest.cpp @@ -19,7 +19,7 @@ static SPoolMem *openPool() { return pPool; } -static void closePool(SPoolMem *pPool) { +static void clearPool(SPoolMem *pPool) { SPoolMem *pMem; do { @@ -35,13 +35,14 @@ static void closePool(SPoolMem *pPool) { } while (1); assert(pPool->size == 0); +} +static void closePool(SPoolMem *pPool) { + clearPool(pPool); tdbOsFree(pPool); } -#define clearPool closePool - -static void *poolMalloc(void *arg, int size) { +static void *poolMalloc(void *arg, size_t size) { void *ptr = NULL; SPoolMem *pPool = (SPoolMem *)arg; SPoolMem *pMem; @@ -118,7 +119,8 @@ TEST(tdb_test, simple_test) { TENV *pEnv; TDB *pDb; FKeyComparator compFunc; - int nData = 50000000; + int nData = 10000000; + TXN txn; // Open Env ret = tdbEnvOpen("tdb", 4096, 64, &pEnv); @@ -130,25 +132,44 @@ TEST(tdb_test, simple_test) { GTEST_ASSERT_EQ(ret, 0); { - char key[64]; - char val[64]; + char key[64]; + char val[64]; + int64_t poolLimit = 4096; // 1M pool limit + int64_t txnid = 0; + SPoolMem *pPool; - { // Insert some data - for (int i = 1; i <= nData;) { - tdbBegin(pEnv, NULL); + // open the pool + pPool = openPool(); - for (int k = 0; k < 2000; k++) { - sprintf(key, "key%d", i); - sprintf(val, "value%d", i); - ret = tdbDbInsert(pDb, key, strlen(key), val, strlen(val)); - GTEST_ASSERT_EQ(ret, 0); - i++; - } + // start a transaction + txnid++; + tdbTxnOpen(&txn, txnid, poolMalloc, poolFree, pPool, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED); + tdbBegin(pEnv, &txn); - tdbCommit(pEnv, NULL); + for (int iData = 1; iData <= nData; iData++) { + sprintf(key, "key%d", iData); + sprintf(val, "value%d", iData); + ret = tdbDbInsert(pDb, key, strlen(key), val, strlen(val), &txn); + GTEST_ASSERT_EQ(ret, 0); + + // if pool is full, commit the transaction and start a new one + if (pPool->size >= poolLimit) { + // commit current transaction + tdbCommit(pEnv, &txn); + tdbTxnClose(&txn); + + // start a new transaction + clearPool(pPool); + txnid++; + tdbTxnOpen(&txn, txnid, poolMalloc, poolFree, pPool, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED); + tdbBegin(pEnv, &txn); } } + // commit the transaction + tdbCommit(pEnv, &txn); + tdbTxnClose(&txn); + { // Query the data void *pVal = NULL; int vLen; From 27a3a1d782ae6e7fd9d21d5875b3763643420e57 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Sat, 2 Apr 2022 19:31:52 +0800 Subject: [PATCH 06/25] feature/qnode --- include/common/tmsg.h | 12 +- include/libs/command/command.h | 8 +- include/libs/nodes/nodes.h | 1 + include/util/tlockfree.h | 1 + source/common/src/tmsg.c | 42 +++ source/libs/command/inc/commandInt.h | 76 +++-- source/libs/command/src/explain.c | 357 ++++++++++++++++++++---- source/libs/executor/inc/executorimpl.h | 4 +- source/libs/executor/src/executorimpl.c | 18 +- source/libs/nodes/src/nodesUtilFuncs.c | 22 ++ source/libs/qworker/inc/qworkerMsg.h | 3 +- source/libs/qworker/src/qworker.c | 5 +- source/libs/qworker/src/qworkerMsg.c | 21 ++ source/libs/scheduler/src/scheduler.c | 169 ++++++----- source/util/src/tlockfree.c | 15 + 15 files changed, 597 insertions(+), 157 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 4cfcf0e888..27b9a11625 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -915,11 +915,19 @@ typedef struct { char data[]; } SRetrieveMetaTableRsp; +typedef struct SExplainExecInfo { + uint64_t startupCost; + uint64_t totalCost; + uint64_t numOfRows; +} SExplainExecInfo; + typedef struct { - int32_t numOfItems; - char data[]; + int32_t numOfPlans; + SExplainExecInfo *subplanInfo; } SExplainRsp; +int32_t tSerializeSExplainRsp(void* buf, int32_t bufLen, SExplainRsp* pRsp); +int32_t tDeserializeSExplainRsp(void* buf, int32_t bufLen, SExplainRsp* pRsp); typedef struct { char fqdn[TSDB_FQDN_LEN]; // end point, hostname:port diff --git a/include/libs/command/command.h b/include/libs/command/command.h index 31e370ac5c..fa229d7265 100644 --- a/include/libs/command/command.h +++ b/include/libs/command/command.h @@ -19,17 +19,11 @@ typedef struct SExplainCtx SExplainCtx; -typedef struct SExplainExecInfo { - uint64_t startupCost; - uint64_t totalCost; - uint64_t numOfRows; -} SExplainExecInfo; - int32_t qExecCommand(SNode* pStmt, SRetrieveTableRsp** pRsp); int32_t qExecStaticExplain(SQueryPlan *pDag, SRetrieveTableRsp **pRsp); int32_t qExecExplainBegin(SQueryPlan *pDag, SExplainCtx **pCtx, int32_t startTs); -int32_t qExecExplainEnd(SExplainCtx *pCtx); +int32_t qExecExplainEnd(SExplainCtx *pCtx, SRetrieveTableRsp **pRsp); int32_t qExplainUpdateExecInfo(SExplainCtx *pCtx, SExplainRsp *pRspMsg, int32_t groupId, SRetrieveTableRsp **pRsp); void qExplainFreeCtx(SExplainCtx *pCtx); diff --git a/include/libs/nodes/nodes.h b/include/libs/nodes/nodes.h index 2979040c07..1c229fabb7 100644 --- a/include/libs/nodes/nodes.h +++ b/include/libs/nodes/nodes.h @@ -217,6 +217,7 @@ int32_t nodesStringToList(const char* pStr, SNodeList** pList); int32_t nodesNodeToSQL(SNode *pNode, char *buf, int32_t bufSize, int32_t *len); char *nodesGetNameFromColumnNode(SNode *pNode); +int32_t nodesGetOutputNumFromSlotList(SNodeList* pSlots); #ifdef __cplusplus } diff --git a/include/util/tlockfree.h b/include/util/tlockfree.h index e49e89c309..44e43f81cf 100644 --- a/include/util/tlockfree.h +++ b/include/util/tlockfree.h @@ -76,6 +76,7 @@ void taosWLockLatch(SRWLatch *pLatch); void taosWUnLockLatch(SRWLatch *pLatch); void taosRLockLatch(SRWLatch *pLatch); void taosRUnLockLatch(SRWLatch *pLatch); +int32_t taosWTryLockLatch(SRWLatch *pLatch); // copy on read #define taosCorBeginRead(x) \ diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index c85184ffba..0b25d7286f 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -2787,6 +2787,48 @@ int32_t tDecodeSMqCMCommitOffsetReq(SCoder *decoder, SMqCMCommitOffsetReq *pReq) return 0; } +int32_t tSerializeSExplainRsp(void* buf, int32_t bufLen, SExplainRsp* pRsp) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + + if (tStartEncode(&encoder) < 0) return -1; + if (tEncodeI32(&encoder, pRsp->numOfPlans) < 0) return -1; + for (int32_t i = 0; i < pRsp->numOfPlans; ++i) { + SExplainExecInfo *info = &pRsp->subplanInfo[i]; + if (tEncodeU64(&encoder, info->startupCost) < 0) return -1; + if (tEncodeU64(&encoder, info->totalCost) < 0) return -1; + if (tEncodeU64(&encoder, info->numOfRows) < 0) return -1; + } + + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); + return tlen; +} + +int32_t tDeserializeSExplainRsp(void* buf, int32_t bufLen, SExplainRsp* pRsp) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + + if (tStartDecode(&decoder) < 0) return -1; + if (tDecodeI32(&decoder, &pRsp->numOfPlans) < 0) return -1; + if (pRsp->numOfPlans > 0) { + pRsp->subplanInfo = taosMemoryMalloc(pRsp->numOfPlans * sizeof(SExplainExecInfo)); + if (pRsp->subplanInfo == NULL) return -1; + } + for (int32_t i = 0; i < pRsp->numOfPlans; ++i) { + if (tDecodeU64(&decoder, &pRsp->subplanInfo[i].startupCost) < 0) return -1; + if (tDecodeU64(&decoder, &pRsp->subplanInfo[i].totalCost) < 0) return -1; + if (tDecodeU64(&decoder, &pRsp->subplanInfo[i].numOfRows) < 0) return -1; + } + + tEndDecode(&decoder); + + tCoderClear(&decoder); + return 0; +} + int32_t tSerializeSSchedulerHbReq(void *buf, int32_t bufLen, SSchedulerHbReq *pReq) { int32_t headLen = sizeof(SMsgHead); if (buf != NULL) { diff --git a/source/libs/command/inc/commandInt.h b/source/libs/command/inc/commandInt.h index de0a7c433a..7e9ea12ea4 100644 --- a/source/libs/command/inc/commandInt.h +++ b/source/libs/command/inc/commandInt.h @@ -26,39 +26,53 @@ extern "C" { #define EXPLAIN_MAX_GROUP_NUM 100 //newline area -#define EXPLAIN_TAG_SCAN_FORMAT "Tag Scan on %s columns=%d width=%d" -#define EXPLAIN_TBL_SCAN_FORMAT "Table Scan on %s columns=%d width=%d" -#define EXPLAIN_SYSTBL_SCAN_FORMAT "System Table Scan on %s columns=%d width=%d" -#define EXPLAIN_PROJECTION_FORMAT "Projection columns=%d width=%d" -#define EXPLAIN_JOIN_FORMAT "%s between %d tables width=%d" -#define EXPLAIN_AGG_FORMAT "Aggragate functions=%d" -#define EXPLAIN_EXCHANGE_FORMAT "Data Exchange %d:1 width=%d" -#define EXPLAIN_SORT_FORMAT "Sort on %d Column(s) width=%d" -#define EXPLAIN_INTERVAL_FORMAT "Interval on Column %s functions=%d interval=%" PRId64 "%c offset=%" PRId64 "%c sliding=%" PRId64 "%c width=%d" -#define EXPLAIN_SESSION_FORMAT "Session gap=%" PRId64 " functions=%d width=%d" +#define EXPLAIN_TAG_SCAN_FORMAT "Tag Scan on %s" +#define EXPLAIN_TBL_SCAN_FORMAT "Table Scan on %s" +#define EXPLAIN_SYSTBL_SCAN_FORMAT "System Table Scan on %s" +#define EXPLAIN_PROJECTION_FORMAT "Projection" +#define EXPLAIN_JOIN_FORMAT "%s" +#define EXPLAIN_AGG_FORMAT "Aggragate" +#define EXPLAIN_EXCHANGE_FORMAT "Data Exchange %d:1" +#define EXPLAIN_SORT_FORMAT "Sort" +#define EXPLAIN_INTERVAL_FORMAT "Interval on Column %s" +#define EXPLAIN_SESSION_FORMAT "Session" #define EXPLAIN_ORDER_FORMAT "Order: %s" #define EXPLAIN_FILTER_FORMAT "Filter: " #define EXPLAIN_FILL_FORMAT "Fill: %s" #define EXPLAIN_ON_CONDITIONS_FORMAT "Join Cond: " #define EXPLAIN_TIMERANGE_FORMAT "Time Range: [%" PRId64 ", %" PRId64 "]" +#define EXPLAIN_OUTPUT_FORMAT "Output: " +#define EXPLAIN_TIME_WINDOWS_FORMAT "Time Window: interval=%" PRId64 "%c offset=%" PRId64 "%c sliding=%" PRId64 "%c" +#define EXPLAIN_WINDOW_FORMAT "Window: gap=%" PRId64 //append area -#define EXPLAIN_GROUPS_FORMAT " groups=%d" -#define EXPLAIN_WIDTH_FORMAT " width=%d" -#define EXPLAIN_LOOPS_FORMAT " loops=%d" -#define EXPLAIN_REVERSE_FORMAT " reverse=%d" +#define EXPLAIN_LEFT_PARENTHESIS_FORMAT " (" +#define EXPLAIN_RIGHT_PARENTHESIS_FORMAT ")" +#define EXPLAIN_BLANK_FORMAT " " +#define EXPLAIN_COST_FORMAT "cost=%.2f..%.2f" +#define EXPLAIN_ROWS_FORMAT "rows=%" PRIu64 +#define EXPLAIN_COLUMNS_FORMAT "columns=%d" +#define EXPLAIN_WIDTH_FORMAT "width=%d" +#define EXPLAIN_GROUPS_FORMAT "groups=%d" +#define EXPLAIN_WIDTH_FORMAT "width=%d" +#define EXPLAIN_LOOPS_FORMAT "loops=%d" +#define EXPLAIN_REVERSE_FORMAT "reverse=%d" +#define EXPLAIN_FUNCTIONS_FORMAT "functions=%d" typedef struct SExplainGroup { int32_t nodeNum; + int32_t physiPlanExecNum; + int32_t physiPlanNum; + int32_t physiPlanExecIdx; SRWLatch lock; SSubplan *plan; - SArray *execInfo; + SArray *nodeExecInfo; //Array } SExplainGroup; typedef struct SExplainResNode { - SNodeList* pChildren; - SPhysiNode* pNode; - void* pExecInfo; + SNodeList* pChildren; + SPhysiNode* pNode; + SArray* pExecInfo; // Array } SExplainResNode; typedef struct SQueryExplainRowInfo { @@ -68,19 +82,21 @@ typedef struct SQueryExplainRowInfo { } SQueryExplainRowInfo; typedef struct SExplainCtx { - double ratio; - bool verbose; + EExplainMode mode; + double ratio; + bool verbose; - int32_t rootGroupId; - int32_t dataSize; - bool execDone; - int64_t reqStartTs; - int64_t jobStartTs; - int64_t jobDoneTs; - char *tbuf; - SArray *rows; - int32_t groupDoneNum; - SHashObj *groupHash; + SRWLatch lock; + int32_t rootGroupId; + int32_t dataSize; + bool execDone; + int64_t reqStartTs; + int64_t jobStartTs; + int64_t jobDoneTs; + char *tbuf; + SArray *rows; + int32_t groupDoneNum; + SHashObj *groupHash; // Hash } SExplainCtx; #define EXPLAIN_ORDER_STRING(_order) ((TSDB_ORDER_ASC == _order) ? "Ascending" : "Descending") diff --git a/source/libs/command/src/explain.c b/source/libs/command/src/explain.c index 4bf354dee5..c8502388ba 100644 --- a/source/libs/command/src/explain.c +++ b/source/libs/command/src/explain.c @@ -17,7 +17,7 @@ #include "plannodes.h" #include "commandInt.h" -int32_t qExplainGenerateResNode(SPhysiNode *pNode, void *pExecInfo, SExplainResNode **pRes); +int32_t qExplainGenerateResNode(SPhysiNode *pNode, SExplainGroup *group, SExplainResNode **pRes); int32_t qExplainAppendGroupResRows(void *pCtx, int32_t groupId, int32_t level); @@ -48,12 +48,30 @@ void qExplainFreeCtx(SExplainCtx *pCtx) { taosMemoryFreeClear(row->buf); } + if (EXPLAIN_MODE_ANALYZE == pCtx->mode && pCtx->groupHash) { + void *pIter = taosHashIterate(pCtx->groupHash, NULL); + while (pIter) { + SExplainGroup *group = (SExplainGroup *)pIter; + if (NULL == group->nodeExecInfo) { + continue; + } + + int32_t num = taosArrayGetSize(group->nodeExecInfo); + for (int32_t i = 0; i < num; ++i) { + SExplainRsp *rsp = taosArrayGet(group->nodeExecInfo, i); + taosMemoryFreeClear(rsp->subplanInfo); + } + + pIter = taosHashIterate(pCtx->groupHash, pIter); + } + } + taosHashCleanup(pCtx->groupHash); taosArrayDestroy(pCtx->rows); taosMemoryFree(pCtx); } -int32_t qExplainInitCtx(SExplainCtx **pCtx, SHashObj *groupHash, bool verbose, double ratio) { +int32_t qExplainInitCtx(SExplainCtx **pCtx, SHashObj *groupHash, bool verbose, double ratio, EExplainMode mode) { int32_t code = 0; SExplainCtx *ctx = taosMemoryCalloc(1, sizeof(SExplainCtx)); if (NULL == ctx) { @@ -73,6 +91,7 @@ int32_t qExplainInitCtx(SExplainCtx **pCtx, SHashObj *groupHash, bool verbose, d QRY_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); } + ctx->mode = mode; ctx->verbose = verbose; ctx->ratio = ratio; ctx->tbuf = tbuf; @@ -92,7 +111,7 @@ _return: QRY_RET(code); } -int32_t qExplainGenerateResChildren(SPhysiNode *pNode, void *pExecInfo, SNodeList **pChildren) { +int32_t qExplainGenerateResChildren(SPhysiNode *pNode, SExplainGroup *group, SNodeList **pChildren) { int32_t tlen = 0; SNodeList *pPhysiChildren = NULL; @@ -164,20 +183,43 @@ int32_t qExplainGenerateResChildren(SPhysiNode *pNode, void *pExecInfo, SNodeLis SNode* node = NULL; SExplainResNode *pResNode = NULL; FOREACH(node, pPhysiChildren) { - QRY_ERR_RET(qExplainGenerateResNode((SPhysiNode *)node, pExecInfo, &pResNode)); + QRY_ERR_RET(qExplainGenerateResNode((SPhysiNode *)node, group, &pResNode)); QRY_ERR_RET(nodesListAppend(*pChildren, pResNode)); } return TSDB_CODE_SUCCESS; } -int32_t qExplainGenerateResNode(SPhysiNode *pNode, void *pExecInfo, SExplainResNode **pResNode) { +int32_t qExplainGenerateResNodeExecInfo(SArray **pExecInfo, SExplainGroup *group) { + *pExecInfo = taosArrayInit(group->nodeNum, sizeof(SExplainExecInfo)); + if (NULL == (*pExecInfo)) { + qError("taosArrayInit %d explainExecInfo failed", group->nodeNum); + return TSDB_CODE_QRY_OUT_OF_MEMORY; + } + + SExplainRsp *rsp = NULL; + for (int32_t i = 0; i < group->nodeNum; ++i) { + rsp = taosArrayGet(group->nodeExecInfo, i); + if (group->physiPlanExecIdx >= rsp->numOfPlans) { + qError("physiPlanIdx %d exceed plan num %d", group->physiPlanExecIdx, rsp->numOfPlans); + return TSDB_CODE_QRY_APP_ERROR; + } + + taosArrayPush(*pExecInfo, rsp->subplanInfo + group->physiPlanExecIdx); + } + + ++group->physiPlanExecIdx; + + return TSDB_CODE_SUCCESS; +} + +int32_t qExplainGenerateResNode(SPhysiNode *pNode, SExplainGroup *group, SExplainResNode **pResNode) { if (NULL == pNode) { *pResNode = NULL; qError("physical node is NULL"); return TSDB_CODE_QRY_APP_ERROR; } - + SExplainResNode *resNode = taosMemoryCalloc(1, sizeof(SExplainResNode)); if (NULL == resNode) { qError("calloc SPhysiNodeExplainRes failed"); @@ -186,8 +228,11 @@ int32_t qExplainGenerateResNode(SPhysiNode *pNode, void *pExecInfo, SExplainResN int32_t code = 0; resNode->pNode = pNode; - resNode->pExecInfo = pExecInfo; - QRY_ERR_JRET(qExplainGenerateResChildren(pNode, pExecInfo, &resNode->pChildren)); + QRY_ERR_JRET(qExplainGenerateResNodeExecInfo(&resNode->pExecInfo, group)); + + QRY_ERR_JRET(qExplainGenerateResChildren(pNode, group, &resNode->pChildren)); + + ++group->physiPlanNum; *pResNode = resNode; @@ -200,7 +245,7 @@ _return: QRY_RET(code); } -int32_t qExplainBufAppendExecInfo(void *pExecInfo, char *tbuf, int32_t *len) { +int32_t qExplainBufAppendExecInfo(SArray *pExecInfo, char *tbuf, int32_t *len) { int32_t tlen = *len; EXPLAIN_ROW_APPEND("(exec info here)"); @@ -247,18 +292,33 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i switch (pNode->type) { case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN: { STagScanPhysiNode *pTagScanNode = (STagScanPhysiNode *)pNode; - EXPLAIN_ROW_NEW(level, EXPLAIN_TAG_SCAN_FORMAT, pTagScanNode->tableName.tname, pTagScanNode->pScanCols->length, pTagScanNode->node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_NEW(level, EXPLAIN_TAG_SCAN_FORMAT, pTagScanNode->tableName.tname); + EXPLAIN_ROW_APPEND(EXPLAIN_LEFT_PARENTHESIS_FORMAT); if (pResNode->pExecInfo) { QRY_ERR_RET(qExplainBufAppendExecInfo(pResNode->pExecInfo, tbuf, &tlen)); - } + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + } + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, pTagScanNode->pScanCols->length); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pTagScanNode->node.pOutputDataBlockDesc->totalRowSize); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); EXPLAIN_ROW_APPEND(EXPLAIN_LOOPS_FORMAT, pTagScanNode->count); if (pTagScanNode->reverse) { + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); EXPLAIN_ROW_APPEND(EXPLAIN_REVERSE_FORMAT, pTagScanNode->reverse); } + EXPLAIN_ROW_APPEND(EXPLAIN_RIGHT_PARENTHESIS_FORMAT); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level)); if (verbose) { + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_OUTPUT_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, nodesGetOutputNumFromSlotList(pTagScanNode->node.pOutputDataBlockDesc->pSlots)); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pTagScanNode->node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_ORDER_FORMAT, EXPLAIN_ORDER_STRING(pTagScanNode->order)); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); @@ -268,18 +328,33 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i case QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN: case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:{ STableScanPhysiNode *pTblScanNode = (STableScanPhysiNode *)pNode; - EXPLAIN_ROW_NEW(level, EXPLAIN_TBL_SCAN_FORMAT, pTblScanNode->scan.tableName.tname, pTblScanNode->scan.pScanCols->length, pTblScanNode->scan.node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_NEW(level, EXPLAIN_TBL_SCAN_FORMAT, pTblScanNode->scan.tableName.tname); + EXPLAIN_ROW_APPEND(EXPLAIN_LEFT_PARENTHESIS_FORMAT); if (pResNode->pExecInfo) { QRY_ERR_RET(qExplainBufAppendExecInfo(pResNode->pExecInfo, tbuf, &tlen)); - } + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + } + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, pTblScanNode->scan.pScanCols->length); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pTblScanNode->scan.node.pOutputDataBlockDesc->totalRowSize); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); EXPLAIN_ROW_APPEND(EXPLAIN_LOOPS_FORMAT, pTblScanNode->scan.count); if (pTblScanNode->scan.reverse) { + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); EXPLAIN_ROW_APPEND(EXPLAIN_REVERSE_FORMAT, pTblScanNode->scan.reverse); } + EXPLAIN_ROW_APPEND(EXPLAIN_RIGHT_PARENTHESIS_FORMAT); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level)); if (verbose) { + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_OUTPUT_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, nodesGetOutputNumFromSlotList(pTblScanNode->scan.node.pOutputDataBlockDesc->pSlots)); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pTblScanNode->scan.node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_ORDER_FORMAT, EXPLAIN_ORDER_STRING(pTblScanNode->scan.order)); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); @@ -299,18 +374,33 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i } case QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN:{ SSystemTableScanPhysiNode *pSTblScanNode = (SSystemTableScanPhysiNode *)pNode; - EXPLAIN_ROW_NEW(level, EXPLAIN_SYSTBL_SCAN_FORMAT, pSTblScanNode->scan.tableName.tname, pSTblScanNode->scan.pScanCols->length, pSTblScanNode->scan.node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_NEW(level, EXPLAIN_SYSTBL_SCAN_FORMAT, pSTblScanNode->scan.tableName.tname); + EXPLAIN_ROW_APPEND(EXPLAIN_LEFT_PARENTHESIS_FORMAT); if (pResNode->pExecInfo) { QRY_ERR_RET(qExplainBufAppendExecInfo(pResNode->pExecInfo, tbuf, &tlen)); - } + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + } + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, pSTblScanNode->scan.pScanCols->length); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pSTblScanNode->scan.node.pOutputDataBlockDesc->totalRowSize); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); EXPLAIN_ROW_APPEND(EXPLAIN_LOOPS_FORMAT, pSTblScanNode->scan.count); if (pSTblScanNode->scan.reverse) { + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); EXPLAIN_ROW_APPEND(EXPLAIN_REVERSE_FORMAT, pSTblScanNode->scan.reverse); } + EXPLAIN_ROW_APPEND(EXPLAIN_RIGHT_PARENTHESIS_FORMAT); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level)); if (verbose) { + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_OUTPUT_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, nodesGetOutputNumFromSlotList(pSTblScanNode->scan.node.pOutputDataBlockDesc->pSlots)); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pSTblScanNode->scan.node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_ORDER_FORMAT, EXPLAIN_ORDER_STRING(pSTblScanNode->scan.order)); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); @@ -327,14 +417,27 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i } case QUERY_NODE_PHYSICAL_PLAN_PROJECT:{ SProjectPhysiNode *pPrjNode = (SProjectPhysiNode *)pNode; - EXPLAIN_ROW_NEW(level, EXPLAIN_PROJECTION_FORMAT, pPrjNode->pProjections->length, pPrjNode->node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_NEW(level, EXPLAIN_PROJECTION_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_LEFT_PARENTHESIS_FORMAT); if (pResNode->pExecInfo) { QRY_ERR_RET(qExplainBufAppendExecInfo(pResNode->pExecInfo, tbuf, &tlen)); - } + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + } + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, pPrjNode->pProjections->length); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pPrjNode->node.pOutputDataBlockDesc->totalRowSize); + EXPLAIN_ROW_APPEND(EXPLAIN_RIGHT_PARENTHESIS_FORMAT); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level)); if (verbose) { + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_OUTPUT_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, nodesGetOutputNumFromSlotList(pPrjNode->node.pOutputDataBlockDesc->pSlots)); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pPrjNode->node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + if (pPrjNode->node.pConditions) { EXPLAIN_ROW_NEW(level + 1, EXPLAIN_FILTER_FORMAT); QRY_ERR_RET(nodesNodeToSQL(pPrjNode->node.pConditions, tbuf + VARSTR_HEADER_SIZE, TSDB_EXPLAIN_RESULT_ROW_SIZE, &tlen)); @@ -346,14 +449,27 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i } case QUERY_NODE_PHYSICAL_PLAN_JOIN:{ SJoinPhysiNode *pJoinNode = (SJoinPhysiNode *)pNode; - EXPLAIN_ROW_NEW(level, EXPLAIN_JOIN_FORMAT, EXPLAIN_JOIN_STRING(pJoinNode->joinType), pJoinNode->pTargets->length, pJoinNode->node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_NEW(level, EXPLAIN_JOIN_FORMAT, EXPLAIN_JOIN_STRING(pJoinNode->joinType)); + EXPLAIN_ROW_APPEND(EXPLAIN_LEFT_PARENTHESIS_FORMAT); if (pResNode->pExecInfo) { QRY_ERR_RET(qExplainBufAppendExecInfo(pResNode->pExecInfo, tbuf, &tlen)); - } + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + } + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, pJoinNode->pTargets->length); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pJoinNode->node.pOutputDataBlockDesc->totalRowSize); + EXPLAIN_ROW_APPEND(EXPLAIN_RIGHT_PARENTHESIS_FORMAT); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level)); if (verbose) { + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_OUTPUT_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, nodesGetOutputNumFromSlotList(pJoinNode->node.pOutputDataBlockDesc->pSlots)); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pJoinNode->node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + if (pJoinNode->node.pConditions) { EXPLAIN_ROW_NEW(level + 1, EXPLAIN_FILTER_FORMAT); QRY_ERR_RET(nodesNodeToSQL(pJoinNode->node.pConditions, tbuf + VARSTR_HEADER_SIZE, TSDB_EXPLAIN_RESULT_ROW_SIZE, &tlen)); @@ -370,19 +486,31 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i } case QUERY_NODE_PHYSICAL_PLAN_AGG:{ SAggPhysiNode *pAggNode = (SAggPhysiNode *)pNode; - EXPLAIN_ROW_NEW(level, EXPLAIN_AGG_FORMAT, pAggNode->pAggFuncs->length); - if (pAggNode->pGroupKeys) { - EXPLAIN_ROW_APPEND(EXPLAIN_GROUPS_FORMAT, pAggNode->pGroupKeys->length); - } - EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pAggNode->node.pOutputDataBlockDesc->outputRowSize); - + EXPLAIN_ROW_NEW(level, EXPLAIN_AGG_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_LEFT_PARENTHESIS_FORMAT); if (pResNode->pExecInfo) { QRY_ERR_RET(qExplainBufAppendExecInfo(pResNode->pExecInfo, tbuf, &tlen)); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + } + EXPLAIN_ROW_APPEND(EXPLAIN_FUNCTIONS_FORMAT, pAggNode->pAggFuncs->length); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pAggNode->node.pOutputDataBlockDesc->totalRowSize); + if (pAggNode->pGroupKeys) { + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_GROUPS_FORMAT, pAggNode->pGroupKeys->length); } + EXPLAIN_ROW_APPEND(EXPLAIN_RIGHT_PARENTHESIS_FORMAT); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level)); if (verbose) { + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_OUTPUT_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, nodesGetOutputNumFromSlotList(pAggNode->node.pOutputDataBlockDesc->pSlots)); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pAggNode->node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + if (pAggNode->node.pConditions) { EXPLAIN_ROW_NEW(level + 1, EXPLAIN_FILTER_FORMAT); QRY_ERR_RET(nodesNodeToSQL(pAggNode->node.pConditions, tbuf + VARSTR_HEADER_SIZE, TSDB_EXPLAIN_RESULT_ROW_SIZE, &tlen)); @@ -400,14 +528,25 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i QRY_ERR_RET(TSDB_CODE_QRY_APP_ERROR); } - EXPLAIN_ROW_NEW(level, EXPLAIN_EXCHANGE_FORMAT, group->nodeNum, pExchNode->node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_NEW(level, EXPLAIN_EXCHANGE_FORMAT, group->nodeNum); + EXPLAIN_ROW_APPEND(EXPLAIN_LEFT_PARENTHESIS_FORMAT); if (pResNode->pExecInfo) { QRY_ERR_RET(qExplainBufAppendExecInfo(pResNode->pExecInfo, tbuf, &tlen)); - } + } + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pExchNode->node.pOutputDataBlockDesc->totalRowSize); + EXPLAIN_ROW_APPEND(EXPLAIN_RIGHT_PARENTHESIS_FORMAT); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level)); if (verbose) { + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_OUTPUT_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, nodesGetOutputNumFromSlotList(pExchNode->node.pOutputDataBlockDesc->pSlots)); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pExchNode->node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + if (pExchNode->node.pConditions) { EXPLAIN_ROW_NEW(level + 1, EXPLAIN_FILTER_FORMAT); QRY_ERR_RET(nodesNodeToSQL(pExchNode->node.pConditions, tbuf + VARSTR_HEADER_SIZE, TSDB_EXPLAIN_RESULT_ROW_SIZE, &tlen)); @@ -421,14 +560,28 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i } case QUERY_NODE_PHYSICAL_PLAN_SORT:{ SSortPhysiNode *pSortNode = (SSortPhysiNode *)pNode; - EXPLAIN_ROW_NEW(level, EXPLAIN_SORT_FORMAT, pSortNode->pSortKeys->length, pSortNode->node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_NEW(level, EXPLAIN_SORT_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_LEFT_PARENTHESIS_FORMAT); if (pResNode->pExecInfo) { QRY_ERR_RET(qExplainBufAppendExecInfo(pResNode->pExecInfo, tbuf, &tlen)); - } + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + } + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, pSortNode->pSortKeys->length); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pSortNode->node.pOutputDataBlockDesc->totalRowSize); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_RIGHT_PARENTHESIS_FORMAT); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level)); if (verbose) { + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_OUTPUT_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, nodesGetOutputNumFromSlotList(pSortNode->node.pOutputDataBlockDesc->pSlots)); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pSortNode->node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + if (pSortNode->node.pConditions) { EXPLAIN_ROW_NEW(level + 1, EXPLAIN_FILTER_FORMAT); QRY_ERR_RET(nodesNodeToSQL(pSortNode->node.pConditions, tbuf + VARSTR_HEADER_SIZE, TSDB_EXPLAIN_RESULT_ROW_SIZE, &tlen)); @@ -440,18 +593,32 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i } case QUERY_NODE_PHYSICAL_PLAN_INTERVAL:{ SIntervalPhysiNode *pIntNode = (SIntervalPhysiNode *)pNode; - EXPLAIN_ROW_NEW(level, EXPLAIN_INTERVAL_FORMAT, nodesGetNameFromColumnNode(pIntNode->pTspk), pIntNode->window.pFuncs->length, - INVERAL_TIME_FROM_PRECISION_TO_UNIT(pIntNode->interval, pIntNode->intervalUnit, pIntNode->precision), pIntNode->intervalUnit, - pIntNode->offset, getPrecisionUnit(pIntNode->precision), - INVERAL_TIME_FROM_PRECISION_TO_UNIT(pIntNode->sliding, pIntNode->slidingUnit, pIntNode->precision), pIntNode->slidingUnit, - pIntNode->window.node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_NEW(level, EXPLAIN_INTERVAL_FORMAT, nodesGetNameFromColumnNode(pIntNode->pTspk)); + EXPLAIN_ROW_APPEND(EXPLAIN_LEFT_PARENTHESIS_FORMAT); if (pResNode->pExecInfo) { QRY_ERR_RET(qExplainBufAppendExecInfo(pResNode->pExecInfo, tbuf, &tlen)); - } + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + } + EXPLAIN_ROW_APPEND(EXPLAIN_FUNCTIONS_FORMAT, pIntNode->window.pFuncs->length); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pIntNode->window.node.pOutputDataBlockDesc->totalRowSize); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level)); if (verbose) { + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_OUTPUT_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, nodesGetOutputNumFromSlotList(pIntNode->window.node.pOutputDataBlockDesc->pSlots)); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pIntNode->window.node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_TIME_WINDOWS_FORMAT, INVERAL_TIME_FROM_PRECISION_TO_UNIT(pIntNode->interval, pIntNode->intervalUnit, pIntNode->precision), + pIntNode->intervalUnit, pIntNode->offset, getPrecisionUnit(pIntNode->precision), + INVERAL_TIME_FROM_PRECISION_TO_UNIT(pIntNode->sliding, pIntNode->slidingUnit, pIntNode->precision), pIntNode->slidingUnit); + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + if (pIntNode->pFill) { EXPLAIN_ROW_NEW(level + 1, EXPLAIN_FILL_FORMAT, getFillModeString(pIntNode->pFill->mode)); EXPLAIN_ROW_END(); @@ -468,18 +635,36 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i break; } case QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW:{ - SSessionWinodwPhysiNode *pIntNode = (SSessionWinodwPhysiNode *)pNode; - EXPLAIN_ROW_NEW(level, EXPLAIN_SESSION_FORMAT, pIntNode->gap, pIntNode->window.pFuncs->length, pIntNode->window.node.pOutputDataBlockDesc->outputRowSize); + SSessionWinodwPhysiNode *pSessNode = (SSessionWinodwPhysiNode *)pNode; + EXPLAIN_ROW_NEW(level, EXPLAIN_SESSION_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_LEFT_PARENTHESIS_FORMAT); if (pResNode->pExecInfo) { QRY_ERR_RET(qExplainBufAppendExecInfo(pResNode->pExecInfo, tbuf, &tlen)); - } + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + } + EXPLAIN_ROW_APPEND(EXPLAIN_FUNCTIONS_FORMAT, pSessNode->window.pFuncs->length); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pSessNode->window.node.pOutputDataBlockDesc->totalRowSize); + EXPLAIN_ROW_APPEND(EXPLAIN_RIGHT_PARENTHESIS_FORMAT); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level)); + if (verbose) { - if (pIntNode->window.node.pConditions) { + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_OUTPUT_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_COLUMNS_FORMAT, nodesGetOutputNumFromSlotList(pSessNode->window.node.pOutputDataBlockDesc->pSlots)); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); + EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pSessNode->window.node.pOutputDataBlockDesc->outputRowSize); + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + + EXPLAIN_ROW_NEW(level + 1, EXPLAIN_WINDOW_FORMAT, pSessNode->gap); + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + + if (pSessNode->window.node.pConditions) { EXPLAIN_ROW_NEW(level + 1, EXPLAIN_FILTER_FORMAT); - QRY_ERR_RET(nodesNodeToSQL(pIntNode->window.node.pConditions, tbuf + VARSTR_HEADER_SIZE, TSDB_EXPLAIN_RESULT_ROW_SIZE, &tlen)); + QRY_ERR_RET(nodesNodeToSQL(pSessNode->window.node.pConditions, tbuf + VARSTR_HEADER_SIZE, TSDB_EXPLAIN_RESULT_ROW_SIZE, &tlen)); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); } @@ -523,7 +708,12 @@ int32_t qExplainAppendGroupResRows(void *pCtx, int32_t groupId, int32_t level) { QRY_ERR_RET(TSDB_CODE_QRY_APP_ERROR); } - QRY_ERR_RET(qExplainGenerateResNode(group->plan->pNode, group->execInfo, &node)); + QRY_ERR_RET(qExplainGenerateResNode(group->plan->pNode, group, &node)); + + if (group->physiPlanNum != group->physiPlanExecNum) { + qError("physiPlanNum %d mismatch with physiExecNum %d in group %d", group->physiPlanNum, group->physiPlanExecNum, groupId); + QRY_ERR_JRET(TSDB_CODE_QRY_APP_ERROR); + } QRY_ERR_JRET(qExplainResNodeToRows(node, ctx, level)); @@ -601,7 +791,7 @@ int32_t qExplainPrepareCtx(SQueryPlan *pDag, SExplainCtx **pCtx) { QRY_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); } - QRY_ERR_JRET(qExplainInitCtx(&ctx, groupHash, pDag->explainInfo.verbose, pDag->explainInfo.ratio)); + QRY_ERR_JRET(qExplainInitCtx(&ctx, groupHash, pDag->explainInfo.verbose, pDag->explainInfo.ratio, pDag->explainInfo.mode)); for (int32_t i = 0; i < levelNum; ++i) { plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i); @@ -625,7 +815,10 @@ int32_t qExplainPrepareCtx(SQueryPlan *pDag, SExplainCtx **pCtx) { continue; } - SExplainGroup group = {.nodeNum = 1, .plan = plan, .execInfo = NULL}; + SExplainGroup group = {0}; + group.nodeNum = 1; + group.plan = plan; + if (0 != taosHashPut(groupHash, &plan->id.groupId, sizeof(plan->id.groupId), &group, sizeof(group))) { qError("taosHashPut to explainGroupHash failed, taskIdx:%d", n); QRY_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); @@ -656,8 +849,70 @@ _return: } -int32_t qExplainUpdateExecInfo(SExplainCtx *pCtx, SExplainRsp *pRspMsg, int32_t groupId, SRetrieveTableRsp **pRsp) { +int32_t qExplainGenerateRsp(SExplainCtx *pCtx, SRetrieveTableRsp **pRsp) { + QRY_ERR_RET(qExplainAppendGroupResRows(pCtx, pCtx->rootGroupId, 0)); + + QRY_ERR_RET(qExplainGetRspFromCtx(pCtx, pRsp)); + return TSDB_CODE_SUCCESS; +} + +int32_t qExplainUpdateExecInfo(SExplainCtx *pCtx, SExplainRsp *pRspMsg, int32_t groupId, SRetrieveTableRsp **pRsp) { + SExplainResNode *node = NULL; + int32_t code = 0; + bool groupDone = false; + SExplainCtx *ctx = (SExplainCtx *)pCtx; + + SExplainGroup *group = taosHashGet(ctx->groupHash, &groupId, sizeof(groupId)); + if (NULL == group) { + qError("group %d not in groupHash", groupId); + taosMemoryFreeClear(pRspMsg->subplanInfo); + QRY_ERR_RET(TSDB_CODE_QRY_APP_ERROR); + } + + taosWLockLatch(&group->lock); + if (NULL == group->nodeExecInfo) { + group->nodeExecInfo = taosArrayInit(group->nodeNum, sizeof(SExplainRsp)); + if (NULL == group->nodeExecInfo) { + qError("taosArrayInit %d explainExecInfo failed", group->nodeNum); + taosMemoryFreeClear(pRspMsg->subplanInfo); + taosWUnLockLatch(&group->lock); + + QRY_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); + } + + group->physiPlanExecNum = pRspMsg->numOfPlans; + } else if (taosArrayGetSize(group->nodeExecInfo) >= group->nodeNum) { + qError("group execInfo already full, size:%d, nodeNum:%d", (int32_t)taosArrayGetSize(group->nodeExecInfo), group->nodeNum); + taosMemoryFreeClear(pRspMsg->subplanInfo); + taosWUnLockLatch(&group->lock); + + QRY_ERR_RET(TSDB_CODE_QRY_APP_ERROR); + } + + if (group->physiPlanExecNum != pRspMsg->numOfPlans) { + qError("physiPlanExecNum %d mismatch with others %d in group %d", pRspMsg->numOfPlans, group->physiPlanExecNum, groupId); + taosMemoryFreeClear(pRspMsg->subplanInfo); + taosWUnLockLatch(&group->lock); + + QRY_ERR_RET(TSDB_CODE_QRY_APP_ERROR); + } + + taosArrayPush(group->nodeExecInfo, pRspMsg); + groupDone = (taosArrayGetSize(group->nodeExecInfo) >= group->nodeNum); + + taosWUnLockLatch(&group->lock); + + if (groupDone && (taosHashGetSize(pCtx->groupHash) == atomic_add_fetch_32(&pCtx->groupDoneNum, 1))) { + if (atomic_load_8((int8_t *)&pCtx->execDone)) { + if (0 == taosWTryLockLatch(&pCtx->lock)) { + QRY_ERR_RET(qExplainGenerateRsp(pCtx, pRsp)); + // LEAVE LOCK THERE + } + } + } + + return TSDB_CODE_SUCCESS; } @@ -666,10 +921,8 @@ int32_t qExecStaticExplain(SQueryPlan *pDag, SRetrieveTableRsp **pRsp) { SExplainCtx *pCtx = NULL; QRY_ERR_RET(qExplainPrepareCtx(pDag, &pCtx)); - - QRY_ERR_JRET(qExplainAppendGroupResRows(pCtx, pCtx->rootGroupId, 0)); - QRY_ERR_JRET(qExplainGetRspFromCtx(pCtx, pRsp)); + QRY_ERR_JRET(qExplainGenerateRsp(pCtx, pRsp)); _return: @@ -687,11 +940,19 @@ int32_t qExecExplainBegin(SQueryPlan *pDag, SExplainCtx **pCtx, int32_t startTs) return TSDB_CODE_SUCCESS; } -int32_t qExecExplainEnd(SExplainCtx *pCtx) { +int32_t qExecExplainEnd(SExplainCtx *pCtx, SRetrieveTableRsp **pRsp) { + int32_t code = 0; pCtx->jobDoneTs = taosGetTimestampMs(); atomic_store_8((int8_t *)&pCtx->execDone, true); + if (taosHashGetSize(pCtx->groupHash) == atomic_load_32(&pCtx->groupDoneNum)) { + if (0 == taosWTryLockLatch(&pCtx->lock)) { + QRY_ERR_RET(qExplainGenerateRsp(pCtx, pRsp)); + // LEAVE LOCK THERE + } + } + return TSDB_CODE_SUCCESS; } diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index a92813812a..f73ef1af38 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -36,6 +36,7 @@ extern "C" { #include "thash.h" #include "tlockfree.h" #include "tpagedbuf.h" +#include "tmsg.h" struct SColumnFilterElem; @@ -160,7 +161,7 @@ typedef struct STaskCostInfo { typedef struct SOperatorCostInfo { uint64_t openCost; - uint64_t execCost; + uint64_t totalCost; } SOperatorCostInfo; // The basic query information extracted from the SQueryInfo tree to support the @@ -714,6 +715,7 @@ int32_t getMaximumIdleDurationSec(); void doInvokeUdf(struct SUdfInfo* pUdfInfo, SqlFunctionCtx* pCtx, int32_t idx, int32_t type); void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status); int32_t createExecTaskInfoImpl(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId, EOPTR_EXEC_MODEL model); +int32_t getOperatorExplainExecInfo(SOperatorInfo *operator, SExplainExecInfo **pRes, int32_t *capacity, int32_t *resNum); #ifdef __cplusplus } diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index e423690740..498426d1a4 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -9523,11 +9523,27 @@ int32_t getOperatorExplainExecInfo(SOperatorInfo *operator, SExplainExecInfo **p *pRes = taosMemoryRealloc(*pRes, (*capacity) * sizeof(SExplainExecInfo)); if (NULL == *pRes) { - qError("malloc %d failed", capacity * sizeof(SExplainExecInfo)); + qError("malloc %d failed", (*capacity) * (int32_t)sizeof(SExplainExecInfo)); return TSDB_CODE_QRY_OUT_OF_MEMORY; } } + (*pRes)[*resNum].numOfRows = operator->resultInfo.totalRows; + (*pRes)[*resNum].startupCost = operator->cost.openCost; + (*pRes)[*resNum].totalCost = operator->cost.totalCost; + + ++(*resNum); + + int32_t code = 0; + for (int32_t i = 0; i < operator->numOfDownstream; ++i) { + code = getOperatorExplainExecInfo(operator->pDownstream[i], pRes, capacity, resNum); + if (code) { + taosMemoryFreeClear(*pRes); + return TSDB_CODE_QRY_OUT_OF_MEMORY; + } + } + + return TSDB_CODE_SUCCESS; } diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index 812fa333b6..2d5b38a1fe 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -1052,4 +1052,26 @@ char *nodesGetNameFromColumnNode(SNode *pNode) { return ((SColumnNode *)pNode)->colName; } +int32_t nodesGetOutputNumFromSlotList(SNodeList* pSlots) { + if (NULL == pSlots || pSlots->length <= 0) { + return 0; + } + + SNode* pNode = NULL; + int32_t num = 0; + FOREACH(pNode, pSlots) { + if (QUERY_NODE_SLOT_DESC != pNode->type) { + continue; + } + + SSlotDescNode *descNode = (SSlotDescNode *)pNode; + if (descNode->output) { + ++num; + } + } + + return num; +} + + diff --git a/source/libs/qworker/inc/qworkerMsg.h b/source/libs/qworker/inc/qworkerMsg.h index 313b9459e2..c0f4d6d157 100644 --- a/source/libs/qworker/inc/qworkerMsg.h +++ b/source/libs/qworker/inc/qworkerMsg.h @@ -23,7 +23,7 @@ extern "C" { #include "qworkerInt.h" #include "dataSinkMgt.h" -int32_t qwProcessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg, int8_t taskType); +int32_t qwProcessQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg, int8_t taskType, int8_t explain); int32_t qwProcessCQuery(QW_FPARAMS_DEF, SQWMsg *qwMsg); int32_t qwProcessReady(QW_FPARAMS_DEF, SQWMsg *qwMsg); int32_t qwProcessFetch(QW_FPARAMS_DEF, SQWMsg *qwMsg); @@ -38,6 +38,7 @@ void qwBuildFetchRsp(void *msg, SOutputData *input, int32_t len, bool qComple int32_t qwBuildAndSendCQueryMsg(QW_FPARAMS_DEF, SQWConnInfo *pConn); int32_t qwBuildAndSendReadyRsp(SQWConnInfo *pConn, int32_t code); int32_t qwBuildAndSendQueryRsp(SQWConnInfo *pConn, int32_t code); +int32_t qwBuildAndSendExplainRsp(SQWConnInfo *pConn, SExplainExecInfo *execInfo, int32_t num); void qwFreeFetchRsp(void *msg); int32_t qwMallocFetchRsp(int32_t length, SRetrieveTableRsp **rsp); int32_t qwGetSchTasksStatus(SQWorkerMgmt *mgmt, uint64_t sId, SSchedulerStatusRsp **rsp); diff --git a/source/libs/qworker/src/qworker.c b/source/libs/qworker/src/qworker.c index 726d0ebf79..a83fcc26f2 100644 --- a/source/libs/qworker/src/qworker.c +++ b/source/libs/qworker/src/qworker.c @@ -549,7 +549,10 @@ int32_t qwHandleTaskComplete(QW_FPARAMS_DEF, SQWTaskCtx *ctx) { int32_t resNum = 0; QW_ERR_RET(qGetExplainExecInfo(ctx->taskHandle, &resNum, &execInfo)); - QW_ERR_RET(qwBuildAndSendExplainRsp(&ctx->ctrlConnInfo, &rsp)); + SQWConnInfo connInfo = {0}; + connInfo.handle = ctx->ctrlConnInfo.handle; + + QW_ERR_RET(qwBuildAndSendExplainRsp(&connInfo, execInfo, resNum)); } qwFreeTaskHandle(QW_FPARAMS(), taskHandle); diff --git a/source/libs/qworker/src/qworkerMsg.c b/source/libs/qworker/src/qworkerMsg.c index c9779f9252..bf8f97aa4c 100644 --- a/source/libs/qworker/src/qworkerMsg.c +++ b/source/libs/qworker/src/qworkerMsg.c @@ -85,6 +85,27 @@ int32_t qwBuildAndSendReadyRsp(SQWConnInfo *pConn, int32_t code) { return TSDB_CODE_SUCCESS; } +int32_t qwBuildAndSendExplainRsp(SQWConnInfo *pConn, SExplainExecInfo *execInfo, int32_t num) { + SExplainRsp rsp = {.numOfPlans = num, .subplanInfo = execInfo}; + + int32_t contLen = tSerializeSExplainRsp(NULL, 0, &rsp); + void *pRsp = rpcMallocCont(contLen); + tSerializeSExplainRsp(pRsp, contLen, &rsp); + + SRpcMsg rpcRsp = { + .msgType = TDMT_VND_EXPLAIN_RSP, + .handle = pConn->handle, + .ahandle = pConn->ahandle, + .pCont = pRsp, + .contLen = contLen, + .code = 0, + }; + + tmsgSendRsp(&rpcRsp); + + return TSDB_CODE_SUCCESS; +} + int32_t qwBuildAndSendHbRsp(SQWConnInfo *pConn, SSchedulerHbRsp *pStatus, int32_t code) { int32_t contLen = tSerializeSSchedulerHbRsp(NULL, 0, pStatus); void *pRsp = rpcMallocCont(contLen); diff --git a/source/libs/scheduler/src/scheduler.c b/source/libs/scheduler/src/scheduler.c index 658d924f38..55eaae1b54 100644 --- a/source/libs/scheduler/src/scheduler.c +++ b/source/libs/scheduler/src/scheduler.c @@ -1043,6 +1043,19 @@ _return: SCH_RET(schProcessOnTaskFailure(pJob, pJob->fetchTask, code)); } +int32_t schProcessOnExplainDone(SSchJob *pJob, SSchTask *pTask, SRetrieveTableRsp *pRsp) { + SCH_TASK_DLOG("got explain rsp, rows:%d, complete:%d", htonl(pRsp->numOfRows), pRsp->completed); + + atomic_store_32(&pJob->resNumOfRows, htonl(pRsp->numOfRows)); + atomic_store_ptr(&pJob->resData, pRsp); + + SCH_SET_TASK_STATUS(pTask, JOB_TASK_STATUS_SUCCEED); + + schProcessOnDataFetched(pJob); + + return TSDB_CODE_SUCCESS; +} + // Note: no more task error processing, handled in function internal int32_t schHandleResponseMsg(SSchJob *pJob, SSchTask *pTask, int32_t msgType, char *msg, int32_t msgSize, int32_t rspCode) { @@ -1061,7 +1074,7 @@ int32_t schHandleResponseMsg(SSchJob *pJob, SSchTask *pTask, int32_t msgType, ch case TDMT_VND_CREATE_TABLE_RSP: { SVCreateTbBatchRsp batchRsp = {0}; if (msg) { - tDeserializeSVCreateTbBatchRsp(msg, msgSize, &batchRsp); + SCH_ERR_JRET(tDeserializeSVCreateTbBatchRsp(msg, msgSize, &batchRsp)); if (batchRsp.rspList) { int32_t num = taosArrayGetSize(batchRsp.rspList); for (int32_t i = 0; i < num; ++i) { @@ -1099,7 +1112,7 @@ int32_t schHandleResponseMsg(SSchJob *pJob, SSchTask *pTask, int32_t msgType, ch case TDMT_VND_QUERY_RSP: { SQueryTableRsp rsp = {0}; if (msg) { - tDeserializeSQueryTableRsp(msg, msgSize, &rsp); + SCH_ERR_JRET(tDeserializeSQueryTableRsp(msg, msgSize, &rsp)); SCH_ERR_JRET(rsp.code); } @@ -1126,13 +1139,11 @@ int32_t schHandleResponseMsg(SSchJob *pJob, SSchTask *pTask, int32_t msgType, ch break; } case TDMT_VND_EXPLAIN_RSP: { - SExplainRsp *taskRsp = (SExplainRsp *)msg; - SCH_ERR_JRET(rspCode); if (NULL == msg) { SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT); } - + if (!SCH_IS_EXPLAIN_JOB(pJob)) { SCH_TASK_ELOG("invalid msg received for none explain query, msg type:%s", TMSG_INFO(msgType)); SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT); @@ -1140,22 +1151,20 @@ int32_t schHandleResponseMsg(SSchJob *pJob, SSchTask *pTask, int32_t msgType, ch if (pJob->resData) { SCH_TASK_ELOG("explain result is already generated, res:%p", pJob->resData); - taosMemoryFreeClear(taskRsp); SCH_ERR_JRET(TSDB_CODE_SCH_STATUS_ERROR); } + SExplainRsp rsp = {0}; + if (tDeserializeSExplainRsp(msg, msgSize, &rsp)) { + taosMemoryFree(rsp.subplanInfo); + SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); + } + SRetrieveTableRsp *pRsp = NULL; - SCH_ERR_JRET(qExplainUpdateExecInfo(pJob->explainCtx, taskRsp, pTask->plan->id.groupId, &pRsp)); + SCH_ERR_JRET(qExplainUpdateExecInfo(pJob->explainCtx, &rsp, pTask->plan->id.groupId, &pRsp)); if (pRsp) { - atomic_store_ptr(&pJob->resData, pRsp); - atomic_store_32(&pJob->resNumOfRows, htonl(pRsp->numOfRows)); - - SCH_SET_TASK_STATUS(pTask, JOB_TASK_STATUS_SUCCEED); - - SCH_TASK_DLOG("got explain rsp, rows:%d, complete:%d", htonl(pRsp->numOfRows), pRsp->completed); - - schProcessOnDataFetched(pJob); + SCH_ERR_JRET(schProcessOnExplainDone(pJob, pTask, pRsp)); } break; } @@ -1168,8 +1177,13 @@ int32_t schHandleResponseMsg(SSchJob *pJob, SSchTask *pTask, int32_t msgType, ch } if (SCH_IS_EXPLAIN_JOB(pJob)) { - if (rsp->completed) { - SCH_ERR_JRET(qExecExplainEnd(pJob->explainCtx)); + if (rsp->completed) { + SRetrieveTableRsp *pRsp = NULL; + SCH_ERR_JRET(qExecExplainEnd(pJob->explainCtx, &pRsp)); + if (pRsp) { + SCH_ERR_JRET(schProcessOnExplainDone(pJob, pTask, pRsp)); + } + return TSDB_CODE_SUCCESS; } @@ -1278,6 +1292,10 @@ int32_t schHandleReadyCallback(void *param, const SDataBuf *pMsg, int32_t code) return schHandleCallback(param, pMsg, TDMT_VND_RES_READY_RSP, code); } +int32_t schHandleExplainCallback(void *param, const SDataBuf *pMsg, int32_t code) { + return schHandleCallback(param, pMsg, TDMT_VND_EXPLAIN_RSP, code); +} + int32_t schHandleDropCallback(void *param, const SDataBuf *pMsg, int32_t code) { SSchTaskCallbackParam *pParam = (SSchTaskCallbackParam *)param; qDebug("QID:%" PRIx64 ",TID:%" PRIx64 " drop task rsp received, code:%x", pParam->queryId, pParam->taskId, code); @@ -1366,6 +1384,9 @@ int32_t schGetCallbackFp(int32_t msgType, __async_send_cb_fn_t *fp) { case TDMT_VND_RES_READY: *fp = schHandleReadyCallback; break; + case TDMT_VND_EXPLAIN: + *fp = schHandleExplainCallback; + break; case TDMT_VND_FETCH: *fp = schHandleFetchCallback; break; @@ -1386,6 +1407,43 @@ int32_t schGetCallbackFp(int32_t msgType, __async_send_cb_fn_t *fp) { return TSDB_CODE_SUCCESS; } +int32_t schGenerateTaskCallBackAHandle(SSchJob *pJob, SSchTask *pTask, int32_t msgType, SMsgSendInfo **pMsgSendInfo) { + int32_t code = 0; + SMsgSendInfo *msgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo)); + if (NULL == msgSendInfo) { + SCH_TASK_ELOG("calloc %d failed", (int32_t)sizeof(SMsgSendInfo)); + SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); + } + + SSchTaskCallbackParam *param = taosMemoryCalloc(1, sizeof(SSchTaskCallbackParam)); + if (NULL == param) { + SCH_TASK_ELOG("calloc %d failed", (int32_t)sizeof(SSchTaskCallbackParam)); + SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); + } + + __async_send_cb_fn_t fp = NULL; + SCH_ERR_JRET(schGetCallbackFp(msgType, &fp)); + + param->queryId = pJob->queryId; + param->refId = pJob->refId; + param->taskId = SCH_TASK_ID(pTask); + param->transport = pJob->transport; + + msgSendInfo->param = param; + msgSendInfo->fp = fp; + + *pMsgSendInfo = msgSendInfo; + + return TSDB_CODE_SUCCESS; + +_return: + + taosMemoryFree(param); + taosMemoryFree(msgSendInfo); + + SCH_RET(code); +} + void schFreeRpcCtxVal(const void *arg) { if (NULL == arg) { return; @@ -1472,8 +1530,8 @@ _return: int32_t schMakeQueryRpcCtx(SSchJob *pJob, SSchTask *pTask, SRpcCtx *pCtx) { int32_t code = 0; - SSchTaskCallbackParam *param = NULL; - SMsgSendInfo *pMsgSendInfo = NULL; + SMsgSendInfo *pReadyMsgSendInfo = NULL; + SMsgSendInfo *pExplainMsgSendInfo = NULL; pCtx->args = taosHashInit(1, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK); if (NULL == pCtx->args) { @@ -1481,31 +1539,18 @@ int32_t schMakeQueryRpcCtx(SSchJob *pJob, SSchTask *pTask, SRpcCtx *pCtx) { SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); } - pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo)); - if (NULL == pMsgSendInfo) { - SCH_TASK_ELOG("calloc %d failed", (int32_t)sizeof(SMsgSendInfo)); + SCH_ERR_JRET(schGenerateTaskCallBackAHandle(pJob, pTask, TDMT_VND_RES_READY, &pReadyMsgSendInfo)); + SCH_ERR_JRET(schGenerateTaskCallBackAHandle(pJob, pTask, TDMT_VND_EXPLAIN, &pExplainMsgSendInfo)); + + int32_t msgType = TDMT_VND_RES_READY_RSP; + SRpcCtxVal ctxVal = {.val = pReadyMsgSendInfo, .clone = schCloneSMsgSendInfo, .freeFunc = schFreeRpcCtxVal}; + if (taosHashPut(pCtx->args, &msgType, sizeof(msgType), &ctxVal, sizeof(ctxVal))) { + SCH_TASK_ELOG("taosHashPut msg %d to rpcCtx failed", msgType); SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); } - param = taosMemoryCalloc(1, sizeof(SSchTaskCallbackParam)); - if (NULL == param) { - SCH_TASK_ELOG("calloc %d failed", (int32_t)sizeof(SSchTaskCallbackParam)); - SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); - } - - int32_t msgType = TDMT_VND_RES_READY_RSP; - __async_send_cb_fn_t fp = NULL; - SCH_ERR_JRET(schGetCallbackFp(TDMT_VND_RES_READY, &fp)); - - param->queryId = pJob->queryId; - param->refId = pJob->refId; - param->taskId = SCH_TASK_ID(pTask); - param->transport = pJob->transport; - - pMsgSendInfo->param = param; - pMsgSendInfo->fp = fp; - - SRpcCtxVal ctxVal = {.val = pMsgSendInfo, .clone = schCloneSMsgSendInfo, .freeFunc = schFreeRpcCtxVal}; + msgType = TDMT_VND_EXPLAIN_RSP; + ctxVal.val = pExplainMsgSendInfo; if (taosHashPut(pCtx->args, &msgType, sizeof(msgType), &ctxVal, sizeof(ctxVal))) { SCH_TASK_ELOG("taosHashPut msg %d to rpcCtx failed", msgType); SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); @@ -1518,8 +1563,16 @@ int32_t schMakeQueryRpcCtx(SSchJob *pJob, SSchTask *pTask, SRpcCtx *pCtx) { _return: taosHashCleanup(pCtx->args); - taosMemoryFreeClear(param); - taosMemoryFreeClear(pMsgSendInfo); + + if (pReadyMsgSendInfo) { + taosMemoryFreeClear(pReadyMsgSendInfo->param); + taosMemoryFreeClear(pReadyMsgSendInfo); + } + + if (pExplainMsgSendInfo) { + taosMemoryFreeClear(pExplainMsgSendInfo->param); + taosMemoryFreeClear(pExplainMsgSendInfo); + } SCH_RET(code); } @@ -1702,32 +1755,13 @@ int32_t schAsyncSendMsg(SSchJob *pJob, SSchTask *pTask, void *transport, SEpSet SSchTrans *trans = (SSchTrans *)transport; - SMsgSendInfo *pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo)); - if (NULL == pMsgSendInfo) { - SCH_TASK_ELOG("calloc %d failed", (int32_t)sizeof(SMsgSendInfo)); - SCH_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); - } + SMsgSendInfo *pMsgSendInfo = NULL; + SCH_ERR_JRET(schGenerateTaskCallBackAHandle(pJob, pTask, msgType, &pMsgSendInfo)); - SSchTaskCallbackParam *param = taosMemoryCalloc(1, sizeof(SSchTaskCallbackParam)); - if (NULL == param) { - SCH_TASK_ELOG("calloc %d failed", (int32_t)sizeof(SSchTaskCallbackParam)); - SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); - } - - __async_send_cb_fn_t fp = NULL; - SCH_ERR_JRET(schGetCallbackFp(msgType, &fp)); - - param->queryId = pJob->queryId; - param->refId = pJob->refId; - param->taskId = SCH_TASK_ID(pTask); - param->transport = trans->transInst; - - pMsgSendInfo->param = param; pMsgSendInfo->msgInfo.pData = msg; pMsgSendInfo->msgInfo.len = msgSize; pMsgSendInfo->msgInfo.handle = trans->transHandle; pMsgSendInfo->msgType = msgType; - pMsgSendInfo->fp = fp; qDebug("start to send %s msg to node[%d,%s,%d], refId:%" PRIx64 "instance:%p, handle:%p", TMSG_INFO(msgType), ntohl(((SMsgHead *)msg)->vgId), epSet->eps[epSet->inUse].fqdn, epSet->eps[epSet->inUse].port, pJob->refId, @@ -1744,8 +1778,11 @@ int32_t schAsyncSendMsg(SSchJob *pJob, SSchTask *pTask, void *transport, SEpSet _return: - taosMemoryFreeClear(param); - taosMemoryFreeClear(pMsgSendInfo); + if (pMsgSendInfo) { + taosMemoryFreeClear(pMsgSendInfo->param); + taosMemoryFreeClear(pMsgSendInfo); + } + SCH_RET(code); } diff --git a/source/util/src/tlockfree.c b/source/util/src/tlockfree.c index 8edcca0c16..a755a67cc8 100644 --- a/source/util/src/tlockfree.c +++ b/source/util/src/tlockfree.c @@ -53,6 +53,21 @@ void taosWLockLatch(SRWLatch *pLatch) { } } +int32_t taosWTryLockLatch(SRWLatch *pLatch) { + SRWLatch oLatch, nLatch; + oLatch = atomic_load_32(pLatch); + if (oLatch) { + return -1; + } + + nLatch = oLatch | TD_RWLATCH_WRITE_FLAG; + if (atomic_val_compare_exchange_32(pLatch, oLatch, nLatch) == oLatch) { + return 0; + } + + return -1; +} + void taosWUnLockLatch(SRWLatch *pLatch) { atomic_store_32(pLatch, 0); } void taosRLockLatch(SRWLatch *pLatch) { From 4540f53b32cedfc3b15646913cdfa6bcd0ea2d44 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 6 Apr 2022 01:45:22 +0000 Subject: [PATCH 07/25] make feature/meta compile --- source/dnode/vnode/src/meta/metaTDBImpl.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c index 8e82cf1abc..a004a96ad0 100644 --- a/source/dnode/vnode/src/meta/metaTDBImpl.c +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -212,7 +212,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { pVal = pBuf = buf; metaEncodeTbInfo(&pBuf, pTbCfg); vLen = POINTER_DISTANCE(pBuf, buf); - ret = tdbDbInsert(pMetaDb->pTbDB, pKey, kLen, pVal, vLen); + ret = tdbDbInsert(pMetaDb->pTbDB, pKey, kLen, pVal, vLen, NULL); if (ret < 0) { return -1; } @@ -234,7 +234,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { pVal = pBuf = buf; metaEncodeSchemaEx(&pBuf, &schemaWrapper); vLen = POINTER_DISTANCE(pBuf, buf); - ret = tdbDbInsert(pMetaDb->pSchemaDB, pKey, kLen, pVal, vLen); + ret = tdbDbInsert(pMetaDb->pSchemaDB, pKey, kLen, pVal, vLen, NULL); if (ret < 0) { return -1; } @@ -248,7 +248,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { kLen = nameLen + 1 + sizeof(uid); pVal = NULL; vLen = 0; - ret = tdbDbInsert(pMetaDb->pNameIdx, pKey, kLen, pVal, vLen); + ret = tdbDbInsert(pMetaDb->pNameIdx, pKey, kLen, pVal, vLen, NULL); if (ret < 0) { return -1; } @@ -259,7 +259,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { kLen = sizeof(uid); pVal = NULL; vLen = 0; - ret = tdbDbInsert(pMetaDb->pStbIdx, pKey, kLen, pVal, vLen); + ret = tdbDbInsert(pMetaDb->pStbIdx, pKey, kLen, pVal, vLen, NULL); if (ret < 0) { return -1; } @@ -270,7 +270,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { kLen = sizeof(ctbIdxKey); pVal = NULL; vLen = 0; - ret = tdbDbInsert(pMetaDb->pCtbIdx, pKey, kLen, pVal, vLen); + ret = tdbDbInsert(pMetaDb->pCtbIdx, pKey, kLen, pVal, vLen, NULL); if (ret < 0) { return -1; } @@ -279,7 +279,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { kLen = sizeof(uid); pVal = NULL; vLen = 0; - ret = tdbDbInsert(pMetaDb->pNtbIdx, pKey, kLen, pVal, vLen); + ret = tdbDbInsert(pMetaDb->pNtbIdx, pKey, kLen, pVal, vLen, NULL); if (ret < 0) { return -1; } From 906b615c9a138501f0f8e683db86f840fb6b022e Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 6 Apr 2022 01:55:37 +0000 Subject: [PATCH 08/25] make TDB compile --- source/dnode/vnode/CMakeLists.txt | 2 +- source/dnode/vnode/src/meta/metaTDBImpl.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/dnode/vnode/CMakeLists.txt b/source/dnode/vnode/CMakeLists.txt index 56a8698d77..6b21f935c4 100644 --- a/source/dnode/vnode/CMakeLists.txt +++ b/source/dnode/vnode/CMakeLists.txt @@ -1,5 +1,5 @@ set(META_DB_IMPL_LIST "BDB" "TDB") -set(META_DB_IMPL "BDB" CACHE STRING "Use BDB as the default META implementation") +set(META_DB_IMPL "TDB" CACHE STRING "Use BDB as the default META implementation") set_property(CACHE META_DB_IMPL PROPERTY STRINGS ${META_DB_IMPL_LIST}) if(META_DB_IMPL IN_LIST META_DB_IMPL_LIST) diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c index a004a96ad0..cfbd83df5c 100644 --- a/source/dnode/vnode/src/meta/metaTDBImpl.c +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -226,10 +226,10 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { if (pTbCfg->type == META_SUPER_TABLE) { schemaWrapper.nCols = pTbCfg->stbCfg.nCols; - schemaWrapper.pSchema = pTbCfg->stbCfg.pSchema; + schemaWrapper.pSchemaEx = pTbCfg->stbCfg.pSchema; } else { schemaWrapper.nCols = pTbCfg->ntbCfg.nCols; - schemaWrapper.pSchema = pTbCfg->ntbCfg.pSchema; + schemaWrapper.pSchemaEx = pTbCfg->ntbCfg.pSchema; } pVal = pBuf = buf; metaEncodeSchemaEx(&pBuf, &schemaWrapper); @@ -349,7 +349,7 @@ STbCfg *metaGetTbInfoByName(SMeta *pMeta, char *tbname, tb_uid_t *uid) { } SSchemaWrapper *metaGetTableSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, bool isinline) { - return *metaGetTableSchemaImpl(pMeta, uid, sver, isinline, false); + return metaGetTableSchemaImpl(pMeta, uid, sver, isinline, false); } static SSchemaWrapper *metaGetTableSchemaImpl(SMeta *pMeta, tb_uid_t uid, int32_t sver, bool isinline, bool isGetEx) { From 317d4ff2eb8dd83c34d6d9fdb6ea6c52df181b5c Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 6 Apr 2022 02:06:59 +0000 Subject: [PATCH 09/25] make tdb work with meta --- source/dnode/vnode/src/meta/metaCommit.c | 21 --------------------- source/dnode/vnode/src/meta/metaTDBImpl.c | 7 +++++++ 2 files changed, 7 insertions(+), 21 deletions(-) delete mode 100644 source/dnode/vnode/src/meta/metaCommit.c diff --git a/source/dnode/vnode/src/meta/metaCommit.c b/source/dnode/vnode/src/meta/metaCommit.c deleted file mode 100644 index 805e4f3e3d..0000000000 --- a/source/dnode/vnode/src/meta/metaCommit.c +++ /dev/null @@ -1,21 +0,0 @@ -/* - * 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 "meta.h" - -int metaCommit(SMeta *pMeta) { - // TODO - return 0; -} \ No newline at end of file diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c index cfbd83df5c..e934caa7e3 100644 --- a/source/dnode/vnode/src/meta/metaTDBImpl.c +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -167,6 +167,8 @@ int metaOpenDB(SMeta *pMeta) { return -1; } + tdbBegin(pMetaDb->pEnv, NULL); + pMeta->pDB = pMetaDb; return 0; } @@ -701,3 +703,8 @@ static void *metaDecodeTbInfo(void *buf, STbCfg *pTbCfg) { } return buf; } + +int metaCommit(SMeta *pMeta) { + tdbCommit(pMeta->pDB->pEnv, NULL); + return 0; +} \ No newline at end of file From 2c6dec89d2215a04b2ce605cc701258bc7d9fcdc Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Wed, 6 Apr 2022 11:32:21 +0800 Subject: [PATCH 10/25] feature/qnode --- include/common/tmsg.h | 1 + include/libs/scalar/filter.h | 2 +- source/libs/command/inc/commandInt.h | 1 + source/libs/command/src/explain.c | 57 +++++++++++++++++-- source/libs/executor/inc/executorimpl.h | 44 +++++++------- source/libs/executor/src/executorimpl.c | 24 ++++++-- source/libs/scalar/src/filter.c | 3 +- .../libs/scalar/test/filter/filterTests.cpp | 22 +++---- source/libs/scheduler/src/scheduler.c | 3 + 9 files changed, 114 insertions(+), 43 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 27b9a11625..9e04b5a618 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -919,6 +919,7 @@ typedef struct SExplainExecInfo { uint64_t startupCost; uint64_t totalCost; uint64_t numOfRows; + void *verboseInfo; } SExplainExecInfo; typedef struct { diff --git a/include/libs/scalar/filter.h b/include/libs/scalar/filter.h index a93180800e..c81cb49b64 100644 --- a/include/libs/scalar/filter.h +++ b/include/libs/scalar/filter.h @@ -40,7 +40,7 @@ extern int32_t filterInitFromNode(SNode *pNode, SFilterInfo **pinfo, uint32_t op extern bool filterExecute(SFilterInfo *info, SSDataBlock *pSrc, int8_t** p, SColumnDataAgg *statis, int16_t numOfCols); extern int32_t filterSetDataFromSlotId(SFilterInfo *info, void *param); extern int32_t filterSetDataFromColId(SFilterInfo *info, void *param); -extern int32_t filterGetTimeRange(SFilterInfo *info, STimeWindow *win); +extern int32_t filterGetTimeRange(SNode *pNode, STimeWindow *win, bool *isStrict); extern int32_t filterConverNcharColumns(SFilterInfo* pFilterInfo, int32_t rows, bool *gotNchar); extern int32_t filterFreeNcharColumns(SFilterInfo* pFilterInfo); extern void filterFreeInfo(SFilterInfo *info); diff --git a/source/libs/command/inc/commandInt.h b/source/libs/command/inc/commandInt.h index 7e9ea12ea4..23e4f2b24f 100644 --- a/source/libs/command/inc/commandInt.h +++ b/source/libs/command/inc/commandInt.h @@ -58,6 +58,7 @@ extern "C" { #define EXPLAIN_LOOPS_FORMAT "loops=%d" #define EXPLAIN_REVERSE_FORMAT "reverse=%d" #define EXPLAIN_FUNCTIONS_FORMAT "functions=%d" +#define EXPLAIN_EXECINFO_FORMAT "cost=%" PRIu64 "..%" PRIu64 " rows=%" PRIu64 typedef struct SExplainGroup { int32_t nodeNum; diff --git a/source/libs/command/src/explain.c b/source/libs/command/src/explain.c index c8502388ba..2a36113749 100644 --- a/source/libs/command/src/explain.c +++ b/source/libs/command/src/explain.c @@ -228,7 +228,10 @@ int32_t qExplainGenerateResNode(SPhysiNode *pNode, SExplainGroup *group, SExplai int32_t code = 0; resNode->pNode = pNode; - QRY_ERR_JRET(qExplainGenerateResNodeExecInfo(&resNode->pExecInfo, group)); + + if (group->nodeExecInfo) { + QRY_ERR_JRET(qExplainGenerateResNodeExecInfo(&resNode->pExecInfo, group)); + } QRY_ERR_JRET(qExplainGenerateResChildren(pNode, group, &resNode->pChildren)); @@ -247,14 +250,52 @@ _return: int32_t qExplainBufAppendExecInfo(SArray *pExecInfo, char *tbuf, int32_t *len) { int32_t tlen = *len; + int32_t nodeNum = taosArrayGetSize(pExecInfo); + SExplainExecInfo maxExecInfo = {0}; - EXPLAIN_ROW_APPEND("(exec info here)"); + for (int32_t i = 0; i < nodeNum; ++i) { + SExplainExecInfo *execInfo = taosArrayGet(pExecInfo, i); + if (execInfo->startupCost > maxExecInfo.startupCost) { + maxExecInfo.startupCost = execInfo->startupCost; + } + if (execInfo->totalCost > maxExecInfo.totalCost) { + maxExecInfo.totalCost = execInfo->totalCost; + } + if (execInfo->numOfRows > maxExecInfo.numOfRows) { + maxExecInfo.numOfRows = execInfo->numOfRows; + } + } + EXPLAIN_ROW_APPEND(EXPLAIN_EXECINFO_FORMAT, maxExecInfo.startupCost, maxExecInfo.totalCost, maxExecInfo.numOfRows); + *len = tlen; return TSDB_CODE_SUCCESS; } +int32_t qExplainBufAppendVerboseExecInfo(SArray *pExecInfo, char *tbuf, int32_t *len) { + int32_t tlen = 0; + bool gotVerbose = false; + int32_t nodeNum = taosArrayGetSize(pExecInfo); + SExplainExecInfo maxExecInfo = {0}; + + for (int32_t i = 0; i < nodeNum; ++i) { + SExplainExecInfo *execInfo = taosArrayGet(pExecInfo, i); + if (execInfo->verboseInfo) { + gotVerbose = true; + } + } + + if (gotVerbose) { + EXPLAIN_ROW_APPEND("exec verbose info"); + } + + *len = tlen; + + return TSDB_CODE_SUCCESS; +} + + int32_t qExplainResAppendRow(SExplainCtx *ctx, char *tbuf, int32_t len, int32_t level) { SQueryExplainRowInfo row = {0}; row.buf = taosMemoryMalloc(len); @@ -322,6 +363,14 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i EXPLAIN_ROW_NEW(level + 1, EXPLAIN_ORDER_FORMAT, EXPLAIN_ORDER_STRING(pTagScanNode->order)); EXPLAIN_ROW_END(); QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + + if (pResNode->pExecInfo) { + QRY_ERR_RET(qExplainBufAppendVerboseExecInfo(pResNode->pExecInfo, tbuf, &tlen)); + if (tlen) { + EXPLAIN_ROW_END(); + QRY_ERR_RET(qExplainResAppendRow(ctx, tbuf, tlen, level + 1)); + } + } } break; } @@ -532,8 +581,8 @@ int32_t qExplainResNodeToRowsImpl(SExplainResNode *pResNode, SExplainCtx *ctx, i EXPLAIN_ROW_APPEND(EXPLAIN_LEFT_PARENTHESIS_FORMAT); if (pResNode->pExecInfo) { QRY_ERR_RET(qExplainBufAppendExecInfo(pResNode->pExecInfo, tbuf, &tlen)); + EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); } - EXPLAIN_ROW_APPEND(EXPLAIN_BLANK_FORMAT); EXPLAIN_ROW_APPEND(EXPLAIN_WIDTH_FORMAT, pExchNode->node.pOutputDataBlockDesc->totalRowSize); EXPLAIN_ROW_APPEND(EXPLAIN_RIGHT_PARENTHESIS_FORMAT); EXPLAIN_ROW_END(); @@ -710,7 +759,7 @@ int32_t qExplainAppendGroupResRows(void *pCtx, int32_t groupId, int32_t level) { QRY_ERR_RET(qExplainGenerateResNode(group->plan->pNode, group, &node)); - if (group->physiPlanNum != group->physiPlanExecNum) { + if ((EXPLAIN_MODE_ANALYZE == ctx->mode) && (group->physiPlanNum != group->physiPlanExecNum)) { qError("physiPlanNum %d mismatch with physiExecNum %d in group %d", group->physiPlanNum, group->physiPlanExecNum, groupId); QRY_ERR_JRET(TSDB_CODE_QRY_APP_ERROR); } diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index f73ef1af38..2f667582e5 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -237,6 +237,7 @@ typedef bool (*__optr_decode_fn_t)(struct SOperatorInfo* pOperator, char *result typedef int32_t (*__optr_open_fn_t)(struct SOperatorInfo* pOptr); typedef SSDataBlock* (*__optr_fn_t)(struct SOperatorInfo* pOptr, bool* newgroup); typedef void (*__optr_close_fn_t)(void* param, int32_t num); +typedef int32_t (*__optr_get_explain_fn_t)(struct SOperatorInfo* pOptr, void **pOptrExplain); typedef struct STaskIdInfo { uint64_t queryId; // this is also a request id @@ -305,26 +306,27 @@ enum { }; typedef struct SOperatorInfo { - uint8_t operatorType; - bool blockingOptr; // block operator or not - uint8_t status; // denote if current operator is completed - int32_t numOfOutput; // number of columns of the current operator results - char* name; // name, used to show the query execution plan - void* info; // extension attribution - SExprInfo* pExpr; - STaskRuntimeEnv* pRuntimeEnv; // todo remove it - SExecTaskInfo* pTaskInfo; - SOperatorCostInfo cost; - SResultInfo resultInfo; - struct SOperatorInfo** pDownstream; // downstram pointer list - int32_t numOfDownstream; // number of downstream. The value is always ONE expect for join operator - __optr_open_fn_t _openFn; // DO NOT invoke this function directly - __optr_fn_t getNextFn; - __optr_fn_t getStreamResFn; // execute the aggregate in the stream model. - __optr_fn_t cleanupFn; // call this function to release the allocated resources ASAP - __optr_close_fn_t closeFn; - __optr_encode_fn_t encodeResultRow; - __optr_decode_fn_t decodeResultRow; + uint8_t operatorType; + bool blockingOptr; // block operator or not + uint8_t status; // denote if current operator is completed + int32_t numOfOutput; // number of columns of the current operator results + char* name; // name, used to show the query execution plan + void* info; // extension attribution + SExprInfo* pExpr; + STaskRuntimeEnv* pRuntimeEnv; // todo remove it + SExecTaskInfo* pTaskInfo; + SOperatorCostInfo cost; + SResultInfo resultInfo; + struct SOperatorInfo** pDownstream; // downstram pointer list + int32_t numOfDownstream; // number of downstream. The value is always ONE expect for join operator + __optr_open_fn_t _openFn; // DO NOT invoke this function directly + __optr_fn_t getNextFn; + __optr_fn_t getStreamResFn; // execute the aggregate in the stream model. + __optr_fn_t cleanupFn; // call this function to release the allocated resources ASAP + __optr_close_fn_t closeFn; + __optr_encode_fn_t encodeResultRow; + __optr_decode_fn_t decodeResultRow; + __optr_get_explain_fn_t getExplainFn; } SOperatorInfo; typedef struct { @@ -715,7 +717,7 @@ int32_t getMaximumIdleDurationSec(); void doInvokeUdf(struct SUdfInfo* pUdfInfo, SqlFunctionCtx* pCtx, int32_t idx, int32_t type); void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status); int32_t createExecTaskInfoImpl(SSubplan* pPlan, SExecTaskInfo** pTaskInfo, SReadHandle* pHandle, uint64_t taskId, EOPTR_EXEC_MODEL model); -int32_t getOperatorExplainExecInfo(SOperatorInfo *operator, SExplainExecInfo **pRes, int32_t *capacity, int32_t *resNum); +int32_t getOperatorExplainExecInfo(SOperatorInfo *operatorInfo, SExplainExecInfo **pRes, int32_t *capacity, int32_t *resNum); #ifdef __cplusplus } diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 498426d1a4..729d9efd86 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -5585,6 +5585,10 @@ SOperatorInfo* createTableScanOperatorInfo(void* pTsdbReadHandle, int32_t order, pOperator->getNextFn = doTableScan; pOperator->pTaskInfo = pTaskInfo; + //pOperator->cost.openCost = 1314; + //pOperator->cost.totalCost = 2324; + //pOperator->resultInfo.totalRows = 3334; + return pOperator; } @@ -9517,7 +9521,7 @@ void releaseQueryBuf(size_t numOfTables) { atomic_add_fetch_64(&tsQueryBufferSizeBytes, t); } -int32_t getOperatorExplainExecInfo(SOperatorInfo *operator, SExplainExecInfo **pRes, int32_t *capacity, int32_t *resNum) { +int32_t getOperatorExplainExecInfo(SOperatorInfo *operatorInfo, SExplainExecInfo **pRes, int32_t *capacity, int32_t *resNum) { if (*resNum >= *capacity) { *capacity += 10; @@ -9528,15 +9532,23 @@ int32_t getOperatorExplainExecInfo(SOperatorInfo *operator, SExplainExecInfo **p } } - (*pRes)[*resNum].numOfRows = operator->resultInfo.totalRows; - (*pRes)[*resNum].startupCost = operator->cost.openCost; - (*pRes)[*resNum].totalCost = operator->cost.totalCost; + (*pRes)[*resNum].numOfRows = operatorInfo->resultInfo.totalRows; + (*pRes)[*resNum].startupCost = operatorInfo->cost.openCost; + (*pRes)[*resNum].totalCost = operatorInfo->cost.totalCost; + + if (operatorInfo->getExplainFn) { + int32_t code = (*operatorInfo->getExplainFn)(operatorInfo, &(*pRes)->verboseInfo); + if (code) { + qError("operator getExplainFn failed, error:%s", tstrerror(code)); + return code; + } + } ++(*resNum); int32_t code = 0; - for (int32_t i = 0; i < operator->numOfDownstream; ++i) { - code = getOperatorExplainExecInfo(operator->pDownstream[i], pRes, capacity, resNum); + for (int32_t i = 0; i < operatorInfo->numOfDownstream; ++i) { + code = getOperatorExplainExecInfo(operatorInfo->pDownstream[i], pRes, capacity, resNum); if (code) { taosMemoryFreeClear(*pRes); return TSDB_CODE_QRY_OUT_OF_MEMORY; diff --git a/source/libs/scalar/src/filter.c b/source/libs/scalar/src/filter.c index b0632bbc34..0502774d1b 100644 --- a/source/libs/scalar/src/filter.c +++ b/source/libs/scalar/src/filter.c @@ -3314,7 +3314,8 @@ bool filterRangeExecute(SFilterInfo *info, SColumnDataAgg *pDataStatis, int32_t -int32_t filterGetTimeRange(SFilterInfo *info, STimeWindow *win) { +int32_t filterGetTimeRange(SNode *pNode, STimeWindow *win, bool *isStrict) { + SFilterInfo *info = NULL; SFilterRange ra = {0}; SFilterRangeCtx *prev = filterInitRangeCtx(TSDB_DATA_TYPE_TIMESTAMP, FLT_OPTION_TIMESTAMP); SFilterRangeCtx *tmpc = filterInitRangeCtx(TSDB_DATA_TYPE_TIMESTAMP, FLT_OPTION_TIMESTAMP); diff --git a/source/libs/scalar/test/filter/filterTests.cpp b/source/libs/scalar/test/filter/filterTests.cpp index 54f82eae2d..696a697a91 100644 --- a/source/libs/scalar/test/filter/filterTests.cpp +++ b/source/libs/scalar/test/filter/filterTests.cpp @@ -234,15 +234,16 @@ TEST(timerangeTest, greater) { flttMakeValueNode(&pval, TSDB_DATA_TYPE_TIMESTAMP, &tsmall); flttMakeOpNode(&opNode1, OP_TYPE_GREATER_THAN, TSDB_DATA_TYPE_BOOL, pcol, pval); - SFilterInfo *filter = NULL; - int32_t code = filterInitFromNode(opNode1, &filter, FLT_OPTION_NO_REWRITE|FLT_OPTION_TIMESTAMP); - ASSERT_EQ(code, 0); + //SFilterInfo *filter = NULL; + //int32_t code = filterInitFromNode(opNode1, &filter, FLT_OPTION_NO_REWRITE|FLT_OPTION_TIMESTAMP); + //ASSERT_EQ(code, 0); STimeWindow win = {0}; - code = filterGetTimeRange(filter, &win); + bool isStrict = false; + int32_t code = filterGetTimeRange(opNode1, &win, &isStrict); ASSERT_EQ(code, 0); ASSERT_EQ(win.skey, tsmall); ASSERT_EQ(win.ekey, INT64_MAX); - filterFreeInfo(filter); + //filterFreeInfo(filter); nodesDestroyNode(opNode1); } @@ -263,15 +264,16 @@ TEST(timerangeTest, greater_and_lower) { flttMakeLogicNode(&logicNode, LOGIC_COND_TYPE_AND, list, 2); - SFilterInfo *filter = NULL; - int32_t code = filterInitFromNode(logicNode, &filter, FLT_OPTION_NO_REWRITE|FLT_OPTION_TIMESTAMP); - ASSERT_EQ(code, 0); + //SFilterInfo *filter = NULL; + //int32_t code = filterInitFromNode(logicNode, &filter, FLT_OPTION_NO_REWRITE|FLT_OPTION_TIMESTAMP); + //ASSERT_EQ(code, 0); STimeWindow win = {0}; - code = filterGetTimeRange(filter, &win); + bool isStrict = false; + int32_t code = filterGetTimeRange(logicNode, &win, &isStrict); ASSERT_EQ(code, 0); ASSERT_EQ(win.skey, tsmall); ASSERT_EQ(win.ekey, tbig); - filterFreeInfo(filter); + //filterFreeInfo(filter); nodesDestroyNode(logicNode); } diff --git a/source/libs/scheduler/src/scheduler.c b/source/libs/scheduler/src/scheduler.c index 55eaae1b54..1675f91330 100644 --- a/source/libs/scheduler/src/scheduler.c +++ b/source/libs/scheduler/src/scheduler.c @@ -199,6 +199,7 @@ int32_t schValidateTaskReceivedMsgType(SSchJob *pJob, SSchTask *pTask, int32_t m int32_t reqMsgType = msgType - 1; switch (msgType) { case TDMT_SCH_LINK_BROKEN: + case TDMT_VND_EXPLAIN_RSP: return TSDB_CODE_SUCCESS; case TDMT_VND_QUERY_RSP: // query_rsp may be processed later than ready_rsp if (lastMsgType != reqMsgType && -1 != lastMsgType && TDMT_VND_FETCH != lastMsgType) { @@ -1187,6 +1188,8 @@ int32_t schHandleResponseMsg(SSchJob *pJob, SSchTask *pTask, int32_t msgType, ch return TSDB_CODE_SUCCESS; } + atomic_val_compare_exchange_32(&pJob->remoteFetch, 1, 0); + SCH_ERR_JRET(schFetchFromRemote(pJob)); return TSDB_CODE_SUCCESS; From 69ff3cc67b4980bb96aa97852b03f3d1eeb3ca09 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 6 Apr 2022 06:37:22 +0000 Subject: [PATCH 11/25] fix an assert bug --- source/libs/tdb/src/db/tdbPage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/libs/tdb/src/db/tdbPage.c b/source/libs/tdb/src/db/tdbPage.c index cc4ce20802..ea75f7fbeb 100644 --- a/source/libs/tdb/src/db/tdbPage.c +++ b/source/libs/tdb/src/db/tdbPage.c @@ -278,7 +278,7 @@ static int tdbPageAllocate(SPage *pPage, int szCell, SCell **ppCell) { // 2. Try to allocate from the page free list cellFree = TDB_PAGE_FCELL(pPage); - ASSERT(cellFree == 0 || cellFree > pPage->pFreeEnd - pPage->pData); + ASSERT(cellFree == 0 || cellFree >= pPage->pFreeEnd - pPage->pData); if (cellFree && pPage->pFreeEnd - pPage->pFreeStart >= TDB_PAGE_OFFSET_SIZE(pPage)) { SCell *pPrevFreeCell = NULL; int szPrevFreeCell; From 61d0180b852f93333d086cba388e33c0918675f9 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 6 Apr 2022 06:37:37 +0000 Subject: [PATCH 12/25] more TDB integration --- source/dnode/vnode/src/meta/metaTDBImpl.c | 125 +++++++++++++++++++--- source/dnode/vnode/src/meta/metaTbUid.c | 2 +- source/dnode/vnode/src/vnd/vnodeCommit.c | 2 +- 3 files changed, 112 insertions(+), 17 deletions(-) diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c index e934caa7e3..282e0a51f6 100644 --- a/source/dnode/vnode/src/meta/metaTDBImpl.c +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -16,15 +16,28 @@ #include "metaDef.h" #include "tdbInt.h" +typedef struct SPoolMem { + int64_t size; + struct SPoolMem *prev; + struct SPoolMem *next; +} SPoolMem; + +static SPoolMem *openPool(); +static void clearPool(SPoolMem *pPool); +static void closePool(SPoolMem *pPool); +static void *poolMalloc(void *arg, size_t size); +static void poolFree(void *arg, void *ptr); struct SMetaDB { - TENV *pEnv; - TDB *pTbDB; - TDB *pSchemaDB; - TDB *pNameIdx; - TDB *pStbIdx; - TDB *pNtbIdx; - TDB *pCtbIdx; + TXN txn; + TENV *pEnv; + TDB *pTbDB; + TDB *pSchemaDB; + TDB *pNameIdx; + TDB *pStbIdx; + TDB *pNtbIdx; + TDB *pCtbIdx; + SPoolMem *pPool; }; typedef struct __attribute__((__packed__)) { @@ -167,6 +180,8 @@ int metaOpenDB(SMeta *pMeta) { return -1; } + pMetaDb->pPool = openPool(); + tdbTxnOpen(&pMetaDb->txn, 0, poolMalloc, poolFree, pMetaDb->pPool, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED); tdbBegin(pMetaDb->pEnv, NULL); pMeta->pDB = pMetaDb; @@ -214,7 +229,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { pVal = pBuf = buf; metaEncodeTbInfo(&pBuf, pTbCfg); vLen = POINTER_DISTANCE(pBuf, buf); - ret = tdbDbInsert(pMetaDb->pTbDB, pKey, kLen, pVal, vLen, NULL); + ret = tdbDbInsert(pMetaDb->pTbDB, pKey, kLen, pVal, vLen, &pMetaDb->txn); if (ret < 0) { return -1; } @@ -236,7 +251,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { pVal = pBuf = buf; metaEncodeSchemaEx(&pBuf, &schemaWrapper); vLen = POINTER_DISTANCE(pBuf, buf); - ret = tdbDbInsert(pMetaDb->pSchemaDB, pKey, kLen, pVal, vLen, NULL); + ret = tdbDbInsert(pMetaDb->pSchemaDB, pKey, kLen, pVal, vLen, &pMeta->pDB->txn); if (ret < 0) { return -1; } @@ -250,7 +265,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { kLen = nameLen + 1 + sizeof(uid); pVal = NULL; vLen = 0; - ret = tdbDbInsert(pMetaDb->pNameIdx, pKey, kLen, pVal, vLen, NULL); + ret = tdbDbInsert(pMetaDb->pNameIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn); if (ret < 0) { return -1; } @@ -261,7 +276,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { kLen = sizeof(uid); pVal = NULL; vLen = 0; - ret = tdbDbInsert(pMetaDb->pStbIdx, pKey, kLen, pVal, vLen, NULL); + ret = tdbDbInsert(pMetaDb->pStbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn); if (ret < 0) { return -1; } @@ -272,7 +287,7 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { kLen = sizeof(ctbIdxKey); pVal = NULL; vLen = 0; - ret = tdbDbInsert(pMetaDb->pCtbIdx, pKey, kLen, pVal, vLen, NULL); + ret = tdbDbInsert(pMetaDb->pCtbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn); if (ret < 0) { return -1; } @@ -281,12 +296,16 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { kLen = sizeof(uid); pVal = NULL; vLen = 0; - ret = tdbDbInsert(pMetaDb->pNtbIdx, pKey, kLen, pVal, vLen, NULL); + ret = tdbDbInsert(pMetaDb->pNtbIdx, pKey, kLen, pVal, vLen, &pMetaDb->txn); if (ret < 0) { return -1; } } + if (pMeta->pDB->pPool->size > 0) { + metaCommit(pMeta); + } + return 0; } @@ -705,6 +724,82 @@ static void *metaDecodeTbInfo(void *buf, STbCfg *pTbCfg) { } int metaCommit(SMeta *pMeta) { - tdbCommit(pMeta->pDB->pEnv, NULL); + TXN *pTxn = &pMeta->pDB->txn; + + // Commit current txn + tdbCommit(pMeta->pDB->pEnv, pTxn); + tdbTxnClose(pTxn); + clearPool(pMeta->pDB->pPool); + + // start a new txn + tdbTxnOpen(&pMeta->pDB->txn, 0, poolMalloc, poolFree, pMeta->pDB->pPool, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED); + tdbBegin(pMeta->pDB->pEnv, pTxn); return 0; -} \ No newline at end of file +} + +static SPoolMem *openPool() { + SPoolMem *pPool = (SPoolMem *)tdbOsMalloc(sizeof(*pPool)); + + pPool->prev = pPool->next = pPool; + pPool->size = 0; + + return pPool; +} + +static void clearPool(SPoolMem *pPool) { + SPoolMem *pMem; + + do { + pMem = pPool->next; + + if (pMem == pPool) break; + + pMem->next->prev = pMem->prev; + pMem->prev->next = pMem->next; + pPool->size -= pMem->size; + + tdbOsFree(pMem); + } while (1); + + assert(pPool->size == 0); +} + +static void closePool(SPoolMem *pPool) { + clearPool(pPool); + tdbOsFree(pPool); +} + +static void *poolMalloc(void *arg, size_t size) { + void *ptr = NULL; + SPoolMem *pPool = (SPoolMem *)arg; + SPoolMem *pMem; + + pMem = (SPoolMem *)tdbOsMalloc(sizeof(*pMem) + size); + if (pMem == NULL) { + assert(0); + } + + pMem->size = sizeof(*pMem) + size; + pMem->next = pPool->next; + pMem->prev = pPool; + + pPool->next->prev = pMem; + pPool->next = pMem; + pPool->size += pMem->size; + + ptr = (void *)(&pMem[1]); + return ptr; +} + +static void poolFree(void *arg, void *ptr) { + SPoolMem *pPool = (SPoolMem *)arg; + SPoolMem *pMem; + + pMem = &(((SPoolMem *)ptr)[-1]); + + pMem->next->prev = pMem->prev; + pMem->prev->next = pMem->next; + pPool->size -= pMem->size; + + tdbOsFree(pMem); +} diff --git a/source/dnode/vnode/src/meta/metaTbUid.c b/source/dnode/vnode/src/meta/metaTbUid.c index 1f57d1396a..b488630024 100644 --- a/source/dnode/vnode/src/meta/metaTbUid.c +++ b/source/dnode/vnode/src/meta/metaTbUid.c @@ -27,5 +27,5 @@ void metaCloseUidGnrt(SMeta *pMeta) { /* TODO */ tb_uid_t metaGenerateUid(SMeta *pMeta) { // Generate a new table UID - return tGenIdPI32(); + return tGenIdPI64(); } diff --git a/source/dnode/vnode/src/vnd/vnodeCommit.c b/source/dnode/vnode/src/vnd/vnodeCommit.c index 696c5f39f6..1717721bb2 100644 --- a/source/dnode/vnode/src/vnd/vnodeCommit.c +++ b/source/dnode/vnode/src/vnd/vnodeCommit.c @@ -47,7 +47,7 @@ int vnodeSyncCommit(SVnode *pVnode) { static int vnodeCommit(void *arg) { SVnode *pVnode = (SVnode *)arg; - metaCommit(pVnode->pMeta); + // metaCommit(pVnode->pMeta); tqCommit(pVnode->pTq); tsdbCommit(pVnode->pTsdb); From 33bcee62c966c8339441b46980ae594ffb2132fb Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 6 Apr 2022 15:09:29 +0800 Subject: [PATCH 13/25] [td-14393] refactor. --- source/client/inc/clientInt.h | 1 + source/client/src/clientImpl.c | 1 + source/client/src/clientMain.c | 7 ++++++- source/client/test/clientTests.cpp | 4 ++-- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/source/client/inc/clientInt.h b/source/client/inc/clientInt.h index 0749ce9e7d..ae1b34a3bd 100644 --- a/source/client/inc/clientInt.h +++ b/source/client/inc/clientInt.h @@ -163,6 +163,7 @@ typedef struct SReqResultInfo { uint64_t totalRows; uint32_t current; bool completed; + int32_t precision; int32_t payloadLen; } SReqResultInfo; diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index 80559c025f..4a2687ed36 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -825,6 +825,7 @@ int32_t setQueryResultFromRsp(SReqResultInfo* pResultInfo, const SRetrieveTableR pResultInfo->current = 0; pResultInfo->completed = (pRsp->completed == 1); pResultInfo->payloadLen = htonl(pRsp->compLen); + pResultInfo->precision = pRsp->precision; // TODO handle the compressed case pResultInfo->totalRows += pResultInfo->numOfRows; diff --git a/source/client/src/clientMain.c b/source/client/src/clientMain.c index ee4fc7fcb5..b2c32a4366 100644 --- a/source/client/src/clientMain.c +++ b/source/client/src/clientMain.c @@ -323,7 +323,12 @@ int taos_affected_rows(TAOS_RES *res) { } int taos_result_precision(TAOS_RES *res) { - return TSDB_TIME_PRECISION_MILLI; + SRequestObj* pRequest = (SRequestObj*) res; + if (pRequest == NULL) { + return TSDB_TIME_PRECISION_MILLI; + } + + return pRequest->body.resInfo.precision; } int taos_select_db(TAOS *taos, const char *db) { diff --git a/source/client/test/clientTests.cpp b/source/client/test/clientTests.cpp index 629dd90da3..772f9049e5 100644 --- a/source/client/test/clientTests.cpp +++ b/source/client/test/clientTests.cpp @@ -400,6 +400,7 @@ TEST(testCase, show_vgroup_Test) { taos_free_result(pRes); taos_close(pConn); } +#endif TEST(testCase, create_multiple_tables) { TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0); @@ -458,7 +459,7 @@ TEST(testCase, create_multiple_tables) { taos_free_result(pRes); - for (int32_t i = 0; i < 20; ++i) { + for (int32_t i = 0; i < 25000; ++i) { char sql[512] = {0}; snprintf(sql, tListLen(sql), "create table t_x_%d using st1 tags(2) t_x_%d using st1 tags(5) t_x_%d using st1 tags(911)", i, @@ -652,7 +653,6 @@ TEST(testCase, projection_query_stables) { taos_free_result(pRes); taos_close(pConn); } -#endif TEST(testCase, agg_query_tables) { TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0); From 398a41d09bfdd62af9ba9e98fa7dec4e2927d463 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Wed, 6 Apr 2022 15:20:38 +0800 Subject: [PATCH 14/25] feature/qnode --- include/common/ttypes.h | 1 + include/common/tvariant.h | 1 + include/libs/nodes/querynodes.h | 2 ++ source/common/src/tvariant.c | 26 +++++++++++++- source/libs/executor/src/executorimpl.c | 22 +++++++++++- source/libs/nodes/src/nodesUtilFuncs.c | 41 +++++++++++++++++++++ source/libs/parser/src/parTranslater.c | 48 ++++--------------------- source/libs/scalar/inc/sclvector.h | 2 ++ source/libs/scalar/src/scalar.c | 3 +- source/libs/scalar/src/sclvector.c | 30 +++++++++++++++- 10 files changed, 130 insertions(+), 46 deletions(-) diff --git a/include/common/ttypes.h b/include/common/ttypes.h index 1cb398fb85..37d688a0ef 100644 --- a/include/common/ttypes.h +++ b/include/common/ttypes.h @@ -146,6 +146,7 @@ typedef struct { #define IS_FLOAT_TYPE(_t) ((_t) == TSDB_DATA_TYPE_FLOAT || (_t) == TSDB_DATA_TYPE_DOUBLE) #define IS_NUMERIC_TYPE(_t) ((IS_SIGNED_NUMERIC_TYPE(_t)) || (IS_UNSIGNED_NUMERIC_TYPE(_t)) || (IS_FLOAT_TYPE(_t))) +#define IS_MATHABLE_TYPE(_t) (IS_NUMERIC_TYPE(_t) || (_t) == (TSDB_DATA_TYPE_BOOL) || (_t) == (TSDB_DATA_TYPE_TIMESTAMP)) #define IS_VALID_TINYINT(_t) ((_t) > INT8_MIN && (_t) <= INT8_MAX) #define IS_VALID_SMALLINT(_t) ((_t) > INT16_MIN && (_t) <= INT16_MAX) diff --git a/include/common/tvariant.h b/include/common/tvariant.h index 63f305ab2d..83dccd0092 100644 --- a/include/common/tvariant.h +++ b/include/common/tvariant.h @@ -59,6 +59,7 @@ int32_t taosVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool #endif int32_t taosVariantTypeSetType(SVariant *pVariant, char type); +char * taosVariantGet(SVariant *pVar, int32_t type); #ifdef __cplusplus } diff --git a/include/libs/nodes/querynodes.h b/include/libs/nodes/querynodes.h index d035f7de8f..87aadfd244 100644 --- a/include/libs/nodes/querynodes.h +++ b/include/libs/nodes/querynodes.h @@ -22,6 +22,7 @@ extern "C" { #include "nodes.h" #include "tmsg.h" +#include "tvariant.h" #define TABLE_TOTAL_COL_NUM(pMeta) ((pMeta)->tableInfo.numOfColumns + (pMeta)->tableInfo.numOfTags) #define TABLE_META_SIZE(pMeta) (NULL == (pMeta) ? 0 : (sizeof(STableMeta) + TABLE_TOTAL_COL_NUM((pMeta)) * sizeof(SSchema))) @@ -317,6 +318,7 @@ bool nodesIsTimelineQuery(const SNode* pQuery); void* nodesGetValueFromNode(SValueNode *pNode); char* nodesGetStrValueFromNode(SValueNode *pNode); char *getFillModeString(EFillMode mode); +void valueNodeToVariant(const SValueNode* pNode, SVariant* pVal); #ifdef __cplusplus } diff --git a/source/common/src/tvariant.c b/source/common/src/tvariant.c index 3995db89b6..77feb04f6c 100644 --- a/source/common/src/tvariant.c +++ b/source/common/src/tvariant.c @@ -1014,4 +1014,28 @@ int32_t taosVariantTypeSetType(SVariant *pVariant, char type) { } return 0; -} \ No newline at end of file +} + +char * taosVariantGet(SVariant *pVar, int32_t type) { + switch (type) { + case TSDB_DATA_TYPE_BOOL: + case TSDB_DATA_TYPE_TINYINT: + case TSDB_DATA_TYPE_SMALLINT: + case TSDB_DATA_TYPE_BIGINT: + case TSDB_DATA_TYPE_INT: + case TSDB_DATA_TYPE_TIMESTAMP: + return (char *)&pVar->i; + case TSDB_DATA_TYPE_DOUBLE: + case TSDB_DATA_TYPE_FLOAT: + return (char *)&pVar->d; + case TSDB_DATA_TYPE_BINARY: + return (char *)pVar->pz; + case TSDB_DATA_TYPE_NCHAR: + return (char *)pVar->ucs4; + default: + return NULL; + } + + return NULL; +} + diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index a1642fc2c8..bb2432c07a 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -1289,6 +1289,12 @@ static void projectApplyFunctions(SExprInfo* pExpr, SSDataBlock* pResult, SSData taosArrayDestroy(pBlockList); + } else if (pExpr[k].pExpr->nodeType == QUERY_NODE_VALUE) { + SColumnInfoData* pColInfoData = taosArrayGet(pResult->pDataBlock, k); + for (int32_t i = 0; i < pSrcBlock->info.rows; ++i) { + colDataAppend(pColInfoData, i, taosVariantGet(&pExpr[k].base.pParam[0].param, pExpr[k].base.pParam[0].type), TSDB_DATA_TYPE_NULL == pExpr[k].base.pParam[0].param.nType); + } + pResult->info.rows = pSrcBlock->info.rows; } else { ASSERT(0); } @@ -2093,7 +2099,7 @@ static SqlFunctionCtx* createSqlFunctionCtx_rv(SExprInfo* pExprInfo, int32_t num } } pCtx->resDataInfo.interBufSize = env.calcMemSize; - } else if (pExpr->pExpr->nodeType == QUERY_NODE_COLUMN || pExpr->pExpr->nodeType == QUERY_NODE_OPERATOR) { + } else if (pExpr->pExpr->nodeType == QUERY_NODE_COLUMN || pExpr->pExpr->nodeType == QUERY_NODE_OPERATOR || pExpr->pExpr->nodeType == QUERY_NODE_VALUE) { pCtx->resDataInfo.interBufSize = pFunct->resSchema.bytes; // for simple column, the intermediate buffer needs to hold one element. } @@ -2864,6 +2870,9 @@ int32_t loadDataBlock(SExecTaskInfo* pTaskInfo, STableScanInfo* pTableScanInfo, if (pTableScanInfo->pFilterNode != NULL) { SFilterInfo* filter = NULL; int32_t code = filterInitFromNode((SNode*)pTableScanInfo->pFilterNode, &filter, 0); + if (code) { + return code; + } SFilterColumnParam param1 = {.numOfCols = pBlock->info.numOfCols, .pDataBlock = pBlock->pDataBlock}; code = filterSetDataFromSlotId(filter, ¶m1); @@ -8309,6 +8318,17 @@ SExprInfo* createExprInfo(SNodeList* pNodeList, SNodeList* pGroupKeys, int32_t* // pExp->base.pParam[0].type = FUNC_PARAM_TYPE_COLUMN; // pExp->base.pParam[0].pCol = createColumn(pTargetNode->dataBlockId, pTargetNode->slotId, pType); + } else if (nodeType(pTargetNode->pExpr) == QUERY_NODE_VALUE) { + pExp->pExpr->nodeType = QUERY_NODE_VALUE; + SValueNode* pValNode = (SValueNode*)pTargetNode->pExpr; + + pExp->base.pParam = taosMemoryCalloc(1, sizeof(SFunctParam)); + pExp->base.numOfParams = 1; + + SDataType* pType = &pValNode->node.resType; + pExp->base.resSchema = createResSchema(pType->type, pType->bytes, pTargetNode->slotId, pType->scale, pType->precision, pValNode->node.aliasName); + pExp->base.pParam[0].type = FUNC_PARAM_TYPE_VALUE; + valueNodeToVariant(pValNode, &pExp->base.pParam[0].param); } else { ASSERT(0); } diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index cfee083f46..6bf2c99d0d 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -1092,4 +1092,45 @@ int32_t nodesGetOutputNumFromSlotList(SNodeList* pSlots) { } +void valueNodeToVariant(const SValueNode* pNode, SVariant* pVal) { + pVal->nType = pNode->node.resType.type; + pVal->nLen = pNode->node.resType.bytes; + switch (pNode->node.resType.type) { + case TSDB_DATA_TYPE_NULL: + break; + case TSDB_DATA_TYPE_BOOL: + pVal->i = pNode->datum.b; + break; + case TSDB_DATA_TYPE_TINYINT: + case TSDB_DATA_TYPE_SMALLINT: + case TSDB_DATA_TYPE_INT: + case TSDB_DATA_TYPE_BIGINT: + case TSDB_DATA_TYPE_TIMESTAMP: + pVal->i = pNode->datum.i; + break; + case TSDB_DATA_TYPE_UTINYINT: + case TSDB_DATA_TYPE_USMALLINT: + case TSDB_DATA_TYPE_UINT: + case TSDB_DATA_TYPE_UBIGINT: + pVal->u = pNode->datum.u; + break; + case TSDB_DATA_TYPE_FLOAT: + case TSDB_DATA_TYPE_DOUBLE: + pVal->d = pNode->datum.d; + break; + case TSDB_DATA_TYPE_NCHAR: + case TSDB_DATA_TYPE_VARCHAR: + case TSDB_DATA_TYPE_VARBINARY: + pVal->pz = pNode->datum.p; + break; + case TSDB_DATA_TYPE_JSON: + case TSDB_DATA_TYPE_DECIMAL: + case TSDB_DATA_TYPE_BLOB: + // todo + default: + break; + } +} + + diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index bf909a0eaf..97fce407b6 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -438,8 +438,12 @@ static EDealRes translateValue(STranslateContext* pCxt, SValueNode* pVal) { static EDealRes translateOperator(STranslateContext* pCxt, SOperatorNode* pOp) { if (nodesIsUnaryOp(pOp)) { - if (OP_TYPE_MINUS == pOp->opType && !IS_NUMERIC_TYPE(((SExprNode*)(pOp->pLeft))->resType.type)) { - return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pLeft))->aliasName); + if (OP_TYPE_MINUS == pOp->opType) { + if (!IS_MATHABLE_TYPE(((SExprNode*)(pOp->pLeft))->resType.type)) { + return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pLeft))->aliasName); + } + pOp->node.resType.type = TSDB_DATA_TYPE_DOUBLE; + pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes; } return DEAL_RES_CONTINUE; } @@ -2338,46 +2342,6 @@ static void addCreateTbReqIntoVgroup(int32_t acctId, SHashObj* pVgroupHashmap, } } -static void valueNodeToVariant(const SValueNode* pNode, SVariant* pVal) { - pVal->nType = pNode->node.resType.type; - pVal->nLen = pNode->node.resType.bytes; - switch (pNode->node.resType.type) { - case TSDB_DATA_TYPE_NULL: - break; - case TSDB_DATA_TYPE_BOOL: - pVal->i = pNode->datum.b; - break; - case TSDB_DATA_TYPE_TINYINT: - case TSDB_DATA_TYPE_SMALLINT: - case TSDB_DATA_TYPE_INT: - case TSDB_DATA_TYPE_BIGINT: - case TSDB_DATA_TYPE_TIMESTAMP: - pVal->i = pNode->datum.i; - break; - case TSDB_DATA_TYPE_UTINYINT: - case TSDB_DATA_TYPE_USMALLINT: - case TSDB_DATA_TYPE_UINT: - case TSDB_DATA_TYPE_UBIGINT: - pVal->u = pNode->datum.u; - break; - case TSDB_DATA_TYPE_FLOAT: - case TSDB_DATA_TYPE_DOUBLE: - pVal->d = pNode->datum.d; - break; - case TSDB_DATA_TYPE_NCHAR: - case TSDB_DATA_TYPE_VARCHAR: - case TSDB_DATA_TYPE_VARBINARY: - pVal->pz = pNode->datum.p; - break; - case TSDB_DATA_TYPE_JSON: - case TSDB_DATA_TYPE_DECIMAL: - case TSDB_DATA_TYPE_BLOB: - // todo - default: - break; - } -} - static int32_t addValToKVRow(STranslateContext* pCxt, SValueNode* pVal, const SSchema* pSchema, SKVRowBuilder* pBuilder) { if (DEAL_RES_ERROR == translateValue(pCxt, pVal)) { return pCxt->errCode; diff --git a/source/libs/scalar/inc/sclvector.h b/source/libs/scalar/inc/sclvector.h index 3f41ad875c..e51115e9c0 100644 --- a/source/libs/scalar/inc/sclvector.h +++ b/source/libs/scalar/inc/sclvector.h @@ -75,6 +75,8 @@ static FORCE_INLINE _getDoubleValue_fn_t getVectorDoubleValueFn(int32_t srcType) p = getVectorDoubleValue_FLOAT; } else if (srcType == TSDB_DATA_TYPE_DOUBLE) { p = getVectorDoubleValue_DOUBLE; + } else if (srcType == TSDB_DATA_TYPE_TIMESTAMP) { + p = getVectorDoubleValue_BIGINT; } else { assert(0); } diff --git a/source/libs/scalar/src/scalar.c b/source/libs/scalar/src/scalar.c index d77ada3b4e..07358f905f 100644 --- a/source/libs/scalar/src/scalar.c +++ b/source/libs/scalar/src/scalar.c @@ -10,7 +10,8 @@ int32_t scalarGetOperatorParamNum(EOperatorType type) { if (OP_TYPE_IS_NULL == type || OP_TYPE_IS_NOT_NULL == type || OP_TYPE_IS_TRUE == type || OP_TYPE_IS_NOT_TRUE == type - || OP_TYPE_IS_FALSE == type || OP_TYPE_IS_NOT_FALSE == type || OP_TYPE_IS_UNKNOWN == type || OP_TYPE_IS_NOT_UNKNOWN == type) { + || OP_TYPE_IS_FALSE == type || OP_TYPE_IS_NOT_FALSE == type || OP_TYPE_IS_UNKNOWN == type || OP_TYPE_IS_NOT_UNKNOWN == type + || OP_TYPE_MINUS == type) { return 1; } diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index ef3be80893..c5f18fb1a9 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -773,6 +773,32 @@ void vectorMathRemainder(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam doReleaseVec(pRightCol, rightConvert); } +void vectorMathMinus(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) { + SColumnInfoData *pOutputCol = pOut->columnData; + + pOut->numOfRows = pLeft->numOfRows; + + int32_t i = ((_ord) == TSDB_ORDER_ASC)? 0 : (pLeft->numOfRows - 1); + int32_t step = ((_ord) == TSDB_ORDER_ASC)? 1 : -1; + + int32_t leftConvert = 0; + SColumnInfoData *pLeftCol = doVectorConvert(pLeft, &leftConvert); + + _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type); + + double *output = (double *)pOutputCol->pData; + for (; i < pLeft->numOfRows && i >= 0; i += step, output += 1) { + *output = - getVectorDoubleValueFnLeft(pLeftCol->pData, i); + } + + pOutputCol->hasNull = pLeftCol->hasNull; + if (pOutputCol->hasNull) { + memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(pLeft->numOfRows)); + } + + doReleaseVec(pLeftCol, leftConvert); +} + void vectorConcat(SScalarParam* pLeft, SScalarParam* pRight, void *out, int32_t _ord) { #if 0 int32_t len = pLeft->bytes + pRight->bytes; @@ -1102,6 +1128,8 @@ _bin_scalar_fn_t getBinScalarOperatorFn(int32_t binFunctionId) { return vectorMathDivide; case OP_TYPE_MOD: return vectorMathRemainder; + case OP_TYPE_MINUS: + return vectorMathMinus; case OP_TYPE_GREATER_THAN: return vectorGreater; case OP_TYPE_GREATER_EQUAL: @@ -1140,4 +1168,4 @@ _bin_scalar_fn_t getBinScalarOperatorFn(int32_t binFunctionId) { assert(0); return NULL; } -} \ No newline at end of file +} From 709dda39b7ea2f4e19ac690003b8055f0bee9247 Mon Sep 17 00:00:00 2001 From: Xiaoyu Wang Date: Wed, 6 Apr 2022 03:22:41 -0400 Subject: [PATCH 15/25] TD-13037 distinct, state window and partition by plan implement --- include/libs/nodes/nodes.h | 2 + include/libs/nodes/plannodes.h | 26 + include/libs/nodes/querynodes.h | 2 +- include/util/taoserror.h | 3 + source/libs/nodes/src/nodesCloneFuncs.c | 8 + source/libs/nodes/src/nodesCodeFuncs.c | 67 +++ source/libs/nodes/src/nodesTraverseFuncs.c | 7 +- source/libs/nodes/src/nodesUtilFuncs.c | 8 +- source/libs/parser/inc/parAst.h | 2 +- source/libs/parser/inc/sql.y | 2 +- source/libs/parser/src/parAstCreater.c | 4 +- source/libs/parser/src/sql.c | 664 +++++++++++---------- source/libs/planner/src/planLogicCreater.c | 67 ++- source/libs/planner/src/planPhysiCreater.c | 114 +++- source/libs/planner/test/plannerTest.cpp | 39 ++ source/util/src/terror.c | 3 + 16 files changed, 676 insertions(+), 342 deletions(-) diff --git a/include/libs/nodes/nodes.h b/include/libs/nodes/nodes.h index d2217f7e01..1d7bd09175 100644 --- a/include/libs/nodes/nodes.h +++ b/include/libs/nodes/nodes.h @@ -163,6 +163,8 @@ typedef enum ENodeType { QUERY_NODE_PHYSICAL_PLAN_SORT, QUERY_NODE_PHYSICAL_PLAN_INTERVAL, QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW, + QUERY_NODE_PHYSICAL_PLAN_STATE_WINDOW, + QUERY_NODE_PHYSICAL_PLAN_PARTITION, QUERY_NODE_PHYSICAL_PLAN_DISPATCH, QUERY_NODE_PHYSICAL_PLAN_INSERT, QUERY_NODE_PHYSICAL_SUBPLAN, diff --git a/include/libs/nodes/plannodes.h b/include/libs/nodes/plannodes.h index 37163f60dd..9f8cddfd54 100644 --- a/include/libs/nodes/plannodes.h +++ b/include/libs/nodes/plannodes.h @@ -104,6 +104,7 @@ typedef struct SWindowLogicNode { SFillNode* pFill; int64_t sessionGap; SNode* pTspk; + SNode* pStateExpr; } SWindowLogicNode; typedef struct SSortLogicNode { @@ -194,11 +195,20 @@ typedef struct SSystemTableScanPhysiNode { int32_t accountId; } SSystemTableScanPhysiNode; +typedef enum EScanRequired { + SCAN_REQUIRED_DATA_NO_NEEDED = 1, + SCAN_REQUIRED_DATA_STATIS_NEEDED, + SCAN_REQUIRED_DATA_ALL_NEEDED, + SCAN_REQUIRED_DATA_DISCARD, +} EScanRequired; + typedef struct STableScanPhysiNode { SScanPhysiNode scan; uint8_t scanFlag; // denotes reversed scan of data or not STimeWindow scanRange; double ratio; + EScanRequired scanRequired; + SNodeList* pScanReferFuncs; } STableScanPhysiNode; typedef STableScanPhysiNode STableSeqScanPhysiNode; @@ -257,17 +267,33 @@ typedef struct SIntervalPhysiNode { SFillNode* pFill; } SIntervalPhysiNode; +typedef struct SMultiTableIntervalPhysiNode { + SIntervalPhysiNode interval; + SNodeList* pPartitionKeys; +} SMultiTableIntervalPhysiNode; + typedef struct SSessionWinodwPhysiNode { SWinodwPhysiNode window; int64_t gap; } SSessionWinodwPhysiNode; +typedef struct SStateWinodwPhysiNode { + SWinodwPhysiNode window; + SNode* pStateKey; +} SStateWinodwPhysiNode; + typedef struct SSortPhysiNode { SPhysiNode node; SNodeList* pExprs; // these are expression list of order_by_clause and parameter expression of aggregate function SNodeList* pSortKeys; // element is SOrderByExprNode, and SOrderByExprNode::pExpr is SColumnNode } SSortPhysiNode; +typedef struct SPartitionPhysiNode { + SPhysiNode node; + SNodeList* pExprs; // these are expression list of partition_by_clause + SNodeList* pPartitionKeys; +} SPartitionPhysiNode; + typedef struct SDataSinkNode { ENodeType type; SDataBlockDescNode* pInputDataBlockDesc; diff --git a/include/libs/nodes/querynodes.h b/include/libs/nodes/querynodes.h index a8ff48072f..82d05853c4 100644 --- a/include/libs/nodes/querynodes.h +++ b/include/libs/nodes/querynodes.h @@ -188,7 +188,7 @@ typedef struct SLimitNode { typedef struct SStateWindowNode { ENodeType type; // QUERY_NODE_STATE_WINDOW - SNode* pCol; + SNode* pExpr; } SStateWindowNode; typedef struct SSessionWindowNode { diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 4e2cb7944a..e7f440e26f 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -483,6 +483,9 @@ int32_t* taosGetErrno(); #define TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME TAOS_DEF_ERROR_CODE(0, 0x2617) #define TSDB_CODE_PAR_CORRESPONDING_STABLE_ERR TAOS_DEF_ERROR_CODE(0, 0x2618) +//planner +#define TSDB_CODE_PLAN_INTERNAL_ERROR TAOS_DEF_ERROR_CODE(0, 0x2700) + #ifdef __cplusplus } #endif diff --git a/source/libs/nodes/src/nodesCloneFuncs.c b/source/libs/nodes/src/nodesCloneFuncs.c index ac78988900..12fe645473 100644 --- a/source/libs/nodes/src/nodesCloneFuncs.c +++ b/source/libs/nodes/src/nodesCloneFuncs.c @@ -283,6 +283,12 @@ static SNode* logicSortCopy(const SSortLogicNode* pSrc, SSortLogicNode* pDst) { return (SNode*)pDst; } +static SNode* logicPartitionCopy(const SPartitionLogicNode* pSrc, SPartitionLogicNode* pDst) { + COPY_BASE_OBJECT_FIELD(node, logicNodeCopy); + CLONE_NODE_LIST_FIELD(pPartitionKeys); + return (SNode*)pDst; +} + static SNode* logicSubplanCopy(const SLogicSubplan* pSrc, SLogicSubplan* pDst) { CLONE_NODE_FIELD(pNode); COPY_SCALAR_FIELD(subplanType); @@ -367,6 +373,8 @@ SNodeptr nodesCloneNode(const SNodeptr pNode) { return logicWindowCopy((const SWindowLogicNode*)pNode, (SWindowLogicNode*)pDst); case QUERY_NODE_LOGIC_PLAN_SORT: return logicSortCopy((const SSortLogicNode*)pNode, (SSortLogicNode*)pDst); + case QUERY_NODE_LOGIC_PLAN_PARTITION: + return logicPartitionCopy((const SPartitionLogicNode*)pNode, (SPartitionLogicNode*)pDst); case QUERY_NODE_LOGIC_SUBPLAN: return logicSubplanCopy((const SLogicSubplan*)pNode, (SLogicSubplan*)pDst); default: diff --git a/source/libs/nodes/src/nodesCodeFuncs.c b/source/libs/nodes/src/nodesCodeFuncs.c index 1b16ba9206..6cb1955307 100644 --- a/source/libs/nodes/src/nodesCodeFuncs.c +++ b/source/libs/nodes/src/nodesCodeFuncs.c @@ -198,6 +198,10 @@ const char* nodesNodeName(ENodeType type) { return "PhysiInterval"; case QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW: return "PhysiSessionWindow"; + case QUERY_NODE_PHYSICAL_PLAN_STATE_WINDOW: + return "PhysiStateWindow"; + case QUERY_NODE_PHYSICAL_PLAN_PARTITION: + return "PhysiPartition"; case QUERY_NODE_PHYSICAL_PLAN_DISPATCH: return "PhysiDispatch"; case QUERY_NODE_PHYSICAL_PLAN_INSERT: @@ -1147,6 +1151,61 @@ static int32_t jsonToPhysiSessionWindowNode(const SJson* pJson, void* pObj) { return code; } +static const char* jkStateWindowPhysiPlanStateKey = "StateKey"; + +static int32_t physiStateWindowNodeToJson(const void* pObj, SJson* pJson) { + const SStateWinodwPhysiNode* pNode = (const SStateWinodwPhysiNode*)pObj; + + int32_t code = physiWindowNodeToJson(pObj, pJson); + if (TSDB_CODE_SUCCESS == code) { + code = tjsonAddObject(pJson, jkStateWindowPhysiPlanStateKey, nodeToJson, pNode->pStateKey); + } + + return code; +} + +static int32_t jsonToPhysiStateWindowNode(const SJson* pJson, void* pObj) { + SStateWinodwPhysiNode* pNode = (SStateWinodwPhysiNode*)pObj; + + int32_t code = jsonToPhysiWindowNode(pJson, pObj); + if (TSDB_CODE_SUCCESS == code) { + code = jsonToNodeObject(pJson, jkStateWindowPhysiPlanStateKey, &pNode->pStateKey); + } + + return code; +} + +static const char* jkPartitionPhysiPlanExprs = "Exprs"; +static const char* jkPartitionPhysiPlanPartitionKeys = "PartitionKeys"; + +static int32_t physiPartitionNodeToJson(const void* pObj, SJson* pJson) { + const SPartitionPhysiNode* pNode = (const SPartitionPhysiNode*)pObj; + + int32_t code = physicPlanNodeToJson(pObj, pJson); + if (TSDB_CODE_SUCCESS == code) { + code = nodeListToJson(pJson, jkPartitionPhysiPlanExprs, pNode->pExprs); + } + if (TSDB_CODE_SUCCESS == code) { + code = nodeListToJson(pJson, jkPartitionPhysiPlanPartitionKeys, pNode->pPartitionKeys); + } + + return code; +} + +static int32_t jsonToPhysiPartitionNode(const SJson* pJson, void* pObj) { + SPartitionPhysiNode* pNode = (SPartitionPhysiNode*)pObj; + + int32_t code = jsonToPhysicPlanNode(pJson, pObj); + if (TSDB_CODE_SUCCESS == code) { + code = jsonToNodeList(pJson, jkPartitionPhysiPlanExprs, &pNode->pExprs); + } + if (TSDB_CODE_SUCCESS == code) { + code = jsonToNodeList(pJson, jkPartitionPhysiPlanPartitionKeys, &pNode->pPartitionKeys); + } + + return code; +} + static const char* jkDataSinkInputDataBlockDesc = "InputDataBlockDesc"; static int32_t physicDataSinkNodeToJson(const void* pObj, SJson* pJson) { @@ -2420,6 +2479,10 @@ static int32_t specificNodeToJson(const void* pObj, SJson* pJson) { return physiIntervalNodeToJson(pObj, pJson); case QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW: return physiSessionWindowNodeToJson(pObj, pJson); + case QUERY_NODE_PHYSICAL_PLAN_STATE_WINDOW: + return physiStateWindowNodeToJson(pObj, pJson); + case QUERY_NODE_PHYSICAL_PLAN_PARTITION: + return physiPartitionNodeToJson(pObj, pJson); case QUERY_NODE_PHYSICAL_PLAN_DISPATCH: return physiDispatchNodeToJson(pObj, pJson); case QUERY_NODE_PHYSICAL_PLAN_INSERT: @@ -2512,6 +2575,10 @@ static int32_t jsonToSpecificNode(const SJson* pJson, void* pObj) { return jsonToPhysiIntervalNode(pJson, pObj); case QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW: return jsonToPhysiSessionWindowNode(pJson, pObj); + case QUERY_NODE_PHYSICAL_PLAN_STATE_WINDOW: + return jsonToPhysiStateWindowNode(pJson, pObj); + case QUERY_NODE_PHYSICAL_PLAN_PARTITION: + return jsonToPhysiPartitionNode(pJson, pObj); case QUERY_NODE_PHYSICAL_PLAN_DISPATCH: return jsonToPhysiDispatchNode(pJson, pObj); case QUERY_NODE_PHYSICAL_SUBPLAN: diff --git a/source/libs/nodes/src/nodesTraverseFuncs.c b/source/libs/nodes/src/nodesTraverseFuncs.c index 15c078d8d2..e59a76ca3c 100644 --- a/source/libs/nodes/src/nodesTraverseFuncs.c +++ b/source/libs/nodes/src/nodesTraverseFuncs.c @@ -78,7 +78,7 @@ static EDealRes walkNode(SNode* pNode, ETraversalOrder order, FNodeWalker walker res = walkNode(((SOrderByExprNode*)pNode)->pExpr, order, walker, pContext); break; case QUERY_NODE_STATE_WINDOW: - res = walkNode(((SStateWindowNode*)pNode)->pCol, order, walker, pContext); + res = walkNode(((SStateWindowNode*)pNode)->pExpr, order, walker, pContext); break; case QUERY_NODE_SESSION_WINDOW: { SSessionWindowNode* pSession = (SSessionWindowNode*)pNode; @@ -212,7 +212,7 @@ static EDealRes rewriteNode(SNode** pRawNode, ETraversalOrder order, FNodeRewrit res = rewriteNode(&(((SOrderByExprNode*)pNode)->pExpr), order, rewriter, pContext); break; case QUERY_NODE_STATE_WINDOW: - res = rewriteNode(&(((SStateWindowNode*)pNode)->pCol), order, rewriter, pContext); + res = rewriteNode(&(((SStateWindowNode*)pNode)->pExpr), order, rewriter, pContext); break; case QUERY_NODE_SESSION_WINDOW: res = rewriteNode(&(((SSessionWindowNode*)pNode)->pCol), order, rewriter, pContext); @@ -301,10 +301,9 @@ void nodesWalkSelectStmt(SSelectStmt* pSelect, ESqlClause clause, FNodeWalker wa case SQL_CLAUSE_GROUP_BY: nodesWalkExpr(pSelect->pHaving, walker, pContext); case SQL_CLAUSE_HAVING: - nodesWalkExprs(pSelect->pOrderByList, walker, pContext); - case SQL_CLAUSE_ORDER_BY: nodesWalkExprs(pSelect->pProjectionList, walker, pContext); case SQL_CLAUSE_SELECT: + nodesWalkExprs(pSelect->pOrderByList, walker, pContext); default: break; } diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index 90f06c6c9c..9bb603e479 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -169,6 +169,8 @@ SNodeptr nodesMakeNode(ENodeType type) { return makeNode(type, sizeof(SWindowLogicNode)); case QUERY_NODE_LOGIC_PLAN_SORT: return makeNode(type, sizeof(SSortLogicNode)); + case QUERY_NODE_LOGIC_PLAN_PARTITION: + return makeNode(type, sizeof(SPartitionLogicNode)); case QUERY_NODE_LOGIC_SUBPLAN: return makeNode(type, sizeof(SLogicSubplan)); case QUERY_NODE_LOGIC_PLAN: @@ -197,6 +199,10 @@ SNodeptr nodesMakeNode(ENodeType type) { return makeNode(type, sizeof(SIntervalPhysiNode)); case QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW: return makeNode(type, sizeof(SSessionWinodwPhysiNode)); + case QUERY_NODE_PHYSICAL_PLAN_STATE_WINDOW: + return makeNode(type, sizeof(SStateWinodwPhysiNode)); + case QUERY_NODE_PHYSICAL_PLAN_PARTITION: + return makeNode(type, sizeof(SPartitionPhysiNode)); case QUERY_NODE_PHYSICAL_PLAN_DISPATCH: return makeNode(type, sizeof(SDataDispatcherNode)); case QUERY_NODE_PHYSICAL_PLAN_INSERT: @@ -302,7 +308,7 @@ void nodesDestroyNode(SNodeptr pNode) { case QUERY_NODE_LIMIT: // no pointer field break; case QUERY_NODE_STATE_WINDOW: - nodesDestroyNode(((SStateWindowNode*)pNode)->pCol); + nodesDestroyNode(((SStateWindowNode*)pNode)->pExpr); break; case QUERY_NODE_SESSION_WINDOW: { SSessionWindowNode* pSession = (SSessionWindowNode*)pNode; diff --git a/source/libs/parser/inc/parAst.h b/source/libs/parser/inc/parAst.h index 1fce11acdf..2fe325f1c5 100644 --- a/source/libs/parser/inc/parAst.h +++ b/source/libs/parser/inc/parAst.h @@ -103,7 +103,7 @@ SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft 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, SNode* pGap); -SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pCol); +SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr); SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding, SNode* pFill); SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues); SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode); diff --git a/source/libs/parser/inc/sql.y b/source/libs/parser/inc/sql.y index 4ae33e5c5c..c30a75aa66 100644 --- a/source/libs/parser/inc/sql.y +++ b/source/libs/parser/inc/sql.y @@ -716,7 +716,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 duration_literal(C) NK_RP. { A = createSessionWindowNode(pCxt, releaseRawExprNode(pCxt, B), releaseRawExprNode(pCxt, C)); } -twindow_clause_opt(A) ::= STATE_WINDOW NK_LP column_reference(B) NK_RP. { A = createStateWindowNode(pCxt, releaseRawExprNode(pCxt, B)); } +twindow_clause_opt(A) ::= STATE_WINDOW NK_LP expression(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); } twindow_clause_opt(A) ::= diff --git a/source/libs/parser/src/parAstCreater.c b/source/libs/parser/src/parAstCreater.c index 07316ddd39..ebcecfe3cb 100644 --- a/source/libs/parser/src/parAstCreater.c +++ b/source/libs/parser/src/parAstCreater.c @@ -730,10 +730,10 @@ SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap return (SNode*)session; } -SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pCol) { +SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr) { SStateWindowNode* state = (SStateWindowNode*)nodesMakeNode(QUERY_NODE_STATE_WINDOW); CHECK_OUT_OF_MEM(state); - state->pCol = pCol; + state->pExpr = pExpr; return (SNode*)state; } diff --git a/source/libs/parser/src/sql.c b/source/libs/parser/src/sql.c index 86e564adab..30c85e28fe 100644 --- a/source/libs/parser/src/sql.c +++ b/source/libs/parser/src/sql.c @@ -209,153 +209,155 @@ typedef union { ** yy_default[] Default action for each state. ** *********** Begin parsing tables **********************************************/ -#define YY_ACTTAB_COUNT (1448) +#define YY_ACTTAB_COUNT (1461) static const YYACTIONTYPE yy_action[] = { - /* 0 */ 1250, 441, 441, 265, 441, 1246, 1413, 72, 304, 306, - /* 10 */ 72, 111, 30, 28, 348, 409, 252, 354, 20, 1263, - /* 20 */ 261, 1153, 990, 1261, 1261, 269, 1261, 1429, 31, 29, - /* 30 */ 27, 26, 25, 1399, 412, 441, 233, 98, 988, 117, - /* 40 */ 409, 305, 1399, 1014, 427, 1395, 1401, 11, 1386, 1429, - /* 50 */ 1301, 30, 28, 1094, 1395, 1401, 425, 1261, 799, 261, + /* 0 */ 1250, 441, 441, 266, 441, 1246, 1413, 72, 305, 307, + /* 10 */ 72, 111, 31, 29, 349, 409, 253, 355, 21, 1263, + /* 20 */ 262, 1153, 990, 1261, 1261, 270, 1261, 1429, 32, 30, + /* 30 */ 28, 27, 26, 1399, 412, 441, 234, 98, 988, 117, + /* 40 */ 409, 306, 1399, 1014, 427, 1395, 1401, 11, 1386, 1429, + /* 50 */ 1301, 31, 29, 1094, 1395, 1401, 425, 1261, 799, 262, /* 60 */ 798, 990, 98, 409, 69, 1414, 1415, 1418, 1462, 1, - /* 70 */ 96, 276, 254, 1458, 120, 1413, 1308, 988, 800, 411, - /* 80 */ 121, 1469, 1470, 1308, 1474, 98, 11, 1340, 399, 251, - /* 90 */ 30, 28, 507, 1490, 1306, 96, 1429, 1252, 261, 325, + /* 70 */ 96, 277, 255, 1458, 120, 1413, 1308, 988, 800, 411, + /* 80 */ 121, 1469, 1470, 1308, 1474, 98, 11, 1340, 402, 252, + /* 90 */ 31, 29, 507, 1490, 1306, 96, 1429, 1252, 262, 326, /* 100 */ 990, 440, 1523, 425, 989, 122, 1469, 1470, 1, 1474, - /* 110 */ 22, 24, 191, 427, 1399, 125, 988, 1386, 96, 1521, - /* 120 */ 31, 29, 27, 26, 25, 11, 1395, 1402, 123, 1469, + /* 110 */ 23, 25, 191, 427, 1399, 125, 988, 1386, 96, 1521, + /* 120 */ 32, 30, 28, 27, 26, 11, 1395, 1402, 123, 1469, /* 130 */ 1470, 507, 1474, 70, 1414, 1415, 1418, 1462, 991, 1386, - /* 140 */ 440, 1461, 1458, 989, 135, 134, 1413, 1, 31, 29, - /* 150 */ 27, 26, 25, 186, 65, 994, 995, 1038, 1039, 1040, - /* 160 */ 1041, 1042, 1043, 1044, 1045, 99, 232, 1429, 1010, 1406, - /* 170 */ 507, 299, 1253, 298, 425, 318, 126, 991, 330, 1010, - /* 180 */ 428, 1404, 989, 264, 427, 1429, 1348, 331, 1386, 12, - /* 190 */ 1413, 398, 425, 131, 994, 995, 1038, 1039, 1040, 1041, + /* 140 */ 440, 1461, 1458, 989, 135, 134, 1413, 1, 32, 30, + /* 150 */ 28, 27, 26, 186, 65, 994, 995, 1038, 1039, 1040, + /* 160 */ 1041, 1042, 1043, 1044, 1045, 99, 233, 1429, 1010, 1406, + /* 170 */ 507, 300, 1253, 299, 425, 319, 126, 991, 331, 1010, + /* 180 */ 428, 1404, 989, 265, 427, 273, 1348, 332, 1386, 12, + /* 190 */ 1413, 126, 1339, 1341, 994, 995, 1038, 1039, 1040, 1041, /* 200 */ 1042, 1043, 1044, 1045, 69, 1414, 1415, 1418, 1462, 441, - /* 210 */ 126, 1429, 254, 1458, 1535, 311, 991, 47, 425, 1308, - /* 220 */ 46, 441, 1336, 1496, 402, 266, 440, 312, 427, 133, + /* 210 */ 126, 1429, 255, 1458, 1535, 312, 991, 1239, 425, 1308, + /* 220 */ 159, 441, 1336, 1496, 352, 267, 440, 313, 427, 133, /* 230 */ 1306, 1261, 1386, 994, 995, 1038, 1039, 1040, 1041, 1042, - /* 240 */ 1043, 1044, 1045, 1261, 404, 400, 30, 28, 70, 1414, - /* 250 */ 1415, 1418, 1462, 64, 261, 329, 990, 1459, 324, 323, - /* 260 */ 322, 321, 320, 60, 317, 316, 315, 314, 310, 309, - /* 270 */ 308, 307, 988, 84, 1118, 12, 83, 82, 81, 80, - /* 280 */ 79, 78, 77, 76, 75, 30, 28, 426, 1156, 361, - /* 290 */ 111, 356, 300, 261, 360, 990, 126, 160, 1264, 357, - /* 300 */ 355, 126, 358, 7, 395, 1116, 1117, 1119, 1120, 84, - /* 310 */ 6, 988, 83, 82, 81, 80, 79, 78, 77, 76, - /* 320 */ 75, 441, 1308, 1523, 30, 28, 507, 338, 273, 503, - /* 330 */ 502, 441, 261, 1306, 990, 126, 125, 1258, 989, 114, - /* 340 */ 1521, 1225, 7, 1261, 272, 31, 29, 27, 26, 25, - /* 350 */ 988, 1339, 1341, 1261, 874, 464, 463, 462, 878, 461, + /* 240 */ 1043, 1044, 1045, 1261, 1237, 354, 31, 29, 70, 1414, + /* 250 */ 1415, 1418, 1462, 340, 262, 330, 990, 1459, 325, 324, + /* 260 */ 323, 322, 321, 340, 318, 317, 316, 315, 311, 310, + /* 270 */ 309, 308, 988, 84, 1118, 12, 83, 82, 81, 80, + /* 280 */ 79, 78, 77, 76, 75, 31, 29, 426, 1156, 362, + /* 290 */ 476, 357, 301, 262, 361, 990, 126, 160, 213, 358, + /* 300 */ 356, 1291, 359, 7, 396, 1116, 1117, 1119, 1120, 84, + /* 310 */ 1178, 988, 83, 82, 81, 80, 79, 78, 77, 76, + /* 320 */ 75, 441, 1308, 1523, 31, 29, 507, 339, 274, 399, + /* 330 */ 476, 441, 262, 1306, 990, 126, 125, 1258, 989, 114, + /* 340 */ 1521, 1225, 7, 1261, 1012, 32, 30, 28, 27, 26, + /* 350 */ 988, 131, 1386, 1261, 874, 464, 463, 462, 878, 461, /* 360 */ 880, 881, 460, 883, 457, 507, 889, 454, 891, 892, - /* 370 */ 451, 448, 991, 27, 26, 25, 113, 989, 1167, 213, - /* 380 */ 1413, 7, 1291, 1375, 31, 29, 27, 26, 25, 994, + /* 370 */ 451, 448, 991, 1177, 113, 48, 1167, 989, 47, 403, + /* 380 */ 1413, 7, 404, 400, 32, 30, 28, 27, 26, 994, /* 390 */ 995, 1038, 1039, 1040, 1041, 1042, 1043, 1044, 1045, 441, - /* 400 */ 1523, 1429, 1201, 1026, 507, 438, 339, 384, 425, 1239, - /* 410 */ 126, 991, 1178, 1522, 428, 1012, 989, 1521, 427, 283, - /* 420 */ 1349, 1261, 1386, 31, 29, 27, 26, 25, 994, 995, + /* 400 */ 1523, 1429, 1201, 1108, 507, 438, 111, 385, 425, 1013, + /* 410 */ 126, 991, 1176, 1522, 1264, 1386, 989, 1521, 427, 503, + /* 420 */ 502, 1261, 1386, 32, 30, 28, 27, 26, 994, 995, /* 430 */ 1038, 1039, 1040, 1041, 1042, 1043, 1044, 1045, 115, 1414, - /* 440 */ 1415, 1418, 1089, 31, 29, 27, 26, 25, 385, 271, - /* 450 */ 991, 361, 1015, 356, 1386, 339, 360, 111, 235, 160, - /* 460 */ 245, 357, 355, 1177, 358, 1263, 1413, 994, 995, 1038, - /* 470 */ 1039, 1040, 1041, 1042, 1043, 1044, 1045, 1537, 235, 1523, - /* 480 */ 30, 28, 159, 9, 8, 403, 351, 1429, 261, 1057, - /* 490 */ 990, 1155, 125, 49, 425, 291, 1521, 246, 375, 244, - /* 500 */ 243, 1026, 350, 1148, 427, 1386, 988, 353, 1386, 1057, - /* 510 */ 293, 1257, 1176, 413, 1175, 93, 92, 91, 90, 89, + /* 440 */ 1415, 1418, 6, 32, 30, 28, 27, 26, 386, 272, + /* 450 */ 991, 362, 1015, 357, 1386, 1248, 361, 111, 236, 160, + /* 460 */ 246, 358, 356, 1175, 359, 1263, 1413, 994, 995, 1038, + /* 470 */ 1039, 1040, 1041, 1042, 1043, 1044, 1045, 1537, 236, 1523, + /* 480 */ 31, 29, 159, 9, 8, 1011, 352, 1429, 262, 1057, + /* 490 */ 990, 1155, 125, 1059, 425, 292, 1521, 247, 376, 245, + /* 500 */ 244, 1026, 351, 1148, 427, 1386, 988, 354, 1386, 1057, + /* 510 */ 294, 1063, 1174, 413, 1173, 93, 92, 91, 90, 89, /* 520 */ 88, 87, 86, 85, 68, 1414, 1415, 1418, 1462, 1523, - /* 530 */ 49, 274, 234, 1458, 159, 1058, 416, 1, 351, 111, - /* 540 */ 95, 1093, 125, 1174, 1523, 441, 1521, 1263, 1256, 1013, - /* 550 */ 1059, 439, 1070, 1062, 1386, 1058, 1386, 125, 476, 353, - /* 560 */ 507, 1521, 172, 1308, 441, 441, 1173, 1261, 1063, 1147, - /* 570 */ 205, 275, 989, 1062, 1307, 1172, 23, 259, 1052, 1053, - /* 580 */ 1054, 1055, 1056, 1060, 1061, 1386, 1261, 1261, 1011, 1476, - /* 590 */ 1194, 21, 1171, 1170, 1169, 1166, 23, 259, 1052, 1053, - /* 600 */ 1054, 1055, 1056, 1060, 1061, 1198, 991, 1473, 1386, 1101, - /* 610 */ 1165, 147, 362, 1248, 119, 1012, 112, 1386, 1481, 1089, - /* 620 */ 344, 219, 146, 994, 995, 1038, 1039, 1040, 1041, 1042, - /* 630 */ 1043, 1044, 1045, 217, 1386, 1386, 1386, 1386, 1164, 1163, - /* 640 */ 1162, 1161, 1160, 136, 1159, 1158, 50, 9, 8, 144, - /* 650 */ 1237, 1413, 1386, 420, 495, 494, 493, 492, 491, 490, + /* 530 */ 50, 275, 235, 1458, 22, 1058, 416, 1, 1308, 111, + /* 540 */ 95, 50, 125, 1172, 1523, 441, 1521, 1263, 1256, 1307, + /* 550 */ 798, 439, 1070, 1062, 1386, 1058, 1386, 125, 467, 1257, + /* 560 */ 507, 1521, 172, 417, 441, 441, 347, 1261, 1171, 1147, + /* 570 */ 205, 276, 989, 1062, 1089, 1170, 24, 260, 1052, 1053, + /* 580 */ 1054, 1055, 1056, 1060, 1061, 1386, 1261, 1261, 1244, 32, + /* 590 */ 30, 28, 27, 26, 1169, 1166, 24, 260, 1052, 1053, + /* 600 */ 1054, 1055, 1056, 1060, 1061, 1198, 991, 28, 27, 26, + /* 610 */ 1386, 147, 428, 1101, 119, 1093, 112, 1386, 1349, 1012, + /* 620 */ 345, 219, 146, 994, 995, 1038, 1039, 1040, 1041, 1042, + /* 630 */ 1043, 1044, 1045, 217, 1165, 1476, 1386, 1386, 1164, 1163, + /* 640 */ 1162, 1161, 1160, 136, 1159, 1158, 51, 1026, 420, 144, + /* 650 */ 998, 1413, 1375, 1473, 495, 494, 493, 492, 491, 490, /* 660 */ 489, 488, 207, 485, 484, 483, 482, 481, 480, 479, - /* 670 */ 478, 477, 1429, 1476, 1476, 486, 58, 467, 294, 425, - /* 680 */ 1386, 1386, 1386, 1386, 1386, 798, 1386, 1386, 417, 427, - /* 690 */ 424, 1472, 1471, 1386, 1254, 152, 476, 154, 150, 1413, - /* 700 */ 153, 346, 373, 143, 67, 138, 998, 140, 383, 69, - /* 710 */ 1414, 1415, 1418, 1462, 1092, 371, 1244, 254, 1458, 1535, - /* 720 */ 1429, 1150, 1151, 165, 137, 415, 466, 412, 1519, 45, - /* 730 */ 44, 303, 156, 130, 1189, 155, 104, 427, 297, 387, - /* 740 */ 158, 1386, 1413, 157, 1168, 1226, 241, 1187, 289, 396, - /* 750 */ 285, 281, 127, 188, 1302, 345, 364, 69, 1414, 1415, - /* 760 */ 1418, 1462, 181, 1429, 1492, 254, 1458, 120, 43, 367, - /* 770 */ 425, 1115, 1001, 175, 126, 997, 177, 1430, 1413, 187, - /* 780 */ 427, 410, 421, 190, 1386, 391, 1489, 32, 32, 32, - /* 790 */ 1064, 1023, 957, 194, 2, 1010, 196, 278, 1049, 1429, - /* 800 */ 69, 1414, 1415, 1418, 1462, 1413, 425, 282, 254, 1458, - /* 810 */ 1535, 101, 240, 242, 433, 418, 427, 841, 102, 1480, - /* 820 */ 1386, 202, 966, 211, 313, 413, 1429, 409, 510, 1338, - /* 830 */ 132, 104, 319, 425, 867, 327, 224, 1414, 1415, 1418, - /* 840 */ 326, 1000, 210, 427, 328, 94, 332, 1386, 1019, 98, - /* 850 */ 333, 499, 43, 209, 446, 862, 1523, 895, 102, 103, - /* 860 */ 1413, 899, 905, 70, 1414, 1415, 1418, 1462, 413, 125, - /* 870 */ 334, 423, 1458, 1521, 1018, 335, 139, 66, 1017, 336, - /* 880 */ 203, 1429, 96, 104, 102, 337, 904, 105, 425, 142, - /* 890 */ 48, 145, 184, 1469, 408, 340, 407, 1016, 427, 1523, - /* 900 */ 347, 349, 1386, 74, 1413, 1251, 376, 149, 1247, 1413, - /* 910 */ 437, 377, 125, 151, 106, 107, 1521, 1249, 115, 1414, - /* 920 */ 1415, 1418, 1245, 108, 109, 1429, 352, 359, 378, 250, - /* 930 */ 1429, 1015, 425, 382, 167, 390, 388, 425, 168, 379, - /* 940 */ 170, 397, 427, 1503, 431, 386, 1386, 427, 1493, 260, - /* 950 */ 389, 1386, 995, 974, 392, 164, 414, 1536, 1502, 5, - /* 960 */ 1483, 173, 228, 1414, 1415, 1418, 1413, 228, 1414, 1415, - /* 970 */ 1418, 1413, 406, 394, 393, 176, 253, 4, 401, 1089, - /* 980 */ 97, 1014, 33, 183, 180, 422, 255, 1429, 118, 1477, - /* 990 */ 419, 429, 1429, 182, 425, 17, 1347, 1346, 430, 425, - /* 1000 */ 263, 434, 1444, 435, 427, 198, 1538, 1413, 1386, 427, - /* 1010 */ 200, 57, 436, 1386, 1520, 1413, 258, 59, 1262, 212, - /* 1020 */ 473, 189, 444, 214, 227, 1414, 1415, 1418, 1429, 228, - /* 1030 */ 1414, 1415, 1418, 487, 208, 425, 1429, 506, 216, 220, - /* 1040 */ 221, 1413, 39, 425, 218, 427, 1380, 1379, 277, 1386, - /* 1050 */ 1413, 279, 262, 427, 1376, 280, 405, 1386, 984, 985, - /* 1060 */ 128, 284, 1429, 1374, 286, 228, 1414, 1415, 1418, 425, - /* 1070 */ 287, 1429, 288, 226, 1414, 1415, 1418, 1413, 425, 427, - /* 1080 */ 1373, 290, 1372, 1386, 1363, 1413, 292, 129, 427, 295, - /* 1090 */ 296, 969, 1386, 968, 1357, 1356, 302, 1355, 1429, 229, - /* 1100 */ 1414, 1415, 1418, 301, 1354, 425, 1429, 1331, 222, 1414, - /* 1110 */ 1415, 1418, 940, 425, 1330, 427, 1329, 1328, 1327, 1386, - /* 1120 */ 1326, 1413, 1325, 427, 1324, 1323, 1413, 1386, 1322, 1321, - /* 1130 */ 1320, 1319, 100, 1318, 1317, 230, 1414, 1415, 1418, 1316, - /* 1140 */ 1315, 1314, 1429, 223, 1414, 1415, 1418, 1429, 1313, 425, - /* 1150 */ 1312, 1311, 1310, 1309, 425, 1200, 1371, 942, 1365, 427, - /* 1160 */ 1353, 1344, 1413, 1386, 427, 1240, 141, 1413, 1386, 811, - /* 1170 */ 1199, 1197, 1186, 1413, 341, 343, 1185, 1182, 342, 231, - /* 1180 */ 1414, 1415, 1418, 1429, 1426, 1414, 1415, 1418, 1429, 1242, - /* 1190 */ 425, 73, 912, 486, 1429, 425, 910, 1241, 1195, 1190, - /* 1200 */ 427, 425, 1238, 148, 1386, 427, 840, 839, 247, 1386, - /* 1210 */ 248, 427, 365, 838, 1413, 1386, 837, 835, 834, 1188, - /* 1220 */ 1425, 1414, 1415, 1418, 1236, 1424, 1414, 1415, 1418, 1413, - /* 1230 */ 249, 238, 1414, 1415, 1418, 1429, 1181, 370, 268, 267, - /* 1240 */ 1413, 368, 425, 1180, 372, 71, 1370, 163, 1003, 42, - /* 1250 */ 1429, 976, 427, 1364, 990, 1413, 1386, 425, 110, 206, - /* 1260 */ 380, 1429, 1352, 472, 996, 166, 1351, 427, 425, 1343, - /* 1270 */ 988, 1386, 237, 1414, 1415, 1418, 1429, 381, 427, 36, - /* 1280 */ 366, 206, 1386, 425, 474, 472, 51, 239, 1414, 1415, - /* 1290 */ 1418, 169, 171, 427, 3, 374, 32, 1386, 236, 1414, - /* 1300 */ 1415, 1418, 37, 471, 470, 469, 474, 468, 116, 162, - /* 1310 */ 174, 1114, 369, 225, 1414, 1415, 1418, 363, 442, 178, - /* 1320 */ 161, 1108, 1107, 14, 507, 471, 470, 469, 52, 468, - /* 1330 */ 999, 179, 53, 34, 19, 1404, 989, 15, 1086, 185, - /* 1340 */ 1085, 35, 124, 1141, 41, 16, 10, 40, 54, 1136, - /* 1350 */ 1135, 256, 1140, 1139, 257, 8, 1050, 1024, 192, 13, - /* 1360 */ 18, 432, 193, 1112, 1004, 195, 197, 55, 1342, 199, - /* 1370 */ 991, 56, 201, 60, 1005, 38, 1403, 896, 445, 270, - /* 1380 */ 204, 1007, 995, 443, 449, 452, 447, 994, 995, 893, - /* 1390 */ 450, 873, 890, 453, 455, 884, 456, 458, 882, 459, - /* 1400 */ 61, 907, 888, 887, 886, 465, 62, 63, 906, 903, - /* 1410 */ 901, 475, 885, 809, 831, 830, 829, 828, 827, 826, - /* 1420 */ 825, 824, 823, 842, 821, 820, 1196, 819, 818, 817, - /* 1430 */ 1184, 816, 815, 814, 496, 497, 500, 1183, 501, 1179, - /* 1440 */ 498, 504, 505, 1154, 992, 215, 508, 509, + /* 670 */ 478, 477, 1429, 1476, 1476, 424, 1386, 165, 58, 425, + /* 680 */ 1386, 1386, 1386, 1386, 1386, 466, 1386, 1386, 284, 427, + /* 690 */ 418, 1472, 1471, 1386, 1481, 1089, 1254, 486, 152, 1413, + /* 700 */ 295, 150, 374, 143, 67, 138, 384, 140, 1168, 69, + /* 710 */ 1414, 1415, 1418, 1462, 1226, 372, 1001, 255, 1458, 1535, + /* 720 */ 1429, 1194, 1189, 154, 137, 415, 153, 412, 1519, 46, + /* 730 */ 45, 304, 156, 130, 1187, 155, 158, 427, 298, 157, + /* 740 */ 1302, 1386, 1413, 363, 365, 64, 242, 188, 290, 397, + /* 750 */ 286, 282, 127, 9, 8, 60, 368, 69, 1414, 1415, + /* 760 */ 1418, 1462, 181, 1429, 346, 255, 1458, 120, 104, 44, + /* 770 */ 425, 388, 1115, 175, 126, 997, 177, 421, 1413, 187, + /* 780 */ 427, 1150, 1151, 1049, 1386, 392, 1489, 33, 1092, 410, + /* 790 */ 1064, 33, 33, 194, 1023, 957, 196, 1492, 1430, 1429, + /* 800 */ 69, 1414, 1415, 1418, 1462, 1413, 425, 190, 255, 1458, + /* 810 */ 1535, 101, 102, 2, 433, 202, 427, 1010, 104, 1480, + /* 820 */ 1386, 867, 283, 279, 241, 413, 1429, 409, 510, 243, + /* 830 */ 966, 44, 841, 425, 862, 211, 225, 1414, 1415, 1418, + /* 840 */ 320, 1000, 210, 427, 314, 94, 1338, 1386, 132, 98, + /* 850 */ 333, 499, 446, 209, 102, 895, 1523, 899, 103, 104, + /* 860 */ 1413, 905, 904, 70, 1414, 1415, 1418, 1462, 413, 125, + /* 870 */ 328, 423, 1458, 1521, 1019, 327, 329, 66, 334, 1018, + /* 880 */ 203, 1429, 96, 102, 335, 336, 105, 139, 425, 337, + /* 890 */ 1017, 338, 184, 1469, 408, 142, 407, 49, 427, 1523, + /* 900 */ 341, 145, 1386, 1016, 1413, 348, 74, 350, 379, 1413, + /* 910 */ 437, 1251, 125, 149, 378, 380, 1521, 1247, 115, 1414, + /* 920 */ 1415, 1418, 151, 106, 353, 1429, 360, 377, 107, 1249, + /* 930 */ 1429, 1015, 425, 1245, 108, 391, 109, 425, 168, 167, + /* 940 */ 170, 251, 427, 387, 398, 389, 1386, 427, 1493, 261, + /* 950 */ 390, 1386, 431, 974, 393, 164, 414, 1536, 383, 1503, + /* 960 */ 995, 173, 229, 1414, 1415, 1418, 1413, 229, 1414, 1415, + /* 970 */ 1418, 1413, 1502, 395, 5, 176, 1089, 394, 254, 406, + /* 980 */ 1483, 97, 401, 4, 1014, 17, 34, 1429, 256, 1477, + /* 990 */ 422, 183, 1429, 419, 425, 1347, 429, 435, 198, 425, + /* 1000 */ 430, 264, 182, 180, 427, 118, 1346, 1413, 1386, 427, + /* 1010 */ 1444, 434, 1262, 1386, 436, 1413, 259, 200, 59, 57, + /* 1020 */ 212, 1520, 214, 208, 228, 1414, 1415, 1418, 1429, 229, + /* 1030 */ 1414, 1415, 1418, 473, 444, 425, 1429, 189, 487, 506, + /* 1040 */ 220, 1413, 1538, 425, 221, 427, 40, 216, 1380, 1386, + /* 1050 */ 1413, 218, 263, 427, 1379, 278, 405, 1386, 1376, 280, + /* 1060 */ 281, 984, 1429, 285, 985, 229, 1414, 1415, 1418, 425, + /* 1070 */ 128, 1429, 1374, 222, 1414, 1415, 1418, 1413, 425, 427, + /* 1080 */ 287, 288, 289, 1386, 1373, 1413, 1372, 291, 427, 1363, + /* 1090 */ 293, 129, 1386, 296, 297, 969, 1357, 968, 1429, 227, + /* 1100 */ 1414, 1415, 1418, 1356, 303, 425, 1429, 302, 230, 1414, + /* 1110 */ 1415, 1418, 1355, 425, 1354, 427, 940, 1331, 1330, 1386, + /* 1120 */ 1329, 1413, 1328, 427, 1327, 1326, 1413, 1386, 1325, 1324, + /* 1130 */ 1323, 1322, 1321, 1320, 1319, 223, 1414, 1415, 1418, 100, + /* 1140 */ 1318, 1317, 1429, 231, 1414, 1415, 1418, 1429, 1316, 425, + /* 1150 */ 1315, 1314, 1313, 1312, 425, 1311, 1310, 942, 1309, 427, + /* 1160 */ 1200, 1371, 1413, 1386, 427, 1365, 1353, 1413, 1386, 1344, + /* 1170 */ 141, 1240, 1199, 1413, 811, 1197, 342, 1186, 343, 224, + /* 1180 */ 1414, 1415, 1418, 1429, 232, 1414, 1415, 1418, 1429, 344, + /* 1190 */ 425, 1185, 1182, 1242, 1429, 425, 910, 73, 1241, 148, + /* 1200 */ 427, 425, 912, 486, 1386, 427, 840, 1195, 839, 1386, + /* 1210 */ 248, 427, 1190, 1188, 1413, 1386, 838, 837, 835, 834, + /* 1220 */ 1426, 1414, 1415, 1418, 249, 1425, 1414, 1415, 1418, 1413, + /* 1230 */ 250, 1424, 1414, 1415, 1418, 1429, 366, 369, 269, 268, + /* 1240 */ 1413, 371, 425, 1181, 1180, 373, 1370, 71, 1003, 1238, + /* 1250 */ 1429, 43, 427, 163, 976, 1413, 1386, 425, 1364, 110, + /* 1260 */ 381, 1429, 1352, 990, 996, 1236, 1351, 427, 425, 382, + /* 1270 */ 166, 1386, 239, 1414, 1415, 1418, 1429, 37, 427, 988, + /* 1280 */ 1343, 1413, 1386, 425, 52, 169, 116, 238, 1414, 1415, + /* 1290 */ 1418, 3, 367, 427, 178, 33, 38, 1386, 240, 1414, + /* 1300 */ 1415, 1418, 1429, 174, 1114, 19, 206, 375, 171, 425, + /* 1310 */ 472, 1404, 179, 237, 1414, 1415, 1418, 14, 442, 427, + /* 1320 */ 1107, 162, 206, 1386, 370, 53, 472, 20, 185, 364, + /* 1330 */ 999, 474, 161, 507, 15, 124, 35, 1086, 1085, 226, + /* 1340 */ 1414, 1415, 1418, 36, 1141, 989, 16, 474, 54, 1136, + /* 1350 */ 471, 470, 469, 10, 468, 1135, 42, 257, 1140, 41, + /* 1360 */ 1139, 258, 8, 192, 1004, 1050, 471, 470, 469, 1024, + /* 1370 */ 468, 13, 18, 1342, 193, 1112, 195, 197, 55, 991, + /* 1380 */ 432, 1007, 995, 199, 201, 56, 1005, 1403, 896, 60, + /* 1390 */ 39, 445, 271, 204, 443, 447, 994, 995, 893, 449, + /* 1400 */ 450, 452, 890, 453, 455, 884, 456, 458, 882, 888, + /* 1410 */ 459, 873, 465, 907, 61, 62, 63, 906, 887, 886, + /* 1420 */ 903, 885, 901, 831, 809, 475, 830, 823, 829, 828, + /* 1430 */ 827, 826, 825, 824, 842, 821, 820, 819, 1196, 1184, + /* 1440 */ 818, 817, 816, 1183, 815, 814, 496, 497, 500, 501, + /* 1450 */ 1179, 504, 498, 505, 1154, 992, 215, 508, 1154, 1154, + /* 1460 */ 509, }; static const YYCODETYPE yy_lookahead[] = { /* 0 */ 226, 210, 210, 217, 210, 226, 204, 216, 216, 210, @@ -376,135 +378,135 @@ static const YYCODETYPE yy_lookahead[] = { /* 150 */ 14, 15, 16, 130, 209, 155, 156, 157, 158, 159, /* 160 */ 160, 161, 162, 163, 164, 220, 18, 225, 20, 69, /* 170 */ 92, 137, 227, 139, 232, 27, 176, 138, 30, 20, - /* 180 */ 242, 81, 104, 245, 242, 225, 248, 39, 246, 69, - /* 190 */ 204, 128, 232, 44, 155, 156, 157, 158, 159, 160, + /* 180 */ 242, 81, 104, 245, 242, 234, 248, 39, 246, 69, + /* 190 */ 204, 176, 241, 242, 155, 156, 157, 158, 159, 160, /* 200 */ 161, 162, 163, 164, 262, 263, 264, 265, 266, 210, - /* 210 */ 176, 225, 270, 271, 272, 216, 138, 68, 232, 225, - /* 220 */ 71, 210, 232, 281, 264, 231, 20, 216, 242, 239, + /* 210 */ 176, 225, 270, 271, 272, 216, 138, 0, 232, 225, + /* 220 */ 57, 210, 232, 281, 61, 231, 20, 216, 242, 239, /* 230 */ 236, 232, 246, 155, 156, 157, 158, 159, 160, 161, - /* 240 */ 162, 163, 164, 232, 181, 182, 12, 13, 262, 263, - /* 250 */ 264, 265, 266, 69, 20, 107, 22, 271, 110, 111, - /* 260 */ 112, 113, 114, 79, 116, 117, 118, 119, 120, 121, + /* 240 */ 162, 163, 164, 232, 0, 82, 12, 13, 262, 263, + /* 250 */ 264, 265, 266, 46, 20, 107, 22, 271, 110, 111, + /* 260 */ 112, 113, 114, 46, 116, 117, 118, 119, 120, 121, /* 270 */ 122, 123, 38, 21, 155, 69, 24, 25, 26, 27, /* 280 */ 28, 29, 30, 31, 32, 12, 13, 14, 0, 49, - /* 290 */ 225, 51, 251, 20, 54, 22, 176, 57, 233, 59, - /* 300 */ 60, 176, 62, 69, 185, 186, 187, 188, 189, 21, - /* 310 */ 43, 38, 24, 25, 26, 27, 28, 29, 30, 31, - /* 320 */ 32, 210, 225, 282, 12, 13, 92, 216, 231, 207, - /* 330 */ 208, 210, 20, 236, 22, 176, 295, 216, 104, 213, - /* 340 */ 299, 215, 69, 232, 234, 12, 13, 14, 15, 16, - /* 350 */ 38, 241, 242, 232, 83, 84, 85, 86, 87, 88, + /* 290 */ 46, 51, 251, 20, 54, 22, 176, 57, 218, 59, + /* 300 */ 60, 221, 62, 69, 185, 186, 187, 188, 189, 21, + /* 310 */ 204, 38, 24, 25, 26, 27, 28, 29, 30, 31, + /* 320 */ 32, 210, 225, 282, 12, 13, 92, 216, 231, 128, + /* 330 */ 46, 210, 20, 236, 22, 176, 295, 216, 104, 213, + /* 340 */ 299, 215, 69, 232, 20, 12, 13, 14, 15, 16, + /* 350 */ 38, 44, 246, 232, 83, 84, 85, 86, 87, 88, /* 360 */ 89, 90, 91, 92, 93, 92, 95, 96, 97, 98, - /* 370 */ 99, 100, 138, 14, 15, 16, 203, 104, 205, 218, - /* 380 */ 204, 69, 221, 0, 12, 13, 14, 15, 16, 155, + /* 370 */ 99, 100, 138, 204, 203, 68, 205, 104, 71, 20, + /* 380 */ 204, 69, 181, 182, 12, 13, 14, 15, 16, 155, /* 390 */ 156, 157, 158, 159, 160, 161, 162, 163, 164, 210, - /* 400 */ 282, 225, 0, 70, 92, 216, 46, 210, 232, 0, - /* 410 */ 176, 138, 204, 295, 242, 20, 104, 299, 242, 36, - /* 420 */ 248, 232, 246, 12, 13, 14, 15, 16, 155, 156, + /* 400 */ 282, 225, 0, 70, 92, 216, 225, 210, 232, 20, + /* 410 */ 176, 138, 204, 295, 233, 246, 104, 299, 242, 207, + /* 420 */ 208, 232, 246, 12, 13, 14, 15, 16, 155, 156, /* 430 */ 157, 158, 159, 160, 161, 162, 163, 164, 262, 263, - /* 440 */ 264, 265, 175, 12, 13, 14, 15, 16, 251, 217, - /* 450 */ 138, 49, 20, 51, 246, 46, 54, 225, 47, 57, + /* 440 */ 264, 265, 43, 12, 13, 14, 15, 16, 251, 217, + /* 450 */ 138, 49, 20, 51, 246, 226, 54, 225, 47, 57, /* 460 */ 35, 59, 60, 204, 62, 233, 204, 155, 156, 157, /* 470 */ 158, 159, 160, 161, 162, 163, 164, 301, 47, 282, /* 480 */ 12, 13, 57, 1, 2, 20, 61, 225, 20, 78, - /* 490 */ 22, 0, 295, 212, 232, 134, 299, 72, 251, 74, + /* 490 */ 22, 0, 295, 124, 232, 134, 299, 72, 251, 74, /* 500 */ 75, 70, 77, 131, 242, 246, 38, 82, 246, 78, - /* 510 */ 149, 230, 204, 251, 204, 24, 25, 26, 27, 28, + /* 510 */ 149, 142, 204, 251, 204, 24, 25, 26, 27, 28, /* 520 */ 29, 30, 31, 32, 262, 263, 264, 265, 266, 282, - /* 530 */ 212, 217, 270, 271, 57, 124, 3, 69, 61, 225, - /* 540 */ 222, 4, 295, 204, 282, 210, 299, 233, 230, 20, - /* 550 */ 124, 216, 70, 142, 246, 124, 246, 295, 46, 82, - /* 560 */ 92, 299, 130, 225, 210, 210, 204, 232, 142, 197, - /* 570 */ 216, 216, 104, 142, 236, 204, 165, 166, 167, 168, - /* 580 */ 169, 170, 171, 172, 173, 246, 232, 232, 20, 260, - /* 590 */ 0, 165, 204, 204, 204, 204, 165, 166, 167, 168, - /* 600 */ 169, 170, 171, 172, 173, 0, 138, 278, 246, 14, - /* 610 */ 204, 33, 22, 226, 36, 20, 18, 246, 174, 175, + /* 530 */ 212, 217, 270, 271, 165, 124, 3, 69, 225, 225, + /* 540 */ 222, 212, 295, 204, 282, 210, 299, 233, 230, 236, + /* 550 */ 22, 216, 70, 142, 246, 124, 246, 295, 80, 230, + /* 560 */ 92, 299, 130, 67, 210, 210, 38, 232, 204, 197, + /* 570 */ 216, 216, 104, 142, 175, 204, 165, 166, 167, 168, + /* 580 */ 169, 170, 171, 172, 173, 246, 232, 232, 226, 12, + /* 590 */ 13, 14, 15, 16, 204, 204, 165, 166, 167, 168, + /* 600 */ 169, 170, 171, 172, 173, 0, 138, 14, 15, 16, + /* 610 */ 246, 33, 242, 14, 36, 4, 18, 246, 248, 20, /* 620 */ 42, 23, 44, 155, 156, 157, 158, 159, 160, 161, - /* 630 */ 162, 163, 164, 35, 246, 246, 246, 246, 204, 204, - /* 640 */ 204, 204, 204, 45, 204, 204, 68, 1, 2, 71, - /* 650 */ 0, 204, 246, 67, 49, 50, 51, 52, 53, 54, + /* 630 */ 162, 163, 164, 35, 204, 260, 246, 246, 204, 204, + /* 640 */ 204, 204, 204, 45, 204, 204, 68, 70, 67, 71, + /* 650 */ 38, 204, 0, 278, 49, 50, 51, 52, 53, 54, /* 660 */ 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, - /* 670 */ 65, 66, 225, 260, 260, 67, 209, 80, 70, 232, - /* 680 */ 246, 246, 246, 246, 246, 22, 246, 246, 67, 242, - /* 690 */ 47, 278, 278, 246, 227, 73, 46, 73, 76, 204, - /* 700 */ 76, 38, 21, 125, 106, 127, 38, 129, 254, 262, - /* 710 */ 263, 264, 265, 266, 177, 34, 226, 270, 271, 272, - /* 720 */ 225, 199, 200, 226, 146, 192, 226, 232, 281, 131, - /* 730 */ 132, 133, 73, 135, 0, 76, 67, 242, 140, 70, - /* 740 */ 73, 246, 204, 76, 205, 215, 148, 0, 150, 293, - /* 750 */ 152, 153, 154, 302, 235, 207, 22, 262, 263, 264, - /* 760 */ 265, 266, 287, 225, 261, 270, 271, 272, 67, 22, - /* 770 */ 232, 70, 104, 67, 176, 38, 70, 225, 204, 284, - /* 780 */ 242, 280, 196, 296, 246, 290, 291, 67, 67, 67, - /* 790 */ 70, 70, 70, 67, 283, 20, 70, 210, 155, 225, - /* 800 */ 262, 263, 264, 265, 266, 204, 232, 36, 270, 271, - /* 810 */ 272, 67, 257, 214, 70, 194, 242, 38, 67, 281, - /* 820 */ 246, 70, 136, 252, 210, 251, 225, 210, 19, 210, - /* 830 */ 115, 67, 240, 232, 70, 124, 262, 263, 264, 265, - /* 840 */ 238, 104, 33, 242, 238, 36, 210, 246, 20, 232, - /* 850 */ 256, 42, 67, 44, 67, 70, 282, 70, 67, 67, + /* 670 */ 65, 66, 225, 260, 260, 47, 246, 226, 209, 232, + /* 680 */ 246, 246, 246, 246, 246, 226, 246, 246, 36, 242, + /* 690 */ 194, 278, 278, 246, 174, 175, 227, 67, 73, 204, + /* 700 */ 70, 76, 21, 125, 106, 127, 254, 129, 205, 262, + /* 710 */ 263, 264, 265, 266, 215, 34, 104, 270, 271, 272, + /* 720 */ 225, 0, 0, 73, 146, 192, 76, 232, 281, 131, + /* 730 */ 132, 133, 73, 135, 0, 76, 73, 242, 140, 76, + /* 740 */ 235, 246, 204, 22, 22, 69, 148, 302, 150, 293, + /* 750 */ 152, 153, 154, 1, 2, 79, 22, 262, 263, 264, + /* 760 */ 265, 266, 287, 225, 207, 270, 271, 272, 67, 67, + /* 770 */ 232, 70, 70, 67, 176, 38, 70, 196, 204, 284, + /* 780 */ 242, 199, 200, 155, 246, 290, 291, 67, 177, 280, + /* 790 */ 70, 67, 67, 67, 70, 70, 70, 261, 225, 225, + /* 800 */ 262, 263, 264, 265, 266, 204, 232, 296, 270, 271, + /* 810 */ 272, 67, 67, 283, 70, 70, 242, 20, 67, 281, + /* 820 */ 246, 70, 36, 210, 257, 251, 225, 210, 19, 214, + /* 830 */ 136, 67, 38, 232, 70, 252, 262, 263, 264, 265, + /* 840 */ 240, 104, 33, 242, 210, 36, 210, 246, 115, 232, + /* 850 */ 210, 42, 67, 44, 67, 70, 282, 70, 67, 67, /* 860 */ 204, 70, 70, 262, 263, 264, 265, 266, 251, 295, - /* 870 */ 242, 270, 271, 299, 20, 250, 212, 68, 20, 232, - /* 880 */ 71, 225, 265, 67, 67, 243, 70, 70, 232, 212, - /* 890 */ 212, 212, 275, 276, 277, 210, 279, 20, 242, 282, - /* 900 */ 206, 225, 246, 210, 204, 225, 232, 225, 225, 204, - /* 910 */ 101, 256, 295, 225, 225, 225, 299, 225, 262, 263, - /* 920 */ 264, 265, 225, 225, 225, 225, 214, 214, 145, 206, - /* 930 */ 225, 20, 232, 242, 209, 126, 232, 232, 129, 255, - /* 940 */ 209, 184, 242, 292, 183, 250, 246, 242, 261, 249, - /* 950 */ 243, 246, 156, 144, 249, 146, 300, 301, 292, 191, - /* 960 */ 289, 247, 262, 263, 264, 265, 204, 262, 263, 264, - /* 970 */ 265, 204, 190, 246, 179, 247, 246, 178, 246, 175, - /* 980 */ 232, 20, 115, 273, 288, 195, 198, 225, 286, 260, - /* 990 */ 193, 246, 225, 285, 232, 69, 247, 247, 246, 232, - /* 1000 */ 246, 127, 269, 244, 242, 232, 303, 204, 246, 242, - /* 1010 */ 209, 209, 243, 246, 298, 204, 249, 69, 232, 221, - /* 1020 */ 214, 297, 228, 210, 262, 263, 264, 265, 225, 262, - /* 1030 */ 263, 264, 265, 214, 209, 232, 225, 206, 211, 219, - /* 1040 */ 219, 204, 253, 232, 202, 242, 0, 0, 60, 246, - /* 1050 */ 204, 38, 249, 242, 0, 151, 294, 246, 38, 38, - /* 1060 */ 38, 151, 225, 0, 38, 262, 263, 264, 265, 232, - /* 1070 */ 38, 225, 151, 262, 263, 264, 265, 204, 232, 242, - /* 1080 */ 0, 38, 0, 246, 0, 204, 38, 69, 242, 142, - /* 1090 */ 141, 104, 246, 138, 0, 0, 134, 0, 225, 262, - /* 1100 */ 263, 264, 265, 50, 0, 232, 225, 0, 262, 263, - /* 1110 */ 264, 265, 81, 232, 0, 242, 0, 0, 0, 246, + /* 870 */ 124, 270, 271, 299, 20, 238, 238, 68, 256, 20, + /* 880 */ 71, 225, 265, 67, 242, 250, 70, 212, 232, 232, + /* 890 */ 20, 243, 275, 276, 277, 212, 279, 212, 242, 282, + /* 900 */ 210, 212, 246, 20, 204, 206, 210, 225, 145, 204, + /* 910 */ 101, 225, 295, 225, 256, 255, 299, 225, 262, 263, + /* 920 */ 264, 265, 225, 225, 214, 225, 214, 232, 225, 225, + /* 930 */ 225, 20, 232, 225, 225, 126, 225, 232, 129, 209, + /* 940 */ 209, 206, 242, 250, 184, 232, 246, 242, 261, 249, + /* 950 */ 243, 246, 183, 144, 249, 146, 300, 301, 242, 292, + /* 960 */ 156, 247, 262, 263, 264, 265, 204, 262, 263, 264, + /* 970 */ 265, 204, 292, 246, 191, 247, 175, 179, 246, 190, + /* 980 */ 289, 232, 246, 178, 20, 69, 115, 225, 198, 260, + /* 990 */ 195, 273, 225, 193, 232, 247, 246, 244, 232, 232, + /* 1000 */ 246, 246, 285, 288, 242, 286, 247, 204, 246, 242, + /* 1010 */ 269, 127, 232, 246, 243, 204, 249, 209, 69, 209, + /* 1020 */ 221, 298, 210, 209, 262, 263, 264, 265, 225, 262, + /* 1030 */ 263, 264, 265, 214, 228, 232, 225, 297, 214, 206, + /* 1040 */ 219, 204, 303, 232, 219, 242, 253, 211, 0, 246, + /* 1050 */ 204, 202, 249, 242, 0, 60, 294, 246, 0, 38, + /* 1060 */ 151, 38, 225, 151, 38, 262, 263, 264, 265, 232, + /* 1070 */ 38, 225, 0, 262, 263, 264, 265, 204, 232, 242, + /* 1080 */ 38, 38, 151, 246, 0, 204, 0, 38, 242, 0, + /* 1090 */ 38, 69, 246, 142, 141, 104, 0, 138, 225, 262, + /* 1100 */ 263, 264, 265, 0, 134, 232, 225, 50, 262, 263, + /* 1110 */ 264, 265, 0, 232, 0, 242, 81, 0, 0, 246, /* 1120 */ 0, 204, 0, 242, 0, 0, 204, 246, 0, 0, - /* 1130 */ 0, 0, 115, 0, 0, 262, 263, 264, 265, 0, + /* 1130 */ 0, 0, 0, 0, 0, 262, 263, 264, 265, 115, /* 1140 */ 0, 0, 225, 262, 263, 264, 265, 225, 0, 232, /* 1150 */ 0, 0, 0, 0, 232, 0, 0, 22, 0, 242, - /* 1160 */ 0, 0, 204, 246, 242, 0, 43, 204, 246, 48, - /* 1170 */ 0, 0, 0, 204, 38, 43, 0, 0, 36, 262, - /* 1180 */ 263, 264, 265, 225, 262, 263, 264, 265, 225, 0, - /* 1190 */ 232, 78, 38, 67, 225, 232, 22, 0, 0, 0, - /* 1200 */ 242, 232, 0, 76, 246, 242, 38, 38, 22, 246, - /* 1210 */ 22, 242, 39, 38, 204, 246, 38, 38, 38, 0, - /* 1220 */ 262, 263, 264, 265, 0, 262, 263, 264, 265, 204, - /* 1230 */ 22, 262, 263, 264, 265, 225, 0, 22, 12, 13, - /* 1240 */ 204, 38, 232, 0, 22, 20, 0, 147, 22, 130, - /* 1250 */ 225, 38, 242, 0, 22, 204, 246, 232, 143, 57, - /* 1260 */ 22, 225, 0, 61, 38, 127, 0, 242, 232, 0, - /* 1270 */ 38, 246, 262, 263, 264, 265, 225, 130, 242, 130, - /* 1280 */ 4, 57, 246, 232, 82, 61, 69, 262, 263, 264, - /* 1290 */ 265, 43, 125, 242, 67, 19, 67, 246, 262, 263, - /* 1300 */ 264, 265, 67, 101, 102, 103, 82, 105, 69, 33, - /* 1310 */ 70, 70, 36, 262, 263, 264, 265, 41, 92, 69, - /* 1320 */ 44, 70, 70, 180, 92, 101, 102, 103, 69, 105, - /* 1330 */ 104, 67, 69, 174, 67, 81, 104, 180, 70, 81, - /* 1340 */ 70, 67, 81, 70, 68, 67, 180, 71, 4, 38, - /* 1350 */ 38, 38, 38, 38, 38, 2, 155, 70, 81, 69, - /* 1360 */ 69, 128, 70, 70, 138, 69, 69, 69, 0, 43, - /* 1370 */ 138, 69, 125, 79, 22, 69, 81, 70, 38, 38, - /* 1380 */ 81, 155, 156, 80, 38, 38, 69, 155, 156, 70, - /* 1390 */ 69, 22, 70, 69, 38, 70, 69, 38, 70, 69, - /* 1400 */ 69, 38, 94, 94, 94, 82, 69, 69, 104, 38, - /* 1410 */ 22, 47, 94, 48, 22, 38, 38, 38, 38, 38, - /* 1420 */ 38, 38, 22, 38, 38, 38, 0, 38, 38, 38, - /* 1430 */ 0, 38, 38, 38, 38, 36, 38, 0, 37, 0, - /* 1440 */ 43, 22, 21, 304, 22, 22, 21, 20, 304, 304, - /* 1450 */ 304, 304, 304, 304, 304, 304, 304, 304, 304, 304, - /* 1460 */ 304, 304, 304, 304, 304, 304, 304, 304, 304, 304, + /* 1160 */ 0, 0, 204, 246, 242, 0, 0, 204, 246, 0, + /* 1170 */ 43, 0, 0, 204, 48, 0, 38, 0, 36, 262, + /* 1180 */ 263, 264, 265, 225, 262, 263, 264, 265, 225, 43, + /* 1190 */ 232, 0, 0, 0, 225, 232, 22, 78, 0, 76, + /* 1200 */ 242, 232, 38, 67, 246, 242, 38, 0, 38, 246, + /* 1210 */ 22, 242, 0, 0, 204, 246, 38, 38, 38, 38, + /* 1220 */ 262, 263, 264, 265, 22, 262, 263, 264, 265, 204, + /* 1230 */ 22, 262, 263, 264, 265, 225, 39, 38, 12, 13, + /* 1240 */ 204, 22, 232, 0, 0, 22, 0, 20, 22, 0, + /* 1250 */ 225, 130, 242, 147, 38, 204, 246, 232, 0, 143, + /* 1260 */ 22, 225, 0, 22, 38, 0, 0, 242, 232, 130, + /* 1270 */ 127, 246, 262, 263, 264, 265, 225, 130, 242, 38, + /* 1280 */ 0, 204, 246, 232, 69, 43, 69, 262, 263, 264, + /* 1290 */ 265, 67, 4, 242, 69, 67, 67, 246, 262, 263, + /* 1300 */ 264, 265, 225, 70, 70, 69, 57, 19, 125, 232, + /* 1310 */ 61, 81, 67, 262, 263, 264, 265, 180, 92, 242, + /* 1320 */ 70, 33, 57, 246, 36, 69, 61, 67, 81, 41, + /* 1330 */ 104, 82, 44, 92, 180, 81, 174, 70, 70, 262, + /* 1340 */ 263, 264, 265, 67, 70, 104, 67, 82, 4, 38, + /* 1350 */ 101, 102, 103, 180, 105, 38, 68, 38, 38, 71, + /* 1360 */ 38, 38, 2, 81, 138, 155, 101, 102, 103, 70, + /* 1370 */ 105, 69, 69, 0, 70, 70, 69, 69, 69, 138, + /* 1380 */ 128, 155, 156, 43, 125, 69, 22, 81, 70, 79, + /* 1390 */ 69, 38, 38, 81, 80, 69, 155, 156, 70, 38, + /* 1400 */ 69, 38, 70, 69, 38, 70, 69, 38, 70, 94, + /* 1410 */ 69, 22, 82, 38, 69, 69, 69, 104, 94, 94, + /* 1420 */ 38, 94, 22, 22, 48, 47, 38, 22, 38, 38, + /* 1430 */ 38, 38, 38, 38, 38, 38, 38, 38, 0, 0, + /* 1440 */ 38, 38, 38, 0, 38, 38, 38, 36, 38, 37, + /* 1450 */ 0, 22, 43, 21, 304, 22, 22, 21, 304, 304, + /* 1460 */ 20, 304, 304, 304, 304, 304, 304, 304, 304, 304, /* 1470 */ 304, 304, 304, 304, 304, 304, 304, 304, 304, 304, /* 1480 */ 304, 304, 304, 304, 304, 304, 304, 304, 304, 304, /* 1490 */ 304, 304, 304, 304, 304, 304, 304, 304, 304, 304, @@ -522,91 +524,93 @@ static const YYCODETYPE yy_lookahead[] = { /* 1610 */ 304, 304, 304, 304, 304, 304, 304, 304, 304, 304, /* 1620 */ 304, 304, 304, 304, 304, 304, 304, 304, 304, 304, /* 1630 */ 304, 304, 304, 304, 304, 304, 304, 304, 304, 304, - /* 1640 */ 304, 304, 304, 304, 304, 304, 304, 304, 304, + /* 1640 */ 304, 304, 304, 304, 304, 304, 304, 304, 304, 304, + /* 1650 */ 304, 304, 304, 304, 304, 304, 304, 304, 304, 304, + /* 1660 */ 304, 304, }; #define YY_SHIFT_COUNT (510) #define YY_SHIFT_MIN (0) -#define YY_SHIFT_MAX (1439) +#define YY_SHIFT_MAX (1450) static const unsigned short int yy_shift_ofst[] = { /* 0 */ 598, 0, 39, 78, 78, 78, 78, 234, 78, 78, /* 10 */ 312, 468, 120, 273, 312, 312, 312, 312, 312, 312, /* 20 */ 312, 312, 312, 312, 312, 312, 312, 312, 312, 312, - /* 30 */ 312, 312, 312, 206, 206, 206, 159, 1226, 1226, 34, - /* 40 */ 81, 81, 125, 1226, 81, 81, 81, 81, 81, 81, - /* 50 */ 360, 395, 465, 465, 125, 529, 395, 81, 81, 395, - /* 60 */ 81, 395, 529, 395, 395, 81, 512, 148, 431, 411, - /* 70 */ 411, 252, 425, 1232, 240, 1232, 1232, 1232, 1232, 1232, - /* 80 */ 1232, 1232, 1232, 1232, 1232, 1232, 1232, 1232, 1232, 1232, - /* 90 */ 1232, 1232, 1232, 1232, 38, 409, 23, 23, 23, 650, - /* 100 */ 568, 529, 395, 395, 395, 597, 271, 271, 271, 271, - /* 110 */ 271, 271, 809, 288, 402, 372, 119, 477, 63, 663, - /* 120 */ 432, 444, 267, 444, 595, 533, 537, 775, 771, 779, - /* 130 */ 686, 775, 775, 715, 711, 711, 775, 828, 529, 854, - /* 140 */ 360, 568, 858, 360, 360, 775, 360, 877, 395, 395, - /* 150 */ 395, 395, 395, 395, 395, 395, 395, 395, 395, 779, - /* 160 */ 779, 775, 877, 568, 828, 783, 529, 854, 512, 568, - /* 170 */ 858, 512, 911, 757, 761, 796, 757, 761, 796, 796, - /* 180 */ 768, 782, 795, 799, 804, 568, 961, 867, 788, 790, - /* 190 */ 797, 926, 395, 761, 796, 796, 761, 796, 874, 568, - /* 200 */ 858, 512, 597, 512, 568, 948, 779, 779, 775, 512, - /* 210 */ 877, 1448, 1448, 1448, 1448, 1448, 605, 578, 491, 1276, - /* 220 */ 1202, 1224, 16, 108, 333, 136, 136, 136, 136, 136, - /* 230 */ 136, 136, 149, 36, 482, 426, 359, 359, 359, 359, - /* 240 */ 383, 361, 608, 622, 624, 659, 667, 590, 734, 747, - /* 250 */ 681, 669, 701, 706, 646, 522, 621, 586, 720, 643, - /* 260 */ 721, 100, 722, 726, 744, 751, 764, 668, 737, 785, - /* 270 */ 787, 791, 792, 816, 817, 184, 1046, 1047, 988, 1054, - /* 280 */ 1013, 904, 1020, 1021, 1022, 910, 1063, 1026, 1032, 921, - /* 290 */ 1080, 1043, 1082, 1048, 1084, 1018, 947, 949, 987, 955, - /* 300 */ 1094, 1095, 1053, 962, 1097, 1104, 1031, 1107, 1114, 1116, - /* 310 */ 1117, 1118, 1120, 1122, 1124, 1125, 1128, 1129, 1130, 1131, - /* 320 */ 1017, 1133, 1134, 1139, 1140, 1141, 1148, 1135, 1150, 1151, - /* 330 */ 1152, 1153, 1155, 1156, 1158, 1160, 1161, 1123, 1165, 1121, - /* 340 */ 1170, 1171, 1136, 1142, 1132, 1172, 1176, 1177, 1189, 1113, - /* 350 */ 1127, 1154, 1126, 1174, 1197, 1168, 1169, 1175, 1178, 1126, - /* 360 */ 1179, 1180, 1198, 1186, 1199, 1188, 1173, 1219, 1208, 1203, - /* 370 */ 1236, 1215, 1243, 1222, 1225, 1246, 1119, 1100, 1213, 1253, - /* 380 */ 1115, 1238, 1147, 1138, 1262, 1266, 1149, 1269, 1217, 1248, - /* 390 */ 1167, 1227, 1229, 1143, 1240, 1235, 1241, 1239, 1250, 1251, - /* 400 */ 1259, 1252, 1264, 1254, 1263, 1267, 1157, 1268, 1270, 1258, - /* 410 */ 1159, 1274, 1261, 1273, 1278, 1166, 1344, 1311, 1312, 1313, - /* 420 */ 1314, 1315, 1316, 1353, 1201, 1277, 1287, 1290, 1291, 1292, - /* 430 */ 1293, 1296, 1297, 1233, 1298, 1368, 1326, 1247, 1302, 1294, - /* 440 */ 1295, 1299, 1352, 1306, 1303, 1307, 1340, 1341, 1317, 1319, - /* 450 */ 1346, 1321, 1322, 1347, 1324, 1325, 1356, 1327, 1328, 1359, - /* 460 */ 1330, 1308, 1309, 1310, 1318, 1369, 1323, 1331, 1363, 1304, - /* 470 */ 1337, 1338, 1371, 1126, 1388, 1365, 1364, 1392, 1377, 1378, - /* 480 */ 1379, 1380, 1381, 1382, 1383, 1400, 1385, 1126, 1386, 1387, - /* 490 */ 1389, 1390, 1391, 1393, 1394, 1395, 1426, 1396, 1399, 1397, - /* 500 */ 1430, 1398, 1401, 1437, 1439, 1419, 1421, 1422, 1423, 1425, - /* 510 */ 1427, + /* 30 */ 312, 312, 312, 312, 206, 206, 206, 159, 1226, 1226, + /* 40 */ 34, 81, 81, 15, 1226, 81, 81, 81, 81, 81, + /* 50 */ 81, 207, 324, 359, 15, 389, 324, 81, 81, 324, + /* 60 */ 81, 324, 389, 324, 324, 81, 284, 148, 431, 411, + /* 70 */ 411, 252, 425, 1241, 240, 1241, 1241, 1241, 1241, 1241, + /* 80 */ 1241, 1241, 1241, 1241, 1241, 1241, 1241, 1241, 1241, 1241, + /* 90 */ 1241, 1241, 1241, 1241, 38, 217, 23, 23, 23, 244, + /* 100 */ 465, 389, 324, 324, 324, 478, 271, 271, 271, 271, + /* 110 */ 271, 271, 809, 288, 402, 372, 119, 163, 201, 528, + /* 120 */ 432, 520, 399, 520, 599, 533, 611, 797, 786, 794, + /* 130 */ 694, 797, 797, 733, 746, 746, 797, 854, 389, 859, + /* 140 */ 207, 465, 870, 207, 207, 797, 207, 883, 324, 324, + /* 150 */ 324, 324, 324, 324, 324, 324, 324, 324, 324, 794, + /* 160 */ 794, 797, 883, 465, 854, 763, 389, 859, 284, 465, + /* 170 */ 870, 284, 911, 760, 769, 804, 760, 769, 804, 804, + /* 180 */ 783, 789, 798, 805, 801, 465, 964, 871, 790, 795, + /* 190 */ 800, 916, 324, 769, 804, 804, 769, 804, 884, 465, + /* 200 */ 870, 284, 478, 284, 465, 949, 794, 794, 797, 284, + /* 210 */ 883, 1461, 1461, 1461, 1461, 1461, 605, 578, 491, 1288, + /* 220 */ 1249, 1265, 333, 16, 108, 577, 136, 136, 136, 136, + /* 230 */ 136, 136, 136, 307, 36, 482, 369, 593, 593, 593, + /* 240 */ 593, 652, 361, 630, 625, 650, 659, 663, 721, 722, + /* 250 */ 734, 681, 701, 702, 706, 752, 582, 496, 581, 720, + /* 260 */ 628, 724, 100, 725, 726, 744, 745, 751, 612, 737, + /* 270 */ 764, 785, 787, 791, 792, 816, 676, 1048, 1054, 995, + /* 280 */ 1058, 1021, 909, 1023, 1026, 1032, 912, 1072, 1042, 1043, + /* 290 */ 931, 1084, 1049, 1086, 1052, 1089, 1022, 951, 953, 991, + /* 300 */ 959, 1096, 1103, 1057, 970, 1112, 1114, 1035, 1117, 1118, + /* 310 */ 1120, 1122, 1124, 1125, 1128, 1129, 1130, 1131, 1132, 1133, + /* 320 */ 1134, 1024, 1140, 1141, 1148, 1150, 1151, 1152, 1135, 1153, + /* 330 */ 1155, 1156, 1158, 1160, 1161, 1165, 1166, 1169, 1127, 1171, + /* 340 */ 1126, 1172, 1175, 1138, 1142, 1146, 1177, 1191, 1192, 1193, + /* 350 */ 1119, 1123, 1164, 1136, 1174, 1198, 1168, 1170, 1178, 1179, + /* 360 */ 1136, 1180, 1181, 1207, 1188, 1212, 1202, 1197, 1213, 1208, + /* 370 */ 1199, 1243, 1219, 1244, 1223, 1227, 1246, 1121, 1106, 1216, + /* 380 */ 1258, 1116, 1238, 1139, 1143, 1262, 1266, 1147, 1280, 1215, + /* 390 */ 1242, 1183, 1224, 1228, 1137, 1233, 1229, 1234, 1217, 1225, + /* 400 */ 1236, 1250, 1245, 1230, 1256, 1260, 1154, 1267, 1268, 1247, + /* 410 */ 1162, 1276, 1254, 1274, 1279, 1173, 1344, 1311, 1317, 1319, + /* 420 */ 1320, 1322, 1323, 1360, 1210, 1282, 1299, 1302, 1303, 1304, + /* 430 */ 1305, 1307, 1308, 1252, 1309, 1373, 1340, 1259, 1316, 1310, + /* 440 */ 1306, 1312, 1364, 1321, 1314, 1318, 1353, 1354, 1326, 1328, + /* 450 */ 1361, 1331, 1332, 1363, 1334, 1335, 1366, 1337, 1338, 1369, + /* 460 */ 1341, 1315, 1324, 1325, 1327, 1389, 1330, 1345, 1375, 1313, + /* 470 */ 1346, 1347, 1382, 1136, 1400, 1376, 1378, 1401, 1388, 1390, + /* 480 */ 1391, 1392, 1393, 1394, 1395, 1405, 1396, 1136, 1397, 1398, + /* 490 */ 1399, 1402, 1403, 1404, 1406, 1407, 1438, 1408, 1411, 1409, + /* 500 */ 1439, 1410, 1412, 1443, 1450, 1429, 1432, 1433, 1434, 1436, + /* 510 */ 1440, }; #define YY_REDUCE_COUNT (215) #define YY_REDUCE_MIN (-226) -#define YY_REDUCE_MAX (1051) +#define YY_REDUCE_MAX (1077) static const short yy_reduce_ofst[] = { /* 0 */ -180, 262, 495, -198, -58, 447, 538, 574, -129, 601, /* 10 */ 656, -14, 617, 700, 705, 762, 176, 767, 803, 811, /* 20 */ 837, 846, 873, 881, 917, 922, 958, 963, 969, 1010, - /* 30 */ 1025, 1036, 1051, -195, -170, -147, 197, -213, -204, 41, - /* 40 */ -209, -206, 247, -132, -208, -175, -1, 11, 111, 121, - /* 50 */ 318, -142, -176, -40, 118, -62, -214, 189, 335, -6, - /* 60 */ 354, 232, 110, 97, 314, 355, -55, -201, -156, -156, - /* 70 */ -156, 173, -185, -107, 126, 208, 259, 308, 310, 339, - /* 80 */ 362, 371, 388, 389, 390, 391, 406, 434, 435, 436, - /* 90 */ 437, 438, 440, 441, 122, 281, 329, 413, 414, 467, - /* 100 */ -10, 172, 65, -149, 338, 161, -226, -221, 387, 490, - /* 110 */ 497, 500, 454, 539, 530, 451, 456, 519, 475, 548, - /* 120 */ 503, 501, 501, 501, 552, 487, 511, 587, 555, 599, - /* 130 */ 571, 614, 619, 592, 602, 606, 636, 594, 628, 625, - /* 140 */ 664, 647, 642, 677, 678, 685, 679, 694, 676, 680, - /* 150 */ 682, 683, 688, 689, 690, 692, 697, 698, 699, 712, - /* 160 */ 713, 693, 723, 674, 655, 684, 691, 695, 725, 704, - /* 170 */ 707, 731, 687, 651, 714, 727, 666, 728, 730, 732, - /* 180 */ 671, 696, 702, 708, 501, 748, 729, 710, 703, 716, - /* 190 */ 724, 733, 552, 749, 745, 752, 750, 754, 759, 773, - /* 200 */ 769, 801, 798, 802, 786, 794, 806, 819, 813, 825, - /* 210 */ 831, 789, 820, 821, 827, 842, + /* 30 */ 1025, 1036, 1051, 1077, -195, -170, -147, 197, -213, -204, + /* 40 */ 41, -209, -206, 247, -132, -208, -175, -1, 11, 111, + /* 50 */ 121, 318, -142, -176, 118, -62, -214, 189, 335, -6, + /* 60 */ 354, 232, -49, 97, 314, 355, -55, -201, -156, -156, + /* 70 */ -156, 171, -185, -107, 126, 106, 169, 208, 259, 308, + /* 80 */ 310, 339, 364, 371, 390, 391, 430, 434, 435, 436, + /* 90 */ 437, 438, 440, 441, 212, 329, 375, 413, 414, 469, + /* 100 */ -10, 370, 181, -149, 313, 80, -226, -221, 229, 362, + /* 110 */ 451, 459, 452, 503, 499, 445, 456, 505, 475, 557, + /* 120 */ 536, 509, 509, 509, 573, 511, 530, 613, 567, 615, + /* 130 */ 583, 634, 636, 600, 637, 638, 640, 622, 642, 635, + /* 140 */ 675, 657, 648, 683, 685, 690, 689, 699, 682, 686, + /* 150 */ 688, 692, 697, 698, 703, 704, 708, 709, 711, 710, + /* 160 */ 712, 696, 735, 695, 658, 660, 716, 693, 730, 713, + /* 170 */ 707, 731, 687, 667, 714, 727, 680, 728, 732, 736, + /* 180 */ 691, 715, 719, 717, 509, 749, 729, 718, 739, 723, + /* 190 */ 740, 741, 573, 748, 750, 754, 759, 755, 753, 766, + /* 200 */ 771, 808, 799, 810, 780, 806, 819, 824, 812, 814, + /* 210 */ 833, 793, 821, 825, 836, 849, }; static const YYACTIONTYPE yy_default[] = { /* 0 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, @@ -614,7 +618,7 @@ static const YYACTIONTYPE yy_default[] = { /* 20 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, /* 30 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, /* 40 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, - /* 50 */ 1205, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, + /* 50 */ 1152, 1205, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, /* 60 */ 1152, 1152, 1152, 1152, 1152, 1152, 1203, 1332, 1152, 1464, /* 70 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, /* 80 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, @@ -631,11 +635,11 @@ static const YYACTIONTYPE yy_default[] = { /* 190 */ 1526, 1152, 1152, 1504, 1152, 1152, 1504, 1152, 1345, 1152, /* 200 */ 1152, 1203, 1152, 1203, 1152, 1259, 1152, 1152, 1152, 1203, /* 210 */ 1152, 1360, 1293, 1293, 1206, 1157, 1152, 1152, 1152, 1152, - /* 220 */ 1152, 1152, 1152, 1152, 1152, 1428, 1517, 1516, 1427, 1441, - /* 230 */ 1440, 1439, 1152, 1152, 1152, 1152, 1422, 1423, 1421, 1420, - /* 240 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, - /* 250 */ 1152, 1152, 1152, 1152, 1465, 1152, 1527, 1531, 1152, 1152, - /* 260 */ 1152, 1405, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, + /* 220 */ 1152, 1152, 1152, 1152, 1152, 1152, 1428, 1517, 1516, 1427, + /* 230 */ 1441, 1440, 1439, 1152, 1152, 1152, 1152, 1422, 1423, 1421, + /* 240 */ 1420, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, + /* 250 */ 1152, 1152, 1152, 1152, 1152, 1465, 1152, 1527, 1531, 1152, + /* 260 */ 1152, 1152, 1405, 1152, 1152, 1152, 1152, 1152, 1152, 1152, /* 270 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, /* 280 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, /* 290 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, @@ -644,11 +648,11 @@ static const YYACTIONTYPE yy_default[] = { /* 320 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, /* 330 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, /* 340 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, - /* 350 */ 1152, 1152, 1304, 1152, 1152, 1152, 1152, 1152, 1152, 1229, - /* 360 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, + /* 350 */ 1152, 1152, 1152, 1304, 1152, 1152, 1152, 1152, 1152, 1152, + /* 360 */ 1229, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, /* 370 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, /* 380 */ 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, 1152, - /* 390 */ 1152, 1488, 1498, 1152, 1152, 1152, 1152, 1152, 1152, 1152, + /* 390 */ 1152, 1152, 1488, 1498, 1152, 1152, 1152, 1152, 1152, 1152, /* 400 */ 1152, 1152, 1152, 1405, 1152, 1515, 1152, 1474, 1470, 1152, /* 410 */ 1152, 1466, 1152, 1152, 1525, 1152, 1152, 1152, 1152, 1152, /* 420 */ 1152, 1152, 1152, 1460, 1152, 1152, 1152, 1152, 1152, 1152, @@ -1424,7 +1428,7 @@ static const char *const yyRuleName[] = { /* 343 */ "partition_by_clause_opt ::= PARTITION BY expression_list", /* 344 */ "twindow_clause_opt ::=", /* 345 */ "twindow_clause_opt ::= SESSION NK_LP column_reference NK_COMMA duration_literal NK_RP", - /* 346 */ "twindow_clause_opt ::= STATE_WINDOW NK_LP column_reference NK_RP", + /* 346 */ "twindow_clause_opt ::= STATE_WINDOW NK_LP expression NK_RP", /* 347 */ "twindow_clause_opt ::= INTERVAL NK_LP duration_literal NK_RP sliding_opt fill_opt", /* 348 */ "twindow_clause_opt ::= INTERVAL NK_LP duration_literal NK_COMMA duration_literal NK_RP sliding_opt fill_opt", /* 349 */ "sliding_opt ::=", @@ -2385,7 +2389,7 @@ static const struct { { 286, -3 }, /* (343) partition_by_clause_opt ::= PARTITION BY expression_list */ { 287, 0 }, /* (344) twindow_clause_opt ::= */ { 287, -6 }, /* (345) twindow_clause_opt ::= SESSION NK_LP column_reference NK_COMMA duration_literal NK_RP */ - { 287, -4 }, /* (346) twindow_clause_opt ::= STATE_WINDOW NK_LP column_reference NK_RP */ + { 287, -4 }, /* (346) twindow_clause_opt ::= STATE_WINDOW NK_LP expression NK_RP */ { 287, -6 }, /* (347) twindow_clause_opt ::= INTERVAL NK_LP duration_literal NK_RP sliding_opt fill_opt */ { 287, -8 }, /* (348) twindow_clause_opt ::= INTERVAL NK_LP duration_literal NK_COMMA duration_literal NK_RP sliding_opt fill_opt */ { 247, 0 }, /* (349) sliding_opt ::= */ @@ -3590,7 +3594,7 @@ static YYACTIONTYPE yy_reduce( case 345: /* twindow_clause_opt ::= SESSION NK_LP column_reference NK_COMMA duration_literal NK_RP */ { yymsp[-5].minor.yy104 = createSessionWindowNode(pCxt, releaseRawExprNode(pCxt, yymsp[-3].minor.yy104), releaseRawExprNode(pCxt, yymsp[-1].minor.yy104)); } break; - case 346: /* twindow_clause_opt ::= STATE_WINDOW NK_LP column_reference NK_RP */ + case 346: /* twindow_clause_opt ::= STATE_WINDOW NK_LP expression NK_RP */ { yymsp[-3].minor.yy104 = createStateWindowNode(pCxt, releaseRawExprNode(pCxt, yymsp[-1].minor.yy104)); } break; case 347: /* twindow_clause_opt ::= INTERVAL NK_LP duration_literal NK_RP sliding_opt fill_opt */ diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index 149071bad2..819eb6d699 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -477,6 +477,18 @@ static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStm return code; } +static int32_t createWindowLogicNodeByState(SLogicPlanContext* pCxt, SStateWindowNode* pState, 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_STATE; + pWindow->pStateExpr = nodesCloneNode(pState->pExpr); + + return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode); +} + static int32_t createWindowLogicNodeBySession(SLogicPlanContext* pCxt, SSessionWindowNode* pSession, SSelectStmt* pSelect, SLogicNode** pLogicNode) { SWindowLogicNode* pWindow = nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW); if (NULL == pWindow) { @@ -525,6 +537,8 @@ static int32_t createWindowLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSele } switch (nodeType(pSelect->pWindow)) { + case QUERY_NODE_STATE_WINDOW: + return createWindowLogicNodeByState(pCxt, (SStateWindowNode*)pSelect->pWindow, pSelect, pLogicNode); case QUERY_NODE_SESSION_WINDOW: return createWindowLogicNodeBySession(pCxt, (SSessionWindowNode*)pSelect->pWindow, pSelect, pLogicNode); case QUERY_NODE_INTERVAL_WINDOW: @@ -642,6 +656,29 @@ static int32_t createPartitionLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pS return TSDB_CODE_OUT_OF_MEMORY; } + SNodeList* pCols = NULL; + int32_t code = nodesCollectColumns(pSelect, SQL_CLAUSE_PARTITION_BY, NULL, &pCols); + if (TSDB_CODE_SUCCESS == code && NULL != pCols) { + pPartition->node.pTargets = nodesCloneList(pCols); + if (NULL == pPartition->node.pTargets) { + code = TSDB_CODE_OUT_OF_MEMORY; + } + } + + if (TSDB_CODE_SUCCESS == code) { + pPartition->pPartitionKeys = nodesCloneList(pSelect->pPartitionByList); + if (NULL == pPartition->pPartitionKeys) { + code = TSDB_CODE_OUT_OF_MEMORY; + } + } + + if (TSDB_CODE_SUCCESS == code) { + *pLogicNode = (SLogicNode*)pPartition; + } else { + nodesDestroyNode(pPartition); + } + + return code; return TSDB_CODE_SUCCESS; } @@ -650,7 +687,35 @@ static int32_t createDistinctLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSe return TSDB_CODE_SUCCESS; } - return TSDB_CODE_SUCCESS; + SAggLogicNode* pAgg = (SAggLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_AGG); + if (NULL == pAgg) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + int32_t code = TSDB_CODE_SUCCESS; + // set grouyp keys, agg funcs and having conditions + pAgg->pGroupKeys = nodesCloneList(pSelect->pProjectionList); + if (NULL == pAgg->pGroupKeys) { + code = TSDB_CODE_OUT_OF_MEMORY; + } + + // rewrite the expression in subsequent clauses + if (TSDB_CODE_SUCCESS == code) { + code = rewriteExpr(pAgg->pGroupKeys, pSelect, SQL_CLAUSE_SELECT); + } + + // set the output + if (TSDB_CODE_SUCCESS == code) { + code = createColumnByRewriteExps(pCxt, pAgg->pGroupKeys, &pAgg->node.pTargets); + } + + if (TSDB_CODE_SUCCESS == code) { + *pLogicNode = (SLogicNode*)pAgg; + } else { + nodesDestroyNode(pAgg); + } + + return code; } static int32_t createSelectLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) { diff --git a/source/libs/planner/src/planPhysiCreater.c b/source/libs/planner/src/planPhysiCreater.c index 04fe0d7da4..965c5c9579 100644 --- a/source/libs/planner/src/planPhysiCreater.c +++ b/source/libs/planner/src/planPhysiCreater.c @@ -176,6 +176,10 @@ static int16_t getUnsetSlotId(const SArray* pSlotIdsInfo) { } static int32_t addDataBlockSlotsImpl(SPhysiPlanContext* pCxt, SNodeList* pList, SDataBlockDescNode* pDataBlockDesc, const char* pStmtName, bool output) { + if (NULL == pList) { + return TSDB_CODE_SUCCESS; + } + int32_t code = TSDB_CODE_SUCCESS; SHashObj* pHash = taosArrayGetP(pCxt->pLocationHelper, pDataBlockDesc->dataBlockId); int16_t nextSlotId = taosHashGetSize(pHash), slotId = 0; @@ -219,6 +223,23 @@ static int32_t addDataBlockSlots(SPhysiPlanContext* pCxt, SNodeList* pList, SDat return addDataBlockSlotsImpl(pCxt, pList, pDataBlockDesc, NULL, false); } +static int32_t addDataBlockSlot(SPhysiPlanContext* pCxt, SNode** pNode, SDataBlockDescNode* pDataBlockDesc) { + if (NULL == pNode || NULL == *pNode) { + return TSDB_CODE_SUCCESS; + } + + SNodeList* pList = NULL; + int32_t code = nodesListMakeAppend(&pList, *pNode); + if (TSDB_CODE_SUCCESS == code) { + code = addDataBlockSlots(pCxt, pList, pDataBlockDesc); + } + if (TSDB_CODE_SUCCESS == code) { + *pNode = nodesListGetNode(pList, 0); + } + nodesClearList(pList); + return code; +} + static int32_t addDataBlockSlotsForProject(SPhysiPlanContext* pCxt, const char* pStmtName, SNodeList* pList, SDataBlockDescNode* pDataBlockDesc) { return addDataBlockSlotsImpl(pCxt, pList, pDataBlockDesc, pStmtName, true); } @@ -244,6 +265,7 @@ static EDealRes doSetSlotId(SNode* pNode, void* pContext) { } // pIndex is definitely not NULL, otherwise it is a bug if (NULL == pIndex) { + pCxt->errCode = TSDB_CODE_PLAN_INTERNAL_ERROR; return DEAL_RES_ERROR; } ((SColumnNode*)pNode)->dataBlockId = pIndex->dataBlockId; @@ -614,6 +636,25 @@ static int32_t rewritePrecalcExprs(SPhysiPlanContext* pCxt, SNodeList* pList, SN return cxt.errCode; } +static int32_t rewritePrecalcExpr(SPhysiPlanContext* pCxt, SNode* pNode, SNodeList** pPrecalcExprs, SNode** pRewritten) { + if (NULL == pNode) { + return TSDB_CODE_SUCCESS; + } + + SNodeList* pList = NULL; + int32_t code = nodesListMakeAppend(&pList, pNode); + SNodeList* pRewrittenList = NULL; + if (TSDB_CODE_SUCCESS == code) { + code = rewritePrecalcExprs(pCxt, pList, pPrecalcExprs, &pRewrittenList); + } + if (TSDB_CODE_SUCCESS == code) { + *pRewritten = nodesListGetNode(pRewrittenList, 0); + } + nodesClearList(pList); + nodesClearList(pRewrittenList); + return code; +} + static int32_t createAggPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SAggLogicNode* pAggLogicNode, SPhysiNode** pPhyNode) { SAggPhysiNode* pAgg = (SAggPhysiNode*)makePhysiNode(pCxt, (SLogicNode*)pAggLogicNode, QUERY_NODE_PHYSICAL_PLAN_AGG); if (NULL == pAgg) { @@ -818,6 +859,40 @@ static int32_t createSessionWindowPhysiNode(SPhysiPlanContext* pCxt, SNodeList* return createWindowPhysiNodeFinalize(pCxt, pChildren, &pSession->window, pWindowLogicNode, pPhyNode); } +static int32_t createStateWindowPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SWindowLogicNode* pWindowLogicNode, SPhysiNode** pPhyNode) { + SStateWinodwPhysiNode* pState = (SStateWinodwPhysiNode*)makePhysiNode(pCxt, (SLogicNode*)pWindowLogicNode, QUERY_NODE_PHYSICAL_PLAN_STATE_WINDOW); + if (NULL == pState) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + SNodeList* pPrecalcExprs = NULL; + SNode* pStateKey = NULL; + int32_t code = rewritePrecalcExpr(pCxt, pWindowLogicNode->pStateExpr, &pPrecalcExprs, &pStateKey); + + 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, &pState->window.pExprs); + if (TSDB_CODE_SUCCESS == code) { + code = addDataBlockSlots(pCxt, pState->window.pExprs, pChildTupe); + } + } + + if (TSDB_CODE_SUCCESS == code) { + code = setNodeSlotId(pCxt, pChildTupe->dataBlockId, -1, pStateKey, &pState->pStateKey); + if (TSDB_CODE_SUCCESS == code) { + code = addDataBlockSlot(pCxt, &pState->pStateKey, pState->window.node.pOutputDataBlockDesc); + } + } + + if (TSDB_CODE_SUCCESS != code) { + nodesDestroyNode(pState); + return code; + } + + return createWindowPhysiNodeFinalize(pCxt, pChildren, &pState->window, pWindowLogicNode, pPhyNode); +} + static int32_t createWindowPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SWindowLogicNode* pWindowLogicNode, SPhysiNode** pPhyNode) { switch (pWindowLogicNode->winType) { case WINDOW_TYPE_INTERVAL: @@ -825,7 +900,7 @@ static int32_t createWindowPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildr case WINDOW_TYPE_SESSION: return createSessionWindowPhysiNode(pCxt, pChildren, pWindowLogicNode, pPhyNode); case WINDOW_TYPE_STATE: - break; + return createStateWindowPhysiNode(pCxt, pChildren, pWindowLogicNode, pPhyNode); default: break; } @@ -867,6 +942,41 @@ static int32_t createSortPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren return code; } +static int32_t createPartitionPhysiNode(SPhysiPlanContext* pCxt, SNodeList* pChildren, SPartitionLogicNode* pPartLogicNode, SPhysiNode** pPhyNode) { + SPartitionPhysiNode* pPart = (SPartitionPhysiNode*)makePhysiNode(pCxt, (SLogicNode*)pPartLogicNode, QUERY_NODE_PHYSICAL_PLAN_PARTITION); + if (NULL == pPart) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + SNodeList* pPrecalcExprs = NULL; + SNodeList* pPartitionKeys = NULL; + int32_t code = rewritePrecalcExprs(pCxt, pPartLogicNode->pPartitionKeys, &pPrecalcExprs, &pPartitionKeys); + + 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, &pPart->pExprs); + if (TSDB_CODE_SUCCESS == code) { + code = addDataBlockSlots(pCxt, pPart->pExprs, pChildTupe); + } + } + + if (TSDB_CODE_SUCCESS == code) { + code = setListSlotId(pCxt, pChildTupe->dataBlockId, -1, pPartitionKeys, &pPart->pPartitionKeys); + if (TSDB_CODE_SUCCESS == code) { + code = addDataBlockSlots(pCxt, pPart->pPartitionKeys, pPart->node.pOutputDataBlockDesc); + } + } + + if (TSDB_CODE_SUCCESS == code) { + *pPhyNode = (SPhysiNode*)pPart; + } else { + nodesDestroyNode(pPart); + } + + return code; +} + static int32_t doCreatePhysiNode(SPhysiPlanContext* pCxt, SLogicNode* pLogicNode, SSubplan* pSubplan, SNodeList* pChildren, SPhysiNode** pPhyNode) { switch (nodeType(pLogicNode)) { case QUERY_NODE_LOGIC_PLAN_SCAN: @@ -883,6 +993,8 @@ static int32_t doCreatePhysiNode(SPhysiPlanContext* pCxt, SLogicNode* pLogicNode return createWindowPhysiNode(pCxt, pChildren, (SWindowLogicNode*)pLogicNode, pPhyNode); case QUERY_NODE_LOGIC_PLAN_SORT: return createSortPhysiNode(pCxt, pChildren, (SSortLogicNode*)pLogicNode, pPhyNode); + case QUERY_NODE_LOGIC_PLAN_PARTITION: + return createPartitionPhysiNode(pCxt, pChildren, (SPartitionLogicNode*)pLogicNode, pPhyNode); default: break; } diff --git a/source/libs/planner/test/plannerTest.cpp b/source/libs/planner/test/plannerTest.cpp index 32b221c4fb..7f79fc6a5a 100644 --- a/source/libs/planner/test/plannerTest.cpp +++ b/source/libs/planner/test/plannerTest.cpp @@ -217,6 +217,32 @@ TEST_F(PlannerTest, sessionWindow) { ASSERT_TRUE(run()); } +TEST_F(PlannerTest, stateWindow) { + setDatabase("root", "test"); + + bind("SELECT count(*) FROM t1 state_window(c1)"); + ASSERT_TRUE(run()); + + bind("SELECT count(*) FROM t1 state_window(c1 + 10)"); + ASSERT_TRUE(run()); +} + +TEST_F(PlannerTest, partitionBy) { + setDatabase("root", "test"); + + bind("SELECT * FROM t1 partition by c1"); + ASSERT_TRUE(run()); + + bind("SELECT count(*) FROM t1 partition by c1"); + ASSERT_TRUE(run()); + + bind("SELECT count(*) FROM t1 partition by c1 group by c2"); + ASSERT_TRUE(run()); + + bind("SELECT count(*) FROM st1 partition by tag1, tag2 interval(10s)"); + ASSERT_TRUE(run()); +} + TEST_F(PlannerTest, orderBy) { setDatabase("root", "test"); @@ -230,6 +256,19 @@ TEST_F(PlannerTest, orderBy) { ASSERT_TRUE(run()); } +TEST_F(PlannerTest, distinct) { + setDatabase("root", "test"); + + bind("SELECT distinct c1 FROM t1"); + ASSERT_TRUE(run()); + + bind("SELECT distinct c1, c2 + 10 FROM t1"); + ASSERT_TRUE(run()); + + bind("SELECT distinct c1 + 10 a FROM t1 order by a"); + ASSERT_TRUE(run()); +} + TEST_F(PlannerTest, limit) { setDatabase("root", "test"); diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 90fa624a8d..38cd5fad4b 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -432,6 +432,9 @@ TAOS_DEFINE_ERROR(TSDB_CODE_SCH_STATUS_ERROR, "scheduler status erro TAOS_DEFINE_ERROR(TSDB_CODE_SCH_INTERNAL_ERROR, "scheduler internal error") TAOS_DEFINE_ERROR(TSDB_CODE_QW_MSG_ERROR, "Invalid msg order") +//planner +TAOS_DEFINE_ERROR(TSDB_CODE_PLAN_INTERNAL_ERROR, "planner internal error") + #ifdef TAOS_ERROR_C }; #endif From cf727c9db30104bfc86ffcc8fab4488d25708336 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 6 Apr 2022 07:54:52 +0000 Subject: [PATCH 16/25] fix show tables problem --- source/libs/executor/src/executorimpl.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 2ca06cb15f..148d16aaf8 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -310,6 +310,10 @@ SSDataBlock* createOutputBuf_rv1(SDataBlockDescNode* pNode) { idata.info.slotId = pDescNode->slotId; idata.info.precision = pDescNode->dataType.precision; + if (IS_VAR_DATA_TYPE(idata.info.type)) { + pBlock->info.hasVarCol = true; + } + taosArrayPush(pBlock->pDataBlock, &idata); } From e40dc9b9291b193886ec2757b0c8a2380ba9c022 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 6 Apr 2022 08:06:28 +0000 Subject: [PATCH 17/25] commit meta when close vnode --- source/dnode/vnode/src/meta/metaTDBImpl.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c index 282e0a51f6..c273f584ff 100644 --- a/source/dnode/vnode/src/meta/metaTDBImpl.c +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -190,6 +190,9 @@ int metaOpenDB(SMeta *pMeta) { void metaCloseDB(SMeta *pMeta) { if (pMeta->pDB) { + tdbCommit(pMeta->pDB->pEnv, &pMeta->pDB->txn); + tdbTxnClose(&pMeta->pDB->txn); + clearPool(pMeta->pDB->pPool); tdbDbClose(pMeta->pDB->pCtbIdx); tdbDbClose(pMeta->pDB->pNtbIdx); tdbDbClose(pMeta->pDB->pStbIdx); From 610832f0b47d2d535bfaf12c60368708d7b74098 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 6 Apr 2022 08:41:53 +0000 Subject: [PATCH 18/25] keep committed data in TDB --- source/libs/tdb/src/db/tdbBtree.c | 3 ++- source/libs/tdb/src/db/tdbPager.c | 36 +++++++++++++++++++------------ source/libs/tdb/src/db/tdbUtil.c | 13 +++++++++++ source/libs/tdb/src/inc/tdbOs.h | 20 +++++++++-------- source/libs/tdb/src/inc/tdbUtil.h | 1 + 5 files changed, 49 insertions(+), 24 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 285434b402..f760598714 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -297,7 +297,8 @@ static int tdbBtreeOpenImpl(SBTree *pBt) { { // 1. TODO: Search the main DB to check if the DB exists - pgno = 0; + ret = tdbPagerOpenDB(pBt->pPager, &pgno, true); + ASSERT(ret == 0); } if (pgno != 0) { diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index f78568b51c..7672ccf34e 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -76,6 +76,8 @@ int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager) { // pPager->jfd = -1; pPager->pageSize = tdbPCacheGetPageSize(pCache); + // pPager->dbOrigSize + ret = tdbGetFileSize(pPager->fd, pPager->pageSize, &(pPager->dbOrigSize)); *ppPager = pPager; return 0; @@ -91,26 +93,32 @@ int tdbPagerOpenDB(SPager *pPager, SPgno *ppgno, bool toCreate) { SPage *pPage; int ret; - { - // TODO: try to search the main DB to get the page number + if (pPager->dbOrigSize > 0) { + pgno = 1; + } else { pgno = 0; } - // if (pgno == 0 && toCreate) { - // ret = tdbPagerAllocPage(pPager, &pPage, &pgno); - // if (ret < 0) { - // return -1; - // } + { + // TODO: try to search the main DB to get the page number + // pgno = 0; + } - // // TODO: Need to zero the page + // if (pgno == 0 && toCreate) { + // ret = tdbPagerAllocPage(pPager, &pPage, &pgno); + // if (ret < 0) { + // return -1; + // } - // ret = tdbPagerWrite(pPager, pPage); - // if (ret < 0) { - // return -1; - // } - // } + // // TODO: Need to zero the page - *ppgno = pgno; + // ret = tdbPagerWrite(pPager, pPage); + // if (ret < 0) { + // return -1; + // } + // } + + *ppgno = pgno; return 0; } diff --git a/source/libs/tdb/src/db/tdbUtil.c b/source/libs/tdb/src/db/tdbUtil.c index fc299b3fc1..09e126772b 100644 --- a/source/libs/tdb/src/db/tdbUtil.c +++ b/source/libs/tdb/src/db/tdbUtil.c @@ -30,5 +30,18 @@ int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique) { ((uint64_t *)fileid)[2] = taosRand(); } + return 0; +} + +int tdbGetFileSize(tdb_fd_t fd, int szPage, SPgno *size) { + int ret; + int64_t szBytes; + + ret = tdbOsFileSize(fd, &szBytes); + if (ret < 0) { + return -1; + } + + *size = szBytes / szPage; return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdbOs.h b/source/libs/tdb/src/inc/tdbOs.h index ae389708f4..1d87285091 100644 --- a/source/libs/tdb/src/inc/tdbOs.h +++ b/source/libs/tdb/src/inc/tdbOs.h @@ -46,13 +46,14 @@ typedef TdFilePtr tdb_fd_t; #define tdbOsOpen(PATH, OPTION, MODE) taosOpenFile((PATH), (OPTION)) -#define tdbOsClose(FD) taosCloseFile(&(FD)) -#define tdbOsRead taosReadFile -#define tdbOsPRead taosPReadFile -#define tdbOsWrite taosWriteFile -#define tdbOsFSync taosFsyncFile -#define tdbOsLSeek taosLSeekFile -#define tdbOsRemove remove +#define tdbOsClose(FD) taosCloseFile(&(FD)) +#define tdbOsRead taosReadFile +#define tdbOsPRead taosPReadFile +#define tdbOsWrite taosWriteFile +#define tdbOsFSync taosFsyncFile +#define tdbOsLSeek taosLSeekFile +#define tdbOsRemove remove +#define tdbOsFileSize(FD, PSIZE) taosFStatFile(FD, PSIZE, NULL) /* directory */ #define tdbOsMkdir taosMkDir @@ -110,10 +111,11 @@ i64 tdbOsWrite(tdb_fd_t fd, const void *pData, i64 nBytes); #define tdbOsFSync fsync #define tdbOsLSeek lseek #define tdbOsRemove remove +#define tdbOsFileSize(FD, PSIZE) /* directory */ -#define tdbOsMkdir mkdir -#define tdbOsRmdir rmdir +#define tdbOsMkdir mkdir +#define tdbOsRmdir rmdir // For threads and lock ----------------- /* spin lock */ diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index 6abddb5b22..042026d0a4 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -29,6 +29,7 @@ extern "C" { #define TDB_ROUND8(x) (((x) + 7) & ~7) int tdbGnrtFileID(const char *fname, uint8_t *fileid, bool unique); +int tdbGetFileSize(tdb_fd_t fd, int szPage, SPgno *size); #define TDB_REALLOC(PTR, SIZE) \ ({ \ From e707047d6c9654ba68ebea2a3460b83d06413d28 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 6 Apr 2022 09:39:05 +0000 Subject: [PATCH 19/25] fix a small bug --- source/dnode/vnode/src/meta/metaTDBImpl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c index c273f584ff..3ab26c3d4d 100644 --- a/source/dnode/vnode/src/meta/metaTDBImpl.c +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -547,7 +547,7 @@ tb_uid_t metaCtbCursorNext(SMCtbCursor *pCtbCur) { return 0; } - pCtbIdxKey = pCtbCur->pVal; + pCtbIdxKey = pCtbCur->pKey; return pCtbIdxKey->uid; } From 6863999ec5f5974dcd807d6eaa51e6ee48dd26a7 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 6 Apr 2022 17:59:08 +0800 Subject: [PATCH 20/25] [td-14493] support having in group by --- source/dnode/vnode/inc/tsdb.h | 4 +- source/dnode/vnode/src/tsdb/tsdbRead.c | 4 +- source/libs/executor/inc/executorimpl.h | 12 +- source/libs/executor/src/executorimpl.c | 210 ++++++----------------- source/libs/executor/src/groupoperator.c | 197 +++++++++++++++++++-- source/libs/executor/src/scanoperator.c | 35 +--- source/libs/executor/src/tsimplehash.c | 6 +- source/libs/function/src/builtinsimpl.c | 2 +- source/util/src/thash.c | 16 +- 9 files changed, 262 insertions(+), 224 deletions(-) diff --git a/source/dnode/vnode/inc/tsdb.h b/source/dnode/vnode/inc/tsdb.h index 3cabb6ce2e..38d6d879fa 100644 --- a/source/dnode/vnode/inc/tsdb.h +++ b/source/dnode/vnode/inc/tsdb.h @@ -172,9 +172,9 @@ tsdbReaderT *tsdbQueryTables(STsdb *tsdb, STsdbQueryCond *pCond, STableGroupInfo tsdbReaderT tsdbQueryCacheLast(STsdb *tsdb, STsdbQueryCond *pCond, STableGroupInfo *groupList, uint64_t qId, void* pMemRef); -int32_t tsdbGetFileBlocksDistInfo(tsdbReaderT* queryHandle, STableBlockDistInfo* pTableBlockInfo); +int32_t tsdbGetFileBlocksDistInfo(tsdbReaderT* pReader, STableBlockDistInfo* pTableBlockInfo); -bool isTsdbCacheLastRow(tsdbReaderT* pTsdbReadHandle); +bool isTsdbCacheLastRow(tsdbReaderT* pReader); /** * diff --git a/source/dnode/vnode/src/tsdb/tsdbRead.c b/source/dnode/vnode/src/tsdb/tsdbRead.c index 4a3e764b13..144aa7fcdc 100644 --- a/source/dnode/vnode/src/tsdb/tsdbRead.c +++ b/source/dnode/vnode/src/tsdb/tsdbRead.c @@ -3046,8 +3046,8 @@ bool tsdbGetExternalRow(tsdbReaderT pHandle) { // return code; //} -bool isTsdbCacheLastRow(tsdbReaderT* pTsdbReadHandle) { - return ((STsdbReadHandle *)pTsdbReadHandle)->cachelastrow > TSDB_CACHED_TYPE_NONE; +bool isTsdbCacheLastRow(tsdbReaderT* pReader) { + return ((STsdbReadHandle *)pReader)->cachelastrow > TSDB_CACHED_TYPE_NONE; } int32_t checkForCachedLastRow(STsdbReadHandle* pTsdbReadHandle, STableGroupInfo *groupList) { diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index 3cbcbc004b..bf9377c83d 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -551,6 +551,7 @@ typedef struct SGroupbyOperatorInfo { SOptrBasicInfo binfo; SArray* pGroupCols; SArray* pGroupColVals; // current group column values, SArray + SNode* pCondition; bool isInit; // denote if current val is initialized or not char* keyBuf; // group by keys for hash int32_t groupKeyLen; // total group by column width @@ -630,9 +631,10 @@ typedef struct SDistinctOperatorInfo { SHashObj* pSet; SSDataBlock* pRes; bool recordNullVal; // has already record the null value, no need to try again - int64_t threshold; // todo remove it - int64_t outputCapacity;// todo remove it - int32_t totalBytes; // todo remove it +// int64_t threshold; // todo remove it +// int64_t outputCapacity;// todo remove it +// int32_t totalBytes; // todo remove it + SResultInfo resInfo; char* buf; SArray* pDistinctDataInfo; } SDistinctOperatorInfo; @@ -651,6 +653,8 @@ void doDestroyBasicInfo(SOptrBasicInfo* pInfo, int32_t numOfOutput); int32_t setSDataBlockFromFetchRsp(SSDataBlock* pRes, SLoadRemoteDataInfo* pLoadInfo, int32_t numOfRows, char* pData, int32_t compLen, int32_t numOfOutput, int64_t startTs, uint64_t* total, SArray* pColList); +void doSetOperatorCompleted(SOperatorInfo* pOperator); +void doFilter(const SNode* pFilterNode, SSDataBlock* pBlock); SOperatorInfo* createExchangeOperatorInfo(const SNodeList* pSources, SSDataBlock* pBlock, SExecTaskInfo* pTaskInfo); SOperatorInfo* createTableScanOperatorInfo(void* pTsdbReadHandle, int32_t order, int32_t numOfCols, int32_t repeatTime, @@ -667,7 +671,7 @@ SOperatorInfo* createIntervalOperatorInfo(SOperatorInfo* downstream, SExprInfo* const STableGroupInfo* pTableGroupInfo, SExecTaskInfo* pTaskInfo); SOperatorInfo* createSessionAggOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExprInfo, int32_t numOfCols, SSDataBlock* pResBlock, int64_t gap, SExecTaskInfo* pTaskInfo); SOperatorInfo* createGroupOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExprInfo, int32_t numOfCols, SSDataBlock* pResultBlock, - SArray* pGroupColList, SExecTaskInfo* pTaskInfo, const STableGroupInfo* pTableGroupInfo); + SArray* pGroupColList, SNode* pCondition, SExecTaskInfo* pTaskInfo, const STableGroupInfo* pTableGroupInfo); SOperatorInfo* createDataBlockInfoScanOperator(void* dataReader, SExecTaskInfo* pTaskInfo); SOperatorInfo* createStreamScanOperatorInfo(void* streamReadHandle, SSDataBlock* pResBlock, SArray* pColList, SArray* pTableIdList, SExecTaskInfo* pTaskInfo); diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 3e47356223..2323291fc1 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -51,8 +51,6 @@ #define GET_FORWARD_DIRECTION_FACTOR(ord) (((ord) == TSDB_ORDER_ASC) ? QUERY_ASC_FORWARD_STEP : QUERY_DESC_FORWARD_STEP) -#define MULTI_KEY_DELIM "-" - enum { TS_JOIN_TS_EQUAL = 0, TS_JOIN_TS_NOT_EQUALS = 1, @@ -221,12 +219,13 @@ static void destroyConditionOperatorInfo(void* param, int32_t numOfOutput); static void destroyOperatorInfo(SOperatorInfo* pOperator); static void destroySysTableScannerOperatorInfo(void* param, int32_t numOfOutput); -static void doSetOperatorCompleted(SOperatorInfo* pOperator) { +void doSetOperatorCompleted(SOperatorInfo* pOperator) { pOperator->status = OP_EXEC_DONE; if (pOperator->pTaskInfo != NULL) { setTaskStatus(pOperator->pTaskInfo, TASK_COMPLETED); } } + #define OPTR_IS_OPENED(_optr) (((_optr)->status & OP_OPENED) == OP_OPENED) #define OPTR_SET_OPENED(_optr) ((_optr)->status |= OP_OPENED) @@ -1245,11 +1244,7 @@ static void doAggregateImpl(SOperatorInfo* pOperator, TSKEY startTs, SqlFunction } static void setPseudoOutputColInfo(SSDataBlock* pResult, SqlFunctionCtx* pCtx, SArray* pPseudoList) { - size_t num = 0; - if (pPseudoList != NULL) { - num = taosArrayGetSize(pPseudoList); - } - + size_t num = (pPseudoList != NULL)? taosArrayGetSize(pPseudoList):0; for (int32_t i = 0; i < num; ++i) { pCtx[i].pOutput = taosArrayGet(pResult->pDataBlock, i); } @@ -1291,8 +1286,10 @@ static void projectApplyFunctions(SExprInfo* pExpr, SSDataBlock* pResult, SSData pCtx[k].pOutput = taosArrayGet(pResult->pDataBlock, k); pCtx[k].offset = pResult->info.rows; // set the start offset - int32_t* outputColIndex = taosArrayGet(pPseudoList, 0); - pCtx[k].pTsOutput = (SColumnInfoData*)pCtx[*outputColIndex].pOutput; + if (taosArrayGetSize(pPseudoList) > 0) { + int32_t* outputColIndex = taosArrayGet(pPseudoList, 0); + pCtx[k].pTsOutput = (SColumnInfoData*)pCtx[*outputColIndex].pOutput; + } int32_t numOfRows = pCtx[k].fpSet.process(&pCtx[k]); pResult->info.rows += numOfRows; @@ -3272,6 +3269,44 @@ void setResultRowOutputBufInitCtx_rv(SResultRow* pResult, SqlFunctionCtx* pCtx, } } +void doFilter(const SNode* pFilterNode, SSDataBlock* pBlock) { + if (pFilterNode == NULL) { + return; + } + + SFilterInfo* filter = NULL; + int32_t code = filterInitFromNode((SNode*)pFilterNode, &filter, 0); + + SFilterColumnParam param1 = {.numOfCols = pBlock->info.numOfCols, .pDataBlock = pBlock->pDataBlock}; + code = filterSetDataFromSlotId(filter, ¶m1); + + int8_t* rowRes = NULL; + bool keep = filterExecute(filter, pBlock, &rowRes, NULL, param1.numOfCols); + + SSDataBlock* px = createOneDataBlock(pBlock); + blockDataEnsureCapacity(px, pBlock->info.rows); + + int32_t numOfRow = 0; + for (int32_t i = 0; i < pBlock->info.numOfCols; ++i) { + SColumnInfoData* pDst = taosArrayGet(px->pDataBlock, i); + SColumnInfoData* pSrc = taosArrayGet(pBlock->pDataBlock, i); + + numOfRow = 0; + for (int32_t j = 0; j < pBlock->info.rows; ++j) { + if (rowRes[j] == 0) { + continue; + } + + colDataAppend(pDst, numOfRow, colDataGetData(pSrc, j), false); + numOfRow += 1; + } + + *pSrc = *pDst; + } + + pBlock->info.rows = numOfRow; +} + void doSetTableGroupOutputBuf(SAggOperatorInfo* pAggInfo, int32_t numOfOutput, int32_t tableGroupId, SExecTaskInfo* pTaskInfo) { // for simple group by query without interval, all the tables belong to one group result. int64_t uid = 0; @@ -6206,14 +6241,6 @@ static void destroyOrderOperatorInfo(void* param, int32_t numOfOutput) { taosArrayDestroy(pInfo->pSortInfo); } -static void destroyDistinctOperatorInfo(void* param, int32_t numOfOutput) { - SDistinctOperatorInfo* pInfo = (SDistinctOperatorInfo*)param; - taosHashCleanup(pInfo->pSet); - taosMemoryFreeClear(pInfo->buf); - taosArrayDestroy(pInfo->pDistinctDataInfo); - pInfo->pRes = blockDataDestroy(pInfo->pRes); -} - void destroyExchangeOperatorInfo(void* param, int32_t numOfOutput) { SExchangeInfo* pExInfo = (SExchangeInfo*)param; taosArrayDestroy(pExInfo->pSources); @@ -6785,146 +6812,7 @@ SOperatorInfo* createTagScanOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SExprInfo return pOperator; } -static bool initMultiDistinctInfo(SDistinctOperatorInfo* pInfo, SOperatorInfo* pOperator, SSDataBlock* pBlock) { - if (taosArrayGetSize(pInfo->pDistinctDataInfo) == pOperator->numOfOutput) { - // distinct info already inited - return true; - } - for (int i = 0; i < pOperator->numOfOutput; i++) { - // pInfo->totalBytes += pOperator->pExpr[i].base.colBytes; - } - for (int i = 0; i < pOperator->numOfOutput; i++) { - int numOfBlock = (int)(taosArrayGetSize(pBlock->pDataBlock)); - assert(i < numOfBlock); - for (int j = 0; j < numOfBlock; j++) { - SColumnInfoData* pColDataInfo = taosArrayGet(pBlock->pDataBlock, j); - if (pColDataInfo->info.colId == pOperator->pExpr[i].base.resSchema.colId) { - SDistinctDataInfo item = {.index = j, .type = pColDataInfo->info.type, .bytes = pColDataInfo->info.bytes}; - taosArrayInsert(pInfo->pDistinctDataInfo, i, &item); - } - } - } - pInfo->totalBytes += (int32_t)strlen(MULTI_KEY_DELIM) * (pOperator->numOfOutput); - pInfo->buf = taosMemoryCalloc(1, pInfo->totalBytes); - return taosArrayGetSize(pInfo->pDistinctDataInfo) == pOperator->numOfOutput ? true : false; -} -static void buildMultiDistinctKey(SDistinctOperatorInfo* pInfo, SSDataBlock* pBlock, int32_t rowId) { - char* p = pInfo->buf; - memset(p, 0, pInfo->totalBytes); - - for (int i = 0; i < taosArrayGetSize(pInfo->pDistinctDataInfo); i++) { - SDistinctDataInfo* pDistDataInfo = (SDistinctDataInfo*)taosArrayGet(pInfo->pDistinctDataInfo, i); - SColumnInfoData* pColDataInfo = taosArrayGet(pBlock->pDataBlock, pDistDataInfo->index); - char* val = ((char*)pColDataInfo->pData) + pColDataInfo->info.bytes * rowId; - if (isNull(val, pDistDataInfo->type)) { - p += pDistDataInfo->bytes; - continue; - } - if (IS_VAR_DATA_TYPE(pDistDataInfo->type)) { - memcpy(p, varDataVal(val), varDataLen(val)); - p += varDataLen(val); - } else { - memcpy(p, val, pDistDataInfo->bytes); - p += pDistDataInfo->bytes; - } - memcpy(p, MULTI_KEY_DELIM, strlen(MULTI_KEY_DELIM)); - p += strlen(MULTI_KEY_DELIM); - } -} - -static SSDataBlock* hashDistinct(SOperatorInfo* pOperator, bool* newgroup) { - if (pOperator->status == OP_EXEC_DONE) { - return NULL; - } - - SDistinctOperatorInfo* pInfo = pOperator->info; - SSDataBlock* pRes = pInfo->pRes; - - pRes->info.rows = 0; - SSDataBlock* pBlock = NULL; - - SOperatorInfo* pDownstream = pOperator->pDownstream[0]; - while (1) { - publishOperatorProfEvent(pDownstream, QUERY_PROF_BEFORE_OPERATOR_EXEC); - pBlock = pDownstream->getNextFn(pDownstream, newgroup); - publishOperatorProfEvent(pDownstream, QUERY_PROF_AFTER_OPERATOR_EXEC); - - if (pBlock == NULL) { - doSetOperatorCompleted(pOperator); - break; - } - if (!initMultiDistinctInfo(pInfo, pOperator, pBlock)) { - doSetOperatorCompleted(pOperator); - break; - } - - // ensure result output buf - if (pRes->info.rows + pBlock->info.rows > pInfo->outputCapacity) { - int32_t newSize = pRes->info.rows + pBlock->info.rows; - for (int i = 0; i < taosArrayGetSize(pRes->pDataBlock); i++) { - SColumnInfoData* pResultColInfoData = taosArrayGet(pRes->pDataBlock, i); - SDistinctDataInfo* pDistDataInfo = taosArrayGet(pInfo->pDistinctDataInfo, i); - char* tmp = taosMemoryRealloc(pResultColInfoData->pData, newSize * pDistDataInfo->bytes); - if (tmp == NULL) { - return NULL; - } else { - pResultColInfoData->pData = tmp; - } - } - pInfo->outputCapacity = newSize; - } - - for (int32_t i = 0; i < pBlock->info.rows; i++) { - buildMultiDistinctKey(pInfo, pBlock, i); - if (taosHashGet(pInfo->pSet, pInfo->buf, pInfo->totalBytes) == NULL) { - int32_t dummy; - taosHashPut(pInfo->pSet, pInfo->buf, pInfo->totalBytes, &dummy, sizeof(dummy)); - for (int j = 0; j < taosArrayGetSize(pRes->pDataBlock); j++) { - SDistinctDataInfo* pDistDataInfo = taosArrayGet(pInfo->pDistinctDataInfo, j); // distinct meta info - SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pDistDataInfo->index); // src - SColumnInfoData* pResultColInfoData = taosArrayGet(pRes->pDataBlock, j); // dist - - char* val = ((char*)pColInfoData->pData) + pDistDataInfo->bytes * i; - char* start = pResultColInfoData->pData + pDistDataInfo->bytes * pInfo->pRes->info.rows; - memcpy(start, val, pDistDataInfo->bytes); - } - pRes->info.rows += 1; - } - } - - if (pRes->info.rows >= pInfo->threshold) { - break; - } - } - return (pInfo->pRes->info.rows > 0) ? pInfo->pRes : NULL; -} - -SOperatorInfo* createDistinctOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExpr, int32_t numOfCols, SSDataBlock* pResBlock, SExecTaskInfo* pTaskInfo) { - SDistinctOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SDistinctOperatorInfo)); - SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo)); - - pOperator->resultInfo.capacity = 4096; // todo extract function. - - pInfo->totalBytes = 0; - pInfo->buf = NULL; - - pInfo->pDistinctDataInfo = taosArrayInit(numOfCols, sizeof(SDistinctDataInfo)); - pInfo->pSet = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK); - - pOperator->name = "DistinctOperator"; - pOperator->blockingOptr = true; - pOperator->status = OP_NOT_OPENED; -// pOperator->operatorType = DISTINCT; - pOperator->pExpr = pExpr; - pOperator->numOfOutput = numOfCols; - pOperator->info = pInfo; - pOperator->getNextFn = hashDistinct; - pOperator->closeFn = destroyDistinctOperatorInfo; - - int32_t code = appendDownstream(pOperator, &downstream, 1); - return pOperator; -} static int32_t getColumnIndexInSource(SQueriedTableInfo* pTableInfo, SExprBasicInfo* pExpr, SColumnInfo* pTagCols) { int32_t j = 0; @@ -7147,7 +7035,6 @@ SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo int32_t numOfCols = 0; tsdbReaderT pDataReader = doCreateDataReader((STableScanPhysiNode*)pPhyNode, pHandle, pTableGroupInfo, (uint64_t)queryId, taskId); SArray* pColList = extractColMatchInfo(pScanPhyNode->pScanCols, pScanPhyNode->node.pOutputDataBlockDesc, &numOfCols); - return createTableScanOperatorInfo(pDataReader, pScanPhyNode->order, numOfCols, pScanPhyNode->count, pScanPhyNode->reverse, pColList, pScanPhyNode->node.pConditions, pTaskInfo); } else if (QUERY_NODE_PHYSICAL_PLAN_EXCHANGE == nodeType(pPhyNode)) { @@ -7157,8 +7044,7 @@ SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo } else if (QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN == nodeType(pPhyNode)) { SScanPhysiNode* pScanPhyNode = (SScanPhysiNode*)pPhyNode; // simple child table. - int32_t code = doCreateTableGroup(pHandle->meta, pScanPhyNode->tableType, pScanPhyNode->uid, pTableGroupInfo, - queryId, taskId); + int32_t code = doCreateTableGroup(pHandle->meta, pScanPhyNode->tableType, pScanPhyNode->uid, pTableGroupInfo, queryId, taskId); SArray* tableIdList = extractTableIdList(pTableGroupInfo); SSDataBlock* pResBlock = createOutputBuf_rv1(pScanPhyNode->node.pOutputDataBlockDesc); @@ -7215,7 +7101,7 @@ SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo if (pAggNode->pGroupKeys != NULL) { SArray* pColList = extractColumnInfo(pAggNode->pGroupKeys); - return createGroupOperatorInfo(op, pExprInfo, num, pResBlock, pColList, pTaskInfo, NULL); + return createGroupOperatorInfo(op, pExprInfo, num, pResBlock, pColList, pAggNode->node.pConditions, pTaskInfo, NULL); } else { return createAggregateOperatorInfo(op, pExprInfo, num, pResBlock, pTaskInfo, pTableGroupInfo); } @@ -7306,7 +7192,7 @@ static tsdbReaderT createDataReaderImpl(STableScanPhysiNode* pTableScanNode, STa continue; } - cond.colList[j].type = pColNode->node.resType.type; + cond.colList[j].type = pColNode->node.resType.type; cond.colList[j].bytes = pColNode->node.resType.bytes; cond.colList[j].colId = pColNode->colId; j += 1; diff --git a/source/libs/executor/src/groupoperator.c b/source/libs/executor/src/groupoperator.c index 90cb97f6c7..c22b58a0d8 100644 --- a/source/libs/executor/src/groupoperator.c +++ b/source/libs/executor/src/groupoperator.c @@ -245,13 +245,14 @@ static SSDataBlock* hashGroupbyAggregate(SOperatorInfo* pOperator, bool* newgrou } SGroupbyOperatorInfo* pInfo = pOperator->info; + SSDataBlock* pRes = pInfo->binfo.pRes; + if (pOperator->status == OP_RES_TO_RETURN) { - toSDatablock(&pInfo->groupResInfo, pInfo->aggSup.pResultBuf, pInfo->binfo.pRes, pInfo->binfo.capacity, - pInfo->binfo.rowCellInfoOffset); - if (pInfo->binfo.pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { + toSDatablock(&pInfo->groupResInfo, pInfo->aggSup.pResultBuf, pRes, pInfo->binfo.capacity, pInfo->binfo.rowCellInfoOffset); + if (pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { pOperator->status = OP_EXEC_DONE; } - return pInfo->binfo.pRes; + return pRes; } int32_t order = TSDB_ORDER_ASC; @@ -283,18 +284,29 @@ static SSDataBlock* hashGroupbyAggregate(SOperatorInfo* pOperator, bool* newgrou // pInfo->binfo.rowCellInfoOffset); // } - blockDataEnsureCapacity(pInfo->binfo.pRes, pInfo->binfo.capacity); + blockDataEnsureCapacity(pRes, pInfo->binfo.capacity); initGroupResInfo(&pInfo->groupResInfo, &pInfo->binfo.resultRowInfo); - toSDatablock(&pInfo->groupResInfo, pInfo->aggSup.pResultBuf, pInfo->binfo.pRes, pInfo->binfo.capacity, - pInfo->binfo.rowCellInfoOffset); - if (pInfo->binfo.pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { - pOperator->status = OP_EXEC_DONE; + + while(1) { + toSDatablock(&pInfo->groupResInfo, pInfo->aggSup.pResultBuf, pRes, pInfo->binfo.capacity, + pInfo->binfo.rowCellInfoOffset); + doFilter(pInfo->pCondition, pRes); + + bool hasRemain = hasRemainDataInCurrentGroup(&pInfo->groupResInfo); + if (!hasRemain) { + pOperator->status = OP_EXEC_DONE; + break; + } + + if (pRes->info.rows > 0) { + break; + } } return pInfo->binfo.pRes; } -SOperatorInfo* createGroupOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExprInfo, int32_t numOfCols, SSDataBlock* pResultBlock, SArray* pGroupColList, SExecTaskInfo* pTaskInfo, +SOperatorInfo* createGroupOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExprInfo, int32_t numOfCols, SSDataBlock* pResultBlock, SArray* pGroupColList, SNode* pCondition, SExecTaskInfo* pTaskInfo, const STableGroupInfo* pTableGroupInfo) { SGroupbyOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupbyOperatorInfo)); SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo)); @@ -303,6 +315,7 @@ SOperatorInfo* createGroupOperatorInfo(SOperatorInfo* downstream, SExprInfo* pEx } pInfo->pGroupCols = pGroupColList; + pInfo->pCondition = pCondition; initAggInfo(&pInfo->binfo, &pInfo->aggSup, pExprInfo, numOfCols, 4096, pResultBlock, pTaskInfo->id.str); initResultRowInfo(&pInfo->binfo.resultRowInfo, 8); @@ -329,4 +342,166 @@ SOperatorInfo* createGroupOperatorInfo(SOperatorInfo* downstream, SExprInfo* pEx taosMemoryFreeClear(pInfo); taosMemoryFreeClear(pOperator); return NULL; -} \ No newline at end of file +} + +#define MULTI_KEY_DELIM "-" + +static void destroyDistinctOperatorInfo(void* param, int32_t numOfOutput) { + SDistinctOperatorInfo* pInfo = (SDistinctOperatorInfo*)param; + taosHashCleanup(pInfo->pSet); + taosMemoryFreeClear(pInfo->buf); + taosArrayDestroy(pInfo->pDistinctDataInfo); + pInfo->pRes = blockDataDestroy(pInfo->pRes); +} + +static void buildMultiDistinctKey(SDistinctOperatorInfo* pInfo, SSDataBlock* pBlock, int32_t rowId) { + char* p = pInfo->buf; +// memset(p, 0, pInfo->totalBytes); + + for (int i = 0; i < taosArrayGetSize(pInfo->pDistinctDataInfo); i++) { + SDistinctDataInfo* pDistDataInfo = (SDistinctDataInfo*)taosArrayGet(pInfo->pDistinctDataInfo, i); + SColumnInfoData* pColDataInfo = taosArrayGet(pBlock->pDataBlock, pDistDataInfo->index); + + char* val = ((char*)pColDataInfo->pData) + pColDataInfo->info.bytes * rowId; + if (isNull(val, pDistDataInfo->type)) { + p += pDistDataInfo->bytes; + continue; + } + if (IS_VAR_DATA_TYPE(pDistDataInfo->type)) { + memcpy(p, varDataVal(val), varDataLen(val)); + p += varDataLen(val); + } else { + memcpy(p, val, pDistDataInfo->bytes); + p += pDistDataInfo->bytes; + } + memcpy(p, MULTI_KEY_DELIM, strlen(MULTI_KEY_DELIM)); + p += strlen(MULTI_KEY_DELIM); + } +} + +static bool initMultiDistinctInfo(SDistinctOperatorInfo* pInfo, SOperatorInfo* pOperator) { + for (int i = 0; i < pOperator->numOfOutput; i++) { + // pInfo->totalBytes += pOperator->pExpr[i].base.colBytes; + } +#if 0 + for (int i = 0; i < pOperator->numOfOutput; i++) { + int numOfCols = (int)(taosArrayGetSize(pBlock->pDataBlock)); + assert(i < numOfCols); + + for (int j = 0; j < numOfCols; j++) { + SColumnInfoData* pColDataInfo = taosArrayGet(pBlock->pDataBlock, j); + if (pColDataInfo->info.colId == pOperator->pExpr[i].base.resSchema.colId) { + SDistinctDataInfo item = {.index = j, .type = pColDataInfo->info.type, .bytes = pColDataInfo->info.bytes}; + taosArrayInsert(pInfo->pDistinctDataInfo, i, &item); + } + } + } +#endif + +// pInfo->totalBytes += (int32_t)strlen(MULTI_KEY_DELIM) * (pOperator->numOfOutput); +// pInfo->buf = taosMemoryCalloc(1, pInfo->totalBytes); + return taosArrayGetSize(pInfo->pDistinctDataInfo) == pOperator->numOfOutput ? true : false; +} + +static SSDataBlock* hashDistinct(SOperatorInfo* pOperator, bool* newgroup) { + if (pOperator->status == OP_EXEC_DONE) { + return NULL; + } + + SDistinctOperatorInfo* pInfo = pOperator->info; + SSDataBlock* pRes = pInfo->pRes; + + pRes->info.rows = 0; + SSDataBlock* pBlock = NULL; + + SOperatorInfo* pDownstream = pOperator->pDownstream[0]; + while (1) { + publishOperatorProfEvent(pDownstream, QUERY_PROF_BEFORE_OPERATOR_EXEC); + pBlock = pDownstream->getNextFn(pDownstream, newgroup); + publishOperatorProfEvent(pDownstream, QUERY_PROF_AFTER_OPERATOR_EXEC); + + if (pBlock == NULL) { + doSetOperatorCompleted(pOperator); + break; + } + + // ensure result output buf + if (pRes->info.rows + pBlock->info.rows > pInfo->resInfo.capacity) { + int32_t newSize = pRes->info.rows + pBlock->info.rows; + for (int i = 0; i < taosArrayGetSize(pRes->pDataBlock); i++) { + SColumnInfoData* pResultColInfoData = taosArrayGet(pRes->pDataBlock, i); + SDistinctDataInfo* pDistDataInfo = taosArrayGet(pInfo->pDistinctDataInfo, i); + +// char* tmp = taosMemoryRealloc(pResultColInfoData->pData, newSize * pDistDataInfo->bytes); +// if (tmp == NULL) { +// return NULL; +// } else { +// pResultColInfoData->pData = tmp; +// } + } + pInfo->resInfo.capacity = newSize; + } + + for (int32_t i = 0; i < pBlock->info.rows; i++) { + buildMultiDistinctKey(pInfo, pBlock, i); + if (taosHashGet(pInfo->pSet, pInfo->buf, 0) == NULL) { + taosHashPut(pInfo->pSet, pInfo->buf, 0, NULL, 0); + + for (int j = 0; j < taosArrayGetSize(pRes->pDataBlock); j++) { + SDistinctDataInfo* pDistDataInfo = taosArrayGet(pInfo->pDistinctDataInfo, j); // distinct meta info + SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pDistDataInfo->index); // src + SColumnInfoData* pResultColInfoData = taosArrayGet(pRes->pDataBlock, j); // dist + + char* val = ((char*)pColInfoData->pData) + pDistDataInfo->bytes * i; + char* start = pResultColInfoData->pData + pDistDataInfo->bytes * pInfo->pRes->info.rows; + memcpy(start, val, pDistDataInfo->bytes); + } + + pRes->info.rows += 1; + } + } + + if (pRes->info.rows >= pInfo->resInfo.threshold) { + break; + } + } + + return (pInfo->pRes->info.rows > 0) ? pInfo->pRes : NULL; +} + +SOperatorInfo* createDistinctOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExpr, int32_t numOfCols, SSDataBlock* pResBlock, SExecTaskInfo* pTaskInfo) { + SDistinctOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SDistinctOperatorInfo)); + SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo)); + if (pInfo == NULL || pOperator == NULL) { + goto _error; + } + + pOperator->resultInfo.capacity = 4096; // todo extract function. + +// pInfo->totalBytes = 0; + pInfo->buf = NULL; + + pInfo->pDistinctDataInfo = taosArrayInit(numOfCols, sizeof(SDistinctDataInfo)); + initMultiDistinctInfo(pInfo, pOperator); + + pInfo->pSet = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK); + + pOperator->name = "DistinctOperator"; + pOperator->blockingOptr = true; + pOperator->status = OP_NOT_OPENED; +// pOperator->operatorType = DISTINCT; + pOperator->pExpr = pExpr; + pOperator->numOfOutput = numOfCols; + pOperator->info = pInfo; + pOperator->getNextFn = hashDistinct; + pOperator->closeFn = destroyDistinctOperatorInfo; + + int32_t code = appendDownstream(pOperator, &downstream, 1); + return pOperator; + + _error: + pTaskInfo->code = TSDB_CODE_OUT_OF_MEMORY; + taosMemoryFree(pInfo); + taosMemoryFree(pOperator); + return NULL; +} diff --git a/source/libs/executor/src/scanoperator.c b/source/libs/executor/src/scanoperator.c index ace84c5f9a..f39a18da68 100644 --- a/source/libs/executor/src/scanoperator.c +++ b/source/libs/executor/src/scanoperator.c @@ -34,6 +34,7 @@ #define SET_REVERSE_SCAN_FLAG(_info) ((_info)->scanFlag = REVERSE_SCAN) #define SWITCH_ORDER(n) (((n) = ((n) == TSDB_ORDER_ASC) ? TSDB_ORDER_DESC : TSDB_ORDER_ASC)) + void switchCtxOrder(SqlFunctionCtx* pCtx, int32_t numOfOutput) { for (int32_t i = 0; i < numOfOutput; ++i) { SWITCH_ORDER(pCtx[i].order); @@ -91,39 +92,7 @@ int32_t loadDataBlock(SExecTaskInfo* pTaskInfo, STableScanInfo* pTableScanInfo, taosArraySet(pBlock->pDataBlock, pColMatchInfo->targetSlotId, p); } - if (pTableScanInfo->pFilterNode != NULL) { - SFilterInfo* filter = NULL; - int32_t code = filterInitFromNode((SNode*)pTableScanInfo->pFilterNode, &filter, 0); - - SFilterColumnParam param1 = {.numOfCols = pBlock->info.numOfCols, .pDataBlock = pBlock->pDataBlock}; - code = filterSetDataFromSlotId(filter, ¶m1); - - int8_t* rowRes = NULL; - bool keep = filterExecute(filter, pBlock, &rowRes, NULL, param1.numOfCols); - - SSDataBlock* px = createOneDataBlock(pBlock); - blockDataEnsureCapacity(px, pBlock->info.rows); - - int32_t numOfRow = 0; - for (int32_t i = 0; i < pBlock->info.numOfCols; ++i) { - SColumnInfoData* pDst = taosArrayGet(px->pDataBlock, i); - SColumnInfoData* pSrc = taosArrayGet(pBlock->pDataBlock, i); - - numOfRow = 0; - for (int32_t j = 0; j < pBlock->info.rows; ++j) { - if (rowRes[j] == 0) { - continue; - } - - colDataAppend(pDst, numOfRow, colDataGetData(pSrc, j), false); - numOfRow += 1; - } - *pSrc = *pDst; - } - - pBlock->info.rows = numOfRow; - } - + doFilter(pTableScanInfo->pFilterNode, pBlock); return TSDB_CODE_SUCCESS; } diff --git a/source/libs/executor/src/tsimplehash.c b/source/libs/executor/src/tsimplehash.c index 981da0415e..5c10216a96 100644 --- a/source/libs/executor/src/tsimplehash.c +++ b/source/libs/executor/src/tsimplehash.c @@ -37,7 +37,7 @@ typedef struct SHNode { char data[]; } SHNode; -typedef struct SSHashObj { +struct SSHashObj { SHNode **hashList; size_t capacity; // number of slots int64_t size; // number of elements in hash table @@ -45,7 +45,7 @@ typedef struct SSHashObj { _equal_fn_t equalFp; // equal function int32_t keyLen; int32_t dataLen; -} SSHashObj; +}; static FORCE_INLINE int32_t taosHashCapacity(int32_t length) { int32_t len = MIN(length, HASH_MAX_CAPACITY); @@ -107,7 +107,7 @@ static SHNode *doCreateHashNode(const void *key, size_t keyLen, const void *pDat return pNewNode; } -void taosHashTableResize(SSHashObj *pHashObj) { +static void taosHashTableResize(SSHashObj *pHashObj) { if (!HASH_NEED_RESIZE(pHashObj)) { return; } diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 6f7fcd37be..fb6abe3c5d 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -917,7 +917,7 @@ int32_t diffFunction(SqlFunctionCtx *pCtx) { colDataAppendInt32(pOutput, pos, &delta); } - if (tsList != NULL) { + if (pTsOutput != NULL) { colDataAppendInt64(pTsOutput, pos, &tsList[i]); } } diff --git a/source/util/src/thash.c b/source/util/src/thash.c index 88e5c12770..809d008aa7 100644 --- a/source/util/src/thash.c +++ b/source/util/src/thash.c @@ -311,10 +311,6 @@ int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, const vo } uint32_t hashVal = (*pHashObj->hashFp)(key, (uint32_t)keyLen); - SHashNode *pNewNode = doCreateHashNode(key, keyLen, data, size, hashVal); - if (pNewNode == NULL) { - return -1; - } // need the resize process, write lock applied if (HASH_NEED_RESIZE(pHashObj)) { @@ -355,6 +351,11 @@ int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, const vo if (pNode == NULL) { // no data in hash table with the specified key, add it into hash table + SHashNode *pNewNode = doCreateHashNode(key, keyLen, data, size, hashVal); + if (pNewNode == NULL) { + return -1; + } + pushfrontNodeInEntryList(pe, pNewNode); assert(pe->next != NULL); @@ -368,9 +369,12 @@ int32_t taosHashPut(SHashObj *pHashObj, const void *key, size_t keyLen, const vo } else { // not support the update operation, return error if (pHashObj->enableUpdate) { + SHashNode *pNewNode = doCreateHashNode(key, keyLen, data, size, hashVal); + if (pNewNode == NULL) { + return -1; + } + doUpdateHashNode(pHashObj, pe, prev, pNode, pNewNode); - } else { - FREE_HASH_NODE(pNewNode); } taosHashEntryWUnlock(pHashObj, pe); From f136211bad2d0b78e31ce032282c04c1249e10f3 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 6 Apr 2022 10:21:36 +0000 Subject: [PATCH 21/25] fix TDB encode and decode bug --- source/libs/tdb/src/inc/tdbUtil.h | 7 +++++-- source/libs/tdb/test/CMakeLists.txt | 6 +++++- source/libs/tdb/test/tdbUtilTest.cpp | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 source/libs/tdb/test/tdbUtilTest.cpp diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index 042026d0a4..7085e71567 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -84,15 +84,18 @@ static inline int tdbPutVarInt(u8 *p, int v) { static inline int tdbGetVarInt(const u8 *p, int *v) { int n = 0; int tv = 0; + int t; for (;;) { if (p[n] <= 0x7f) { - tv = (tv << 7) | p[n]; + t = p[n]; + tv |= (t << (7 * n)); n++; break; } - tv = (tv << 7) | (p[n] & 0x7f); + t = p[n] & 0x7f; + tv |= (t << (7 * n)); n++; } diff --git a/source/libs/tdb/test/CMakeLists.txt b/source/libs/tdb/test/CMakeLists.txt index 2d77c1f4e9..b2c8aaf9bc 100644 --- a/source/libs/tdb/test/CMakeLists.txt +++ b/source/libs/tdb/test/CMakeLists.txt @@ -1,3 +1,7 @@ # tdbTest add_executable(tdbTest "tdbTest.cpp") -target_link_libraries(tdbTest tdb gtest gtest_main) \ No newline at end of file +target_link_libraries(tdbTest tdb gtest gtest_main) + +# tdbUtilTest +add_executable(tdbUtilTest "tdbUtilTest.cpp") +target_link_libraries(tdbUtilTest tdb gtest gtest_main) \ No newline at end of file diff --git a/source/libs/tdb/test/tdbUtilTest.cpp b/source/libs/tdb/test/tdbUtilTest.cpp new file mode 100644 index 0000000000..5fd45daa3e --- /dev/null +++ b/source/libs/tdb/test/tdbUtilTest.cpp @@ -0,0 +1,20 @@ +#include + +#include "tdbInt.h" + +#include + +TEST(tdb_util_test, simple_test) { + int vEncode = 5000; + int vDecode; + int nEncode; + int nDecode; + u8 buffer[128]; + + nEncode = tdbPutVarInt(buffer, vEncode); + + nDecode = tdbGetVarInt(buffer, &vDecode); + + GTEST_ASSERT_EQ(nEncode, nDecode); + GTEST_ASSERT_EQ(vEncode, vDecode); +} \ No newline at end of file From 3c7927d2b4dfc046e00643a567b57427da3ed438 Mon Sep 17 00:00:00 2001 From: Xiaoyu Wang Date: Wed, 6 Apr 2022 06:50:38 -0400 Subject: [PATCH 22/25] normal table rollup translate, and condition rewrite --- include/libs/nodes/cmdnodes.h | 1 + source/libs/parser/src/parAstCreater.c | 1 + source/libs/parser/src/parAstParser.c | 2 +- source/libs/parser/src/parCalcConst.c | 83 ++++++++++++++++++++++- source/libs/parser/src/parTranslater.c | 72 ++++++++++++++------ source/libs/parser/test/parserAstTest.cpp | 22 +++++- 6 files changed, 157 insertions(+), 24 deletions(-) diff --git a/include/libs/nodes/cmdnodes.h b/include/libs/nodes/cmdnodes.h index 069aec14b5..921d3cc07b 100644 --- a/include/libs/nodes/cmdnodes.h +++ b/include/libs/nodes/cmdnodes.h @@ -94,6 +94,7 @@ typedef struct SColumnDefNode { char colName[TSDB_COL_NAME_LEN]; SDataType dataType; char comments[TSDB_STB_COMMENT_LEN]; + bool sma; } SColumnDefNode; typedef struct SCreateTableStmt { diff --git a/source/libs/parser/src/parAstCreater.c b/source/libs/parser/src/parAstCreater.c index ebcecfe3cb..3f7c22f4c5 100644 --- a/source/libs/parser/src/parAstCreater.c +++ b/source/libs/parser/src/parAstCreater.c @@ -1050,6 +1050,7 @@ SNode* createColumnDefNode(SAstCreateContext* pCxt, const SToken* pColName, SDat if (NULL != pComment) { trimString(pComment->z, pComment->n, pCol->comments, sizeof(pCol->comments)); } + pCol->sma = true; return (SNode*)pCol; } diff --git a/source/libs/parser/src/parAstParser.c b/source/libs/parser/src/parAstParser.c index 76af6b7ac7..8d0ddafd26 100644 --- a/source/libs/parser/src/parAstParser.c +++ b/source/libs/parser/src/parAstParser.c @@ -65,7 +65,7 @@ int32_t parse(SParseContext* pParseCxt, SQuery** pQuery) { } default: Parse(pParser, t0.type, t0, &cxt); - // ParseTrace(stdout, ""); + ParseTrace(stdout, ""); if (!cxt.valid) { goto abort_parse; } diff --git a/source/libs/parser/src/parCalcConst.c b/source/libs/parser/src/parCalcConst.c index 46cdcc4243..d21628956a 100644 --- a/source/libs/parser/src/parCalcConst.c +++ b/source/libs/parser/src/parCalcConst.c @@ -90,14 +90,91 @@ static EDealRes calcConst(SNode** pNode, void* pContext) { return DEAL_RES_CONTINUE; } +static bool isCondition(const SNode* pNode) { + if (QUERY_NODE_OPERATOR == nodeType(pNode)) { + return nodesIsComparisonOp((const SOperatorNode*)pNode); + } + return (QUERY_NODE_LOGIC_CONDITION == nodeType(pNode)); +} + +static int32_t rewriteIsTrue(SNode* pSrc, SNode** pIsTrue) { + SOperatorNode* pOp = nodesMakeNode(QUERY_NODE_OPERATOR); + if (NULL == pOp) { + return TSDB_CODE_OUT_OF_MEMORY; + } + pOp->opType = OP_TYPE_IS_TRUE; + pOp->pLeft = pSrc; + *pIsTrue = (SNode*)pOp; + return TSDB_CODE_SUCCESS; +} + +static EDealRes doRewriteCondition(SNode** pNode, void* pContext) { + if (QUERY_NODE_LOGIC_CONDITION == nodeType(*pNode)) { + SNode* pParam = NULL; + FOREACH(pParam, ((SLogicConditionNode*)*pNode)->pParameterList) { + if (!isCondition(pParam)) { + SNode* pIsTrue = NULL; + if (TSDB_CODE_SUCCESS != rewriteIsTrue(pParam, &pIsTrue)) { + ((SCalcConstContext*)pContext)->code = TSDB_CODE_OUT_OF_MEMORY; + return DEAL_RES_ERROR; + } + REPLACE_NODE(pIsTrue); + } + } + } + return DEAL_RES_CONTINUE; +} + +static int32_t rewriteCondition(SCalcConstContext* pCxt, SNode** pNode) { + if (!isCondition(*pNode)) { + return rewriteIsTrue(*pNode, pNode); + } + nodesRewriteExprPostOrder(pNode, doRewriteCondition, pCxt); + return pCxt->code; +} + +static int32_t rewriteConditionForFromTable(SCalcConstContext* pCxt, SNode* pTable) { + if (QUERY_NODE_JOIN_TABLE == nodeType(pTable)) { + SJoinTableNode* pJoin = (SJoinTableNode*)pTable; + pCxt->code = rewriteConditionForFromTable(pCxt, pJoin->pLeft); + if (TSDB_CODE_SUCCESS == pCxt->code) { + pCxt->code = rewriteConditionForFromTable(pCxt, pJoin->pRight); + } + if (TSDB_CODE_SUCCESS == pCxt->code) { + pCxt->code = rewriteCondition(pCxt, &pJoin->pOnCond); + } + } + return pCxt->code; +} + +static int32_t calcConstFromTable(SCalcConstContext* pCxt, SSelectStmt* pSelect) { + nodesRewriteExprPostOrder(&pSelect->pFromTable, calcConst, pCxt); + if (TSDB_CODE_SUCCESS == pCxt->code) { + pCxt->code = rewriteConditionForFromTable(pCxt, pSelect->pFromTable); + } + return pCxt->code; +} + +static int32_t calcConstCondition(SCalcConstContext* pCxt, SNode** pCond) { + if (NULL == *pCond) { + return TSDB_CODE_SUCCESS; + } + + nodesRewriteExprPostOrder(pCond, calcConst, pCxt); + if (TSDB_CODE_SUCCESS == pCxt->code) { + pCxt->code = rewriteCondition(pCxt, pCond); + } + return pCxt->code; +} + static int32_t calcConstSelect(SSelectStmt* pSelect) { SCalcConstContext cxt = { .code = TSDB_CODE_SUCCESS }; nodesRewriteExprsPostOrder(pSelect->pProjectionList, calcConst, &cxt); if (TSDB_CODE_SUCCESS == cxt.code) { - nodesRewriteExprPostOrder(&pSelect->pFromTable, calcConst, &cxt); + cxt.code = calcConstFromTable(&cxt, pSelect); } if (TSDB_CODE_SUCCESS == cxt.code) { - nodesRewriteExprPostOrder(&pSelect->pWhere, calcConst, &cxt); + cxt.code = calcConstCondition(&cxt, &pSelect->pWhere); } if (TSDB_CODE_SUCCESS == cxt.code) { nodesRewriteExprsPostOrder(pSelect->pPartitionByList, calcConst, &cxt); @@ -109,7 +186,7 @@ static int32_t calcConstSelect(SSelectStmt* pSelect) { nodesRewriteExprsPostOrder(pSelect->pGroupByList, calcConst, &cxt); } if (TSDB_CODE_SUCCESS == cxt.code) { - nodesRewriteExprPostOrder(&pSelect->pHaving, calcConst, &cxt); + cxt.code = calcConstCondition(&cxt, &pSelect->pHaving); } if (TSDB_CODE_SUCCESS == cxt.code) { nodesRewriteExprsPostOrder(pSelect->pOrderByList, calcConst, &cxt); diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index bf909a0eaf..813cf3477d 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -1115,7 +1115,7 @@ static int32_t columnNodeToField(SNodeList* pList, SArray** pArray) { return TSDB_CODE_SUCCESS; } -static const SColumnDefNode* findColDef(const SNodeList* pCols, const SColumnNode* pCol) { +static SColumnDefNode* findColDef(SNodeList* pCols, const SColumnNode* pCol) { SNode* pColDef = NULL; FOREACH(pColDef, pCols) { if (0 == strcmp(pCol->colName, ((SColumnDefNode*)pColDef)->colName)) { @@ -1125,16 +1125,20 @@ static const SColumnDefNode* findColDef(const SNodeList* pCols, const SColumnNod return NULL; } -static int32_t checkCreateSuperTable(STranslateContext* pCxt, SCreateTableStmt* pStmt) { +static int32_t checkCreateTable(STranslateContext* pCxt, SCreateTableStmt* pStmt) { if (NULL != pStmt->pOptions->pSma) { SNode* pNode = NULL; + FOREACH(pNode, pStmt->pCols) { + ((SColumnDefNode*)pNode)->sma = false; + } FOREACH(pNode, pStmt->pOptions->pSma) { SColumnNode* pSmaCol = (SColumnNode*)pNode; - const SColumnDefNode* pColDef = findColDef(pStmt->pCols, pSmaCol); + SColumnDefNode* pColDef = findColDef(pStmt->pCols, pSmaCol); if (NULL == pColDef) { return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_COLUMN, pSmaCol->colName); } pSmaCol->node.resType = pColDef->dataType; + pColDef->sma = true; } } if (NULL != pStmt->pOptions->pFuncs) { @@ -1154,7 +1158,7 @@ static int32_t getAggregationMethod(SNodeList* pFuncs) { } static int32_t translateCreateSuperTable(STranslateContext* pCxt, SCreateTableStmt* pStmt) { - int32_t code = checkCreateSuperTable(pCxt, pStmt); + int32_t code = checkCreateTable(pCxt, pStmt); if (TSDB_CODE_SUCCESS != code) { return code; } @@ -1485,7 +1489,7 @@ static int32_t nodeTypeToShowType(ENodeType nt) { case QUERY_NODE_SHOW_CONNECTIONS_STMT: return TSDB_MGMT_TABLE_CONNS; case QUERY_NODE_SHOW_LICENCE_STMT: - return 0; // todo + return TSDB_MGMT_TABLE_GRANTS; case QUERY_NODE_SHOW_QUERIES_STMT: return TSDB_MGMT_TABLE_QUERIES; case QUERY_NODE_SHOW_SCORES_STMT: @@ -2157,11 +2161,11 @@ typedef struct SVgroupTablesBatch { char dbName[TSDB_DB_NAME_LEN]; } SVgroupTablesBatch; -static void toSchema(const SColumnDefNode* pCol, col_id_t colId, SSchemaEx* pSchema) { +static void toSchemaEx(const SColumnDefNode* pCol, col_id_t colId, SSchemaEx* pSchema) { pSchema->colId = colId; pSchema->type = pCol->dataType.type; pSchema->bytes = calcTypeBytes(pCol->dataType); - pSchema->sma = TSDB_BSMA_TYPE_LATEST; // TODO: use default value currently, and use the real value later. + pSchema->sma = pCol->sma ? TSDB_BSMA_TYPE_LATEST : TSDB_BSMA_TYPE_NONE; strcpy(pSchema->name, pCol->colName); } @@ -2171,33 +2175,60 @@ static void destroyCreateTbReq(SVCreateTbReq* pReq) { taosMemoryFreeClear(pReq->ntbCfg.pSchema); } -static int32_t buildNormalTableBatchReq(int32_t acctId, const char* pDbName, const char* pTableName, - const SNodeList* pColumns, const SVgroupInfo* pVgroupInfo, SVgroupTablesBatch* pBatch) { +static int32_t buildSmaParam(STableOptions* pOptions, SVCreateTbReq* pReq) { + if (0 == LIST_LENGTH(pOptions->pFuncs)) { + return TSDB_CODE_SUCCESS; + } + + pReq->ntbCfg.pRSmaParam = taosMemoryCalloc(1, sizeof(SRSmaParam)); + if (NULL == pReq->ntbCfg.pRSmaParam) { + return TSDB_CODE_OUT_OF_MEMORY; + } + pReq->ntbCfg.pRSmaParam->delay = pOptions->delay; + pReq->ntbCfg.pRSmaParam->xFilesFactor = pOptions->filesFactor; + pReq->ntbCfg.pRSmaParam->nFuncIds = LIST_LENGTH(pOptions->pFuncs); + pReq->ntbCfg.pRSmaParam->pFuncIds = taosMemoryCalloc(pReq->ntbCfg.pRSmaParam->nFuncIds, sizeof(func_id_t)); + if (NULL == pReq->ntbCfg.pRSmaParam->pFuncIds) { + return TSDB_CODE_OUT_OF_MEMORY; + } + int32_t index = 0; + SNode* pFunc = NULL; + FOREACH(pFunc, pOptions->pFuncs) { + pReq->ntbCfg.pRSmaParam->pFuncIds[index++] = ((SFunctionNode*)pFunc)->funcId; + } + + return TSDB_CODE_SUCCESS; +} + +static int32_t buildNormalTableBatchReq(int32_t acctId, const SCreateTableStmt* pStmt, const SVgroupInfo* pVgroupInfo, SVgroupTablesBatch* pBatch) { char dbFName[TSDB_DB_FNAME_LEN] = {0}; SName name = { .type = TSDB_DB_NAME_T, .acctId = acctId }; - strcpy(name.dbname, pDbName); + strcpy(name.dbname, pStmt->dbName); tNameGetFullDbName(&name, dbFName); SVCreateTbReq req = {0}; req.type = TD_NORMAL_TABLE; req.dbFName = strdup(dbFName); - req.name = strdup(pTableName); - req.ntbCfg.nCols = LIST_LENGTH(pColumns); + req.name = strdup(pStmt->tableName); + req.ntbCfg.nCols = LIST_LENGTH(pStmt->pCols); req.ntbCfg.pSchema = taosMemoryCalloc(req.ntbCfg.nCols, sizeof(SSchemaEx)); if (NULL == req.name || NULL == req.ntbCfg.pSchema) { destroyCreateTbReq(&req); return TSDB_CODE_OUT_OF_MEMORY; } SNode* pCol; - int32_t index = 0; - FOREACH(pCol, pColumns) { - toSchema((SColumnDefNode*)pCol, index + 1, req.ntbCfg.pSchema + index); + col_id_t index = 0; + FOREACH(pCol, pStmt->pCols) { + toSchemaEx((SColumnDefNode*)pCol, index + 1, req.ntbCfg.pSchema + index); ++index; } - // TODO: use the real sma for normal table. + if (TSDB_CODE_SUCCESS != buildSmaParam(pStmt->pOptions, &req)) { + destroyCreateTbReq(&req); + return TSDB_CODE_OUT_OF_MEMORY; + } pBatch->info = *pVgroupInfo; - strcpy(pBatch->dbName, pDbName); + strcpy(pBatch->dbName, pStmt->dbName); pBatch->req.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq)); if (NULL == pBatch->req.pArray) { destroyCreateTbReq(&req); @@ -2278,7 +2309,7 @@ static int32_t buildCreateTableDataBlock(int32_t acctId, const SCreateTableStmt* } SVgroupTablesBatch tbatch = {0}; - int32_t code = buildNormalTableBatchReq(acctId, pStmt->dbName, pStmt->tableName, pStmt->pCols, pInfo, &tbatch); + int32_t code = buildNormalTableBatchReq(acctId, pStmt, pInfo, &tbatch); if (TSDB_CODE_SUCCESS == code) { code = serializeVgroupTablesBatch(&tbatch, *pBufArray); } @@ -2293,8 +2324,11 @@ static int32_t buildCreateTableDataBlock(int32_t acctId, const SCreateTableStmt* static int32_t rewriteCreateTable(STranslateContext* pCxt, SQuery* pQuery) { SCreateTableStmt* pStmt = (SCreateTableStmt*)pQuery->pRoot; + int32_t code = checkCreateTable(pCxt, pStmt); SVgroupInfo info = {0}; - int32_t code = getTableHashVgroup(pCxt, pStmt->dbName, pStmt->tableName, &info); + if (TSDB_CODE_SUCCESS == code) { + code = getTableHashVgroup(pCxt, pStmt->dbName, pStmt->tableName, &info); + } SArray* pBufArray = NULL; if (TSDB_CODE_SUCCESS == code) { code = buildCreateTableDataBlock(pCxt->pParseCxt->acctId, pStmt, &info, &pBufArray); diff --git a/source/libs/parser/test/parserAstTest.cpp b/source/libs/parser/test/parserAstTest.cpp index 72670bb79a..8b19701c52 100644 --- a/source/libs/parser/test/parserAstTest.cpp +++ b/source/libs/parser/test/parserAstTest.cpp @@ -44,7 +44,7 @@ protected: query_ = nullptr; bool res = runImpl(parseCode, translateCode); qDestroyQuery(query_); - if (!res) { + if (1/*!res*/) { dump(); } return res; @@ -69,6 +69,12 @@ private: return (terrno == translateCode); } translatedAstStr_ = toString(query_->pRoot); + code = calculateConstant(&cxt_, query_); + if (code != TSDB_CODE_SUCCESS) { + calcConstErrStr_ = string("code:") + tstrerror(code) + string(", msg:") + errMagBuf_; + return false; + } + calcConstAstStr_ = toString(query_->pRoot); return (TSDB_CODE_SUCCESS == translateCode); } @@ -88,6 +94,13 @@ private: cout << "translate output: " << endl; cout << translatedAstStr_ << endl; } + if (!calcConstErrStr_.empty()) { + cout << "calculateConstant error: " << calcConstErrStr_ << endl; + } + if (!calcConstAstStr_.empty()) { + cout << "calculateConstant output: " << endl; + cout << calcConstAstStr_ << endl; + } } string toString(const SNode* pRoot, bool format = false) { @@ -112,6 +125,8 @@ private: parsedAstStr_.clear(); translateErrStr_.clear(); translatedAstStr_.clear(); + calcConstErrStr_.clear(); + calcConstAstStr_.clear(); } string acctId_; @@ -124,6 +139,8 @@ private: string parsedAstStr_; string translateErrStr_; string translatedAstStr_; + string calcConstErrStr_; + string calcConstAstStr_; }; TEST_F(ParserTest, createAccount) { @@ -191,6 +208,9 @@ TEST_F(ParserTest, selectConstant) { bind("SELECT 1234567890123456789012345678901234567890, 20.1234567890123456789012345678901234567890, 'abc', \"wxy\", TIMESTAMP '2022-02-09 17:30:20', true, false, 15s FROM t1"); ASSERT_TRUE(run()); + + bind("SELECT 123 + 45 FROM t1 where 2 - 1"); + ASSERT_TRUE(run()); } TEST_F(ParserTest, selectExpression) { From eafc0f88bc5f6655a3906ede85ffcc9f15d43f29 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 6 Apr 2022 11:00:52 +0000 Subject: [PATCH 23/25] remove tid from tsdb --- source/dnode/vnode/src/inc/tsdbReadImpl.h | 32 +++++++++++----------- source/dnode/vnode/src/tsdb/tsdbCommit.c | 1 - source/dnode/vnode/src/tsdb/tsdbReadImpl.c | 31 +++++++++++---------- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdbReadImpl.h b/source/dnode/vnode/src/inc/tsdbReadImpl.h index 90a877bb83..682306221d 100644 --- a/source/dnode/vnode/src/inc/tsdbReadImpl.h +++ b/source/dnode/vnode/src/inc/tsdbReadImpl.h @@ -17,12 +17,12 @@ #define _TD_TSDB_READ_IMPL_H_ #include "os.h" +#include "tcommon.h" #include "tfs.h" #include "tsdb.h" #include "tsdbFile.h" -#include "tskiplist.h" #include "tsdbMemory.h" -#include "tcommon.h" +#include "tskiplist.h" #ifdef __cplusplus extern "C" { @@ -31,7 +31,6 @@ extern "C" { typedef struct SReadH SReadH; typedef struct { - int32_t tid; uint32_t len; uint32_t offset; uint32_t hasLast : 2; @@ -81,7 +80,7 @@ typedef struct { TSKEY keyLast; } SBlockV0; -#define SBlock SBlockV0 // latest SBlock definition +#define SBlock SBlockV0 // latest SBlock definition #endif @@ -165,19 +164,19 @@ typedef struct { typedef void SAggrBlkData; // SBlockCol cols[]; struct SReadH { - STsdb * pRepo; + STsdb *pRepo; SDFileSet rSet; // FSET to read - SArray * aBlkIdx; // SBlockIdx array - STable * pTable; // table to read - SBlockIdx * pBlkIdx; // current reading table SBlockIdx + SArray *aBlkIdx; // SBlockIdx array + STable *pTable; // table to read + SBlockIdx *pBlkIdx; // current reading table SBlockIdx int cidx; - SBlockInfo * pBlkInfo; - SBlockData * pBlkData; // Block info + SBlockInfo *pBlkInfo; + SBlockData *pBlkData; // Block info SAggrBlkData *pAggrBlkData; // Aggregate Block info - SDataCols * pDCols[2]; - void * pBuf; // buffer - void * pCBuf; // compression buffer - void * pExBuf; // extra buffer + SDataCols *pDCols[2]; + void *pBuf; // buffer + void *pCBuf; // compression buffer + void *pExBuf; // extra buffer }; #define TSDB_READ_REPO(rh) ((rh)->pRepo) @@ -222,14 +221,15 @@ int tsdbLoadBlockIdx(SReadH *pReadh); int tsdbSetReadTable(SReadH *pReadh, STable *pTable); int tsdbLoadBlockInfo(SReadH *pReadh, void *pTarget); int tsdbLoadBlockData(SReadH *pReadh, SBlock *pBlock, SBlockInfo *pBlockInfo); -int tsdbLoadBlockDataCols(SReadH *pReadh, SBlock *pBlock, SBlockInfo *pBlkInfo, const int16_t *colIds, int numOfColsIds); +int tsdbLoadBlockDataCols(SReadH *pReadh, SBlock *pBlock, SBlockInfo *pBlkInfo, const int16_t *colIds, + int numOfColsIds); int tsdbLoadBlockStatis(SReadH *pReadh, SBlock *pBlock); int tsdbEncodeSBlockIdx(void **buf, SBlockIdx *pIdx); void *tsdbDecodeSBlockIdx(void *buf, SBlockIdx *pIdx); void tsdbGetBlockStatis(SReadH *pReadh, SDataStatis *pStatis, int numOfCols, SBlock *pBlock); static FORCE_INLINE int tsdbMakeRoom(void **ppBuf, size_t size) { - void * pBuf = *ppBuf; + void *pBuf = *ppBuf; size_t tsize = taosTSizeof(pBuf); if (tsize < size) { diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit.c b/source/dnode/vnode/src/tsdb/tsdbCommit.c index 2a8875fefe..18d3de94cc 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit.c @@ -701,7 +701,6 @@ int tsdbWriteBlockInfoImpl(SDFile *pHeadf, STable *pTable, SArray *pSupA, SArray // Set pIdx pBlock = taosArrayGetLast(pSupA); - pIdx->tid = TABLE_TID(pTable); pIdx->uid = TABLE_UID(pTable); pIdx->hasLast = pBlock->last ? 1 : 0; pIdx->maxKey = pBlock->keyLast; diff --git a/source/dnode/vnode/src/tsdb/tsdbReadImpl.c b/source/dnode/vnode/src/tsdb/tsdbReadImpl.c index 8cb90d76a6..687e2ca776 100644 --- a/source/dnode/vnode/src/tsdb/tsdbReadImpl.c +++ b/source/dnode/vnode/src/tsdb/tsdbReadImpl.c @@ -98,7 +98,7 @@ int tsdbSetAndOpenReadFSet(SReadH *pReadh, SDFileSet *pSet) { void tsdbCloseAndUnsetFSet(SReadH *pReadh) { tsdbResetReadFile(pReadh); } int tsdbLoadBlockIdx(SReadH *pReadh) { - SDFile * pHeadf = TSDB_READ_HEAD_FILE(pReadh); + SDFile *pHeadf = TSDB_READ_HEAD_FILE(pReadh); SBlockIdx blkIdx; ASSERT(taosArrayGetSize(pReadh->aBlkIdx) == 0); @@ -149,8 +149,8 @@ int tsdbLoadBlockIdx(SReadH *pReadh) { } tsize++; - ASSERT(tsize == 1 || ((SBlockIdx *)taosArrayGet(pReadh->aBlkIdx, tsize - 2))->tid < - ((SBlockIdx *)taosArrayGet(pReadh->aBlkIdx, tsize - 1))->tid); + // ASSERT(tsize == 1 || ((SBlockIdx *)taosArrayGet(pReadh->aBlkIdx, tsize - 2))->tid < + // ((SBlockIdx *)taosArrayGet(pReadh->aBlkIdx, tsize - 1))->tid); } return 0; @@ -180,7 +180,7 @@ int tsdbSetReadTable(SReadH *pReadh, STable *pTable) { } SBlockIdx *pBlkIdx = taosArrayGet(pReadh->aBlkIdx, pReadh->cidx); - if (pBlkIdx->tid == TABLE_TID(pTable)) { + if (pBlkIdx->uid == TABLE_TID(pTable)) { if (pBlkIdx->uid == TABLE_UID(pTable)) { pReadh->pBlkIdx = pBlkIdx; } else { @@ -188,7 +188,7 @@ int tsdbSetReadTable(SReadH *pReadh, STable *pTable) { } pReadh->cidx++; break; - } else if (pBlkIdx->tid > TABLE_TID(pTable)) { + } else if (pBlkIdx->uid > TABLE_TID(pTable)) { pReadh->pBlkIdx = NULL; break; } else { @@ -205,7 +205,7 @@ int tsdbSetReadTable(SReadH *pReadh, STable *pTable) { int tsdbLoadBlockInfo(SReadH *pReadh, void *pTarget) { ASSERT(pReadh->pBlkIdx != NULL); - SDFile * pHeadf = TSDB_READ_HEAD_FILE(pReadh); + SDFile *pHeadf = TSDB_READ_HEAD_FILE(pReadh); SBlockIdx *pBlkIdx = pReadh->pBlkIdx; if (tsdbSeekDFile(pHeadf, pBlkIdx->offset, SEEK_SET) < 0) { @@ -237,7 +237,7 @@ int tsdbLoadBlockInfo(SReadH *pReadh, void *pTarget) { return -1; } - ASSERT(pBlkIdx->tid == pReadh->pBlkInfo->tid && pBlkIdx->uid == pReadh->pBlkInfo->uid); + // ASSERT(pBlkIdx->tid == pReadh->pBlkInfo->tid && pBlkIdx->uid == pReadh->pBlkInfo->uid); if (pTarget) { memcpy(pTarget, (void *)(pReadh->pBlkInfo), pBlkIdx->len); @@ -275,7 +275,8 @@ int tsdbLoadBlockData(SReadH *pReadh, SBlock *pBlock, SBlockInfo *pBlkInfo) { return 0; } -int tsdbLoadBlockDataCols(SReadH *pReadh, SBlock *pBlock, SBlockInfo *pBlkInfo, const int16_t *colIds, int numOfColsIds) { +int tsdbLoadBlockDataCols(SReadH *pReadh, SBlock *pBlock, SBlockInfo *pBlkInfo, const int16_t *colIds, + int numOfColsIds) { ASSERT(pBlock->numOfSubBlocks > 0); int8_t update = pReadh->pRepo->config.update; @@ -388,7 +389,7 @@ static int tsdbLoadBlockOffset(SReadH *pReadh, SBlock *pBlock) { int tsdbEncodeSBlockIdx(void **buf, SBlockIdx *pIdx) { int tlen = 0; - tlen += taosEncodeVariantI32(buf, pIdx->tid); + // tlen += taosEncodeVariantI32(buf, pIdx->tid); tlen += taosEncodeVariantU32(buf, pIdx->len); tlen += taosEncodeVariantU32(buf, pIdx->offset); tlen += taosEncodeFixedU8(buf, pIdx->hasLast); @@ -404,7 +405,7 @@ void *tsdbDecodeSBlockIdx(void *buf, SBlockIdx *pIdx) { uint32_t numOfBlocks = 0; uint64_t value = 0; - if ((buf = taosDecodeVariantI32(buf, &(pIdx->tid))) == NULL) return NULL; + // if ((buf = taosDecodeVariantI32(buf, &(pIdx->tid))) == NULL) return NULL; if ((buf = taosDecodeVariantU32(buf, &(pIdx->len))) == NULL) return NULL; if ((buf = taosDecodeVariantU32(buf, &(pIdx->offset))) == NULL) return NULL; if ((buf = taosDecodeFixedU8(buf, &(hasLast))) == NULL) return NULL; @@ -538,9 +539,9 @@ static int tsdbLoadBlockDataImpl(SReadH *pReadh, SBlock *pBlock, SDataCols *pDat pDataCols->numOfRows = pBlock->numOfRows; // Recover the data - int ccol = 0; // loop iter for SBlockCol object - int dcol = 0; // loop iter for SDataCols object - int nBitmaps = (int)TD_BITMAP_BYTES(pBlock->numOfRows); + int ccol = 0; // loop iter for SBlockCol object + int dcol = 0; // loop iter for SDataCols object + int nBitmaps = (int)TD_BITMAP_BYTES(pBlock->numOfRows); SBlockCol *pBlockCol = NULL; while (dcol < pDataCols->numOfCols) { SDataCol *pDataCol = &(pDataCols->cols[dcol]); @@ -686,7 +687,7 @@ static int tsdbLoadBlockDataColsImpl(SReadH *pReadh, SBlock *pBlock, SDataCols * ASSERT(pBlock->numOfSubBlocks == 0 || pBlock->numOfSubBlocks == 1); ASSERT(colIds[0] == PRIMARYKEY_TIMESTAMP_COL_ID); - SDFile * pDFile = (pBlock->last) ? TSDB_READ_LAST_FILE(pReadh) : TSDB_READ_DATA_FILE(pReadh); + SDFile *pDFile = (pBlock->last) ? TSDB_READ_LAST_FILE(pReadh) : TSDB_READ_DATA_FILE(pReadh); SBlockCol blockCol = {0}; tdResetDataCols(pDataCols); @@ -700,7 +701,7 @@ static int tsdbLoadBlockDataColsImpl(SReadH *pReadh, SBlock *pBlock, SDataCols * int ccol = 0; for (int i = 0; i < numOfColIds; i++) { int16_t colId = colIds[i]; - SDataCol * pDataCol = NULL; + SDataCol *pDataCol = NULL; SBlockCol *pBlockCol = NULL; while (true) { From 70810ddb39382910b950390897d582cc30eecd90 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 6 Apr 2022 11:37:34 +0000 Subject: [PATCH 24/25] fix a small bug --- source/dnode/vnode/src/meta/metaTDBImpl.c | 8 ++++++++ source/libs/tdb/src/db/tdbBtree.c | 20 +++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/source/dnode/vnode/src/meta/metaTDBImpl.c b/source/dnode/vnode/src/meta/metaTDBImpl.c index 3ab26c3d4d..812ec84a9a 100644 --- a/source/dnode/vnode/src/meta/metaTDBImpl.c +++ b/source/dnode/vnode/src/meta/metaTDBImpl.c @@ -226,6 +226,14 @@ int metaSaveTableToDB(SMeta *pMeta, STbCfg *pTbCfg) { uid = metaGenerateUid(pMeta); } + // check name and uid unique + if (tdbDbGet(pMetaDb->pTbDB, &uid, sizeof(uid), NULL, NULL) == 0) { + return -1; + } + if (tdbDbGet(pMetaDb->pNameIdx, pTbCfg->name, strlen(pTbCfg->name) + 1, NULL, NULL) == 0) { + return -1; + } + // save to table.db pKey = &uid; kLen = sizeof(uid); diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index f760598714..9df749bec7 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -233,7 +233,7 @@ int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkL ASSERT(0); } - if (cret) { + if (btc.idx < 0 || cret) { tdbBtcClose(&btc); return -1; } @@ -253,15 +253,17 @@ int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkL memcpy(*ppKey, cd.pKey, cd.kLen); } - pTVal = TDB_REALLOC(*ppVal, cd.vLen); - if (pTVal == NULL) { - tdbBtcClose(&btc); - ASSERT(0); - return -1; + if (ppVal) { + pTVal = TDB_REALLOC(*ppVal, cd.vLen); + if (pTVal == NULL) { + tdbBtcClose(&btc); + ASSERT(0); + return -1; + } + *ppVal = pTVal; + *vLen = cd.vLen; + memcpy(*ppVal, cd.pVal, cd.vLen); } - *ppVal = pTVal; - *vLen = cd.vLen; - memcpy(*ppVal, cd.pVal, cd.vLen); tdbBtcClose(&btc); From ead76ab6d2c599cb1fa2c488ad463d91f64ee471 Mon Sep 17 00:00:00 2001 From: Xiaoyu Wang Date: Wed, 6 Apr 2022 19:59:27 +0800 Subject: [PATCH 25/25] normal table rollup translate, and condition rewrite --- source/libs/parser/src/parAstParser.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/libs/parser/src/parAstParser.c b/source/libs/parser/src/parAstParser.c index 8d0ddafd26..76af6b7ac7 100644 --- a/source/libs/parser/src/parAstParser.c +++ b/source/libs/parser/src/parAstParser.c @@ -65,7 +65,7 @@ int32_t parse(SParseContext* pParseCxt, SQuery** pQuery) { } default: Parse(pParser, t0.type, t0, &cxt); - ParseTrace(stdout, ""); + // ParseTrace(stdout, ""); if (!cxt.valid) { goto abort_parse; }