Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
0228caaa
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看板
提交
0228caaa
编写于
10月 18, 2022
作者:
X
Xiaoyu Wang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
enh: last cache optimize
上级
c8b180b1
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
109 addition
and
25 deletion
+109
-25
include/libs/function/functionMgt.h
include/libs/function/functionMgt.h
+3
-0
source/libs/function/src/builtins.c
source/libs/function/src/builtins.c
+10
-0
source/libs/function/src/functionMgt.c
source/libs/function/src/functionMgt.c
+6
-0
source/libs/planner/src/planOptimizer.c
source/libs/planner/src/planOptimizer.c
+71
-7
source/libs/planner/test/planBasicTest.cpp
source/libs/planner/test/planBasicTest.cpp
+0
-18
source/libs/planner/test/planOptimizeTest.cpp
source/libs/planner/test/planOptimizeTest.cpp
+19
-0
未找到文件。
include/libs/function/functionMgt.h
浏览文件 @
0228caaa
...
...
@@ -129,6 +129,7 @@ typedef enum EFunctionType {
FUNCTION_TYPE_TO_COLUMN
,
FUNCTION_TYPE_GROUP_KEY
,
FUNCTION_TYPE_CACHE_LAST_ROW
,
FUNCTION_TYPE_CACHE_LAST
,
// distributed splitting functions
FUNCTION_TYPE_APERCENTILE_PARTIAL
=
4000
,
...
...
@@ -216,6 +217,8 @@ bool fmIsKeepOrderFunc(int32_t funcId);
bool
fmIsCumulativeFunc
(
int32_t
funcId
);
bool
fmIsInterpPseudoColumnFunc
(
int32_t
funcId
);
void
getLastCacheDataType
(
SDataType
*
pType
);
int32_t
fmGetDistMethod
(
const
SFunctionNode
*
pFunc
,
SFunctionNode
**
pPartialFunc
,
SFunctionNode
**
pMergeFunc
);
typedef
enum
EFuncDataRequired
{
...
...
source/libs/function/src/builtins.c
浏览文件 @
0228caaa
...
...
@@ -2405,6 +2405,16 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
.
processFunc
=
lastFunctionMerge
,
.
finalizeFunc
=
firstLastFinalize
},
{
.
name
=
"_cache_last"
,
.
type
=
FUNCTION_TYPE_CACHE_LAST
,
.
classification
=
FUNC_MGT_AGG_FUNC
|
FUNC_MGT_MULTI_RES_FUNC
|
FUNC_MGT_FORBID_STREAM_FUNC
,
.
translateFunc
=
translateFirstLast
,
.
getEnvFunc
=
getFirstLastFuncEnv
,
.
initFunc
=
functionSetup
,
.
processFunc
=
lastFunctionMerge
,
.
finalizeFunc
=
firstLastFinalize
},
{
.
name
=
"_last_row_partial"
,
.
type
=
FUNCTION_TYPE_LAST_PARTIAL
,
...
...
source/libs/function/src/functionMgt.c
浏览文件 @
0228caaa
...
...
@@ -16,6 +16,7 @@
#include "functionMgt.h"
#include "builtins.h"
#include "builtinsimpl.h"
#include "functionMgtInt.h"
#include "taos.h"
#include "taoserror.h"
...
...
@@ -314,6 +315,11 @@ bool fmIsSameInOutType(int32_t funcId) {
return
res
;
}
void
getLastCacheDataType
(
SDataType
*
pType
)
{
pType
->
bytes
=
getFirstLastInfoSize
(
pType
->
bytes
)
+
VARSTR_HEADER_SIZE
;
pType
->
type
=
TSDB_DATA_TYPE_BINARY
;
}
static
int32_t
getFuncInfo
(
SFunctionNode
*
pFunc
)
{
char
msg
[
128
]
=
{
0
};
return
fmGetFuncInfo
(
pFunc
,
msg
,
sizeof
(
msg
));
...
...
source/libs/planner/src/planOptimizer.c
浏览文件 @
0228caaa
...
...
@@ -2120,6 +2120,22 @@ static int32_t rewriteUniqueOptimize(SOptimizeContext* pCxt, SLogicSubplan* pLog
return
rewriteUniqueOptimizeImpl
(
pCxt
,
pLogicSubplan
,
pIndef
);
}
static
EDealRes
lastRowScanOptHasTagImpl
(
SNode
*
pNode
,
void
*
pContext
)
{
if
(
QUERY_NODE_COLUMN
==
nodeType
(
pNode
))
{
if
(
COLUMN_TYPE_TAG
==
((
SColumnNode
*
)
pNode
)
->
colType
||
COLUMN_TYPE_TBNAME
==
((
SColumnNode
*
)
pNode
)
->
colType
)
{
*
(
bool
*
)
pContext
=
true
;
return
DEAL_RES_END
;
}
}
return
DEAL_RES_CONTINUE
;
}
static
bool
lastRowScanOptHasTag
(
SNode
*
pExpr
)
{
bool
hasTag
=
false
;
nodesWalkExpr
(
pExpr
,
lastRowScanOptHasTagImpl
,
&
hasTag
);
return
hasTag
;
}
static
bool
lastRowScanOptMayBeOptimized
(
SLogicNode
*
pNode
)
{
if
(
QUERY_NODE_LOGIC_PLAN_AGG
!=
nodeType
(
pNode
)
||
1
!=
LIST_LENGTH
(
pNode
->
pChildren
)
||
QUERY_NODE_LOGIC_PLAN_SCAN
!=
nodeType
(
nodesListGetNode
(
pNode
->
pChildren
,
0
)))
{
...
...
@@ -2134,12 +2150,22 @@ static bool lastRowScanOptMayBeOptimized(SLogicNode* pNode) {
return
false
;
}
bool
hasLastFunc
=
false
;
bool
hasSelectFunc
=
false
;
SNode
*
pFunc
=
NULL
;
FOREACH
(
pFunc
,
((
SAggLogicNode
*
)
pNode
)
->
pAggFuncs
)
{
if
(
FUNCTION_TYPE_LAST_ROW
!=
((
SFunctionNode
*
)
pFunc
)
->
funcType
&&
FUNCTION_TYPE_LAST
!=
((
SFunctionNode
*
)
pFunc
)
->
funcType
&&
FUNCTION_TYPE_SELECT_VALUE
!=
((
SFunctionNode
*
)
pFunc
)
->
funcType
&&
FUNCTION_TYPE_GROUP_KEY
!=
((
SFunctionNode
*
)
pFunc
)
->
funcType
)
{
SFunctionNode
*
pAggFunc
=
(
SFunctionNode
*
)
pFunc
;
if
(
FUNCTION_TYPE_LAST
==
pAggFunc
->
funcType
)
{
if
(
hasSelectFunc
||
lastRowScanOptHasTag
(
nodesListGetNode
(
pAggFunc
->
pParameterList
,
0
)))
{
return
false
;
}
hasLastFunc
=
true
;
}
else
if
(
FUNCTION_TYPE_SELECT_VALUE
==
pAggFunc
->
funcType
||
FUNCTION_TYPE_GROUP_KEY
==
pAggFunc
->
funcType
)
{
if
(
hasLastFunc
)
{
return
false
;
}
hasSelectFunc
=
true
;
}
else
if
(
FUNCTION_TYPE_LAST_ROW
!=
pAggFunc
->
funcType
)
{
return
false
;
}
}
...
...
@@ -2147,6 +2173,31 @@ static bool lastRowScanOptMayBeOptimized(SLogicNode* pNode) {
return
true
;
}
typedef
struct
SLastRowScanOptSetColDataTypeCxt
{
bool
doAgg
;
SNodeList
*
pLastCols
;
}
SLastRowScanOptSetColDataTypeCxt
;
static
EDealRes
lastRowScanOptSetColDataType
(
SNode
*
pNode
,
void
*
pContext
)
{
if
(
QUERY_NODE_COLUMN
==
nodeType
(
pNode
))
{
SLastRowScanOptSetColDataTypeCxt
*
pCxt
=
pContext
;
if
(
pCxt
->
doAgg
)
{
nodesListMakeAppend
(
&
pCxt
->
pLastCols
,
pNode
);
getLastCacheDataType
(
&
(((
SColumnNode
*
)
pNode
)
->
node
.
resType
));
}
else
{
SNode
*
pCol
=
NULL
;
FOREACH
(
pCol
,
pCxt
->
pLastCols
)
{
if
(
nodesEqualNode
(
pCol
,
pNode
))
{
getLastCacheDataType
(
&
(((
SColumnNode
*
)
pNode
)
->
node
.
resType
));
break
;
}
}
}
return
DEAL_RES_IGNORE_CHILD
;
}
return
DEAL_RES_CONTINUE
;
}
static
int32_t
lastRowScanOptimize
(
SOptimizeContext
*
pCxt
,
SLogicSubplan
*
pLogicSubplan
)
{
SAggLogicNode
*
pAgg
=
(
SAggLogicNode
*
)
optFindPossibleNode
(
pLogicSubplan
->
pNode
,
lastRowScanOptMayBeOptimized
);
...
...
@@ -2154,22 +2205,35 @@ static int32_t lastRowScanOptimize(SOptimizeContext* pCxt, SLogicSubplan* pLogic
return
TSDB_CODE_SUCCESS
;
}
SNode
*
pNode
=
NULL
;
SLastRowScanOptSetColDataTypeCxt
cxt
=
{.
doAgg
=
true
,
.
pLastCols
=
NULL
};
SNode
*
pNode
=
NULL
;
FOREACH
(
pNode
,
pAgg
->
pAggFuncs
)
{
SFunctionNode
*
pFunc
=
(
SFunctionNode
*
)
pNode
;
if
(
FUNCTION_TYPE_LAST_ROW
==
pFunc
->
funcType
||
FUNCTION_TYPE_LAST
==
pFunc
->
funcType
)
{
int32_t
len
=
snprintf
(
pFunc
->
functionName
,
sizeof
(
pFunc
->
functionName
),
"_cache_last_row"
);
int32_t
funcType
=
pFunc
->
funcType
;
if
(
FUNCTION_TYPE_LAST_ROW
==
funcType
||
FUNCTION_TYPE_LAST
==
funcType
)
{
int32_t
len
=
snprintf
(
pFunc
->
functionName
,
sizeof
(
pFunc
->
functionName
),
FUNCTION_TYPE_LAST_ROW
==
funcType
?
"_cache_last_row"
:
"_cache_last"
);
pFunc
->
functionName
[
len
]
=
'\0'
;
int32_t
code
=
fmGetFuncInfo
(
pFunc
,
NULL
,
0
);
if
(
TSDB_CODE_SUCCESS
!=
code
)
{
nodesClearList
(
cxt
.
pLastCols
);
return
code
;
}
if
(
FUNCTION_TYPE_LAST
==
funcType
)
{
nodesWalkExpr
(
nodesListGetNode
(
pFunc
->
pParameterList
,
0
),
lastRowScanOptSetColDataType
,
&
cxt
);
}
}
}
SScanLogicNode
*
pScan
=
(
SScanLogicNode
*
)
nodesListGetNode
(
pAgg
->
node
.
pChildren
,
0
);
pScan
->
scanType
=
SCAN_TYPE_LAST_ROW
;
pScan
->
igLastNull
=
pAgg
->
hasLast
?
true
:
false
;
if
(
NULL
!=
cxt
.
pLastCols
)
{
cxt
.
doAgg
=
false
;
nodesWalkExprs
(
pScan
->
pScanCols
,
lastRowScanOptSetColDataType
,
&
cxt
);
nodesWalkExprs
(
pScan
->
pScanPseudoCols
,
lastRowScanOptSetColDataType
,
&
cxt
);
nodesClearList
(
cxt
.
pLastCols
);
}
pAgg
->
hasLastRow
=
false
;
pAgg
->
hasLast
=
false
;
...
...
source/libs/planner/test/planBasicTest.cpp
浏览文件 @
0228caaa
...
...
@@ -105,24 +105,6 @@ TEST_F(PlanBasicTest, interpFunc) {
run
(
"SELECT _IROWTS, INTERP(c1) FROM t1 RANGE('2017-7-14 18:00:00', '2017-7-14 19:00:00') EVERY(5s) FILL(LINEAR)"
);
}
TEST_F
(
PlanBasicTest
,
lastRowFunc
)
{
useDb
(
"root"
,
"cache_db"
);
run
(
"SELECT LAST_ROW(c1) FROM t1"
);
run
(
"SELECT LAST_ROW(*) FROM t1"
);
run
(
"SELECT LAST_ROW(c1, c2) FROM t1"
);
run
(
"SELECT LAST_ROW(c1), c2 FROM t1"
);
run
(
"SELECT LAST_ROW(c1) FROM st1"
);
run
(
"SELECT LAST_ROW(c1) FROM st1 PARTITION BY TBNAME"
);
run
(
"SELECT LAST_ROW(c1), SUM(c3) FROM t1"
);
}
TEST_F
(
PlanBasicTest
,
lastRowFuncWithoutCache
)
{
useDb
(
"root"
,
"test"
);
...
...
source/libs/planner/test/planOptimizeTest.cpp
浏览文件 @
0228caaa
...
...
@@ -109,9 +109,28 @@ TEST_F(PlanOptimizeTest, mergeProjects) {
TEST_F
(
PlanOptimizeTest
,
pushDownProjectCond
)
{
useDb
(
"root"
,
"test"
);
run
(
"select 1-abs(c1) from (select unique(c1) c1 from st1s3) where 1-c1>5 order by 1 nulls first"
);
}
TEST_F
(
PlanOptimizeTest
,
LastRowScan
)
{
useDb
(
"root"
,
"cache_db"
);
run
(
"SELECT LAST_ROW(c1), c2 FROM t1"
);
run
(
"SELECT LAST_ROW(c1), c2, tag1, tbname FROM st1"
);
run
(
"SELECT LAST_ROW(c1) FROM st1 PARTITION BY TBNAME"
);
run
(
"SELECT LAST_ROW(c1), SUM(c3) FROM t1"
);
run
(
"SELECT LAST_ROW(tag1) FROM st1"
);
run
(
"SELECT LAST(c1) FROM st1"
);
run
(
"SELECT LAST(c1), c2 FROM st1"
);
}
TEST_F
(
PlanOptimizeTest
,
tagScan
)
{
useDb
(
"root"
,
"test"
);
run
(
"select tag1 from st1 group by tag1"
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录