Merge pull request #28793 from taosdata/fix/main/TD-32883

remove duplicate group by cols
This commit is contained in:
Shengliang Guan 2024-11-18 19:32:16 +08:00 committed by GitHub
commit 6044bb06f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 83 additions and 11 deletions

View File

@ -174,6 +174,7 @@ char* nodesGetNameFromColumnNode(SNode* pNode);
int32_t nodesGetOutputNumFromSlotList(SNodeList* pSlots);
void nodesSortList(SNodeList** pList, int32_t (*)(SNode* pNode1, SNode* pNode2));
void destroyFuncParam(void* pFuncStruct);
int32_t nodesListDeduplicate(SNodeList** pList);
#ifdef __cplusplus
}

View File

@ -6115,7 +6115,7 @@ int32_t tsdbNextDataBlock2(STsdbReader* pReader, bool* hasNext) {
TSDB_CHECK_CODE(code, lino, _end);
}
goto _end;
return code;
}
}
@ -6142,7 +6142,7 @@ int32_t tsdbNextDataBlock2(STsdbReader* pReader, bool* hasNext) {
acquired = false;
TSDB_CHECK_CODE(code, lino, _end);
}
goto _end;
return code;
}
if (pReader->step == EXTERNAL_ROWS_MAIN && pReader->innerReader[1] != NULL) {

View File

@ -3043,7 +3043,6 @@ static int32_t setBlockIntoRes(SStreamScanInfo* pInfo, const SSDataBlock* pBlock
}
if (code) {
blockDataFreeRes((SSDataBlock*)pBlock);
QUERY_CHECK_CODE(code, lino, _end);
}

View File

@ -153,6 +153,12 @@ static bool caseWhenNodeEqual(const SCaseWhenNode* a, const SCaseWhenNode* b) {
return true;
}
static bool groupingSetNodeEqual(const SGroupingSetNode* a, const SGroupingSetNode* b) {
COMPARE_SCALAR_FIELD(groupingSetType);
COMPARE_NODE_LIST_FIELD(pParameterList);
return true;
}
bool nodesEqualNode(const SNode* a, const SNode* b) {
if (a == b) {
return true;
@ -181,10 +187,11 @@ bool nodesEqualNode(const SNode* a, const SNode* b) {
return whenThenNodeEqual((const SWhenThenNode*)a, (const SWhenThenNode*)b);
case QUERY_NODE_CASE_WHEN:
return caseWhenNodeEqual((const SCaseWhenNode*)a, (const SCaseWhenNode*)b);
case QUERY_NODE_GROUPING_SET:
return groupingSetNodeEqual((const SGroupingSetNode*)a, (const SGroupingSetNode*)b);
case QUERY_NODE_REAL_TABLE:
case QUERY_NODE_TEMP_TABLE:
case QUERY_NODE_JOIN_TABLE:
case QUERY_NODE_GROUPING_SET:
case QUERY_NODE_ORDER_BY_EXPR:
case QUERY_NODE_LIMIT:
return false;

View File

@ -2948,3 +2948,46 @@ void nodesSortList(SNodeList** pList, int32_t (*comp)(SNode* pNode1, SNode* pNod
inSize *= 2;
}
}
static SNode* nodesListFindNode(SNodeList* pList, SNode* pNode) {
SNode* pFound = NULL;
FOREACH(pFound, pList) {
if (nodesEqualNode(pFound, pNode)) {
break;
}
}
return pFound;
}
int32_t nodesListDeduplicate(SNodeList** ppList) {
if (!ppList || LIST_LENGTH(*ppList) <= 1) return TSDB_CODE_SUCCESS;
if (LIST_LENGTH(*ppList) == 2) {
SNode* pNode1 = nodesListGetNode(*ppList, 0);
SNode* pNode2 = nodesListGetNode(*ppList, 1);
if (nodesEqualNode(pNode1, pNode2)) {
SListCell* pCell = nodesListGetCell(*ppList, 1);
(void)nodesListErase(*ppList, pCell);
}
return TSDB_CODE_SUCCESS;
}
SNodeList* pTmp = NULL;
int32_t code = nodesMakeList(&pTmp);
if (TSDB_CODE_SUCCESS == code) {
SNode* pNode = NULL;
FOREACH(pNode, *ppList) {
SNode* pFound = nodesListFindNode(pTmp, pNode);
if (NULL == pFound) {
code = nodesCloneNode(pNode, &pFound);
if (TSDB_CODE_SUCCESS == code) code = nodesListStrictAppend(pTmp, pFound);
if (TSDB_CODE_SUCCESS != code) break;
}
}
}
if (TSDB_CODE_SUCCESS == code) {
nodesDestroyList(*ppList);
*ppList = pTmp;
} else {
nodesDestroyList(pTmp);
}
return code;
}

View File

@ -336,7 +336,10 @@ static int32_t calcConstGroupBy(SCalcConstContext* pCxt, SSelectStmt* pSelect) {
}
}
}
NODES_DESTORY_LIST(pSelect->pGroupByList);
FOREACH(pNode, pSelect->pGroupByList) {
if (!cell->pPrev) continue;
ERASE_NODE(pSelect->pGroupByList);
}
}
return code;
}

View File

@ -838,8 +838,11 @@ static int32_t createAggLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect,
}
if (NULL != pSelect->pGroupByList) {
pAgg->pGroupKeys = NULL;
code = nodesCloneList(pSelect->pGroupByList, &pAgg->pGroupKeys);
code = nodesListDeduplicate(&pSelect->pGroupByList);
if (TSDB_CODE_SUCCESS == code) {
pAgg->pGroupKeys = NULL;
code = nodesCloneList(pSelect->pGroupByList, &pAgg->pGroupKeys);
}
}
// rewrite the expression in subsequent clauses

View File

@ -422,21 +422,36 @@ class TDTestCase:
def test_TS5567(self):
tdSql.query(f"select const_col from (select 1 as const_col from {self.dbname}.{self.stable}) t group by const_col")
tdSql.checkRows(50)
tdSql.checkRows(1)
tdSql.query(f"select const_col from (select 1 as const_col from {self.dbname}.{self.stable}) t partition by const_col")
tdSql.checkRows(50)
tdSql.query(f"select const_col from (select 1 as const_col, count(c1) from {self.dbname}.{self.stable} t group by c1) group by const_col")
tdSql.checkRows(10)
tdSql.checkRows(1)
tdSql.query(f"select const_col from (select 1 as const_col, count(c1) from {self.dbname}.{self.stable} t group by c1) partition by const_col")
tdSql.checkRows(10)
tdSql.query(f"select const_col as c_c from (select 1 as const_col from {self.dbname}.{self.stable}) t group by c_c")
tdSql.checkRows(50)
tdSql.checkRows(1)
tdSql.query(f"select const_col as c_c from (select 1 as const_col from {self.dbname}.{self.stable}) t partition by c_c")
tdSql.checkRows(50)
tdSql.query(f"select const_col from (select 1 as const_col, count(c1) from {self.dbname}.{self.stable} t group by c1) group by 1")
tdSql.checkRows(10)
tdSql.checkRows(1)
tdSql.query(f"select const_col from (select 1 as const_col, count(c1) from {self.dbname}.{self.stable} t group by c1) partition by 1")
tdSql.checkRows(10)
def test_TD_32883(self):
sql = "select avg(c1), t9 from db.stb group by t9,t9, tbname"
tdSql.query(sql, queryTimes=1)
tdSql.checkRows(5)
sql = "select avg(c1), t10 from db.stb group by t10,t10, tbname"
tdSql.query(sql, queryTimes=1)
tdSql.checkRows(5)
sql = "select avg(c1), t10 from db.stb partition by t10,t10, tbname"
tdSql.query(sql, queryTimes=1)
tdSql.checkRows(5)
sql = "select avg(c1), concat(t9,t10) from db.stb group by concat(t9,t10), concat(t9,t10),tbname"
tdSql.query(sql, queryTimes=1)
tdSql.checkRows(5)
def run(self):
tdSql.prepare()
self.prepare_db()
@ -470,6 +485,7 @@ class TDTestCase:
self.test_event_window(nonempty_tb_num)
self.test_TS5567()
self.test_TD_32883()
## test old version before changed
# self.test_groupby('group', 0, 0)