Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
09c6fd9c
TDengine
项目概览
taosdata
/
TDengine
12 个月 前同步成功
通知
1180
Star
22014
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
09c6fd9c
编写于
7月 04, 2022
作者:
G
Ganlin Zhao
提交者:
GitHub
7月 04, 2022
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #14498 from taosdata/fix/TD-16972
fix(query): add histogram function parameter check
上级
c943710a
a7e222ff
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
199 addition
and
1 deletion
+199
-1
source/libs/function/src/builtins.c
source/libs/function/src/builtins.c
+198
-0
source/libs/function/src/functionMgt.c
source/libs/function/src/functionMgt.c
+1
-1
未找到文件。
source/libs/function/src/builtins.c
浏览文件 @
09c6fd9c
...
...
@@ -18,6 +18,7 @@
#include "querynodes.h"
#include "scalar.h"
#include "taoserror.h"
#include "cJSON.h"
static
int32_t
buildFuncErrMsg
(
char
*
pErrBuf
,
int32_t
len
,
int32_t
errCode
,
const
char
*
pFormat
,
...)
{
va_list
vArgList
;
...
...
@@ -796,6 +797,165 @@ static int32_t translateLeastSQR(SFunctionNode* pFunc, char* pErrBuf, int32_t le
return
TSDB_CODE_SUCCESS
;
}
typedef
enum
{
UNKNOWN_BIN
=
0
,
USER_INPUT_BIN
,
LINEAR_BIN
,
LOG_BIN
}
EHistoBinType
;
static
int8_t
validateHistogramBinType
(
char
*
binTypeStr
)
{
int8_t
binType
;
if
(
strcasecmp
(
binTypeStr
,
"user_input"
)
==
0
)
{
binType
=
USER_INPUT_BIN
;
}
else
if
(
strcasecmp
(
binTypeStr
,
"linear_bin"
)
==
0
)
{
binType
=
LINEAR_BIN
;
}
else
if
(
strcasecmp
(
binTypeStr
,
"log_bin"
)
==
0
)
{
binType
=
LOG_BIN
;
}
else
{
binType
=
UNKNOWN_BIN
;
}
return
binType
;
}
static
bool
validateHistogramBinDesc
(
char
*
binDescStr
,
int8_t
binType
,
char
*
errMsg
,
int32_t
msgLen
)
{
const
char
*
msg1
=
"HISTOGRAM function requires four parameters"
;
const
char
*
msg3
=
"HISTOGRAM function invalid format for binDesc parameter"
;
const
char
*
msg4
=
"HISTOGRAM function binDesc parameter
\"
count
\"
should be in range [1, 1000]"
;
const
char
*
msg5
=
"HISTOGRAM function bin/parameter should be in range [-DBL_MAX, DBL_MAX]"
;
const
char
*
msg6
=
"HISTOGRAM function binDesc parameter
\"
width
\"
cannot be 0"
;
const
char
*
msg7
=
"HISTOGRAM function binDesc parameter
\"
start
\"
cannot be 0 with
\"
log_bin
\"
type"
;
const
char
*
msg8
=
"HISTOGRAM function binDesc parameter
\"
factor
\"
cannot be negative or equal to 0/1"
;
cJSON
*
binDesc
=
cJSON_Parse
(
binDescStr
);
int32_t
numOfBins
;
double
*
intervals
;
if
(
cJSON_IsObject
(
binDesc
))
{
/* linaer/log bins */
int32_t
numOfParams
=
cJSON_GetArraySize
(
binDesc
);
int32_t
startIndex
;
if
(
numOfParams
!=
4
)
{
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg1
);
return
false
;
}
cJSON
*
start
=
cJSON_GetObjectItem
(
binDesc
,
"start"
);
cJSON
*
factor
=
cJSON_GetObjectItem
(
binDesc
,
"factor"
);
cJSON
*
width
=
cJSON_GetObjectItem
(
binDesc
,
"width"
);
cJSON
*
count
=
cJSON_GetObjectItem
(
binDesc
,
"count"
);
cJSON
*
infinity
=
cJSON_GetObjectItem
(
binDesc
,
"infinity"
);
if
(
!
cJSON_IsNumber
(
start
)
||
!
cJSON_IsNumber
(
count
)
||
!
cJSON_IsBool
(
infinity
))
{
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg3
);
return
false
;
}
if
(
count
->
valueint
<=
0
||
count
->
valueint
>
1000
)
{
// limit count to 1000
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg4
);
return
false
;
}
if
(
isinf
(
start
->
valuedouble
)
||
(
width
!=
NULL
&&
isinf
(
width
->
valuedouble
))
||
(
factor
!=
NULL
&&
isinf
(
factor
->
valuedouble
))
||
(
count
!=
NULL
&&
isinf
(
count
->
valuedouble
)))
{
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg5
);
return
false
;
}
int32_t
counter
=
(
int32_t
)
count
->
valueint
;
if
(
infinity
->
valueint
==
false
)
{
startIndex
=
0
;
numOfBins
=
counter
+
1
;
}
else
{
startIndex
=
1
;
numOfBins
=
counter
+
3
;
}
intervals
=
taosMemoryCalloc
(
numOfBins
,
sizeof
(
double
));
if
(
cJSON_IsNumber
(
width
)
&&
factor
==
NULL
&&
binType
==
LINEAR_BIN
)
{
// linear bin process
if
(
width
->
valuedouble
==
0
)
{
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg6
);
taosMemoryFree
(
intervals
);
return
false
;
}
for
(
int
i
=
0
;
i
<
counter
+
1
;
++
i
)
{
intervals
[
startIndex
]
=
start
->
valuedouble
+
i
*
width
->
valuedouble
;
if
(
isinf
(
intervals
[
startIndex
]))
{
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg5
);
taosMemoryFree
(
intervals
);
return
false
;
}
startIndex
++
;
}
}
else
if
(
cJSON_IsNumber
(
factor
)
&&
width
==
NULL
&&
binType
==
LOG_BIN
)
{
// log bin process
if
(
start
->
valuedouble
==
0
)
{
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg7
);
taosMemoryFree
(
intervals
);
return
false
;
}
if
(
factor
->
valuedouble
<
0
||
factor
->
valuedouble
==
0
||
factor
->
valuedouble
==
1
)
{
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg8
);
taosMemoryFree
(
intervals
);
return
false
;
}
for
(
int
i
=
0
;
i
<
counter
+
1
;
++
i
)
{
intervals
[
startIndex
]
=
start
->
valuedouble
*
pow
(
factor
->
valuedouble
,
i
*
1
.
0
);
if
(
isinf
(
intervals
[
startIndex
]))
{
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg5
);
taosMemoryFree
(
intervals
);
return
false
;
}
startIndex
++
;
}
}
else
{
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg3
);
taosMemoryFree
(
intervals
);
return
false
;
}
if
(
infinity
->
valueint
==
true
)
{
intervals
[
0
]
=
-
INFINITY
;
intervals
[
numOfBins
-
1
]
=
INFINITY
;
// in case of desc bin orders, -inf/inf should be swapped
ASSERT
(
numOfBins
>=
4
);
if
(
intervals
[
1
]
>
intervals
[
numOfBins
-
2
])
{
TSWAP
(
intervals
[
0
],
intervals
[
numOfBins
-
1
]);
}
}
}
else
if
(
cJSON_IsArray
(
binDesc
))
{
/* user input bins */
if
(
binType
!=
USER_INPUT_BIN
)
{
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg3
);
return
false
;
}
numOfBins
=
cJSON_GetArraySize
(
binDesc
);
intervals
=
taosMemoryCalloc
(
numOfBins
,
sizeof
(
double
));
cJSON
*
bin
=
binDesc
->
child
;
if
(
bin
==
NULL
)
{
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg3
);
taosMemoryFree
(
intervals
);
return
false
;
}
int
i
=
0
;
while
(
bin
)
{
intervals
[
i
]
=
bin
->
valuedouble
;
if
(
!
cJSON_IsNumber
(
bin
))
{
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg3
);
taosMemoryFree
(
intervals
);
return
false
;
}
if
(
i
!=
0
&&
intervals
[
i
]
<=
intervals
[
i
-
1
])
{
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg3
);
taosMemoryFree
(
intervals
);
return
false
;
}
bin
=
bin
->
next
;
i
++
;
}
}
else
{
snprintf
(
errMsg
,
msgLen
,
"%s"
,
msg3
);
return
false
;
}
taosMemoryFree
(
intervals
);
return
true
;
}
static
int32_t
translateHistogram
(
SFunctionNode
*
pFunc
,
char
*
pErrBuf
,
int32_t
len
)
{
int32_t
numOfParams
=
LIST_LENGTH
(
pFunc
->
pParameterList
);
if
(
4
!=
numOfParams
)
{
...
...
@@ -814,6 +974,8 @@ static int32_t translateHistogram(SFunctionNode* pFunc, char* pErrBuf, int32_t l
return
invaildFuncParaTypeErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
}
int8_t
binType
;
char
*
binDesc
;
for
(
int32_t
i
=
1
;
i
<
numOfParams
;
++
i
)
{
SNode
*
pParamNode
=
nodesListGetNode
(
pFunc
->
pParameterList
,
i
);
if
(
QUERY_NODE_VALUE
!=
nodeType
(
pParamNode
))
{
...
...
@@ -824,6 +986,23 @@ static int32_t translateHistogram(SFunctionNode* pFunc, char* pErrBuf, int32_t l
pValue
->
notReserved
=
true
;
if
(
i
==
1
)
{
binType
=
validateHistogramBinType
(
varDataVal
(
pValue
->
datum
.
p
));
if
(
binType
==
UNKNOWN_BIN
)
{
return
buildFuncErrMsg
(
pErrBuf
,
len
,
TSDB_CODE_FUNC_FUNTION_ERROR
,
"HISTOGRAM function binType parameter should be "
"
\"
user_input
\"
,
\"
log_bin
\"
or
\"
linear_bin
\"
"
);
}
}
if
(
i
==
2
)
{
char
errMsg
[
128
]
=
{
0
};
binDesc
=
varDataVal
(
pValue
->
datum
.
p
);
if
(
!
validateHistogramBinDesc
(
binDesc
,
binType
,
errMsg
,
(
int32_t
)
sizeof
(
errMsg
)))
{
return
buildFuncErrMsg
(
pErrBuf
,
len
,
TSDB_CODE_FUNC_FUNTION_ERROR
,
errMsg
);
}
}
if
(
i
==
3
&&
pValue
->
datum
.
i
!=
1
&&
pValue
->
datum
.
i
!=
0
)
{
return
buildFuncErrMsg
(
pErrBuf
,
len
,
TSDB_CODE_FUNC_FUNTION_ERROR
,
"HISTOGRAM function normalized parameter should be 0/1"
);
...
...
@@ -853,6 +1032,8 @@ static int32_t translateHistogramImpl(SFunctionNode* pFunc, char* pErrBuf, int32
return
invaildFuncParaTypeErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
}
int8_t
binType
;
char
*
binDesc
;
for
(
int32_t
i
=
1
;
i
<
numOfParams
;
++
i
)
{
SNode
*
pParamNode
=
nodesListGetNode
(
pFunc
->
pParameterList
,
i
);
if
(
QUERY_NODE_VALUE
!=
nodeType
(
pParamNode
))
{
...
...
@@ -863,6 +1044,23 @@ static int32_t translateHistogramImpl(SFunctionNode* pFunc, char* pErrBuf, int32
pValue
->
notReserved
=
true
;
if
(
i
==
1
)
{
binType
=
validateHistogramBinType
(
varDataVal
(
pValue
->
datum
.
p
));
if
(
binType
==
UNKNOWN_BIN
)
{
return
buildFuncErrMsg
(
pErrBuf
,
len
,
TSDB_CODE_FUNC_FUNTION_ERROR
,
"HISTOGRAM function binType parameter should be "
"
\"
user_input
\"
,
\"
log_bin
\"
or
\"
linear_bin
\"
"
);
}
}
if
(
i
==
2
)
{
char
errMsg
[
128
]
=
{
0
};
binDesc
=
varDataVal
(
pValue
->
datum
.
p
);
if
(
!
validateHistogramBinDesc
(
binDesc
,
binType
,
errMsg
,
(
int32_t
)
sizeof
(
errMsg
)))
{
return
buildFuncErrMsg
(
pErrBuf
,
len
,
TSDB_CODE_FUNC_FUNTION_ERROR
,
errMsg
);
}
}
if
(
i
==
3
&&
pValue
->
datum
.
i
!=
1
&&
pValue
->
datum
.
i
!=
0
)
{
return
buildFuncErrMsg
(
pErrBuf
,
len
,
TSDB_CODE_FUNC_FUNTION_ERROR
,
"HISTOGRAM function normalized parameter should be 0/1"
);
...
...
source/libs/function/src/functionMgt.c
浏览文件 @
09c6fd9c
...
...
@@ -260,7 +260,7 @@ bool fmIsSameInOutType(int32_t funcId) {
}
static
int32_t
getFuncInfo
(
SFunctionNode
*
pFunc
)
{
char
msg
[
64
]
=
{
0
};
char
msg
[
128
]
=
{
0
};
return
fmGetFuncInfo
(
pFunc
,
msg
,
sizeof
(
msg
));
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录