diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 4d63438c9a5582981b86e41599b6b3a30314c9c9..d5e19950d77c07994ae627da89f90b8b77c0fb0f 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -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); diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index 6b8e31b181559c3d2e92cb52c5b50d4261c66611..b25fb07c3a4e99b8dc60cbc45a93f8d9c211d3ef 100644 --- a/src/query/src/qAggMain.c +++ b/src/query/src/qAggMain.c @@ -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