From 30b8bd1b16c8f6aaba3ade41a781e378ce07c4be Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Sun, 5 Jun 2022 21:31:54 +0800 Subject: [PATCH] feat: add sort/group logic for json --- source/libs/scalar/src/filter.c | 3 +- source/libs/scalar/src/sclvector.c | 48 +++++++++++++------ .../libs/scalar/test/scalar/scalarTests.cpp | 10 ++-- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/source/libs/scalar/src/filter.c b/source/libs/scalar/src/filter.c index 195ec8a577..2a23cbeed5 100644 --- a/source/libs/scalar/src/filter.c +++ b/source/libs/scalar/src/filter.c @@ -30,7 +30,8 @@ OptrStr gOptrStr[] = { {OP_TYPE_MULTI, "*"}, {OP_TYPE_DIV, "/"}, {OP_TYPE_MOD, "%"}, - + {OP_TYPE_MINUS, "minus"}, + {OP_TYPE_ASSIGN, "assign"}, // bit operator {OP_TYPE_BIT_AND, "&"}, {OP_TYPE_BIT_OR, "|"}, diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index 85778e6a35..d63a9ebb48 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -452,13 +452,13 @@ double getVectorDoubleValue_JSON(void *src, int32_t index){ return out; } -void convertJsonValue(__compar_fn_t *fp, int32_t optr, int8_t typeLeft, int8_t typeRight, char **pLeftData, char **pRightData, void *pLeftOut, void *pRightOut, bool *isNull){ +bool convertJsonValue(__compar_fn_t *fp, int32_t optr, int8_t typeLeft, int8_t typeRight, char **pLeftData, char **pRightData, void *pLeftOut, void *pRightOut, bool *isNull){ if(optr == OP_TYPE_JSON_CONTAINS) { - return; + return true; } if(typeLeft != TSDB_DATA_TYPE_JSON && typeRight != TSDB_DATA_TYPE_JSON){ - return; + return true; } if(typeLeft == TSDB_DATA_TYPE_JSON){ @@ -469,15 +469,22 @@ void convertJsonValue(__compar_fn_t *fp, int32_t optr, int8_t typeLeft, int8_t t typeRight = **pRightData; (*pRightData) ++; } + + if(optr == OP_TYPE_LIKE || optr == OP_TYPE_NOT_LIKE || optr == OP_TYPE_MATCH || optr == OP_TYPE_NMATCH){ + if(typeLeft != TSDB_DATA_TYPE_NCHAR && typeLeft != TSDB_DATA_TYPE_BINARY){ + return false; + } + } + if(typeLeft == TSDB_DATA_TYPE_NULL || typeRight == TSDB_DATA_TYPE_NULL){ *isNull = true; - return; + return true; } int8_t type = vectorGetConvertType(typeLeft, typeRight); if(type == 0) { *fp = filterGetCompFunc(typeLeft, optr); - return; + return true; } *fp = filterGetCompFunc(type, optr); @@ -497,6 +504,7 @@ void convertJsonValue(__compar_fn_t *fp, int32_t optr, int8_t typeLeft, int8_t t convertNumberToNumber(*pRightData, pRightOut, typeRight, type); *pRightData = pRightOut; } + return true; } int32_t vectorConvertToVarData(const SScalarParam* pIn, SScalarParam* pOut, int16_t inType, int16_t outType) { @@ -1533,12 +1541,16 @@ void vectorCompareImpl(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam * int64_t leftOut = 0; int64_t rightOut = 0; bool isJsonnull = false; - convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData, &leftOut, &rightOut, &isJsonnull); + bool result = convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData, &leftOut, &rightOut, &isJsonnull); if(isJsonnull){ ASSERT(0); } - bool res = filterDoCompare(fp, optr, pLeftData, pRightData); - colDataAppendInt8(pOut->columnData, i, (int8_t*)&res); + if(!result){ + colDataAppendInt8(pOut->columnData, i, (int8_t*)&result); + }else{ + bool res = filterDoCompare(fp, optr, pLeftData, pRightData); + colDataAppendInt8(pOut->columnData, i, (int8_t*)&res); + } } } else if (pRight->numOfRows == 1) { ASSERT(pLeft->pHashFilter == NULL); @@ -1554,12 +1566,16 @@ void vectorCompareImpl(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam * int64_t leftOut = 0; int64_t rightOut = 0; bool isJsonnull = false; - convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData, &leftOut, &rightOut, &isJsonnull); + bool result = convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData, &leftOut, &rightOut, &isJsonnull); if(isJsonnull){ ASSERT(0); } - bool res = filterDoCompare(fp, optr, pLeftData, pRightData); - colDataAppendInt8(pOut->columnData, i, (int8_t*)&res); + if(!result){ + colDataAppendInt8(pOut->columnData, i, (int8_t*)&result); + }else{ + bool res = filterDoCompare(fp, optr, pLeftData, pRightData); + colDataAppendInt8(pOut->columnData, i, (int8_t*)&res); + } } } else if (pLeft->numOfRows == 1) { for (; i >= 0 && i < pRight->numOfRows; i += step) { @@ -1574,12 +1590,16 @@ void vectorCompareImpl(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam * int64_t leftOut = 0; int64_t rightOut = 0; bool isJsonnull = false; - convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData, &leftOut, &rightOut, &isJsonnull); + bool result = convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData, &leftOut, &rightOut, &isJsonnull); if(isJsonnull){ ASSERT(0); } - bool res = filterDoCompare(fp, optr, pLeftData, pRightData); - colDataAppendInt8(pOut->columnData, i, (int8_t*)&res); + if(!result){ + colDataAppendInt8(pOut->columnData, i, (int8_t*)&result); + }else{ + bool res = filterDoCompare(fp, optr, pLeftData, pRightData); + colDataAppendInt8(pOut->columnData, i, (int8_t*)&res); + } } } } diff --git a/source/libs/scalar/test/scalar/scalarTests.cpp b/source/libs/scalar/test/scalar/scalarTests.cpp index 502ec17ead..2912fce8be 100644 --- a/source/libs/scalar/test/scalar/scalarTests.cpp +++ b/source/libs/scalar/test/scalar/scalarTests.cpp @@ -1382,7 +1382,7 @@ TEST(columnTest, json_column_logic_op) { printf("--------------------json int-- -10 {1, 8, 2, 2, 3, 0, 0, 0, 0}-------------------\n"); key = "k6"; - bool eRes5[len+len1] = {false, false, true, true, false, true, false, true, true, false, false, false, true}; + bool eRes5[len+len1] = {false, false, true, true, false, true, false, true, true, false, false, false, false}; for(int i = 0; i < len; i++){ makeCalculate(row, key, TSDB_DATA_TYPE_INT, &input[i], eRes5[i], op[i], false); } @@ -1411,7 +1411,7 @@ TEST(columnTest, json_column_logic_op) { for(int i = len; i < len + len1; i++){ void* rightData = prepareNchar(inputNchar[i-len]); - makeCalculate(row, key, TSDB_DATA_TYPE_NCHAR, rightData, eRes5[i], op[i], false); + makeCalculate(row, key, TSDB_DATA_TYPE_NCHAR, rightData, eRes6[i], op[i], false); taosMemoryFree(rightData); } @@ -1430,7 +1430,7 @@ TEST(columnTest, json_column_logic_op) { for(int i = len; i < len + len1; i++){ void* rightData = prepareNchar(inputNchar[i-len]); - makeCalculate(row, key, TSDB_DATA_TYPE_NCHAR, rightData, eRes5[i], op[i], false); + makeCalculate(row, key, TSDB_DATA_TYPE_NCHAR, rightData, eRes7[i], op[i], false); taosMemoryFree(rightData); } @@ -1438,7 +1438,7 @@ TEST(columnTest, json_column_logic_op) { printf("--------------------json string-- 6.6hello {1, 8, 2, 2, 3, 0, 0, 0, 0}-------------------\n"); key = "k9"; - bool eRes8[len+len1] = {true, false, false, false, false, true, false, true, true, false, true, true, true}; + bool eRes8[len+len1] = {true, false, false, false, false, true, false, true, true, false, true, false, true}; for(int i = 0; i < len; i++){ makeCalculate(row, key, TSDB_DATA_TYPE_INT, &input[i], eRes8[i], op[i], false); } @@ -1449,7 +1449,7 @@ TEST(columnTest, json_column_logic_op) { for(int i = len; i < len + len1; i++){ void* rightData = prepareNchar(inputNchar[i-len]); - makeCalculate(row, key, TSDB_DATA_TYPE_NCHAR, rightData, eRes5[i], op[i], false); + makeCalculate(row, key, TSDB_DATA_TYPE_NCHAR, rightData, eRes8[i], op[i], false); taosMemoryFree(rightData); }