Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
a3f5e472
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看板
未验证
提交
a3f5e472
编写于
5月 16, 2022
作者:
G
Ganlin Zhao
提交者:
GitHub
5月 16, 2022
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #12552 from taosdata/feature/3.0_glzhao
feat(query): add tail function
上级
a934c642
ede4a57c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
193 addition
and
2 deletion
+193
-2
source/libs/function/inc/builtinsimpl.h
source/libs/function/inc/builtinsimpl.h
+6
-1
source/libs/function/src/builtins.c
source/libs/function/src/builtins.c
+39
-0
source/libs/function/src/builtinsimpl.c
source/libs/function/src/builtinsimpl.c
+148
-1
未找到文件。
source/libs/function/inc/builtinsimpl.h
浏览文件 @
a3f5e472
...
...
@@ -105,7 +105,12 @@ int32_t mavgFunction(SqlFunctionCtx* pCtx);
bool
getSampleFuncEnv
(
struct
SFunctionNode
*
pFunc
,
SFuncExecEnv
*
pEnv
);
bool
sampleFunctionSetup
(
SqlFunctionCtx
*
pCtx
,
SResultRowEntryInfo
*
pResultInfo
);
int32_t
sampleFunction
(
SqlFunctionCtx
*
pCtx
);
int32_t
sampleFinalize
(
SqlFunctionCtx
*
pCtx
,
SSDataBlock
*
pBlock
);
//int32_t sampleFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock);
bool
getTailFuncEnv
(
struct
SFunctionNode
*
pFunc
,
SFuncExecEnv
*
pEnv
);
bool
tailFunctionSetup
(
SqlFunctionCtx
*
pCtx
,
SResultRowEntryInfo
*
pResultInfo
);
int32_t
tailFunction
(
SqlFunctionCtx
*
pCtx
);
int32_t
tailFinalize
(
SqlFunctionCtx
*
pCtx
,
SSDataBlock
*
pBlock
);
bool
getSelectivityFuncEnv
(
SFunctionNode
*
pFunc
,
SFuncExecEnv
*
pEnv
);
...
...
source/libs/function/src/builtins.c
浏览文件 @
a3f5e472
...
...
@@ -386,6 +386,35 @@ static int32_t translateSample(SFunctionNode* pFunc, char* pErrBuf, int32_t len)
return
TSDB_CODE_SUCCESS
;
}
static
int32_t
translateTail
(
SFunctionNode
*
pFunc
,
char
*
pErrBuf
,
int32_t
len
)
{
int32_t
numOfParams
=
LIST_LENGTH
(
pFunc
->
pParameterList
);
if
(
2
!=
numOfParams
&&
3
!=
numOfParams
)
{
return
invaildFuncParaNumErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
}
SNode
*
pPara
=
nodesListGetNode
(
pFunc
->
pParameterList
,
0
);
if
(
QUERY_NODE_COLUMN
!=
nodeType
(
pPara
))
{
return
buildFuncErrMsg
(
pErrBuf
,
len
,
TSDB_CODE_FUNC_FUNTION_ERROR
,
"The input parameter of TAIL function can only be column"
);
}
for
(
int32_t
i
=
1
;
i
<
numOfParams
;
++
i
)
{
uint8_t
paraType
=
((
SExprNode
*
)
nodesListGetNode
(
pFunc
->
pParameterList
,
i
))
->
resType
.
type
;
if
(
!
IS_INTEGER_TYPE
(
paraType
))
{
return
invaildFuncParaTypeErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
}
}
SExprNode
*
pCol
=
(
SExprNode
*
)
nodesListGetNode
(
pFunc
->
pParameterList
,
0
);
uint8_t
colType
=
pCol
->
resType
.
type
;
if
(
IS_VAR_DATA_TYPE
(
colType
))
{
pFunc
->
node
.
resType
=
(
SDataType
){.
bytes
=
pCol
->
resType
.
bytes
,
.
type
=
colType
};
}
else
{
pFunc
->
node
.
resType
=
(
SDataType
){.
bytes
=
tDataTypes
[
colType
].
bytes
,
.
type
=
colType
};
}
return
TSDB_CODE_SUCCESS
;
}
static
int32_t
translateLastRow
(
SFunctionNode
*
pFunc
,
char
*
pErrBuf
,
int32_t
len
)
{
// todo
return
TSDB_CODE_SUCCESS
;
...
...
@@ -850,6 +879,16 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
.
processFunc
=
sampleFunction
,
.
finalizeFunc
=
NULL
},
{
.
name
=
"tail"
,
.
type
=
FUNCTION_TYPE_TAIL
,
.
classification
=
FUNC_MGT_NONSTANDARD_SQL_FUNC
|
FUNC_MGT_TIMELINE_FUNC
,
.
translateFunc
=
translateTail
,
.
getEnvFunc
=
getTailFuncEnv
,
.
initFunc
=
tailFunctionSetup
,
.
processFunc
=
tailFunction
,
.
finalizeFunc
=
tailFinalize
},
{
.
name
=
"abs"
,
.
type
=
FUNCTION_TYPE_ABS
,
...
...
source/libs/function/src/builtinsimpl.c
浏览文件 @
a3f5e472
...
...
@@ -18,12 +18,15 @@
#include "function.h"
#include "querynodes.h"
#include "taggfunction.h"
#include "tcompare.h"
#include "tdatablock.h"
#include "tpercentile.h"
#define HISTOGRAM_MAX_BINS_NUM 1000
#define MAVG_MAX_POINTS_NUM 1000
#define SAMPLE_MAX_POINTS_NUM 1000
#define TAIL_MAX_POINTS_NUM 100
#define TAIL_MAX_OFFSET 100
typedef
struct
SSumRes
{
union
{
...
...
@@ -161,6 +164,21 @@ typedef struct SSampleInfo {
int64_t
*
timestamp
;
}
SSampleInfo
;
typedef
struct
STailItem
{
int64_t
timestamp
;
bool
isNull
;
char
data
[];
}
STailItem
;
typedef
struct
STailInfo
{
int32_t
numOfPoints
;
int32_t
numAdded
;
int32_t
offset
;
uint8_t
colType
;
int16_t
colBytes
;
STailItem
**
pItems
;
}
STailInfo
;
#define SET_VAL(_info, numOfElem, res) \
do { \
if ((numOfElem) <= 0) { \
...
...
@@ -3107,7 +3125,7 @@ int32_t sampleFunction(SqlFunctionCtx* pCtx) {
int32_t
startOffset
=
pCtx
->
offset
;
for
(
int32_t
i
=
pInput
->
startRowIndex
;
i
<
pInput
->
numOfRows
+
pInput
->
startRowIndex
;
i
+=
1
)
{
if
(
colDataIsNull_
f
(
pInputCol
->
nullbitmap
,
i
))
{
if
(
colDataIsNull_
s
(
pInputCol
,
i
))
{
//colDataAppendNULL(pOutput, i);
continue
;
}
...
...
@@ -3141,3 +3159,132 @@ int32_t sampleFunction(SqlFunctionCtx* pCtx) {
//
// return pResInfo->numOfRes;
//}
bool
getTailFuncEnv
(
SFunctionNode
*
pFunc
,
SFuncExecEnv
*
pEnv
)
{
SColumnNode
*
pCol
=
(
SColumnNode
*
)
nodesListGetNode
(
pFunc
->
pParameterList
,
0
);
SValueNode
*
pVal
=
(
SValueNode
*
)
nodesListGetNode
(
pFunc
->
pParameterList
,
1
);
int32_t
numOfPoints
=
pVal
->
datum
.
i
;
pEnv
->
calcMemSize
=
sizeof
(
STailInfo
)
+
numOfPoints
*
(
POINTER_BYTES
+
sizeof
(
STailItem
)
+
pCol
->
node
.
resType
.
bytes
);
return
true
;
}
bool
tailFunctionSetup
(
SqlFunctionCtx
*
pCtx
,
SResultRowEntryInfo
*
pResultInfo
)
{
if
(
!
functionSetup
(
pCtx
,
pResultInfo
))
{
return
false
;
}
STailInfo
*
pInfo
=
GET_ROWCELL_INTERBUF
(
pResultInfo
);
pInfo
->
numAdded
=
0
;
pInfo
->
numOfPoints
=
pCtx
->
param
[
1
].
param
.
i
;
if
(
pCtx
->
numOfParams
==
4
)
{
pInfo
->
offset
=
pCtx
->
param
[
2
].
param
.
i
;
}
else
{
pInfo
->
offset
=
0
;
}
pInfo
->
colType
=
pCtx
->
resDataInfo
.
type
;
pInfo
->
colBytes
=
pCtx
->
resDataInfo
.
bytes
;
if
((
pInfo
->
numOfPoints
<
1
||
pInfo
->
numOfPoints
>
TAIL_MAX_POINTS_NUM
)
||
(
pInfo
->
numOfPoints
<
0
||
pInfo
->
numOfPoints
>
TAIL_MAX_OFFSET
))
{
return
false
;
}
pInfo
->
pItems
=
(
STailItem
**
)((
char
*
)
pInfo
+
sizeof
(
STailInfo
));
char
*
pItem
=
(
char
*
)
pInfo
->
pItems
+
pInfo
->
numOfPoints
*
POINTER_BYTES
;
size_t
unitSize
=
sizeof
(
STailItem
)
+
pInfo
->
colBytes
;
for
(
int32_t
i
=
0
;
i
<
pInfo
->
numOfPoints
;
++
i
)
{
pInfo
->
pItems
[
i
]
=
(
STailItem
*
)(
pItem
+
i
*
unitSize
);
pInfo
->
pItems
[
i
]
->
isNull
=
false
;
}
return
true
;
}
static
void
tailAssignResult
(
STailItem
*
pItem
,
char
*
data
,
int32_t
colBytes
,
TSKEY
ts
,
bool
isNull
)
{
pItem
->
timestamp
=
ts
;
if
(
isNull
)
{
pItem
->
isNull
=
true
;
}
else
{
memcpy
(
pItem
->
data
,
data
,
colBytes
);
}
}
static
int32_t
tailCompFn
(
const
void
*
p1
,
const
void
*
p2
,
const
void
*
param
)
{
STailItem
*
d1
=
*
(
STailItem
**
)
p1
;
STailItem
*
d2
=
*
(
STailItem
**
)
p2
;
return
compareInt64Val
(
&
d1
->
timestamp
,
&
d2
->
timestamp
);
}
static
void
doTailAdd
(
STailInfo
*
pInfo
,
char
*
data
,
TSKEY
ts
,
bool
isNull
)
{
STailItem
**
pList
=
pInfo
->
pItems
;
if
(
pInfo
->
numAdded
<
pInfo
->
numOfPoints
)
{
tailAssignResult
(
pList
[
pInfo
->
numAdded
],
data
,
pInfo
->
colBytes
,
ts
,
isNull
);
taosheapsort
((
void
*
)
pList
,
sizeof
(
STailItem
**
),
pInfo
->
numAdded
+
1
,
NULL
,
tailCompFn
,
0
);
pInfo
->
numAdded
++
;
}
else
if
(
pList
[
0
]
->
timestamp
<
ts
)
{
tailAssignResult
(
pList
[
0
],
data
,
pInfo
->
colBytes
,
ts
,
isNull
);
taosheapadjust
((
void
*
)
pList
,
sizeof
(
STailItem
**
),
0
,
pInfo
->
numOfPoints
-
1
,
NULL
,
tailCompFn
,
NULL
,
0
);
}
}
int32_t
tailFunction
(
SqlFunctionCtx
*
pCtx
)
{
SResultRowEntryInfo
*
pResInfo
=
GET_RES_INFO
(
pCtx
);
STailInfo
*
pInfo
=
GET_ROWCELL_INTERBUF
(
pResInfo
);
SInputColumnInfoData
*
pInput
=
&
pCtx
->
input
;
TSKEY
*
tsList
=
(
int64_t
*
)
pInput
->
pPTS
->
pData
;
SColumnInfoData
*
pInputCol
=
pInput
->
pData
[
0
];
SColumnInfoData
*
pTsOutput
=
pCtx
->
pTsOutput
;
SColumnInfoData
*
pOutput
=
(
SColumnInfoData
*
)
pCtx
->
pOutput
;
int32_t
startOffset
=
pCtx
->
offset
;
if
(
pInfo
->
offset
>=
pInput
->
numOfRows
)
{
return
0
;
}
else
{
pInfo
->
numOfPoints
=
MIN
(
pInfo
->
numOfPoints
,
pInput
->
numOfRows
-
pInfo
->
offset
);
}
for
(
int32_t
i
=
pInput
->
startRowIndex
;
i
<
pInput
->
numOfRows
+
pInput
->
startRowIndex
-
pInfo
->
offset
;
i
+=
1
)
{
char
*
data
=
colDataGetData
(
pInputCol
,
i
);
doTailAdd
(
pInfo
,
data
,
tsList
[
i
],
colDataIsNull_s
(
pInputCol
,
i
));
}
taosqsort
(
pInfo
->
pItems
,
pInfo
->
numOfPoints
,
POINTER_BYTES
,
NULL
,
tailCompFn
);
for
(
int32_t
i
=
0
;
i
<
pInfo
->
numOfPoints
;
++
i
)
{
int32_t
pos
=
startOffset
+
i
;
STailItem
*
pItem
=
pInfo
->
pItems
[
i
];
if
(
pItem
->
isNull
)
{
colDataAppendNULL
(
pOutput
,
pos
);
}
else
{
colDataAppend
(
pOutput
,
pos
,
pItem
->
data
,
false
);
}
}
return
pInfo
->
numOfPoints
;
}
int32_t
tailFinalize
(
SqlFunctionCtx
*
pCtx
,
SSDataBlock
*
pBlock
)
{
SResultRowEntryInfo
*
pEntryInfo
=
GET_RES_INFO
(
pCtx
);
STailInfo
*
pInfo
=
GET_ROWCELL_INTERBUF
(
pEntryInfo
);
pEntryInfo
->
complete
=
true
;
int32_t
type
=
pCtx
->
input
.
pData
[
0
]
->
info
.
type
;
int32_t
slotId
=
pCtx
->
pExpr
->
base
.
resSchema
.
slotId
;
SColumnInfoData
*
pCol
=
taosArrayGet
(
pBlock
->
pDataBlock
,
slotId
);
// todo assign the tag value and the corresponding row data
int32_t
currentRow
=
pBlock
->
info
.
rows
;
for
(
int32_t
i
=
0
;
i
<
pEntryInfo
->
numOfRes
;
++
i
)
{
STailItem
*
pItem
=
pInfo
->
pItems
[
i
];
colDataAppend
(
pCol
,
currentRow
,
pItem
->
data
,
false
);
//setSelectivityValue(pCtx, pBlock, &pInfo->pItems[i].tuplePos, currentRow);
currentRow
+=
1
;
}
return
pEntryInfo
->
numOfRes
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录