提交 71b85039 编写于 作者: S shenglian zhou 提交者: shenglian zhou

[TD-InfluxDB2.6]<feature>(query):diff ignore negative

上级 789851ac
......@@ -2668,6 +2668,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col
const char* msg14 = "third parameter algorithm must be 'default' or 't-digest'";
const char* msg15 = "parameter is out of range [1, 1000]";
const char* msg16 = "elapsed duration should be greater than or equal to database precision";
const char* msg17 = "the second paramter of diff should be 0 or 1";
switch (functionId) {
case TSDB_FUNC_COUNT: {
......@@ -2766,9 +2767,11 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col
// no parameters or more than one parameter for function
if (pItem->pNode->Expr.paramList == NULL ||
(functionId != TSDB_FUNC_LEASTSQR && functionId != TSDB_FUNC_DERIVATIVE && functionId != TSDB_FUNC_ELAPSED && numOfParams != 1) ||
(functionId != TSDB_FUNC_LEASTSQR && functionId != TSDB_FUNC_DERIVATIVE && functionId != TSDB_FUNC_ELAPSED && functionId != TSDB_FUNC_DIFF
&& numOfParams != 1) ||
((functionId == TSDB_FUNC_LEASTSQR || functionId == TSDB_FUNC_DERIVATIVE) && numOfParams != 3) ||
(functionId == TSDB_FUNC_ELAPSED && numOfParams != 1 && numOfParams != 2)) {
(functionId == TSDB_FUNC_ELAPSED && numOfParams != 1 && numOfParams != 2) ||
(functionId == TSDB_FUNC_DIFF && numOfParams != 1 && numOfParams != 2)) {
return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2);
}
......@@ -2883,6 +2886,21 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col
tscExprAddParams(&pExpr->base, val, TSDB_DATA_TYPE_BIGINT, LONG_BYTES);
}
} else if (functionId == TSDB_FUNC_DIFF) {
char val[8] = {0};
if (numOfParams == 2) {
tVariant* variantDiff = &pParamElem[1].pNode->value;
if (variantDiff->nType != TSDB_DATA_TYPE_BIGINT) {
return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2);
}
tVariantDump(variantDiff, val, TSDB_DATA_TYPE_BIGINT, true);
int64_t ignoreNegative = GET_INT64_VAL(val);
if (ignoreNegative != 0 && ignoreNegative != 1) {
return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg17);
}
}
tscExprAddParams(&pExpr->base, val, TSDB_DATA_TYPE_BIGINT, sizeof(int64_t));
}
SColumnList ids = createColumnList(1, index.tableIndex, index.columnIndex);
......
......@@ -204,6 +204,7 @@ typedef struct SElapsedInfo {
typedef struct {
bool valueAssigned;
bool ignoreNegative;
union {
int64_t i64Prev;
double d64Prev;
......@@ -3025,6 +3026,7 @@ static bool diff_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResIn
SDiffFuncInfo* pDiffInfo = GET_ROWCELL_INTERBUF(pResInfo);
pDiffInfo->valueAssigned = false;
pDiffInfo->i64Prev = 0;
pDiffInfo->ignoreNegative = (pCtx->param[0].i64 == 1) ? true : false;
return true;
}
......@@ -3257,6 +3259,9 @@ static void diff_function(SQLFunctionCtx *pCtx) {
if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) {
continue;
}
if ((pDiffInfo->ignoreNegative) && (pData[i] < 0)) {
continue;
}
if (pDiffInfo->valueAssigned) {
*pOutput = (int32_t)(pData[i] - pDiffInfo->i64Prev); // direct previous may be null
......@@ -3279,6 +3284,9 @@ static void diff_function(SQLFunctionCtx *pCtx) {
if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) {
continue;
}
if ((pDiffInfo->ignoreNegative) && (pData[i] < 0)) {
continue;
}
if (pDiffInfo->valueAssigned) {
*pOutput = pData[i] - pDiffInfo->i64Prev; // direct previous may be null
......@@ -3301,6 +3309,9 @@ static void diff_function(SQLFunctionCtx *pCtx) {
if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) {
continue;
}
if ((pDiffInfo->ignoreNegative) && (pData[i] < 0)) {
continue;
}
if (pDiffInfo->valueAssigned) { // initial value is not set yet
SET_DOUBLE_VAL(pOutput, pData[i] - pDiffInfo->d64Prev); // direct previous may be null
......@@ -3323,6 +3334,9 @@ static void diff_function(SQLFunctionCtx *pCtx) {
if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) {
continue;
}
if ((pDiffInfo->ignoreNegative) && (pData[i] < 0)) {
continue;
}
if (pDiffInfo->valueAssigned) { // initial value is not set yet
*pOutput = (float)(pData[i] - pDiffInfo->d64Prev); // direct previous may be null
......@@ -3345,6 +3359,9 @@ static void diff_function(SQLFunctionCtx *pCtx) {
if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) {
continue;
}
if ((pDiffInfo->ignoreNegative) && (pData[i] < 0)) {
continue;
}
if (pDiffInfo->valueAssigned) { // initial value is not set yet
*pOutput = (int16_t)(pData[i] - pDiffInfo->i64Prev); // direct previous may be null
......@@ -3368,6 +3385,9 @@ static void diff_function(SQLFunctionCtx *pCtx) {
if (pCtx->hasNull && isNull((char *)&pData[i], pCtx->inputType)) {
continue;
}
if ((pDiffInfo->ignoreNegative) && (pData[i] < 0)) {
continue;
}
if (pDiffInfo->valueAssigned) { // initial value is not set yet
*pOutput = (int8_t)(pData[i] - pDiffInfo->i64Prev); // direct previous may be null
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册