From a3a959043ea09943b7ada3aeab24983ad696ba26 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Sun, 5 Jun 2022 22:25:10 +0800 Subject: [PATCH] feat: test arith/logic operator for json --- include/util/tdef.h | 2 +- source/libs/nodes/src/nodesUtilFuncs.c | 2 +- source/libs/parser/inc/sql.y | 2 +- source/libs/parser/src/sql.c | 2 +- source/libs/scalar/src/filter.c | 2 +- source/libs/scalar/src/sclvector.c | 35 +++++++++++-------- .../libs/scalar/test/scalar/scalarTests.cpp | 6 ++-- 7 files changed, 28 insertions(+), 23 deletions(-) diff --git a/include/util/tdef.h b/include/util/tdef.h index 0ae22d1953..ebfb7b696a 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -129,7 +129,7 @@ typedef enum EOperatorType { OP_TYPE_SUB, OP_TYPE_MULTI, OP_TYPE_DIV, - OP_TYPE_MOD, + OP_TYPE_REM, // unary arithmetic operator OP_TYPE_MINUS, OP_TYPE_ASSIGN, diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index 76f15afc8e..d160cb8f44 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -1089,7 +1089,7 @@ bool nodesIsArithmeticOp(const SOperatorNode* pOp) { case OP_TYPE_SUB: case OP_TYPE_MULTI: case OP_TYPE_DIV: - case OP_TYPE_MOD: + case OP_TYPE_REM: return true; default: break; diff --git a/source/libs/parser/inc/sql.y b/source/libs/parser/inc/sql.y index 6c090a0790..5e8ab9b9aa 100644 --- a/source/libs/parser/inc/sql.y +++ b/source/libs/parser/inc/sql.y @@ -608,7 +608,7 @@ expression(A) ::= expression(B) NK_SLASH expression(C). expression(A) ::= expression(B) NK_REM expression(C). { SToken s = getTokenFromRawExprNode(pCxt, B); SToken e = getTokenFromRawExprNode(pCxt, C); - A = createRawExprNodeExt(pCxt, &s, &e, createOperatorNode(pCxt, OP_TYPE_MOD, releaseRawExprNode(pCxt, B), releaseRawExprNode(pCxt, C))); + A = createRawExprNodeExt(pCxt, &s, &e, createOperatorNode(pCxt, OP_TYPE_REM, releaseRawExprNode(pCxt, B), releaseRawExprNode(pCxt, C))); } expression(A) ::= column_reference(B) NK_ARROW NK_STRING(C). { SToken s = getTokenFromRawExprNode(pCxt, B); diff --git a/source/libs/parser/src/sql.c b/source/libs/parser/src/sql.c index ff4fe4032e..cd84495b9b 100644 --- a/source/libs/parser/src/sql.c +++ b/source/libs/parser/src/sql.c @@ -4540,7 +4540,7 @@ static YYACTIONTYPE yy_reduce( { SToken s = getTokenFromRawExprNode(pCxt, yymsp[-2].minor.yy686); SToken e = getTokenFromRawExprNode(pCxt, yymsp[0].minor.yy686); - yylhsminor.yy686 = createRawExprNodeExt(pCxt, &s, &e, createOperatorNode(pCxt, OP_TYPE_MOD, releaseRawExprNode(pCxt, yymsp[-2].minor.yy686), releaseRawExprNode(pCxt, yymsp[0].minor.yy686))); + yylhsminor.yy686 = createRawExprNodeExt(pCxt, &s, &e, createOperatorNode(pCxt, OP_TYPE_REM, releaseRawExprNode(pCxt, yymsp[-2].minor.yy686), releaseRawExprNode(pCxt, yymsp[0].minor.yy686))); } yymsp[-2].minor.yy686 = yylhsminor.yy686; break; diff --git a/source/libs/scalar/src/filter.c b/source/libs/scalar/src/filter.c index 2a23cbeed5..5fdbbfe810 100644 --- a/source/libs/scalar/src/filter.c +++ b/source/libs/scalar/src/filter.c @@ -29,7 +29,7 @@ OptrStr gOptrStr[] = { {OP_TYPE_SUB, "-"}, {OP_TYPE_MULTI, "*"}, {OP_TYPE_DIV, "/"}, - {OP_TYPE_MOD, "%"}, + {OP_TYPE_REM, "%"}, {OP_TYPE_MINUS, "minus"}, {OP_TYPE_ASSIGN, "assign"}, // bit operator diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index d63a9ebb48..d9f582e669 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -1245,8 +1245,8 @@ void vectorMathRemainder(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam SColumnInfoData *pLeftCol = doVectorConvert(pLeft, &leftConvert); SColumnInfoData *pRightCol = doVectorConvert(pRight, &rightConvert); - _getBigintValue_fn_t getVectorBigintValueFnLeft = getVectorBigintValueFn(pLeftCol->info.type); - _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type); + _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type); + _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type); double *output = (double *)pOutputCol->pData; @@ -1257,17 +1257,17 @@ void vectorMathRemainder(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam continue; } - int64_t lx = getVectorBigintValueFnLeft(LEFT_COL, i); - int64_t rx = getVectorBigintValueFnRight(RIGHT_COL, i); - if (rx == 0) { + double lx = getVectorDoubleValueFnLeft(LEFT_COL, i); + double rx = getVectorDoubleValueFnRight(RIGHT_COL, i); + if (isnan(lx) || isinf(lx) || isnan(rx) || isinf(rx) || FLT_EQUAL(rx, 0)) { colDataAppendNULL(pOutputCol, i); continue; } - *output = lx % rx; + *output = lx - ((int64_t)(lx / rx)) * rx; } } else if (pLeft->numOfRows == 1) { - int64_t lx = getVectorBigintValueFnLeft(LEFT_COL, 0); + double lx = getVectorDoubleValueFnLeft(LEFT_COL, 0); if (IS_HELPER_NULL(pLeftCol, 0)) { // Set pLeft->numOfRows NULL value colDataAppendNNULL(pOutputCol, 0, pRight->numOfRows); } else { @@ -1277,18 +1277,18 @@ void vectorMathRemainder(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam continue; } - int64_t rx = getVectorBigintValueFnRight(RIGHT_COL, i); - if (rx == 0){ + double rx = getVectorDoubleValueFnRight(RIGHT_COL, i); + if (isnan(rx) || isinf(rx) || FLT_EQUAL(rx, 0)) { colDataAppendNULL(pOutputCol, i); continue; } - *output = lx % rx; + *output = lx - ((int64_t)(lx / rx)) * rx; } } } else if (pRight->numOfRows == 1) { - int64_t rx = getVectorBigintValueFnRight(RIGHT_COL, 0); - if (IS_HELPER_NULL(pRightCol, 0) || rx == 0) { // Set pLeft->numOfRows NULL value + double rx = getVectorDoubleValueFnRight(RIGHT_COL, 0); + if (IS_HELPER_NULL(pRightCol, 0) || FLT_EQUAL(rx, 0)) { // Set pLeft->numOfRows NULL value colDataAppendNNULL(pOutputCol, 0, pLeft->numOfRows); } else { for (; i >= 0 && i < pLeft->numOfRows; i += step, output += 1) { @@ -1297,8 +1297,13 @@ void vectorMathRemainder(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam continue; } - int64_t lx = getVectorBigintValueFnLeft(LEFT_COL, i); - *output = lx % rx; + double lx = getVectorDoubleValueFnLeft(LEFT_COL, i); + if (isnan(lx) || isinf(lx)) { + colDataAppendNULL(pOutputCol, i); + continue; + } + + *output = lx - ((int64_t)(lx / rx)) * rx; } } } @@ -1712,7 +1717,7 @@ _bin_scalar_fn_t getBinScalarOperatorFn(int32_t binFunctionId) { return vectorMathMultiply; case OP_TYPE_DIV: return vectorMathDivide; - case OP_TYPE_MOD: + case OP_TYPE_REM: return vectorMathRemainder; case OP_TYPE_MINUS: return vectorMathMinus; diff --git a/source/libs/scalar/test/scalar/scalarTests.cpp b/source/libs/scalar/test/scalar/scalarTests.cpp index 2912fce8be..8827c2f415 100644 --- a/source/libs/scalar/test/scalar/scalarTests.cpp +++ b/source/libs/scalar/test/scalar/scalarTests.cpp @@ -1061,7 +1061,7 @@ void makeJsonArrow(SSDataBlock **src, SNode **opNode, void *json, char *key){ void makeOperator(SNode **opNode, SArray *blockList, EOperatorType opType, int32_t rightType, void *rightData, bool isReverse){ int32_t resType = TSDB_DATA_TYPE_NULL; if(opType == OP_TYPE_ADD || opType == OP_TYPE_SUB || opType == OP_TYPE_MULTI || - opType == OP_TYPE_DIV || opType == OP_TYPE_MOD || opType == OP_TYPE_MINUS){ + opType == OP_TYPE_DIV || opType == OP_TYPE_REM || opType == OP_TYPE_MINUS){ resType = TSDB_DATA_TYPE_DOUBLE; }else if(opType == OP_TYPE_BIT_AND || opType == OP_TYPE_BIT_OR){ resType = TSDB_DATA_TYPE_BIGINT; @@ -1106,7 +1106,7 @@ void makeCalculate(void *json, void *key, int32_t rightType, void *rightData, do printf("result:NULL\n"); }else if(opType == OP_TYPE_ADD || opType == OP_TYPE_SUB || opType == OP_TYPE_MULTI || opType == OP_TYPE_DIV || - opType == OP_TYPE_MOD || opType == OP_TYPE_MINUS){ + opType == OP_TYPE_REM || opType == OP_TYPE_MINUS){ printf("op:%s,1result:%f,except:%f\n", gOptrStr[opType].str, *((double *)colDataGetData(column, 0)), exceptValue); ASSERT_TRUE(fabs(*((double *)colDataGetData(column, 0)) - exceptValue) < 0.0001); }else if(opType == OP_TYPE_BIT_AND || opType == OP_TYPE_BIT_OR){ @@ -1136,7 +1136,7 @@ TEST(columnTest, json_column_arith_op) { const int32_t len = 8; EOperatorType op[len] = {OP_TYPE_ADD, OP_TYPE_SUB, OP_TYPE_MULTI, OP_TYPE_DIV, - OP_TYPE_MOD, OP_TYPE_MINUS, OP_TYPE_BIT_AND, OP_TYPE_BIT_OR}; + OP_TYPE_REM, OP_TYPE_MINUS, OP_TYPE_BIT_AND, OP_TYPE_BIT_OR}; int32_t input[len] = {1, 8, 2, 2, 3, 0, -4, 9}; printf("--------------------json int-4 op {1, 8, 2, 2, 3, 0, -4, 9}--------------------\n"); -- GitLab