From aae8adeb5b2d82d70c5e6fe82a321ad02c032d32 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 6 Jul 2022 20:57:42 +0800 Subject: [PATCH] fix:timestamp precision in josn protocol --- source/client/src/clientSml.c | 25 ++-- source/client/test/smlTest.cpp | 208 ++++++++++++++++++++++++++++++++- 2 files changed, 220 insertions(+), 13 deletions(-) diff --git a/source/client/src/clientSml.c b/source/client/src/clientSml.c index 5039f8bbaf..b6972e4670 100644 --- a/source/client/src/clientSml.c +++ b/source/client/src/clientSml.c @@ -1570,41 +1570,40 @@ static int32_t smlParseTSFromJSONObj(SSmlHandle *info, cJSON *root, int64_t *tsV return TSDB_CODE_TSC_INVALID_TIME_STAMP; } + *tsVal = timeDouble; size_t typeLen = strlen(type->valuestring); if (typeLen == 1 && (type->valuestring[0] == 's' || type->valuestring[0] == 'S')) { // seconds - timeDouble = timeDouble * 1e9; + *tsVal = *tsVal * NANOSECOND_PER_SEC; + timeDouble = timeDouble * NANOSECOND_PER_SEC; if (smlDoubleToInt64OverFlow(timeDouble)) { smlBuildInvalidDataMsg(&info->msgBuf, "timestamp is too large", NULL); return TSDB_CODE_TSC_INVALID_TIME_STAMP; } - *tsVal = timeDouble; } else if (typeLen == 2 && (type->valuestring[1] == 's' || type->valuestring[1] == 'S')) { switch (type->valuestring[0]) { case 'm': case 'M': // milliseconds - timeDouble = timeDouble * 1e6; + *tsVal = *tsVal * NANOSECOND_PER_MSEC; + timeDouble = timeDouble * NANOSECOND_PER_MSEC; if (smlDoubleToInt64OverFlow(timeDouble)) { smlBuildInvalidDataMsg(&info->msgBuf, "timestamp is too large", NULL); return TSDB_CODE_TSC_INVALID_TIME_STAMP; } - *tsVal = timeDouble; break; case 'u': case 'U': // microseconds - timeDouble = timeDouble * 1e3; + *tsVal = *tsVal * NANOSECOND_PER_USEC; + timeDouble = timeDouble * NANOSECOND_PER_USEC; if (smlDoubleToInt64OverFlow(timeDouble)) { smlBuildInvalidDataMsg(&info->msgBuf, "timestamp is too large", NULL); return TSDB_CODE_TSC_INVALID_TIME_STAMP; } - *tsVal = timeDouble; break; case 'n': case 'N': - // nanoseconds - *tsVal = timeDouble; break; default: return TSDB_CODE_TSC_INVALID_JSON; @@ -1641,21 +1640,23 @@ static int32_t smlParseTSFromJSON(SSmlHandle *info, cJSON *root, SArray *cols) { if (timeDouble < 0) { return TSDB_CODE_TSC_INVALID_TIME_STAMP; } + uint8_t tsLen = smlGetTimestampLen((int64_t)timeDouble); + tsVal = (int64_t)timeDouble; if (tsLen == TSDB_TIME_PRECISION_SEC_DIGITS) { - timeDouble = timeDouble * 1e9; + tsVal = tsVal * NANOSECOND_PER_SEC; + timeDouble = timeDouble * NANOSECOND_PER_SEC; if (smlDoubleToInt64OverFlow(timeDouble)) { smlBuildInvalidDataMsg(&info->msgBuf, "timestamp is too large", NULL); return TSDB_CODE_TSC_INVALID_TIME_STAMP; } - tsVal = timeDouble; } else if (tsLen == TSDB_TIME_PRECISION_MILLI_DIGITS) { - timeDouble = timeDouble * 1e6; + tsVal = tsVal * NANOSECOND_PER_MSEC; + timeDouble = timeDouble * NANOSECOND_PER_MSEC; if (smlDoubleToInt64OverFlow(timeDouble)) { smlBuildInvalidDataMsg(&info->msgBuf, "timestamp is too large", NULL); return TSDB_CODE_TSC_INVALID_TIME_STAMP; } - tsVal = timeDouble; } else if (timeDouble == 0) { tsVal = taosGetTimestampNs(); } else { diff --git a/source/client/test/smlTest.cpp b/source/client/test/smlTest.cpp index 832564e0db..a6062efb98 100644 --- a/source/client/test/smlTest.cpp +++ b/source/client/test/smlTest.cpp @@ -1284,4 +1284,210 @@ TEST(testCase, sml_dup_time_Test) { ASSERT_EQ(taos_errno(pRes), 0); taos_free_result(pRes); } -*/ + + +TEST(testCase, sml_16960_Test) { +TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0); +ASSERT_NE(taos, nullptr); + +TAOS_RES* pRes = taos_query(taos, "create database if not exists d16368 schemaless 1"); +taos_free_result(pRes); + +pRes = taos_query(taos, "use d16368"); +taos_free_result(pRes); + +const char *sql[] = { + "[\n" + "{\n" + "\"timestamp\":\n" + "\n" + "{ \"value\": 1349020800000, \"type\": \"ms\" }\n" + ",\n" + "\"value\":\n" + "\n" + "{ \"value\": 830525384, \"type\": \"int\" }\n" + ",\n" + "\"tags\": {\n" + "\"id\": \"stb00_0\",\n" + "\"t0\":\n" + "\n" + "{ \"value\": 83972721, \"type\": \"int\" }\n" + ",\n" + "\"t1\":\n" + "\n" + "{ \"value\": 539147525, \"type\": \"int\" }\n" + ",\n" + "\"t2\":\n" + "\n" + "{ \"value\": 618258572, \"type\": \"int\" }\n" + ",\n" + "\"t3\":\n" + "\n" + "{ \"value\": -10536201, \"type\": \"int\" }\n" + ",\n" + "\"t4\":\n" + "\n" + "{ \"value\": 349227409, \"type\": \"int\" }\n" + ",\n" + "\"t5\":\n" + "\n" + "{ \"value\": 249347042, \"type\": \"int\" }\n" + "},\n" + "\"metric\": \"stb0\"\n" + "},\n" + "{\n" + "\"timestamp\":\n" + "\n" + "{ \"value\": 1349020800001, \"type\": \"ms\" }\n" + ",\n" + "\"value\":\n" + "\n" + "{ \"value\": -588348364, \"type\": \"int\" }\n" + ",\n" + "\"tags\": {\n" + "\"id\": \"stb00_0\",\n" + "\"t0\":\n" + "\n" + "{ \"value\": 83972721, \"type\": \"int\" }\n" + ",\n" + "\"t1\":\n" + "\n" + "{ \"value\": 539147525, \"type\": \"int\" }\n" + ",\n" + "\"t2\":\n" + "\n" + "{ \"value\": 618258572, \"type\": \"int\" }\n" + ",\n" + "\"t3\":\n" + "\n" + "{ \"value\": -10536201, \"type\": \"int\" }\n" + ",\n" + "\"t4\":\n" + "\n" + "{ \"value\": 349227409, \"type\": \"int\" }\n" + ",\n" + "\"t5\":\n" + "\n" + "{ \"value\": 249347042, \"type\": \"int\" }\n" + "},\n" + "\"metric\": \"stb0\"\n" + "},\n" + "{\n" + "\"timestamp\":\n" + "\n" + "{ \"value\": 1349020800002, \"type\": \"ms\" }\n" + ",\n" + "\"value\":\n" + "\n" + "{ \"value\": -370310823, \"type\": \"int\" }\n" + ",\n" + "\"tags\": {\n" + "\"id\": \"stb00_0\",\n" + "\"t0\":\n" + "\n" + "{ \"value\": 83972721, \"type\": \"int\" }\n" + ",\n" + "\"t1\":\n" + "\n" + "{ \"value\": 539147525, \"type\": \"int\" }\n" + ",\n" + "\"t2\":\n" + "\n" + "{ \"value\": 618258572, \"type\": \"int\" }\n" + ",\n" + "\"t3\":\n" + "\n" + "{ \"value\": -10536201, \"type\": \"int\" }\n" + ",\n" + "\"t4\":\n" + "\n" + "{ \"value\": 349227409, \"type\": \"int\" }\n" + ",\n" + "\"t5\":\n" + "\n" + "{ \"value\": 249347042, \"type\": \"int\" }\n" + "},\n" + "\"metric\": \"stb0\"\n" + "},\n" + "{\n" + "\"timestamp\":\n" + "\n" + "{ \"value\": 1349020800003, \"type\": \"ms\" }\n" + ",\n" + "\"value\":\n" + "\n" + "{ \"value\": -811250191, \"type\": \"int\" }\n" + ",\n" + "\"tags\": {\n" + "\"id\": \"stb00_0\",\n" + "\"t0\":\n" + "\n" + "{ \"value\": 83972721, \"type\": \"int\" }\n" + ",\n" + "\"t1\":\n" + "\n" + "{ \"value\": 539147525, \"type\": \"int\" }\n" + ",\n" + "\"t2\":\n" + "\n" + "{ \"value\": 618258572, \"type\": \"int\" }\n" + ",\n" + "\"t3\":\n" + "\n" + "{ \"value\": -10536201, \"type\": \"int\" }\n" + ",\n" + "\"t4\":\n" + "\n" + "{ \"value\": 349227409, \"type\": \"int\" }\n" + ",\n" + "\"t5\":\n" + "\n" + "{ \"value\": 249347042, \"type\": \"int\" }\n" + "},\n" + "\"metric\": \"stb0\"\n" + "},\n" + "{\n" + "\"timestamp\":\n" + "\n" + "{ \"value\": 1349020800004, \"type\": \"ms\" }\n" + ",\n" + "\"value\":\n" + "\n" + "{ \"value\": -330340558, \"type\": \"int\" }\n" + ",\n" + "\"tags\": {\n" + "\"id\": \"stb00_0\",\n" + "\"t0\":\n" + "\n" + "{ \"value\": 83972721, \"type\": \"int\" }\n" + ",\n" + "\"t1\":\n" + "\n" + "{ \"value\": 539147525, \"type\": \"int\" }\n" + ",\n" + "\"t2\":\n" + "\n" + "{ \"value\": 618258572, \"type\": \"int\" }\n" + ",\n" + "\"t3\":\n" + "\n" + "{ \"value\": -10536201, \"type\": \"int\" }\n" + ",\n" + "\"t4\":\n" + "\n" + "{ \"value\": 349227409, \"type\": \"int\" }\n" + ",\n" + "\"t5\":\n" + "\n" + "{ \"value\": 249347042, \"type\": \"int\" }\n" + "},\n" + "\"metric\": \"stb0\"\n" + "}\n" + "]" +}; + +pRes = taos_schemaless_insert(taos, (char**)sql, sizeof(sql)/sizeof(sql[0]), TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); +ASSERT_EQ(taos_errno(pRes), 0); +taos_free_result(pRes); +} +*/ \ No newline at end of file