diff --git a/source/libs/executor/src/scanoperator.c b/source/libs/executor/src/scanoperator.c index 51edfcb42c..6894b4bc09 100644 --- a/source/libs/executor/src/scanoperator.c +++ b/source/libs/executor/src/scanoperator.c @@ -3968,22 +3968,33 @@ static SSDataBlock* getBlockForTableMergeScan(void* param) { } -SArray* generateSortByTsInfo(SArray* colMatchInfo, int32_t order) { +SArray* generateSortByTsPkInfo(SArray* colMatchInfo, int32_t order) { int32_t tsTargetSlotId = 0; + int32_t pkTargetSlotId = -1; for (int32_t i = 0; i < taosArrayGetSize(colMatchInfo); ++i) { SColMatchItem* colInfo = taosArrayGet(colMatchInfo, i); if (colInfo->colId == PRIMARYKEY_TIMESTAMP_COL_ID) { tsTargetSlotId = colInfo->dstSlotId; } + if (colInfo->isPk) { + pkTargetSlotId = colInfo->dstSlotId; + } } SArray* pList = taosArrayInit(1, sizeof(SBlockOrderInfo)); - SBlockOrderInfo bi = {0}; - bi.order = order; - bi.slotId = tsTargetSlotId; - bi.nullFirst = NULL_ORDER_FIRST; + SBlockOrderInfo biTs = {0}; + biTs.order = order; + biTs.slotId = tsTargetSlotId; + biTs.nullFirst = NULL_ORDER_FIRST; + taosArrayPush(pList, &biTs); - taosArrayPush(pList, &bi); + if (pkTargetSlotId != -1) { + SBlockOrderInfo biPk = {0}; + biPk.order = order; + biPk.slotId = pkTargetSlotId; + biPk.nullFirst = NULL_ORDER_FIRST; + taosArrayPush(pList, &biPk); + } return pList; } @@ -4354,7 +4365,7 @@ SOperatorInfo* createTableMergeScanOperatorInfo(STableScanPhysiNode* pTableScanN pInfo->bSortRowId = false; } - pInfo->pSortInfo = generateSortByTsInfo(pInfo->base.matchInfo.pList, pInfo->base.cond.order); + pInfo->pSortInfo = generateSortByTsPkInfo(pInfo->base.matchInfo.pList, pInfo->base.cond.order); pInfo->pReaderBlock = createOneDataBlock(pInfo->pResBlock, false); pInfo->needCountEmptyTable = tsCountAlwaysReturnValue && pTableScanNode->needCountEmptyTable; diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index c34d8ac64f..b4b45ced46 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -307,6 +307,26 @@ static int32_t addPrimaryKeyCol(uint64_t tableId, const SSchema* pSchema, SNodeL return TSDB_CODE_SUCCESS; } +static int32_t addPkCol(uint64_t tableId, const SSchema* pSchema, SNodeList** pCols) { + bool found = false; + SNode* pCol = NULL; + FOREACH(pCol, *pCols) { + if (pSchema->colId == ((SColumnNode*)pCol)->colId) { + found = true; + break; + } + } + + if (!found) { + return nodesListMakeStrictAppend(pCols, createFirstCol(tableId, pSchema)); + } + return TSDB_CODE_SUCCESS; +} + +static bool hasPkInTable(const STableMeta* pTableMeta) { + return pTableMeta->tableInfo.numOfColumns>=2 && pTableMeta->schema[1].flags & COL_IS_KEY; +} + static int32_t addSystableFirstCol(uint64_t tableId, const SSchema* pSchema, SNodeList** pCols) { if (LIST_LENGTH(*pCols) > 0) { return TSDB_CODE_SUCCESS; @@ -318,6 +338,9 @@ static int32_t addDefaultScanCol(const STableMeta* pMeta, SNodeList** pCols) { if (TSDB_SYSTEM_TABLE == pMeta->tableType) { return addSystableFirstCol(pMeta->uid, pMeta->schema, pCols); } + if (hasPkInTable(pMeta)) { + addPkCol(pMeta->uid, pMeta->schema + 1, pCols); + } return addPrimaryKeyCol(pMeta->uid, pMeta->schema, pCols); }