From 1949ceb6dcfb7ad15568d5ca72253576f92372ea Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Wed, 15 Sep 2021 18:05:22 +0800 Subject: [PATCH] [TD-6645/enhance]: Add default type convertion for numeric values for JSON numbers. Interger number -> BIGINT (contains only digits and '+' '-') Floating number -> DOUBLE (including decimal point format or scientific notation format) --- src/client/inc/tscParseLine.h | 3 +++ src/client/src/tscParseLineProtocol.c | 4 ++-- src/client/src/tscParseOpenTSDB.c | 19 ++++++++++++++----- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/client/inc/tscParseLine.h b/src/client/inc/tscParseLine.h index 401dcafdfb..e36c0bbc0b 100644 --- a/src/client/inc/tscParseLine.h +++ b/src/client/inc/tscParseLine.h @@ -54,6 +54,9 @@ typedef struct { int tscSmlInsert(TAOS* taos, TAOS_SML_DATA_POINT* points, int numPoint, SSmlLinesInfo* info); bool checkDuplicateKey(char *key, SHashObj *pHash, SSmlLinesInfo* info); +bool isValidInteger(char *str); +bool isValidFloat(char *str); + int32_t isValidChildTableName(const char *pTbName, int16_t len); bool convertSmlValueType(TAOS_SML_KV *pVal, char *value, diff --git a/src/client/src/tscParseLineProtocol.c b/src/client/src/tscParseLineProtocol.c index 297f4680ee..229ab86ec5 100644 --- a/src/client/src/tscParseLineProtocol.c +++ b/src/client/src/tscParseLineProtocol.c @@ -1137,7 +1137,7 @@ static void escapeSpecialCharacter(uint8_t field, const char **pos) { *pos = cur; } -static bool isValidInteger(char *str) { +bool isValidInteger(char *str) { char *c = str; if (*c != '+' && *c != '-' && !isdigit(*c)) { return false; @@ -1152,7 +1152,7 @@ static bool isValidInteger(char *str) { return true; } -static bool isValidFloat(char *str) { +bool isValidFloat(char *str) { char *c = str; uint8_t has_dot, has_exp, has_sign; has_dot = 0; diff --git a/src/client/src/tscParseOpenTSDB.c b/src/client/src/tscParseOpenTSDB.c index 2ec4eda772..6735462f17 100644 --- a/src/client/src/tscParseOpenTSDB.c +++ b/src/client/src/tscParseOpenTSDB.c @@ -741,11 +741,20 @@ int32_t parseValueFromJSON(cJSON *root, TAOS_SML_KV *pVal, SSmlLinesInfo* info) break; } case cJSON_Number: { - //convert default JSON Number type to float - pVal->type = TSDB_DATA_TYPE_FLOAT; - pVal->length = (int16_t)tDataTypes[pVal->type].bytes; - pVal->value = tcalloc(pVal->length, 1); - *(float *)(pVal->value) = (float)(root->valuedouble); + //convert default JSON Number type to BIGINT/DOUBLE + if (isValidInteger(root->numberstring)) { + pVal->type = TSDB_DATA_TYPE_BIGINT; + pVal->length = (int16_t)tDataTypes[pVal->type].bytes; + pVal->value = tcalloc(pVal->length, 1); + *(int64_t *)(pVal->value) = (int64_t)(root->valuedouble); + } else if (isValidFloat(root->numberstring)) { + pVal->type = TSDB_DATA_TYPE_DOUBLE; + pVal->length = (int16_t)tDataTypes[pVal->type].bytes; + pVal->value = tcalloc(pVal->length, 1); + *(double *)(pVal->value) = (double)(root->valuedouble); + } else { + return TSDB_CODE_TSC_INVALID_JSON_TYPE; + } break; } case cJSON_String: { -- GitLab