Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
1f88ea55
TDengine
项目概览
taosdata
/
TDengine
大约 2 年 前同步成功
通知
1192
Star
22018
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看板
提交
1f88ea55
编写于
7月 01, 2022
作者:
G
Ganlin Zhao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(quer): fix time unit parameter check
TD-15257
上级
f9d25078
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
191 addition
and
168 deletion
+191
-168
source/libs/function/src/builtins.c
source/libs/function/src/builtins.c
+191
-168
未找到文件。
source/libs/function/src/builtins.c
浏览文件 @
1f88ea55
...
@@ -39,6 +39,174 @@ static int32_t invaildFuncParaValueErrMsg(char* pErrBuf, int32_t len, const char
...
@@ -39,6 +39,174 @@ static int32_t invaildFuncParaValueErrMsg(char* pErrBuf, int32_t len, const char
return
buildFuncErrMsg
(
pErrBuf
,
len
,
TSDB_CODE_FUNC_FUNTION_PARA_VALUE
,
"Invalid parameter value : %s"
,
pFuncName
);
return
buildFuncErrMsg
(
pErrBuf
,
len
,
TSDB_CODE_FUNC_FUNTION_PARA_VALUE
,
"Invalid parameter value : %s"
,
pFuncName
);
}
}
#define TIME_UNIT_INVALID 1
#define TIME_UNIT_TOO_SMALL 2
static
int32_t
validateTimeUnitParam
(
uint8_t
dbPrec
,
const
SValueNode
*
pVal
)
{
if
(
!
pVal
->
isDuration
)
{
return
TIME_UNIT_INVALID
;
}
if
(
TSDB_TIME_PRECISION_MILLI
==
dbPrec
&&
0
==
strcasecmp
(
pVal
->
literal
,
"1u"
))
{
return
TIME_UNIT_TOO_SMALL
;
}
if
(
pVal
->
literal
[
0
]
!=
'1'
||
(
pVal
->
literal
[
1
]
!=
'u'
&&
pVal
->
literal
[
1
]
!=
'a'
&&
pVal
->
literal
[
1
]
!=
's'
&&
pVal
->
literal
[
1
]
!=
'm'
&&
pVal
->
literal
[
1
]
!=
'h'
&&
pVal
->
literal
[
1
]
!=
'd'
&&
pVal
->
literal
[
1
]
!=
'w'
))
{
return
TIME_UNIT_INVALID
;
}
return
TSDB_CODE_SUCCESS
;
}
/* Following are valid ISO-8601 timezone format:
* 1 z/Z
* 2 ±hh:mm
* 3 ±hhmm
* 4 ±hh
*
*/
static
bool
validateHourRange
(
int8_t
hour
)
{
if
(
hour
<
0
||
hour
>
12
)
{
return
false
;
}
return
true
;
}
static
bool
validateMinuteRange
(
int8_t
hour
,
int8_t
minute
,
char
sign
)
{
if
(
minute
==
0
||
(
minute
==
30
&&
(
hour
==
3
||
hour
==
5
)
&&
sign
==
'+'
))
{
return
true
;
}
return
false
;
}
static
bool
validateTimestampDigits
(
const
SValueNode
*
pVal
)
{
if
(
!
IS_INTEGER_TYPE
(
pVal
->
node
.
resType
.
type
))
{
return
false
;
}
int64_t
tsVal
=
pVal
->
datum
.
i
;
char
fraction
[
20
]
=
{
0
};
NUM_TO_STRING
(
pVal
->
node
.
resType
.
type
,
&
tsVal
,
sizeof
(
fraction
),
fraction
);
int32_t
tsDigits
=
(
int32_t
)
strlen
(
fraction
);
if
(
tsDigits
>
TSDB_TIME_PRECISION_SEC_DIGITS
)
{
if
(
tsDigits
==
TSDB_TIME_PRECISION_MILLI_DIGITS
||
tsDigits
==
TSDB_TIME_PRECISION_MICRO_DIGITS
||
tsDigits
==
TSDB_TIME_PRECISION_NANO_DIGITS
)
{
return
true
;
}
else
{
return
false
;
}
}
return
true
;
}
static
bool
validateTimezoneFormat
(
const
SValueNode
*
pVal
)
{
if
(
TSDB_DATA_TYPE_BINARY
!=
pVal
->
node
.
resType
.
type
)
{
return
false
;
}
char
*
tz
=
varDataVal
(
pVal
->
datum
.
p
);
int32_t
len
=
varDataLen
(
pVal
->
datum
.
p
);
char
buf
[
3
]
=
{
0
};
int8_t
hour
=
-
1
,
minute
=
-
1
;
if
(
len
==
0
)
{
return
false
;
}
else
if
(
len
==
1
&&
(
tz
[
0
]
==
'z'
||
tz
[
0
]
==
'Z'
))
{
return
true
;
}
else
if
((
tz
[
0
]
==
'+'
||
tz
[
0
]
==
'-'
))
{
switch
(
len
)
{
case
3
:
case
5
:
{
for
(
int32_t
i
=
1
;
i
<
len
;
++
i
)
{
if
(
!
isdigit
(
tz
[
i
]))
{
return
false
;
}
if
(
i
==
2
)
{
memcpy
(
buf
,
&
tz
[
i
-
1
],
2
);
hour
=
taosStr2Int8
(
buf
,
NULL
,
10
);
if
(
!
validateHourRange
(
hour
))
{
return
false
;
}
}
else
if
(
i
==
4
)
{
memcpy
(
buf
,
&
tz
[
i
-
1
],
2
);
minute
=
taosStr2Int8
(
buf
,
NULL
,
10
);
if
(
!
validateMinuteRange
(
hour
,
minute
,
tz
[
0
]))
{
return
false
;
}
}
}
break
;
}
case
6
:
{
for
(
int32_t
i
=
1
;
i
<
len
;
++
i
)
{
if
(
i
==
3
)
{
if
(
tz
[
i
]
!=
':'
)
{
return
false
;
}
continue
;
}
if
(
!
isdigit
(
tz
[
i
]))
{
return
false
;
}
if
(
i
==
2
)
{
memcpy
(
buf
,
&
tz
[
i
-
1
],
2
);
hour
=
taosStr2Int8
(
buf
,
NULL
,
10
);
if
(
!
validateHourRange
(
hour
))
{
return
false
;
}
}
else
if
(
i
==
5
)
{
memcpy
(
buf
,
&
tz
[
i
-
1
],
2
);
minute
=
taosStr2Int8
(
buf
,
NULL
,
10
);
if
(
!
validateMinuteRange
(
hour
,
minute
,
tz
[
0
]))
{
return
false
;
}
}
}
break
;
}
default:
{
return
false
;
}
}
}
else
{
return
false
;
}
return
true
;
}
void
static
addTimezoneParam
(
SNodeList
*
pList
)
{
char
buf
[
6
]
=
{
0
};
time_t
t
=
taosTime
(
NULL
);
struct
tm
*
tmInfo
=
taosLocalTime
(
&
t
,
NULL
);
strftime
(
buf
,
sizeof
(
buf
),
"%z"
,
tmInfo
);
int32_t
len
=
(
int32_t
)
strlen
(
buf
);
SValueNode
*
pVal
=
(
SValueNode
*
)
nodesMakeNode
(
QUERY_NODE_VALUE
);
pVal
->
literal
=
strndup
(
buf
,
len
);
pVal
->
isDuration
=
false
;
pVal
->
translate
=
true
;
pVal
->
node
.
resType
.
type
=
TSDB_DATA_TYPE_BINARY
;
pVal
->
node
.
resType
.
bytes
=
len
+
VARSTR_HEADER_SIZE
;
pVal
->
node
.
resType
.
precision
=
TSDB_TIME_PRECISION_MILLI
;
pVal
->
datum
.
p
=
taosMemoryCalloc
(
1
,
len
+
VARSTR_HEADER_SIZE
+
1
);
varDataSetLen
(
pVal
->
datum
.
p
,
len
);
strncpy
(
varDataVal
(
pVal
->
datum
.
p
),
pVal
->
literal
,
len
);
nodesListAppend
(
pList
,
(
SNode
*
)
pVal
);
}
void
static
addDbPrecisonParam
(
SNodeList
**
pList
,
uint8_t
precision
)
{
void
static
addDbPrecisonParam
(
SNodeList
**
pList
,
uint8_t
precision
)
{
SValueNode
*
pVal
=
(
SValueNode
*
)
nodesMakeNode
(
QUERY_NODE_VALUE
);
SValueNode
*
pVal
=
(
SValueNode
*
)
nodesMakeNode
(
QUERY_NODE_VALUE
);
pVal
->
literal
=
NULL
;
pVal
->
literal
=
NULL
;
...
@@ -833,6 +1001,19 @@ static int32_t translateStateDuration(SFunctionNode* pFunc, char* pErrBuf, int32
...
@@ -833,6 +1001,19 @@ static int32_t translateStateDuration(SFunctionNode* pFunc, char* pErrBuf, int32
return
invaildFuncParaTypeErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
return
invaildFuncParaTypeErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
}
}
if
(
numOfParams
==
4
)
{
uint8_t
dbPrec
=
pFunc
->
node
.
resType
.
precision
;
int32_t
ret
=
validateTimeUnitParam
(
dbPrec
,
(
SValueNode
*
)
nodesListGetNode
(
pFunc
->
pParameterList
,
3
));
if
(
ret
==
TIME_UNIT_TOO_SMALL
)
{
return
buildFuncErrMsg
(
pErrBuf
,
len
,
TSDB_CODE_FUNC_FUNTION_ERROR
,
"STATEDURATION function time unit parameter should be greater than db precision"
);
}
else
if
(
ret
==
TIME_UNIT_INVALID
)
{
return
buildFuncErrMsg
(
pErrBuf
,
len
,
TSDB_CODE_FUNC_FUNTION_ERROR
,
"STATEDURATION function time unit parameter should be one of the following: [1u, 1a, 1s, 1m, 1h, 1d, 1w]"
);
}
}
// set result type
// set result type
pFunc
->
node
.
resType
=
(
SDataType
){.
bytes
=
tDataTypes
[
TSDB_DATA_TYPE_BIGINT
].
bytes
,
.
type
=
TSDB_DATA_TYPE_BIGINT
};
pFunc
->
node
.
resType
=
(
SDataType
){.
bytes
=
tDataTypes
[
TSDB_DATA_TYPE_BIGINT
].
bytes
,
.
type
=
TSDB_DATA_TYPE_BIGINT
};
return
TSDB_CODE_SUCCESS
;
return
TSDB_CODE_SUCCESS
;
...
@@ -1285,174 +1466,6 @@ static int32_t translateCast(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
...
@@ -1285,174 +1466,6 @@ static int32_t translateCast(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
return
TSDB_CODE_SUCCESS
;
return
TSDB_CODE_SUCCESS
;
}
}
#define TIME_UNIT_INVALID 1
#define TIME_UNIT_TOO_SMALL 2
static
int32_t
validateTimeUnitParam
(
uint8_t
dbPrec
,
const
SValueNode
*
pVal
)
{
if
(
!
pVal
->
isDuration
)
{
return
TIME_UNIT_INVALID
;
}
if
(
TSDB_TIME_PRECISION_MILLI
==
dbPrec
&&
0
==
strcasecmp
(
pVal
->
literal
,
"1u"
))
{
return
TIME_UNIT_TOO_SMALL
;
}
if
(
pVal
->
literal
[
0
]
!=
'1'
||
(
pVal
->
literal
[
1
]
!=
'u'
&&
pVal
->
literal
[
1
]
!=
'a'
&&
pVal
->
literal
[
1
]
!=
's'
&&
pVal
->
literal
[
1
]
!=
'm'
&&
pVal
->
literal
[
1
]
!=
'h'
&&
pVal
->
literal
[
1
]
!=
'd'
&&
pVal
->
literal
[
1
]
!=
'w'
))
{
return
TIME_UNIT_INVALID
;
}
return
TSDB_CODE_SUCCESS
;
}
/* Following are valid ISO-8601 timezone format:
* 1 z/Z
* 2 ±hh:mm
* 3 ±hhmm
* 4 ±hh
*
*/
static
bool
validateHourRange
(
int8_t
hour
)
{
if
(
hour
<
0
||
hour
>
12
)
{
return
false
;
}
return
true
;
}
static
bool
validateMinuteRange
(
int8_t
hour
,
int8_t
minute
,
char
sign
)
{
if
(
minute
==
0
||
(
minute
==
30
&&
(
hour
==
3
||
hour
==
5
)
&&
sign
==
'+'
))
{
return
true
;
}
return
false
;
}
static
bool
validateTimestampDigits
(
const
SValueNode
*
pVal
)
{
if
(
!
IS_INTEGER_TYPE
(
pVal
->
node
.
resType
.
type
))
{
return
false
;
}
int64_t
tsVal
=
pVal
->
datum
.
i
;
char
fraction
[
20
]
=
{
0
};
NUM_TO_STRING
(
pVal
->
node
.
resType
.
type
,
&
tsVal
,
sizeof
(
fraction
),
fraction
);
int32_t
tsDigits
=
(
int32_t
)
strlen
(
fraction
);
if
(
tsDigits
>
TSDB_TIME_PRECISION_SEC_DIGITS
)
{
if
(
tsDigits
==
TSDB_TIME_PRECISION_MILLI_DIGITS
||
tsDigits
==
TSDB_TIME_PRECISION_MICRO_DIGITS
||
tsDigits
==
TSDB_TIME_PRECISION_NANO_DIGITS
)
{
return
true
;
}
else
{
return
false
;
}
}
return
true
;
}
static
bool
validateTimezoneFormat
(
const
SValueNode
*
pVal
)
{
if
(
TSDB_DATA_TYPE_BINARY
!=
pVal
->
node
.
resType
.
type
)
{
return
false
;
}
char
*
tz
=
varDataVal
(
pVal
->
datum
.
p
);
int32_t
len
=
varDataLen
(
pVal
->
datum
.
p
);
char
buf
[
3
]
=
{
0
};
int8_t
hour
=
-
1
,
minute
=
-
1
;
if
(
len
==
0
)
{
return
false
;
}
else
if
(
len
==
1
&&
(
tz
[
0
]
==
'z'
||
tz
[
0
]
==
'Z'
))
{
return
true
;
}
else
if
((
tz
[
0
]
==
'+'
||
tz
[
0
]
==
'-'
))
{
switch
(
len
)
{
case
3
:
case
5
:
{
for
(
int32_t
i
=
1
;
i
<
len
;
++
i
)
{
if
(
!
isdigit
(
tz
[
i
]))
{
return
false
;
}
if
(
i
==
2
)
{
memcpy
(
buf
,
&
tz
[
i
-
1
],
2
);
hour
=
taosStr2Int8
(
buf
,
NULL
,
10
);
if
(
!
validateHourRange
(
hour
))
{
return
false
;
}
}
else
if
(
i
==
4
)
{
memcpy
(
buf
,
&
tz
[
i
-
1
],
2
);
minute
=
taosStr2Int8
(
buf
,
NULL
,
10
);
if
(
!
validateMinuteRange
(
hour
,
minute
,
tz
[
0
]))
{
return
false
;
}
}
}
break
;
}
case
6
:
{
for
(
int32_t
i
=
1
;
i
<
len
;
++
i
)
{
if
(
i
==
3
)
{
if
(
tz
[
i
]
!=
':'
)
{
return
false
;
}
continue
;
}
if
(
!
isdigit
(
tz
[
i
]))
{
return
false
;
}
if
(
i
==
2
)
{
memcpy
(
buf
,
&
tz
[
i
-
1
],
2
);
hour
=
taosStr2Int8
(
buf
,
NULL
,
10
);
if
(
!
validateHourRange
(
hour
))
{
return
false
;
}
}
else
if
(
i
==
5
)
{
memcpy
(
buf
,
&
tz
[
i
-
1
],
2
);
minute
=
taosStr2Int8
(
buf
,
NULL
,
10
);
if
(
!
validateMinuteRange
(
hour
,
minute
,
tz
[
0
]))
{
return
false
;
}
}
}
break
;
}
default:
{
return
false
;
}
}
}
else
{
return
false
;
}
return
true
;
}
void
static
addTimezoneParam
(
SNodeList
*
pList
)
{
char
buf
[
6
]
=
{
0
};
time_t
t
=
taosTime
(
NULL
);
struct
tm
*
tmInfo
=
taosLocalTime
(
&
t
,
NULL
);
strftime
(
buf
,
sizeof
(
buf
),
"%z"
,
tmInfo
);
int32_t
len
=
(
int32_t
)
strlen
(
buf
);
SValueNode
*
pVal
=
(
SValueNode
*
)
nodesMakeNode
(
QUERY_NODE_VALUE
);
pVal
->
literal
=
strndup
(
buf
,
len
);
pVal
->
isDuration
=
false
;
pVal
->
translate
=
true
;
pVal
->
node
.
resType
.
type
=
TSDB_DATA_TYPE_BINARY
;
pVal
->
node
.
resType
.
bytes
=
len
+
VARSTR_HEADER_SIZE
;
pVal
->
node
.
resType
.
precision
=
TSDB_TIME_PRECISION_MILLI
;
pVal
->
datum
.
p
=
taosMemoryCalloc
(
1
,
len
+
VARSTR_HEADER_SIZE
+
1
);
varDataSetLen
(
pVal
->
datum
.
p
,
len
);
strncpy
(
varDataVal
(
pVal
->
datum
.
p
),
pVal
->
literal
,
len
);
nodesListAppend
(
pList
,
(
SNode
*
)
pVal
);
}
static
int32_t
translateToIso8601
(
SFunctionNode
*
pFunc
,
char
*
pErrBuf
,
int32_t
len
)
{
static
int32_t
translateToIso8601
(
SFunctionNode
*
pFunc
,
char
*
pErrBuf
,
int32_t
len
)
{
int32_t
numOfParams
=
LIST_LENGTH
(
pFunc
->
pParameterList
);
int32_t
numOfParams
=
LIST_LENGTH
(
pFunc
->
pParameterList
);
if
(
1
!=
numOfParams
&&
2
!=
numOfParams
)
{
if
(
1
!=
numOfParams
&&
2
!=
numOfParams
)
{
...
@@ -1521,6 +1534,16 @@ static int32_t translateTimeTruncate(SFunctionNode* pFunc, char* pErrBuf, int32_
...
@@ -1521,6 +1534,16 @@ static int32_t translateTimeTruncate(SFunctionNode* pFunc, char* pErrBuf, int32_
//add database precision as param
//add database precision as param
uint8_t
dbPrec
=
pFunc
->
node
.
resType
.
precision
;
uint8_t
dbPrec
=
pFunc
->
node
.
resType
.
precision
;
int32_t
ret
=
validateTimeUnitParam
(
dbPrec
,
(
SValueNode
*
)
nodesListGetNode
(
pFunc
->
pParameterList
,
1
));
if
(
ret
==
TIME_UNIT_TOO_SMALL
)
{
return
buildFuncErrMsg
(
pErrBuf
,
len
,
TSDB_CODE_FUNC_FUNTION_ERROR
,
"TIMETRUNCATE function time unit parameter should be greater than db precision"
);
}
else
if
(
ret
==
TIME_UNIT_INVALID
)
{
return
buildFuncErrMsg
(
pErrBuf
,
len
,
TSDB_CODE_FUNC_FUNTION_ERROR
,
"TIMETRUNCATE function time unit parameter should be one of the following: [1u, 1a, 1s, 1m, 1h, 1d, 1w]"
);
}
addDbPrecisonParam
(
&
pFunc
->
pParameterList
,
dbPrec
);
addDbPrecisonParam
(
&
pFunc
->
pParameterList
,
dbPrec
);
pFunc
->
node
.
resType
=
pFunc
->
node
.
resType
=
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录