Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
ff804598
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看板
提交
ff804598
编写于
4月 18, 2022
作者:
X
Xiaoyu Wang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: implementation of parser and planner for stmt API
上级
ad940906
变更
16
展开全部
隐藏空白更改
内联
并排
Showing
16 changed file
with
1189 addition
and
1006 deletion
+1189
-1006
include/libs/nodes/plannodes.h
include/libs/nodes/plannodes.h
+3
-0
include/libs/nodes/querynodes.h
include/libs/nodes/querynodes.h
+1
-0
include/libs/planner/planner.h
include/libs/planner/planner.h
+5
-0
source/libs/nodes/src/nodesTraverseFuncs.c
source/libs/nodes/src/nodesTraverseFuncs.c
+285
-64
source/libs/parser/inc/parAst.h
source/libs/parser/inc/parAst.h
+2
-1
source/libs/parser/inc/sql.y
source/libs/parser/inc/sql.y
+2
-2
source/libs/parser/src/parAstCreater.c
source/libs/parser/src/parAstCreater.c
+10
-9
source/libs/parser/src/parAstParser.c
source/libs/parser/src/parAstParser.c
+0
-1
source/libs/parser/src/parTranslater.c
source/libs/parser/src/parTranslater.c
+3
-0
source/libs/parser/src/sql.c
source/libs/parser/src/sql.c
+470
-924
source/libs/parser/test/parserTestUtil.h
source/libs/parser/test/parserTestUtil.h
+6
-1
source/libs/planner/src/planner.c
source/libs/planner/src/planner.c
+104
-0
source/libs/planner/test/planStmtTest.cpp
source/libs/planner/test/planStmtTest.cpp
+54
-0
source/libs/planner/test/planTestUtil.cpp
source/libs/planner/test/planTestUtil.cpp
+205
-0
source/libs/planner/test/planTestUtil.h
source/libs/planner/test/planTestUtil.h
+35
-0
source/libs/planner/test/plannerTest.cpp
source/libs/planner/test/plannerTest.cpp
+4
-4
未找到文件。
include/libs/nodes/plannodes.h
浏览文件 @
ff804598
...
...
@@ -355,8 +355,11 @@ typedef struct SQueryPlan {
int32_t
numOfSubplans
;
SNodeList
*
pSubplans
;
// Element is SNodeListNode. The execution level of subplan, starting from 0.
SExplainInfo
explainInfo
;
SNodeList
*
pPlaceholderValues
;
}
SQueryPlan
;
void
nodesWalkPhysiPlan
(
SNode
*
pNode
,
FNodeWalker
walker
,
void
*
pContext
);
#ifdef __cplusplus
}
#endif
...
...
include/libs/nodes/querynodes.h
浏览文件 @
ff804598
...
...
@@ -82,6 +82,7 @@ typedef struct SValueNode {
bool
isDuration
;
bool
translate
;
bool
genByCalc
;
int16_t
placeholderNo
;
union
{
bool
b
;
int64_t
i
;
...
...
include/libs/planner/planner.h
浏览文件 @
ff804598
...
...
@@ -21,6 +21,7 @@ extern "C" {
#endif
#include "plannodes.h"
#include "taos.h"
typedef
struct
SPlanContext
{
uint64_t
queryId
;
...
...
@@ -32,6 +33,7 @@ typedef struct SPlanContext {
bool
showRewrite
;
int8_t
triggerType
;
int64_t
watermark
;
bool
isStmtQuery
;
}
SPlanContext
;
// Create the physical plan for the query, according to the AST.
...
...
@@ -43,6 +45,9 @@ int32_t qCreateQueryPlan(SPlanContext* pCxt, SQueryPlan** pPlan, SArray* pExecNo
// @pSource one execution location of this group of datasource subplans
int32_t
qSetSubplanExecutionNode
(
SSubplan
*
pSubplan
,
int32_t
groupId
,
SDownstreamSourceNode
*
pSource
);
typedef
TAOS_MULTI_BIND
TAOS_BIND_v2
;
// todo remove
int32_t
qStmtBindParam
(
SQueryPlan
*
pPlan
,
TAOS_BIND_v2
*
pParams
);
// Convert to subplan to string for the scheduler to send to the executor
int32_t
qSubPlanToString
(
const
SSubplan
*
pSubplan
,
char
**
pStr
,
int32_t
*
pLen
);
int32_t
qStringToSubplan
(
const
char
*
pStr
,
SSubplan
**
pSubplan
);
...
...
source/libs/nodes/src/nodesTraverseFuncs.c
浏览文件 @
ff804598
此差异已折叠。
点击以展开。
source/libs/parser/inc/parAst.h
浏览文件 @
ff804598
...
...
@@ -32,6 +32,7 @@ typedef struct SAstCreateContext {
bool
notSupport
;
bool
valid
;
SNode
*
pRootNode
;
int16_t
placeholderNo
;
}
SAstCreateContext
;
typedef
enum
EDatabaseOptionType
{
...
...
@@ -86,7 +87,7 @@ SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pC
SNode
*
createValueNode
(
SAstCreateContext
*
pCxt
,
int32_t
dataType
,
const
SToken
*
pLiteral
);
SNode
*
createDurationValueNode
(
SAstCreateContext
*
pCxt
,
const
SToken
*
pLiteral
);
SNode
*
createDefaultDatabaseCondValue
(
SAstCreateContext
*
pCxt
);
SNode
*
createPlaceholderValueNode
(
SAstCreateContext
*
pCxt
);
SNode
*
createPlaceholderValueNode
(
SAstCreateContext
*
pCxt
,
const
SToken
*
pLiteral
);
SNode
*
setProjectionAlias
(
SAstCreateContext
*
pCxt
,
SNode
*
pNode
,
const
SToken
*
pAlias
);
SNode
*
createLogicConditionNode
(
SAstCreateContext
*
pCxt
,
ELogicConditionType
type
,
SNode
*
pParam1
,
SNode
*
pParam2
);
SNode
*
createOperatorNode
(
SAstCreateContext
*
pCxt
,
EOperatorType
type
,
SNode
*
pLeft
,
SNode
*
pRight
);
...
...
source/libs/parser/inc/sql.y
浏览文件 @
ff804598
...
...
@@ -465,8 +465,8 @@ literal(A) ::= NK_STRING(B).
literal(A) ::= NK_BOOL(B). { A = createRawExprNode(pCxt, &B, createValueNode(pCxt, TSDB_DATA_TYPE_BOOL, &B)); }
literal(A) ::= TIMESTAMP(B) NK_STRING(C). { A = createRawExprNodeExt(pCxt, &B, &C, createValueNode(pCxt, TSDB_DATA_TYPE_TIMESTAMP, &C)); }
literal(A) ::= duration_literal(B). { A = B; }
literal(A) ::= NULL(B). { A = createRawExprNode(pCxt, &B, createValueNode(pCxt, TSDB_DATA_TYPE_NULL,
NULL
)); }
literal(A) ::= NK_QUESTION(B). { A = createRawExprNode(pCxt, &B, createPlaceholderValueNode(pCxt)); }
literal(A) ::= NULL(B). { A = createRawExprNode(pCxt, &B, createValueNode(pCxt, TSDB_DATA_TYPE_NULL,
&B
)); }
literal(A) ::= NK_QUESTION(B). { A = createRawExprNode(pCxt, &B, createPlaceholderValueNode(pCxt
, &B
)); }
duration_literal(A) ::= NK_VARIABLE(B). { A = createRawExprNode(pCxt, &B, createDurationValueNode(pCxt, &B)); }
...
...
source/libs/parser/src/parAstCreater.c
浏览文件 @
ff804598
...
...
@@ -44,6 +44,7 @@ void initAstCreateContext(SParseContext* pParseCxt, SAstCreateContext* pCxt) {
pCxt
->
notSupport
=
false
;
pCxt
->
valid
=
true
;
pCxt
->
pRootNode
=
NULL
;
pCxt
->
placeholderNo
=
1
;
}
static
void
trimEscape
(
SToken
*
pName
)
{
...
...
@@ -258,14 +259,12 @@ SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pC
SNode
*
createValueNode
(
SAstCreateContext
*
pCxt
,
int32_t
dataType
,
const
SToken
*
pLiteral
)
{
SValueNode
*
val
=
(
SValueNode
*
)
nodesMakeNode
(
QUERY_NODE_VALUE
);
CHECK_OUT_OF_MEM
(
val
);
if
(
NULL
!=
pLiteral
)
{
val
->
literal
=
strndup
(
pLiteral
->
z
,
pLiteral
->
n
);
if
(
TK_NK_ID
!=
pLiteral
->
type
&&
TK_TIMEZONE
!=
pLiteral
->
type
&&
(
IS_VAR_DATA_TYPE
(
dataType
)
||
TSDB_DATA_TYPE_TIMESTAMP
==
dataType
))
{
trimString
(
pLiteral
->
z
,
pLiteral
->
n
,
val
->
literal
,
pLiteral
->
n
);
}
CHECK_OUT_OF_MEM
(
val
->
literal
);
val
->
literal
=
strndup
(
pLiteral
->
z
,
pLiteral
->
n
);
if
(
TK_NK_ID
!=
pLiteral
->
type
&&
TK_TIMEZONE
!=
pLiteral
->
type
&&
(
IS_VAR_DATA_TYPE
(
dataType
)
||
TSDB_DATA_TYPE_TIMESTAMP
==
dataType
))
{
trimString
(
pLiteral
->
z
,
pLiteral
->
n
,
val
->
literal
,
pLiteral
->
n
);
}
CHECK_OUT_OF_MEM
(
val
->
literal
);
val
->
node
.
resType
.
type
=
dataType
;
val
->
node
.
resType
.
bytes
=
IS_VAR_DATA_TYPE
(
dataType
)
?
strlen
(
val
->
literal
)
:
tDataTypes
[
dataType
].
bytes
;
if
(
TSDB_DATA_TYPE_TIMESTAMP
==
dataType
)
{
...
...
@@ -306,10 +305,12 @@ SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
return
(
SNode
*
)
val
;
}
SNode
*
createPlaceholderValueNode
(
SAstCreateContext
*
pCxt
)
{
SNode
*
createPlaceholderValueNode
(
SAstCreateContext
*
pCxt
,
const
SToken
*
pLiteral
)
{
SValueNode
*
val
=
(
SValueNode
*
)
nodesMakeNode
(
QUERY_NODE_VALUE
);
CHECK_OUT_OF_MEM
(
val
);
// todo
val
->
literal
=
strndup
(
pLiteral
->
z
,
pLiteral
->
n
);
CHECK_OUT_OF_MEM
(
val
->
literal
);
val
->
placeholderNo
=
pCxt
->
placeholderNo
++
;
return
(
SNode
*
)
val
;
}
...
...
source/libs/parser/src/parAstParser.c
浏览文件 @
ff804598
...
...
@@ -50,7 +50,6 @@ int32_t parse(SParseContext* pParseCxt, SQuery** pQuery) {
Parse
(
pParser
,
0
,
t0
,
&
cxt
);
goto
abort_parse
;
}
case
TK_NK_QUESTION
:
case
TK_NK_ILLEGAL
:
{
snprintf
(
cxt
.
pQueryCxt
->
pMsg
,
cxt
.
pQueryCxt
->
msgLen
,
"unrecognized token:
\"
%s
\"
"
,
t0
.
z
);
cxt
.
valid
=
false
;
...
...
source/libs/parser/src/parTranslater.c
浏览文件 @
ff804598
...
...
@@ -403,6 +403,9 @@ static EDealRes translateColumn(STranslateContext* pCxt, SColumnNode* pCol) {
static
EDealRes
translateValue
(
STranslateContext
*
pCxt
,
SValueNode
*
pVal
)
{
uint8_t
precision
=
(
NULL
!=
pCxt
->
pCurrStmt
?
pCxt
->
pCurrStmt
->
precision
:
pVal
->
node
.
resType
.
precision
);
pVal
->
node
.
resType
.
precision
=
precision
;
if
(
pVal
->
placeholderNo
>
0
)
{
return
DEAL_RES_CONTINUE
;
}
if
(
pVal
->
isDuration
)
{
if
(
parseNatualDuration
(
pVal
->
literal
,
strlen
(
pVal
->
literal
),
&
pVal
->
datum
.
i
,
&
pVal
->
unit
,
precision
)
!=
TSDB_CODE_SUCCESS
)
{
...
...
source/libs/parser/src/sql.c
浏览文件 @
ff804598
此差异已折叠。
点击以展开。
source/libs/parser/test/parserTestUtil.h
浏览文件 @
ff804598
...
...
@@ -13,4 +13,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
extern
bool
g_isDump
;
\ No newline at end of file
#ifndef PARSER_TEST_UTIL_H
#define PARSER_TEST_UTIL_H
extern
bool
g_isDump
;
#endif // PARSER_TEST_UTIL_H
source/libs/planner/src/planner.c
浏览文件 @
ff804598
...
...
@@ -17,6 +17,31 @@
#include "planInt.h"
typedef
struct
SCollectPlaceholderValuesCxt
{
int32_t
errCode
;
SNodeList
*
pValues
;
}
SCollectPlaceholderValuesCxt
;
static
EDealRes
collectPlaceholderValuesImpl
(
SNode
*
pNode
,
void
*
pContext
)
{
if
(
QUERY_NODE_VALUE
==
nodeType
(
pNode
)
&&
((
SValueNode
*
)
pNode
)
->
placeholderNo
>
0
)
{
SCollectPlaceholderValuesCxt
*
pCxt
=
pContext
;
pCxt
->
errCode
=
nodesListMakeAppend
(
&
pCxt
->
pValues
,
pNode
);
return
TSDB_CODE_SUCCESS
==
pCxt
->
errCode
?
DEAL_RES_IGNORE_CHILD
:
DEAL_RES_ERROR
;
}
return
DEAL_RES_CONTINUE
;
}
static
int32_t
collectPlaceholderValues
(
SPlanContext
*
pCxt
,
SQueryPlan
*
pPlan
)
{
SCollectPlaceholderValuesCxt
cxt
=
{
.
errCode
=
TSDB_CODE_SUCCESS
,
.
pValues
=
NULL
};
nodesWalkPhysiPlan
((
SNode
*
)
pPlan
,
collectPlaceholderValuesImpl
,
&
cxt
);
if
(
TSDB_CODE_SUCCESS
==
cxt
.
errCode
)
{
pPlan
->
pPlaceholderValues
=
cxt
.
pValues
;
}
else
{
nodesDestroyList
(
cxt
.
pValues
);
}
return
cxt
.
errCode
;
}
int32_t
qCreateQueryPlan
(
SPlanContext
*
pCxt
,
SQueryPlan
**
pPlan
,
SArray
*
pExecNodeList
)
{
SLogicNode
*
pLogicNode
=
NULL
;
SLogicSubplan
*
pLogicSubplan
=
NULL
;
...
...
@@ -35,6 +60,9 @@ int32_t qCreateQueryPlan(SPlanContext* pCxt, SQueryPlan** pPlan, SArray* pExecNo
if
(
TSDB_CODE_SUCCESS
==
code
)
{
code
=
createPhysiPlan
(
pCxt
,
pLogicPlan
,
pPlan
,
pExecNodeList
);
}
if
(
TSDB_CODE_SUCCESS
==
code
&&
pCxt
->
isStmtQuery
)
{
code
=
collectPlaceholderValues
(
pCxt
,
*
pPlan
);
}
nodesDestroyNode
(
pLogicNode
);
nodesDestroyNode
(
pLogicSubplan
);
...
...
@@ -73,6 +101,82 @@ int32_t qSetSubplanExecutionNode(SSubplan* subplan, int32_t groupId, SDownstream
return
setSubplanExecutionNode
(
subplan
->
pNode
,
groupId
,
pSource
);
}
static
int32_t
setValueByBindParam
(
SValueNode
*
pVal
,
TAOS_BIND_v2
*
pParam
)
{
if
(
1
==
*
(
pParam
->
is_null
))
{
pVal
->
node
.
resType
.
type
=
TSDB_DATA_TYPE_NULL
;
pVal
->
node
.
resType
.
bytes
=
tDataTypes
[
TSDB_DATA_TYPE_NULL
].
bytes
;
return
TSDB_CODE_SUCCESS
;
}
pVal
->
node
.
resType
.
type
=
pParam
->
buffer_type
;
pVal
->
node
.
resType
.
bytes
=
*
(
pParam
->
length
);
switch
(
pParam
->
buffer_type
)
{
case
TSDB_DATA_TYPE_BOOL
:
pVal
->
datum
.
b
=
*
((
bool
*
)
pParam
->
buffer
);
break
;
case
TSDB_DATA_TYPE_TINYINT
:
pVal
->
datum
.
i
=
*
((
int8_t
*
)
pParam
->
buffer
);
break
;
case
TSDB_DATA_TYPE_SMALLINT
:
pVal
->
datum
.
i
=
*
((
int16_t
*
)
pParam
->
buffer
);
break
;
case
TSDB_DATA_TYPE_INT
:
pVal
->
datum
.
i
=
*
((
int32_t
*
)
pParam
->
buffer
);
break
;
case
TSDB_DATA_TYPE_BIGINT
:
pVal
->
datum
.
i
=
*
((
int64_t
*
)
pParam
->
buffer
);
break
;
case
TSDB_DATA_TYPE_FLOAT
:
pVal
->
datum
.
d
=
*
((
float
*
)
pParam
->
buffer
);
break
;
case
TSDB_DATA_TYPE_DOUBLE
:
pVal
->
datum
.
d
=
*
((
double
*
)
pParam
->
buffer
);
break
;
case
TSDB_DATA_TYPE_VARCHAR
:
case
TSDB_DATA_TYPE_VARBINARY
:
pVal
->
datum
.
p
=
taosMemoryCalloc
(
1
,
pVal
->
node
.
resType
.
bytes
+
VARSTR_HEADER_SIZE
+
1
);
if
(
NULL
==
pVal
->
datum
.
p
)
{
return
TSDB_CODE_OUT_OF_MEMORY
;
}
varDataSetLen
(
pVal
->
datum
.
p
,
pVal
->
node
.
resType
.
bytes
);
strncpy
(
varDataVal
(
pVal
->
datum
.
p
),
(
const
char
*
)
pParam
->
buffer
,
pVal
->
node
.
resType
.
bytes
);
break
;
case
TSDB_DATA_TYPE_TIMESTAMP
:
pVal
->
datum
.
i
=
*
((
int64_t
*
)
pParam
->
buffer
);
break
;
case
TSDB_DATA_TYPE_UTINYINT
:
pVal
->
datum
.
u
=
*
((
uint8_t
*
)
pParam
->
buffer
);
break
;
case
TSDB_DATA_TYPE_USMALLINT
:
pVal
->
datum
.
u
=
*
((
uint16_t
*
)
pParam
->
buffer
);
break
;
case
TSDB_DATA_TYPE_UINT
:
pVal
->
datum
.
u
=
*
((
uint32_t
*
)
pParam
->
buffer
);
break
;
case
TSDB_DATA_TYPE_UBIGINT
:
pVal
->
datum
.
u
=
*
((
uint64_t
*
)
pParam
->
buffer
);
break
;
case
TSDB_DATA_TYPE_NCHAR
:
case
TSDB_DATA_TYPE_JSON
:
case
TSDB_DATA_TYPE_DECIMAL
:
case
TSDB_DATA_TYPE_BLOB
:
case
TSDB_DATA_TYPE_MEDIUMBLOB
:
// todo
default:
break
;
}
pVal
->
translate
=
true
;
return
TSDB_CODE_SUCCESS
;
}
int32_t
qStmtBindParam
(
SQueryPlan
*
pPlan
,
TAOS_BIND_v2
*
pParams
)
{
int32_t
index
=
0
;
SNode
*
pNode
=
NULL
;
FOREACH
(
pNode
,
pPlan
->
pPlaceholderValues
)
{
setValueByBindParam
((
SValueNode
*
)
pNode
,
pParams
+
index
);
}
return
TSDB_CODE_SUCCESS
;
}
int32_t
qSubPlanToString
(
const
SSubplan
*
pSubplan
,
char
**
pStr
,
int32_t
*
pLen
)
{
if
(
SUBPLAN_TYPE_MODIFY
==
pSubplan
->
subplanType
)
{
SDataInserterNode
*
insert
=
(
SDataInserterNode
*
)
pSubplan
->
pDataSink
;
...
...
source/libs/planner/test/planStmtTest.cpp
0 → 100644
浏览文件 @
ff804598
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "planTestUtil.h"
#include "planner.h"
using
namespace
std
;
class
PlanStmtTest
:
public
PlannerTestBase
{
public:
void
prepare
(
const
string
&
sql
)
{
run
(
sql
);
// todo calloc pBindParams_
}
void
bindParam
(
int32_t
val
)
{
TAOS_BIND_v2
*
pBind
=
pBindParams_
+
paramNo_
++
;
pBind
->
buffer_type
=
TSDB_DATA_TYPE_INT
;
pBind
->
num
=
1
;
pBind
->
buffer_length
=
sizeof
(
int32_t
);
pBind
->
buffer
=
taosMemoryCalloc
(
1
,
pBind
->
buffer_length
);
pBind
->
length
=
(
int32_t
*
)
taosMemoryCalloc
(
1
,
sizeof
(
int32_t
));
pBind
->
is_null
=
(
char
*
)
taosMemoryCalloc
(
1
,
sizeof
(
char
));
*
((
int32_t
*
)
pBind
->
buffer
)
=
val
;
*
(
pBind
->
length
)
=
sizeof
(
int32_t
);
*
(
pBind
->
is_null
)
=
0
;
}
void
exec
()
{
// todo
}
private:
TAOS_BIND_v2
*
pBindParams_
;
int32_t
paramNo_
;
};
TEST_F
(
PlanStmtTest
,
stmt
)
{
useDb
(
"root"
,
"test"
);
run
(
"SELECT * FROM t1 where c1 = ?"
);
}
source/libs/planner/test/planTestUtil.cpp
0 → 100644
浏览文件 @
ff804598
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "planTestUtil.h"
#include <algorithm>
#include "cmdnodes.h"
#include "parser.h"
#include "planInt.h"
using
namespace
std
;
using
namespace
testing
;
#define DO_WITH_THROW(func, ...) \
do { \
int32_t code__ = func(__VA_ARGS__); \
if (TSDB_CODE_SUCCESS != code__) { \
throw runtime_error("sql:[" + stmtEnv_.sql_ + "] " #func " code:" + to_string(code__) + ", strerror:" + string(tstrerror(code__)) + ", msg:" + string(stmtEnv_.msgBuf_.data())); \
} \
} while(0);
class
PlannerTestBaseImpl
{
public:
void
useDb
(
const
string
&
acctId
,
const
string
&
db
)
{
caseEnv_
.
acctId_
=
acctId
;
caseEnv_
.
db_
=
db
;
}
void
run
(
const
string
&
sql
)
{
reset
();
try
{
SQuery
*
pQuery
=
nullptr
;
doParseSql
(
sql
,
&
pQuery
);
SPlanContext
cxt
=
{
0
};
setPlanContext
(
pQuery
,
&
cxt
);
SLogicNode
*
pLogicNode
=
nullptr
;
doCreateLogicPlan
(
&
cxt
,
&
pLogicNode
);
doOptimizeLogicPlan
(
&
cxt
,
pLogicNode
);
SLogicSubplan
*
pLogicSubplan
=
nullptr
;
doSplitLogicPlan
(
&
cxt
,
pLogicNode
,
&
pLogicSubplan
);
SQueryLogicPlan
*
pLogicPlan
=
nullptr
;
doScaleOutLogicPlan
(
&
cxt
,
pLogicSubplan
,
&
pLogicPlan
);
SQueryPlan
*
pPlan
=
nullptr
;
doCreatePhysiPlan
(
&
cxt
,
pLogicPlan
,
&
pPlan
,
NULL
);
}
catch
(...)
{
dump
();
throw
;
}
}
private:
struct
caseEnv
{
string
acctId_
;
string
db_
;
};
struct
stmtEnv
{
string
sql_
;
array
<
char
,
1024
>
msgBuf_
;
};
struct
stmtRes
{
string
ast_
;
string
rawLogicPlan_
;
string
optimizedLogicPlan_
;
string
splitLogicPlan_
;
string
scaledLogicPlan_
;
string
physiPlan_
;
};
void
reset
()
{
stmtEnv_
.
sql_
.
clear
();
stmtEnv_
.
msgBuf_
.
fill
(
0
);
res_
.
ast_
.
clear
();
res_
.
rawLogicPlan_
.
clear
();
res_
.
optimizedLogicPlan_
.
clear
();
res_
.
splitLogicPlan_
.
clear
();
res_
.
scaledLogicPlan_
.
clear
();
res_
.
physiPlan_
.
clear
();
}
void
dump
()
{
cout
<<
"==========================================sql : ["
<<
stmtEnv_
.
sql_
<<
"]"
<<
endl
;
cout
<<
"syntax tree : "
<<
endl
;
cout
<<
res_
.
ast_
<<
endl
;
cout
<<
"raw logic plan : "
<<
endl
;
cout
<<
res_
.
rawLogicPlan_
<<
endl
;
cout
<<
"optimized logic plan : "
<<
endl
;
cout
<<
res_
.
optimizedLogicPlan_
<<
endl
;
cout
<<
"split logic plan : "
<<
endl
;
cout
<<
res_
.
splitLogicPlan_
<<
endl
;
cout
<<
"scaled logic plan : "
<<
endl
;
cout
<<
res_
.
scaledLogicPlan_
<<
endl
;
cout
<<
"physical plan : "
<<
endl
;
cout
<<
res_
.
physiPlan_
<<
endl
;
}
void
doParseSql
(
const
string
&
sql
,
SQuery
**
pQuery
)
{
stmtEnv_
.
sql_
=
sql
;
transform
(
stmtEnv_
.
sql_
.
begin
(),
stmtEnv_
.
sql_
.
end
(),
stmtEnv_
.
sql_
.
begin
(),
::
tolower
);
SParseContext
cxt
=
{
0
};
cxt
.
acctId
=
atoi
(
caseEnv_
.
acctId_
.
c_str
());
cxt
.
db
=
caseEnv_
.
db_
.
c_str
();
cxt
.
pSql
=
stmtEnv_
.
sql_
.
c_str
();
cxt
.
sqlLen
=
stmtEnv_
.
sql_
.
length
();
cxt
.
pMsg
=
stmtEnv_
.
msgBuf_
.
data
();
cxt
.
msgLen
=
stmtEnv_
.
msgBuf_
.
max_size
();
DO_WITH_THROW
(
qParseQuerySql
,
&
cxt
,
pQuery
);
res_
.
ast_
=
toString
((
*
pQuery
)
->
pRoot
);
}
void
doCreateLogicPlan
(
SPlanContext
*
pCxt
,
SLogicNode
**
pLogicNode
)
{
DO_WITH_THROW
(
createLogicPlan
,
pCxt
,
pLogicNode
);
res_
.
rawLogicPlan_
=
toString
((
SNode
*
)(
*
pLogicNode
));
}
void
doOptimizeLogicPlan
(
SPlanContext
*
pCxt
,
SLogicNode
*
pLogicNode
)
{
DO_WITH_THROW
(
optimizeLogicPlan
,
pCxt
,
pLogicNode
);
res_
.
optimizedLogicPlan_
=
toString
((
SNode
*
)
pLogicNode
);
}
void
doSplitLogicPlan
(
SPlanContext
*
pCxt
,
SLogicNode
*
pLogicNode
,
SLogicSubplan
**
pLogicSubplan
)
{
DO_WITH_THROW
(
splitLogicPlan
,
pCxt
,
pLogicNode
,
pLogicSubplan
);
res_
.
splitLogicPlan_
=
toString
((
SNode
*
)(
*
pLogicSubplan
));
}
void
doScaleOutLogicPlan
(
SPlanContext
*
pCxt
,
SLogicSubplan
*
pLogicSubplan
,
SQueryLogicPlan
**
pLogicPlan
)
{
DO_WITH_THROW
(
scaleOutLogicPlan
,
pCxt
,
pLogicSubplan
,
pLogicPlan
);
res_
.
scaledLogicPlan_
=
toString
((
SNode
*
)(
*
pLogicPlan
));
}
void
doCreatePhysiPlan
(
SPlanContext
*
pCxt
,
SQueryLogicPlan
*
pLogicPlan
,
SQueryPlan
**
pPlan
,
SArray
*
pExecNodeList
)
{
DO_WITH_THROW
(
createPhysiPlan
,
pCxt
,
pLogicPlan
,
pPlan
,
pExecNodeList
);
res_
.
physiPlan_
=
toString
((
SNode
*
)(
*
pPlan
));
}
void
setPlanContext
(
SQuery
*
pQuery
,
SPlanContext
*
pCxt
)
{
if
(
QUERY_NODE_CREATE_TOPIC_STMT
==
nodeType
(
pQuery
->
pRoot
))
{
pCxt
->
pAstRoot
=
((
SCreateTopicStmt
*
)
pQuery
->
pRoot
)
->
pQuery
;
pCxt
->
topicQuery
=
true
;
}
else
if
(
QUERY_NODE_CREATE_INDEX_STMT
==
nodeType
(
pQuery
->
pRoot
))
{
SMCreateSmaReq
req
=
{
0
};
tDeserializeSMCreateSmaReq
(
pQuery
->
pCmdMsg
->
pMsg
,
pQuery
->
pCmdMsg
->
msgLen
,
&
req
);
nodesStringToNode
(
req
.
ast
,
&
pCxt
->
pAstRoot
);
pCxt
->
streamQuery
=
true
;
}
else
if
(
QUERY_NODE_CREATE_STREAM_STMT
==
nodeType
(
pQuery
->
pRoot
))
{
SCreateStreamStmt
*
pStmt
=
(
SCreateStreamStmt
*
)
pQuery
->
pRoot
;
pCxt
->
pAstRoot
=
pStmt
->
pQuery
;
pCxt
->
streamQuery
=
true
;
pCxt
->
triggerType
=
pStmt
->
pOptions
->
triggerType
;
pCxt
->
watermark
=
(
NULL
!=
pStmt
->
pOptions
->
pWatermark
?
((
SValueNode
*
)
pStmt
->
pOptions
->
pWatermark
)
->
datum
.
i
:
0
);
}
else
{
pCxt
->
pAstRoot
=
pQuery
->
pRoot
;
}
}
string
toString
(
const
SNode
*
pRoot
)
{
char
*
pStr
=
NULL
;
int32_t
len
=
0
;
DO_WITH_THROW
(
nodesNodeToString
,
pRoot
,
false
,
&
pStr
,
&
len
)
string
str
(
pStr
);
taosMemoryFreeClear
(
pStr
);
return
str
;
}
caseEnv
caseEnv_
;
stmtEnv
stmtEnv_
;
stmtRes
res_
;
};
PlannerTestBase
::
PlannerTestBase
()
:
impl_
(
new
PlannerTestBaseImpl
())
{
}
PlannerTestBase
::~
PlannerTestBase
()
{
}
void
PlannerTestBase
::
useDb
(
const
std
::
string
&
acctId
,
const
std
::
string
&
db
)
{
impl_
->
useDb
(
acctId
,
db
);
}
void
PlannerTestBase
::
run
(
const
std
::
string
&
sql
)
{
return
impl_
->
run
(
sql
);
}
source/libs/planner/test/planTestUtil.h
0 → 100644
浏览文件 @
ff804598
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PLAN_TEST_UTIL_H
#define PLAN_TEST_UTIL_H
#include <gtest/gtest.h>
class
PlannerTestBaseImpl
;
class
PlannerTestBase
:
public
testing
::
Test
{
public:
PlannerTestBase
();
virtual
~
PlannerTestBase
();
void
useDb
(
const
std
::
string
&
acctId
,
const
std
::
string
&
db
);
void
run
(
const
std
::
string
&
sql
);
private:
std
::
unique_ptr
<
PlannerTestBaseImpl
>
impl_
;
};
#endif // PLAN_TEST_UTIL_H
source/libs/planner/test/plannerTest.cpp
浏览文件 @
ff804598
...
...
@@ -90,17 +90,16 @@ protected:
return
false
;
}
SQueryPlan
*
pPlan
=
nullptr
;
code
=
createPhysiPlan
(
&
cxt
,
pLogicPlan
,
&
pPlan
,
NULL
);
code
=
createPhysiPlan
(
&
cxt
,
pLogicPlan
,
&
plan_
,
NULL
);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
cout
<<
"sql:["
<<
cxt_
.
pSql
<<
"] createPhysiPlan code:"
<<
code
<<
", strerror:"
<<
tstrerror
(
code
)
<<
endl
;
return
false
;
}
cout
<<
"unformatted physical plan : "
<<
endl
;
cout
<<
toString
((
const
SNode
*
)
p
Plan
,
false
)
<<
endl
;
cout
<<
toString
((
const
SNode
*
)
p
lan_
,
false
)
<<
endl
;
SNode
*
pNode
;
FOREACH
(
pNode
,
p
Plan
->
pSubplans
)
{
FOREACH
(
pNode
,
p
lan_
->
pSubplans
)
{
SNode
*
pSubplan
;
FOREACH
(
pSubplan
,
((
SNodeListNode
*
)
pNode
)
->
pNodeList
)
{
cout
<<
"unformatted physical subplan : "
<<
endl
;
...
...
@@ -160,6 +159,7 @@ private:
string
sqlBuf_
;
SParseContext
cxt_
;
SQuery
*
query_
;
SQueryPlan
*
plan_
;
};
TEST_F
(
PlannerTest
,
selectBasic
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录