提交 e4cb5c29 编写于 作者: S shenglian zhou

sqlexpr_type_* introduced for validate sql expr

上级 fc8bb51e
...@@ -1893,7 +1893,7 @@ static int32_t handleSQLExprItem(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t ...@@ -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"; const char* msg1 = "invalid column name, illegal column type, or columns in arithmetic expression from two tables";
SColumnList columnList = {0}; SColumnList columnList = {0};
int32_t arithmeticType = NON_ARITHMEIC_EXPR; int32_t arithmeticType = SQLEXPR_TYPE_UNASSIGNED;
uint64_t uid; uint64_t uid;
if (validateSQLExprItem(pCmd, pItem->pNode, pQueryInfo, &columnList, &arithmeticType, &uid) != TSDB_CODE_SUCCESS) { 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 ...@@ -1901,7 +1901,7 @@ static int32_t handleSQLExprItem(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t
} }
int32_t code = TSDB_CODE_SUCCESS; int32_t code = TSDB_CODE_SUCCESS;
if (arithmeticType == NORMAL_ARITHMETIC) { if (arithmeticType == SQLEXPR_TYPE_SCALAR) {
code = handleScalarExpr(pCmd, pQueryInfo, exprIndex, pItem, &columnList, true); code = handleScalarExpr(pCmd, pQueryInfo, exprIndex, pItem, &columnList, true);
} else { } else {
code = handleAggregateExpr(pCmd, pQueryInfo, exprIndex, pItem, &columnList, true); code = handleAggregateExpr(pCmd, pQueryInfo, exprIndex, pItem, &columnList, true);
...@@ -4352,53 +4352,60 @@ static int32_t getJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr* ...@@ -4352,53 +4352,60 @@ static int32_t getJoinCondInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr*
return checkAndSetJoinCondInfo(pCmd, pQueryInfo, pExpr); return checkAndSetJoinCondInfo(pCmd, pQueryInfo, pExpr);
} }
static int32_t validateSQLExprSQLFunc(SSqlCmd* pCmd, tSqlExpr* pExpr, static int32_t validateSQLExprItemSQLFunc(SSqlCmd* pCmd, tSqlExpr* pExpr,
SQueryInfo* pQueryInfo, SColumnList* pList, int32_t* type, uint64_t *uid) { SQueryInfo* pQueryInfo, SColumnList* pList, int32_t* type, uint64_t *uid) {
int32_t code = TSDB_CODE_SUCCESS; int32_t code = TSDB_CODE_SUCCESS;
int32_t functionId = isValidFunction(pExpr->Expr.operand.z, pExpr->Expr.operand.n); 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; pExpr->functionId = functionId;
if (pExpr->Expr.paramList != NULL) { if (pExpr->Expr.paramList != NULL) {
size_t numChildren = taosArrayGetSize(pExpr->Expr.paramList); size_t numChildren = taosArrayGetSize(pExpr->Expr.paramList);
int32_t* childrenTypes = calloc(numChildren, sizeof(int32_t));
for (int32_t i = 0; i < numChildren; ++i) { for (int32_t i = 0; i < numChildren; ++i) {
tSqlExprItem* pParamElem = taosArrayGet(pExpr->Expr.paramList, i); tSqlExprItem* pParamElem = taosArrayGet(pExpr->Expr.paramList, i);
if (!TSDB_FUNC_IS_SCALAR(functionId) && code = validateSQLExprItem(pCmd, pParamElem->pNode, pQueryInfo, pList, childrenTypes + i, uid);
(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) { if (code != TSDB_CODE_SUCCESS) {
free(childrenTypes);
return code; 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)) { }
if (pExpr->Expr.paramList != NULL)
{ {
size_t numChildren = taosArrayGetSize(pExpr->Expr.paramList); bool allChildValue = true;
for (int32_t i = 0; i < numChildren; ++i) { bool anyChildScalar = false;
tSqlExprItem* pParamElem = taosArrayGet(pExpr->Expr.paramList, i); bool anyChildAgg = false;
if (pParamElem->pNode->type == SQL_NODE_EXPR || pParamElem->pNode->type == SQL_NODE_SQLFUNCTION) { 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; 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); int32_t outputIndex = (int32_t)tscNumOfExprs(pQueryInfo);
tSqlExprItem item = {.pNode = pExpr, .aliasName = NULL}; tSqlExprItem item = {.pNode = pExpr, .aliasName = NULL};
...@@ -4450,52 +4457,61 @@ static int32_t validateSQLExprSQLFunc(SSqlCmd* pCmd, tSqlExpr* pExpr, ...@@ -4450,52 +4457,61 @@ static int32_t validateSQLExprSQLFunc(SSqlCmd* pCmd, tSqlExpr* pExpr,
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
static int32_t validateSQLExprItem(SSqlCmd* pCmd, tSqlExpr* pExpr, static int32_t validateSQLExprItemArithmeticExpr(SSqlCmd* pCmd, tSqlExpr* pExpr, SQueryInfo* pQueryInfo, SColumnList* pList,
SQueryInfo* pQueryInfo, SColumnList* pList, int32_t* type, uint64_t* uid) { int32_t* type, uint64_t* uid) {
if (pExpr == NULL) {
return TSDB_CODE_SUCCESS;
}
if (pExpr->type == SQL_NODE_EXPR) {
if (*type == NON_ARITHMEIC_EXPR) {
*type = NORMAL_ARITHMETIC;
}
uint64_t uidLeft = 0; uint64_t uidLeft = 0;
uint64_t uidRight = 0; uint64_t uidRight = 0;
int32_t ret = validateSQLExprItem(pCmd, pExpr->pLeft, pQueryInfo, pList, type, &uidLeft); 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) { if (ret != TSDB_CODE_SUCCESS) {
return ret; return ret;
} }
ret = validateSQLExprItem(pCmd, pExpr->pRight, pQueryInfo, pList, type, &uidRight); ret = validateSQLExprItem(pCmd, pExpr->pRight, pQueryInfo, pList, &rightType, &uidRight);
if (ret != TSDB_CODE_SUCCESS) { if (ret != TSDB_CODE_SUCCESS) {
return ret; return ret;
} }
if (uidLeft != uidRight && uidLeft != 0 && uidRight != 0) {
return TSDB_CODE_TSC_INVALID_OPERATION;
}
*uid = uidLeft;
{ {
tSqlExpr* leftChild = pExpr->pLeft; assert(leftType != SQLEXPR_TYPE_UNASSIGNED && rightType != SQLEXPR_TYPE_UNASSIGNED);
tSqlExpr* rightChild = pExpr->pRight;
// return invalid operation when one child aggregate and the other child scalar or column // 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 (leftType == SQLEXPR_TYPE_AGG && rightType == SQLEXPR_TYPE_SCALAR) {
if ((rightChild->type == SQL_NODE_SQLFUNCTION && TSDB_FUNC_IS_SCALAR(rightChild->functionId)) ||
rightChild->type == SQL_NODE_TABLE_COLUMN) {
return TSDB_CODE_TSC_INVALID_OPERATION; return TSDB_CODE_TSC_INVALID_OPERATION;
} }
} if (rightType == SQLEXPR_TYPE_AGG && leftType == SQLEXPR_TYPE_SCALAR) {
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; 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;
}
if (uidLeft != uidRight && uidLeft != 0 && uidRight != 0) { static int32_t validateSQLExprItem(SSqlCmd* pCmd, tSqlExpr* pExpr,
return TSDB_CODE_TSC_INVALID_OPERATION; SQueryInfo* pQueryInfo, SColumnList* pList, int32_t* type, uint64_t* uid) {
if (pExpr == NULL) {
return TSDB_CODE_SUCCESS;
} }
*uid = uidLeft;
if (pExpr->type == SQL_NODE_EXPR) {
int32_t ret = validateSQLExprItemArithmeticExpr(pCmd, pExpr, pQueryInfo, pList, type, uid);
if (ret != TSDB_CODE_SUCCESS) {
return ret;
}
} else if (pExpr->type == SQL_NODE_SQLFUNCTION) { } else if (pExpr->type == SQL_NODE_SQLFUNCTION) {
int32_t ret = validateSQLExprSQLFunc(pCmd, pExpr, pQueryInfo, pList, type, uid); int32_t ret = validateSQLExprItemSQLFunc(pCmd, pExpr, pQueryInfo, pList, type, uid);
if (ret != TSDB_CODE_SUCCESS) { if (ret != TSDB_CODE_SUCCESS) {
return ret; return ret;
} }
...@@ -4507,10 +4523,16 @@ static int32_t validateSQLExprItem(SSqlCmd* pCmd, tSqlExpr* pExpr, ...@@ -4507,10 +4523,16 @@ static int32_t validateSQLExprItem(SSqlCmd* pCmd, tSqlExpr* pExpr,
} }
pList->ids[pList->num++] = index; pList->ids[pList->num++] = index;
} else if ((pExpr->tokenId == TK_FLOAT && (isnan(pExpr->value.dKey) || isinf(pExpr->value.dKey))) || *type = SQLEXPR_TYPE_SCALAR;
} else {
if ((pExpr->tokenId == TK_FLOAT && (isnan(pExpr->value.dKey) || isinf(pExpr->value.dKey))) ||
pExpr->tokenId == TK_NULL) { pExpr->tokenId == TK_NULL) {
return TSDB_CODE_TSC_INVALID_OPERATION; return TSDB_CODE_TSC_INVALID_OPERATION;
} }
if (pExpr->type == SQL_NODE_VALUE) {
*type = SQLEXPR_TYPE_VALUE;
}
}
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
......
...@@ -28,9 +28,10 @@ extern "C" { ...@@ -28,9 +28,10 @@ extern "C" {
#define ParseTOKENTYPE SStrToken #define ParseTOKENTYPE SStrToken
#define NON_ARITHMEIC_EXPR 0 #define SQLEXPR_TYPE_UNASSIGNED 0
#define NORMAL_ARITHMETIC 1 #define SQLEXPR_TYPE_SCALAR 1
#define AGG_ARIGHTMEIC 2 #define SQLEXPR_TYPE_AGG 2
#define SQLEXPR_TYPE_VALUE 3
enum SQL_NODE_TYPE { enum SQL_NODE_TYPE {
SQL_NODE_TABLE_COLUMN= 1, SQL_NODE_TABLE_COLUMN= 1,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册