From f99a61a4644e3fb08ad6e531400f068db70dff4e Mon Sep 17 00:00:00 2001 From: Xiaoyu Wang Date: Fri, 8 Jul 2022 21:05:19 +0800 Subject: [PATCH] fix: some problem of parser and planner --- include/util/taoserror.h | 1 + source/libs/parser/src/parAstParser.c | 3 + source/libs/parser/src/parTranslater.c | 110 +++++++++++------- source/libs/planner/src/planLogicCreater.c | 7 +- source/libs/planner/test/planIntervalTest.cpp | 2 + source/libs/scalar/src/scalar.c | 14 +-- tests/script/tsim/show/basic.sim | 4 +- .../2-query/query_cols_tags_and_or.py | 20 ++-- 8 files changed, 93 insertions(+), 68 deletions(-) diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 41d5910625..05d1bd8ae3 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -582,6 +582,7 @@ int32_t* taosGetErrno(); #define TSDB_CODE_PAR_INVALID_INTERP_CLAUSE TAOS_DEF_ERROR_CODE(0, 0x265D) #define TSDB_CODE_PAR_NO_VALID_FUNC_IN_WIN TAOS_DEF_ERROR_CODE(0, 0x265E) #define TSDB_CODE_PAR_ONLY_SUPPORT_SINGLE_TABLE TAOS_DEF_ERROR_CODE(0, 0x265F) +#define TSDB_CODE_PAR_INVALID_SMA_INDEX TAOS_DEF_ERROR_CODE(0, 0x265C) //planner #define TSDB_CODE_PLAN_INTERNAL_ERROR TAOS_DEF_ERROR_CODE(0, 0x2700) diff --git a/source/libs/parser/src/parAstParser.c b/source/libs/parser/src/parAstParser.c index f38def0b1d..2060c3da4c 100644 --- a/source/libs/parser/src/parAstParser.c +++ b/source/libs/parser/src/parAstParser.c @@ -270,6 +270,9 @@ static int32_t collectMetaKeyFromCreateIndex(SCollectMetaKeyCxt* pCxt, SCreateIn if (TSDB_CODE_SUCCESS == code) { code = reserveDbVgInfoInCache(pCxt->pParseCxt->acctId, pCxt->pParseCxt->db, pCxt->pMetaCache); } + if (TSDB_CODE_SUCCESS == code) { + code = reserveDbCfgInCache(pCxt->pParseCxt->acctId, pCxt->pParseCxt->db, pCxt->pMetaCache); + } } return code; } diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 4ffcd34d70..46015e5bb5 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -962,6 +962,10 @@ static EDealRes translateValue(STranslateContext* pCxt, SValueNode* pVal) { return translateValueImpl(pCxt, pVal, dt, false); } +static int32_t doTranslateValue(STranslateContext* pCxt, SValueNode* pVal) { + return DEAL_RES_ERROR == translateValue(pCxt, pVal) ? pCxt->errCode : TSDB_CODE_SUCCESS; +} + static bool isMultiResFunc(SNode* pNode) { if (NULL == pNode) { return false; @@ -1430,6 +1434,24 @@ static EDealRes rewriteColToSelectValFunc(STranslateContext* pCxt, SNode** pNode return TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR; } +static EDealRes rewriteExprToGroupKeyFunc(STranslateContext* pCxt, SNode** pNode) { + SFunctionNode* pFunc = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION); + if (NULL == pFunc) { + pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY; + return DEAL_RES_ERROR; + } + + strcpy(pFunc->functionName, "_group_key"); + strcpy(pFunc->node.aliasName, ((SExprNode*)*pNode)->aliasName); + pCxt->errCode = nodesListMakeAppend(&pFunc->pParameterList, *pNode); + if (TSDB_CODE_SUCCESS == pCxt->errCode) { + *pNode = (SNode*)pFunc; + pCxt->errCode = fmGetFuncInfo(pFunc, pCxt->msgBuf.buf, pCxt->msgBuf.len); + } + + return (TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR); +} + static EDealRes doCheckExprForGroupBy(SNode** pNode, void* pContext) { SCheckExprForGroupByCxt* pCxt = (SCheckExprForGroupByCxt*)pContext; if (!nodesIsExprNode(*pNode) || isAliasColumn(*pNode)) { @@ -1455,7 +1477,7 @@ static EDealRes doCheckExprForGroupBy(SNode** pNode, void* pContext) { SNode* pPartKey = NULL; FOREACH(pPartKey, ((SSelectStmt*)pCxt->pTranslateCxt->pCurrStmt)->pPartitionByList) { if (nodesEqualNode(pPartKey, *pNode)) { - return DEAL_RES_IGNORE_CHILD; + return rewriteExprToGroupKeyFunc(pCxt->pTranslateCxt, pNode); } } if (isScanPseudoColumnFunc(*pNode) || QUERY_NODE_COLUMN == nodeType(*pNode)) { @@ -1515,35 +1537,30 @@ static int32_t rewriteColsToSelectValFunc(STranslateContext* pCxt, SSelectStmt* typedef struct CheckAggColCoexistCxt { STranslateContext* pTranslateCxt; - bool existAggFunc; + bool existVectorFunc; bool existCol; - bool existIndefiniteRowsFunc; int32_t selectFuncNum; - bool existOtherAggFunc; + bool existOtherVectorFunc; } CheckAggColCoexistCxt; -static EDealRes doCheckAggColCoexist(SNode* pNode, void* pContext) { +static EDealRes doCheckAggColCoexist(SNode** pNode, void* pContext) { CheckAggColCoexistCxt* pCxt = (CheckAggColCoexistCxt*)pContext; - if (isSelectFunc(pNode)) { + if (isSelectFunc(*pNode)) { ++(pCxt->selectFuncNum); - } else if (isAggFunc(pNode)) { - pCxt->existOtherAggFunc = true; - } - if (isAggFunc(pNode)) { - pCxt->existAggFunc = true; - return DEAL_RES_IGNORE_CHILD; + } else if (isAggFunc(*pNode)) { + pCxt->existOtherVectorFunc = true; } - if (isIndefiniteRowsFunc(pNode)) { - pCxt->existIndefiniteRowsFunc = true; + if (isVectorFunc(*pNode)) { + pCxt->existVectorFunc = true; return DEAL_RES_IGNORE_CHILD; } SNode* pPartKey = NULL; FOREACH(pPartKey, ((SSelectStmt*)pCxt->pTranslateCxt->pCurrStmt)->pPartitionByList) { - if (nodesEqualNode(pPartKey, pNode)) { - return DEAL_RES_IGNORE_CHILD; + if (nodesEqualNode(pPartKey, *pNode)) { + return rewriteExprToGroupKeyFunc(pCxt->pTranslateCxt, pNode); } } - if (isScanPseudoColumnFunc(pNode) || QUERY_NODE_COLUMN == nodeType(pNode)) { + if (isScanPseudoColumnFunc(*pNode) || QUERY_NODE_COLUMN == nodeType(*pNode)) { pCxt->existCol = true; } return DEAL_RES_CONTINUE; @@ -1554,24 +1571,20 @@ static int32_t checkAggColCoexist(STranslateContext* pCxt, SSelectStmt* pSelect) return TSDB_CODE_SUCCESS; } CheckAggColCoexistCxt cxt = {.pTranslateCxt = pCxt, - .existAggFunc = false, + .existVectorFunc = false, .existCol = false, - .existIndefiniteRowsFunc = false, .selectFuncNum = 0, - .existOtherAggFunc = false}; - nodesWalkExprs(pSelect->pProjectionList, doCheckAggColCoexist, &cxt); + .existOtherVectorFunc = false}; + nodesRewriteExprs(pSelect->pProjectionList, doCheckAggColCoexist, &cxt); if (!pSelect->isDistinct) { - nodesWalkExprs(pSelect->pOrderByList, doCheckAggColCoexist, &cxt); + nodesRewriteExprs(pSelect->pOrderByList, doCheckAggColCoexist, &cxt); } - if (1 == cxt.selectFuncNum && !cxt.existOtherAggFunc) { + if (1 == cxt.selectFuncNum && !cxt.existOtherVectorFunc) { return rewriteColsToSelectValFunc(pCxt, pSelect); } - if ((cxt.selectFuncNum > 1 || cxt.existAggFunc || NULL != pSelect->pWindow) && cxt.existCol) { + if ((cxt.selectFuncNum > 1 || cxt.existVectorFunc || NULL != pSelect->pWindow) && cxt.existCol) { return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NOT_SINGLE_GROUP); } - if (cxt.existIndefiniteRowsFunc && cxt.existCol) { - return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NOT_ALLOWED_FUNC); - } return TSDB_CODE_SUCCESS; } @@ -4047,29 +4060,42 @@ static int32_t buildCreateSmaReq(STranslateContext* pCxt, SCreateIndexStmt* pStm return code; } -static int32_t translateCreateSmaIndex(STranslateContext* pCxt, SCreateIndexStmt* pStmt) { - if (DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStmt->pOptions->pInterval) || - (NULL != pStmt->pOptions->pOffset && - DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStmt->pOptions->pOffset)) || - (NULL != pStmt->pOptions->pSliding && - DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStmt->pOptions->pSliding))) { - return pCxt->errCode; +static int32_t checkCreateSmaIndex(STranslateContext* pCxt, SCreateIndexStmt* pStmt) { + SDbCfgInfo dbCfg = {0}; + int32_t code = getDBCfg(pCxt, pCxt->pParseCxt->db, &dbCfg); + if (TSDB_CODE_SUCCESS == code && NULL != dbCfg.pRetensions) { + code = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_SMA_INDEX, + "Tables configured with the 'ROLLUP' option do not support creating sma index"); + } + if (TSDB_CODE_SUCCESS == code) { + code = doTranslateValue(pCxt, (SValueNode*)pStmt->pOptions->pInterval); + } + if (TSDB_CODE_SUCCESS == code && NULL != pStmt->pOptions->pOffset) { + code = doTranslateValue(pCxt, (SValueNode*)pStmt->pOptions->pOffset); + } + if (TSDB_CODE_SUCCESS == code && NULL != pStmt->pOptions->pSliding) { + code = doTranslateValue(pCxt, (SValueNode*)pStmt->pOptions->pSliding); } - if (NULL != pStmt->pOptions->pStreamOptions) { + if (TSDB_CODE_SUCCESS == code && NULL != pStmt->pOptions->pStreamOptions) { SStreamOptions* pStreamOpt = (SStreamOptions*)pStmt->pOptions->pStreamOptions; - if (NULL != pStreamOpt->pWatermark && - (DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStreamOpt->pWatermark))) { - return pCxt->errCode; + if (NULL != pStreamOpt->pWatermark) { + code = doTranslateValue(pCxt, (SValueNode*)pStreamOpt->pWatermark); } - - if (NULL != pStreamOpt->pDelay && (DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStreamOpt->pDelay))) { - return pCxt->errCode; + if (TSDB_CODE_SUCCESS == code && NULL != pStreamOpt->pDelay) { + code = doTranslateValue(pCxt, (SValueNode*)pStreamOpt->pDelay); } } + return code; +} + +static int32_t translateCreateSmaIndex(STranslateContext* pCxt, SCreateIndexStmt* pStmt) { SMCreateSmaReq createSmaReq = {0}; - int32_t code = buildCreateSmaReq(pCxt, pStmt, &createSmaReq); + int32_t code = checkCreateSmaIndex(pCxt, pStmt); + if (TSDB_CODE_SUCCESS == code) { + code = buildCreateSmaReq(pCxt, pStmt, &createSmaReq); + } if (TSDB_CODE_SUCCESS == code) { code = buildCmdMsg(pCxt, TDMT_MND_CREATE_SMA, (FSerializeFunc)tSerializeSMCreateSmaReq, &createSmaReq); } diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index 703395b0d5..cb38e1fc18 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -485,10 +485,6 @@ static int32_t createAggLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, code = rewriteExprsForSelect(pAgg->pAggFuncs, pSelect, SQL_CLAUSE_GROUP_BY); } - if (NULL != pSelect->pPartitionByList) { - code = createGroupKeysFromPartKeys(pSelect->pPartitionByList, &pAgg->pGroupKeys); - } - if (NULL != pSelect->pGroupByList) { if (NULL != pAgg->pGroupKeys) { code = nodesListStrictAppendList(pAgg->pGroupKeys, nodesCloneList(pSelect->pGroupByList)); @@ -845,8 +841,7 @@ static int32_t createProjectLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSel } static int32_t createPartitionLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) { - if (NULL == pSelect->pPartitionByList || (pSelect->hasAggFuncs && NULL == pSelect->pWindow) || - NULL != pSelect->pGroupByList) { + if (NULL == pSelect->pPartitionByList) { return TSDB_CODE_SUCCESS; } diff --git a/source/libs/planner/test/planIntervalTest.cpp b/source/libs/planner/test/planIntervalTest.cpp index 10ef09adb9..73fa898bf8 100644 --- a/source/libs/planner/test/planIntervalTest.cpp +++ b/source/libs/planner/test/planIntervalTest.cpp @@ -62,4 +62,6 @@ TEST_F(PlanIntervalTest, stable) { run("SELECT _WSTARTTS, COUNT(*) FROM st1 INTERVAL(10s)"); run("SELECT _WSTARTTS, COUNT(*) FROM st1 PARTITION BY TBNAME INTERVAL(10s)"); + + run("SELECT TBNAME, COUNT(*) FROM st1 PARTITION BY TBNAME INTERVAL(10s)"); } diff --git a/source/libs/scalar/src/scalar.c b/source/libs/scalar/src/scalar.c index e1aeaf219b..91a24986aa 100644 --- a/source/libs/scalar/src/scalar.c +++ b/source/libs/scalar/src/scalar.c @@ -1069,15 +1069,13 @@ static int32_t getArithmeticOperatorResultType(SOperatorNode* pOp) { static int32_t getComparisonOperatorResultType(SOperatorNode* pOp) { SDataType ldt = ((SExprNode*)(pOp->pLeft))->resType; - if (NULL != pOp->pRight) { + if (OP_TYPE_IN == pOp->opType || OP_TYPE_NOT_IN == pOp->opType) { + ((SExprNode*)(pOp->pRight))->resType = ldt; + } else if (nodesIsRegularOp(pOp)) { SDataType rdt = ((SExprNode*)(pOp->pRight))->resType; - if (OP_TYPE_IN == pOp->opType || OP_TYPE_NOT_IN == pOp->opType) { - rdt = ldt; - } else if (nodesIsRegularOp(pOp)) { - if (!IS_VAR_DATA_TYPE(ldt.type) || QUERY_NODE_VALUE != nodeType(pOp->pRight) || - (!IS_STR_DATA_TYPE(rdt.type) && (rdt.type != TSDB_DATA_TYPE_NULL))) { - return TSDB_CODE_TSC_INVALID_OPERATION; - } + if (!IS_VAR_DATA_TYPE(ldt.type) || QUERY_NODE_VALUE != nodeType(pOp->pRight) || + (!IS_STR_DATA_TYPE(rdt.type) && (rdt.type != TSDB_DATA_TYPE_NULL))) { + return TSDB_CODE_TSC_INVALID_OPERATION; } } pOp->node.resType.type = TSDB_DATA_TYPE_BOOL; diff --git a/tests/script/tsim/show/basic.sim b/tests/script/tsim/show/basic.sim index 4d646f39e3..d0643bc7f2 100644 --- a/tests/script/tsim/show/basic.sim +++ b/tests/script/tsim/show/basic.sim @@ -99,7 +99,7 @@ if $rows != 1 then endi #sql select * from information_schema.`streams` sql select * from information_schema.user_tables -if $rows != 31 then +if $rows != 30 then return -1 endi #sql select * from information_schema.user_table_distributed @@ -197,7 +197,7 @@ if $rows != 1 then endi #sql select * from performance_schema.`streams` sql select * from information_schema.user_tables -if $rows != 31 then +if $rows != 30 then return -1 endi #sql select * from information_schema.user_table_distributed diff --git a/tests/system-test/2-query/query_cols_tags_and_or.py b/tests/system-test/2-query/query_cols_tags_and_or.py index f8a44f735f..c9df6f61bb 100644 --- a/tests/system-test/2-query/query_cols_tags_and_or.py +++ b/tests/system-test/2-query/query_cols_tags_and_or.py @@ -1066,7 +1066,7 @@ class TDTestCase: tdSql.checkEqual(self.queryLastC10(query_sql), 11) if select_elm == "*" else False # in query_sql = f'select {select_elm} from {tb_name} where c5 in (1, 6.6)' - tdSql.error(query_sql) + tdSql.query(query_sql) # not in query_sql = f'select {select_elm} from {tb_name} where c5 not in (2, 3)' tdSql.query(query_sql) @@ -1074,10 +1074,10 @@ class TDTestCase: tdSql.checkEqual(self.queryLastC10(query_sql), 11) if select_elm == "*" else False # and query_sql = f'select {select_elm} from {tb_name} where c5 > 0 and c5 >= 1 and c5 < 7 and c5 <= 6.6 and c5 != 2 and c5 <> 2 and c5 = 6.6 and c5 is not null and c5 between 2 and 6.6 and c5 not between 1 and 2 and c5 in (2,6.6) and c5 not in (1,2)' - tdSql.error(query_sql) + tdSql.query(query_sql) # or query_sql = f'select {select_elm} from {tb_name} where c5 > 6 or c5 >= 6.6 or c5 < 1 or c5 <= 0 or c5 != 1.1 or c5 <> 1.1 or c5 = 5 or c5 is null or c5 between 4 and 5 or c5 not between 1 and 3 or c5 in (4,5) or c5 not in (1.1,3)' - tdSql.error(query_sql) + tdSql.query(query_sql) # and or query_sql = f'select {select_elm} from {tb_name} where c5 > 0 and c5 >= 1 or c5 < 5 and c5 <= 6.6 and c5 != 2 and c5 <> 2 and c5 = 4 or c5 is not null and c5 between 2 and 4 and c5 not between 1 and 2 and c5 in (2,4) and c5 not in (1,2)' tdSql.query(query_sql) @@ -1145,7 +1145,7 @@ class TDTestCase: tdSql.checkEqual(self.queryLastC10(query_sql), 11) if select_elm == "*" else False # in query_sql = f'select {select_elm} from {tb_name} where c6 in (1, 7.7)' - tdSql.error(query_sql) + tdSql.query(query_sql) # not in query_sql = f'select {select_elm} from {tb_name} where c6 not in (2, 3)' tdSql.query(query_sql) @@ -1153,10 +1153,10 @@ class TDTestCase: tdSql.checkEqual(self.queryLastC10(query_sql), 11) if select_elm == "*" else False # and query_sql = f'select {select_elm} from {tb_name} where c6 > 0 and c6 >= 1 and c6 < 8 and c6 <= 7.7 and c6 != 2 and c6 <> 2 and c6 = 7.7 and c6 is not null and c6 between 2 and 7.7 and c6 not between 1 and 2 and c6 in (2,7.7) and c6 not in (1,2)' - tdSql.error(query_sql) + tdSql.query(query_sql) # or query_sql = f'select {select_elm} from {tb_name} where c6 > 7 or c6 >= 7.7 or c6 < 1 or c6 <= 0 or c6 != 1.1 or c6 <> 1.1 or c6 = 5 or c6 is null or c6 between 4 and 5 or c6 not between 1 and 3 or c6 in (4,5) or c6 not in (1.1,3)' - tdSql.error(query_sql) + tdSql.query(query_sql) # and or query_sql = f'select {select_elm} from {tb_name} where c6 > 0 and c6 >= 1 or c6 < 5 and c6 <= 7.7 and c6 != 2 and c6 <> 2 and c6 = 4 or c6 is not null and c6 between 2 and 4 and c6 not between 1 and 2 and c6 in (2,4) and c6 not in (1,2)' tdSql.query(query_sql) @@ -1398,7 +1398,7 @@ class TDTestCase: tdSql.checkEqual(self.queryLastC10(query_sql), 11) if select_elm == "*" else False # in query_sql = f'select {select_elm} from {tb_name} where c9 in ("binar", false)' - tdSql.error(query_sql) + tdSql.query(query_sql) # # not in query_sql = f'select {select_elm} from {tb_name} where c9 not in (true)' tdSql.query(query_sql) @@ -1407,13 +1407,13 @@ class TDTestCase: # # and query_sql = f'select {select_elm} from {tb_name} where c9 = true and c9 != "false" and c9 <> "binary" and c9 is not null and c9 in ("binary", true) and c9 not in ("binary")' - tdSql.error(query_sql) + tdSql.query(query_sql) # # or query_sql = f'select {select_elm} from {tb_name} where c9 = true or c9 != "false" or c9 <> "binary" or c9 = "true" or c9 is not null or c9 in ("binary", true) or c9 not in ("binary")' - tdSql.error(query_sql) + tdSql.query(query_sql) # # and or query_sql = f'select {select_elm} from {tb_name} where c9 = true and c9 != "false" or c9 <> "binary" or c9 = "true" and c9 is not null or c9 in ("binary", true) or c9 not in ("binary")' - tdSql.error(query_sql) + tdSql.query(query_sql) query_sql = f'select c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13 from {tb_name} where c9 > "binary" and c9 >= "binary8" or c9 < "binary9" and c9 <= "binary" and c9 != 2 and c9 <> 2 and c9 = 4 or c9 is not null and c9 between 2 and 4 and c9 not between 1 and 2 and c9 in (2,4) and c9 not in (1,2)' tdSql.query(query_sql) tdSql.checkRows(9) -- GitLab