提交 5116a654 编写于 作者: S shenglian zhou

add TSQL_NODE_FUNC. before arithmetic traverse modification

上级 ec9fcda9
......@@ -4313,54 +4313,68 @@ static int32_t validateSQLExpr(SSqlCmd* pCmd, tSqlExpr* pExpr, SQueryInfo* pQuer
pExpr->tokenId == TK_NULL) {
return TSDB_CODE_TSC_INVALID_OPERATION;
} else if (pExpr->type == SQL_NODE_SQLFUNCTION) {
if (*type == NON_ARITHMEIC_EXPR) {
*type = AGG_ARIGHTMEIC;
} else if (*type == NORMAL_ARITHMETIC) {
int32_t functionId = isValidFunction(pExpr->Expr.operand.z, pExpr->Expr.operand.n);
if (functionId < 0) {
return TSDB_CODE_TSC_INVALID_OPERATION;
}
int32_t outputIndex = (int32_t)tscNumOfExprs(pQueryInfo);
tSqlExprItem item = {.pNode = pExpr, .aliasName = NULL};
// sql function list in selection clause.
// Append the sqlExpr into exprList of pQueryInfo structure sequentially
pExpr->functionId = isValidFunction(pExpr->Expr.operand.z, pExpr->Expr.operand.n);
if (pExpr->functionId < 0) {
return TSDB_CODE_TSC_INVALID_OPERATION;
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)) {
return TSDB_CODE_TSC_INVALID_OPERATION;
}
} else {
if (TSDB_FUNC_IS_SCALAR(functionId)) {
return TSDB_CODE_TSC_INVALID_OPERATION;
}
}
if (addExprAndResultField(pCmd, pQueryInfo, outputIndex, &item, false, NULL) != TSDB_CODE_SUCCESS) {
return TSDB_CODE_TSC_INVALID_OPERATION;
}
if (*type == AGG_ARIGHTMEIC) {
int32_t outputIndex = (int32_t)tscNumOfExprs(pQueryInfo);
// It is invalid in case of more than one sqlExpr, such as first(ts, k) - last(ts, k)
int32_t inc = (int32_t) tscNumOfExprs(pQueryInfo) - outputIndex;
if (inc > 1) {
return TSDB_CODE_TSC_INVALID_OPERATION;
}
tSqlExprItem item = {.pNode = pExpr, .aliasName = NULL};
// Not supported data type in arithmetic expression
uint64_t id = -1;
for(int32_t i = 0; i < inc; ++i) {
SExprInfo* p1 = tscExprGet(pQueryInfo, i + outputIndex);
// sql function list in selection clause.
// Append the sqlExpr into exprList of pQueryInfo structure sequentially
pExpr->functionId = functionId;
int16_t t = p1->base.resType;
if (t == TSDB_DATA_TYPE_BINARY || t == TSDB_DATA_TYPE_NCHAR || t == TSDB_DATA_TYPE_BOOL || t == TSDB_DATA_TYPE_TIMESTAMP) {
if (addExprAndResultField(pCmd, pQueryInfo, outputIndex, &item, false, NULL) != TSDB_CODE_SUCCESS) {
return TSDB_CODE_TSC_INVALID_OPERATION;
}
if (i == 0) {
id = p1->base.uid;
continue;
// It is invalid in case of more than one sqlExpr, such as first(ts, k) - last(ts, k)
int32_t inc = (int32_t)tscNumOfExprs(pQueryInfo) - outputIndex;
if (inc > 1) {
return TSDB_CODE_TSC_INVALID_OPERATION;
}
if (id != p1->base.uid) {
return TSDB_CODE_TSC_INVALID_OPERATION;
// Not supported data type in arithmetic expression
uint64_t id = -1;
for (int32_t i = 0; i < inc; ++i) {
SExprInfo* p1 = tscExprGet(pQueryInfo, i + outputIndex);
int16_t t = p1->base.resType;
if (t == TSDB_DATA_TYPE_BINARY || t == TSDB_DATA_TYPE_NCHAR || t == TSDB_DATA_TYPE_BOOL ||
t == TSDB_DATA_TYPE_TIMESTAMP) {
return TSDB_CODE_TSC_INVALID_OPERATION;
}
if (i == 0) {
id = p1->base.uid;
continue;
}
if (id != p1->base.uid) {
return TSDB_CODE_TSC_INVALID_OPERATION;
}
}
}
*uid = id;
*uid = id;
}
}
return TSDB_CODE_SUCCESS;
......@@ -9310,30 +9324,50 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS
}
return ret;
} else if (pSqlExpr->type == SQL_NODE_SQLFUNCTION) {
// arithmetic expression on the results of aggregation functions
*pExpr = calloc(1, sizeof(tExprNode));
(*pExpr)->nodeType = TSQL_NODE_COL;
(*pExpr)->pSchema = calloc(1, sizeof(SSchema));
strncpy((*pExpr)->pSchema->name, pSqlExpr->exprToken.z, pSqlExpr->exprToken.n);
// set the input column data byte and type.
size_t size = taosArrayGetSize(pQueryInfo->exprList);
for (int32_t i = 0; i < size; ++i) {
SExprInfo* p1 = taosArrayGetP(pQueryInfo->exprList, i);
if (strcmp((*pExpr)->pSchema->name, p1->base.aliasName) == 0) {
(*pExpr)->pSchema->type = (uint8_t)p1->base.resType;
(*pExpr)->pSchema->bytes = p1->base.resBytes;
(*pExpr)->pSchema->colId = p1->base.resColId;
if (uid != NULL) {
*uid = p1->base.uid;
if (TSDB_FUNC_IS_SCALAR(pSqlExpr->functionId)) {
*pExpr = calloc(1, sizeof(tExprNode));
(*pExpr)->nodeType = TSQL_NODE_FUNC;
(*pExpr)->_func.functionId = pSqlExpr->functionId;
SArray* paramList = pSqlExpr->Expr.paramList;
size_t paramSize = taosArrayGetSize(paramList);
if (paramSize > 0) {
(*pExpr)->_func.numChilds = paramSize;
(*pExpr)->_func.pChilds = (tExprNode**)calloc(paramSize, sizeof(tExprNode*));
}
for (int i = 0; i < taosArrayGetSize(paramList); ++i) {
tSqlExprItem* param = taosArrayGet(paramList, i);
tSqlExpr* paramNode = param->pNode;
int32_t ret = exprTreeFromSqlExpr(pCmd, (*pExpr)->_func.pChilds+i, paramNode, pQueryInfo, pCols, uid);
if (ret != TSDB_CODE_SUCCESS) {
return ret;
}
break;
}
}
} else {
// arithmetic expression on the results of aggregation functions
*pExpr = calloc(1, sizeof(tExprNode));
(*pExpr)->nodeType = TSQL_NODE_COL;
(*pExpr)->pSchema = calloc(1, sizeof(SSchema));
strncpy((*pExpr)->pSchema->name, pSqlExpr->exprToken.z, pSqlExpr->exprToken.n);
// set the input column data byte and type.
size_t size = taosArrayGetSize(pQueryInfo->exprList);
for (int32_t i = 0; i < size; ++i) {
SExprInfo* p1 = taosArrayGetP(pQueryInfo->exprList, i);
if (strcmp((*pExpr)->pSchema->name, p1->base.aliasName) == 0) {
(*pExpr)->pSchema->type = (uint8_t)p1->base.resType;
(*pExpr)->pSchema->bytes = p1->base.resBytes;
(*pExpr)->pSchema->colId = p1->base.resColId;
if (uid != NULL) {
*uid = p1->base.uid;
}
break;
}
} // endfor each expr
} // end not scalar function
} else if (pSqlExpr->type == SQL_NODE_TABLE_COLUMN) { // column name, normal column arithmetic expression
int32_t ret = getColumnIndexByName(&pSqlExpr->columnName, pQueryInfo, &index, tscGetErrorMsgPayload(pCmd));
if (ret != TSDB_CODE_SUCCESS) {
......
......@@ -49,6 +49,7 @@ enum {
TSQL_NODE_EXPR = 0x1,
TSQL_NODE_COL = 0x2,
TSQL_NODE_VALUE = 0x4,
TSQL_NODE_FUNC = 0x8
};
/**
......@@ -74,7 +75,14 @@ typedef struct tExprNode {
} _node;
struct SSchema *pSchema;
tVariant *pVal;
struct {
int16_t functionId;
uint8_t numChilds;
struct tExprNode** pChilds;
} _func;
};
} tExprNode;
......
......@@ -119,6 +119,8 @@ void tExprTreeDestroy(tExprNode *pNode, void (*fp)(void *)) {
tVariantDestroy(pNode->pVal);
} else if (pNode->nodeType == TSQL_NODE_COL) {
tfree(pNode->pSchema);
} else if (pNode->nodeType == TSQL_NODE_FUNC) {
doExprTreeDestroy(&pNode, fp);
}
free(pNode);
......@@ -141,6 +143,10 @@ static void doExprTreeDestroy(tExprNode **pExpr, void (*fp)(void *)) {
free((*pExpr)->pVal);
} else if ((*pExpr)->nodeType == TSQL_NODE_COL) {
free((*pExpr)->pSchema);
} else if ((*pExpr)->nodeType == TSQL_NODE_FUNC) {
for (int i = 0; i < (*pExpr)->_func.numChilds; ++i) {
doExprTreeDestroy((*pExpr)->_func.pChilds + i, fp);
}
}
free(*pExpr);
......@@ -307,6 +313,12 @@ static void exprTreeToBinaryImpl(SBufferWriter* bw, tExprNode* expr) {
tbufWriteUint8(bw, expr->_node.hasPK);
exprTreeToBinaryImpl(bw, expr->_node.pLeft);
exprTreeToBinaryImpl(bw, expr->_node.pRight);
} else if (expr->nodeType == TSQL_NODE_FUNC) {
tbufWriteInt16(bw, expr->_func.functionId);
tbufWriteUint8(bw, expr->_func.numChilds);
for (int i = 0; i < expr->_func.numChilds; ++i) {
exprTreeToBinaryImpl(bw, expr->_func.pChilds[i]);
}
}
}
......@@ -372,6 +384,13 @@ static tExprNode* exprTreeFromBinaryImpl(SBufferReader* br) {
pExpr->_node.pLeft = exprTreeFromBinaryImpl(br);
pExpr->_node.pRight = exprTreeFromBinaryImpl(br);
assert(pExpr->_node.pLeft != NULL && pExpr->_node.pRight != NULL);
} else if (pExpr->nodeType == TSQL_NODE_FUNC) {
pExpr->_func.functionId = tbufReadInt16(br);
pExpr->_func.numChilds = tbufReadUint8(br);
pExpr->_func.pChilds = (tExprNode**)calloc(pExpr->_func.numChilds, sizeof(tExprNode*));
for (int i = 0; i < pExpr->_func.numChilds; ++i) {
pExpr->_func.pChilds[i] = exprTreeFromBinaryImpl(br);
}
}
CLEANUP_EXECUTE_TO(anchor, false);
......@@ -620,6 +639,12 @@ tExprNode* exprdup(tExprNode* pNode) {
} else if (pNode->nodeType == TSQL_NODE_COL) {
pCloned->pSchema = calloc(1, sizeof(SSchema));
*pCloned->pSchema = *pNode->pSchema;
} else if (pNode->nodeType == TSQL_NODE_FUNC) {
pCloned->_func.functionId = pNode->_func.functionId;
pCloned->_func.numChilds = pNode->_func.numChilds;
for (int i = 0; i < pNode->_func.numChilds; ++i) {
pCloned->_func.pChilds[i] = exprdup(pNode->_func.pChilds[i]);
}
}
pCloned->nodeType = pNode->nodeType;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册