Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
ffc9558e
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看板
提交
ffc9558e
编写于
9月 10, 2021
作者:
A
AlexDuan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
put code in order
上级
0b92dee8
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
42 addition
and
58 deletion
+42
-58
src/query/inc/tdigest.h
src/query/inc/tdigest.h
+28
-10
src/query/src/qAggMain.c
src/query/src/qAggMain.c
+0
-8
src/query/src/tdigest.c
src/query/src/tdigest.c
+14
-40
未找到文件。
src/query/inc/tdigest.h
浏览文件 @
ffc9558e
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* include/tdigest.c
*
...
...
@@ -7,17 +22,19 @@
#ifndef TDIGEST_H
#define TDIGEST_H
#define DEFAULT_COMPRESSION 400
#define COMPRESSION 400
#define GET_CENTROID(compression) (ceil(compression * M_PI / 2) + 1)
#define GET_THRESHOLD(compression) (7.5 + 0.37 * compression - 2e-4 * pow(compression, 2))
#define TDIGEST_SIZE(compression) (sizeof(TDigest) + sizeof(Centroid)*GET_CENTROID(compression) + sizeof(Point)*GET_THRESHOLD(compression))
typedef
struct
Centroid
{
long
long
weight
;
double
mean
;
long
long
weight
;
}
Centroid
;
typedef
struct
Point
{
double
value
;
long
long
weight
;
struct
Point
*
next
;
}
Point
;
typedef
struct
TDigest
{
...
...
@@ -36,12 +53,13 @@ typedef struct TDigest {
Centroid
*
centroids
;
}
TDigest
;
extern
struct
TDigest
*
tdigestNew
(
int
compression
);
extern
void
tdigestAdd
(
struct
TDigest
*
t
,
double
x
,
long
long
w
);
extern
void
tdigestMerge
(
struct
TDigest
*
t1
,
struct
TDigest
*
t2
);
extern
double
tdigestCDF
(
struct
TDigest
*
t
,
double
x
);
extern
double
tdigestQuantile
(
struct
TDigest
*
t
,
double
q
);
extern
void
tdigestCompress
(
struct
TDigest
*
t
);
extern
void
tdigestFree
(
struct
TDigest
*
t
);
TDigest
*
tdigestNewFrom
(
void
*
pBuf
,
int
compression
);
void
tdigestAdd
(
TDigest
*
t
,
double
x
,
long
long
w
);
void
tdigestMerge
(
TDigest
*
t1
,
TDigest
*
t2
);
double
tdigestCDF
(
TDigest
*
t
,
double
x
);
double
tdigestQuantile
(
TDigest
*
t
,
double
q
);
void
tdigestCompress
(
TDigest
*
t
);
void
tdigestFreeFrom
(
TDigest
*
t
);
void
tdigestAutoFill
(
TDigest
*
t
,
int
compression
);
#endif
/* TDIGEST_H */
src/query/src/qAggMain.c
浏览文件 @
ffc9558e
...
...
@@ -2442,7 +2442,6 @@ static SAPercentileInfo *getAPerctInfo(SQLFunctionCtx *pCtx) {
return
pInfo
;
}
//
// ----------------- tdigest -------------------
//
...
...
@@ -2477,7 +2476,6 @@ static void tdigest_do(SQLFunctionCtx *pCtx) {
if
(
pCtx
->
hasNull
&&
isNull
(
data
,
pCtx
->
inputType
))
{
continue
;
}
notNullElems
+=
1
;
double
v
=
0
;
// value
...
...
@@ -2486,14 +2484,11 @@ static void tdigest_do(SQLFunctionCtx *pCtx) {
tdigestAdd
(
pAPerc
->
pTDigest
,
v
,
w
);
}
//tdigestCompress(pAPerc->pTDigest);
if
(
!
pCtx
->
hasNull
)
{
assert
(
pCtx
->
size
==
notNullElems
);
}
SET_VAL
(
pCtx
,
notNullElems
,
1
);
if
(
notNullElems
>
0
)
{
pResInfo
->
hasResult
=
DATA_SET_FLAG
;
}
...
...
@@ -2548,9 +2543,7 @@ static void tdigest_finalizer(SQLFunctionCtx *pCtx) {
}
}
tdigestFreeFrom
(
pAPerc
->
pTDigest
);
pAPerc
->
pTDigest
=
NULL
;
doFinalizer
(
pCtx
);
}
...
...
@@ -2653,7 +2646,6 @@ static void apercentile_func_merge(SQLFunctionCtx *pCtx) {
SET_VAL
(
pCtx
,
1
,
1
);
}
static
void
apercentile_finalizer
(
SQLFunctionCtx
*
pCtx
)
{
if
(
getAlgo
(
pCtx
)
==
ALGO_TDIGEST
)
{
tdigest_finalizer
(
pCtx
);
...
...
src/query/src/tdigest.c
浏览文件 @
ffc9558e
...
...
@@ -31,7 +31,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tdigest.h"
#define INTERPOLATE(x, x0, x1) (((x) - (x0)) / ((x1) - (x0)))
...
...
@@ -47,24 +46,21 @@
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
typedef
struct
MergeArgs
{
TDigest
*
t
;
Centroid
*
centroids
;
int
idx
;
double
weight_so_far
;
double
k1
;
double
min
;
double
max
;
}
MergeArgs
;
void
tdigestAutoFill
(
TDigest
*
t
,
int
compression
)
{
t
->
centroids
=
(
Centroid
*
)((
char
*
)
t
+
sizeof
(
TDigest
));
t
->
buffered_pts
=
(
Point
*
)
((
char
*
)
t
+
sizeof
(
TDigest
)
+
sizeof
(
Centroid
)
*
(
int
)
GET_CENTROID
(
compression
));
}
TDigest
*
tdigestNew
(
int
compression
)
{
TDigest
*
t
=
malloc
(
sizeof
(
TDigest
));
memset
(
t
,
0
,
sizeof
(
TDigest
));
t
->
compression
=
compression
;
t
->
size
=
GET_CENTROID
(
compression
);
t
->
threshold
=
GET_THRESHOLD
(
compression
);
t
->
min
=
INFINITY
;
return
t
;
}
TDigest
*
tdigestNewFrom
(
void
*
pBuf
,
int
compression
)
{
memset
(
pBuf
,
0
,
sizeof
(
TDigest
)
+
sizeof
(
Centroid
)
*
(
compression
+
1
));
TDigest
*
t
=
(
TDigest
*
)
pBuf
;
...
...
@@ -87,16 +83,6 @@ static int centroid_cmp(const void *a, const void *b) {
return
0
;
}
typedef
struct
MergeArgs
{
TDigest
*
t
;
Centroid
*
centroids
;
int
idx
;
double
weight_so_far
;
double
k1
;
double
min
;
double
max
;
}
MergeArgs
;
static
void
merge_centroid
(
MergeArgs
*
args
,
Centroid
*
merge
)
{
double
k2
;
Centroid
*
c
=
&
args
->
centroids
[
args
->
idx
];
...
...
@@ -168,7 +154,6 @@ void tdigestCompress(TDigest *t) {
while
(
i
<
num_unmerged
)
merge_centroid
(
&
args
,
&
unmerged_centroids
[
i
++
]);
free
(
unmerged_centroids
);
while
(
j
<
t
->
num_centroids
)
...
...
@@ -244,7 +229,7 @@ double tdigestCDF(TDigest *t, double x) {
if
(
x
<
a
->
mean
+
right
)
{
double
cdf
=
(
weight_so_far
+
a
->
weight
*
INTERPOLATE
(
x
,
a
->
mean
-
left
,
a
->
mean
+
right
))
*
INTERPOLATE
(
x
,
a
->
mean
-
left
,
a
->
mean
+
right
))
/
t
->
total_weight
;
return
MAX
(
cdf
,
0
.
0
);
}
...
...
@@ -256,10 +241,10 @@ double tdigestCDF(TDigest *t, double x) {
a
=
b
;
right
=
t
->
max
-
a
->
mean
;
if
(
x
<
a
->
mean
+
right
)
return
(
weight_so_far
+
a
->
weight
*
INTERPOLATE
(
x
,
a
->
mean
-
left
,
a
->
mean
+
right
))
if
(
x
<
a
->
mean
+
right
)
{
return
(
weight_so_far
+
a
->
weight
*
INTERPOLATE
(
x
,
a
->
mean
-
left
,
a
->
mean
+
right
))
/
t
->
total_weight
;
}
return
1
;
}
...
...
@@ -301,14 +286,11 @@ double tdigestQuantile(TDigest *t, double q) {
left
=
right
;
b
=
c
;
right
=
(
b
->
weight
*
a
->
mean
+
a
->
weight
*
b
->
mean
)
/
(
a
->
weight
+
b
->
weight
);
right
=
(
b
->
weight
*
a
->
mean
+
a
->
weight
*
b
->
mean
)
/
(
a
->
weight
+
b
->
weight
);
if
(
idx
<
weight_so_far
+
a
->
weight
)
{
double
p
=
(
idx
-
weight_so_far
)
/
a
->
weight
;
return
left
*
(
1
-
p
)
+
right
*
p
;
}
weight_so_far
+=
a
->
weight
;
}
...
...
@@ -336,12 +318,4 @@ void tdigestMerge(TDigest *t1, TDigest *t2) {
for
(
int
i
=
0
;
i
<
t2
->
num_centroids
;
i
++
)
{
tdigestAdd
(
t1
,
t2
->
centroids
[
i
].
mean
,
t2
->
centroids
[
i
].
weight
);
}
}
void
tdigestFree
(
TDigest
*
t
)
{
free
(
t
);
}
void
tdigestFreeFrom
(
TDigest
*
t
)
{
// nothing to do
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录