Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix diff not support unsigned type #22782

Merged
merged 4 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion source/libs/function/src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,7 @@ 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 &&
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
8 changes: 8 additions & 0 deletions source/libs/function/src/builtinsimpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2714,16 +2714,20 @@ static int32_t doSetPrevVal(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
case TSDB_DATA_TYPE_BOOL:
pDiffInfo->prev.i64 = *(bool*)pv ? 1 : 0;
break;
case TSDB_DATA_TYPE_UTINYINT:
case TSDB_DATA_TYPE_TINYINT:
pDiffInfo->prev.i64 = *(int8_t*)pv;
break;
case TSDB_DATA_TYPE_UINT:
case TSDB_DATA_TYPE_INT:
pDiffInfo->prev.i64 = *(int32_t*)pv;
break;
case TSDB_DATA_TYPE_USMALLINT:
case TSDB_DATA_TYPE_SMALLINT:
pDiffInfo->prev.i64 = *(int16_t*)pv;
break;
case TSDB_DATA_TYPE_TIMESTAMP:
case TSDB_DATA_TYPE_UBIGINT:
case TSDB_DATA_TYPE_BIGINT:
pDiffInfo->prev.i64 = *(int64_t*)pv;
break;
Expand All @@ -2745,6 +2749,7 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
int64_t ts) {
pDiffInfo->prevTs = ts;
switch (type) {
case TSDB_DATA_TYPE_UINT:
case TSDB_DATA_TYPE_INT: {
int32_t v = *(int32_t*)pv;
int64_t delta = v - pDiffInfo->prev.i64; // direct previous may be null
Expand All @@ -2758,6 +2763,7 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
break;
}
case TSDB_DATA_TYPE_BOOL:
case TSDB_DATA_TYPE_UTINYINT:
case TSDB_DATA_TYPE_TINYINT: {
int8_t v = *(int8_t*)pv;
int64_t delta = v - pDiffInfo->prev.i64; // direct previous may be null
Expand All @@ -2769,6 +2775,7 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
pDiffInfo->prev.i64 = v;
break;
}
case TSDB_DATA_TYPE_USMALLINT:
case TSDB_DATA_TYPE_SMALLINT: {
int16_t v = *(int16_t*)pv;
int64_t delta = v - pDiffInfo->prev.i64; // direct previous may be null
Expand All @@ -2781,6 +2788,7 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
break;
}
case TSDB_DATA_TYPE_TIMESTAMP:
case TSDB_DATA_TYPE_UBIGINT:
case TSDB_DATA_TYPE_BIGINT: {
int64_t v = *(int64_t*)pv;
int64_t delta = v - pDiffInfo->prev.i64; // direct previous may be null
Expand Down
24 changes: 19 additions & 5 deletions tests/system-test/2-query/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def init(self, conn, logSql, replicaVar=1):
self.perfix = 'dev'
self.tables = 10

def check_result(self):
for i in range(self.rowNum):
tdSql.checkData(i, 0, 1);

def run(self):
tdSql.prepare()
Expand Down Expand Up @@ -179,11 +182,6 @@ def run(self):
tdSql.error(f"select diff(col8) from {dbname}.stb_1")
tdSql.error(f"select diff(col9) from {dbname}.stb")
tdSql.error(f"select diff(col9) from {dbname}.stb_1")
tdSql.error(f"select diff(col11) from {dbname}.stb_1")
tdSql.error(f"select diff(col12) from {dbname}.stb_1")
tdSql.error(f"select diff(col13) from {dbname}.stb_1")
tdSql.error(f"select diff(col14) from {dbname}.stb_1")
tdSql.error(f"select diff(col14) from {dbname}.stb_1")
tdSql.error(f"select diff(col1,col1,col1) from {dbname}.stb_1")
tdSql.error(f"select diff(col1,1,col1) from {dbname}.stb_1")
tdSql.error(f"select diff(col1,col1,col) from {dbname}.stb_1")
Expand Down Expand Up @@ -217,6 +215,22 @@ def run(self):
tdSql.query(f"select diff(col6) from {dbname}.stb_1")
tdSql.checkRows(10)

tdSql.query(f"select diff(col11) from {dbname}.stb_1")
tdSql.checkRows(10)
self.check_result()

tdSql.query(f"select diff(col12) from {dbname}.stb_1")
tdSql.checkRows(10)
self.check_result()

tdSql.query(f"select diff(col13) from {dbname}.stb_1")
tdSql.checkRows(10)
self.check_result()

tdSql.query(f"select diff(col14) from {dbname}.stb_1")
tdSql.checkRows(10)
self.check_result()

tdSql.execute(f'''create table {dbname}.stb1(ts timestamp, col1 tinyint, col2 smallint, col3 int, col4 bigint, col5 float, col6 double,
col7 bool, col8 binary(20), col9 nchar(20), col11 tinyint unsigned, col12 smallint unsigned, col13 int unsigned, col14 bigint unsigned) tags(loc nchar(20))''')
tdSql.execute(f"create table {dbname}.stb1_1 using {dbname}.stb tags('shanghai')")
Expand Down