enh: optimize group by tag queries
This commit is contained in:
parent
c0aa91ff1e
commit
12dd3a621e
|
@ -128,6 +128,7 @@ typedef struct SScanLogicNode {
|
|||
bool paraTablesSort; // for table merge scan
|
||||
bool smallDataTsSort; // disable row id sort for table merge scan
|
||||
bool needSplit;
|
||||
bool noPseudoRefAfterGrp; // no pseudo columns referenced ater group/partition clause
|
||||
} SScanLogicNode;
|
||||
|
||||
typedef struct SJoinLogicNode {
|
||||
|
|
|
@ -510,6 +510,7 @@ static int32_t logicScanCopy(const SScanLogicNode* pSrc, SScanLogicNode* pDst) {
|
|||
COPY_SCALAR_FIELD(paraTablesSort);
|
||||
COPY_SCALAR_FIELD(smallDataTsSort);
|
||||
COPY_SCALAR_FIELD(needSplit);
|
||||
COPY_SCALAR_FIELD(noPseudoRefAfterGrp);
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
|
|
|
@ -406,6 +406,47 @@ static int32_t makeScanLogicNode(SLogicPlanContext* pCxt, SRealTableNode* pRealT
|
|||
|
||||
static bool needScanDefaultCol(EScanType scanType) { return SCAN_TYPE_TABLE_COUNT != scanType; }
|
||||
|
||||
static int32_t updateScanNoPseudoRefAfterGrp(SSelectStmt* pSelect, SScanLogicNode* pScan, SRealTableNode* pRealTable) {
|
||||
if (NULL == pScan->pScanPseudoCols || pScan->pScanPseudoCols->length <= 0) {
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
SNodeList* pList = NULL;
|
||||
int32_t code = 0;
|
||||
if (NULL == pSelect->pPartitionByList || pSelect->pPartitionByList->length <= 0) {
|
||||
if (NULL == pSelect->pGroupByList || pSelect->pGroupByList->length <= 0) {
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
code = nodesCollectColumns(pSelect, SQL_CLAUSE_GROUP_BY, pRealTable->table.tableAlias, COLLECT_COL_TYPE_TAG,
|
||||
&pList);
|
||||
if (TSDB_CODE_SUCCESS == code) {
|
||||
code = nodesCollectFuncs(pSelect, SQL_CLAUSE_GROUP_BY, pRealTable->table.tableAlias, fmIsScanPseudoColumnFunc,
|
||||
&pList);
|
||||
}
|
||||
if (TSDB_CODE_SUCCESS == code && (NULL == pList || pList->length <= 0)) {
|
||||
pScan->noPseudoRefAfterGrp = true;
|
||||
}
|
||||
goto _return;
|
||||
}
|
||||
|
||||
code = nodesCollectColumns(pSelect, SQL_CLAUSE_PARTITION_BY, pRealTable->table.tableAlias, COLLECT_COL_TYPE_TAG,
|
||||
&pList);
|
||||
if (TSDB_CODE_SUCCESS == code) {
|
||||
code = nodesCollectFuncs(pSelect, SQL_CLAUSE_PARTITION_BY, pRealTable->table.tableAlias, fmIsScanPseudoColumnFunc,
|
||||
&pList);
|
||||
}
|
||||
|
||||
if (TSDB_CODE_SUCCESS == code && (NULL == pList || pList->length <= 0)) {
|
||||
pScan->noPseudoRefAfterGrp = true;
|
||||
}
|
||||
|
||||
_return:
|
||||
|
||||
nodesDestroyList(pList);
|
||||
return code;
|
||||
}
|
||||
|
||||
static int32_t createScanLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SRealTableNode* pRealTable,
|
||||
SLogicNode** pLogicNode) {
|
||||
SScanLogicNode* pScan = NULL;
|
||||
|
@ -437,7 +478,13 @@ static int32_t createScanLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect
|
|||
&pScan->pScanPseudoCols);
|
||||
}
|
||||
|
||||
pScan->scanType = getScanType(pCxt, pScan->pScanPseudoCols, pScan->pScanCols, pScan->tableType, pSelect->tagScan);
|
||||
if (TSDB_CODE_SUCCESS == code) {
|
||||
code = updateScanNoPseudoRefAfterGrp(pSelect, pScan, pRealTable);
|
||||
}
|
||||
|
||||
if (TSDB_CODE_SUCCESS == code) {
|
||||
pScan->scanType = getScanType(pCxt, pScan->pScanPseudoCols, pScan->pScanCols, pScan->tableType, pSelect->tagScan);
|
||||
}
|
||||
|
||||
// rewrite the expression in subsequent clauses
|
||||
if (TSDB_CODE_SUCCESS == code) {
|
||||
|
|
|
@ -3302,6 +3302,17 @@ static int32_t partTagsRewriteGroupTagsToFuncs(SNodeList* pGroupTags, int32_t st
|
|||
return code;
|
||||
}
|
||||
|
||||
static int32_t partTagsOptRemovePseudoCols(SScanLogicNode* pScan) {
|
||||
if (!pScan->noPseudoRefAfterGrp || NULL == pScan->pScanPseudoCols || pScan->pScanPseudoCols->length <= 0) {
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
nodesDestroyList(pScan->pScanPseudoCols);
|
||||
pScan->pScanPseudoCols = NULL;
|
||||
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
static int32_t partTagsOptimize(SOptimizeContext* pCxt, SLogicSubplan* pLogicSubplan) {
|
||||
SLogicNode* pNode = optFindPossibleNode(pLogicSubplan->pNode, partTagsOptMayBeOptimized, NULL);
|
||||
if (NULL == pNode) {
|
||||
|
@ -3362,6 +3373,9 @@ static int32_t partTagsOptimize(SOptimizeContext* pCxt, SLogicSubplan* pLogicSub
|
|||
code = partTagsRewriteGroupTagsToFuncs(pScan->pGroupTags, start, pAgg);
|
||||
}
|
||||
}
|
||||
if (TSDB_CODE_SUCCESS == code) {
|
||||
code = partTagsOptRemovePseudoCols(pScan);
|
||||
}
|
||||
if (TSDB_CODE_SUCCESS == code) {
|
||||
code = partTagsOptRebuildTbanme(pScan->pGroupTags);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue