Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
805ce63f
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22017
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看板
提交
805ce63f
编写于
6月 21, 2022
作者:
S
slzhou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: add sort group operator
上级
22f0ae45
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
254 addition
and
0 deletion
+254
-0
source/libs/executor/src/sortoperator.c
source/libs/executor/src/sortoperator.c
+254
-0
未找到文件。
source/libs/executor/src/sortoperator.c
浏览文件 @
805ce63f
...
...
@@ -232,6 +232,260 @@ int32_t getExplainExecInfo(SOperatorInfo* pOptr, void** pOptrExplain, uint32_t*
return
TSDB_CODE_SUCCESS
;
}
//=====================================================================================
// Group Sort Operator
typedef
enum
EChildOperatorStatus
{
CHILD_OP_NEW_GROUP
,
CHILD_OP_SAME_GROUP
,
CHILD_OP_FINISHED
}
EChildOperatorStatus
;
typedef
struct
SGroupSortOperatorInfo
{
SOptrBasicInfo
binfo
;
uint32_t
sortBufSize
;
// max buffer size for in-memory sort
SArray
*
pSortInfo
;
SArray
*
pColMatchInfo
;
// for index map from table scan output
int32_t
bufPageSize
;
int64_t
startTs
;
// sort start time
uint64_t
sortElapsed
;
// sort elapsed time, time to flush to disk not included.
SSDataBlock
*
prefetchedDatablock
;
bool
hasGroupId
;
uint64_t
currGroupId
;
SSortHandle
*
pCurrSortHandle
;
EChildOperatorStatus
childOpStatus
;
}
SGroupSortOperatorInfo
;
SSDataBlock
*
getGroupSortedBlockData
(
SSortHandle
*
pHandle
,
SSDataBlock
*
pDataBlock
,
int32_t
capacity
,
SArray
*
pColMatchInfo
,
SGroupSortOperatorInfo
*
pInfo
)
{
blockDataCleanup
(
pDataBlock
);
SSDataBlock
*
p
=
tsortGetSortedDataBlock
(
pHandle
);
if
(
p
==
NULL
)
{
return
NULL
;
}
blockDataEnsureCapacity
(
p
,
capacity
);
while
(
1
)
{
STupleHandle
*
pTupleHandle
=
tsortNextTuple
(
pHandle
);
if
(
pTupleHandle
==
NULL
)
{
break
;
}
appendOneRowToDataBlock
(
p
,
pTupleHandle
);
if
(
p
->
info
.
rows
>=
capacity
)
{
break
;
}
}
if
(
p
->
info
.
rows
>
0
)
{
int32_t
numOfCols
=
taosArrayGetSize
(
pColMatchInfo
);
for
(
int32_t
i
=
0
;
i
<
numOfCols
;
++
i
)
{
SColMatchInfo
*
pmInfo
=
taosArrayGet
(
pColMatchInfo
,
i
);
ASSERT
(
pmInfo
->
matchType
==
COL_MATCH_FROM_SLOT_ID
);
SColumnInfoData
*
pSrc
=
taosArrayGet
(
p
->
pDataBlock
,
pmInfo
->
srcSlotId
);
SColumnInfoData
*
pDst
=
taosArrayGet
(
pDataBlock
->
pDataBlock
,
pmInfo
->
targetSlotId
);
colDataAssign
(
pDst
,
pSrc
,
p
->
info
.
rows
);
}
pDataBlock
->
info
.
rows
=
p
->
info
.
rows
;
pDataBlock
->
info
.
capacity
=
p
->
info
.
rows
;
}
blockDataDestroy
(
p
);
return
(
pDataBlock
->
info
.
rows
>
0
)
?
pDataBlock
:
NULL
;
}
typedef
struct
SGroupSortSourceParam
{
SOperatorInfo
*
childOpInfo
;
SGroupSortOperatorInfo
*
grpSortOpInfo
;
}
SGroupSortSourceParam
;
SSDataBlock
*
fetchNextGroupSortDataBlock
(
void
*
param
)
{
SGroupSortSourceParam
*
source
=
param
;
SGroupSortOperatorInfo
*
grpSortOpInfo
=
source
->
grpSortOpInfo
;
if
(
grpSortOpInfo
->
prefetchedDatablock
)
{
SSDataBlock
*
pBlock
=
grpSortOpInfo
->
prefetchedDatablock
;
grpSortOpInfo
->
prefetchedDatablock
=
NULL
;
return
pBlock
;
}
else
{
SOperatorInfo
*
childOp
=
source
->
childOpInfo
;
SSDataBlock
*
block
=
childOp
->
fpSet
.
getNextFn
(
childOp
);
if
(
block
!=
NULL
)
{
if
(
block
->
info
.
groupId
==
grpSortOpInfo
->
currGroupId
)
{
grpSortOpInfo
->
childOpStatus
=
CHILD_OP_SAME_GROUP
;
return
block
;
}
else
{
grpSortOpInfo
->
childOpStatus
=
CHILD_OP_NEW_GROUP
;
grpSortOpInfo
->
prefetchedDatablock
=
block
;
return
NULL
;
}
}
else
{
grpSortOpInfo
->
childOpStatus
=
CHILD_OP_FINISHED
;
return
NULL
;
}
}
}
int32_t
beginSortGroup
(
SOperatorInfo
*
pOperator
)
{
SGroupSortOperatorInfo
*
pInfo
=
pOperator
->
info
;
SExecTaskInfo
*
pTaskInfo
=
pOperator
->
pTaskInfo
;
// pInfo->binfo.pRes is not equalled to the input datablock.
pInfo
->
pCurrSortHandle
=
tsortCreateSortHandle
(
pInfo
->
pSortInfo
,
SORT_SINGLESOURCE_SORT
,
-
1
,
-
1
,
NULL
,
pTaskInfo
->
id
.
str
);
tsortSetFetchRawDataFp
(
pInfo
->
pCurrSortHandle
,
fetchNextGroupSortDataBlock
,
applyScalarFunction
,
pOperator
);
SSortSource
*
ps
=
taosMemoryCalloc
(
1
,
sizeof
(
SSortSource
));
SGroupSortSourceParam
*
param
=
taosMemoryCalloc
(
1
,
sizeof
(
SGroupSortSourceParam
));
param
->
childOpInfo
=
pOperator
->
pDownstream
[
0
];
param
->
grpSortOpInfo
=
pInfo
;
ps
->
param
=
param
;
tsortAddSource
(
pInfo
->
pCurrSortHandle
,
ps
);
int32_t
code
=
tsortOpen
(
pInfo
->
pCurrSortHandle
);
taosMemoryFreeClear
(
ps
);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
longjmp
(
pTaskInfo
->
env
,
terrno
);
}
return
TSDB_CODE_SUCCESS
;
}
int32_t
finishSortGroup
(
SOperatorInfo
*
pOperator
)
{
SGroupSortOperatorInfo
*
pInfo
=
pOperator
->
info
;
if
(
pInfo
->
pCurrSortHandle
!=
NULL
)
{
tsortDestroySortHandle
(
pInfo
->
pCurrSortHandle
);
}
pInfo
->
pCurrSortHandle
=
NULL
;
return
TSDB_CODE_SUCCESS
;
}
SSDataBlock
*
doGroupSort
(
SOperatorInfo
*
pOperator
)
{
if
(
pOperator
->
status
==
OP_EXEC_DONE
)
{
return
NULL
;
}
SExecTaskInfo
*
pTaskInfo
=
pOperator
->
pTaskInfo
;
SGroupSortOperatorInfo
*
pInfo
=
pOperator
->
info
;
int32_t
code
=
pOperator
->
fpSet
.
_openFn
(
pOperator
);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
longjmp
(
pTaskInfo
->
env
,
code
);
}
if
(
!
pInfo
->
hasGroupId
)
{
pInfo
->
hasGroupId
=
true
;
pInfo
->
prefetchedDatablock
=
pOperator
->
pDownstream
[
0
]
->
fpSet
.
getNextFn
(
pOperator
->
pDownstream
[
0
]);
pInfo
->
currGroupId
=
pInfo
->
prefetchedDatablock
->
info
.
groupId
;
pInfo
->
childOpStatus
=
CHILD_OP_NEW_GROUP
;
beginSortGroup
(
pOperator
);
}
SSDataBlock
*
pBlock
=
NULL
;
while
(
pInfo
->
childOpStatus
!=
CHILD_OP_FINISHED
)
{
pBlock
=
getGroupSortedBlockData
(
pInfo
->
pCurrSortHandle
,
pInfo
->
binfo
.
pRes
,
pOperator
->
resultInfo
.
capacity
,
pInfo
->
pColMatchInfo
,
pInfo
);
if
(
pBlock
!=
NULL
)
{
pBlock
->
info
.
groupId
=
pInfo
->
currGroupId
;
pOperator
->
resultInfo
.
totalRows
+=
pBlock
->
info
.
rows
;
}
if
(
pInfo
->
childOpStatus
==
CHILD_OP_NEW_GROUP
)
{
finishSortGroup
(
pOperator
);
pInfo
->
currGroupId
=
pInfo
->
prefetchedDatablock
->
info
.
groupId
;
beginSortGroup
(
pOperator
);
}
return
pBlock
;
}
if
(
pInfo
->
childOpStatus
==
CHILD_OP_FINISHED
)
{
finishSortGroup
(
pOperator
);
doSetOperatorCompleted
(
pOperator
);
}
return
pBlock
;
}
int32_t
getGroupSortExplainExecInfo
(
SOperatorInfo
*
pOptr
,
void
**
pOptrExplain
,
uint32_t
*
len
)
{
//TODO: accumulate all sort handles;
return
TSDB_CODE_SUCCESS
;
}
//TODO:
SOperatorInfo
*
createGroupSortOperatorInfo
(
SOperatorInfo
*
downstream
,
SSortPhysiNode
*
pSortPhyNode
,
SExecTaskInfo
*
pTaskInfo
)
{
SSortOperatorInfo
*
pInfo
=
taosMemoryCalloc
(
1
,
sizeof
(
SSortOperatorInfo
));
SOperatorInfo
*
pOperator
=
taosMemoryCalloc
(
1
,
sizeof
(
SOperatorInfo
));
if
(
pInfo
==
NULL
||
pOperator
==
NULL
/* || rowSize > 100 * 1024 * 1024*/
)
{
goto
_error
;
}
SDataBlockDescNode
*
pDescNode
=
pSortPhyNode
->
node
.
pOutputDataBlockDesc
;
int32_t
numOfCols
=
0
;
SSDataBlock
*
pResBlock
=
createResDataBlock
(
pDescNode
);
SExprInfo
*
pExprInfo
=
createExprInfo
(
pSortPhyNode
->
pExprs
,
NULL
,
&
numOfCols
);
int32_t
numOfOutputCols
=
0
;
SArray
*
pColMatchColInfo
=
extractColMatchInfo
(
pSortPhyNode
->
pTargets
,
pDescNode
,
&
numOfOutputCols
,
COL_MATCH_FROM_SLOT_ID
);
pOperator
->
exprSupp
.
pCtx
=
createSqlFunctionCtx
(
pExprInfo
,
numOfCols
,
&
pOperator
->
exprSupp
.
rowEntryInfoOffset
);
pInfo
->
binfo
.
pRes
=
pResBlock
;
initResultSizeInfo
(
pOperator
,
1024
);
pInfo
->
pSortInfo
=
createSortInfo
(
pSortPhyNode
->
pSortKeys
);;
pInfo
->
pColMatchInfo
=
pColMatchColInfo
;
pOperator
->
name
=
"GroupSortOperator"
;
//TODO
pOperator
->
operatorType
=
QUERY_NODE_PHYSICAL_PLAN_SORT
;
pOperator
->
blocking
=
true
;
pOperator
->
status
=
OP_NOT_OPENED
;
pOperator
->
info
=
pInfo
;
pOperator
->
exprSupp
.
pExprInfo
=
pExprInfo
;
pOperator
->
exprSupp
.
numOfExprs
=
numOfCols
;
pOperator
->
pTaskInfo
=
pTaskInfo
;
pOperator
->
fpSet
=
createOperatorFpSet
(
operatorDummyOpenFn
,
doGroupSort
,
NULL
,
NULL
,
destroyOrderOperatorInfo
,
NULL
,
NULL
,
getGroupSortExplainExecInfo
);
int32_t
code
=
appendDownstream
(
pOperator
,
&
downstream
,
1
);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
goto
_error
;
}
return
pOperator
;
_error:
pTaskInfo
->
code
=
TSDB_CODE_OUT_OF_MEMORY
;
taosMemoryFree
(
pInfo
);
taosMemoryFree
(
pOperator
);
return
NULL
;
}
void
destroyGroupSortOperatorInfo
(
void
*
param
,
int32_t
numOfOutput
)
{
SGroupSortOperatorInfo
*
pInfo
=
(
SGroupSortOperatorInfo
*
)
param
;
pInfo
->
binfo
.
pRes
=
blockDataDestroy
(
pInfo
->
binfo
.
pRes
);
taosArrayDestroy
(
pInfo
->
pSortInfo
);
taosArrayDestroy
(
pInfo
->
pColMatchInfo
);
}
//TODO: sort group
//TODO: msortCompare compare group id in multiway merge sort.
//TODO: table merge scan, group first, then for each group, multiple readers
//=====================================================================================
// Multiway Sort Merge operator
typedef
struct
SMultiwaySortMergeOperatorInfo
{
SOptrBasicInfo
binfo
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录