Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
9c68bde6
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看板
提交
9c68bde6
编写于
6月 26, 2022
作者:
X
Xiaoyu Wang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: tail function rewrite to statement
上级
ab95a49f
变更
2
展开全部
显示空白变更内容
内联
并排
Showing
2 changed file
with
94 addition
and
220 deletion
+94
-220
source/libs/parser/src/parTranslater.c
source/libs/parser/src/parTranslater.c
+0
-108
source/libs/planner/src/planOptimizer.c
source/libs/planner/src/planOptimizer.c
+94
-112
未找到文件。
source/libs/parser/src/parTranslater.c
浏览文件 @
9c68bde6
...
...
@@ -2456,111 +2456,6 @@ static int32_t rewriteUniqueStmt(STranslateContext* pCxt, SSelectStmt* pSelect)
return
cxt
.
pTranslateCxt
->
errCode
;
}
typedef
struct
SRwriteTailCxt
{
STranslateContext
*
pTranslateCxt
;
int64_t
limit
;
int64_t
offset
;
}
SRwriteTailCxt
;
static
EDealRes
rewriteTailFunc
(
SNode
**
pNode
,
void
*
pContext
)
{
SRwriteTailCxt
*
pCxt
=
pContext
;
if
(
QUERY_NODE_FUNCTION
==
nodeType
(
*
pNode
))
{
SFunctionNode
*
pFunc
=
(
SFunctionNode
*
)
*
pNode
;
if
(
FUNCTION_TYPE_TAIL
==
pFunc
->
funcType
)
{
pCxt
->
limit
=
((
SValueNode
*
)
nodesListGetNode
(
pFunc
->
pParameterList
,
1
))
->
datum
.
i
;
if
(
3
==
LIST_LENGTH
(
pFunc
->
pParameterList
))
{
pCxt
->
offset
=
((
SValueNode
*
)
nodesListGetNode
(
pFunc
->
pParameterList
,
2
))
->
datum
.
i
;
}
SNode
*
pExpr
=
nodesListGetNode
(
pFunc
->
pParameterList
,
0
);
strcpy
(((
SExprNode
*
)
pExpr
)
->
aliasName
,
((
SExprNode
*
)
*
pNode
)
->
aliasName
);
NODES_CLEAR_LIST
(
pFunc
->
pParameterList
);
nodesDestroyNode
(
*
pNode
);
*
pNode
=
pExpr
;
return
DEAL_RES_IGNORE_CHILD
;
}
}
return
DEAL_RES_CONTINUE
;
}
static
int32_t
createLimieNode
(
SRwriteTailCxt
*
pCxt
,
SLimitNode
**
pOutput
)
{
*
pOutput
=
(
SLimitNode
*
)
nodesMakeNode
(
QUERY_NODE_LIMIT
);
if
(
NULL
==
*
pOutput
)
{
return
TSDB_CODE_OUT_OF_MEMORY
;
}
(
*
pOutput
)
->
limit
=
pCxt
->
limit
;
(
*
pOutput
)
->
offset
=
pCxt
->
offset
;
return
TSDB_CODE_SUCCESS
;
}
static
SNode
*
createOrderByExpr
(
STranslateContext
*
pCxt
)
{
SOrderByExprNode
*
pOrder
=
(
SOrderByExprNode
*
)
nodesMakeNode
(
QUERY_NODE_ORDER_BY_EXPR
);
if
(
NULL
==
pOrder
)
{
return
NULL
;
}
pCxt
->
errCode
=
createPrimaryKeyCol
(
pCxt
,
&
pOrder
->
pExpr
);
if
(
TSDB_CODE_SUCCESS
!=
pCxt
->
errCode
)
{
nodesDestroyNode
((
SNode
*
)
pOrder
);
return
NULL
;
}
pOrder
->
order
=
ORDER_DESC
;
pOrder
->
nullOrder
=
NULL_ORDER_FIRST
;
return
(
SNode
*
)
pOrder
;
}
static
int32_t
rewriteTailStmtInplace
(
STranslateContext
*
pCxt
,
SSelectStmt
*
pSelect
)
{
SRwriteTailCxt
cxt
=
{.
pTranslateCxt
=
pCxt
,
.
limit
=
-
1
,
.
offset
=
-
1
};
nodesRewriteExprs
(
pSelect
->
pProjectionList
,
rewriteTailFunc
,
&
cxt
);
int32_t
code
=
nodesListMakeStrictAppend
(
&
pSelect
->
pOrderByList
,
createOrderByExpr
(
pCxt
));
if
(
TSDB_CODE_SUCCESS
==
code
)
{
code
=
createLimieNode
(
&
cxt
,
&
pSelect
->
pLimit
);
}
pSelect
->
hasIndefiniteRowsFunc
=
false
;
pSelect
->
groupSort
=
(
NULL
!=
pSelect
->
pPartitionByList
);
return
code
;
}
static
int32_t
rewriteTailStmtSubquery
(
STranslateContext
*
pCxt
,
SSelectStmt
*
pSelect
)
{
SSelectStmt
*
pSubquery
=
(
SSelectStmt
*
)
nodesMakeNode
(
QUERY_NODE_SELECT_STMT
);
if
(
NULL
==
pSubquery
)
{
return
TSDB_CODE_OUT_OF_MEMORY
;
}
TSWAP
(
pSubquery
->
pProjectionList
,
pSelect
->
pProjectionList
);
return
TSDB_CODE_PAR_INTERNAL_ERROR
;
}
/* case 1:
* in: select tail(expr, k, f) from t where_clause
* out: select expr from t where_clause order by _rowts desc limit k offset f
*
* case 2:
* in: select tail(expr, k, f) from t where_clause partition_by_clause
* out: select expr from t where_clause partition_by_clause sort by _rowts desc limit k offset f
*
* case 3:
* in: select tail(expr, k, f) from t where_clause order_by_clause limit_clause
* out: select expr from (
* select expr from t where_clause order by _rowts desc limit k offset f
* ) order_by_clause limit_clause
*
* case 4:
* in: select tail(expr, k, f) from t where_clause partition_by_clause limit_clause
* out: select expr from (
* select expr, part_key_list from t where_clause partition_by_clause sort by _rowts desc limit k offset f
* ) partition_by_clause limit_clause
*/
static
int32_t
rewriteTailStmt
(
STranslateContext
*
pCxt
,
SSelectStmt
*
pSelect
)
{
if
(
!
pSelect
->
hasTailFunc
)
{
return
TSDB_CODE_SUCCESS
;
}
if
(
NULL
==
pSelect
->
pOrderByList
&&
NULL
==
pSelect
->
pLimit
&&
NULL
==
pSelect
->
pSlimit
)
{
return
rewriteTailStmtInplace
(
pCxt
,
pSelect
);
}
else
{
return
rewriteTailStmtSubquery
(
pCxt
,
pSelect
);
}
}
typedef
struct
SReplaceOrderByAliasCxt
{
STranslateContext
*
pTranslateCxt
;
SNodeList
*
pProjectionList
;
...
...
@@ -2637,9 +2532,6 @@ static int32_t translateSelectFrom(STranslateContext* pCxt, SSelectStmt* pSelect
if
(
TSDB_CODE_SUCCESS
==
code
)
{
code
=
rewriteUniqueStmt
(
pCxt
,
pSelect
);
}
// if (TSDB_CODE_SUCCESS == code) {
// code = rewriteTailStmt(pCxt, pSelect);
// }
if
(
TSDB_CODE_SUCCESS
==
code
)
{
code
=
rewriteTimelineFunc
(
pCxt
,
pSelect
);
}
...
...
source/libs/planner/src/planOptimizer.c
浏览文件 @
9c68bde6
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录