diff --git a/include/common/tdatablock.h b/include/common/tdatablock.h index 244894b59b..f2a33cc47f 100644 --- a/include/common/tdatablock.h +++ b/include/common/tdatablock.h @@ -136,32 +136,55 @@ static FORCE_INLINE void colDataSetNNULL(SColumnInfoData* pColumnInfoData, uint3 } static FORCE_INLINE void colDataSetInt8(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, int8_t* v) { - ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_TINYINT || - pColumnInfoData->info.type == TSDB_DATA_TYPE_UTINYINT || pColumnInfoData->info.type == TSDB_DATA_TYPE_BOOL); + ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_TINYINT || pColumnInfoData->info.type == TSDB_DATA_TYPE_BOOL); char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex; *(int8_t*)p = *(int8_t*)v; } static FORCE_INLINE void colDataSetInt16(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, int16_t* v) { - ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_SMALLINT || - pColumnInfoData->info.type == TSDB_DATA_TYPE_USMALLINT); + ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_SMALLINT); char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex; *(int16_t*)p = *(int16_t*)v; } static FORCE_INLINE void colDataSetInt32(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, int32_t* v) { - ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_INT || pColumnInfoData->info.type == TSDB_DATA_TYPE_UINT); + ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_INT); char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex; *(int32_t*)p = *(int32_t*)v; } static FORCE_INLINE void colDataSetInt64(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, int64_t* v) { int32_t type = pColumnInfoData->info.type; - ASSERT(type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_UBIGINT || type == TSDB_DATA_TYPE_TIMESTAMP); + ASSERT(type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_TIMESTAMP); char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex; *(int64_t*)p = *(int64_t*)v; } +static FORCE_INLINE void colDataSetUInt8(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, uint8_t* v) { + ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_UTINYINT); + char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex; + *(uint8_t*)p = *(uint8_t*)v; +} + +static FORCE_INLINE void colDataSetUInt16(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, uint16_t* v) { + ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_USMALLINT); + char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex; + *(uint16_t*)p = *(uint16_t*)v; +} + +static FORCE_INLINE void colDataSetUInt32(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, uint32_t* v) { + ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_UINT); + char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex; + *(uint32_t*)p = *(uint32_t*)v; +} + +static FORCE_INLINE void colDataSetUInt64(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, uint64_t* v) { + int32_t type = pColumnInfoData->info.type; + ASSERT(type == TSDB_DATA_TYPE_UBIGINT); + char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex; + *(uint64_t*)p = *(uint64_t*)v; +} + static FORCE_INLINE void colDataSetFloat(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, float* v) { ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_FLOAT); char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex; diff --git a/source/libs/function/src/builtins.c b/source/libs/function/src/builtins.c index 256123d62b..94ec5dc16a 100644 --- a/source/libs/function/src/builtins.c +++ b/source/libs/function/src/builtins.c @@ -1786,8 +1786,8 @@ static int32_t translateDiff(SFunctionNode* pFunc, char* pErrBuf, int32_t len) { } uint8_t colType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type; - if (!IS_SIGNED_NUMERIC_TYPE(colType) && !IS_FLOAT_TYPE(colType) && TSDB_DATA_TYPE_BOOL != colType && - !IS_TIMESTAMP_TYPE(colType)) { + if (!IS_INTEGER_TYPE(colType) && !IS_FLOAT_TYPE(colType) && + TSDB_DATA_TYPE_BOOL != colType && !IS_TIMESTAMP_TYPE(colType)) { return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName); } @@ -1815,6 +1815,8 @@ static int32_t translateDiff(SFunctionNode* pFunc, char* pErrBuf, int32_t len) { uint8_t resType; if (IS_SIGNED_NUMERIC_TYPE(colType) || IS_TIMESTAMP_TYPE(colType) || TSDB_DATA_TYPE_BOOL == colType) { resType = TSDB_DATA_TYPE_BIGINT; + } else if (IS_UNSIGNED_NUMERIC_TYPE(colType)) { + resType = TSDB_DATA_TYPE_UBIGINT; } else { resType = TSDB_DATA_TYPE_DOUBLE; } diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index bcbb3af950..e5a06b108d 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -115,8 +115,9 @@ typedef struct SDiffInfo { bool ignoreNegative; // replace the ignore with case when bool firstOutput; union { - int64_t i64; - double d64; + int64_t i64; + uint64_t u64; + double d64; } prev; int64_t prevTs; @@ -2733,6 +2734,18 @@ static int32_t doSetPrevVal(SDiffInfo* pDiffInfo, int32_t type, const char* pv, case TSDB_DATA_TYPE_DOUBLE: pDiffInfo->prev.d64 = *(double*)pv; break; + case TSDB_DATA_TYPE_UTINYINT: + pDiffInfo->prev.u64 = *(uint8_t*)pv; + break; + case TSDB_DATA_TYPE_UINT: + pDiffInfo->prev.u64 = *(uint32_t*)pv; + break; + case TSDB_DATA_TYPE_USMALLINT: + pDiffInfo->prev.u64 = *(uint16_t*)pv; + break; + case TSDB_DATA_TYPE_UBIGINT: + pDiffInfo->prev.u64 = *(uint64_t*)pv; + break; default: return TSDB_CODE_FUNC_FUNTION_PARA_TYPE; } @@ -2814,6 +2827,51 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv, pDiffInfo->prev.d64 = v; break; } + case TSDB_DATA_TYPE_UTINYINT: { + uint8_t v = *(uint8_t*)pv; + uint64_t delta = v - pDiffInfo->prev.u64; // direct previous may be null + if (delta < 0 && pDiffInfo->ignoreNegative) { + colDataSetNull_f_s(pOutput, pos); + } else { + colDataSetUInt64(pOutput, pos, &delta); + } + pDiffInfo->prev.u64 = v; + break; + } + case TSDB_DATA_TYPE_USMALLINT: { + uint16_t v = *(uint16_t*)pv; + uint64_t delta = v - pDiffInfo->prev.u64; // direct previous may be null + if (delta < 0 && pDiffInfo->ignoreNegative) { + colDataSetNull_f_s(pOutput, pos); + } else { + colDataSetUInt64(pOutput, pos, &delta); + } + pDiffInfo->prev.u64 = v; + break; + } + case TSDB_DATA_TYPE_UINT: { + uint32_t v = *(uint32_t*)pv; + uint64_t delta = v - pDiffInfo->prev.u64; // direct previous may be null + if (delta < 0 && pDiffInfo->ignoreNegative) { + colDataSetNull_f_s(pOutput, pos); + } else { + colDataSetUInt64(pOutput, pos, &delta); + } + pDiffInfo->prev.u64 = v; + + break; + } + case TSDB_DATA_TYPE_UBIGINT: { + uint64_t v = *(uint64_t*)pv; + uint64_t delta = v - pDiffInfo->prev.i64; // direct previous may be null + if (delta < 0 && pDiffInfo->ignoreNegative) { + colDataSetNull_f_s(pOutput, pos); + } else { + colDataSetUInt64(pOutput, pos, &delta); + } + pDiffInfo->prev.u64 = v; + break; + } default: return TSDB_CODE_FUNC_FUNTION_PARA_TYPE; }