From 9453f80d658bbdbb85c1bce31769e311333bd4b8 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Mon, 16 May 2022 17:14:47 +0800 Subject: [PATCH] fix(index): fix index condition error --- source/libs/index/inc/indexComm.h | 8 +- source/libs/index/src/index.c | 2 +- source/libs/index/src/indexComm.c | 196 ++++++++++++++++---------- source/libs/index/test/.utilUT.cc.swn | Bin 20480 -> 0 bytes source/libs/index/test/jsonUT.cc | 2 +- source/libs/index/test/utilUT.cc | 24 ++-- 6 files changed, 140 insertions(+), 92 deletions(-) delete mode 100644 source/libs/index/test/.utilUT.cc.swn diff --git a/source/libs/index/inc/indexComm.h b/source/libs/index/inc/indexComm.h index 9b23e4eb44..3066fd1c2c 100644 --- a/source/libs/index/inc/indexComm.h +++ b/source/libs/index/inc/indexComm.h @@ -33,17 +33,17 @@ typedef enum { MATCH, CONTINUE, BREAK } TExeCond; typedef TExeCond (*_cache_range_compare)(void* a, void* b, int8_t type); -TExeCond tDoCommpare(__compar_fn_t func, int8_t comType, void* a, void* b); +TExeCond tCompare(__compar_fn_t func, int8_t cmpType, void* a, void* b, int8_t dType); +TExeCond tDoCompare(__compar_fn_t func, int8_t cmpType, void* a, void* b); _cache_range_compare indexGetCompare(RangeType ty); int32_t indexConvertData(void* src, int8_t type, void** dst); +int32_t indexConvertDataToStr(void* src, int8_t type, void** dst); int32_t indexGetDataByteLen(int8_t type); -int32_t indexMayFillNumbericData(void* number, int32_t tlen); - -int32_t indexMayUnfillNumbericData(void* number, int32_t tlen); +char* indexInt2str(int64_t val, char* dst, int radix); #ifdef __cplusplus } diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index b9df2e88ce..162d64c41c 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -271,7 +271,7 @@ SIndexTerm* indexTermCreate(int64_t suid, SIndexOperOnColumn oper, uint8_t colTy tm->nColName = nColName; char* buf = NULL; - int32_t len = indexConvertData((void*)colVal, INDEX_TYPE_GET_TYPE(colType), (void**)&buf); + int32_t len = indexConvertDataToStr((void*)colVal, INDEX_TYPE_GET_TYPE(colType), (void**)&buf); assert(len != -1); tm->colVal = buf; diff --git a/source/libs/index/src/indexComm.c b/source/libs/index/src/indexComm.c index 89f19a3b81..0c2e9e6b44 100644 --- a/source/libs/index/src/indexComm.c +++ b/source/libs/index/src/indexComm.c @@ -19,10 +19,38 @@ #include "tcoding.h" #include "tcompare.h" #include "tdataformat.h" +#include "ttypes.h" char JSON_COLUMN[] = "JSON"; char JSON_VALUE_DELIM = '&'; +char* indexInt2str(int64_t val, char* dst, int radix) { + char buffer[65]; + char* p; + int64_t new_val; + uint64_t uval = (uint64_t)val; + + if (radix < 0) { + if (val < 0) { + *dst++ = '-'; + uval = (uint64_t)0 - uval; /* Avoid integer overflow in (-val) for LLONG_MIN (BUG#31799). */ + } + } + p = &buffer[sizeof(buffer) - 1]; + *p = '\0'; + new_val = (int64_t)(uval / 10); + *--p = '0' + (char)(uval - (uint64_t)new_val * 10); + val = new_val; + + while (val != 0) { + new_val = val / 10; + *--p = '0' + (char)(val - new_val * 10); + val = new_val; + } + while ((*dst++ = *p++) != 0) + ; + return dst - 1; +} static __compar_fn_t indexGetCompar(int8_t type) { if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { return (__compar_fn_t)strcmp; @@ -31,41 +59,49 @@ static __compar_fn_t indexGetCompar(int8_t type) { } static TExeCond tCompareLessThan(void* a, void* b, int8_t type) { __compar_fn_t func = indexGetCompar(type); - - int32_t tlen = indexGetDataByteLen(type); - indexMayUnfillNumbericData(a, tlen); - indexMayUnfillNumbericData(b, tlen); - return tDoCommpare(func, QUERY_LESS_THAN, a, b); + return tCompare(func, QUERY_LESS_THAN, a, b, type); } static TExeCond tCompareLessEqual(void* a, void* b, int8_t type) { __compar_fn_t func = indexGetCompar(type); - - int32_t tlen = indexGetDataByteLen(type); - indexMayUnfillNumbericData(a, tlen); - indexMayUnfillNumbericData(b, tlen); - return tDoCommpare(func, QUERY_LESS_EQUAL, a, b); + return tCompare(func, QUERY_LESS_EQUAL, a, b, type); } static TExeCond tCompareGreaterThan(void* a, void* b, int8_t type) { __compar_fn_t func = indexGetCompar(type); - - int32_t tlen = indexGetDataByteLen(type); - indexMayUnfillNumbericData(a, tlen); - indexMayUnfillNumbericData(b, tlen); - return tDoCommpare(func, QUERY_GREATER_THAN, a, b); + return tCompare(func, QUERY_GREATER_THAN, a, b, type); } static TExeCond tCompareGreaterEqual(void* a, void* b, int8_t type) { __compar_fn_t func = indexGetCompar(type); - - int32_t tlen = indexGetDataByteLen(type); - indexMayUnfillNumbericData(a, tlen); - indexMayUnfillNumbericData(b, tlen); - return tDoCommpare(func, QUERY_GREATER_EQUAL, a, b); + return tCompare(func, QUERY_GREATER_EQUAL, a, b, type); } - -TExeCond tDoCommpare(__compar_fn_t func, int8_t comType, void* a, void* b) { +TExeCond tCompare(__compar_fn_t func, int8_t cmptype, void* a, void* b, int8_t dtype) { + if (dtype == TSDB_DATA_TYPE_BINARY || dtype == TSDB_DATA_TYPE_NCHAR) { + return tDoCompare(func, cmptype, a, b); + } +#if 1 + int8_t bytes = tDataTypes[dtype].bytes; + if (bytes == 1) { + int8_t va = taosStr2int64(a); + int8_t vb = taosStr2int64(b); + return tDoCompare(func, cmptype, &va, &vb); + } else if (bytes == 2) { + int16_t va = taosStr2int64(a); + int16_t vb = taosStr2int64(b); + return tDoCompare(func, cmptype, &va, &vb); + } else if (bytes == 4) { + int32_t va = taosStr2int64(a); + int32_t vb = taosStr2int64(b); + return tDoCompare(func, cmptype, &va, &vb); + } else { + int64_t va = taosStr2int64(a); + int64_t vb = taosStr2int64(b); + return tDoCompare(func, cmptype, &va, &vb); + } +#endif +} +TExeCond tDoCompare(__compar_fn_t func, int8_t comparType, void* a, void* b) { // optime later int32_t ret = func(a, b); - switch (comType) { + switch (comparType) { case QUERY_LESS_THAN: { if (ret < 0) return MATCH; } break; @@ -231,78 +267,92 @@ int32_t indexConvertData(void* src, int8_t type, void** dst) { break; } *dst = *dst - tlen; - - indexMayFillNumbericData(*dst, tlen); - - // if (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR && type != TSDB_DATA_TYPE_VARBINARY && - // type != TSDB_DATA_TYPE_VARCHAR) { uint8_t* p = *dst; - // for (int i = 0; i < tlen; i++) { - // if (p[i] == 0) { - // p[i] = (uint8_t)'0'; - // } - // } - //} + // indexMayFillNumbericData(*dst, tlen); return tlen; } -int32_t indexGetDataByteLen(int8_t type) { - int32_t tlen = -1; +int32_t indexConvertDataToStr(void* src, int8_t type, void** dst) { + int tlen = tDataTypes[type].bytes; + switch (type) { case TSDB_DATA_TYPE_TIMESTAMP: - tlen = sizeof(int64_t); + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(int64_t*)src, *dst, -1); break; case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_UTINYINT: - tlen = sizeof(uint8_t); + // tlen = taosEncodeFixedU8(NULL, *(uint8_t*)src); + //*dst = taosMemoryCalloc(1, tlen + 1); + // tlen = taosEncodeFixedU8(dst, *(uint8_t*)src); + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(uint8_t*)src, *dst, 1); break; case TSDB_DATA_TYPE_TINYINT: - tlen = sizeof(uint8_t); + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(int8_t*)src, *dst, 1); break; case TSDB_DATA_TYPE_SMALLINT: - tlen = sizeof(int16_t); + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(int16_t*)src, *dst, -1); break; case TSDB_DATA_TYPE_USMALLINT: - tlen = sizeof(uint16_t); + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(uint16_t*)src, *dst, -1); break; case TSDB_DATA_TYPE_INT: - tlen = sizeof(int32_t); - break; - case TSDB_DATA_TYPE_UINT: - tlen = sizeof(uint32_t); - break; - case TSDB_DATA_TYPE_BIGINT: - tlen = sizeof(int64_t); - break; - case TSDB_DATA_TYPE_UBIGINT: - tlen = sizeof(uint64_t); + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(int32_t*)src, *dst, -1); break; case TSDB_DATA_TYPE_FLOAT: - tlen = sizeof(float); + tlen = taosEncodeBinary(NULL, src, sizeof(float)); + *dst = taosMemoryCalloc(1, tlen + 1); + tlen = taosEncodeBinary(dst, src, sizeof(float)); + break; + case TSDB_DATA_TYPE_UINT: + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(uint32_t*)src, *dst, 1); + break; + case TSDB_DATA_TYPE_BIGINT: + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(int64_t*)src, *dst, 1); break; case TSDB_DATA_TYPE_DOUBLE: - tlen = sizeof(double); + tlen = taosEncodeBinary(NULL, src, sizeof(double)); + *dst = taosMemoryCalloc(1, tlen + 1); + tlen = taosEncodeBinary(dst, src, sizeof(double)); break; + case TSDB_DATA_TYPE_UBIGINT: + assert(0); + *dst = taosMemoryCalloc(1, sizeof(int64_t) + 1); + indexInt2str(*(uint64_t*)src, *dst, 1); + break; + case TSDB_DATA_TYPE_NCHAR: { + tlen = taosEncodeBinary(NULL, varDataVal(src), varDataLen(src)); + *dst = taosMemoryCalloc(1, tlen + 1); + tlen = taosEncodeBinary(dst, varDataVal(src), varDataLen(src)); + *dst = *dst - tlen; + + break; + } + case TSDB_DATA_TYPE_VARCHAR: { // TSDB_DATA_TYPE_BINARY +#if 1 + tlen = taosEncodeBinary(NULL, src, strlen(src)); + *dst = taosMemoryCalloc(1, tlen + 1); + tlen = taosEncodeBinary(dst, src, strlen(src)); + *dst = *dst - tlen; + break; +#endif + } + case TSDB_DATA_TYPE_VARBINARY: +#if 1 + tlen = taosEncodeBinary(NULL, src, strlen(src)); + *dst = taosMemoryCalloc(1, tlen + 1); + tlen = taosEncodeBinary(dst, src, strlen(src)); + *dst = *dst - tlen; + break; +#endif default: + TASSERT(0); break; } return tlen; } - -int32_t indexMayFillNumbericData(void* number, int32_t tlen) { - for (int i = 0; i < tlen; i++) { - int8_t* p = number; - if (p[i] == 0) { - p[i] = (uint8_t)'0'; - } - } - return 0; -} - -int32_t indexMayUnfillNumbericData(void* number, int32_t tlen) { - for (int i = 0; i < tlen; i++) { - int8_t* p = number; - if (p[i] == (uint8_t)'0') { - p[i] = 0; - } - } - return 0; -} diff --git a/source/libs/index/test/.utilUT.cc.swn b/source/libs/index/test/.utilUT.cc.swn deleted file mode 100644 index 3023c72b42057ca24f48699b8e9353645b647d6c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20480 zcmeI3ZEPGz8OJv@Eu!VovEw*NOXHVd+Cb~G=kwZLX4xQ%IqNDxpCHiG+YCR8&HU7gbaQR6g+1v>%XwwxohqrO>LhK&5=(H?zBU z=gs+EQWK}qp7hK4c6Vl<`OmYn&oeU_jco4UBsy0{H9m_q&Hmv2(HHufwWbF(%`#0} z9fyXuuu^(BZH(r_Ms`f!8MgJj z9WK~LdT4NEDpjq%;9Q_Upg^4p6zuWqIu~m3STrI_tXRHGym!e^orQzT0tEsE0tEsE z0tEsE0tEsE0tEsEE(HbbT$6SO4Y|y1%zf_nJm2>_+~0S(-=FuLZ*qUHcE1<76?pZy zhl8&`fk1&kfk1&kfk1&kfk1&kfk1&kfk1&kfk1&dP#`&`X^XgTLQVj%{_o}gFL3ZQ zJO&TLLvSw?UgHOU5 zSPECeYx6bjN%$!ogQIW<48vwv45G z*a6$18?J#Sc6z0Jx{Np%$1CGINkUld2zK;;&V!a3@ zZnUiAF45XzC(XS2d!ugO63SbtHW6)Es|issvUbO+ZMH~SR&47Ch4yrEE&?%#?_En< z#9k1hSjjDg{A6gnO*B&|y44s_HI18=2vM6bs3fwMuXQ4C?AFckki4u#gv)_|-7C?!bd^SWh+iaH=>f)+Yei}mY8Ox?if_lg>3 z))CviIdxQ#GG3pQ@##{=eNxu!c*n@{j*mME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