diff --git a/src/common/inc/texpr.h b/src/common/inc/texpr.h index 311f65073367e2b2a2c5e436b4655cf12ef4c02a..2235d2271081054e60898e5ef117d54dc898ce32 100644 --- a/src/common/inc/texpr.h +++ b/src/common/inc/texpr.h @@ -65,7 +65,12 @@ struct SSchema; #define TSDB_FUNC_SCALAR_CONCAT_WS (TSDB_FUNC_FLAG_SCALAR | 0x000F) #define TSDB_FUNC_SCALAR_CHAR_LENGTH (TSDB_FUNC_FLAG_SCALAR | 0x0010) #define TSDB_FUNC_SCALAR_CAST (TSDB_FUNC_FLAG_SCALAR | 0x0011) -#define TSDB_FUNC_SCALAR_MAX_NUM 18 +#define TSDB_FUNC_SCALAR_LOWER (TSDB_FUNC_FLAG_SCALAR | 0x0012) +#define TSDB_FUNC_SCALAR_UPPER (TSDB_FUNC_FLAG_SCALAR | 0x0013) +#define TSDB_FUNC_SCALAR_LTRIM (TSDB_FUNC_FLAG_SCALAR | 0x0014) +#define TSDB_FUNC_SCALAR_RTRIM (TSDB_FUNC_FLAG_SCALAR | 0x0015) +#define TSDB_FUNC_SCALAR_SUBSTR (TSDB_FUNC_FLAG_SCALAR | 0x0016) +#define TSDB_FUNC_SCALAR_MAX_NUM 23 #define TSDB_FUNC_SCALAR_NAME_MAX_LEN 16 diff --git a/src/common/src/texpr.c b/src/common/src/texpr.c index db3bec2ddfeee60a90df33cae4c9770bfdb7c46f..1fe05c232fb5253e5d45d8a6fee063075398c4d3 100644 --- a/src/common/src/texpr.c +++ b/src/common/src/texpr.c @@ -33,6 +33,8 @@ static int32_t exprValidateMathNode(tExprNode *pExpr); static int32_t exprValidateStringConcatNode(tExprNode *pExpr); static int32_t exprValidateStringConcatWsNode(tExprNode *pExpr); static int32_t exprValidateStringLengthNode(tExprNode *pExpr); +static int32_t exprValidateStringLowerUpperTrimNode(char* msgBuf, tExprNode *pExpr); +static int32_t exprValidateStringSubstrNode(char* msgBuf, tExprNode *pExpr); static int32_t exprValidateCastNode(char* msgbuf, tExprNode *pExpr); static int32_t exprInvalidOperationMsg(char *msgbuf, const char *msg) { @@ -77,6 +79,15 @@ int32_t exprTreeValidateFunctionNode(char* msgbuf, tExprNode *pExpr) { case TSDB_FUNC_SCALAR_CONCAT_WS: { return exprValidateStringConcatWsNode(pExpr); } + case TSDB_FUNC_SCALAR_LOWER: + case TSDB_FUNC_SCALAR_UPPER: + case TSDB_FUNC_SCALAR_LTRIM: + case TSDB_FUNC_SCALAR_RTRIM: { + return exprValidateStringLowerUpperTrimNode(msgbuf, pExpr); + } + case TSDB_FUNC_SCALAR_SUBSTR: { + return exprValidateStringSubstrNode(msgbuf, pExpr); + } default: break; @@ -1042,6 +1053,58 @@ int32_t exprValidateStringLengthNode(tExprNode *pExpr) { return TSDB_CODE_SUCCESS; } +int32_t exprValidateStringLowerUpperTrimNode(char* msgBuf, tExprNode *pExpr) { + if (pExpr->_func.numChildren != 1) { + return TSDB_CODE_TSC_INVALID_OPERATION; + } + + tExprNode* child1 = pExpr->_func.pChildren[0]; + + if (child1->nodeType == TSQL_NODE_VALUE) { + child1->resultType = (int16_t)child1->pVal->nType; + child1->resultBytes = (int16_t)(child1->pVal->nLen + VARSTR_HEADER_SIZE); + } + + if (!IS_VAR_DATA_TYPE(child1->resultType)) { + return TSDB_CODE_TSC_INVALID_OPERATION; + } + + pExpr->resultType = child1->resultType; + pExpr->resultBytes = child1->resultBytes; + + return TSDB_CODE_SUCCESS; +} + +int32_t exprValidateStringSubstrNode(char* msgBuf, tExprNode *pExpr) { + if (pExpr->_func.numChildren != 2 || pExpr->_func.numChildren != 3) { + return TSDB_CODE_TSC_INVALID_OPERATION; + } + + tExprNode* child1 = pExpr->_func.pChildren[0]; + + if (child1->nodeType == TSQL_NODE_VALUE) { + child1->resultType = (int16_t)child1->pVal->nType; + child1->resultBytes = (int16_t)(child1->pVal->nLen + VARSTR_HEADER_SIZE); + } + + if (!IS_VAR_DATA_TYPE(child1->resultType)) { + return TSDB_CODE_TSC_INVALID_OPERATION; + } + + tExprNode* pos = pExpr->_func.pChildren[1]; + tExprNode* length = (pExpr->_func.numChildren == 3) ? pExpr->_func.pChildren[2] : NULL; + + if (!IS_NUMERIC_TYPE(pos->resultType) || + (length != NULL && !IS_NUMERIC_TYPE(length->resultType))) { + return TSDB_CODE_TSC_INVALID_OPERATION; + } + + pExpr->resultType = child1->resultType; + pExpr->resultBytes = child1->resultBytes; + + return TSDB_CODE_SUCCESS; +} + int32_t exprValidateCastNode(char* msgbuf, tExprNode *pExpr) { const char* msg1 = "invalid param num for cast function"; const char* msg2 = "the second param should be a valid type name for cast function"; @@ -1409,6 +1472,16 @@ void vectorCharLength(int16_t functionId, tExprOperandInfo *pInputs, int32_t num } } +void vectorLowerUpperTrimFunc(int16_t functionId, tExprOperandInfo *pInputs, int32_t numInputs, tExprOperandInfo* pOutput, int32_t order) { + assert(numInputs == 1); + assert(pInputs[0].numOfRows == 1 || pInputs[0].numOfRows == pOutput->numOfRows); + +} + +void vectorSubstrFunc(int16_t functionId, tExprOperandInfo *pInputs, int32_t numInputs, tExprOperandInfo* pOutput, int32_t order) { + +} + void vectorMathFunc(int16_t functionId, tExprOperandInfo *pInputs, int32_t numInputs, tExprOperandInfo* pOutput, int32_t order) { for (int i = 0; i < numInputs; ++i) { assert(pInputs[i].numOfRows == 1 || pInputs[i].numOfRows == pOutput->numOfRows); @@ -1724,4 +1797,30 @@ tScalarFunctionInfo aScalarFunctions[] = { "cast", vectorMathFunc }, + { + TSDB_FUNC_SCALAR_LOWER, + "lower", + vectorLowerUpperTrimFunc + }, + { + TSDB_FUNC_SCALAR_UPPER, + "upper", + vectorLowerUpperTrimFunc + }, + { + TSDB_FUNC_SCALAR_LTRIM, + "ltrim", + vectorLowerUpperTrimFunc + }, + { + TSDB_FUNC_SCALAR_RTRIM, + "rtrim", + vectorLowerUpperTrimFunc + }, + { + TSDB_FUNC_SCALAR_SUBSTR, + "substr", + vectorSubstrFunc + }, + };