Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
011bd323
T
TDengine
项目概览
taosdata
/
TDengine
大约 1 年 前同步成功
通知
1181
Star
22014
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
011bd323
编写于
5月 23, 2022
作者:
G
Ganlin Zhao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(query): add apercentile function
上级
c62aabca
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
149 addition
and
6 deletion
+149
-6
source/libs/function/inc/builtinsimpl.h
source/libs/function/inc/builtinsimpl.h
+5
-0
source/libs/function/src/builtins.c
source/libs/function/src/builtins.c
+4
-4
source/libs/function/src/builtinsimpl.c
source/libs/function/src/builtinsimpl.c
+140
-2
未找到文件。
source/libs/function/inc/builtinsimpl.h
浏览文件 @
011bd323
...
...
@@ -67,6 +67,11 @@ bool percentileFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResultI
int32_t
percentileFunction
(
SqlFunctionCtx
*
pCtx
);
int32_t
percentileFinalize
(
SqlFunctionCtx
*
pCtx
,
SSDataBlock
*
pBlock
);
bool
getApercentileFuncEnv
(
struct
SFunctionNode
*
pFunc
,
SFuncExecEnv
*
pEnv
);
bool
apercentileFunctionSetup
(
SqlFunctionCtx
*
pCtx
,
SResultRowEntryInfo
*
pResultInfo
);
int32_t
apercentileFunction
(
SqlFunctionCtx
*
pCtx
);
int32_t
apercentileFinalize
(
SqlFunctionCtx
*
pCtx
,
SSDataBlock
*
pBlock
);
bool
getDiffFuncEnv
(
struct
SFunctionNode
*
pFunc
,
SFuncExecEnv
*
pEnv
);
bool
diffFunctionSetup
(
SqlFunctionCtx
*
pCtx
,
SResultRowEntryInfo
*
pResInfo
);
int32_t
diffFunction
(
SqlFunctionCtx
*
pCtx
);
...
...
source/libs/function/src/builtins.c
浏览文件 @
011bd323
...
...
@@ -815,10 +815,10 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
.
type
=
FUNCTION_TYPE_APERCENTILE
,
.
classification
=
FUNC_MGT_AGG_FUNC
,
.
translateFunc
=
translateApercentile
,
.
getEnvFunc
=
get
Minmax
FuncEnv
,
.
initFunc
=
minmax
FunctionSetup
,
.
processFunc
=
max
Function
,
.
finalizeFunc
=
function
Finalize
.
getEnvFunc
=
get
Apercentile
FuncEnv
,
.
initFunc
=
apercentile
FunctionSetup
,
.
processFunc
=
apercentile
Function
,
.
finalizeFunc
=
apercentile
Finalize
},
{
.
name
=
"top"
,
...
...
source/libs/function/src/builtinsimpl.c
浏览文件 @
011bd323
...
...
@@ -20,6 +20,8 @@
#include "taggfunction.h"
#include "tcompare.h"
#include "tdatablock.h"
#include "tdigest.h"
#include "thistogram.h"
#include "tpercentile.h"
#define HISTOGRAM_MAX_BINS_NUM 1000
...
...
@@ -95,6 +97,19 @@ typedef struct SPercentileInfo {
int64_t
numOfElems
;
}
SPercentileInfo
;
typedef
struct
SAPercentileInfo
{
double
result
;
int8_t
algo
;
SHistogramInfo
*
pHisto
;
TDigest
*
pTDigest
;
}
SAPercentileInfo
;
typedef
enum
{
APERCT_ALGO_UNKNOWN
=
0
,
APERCT_ALGO_DEFAULT
,
APERCT_ALGO_TDIGEST
,
}
EAPerctAlgoType
;
typedef
struct
SDiffInfo
{
bool
hasPrev
;
bool
includeNull
;
...
...
@@ -1790,6 +1805,131 @@ int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
return
functionFinalize
(
pCtx
,
pBlock
);
}
bool
getApercentileFuncEnv
(
SFunctionNode
*
pFunc
,
SFuncExecEnv
*
pEnv
)
{
int32_t
bytesHist
=
(
int32_t
)(
sizeof
(
SAPercentileInfo
)
+
sizeof
(
SHistogramInfo
)
+
sizeof
(
SHistBin
)
*
(
MAX_HISTOGRAM_BIN
+
1
));
int32_t
bytesDigest
=
(
int32_t
)(
sizeof
(
SAPercentileInfo
)
+
TDIGEST_SIZE
(
COMPRESSION
));
pEnv
->
calcMemSize
=
MAX
(
bytesHist
,
bytesDigest
);
return
true
;
}
static
int8_t
getApercentileAlgo
(
char
*
algoStr
)
{
int8_t
algoType
;
if
(
strcasecmp
(
algoStr
,
"default"
)
==
0
)
{
algoType
=
APERCT_ALGO_DEFAULT
;
}
else
if
(
strcasecmp
(
algoStr
,
"t-digest"
)
==
0
)
{
algoType
=
APERCT_ALGO_TDIGEST
;
}
else
{
algoType
=
APERCT_ALGO_UNKNOWN
;
}
return
algoType
;
}
static
void
buildHistogramInfo
(
SAPercentileInfo
*
pInfo
)
{
pInfo
->
pHisto
=
(
SHistogramInfo
*
)
((
char
*
)
pInfo
+
sizeof
(
SAPercentileInfo
));
pInfo
->
pHisto
->
elems
=
(
SHistBin
*
)
((
char
*
)
pInfo
->
pHisto
+
sizeof
(
SHistogramInfo
));
}
bool
apercentileFunctionSetup
(
SqlFunctionCtx
*
pCtx
,
SResultRowEntryInfo
*
pResultInfo
)
{
if
(
!
functionSetup
(
pCtx
,
pResultInfo
))
{
return
false
;
}
SAPercentileInfo
*
pInfo
=
GET_ROWCELL_INTERBUF
(
pResultInfo
);
if
(
pCtx
->
numOfParams
==
2
)
{
pInfo
->
algo
=
APERCT_ALGO_DEFAULT
;
}
else
if
(
pCtx
->
numOfParams
==
3
)
{
pInfo
->
algo
=
getApercentileAlgo
(
pCtx
->
param
[
2
].
param
.
pz
);
if
(
pInfo
->
algo
==
APERCT_ALGO_UNKNOWN
)
{
return
false
;
}
}
char
*
tmp
=
(
char
*
)
pInfo
+
sizeof
(
SAPercentileInfo
);
if
(
pInfo
->
algo
==
APERCT_ALGO_TDIGEST
)
{
pInfo
->
pTDigest
=
tdigestNewFrom
(
tmp
,
COMPRESSION
);
}
else
{
buildHistogramInfo
(
pInfo
);
pInfo
->
pHisto
=
tHistogramCreateFrom
(
tmp
,
MAX_HISTOGRAM_BIN
);
}
return
true
;
}
int32_t
apercentileFunction
(
SqlFunctionCtx
*
pCtx
)
{
int32_t
notNullElems
=
0
;
SResultRowEntryInfo
*
pResInfo
=
GET_RES_INFO
(
pCtx
);
SInputColumnInfoData
*
pInput
=
&
pCtx
->
input
;
//SColumnDataAgg* pAgg = pInput->pColumnDataAgg[0];
SColumnInfoData
*
pCol
=
pInput
->
pData
[
0
];
int32_t
type
=
pCol
->
info
.
type
;
SAPercentileInfo
*
pInfo
=
GET_ROWCELL_INTERBUF
(
pResInfo
);
int32_t
start
=
pInput
->
startRowIndex
;
if
(
pInfo
->
algo
==
APERCT_ALGO_TDIGEST
)
{
for
(
int32_t
i
=
start
;
i
<
pInput
->
numOfRows
+
start
;
++
i
)
{
if
(
colDataIsNull_f
(
pCol
->
nullbitmap
,
i
))
{
continue
;
}
notNullElems
+=
1
;
char
*
data
=
colDataGetData
(
pCol
,
i
);
double
v
=
0
;
// value
int64_t
w
=
1
;
// weigth
GET_TYPED_DATA
(
v
,
double
,
type
,
data
);
tdigestAdd
(
pInfo
->
pTDigest
,
v
,
w
);
}
}
else
{
for
(
int32_t
i
=
start
;
i
<
pInput
->
numOfRows
+
start
;
++
i
)
{
if
(
colDataIsNull_f
(
pCol
->
nullbitmap
,
i
))
{
continue
;
}
notNullElems
+=
1
;
char
*
data
=
colDataGetData
(
pCol
,
i
);
double
v
=
0
;
GET_TYPED_DATA
(
v
,
double
,
type
,
data
);
tHistogramAdd
(
&
pInfo
->
pHisto
,
v
);
}
}
SET_VAL
(
pResInfo
,
notNullElems
,
1
);
return
TSDB_CODE_SUCCESS
;
}
int32_t
apercentileFinalize
(
SqlFunctionCtx
*
pCtx
,
SSDataBlock
*
pBlock
)
{
SVariant
*
pVal
=
&
pCtx
->
param
[
1
].
param
;
double
percent
=
(
pVal
->
nType
==
TSDB_DATA_TYPE_BIGINT
)
?
pVal
->
i
:
pVal
->
d
;
SResultRowEntryInfo
*
pResInfo
=
GET_RES_INFO
(
pCtx
);
SAPercentileInfo
*
pInfo
=
(
SAPercentileInfo
*
)
GET_ROWCELL_INTERBUF
(
pResInfo
);
if
(
pInfo
->
algo
==
APERCT_ALGO_TDIGEST
)
{
if
(
pInfo
->
pTDigest
->
size
>
0
)
{
pInfo
->
result
=
tdigestQuantile
(
pInfo
->
pTDigest
,
percent
/
100
);
}
else
{
// no need to free
//setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes);
return
TSDB_CODE_SUCCESS
;
}
}
else
{
if
(
pInfo
->
pHisto
->
numOfElems
>
0
)
{
double
ratio
[]
=
{
percent
};
double
*
res
=
tHistogramUniform
(
pInfo
->
pHisto
,
ratio
,
1
);
pInfo
->
result
=
*
res
;
//memcpy(pCtx->pOutput, res, sizeof(double));
taosMemoryFree
(
res
);
}
else
{
// no need to free
//setNull(pCtx->pOutput, pCtx->outputType, pCtx->outputBytes);
return
TSDB_CODE_SUCCESS
;
}
}
return
functionFinalize
(
pCtx
,
pBlock
);
}
bool
getFirstLastFuncEnv
(
SFunctionNode
*
pFunc
,
SFuncExecEnv
*
pEnv
)
{
SColumnNode
*
pNode
=
nodesListGetNode
(
pFunc
->
pParameterList
,
0
);
pEnv
->
calcMemSize
=
pNode
->
node
.
resType
.
bytes
+
sizeof
(
int64_t
);
...
...
@@ -1802,8 +1942,6 @@ bool getSelectivityFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
return
true
;
}
static
FORCE_INLINE
TSKEY
getRowPTs
(
SColumnInfoData
*
pTsColInfo
,
int32_t
rowIndex
)
{
if
(
pTsColInfo
==
NULL
)
{
return
0
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录