From 72307ab87720a9d814f6121c212ed0e52f2abb0e Mon Sep 17 00:00:00 2001 From: slzhou Date: Fri, 22 Sep 2023 14:27:01 +0800 Subject: [PATCH] enhance: move bi to enterprise --- source/libs/parser/CMakeLists.txt | 3 + source/libs/parser/inc/parInt.h | 1 - source/libs/parser/inc/parTranslater.h | 55 +++++++++++ source/libs/parser/src/parTranslater.c | 123 +------------------------ tests/parallel_test/cases.task | 2 +- 5 files changed, 63 insertions(+), 121 deletions(-) create mode 100644 source/libs/parser/inc/parTranslater.h diff --git a/source/libs/parser/CMakeLists.txt b/source/libs/parser/CMakeLists.txt index 41553918e1..487b4e3e36 100644 --- a/source/libs/parser/CMakeLists.txt +++ b/source/libs/parser/CMakeLists.txt @@ -1,4 +1,7 @@ aux_source_directory(src PARSER_SRC) +IF (TD_BI_SUPPORT) + LIST(APPEND PARSER_SRC ${TD_ENTERPRISE_DIR}/src/plugins/bi/src/biRewriteQuery.c) +ENDIF () add_library(parser STATIC ${PARSER_SRC}) target_include_directories( parser diff --git a/source/libs/parser/inc/parInt.h b/source/libs/parser/inc/parInt.h index 69253e62e2..9e95478b3f 100644 --- a/source/libs/parser/inc/parInt.h +++ b/source/libs/parser/inc/parInt.h @@ -36,7 +36,6 @@ int32_t extractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pS int32_t calculateConstant(SParseContext* pParseCxt, SQuery* pQuery); int32_t translatePostCreateStream(SParseContext* pParseCxt, SQuery* pQuery, void** pResRow); int32_t translatePostCreateSmaIndex(SParseContext* pParseCxt, SQuery* pQuery, void** pResRow); - #ifdef __cplusplus } #endif diff --git a/source/libs/parser/inc/parTranslater.h b/source/libs/parser/inc/parTranslater.h new file mode 100644 index 0000000000..553123da99 --- /dev/null +++ b/source/libs/parser/inc/parTranslater.h @@ -0,0 +1,55 @@ +/* + * 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 _TD_PARSER_TRANS_H_ +#define _TD_PARSER_TRANS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "parToken.h" +#include "parUtil.h" +#include "parser.h" + +typedef struct STranslateContext { + SParseContext* pParseCxt; + int32_t errCode; + SMsgBuf msgBuf; + SArray* pNsLevel; // element is SArray*, the element of this subarray is STableNode* + int32_t currLevel; + int32_t levelNo; + ESqlClause currClause; + SNode* pCurrStmt; + SCmdMsgInfo* pCmdMsg; + SHashObj* pDbs; + SHashObj* pTables; + SHashObj* pTargetTables; + SExplainOptions* pExplainOpt; + SParseMetaCache* pMetaCache; + bool createStream; + bool stableQuery; + bool showRewrite; + SNode* pPrevRoot; + SNode* pPostRoot; +} STranslateContext; + +int32_t biRewriteSelectStar(STranslateContext* pCxt, SSelectStmt* pSelect); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_PARSER_TRANS_H_*/ \ No newline at end of file diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index be79f0a6d2..64fb59eb3d 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -14,6 +14,7 @@ */ #include "parInt.h" +#include "parTranslater.h" #include "catalog.h" #include "cmdnodes.h" @@ -35,28 +36,6 @@ typedef struct SRewriteTbNameContext { char* pTbName; } SRewriteTbNameContext; -typedef struct STranslateContext { - SParseContext* pParseCxt; - int32_t errCode; - SMsgBuf msgBuf; - SArray* pNsLevel; // element is SArray*, the element of this subarray is STableNode* - int32_t currLevel; - int32_t levelNo; - ESqlClause currClause; - SNode* pCurrStmt; - SCmdMsgInfo* pCmdMsg; - SHashObj* pDbs; - SHashObj* pTables; - SHashObj* pTargetTables; - SExplainOptions* pExplainOpt; - SParseMetaCache* pMetaCache; - bool createStream; - bool stableQuery; - bool showRewrite; - SNode* pPrevRoot; - SNode* pPostRoot; -} STranslateContext; - typedef struct SBuildTopicContext { bool colExists; bool colNotFound; @@ -3022,107 +3001,13 @@ static int32_t createTags(STranslateContext* pCxt, SNodeList** pOutput) { return TSDB_CODE_SUCCESS; } -static void biMakeAliasNameInMD5(char* pExprStr, int32_t len, char* pAlias) { - T_MD5_CTX ctx; - tMD5Init(&ctx); - tMD5Update(&ctx, pExprStr, len); - tMD5Final(&ctx); - char* p = pAlias; - for (uint8_t i = 0; i < tListLen(ctx.digest); ++i) { - sprintf(p, "%02x", ctx.digest[i]); - p += 2; - } -} - -static SNode* biMakeTbnameProjectAstNode(char* funcName, char* tableAlias) { - SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE); - val->literal = strdup(tableAlias); - val->node.resType.type = TSDB_DATA_TYPE_BINARY; - val->node.resType.bytes = strlen(val->literal); - val->isDuration = false; - val->translate = false; - - SNodeList* paramList = nodesMakeList(); - nodesListAppend(paramList, (SNode*)val); - - SFunctionNode* tbNameFunc = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION); - strncpy(tbNameFunc->functionName, "tbname", strlen("tbname")); - nodesListMakeAppend(&tbNameFunc->pParameterList, (SNode*)val); - - snprintf(tbNameFunc->node.userAlias, sizeof(tbNameFunc->node.userAlias), "%s.tbname", tableAlias); - biMakeAliasNameInMD5(tbNameFunc->node.userAlias, strlen(tbNameFunc->node.userAlias), tbNameFunc->node.aliasName); - if (funcName == NULL) { - return (SNode*)tbNameFunc; - } else { - SFunctionNode* multiResFunc = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION); - strncpy(multiResFunc->functionName, funcName, strlen(funcName)); - nodesListMakeAppend(&multiResFunc->pParameterList, (SNode*)tbNameFunc); - - snprintf(multiResFunc->node.userAlias, sizeof(multiResFunc->node.userAlias), "%s(%s.tbname)", funcName, tableAlias); - biMakeAliasNameInMD5(multiResFunc->node.userAlias, strlen(multiResFunc->node.userAlias), multiResFunc->node.aliasName); - return (SNode*)multiResFunc; - } -} - -static int32_t biRewriteSelectFuncParamStar(STranslateContext* pCxt, SSelectStmt* pSelect, SNode* pNode, SListCell* pSelectListCell) { - SNodeList* pTbnameNodeList = nodesMakeList(); - - SFunctionNode* pFunc = (SFunctionNode*)pNode; - if (strcasecmp(pFunc->functionName, "last") == 0 || - strcasecmp(pFunc->functionName, "last_row") == 0 || - strcasecmp(pFunc->functionName, "first") == 0) { - SNodeList* pParams = pFunc->pParameterList; - SNode* pPara = NULL; - FOREACH(pPara, pParams) { - if (isStar(pPara)) { - SArray* pTables = taosArrayGetP(pCxt->pNsLevel, pCxt->currLevel); - size_t n = taosArrayGetSize(pTables); - for (int32_t i = 0; i < n; ++i) { - STableNode* pTable = taosArrayGetP(pTables, i); - SNode* pTbnameNode = biMakeTbnameProjectAstNode(pFunc->functionName, pTable->tableAlias); - nodesListAppend(pTbnameNodeList, pTbnameNode); - } - nodesListInsertListAfterPos(pSelect->pProjectionList, pSelectListCell, pTbnameNodeList); - } else if (isTableStar(pPara)) { - char* pTableAlias = ((SColumnNode*)pPara)->tableAlias; - SNode* pTbnameNode = biMakeTbnameProjectAstNode(pFunc->functionName, pTableAlias); - nodesListAppend(pTbnameNodeList, pTbnameNode); - nodesListInsertListAfterPos(pSelect->pProjectionList, pSelectListCell, pTbnameNodeList); - } - } - } - return TSDB_CODE_SUCCESS; -} - -// after translate from -// before translate select list -static int32_t biRewriteSelectStar(STranslateContext* pCxt, SSelectStmt* pSelect) { - SNode* pNode = NULL; - SNodeList* pTbnameNodeList = nodesMakeList(); - WHERE_EACH(pNode, pSelect->pProjectionList) { - if (isStar(pNode)) { - SArray* pTables = taosArrayGetP(pCxt->pNsLevel, pCxt->currLevel); - size_t n = taosArrayGetSize(pTables); - for (int32_t i = 0; i < n; ++i) { - STableNode* pTable = taosArrayGetP(pTables, i); - SNode* pTbnameNode = biMakeTbnameProjectAstNode(NULL, pTable->tableAlias); - nodesListAppend(pTbnameNodeList, pTbnameNode); - } - nodesListInsertListAfterPos(pSelect->pProjectionList, cell, pTbnameNodeList); - } else if (isTableStar(pNode)) { - char* pTableAlias = ((SColumnNode*)pNode)->tableAlias; - SNode* pTbnameNode = biMakeTbnameProjectAstNode(NULL, pTableAlias); - nodesListAppend(pTbnameNodeList, pTbnameNode); - nodesListInsertListAfterPos(pSelect->pProjectionList, cell, pTbnameNodeList); - } else if (nodeType(pNode) == QUERY_NODE_FUNCTION) { - biRewriteSelectFuncParamStar(pCxt, pSelect, pNode, cell); - } - WHERE_NEXT; - } +#ifndef TD_BI_SUPPORT +int32_t biRewriteSelectStar(STranslateContext* pCxt, SSelectStmt* pSelect) { return TSDB_CODE_SUCCESS; } +#endif static int32_t translateStar(STranslateContext* pCxt, SSelectStmt* pSelect) { SNode* pNode = NULL; WHERE_EACH(pNode, pSelect->pProjectionList) { diff --git a/tests/parallel_test/cases.task b/tests/parallel_test/cases.task index af0c5dcd51..0c7b3cf416 100644 --- a/tests/parallel_test/cases.task +++ b/tests/parallel_test/cases.task @@ -1011,7 +1011,7 @@ ,,y,script,./test.sh -f tsim/query/partitionby.sim ,,y,script,./test.sh -f tsim/query/tableCount.sim ,,y,script,./test.sh -f tsim/query/show_db_table_kind.sim -,,y,script,./test.sh -f tsim/query/bi_star_tbname.sim +#,,y,script,./test.sh -f tsim/query/bi_star_tbname.sim ,,y,script,./test.sh -f tsim/query/tag_scan.sim ,,y,script,./test.sh -f tsim/query/nullColSma.sim ,,y,script,./test.sh -f tsim/query/bug3398.sim