From 80f77251b2893c2f638eec403fe1b3a2aaff2cba Mon Sep 17 00:00:00 2001 From: shenglian zhou Date: Thu, 21 Sep 2023 17:04:12 +0800 Subject: [PATCH 01/13] merge with 3.0 and finish coding star expansion --- include/libs/parser/parser.h | 2 +- source/libs/parser/src/parTranslater.c | 113 ++++++++++++++++++++++--- 2 files changed, 104 insertions(+), 11 deletions(-) diff --git a/include/libs/parser/parser.h b/include/libs/parser/parser.h index 89b7cccd24..4b1af5cc26 100644 --- a/include/libs/parser/parser.h +++ b/include/libs/parser/parser.h @@ -64,7 +64,7 @@ typedef struct SParseContext { SArray* pTableMetaPos; // sql table pos => catalog data pos SArray* pTableVgroupPos; // sql table pos => catalog data pos int64_t allocatorId; - int32_t biMode; + int8_t biMode; } SParseContext; int32_t qParseSql(SParseContext* pCxt, SQuery** pQuery); diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index c702400526..1c71a7d911 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -2857,7 +2857,7 @@ static int32_t translateTable(STranslateContext* pCxt, SNode* pTable) { return code; } -static int32_t createAllColumns(STranslateContext* pCxt, bool igTags, SNodeList** pCols) { +static int32_t createAllColumns(STranslateContext* pCxt, bool igTags, SNodeList** pCols) { *pCols = nodesMakeList(); if (NULL == *pCols) { return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY); @@ -3022,6 +3022,105 @@ 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* biMakeTbnameProjectionNode(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 (pFunc->funcType == FUNCTION_TYPE_LAST || pFunc->funcType == FUNCTION_TYPE_LAST_ROW || pFunc->funcType == FUNCTION_TYPE_FIRST) { + 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 = biMakeTbnameProjectionNode(pFunc->functionName, pTable->tableAlias); + nodesListAppend(pTbnameNodeList, pTbnameNode); + } + nodesListInsertList(pSelect->pProjectionList, pSelectListCell, pTbnameNodeList); + } else if (isTableStar(pPara)) { + char* pTableAlias = ((SColumnNode*)pPara)->tableAlias; + SNode* pTbnameNode = biMakeTbnameProjectionNode(pFunc->functionName, pTableAlias); + nodesListAppend(pTbnameNodeList, pTbnameNode); + nodesListInsertList(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 = biMakeTbnameProjectionNode(NULL, pTable->tableAlias); + nodesListAppend(pTbnameNodeList, pTbnameNode); + } + INSERT_LIST(pSelect->pProjectionList, pTbnameNodeList); + } else if (isTableStar(pNode)) { + char* pTableAlias = ((SColumnNode*)pNode)->tableAlias; + SNode* pTbnameNode = biMakeTbnameProjectionNode(NULL, pTableAlias); + nodesListAppend(pTbnameNodeList, pTbnameNode); + INSERT_LIST(pSelect->pProjectionList, pTbnameNodeList); + } else if (isMultiResFunc(pNode)) { + biRewriteSelectFuncParamStar(pCxt, pSelect, pNode, cell); + } + WHERE_NEXT; + } + + return TSDB_CODE_SUCCESS; +} + static int32_t translateStar(STranslateContext* pCxt, SSelectStmt* pSelect) { SNode* pNode = NULL; WHERE_EACH(pNode, pSelect->pProjectionList) { @@ -3935,6 +4034,9 @@ static int32_t translateSelectFrom(STranslateContext* pCxt, SSelectStmt* pSelect if (TSDB_CODE_SUCCESS == code) { code = translateHaving(pCxt, pSelect); } + if (TSDB_CODE_SUCCESS == code && pCxt->pParseCxt->biMode != 0) { + code = biRewriteSelectStar(pCxt, pSelect); + } if (TSDB_CODE_SUCCESS == code) { code = translateSelectList(pCxt, pSelect); } @@ -9537,11 +9639,6 @@ static int32_t setQuery(STranslateContext* pCxt, SQuery* pQuery) { return TSDB_CODE_SUCCESS; } -static int32_t rewriteQueryForBI(STranslateContext* pParseCxt, SQuery* pQuery) { - - return TSDB_CODE_SUCCESS; -} - int32_t translate(SParseContext* pParseCxt, SQuery* pQuery, SParseMetaCache* pMetaCache) { STranslateContext cxt = {0}; @@ -9549,13 +9646,9 @@ int32_t translate(SParseContext* pParseCxt, SQuery* pQuery, SParseMetaCache* pMe if (TSDB_CODE_SUCCESS == code) { code = rewriteQuery(&cxt, pQuery); } - if (TSDB_CODE_SUCCESS == code && pParseCxt->biMode != 0) { - code = rewriteQueryForBI(&cxt, pQuery); - } if (TSDB_CODE_SUCCESS == code) { code = translateQuery(&cxt, pQuery->pRoot); } - if (TSDB_CODE_SUCCESS == code && (cxt.pPrevRoot || cxt.pPostRoot)) { pQuery->pPrevRoot = cxt.pPrevRoot; pQuery->pPostRoot = cxt.pPostRoot; From 9bb628650c41a3d91ae494d59518085073a1c328 Mon Sep 17 00:00:00 2001 From: slzhou Date: Fri, 22 Sep 2023 10:13:17 +0800 Subject: [PATCH 02/13] fix: pass basic test --- include/libs/nodes/nodes.h | 1 + source/libs/nodes/src/nodesUtilFuncs.c | 20 ++++++++++++++ source/libs/parser/src/parTranslater.c | 38 ++++++++++++++------------ 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/include/libs/nodes/nodes.h b/include/libs/nodes/nodes.h index 6e36ea7514..9725aa48c0 100644 --- a/include/libs/nodes/nodes.h +++ b/include/libs/nodes/nodes.h @@ -124,6 +124,7 @@ int32_t nodesListStrictAppendList(SNodeList* pTarget, SNodeList* pSrc); int32_t nodesListPushFront(SNodeList* pList, SNode* pNode); SListCell* nodesListErase(SNodeList* pList, SListCell* pCell); void nodesListInsertList(SNodeList* pTarget, SListCell* pPos, SNodeList* pSrc); +void nodesListInsertListAfterPos(SNodeList* pTarget, SListCell* pPos, SNodeList* pSrc); SNode* nodesListGetNode(SNodeList* pList, int32_t index); SListCell* nodesListGetCell(SNodeList* pList, int32_t index); void nodesDestroyList(SNodeList* pList); diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index 580160c50b..a9de910b98 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -1595,6 +1595,26 @@ void nodesListInsertList(SNodeList* pTarget, SListCell* pPos, SNodeList* pSrc) { nodesFree(pSrc); } +void nodesListInsertListAfterPos(SNodeList* pTarget, SListCell* pPos, SNodeList* pSrc) { + if (NULL == pTarget || NULL == pPos || NULL == pSrc || NULL == pSrc->pHead) { + return; + } + + if (NULL == pPos->pNext) { + pTarget->pTail = pSrc->pHead; + } else { + pPos->pNext->pPrev = pSrc->pHead; + } + + pSrc->pHead->pPrev = pPos; + pSrc->pTail->pNext = pPos->pNext; + + pPos->pNext = pSrc->pHead; + + pTarget->length += pSrc->length; + nodesFree(pSrc); +} + SNode* nodesListGetNode(SNodeList* pList, int32_t index) { SNode* node; FOREACH(node, pList) { diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 1c71a7d911..be79f0a6d2 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3034,7 +3034,7 @@ static void biMakeAliasNameInMD5(char* pExprStr, int32_t len, char* pAlias) { } } -static SNode* biMakeTbnameProjectionNode(char* funcName, char* tableAlias) { +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; @@ -3045,18 +3045,18 @@ static SNode* biMakeTbnameProjectionNode(char* funcName, char* tableAlias) { 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); + 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); + 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; + return (SNode*)tbNameFunc; } else { SFunctionNode* multiResFunc = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION); strncpy(multiResFunc->functionName, funcName, strlen(funcName)); - nodesListMakeAppend(&multiResFunc->pParameterList, (SNode*)tbNamefunc); + 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); @@ -3068,7 +3068,9 @@ static int32_t biRewriteSelectFuncParamStar(STranslateContext* pCxt, SSelectStmt SNodeList* pTbnameNodeList = nodesMakeList(); SFunctionNode* pFunc = (SFunctionNode*)pNode; - if (pFunc->funcType == FUNCTION_TYPE_LAST || pFunc->funcType == FUNCTION_TYPE_LAST_ROW || pFunc->funcType == FUNCTION_TYPE_FIRST) { + 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) { @@ -3077,15 +3079,15 @@ static int32_t biRewriteSelectFuncParamStar(STranslateContext* pCxt, SSelectStmt size_t n = taosArrayGetSize(pTables); for (int32_t i = 0; i < n; ++i) { STableNode* pTable = taosArrayGetP(pTables, i); - SNode* pTbnameNode = biMakeTbnameProjectionNode(pFunc->functionName, pTable->tableAlias); + SNode* pTbnameNode = biMakeTbnameProjectAstNode(pFunc->functionName, pTable->tableAlias); nodesListAppend(pTbnameNodeList, pTbnameNode); } - nodesListInsertList(pSelect->pProjectionList, pSelectListCell, pTbnameNodeList); + nodesListInsertListAfterPos(pSelect->pProjectionList, pSelectListCell, pTbnameNodeList); } else if (isTableStar(pPara)) { char* pTableAlias = ((SColumnNode*)pPara)->tableAlias; - SNode* pTbnameNode = biMakeTbnameProjectionNode(pFunc->functionName, pTableAlias); + SNode* pTbnameNode = biMakeTbnameProjectAstNode(pFunc->functionName, pTableAlias); nodesListAppend(pTbnameNodeList, pTbnameNode); - nodesListInsertList(pSelect->pProjectionList, pSelectListCell, pTbnameNodeList); + nodesListInsertListAfterPos(pSelect->pProjectionList, pSelectListCell, pTbnameNodeList); } } } @@ -3103,16 +3105,16 @@ static int32_t biRewriteSelectStar(STranslateContext* pCxt, SSelectStmt* pSelect size_t n = taosArrayGetSize(pTables); for (int32_t i = 0; i < n; ++i) { STableNode* pTable = taosArrayGetP(pTables, i); - SNode* pTbnameNode = biMakeTbnameProjectionNode(NULL, pTable->tableAlias); + SNode* pTbnameNode = biMakeTbnameProjectAstNode(NULL, pTable->tableAlias); nodesListAppend(pTbnameNodeList, pTbnameNode); } - INSERT_LIST(pSelect->pProjectionList, pTbnameNodeList); + nodesListInsertListAfterPos(pSelect->pProjectionList, cell, pTbnameNodeList); } else if (isTableStar(pNode)) { char* pTableAlias = ((SColumnNode*)pNode)->tableAlias; - SNode* pTbnameNode = biMakeTbnameProjectionNode(NULL, pTableAlias); + SNode* pTbnameNode = biMakeTbnameProjectAstNode(NULL, pTableAlias); nodesListAppend(pTbnameNodeList, pTbnameNode); - INSERT_LIST(pSelect->pProjectionList, pTbnameNodeList); - } else if (isMultiResFunc(pNode)) { + nodesListInsertListAfterPos(pSelect->pProjectionList, cell, pTbnameNodeList); + } else if (nodeType(pNode) == QUERY_NODE_FUNCTION) { biRewriteSelectFuncParamStar(pCxt, pSelect, pNode, cell); } WHERE_NEXT; From 1b71cd4b8d7347f0ecfa1fe4c16d44ed3ab89149 Mon Sep 17 00:00:00 2001 From: shenglian zhou Date: Fri, 22 Sep 2023 13:29:17 +0800 Subject: [PATCH 03/13] fix: add $cols to script support --- utils/tsim/inc/simInt.h | 1 + utils/tsim/src/simExe.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/utils/tsim/inc/simInt.h b/utils/tsim/inc/simInt.h index 8ff3f3b183..118881bf38 100644 --- a/utils/tsim/inc/simInt.h +++ b/utils/tsim/inc/simInt.h @@ -161,6 +161,7 @@ typedef struct _script_t { bool killed; void *taos; char rows[12]; // number of rows data retrieved + char cols[12]; // number of columns data retrieved char data[MAX_QUERY_ROW_NUM][MAX_QUERY_COL_NUM][MAX_QUERY_VALUE_LEN]; // query results char system_exit_code[12]; char system_ret_content[MAX_SYSTEM_RESULT_LEN]; diff --git a/utils/tsim/src/simExe.c b/utils/tsim/src/simExe.c index 9a0a156717..1e0a49b9ba 100644 --- a/utils/tsim/src/simExe.c +++ b/utils/tsim/src/simExe.c @@ -93,6 +93,8 @@ char *simGetVariable(SScript *script, char *varName, int32_t varLen) { if (strncmp(varName, "rows", varLen) == 0) return script->rows; + if (strncmp(varName, "cols", varLen) == 0) return script->cols; + if (strncmp(varName, "system_exit", varLen) == 0) return script->system_exit_code; if (strncmp(varName, "system_content", varLen) == 0) return script->system_ret_content; @@ -808,6 +810,7 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) { taos_free_result(pSql); sprintf(script->rows, "%d", numOfRows); + sprintf(script->cols, "%d", num_fields); script->linePos++; return true; @@ -822,6 +825,7 @@ bool simExecuteSqlImpCmd(SScript *script, char *rest, bool isSlow) { simDebug("script:%s, exec:%s", script->fileName, rest); strcpy(script->rows, "-1"); + strcpy(script->cols, "-1"); for (int32_t row = 0; row < MAX_QUERY_ROW_NUM; ++row) { for (int32_t col = 0; col < MAX_QUERY_COL_NUM; ++col) { strcpy(script->data[row][col], "null"); @@ -918,6 +922,7 @@ bool simExecuteSqlErrorCmd(SScript *script, char *rest) { simDebug("script:%s, exec:%s", script->fileName, rest); strcpy(script->rows, "-1"); + strcpy(script->cols, "-1"); for (int32_t row = 0; row < MAX_QUERY_ROW_NUM; ++row) { for (int32_t col = 0; col < MAX_QUERY_COL_NUM; ++col) { strcpy(script->data[row][col], "null"); From d858b26939c528a46454d338e791deeec1656708 Mon Sep 17 00:00:00 2001 From: slzhou Date: Fri, 22 Sep 2023 13:34:14 +0800 Subject: [PATCH 04/13] enhance: add bi mode to script --- tests/parallel_test/cases.task | 1 + tests/script/tsim/query/bi_star_table.sim | 29 +++++++++++++++++++++++ utils/tsim/inc/simInt.h | 2 ++ utils/tsim/src/simExe.c | 20 ++++++++++++++++ utils/tsim/src/simParse.c | 27 +++++++++++++++++++++ 5 files changed, 79 insertions(+) create mode 100644 tests/script/tsim/query/bi_star_table.sim diff --git a/tests/parallel_test/cases.task b/tests/parallel_test/cases.task index 1d90cb12ad..af0c5dcd51 100644 --- a/tests/parallel_test/cases.task +++ b/tests/parallel_test/cases.task @@ -1011,6 +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/tag_scan.sim ,,y,script,./test.sh -f tsim/query/nullColSma.sim ,,y,script,./test.sh -f tsim/query/bug3398.sim diff --git a/tests/script/tsim/query/bi_star_table.sim b/tests/script/tsim/query/bi_star_table.sim new file mode 100644 index 0000000000..5112ae3839 --- /dev/null +++ b/tests/script/tsim/query/bi_star_table.sim @@ -0,0 +1,29 @@ +system sh/stop_dnodes.sh +system sh/deploy.sh -n dnode1 -i 1 +system sh/exec.sh -n dnode1 -s start +sql connect + +sql drop database if exists db1; +sql create database db1 vgroups 3; +sql create database db1; +sql use db1; +sql create stable sta (ts timestamp, f1 int, f2 binary(200)) tags(t1 int, t2 int, t3 int); +sql create stable stb (ts timestamp, f1 int, f2 binary(200)) tags(t1 int, t2 int, t3 int); +sql create table tba1 using sta tags(1, 1, 1); +sql create table tba2 using sta tags(2, 2, 2); +sql insert into tba1 values(now, 1, "1"); +sql insert into tba2 values(now + 1s, 2, "2"); +sql create table tbn1 (ts timestamp, f1 int); +sql create database db2 vgroups 3; +sql create database db2; +sql use db2; +sql create stable sta (ts timestamp, f1 int, f2 binary(200)) tags(t1 int, t2 int, t3 int); +sql create stable stb (ts timestamp, f1 int, f2 binary(200)) tags(t1 int, t2 int, t3 int); +sql create table tba1 using sta tags(1, 1, 1); +sql create table tba2 using sta tags(2, 2, 2); + +set_bi_mode 1 +sql select * from db1.sta; +print $rows +print $data00 $data01 $data02 $data03 $data04 $data05 $data06 +system sh/exec.sh -n dnode1 -s stop -x SIGINT diff --git a/utils/tsim/inc/simInt.h b/utils/tsim/inc/simInt.h index 8ff3f3b183..fb92c18a5a 100644 --- a/utils/tsim/inc/simInt.h +++ b/utils/tsim/inc/simInt.h @@ -123,6 +123,7 @@ enum { SIM_CMD_RETURN, SIM_CMD_LINE_INSERT, SIM_CMD_LINE_INSERT_ERROR, + SIM_CMD_SET_BI_MODE, SIM_CMD_END }; @@ -210,6 +211,7 @@ bool simExecuteSqlSlowCmd(SScript *script, char *option); bool simExecuteRestfulCmd(SScript *script, char *rest); bool simExecuteLineInsertCmd(SScript *script, char *option); bool simExecuteLineInsertErrorCmd(SScript *script, char *option); +bool simExecuteSetBIModeCmd(SScript *script, char *option); void simVisuallizeOption(SScript *script, char *src, char *dst); #endif /*_TD_SIM_INT_H_*/ diff --git a/utils/tsim/src/simExe.c b/utils/tsim/src/simExe.c index 9a0a156717..4fd218bd14 100644 --- a/utils/tsim/src/simExe.c +++ b/utils/tsim/src/simExe.c @@ -502,6 +502,26 @@ bool simExecuteSystemContentCmd(SScript *script, char *option) { return true; } +bool simExecuteSetBIModeCmd(SScript *script, char *option) { + char buf[1024]; + + simVisuallizeOption(script, option, buf); + option = buf; + + int32_t mode = atoi(option); + + simInfo("script:%s, set bi mode %d", script->fileName, mode); + + if (mode != 0) { + taos_set_conn_mode(script->taos, TAOS_CONN_MODE_BI, 1); + } else { + taos_set_conn_mode(script->taos, TAOS_CONN_MODE_BI, 0); + } + + script->linePos++; + return true; +} + bool simExecutePrintCmd(SScript *script, char *rest) { char buf[65536]; diff --git a/utils/tsim/src/simParse.c b/utils/tsim/src/simParse.c index cdd918ac00..fbaf08bf3b 100644 --- a/utils/tsim/src/simParse.c +++ b/utils/tsim/src/simParse.c @@ -745,6 +745,25 @@ bool simParseSystemContentCmd(char *rest, SCommand *pCmd, int32_t lineNum) { return true; } +bool simParseSetBIModeCmd(char *rest, SCommand *pCmd, int32_t lineNum) { + char *token; + int32_t tokenLen; + + cmdLine[numOfLines].cmdno = SIM_CMD_SET_BI_MODE; + cmdLine[numOfLines].lineNum = lineNum; + + paGetToken(rest, &token, &tokenLen); + if (tokenLen > 0) { + cmdLine[numOfLines].optionOffset = optionOffset; + memcpy(optionBuffer + optionOffset, token, tokenLen); + optionOffset += tokenLen + 1; + *(optionBuffer + optionOffset - 1) = 0; + } + + numOfLines++; + return true; +} + bool simParseSleepCmd(char *rest, SCommand *pCmd, int32_t lineNum) { char *token; int32_t tokenLen; @@ -1074,6 +1093,14 @@ void simInitsimCmdList() { simCmdList[cmdno].executeCmd = simExecuteReturnCmd; simAddCmdIntoHash(&(simCmdList[cmdno])); + cmdno = SIM_CMD_SET_BI_MODE; + simCmdList[cmdno].cmdno = cmdno; + strcpy(simCmdList[cmdno].name, "set_bi_mode"); + simCmdList[cmdno].nlen = (int16_t)strlen(simCmdList[cmdno].name); + simCmdList[cmdno].parseCmd = simParseSetBIModeCmd; + simCmdList[cmdno].executeCmd = simExecuteSetBIModeCmd; + simAddCmdIntoHash(&(simCmdList[cmdno])); + #if 0 cmdno = SIM_CMD_LINE_INSERT; simCmdList[cmdno].cmdno = cmdno; From 6134c9039860afc5e1041cc848c3741f0f70baaf Mon Sep 17 00:00:00 2001 From: slzhou Date: Fri, 22 Sep 2023 13:47:50 +0800 Subject: [PATCH 05/13] fix: add more test case --- tests/script/tsim/query/bi_star_table.sim | 83 ++++++++++++++++++++++- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/tests/script/tsim/query/bi_star_table.sim b/tests/script/tsim/query/bi_star_table.sim index 5112ae3839..618408ff7d 100644 --- a/tests/script/tsim/query/bi_star_table.sim +++ b/tests/script/tsim/query/bi_star_table.sim @@ -23,7 +23,84 @@ sql create table tba1 using sta tags(1, 1, 1); sql create table tba2 using sta tags(2, 2, 2); set_bi_mode 1 -sql select * from db1.sta; -print $rows -print $data00 $data01 $data02 $data03 $data04 $data05 $data06 +sql select * from db1.sta order by ts; +if $cols != 7 then + return -1 +endi + +if $data06 != tba1 then + return -1 +endi + +sql select last(*) from db1.sta; +if $cols != 4 then + return -1 +endi + +if $data03 != tba2 then + return -1 +endi + +sql select last_row(*) from db1.sta; +if $cols != 4 then + return -1 +endi + +if $data03 != tba2 then + return -1 +endi + +sql select first(*) from db1.sta; +if $cols != 4 then + return -1 +endi + +if $data03 != tba1 then + return -1 +endi + +print "=====table star =====================" + +sql select b.* from db1.sta b order by ts; +if $cols != 7 then + return -1 +endi + +if $data06 != tba1 then + return -1 +endi + +sql select last(b.*) from db1.sta b; +if $cols != 4 then + return -1 +endi + +if $data03 != tba2 then + return -1 +endi + +sql select last_row(b.*) from db1.sta b; +if $cols != 4 then + return -1 +endi + +if $data03 != tba2 then + return -1 +endi + +sql select first(b.*) from db1.sta b; +if $cols != 4 then + return -1 +endi + +if $data03 != tba1 then + return -1 +endi + +set_bi_mode 0 +sql select * from db1.sta order by ts; +if $cols != 6 then + return -1 +endi + system sh/exec.sh -n dnode1 -s stop -x SIGINT From 72307ab87720a9d814f6121c212ed0e52f2abb0e Mon Sep 17 00:00:00 2001 From: slzhou Date: Fri, 22 Sep 2023 14:27:01 +0800 Subject: [PATCH 06/13] 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 From 18f8e0dc8d2c824950c4c1cb9345aa9974c4e9b5 Mon Sep 17 00:00:00 2001 From: slzhou Date: Fri, 22 Sep 2023 14:31:47 +0800 Subject: [PATCH 07/13] fix: enterprise/community bi support --- source/libs/parser/inc/parTranslater.h | 2 ++ source/libs/parser/src/parTranslater.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/source/libs/parser/inc/parTranslater.h b/source/libs/parser/inc/parTranslater.h index 553123da99..ca21429167 100644 --- a/source/libs/parser/inc/parTranslater.h +++ b/source/libs/parser/inc/parTranslater.h @@ -47,6 +47,8 @@ typedef struct STranslateContext { } STranslateContext; int32_t biRewriteSelectStar(STranslateContext* pCxt, SSelectStmt* pSelect); +bool isStar(SNode* pNode); +bool isTableStar(SNode* pNode); #ifdef __cplusplus } diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 64fb59eb3d..089b354f0d 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -1789,12 +1789,12 @@ static int32_t translateBlockDistFunc(STranslateContext* pCtx, SFunctionNode* pF return TSDB_CODE_SUCCESS; } -static bool isStar(SNode* pNode) { +bool isStar(SNode* pNode) { return (QUERY_NODE_COLUMN == nodeType(pNode)) && ('\0' == ((SColumnNode*)pNode)->tableAlias[0]) && (0 == strcmp(((SColumnNode*)pNode)->colName, "*")); } -static bool isTableStar(SNode* pNode) { +bool isTableStar(SNode* pNode) { return (QUERY_NODE_COLUMN == nodeType(pNode)) && ('\0' != ((SColumnNode*)pNode)->tableAlias[0]) && (0 == strcmp(((SColumnNode*)pNode)->colName, "*")); } From 57d5d793f861d1ca4d79ca5152499dac1a7b5a0a Mon Sep 17 00:00:00 2001 From: slzhou Date: Fri, 22 Sep 2023 15:06:38 +0800 Subject: [PATCH 08/13] fix: comm/enterprise split --- source/libs/parser/src/parTranslater.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 089b354f0d..d5845834db 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3002,12 +3002,12 @@ static int32_t createTags(STranslateContext* pCxt, SNodeList** pOutput) { } -#ifndef TD_BI_SUPPORT +#ifndef TD_ENTERPRISE 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) { From c6ab8e2ca73198afd358340a81c89a59cdd41134 Mon Sep 17 00:00:00 2001 From: slzhou Date: Fri, 22 Sep 2023 15:09:21 +0800 Subject: [PATCH 09/13] feat: add test case back --- tests/parallel_test/cases.task | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/parallel_test/cases.task b/tests/parallel_test/cases.task index 0c7b3cf416..af0c5dcd51 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 From fb62588dd35eae0832744b24514edad1e2379780 Mon Sep 17 00:00:00 2001 From: slzhou Date: Fri, 22 Sep 2023 15:26:39 +0800 Subject: [PATCH 10/13] fix: nodesIsStar and nodesIsTableStar --- include/libs/nodes/querynodes.h | 3 +++ source/libs/nodes/src/nodesUtilFuncs.c | 10 ++++++++++ source/libs/parser/inc/parTranslater.h | 2 -- source/libs/parser/src/parTranslater.c | 19 +++++-------------- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/include/libs/nodes/querynodes.h b/include/libs/nodes/querynodes.h index 972421b1e7..98f0a795f7 100644 --- a/include/libs/nodes/querynodes.h +++ b/include/libs/nodes/querynodes.h @@ -536,6 +536,9 @@ int32_t nodesMergeConds(SNode** pDst, SNodeList** pSrc); const char* operatorTypeStr(EOperatorType type); const char* logicConditionTypeStr(ELogicConditionType type); +bool nodesIsStar(SNode* pNode); +bool nodesIsTableStar(SNode* pNode); + #ifdef __cplusplus } #endif diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index a9de910b98..bf5b6c8080 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -2337,4 +2337,14 @@ SValueNode* nodesMakeValueNodeFromBool(bool b) { pValNode->isNull = false; } return pValNode; +} + +bool nodesIsStar(SNode* pNode) { + return (QUERY_NODE_COLUMN == nodeType(pNode)) && ('\0' == ((SColumnNode*)pNode)->tableAlias[0]) && + (0 == strcmp(((SColumnNode*)pNode)->colName, "*")); +} + +bool nodesIsTableStar(SNode* pNode) { + return (QUERY_NODE_COLUMN == nodeType(pNode)) && ('\0' != ((SColumnNode*)pNode)->tableAlias[0]) && + (0 == strcmp(((SColumnNode*)pNode)->colName, "*")); } \ No newline at end of file diff --git a/source/libs/parser/inc/parTranslater.h b/source/libs/parser/inc/parTranslater.h index ca21429167..553123da99 100644 --- a/source/libs/parser/inc/parTranslater.h +++ b/source/libs/parser/inc/parTranslater.h @@ -47,8 +47,6 @@ typedef struct STranslateContext { } STranslateContext; int32_t biRewriteSelectStar(STranslateContext* pCxt, SSelectStmt* pSelect); -bool isStar(SNode* pNode); -bool isTableStar(SNode* pNode); #ifdef __cplusplus } diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index d5845834db..8f066307dc 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -1789,17 +1789,8 @@ static int32_t translateBlockDistFunc(STranslateContext* pCtx, SFunctionNode* pF return TSDB_CODE_SUCCESS; } -bool isStar(SNode* pNode) { - return (QUERY_NODE_COLUMN == nodeType(pNode)) && ('\0' == ((SColumnNode*)pNode)->tableAlias[0]) && - (0 == strcmp(((SColumnNode*)pNode)->colName, "*")); -} -bool isTableStar(SNode* pNode) { - return (QUERY_NODE_COLUMN == nodeType(pNode)) && ('\0' != ((SColumnNode*)pNode)->tableAlias[0]) && - (0 == strcmp(((SColumnNode*)pNode)->colName, "*")); -} - -static bool isStarParam(SNode* pNode) { return isStar(pNode) || isTableStar(pNode); } +static bool isStarParam(SNode* pNode) { return nodesIsStar(pNode) || nodesIsTableStar(pNode); } static int32_t translateMultiResFunc(STranslateContext* pCxt, SFunctionNode* pFunc) { if (!fmIsMultiResFunc(pFunc->funcId)) { @@ -2917,9 +2908,9 @@ static int32_t createMultiResFuncsParas(STranslateContext* pCxt, SNodeList* pSrc SNodeList* pExprs = NULL; SNode* pPara = NULL; FOREACH(pPara, pSrcParas) { - if (isStar(pPara)) { + if (nodesIsStar(pPara)) { code = createAllColumns(pCxt, true, &pExprs); - } else if (isTableStar(pPara)) { + } else if (nodesIsTableStar(pPara)) { code = createTableAllCols(pCxt, (SColumnNode*)pPara, true, &pExprs); } else { code = nodesListMakeStrictAppend(&pExprs, nodesCloneNode(pPara)); @@ -3012,7 +3003,7 @@ static int32_t translateStar(STranslateContext* pCxt, SSelectStmt* pSelect) { SNode* pNode = NULL; WHERE_EACH(pNode, pSelect->pProjectionList) { int32_t code = TSDB_CODE_SUCCESS; - if (isStar(pNode)) { + if (nodesIsStar(pNode)) { SNodeList* pCols = NULL; code = createAllColumns(pCxt, false, &pCols); if (TSDB_CODE_SUCCESS == code) { @@ -3032,7 +3023,7 @@ static int32_t translateStar(STranslateContext* pCxt, SSelectStmt* pSelect) { ERASE_NODE(pSelect->pProjectionList); continue; } - } else if (isTableStar(pNode)) { + } else if (nodesIsTableStar(pNode)) { SNodeList* pCols = NULL; code = createTableAllCols(pCxt, (SColumnNode*)pNode, false, &pCols); if (TSDB_CODE_SUCCESS == code) { From df19645a90cfdeede92eb53b80e37654679a1dfb Mon Sep 17 00:00:00 2001 From: shenglian zhou Date: Fri, 22 Sep 2023 22:02:30 +0800 Subject: [PATCH 11/13] fix: check table is super table when rewrite star for bi tool --- source/libs/parser/inc/parTranslater.h | 2 +- source/libs/parser/src/parTranslater.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/libs/parser/inc/parTranslater.h b/source/libs/parser/inc/parTranslater.h index 553123da99..7f721a2c25 100644 --- a/source/libs/parser/inc/parTranslater.h +++ b/source/libs/parser/inc/parTranslater.h @@ -47,7 +47,7 @@ typedef struct STranslateContext { } STranslateContext; int32_t biRewriteSelectStar(STranslateContext* pCxt, SSelectStmt* pSelect); - +int32_t findTable(STranslateContext* pCxt, const char* pTableAlias, STableNode** pOutput); #ifdef __cplusplus } #endif diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 8f066307dc..9dce3d2fe4 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -1398,7 +1398,7 @@ static EDealRes haveVectorFunction(SNode* pNode, void* pContext) { return DEAL_RES_CONTINUE; } -static int32_t findTable(STranslateContext* pCxt, const char* pTableAlias, STableNode** pOutput) { +int32_t findTable(STranslateContext* pCxt, const char* pTableAlias, STableNode** pOutput) { SArray* pTables = taosArrayGetP(pCxt->pNsLevel, pCxt->currLevel); size_t nums = taosArrayGetSize(pTables); for (size_t i = 0; i < nums; ++i) { From d5d702933833fdc59d7b9a5b264c393e0519e071 Mon Sep 17 00:00:00 2001 From: slzhou Date: Sun, 24 Sep 2023 21:45:32 +0800 Subject: [PATCH 12/13] fix: ci error --- tests/parallel_test/cases.task | 2 +- tests/script/tsim/query/bi_star_table.sim | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/parallel_test/cases.task b/tests/parallel_test/cases.task index af0c5dcd51..621c0affb5 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_table.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 diff --git a/tests/script/tsim/query/bi_star_table.sim b/tests/script/tsim/query/bi_star_table.sim index 618408ff7d..6bd6938678 100644 --- a/tests/script/tsim/query/bi_star_table.sim +++ b/tests/script/tsim/query/bi_star_table.sim @@ -97,6 +97,11 @@ if $data03 != tba1 then return -1 endi +sql select * from (select f1 from db1.sta); +if $cols != 1 then + return -1 +endi + set_bi_mode 0 sql select * from db1.sta order by ts; if $cols != 6 then From 61f006809f8a9eef2b93b7b6263d8ea72a38b5d2 Mon Sep 17 00:00:00 2001 From: slzhou Date: Sun, 24 Sep 2023 22:31:40 +0800 Subject: [PATCH 13/13] fix: limit error --- tests/script/tsim/parser/limit.sim | 5 ++++- tests/script/tsim/parser/limit_stb.sim | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/script/tsim/parser/limit.sim b/tests/script/tsim/parser/limit.sim index e79640f3af..900ca220ad 100644 --- a/tests/script/tsim/parser/limit.sim +++ b/tests/script/tsim/parser/limit.sim @@ -40,16 +40,19 @@ while $i < $halfNum $c = $x / 10 $c = $c * 10 $c = $x - $c + $c2 = $c $binary = 'binary . $c $binary = $binary . ' $nchar = 'nchar . $c $nchar = $nchar . ' $ts = $ts + $i + print insert into $tb values ( $ts , $c , $c , $c , $c , $c , $c , true, $binary , $nchar ) sql insert into $tb values ( $ts , $c , $c , $c , $c , $c , $c , true, $binary , $nchar ) $ts = $ts + $halfNum - sql insert into $tb1 values ( $ts , $c , NULL , $c , NULL , $c , $c , true, $binary , $nchar ) + print insert into $tb1 values ( $ts , $c2 , NULL , $c2 , NULL , $c2 , $c2 , true, $binary , $nchar ) + sql insert into $tb1 values ( $ts , $c2 , NULL , $c2 , NULL , $c2 , $c2 , true, $binary , $nchar ) $x = $x + 1 endw diff --git a/tests/script/tsim/parser/limit_stb.sim b/tests/script/tsim/parser/limit_stb.sim index 46bd6260c3..7d6aff3b51 100644 --- a/tests/script/tsim/parser/limit_stb.sim +++ b/tests/script/tsim/parser/limit_stb.sim @@ -32,11 +32,13 @@ sql select * from $stb limit 5 if $rows != 5 then return -1 endi -sql select * from $stb limit 5 offset 1 +print select * from $stb order by ts limit 5 offset 1 +sql select * from $stb order by ts limit 5 offset 1 if $rows != 5 then return -1 endi -if $data01 != 1 then +print $data01 $data41 +if $data01 != 0 then return -1 endi #if $data41 != 5 then @@ -48,10 +50,12 @@ if $rows != 5 then return -1 endi +print select * from $stb order by ts desc limit 5 offset 1 sql select * from $stb order by ts desc limit 5 offset 1 if $rows != 5 then return -1 endi +print $data01 $data11 $data41 if $data01 != 9 then return -1 endi