From 059533b4f256afc1dbd90f2b0fc7509e4b671222 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Tue, 12 Oct 2021 10:48:52 +0800 Subject: [PATCH] TD-6129 add json nchar & json binary from python connector & fix json bug --- src/connector/python/taos/bind.py | 27 ++++++++++++++++++++++---- src/connector/python/taos/constants.py | 3 ++- src/connector/python/taos/cursor.py | 7 +++++-- src/connector/python/taos/field.py | 6 ++++-- src/tsdb/src/tsdbRead.c | 4 ++-- 5 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/connector/python/taos/bind.py b/src/connector/python/taos/bind.py index 05659714ef..23f797dc87 100644 --- a/src/connector/python/taos/bind.py +++ b/src/connector/python/taos/bind.py @@ -124,7 +124,7 @@ class TaosBind(ctypes.Structure): self.buffer_length = length self.length = pointer(c_size_t(self.buffer_length)) - def json(self, value): + def json_binary(self, value): buffer = None length = 0 if isinstance(value, str): @@ -134,7 +134,22 @@ class TaosBind(ctypes.Structure): else: buffer = value length = len(value) - self.buffer_type = FieldType.C_JSON + self.buffer_type = FieldType.C_JSON_BINARY + self.buffer = cast(buffer, c_void_p) + self.buffer_length = length + self.length = pointer(c_size_t(self.buffer_length)) + + def json_nchar(self, value): + buffer = None + length = 0 + if isinstance(value, str): + bytes = value.encode("utf-8") + buffer = create_string_buffer(bytes) + length = len(bytes) + else: + buffer = value + length = len(value) + self.buffer_type = FieldType.C_JSON_NCHAR self.buffer = cast(buffer, c_void_p) self.buffer_length = length self.length = pointer(c_size_t(self.buffer_length)) @@ -371,9 +386,13 @@ class TaosMultiBind(ctypes.Structure): self.buffer_type = FieldType.C_NCHAR self._str_to_buffer(values) - def json(self, values): + def json_binary(self, values): + self.buffer_type = FieldType.C_JSON_BINARY + self._str_to_buffer(values) + + def json_nchar(self, values): # type: (list[str]) -> None - self.buffer_type = FieldType.C_JSON + self.buffer_type = FieldType.C_JSON_NCHAR self._str_to_buffer(values) def tinyint_unsigned(self, values): diff --git a/src/connector/python/taos/constants.py b/src/connector/python/taos/constants.py index 34044a15fc..4d9591e0f3 100644 --- a/src/connector/python/taos/constants.py +++ b/src/connector/python/taos/constants.py @@ -25,7 +25,8 @@ class FieldType(object): C_SMALLINT_UNSIGNED = 12 C_INT_UNSIGNED = 13 C_BIGINT_UNSIGNED = 14 - C_JSON = 15 + C_JSON_BINARY = 15 + C_JSON_NCHAR = 16 # NULL value definition # NOTE: These values should change according to C definition in tsdb.h C_BOOL_NULL = 0x02 diff --git a/src/connector/python/taos/cursor.py b/src/connector/python/taos/cursor.py index a8d82bea2e..87d625fdc3 100644 --- a/src/connector/python/taos/cursor.py +++ b/src/connector/python/taos/cursor.py @@ -188,8 +188,11 @@ class TaosCursor(object): if dataType.upper() == "NCHAR": if self._description[col][1] == FieldType.C_NCHAR: return True - if dataType.upper() == "JSON": - if self._description[col][1] == FieldType.C_JSON: + if dataType.upper() == "JSON BINARY": + if self._description[col][1] == FieldType.C_JSON_BINARY: + return True + if dataType.upper() == "JSON NCHAR": + if self._description[col][1] == FieldType.C_JSON_NCHAR: return True return False diff --git a/src/connector/python/taos/field.py b/src/connector/python/taos/field.py index 9d1799d367..9743c44b1d 100644 --- a/src/connector/python/taos/field.py +++ b/src/connector/python/taos/field.py @@ -207,7 +207,8 @@ CONVERT_FUNC = { FieldType.C_SMALLINT_UNSIGNED: _crow_smallint_unsigned_to_python, FieldType.C_INT_UNSIGNED: _crow_int_unsigned_to_python, FieldType.C_BIGINT_UNSIGNED: _crow_bigint_unsigned_to_python, - FieldType.C_JSON: _crow_nchar_to_python, + FieldType.C_JSON_NCHAR: _crow_nchar_to_python, + FieldType.C_JSON_BINARY: _crow_binary_to_python, } CONVERT_FUNC_BLOCK = { @@ -225,7 +226,8 @@ CONVERT_FUNC_BLOCK = { FieldType.C_SMALLINT_UNSIGNED: _crow_smallint_unsigned_to_python, FieldType.C_INT_UNSIGNED: _crow_int_unsigned_to_python, FieldType.C_BIGINT_UNSIGNED: _crow_bigint_unsigned_to_python, - FieldType.C_JSON: _crow_nchar_to_python_block, + FieldType.C_JSON_NCHAR: _crow_nchar_to_python_block, + FieldType.C_JSON_BINARY: _crow_binary_to_python_block, } # Corresponding TAOS_FIELD structure in C diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index 8500d33cf1..f309375c8f 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -2678,7 +2678,7 @@ static int tsdbReadRowsFromCache(STableCheckInfo* pCheckInfo, TSKEY maxKey, int static int32_t getAllTableList(STable* pSuperTable, SArray* list) { STSchema* pTagSchema = tsdbGetTableTagSchema(pSuperTable); - if(pTagSchema->numOfCols == 1 && IS_JSON_DATA_TYPE(pTagSchema->columns[0].type)){ + if(pTagSchema && pTagSchema->numOfCols == 1 && IS_JSON_DATA_TYPE(pTagSchema->columns[0].type)){ SArray** pRecord = taosHashIterate(pSuperTable->jsonKeyMap, NULL); SArray* tablist = taosArrayInit(32, sizeof(JsonMapValue)); @@ -4081,7 +4081,7 @@ static FORCE_INLINE int32_t tsdbGetJsonTagDataFromId(void *param, int32_t id, ch if (id == TSDB_TBNAME_COLUMN_INDEX) { *data = TABLE_NAME(pTable); } else { - void* jsonData = getJsonTagValue(pTable, name, strlen(name), pTable->tagSchema->columns->type); + void* jsonData = getJsonTagValue(pTable, name, strlen(name), pTable->pSuper->tagSchema->columns->type); if (jsonData != NULL) jsonData += CHAR_BYTES; // jump type *data = jsonData; } -- GitLab