Merge pull request #21944 from taosdata/fix/TD-25098
fix: remove order logic in diff function
This commit is contained in:
commit
1aeb15cbff
|
@ -2702,13 +2702,12 @@ static int32_t doSetPrevVal(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
|
||||||
}
|
}
|
||||||
|
|
||||||
static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv, SColumnInfoData* pOutput, int32_t pos,
|
static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv, SColumnInfoData* pOutput, int32_t pos,
|
||||||
int32_t order, int64_t ts) {
|
int64_t ts) {
|
||||||
int32_t factor = (order == TSDB_ORDER_ASC) ? 1 : -1;
|
|
||||||
pDiffInfo->prevTs = ts;
|
pDiffInfo->prevTs = ts;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case TSDB_DATA_TYPE_INT: {
|
case TSDB_DATA_TYPE_INT: {
|
||||||
int32_t v = *(int32_t*)pv;
|
int32_t v = *(int32_t*)pv;
|
||||||
int64_t delta = factor * (v - pDiffInfo->prev.i64); // direct previous may be null
|
int64_t delta = v - pDiffInfo->prev.i64; // direct previous may be null
|
||||||
if (delta < 0 && pDiffInfo->ignoreNegative) {
|
if (delta < 0 && pDiffInfo->ignoreNegative) {
|
||||||
colDataSetNull_f_s(pOutput, pos);
|
colDataSetNull_f_s(pOutput, pos);
|
||||||
} else {
|
} else {
|
||||||
|
@ -2721,7 +2720,7 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
|
||||||
case TSDB_DATA_TYPE_BOOL:
|
case TSDB_DATA_TYPE_BOOL:
|
||||||
case TSDB_DATA_TYPE_TINYINT: {
|
case TSDB_DATA_TYPE_TINYINT: {
|
||||||
int8_t v = *(int8_t*)pv;
|
int8_t v = *(int8_t*)pv;
|
||||||
int64_t delta = factor * (v - pDiffInfo->prev.i64); // direct previous may be null
|
int64_t delta = v - pDiffInfo->prev.i64; // direct previous may be null
|
||||||
if (delta < 0 && pDiffInfo->ignoreNegative) {
|
if (delta < 0 && pDiffInfo->ignoreNegative) {
|
||||||
colDataSetNull_f_s(pOutput, pos);
|
colDataSetNull_f_s(pOutput, pos);
|
||||||
} else {
|
} else {
|
||||||
|
@ -2732,7 +2731,7 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
|
||||||
}
|
}
|
||||||
case TSDB_DATA_TYPE_SMALLINT: {
|
case TSDB_DATA_TYPE_SMALLINT: {
|
||||||
int16_t v = *(int16_t*)pv;
|
int16_t v = *(int16_t*)pv;
|
||||||
int64_t delta = factor * (v - pDiffInfo->prev.i64); // direct previous may be null
|
int64_t delta = v - pDiffInfo->prev.i64; // direct previous may be null
|
||||||
if (delta < 0 && pDiffInfo->ignoreNegative) {
|
if (delta < 0 && pDiffInfo->ignoreNegative) {
|
||||||
colDataSetNull_f_s(pOutput, pos);
|
colDataSetNull_f_s(pOutput, pos);
|
||||||
} else {
|
} else {
|
||||||
|
@ -2744,7 +2743,7 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
|
||||||
case TSDB_DATA_TYPE_TIMESTAMP:
|
case TSDB_DATA_TYPE_TIMESTAMP:
|
||||||
case TSDB_DATA_TYPE_BIGINT: {
|
case TSDB_DATA_TYPE_BIGINT: {
|
||||||
int64_t v = *(int64_t*)pv;
|
int64_t v = *(int64_t*)pv;
|
||||||
int64_t delta = factor * (v - pDiffInfo->prev.i64); // direct previous may be null
|
int64_t delta = v - pDiffInfo->prev.i64; // direct previous may be null
|
||||||
if (delta < 0 && pDiffInfo->ignoreNegative) {
|
if (delta < 0 && pDiffInfo->ignoreNegative) {
|
||||||
colDataSetNull_f_s(pOutput, pos);
|
colDataSetNull_f_s(pOutput, pos);
|
||||||
} else {
|
} else {
|
||||||
|
@ -2755,7 +2754,7 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
|
||||||
}
|
}
|
||||||
case TSDB_DATA_TYPE_FLOAT: {
|
case TSDB_DATA_TYPE_FLOAT: {
|
||||||
float v = *(float*)pv;
|
float v = *(float*)pv;
|
||||||
double delta = factor * (v - pDiffInfo->prev.d64); // direct previous may be null
|
double delta = v - pDiffInfo->prev.d64; // direct previous may be null
|
||||||
if ((delta < 0 && pDiffInfo->ignoreNegative) || isinf(delta) || isnan(delta)) { // check for overflow
|
if ((delta < 0 && pDiffInfo->ignoreNegative) || isinf(delta) || isnan(delta)) { // check for overflow
|
||||||
colDataSetNull_f_s(pOutput, pos);
|
colDataSetNull_f_s(pOutput, pos);
|
||||||
} else {
|
} else {
|
||||||
|
@ -2766,7 +2765,7 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
|
||||||
}
|
}
|
||||||
case TSDB_DATA_TYPE_DOUBLE: {
|
case TSDB_DATA_TYPE_DOUBLE: {
|
||||||
double v = *(double*)pv;
|
double v = *(double*)pv;
|
||||||
double delta = factor * (v - pDiffInfo->prev.d64); // direct previous may be null
|
double delta = v - pDiffInfo->prev.d64; // direct previous may be null
|
||||||
if ((delta < 0 && pDiffInfo->ignoreNegative) || isinf(delta) || isnan(delta)) { // check for overflow
|
if ((delta < 0 && pDiffInfo->ignoreNegative) || isinf(delta) || isnan(delta)) { // check for overflow
|
||||||
colDataSetNull_f_s(pOutput, pos);
|
colDataSetNull_f_s(pOutput, pos);
|
||||||
} else {
|
} else {
|
||||||
|
@ -2797,82 +2796,42 @@ int32_t diffFunction(SqlFunctionCtx* pCtx) {
|
||||||
|
|
||||||
SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
|
SColumnInfoData* pOutput = (SColumnInfoData*)pCtx->pOutput;
|
||||||
|
|
||||||
if (pCtx->order == TSDB_ORDER_ASC) {
|
for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
|
||||||
for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
|
int32_t pos = startOffset + numOfElems;
|
||||||
int32_t pos = startOffset + numOfElems;
|
|
||||||
|
|
||||||
if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
|
if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
|
||||||
if (pDiffInfo->includeNull) {
|
if (pDiffInfo->includeNull) {
|
||||||
colDataSetNull_f_s(pOutput, pos);
|
colDataSetNull_f_s(pOutput, pos);
|
||||||
|
|
||||||
numOfElems += 1;
|
numOfElems += 1;
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
char* pv = colDataGetData(pInputCol, i);
|
|
||||||
|
|
||||||
if (pDiffInfo->hasPrev) {
|
|
||||||
if (tsList[i] == pDiffInfo->prevTs) {
|
|
||||||
return TSDB_CODE_FUNC_DUP_TIMESTAMP;
|
|
||||||
}
|
|
||||||
int32_t code = doHandleDiff(pDiffInfo, pInputCol->info.type, pv, pOutput, pos, pCtx->order, tsList[i]);
|
|
||||||
if (code != TSDB_CODE_SUCCESS) {
|
|
||||||
return code;
|
|
||||||
}
|
|
||||||
// handle selectivity
|
|
||||||
if (pCtx->subsidiaries.num > 0) {
|
|
||||||
appendSelectivityValue(pCtx, i, pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
numOfElems++;
|
|
||||||
} else {
|
|
||||||
int32_t code = doSetPrevVal(pDiffInfo, pInputCol->info.type, pv, tsList[i]);
|
|
||||||
if (code != TSDB_CODE_SUCCESS) {
|
|
||||||
return code;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pDiffInfo->hasPrev = true;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
for (int32_t i = pInput->startRowIndex; i < pInput->numOfRows + pInput->startRowIndex; i += 1) {
|
|
||||||
int32_t pos = startOffset + numOfElems;
|
|
||||||
|
|
||||||
if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
|
char* pv = colDataGetData(pInputCol, i);
|
||||||
if (pDiffInfo->includeNull) {
|
|
||||||
colDataSetNull_f_s(pOutput, pos);
|
if (pDiffInfo->hasPrev) {
|
||||||
numOfElems += 1;
|
if (tsList[i] == pDiffInfo->prevTs) {
|
||||||
}
|
return TSDB_CODE_FUNC_DUP_TIMESTAMP;
|
||||||
continue;
|
}
|
||||||
|
int32_t code = doHandleDiff(pDiffInfo, pInputCol->info.type, pv, pOutput, pos, tsList[i]);
|
||||||
|
if (code != TSDB_CODE_SUCCESS) {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
// handle selectivity
|
||||||
|
if (pCtx->subsidiaries.num > 0) {
|
||||||
|
appendSelectivityValue(pCtx, i, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
char* pv = colDataGetData(pInputCol, i);
|
numOfElems++;
|
||||||
|
} else {
|
||||||
// there is a row of previous data block to be handled in the first place.
|
int32_t code = doSetPrevVal(pDiffInfo, pInputCol->info.type, pv, tsList[i]);
|
||||||
if (pDiffInfo->hasPrev) {
|
if (code != TSDB_CODE_SUCCESS) {
|
||||||
if (tsList[i] == pDiffInfo->prevTs) {
|
return code;
|
||||||
return TSDB_CODE_FUNC_DUP_TIMESTAMP;
|
|
||||||
}
|
|
||||||
int32_t code = doHandleDiff(pDiffInfo, pInputCol->info.type, pv, pOutput, pos, pCtx->order, tsList[i]);
|
|
||||||
if (code != TSDB_CODE_SUCCESS) {
|
|
||||||
return code;
|
|
||||||
}
|
|
||||||
// handle selectivity
|
|
||||||
if (pCtx->subsidiaries.num > 0) {
|
|
||||||
appendSelectivityValue(pCtx, i, pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
numOfElems++;
|
|
||||||
} else {
|
|
||||||
int32_t code = doSetPrevVal(pDiffInfo, pInputCol->info.type, pv, tsList[i]);
|
|
||||||
if (code != TSDB_CODE_SUCCESS) {
|
|
||||||
return code;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pDiffInfo->hasPrev = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pDiffInfo->hasPrev = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pResInfo->numOfRes = numOfElems;
|
pResInfo->numOfRes = numOfElems;
|
||||||
|
|
|
@ -23,7 +23,7 @@ class TDTestCase:
|
||||||
tdSql.execute(
|
tdSql.execute(
|
||||||
f"create table {dbname}.ntb(ts timestamp,c1 int,c2 double,c3 float)")
|
f"create table {dbname}.ntb(ts timestamp,c1 int,c2 double,c3 float)")
|
||||||
tdSql.execute(
|
tdSql.execute(
|
||||||
f"insert into {dbname}.ntb values(now,1,1.0,10.5)(now+1s,10,-100.0,5.1)(now+10s,-1,15.1,5.0)")
|
f"insert into {dbname}.ntb values('2023-01-01 00:00:01',1,1.0,10.5)('2023-01-01 00:00:02',10,-100.0,5.1)('2023-01-01 00:00:03',-1,15.1,5.0)")
|
||||||
|
|
||||||
tdSql.query(f"select diff(c1,0) from {dbname}.ntb")
|
tdSql.query(f"select diff(c1,0) from {dbname}.ntb")
|
||||||
tdSql.checkRows(2)
|
tdSql.checkRows(2)
|
||||||
|
@ -233,6 +233,40 @@ class TDTestCase:
|
||||||
tdSql.checkRows(19)
|
tdSql.checkRows(19)
|
||||||
tdSql.checkData(0,0,None)
|
tdSql.checkData(0,0,None)
|
||||||
|
|
||||||
|
# TD-25098
|
||||||
|
|
||||||
|
tdSql.query(f"select ts, diff(c1) from {dbname}.ntb order by ts")
|
||||||
|
tdSql.checkRows(2)
|
||||||
|
tdSql.checkData(0, 0, '2023-01-01 00:00:02.000')
|
||||||
|
tdSql.checkData(1, 0, '2023-01-01 00:00:03.000')
|
||||||
|
|
||||||
|
tdSql.checkData(0, 1, 9)
|
||||||
|
tdSql.checkData(1, 1, -11)
|
||||||
|
|
||||||
|
tdSql.query(f"select ts, diff(c1) from {dbname}.ntb order by ts desc")
|
||||||
|
tdSql.checkRows(2)
|
||||||
|
tdSql.checkData(0, 0, '2023-01-01 00:00:03.000')
|
||||||
|
tdSql.checkData(1, 0, '2023-01-01 00:00:02.000')
|
||||||
|
|
||||||
|
tdSql.checkData(0, 1, -11)
|
||||||
|
tdSql.checkData(1, 1, 9)
|
||||||
|
|
||||||
|
tdSql.query(f"select ts, diff(c1) from (select * from {dbname}.ntb order by ts)")
|
||||||
|
tdSql.checkRows(2)
|
||||||
|
tdSql.checkData(0, 0, '2023-01-01 00:00:02.000')
|
||||||
|
tdSql.checkData(1, 0, '2023-01-01 00:00:03.000')
|
||||||
|
|
||||||
|
tdSql.checkData(0, 1, 9)
|
||||||
|
tdSql.checkData(1, 1, -11)
|
||||||
|
|
||||||
|
tdSql.query(f"select ts, diff(c1) from (select * from {dbname}.ntb order by ts desc)")
|
||||||
|
tdSql.checkRows(2)
|
||||||
|
tdSql.checkData(0, 0, '2023-01-01 00:00:02.000')
|
||||||
|
tdSql.checkData(1, 0, '2023-01-01 00:00:01.000')
|
||||||
|
|
||||||
|
tdSql.checkData(0, 1, 11)
|
||||||
|
tdSql.checkData(1, 1, -9)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
tdSql.close()
|
tdSql.close()
|
||||||
tdLog.success("%s successfully executed" % __file__)
|
tdLog.success("%s successfully executed" % __file__)
|
||||||
|
|
Loading…
Reference in New Issue