From 4208557d6d3751367645767be9303e014eb50568 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Thu, 28 Mar 2024 16:44:07 +0800 Subject: [PATCH] fix: count(*) rewrite issue --- source/libs/parser/src/parTranslater.c | 40 ++++++++++++++++++++----- source/libs/planner/src/planOptimizer.c | 2 ++ tests/script/tsim/join/full_join.sim | 22 ++++++++++++++ tests/script/tsim/join/left_join.sim | 22 ++++++++++++++ tests/script/tsim/join/right_join.sim | 21 +++++++++++++ 5 files changed, 100 insertions(+), 7 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 9dd9b44685..2d1f528461 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -1906,6 +1906,30 @@ static int32_t rewriteCountStarAsCount1(STranslateContext* pCxt, SFunctionNode* return code; } +STableNode* getJoinProbeTable(STranslateContext* pCxt) { + if (QUERY_NODE_SELECT_STMT != nodeType(pCxt->pCurrStmt)) { + return NULL; + } + SSelectStmt* pSelect = (SSelectStmt*)pCxt->pCurrStmt; + if (NULL == pSelect->pFromTable || QUERY_NODE_JOIN_TABLE != nodeType(pSelect->pFromTable)) { + return NULL; + } + + SJoinTableNode* pJoin = (SJoinTableNode*)pSelect->pFromTable; + switch (pJoin->joinType) { + case JOIN_TYPE_INNER: + case JOIN_TYPE_LEFT: + return (STableNode*)pJoin->pLeft; + case JOIN_TYPE_RIGHT: + return (STableNode*)pJoin->pRight; + default: + break; + } + + return NULL; +} + + // count(*) is rewritten as count(ts) for scannning optimization static int32_t rewriteCountStar(STranslateContext* pCxt, SFunctionNode* pCount) { SColumnNode* pCol = (SColumnNode*)nodesListGetNode(pCount->pParameterList, 0); @@ -1914,17 +1938,19 @@ static int32_t rewriteCountStar(STranslateContext* pCxt, SFunctionNode* pCount) size_t nums = taosArrayGetSize(pTables); int32_t code = 0; if ('\0' == pCol->tableAlias[0] && nums > 1) { - code = rewriteCountStarAsCount1(pCxt, pCount); + pTable = getJoinProbeTable(pCxt); } else { code = findTable(pCxt, ('\0' == pCol->tableAlias[0] ? NULL : pCol->tableAlias), &pTable); - if (TSDB_CODE_SUCCESS == code) { - if (QUERY_NODE_REAL_TABLE == nodeType(pTable)) { - setColumnInfoBySchema((SRealTableNode*)pTable, ((SRealTableNode*)pTable)->pMeta->schema, -1, pCol); - } else { - code = rewriteCountStarAsCount1(pCxt, pCount); - } + } + + if (TSDB_CODE_SUCCESS == code) { + if (NULL != pTable && QUERY_NODE_REAL_TABLE == nodeType(pTable)) { + setColumnInfoBySchema((SRealTableNode*)pTable, ((SRealTableNode*)pTable)->pMeta->schema, -1, pCol); + } else { + code = rewriteCountStarAsCount1(pCxt, pCount); } } + return code; } diff --git a/source/libs/planner/src/planOptimizer.c b/source/libs/planner/src/planOptimizer.c index fd1c40db77..54010cfc6b 100644 --- a/source/libs/planner/src/planOptimizer.c +++ b/source/libs/planner/src/planOptimizer.c @@ -4469,7 +4469,9 @@ static int32_t stbJoinOptCreateTableScanNodes(SLogicNode* pJoin, SNodeList** ppL //} nodesDestroyNode(pScan->pTagCond); + pScan->pTagCond = NULL; nodesDestroyNode(pScan->pTagIndexCond); + pScan->pTagIndexCond = NULL; pScan->node.dynamicOp = true; *(srcScan + i++) = pScan->pVgroupList->numOfVgroups <= 1; diff --git a/tests/script/tsim/join/full_join.sim b/tests/script/tsim/join/full_join.sim index 3037f4859a..59ff6a61dc 100644 --- a/tests/script/tsim/join/full_join.sim +++ b/tests/script/tsim/join/full_join.sim @@ -238,3 +238,25 @@ endi if $data61 != @23-11-17 16:29:04.000@ then return -1 endi + +sql select count(*) from tba1 a full join tba2 b on a.ts=b.ts; +if $rows != 1 then + return -1 +endi +if $data00 != 4 then + return -1 +endi +sql select count(a.*) from tba1 a full join tba2 b on a.ts=b.ts; +if $rows != 1 then + return -1 +endi +if $data00 != 4 then + return -1 +endi +sql select count(b.*) from tba1 a full join tba2 b on a.ts=b.ts; +if $rows != 1 then + return -1 +endi +if $data00 != 4 then + return -1 +endi diff --git a/tests/script/tsim/join/left_join.sim b/tests/script/tsim/join/left_join.sim index 653f3bb0c1..8491c98d16 100644 --- a/tests/script/tsim/join/left_join.sim +++ b/tests/script/tsim/join/left_join.sim @@ -249,3 +249,25 @@ endi if $data31 != NULL then return -1 endi + +sql select count(*) from tba1 a left join tba2 b on a.ts=b.ts; +if $rows != 1 then + return -1 +endi +if $data00 != 4 then + return -1 +endi +sql select count(a.*) from tba1 a left join tba2 b on a.ts=b.ts; +if $rows != 1 then + return -1 +endi +if $data00 != 4 then + return -1 +endi +sql select count(b.*) from tba1 a left join tba2 b on a.ts=b.ts; +if $rows != 1 then + return -1 +endi +if $data00 != 2 then + return -1 +endi diff --git a/tests/script/tsim/join/right_join.sim b/tests/script/tsim/join/right_join.sim index d0fdbcbf1d..a6d4b6579a 100644 --- a/tests/script/tsim/join/right_join.sim +++ b/tests/script/tsim/join/right_join.sim @@ -158,3 +158,24 @@ if $data31 != 4 then return -1 endi +sql select count(*) from tba1 a right join tba2 b on a.ts=b.ts; +if $rows != 1 then + return -1 +endi +if $data00 != 4 then + return -1 +endi +sql select count(a.*) from tba1 a right join tba2 b on a.ts=b.ts; +if $rows != 1 then + return -1 +endi +if $data00 != 2 then + return -1 +endi +sql select count(b.*) from tba1 a right join tba2 b on a.ts=b.ts; +if $rows != 1 then + return -1 +endi +if $data00 != 4 then + return -1 +endi