From ccbf0fdb110df14017067b203b55b59a1378cb57 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Thu, 2 Dec 2021 16:17:04 +0800 Subject: [PATCH] enable different value type of the same key in json --- src/client/src/tscSubquery.c | 8 +++++++- src/query/src/qExtbuffer.c | 8 +++++++- src/util/src/tcompare.c | 14 +++++++++----- tests/pytest/stable/json_tag.py | 2 ++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/client/src/tscSubquery.c b/src/client/src/tscSubquery.c index 9566de4636..e3384de827 100644 --- a/src/client/src/tscSubquery.c +++ b/src/client/src/tscSubquery.c @@ -756,9 +756,15 @@ int32_t tagValCompar(const void* p1, const void* p2) { }else if(!f1IsJsonNull && f2IsJsonNull) { return 1; } - if(*t1->tag != *t2->tag) { + if(*t1->tag != *t2->tag && !(IS_NUMERIC_TYPE(*t1->tag) && IS_NUMERIC_TYPE(*t2->tag))) { return 1; } + if(*t1->tag == TSDB_DATA_TYPE_BIGINT && *t2->tag == TSDB_DATA_TYPE_DOUBLE){ + DEFAULT_DOUBLE_COMP(GET_INT64_VAL(t1->tag + CHAR_BYTES), GET_DOUBLE_VAL(t2->tag + CHAR_BYTES)); + }else if(*t1->tag == TSDB_DATA_TYPE_DOUBLE && *t2->tag == TSDB_DATA_TYPE_BIGINT){ + DEFAULT_DOUBLE_COMP(GET_DOUBLE_VAL(t1->tag + CHAR_BYTES), GET_INT64_VAL(t2->tag + CHAR_BYTES)); + } + __compar_fn_t func = getComparFunc(t1->tag[0], 0); return func(t1->tag + CHAR_BYTES, t2->tag + CHAR_BYTES); } diff --git a/src/query/src/qExtbuffer.c b/src/query/src/qExtbuffer.c index 0fbf195421..42aeae008f 100644 --- a/src/query/src/qExtbuffer.c +++ b/src/query/src/qExtbuffer.c @@ -386,9 +386,15 @@ int32_t columnValueAscendingComparator(char *f1, char *f2, int32_t type, int32_t }else if(!f1IsJsonNull && f2IsJsonNull) { return 1; } - if(*f1 != *f2) { + + if(*f1 != *f2 && !(IS_NUMERIC_TYPE(*f1) && IS_NUMERIC_TYPE(*f2))) { return 1; } + if(*f1 == TSDB_DATA_TYPE_BIGINT && *f2 == TSDB_DATA_TYPE_DOUBLE){ + DEFAULT_DOUBLE_COMP(GET_INT64_VAL(f1 + CHAR_BYTES), GET_DOUBLE_VAL(f2 + CHAR_BYTES)); + }else if(*f1 == TSDB_DATA_TYPE_DOUBLE && *f2 == TSDB_DATA_TYPE_BIGINT){ + DEFAULT_DOUBLE_COMP(GET_DOUBLE_VAL(f1 + CHAR_BYTES), GET_INT64_VAL(f2 + CHAR_BYTES)); + } type = *f1; f1 += CHAR_BYTES; f2 += CHAR_BYTES; diff --git a/src/util/src/tcompare.c b/src/util/src/tcompare.c index f58b9c9cf7..d60dbe6189 100644 --- a/src/util/src/tcompare.c +++ b/src/util/src/tcompare.c @@ -220,9 +220,6 @@ int32_t compareLenPrefixedWStrDesc(const void* pLeft, const void* pRight) { } int32_t compareJsonVal(const void *pLeft, const void *pRight) { - if(*(char*)pLeft == TSDB_DATA_TYPE_BINARY){ // json null - return -1; - } const tVariant* right = pRight; if(right->nType != *(char*)pLeft && !(IS_NUMERIC_TYPE(right->nType) && IS_NUMERIC_TYPE(*(char*)pLeft))) return TSDB_DATA_JSON_CAN_NOT_COMPARE; @@ -244,8 +241,10 @@ int32_t compareJsonVal(const void *pLeft, const void *pRight) { return ret; } return (ret < 0) ? -1 : 1; + }else if(type == TSDB_DATA_TYPE_BINARY) { //json null + return 0; }else{ - assert(0); + assert(0); } } @@ -636,9 +635,14 @@ int32_t doCompare(const char* f1, const char* f2, int32_t type, size_t size) { }else if(!f1IsJsonNull && f2IsJsonNull) { return 1; } - if(*f1 != *f2) { + if(*f1 != *f2 && !(IS_NUMERIC_TYPE(*f1) && IS_NUMERIC_TYPE(*f2))) { return 1; } + if(*f1 == TSDB_DATA_TYPE_BIGINT && *f2 == TSDB_DATA_TYPE_DOUBLE){ + DEFAULT_DOUBLE_COMP(GET_INT64_VAL(f1 + CHAR_BYTES), GET_DOUBLE_VAL(f2 + CHAR_BYTES)); + }else if(*f1 == TSDB_DATA_TYPE_DOUBLE && *f2 == TSDB_DATA_TYPE_BIGINT){ + DEFAULT_DOUBLE_COMP(GET_DOUBLE_VAL(f1 + CHAR_BYTES), GET_INT64_VAL(f2 + CHAR_BYTES)); + } type = *f1; f1 += CHAR_BYTES; f2 += CHAR_BYTES; diff --git a/tests/pytest/stable/json_tag.py b/tests/pytest/stable/json_tag.py index e54832b953..cd86191a81 100644 --- a/tests/pytest/stable/json_tag.py +++ b/tests/pytest/stable/json_tag.py @@ -167,6 +167,8 @@ class TDTestCase: tdSql.checkData(0, 1, 2) tdSql.query("select * from jsons1 where jtag->'tag1'=10") tdSql.checkRows(0) + tdSql.query("select * from jsons1 where jtag->'tag1'<54") + tdSql.checkRows(3) # where json value is double tdSql.query("select * from jsons1 where jtag->'tag1'=1.232") tdSql.checkRows(1) -- GitLab