enhance: optimize msortComparFn for table merge scan

This commit is contained in:
slzhou 2023-07-17 21:07:55 +08:00
parent 1b9754f1bc
commit ba2b404295
2 changed files with 63 additions and 38 deletions

View File

@ -54,6 +54,12 @@ typedef struct SMsortComparParam {
int32_t numOfSources; int32_t numOfSources;
SArray* orderInfo; // SArray<SBlockOrderInfo> SArray* orderInfo; // SArray<SBlockOrderInfo>
bool cmpGroupId; bool cmpGroupId;
int32_t sortType;
// the following field to speed up when sortType == SORT_TABLE_MERGE_SCAN
int32_t tsSlotId;
int32_t order;
__compar_fn_t cmpFn;
} SMsortComparParam; } SMsortComparParam;
typedef struct SSortHandle SSortHandle; typedef struct SSortHandle SSortHandle;

View File

@ -197,7 +197,13 @@ SSortHandle* tsortCreateSortHandle(SArray* pSortInfo, int32_t type, int32_t page
pSortHandle->pOrderedSource = taosArrayInit(4, POINTER_BYTES); pSortHandle->pOrderedSource = taosArrayInit(4, POINTER_BYTES);
pSortHandle->cmpParam.orderInfo = pSortInfo; pSortHandle->cmpParam.orderInfo = pSortInfo;
pSortHandle->cmpParam.cmpGroupId = false; pSortHandle->cmpParam.cmpGroupId = false;
pSortHandle->cmpParam.sortType = type;
if (type == SORT_TABLE_MERGE_SCAN) {
SBlockOrderInfo* pOrder = TARRAY_GET_ELEM(pSortInfo, 0);
pSortHandle->cmpParam.tsSlotId = pOrder->slotId;
pSortHandle->cmpParam.order = pOrder->order;
pSortHandle->cmpParam.cmpFn = (pOrder->order == TSDB_ORDER_ASC) ? compareInt64Val : compareInt64ValDesc;
}
tsortSetComparFp(pSortHandle, msortComparFn); tsortSetComparFp(pSortHandle, msortComparFn);
if (idstr != NULL) { if (idstr != NULL) {
@ -489,6 +495,8 @@ static int32_t adjustMergeTreeForNextTuple(SSortSource* pSource, SMultiwayMergeT
} }
releaseBufPage(pHandle->pBuf, pPage); releaseBufPage(pHandle->pBuf, pPage);
if (pSource->pageIndex % 256 == 0)
uInfo("got block from page %d from ext mem source %p", pSource->pageIndex, pSource);
} }
} else { } else {
int64_t st = taosGetTimestampUs(); int64_t st = taosGetTimestampUs();
@ -498,6 +506,7 @@ static int32_t adjustMergeTreeForNextTuple(SSortSource* pSource, SMultiwayMergeT
if (pSource->src.pBlock == NULL) { if (pSource->src.pBlock == NULL) {
(*numOfCompleted) += 1; (*numOfCompleted) += 1;
pSource->src.rowIndex = -1; pSource->src.rowIndex = -1;
uInfo("adjust merge tree. %d source completed", *numOfCompleted);
} }
} }
} }
@ -578,53 +587,63 @@ int32_t msortComparFn(const void* pLeft, const void* pRight, void* param) {
} }
} }
for (int32_t i = 0; i < pInfo->size; ++i) { if (pParam->sortType == SORT_TABLE_MERGE_SCAN) {
SBlockOrderInfo* pOrder = TARRAY_GET_ELEM(pInfo, i); SColumnInfoData* pLeftColInfoData = TARRAY_GET_ELEM(pLeftBlock->pDataBlock, pParam->tsSlotId);
SColumnInfoData* pLeftColInfoData = TARRAY_GET_ELEM(pLeftBlock->pDataBlock, pOrder->slotId); SColumnInfoData* pRightColInfoData = TARRAY_GET_ELEM(pRightBlock->pDataBlock, pParam->tsSlotId);
int64_t* left1 = (int64_t*)(pLeftColInfoData->pData) + pLeftSource->src.rowIndex;
int64_t* right1 = (int64_t*)(pRightColInfoData->pData) + pRightSource->src.rowIndex;
bool leftNull = false; int ret = pParam->cmpFn(left1, right1);
if (pLeftColInfoData->hasNull) { return ret;
if (pLeftBlock->pBlockAgg == NULL) { } else {
leftNull = colDataIsNull_s(pLeftColInfoData, pLeftSource->src.rowIndex); for (int32_t i = 0; i < pInfo->size; ++i) {
} else { SBlockOrderInfo* pOrder = TARRAY_GET_ELEM(pInfo, i);
leftNull = SColumnInfoData* pLeftColInfoData = TARRAY_GET_ELEM(pLeftBlock->pDataBlock, pOrder->slotId);
colDataIsNull(pLeftColInfoData, pLeftBlock->info.rows, pLeftSource->src.rowIndex, pLeftBlock->pBlockAgg[i]); SColumnInfoData* pRightColInfoData = TARRAY_GET_ELEM(pRightBlock->pDataBlock, pOrder->slotId);
bool leftNull = false;
if (pLeftColInfoData->hasNull) {
if (pLeftBlock->pBlockAgg == NULL) {
leftNull = colDataIsNull_s(pLeftColInfoData, pLeftSource->src.rowIndex);
} else {
leftNull = colDataIsNull(pLeftColInfoData, pLeftBlock->info.rows, pLeftSource->src.rowIndex,
pLeftBlock->pBlockAgg[i]);
}
} }
}
SColumnInfoData* pRightColInfoData = TARRAY_GET_ELEM(pRightBlock->pDataBlock, pOrder->slotId); bool rightNull = false;
bool rightNull = false; if (pRightColInfoData->hasNull) {
if (pRightColInfoData->hasNull) { if (pRightBlock->pBlockAgg == NULL) {
if (pRightBlock->pBlockAgg == NULL) { rightNull = colDataIsNull_s(pRightColInfoData, pRightSource->src.rowIndex);
rightNull = colDataIsNull_s(pRightColInfoData, pRightSource->src.rowIndex); } else {
} else { rightNull = colDataIsNull(pRightColInfoData, pRightBlock->info.rows, pRightSource->src.rowIndex,
rightNull = colDataIsNull(pRightColInfoData, pRightBlock->info.rows, pRightSource->src.rowIndex, pRightBlock->pBlockAgg[i]);
pRightBlock->pBlockAgg[i]); }
} }
}
if (leftNull && rightNull) { if (leftNull && rightNull) {
continue; // continue to next slot continue; // continue to next slot
} }
if (rightNull) { if (rightNull) {
return pOrder->nullFirst ? 1 : -1; return pOrder->nullFirst ? 1 : -1;
} }
if (leftNull) { if (leftNull) {
return pOrder->nullFirst ? -1 : 1; return pOrder->nullFirst ? -1 : 1;
} }
void* left1 = colDataGetData(pLeftColInfoData, pLeftSource->src.rowIndex); void* left1 = colDataGetData(pLeftColInfoData, pLeftSource->src.rowIndex);
void* right1 = colDataGetData(pRightColInfoData, pRightSource->src.rowIndex); void* right1 = colDataGetData(pRightColInfoData, pRightSource->src.rowIndex);
__compar_fn_t fn = getKeyComparFunc(pLeftColInfoData->info.type, pOrder->order); __compar_fn_t fn = getKeyComparFunc(pLeftColInfoData->info.type, pOrder->order);
int ret = fn(left1, right1); int ret = fn(left1, right1);
if (ret == 0) { if (ret == 0) {
continue; continue;
} else { } else {
return ret; return ret;
}
} }
} }
return 0; return 0;