Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
89e4be84
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
89e4be84
编写于
7月 04, 2022
作者:
G
Ganlin Zhao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(query): add histogram function parameter check
TD-16972
上级
8a76abc3
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
179 addition
and
0 deletion
+179
-0
source/libs/function/src/builtins.c
source/libs/function/src/builtins.c
+179
-0
未找到文件。
source/libs/function/src/builtins.c
浏览文件 @
89e4be84
...
...
@@ -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
[
64
]
=
{
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"
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录