Skip to content

Commit

Permalink
fix: fix diff not support unsigned type
Browse files Browse the repository at this point in the history
  • Loading branch information
glzhao89 committed Sep 6, 2023
1 parent 1bce2b7 commit 7ccf959
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 10 deletions.
35 changes: 29 additions & 6 deletions include/common/tdatablock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions source/libs/function/src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down
62 changes: 60 additions & 2 deletions source/libs/function/src/builtinsimpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 7ccf959

Please sign in to comment.