未验证 提交 5972ee30 编写于 作者: X Xiaoyu Wang 提交者: GitHub

Merge pull request #11332 from taosdata/feature/3.0_wxy

constant condition optimize
...@@ -238,6 +238,7 @@ typedef struct SSelectStmt { ...@@ -238,6 +238,7 @@ typedef struct SSelectStmt {
SNode* pSlimit; SNode* pSlimit;
char stmtName[TSDB_TABLE_NAME_LEN]; char stmtName[TSDB_TABLE_NAME_LEN];
uint8_t precision; uint8_t precision;
bool isEmptyResult;
} SSelectStmt; } SSelectStmt;
typedef enum ESetOperatorType { typedef enum ESetOperatorType {
......
...@@ -44,8 +44,15 @@ typedef struct SCmdMsgInfo { ...@@ -44,8 +44,15 @@ typedef struct SCmdMsgInfo {
void* pExtension; // todo remove it soon void* pExtension; // todo remove it soon
} SCmdMsgInfo; } SCmdMsgInfo;
typedef enum EQueryExecMode {
QUERY_EXEC_MODE_LOCAL = 1,
QUERY_EXEC_MODE_RPC,
QUERY_EXEC_MODE_SCHEDULE,
QUERY_EXEC_MODE_EMPTY_RESULT
} EQueryExecMode;
typedef struct SQuery { typedef struct SQuery {
bool directRpc; EQueryExecMode execMode;
bool haveResultSet; bool haveResultSet;
SNode* pRoot; SNode* pRoot;
int32_t numOfResCols; int32_t numOfResCols;
...@@ -55,7 +62,6 @@ typedef struct SQuery { ...@@ -55,7 +62,6 @@ typedef struct SQuery {
SArray* pDbList; SArray* pDbList;
SArray* pTableList; SArray* pTableList;
bool showRewrite; bool showRewrite;
bool localCmd;
} SQuery; } SQuery;
int32_t qParseQuerySql(SParseContext* pCxt, SQuery** pQuery); int32_t qParseQuerySql(SParseContext* pCxt, SQuery** pQuery);
......
...@@ -281,22 +281,35 @@ int32_t scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList ...@@ -281,22 +281,35 @@ int32_t scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList
SRequestObj* execQueryImpl(STscObj* pTscObj, const char* sql, int sqlLen) { SRequestObj* execQueryImpl(STscObj* pTscObj, const char* sql, int sqlLen) {
SRequestObj* pRequest = NULL; SRequestObj* pRequest = NULL;
SQuery* pQuery = NULL; SQuery* pQuery = NULL;
int32_t code = 0;
SArray* pNodeList = taosArrayInit(4, sizeof(struct SQueryNodeAddr)); SArray* pNodeList = taosArrayInit(4, sizeof(struct SQueryNodeAddr));
CHECK_CODE_GOTO(buildRequest(pTscObj, sql, sqlLen, &pRequest), _return); int32_t code = buildRequest(pTscObj, sql, sqlLen, &pRequest);
CHECK_CODE_GOTO(parseSql(pRequest, false, &pQuery), _return); if (TSDB_CODE_SUCCESS == code) {
code = parseSql(pRequest, false, &pQuery);
}
if (pQuery->localCmd) { if (TSDB_CODE_SUCCESS == code) {
CHECK_CODE_GOTO(execLocalCmd(pRequest, pQuery), _return); switch (pQuery->execMode) {
} else if (pQuery->directRpc) { case QUERY_EXEC_MODE_LOCAL:
CHECK_CODE_GOTO(execDdlQuery(pRequest, pQuery), _return); code = execLocalCmd(pRequest, pQuery);
} else { break;
CHECK_CODE_GOTO(getPlan(pRequest, pQuery, &pRequest->body.pDag, pNodeList), _return); case QUERY_EXEC_MODE_RPC:
CHECK_CODE_GOTO(scheduleQuery(pRequest, pRequest->body.pDag, pNodeList), _return); code = execDdlQuery(pRequest, pQuery);
break;
case QUERY_EXEC_MODE_SCHEDULE:
code = getPlan(pRequest, pQuery, &pRequest->body.pDag, pNodeList);
if (TSDB_CODE_SUCCESS == code) {
code = scheduleQuery(pRequest, pRequest->body.pDag, pNodeList);
}
break;
case QUERY_EXEC_MODE_EMPTY_RESULT:
pRequest->type = TSDB_SQL_RETRIEVE_EMPTY_RESULT;
break;
default:
break;
}
} }
_return:
taosArrayDestroy(pNodeList); taosArrayDestroy(pNodeList);
qDestroyQuery(pQuery); qDestroyQuery(pQuery);
if (NULL != pRequest && TSDB_CODE_SUCCESS != code) { if (NULL != pRequest && TSDB_CODE_SUCCESS != code) {
......
...@@ -149,22 +149,37 @@ static int32_t rewriteConditionForFromTable(SCalcConstContext* pCxt, SNode* pTab ...@@ -149,22 +149,37 @@ static int32_t rewriteConditionForFromTable(SCalcConstContext* pCxt, SNode* pTab
return pCxt->code; return pCxt->code;
} }
static void rewriteConstCondition(SSelectStmt* pSelect, SNode** pCond) {
if (QUERY_NODE_VALUE != nodeType(*pCond)) {
return;
}
if (((SValueNode*)*pCond)->datum.b) {
nodesDestroyNode(*pCond);
*pCond = NULL;
} else {
pSelect->isEmptyResult = true;
}
}
static int32_t calcConstFromTable(SCalcConstContext* pCxt, SSelectStmt* pSelect) { static int32_t calcConstFromTable(SCalcConstContext* pCxt, SSelectStmt* pSelect) {
nodesRewriteExprPostOrder(&pSelect->pFromTable, calcConst, pCxt); pCxt->code = rewriteConditionForFromTable(pCxt, pSelect->pFromTable);
if (TSDB_CODE_SUCCESS == pCxt->code) { if (TSDB_CODE_SUCCESS == pCxt->code) {
pCxt->code = rewriteConditionForFromTable(pCxt, pSelect->pFromTable); nodesRewriteExprPostOrder(&pSelect->pFromTable, calcConst, pCxt);
} }
return pCxt->code; return pCxt->code;
} }
static int32_t calcConstCondition(SCalcConstContext* pCxt, SNode** pCond) { static int32_t calcConstCondition(SCalcConstContext* pCxt, SSelectStmt* pSelect, SNode** pCond) {
if (NULL == *pCond) { if (NULL == *pCond) {
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
nodesRewriteExprPostOrder(pCond, calcConst, pCxt); pCxt->code = rewriteCondition(pCxt, pCond);
if (TSDB_CODE_SUCCESS == pCxt->code) { if (TSDB_CODE_SUCCESS == pCxt->code) {
pCxt->code = rewriteCondition(pCxt, pCond); nodesRewriteExprPostOrder(pCond, calcConst, pCxt);
}
if (TSDB_CODE_SUCCESS == pCxt->code) {
rewriteConstCondition(pSelect, pCond);
} }
return pCxt->code; return pCxt->code;
} }
...@@ -176,7 +191,7 @@ static int32_t calcConstSelect(SSelectStmt* pSelect) { ...@@ -176,7 +191,7 @@ static int32_t calcConstSelect(SSelectStmt* pSelect) {
cxt.code = calcConstFromTable(&cxt, pSelect); cxt.code = calcConstFromTable(&cxt, pSelect);
} }
if (TSDB_CODE_SUCCESS == cxt.code) { if (TSDB_CODE_SUCCESS == cxt.code) {
cxt.code = calcConstCondition(&cxt, &pSelect->pWhere); cxt.code = calcConstCondition(&cxt, pSelect, &pSelect->pWhere);
} }
if (TSDB_CODE_SUCCESS == cxt.code) { if (TSDB_CODE_SUCCESS == cxt.code) {
nodesRewriteExprsPostOrder(pSelect->pPartitionByList, calcConst, &cxt); nodesRewriteExprsPostOrder(pSelect->pPartitionByList, calcConst, &cxt);
...@@ -188,7 +203,7 @@ static int32_t calcConstSelect(SSelectStmt* pSelect) { ...@@ -188,7 +203,7 @@ static int32_t calcConstSelect(SSelectStmt* pSelect) {
nodesRewriteExprsPostOrder(pSelect->pGroupByList, calcConst, &cxt); nodesRewriteExprsPostOrder(pSelect->pGroupByList, calcConst, &cxt);
} }
if (TSDB_CODE_SUCCESS == cxt.code) { if (TSDB_CODE_SUCCESS == cxt.code) {
cxt.code = calcConstCondition(&cxt, &pSelect->pHaving); cxt.code = calcConstCondition(&cxt, pSelect, &pSelect->pHaving);
} }
if (TSDB_CODE_SUCCESS == cxt.code) { if (TSDB_CODE_SUCCESS == cxt.code) {
nodesRewriteExprsPostOrder(pSelect->pOrderByList, calcConst, &cxt); nodesRewriteExprsPostOrder(pSelect->pOrderByList, calcConst, &cxt);
...@@ -208,6 +223,22 @@ static int32_t calcConstQuery(SNode* pStmt) { ...@@ -208,6 +223,22 @@ static int32_t calcConstQuery(SNode* pStmt) {
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
static bool isEmptyResultQuery(SNode* pStmt) {
switch (nodeType(pStmt)) {
case QUERY_NODE_SELECT_STMT:
return ((SSelectStmt*)pStmt)->isEmptyResult;
case QUERY_NODE_EXPLAIN_STMT:
return isEmptyResultQuery(((SExplainStmt*)pStmt)->pQuery);
default:
break;
}
return false;
}
int32_t calculateConstant(SParseContext* pParseCxt, SQuery* pQuery) { int32_t calculateConstant(SParseContext* pParseCxt, SQuery* pQuery) {
return calcConstQuery(pQuery->pRoot); int32_t code = calcConstQuery(pQuery->pRoot);
if (TSDB_CODE_SUCCESS == code) {
pQuery->execMode = isEmptyResultQuery(pQuery->pRoot) ? QUERY_EXEC_MODE_EMPTY_RESULT : pQuery->execMode;
}
return code;
} }
...@@ -1084,7 +1084,7 @@ int32_t parseInsertSql(SParseContext* pContext, SQuery** pQuery) { ...@@ -1084,7 +1084,7 @@ int32_t parseInsertSql(SParseContext* pContext, SQuery** pQuery) {
if (NULL == *pQuery) { if (NULL == *pQuery) {
return TSDB_CODE_OUT_OF_MEMORY; return TSDB_CODE_OUT_OF_MEMORY;
} }
(*pQuery)->directRpc = false; (*pQuery)->execMode = QUERY_EXEC_MODE_SCHEDULE;
(*pQuery)->haveResultSet = false; (*pQuery)->haveResultSet = false;
(*pQuery)->msgType = TDMT_VND_SUBMIT; (*pQuery)->msgType = TDMT_VND_SUBMIT;
(*pQuery)->pRoot = (SNode*)context.pOutput; (*pQuery)->pRoot = (SNode*)context.pOutput;
......
...@@ -2923,21 +2923,23 @@ static int32_t setQuery(STranslateContext* pCxt, SQuery* pQuery) { ...@@ -2923,21 +2923,23 @@ static int32_t setQuery(STranslateContext* pCxt, SQuery* pQuery) {
switch (nodeType(pQuery->pRoot)) { switch (nodeType(pQuery->pRoot)) {
case QUERY_NODE_SELECT_STMT: case QUERY_NODE_SELECT_STMT:
case QUERY_NODE_EXPLAIN_STMT: case QUERY_NODE_EXPLAIN_STMT:
pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
pQuery->haveResultSet = true; pQuery->haveResultSet = true;
pQuery->msgType = TDMT_VND_QUERY; pQuery->msgType = TDMT_VND_QUERY;
break; break;
case QUERY_NODE_VNODE_MODIF_STMT: case QUERY_NODE_VNODE_MODIF_STMT:
pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
pQuery->msgType = TDMT_VND_CREATE_TABLE; pQuery->msgType = TDMT_VND_CREATE_TABLE;
break; break;
case QUERY_NODE_DESCRIBE_STMT: case QUERY_NODE_DESCRIBE_STMT:
pQuery->localCmd = true; pQuery->execMode = QUERY_EXEC_MODE_LOCAL;
pQuery->haveResultSet = true; pQuery->haveResultSet = true;
break; break;
case QUERY_NODE_RESET_QUERY_CACHE_STMT: case QUERY_NODE_RESET_QUERY_CACHE_STMT:
pQuery->localCmd = true; pQuery->execMode = QUERY_EXEC_MODE_LOCAL;
break; break;
default: default:
pQuery->directRpc = true; pQuery->execMode = QUERY_EXEC_MODE_RPC;
if (NULL != pCxt->pCmdMsg) { if (NULL != pCxt->pCmdMsg) {
TSWAP(pQuery->pCmdMsg, pCxt->pCmdMsg, SCmdMsgInfo*); TSWAP(pQuery->pCmdMsg, pCxt->pCmdMsg, SCmdMsgInfo*);
pQuery->msgType = pQuery->pCmdMsg->msgType; pQuery->msgType = pQuery->pCmdMsg->msgType;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册