Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
8aea80f5
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
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看板
未验证
提交
8aea80f5
编写于
4月 22, 2022
作者:
W
wade zhang
提交者:
GitHub
4月 22, 2022
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #11751 from taosdata/docs/wade-220422-udf
docs: replace code with #include
上级
f24e1792
f1da77b1
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
3 addition
and
237 deletion
+3
-237
docs-cn/08-advance-feature/04-udf.md
docs-cn/08-advance-feature/04-udf.md
+3
-237
未找到文件。
docs-cn/08-advance-feature/04-udf.md
浏览文件 @
8aea80f5
...
...
@@ -179,37 +179,7 @@ SELECT X(c) FROM table/stable;
<summary>
add_one.c
</summary>
```
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef
struct
SUdfInit
{
int
maybe_null
;
/* 1 if function can return NULL */
int
decimals
;
/* for real functions */
long
long
length
;
/* For string functions */
char
*
ptr
;
/* free pointer for function data */
int
const_item
;
/* 0 if result is independent of arguments */
}
SUdfInit
;
void
add_one
(
char
*
data
,
short
itype
,
short
ibytes
,
int
numOfRows
,
long
long
*
ts
,
char
*
dataOutput
,
char
*
interBUf
,
char
*
tsOutput
,
int
*
numOfOutput
,
short
otype
,
short
obytes
,
SUdfInit
*
buf
)
{
int
i
;
int
r
=
0
;
// printf("add_one input data:%p, type:%d, rows:%d, ts:%p,%lld, dataoutput:%p, tsOutput:%p, numOfOutput:%p, buf:%p\n", data, itype, numOfRows, ts, *ts, dataOutput, tsOutput, numOfOutput, buf);
if
(
itype
==
4
)
{
for
(
i
=
0
;
i
<
numOfRows
;
++
i
)
{
// printf("input %d - %d", i, *((int *)data + i));
*
((
int
*
)
dataOutput
+
i
)
=*
((
int
*
)
data
+
i
)
+
1
;
// printf(", output %d\n", *((int *)dataOutput+i));
if
(
tsOutput
)
{
*
(
long
long
*
)
tsOutput
=
1000000
;
}
}
*
numOfOutput
=
numOfRows
;
// printf("add_one out, numOfOutput:%d\n", *numOfOutput);
}
}
{{
#
include
tests
/
script
/
sh
/
add_one
.
c
}}
```
</details>
...
...
@@ -220,101 +190,7 @@ void add_one(char* data, short itype, short ibytes, int numOfRows, long long* ts
<summary>
abs_max.c
</summary>
```
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
typedef
struct
SUdfInit
{
int
maybe_null
;
/* 1 if function can return NULL */
int
decimals
;
/* for real functions */
int64_t
length
;
/* For string functions */
char
*
ptr
;
/* free pointer for function data */
int
const_item
;
/* 0 if result is independent of arguments */
}
SUdfInit
;
#define TSDB_DATA_INT_NULL 0x80000000L
#define TSDB_DATA_BIGINT_NULL 0x8000000000000000L
void
abs_max
(
char
*
data
,
short
itype
,
short
ibytes
,
int
numOfRows
,
int64_t
*
ts
,
char
*
dataOutput
,
char
*
interBuf
,
char
*
tsOutput
,
int
*
numOfOutput
,
short
otype
,
short
obytes
,
SUdfInit
*
buf
)
{
int
i
;
int64_t
r
=
0
;
// printf("abs_max input data:%p, type:%d, rows:%d, ts:%p, %" PRId64 ", dataoutput:%p, tsOutput:%p, numOfOutput:%p, buf:%p\n", data, itype, numOfRows, ts, *ts, dataOutput, tsOutput, numOfOutput, buf);
if
(
itype
==
5
)
{
r
=*
(
int64_t
*
)
dataOutput
;
*
numOfOutput
=
0
;
for
(
i
=
0
;
i
<
numOfRows
;
++
i
)
{
if
(
*
((
int64_t
*
)
data
+
i
)
==
TSDB_DATA_BIGINT_NULL
)
{
continue
;
}
*
numOfOutput
=
1
;
//int64_t v = abs(*((int64_t *)data + i));
int64_t
v
=
*
((
int64_t
*
)
data
+
i
);
if
(
v
<
0
)
{
v
=
0
-
v
;
}
if
(
v
>
r
)
{
r
=
v
;
}
}
*
(
int64_t
*
)
dataOutput
=
r
;
// printf("abs_max out, dataoutput:%" PRId64", numOfOutput:%d\n", *(int64_t *)dataOutput, *numOfOutput);
}
else
{
*
numOfOutput
=
0
;
}
}
void
abs_max_finalize
(
char
*
dataOutput
,
char
*
interBuf
,
int
*
numOfOutput
,
SUdfInit
*
buf
)
{
int
i
;
//int64_t r = 0;
// printf("abs_max_finalize dataoutput:%p:%d, numOfOutput:%d, buf:%p\n", dataOutput, *dataOutput, *numOfOutput, buf);
// *numOfOutput=1;
// printf("abs_max finalize, dataoutput:%" PRId64", numOfOutput:%d\n", *(int64_t *)dataOutput, *numOfOutput);
}
void
abs_max_merge
(
char
*
data
,
int32_t
numOfRows
,
char
*
dataOutput
,
int32_t
*
numOfOutput
,
SUdfInit
*
buf
)
{
int64_t
r
=
0
;
if
(
numOfRows
>
0
)
{
r
=
*
((
int64_t
*
)
data
);
}
// printf("abs_max_merge numOfRows:%d, dataoutput:%p, buf:%p\n", numOfRows, dataOutput, buf);
for
(
int
i
=
1
;
i
<
numOfRows
;
++
i
)
{
// printf("abs_max_merge %d - %" PRId64"\n", i, *((int64_t *)data + i));
if
(
*
((
int64_t
*
)
data
+
i
)
>
r
)
{
r
=
*
((
int64_t
*
)
data
+
i
);
}
}
*
(
int64_t
*
)
dataOutput
=
r
;
if
(
numOfRows
>
0
)
{
*
numOfOutput
=
1
;
}
else
{
*
numOfOutput
=
0
;
}
// printf("abs_max_merge, dataoutput:%" PRId64", numOfOutput:%d\n", *(int64_t *)dataOutput, *numOfOutput);
}
int
abs_max_init
(
SUdfInit
*
buf
)
{
// printf("abs_max init\n");
return
0
;
}
void
abs_max_destroy
(
SUdfInit
*
buf
)
{
// printf("abs_max destroy\n");
}
{{
#
include
tests
/
script
/
sh
/
abs_max
.
c
}}
```
</details>
...
...
@@ -325,117 +201,7 @@ void abs_max_destroy(SUdfInit* buf) {
<summary>
demo.c
</summary>
```
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef
struct
SUdfInit
{
int
maybe_null
;
/* 1 if function can return NULL */
int
decimals
;
/* for real functions */
long
long
length
;
/* For string functions */
char
*
ptr
;
/* free pointer for function data */
int
const_item
;
/* 0 if result is independent of arguments */
}
SUdfInit
;
typedef
struct
SDemo
{
double
sum
;
int
num
;
short
otype
;
}
SDemo
;
#define FLOAT_NULL 0x7FF00000 // it is an NAN
#define DOUBLE_NULL 0x7FFFFF0000000000L // it is an NAN
void
demo
(
char
*
data
,
short
itype
,
short
ibytes
,
int
numOfRows
,
long
long
*
ts
,
char
*
dataOutput
,
char
*
interBuf
,
char
*
tsOutput
,
int
*
numOfOutput
,
short
otype
,
short
obytes
,
SUdfInit
*
buf
)
{
int
i
;
double
r
=
0
;
SDemo
*
p
=
(
SDemo
*
)
interBuf
;
SDemo
*
q
=
(
SDemo
*
)
dataOutput
;
printf
(
"demo input data:%p, type:%d, rows:%d, ts:%p,%lld, dataoutput:%p, interBUf:%p, tsOutput:%p, numOfOutput:%p, buf:%p
\n
"
,
data
,
itype
,
numOfRows
,
ts
,
*
ts
,
dataOutput
,
interBuf
,
tsOutput
,
numOfOutput
,
buf
);
for
(
i
=
0
;
i
<
numOfRows
;
++
i
)
{
if
(
itype
==
4
)
{
r
=*
((
int
*
)
data
+
i
);
}
else
if
(
itype
==
6
)
{
r
=*
((
float
*
)
data
+
i
);
}
else
if
(
itype
==
7
)
{
r
=*
((
double
*
)
data
+
i
);
}
p
->
sum
+=
r
*
r
;
}
p
->
otype
=
otype
;
p
->
num
+=
numOfRows
;
q
->
sum
=
p
->
sum
;
q
->
num
=
p
->
num
;
q
->
otype
=
p
->
otype
;
*
numOfOutput
=
1
;
printf
(
"demo out, sum:%f, num:%d, numOfOutput:%d
\n
"
,
p
->
sum
,
p
->
num
,
*
numOfOutput
);
}
void
demo_merge
(
char
*
data
,
int32_t
numOfRows
,
char
*
dataOutput
,
int32_t
*
numOfOutput
,
SUdfInit
*
buf
)
{
int
i
;
SDemo
*
p
=
(
SDemo
*
)
data
;
SDemo
res
=
{
0
};
printf
(
"demo_merge input data:%p, rows:%d, dataoutput:%p, numOfOutput:%p, buf:%p
\n
"
,
data
,
numOfRows
,
dataOutput
,
numOfOutput
,
buf
);
for
(
i
=
0
;
i
<
numOfRows
;
++
i
)
{
res
.
sum
+=
p
->
sum
*
p
->
sum
;
res
.
num
+=
p
->
num
;
p
++
;
}
p
->
sum
=
res
.
sum
;
p
->
num
=
res
.
num
;
*
numOfOutput
=
1
;
printf
(
"demo out, sum:%f, num:%d, numOfOutput:%d
\n
"
,
p
->
sum
,
p
->
num
,
*
numOfOutput
);
}
void
demo_finalize
(
char
*
dataOutput
,
char
*
interBuf
,
int
*
numOfOutput
,
SUdfInit
*
buf
)
{
SDemo
*
p
=
(
SDemo
*
)
interBuf
;
printf
(
"demo_finalize interbuf:%p, numOfOutput:%p, buf:%p, sum:%f, num:%d
\n
"
,
interBuf
,
numOfOutput
,
buf
,
p
->
sum
,
p
->
num
);
if
(
p
->
otype
==
6
)
{
if
(
p
->
num
!=
30000
)
{
*
(
unsigned
int
*
)
dataOutput
=
FLOAT_NULL
;
}
else
{
*
(
float
*
)
dataOutput
=
(
float
)(
p
->
sum
/
p
->
num
);
}
printf
(
"finalize values:%f
\n
"
,
*
(
float
*
)
dataOutput
);
}
else
if
(
p
->
otype
==
7
)
{
if
(
p
->
num
!=
30000
)
{
*
(
unsigned
long
long
*
)
dataOutput
=
DOUBLE_NULL
;
}
else
{
*
(
double
*
)
dataOutput
=
(
double
)(
p
->
sum
/
p
->
num
);
}
printf
(
"finalize values:%f
\n
"
,
*
(
double
*
)
dataOutput
);
}
*
numOfOutput
=
1
;
printf
(
"demo finalize, numOfOutput:%d
\n
"
,
*
numOfOutput
);
}
int
demo_init
(
SUdfInit
*
buf
)
{
printf
(
"demo init
\n
"
);
return
0
;
}
void
demo_destroy
(
SUdfInit
*
buf
)
{
printf
(
"demo destroy
\n
"
);
}
{{
#
include
tests
/
script
/
sh
/
demo
.
c
}}
```
</details>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录