Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
3954edb3
T
TDengine
项目概览
taosdata
/
TDengine
接近 2 年 前同步成功
通知
1192
Star
22018
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看板
提交
3954edb3
编写于
6月 13, 2022
作者:
X
Xiaoyu Wang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: avg function rewrite
上级
3eda47e1
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
83 addition
and
9 deletion
+83
-9
include/libs/function/functionMgt.h
include/libs/function/functionMgt.h
+4
-0
source/libs/function/inc/builtins.h
source/libs/function/inc/builtins.h
+2
-0
source/libs/function/inc/functionMgtInt.h
source/libs/function/inc/functionMgtInt.h
+1
-0
source/libs/function/src/builtins.c
source/libs/function/src/builtins.c
+44
-0
source/libs/function/src/functionMgt.c
source/libs/function/src/functionMgt.c
+11
-0
source/libs/parser/src/parTranslater.c
source/libs/parser/src/parTranslater.c
+12
-9
source/libs/parser/test/parInsertTest.cpp
source/libs/parser/test/parInsertTest.cpp
+1
-0
source/libs/planner/test/planGroupByTest.cpp
source/libs/planner/test/planGroupByTest.cpp
+8
-0
未找到文件。
include/libs/function/functionMgt.h
浏览文件 @
3954edb3
...
...
@@ -171,6 +171,10 @@ bool fmIsRepeatScanFunc(int32_t funcId);
bool
fmIsUserDefinedFunc
(
int32_t
funcId
);
bool
fmIsDistExecFunc
(
int32_t
funcId
);
bool
fmIsForbidFillFunc
(
int32_t
funcId
);
bool
fmIsForbidStreamFunc
(
int32_t
funcId
);
bool
fmNeedRewrite
(
int32_t
funcId
);
int32_t
fmRewriteFunc
(
SNode
**
pFunc
);
int32_t
fmGetDistMethod
(
const
SFunctionNode
*
pFunc
,
SFunctionNode
**
pPartialFunc
,
SFunctionNode
**
pMergeFunc
);
...
...
source/libs/function/inc/builtins.h
浏览文件 @
3954edb3
...
...
@@ -23,6 +23,7 @@ extern "C" {
#include "functionMgtInt.h"
typedef
int32_t
(
*
FTranslateFunc
)(
SFunctionNode
*
pFunc
,
char
*
pErrBuf
,
int32_t
len
);
typedef
int32_t
(
*
FRewriteFunc
)(
SNode
**
pFunc
);
typedef
EFuncDataRequired
(
*
FFuncDataRequired
)(
SFunctionNode
*
pFunc
,
STimeWindow
*
pTimeWindow
);
typedef
struct
SBuiltinFuncDefinition
{
...
...
@@ -30,6 +31,7 @@ typedef struct SBuiltinFuncDefinition {
EFunctionType
type
;
uint64_t
classification
;
FTranslateFunc
translateFunc
;
FRewriteFunc
rewriteFunc
;
FFuncDataRequired
dataRequiredFunc
;
FExecGetEnv
getEnvFunc
;
FExecInit
initFunc
;
...
...
source/libs/function/inc/functionMgtInt.h
浏览文件 @
3954edb3
...
...
@@ -42,6 +42,7 @@ extern "C" {
#define FUNC_MGT_SELECT_FUNC FUNC_MGT_FUNC_CLASSIFICATION_MASK(13)
#define FUNC_MGT_REPEAT_SCAN_FUNC FUNC_MGT_FUNC_CLASSIFICATION_MASK(14)
#define FUNC_MGT_FORBID_FILL_FUNC FUNC_MGT_FUNC_CLASSIFICATION_MASK(15)
#define FUNC_MGT_FORBID_STREAM_FUNC FUNC_MGT_FUNC_CLASSIFICATION_MASK(16)
#define FUNC_MGT_TEST_MASK(val, mask) (((val) & (mask)) != 0)
...
...
source/libs/function/src/builtins.c
浏览文件 @
3954edb3
...
...
@@ -1333,6 +1333,49 @@ static bool getBlockDistFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv
return
true
;
}
static
int32_t
rewriteAvg
(
SNode
**
pFunc
)
{
SOperatorNode
*
pOper
=
(
SOperatorNode
*
)
nodesMakeNode
(
QUERY_NODE_OPERATOR
);
if
(
NULL
==
pOper
)
{
return
TSDB_CODE_OUT_OF_MEMORY
;
}
SFunctionNode
*
pAvg
=
(
SFunctionNode
*
)
*
pFunc
;
pOper
->
node
.
resType
=
pAvg
->
node
.
resType
;
strcpy
(
pOper
->
node
.
aliasName
,
pAvg
->
node
.
aliasName
);
pOper
->
opType
=
OP_TYPE_DIV
;
pOper
->
pLeft
=
nodesMakeNode
(
QUERY_NODE_FUNCTION
);
pOper
->
pRight
=
nodesMakeNode
(
QUERY_NODE_FUNCTION
);
if
(
NULL
==
pOper
->
pLeft
||
NULL
==
pOper
->
pRight
)
{
nodesDestroyNode
((
SNode
*
)
pOper
);
return
TSDB_CODE_OUT_OF_MEMORY
;
}
SFunctionNode
*
pSum
=
(
SFunctionNode
*
)
pOper
->
pLeft
;
strcpy
(
pSum
->
functionName
,
"sum"
);
pSum
->
pParameterList
=
nodesCloneList
(
pAvg
->
pParameterList
);
if
(
NULL
==
pSum
->
pParameterList
)
{
nodesDestroyNode
((
SNode
*
)
pOper
);
return
TSDB_CODE_OUT_OF_MEMORY
;
}
char
msgBuf
[
64
]
=
{
0
};
int32_t
code
=
fmGetFuncInfo
(
pSum
,
msgBuf
,
sizeof
(
msgBuf
));
if
(
TSDB_CODE_SUCCESS
==
code
)
{
SFunctionNode
*
pCount
=
(
SFunctionNode
*
)
pOper
->
pRight
;
strcpy
(
pCount
->
functionName
,
"count"
);
TSWAP
(
pCount
->
pParameterList
,
pAvg
->
pParameterList
);
code
=
fmGetFuncInfo
(
pCount
,
msgBuf
,
sizeof
(
msgBuf
));
}
if
(
TSDB_CODE_SUCCESS
==
code
)
{
nodesDestroyNode
((
SNode
*
)
pAvg
);
*
pFunc
=
(
SNode
*
)
pOper
;
}
else
{
nodesDestroyNode
((
SNode
*
)
pOper
);
}
return
code
;
}
// clang-format off
const
SBuiltinFuncDefinition
funcMgtBuiltins
[]
=
{
{
...
...
@@ -1422,6 +1465,7 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
.
type
=
FUNCTION_TYPE_AVG
,
.
classification
=
FUNC_MGT_AGG_FUNC
,
.
translateFunc
=
translateInNumOutDou
,
.
rewriteFunc
=
rewriteAvg
,
.
getEnvFunc
=
getAvgFuncEnv
,
.
initFunc
=
avgFunctionSetup
,
.
processFunc
=
avgFunction
,
...
...
source/libs/function/src/functionMgt.c
浏览文件 @
3954edb3
...
...
@@ -161,6 +161,8 @@ bool fmIsUserDefinedFunc(int32_t funcId) { return funcId > FUNC_UDF_ID_START; }
bool
fmIsForbidFillFunc
(
int32_t
funcId
)
{
return
isSpecificClassifyFunc
(
funcId
,
FUNC_MGT_FORBID_FILL_FUNC
);
}
bool
fmIsForbidStreamFunc
(
int32_t
funcId
)
{
return
isSpecificClassifyFunc
(
funcId
,
FUNC_MGT_FORBID_STREAM_FUNC
);
}
void
fmFuncMgtDestroy
()
{
void
*
m
=
gFunMgtService
.
pFuncNameHashTable
;
if
(
m
!=
NULL
&&
atomic_val_compare_exchange_ptr
((
void
**
)
&
gFunMgtService
.
pFuncNameHashTable
,
m
,
0
)
==
m
)
{
...
...
@@ -297,3 +299,12 @@ int32_t fmGetDistMethod(const SFunctionNode* pFunc, SFunctionNode** pPartialFunc
return
code
;
}
bool
fmNeedRewrite
(
int32_t
funcId
)
{
if
(
fmIsUserDefinedFunc
(
funcId
))
{
return
false
;
}
return
NULL
!=
funcMgtBuiltins
[
funcId
].
rewriteFunc
;
}
int32_t
fmRewriteFunc
(
SNode
**
pFunc
)
{
return
funcMgtBuiltins
[((
SFunctionNode
*
)
*
pFunc
)
->
funcId
].
rewriteFunc
(
pFunc
);
}
source/libs/parser/src/parTranslater.c
浏览文件 @
3954edb3
...
...
@@ -1076,29 +1076,32 @@ static void setFuncClassification(SSelectStmt* pSelect, SFunctionNode* pFunc) {
}
}
static
EDealRes
translateFunction
(
STranslateContext
*
pCxt
,
SFunctionNode
*
pFunc
)
{
static
EDealRes
translateFunction
(
STranslateContext
*
pCxt
,
SFunctionNode
*
*
pFunc
)
{
SNode
*
pParam
=
NULL
;
FOREACH
(
pParam
,
pFunc
->
pParameterList
)
{
FOREACH
(
pParam
,
(
*
pFunc
)
->
pParameterList
)
{
if
(
isMultiResFunc
(
pParam
))
{
return
generateDealNodeErrMsg
(
pCxt
,
TSDB_CODE_PAR_WRONG_VALUE_TYPE
,
((
SExprNode
*
)
pParam
)
->
aliasName
);
}
}
pCxt
->
errCode
=
getFuncInfo
(
pCxt
,
pFunc
);
pCxt
->
errCode
=
getFuncInfo
(
pCxt
,
*
pFunc
);
if
(
TSDB_CODE_SUCCESS
==
pCxt
->
errCode
)
{
pCxt
->
errCode
=
translateAggFunc
(
pCxt
,
pFunc
);
pCxt
->
errCode
=
translateAggFunc
(
pCxt
,
*
pFunc
);
}
if
(
TSDB_CODE_SUCCESS
==
pCxt
->
errCode
)
{
pCxt
->
errCode
=
translateScanPseudoColumnFunc
(
pCxt
,
pFunc
);
pCxt
->
errCode
=
translateScanPseudoColumnFunc
(
pCxt
,
*
pFunc
);
}
if
(
TSDB_CODE_SUCCESS
==
pCxt
->
errCode
)
{
pCxt
->
errCode
=
translateIndefiniteRowsFunc
(
pCxt
,
pFunc
);
pCxt
->
errCode
=
translateIndefiniteRowsFunc
(
pCxt
,
*
pFunc
);
}
if
(
TSDB_CODE_SUCCESS
==
pCxt
->
errCode
)
{
pCxt
->
errCode
=
translateForbidFillFunc
(
pCxt
,
pFunc
);
pCxt
->
errCode
=
translateForbidFillFunc
(
pCxt
,
*
pFunc
);
}
if
(
TSDB_CODE_SUCCESS
==
pCxt
->
errCode
)
{
setFuncClassification
(
pCxt
->
pCurrSelectStmt
,
pFunc
);
setFuncClassification
(
pCxt
->
pCurrSelectStmt
,
*
pFunc
);
}
if
(
TSDB_CODE_SUCCESS
==
pCxt
->
errCode
&&
fmNeedRewrite
((
*
pFunc
)
->
funcId
))
{
pCxt
->
errCode
=
fmRewriteFunc
((
SNode
**
)
pFunc
);
}
return
TSDB_CODE_SUCCESS
==
pCxt
->
errCode
?
DEAL_RES_CONTINUE
:
DEAL_RES_ERROR
;
}
...
...
@@ -1123,7 +1126,7 @@ static EDealRes doTranslateExpr(SNode** pNode, void* pContext) {
case
QUERY_NODE_OPERATOR
:
return
translateOperator
(
pCxt
,
(
SOperatorNode
**
)
pNode
);
case
QUERY_NODE_FUNCTION
:
return
translateFunction
(
pCxt
,
(
SFunctionNode
*
)
*
pNode
);
return
translateFunction
(
pCxt
,
(
SFunctionNode
*
*
)
pNode
);
case
QUERY_NODE_LOGIC_CONDITION
:
return
translateLogicCond
(
pCxt
,
(
SLogicConditionNode
*
)
*
pNode
);
case
QUERY_NODE_TEMP_TABLE
:
...
...
source/libs/parser/test/parInsertTest.cpp
浏览文件 @
3954edb3
...
...
@@ -35,6 +35,7 @@ string toString(int32_t code) { return tstrerror(code); }
// [...];
class
InsertTest
:
public
Test
{
protected:
InsertTest
()
:
res_
(
nullptr
)
{}
~
InsertTest
()
{
reset
();
}
void
setDatabase
(
const
string
&
acctId
,
const
string
&
db
)
{
...
...
source/libs/planner/test/planGroupByTest.cpp
浏览文件 @
3954edb3
...
...
@@ -53,6 +53,14 @@ TEST_F(PlanGroupByTest, aggFunc) {
run
(
"SELECT SUM(10), COUNT(c1) FROM t1 GROUP BY c2"
);
}
TEST_F
(
PlanGroupByTest
,
rewriteFunc
)
{
useDb
(
"root"
,
"test"
);
run
(
"SELECT AVG(c1) FROM t1"
);
run
(
"SELECT AVG(c1) FROM t1 GROUP BY c2"
);
}
TEST_F
(
PlanGroupByTest
,
selectFunc
)
{
useDb
(
"root"
,
"test"
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录