From 186c2694fec951a4e65fc644d01c9567493d040d Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Fri, 26 Nov 2021 12:55:47 +0800 Subject: [PATCH] fix the output format of json string; add "" --- src/client/src/tscUtil.c | 12 ++++++++++-- tests/pytest/stable/json_tag.py | 8 ++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 67da9885c0..8a92a982a7 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -763,19 +763,27 @@ static void setResRawPtrImpl(SSqlRes* pRes, SInternalField* pInfo, int32_t i, bo char* dst = pRes->buffer[i] + k * pInfo->field.bytes; char type = *p; char* realData = p + CHAR_BYTES; - if (type == TSDB_DATA_TYPE_NCHAR && isNull(realData, TSDB_DATA_TYPE_NCHAR)) { + if (type == TSDB_DATA_TYPE_JSON && isNull(realData, TSDB_DATA_TYPE_JSON)) { memcpy(dst, realData, varDataTLen(realData)); } else if (type == TSDB_DATA_TYPE_BINARY) { assert(*(uint32_t*)varDataVal(realData) == TSDB_DATA_JSON_null); // json null value assert(varDataLen(realData) == INT_BYTES); sprintf(varDataVal(dst), "%s", "null"); varDataSetLen(dst, strlen(varDataVal(dst))); - }else if (type == TSDB_DATA_TYPE_NCHAR) { + }else if (type == TSDB_DATA_TYPE_JSON) { int32_t length = taosUcs4ToMbs(varDataVal(realData), varDataLen(realData), varDataVal(dst)); varDataSetLen(dst, length); if (length == 0) { tscError("charset:%s to %s. val:%s convert failed.", DEFAULT_UNICODE_ENCODEC, tsCharset, (char*)p); } + }else if (type == TSDB_DATA_TYPE_NCHAR) { // value -> "value" + *(char*)varDataVal(dst) = '\"'; + int32_t length = taosUcs4ToMbs(varDataVal(realData), varDataLen(realData), POINTER_SHIFT(varDataVal(dst), CHAR_BYTES)); + *(char*)(POINTER_SHIFT(varDataVal(dst), length + CHAR_BYTES)) = '\"'; + varDataSetLen(dst, length + CHAR_BYTES*2); + if (length == 0) { + tscError("charset:%s to %s. val:%s convert failed.", DEFAULT_UNICODE_ENCODEC, tsCharset, (char*)p); + } }else if (type == TSDB_DATA_TYPE_DOUBLE) { double jsonVd = *(double*)(realData); sprintf(varDataVal(dst), "%.9lf", jsonVd); diff --git a/tests/pytest/stable/json_tag.py b/tests/pytest/stable/json_tag.py index d953526ec4..514d05c587 100644 --- a/tests/pytest/stable/json_tag.py +++ b/tests/pytest/stable/json_tag.py @@ -69,7 +69,7 @@ class TDTestCase: # test select condition tdSql.query("select jtag->'location' from db_json_tag_test.jsons1_2") - tdSql.checkData(0, 0, "beijing") + tdSql.checkData(0, 0, "\"beijing\"") tdSql.query("select jtag->'location' from db_json_tag_test.jsons1") tdSql.checkRows(4) @@ -105,7 +105,7 @@ class TDTestCase: tdSql.checkRows(0) tdSql.query("select jtag->'sex' from db_json_tag_test.jsons1 where jtag?'sex' or jtag?'num'") - tdSql.checkData(0, 0, "femail") + tdSql.checkData(0, 0, "\"femail\"") tdSql.checkRows(3) tdSql.query("select *,tbname from db_json_tag_test.jsons1 where jtag->'location'='beijing'") @@ -265,13 +265,13 @@ class TDTestCase: tdSql.execute("insert into db_json_tag_test.jsons2_1 values('2020-04-18 15:00:00.000', 1, false, 'json1')") tdSql.query("select 'sss',33,a.jtag->'loc' from db_json_tag_test.jsons2 a,db_json_tag_test.jsons3 b where a.ts=b.ts and a.jtag->'loc'=b.jtag->'loc'") tdSql.checkData(0, 0, "sss") - tdSql.checkData(0, 2, "fff") + tdSql.checkData(0, 2, "\"fff\"") # test group by & order by string tdSql.query("select avg(dataint),count(*) from db_json_tag_test.jsons1 group by jtag->'location' order by jtag->'location' desc") tdSql.checkData(1, 0, 2.5) tdSql.checkData(1, 1, 2) - tdSql.checkData(1, 2, "beijing") + tdSql.checkData(1, 2, "\"beijing\"") tdSql.checkData(2, 2, None) # test group by & order by int -- GitLab