Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
慢慢CG
TDengine
提交
8155c9fb
T
TDengine
项目概览
慢慢CG
/
TDengine
与 Fork 源项目一致
Fork自
taosdata / TDengine
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
8155c9fb
编写于
5月 08, 2021
作者:
Y
yihaoDeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[TD-3980] support lua script
上级
148a73fe
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
79 addition
and
18 deletion
+79
-18
src/query/inc/qScript.h
src/query/inc/qScript.h
+3
-2
src/query/inc/qUdf.h
src/query/inc/qUdf.h
+2
-2
src/query/src/qExecutor.c
src/query/src/qExecutor.c
+9
-3
src/query/src/qScript.c
src/query/src/qScript.c
+36
-4
tests/script/sh/demo.lua
tests/script/sh/demo.lua
+29
-7
未找到文件。
src/query/inc/qScript.h
浏览文件 @
8155c9fb
...
@@ -60,8 +60,9 @@ typedef struct ScriptCtx {
...
@@ -60,8 +60,9 @@ typedef struct ScriptCtx {
int
taosLoadScriptInit
(
void
*
pInit
);
int
taosLoadScriptInit
(
void
*
pInit
);
void
taosLoadScriptNormal
(
void
*
pInit
,
char
*
pInput
,
int16_t
iType
,
int16_t
iBytes
,
int32_t
numOfRows
,
void
taosLoadScriptNormal
(
void
*
pInit
,
char
*
pInput
,
int16_t
iType
,
int16_t
iBytes
,
int32_t
numOfRows
,
int64_t
*
ptsList
,
char
*
pOutput
,
char
*
ptsOutput
,
int32_t
*
numOfOutput
,
int16_t
oType
,
int16_t
oBytes
);
int64_t
*
ptsList
,
int64_t
key
,
char
*
pOutput
,
char
*
ptsOutput
,
int32_t
*
numOfOutput
,
int16_t
oType
,
int16_t
oBytes
);
void
taosLoadScriptFinalize
(
void
*
pInit
,
char
*
pOutput
,
int32_t
*
output
);
void
taosLoadScriptFinalize
(
void
*
pInit
,
int64_t
key
,
char
*
pOutput
,
int32_t
*
output
);
void
taosLoadScriptMerge
(
void
*
pCtx
,
char
*
data
,
int32_t
numOfRows
,
char
*
dataOutput
,
int32_t
*
numOfOutput
);
void
taosLoadScriptDestroy
(
void
*
pInit
);
void
taosLoadScriptDestroy
(
void
*
pInit
);
typedef
struct
{
typedef
struct
{
...
...
src/query/inc/qUdf.h
浏览文件 @
8155c9fb
...
@@ -57,8 +57,8 @@ typedef struct SUdfInfo {
...
@@ -57,8 +57,8 @@ typedef struct SUdfInfo {
typedef
int32_t
(
*
scriptInitFunc
)(
void
*
pCtx
);
typedef
int32_t
(
*
scriptInitFunc
)(
void
*
pCtx
);
typedef
void
(
*
scriptNormalFunc
)(
void
*
pCtx
,
char
*
data
,
int16_t
iType
,
int16_t
iBytes
,
int32_t
numOfRows
,
typedef
void
(
*
scriptNormalFunc
)(
void
*
pCtx
,
char
*
data
,
int16_t
iType
,
int16_t
iBytes
,
int32_t
numOfRows
,
int64_t
*
ts
,
char
*
dataOutput
,
char
*
tsOutput
,
int32_t
*
numOfOutput
,
int16_t
oType
,
int16_t
oBytes
);
int64_t
*
ptList
,
int64_t
key
,
char
*
dataOutput
,
char
*
tsOutput
,
int32_t
*
numOfOutput
,
int16_t
oType
,
int16_t
oBytes
);
typedef
void
(
*
scriptFinalizeFunc
)(
void
*
pCtx
,
char
*
dataOutput
,
int32_t
*
numOfOutput
);
typedef
void
(
*
scriptFinalizeFunc
)(
void
*
pCtx
,
int64_t
key
,
char
*
dataOutput
,
int32_t
*
numOfOutput
);
typedef
void
(
*
scriptMergeFunc
)(
void
*
pCtx
,
char
*
data
,
int32_t
numOfRows
,
char
*
dataOutput
,
int32_t
*
numOfOutput
);
typedef
void
(
*
scriptMergeFunc
)(
void
*
pCtx
,
char
*
data
,
int32_t
numOfRows
,
char
*
dataOutput
,
int32_t
*
numOfOutput
);
typedef
void
(
*
scriptDestroyFunc
)(
void
*
pCtx
);
typedef
void
(
*
scriptDestroyFunc
)(
void
*
pCtx
);
...
...
src/query/src/qExecutor.c
浏览文件 @
8155c9fb
...
@@ -715,7 +715,7 @@ void doInvokeUdf(SUdfInfo* pUdfInfo, SQLFunctionCtx *pCtx, int32_t idx, int32_t
...
@@ -715,7 +715,7 @@ void doInvokeUdf(SUdfInfo* pUdfInfo, SQLFunctionCtx *pCtx, int32_t idx, int32_t
case
TSDB_UDF_FUNC_NORMAL
:
case
TSDB_UDF_FUNC_NORMAL
:
if
(
pUdfInfo
->
isScript
)
{
if
(
pUdfInfo
->
isScript
)
{
(
*
(
scriptNormalFunc
)
pUdfInfo
->
funcs
[
TSDB_UDF_FUNC_NORMAL
])(
pUdfInfo
->
pScriptCtx
,
(
*
(
scriptNormalFunc
)
pUdfInfo
->
funcs
[
TSDB_UDF_FUNC_NORMAL
])(
pUdfInfo
->
pScriptCtx
,
(
char
*
)
pCtx
->
pInput
+
idx
*
pCtx
->
inputType
,
pCtx
->
inputType
,
pCtx
->
inputBytes
,
pCtx
->
size
,
pCtx
->
ptsList
,
pCtx
->
pOutput
,
(
char
*
)
pCtx
->
pInput
+
idx
*
pCtx
->
inputType
,
pCtx
->
inputType
,
pCtx
->
inputBytes
,
pCtx
->
size
,
pCtx
->
ptsList
,
pCtx
->
startTs
,
pCtx
->
pOutput
,
(
char
*
)
pCtx
->
ptsOutputBuf
,
&
output
,
pCtx
->
outputType
,
pCtx
->
outputBytes
);
(
char
*
)
pCtx
->
ptsOutputBuf
,
&
output
,
pCtx
->
outputType
,
pCtx
->
outputBytes
);
}
else
{
}
else
{
SResultRowCellInfo
*
pResInfo
=
GET_RES_INFO
(
pCtx
);
SResultRowCellInfo
*
pResInfo
=
GET_RES_INFO
(
pCtx
);
...
@@ -739,7 +739,11 @@ void doInvokeUdf(SUdfInfo* pUdfInfo, SQLFunctionCtx *pCtx, int32_t idx, int32_t
...
@@ -739,7 +739,11 @@ void doInvokeUdf(SUdfInfo* pUdfInfo, SQLFunctionCtx *pCtx, int32_t idx, int32_t
break
;
break
;
case
TSDB_UDF_FUNC_MERGE
:
case
TSDB_UDF_FUNC_MERGE
:
(
*
(
udfMergeFunc
)
pUdfInfo
->
funcs
[
TSDB_UDF_FUNC_MERGE
])(
pCtx
->
pInput
,
pCtx
->
size
,
pCtx
->
pOutput
,
&
output
,
&
pUdfInfo
->
init
);
if
(
pUdfInfo
->
isScript
)
{
(
*
(
scriptMergeFunc
)
pUdfInfo
->
funcs
[
TSDB_UDF_FUNC_MERGE
])(
pUdfInfo
->
pScriptCtx
,
pCtx
->
pInput
,
pCtx
->
size
,
pCtx
->
pOutput
,
&
output
);
}
else
{
(
*
(
udfMergeFunc
)
pUdfInfo
->
funcs
[
TSDB_UDF_FUNC_MERGE
])(
pCtx
->
pInput
,
pCtx
->
size
,
pCtx
->
pOutput
,
&
output
,
&
pUdfInfo
->
init
);
}
// set the output value exist
// set the output value exist
pCtx
->
resultInfo
->
numOfRes
=
output
;
pCtx
->
resultInfo
->
numOfRes
=
output
;
...
@@ -753,7 +757,7 @@ void doInvokeUdf(SUdfInfo* pUdfInfo, SQLFunctionCtx *pCtx, int32_t idx, int32_t
...
@@ -753,7 +757,7 @@ void doInvokeUdf(SUdfInfo* pUdfInfo, SQLFunctionCtx *pCtx, int32_t idx, int32_t
SResultRowCellInfo
*
pResInfo
=
GET_RES_INFO
(
pCtx
);
SResultRowCellInfo
*
pResInfo
=
GET_RES_INFO
(
pCtx
);
void
*
interBuf
=
(
void
*
)
GET_ROWCELL_INTERBUF
(
pResInfo
);
void
*
interBuf
=
(
void
*
)
GET_ROWCELL_INTERBUF
(
pResInfo
);
if
(
pUdfInfo
->
isScript
)
{
if
(
pUdfInfo
->
isScript
)
{
(
*
(
scriptFinalizeFunc
)
pUdfInfo
->
funcs
[
TSDB_UDF_FUNC_FINALIZE
])(
pUdfInfo
->
pScriptCtx
,
pCtx
->
pOutput
,
&
output
);
(
*
(
scriptFinalizeFunc
)
pUdfInfo
->
funcs
[
TSDB_UDF_FUNC_FINALIZE
])(
pUdfInfo
->
pScriptCtx
,
pCtx
->
startTs
,
pCtx
->
pOutput
,
&
output
);
}
else
{
}
else
{
(
*
(
udfFinalizeFunc
)
pUdfInfo
->
funcs
[
TSDB_UDF_FUNC_FINALIZE
])(
pCtx
->
pOutput
,
interBuf
,
&
output
,
&
pUdfInfo
->
init
);
(
*
(
udfFinalizeFunc
)
pUdfInfo
->
funcs
[
TSDB_UDF_FUNC_FINALIZE
])(
pCtx
->
pOutput
,
interBuf
,
&
output
,
&
pUdfInfo
->
init
);
}
}
...
@@ -3279,6 +3283,7 @@ void finalizeQueryResult(SOperatorInfo* pOperator, SQLFunctionCtx* pCtx, SResult
...
@@ -3279,6 +3283,7 @@ void finalizeQueryResult(SOperatorInfo* pOperator, SQLFunctionCtx* pCtx, SResult
setResultOutputBuf
(
pRuntimeEnv
,
buf
,
pCtx
,
numOfOutput
,
rowCellInfoOffset
);
setResultOutputBuf
(
pRuntimeEnv
,
buf
,
pCtx
,
numOfOutput
,
rowCellInfoOffset
);
for
(
int32_t
j
=
0
;
j
<
numOfOutput
;
++
j
)
{
for
(
int32_t
j
=
0
;
j
<
numOfOutput
;
++
j
)
{
pCtx
[
j
].
startTs
=
buf
->
win
.
skey
;
if
(
pCtx
[
j
].
functionId
<
0
)
{
if
(
pCtx
[
j
].
functionId
<
0
)
{
doInvokeUdf
(
pRuntimeEnv
->
pUdfInfo
,
&
pCtx
[
j
],
0
,
TSDB_UDF_FUNC_FINALIZE
);
doInvokeUdf
(
pRuntimeEnv
->
pUdfInfo
,
&
pCtx
[
j
],
0
,
TSDB_UDF_FUNC_FINALIZE
);
}
else
{
}
else
{
...
@@ -6676,6 +6681,7 @@ int32_t initUdfInfo(SUdfInfo* pUdfInfo) {
...
@@ -6676,6 +6681,7 @@ int32_t initUdfInfo(SUdfInfo* pUdfInfo) {
if
(
pUdfInfo
->
funcType
==
TSDB_UDF_TYPE_AGGREGATE
)
{
if
(
pUdfInfo
->
funcType
==
TSDB_UDF_TYPE_AGGREGATE
)
{
pUdfInfo
->
funcs
[
TSDB_UDF_FUNC_FINALIZE
]
=
taosLoadScriptFinalize
;
pUdfInfo
->
funcs
[
TSDB_UDF_FUNC_FINALIZE
]
=
taosLoadScriptFinalize
;
pUdfInfo
->
funcs
[
TSDB_UDF_FUNC_MERGE
]
=
taosLoadScriptMerge
;
}
}
pUdfInfo
->
funcs
[
TSDB_UDF_FUNC_DESTROY
]
=
taosLoadScriptDestroy
;
pUdfInfo
->
funcs
[
TSDB_UDF_FUNC_DESTROY
]
=
taosLoadScriptDestroy
;
...
...
src/query/src/qScript.c
浏览文件 @
8155c9fb
...
@@ -103,7 +103,7 @@ int taosLoadScriptInit(void* pInit) {
...
@@ -103,7 +103,7 @@ int taosLoadScriptInit(void* pInit) {
return
0
;
return
0
;
}
}
void
taosLoadScriptNormal
(
void
*
pInit
,
char
*
pInput
,
int16_t
iType
,
int16_t
iBytes
,
int32_t
numOfRows
,
void
taosLoadScriptNormal
(
void
*
pInit
,
char
*
pInput
,
int16_t
iType
,
int16_t
iBytes
,
int32_t
numOfRows
,
int64_t
*
ptsList
,
char
*
pOutput
,
char
*
ptsOutput
,
int32_t
*
numOfOutput
,
int16_t
oType
,
int16_t
oBytes
)
{
int64_t
*
ptsList
,
int64_t
key
,
char
*
pOutput
,
char
*
ptsOutput
,
int32_t
*
numOfOutput
,
int16_t
oType
,
int16_t
oBytes
)
{
ScriptCtx
*
pCtx
=
pInit
;
ScriptCtx
*
pCtx
=
pInit
;
char
funcName
[
MAX_FUNC_NAME
]
=
{
0
};
char
funcName
[
MAX_FUNC_NAME
]
=
{
0
};
sprintf
(
funcName
,
"%s_add"
,
pCtx
->
funcName
);
sprintf
(
funcName
,
"%s_add"
,
pCtx
->
funcName
);
...
@@ -124,8 +124,9 @@ void taosLoadScriptNormal(void *pInit, char *pInput, int16_t iType, int16_t iByt
...
@@ -124,8 +124,9 @@ void taosLoadScriptNormal(void *pInit, char *pInput, int16_t iType, int16_t iByt
if
(
lua_istable
(
lua
,
-
1
))
{
if
(
lua_istable
(
lua
,
-
1
))
{
isGlobalState
=
true
;
isGlobalState
=
true
;
}
}
lua_pushnumber
(
lua
,
key
);
// do call lua script
// do call lua script
if
(
lua_pcall
(
lua
,
2
,
1
,
0
)
!=
0
)
{
if
(
lua_pcall
(
lua
,
3
,
1
,
0
)
!=
0
)
{
qError
(
"SCRIPT ERROR: %s"
,
lua_tostring
(
lua
,
-
1
));
qError
(
"SCRIPT ERROR: %s"
,
lua_tostring
(
lua
,
-
1
));
lua_pop
(
lua
,
-
1
);
lua_pop
(
lua
,
-
1
);
return
;
return
;
...
@@ -139,22 +140,51 @@ void taosLoadScriptNormal(void *pInit, char *pInput, int16_t iType, int16_t iByt
...
@@ -139,22 +140,51 @@ void taosLoadScriptNormal(void *pInit, char *pInput, int16_t iType, int16_t iByt
*
numOfOutput
=
tNumOfOutput
;
*
numOfOutput
=
tNumOfOutput
;
}
}
void
taosLoadScriptMerge
(
void
*
pInit
,
char
*
data
,
int32_t
numOfRows
,
char
*
pOutput
,
int32_t
*
numOfOutput
)
{
ScriptCtx
*
pCtx
=
pInit
;
char
funcName
[
MAX_FUNC_NAME
]
=
{
0
};
sprintf
(
funcName
,
"%s_merge"
,
pCtx
->
funcName
);
lua_State
*
lua
=
pCtx
->
pEnv
->
lua_state
;
lua_getglobal
(
lua
,
funcName
);
if
(
!
lua_isfunction
(
lua
,
-
1
))
{
qError
(
"SCRIPT ERROR: %s"
,
lua_tostring
(
lua
,
-
1
));
return
;
}
lua_getglobal
(
lua
,
"global"
);
if
(
lua_pcall
(
lua
,
1
,
1
,
0
)
!=
0
)
{
qError
(
"SCRIPT ERROR: %s"
,
lua_tostring
(
lua
,
-
1
));
lua_pop
(
lua
,
-
1
);
return
;
}
int
tNumOfOutput
=
0
;
luaValueToTaosType
(
lua
,
pOutput
,
&
tNumOfOutput
,
pCtx
->
resType
,
pCtx
->
resBytes
);
*
numOfOutput
=
tNumOfOutput
;
}
//do not support agg now
//do not support agg now
void
taosLoadScriptFinalize
(
void
*
pInit
,
char
*
pOutput
,
int32_t
*
numOfOutput
)
{
void
taosLoadScriptFinalize
(
void
*
pInit
,
int64_t
key
,
char
*
pOutput
,
int32_t
*
numOfOutput
)
{
ScriptCtx
*
pCtx
=
pInit
;
ScriptCtx
*
pCtx
=
pInit
;
char
funcName
[
MAX_FUNC_NAME
]
=
{
0
};
char
funcName
[
MAX_FUNC_NAME
]
=
{
0
};
sprintf
(
funcName
,
"%s_finalize"
,
pCtx
->
funcName
);
sprintf
(
funcName
,
"%s_finalize"
,
pCtx
->
funcName
);
lua_State
*
lua
=
pCtx
->
pEnv
->
lua_state
;
lua_State
*
lua
=
pCtx
->
pEnv
->
lua_state
;
lua_getglobal
(
lua
,
funcName
);
lua_getglobal
(
lua
,
funcName
);
if
(
!
lua_isfunction
(
lua
,
-
1
))
{
qError
(
"SCRIPT ERROR: %s"
,
lua_tostring
(
lua
,
-
1
));
return
;
}
lua_getglobal
(
lua
,
"global"
);
lua_getglobal
(
lua
,
"global"
);
if
(
lua_pcall
(
lua
,
1
,
1
,
0
)
!=
0
)
{
lua_pushnumber
(
lua
,
key
);
if
(
lua_pcall
(
lua
,
2
,
2
,
0
)
!=
0
)
{
qError
(
"SCRIPT ERROR: %s"
,
lua_tostring
(
lua
,
-
1
));
qError
(
"SCRIPT ERROR: %s"
,
lua_tostring
(
lua
,
-
1
));
lua_pop
(
lua
,
-
1
);
lua_pop
(
lua
,
-
1
);
return
;
return
;
}
}
lua_setglobal
(
lua
,
"global"
);
int
tNumOfOutput
=
0
;
int
tNumOfOutput
=
0
;
luaValueToTaosType
(
lua
,
pOutput
,
&
tNumOfOutput
,
pCtx
->
resType
,
pCtx
->
resBytes
);
luaValueToTaosType
(
lua
,
pOutput
,
&
tNumOfOutput
,
pCtx
->
resType
,
pCtx
->
resBytes
);
*
numOfOutput
=
tNumOfOutput
;
*
numOfOutput
=
tNumOfOutput
;
...
@@ -256,6 +286,8 @@ void luaValueToTaosType(lua_State *lua, char *interBuf, int32_t *numOfOutput, in
...
@@ -256,6 +286,8 @@ void luaValueToTaosType(lua_State *lua, char *interBuf, int32_t *numOfOutput, in
}
}
break
;
break
;
default:
default:
setNull
(
interBuf
,
oType
,
oBytes
);
sz
=
1
;
break
;
break
;
}
}
lua_pop
(
lua
,
1
);
// pop ret value from script
lua_pop
(
lua
,
1
);
// pop ret value from script
...
...
tests/script/sh/demo.lua
浏览文件 @
8155c9fb
funcName
=
"test"
funcName
=
"test"
global
=
{}
global
=
{}
global
[
"sum"
]
=
0
.
0
global
[
"num"
]
=
0
function
test_init
()
function
test_init
()
return
global
return
global
end
end
function
test_add
(
rows
,
ans
)
function
test_add
(
rows
,
ans
,
key
)
t
=
{}
t
[
"sum"
]
=
0
.
0
t
[
"num"
]
=
0
for
i
=
1
,
#
rows
do
for
i
=
1
,
#
rows
do
ans
[
"sum"
]
=
ans
[
"sum"
]
+
rows
[
i
]
*
rows
[
i
]
t
[
"sum"
]
=
t
[
"sum"
]
+
rows
[
i
]
*
rows
[
i
]
end
end
ans
[
"num"
]
=
ans
[
"num"
]
+
#
rows
t
[
"num"
]
=
#
rows
if
(
ans
[
key
]
~=
nil
)
then
ans
[
key
][
"sum"
]
=
ans
[
key
][
"sum"
]
+
t
[
"sum"
]
ans
[
key
][
"num"
]
=
ans
[
key
][
"num"
]
+
t
[
"num"
]
else
ans
[
key
]
=
t
end
return
ans
;
return
ans
;
end
end
function
test_finalize
(
ans
)
function
test_finalize
(
ans
,
key
)
return
ans
[
"sum"
]
/
ans
[
"num"
];
local
ret
=
0
.
0
if
(
ans
[
key
]
~=
nil
)
then
ret
=
ans
[
key
][
"sum"
]
/
ans
[
key
][
"num"
]
ans
[
key
][
"sum"
]
=
0
.
0
ans
[
key
][
"num"
]
=
0
else
ret
=
inf
end
return
ret
,
ans
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录