Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
3e79defd
T
TDengine
项目概览
taosdata
/
TDengine
11 个月 前同步成功
通知
1179
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,发现更多精彩内容 >>
未验证
提交
3e79defd
编写于
6月 20, 2022
作者:
G
Ganlin Zhao
提交者:
GitHub
6月 20, 2022
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #14003 from taosdata/refactor/top_bot_refact
refactor: refactor top/bottom function and selectivity
上级
3155a228
0ad4c6ca
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
13 addition
and
194 deletion
+13
-194
source/libs/function/src/builtins.c
source/libs/function/src/builtins.c
+10
-110
source/libs/function/src/builtinsimpl.c
source/libs/function/src/builtinsimpl.c
+3
-84
未找到文件。
source/libs/function/src/builtins.c
浏览文件 @
3e79defd
...
...
@@ -419,7 +419,7 @@ static int32_t translateTopBot(SFunctionNode* pFunc, char* pErrBuf, int32_t len)
return
TSDB_CODE_SUCCESS
;
}
int32_t
topCreateMergePara
(
SNodeList
*
pRawParameters
,
SNode
*
pPartialRes
,
SNodeList
**
pParameters
)
{
int32_t
top
Bot
CreateMergePara
(
SNodeList
*
pRawParameters
,
SNode
*
pPartialRes
,
SNodeList
**
pParameters
)
{
int32_t
code
=
nodesListMakeAppend
(
pParameters
,
pPartialRes
);
if
(
TSDB_CODE_SUCCESS
==
code
)
{
code
=
nodesListStrictAppend
(
*
pParameters
,
nodesCloneNode
(
nodesListGetNode
(
pRawParameters
,
1
)));
...
...
@@ -427,65 +427,6 @@ int32_t topCreateMergePara(SNodeList* pRawParameters, SNode* pPartialRes, SNodeL
return
TSDB_CODE_SUCCESS
;
}
static
int32_t
translateTopBotImpl
(
SFunctionNode
*
pFunc
,
char
*
pErrBuf
,
int32_t
len
,
bool
isPartial
)
{
int32_t
numOfParams
=
LIST_LENGTH
(
pFunc
->
pParameterList
);
if
(
isPartial
)
{
if
(
2
!=
numOfParams
)
{
return
invaildFuncParaNumErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
}
uint8_t
para1Type
=
((
SExprNode
*
)
nodesListGetNode
(
pFunc
->
pParameterList
,
0
))
->
resType
.
type
;
uint8_t
para2Type
=
((
SExprNode
*
)
nodesListGetNode
(
pFunc
->
pParameterList
,
1
))
->
resType
.
type
;
if
(
!
IS_NUMERIC_TYPE
(
para1Type
)
||
!
IS_INTEGER_TYPE
(
para2Type
))
{
return
invaildFuncParaTypeErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
}
// param1
SNode
*
pParamNode1
=
nodesListGetNode
(
pFunc
->
pParameterList
,
1
);
if
(
nodeType
(
pParamNode1
)
!=
QUERY_NODE_VALUE
)
{
return
invaildFuncParaTypeErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
}
SValueNode
*
pValue
=
(
SValueNode
*
)
pParamNode1
;
if
(
pValue
->
node
.
resType
.
type
!=
TSDB_DATA_TYPE_BIGINT
)
{
return
invaildFuncParaTypeErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
}
if
(
pValue
->
datum
.
i
<
1
||
pValue
->
datum
.
i
>
100
)
{
return
invaildFuncParaValueErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
}
pValue
->
notReserved
=
true
;
// set result type
pFunc
->
node
.
resType
=
(
SDataType
){.
bytes
=
getTopBotInfoSize
(
pValue
->
datum
.
i
)
+
VARSTR_HEADER_SIZE
,
.
type
=
TSDB_DATA_TYPE_BINARY
};
}
else
{
if
(
1
!=
numOfParams
)
{
return
invaildFuncParaNumErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
}
uint8_t
para1Type
=
((
SExprNode
*
)
nodesListGetNode
(
pFunc
->
pParameterList
,
0
))
->
resType
.
type
;
if
(
TSDB_DATA_TYPE_BINARY
!=
para1Type
)
{
return
invaildFuncParaTypeErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
}
// Do nothing. We can only access output of partial functions as input,
// so original input type cannot be obtained, resType will be set same
// as original function input type after merge function created.
}
return
TSDB_CODE_SUCCESS
;
}
static
int32_t
translateTopBotPartial
(
SFunctionNode
*
pFunc
,
char
*
pErrBuf
,
int32_t
len
)
{
return
translateTopBotImpl
(
pFunc
,
pErrBuf
,
len
,
true
);
}
static
int32_t
translateTopBotMerge
(
SFunctionNode
*
pFunc
,
char
*
pErrBuf
,
int32_t
len
)
{
return
translateTopBotImpl
(
pFunc
,
pErrBuf
,
len
,
false
);
}
static
int32_t
translateSpread
(
SFunctionNode
*
pFunc
,
char
*
pErrBuf
,
int32_t
len
)
{
if
(
1
!=
LIST_LENGTH
(
pFunc
->
pParameterList
))
{
return
invaildFuncParaNumErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
...
...
@@ -1735,31 +1676,9 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
.
processFunc
=
topFunction
,
.
finalizeFunc
=
topBotFinalize
,
.
combineFunc
=
topCombine
,
.
pPartialFunc
=
"_top_partial"
,
.
pMergeFunc
=
"_top_merge"
,
// .createMergeParaFuc = topCreateMergePara
},
{
.
name
=
"_top_partial"
,
.
type
=
FUNCTION_TYPE_TOP_PARTIAL
,
.
classification
=
FUNC_MGT_AGG_FUNC
|
FUNC_MGT_SELECT_FUNC
|
FUNC_MGT_INDEFINITE_ROWS_FUNC
,
.
translateFunc
=
translateTopBotPartial
,
.
getEnvFunc
=
getTopBotFuncEnv
,
.
initFunc
=
topBotFunctionSetup
,
.
processFunc
=
topFunction
,
.
finalizeFunc
=
topBotPartialFinalize
,
.
combineFunc
=
topCombine
,
},
{
.
name
=
"_top_merge"
,
.
type
=
FUNCTION_TYPE_TOP_MERGE
,
.
classification
=
FUNC_MGT_AGG_FUNC
|
FUNC_MGT_SELECT_FUNC
|
FUNC_MGT_INDEFINITE_ROWS_FUNC
,
.
translateFunc
=
translateTopBotMerge
,
.
getEnvFunc
=
getTopBotMergeFuncEnv
,
.
initFunc
=
functionSetup
,
.
processFunc
=
topFunctionMerge
,
.
finalizeFunc
=
topBotMergeFinalize
,
.
combineFunc
=
topCombine
,
.
pPartialFunc
=
"top"
,
.
pMergeFunc
=
"top"
,
.
createMergeParaFuc
=
topBotCreateMergePara
},
{
.
name
=
"bottom"
,
...
...
@@ -1771,30 +1690,9 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
.
processFunc
=
bottomFunction
,
.
finalizeFunc
=
topBotFinalize
,
.
combineFunc
=
bottomCombine
,
.
pPartialFunc
=
"_bottom_partial"
,
.
pMergeFunc
=
"_bottom_merge"
},
{
.
name
=
"_bottom_partial"
,
.
type
=
FUNCTION_TYPE_BOTTOM_PARTIAL
,
.
classification
=
FUNC_MGT_AGG_FUNC
|
FUNC_MGT_SELECT_FUNC
|
FUNC_MGT_INDEFINITE_ROWS_FUNC
,
.
translateFunc
=
translateTopBotPartial
,
.
getEnvFunc
=
getTopBotFuncEnv
,
.
initFunc
=
topBotFunctionSetup
,
.
processFunc
=
bottomFunction
,
.
finalizeFunc
=
topBotPartialFinalize
,
.
combineFunc
=
bottomCombine
,
},
{
.
name
=
"_bottom_merge"
,
.
type
=
FUNCTION_TYPE_BOTTOM_MERGE
,
.
classification
=
FUNC_MGT_AGG_FUNC
|
FUNC_MGT_SELECT_FUNC
|
FUNC_MGT_INDEFINITE_ROWS_FUNC
,
.
translateFunc
=
translateTopBotMerge
,
.
getEnvFunc
=
getTopBotMergeFuncEnv
,
.
initFunc
=
functionSetup
,
.
processFunc
=
bottomFunctionMerge
,
.
finalizeFunc
=
topBotMergeFinalize
,
.
combineFunc
=
bottomCombine
,
.
pPartialFunc
=
"bottom"
,
.
pMergeFunc
=
"bottom"
,
.
createMergeParaFuc
=
topBotCreateMergePara
},
{
.
name
=
"spread"
,
...
...
@@ -2524,7 +2422,9 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
.
getEnvFunc
=
getSelectivityFuncEnv
,
// todo remove this function later.
.
initFunc
=
functionSetup
,
.
processFunc
=
NULL
,
.
finalizeFunc
=
NULL
.
finalizeFunc
=
NULL
,
.
pPartialFunc
=
"_select_value"
,
.
pMergeFunc
=
"_select_value"
},
{
.
name
=
"_block_dist"
,
...
...
source/libs/function/src/builtinsimpl.c
浏览文件 @
3e79defd
...
...
@@ -67,8 +67,7 @@ typedef struct STopBotResItem {
typedef
struct
STopBotRes
{
int32_t
maxSize
;
int16_t
type
;
// store the original input type, used in merge function
int32_t
numOfItems
;
int16_t
type
;
STopBotResItem
*
pItems
;
}
STopBotRes
;
...
...
@@ -2872,12 +2871,6 @@ bool getTopBotFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) {
return
true
;
}
bool
getTopBotMergeFuncEnv
(
SFunctionNode
*
pFunc
,
SFuncExecEnv
*
pEnv
)
{
// intermediate result is binary and length contains VAR header size
pEnv
->
calcMemSize
=
pFunc
->
node
.
resType
.
bytes
;
return
true
;
}
bool
topBotFunctionSetup
(
SqlFunctionCtx
*
pCtx
,
SResultRowEntryInfo
*
pResInfo
)
{
if
(
!
functionSetup
(
pCtx
,
pResInfo
))
{
return
false
;
...
...
@@ -2952,50 +2945,6 @@ int32_t bottomFunction(SqlFunctionCtx* pCtx) {
return
TSDB_CODE_SUCCESS
;
}
static
void
topBotTransferInfo
(
SqlFunctionCtx
*
pCtx
,
STopBotRes
*
pInput
,
bool
isTopQuery
)
{
for
(
int32_t
i
=
0
;
i
<
pInput
->
numOfItems
;
i
++
)
{
addResult
(
pCtx
,
&
pInput
->
pItems
[
i
],
pInput
->
type
,
isTopQuery
);
}
}
int32_t
topFunctionMerge
(
SqlFunctionCtx
*
pCtx
)
{
SResultRowEntryInfo
*
pEntryInfo
=
GET_RES_INFO
(
pCtx
);
SInputColumnInfoData
*
pInput
=
&
pCtx
->
input
;
SColumnInfoData
*
pCol
=
pInput
->
pData
[
0
];
ASSERT
(
pCol
->
info
.
type
==
TSDB_DATA_TYPE_BINARY
);
int32_t
start
=
pInput
->
startRowIndex
;
char
*
data
=
colDataGetData
(
pCol
,
start
);
STopBotRes
*
pInputInfo
=
(
STopBotRes
*
)
varDataVal
(
data
);
STopBotRes
*
pInfo
=
GET_ROWCELL_INTERBUF
(
GET_RES_INFO
(
pCtx
));
pInfo
->
maxSize
=
pInputInfo
->
maxSize
;
pInfo
->
type
=
pInputInfo
->
type
;
topBotTransferInfo
(
pCtx
,
pInputInfo
,
true
);
SET_VAL
(
GET_RES_INFO
(
pCtx
),
pEntryInfo
->
numOfRes
,
pEntryInfo
->
numOfRes
);
return
TSDB_CODE_SUCCESS
;
}
int32_t
bottomFunctionMerge
(
SqlFunctionCtx
*
pCtx
)
{
SResultRowEntryInfo
*
pEntryInfo
=
GET_RES_INFO
(
pCtx
);
SInputColumnInfoData
*
pInput
=
&
pCtx
->
input
;
SColumnInfoData
*
pCol
=
pInput
->
pData
[
0
];
ASSERT
(
pCol
->
info
.
type
==
TSDB_DATA_TYPE_BINARY
);
int32_t
start
=
pInput
->
startRowIndex
;
char
*
data
=
colDataGetData
(
pCol
,
start
);
STopBotRes
*
pInputInfo
=
(
STopBotRes
*
)
varDataVal
(
data
);
STopBotRes
*
pInfo
=
GET_ROWCELL_INTERBUF
(
GET_RES_INFO
(
pCtx
));
pInfo
->
maxSize
=
pInputInfo
->
maxSize
;
pInfo
->
type
=
pInputInfo
->
type
;
topBotTransferInfo
(
pCtx
,
pInputInfo
,
false
);
SET_VAL
(
GET_RES_INFO
(
pCtx
),
pEntryInfo
->
numOfRes
,
pEntryInfo
->
numOfRes
);
return
TSDB_CODE_SUCCESS
;
}
static
int32_t
topBotResComparFn
(
const
void
*
p1
,
const
void
*
p2
,
const
void
*
param
)
{
uint16_t
type
=
*
(
uint16_t
*
)
param
;
...
...
@@ -3044,8 +2993,6 @@ void doAddIntoResult(SqlFunctionCtx* pCtx, void* pData, int32_t rowIndex, SSData
// allocate the buffer and keep the data of this row into the new allocated buffer
pEntryInfo
->
numOfRes
++
;
// accumulate number of items for each vgroup, this info is needed for merge
pRes
->
numOfItems
++
;
taosheapsort
((
void
*
)
pItems
,
sizeof
(
STopBotResItem
),
pEntryInfo
->
numOfRes
,
(
const
void
*
)
&
type
,
topBotResComparFn
,
!
isTopQuery
);
}
else
{
// replace the minimum value in the result
...
...
@@ -3144,7 +3091,7 @@ void copyTupleData(SqlFunctionCtx* pCtx, int32_t rowIndex, const SSDataBlock* pS
releaseBufPage
(
pCtx
->
pBuf
,
pPage
);
}
int32_t
topBotFinalize
Impl
(
SqlFunctionCtx
*
pCtx
,
SSDataBlock
*
pBlock
,
bool
isMerge
)
{
int32_t
topBotFinalize
(
SqlFunctionCtx
*
pCtx
,
SSDataBlock
*
pBlock
)
{
SResultRowEntryInfo
*
pEntryInfo
=
GET_RES_INFO
(
pCtx
);
STopBotRes
*
pRes
=
GET_ROWCELL_INTERBUF
(
pEntryInfo
);
...
...
@@ -3164,39 +3111,13 @@ int32_t topBotFinalizeImpl(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, bool isMer
colDataAppend
(
pCol
,
currentRow
,
(
const
char
*
)
&
pItem
->
v
.
i
,
false
);
}
if
(
!
isMerge
)
{
setSelectivityValue
(
pCtx
,
pBlock
,
&
pRes
->
pItems
[
i
].
tuplePos
,
currentRow
);
}
setSelectivityValue
(
pCtx
,
pBlock
,
&
pRes
->
pItems
[
i
].
tuplePos
,
currentRow
);
currentRow
+=
1
;
}
return
pEntryInfo
->
numOfRes
;
}
int32_t
topBotFinalize
(
SqlFunctionCtx
*
pCtx
,
SSDataBlock
*
pBlock
)
{
return
topBotFinalizeImpl
(
pCtx
,
pBlock
,
false
);
}
int32_t
topBotMergeFinalize
(
SqlFunctionCtx
*
pCtx
,
SSDataBlock
*
pBlock
)
{
return
topBotFinalizeImpl
(
pCtx
,
pBlock
,
true
);
}
int32_t
topBotPartialFinalize
(
SqlFunctionCtx
*
pCtx
,
SSDataBlock
*
pBlock
)
{
SResultRowEntryInfo
*
pEntryInfo
=
GET_RES_INFO
(
pCtx
);
STopBotRes
*
pRes
=
GET_ROWCELL_INTERBUF
(
GET_RES_INFO
(
pCtx
));
int32_t
resultBytes
=
getTopBotInfoSize
(
pRes
->
maxSize
);
char
*
res
=
taosMemoryCalloc
(
resultBytes
+
VARSTR_HEADER_SIZE
,
sizeof
(
char
));
memcpy
(
varDataVal
(
res
),
pRes
,
resultBytes
);
varDataSetLen
(
res
,
resultBytes
);
int32_t
slotId
=
pCtx
->
pExpr
->
base
.
resSchema
.
slotId
;
SColumnInfoData
*
pCol
=
taosArrayGet
(
pBlock
->
pDataBlock
,
slotId
);
colDataAppend
(
pCol
,
pBlock
->
info
.
rows
,
res
,
false
);
taosMemoryFree
(
res
);
return
1
;
}
void
addResult
(
SqlFunctionCtx
*
pCtx
,
STopBotResItem
*
pSourceItem
,
int16_t
type
,
bool
isTopQuery
)
{
SResultRowEntryInfo
*
pEntryInfo
=
GET_RES_INFO
(
pCtx
);
STopBotRes
*
pRes
=
getTopBotOutputInfo
(
pCtx
);
...
...
@@ -3211,8 +3132,6 @@ void addResult(SqlFunctionCtx* pCtx, STopBotResItem* pSourceItem, int16_t type,
pItem
->
tuplePos
.
pageId
=
-
1
;
replaceTupleData
(
&
pItem
->
tuplePos
,
&
pSourceItem
->
tuplePos
);
pEntryInfo
->
numOfRes
++
;
// accumulate number of items for each vgroup, this info is needed for merge
pRes
->
numOfItems
++
;
taosheapsort
((
void
*
)
pItems
,
sizeof
(
STopBotResItem
),
pEntryInfo
->
numOfRes
,
(
const
void
*
)
&
type
,
topBotResComparFn
,
!
isTopQuery
);
}
else
{
// replace the minimum value in the result
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录