Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
冷面1992
TDengine
提交
fe229db4
TDengine
项目概览
冷面1992
/
TDengine
与 Fork 源项目一致
Fork自
taosdata / TDengine
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
fe229db4
编写于
12月 11, 2020
作者:
H
haojun Liao
提交者:
GitHub
12月 11, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4531 from taosdata/feature/query
Feature/query
上级
ff1cb186
5721bd12
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
91 addition
and
69 deletion
+91
-69
src/client/src/tscFunctionImpl.c
src/client/src/tscFunctionImpl.c
+76
-66
src/query/inc/tsqlfunction.h
src/query/inc/tsqlfunction.h
+1
-2
tests/script/general/parser/function.sim
tests/script/general/parser/function.sim
+14
-1
未找到文件。
src/client/src/tscFunctionImpl.c
浏览文件 @
fe229db4
...
...
@@ -3648,11 +3648,21 @@ static bool twa_function_setup(SQLFunctionCtx *pCtx) {
SResultRowCellInfo
*
pResInfo
=
GET_RES_INFO
(
pCtx
);
STwaInfo
*
pInfo
=
GET_ROWCELL_INTERBUF
(
pResInfo
);
pInfo
->
lastKey
=
INT64_MIN
;
pInfo
->
p
.
key
=
INT64_MIN
;
pInfo
->
win
=
TSWINDOW_INITIALIZER
;
return
true
;
}
static
double
twa_get_area
(
SPoint1
s
,
SPoint1
e
)
{
if
((
s
.
val
>=
0
&&
e
.
val
>=
0
)
||
(
s
.
val
<=
0
&&
e
.
val
<=
0
))
{
return
(
s
.
val
+
e
.
val
)
*
(
e
.
key
-
s
.
key
)
/
2
;
}
double
x
=
(
s
.
key
*
e
.
val
-
e
.
key
*
s
.
val
)
/
(
e
.
val
-
s
.
val
);
double
val
=
(
s
.
val
*
(
x
-
s
.
key
)
+
e
.
val
*
(
e
.
key
-
x
))
/
2
;
return
val
;
}
static
int32_t
twa_function_impl
(
SQLFunctionCtx
*
pCtx
,
int32_t
tsIndex
,
int32_t
index
,
int32_t
size
)
{
int32_t
notNullElems
=
0
;
TSKEY
*
primaryKey
=
pCtx
->
ptsList
;
...
...
@@ -3663,28 +3673,29 @@ static int32_t twa_function_impl(SQLFunctionCtx* pCtx, int32_t tsIndex, int32_t
int32_t
i
=
index
;
int32_t
step
=
GET_FORWARD_DIRECTION_FACTOR
(
pCtx
->
order
);
SPoint1
*
last
=
&
pInfo
->
p
;
if
(
pCtx
->
start
.
key
!=
INT64_MIN
)
{
assert
((
pCtx
->
start
.
key
<
primaryKey
[
tsIndex
+
i
]
&&
pCtx
->
order
==
TSDB_ORDER_ASC
)
||
(
pCtx
->
start
.
key
>
primaryKey
[
tsIndex
+
i
]
&&
pCtx
->
order
==
TSDB_ORDER_DESC
));
assert
(
pInfo
->
lastK
ey
==
INT64_MIN
);
assert
(
last
->
k
ey
==
INT64_MIN
);
pInfo
->
lastK
ey
=
primaryKey
[
tsIndex
+
i
];
GET_TYPED_DATA
(
pInfo
->
lastValue
,
double
,
pCtx
->
inputType
,
GET_INPUT_CHAR_INDEX
(
pCtx
,
index
));
last
->
k
ey
=
primaryKey
[
tsIndex
+
i
];
GET_TYPED_DATA
(
last
->
val
,
double
,
pCtx
->
inputType
,
GET_INPUT_CHAR_INDEX
(
pCtx
,
index
));
pInfo
->
dOutput
+=
((
pInfo
->
lastValue
+
pCtx
->
start
.
val
)
/
2
)
*
(
pInfo
->
lastKey
-
pCtx
->
start
.
key
);
pInfo
->
dOutput
+=
twa_get_area
(
pCtx
->
start
,
*
last
);
pInfo
->
hasResult
=
DATA_SET_FLAG
;
pInfo
->
win
.
skey
=
pCtx
->
start
.
key
;
notNullElems
++
;
i
+=
step
;
}
else
if
(
pInfo
->
lastK
ey
==
INT64_MIN
)
{
pInfo
->
lastK
ey
=
primaryKey
[
tsIndex
+
i
];
GET_TYPED_DATA
(
pInfo
->
lastValue
,
double
,
pCtx
->
inputType
,
GET_INPUT_CHAR_INDEX
(
pCtx
,
index
));
}
else
if
(
pInfo
->
p
.
k
ey
==
INT64_MIN
)
{
last
->
k
ey
=
primaryKey
[
tsIndex
+
i
];
GET_TYPED_DATA
(
last
->
val
,
double
,
pCtx
->
inputType
,
GET_INPUT_CHAR_INDEX
(
pCtx
,
index
));
pInfo
->
hasResult
=
DATA_SET_FLAG
;
pInfo
->
win
.
skey
=
pInfo
->
lastK
ey
;
pInfo
->
win
.
skey
=
last
->
k
ey
;
notNullElems
++
;
i
+=
step
;
}
...
...
@@ -3698,9 +3709,9 @@ static int32_t twa_function_impl(SQLFunctionCtx* pCtx, int32_t tsIndex, int32_t
continue
;
}
pInfo
->
dOutput
+=
((
val
[
i
]
+
pInfo
->
lastValue
)
/
2
)
*
(
primaryKey
[
i
+
tsIndex
]
-
pInfo
->
lastKey
)
;
pInfo
->
lastValue
=
val
[
i
]
;
pInfo
->
lastKey
=
primaryKey
[
i
+
tsIndex
]
;
SPoint1
st
=
{.
key
=
primaryKey
[
i
+
tsIndex
],
.
val
=
val
[
i
]}
;
pInfo
->
dOutput
+=
twa_get_area
(
pInfo
->
p
,
st
)
;
pInfo
->
p
=
st
;
}
break
;
}
...
...
@@ -3711,9 +3722,9 @@ static int32_t twa_function_impl(SQLFunctionCtx* pCtx, int32_t tsIndex, int32_t
continue
;
}
pInfo
->
dOutput
+=
((
val
[
i
]
+
pInfo
->
lastValue
)
/
2
)
*
(
primaryKey
[
i
+
tsIndex
]
-
pInfo
->
lastKey
)
;
pInfo
->
lastValue
=
val
[
i
]
;
pInfo
->
lastKey
=
primaryKey
[
i
+
tsIndex
]
;
SPoint1
st
=
{.
key
=
primaryKey
[
i
+
tsIndex
],
.
val
=
val
[
i
]}
;
pInfo
->
dOutput
+=
twa_get_area
(
pInfo
->
p
,
st
)
;
pInfo
->
p
=
st
;
}
break
;
}
...
...
@@ -3724,9 +3735,9 @@ static int32_t twa_function_impl(SQLFunctionCtx* pCtx, int32_t tsIndex, int32_t
continue
;
}
pInfo
->
dOutput
+=
((
val
[
i
]
+
pInfo
->
lastValue
)
/
2
)
*
(
primaryKey
[
i
+
tsIndex
]
-
pInfo
->
lastKey
)
;
pInfo
->
lastValue
=
val
[
i
]
;
pInfo
->
lastKey
=
primaryKey
[
i
+
tsIndex
]
;
SPoint1
st
=
{.
key
=
primaryKey
[
i
+
tsIndex
],
.
val
=
val
[
i
]}
;
pInfo
->
dOutput
+=
twa_get_area
(
pInfo
->
p
,
st
)
;
pInfo
->
p
=
st
;
}
break
;
}
...
...
@@ -3737,9 +3748,9 @@ static int32_t twa_function_impl(SQLFunctionCtx* pCtx, int32_t tsIndex, int32_t
continue
;
}
pInfo
->
dOutput
+=
((
val
[
i
]
+
pInfo
->
lastValue
)
/
2
)
*
(
primaryKey
[
i
+
tsIndex
]
-
pInfo
->
lastKey
)
;
pInfo
->
lastValue
=
(
double
)
val
[
i
]
;
pInfo
->
lastKey
=
primaryKey
[
i
+
tsIndex
]
;
SPoint1
st
=
{.
key
=
primaryKey
[
i
+
tsIndex
],
.
val
=
(
double
)
val
[
i
]}
;
pInfo
->
dOutput
+=
twa_get_area
(
pInfo
->
p
,
st
)
;
pInfo
->
p
=
st
;
}
break
;
}
...
...
@@ -3750,9 +3761,9 @@ static int32_t twa_function_impl(SQLFunctionCtx* pCtx, int32_t tsIndex, int32_t
continue
;
}
pInfo
->
dOutput
+=
((
val
[
i
]
+
pInfo
->
lastValue
)
/
2
)
*
(
primaryKey
[
i
+
tsIndex
]
-
pInfo
->
lastKey
)
;
pInfo
->
lastValue
=
val
[
i
]
;
pInfo
->
lastKey
=
primaryKey
[
i
+
tsIndex
]
;
SPoint1
st
=
{.
key
=
primaryKey
[
i
+
tsIndex
],
.
val
=
val
[
i
]}
;
pInfo
->
dOutput
+=
twa_get_area
(
pInfo
->
p
,
st
)
;
pInfo
->
p
=
st
;
}
break
;
}
...
...
@@ -3763,9 +3774,9 @@ static int32_t twa_function_impl(SQLFunctionCtx* pCtx, int32_t tsIndex, int32_t
continue
;
}
pInfo
->
dOutput
+=
((
val
[
i
]
+
pInfo
->
lastValue
)
/
2
)
*
(
primaryKey
[
i
+
tsIndex
]
-
pInfo
->
lastKey
)
;
pInfo
->
lastValue
=
val
[
i
]
;
pInfo
->
lastKey
=
primaryKey
[
i
+
tsIndex
]
;
SPoint1
st
=
{.
key
=
primaryKey
[
i
+
tsIndex
],
.
val
=
val
[
i
]}
;
pInfo
->
dOutput
+=
twa_get_area
(
pInfo
->
p
,
st
)
;
pInfo
->
p
=
st
;
}
break
;
}
...
...
@@ -3774,20 +3785,19 @@ static int32_t twa_function_impl(SQLFunctionCtx* pCtx, int32_t tsIndex, int32_t
// the last interpolated time window value
if
(
pCtx
->
end
.
key
!=
INT64_MIN
)
{
pInfo
->
dOutput
+=
((
pInfo
->
lastValue
+
pCtx
->
end
.
val
)
/
2
)
*
(
pCtx
->
end
.
key
-
pInfo
->
lastKey
);
pInfo
->
lastValue
=
pCtx
->
end
.
val
;
pInfo
->
lastKey
=
pCtx
->
end
.
key
;
pInfo
->
dOutput
+=
twa_get_area
(
pInfo
->
p
,
pCtx
->
end
);
pInfo
->
p
=
pCtx
->
end
;
}
pInfo
->
win
.
ekey
=
pInfo
->
lastK
ey
;
pInfo
->
win
.
ekey
=
pInfo
->
p
.
k
ey
;
return
notNullElems
;
}
static
void
twa_function
(
SQLFunctionCtx
*
pCtx
)
{
void
*
data
=
GET_INPUT_CHAR
(
pCtx
);
void
*
data
=
GET_INPUT_CHAR
(
pCtx
);
SResultRowCellInfo
*
pResInfo
=
GET_RES_INFO
(
pCtx
);
STwaInfo
*
pInfo
=
GET_ROWCELL_INTERBUF
(
pResInfo
);
STwaInfo
*
pInfo
=
GET_ROWCELL_INTERBUF
(
pResInfo
);
// skip null value
int32_t
step
=
GET_FORWARD_DIRECTION_FACTOR
(
pCtx
->
order
);
...
...
@@ -3808,6 +3818,7 @@ static void twa_function(SQLFunctionCtx *pCtx) {
}
}
//TODO refactor
static
void
twa_function_f
(
SQLFunctionCtx
*
pCtx
,
int32_t
index
)
{
void
*
pData
=
GET_INPUT_CHAR_INDEX
(
pCtx
,
index
);
if
(
pCtx
->
hasNull
&&
isNull
(
pData
,
pCtx
->
inputType
))
{
...
...
@@ -3824,23 +3835,23 @@ static void twa_function_f(SQLFunctionCtx *pCtx, int32_t index) {
int32_t
size
=
pCtx
->
size
;
if
(
pCtx
->
start
.
key
!=
INT64_MIN
)
{
assert
(
pInfo
->
lastK
ey
==
INT64_MIN
);
assert
(
pInfo
->
p
.
k
ey
==
INT64_MIN
);
pInfo
->
lastK
ey
=
primaryKey
[
index
];
GET_TYPED_DATA
(
pInfo
->
lastValue
,
double
,
pCtx
->
inputType
,
GET_INPUT_CHAR_INDEX
(
pCtx
,
index
));
pInfo
->
p
.
k
ey
=
primaryKey
[
index
];
GET_TYPED_DATA
(
pInfo
->
p
.
val
,
double
,
pCtx
->
inputType
,
GET_INPUT_CHAR_INDEX
(
pCtx
,
index
));
pInfo
->
dOutput
+=
((
pInfo
->
lastValue
+
pCtx
->
start
.
val
)
/
2
)
*
(
pInfo
->
lastKey
-
pCtx
->
start
.
key
);
pInfo
->
dOutput
+=
twa_get_area
(
pCtx
->
start
,
pInfo
->
p
);
pInfo
->
hasResult
=
DATA_SET_FLAG
;
pInfo
->
win
.
skey
=
pCtx
->
start
.
key
;
notNullElems
++
;
i
+=
1
;
}
else
if
(
pInfo
->
lastK
ey
==
INT64_MIN
)
{
pInfo
->
lastK
ey
=
primaryKey
[
index
];
GET_TYPED_DATA
(
pInfo
->
lastValue
,
double
,
pCtx
->
inputType
,
GET_INPUT_CHAR_INDEX
(
pCtx
,
index
));
}
else
if
(
pInfo
->
p
.
k
ey
==
INT64_MIN
)
{
pInfo
->
p
.
k
ey
=
primaryKey
[
index
];
GET_TYPED_DATA
(
pInfo
->
p
.
val
,
double
,
pCtx
->
inputType
,
GET_INPUT_CHAR_INDEX
(
pCtx
,
index
));
pInfo
->
hasResult
=
DATA_SET_FLAG
;
pInfo
->
win
.
skey
=
pInfo
->
lastK
ey
;
pInfo
->
win
.
skey
=
pInfo
->
p
.
k
ey
;
notNullElems
++
;
i
+=
1
;
}
...
...
@@ -3854,9 +3865,9 @@ static void twa_function_f(SQLFunctionCtx *pCtx, int32_t index) {
continue
;
}
pInfo
->
dOutput
+=
((
val
[
i
]
+
pInfo
->
lastValue
)
/
2
)
*
(
primaryKey
[
i
+
index
]
-
pInfo
->
lastKey
)
;
pInfo
->
lastValue
=
val
[
i
]
;
pInfo
->
lastKey
=
primaryKey
[
i
+
index
]
;
SPoint1
st
=
{.
key
=
primaryKey
[
i
+
index
],
.
val
=
val
[
i
]}
;
pInfo
->
dOutput
+=
twa_get_area
(
pInfo
->
p
,
st
)
;
pInfo
->
p
=
st
;
}
break
;
}
...
...
@@ -3867,9 +3878,9 @@ static void twa_function_f(SQLFunctionCtx *pCtx, int32_t index) {
continue
;
}
pInfo
->
dOutput
+=
((
val
[
i
]
+
pInfo
->
lastValue
)
/
2
)
*
(
primaryKey
[
i
+
index
]
-
pInfo
->
lastKey
)
;
pInfo
->
lastValue
=
val
[
i
]
;
pInfo
->
lastKey
=
primaryKey
[
i
+
index
]
;
SPoint1
st
=
{.
key
=
primaryKey
[
i
+
index
],
.
val
=
val
[
i
]}
;
pInfo
->
dOutput
+=
twa_get_area
(
pInfo
->
p
,
st
)
;
pInfo
->
p
=
st
;
}
break
;
}
...
...
@@ -3880,9 +3891,9 @@ static void twa_function_f(SQLFunctionCtx *pCtx, int32_t index) {
continue
;
}
pInfo
->
dOutput
+=
((
val
[
i
]
+
pInfo
->
lastValue
)
/
2
)
*
(
primaryKey
[
i
+
index
]
-
pInfo
->
lastKey
)
;
pInfo
->
lastValue
=
val
[
i
]
;
pInfo
->
lastKey
=
primaryKey
[
i
+
index
]
;
SPoint1
st
=
{.
key
=
primaryKey
[
i
+
index
],
.
val
=
val
[
i
]}
;
pInfo
->
dOutput
+=
twa_get_area
(
pInfo
->
p
,
st
)
;
pInfo
->
p
=
st
;
}
break
;
}
...
...
@@ -3893,9 +3904,9 @@ static void twa_function_f(SQLFunctionCtx *pCtx, int32_t index) {
continue
;
}
pInfo
->
dOutput
+=
((
val
[
i
]
+
pInfo
->
lastValue
)
/
2
)
*
(
primaryKey
[
i
+
index
]
-
pInfo
->
lastKey
)
;
pInfo
->
lastValue
=
(
double
)
val
[
i
]
;
pInfo
->
lastKey
=
primaryKey
[
i
+
index
]
;
SPoint1
st
=
{.
key
=
primaryKey
[
i
+
index
],
.
val
=
(
double
)
val
[
i
]}
;
pInfo
->
dOutput
+=
twa_get_area
(
pInfo
->
p
,
st
)
;
pInfo
->
p
=
st
;
}
break
;
}
...
...
@@ -3906,9 +3917,9 @@ static void twa_function_f(SQLFunctionCtx *pCtx, int32_t index) {
continue
;
}
pInfo
->
dOutput
+=
((
val
[
i
]
+
pInfo
->
lastValue
)
/
2
)
*
(
primaryKey
[
i
+
index
]
-
pInfo
->
lastKey
)
;
pInfo
->
lastValue
=
val
[
i
]
;
pInfo
->
lastKey
=
primaryKey
[
i
+
index
]
;
SPoint1
st
=
{.
key
=
primaryKey
[
i
+
index
],
.
val
=
val
[
i
]}
;
pInfo
->
dOutput
+=
twa_get_area
(
pInfo
->
p
,
st
);
//((val[i] + pInfo->p.val) / 2) * (primaryKey[i + index] - pInfo->p.key)
;
pInfo
->
p
=
st
;
}
break
;
}
...
...
@@ -3919,9 +3930,9 @@ static void twa_function_f(SQLFunctionCtx *pCtx, int32_t index) {
continue
;
}
pInfo
->
dOutput
+=
((
val
[
i
]
+
pInfo
->
lastValue
)
/
2
)
*
(
primaryKey
[
i
+
index
]
-
pInfo
->
lastKey
)
;
pInfo
->
lastValue
=
val
[
i
]
;
pInfo
->
lastKey
=
primaryKey
[
i
+
index
]
;
SPoint1
st
=
{.
key
=
primaryKey
[
i
+
index
],
.
val
=
val
[
i
]}
;
pInfo
->
dOutput
+=
twa_get_area
(
pInfo
->
p
,
st
);
//((val[i] + pInfo->p.val) / 2) * (primaryKey[i + index] - pInfo->p.key)
;
pInfo
->
p
=
st
;
}
break
;
}
...
...
@@ -3930,12 +3941,11 @@ static void twa_function_f(SQLFunctionCtx *pCtx, int32_t index) {
// the last interpolated time window value
if
(
pCtx
->
end
.
key
!=
INT64_MIN
)
{
pInfo
->
dOutput
+=
((
pInfo
->
lastValue
+
pCtx
->
end
.
val
)
/
2
)
*
(
pCtx
->
end
.
key
-
pInfo
->
lastKey
);
pInfo
->
lastValue
=
pCtx
->
end
.
val
;
pInfo
->
lastKey
=
pCtx
->
end
.
key
;
pInfo
->
dOutput
+=
twa_get_area
(
pInfo
->
p
,
pCtx
->
end
);
//((pInfo->p.val + pCtx->end.val) / 2) * (pCtx->end.key - pInfo->p.key);
pInfo
->
p
=
pCtx
->
end
;
}
pInfo
->
win
.
ekey
=
pInfo
->
lastK
ey
;
pInfo
->
win
.
ekey
=
pInfo
->
p
.
k
ey
;
SET_VAL
(
pCtx
,
notNullElems
,
1
);
...
...
@@ -3966,7 +3976,7 @@ static void twa_func_merge(SQLFunctionCtx *pCtx) {
pBuf
->
dOutput
+=
pInput
->
dOutput
;
pBuf
->
win
=
pInput
->
win
;
pBuf
->
lastKey
=
pInput
->
lastKey
;
pBuf
->
p
=
pInput
->
p
;
}
SET_VAL
(
pCtx
,
numOfNotNull
,
1
);
...
...
@@ -3998,9 +4008,9 @@ void twa_function_finalizer(SQLFunctionCtx *pCtx) {
return
;
}
assert
(
pInfo
->
win
.
ekey
==
pInfo
->
lastK
ey
&&
pInfo
->
hasResult
==
pResInfo
->
hasResult
);
assert
(
pInfo
->
win
.
ekey
==
pInfo
->
p
.
k
ey
&&
pInfo
->
hasResult
==
pResInfo
->
hasResult
);
if
(
pInfo
->
win
.
ekey
==
pInfo
->
win
.
skey
)
{
*
(
double
*
)
pCtx
->
aOutputBuf
=
pInfo
->
lastValue
;
*
(
double
*
)
pCtx
->
aOutputBuf
=
pInfo
->
p
.
val
;
}
else
{
*
(
double
*
)
pCtx
->
aOutputBuf
=
pInfo
->
dOutput
/
(
pInfo
->
win
.
ekey
-
pInfo
->
win
.
skey
);
}
...
...
src/query/inc/tsqlfunction.h
浏览文件 @
fe229db4
...
...
@@ -250,10 +250,9 @@ enum {
};
typedef
struct
STwaInfo
{
TSKEY
lastKey
;
int8_t
hasResult
;
// flag to denote has value
double
dOutput
;
double
lastValue
;
SPoint1
p
;
STimeWindow
win
;
}
STwaInfo
;
...
...
tests/script/general/parser/function.sim
浏览文件 @
fe229db4
...
...
@@ -22,7 +22,7 @@ $db = $dbPrefix . $i
$mt = $mtPrefix . $i
sql drop database if exists $db
sql create database $db
sql create database $db
keep 36500
sql use $db
print =====================================> test case for twa in single block
...
...
@@ -231,3 +231,16 @@ sql select twa(k),avg(k),count(1) from t1 where ts>='2015-8-18 00:00:00' and ts<
#todo add test case while column filter exists.
#sql select count(*),TWA(k) from tm0 where ts>='1970-1-1 13:43:00' and ts<='1970-1-1 13:44:10' interval(9s)
sql create table tm0 (ts timestamp, k float);
sql insert into tm0 values(100000000, 5);
sql insert into tm0 values(100003000, -9);
sql select twa(k) from tm0 where ts<now
if $rows != 1 then
return -1
endi
if $data00 != -2.000000000 then
print expect -2.000000000, actual: $data00
return -1
endi
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录