From 5344ab10f2400e73be50613a8ceba8bd5cea0d5a Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Tue, 31 May 2022 06:26:46 -0400 Subject: [PATCH 1/3] fix((query): add statecount/stateduration parameter check TD-16160 --- source/libs/function/src/builtins.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/source/libs/function/src/builtins.c b/source/libs/function/src/builtins.c index 4725f11715..798b03362c 100644 --- a/source/libs/function/src/builtins.c +++ b/source/libs/function/src/builtins.c @@ -183,7 +183,7 @@ static int32_t translatePercentile(SFunctionNode* pFunc, char* pErrBuf, int32_t return TSDB_CODE_SUCCESS; } -static bool validAperventileAlgo(const SValueNode* pVal) { +static bool validateAperventileAlgo(const SValueNode* pVal) { if (TSDB_DATA_TYPE_BINARY != pVal->node.resType.type) { return false; } @@ -231,7 +231,7 @@ static int32_t translateApercentile(SFunctionNode* pFunc, char* pErrBuf, int32_t } SNode* pParamNode2 = nodesListGetNode(pFunc->pParameterList, 2); - if (QUERY_NODE_VALUE != nodeType(pParamNode2) || !validAperventileAlgo((SValueNode*)pParamNode2)) { + if (QUERY_NODE_VALUE != nodeType(pParamNode2) || !validateAperventileAlgo((SValueNode*)pParamNode2)) { return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, "Third parameter algorithm of apercentile must be 'default' or 't-digest'"); } @@ -438,6 +438,18 @@ static int32_t translateHLL(SFunctionNode* pFunc, char* pErrBuf, int32_t len) { return TSDB_CODE_SUCCESS; } +static bool validateStateOper(const SValueNode* pVal) { + if (TSDB_DATA_TYPE_BINARY != pVal->node.resType.type) { + return false; + } + return (0 == strcasecmp(varDataVal(pVal->datum.p), "GT") || + 0 == strcasecmp(varDataVal(pVal->datum.p), "GE") || + 0 == strcasecmp(varDataVal(pVal->datum.p), "LT") || + 0 == strcasecmp(varDataVal(pVal->datum.p), "LE") || + 0 == strcasecmp(varDataVal(pVal->datum.p), "EQ") || + 0 == strcasecmp(varDataVal(pVal->datum.p), "NE")); +} + static int32_t translateStateCount(SFunctionNode* pFunc, char* pErrBuf, int32_t len) { int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList); if (3 != numOfParams) { @@ -464,6 +476,12 @@ static int32_t translateStateCount(SFunctionNode* pFunc, char* pErrBuf, int32_t SValueNode* pValue = (SValueNode*)pParamNode; + if (i == 1 && !validateStateOper(pValue)) { + return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, + "Second parameter of STATECOUNT/STATEDURATION function" + "must be one of the following: 'GE', 'GT', 'LE', 'LT', 'EQ', 'NE'"); + } + pValue->notReserved = true; } @@ -504,6 +522,12 @@ static int32_t translateStateDuration(SFunctionNode* pFunc, char* pErrBuf, int32 SValueNode* pValue = (SValueNode*)pParamNode; + if (i == 1 && !validateStateOper(pValue)) { + return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, + "Second parameter of STATECOUNT/STATEDURATION function" + "must be one of the following: 'GE', 'GT', 'LE', 'LT', 'EQ', 'NE'"); + } + pValue->notReserved = true; } From 93a3c271689db0faf0ebdc73330d61e6e9148cdb Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Tue, 31 May 2022 06:44:44 -0400 Subject: [PATCH 2/3] fix(query): restrict stateduration function time unit parameter greater than db precision. TD-16171 --- source/libs/function/src/builtins.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/source/libs/function/src/builtins.c b/source/libs/function/src/builtins.c index 798b03362c..d269e59c1e 100644 --- a/source/libs/function/src/builtins.c +++ b/source/libs/function/src/builtins.c @@ -478,7 +478,7 @@ static int32_t translateStateCount(SFunctionNode* pFunc, char* pErrBuf, int32_t if (i == 1 && !validateStateOper(pValue)) { return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, - "Second parameter of STATECOUNT/STATEDURATION function" + "Second parameter of STATECOUNT function" "must be one of the following: 'GE', 'GT', 'LE', 'LT', 'EQ', 'NE'"); } @@ -524,10 +524,14 @@ static int32_t translateStateDuration(SFunctionNode* pFunc, char* pErrBuf, int32 if (i == 1 && !validateStateOper(pValue)) { return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, - "Second parameter of STATECOUNT/STATEDURATION function" + "Second parameter of STATEDURATION function" "must be one of the following: 'GE', 'GT', 'LE', 'LT', 'EQ', 'NE'"); + } else if (i == 3 && pValue->datum.i == 0) { + return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, + "STATEDURATION function time unit parameter should be greater than db precision"); } + pValue->notReserved = true; } From 11e55389b14dc32769952a59c023d58653026fc5 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Tue, 31 May 2022 06:54:27 -0400 Subject: [PATCH 3/3] fix: fix typo --- source/libs/function/src/builtins.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/libs/function/src/builtins.c b/source/libs/function/src/builtins.c index d269e59c1e..add94cb83c 100644 --- a/source/libs/function/src/builtins.c +++ b/source/libs/function/src/builtins.c @@ -183,7 +183,7 @@ static int32_t translatePercentile(SFunctionNode* pFunc, char* pErrBuf, int32_t return TSDB_CODE_SUCCESS; } -static bool validateAperventileAlgo(const SValueNode* pVal) { +static bool validateApercentileAlgo(const SValueNode* pVal) { if (TSDB_DATA_TYPE_BINARY != pVal->node.resType.type) { return false; } @@ -231,7 +231,7 @@ static int32_t translateApercentile(SFunctionNode* pFunc, char* pErrBuf, int32_t } SNode* pParamNode2 = nodesListGetNode(pFunc->pParameterList, 2); - if (QUERY_NODE_VALUE != nodeType(pParamNode2) || !validateAperventileAlgo((SValueNode*)pParamNode2)) { + if (QUERY_NODE_VALUE != nodeType(pParamNode2) || !validateApercentileAlgo((SValueNode*)pParamNode2)) { return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, "Third parameter algorithm of apercentile must be 'default' or 't-digest'"); }