fix: count(*) rewrite issue

This commit is contained in:
dapan1121 2024-03-28 16:44:07 +08:00
parent 74cef4ac6b
commit 4208557d6d
5 changed files with 100 additions and 7 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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