Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
b18995c4
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看板
未验证
提交
b18995c4
编写于
2月 07, 2022
作者:
X
xiao-yu-wang
提交者:
GitHub
2月 07, 2022
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #10127 from taosdata/feature/3.0_wxy
TD-13338 SELECT statement translate code
上级
fbdb47fe
59ebc0a1
变更
8
展开全部
显示空白变更内容
内联
并排
Showing
8 changed file
with
1062 addition
and
794 deletion
+1062
-794
include/common/ttokendef.h
include/common/ttokendef.h
+5
-6
source/libs/parser/inc/astCreateFuncs.h
source/libs/parser/inc/astCreateFuncs.h
+1
-1
source/libs/parser/inc/new_sql.y
source/libs/parser/inc/new_sql.y
+5
-5
source/libs/parser/inc/parserImpl.h
source/libs/parser/inc/parserImpl.h
+1
-0
source/libs/parser/src/astCreateFuncs.c
source/libs/parser/src/astCreateFuncs.c
+14
-4
source/libs/parser/src/new_sql.c
source/libs/parser/src/new_sql.c
+678
-689
source/libs/parser/src/parserImpl.c
source/libs/parser/src/parserImpl.c
+220
-65
source/libs/parser/test/newParserTest.cpp
source/libs/parser/test/newParserTest.cpp
+138
-24
未找到文件。
include/common/ttokendef.h
浏览文件 @
b18995c4
...
...
@@ -273,12 +273,11 @@
#define NEW_TK_SOFFSET 64
#define NEW_TK_LIMIT 65
#define NEW_TK_OFFSET 66
#define NEW_TK_NK_LR 67
#define NEW_TK_ASC 68
#define NEW_TK_DESC 69
#define NEW_TK_NULLS 70
#define NEW_TK_FIRST 71
#define NEW_TK_LAST 72
#define NEW_TK_ASC 67
#define NEW_TK_DESC 68
#define NEW_TK_NULLS 69
#define NEW_TK_FIRST 70
#define NEW_TK_LAST 71
#define TK_SPACE 300
#define TK_COMMENT 301
...
...
source/libs/parser/inc/astCreateFuncs.h
浏览文件 @
b18995c4
...
...
@@ -30,7 +30,7 @@ extern SToken nil_token;
SNodeList
*
createNodeList
(
SAstCreateContext
*
pCxt
,
SNode
*
pNode
);
SNodeList
*
addNodeToList
(
SAstCreateContext
*
pCxt
,
SNodeList
*
pList
,
SNode
*
pNode
);
SNode
*
createColumnNode
(
SAstCreateContext
*
pCxt
,
const
SToken
*
pTable
Name
,
const
SToken
*
pColumnName
);
SNode
*
createColumnNode
(
SAstCreateContext
*
pCxt
,
const
SToken
*
pTable
Alias
,
const
SToken
*
pColumnName
);
SNode
*
createValueNode
(
SAstCreateContext
*
pCxt
,
int32_t
dataType
,
const
SToken
*
pLiteral
);
SNode
*
createDurationValueNode
(
SAstCreateContext
*
pCxt
,
const
SToken
*
pLiteral
);
SNode
*
addMinusSign
(
SAstCreateContext
*
pCxt
,
SNode
*
pNode
);
...
...
source/libs/parser/inc/new_sql.y
浏览文件 @
b18995c4
...
...
@@ -187,7 +187,7 @@ table_reference(A) ::= joined_table(B).
table_primary(A) ::= table_name(B) alias_opt(C). { PARSER_TRACE; A = createRealTableNode(pCxt, NULL, &B, &C); }
table_primary(A) ::= db_name(B) NK_DOT table_name(C) alias_opt(D). { PARSER_TRACE; A = createRealTableNode(pCxt, &B, &C, &D); }
table_primary(A) ::= subquery(B) alias_opt(C). { PARSER_TRACE; A = createTempTableNode(pCxt, B, &C); }
table_primary
::= parenthesized_joined_table.
table_primary
(A) ::= parenthesized_joined_table(B). { PARSER_TRACE; A = B; }
%type alias_opt { SToken }
%destructor alias_opt { PARSER_DESTRUCTOR_TRACE; }
...
...
@@ -297,9 +297,9 @@ query_expression_body(A) ::=
query_expression_body(B) UNION ALL query_expression_body(D). { PARSER_TRACE; A = createSetOperator(pCxt, SET_OP_TYPE_UNION_ALL, B, D); }
query_primary(A) ::= query_specification(B). { PARSER_TRACE; A = B; }
query_primary(A) ::=
NK_LP query_expression_body(B)
order_by_clause_opt limit_clause_opt s
limit_clause_opt NK_RP. { PARSER_TRACE; A = B;}
//
query_primary(A) ::=
//
NK_LP query_expression_body(B)
// order_by_clause_opt slimit_clause_opt
limit_clause_opt NK_RP. { PARSER_TRACE; A = B;}
%type order_by_clause_opt { SNodeList* }
%destructor order_by_clause_opt { PARSER_DESTRUCTOR_TRACE; nodesDestroyList($$); }
...
...
@@ -317,7 +317,7 @@ limit_clause_opt(A) ::= LIMIT NK_INTEGER(B) OFFSET NK_INTEGER(C).
limit_clause_opt(A) ::= LIMIT NK_INTEGER(C) NK_COMMA NK_INTEGER(B). { PARSER_TRACE; A = createLimitNode(pCxt, &B, &C); }
/************************************************ subquery ************************************************************/
subquery(A) ::= NK_L
R
query_expression(B) NK_RP. { PARSER_TRACE; A = B; }
subquery(A) ::= NK_L
P
query_expression(B) NK_RP. { PARSER_TRACE; A = B; }
/************************************************ search_condition ****************************************************/
search_condition(A) ::= boolean_value_expression(B). { PARSER_TRACE; A = B; }
...
...
source/libs/parser/inc/parserImpl.h
浏览文件 @
b18995c4
...
...
@@ -28,6 +28,7 @@ typedef struct SQuery {
}
SQuery
;
int32_t
doParse
(
SParseContext
*
pParseCxt
,
SQuery
*
pQuery
);
int32_t
doTranslate
(
SParseContext
*
pParseCxt
,
SQuery
*
pQuery
);
#ifdef __cplusplus
}
...
...
source/libs/parser/src/astCreateFuncs.c
浏览文件 @
b18995c4
...
...
@@ -60,14 +60,14 @@ SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode
return
nodesListAppend
(
pList
,
pNode
);
}
SNode
*
createColumnNode
(
SAstCreateContext
*
pCxt
,
const
SToken
*
pTable
Name
,
const
SToken
*
pColumnName
)
{
if
(
!
checkTableName
(
pCxt
,
pTable
Name
)
||
!
checkColumnName
(
pCxt
,
pColumnName
))
{
SNode
*
createColumnNode
(
SAstCreateContext
*
pCxt
,
const
SToken
*
pTable
Alias
,
const
SToken
*
pColumnName
)
{
if
(
!
checkTableName
(
pCxt
,
pTable
Alias
)
||
!
checkColumnName
(
pCxt
,
pColumnName
))
{
return
NULL
;
}
SColumnNode
*
col
=
(
SColumnNode
*
)
nodesMakeNode
(
QUERY_NODE_COLUMN
);
CHECK_OUT_OF_MEM
(
col
);
if
(
NULL
!=
pTable
Name
)
{
strncpy
(
col
->
table
Name
,
pTableName
->
z
,
pTableName
->
n
);
if
(
NULL
!=
pTable
Alias
)
{
strncpy
(
col
->
table
Alias
,
pTableAlias
->
z
,
pTableAlias
->
n
);
}
strncpy
(
col
->
colName
,
pColumnName
->
z
,
pColumnName
->
n
);
return
(
SNode
*
)
col
;
...
...
@@ -151,6 +151,13 @@ SNode* createRealTableNode(SAstCreateContext* pCxt, const SToken* pDbName, const
CHECK_OUT_OF_MEM
(
realTable
);
if
(
NULL
!=
pDbName
)
{
strncpy
(
realTable
->
table
.
dbName
,
pDbName
->
z
,
pDbName
->
n
);
}
else
{
strcpy
(
realTable
->
table
.
dbName
,
pCxt
->
pQueryCxt
->
db
);
}
if
(
NULL
!=
pTableAlias
&&
TK_NIL
!=
pTableAlias
->
type
)
{
strncpy
(
realTable
->
table
.
tableAlias
,
pTableAlias
->
z
,
pTableAlias
->
n
);
}
else
{
strncpy
(
realTable
->
table
.
tableAlias
,
pTableName
->
z
,
pTableName
->
n
);
}
strncpy
(
realTable
->
table
.
tableName
,
pTableName
->
z
,
pTableName
->
n
);
return
(
SNode
*
)
realTable
;
...
...
@@ -160,6 +167,9 @@ SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const STok
STempTableNode
*
tempTable
=
(
STempTableNode
*
)
nodesMakeNode
(
QUERY_NODE_TEMP_TABLE
);
CHECK_OUT_OF_MEM
(
tempTable
);
tempTable
->
pSubquery
=
pSubquery
;
if
(
NULL
!=
pTableAlias
&&
TK_NIL
!=
pTableAlias
->
type
)
{
strncpy
(
tempTable
->
table
.
tableAlias
,
pTableAlias
->
z
,
pTableAlias
->
n
);
}
return
(
SNode
*
)
tempTable
;
}
...
...
source/libs/parser/src/new_sql.c
浏览文件 @
b18995c4
此差异已折叠。
点击以展开。
source/libs/parser/src/parserImpl.c
浏览文件 @
b18995c4
...
...
@@ -25,9 +25,85 @@ typedef void (*FFree)(void*);
extern
void
*
NewParseAlloc
(
FMalloc
);
extern
void
NewParse
(
void
*
,
int
,
SToken
,
void
*
);
extern
void
NewParseFree
(
void
*
,
FFree
);
extern
void
NewParseTrace
(
FILE
*
,
char
*
);
static
uint32_t
toNewTokenId
(
uint32_t
tokenId
)
{
// #define 1
// #define NEW_TK_AND 2
// #define NEW_TK_UNION 3
// #define NEW_TK_ALL 4
// #define NEW_TK_MINUS 5
// #define NEW_TK_EXCEPT 6
// #define NEW_TK_INTERSECT 7
// #define NEW_TK_NK_PLUS 8
// #define NEW_TK_NK_MINUS 9
// #define NEW_TK_NK_STAR 10
// #define NEW_TK_NK_SLASH 11
// #define NEW_TK_NK_REM 12
// #define NEW_TK_SHOW 13
// #define NEW_TK_DATABASES 14
// #define NEW_TK_NK_INTEGER 15
// #define NEW_TK_NK_FLOAT 16
// #define NEW_TK_NK_STRING 17
// #define NEW_TK_NK_BOOL 18
// #define NEW_TK_TIMESTAMP 19
// #define NEW_TK_NK_VARIABLE 20
// #define NEW_TK_NK_COMMA 21
// #define NEW_TK_NK_ID 22
// #define NEW_TK_NK_LP 23
// #define NEW_TK_NK_RP 24
// #define NEW_TK_NK_DOT 25
// #define NEW_TK_BETWEEN 26
// #define NEW_TK_NOT 27
// #define NEW_TK_IS 28
// #define NEW_TK_NULL 29
// #define NEW_TK_NK_LT 30
// #define NEW_TK_NK_GT 31
// #define NEW_TK_NK_LE 32
// #define NEW_TK_NK_GE 33
// #define NEW_TK_NK_NE 34
// #define 35
// #define NEW_TK_LIKE 36
// #define NEW_TK_MATCH 37
// #define NEW_TK_NMATCH 38
// #define NEW_TK_IN 39
// #define NEW_TK_FROM 40
// #define NEW_TK_AS 41
// #define NEW_TK_JOIN 42
// #define NEW_TK_ON 43
// #define NEW_TK_INNER 44
// #define NEW_TK_SELECT 45
// #define NEW_TK_DISTINCT 46
// #define 47
// #define NEW_TK_PARTITION 48
// #define NEW_TK_BY 49
// #define NEW_TK_SESSION 50
// #define NEW_TK_STATE_WINDOW 51
// #define NEW_TK_INTERVAL 52
// #define NEW_TK_SLIDING 53
// #define NEW_TK_FILL 54
// #define NEW_TK_VALUE 55
// #define NEW_TK_NONE 56
// #define NEW_TK_PREV 57
// #define NEW_TK_LINEAR 58
// #define NEW_TK_NEXT 59
// #define NEW_TK_GROUP 60
// #define NEW_TK_HAVING 61
// #define NEW_TK_ORDER 62
// #define NEW_TK_SLIMIT 63
// #define NEW_TK_SOFFSET 64
// #define NEW_TK_LIMIT 65
// #define NEW_TK_OFFSET 66
// #define NEW_TK_NK_LR 67
// #define NEW_TK_ASC 68
// #define NEW_TK_DESC 69
// #define NEW_TK_NULLS 70
// #define NEW_TK_FIRST 71
// #define NEW_TK_LAST 72
switch
(
tokenId
)
{
case
TK_OR
:
return
NEW_TK_OR
;
case
TK_UNION
:
return
NEW_TK_UNION
;
case
TK_ALL
:
...
...
@@ -54,10 +130,14 @@ static uint32_t toNewTokenId(uint32_t tokenId) {
return
NEW_TK_NK_COMMA
;
case
TK_DOT
:
return
NEW_TK_NK_DOT
;
case
TK_EQ
:
return
NEW_TK_NK_EQ
;
case
TK_SELECT
:
return
NEW_TK_SELECT
;
case
TK_DISTINCT
:
return
NEW_TK_DISTINCT
;
case
TK_WHERE
:
return
NEW_TK_WHERE
;
case
TK_AS
:
return
NEW_TK_AS
;
case
TK_FROM
:
...
...
@@ -70,6 +150,10 @@ static uint32_t toNewTokenId(uint32_t tokenId) {
return
NEW_TK_ASC
;
case
TK_DESC
:
return
NEW_TK_DESC
;
case
TK_SPACE
:
break
;
default:
printf
(
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!tokenId = %d
\n
"
,
tokenId
);
}
return
tokenId
;
}
...
...
@@ -125,6 +209,7 @@ int32_t doParse(SParseContext* pParseCxt, SQuery* pQuery) {
default:
NewParse
(
pParser
,
t0
.
type
,
t0
,
&
cxt
);
// NewParseTrace(stdout, "");
if
(
!
cxt
.
valid
)
{
goto
abort_parse
;
}
...
...
@@ -147,12 +232,18 @@ abort_parse:
// STableMeta* pMeta;
// } SNamespace;
typedef
enum
ESqlClause
{
SQL_CLAUSE_FROM
=
1
,
SQL_CLAUSE_WHERE
}
ESqlClause
;
typedef
struct
STranslateContext
{
SParseContext
*
pParseCxt
;
int32_t
errCode
;
SMsgBuf
msgBuf
;
SArray
*
pNsLevel
;
// element is SArray*, the element of this subarray is STableNode*
int32_t
currLevel
;
ESqlClause
currClause
;
}
STranslateContext
;
static
int32_t
translateSubquery
(
STranslateContext
*
pCxt
,
SNode
*
pNode
);
...
...
@@ -177,19 +268,28 @@ static int32_t generateSyntaxErrMsg(STranslateContext* pCxt, int32_t errCode, co
}
static
int32_t
addNamespace
(
STranslateContext
*
pCxt
,
void
*
pTable
)
{
SArray
*
pTables
=
NULL
;
if
(
taosArrayGetSize
(
pCxt
->
pNsLevel
)
>
pCxt
->
currLevel
)
{
pTables
=
taosArrayGetP
(
pCxt
->
pNsLevel
,
pCxt
->
currLevel
);
size_t
currTotalLevel
=
taosArrayGetSize
(
pCxt
->
pNsLevel
);
if
(
currTotalLevel
>
pCxt
->
currLevel
)
{
SArray
*
pTables
=
taosArrayGetP
(
pCxt
->
pNsLevel
,
pCxt
->
currLevel
);
taosArrayPush
(
pTables
,
&
pTable
);
}
else
{
pTables
=
taosArrayInit
(
TARRAY_MIN_SIZE
,
POINTER_BYTES
);
}
do
{
SArray
*
pTables
=
taosArrayInit
(
TARRAY_MIN_SIZE
,
POINTER_BYTES
);
if
(
pCxt
->
currLevel
==
currTotalLevel
)
{
taosArrayPush
(
pTables
,
&
pTable
);
}
taosArrayPush
(
pCxt
->
pNsLevel
,
&
pTables
);
++
currTotalLevel
;
}
while
(
currTotalLevel
<=
pCxt
->
currLevel
);
}
return
TSDB_CODE_SUCCESS
;
}
static
SName
*
toName
(
const
SRealTableNode
*
pRealTable
,
SName
*
pName
)
{
strncpy
(
pName
->
dbname
,
pRealTable
->
table
.
dbName
,
strlen
(
pRealTable
->
table
.
dbName
));
strncpy
(
pName
->
dbname
,
pRealTable
->
table
.
tableName
,
strlen
(
pRealTable
->
table
.
tableName
));
static
SName
*
toName
(
int32_t
acctId
,
const
SRealTableNode
*
pRealTable
,
SName
*
pName
)
{
pName
->
type
=
TSDB_TABLE_NAME_T
;
pName
->
acctId
=
acctId
;
strcpy
(
pName
->
dbname
,
pRealTable
->
table
.
dbName
);
strcpy
(
pName
->
tname
,
pRealTable
->
table
.
tableName
);
return
pName
;
}
...
...
@@ -213,26 +313,42 @@ static SNodeList* getProjectList(SNode* pNode) {
return
NULL
;
}
static
void
setColumnInfoBySchema
(
const
STableNode
*
pTable
,
const
SSchema
*
pColSchema
,
SColumnNode
*
pCol
)
{
strcpy
(
pCol
->
dbName
,
pTable
->
dbName
);
strcpy
(
pCol
->
tableAlias
,
pTable
->
tableAlias
);
strcpy
(
pCol
->
tableName
,
pTable
->
tableName
);
strcpy
(
pCol
->
colName
,
pColSchema
->
name
);
if
(
'\0'
==
pCol
->
node
.
aliasName
[
0
])
{
strcpy
(
pCol
->
node
.
aliasName
,
pColSchema
->
name
);
}
pCol
->
colId
=
pColSchema
->
colId
;
pCol
->
colType
=
pColSchema
->
type
;
pCol
->
node
.
resType
.
bytes
=
pColSchema
->
bytes
;
}
static
void
setColumnInfoByExpr
(
const
STableNode
*
pTable
,
SExprNode
*
pExpr
,
SColumnNode
*
pCol
)
{
pCol
->
pProjectRef
=
(
SNode
*
)
pExpr
;
pExpr
->
pAssociationList
=
nodesListAppend
(
pExpr
->
pAssociationList
,
(
SNode
*
)
pCol
);
strcpy
(
pCol
->
tableAlias
,
pTable
->
tableAlias
);
strcpy
(
pCol
->
colName
,
pExpr
->
aliasName
);
pCol
->
node
.
resType
=
pExpr
->
resType
;
}
static
int32_t
createColumnNodeByTable
(
const
STableNode
*
pTable
,
SNodeList
*
pList
)
{
if
(
QUERY_NODE_REAL_TABLE
==
nodeType
(
pTable
))
{
const
STableMeta
*
pMeta
=
((
SRealTableNode
*
)
pTable
)
->
pMeta
;
int32_t
nums
=
pMeta
->
tableInfo
.
numOfTags
+
pMeta
->
tableInfo
.
numOfColumns
;
for
(
int32_t
i
=
0
;
i
<
nums
;
++
i
)
{
SColumnNode
*
pCol
=
(
SColumnNode
*
)
nodesMakeNode
(
QUERY_NODE_COLUMN
);
pCol
->
colId
=
pMeta
->
schema
[
i
].
colId
;
pCol
->
colType
=
pMeta
->
schema
[
i
].
type
;
pCol
->
node
.
resType
.
bytes
=
pMeta
->
schema
[
i
].
bytes
;
setColumnInfoBySchema
(
pTable
,
pMeta
->
schema
+
i
,
pCol
);
nodesListAppend
(
pList
,
(
SNode
*
)
pCol
);
}
}
else
{
SNodeList
*
pProjectList
=
getProjectList
(((
STempTableNode
*
)
pTable
)
->
pSubquery
);
SNode
*
pNode
;
FOREACH
(
pNode
,
pProjectList
)
{
SExprNode
*
pExpr
=
(
SExprNode
*
)
pNode
;
SColumnNode
*
pCol
=
(
SColumnNode
*
)
nodesMakeNode
(
QUERY_NODE_COLUMN
);
pCol
->
pProjectRef
=
(
SNode
*
)
pExpr
;
pExpr
->
pAssociationList
=
nodesListAppend
(
pExpr
->
pAssociationList
,
(
SNode
*
)
pCol
);
pCol
->
node
.
resType
=
pExpr
->
resType
;
setColumnInfoByExpr
(
pTable
,
(
SExprNode
*
)
pNode
,
pCol
);
nodesListAppend
(
pList
,
(
SNode
*
)
pCol
);
}
}
...
...
@@ -245,9 +361,7 @@ static bool findAndSetColumn(SColumnNode* pCol, const STableNode* pTable) {
int32_t
nums
=
pMeta
->
tableInfo
.
numOfTags
+
pMeta
->
tableInfo
.
numOfColumns
;
for
(
int32_t
i
=
0
;
i
<
nums
;
++
i
)
{
if
(
0
==
strcmp
(
pCol
->
colName
,
pMeta
->
schema
[
i
].
name
))
{
pCol
->
colId
=
pMeta
->
schema
[
i
].
colId
;
pCol
->
colType
=
pMeta
->
schema
[
i
].
type
;
pCol
->
node
.
resType
.
bytes
=
pMeta
->
schema
[
i
].
bytes
;
setColumnInfoBySchema
(
pTable
,
pMeta
->
schema
+
i
,
pCol
);
found
=
true
;
break
;
}
...
...
@@ -258,9 +372,7 @@ static bool findAndSetColumn(SColumnNode* pCol, const STableNode* pTable) {
FOREACH
(
pNode
,
pProjectList
)
{
SExprNode
*
pExpr
=
(
SExprNode
*
)
pNode
;
if
(
0
==
strcmp
(
pCol
->
colName
,
pExpr
->
aliasName
))
{
pCol
->
pProjectRef
=
(
SNode
*
)
pExpr
;
pExpr
->
pAssociationList
=
nodesListAppend
(
pExpr
->
pAssociationList
,
(
SNode
*
)
pCol
);
pCol
->
node
.
resType
=
pExpr
->
resType
;
setColumnInfoByExpr
(
pTable
,
pExpr
,
pCol
);
found
=
true
;
break
;
}
...
...
@@ -269,18 +381,11 @@ static bool findAndSetColumn(SColumnNode* pCol, const STableNode* pTable) {
return
found
;
}
static
bool
doTranslateExpr
(
SNode
*
pNode
,
void
*
pContext
)
{
STranslateContext
*
pCxt
=
(
STranslateContext
*
)
pContext
;
switch
(
nodeType
(
pNode
))
{
case
QUERY_NODE_COLUMN
:
{
SColumnNode
*
pCol
=
(
SColumnNode
*
)
pNode
;
static
bool
translateColumnWithPrefix
(
STranslateContext
*
pCxt
,
SColumnNode
*
pCol
)
{
SArray
*
pTables
=
taosArrayGetP
(
pCxt
->
pNsLevel
,
pCxt
->
currLevel
);
size_t
nums
=
taosArrayGetSize
(
pTables
);
bool
hasTableAlias
=
(
'\0'
!=
pCol
->
tableAlias
[
0
]);
bool
found
=
false
;
for
(
size_t
i
=
0
;
i
<
nums
;
++
i
)
{
STableNode
*
pTable
=
taosArrayGetP
(
pTables
,
i
);
if
(
hasTableAlias
)
{
if
(
belongTable
(
pCxt
->
pParseCxt
->
db
,
pCol
,
pTable
))
{
if
(
findAndSetColumn
(
pCol
,
pTable
))
{
break
;
...
...
@@ -288,7 +393,16 @@ static bool doTranslateExpr(SNode* pNode, void* pContext) {
generateSyntaxErrMsg
(
pCxt
,
TSDB_CODE_PARSER_INVALID_COLUMN
,
pCol
->
colName
);
return
false
;
}
}
else
{
}
return
true
;
}
static
bool
translateColumnWithoutPrefix
(
STranslateContext
*
pCxt
,
SColumnNode
*
pCol
)
{
SArray
*
pTables
=
taosArrayGetP
(
pCxt
->
pNsLevel
,
pCxt
->
currLevel
);
size_t
nums
=
taosArrayGetSize
(
pTables
);
bool
found
=
false
;
for
(
size_t
i
=
0
;
i
<
nums
;
++
i
)
{
STableNode
*
pTable
=
taosArrayGetP
(
pTables
,
i
);
if
(
findAndSetColumn
(
pCol
,
pTable
))
{
if
(
found
)
{
generateSyntaxErrMsg
(
pCxt
,
TSDB_CODE_PARSER_AMBIGUOUS_COLUMN
,
pCol
->
colName
);
...
...
@@ -297,17 +411,44 @@ static bool doTranslateExpr(SNode* pNode, void* pContext) {
found
=
true
;
}
}
if
(
!
found
)
{
generateSyntaxErrMsg
(
pCxt
,
TSDB_CODE_PARSER_INVALID_COLUMN
,
pCol
->
colName
);
return
false
;
}
break
;
}
case
QUERY_NODE_VALUE
:
break
;
// todo check literal format
case
QUERY_NODE_OPERATOR
:
{
return
true
;
}
break
;
static
bool
translateColumn
(
STranslateContext
*
pCxt
,
SColumnNode
*
pCol
)
{
if
(
'\0'
!=
pCol
->
tableAlias
[
0
])
{
return
translateColumnWithPrefix
(
pCxt
,
pCol
);
}
return
translateColumnWithoutPrefix
(
pCxt
,
pCol
);
}
// check literal format
static
bool
translateValue
(
STranslateContext
*
pCxt
,
SValueNode
*
pVal
)
{
return
true
;
}
static
bool
translateOperator
(
STranslateContext
*
pCxt
,
SOperatorNode
*
pOp
)
{
return
true
;
}
static
bool
translateFunction
(
STranslateContext
*
pCxt
,
SFunctionNode
*
pFunc
)
{
return
true
;
}
static
bool
doTranslateExpr
(
SNode
*
pNode
,
void
*
pContext
)
{
STranslateContext
*
pCxt
=
(
STranslateContext
*
)
pContext
;
switch
(
nodeType
(
pNode
))
{
case
QUERY_NODE_COLUMN
:
return
translateColumn
(
pCxt
,
(
SColumnNode
*
)
pNode
);
case
QUERY_NODE_VALUE
:
return
translateValue
(
pCxt
,
(
SValueNode
*
)
pNode
);
case
QUERY_NODE_OPERATOR
:
return
translateOperator
(
pCxt
,
(
SOperatorNode
*
)
pNode
);
case
QUERY_NODE_FUNCTION
:
break
;
// todo
return
translateFunction
(
pCxt
,
(
SFunctionNode
*
)
pNode
);
case
QUERY_NODE_TEMP_TABLE
:
return
translateSubquery
(
pCxt
,
((
STempTableNode
*
)
pNode
)
->
pSubquery
);
default:
...
...
@@ -331,15 +472,9 @@ static int32_t translateTable(STranslateContext* pCxt, SNode* pTable) {
switch
(
nodeType
(
pTable
))
{
case
QUERY_NODE_REAL_TABLE
:
{
SRealTableNode
*
pRealTable
=
(
SRealTableNode
*
)
pTable
;
if
(
'\0'
==
pRealTable
->
table
.
dbName
[
0
])
{
strcpy
(
pRealTable
->
table
.
dbName
,
pCxt
->
pParseCxt
->
db
);
}
if
(
'\0'
==
pRealTable
->
table
.
tableAlias
[
0
])
{
strcpy
(
pRealTable
->
table
.
tableAlias
,
pRealTable
->
table
.
tableName
);
}
SName
name
;
code
=
catalogGetTableMeta
(
pCxt
->
pParseCxt
->
pCatalog
,
pCxt
->
pParseCxt
->
pTransporter
,
&
(
pCxt
->
pParseCxt
->
mgmtEpSet
),
toName
(
pRealTable
,
&
name
),
&
(
pRealTable
->
pMeta
));
code
=
catalogGetTableMeta
(
pCxt
->
pParseCxt
->
pCatalog
,
pCxt
->
pParseCxt
->
pTransporter
,
&
(
pCxt
->
pParseCxt
->
mgmtEpSet
),
toName
(
pCxt
->
pParseCxt
->
acctId
,
pRealTable
,
&
name
),
&
(
pRealTable
->
pMeta
));
if
(
TSDB_CODE_SUCCESS
!=
code
)
{
return
generateSyntaxErrMsg
(
pCxt
,
TSDB_CODE_PARSER_TABLE_NOT_EXIST
,
pRealTable
->
table
.
tableName
);
}
...
...
@@ -371,10 +506,16 @@ static int32_t translateTable(STranslateContext* pCxt, SNode* pTable) {
return
code
;
}
static
int32_t
translateFrom
(
STranslateContext
*
pCxt
,
SNode
*
pTable
)
{
pCxt
->
currClause
=
SQL_CLAUSE_FROM
;
return
translateTable
(
pCxt
,
pTable
);
}
static
int32_t
translateStar
(
STranslateContext
*
pCxt
,
SSelectStmt
*
pSelect
,
bool
*
pIsSelectStar
)
{
if
(
NULL
==
pSelect
->
pProjectionList
)
{
// select * ...
SArray
*
pTables
=
taosArrayGetP
(
pCxt
->
pNsLevel
,
pCxt
->
currLevel
);
size_t
nums
=
taosArrayGetSize
(
pTables
);
pSelect
->
pProjectionList
=
nodesMakeList
();
for
(
size_t
i
=
0
;
i
<
nums
;
++
i
)
{
STableNode
*
pTable
=
taosArrayGetP
(
pTables
,
i
);
createColumnNodeByTable
(
pTable
,
pSelect
->
pProjectionList
);
...
...
@@ -383,14 +524,18 @@ static int32_t translateStar(STranslateContext* pCxt, SSelectStmt* pSelect, bool
}
else
{
}
return
TSDB_CODE_SUCCESS
;
}
static
int32_t
translateSelect
(
STranslateContext
*
pCxt
,
SSelectStmt
*
pSelect
)
{
int32_t
code
=
TSDB_CODE_SUCCESS
;
code
=
translate
Table
(
pCxt
,
pSelect
->
pFromTable
);
code
=
translate
From
(
pCxt
,
pSelect
->
pFromTable
);
if
(
TSDB_CODE_SUCCESS
==
code
)
{
code
=
translateExpr
(
pCxt
,
pSelect
->
pWhere
);
}
if
(
TSDB_CODE_SUCCESS
==
code
)
{
code
=
translateExprList
(
pCxt
,
pSelect
->
pGroupByList
);
}
bool
isSelectStar
=
false
;
if
(
TSDB_CODE_SUCCESS
==
code
)
{
code
=
translateStar
(
pCxt
,
pSelect
,
&
isSelectStar
);
...
...
@@ -398,6 +543,7 @@ static int32_t translateSelect(STranslateContext* pCxt, SSelectStmt* pSelect) {
if
(
TSDB_CODE_SUCCESS
==
code
&&
!
isSelectStar
)
{
code
=
translateExprList
(
pCxt
,
pSelect
->
pProjectionList
);
}
// printf("%s:%d code = %d\n", __FUNCTION__, __LINE__, code);
return
code
;
}
...
...
@@ -415,12 +561,21 @@ static int32_t translateQuery(STranslateContext* pCxt, SNode* pNode) {
static
int32_t
translateSubquery
(
STranslateContext
*
pCxt
,
SNode
*
pNode
)
{
++
(
pCxt
->
currLevel
);
ESqlClause
currClause
=
pCxt
->
currClause
;
int32_t
code
=
translateQuery
(
pCxt
,
pNode
);
--
(
pCxt
->
currLevel
);
pCxt
->
currClause
=
currClause
;
return
code
;
}
int32_t
doTranslate
(
SParseContext
*
pParseCxt
,
SQuery
*
pQuery
)
{
STranslateContext
cxt
=
{
.
pParseCxt
=
pParseCxt
,
.
pNsLevel
=
taosArrayInit
(
TARRAY_MIN_SIZE
,
POINTER_BYTES
),
.
currLevel
=
0
};
STranslateContext
cxt
=
{
.
pParseCxt
=
pParseCxt
,
.
errCode
=
TSDB_CODE_SUCCESS
,
.
msgBuf
=
{
.
buf
=
pParseCxt
->
pMsg
,
.
len
=
pParseCxt
->
msgLen
},
.
pNsLevel
=
taosArrayInit
(
TARRAY_MIN_SIZE
,
POINTER_BYTES
),
.
currLevel
=
0
,
.
currClause
=
0
};
return
translateQuery
(
&
cxt
,
pQuery
->
pRoot
);
}
source/libs/parser/test/newParserTest.cpp
浏览文件 @
b18995c4
...
...
@@ -38,15 +38,38 @@ protected:
}
bool
run
(
int32_t
expect
Code
=
TSDB_CODE_SUCCESS
)
{
bool
run
(
int32_t
parseCode
=
TSDB_CODE_SUCCESS
,
int32_t
translate
Code
=
TSDB_CODE_SUCCESS
)
{
int32_t
code
=
doParse
(
&
cxt_
,
&
query_
);
// cout << "doParse return " << code << endl;
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
cout
<<
"sql:["
<<
cxt_
.
pSql
<<
"] code:"
<<
tstrerror
(
code
)
<<
", msg:"
<<
errMagBuf_
<<
endl
;
return
(
code
==
expectCode
);
return
(
TSDB_CODE_SUCCESS
!=
parseCode
);
}
if
(
TSDB_CODE_SUCCESS
!=
parseCode
)
{
return
false
;
}
code
=
doTranslate
(
&
cxt_
,
&
query_
);
// cout << "doTranslate return " << code << endl;
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
cout
<<
"sql:["
<<
cxt_
.
pSql
<<
"] code:"
<<
tstrerror
(
code
)
<<
", msg:"
<<
errMagBuf_
<<
endl
;
return
(
TSDB_CODE_SUCCESS
!=
translateCode
);
}
if
(
NULL
!=
query_
.
pRoot
&&
QUERY_NODE_SELECT_STMT
==
nodeType
(
query_
.
pRoot
))
{
SSelectStmt
*
select
=
(
SSelectStmt
*
)
query_
.
pRoot
;
string
sql
(
"SELECT "
);
string
sql
;
selectToSql
(
query_
.
pRoot
,
sql
);
cout
<<
"input sql : ["
<<
cxt_
.
pSql
<<
"]"
<<
endl
;
cout
<<
"output sql : ["
<<
sql
<<
"]"
<<
endl
;
}
return
(
TSDB_CODE_SUCCESS
==
translateCode
);
}
private:
static
const
int
max_err_len
=
1024
;
static
const
int
max_sql_len
=
1024
*
1024
;
void
selectToSql
(
const
SNode
*
node
,
string
&
sql
)
{
SSelectStmt
*
select
=
(
SSelectStmt
*
)
node
;
sql
.
append
(
"SELECT "
);
if
(
select
->
isDistinct
)
{
sql
.
append
(
"DISTINCT "
);
}
...
...
@@ -54,18 +77,16 @@ protected:
sql
.
append
(
"* "
);
}
else
{
nodeListToSql
(
select
->
pProjectionList
,
sql
);
sql
.
append
(
" "
);
}
sql
.
append
(
"FROM "
);
tableToSql
(
select
->
pFromTable
,
sql
);
cout
<<
sql
<<
endl
;
if
(
nullptr
!=
select
->
pWhere
)
{
sql
.
append
(
" WHERE "
);
nodeToSql
(
select
->
pWhere
,
sql
);
}
return
(
code
==
expectCode
);
}
private:
static
const
int
max_err_len
=
1024
;
static
const
int
max_sql_len
=
1024
*
1024
;
void
tableToSql
(
const
SNode
*
node
,
string
&
sql
)
{
const
STableNode
*
table
=
(
const
STableNode
*
)
node
;
switch
(
nodeType
(
node
))
{
...
...
@@ -78,6 +99,88 @@ private:
sql
.
append
(
realTable
->
table
.
tableName
);
break
;
}
case
QUERY_NODE_TEMP_TABLE
:
{
STempTableNode
*
tempTable
=
(
STempTableNode
*
)
table
;
sql
.
append
(
"("
);
selectToSql
(
tempTable
->
pSubquery
,
sql
);
sql
.
append
(
") "
);
sql
.
append
(
tempTable
->
table
.
tableAlias
);
break
;
}
case
QUERY_NODE_JOIN_TABLE
:
{
SJoinTableNode
*
joinTable
=
(
SJoinTableNode
*
)
table
;
tableToSql
(
joinTable
->
pLeft
,
sql
);
sql
.
append
(
" JOIN "
);
tableToSql
(
joinTable
->
pRight
,
sql
);
if
(
nullptr
!=
joinTable
->
pOnCond
)
{
sql
.
append
(
" ON "
);
nodeToSql
(
joinTable
->
pOnCond
,
sql
);
}
break
;
}
default:
break
;
}
}
string
opTypeToSql
(
EOperatorType
type
)
{
switch
(
type
)
{
case
OP_TYPE_ADD
:
return
" + "
;
case
OP_TYPE_SUB
:
return
" - "
;
case
OP_TYPE_MULTI
:
case
OP_TYPE_DIV
:
case
OP_TYPE_MOD
:
case
OP_TYPE_GREATER_THAN
:
case
OP_TYPE_GREATER_EQUAL
:
case
OP_TYPE_LOWER_THAN
:
case
OP_TYPE_LOWER_EQUAL
:
case
OP_TYPE_EQUAL
:
return
" = "
;
case
OP_TYPE_NOT_EQUAL
:
case
OP_TYPE_IN
:
case
OP_TYPE_NOT_IN
:
case
OP_TYPE_LIKE
:
case
OP_TYPE_NOT_LIKE
:
case
OP_TYPE_MATCH
:
case
OP_TYPE_NMATCH
:
case
OP_TYPE_JSON_GET_VALUE
:
case
OP_TYPE_JSON_CONTAINS
:
default:
break
;
}
return
" unknown operator "
;
}
void
nodeToSql
(
const
SNode
*
node
,
string
&
sql
)
{
if
(
nullptr
==
node
)
{
return
;
}
switch
(
nodeType
(
node
))
{
case
QUERY_NODE_COLUMN
:
{
SColumnNode
*
pCol
=
(
SColumnNode
*
)
node
;
if
(
'\0'
!=
pCol
->
dbName
[
0
])
{
sql
.
append
(
pCol
->
dbName
);
sql
.
append
(
"."
);
}
if
(
'\0'
!=
pCol
->
tableAlias
[
0
])
{
sql
.
append
(
pCol
->
tableAlias
);
sql
.
append
(
"."
);
}
sql
.
append
(
pCol
->
colName
);
break
;
}
case
QUERY_NODE_VALUE
:
break
;
case
QUERY_NODE_OPERATOR
:
{
SOperatorNode
*
pOp
=
(
SOperatorNode
*
)
node
;
nodeToSql
(
pOp
->
pLeft
,
sql
);
sql
.
append
(
opTypeToSql
(
pOp
->
opType
));
nodeToSql
(
pOp
->
pRight
,
sql
);
break
;
}
default:
break
;
}
...
...
@@ -91,14 +194,9 @@ private:
sql
.
append
(
", "
);
}
firstNode
=
false
;
switch
(
nodeType
(
node
))
{
case
QUERY_NODE_COLUMN
:
sql
.
append
(((
SColumnNode
*
)
node
)
->
colName
);
break
;
nodeToSql
(
node
,
sql
);
}
}
sql
.
append
(
" "
);
}
void
reset
()
{
memset
(
&
cxt_
,
0
,
sizeof
(
cxt_
));
...
...
@@ -125,10 +223,13 @@ TEST_F(NewParserTest, selectStar) {
bind
(
"SELECT * FROM test.t1"
);
ASSERT_TRUE
(
run
());
bind
(
"SELECT ts FROM t1"
);
bind
(
"SELECT ts, c1 FROM t1"
);
ASSERT_TRUE
(
run
());
bind
(
"SELECT ts, t.c1 FROM (SELECT * FROM t1) t"
);
ASSERT_TRUE
(
run
());
bind
(
"SELECT
ts, tag1, c1 FROM t
1"
);
bind
(
"SELECT
* FROM t1 tt1, t1 tt2 WHERE tt1.c1 = tt2.c
1"
);
ASSERT_TRUE
(
run
());
}
...
...
@@ -147,3 +248,16 @@ TEST_F(NewParserTest, syntaxError) {
bind
(
"SELECT * FROM test.t1 t WHER"
);
ASSERT_TRUE
(
run
(
TSDB_CODE_FAILED
));
}
TEST_F
(
NewParserTest
,
semanticError
)
{
setDatabase
(
"root"
,
"test"
);
bind
(
"SELECT * FROM t10"
);
ASSERT_TRUE
(
run
(
TSDB_CODE_SUCCESS
,
TSDB_CODE_FAILED
));
bind
(
"SELECT c1, c3 FROM t1"
);
ASSERT_TRUE
(
run
(
TSDB_CODE_SUCCESS
,
TSDB_CODE_FAILED
));
bind
(
"SELECT c2 FROM t1 tt1, t1 tt2 WHERE tt1.c1 = tt2.c1"
);
ASSERT_TRUE
(
run
(
TSDB_CODE_SUCCESS
,
TSDB_CODE_FAILED
));
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录