Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
e4cb5c29
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看板
提交
e4cb5c29
编写于
11月 11, 2021
作者:
S
shenglian zhou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
sqlexpr_type_* introduced for validate sql expr
上级
fc8bb51e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
94 addition
and
71 deletion
+94
-71
src/client/src/tscSQLParser.c
src/client/src/tscSQLParser.c
+90
-68
src/query/inc/qSqlparser.h
src/query/inc/qSqlparser.h
+4
-3
未找到文件。
src/client/src/tscSQLParser.c
浏览文件 @
e4cb5c29
...
...
@@ -1893,7 +1893,7 @@ static int32_t handleSQLExprItem(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t
const
char
*
msg1
=
"invalid column name, illegal column type, or columns in arithmetic expression from two tables"
;
SColumnList
columnList
=
{
0
};
int32_t
arithmeticType
=
NON_ARITHMEIC_EXPR
;
int32_t
arithmeticType
=
SQLEXPR_TYPE_UNASSIGNED
;
uint64_t
uid
;
if
(
validateSQLExprItem
(
pCmd
,
pItem
->
pNode
,
pQueryInfo
,
&
columnList
,
&
arithmeticType
,
&
uid
)
!=
TSDB_CODE_SUCCESS
)
{
...
...
@@ -1901,7 +1901,7 @@ static int32_t handleSQLExprItem(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t
}
int32_t
code
=
TSDB_CODE_SUCCESS
;
if
(
arithmeticType
==
NORMAL_ARITHMETIC
)
{
if
(
arithmeticType
==
SQLEXPR_TYPE_SCALAR
)
{
code
=
handleScalarExpr
(
pCmd
,
pQueryInfo
,
exprIndex
,
pItem
,
&
columnList
,
true
);
}
else
{
code
=
handleAggregateExpr
(
pCmd
,
pQueryInfo
,
exprIndex
,
pItem
,
&
columnList
,
true
);
...
...
@@ -4352,53 +4352,60 @@ static int32_t getJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr*
return
checkAndSetJoinCondInfo
(
pCmd
,
pQueryInfo
,
pExpr
);
}
static
int32_t
validateSQLExprSQLFunc
(
SSqlCmd
*
pCmd
,
tSqlExpr
*
pExpr
,
static
int32_t
validateSQLExpr
Item
SQLFunc
(
SSqlCmd
*
pCmd
,
tSqlExpr
*
pExpr
,
SQueryInfo
*
pQueryInfo
,
SColumnList
*
pList
,
int32_t
*
type
,
uint64_t
*
uid
)
{
int32_t
code
=
TSDB_CODE_SUCCESS
;
int32_t
functionId
=
isValidFunction
(
pExpr
->
Expr
.
operand
.
z
,
pExpr
->
Expr
.
operand
.
n
);
if
(
*
type
==
NON_ARITHMEIC_EXPR
)
{
if
(
TSDB_FUNC_IS_SCALAR
(
functionId
))
{
*
type
=
NORMAL_ARITHMETIC
;
}
else
{
*
type
=
AGG_ARIGHTMEIC
;
}
}
else
if
(
*
type
==
NORMAL_ARITHMETIC
)
{
if
(
!
TSDB_FUNC_IS_SCALAR
(
functionId
))
{
*
type
=
AGG_ARIGHTMEIC
;
}
}
pExpr
->
functionId
=
functionId
;
if
(
pExpr
->
Expr
.
paramList
!=
NULL
)
{
size_t
numChildren
=
taosArrayGetSize
(
pExpr
->
Expr
.
paramList
);
int32_t
*
childrenTypes
=
calloc
(
numChildren
,
sizeof
(
int32_t
));
for
(
int32_t
i
=
0
;
i
<
numChildren
;
++
i
)
{
tSqlExprItem
*
pParamElem
=
taosArrayGet
(
pExpr
->
Expr
.
paramList
,
i
);
code
=
validateSQLExprItem
(
pCmd
,
pParamElem
->
pNode
,
pQueryInfo
,
pList
,
childrenTypes
+
i
,
uid
);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
free
(
childrenTypes
);
return
code
;
}
if
(
!
TSDB_FUNC_IS_SCALAR
(
functionId
)
&&
(
pParamElem
->
pNode
->
type
==
SQL_NODE_EXPR
||
pParamElem
->
pNode
->
type
==
SQL_NODE_SQLFUNCTION
))
{
return
TSDB_CODE_TSC_INVALID_OPERATION
;
}
if
(
TSDB_FUNC_IS_SCALAR
(
functionId
))
{
code
=
validateSQLExprItem
(
pCmd
,
pParamElem
->
pNode
,
pQueryInfo
,
pList
,
type
,
uid
);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
return
code
;
}
}
}
}
if
(
!
TSDB_FUNC_IS_SCALAR
(
functionId
))
{
if
(
pExpr
->
Expr
.
paramList
!=
NULL
)
}
{
size_t
numChildren
=
taosArrayGetSize
(
pExpr
->
Expr
.
paramList
);
for
(
int32_t
i
=
0
;
i
<
numChildren
;
++
i
)
{
tSqlExprItem
*
pParamElem
=
taosArrayGet
(
pExpr
->
Expr
.
paramList
,
i
);
if
(
pParamElem
->
pNode
->
type
==
SQL_NODE_EXPR
||
pParamElem
->
pNode
->
type
==
SQL_NODE_SQLFUNCTION
)
{
return
TSDB_CODE_TSC_INVALID_OPERATION
;
bool
allChildValue
=
true
;
bool
anyChildScalar
=
false
;
bool
anyChildAgg
=
false
;
for
(
int
i
=
0
;
i
<
numChildren
;
++
i
)
{
assert
(
childrenTypes
[
i
]
!=
SQLEXPR_TYPE_UNASSIGNED
);
allChildValue
=
allChildValue
&&
(
childrenTypes
[
i
]
==
SQLEXPR_TYPE_VALUE
);
anyChildScalar
=
anyChildScalar
||
(
childrenTypes
[
i
]
==
SQLEXPR_TYPE_SCALAR
);
anyChildAgg
=
anyChildAgg
||
(
childrenTypes
[
i
]
==
SQLEXPR_TYPE_AGG
);
}
if
(
anyChildAgg
&&
anyChildScalar
)
{
return
TSDB_CODE_TSC_INVALID_OPERATION
;
}
if
(
TSDB_FUNC_IS_SCALAR
(
functionId
))
{
if
(
anyChildAgg
)
{
*
type
=
SQLEXPR_TYPE_AGG
;
}
else
if
(
allChildValue
)
{
*
type
=
SQLEXPR_TYPE_VALUE
;
}
else
{
*
type
=
SQLEXPR_TYPE_SCALAR
;
}
}
else
{
*
type
=
SQLEXPR_TYPE_AGG
;
}
}
free
(
childrenTypes
);
}
if
(
!
TSDB_FUNC_IS_SCALAR
(
functionId
))
{
int32_t
outputIndex
=
(
int32_t
)
tscNumOfExprs
(
pQueryInfo
);
tSqlExprItem
item
=
{.
pNode
=
pExpr
,
.
aliasName
=
NULL
};
...
...
@@ -4450,6 +4457,48 @@ static int32_t validateSQLExprSQLFunc(SSqlCmd* pCmd, tSqlExpr* pExpr,
return
TSDB_CODE_SUCCESS
;
}
static
int32_t
validateSQLExprItemArithmeticExpr
(
SSqlCmd
*
pCmd
,
tSqlExpr
*
pExpr
,
SQueryInfo
*
pQueryInfo
,
SColumnList
*
pList
,
int32_t
*
type
,
uint64_t
*
uid
)
{
uint64_t
uidLeft
=
0
;
uint64_t
uidRight
=
0
;
int32_t
leftType
=
SQLEXPR_TYPE_UNASSIGNED
;
int32_t
rightType
=
SQLEXPR_TYPE_UNASSIGNED
;
int32_t
ret
=
validateSQLExprItem
(
pCmd
,
pExpr
->
pLeft
,
pQueryInfo
,
pList
,
&
leftType
,
&
uidLeft
);
if
(
ret
!=
TSDB_CODE_SUCCESS
)
{
return
ret
;
}
ret
=
validateSQLExprItem
(
pCmd
,
pExpr
->
pRight
,
pQueryInfo
,
pList
,
&
rightType
,
&
uidRight
);
if
(
ret
!=
TSDB_CODE_SUCCESS
)
{
return
ret
;
}
if
(
uidLeft
!=
uidRight
&&
uidLeft
!=
0
&&
uidRight
!=
0
)
{
return
TSDB_CODE_TSC_INVALID_OPERATION
;
}
*
uid
=
uidLeft
;
{
assert
(
leftType
!=
SQLEXPR_TYPE_UNASSIGNED
&&
rightType
!=
SQLEXPR_TYPE_UNASSIGNED
);
// return invalid operation when one child aggregate and the other child scalar or column
if
(
leftType
==
SQLEXPR_TYPE_AGG
&&
rightType
==
SQLEXPR_TYPE_SCALAR
)
{
return
TSDB_CODE_TSC_INVALID_OPERATION
;
}
if
(
rightType
==
SQLEXPR_TYPE_AGG
&&
leftType
==
SQLEXPR_TYPE_SCALAR
)
{
return
TSDB_CODE_TSC_INVALID_OPERATION
;
}
if
(
leftType
==
SQLEXPR_TYPE_AGG
||
rightType
==
SQLEXPR_TYPE_AGG
)
{
*
type
=
SQLEXPR_TYPE_AGG
;
}
else
if
(
leftType
==
SQLEXPR_TYPE_VALUE
&&
rightType
==
SQLEXPR_TYPE_VALUE
)
{
*
type
=
SQLEXPR_TYPE_VALUE
;
}
else
if
(
leftType
==
SQLEXPR_TYPE_SCALAR
||
rightType
==
SQLEXPR_TYPE_SCALAR
){
*
type
=
SQLEXPR_TYPE_SCALAR
;
}
}
return
TSDB_CODE_SUCCESS
;
}
static
int32_t
validateSQLExprItem
(
SSqlCmd
*
pCmd
,
tSqlExpr
*
pExpr
,
SQueryInfo
*
pQueryInfo
,
SColumnList
*
pList
,
int32_t
*
type
,
uint64_t
*
uid
)
{
if
(
pExpr
==
NULL
)
{
...
...
@@ -4457,45 +4506,12 @@ static int32_t validateSQLExprItem(SSqlCmd* pCmd, tSqlExpr* pExpr,
}
if
(
pExpr
->
type
==
SQL_NODE_EXPR
)
{
if
(
*
type
==
NON_ARITHMEIC_EXPR
)
{
*
type
=
NORMAL_ARITHMETIC
;
}
uint64_t
uidLeft
=
0
;
uint64_t
uidRight
=
0
;
int32_t
ret
=
validateSQLExprItem
(
pCmd
,
pExpr
->
pLeft
,
pQueryInfo
,
pList
,
type
,
&
uidLeft
);
if
(
ret
!=
TSDB_CODE_SUCCESS
)
{
return
ret
;
}
ret
=
validateSQLExprItem
(
pCmd
,
pExpr
->
pRight
,
pQueryInfo
,
pList
,
type
,
&
uidRight
);
int32_t
ret
=
validateSQLExprItemArithmeticExpr
(
pCmd
,
pExpr
,
pQueryInfo
,
pList
,
type
,
uid
);
if
(
ret
!=
TSDB_CODE_SUCCESS
)
{
return
ret
;
}
{
tSqlExpr
*
leftChild
=
pExpr
->
pLeft
;
tSqlExpr
*
rightChild
=
pExpr
->
pRight
;
// return invalid operation when one child aggregate and the other child scalar or column
if
(
leftChild
->
type
==
SQL_NODE_SQLFUNCTION
&&
!
TSDB_FUNC_IS_SCALAR
(
leftChild
->
functionId
))
{
if
((
rightChild
->
type
==
SQL_NODE_SQLFUNCTION
&&
TSDB_FUNC_IS_SCALAR
(
rightChild
->
functionId
))
||
rightChild
->
type
==
SQL_NODE_TABLE_COLUMN
)
{
return
TSDB_CODE_TSC_INVALID_OPERATION
;
}
}
if
(
rightChild
->
type
==
SQL_NODE_SQLFUNCTION
&&
!
TSDB_FUNC_IS_SCALAR
(
rightChild
->
functionId
))
{
if
((
leftChild
->
type
==
SQL_NODE_SQLFUNCTION
&&
TSDB_FUNC_IS_SCALAR
(
leftChild
->
functionId
))
||
leftChild
->
type
==
SQL_NODE_TABLE_COLUMN
)
{
return
TSDB_CODE_TSC_INVALID_OPERATION
;
}
}
}
if
(
uidLeft
!=
uidRight
&&
uidLeft
!=
0
&&
uidRight
!=
0
)
{
return
TSDB_CODE_TSC_INVALID_OPERATION
;
}
*
uid
=
uidLeft
;
}
else
if
(
pExpr
->
type
==
SQL_NODE_SQLFUNCTION
)
{
int32_t
ret
=
validateSQLExprSQLFunc
(
pCmd
,
pExpr
,
pQueryInfo
,
pList
,
type
,
uid
);
int32_t
ret
=
validateSQLExpr
Item
SQLFunc
(
pCmd
,
pExpr
,
pQueryInfo
,
pList
,
type
,
uid
);
if
(
ret
!=
TSDB_CODE_SUCCESS
)
{
return
ret
;
}
...
...
@@ -4507,9 +4523,15 @@ static int32_t validateSQLExprItem(SSqlCmd* pCmd, tSqlExpr* pExpr,
}
pList
->
ids
[
pList
->
num
++
]
=
index
;
}
else
if
((
pExpr
->
tokenId
==
TK_FLOAT
&&
(
isnan
(
pExpr
->
value
.
dKey
)
||
isinf
(
pExpr
->
value
.
dKey
)))
||
pExpr
->
tokenId
==
TK_NULL
)
{
return
TSDB_CODE_TSC_INVALID_OPERATION
;
*
type
=
SQLEXPR_TYPE_SCALAR
;
}
else
{
if
((
pExpr
->
tokenId
==
TK_FLOAT
&&
(
isnan
(
pExpr
->
value
.
dKey
)
||
isinf
(
pExpr
->
value
.
dKey
)))
||
pExpr
->
tokenId
==
TK_NULL
)
{
return
TSDB_CODE_TSC_INVALID_OPERATION
;
}
if
(
pExpr
->
type
==
SQL_NODE_VALUE
)
{
*
type
=
SQLEXPR_TYPE_VALUE
;
}
}
return
TSDB_CODE_SUCCESS
;
}
...
...
src/query/inc/qSqlparser.h
浏览文件 @
e4cb5c29
...
...
@@ -28,9 +28,10 @@ extern "C" {
#define ParseTOKENTYPE SStrToken
#define NON_ARITHMEIC_EXPR 0
#define NORMAL_ARITHMETIC 1
#define AGG_ARIGHTMEIC 2
#define SQLEXPR_TYPE_UNASSIGNED 0
#define SQLEXPR_TYPE_SCALAR 1
#define SQLEXPR_TYPE_AGG 2
#define SQLEXPR_TYPE_VALUE 3
enum
SQL_NODE_TYPE
{
SQL_NODE_TABLE_COLUMN
=
1
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录