diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 7081a65dd0f785924037e4c0a4acc2fb8d6ef698..77cde174eee2d063259337ebff380fb3b0c3dcc2 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -166,6 +166,7 @@ typedef struct SSampleInfo { typedef struct STailItem { int64_t timestamp; + bool isNull; char data[]; } STailItem; @@ -3124,7 +3125,7 @@ int32_t sampleFunction(SqlFunctionCtx* pCtx) { int32_t startOffset = pCtx->offset; for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) { - if (colDataIsNull_f(pInputCol->nullbitmap, i)) { + if (colDataIsNull_s(pInputCol, i)) { //colDataAppendNULL(pOutput, i); continue; } @@ -3190,14 +3191,19 @@ bool tailFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo *pResultInfo) { size_t unitSize = sizeof(STailItem) + pInfo->colBytes; for (int32_t i = 0; i < pInfo->numOfPoints; ++i) { pInfo->pItems[i] = (STailItem *)(pItem + i * unitSize); + pInfo->pItems[i]->isNull = false; } return true; } -static void tailAssignResult(STailItem* pItem, char *data, int32_t colBytes, TSKEY ts) { +static void tailAssignResult(STailItem* pItem, char *data, int32_t colBytes, TSKEY ts, bool isNull) { pItem->timestamp = ts; - memcpy(pItem->data, data, colBytes); + if (isNull) { + pItem->isNull = true; + } else { + memcpy(pItem->data, data, colBytes); + } } static int32_t tailCompFn(const void *p1, const void *p2, const void *param) { @@ -3206,14 +3212,14 @@ static int32_t tailCompFn(const void *p1, const void *p2, const void *param) { return compareInt64Val(&d1->timestamp, &d2->timestamp); } -static void doTailAdd(STailInfo* pInfo, char *data, TSKEY ts) { +static void doTailAdd(STailInfo* pInfo, char *data, TSKEY ts, bool isNull) { STailItem **pList = pInfo->pItems; if (pInfo->numAdded < pInfo->numOfPoints) { - tailAssignResult(pList[pInfo->numAdded], data, pInfo->colBytes, ts); + tailAssignResult(pList[pInfo->numAdded], data, pInfo->colBytes, ts, isNull); taosheapsort((void *)pList, sizeof(STailItem **), pInfo->numAdded + 1, NULL, tailCompFn, 0); pInfo->numAdded++; } else if (pList[0]->timestamp < ts) { - tailAssignResult(pList[0], data, pInfo->colBytes, ts); + tailAssignResult(pList[0], data, pInfo->colBytes, ts, isNull); taosheapadjust((void *)pList, sizeof(STailItem **), 0, pInfo->numOfPoints - 1, NULL, tailCompFn, NULL, 0); } } @@ -3231,19 +3237,21 @@ int32_t tailFunction(SqlFunctionCtx* pCtx) { int32_t startOffset = pCtx->offset; for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) { - if (colDataIsNull_f(pInputCol->nullbitmap, i)) { - colDataAppendNULL(pOutput, i); - continue; - } char* data = colDataGetData(pInputCol, i); - doTailAdd(pInfo, data, tsList[i]); + doTailAdd(pInfo, data, tsList[i], colDataIsNull_s(pInputCol, i)); } + taosqsort(pInfo->pItems, pInfo->numOfPoints, POINTER_BYTES, NULL, tailCompFn); + for (int32_t i = 0; i < pInfo->numOfPoints; ++i) { int32_t pos = startOffset + i; STailItem *pItem = pInfo->pItems[i]; - colDataAppend(pOutput, pos, pItem->data, false); + if (pItem->isNull) { + colDataAppendNULL(pOutput, pos); + } else { + colDataAppend(pOutput, pos, pItem->data, false); + } } return pInfo->numOfPoints;